Open History to see the last commit
1import {
2 type EnvironmentId,
3 type ModelSelection,
4 type RuntimeMode,
5 type ThreadId,
6} from "@t3tools/contracts";
7import { scopeThreadRef } from "@t3tools/client-runtime/environment";
8import { deriveThreadTitleSeed } from "@t3tools/client-runtime/operations";
9import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
10import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
11import { useNavigate } from "@tanstack/react-router";
12import { newMessageId } from "~/lib/utils";
13import { collapseExpandedComposerCursor } from "../../composer-logic";
14import {
15 type ComposerThreadTarget,
16 composerDraftHasUserContent,
17 type DraftId,
18 useComposerDraftStore,
19} from "../../composerDraftStore";
20import type { ComposerHandleRef } from "../../composerHandleContext";
21import { releaseDraftAttachments } from "../../lib/attachmentUploadQueue";
22import type { buildMessageContext } from "../../lib/composerContextRecords";
23import { appAtomRegistry } from "../../rpc/atomRegistry";
24import { threadEnvironment } from "../../state/threads";
25import { useOrchestrationCommand } from "../../state/use-orchestration-command";
26import { buildThreadRouteParams } from "../../threadRoutes";
27import { DEFAULT_INTERACTION_MODE } from "../../types";
28import { stackedThreadToast, toastManager } from "../ui/toast";
29import {
30 boundAttachmentIds,
31 type ComposerSendSnapshot,
32 type makeThreadErrorSetter,
33 withMessageContext,
34} from "../ChatView.logic";
35import {
36 draftFanoutSelectionsAtom,
37 draftSendState,
38 type MultipleModelTarget,
39 type prepareTurnAttachments,
40 restoreComposerDraft,
41 startFanoutThread,
42} from "./chatViewSend";
43import type { useLocalDispatchState } from "./useLocalDispatchState";
44
45interface MultipleModelSendInput {
46 readonly environmentId: EnvironmentId;
47 readonly routeThreadKey: string;
48 readonly currentRouteThreadKeyRef: { readonly current: string | null };
49 readonly draftId: DraftId | null;
50 readonly composerDraftTarget: ComposerThreadTarget;
51 readonly composerRef: ComposerHandleRef;
52 readonly promptRef: { current: string };
53 readonly runtimeMode: RuntimeMode;
54 readonly startFromOrigin: boolean;
55 readonly resetLocalDispatch: ReturnType<typeof useLocalDispatchState>["resetLocalDispatch"];
56 readonly setThreadError: ReturnType<typeof makeThreadErrorSetter>;
57}
58
59/**
60 * Starts one background thread per selected model, each in its own worktree. Failed models
61 * stay selected and the prompt comes back, either straight into this composer or, when the
62 * user has moved on, through a "Restore prompt" toast.
63 */
64export function useMultipleModelSend(input: MultipleModelSendInput) {
65 const {
66 environmentId,
67 routeThreadKey,
68 currentRouteThreadKeyRef,
69 draftId,
70 composerDraftTarget,
71 composerRef,
72 promptRef,
73 runtimeMode,
74 startFromOrigin,
75 resetLocalDispatch,
76 setThreadError,
77 } = input;
78 const navigate = useNavigate();
79 const startThreadTurn = useOrchestrationCommand(threadEnvironment.startTurn, {
80 reportFailure: false,
81 });
82 const clearComposerDraftContent = useComposerDraftStore((store) => store.clearComposerContent);
83 const fanoutSelectionsAtom = draftFanoutSelectionsAtom(routeThreadKey);
84 const { sendInFlight: sendInFlightRef, uncertainSubmissions: uncertainSubmissionsRef } =
85 draftSendState(routeThreadKey);
86
87 return async ({
88 threadId,
89 project,
90 branch,
91 selections,
92 targets,
93 sendSnapshot,
94 createdAt,
95 attachmentsPromise,
96 usesAttachmentUploads,
97 readFileBlockReason,
98 buildOutgoingMessageContext,
99 isCurrentSend,
100 }: {
101 readonly threadId: ThreadId;
102 readonly project: EnvironmentProject;
103 readonly branch: string;
104 readonly selections: ReadonlyArray<ModelSelection>;
105 readonly targets: ReadonlyArray<MultipleModelTarget>;
106 readonly sendSnapshot: ComposerSendSnapshot;
107 readonly createdAt: string;
108 readonly attachmentsPromise: ReturnType<typeof prepareTurnAttachments>;
109 readonly usesAttachmentUploads: boolean;
110 readonly readFileBlockReason: () => string | null;
111 readonly buildOutgoingMessageContext: (
112 attachmentIds: ReadonlyArray<string>,
113 ) => ReturnType<typeof buildMessageContext>;
114 /** False once a newer send from this composer started. */
115 readonly isCurrentSend: () => boolean;
116 }) => {
117 const { attachments: attachmentsSnapshot, messageText } = sendSnapshot;
118 const failedSelections: ModelSelection[] = [];
119 let clearedDraft = false;
120 let releasedComposer = false;
121 let canRestoreDraft = () => false;
122 let startedCount = 0;
123 // Anything thrown here (an attachment the server can no longer take)
124 // fails every selection; the draft restore below still runs.
125 const sendResult = await settlePromise(async () => {
126 const attachments = await attachmentsPromise;
127 const fileBlockReason = readFileBlockReason();
128 if (fileBlockReason !== null) throw new Error(fileBlockReason);
129 const context = buildOutgoingMessageContext(
130 boundAttachmentIds(attachments, attachmentsSnapshot),
131 );
132 const title = deriveThreadTitleSeed({
133 text: messageText,
134 attachments: attachmentsSnapshot,
135 });
136 promptRef.current = "";
137 clearComposerDraftContent(composerDraftTarget);
138 composerRef.current?.resetCursorState();
139 clearedDraft = true;
140 const clearedDraftSnapshot = useComposerDraftStore
141 .getState()
142 .getComposerDraft(composerDraftTarget);
143 const submittedSelections = appAtomRegistry.get(fanoutSelectionsAtom);
144 canRestoreDraft = () =>
145 currentRouteThreadKeyRef.current === routeThreadKey &&
146 isCurrentSend() &&
147 useComposerDraftStore.getState().getComposerDraft(composerDraftTarget) ===
148 clearedDraftSnapshot &&
149 appAtomRegistry.get(fanoutSelectionsAtom) === submittedSelections;
150 setThreadError(threadId, null);
151 const starts = Promise.all(
152 targets.map(async (target) => {
153 const started = await startFanoutThread({
154 retryKey: JSON.stringify([
155 routeThreadKey,
156 target.selection.instanceId,
157 target.selection.model,
158 ]),
159 model: target.selection.model,
160 uncertainSubmissions: uncertainSubmissionsRef.current,
161 start: (targetThreadId) => {
162 return startThreadTurn({
163 environmentId,
164 input: {
165 threadId: targetThreadId,
166 message: {
167 messageId: newMessageId(),
168 role: "user",
169 attachments,
170 ...withMessageContext({
171 text: target.text,
172 context,
173 }),
174 },
175 modelSelection: target.selection,
176 titleSeed: title,
177 runtimeMode,
178 interactionMode: DEFAULT_INTERACTION_MODE,
179 bootstrap: {
180 createThread: {
181 projectId: project.id,
182 title,
183 modelSelection: target.selection,
184 runtimeMode,
185 interactionMode: DEFAULT_INTERACTION_MODE,
186 branch,
187 worktreePath: null,
188 createdAt: createdAt,
189 },
190 prepareWorktree: {
191 projectCwd: project.workspaceRoot,
192 baseBranch: branch,
193 requireWorktree: true,
194 ...(startFromOrigin ? { startFromOrigin: true } : {}),
195 },
196 runSetupScript: true,
197 },
198 createdAt: createdAt,
199 },
200 });
201 },
202 openThread: (threadId) =>
203 void navigate({
204 to: "/$environmentId/$threadId",
205 params: buildThreadRouteParams(scopeThreadRef(environmentId, threadId)),
206 }),
207 });
208 if (started) startedCount += 1;
209 else failedSelections.push(target.selection);
210 }),
211 );
212 sendInFlightRef.current = false;
213 resetLocalDispatch();
214 releasedComposer = true;
215 await starts;
216 if (startedCount > 0) {
217 toastManager.add(
218 stackedThreadToast({
219 type: "success",
220 title: `Started ${startedCount} ${startedCount === 1 ? "thread" : "threads"} in background`,
221 }),
222 );
223 }
224 if (failedSelections.length === 0 && usesAttachmentUploads) {
225 releaseDraftAttachments(attachmentsSnapshot);
226 }
227 });
228 if (sendResult._tag === "Failure") {
229 const error = squashAtomCommandFailure(sendResult);
230 failedSelections.push(...selections);
231 setThreadError(threadId, error instanceof Error ? error.message : "Failed to send messages.");
232 }
233 const restoreFailedDraft = () => {
234 appAtomRegistry.set(fanoutSelectionsAtom, failedSelections);
235 if (clearedDraft) {
236 restoreComposerDraft(composerDraftTarget, sendSnapshot);
237 if (composerRef.current && currentRouteThreadKeyRef.current === routeThreadKey) {
238 promptRef.current = sendSnapshot.draftText;
239 composerRef.current.resetCursorState({
240 cursor: collapseExpandedComposerCursor(
241 sendSnapshot.draftText,
242 sendSnapshot.draftText.length,
243 ),
244 prompt: sendSnapshot.draftText,
245 detectTrigger: true,
246 });
247 }
248 }
249 };
250 if (failedSelections.length > 0) {
251 if (canRestoreDraft() && composerRef.current) {
252 restoreFailedDraft();
253 } else if (clearedDraft) {
254 const recoveryToastId = toastManager.add(
255 stackedThreadToast({
256 type: "error",
257 title: "A background prompt could not be sent",
258 description:
259 "Your newer draft is unchanged. Restore the failed prompt when this composer is empty.",
260 timeout: 0,
261 actionProps: {
262 children: "Restore prompt",
263 onClick: () => {
264 if (
265 !draftId ||
266 !useComposerDraftStore.getState().getDraftSession(draftId) ||
267 sendInFlightRef.current ||
268 composerDraftHasUserContent(
269 useComposerDraftStore.getState().getComposerDraft(composerDraftTarget),
270 )
271 ) {
272 toastManager.update(recoveryToastId, {
273 description:
274 "Return to the original draft and send or clear its current prompt before restoring.",
275 });
276 return;
277 }
278 restoreFailedDraft();
279 void navigate({ to: "/draft/$draftId", params: { draftId } });
280 toastManager.close(recoveryToastId);
281 },
282 },
283 }),
284 );
285 }
286 }
287 if (!releasedComposer) {
288 sendInFlightRef.current = false;
289 resetLocalDispatch();
290 }
291 };
292}