This directory contains the core logic of Conch.
conch.ts:ConchClass (Facade)- Provides the high-level API used by consumers.
- Integrates
ConchSessionandITerminalBackend, wrapping synchronization (Action Queue) and wait logic.
session.ts:ConchSessionClass- The controller that bridges the backend (process) and the frontend (xterm screen).
- Responsible for generating snapshots as "Facts", normalizing input, synchronizing resizes, etc.
types.ts: Common Type DefinitionsITerminalBackend(Interface for backends)ISnapshot(Type definition for snapshots), etc.
keymap.ts: Keymap Definitions- Mappings between key names used in
press/chordmethods and ANSI escape sequences.
- Mappings between key names used in
utils.ts: Wait & Locator UtilitieswaitForText,waitForStable: Functions to wait for screen state changes.cropText,findText: Functions to extract information from snapshots.encodeScriptForShell: Cross-platform (Linux/macOS) shell script injection helper.
backend/: Backend Adapters- Contains backend implementations (
LocalPty,DockerPty,SshPty).TmuxPtyis planned.
- Contains backend implementations (
index.ts: Entry Point- Exports the public API of the library.
Conch follows the design philosophy of separating "Facts" from "Interpretation". Core focuses on maintaining accurate screen state, while leaving semantic interpretation to Userland (test code or agents).
graph TD
subgraph Backend [Backend Adapters]
LP[LocalPty]
DP[DockerPty]
SP[SshPty]
TP[TmuxPty 🔜]
end
subgraph Core [Conch Core]
Facade[Conch Facade]
Session[ConchSession]
Xterm[xterm-headless]
end
subgraph Utils [Userland Tools]
Wait[Wait Utils]
Locator[Locator Utils]
Formatter[Formatter]
end
Human((Human / Telnet))
Agent((AI Agent))
%% Data Flow
LP -->|onData| Session
Session -->|write| Xterm
%% Output
Session -->|onOutput| Human
%% Input
Human -->|write| Facade
Agent -->|run/press| Facade
Facade -->|write| Session
%% Snapshot & Analysis
Agent -->|getSnapshot| Facade
Facade -->|getSnapshot| Session
Agent -.->|use| Wait
Agent -.->|use| Locator
Wait -.->|getSnapshot| Session
Locator -.->|text/meta| Session
- High-Level Control: Provides high-level flows like
run()andpressAndSnapshot()that combine action, waiting, and snapshotting. - Terminal State: Accurately reflects and maintains output from the backend in the xterm buffer.
- Terminal Query Auto-Responder: Intercepts terminal capability queries (DA1, DA2, CPR/DSR, DECRQM) from TUI applications (vim, less, nano, etc.) and writes standard responses back to the backend. Without this, xterm.js headless cannot answer these queries and TUI apps block on startup.
- Facts Provider: Provides "facts" such as cursor position and viewport information through snapshots, not just text.
- Input Normalization: Converts abstract operations like
press('Enter')into appropriate escape sequences and sends them to the backend. - Shell Integration (OSC 133): Injects scripts that emit full A/B/C/D markers. Bash uses a DEBUG trap for the C marker; PowerShell uses a PSReadLine Enter handler. The C/D boundary enables deterministic output extraction in
run(). - Consistency: Guarantees timing consistency between asynchronous write operations and snapshot acquisition via
drain().
- Interpretation: Application-specific semantics (e.g., "this is the selected line", "an error is displayed") are handled in Userland, not Core.
- Formatting: Decorations such as adding line numbers or coloring are done through
formatter.