Open History to see the last commit
1/**
2 * Converts Neon AI Gateway catalog rows into packages/ai-neon/src/model-meta.ts.
3 *
4 * Usage:
5 * pnpm tsx scripts/convert-neon-models.ts
6 */
7
8import { writeFile } from 'node:fs/promises'
9import { dirname, resolve } from 'node:path'
10import { fileURLToPath } from 'node:url'
11import { ANTHROPIC_MODELS } from '../packages/ai-anthropic/src/model-meta'
12import { models } from './neon.models'
13import {
14 reasoningShapeName,
15 reasoningShapeTable,
16} from './model-sync/reasoning-shapes'
17import type { NeonCatalogModel } from './neon.models'
18import type { ReasoningLevel, ReasoningLevels } from '@tanstack/ai'
19import type { ReasoningShape } from './model-sync/reasoning-shapes'
20
21const __dirname = dirname(fileURLToPath(import.meta.url))
22const OUTPUT_PATH = resolve(__dirname, '../packages/ai-neon/src/model-meta.ts')
23
24type InputModality = 'text' | 'image' | 'audio' | 'video' | 'document'
25
26const INPUT_MODALITY_ORDER: Array<InputModality> = [
27 'text',
28 'image',
29 'document',
30 'audio',
31 'video',
32]
33
34const MODALITY_MAP: Record<string, InputModality> = {
35 text: 'text',
36 image: 'image',
37 audio: 'audio',
38 video: 'video',
39 pdf: 'document',
40 file: 'document',
41 document: 'document',
42}
43
44// The effort values that are TanStack levels of the same name. `none` is `off`.
45const LEVELS = new Set<string>([
46 'minimal',
47 'low',
48 'medium',
49 'high',
50 'xhigh',
51 'max',
52])
53
54function formatStringArray(ids: Array<string>): string {
55 if (ids.length === 0) return '[] as const'
56 return `[\n${ids.map((id) => ` ${JSON.stringify(id)},`).join('\n')}\n] as const`
57}
58
59function inputModalitiesFor(model: NeonCatalogModel): Array<InputModality> {
60 const mapped = new Set<InputModality>(['text'])
61 for (const modality of model.modalities?.input ?? []) {
62 const known = MODALITY_MAP[modality.toLowerCase()]
63 if (known) mapped.add(known)
64 }
65 return INPUT_MODALITY_ORDER.filter((modality) => mapped.has(modality))
66}
67
68/**
69 * The model's `chat({ reasoning })` data, from its `effort` values. Both
70 * OpenAI-shaped routes send one effort value (`reasoning_effort`, or
71 * `reasoning.effort` on Responses), so no model takes a token budget here.
72 */
73function reasoningFor(model: NeonCatalogModel): ReasoningShape | undefined {
74 if (!model.reasoning) return undefined
75 const effort = model.reasoning_options?.find(
76 (option) => option.type === 'effort',
77 )
78 const levels: ReasoningLevels = {}
79 for (const value of effort?.values ?? []) {
80 if (value === 'none') levels.off = 'none'
81 else if (LEVELS.has(value)) levels[value as ReasoningLevel] = value
82 }
83 if (Object.keys(levels).length === 0) return undefined
84 return { levels, budget: false }
85}
86
87function convertModels(): string {
88 const anthropicIds = new Set<string>(ANTHROPIC_MODELS)
89 const chat: Array<NeonCatalogModel> = []
90 const embedding: Array<string> = []
91 let unreleased = 0
92 let unserved = 0
93
94 for (const model of models) {
95 if (model.released !== true) {
96 unreleased += 1
97 continue
98 }
99 const capabilities = model.capabilities ?? {}
100 if (model.type === 'embedding') {
101 if (capabilities.embeddings) embedding.push(model.id)
102 continue
103 }
104 if (capabilities.chat !== 'not-served' || capabilities.responses) {
105 chat.push(model)
106 } else {
107 unserved += 1
108 }
109 }
110
111 chat.sort((a, b) => a.id.localeCompare(b.id))
112 embedding.sort((a, b) => a.localeCompare(b))
113
114 if (chat.length === 0) {
115 throw new Error(
116 'The Neon catalog produced no chat models. Refusing to overwrite model-meta.ts.',
117 )
118 }
119 if (embedding.length === 0) {
120 throw new Error(
121 'The Neon catalog produced no embedding models. Refusing to overwrite model-meta.ts.',
122 )
123 }
124
125 // A Claude id that ai-anthropic does not know yet stays on Chat Completions
126 // until the provider-model sync adds it there.
127 const anthropic = chat.filter(
128 (model) =>
129 model.capabilities?.native_dialect === 'anthropic' &&
130 anthropicIds.has(model.id),
131 )
132 const anthropicSet = new Set(anthropic.map((model) => model.id))
133 const responses = chat.filter((model) => model.capabilities?.responses)
134 const responsesOnly = responses.filter(
135 (model) => model.capabilities?.chat === 'not-served',
136 )
137 const rejectsSampling = chat.filter((model) => model.temperature === false)
138 // Measured against the gateway: every Gemini-dialect model 400s on
139 // `stream_options` on Chat Completions, and streams usage without it.
140 const rejectsStreamOptions = chat.filter(
141 (model) => model.capabilities?.native_dialect === 'gemini',
142 )
143
144 console.log(
145 `Classified ${models.length} models: chat=${chat.length} (anthropic=${anthropic.length} responses=${responses.length} responses-only=${responsesOnly.length}) embedding=${embedding.length} unreleased=${unreleased} unserved=${unserved}`,
146 )
147
148 const shapes: Array<ReasoningShape> = []
149 const reasoningEntries: Array<string> = []
150 for (const model of chat) {
151 // ai-anthropic owns the reasoning data of the models it serves.
152 if (anthropicSet.has(model.id)) continue
153 const shape = reasoningFor(model)
154 if (!shape) continue
155 shapes.push(shape)
156 reasoningEntries.push(
157 ` ${JSON.stringify(model.id)}: ${reasoningShapeName(shape)},`,
158 )
159 }
160
161 const ids = (rows: Array<NeonCatalogModel>) => rows.map((model) => model.id)
162 const modalityEntries = chat
163 .map(
164 (model) =>
165 ` ${JSON.stringify(model.id)}: [${inputModalitiesFor(model)
166 .map((modality) => `'${modality}'`)
167 .join(', ')}],`,
168 )
169 .join('\n')
170
171 return `/**
172 * This file is generated by scripts/convert-neon-models.ts. Do not edit by hand.
173 */
174
175import type { ModelReasoning } from '@tanstack/ai'
176import type { ModelReasoningCapability } from '@tanstack/ai/adapter-internals'
177
178/** Every chat model the gateway serves, on any route. */
179export const NEON_CHAT_MODELS = ${formatStringArray(ids(chat))}
180
181export type NeonChatModel = (typeof NEON_CHAT_MODELS)[number]
182
183/** Claude models. \`neonText\` sends them to the Anthropic Messages route. */
184export const NEON_ANTHROPIC_MODELS = ${formatStringArray(ids(anthropic))}
185
186export type NeonAnthropicModel = (typeof NEON_ANTHROPIC_MODELS)[number]
187
188/** Models the Responses route serves. They take \`api: 'responses'\`. */
189export const NEON_RESPONSES_MODELS = ${formatStringArray(ids(responses))}
190
191export type NeonResponsesModel = (typeof NEON_RESPONSES_MODELS)[number]
192
193/** Models only the Responses route serves. \`neonText\` always sends them there. */
194export const NEON_RESPONSES_ONLY_MODELS = ${formatStringArray(ids(responsesOnly))}
195
196export type NeonResponsesOnlyModel = (typeof NEON_RESPONSES_ONLY_MODELS)[number]
197
198export const NEON_EMBEDDING_MODELS = ${formatStringArray(embedding)}
199
200export type NeonEmbeddingModel = (typeof NEON_EMBEDDING_MODELS)[number]
201
202/** Models that reject \`temperature\` and \`top_p\`. The adapters drop both. */
203export const NEON_MODELS_REJECTING_SAMPLING: ReadonlySet<string> = new Set(${formatStringArray(ids(rejectsSampling))})
204
205/**
206 * Models that reject \`stream_options\` on Chat Completions. They stream
207 * usage without it.
208 */
209export const NEON_MODELS_REJECTING_STREAM_OPTIONS: ReadonlySet<string> = new Set(${formatStringArray(ids(rejectsStreamOptions))})
210
211/** The input modalities of each chat model. */
212export const NEON_MODEL_INPUT_MODALITIES = {
213${modalityEntries}
214} as const satisfies Record<NeonChatModel, ReadonlyArray<string>>
215
216export type NeonModelInputModalitiesByName = typeof NEON_MODEL_INPUT_MODALITIES
217
218// Reasoning
219//
220// Each model's data for \`chat({ reasoning })\`: the levels it has, with the
221// effort value the gateway takes for each. Each model points at one shape.
222
223${reasoningShapeTable(shapes).code}
224
225const NEON_REASONING = {
226${reasoningEntries.join('\n')}
227} satisfies Partial<Record<NeonChatModel, ModelReasoning>>
228
229/**
230 * Each chat model's reasoning levels, for \`chat({ reasoning })\`. A model
231 * that is not here does not reason.
232 */
233export type NeonModelReasoningByName = {
234 [K in keyof typeof NEON_REASONING]: ModelReasoningCapability<
235 (typeof NEON_REASONING)[K]
236 >
237}
238
239/**
240 * Runtime map from chat model id to its reasoning data, for the text
241 * adapters. An unknown id gives \`undefined\`: the adapter sends no reasoning
242 * field.
243 */
244export const NEON_MODEL_REASONING: Readonly<Record<string, ModelReasoning>> =
245 NEON_REASONING
246`
247}
248
249async function main() {
250 await writeFile(OUTPUT_PATH, convertModels(), 'utf-8')
251 console.log(`Model meta file written to ${OUTPUT_PATH}`)
252}
253
254main().catch((error) => {
255 console.error(error)
256 process.exit(1)
257})