UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

134 lines (133 loc) • 5.32 kB
import { c as normalizeOptionalLowercaseString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { a as asOptionalRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import { n as buildAgentMainSessionKey } from "./session-key-DW1jcHbM.mjs"; import { D as parseThreadSessionSuffix, j as parseAgentSessionKey, u as resolveEventSessionKey } from "./session-key-CqAyZeVk.mjs"; import { t as deriveSessionChatTypeFromKey } from "./session-chat-type-shared-BP82uKBg.mjs"; import { r as resolveChannelAccountEntry } from "./account-lookup-FhQZVFfo.mjs"; import { i as resolvePinnedMainDmOwnerFromAllowlist } from "./dm-policy-shared-DYfj6q8U.mjs"; import { o as resolveAgentRoute } from "./resolve-route-DBnTWpM6.mjs"; //#region src/infra/event-session-routing.ts function readAllowFrom(e) { const t = asOptionalRecord(e)?.allowFrom; return Array.isArray(t) ? t : void 0; } function readDmAllowFrom(e) { return readAllowFrom(asOptionalRecord(e)?.dm); } /** Parse an agent direct-session key into channel/account/peer routing parts. */ function parseDirectAgentSessionTarget(e) { const { baseSessionKey: t } = parseThreadSessionSuffix(e); const n = t ?? e; const r = parseAgentSessionKey(n); if (!r || deriveSessionChatTypeFromKey(n) !== "direct") return null; const i = r.rest.split(":"); const a = i.findIndex((e) => e === "direct" || e === "dm"); if (a < 0 || a > 2 || a >= i.length - 1) return null; const o = normalizeLowercaseStringOrEmpty(i.slice(a + 1).join(":")); if (!o) return null; return { agentId: r.agentId, ...a >= 1 ? { channel: normalizeLowercaseStringOrEmpty(i[0]) } : {}, ...a >= 2 ? { accountId: normalizeLowercaseStringOrEmpty(i[1]) } : {}, peerId: o }; } /** Resolve the configured DM allowlist that applies to an event session. */ function resolveEventSessionAllowFrom(e) { const t = e.cfg; if (!t?.channels) return; const n = normalizeLowercaseStringOrEmpty(e.channel ?? e.target?.channel); if (!n) return; const r = isRecord(t.channels) ? t.channels[n] : void 0; if (!isRecord(r)) return; const i = normalizeLowercaseStringOrEmpty(e.accountId ?? e.target?.accountId); const a = i && isRecord(r.accounts) ? resolveChannelAccountEntry(r.accounts, i, n, (e) => e) : void 0; const o = asOptionalRecord(asOptionalRecord(a)?.config); return readDmAllowFrom(a) ?? readDmAllowFrom(o) ?? readAllowFrom(a) ?? readAllowFrom(o) ?? readDmAllowFrom(r) ?? readAllowFrom(r); } function shouldPreserveDirectSessionKeyFromRoute(e) { if (!e.cfg || !e.target?.channel) return false; try { const t = resolveAgentRoute({ cfg: e.cfg, channel: e.target.channel, accountId: e.target.accountId, peer: { kind: "direct", id: e.target.peerId } }); const { baseSessionKey: n } = parseThreadSessionSuffix(e.sessionKey); const r = normalizeLowercaseStringOrEmpty(t.sessionKey); return t.lastRoutePolicy === "session" && (r === normalizeLowercaseStringOrEmpty(e.sessionKey) || n !== void 0 && r === normalizeLowercaseStringOrEmpty(n)); } catch { return false; } } /** Build the routing policy used by session events. */ function resolveEventSessionRoutingPolicy(e) { const t = parseDirectAgentSessionTarget(e.sessionKey); const n = normalizeOptionalLowercaseString(e.channel ?? t?.channel); const r = normalizeOptionalLowercaseString(e.accountId ?? t?.accountId); const i = resolveEventSessionAllowFrom({ cfg: e.cfg, target: t, channel: n, accountId: r }); return { mainKey: e.cfg?.session?.mainKey, sessionScope: e.cfg?.session?.scope, dmScope: e.cfg?.session?.dmScope, allowFrom: i, channel: n, accountId: r, preserveSessionKey: e.sessionKey ? shouldPreserveDirectSessionKeyFromRoute({ cfg: e.cfg, sessionKey: e.sessionKey, target: t }) : false }; } /** Resolve a direct DM event session to the configured main session when allowed. */ function resolveMainScopedEventSessionKey(e) { const t = e.sessionKey.trim(); if (!t || e.policy?.preserveSessionKey === true) return null; const n = parseDirectAgentSessionTarget(t); if (!n) return null; const r = normalizeAgentId(e.agentId ?? n.agentId); if (normalizeAgentId(n.agentId) !== r) return null; const i = e.policy ?? resolveEventSessionRoutingPolicy({ cfg: e.cfg, sessionKey: t }); const a = Array.from(i.allowFrom ?? []); const o = resolvePinnedMainDmOwnerFromAllowlist({ dmScope: i.dmScope ?? e.cfg?.session?.dmScope, allowFrom: a, normalizeEntry: normalizeOptionalLowercaseString }); if (!o || normalizeOptionalLowercaseString(n.peerId) !== o) return null; if (shouldPreserveDirectSessionKeyFromRoute({ cfg: e.cfg, sessionKey: t, target: n })) return null; if (i.sessionScope === "global") return "global"; return buildAgentMainSessionKey({ agentId: r, mainKey: i.mainKey ?? e.cfg?.session?.mainKey }); } /** Apply event routing policy to a raw session key. */ function resolveEventSessionKeyForPolicy(e, t) { const n = resolveEventSessionKey(e, t?.mainKey, t?.sessionScope); if (n !== e) return n; return resolveMainScopedEventSessionKey({ sessionKey: e, policy: t }) ?? e; } //#endregion export { resolveEventSessionRoutingPolicy as n, resolveMainScopedEventSessionKey as r, resolveEventSessionKeyForPolicy as t };