test: harden media provider auto-registration (#56279) (thanks @Ezio0)

This commit is contained in:
Peter Steinberger
2026-04-04 11:35:19 +01:00
parent 277df463d6
commit 5bef64bc31
2 changed files with 60 additions and 2 deletions

View File

@@ -82,4 +82,56 @@ describe("media-understanding provider registry", () => {
expect(glmProvider?.describeImages).toBeDefined();
expect(textOnlyProvider).toBeUndefined();
});
it("does not override plugin-registered providers when config also has image-capable models", async () => {
const pluginRegistry = createEmptyPluginRegistry();
pluginRegistry.mediaUnderstandingProviders.push({
pluginId: "google",
pluginName: "Google Plugin",
source: "test",
provider: {
id: "google",
capabilities: ["image", "audio", "video"],
describeImage: async () => ({ text: "plugin image" }),
transcribeAudio: async () => ({ text: "plugin audio" }),
},
});
setActivePluginRegistry(pluginRegistry);
const cfg = {
models: {
providers: {
google: {
models: [{ id: "custom-gemini", input: ["text", "image"] }],
},
},
},
} as never;
const registry = buildMediaUnderstandingRegistry(undefined, cfg);
const provider = getMediaUnderstandingProvider("google", registry);
expect(provider?.capabilities).toEqual(["image", "audio", "video"]);
expect(await provider?.describeImage?.({} as never)).toEqual({ text: "plugin image" });
expect(await provider?.transcribeAudio?.({} as never)).toEqual({ text: "plugin audio" });
});
it("does not auto-register providers with audio or video only inputs", () => {
const cfg = {
models: {
providers: {
avOnly: {
models: [
{ id: "audio-model", input: ["text", "audio"] },
{ id: "video-model", input: ["text", "video"] },
],
},
},
},
} as never;
const registry = buildMediaUnderstandingRegistry(undefined, cfg);
expect(getMediaUnderstandingProvider("avOnly", registry)).toBeUndefined();
});
});

View File

@@ -4,6 +4,12 @@ import { describeImageWithModel, describeImagesWithModel } from "./image-runtime
import { normalizeMediaProviderId } from "./provider-id.js";
import type { MediaUnderstandingProvider } from "./types.js";
type ConfigProvider = NonNullable<
NonNullable<NonNullable<OpenClawConfig["models"]>["providers"]>[string]
>;
type ConfigProviderModel = NonNullable<ConfigProvider["models"]>[number];
function mergeProviderIntoRegistry(
registry: Map<string, MediaUnderstandingProvider>,
provider: MediaUnderstandingProvider,
@@ -47,9 +53,9 @@ export function buildMediaUnderstandingRegistry(
if (registry.has(normalizedKey)) {
continue;
}
const models = (providerCfg as { models?: Array<{ input?: string[] }> })?.models ?? [];
const models = providerCfg.models ?? [];
const hasImageModel = models.some(
(m) => Array.isArray(m?.input) && m.input.includes("image"),
(m: ConfigProviderModel) => Array.isArray(m?.input) && m.input.includes("image"),
);
if (hasImageModel) {
const autoProvider: MediaUnderstandingProvider = {