/** * TerminalManager - Terminal session orchestration service interface. * * Owns terminal lifecycle operations, output fanout, and session state * transitions for thread-scoped terminals. * * @module TerminalManager */ import { TerminalClearInput, TerminalCloseInput, TerminalEvent, TerminalCwdError, TerminalError, TerminalHistoryError, TerminalNotRunningError, TerminalOpenInput, TerminalResizeInput, TerminalRestartInput, TerminalSessionSnapshot, TerminalSessionLookupError, TerminalSessionStatus, TerminalWriteInput, } from "@t3tools/contracts"; import type { PtyProcess } from "./PTY.ts"; import * as Effect from "effect/Effect"; import * as Context from "effect/Context"; export { TerminalCwdError, TerminalError, TerminalHistoryError, TerminalNotRunningError, TerminalSessionLookupError, }; export interface TerminalSessionState { threadId: string; terminalId: string; cwd: string; worktreePath: string | null; status: TerminalSessionStatus; pid: number | null; history: string; pendingHistoryControlSequence: string; exitCode: number | null; exitSignal: number | null; updatedAt: string; cols: number; rows: number; process: PtyProcess | null; unsubscribeData: (() => void) | null; unsubscribeExit: (() => void) | null; hasRunningSubprocess: boolean; runtimeEnv: Record | null; } export interface ShellCandidate { shell: string; args?: string[]; } export interface TerminalStartInput extends TerminalOpenInput { cols: number; rows: number; } /** * TerminalManagerShape - Service API for terminal session lifecycle operations. */ export interface TerminalManagerShape { /** * Open or attach to a terminal session. * * Reuses an existing session for the same thread/terminal id and restores * persisted history on first open. */ readonly open: ( input: TerminalOpenInput, ) => Effect.Effect; /** * Write input bytes to a terminal session. */ readonly write: (input: TerminalWriteInput) => Effect.Effect; /** * Resize the PTY backing a terminal session. */ readonly resize: (input: TerminalResizeInput) => Effect.Effect; /** * Clear terminal output history. */ readonly clear: (input: TerminalClearInput) => Effect.Effect; /** * Restart a terminal session in place. * * Always resets history before spawning the new process. */ readonly restart: ( input: TerminalRestartInput, ) => Effect.Effect; /** * Close an active terminal session. * * When `terminalId` is omitted, closes all sessions for the thread. */ readonly close: (input: TerminalCloseInput) => Effect.Effect; /** * Subscribe to terminal runtime events with a direct callback. * * Returns an unsubscribe function. */ readonly subscribe: ( listener: (event: TerminalEvent) => Effect.Effect, ) => Effect.Effect<() => void>; } /** * TerminalManager - Service tag for terminal session orchestration. */ export class TerminalManager extends Context.Service()( "t3/terminal/Services/Manager/TerminalManager", ) {}