TypeScript
refactor(web): proposed-plan follow-ups and implement-in-new-thread live in their own hooks (#23091)
Julius Marminge36d1e2fec588
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsxindex 04457062f72..0546e0098fb 100644--- a/apps/web/src/components/ChatView.tsx+++ b/apps/web/src/components/ChatView.tsx@@ -16,6 +16,7 @@ import { useRightPanelActions } from "./chat/useRightPanelActions";import { type TerminalLaunchContext, useChatTerminalActions } from "./chat/useChatTerminalActions";import { useComposerSend } from "./chat/useComposerSend";import { useComposerResume } from "./chat/useComposerResume";+import { useImplementPlanInNewThread } from "./chat/useProposedPlanSend";import { ThreadFind, ThreadFindCanvas, type ThreadFindControls } from "./chat/ThreadFindProvider";import { THREAD_FIND_BAR_RESERVED_HEIGHT } from "./chat/ThreadFindBar";import { usageLimitRecoveryBannerItem } from "./chat/UsageLimitRecoveryBanner";@@ -2945,7 +2946,23 @@ export default function ChatView(props: ChatViewProps) {scrollToEnd,setThreadError,});- const { onSend, onImplementPlanInNewThread } = useComposerSend({+ const onImplementPlanInNewThread = useImplementPlanInNewThread({+ environmentId,+ routeThreadKey,+ activeThread,+ activeThreadBranch,+ activeProposedPlan,+ isServerThread,+ isSendBusy,+ isConnecting,+ composerRef,+ beginLocalDispatch,+ resetLocalDispatch,+ activeProject,+ activeEnvironmentUnavailable,+ defaultRuntimeMode,+ });+ const { onSend } = useComposerSend({environmentId,routeThreadKey,currentRouteThreadKeyRef,@@ -2963,7 +2980,6 @@ export default function ChatView(props: ChatViewProps) {isGitRepo,phase,runtimeMode,- defaultRuntimeMode,sendEnvMode,startFromOrigin,localCheckoutBranchMismatch,diff --git a/apps/web/src/components/chat/useComposerSend.ts b/apps/web/src/components/chat/useComposerSend.tsindex be9cbe24b87..df465e04811 100644--- a/apps/web/src/components/chat/useComposerSend.ts+++ b/apps/web/src/components/chat/useComposerSend.ts@@ -7,7 +7,6 @@ import {deriveComposerSendState,readFileAsDataUrl,revokeUserMessagePreviewUrls,- waitForStartedServerThread,boundAttachmentIds,formatOutgoingPrompt,optimisticMessageAttachments,@@ -42,7 +41,6 @@ import {scopeThreadRef,} from "@t3tools/client-runtime/environment";import { createModelSelection } from "@t3tools/shared/model";-import { truncate } from "@t3tools/shared/String";import { type SetStateAction, useLayoutEffect, useRef } from "react";import { flushSync } from "react-dom";import { useNavigate } from "@tanstack/react-router";@@ -57,11 +55,7 @@ import {collapseExpandedComposerCursor,parseStandaloneComposerSlashCommand,} from "../../composer-logic";-import {- buildPlanImplementationThreadTitle,- buildPlanImplementationPrompt,- resolvePlanFollowUpSubmission,-} from "../../proposedPlan";+import { resolvePlanFollowUpSubmission } from "../../proposedPlan";import { type ChatMessage } from "../../types";import { stackedThreadToast, toastManager } from "../ui/toast";import { newMessageId, newThreadId } from "~/lib/utils";@@ -89,6 +83,7 @@ import {import { environmentServerConfigsAtom } from "../../state/server";import { threadEnvironment } from "../../state/threads";import { runMobileComposerTransition } from "./draftHeroTransition";+import { usePlanFollowUpSend } from "./useProposedPlanSend";import { type ComposerDispatchMode } from "@t3tools/client-runtime/state/composer-dispatch";import {awaitAttachmentUploads,@@ -163,7 +158,6 @@ interface ComposerSendInput {readonly isGitRepo: boolean;readonly phase: SessionPhase;readonly runtimeMode: RuntimeMode;- readonly defaultRuntimeMode: RuntimeMode;readonly sendEnvMode: DraftThreadEnvMode;readonly startFromOrigin: boolean;readonly localCheckoutBranchMismatch: {@@ -264,7 +258,6 @@ export function useComposerSend(input: ComposerSendInput) {isGitRepo,phase,runtimeMode,- defaultRuntimeMode,sendEnvMode,startFromOrigin,localCheckoutBranchMismatch,@@ -328,8 +321,6 @@ export function useComposerSend(input: ComposerSendInput) {} = input;const handleNewThread = useNewThreadHandler();const navigate = useNavigate();- const createThread = useOrchestrationCommand(threadEnvironment.create, { reportFailure: false });- const deleteThread = useOrchestrationCommand(threadEnvironment.delete, { reportFailure: false });const startThreadTurn = useOrchestrationCommand(threadEnvironment.startTurn, {reportFailure: false,});@@ -345,10 +336,26 @@ export function useComposerSend(input: ComposerSendInput) {);const setComposerDraftReviewComments = useComposerDraftStore((store) => store.setReviewComments);const setComposerDraftThreadContexts = useComposerDraftStore((store) => store.setThreadContexts);- const setComposerDraftInteractionMode = useComposerDraftStore(- (store) => store.setInteractionMode,- );const clearComposerDraftContent = useComposerDraftStore((store) => store.clearComposerContent);+ const submitPlanFollowUp = usePlanFollowUpSend({+ environmentId,+ routeThreadKey,+ activeThread,+ activeThreadBranch,+ activeProposedPlan,+ isServerThread,+ isSendBusy,+ isConnecting,+ composerRef,+ beginLocalDispatch,+ resetLocalDispatch,+ runtimeMode,+ localCheckoutBranchMismatch,+ setOptimisticUserMessages,+ setThreadError,+ anchorNewTurn,+ persistThreadSettingsForNextTurn,+ });const fanoutSelectionsAtom = draftFanoutSelectionsAtom(routeThreadKey);const readMultipleModelSelections = () => appAtomRegistry.get(fanoutSelectionsAtom);const { sendInFlight: sendInFlightRef, uncertainSubmissions: uncertainMultipleSubmissionsRef } =@@ -649,7 +656,7 @@ export function useComposerSend(input: ComposerSendInput) {promptRef.current = "";clearComposerDraftContent(composerDraftTarget);composerRef.current?.resetCursorState();- const followUpSent = await onSubmitPlanFollowUp({+ const followUpSent = await submitPlanFollowUp({text: followUp.text,context: buildMessageContext({terminalContexts: sendableComposerTerminalContexts,@@ -1542,282 +1549,5 @@ export function useComposerSend(input: ComposerSendInput) {}};- async function onSubmitPlanFollowUp({- text,- context,- interactionMode: nextInteractionMode,- }: {- text: string;- context?: ReturnType<typeof buildMessageContext>;- interactionMode: "default" | "plan";- }) {- if (- !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope) ||- !activeThread ||- !isServerThread ||- isSendBusy ||- isConnecting ||- sendInFlightRef.current- ) {- return false;- }-- const trimmed = text.trim();- if (!trimmed) {- return false;- }-- const sendCtx = composerRef.current?.getSendContext();- if (!sendCtx?.providerAvailable || !sendCtx.interactionModeEnabled) {- return false;- }- const {- selectedProvider: ctxSelectedProvider,- selectedModel: ctxSelectedModel,- selectedProviderModels: ctxSelectedProviderModels,- selectedPromptEffort: ctxSelectedPromptEffort,- selectedModelSelection: ctxSelectedModelSelection,- } = sendCtx;-- const threadIdForSend = activeThread.id;- const messageIdForSend = newMessageId();- const messageCreatedAt = new Date().toISOString();- const outgoingMessageText = formatOutgoingPrompt({- provider: ctxSelectedProvider,- model: ctxSelectedModel,- models: ctxSelectedProviderModels,- effort: ctxSelectedPromptEffort,- text: trimmed,- });-- sendInFlightRef.current = true;- beginLocalDispatch({ preparingWorktree: false });- setThreadError(threadIdForSend, null);-- anchorNewTurn(- scopedThreadKey(scopeThreadRef(activeThread.environmentId, threadIdForSend)),- messageIdForSend,- );-- setOptimisticUserMessages((existing) => [- ...existing,- {- id: messageIdForSend,- role: "user",- text: outgoingMessageText,- ...(context ? { context } : {}),- runId: null,- createdAt: messageCreatedAt,- updatedAt: messageCreatedAt,- streaming: false,- },- ]);-- const settingsResult = await persistThreadSettingsForNextTurn({- threadId: threadIdForSend,- createdAt: messageCreatedAt,- ...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),- runtimeMode,- interactionMode: nextInteractionMode,- });- let failure: AtomCommandResult<unknown, unknown> | null =- settingsResult._tag === "Failure" ? settingsResult : null;-- if (failure === null) {- setComposerDraftInteractionMode(- scopeThreadRef(activeThread.environmentId, threadIdForSend),- nextInteractionMode,- );-- const startResult = await startThreadTurn({- environmentId,- input: {- threadId: threadIdForSend,- message: {- messageId: messageIdForSend,- role: "user",- text: outgoingMessageText,- ...(context ? { context } : {}),- attachments: [],- },- modelSelection: ctxSelectedModelSelection,- titleSeed: activeThread.title,- runtimeMode,- interactionMode: nextInteractionMode,- ...(nextInteractionMode === "default" && activeProposedPlan- ? {- sourceProposedPlan: {- threadId: activeThread.id,- planId: activeProposedPlan.id,- },- }- : {}),- createdAt: messageCreatedAt,- },- });- failure = startResult._tag === "Failure" ? startResult : null;- }-- if (failure === null) {- sendInFlightRef.current = false;- resetLocalDispatch();- return true;- }-- setOptimisticUserMessages((existing) =>- existing.filter((message) => message.id !== messageIdForSend),- );- if (!isAtomCommandInterrupted(failure)) {- const error = squashAtomCommandFailure(failure);- setThreadError(- threadIdForSend,- error instanceof Error ? error.message : "Failed to send plan follow-up.",- );- }- sendInFlightRef.current = false;- resetLocalDispatch();- return false;- }-- const onImplementPlanInNewThread = async () => {- if (- !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope) ||- !activeThread ||- !activeProject ||- !activeProposedPlan ||- !isServerThread ||- isSendBusy ||- isConnecting ||- activeEnvironmentUnavailable ||- sendInFlightRef.current- ) {- return;- }-- const sendCtx = composerRef.current?.getSendContext();- if (!sendCtx?.providerAvailable || !sendCtx.interactionModeEnabled) {- return;- }- const {- selectedProvider: ctxSelectedProvider,- selectedModel: ctxSelectedModel,- selectedProviderModels: ctxSelectedProviderModels,- selectedPromptEffort: ctxSelectedPromptEffort,- selectedModelSelection: ctxSelectedModelSelection,- } = sendCtx;-- const createdAt = new Date().toISOString();- const nextThreadId = newThreadId();- const planMarkdown = activeProposedPlan.planMarkdown;- const implementationPrompt = buildPlanImplementationPrompt(planMarkdown);- const outgoingImplementationPrompt = formatOutgoingPrompt({- provider: ctxSelectedProvider,- model: ctxSelectedModel,- models: ctxSelectedProviderModels,- effort: ctxSelectedPromptEffort,- text: implementationPrompt,- });- const nextThreadTitle = truncate(buildPlanImplementationThreadTitle(planMarkdown));- const nextThreadModelSelection: ModelSelection = ctxSelectedModelSelection;-- sendInFlightRef.current = true;- beginLocalDispatch({ preparingWorktree: false });- const finish = () => {- sendInFlightRef.current = false;- resetLocalDispatch();- };-- const createResult = await createThread({- environmentId,- input: {- threadId: nextThreadId,- projectId: activeProject.id,- title: nextThreadTitle,- modelSelection: nextThreadModelSelection,- runtimeMode: defaultRuntimeMode,- interactionMode: "default",- branch: activeThreadBranch,- worktreePath: activeThread.worktreePath,- createdAt,- },- });- let failure: AtomCommandResult<unknown, unknown> | null =- createResult._tag === "Failure" ? createResult : null;-- if (failure === null) {- const startResult = await startThreadTurn({- environmentId,- input: {- threadId: nextThreadId,- message: {- messageId: newMessageId(),- role: "user",- text: outgoingImplementationPrompt,- attachments: [],- },- modelSelection: ctxSelectedModelSelection,- titleSeed: nextThreadTitle,- runtimeMode: defaultRuntimeMode,- interactionMode: "default",- sourceProposedPlan: {- threadId: activeThread.id,- planId: activeProposedPlan.id,- },- createdAt,- },- });- failure = startResult._tag === "Failure" ? startResult : null;- }-- if (failure === null) {- const startedResult = await settlePromise(() =>- waitForStartedServerThread(scopeThreadRef(activeThread.environmentId, nextThreadId)),- );- failure = startedResult._tag === "Failure" ? startedResult : null;- }-- if (failure === null) {- const navigateResult = await settlePromise(() =>- navigate({- to: "/$environmentId/$threadId",- params: {- environmentId: activeThread.environmentId,- threadId: nextThreadId,- },- }),- );- failure = navigateResult._tag === "Failure" ? navigateResult : null;- }-- if (failure !== null) {- const cleanupResult = await deleteThread({- environmentId,- input: {- threadId: nextThreadId,- },- });- if (cleanupResult._tag === "Failure" && !isAtomCommandInterrupted(cleanupResult)) {- console.warn(- "Failed to clean up implementation thread after start failure.",- squashAtomCommandFailure(cleanupResult),- );- }- if (!isAtomCommandInterrupted(failure)) {- const error = squashAtomCommandFailure(failure);- toastManager.add(- stackedThreadToast({- type: "error",- title: "Could not start implementation thread",- description:- error instanceof Error- ? error.message- : "An error occurred while creating the new thread.",- }),- );- }- }- finish();- };-- return { onSend, onImplementPlanInNewThread };+ return { onSend };}diff --git a/apps/web/src/components/chat/useProposedPlanSend.ts b/apps/web/src/components/chat/useProposedPlanSend.tsnew file mode 100644index 00000000000..7f4906d8664--- /dev/null+++ b/apps/web/src/components/chat/useProposedPlanSend.ts@@ -0,0 +1,402 @@+import {+ AuthOrchestrationOperateScope,+ type EnvironmentId,+ type ModelSelection,+ type ProviderInteractionMode,+ type RuntimeMode,+} from "@t3tools/contracts";+import { scopeThreadRef } from "@t3tools/client-runtime/environment";+import {+ isAtomCommandInterrupted,+ settlePromise,+ squashAtomCommandFailure,+ type AtomCommandResult,+} from "@t3tools/client-runtime/state/runtime";+import type {+ EnvironmentProject,+ EnvironmentThreadShell,+} from "@t3tools/client-runtime/state/shell";+import { scopedThreadKey } from "@t3tools/client-runtime/environment";+import { truncate } from "@t3tools/shared/String";+import { useNavigate } from "@tanstack/react-router";+import type { Dispatch, SetStateAction } from "react";+import { newMessageId, newThreadId } from "~/lib/utils";+import { useComposerDraftStore } from "../../composerDraftStore";+import type { ComposerHandleRef } from "../../composerHandleContext";+import type { buildMessageContext } from "../../lib/composerContextRecords";+import {+ buildPlanImplementationPrompt,+ buildPlanImplementationThreadTitle,+} from "../../proposedPlan";+import type { LatestProposedPlanState } from "../../session-logic";+import { readEnvironmentScope } from "../../state/session";+import { threadEnvironment } from "../../state/threads";+import { useOrchestrationCommand } from "../../state/use-orchestration-command";+import type { ChatMessage } from "../../types";+import { stackedThreadToast, toastManager } from "../ui/toast";+import {+ formatOutgoingPrompt,+ type makeThreadErrorSetter,+ waitForStartedServerThread,+} from "../ChatView.logic";+import { draftSendState } from "./chatViewSend";+import type { useLocalDispatchState } from "./useLocalDispatchState";+import type { useTimelineFollow } from "./useTimelineFollow";++type LocalDispatchState = ReturnType<typeof useLocalDispatchState>;++interface PlanSendBase {+ readonly environmentId: EnvironmentId;+ readonly routeThreadKey: string;+ readonly activeThread: EnvironmentThreadShell | undefined;+ readonly activeThreadBranch: string | null;+ readonly activeProposedPlan: LatestProposedPlanState | null;+ readonly isServerThread: boolean;+ readonly isSendBusy: boolean;+ readonly isConnecting: boolean;+ readonly composerRef: ComposerHandleRef;+ readonly beginLocalDispatch: LocalDispatchState["beginLocalDispatch"];+ readonly resetLocalDispatch: LocalDispatchState["resetLocalDispatch"];+}++interface PlanFollowUpSendInput extends PlanSendBase {+ readonly runtimeMode: RuntimeMode;+ readonly localCheckoutBranchMismatch: { readonly currentBranch: string } | null;+ readonly setOptimisticUserMessages: Dispatch<SetStateAction<ChatMessage[]>>;+ readonly setThreadError: ReturnType<typeof makeThreadErrorSetter>;+ readonly anchorNewTurn: ReturnType<typeof useTimelineFollow>["anchorNewTurn"];+ readonly persistThreadSettingsForNextTurn: (input: {+ threadId: EnvironmentThreadShell["id"];+ createdAt: string;+ branch?: string;+ runtimeMode: RuntimeMode;+ interactionMode: ProviderInteractionMode;+ }) => Promise<AtomCommandResult<void, unknown>>;+}++/**+ * Answers a proposed plan on this thread: refine it in plan mode, or implement it here+ * (which links the turn to the plan). Resolves whether the turn started.+ */+export function usePlanFollowUpSend(input: PlanFollowUpSendInput) {+ const {+ environmentId,+ routeThreadKey,+ activeThread,+ activeProposedPlan,+ isServerThread,+ isSendBusy,+ isConnecting,+ composerRef,+ beginLocalDispatch,+ resetLocalDispatch,+ runtimeMode,+ localCheckoutBranchMismatch,+ setOptimisticUserMessages,+ setThreadError,+ anchorNewTurn,+ persistThreadSettingsForNextTurn,+ } = input;+ const startThreadTurn = useOrchestrationCommand(threadEnvironment.startTurn, {+ reportFailure: false,+ });+ const setComposerDraftInteractionMode = useComposerDraftStore(+ (store) => store.setInteractionMode,+ );+ const { sendInFlight: sendInFlightRef } = draftSendState(routeThreadKey);++ return async function submitPlanFollowUp({+ text,+ context,+ interactionMode: nextInteractionMode,+ }: {+ text: string;+ context?: ReturnType<typeof buildMessageContext>;+ interactionMode: "default" | "plan";+ }) {+ if (+ !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope) ||+ !activeThread ||+ !isServerThread ||+ isSendBusy ||+ isConnecting ||+ sendInFlightRef.current+ ) {+ return false;+ }++ const trimmed = text.trim();+ if (!trimmed) {+ return false;+ }++ const sendCtx = composerRef.current?.getSendContext();+ if (!sendCtx?.providerAvailable || !sendCtx.interactionModeEnabled) {+ return false;+ }++ const threadIdForSend = activeThread.id;+ const messageIdForSend = newMessageId();+ const messageCreatedAt = new Date().toISOString();+ const outgoingMessageText = formatOutgoingPrompt({+ provider: sendCtx.selectedProvider,+ model: sendCtx.selectedModel,+ models: sendCtx.selectedProviderModels,+ effort: sendCtx.selectedPromptEffort,+ text: trimmed,+ });++ sendInFlightRef.current = true;+ beginLocalDispatch({ preparingWorktree: false });+ setThreadError(threadIdForSend, null);++ anchorNewTurn(+ scopedThreadKey(scopeThreadRef(activeThread.environmentId, threadIdForSend)),+ messageIdForSend,+ );++ setOptimisticUserMessages((existing) => [+ ...existing,+ {+ id: messageIdForSend,+ role: "user",+ text: outgoingMessageText,+ ...(context ? { context } : {}),+ runId: null,+ createdAt: messageCreatedAt,+ updatedAt: messageCreatedAt,+ streaming: false,+ },+ ]);++ const settingsResult = await persistThreadSettingsForNextTurn({+ threadId: threadIdForSend,+ createdAt: messageCreatedAt,+ ...(localCheckoutBranchMismatch ? { branch: localCheckoutBranchMismatch.currentBranch } : {}),+ runtimeMode,+ interactionMode: nextInteractionMode,+ });+ let failure: AtomCommandResult<unknown, unknown> | null =+ settingsResult._tag === "Failure" ? settingsResult : null;++ if (failure === null) {+ setComposerDraftInteractionMode(+ scopeThreadRef(activeThread.environmentId, threadIdForSend),+ nextInteractionMode,+ );++ const startResult = await startThreadTurn({+ environmentId,+ input: {+ threadId: threadIdForSend,+ message: {+ messageId: messageIdForSend,+ role: "user",+ text: outgoingMessageText,+ ...(context ? { context } : {}),+ attachments: [],+ },+ modelSelection: sendCtx.selectedModelSelection,+ titleSeed: activeThread.title,+ runtimeMode,+ interactionMode: nextInteractionMode,+ ...(nextInteractionMode === "default" && activeProposedPlan+ ? {+ sourceProposedPlan: {+ threadId: activeThread.id,+ planId: activeProposedPlan.id,+ },+ }+ : {}),+ createdAt: messageCreatedAt,+ },+ });+ failure = startResult._tag === "Failure" ? startResult : null;+ }++ if (failure === null) {+ sendInFlightRef.current = false;+ resetLocalDispatch();+ return true;+ }++ setOptimisticUserMessages((existing) =>+ existing.filter((message) => message.id !== messageIdForSend),+ );+ if (!isAtomCommandInterrupted(failure)) {+ const error = squashAtomCommandFailure(failure);+ setThreadError(+ threadIdForSend,+ error instanceof Error ? error.message : "Failed to send plan follow-up.",+ );+ }+ sendInFlightRef.current = false;+ resetLocalDispatch();+ return false;+ };+}++interface ImplementPlanInNewThreadInput extends PlanSendBase {+ readonly activeProject: EnvironmentProject | null;+ readonly activeEnvironmentUnavailable: boolean;+ readonly defaultRuntimeMode: RuntimeMode;+}++/**+ * Starts a fresh thread that implements the active proposed plan, then opens it. The new+ * thread is deleted again if it cannot be started or opened.+ */+export function useImplementPlanInNewThread(input: ImplementPlanInNewThreadInput) {+ const {+ environmentId,+ routeThreadKey,+ activeThread,+ activeThreadBranch,+ activeProposedPlan,+ isServerThread,+ isSendBusy,+ isConnecting,+ composerRef,+ beginLocalDispatch,+ resetLocalDispatch,+ activeProject,+ activeEnvironmentUnavailable,+ defaultRuntimeMode,+ } = input;+ const navigate = useNavigate();+ const createThread = useOrchestrationCommand(threadEnvironment.create, { reportFailure: false });+ const deleteThread = useOrchestrationCommand(threadEnvironment.delete, { reportFailure: false });+ const startThreadTurn = useOrchestrationCommand(threadEnvironment.startTurn, {+ reportFailure: false,+ });+ const { sendInFlight: sendInFlightRef } = draftSendState(routeThreadKey);++ return async () => {+ if (+ !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope) ||+ !activeThread ||+ !activeProject ||+ !activeProposedPlan ||+ !isServerThread ||+ isSendBusy ||+ isConnecting ||+ activeEnvironmentUnavailable ||+ sendInFlightRef.current+ ) {+ return;+ }++ const sendCtx = composerRef.current?.getSendContext();+ if (!sendCtx?.providerAvailable || !sendCtx.interactionModeEnabled) {+ return;+ }++ const createdAt = new Date().toISOString();+ const nextThreadId = newThreadId();+ const planMarkdown = activeProposedPlan.planMarkdown;+ const outgoingImplementationPrompt = formatOutgoingPrompt({+ provider: sendCtx.selectedProvider,+ model: sendCtx.selectedModel,+ models: sendCtx.selectedProviderModels,+ effort: sendCtx.selectedPromptEffort,+ text: buildPlanImplementationPrompt(planMarkdown),+ });+ const nextThreadTitle = truncate(buildPlanImplementationThreadTitle(planMarkdown));+ const nextThreadModelSelection: ModelSelection = sendCtx.selectedModelSelection;++ sendInFlightRef.current = true;+ beginLocalDispatch({ preparingWorktree: false });++ const createResult = await createThread({+ environmentId,+ input: {+ threadId: nextThreadId,+ projectId: activeProject.id,+ title: nextThreadTitle,+ modelSelection: nextThreadModelSelection,+ runtimeMode: defaultRuntimeMode,+ interactionMode: "default",+ branch: activeThreadBranch,+ worktreePath: activeThread.worktreePath,+ createdAt,+ },+ });+ let failure: AtomCommandResult<unknown, unknown> | null =+ createResult._tag === "Failure" ? createResult : null;++ if (failure === null) {+ const startResult = await startThreadTurn({+ environmentId,+ input: {+ threadId: nextThreadId,+ message: {+ messageId: newMessageId(),+ role: "user",+ text: outgoingImplementationPrompt,+ attachments: [],+ },+ modelSelection: nextThreadModelSelection,+ titleSeed: nextThreadTitle,+ runtimeMode: defaultRuntimeMode,+ interactionMode: "default",+ sourceProposedPlan: {+ threadId: activeThread.id,+ planId: activeProposedPlan.id,+ },+ createdAt,+ },+ });+ failure = startResult._tag === "Failure" ? startResult : null;+ }++ if (failure === null) {+ const startedResult = await settlePromise(() =>+ waitForStartedServerThread(scopeThreadRef(activeThread.environmentId, nextThreadId)),+ );+ failure = startedResult._tag === "Failure" ? startedResult : null;+ }++ if (failure === null) {+ const navigateResult = await settlePromise(() =>+ navigate({+ to: "/$environmentId/$threadId",+ params: {+ environmentId: activeThread.environmentId,+ threadId: nextThreadId,+ },+ }),+ );+ failure = navigateResult._tag === "Failure" ? navigateResult : null;+ }++ if (failure !== null) {+ const cleanupResult = await deleteThread({+ environmentId,+ input: {+ threadId: nextThreadId,+ },+ });+ if (cleanupResult._tag === "Failure" && !isAtomCommandInterrupted(cleanupResult)) {+ console.warn(+ "Failed to clean up implementation thread after start failure.",+ squashAtomCommandFailure(cleanupResult),+ );+ }+ if (!isAtomCommandInterrupted(failure)) {+ const error = squashAtomCommandFailure(failure);+ toastManager.add(+ stackedThreadToast({+ type: "error",+ title: "Could not start implementation thread",+ description:+ error instanceof Error+ ? error.message+ : "An error occurred while creating the new thread.",+ }),+ );+ }+ }+ sendInFlightRef.current = false;+ resetLocalDispatch();+ };+}