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

opencode-catalog.ts

482 lines16.2 KB

1/** OpenCode owns models.dev. Only its two providers may read these facts. */
2import { compileTokenCard } from '@modelschemas/rate-card'
3import type { RateCard, TokenRateTier } from '@modelschemas/rate-card'
4​
5import { cachedDocs } from './model-facts.ts'
6import type { ChatRequestMap } from './request-map.ts'
7import { fetchText, sha256Text } from './types.ts'
8import type {
9 FactSource,
10 ModelFactSources,
11 ModelInfo,
12 ModelReasoning,
13} from './types.ts'
14​
15export const OPENCODE_CATALOG_URL = 'https://models.dev/api.json'
16type OpenCodeProvider = 'opencode' | 'opencode-go'
17​
18/** Path under the provider's `/v1` base. A package this map does not name has no route. */
19const PACKAGE_SUFFIX: Record<string, (id: string) => string> = {
20 '@ai-sdk/anthropic': () => 'messages',
21 '@ai-sdk/openai': () => 'responses',
22 '@ai-sdk/google': (id) => `models/${id}`,
23 '@ai-sdk/openai-compatible': () => 'chat/completions',
25​
26const COST_LEVERS: Record<string, string> = {
27 input: 'input_tokens',
28 output: 'output_tokens',
29 cache_read: 'cache_read_tokens',
30 cache_write: 'cache_write_tokens',
31 reasoning: 'reasoning_tokens',
32 input_audio: 'audio_tokens',
33 output_audio: 'audio_output_tokens',
35​
36export interface OpenCodeCatalogLoad {
37 payload: unknown
38 hash: string
39 extractedAt: string
41​
42function record(value: unknown): value is Record<string, unknown> {
43 return value !== null && typeof value === 'object' && !Array.isArray(value)
45function strings(value: unknown): value is Array<string> {
46 return Array.isArray(value) && value.every((v) => typeof v === 'string')
48function reasoning(value: unknown): ModelReasoning | null {
49 if (value === undefined) return null
50 if (!Array.isArray(value))
51 throw new Error('opencode: unreadable reasoning_options')
52 const options = value.map((option: unknown) => {
53 if (
54 !record(option) ||
55 !['toggle', 'effort', 'budget_tokens'].includes(String(option.type))
56 ) {
57 throw new Error('opencode: unreadable reasoning option')
58 }
59 if (
60 option.type === 'effort' &&
61 (!strings(option.values) || option.values.length === 0)
62 ) {
63 throw new Error('opencode: unreadable effort values')
64 }
65 return option
66 })
67 const toggle = options.some((option) => option.type === 'toggle')
68 const effort = options.find((option) => option.type === 'effort')
69 if (effort && strings(effort.values))
70 return {
71 mode: 'effort',
72 efforts: effort.values,
73 mandatory:
74 toggle ||
75 effort.values.some((v) => ['none', 'off', 'disabled'].includes(v))
76 ? false
77 : null,
78 }
79 if (options.some((option) => option.type === 'budget_tokens'))
80 return { mode: 'budget', mandatory: toggle ? false : null }
81 return toggle ? { mode: 'toggle', mandatory: false } : null
83​
84/** USD per million tokens → USD per token. Throws on a shape a card must not invent. */
85function perToken(
86 cost: Record<string, unknown>,
87 id: string,
88 allowTier: boolean,
89): Record<string, number> {
90 const rates: Record<string, number> = {}
91 for (const [key, value] of Object.entries(cost)) {
92 if (allowTier && key === 'tier') continue
93 const lever = COST_LEVERS[key]
94 if (!lever) throw new Error(`opencode: unreadable cost.${key} for ${id}`)
95 if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
96 throw new Error(`opencode: unreadable cost.${key} for ${id}`)
97 }
98 rates[lever] = value / 1e6
99 }
100 if (rates.input_tokens === undefined || rates.output_tokens === undefined) {
101 throw new Error(`opencode: cost for ${id} omits input or output`)
102 }
103 return rates
105​
106function sameRates(a: Record<string, number>, b: Record<string, number>) {
107 const keys = new Set([...Object.keys(a), ...Object.keys(b)])
108 for (const key of keys) if (a[key] !== b[key]) return false
109 return true
111​
112/**
113 * Zen `cost`, including context `tiers`. `context_over_200k` is a copy of
114 * one tier's rates (its name is not the threshold when the tier size is
115 * 272K); a copy that matches no tier is unreadable. All-zero quotes are a
116 * sourced free card. Go does not call this: its `cost` is not a billed price.
117 */
118export function compileOpenCodeCost(
119 cost: unknown,
120 id: string,
121 source: RateCard['source'],
122): RateCard {
123 if (!record(cost)) throw new Error(`opencode: unreadable cost for ${id}`)
124 const { tiers: rawTiers, context_over_200k: legacy, ...baseCost } = cost
125 const base = perToken(baseCost, id, false)
126 const tiers: Array<TokenRateTier> = []
127 if (rawTiers !== undefined) {
128 if (!Array.isArray(rawTiers)) {
129 throw new Error(`opencode: unreadable cost.tiers for ${id}`)
130 }
131 const seen = new Set<number>()
132 for (const tier of rawTiers) {
133 if (!record(tier) || !record(tier.tier)) {
134 throw new Error(`opencode: unreadable cost tier for ${id}`)
135 }
136 if (tier.tier.type !== 'context') {
137 throw new Error(`opencode: unreadable cost tier for ${id}`)
138 }
139 const size = tier.tier.size
140 if (
141 typeof size !== 'number' ||
142 !Number.isSafeInteger(size) ||
143 size <= 0
144 ) {
145 throw new Error(`opencode: unreadable cost tier for ${id}`)
146 }
147 if (seen.has(size)) {
148 throw new Error(`opencode: duplicate cost tier for ${id}`)
149 }
150 seen.add(size)
151 const rates = perToken(tier, id, true)
152 if (Object.keys(base).some((lever) => !(lever in rates))) {
153 throw new Error(`opencode: cost tier for ${id} omits a base rate`)
154 }
155 tiers.push({ minPromptTokens: size, rates })
156 }
157 }
158 if (legacy !== undefined) {
159 if (!record(legacy)) {
160 throw new Error(`opencode: unreadable context_over_200k for ${id}`)
161 }
162 const rates = perToken(legacy, id, false)
163 const copied = tiers.some((tier) => sameRates(tier.rates, rates))
164 if (!copied) {
165 throw new Error(
166 `opencode: context_over_200k disagrees with cost tiers for ${id}`,
167 )
168 }
169 }
170 if (
171 Object.values(base).every((rate) => rate === 0) &&
172 tiers.every((tier) => Object.values(tier.rates).every((rate) => rate === 0))
173 ) {
174 if (tiers.length > 0) {
175 throw new Error(`opencode: zero base rate with a tier for ${id}`)
176 }
177 return {
178 inputs: Object.fromEntries(
179 Object.keys(base).map((lever) => [
180 lever,
181 {
182 param: lever,
183 bound: 'usage',
184 kind: 'number',
185 ...(!['input_tokens', 'output_tokens'].includes(lever)
186 ? { default: 0 }
187 : {}),
188 },
189 ]),
190 ),
191 tables: { rate: { base } },
192 price: 0,
193 examples: [],
194 source,
195 }
196 }
197 if (Object.values(base).every((rate) => rate === 0) && tiers.length > 0) {
198 throw new Error(`opencode: zero base rate with a priced tier for ${id}`)
199 }
200 const audio = ['audio_tokens', 'audio_output_tokens'].filter(
201 (lever) => lever in base || tiers.some((tier) => lever in tier.rates),
202 )
203 const card = compileTokenCard(
204 base,
205 tiers,
206 source,
207 audio.length > 0 ? { extraPromptLevers: audio } : undefined,
208 )
209 if (!card) throw new Error(`opencode: unreadable cost for ${id}`)
210 return card
212​
213/** Path under the host. Zen follows models.dev `api`; Go stays on the documented `/zen/go/v1` ids. */
214function schemaEndpointId(
215 provider: OpenCodeProvider,
216 apiPath: string | null,
217 npm: string,
218 id: string,
219): string | null {
220 const suffix = PACKAGE_SUFFIX[npm]?.(id) ?? null
221 if (!suffix) return null
222 if (id.includes('/'))
223 throw new Error(`${provider}: unreadable model id ${id}`)
224 // Go's console page still publishes https://opencode.ai/zen/go/v1, and that
225 // host serves the same listing as models.dev's /inference/go/v1. Keep the
226 // ids already stored (`v1/...`) until the documented base is the one to move.
227 return provider === 'opencode' ? `${apiPath}/${suffix}` : `v1/${suffix}`
229​
230export function parseOpenCodeCatalog(
231 payload: unknown,
232 provider: OpenCodeProvider,
233 hash: string,
234 extractedAt?: string,
235): Record<string, ModelInfo> {
236 if (!record(payload) || !record(payload[provider])) {
237 throw new Error(`${provider}: models.dev has no provider models object`)
238 }
239 const block = payload[provider]
240 const models = block.models
241 if (!record(models)) {
242 throw new Error(`${provider}: models.dev has no provider models object`)
243 }
244 if (typeof block.npm !== 'string' || block.npm.length === 0) {
245 throw new Error(`${provider}: models.dev provider npm is missing`)
246 }
247 let apiPath: string | null = null
248 if (provider === 'opencode') {
249 if (typeof block.api !== 'string') {
250 throw new Error('opencode: models.dev api is missing')
251 }
252 const url = new URL(block.api)
253 apiPath = url.pathname.replace(/^\//, '').replace(/\/$/, '')
254 if (url.origin !== 'https://opencode.ai' || apiPath.length === 0) {
255 throw new Error('opencode: models.dev api is not an opencode.ai path')
256 }
257 }
258 const out: Record<string, ModelInfo> = {}
259 for (const [id, row] of Object.entries(models)) {
260 if (!record(row) || row.id !== id)
261 throw new Error(`${provider}: unreadable catalog model ${id}`)
262 const source = (path: string): FactSource => ({
263 derivation: 'listing',
264 sourceUrl: OPENCODE_CATALOG_URL,
265 sourceHash: hash,
266 path: `${provider}.models.${id}.${path}`,
267 })
268 const factSources: ModelFactSources = {}
269 const model: ModelInfo = {
270 rawId: id,
271 releasedAt: null,
272 knowledgeCutoff: null,
273 openWeights: null,
274 contextWindow: null,
275 maxOutput: null,
276 modalities: null,
277 capabilities: null,
278 reasoning: reasoning(row.reasoning_options),
279 requestMap: null,
280 providerMetadata: row,
281 }
282 if (row.name !== undefined) {
283 if (typeof row.name !== 'string' || row.name.length === 0) {
284 throw new Error(`${provider}: unreadable name for ${id}`)
285 }
286 model.displayName = row.name
287 }
288 let modelNpm: string | null = null
289 if (row.provider !== undefined) {
290 if (!record(row.provider)) {
291 throw new Error(`${provider}: unreadable provider for ${id}`)
292 }
293 if (row.provider.npm !== undefined) {
294 if (
295 typeof row.provider.npm !== 'string' ||
296 row.provider.npm.length === 0
297 ) {
298 throw new Error(`${provider}: unreadable provider.npm for ${id}`)
299 }
300 modelNpm = row.provider.npm
301 }
302 }
303 const npm = modelNpm ?? block.npm
304 const endpoint = schemaEndpointId(provider, apiPath, npm, id)
305 if (endpoint) {
306 model.schemaEndpointId = endpoint
307 model.activity = 'chat'
308 factSources.schemaEndpointId = {
309 derivation: 'listing',
310 sourceUrl: OPENCODE_CATALOG_URL,
311 sourceHash: hash,
312 // The route is the package's path, not an endpoint stated on the model.
313 path: modelNpm
314 ? `${provider}.models.${id}.provider.npm`
315 : `${provider}.npm`,
316 }
317 }
318 if (provider === 'opencode' && row.cost !== undefined) {
319 if (!extractedAt)
320 throw new Error('opencode: catalog extractedAt is missing')
321 model.pricing = compileOpenCodeCost(row.cost, id, {
322 url: OPENCODE_CATALOG_URL,
323 hash,
324 extractedAt,
325 })
326 factSources.pricing = {
327 derivation: 'listing',
328 sourceUrl: OPENCODE_CATALOG_URL,
329 sourceHash: hash,
330 path: `${provider}.models.${id}.cost`,
331 }
332 }
333 if (row.release_date !== undefined) {
334 if (
335 typeof row.release_date !== 'string' ||
336 !/^\d{4}-\d{2}-\d{2}$/.test(row.release_date)
337 )
338 throw new Error(`${provider}: unreadable release_date for ${id}`)
339 const timestamp = Date.parse(`${row.release_date}T00:00:00.000Z`)
340 if (
341 !Number.isFinite(timestamp) ||
342 new Date(timestamp).toISOString().slice(0, 10) !== row.release_date
343 )
344 throw new Error(`${provider}: unreadable release_date for ${id}`)
345 model.releasedAt = timestamp / 1000
346 }
347 if (row.knowledge !== undefined) {
348 if (
349 typeof row.knowledge !== 'string' ||
350 !/^\d{4}-(0[1-9]|1[0-2])(?:-\d{2})?$/.test(row.knowledge)
351 )
352 throw new Error(`${provider}: unreadable knowledge for ${id}`)
353 if (row.knowledge.length === 10) {
354 const timestamp = Date.parse(`${row.knowledge}T00:00:00.000Z`)
355 if (
356 !Number.isFinite(timestamp) ||
357 new Date(timestamp).toISOString().slice(0, 10) !== row.knowledge
358 )
359 throw new Error(`${provider}: unreadable knowledge for ${id}`)
360 }
361 model.knowledgeCutoff = row.knowledge
362 factSources.knowledgeCutoff = source('knowledge')
363 }
364 if (row.open_weights !== undefined) {
365 if (typeof row.open_weights !== 'boolean')
366 throw new Error(`${provider}: unreadable open_weights for ${id}`)
367 model.openWeights = row.open_weights
368 factSources.openWeights = source('open_weights')
369 }
370 if (row.limit !== undefined) {
371 if (!record(row.limit))
372 throw new Error(`${provider}: unreadable limits for ${id}`)
373 for (const [key, fact] of [
374 ['context', 'contextWindow'],
375 ['output', 'maxOutput'],
376 ] as const) {
377 const value = row.limit[key]
378 if (value === undefined) continue
379 if (
380 typeof value !== 'number' ||
381 !Number.isSafeInteger(value) ||
382 value <= 0
383 )
384 throw new Error(`${provider}: unreadable ${key} limit for ${id}`)
385 model[fact] = value
386 factSources[fact] = source(`limit.${key}`)
387 }
388 }
389 if (row.modalities !== undefined) {
390 if (
391 !record(row.modalities) ||
392 !strings(row.modalities.input) ||
393 !strings(row.modalities.output)
394 )
395 throw new Error(`${provider}: unreadable modalities for ${id}`)
396 model.modalities = row.modalities
397 factSources.modalities = source('modalities')
398 }
399 const capabilities: Record<string, boolean> = {}
400 const sources: Record<string, FactSource> = {}
401 for (const [field, flag] of [
402 ['tool_call', 'tools'],
403 ['reasoning', 'reasoning'],
404 ['structured_output', 'structured_outputs'],
405 ] as const) {
406 if (row[field] === undefined) continue
407 if (typeof row[field] !== 'boolean')
408 throw new Error(`${provider}: unreadable ${field} for ${id}`)
409 capabilities[flag] = row[field]
410 sources[flag] = source(field)
411 }
412 if (record(row.modalities) && strings(row.modalities.input)) {
413 capabilities.vision = row.modalities.input.includes('image')
414 sources.vision = source('modalities.input')
415 }
416 if (Object.keys(capabilities).length) {
417 model.capabilities = capabilities
418 factSources.capabilities = sources
419 }
420 if (model.reasoning) factSources.reasoning = source('reasoning_options')
421 if (
422 row.interleaved !== undefined &&
423 typeof row.interleaved !== 'boolean' &&
424 (!record(row.interleaved) || typeof row.interleaved.field !== 'string')
425 )
426 throw new Error(`${provider}: unreadable interleaved for ${id}`)
427 if (
428 record(row.interleaved) &&
429 row.interleaved.field === 'reasoning_content'
430 ) {
431 const map: ChatRequestMap = {
432 thinking: null,
433 maxTokensField: null,
434 developerRole: null,
435 replayReasoningContent: true,
436 store: null,
437 strictTools: null,
438 sessionAffinity: null,
439 cacheControl: null,
440 toolStream: null,
441 reasoningEffort: null,
442 }
443 model.requestMap = map
444 factSources.requestMap = source('interleaved.field')
445 }
446 model.factSources = factSources
447 out[id] = model
448 }
449 if (!Object.keys(out).length)
450 throw new Error(`${provider}: models.dev listed no models`)
451 return out
453​
454export async function loadOpenCodeCatalog(
455 kv?: KVNamespace,
456): Promise<OpenCodeCatalogLoad> {
457 // A v31 entry is `{payload, hash}` with no `extractedAt`. Reading it
458 // throws on every Zen price until the TTL. This key misses that entry.
459 return cachedDocs(kv, `${OPENCODE_CATALOG_URL}#extracted-at`, async () => {
460 const text = await fetchText(OPENCODE_CATALOG_URL, {
461 signal: AbortSignal.timeout(30_000),
462 })
463 return {
464 payload: JSON.parse(text) as unknown,
465 hash: await sha256Text(text),
466 extractedAt: new Date().toISOString(),
467 }
468 })
470​
471export async function openCodeCatalog(
472 provider: OpenCodeProvider,
473 kv?: KVNamespace,
474): Promise<Record<string, ModelInfo>> {
475 const catalog = await loadOpenCodeCatalog(kv)
476 return parseOpenCodeCatalog(
477 catalog.payload,
478 provider,
479 catalog.hash,
480 catalog.extractedAt,
481 )