Skip to content

Commit a8062ea

Browse files
authored
fix(provider): derive variants from reasoning metadata (anomalyco#36624)
1 parent f476847 commit a8062ea

8 files changed

Lines changed: 532 additions & 173 deletions

File tree

‎packages/core/src/models-dev.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,31 @@ const Cost = Schema.Struct({
4444
),
4545
})
4646

47+
const ReasoningOption = Schema.Union([
48+
Schema.Struct({
49+
type: Schema.Literal("effort"),
50+
values: Schema.Array(Schema.NullOr(Schema.String)),
51+
}),
52+
Schema.Struct({
53+
type: Schema.Literal("toggle"),
54+
}),
55+
Schema.Struct({
56+
type: Schema.Literal("budget_tokens"),
57+
min: Schema.optional(Schema.Finite),
58+
max: Schema.optional(Schema.Finite),
59+
}),
60+
])
61+
4762
export const Model = Schema.Struct({
4863
id: Schema.String,
4964
name: Schema.String,
5065
family: Schema.optional(Schema.String),
5166
release_date: Schema.String,
5267
attachment: Schema.Boolean,
5368
reasoning: Schema.Boolean,
54-
// models.dev is external metadata and reasoning controls are expected to evolve.
55-
// Provider normalization extracts the subset understood by this client.
56-
reasoning_options: Schema.optional(Schema.Unknown),
5769
temperature: Schema.Boolean,
5870
tool_call: Schema.Boolean,
71+
reasoning_options: Schema.optional(Schema.Array(ReasoningOption)),
5972
interleaved: Schema.optional(
6073
Schema.Union([
6174
Schema.Literal(true),

‎packages/core/test/models.test.ts‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { describe, expect, beforeAll, beforeEach, afterAll, test } from "bun:test"
2-
import { Effect, Layer, Ref, Schema } from "effect"
1+
import { describe, expect, beforeAll, beforeEach, afterAll } from "bun:test"
2+
import { Effect, Layer, Ref } from "effect"
33
import { HttpClient, HttpClientResponse } from "effect/unstable/http"
44
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
55
import { LayerNodePlatform } from "@opencode-ai/core/effect/app-node-platform"
@@ -126,21 +126,6 @@ const initialState: MockState = {
126126
calls: [],
127127
}
128128

129-
test("models.dev model schema keeps reasoning options permissive", () => {
130-
const model = Schema.decodeUnknownSync(ModelsDev.Model)({
131-
id: "acme-1",
132-
name: "Acme One",
133-
release_date: "2026-01-01",
134-
attachment: false,
135-
reasoning: true,
136-
reasoning_options: [{ type: "future_control", value: { nested: true } }, "future-shape"],
137-
temperature: true,
138-
tool_call: true,
139-
limit: { context: 128000, output: 8192 },
140-
})
141-
expect(model.reasoning_options).toEqual([{ type: "future_control", value: { nested: true } }, "future-shape"])
142-
})
143-
144129
describe("ModelsDev Service", () => {
145130
it.live("get() returns providers from disk when cache file exists", () =>
146131
Effect.gen(function* () {

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -981,21 +981,6 @@ const ProviderInterleaved = Schema.Union([
981981
}),
982982
])
983983

984-
const ProviderReasoningOption = Schema.Union([
985-
Schema.Struct({
986-
type: Schema.Literal("effort"),
987-
values: Schema.Array(Schema.NullOr(Schema.String)),
988-
}),
989-
Schema.Struct({
990-
type: Schema.Literal("toggle"),
991-
}),
992-
Schema.Struct({
993-
type: Schema.Literal("budget_tokens"),
994-
min: optional(Schema.Finite),
995-
max: optional(Schema.Finite),
996-
}),
997-
])
998-
999984
const ProviderCapabilities = Schema.Struct({
1000985
temperature: Schema.Boolean,
1001986
reasoning: Schema.Boolean,
@@ -1054,7 +1039,6 @@ export const Model = Schema.Struct({
10541039
options: Schema.Record(Schema.String, Schema.Any),
10551040
headers: Schema.Record(Schema.String, Schema.String),
10561041
release_date: Schema.String,
1057-
reasoning_options: optional(Schema.Array(ProviderReasoningOption)),
10581042
variants: optional(Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Any))),
10591043
}).annotate({ identifier: "Model" })
10601044
export type Model = Types.DeepMutable<Schema.Schema.Type<typeof Model>>
@@ -1218,36 +1202,6 @@ function cost(c: ModelsDev.Model["cost"]): Model["cost"] {
12181202
return result
12191203
}
12201204

1221-
type ReasoningOption = NonNullable<Model["reasoning_options"]>[number]
1222-
1223-
function reasoningOptions(input: unknown): Model["reasoning_options"] {
1224-
if (!Array.isArray(input)) return []
1225-
return input.flatMap((option) => {
1226-
const normalized = normalizeReasoningOption(option)
1227-
return normalized ? [normalized] : []
1228-
})
1229-
}
1230-
1231-
function normalizeReasoningOption(option: unknown): ReasoningOption | undefined {
1232-
if (!isRecord(option)) return
1233-
if (option.type === "effort") {
1234-
if (!Array.isArray(option.values)) return
1235-
return {
1236-
type: "effort",
1237-
values: option.values.filter((value): value is string | null => value === null || typeof value === "string"),
1238-
}
1239-
}
1240-
if (option.type === "toggle") return { type: "toggle" }
1241-
if (option.type !== "budget_tokens") return
1242-
const min = typeof option.min === "number" && Number.isFinite(option.min) ? option.min : undefined
1243-
const max = typeof option.max === "number" && Number.isFinite(option.max) ? option.max : undefined
1244-
return {
1245-
type: "budget_tokens",
1246-
...(min === undefined ? {} : { min }),
1247-
...(max === undefined ? {} : { max }),
1248-
}
1249-
}
1250-
12511205
function fromModelsDevModel(provider: ModelsDev.Provider, model: ModelsDev.Model): Model {
12521206
const base: Model = {
12531207
id: ModelV2.ID.make(model.id),
@@ -1290,13 +1244,14 @@ function fromModelsDevModel(provider: ModelsDev.Provider, model: ModelsDev.Model
12901244
interleaved: model.interleaved ?? false,
12911245
},
12921246
release_date: model.release_date ?? "",
1293-
reasoning_options: reasoningOptions(model.reasoning_options),
12941247
variants: {},
12951248
}
12961249

1250+
const variants = ProviderTransform.reasoningVariants(model, base) ?? ProviderTransform.variants(base)
1251+
12971252
return {
12981253
...base,
1299-
variants: mapValues(ProviderTransform.variants(base), (v) => v),
1254+
variants: mapValues(variants, (v) => v),
13001255
}
13011256
}
13021257

@@ -1537,10 +1492,13 @@ const layer = Layer.effect(
15371492
headers: mergeDeep(existingModel?.headers ?? {}, model.headers ?? {}),
15381493
family: model.family ?? existingModel?.family ?? "",
15391494
release_date: model.release_date ?? existingModel?.release_date ?? "",
1540-
reasoning_options: existingModel?.reasoning_options,
15411495
variants: {},
15421496
}
1543-
const merged = mergeDeep(ProviderTransform.variants(parsedModel), model.variants ?? {})
1497+
const variants =
1498+
existingModel?.api.npm === parsedModel.api.npm
1499+
? (existingModel.variants ?? ProviderTransform.variants(parsedModel))
1500+
: ProviderTransform.variants(parsedModel)
1501+
const merged = mergeDeep(variants, model.variants ?? {})
15441502
parsedModel.variants = mapValues(
15451503
pickBy(merged, (v) => !v.disabled),
15461504
(v) => omit(v, ["disabled"]),
@@ -1668,7 +1626,7 @@ const layer = Layer.effect(
16681626
)
16691627
delete provider.models[modelID]
16701628

1671-
if (!model.variants || Object.keys(model.variants).length === 0) {
1629+
if (model.variants === undefined) {
16721630
model.variants = mapValues(ProviderTransform.variants(model), (v) => v)
16731631
}
16741632

‎packages/opencode/src/provider/transform.ts‎

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,4 +1578,189 @@ export function schema(model: Provider.Model, schema: JSONSchema7): JSONSchema7
15781578
return schema
15791579
}
15801580

1581+
export function reasoningVariants(model: ModelsDev.Model, target: Provider.Model): Provider.Model["variants"] {
1582+
const options = model.reasoning_options
1583+
if (options === undefined) return
1584+
if (options.length === 0) return {}
1585+
1586+
const effort = options.find((option) => option.type === "effort")
1587+
if (effort) return nonEmptyVariants(effortVariants(target, effort.values))
1588+
1589+
const toggle = options.some((option) => option.type === "toggle")
1590+
const budget = options.find((option) => option.type === "budget_tokens")
1591+
if (!budget) return toggle ? nonEmptyVariants(reasoningToggle(target)) : undefined
1592+
1593+
return nonEmptyVariants({
1594+
...(toggle ? reasoningToggle(target) : {}),
1595+
...budgetVariants(target, budget.min, budget.max),
1596+
})
1597+
}
1598+
1599+
function effortVariants(model: Provider.Model, values: readonly unknown[]) {
1600+
return Object.fromEntries(
1601+
values.flatMap((value) => {
1602+
const id = (() => {
1603+
if (value === null) return "none"
1604+
if (typeof value === "string") return value
1605+
})()
1606+
if (id === undefined) return []
1607+
const settings = reasoningEffort(model, id)
1608+
return settings ? [[id, settings]] : []
1609+
}),
1610+
)
1611+
}
1612+
1613+
function budgetVariants(model: Provider.Model, min?: number, max?: number) {
1614+
const limit = model.limit.output - 1
1615+
if (limit <= 0) return {}
1616+
const high = Math.min(
1617+
max === undefined ? Math.max(min ?? 0, 16_000) : Math.min(Math.max(min ?? 0, 16_000), max),
1618+
limit,
1619+
)
1620+
const maximum = max === undefined ? undefined : Math.min(max, limit)
1621+
return Object.fromEntries(
1622+
[
1623+
{ id: "high", budget: high },
1624+
...(maximum === undefined || maximum === high ? [] : [{ id: "max", budget: maximum }]),
1625+
].flatMap((item) => {
1626+
const settings = reasoningBudget(model, item.budget)
1627+
return settings ? [[item.id, settings]] : []
1628+
}),
1629+
)
1630+
}
1631+
1632+
function nonEmptyVariants(variants: NonNullable<Provider.Model["variants"]>): Provider.Model["variants"] {
1633+
return Object.keys(variants).length > 0 ? variants : undefined
1634+
}
1635+
1636+
function reasoningToggle(model: Provider.Model): NonNullable<Provider.Model["variants"]> {
1637+
if (model.api.npm === "@ai-sdk/alibaba")
1638+
return {
1639+
none: { enableThinking: false },
1640+
high: { enableThinking: true },
1641+
}
1642+
if (model.api.npm === "@ai-sdk/cohere")
1643+
return {
1644+
none: { thinking: { type: "disabled" } },
1645+
high: { thinking: { type: "enabled" } },
1646+
}
1647+
return {}
1648+
}
1649+
1650+
function reasoningEffort(model: Provider.Model, effort: string) {
1651+
switch (model.api.npm) {
1652+
case "@openrouter/ai-sdk-provider":
1653+
return { reasoning: { effort } }
1654+
case "@ai-sdk/anthropic":
1655+
case "@ai-sdk/google-vertex/anthropic":
1656+
return anthropicEffort(model, effort)
1657+
case "@ai-sdk/google":
1658+
case "@ai-sdk/google-vertex":
1659+
return { thinkingConfig: { includeThoughts: true, thinkingLevel: effort } }
1660+
case "@ai-sdk/amazon-bedrock":
1661+
if (anthropicAdaptiveEfforts(model.api.id))
1662+
return {
1663+
reasoningConfig: {
1664+
type: "adaptive",
1665+
maxReasoningEffort: effort,
1666+
...(anthropicOmitsThinking(model.api.id) ? { display: "summarized" } : {}),
1667+
},
1668+
}
1669+
if (model.api.id.includes("anthropic")) return
1670+
return { reasoningConfig: { type: "enabled", maxReasoningEffort: effort } }
1671+
case "@ai-sdk/gateway":
1672+
if (model.id.includes("anthropic")) return { thinking: { type: "adaptive", display: "summarized" }, effort }
1673+
if (model.id.includes("google")) return { thinkingConfig: { includeThoughts: true, thinkingLevel: effort } }
1674+
return { reasoningEffort: effort }
1675+
case "@ai-sdk/github-copilot":
1676+
// OAuth discovery replaces these with variants from Copilot's /models capabilities.
1677+
if (model.id.includes("gemini")) return
1678+
if (model.id.includes("claude")) return { reasoningEffort: effort }
1679+
return { reasoningEffort: effort, reasoningSummary: "auto", include: INCLUDE_ENCRYPTED_REASONING }
1680+
case "@ai-sdk/openai":
1681+
case "@ai-sdk/amazon-bedrock/mantle":
1682+
return { reasoningEffort: effort, reasoningSummary: "auto", include: INCLUDE_ENCRYPTED_REASONING }
1683+
case "@ai-sdk/azure":
1684+
return { reasoningEffort: effort, reasoningSummary: "auto", include: INCLUDE_ENCRYPTED_REASONING }
1685+
case "@jerome-benoit/sap-ai-provider-v2":
1686+
if (model.id.includes("anthropic"))
1687+
return { modelParams: { thinking: { type: "adaptive", display: "summarized" }, output_config: { effort } } }
1688+
return { modelParams: { reasoning_effort: effort } }
1689+
case "@ai-sdk/openai-compatible":
1690+
case "@ai-sdk/xai":
1691+
case "@ai-sdk/mistral":
1692+
case "@ai-sdk/groq":
1693+
case "@ai-sdk/cerebras":
1694+
case "@ai-sdk/deepinfra":
1695+
case "@ai-sdk/togetherai":
1696+
case "venice-ai-sdk-provider":
1697+
case "ai-gateway-provider":
1698+
return { reasoningEffort: effort }
1699+
case "@ai-sdk/cohere":
1700+
case "@ai-sdk/perplexity":
1701+
case "@ai-sdk/vercel":
1702+
case "@ai-sdk/alibaba":
1703+
case "gitlab-ai-provider":
1704+
return
1705+
}
1706+
}
1707+
1708+
function anthropicEffort(model: Provider.Model, effort: string) {
1709+
if (["opus-4-5", "opus-4.5"].some((value) => model.api.id.includes(value))) return { effort }
1710+
if (!anthropicAdaptiveEfforts(model.api.id)) return
1711+
return {
1712+
thinking: {
1713+
type: "adaptive",
1714+
...(anthropicOmitsThinking(model.api.id) ? { display: "summarized" } : {}),
1715+
},
1716+
effort,
1717+
}
1718+
}
1719+
1720+
function reasoningBudget(model: Provider.Model, budget: number) {
1721+
switch (model.api.npm) {
1722+
case "@openrouter/ai-sdk-provider":
1723+
return { reasoning: { max_tokens: budget } }
1724+
case "@ai-sdk/anthropic":
1725+
case "@ai-sdk/google-vertex/anthropic":
1726+
return { thinking: { type: "enabled", budgetTokens: budget } }
1727+
case "@ai-sdk/google":
1728+
case "@ai-sdk/google-vertex":
1729+
return { thinkingConfig: { includeThoughts: true, thinkingBudget: budget } }
1730+
case "@ai-sdk/amazon-bedrock":
1731+
return { reasoningConfig: { type: "enabled", budgetTokens: budget } }
1732+
case "@ai-sdk/gateway":
1733+
if (model.id.includes("anthropic")) return { thinking: { type: "enabled", budgetTokens: budget } }
1734+
if (model.id.includes("google")) return { thinkingConfig: { includeThoughts: true, thinkingBudget: budget } }
1735+
return
1736+
case "@ai-sdk/cohere":
1737+
return { thinking: { type: "enabled", tokenBudget: budget } }
1738+
case "@ai-sdk/alibaba":
1739+
return { enableThinking: true, thinkingBudget: budget }
1740+
case "@jerome-benoit/sap-ai-provider-v2":
1741+
if (model.id.includes("anthropic"))
1742+
return { modelParams: { thinking: { type: "enabled", budget_tokens: budget } } }
1743+
if (model.id.includes("gemini"))
1744+
return { modelParams: { thinkingConfig: { includeThoughts: true, thinkingBudget: budget } } }
1745+
return
1746+
case "@ai-sdk/amazon-bedrock/mantle":
1747+
case "@ai-sdk/azure":
1748+
case "@ai-sdk/cerebras":
1749+
case "@ai-sdk/deepinfra":
1750+
case "@ai-sdk/github-copilot":
1751+
case "@ai-sdk/groq":
1752+
case "@ai-sdk/mistral":
1753+
case "@ai-sdk/openai":
1754+
case "@ai-sdk/openai-compatible":
1755+
case "@ai-sdk/perplexity":
1756+
case "@ai-sdk/togetherai":
1757+
case "@ai-sdk/vercel":
1758+
case "@ai-sdk/xai":
1759+
case "ai-gateway-provider":
1760+
case "gitlab-ai-provider":
1761+
case "venice-ai-sdk-provider":
1762+
return
1763+
}
1764+
}
1765+
15811766
export * as ProviderTransform from "./transform"

0 commit comments

Comments
 (0)