UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

205 lines (204 loc) • 7.44 kB
import { T as runWithDiagnosticTraceContext, b as getActiveDiagnosticTraceContext } from "./diagnostics-timeline-CSwiG24r.mjs"; import { r as scheduleAbsoluteDeadline } from "./absolute-deadline-kK4DcdPs.mjs"; import { a as captureGatewayRootWorkAdmissionContinuationScope } from "./gateway-work-admission-79pid1hQ.mjs"; import { t as NODE_INVOKE_PAIRING_CHANGED_ABORT } from "./node-registry-private-token-ChofAGxk.mjs"; //#region src/gateway/node-registry.invoke-stream.ts /** A node may emit this only before invoking a handler or sending any progress. */ const NODE_INVOKE_NOT_READY = "NODE_NOT_READY"; const MAX_PENDING_PROGRESS_CHUNKS = 128; const MAX_INVOKE_INPUT_BYTES = 16384; var NodeInvokeNotPendingError = class extends Error { constructor() { super("node invoke is not pending"); this.name = "NodeInvokeNotPendingError"; } }; var NodeInvokeStreamController = class { constructor(e) { this.options = e; } sendInput(e, t) { const n = this.options.pendingInvokes.get(e); if (!n) throw new NodeInvokeNotPendingError(); const r = JSON.stringify(t); if (r === void 0) throw new Error("node invoke input is not serializable"); if (Buffer.byteLength(r, "utf8") > MAX_INVOKE_INPUT_BYTES) throw new Error("node invoke input exceeds 16 KiB"); if (!this.getPending(e, n.nodeId, n.connId)) throw this.options.pendingInvokes.has(e) ? /* @__PURE__ */ new Error("node invoke is not pending") : new NodeInvokeNotPendingError(); if (!this.options.sendInput(e, n, n.nextInputSeq, r)) throw new Error("failed to send node invoke input"); n.nextInputSeq += 1; } reconcileRuntimePolicy() { for (const [e, t] of this.options.pendingInvokes) if (!this.settleIfExpired(e, t)) this.settleIfPolicyChanged(e, t); } handleDisconnect(e) { for (const [t, n] of this.options.pendingInvokes) { if (n.connId !== e) continue; if (this.settleIfExpired(t, n)) continue; if (!this.takePending(t, n)) continue; this.options.disconnectPending(n); } } handleResult(e) { const t = this.getPending(e.id, e.nodeId, e.connId); if (!t || !this.takePending(e.id, t)) return false; if (!e.ok) this.options.onFailedResult(t); const n = e.error?.code === "NODE_NOT_READY" && t.receivedProgress ? { code: "UNAVAILABLE", message: "node reported not-ready after invocation progress" } : e.error ?? null; t.resolve({ ok: e.ok, payload: e.payload, payloadJSON: e.payloadJSON ?? null, error: n }); return true; } armPending(e) { const t = captureGatewayRootWorkAdmissionContinuationScope(); if (t) e.pending.admissionContinuation = t; e.pending.deadlineAtMs = e.deadlineAtMs ?? (e.timeoutMs > 0 ? performance.now() + e.timeoutMs : void 0); this.options.pendingInvokes.set(e.requestId, e.pending); if (e.pending.deadlineAtMs !== void 0) { e.pending.cancelHardDeadline = scheduleAbsoluteDeadline(e.pending.deadlineAtMs, () => this.settleTimeout(e.requestId, e.pending), () => performance.now()); if (this.options.pendingInvokes.get(e.requestId) !== e.pending) return; } if (e.pending.onProgress && e.idleTimeoutMs > 0) e.pending.idleTimeoutMs = e.idleTimeoutMs; if (e.timeoutMs === 0) this.resetIdleTimer(e.requestId, e.pending); if (e.signal) { const onAbort = () => { if (this.settleIfExpired(e.requestId, e.pending)) return; const t = e.signal?.reason === NODE_INVOKE_PAIRING_CHANGED_ABORT; this.cancelPending(e.requestId, e.pending, t ? { code: "PAIRING_CHANGED", message: "node pairing changed after dispatch" } : { code: "ABORTED", message: "node invoke cancelled" }); }; e.signal.addEventListener("abort", onAbort, { once: true }); e.pending.removeAbortListener = () => e.signal?.removeEventListener("abort", onAbort); if (e.signal.aborted) onAbort(); } } handleProgress(e) { const t = this.getPending(e.invokeId, e.nodeId, e.connId); if (!t || e.seq < t.nextProgressSeq) return false; t.receivedProgress = true; if (!t.onProgress) return false; if (e.seq > t.nextProgressSeq) { if (t.progressChunks.has(e.seq)) return false; if (t.progressChunks.size >= MAX_PENDING_PROGRESS_CHUNKS) return false; } t.progressChunks.set(e.seq, e.chunk); if (!t.idleTimer) this.resetIdleTimer(e.invokeId, t); while (true) { const n = t.progressChunks.get(t.nextProgressSeq); if (n === void 0) break; if (!this.getPending(e.invokeId, e.nodeId, e.connId)) break; t.progressChunks.delete(t.nextProgressSeq); t.nextProgressSeq += 1; try { t.onProgress(n); } catch (n) { this.options.sendCancel(e.invokeId, t); this.clearTimers(t); this.options.pendingInvokes.delete(e.invokeId); t.reject(n instanceof Error ? n : new Error(String(n))); break; } if (this.options.pendingInvokes.get(e.invokeId) !== t) { t.progressChunks.clear(); break; } if (!this.getPending(e.invokeId, e.nodeId, e.connId)) break; this.resetIdleTimer(e.invokeId, t); } return true; } runPendingContinuation(e) { const t = this.getPending(e.invokeId, e.nodeId, e.connId); if (!t) return null; if (t.admissionContinuation) return t.admissionContinuation.run(e.run); return t.isCompletionAuthorized ? e.run() : null; } isPending(e, t, n) { return this.getPending(e, t, n) !== void 0; } getPending(e, t, n) { const r = this.options.pendingInvokes.get(e); if (!r || r.nodeId !== t || r.connId !== n || !this.options.isConnectionActive(r) || this.settleIfExpired(e, r) || this.settleIfPolicyChanged(e, r)) return; try { if (r.isCompletionAuthorized?.() !== false) return r; } catch {} this.cancelPending(e, r, { code: "APPROVAL_AUTHORITY_CLOSED", message: "node invoke authority closed before settlement" }); } clearTimers(e) { e.cancelHardDeadline?.(); e.cancelHardDeadline = void 0; clearTimeout(e.idleTimer); e.idleTraceContext = void 0; e.removeAbortListener?.(); e.removeAbortListener = void 0; e.admissionContinuation?.release(); e.admissionContinuation = void 0; } resetIdleTimer(e, t) { if (!t.idleTimeoutMs) return; t.idleTraceContext = getActiveDiagnosticTraceContext(); t.idleTimer = t.idleTimer?.refresh() ?? setTimeout(() => { runWithDiagnosticTraceContext(t.idleTraceContext, () => { this.settleTimeout(e, t, { code: "IDLE_TIMEOUT", message: "node invoke produced no progress" }); }); }, t.idleTimeoutMs); } settleIfExpired(e, t) { if (t.deadlineAtMs === void 0 || performance.now() < t.deadlineAtMs) return false; this.settleTimeout(e, t); return true; } settleTimeout(e, t, n = { code: "TIMEOUT", message: "node invoke timed out" }) { if (!this.takePending(e, t)) return; this.options.sendCancel(e, t); t.resolve({ ok: false, error: n }); } settleIfPolicyChanged(e, t) { if (this.options.isCommandAllowed(t.nodeId, t.command)) return false; this.cancelPending(e, t, { code: "POLICY_CHANGED", message: "node command is no longer allowed" }); return true; } cancelPending(e, t, n) { if (this.takePending(e, t)) { this.options.sendCancel(e, t); this.options.onFailedResult(t); t.resolve({ ok: false, error: n }); } } takePending(e, t) { if (this.options.pendingInvokes.get(e) !== t) return false; this.options.pendingInvokes.delete(e); this.clearTimers(t); return true; } }; //#endregion export { NodeInvokeNotPendingError as n, NodeInvokeStreamController as r, NODE_INVOKE_NOT_READY as t };