@devries-cameron20 / t3code Mirror · read-only / commits / e00cda2cd8
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 23, 08:00 UTC

refactor(web): move the persistent terminal drawer and panel out of ChatView.tsx (#20164)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 17:39 UTC e00cda2cd8c5d982710fed270cb92b80c817d142
2 files changed +654 -615
apps/web/src/components/ChatView.tsx
+7 -615
@@ -64,7 +64,6 @@ import {
64 type PreviewAnnotationPayload,
65 ProviderInstanceId,
66 type ServerProvider,
67 - type ResolvedKeybindingsConfig,
67 type ScopedThreadRef,
68 type ThreadId,
69 type ThreadLinkedPullRequest,
@@ -148,7 +147,6 @@ import { Atom } from "effect/reactivity";
147 import {
148 Fragment,
149 lazy,
151 - memo,
150 type SetStateAction,
151 Suspense,
152 useCallback,
@@ -284,7 +282,10 @@ import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
282 import { isEditableFocused } from "../lib/editableFocus";
283 import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
284 import { resolveChatShortcutCommand, shortcutLabelForCommand } from "../keybindings";
287 -import ThreadTerminalDrawer from "./ThreadTerminalDrawer";
285 +import {
286 + PersistentThreadTerminalDrawer,
287 + PersistentThreadTerminalPanel,
288 +} from "./PersistentThreadTerminals";
289 import {
290 AlarmClockIcon,
291 CheckCircle2Icon,
@@ -828,8 +829,6 @@ interface TerminalLaunchContext {
829 worktreePath: string | null;
830 }
831
831 -type PersistentTerminalLaunchContext = Pick<TerminalLaunchContext, "cwd" | "worktreePath">;
832 -
832 function useLocalDispatchState(input: {
833 activeThread: Thread | undefined;
834 activeLatestRun: Thread["latestRun"] | null;
@@ -900,616 +899,9 @@ function useLocalDispatchState(input: {
899 };
900 }
901
903 -function terminalIdListsEqual(left: readonly string[], right: readonly string[]): boolean {
904 - if (left.length !== right.length) {
905 - return false;
906 - }
907 - if (left.length === 0) {
908 - return true;
909 - }
910 - const sortedLeft = left.toSorted((a, b) => a.localeCompare(b));
911 - const sortedRight = right.toSorted((a, b) => a.localeCompare(b));
912 - for (let index = 0; index < sortedLeft.length; index += 1) {
913 - if (sortedLeft[index] !== sortedRight[index]) {
914 - return false;
915 - }
916 - }
917 - return true;
918 -}
919 -
920 -/**
921 - * Typical right after `terminal.open`: known-session list lags; reconciling would drop the new id
922 - * and later re-add it as a separate group (no split layout).
923 - */
924 -function serverTerminalIdsStrictSubsetOfClient(
925 - serverIds: readonly string[],
926 - clientIds: readonly string[],
927 -): boolean {
928 - if (serverIds.length >= clientIds.length || clientIds.length === 0) {
929 - return false;
930 - }
931 - const clientSet = new Set(clientIds);
932 - for (const id of serverIds) {
933 - if (!clientSet.has(id)) {
934 - return false;
935 - }
936 - }
937 - return true;
938 -}
939 -
940 -interface PersistentThreadTerminalDrawerProps {
941 - threadRef: { environmentId: EnvironmentId; threadId: ThreadId };
942 - threadId: ThreadId;
943 - active: boolean;
944 - launchContext: PersistentTerminalLaunchContext | null;
945 - focusRequestId: number;
946 - splitShortcutLabel: string | undefined;
947 - splitVerticalShortcutLabel: string | undefined;
948 - newShortcutLabel: string | undefined;
949 - closeShortcutLabel: string | undefined;
950 - keybindings: ResolvedKeybindingsConfig;
951 - onAddTerminalContext: (selection: TerminalContextSelection) => void;
952 -}
953 -
954 -const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDrawer({
955 - threadRef,
956 - threadId,
957 - active,
958 - launchContext,
959 - focusRequestId,
960 - splitShortcutLabel,
961 - splitVerticalShortcutLabel,
962 - newShortcutLabel,
963 - closeShortcutLabel,
964 - keybindings,
965 - onAddTerminalContext,
966 -}: PersistentThreadTerminalDrawerProps) {
967 - const canOperateTerminal = useEnvironmentScope(threadRef.environmentId, AuthTerminalOperateScope);
968 - const hasTerminalWriteAccess = useCallback(
969 - () => readEnvironmentScope(threadRef.environmentId, AuthTerminalOperateScope),
970 - [threadRef.environmentId],
971 - );
972 - const openTerminal = useAtomCommand(terminalEnvironment.open, "terminal open");
973 - const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write");
974 - const closeTerminalMutation = useAtomCommand(terminalEnvironment.close, "terminal close");
975 - const serverThread = useThreadShell(threadRef);
976 - const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
977 - const projectRef = serverThread
978 - ? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
979 - : draftThread
980 - ? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
981 - : null;
982 - const project = useProject(projectRef);
983 - const terminalUiState = useTerminalUiStateStore((state) =>
984 - selectThreadTerminalUiState(state.terminalUiStateByThreadKey, threadRef),
985 - );
986 - const visible = active && terminalUiState.terminalOpen;
987 - const knownTerminalSessions = useKnownTerminalSessions({
988 - environmentId: threadRef.environmentId,
989 - threadId,
990 - });
991 - const panelSurfaces = useRightPanelStore(
992 - (state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces,
993 - );
994 - const panelTerminalIds = useMemo(
995 - () =>
996 - new Set(
997 - panelSurfaces.flatMap((surface) =>
998 - surface.kind === "terminal" ? surface.terminalIds : [],
999 - ),
1000 - ),
1001 - [panelSurfaces],
1002 - );
1003 - const drawerTerminalSessions = useMemo(
1004 - () =>
1005 - knownTerminalSessions?.filter(
1006 - (session) => !panelTerminalIds.has(session.target.terminalId),
1007 - ) ?? [],
1008 - [knownTerminalSessions, panelTerminalIds],
1009 - );
1010 - const terminalLabelsById = useMemo(() => {
1011 - const next = new Map<string, string>();
1012 - for (const session of drawerTerminalSessions) {
1013 - next.set(
1014 - session.target.terminalId,
1015 - resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
1016 - );
1017 - }
1018 - return next;
1019 - }, [drawerTerminalSessions]);
1020 - const terminalLaunchLocationsById = useMemo(() => {
1021 - const next = new Map<
1022 - string,
1023 - {
1024 - readonly cwd: string;
1025 - readonly worktreePath: string | null;
1026 - readonly runtimeEnv: Record<string, string>;
1027 - }
1028 - >();
1029 - if (!project) {
1030 - return next;
1031 - }
1032 -
1033 - for (const session of drawerTerminalSessions) {
1034 - const summary = session.state.summary;
1035 - if (!summary) {
1036 - continue;
1037 - }
1038 - const worktreePathForLaunch =
1039 - launchContext !== null ? launchContext.worktreePath : summary.worktreePath;
1040 - next.set(session.target.terminalId, {
1041 - cwd: launchContext?.cwd ?? summary.cwd,
1042 - worktreePath: worktreePathForLaunch,
1043 - runtimeEnv: projectScriptRuntimeEnv({
1044 - project: { cwd: project.workspaceRoot },
1045 - worktreePath: worktreePathForLaunch,
1046 - }),
1047 - });
1048 - }
1049 -
1050 - return next;
1051 - }, [drawerTerminalSessions, launchContext, project]);
1052 - const serverOrderedTerminalIds = useMemo(
1053 - () => drawerTerminalSessions.map((session) => session.target.terminalId),
1054 - [drawerTerminalSessions],
1055 - );
1056 - // Every client-side id source participates in allocation: the server list
1057 - // lags fresh opens, and panel terminals are filtered out of the drawer's
1058 - // sessions — an id collision attaches two viewports to one PTY session.
1059 - const allocatableTerminalIds = useMemo(
1060 - () => [
1061 - ...new Set([
1062 - ...serverOrderedTerminalIds,
1063 - ...terminalUiState.terminalIds,
1064 - ...panelTerminalIds,
1065 - ]),
1066 - ],
1067 - [panelTerminalIds, serverOrderedTerminalIds, terminalUiState.terminalIds],
1068 - );
1069 - const allocateTerminalId = useCallback(
1070 - () =>
1071 - nextTerminalId(
1072 - allocatableTerminalIds,
1073 - knownTerminalSessions === null ||
1074 - !readEnvironmentScope(threadRef.environmentId, AuthTerminalReadScope)
1075 - ? randomUUID()
1076 - : undefined,
1077 - ),
1078 - [allocatableTerminalIds, knownTerminalSessions, threadRef.environmentId],
1079 - );
1080 - const storeSetTerminalHeight = useTerminalUiStateStore((state) => state.setTerminalHeight);
1081 - const storeSplitTerminal = useTerminalUiStateStore((state) => state.splitTerminal);
1082 - const storeSplitTerminalVertical = useTerminalUiStateStore(
1083 - (state) => state.splitTerminalVertical,
1084 - );
1085 - const storeNewTerminal = useTerminalUiStateStore((state) => state.newTerminal);
1086 - const storeSetActiveTerminal = useTerminalUiStateStore((state) => state.setActiveTerminal);
1087 - const storeCloseTerminal = useTerminalUiStateStore((state) => state.closeTerminal);
1088 - const reconcileTerminalIds = useTerminalUiStateStore((state) => state.reconcileTerminalIds);
1089 -
1090 - useEffect(() => {
1091 - if (terminalIdListsEqual(serverOrderedTerminalIds, terminalUiState.terminalIds)) {
1092 - return;
1093 - }
1094 - if (
1095 - serverTerminalIdsStrictSubsetOfClient(serverOrderedTerminalIds, terminalUiState.terminalIds)
1096 - ) {
1097 - return;
1098 - }
1099 - reconcileTerminalIds(threadRef, serverOrderedTerminalIds);
1100 - }, [reconcileTerminalIds, serverOrderedTerminalIds, terminalUiState.terminalIds, threadRef]);
1101 - const [localFocusRequestId, setLocalFocusRequestId] = useState(0);
1102 - const worktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
1103 - const effectiveWorktreePath = useMemo(() => {
1104 - if (launchContext !== null) {
1105 - return launchContext.worktreePath;
1106 - }
1107 - return worktreePath;
1108 - }, [launchContext, worktreePath]);
1109 - const cwd = useMemo(
1110 - () =>
1111 - launchContext?.cwd ??
1112 - (project
1113 - ? projectScriptCwd({
1114 - project: { cwd: project.workspaceRoot },
1115 - worktreePath: effectiveWorktreePath,
1116 - })
1117 - : null),
1118 - [effectiveWorktreePath, launchContext?.cwd, project],
1119 - );
1120 - const runtimeEnv = useMemo(
1121 - () =>
1122 - project
1123 - ? projectScriptRuntimeEnv({
1124 - project: { cwd: project.workspaceRoot },
1125 - worktreePath: effectiveWorktreePath,
1126 - })
1127 - : {},
1128 - [effectiveWorktreePath, project],
1129 - );
1130 -
1131 - const bumpFocusRequestId = useCallback(() => {
1132 - if (!visible) {
1133 - return;
1134 - }
1135 - setLocalFocusRequestId((value) => value + 1);
1136 - }, [visible]);
1137 -
1138 - const setTerminalHeight = useCallback(
1139 - (height: number) => {
1140 - storeSetTerminalHeight(threadRef, height);
1141 - },
1142 - [storeSetTerminalHeight, threadRef],
1143 - );
1144 -
1145 - const splitTerminal = useCallback(() => {
1146 - if (!hasTerminalWriteAccess() || !cwd) {
1147 - return;
1148 - }
1149 - const terminalId = allocateTerminalId();
1150 - storeSplitTerminal(threadRef, terminalId);
1151 - bumpFocusRequestId();
1152 - void openTerminal({
1153 - environmentId: threadRef.environmentId,
1154 - input: {
1155 - threadId,
1156 - terminalId,
1157 - cwd,
1158 - ...(effectiveWorktreePath != null ? { worktreePath: effectiveWorktreePath } : {}),
1159 - env: runtimeEnv,
1160 - },
1161 - });
1162 - }, [
1163 - allocateTerminalId,
1164 - bumpFocusRequestId,
1165 - cwd,
1166 - effectiveWorktreePath,
1167 - runtimeEnv,
1168 - storeSplitTerminal,
1169 - threadId,
1170 - threadRef,
1171 - openTerminal,
1172 - hasTerminalWriteAccess,
1173 - ]);
1174 - const splitTerminalVertical = useCallback(() => {
1175 - if (!hasTerminalWriteAccess() || !cwd) {
1176 - return;
1177 - }
1178 - const terminalId = allocateTerminalId();
1179 - storeSplitTerminalVertical(threadRef, terminalId);
1180 - bumpFocusRequestId();
1181 - void openTerminal({
1182 - environmentId: threadRef.environmentId,
1183 - input: {
1184 - threadId,
1185 - terminalId,
1186 - cwd,
1187 - ...(effectiveWorktreePath != null ? { worktreePath: effectiveWorktreePath } : {}),
1188 - env: runtimeEnv,
1189 - },
1190 - });
1191 - }, [
1192 - allocateTerminalId,
1193 - bumpFocusRequestId,
1194 - cwd,
1195 - effectiveWorktreePath,
1196 - openTerminal,
1197 - hasTerminalWriteAccess,
1198 - runtimeEnv,
1199 - storeSplitTerminalVertical,
1200 - threadId,
1201 - threadRef,
1202 - ]);
1203 -
1204 - const createNewTerminal = useCallback(() => {
1205 - if (!hasTerminalWriteAccess() || !cwd) {
1206 - return;
1207 - }
1208 - const terminalId = allocateTerminalId();
1209 - storeNewTerminal(threadRef, terminalId);
1210 - bumpFocusRequestId();
1211 - void openTerminal({
1212 - environmentId: threadRef.environmentId,
1213 - input: {
1214 - threadId,
1215 - terminalId,
1216 - cwd,
1217 - ...(effectiveWorktreePath != null ? { worktreePath: effectiveWorktreePath } : {}),
1218 - env: runtimeEnv,
1219 - },
1220 - });
1221 - }, [
1222 - bumpFocusRequestId,
1223 - cwd,
1224 - effectiveWorktreePath,
1225 - allocateTerminalId,
1226 - runtimeEnv,
1227 - storeNewTerminal,
1228 - threadId,
1229 - threadRef,
1230 - openTerminal,
1231 - hasTerminalWriteAccess,
1232 - ]);
1233 -
1234 - const activateTerminal = useCallback(
1235 - (terminalId: string) => {
1236 - storeSetActiveTerminal(threadRef, terminalId);
1237 - bumpFocusRequestId();
1238 - },
1239 - [bumpFocusRequestId, storeSetActiveTerminal, threadRef],
1240 - );
1241 -
1242 - const closeTerminal = useCallback(
1243 - (terminalId: string) => {
1244 - if (!hasTerminalWriteAccess()) return;
1245 - const fallbackExitWrite = () =>
1246 - writeTerminal({
1247 - environmentId: threadRef.environmentId,
1248 - input: { threadId, terminalId, data: "exit\n" },
1249 - });
1250 -
1251 - void (async () => {
1252 - const closeResult = await closeTerminalMutation({
1253 - environmentId: threadRef.environmentId,
1254 - input: {
1255 - threadId,
1256 - terminalId,
1257 - deleteHistory: true,
1258 - },
1259 - });
1260 - if (
1261 - closeResult._tag === "Failure" &&
1262 - !isAtomCommandInterrupted(closeResult) &&
1263 - hasTerminalWriteAccess()
1264 - ) {
1265 - await fallbackExitWrite();
1266 - }
1267 - })();
1268 -
1269 - storeCloseTerminal(threadRef, terminalId);
1270 - bumpFocusRequestId();
1271 - },
1272 - [
1273 - bumpFocusRequestId,
1274 - storeCloseTerminal,
1275 - threadId,
1276 - threadRef,
1277 - closeTerminalMutation,
1278 - hasTerminalWriteAccess,
1279 - writeTerminal,
1280 - ],
1281 - );
1282 -
1283 - const handleAddTerminalContext = useCallback(
1284 - (selection: TerminalContextSelection) => {
1285 - if (!visible) {
1286 - return;
1287 - }
1288 - onAddTerminalContext(selection);
1289 - },
1290 - [onAddTerminalContext, visible],
1291 - );
1292 -
1293 - if (!project || (!terminalUiState.terminalOpen && !active) || !cwd) {
1294 - return null;
1295 - }
1296 -
1297 - return (
1298 - <div
1299 - className={cn(
1300 - "grid shrink-0 overflow-clip",
1301 - active ? (visible ? "grid-rows-[1fr]" : "grid-rows-[0fr]") : "hidden",
1302 - active &&
1303 - "[[data-panel-animations=true]_&]:transition-[grid-template-rows] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out",
1304 - active && visible && "[[data-panel-animations=true]_&]:starting:grid-rows-[0fr]!",
1305 - )}
1306 - >
1307 - <div className="min-h-0 overflow-clip">
1308 - <ThreadTerminalDrawer
1309 - threadRef={threadRef}
1310 - threadId={threadId}
1311 - cwd={cwd}
1312 - worktreePath={effectiveWorktreePath}
1313 - runtimeEnv={runtimeEnv}
1314 - visible={visible}
1315 - height={terminalUiState.terminalHeight}
1316 - terminalIds={canOperateTerminal ? terminalUiState.terminalIds : serverOrderedTerminalIds}
1317 - activeTerminalId={terminalUiState.activeTerminalId}
1318 - terminalGroups={terminalUiState.terminalGroups}
1319 - activeTerminalGroupId={terminalUiState.activeTerminalGroupId}
1320 - focusRequestId={focusRequestId + localFocusRequestId + (visible ? 1 : 0)}
1321 - onSplitTerminal={splitTerminal}
1322 - onSplitTerminalVertical={splitTerminalVertical}
1323 - onNewTerminal={createNewTerminal}
1324 - splitShortcutLabel={visible ? splitShortcutLabel : undefined}
1325 - splitVerticalShortcutLabel={visible ? splitVerticalShortcutLabel : undefined}
1326 - newShortcutLabel={visible ? newShortcutLabel : undefined}
1327 - closeShortcutLabel={visible ? closeShortcutLabel : undefined}
1328 - keybindings={keybindings}
1329 - onActiveTerminalChange={activateTerminal}
1330 - onCloseTerminal={closeTerminal}
1331 - onHeightChange={setTerminalHeight}
1332 - onAddTerminalContext={handleAddTerminalContext}
1333 - terminalLabelsById={terminalLabelsById}
1334 - terminalLaunchLocationsById={terminalLaunchLocationsById}
1335 - />
1336 - </div>
1337 - </div>
1338 - );
1339 -});
1340 -
1341 -interface PersistentThreadTerminalPanelProps {
1342 - visible: boolean;
1343 - threadRef: ScopedThreadRef;
1344 - surface: Extract<RightPanelSurface, { kind: "terminal" }>;
1345 - launchContext: PersistentTerminalLaunchContext | null;
1346 - focusRequestId: number;
1347 - keybindings: ResolvedKeybindingsConfig;
1348 - onAddTerminalContext: (selection: TerminalContextSelection) => void;
1349 - onSplitTerminal: () => void;
1350 - onSplitTerminalVertical: () => void;
1351 - onNewTerminal: () => void;
1352 - onActiveTerminalChange: (terminalId: string) => void;
1353 - onCloseTerminal: (terminalId: string) => void;
1354 - splitShortcutLabel?: string | undefined;
1355 - splitVerticalShortcutLabel?: string | undefined;
1356 - newShortcutLabel?: string | undefined;
1357 - closeShortcutLabel?: string | undefined;
1358 -}
1359 -
1360 -const PersistentThreadTerminalPanel = memo(function PersistentThreadTerminalPanel({
1361 - visible,
1362 - threadRef,
1363 - surface,
1364 - launchContext,
1365 - focusRequestId,
1366 - keybindings,
1367 - onAddTerminalContext,
1368 - onSplitTerminal,
1369 - onSplitTerminalVertical,
1370 - onNewTerminal,
1371 - onActiveTerminalChange,
1372 - onCloseTerminal,
1373 - splitShortcutLabel,
1374 - splitVerticalShortcutLabel,
1375 - newShortcutLabel,
1376 - closeShortcutLabel,
1377 -}: PersistentThreadTerminalPanelProps) {
1378 - const serverThread = useThreadShell(threadRef);
1379 - const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
1380 - const projectRef = serverThread
1381 - ? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
1382 - : draftThread
1383 - ? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
1384 - : null;
1385 - const project = useProject(projectRef);
1386 - const knownTerminalSessions = useKnownTerminalSessions({
1387 - environmentId: threadRef.environmentId,
1388 - threadId: threadRef.threadId,
1389 - });
1390 - const threadWorktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
1391 - const activeSummary =
1392 - knownTerminalSessions?.find((session) => session.target.terminalId === surface.activeTerminalId)
1393 - ?.state.summary ?? null;
1394 - const worktreePath =
1395 - launchContext?.worktreePath ?? activeSummary?.worktreePath ?? threadWorktreePath;
1396 - const cwd = useMemo(
1397 - () =>
1398 - launchContext?.cwd ??
1399 - activeSummary?.cwd ??
1400 - (project
1401 - ? projectScriptCwd({
1402 - project: { cwd: project.workspaceRoot },
1403 - worktreePath,
1404 - })
1405 - : null),
1406 - [activeSummary?.cwd, launchContext?.cwd, project, worktreePath],
1407 - );
1408 - const runtimeEnv = useMemo(
1409 - () =>
1410 - project
1411 - ? projectScriptRuntimeEnv({
1412 - project: { cwd: project.workspaceRoot },
1413 - worktreePath,
1414 - })
1415 - : {},
1416 - [project, worktreePath],
1417 - );
1418 - const terminalLabelsById = useMemo(() => {
1419 - const labels = new Map<string, string>();
1420 - for (const terminalId of surface.terminalIds) {
1421 - const summary =
1422 - knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
1423 - .summary ?? null;
1424 - labels.set(terminalId, resolveTerminalSessionLabel(terminalId, summary));
1425 - }
1426 - return labels;
1427 - }, [knownTerminalSessions, surface.terminalIds]);
1428 - const terminalLaunchLocationsById = useMemo(() => {
1429 - const locations = new Map<
1430 - string,
1431 - {
1432 - readonly cwd: string;
1433 - readonly worktreePath: string | null;
1434 - readonly runtimeEnv: Record<string, string>;
1435 - }
1436 - >();
1437 - for (const terminalId of surface.terminalIds) {
1438 - const summary =
1439 - knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
1440 - .summary ?? null;
1441 - const terminalWorktreePath =
1442 - launchContext?.worktreePath ?? summary?.worktreePath ?? threadWorktreePath;
1443 - const terminalCwd =
1444 - launchContext?.cwd ??
1445 - summary?.cwd ??
1446 - (project
1447 - ? projectScriptCwd({
1448 - project: { cwd: project.workspaceRoot },
1449 - worktreePath: terminalWorktreePath,
1450 - })
1451 - : null);
1452 - if (!terminalCwd || !project) continue;
1453 - locations.set(terminalId, {
1454 - cwd: terminalCwd,
1455 - worktreePath: terminalWorktreePath,
1456 - runtimeEnv: projectScriptRuntimeEnv({
1457 - project: { cwd: project.workspaceRoot },
1458 - worktreePath: terminalWorktreePath,
1459 - }),
1460 - });
1461 - }
1462 - return locations;
1463 - }, [
1464 - knownTerminalSessions,
1465 - launchContext?.cwd,
1466 - launchContext?.worktreePath,
1467 - project,
1468 - surface.terminalIds,
1469 - threadWorktreePath,
1470 - ]);
1471 -
1472 - if (!project || !cwd) return null;
1473 -
1474 - return (
1475 - <ThreadTerminalDrawer
1476 - mode="panel"
1477 - visible={visible}
1478 - threadRef={threadRef}
1479 - threadId={threadRef.threadId}
1480 - cwd={cwd}
1481 - worktreePath={worktreePath}
1482 - runtimeEnv={runtimeEnv}
1483 - height={0}
1484 - terminalIds={surface.terminalIds}
1485 - activeTerminalId={surface.activeTerminalId}
1486 - terminalGroups={[
1487 - {
1488 - id: surface.id,
1489 - terminalIds: surface.terminalIds,
1490 - ...(surface.splitDirection === "vertical" ? { splitDirection: "vertical" as const } : {}),
1491 - },
1492 - ]}
1493 - activeTerminalGroupId={surface.id}
1494 - focusRequestId={focusRequestId}
1495 - onSplitTerminal={onSplitTerminal}
1496 - onSplitTerminalVertical={onSplitTerminalVertical}
1497 - onNewTerminal={onNewTerminal}
1498 - splitShortcutLabel={splitShortcutLabel}
1499 - splitVerticalShortcutLabel={splitVerticalShortcutLabel}
1500 - newShortcutLabel={newShortcutLabel}
1501 - closeShortcutLabel={closeShortcutLabel}
1502 - onActiveTerminalChange={onActiveTerminalChange}
1503 - onCloseTerminal={onCloseTerminal}
1504 - onHeightChange={() => undefined}
1505 - onAddTerminalContext={onAddTerminalContext}
1506 - terminalLabelsById={terminalLabelsById}
1507 - terminalLaunchLocationsById={terminalLaunchLocationsById}
1508 - keybindings={keybindings}
1509 - />
1510 - );
1511 -});
1512 -
902 +// Errors surface through two maps (draft-keyed and thread-keyed) whose entries
903 +// can race around promotion, so each write carries its time to let the latest
904 +// one win when they collide.
905 type LocalThreadErrorEntry = {
906 readonly message: string | null;
907 readonly at: number;
apps/web/src/components/PersistentThreadTerminals.tsx new
+647
@@ -0,0 +1,647 @@
1 +import {
2 + AuthTerminalOperateScope,
3 + AuthTerminalReadScope,
4 + type EnvironmentId,
5 + type ResolvedKeybindingsConfig,
6 + type ScopedThreadRef,
7 + type ThreadId,
8 +} from "@t3tools/contracts";
9 +import { scopeProjectRef } from "@t3tools/client-runtime/environment";
10 +import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime";
11 +import { projectScriptCwd, projectScriptRuntimeEnv } from "@t3tools/shared/projectScripts";
12 +import { nextTerminalId, resolveTerminalSessionLabel } from "@t3tools/shared/terminalLabels";
13 +import { memo, useCallback, useEffect, useMemo, useState } from "react";
14 +import { useComposerDraftStore } from "../composerDraftStore";
15 +import type { TerminalContextSelection } from "../lib/terminalContext";
16 +import { cn, randomUUID } from "~/lib/utils";
17 +import {
18 + selectThreadRightPanelState,
19 + useRightPanelStore,
20 + type RightPanelSurface,
21 +} from "../rightPanelStore";
22 +import { useProject, useThreadShell } from "../state/entities";
23 +import { readEnvironmentScope, useEnvironmentScope } from "../state/session";
24 +import { terminalEnvironment } from "../state/terminal";
25 +import { useKnownTerminalSessions } from "../state/terminalSessions";
26 +import { useAtomCommand } from "../state/use-atom-command";
27 +import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
28 +import ThreadTerminalDrawer from "./ThreadTerminalDrawer";
29 +
30 +/** Where a drawer or panel opens new terminals when a script run overrides the thread's own. */
31 +export type PersistentTerminalLaunchContext = {
32 + readonly cwd: string;
33 + readonly worktreePath: string | null;
34 +};
35 +
36 +/** Same terminal ids (order ignored) — avoids reconcile when only server session ordering differs. */
37 +function terminalIdListsEqual(left: readonly string[], right: readonly string[]): boolean {
38 + if (left.length !== right.length) {
39 + return false;
40 + }
41 + if (left.length === 0) {
42 + return true;
43 + }
44 + const sortedLeft = left.toSorted((a, b) => a.localeCompare(b));
45 + const sortedRight = right.toSorted((a, b) => a.localeCompare(b));
46 + for (let index = 0; index < sortedLeft.length; index += 1) {
47 + if (sortedLeft[index] !== sortedRight[index]) {
48 + return false;
49 + }
50 + }
51 + return true;
52 +}
53 +
54 +/**
55 + * Server knows about fewer sessions than the client, but every server id still exists locally.
56 + * Typical right after `terminal.open`: known-session list lags; reconciling would drop the new id
57 + * and later re-add it as a separate group (no split layout).
58 + */
59 +function serverTerminalIdsStrictSubsetOfClient(
60 + serverIds: readonly string[],
61 + clientIds: readonly string[],
62 +): boolean {
63 + if (serverIds.length >= clientIds.length || clientIds.length === 0) {
64 + return false;
65 + }
66 + const clientSet = new Set(clientIds);
67 + for (const id of serverIds) {
68 + if (!clientSet.has(id)) {
69 + return false;
70 + }
71 + }
72 + return true;
73 +}
74 +
75 +interface PersistentThreadTerminalDrawerProps {
76 + threadRef: { environmentId: EnvironmentId; threadId: ThreadId };
77 + threadId: ThreadId;
78 + active: boolean;
79 + launchContext: PersistentTerminalLaunchContext | null;
80 + focusRequestId: number;
81 + splitShortcutLabel: string | undefined;
82 + splitVerticalShortcutLabel: string | undefined;
83 + newShortcutLabel: string | undefined;
84 + closeShortcutLabel: string | undefined;
85 + keybindings: ResolvedKeybindingsConfig;
86 + onAddTerminalContext: (selection: TerminalContextSelection) => void;
87 +}
88 +
89 +export const PersistentThreadTerminalDrawer = memo(function PersistentThreadTerminalDrawer({
90 + threadRef,
91 + threadId,
92 + active,
93 + launchContext,
94 + focusRequestId,
95 + splitShortcutLabel,
96 + splitVerticalShortcutLabel,
97 + newShortcutLabel,
98 + closeShortcutLabel,
99 + keybindings,
100 + onAddTerminalContext,
101 +}: PersistentThreadTerminalDrawerProps) {
102 + const canOperateTerminal = useEnvironmentScope(threadRef.environmentId, AuthTerminalOperateScope);
103 + const hasTerminalWriteAccess = useCallback(
104 + () => readEnvironmentScope(threadRef.environmentId, AuthTerminalOperateScope),
105 + [threadRef.environmentId],
106 + );
107 + const openTerminal = useAtomCommand(terminalEnvironment.open, "terminal open");
108 + const writeTerminal = useAtomCommand(terminalEnvironment.write, "terminal write");
109 + const closeTerminalMutation = useAtomCommand(terminalEnvironment.close, "terminal close");
110 + const serverThread = useThreadShell(threadRef);
111 + const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
112 + const projectRef = serverThread
113 + ? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
114 + : draftThread
115 + ? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
116 + : null;
117 + const project = useProject(projectRef);
118 + const terminalUiState = useTerminalUiStateStore((state) =>
119 + selectThreadTerminalUiState(state.terminalUiStateByThreadKey, threadRef),
120 + );
121 + const visible = active && terminalUiState.terminalOpen;
122 + const knownTerminalSessions = useKnownTerminalSessions({
123 + environmentId: threadRef.environmentId,
124 + threadId,
125 + });
126 + const panelSurfaces = useRightPanelStore(
127 + (state) => selectThreadRightPanelState(state.byThreadKey, threadRef).surfaces,
128 + );
129 + const panelTerminalIds = useMemo(
130 + () =>
131 + new Set(
132 + panelSurfaces.flatMap((surface) =>
133 + surface.kind === "terminal" ? surface.terminalIds : [],
134 + ),
135 + ),
136 + [panelSurfaces],
137 + );
138 + const drawerTerminalSessions = useMemo(
139 + () =>
140 + knownTerminalSessions?.filter(
141 + (session) => !panelTerminalIds.has(session.target.terminalId),
142 + ) ?? [],
143 + [knownTerminalSessions, panelTerminalIds],
144 + );
145 + const terminalLabelsById = useMemo(() => {
146 + const next = new Map<string, string>();
147 + for (const session of drawerTerminalSessions) {
148 + next.set(
149 + session.target.terminalId,
150 + resolveTerminalSessionLabel(session.target.terminalId, session.state.summary),
151 + );
152 + }
153 + return next;
154 + }, [drawerTerminalSessions]);
155 + const terminalLaunchLocationsById = useMemo(() => {
156 + const next = new Map<
157 + string,
158 + {
159 + readonly cwd: string;
160 + readonly worktreePath: string | null;
161 + readonly runtimeEnv: Record<string, string>;
162 + }
163 + >();
164 + if (!project) {
165 + return next;
166 + }
167 +
168 + for (const session of drawerTerminalSessions) {
169 + const summary = session.state.summary;
170 + if (!summary) {
171 + continue;
172 + }
173 + const worktreePathForLaunch =
174 + launchContext !== null ? launchContext.worktreePath : summary.worktreePath;
175 + next.set(session.target.terminalId, {
176 + cwd: launchContext?.cwd ?? summary.cwd,
177 + worktreePath: worktreePathForLaunch,
178 + runtimeEnv: projectScriptRuntimeEnv({
179 + project: { cwd: project.workspaceRoot },
180 + worktreePath: worktreePathForLaunch,
181 + }),
182 + });
183 + }
184 +
185 + return next;
186 + }, [drawerTerminalSessions, launchContext, project]);
187 + const serverOrderedTerminalIds = useMemo(
188 + () => drawerTerminalSessions.map((session) => session.target.terminalId),
189 + [drawerTerminalSessions],
190 + );
191 + // Every client-side id source participates in allocation: the server list
192 + // lags fresh opens, and panel terminals are filtered out of the drawer's
193 + // sessions — an id collision attaches two viewports to one PTY session.
194 + const allocatableTerminalIds = useMemo(
195 + () => [
196 + ...new Set([
197 + ...serverOrderedTerminalIds,
198 + ...terminalUiState.terminalIds,
199 + ...panelTerminalIds,
200 + ]),
201 + ],
202 + [panelTerminalIds, serverOrderedTerminalIds, terminalUiState.terminalIds],
203 + );
204 + const allocateTerminalId = useCallback(
205 + () =>
206 + nextTerminalId(
207 + allocatableTerminalIds,
208 + knownTerminalSessions === null ||
209 + !readEnvironmentScope(threadRef.environmentId, AuthTerminalReadScope)
210 + ? randomUUID()
211 + : undefined,
212 + ),
213 + [allocatableTerminalIds, knownTerminalSessions, threadRef.environmentId],
214 + );
215 + const storeSetTerminalHeight = useTerminalUiStateStore((state) => state.setTerminalHeight);
216 + const storeSplitTerminal = useTerminalUiStateStore((state) => state.splitTerminal);
217 + const storeSplitTerminalVertical = useTerminalUiStateStore(
218 + (state) => state.splitTerminalVertical,
219 + );
220 + const storeNewTerminal = useTerminalUiStateStore((state) => state.newTerminal);
221 + const storeSetActiveTerminal = useTerminalUiStateStore((state) => state.setActiveTerminal);
222 + const storeCloseTerminal = useTerminalUiStateStore((state) => state.closeTerminal);
223 + const reconcileTerminalIds = useTerminalUiStateStore((state) => state.reconcileTerminalIds);
224 +
225 + useEffect(() => {
226 + if (terminalIdListsEqual(serverOrderedTerminalIds, terminalUiState.terminalIds)) {
227 + return;
228 + }
229 + if (
230 + serverTerminalIdsStrictSubsetOfClient(serverOrderedTerminalIds, terminalUiState.terminalIds)
231 + ) {
232 + return;
233 + }
234 + reconcileTerminalIds(threadRef, serverOrderedTerminalIds);
235 + }, [reconcileTerminalIds, serverOrderedTerminalIds, terminalUiState.terminalIds, threadRef]);
236 + const [localFocusRequestId, setLocalFocusRequestId] = useState(0);
237 + const worktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
238 + const effectiveWorktreePath = useMemo(() => {
239 + if (launchContext !== null) {
240 + return launchContext.worktreePath;
241 + }
242 + return worktreePath;
243 + }, [launchContext, worktreePath]);
244 + const cwd = useMemo(
245 + () =>
246 + launchContext?.cwd ??
247 + (project
248 + ? projectScriptCwd({
249 + project: { cwd: project.workspaceRoot },
250 + worktreePath: effectiveWorktreePath,
251 + })
252 + : null),
253 + [effectiveWorktreePath, launchContext?.cwd, project],
254 + );
255 + const runtimeEnv = useMemo(
256 + () =>
257 + project
258 + ? projectScriptRuntimeEnv({
259 + project: { cwd: project.workspaceRoot },
260 + worktreePath: effectiveWorktreePath,
261 + })
262 + : {},
263 + [effectiveWorktreePath, project],
264 + );
265 +
266 + const bumpFocusRequestId = useCallback(() => {
267 + if (!visible) {
268 + return;
269 + }
270 + setLocalFocusRequestId((value) => value + 1);
271 + }, [visible]);
272 +
273 + const setTerminalHeight = useCallback(
274 + (height: number) => {
275 + storeSetTerminalHeight(threadRef, height);
276 + },
277 + [storeSetTerminalHeight, threadRef],
278 + );
279 +
280 + const splitTerminal = useCallback(() => {
281 + if (!hasTerminalWriteAccess() || !cwd) {
282 + return;
283 + }
284 + const terminalId = allocateTerminalId();
285 + storeSplitTerminal(threadRef, terminalId);
286 + bumpFocusRequestId();
287 + void openTerminal({
288 + environmentId: threadRef.environmentId,
289 + input: {
290 + threadId,
291 + terminalId,
292 + cwd,
293 + ...(effectiveWorktreePath != null ? { worktreePath: effectiveWorktreePath } : {}),
294 + env: runtimeEnv,
295 + },
296 + });
297 + }, [
298 + allocateTerminalId,
299 + bumpFocusRequestId,
300 + cwd,
301 + effectiveWorktreePath,
302 + runtimeEnv,
303 + storeSplitTerminal,
304 + threadId,
305 + threadRef,
306 + openTerminal,
307 + hasTerminalWriteAccess,
308 + ]);
309 + const splitTerminalVertical = useCallback(() => {
310 + if (!hasTerminalWriteAccess() || !cwd) {
311 + return;
312 + }
313 + const terminalId = allocateTerminalId();
314 + storeSplitTerminalVertical(threadRef, terminalId);
315 + bumpFocusRequestId();
316 + void openTerminal({
317 + environmentId: threadRef.environmentId,
318 + input: {
319 + threadId,
320 + terminalId,
321 + cwd,
322 + ...(effectiveWorktreePath != null ? { worktreePath: effectiveWorktreePath } : {}),
323 + env: runtimeEnv,
324 + },
325 + });
326 + }, [
327 + allocateTerminalId,
328 + bumpFocusRequestId,
329 + cwd,
330 + effectiveWorktreePath,
331 + openTerminal,
332 + hasTerminalWriteAccess,
333 + runtimeEnv,
334 + storeSplitTerminalVertical,
335 + threadId,
336 + threadRef,
337 + ]);
338 +
339 + const createNewTerminal = useCallback(() => {
340 + if (!hasTerminalWriteAccess() || !cwd) {
341 + return;
342 + }
343 + const terminalId = allocateTerminalId();
344 + storeNewTerminal(threadRef, terminalId);
345 + bumpFocusRequestId();
346 + void openTerminal({
347 + environmentId: threadRef.environmentId,
348 + input: {
349 + threadId,
350 + terminalId,
351 + cwd,
352 + ...(effectiveWorktreePath != null ? { worktreePath: effectiveWorktreePath } : {}),
353 + env: runtimeEnv,
354 + },
355 + });
356 + }, [
357 + bumpFocusRequestId,
358 + cwd,
359 + effectiveWorktreePath,
360 + allocateTerminalId,
361 + runtimeEnv,
362 + storeNewTerminal,
363 + threadId,
364 + threadRef,
365 + openTerminal,
366 + hasTerminalWriteAccess,
367 + ]);
368 +
369 + const activateTerminal = useCallback(
370 + (terminalId: string) => {
371 + storeSetActiveTerminal(threadRef, terminalId);
372 + bumpFocusRequestId();
373 + },
374 + [bumpFocusRequestId, storeSetActiveTerminal, threadRef],
375 + );
376 +
377 + const closeTerminal = useCallback(
378 + (terminalId: string) => {
379 + if (!hasTerminalWriteAccess()) return;
380 + const fallbackExitWrite = () =>
381 + writeTerminal({
382 + environmentId: threadRef.environmentId,
383 + input: { threadId, terminalId, data: "exit\n" },
384 + });
385 +
386 + void (async () => {
387 + const closeResult = await closeTerminalMutation({
388 + environmentId: threadRef.environmentId,
389 + input: {
390 + threadId,
391 + terminalId,
392 + deleteHistory: true,
393 + },
394 + });
395 + if (
396 + closeResult._tag === "Failure" &&
397 + !isAtomCommandInterrupted(closeResult) &&
398 + hasTerminalWriteAccess()
399 + ) {
400 + await fallbackExitWrite();
401 + }
402 + })();
403 +
404 + storeCloseTerminal(threadRef, terminalId);
405 + bumpFocusRequestId();
406 + },
407 + [
408 + bumpFocusRequestId,
409 + storeCloseTerminal,
410 + threadId,
411 + threadRef,
412 + closeTerminalMutation,
413 + hasTerminalWriteAccess,
414 + writeTerminal,
415 + ],
416 + );
417 +
418 + const handleAddTerminalContext = useCallback(
419 + (selection: TerminalContextSelection) => {
420 + if (!visible) {
421 + return;
422 + }
423 + onAddTerminalContext(selection);
424 + },
425 + [onAddTerminalContext, visible],
426 + );
427 +
428 + if (!project || (!terminalUiState.terminalOpen && !active) || !cwd) {
429 + return null;
430 + }
431 +
432 + return (
433 + <div
434 + className={cn(
435 + "grid shrink-0 overflow-clip",
436 + active ? (visible ? "grid-rows-[1fr]" : "grid-rows-[0fr]") : "hidden",
437 + active &&
438 + "[[data-panel-animations=true]_&]:transition-[grid-template-rows] [[data-panel-animations=true]_&]:duration-(--panel-animation-duration) [[data-panel-animations=true]_&]:ease-out",
439 + active && visible && "[[data-panel-animations=true]_&]:starting:grid-rows-[0fr]!",
440 + )}
441 + >
442 + <div className="min-h-0 overflow-clip">
443 + <ThreadTerminalDrawer
444 + threadRef={threadRef}
445 + threadId={threadId}
446 + cwd={cwd}
447 + worktreePath={effectiveWorktreePath}
448 + runtimeEnv={runtimeEnv}
449 + visible={visible}
450 + height={terminalUiState.terminalHeight}
451 + // Known-session order is MRU and changes on focus; persisted store order keeps sidebar labels stable.
452 + terminalIds={canOperateTerminal ? terminalUiState.terminalIds : serverOrderedTerminalIds}
453 + activeTerminalId={terminalUiState.activeTerminalId}
454 + terminalGroups={terminalUiState.terminalGroups}
455 + activeTerminalGroupId={terminalUiState.activeTerminalGroupId}
456 + focusRequestId={focusRequestId + localFocusRequestId + (visible ? 1 : 0)}
457 + onSplitTerminal={splitTerminal}
458 + onSplitTerminalVertical={splitTerminalVertical}
459 + onNewTerminal={createNewTerminal}
460 + splitShortcutLabel={visible ? splitShortcutLabel : undefined}
461 + splitVerticalShortcutLabel={visible ? splitVerticalShortcutLabel : undefined}
462 + newShortcutLabel={visible ? newShortcutLabel : undefined}
463 + closeShortcutLabel={visible ? closeShortcutLabel : undefined}
464 + keybindings={keybindings}
465 + onActiveTerminalChange={activateTerminal}
466 + onCloseTerminal={closeTerminal}
467 + onHeightChange={setTerminalHeight}
468 + onAddTerminalContext={handleAddTerminalContext}
469 + terminalLabelsById={terminalLabelsById}
470 + terminalLaunchLocationsById={terminalLaunchLocationsById}
471 + />
472 + </div>
473 + </div>
474 + );
475 +});
476 +
477 +interface PersistentThreadTerminalPanelProps {
478 + visible: boolean;
479 + threadRef: ScopedThreadRef;
480 + surface: Extract<RightPanelSurface, { kind: "terminal" }>;
481 + launchContext: PersistentTerminalLaunchContext | null;
482 + focusRequestId: number;
483 + keybindings: ResolvedKeybindingsConfig;
484 + onAddTerminalContext: (selection: TerminalContextSelection) => void;
485 + onSplitTerminal: () => void;
486 + onSplitTerminalVertical: () => void;
487 + onNewTerminal: () => void;
488 + onActiveTerminalChange: (terminalId: string) => void;
489 + onCloseTerminal: (terminalId: string) => void;
490 + splitShortcutLabel?: string | undefined;
491 + splitVerticalShortcutLabel?: string | undefined;
492 + newShortcutLabel?: string | undefined;
493 + closeShortcutLabel?: string | undefined;
494 +}
495 +
496 +export const PersistentThreadTerminalPanel = memo(function PersistentThreadTerminalPanel({
497 + visible,
498 + threadRef,
499 + surface,
500 + launchContext,
501 + focusRequestId,
502 + keybindings,
503 + onAddTerminalContext,
504 + onSplitTerminal,
505 + onSplitTerminalVertical,
506 + onNewTerminal,
507 + onActiveTerminalChange,
508 + onCloseTerminal,
509 + splitShortcutLabel,
510 + splitVerticalShortcutLabel,
511 + newShortcutLabel,
512 + closeShortcutLabel,
513 +}: PersistentThreadTerminalPanelProps) {
514 + const serverThread = useThreadShell(threadRef);
515 + const draftThread = useComposerDraftStore((store) => store.getDraftThreadByRef(threadRef));
516 + const projectRef = serverThread
517 + ? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
518 + : draftThread
519 + ? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
520 + : null;
521 + const project = useProject(projectRef);
522 + const knownTerminalSessions = useKnownTerminalSessions({
523 + environmentId: threadRef.environmentId,
524 + threadId: threadRef.threadId,
525 + });
526 + const threadWorktreePath = serverThread?.worktreePath ?? draftThread?.worktreePath ?? null;
527 + const activeSummary =
528 + knownTerminalSessions?.find((session) => session.target.terminalId === surface.activeTerminalId)
529 + ?.state.summary ?? null;
530 + const worktreePath =
531 + launchContext?.worktreePath ?? activeSummary?.worktreePath ?? threadWorktreePath;
532 + const cwd = useMemo(
533 + () =>
534 + launchContext?.cwd ??
535 + activeSummary?.cwd ??
536 + (project
537 + ? projectScriptCwd({
538 + project: { cwd: project.workspaceRoot },
539 + worktreePath,
540 + })
541 + : null),
542 + [activeSummary?.cwd, launchContext?.cwd, project, worktreePath],
543 + );
544 + const runtimeEnv = useMemo(
545 + () =>
546 + project
547 + ? projectScriptRuntimeEnv({
548 + project: { cwd: project.workspaceRoot },
549 + worktreePath,
550 + })
551 + : {},
552 + [project, worktreePath],
553 + );
554 + const terminalLabelsById = useMemo(() => {
555 + const labels = new Map<string, string>();
556 + for (const terminalId of surface.terminalIds) {
557 + const summary =
558 + knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
559 + .summary ?? null;
560 + labels.set(terminalId, resolveTerminalSessionLabel(terminalId, summary));
561 + }
562 + return labels;
563 + }, [knownTerminalSessions, surface.terminalIds]);
564 + const terminalLaunchLocationsById = useMemo(() => {
565 + const locations = new Map<
566 + string,
567 + {
568 + readonly cwd: string;
569 + readonly worktreePath: string | null;
570 + readonly runtimeEnv: Record<string, string>;
571 + }
572 + >();
573 + for (const terminalId of surface.terminalIds) {
574 + const summary =
575 + knownTerminalSessions?.find((session) => session.target.terminalId === terminalId)?.state
576 + .summary ?? null;
577 + const terminalWorktreePath =
578 + launchContext?.worktreePath ?? summary?.worktreePath ?? threadWorktreePath;
579 + const terminalCwd =
580 + launchContext?.cwd ??
581 + summary?.cwd ??
582 + (project
583 + ? projectScriptCwd({
584 + project: { cwd: project.workspaceRoot },
585 + worktreePath: terminalWorktreePath,
586 + })
587 + : null);
588 + if (!terminalCwd || !project) continue;
589 + locations.set(terminalId, {
590 + cwd: terminalCwd,
591 + worktreePath: terminalWorktreePath,
592 + runtimeEnv: projectScriptRuntimeEnv({
593 + project: { cwd: project.workspaceRoot },
594 + worktreePath: terminalWorktreePath,
595 + }),
596 + });
597 + }
598 + return locations;
599 + }, [
600 + knownTerminalSessions,
601 + launchContext?.cwd,
602 + launchContext?.worktreePath,
603 + project,
604 + surface.terminalIds,
605 + threadWorktreePath,
606 + ]);
607 +
608 + if (!project || !cwd) return null;
609 +
610 + return (
611 + <ThreadTerminalDrawer
612 + mode="panel"
613 + visible={visible}
614 + threadRef={threadRef}
615 + threadId={threadRef.threadId}
616 + cwd={cwd}
617 + worktreePath={worktreePath}
618 + runtimeEnv={runtimeEnv}
619 + height={0}
620 + terminalIds={surface.terminalIds}
621 + activeTerminalId={surface.activeTerminalId}
622 + terminalGroups={[
623 + {
624 + id: surface.id,
625 + terminalIds: surface.terminalIds,
626 + ...(surface.splitDirection === "vertical" ? { splitDirection: "vertical" as const } : {}),
627 + },
628 + ]}
629 + activeTerminalGroupId={surface.id}
630 + focusRequestId={focusRequestId}
631 + onSplitTerminal={onSplitTerminal}
632 + onSplitTerminalVertical={onSplitTerminalVertical}
633 + onNewTerminal={onNewTerminal}
634 + splitShortcutLabel={splitShortcutLabel}
635 + splitVerticalShortcutLabel={splitVerticalShortcutLabel}
636 + newShortcutLabel={newShortcutLabel}
637 + closeShortcutLabel={closeShortcutLabel}
638 + onActiveTerminalChange={onActiveTerminalChange}
639 + onCloseTerminal={onCloseTerminal}
640 + onHeightChange={() => undefined}
641 + onAddTerminalContext={onAddTerminalContext}
642 + terminalLabelsById={terminalLabelsById}
643 + terminalLaunchLocationsById={terminalLaunchLocationsById}
644 + keybindings={keybindings}
645 + />
646 + );
647 +});