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

Manager.ts

126 lines3.2 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 TerminalCwdError,
14 TerminalError,
15 TerminalHistoryError,
16 TerminalNotRunningError,
17 TerminalOpenInput,
18 TerminalResizeInput,
19 TerminalRestartInput,
20 TerminalSessionSnapshot,
21 TerminalSessionLookupError,
22 TerminalSessionStatus,
23 TerminalWriteInput,
24} from "@t3tools/contracts";
25import { PtyProcess } from "./PTY";
26import { Effect, ServiceMap } from "effect";
27​
28export {
29 TerminalCwdError,
30 TerminalError,
31 TerminalHistoryError,
32 TerminalNotRunningError,
33 TerminalSessionLookupError,
34};
35​
36export interface TerminalSessionState {
37 threadId: string;
38 terminalId: string;
39 cwd: string;
40 status: TerminalSessionStatus;
41 pid: number | null;
42 history: string;
43 pendingHistoryControlSequence: string;
44 exitCode: number | null;
45 exitSignal: number | null;
46 updatedAt: string;
47 cols: number;
48 rows: number;
49 process: PtyProcess | null;
50 unsubscribeData: (() => void) | null;
51 unsubscribeExit: (() => void) | null;
52 hasRunningSubprocess: boolean;
53 runtimeEnv: Record<string, string> | null;
55​
56export interface ShellCandidate {
57 shell: string;
58 args?: string[];
60​
61export interface TerminalStartInput extends TerminalOpenInput {
62 cols: number;
63 rows: number;
65​
66/**
67 * TerminalManagerShape - Service API for terminal session lifecycle operations.
68 */
69export interface TerminalManagerShape {
70 /**
71 * Open or attach to a terminal session.
72 *
73 * Reuses an existing session for the same thread/terminal id and restores
74 * persisted history on first open.
75 */
76 readonly open: (
77 input: TerminalOpenInput,
78 ) => Effect.Effect<TerminalSessionSnapshot, TerminalError>;
79​
80 /**
81 * Write input bytes to a terminal session.
82 */
83 readonly write: (input: TerminalWriteInput) => Effect.Effect<void, TerminalError>;
84​
85 /**
86 * Resize the PTY backing a terminal session.
87 */
88 readonly resize: (input: TerminalResizeInput) => Effect.Effect<void, TerminalError>;
89​
90 /**
91 * Clear terminal output history.
92 */
93 readonly clear: (input: TerminalClearInput) => Effect.Effect<void, TerminalError>;
94​
95 /**
96 * Restart a terminal session in place.
97 *
98 * Always resets history before spawning the new process.
99 */
100 readonly restart: (
101 input: TerminalRestartInput,
102 ) => Effect.Effect<TerminalSessionSnapshot, TerminalError>;
103​
104 /**
105 * Close an active terminal session.
106 *
107 * When `terminalId` is omitted, closes all sessions for the thread.
108 */
109 readonly close: (input: TerminalCloseInput) => Effect.Effect<void, TerminalError>;
110​
111 /**
112 * Subscribe to terminal runtime events with a direct callback.
113 *
114 * Returns an unsubscribe function.
115 */
116 readonly subscribe: (
117 listener: (event: TerminalEvent) => Effect.Effect<void>,
118 ) => Effect.Effect<() => void>;
120​
121/**
122 * TerminalManager - Service tag for terminal session orchestration.
123 */
124export class TerminalManager extends ServiceMap.Service<TerminalManager, TerminalManagerShape>()(
125 "t3/terminal/Services/Manager/TerminalManager",
126) {}