UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

103 lines (102 loc) • 5.37 kB
import { r as getAsyncWorkSignal } from "./async-work-scope-DDiH0P7W.mjs"; import { n as normalizeAgentId } from "./agent-id-D1-Q6vR4.mjs"; import "./session-key-DY3MOX4N.mjs"; import { h as parseAgentSessionKey } from "./session-key-utils-CDyBC9Gn.mjs"; import { g as resolveDefaultAgentId } from "./agent-scope-config-BEk6-ThS.mjs"; import "./agent-scope-bJOokm5r.mjs"; import { r as getRuntimeConfig } from "./io.runtime-B134MOMz.mjs"; import "./io-BGddrCVX.mjs"; import { s as loadSessionEntryReadOnly } from "./session-accessor.sqlite-entry-DLa7_9ui.mjs"; import "./session-accessor-C3ljJ974.mjs"; import { a as resolveSessionStoreKey, r as resolveSessionStoreAgentId } from "./session-store-key-Dvf3CKvN.mjs"; import { o as getSubagentSessionListReadSnapshotIdentity, u as prepareOptionalSubagentSessionListReadCache } from "./subagent-registry-state-6clmFwBl.mjs"; import { i as getLatestLiveSubagentRunByChildSessionKey, t as buildLatestSubagentSessionListReadIndex } from "./subagent-registry-read-kqDz6PtN.mjs"; import "./operator-approval-store-BakMJHIi.mjs"; //#region src/gateway/approval-session-audience.ts /** Resolves the source session and its operator-visible ancestor audience. */ function resolveApprovalSessionAudience(cfg, persisted, source, sourceAgentId) { const canonicalize = (sessionKey, relativeToSessionKey) => { const raw = sessionKey?.trim(); if (!raw) return null; if (!relativeToSessionKey) return canonicalizeApprovalSourceStreamKey(cfg, raw, sourceAgentId).trim() || null; const relativeAgentId = resolveSessionStoreAgentId(cfg, relativeToSessionKey); const canonical = resolveSessionStoreKey({ cfg, sessionKey: raw, storeAgentId: relativeAgentId }); return (canonical ? resolveApprovalSourceStreamKey(canonical, relativeAgentId) : canonical).trim() || null; }; const sourceSessionKey = canonicalize(source); if (!sourceSessionKey) return []; const queued = /* @__PURE__ */ new Set([sourceSessionKey]); const pending = [sourceSessionKey]; const enqueue = (sessionKey) => { if (!sessionKey || queued.has(sessionKey) || pending.length >= 64) return; queued.add(sessionKey); pending.push(sessionKey); }; for (const sessionKey of pending) { const subagentLineage = persisted ? buildLatestSubagentSessionListReadIndex([sessionKey]).getLatestSubagentRun(sessionKey) : getLatestLiveSubagentRunByChildSessionKey(sessionKey); const registryParents = [subagentLineage?.controllerSessionKey, subagentLineage?.requesterSessionKey].map((parent) => canonicalize(parent, sessionKey)).filter((candidate) => Boolean(candidate)); if (registryParents.length > 0) { for (const parentSessionKey of registryParents) enqueue(parentSessionKey); continue; } const parsed = parseAgentSessionKey(sessionKey); const target = parsed?.rest.toLowerCase() === "global" ? { agentId: normalizeAgentId(parsed.agentId), sessionKey: "global" } : { agentId: resolveSessionStoreAgentId(cfg, sessionKey), sessionKey }; const storedLineage = loadSessionEntryReadOnly({ ...target, clone: false, hydrateSkillPromptRefs: false }); enqueue(canonicalize(storedLineage?.parentSessionKey?.trim() ? storedLineage.parentSessionKey : storedLineage?.spawnedBy, sessionKey)); } return pending; } /** Canonicalize one source key against config: agent scoping, main-key aliases, global sentinel. */ function canonicalizeApprovalSourceStreamKey(cfg, sessionKey, sourceAgentId) { const ownerAgentId = normalizeAgentId(sourceAgentId ?? resolveDefaultAgentId(cfg)); const lowered = sessionKey.trim().toLowerCase(); const scoped = parseAgentSessionKey(sessionKey) || lowered === "global" || lowered === "unknown" ? sessionKey : `agent:${ownerAgentId}:${sessionKey}`; return resolveApprovalSourceStreamKey(resolveSessionStoreKey({ cfg, sessionKey: scoped }), ownerAgentId); } /** Preserves source routing when lineage is unavailable, after read preparation settles. */ async function resolveApprovalSessionAudienceWithFallback(sourceSessionKey, sourceAgentId) { let persisted; do { persisted = await prepareOptionalSubagentSessionListReadCache(); getAsyncWorkSignal()?.throwIfAborted(); } while (persisted && !getSubagentSessionListReadSnapshotIdentity()); try { return resolveApprovalSessionAudience(getRuntimeConfig(), persisted, sourceSessionKey, sourceAgentId); } catch { return [resolveApprovalFallbackAudienceSessionKey(sourceSessionKey, sourceAgentId)]; } } function resolveApprovalFallbackAudienceSessionKey(sourceSessionKey, sourceAgentId) { try { return canonicalizeApprovalSourceStreamKey(getRuntimeConfig(), sourceSessionKey, sourceAgentId); } catch { return resolveApprovalSourceStreamKey(sourceSessionKey, sourceAgentId); } } /** Best-effort stream key used when lineage lookup is unavailable. */ function resolveApprovalSourceStreamKey(sourceSessionKey, sourceAgentId) { const normalizedSessionKey = sourceSessionKey.trim(); const lowered = normalizedSessionKey.toLowerCase(); if (!sourceAgentId || lowered === "unknown" || parseAgentSessionKey(normalizedSessionKey)) return normalizedSessionKey; const agentId = normalizeAgentId(sourceAgentId); return lowered === "global" ? `agent:${agentId}:global` : `agent:${agentId}:${normalizedSessionKey}`; } //#endregion export { resolveApprovalSourceStreamKey as n, resolveApprovalSessionAudienceWithFallback as t };