Skip to content

terminal

Install the required plugin:

Terminal window
tynd add terminal
import { terminal } from "@tynd/plugin-terminal/client";

Real PTY sessions. ConPTY on Windows, POSIX PTY elsewhere. Pair with xterm.js for a full interactive terminal UI.

interface TerminalSpawnOptions {
shell?: string; // default: $SHELL on Unix, %COMSPEC% on Windows
args?: string[];
cwd?: string;
env?: Record<string, string>;
cols?: number; // default 80
rows?: number; // default 24
}
const t = await terminal.spawn({ cols: 120, rows: 30 });
interface TerminalHandle {
id: number;
write(data: string | Uint8Array): Promise<void>;
resize(cols: number, rows: number): Promise<void>;
kill(): Promise<void>;
onData(handler: (chunk: Uint8Array) => void): () => void;
onExit(handler: (code: number | null) => void): () => void;
}

Streams raw PTY bytes (base64-encoded on the IPC channel; decoded to Uint8Array for you). Output that arrives before the first onData listener is buffered (up to 1 MiB, oldest chunks dropped first) and flushed to it.

t.onData((bytes) => xterm.write(bytes));

Fires once when the shell exits on its own. code is the exit code, or null when the host could not read the exit status. It does not fire after kill().

t.onExit((code) => console.log("shell exited:", code));
await t.write("ls -la\n");
await t.write(new Uint8Array([0x03])); // Ctrl+C

Call after the user resizes the terminal UI.

Force-close the session. Idempotent. The session’s onData and onExit listeners are removed and no exit event is delivered.

IDs of currently-open sessions.

const ids = await terminal.list();
import { Terminal } from "@xterm/xterm";
import { FitAddon } from "@xterm/addon-fit";
import { terminal } from "@tynd/plugin-terminal/client";
import "@xterm/xterm/css/xterm.css";
const xterm = new Terminal();
const fit = new FitAddon();
xterm.loadAddon(fit);
xterm.open(document.getElementById("terminal")!);
fit.fit();
const pty = await terminal.spawn({ cols: xterm.cols, rows: xterm.rows });
pty.onData((bytes) => xterm.write(bytes));
pty.onExit((code) => xterm.writeln(`\r\n[process exited: ${code ?? "killed"}]`));
xterm.onData((data) => pty.write(data));
new ResizeObserver(() => {
fit.fit();
void pty.resize(xterm.cols, xterm.rows);
}).observe(document.getElementById("terminal")!);
  • Output from the PTY is base64-encoded on the IPC channel.
  • Up to 64 concurrent sessions, each with its own id and reader thread. spawn rejects past that limit.
  • Sessions stay open across calls until the shell exits or kill() runs; the host closes the remaining ones on app exit.
  • process — one-shot exec with buffered output.
  • sidecar — run a bundled helper binary.