UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

381 lines (380 loc) • 16.7 kB
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs"; import { u as normalizeSortedUniqueStringEntries } from "./string-normalization-DLmfYZXI.mjs"; import { i as resolveConfigEnvVars } from "./env-substitution-CgVeOqUd.mjs"; import "./utils-C8YECn2Q.mjs"; import { i as getPluginRegistryForContext } from "./gateway-request-scope-BvvDhs5D.mjs"; import { n as getPluginRuntimeLoadContextState } from "./load-context-state-Cj2HcQlf.mjs"; import { a as getPluginCache } from "./plugin-cache-B_eRuqTM.mjs"; import { a as createConfigRuntimeEnv } from "./config-env-vars-ByeGN-9N.mjs"; import { f as resolvePluginRuntimeArtifactPreference } from "./bundled-dir-BKvdAqkP.mjs"; import { C as normalizePluginsConfigWithResolverCore, b as withContextEngineOwner, h as resolveEligibleContextEngineOwnerIds, m as resolveEligibleContextEngineDeclaredOwners, r as createPluginActivationSource, t as applyTestPluginDefaults, u as normalizePluginsConfig } from "./config-state-B1JlHgba.mjs"; import { p as extractPluginInstallRecordsFromInstalledPluginIndex } from "./installed-plugin-index-DYqse_gE.mjs"; import { r as getRuntimeConfigCapture } from "./runtime-config-capture-state-CvRIlfib.mjs"; import { i as loadInstalledPluginIndexInstallRecordsSync } from "./installed-plugin-index-record-reader-B-7uGQZ9.mjs"; import { n as resolvePluginDiscoveryContext } from "./plugin-control-plane-context-BmY6xICT.mjs"; import { i as getCurrentPluginMetadataSnapshot, p as normalizePluginIdScope } from "./current-plugin-metadata-snapshot-H0TP3PFt.mjs"; import { c as getPluginLoaderCacheState } from "./registry-lifecycle-CJjWnl-y.mjs"; import { f as getActivePluginRegistryWorkspaceDir, l as getActivePluginRegistry, u as getActivePluginRegistryKey } from "./runtime-ezCEpumg.mjs"; import { n as getPluginRegistryRuntime } from "./registry-runtime-binding-B8M1f2m4.mjs"; import { s as matchesPluginRuntimeArtifactSelection } from "./plugin-runtime-artifact-binding-BHK81c8H.mjs"; import { t as captureRuntimeConfig } from "./runtime-source-projection-DTwMWCo9.mjs"; import { t as resolvePluginActivationSourceConfig } from "./activation-source-config-BX9rE6k7.mjs"; import "./env-vars-DN2VAsXE.mjs"; import "./installed-plugin-index-records-MNCNSlg8.mjs"; import { s as resolvePluginRegistrationConfigKey, t as activationConfigFingerprint } from "./load-context-CwTU1iXB.mjs"; import { createHash } from "node:crypto"; //#region src/plugins/loader-load-context.ts const runtimeBindingCacheIds = /* @__PURE__ */ new WeakMap(); let nextRuntimeBindingCacheId = 1; function resolveRuntimeBindingCacheId(e) { if (!e) return; const t = runtimeBindingCacheIds.get(e); if (t !== void 0) return t; const n = nextRuntimeBindingCacheId++; runtimeBindingCacheIds.set(e, n); return n; } function buildActivationMetadataHash(e) { const t = Object.entries(e.activationSource.rootConfig?.channels ?? {}).flatMap(([e, t]) => { if (!t || typeof t !== "object" || Array.isArray(t)) return []; const n = t.enabled; return typeof n === "boolean" ? [[e, n]] : []; }).toSorted(([e], [t]) => e.localeCompare(t)); const n = Object.entries(e.activationSource.plugins.entries).map(([e, { enabled: t }]) => [e, t]).toSorted(([e], [t]) => e.localeCompare(t)); const r = Object.entries(e.autoEnabledReasons).map(([e, t]) => [e, [...t]]).toSorted(([e], [t]) => e.localeCompare(t)); return createHash("sha256").update(JSON.stringify({ enabled: e.activationSource.plugins.enabled, allow: e.activationSource.plugins.allow, deny: e.activationSource.plugins.deny, memorySlot: e.activationSource.plugins.slots.memory, contextEngineSlot: e.activationSource.plugins.slots.contextEngine, entries: n, channelEnablement: t, autoEnabledReasons: r })).digest("hex"); } function resolveRuntimeSubagentMode(e) { if (e?.allowGatewaySubagentBinding === true) return "gateway-bindable"; return e?.subagent ? "explicit" : "default"; } function resolveCoreGatewayMethodNames(e) { return [.../* @__PURE__ */ new Set([...e.coreGatewayMethodNames ?? [], ...Object.keys(e.coreGatewayHandlers ?? {})])].toSorted(); } function mergePluginTrustList(e, t) { if (e === t || t.length === 0) return e; const n = [...e]; const r = new Set(n); for (const e of t) if (!r.has(e)) { n.push(e); r.add(e); } return n.length === e.length ? e : n; } function mergeTrustPluginConfigFromActivationSource(e) { const t = e.activationSource.plugins; const n = mergePluginTrustList(e.normalized.allow, t.allow); const r = mergePluginTrustList(e.normalized.deny, t.deny); const i = mergePluginTrustList(e.normalized.loadPaths, t.loadPaths); if (n === e.normalized.allow && r === e.normalized.deny && i === e.normalized.loadPaths) return e.normalized; return { ...e.normalized, allow: n, deny: r, loadPaths: i }; } function resolvePluginLoadCacheContext(e = {}) { const t = getPluginLoaderCacheState(); const n = e.resolveRawConfigEnvVars === true; const r = e.env ?? process.env; const i = e.config ?? {}; const a = resolvePluginActivationSourceConfig({ config: e.config, activationSourceConfig: e.activationSourceConfig }); const o = n ? createConfigRuntimeEnv(i, r) : r; const s = applyTestPluginDefaults(n ? resolveConfigEnvVars(i, o, { onMissing: () => void 0 }) : i, o); const c = n ? resolveConfigEnvVars(a, o, { onMissing: () => void 0 }) : a; const l = e.normalizePluginId ? (t) => normalizePluginsConfigWithResolverCore(t, e.normalizePluginId) : normalizePluginsConfig; const u = l(s.plugins); const d = createPluginActivationSource({ config: c, plugins: s.plugins === c.plugins ? u : l(c.plugins) }); const f = mergeTrustPluginConfigFromActivationSource({ normalized: u, activationSource: d }); const p = normalizePluginIdScope(e.onlyPluginIds); const m = e.includeSetupOnlyChannelPlugins === true; const h = e.forceSetupOnlyChannelPlugins === true; const g = e.channelPluginLoadIntent ?? "full"; const _ = resolvePluginRuntimeArtifactPreference(e.preferBuiltPluginArtifacts); const v = resolveRuntimeSubagentMode(e.runtimeOptions); const y = v === "gateway-bindable" && e.mode !== "cli-metadata" && (!e.runtimeOptions?.nodes || !e.runtimeOptions?.subagent) ? getActivePluginRegistry() : void 0; const b = y ? getPluginRegistryRuntime(y) : void 0; const x = resolveCoreGatewayMethodNames(e); const S = e.metadataSnapshot ?? (e.installRecords === void 0 && f.loadPaths === u.loadPaths && !n && (e.env === void 0 || e.env === process.env) ? getCurrentPluginMetadataSnapshot({ config: i, env: o, workspaceDir: e.workspaceDir }) ?? (p !== void 0 ? getCurrentPluginMetadataSnapshot({ config: i, env: o, workspaceDir: e.workspaceDir, pluginIds: p }) : void 0) : void 0); const C = { ...e.installRecords ?? (S ? extractPluginInstallRecordsFromInstalledPluginIndex(S.index) : loadInstalledPluginIndexInstallRecordsSync({ env: o })), ...s.plugins?.installs }; const w = resolvePluginDiscoveryContext({ workspaceDir: e.workspaceDir, loadPaths: f.loadPaths, env: o }); const T = resolvePluginRegistrationConfigKey({ runtimeEntries: u.entries, sourceEntries: d.plugins.entries }); const E = e.mode !== "cli-metadata" && e.activate !== false; const D = e.runtimeSideEffects ?? E; const O = e.registrationConfigOrigin; const k = !E && O && getRuntimeConfigCapture(O) ? resolveRuntimeBindingCacheId(O) : void 0; const A = e.manifestRegistry ?? (e.discovery === void 0 ? S?.manifestRegistry : void 0); const { roots: j, loadPaths: M, devSourceRoot: N } = w; const P = Object.fromEntries(Object.entries(C).map(([e, t]) => [e, { ...t, installPath: typeof t.installPath === "string" ? resolveUserPath(t.installPath, o) : t.installPath, sourcePath: typeof t.sourcePath === "string" ? resolveUserPath(t.sourcePath, o) : t.sourcePath }])); const F = { metadataSnapshot: resolveRuntimeBindingCacheId(S), roots: j, devSourceRoot: N, plugins: { ...f, loadPaths: M, entries: Object.entries(f.entries).map(([e, t]) => [e, t.enabled]) }, registrationConfigKey: T, installs: P, discoverySources: (e.manifestRegistry ? void 0 : e.discovery)?.candidates.map((e) => [ e.effectivePluginId ?? e.idHint, e.origin, e.rootDir, e.source, e.setupSource, e.sourcePreferred, e.configSelected, e.packageManifest?.build?.bundledDist ]), activationMetadataKey: buildActivationMetadataHash({ activationSource: d, autoEnabledReasons: e.autoEnabledReasons ?? {} }), capabilityCatalogIdentity: e.capabilityCatalog ? JSON.stringify([ e.capabilityCatalog.family, resolveRuntimeBindingCacheId(e.capabilityCatalog.context), resolveRuntimeBindingCacheId(getPluginCache()), resolveRuntimeBindingCacheId(getPluginRegistryForContext() ?? void 0) ]) : void 0, allowProcessHomeSessionCatalogs: e.allowProcessHomeSessionCatalogs !== false, onlyPluginIds: p, includeSetupOnlyChannelPlugins: m, forceSetupOnlyChannelPlugins: h, channelPluginLoadIntent: g, artifactPreference: _, resolveRawConfigEnvVars: e.resolveRawConfigEnvVars === true, loadModules: e.loadModules !== false, toolDiscovery: e.toolDiscovery === true, runtimeSubagentMode: v, runtimeBindingIdentity: JSON.stringify({ capabilityCatalogContext: resolveRuntimeBindingCacheId(e.capabilityCatalogContext), modelAuth: resolveRuntimeBindingCacheId(e.runtimeOptions?.modelAuth), modelConfig: resolveRuntimeBindingCacheId(e.runtimeOptions?.modelConfig), nodes: resolveRuntimeBindingCacheId(e.runtimeOptions?.nodes), subagent: resolveRuntimeBindingCacheId(e.runtimeOptions?.subagent), borrowedGatewayRuntime: E ? void 0 : resolveRuntimeBindingCacheId(b) }), pluginSdkResolution: e.pluginSdkResolution ?? "auto", coreGatewayMethodNames: x, activate: E, runtimeSideEffects: D, mode: e.mode ?? "full", expectedSourceDigests: e.expectedSourceDigests ? Object.entries(e.expectedSourceDigests).toSorted(([e], [t]) => e.localeCompare(t)) : void 0 }; const I = k ? [activationConfigFingerprint(s), activationConfigFingerprint(c)] : void 0; const L = JSON.stringify(F); const R = I ? JSON.stringify({ ...F, roots: { ...j, workspace: void 0 }, registrationConfigOrigin: k, registrationSnapshots: I }) : L; const resolveManifestCacheKey = (e) => createHash("sha256").update(e ? R : L).update(JSON.stringify(e?.plugins.map((e) => [ e.id, e.contextEngineIds, e.origin, e.rootDir, e.source, e.setupSource, e.providerDiscoverySource, e.capabilityCatalogSource, e.sourcePreferred, e.packageManifest?.build?.bundledDist ])) ?? "").digest("hex"); const z = resolveManifestCacheKey(A); let B; const capture = () => { if (!B) { const e = captureRuntimeConfig(s); if (k !== void 0 && e !== s) runtimeBindingCacheIds.set(e, k); const t = c === s ? e : captureRuntimeConfig(c); const n = l(e.plugins); const r = createPluginActivationSource({ config: t, plugins: e.plugins === t.plugins ? n : l(t.plugins) }); B = { cfg: e, activationSourceConfig: t, activationSource: r, normalized: mergeTrustPluginConfigFromActivationSource({ normalized: n, activationSource: r }) }; } return B; }; return { cacheState: t, env: o, get cfg() { return capture().cfg; }, registrationConfigKey: T, metadataSnapshot: S, get normalized() { return capture().normalized; }, get activationSourceConfig() { return capture().activationSourceConfig; }, get activationSource() { return capture().activationSource; }, /** Admits the selected engine's unique eligible owner before any plugin module imports. */ selectContextEngineOwner(e) { const t = S?.registryIndex.plugins.map((e) => ({ id: e.pluginId, contextEngineIds: e.contextEngineIds })) ?? e; const n = capture(); const r = n.normalized.slots.contextEngine; const i = resolveEligibleContextEngineOwnerIds(n.normalized, r, t); if (i.length > 1) { const e = resolveEligibleContextEngineDeclaredOwners(n.normalized, r, t).pluginIds.length === i.length ? "declared" : "approved"; throw new Error(`Context engine "${r}" has ambiguous ${e} owners: ${i.toSorted().join(", ")}. Select an engine with a unique plugin owner.`); } n.normalized = withContextEngineOwner(n.normalized, t); n.activationSource = { ...n.activationSource, plugins: withContextEngineOwner(n.activationSource.plugins, t) }; const a = i[0]; return r && (n.normalized.contextEngineOwnerId || t.some((e) => e.contextEngineIds?.includes(r))) ? Object.freeze({ engineId: r, owner: a ? `plugin:${a}` : null }) : void 0; }, autoEnabledReasons: e.autoEnabledReasons ?? {}, onlyPluginIds: p, includeSetupOnlyChannelPlugins: m, forceSetupOnlyChannelPlugins: h, channelPluginLoadIntent: g, artifactPreference: _, shouldActivate: E, runtimeSideEffects: D, shouldLoadModules: e.loadModules !== false, runtimeSubagentMode: v, borrowedGatewayRuntime: b, installRecords: C, devSourceRoot: w.devSourceRoot, cacheKey: z, resolveManifestCacheKey }; } //#endregion //#region src/plugins/active-runtime-registry.ts /** Return the exact active registry without triggering a fresh load on cache miss. */ function resolveCompatibleRuntimePluginRegistry(e) { const t = getActivePluginRegistry() ?? void 0; if (!t || e === void 0) return t; const n = getActivePluginRegistryKey(); if (!n) return; const r = resolvePluginLoadCacheContext(e).cacheKey; if (r === n) return t; const i = getPluginRuntimeLoadContextState(t)?.loaderCacheIdentity; return i && (i.requestKey === n && i.resolvedKey === r || i.resolvedKey === n && i.requestKey === r) ? t : void 0; } function isRuntimePluginRecordLoaded(e) { return e.status === "loaded" && (e.format === "bundle" || e.imported !== false); } /** Lists runtime-loaded plugin ids from an immutable/request-scoped registry handle. */ function listRuntimePluginIdsFromRegistry(e) { return normalizeSortedUniqueStringEntries(e.plugins.filter(isRuntimePluginRecordLoaded).map((e) => e.id)); } function listLoadedRuntimePluginIds() { const e = getActivePluginRegistry(); return e ? listRuntimePluginIdsFromRegistry(e) : []; } function registryContainsRuntimePluginIds(e, t) { if (t === void 0) return true; if (t.length === 0 && e.plugins.length > 0) return false; const n = new Set(t); for (const t of e.plugins) if (t.status === void 0 || isRuntimePluginRecordLoaded(t)) n.delete(t.id); if (t.length > 0 && n.size === 0) return true; if (e.plugins.some((e) => n.has(e.id))) return false; for (const [r, i] of Object.entries(e)) { if (r === "diagnostics" || r === "channelSetups" || !Array.isArray(i)) continue; for (const e of i) if (e && typeof e === "object" && "pluginId" in e) { const r = e.pluginId; if (typeof r === "string" && r.length > 0) { if (t.length === 0) return false; n.delete(r); if (n.size === 0) return true; } } } return t.length === 0; } /** Indexes selected owners; omitted artifact preference retains the loaded owner's policy. */ function createRuntimePluginManifestLookup(e, t, n) { const r = new Map(e.plugins.filter(isRuntimePluginRecordLoaded).map((e) => [e.id, e])); const i = new Map(t.toReversed().map((e) => [e.id, e])); return (e) => { const t = r.get(e); const a = i.get(e); return t && a && t.origin === a.origin && matchesPluginRuntimeArtifactSelection(t, a, n) ? t : void 0; }; } function getLoadedRuntimePluginRegistry(e = {}) { const t = e.requiredPluginIds ?? e.loadOptions?.onlyPluginIds; const n = t === void 0 ? void 0 : normalizeSortedUniqueStringEntries(t); if (e.loadOptions && n === void 0) return resolveCompatibleRuntimePluginRegistry(e.loadOptions); const r = getActivePluginRegistryWorkspaceDir(); const i = e.workspaceDir ?? e.loadOptions?.workspaceDir; if ((Object.hasOwn(e, "workspaceDir") || e.loadOptions || i !== void 0) && r !== i) return; const a = getActivePluginRegistry(); if (!a) return; if (!registryContainsRuntimePluginIds(a, n) || e.loadOptions?.manifestRegistry && n !== void 0 && !n.every(createRuntimePluginManifestLookup(a, e.loadOptions.manifestRegistry.plugins, e.loadOptions.preferBuiltPluginArtifacts))) return; if (e.loadOptions?.discovery && !e.loadOptions.manifestRegistry) return resolveCompatibleRuntimePluginRegistry(e.loadOptions); return a; } //#endregion export { registryContainsRuntimePluginIds as a, resolveRuntimeSubagentMode as c, listRuntimePluginIdsFromRegistry as i, getLoadedRuntimePluginRegistry as n, resolveCompatibleRuntimePluginRegistry as o, listLoadedRuntimePluginIds as r, resolvePluginLoadCacheContext as s, createRuntimePluginManifestLookup as t };