UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

93 lines • 4.71 kB
/** * The headless engine: a multi-session RPC host with no UI that OWNS its sessions and their loops. * Clients and other sessions attach and detach over RPC; a detach never ends a session or a running * turn, and the engine does not exit for being empty. It needs only the engine's own configuration * (an agent dir, a cwd, a runtime factory) and reads nothing product-specific. */ import { posix, win32 } from "node:path"; import { resolveSocketTransportAddress } from "../rpc/socket-transport.js"; import { createHandSelection, } from "./hand-selection.js"; import { gatePresence } from "./presence-gates.js"; /** The headless engine's socket under `<agentDir>/rpc/`, apart from the shared host's `rpc.sock`. */ const HEADLESS_SOCKET_FILE = "headless.sock"; export function headlessSocketPath(agentDir, platform = process.platform) { return (platform === "win32" ? win32 : posix).join(agentDir, "rpc", HEADLESS_SOCKET_FILE); } /** The address a client dials: the socket path on POSIX, its named pipe on Windows. */ export function headlessEndpoint(agentDir, platform = process.platform, secret) { return resolveSocketTransportAddress(headlessSocketPath(agentDir, platform), platform, secret); } export function headlessHostOptions(options) { const { presence, hands, ...host } = options; const runtimes = new Map(); const handSelection = hands === undefined ? undefined : createHandSelection(hands, presence === undefined ? {} : presenceHooks(presence, runtimes)); // Presence gates the session first; the hand selection then installs every host through the same gate, so a // chosen hand's calls wait for presence and `null` gives back the gated host the session started with. const gated = presence === undefined ? host.createRuntime : presenceRuntime(host.createRuntime, presence); return { ...host, sessionOwner: "machine", listen: options.listen ?? `unix://${headlessSocketPath(options.agentDir)}`, createRuntime: handSelection === undefined ? gated : handRuntime(gated, handSelection, runtimes), ...(presence === undefined ? {} : { sessionPresence: presence.presence }), ...(handSelection === undefined ? {} : { handSelection }), }; } /** The presence gate for a host the hand selection installs, and the hands presence tracks after each choice. */ function presenceHooks(binding, runtimes) { // The presence update for each session's latest choice: a tool batch waits for it before asking presence, so it // is gated on the hand it will run on, and a failed update fails that batch instead of going unhandled. const tracking = new Map(); return { gate: (sessionId, host) => ({ execute: async (call, options) => { await tracking.get(sessionId); await binding.presence.ready(sessionId, "tools", options.signal); return host.execute(call, options); }, }), onChange: (sessionId, device) => { const runtime = runtimes.get(sessionId); const sessionHands = device !== null ? [device] : runtime === undefined ? [] : binding.handsOf(runtime); const update = binding.presence.track(sessionId, sessionHands); update.catch(() => { }); tracking.set(sessionId, update); }, onRelease: (sessionId) => { tracking.delete(sessionId); }, }; } function handRuntime(createRuntime, selection, runtimes) { const restored = async (input) => { const runtime = await createRuntime(input); const { session } = runtime; const sessionId = session.sessionId; runtimes.set(sessionId, runtime); selection.restore(sessionId, session); // A session that ends releases its link to the hand; a later runtime of the same id starts clean. const dispose = session.dispose.bind(session); session.dispose = () => { if (runtimes.get(sessionId) === runtime) { runtimes.delete(sessionId); selection.release(sessionId); } dispose(); }; return runtime; }; return Object.assign(restored, createRuntime); } function presenceRuntime(createRuntime, binding) { const tracked = async (input) => { const runtime = await createRuntime(input); const { session } = runtime; await binding.presence.track(session.sessionId, binding.handsOf(runtime)); gatePresence(session.agent, binding.presence, session.sessionId); return runtime; }; return Object.assign(tracked, createRuntime); } //# sourceMappingURL=headless-host.js.map