The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | AuthTerminalOperateScope, |
| 3 | type EnvironmentId, |
| 4 | type ResolvedKeybindingsConfig, |
| 5 | type ScopedThreadRef, |
| 6 | type ThreadId, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { scopeProjectRef } from "@t3tools/client-runtime/environment"; |
| 9 | import { projectScriptCwd, projectScriptRuntimeEnv } from "@t3tools/shared/projectScripts"; |
| 10 | import { resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabelsCopy"; |
| 11 | import { useMessages } from "gt-react"; |
| 12 | import { memo, useEffect } from "react"; |
| 13 | import { useComposerDraftStore } from "../composerDraftStore"; |
| 14 | import { useStableCallback } from "../hooks/useStableCallback"; |
| 15 | import type { TerminalContextSelection } from "../lib/terminalContext"; |
| 16 | import { focusActiveDrawerTerminal, focusTerminal } from "../lib/terminalFocus"; |
| 17 | import { cn } from "~/lib/utils"; |
| 18 | import { |
| 19 | selectThreadRightPanelState, |
| 20 | useRightPanelStore, |
| 21 | type RightPanelSurface, |
| 22 | } from "../rightPanelStore"; |
| 23 | import { useProject, useQuietThreadShell } from "../state/entities"; |
| 24 | import { useEnvironmentScope } from "../state/session"; |
| 25 | import { useKnownTerminalSessions, useTerminalDrawerSessions } from "../state/terminalSessions"; |
| 26 | import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; |
| 27 | import { useThreadTerminalActions } from "./terminal/useThreadTerminalActions"; |
| 28 | import ThreadTerminalDrawer from "./ThreadTerminalDrawer"; |
| 29 | import { resolveTerminalWorktreePath } from "./ChatView.logic"; |
| 30 | |
| 31 | /** Where a drawer or panel opens new terminals when a script run overrides the thread's own. */ |
| 32 | export type PersistentTerminalLaunchContext = { |
| 33 | readonly cwd: string; |
| 34 | readonly worktreePath: string | null; |
| 35 | }; |
| 36 | |
| 37 | interface TerminalLaunchLocationWithEnv { |
| 38 | readonly cwd: string; |
| 39 | readonly worktreePath: string | null; |
| 40 | readonly runtimeEnv: Record<string, string>; |
| 41 | } |
| 42 | |
| 43 | interface PersistentThreadTerminalDrawerProps { |
| 44 | threadRef: { environmentId: EnvironmentId; threadId: ThreadId }; |
| 45 | threadId: ThreadId; |
| 46 | active: boolean; |
| 47 | composerHeight: number; |
| 48 | launchContext: PersistentTerminalLaunchContext | null; |
| 49 | splitShortcutLabel: string | undefined; |
| 50 | splitVerticalShortcutLabel: string | undefined; |
| 51 | newShortcutLabel: string | undefined; |
| 52 | closeShortcutLabel: string | undefined; |
| 53 | keybindings: ResolvedKeybindingsConfig; |
| 54 | onAddTerminalContext: (selection: TerminalContextSelection) => void; |
| 55 | } |
| 56 | |
| 57 | export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDrawer({ |
| 58 | threadRef, |
| 59 | threadId, |
| 60 | active, |
| 61 | composerHeight, |
| 62 | launchContext, |
| 63 | splitShortcutLabel, |
| 64 | splitVerticalShortcutLabel, |
| 65 | newShortcutLabel, |
| 66 | closeShortcutLabel, |
| 67 | keybindings, |
| 68 | onAddTerminalContext, |
| 69 | }: PersistentThreadTerminalDrawerProps) { |
| 70 | const messages = useMessages(); |
| 71 | const canOperateTerminal = useEnvironmentScope(threadRef.environmentId, AuthTerminalOperateScope); |
| 72 | const terminalActions = useThreadTerminalActions(threadRef); |
| 73 | const serverThread = useQuietThreadShell(threadRef); |
| 74 | const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef)); |
| 75 | const projectRef = serverThread |
| 76 | ? scopeProjectRef(serverThread.environmentId, serverThread.projectId) |
| 77 | : draftThread |
| 78 | ? scopeProjectRef(draftThread.environmentId, draftThread.projectId) |
| 79 | : null; |
| 80 | const project = useProject(projectRef); |
| 81 | const terminalUiState = useTerminalUiStateStore((state) => |
| 82 | selectThreadTerminalUiState(state.terminalUiStateByThreadKey, threadRef), |
| 83 | ); |
| 84 | const visible = active && terminalUiState.terminalOpen; |
| 85 | const metadata = useTerminalDrawerSessions(threadRef.environmentId, threadId); |
| 86 | const knownTerminalSessions = metadata?.sessions; |
| 87 | const snapshotId = metadata?.snapshotId; |
| 88 | const panelSurfaces = useRightPanelStore( |
| 89 | (state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces, |
| 90 | ); |
| 91 | const panelTerminalIds = new Set( |
| 92 | panelSurfaces.flatMap((surface) => (surface.kind === "terminal" ? surface.terminalIds : [])), |
| 93 | ); |
| 94 | const drawerTerminalSessions = |
| 95 | knownTerminalSessions?.filter((session) => !panelTerminalIds.has(session.target.terminalId)) ?? |
| 96 | []; |
| 97 | const terminalLabelsById = new Map<string, string>(); |
| 98 | for (const session of drawerTerminalSessions) { |
| 99 | terminalLabelsById.set( |
| 100 | session.target.terminalId, |
| 101 | resolveTerminalSessionLabel(session.target.terminalId, session.state.summary, messages), |
| 102 | ); |
| 103 | } |
| 104 | const terminalLaunchLocationsById = new Map<string, TerminalLaunchLocationWithEnv>(); |
| 105 | for (const session of drawerTerminalSessions) { |
| 106 | const summary = session.state.summary; |
| 107 | if (!project || !summary) continue; |
| 108 | const worktreePathForLaunch = |
| 109 | launchContext !== null ? launchContext.worktreePath : summary.worktreePath; |
| 110 | terminalLaunchLocationsById.set(session.target.terminalId, { |
| 111 | cwd: launchContext?.cwd ?? summary.cwd, |
| 112 | worktreePath: worktreePathForLaunch, |
| 113 | runtimeEnv: projectScriptRuntimeEnv({ |
| 114 | project: { cwd: project.workspaceRoot }, |
| 115 | worktreePath: worktreePathForLaunch, |
| 116 | }), |
| 117 | }); |
| 118 | } |
| 119 | const serverOrderedTerminalIds = drawerTerminalSessions.map( |
| 120 | (session) => session.target.terminalId, |
| 121 | ); |
| 122 | const storeSetTerminalHeight = useTerminalUiStateStore((state) => state.setTerminalHeight); |
| 123 | const storeSplitTerminal = useTerminalUiStateStore((state) => state.splitTerminal); |
| 124 | const storeSplitTerminalVertical = useTerminalUiStateStore( |
| 125 | (state) => state.splitTerminalVertical, |
| 126 | ); |
| 127 | const storeNewTerminal = useTerminalUiStateStore((state) => state.newTerminal); |
| 128 | const storeSetActiveTerminal = useTerminalUiStateStore((state) => state.setActiveTerminal); |
| 129 | const storeCloseTerminal = useTerminalUiStateStore((state) => state.closeTerminal); |
| 130 | const reconcileTerminalIds = useTerminalUiStateStore((state) => state.reconcileTerminalIds); |
| 131 | |
| 132 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- feeds the server's terminal list into the terminal UI store, which owns how local ids follow it |
| 133 | useEffect(() => { |
| 134 | if (snapshotId === undefined) return; |
| 135 | reconcileTerminalIds(threadRef, serverOrderedTerminalIds, snapshotId); |
| 136 | }, [reconcileTerminalIds, serverOrderedTerminalIds, snapshotId, threadRef]); |
| 137 | const worktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null; |
| 138 | const effectiveWorktreePath = launchContext !== null ? launchContext.worktreePath : worktreePath; |
| 139 | const cwd = |
| 140 | launchContext?.cwd ?? |
| 141 | (project |
| 142 | ? projectScriptCwd({ |
| 143 | project: { cwd: project.workspaceRoot }, |
| 144 | worktreePath: effectiveWorktreePath, |
| 145 | }) |
| 146 | : null); |
| 147 | const runtimeEnv = project |
| 148 | ? projectScriptRuntimeEnv({ |
| 149 | project: { cwd: project.workspaceRoot }, |
| 150 | worktreePath: effectiveWorktreePath, |
| 151 | }) |
| 152 | : {}; |
| 153 | |
| 154 | const setTerminalHeight = (height: number) => { |
| 155 | storeSetTerminalHeight(threadRef, height); |
| 156 | }; |
| 157 | |
| 158 | const launchDrawerTerminal = (place: (terminalId: string) => void) => { |
| 159 | if (!cwd) return; |
| 160 | const launched = terminalActions.launchTerminal({ |
| 161 | place, |
| 162 | location: { cwd, worktreePath: effectiveWorktreePath }, |
| 163 | }); |
| 164 | if (!launched) return; |
| 165 | if (visible) focusTerminal(threadRef, launched.terminalId); |
| 166 | void launched.result; |
| 167 | }; |
| 168 | const splitTerminal = useStableCallback(() => |
| 169 | launchDrawerTerminal((terminalId) => storeSplitTerminal(threadRef, terminalId)), |
| 170 | ); |
| 171 | const splitTerminalVertical = useStableCallback(() => |
| 172 | launchDrawerTerminal((terminalId) => storeSplitTerminalVertical(threadRef, terminalId)), |
| 173 | ); |
| 174 | const createNewTerminal = useStableCallback(() => |
| 175 | launchDrawerTerminal((terminalId) => storeNewTerminal(threadRef, terminalId)), |
| 176 | ); |
| 177 | |
| 178 | const activateTerminal = (terminalId: string) => { |
| 179 | storeSetActiveTerminal(threadRef, terminalId); |
| 180 | if (visible) focusTerminal(threadRef, terminalId); |
| 181 | }; |
| 182 | |
| 183 | const closeTerminal = useStableCallback((terminalId: string) => { |
| 184 | if (!terminalActions.canWrite()) return; |
| 185 | terminalActions.closeTerminalSession(terminalId); |
| 186 | storeCloseTerminal(threadRef, terminalId); |
| 187 | if (visible) focusActiveDrawerTerminal(threadRef); |
| 188 | }); |
| 189 | |
| 190 | const handleAddTerminalContext = (selection: TerminalContextSelection) => { |
| 191 | if (!visible) { |
| 192 | return; |
| 193 | } |
| 194 | onAddTerminalContext(selection); |
| 195 | }; |
| 196 | |
| 197 | if (!project || (!terminalUiState.terminalOpen && !active) || !cwd) { |
| 198 | return null; |
| 199 | } |
| 200 | |
| 201 | return ( |
| 202 | <div |
| 203 | className={cn( |
| 204 | "grid shrink-0 overflow-clip", |
| 205 | active ? (visible ? "grid-rows-[1fr]" : "grid-rows-[0fr]") : "hidden", |
| 206 | active && |
| 207 | "[[data-panel-animations=true]_&]:transition-[grid-template-rows] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out", |
| 208 | active && visible && "[[data-panel-animations=true]_&]:starting:grid-rows-[0fr]!", |
| 209 | )} |
| 210 | > |
| 211 | <div className="min-h-0 overflow-clip"> |
| 212 | <ThreadTerminalDrawer |
| 213 | threadRef={threadRef} |
| 214 | threadId={threadId} |
| 215 | cwd={cwd} |
| 216 | worktreePath={effectiveWorktreePath} |
| 217 | runtimeEnv={runtimeEnv} |
| 218 | visible={visible} |
| 219 | height={terminalUiState.terminalHeight} |
| 220 | composerHeight={composerHeight} |
| 221 | // Known-session order is MRU and changes on focus; persisted store order keeps sidebar labels stable. |
| 222 | terminalIds={canOperateTerminal ? terminalUiState.terminalIds : serverOrderedTerminalIds} |
| 223 | activeTerminalId={terminalUiState.activeTerminalId} |
| 224 | terminalGroups={terminalUiState.terminalGroups} |
| 225 | activeTerminalGroupId={terminalUiState.activeTerminalGroupId} |
| 226 | onSplitTerminal={splitTerminal} |
| 227 | onSplitTerminalVertical={splitTerminalVertical} |
| 228 | onNewTerminal={createNewTerminal} |
| 229 | splitShortcutLabel={visible ? splitShortcutLabel : undefined} |
| 230 | splitVerticalShortcutLabel={visible ? splitVerticalShortcutLabel : undefined} |
| 231 | newShortcutLabel={visible ? newShortcutLabel : undefined} |
| 232 | closeShortcutLabel={visible ? closeShortcutLabel : undefined} |
| 233 | keybindings={keybindings} |
| 234 | onActiveTerminalChange={activateTerminal} |
| 235 | onCloseTerminal={closeTerminal} |
| 236 | onHeightChange={setTerminalHeight} |
| 237 | onAddTerminalContext={handleAddTerminalContext} |
| 238 | terminalLabelsById={terminalLabelsById} |
| 239 | terminalLaunchLocationsById={terminalLaunchLocationsById} |
| 240 | /> |
| 241 | </div> |
| 242 | </div> |
| 243 | ); |
| 244 | }); |
| 245 | |
| 246 | export interface PersistentThreadTerminalPanelProps { |
| 247 | visible: boolean; |
| 248 | threadRef: ScopedThreadRef; |
| 249 | surface: Extract<RightPanelSurface, { kind: "terminal" }>; |
| 250 | launchContext: PersistentTerminalLaunchContext | null; |
| 251 | keybindings: ResolvedKeybindingsConfig; |
| 252 | onAddTerminalContext: (selection: TerminalContextSelection) => void; |
| 253 | onSplitTerminal: () => void; |
| 254 | onSplitTerminalVertical: () => void; |
| 255 | onNewTerminal: () => void; |
| 256 | onActiveTerminalChange: (terminalId: string) => void; |
| 257 | onCloseTerminal: (terminalId: string, threadRef?: ScopedThreadRef) => void; |
| 258 | splitShortcutLabel?: string | undefined; |
| 259 | splitVerticalShortcutLabel?: string | undefined; |
| 260 | newShortcutLabel?: string | undefined; |
| 261 | closeShortcutLabel?: string | undefined; |
| 262 | } |
| 263 | |
| 264 | export const PersistentThreadTerminalPanel = memo(function PersistentThreadTerminalPanel({ |
| 265 | visible, |
| 266 | threadRef, |
| 267 | surface, |
| 268 | launchContext, |
| 269 | keybindings, |
| 270 | onAddTerminalContext, |
| 271 | onSplitTerminal, |
| 272 | onSplitTerminalVertical, |
| 273 | onNewTerminal, |
| 274 | onActiveTerminalChange, |
| 275 | onCloseTerminal, |
| 276 | splitShortcutLabel, |
| 277 | splitVerticalShortcutLabel, |
| 278 | newShortcutLabel, |
| 279 | closeShortcutLabel, |
| 280 | }: PersistentThreadTerminalPanelProps) { |
| 281 | const messages = useMessages(); |
| 282 | const serverThread = useQuietThreadShell(threadRef); |
| 283 | const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef)); |
| 284 | const projectRef = serverThread |
| 285 | ? scopeProjectRef(serverThread.environmentId, serverThread.projectId) |
| 286 | : draftThread |
| 287 | ? scopeProjectRef(draftThread.environmentId, draftThread.projectId) |
| 288 | : null; |
| 289 | const project = useProject(projectRef); |
| 290 | const knownTerminalSessions = useKnownTerminalSessions({ |
| 291 | environmentId: threadRef.environmentId, |
| 292 | threadId: threadRef.threadId, |
| 293 | }); |
| 294 | const threadWorktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null; |
| 295 | const activeSummary = |
| 296 | knownTerminalSessions?.find((session) => session.target.terminalId === surface.activeTerminalId) |
| 297 | ?.state.summary ?? null; |
| 298 | const worktreePath = resolveTerminalWorktreePath({ |
| 299 | launchContext, |
| 300 | summary: activeSummary, |
| 301 | threadWorktreePath, |
| 302 | }); |
| 303 | const cwd = |
| 304 | launchContext?.cwd ?? |
| 305 | activeSummary?.cwd ?? |
| 306 | (project ? projectScriptCwd({ project: { cwd: project.workspaceRoot }, worktreePath }) : null); |
| 307 | const runtimeEnv = project |
| 308 | ? projectScriptRuntimeEnv({ project: { cwd: project.workspaceRoot }, worktreePath }) |
| 309 | : {}; |
| 310 | const terminalLabelsById = new Map<string, string>(); |
| 311 | const terminalLaunchLocationsById = new Map<string, TerminalLaunchLocationWithEnv>(); |
| 312 | for (const terminalId of surface.terminalIds) { |
| 313 | const summary = |
| 314 | knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state |
| 315 | .summary ?? null; |
| 316 | terminalLabelsById.set(terminalId, resolveTerminalSessionLabel(terminalId, summary, messages)); |
| 317 | if (!project) continue; |
| 318 | const terminalWorktreePath = resolveTerminalWorktreePath({ |
| 319 | launchContext, |
| 320 | summary, |
| 321 | threadWorktreePath, |
| 322 | }); |
| 323 | terminalLaunchLocationsById.set(terminalId, { |
| 324 | cwd: |
| 325 | launchContext?.cwd ?? |
| 326 | summary?.cwd ?? |
| 327 | projectScriptCwd({ |
| 328 | project: { cwd: project.workspaceRoot }, |
| 329 | worktreePath: terminalWorktreePath, |
| 330 | }), |
| 331 | worktreePath: terminalWorktreePath, |
| 332 | runtimeEnv: projectScriptRuntimeEnv({ |
| 333 | project: { cwd: project.workspaceRoot }, |
| 334 | worktreePath: terminalWorktreePath, |
| 335 | }), |
| 336 | }); |
| 337 | } |
| 338 | |
| 339 | if (!project || !cwd) return null; |
| 340 | |
| 341 | return ( |
| 342 | <ThreadTerminalDrawer |
| 343 | mode="panel" |
| 344 | visible={visible} |
| 345 | threadRef={threadRef} |
| 346 | threadId={threadRef.threadId} |
| 347 | cwd={cwd} |
| 348 | worktreePath={worktreePath} |
| 349 | runtimeEnv={runtimeEnv} |
| 350 | height={0} |
| 351 | terminalIds={surface.terminalIds} |
| 352 | activeTerminalId={surface.activeTerminalId} |
| 353 | terminalGroups={[ |
| 354 | { |
| 355 | id: surface.id, |
| 356 | terminalIds: surface.terminalIds, |
| 357 | ...(surface.splitDirection === "vertical" ? { splitDirection: "vertical" as const } : {}), |
| 358 | }, |
| 359 | ]} |
| 360 | activeTerminalGroupId={surface.id} |
| 361 | onSplitTerminal={onSplitTerminal} |
| 362 | onSplitTerminalVertical={onSplitTerminalVertical} |
| 363 | onNewTerminal={onNewTerminal} |
| 364 | splitShortcutLabel={splitShortcutLabel} |
| 365 | splitVerticalShortcutLabel={splitVerticalShortcutLabel} |
| 366 | newShortcutLabel={newShortcutLabel} |
| 367 | closeShortcutLabel={closeShortcutLabel} |
| 368 | onActiveTerminalChange={onActiveTerminalChange} |
| 369 | onCloseTerminal={onCloseTerminal} |
| 370 | onHeightChange={() => undefined} |
| 371 | onAddTerminalContext={onAddTerminalContext} |
| 372 | terminalLabelsById={terminalLabelsById} |
| 373 | terminalLaunchLocationsById={terminalLaunchLocationsById} |
| 374 | keybindings={keybindings} |
| 375 | /> |
| 376 | ); |
| 377 | }); |