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