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

terminalLaunchContext.ts

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