UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

103 lines (102 loc) • 3.73 kB
import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs"; import { i as tryResolveCronJobEffectiveAgentId } from "./agent-id-CX8HtxAT.mjs"; import { t as resolveCronAgentSessionKey } from "./session-key-BQPSZVRH.mjs"; import { s as resolveCronSessionTargetSessionKey } from "./session-target-C-4MLah6.mjs"; //#region src/cron/job-session-bindings.ts const preparedBindings = /* @__PURE__ */ new WeakMap(); /** * Maps each canonical session key to its owning agent: the session the run * joins (main/isolated/session:<key>) plus the explicit wake/delivery lane in * job.sessionKey and the creating conversation. Keys compare equal to physical * Gateway session-store rows; values retain ownership when the key is unqualified. */ function resolveCronJobSessionBindings(e, t) { const n = t.cfg.session?.mainKey; const r = t.cfg.session?.scope; const i = e.sourceConversation?.sessionKey; const a = preparedBindings.get(e); for (const o of a ?? []) if (o.id === e.id && o.agentId === e.agentId && o.sessionKey === e.sessionKey && o.sessionTarget === e.sessionTarget && o.sourceSessionKey === i && o.defaultAgentId === t.defaultAgentId && o.mainKey === n && o.scope === r) return o.bindings; const o = /* @__PURE__ */ new Map(); const s = { id: e.id, agentId: e.agentId, sessionKey: e.sessionKey, sessionTarget: e.sessionTarget, sourceSessionKey: i, defaultAgentId: t.defaultAgentId, mainKey: n, scope: r, bindings: o }; if (a) { if (a.length === 4) a.shift(); a.push(s); } else preparedBindings.set(e, [s]); const c = tryResolveCronJobEffectiveAgentId(e, t.defaultAgentId); if (!c) return o; const add = (e) => { const i = e?.trim(); if (!i) return; const a = r === "global" && i === "global" ? i : resolveCronAgentSessionKey({ sessionKey: i, agentId: c, mainKey: n, cfg: t.cfg }); o.set(a, parseAgentSessionKey(a)?.agentId ?? c); }; try { if (e.sessionTarget === "main") add("main"); else if (e.sessionTarget === "isolated" || e.sessionTarget === "current") add(`cron:${e.id}`); else add(resolveCronSessionTargetSessionKey(e.sessionTarget)); add(e.sessionKey); add(e.sourceConversation?.sessionKey); } catch { o.clear(); } return o; } /** Signals a locked re-check found the job no longer bound; a per-job no-op. */ var CronJobBindingStaleError = class extends Error { constructor() { super("cron job binding changed concurrently"); } }; /** Disables enabled jobs bound to any archived session with one job-list scan. */ async function disableCronJobsBoundToSessions(e) { const t = [...new Set(e.sessionKeys.map((e) => e.trim()).filter(Boolean))]; const n = new Map(t.map((e) => [e, []])); if (t.length === 0) return n; const r = new Set(t); const i = await e.cron.list(); const a = e.cron.getDefaultAgentId(); const matchingSessionKeys = (t) => { if (!t.enabled) return []; return [...resolveCronJobSessionBindings(t, { cfg: e.cfg, defaultAgentId: a }).keys()].filter((e) => r.has(e)); }; const o = []; for (const t of i) { if (matchingSessionKeys(t).length === 0) continue; try { let r = []; await e.cron.updateWithPrecondition(t.id, { enabled: false }, (e) => { r = matchingSessionKeys(e); if (r.length === 0) throw new CronJobBindingStaleError(); }); for (const e of r) n.get(e)?.push(t.id); } catch (e) { if (e instanceof CronJobBindingStaleError) continue; o.push(e); } } if (o.length > 0) { const e = t.length === 1 ? `bound to ${t[0]}` : `bound to ${t.length} archived sessions`; throw new AggregateError(o, `failed to disable ${o.length} cron job(s) ${e}`); } return n; } //#endregion export { resolveCronJobSessionBindings as n, disableCronJobsBoundToSessions as t };