UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

115 lines (114 loc) • 5.04 kB
import { o as getAsyncWorkSignal } from "./async-work-scope-BXagjUap.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs"; import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs"; import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs"; import { _ as resolveDefaultAgentId } from "./agent-scope-config-DmXgFGKP.mjs"; import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs"; import "./agent-scope-BwB8YH6y.mjs"; import "./io-4QsrsWTJ.mjs"; import { i as readSessionEntryReadOnlyInWorker } from "./session-entry-read-runtime-1Fp1sybj.mjs"; import { i as resolveSessionStoreAgentId, o as resolveSessionStoreKey } from "./session-store-key-CEwU3noi.mjs"; import { c as getSubagentSessionListReadSnapshotIdentity, f as prepareOptionalSubagentSessionListReadCache } from "./subagent-registry-state-CMZQXOjY.mjs"; import { i as getLatestLiveSubagentRunByChildSessionKey, t as buildLatestSubagentSessionListReadIndex } from "./subagent-registry-read-DHkmVSx2.mjs"; import { t as captureSessionEntryMetadataRead } from "./session-entry-source-authority-Bxiv7qVT.mjs"; import "./operator-approval-store-lZFt_sJ1.mjs"; //#region src/gateway/approval-session-audience.ts /** Resolves the source session and its operator-visible ancestor audience. */ async function resolveApprovalSessionAudience(e, t, n, r, i) { const canonicalize = (t, n) => { const i = t?.trim(); if (!i) return null; if (!n) return canonicalizeApprovalSourceStreamKey(e, i, r).trim() || null; const a = resolveSessionStoreAgentId(e, n); const o = resolveSessionStoreKey({ cfg: e, sessionKey: i, storeAgentId: a }); return (o ? resolveApprovalSourceStreamKey(o, a) : o).trim() || null; }; const a = canonicalize(n); if (!a) return []; const o = /* @__PURE__ */ new Set([a]); const s = [a]; const enqueue = (e) => { if (!e || o.has(e) || s.length >= 64) return; o.add(e); s.push(e); }; for (const n of s) { const r = t ? buildLatestSubagentSessionListReadIndex([n]).getLatestSubagentRun(n) : getLatestLiveSubagentRunByChildSessionKey(n); const o = [r?.controllerSessionKey, r?.requesterSessionKey].map((e) => canonicalize(e, n)).filter((e) => Boolean(e)); if (o.length > 0) { for (const e of o) enqueue(e); continue; } const s = parseAgentSessionKey(n); const c = s?.rest.toLowerCase() === "global" ? { agentId: normalizeAgentId(s.agentId), sessionKey: "global" } : { agentId: resolveSessionStoreAgentId(e, n), sessionKey: n }; const l = isIncognitoSessionKey(n) ? n === a ? i : captureSessionEntryMetadataRead(c, () => getAsyncWorkSignal()?.throwIfAborted()) : void 0; const u = l ? l.readCurrent() : await readSessionEntryReadOnlyInWorker({ ...c, storePath: resolveSessionStorePathCore(e.session?.store, { agentId: c.agentId }), clone: false, hydrateSkillPromptRefs: false }); enqueue(canonicalize(u?.parentSessionKey?.trim() ? u.parentSessionKey : u?.spawnedBy, n)); } return s; } /** Canonicalize one source key against config: agent scoping, main-key aliases, global sentinel. */ function canonicalizeApprovalSourceStreamKey(e, t, n) { const r = normalizeAgentId(n ?? resolveDefaultAgentId(e)); const i = t.trim().toLowerCase(); const a = parseAgentSessionKey(t) || i === "global" || i === "unknown" ? t : `agent:${r}:${t}`; return resolveApprovalSourceStreamKey(resolveSessionStoreKey({ cfg: e, sessionKey: a }), r); } /** Preserves source routing when lineage is unavailable, after read preparation settles. */ async function resolveApprovalSessionAudienceWithFallback(e, t) { const n = captureSessionEntryMetadataRead({ sessionKey: e, ...t ? { agentId: t } : {} }, () => getAsyncWorkSignal()?.throwIfAborted()); const r = n?.readCurrent(); let i; do { i = await prepareOptionalSubagentSessionListReadCache(); getAsyncWorkSignal()?.throwIfAborted(); } while (i && !getSubagentSessionListReadSnapshotIdentity()); if (n) { const e = n.readCurrent(); if (e?.sessionId !== r?.sessionId || e?.lifecycleRevision !== r?.lifecycleRevision) throw new Error("Approval source session changed during preparation"); } try { return await resolveApprovalSessionAudience(readRuntimeConfig(), i, e, t, n); } catch { return [resolveApprovalFallbackAudienceSessionKey(e, t)]; } } function resolveApprovalFallbackAudienceSessionKey(e, t) { try { return canonicalizeApprovalSourceStreamKey(readRuntimeConfig(), e, t); } catch { return resolveApprovalSourceStreamKey(e, t); } } /** Best-effort stream key used when lineage lookup is unavailable. */ function resolveApprovalSourceStreamKey(e, t) { const n = e.trim(); const r = n.toLowerCase(); if (!t || r === "unknown" || parseAgentSessionKey(n)) return n; const i = normalizeAgentId(t); return r === "global" ? `agent:${i}:global` : `agent:${i}:${n}`; } //#endregion export { resolveApprovalSourceStreamKey as n, resolveApprovalSessionAudienceWithFallback as t };