Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Manager.ts

117 lines3.0 KB

1/**
2 * TerminalManager - Terminal session orchestration service interface.
3 *
4 * Owns terminal lifecycle operations, output fanout, and session state
5 * transitions for thread-scoped terminals.
6 *
7 * @module TerminalManager
8 */
9import {
10 TerminalClearInput,
11 TerminalCloseInput,
12 TerminalEvent,
13 TerminalOpenInput,
14 TerminalResizeInput,
15 TerminalSessionSnapshot,
16 TerminalSessionStatus,
17 TerminalWriteInput,
18} from "@t3tools/contracts";
19import { PtyProcess } from "./PTY";
20import { Effect, Schema, ServiceMap } from "effect";
21​
22export class TerminalError extends Schema.TaggedErrorClass<TerminalError>()("TerminalError", {
23 message: Schema.String,
24 cause: Schema.optional(Schema.Defect),
25}) {}
26​
27export interface TerminalSessionState {
28 threadId: string;
29 terminalId: string;
30 cwd: string;
31 status: TerminalSessionStatus;
32 pid: number | null;
33 history: string;
34 exitCode: number | null;
35 exitSignal: number | null;
36 updatedAt: string;
37 cols: number;
38 rows: number;
39 process: PtyProcess | null;
40 unsubscribeData: (() => void) | null;
41 unsubscribeExit: (() => void) | null;
42 hasRunningSubprocess: boolean;
43 runtimeEnv: Record<string, string> | null;
45​
46export interface ShellCandidate {
47 shell: string;
48 args?: string[];
50​
51export interface TerminalStartInput extends TerminalOpenInput {
52 cols: number;
53 rows: number;
55​
56/**
57 * TerminalManagerShape - Service API for terminal session lifecycle operations.
58 */
59export interface TerminalManagerShape {
60 /**
61 * Open or attach to a terminal session.
62 *
63 * Reuses an existing session for the same thread/terminal id and restores
64 * persisted history on first open.
65 */
66 readonly open: (
67 input: TerminalOpenInput,
68 ) => Effect.Effect<TerminalSessionSnapshot, TerminalError>;
69​
70 /**
71 * Write input bytes to a terminal session.
72 */
73 readonly write: (input: TerminalWriteInput) => Effect.Effect<void, TerminalError>;
74​
75 /**
76 * Resize the PTY backing a terminal session.
77 */
78 readonly resize: (input: TerminalResizeInput) => Effect.Effect<void, TerminalError>;
79​
80 /**
81 * Clear terminal output history.
82 */
83 readonly clear: (input: TerminalClearInput) => Effect.Effect<void, TerminalError>;
84​
85 /**
86 * Restart a terminal session in place.
87 *
88 * Always resets history before spawning the new process.
89 */
90 readonly restart: (
91 input: TerminalOpenInput,
92 ) => Effect.Effect<TerminalSessionSnapshot, TerminalError>;
93​
94 /**
95 * Close an active terminal session.
96 *
97 * When `terminalId` is omitted, closes all sessions for the thread.
98 */
99 readonly close: (input: TerminalCloseInput) => Effect.Effect<void, TerminalError>;
100​
101 /**
102 * Subscribe to terminal runtime events.
103 */
104 readonly subscribe: (listener: (event: TerminalEvent) => void) => Effect.Effect<() => void>;
105​
106 /**
107 * Dispose all managed terminal resources.
108 */
109 readonly dispose: Effect.Effect<void>;
111​
112/**
113 * TerminalManager - Service tag for terminal session orchestration.
114 */
115export class TerminalManager extends ServiceMap.Service<TerminalManager, TerminalManagerShape>()(
116 "t3/terminal/Services/Manager/TerminalManager",
117) {}