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

Manager.ts

152 lines4.3 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 TerminalRestartInput,
16 TerminalSessionSnapshot,
17 TerminalWriteInput,
18} from "@t3tools/contracts";
19import { Effect, Schema, ServiceMap } from "effect";
20​
21export class TerminalCwdError extends Schema.TaggedErrorClass<TerminalCwdError>()(
22 "TerminalCwdError",
23 {
24 cwd: Schema.String,
25 reason: Schema.Literals(["notFound", "notDirectory", "statFailed"]),
26 cause: Schema.optional(Schema.Defect),
27 },
28) {
29 override get message() {
30 if (this.reason === "notDirectory") {
31 return `Terminal cwd is not a directory: ${this.cwd}`;
32 }
33 if (this.reason === "notFound") {
34 return `Terminal cwd does not exist: ${this.cwd}`;
35 }
36 const causeMessage =
37 this.cause && typeof this.cause === "object" && "message" in this.cause
38 ? this.cause.message
39 : undefined;
40 return causeMessage
41 ? `Failed to access terminal cwd: ${this.cwd} (${causeMessage})`
42 : `Failed to access terminal cwd: ${this.cwd}`;
43 }
45​
46export class TerminalHistoryError extends Schema.TaggedErrorClass<TerminalHistoryError>()(
47 "TerminalHistoryError",
48 {
49 operation: Schema.Literals(["read", "truncate", "migrate"]),
50 threadId: Schema.String,
51 terminalId: Schema.String,
52 cause: Schema.optional(Schema.Defect),
53 },
54) {
55 override get message() {
56 return `Failed to ${this.operation} terminal history for thread: ${this.threadId}, terminal: ${this.terminalId}`;
57 }
59​
60export class TerminalSessionLookupError extends Schema.TaggedErrorClass<TerminalSessionLookupError>()(
61 "TerminalSessionLookupError",
62 {
63 threadId: Schema.String,
64 terminalId: Schema.String,
65 },
66) {
67 override get message() {
68 return `Unknown terminal thread: ${this.threadId}, terminal: ${this.terminalId}`;
69 }
71​
72export class TerminalNotRunningError extends Schema.TaggedErrorClass<TerminalNotRunningError>()(
73 "TerminalNotRunningError",
74 {
75 threadId: Schema.String,
76 terminalId: Schema.String,
77 },
78) {
79 override get message() {
80 return `Terminal is not running for thread: ${this.threadId}, terminal: ${this.terminalId}`;
81 }
83​
84export const TerminalError = Schema.Union([
85 TerminalCwdError,
86 TerminalHistoryError,
87 TerminalSessionLookupError,
88 TerminalNotRunningError,
89]);
90export type TerminalError = typeof TerminalError.Type;
91​
92/**
93 * TerminalManagerShape - Service API for terminal session lifecycle operations.
94 */
95export interface TerminalManagerShape {
96 /**
97 * Open or attach to a terminal session.
98 *
99 * Reuses an existing session for the same thread/terminal id and restores
100 * persisted history on first open.
101 */
102 readonly open: (
103 input: TerminalOpenInput,
104 ) => Effect.Effect<TerminalSessionSnapshot, TerminalError>;
105​
106 /**
107 * Write input bytes to a terminal session.
108 */
109 readonly write: (input: TerminalWriteInput) => Effect.Effect<void, TerminalError>;
110​
111 /**
112 * Resize the PTY backing a terminal session.
113 */
114 readonly resize: (input: TerminalResizeInput) => Effect.Effect<void, TerminalError>;
115​
116 /**
117 * Clear terminal output history.
118 */
119 readonly clear: (input: TerminalClearInput) => Effect.Effect<void, TerminalError>;
120​
121 /**
122 * Restart a terminal session in place.
123 *
124 * Always resets history before spawning the new process.
125 */
126 readonly restart: (
127 input: TerminalRestartInput,
128 ) => Effect.Effect<TerminalSessionSnapshot, TerminalError>;
129​
130 /**
131 * Close an active terminal session.
132 *
133 * When `terminalId` is omitted, closes all sessions for the thread.
134 */
135 readonly close: (input: TerminalCloseInput) => Effect.Effect<void, TerminalError>;
136​
137 /**
138 * Subscribe to terminal runtime events with a direct callback.
139 *
140 * Returns an unsubscribe function.
141 */
142 readonly subscribe: (
143 listener: (event: TerminalEvent) => Effect.Effect<void>,
144 ) => Effect.Effect<() => void>;
146​
147/**
148 * TerminalManager - Service tag for terminal session orchestration.
149 */
150export class TerminalManager extends ServiceMap.Service<TerminalManager, TerminalManagerShape>()(
151 "t3/terminal/Services/Manager/TerminalManager",
152) {}