@@ -39,7 +39,6 @@ import type {
3939 EnvironmentId ,
4040 MessageId ,
4141 ModelSelection ,
42- ProviderApprovalDecision ,
4342 ProviderInteractionMode ,
4443 RuntimeMode ,
4544 RuntimeRequestId ,
@@ -52,7 +51,11 @@ import {
5251 appendCodexArtifactTemplateUsePrompt ,
5352 type CodexArtifactTemplate ,
5453} from "@t3tools/shared/codexArtifactTemplates" ;
55- import type { ThreadUserInputQuestion } from "@t3tools/client-runtime/state/thread-requests" ;
54+ import {
55+ declinePendingApprovals ,
56+ type ApprovalResponder ,
57+ type ThreadUserInputQuestion ,
58+ } from "@t3tools/client-runtime/state/thread-requests" ;
5659import {
5760 presentPendingBackgroundWork ,
5861 presentProviderGoal ,
@@ -199,6 +202,7 @@ export interface ThreadDetailScreenProps {
199202 | { readonly kind : "failed" ; readonly reason : string ; readonly onEditTask : ( ) => void }
200203 | null ;
201204 readonly activePendingApproval : PendingApproval | null ;
205+ readonly activePendingApprovals : ReadonlyArray < PendingApproval > ;
202206 readonly respondingApprovalId : RuntimeRequestId | null ;
203207 readonly activePendingUserInput : PendingUserInput | null ;
204208 readonly activePendingUserInputDrafts : Record < string , PendingUserInputDraftAnswer > ;
@@ -236,16 +240,16 @@ export interface ThreadDetailScreenProps {
236240 readonly onStopThread : ( ) => void ;
237241 /** Present while the thread's queue is held. */
238242 readonly onResumeQueue : ( ( ) => Promise < void > ) | null ;
239- readonly onSendMessage : ( followUp ?: ActiveTurnComposerAction ) => Promise < MessageId | null > ;
243+ readonly onSendMessage : (
244+ followUp ?: ActiveTurnComposerAction ,
245+ beforeDispatch ?: ( ) => Promise < boolean > ,
246+ ) => Promise < MessageId | null > ;
240247 readonly onReconnectEnvironment : ( ) => void ;
241248 readonly canSwitchThreadProvider : boolean ;
242249 readonly onUpdateThreadModelSelection : ( modelSelection : ModelSelection ) => void ;
243250 readonly onUpdateThreadRuntimeMode : ( runtimeMode : RuntimeMode ) => void ;
244251 readonly onUpdateThreadInteractionMode : ( interactionMode : ProviderInteractionMode ) => void ;
245- readonly onRespondToApproval : (
246- requestId : RuntimeRequestId ,
247- decision : ProviderApprovalDecision ,
248- ) => Promise < unknown > ;
252+ readonly onRespondToApproval : ApprovalResponder ;
249253 readonly onSelectUserInputOption : (
250254 requestId : RuntimeRequestId ,
251255 question : ThreadUserInputQuestion ,
@@ -1152,12 +1156,37 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
11521156 selectedThreadKey ,
11531157 ] ) ;
11541158
1155- const handleSendMessage = async ( followUp ?: ActiveTurnComposerAction ) => {
1159+ const activePendingApprovalsRef = useRef ( props . activePendingApprovals ) ;
1160+ useLayoutEffect ( ( ) => {
1161+ activePendingApprovalsRef . current = props . activePendingApprovals ;
1162+ } , [ props . activePendingApprovals ] ) ;
1163+ const handleSendMessage = async (
1164+ followUp ?: ActiveTurnComposerAction ,
1165+ stillSendable ?: ( ) => boolean ,
1166+ ) => {
11561167 const targetThreadKey = selectedThreadKey ;
11571168 const hasUserMessage = selectedThreadFeed . some (
11581169 ( entry ) => entry . type === "message" && entry . message . role === "user" ,
11591170 ) ;
1160- const messageId = await props . onSendMessage ( followUp ) ;
1171+ // Runs only once the send path has validated the draft and is about to deliver it, so an
1172+ // empty submit, a queued-message edit, or `/feedback` leaves approvals alone.
1173+ const approvalsToDecline = props . activePendingApprovals ;
1174+ const declineBeforeDispatch =
1175+ approvalsToDecline . length > 0 && props . activePendingUserInput === null
1176+ ? async ( ) =>
1177+ props . respondingApprovalId === null &&
1178+ ( await declinePendingApprovals ( approvalsToDecline , props . onRespondToApproval ) ) &&
1179+ selectedThreadKeyRef . current === targetThreadKey &&
1180+ // A draft edited or pasted into during the decline isn't what the user sent.
1181+ stillSendable ?.( ) !== false &&
1182+ // An approval that arrived during the decline would block the redirect; the draft
1183+ // stays so the next send declines it too.
1184+ ! activePendingApprovalsRef . current . some (
1185+ ( approval ) =>
1186+ ! approvalsToDecline . some ( ( declined ) => declined . requestId === approval . requestId ) ,
1187+ )
1188+ : undefined ;
1189+ const messageId = await props . onSendMessage ( followUp , declineBeforeDispatch ) ;
11611190 if ( messageId === null || selectedThreadKeyRef . current !== targetThreadKey ) {
11621191 return messageId ;
11631192 }
@@ -1568,7 +1597,11 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
15681597 canOperateThread = { props . canOperateThread }
15691598 reportedModelSelection = { reportedModelSelection }
15701599 editorRef = { composerEditorRef }
1571- placeholder = "Ask the repo agent, or run a command…"
1600+ placeholder = {
1601+ props . activePendingApproval
1602+ ? "Send a message to decline and redirect the agent"
1603+ : "Ask the repo agent, or run a command…"
1604+ }
15721605 contentMaxWidth = { contentMaxWidth }
15731606 connectionState = { props . connectionStateLabel }
15741607 environmentLabel = { props . environmentLabel }
@@ -1592,7 +1625,12 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
15921625 : null
15931626 }
15941627 draftKey = { props . composerDraftKey ?? undefined }
1595- followUpBehavior = { props . followUpBehavior }
1628+ followUpBehavior = {
1629+ props . activePendingApprovals . length > 0 &&
1630+ props . activePendingUserInput === null
1631+ ? "steer"
1632+ : props . followUpBehavior
1633+ }
15961634 canSteerActiveTurn = { props . canSteerActiveTurn }
15971635 hidden = { composerSlotHidden }
15981636 queuedEdit = {
0 commit comments