diff --git a/packages/core/src/models-dev.ts b/packages/core/src/models-dev.ts index 79d85807a95..3ed24632cf7 100644 --- a/packages/core/src/models-dev.ts +++ b/packages/core/src/models-dev.ts @@ -240,17 +240,16 @@ function entries(record: Record | undefined, decode: (input: } function route(providerID: Provider.ID, model: SourceModel, unknown: Set): Route | undefined { - // A chat model uses its first API with a package. Other types (image, decision…) only run through - // opencode's own packages: among the ones this build can load, they prefer the entrypoint for their type - // (`providers/

/images`) over a chat API listed first, then their first API with one, and are skipped without one. + // A chat model uses its first API with a package. Other types (image, decision…) only run through the + // opencode entrypoint for their type (`providers/

/images`) that this build can load, and are skipped + // without one: a chat entrypoint loads a chat model, which can't generate images or decisions. const apis = entries(model.api, decodeApi).map( ([protocol, entry]) => [protocol, entry, loadable(entry.sdk?.opencodeai, unknown)] as const, ) const [protocol, api, opencodeai] = (model.type === "chat" ? apis.find(([, entry, opencodeai]) => opencodeai || entry.sdk?.aisdk) - : (apis.find(([, , opencodeai]) => opencodeai?.endsWith(`/${model.type}s`)) ?? - apis.find(([, , opencodeai]) => opencodeai))) ?? [] + : apis.find(([, , opencodeai]) => opencodeai?.endsWith(`/${model.type}s`))) ?? [] if (!protocol || !api) return undefined const aisdk = api.sdk?.aisdk const packageName = diff --git a/packages/core/test/models.test.ts b/packages/core/test/models.test.ts index b9a3801a2f5..50963583fe6 100644 --- a/packages/core/test/models.test.ts +++ b/packages/core/test/models.test.ts @@ -496,11 +496,52 @@ describe("ModelsDev Service", () => { }), ) + it.effect("skips non-chat models whose only opencode package serves another type", () => + Effect.gen(function* () { + const api = (opencodeai: string) => ({ + "chat-completions": { base_url: "https://api.acme.test/v1", sdk: { opencodeai } }, + }) + const chatPackage = "@opencode/ai/providers/openai-compatible" + const [acme] = yield* ModelsDev.decodeSnapshot( + JSON.stringify({ + acme: { + ...fixture.acme, + models: { + chat: sourceModel("chat", { api: api(chatPackage) }), + "image-chat-only": sourceModel("image-chat-only", { type: "image", api: api(chatPackage) }), + image: sourceModel("image", { + type: "image", + api: { + ...api(chatPackage), + images: api("@opencode/ai/providers/openai-compatible/images")["chat-completions"], + }, + }), + "decision-chat-only": sourceModel("decision-chat-only", { type: "decision", api: api(chatPackage) }), + "decision-image-only": sourceModel("decision-image-only", { + type: "decision", + api: api("@opencode/ai/providers/openrouter/images"), + }), + decision: sourceModel("decision", { + type: "decision", + api: api("@opencode/ai/providers/openrouter/decisions"), + }), + }, + }, + }), + ) + expect(acme?.models.map((item) => [item.id, item.type, item.package ?? acme.info.package])).toEqual([ + [Model.ID.make("chat"), "chat", chatPackage], + [Model.ID.make("image"), "image", "@opencode/ai/providers/openai-compatible/images"], + [Model.ID.make("decision"), "decision", "@opencode/ai/providers/openrouter/decisions"], + ]) + }), + ) + it.live("drops model types opencode doesn't keep", () => Effect.gen(function* () { const cache = makeCache() const api = { - images: { base_url: "https://api.acme.test/v1", sdk: { opencodeai: "@opencode/ai/providers/openai" } }, + images: { base_url: "https://api.acme.test/v1", sdk: { opencodeai: "@opencode/ai/providers/openai/images" } }, } const model = (id: string, type: string) => ({ id,