





















@@ -4,6 +4,11 @@ import {
44withBundledPluginEnablementCompat,
55withBundledPluginVitestCompat,
66} from "./bundled-compat.js";
7+import {
8+buildPluginSnapshotCacheEnvKey,
9+resolvePluginSnapshotCacheTtlMs,
10+shouldUsePluginSnapshotCache,
11+} from "./cache-controls.js";
712import { hasExplicitPluginConfig } from "./config-policy.js";
813import { resolveRuntimePluginRegistry } from "./loader.js";
914import { loadPluginManifestRegistryForPluginRegistry } from "./plugin-registry.js";
@@ -32,6 +37,11 @@ type CapabilityContractKey =
3237type CapabilityProviderForKey<K extends CapabilityProviderRegistryKey> =
3338PluginRegistry[K][number] extends { provider: infer T } ? T : never;
343940+type CapabilityProviderPluginIdCacheEntry = {
41+expiresAt: number;
42+pluginIds: string[];
43+};
44+3545const CAPABILITY_CONTRACT_KEY: Record<CapabilityProviderRegistryKey, CapabilityContractKey> = {
3646memoryEmbeddingProviders: "memoryEmbeddingProviders",
3747speechProviders: "speechProviders",
@@ -43,15 +53,86 @@ const CAPABILITY_CONTRACT_KEY: Record<CapabilityProviderRegistryKey, CapabilityC
4353musicGenerationProviders: "musicGenerationProviders",
4454};
455556+const capabilityProviderPluginIdCache = new WeakMap<
57+OpenClawConfig,
58+WeakMap<NodeJS.ProcessEnv, Map<string, CapabilityProviderPluginIdCacheEntry>>
59+>();
60+61+function buildCapabilityProviderPluginIdCacheKey(params: {
62+key: CapabilityProviderRegistryKey;
63+env: NodeJS.ProcessEnv;
64+providerId?: string;
65+}): string {
66+return JSON.stringify({
67+key: params.key,
68+providerId: params.providerId ?? "",
69+env: buildPluginSnapshotCacheEnvKey(params.env),
70+});
71+}
72+73+function getCachedCapabilityProviderPluginIds(params: {
74+key: CapabilityProviderRegistryKey;
75+cfg?: OpenClawConfig;
76+env: NodeJS.ProcessEnv;
77+providerId?: string;
78+}): string[] | undefined {
79+if (!params.cfg || !shouldUsePluginSnapshotCache(params.env)) {
80+return undefined;
81+}
82+const envCache = capabilityProviderPluginIdCache.get(params.cfg)?.get(params.env);
83+const cached = envCache?.get(buildCapabilityProviderPluginIdCacheKey(params));
84+if (!cached || cached.expiresAt <= Date.now()) {
85+return undefined;
86+}
87+return [...cached.pluginIds];
88+}
89+90+function memoizeCapabilityProviderPluginIds(params: {
91+key: CapabilityProviderRegistryKey;
92+cfg?: OpenClawConfig;
93+env: NodeJS.ProcessEnv;
94+providerId?: string;
95+pluginIds: string[];
96+}): void {
97+if (!params.cfg || !shouldUsePluginSnapshotCache(params.env)) {
98+return;
99+}
100+let configCache = capabilityProviderPluginIdCache.get(params.cfg);
101+if (!configCache) {
102+configCache = new WeakMap<
103+NodeJS.ProcessEnv,
104+Map<string, CapabilityProviderPluginIdCacheEntry>
105+>();
106+capabilityProviderPluginIdCache.set(params.cfg, configCache);
107+}
108+let envCache = configCache.get(params.env);
109+if (!envCache) {
110+envCache = new Map<string, CapabilityProviderPluginIdCacheEntry>();
111+configCache.set(params.env, envCache);
112+}
113+envCache.set(buildCapabilityProviderPluginIdCacheKey(params), {
114+expiresAt: Date.now() + resolvePluginSnapshotCacheTtlMs(params.env),
115+pluginIds: [...params.pluginIds],
116+});
117+}
118+46119function resolveBundledCapabilityCompatPluginIds(params: {
47120key: CapabilityProviderRegistryKey;
48121cfg?: OpenClawConfig;
49122providerId?: string;
50123}): string[] {
124+const env = process.env;
125+const cached = getCachedCapabilityProviderPluginIds({
126+ ...params,
127+ env,
128+});
129+if (cached) {
130+return cached;
131+}
51132const contractKey = CAPABILITY_CONTRACT_KEY[params.key];
52-return loadPluginManifestRegistryForPluginRegistry({
133+const pluginIds = loadPluginManifestRegistryForPluginRegistry({
53134config: params.cfg,
54-env: process.env,
135+ env,
55136includeDisabled: true,
56137})
57138.plugins.filter(
@@ -62,6 +143,12 @@ function resolveBundledCapabilityCompatPluginIds(params: {
62143)
63144.map((plugin) => plugin.id)
64145.toSorted((left, right) => left.localeCompare(right));
146+memoizeCapabilityProviderPluginIds({
147+ ...params,
148+ env,
149+ pluginIds,
150+});
151+return pluginIds;
65152}
6615367154function resolveCapabilityProviderConfig(params: {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。