UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

206 lines (205 loc) • 7.42 kB
import { a as pluginInstanceInvocation } from "./plugin-instance-invocation-Bq_toGDw.mjs"; import "./session-key-CqAyZeVk.mjs"; import { n as normalizeAccountId } from "./account-id-BqKT3GuY.mjs"; import { a as getPluginInstanceOwner, l as getPluginValueInstance } from "./plugin-instance-scope-oezVdCr2.mjs"; import { l as getPluginRecordRegistry } from "./registry-lifecycle-CJjWnl-y.mjs"; import { r as resolveChannelAccountEntry } from "./account-lookup-FhQZVFfo.mjs"; import { i as resolveStableChannelIngressPolicy, r as resolveChannelIngressPolicy, t as createChannelIngressPolicyResolver } from "./runtime-CH2MJcMr.mjs"; import "./store-allow-from-qX0U8hg9.mjs"; import { n as createChannelIngressMonitor } from "./ingress-monitor-CkIIn1aC.mjs"; //#region src/channels/message/ingress-hooks.ts /** Await enabled ingress policies without materializing a plugin runtime at channel startup. */ async function prepareChannelIngressEvent(e, t) { const { getGlobalHookRunner: n } = await import("./plugins/hook-runner-global.js"); return await n()?.runBeforeChannelIngress({ event: e }, t); } //#endregion //#region src/config/implicit-mentions.ts const SHIPPED_IMPLICIT_MENTION_DEFAULTS = { replyToBot: true, quotedBot: true, threadParticipation: true }; /** Resolves each implicit-mention kind using account, channel, defaults, then shipped behavior. */ function resolveChannelImplicitMentions(e) { const t = e.cfg.channels?.[e.channel]; const n = resolveChannelAccountEntry(t?.accounts, normalizeAccountId(e.accountId), e.channel); const r = e.cfg.channels?.defaults?.implicitMentions; const resolve = (e) => n?.implicitMentions?.[e] ?? t?.implicitMentions?.[e] ?? r?.[e] ?? SHIPPED_IMPLICIT_MENTION_DEFAULTS[e]; return { replyToBot: resolve("replyToBot"), quotedBot: resolve("quotedBot"), threadParticipation: resolve("threadParticipation") }; } //#endregion //#region src/plugin-sdk/channel-ingress-runtime.ts /** * High-level runtime resolver for inbound channel access decisions. * * New receive paths use runtime.channel.inbound.ingress. This subpath retains * released resolver adapters alongside identity, policy, and monitor helpers. */ function currentIngressInstance() { const e = pluginInstanceInvocation.getStore()?.instance; const t = e && getPluginInstanceOwner(e); return t?.instance?.hasActiveCall && t.instance === e && !t.revoked ? t.instance : void 0; } function registeredIngress(e, t) { const n = e?.owner; if (!e?.hasActiveCall || e.lifecycle.signal.aborted || !n || n.revoked) return; const r = getPluginRecordRegistry(n.registry, n.record).channels.find((r) => r.pluginId === n.record.id && r.plugin.id === t.trim() && getPluginValueInstance(r.plugin) === e); return r?.captureReadAuthority?.()?.() ? r.resolveChannelRuntime?.().inbound.ingress : void 0; } /** Retain the creating instance when adapting a released reusable resolver. */ function createChannelIngressResolver(e) { const t = currentIngressInstance(); const n = createChannelIngressPolicyResolver(e); const resolve = () => registeredIngress(t, e.channelId)?.createResolver(e) ?? n; return { message: (e) => resolve().message(e), command: (e) => resolve().command(e), event: (e) => resolve().event(e) }; } /** Preserve the released helper's trusted attribution within its managed callback. */ async function resolveChannelMessageIngress(e) { const t = registeredIngress(currentIngressInstance(), e.channelId); return await (t ? t.resolve(e) : resolveChannelIngressPolicy(e)); } /** Preserve the released stable-identity helper through the same ingress owner. */ async function resolveStableChannelMessageIngress(e) { const t = registeredIngress(currentIngressInstance(), e.channelId); return await (t ? t.resolveStable(e) : resolveStableChannelIngressPolicy(e)); } /** Version-1 raw events, direct admission wakeups, eight deliveries, and standard retention. */ function createStandardRawEventIngressMonitor(e) { const { classifyAdmissionError: t, createStoppedError: n, drain: r, payload: i, pollIntervalMs: a, ...o } = e; const s = n ?? (() => /* @__PURE__ */ new Error("Channel ingress monitor is stopped.")); const c = createChannelIngressMonitor({ ...o, inspect: e.inspect, payload: { ...i, storage: "raw-event", version: 1 }, pollIntervalMs: a ?? 500, retention: "standard", drain: { ...r, startLimit: 8 }, admissionMode: "while-running", createStoppedError: s }); return { receive: async (n) => { if (!c.isRunning()) throw s(); let r; try { r = e.inspect(n); } catch (e) { const n = t(e); if (n === void 0) throw e; return { kind: "invalid", message: n }; } if (!r) return { kind: "ignored" }; await c.admit(n, { facts: r }); return { kind: "durable" }; }, start: c.start, stop: c.stop, waitForIdle: c.waitForIdle }; } /** Fan one logical inbound turn's ownership lifecycle across its durable claims. */ function fanInChannelIngressLifecycles(e) { const t = e.filter((e) => e !== void 0); const n = t[0]; if (!n) return { lifecycle: void 0, settle: async () => {}, abandon: async () => {}, cancel: async () => {} }; let r = false; let i = false; const settleOnce = async (e) => { if (i) return; i = true; await e(); }; const fanOut = async (e, n = t) => { await Promise.all(n.map(async (t) => await e(t))); }; const abandonAll = () => settleOnce(() => fanOut((e) => e.onAbandoned())); const failEach = (e, t) => fanOut((e) => e.onFailed ? e.onFailed(t) : e.onAbandoned(), e); const failAll = (e) => settleOnce(() => failEach(t, e)); const adoptAll = async () => { for (const [e, n] of t.entries()) try { await n.onAdopted(); } catch (n) { await Promise.allSettled([settleOnce(() => failEach(t.slice(e), n))]); throw n; } }; const a = t.every((e) => e.onCancelled !== void 0); const o = t.map((e) => e.deferredHeartbeatIntervalMs).filter((e) => e !== void 0 && Number.isFinite(e) && e > 0); const cancelAll = () => settleOnce(() => fanOut((e) => e.onCancelled ? e.onCancelled() : e.onAbandoned())); return { lifecycle: { abortSignal: t.length === 1 ? n.abortSignal : AbortSignal.any(t.map((e) => e.abortSignal)), onAdopted: async () => { r = true; await adoptAll(); }, onDeferred: () => { r = true; for (const e of t) e.onDeferred(); }, onDeferredHeartbeat: () => { for (const e of t) e.onDeferredHeartbeat?.(); }, ...o.length > 0 ? { deferredHeartbeatIntervalMs: Math.min(...o) } : {}, onAdoptionFinalizing: () => { for (const e of t) e.onAdoptionFinalizing(); }, onFailed: async (e) => { r = true; await failAll(e); }, ...a ? { onCancelled: async () => { r = true; await cancelAll(); } } : {}, onAbandoned: async () => { r = true; await abandonAll(); } }, settle: async () => { if (!r) { r = true; await adoptAll(); } }, abandon: async (e) => { if (!r) { r = true; await (e === void 0 ? abandonAll() : failAll(e)); } }, cancel: async () => { if (!r) { r = true; await cancelAll(); } } }; } //#endregion export { resolveStableChannelMessageIngress as a, resolveChannelMessageIngress as i, createStandardRawEventIngressMonitor as n, resolveChannelImplicitMentions as o, fanInChannelIngressLifecycles as r, prepareChannelIngressEvent as s, createChannelIngressResolver as t };