The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { ThreadStructure } from "@t3tools/client-runtime/state/shell"; |
| 2 | import type { |
| 3 | EnvironmentId, |
| 4 | RunId, |
| 5 | ScopedThreadRef, |
| 6 | ThreadId, |
| 7 | WorktreeSetupSnapshot, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { workspacePreparationRetryRunIds } from "@t3tools/client-runtime/state/turn-item-presentation"; |
| 10 | import { useState } from "react"; |
| 11 | import { useStableCallback } from "../../hooks/useStableCallback"; |
| 12 | import { useEnvironmentQuery } from "../../state/query"; |
| 13 | import { threadEnvironment } from "../../state/threads"; |
| 14 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 15 | import { vcsEnvironment } from "../../state/vcs"; |
| 16 | import { useTerminalUiStateStore } from "../../terminalUiStateStore"; |
| 17 | import type { Thread } from "../../types"; |
| 18 | import { focusTerminal } from "../../lib/terminalFocus"; |
| 19 | import { resolveVisibleWorktreeSetup, resolveWorktreeSetupProgress } from "../ChatView.logic"; |
| 20 | import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup"; |
| 21 | |
| 22 | // Share in-flight preparation retries across ChatView instances. |
| 23 | const retryingWorkspacePreparationRunIds = new Set<RunId>(); |
| 24 | |
| 25 | export function useWorktreeSetup(input: { |
| 26 | readonly routeThreadRef: ScopedThreadRef; |
| 27 | // Use the draft id until promotion: bootstrap rotates the thread id. |
| 28 | readonly ownerKey: string; |
| 29 | readonly activeThread: |
| 30 | | { readonly id: ThreadId; readonly worktreePath: string | null } |
| 31 | | undefined; |
| 32 | readonly threadRef: ScopedThreadRef | null; |
| 33 | readonly serverProjection: ThreadStructure | null; |
| 34 | readonly activityRun: Thread["latestRun"] | null; |
| 35 | readonly localPreparing: boolean; |
| 36 | readonly sendQueuesBehindSetup: boolean; |
| 37 | readonly followUpSent: boolean; |
| 38 | }) { |
| 39 | const { |
| 40 | routeThreadRef, |
| 41 | ownerKey, |
| 42 | activeThread, |
| 43 | threadRef, |
| 44 | serverProjection, |
| 45 | activityRun, |
| 46 | localPreparing, |
| 47 | sendQueuesBehindSetup, |
| 48 | followUpSent, |
| 49 | } = input; |
| 50 | const cancelWorktreeSetup = useAtomCommand(vcsEnvironment.cancelWorktreeSetup, { |
| 51 | reportFailure: false, |
| 52 | }); |
| 53 | const retryWorkspacePreparation = useAtomCommand(threadEnvironment.retryWorkspacePreparation); |
| 54 | const storeEnsureTerminal = useTerminalUiStateStore((state) => state.ensureTerminal); |
| 55 | const [startedSetup, setStartedSetup] = useState<{ |
| 56 | readonly environmentId: EnvironmentId; |
| 57 | readonly threadId: ThreadId; |
| 58 | readonly ownerKey: string; |
| 59 | } | null>(null); |
| 60 | // Retain the dropped snapshot until settled activity reaches the projection. |
| 61 | const [heldSnapshot, setHeldSnapshot] = useState<WorktreeSetupSnapshot | null>(null); |
| 62 | const started = startedSetup !== null && startedSetup.ownerKey === ownerKey; |
| 63 | const target = started ? startedSetup : routeThreadRef; |
| 64 | const runPreparing = activityRun?.status === "preparing"; |
| 65 | const snapshotQuery = useEnvironmentQuery( |
| 66 | started || |
| 67 | (activeThread?.id === routeThreadRef.threadId && |
| 68 | (localPreparing || runPreparing || activeThread.worktreePath !== null)) |
| 69 | ? vcsEnvironment.worktreeSetup({ |
| 70 | environmentId: target.environmentId, |
| 71 | input: { threadId: target.threadId }, |
| 72 | }) |
| 73 | : null, |
| 74 | ); |
| 75 | const latestSnapshot = snapshotQuery.data; |
| 76 | const [heldFor, setHeldFor] = useState<{ |
| 77 | readonly routeThreadRef: ScopedThreadRef; |
| 78 | readonly latestSnapshot: WorktreeSetupSnapshot | null | undefined; |
| 79 | }>({ routeThreadRef, latestSnapshot: null }); |
| 80 | if (heldFor.routeThreadRef !== routeThreadRef || heldFor.latestSnapshot !== latestSnapshot) { |
| 81 | const routeChanged = heldFor.routeThreadRef !== routeThreadRef; |
| 82 | setHeldFor({ routeThreadRef, latestSnapshot }); |
| 83 | if (routeChanged) { |
| 84 | setHeldSnapshot(null); |
| 85 | } else if (latestSnapshot) { |
| 86 | setHeldSnapshot((current) => |
| 87 | current?.threadId === latestSnapshot.threadId && current.sequence > latestSnapshot.sequence |
| 88 | ? current |
| 89 | : latestSnapshot, |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | const { snapshot: liveSnapshot, isPreparingWorktree } = resolveWorktreeSetupProgress({ |
| 94 | threadId: target.threadId, |
| 95 | localPreparing, |
| 96 | runStatus: activityRun?.status, |
| 97 | latest: latestSnapshot, |
| 98 | held: heldSnapshot, |
| 99 | }); |
| 100 | const worktreeSetup = resolveVisibleWorktreeSetup({ |
| 101 | live: liveSnapshot, |
| 102 | recorded: null, |
| 103 | runs: serverProjection?.runs ?? [], |
| 104 | followUpSent, |
| 105 | }); |
| 106 | // After agent handoff, sends queue behind setup without awaiting its scripts. |
| 107 | const blocksSend = |
| 108 | !sendQueuesBehindSetup && |
| 109 | (worktreeSetup !== null |
| 110 | ? worktreeSetup.phase === "running" && !worktreeSetupAgentStarted(worktreeSetup) |
| 111 | : isPreparingWorktree); |
| 112 | |
| 113 | const retryableRunIdsKey = [ |
| 114 | ...workspacePreparationRetryRunIds( |
| 115 | serverProjection?.runs ?? [], |
| 116 | serverProjection?.turnItems ?? [], |
| 117 | ), |
| 118 | ].join("\n"); |
| 119 | const [retryableRunIds, setRetryableRunIds] = useState({ |
| 120 | key: retryableRunIdsKey, |
| 121 | ids: new Set(retryableRunIdsKey === "" ? [] : (retryableRunIdsKey.split("\n") as RunId[])), |
| 122 | }); |
| 123 | if (retryableRunIds.key !== retryableRunIdsKey) { |
| 124 | setRetryableRunIds({ |
| 125 | key: retryableRunIdsKey, |
| 126 | ids: new Set(retryableRunIdsKey === "" ? [] : (retryableRunIdsKey.split("\n") as RunId[])), |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | const begin = useStableCallback( |
| 131 | (thread: { readonly environmentId: EnvironmentId; readonly threadId: ThreadId } | null) => |
| 132 | setStartedSetup(thread === null ? null : { ...thread, ownerKey }), |
| 133 | ); |
| 134 | const cancel = useStableCallback(async () => { |
| 135 | if (!worktreeSetup || worktreeSetup.phase !== "running") return false; |
| 136 | const result = await cancelWorktreeSetup({ |
| 137 | environmentId: target.environmentId, |
| 138 | input: { threadId: worktreeSetup.threadId }, |
| 139 | }); |
| 140 | return result._tag === "Success" && result.value.cancelled; |
| 141 | }); |
| 142 | const retryPreparation = useStableCallback((runId: RunId) => { |
| 143 | if (!threadRef || retryingWorkspacePreparationRunIds.has(runId)) return; |
| 144 | retryingWorkspacePreparationRunIds.add(runId); |
| 145 | void retryWorkspacePreparation({ |
| 146 | environmentId: threadRef.environmentId, |
| 147 | input: { threadId: threadRef.threadId, runId }, |
| 148 | }).finally(() => retryingWorkspacePreparationRunIds.delete(runId)); |
| 149 | }); |
| 150 | // Failed bootstrap deletes threads and terminals; expose terminals only for active threads. |
| 151 | const setupThreadRef = |
| 152 | worktreeSetup && threadRef && worktreeSetup.threadId === threadRef.threadId ? threadRef : null; |
| 153 | const openSetupTerminal = setupThreadRef |
| 154 | ? (terminalId: string) => { |
| 155 | storeEnsureTerminal(setupThreadRef, terminalId, { open: true, active: true }); |
| 156 | focusTerminal(setupThreadRef, terminalId); |
| 157 | } |
| 158 | : null; |
| 159 | |
| 160 | return { |
| 161 | worktreeSetup, |
| 162 | isPreparingWorktree, |
| 163 | blocksSend, |
| 164 | retryableRunIds: retryableRunIds.ids, |
| 165 | begin, |
| 166 | cancel, |
| 167 | retryPreparation, |
| 168 | openSetupTerminal, |
| 169 | }; |
| 170 | } |