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
13 changes: 12 additions & 1 deletion apps/server/src/terminal/Layers/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DEFAULT_TERMINAL_ID,
type TerminalEvent,
type TerminalOpenInput,
type TerminalRestartInput,
} from "@t3tools/contracts";
import { afterEach, describe, expect, it } from "vitest";

Expand Down Expand Up @@ -134,6 +135,16 @@ function openInput(overrides: Partial<TerminalOpenInput> = {}): TerminalOpenInpu
};
}

function restartInput(overrides: Partial<TerminalRestartInput> = {}): TerminalRestartInput {
return {
threadId: "thread-1",
cwd: process.cwd(),
cols: 100,
rows: 24,
...overrides,
};
}

function historyLogName(threadId: string): string {
return `terminal_${Encoding.encodeBase64Url(threadId)}.log`;
}
Expand Down Expand Up @@ -361,7 +372,7 @@ describe("TerminalManager", () => {
firstProcess.emitData("before restart\n");
await waitFor(() => fs.existsSync(historyLogPath(logsDir)));

const snapshot = await manager.restart(openInput());
const snapshot = await manager.restart(restartInput());
expect(snapshot.history).toBe("");
expect(snapshot.status).toBe("running");
expect(ptyAdapter.spawnInputs).toHaveLength(2);
Expand Down
6 changes: 4 additions & 2 deletions apps/server/src/terminal/Layers/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TerminalCloseInput,
TerminalOpenInput,
TerminalResizeInput,
TerminalRestartInput,
TerminalWriteInput,
type TerminalEvent,
type TerminalSessionSnapshot,
Expand Down Expand Up @@ -37,6 +38,7 @@ const DEFAULT_OPEN_ROWS = 30;
const TERMINAL_ENV_BLOCKLIST = new Set(["PORT", "ELECTRON_RENDERER_PORT", "ELECTRON_RUN_AS_NODE"]);

const decodeTerminalOpenInput = Schema.decodeUnknownSync(TerminalOpenInput);
const decodeTerminalRestartInput = Schema.decodeUnknownSync(TerminalRestartInput);
const decodeTerminalWriteInput = Schema.decodeUnknownSync(TerminalWriteInput);
const decodeTerminalResizeInput = Schema.decodeUnknownSync(TerminalResizeInput);
const decodeTerminalClearInput = Schema.decodeUnknownSync(TerminalClearInput);
Expand Down Expand Up @@ -478,8 +480,8 @@ export class TerminalManagerRuntime extends EventEmitter<TerminalManagerEvents>
});
}

async restart(raw: TerminalOpenInput): Promise<TerminalSessionSnapshot> {
const input = decodeTerminalOpenInput(raw);
async restart(raw: TerminalRestartInput): Promise<TerminalSessionSnapshot> {
const input = decodeTerminalRestartInput(raw);
return this.runWithThreadLock(input.threadId, async () => {
await this.assertValidCwd(input.cwd);

Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/terminal/Services/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TerminalEvent,
TerminalOpenInput,
TerminalResizeInput,
TerminalRestartInput,
TerminalSessionSnapshot,
TerminalSessionStatus,
TerminalWriteInput,
Expand Down Expand Up @@ -88,7 +89,7 @@ export interface TerminalManagerShape {
* Always resets history before spawning the new process.
*/
readonly restart: (
input: TerminalOpenInput,
input: TerminalRestartInput,
) => Effect.Effect<TerminalSessionSnapshot, TerminalError>;

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/contracts/src/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
TerminalEvent,
TerminalOpenInput,
TerminalResizeInput,
TerminalRestartInput,
TerminalSessionSnapshot,
TerminalWriteInput,
} from "./terminal";
Expand Down Expand Up @@ -103,7 +104,7 @@ export interface NativeApi {
write: (input: TerminalWriteInput) => Promise<void>;
resize: (input: TerminalResizeInput) => Promise<void>;
clear: (input: TerminalClearInput) => Promise<void>;
restart: (input: TerminalOpenInput) => Promise<TerminalSessionSnapshot>;
restart: (input: TerminalRestartInput) => Promise<TerminalSessionSnapshot>;
close: (input: TerminalCloseInput) => Promise<void>;
onEvent: (callback: (event: TerminalEvent) => void) => () => void;
};
Expand Down
3 changes: 2 additions & 1 deletion packages/contracts/src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ export const TerminalClearInput = TerminalSessionInput;
export type TerminalClearInput = Schema.Codec.Encoded<typeof TerminalClearInput>;

export const TerminalRestartInput = Schema.Struct({
...TerminalThreadInput.fields,
...TerminalSessionInput.fields,
cwd: TrimmedNonEmptyStringSchema,
cols: TerminalColsSchema,
rows: TerminalRowsSchema,
env: Schema.optional(TerminalEnvSchema),
});
export type TerminalRestartInput = Schema.Codec.Encoded<typeof TerminalRestartInput>;

export const TerminalCloseInput = Schema.Struct({
...TerminalThreadInput.fields,
Expand Down