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

Manager.ts

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