Open History to see the last commit
1import { openCodeRouteSpec, classifyOpenCodeRoute } from '../opencode-routes.ts'
2/** OpenCode Zen: live ids, with routes and prices from OpenCode's models.dev catalog. */
3import {
4 loadOpenCodeCatalog,
5 OPENCODE_CATALOG_URL,
6 parseOpenCodeCatalog,
7} from '../opencode-catalog.ts'
8import { fetchJson } from '../types.ts'
9import type {
10 ListModelsResult,
11 ModelInfo,
12 ProviderConfig,
13 ProviderSecrets,
14 SpecFetchResult,
15} from '../types.ts'
16
17export const OPENCODE_MODELS_URL = 'https://opencode.ai/inference/v1/models'
18
19const FETCH_TIMEOUT_MS = 30_000
20
21function isRecord(value: unknown): value is Record<string, unknown> {
22 return typeof value === 'object' && value !== null && !Array.isArray(value)
23}
24
25export function parseOpencodeModels(
26 payload: unknown,
27 tag = 'opencode',
28): Array<ModelInfo> {
29 if (!isRecord(payload) || !Array.isArray(payload.data)) {
30 throw new Error(`${tag}: models payload has no data array`)
31 }
32 const models: Array<ModelInfo> = []
33 for (const row of payload.data) {
34 if (!isRecord(row) || typeof row.id !== 'string' || row.id.length === 0) {
35 continue
36 }
37 models.push({
38 rawId: row.id,
39 releasedAt: null,
40 pricing: null,
41 })
42 }
43 if (models.length === 0) {
44 throw new Error(`${tag}: models payload listed no ids`)
45 }
46 return models
47}
48
49function withCatalog(
50 listed: Array<ModelInfo>,
51 catalog: Record<string, ModelInfo>,
52): Array<ModelInfo> {
53 return listed.map((model): ModelInfo => {
54 const facts = catalog[model.rawId]
55 if (!facts) return { ...model, absent: { releasedAt: 'cleared' } }
56 return {
57 ...model,
58 ...facts,
59 ...(facts.releasedAt == null
60 ? { absent: { releasedAt: 'cleared' } }
61 : {}),
62 }
63 })
64}
65
66async function listModels(
67 _env: ProviderSecrets,
68 kv?: KVNamespace,
69): Promise<ListModelsResult> {
70 const signal = AbortSignal.timeout(FETCH_TIMEOUT_MS)
71 const [payload, loaded] = await Promise.all([
72 fetchJson(OPENCODE_MODELS_URL, { signal }),
73 loadOpenCodeCatalog(kv),
74 ])
75 const catalog = parseOpenCodeCatalog(
76 loaded.payload,
77 'opencode',
78 loaded.hash,
79 loaded.extractedAt,
80 )
81 const models = withCatalog(parseOpencodeModels(payload), catalog)
82 if (!models.some((model) => model.pricing)) {
83 throw new Error('opencode: catalog priced 0 listed models')
84 }
85 return { models }
86}
87
88async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
89 const loaded = await loadOpenCodeCatalog()
90 const catalog = parseOpenCodeCatalog(
91 loaded.payload,
92 'opencode',
93 loaded.hash,
94 loaded.extractedAt,
95 )
96 return openCodeRouteSpec(
97 Object.values(catalog),
98 { url: OPENCODE_CATALOG_URL, hash: loaded.hash },
99 false,
100 )
101}
102
103// No `upstreamModelIdentity`, here or for OpenCode Go: OpenCode names no
104// maker. `owned_by` is `opencode` on every row, and the catalog's AI SDK
105// package names the wire protocol: `@ai-sdk/anthropic` also serves the Qwen
106// rows, `@ai-sdk/openai` the Grok and Muse Spark rows.
107export const provider: ProviderConfig = {
108 id: 'opencode',
109 displayName: 'OpenCode Zen',
110 specSourceUrl: OPENCODE_CATALOG_URL,
111 modelsEndpoint: OPENCODE_MODELS_URL,
112 defaultDerivation: 'docs-derived',
113 bindSyncedRoutesOnly: true,
114 bindStoredRoutesWithoutSchemas: true,
115 fetchSpec,
116 listModels,
117 classify: classifyOpenCodeRoute,
118}