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

main
ts 54 lines 1.7 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Effect from "effect/Effect";
3 import * as Schema from "effect/Schema";
4
5 export class PtySpawnError extends Schema.TaggedError<PtySpawnError>()("PtySpawnError", {
6 adapter: Schema.String,
7 shell: Schema.optional(Schema.String),
8 attemptedShells: Schema.optional(Schema.Array(Schema.String)),
9 cause: Schema.optional(Schema.Defect()),
10 }) {
11 override get message(): string {
12 const shell = this.shell === undefined ? "" : ` '${this.shell}'`;
13 const attemptedShells =
14 this.attemptedShells === undefined || this.attemptedShells.length === 0
15 ? ""
16 : ` Tried shells: ${this.attemptedShells.join(", ")}.`;
17 return `Failed to spawn PTY process${shell} with ${this.adapter}.${attemptedShells}`;
18 }
19 }
20
21 export interface PtyExitEvent {
22 exitCode: number;
23 signal: number | null;
24 }
25
26 /** What a PTY receives for client terminal input, whose `binary` data is one byte per code unit. */
27 export function ptyInput(data: string, binary: boolean | undefined): string | Buffer {
28 return binary ? Buffer.from(data, "latin1") : data;
29 }
30
31 export interface PtyProcess {
32 readonly pid: number;
33 write(data: string | Buffer): void;
34 resize(cols: number, rows: number): void;
35 kill(signal?: string): void;
36 onData(callback: (data: string) => void): () => void;
37 onExit(callback: (event: PtyExitEvent) => void): () => void;
38 }
39
40 export interface PtySpawnInput {
41 shell: string;
42 args?: string[];
43 cwd: string;
44 cols: number;
45 rows: number;
46 env: NodeJS.ProcessEnv;
47 }
48
49 export class PtyAdapter extends Context.Service<
50 PtyAdapter,
51 {
52 readonly spawn: (input: PtySpawnInput) => Effect.Effect<PtyProcess, PtySpawnError>;
53 }
54 >()("@t3tools/shared/PtyAdapter") {}