55 restorePlanFollowUpComposer ,
66 buildExpiredTerminalContextToastCopy ,
77 deriveComposerSendState ,
8- readFileAsDataUrl ,
98 revokeUserMessagePreviewUrls ,
109 boundAttachmentIds ,
1110 formatOutgoingPrompt ,
@@ -84,6 +83,7 @@ import { environmentServerConfigsAtom } from "../../state/server";
8483import { threadEnvironment } from "../../state/threads" ;
8584import { runMobileComposerTransition } from "./draftHeroTransition" ;
8685import { usePlanFollowUpSend } from "./useProposedPlanSend" ;
86+ import { useMultipleModelSend } from "./useMultipleModelSend" ;
8787import { type ComposerDispatchMode } from "@t3tools/client-runtime/state/composer-dispatch" ;
8888import {
8989 awaitAttachmentUploads ,
@@ -102,12 +102,11 @@ import {
102102 threadContextsOutsideEnvironment ,
103103} from "@t3tools/shared/composerContextReferences" ;
104104import {
105- draftFanoutSelectionsAtom ,
106105 draftSendState ,
107106 type MultipleModelTarget ,
108107 resolveMultipleModelTargets ,
108+ prepareTurnAttachments ,
109109 restoreComposerDraft ,
110- startFanoutThread ,
111110} from "./chatViewSend" ;
112111import type { EnvironmentServerConfig } from "@t3tools/client-runtime/connection" ;
113112import type {
@@ -215,9 +214,6 @@ interface ComposerSendInput {
215214 readonly setFeedbackSubmissionsByThreadKey : Dispatch <
216215 SetStateAction < Record < string , ReadonlyArray < CodexFeedbackSubmission > > >
217216 > ;
218- readonly setMultipleModelSelections : (
219- selections : SetStateAction < ReadonlyArray < ModelSelection > | null > ,
220- ) => void ;
221217 readonly setThreadError : ReturnType < typeof makeThreadErrorSetter > ;
222218 readonly setKeepFullHistory : ( threadKey : string , keep : boolean ) => void ;
223219 readonly anchorNewTurn : TimelineFollow [ "anchorNewTurn" ] ;
@@ -310,7 +306,6 @@ export function useComposerSend(input: ComposerSendInput) {
310306 setSetupQueueSendThreadKey,
311307 setOptimisticUserMessages,
312308 setFeedbackSubmissionsByThreadKey,
313- setMultipleModelSelections,
314309 setThreadError,
315310 setKeepFullHistory,
316311 anchorNewTurn,
@@ -337,6 +332,19 @@ export function useComposerSend(input: ComposerSendInput) {
337332 const setComposerDraftReviewComments = useComposerDraftStore ( ( store ) => store . setReviewComments ) ;
338333 const setComposerDraftThreadContexts = useComposerDraftStore ( ( store ) => store . setThreadContexts ) ;
339334 const clearComposerDraftContent = useComposerDraftStore ( ( store ) => store . clearComposerContent ) ;
335+ const sendToMultipleModels = useMultipleModelSend ( {
336+ environmentId,
337+ routeThreadKey,
338+ currentRouteThreadKeyRef,
339+ draftId,
340+ composerDraftTarget,
341+ composerRef,
342+ promptRef,
343+ runtimeMode,
344+ startFromOrigin,
345+ resetLocalDispatch,
346+ setThreadError,
347+ } ) ;
340348 const submitPlanFollowUp = usePlanFollowUpSend ( {
341349 environmentId,
342350 routeThreadKey,
@@ -356,10 +364,7 @@ export function useComposerSend(input: ComposerSendInput) {
356364 anchorNewTurn,
357365 persistThreadSettingsForNextTurn,
358366 } ) ;
359- const fanoutSelectionsAtom = draftFanoutSelectionsAtom ( routeThreadKey ) ;
360- const readMultipleModelSelections = ( ) => appAtomRegistry . get ( fanoutSelectionsAtom ) ;
361- const { sendInFlight : sendInFlightRef , uncertainSubmissions : uncertainMultipleSubmissionsRef } =
362- draftSendState ( routeThreadKey ) ;
367+ const { sendInFlight : sendInFlightRef } = draftSendState ( routeThreadKey ) ;
363368 const composerSendGenerationRef = useRef ( 0 ) ;
364369 const environmentUnavailableSendToastSlotRef = useRef ( 0 ) ;
365370 // A send awaits uploads before it declines, so it reads the approvals pending by then.
@@ -1029,207 +1034,26 @@ export function useComposerSend(input: ComposerSendInput) {
10291034 ) ;
10301035 }
10311036
1032- const turnAttachmentsPromise = Promise . all (
1033- composerAttachmentsSnapshot . map ( async ( attachment ) => {
1034- if ( turnUsesAttachmentUploads ) {
1035- const uploaded = getUploadedAttachments ( { environmentId, images : [ attachment ] } ) ?. [ 0 ] ;
1036- if ( ! uploaded ) {
1037- throw new Error ( `Attachment '${ attachment . name } ' did not finish uploading.` ) ;
1038- }
1039- return uploaded ;
1040- }
1041- if ( attachment . type !== "image" ) {
1042- throw new Error ( "This server does not support file attachments." ) ;
1043- }
1044- return {
1045- type : "image" as const ,
1046- id : attachment . id ,
1047- name : attachment . name ,
1048- mimeType : attachment . mimeType ,
1049- sizeBytes : attachment . sizeBytes ,
1050- dataUrl : await readFileAsDataUrl ( attachment . file ) ,
1051- ...( attachment . source ? { source : attachment . source } : { } ) ,
1052- } ;
1053- } ) ,
1054- ) ;
1037+ const turnAttachmentsPromise = prepareTurnAttachments ( {
1038+ environmentId,
1039+ attachments : composerAttachmentsSnapshot ,
1040+ usesAttachmentUploads : turnUsesAttachmentUploads ,
1041+ } ) ;
10551042 if ( multipleModelSelections !== null ) {
1056- const failedSelections : ModelSelection [ ] = [ ] ;
1057- let clearedDraft = false ;
1058- let releasedComposer = false ;
1059- let canRestoreDraft = ( ) => false ;
1060- let startedCount = 0 ;
1061- // Anything thrown here (an attachment the server can no longer take)
1062- // fails every selection; the draft restore below still runs.
1063- const sendResult = await settlePromise ( async ( ) => {
1064- const attachments = await turnAttachmentsPromise ;
1065- const fileBlockReason = readLiveAttachmentCapabilities ( ) . fileBlockReason ;
1066- if ( fileBlockReason !== null ) throw new Error ( fileBlockReason ) ;
1067- const context = buildOutgoingMessageContext (
1068- boundAttachmentIds ( attachments , composerAttachmentsSnapshot ) ,
1069- ) ;
1070- const title = deriveThreadTitleSeed ( {
1071- text : messageTextForSend ,
1072- attachments : composerAttachmentsSnapshot ,
1073- } ) ;
1074- promptRef . current = "" ;
1075- clearComposerDraftContent ( composerDraftTarget ) ;
1076- composerRef . current ?. resetCursorState ( ) ;
1077- clearedDraft = true ;
1078- const clearedDraftSnapshot = useComposerDraftStore
1079- . getState ( )
1080- . getComposerDraft ( composerDraftTarget ) ;
1081- const submittedSelections = readMultipleModelSelections ( ) ;
1082- canRestoreDraft = ( ) =>
1083- currentRouteThreadKeyRef . current === routeThreadKey &&
1084- composerSendGenerationRef . current === sendGeneration &&
1085- useComposerDraftStore . getState ( ) . getComposerDraft ( composerDraftTarget ) ===
1086- clearedDraftSnapshot &&
1087- readMultipleModelSelections ( ) === submittedSelections ;
1088- setThreadError ( threadIdForSend , null ) ;
1089- const starts = Promise . all (
1090- multipleTargets . map ( async ( target ) => {
1091- const started = await startFanoutThread ( {
1092- retryKey : JSON . stringify ( [
1093- routeThreadKey ,
1094- target . selection . instanceId ,
1095- target . selection . model ,
1096- ] ) ,
1097- model : target . selection . model ,
1098- uncertainSubmissions : uncertainMultipleSubmissionsRef . current ,
1099- start : ( targetThreadId ) => {
1100- const supportsInlineMessageContext =
1101- appAtomRegistry . get ( environmentServerConfigsAtom ) . get ( environmentId ) ?. environment
1102- . capabilities . inlineMessageContext === true ;
1103- return startThreadTurn ( {
1104- environmentId,
1105- input : {
1106- threadId : targetThreadId ,
1107- message : {
1108- messageId : newMessageId ( ) ,
1109- role : "user" ,
1110- attachments,
1111- ...withMessageContext ( {
1112- text : target . text ,
1113- context,
1114- supportsInlineMessageContext,
1115- } ) ,
1116- } ,
1117- modelSelection : target . selection ,
1118- titleSeed : title ,
1119- runtimeMode,
1120- interactionMode : target . interactionMode ,
1121- bootstrap : {
1122- createThread : {
1123- projectId : activeProject . id ,
1124- title,
1125- modelSelection : target . selection ,
1126- runtimeMode,
1127- interactionMode : target . interactionMode ,
1128- branch : activeThreadBranch ,
1129- worktreePath : null ,
1130- createdAt : messageCreatedAt ,
1131- } ,
1132- prepareWorktree : {
1133- projectCwd : activeProject . workspaceRoot ,
1134- baseBranch : activeThreadBranch ! ,
1135- requireWorktree : true ,
1136- ...( startFromOrigin ? { startFromOrigin : true } : { } ) ,
1137- } ,
1138- runSetupScript : true ,
1139- } ,
1140- createdAt : messageCreatedAt ,
1141- } ,
1142- } ) ;
1143- } ,
1144- openThread : ( threadId ) =>
1145- void navigate ( {
1146- to : "/$environmentId/$threadId" ,
1147- params : buildThreadRouteParams ( scopeThreadRef ( environmentId , threadId ) ) ,
1148- } ) ,
1149- } ) ;
1150- if ( started ) startedCount += 1 ;
1151- else failedSelections . push ( target . selection ) ;
1152- } ) ,
1153- ) ;
1154- sendInFlightRef . current = false ;
1155- resetLocalDispatch ( ) ;
1156- releasedComposer = true ;
1157- await starts ;
1158- if ( startedCount > 0 ) {
1159- toastManager . add (
1160- stackedThreadToast ( {
1161- type : "success" ,
1162- title : `Started ${ startedCount } ${ startedCount === 1 ? "thread" : "threads" } in background` ,
1163- } ) ,
1164- ) ;
1165- }
1166- if ( failedSelections . length === 0 && turnUsesAttachmentUploads ) {
1167- releaseDraftAttachments ( composerAttachmentsSnapshot ) ;
1168- }
1043+ await sendToMultipleModels ( {
1044+ threadId : threadIdForSend ,
1045+ project : activeProject ,
1046+ branch : activeThreadBranch ! ,
1047+ selections : multipleModelSelections ,
1048+ targets : multipleTargets ,
1049+ sendSnapshot,
1050+ createdAt : messageCreatedAt ,
1051+ attachmentsPromise : turnAttachmentsPromise ,
1052+ usesAttachmentUploads : turnUsesAttachmentUploads ,
1053+ readFileBlockReason : ( ) => readLiveAttachmentCapabilities ( ) . fileBlockReason ,
1054+ buildOutgoingMessageContext,
1055+ isCurrentSend : ( ) => composerSendGenerationRef . current === sendGeneration ,
11691056 } ) ;
1170- if ( sendResult . _tag === "Failure" ) {
1171- const error = squashAtomCommandFailure ( sendResult ) ;
1172- failedSelections . push ( ...multipleModelSelections ) ;
1173- setThreadError (
1174- threadIdForSend ,
1175- error instanceof Error ? error . message : "Failed to send messages." ,
1176- ) ;
1177- }
1178- const restoreFailedDraft = ( ) => {
1179- setMultipleModelSelections ( failedSelections ) ;
1180- if ( clearedDraft ) {
1181- restoreComposerDraft ( composerDraftTarget , sendSnapshot ) ;
1182- if ( composerRef . current && currentRouteThreadKeyRef . current === routeThreadKey ) {
1183- promptRef . current = messageTextForSend ;
1184- composerRef . current . resetCursorState ( {
1185- cursor : collapseExpandedComposerCursor ( messageTextForSend , messageTextForSend . length ) ,
1186- prompt : messageTextForSend ,
1187- detectTrigger : true ,
1188- } ) ;
1189- }
1190- }
1191- } ;
1192- if ( failedSelections . length > 0 ) {
1193- if ( canRestoreDraft ( ) && composerRef . current ) {
1194- restoreFailedDraft ( ) ;
1195- } else if ( clearedDraft ) {
1196- const recoveryToastId = toastManager . add (
1197- stackedThreadToast ( {
1198- type : "error" ,
1199- title : "A background prompt could not be sent" ,
1200- description :
1201- "Your newer draft is unchanged. Restore the failed prompt when this composer is empty." ,
1202- timeout : 0 ,
1203- actionProps : {
1204- children : "Restore prompt" ,
1205- onClick : ( ) => {
1206- if (
1207- ! draftId ||
1208- ! useComposerDraftStore . getState ( ) . getDraftSession ( draftId ) ||
1209- sendInFlightRef . current ||
1210- composerDraftHasUserContent (
1211- useComposerDraftStore . getState ( ) . getComposerDraft ( composerDraftTarget ) ,
1212- )
1213- ) {
1214- toastManager . update ( recoveryToastId , {
1215- description :
1216- "Return to the original draft and send or clear its current prompt before restoring." ,
1217- } ) ;
1218- return ;
1219- }
1220- restoreFailedDraft ( ) ;
1221- void navigate ( { to : "/draft/$draftId" , params : { draftId } } ) ;
1222- toastManager . close ( recoveryToastId ) ;
1223- } ,
1224- } ,
1225- } ) ,
1226- ) ;
1227- }
1228- }
1229- if ( ! releasedComposer ) {
1230- sendInFlightRef . current = false ;
1231- resetLocalDispatch ( ) ;
1232- }
12331057 return ;
12341058 }
12351059 const optimisticAttachments = optimisticMessageAttachments ( composerAttachmentsSnapshot ) ;
0 commit comments