@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
89 lines • 3.16 kB
JavaScript
import { loadPty } from "./pty.lazy.js";
const SCREEN_SCROLLBACK = 1000;
class InProcessTerminal {
constructor(id, session, screenModel) {
this.exitWaiters = new Set();
const pid = session.pid;
if (pid === undefined)
throw new Error("terminal session started without a pid");
this.id = id;
this.pid = pid;
this.session = session;
this.screenModel = screenModel;
session.onData((chunk) => {
void this.screenModel.feed(chunk);
});
void session.waitExit().then((exit) => this.settle({ exitCode: exit.exitCode, signal: exit.signal }), () => this.settle({ exitCode: null, signal: null }));
}
settle(exit) {
if (this.exit !== undefined)
return;
this.exit = exit;
for (const waiter of this.exitWaiters)
waiter(exit);
this.exitWaiters.clear();
}
onData(listener) {
return this.session.onData(listener);
}
onExit(listener) {
const exit = this.exit;
if (exit !== undefined) {
queueMicrotask(() => listener(exit));
return () => { };
}
this.exitWaiters.add(listener);
return () => this.exitWaiters.delete(listener);
}
write(bytes) {
this.session.write(bytes);
}
resize(cols, rows) {
this.session.resize(cols, rows);
void this.screenModel.resize(cols, rows);
}
signal(signal) {
this.session.kill(signal);
}
async screen(history = 0) {
await this.screenModel.flush();
const snapshot = this.screenModel.snapshot();
const lines = [
...snapshot.scrollback.slice(Math.max(0, snapshot.scrollback.length - history)),
...snapshot.visibleGrid,
];
return lines.join("\n").replace(/\s+$/, "");
}
}
/**
* A holder link whose terminals live in this process, over the existing pty package (native PTY, Bun terminal or
* pipe fallback). Stand-in for the separate holder process: terminals end when this process ends.
*
* pi-pty loads on the first spawn (`./pty.lazy.ts`), so importing this module keeps `@xterm/headless` off the graph.
*/
export function createInProcessHolderLink() {
const live = new Set();
return {
async spawn(req) {
const { createTerminalSession, TerminalScreen } = await loadPty();
const session = createTerminalSession({
command: req.command,
args: req.args,
cwd: req.cwd,
env: req.env,
cols: req.cols,
rows: req.rows,
});
const screen = new TerminalScreen({ cols: req.cols, rows: req.rows, scrollback: SCREEN_SCROLLBACK });
const terminal = new InProcessTerminal(req.id, session, screen);
live.add(terminal);
terminal.onExit(() => live.delete(terminal));
return terminal;
},
async close() {
for (const terminal of live)
terminal.signal("SIGKILL");
},
};
}
//# sourceMappingURL=in-process-holder.js.map