Open History to see the last commit
1import type { EnvironmentServerConfig } from "@t3tools/client-runtime/connection";
2import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
3import type { Dispatch } from "react";
4import type { ComposerThreadTarget } from "../../composerDraftStore";
5import type { ComposerHandleRef } from "../../composerHandleContext";
6import {
7 isThreadWokeUnseen,
8 branchMismatchKey,
9 isBranchMismatchDismissedForSession,
10 shouldShowBranchMismatchBanner,
11 chatActionErrorMessage,
12 makeThreadErrorSetter,
13} from "../ChatView.logic";
14import { useStableCallback } from "../../hooks/useStableCallback";
15import { useDeadlinePassed, useNowAfter } from "../../hooks/useDeadline";
16import type { usePendingThreadRequests } from "./usePendingThreadRequests";
17import {
18 AuthOrchestrationOperateScope,
19 AuthSourceControlWriteScope,
20 type EnvironmentId,
21 type ScopedThreadRef,
22 type ThreadId,
23 ProviderInteractionMode,
24 RuntimeMode,
25} from "@t3tools/contracts";
26import {
27 effectiveSnoozed,
28 snoozeWakeLabel,
29 threadWokeAt,
30} from "@t3tools/client-runtime/state/thread-settled";
31import {
32 BACKGROUND_WORK_STOP_RETRY_AFTER_MS,
33 type BackgroundWorkStopRequest,
34 isBackgroundWorkStopPending,
35} from "@t3tools/client-runtime/state/thread-execution";
36import { useAcknowledgeThreadWoke, useThreadActions } from "../../hooks/useThreadActions";
37import { settleAndAdvance } from "../../settleAndAdvance";
38import type { ChatShortcutCommands } from "./useChatShortcuts";
39import { resolveThreadLastVisitedAt } from "../Sidebar.logic";
40import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
41import { type SetStateAction, useRef, useState } from "react";
42import { useNavigate, useRouter } from "@tanstack/react-router";
43import {
44 isAtomCommandInterrupted,
45 squashAtomCommandFailure,
46 type AtomCommandResult,
47} from "@t3tools/client-runtime/state/runtime";
48import { useUiStateStore } from "../../uiStateStore";
49import { type ChatMessage, DEFAULT_INTERACTION_MODE } from "../../types";
50import { AlarmClockIcon, CheckCircle2Icon } from "lucide-react";
51import { stackedThreadToast, toastManager } from "../ui/toast";
52import { titleRegenerationToast } from "../threadTitleRegenerationToast";
53import { newMessageId } from "~/lib/utils";
54import { buildThreadRouteParams } from "../../threadRoutes";
55import { composerDraftHasUserContent, useComposerDraftStore } from "../../composerDraftStore";
56import { useEnvironmentScope } from "~/state/session";
57import { threadEnvironment } from "../../state/threads";
58import { vcsEnvironment } from "../../state/vcs";
59import { useChildThreadInputs, readThreadShell, waitForThreadShell } from "../../state/entities";
60import { serverEnvironment } from "../../state/server";
61import { environmentShell } from "../../state/shell";
62import { ThreadStatusLine } from "./ThreadStatusLine";
63import { formatRelativeTimeLabel } from "../../timestampFormat";
64import { appAtomRegistry } from "../../rpc/atomRegistry";
65import { readEnvironmentScope } from "../../state/session";
66import { useAtomCommand } from "../../state/use-atom-command";
67import { useOrchestrationCommand } from "../../state/use-orchestration-command";
68import {
69 backgroundWorkNowItem,
70 childInputNowItem,
71 goalNowItem,
72 runStopNowItem,
73} from "./ThreadActivityBanners";
74import { useForceStopThread } from "./useForceStopThread";
75import { useAtomValue } from "@effect/atom-react";
76import type { RunStopPresentation } from "@t3tools/client-runtime/state/thread-execution";
77import { Atom } from "effect/reactivity";
78import { environmentThreadDetails } from "../../state/threads";
79import { resolveThreadOpenRoute } from "../../lib/sideChatPane";
80import type { useLocalDispatchState } from "./useLocalDispatchState";
81const RELATED_THREAD_SHELL_WAIT_MS = 1_000;
82const NO_RUN_STOP_ATOM = Atom.make<RunStopPresentation | null>(null).pipe(
83 Atom.withLabel("web-thread-run-stop:empty"),
84);
85
86type PendingThreadRequests = ReturnType<typeof usePendingThreadRequests>;
87type LocalDispatchState = ReturnType<typeof useLocalDispatchState>;
88
89interface ThreadLifecycleInput {
90 readonly environmentId: EnvironmentId;
91 readonly routeThreadKey: string;
92 readonly currentRouteThreadKeyRef: { readonly current: string | null };
93 readonly canOperateThread: boolean;
94 readonly clientSettingsHydrated: boolean;
95 readonly activeThread: EnvironmentThreadShell | undefined;
96 readonly activeThreadId: ThreadId | null;
97 readonly activeThreadRef: ScopedThreadRef | null;
98 /** Null until the thread's detail may load; see resolveThreadDetailRef. */
99 readonly threadDetailRef: ScopedThreadRef | null;
100 readonly activeThreadKey: string | null;
101 readonly activeThreadShell: EnvironmentThreadShell | null;
102 /** The usage-limit recovery banner shows this snooze and its Wake now. */
103 readonly limitRecoveryHoldsSnooze: boolean;
104 readonly activeLatestRun: EnvironmentThreadShell["latestRun"];
105 readonly activeProjectCwd: string | null;
106 readonly serverConfig: EnvironmentServerConfig | null;
107 readonly runtimeMode: RuntimeMode;
108 readonly isWorking: boolean;
109 /** Also true while the server prepares, starts, or queues a run this client did not send. */
110 readonly runIsLive: boolean;
111 readonly pendingBackgroundTasks: Parameters<typeof backgroundWorkNowItem>[0]["tasks"];
112 readonly turnItems: Parameters<typeof backgroundWorkNowItem>[0]["turnItems"];
113 readonly activePendingApproval: PendingThreadRequests["activePendingApproval"];
114 readonly activePendingUserInput: PendingThreadRequests["activePendingUserInput"];
115 readonly localCheckoutBranchMismatch: {
116 readonly threadBranch: string;
117 readonly currentBranch: string;
118 } | null;
119 readonly gitStatusQuery: { readonly refresh: () => void };
120 readonly nowMinute: string;
121 readonly composerRef: ComposerHandleRef;
122 readonly composerDraftTarget: ComposerThreadTarget;
123 readonly sendInFlightRef: { current: boolean };
124 readonly setOptimisticUserMessages: Dispatch<SetStateAction<ChatMessage[]>>;
125 readonly setThreadError: ReturnType<typeof makeThreadErrorSetter>;
126 readonly clearUsageLimitsFor: (threadKey: string) => void;
127 readonly beginLocalDispatch: LocalDispatchState["beginLocalDispatch"];
128 readonly resetLocalDispatch: LocalDispatchState["resetLocalDispatch"];
129 readonly scrollToEnd: (animated?: boolean) => void;
130 readonly persistThreadSettingsForNextTurn: (input: {
131 threadId: ThreadId;
132 createdAt: string;
133 branch?: string;
134 runtimeMode: RuntimeMode;
135 interactionMode: ProviderInteractionMode;
136 }) => Promise<AtomCommandResult<void, unknown>>;
137}
138
139/**
140 * Thread state the chat shows and changes outside a normal send: snooze and settle with
141 * their status line, the local checkout's branch, background work, child-thread and goal
142 * banners, opening related threads, and standalone slash commands.
143 */
144export function useThreadLifecycle(input: ThreadLifecycleInput) {
145 const {
146 environmentId,
147 routeThreadKey,
148 currentRouteThreadKeyRef,
149 canOperateThread,
150 clientSettingsHydrated,
151 activeThread,
152 activeThreadId,
153 activeThreadRef,
154 threadDetailRef,
155 activeThreadKey,
156 activeThreadShell,
157 activeLatestRun,
158 limitRecoveryHoldsSnooze,
159 activeProjectCwd,
160 serverConfig,
161 runtimeMode,
162 isWorking,
163 runIsLive,
164 pendingBackgroundTasks,
165 turnItems,
166 activePendingApproval,
167 activePendingUserInput,
168 localCheckoutBranchMismatch,
169 gitStatusQuery,
170 nowMinute,
171 composerRef,
172 composerDraftTarget,
173 sendInFlightRef,
174 setOptimisticUserMessages,
175 setThreadError,
176 clearUsageLimitsFor,
177 beginLocalDispatch,
178 resetLocalDispatch,
179 scrollToEnd,
180 persistThreadSettingsForNextTurn,
181 } = input;
182 const navigate = useNavigate();
183 const router = useRouter();
184 const { settleThread, pinThread, confirmAndUnpinThread } = useThreadActions();
185 const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
186 const switchGitRef = useAtomCommand(vcsEnvironment.switchRef, { reportFailure: false });
187 const updateThreadMetadata = useOrchestrationCommand(threadEnvironment.updateMetadata, {
188 reportFailure: false,
189 });
190 const regenerateThreadTitle = useOrchestrationCommand(threadEnvironment.regenerateTitle, {
191 reportFailure: false,
192 });
193 const startThreadTurn = useOrchestrationCommand(threadEnvironment.startTurn, {
194 reportFailure: false,
195 });
196 const interruptThreadTurn = useOrchestrationCommand(threadEnvironment.interruptTurn, {
197 reportFailure: false,
198 });
199
200 const serverConfigLoaded = serverConfig !== null;
201 const activeThreadPinned = serverConfigLoaded && activeThreadShell?.pinnedAt != null;
202 // A clock read in render would stay frozen in the compiler's cache, so the
203 // snooze checks read a snapshot that moves when the wake time changes or passes.
204 const snoozeClock = new Date(
205 useNowAfter(Date.parse(activeThreadShell?.snoozedUntil ?? "") || null),
206 ).toISOString();
207 const activeThreadSnoozed =
208 activeThreadShell !== null &&
209 serverConfigLoaded &&
210 effectiveSnoozed(activeThreadShell, { now: snoozeClock });
211 const activeThreadWokeAt =
212 activeThreadShell !== null && serverConfigLoaded
213 ? threadWokeAt(activeThreadShell, { now: snoozeClock })
214 : null;
215 const acknowledgeThreadWoke = useAcknowledgeThreadWoke();
216 const acknowledgeActiveThreadWoke = () => {
217 if (activeThreadRef === null || activeThreadWokeAt === null) return;
218 acknowledgeThreadWoke(activeThreadRef, activeThreadWokeAt);
219 };
220 const acknowledgeActiveThreadWokeStable = useStableCallback(acknowledgeActiveThreadWoke);
221 const activeThreadLocalVisitedAt = useUiStateStore((store) =>
222 activeThreadKey === null ? undefined : store.threadLastVisitedAtById[activeThreadKey],
223 );
224 const activeThreadLastVisitedAt = resolveThreadLastVisitedAt(
225 activeThreadShell?.lastVisitedAt,
226 activeThreadLocalVisitedAt,
227 );
228 const activeThreadWokeVisible = isThreadWokeUnseen({
229 wokeAt: activeThreadWokeAt,
230 settled: activeThreadShell?.settledOverride === "settled",
231 lastVisitedAt: activeThreadLastVisitedAt,
232 latestRunCompletedAt: activeLatestRun?.completedAt ?? null,
233 });
234 const activeThreadSettled =
235 serverConfigLoaded && activeThreadShell?.settledOverride === "settled";
236 const unsettleThreadMutation = useOrchestrationCommand(threadEnvironment.unsettle, {
237 reportFailure: false,
238 });
239 const [unsettlingThreadKey, setUnsettlingThreadKey] = useState<string | null>(null);
240 const isUnsettling = unsettlingThreadKey !== null && unsettlingThreadKey === activeThreadKey;
241 const handleUnsettleActiveThread = async () => {
242 if (
243 !activeThreadRef ||
244 !readEnvironmentScope(activeThreadRef.environmentId, AuthOrchestrationOperateScope)
245 )
246 return;
247 const threadKey = scopedThreadKey(activeThreadRef);
248 setUnsettlingThreadKey(threadKey);
249 // Commands settle into a result, so this always runs.
250 const result = await unsettleThreadMutation({
251 environmentId: activeThreadRef.environmentId,
252 input: { threadId: activeThreadRef.threadId, reason: "user" },
253 });
254 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
255 const error = squashAtomCommandFailure(result);
256 toastManager.add(
257 stackedThreadToast({
258 type: "error",
259 title: "Failed to un-settle thread",
260 description: error instanceof Error ? error.message : "An error occurred.",
261 }),
262 );
263 }
264 setUnsettlingThreadKey((current) => (current === threadKey ? null : current));
265 };
266 const handleUnsettleActiveThreadStable = useStableCallback(handleUnsettleActiveThread);
267 const unsnoozeThreadMutation = useOrchestrationCommand(threadEnvironment.unsnooze, {
268 reportFailure: false,
269 });
270 const [unsnoozingThreadKey, setUnsnoozingThreadKey] = useState<string | null>(null);
271 const isUnsnoozing = unsnoozingThreadKey !== null && unsnoozingThreadKey === activeThreadKey;
272 const handleUnsnoozeActiveThread = async () => {
273 if (
274 !activeThreadRef ||
275 !readEnvironmentScope(activeThreadRef.environmentId, AuthOrchestrationOperateScope)
276 )
277 return;
278 const threadKey = scopedThreadKey(activeThreadRef);
279 setUnsnoozingThreadKey(threadKey);
280 // Commands settle into a result, so this always runs.
281 const recovery = activeThreadShell?.limitRecovery;
282 const recoveryOwnsSnooze =
283 recovery?.snooze === true &&
284 recovery.runId === activeThreadShell?.latestRun?.runId &&
285 activeThreadShell?.snoozedUntil != null &&
286 Date.parse(activeThreadShell.snoozedUntil) === Date.parse(recovery.resetAt);
287 const result = recoveryOwnsSnooze
288 ? await updateThreadMetadata({
289 environmentId: activeThreadRef.environmentId,
290 input: {
291 threadId: activeThreadRef.threadId,
292 limitRecovery: { runId: recovery.runId, resetAt: recovery.resetAt, snooze: false },
293 },
294 })
295 : await unsnoozeThreadMutation({
296 environmentId: activeThreadRef.environmentId,
297 input: { threadId: activeThreadRef.threadId, reason: "user" },
298 });
299 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
300 const error = squashAtomCommandFailure(result);
301 toastManager.add(
302 stackedThreadToast({
303 type: "error",
304 title: "Failed to wake thread",
305 description: error instanceof Error ? error.message : "An error occurred.",
306 }),
307 );
308 }
309 setUnsnoozingThreadKey((current) => (current === threadKey ? null : current));
310 };
311 const handleUnsnoozeActiveThreadStable = useStableCallback(handleUnsnoozeActiveThread);
312 const [restoringBranchThreadKeys, setRestoringBranchThreadKeys] = useState<ReadonlySet<string>>(
313 () => new Set(),
314 );
315 const isRestoringThreadBranch =
316 activeThreadKey !== null && restoringBranchThreadKeys.has(activeThreadKey);
317 const [revealedBranchMismatchKey, setRevealedBranchMismatchKey] = useState<string | null>(null);
318 const [, setBranchMismatchDismissTick] = useState(0);
319 const composerHasDraftContent = useComposerDraftStore((store) =>
320 composerDraftHasUserContent(store.getComposerDraft(composerDraftTarget)),
321 );
322 const activeBranchMismatchKey = branchMismatchKey(
323 activeThread?.id ?? null,
324 localCheckoutBranchMismatch,
325 );
326 const showBranchMismatchBanner = shouldShowBranchMismatchBanner({
327 hasMismatch: localCheckoutBranchMismatch !== null,
328 isDismissed: isBranchMismatchDismissedForSession(activeBranchMismatchKey),
329 composerHasContent: composerHasDraftContent,
330 wasShownForCurrentMismatch:
331 revealedBranchMismatchKey !== null && revealedBranchMismatchKey === activeBranchMismatchKey,
332 });
333 const nextRevealedBranchMismatchKey = showBranchMismatchBanner
334 ? activeBranchMismatchKey
335 : revealedBranchMismatchKey !== activeBranchMismatchKey
336 ? null
337 : revealedBranchMismatchKey;
338 if (nextRevealedBranchMismatchKey !== revealedBranchMismatchKey) {
339 setRevealedBranchMismatchKey(nextRevealedBranchMismatchKey);
340 }
341 const handleSwitchCheckoutToThread = async () => {
342 if (!canWriteSourceControl) return;
343 if (
344 !activeProjectCwd ||
345 !activeThread ||
346 !localCheckoutBranchMismatch ||
347 isRestoringThreadBranch
348 ) {
349 return;
350 }
351 const threadKey = scopedThreadKey(scopeThreadRef(environmentId, activeThread.id));
352 const finishRestore = () =>
353 setRestoringBranchThreadKeys((current) => {
354 const next = new Set(current);
355 next.delete(threadKey);
356 return next;
357 });
358 setRestoringBranchThreadKeys((current) => new Set(current).add(threadKey));
359 const checkoutResult = await switchGitRef({
360 environmentId,
361 input: {
362 cwd: activeProjectCwd,
363 refName: localCheckoutBranchMismatch.threadBranch,
364 },
365 });
366 if (checkoutResult._tag === "Failure") {
367 finishRestore();
368 if (!isAtomCommandInterrupted(checkoutResult)) {
369 toastManager.add(
370 stackedThreadToast({
371 type: "error",
372 title: "Failed to switch checkout",
373 description: chatActionErrorMessage(squashAtomCommandFailure(checkoutResult)),
374 }),
375 );
376 }
377 return;
378 }
379
380 if (!readEnvironmentScope(environmentId, AuthSourceControlWriteScope)) {
381 finishRestore();
382 return;
383 }
384 const nextBranch = checkoutResult.value.refName ?? localCheckoutBranchMismatch.threadBranch;
385 if (nextBranch !== activeThread.branch) {
386 const updateResult = await updateThreadMetadata({
387 environmentId,
388 input: { threadId: activeThread.id, branch: nextBranch, worktreePath: null },
389 });
390 if (updateResult._tag === "Failure") {
391 finishRestore();
392 if (!isAtomCommandInterrupted(updateResult)) {
393 toastManager.add(
394 stackedThreadToast({
395 type: "error",
396 title: "Checkout switched, but the thread could not be updated",
397 description: chatActionErrorMessage(squashAtomCommandFailure(updateResult)),
398 }),
399 );
400 }
401 gitStatusQuery.refresh();
402 return;
403 }
404 }
405 gitStatusQuery.refresh();
406 finishRestore();
407 if (currentRouteThreadKeyRef.current === routeThreadKey) {
408 window.requestAnimationFrame(() => {
409 if (currentRouteThreadKeyRef.current === routeThreadKey) composerRef.current?.focusAtEnd();
410 });
411 }
412 };
413 const handleSwitchCheckoutToThreadStable = useStableCallback(() => {
414 void handleSwitchCheckoutToThread();
415 });
416 const activeBackgroundTasks = activeThread ? pendingBackgroundTasks : [];
417 const [backgroundWorkStops, setBackgroundWorkStops] = useState<
418 Record<string, BackgroundWorkStopRequest | undefined>
419 >({});
420 const backgroundWorkStop = backgroundWorkStops[`${environmentId}:${activeThreadId}`] ?? null;
421 const isStoppingBackgroundWork = isBackgroundWorkStopPending(
422 backgroundWorkStop,
423 `${environmentId}:${activeThreadId}`,
424 pendingBackgroundTasks,
425 useDeadlinePassed(backgroundWorkStop?.retryAt ?? null),
426 );
427 const handleStopBackgroundWork = async () => {
428 if (
429 isStoppingBackgroundWork ||
430 !activeThread ||
431 !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)
432 )
433 return;
434 const request: BackgroundWorkStopRequest = {
435 threadKey: `${environmentId}:${activeThread.id}`,
436 taskIds: pendingBackgroundTasks.map((task) => task.taskId),
437 retryAt: null,
438 };
439 setBackgroundWorkStops((current) => ({ ...current, [request.threadKey]: request }));
440 const result = await interruptThreadTurn({
441 environmentId,
442 input: { threadId: activeThread.id },
443 });
444 setBackgroundWorkStops((current) =>
445 current[request.threadKey] !== request
446 ? current
447 : {
448 ...current,
449 [request.threadKey]:
450 result._tag === "Failure"
451 ? undefined
452 : { ...request, retryAt: Date.now() + BACKGROUND_WORK_STOP_RETRY_AFTER_MS },
453 },
454 );
455 if (result._tag === "Failure") {
456 if (!isAtomCommandInterrupted(result)) {
457 const error = squashAtomCommandFailure(result);
458 setThreadError(
459 activeThread.id,
460 error instanceof Error ? error.message : "Failed to stop background work.",
461 );
462 }
463 }
464 };
465 const handleStopBackgroundWorkStable = useStableCallback(handleStopBackgroundWork);
466 const openRelatedThreadGenerationRef = useRef(0);
467 // A task delegated to a linked environment opens there; the default is this thread's own.
468 const onOpenRelatedThread = async (
469 threadId: ThreadId,
470 threadEnvironmentId: EnvironmentId = environmentId,
471 ) => {
472 const generation = ++openRelatedThreadGenerationRef.current;
473 const threadRef = scopeThreadRef(threadEnvironmentId, threadId);
474 const isEnvironmentLive = () =>
475 appAtomRegistry.get(environmentShell.stateValueAtom(threadEnvironmentId)).status === "live";
476 // The shell can leave idle subagent children out, so absence is not deletion there: the
477 // route decides from the thread's own detail.
478 const shellOmitsChildren =
479 appAtomRegistry.get(serverEnvironment.configValueAtom(threadEnvironmentId))
480 ?.shellThreadFamilies === true;
481 // A just-launched child can reach this timeline before its shell syncs.
482 if (!shellOmitsChildren && isEnvironmentLive() && readThreadShell(threadRef) === null) {
483 const location = router.latestLocation;
484 await waitForThreadShell(threadRef, RELATED_THREAD_SHELL_WAIT_MS);
485 if (
486 generation !== openRelatedThreadGenerationRef.current ||
487 currentRouteThreadKeyRef.current !== routeThreadKey ||
488 router.latestLocation !== location
489 ) {
490 return;
491 }
492 }
493 const shell = readThreadShell(threadRef);
494 if (shell === null ? !shellOmitsChildren && isEnvironmentLive() : shell.deletedAt != null) {
495 toastManager.add({ type: "info", title: "This thread is no longer available" });
496 return;
497 }
498 const routeRef = resolveThreadOpenRoute(threadRef, shell);
499 if (scopedThreadKey(routeRef) === routeThreadKey) return;
500 void navigate({
501 to: "/$environmentId/$threadId",
502 params: buildThreadRouteParams(routeRef),
503 });
504 };
505 const onOpenRelatedThreadStable = useStableCallback(onOpenRelatedThread);
506 const childThreadInputs = useChildThreadInputs(activeThreadRef);
507 const childInputNow = childInputNowItem({
508 childThreadInputs,
509 parentAwaitingUser: Boolean(
510 activePendingApproval || activePendingUserInput || activeThreadShell?.hasPendingUserInput,
511 ),
512 onOpenThread: onOpenRelatedThreadStable,
513 });
514
515 const sendStandaloneCommand = async (text: string, failureMessage: string) => {
516 if (!activeThread || !clientSettingsHydrated || sendInFlightRef.current) return;
517 const context = composerRef.current?.getSendContext();
518 if (!context?.providerAvailable) return;
519
520 const threadId = activeThread.id;
521 const messageId = newMessageId();
522 const createdAt = new Date().toISOString();
523 sendInFlightRef.current = true;
524 beginLocalDispatch();
525 setThreadError(threadId, null);
526 setOptimisticUserMessages((messages) => [
527 ...messages,
528 {
529 id: messageId,
530 role: "user",
531 text,
532 runId: null,
533 createdAt,
534 updatedAt: createdAt,
535 streaming: false,
536 },
537 ]);
538 scrollToEnd();
539 // Commands settle into a result, so this always runs.
540 const settingsResult = await persistThreadSettingsForNextTurn({
541 threadId,
542 createdAt,
543 ...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),
544 runtimeMode,
545 interactionMode: DEFAULT_INTERACTION_MODE,
546 });
547 const result =
548 settingsResult._tag === "Failure"
549 ? settingsResult
550 : await startThreadTurn({
551 environmentId,
552 input: {
553 threadId,
554 message: { messageId, role: "user", text, attachments: [] },
555 modelSelection: context.selectedModelSelection,
556 runtimeMode,
557 interactionMode: DEFAULT_INTERACTION_MODE,
558 createdAt,
559 },
560 });
561 if (result._tag === "Failure") {
562 setOptimisticUserMessages((messages) =>
563 messages.filter((message) => message.id !== messageId),
564 );
565 resetLocalDispatch();
566 if (!isAtomCommandInterrupted(result)) {
567 const error = squashAtomCommandFailure(result);
568 setThreadError(threadId, error instanceof Error ? error.message : failureMessage);
569 }
570 } else {
571 clearUsageLimitsFor(routeThreadKey);
572 }
573 sendInFlightRef.current = false;
574 };
575 const sendStandaloneCommandStable = useStableCallback(sendStandaloneCommand);
576 const activeGoal = activeThreadShell?.goal ?? null;
577 const goalNow =
578 activeThread && activeGoal !== null
579 ? goalNowItem({
580 threadId: activeThread.id,
581 goal: activeGoal,
582 isWorking,
583 onSendCommand: sendStandaloneCommandStable,
584 })
585 : null;
586
587 const forceStop = useForceStopThread(environmentId);
588 const anyRunStop = useAtomValue(
589 threadDetailRef ? environmentThreadDetails.runStopAtom(threadDetailRef) : NO_RUN_STOP_ATOM,
590 );
591 // A background Stop matters only while that work is still pending.
592 const runStop = anyRunStop?.background && activeBackgroundTasks.length === 0 ? null : anyRunStop;
593 const runStopNow =
594 activeThread && runStop !== null
595 ? runStopNowItem({
596 threadId: activeThread.id,
597 stop: runStop,
598 canForceStop: forceStop.canForceStop,
599 forceStopping: forceStop.stoppingThreadId === activeThread.id,
600 onForceStop: () => void forceStop.forceStopThread(activeThread.id, runStop.runId),
601 })
602 : null;
603 const backgroundWorkNow = activeThread
604 ? backgroundWorkNowItem({
605 environmentId,
606 threadId: activeThread.id,
607 tasks: activeBackgroundTasks,
608 turnItems,
609 // Interrupt stops the active run first, not this work; the composer owns that Stop.
610 stopDisabled: runIsLive || !canOperateThread || isStoppingBackgroundWork,
611 stopping: isStoppingBackgroundWork,
612 onStop: handleStopBackgroundWorkStable,
613 onOpenThread: onOpenRelatedThreadStable,
614 })
615 : null;
616 // The relative times read the clock; nowMinute re-renders them on the minute.
617 const snoozedUntil = activeThreadShell?.snoozedUntil ?? null;
618 const settledAt = activeThreadShell?.settledAt ?? null;
619 // Same rounding as the sidebar's snoozed shelf, so both read the same hour.
620 const snoozeWake = snoozedUntil
621 ? snoozeWakeLabel(snoozedUntil, { now: `${nowMinute}:00.000Z` })
622 : null;
623 const threadStatusLine = activeThreadSnoozed ? (
624 limitRecoveryHoldsSnooze ? null : (
625 <ThreadStatusLine
626 icon={<AlarmClockIcon />}
627 label={snoozeWake && snoozeWake !== "now" ? `Snoozed, ${snoozeWake} left` : "Snoozed"}
628 clockMinute={nowMinute}
629 actionLabel={isUnsnoozing ? "Waking..." : "Wake now"}
630 actionDisabled={!canOperateThread || isUnsnoozing}
631 onAction={() => void handleUnsnoozeActiveThreadStable()}
632 />
633 )
634 ) : activeThreadSettled ? (
635 <ThreadStatusLine
636 icon={<CheckCircle2Icon />}
637 label={settledAt ? `Settled ${formatRelativeTimeLabel(settledAt)}` : "Settled"}
638 clockMinute={nowMinute}
639 actionLabel={isUnsettling ? "Un-settling..." : "Un-settle"}
640 actionDisabled={!canOperateThread || isUnsettling}
641 onAction={() => void handleUnsettleActiveThreadStable()}
642 />
643 ) : activeThreadWokeVisible ? (
644 <ThreadStatusLine
645 icon={<AlarmClockIcon />}
646 label="Woke from snooze"
647 actionLabel="Dismiss"
648 onAction={acknowledgeActiveThreadWokeStable}
649 />
650 ) : null;
651 const toastThreadActionFailure =
652 (title: string) => (result: AtomCommandResult<unknown, unknown>) => {
653 if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
654 const error = squashAtomCommandFailure(result);
655 toastManager.add(
656 stackedThreadToast({
657 type: "error",
658 title,
659 description: error instanceof Error ? error.message : "An error occurred.",
660 }),
661 );
662 };
663 const shortcuts = {
664 "thread.settle": () => {
665 if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
666 if (!activeThreadShell || !activeThreadRef || !serverConfigLoaded) return;
667 if (activeThreadSettled) {
668 void handleUnsettleActiveThread();
669 return;
670 }
671 if (settleAndAdvance(activeThreadRef)) return;
672 void settleThread(activeThreadRef).then(toastThreadActionFailure("Failed to settle thread"));
673 },
674 "thread.pin": ({ event }) => {
675 // An open project list pins its highlighted project with this key instead.
676 if (
677 event.target instanceof Element &&
678 event.target.closest('[role="combobox"][aria-expanded="true"]') !== null
679 ) {
680 return false;
681 }
682 if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
683 if (!activeThreadShell || !activeThreadRef || !serverConfigLoaded) return;
684 void (
685 activeThreadPinned ? confirmAndUnpinThread(activeThreadRef) : pinThread(activeThreadRef)
686 ).then(
687 toastThreadActionFailure(
688 activeThreadPinned ? "Failed to unpin thread" : "Failed to pin thread",
689 ),
690 );
691 },
692 "thread.regenerateTitle": () => {
693 if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false;
694 if (!activeThreadShell || !activeThreadRef || !serverConfigLoaded) return;
695 if (activeThreadShell.titleRegeneration != null) return;
696 void regenerateThreadTitle({
697 environmentId: activeThreadRef.environmentId,
698 input: { threadId: activeThreadRef.threadId },
699 }).then((result) => {
700 const toast = titleRegenerationToast([result]);
701 if (toast) toastManager.add(toast);
702 });
703 },
704 } satisfies ChatShortcutCommands;
705 return {
706 shortcuts,
707 acknowledgeActiveThreadWoke,
708 activeBranchMismatchKey,
709 // A Stop that went unanswered needs the user; one in progress is the thread's work.
710 attentionNow: runStop?.state === "unresponsive" ? runStopNow : childInputNow,
711 workNow: runStop?.state === "stopping" ? runStopNow : backgroundWorkNow,
712 childInputNow,
713 goalNow,
714 handleSwitchCheckoutToThread,
715 handleSwitchCheckoutToThreadStable,
716 isRestoringThreadBranch,
717 onOpenRelatedThread,
718 onOpenRelatedThreadStable,
719 sendStandaloneCommand,
720 setBranchMismatchDismissTick,
721 showBranchMismatchBanner,
722 threadStatusLine,
723 };
724}