UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

107 lines (106 loc) • 4.46 kB
import { O as validateAgentRunDelegatedAuthority, l as getAgentRunContext } from "./agent-run-registry-D7vkrqFT.mjs"; import { r as getAgentRunLifecycleGeneration } from "./agent-run-registry-state-BFfw9a4W.mjs"; //#region src/infra/agent-run-terminal-writes.ts const terminalWrites = /* @__PURE__ */ new WeakMap(); const operationalTerminalWrites = /* @__PURE__ */ new WeakMap(); function prepareTerminalWrites(e, t) { let n = terminalWrites.get(e); const r = operationalTerminalWrites.get(t); if (r && n !== r || e.delegatedAuthority && e.delegatedAuthority.operationalRunInstance !== t) throw new Error("Terminal write settlement owner changed"); if (!n || n.instance !== t) { n = { owner: e, instance: t, pending: /* @__PURE__ */ new Set() }; terminalWrites.set(e, n); } operationalTerminalWrites.set(t, n); return n; } /** Register settlement before a projected turn can fail prior to runtime admission. */ function bindAgentRunTerminalWriteSettlement(e) { const t = getAgentRunContext(e.runId); if (t) prepareTerminalWrites(t, e); } function trackTerminalWrite(e, t) { e.pending.add(t); const settled = () => e.pending.delete(t); t.then(settled, settled); } /** Capture accepted-work settlement independently of permission to perform a write. */ function captureAgentRunTerminalPersistence(e, t = getAgentRunContext(e)) { const n = captureAgentRunTerminalWriteContext(e, t); const r = t ? terminalWrites.get(t) : void 0; return { writeContext: n, track: n?.track ?? (r ? (e) => trackTerminalWrite(r, e) : void 0) }; } /** Bind a prepared runtime's write context to its exact live operational owner. */ function bindAgentRunTerminalWriteContext(e, t) { const n = getAgentRunContext(e.operationalRunInstance.runId); if (n?.delegatedAuthority !== e || !validateAgentRunDelegatedAuthority(e)) throw new Error("Terminal write owner is no longer active"); const r = prepareTerminalWrites(n, e.operationalRunInstance); r.context = t; } /** * Run a transcript write that a live run produced outside its own async context (its message * tool's delivery mirror) inside the run's bound write context, so the run's CLI history writer * covers the row like the run's other rows. Without a current binding the write runs as before. */ function runInAgentRunWriteContext(e, t) { const n = e ? getAgentRunContext(e) : void 0; const r = n ? terminalWrites.get(n) : void 0; const i = n?.delegatedAuthority?.operationalRunInstance === r?.instance ? r?.context : void 0; return i ? i.run(t) : t(); } /** A new fallback candidate cannot borrow the preceding runtime's account context. */ function clearAgentRunTerminalWriteContext(e) { const t = getAgentRunContext(e.runId); const n = t ? terminalWrites.get(t) : void 0; if (t && n?.instance === e) { const r = { ...n, context: void 0 }; terminalWrites.set(t, r); operationalTerminalWrites.set(e, r); } } /** Capture before async session resolution; a replaced candidate revokes this exact capture. */ function captureAgentRunTerminalWriteContext(e, t) { if (!t?.lifecycleGeneration) return; const refuse = () => { throw new Error("Terminal write owner changed before commit"); }; const n = t.delegatedAuthority; const r = n ? prepareTerminalWrites(t, n.operationalRunInstance) : terminalWrites.get(t); if (!r) return; const i = t.lifecycleGeneration; const a = r; const o = a.context; const assertCurrent = () => { const n = getAgentRunContext(e); if (getAgentRunLifecycleGeneration() !== i || n && n !== t || terminalWrites.get(t) !== a || a.context !== o || t.delegatedAuthority && t.delegatedAuthority.operationalRunInstance !== a.instance) refuse(); }; return { assertCurrent, run: (e) => { assertCurrent(); return o ? o.run(e) : e(); }, track: (e) => trackTerminalWrite(a, e) }; } /** Join accepted terminal writes, then retire their binding; execution close stays immediate. */ async function drainAgentRunTerminalWrites(e) { const t = operationalTerminalWrites.get(e); if (t) { while (t.pending.size > 0) await Promise.allSettled(t.pending); if (terminalWrites.get(t.owner)?.instance === e) terminalWrites.delete(t.owner); operationalTerminalWrites.delete(e); } } //#endregion export { drainAgentRunTerminalWrites as a, clearAgentRunTerminalWriteContext as i, bindAgentRunTerminalWriteSettlement as n, runInAgentRunWriteContext as o, captureAgentRunTerminalPersistence as r, bindAgentRunTerminalWriteContext as t };