From a5a46c53e388f8d1069af9ddbdc8fe5b68341bd5 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Tue, 10 Nov 2026 09:23:40 -0600 Subject: [PATCH] fix(core): keep each model's reasoning levels when respelling --- packages/core/src/config/plugin/provider.ts | 17 +++++++- packages/core/test/config/provider.test.ts | 47 ++++++++++++++++++--- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/packages/core/src/config/plugin/provider.ts b/packages/core/src/config/plugin/provider.ts index c4fab3cabcb..e300a0d0cec 100644 --- a/packages/core/src/config/plugin/provider.ts +++ b/packages/core/src/config/plugin/provider.ts @@ -158,7 +158,8 @@ export const Plugin = define({ ((config.variants === undefined && !source?.base && !hadVariants) || repackaged) ) models.update(providerID, id, (model) => { - model.variants = [...Variant.resolve({ ...model, package: model.package ?? providerPackage })] + const target = { ...model, package: model.package ?? providerPackage } + model.variants = source?.base ? respell(target, source.base.variants) : [...Variant.resolve(target)] // Configured variants overlay the respelled defaults. for (const variants of configured.get(key) ?? []) overlayVariants(model, variants) }) @@ -169,7 +170,7 @@ export const Plugin = define({ // Leave models whose variants or package an earlier model transform replaced. if (!model || model.package !== undefined || !isDeepStrictEqual(model.variants, variants)) continue models.update(providerID, id, (model) => { - model.variants = [...Variant.resolve({ ...model, package: providerPackage })] + model.variants = respell({ ...model, package: providerPackage }, variants) }) } } @@ -299,6 +300,18 @@ function overlay( }) } +/** + * Spells the reasoning levels a model already offers for the package it now runs with. Models don't keep the + * catalog's reasoning support, so the current variant ids stand in for it; a level the package can't spell is dropped. + */ +function respell(model: Model.Info, previous: Model.Info["variants"]) { + const spelled = [ + ...(previous.some((variant) => variant.id === "thinking") ? Variant.resolve(model, [{ type: "toggle" }]) : []), + ...Variant.resolve(model), + ] + return previous.flatMap((variant) => spelled.find((item) => item.id === variant.id) ?? []) +} + type ConfigVariants = NonNullable[string]["variants"]> function overlayVariants(model: Model.MutableInfo, variants: ConfigVariants) { diff --git a/packages/core/test/config/provider.test.ts b/packages/core/test/config/provider.test.ts index 7043c167191..5c257aef53d 100644 --- a/packages/core/test/config/provider.test.ts +++ b/packages/core/test/config/provider.test.ts @@ -1012,7 +1012,7 @@ describe("ConfigProviderPlugin.Plugin", () => { ]) const model = required(yield* models.get(Provider.ID.make("google-ix"), Model.ID.make("gemini-4-argon"))) - expect(model.variants).toEqual(interactions(["low", "medium", "high"])) + expect(model.variants).toEqual(interactions(["low", "high"])) }), ) @@ -1036,8 +1036,8 @@ describe("ConfigProviderPlugin.Plugin", () => { const variants = (id: string) => models.get(Provider.ID.make("google"), Model.ID.make(id)).pipe(Effect.map((model) => required(model).variants)) - expect(yield* variants("gemini-4-argon")).toEqual(interactions(["low", "medium", "high"])) - expect(yield* variants("gemini-4-flash")).toEqual(interactions(["low", "medium", "high"])) + expect(yield* variants("gemini-4-argon")).toEqual(interactions(["low", "high"])) + expect(yield* variants("gemini-4-flash")).toEqual(interactions(["low", "high"])) // Its own package still applies, so its variants stay as they were. expect((yield* variants("gemini-4-routed")).map((variant) => variant.settings)).toEqual([ { thinkingConfig: { includeThoughts: true, thinkingLevel: "low" } }, @@ -1046,6 +1046,44 @@ describe("ConfigProviderPlugin.Plugin", () => { }), ) + it.effect("keeps each model's reasoning levels when a package switch respells them", () => + Effect.gen(function* () { + const providers = yield* Provider.Service + const models = yield* Model.Service + const providerID = Provider.ID.make("zai") + yield* providers.transform((editor) => { + editor.update(providerID, (provider) => { + provider.package = "@opencode/ai/providers/openai-compatible" + }) + const levels = { "glm-5": ["none", "thinking"], "glm-4.7": ["low", "high", "ultra"] } + for (const [id, ids] of Object.entries(levels)) + editor.models.update(providerID, Model.ID.make(id), (model) => { + model.variants = ids.map((variant) => ({ id: Model.VariantID.make(variant), body: { level: variant } })) + }) + }) + yield* addPlugin([ + new Document({ + type: "document", + info: decode({ providers: { zai: { package: "@opencode/ai/providers/anthropic" } } }), + }), + ]) + + const variants = (id: string) => + models.get(providerID, Model.ID.make(id)).pipe(Effect.map((model) => required(model).variants)) + expect(yield* variants("glm-5")).toEqual([ + { id: Model.VariantID.make("none"), settings: { thinking: { type: "disabled" } } }, + { id: Model.VariantID.make("thinking"), settings: { thinking: { type: "adaptive", display: "summarized" } } }, + ]) + // A level the new package can't spell is dropped rather than kept in the old spelling. + expect(yield* variants("glm-4.7")).toEqual( + ["low", "high"].map((effort) => ({ + id: Model.VariantID.make(effort), + settings: { thinking: { type: "adaptive", display: "summarized" }, effort }, + })), + ) + }), + ) + it.effect("overlays configured variants on the variants respelled for a package switch", () => Effect.gen(function* () { const models = yield* Model.Service @@ -1073,10 +1111,9 @@ describe("ConfigProviderPlugin.Plugin", () => { ]) const model = required(yield* models.get(Provider.ID.make("google-ix"), Model.ID.make("gemini-4-argon"))) - const [low, medium, high] = interactions(["low", "medium", "high"]) + const [low, high] = interactions(["low", "high"]) expect(model.variants).toEqual([ { ...low, settings: { thinkingLevel: "low", thinkingSummaries: "none" } }, - medium, { ...high, headers: { "x-tier": "high" } }, ]) }),