Open History to see the last commit
1/**
2 * Syncs OpenRouter models into native provider model-meta.ts files.
3 *
4 * For each supported provider (OpenAI, Anthropic, Gemini, Grok), this script:
5 * 1. Reads the OpenRouter model list
6 * 2. Filters models matching the provider prefix
7 * 3. Identifies models missing from the provider's model-meta.ts
8 * 4. Generates and inserts new model constants, array entries, and type map entries
9 *
10 * Usage:
11 * pnpm tsx scripts/sync-provider-models.ts
12 *
13 * ## Providers deliberately NOT synced
14 *
15 * The sync is only safe when the provider's own model ids can be derived from
16 * OpenRouter's. Adding an entry to `PROVIDER_MAP` for a provider where that
17 * doesn't hold generates ids that 404 at request time, which is worse than no
18 * automation. These are excluded on purpose:
19 *
20 * - **byteplus** (`@tanstack/ai-byteplus`) — OpenRouter lists the same models
21 * under undated slugs (`bytedance-seed/seed-1.6`), but BytePlus Ark
22 * addresses them by *date-suffixed* id (`seed-1-6-250615`), and the suffix
23 * is not derivable from anything OpenRouter publishes. Ark's own `GET
24 * /models` is the catalog, and it is not exhaustive. On top of that the
25 * package's capability tables are live-probe-verified specifically because
26 * the published metadata is wrong in both directions, so a metadata-driven
27 * sync would overwrite probed facts with worse ones. Use `/gap-analysis
28 * byteplus` instead; the probe recipe is in that package's `model-meta.ts`.
29 * - **fal**, **elevenlabs** — media-only providers whose endpoint ids are not
30 * OpenRouter models at all. fal image fields have their own generator
31 * (`scripts/generate-fal-image-field-map.ts`).
32 * - **bedrock** — ids are AWS-region-qualified; see
33 * `scripts/fetch-bedrock-models.ts`.
34 */
35
36import { execFileSync } from 'node:child_process'
37import { readFile, writeFile } from 'node:fs/promises'
38import { dirname, resolve } from 'node:path'
39import { fileURLToPath } from 'node:url'
40import { models } from './openrouter.models'
41import type { OpenRouterModel } from './openrouter.models'
42
43const __dirname = dirname(fileURLToPath(import.meta.url))
44const ROOT = resolve(__dirname, '..')
45
46// ---------------------------------------------------------------------------
47// Provider configuration
48// ---------------------------------------------------------------------------
49
50/** Seconds in 30 days — models older than this before the last sync are skipped */
51const MAX_MODEL_AGE_SECONDS = 30 * 24 * 60 * 60
52const LAST_RUN_FILE = resolve(ROOT, 'scripts/.sync-models-last-run')
53
54interface ProviderConfig {
55 /** npm package name for changeset */
56 packageName: string
57 metaFile: string
58 /** How array entries reference the constant, e.g. '.name' or '.id' */
59 arrayRef: '.name' | '.id'
60 /** Which field name is used for context window size */
61 contextField: 'context_window' | 'max_input_tokens'
62 /** Name of the exported chat model array */
63 chatArrayName: string
64 /** Name of the provider options type map */
65 providerOptionsTypeName: string
66 /** Name of the input modalities type map */
67 inputModalitiesTypeName: string
68 /**
69 * Name of the runtime `Record<string, number>` mapping model id →
70 * `max_output_tokens`, if the provider maintains one. Anthropic uses this to
71 * default the required `max_tokens` request field to the model's real ceiling
72 * (issue #849); other providers treat token limits as optional and omit it.
73 */
74 maxOutputTokensMapName?: string
75 /** The supports block template (minus input modalities, which come from OpenRouter) */
76 referenceSupportsBody: string
77 /** Valid input modality types for this provider's ModelMeta interface */
78 validInputModalities: Array<InputModality>
79 /** The satisfies type clause (after 'as const satisfies') */
80 referenceSatisfies: string
81 /** The type string for provider options map entries */
82 referenceProviderOptionsEntry: string
83 /** Whether this provider has both name AND id fields */
84 hasBothNameAndId: boolean
85 /** Whether the provider options type is a mapped type (skip insertion) */
86 providerOptionsIsMappedType: boolean
87 /** Model ID patterns to always skip (matched against stripped ID) */
88 skipPatterns: Array<string>
89}
90
91const PROVIDER_MAP: Record<string, ProviderConfig> = {
92 'openai/': {
93 packageName: '@tanstack/ai-openai',
94 metaFile: resolve(ROOT, 'packages/ai-openai/src/model-meta.ts'),
95 arrayRef: '.name',
96 contextField: 'context_window',
97 chatArrayName: 'OPENAI_CHAT_MODELS',
98 providerOptionsTypeName: 'OpenAIChatModelProviderOptionsByName',
99 inputModalitiesTypeName: 'OpenAIModelInputModalitiesByName',
100 validInputModalities: ['text', 'image', 'audio', 'video'],
101 referenceSupportsBody: ` output: ['text'],
102 endpoints: ['chat', 'chat-completions'],
103 features: ['streaming', 'function_calling', 'structured_outputs', 'distillation'],
104 tools: ['web_search', 'web_search_preview', 'file_search', 'image_generation', 'code_interpreter', 'mcp', 'computer_use', 'local_shell', 'shell', 'apply_patch'],`,
105 referenceSatisfies:
106 'ModelMeta<OpenAIBaseOptions & OpenAIReasoningOptions & OpenAIStructuredOutputOptions & OpenAIToolsOptions & OpenAIStreamingOptions & OpenAIMetadataOptions>',
107 referenceProviderOptionsEntry:
108 'OpenAIBaseOptions & OpenAIReasoningOptions & OpenAIStructuredOutputOptions & OpenAIToolsOptions & OpenAIStreamingOptions & OpenAIMetadataOptions',
109 hasBothNameAndId: false,
110 providerOptionsIsMappedType: false,
111 skipPatterns: [
112 'gpt-3.5-', // Legacy GPT-3.5 models
113 'gpt-4-', // Legacy GPT-4 base models (not 4.1+)
114 'gpt-4o', // GPT-4o variants (4o, 4o-mini, 4o-audio, etc.)
115 'gpt-oss-', // Open-source/experimental models
116 'chatgpt-', // ChatGPT branded models
117 ],
118 },
119 'anthropic/': {
120 packageName: '@tanstack/ai-anthropic',
121 metaFile: resolve(ROOT, 'packages/ai-anthropic/src/model-meta.ts'),
122 arrayRef: '.id',
123 contextField: 'context_window',
124 chatArrayName: 'ANTHROPIC_MODELS',
125 providerOptionsTypeName: 'AnthropicChatModelProviderOptionsByName',
126 inputModalitiesTypeName: 'AnthropicModelInputModalitiesByName',
127 maxOutputTokensMapName: 'ANTHROPIC_MODEL_MAX_OUTPUT_TOKENS',
128 validInputModalities: ['text', 'image', 'audio', 'video', 'document'],
129 referenceSupportsBody: ` extended_thinking: true,
130 priority_tier: true,
131 tools: ['web_search', 'web_fetch', 'code_execution', 'computer_use', 'bash', 'text_editor', 'memory'],`,
132 referenceSatisfies:
133 'ModelMeta<AnthropicContainerOptions & AnthropicContextManagementOptions & AnthropicMCPOptions & AnthropicServiceTierOptions & AnthropicStopSequencesOptions & AnthropicThinkingOptions & AnthropicToolChoiceOptions & AnthropicSamplingOptions>',
134 referenceProviderOptionsEntry:
135 'AnthropicContainerOptions & AnthropicContextManagementOptions & AnthropicMCPOptions & AnthropicServiceTierOptions & AnthropicStopSequencesOptions & AnthropicThinkingOptions & AnthropicToolChoiceOptions & AnthropicSamplingOptions',
136 hasBothNameAndId: true,
137 providerOptionsIsMappedType: false,
138 skipPatterns: [],
139 },
140 'google/': {
141 packageName: '@tanstack/ai-gemini',
142 metaFile: resolve(ROOT, 'packages/ai-gemini/src/model-meta.ts'),
143 arrayRef: '.name',
144 contextField: 'max_input_tokens',
145 chatArrayName: 'GEMINI_MODELS',
146 providerOptionsTypeName: 'GeminiChatModelProviderOptionsByName',
147 inputModalitiesTypeName: 'GeminiModelInputModalitiesByName',
148 validInputModalities: ['text', 'image', 'audio', 'video', 'document'],
149 referenceSupportsBody: ` output: ['text'],
150 capabilities: ['batch_api', 'caching', 'function_calling', 'structured_output', 'thinking'],
151 tools: ['code_execution', 'file_search', 'google_search', 'url_context'],`,
152 referenceSatisfies:
153 'ModelMeta<GeminiToolConfigOptions & GeminiSafetyOptions & GeminiCommonConfigOptions & GeminiCachedContentOptions & GeminiStructuredOutputOptions & GeminiThinkingOptions>',
154 referenceProviderOptionsEntry:
155 'GeminiToolConfigOptions & GeminiSafetyOptions & GeminiCommonConfigOptions & GeminiCachedContentOptions & GeminiStructuredOutputOptions & GeminiThinkingOptions',
156 hasBothNameAndId: false,
157 providerOptionsIsMappedType: false,
158 skipPatterns: [
159 'gemma-', // Gemma open-source models (not Gemini API models)
160 ],
161 },
162 'x-ai/': {
163 packageName: '@tanstack/ai-grok',
164 metaFile: resolve(ROOT, 'packages/ai-grok/src/model-meta.ts'),
165 arrayRef: '.name',
166 contextField: 'context_window',
167 chatArrayName: 'GROK_CHAT_MODELS',
168 providerOptionsTypeName: 'GrokChatModelProviderOptionsByName',
169 inputModalitiesTypeName: 'GrokModelInputModalitiesByName',
170 validInputModalities: ['text', 'image', 'audio', 'video', 'document'],
171 referenceSupportsBody: ` output: ['text'],
172 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
173 tools: [],`,
174 referenceSatisfies: 'ModelMeta',
175 referenceProviderOptionsEntry: 'GrokProviderOptions',
176 hasBothNameAndId: false,
177 providerOptionsIsMappedType: true,
178 skipPatterns: [],
179 },
180}
181
182// ---------------------------------------------------------------------------
183// Utility functions
184// ---------------------------------------------------------------------------
185
186type InputModality = 'text' | 'image' | 'audio' | 'video' | 'document'
187
188const MODALITY_MAP: Record<string, InputModality> = {
189 text: 'text',
190 image: 'image',
191 audio: 'audio',
192 video: 'video',
193 file: 'document',
194 document: 'document',
195}
196
197/**
198 * Map OpenRouter input modalities to our standard modality types.
199 * Same mapping as the existing convert-openrouter-models.ts script.
200 */
201function mapInputModalities(modalities: Array<string>): Array<InputModality> {
202 const mapped = modalities
203 .map((m) => MODALITY_MAP[m.toLowerCase()])
204 .filter((m): m is InputModality => m !== undefined)
205 // Ensure at least 'text' is present
206 if (!mapped.includes('text')) {
207 mapped.unshift('text')
208 }
209 return mapped
210}
211
212/** Strip the provider prefix from an OpenRouter model ID */
213function stripPrefix(prefix: string, modelId: string): string {
214 return modelId.slice(prefix.length)
215}
216
217/**
218 * Convert a model ID (after prefix stripping) to a TypeScript constant name.
219 * E.g. 'gpt-6' -> 'GPT_6', 'grok-4.20-multi-agent' -> 'GROK_4_20_MULTI_AGENT'
220 */
221function toConstName(prefix: string, modelId: string): string {
222 const stripped = stripPrefix(prefix, modelId)
223 return stripped
224 .replace(/[-]/g, '_')
225 .replace(/[.]/g, '_')
226 .replace(/[:]/g, '_')
227 .replace(/[/]/g, '_')
228 .toUpperCase()
229}
230
231/**
232 * Convert an OpenRouter price string to per-million-token pricing.
233 * Same logic as the existing convert script.
234 */
235function convertPrice(priceStr: string | undefined): number {
236 const price = parseFloat(priceStr ?? '0')
237 if (isNaN(price)) return 0
238 const result = price * 1_000_000
239 return Math.round(result * 1e10) / 1e10
240}
241
242/**
243 * Normalize a model ID for comparison.
244 * Handles cases where the same model uses dots vs dashes
245 * (e.g., Anthropic's 'claude-3-5-haiku' vs OpenRouter's 'claude-3.5-haiku').
246 */
247function normalizeId(id: string): string {
248 return id.replace(/[.]/g, '-')
249}
250
251/**
252 * Extract existing model IDs from a model-meta file.
253 * Matches BOTH name: 'xxx' AND id: 'xxx' lines to get a complete set,
254 * since providers like Anthropic use different naming between name and id.
255 * Returns a normalized set for comparison purposes.
256 */
257function extractExistingModelIds(content: string): Set<string> {
258 const ids = new Set<string>()
259 // Match both name and id fields inside const blocks
260 const nameRegex = /^\s+name:\s*'([^']+)'/gm
261 const idRegex = /^\s+id:\s*'([^']+)'/gm
262 let match
263 while ((match = nameRegex.exec(content)) !== null) {
264 ids.add(normalizeId(match[1]!))
265 }
266 while ((match = idRegex.exec(content)) !== null) {
267 ids.add(normalizeId(match[1]!))
268 }
269 return ids
270}
271
272/**
273 * Extract existing constant names from a model-meta file.
274 * Matches: const UPPER_CASE_NAME =
275 */
276function extractExistingConstNames(content: string): Set<string> {
277 const names = new Set<string>()
278 const regex = /^const\s+([A-Z][A-Z0-9_]+)\s*=/gm
279 let match
280 while ((match = regex.exec(content)) !== null) {
281 names.add(match[1]!)
282 }
283 return names
284}
285
286/**
287 * Check if an OpenRouter model outputs text (for chat model array).
288 */
289function outputsText(model: OpenRouterModel): boolean {
290 return model.architecture.output_modalities.includes('text')
291}
292
293/**
294 * Check if an OpenRouter model produces image output.
295 *
296 * Image-generation models are skipped entirely by the sync — regardless of
297 * whether they ALSO output text — because image model arrays require manual
298 * curation with specialized type maps (sizes, provider options, the native
299 * image union). This covers text+image "native" image models such as the
300 * Gemini "Nano Banana" family (`gemini-*-image`, e.g.
301 * `gemini-3.1-flash-lite-image`), which would otherwise be misclassified as
302 * chat models (they output text) and inserted with a bogus `output: ['text']`.
303 */
304function outputsImage(model: OpenRouterModel): boolean {
305 return model.architecture.output_modalities.includes('image')
306}
307
308/**
309 * Non-chat model family prefixes to exclude from chat model arrays.
310 * These are audio/music/video/image generation models that happen to
311 * include 'text' in their output modalities but are not chat models.
312 */
313const NON_CHAT_MODEL_PREFIXES = [
314 'lyria-', // Google music generation
315 'veo-', // Google video generation
316 'imagen-', // Google image generation
317 'sora-', // OpenAI video generation
318 'dall-e-', // OpenAI image generation
319 'tts-', // Text-to-speech models
320]
321
322function isNonChatModel(strippedId: string): boolean {
323 return NON_CHAT_MODEL_PREFIXES.some((p) => strippedId.startsWith(p))
324}
325
326/**
327 * Check if a model should be skipped based on provider-specific patterns.
328 */
329function matchesSkipPattern(
330 strippedId: string,
331 patterns: Array<string>,
332): boolean {
333 return patterns.some((p) => strippedId.startsWith(p))
334}
335
336/**
337 * Read the last sync run timestamp. Returns epoch seconds, or null if no previous run.
338 */
339async function readLastRunTimestamp(): Promise<number | null> {
340 try {
341 const content = await readFile(LAST_RUN_FILE, 'utf-8')
342 const ts = parseInt(content.trim(), 10)
343 return isNaN(ts) ? null : ts
344 } catch {
345 return null
346 }
347}
348
349/**
350 * Write the current timestamp as the last sync run.
351 */
352async function writeLastRunTimestamp(): Promise<void> {
353 const now = Math.floor(Date.now() / 1000)
354 await writeFile(LAST_RUN_FILE, String(now) + '\n', 'utf-8')
355}
356
357/**
358 * Check if a model is too old to sync. Models created more than 30 days
359 * before the last sync run are considered deprecated/legacy and skipped.
360 */
361function isModelTooOld(
362 model: OpenRouterModel,
363 cutoffTimestamp: number,
364): boolean {
365 if (!model.created) return false // No date = don't skip
366 return model.created < cutoffTimestamp
367}
368
369// ---------------------------------------------------------------------------
370// Model constant generation
371// ---------------------------------------------------------------------------
372
373function generateModelConstant(
374 model: OpenRouterModel,
375 prefix: string,
376 config: ProviderConfig,
377): string {
378 const constName = toConstName(prefix, model.id)
379 const strippedId = stripPrefix(prefix, model.id)
380
381 const inputNormal = convertPrice(model.pricing.prompt)
382 const inputCached = convertPrice(model.pricing.input_cache_read)
383 const outputNormal = convertPrice(model.pricing.completion)
384
385 // Use actual input modalities from OpenRouter data, filtered to what this provider supports
386 const inputModalities = mapInputModalities(
387 model.architecture.input_modalities,
388 ).filter((m) => config.validInputModalities.includes(m))
389 const inputModalitiesStr = inputModalities.map((m) => `'${m}'`).join(', ')
390
391 const lines: Array<string> = []
392 lines.push(`const ${constName} = {`)
393
394 // name field
395 lines.push(` name: '${strippedId}',`)
396
397 // id field (Anthropic has both name and id, set to same value for new models)
398 if (config.hasBothNameAndId) {
399 lines.push(` id: '${strippedId}',`)
400 }
401
402 // context / max_input_tokens
403 if (model.context_length > 0) {
404 lines.push(
405 ` ${config.contextField}: ${formatNumber(model.context_length)},`,
406 )
407 }
408
409 // max_output_tokens
410 if (model.top_provider.max_completion_tokens) {
411 lines.push(
412 ` max_output_tokens: ${formatNumber(model.top_provider.max_completion_tokens)},`,
413 )
414 }
415
416 // supports block (actual input modalities + reference capabilities)
417 lines.push(` supports: {`)
418 lines.push(` input: [${inputModalitiesStr}],`)
419 lines.push(config.referenceSupportsBody)
420 lines.push(` },`)
421
422 // pricing
423 lines.push(` pricing: {`)
424 lines.push(` input: {`)
425 lines.push(` normal: ${inputNormal},`)
426 if (inputCached > 0) {
427 lines.push(` cached: ${inputCached},`)
428 }
429 lines.push(` },`)
430 lines.push(` output: {`)
431 lines.push(` normal: ${outputNormal},`)
432 lines.push(` },`)
433 lines.push(` },`)
434
435 lines.push(`} as const satisfies ${config.referenceSatisfies}`)
436
437 return lines.join('\n')
438}
439
440/**
441 * Format a number with underscore separators for readability.
442 * E.g. 131072 -> '131_072', 200000 -> '200_000'
443 */
444function formatNumber(n: number): string {
445 if (n < 1000) return String(n)
446 const str = String(n)
447 // Insert underscores every 3 digits from the right
448 const parts: Array<string> = []
449 let remaining = str
450 while (remaining.length > 3) {
451 parts.unshift(remaining.slice(-3))
452 remaining = remaining.slice(0, -3)
453 }
454 parts.unshift(remaining)
455 return parts.join('_')
456}
457
458// ---------------------------------------------------------------------------
459// File modification
460// ---------------------------------------------------------------------------
461
462/**
463 * Insert new model constants before the first `export` statement.
464 */
465function insertConstants(content: string, constants: Array<string>): string {
466 const block = '\n' + constants.join('\n\n') + '\n'
467 // Find the first `\nexport ` that is not inside a comment
468 const exportIndex = content.indexOf('\nexport ')
469 if (exportIndex === -1) {
470 // Fallback: append before end of file
471 return content + block
472 }
473 return content.slice(0, exportIndex) + block + content.slice(exportIndex)
474}
475
476/**
477 * Add entries to an array like: export const ARRAY_NAME = [ ... ] as const
478 *
479 * New entries are inserted immediately AFTER the opening bracket, so this
480 * only ever writes separators it owns: each inserted line carries its own
481 * trailing comma, and the existing body — multi-line with trailing commas, a
482 * short single-line array, comment lines, or nothing at all — follows
483 * unchanged. Inserting before the CLOSING bracket instead means guessing
484 * whether the last existing entry already has a comma, which is how a
485 * single-line `GROK_CHAT_MODELS` produced a syntax error (and a dead daily
486 * sync) the day grok-4.5 arrived. `pnpm format` reflows the result, so the
487 * inserted lines only need to parse, not to be pretty.
488 */
489function addToArray(
490 content: string,
491 arrayName: string,
492 entries: Array<string>,
493 arrayRef: string,
494): string {
495 const open = `export const ${arrayName} = [`
496 const openIndex = content.indexOf(open)
497 if (openIndex === -1) {
498 console.warn(` Warning: Could not find array '${arrayName}' in file`)
499 return content
500 }
501
502 const newEntries = entries
503 .map((constName) => ` ${constName}${arrayRef},`)
504 .join('\n')
505 const insertAt = openIndex + open.length
506 return `${content.slice(0, insertAt)}\n${newEntries}${content.slice(insertAt)}`
507}
508
509/**
510 * Add entries to a type map like:
511 * export type TypeName = {
512 * ...existing entries...
513 * }
514 */
515function addToTypeMap(
516 content: string,
517 typeName: string,
518 entries: Array<string>,
519): string {
520 // Match: export type TypeName = { ... \n}
521 const pattern = new RegExp(
522 `(export type ${typeName} = \\{[\\s\\S]*?)(\\n\\})`,
523 )
524 const match = pattern.exec(content)
525 if (!match) {
526 console.warn(` Warning: Could not find type map '${typeName}' in file`)
527 return content
528 }
529
530 const newEntries = entries.join('\n')
531 // Use replacer function to prevent $-character interpretation in replacement string
532 return content.replace(pattern, () => `${match[1]}\n${newEntries}${match[2]}`)
533}
534
535/**
536 * Add entries to a runtime object literal like:
537 * const MAP_NAME: Record<string, number> = {
538 * ...existing entries...
539 * }
540 * Used for the Anthropic id → max_output_tokens map (issue #849), which is a
541 * value declaration rather than a `type` alias.
542 */
543function addToObjectMap(
544 content: string,
545 mapName: string,
546 entries: Array<string>,
547): string {
548 // Match: const MAP_NAME: Record<string, number> = { ... \n}
549 const pattern = new RegExp(
550 `(const ${mapName}: Record<string, number> = \\{[\\s\\S]*?)(\\n\\})`,
551 )
552 const match = pattern.exec(content)
553 if (!match) {
554 console.warn(` Warning: Could not find object map '${mapName}' in file`)
555 return content
556 }
557
558 const newEntries = entries.join('\n')
559 // Use replacer function to prevent $-character interpretation in replacement string
560 return content.replace(pattern, () => `${match[1]}\n${newEntries}${match[2]}`)
561}
562
563// ---------------------------------------------------------------------------
564// Git-based change detection
565// ---------------------------------------------------------------------------
566
567/**
568 * Detect packages with uncommitted changes by running `git diff`.
569 * This captures changes from ALL prior pipeline steps (e.g. convert-openrouter-models.ts)
570 * not just the models added by this script.
571 */
572function detectChangedPackages(): Set<string> {
573 const changed = new Set<string>()
574 try {
575 const diff = execFileSync(
576 'git',
577 ['diff', 'HEAD', '--name-only', '--', 'packages/'],
578 { encoding: 'utf-8', cwd: ROOT },
579 ).trim()
580 if (!diff) return changed
581
582 for (const line of diff.split('\n')) {
583 // packages/ai-openrouter/... → @tanstack/ai-openrouter
584 const match = line.match(/^packages\/([\w-]+)\//)
585 if (match) {
586 changed.add(`@tanstack/${match[1]}`)
587 }
588 }
589 } catch {
590 // git not available (e.g. running outside a repo) — fall back to empty set
591 }
592 return changed
593}
594
595// ---------------------------------------------------------------------------
596// Main
597// ---------------------------------------------------------------------------
598
599async function main() {
600 let totalAdded = 0
601 const changedPackages = new Set<string>()
602
603 // Determine age cutoff: skip models created >30 days before last run
604 const lastRun = await readLastRunTimestamp()
605 const now = Math.floor(Date.now() / 1000)
606 const cutoffTimestamp = (lastRun ?? now) - MAX_MODEL_AGE_SECONDS
607 const cutoffDate = new Date(cutoffTimestamp * 1000)
608 .toISOString()
609 .split('T')[0]
610 console.log(
611 `Model age cutoff: ${cutoffDate} (skipping models created before this date)`,
612 )
613
614 for (const [prefix, config] of Object.entries(PROVIDER_MAP)) {
615 console.log(`\nProcessing provider: ${prefix}`)
616
617 // Filter OpenRouter models for this prefix
618 const providerModels = models.filter((m) => m.id.startsWith(prefix))
619 console.log(
620 ` Found ${providerModels.length} OpenRouter models with prefix '${prefix}'`,
621 )
622
623 // Read the provider's model-meta.ts
624 let content: string
625 try {
626 content = await readFile(config.metaFile, 'utf-8')
627 } catch {
628 console.warn(` Skipping: could not read ${config.metaFile}`)
629 continue
630 }
631
632 // Extract existing model IDs (normalized) and constant names
633 const existingIds = extractExistingModelIds(content)
634 const existingConstNames = extractExistingConstNames(content)
635
636 console.log(
637 ` Existing models in file: ${existingIds.size} IDs, ${existingConstNames.size} constants`,
638 )
639
640 // Find new models
641 const newModels: Array<{
642 model: OpenRouterModel
643 constName: string
644 strippedId: string
645 }> = []
646
647 for (const model of providerModels) {
648 const strippedId = stripPrefix(prefix, model.id)
649 const constName = toConstName(prefix, model.id)
650
651 // Skip models with ':' variants (e.g., 'anthropic/claude-3.7-sonnet:thinking')
652 // These are routing variants, not separate models
653 if (strippedId.includes(':')) {
654 continue
655 }
656
657 // Skip non-chat model families (audio/music/video/image generation)
658 if (isNonChatModel(strippedId)) {
659 continue
660 }
661
662 // Skip provider-specific patterns (deprecated/legacy model families)
663 if (matchesSkipPattern(strippedId, config.skipPatterns)) {
664 continue
665 }
666
667 // Skip models that are too old (created >30 days before last sync)
668 if (isModelTooOld(model, cutoffTimestamp)) {
669 continue
670 }
671
672 // Normalize for comparison to handle dots-vs-dashes naming differences
673 if (
674 !existingIds.has(normalizeId(strippedId)) &&
675 !existingConstNames.has(constName)
676 ) {
677 newModels.push({ model, constName, strippedId })
678 }
679 }
680
681 if (newModels.length === 0) {
682 console.log(' No new models to add.')
683 continue
684 }
685
686 console.log(` Adding ${newModels.length} new models:`)
687 for (const { strippedId, constName } of newModels) {
688 console.log(` - ${strippedId} (${constName})`)
689 }
690
691 // Filter out image-generation models (they need manual curation for
692 // size/provider type maps + the native image union). This includes
693 // text+image models like the Gemini "Nano Banana" native image family,
694 // not just image-only models.
695 const filteredModels = newModels.filter(({ model }) => !outputsImage(model))
696 const skippedImageModels = newModels.length - filteredModels.length
697 if (skippedImageModels > 0) {
698 console.log(
699 ` Skipping ${skippedImageModels} image-generation models (require manual curation)`,
700 )
701 }
702
703 if (filteredModels.length === 0) {
704 console.log(' No eligible models to add after filtering.')
705 continue
706 }
707
708 // Generate constants
709 const constants = filteredModels.map(({ model }) =>
710 generateModelConstant(model, prefix, config),
711 )
712
713 // Insert constants before first export
714 content = insertConstants(content, constants)
715
716 // Filter to chat-eligible models: must output text
717 const chatModels = filteredModels.filter(({ model }) => outputsText(model))
718
719 if (chatModels.length > 0) {
720 content = addToArray(
721 content,
722 config.chatArrayName,
723 chatModels.map(({ constName }) => constName),
724 config.arrayRef,
725 )
726 }
727
728 // NOTE: We intentionally do NOT add models to image arrays.
729 // Image model arrays have specialized type maps (sizes, provider options)
730 // that require manual curation.
731
732 // Add to provider options type map (skip for Grok - uses mapped type)
733 if (!config.providerOptionsIsMappedType && chatModels.length > 0) {
734 const providerOptionsEntries = chatModels.map(
735 ({ constName }) =>
736 ` [${constName}${config.arrayRef}]: ${config.referenceProviderOptionsEntry}`,
737 )
738 if (providerOptionsEntries.length > 0) {
739 content = addToTypeMap(
740 content,
741 config.providerOptionsTypeName,
742 providerOptionsEntries,
743 )
744 }
745 }
746
747 // Add to input modalities type map
748 const modalityEntries = chatModels.map(
749 ({ constName }) =>
750 ` [${constName}${config.arrayRef}]: typeof ${constName}.supports.input`,
751 )
752 if (modalityEntries.length > 0) {
753 content = addToTypeMap(
754 content,
755 config.inputModalitiesTypeName,
756 modalityEntries,
757 )
758 }
759
760 // Add to the id → max_output_tokens runtime map (Anthropic only). Only
761 // models whose generated constant actually carries `max_output_tokens`
762 // (i.e. OpenRouter reported a `max_completion_tokens`) get an entry; the
763 // rest correctly fall through to the map's constant default. Keeps the map
764 // in lockstep with the chat-model array so a synced model resolves to its
765 // real ceiling instead of the fallback (issue #849).
766 if (config.maxOutputTokensMapName) {
767 const maxOutputEntries = chatModels
768 .filter(({ model }) => model.top_provider.max_completion_tokens)
769 .map(
770 ({ constName }) =>
771 ` [${constName}${config.arrayRef}]: ${constName}.max_output_tokens,`,
772 )
773 if (maxOutputEntries.length > 0) {
774 content = addToObjectMap(
775 content,
776 config.maxOutputTokensMapName,
777 maxOutputEntries,
778 )
779 }
780 }
781
782 // Write the modified file
783 await writeFile(config.metaFile, content, 'utf-8')
784 console.log(` Wrote updated file: ${config.metaFile}`)
785 totalAdded += filteredModels.length
786 changedPackages.add(config.packageName)
787 }
788
789 console.log(`\nDone. Added ${totalAdded} new models total.`)
790
791 // Record this run's timestamp for future age-based filtering
792 await writeLastRunTimestamp()
793
794 // Detect all packages with uncommitted changes (includes changes from
795 // convert-openrouter-models.ts which runs before this script)
796 const allChangedPackages = detectChangedPackages()
797 for (const pkg of changedPackages) {
798 allChangedPackages.add(pkg)
799 }
800
801 if (allChangedPackages.size > 0) {
802 await createChangeset(allChangedPackages)
803 }
804}
805
806/**
807 * Create or update the sync-models changeset file.
808 * If one already exists, merges the package lists. Otherwise creates a new one.
809 */
810async function createChangeset(changedPackages: Set<string>) {
811 const changesetDir = resolve(ROOT, '.changeset')
812 const { readdir } = await import('node:fs/promises')
813 const files = await readdir(changesetDir)
814 const existing = files.find(
815 (f) => f.startsWith('sync-models') && f.endsWith('.md'),
816 )
817
818 if (existing) {
819 const existingPath = resolve(changesetDir, existing)
820 const existingContent = await readFile(existingPath, 'utf-8')
821
822 // Merge existing packages into the set
823 const pkgRegex = /'([^']+)':\s*patch/g
824 let match
825 while ((match = pkgRegex.exec(existingContent)) !== null) {
826 changedPackages.add(match[1]!)
827 }
828
829 const content = buildChangesetContent(changedPackages)
830 await writeFile(existingPath, content, 'utf-8')
831 console.log(`\nChangeset updated: ${existingPath}`)
832 } else {
833 const changesetFile = resolve(changesetDir, 'sync-models.md')
834 const content = buildChangesetContent(changedPackages)
835 await writeFile(changesetFile, content, 'utf-8')
836 console.log(`\nChangeset created: ${changesetFile}`)
837 }
838
839 console.log(` Packages: ${Array.from(changedPackages).sort().join(', ')}`)
840}
841
842function buildChangesetContent(packages: Set<string>): string {
843 const packageLines = Array.from(packages)
844 .sort()
845 .map((pkg) => `'${pkg}': patch`)
846 .join('\n')
847 return `---\n${packageLines}\n---\n\nUpdate model metadata from OpenRouter API\n`
848}
849
850main().catch((err) => {
851 console.error(err)
852 process.exit(1)
853})