UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

273 lines (272 loc) • 9.25 kB
import { l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import "./session-key-CqAyZeVk.mjs"; import { n as normalizeAccountId } from "./account-id-BqKT3GuY.mjs"; import { n as parseToolsBySenderTypedKey } from "./types.tools-BQVTy9eE.mjs"; import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs"; import { i as resolveChannelAccountKey, r as resolveChannelAccountEntry } from "./account-lookup-FhQZVFfo.mjs"; import { n as resolveMergedAccountConfig } from "./channel-account-config-BvENCH3O.mjs"; //#region src/config/channel-groups.ts function resolveChannelGroups(e, t, n) { const r = normalizeAccountId(n); const i = e.channels?.[t]; if (!i) return; return resolveMergedAccountConfig({ channelConfig: i, channelId: t, accounts: i.accounts, accountId: r, inheritEmptyKeys: Object.keys(i.accounts ?? {}).length > 1 ? {} : { groups: "object" } }).groups; } //#endregion //#region src/config/tools-by-sender.ts const compiledToolsBySenderCache = /* @__PURE__ */ new WeakMap(); function normalizeSenderKey(e, t = false) { const n = e.trim(); if (!n) return ""; const r = t && n.startsWith("@") ? n.slice(1) : n; return normalizeLowercaseStringOrEmpty(r); } function normalizeTypedSenderKey(e, t) { if (t === "channel") return normalizeChannelSenderKey(e); return normalizeSenderKey(e, t === "username"); } function normalizeSenderPolicyChannel(e) { const t = normalizeOptionalString(e); if (!t) return ""; return normalizeMessageChannel(t) ?? normalizeSenderKey(t); } function normalizeChannelSenderKey(e) { const t = e.trim(); const n = t.indexOf(":"); if (n <= 0 || n === t.length - 1) return ""; const r = normalizeSenderPolicyChannel(t.slice(0, n)); const i = normalizeTypedSenderKey(t.slice(n + 1), "id"); if (!r || !i) return ""; return `${r}:${i}`; } function resolveCompiledToolsBySenderPolicy(e) { const t = compiledToolsBySenderCache.get(e); if (t) return t; const n = Object.entries(e); if (n.length === 0) return; const r = { channel: /* @__PURE__ */ new Map(), id: /* @__PURE__ */ new Map(), e164: /* @__PURE__ */ new Map(), username: /* @__PURE__ */ new Map(), name: /* @__PURE__ */ new Map() }; let i; for (const [e, t] of n) { if (!t) continue; const n = e.trim(); if (!n) continue; if (n === "*") { i = t; continue; } const a = parseToolsBySenderTypedKey(n); if (!a) throw new Error("Untyped toolsBySender keys are retired. Run \"openclaw doctor --fix\"."); const o = normalizeTypedSenderKey(a.value, a.type); const s = r[a.type]; if (o && !s.has(o)) s.set(o, t); } const a = { buckets: r, wildcard: i }; compiledToolsBySenderCache.set(e, a); return a; } function normalizeSenderIdCandidates(e) { const t = normalizeOptionalString(e); if (!t) return []; const n = normalizeTypedSenderKey(t, "id"); const r = normalizeSenderKey(t, true); if (!r || r === n) return [n]; return [n, r]; } function matchToolsBySenderPolicy(e, t) { const n = normalizeSenderIdCandidates(t.senderId); const r = normalizeSenderPolicyChannel(t.messageProvider); for (const t of ["channel", "id"]) { if (t === "channel" && !r) continue; for (const i of n) { const n = t === "channel" ? `${r}:${i}` : i; const a = e.buckets[t].get(n); if (a) return a; } } for (const [n, r] of [ ["e164", t.senderE164], ["username", t.senderUsername], ["name", t.senderName] ]) { const t = normalizeTypedSenderKey(normalizeOptionalString(r) ?? "", n); if (t) { const r = e.buckets[n].get(t); if (r) return r; } } return e.wildcard; } function resolveToolsBySender(e) { const t = e.toolsBySender; if (!t) return; const n = resolveCompiledToolsBySenderPolicy(t); if (!n) return; return matchToolsBySenderPolicy(n, e); } //#endregion //#region src/config/group-scope-tree.ts const encodeScopeSegment = (e) => `${e.length}:${e}`; function scopeKey(...e) { return e.map(([e, t]) => `${e}:${encodeScopeSegment(t)}`).join("/"); } function buildChannelGroupsScopeTree(e, t, n) { const { "*": r, ...i } = resolveChannelGroups(e, t, n) ?? {}; return { defaults: r, scopes: i }; } function resolveScopeKeyCaseInsensitive(e, t) { if (!t) return; if (Object.hasOwn(e.scopes, t)) return t; const n = normalizeLowercaseStringOrEmpty(t); return Object.keys(e.scopes).find((e) => normalizeLowercaseStringOrEmpty(e) === n); } function resolveFromScopes(e) { for (let t = e.path.length - 1; t >= 0; t -= 1) { const n = e.path[t]; if (n === void 0 || !Object.hasOwn(e.tree.scopes, n)) continue; const r = e.tree.scopes[n]; if (!r) continue; const i = e.resolveNode(r); if (i !== void 0) return i; } return e.tree.defaults ? e.resolveNode(e.tree.defaults) : void 0; } function resolveScopeRequireMention(e) { const { requireMentionOverride: t, overrideOrder: n = "after-config" } = e; const r = resolveFromScopes({ tree: e.tree, path: e.path, resolveNode: (e) => e.requireMention }); if (n === "before-config" && typeof t === "boolean") return t; if (typeof r === "boolean") return r; if (n !== "before-config" && typeof t === "boolean") return t; if (e.configuredScopeDefaultsToNoMention && e.path.some((t) => Object.hasOwn(e.tree.scopes, t))) return false; return true; } function resolveScopeToolsPolicy(e) { return resolveFromScopes({ tree: e.tree, path: e.path, resolveNode: (t) => (e.senderPolicyMode === "never" ? void 0 : resolveToolsBySender({ toolsBySender: t.toolsBySender, senderId: e.senderId, senderName: e.senderName, senderUsername: e.senderUsername, senderE164: e.senderE164, messageProvider: e.messageProvider })) ?? t.tools }); } function resolveScopeIntroHint(e) { return resolveFromScopes({ tree: e.tree, path: e.path, resolveNode: (e) => e.introHint }); } //#endregion //#region src/config/group-policy.ts function resolveChannelGroupConfig(e, t, n = false) { if (!e) return; const r = e[t]; if (r) return r; if (!n) return; const i = normalizeLowercaseStringOrEmpty(t); const a = Object.keys(e).find((e) => e !== "*" && normalizeLowercaseStringOrEmpty(e) === i); if (!a) return; return e[a]; } /** Locate the authored map selected by the channel owner without changing its inheritance rules. */ function resolveChannelGroupsConfigPath(e) { const t = `channels.${e.channel}.groups`; const n = (e.cfg.channels?.[e.channel])?.accounts; if (!n) return t; const r = normalizeAccountId(e.accountId); const i = resolveChannelAccountKey(n, r, e.channel); const a = i ? n[i] : void 0; if (!a || e.groups !== void 0 && e.groups !== a.groups) return t; return i ? `channels.${e.channel}.accounts[${JSON.stringify(i)}].groups` : t; } function resolveChannelGroupPolicyMode(e, t, n) { const r = normalizeAccountId(n); const i = e.channels?.[t]; if (!i) return; return resolveChannelAccountEntry(i.accounts, r, t)?.groupPolicy ?? i.groupPolicy; } function resolveChannelGroupPolicy(e) { const { cfg: t, channel: n } = e; const r = resolveChannelGroups(t, n, e.accountId); const i = resolveChannelGroupPolicyMode(t, n, e.accountId); const a = Boolean(r && Object.keys(r).length > 0); const o = i === "allowlist" || a; const s = e.groupId?.trim(); const c = s ? resolveChannelGroupConfig(r, s, e.groupIdCaseInsensitive) : void 0; const l = r?.["*"]; const u = o && Boolean(r && Object.hasOwn(r, "*")); const d = i === "allowlist" && !a && Boolean(e.hasGroupAllowFrom); return { allowlistEnabled: o, allowed: i === "disabled" ? false : !o || u || Boolean(c) || d, groupConfig: c, defaultConfig: l }; } function buildSelectedGroupScope(e, t) { const project = (e) => ({ requireMention: typeof e.requireMention === "boolean" ? e.requireMention : void 0, tools: e.tools || void 0, toolsBySender: e.toolsBySender }); return { tree: { scopes: e ? { selected: project(e) } : {}, defaults: t ? project(t) : void 0 }, path: e ? ["selected"] : [] }; } function resolveChannelGroupRequireMention(e) { const { groupConfig: t, defaultConfig: n } = resolveChannelGroupPolicy(e); return resolveScopeRequireMention({ ...buildSelectedGroupScope(t, n), requireMentionOverride: e.requireMentionOverride, overrideOrder: e.overrideOrder, configuredScopeDefaultsToNoMention: e.configuredGroupDefaultsToNoMention }); } function resolveChannelGroupToolsPolicy(e) { const t = resolveChannelGroups(e.cfg, e.channel, e.accountId); const n = [e.groupId, ...Array.isArray(e.groupIdCandidates) ? e.groupIdCandidates : []]; let r; for (const i of n) { const n = i?.trim(); if (!n) continue; r = resolveChannelGroupConfig(t, n, e.groupIdCaseInsensitive); if (r) break; } return resolveScopeToolsPolicy({ ...e, ...buildSelectedGroupScope(r, t?.["*"]), messageProvider: e.messageProvider ?? e.channel }); } //#endregion export { buildChannelGroupsScopeTree as a, resolveScopeKeyCaseInsensitive as c, scopeKey as d, resolveToolsBySender as f, resolveChannelGroupsConfigPath as i, resolveScopeRequireMention as l, resolveChannelGroupRequireMention as n, encodeScopeSegment as o, resolveChannelGroups as p, resolveChannelGroupToolsPolicy as r, resolveScopeIntroHint as s, resolveChannelGroupPolicy as t, resolveScopeToolsPolicy as u };