UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

120 lines (119 loc) • 4.56 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { t as KeyedAsyncQueue } from "./keyed-async-queue-DewLnYvW.mjs"; //#region src/auto-reply/reply/message-injection-authority.ts const injectionQueues = resolveGlobalSingleton(Symbol.for("openclaw.messageInjectionQueues"), () => /* @__PURE__ */ new WeakMap()); const injectionAdmissions = resolveGlobalSingleton(Symbol.for("openclaw.messageInjectionAdmissions"), () => /* @__PURE__ */ new WeakMap()); const questionAssertionFrame = resolveGlobalSingleton(Symbol.for("openclaw.messageInjectionQuestionAssertionFrame"), () => ({ active: false })); /** Released adapters may wrap the callback; scope only its synchronous question assertion. */ function withQuestionInputAssertion(e) { const t = questionAssertionFrame.active; questionAssertionFrame.active = true; try { e(); } finally { questionAssertionFrame.active = t; } } /** Queue predicates remain final; question grants retain the original policy/source assertion. */ function createLegacyMessageInjectionAuthority(e, t) { return createMessageInjectionAuthority(() => { e(); if (!questionAssertionFrame.active) { t(); e(); } return true; }); } /** Bind final enqueue custody to this exact host-owned preparation. */ function bindMessageInjectionAdmission(e, t) { injectionAdmissions.set(e, t); } async function withMessageInjectionAdmission(e, t) { let n = false; const consumeSync = () => { const e = t(); n = true; return e; }; const r = e && injectionAdmissions.get(e); if (!r) { if (e) await e(); return consumeSync(); } try { return await r(consumeSync); } catch (e) { if (n) throw new MessageInjectionAcceptedUnconfirmedError({ cause: e }); throw e; } } /** Order preparation on the captured backend. */ function enqueueMessageInjection(e, t) { let n = injectionQueues.get(e); if (!n) { n = new KeyedAsyncQueue(); injectionQueues.set(e, n); } return n.enqueue("input", t); } /** A refused owner assertion is terminal for this input, not permission to redispatch it. */ var MessageInjectionAuthorityError = class extends Error { constructor(e) { super("Message injection authority is no longer current", e); this.name = "MessageInjectionAuthorityError"; } }; /** The input belongs to another requester and may be admitted as a separate turn. */ var MessageInjectionRequesterMismatchError = class extends Error { constructor() { super("Steering requires the active turn's authenticated requester"); this.name = "MessageInjectionRequesterMismatchError"; } }; /** A live caller needs a separate execution; this is not a failed or revoked publisher. */ var MessageInjectionPolicyMismatchError = class extends Error { constructor(e, t = "Current caller policy does not match the active execution") { super(t); this.reason = e; this.name = "MessageInjectionPolicyMismatchError"; } }; /** The caller remains valid, but this target can no longer receive its input. */ var MessageInjectionTargetUnavailableError = class extends Error { constructor(e) { super(e); this.name = "MessageInjectionTargetUnavailableError"; } }; /** The delivery owner removed the exact queued input before provider submission. */ var MessageInjectionWithdrawnError = class extends MessageInjectionTargetUnavailableError { constructor(e) { super(e); this.name = "MessageInjectionWithdrawnError"; } }; /** Enqueued input retains custody even when admission cleanup or notification fails. */ var MessageInjectionAcceptedUnconfirmedError = class extends Error { constructor(e) { super("Message injection was accepted but its completion is unconfirmed", e); this.name = "MessageInjectionAcceptedUnconfirmedError"; } }; /** One injection stays revoked even if its source later appears current again. */ function createMessageInjectionAuthority(e) { let t; return () => { if (!t) { try { if (e()) return; } catch (e) { t = new MessageInjectionAuthorityError({ cause: e }); } t ??= new MessageInjectionAuthorityError(); } throw t; }; } //#endregion export { MessageInjectionTargetUnavailableError as a, createLegacyMessageInjectionAuthority as c, withMessageInjectionAdmission as d, withQuestionInputAssertion as f, MessageInjectionRequesterMismatchError as i, createMessageInjectionAuthority as l, MessageInjectionAuthorityError as n, MessageInjectionWithdrawnError as o, MessageInjectionPolicyMismatchError as r, bindMessageInjectionAdmission as s, MessageInjectionAcceptedUnconfirmedError as t, enqueueMessageInjection as u };