@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
101 lines • 3.52 kB
JavaScript
import { TuiAltScreen } from "@earendil-works/pi-tui";
import { getAgentDir } from "../../config.js";
import { markModeCrashHandler } from "../../core/bootstrap-crash-listeners.js";
import { AttachedTui, validateAttachTerminal } from "./attach.js";
import { initTheme, stopThemeWatcher } from "./theme/theme.js";
import { createInteractiveTui } from "./tui-renderer.js";
/** Lazy CLI presentation boundary, parallel to the ordinary interactive mode. */
export async function runAttachedTui(options) {
validateAttachTerminal(process.stdin.isTTY, process.stdout.isTTY);
initTheme(options.theme);
const ui = createInteractiveTui({
tuiMode: options.tuiMode ?? "regular",
showHardwareCursor: false,
logDirectory: getAgentDir(),
});
let restored = false;
const restore = () => {
if (restored)
return;
restored = true;
try {
ui.stop();
}
finally {
stopThemeWatcher();
}
};
// The one crash path: it never defers to another listener. It restores the terminal, then the
// process dies non-zero with the original error. Marked, so the bootstrap listener defers to it.
const crash = (error) => {
try {
restore();
}
finally {
console.error(error);
process.exit(1);
}
};
markModeCrashHandler(crash);
process.prependListener("uncaughtException", crash);
process.prependListener("unhandledRejection", crash);
let finish = () => { };
const done = new Promise((resolve) => {
finish = resolve;
});
let interrupt = () => { };
const interrupted = new Promise((resolve) => {
interrupt = () => resolve("interrupted");
});
const view = new AttachedTui(ui, finish);
// A signal ends the presentation at once, also while connect() still waits on the endpoint. Once it
// is leaving, the release can wait on a silent endpoint: a further signal restores the terminal
// (idempotently) and exits at once, non-zero.
let leaving = false;
const stop = (exitCode) => {
if (leaving) {
restore();
process.exit(exitCode);
}
leaving = true;
finish();
interrupt();
};
const onSigterm = () => stop(143);
const onSigint = () => stop(130);
process.on("SIGTERM", onSigterm);
process.on("SIGINT", onSigint);
ui.addChild(view);
if (ui instanceof TuiAltScreen)
ui.setLayoutRoot(view.layoutRoot);
ui.setFocus(view);
ui.start();
try {
const outcome = await Promise.race([
view.connect(options.address, options.session, options.startup).then(() => "connected"),
interrupted,
]);
if (outcome === "connected")
await done;
}
finally {
leaving = true;
try {
// The terminal comes back first: the release below can wait out its whole deadline.
restore();
}
finally {
// Even if restoring throws, the release is sent and the listeners come off.
try {
await view.close();
}
finally {
process.off("SIGTERM", onSigterm);
process.off("SIGINT", onSigint);
process.off("uncaughtException", crash);
process.off("unhandledRejection", crash);
}
}
}
}
//# sourceMappingURL=run-attached-tui.js.map