UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

266 lines (265 loc) • 13.5 kB
import { d as normalizeSortedUniqueTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs"; import { i as getPluginExecutionFrame, o as runWithPluginExecutionFrame, r as createPluginExecutionFrame } from "./plugin-instance-invocation-Bq_toGDw.mjs"; import { t as listAgentWorkspaceDirs } from "./workspace-dirs-Dba0CE04.mjs"; import { v as resolveImmutableInstallIdentity } from "./discovery-DvJYrQFY.mjs"; import { S as runOutsidePluginCache, d as getProcessPluginCache, f as getScopedPluginCache, m as invalidatePluginCacheMetadata, p as getScopedPluginCaches, u as getPluginMetadataSnapshotCache, w as withPluginCache } from "./plugin-cache-B_eRuqTM.mjs"; import { c as setCurrentPluginMetadataSnapshotState, i as getGatewayPluginMetadataSnapshot, n as currentPluginMetadataConfigIdentityCache, r as getCurrentPluginMetadataSnapshotState, s as selectCurrentPluginMetadataCache } from "./current-plugin-metadata-state-BmYo9Q7A.mjs"; import { r as resolveInstalledPluginIndexInstallOwner } from "./installed-plugin-index-install-owner-DoEpptXw.mjs"; import { t as registerPluginMetadataSnapshotReaders } from "./plugin-metadata-snapshot-readers-J64uR9fG.mjs"; import { n as registerPluginMetadataProcessMemoLifecycleClear } from "./plugin-metadata-lifecycle-C9EqhiS5.mjs"; import { a as safeRealpathSync } from "./path-safety-CTChYC4Z.mjs"; import { m as resolvePluginRuntimeExecutionArtifact, p as resolvePluginRuntimeArtifactSelection } from "./bundled-dir-BKvdAqkP.mjs"; import { b as resolveInstalledPluginIndexPolicyHash } from "./installed-plugin-index-DYqse_gE.mjs"; import { t as resolvePluginMetadataEnvFingerprint } from "./plugin-metadata-env-DmZ06vdA.mjs"; import { a as resolveInstalledManifestRegistryIndexFingerprint, n as resolvePluginDiscoveryContext, t as resolvePluginControlPlaneFingerprint } from "./plugin-control-plane-context-BmY6xICT.mjs"; import { a as preparePluginNativeAdmissions, o as publishInspectionNativeEsmPolicy } from "./plugin-native-admission-state-DSOJoYAS.mjs"; //#region src/plugins/plugin-scope.ts /** Normalizes plugin id scope input into a sorted unique string list. */ function normalizePluginIdScope(e) { return e === void 0 ? void 0 : normalizeSortedUniqueTrimmedStringList(e); } /** True when plugin scope was explicitly provided, including an empty scope. */ function hasExplicitPluginIdScope(e) { return e !== void 0; } /** True when plugin scope was explicitly provided with at least one id. */ function hasNonEmptyPluginIdScope(e) { return e !== void 0 && e.length > 0; } /** Creates a lookup set for explicit plugin scope, or null when unscoped. */ function createPluginIdScopeSet(e) { if (e === void 0) return null; return new Set(e); } /** Serializes plugin scope for cache keys. */ function serializePluginIdScope(e) { return e === void 0 ? "__unscoped__" : JSON.stringify(e); } //#endregion //#region src/plugins/current-plugin-metadata-snapshot.ts function resolveAgentWorkspaceFingerprint(e, t) { return JSON.stringify(listAgentWorkspaceDirs(e, t)); } function prepareInspectionNativeEsmPolicy(e, t, n) { const r = /* @__PURE__ */ new Map(); const i = new Map(e.manifestRegistry.plugins.map((e) => [`${e.rootDir}\0${e.id}`, e])); for (const t of e.index.plugins) { const n = resolveImmutableInstallIdentity(t.origin, resolveInstalledPluginIndexInstallOwner(t), e.index.installRecords); const a = i.get(`${t.rootDir}\0${t.pluginId}`); if (!n || !a) continue; for (const e of ["runtime", "setup"]) { const t = e === "runtime" ? a.source : a.setupSource; if (!t) continue; for (const i of [false, true]) { const o = resolvePluginRuntimeExecutionArtifact(resolvePluginRuntimeArtifactSelection({ ...a, source: t, entryKind: e, preferBuiltPluginArtifacts: i })); const s = safeRealpathSync(o.source); if (s) r.set(s, n); } } } return { workspaces: new Set(t), sources: r, devSourceRoot: resolvePluginDiscoveryContext({ config: n.config, env: n.env, workspaceDir: n.workspaceDir ?? e.workspaceDir }).devSourceRoot }; } function prepareConfigCompatibility(e, t, n) { const r = t.compatibleConfigs?.map((e) => resolveInstalledPluginIndexPolicyHash(e, t.env)); const i = t.compatibleConfigs?.map((e, t) => n(e, r?.[t])); const a = [...t.compatibleConfigs ?? []]; if (t.config) { const n = resolveInstalledPluginIndexPolicyHash(t.config, t.env); if (n === e.policyHash || r?.includes(n)) a.push(t.config); } return { compatibleConfigFingerprints: i, configIdentities: a }; } function prepareCurrentPluginMetadataSnapshotPublication(e, t, n = "operation") { const fingerprint = (n, r) => resolvePluginControlPlaneFingerprint({ config: n, env: t.env, index: e.index, policyHash: r, workspaceDir: t.workspaceDir ?? e.workspaceDir }); const { compatibleConfigFingerprints: r, configIdentities: i } = prepareConfigCompatibility(e, t, fingerprint); const a = fingerprint(t.config, e.policyHash); const o = fingerprint({}, e.policyHash); const s = a === o || e.configFingerprint === o || Boolean(r?.includes(o)); const c = resolvePluginMetadataEnvFingerprint(t.env); const l = n === "gateway" && t.config ? listAgentWorkspaceDirs(t.config, t.env) : void 0; const u = l && JSON.stringify(l); const d = l && prepareInspectionNativeEsmPolicy(e, l, t); return () => { if (getCurrentPluginMetadataSnapshotState().owner === "gateway" && n !== "gateway") throw new Error("Gateway plugin metadata can only be replaced after shutdown"); currentPluginMetadataConfigIdentityCache.clear(); setCurrentPluginMetadataSnapshotState(e, a, r, n === "gateway" || s ? e.owners.modelIdNormalizationPolicies : void 0, n, c, s, u); if (d) publishInspectionNativeEsmPolicy(getProcessPluginCache(), d); for (const e of i) currentPluginMetadataConfigIdentityCache.add(e); }; } /** Prepares fingerprints before the Gateway's synchronous runtime publication edge. */ function prepareGatewayPluginMetadataSnapshotPublication(e, t = {}) { if (e.pluginIds !== void 0) throw new Error("Gateway plugin metadata must include the complete startup inventory"); const n = getPluginMetadataSnapshotCache(e); const r = withPluginCache(n, () => prepareCurrentPluginMetadataSnapshotPublication(e, t, "gateway")); return () => { selectCurrentPluginMetadataCache(n); r(); }; } /** Only the Gateway lifecycle publishes a complete replacement inventory. */ function setGatewayPluginMetadataSnapshot(e, t = {}) { if (e) prepareGatewayPluginMetadataSnapshotPublication(e, t)(); } /** Publishes a prepared CLI snapshot without displacing a lifecycle owner. */ function adoptCurrentPluginMetadataSnapshotIfAbsent(e, t = {}) { if (getScopedPluginCache()?.kind === "operation" || getCurrentPluginMetadataSnapshotState().snapshot !== void 0) return; prepareCurrentPluginMetadataSnapshotPublication(e, t)(); } /** Explicit installation refreshes the command before its next metadata phase. */ function clearCurrentPluginMetadataOperation() { const e = new Set(getScopedPluginCaches()); const t = /* @__PURE__ */ new Set(); for (let n = getPluginExecutionFrame()?.metadataScope; n; n = n.parent) if (n.immutableRuntimeGeneration) t.add(n.cache); else e.add(n.cache); for (const n of e) if (n.kind === "operation" && !t.has(n)) invalidatePluginCacheMetadata(n); } function isScopedSnapshotInCurrentCache(e) { if (!e.immutableRuntimeGeneration && e.metadata !== e.cache.metadata) return false; const t = getScopedPluginCache(); return t?.kind !== "operation" || e.cache === t; } /** Carries one owner-prepared metadata generation through nested async plugin lookups. */ function withPluginMetadataSnapshotScope(e, t, n = {}) { return runWithPluginExecutionFrame(createPluginMetadataSnapshotFrame(e, n), t); } /** Compose metadata and its cache before the runtime owner enters the async scope. */ function createPluginMetadataSnapshotFrame(e, t = {}) { const n = getPluginExecutionFrame(); const r = getPluginMetadataSnapshotCache(e); const i = t.trustConfigIdentity === true || e.pluginIds === void 0 && isCurrentPluginMetadataSnapshotRuntimeGeneration(e); preparePluginNativeAdmissions(e.index, r); const a = t.workspaceDir ?? e.workspaceDir; const fingerprint = (n, i) => resolvePluginControlPlaneFingerprint({ config: n, env: t.env, inventoryFingerprint: withPluginCache(r, () => resolveInstalledManifestRegistryIndexFingerprint(e.index)), policyHash: i, workspaceDir: a }); const o = i ? void 0 : prepareConfigCompatibility(e, t, fingerprint); const s = !i && t.config ? fingerprint(t.config, e.policyHash) : e.configFingerprint; const c = o && new WeakSet(o.configIdentities); return createPluginExecutionFrame({ ...n, cacheScope: { cache: r, parent: n?.cacheScope }, metadataScope: { snapshot: e, cache: r, metadata: r.metadata, configFingerprint: s, envFingerprint: resolvePluginMetadataEnvFingerprint(t.env), compatibleConfigFingerprints: o?.compatibleConfigFingerprints, hasConfigIdentity: c ? (e) => c.has(e) : void 0, immutableRuntimeGeneration: i, parent: n?.metadataScope } }, n); } function runOutsidePluginMetadataSnapshotScope(e) { const t = getPluginExecutionFrame(); return runWithPluginExecutionFrame(createPluginExecutionFrame({ ...t, metadataScope: void 0 }, t), () => runOutsidePluginCache(e)); } const NEEDS_PREPARED_POLICY = Symbol("needs-prepared-policy"); function resolveCompatiblePluginMetadataSnapshot(e, t, n = {}) { const r = e.snapshot; if (!r) return; if (e.immutableRuntimeGeneration) return r; const i = t.env ?? process.env; if (e.envFingerprint !== resolvePluginMetadataEnvFingerprint(i)) return; const a = normalizePluginIdScope(t.pluginIds ?? t.pluginIdScope?.resolve({ index: r.index })); const o = normalizePluginIdScope(r.pluginIds); if (a !== void 0 && serializePluginIdScope(o) !== serializePluginIdScope(a)) return; if (o !== void 0 && a === void 0 && t.allowScopedSnapshot !== true) return; const s = t.workspaceDir ?? (t.allowWorkspaceScopedSnapshot === true || n.scopedOwnerContext === true ? r.workspaceDir : void 0); if (r.workspaceDir !== void 0 && s === void 0) return; if (s !== void 0 && (r.workspaceDir ?? "") !== (s ?? "")) return; const c = Boolean(t.config && e.hasConfigIdentity?.(t.config)); if (c && t.requireDefaultDiscoveryContext !== true) return r; if (t.config && !c && t.allowSynchronousPolicyRead === false) return NEEDS_PREPARED_POLICY; const l = t.config && !c ? resolveInstalledPluginIndexPolicyHash(t.config, t.env) : void 0; if (t.config && !c) { const n = resolvePluginControlPlaneFingerprint({ config: t.config, env: i, index: r.index, policyHash: l, workspaceDir: s }); if (!(e.configFingerprint === n || r.configFingerprint === n || Boolean(e.compatibleConfigFingerprints?.includes(n)))) return; } if (t.requireDefaultDiscoveryContext === true && n.scopedOwnerContext !== true && e.defaultDiscoveryCompatible !== true) return; return r; } /** Reads Gateway-owned metadata from an operation cache only when its inputs still match. */ function getCompatibleProcessGatewayPluginMetadataSnapshot(e = {}) { const t = getCurrentPluginMetadataSnapshotState(); if (t.owner !== "gateway") return; if (!e.config || t.agentWorkspaceFingerprint !== resolveAgentWorkspaceFingerprint(e.config, e.env)) return; const n = resolveCompatiblePluginMetadataSnapshot({ ...t, hasConfigIdentity: (e) => currentPluginMetadataConfigIdentityCache.has(e) }, e); return n === NEEDS_PREPARED_POLICY ? void 0 : n; } function isCurrentPluginMetadataSnapshotRuntimeGeneration(e) { const t = getGatewayPluginMetadataSnapshot(); if (t && t.index === e.index) return true; for (let t = getPluginExecutionFrame()?.metadataScope; t; t = t.parent) { if (!isScopedSnapshotInCurrentCache(t)) continue; if (t.snapshot?.index === e.index && t.immutableRuntimeGeneration === true) return true; } return false; } function getCurrentPluginMetadataSnapshot(e = {}) { for (let t = getPluginExecutionFrame()?.metadataScope; t; t = t.parent) { if (!isScopedSnapshotInCurrentCache(t)) continue; const n = resolveCompatiblePluginMetadataSnapshot(t, e, { scopedOwnerContext: true }); if (n === NEEDS_PREPARED_POLICY) return; if (n) return n; } const t = getScopedPluginCache(); if (t && t !== getProcessPluginCache()) return; const n = getCurrentPluginMetadataSnapshotState(); const r = resolveCompatiblePluginMetadataSnapshot({ ...n, hasConfigIdentity: (e) => currentPluginMetadataConfigIdentityCache.has(e), immutableRuntimeGeneration: n.owner === "gateway" }, e); return r === NEEDS_PREPARED_POLICY ? void 0 : r; } registerPluginMetadataSnapshotReaders({ adoptCurrentPluginMetadataSnapshotIfAbsent, getCurrentPluginMetadataSnapshot }); registerPluginMetadataProcessMemoLifecycleClear(clearCurrentPluginMetadataOperation, { owner: "operation" }); //#endregion export { isCurrentPluginMetadataSnapshotRuntimeGeneration as a, setGatewayPluginMetadataSnapshot as c, hasExplicitPluginIdScope as d, hasNonEmptyPluginIdScope as f, getCurrentPluginMetadataSnapshot as i, withPluginMetadataSnapshotScope as l, serializePluginIdScope as m, createPluginMetadataSnapshotFrame as n, prepareGatewayPluginMetadataSnapshotPublication as o, normalizePluginIdScope as p, getCompatibleProcessGatewayPluginMetadataSnapshot as r, runOutsidePluginMetadataSnapshotScope as s, adoptCurrentPluginMetadataSnapshotIfAbsent as t, createPluginIdScopeSet as u };