UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

215 lines (214 loc) 7.96 kB
import { i as resolveGlobalSingleton } from "./global-singleton-Dc_stLtU.js"; import { fileURLToPath } from "node:url"; import path from "node:path"; import { AsyncLocalStorage } from "node:async_hooks"; //#region src/plugins/plugin-cache-artifacts.ts function createPluginCacheArtifacts() { return { moduleLoaders: /* @__PURE__ */ new Map(), sources: /* @__PURE__ */ new Map(), sourceAliases: /* @__PURE__ */ new Map() }; } function createPluginRootArtifacts() { return { artifactLoadsInProgress: /* @__PURE__ */ new Set(), artifacts: /* @__PURE__ */ new Map(), runtimeArtifacts: /* @__PURE__ */ new Map(), entryBoundaries: /* @__PURE__ */ new Map(), entryPaths: /* @__PURE__ */ new Map() }; } //#endregion //#region src/plugins/plugin-cache-management.ts function createPluginCacheManagement() { return { installRecords: /* @__PURE__ */ new Map(), persistedInstalledIndex: /* @__PURE__ */ new Map(), dependencyStatus: /* @__PURE__ */ new WeakMap() }; } //#endregion //#region src/plugins/plugin-cache-metadata.ts function createPluginCacheMetadata() { return { metadata: { current: { snapshot: void 0, owner: "operation", configFingerprint: void 0, envFingerprint: void 0, defaultDiscoveryCompatible: false, compatiblePolicyHashes: void 0, compatibleConfigFingerprints: void 0, modelIdNormalizationPolicies: void 0, revision: Symbol("plugin-metadata-snapshot"), configIdentities: /* @__PURE__ */ new WeakSet() }, snapshots: /* @__PURE__ */ new Map(), discovery: /* @__PURE__ */ new Map(), projections: /* @__PURE__ */ new WeakMap(), projectionSources: /* @__PURE__ */ new WeakMap(), completions: /* @__PURE__ */ new WeakMap(), indexFingerprints: /* @__PURE__ */ new WeakMap(), channelAdapters: /* @__PURE__ */ new WeakMap(), bundledChannelCatalogs: /* @__PURE__ */ new Map(), staticCatalogStates: /* @__PURE__ */ new WeakMap(), modelSuppressionResolvers: /* @__PURE__ */ new WeakMap() } }; } //#endregion //#region src/plugins/plugin-cache-sdk.ts function createPluginSdkHostFacts() { return { workspaceExports: /* @__PURE__ */ new Map(), subpathsByOwner: /* @__PURE__ */ new Map(), aliasesByOwner: /* @__PURE__ */ new Map(), bundledAliasesByMode: /* @__PURE__ */ new Map(), workspaceAliasesByMode: /* @__PURE__ */ new Map() }; } /** Derived SDK facts share the plugin cache lifetime; none owns a separate expiry. */ function createPluginCacheSdk() { return { hosts: /* @__PURE__ */ new Map(), contexts: /* @__PURE__ */ new Map(), packageNames: /* @__PURE__ */ new Map(), packageSearches: /* @__PURE__ */ new Map(), argvDirectories: /* @__PURE__ */ new Map(), devSourceRoots: /* @__PURE__ */ new Map(), bundledPackages: /* @__PURE__ */ new Map(), runtimeModules: /* @__PURE__ */ new Map(), usableDistArtifacts: /* @__PURE__ */ new Map(), normalizedJitiAliases: /* @__PURE__ */ new Map(), aliasFacts: /* @__PURE__ */ new WeakMap(), mergedAliases: /* @__PURE__ */ new WeakMap(), native: { sdkProviders: /* @__PURE__ */ new Map(), aliases: /* @__PURE__ */ new Map(), aliasesByMap: /* @__PURE__ */ new WeakMap(), registeredHosts: /* @__PURE__ */ new Set(), hostRoots: /* @__PURE__ */ new Map(), nearestPackageRoots: /* @__PURE__ */ new Map(), loaderPackageRoots: /* @__PURE__ */ new Map(), allowedParentRoots: /* @__PURE__ */ new Map() } }; } function getPluginSdkHostFacts(cache, packageRoot) { let facts = cache.hosts.get(packageRoot); if (!facts) { facts = createPluginSdkHostFacts(); cache.hosts.set(packageRoot, facts); } return facts; } //#endregion //#region src/plugins/plugin-cache.ts const state = resolveGlobalSingleton(Symbol.for("openclaw.pluginCache"), () => ({ scope: new AsyncLocalStorage(), snapshotOwners: /* @__PURE__ */ new WeakMap() })); /** A cache generation owns progressively acquired facts, never mutable activation state. */ function createPluginCache(options = {}) { return { kind: options.kind ?? "operation", roots: /* @__PURE__ */ new Map(), rootAliases: /* @__PURE__ */ new Map(), sdk: createPluginCacheSdk(), ...createPluginCacheMetadata(), ...createPluginCacheManagement(), ...createPluginCacheArtifacts() }; } function getProcessPluginCache() { return state.current ??= createPluginCache({ kind: "process" }); } /** Startup publishes its complete owner, including facts acquired before the kernel existed. */ function adoptProcessPluginCache(cache) { cache.kind = "process"; state.current = cache; } function getScopedPluginCache() { return state.scope.getStore(); } function getPluginCache() { return getScopedPluginCache() ?? getProcessPluginCache(); } function withPluginCache(cache, run) { return state.scope.run(cache, run); } /** Frozen views retain their producer so deferred access fills the same generation. */ function bindPluginMetadataSnapshotCache(snapshot, cache = getPluginCache()) { state.snapshotOwners.set(snapshot, cache); } function getPluginMetadataSnapshotCache(snapshot) { return state.snapshotOwners.get(snapshot) ?? getPluginCache(); } /** Only the lifecycle owner retires the process cache; operation scopes remain independent. */ function resetPluginCache() { const previous = state.current; state.current = void 0; previous?.disposeModules?.(); } function getPluginCacheRoot(rootDir) { const cache = getPluginCache(); const cached = cache.roots.get(cache.rootAliases.get(rootDir) ?? rootDir); if (cached) return cached; const lexical = path.resolve(rootDir); const key = cache.rootAliases.get(lexical) ?? lexical; let root = cache.roots.get(key); if (!root) { root = { rootDir: key, files: /* @__PURE__ */ new Map(), checkedEntries: /* @__PURE__ */ new Map(), paths: /* @__PURE__ */ new Map(), ...createPluginRootArtifacts() }; cache.roots.set(key, root); } return root; } function mergeRootFacts(target, source) { for (const [key, value] of source) if (!target.has(key)) target.set(key, value); } /** Bind aliases only after a checked file establishes their shared package boundary. */ function bindPluginCacheRoot(rootDir, canonicalRoot) { const cache = getPluginCache(); const lexical = path.resolve(rootDir); const canonical = path.resolve(canonicalRoot); const root = getPluginCacheRoot(lexical); root.rootDir = canonical; const existing = cache.roots.get(canonical); if (existing && existing !== root) { mergeRootFacts(root.files, existing.files); mergeRootFacts(root.checkedEntries, existing.checkedEntries); mergeRootFacts(root.paths, existing.paths); mergeRootFacts(root.artifacts, existing.artifacts); mergeRootFacts(root.runtimeArtifacts, existing.runtimeArtifacts); mergeRootFacts(root.entryBoundaries, existing.entryBoundaries); mergeRootFacts(root.entryPaths, existing.entryPaths); root.directory ??= existing.directory; root.publicSurfaceBoundary ??= existing.publicSurfaceBoundary; for (const artifact of existing.artifactLoadsInProgress) root.artifactLoadsInProgress.add(artifact); Object.assign(existing, root); } cache.roots.set(canonical, root); cache.rootAliases.set(lexical, canonical); return root; } function getPluginCacheSource(modulePath, cache = getPluginCache()) { const lexical = path.resolve(modulePath.startsWith("file:") ? fileURLToPath(modulePath) : modulePath); const key = cache.sourceAliases.get(lexical) ?? lexical; let source = cache.sources.get(key); if (!source) { source = { variants: /* @__PURE__ */ new Map(), validatedBoundaries: /* @__PURE__ */ new Set() }; cache.sources.set(key, source); } return source; } //#endregion export { getPluginCache as a, getPluginMetadataSnapshotCache as c, resetPluginCache as d, withPluginCache as f, createPluginCache as i, getProcessPluginCache as l, bindPluginCacheRoot as n, getPluginCacheRoot as o, getPluginSdkHostFacts as p, bindPluginMetadataSnapshotCache as r, getPluginCacheSource as s, adoptProcessPluginCache as t, getScopedPluginCache as u };