Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/command-center/cells.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { AgentSession, WorkspaceMode } from "@posthog/shared";
import type { Task } from "@posthog/shared/domain-types";
import { getTerminalCellId, isBrainrotCell, isTerminalCell } from "./grid";
import {
getTerminalCellCwd,
getTerminalCellId,
isBrainrotCell,
isTerminalCell,
} from "./grid";
import { type CellStatus, deriveStatus, getRepoName } from "./status";

export interface CommandCenterCellData {
Expand All @@ -15,6 +20,7 @@ export interface CommandCenterCellData {
isBrainrot: boolean;
// Standalone terminal slot, independent of any agent run.
terminalId: string | null;
terminalCwd: string | null;
}

export interface BuildCellsInput {
Expand All @@ -32,6 +38,7 @@ const EMPTY_CELL_DATA = {
workspaceMode: null,
isBrainrot: false,
terminalId: null,
terminalCwd: null,
};

export function buildCommandCenterCells(
Expand All @@ -49,6 +56,7 @@ export function buildCommandCenterCells(
...EMPTY_CELL_DATA,
cellIndex,
terminalId: getTerminalCellId(cellValue),
terminalCwd: getTerminalCellCwd(cellValue),
};
}

Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/command-center/grid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getCellCount,
getCellSessionId,
getGridDimensions,
getTerminalCellCwd,
getTerminalCellId,
isBrainrotCell,
isTerminalCell,
Expand Down Expand Up @@ -71,6 +72,14 @@ describe("terminal cells", () => {
const value = makeTerminalCellValue("abc123");
expect(isTerminalCell(value)).toBe(true);
expect(getTerminalCellId(value)).toBe("abc123");
expect(getTerminalCellCwd(value)).toBeNull();
});

it("round-trips a terminal id and cwd through the cell value", () => {
const value = makeTerminalCellValue("abc123", "/Users/me/my:repo");
expect(isTerminalCell(value)).toBe(true);
expect(getTerminalCellId(value)).toBe("abc123");
expect(getTerminalCellCwd(value)).toBe("/Users/me/my:repo");
});

it.each([
Expand Down
24 changes: 19 additions & 5 deletions packages/core/src/command-center/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,28 @@ export function isTerminalCell(value: string | null): value is string {
return value?.startsWith(TERMINAL_CELL_PREFIX) ?? false;
}

export function makeTerminalCellValue(terminalId: string): string {
return `${TERMINAL_CELL_PREFIX}${terminalId}`;
// terminalId is a base36 random string (no colon), so an optional cwd can be
// appended after a colon. cwd is URI-encoded, so it never contains a colon.
export function makeTerminalCellValue(
terminalId: string,
cwd?: string,
): string {
const base = `${TERMINAL_CELL_PREFIX}${terminalId}`;
return cwd ? `${base}:${encodeURIComponent(cwd)}` : base;
}

export function getTerminalCellId(value: string | null): string | null {
return isTerminalCell(value)
? value.slice(TERMINAL_CELL_PREFIX.length)
: null;
if (!isTerminalCell(value)) return null;
const rest = value.slice(TERMINAL_CELL_PREFIX.length);
const colon = rest.indexOf(":");
return colon === -1 ? rest : rest.slice(0, colon);
}

export function getTerminalCellCwd(value: string | null): string | null {
if (!isTerminalCell(value)) return null;
const rest = value.slice(TERMINAL_CELL_PREFIX.length);
const colon = rest.indexOf(":");
return colon === -1 ? null : decodeURIComponent(rest.slice(colon + 1));
}

export function getGridDimensions(preset: LayoutPreset): GridDimensions {
Expand Down
10 changes: 7 additions & 3 deletions packages/ui/src/features/command-center/commandCenterStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ interface CommandCenterStoreActions {
setActiveCell: (cellIndex: number | null) => void;
assignTask: (cellIndex: number, taskId: string) => void;
setBrainrotCell: (cellIndex: number) => void;
setTerminalCell: (cellIndex: number, terminalId: string) => void;
setTerminalCell: (
cellIndex: number,
terminalId: string,
cwd?: string,
) => void;
autofillCells: (taskIds: string[]) => void;
clearCell: (cellIndex: number) => void;
removeTaskById: (taskId: string) => void;
Expand Down Expand Up @@ -118,11 +122,11 @@ export const useCommandCenterStore = create<CommandCenterStore>()(
};
}),

setTerminalCell: (cellIndex, terminalId) =>
setTerminalCell: (cellIndex, terminalId, cwd) =>
set((state) => {
if (cellIndex < 0 || cellIndex >= state.cells.length) return state;
const cells = [...state.cells];
cells[cellIndex] = makeTerminalCellValue(terminalId);
cells[cellIndex] = makeTerminalCellValue(terminalId, cwd);
return {
cells,
activeTaskId: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ function EmptyCell({ cellIndex }: { cellIndex: number }) {
setBrainrotCell(cellIndex);
}, [layout, cells, setBrainrotCell, cellIndex]);

const handleNewTerminal = useCallback(() => {
setTerminalCell(cellIndex, secureRandomString(8));
}, [setTerminalCell, cellIndex]);
const handleNewTerminal = useCallback(
(cwd?: string) => {
setTerminalCell(cellIndex, secureRandomString(8), cwd);
},
[setTerminalCell, cellIndex],
);

const handleTaskCreated = useCallback(
(task: Task) => {
Expand Down Expand Up @@ -279,13 +282,15 @@ function BrainrotCell({ cellIndex }: { cellIndex: number }) {
function TerminalCell({
cellIndex,
terminalId,
terminalCwd,
}: {
cellIndex: number;
terminalId: string;
terminalCwd: string | null;
}) {
const clearCell = useCommandCenterStore((s) => s.clearCell);
const { getRecentFolders, getFolderDisplayName } = useFolders();
const cwd = getRecentFolders(1)[0]?.path;
const cwd = terminalCwd ?? getRecentFolders(1)[0]?.path;
const folderName = cwd ? getFolderDisplayName(cwd) : null;
const stateKey = getTerminalCellStateKey(terminalId);

Expand Down Expand Up @@ -413,7 +418,11 @@ export function CommandCenterPanel({

if (cell.terminalId) {
return (
<TerminalCell cellIndex={cell.cellIndex} terminalId={cell.terminalId} />
<TerminalCell
cellIndex={cell.cellIndex}
terminalId={cell.terminalId}
terminalCwd={cell.terminalCwd}
/>
);
}

Expand Down
Loading
Loading