UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

314 lines (313 loc) • 15.1 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs"; import { d as normalizeSortedUniqueTrimmedStringList, y as sortUniqueStrings } from "./string-normalization-DLmfYZXI.mjs"; import { i as parseConcreteConfigPathTokens } from "./dot-path-BJ4tyWKJ.mjs"; import { _ as resolveSecretInputRef } from "./types.secrets-BmNLLnHp.mjs"; import "./session-key-CqAyZeVk.mjs"; import { r as normalizeOptionalAccountId } from "./account-id-BqKT3GuY.mjs"; import { l as getConfigResolutionFacts } from "./resolution-facts-xaIKuREs.mjs"; import { r as matchPathTokens, t as compileTargetRegistryEntry } from "./target-registry-pattern-CmdG152-.mjs"; import { t as loadChannelSecretContractApi } from "./channel-contract-api-BoOS1ajO.mjs"; import { o as listSecretTargetRegistryEntries, r as discoverConfigSecretTargetsByIds } from "./target-registry-query-le6rMGHo.mjs"; import "./target-registry-C0PdHjLu.mjs"; import { a as getActiveSecretsRuntimeConfigSnapshot } from "./runtime-state-Dq_fFDZp.mjs"; import { n as GATEWAY_AUTH_SURFACE_PATHS } from "./secret-input-paths-k_sABTwS.mjs"; import { t as listReadOnlyChannelPluginsForConfig } from "./read-only-idsFCBWl.mjs"; import { n as sortPluginEntriesForAutoDetect } from "./plugin-entry-order-fLLyEg7i.mjs"; import { t as resolvePluginWebFetchProviders } from "./web-fetch-providers.runtime.js"; import { t as resolvePluginWebSearchProviders } from "./web-search-providers.runtime.js"; import { i as resolveChannelDefaultAccountId } from "./helpers-ELZNDAnv.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/cli/command-secret-targets.ts const STATIC_QR_REMOTE_TARGET_IDS = ["gateway.remote.token", "gateway.remote.password"]; const STATIC_MODEL_TARGET_IDS = [ "models.providers.*.apiKey", "models.providers.*.headers.*", "models.providers.*.request.headers.*", "models.providers.*.request.auth.token", "models.providers.*.request.auth.value", "models.providers.*.request.proxy.tls.ca", "models.providers.*.request.proxy.tls.cert", "models.providers.*.request.proxy.tls.key", "models.providers.*.request.proxy.tls.passphrase", "models.providers.*.request.tls.ca", "models.providers.*.request.tls.cert", "models.providers.*.request.tls.key", "models.providers.*.request.tls.passphrase" ]; const STATIC_TTS_TARGET_IDS = [ ...STATIC_MODEL_TARGET_IDS, "agents.entries.*.tts.providers.*.apiKey", "agents.entries.*.tts.personas.*.providers.*.apiKey", "tts.providers.*.apiKey", "tts.personas.*.providers.*.apiKey" ]; const STATIC_MEMORY_TARGET_IDS = [ "memory.search.remote.apiKey", "memory.search.remote.headers.*", "agents.entries.*.memory.search.remote.apiKey", "agents.entries.*.memory.search.remote.headers.*" ]; const STATIC_AGENT_RUNTIME_BASE_TARGET_IDS = [ ...STATIC_TTS_TARGET_IDS, ...STATIC_MEMORY_TARGET_IDS, "skills.entries.*.apiKey" ]; const STATIC_MEMORY_EMBEDDING_TARGET_IDS = [...STATIC_MODEL_TARGET_IDS, ...STATIC_MEMORY_TARGET_IDS]; const STATIC_STATUS_TARGET_IDS = [...GATEWAY_AUTH_SURFACE_PATHS, ...STATIC_MEMORY_TARGET_IDS]; let cachedAgentRuntimeBaseTargetIds; const cachedCapabilityWebTargetIds = {}; let cachedChannelSecretTargetIds; function getChannelSecretTargetIds() { return cachedChannelSecretTargetIds ??= listSecretTargetRegistryEntries().map((e) => e.id).filter((e) => e.startsWith("channels.")).toSorted(); } function pluginWebCredentialConfigPath(e) { const t = e.pathPatternSegments; if (t?.[0] === "plugins" && t[1] === "entries" && t[3] === "config") return t.slice(4).join("."); for (const t of ["webSearch.apiKey", "webFetch.apiKey"]) if (e.id.startsWith("plugins.entries.") && e.id.endsWith(`.config.${t}`)) return t; } function getCapabilityWebTargetIds(e) { return cachedCapabilityWebTargetIds[e] ??= sortUniqueStrings(listSecretTargetRegistryEntries().filter((t) => pluginWebCredentialConfigPath(t) === (e === "search" ? "webSearch.apiKey" : "webFetch.apiKey")).map((e) => e.id)); } function isConfiguredSecretCandidate(e) { if (typeof e === "string") return e.trim().length > 0; return e !== void 0 && e !== null; } function resolveWebProviders(e, t) { return t === "search" ? resolvePluginWebSearchProviders({ config: e }) : resolvePluginWebFetchProviders({ config: e }); } function targetIdsForConfigPath(e) { const t = parseConcreteConfigPathTokens(e); return listSecretTargetRegistryEntries({ configPath: t }).filter((e) => matchPathTokens(t, compileTargetRegistryEntry(e).pathTokens)).map((e) => e.id).toSorted(); } function addConfigPathTargets(e) { const t = targetIdsForConfigPath(e.path); if (t.length === 0) return false; for (const n of t) { e.targetIds.add(n); if (n !== e.path) e.allowedPaths.add(e.path); } e.targetPaths.add(e.path); return true; } function discoverConfiguredTargetPaths(e, t, n) { const r = /* @__PURE__ */ new Set(); for (const i of discoverConfigSecretTargetsByIds(e, t)) { if (n && !n.has(i.path)) continue; r.add(i.path); } return r.size > 0 ? r : void 0; } function addFallbackPathTargets(e) { const t = new Set(e.targetIds); const n = addConfigPathTargets(e); for (const n of e.targetIds) if (!t.has(n)) e.fallbackTargetIds.add(n); if (n) e.fallbackPaths.add(e.path); } function getCapabilityWebAutoDetectTargets(e, t) { const n = new Set(getCapabilityWebTargetIds(t)); const r = new Set(n); const i = /* @__PURE__ */ new Set(); const a = /* @__PURE__ */ new Set(); for (const n of sortPluginEntriesForAutoDetect(resolveWebProviders(e, t))) { if (isConfiguredSecretCandidate(n.getConfiguredCredentialValue?.(e))) break; const t = n.getConfiguredCredentialFallback?.(e); const o = t?.path?.trim(); if (!o || !isConfiguredSecretCandidate(t?.value)) continue; for (const e of targetIdsForConfigPath(o)) { r.add(e); i.add(e); } a.add(o); break; } if (i.size === 0) return { targetIds: r }; const o = discoverConfiguredTargetPaths(e, n); const s = /* @__PURE__ */ new Set([...o ?? [], ...a]); const c = discoverConfiguredTargetPaths(e, i, s); return { targetIds: r, allowedPaths: s, ...c ? { optionalActivePaths: c } : {} }; } function getAgentRuntimeBaseTargetIds() { cachedAgentRuntimeBaseTargetIds ??= [...STATIC_AGENT_RUNTIME_BASE_TARGET_IDS, ...listSecretTargetRegistryEntries().filter((e) => { const t = pluginWebCredentialConfigPath(e); return t === "webSearch.apiKey" || t === "webFetch.apiKey"; }).map((e) => e.id).toSorted()]; return cachedAgentRuntimeBaseTargetIds; } function isScopedChannelSecretTargetEntry(e) { const t = normalizeOptionalString(e.pluginChannelId); if (!t) return false; const n = `channels.${t}.`; return e.entry.id.startsWith(n) && e.entry.configFile === "openclaw.json" && typeof e.entry.pathPattern === "string" && e.entry.pathPattern.startsWith(n) && (e.entry.refPathPattern === void 0 || e.entry.refPathPattern.startsWith(n)); } /** Channel secret targets contributed by channels currently present in config/read-only plugins. */ function getConfiguredChannelsCommandSecretTargetIds(e, t = process.env) { const n = /* @__PURE__ */ new Set(); const r = e.channels; if (r && typeof r === "object" && !Array.isArray(r)) for (const i of Object.keys(r)) { if (i === "defaults") continue; const r = loadChannelSecretContractApi({ channelId: i, config: e, env: t }); for (const e of r?.secretTargetRegistryEntries ?? []) if (isScopedChannelSecretTargetEntry({ entry: e, pluginChannelId: i })) n.add(e.id); } for (const r of listReadOnlyChannelPluginsForConfig(e, { env: t, includePersistedAuthState: false })) for (const e of r.secrets?.secretTargetRegistryEntries ?? []) if (isScopedChannelSecretTargetEntry({ entry: e, pluginChannelId: r.id })) n.add(e.id); return new Set([...n].toSorted((e, t) => e.localeCompare(t))); } function selectChannelTargetIds(e) { const t = getChannelSecretTargetIds(); if (!e) return new Set(t); return new Set(t.filter((t) => t.startsWith(`channels.${e}.`))); } /** Return channel secret targets, optionally narrowed to one channel account subtree. */ function getScopedChannelsCommandSecretTargets(e) { const t = normalizeOptionalString(e.channel); const n = e.channels === void 0 ? void 0 : normalizeSortedUniqueTrimmedStringList(e.channels); const r = n === void 0 ? selectChannelTargetIds(t) : new Set(n.flatMap((e) => [...selectChannelTargetIds(e)])); const i = normalizeOptionalAccountId(e.accountId); const a = t && !i && e.defaultAccountWhenMissing ? listReadOnlyChannelPluginsForConfig(e.config, { includePersistedAuthState: false }).find((e) => e.id === t) : void 0; const o = i ?? (a ? normalizeOptionalAccountId(resolveChannelDefaultAccountId({ plugin: a, cfg: e.config })) : void 0); const s = n ?? (t ? [t] : []); if (s.length === 0 || !o) return { targetIds: r }; const c = /* @__PURE__ */ new Set(); for (const t of discoverConfigSecretTargetsByIds(e.config, r)) if (s.some((e) => { const [n, r, i, a] = t.pathSegments; if (n !== "channels" || r !== e) return false; if (i !== "accounts") return true; return normalizeOptionalAccountId(a) === o; })) c.add(t.path); return { targetIds: r, allowedPaths: c }; } /** Secret targets needed by QR remote pairing flows. */ function getQrRemoteCommandSecretTargetIds() { return new Set(STATIC_QR_REMOTE_TARGET_IDS); } /** All registered channel secret targets, regardless of current config. */ function getChannelsCommandSecretTargetIds() { return new Set(getChannelSecretTargetIds()); } /** Model-provider credential targets used by commands that can touch provider config. */ function getModelsCommandSecretTargetIds() { return new Set(STATIC_MODEL_TARGET_IDS); } function getSelectedMemoryProviderSecretTargetIds(e) { return discoverConfigSecretTargetsByIds(e).filter(({ pathSegments: t }) => t[0] === "plugins" && t[2] === e.plugins?.slots?.memory).map(({ entry: e }) => e.id); } /** Credential targets required by memory embeddings and the selected memory plugin. */ function getMemoryEmbeddingCommandSecretTargetIds(e) { return /* @__PURE__ */ new Set([...STATIC_MEMORY_EMBEDDING_TARGET_IDS, ...e?.plugins?.slots?.memory ? getSelectedMemoryProviderSecretTargetIds(e) : []]); } /** Credential targets required by text-to-speech flows. */ function getTtsCommandSecretTargetIds() { return new Set(STATIC_TTS_TARGET_IDS); } /** Agent startup targets not already owned by its prepared secrets snapshot. */ function getAgentRuntimeCommandSecretTargetIds(e) { const t = getActiveSecretsRuntimeConfigSnapshot(); if (t?.configRefsPrepared && (e.config === t.config || getConfigResolutionFacts(e.config) === getConfigResolutionFacts(t.config) && isDeepStrictEqual(e.config, t.config))) return e.includeChannelTargets ? getChannelsCommandSecretTargetIds() : /* @__PURE__ */ new Set(); if (e.includeChannelTargets !== true) return new Set(getAgentRuntimeBaseTargetIds()); return /* @__PURE__ */ new Set([...getAgentRuntimeBaseTargetIds(), ...getChannelSecretTargetIds()]); } /** * Web credentials are needed only if the model invokes the corresponding tool. * Keep them materializable for agent runs without making tool-owner outages block turn startup. */ function getAgentRuntimeOptionalCommandSecretPaths(e) { const t = /* @__PURE__ */ new Set([...getCapabilityWebTargetIds("search"), ...getCapabilityWebTargetIds("fetch")]); const n = e.secrets?.defaults; return new Set(discoverConfigSecretTargetsByIds(e, t).filter((e) => Boolean(resolveSecretInputRef({ value: e.value, refValue: e.refValue, defaults: n }).ref)).map((e) => e.path)); } function getCapabilityWebCommandSecretTargets(e, t, n) { const r = asOptionalRecord(e.tools?.web?.[t]); if (r?.enabled === false) return { targetIds: new Set(getCapabilityWebTargetIds(t)) }; const i = normalizeOptionalLowercaseString(n) ?? normalizeOptionalLowercaseString(r?.provider); if (!i) return getCapabilityWebAutoDetectTargets(e, t); const a = { targetIds: /* @__PURE__ */ new Set(), targetPaths: /* @__PURE__ */ new Set(), allowedPaths: /* @__PURE__ */ new Set(), fallbackTargetIds: /* @__PURE__ */ new Set(), fallbackPaths: /* @__PURE__ */ new Set() }; const o = structuredClone(e); const s = (o.tools ??= {}).web ??= {}; s[t] = { ...asOptionalRecord(s[t]), provider: i }; const c = resolveWebProviders(o, t).filter((e) => e.id === i); for (const n of c) { if (n.credentialPath.trim()) addConfigPathTargets({ ...a, path: n.credentialPath }); if (isConfiguredSecretCandidate(n.getConfiguredCredentialValue?.(e))) continue; const r = n.getConfiguredCredentialFallback?.(e)?.path?.trim(); if (r) addFallbackPathTargets({ ...a, path: r }); const o = t === "search" ? i === "gemini" ? "models.providers.google.apiKey" : i === "ollama" ? "models.providers.ollama.apiKey" : void 0 : void 0; if (o && !a.fallbackPaths.has(o) && discoverConfiguredTargetPaths(e, new Set(targetIdsForConfigPath(o)))?.has(o)) addFallbackPathTargets({ ...a, path: o }); } const l = { ...a, matchedProvider: c.length > 0 }; if (!l.matchedProvider && !n) return getCapabilityWebAutoDetectTargets(e, t); const u = new Set([...l.targetIds].toSorted()); const d = l.allowedPaths.size > 0 ? new Set([...l.targetPaths].toSorted()) : void 0; const f = discoverConfiguredTargetPaths(e, n ? u : new Set([...l.fallbackTargetIds].toSorted()), d); return { targetIds: u, ...d ? { allowedPaths: d } : {}, ...f ? { forcedActivePaths: f } : {} }; } /** Web-fetch target scope for selected/auto-detected providers and configured fallback paths. */ function getCapabilityWebFetchCommandSecretTargets(e, t) { return getCapabilityWebCommandSecretTargets(e, "fetch", t?.providerId); } /** Web-search target scope for selected/auto-detected providers and configured fallback paths. */ function getCapabilityWebSearchCommandSecretTargets(e, t) { return getCapabilityWebCommandSecretTargets(e, "search", t?.providerId); } /** Status command targets; channel targets can be limited to configured channel plugins. */ function getStatusCommandSecretTargetIds(e, t) { const n = e ? getConfiguredChannelsCommandSecretTargetIds(e, t) : getChannelSecretTargetIds(); return /* @__PURE__ */ new Set([...STATIC_STATUS_TARGET_IDS, ...n]); } /** Secret targets that the security audit command is allowed to inspect. */ function getSecurityAuditCommandSecretTargetIds() { return /* @__PURE__ */ new Set([...GATEWAY_AUTH_SURFACE_PATHS, ...getChannelSecretTargetIds()]); } //#endregion export { getChannelsCommandSecretTargetIds as a, getModelsCommandSecretTargetIds as c, getSecurityAuditCommandSecretTargetIds as d, getStatusCommandSecretTargetIds as f, getCapabilityWebSearchCommandSecretTargets as i, getQrRemoteCommandSecretTargetIds as l, getAgentRuntimeOptionalCommandSecretPaths as n, getConfiguredChannelsCommandSecretTargetIds as o, getTtsCommandSecretTargetIds as p, getCapabilityWebFetchCommandSecretTargets as r, getMemoryEmbeddingCommandSecretTargetIds as s, getAgentRuntimeCommandSecretTargetIds as t, getScopedChannelsCommandSecretTargets as u };