UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

207 lines (206 loc) • 8.34 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as appendConfigPathSegment } from "./dot-path-BJ4tyWKJ.mjs"; import { o as openRootFileSync } from "./boundary-file-read-BfaFLDqt.mjs"; import { c as getPluginCacheRoot } from "./plugin-cache-B_eRuqTM.mjs"; import { r as pluginCacheExistsSync } from "./plugin-cache-files-e1aGp2KM.mjs"; import { t as shouldRejectHardlinkedPluginFiles } from "./hardlink-policy-Kq1Lxk7_.mjs"; import { n as getOfficialExternalChannelSecretContract, s as listOfficialExternalChannelCatalogEntries } from "./official-external-plugin-catalog-DtkWz1Xo.mjs"; import { o as getOfficialExternalPluginCatalogManifest } from "./official-external-plugin-catalog-source-4o5oPhVN.mjs"; import { n as resolveConfigWidePluginManifestRegistry } from "./io.plugin-metadata-CBwzHw3m.mjs"; import { t as getCachedPluginModuleLoader } from "./plugin-module-loader-cache-DgNX_BTY.mjs"; import { t as loadBundledPluginPublicArtifactModuleFromCandidatesSync } from "./public-surface-loader-DENdCm5a.mjs"; import "./shared-DyBTHA2R.mjs"; import { c as isChannelAccountEffectivelyEnabled, l as isEnabledFlag, r as collectSecretInputAssignment } from "./runtime-shared-BVJ4BbUu.mjs"; import { c as getChannelRecord, i as createChannelAccountSecretOwner, o as createChannelSecretTargetRegistryEntries } from "./channel-secret-basic-runtime-BfMvpRGw.mjs"; import fs from "node:fs"; import { fileURLToPath } from "node:url"; import path from "node:path"; //#region src/secrets/official-external-channel-secret-contract.ts /** Host fallback secret contracts for external channels without contract artifacts. */ function hasActivationValue(e) { if (!e.activationField) return true; if (normalizeOptionalString(e.record[e.activationField])) return true; return Boolean(e.allowEnv && e.activationEnv && normalizeOptionalString(e.env[e.activationEnv])); } function loadOfficialExternalChannelSecretContractApi(e) { const t = getOfficialExternalChannelSecretContract(e); if (!t) return; const n = t.fields.map((e) => e.field); return { secretTargetRegistryEntries: createChannelSecretTargetRegistryEntries({ channelKey: t.channelId, channel: n, account: n }), collectRuntimeConfigAssignments({ config: e, defaults: n, context: r }) { const i = getChannelRecord(e, t.channelId); if (!i) return; for (const e of t.fields) { const a = e.activationEnv ? normalizeOptionalString(r.env[e.activationEnv]) : void 0; if (isEnabledFlag(i) && e.activationField && !normalizeOptionalString(i[e.activationField]) && a) i[e.activationField] = a; const { accounts: o, ...s } = i; const c = Object.fromEntries(Object.entries(s).filter(([e]) => !t.fields.some((t) => e === t.field || e === t.activationField))); const collectAccount = (a, o, c, l) => { const u = c ? `channels.${t.channelId}` : appendConfigPathSegment(`channels.${t.channelId}.accounts`, a); collectSecretInputAssignment({ value: o[e.field], path: `${u}.${e.field}`, expected: "string", defaults: n, context: r, active: (c ? isEnabledFlag(i) : isChannelAccountEffectivelyEnabled(i, o)) && hasActivationValue({ record: o, activationField: e.activationField, activationEnv: e.activationEnv, env: r.env, allowEnv: c }), inactiveReason: `external channel${c ? "" : " account"} is disabled or ${e.activationField ?? "its credential surface"} is not configured.`, owner: createChannelAccountSecretOwner(t.channelId, a, i, c ? s : o, l), apply: (t) => { o[e.field] = t; } }); }; collectAccount("default", i, true, s); const l = isRecord(i.accounts) ? i.accounts : void 0; if (!l) continue; for (const [t, n] of Object.entries(l)) { const r = isRecord(n) ? n : void 0; if (!r || !Object.hasOwn(r, e.field)) continue; collectAccount(t, r, false, { channel: c, account: r }); } } } }; } function listOfficialExternalChannelSecretTargetRegistryEntries() { return listOfficialExternalChannelCatalogEntries().flatMap((e) => { const t = normalizeOptionalString(getOfficialExternalPluginCatalogManifest(e)?.channel?.id); return t ? loadOfficialExternalChannelSecretContractApi(t)?.secretTargetRegistryEntries ?? [] : []; }); } //#endregion //#region src/secrets/channel-contract-api.ts /** Loads channel secret contract APIs from bundled and external plugin artifacts. */ const CONTRACT_API_EXTENSIONS = [ ".js", ".mjs", ".cjs", ".ts", ".mts", ".cts" ]; const CURRENT_MODULE_PATH = fileURLToPath(import.meta.url); const RUNNING_FROM_BUILT_ARTIFACT = CURRENT_MODULE_PATH.includes(`${path.sep}dist${path.sep}`) || CURRENT_MODULE_PATH.includes(`${path.sep}dist-runtime${path.sep}`); function loadBundledChannelSecretContractApi(e) { return loadBundledPluginPublicArtifactModuleFromCandidatesSync({ dirName: e, artifactCandidates: ["secret-contract-api.js"] }) ?? void 0; } function orderedContractApiExtensions() { return RUNNING_FROM_BUILT_ARTIFACT ? CONTRACT_API_EXTENSIONS : [...CONTRACT_API_EXTENSIONS.slice(3), ...CONTRACT_API_EXTENSIONS.slice(0, 3)]; } function resolvePluginContractApiPath(e) { const t = getPluginCacheRoot(e).artifacts; const n = "channel-secret-contract"; const r = t.get(n); if (r !== void 0) return r?.modulePath ?? null; const i = RUNNING_FROM_BUILT_ARTIFACT ? [path.join(e, "dist"), e] : [e, path.join(e, "dist")]; for (const r of ["secret-contract-api", "contract-api"]) for (const a of i) for (const i of orderedContractApiExtensions()) { const o = path.join(a, `${r}${i}`); if (pluginCacheExistsSync(o)) { t.set(n, { modulePath: o, boundaryRoot: e }); return o; } } t.set(n, null); return null; } function loadExternalChannelSecretContractFromRecord(e, t = process.env, n = false) { const r = resolvePluginContractApiPath(e.rootDir); if (!r) return; const i = getPluginCacheRoot(e.rootDir).artifacts; const a = shouldRejectHardlinkedPluginFiles({ origin: e.origin, rootDir: e.rootDir, env: t }); const o = `channel-secret-contract:validated:${a}`; let s = i.get(o); if (s === void 0) { const t = openRootFileSync({ absolutePath: r, rootPath: e.rootDir, boundaryLabel: "plugin root", rejectHardlinks: a, skipLexicalRootCheck: true }); if (t.ok) { fs.closeSync(t.fd); s = { modulePath: t.path, boundaryRoot: e.rootDir }; } else s = null; i.set(o, s); } if (!s) { if (n) throw new Error(`Unable to open channel secret contract for ${e.id}`); return; } try { const e = getCachedPluginModuleLoader({ modulePath: s.modulePath, importerUrl: import.meta.url })(s.modulePath); if (e.collectRuntimeConfigAssignments || e.secretTargetRegistryEntries) return e; } catch (e) { if (n) throw e; } } function recordOwnsChannel(e, t) { return e.channels.includes(t) || Object.hasOwn(e.channelConfigs ?? {}, t) || e.channelCatalogMeta?.id === t || e.packageChannel?.id === t; } function loadChannelSecretContractApi(e) { const t = loadBundledChannelSecretContractApi(e.channelId); if (t || e.bundledOnly) return t; const n = e.env ?? process.env; const r = loadOfficialExternalChannelSecretContractApi(e.channelId); let i; try { const t = e.channelId; const r = e.config; const a = e.loadablePluginOrigins; i = resolveConfigWidePluginManifestRegistry({ config: r, env: n }).plugins.filter((e) => e.origin !== "bundled").filter((e) => recordOwnsChannel(e, t)).filter((e) => !a || a.has(e.id)).toSorted((e, n) => { if (e.id === t && n.id !== t) return -1; if (n.id === t && e.id !== t) return 1; return e.id.localeCompare(n.id); }); } catch (e) { if (r) return r; throw e; } for (const e of i) { const t = loadExternalChannelSecretContractFromRecord(e, n); if (t) return t; } return r; } function loadChannelSecretContractApiForRecord(e, t) { if (e.origin === "bundled") return loadBundledChannelSecretContractApi(e.id); return loadExternalChannelSecretContractFromRecord(e, process.env, t?.throwOnLoadError); } //#endregion export { loadChannelSecretContractApiForRecord as n, listOfficialExternalChannelSecretTargetRegistryEntries as r, loadChannelSecretContractApi as t };