@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 115 lines 4.02 KB
Raw
1 import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2 import { useState } from "react";
3
4 interface TerminalLocationLike {
5 readonly cwd: string;
6 readonly worktreePath: string | null;
7 }
8
9 interface PendingTerminalLaunchTarget {
10 readonly environmentId: EnvironmentId;
11 readonly threadId: ThreadId;
12 readonly terminalId: string;
13 }
14
15 export 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;
21 }
22
23 const pendingTerminalLaunches = new Map<string, PendingTerminalLaunch>();
24
25 function pendingTerminalLaunchKey(input: PendingTerminalLaunchTarget): string {
26 return `${input.environmentId}:${input.threadId}:${input.terminalId}`;
27 }
28
29 export 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 });
40 }
41
42 export 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;
52 }
53
54 export function resolvePreferredThreadWorktreePath(input: {
55 readonly threadShellWorktreePath: string | null;
56 readonly threadDetailWorktreePath: string | null;
57 }): string | null {
58 return input.threadDetailWorktreePath ?? input.threadShellWorktreePath ?? null;
59 }
60
61 export 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 };
85 }
86
87 /** The first location resolved for a terminal target stays fixed for that target. */
88 export 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;
115 }