




















@@ -14,6 +14,22 @@ import type {
1414ProviderWrapStreamFnContext,
1515} from "./types.js";
161617+const providerRuntimePluginCache = new WeakMap<
18+OpenClawConfig,
19+Map<string, ProviderPlugin | null>
20+>();
21+22+type ProviderRuntimePluginLookupParams = {
23+provider: string;
24+config?: OpenClawConfig;
25+workspaceDir?: string;
26+env?: NodeJS.ProcessEnv;
27+applyAutoEnable?: boolean;
28+bundledProviderAllowlistCompat?: boolean;
29+bundledProviderVitestCompat?: boolean;
30+installBundledRuntimeDeps?: boolean;
31+};
32+1733function matchesProviderId(provider: ProviderPlugin, providerId: string): boolean {
1834const normalized = normalizeProviderId(providerId);
1935if (!normalized) {
@@ -27,6 +43,33 @@ function matchesProviderId(provider: ProviderPlugin, providerId: string): boolea
2743);
2844}
294546+function resolveProviderRuntimePluginCacheKey(params: ProviderRuntimePluginLookupParams): string {
47+return JSON.stringify({
48+provider: normalizeLowercaseStringOrEmpty(params.provider),
49+plugins: params.config?.plugins,
50+models: params.config?.models?.providers,
51+workspaceDir: params.workspaceDir ?? "",
52+applyAutoEnable: params.applyAutoEnable ?? null,
53+bundledProviderAllowlistCompat: params.bundledProviderAllowlistCompat ?? null,
54+bundledProviderVitestCompat: params.bundledProviderVitestCompat ?? null,
55+installBundledRuntimeDeps: params.installBundledRuntimeDeps ?? null,
56+});
57+}
58+59+function resolveProviderRuntimePluginCache(
60+params: ProviderRuntimePluginLookupParams,
61+): Map<string, ProviderPlugin | null> | undefined {
62+if (!params.config || (params.env && params.env !== process.env)) {
63+return undefined;
64+}
65+let cache = providerRuntimePluginCache.get(params.config);
66+if (!cache) {
67+cache = new Map();
68+providerRuntimePluginCache.set(params.config, cache);
69+}
70+return cache;
71+}
72+3073function matchesProviderLiteralId(provider: ProviderPlugin, providerId: string): boolean {
3174const normalized = normalizeLowercaseStringOrEmpty(providerId);
3275return !!normalized && normalizeLowercaseStringOrEmpty(provider.id) === normalized;
@@ -51,7 +94,6 @@ export function resolveProviderPluginsForHooks(params: {
5194 workspaceDir,
5295 env,
5396activate: false,
54-cache: false,
5597applyAutoEnable: params.applyAutoEnable,
5698bundledProviderAllowlistCompat: params.bundledProviderAllowlistCompat ?? true,
5799bundledProviderVitestCompat: params.bundledProviderVitestCompat ?? true,
@@ -65,7 +107,6 @@ export function resolveProviderPluginsForHooks(params: {
65107 workspaceDir,
66108 env,
67109activate: false,
68-cache: false,
69110applyAutoEnable: params.applyAutoEnable,
70111bundledProviderAllowlistCompat: params.bundledProviderAllowlistCompat ?? true,
71112bundledProviderVitestCompat: params.bundledProviderVitestCompat ?? true,
@@ -74,21 +115,19 @@ export function resolveProviderPluginsForHooks(params: {
74115return resolved;
75116}
7611777-export function resolveProviderRuntimePlugin(params: {
78-provider: string;
79-config?: OpenClawConfig;
80-workspaceDir?: string;
81-env?: NodeJS.ProcessEnv;
82-applyAutoEnable?: boolean;
83-bundledProviderAllowlistCompat?: boolean;
84-bundledProviderVitestCompat?: boolean;
85-installBundledRuntimeDeps?: boolean;
86-}): ProviderPlugin | undefined {
118+export function resolveProviderRuntimePlugin(
119+params: ProviderRuntimePluginLookupParams,
120+): ProviderPlugin | undefined {
121+const cache = resolveProviderRuntimePluginCache(params);
122+const cacheKey = cache ? resolveProviderRuntimePluginCacheKey(params) : "";
123+if (cache?.has(cacheKey)) {
124+return cache.get(cacheKey) ?? undefined;
125+}
87126const apiOwnerHint = resolveProviderConfigApiOwnerHint({
88127provider: params.provider,
89128config: params.config,
90129});
91-return resolveProviderPluginsForHooks({
130+const plugin = resolveProviderPluginsForHooks({
92131config: params.config,
93132workspaceDir: params.workspaceDir ?? getActivePluginRegistryWorkspaceDirFromState(),
94133env: params.env,
@@ -105,6 +144,8 @@ export function resolveProviderRuntimePlugin(params: {
105144}
106145return matchesProviderId(plugin, params.provider);
107146});
147+cache?.set(cacheKey, plugin ?? null);
148+return plugin;
108149}
109150110151export function resolveProviderHookPlugin(params: {
@@ -140,7 +181,9 @@ export function resolveProviderExtraParamsForTransport(params: {
140181env?: NodeJS.ProcessEnv;
141182context: ProviderExtraParamsForTransportContext;
142183}) {
143-return resolveProviderHookPlugin(params)?.extraParamsForTransport?.(params.context) ?? undefined;
184+return (
185+resolveProviderRuntimePlugin(params)?.extraParamsForTransport?.(params.context) ?? undefined
186+);
144187}
145188146189export function resolveProviderAuthProfileId(params: {
@@ -150,7 +193,7 @@ export function resolveProviderAuthProfileId(params: {
150193env?: NodeJS.ProcessEnv;
151194context: ProviderResolveAuthProfileIdContext;
152195}): string | undefined {
153-const resolved = resolveProviderHookPlugin(params)?.resolveAuthProfileId?.(params.context);
196+const resolved = resolveProviderRuntimePlugin(params)?.resolveAuthProfileId?.(params.context);
154197return typeof resolved === "string" && resolved.trim() ? resolved.trim() : undefined;
155198}
156199@@ -171,5 +214,5 @@ export function wrapProviderStreamFn(params: {
171214env?: NodeJS.ProcessEnv;
172215context: ProviderWrapStreamFnContext;
173216}) {
174-return resolveProviderHookPlugin(params)?.wrapStreamFn?.(params.context) ?? undefined;
217+return resolveProviderRuntimePlugin(params)?.wrapStreamFn?.(params.context) ?? undefined;
175218}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。