Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

terminalLaunchContext.ts

115 lines4.0 KB

1import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { useState } from "react";
3​
4interface TerminalLocationLike {
5 readonly cwd: string;
6 readonly worktreePath: string | null;
7}
8​
9interface PendingTerminalLaunchTarget {
10 readonly environmentId: EnvironmentId;
11 readonly threadId: ThreadId;
12 readonly terminalId: string;
14​
15export interface PendingTerminalLaunch {
16 readonly cwd: string;
17 readonly worktreePath: string | null;
18 readonly env?: Record<string, string>;
19 readonly initialInput?: string;
20 readonly stopOnSettle?: boolean;
22​
23const pendingTerminalLaunches = new Map<string, PendingTerminalLaunch>();
24​
25function pendingTerminalLaunchKey(input: PendingTerminalLaunchTarget): string {
26 return `${input.environmentId}:${input.threadId}:${input.terminalId}`;
28​
29export function stagePendingTerminalLaunch(input: {
30 readonly target: PendingTerminalLaunchTarget;
31 readonly launch: PendingTerminalLaunch;
32}) {
33 pendingTerminalLaunches.set(pendingTerminalLaunchKey(input.target), {
34 cwd: input.launch.cwd,
35 worktreePath: input.launch.worktreePath,
36 env: input.launch.env ? { ...input.launch.env } : undefined,
37 initialInput: input.launch.initialInput,
38 stopOnSettle: input.launch.stopOnSettle,
39 });
41​
42export function takePendingTerminalLaunch(
43 target: PendingTerminalLaunchTarget,
44): PendingTerminalLaunch | null {
45 const key = pendingTerminalLaunchKey(target);
46 const launch = pendingTerminalLaunches.get(key) ?? null;
47 if (launch) {
48 pendingTerminalLaunches.delete(key);
49 }
50​
51 return launch;
53​
54export function resolvePreferredThreadWorktreePath(input: {
55 readonly threadShellWorktreePath: string | null;
56 readonly threadDetailWorktreePath: string | null;
57}): string | null {
58 return input.threadDetailWorktreePath ?? input.threadShellWorktreePath ?? null;
60​
61export function resolveTerminalOpenLocation(input: {
62 readonly terminalLocation: TerminalLocationLike | null;
63 readonly activeSessionLocation: TerminalLocationLike | null;
64 readonly workspaceRoot: string;
65 readonly threadShellWorktreePath: string | null;
66 readonly threadDetailWorktreePath: string | null;
67}): {
68 readonly cwd: string;
69 readonly worktreePath: string | null;
70} {
71 const sessionLocation = input.terminalLocation ?? input.activeSessionLocation;
72​
73 // Keep cwd paired with its identity; null identifies the project checkout.
74 if (sessionLocation !== null) {
75 return { cwd: sessionLocation.cwd, worktreePath: sessionLocation.worktreePath };
76 }
77 const preferredThreadWorktreePath = resolvePreferredThreadWorktreePath({
78 threadShellWorktreePath: input.threadShellWorktreePath,
79 threadDetailWorktreePath: input.threadDetailWorktreePath,
80 });
81 return {
82 cwd: preferredThreadWorktreePath ?? input.workspaceRoot,
83 worktreePath: preferredThreadWorktreePath,
84 };
86​
87/** The first location resolved for a terminal target stays fixed for that target. */
88export function useInitialTerminalLaunchLocation(input: {
89 readonly targetKey: string;
90 readonly pendingLaunch: PendingTerminalLaunch | null;
91 readonly isAuthenticated: boolean;
92 readonly canRead: boolean;
93 readonly canOperate: boolean;
94 readonly hasLoadedSessions: boolean;
95 readonly location: Parameters<typeof resolveTerminalOpenLocation>[0] | null;
96}) {
97 let candidate: TerminalLocationLike | null = null;
98 if (input.location !== null && input.isAuthenticated && (input.canRead || input.canOperate)) {
99 if (input.pendingLaunch !== null) {
100 candidate = {
101 cwd: input.pendingLaunch.cwd,
102 worktreePath: input.pendingLaunch.worktreePath,
103 };
104 } else if (!input.canRead || input.hasLoadedSessions) {
105 // Metadata must establish whether this target has a location before we retain a fallback.
106 candidate = resolveTerminalOpenLocation(input.location);
107 }
108 }
109 const [entry, setEntry] = useState(() => ({ key: input.targetKey, location: candidate }));
110 if (entry.key !== input.targetKey || (entry.location === null && candidate !== null)) {
111 setEntry({ key: input.targetKey, location: candidate });
112 return candidate;
113 }
114 return entry.location;