Skip to content

Commit 6d20962

Browse files
committed
perf(web-search): defer bundled provider runtime loading
Discover bundled search providers from enabled public descriptors and load only the selected runtime when creating its tool. Preserve activation and provider selection while removing duplicate scope forwarding.
1 parent 3e37496 commit 6d20962

4 files changed

Lines changed: 140 additions & 88 deletions

File tree

‎src/plugins/web-provider-public-artifacts.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,20 @@ function resolveBundledCandidatePluginIds(params: {
8484
}
8585

8686
function resolveBundledRuntimeCandidatePluginIds(params: {
87+
contract: "webSearchProviders" | "webFetchProviders";
8788
config?: PluginLoadOptions["config"];
8889
workspaceDir?: string;
8990
env?: PluginLoadOptions["env"];
9091
onlyPluginIds: readonly string[];
9192
manifestRecords?: readonly PluginManifestRecord[];
9293
}): string[] | null {
93-
const resolvedConfig = resolveBundledWebFetchResolutionConfig(params).config;
94+
const search = params.contract === "webSearchProviders";
95+
const resolvedConfig = (
96+
search ? resolveBundledWebSearchResolutionConfig : resolveBundledWebFetchResolutionConfig
97+
)(params).config;
9498
const candidates = resolveManifestDeclaredWebProviderCandidates({
95-
contract: "webFetchProviders",
96-
configKey: "webFetch",
99+
contract: params.contract,
100+
configKey: search ? "webSearch" : "webFetch",
97101
config: params.config,
98102
workspaceDir: params.workspaceDir,
99103
env: params.env,
@@ -115,7 +119,7 @@ function resolveBundledRuntimeCandidatePluginIds(params: {
115119
workspaceDir: params.workspaceDir,
116120
env: params.env,
117121
onlyPluginIds: pluginIds,
118-
contract: "webFetchProviders",
122+
contract: params.contract,
119123
manifestRecords: candidates.manifestRecords,
120124
}).map((plugin) => plugin.id),
121125
);
@@ -205,12 +209,25 @@ export function resolveBundledWebFetchProvidersFromPublicArtifacts(
205209
});
206210
}
207211

212+
export function resolveEnabledBundledWebSearchProvidersFromPublicArtifacts(
213+
params: BundledWebProviderPublicArtifactParams & { onlyPluginIds: readonly string[] },
214+
): PluginWebSearchProviderEntry[] | null {
215+
const pluginIds = resolveBundledRuntimeCandidatePluginIds({
216+
...params,
217+
contract: "webSearchProviders",
218+
});
219+
return pluginIds
220+
? resolveBundledExplicitWebSearchProvidersFromPublicArtifacts({ onlyPluginIds: pluginIds })
221+
: null;
222+
}
223+
208224
export function resolveBundledRuntimeWebFetchProvidersFromPublicArtifacts(
209225
params: BundledWebProviderPublicArtifactParams & {
210226
onlyPluginIds: readonly string[];
211227
},
212228
): PluginWebFetchProviderEntry[] | null {
213229
const pluginIds = resolveBundledRuntimeCandidatePluginIds({
230+
contract: "webFetchProviders",
214231
config: params.config,
215232
workspaceDir: params.workspaceDir,
216233
env: params.env,

‎src/plugins/web-search-providers.runtime.test.ts‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/** Covers runtime loading and sorting for plugin web search providers. */
22
import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures";
33
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
4+
import { createWebSearchTestProvider } from "../test-utils/web-provider-runtime.test-helpers.js";
5+
import * as publicArtifacts from "./web-provider-public-artifacts.explicit.js";
46

57
type RegistryModule = typeof import("./registry.js");
68
type RuntimeModule = typeof import("./runtime.js");
@@ -34,6 +36,7 @@ let loadInstalledPluginManifestRegistryMock: ReturnType<
3436
>;
3537
let setActivePluginRegistry: RuntimeModule["setActivePluginRegistry"];
3638
let resolvePluginWebSearchProviders: WebSearchProvidersRuntimeModule["resolvePluginWebSearchProviders"];
39+
let resolveRuntimeWebSearchProviders: WebSearchProvidersRuntimeModule["resolveRuntimeWebSearchProviders"];
3740
let loadOpenClawPluginsMock: ReturnType<typeof vi.fn>;
3841
let loaderModule: typeof import("./loader.js");
3942
let pluginAutoEnableModule: PluginAutoEnableModule;
@@ -320,7 +323,8 @@ describe("resolvePluginWebSearchProviders", () => {
320323
loaderModule = await import("./loader.js");
321324
pluginAutoEnableModule = await import("../config/plugin-auto-enable.js");
322325
({ resetPluginRuntimeStateForTest, setActivePluginRegistry } = await import("./runtime.js"));
323-
({ resolvePluginWebSearchProviders } = await import("./web-search-providers.runtime.js"));
326+
({ resolvePluginWebSearchProviders, resolveRuntimeWebSearchProviders } =
327+
await import("./web-search-providers.runtime.js"));
324328
});
325329

326330
beforeEach(() => {
@@ -362,6 +366,63 @@ describe("resolvePluginWebSearchProviders", () => {
362366
expectLoaderCallCount(1);
363367
});
364368

369+
it("loads only the selected runtime when a bundled search tool is created", () => {
370+
loadInstalledPluginManifestRegistryMock.mockReturnValue({
371+
plugins: [
372+
createWebSearchManifestRecord({ id: "brave", providerId: "brave" }),
373+
createWebSearchManifestRecord({ id: "google", providerId: "gemini" }),
374+
],
375+
diagnostics: [],
376+
});
377+
vi.spyOn(
378+
publicArtifacts,
379+
"resolveBundledExplicitWebSearchProvidersFromPublicArtifacts",
380+
).mockReturnValue([
381+
createWebSearchTestProvider({
382+
pluginId: "brave",
383+
id: "brave",
384+
credentialPath: "plugins.entries.brave.config.webSearch.apiKey",
385+
createTool: () => null,
386+
}),
387+
createWebSearchTestProvider({
388+
pluginId: "google",
389+
id: "gemini",
390+
credentialPath: "plugins.entries.google.config.webSearch.apiKey",
391+
createTool: () => null,
392+
}),
393+
]);
394+
const config = {
395+
plugins: {
396+
allow: ["brave", "google"],
397+
entries: { brave: { enabled: true }, google: { enabled: true } },
398+
},
399+
};
400+
401+
const providers = resolveRuntimeWebSearchProviders(createSnapshotParams({ config }));
402+
expect(toRuntimeProviderKeys(providers)).toEqual(["brave:brave", "google:gemini"]);
403+
expectLoaderCallCount(0);
404+
405+
expect(providers[0]?.createTool({ config })?.description).toBe("brave");
406+
expectLoaderCallCount(1);
407+
expect(
408+
requireLastCallFirstArg(loadOpenClawPluginsMock, "loadOpenClawPlugins").onlyPluginIds,
409+
).toEqual(["brave"]);
410+
});
411+
412+
it("does not discover explicitly disabled bundled providers", () => {
413+
loadInstalledPluginManifestRegistryMock.mockReturnValue({
414+
plugins: [createWebSearchManifestRecord({ id: "brave", providerId: "brave" })],
415+
diagnostics: [],
416+
});
417+
const providers = resolveRuntimeWebSearchProviders({
418+
config: { plugins: { entries: { brave: { enabled: false } } } },
419+
onlyPluginIds: ["brave"],
420+
});
421+
422+
expect(providers).toEqual([]);
423+
expectLoaderCallCount(0);
424+
});
425+
365426
it("loads manifest-declared web-search providers in setup mode", () => {
366427
const providers = resolvePluginWebSearchProviders({
367428
config: {

‎src/plugins/web-search-providers.runtime.ts‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { loadOpenClawPlugins } from "./loader.js";
33
import type { PluginLoadOptions } from "./loader.js";
44
import type { PluginManifestRecord } from "./manifest-registry.js";
55
import type { PluginWebSearchProviderEntry } from "./types.js";
6-
import { resolveBundledWebSearchProvidersFromPublicArtifacts } from "./web-provider-public-artifacts.js";
6+
import {
7+
resolveBundledWebSearchProvidersFromPublicArtifacts,
8+
resolveEnabledBundledWebSearchProvidersFromPublicArtifacts,
9+
} from "./web-provider-public-artifacts.js";
710
import {
811
mapRegistryProviders,
912
resolveManifestDeclaredWebProviderCandidatePluginIds,
@@ -45,6 +48,32 @@ function mapRegistryWebSearchProviders(params: {
4548
});
4649
}
4750

51+
const providerResolution = {
52+
resolveBundledResolutionConfig: resolveBundledWebSearchResolutionConfig,
53+
resolveCandidatePluginIds: resolveWebSearchCandidatePluginIds,
54+
mapRegistryProviders: mapRegistryWebSearchProviders,
55+
};
56+
57+
function resolveLazyBundledWebSearchProviders(
58+
params: Parameters<typeof resolveEnabledBundledWebSearchProvidersFromPublicArtifacts>[0],
59+
): PluginWebSearchProviderEntry[] | null {
60+
const providers = resolveEnabledBundledWebSearchProvidersFromPublicArtifacts(params);
61+
return (
62+
providers?.map((provider) => {
63+
const lazyProvider = Object.assign({}, provider);
64+
lazyProvider.createTool = (context) => {
65+
// Public descriptors can have setup-only factories; execution belongs to the scoped registry.
66+
const runtime = resolvePluginWebProviders(
67+
{ ...params, onlyPluginIds: [provider.pluginId] },
68+
providerResolution,
69+
).find((entry) => entry.pluginId === provider.pluginId && entry.id === provider.id);
70+
return runtime?.createTool(context) ?? null;
71+
};
72+
return lazyProvider;
73+
}) ?? null
74+
);
75+
}
76+
4877
export function resolvePluginWebSearchProviders(params: {
4978
config?: PluginLoadOptions["config"];
5079
workspaceDir?: string;
@@ -57,9 +86,7 @@ export function resolvePluginWebSearchProviders(params: {
5786
manifestRecords?: readonly PluginManifestRecord[];
5887
}): PluginWebSearchProviderEntry[] {
5988
return resolvePluginWebProviders(params, {
60-
resolveBundledResolutionConfig: resolveBundledWebSearchResolutionConfig,
61-
resolveCandidatePluginIds: resolveWebSearchCandidatePluginIds,
62-
mapRegistryProviders: mapRegistryWebSearchProviders,
89+
...providerResolution,
6390
resolveBundledPublicArtifactProviders: resolveBundledWebSearchProvidersFromPublicArtifacts,
6491
});
6592
}
@@ -73,8 +100,7 @@ export function resolveRuntimeWebSearchProviders(params: {
73100
manifestRecords?: readonly PluginManifestRecord[];
74101
}): PluginWebSearchProviderEntry[] {
75102
return resolvePluginWebProviders(params, {
76-
resolveBundledResolutionConfig: resolveBundledWebSearchResolutionConfig,
77-
resolveCandidatePluginIds: resolveWebSearchCandidatePluginIds,
78-
mapRegistryProviders: mapRegistryWebSearchProviders,
103+
...providerResolution,
104+
resolveBundledRuntimeArtifactProviders: resolveLazyBundledWebSearchProviders,
79105
});
80106
}

‎src/web-search/runtime.ts‎

Lines changed: 24 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -235,66 +235,6 @@ function resolveRuntimePreferredWebSearchProviderId(params: {
235235
: undefined;
236236
}
237237

238-
function resolveExplicitWebSearchProviderId(params: {
239-
search?: WebSearchConfig;
240-
runtimeWebSearch?: RuntimeWebSearchMetadata;
241-
providerId?: string;
242-
includeRuntimeSelection?: boolean;
243-
}): string | undefined {
244-
const callerProviderId = normalizeOptionalLowercaseString(params.providerId);
245-
if (callerProviderId) {
246-
return callerProviderId;
247-
}
248-
249-
if (params.includeRuntimeSelection && params.runtimeWebSearch?.providerSource === "configured") {
250-
const runtimeProviderId = normalizeOptionalLowercaseString(
251-
params.runtimeWebSearch.selectedProvider ?? params.runtimeWebSearch.providerConfigured,
252-
);
253-
if (runtimeProviderId) {
254-
return runtimeProviderId;
255-
}
256-
}
257-
258-
const configuredProviderId =
259-
params.search && "provider" in params.search
260-
? normalizeOptionalLowercaseString(params.search.provider)
261-
: undefined;
262-
if (configuredProviderId) {
263-
return configuredProviderId;
264-
}
265-
return undefined;
266-
}
267-
268-
function resolveExplicitWebSearchProviderPluginIds(params: {
269-
config?: OpenClawConfig;
270-
search?: WebSearchConfig;
271-
runtimeWebSearch?: RuntimeWebSearchMetadata;
272-
providerId?: string;
273-
includeRuntimeSelection?: boolean;
274-
}): readonly string[] | undefined {
275-
const providerId = resolveExplicitWebSearchProviderId(params);
276-
if (!providerId) {
277-
return undefined;
278-
}
279-
const ownerPluginId = resolveManifestContractOwnerPluginId({
280-
config: params.config,
281-
contract: "webSearchProviders",
282-
value: providerId,
283-
});
284-
return ownerPluginId ? [ownerPluginId] : undefined;
285-
}
286-
287-
function resolveWebSearchProviderLoadScope(params: {
288-
config?: OpenClawConfig;
289-
search?: WebSearchConfig;
290-
runtimeWebSearch?: RuntimeWebSearchMetadata;
291-
providerId?: string;
292-
includeRuntimeSelection?: boolean;
293-
}): { onlyPluginIds?: readonly string[] } {
294-
const onlyPluginIds = resolveExplicitWebSearchProviderPluginIds(params);
295-
return onlyPluginIds ? { onlyPluginIds } : {};
296-
}
297-
298238
type WebSearchRequestContext = {
299239
config?: OpenClawConfig;
300240
search?: WebSearchConfig;
@@ -325,23 +265,31 @@ function loadSortedWebSearchProviders(
325265
preferRuntimeProviders?: boolean;
326266
},
327267
): PluginWebSearchProviderEntry[] {
328-
const loadScope = resolveWebSearchProviderLoadScope({
329-
config: params.config,
330-
search: params.search,
331-
runtimeWebSearch: params.runtimeWebSearch,
332-
providerId: params.providerId,
333-
includeRuntimeSelection: Boolean(params.preferRuntimeProviders),
334-
});
268+
const runtimeProviderId =
269+
params.preferRuntimeProviders && params.runtimeWebSearch?.providerSource === "configured"
270+
? normalizeOptionalLowercaseString(
271+
params.runtimeWebSearch.selectedProvider ?? params.runtimeWebSearch.providerConfigured,
272+
)
273+
: undefined;
274+
const providerId =
275+
normalizeOptionalLowercaseString(params.providerId) ??
276+
runtimeProviderId ??
277+
normalizeOptionalLowercaseString(params.search?.provider);
278+
const pluginId = providerId
279+
? resolveManifestContractOwnerPluginId({
280+
config: params.config,
281+
contract: "webSearchProviders",
282+
value: providerId,
283+
})
284+
: undefined;
285+
const resolveProviders = params.preferRuntimeProviders
286+
? resolveRuntimeWebSearchProviders
287+
: resolvePluginWebSearchProviders;
335288
return sortWebSearchProvidersForAutoDetect(
336-
params.preferRuntimeProviders
337-
? resolveRuntimeWebSearchProviders({
338-
config: params.config,
339-
...loadScope,
340-
})
341-
: resolvePluginWebSearchProviders({
342-
config: params.config,
343-
...loadScope,
344-
}),
289+
resolveProviders({
290+
config: params.config,
291+
...(pluginId ? { onlyPluginIds: [pluginId] } : {}),
292+
}),
345293
);
346294
}
347295

0 commit comments

Comments
 (0)