Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

opencode.test.ts

370 lines12.2 KB

1import { price } from '@modelschemas/rate-card'
2import type { RateCard } from '@modelschemas/rate-card'
3import { OPENCODE_CATALOG_URL } from '../opencode-catalog.ts'
4​
5import { afterEach, describe, expect, it } from 'vitest'
6​
7import {
8 OPENCODE_DOCS_MARKDOWN,
9 OPENCODE_DOCS_URL,
10 OPENCODE_MODELS_URL,
11 parseZenDocs,
12 provider,
13} from './opencode.ts'
14​
15/** Excerpt of https://opencode.ai/zen/v1/models (2026-10-06). */
16const LISTING = {
17 object: 'list',
18 data: [
19 'claude-opus-5-5',
20 'claude-sonnet-5-5',
21 'gemini-3.1-pro',
22 'gpt-5.5',
23 'qwen3.8-max',
24 'jev-1.13',
25 'big-pickle',
26 ].map((id) => ({
27 id,
28 object: 'model',
29 created: 1791285710,
30 owned_by: 'opencode',
31 })),
33​
34/** Excerpt of https://opencode.ai/docs/zen.md (2026-10-06), rows verbatim. */
35const DOCS = `OpenCode Zen is a list of tested and verified models provided by the OpenCode team.
36​
37---
38​
39## Endpoints
40​
41You can also access our models through the following API endpoints.
42​
43| Model | Model ID | Endpoint | AI SDK Package |
44| ------------------------------- | ------------------------------- | --------------------------------------------------------- | --------------------------- |
45| GPT 5.5 | gpt-5.5 | \`https://opencode.ai/zen/v1/responses\` | \`@ai-sdk/openai\` |
46| Claude Opus 5.5 | claude-opus-5-5 | \`https://opencode.ai/zen/v1/messages\` | \`@ai-sdk/anthropic\` |
47| Gemini 3.1 Pro | gemini-3.1-pro | \`https://opencode.ai/zen/v1/models/gemini-3.1-pro\` | \`@ai-sdk/google\` |
48| Qwen3.8 Max | qwen3.8-max | \`https://opencode.ai/zen/v1/chat/completions\` | \`@ai-sdk/openai-compatible\` |
49| Jev 1.13 | jev-1.13 | \`https://opencode.ai/zen/v1/systemone\` | - |
50| Big Pickle | big-pickle | \`https://opencode.ai/zen/v1/chat/completions\` | \`@ai-sdk/openai-compatible\` |
51​
52The [model id](/docs/config/#models) in your OpenCode config
53uses the format \`opencode/<model-id>\`.
54​
55---
56​
57## Pricing
58​
59We support a pay-as-you-go model. Below are the prices **per 1M tokens**.
60​
61| Model | Input | Output | Cached Read | Cached Write |
62| --------------------------------- | ------ | ------- | ----------- | ------------ |
63| Big Pickle | Free | Free | Free | - |
64| Jev 1.13 | $0.042 | Free | - | - |
65| Qwen3.8 Max | $2.00 | $6.00 | $0.25 | $2.50 |
66| Claude Opus 5.5 | $4.00 | $20.00 | $0.20 | $5.00 |
67| Gemini 3.1 Pro (≤ 200K tokens) | $2.00 | $12.00 | $0.20 | - |
68| Gemini 3.1 Pro (> 200K tokens) | $4.00 | $18.00 | $0.40 | - |
69| GPT 5.5 (≤ 272K tokens) | $5.00 | $30.00 | $0.50 | - |
70| GPT 5.5 (> 272K tokens) | $10.00 | $45.00 | $1.00 | - |
71​
72The free models:
73​
74- Big Pickle is a stealth model that's free on OpenCode for a limited time.
75​
76---
77​
78### Deprecated models
79​
80| Model | Deprecation date |
81| ------------------ | ----------------- |
82| GPT 5.2 Codex | July 23, 2026 |
83​
84---
85​
86## Privacy
87​
88All our models are hosted in the US.
90​
91const originalFetch = globalThis.fetch
92​
93afterEach(() => {
94 globalThis.fetch = originalFetch
95})
96​
97function serve(docs: string) {
98 globalThis.fetch = ((url: string) => {
99 if (String(url) === OPENCODE_MODELS_URL) {
100 return Promise.resolve(new Response(JSON.stringify(LISTING)))
101 }
102 if (String(url) === OPENCODE_CATALOG_URL) {
103 return Promise.resolve(
104 new Response(
105 JSON.stringify({
106 opencode: { models: { synthetic: { id: 'synthetic' } } },
107 }),
108 ),
109 )
110 }
111 if (String(url) === OPENCODE_DOCS_MARKDOWN) {
112 return Promise.resolve(new Response(docs))
113 }
114 if (
115 String(url).startsWith(
116 'https://raw.githubusercontent.com/anomalyco/opencode/dev/',
117 )
118 )
119 return Promise.resolve(
120 new Response('export function POST(input: APIEvent) {}'),
121 )
122 return Promise.reject(new Error(`unexpected fetch: ${String(url)}`))
123 }) as typeof fetch
125​
126type Card = { tables: { rate: Record<string, Record<string, number>> } }
127​
128describe('opencode', () => {
129 it('reads activity and price from the docs tables', async () => {
130 serve(DOCS)
131 const { models } = await provider.listModels({})
132 const byId = Object.fromEntries(models.map((m) => [m.rawId, m]))
133​
134 expect(models.map((m) => m.rawId)).toEqual(LISTING.data.map((r) => r.id))
135​
136 const opus = byId['claude-opus-5-5']
137 expect(opus).toMatchObject({
138 displayName: 'Claude Opus 5.5',
139 activity: 'chat',
140 factSources: {
141 pricing: { derivation: 'docs-derived', sourceUrl: OPENCODE_DOCS_URL },
142 },
143 })
144 expect((opus?.pricing as Card).tables.rate).toEqual({
145 base: {
146 input_tokens: 4 / 1e6,
147 output_tokens: 20 / 1e6,
148 cache_read_tokens: 0.2 / 1e6,
149 cache_write_tokens: 5 / 1e6,
150 },
151 })
152​
153 // `≤ 272K` is the base row and `> 272K` the tier above it.
154 const gpt = byId['gpt-5.5']
155 expect(gpt).toMatchObject({
156 activity: 'chat',
157 })
158 expect((gpt?.pricing as Card).tables.rate).toEqual({
159 base: {
160 input_tokens: 5 / 1e6,
161 output_tokens: 30 / 1e6,
162 cache_read_tokens: 0.5 / 1e6,
163 },
164 '272000': {
165 input_tokens: 10 / 1e6,
166 output_tokens: 45 / 1e6,
167 cache_read_tokens: 1 / 1e6,
168 },
169 })
170​
171 expect(byId['gemini-3.1-pro']).toMatchObject({
172 activity: 'chat',
173 })
174 expect(byId['qwen3.8-max']).toMatchObject({
175 activity: 'chat',
176 })
177​
178 // System One is not a chat route.
179 expect(byId['jev-1.13']).toMatchObject({
180 activity: null,
181 })
182 // The native table explicitly quotes Free for input, output and cache.
183 expect(byId['big-pickle']).toMatchObject({
184 activity: 'chat',
185 pricing: {
186 price: 0,
187 tables: {
188 rate: {
189 base: { input_tokens: 0, output_tokens: 0, cache_read_tokens: 0 },
190 },
191 },
192 },
193 })
194 expect(byId['big-pickle']?.factSources?.pricing?.sourceUrl).toBe(
195 OPENCODE_DOCS_URL,
196 )
197 expect(
198 price(
199 byId['big-pickle']!.pricing as RateCard,
200 {},
201 { input_tokens: 10_000, output_tokens: 2_000, cache_read_tokens: 300 },
202 ),
203 ).toBe(0)
204 expect(byId['big-pickle']?.factSources?.schemaEndpointId?.sourceUrl).toBe(
205 OPENCODE_DOCS_URL,
206 )
207 // Listed, but in neither docs table.
208 expect(byId['claude-sonnet-5-5']).toEqual({
209 rawId: 'claude-sonnet-5-5',
210 releasedAt: null,
211 absent: { releasedAt: 'cleared' },
212 pricing: null,
213 })
214​
215 expect(byId['claude-opus-5-5']?.schemaEndpointId).toBe('v1/messages')
216 expect(byId['gpt-5.5']?.schemaEndpointId).toBe('v1/responses')
217 expect(byId['gemini-3.1-pro']?.schemaEndpointId).toBe(
218 'v1/models/gemini-3.1-pro',
219 )
220 expect(provider.bindSyncedRoutesOnly).toBe(true)
221​
222 expect(
223 (await provider.fetchSpec({})).specs[0]?.paths?.['/v1/messages']?.post,
224 ).toMatchObject({ 'x-modelschemas-route-only': true })
225 })
226​
227 it('merges matching native facts and preserves per-model protocol routes', async () => {
228 serve(DOCS)
229 const fetchDocs = globalThis.fetch
230 globalThis.fetch = ((url: string, init?: RequestInit) => {
231 if (String(url) === OPENCODE_CATALOG_URL)
232 return Promise.resolve(
233 new Response(
234 JSON.stringify({
235 opencode: {
236 models: {
237 'claude-opus-5-5': {
238 id: 'claude-opus-5-5',
239 limit: { context: 1234, output: 234 },
240 tool_call: true,
241 interleaved: { field: 'reasoning_content' },
242 reasoning_options: [{ type: 'budget_tokens', min: 1 }],
243 },
244 },
245 },
246 }),
247 ),
248 )
249 return fetchDocs(url, init)
250 }) as typeof fetch
251 const model = (await provider.listModels({})).models.find(
252 (m) => m.rawId === 'claude-opus-5-5',
253 )
254 expect(model).toMatchObject({
255 contextWindow: 1234,
256 maxOutput: 234,
257 schemaEndpointId: 'v1/messages',
258 reasoning: { mode: 'budget', mandatory: null },
259 requestMap: { replayReasoningContent: true },
260 })
261 expect(model?.factSources?.contextWindow?.sourceUrl).toBe(
262 OPENCODE_CATALOG_URL,
263 )
264 expect(model?.factSources?.pricing?.sourceUrl).toBe(OPENCODE_DOCS_URL)
265 })
266​
267 it('fails the sync when the native catalog is unavailable or malformed', async () => {
268 for (const response of [
269 new Response('unavailable', { status: 503 }),
270 new Response('{}'),
271 ]) {
272 serve(DOCS)
273 const fetchDocs = globalThis.fetch
274 globalThis.fetch = ((url: string, init?: RequestInit) =>
275 String(url) === OPENCODE_CATALOG_URL
276 ? Promise.resolve(response)
277 : fetchDocs(url, init)) as typeof fetch
278 await expect(provider.listModels({})).rejects.toThrow()
279 }
280 })
281​
282 it('throws on a page it cannot read', () => {
283 expect(() => parseZenDocs('<!DOCTYPE html><html>Not found</html>')).toThrow(
284 /HTML/,
285 )
286 expect(() =>
287 parseZenDocs(DOCS.replace('## Endpoints', '## Routes')),
288 ).toThrow(/Endpoints table/)
289 expect(() =>
290 parseZenDocs(DOCS.replace('per 1M tokens', 'per 1K tokens')),
291 ).toThrow(/per-1M-token/)
292 expect(() =>
293 parseZenDocs(DOCS.replace('| Cached Read |', '| Cache Hit |')),
294 ).toThrow(/per-1M-token/)
295 expect(() =>
296 parseZenDocs(
297 DOCS.replace('opencode.ai/zen/v1/messages', 'example.com/v1/messages'),
298 ),
299 ).toThrow(/unreadable Endpoints row/)
300 })
301​
302 it('reads the published current discounted price without calculating it', () => {
303 const docs = DOCS.replace(
304 '| Qwen3.8 Max | $2.00 | $6.00 | $0.25 | $2.50 |',
305 '| Qwen3.8 Max (50% off) | ~~$4.00~~ $2.00 | ~~$12.00~~ $6.00 | $0.25 | $2.50 |',
306 )
307 expect(docs).not.toBe(DOCS)
308 expect(parseZenDocs(docs)['qwen3.8-max']?.rates?.base.input_tokens).toBe(
309 2 / 1e6,
310 )
311 })
312​
313 it('throws on unreadable prices rather than hide failures', () => {
314 expect(() => parseZenDocs(DOCS.replace('$20.00 ', '$20/hour'))).toThrow(
315 /unreadable Pricing/,
316 )
317 expect(() =>
318 parseZenDocs(DOCS.replace(/\| GPT 5\.5 \(≤ 272K tokens\).*\n/, '')),
319 ).toThrow(/unreadable Pricing/)
320 expect(() =>
321 parseZenDocs(
322 DOCS.replace(
323 'Gemini 3.1 Pro (> 200K tokens)',
324 'Gemini 3.1 Pro (> 400K tokens)',
325 ),
326 ),
327 ).toThrow(/unreadable Pricing/)
328 expect(() =>
329 parseZenDocs(
330 DOCS.replace(
331 '| $10.00 | $45.00 | $1.00 |',
332 '| $10.00 | $45.00 | - |',
333 ),
334 ),
335 ).toThrow(/unreadable Pricing/)
336 })
337​
338 it('leaves undocumented routes unclassified', () => {
339 // An unknown route stays unclassified.
340 const route = parseZenDocs(
341 DOCS.replace(
342 'zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |\n| Jev',
343 'zen/v1/embeddings` | - |\n| Jev',
344 ),
345 )
346 expect(route['qwen3.8-max']).toMatchObject({
347 activity: null,
348 })
349 })
350})
351​
352it('tracks a native Free-to-paid price change and leaves unquoted output unknown', async () => {
353 const freeRow = /\| Big Pickle\s+\| Free\s+\| Free\s+\| Free\s+\| -\s+\|/
354 serve(DOCS.replace(freeRow, '| Big Pickle | $0.10 | $0.20 | $0.01 | - |'))
355 const paid = (await provider.listModels({})).models.find(
356 (model) => model.rawId === 'big-pickle',
357 )!
358 expect(
359 price(
360 paid.pricing as RateCard,
361 {},
362 { input_tokens: 1_000_000, output_tokens: 1_000_000 },
363 ),
364 ).toBeCloseTo(0.3)
365 serve(DOCS.replace(freeRow, '| Big Pickle | Free | - | Free | - |'))
366 const partial = (await provider.listModels({})).models.find(
367 (model) => model.rawId === 'big-pickle',
368 )!
369 expect(partial.pricing).toBeNull()
370})