UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

91 lines (90 loc) 3.26 kB
import { t as pruneMapToMaxSize } from "./map-size-CNcWiFKu.js"; import { n as registerPluginMetadataProcessMemoLifecycleClear } from "./plugin-metadata-lifecycle-C6m-q5Au.js"; //#region src/plugins/plugin-cache-primitives.ts /** Small process-local LRU cache for runtime registries and compiled validators. */ var PluginLruCache = class { #maxEntries; #entries = /* @__PURE__ */ new Map(); constructor(maxEntries) { this.#maxEntries = normalizeMaxEntries(maxEntries, 1); } get size() { return this.#entries.size; } clear() { this.#entries.clear(); } deleteValue(value) { for (const [key, entry] of this.#entries) if (entry === value) this.#entries.delete(key); } /** Returns a cached value and refreshes its recency when present. */ get(cacheKey) { if (!this.#entries.has(cacheKey)) return; const cached = this.#entries.get(cacheKey); this.#entries.delete(cacheKey); this.#entries.set(cacheKey, cached); return cached; } /** Stores a value as the newest entry and evicts oldest entries past capacity. */ set(cacheKey, value) { if (this.#entries.has(cacheKey)) this.#entries.delete(cacheKey); this.#entries.set(cacheKey, value); pruneMapToMaxSize(this.#entries, this.#maxEntries); } }; /** Resolves a config-scoped cached value; calls without config intentionally bypass caching. */ function resolveConfigScopedRuntimeCacheValue(params) { if (!params.config) return params.load(); let configCache = params.cache.get(params.config); if (!configCache) { configCache = /* @__PURE__ */ new Map(); params.cache.set(params.config, configCache); } if (configCache.has(params.key)) return configCache.get(params.key); const loaded = params.load(); configCache.set(params.key, loaded); return loaded; } /** Encodes structured cache dimensions without separator ambiguity. */ function createPluginCacheKey(parts) { return JSON.stringify(parts); } /** Creates a config-scoped promise cache that drops rejected loads so callers can retry. */ function createConfigScopedPromiseLoader(load) { let defaultPromise; let promisesByConfig = /* @__PURE__ */ new WeakMap(); const createPromise = (config) => { const promise = Promise.resolve().then(() => load(config)); promise.catch(() => { if (config) { if (promisesByConfig.get(config) === promise) promisesByConfig.delete(config); } else if (defaultPromise === promise) defaultPromise = void 0; }); return promise; }; const loader = { async load(config) { if (!config) { defaultPromise ??= createPromise(); return await defaultPromise; } const cached = promisesByConfig.get(config); if (cached) return await cached; const promise = createPromise(config); promisesByConfig.set(config, promise); return await promise; }, clear() { defaultPromise = void 0; promisesByConfig = /* @__PURE__ */ new WeakMap(); } }; registerPluginMetadataProcessMemoLifecycleClear(() => loader.clear()); return loader; } function normalizeMaxEntries(value, fallback) { if (!Number.isFinite(value) || value <= 0) return fallback; return Math.max(1, Math.floor(value)); } //#endregion export { resolveConfigScopedRuntimeCacheValue as i, createConfigScopedPromiseLoader as n, createPluginCacheKey as r, PluginLruCache as t };