Skip to content

Commit 2a767ee

Browse files
authored
refactor(plugins): simplify web provider resolution (#132931)
1 parent 41dccab commit 2a767ee

4 files changed

Lines changed: 28 additions & 211 deletions

File tree

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import {
1515
mapRegistryProviders,
1616
resolveManifestDeclaredWebProviderCandidatePluginIds,
1717
} from "./web-provider-resolution-shared.js";
18-
import {
19-
resolvePluginWebProviders,
20-
resolveRuntimeWebProviders,
21-
} from "./web-provider-runtime-shared.js";
18+
import { resolvePluginWebProviders } from "./web-provider-runtime-shared.js";
2219

2320
function resolveWebFetchCandidatePluginIds(params: {
2421
config?: PluginLoadOptions["config"];
@@ -85,7 +82,7 @@ export function resolveRuntimeWebFetchProviders(params: {
8582
origin?: PluginManifestRecord["origin"];
8683
manifestRecords?: readonly PluginManifestRecord[];
8784
}): PluginWebFetchProviderEntry[] {
88-
return resolveRuntimeWebProviders(params, {
85+
return resolvePluginWebProviders(params, {
8986
resolveBundledResolutionConfig: resolveBundledWebFetchResolutionConfig,
9087
resolveCandidatePluginIds: resolveWebFetchCandidatePluginIds,
9188
mapRegistryProviders: mapRegistryWebFetchProviders,

‎src/plugins/web-provider-runtime-shared.test.ts‎

Lines changed: 12 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ vi.mock("./runtime/load-context.js", () => ({
4848
}));
4949

5050
let resolvePluginWebProviders: typeof import("./web-provider-runtime-shared.js").resolvePluginWebProviders;
51-
let resolveRuntimeWebProviders: typeof import("./web-provider-runtime-shared.js").resolveRuntimeWebProviders;
5251

5352
const requireRecord = createRequireRecord("record", "expected-non-array-record");
5453

@@ -58,8 +57,7 @@ function mockArg(mock: ReturnType<typeof vi.fn>, callIndex = 0): Record<string,
5857

5958
describe("web-provider-runtime-shared", () => {
6059
beforeAll(async () => {
61-
({ resolvePluginWebProviders, resolveRuntimeWebProviders } =
62-
await import("./web-provider-runtime-shared.js"));
60+
({ resolvePluginWebProviders } = await import("./web-provider-runtime-shared.js"));
6361
});
6462

6563
beforeEach(() => {
@@ -87,34 +85,11 @@ describe("web-provider-runtime-shared", () => {
8785
});
8886

8987
it("preserves explicit empty scopes in runtime-compatible web provider loads", () => {
88+
const activeRegistry = { source: "active" };
9089
const mapRegistryProviders = vi.fn(() => []);
91-
mocks.getLoadedRuntimePluginRegistry.mockReturnValue({} as never);
92-
93-
resolvePluginWebProviders(
94-
{
95-
config: {},
96-
onlyPluginIds: [],
97-
},
98-
{
99-
resolveBundledResolutionConfig: () => ({
100-
config: {},
101-
activationSourceConfig: {},
102-
autoEnabledReasons: {},
103-
}),
104-
resolveCandidatePluginIds: () => [],
105-
mapRegistryProviders,
106-
},
107-
);
108-
109-
expect(mockArg(mocks.getLoadedRuntimePluginRegistry).requiredPluginIds).toEqual([]);
110-
expect(mockArg(mapRegistryProviders).onlyPluginIds).toEqual([]);
111-
});
112-
113-
it("preserves explicit empty scopes in direct runtime web provider resolution", () => {
114-
const mapRegistryProviders = vi.fn(() => []);
115-
mocks.getLoadedRuntimePluginRegistry.mockReturnValue({} as never);
90+
mocks.getLoadedRuntimePluginRegistry.mockReturnValue(activeRegistry as never);
11691

117-
resolveRuntimeWebProviders(
92+
const result = resolvePluginWebProviders(
11893
{
11994
config: {},
12095
onlyPluginIds: [],
@@ -131,14 +106,19 @@ describe("web-provider-runtime-shared", () => {
131106
);
132107

133108
expect(mockArg(mocks.getLoadedRuntimePluginRegistry).requiredPluginIds).toEqual([]);
134-
expect(mockArg(mapRegistryProviders).onlyPluginIds).toEqual([]);
109+
expect(mapRegistryProviders).toHaveBeenCalledWith({
110+
registry: activeRegistry,
111+
onlyPluginIds: [],
112+
});
113+
expect(result).toStrictEqual([]);
114+
expect(mocks.loadOpenClawPlugins).not.toHaveBeenCalled();
135115
});
136116

137117
it("preserves explicit scopes when config is omitted in direct runtime resolution", () => {
138118
const mapRegistryProviders = vi.fn(() => []);
139119
mocks.getLoadedRuntimePluginRegistry.mockReturnValue({} as never);
140120

141-
resolveRuntimeWebProviders(
121+
resolvePluginWebProviders(
142122
{
143123
onlyPluginIds: ["alpha"],
144124
},
@@ -209,34 +189,6 @@ describe("web-provider-runtime-shared", () => {
209189
expect(mocks.loadOpenClawPlugins).not.toHaveBeenCalled();
210190
});
211191

212-
it("preserves explicit empty candidate scopes when reusing the active registry", () => {
213-
const activeRegistry = { source: "active" };
214-
const mapRegistryProviders = vi.fn(() => []);
215-
mocks.getLoadedRuntimePluginRegistry.mockReturnValue(activeRegistry);
216-
217-
resolvePluginWebProviders(
218-
{
219-
config: {},
220-
onlyPluginIds: [],
221-
},
222-
{
223-
resolveBundledResolutionConfig: () => ({
224-
config: {},
225-
activationSourceConfig: {},
226-
autoEnabledReasons: {},
227-
}),
228-
resolveCandidatePluginIds: () => [],
229-
mapRegistryProviders,
230-
},
231-
);
232-
233-
expect(mapRegistryProviders).toHaveBeenCalledWith({
234-
registry: activeRegistry,
235-
onlyPluginIds: [],
236-
});
237-
expect(mocks.loadOpenClawPlugins).not.toHaveBeenCalled();
238-
});
239-
240192
it("uses loaded runtime web providers without runtime plugin loads", () => {
241193
const loadedRegistry = { source: "loaded" };
242194
const mapRegistryProviders = vi.fn(() => ["provider"]);
@@ -436,31 +388,6 @@ describe("web-provider-runtime-shared", () => {
436388
expect(mapRegistryProviders).toHaveBeenCalledTimes(2);
437389
});
438390

439-
it("does not fall back when the active runtime registry returns empty under an explicit empty scope", () => {
440-
const activeRegistry = { source: "active" };
441-
const mapRegistryProviders = vi.fn(() => []);
442-
mocks.getLoadedRuntimePluginRegistry.mockReturnValue(activeRegistry as never);
443-
444-
const result = resolvePluginWebProviders(
445-
{
446-
config: {},
447-
onlyPluginIds: [],
448-
},
449-
{
450-
resolveBundledResolutionConfig: () => ({
451-
config: {},
452-
activationSourceConfig: {},
453-
autoEnabledReasons: {},
454-
}),
455-
resolveCandidatePluginIds: () => [],
456-
mapRegistryProviders,
457-
},
458-
);
459-
460-
expect(result).toStrictEqual([]);
461-
expect(mocks.loadOpenClawPlugins).not.toHaveBeenCalled();
462-
});
463-
464391
it("does not treat an active registry missing declared candidates as authoritative", () => {
465392
// Regression: an active registry with SOME web providers used to win even when a
466393
// manifest-declared candidate (npm-installed Brave with BRAVE_API_KEY set) was
@@ -481,7 +408,7 @@ describe("web-provider-runtime-shared", () => {
481408
});
482409
mocks.loadOpenClawPlugins.mockReturnValue(scopedRegistry as never);
483410

484-
const result = resolveRuntimeWebProviders(
411+
const result = resolvePluginWebProviders(
485412
{
486413
config: {},
487414
env: { BRAVE_API_KEY: "key" } as never,
@@ -501,66 +428,6 @@ describe("web-provider-runtime-shared", () => {
501428
expect(mocks.loadOpenClawPlugins).toHaveBeenCalledTimes(1);
502429
});
503430

504-
it("falls back when the direct runtime registry has no web providers", () => {
505-
const activeRegistry = { source: "active" };
506-
const fallbackRegistry = { source: "fallback" };
507-
const mapRegistryProviders = vi.fn(({ registry }) =>
508-
registry === fallbackRegistry ? ["brave"] : [],
509-
);
510-
mocks.getLoadedRuntimePluginRegistry.mockImplementation((args: unknown) => {
511-
const requiredPluginIds = (args as { requiredPluginIds?: readonly string[] })
512-
?.requiredPluginIds;
513-
if (requiredPluginIds === undefined) {
514-
return activeRegistry as never;
515-
}
516-
return undefined;
517-
});
518-
mocks.loadOpenClawPlugins.mockReturnValue(fallbackRegistry as never);
519-
520-
const result = resolveRuntimeWebProviders(
521-
{
522-
config: {},
523-
},
524-
{
525-
resolveBundledResolutionConfig: () => ({
526-
config: {},
527-
activationSourceConfig: {},
528-
autoEnabledReasons: {},
529-
}),
530-
resolveCandidatePluginIds: () => undefined,
531-
mapRegistryProviders,
532-
},
533-
);
534-
535-
expect(result).toEqual(["brave"]);
536-
expect(mocks.loadOpenClawPlugins).toHaveBeenCalledTimes(1);
537-
});
538-
539-
it("does not fall back when direct runtime registry returns empty under an explicit empty scope", () => {
540-
const activeRegistry = { source: "active" };
541-
const mapRegistryProviders = vi.fn(() => []);
542-
mocks.getLoadedRuntimePluginRegistry.mockReturnValue(activeRegistry as never);
543-
544-
const result = resolveRuntimeWebProviders(
545-
{
546-
config: {},
547-
onlyPluginIds: [],
548-
},
549-
{
550-
resolveBundledResolutionConfig: () => ({
551-
config: {},
552-
activationSourceConfig: {},
553-
autoEnabledReasons: {},
554-
}),
555-
resolveCandidatePluginIds: () => [],
556-
mapRegistryProviders,
557-
},
558-
);
559-
560-
expect(result).toStrictEqual([]);
561-
expect(mocks.loadOpenClawPlugins).not.toHaveBeenCalled();
562-
});
563-
564431
it("keeps explicit setup web provider cache opt-outs", () => {
565432
const loadedRegistry = { source: "setup" };
566433
const mapRegistryProviders = vi.fn(() => ["provider"]);

‎src/plugins/web-provider-runtime-shared.ts‎

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ type WebProviderRuntimeContext = {
8080
onlyPluginIds?: string[];
8181
};
8282

83-
type RuntimeRegistryWebProviderResolution<TEntry> = {
84-
providers: TEntry[];
85-
shouldReturn: boolean;
86-
};
87-
8883
function resolveWebProviderRuntimeContext<TEntry>(
8984
params: ResolvePluginWebProvidersParams,
9085
deps: ResolveWebProviderRuntimeDeps<TEntry>,
@@ -164,25 +159,6 @@ function resolveWebProviderLoadOptions(
164159
);
165160
}
166161

167-
function resolveRuntimeRegistryWebProviders<TEntry>(params: {
168-
hasExplicitEmptyScope: boolean;
169-
mapRegistryProviders: ResolveWebProviderRuntimeDeps<TEntry>["mapRegistryProviders"];
170-
onlyPluginIds?: readonly string[];
171-
registry: PluginRegistry | undefined;
172-
}): RuntimeRegistryWebProviderResolution<TEntry> | undefined {
173-
if (!params.registry) {
174-
return undefined;
175-
}
176-
const providers = params.mapRegistryProviders({
177-
registry: params.registry,
178-
onlyPluginIds: params.onlyPluginIds,
179-
});
180-
return {
181-
providers,
182-
shouldReturn: providers.length > 0 || params.hasExplicitEmptyScope,
183-
};
184-
}
185-
186162
/** Resolves plugin web providers from setup, active runtime, or a scoped load. */
187163
export function resolvePluginWebProviders<TEntry>(
188164
params: ResolvePluginWebProvidersParams,
@@ -250,24 +226,18 @@ export function resolvePluginWebProviders<TEntry>(
250226
workspaceDir: context.workspaceDir,
251227
requiredPluginIds: context.loadPluginIds,
252228
});
253-
const scopedPluginIds = context.onlyPluginIds;
254-
const hasExplicitEmptyScope = scopedPluginIds !== undefined && scopedPluginIds.length === 0;
255-
const compatibleProviders = resolveRuntimeRegistryWebProviders({
256-
hasExplicitEmptyScope,
257-
mapRegistryProviders: deps.mapRegistryProviders,
258-
onlyPluginIds: context.onlyPluginIds,
259-
registry: compatible,
260-
});
261-
if (compatibleProviders?.shouldReturn) {
262-
return compatibleProviders.providers;
263-
}
264-
if (compatibleProviders) {
265-
// The active gateway plugin registry may be otherwise compatible with this
266-
// config while contributing zero web providers (for example when channels,
267-
// memory, harnesses, and sidecars are loaded but Brave/web providers are
268-
// not). Do not treat that empty active registry as authoritative: fall
269-
// through to a scoped provider load below so first-class assistant tools
270-
// still see the configured provider.
229+
const hasExplicitEmptyScope =
230+
context.onlyPluginIds !== undefined && context.onlyPluginIds.length === 0;
231+
// Candidate coverage is checked before reuse. An empty compatible registry is
232+
// authoritative only for an explicit empty scope; otherwise load below.
233+
if (compatible) {
234+
const providers = deps.mapRegistryProviders({
235+
registry: compatible,
236+
onlyPluginIds: context.onlyPluginIds,
237+
});
238+
if (providers.length > 0 || hasExplicitEmptyScope) {
239+
return providers;
240+
}
271241
}
272242
if (isPluginRegistryLoadInFlight(loadOptions)) {
273243
return [];
@@ -297,17 +267,3 @@ export function resolvePluginWebProviders<TEntry>(
297267
onlyPluginIds: context.onlyPluginIds,
298268
});
299269
}
300-
301-
/** Resolves web providers from the active runtime registry before falling back to plugin loading. */
302-
export function resolveRuntimeWebProviders<TEntry>(
303-
params: Omit<ResolvePluginWebProvidersParams, "activate" | "cache" | "mode">,
304-
deps: ResolveWebProviderRuntimeDeps<TEntry>,
305-
): TEntry[] {
306-
// Do not treat the active registry's provider set as authoritative here: it can
307-
// be non-empty while still missing manifest-declared candidates that never load
308-
// at startup (for example an npm-installed Brave plugin with BRAVE_API_KEY set,
309-
// whose manifest uses activation.onStartup=false). resolvePluginWebProviders
310-
// reuses the active registry only when it covers every declared candidate, and
311-
// otherwise runs the same scoped load the explicitly-configured path uses.
312-
return resolvePluginWebProviders(params, deps);
313-
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ import {
88
mapRegistryProviders,
99
resolveManifestDeclaredWebProviderCandidatePluginIds,
1010
} from "./web-provider-resolution-shared.js";
11-
import {
12-
resolvePluginWebProviders,
13-
resolveRuntimeWebProviders,
14-
} from "./web-provider-runtime-shared.js";
11+
import { resolvePluginWebProviders } from "./web-provider-runtime-shared.js";
1512
import {
1613
resolveBundledWebSearchResolutionConfig,
1714
sortWebSearchProviders,
@@ -75,7 +72,7 @@ export function resolveRuntimeWebSearchProviders(params: {
7572
origin?: PluginManifestRecord["origin"];
7673
manifestRecords?: readonly PluginManifestRecord[];
7774
}): PluginWebSearchProviderEntry[] {
78-
return resolveRuntimeWebProviders(params, {
75+
return resolvePluginWebProviders(params, {
7976
resolveBundledResolutionConfig: resolveBundledWebSearchResolutionConfig,
8077
resolveCandidatePluginIds: resolveWebSearchCandidatePluginIds,
8178
mapRegistryProviders: mapRegistryWebSearchProviders,

0 commit comments

Comments
 (0)