Open History to see the last commit
1import type {
2 EnvironmentProject,
3 EnvironmentThreadShell,
4} from "@t3tools/client-runtime/state/shell";
5import type { Dispatch } from "react";
6import type { ComposerHandleRef } from "../../composerHandleContext";
7import type { selectThreadTerminalUiState } from "../../terminalUiStateStore";
8import { useStableCallback } from "../../hooks/useStableCallback";
9import { useThreadTerminalActions } from "../terminal/useThreadTerminalActions";
10import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
11import {
12 AuthTerminalReadScope,
13 AuthTerminalOperateScope,
14 type EnvironmentId,
15 type ProjectScript,
16 type ScopedThreadRef,
17 type ThreadId,
18} from "@t3tools/contracts";
19import { projectScriptTerminalInput } from "@t3tools/shared/projectScripts";
20import { type SetStateAction } from "react";
21import {
22 isAtomCommandInterrupted,
23 squashAtomCommandFailure,
24} from "@t3tools/client-runtime/state/runtime";
25import { MAX_TERMINALS_PER_GROUP } from "../../types";
26import {
27 selectThreadRightPanelState,
28 type RightPanelSurface,
29 useRightPanelStore,
30} from "../../rightPanelStore";
31import { isPreviewSupportedInRuntime } from "../../previewStateStore";
32import { openUrlInPreview } from "../../browser/openFileInPreview";
33import { resolveDiscoveredServerUrl } from "../../browser/browserTargetResolver";
34import { stackedThreadToast, toastManager } from "../ui/toast";
35import { confirmTerminalClose } from "../../lib/terminalCloseConfirm";
36import { findReusableProjectScriptTerminal } from "../../lib/projectScriptTerminal";
37import { type TerminalContextSelection } from "../../lib/terminalContext";
38import {
39 focusActiveDrawerTerminal,
40 focusActivePanelTerminal,
41 focusTerminal,
42} from "../../lib/terminalFocus";
43import { useTerminalUiStateStore } from "../../terminalUiStateStore";
44import {
45 LAST_INVOKED_SCRIPT_BY_PROJECT_KEY,
46 LastInvokedScriptByProjectSchema,
47 makeThreadErrorSetter,
48} from "../ChatView.logic";
49import { useLocalStorage } from "~/hooks/useLocalStorage";
50import { previewEnvironment } from "../../state/preview";
51import { readEnvironmentScope } from "../../state/session";
52import { useAtomCommand } from "../../state/use-atom-command";
53import type { ChatShortcutCommands } from "./useChatShortcuts";
54const SCRIPT_TERMINAL_COLS = 120;
55const SCRIPT_TERMINAL_ROWS = 30;
56
57export interface TerminalLaunchContext {
58 threadId: ThreadId;
59 cwd: string;
60 worktreePath: string | null;
61}
62
63interface ChatTerminalActionsInput {
64 readonly environmentId: EnvironmentId;
65 readonly composerRef: ComposerHandleRef;
66 readonly terminalUiState: ReturnType<typeof selectThreadTerminalUiState>;
67 readonly activeThread: EnvironmentThreadShell | undefined;
68 readonly activeThreadId: ThreadId | null;
69 readonly activeThreadRef: ScopedThreadRef | null;
70 readonly activeProject: EnvironmentProject | null;
71 readonly activeRightPanelSurface: RightPanelSurface | null;
72 readonly runningTerminalIds: ReadonlyArray<string>;
73 readonly activeServerOrderedTerminalIds: ReadonlyArray<string>;
74 readonly activeTerminalLabelsById: ReadonlyMap<string, string>;
75 readonly panelTerminalIds: ReadonlySet<string>;
76 readonly canReuseTerminal: boolean;
77 readonly gitCwd: string | null;
78 readonly setTerminalUiLaunchContext: Dispatch<SetStateAction<TerminalLaunchContext | null>>;
79 readonly setThreadError: ReturnType<typeof makeThreadErrorSetter>;
80}
81
82/**
83 * The thread's terminals: the drawer's and the right panel's, and project scripts and chat
84 * code blocks, which run in a reused or new drawer terminal.
85 */
86export function useChatTerminalActions(input: ChatTerminalActionsInput) {
87 const {
88 environmentId,
89 composerRef,
90 terminalUiState,
91 activeThread,
92 activeThreadId,
93 activeThreadRef,
94 activeProject,
95 activeRightPanelSurface,
96 runningTerminalIds,
97 activeServerOrderedTerminalIds,
98 activeTerminalLabelsById,
99 panelTerminalIds,
100 canReuseTerminal,
101 gitCwd,
102 setTerminalUiLaunchContext,
103 setThreadError,
104 } = input;
105 const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
106
107 const hasTerminalWriteAccess = () =>
108 readEnvironmentScope(environmentId, AuthTerminalOperateScope);
109 const [lastInvokedScriptByProjectId, setLastInvokedScriptByProjectId] = useLocalStorage(
110 LAST_INVOKED_SCRIPT_BY_PROJECT_KEY,
111 {},
112 LastInvokedScriptByProjectSchema,
113 );
114 const storeSetTerminalOpen = useTerminalUiStateStore((s) => s.setTerminalOpen);
115 const storeEnsureTerminal = useTerminalUiStateStore((state) => state.ensureTerminal);
116 const storeSplitTerminal = useTerminalUiStateStore((s) => s.splitTerminal);
117 const storeSplitTerminalVertical = useTerminalUiStateStore((s) => s.splitTerminalVertical);
118 const storeNewTerminal = useTerminalUiStateStore((s) => s.newTerminal);
119 const storeCloseTerminal = useTerminalUiStateStore((s) => s.closeTerminal);
120 const terminalActions = useThreadTerminalActions(activeThreadRef);
121
122 const activeTerminalGroup =
123 terminalUiState.terminalGroups.find(
124 (group) => group.id === terminalUiState.activeTerminalGroupId,
125 ) ??
126 terminalUiState.terminalGroups.find((group) =>
127 group.terminalIds.includes(terminalUiState.activeTerminalId),
128 ) ??
129 null;
130 const hasReachedSplitLimit =
131 (activeTerminalGroup?.terminalIds.length ?? 0) >= MAX_TERMINALS_PER_GROUP;
132 /*__HOOK__*/
133 const addTerminalContextToDraft = (selection: TerminalContextSelection) => {
134 composerRef.current?.addTerminalContext(selection);
135 };
136 const addTerminalContextToDraftStable = useStableCallback(addTerminalContextToDraft);
137 const setTerminalOpen = (open: boolean) => {
138 if (!activeThreadRef) return;
139 storeSetTerminalOpen(activeThreadRef, open);
140 focusActiveDrawerTerminal(activeThreadRef);
141 };
142 const setTerminalOpenStable = useStableCallback(setTerminalOpen);
143 const toggleTerminalVisibility = () => {
144 if (!activeThreadRef) return;
145 const nextOpen = !terminalUiState.terminalOpen;
146 if (
147 nextOpen &&
148 !readEnvironmentScope(environmentId, AuthTerminalReadScope) &&
149 !hasTerminalWriteAccess()
150 )
151 return;
152 if (nextOpen && hasTerminalWriteAccess() && terminalUiState.terminalIds.length === 0) {
153 // Server-created setup terminals exist before this client's drawer mounts.
154 // Reuse them before allocating a shell from the still-empty UI state.
155 const existingTerminalId =
156 runningTerminalIds.find((id) => !panelTerminalIds.has(id)) ??
157 activeServerOrderedTerminalIds.find((id) => !panelTerminalIds.has(id));
158 if (existingTerminalId !== undefined) {
159 storeEnsureTerminal(activeThreadRef, existingTerminalId, { open: true, active: true });
160 focusActiveDrawerTerminal(activeThreadRef);
161 return;
162 }
163 void terminalActions.launchTerminal({
164 place: (terminalId) => storeEnsureTerminal(activeThreadRef, terminalId, { open: true }),
165 })?.result;
166 focusActiveDrawerTerminal(activeThreadRef);
167 return;
168 }
169 setTerminalOpen(nextOpen);
170 };
171 const toggleTerminalVisibilityStable = useStableCallback(toggleTerminalVisibility);
172 const splitTerminal = (direction: "horizontal" | "vertical" = "horizontal") => {
173 if (!activeThreadRef || hasReachedSplitLimit) return;
174 const launched = terminalActions.launchTerminal({
175 place: (terminalId) =>
176 direction === "vertical"
177 ? storeSplitTerminalVertical(activeThreadRef, terminalId)
178 : storeSplitTerminal(activeThreadRef, terminalId),
179 });
180 if (!launched) return;
181 focusTerminal(activeThreadRef, launched.terminalId);
182 void launched.result;
183 };
184 const splitTerminalStable = useStableCallback(splitTerminal);
185 const createNewTerminal = () => {
186 if (!activeThreadRef) return;
187 const launched = terminalActions.launchTerminal({
188 place: (terminalId) => storeNewTerminal(activeThreadRef, terminalId),
189 });
190 if (!launched) return;
191 focusTerminal(activeThreadRef, launched.terminalId);
192 void launched.result;
193 };
194 const createNewTerminalStable = useStableCallback(createNewTerminal);
195 const closeTerminal = (terminalId: string) => {
196 if (!hasTerminalWriteAccess() || !activeThreadRef) return;
197 terminalActions.closeTerminalSession(terminalId, activeThreadRef);
198 storeCloseTerminal(activeThreadRef, terminalId);
199 focusActiveDrawerTerminal(activeThreadRef);
200 };
201 const runProjectScript = async (
202 script: ProjectScript,
203 options?: {
204 cwd?: string;
205 env?: Record<string, string>;
206 worktreePath?: string | null;
207 preferNewTerminal?: boolean;
208 rememberAsLastInvoked?: boolean;
209 shellHistory?: "preserve" | "suppress";
210 },
211 ) => {
212 if (!hasTerminalWriteAccess() || !activeThreadId || !activeProject || !activeThread) return;
213 if (options?.rememberAsLastInvoked !== false) {
214 setLastInvokedScriptByProjectId((current) => {
215 if (current[activeProject.id] === script.id) return current;
216 return { ...current, [activeProject.id]: script.id };
217 });
218 }
219 const targetCwd = options?.cwd ?? gitCwd ?? activeProject.workspaceRoot;
220 const reusableTerminalId =
221 script.stopOnSettle ||
222 options?.preferNewTerminal ||
223 !canReuseTerminal ||
224 !readEnvironmentScope(environmentId, AuthTerminalReadScope)
225 ? null
226 : findReusableProjectScriptTerminal({
227 // Server sessions cover drawer terminals this client has not opened yet, such as a
228 // worktree setup shell; the helper drops the panel-owned ones.
229 drawerTerminalIds: [
230 ...new Set([...terminalUiState.terminalIds, ...activeServerOrderedTerminalIds]),
231 ],
232 activeTerminalId: terminalUiState.activeTerminalId,
233 panelTerminalIds,
234 runningTerminalIds,
235 });
236 const targetWorktreePath = options?.worktreePath ?? activeThread.worktreePath ?? null;
237
238 setTerminalUiLaunchContext({
239 threadId: activeThreadId,
240 cwd: targetCwd,
241 worktreePath: targetWorktreePath,
242 });
243 if (!activeThreadRef) {
244 return;
245 }
246
247 const launched = terminalActions.launchTerminal({
248 // The drawer opens with the target already placed: opening it empty would claim
249 // DEFAULT_THREAD_TERMINAL_ID, which a right-panel terminal may already own.
250 place: (terminalId) =>
251 reusableTerminalId === null
252 ? storeNewTerminal(activeThreadRef, terminalId)
253 : storeEnsureTerminal(activeThreadRef, terminalId, { open: true }),
254 ...(reusableTerminalId === null
255 ? { size: { cols: SCRIPT_TERMINAL_COLS, rows: SCRIPT_TERMINAL_ROWS } }
256 : { terminalId: reusableTerminalId }),
257 location: { cwd: targetCwd, worktreePath: targetWorktreePath },
258 stopOnSettle: script.stopOnSettle ?? false,
259 ...(options?.env ? { extraEnv: options.env } : {}),
260 });
261 if (!launched) return;
262 const targetTerminalId = launched.terminalId;
263 focusTerminal(activeThreadRef, targetTerminalId);
264 const openResult = await launched.result;
265 if (openResult._tag === "Failure") {
266 if (!isAtomCommandInterrupted(openResult)) {
267 const error = squashAtomCommandFailure(openResult);
268 setThreadError(
269 activeThreadId,
270 error instanceof Error ? error.message : `Failed to run script "${script.name}".`,
271 );
272 }
273 return;
274 }
275
276 const writeResult = await terminalActions.writeToTerminal(
277 targetTerminalId,
278 options?.shellHistory === "preserve"
279 ? `${script.command}\r`
280 : projectScriptTerminalInput(script.command),
281 );
282 if (!writeResult) return;
283 if (writeResult._tag === "Failure") {
284 if (!isAtomCommandInterrupted(writeResult)) {
285 const error = squashAtomCommandFailure(writeResult);
286 setThreadError(
287 activeThreadId,
288 error instanceof Error ? error.message : `Failed to run script "${script.name}".`,
289 );
290 }
291 return;
292 }
293 if (script.autoOpenPreview && script.previewUrl && isPreviewSupportedInRuntime()) {
294 const previewResult = await openUrlInPreview({
295 threadRef: activeThreadRef,
296 url: resolveDiscoveredServerUrl(activeThreadRef.environmentId, script.previewUrl),
297 openPreview,
298 });
299 if (previewResult._tag === "Failure" && !isAtomCommandInterrupted(previewResult)) {
300 const error = squashAtomCommandFailure(previewResult);
301 toastManager.add(
302 stackedThreadToast({
303 type: "error",
304 title: "Could not open preview",
305 description: error instanceof Error ? error.message : "An unexpected error occurred.",
306 }),
307 );
308 }
309 }
310 };
311 const runProjectScriptStable = useStableCallback(runProjectScript);
312
313 const runShellCommand = (command: string) => {
314 void runProjectScript(
315 {
316 id: "chat-code-block",
317 name: "Chat code block",
318 command,
319 icon: "play",
320 runOnWorktreeCreate: false,
321 },
322 { rememberAsLastInvoked: false, shellHistory: "preserve" },
323 );
324 };
325 const runShellCommandStable = useStableCallback(runShellCommand);
326 const addTerminalSurface = () => {
327 if (!activeThreadRef) return;
328 const launched = terminalActions.launchTerminal({
329 place: (terminalId) =>
330 useRightPanelStore.getState().openTerminal(activeThreadRef, terminalId),
331 });
332 if (!launched) return;
333 focusTerminal(activeThreadRef, launched.terminalId);
334 void launched.result;
335 };
336 const addTerminalSurfaceStable = useStableCallback(addTerminalSurface);
337 const splitPanelTerminal = (direction: "horizontal" | "vertical" = "horizontal") => {
338 if (
339 !activeThreadRef ||
340 activeRightPanelSurface?.kind !== "terminal" ||
341 activeRightPanelSurface.terminalIds.length >= MAX_TERMINALS_PER_GROUP
342 ) {
343 return;
344 }
345 const surfaceId = activeRightPanelSurface.id;
346 const launched = terminalActions.launchTerminal({
347 place: (terminalId) =>
348 useRightPanelStore
349 .getState()
350 .splitTerminal(activeThreadRef, surfaceId, terminalId, direction),
351 });
352 if (!launched) return;
353 focusTerminal(activeThreadRef, launched.terminalId);
354 void launched.result;
355 };
356 const splitPanelTerminalStable = useStableCallback(splitPanelTerminal);
357 const splitPanelTerminalVertical = () => {
358 splitPanelTerminal("vertical");
359 };
360 const splitPanelTerminalVerticalStable = useStableCallback(splitPanelTerminalVertical);
361 const activatePanelTerminal = (terminalId: string) => {
362 if (!activeThreadRef || activeRightPanelSurface?.kind !== "terminal") return;
363 useRightPanelStore
364 .getState()
365 .activateTerminal(activeThreadRef, activeRightPanelSurface.id, terminalId);
366 focusTerminal(activeThreadRef, terminalId);
367 };
368 const activatePanelTerminalStable = useStableCallback(activatePanelTerminal);
369 const closePanelTerminal = (terminalId: string, target = activeThreadRef) => {
370 if (!target || !readEnvironmentScope(target.environmentId, AuthTerminalOperateScope)) return;
371 const store = useRightPanelStore.getState();
372 const surface = selectThreadRightPanelState(store.byThreadKey, target).surfaces.find(
373 (entry) => entry.kind === "terminal" && entry.terminalIds.includes(terminalId),
374 );
375 if (!surface) return;
376 terminalActions.closeTerminalSession(terminalId, target);
377 storeCloseTerminal(target, terminalId);
378 store.closeTerminal(target, surface.id, terminalId);
379 focusActivePanelTerminal(target);
380 };
381 const closePanelTerminalStable = useStableCallback(closePanelTerminal);
382 const requestCloseTerminal = (terminalId: string) => {
383 if (!hasTerminalWriteAccess()) return;
384 const label = activeTerminalLabelsById.get(terminalId) ?? getTerminalLabel(terminalId);
385 void confirmTerminalClose([label]).then((confirmed) => {
386 if (confirmed && readEnvironmentScope(environmentId, AuthTerminalOperateScope)) {
387 closeTerminal(terminalId);
388 }
389 });
390 };
391 const requestCloseTerminalStable = useStableCallback(requestCloseTerminal);
392 const requestClosePanelTerminal = (terminalId: string) => {
393 if (!hasTerminalWriteAccess()) return;
394 const label = activeTerminalLabelsById.get(terminalId) ?? getTerminalLabel(terminalId);
395 void confirmTerminalClose([label]).then((confirmed) => {
396 if (confirmed && readEnvironmentScope(environmentId, AuthTerminalOperateScope)) {
397 closePanelTerminal(terminalId);
398 }
399 });
400 };
401 const requestClosePanelTerminalStable = useStableCallback(requestClosePanelTerminal);
402 const openTerminalIfClosed = () => {
403 if (!terminalUiState.terminalOpen) setTerminalOpen(true);
404 };
405 // A focused right-panel terminal takes the split/new/close chords; otherwise they act on the drawer.
406 const shortcuts = {
407 "terminal.toggle": () => {
408 if (
409 !terminalUiState.terminalOpen &&
410 !readEnvironmentScope(environmentId, AuthTerminalReadScope) &&
411 !hasTerminalWriteAccess()
412 )
413 return false;
414 toggleTerminalVisibility();
415 },
416 "terminal.split": ({ terminalFocusOwner }) => {
417 if (!hasTerminalWriteAccess()) return false;
418 if (terminalFocusOwner === "right-panel") return void splitPanelTerminal();
419 openTerminalIfClosed();
420 splitTerminal();
421 },
422 "terminal.splitVertical": ({ terminalFocusOwner }) => {
423 if (!hasTerminalWriteAccess()) return false;
424 if (terminalFocusOwner === "right-panel") return void splitPanelTerminal("vertical");
425 openTerminalIfClosed();
426 splitTerminal("vertical");
427 },
428 "terminal.close": ({ terminalFocusOwner }) => {
429 if (!hasTerminalWriteAccess()) return false;
430 if (terminalFocusOwner === "right-panel" && activeRightPanelSurface?.kind === "terminal") {
431 requestClosePanelTerminal(activeRightPanelSurface.activeTerminalId);
432 return;
433 }
434 if (terminalUiState.terminalOpen) requestCloseTerminal(terminalUiState.activeTerminalId);
435 },
436 "terminal.new": ({ terminalFocusOwner }) => {
437 if (!hasTerminalWriteAccess()) return false;
438 if (terminalFocusOwner === "right-panel") return void addTerminalSurface();
439 openTerminalIfClosed();
440 createNewTerminal();
441 },
442 } satisfies ChatShortcutCommands;
443 return {
444 shortcuts,
445 activatePanelTerminalStable,
446 addTerminalContextToDraftStable,
447 addTerminalSurfaceStable,
448 closePanelTerminalStable,
449 createNewTerminalStable,
450 lastInvokedScriptByProjectId,
451 requestClosePanelTerminalStable,
452 requestCloseTerminalStable,
453 runProjectScriptStable,
454 runShellCommandStable,
455 setTerminalOpenStable,
456 splitPanelTerminalStable,
457 splitPanelTerminalVerticalStable,
458 splitTerminalStable,
459 toggleTerminalVisibilityStable,
460 };
461}