diff --git a/docs/architecture/tla/OpencodeGoModelMerge.LiveOverride.cfg b/docs/architecture/tla/OpencodeGoModelMerge.LiveOverride.cfg new file mode 100644 index 00000000000..5b6d6c744de --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.LiveOverride.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "LiveOverride" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.NoLiveMaxTokens.cfg b/docs/architecture/tla/OpencodeGoModelMerge.NoLiveMaxTokens.cfg new file mode 100644 index 00000000000..3b43a5ac647 --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.NoLiveMaxTokens.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "NoLiveMaxTokens" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.NoNestedLimit.cfg b/docs/architecture/tla/OpencodeGoModelMerge.NoNestedLimit.cfg new file mode 100644 index 00000000000..6be294bc9ad --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.NoNestedLimit.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "NoNestedLimit" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect OnlyChatListed +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.NoTypeFilter.cfg b/docs/architecture/tla/OpencodeGoModelMerge.NoTypeFilter.cfg new file mode 100644 index 00000000000..f0edf6cb302 --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.NoTypeFilter.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "NoTypeFilter" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect OnlyChatListed +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.UnknownIgnoresLive.cfg b/docs/architecture/tla/OpencodeGoModelMerge.UnknownIgnoresLive.cfg new file mode 100644 index 00000000000..d8742da58fa --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.UnknownIgnoresLive.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "UnknownIgnoresLive" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.cfg b/docs/architecture/tla/OpencodeGoModelMerge.cfg new file mode 100644 index 00000000000..4c6577fef65 --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.cfg @@ -0,0 +1,5 @@ +SPECIFICATION Spec +CONSTANTS + Variant = "Fixed" +INVARIANTS TypeOk CuratedContextWindowFromRegistry UnknownContextWindowCorrect CuratedLiveMaxTokensMerged UnknownMaxTokensCorrect OnlyChatListed +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpencodeGoModelMerge.tla b/docs/architecture/tla/OpencodeGoModelMerge.tla new file mode 100644 index 00000000000..1100747fffe --- /dev/null +++ b/docs/architecture/tla/OpencodeGoModelMerge.tla @@ -0,0 +1,140 @@ +---- MODULE OpencodeGoModelMerge ---- +EXTENDS Integers +\* Source: parseOpencodeGoModel (src/api/providers/fetchers/opencode-go.ts). +\* Sink: ModelInfo.contextWindow published by getOpencodeGoModels for the model picker. +\* Failure: a curated model gets its registry contextWindow replaced by the live +\* context_window value. The Go endpoint reports a blanket 200000 (issue 723). +\* Invariant sentences came before the model and before the fix: +\* S1 a registry model must keep the registry contextWindow. +\* S2 an unknown model must keep the live value, and the default when absent. +\* S3 a registry model must still take a live max-token value. +\* S4 an unknown model must keep the live max-token rule and the default. +\* Invariants: CuratedContextWindowFromRegistry, UnknownContextWindowCorrect, +\* CuratedLiveMaxTokensMerged, UnknownMaxTokensCorrect. +\* Expect: Fixed holds. LiveOverride models the old code and must fail S1. +\* Expect: UnknownIgnoresLive fails S2. NoLiveMaxTokens fails S3. +\* Result: Fixed holds in 24 states. LiveOverride fails S1 on a curated model +\* with a live 0: [curated |-> TRUE, liveContextWindow |-> 0, liveMaxTokens |-> -1]. +\* Result: UnknownIgnoresLive fails S2 on a live 0. NoLiveMaxTokens fails S3 on +\* liveMaxTokens 0. Each sabotage run fails on a live zero. Not vacuous. +\* Replay: the unfixed parseOpencodeGoModel fails 6 tests, with 200000 in +\* place of a curated 1M window. The fix passes all. No false alarm. +\* Review (PR 2646, thread 4656589514): one finding, added 1 (input rule: +\* 0 is a live value; the sentinel -1 marks absence), covered 0, gap 0. +\* Maintainer round (edelauna, review 6033833085): the endpoint publishes +\* limit.context/limit.output and modalities.input with a `type` +\* discriminator; unknown models must take the live values, and non-chat +\* types must be skipped. Added the liveShape (absent/flat/nested) and +\* modelType (chat/embedding/untyped) inputs, the Listed guard, and the +\* OnlyChatListed invariant. NoNestedLimit fails the live-merge invariants +\* on nested-only entries; NoTypeFilter fails OnlyChatListed. 0 covered, +\* 1 added, 0 gap. Fixed gate grows 24 to 150 states. +\* CodeRabbit round (review 6040141115, thread 4680745542, mutation +\* report 7133201488): finding A — the nested-metadata tests called the +\* parser directly and never exercised the endpoint schema or fetcher. +\* The nested merge contract was already covered (liveShape "nested", +\* the NoNestedLimit sabotage); added 2 fetcher-path tests at the real +\* axios seam: curated + unknown entries carrying nested limit and +\* modalities plus conflicting flat fields, and a modalities entry +\* without input that must fall back to supports_images. The 3 +\* surviving mutants are killed, each apply/revert-verified on the +\* focused spec: L32/L38 (a nested schema object emptied) fail the +\* precedence test, L86 (dropped ?.) fails the missing-input test. +\* Image support stays with the not-modeled list (L86 is killed at +\* test level). Scope check (pre-merge "Out of Scope Changes"): the +\* type filter stays — maintainer-requested (review 6033833085) — and +\* is now linked as issue 2679 in the PR Scope section. covered 1, +\* added 1, gap 0. Header-only .tla edit; the gate re-ran unchanged. +\* Cost: four TLC runs take about 4 seconds. Focused tests take about 5 seconds. +\* Agent work: about 15 minutes, including tests. +\* Not modeled: the parser, the field-name pair, capability flags, and pricing. +\* The model checks the merge contract, not the vendor facts. + +CONSTANT Variant \* "Fixed", "LiveOverride", "UnknownIgnoresLive", "NoLiveMaxTokens" + +VARIABLE in + +\* The live payload omits a field or reports a value. Nil is the -1 sentinel. +\* 0 is a real live value: ?? falls back only on null and undefined. +\* 200000 is the blanket value the endpoint reports for every model. +\* 2000000 is a live value larger than the registry one. +Nil == -1 + +\* The absent shape carries no values at all; a present shape may still +\* omit either field individually (Nil marks that field's absence). +Inputs == {i \in [curated : BOOLEAN, + liveContextWindow : {Nil, 0, 200000, 2000000}, + liveMaxTokens : {Nil, 0, 65536}, + liveShape : {"absent", "flat", "nested"}, + modelType : {"chat", "embedding", "untyped"}] : + (i.liveShape = "absent" => i.liveContextWindow = Nil /\ i.liveMaxTokens = Nil)} + +\* Registry limits for one curated model, as deepseek-v4-flash. +CuratedContextWindow == 1000000 +CuratedMaxTokens == 384000 +\* Default limits for an unknown model, as opencodeGoDefaultModelInfo. +DefaultContextWindow == 200000 +DefaultMaxTokens == 32768 + +\* One value models the context_window and context_length pair. +\* The parser reads the nested limit objects; the flat fields stay a +\* fallback. A nested-only payload loses its live values when the nested +\* read is sabotaged (NoNestedLimit). +LiveContext(i) == + IF i.liveShape = "absent" THEN Nil + ELSE IF i.liveShape = "nested" /\ Variant = "NoNestedLimit" THEN Nil + ELSE i.liveContextWindow + +LiveMaxTokens(i) == + IF i.liveShape = "absent" THEN Nil + ELSE IF i.liveShape = "nested" /\ Variant = "NoNestedLimit" THEN Nil + ELSE i.liveMaxTokens + +\* Fixed code: an entry whose type is present and not "chat" is skipped +\* from the catalog. Untyped entries stay listed for older responses. +\* Sabotage NoTypeFilter lists every parsed entry. +Listed(i) == ~(Variant # "NoTypeFilter" /\ i.modelType = "embedding") + +PublishedContextWindow(i) == + IF i.curated + THEN IF Variant = "LiveOverride" /\ LiveContext(i) # Nil + THEN LiveContext(i) + ELSE CuratedContextWindow + ELSE IF LiveContext(i) = Nil \/ Variant = "UnknownIgnoresLive" + THEN DefaultContextWindow + ELSE LiveContext(i) + +PublishedMaxTokens(i) == + IF i.curated + THEN IF Variant = "NoLiveMaxTokens" \/ LiveMaxTokens(i) = Nil + THEN CuratedMaxTokens + ELSE LiveMaxTokens(i) + ELSE IF LiveMaxTokens(i) = Nil + THEN DefaultMaxTokens + ELSE LiveMaxTokens(i) + +TypeOk == in \in Inputs +\* A skipped entry never reaches the merge, so the merge contract applies +\* only to listed entries. The EXPECTED side uses the raw payload value, not +\* the read operator, so a dropped read (NoNestedLimit) cannot vacuously +\* pass by flipping both sides. +CuratedContextWindowFromRegistry == + Listed(in) /\ in.curated => PublishedContextWindow(in) = CuratedContextWindow +UnknownContextWindowCorrect == + Listed(in) /\ ~in.curated => PublishedContextWindow(in) = + IF in.liveShape = "absent" \/ in.liveContextWindow = Nil THEN DefaultContextWindow ELSE in.liveContextWindow +CuratedLiveMaxTokensMerged == + Listed(in) /\ in.curated /\ in.liveShape # "absent" /\ in.liveMaxTokens # Nil => + PublishedMaxTokens(in) = in.liveMaxTokens +UnknownMaxTokensCorrect == + Listed(in) /\ ~in.curated => PublishedMaxTokens(in) = + IF in.liveShape = "absent" \/ in.liveMaxTokens = Nil THEN DefaultMaxTokens ELSE in.liveMaxTokens + +\* Maintainer round: an entry whose declared type is not "chat" is never +\* published to the picker. +OnlyChatListed == in.modelType = "embedding" => ~Listed(in) + +Init == in \in Inputs +Next == UNCHANGED in +Spec == Init /\ [][Next]_in +==== diff --git a/packages/types/src/providers/opencode-go.ts b/packages/types/src/providers/opencode-go.ts index f7f23c1aa60..1de52808693 100644 --- a/packages/types/src/providers/opencode-go.ts +++ b/packages/types/src/providers/opencode-go.ts @@ -41,8 +41,11 @@ export const OPENCODE_GO_DEFAULT_TEMPERATURE = 0 * https://opencode.ai/docs/go/#usage-limits. It also includes explicitly * curated Responses-format models whose gateway metadata is not available * through `/models`. The fetcher merges the live `/models` payload on top of - * these defaults so that context-window and max-token values stay in sync - * with the gateway while capability flags and pricing remain correct. + * these defaults so that max-token and image-support values stay in sync + * with the gateway while capability flags, pricing, and the curated context + * windows remain correct. The registry context window is authoritative for + * curated models: the `/models` endpoint reports a blanket `context_window` + * for every model, so a live value must not override it. * * `supportsPromptCache` has two distinct meanings depending on the wire format: * diff --git a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts index 869304dcc12..3ac7367c74c 100644 --- a/src/api/providers/fetchers/__tests__/opencode-go.spec.ts +++ b/src/api/providers/fetchers/__tests__/opencode-go.spec.ts @@ -39,17 +39,18 @@ describe("Opencode Go Fetchers", () => { }) expect(Object.keys(models).sort()).toEqual(["deepseek-v4-pro", "glm-5.1"]) - // Live endpoint values override the native registry for volatile fields, - // while capability flags and pricing come from the native registry. + // The registry context window stays authoritative for curated models: + // the endpoint reports a blanket context_window, so live limits only + // apply to max tokens while capability flags come from the registry. expect(models["glm-5.1"]).toMatchObject({ - contextWindow: 202752, + contextWindow: 204_800, maxTokens: 32768, supportsPromptCache: true, supportsReasoningEffort: ["disable", "medium"], preserveReasoning: true, description: "Zhipu GLM 5.1", }) - expect(models["deepseek-v4-pro"].contextWindow).toBe(1048576) + expect(models["deepseek-v4-pro"].contextWindow).toBe(1_000_000) expect(models["deepseek-v4-pro"].supportsReasoningEffort).toEqual([ "disable", "low", @@ -89,10 +90,11 @@ describe("Opencode Go Fetchers", () => { const models = await getOpencodeGoModels("k") // Live limits never override the curated temperature capability - // (Zoo-Code-Org/Zoo-Code#2055). - expect(models["grok-4.6"]).toMatchObject({ contextWindow: 400_000, supportsTemperature: true }) + // (Zoo-Code-Org/Zoo-Code#2055), and never override the curated + // context window (Zoo-Code-Org/Zoo-Code#723). + expect(models["grok-4.6"]).toMatchObject({ contextWindow: 500_000, supportsTemperature: true }) expect(models["muse-spark-1.2-contributor"]).toMatchObject({ maxTokens: 65_536, supportsTemperature: true }) - expect(models["gpt-5.6-luna"]).toMatchObject({ contextWindow: 900_000, supportsTemperature: false }) + expect(models["gpt-5.6-luna"]).toMatchObject({ contextWindow: 1_050_000, supportsTemperature: false }) }) it("uses curated gpt-6-luna metadata when the live model omits metadata", async () => { @@ -226,6 +228,23 @@ describe("Opencode Go Fetchers", () => { warnSpy.mockRestore() }) + it("skips models whose type is not chat (maintainer review)", async () => { + mockedAxios.get.mockResolvedValue({ + data: { + data: [ + { id: "chat-model", type: "chat" }, + { id: "embed-model", type: "embedding" }, + { id: "untyped-model" }, + ], + }, + }) + + const models = await getOpencodeGoModels() + + // Untyped entries stay listed for older responses. + expect(Object.keys(models).sort()).toEqual(["chat-model", "untyped-model"]) + }) + it("forwards the caller's abort signal to the request", async () => { mockedAxios.get.mockResolvedValue({ data: { data: [] } }) const controller = new AbortController() @@ -252,6 +271,64 @@ describe("Opencode Go Fetchers", () => { await expect(fetchPromise).rejects.toMatchObject({ name: "AbortError" }) }) + + it("nested limit and modalities win over conflicting flat fields through the fetcher path", async () => { + mockedAxios.get.mockResolvedValue({ + data: { + data: [ + { + id: "glm-5.1", + type: "chat", + limit: { context: 999_000, output: 7_777 }, + modalities: { input: ["text", "image"] }, + // Conflicting flat values must lose to the nested shape. + context_window: 888_000, + max_tokens: 6_666, + supports_images: false, + }, + { + id: "some-unknown-model", + type: "chat", + limit: { context: 123_456, output: 4_096 }, + modalities: { input: ["text", "image"] }, + context_window: 666_000, + max_tokens: 6_666, + supports_images: false, + }, + ], + }, + }) + + const models = await getOpencodeGoModels("k") + + // Curated: the registry context window stays authoritative, while the + // nested limit.output and modalities.input win over the flat fields. + expect(models["glm-5.1"]).toMatchObject({ + contextWindow: 204_800, + maxTokens: 7_777, + supportsImages: true, + }) + // Unknown: the nested limit.context drives the published window too. + expect(models["some-unknown-model"]).toMatchObject({ + contextWindow: 123_456, + maxTokens: 4_096, + supportsImages: true, + }) + }) + + it("falls back to flat supports_images when modalities omits input", async () => { + mockedAxios.get.mockResolvedValue({ + data: { + data: [{ id: "another-unknown-model", type: "chat", modalities: {}, supports_images: true }], + }, + }) + + const models = await getOpencodeGoModels("k") + + // modalities.input is optional: the fetcher must fall back to the flat + // field instead of dereferencing the missing input array. + expect(models["another-unknown-model"].supportsImages).toBe(true) + }) }) describe("parseOpencodeGoModel", () => { @@ -313,10 +390,11 @@ describe("Opencode Go Fetchers", () => { } }) - it("merges live endpoint values over the native registry for a curated model", () => { + it("keeps the registry context window and merges live max tokens for a curated model", () => { const info = parseOpencodeGoModel({ id: "glm-5.1", context_window: 150000, max_output_tokens: 8000 }) - // Live values win for volatile fields. - expect(info.contextWindow).toBe(150000) + // The registry context window stays authoritative (Zoo-Code-Org/Zoo-Code#723). + expect(info.contextWindow).toBe(204_800) + // Live max-token values still win. expect(info.maxTokens).toBe(8000) // Capability flags and pricing come from the native registry. expect(info.supportsPromptCache).toBe(true) @@ -326,20 +404,70 @@ describe("Opencode Go Fetchers", () => { expect(info.inputPrice).toBe(1.4) }) - it("keeps gpt-6-luna live limits authoritative over its curated metadata", () => { + it("keeps the curated gpt-6-luna context window over a smaller live value", () => { const info = parseOpencodeGoModel({ id: "gpt-6-luna", context_window: 1_000_000, max_output_tokens: 64_000, }) - expect(info.contextWindow).toBe(1_000_000) + expect(info.contextWindow).toBe(1_050_000) expect(info.maxTokens).toBe(64_000) expect(info.supportsImages).toBe(true) expect(info.inputPrice).toBe(0.1) expect(info.cacheWritesPrice).toBe(0.125) }) + it("keeps the curated context window when the endpoint reports the blanket 200000 (#723)", () => { + // The Go /models endpoint reports context_window: 200000 for every + // model. deepseek-v4-flash is curated with a 1M window and must keep it. + const info = parseOpencodeGoModel({ id: "deepseek-v4-flash", context_window: 200_000 }) + expect(info.contextWindow).toBe(1_000_000) + expect(info.maxTokens).toBe(384_000) + expect(info.supportsPromptCache).toBe(true) + }) + + it("the registry context window is authoritative for every curated model, whatever the live value", () => { + for (const [id, native] of Object.entries(opencodeGoModels)) { + const blanket = parseOpencodeGoModel({ id, context_window: 200_000 }) + expect(blanket.contextWindow, id).toBe(native.contextWindow) + + // A live value larger than the registry one must not win either. + const larger = parseOpencodeGoModel({ id, context_length: 2_000_000 }) + expect(larger.contextWindow, id).toBe(native.contextWindow) + } + }) + + it("an unknown model keeps the live context value in both field forms", () => { + const window = parseOpencodeGoModel({ id: "x", context_window: 123_456 }) + expect(window.contextWindow).toBe(123_456) + + const length = parseOpencodeGoModel({ id: "x", context_length: 654_321 }) + expect(length.contextWindow).toBe(654_321) + }) + + it("live max tokens still override the curated maxTokens in both field forms", () => { + const output = parseOpencodeGoModel({ id: "glm-5.1", max_output_tokens: 9_999 }) + expect(output.maxTokens).toBe(9_999) + + const max = parseOpencodeGoModel({ id: "glm-5.1", max_tokens: 8_888 }) + expect(max.maxTokens).toBe(8_888) + }) + + it("keeps a live zero max-token value for a curated model", () => { + const info = parseOpencodeGoModel({ id: "glm-5.1", context_window: 150_000, max_output_tokens: 0 }) + // 0 is a live value, not an absent field: ?? falls back only on null and undefined. + expect(info.maxTokens).toBe(0) + // The registry context window stays authoritative. + expect(info.contextWindow).toBe(204_800) + }) + + it("keeps a live zero context window for an unknown model", () => { + const info = parseOpencodeGoModel({ id: "x", context_window: 0 }) + expect(info.contextWindow).toBe(0) + expect(info.maxTokens).toBe(opencodeGoDefaultModelInfo.maxTokens) + }) + it("uses native registry defaults when the live payload omits volatile fields", () => { const info = parseOpencodeGoModel({ id: "deepseek-v4-flash" }) const native = getOpencodeGoModelInfo("deepseek-v4-flash")! @@ -435,5 +563,48 @@ describe("Opencode Go Fetchers", () => { expect(info.description).toBeTruthy() } }) + + it("an unknown model takes its live values from limit and modalities (maintainer review)", () => { + const info = parseOpencodeGoModel({ + id: "x", + type: "chat", + limit: { context: 123_456, output: 4_096 }, + modalities: { input: ["text", "image"] }, + }) + expect(info.contextWindow).toBe(123_456) + expect(info.maxTokens).toBe(4_096) + expect(info.supportsImages).toBe(true) + }) + + it("nested limit values win over the flat fallback fields", () => { + const info = parseOpencodeGoModel({ + id: "x", + limit: { context: 555_000, output: 5_555 }, + context_window: 666_000, + max_output_tokens: 6_666, + }) + expect(info.contextWindow).toBe(555_000) + expect(info.maxTokens).toBe(5_555) + }) + + it("keeps the first flat alias when both fields of a pair are set", () => { + const info = parseOpencodeGoModel({ + id: "x", + context_window: 111_111, + context_length: 222_222, + max_output_tokens: 444_444, + max_tokens: 333_333, + }) + expect(info.contextWindow).toBe(111_111) + expect(info.maxTokens).toBe(444_444) + }) + + it("modalities.input drives image support with the flat field as fallback", () => { + const fromModalities = parseOpencodeGoModel({ id: "x", modalities: { input: ["text"] } }) + expect(fromModalities.supportsImages).toBe(false) + + const fromFlat = parseOpencodeGoModel({ id: "x", supports_images: true }) + expect(fromFlat.supportsImages).toBe(true) + }) }) }) diff --git a/src/api/providers/fetchers/opencode-go.ts b/src/api/providers/fetchers/opencode-go.ts index 68ac3d2d454..f920ab59573 100644 --- a/src/api/providers/fetchers/opencode-go.ts +++ b/src/api/providers/fetchers/opencode-go.ts @@ -10,11 +10,15 @@ const OPENCODE_GO_BASE_URL = "https://opencode.ai/zen/go/v1" // The Opencode Go `/models` endpoint follows the OpenAI `/models` shape. The // `id` is the only guaranteed field; metadata is optional and best-effort, so -// the schema is intentionally permissive. Pricing is intentionally NOT parsed: -// the units returned by the endpoint aren't documented, and reporting a wrong -// cost is worse than reporting "unknown" — so cost stays sourced from the -// native registry (or undefined for unknown models) until the pricing shape is -// confirmed against the live endpoint. +// the schema is intentionally permissive. The current endpoint publishes +// limits under `limit.context`/`limit.output`, image support under +// `modalities.input`, and a `type` discriminator; the flat fields stay +// parsed as a fallback for older responses (maintainer review, PR #2646). +// Pricing is intentionally NOT parsed: the units returned by the endpoint +// aren't documented, and reporting a wrong cost is worse than reporting +// "unknown" — so cost stays sourced from the native registry (or undefined +// for unknown models) until the pricing shape is confirmed against the live +// endpoint. const opencodeGoModelSchema = z.object({ id: z.string(), name: z.string().optional(), @@ -24,6 +28,18 @@ const opencodeGoModelSchema = z.object({ max_tokens: z.number().optional(), max_output_tokens: z.number().optional(), supports_images: z.boolean().optional(), + limit: z + .object({ + context: z.number().optional(), + output: z.number().optional(), + }) + .optional(), + modalities: z + .object({ + input: z.array(z.string()).optional(), + }) + .optional(), + type: z.string().optional(), }) export type OpencodeGoModel = z.infer @@ -36,19 +52,24 @@ const opencodeGoModelsResponseSchema = z.object({ * Maps a raw Opencode Go model entry to the internal {@link ModelInfo} shape. * * The Go `/models` endpoint only reliably returns `id` and (sometimes) - * `context_window`/`max_tokens`. It does NOT advertise capability flags - * (`supportsReasoningEffort`, `preserveReasoning`, `supportsMaxTokens`, - * `supportsPromptCache`) or pricing, all of which the extension needs to drive - * reasoning controls, interleaved-thinking tool calls, the max-output-tokens - * slider, and accurate cost reporting. + * limits and image support — under `limit.context`/`limit.output` and + * `modalities.input` on the current deployment, or the flat + * `context_window`/`max_tokens` fields on older ones. It does NOT advertise + * capability flags (`supportsReasoningEffort`, `preserveReasoning`, + * `supportsMaxTokens`, `supportsPromptCache`) or pricing, all of which the + * extension needs to drive reasoning controls, interleaved-thinking tool + * calls, the max-output-tokens slider, and accurate cost reporting. * * Resolution order for a fully-populated {@link ModelInfo}: * 1. Use the native registry ({@link getOpencodeGoModelInfo}) when the model - * is curated, including its capabilities and pricing. - * 2. Override static limits and image support with live `/models` values when - * present, keeping the gateway authoritative for volatile fields. + * is curated, including its context window, capabilities and pricing. + * 2. Override max tokens and image support with live `/models` values when + * present. The registry context window stays authoritative: the endpoint + * reports a blanket `context_window` (200k) for every model, so a live + * value would clobber curated windows (Zoo-Code-Org/Zoo-Code#723). * 3. Fall back to {@link opencodeGoDefaultModelInfo} for an unknown model, * ensuring downstream consumers always receive a fully-populated object. + * Unknown models still take the live context window when present. * * @param model - Validated model entry from the `/models` response. * @returns Normalised model metadata suitable for the model picker. @@ -56,15 +77,17 @@ const opencodeGoModelsResponseSchema = z.object({ export const parseOpencodeGoModel = (model: OpencodeGoModel): ModelInfo => { const native = getOpencodeGoModelInfo(model.id) - // Live endpoint values take precedence over the registry for volatile fields. - const liveContextWindow = model.context_window ?? model.context_length - const liveMaxTokens = model.max_output_tokens ?? model.max_tokens - const liveSupportsImages = model.supports_images + // Live endpoint values take precedence for max tokens and image support. + // The registry context window stays authoritative for curated models. + // The nested limit/modalties objects are the documented shape; the flat + // fields stay as a fallback (maintainer review, PR #2646). + const liveContextWindow = model.limit?.context ?? model.context_window ?? model.context_length + const liveMaxTokens = model.limit?.output ?? model.max_output_tokens ?? model.max_tokens + const liveSupportsImages = model.modalities?.input?.includes("image") ?? model.supports_images if (native) { return { ...native, - ...(liveContextWindow !== undefined && { contextWindow: liveContextWindow }), ...(liveMaxTokens !== undefined && { maxTokens: liveMaxTokens }), ...(liveSupportsImages !== undefined && { supportsImages: liveSupportsImages }), description: model.description ?? model.name ?? native.description, @@ -120,6 +143,12 @@ export async function getOpencodeGoModels( console.warn(`Skipping invalid Opencode Go model entry: ${JSON.stringify(rawModel)}`) continue } + // Only chat models belong in the picker; an embedding or other + // type would surface as an unusable choice. Entries without a + // type stay listed for older responses (maintainer review). + if (parsed.data.type !== undefined && parsed.data.type !== "chat") { + continue + } models[parsed.data.id] = parseOpencodeGoModel(parsed.data) } } catch (error) {