UNPKG

@code-yeongyu/senpi

Version:

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

115 lines • 4.38 kB
export async function openHeadlessPresence(options) { const { ledger, placement } = options; const hands = new Map(); const pauses = new Map(); const held = new Map(); const offline = new Set(); const listeners = new Set(); let changed = Promise.withResolvers(); let serial = Promise.resolve(); const notify = (sessionId) => { changed.resolve(); changed = Promise.withResolvers(); for (const listener of [...listeners]) listener(sessionId); }; const remember = (pause) => { const open = pauses.get(pause.sessionId) ?? new Map(); open.set(pause.machine, pause); pauses.set(pause.sessionId, open); }; for (const pause of await ledger.openPauses()) { remember(pause); offline.add(pause.machine); } /** Presence operations run one at a time, so the in-memory view follows the durable order. */ const serialized = (operation) => { const run = serial.then(operation); serial = run.catch(() => undefined); return run; }; const roleOf = (sessionId, machine) => { if (machine === placement) return "owner"; return hands.get(sessionId)?.includes(machine) === true ? "hand" : undefined; }; const pauseOn = async (sessionId, machine) => { const role = roleOf(sessionId, machine); if (role === undefined || pauses.get(sessionId)?.has(machine) === true) return; remember(await ledger.pause(sessionId, machine, role)); notify(sessionId); }; const blocked = (sessionId, work) => { const open = pauses.get(sessionId); if (open === undefined) return false; return work === "tools" ? open.size > 0 : open.has(placement); }; const adjustHeld = (sessionId, delta) => { held.set(sessionId, (held.get(sessionId) ?? 0) + delta); notify(sessionId); }; return { placement, track: (sessionId, sessionHands) => serialized(async () => { hands.set(sessionId, [...sessionHands]); for (const machine of offline) await pauseOn(sessionId, machine); }), machineOffline: (machine) => serialized(async () => { offline.add(machine); for (const sessionId of hands.keys()) await pauseOn(sessionId, machine); }), machineOnline: (machine) => serialized(async () => { offline.delete(machine); for (const [sessionId, open] of pauses) { if (!open.has(machine)) continue; await ledger.resume(sessionId, machine); open.delete(machine); if (open.size === 0) pauses.delete(sessionId); notify(sessionId); } }), view: (sessionId) => { const open = [...(pauses.get(sessionId)?.values() ?? [])]; return { placement, state: pauses.get(sessionId)?.has(placement) === true ? "offline" : "online", paused: open.length > 0, waitingOn: open .map(({ machine, role }) => ({ machine, role })) .sort((a, b) => a.machine.localeCompare(b.machine)), held: held.get(sessionId) ?? 0, }; }, ready: async (sessionId, work, signal) => { if (!blocked(sessionId, work) || signal?.aborted === true) return; adjustHeld(sessionId, 1); const aborted = Promise.withResolvers(); let stopped = false; const onAbort = () => { stopped = true; aborted.resolve(); }; signal?.addEventListener("abort", onAbort, { once: true }); try { while (!stopped && blocked(sessionId, work)) await Promise.race([changed.promise, aborted.promise]); } finally { signal?.removeEventListener("abort", onAbort); adjustHeld(sessionId, -1); } }, subscribe: (listener) => { listeners.add(listener); return () => listeners.delete(listener); }, }; } //# sourceMappingURL=presence.js.map