UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

285 lines (284 loc) • 10.1 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as appendConfigPathSegment } from "./dot-path-BJ4tyWKJ.mjs"; import { a as coerceSecretRef } from "./types.secrets-BmNLLnHp.mjs"; import { n as normalizeAccountId } from "./account-id-BqKT3GuY.mjs"; import "./shared-DyBTHA2R.mjs"; import { c as isChannelAccountEffectivelyEnabled, l as isEnabledFlag, r as collectSecretInputAssignment, s as hasOwnProperty } from "./runtime-shared-BVJ4BbUu.mjs"; //#region src/secrets/channel-secret-basic-runtime.ts /** Basic channel secret runtime helpers for account/root credential collection. */ function buildChannelSecretTargetRegistryEntry(e) { const t = typeof e.spec === "string" ? { path: e.spec } : e.spec; const n = e.scope === "account" ? `channels.${e.channelKey}.accounts.*` : `channels.${e.channelKey}`; const r = `${n}.${t.path}`; return { id: r, targetType: t.targetType ?? r, ...t.targetTypeAliases ? { targetTypeAliases: t.targetTypeAliases } : {}, configFile: "openclaw.json", pathPattern: r, ...t.refPath ? { refPathPattern: `${n}.${t.refPath}` } : {}, secretShape: t.secretShape ?? "secret_input", expectedResolvedValue: t.expectedResolvedValue ?? "string", includeInPlan: true, includeInConfigure: true, includeInAudit: true, ...t.accountIdPathSegmentIndex !== void 0 ? { accountIdPathSegmentIndex: t.accountIdPathSegmentIndex } : {} }; } function createChannelSecretTargetRegistryEntries(e) { const entriesForScope = (t) => (e[t] ?? []).map((n) => buildChannelSecretTargetRegistryEntry({ channelKey: e.channelKey, scope: t, spec: n })); return [...entriesForScope("account"), ...entriesForScope("channel")]; } /** Keeps registry discovery and account-surface resolution shared while channels own activation. */ function createChannelSecretContract(e) { return { secretTargetRegistryEntries: createChannelSecretTargetRegistryEntries(e), collectRuntimeConfigAssignments(t) { const n = getChannelRecord(t.config, e.channelKey); if (!n) return; e.collect({ ...t, channelKey: e.channelKey, channel: n, surface: resolveChannelAccountSurface(n, e.hasImplicitDefaultAccount?.({ ...t, channel: n })), defaults: t.defaults }); } }; } /** Builds the common registry and runtime collector used by simple channel secrets. */ function createSimpleChannelSecretContract(e) { const t = createChannelSecretTargetRegistryEntries({ channelKey: e.channelKey, account: e.accountFields, channel: e.channelFields }); const n = typeof e.mode === "object" ? e.mode.collectionFields : [.../* @__PURE__ */ new Set([...e.accountFields, ...e.channelFields])]; const collectRuntimeConfigAssignments = (t) => { const r = e.mode === "channel-only"; const i = getChannelRecord(t.config, e.channelKey); if (!i) return; if (r) { for (const r of n) collectSecretInputAssignment({ value: i[r], path: `channels.${e.channelKey}.${r}`, expected: "string", defaults: t.defaults, context: t.context, active: i.enabled !== false, inactiveReason: `${e.label} channel is disabled.`, owner: { ownerKind: "account", ownerId: `${e.channelKey}:default`, requiredForGateway: false, disposition: "isolate", contract: i }, apply: (e) => { i[r] = e; } }); return; } const a = resolveChannelAccountSurface(i); for (const r of n) { const n = e.mode === "channel-surface" ? `${e.label} channel is disabled.` : e.mode === "account-inheritance" ? `no enabled account inherits this top-level ${e.label} ${r}.` : `no enabled ${e.label} surface inherits this top-level ${r}.`; collectSimpleChannelFieldAssignments({ channelKey: e.channelKey, field: r, channel: i, surface: a, defaults: t.defaults, context: t.context, topInactiveReason: n, accountInactiveReason: e.mode === "channel-surface" ? `${e.label} channel is disabled.` : `${e.label} account is disabled.` }); } }; return { secretTargetRegistryEntries: t, collectRuntimeConfigAssignments }; } /** Stable owner identity shared by SecretRef collection and channel activation. */ function createChannelAccountSecretOwner(e, t, n, r, i) { const { accounts: a, ...o } = n; return { ownerKind: "account", ownerId: `${e}:${normalizeAccountId(t)}`, requiredForGateway: false, disposition: "isolate", contract: i ?? { channel: o, account: r } }; } /** Reads a channel config block when it exists as an object. */ function getChannelRecord(e, t) { const n = e.channels; if (!isRecord(n)) return; const r = n[t]; return isRecord(r) ? r : void 0; } /** Reads a channel config and its resolved account surface in one step. */ function getChannelSurface(e, t) { const n = getChannelRecord(e, t); if (!n) return null; return { channel: n, surface: resolveChannelAccountSurface(n) }; } /** Resolves explicit channel accounts or creates a default account backed by the channel root. */ function resolveChannelAccountSurface(e, t = false) { const n = isEnabledFlag(e); const r = e.accounts; if (!isRecord(r) || Object.keys(r).length === 0) return { hasExplicitAccounts: false, channelEnabled: n, accounts: [{ accountId: "default", account: e, enabled: n }] }; const i = []; for (const [t, n] of Object.entries(r)) { if (!isRecord(n)) continue; i.push({ accountId: t, account: n, enabled: isChannelAccountEffectivelyEnabled(e, n) }); } if (t && !Object.keys(r).some((e) => normalizeAccountId(e) === "default")) i.push({ accountId: "default", account: {}, enabled: n }); return { hasExplicitAccounts: true, channelEnabled: n, accounts: i }; } function isBaseFieldActiveForChannelSurface(e, t) { if (!e.channelEnabled) return false; if (!e.hasExplicitAccounts) return true; return e.accounts.some(({ account: e, enabled: n }) => n && !hasOwnProperty(e, t)); } /** Normalizes optional channel secret strings before deciding whether a value is configured. */ function normalizeSecretStringValue(e) { return typeof e === "string" ? e.trim() : ""; } /** Returns true when a channel value contains plaintext or a SecretRef-compatible value. */ function hasConfiguredSecretInputValue(e, t) { return normalizeSecretStringValue(e).length > 0 || coerceSecretRef(e, t) !== null; } /** Collects root/account channel field SecretRef assignments for one credential path. */ function collectSimpleChannelFieldAssignments(e) { collectConditionalChannelFieldAssignments({ ...e, topLevelActiveWithoutAccounts: true, topLevelInheritedAccountActive: ({ account: t, enabled: n }) => n && !hasOwnProperty(t, e.field), accountActive: ({ enabled: e }) => e }); } /** Collects a channel field whose active state depends on caller-provided account predicates. */ function collectConditionalChannelFieldAssignments(e) { collectChannelFieldAssignments({ ...e, nestedKey: void 0, topLevelActiveWithoutAccounts: e.surface.channelEnabled && e.topLevelActiveWithoutAccounts }); } /** Collects a nested channel field from root and account-specific nested config blocks. */ function collectNestedChannelFieldAssignments(e) { collectChannelFieldAssignments({ ...e, topLevelActiveWithoutAccounts: e.topLevelActive, topLevelInheritedAccountActive: e.topLevelInheritedAccountActive ?? (({ account: t, enabled: n }) => e.topLevelActive && n && !hasOwnProperty(t, e.nestedKey)) }); } function collectChannelFieldAssignments(e) { const t = e.nestedKey === void 0 ? e.field : `${e.nestedKey}.${e.field}`; const targetFor = (t) => { const n = e.nestedKey === void 0 ? t : t[e.nestedKey]; return isRecord(n) ? n : void 0; }; const n = targetFor(e.channel); if (n) { const { channelKey: r, channel: i } = e; const a = n[e.field]; const o = `channels.${e.channelKey}.${t}`; const { surface: s, defaults: c, context: l } = e; const u = e.topLevelActiveWithoutAccounts; const d = e.topLevelInheritedAccountActive; const f = e.topInactiveReason; const apply = (t) => { n[e.field] = t; }; const p = s.hasExplicitAccounts ? s.accounts.filter(d) : u ? [{ accountId: "default", account: {}, enabled: true }] : []; if (p.length === 0) collectSecretInputAssignment({ value: a, path: o, expected: "string", defaults: c, context: l, active: false, inactiveReason: f, apply }); else { const { accounts: e, ...t } = i; const n = { channel: t, consumers: p.map(({ accountId: e, account: t }) => ({ accountId: normalizeAccountId(e), account: t })).toSorted((e, t) => e.accountId.localeCompare(t.accountId)) }; for (const { accountId: e, account: t } of p) collectSecretInputAssignment({ value: a, path: o, expected: "string", defaults: c, context: l, owner: createChannelAccountSecretOwner(r, e, i, t, n), apply }); } } if (!e.surface.hasExplicitAccounts) return; for (const n of e.surface.accounts) { const r = targetFor(n.account); if (!r || e.nestedKey === void 0 && !hasOwnProperty(r, e.field)) continue; collectSecretInputAssignment({ value: r[e.field], path: `${appendConfigPathSegment(`channels.${e.channelKey}.accounts`, n.accountId)}.${t}`, expected: "string", defaults: e.defaults, context: e.context, active: e.accountActive(n), inactiveReason: typeof e.accountInactiveReason === "function" ? e.accountInactiveReason(n) : e.accountInactiveReason, owner: createChannelAccountSecretOwner(e.channelKey, n.accountId, e.channel, n.account), apply: (t) => { r[e.field] = t; } }); } } //#endregion export { createChannelSecretContract as a, getChannelRecord as c, isBaseFieldActiveForChannelSurface as d, normalizeSecretStringValue as f, createChannelAccountSecretOwner as i, getChannelSurface as l, collectNestedChannelFieldAssignments as n, createChannelSecretTargetRegistryEntries as o, resolveChannelAccountSurface as p, collectSimpleChannelFieldAssignments as r, createSimpleChannelSecretContract as s, collectConditionalChannelFieldAssignments as t, hasConfiguredSecretInputValue as u };