UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

64 lines (63 loc) • 2.54 kB
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { S as runOncePerAgentRun } from "./agent-events-BdfWYmot.mjs"; import { n as SILENT_REPLY_TOKEN } from "./tokens-CcE6ESvH.mjs"; import { n as getGlobalHookRunner } from "./hook-runner-global-DmYyIVDp.mjs"; import { c as readClaimingHookAdmission, l as withClaimingHookAdmission } from "./hooks-BiZpB_10.mjs"; import { n as isPluginHookAgentTrigger } from "./hook-types-CLw-HaZh.mjs"; import { i as withGuardedFetchRequestAuthority } from "./fetch-request-authority-nK0cxa_-.mjs"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/plugins/before-agent-reply.ts const beforeAgentReplyObserver = resolveGlobalSingleton(Symbol.for("openclaw.beforeAgentReply.observer"), () => new AsyncLocalStorage()); /** Attaches durable admission bookkeeping without moving hook ownership out of the runner. */ function withBeforeAgentReplyObserver(e, t) { return beforeAgentReplyObserver.run({ ...e }, t); } /** Preserves the full plugin reply contract, including private payload metadata. */ function buildHandledBeforeAgentReplyPayloads(e) { return [e ?? { text: "NO_REPLY" }]; } /** Runs the reply claim hook once for one admitted turn, across model fallbacks. */ function runBeforeAgentReplyForTurn(e) { const t = e.trigger; if (!isPluginHookAgentTrigger(t)) return Promise.resolve(void 0); const n = { ...e.context, trigger: t }; const r = readClaimingHookAdmission(n); const i = e.assertCurrent ? () => { r?.assertCurrent?.(); e.assertCurrent?.(); } : r?.assertCurrent; const run = () => runOncePerAgentRun(e.runId, "before_agent_reply", async () => { const t = getGlobalHookRunner(); if (!t?.hasHooks("before_agent_reply", n)) return; await e.awaitSessionDiffBaseline?.(); i?.(); const r = beforeAgentReplyObserver.getStore(); const a = r && (!r.runId || r.runId === e.runId) ? r : void 0; if (a && !a.runId) a.runId = e.runId; if (await a?.beforeDispatch() === false) return; i?.(); e.onDispatch?.(); i?.(); let o = await t.runBeforeAgentReply(e.event, n); i?.(); if (!o?.handled) e.onDeclined?.(); i?.(); if (a) o = await a.afterDispatch(o); i?.(); return o; }); return i ? withGuardedFetchRequestAuthority(i, async (e) => { withClaimingHookAdmission(n, { ...r, assertCurrent: e }); const t = await run(); e?.(); return t; }) : run(); } //#endregion export { runBeforeAgentReplyForTurn as n, withBeforeAgentReplyObserver as r, buildHandledBeforeAgentReplyPayloads as t };