diff --git a/packages/core/src/plugin/variant.ts b/packages/core/src/plugin/variant.ts index 7d2fe6740144..6a8178b93a8b 100644 --- a/packages/core/src/plugin/variant.ts +++ b/packages/core/src/plugin/variant.ts @@ -1,34 +1,77 @@ export * as VariantPlugin from "./variant.js" -import { Effect } from "effect" +import { type Entry } from "@opencode-ai/schema/config" +import { Effect, Stream } from "effect" import { define } from "@opencode-ai/plugin/effect/plugin" +import { Config } from "../config.js" import { Model } from "../model.js" import { Provider } from "../provider.js" export const Plugin = define({ id: "opencode.variant", effect: Effect.fn(function* (ctx) { + const config = yield* Config.Service + const loaded = { entries: yield* config.entries() } yield* ctx.catalog.transform((catalog) => { + const configured = configuredModels(loaded.entries) for (const record of catalog.provider.list()) { for (const model of record.models.values()) { catalog.model.update(model.providerID, model.id, (draft) => { - const generated = generate(draft, record.provider) - if (generated.length === 0) return - - const variants = draft.variants ?? [] - const explicit = new Map(variants.map((variant) => [variant.id, variant])) - const generatedIDs = new Set(generated.map((variant) => variant.id)) - draft.variants = [ - ...generated.map((variant) => explicit.get(variant.id) ?? variant), - ...variants.filter((variant) => !generatedIDs.has(variant.id)), - ] + const intent = configured.get(`${model.providerID}\0${model.id}`) + if (intent === "clear") { + draft.variants = [] + return + } + if (intent === "suppress") return + if (intent === "fallback") { + if (draft.variants.length > 0) return + apply(draft, fallback(draft, record.provider)) + return + } + apply(draft, generate(draft, record.provider)) }) } } }) + yield* ctx.event.subscribe().pipe( + Stream.filter((event) => event.type === "config.updated"), + Stream.runForEach(() => + config.entries().pipe( + Effect.tap((entries) => Effect.sync(() => (loaded.entries = entries))), + Effect.andThen(ctx.catalog.reload()), + ), + ), + Effect.forkScoped({ startImmediately: true }), + ) }), }) +function apply(draft: Model.MutableInfo, generated: NonNullable) { + if (generated.length === 0) return + const variants = draft.variants ?? [] + const explicit = new Map(variants.map((variant) => [variant.id, variant])) + const generatedIDs = new Set(generated.map((variant) => variant.id)) + draft.variants = [ + ...generated.map((variant) => explicit.get(variant.id) ?? variant), + ...variants.filter((variant) => !generatedIDs.has(variant.id)), + ] +} + +function configuredModels(entries: readonly Entry[]) { + const result = new Map() + for (const entry of entries) { + if (entry.type !== "document") continue + for (const [providerID, provider] of Object.entries(entry.info.providers ?? {})) { + for (const [modelID, model] of Object.entries(provider.models ?? {})) { + const key = `${providerID}\0${modelID}` + if (!result.has(key)) result.set(key, "fallback") + if (model.variants !== undefined) result.set(key, model.variants.length === 0 ? "clear" : "suppress") + } + } + } + return result +} + export function generate( model: { readonly id: string; readonly modelID?: string; readonly package?: string }, provider?: { readonly package: string }, @@ -42,3 +85,153 @@ export function generate( settings: { reasoningEffort: id }, })) } + +const OPENAI_EFFORTS = ["none", "low", "medium", "high", "xhigh", "max"] +const COMMON_EFFORTS = ["low", "medium", "high"] +const ENCRYPTED_REASONING = ["reasoning.encrypted_content"] +const CLAUDE_MANUAL_THINKING_MAX = { haiku: [4, 5], sonnet: [4, 5], opus: [4, 5] } as const + +export function fallback( + model: { + readonly modelID: string + readonly package?: string + readonly settings?: Readonly> + readonly limit: { readonly output: number } + }, + provider?: { readonly package: string }, +): NonNullable { + const packageName = model.package ?? provider?.package + if (openAIResponses(packageName, model.settings)) + return OPENAI_EFFORTS.map((id) => ({ + id: Model.VariantID.make(id), + settings: settings(packageName, { + reasoningEffort: id, + reasoningSummary: "auto", + include: ENCRYPTED_REASONING, + }), + })) + if (openAIChat(packageName, model.settings)) return efforts(packageName, COMMON_EFFORTS) + if (google(packageName)) return googleVariants(packageName, model.modelID, model.limit.output) + if (anthropic(packageName)) return anthropicVariants(packageName, model.modelID, model.limit.output) + return [] +} + +function openAIResponses(packageName: string | undefined, settings: Readonly> | undefined) { + if (Provider.isAISDK(packageName)) + return ( + Provider.packageName(packageName) === "@ai-sdk/openai" || + (Provider.packageName(packageName) === "@ai-sdk/azure" && settings?.useCompletionUrls !== true) + ) + return [ + "@opencode-ai/ai/providers/openai", + "@opencode-ai/ai/providers/openai/responses", + "@opencode-ai/ai/providers/azure", + "@opencode-ai/ai/providers/azure/responses", + "@opencode-ai/ai/providers/google-vertex/responses", + ].includes(packageName ?? "") +} + +function openAIChat(packageName: string | undefined, settings: Readonly> | undefined) { + if (Provider.isAISDK(packageName)) + return ( + Provider.packageName(packageName) === "@ai-sdk/openai-compatible" || + (Provider.packageName(packageName) === "@ai-sdk/azure" && settings?.useCompletionUrls === true) + ) + return [ + "@opencode-ai/ai/providers/openai/chat", + "@opencode-ai/ai/providers/openai-compatible", + "@opencode-ai/ai/providers/azure/chat", + "@opencode-ai/ai/providers/google-vertex/chat", + ].includes(packageName ?? "") +} + +function google(packageName: string | undefined) { + if (Provider.isAISDK(packageName)) + return ["@ai-sdk/google", "@ai-sdk/google-vertex"].includes(Provider.packageName(packageName)) + return [ + "@opencode-ai/ai/providers/google", + "@opencode-ai/ai/providers/google-vertex", + "@opencode-ai/ai/providers/google-vertex/gemini", + ].includes(packageName ?? "") +} + +function anthropic(packageName: string | undefined) { + if (Provider.isAISDK(packageName)) + return ["@ai-sdk/anthropic", "@ai-sdk/google-vertex/anthropic"].includes(Provider.packageName(packageName)) + return [ + "@opencode-ai/ai/providers/anthropic", + "@opencode-ai/ai/providers/anthropic-compatible", + "@opencode-ai/ai/providers/google-vertex/messages", + ].includes(packageName ?? "") +} + +function settings(packageName: string | undefined, value: Readonly>) { + return Provider.isAISDK(packageName) ? value : { providerOptions: value } +} + +function efforts(packageName: string | undefined, ids: readonly string[]) { + return ids.map((id) => ({ id: Model.VariantID.make(id), settings: settings(packageName, { reasoningEffort: id }) })) +} + +function googleVariants( + packageName: string | undefined, + modelID: string, + output: number, +): NonNullable { + if (/(?:^|[/.:_-])gemini-2[.-]5(?:[/.:_-]|$)/i.test(modelID)) { + const maximum = output - 1 + if (maximum <= 0) return [] + return [ + { id: "high", budget: 16_000 }, + { id: "max", budget: /(?:^|[/.:_-])pro(?:[/.:_-]|$)/i.test(modelID) ? 32_768 : 24_576 }, + ].map((item) => ({ + id: Model.VariantID.make(item.id), + settings: settings(packageName, { + thinkingConfig: { includeThoughts: true, thinkingBudget: Math.min(item.budget, maximum) }, + }), + })) + } + return COMMON_EFFORTS.map((effort) => ({ + id: Model.VariantID.make(effort), + settings: settings(packageName, { thinkingConfig: { includeThoughts: true, thinkingLevel: effort } }), + })) +} + +function anthropicVariants( + packageName: string | undefined, + modelID: string, + output: number, +): NonNullable { + const model = claudeModel(modelID) + const version = model && CLAUDE_MANUAL_THINKING_MAX[model.family] + const manual = version && (model.major < version[0] || (model.major === version[0] && model.minor <= version[1])) + if (manual) { + const maximum = Math.min(31_999, output - 1) + if (maximum <= 0) return [] + return [ + { id: "high", budget: Math.min(16_000, maximum) }, + { id: "max", budget: maximum }, + ].map((item) => ({ + id: Model.VariantID.make(item.id), + settings: settings(packageName, { thinking: { type: "enabled", budgetTokens: item.budget } }), + })) + } + const ids = + !model || model.major > 4 || model.minor >= 7 ? [...COMMON_EFFORTS, "xhigh", "max"] : [...COMMON_EFFORTS, "max"] + return ids.map((id) => ({ + id: Model.VariantID.make(id), + settings: settings(packageName, { thinking: { type: "adaptive", display: "summarized" }, effort: id }), + })) +} + +function claudeModel(modelID: string) { + const familyFirst = /(?:^|[/.:_-])(opus|sonnet|haiku)-([1-9]\d*)(?:[.-](\d{1,2}))?(?:[/.:_-]|$)/i.exec(modelID) + const versionFirst = /(?:^|[/.:_-])claude-([1-9]\d*)(?:[.-](\d{1,2}))?-(opus|sonnet|haiku)(?:[/.:_-]|$)/i.exec( + modelID, + ) + const family = (["haiku", "sonnet", "opus"] as const).find((item) => item === (familyFirst?.[1] ?? versionFirst?.[3])) + const major = Number(familyFirst?.[2] ?? versionFirst?.[1]) + const minor = Number(familyFirst?.[3] ?? versionFirst?.[2] ?? 0) + if (!family || !Number.isFinite(major) || !Number.isFinite(minor)) return + return { family, major, minor } +} diff --git a/packages/core/test/config/plugin.test.ts b/packages/core/test/config/plugin.test.ts index 34184bccd791..169f971af7fe 100644 --- a/packages/core/test/config/plugin.test.ts +++ b/packages/core/test/config/plugin.test.ts @@ -399,10 +399,38 @@ describe("PluginSupervisor config", () => { }), ) + it.live("lets an explicit empty config array clear generated variants", () => + withLocation( + { + plugins: [path.join(import.meta.dir, "../plugin/fixtures/variant-source-plugin.ts")], + providers: { + configured: { + models: { + "glm-5.2": { variants: [] }, + }, + }, + }, + }, + Effect.gen(function* () { + yield* ready() + const catalog = yield* Catalog.Service + expect((yield* catalog.model.get(Provider.ID.make("configured"), Model.ID.make("glm-5.2")))?.variants).toEqual( + [], + ) + }), + ), + ) + it.live("allows variant generation to be disabled", () => withLocation( { plugins: [path.join(import.meta.dir, "../plugin/fixtures/variant-source-plugin.ts"), "-opencode.variant"], + providers: { + custom: { + package: "aisdk:@ai-sdk/openai", + models: { reasoner: {} }, + }, + }, }, Effect.gen(function* () { yield* ready() @@ -413,6 +441,7 @@ describe("PluginSupervisor config", () => { expect((yield* catalog.model.get(Provider.ID.make("configured"), Model.ID.make("glm-5.2")))?.variants).toEqual([ expect.objectContaining({ id: "high", headers: { custom: "true" } }), ]) + expect((yield* catalog.model.get(Provider.ID.make("custom"), Model.ID.make("reasoner")))?.variants).toEqual([]) }), ), ) diff --git a/packages/core/test/config/provider.test.ts b/packages/core/test/config/provider.test.ts index b06dccc75b7b..ef6f69b148c8 100644 --- a/packages/core/test/config/provider.test.ts +++ b/packages/core/test/config/provider.test.ts @@ -9,16 +9,18 @@ import { Integration } from "@opencode-ai/core/integration" import { Model } from "@opencode-ai/core/model" import { Plugin } from "@opencode-ai/core/plugin" import { PluginHost } from "@opencode-ai/core/plugin/host" +import { VariantPlugin } from "@opencode-ai/core/plugin/variant" import { Provider } from "@opencode-ai/core/provider" import { testEffect } from "../lib/effect" import { PluginTestLayer } from "../plugin/fixture" const it = testEffect(PluginTestLayer) -const addPlugin = Effect.fn(function* (entries: Entry[]) { +const addPlugin = Effect.fn(function* (entries: Entry[], variants = false) { const plugin = yield* Plugin.Service const host = yield* PluginHost.make(plugin) yield* ConfigProviderPlugin.Plugin.effect(host).pipe(Effect.provide(Config.testLayer(entries))) + if (variants) yield* VariantPlugin.Plugin.effect(host).pipe(Effect.provide(Config.testLayer(entries))) }) function required(value: T | undefined): T { @@ -104,6 +106,179 @@ describe("ConfigProviderPlugin.Plugin", () => { }), ) + it.effect("adds fallback variants to new configured models when variants are omitted", () => + Effect.gen(function* () { + const catalog = yield* Catalog.Service + const providerID = Provider.ID.make("custom") + const modelID = Model.ID.make("gpt-next") + const entries = [ + new Document({ + type: "document", + info: decode({ + providers: { + custom: { + package: "aisdk:@ai-sdk/openai", + models: { "gpt-next": {} }, + }, + }, + }), + }), + ] + + yield* addPlugin(entries, true) + + const variants = required(yield* catalog.model.get(providerID, modelID)).variants + expect(variants.map((variant) => String(variant.id))).toEqual(["none", "low", "medium", "high", "xhigh", "max"]) + expect(variants[3]?.settings).toEqual({ + reasoningEffort: "high", + reasoningSummary: "auto", + include: ["reasoning.encrypted_content"], + }) + }), + ) + + it.effect("keeps explicit empty and custom configured variants", () => + Effect.gen(function* () { + const catalog = yield* Catalog.Service + const providerID = Provider.ID.make("custom") + const entries = [ + new Document({ + type: "document", + info: decode({ + providers: { + custom: { + package: "aisdk:@ai-sdk/openai", + models: { + disabled: { variants: [] }, + explicit: { variants: [{ id: "deep", settings: { reasoningEffort: "max" } }] }, + }, + }, + }, + }), + }), + ] + + yield* addPlugin(entries, true) + + expect((yield* catalog.model.get(providerID, Model.ID.make("disabled")))?.variants).toEqual([]) + expect( + (yield* catalog.model.get(providerID, Model.ID.make("explicit")))?.variants.map((variant) => ({ + ...variant, + id: String(variant.id), + })), + ).toEqual([{ id: "deep", settings: { reasoningEffort: "max" } }]) + }), + ) + + it.effect("preserves authoritative catalog variants", () => + Effect.gen(function* () { + const catalog = yield* Catalog.Service + const providerID = Provider.ID.make("custom") + const modelID = Model.ID.make("known") + yield* catalog.transform((draft) => { + draft.model.update(providerID, modelID, (model) => { + model.variants = [{ id: Model.VariantID.make("custom"), settings: { reasoningEffort: "high" } }] + }) + }) + const entries = [ + new Document({ + type: "document", + info: decode({ + providers: { + custom: { + package: "aisdk:@ai-sdk/openai", + models: { known: { name: "Known" } }, + }, + }, + }), + }), + ] + + yield* addPlugin(entries, true) + + expect((yield* catalog.model.get(providerID, modelID))?.variants).toEqual([ + { id: Model.VariantID.make("custom"), settings: { reasoningEffort: "high" } }, + ]) + }), + ) + + it.effect("lets an explicit empty array clear inherited variants", () => + Effect.gen(function* () { + const catalog = yield* Catalog.Service + const providerID = Provider.ID.make("custom") + const modelID = Model.ID.make("known") + yield* catalog.transform((draft) => { + draft.model.update(providerID, modelID, (model) => { + model.variants = [{ id: Model.VariantID.make("high"), settings: { reasoningEffort: "high" } }] + }) + }) + const entries = [ + new Document({ + type: "document", + info: decode({ + providers: { + custom: { + package: "aisdk:@ai-sdk/openai", + models: { known: { variants: [] } }, + }, + }, + }), + }), + ] + + yield* addPlugin(entries, true) + + expect((yield* catalog.model.get(providerID, modelID))?.variants).toEqual([]) + }), + ) + + it.effect("respects layered variant intent and the final package flavor", () => + Effect.gen(function* () { + const catalog = yield* Catalog.Service + const providerID = Provider.ID.make("custom") + const entries = [ + new Document({ + type: "document", + info: decode({ + providers: { + custom: { + package: "aisdk:@ai-sdk/openai-compatible", + models: { + cleared: {}, + disabled: { variants: [] }, + flavor: {}, + }, + }, + }, + }), + }), + new Document({ + type: "document", + info: decode({ + providers: { + custom: { + package: "aisdk:@ai-sdk/openai", + models: { + cleared: { variants: [] }, + disabled: { name: "Disabled" }, + flavor: { name: "OpenAI" }, + }, + }, + }, + }), + }), + ] + + yield* addPlugin(entries, true) + + expect((yield* catalog.model.get(providerID, Model.ID.make("cleared")))?.variants).toEqual([]) + expect((yield* catalog.model.get(providerID, Model.ID.make("disabled")))?.variants).toEqual([]) + expect( + (yield* catalog.model.get(providerID, Model.ID.make("flavor")))?.variants.map((variant) => String(variant.id)), + ).toEqual(["none", "low", "medium", "high", "xhigh", "max"]) + }), + ) + it.effect("preserves catalog capabilities unless config overrides them", () => Effect.gen(function* () { const catalog = yield* Catalog.Service diff --git a/packages/core/test/model-resolver.test.ts b/packages/core/test/model-resolver.test.ts index 7d7ee3bafeaa..634724a17edc 100644 --- a/packages/core/test/model-resolver.test.ts +++ b/packages/core/test/model-resolver.test.ts @@ -9,6 +9,7 @@ import { Integration } from "@opencode-ai/core/integration" import { Compatibility, ID, Info, VariantID } from "@opencode-ai/core/model" import { Provider } from "@opencode-ai/core/provider" import { ModelResolver } from "@opencode-ai/core/model-resolver" +import { VariantPlugin } from "@opencode-ai/core/plugin/variant" import { Catalog } from "@opencode-ai/core/catalog" import { AISDK } from "@opencode-ai/core/aisdk" import { Npm } from "@opencode-ai/util/npm" @@ -509,6 +510,34 @@ describe("ModelResolver", () => { }), ) + it.effect("applies native OpenAI fallback settings to Responses requests", () => + Effect.gen(function* () { + const packageName = "@opencode-ai/ai/providers/openai" + const base = model(packageName, { modelID: "gpt-next", limit: { context: 100, output: 32_000 } }) + const catalog = model(packageName, { + modelID: "gpt-next", + limit: { context: 100, output: 32_000 }, + variants: VariantPlugin.fallback(base), + }) + const resolved = yield* ModelResolver.resolveModel( + catalog, + VariantID.make("high"), + Credential.Key.make({ type: "key", key: "secret" }), + ) + + expect(resolved.route.defaults.providerOptions).toMatchObject({ + reasoningEffort: "high", + reasoningSummary: "auto", + include: ["reasoning.encrypted_content"], + }) + const prepared = yield* compileRequest(LLM.request({ model: resolved, prompt: "Hello" })) + expect(prepared.body).toMatchObject({ + include: ["reasoning.encrypted_content"], + reasoning: { effort: "high", summary: "auto" }, + }) + }), + ) + it.effect("overlays selected OpenAI-compatible variant bodies", () => Effect.gen(function* () { const catalog = model(Provider.aisdk("@ai-sdk/openai-compatible"), { @@ -885,12 +914,7 @@ describe("ModelResolver", () => { { reasoning: { effort: "high" } }, { reasoning: { effort: "high" } }, ], - [ - "@ai-sdk/xai", - "@opencode-ai/ai/providers/xai", - { reasoningEffort: "high" }, - { reasoningEffort: "high" }, - ], + ["@ai-sdk/xai", "@opencode-ai/ai/providers/xai", { reasoningEffort: "high" }, { reasoningEffort: "high" }], ] as const yield* Effect.forEach(packages, ([catalogPackage, nativePackage, sourceOptions, providerOptions]) => @@ -940,11 +964,7 @@ describe("ModelResolver", () => { ["@ai-sdk/azure", "@opencode-ai/ai/providers/azure/responses", "api-model"], ["@ai-sdk/google", "@opencode-ai/ai/providers/google", "api-model"], ["@ai-sdk/google-vertex", "@opencode-ai/ai/providers/google-vertex", "api-model"], - [ - "@ai-sdk/google-vertex/anthropic", - "@opencode-ai/ai/providers/google-vertex/messages", - "claude-sonnet-4-6", - ], + ["@ai-sdk/google-vertex/anthropic", "@opencode-ai/ai/providers/google-vertex/messages", "claude-sonnet-4-6"], ["@ai-sdk/openai", "@opencode-ai/ai/providers/openai", "api-model"], ["@ai-sdk/openai-compatible", "@opencode-ai/ai/providers/openai-compatible", "api-model"], ["@openrouter/ai-sdk-provider", "@opencode-ai/ai/providers/openrouter", "api-model"], diff --git a/packages/core/test/plugin/variant.test.ts b/packages/core/test/plugin/variant.test.ts index 1df1082fb663..6926a41e7f76 100644 --- a/packages/core/test/plugin/variant.test.ts +++ b/packages/core/test/plugin/variant.test.ts @@ -1,5 +1,6 @@ -import { describe, expect } from "bun:test" +import { describe, expect, test } from "bun:test" import { Catalog } from "@opencode-ai/core/catalog" +import { Config } from "@opencode-ai/core/config" import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder" import { LayerNode } from "@opencode-ai/util/effect/layer-node" import { Location } from "@opencode-ai/core/location" @@ -31,7 +32,9 @@ describe("VariantPlugin", () => { model.package = Provider.aisdk("@ai-sdk/openai-compatible") }) }) - yield* VariantPlugin.Plugin.effect(host({ catalog: catalogHost(service) })) + yield* VariantPlugin.Plugin.effect(host({ catalog: catalogHost(service) })).pipe( + Effect.provide(Config.testLayer([])), + ) expect((yield* service.model.get(Provider.ID.opencode, Model.ID.make("glm-5.2")))?.variants).toEqual([ expect.objectContaining({ id: "high", settings: { reasoningEffort: "high" } }), @@ -50,7 +53,9 @@ describe("VariantPlugin", () => { model.variants = [{ id: Model.VariantID.make("high"), settings: {}, headers: { custom: "true" }, body: {} }] }) }) - yield* VariantPlugin.Plugin.effect(host({ catalog: catalogHost(service) })) + yield* VariantPlugin.Plugin.effect(host({ catalog: catalogHost(service) })).pipe( + Effect.provide(Config.testLayer([])), + ) expect((yield* service.model.get(Provider.ID.opencode, Model.ID.make("glm-5.2")))?.variants).toEqual([ expect.objectContaining({ id: "high", headers: { custom: "true" } }), @@ -59,3 +64,153 @@ describe("VariantPlugin", () => { }), ) }) + +describe("VariantPlugin.fallback", () => { + const model = ( + modelID: string, + packageName: string, + output = 32_000, + settings?: Readonly>, + ) => ({ + modelID, + package: packageName, + settings, + limit: { output }, + }) + const plain = (variants: Model.Info["variants"]) => + variants.map((variant) => ({ ...variant, id: String(variant.id) })) + const settings = (packageName: string, value: Readonly>) => + Provider.isAISDK(packageName) ? value : { providerOptions: value } + + test.each([ + Provider.aisdk("@ai-sdk/openai"), + Provider.aisdk("@ai-sdk/azure"), + "@opencode-ai/ai/providers/openai", + "@opencode-ai/ai/providers/openai/responses", + "@opencode-ai/ai/providers/azure", + "@opencode-ai/ai/providers/azure/responses", + "@opencode-ai/ai/providers/google-vertex/responses", + ])("adds OpenAI Responses variants for %s", (packageName) => { + const variants = VariantPlugin.fallback(model("gpt-next", packageName)) + + expect(variants.map((variant) => String(variant.id))).toEqual(["none", "low", "medium", "high", "xhigh", "max"]) + expect(variants[0]?.settings).toEqual( + settings(packageName, { + reasoningEffort: "none", + reasoningSummary: "auto", + include: ["reasoning.encrypted_content"], + }), + ) + }) + + test.each([ + Provider.aisdk("@ai-sdk/openai-compatible"), + "@opencode-ai/ai/providers/openai/chat", + "@opencode-ai/ai/providers/openai-compatible", + "@opencode-ai/ai/providers/azure/chat", + "@opencode-ai/ai/providers/google-vertex/chat", + ])("adds conservative chat variants for %s", (packageName) => { + expect(plain(VariantPlugin.fallback(model("reasoner", packageName)))).toEqual([ + { id: "low", settings: settings(packageName, { reasoningEffort: "low" }) }, + { id: "medium", settings: settings(packageName, { reasoningEffort: "medium" }) }, + { id: "high", settings: settings(packageName, { reasoningEffort: "high" }) }, + ]) + }) + + test("uses chat fallbacks for AI SDK Azure completion URLs", () => { + const variants = VariantPlugin.fallback( + model("deployment", Provider.aisdk("@ai-sdk/azure"), 32_000, { useCompletionUrls: true }), + ) + + expect(plain(variants)).toEqual([ + { id: "low", settings: { reasoningEffort: "low" } }, + { id: "medium", settings: { reasoningEffort: "medium" } }, + { id: "high", settings: { reasoningEffort: "high" } }, + ]) + }) + + test.each([ + Provider.aisdk("@ai-sdk/google"), + Provider.aisdk("@ai-sdk/google-vertex"), + "@opencode-ai/ai/providers/google", + "@opencode-ai/ai/providers/google-vertex", + "@opencode-ai/ai/providers/google-vertex/gemini", + ])("adds Google level and legacy budget variants for %s", (packageName) => { + expect(plain(VariantPlugin.fallback(model("gemini-next", packageName)))).toEqual([ + { + id: "low", + settings: settings(packageName, { thinkingConfig: { includeThoughts: true, thinkingLevel: "low" } }), + }, + { + id: "medium", + settings: settings(packageName, { thinkingConfig: { includeThoughts: true, thinkingLevel: "medium" } }), + }, + { + id: "high", + settings: settings(packageName, { thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } }), + }, + ]) + expect(plain(VariantPlugin.fallback(model("gemini-2.5-pro", packageName, 64_000)))).toEqual([ + { + id: "high", + settings: settings(packageName, { thinkingConfig: { includeThoughts: true, thinkingBudget: 16_000 } }), + }, + { + id: "max", + settings: settings(packageName, { thinkingConfig: { includeThoughts: true, thinkingBudget: 32_768 } }), + }, + ]) + expect(VariantPlugin.fallback(model("gemini-12.5-pro", packageName)).map((variant) => String(variant.id))).toEqual([ + "low", + "medium", + "high", + ]) + }) + + test.each([ + Provider.aisdk("@ai-sdk/anthropic"), + Provider.aisdk("@ai-sdk/google-vertex/anthropic"), + "@opencode-ai/ai/providers/anthropic", + "@opencode-ai/ai/providers/anthropic-compatible", + "@opencode-ai/ai/providers/google-vertex/messages", + ])("adds Anthropic adaptive and legacy budget variants for %s", (packageName) => { + expect(VariantPlugin.fallback(model("claude-opus-4-7", packageName)).map((variant) => String(variant.id))).toEqual([ + "low", + "medium", + "high", + "xhigh", + "max", + ]) + expect(VariantPlugin.fallback(model("claude-opus-4-7", packageName))[0]?.settings).toEqual( + settings(packageName, { + thinking: { type: "adaptive", display: "summarized" }, + effort: "low", + }), + ) + expect(plain(VariantPlugin.fallback(model("claude-haiku-4-5", packageName, 20_000)))).toEqual([ + { id: "high", settings: settings(packageName, { thinking: { type: "enabled", budgetTokens: 16_000 } }) }, + { id: "max", settings: settings(packageName, { thinking: { type: "enabled", budgetTokens: 19_999 } }) }, + ]) + for (const family of ["haiku", "sonnet", "opus"]) { + expect(VariantPlugin.fallback(model(`claude-${family}-4-5`, packageName))[0]?.settings).toEqual( + settings(packageName, { thinking: { type: "enabled", budgetTokens: 16_000 } }), + ) + expect(VariantPlugin.fallback(model(`claude-${family}-4-6`, packageName))[0]?.settings).toEqual( + settings(packageName, { + thinking: { type: "adaptive", display: "summarized" }, + effort: "low", + }), + ) + } + expect(VariantPlugin.fallback(model("claude-mythos-4-5", packageName))[0]?.settings).toEqual( + settings(packageName, { + thinking: { type: "adaptive", display: "summarized" }, + effort: "low", + }), + ) + }) + + test("does not add fallbacks for unknown packages", () => { + expect(VariantPlugin.fallback(model("reasoner", "custom"))).toEqual([]) + }) +})