UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

101 lines (100 loc) 5.47 kB
import { n as normalizeAgentId, r as normalizeAgentIdStrict } from "./agent-id-CeT3w4ap.js"; import { D as tryResolveLegacyCompatibilityAgentId, o as listAgentIds, t as AgentSelectionRequiredError } from "./agent-scope-config-DcbEhP0R.js"; import { O as parseAgentSessionKey, u as normalizeMainKey } from "./session-key-BnWWjqNc.js"; import "./legacy.default-agent-owner-BGwEdQRe.js"; import "./agent-scope-DbtJyKUL.js"; import { n as resolvePersistedSessionStoreOwnerForKey } from "./session-store-owner-CR2Ag3xK.js"; import { t as ErrorCodes } from "./gateway-error-details-w0nAGBBp.js"; import { d as errorShape } from "./error-codes-Bo8q2D1o.js"; import { n as resolveSessionSubscriptionKeys } from "./session-subscription-keys-BwUDpvzj.js"; //#region src/gateway/session-request-agent.ts /** Resolves public event identity separately from private session routing ownership. */ function resolveSessionEventAgentScope(cfg, key, explicitAgentId, publishQualifiedAgent = false) { const parsed = parseAgentSessionKey(key.trim()); const keyAgentId = parsed?.agentId ? normalizeAgentId(parsed.agentId) : void 0; const explicit = explicitAgentId === void 0 ? null : normalizeAgentIdStrict(explicitAgentId); if (explicit !== null && !explicit.ok) return null; if (explicit?.value && keyAgentId && explicit.value !== keyAgentId) return null; const persistedOwner = resolvePersistedSessionStoreOwnerForKey(cfg, key); const compatibilityOwnerAgentId = keyAgentId ? void 0 : tryResolveSessionCompatibilityOwnerAgentId(cfg, key); return [ explicit?.value ?? (publishQualifiedAgent && keyAgentId && listAgentIds(cfg).includes(keyAgentId) ? keyAgentId : void 0), explicit?.value ?? keyAgentId ?? compatibilityOwnerAgentId ?? (persistedOwner.kind === "retired" ? persistedOwner.agentId : void 0), compatibilityOwnerAgentId ]; } /** Binds a retired unqualified owner to its private sharing scope. */ function resolvePrivateSessionEventBroadcastScope(key, [eventAgentId, routingAgentId, compatibilityOwnerAgentId]) { return key && !parseAgentSessionKey(key) && !eventAgentId && routingAgentId && !compatibilityOwnerAgentId ? { agentId: routingAgentId, sessionKeys: resolveSessionSubscriptionKeys(key, routingAgentId) } : void 0; } /** Resolves only stable implicit ownership for unscoped session rows and active runs. */ function tryResolveSessionCompatibilityOwnerAgentId(cfg, key) { const persistedStoreOwner = resolvePersistedSessionStoreOwnerForKey(cfg, key); if (persistedStoreOwner.kind === "configured") return persistedStoreOwner.agentId; return persistedStoreOwner.kind === "retired" ? void 0 : tryResolveLegacyCompatibilityAgentId(cfg); } function resolveRequestedSessionAgentId(cfg, key, explicitAgentId) { const parsed = parseAgentSessionKey(key?.trim()); const configuredAgentIds = listAgentIds(cfg); const normalizedRequest = explicitAgentId === void 0 ? null : normalizeAgentIdStrict(explicitAgentId); if (normalizedRequest && !normalizedRequest.ok) return { ok: false, error: errorShape(ErrorCodes.INVALID_REQUEST, `Unknown agent id "${explicitAgentId}"`) }; const normalizedRequestedAgentId = normalizedRequest?.value; if (normalizedRequestedAgentId && !configuredAgentIds.includes(normalizedRequestedAgentId)) return { ok: false, error: errorShape(ErrorCodes.INVALID_REQUEST, `Unknown agent id "${explicitAgentId}"`) }; let ownerKey = key; if (parsed?.agentId) { const keyAgentId = normalizeAgentId(parsed.agentId); const keyIsGlobalMainAlias = cfg.session?.scope === "global" && (parsed.rest === "main" || parsed.rest === normalizeMainKey(cfg.session?.mainKey)); if (keyIsGlobalMainAlias && !configuredAgentIds.includes(keyAgentId)) return { ok: false, error: errorShape(ErrorCodes.INVALID_REQUEST, `Unknown agent id "${parsed.agentId}"`) }; if (normalizedRequestedAgentId && keyAgentId !== normalizedRequestedAgentId) return { ok: false, error: errorShape(ErrorCodes.INVALID_REQUEST, `agent "${explicitAgentId}" does not match session key agent "${keyAgentId}"`) }; if (!keyIsGlobalMainAlias || !normalizedRequestedAgentId) return { ok: true, agentId: keyAgentId }; ownerKey = "global"; } const persistedStoreOwner = resolvePersistedSessionStoreOwnerForKey(cfg, ownerKey); if (persistedStoreOwner.kind === "retired") return { ok: false, error: errorShape(ErrorCodes.INVALID_REQUEST, `session key belongs to retired agent "${persistedStoreOwner.agentId}"`) }; if (normalizedRequestedAgentId) { if (persistedStoreOwner.kind === "configured" && persistedStoreOwner.agentId !== normalizedRequestedAgentId) return { ok: false, error: errorShape(ErrorCodes.INVALID_REQUEST, `agent "${explicitAgentId}" does not match session key agent "${persistedStoreOwner.agentId}"`) }; return { ok: true, agentId: normalizedRequestedAgentId }; } const inferredAgentId = tryResolveSessionCompatibilityOwnerAgentId(cfg, key); if (inferredAgentId) return { ok: true, agentId: inferredAgentId }; const selectionError = new AgentSelectionRequiredError(configuredAgentIds, { surface: `session key "${key}"`, hint: "Pass agentId or use an agent-prefixed session key." }); return { ok: false, error: errorShape(ErrorCodes.INVALID_REQUEST, selectionError.message) }; } //#endregion export { tryResolveSessionCompatibilityOwnerAgentId as i, resolveRequestedSessionAgentId as n, resolveSessionEventAgentScope as r, resolvePrivateSessionEventBroadcastScope as t };