Skip to content

Commit 9aeedba

Browse files
committed
refactor(media): consolidate automatic provider selection
1 parent 5d69182 commit 9aeedba

5 files changed

Lines changed: 330 additions & 149 deletions

File tree

‎src/media-understanding/defaults.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Media-understanding defaults tests keep bundled provider metadata priorities,
22
// default models, and native document support aligned with manifests.
33
import { describe, expect, it, vi } from "vitest";
4+
import type { MediaUnderstandingCapability, MediaUnderstandingProvider } from "./types.js";
45

56
const mediaMetadataPlugins = vi.hoisted(() => [
67
{
@@ -112,6 +113,43 @@ import {
112113
} from "./defaults.js";
113114

114115
describe("resolveDefaultMediaModel", () => {
116+
it.each<MediaUnderstandingCapability>(["image", "audio", "video"])(
117+
"uses supplied %s registry defaults before configured models",
118+
(capability) => {
119+
const providerRegistry = new Map<string, MediaUnderstandingProvider>([
120+
["google", { id: "google", defaultModels: { [capability]: " registry-model " } }],
121+
["blank", { id: "blank", defaultModels: { [capability]: " \t " } }],
122+
]);
123+
const models = [" GEMINI ", "blank", "missing"].map((providerId) =>
124+
resolveDefaultMediaModel({
125+
capability,
126+
providerId,
127+
providerRegistry,
128+
cfg: {
129+
models: {
130+
providers: {
131+
google: {
132+
baseUrl: "https://example.invalid",
133+
models: [
134+
{
135+
id: "configured-image",
136+
name: "configured",
137+
input: ["image"],
138+
reasoning: false,
139+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
140+
maxTokens: 1024,
141+
},
142+
],
143+
},
144+
},
145+
},
146+
},
147+
}),
148+
);
149+
expect(models).toEqual(["registry-model", undefined, undefined]);
150+
},
151+
);
152+
115153
it("resolves bundled audio defaults from provider metadata", () => {
116154
expect(resolveDefaultMediaModel({ providerId: "mistral", capability: "audio" })).toBe(
117155
"voxtral-mini-latest",
@@ -169,6 +207,42 @@ describe("resolveDefaultMediaModel", () => {
169207
});
170208

171209
describe("resolveAutoMediaKeyProviders", () => {
210+
it.each<MediaUnderstandingCapability>(["image", "audio", "video"])(
211+
"orders %s priorities without conflating declared capabilities and runtime hooks",
212+
(capability) => {
213+
const hooks = {
214+
describeImage: async () => ({ text: "image" }),
215+
transcribeAudio: async () => ({ text: "audio" }),
216+
describeVideo: async () => ({ text: "video" }),
217+
};
218+
const providers: MediaUnderstandingProvider[] = [
219+
{ id: "z-tie", capabilities: [capability], autoPriority: { [capability]: 2 } },
220+
{ id: "a-tie", capabilities: [capability], autoPriority: { [capability]: 2 } },
221+
{ id: "zero", capabilities: [capability], autoPriority: { [capability]: 0 } },
222+
{ id: "negative", capabilities: [capability], autoPriority: { [capability]: -2 } },
223+
{ id: "nan", capabilities: [capability], autoPriority: { [capability]: Number.NaN } },
224+
{ id: "infinity", capabilities: [capability], autoPriority: { [capability]: Infinity } },
225+
{ id: "declared-empty", capabilities: [], autoPriority: { [capability]: -10 }, ...hooks },
226+
{ id: "hook-only", autoPriority: { [capability]: 1 }, ...hooks },
227+
{ id: "no-hook", autoPriority: { [capability]: -20 } },
228+
{ id: "gemini", capabilities: [capability], autoPriority: { [capability]: 3 } },
229+
{ id: "google", capabilities: [capability], autoPriority: { [capability]: 4 } },
230+
];
231+
const providerRegistry = new Map(providers.map((provider) => [provider.id, provider]));
232+
233+
expect(resolveAutoMediaKeyProviders({ capability, providerRegistry })).toEqual([
234+
"negative",
235+
"zero",
236+
"hook-only",
237+
"a-tie",
238+
"z-tie",
239+
"google",
240+
"google",
241+
]);
242+
expect([...providerRegistry.values()]).toEqual(providers);
243+
},
244+
);
245+
172246
it("keeps the bundled audio fallback order", () => {
173247
expect(resolveAutoMediaKeyProviders({ capability: "audio" })).toEqual([
174248
"openai",

‎src/media-understanding/defaults.ts‎

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// metadata, and capability declarations.
33
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
44
import { uniqueStrings } from "@openclaw/normalization-core/string-normalization";
5-
import { providerSupportsCapability } from "../../packages/media-understanding-common/src/provider-supports.js";
65
import { resolveRuntimeConfigCacheKey } from "../config/runtime-snapshot.js";
76
import type { OpenClawConfig } from "../config/types.js";
87
import { pruneMapToMaxSize } from "../infra/map-size.js";
98
import { buildMediaUnderstandingManifestMetadataRegistry } from "./manifest-metadata.js";
109
import {
1110
normalizeMediaExecutionProviderId,
1211
normalizeMediaProviderId,
12+
resolveAutoMediaKeyProvidersFromRegistry,
13+
resolveDefaultMediaModelFromRegistry,
1314
} from "./provider-registry.js";
1415
import type { MediaUnderstandingCapability, MediaUnderstandingProvider } from "./types.js";
1516
export {
@@ -58,15 +59,6 @@ function resolveDefaultRegistry(cfg?: OpenClawConfig, workspaceDir?: string) {
5859
return cacheConfigRegistry(cacheKey, registry);
5960
}
6061

61-
function providerHasDeclaredCapability(
62-
provider: MediaUnderstandingProvider | undefined,
63-
capability: MediaUnderstandingCapability,
64-
): boolean {
65-
return (
66-
provider?.capabilities?.includes(capability) ?? providerSupportsCapability(provider, capability)
67-
);
68-
}
69-
7062
function resolveConfiguredImageProviderModel(params: {
7163
cfg?: OpenClawConfig;
7264
providerId: string;
@@ -161,14 +153,11 @@ export function resolveDefaultMediaModel(params: {
161153
}
162154
const registry =
163155
params.providerRegistry ?? resolveDefaultRegistry(params.cfg, params.workspaceDir);
164-
const provider = registry.get(normalizeMediaProviderId(params.providerId));
165-
const manifestDefaultModel = normalizeOptionalString(
166-
provider?.defaultModels?.[params.capability],
167-
);
168-
if (manifestDefaultModel) {
169-
return manifestDefaultModel;
170-
}
171-
return undefined;
156+
return resolveDefaultMediaModelFromRegistry({
157+
providerId: params.providerId,
158+
capability: params.capability,
159+
providerRegistry: registry,
160+
});
172161
}
173162

174163
/** Resolves auto-discovery provider order for a media capability using manifest priorities. */
@@ -180,27 +169,10 @@ export function resolveAutoMediaKeyProviders(params: {
180169
}): string[] {
181170
const registry =
182171
params.providerRegistry ?? resolveDefaultRegistry(params.cfg, params.workspaceDir);
183-
type AutoProviderEntry = {
184-
provider: MediaUnderstandingProvider;
185-
priority: number;
186-
};
187-
const prioritized = [...registry.values()]
188-
.filter((provider) => providerHasDeclaredCapability(provider, params.capability))
189-
.map((provider): AutoProviderEntry | null => {
190-
const priority = provider.autoPriority?.[params.capability];
191-
return typeof priority === "number" && Number.isFinite(priority)
192-
? { provider, priority }
193-
: null;
194-
})
195-
.filter((entry): entry is AutoProviderEntry => entry !== null)
196-
.toSorted((left, right) => {
197-
if (left.priority !== right.priority) {
198-
return left.priority - right.priority;
199-
}
200-
return left.provider.id.localeCompare(right.provider.id);
201-
})
202-
.map((entry) => normalizeMediaProviderId(entry.provider.id))
203-
.filter(Boolean);
172+
const prioritized = resolveAutoMediaKeyProvidersFromRegistry({
173+
capability: params.capability,
174+
providerRegistry: registry,
175+
});
204176
if (params.providerRegistry || params.capability !== "image") {
205177
return prioritized;
206178
}

‎src/media-understanding/provider-registry.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
1+
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
12
import { normalizeMediaProviderId } from "../../packages/media-understanding-common/src/provider-id.js";
3+
import { providerSupportsCapability } from "../../packages/media-understanding-common/src/provider-supports.js";
24
import type { OpenClawConfig } from "../config/types.js";
35
import { resolvePluginCapabilityProviders } from "../plugins/capability-provider-runtime.js";
46
import { resolveImageCapableConfigProviderIds } from "./config-provider-models.js";
57
import { describeImageWithModel, describeImagesWithModel } from "./image-runtime.js";
6-
import type { MediaUnderstandingProvider } from "./types.js";
8+
import type { MediaUnderstandingCapability, MediaUnderstandingProvider } from "./types.js";
9+
10+
export function resolveDefaultMediaModelFromRegistry(params: {
11+
providerId: string;
12+
capability: MediaUnderstandingCapability;
13+
providerRegistry: Map<string, MediaUnderstandingProvider>;
14+
}): string | undefined {
15+
const provider = params.providerRegistry.get(normalizeMediaProviderId(params.providerId));
16+
return normalizeOptionalString(provider?.defaultModels?.[params.capability]);
17+
}
18+
19+
export function resolveAutoMediaKeyProvidersFromRegistry(params: {
20+
capability: MediaUnderstandingCapability;
21+
providerRegistry: Map<string, MediaUnderstandingProvider>;
22+
}): string[] {
23+
type AutoProviderEntry = {
24+
provider: MediaUnderstandingProvider;
25+
priority: number;
26+
};
27+
return [...params.providerRegistry.values()]
28+
.filter(
29+
(provider) =>
30+
provider.capabilities?.includes(params.capability) ??
31+
providerSupportsCapability(provider, params.capability),
32+
)
33+
.map((provider): AutoProviderEntry | null => {
34+
const priority = provider.autoPriority?.[params.capability];
35+
return typeof priority === "number" && Number.isFinite(priority)
36+
? { provider, priority }
37+
: null;
38+
})
39+
.filter((entry): entry is AutoProviderEntry => entry !== null)
40+
.toSorted((left, right) => {
41+
if (left.priority !== right.priority) {
42+
return left.priority - right.priority;
43+
}
44+
return left.provider.id.localeCompare(right.provider.id);
45+
})
46+
.map((entry) => normalizeMediaProviderId(entry.provider.id))
47+
.filter(Boolean);
48+
}
749

850
function mergeProviderIntoRegistry(
951
registry: Map<string, MediaUnderstandingProvider>,

0 commit comments

Comments
 (0)