Enable terminals for local draft threads
Adds local terminal state management for draft threads before they're promoted to server threads. Terminal operations (open, split, create, close, activate, resize) now work on draft threads and hydrate into the server thread state when the draft is promoted. Co-Authored-By: Claude Opus 4.5 <[email protected]>
This commit is contained in:
1 parent
2c7101092a
commit
b1c85fa3cc
5 files changed
+767
-109
No files matched your search
@@ -57,6 +57,11 @@ import {
|
||||
type Thread,
|
||||
type TurnDiffSummary,
|
||||
} from "../types";
|
||||
import {
|
||||
createDefaultDraftThreadTerminalState,
|
||||
reduceDraftThreadTerminalState,
|
||||
type DraftThreadTerminalState,
|
||||
} from "../draftThreadTerminalState";
|
||||
import { basenameOfPath, getVscodeIconUrlForEntry } from "../vscode-icons";
|
||||
import { useTheme } from "../hooks/useTheme";
|
||||
import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries";
|
||||
@@ -198,6 +203,7 @@ function buildExpandedImagePreview(
|
||||
function buildLocalDraftThread(
|
||||
threadId: ThreadId,
|
||||
draftThread: DraftThreadState,
|
||||
draftTerminalState: DraftThreadTerminalState,
|
||||
fallbackModel: string,
|
||||
error: string | null,
|
||||
): Thread {
|
||||
@@ -207,18 +213,13 @@ function buildLocalDraftThread(
|
||||
projectId: draftThread.projectId,
|
||||
title: "New thread",
|
||||
model: fallbackModel,
|
||||
terminalOpen: false,
|
||||
terminalHeight: 280,
|
||||
terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
|
||||
runningTerminalIds: [],
|
||||
activeTerminalId: DEFAULT_THREAD_TERMINAL_ID,
|
||||
terminalGroups: [
|
||||
{
|
||||
id: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
|
||||
terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
|
||||
},
|
||||
],
|
||||
activeTerminalGroupId: `group-${DEFAULT_THREAD_TERMINAL_ID}`,
|
||||
terminalOpen: draftTerminalState.terminalOpen,
|
||||
terminalHeight: draftTerminalState.terminalHeight,
|
||||
terminalIds: draftTerminalState.terminalIds,
|
||||
runningTerminalIds: draftTerminalState.runningTerminalIds,
|
||||
activeTerminalId: draftTerminalState.activeTerminalId,
|
||||
terminalGroups: draftTerminalState.terminalGroups,
|
||||
activeTerminalGroupId: draftTerminalState.activeTerminalGroupId,
|
||||
session: null,
|
||||
messages: [],
|
||||
error,
|
||||
@@ -232,6 +233,46 @@ function buildLocalDraftThread(
|
||||
};
|
||||
}
|
||||
|
||||
function threadTerminalsMatchState(thread: Thread, terminalState: DraftThreadTerminalState): boolean {
|
||||
if (
|
||||
thread.terminalOpen !== terminalState.terminalOpen ||
|
||||
thread.terminalHeight !== terminalState.terminalHeight ||
|
||||
thread.activeTerminalId !== terminalState.activeTerminalId ||
|
||||
thread.activeTerminalGroupId !== terminalState.activeTerminalGroupId
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
thread.terminalIds.length !== terminalState.terminalIds.length ||
|
||||
thread.runningTerminalIds.length !== terminalState.runningTerminalIds.length ||
|
||||
thread.terminalGroups.length !== terminalState.terminalGroups.length
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
if (!thread.terminalIds.every((terminalId, index) => terminalId === terminalState.terminalIds[index])) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
!thread.runningTerminalIds.every(
|
||||
(terminalId, index) => terminalId === terminalState.runningTerminalIds[index],
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return thread.terminalGroups.every((group, groupIndex) => {
|
||||
const terminalStateGroup = terminalState.terminalGroups[groupIndex];
|
||||
if (!terminalStateGroup || group.id !== terminalStateGroup.id) {
|
||||
return false;
|
||||
}
|
||||
if (group.terminalIds.length !== terminalStateGroup.terminalIds.length) {
|
||||
return false;
|
||||
}
|
||||
return group.terminalIds.every(
|
||||
(terminalId, terminalIdIndex) => terminalId === terminalStateGroup.terminalIds[terminalIdIndex],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function revokeBlobPreviewUrl(previewUrl: string | undefined): void {
|
||||
if (!previewUrl || typeof URL === "undefined" || !previewUrl.startsWith("blob:")) {
|
||||
return;
|
||||
@@ -475,6 +516,9 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
const [localDraftErrorsByThreadId, setLocalDraftErrorsByThreadId] = useState<
|
||||
Record<ThreadId, string | null>
|
||||
>({});
|
||||
const [draftTerminalStateByThreadId, setDraftTerminalStateByThreadId] = useState<
|
||||
Record<ThreadId, DraftThreadTerminalState>
|
||||
>({});
|
||||
const [sendPhase, setSendPhase] = useState<SendPhase>("idle");
|
||||
const [isConnecting, _setIsConnecting] = useState(false);
|
||||
const [isRevertingCheckpoint, setIsRevertingCheckpoint] = useState(false);
|
||||
@@ -497,6 +541,26 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
const dragDepthRef = useRef(0);
|
||||
const terminalOpenByThreadRef = useRef<Record<string, boolean>>({});
|
||||
|
||||
const updateDraftTerminalState = useCallback(
|
||||
(
|
||||
targetThreadId: ThreadId,
|
||||
updater: (state: DraftThreadTerminalState) => DraftThreadTerminalState,
|
||||
) => {
|
||||
setDraftTerminalStateByThreadId((existing) => {
|
||||
const previous = existing[targetThreadId] ?? createDefaultDraftThreadTerminalState();
|
||||
const next = updater(previous);
|
||||
if (next === previous) {
|
||||
return existing;
|
||||
}
|
||||
return {
|
||||
...existing,
|
||||
[targetThreadId]: next,
|
||||
};
|
||||
});
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const setPrompt = useCallback(
|
||||
(nextPrompt: string) => {
|
||||
setComposerDraftPrompt(threadId, nextPrompt);
|
||||
@@ -531,17 +595,19 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
const serverThread = state.threads.find((t) => t.id === threadId);
|
||||
const fallbackDraftProject = state.projects.find((project) => project.id === draftThread?.projectId);
|
||||
const localDraftError = localDraftErrorsByThreadId[threadId] ?? null;
|
||||
const localDraftTerminalState = draftTerminalStateByThreadId[threadId];
|
||||
const localDraftThread = useMemo(
|
||||
() =>
|
||||
draftThread
|
||||
? buildLocalDraftThread(
|
||||
threadId,
|
||||
draftThread,
|
||||
localDraftTerminalState ?? createDefaultDraftThreadTerminalState(),
|
||||
fallbackDraftProject?.model ?? DEFAULT_MODEL,
|
||||
localDraftError,
|
||||
)
|
||||
: undefined,
|
||||
[draftThread, fallbackDraftProject?.model, localDraftError, threadId],
|
||||
[draftThread, fallbackDraftProject?.model, localDraftError, localDraftTerminalState, threadId],
|
||||
);
|
||||
const activeThread = serverThread ?? localDraftThread;
|
||||
const isServerThread = serverThread !== undefined;
|
||||
@@ -569,6 +635,27 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
});
|
||||
}, [serverThread, threadId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!serverThread || !localDraftTerminalState) {
|
||||
return;
|
||||
}
|
||||
if (!threadTerminalsMatchState(serverThread, localDraftTerminalState)) {
|
||||
dispatch({
|
||||
type: "HYDRATE_THREAD_TERMINALS",
|
||||
threadId: serverThread.id,
|
||||
terminalState: localDraftTerminalState,
|
||||
});
|
||||
return;
|
||||
}
|
||||
setDraftTerminalStateByThreadId((existing) => {
|
||||
if (!(threadId in existing)) {
|
||||
return existing;
|
||||
}
|
||||
const { [threadId]: _removed, ...rest } = existing;
|
||||
return rest as Record<ThreadId, DraftThreadTerminalState>;
|
||||
});
|
||||
}, [dispatch, localDraftTerminalState, serverThread, threadId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeThread?.id) return;
|
||||
if (!latestTurnSettled) return;
|
||||
@@ -852,6 +939,29 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
);
|
||||
const hasReachedTerminalLimit =
|
||||
(activeThread?.terminalIds.length ?? 0) >= MAX_THREAD_TERMINAL_COUNT;
|
||||
const setThreadError = useCallback(
|
||||
(targetThreadId: ThreadId | null, error: string | null) => {
|
||||
if (!targetThreadId) return;
|
||||
if (state.threads.some((thread) => thread.id === targetThreadId)) {
|
||||
dispatch({
|
||||
type: "SET_ERROR",
|
||||
threadId: targetThreadId,
|
||||
error,
|
||||
});
|
||||
return;
|
||||
}
|
||||
setLocalDraftErrorsByThreadId((existing) => {
|
||||
if ((existing[targetThreadId] ?? null) === error) {
|
||||
return existing;
|
||||
}
|
||||
return {
|
||||
...existing,
|
||||
[targetThreadId]: error,
|
||||
};
|
||||
});
|
||||
},
|
||||
[dispatch, state.threads],
|
||||
);
|
||||
|
||||
const focusComposer = useCallback(() => {
|
||||
const textarea = textareaRef.current;
|
||||
@@ -865,43 +975,108 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
focusComposer();
|
||||
});
|
||||
}, [focusComposer]);
|
||||
const setTerminalOpen = useCallback(
|
||||
(open: boolean) => {
|
||||
if (!activeThreadId) return;
|
||||
if (isLocalDraftThread) {
|
||||
updateDraftTerminalState(activeThreadId, (draftState) =>
|
||||
reduceDraftThreadTerminalState(draftState, {
|
||||
type: "set-open",
|
||||
open,
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_THREAD_TERMINAL_OPEN",
|
||||
threadId: activeThreadId,
|
||||
open,
|
||||
});
|
||||
},
|
||||
[activeThreadId, dispatch, isLocalDraftThread, updateDraftTerminalState],
|
||||
);
|
||||
const setTerminalHeight = useCallback(
|
||||
(height: number) => {
|
||||
if (!activeThreadId) return;
|
||||
if (isLocalDraftThread) {
|
||||
updateDraftTerminalState(activeThreadId, (draftState) =>
|
||||
reduceDraftThreadTerminalState(draftState, {
|
||||
type: "set-height",
|
||||
height,
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: "SET_THREAD_TERMINAL_HEIGHT",
|
||||
threadId: activeThreadId,
|
||||
height,
|
||||
});
|
||||
},
|
||||
[activeThreadId, dispatch, isLocalDraftThread, updateDraftTerminalState],
|
||||
);
|
||||
const toggleTerminalVisibility = useCallback(() => {
|
||||
if (!activeThreadId) return;
|
||||
dispatch({
|
||||
type: "SET_THREAD_TERMINAL_OPEN",
|
||||
threadId: activeThreadId,
|
||||
open: !activeThread?.terminalOpen,
|
||||
});
|
||||
}, [activeThread?.terminalOpen, activeThreadId, dispatch]);
|
||||
setTerminalOpen(!activeThread?.terminalOpen);
|
||||
}, [activeThread?.terminalOpen, activeThreadId, setTerminalOpen]);
|
||||
const splitTerminal = useCallback(() => {
|
||||
if (!activeThreadId || hasReachedTerminalLimit) return;
|
||||
dispatch({
|
||||
type: "SPLIT_THREAD_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId: `terminal-${crypto.randomUUID()}`,
|
||||
});
|
||||
setTerminalFocusRequestId((value) => value + 1);
|
||||
}, [activeThreadId, dispatch, hasReachedTerminalLimit]);
|
||||
const createNewTerminal = useCallback(() => {
|
||||
if (!activeThreadId || hasReachedTerminalLimit) return;
|
||||
dispatch({
|
||||
type: "NEW_THREAD_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId: `terminal-${crypto.randomUUID()}`,
|
||||
});
|
||||
setTerminalFocusRequestId((value) => value + 1);
|
||||
}, [activeThreadId, dispatch, hasReachedTerminalLimit]);
|
||||
const activateTerminal = useCallback(
|
||||
(terminalId: string) => {
|
||||
if (!activeThreadId) return;
|
||||
const terminalId = `terminal-${crypto.randomUUID()}`;
|
||||
if (isLocalDraftThread) {
|
||||
updateDraftTerminalState(activeThreadId, (draftState) =>
|
||||
reduceDraftThreadTerminalState(draftState, {
|
||||
type: "split",
|
||||
terminalId,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
dispatch({
|
||||
type: "SET_THREAD_ACTIVE_TERMINAL",
|
||||
type: "SPLIT_THREAD_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId,
|
||||
});
|
||||
}
|
||||
setTerminalFocusRequestId((value) => value + 1);
|
||||
}, [activeThreadId, dispatch, hasReachedTerminalLimit, isLocalDraftThread, updateDraftTerminalState]);
|
||||
const createNewTerminal = useCallback(() => {
|
||||
if (!activeThreadId || hasReachedTerminalLimit) return;
|
||||
const terminalId = `terminal-${crypto.randomUUID()}`;
|
||||
if (isLocalDraftThread) {
|
||||
updateDraftTerminalState(activeThreadId, (draftState) =>
|
||||
reduceDraftThreadTerminalState(draftState, {
|
||||
type: "new",
|
||||
terminalId,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
dispatch({
|
||||
type: "NEW_THREAD_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId,
|
||||
});
|
||||
}
|
||||
setTerminalFocusRequestId((value) => value + 1);
|
||||
}, [activeThreadId, dispatch, hasReachedTerminalLimit, isLocalDraftThread, updateDraftTerminalState]);
|
||||
const activateTerminal = useCallback(
|
||||
(terminalId: string) => {
|
||||
if (!activeThreadId) return;
|
||||
if (isLocalDraftThread) {
|
||||
updateDraftTerminalState(activeThreadId, (draftState) =>
|
||||
reduceDraftThreadTerminalState(draftState, {
|
||||
type: "set-active",
|
||||
terminalId,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
dispatch({
|
||||
type: "SET_THREAD_ACTIVE_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId,
|
||||
});
|
||||
}
|
||||
setTerminalFocusRequestId((value) => value + 1);
|
||||
},
|
||||
[activeThreadId, dispatch],
|
||||
[activeThreadId, dispatch, isLocalDraftThread, updateDraftTerminalState],
|
||||
);
|
||||
const closeTerminal = useCallback(
|
||||
(terminalId: string) => {
|
||||
@@ -924,14 +1099,23 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
} else {
|
||||
void fallbackExitWrite();
|
||||
}
|
||||
dispatch({
|
||||
type: "CLOSE_THREAD_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId,
|
||||
});
|
||||
if (isLocalDraftThread) {
|
||||
updateDraftTerminalState(activeThreadId, (draftState) =>
|
||||
reduceDraftThreadTerminalState(draftState, {
|
||||
type: "close",
|
||||
terminalId,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
dispatch({
|
||||
type: "CLOSE_THREAD_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId,
|
||||
});
|
||||
}
|
||||
setTerminalFocusRequestId((value) => value + 1);
|
||||
},
|
||||
[activeThread?.terminalIds.length, activeThreadId, dispatch],
|
||||
[activeThread?.terminalIds.length, activeThreadId, dispatch, isLocalDraftThread, updateDraftTerminalState],
|
||||
);
|
||||
const runProjectScript = useCallback(
|
||||
async (
|
||||
@@ -965,23 +1149,37 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
? `terminal-${crypto.randomUUID()}`
|
||||
: baseTerminalId;
|
||||
|
||||
dispatch({
|
||||
type: "SET_THREAD_TERMINAL_OPEN",
|
||||
threadId: activeThreadId,
|
||||
open: true,
|
||||
});
|
||||
setTerminalOpen(true);
|
||||
if (shouldCreateNewTerminal) {
|
||||
dispatch({
|
||||
type: "NEW_THREAD_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId: targetTerminalId,
|
||||
});
|
||||
if (isLocalDraftThread) {
|
||||
updateDraftTerminalState(activeThreadId, (draftState) =>
|
||||
reduceDraftThreadTerminalState(draftState, {
|
||||
type: "new",
|
||||
terminalId: targetTerminalId,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
dispatch({
|
||||
type: "NEW_THREAD_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId: targetTerminalId,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
dispatch({
|
||||
type: "SET_THREAD_ACTIVE_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId: targetTerminalId,
|
||||
});
|
||||
if (isLocalDraftThread) {
|
||||
updateDraftTerminalState(activeThreadId, (draftState) =>
|
||||
reduceDraftThreadTerminalState(draftState, {
|
||||
type: "set-active",
|
||||
terminalId: targetTerminalId,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
dispatch({
|
||||
type: "SET_THREAD_ACTIVE_TERMINAL",
|
||||
threadId: activeThreadId,
|
||||
terminalId: targetTerminalId,
|
||||
});
|
||||
}
|
||||
}
|
||||
setTerminalFocusRequestId((value) => value + 1);
|
||||
|
||||
@@ -1012,14 +1210,24 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
data: `${script.command}\r`,
|
||||
});
|
||||
} catch (error) {
|
||||
dispatch({
|
||||
type: "SET_ERROR",
|
||||
threadId: activeThreadId,
|
||||
error: error instanceof Error ? error.message : `Failed to run script "${script.name}".`,
|
||||
});
|
||||
setThreadError(
|
||||
activeThreadId,
|
||||
error instanceof Error ? error.message : `Failed to run script "${script.name}".`,
|
||||
);
|
||||
}
|
||||
},
|
||||
[activeProject, activeThread, activeThreadId, dispatch, gitCwd, isServerThread],
|
||||
[
|
||||
activeProject,
|
||||
activeThread,
|
||||
activeThreadId,
|
||||
dispatch,
|
||||
gitCwd,
|
||||
isLocalDraftThread,
|
||||
isServerThread,
|
||||
setTerminalOpen,
|
||||
setThreadError,
|
||||
updateDraftTerminalState,
|
||||
],
|
||||
);
|
||||
const persistProjectScripts = useCallback(
|
||||
async (input: {
|
||||
@@ -1466,11 +1674,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (!activeThread?.terminalOpen) {
|
||||
dispatch({
|
||||
type: "SET_THREAD_TERMINAL_OPEN",
|
||||
threadId: activeThreadId,
|
||||
open: true,
|
||||
});
|
||||
setTerminalOpen(true);
|
||||
}
|
||||
splitTerminal();
|
||||
return;
|
||||
@@ -1488,11 +1692,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (!activeThread?.terminalOpen) {
|
||||
dispatch({
|
||||
type: "SET_THREAD_TERMINAL_OPEN",
|
||||
threadId: activeThreadId,
|
||||
open: true,
|
||||
});
|
||||
setTerminalOpen(true);
|
||||
}
|
||||
createNewTerminal();
|
||||
return;
|
||||
@@ -1522,7 +1722,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
activeThreadId,
|
||||
closeTerminal,
|
||||
createNewTerminal,
|
||||
dispatch,
|
||||
setTerminalOpen,
|
||||
runProjectScript,
|
||||
splitTerminal,
|
||||
keybindings,
|
||||
@@ -1530,30 +1730,6 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
toggleTerminalVisibility,
|
||||
]);
|
||||
|
||||
const setThreadError = useCallback(
|
||||
(threadId: ThreadId | null, error: string | null) => {
|
||||
if (!threadId) return;
|
||||
if (state.threads.some((thread) => thread.id === threadId)) {
|
||||
dispatch({
|
||||
type: "SET_ERROR",
|
||||
threadId,
|
||||
error,
|
||||
});
|
||||
return;
|
||||
}
|
||||
setLocalDraftErrorsByThreadId((existing) => {
|
||||
if ((existing[threadId] ?? null) === error) {
|
||||
return existing;
|
||||
}
|
||||
return {
|
||||
...existing,
|
||||
[threadId]: error,
|
||||
};
|
||||
});
|
||||
},
|
||||
[dispatch, state.threads],
|
||||
);
|
||||
|
||||
const addComposerImages = (files: File[]) => {
|
||||
if (!activeThreadId || files.length === 0) return;
|
||||
|
||||
@@ -2489,13 +2665,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
|
||||
closeShortcutLabel={closeTerminalShortcutLabel ?? undefined}
|
||||
onActiveTerminalChange={activateTerminal}
|
||||
onCloseTerminal={closeTerminal}
|
||||
onHeightChange={(height) =>
|
||||
dispatch({
|
||||
type: "SET_THREAD_TERMINAL_HEIGHT",
|
||||
threadId: activeThread.id,
|
||||
height,
|
||||
})
|
||||
}
|
||||
onHeightChange={setTerminalHeight}
|
||||
/>
|
||||
);
|
||||
})()}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
createDefaultDraftThreadTerminalState,
|
||||
reduceDraftThreadTerminalState,
|
||||
} from "./draftThreadTerminalState";
|
||||
|
||||
describe("draftThreadTerminalState", () => {
|
||||
it("creates a closed default terminal state", () => {
|
||||
expect(createDefaultDraftThreadTerminalState()).toEqual({
|
||||
terminalOpen: false,
|
||||
terminalHeight: 280,
|
||||
terminalIds: ["default"],
|
||||
runningTerminalIds: [],
|
||||
activeTerminalId: "default",
|
||||
terminalGroups: [{ id: "group-default", terminalIds: ["default"] }],
|
||||
activeTerminalGroupId: "group-default",
|
||||
});
|
||||
});
|
||||
|
||||
it("opens and splits terminal tabs into the active group", () => {
|
||||
const opened = reduceDraftThreadTerminalState(createDefaultDraftThreadTerminalState(), {
|
||||
type: "set-open",
|
||||
open: true,
|
||||
});
|
||||
const split = reduceDraftThreadTerminalState(opened, {
|
||||
type: "split",
|
||||
terminalId: "terminal-2",
|
||||
});
|
||||
|
||||
expect(split.terminalOpen).toBe(true);
|
||||
expect(split.terminalIds).toEqual(["default", "terminal-2"]);
|
||||
expect(split.activeTerminalId).toBe("terminal-2");
|
||||
expect(split.terminalGroups).toEqual([
|
||||
{
|
||||
id: "group-default",
|
||||
terminalIds: ["default", "terminal-2"],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("creates new terminals in a new group", () => {
|
||||
const next = reduceDraftThreadTerminalState(createDefaultDraftThreadTerminalState(), {
|
||||
type: "new",
|
||||
terminalId: "terminal-2",
|
||||
});
|
||||
|
||||
expect(next.terminalIds).toEqual(["default", "terminal-2"]);
|
||||
expect(next.activeTerminalId).toBe("terminal-2");
|
||||
expect(next.activeTerminalGroupId).toBe("group-terminal-2");
|
||||
expect(next.terminalGroups).toEqual([
|
||||
{ id: "group-default", terminalIds: ["default"] },
|
||||
{ id: "group-terminal-2", terminalIds: ["terminal-2"] },
|
||||
]);
|
||||
});
|
||||
|
||||
it("resets to default when closing the last terminal", () => {
|
||||
const closed = reduceDraftThreadTerminalState(createDefaultDraftThreadTerminalState(), {
|
||||
type: "close",
|
||||
terminalId: "default",
|
||||
});
|
||||
|
||||
expect(closed).toEqual(createDefaultDraftThreadTerminalState());
|
||||
});
|
||||
|
||||
it("keeps a valid active terminal after closing an active split terminal", () => {
|
||||
const splitOnce = reduceDraftThreadTerminalState(createDefaultDraftThreadTerminalState(), {
|
||||
type: "split",
|
||||
terminalId: "terminal-2",
|
||||
});
|
||||
const splitTwice = reduceDraftThreadTerminalState(splitOnce, {
|
||||
type: "split",
|
||||
terminalId: "terminal-3",
|
||||
});
|
||||
|
||||
const closed = reduceDraftThreadTerminalState(splitTwice, {
|
||||
type: "close",
|
||||
terminalId: "terminal-3",
|
||||
});
|
||||
|
||||
expect(closed.activeTerminalId).toBe("terminal-2");
|
||||
expect(closed.terminalIds).toEqual(["default", "terminal-2"]);
|
||||
expect(closed.terminalGroups).toEqual([
|
||||
{ id: "group-default", terminalIds: ["default", "terminal-2"] },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,332 @@
|
||||
import {
|
||||
DEFAULT_THREAD_TERMINAL_HEIGHT,
|
||||
DEFAULT_THREAD_TERMINAL_ID,
|
||||
MAX_THREAD_TERMINAL_COUNT,
|
||||
type ThreadTerminalGroup,
|
||||
} from "./types";
|
||||
|
||||
export interface DraftThreadTerminalState {
|
||||
terminalOpen: boolean;
|
||||
terminalHeight: number;
|
||||
terminalIds: string[];
|
||||
runningTerminalIds: string[];
|
||||
activeTerminalId: string;
|
||||
terminalGroups: ThreadTerminalGroup[];
|
||||
activeTerminalGroupId: string;
|
||||
}
|
||||
|
||||
export type DraftThreadTerminalAction =
|
||||
| { type: "set-open"; open: boolean }
|
||||
| { type: "set-height"; height: number }
|
||||
| { type: "split"; terminalId: string }
|
||||
| { type: "new"; terminalId: string }
|
||||
| { type: "set-active"; terminalId: string }
|
||||
| { type: "close"; terminalId: string }
|
||||
| { type: "set-activity"; terminalId: string; hasRunningSubprocess: boolean };
|
||||
|
||||
function normalizeTerminalIds(terminalIds: string[]): string[] {
|
||||
return [...new Set(terminalIds.map((id) => id.trim()).filter((id) => id.length > 0))];
|
||||
}
|
||||
|
||||
function fallbackGroupId(terminalId: string): string {
|
||||
return `group-${terminalId}`;
|
||||
}
|
||||
|
||||
function assignUniqueGroupId(baseId: string, usedGroupIds: Set<string>): string {
|
||||
let candidate = baseId;
|
||||
let index = 2;
|
||||
while (usedGroupIds.has(candidate)) {
|
||||
candidate = `${baseId}-${index}`;
|
||||
index += 1;
|
||||
}
|
||||
usedGroupIds.add(candidate);
|
||||
return candidate;
|
||||
}
|
||||
|
||||
function findGroupIndexByTerminalId(terminalGroups: ThreadTerminalGroup[], terminalId: string): number {
|
||||
return terminalGroups.findIndex((group) => group.terminalIds.includes(terminalId));
|
||||
}
|
||||
|
||||
function normalizeTerminalGroups(
|
||||
terminalGroups: ThreadTerminalGroup[],
|
||||
terminalIds: string[],
|
||||
): ThreadTerminalGroup[] {
|
||||
const validTerminalIdSet = new Set(terminalIds);
|
||||
const assignedTerminalIds = new Set<string>();
|
||||
const nextGroups: ThreadTerminalGroup[] = [];
|
||||
const usedGroupIds = new Set<string>();
|
||||
|
||||
for (const group of terminalGroups) {
|
||||
const groupTerminalIds = normalizeTerminalIds(group.terminalIds).filter((terminalId) => {
|
||||
if (!validTerminalIdSet.has(terminalId)) return false;
|
||||
if (assignedTerminalIds.has(terminalId)) return false;
|
||||
return true;
|
||||
});
|
||||
if (groupTerminalIds.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const terminalId of groupTerminalIds) {
|
||||
assignedTerminalIds.add(terminalId);
|
||||
}
|
||||
|
||||
nextGroups.push({
|
||||
id: assignUniqueGroupId(group.id, usedGroupIds),
|
||||
terminalIds: groupTerminalIds,
|
||||
});
|
||||
}
|
||||
|
||||
for (const terminalId of terminalIds) {
|
||||
if (assignedTerminalIds.has(terminalId)) continue;
|
||||
nextGroups.push({
|
||||
id: assignUniqueGroupId(fallbackGroupId(terminalId), usedGroupIds),
|
||||
terminalIds: [terminalId],
|
||||
});
|
||||
}
|
||||
|
||||
if (nextGroups.length === 0) {
|
||||
return [
|
||||
{
|
||||
id: fallbackGroupId(DEFAULT_THREAD_TERMINAL_ID),
|
||||
terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
return nextGroups;
|
||||
}
|
||||
|
||||
export function createDefaultDraftThreadTerminalState(): DraftThreadTerminalState {
|
||||
return {
|
||||
terminalOpen: false,
|
||||
terminalHeight: DEFAULT_THREAD_TERMINAL_HEIGHT,
|
||||
terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
|
||||
runningTerminalIds: [],
|
||||
activeTerminalId: DEFAULT_THREAD_TERMINAL_ID,
|
||||
terminalGroups: [
|
||||
{
|
||||
id: fallbackGroupId(DEFAULT_THREAD_TERMINAL_ID),
|
||||
terminalIds: [DEFAULT_THREAD_TERMINAL_ID],
|
||||
},
|
||||
],
|
||||
activeTerminalGroupId: fallbackGroupId(DEFAULT_THREAD_TERMINAL_ID),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeDraftThreadTerminals(state: DraftThreadTerminalState): DraftThreadTerminalState {
|
||||
const terminalIds = normalizeTerminalIds(state.terminalIds);
|
||||
const nextTerminalIds = terminalIds.length > 0 ? terminalIds : [DEFAULT_THREAD_TERMINAL_ID];
|
||||
const runningTerminalIds = normalizeTerminalIds(state.runningTerminalIds).filter((terminalId) =>
|
||||
nextTerminalIds.includes(terminalId),
|
||||
);
|
||||
const activeTerminalId = nextTerminalIds.includes(state.activeTerminalId)
|
||||
? state.activeTerminalId
|
||||
: (nextTerminalIds[0] ?? DEFAULT_THREAD_TERMINAL_ID);
|
||||
const terminalGroups = normalizeTerminalGroups(state.terminalGroups, nextTerminalIds);
|
||||
const activeGroupIdFromState = terminalGroups.some((group) => group.id === state.activeTerminalGroupId)
|
||||
? state.activeTerminalGroupId
|
||||
: null;
|
||||
const activeGroupIdFromTerminal =
|
||||
terminalGroups.find((group) => group.terminalIds.includes(activeTerminalId))?.id ?? null;
|
||||
|
||||
return {
|
||||
terminalOpen: state.terminalOpen,
|
||||
terminalHeight:
|
||||
Number.isFinite(state.terminalHeight) && state.terminalHeight > 0
|
||||
? state.terminalHeight
|
||||
: DEFAULT_THREAD_TERMINAL_HEIGHT,
|
||||
terminalIds: nextTerminalIds,
|
||||
runningTerminalIds,
|
||||
activeTerminalId,
|
||||
terminalGroups,
|
||||
activeTerminalGroupId:
|
||||
activeGroupIdFromState ??
|
||||
activeGroupIdFromTerminal ??
|
||||
terminalGroups[0]?.id ??
|
||||
fallbackGroupId(DEFAULT_THREAD_TERMINAL_ID),
|
||||
};
|
||||
}
|
||||
|
||||
function closeDraftTerminal(
|
||||
state: DraftThreadTerminalState,
|
||||
terminalId: string,
|
||||
): DraftThreadTerminalState {
|
||||
if (!state.terminalIds.includes(terminalId)) {
|
||||
return state;
|
||||
}
|
||||
|
||||
const remainingTerminalIds = state.terminalIds.filter((id) => id !== terminalId);
|
||||
if (remainingTerminalIds.length === 0) {
|
||||
return createDefaultDraftThreadTerminalState();
|
||||
}
|
||||
|
||||
const closedTerminalIndex = state.terminalIds.indexOf(terminalId);
|
||||
const nextActiveTerminalId =
|
||||
state.activeTerminalId === terminalId
|
||||
? (remainingTerminalIds[Math.min(closedTerminalIndex, remainingTerminalIds.length - 1)] ??
|
||||
remainingTerminalIds[0] ??
|
||||
DEFAULT_THREAD_TERMINAL_ID)
|
||||
: state.activeTerminalId;
|
||||
|
||||
const terminalGroups = state.terminalGroups
|
||||
.map((group) => ({
|
||||
...group,
|
||||
terminalIds: group.terminalIds.filter((id) => id !== terminalId),
|
||||
}))
|
||||
.filter((group) => group.terminalIds.length > 0);
|
||||
|
||||
const nextActiveTerminalGroupId =
|
||||
terminalGroups.find((group) => group.terminalIds.includes(nextActiveTerminalId))?.id ??
|
||||
terminalGroups[0]?.id ??
|
||||
fallbackGroupId(nextActiveTerminalId);
|
||||
|
||||
return normalizeDraftThreadTerminals({
|
||||
terminalOpen: state.terminalOpen,
|
||||
terminalHeight: state.terminalHeight,
|
||||
terminalIds: remainingTerminalIds,
|
||||
runningTerminalIds: state.runningTerminalIds.filter((id) => id !== terminalId),
|
||||
activeTerminalId: nextActiveTerminalId,
|
||||
terminalGroups,
|
||||
activeTerminalGroupId: nextActiveTerminalGroupId,
|
||||
});
|
||||
}
|
||||
|
||||
function copyTerminalGroups(groups: ThreadTerminalGroup[]): ThreadTerminalGroup[] {
|
||||
return groups.map((group) => ({
|
||||
id: group.id,
|
||||
terminalIds: [...group.terminalIds],
|
||||
}));
|
||||
}
|
||||
|
||||
export function reduceDraftThreadTerminalState(
|
||||
state: DraftThreadTerminalState,
|
||||
action: DraftThreadTerminalAction,
|
||||
): DraftThreadTerminalState {
|
||||
const normalized = normalizeDraftThreadTerminals(state);
|
||||
|
||||
if (action.type === "set-open") {
|
||||
if (normalized.terminalOpen === action.open) return normalized;
|
||||
return {
|
||||
...normalized,
|
||||
terminalOpen: action.open,
|
||||
};
|
||||
}
|
||||
|
||||
if (action.type === "set-height") {
|
||||
if (!Number.isFinite(action.height) || action.height <= 0 || normalized.terminalHeight === action.height) {
|
||||
return normalized;
|
||||
}
|
||||
return {
|
||||
...normalized,
|
||||
terminalHeight: action.height,
|
||||
};
|
||||
}
|
||||
|
||||
if (action.type === "set-active") {
|
||||
if (!normalized.terminalIds.includes(action.terminalId)) {
|
||||
return normalized;
|
||||
}
|
||||
const activeTerminalGroupId =
|
||||
normalized.terminalGroups.find((group) => group.terminalIds.includes(action.terminalId))?.id ??
|
||||
normalized.activeTerminalGroupId;
|
||||
return {
|
||||
...normalized,
|
||||
activeTerminalId: action.terminalId,
|
||||
activeTerminalGroupId,
|
||||
};
|
||||
}
|
||||
|
||||
if (action.type === "close") {
|
||||
return closeDraftTerminal(normalized, action.terminalId);
|
||||
}
|
||||
|
||||
if (action.type === "set-activity") {
|
||||
if (!normalized.terminalIds.includes(action.terminalId)) {
|
||||
return normalized;
|
||||
}
|
||||
const runningTerminalIds = new Set(normalized.runningTerminalIds);
|
||||
if (action.hasRunningSubprocess) {
|
||||
runningTerminalIds.add(action.terminalId);
|
||||
} else {
|
||||
runningTerminalIds.delete(action.terminalId);
|
||||
}
|
||||
return {
|
||||
...normalized,
|
||||
runningTerminalIds: [...runningTerminalIds],
|
||||
};
|
||||
}
|
||||
|
||||
if (!action.terminalId || action.terminalId.trim().length === 0) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const isNewTerminal = !normalized.terminalIds.includes(action.terminalId);
|
||||
if (isNewTerminal && normalized.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const terminalIds = isNewTerminal
|
||||
? [...normalized.terminalIds, action.terminalId]
|
||||
: normalized.terminalIds;
|
||||
const terminalGroups = copyTerminalGroups(normalized.terminalGroups);
|
||||
const existingGroupIndex = findGroupIndexByTerminalId(terminalGroups, action.terminalId);
|
||||
if (existingGroupIndex >= 0) {
|
||||
terminalGroups[existingGroupIndex]!.terminalIds = terminalGroups[existingGroupIndex]!.terminalIds.filter(
|
||||
(id) => id !== action.terminalId,
|
||||
);
|
||||
if (terminalGroups[existingGroupIndex]!.terminalIds.length === 0) {
|
||||
terminalGroups.splice(existingGroupIndex, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (action.type === "new") {
|
||||
const usedGroupIds = new Set(terminalGroups.map((group) => group.id));
|
||||
const nextGroupId = assignUniqueGroupId(fallbackGroupId(action.terminalId), usedGroupIds);
|
||||
terminalGroups.push({ id: nextGroupId, terminalIds: [action.terminalId] });
|
||||
return normalizeDraftThreadTerminals({
|
||||
...normalized,
|
||||
terminalOpen: true,
|
||||
terminalIds,
|
||||
activeTerminalId: action.terminalId,
|
||||
terminalGroups,
|
||||
activeTerminalGroupId: nextGroupId,
|
||||
});
|
||||
}
|
||||
|
||||
let activeGroupIndex = terminalGroups.findIndex((group) => group.id === normalized.activeTerminalGroupId);
|
||||
if (activeGroupIndex < 0) {
|
||||
activeGroupIndex = findGroupIndexByTerminalId(terminalGroups, normalized.activeTerminalId);
|
||||
}
|
||||
if (activeGroupIndex < 0) {
|
||||
const usedGroupIds = new Set(terminalGroups.map((group) => group.id));
|
||||
const nextGroupId = assignUniqueGroupId(
|
||||
fallbackGroupId(normalized.activeTerminalId),
|
||||
usedGroupIds,
|
||||
);
|
||||
terminalGroups.push({ id: nextGroupId, terminalIds: [normalized.activeTerminalId] });
|
||||
activeGroupIndex = terminalGroups.length - 1;
|
||||
}
|
||||
|
||||
const destinationGroup = terminalGroups[activeGroupIndex];
|
||||
if (!destinationGroup) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
if (!destinationGroup.terminalIds.includes(action.terminalId)) {
|
||||
const anchorIndex = destinationGroup.terminalIds.indexOf(normalized.activeTerminalId);
|
||||
if (anchorIndex >= 0) {
|
||||
destinationGroup.terminalIds.splice(anchorIndex + 1, 0, action.terminalId);
|
||||
} else {
|
||||
destinationGroup.terminalIds.push(action.terminalId);
|
||||
}
|
||||
}
|
||||
|
||||
return normalizeDraftThreadTerminals({
|
||||
...normalized,
|
||||
terminalOpen: true,
|
||||
terminalIds,
|
||||
activeTerminalId: action.terminalId,
|
||||
terminalGroups,
|
||||
activeTerminalGroupId: destinationGroup.id,
|
||||
});
|
||||
}
|
||||
@@ -158,4 +158,43 @@ describe("store terminal activity reducer", () => {
|
||||
|
||||
expect(next.threads[0]?.runningTerminalIds).toEqual([]);
|
||||
});
|
||||
|
||||
it("hydrates terminal state for an existing thread", () => {
|
||||
const state = makeState(makeThread());
|
||||
const next = reducer(state, {
|
||||
type: "HYDRATE_THREAD_TERMINALS",
|
||||
threadId: ThreadId.makeUnsafe("thread-1"),
|
||||
terminalState: {
|
||||
terminalOpen: true,
|
||||
terminalHeight: 360,
|
||||
terminalIds: ["default", "terminal-2"],
|
||||
runningTerminalIds: ["terminal-2"],
|
||||
activeTerminalId: "terminal-2",
|
||||
terminalGroups: [
|
||||
{
|
||||
id: "group-default",
|
||||
terminalIds: ["default"],
|
||||
},
|
||||
{
|
||||
id: "group-terminal-2",
|
||||
terminalIds: ["terminal-2"],
|
||||
},
|
||||
],
|
||||
activeTerminalGroupId: "group-terminal-2",
|
||||
},
|
||||
});
|
||||
|
||||
expect(next.threads[0]).toMatchObject({
|
||||
terminalOpen: true,
|
||||
terminalHeight: 360,
|
||||
terminalIds: ["default", "terminal-2"],
|
||||
runningTerminalIds: ["terminal-2"],
|
||||
activeTerminalId: "terminal-2",
|
||||
terminalGroups: [
|
||||
{ id: "group-default", terminalIds: ["default"] },
|
||||
{ id: "group-terminal-2", terminalIds: ["terminal-2"] },
|
||||
],
|
||||
activeTerminalGroupId: "group-terminal-2",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -32,6 +32,19 @@ import {
|
||||
|
||||
type Action =
|
||||
| { type: "SYNC_SERVER_READ_MODEL"; readModel: OrchestrationReadModel }
|
||||
| {
|
||||
type: "HYDRATE_THREAD_TERMINALS";
|
||||
threadId: ThreadId;
|
||||
terminalState: {
|
||||
terminalOpen: boolean;
|
||||
terminalHeight: number;
|
||||
terminalIds: string[];
|
||||
runningTerminalIds: string[];
|
||||
activeTerminalId: string;
|
||||
terminalGroups: ThreadTerminalGroup[];
|
||||
activeTerminalGroupId: string;
|
||||
};
|
||||
}
|
||||
| { type: "MARK_THREAD_VISITED"; threadId: ThreadId; visitedAt?: string }
|
||||
| { type: "MARK_THREAD_UNREAD"; threadId: ThreadId }
|
||||
| { type: "TOGGLE_PROJECT"; projectId: Project["id"] }
|
||||
@@ -536,6 +549,23 @@ export function reducer(state: AppState, action: Action): AppState {
|
||||
};
|
||||
}
|
||||
|
||||
case "HYDRATE_THREAD_TERMINALS":
|
||||
return {
|
||||
...state,
|
||||
threads: updateThread(state.threads, action.threadId, (thread) =>
|
||||
normalizeThreadTerminals({
|
||||
...thread,
|
||||
terminalOpen: action.terminalState.terminalOpen,
|
||||
terminalHeight: action.terminalState.terminalHeight,
|
||||
terminalIds: action.terminalState.terminalIds,
|
||||
runningTerminalIds: action.terminalState.runningTerminalIds,
|
||||
activeTerminalId: action.terminalState.activeTerminalId,
|
||||
terminalGroups: action.terminalState.terminalGroups,
|
||||
activeTerminalGroupId: action.terminalState.activeTerminalGroupId,
|
||||
}),
|
||||
),
|
||||
};
|
||||
|
||||
case "MARK_THREAD_VISITED": {
|
||||
const visitedAt = action.visitedAt ?? new Date().toISOString();
|
||||
const visitedAtMs = Date.parse(visitedAt);
|
||||
|
||||
Reference in new issue
Block a user