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

terminalLaunchContext.ts

90 lines2.8 KB

1import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2​
3interface TerminalLocationLike {
4 readonly cwd: string;
5 readonly worktreePath: string | null;
6}
7​
8interface PendingTerminalLaunchTarget {
9 readonly environmentId: EnvironmentId;
10 readonly threadId: ThreadId;
11 readonly terminalId: string;
13​
14export interface PendingTerminalLaunch {
15 readonly cwd: string;
16 readonly worktreePath: string | null;
17 readonly env?: Record<string, string>;
18 readonly initialInput?: string;
20​
21const pendingTerminalLaunches = new Map<string, PendingTerminalLaunch>();
22​
23function pendingTerminalLaunchKey(input: PendingTerminalLaunchTarget): string {
24 return `${input.environmentId}:${input.threadId}:${input.terminalId}`;
26​
27export function stagePendingTerminalLaunch(input: {
28 readonly target: PendingTerminalLaunchTarget;
29 readonly launch: PendingTerminalLaunch;
30}) {
31 pendingTerminalLaunches.set(pendingTerminalLaunchKey(input.target), {
32 cwd: input.launch.cwd,
33 worktreePath: input.launch.worktreePath,
34 env: input.launch.env ? { ...input.launch.env } : undefined,
35 initialInput: input.launch.initialInput,
36 });
38​
39export function peekPendingTerminalLaunch(
40 target: PendingTerminalLaunchTarget,
41): PendingTerminalLaunch | null {
42 return pendingTerminalLaunches.get(pendingTerminalLaunchKey(target)) ?? null;
44​
45export function takePendingTerminalLaunch(
46 target: PendingTerminalLaunchTarget,
47): PendingTerminalLaunch | null {
48 const key = pendingTerminalLaunchKey(target);
49 const launch = pendingTerminalLaunches.get(key) ?? null;
50 if (launch) {
51 pendingTerminalLaunches.delete(key);
52 }
53​
54 return launch;
56​
57export function resolvePreferredThreadWorktreePath(input: {
58 readonly threadShellWorktreePath: string | null;
59 readonly threadDetailWorktreePath: string | null;
60}): string | null {
61 return input.threadDetailWorktreePath ?? input.threadShellWorktreePath ?? null;
63​
64export function resolveTerminalOpenLocation(input: {
65 readonly terminalLocation: TerminalLocationLike | null;
66 readonly activeSessionLocation: TerminalLocationLike | null;
67 readonly workspaceRoot: string;
68 readonly threadShellWorktreePath: string | null;
69 readonly threadDetailWorktreePath: string | null;
70}): {
71 readonly cwd: string;
72 readonly worktreePath: string | null;
73} {
74 const preferredThreadWorktreePath = resolvePreferredThreadWorktreePath({
75 threadShellWorktreePath: input.threadShellWorktreePath,
76 threadDetailWorktreePath: input.threadDetailWorktreePath,
77 });
78​
79 return {
80 cwd:
81 input.terminalLocation?.cwd ??
82 input.activeSessionLocation?.cwd ??
83 preferredThreadWorktreePath ??
84 input.workspaceRoot,
85 worktreePath:
86 input.terminalLocation?.worktreePath ??
87 input.activeSessionLocation?.worktreePath ??
88 preferredThreadWorktreePath,
89 };