UNPKG

@code-yeongyu/senpi

Version:

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

119 lines • 5.73 kB
import { AsyncLocalStorage } from "node:async_hooks"; export function createLoopHookCall(late) { const calls = new AsyncLocalStorage(); // The latest call each loop session started: a newer one means the loop acts on that session again. const latest = new Map(); return { current: () => { const call = calls.getStore(); return call?.live() === true ? call.facts : undefined; }, sender: () => calls.getStore()?.facts.sendMessage, writer: () => calls.getStore()?.facts, async run(binding, dispatch) { const { sessionId, idle } = binding; const token = {}; latest.set(sessionId, token); let live = true; let userMessagesSent = 0; const controller = new AbortController(); const effects = { sent: [], notices: [], entries: [], userMessages: [] }; const facts = Object.freeze({ sessionManager: readOnlySession(binding, { live: () => live, ownWork: () => live || calls.getStore()?.facts === facts, }), signal: controller.signal, isIdle: () => (live || binding.endsTurn !== true ? idle : latest.get(sessionId) === token), sendMessage: (message, options) => { const entry = options === undefined ? { message } : { message, options }; if (live) effects.sent.push(entry); else late(sessionId).sendMessage(message, options); }, notify: (message, type) => { const entry = type === undefined ? { message } : { message, type }; if (live) effects.notices.push(entry); else late(sessionId).notify(message, type); }, appendEntry: (customType, data) => { const entry = data === undefined ? { customType } : { customType, data }; if (live) effects.entries.push(entry); else late(sessionId).appendEntry(customType, data); }, sendUserMessage: (content, options) => { const entry = options === undefined ? { content } : { content, options }; userMessagesSent++; if (live) effects.userMessages.push(entry); else late(sessionId).sendUserMessage(content, options); }, // Only what this call (or its work) sent. hasPendingMessages: () => userMessagesSent > 0, }); try { return await calls.run({ live: () => live, facts }, () => dispatch(effects)); } finally { live = false; controller.abort(); } }, }; } /** * The deliberate refusal after a loop-hook call ended (option A): the loop session's identity (`getSessionId`, * `getSessionFile`, `getCwd`) is bound to that call only, so reading it from a kept context afterwards throws this * instead of answering. The session the call carried (`getLeafId`, `reduceHistory`) stays readable by the call's own * work (its async context: a Stop it held for background work), and refused from any other context. Every other member * keeps working: late writes, messages and notices go to the call's late route; the signal stays aborted; idle keeps * its value, except that after a call that ended the loop's turn it answers idle until the loop starts another call. */ export class LoopHookCallEndedError extends Error { constructor(member) { super(`this loop-hook call has ended; ${member} is bound to that call only`); this.name = "LoopHookCallEndedError"; this.member = member; } } function readOnlySession({ sessionId, cwd, carried }, gates) { const gated = (open) => (member, read) => () => { if (!open()) throw new LoopHookCallEndedError(`sessionManager.${member}`); return read(); }; const bound = gated(gates.live); const durable = gated(gates.ownWork); const unbound = (member) => () => { throw new Error(`sessionManager.${member} is loop-side: a loop-hook call on a hand carries only the session's id`); }; return Object.freeze({ getSessionId: bound("getSessionId", () => sessionId), getSessionFile: bound("getSessionFile", () => undefined), getCwd: bound("getCwd", () => cwd), getSessionDir: unbound("getSessionDir"), getLeafId: carried === undefined ? unbound("getLeafId") : durable("getLeafId", () => carried.leafId), getLeafEntry: unbound("getLeafEntry"), getEntry: unbound("getEntry"), getLabel: unbound("getLabel"), getBranch: unbound("getBranch"), getFullBranch: unbound("getFullBranch"), buildContextEntries: unbound("buildContextEntries"), buildSessionContext: unbound("buildSessionContext"), buildSessionProjection: unbound("buildSessionProjection"), getHeader: unbound("getHeader"), getEntries: unbound("getEntries"), reduceHistory: carried === undefined ? unbound("reduceHistory") : (reducer) => durable("reduceHistory", () => carried.handEntries.reduce(reducer.add, reducer.initial()))(), getTree: unbound("getTree"), getSessionName: unbound("getSessionName"), }); } //# sourceMappingURL=loop-hook-call.js.map