fix(core): keep a mode's ID on the service tier it asks for (#65989)

This commit is contained in:
victor 2026-11-24 10:20:02 +01:00 • committed by GitHub
parent 7dd39a70b6
commit e4cd9c2bc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 74 additions and 6 deletions

View file

@ -290,16 +290,23 @@ const normalize = Effect.fnUntraced(function* (
return [
{ ...base, variants },
...tiers.flatMap((tier) => {
// "priority" is the tier people know as fast.
const name = tier === "priority" && !tiers.includes("fast") ? "fast" : tier
const mode = modes.find(([key, options]) => (options.service_tier ?? key) === tier)
// A mode for the tier keeps its ID, so a saved session asks for the same tier, unless another model already
// has that ID. Otherwise "priority" is the tier people know as fast.
const name =
mode && !ids.has(Model.ID.make(`${source.model.id}-${mode[0]}`))
? mode[0]
: tier === "priority" && !tiers.includes("fast")
? "fast"
: tier
const speedID = Model.ID.make(`${source.model.id}-${name}`)
if (ids.has(speedID)) return []
// Until every tier has a multiplier, a mode for the tier may still carry its price.
const mode = modes.find(([key, options]) => (options.service_tier ?? key) === tier)?.[1]
// A mode's ID stays with the tier it asks for, even when another tier has that name.
if (ids.has(speedID) || (!mode && modes.some(([key]) => key === name))) return []
return [
modelInfo(providerID, { packageName, baseURL }, speedID, source, {
...speed(name, tier),
cost: modeCost(source.model, baseCost, tier, { cost: mode?.cost }),
// Until every tier has a multiplier, a mode for the tier may still carry its price.
cost: modeCost(source.model, baseCost, tier, { cost: mode?.[1].cost }),
request: tierRequest(source.route.protocol, tier),
}),
]

View file

@ -735,6 +735,67 @@ describe("ModelsDev Service", () => {
}),
)
it.effect("keeps a mode's ID on the tier the mode asks for", () =>
Effect.gen(function* () {
const [acme] = yield* ModelsDev.decodeSnapshot(
JSON.stringify({
acme: {
...fixture.acme,
models: {
"acme-1": sourceModel("acme-1", {
capabilities: {
tools: { supported: true },
reasoning: { supported: false },
service_tier: ["priority", "fast"],
},
// Saved sessions hold "acme-1-fast", which has always asked for the priority tier. The "fast" tier
// would take that ID, so it isn't offered as a second choice next to it.
experimental: { modes: { fast: { service_tier: "priority", body: { service_tier: "priority" } } } },
}),
},
},
}),
false,
true,
)
expect(acme?.models.map((item) => [item.id, item.settings?.serviceTier])).toEqual([
["acme-1", undefined],
["acme-1-fast", "priority"],
])
}),
)
it.effect("names a mode's tier after the tier when another model already has the mode's ID", () =>
Effect.gen(function* () {
const [acme] = yield* ModelsDev.decodeSnapshot(
JSON.stringify({
acme: {
...fixture.acme,
models: {
"acme-1": sourceModel("acme-1", {
capabilities: {
tools: { supported: true },
reasoning: { supported: false },
service_tier: ["priority", "fast"],
},
experimental: { modes: { fast: { service_tier: "priority", body: { service_tier: "priority" } } } },
}),
// A separate model already holds "acme-1-fast", so the priority tier keeps its own name.
"acme-1-fast": sourceModel("acme-1-fast"),
},
},
}),
false,
true,
)
expect(acme?.models.map((item) => [item.id, item.settings?.serviceTier])).toEqual([
["acme-1", undefined],
["acme-1-priority", "priority"],
["acme-1-fast", undefined],
])
}),
)
it.effect("skips non-chat models whose only opencode package serves another type", () =>
Effect.gen(function* () {
const api = (opencodeai: string) => ({