/** * Syncs OpenRouter models into native provider model-meta.ts files. * * For each supported provider (OpenAI, Anthropic, Gemini, Grok), this script: * 1. Reads the OpenRouter model list * 2. Filters models matching the provider prefix * 3. Identifies models missing from the provider's model-meta.ts * 4. Generates and inserts new model constants, array entries, and type map entries * * Usage: * pnpm tsx scripts/sync-provider-models.ts * * ## Providers deliberately NOT synced * * The sync is only safe when the provider's own model ids can be derived from * OpenRouter's. Adding an entry to `PROVIDER_MAP` for a provider where that * doesn't hold generates ids that 404 at request time, which is worse than no * automation. These are excluded on purpose: * * - **byteplus** (`@tanstack/ai-byteplus`) — OpenRouter lists the same models * under undated slugs (`bytedance-seed/seed-1.6`), but BytePlus Ark * addresses them by *date-suffixed* id (`seed-1-6-250615`), and the suffix * is not derivable from anything OpenRouter publishes. Ark's own `GET * /models` is the catalog, and it is not exhaustive. On top of that the * package's capability tables are live-probe-verified specifically because * the published metadata is wrong in both directions, so a metadata-driven * sync would overwrite probed facts with worse ones. Use `/gap-analysis * byteplus` instead; the probe recipe is in that package's `model-meta.ts`. * - **fal** — media-only provider whose endpoint ids are not OpenRouter * models. fal image fields have their own generator * (`scripts/generate-fal-image-field-map.ts`). * - **elevenlabs** — text-to-speech, music, sound effects, and voice design * ids come from modelschemas via `scripts/sync-elevenlabs-models.ts`, not * OpenRouter. Transcription stays hand-maintained. * - **bedrock** — ids are AWS-region-qualified; see * `scripts/fetch-bedrock-models.ts`. */ import { execFileSync } from 'node:child_process' import { readFile, writeFile } from 'node:fs/promises' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { isRoutingAlias, toModelConstName, toNativeProviderId, } from './model-sync/ids' import { applyChatModelCatalogInserts, insertConstants, } from './model-sync/native-insert' import { buildAnthropicProviderOptionsType, buildProviderSupportsBody, } from './model-sync/provider-supports' import type { SyncedProvider } from './model-sync/provider-supports' import { models } from './openrouter.models' import type { OpenRouterModel } from './openrouter.models' const __dirname = dirname(fileURLToPath(import.meta.url)) const ROOT = resolve(__dirname, '..') // --------------------------------------------------------------------------- // Provider configuration // --------------------------------------------------------------------------- /** Seconds in 30 days — models older than this before the last sync are skipped */ const MAX_MODEL_AGE_SECONDS = 30 * 24 * 60 * 60 const LAST_RUN_FILE = resolve(ROOT, 'scripts/.sync-models-last-run') interface ProviderConfig { /** npm package name for changeset */ packageName: string metaFile: string /** How array entries reference the constant, e.g. '.name' or '.id' */ arrayRef: '.name' | '.id' /** Which field name is used for context window size */ contextField: 'context_window' | 'max_input_tokens' /** Name of the exported chat model array */ chatArrayName: string /** Name of the provider options type map */ providerOptionsTypeName: string /** Name of the input modalities type map */ inputModalitiesTypeName: string /** * Name of the per-model tool-capabilities type map. Missing this lets * `ResolveToolCapabilities` fall back to `readonly []` and every provider * tool stops type-checking on the new model. */ toolCapabilitiesTypeName?: string /** * Name of the runtime `Record` mapping model id → * `max_output_tokens`, if the provider maintains one. Anthropic uses this to * default the required `max_tokens` request field to the model's real ceiling * (issue #849); other providers treat token limits as optional and omit it. */ maxOutputTokensMapName?: string /** * Runtime set of models that accept tools plus `output_config.format` in * one request. New Anthropic ids are inserted here. `-fast` variants stay * out: fast mode is a request parameter, not a model id, and the model-meta * test expects every registered model in the set. */ combinedToolsAndSchemaSetName?: string /** Provider key for conservative supports generation */ kind: SyncedProvider /** Valid input modality types for this provider's ModelMeta interface */ validInputModalities: Array /** The satisfies type clause (after 'as const satisfies') */ referenceSatisfies: string /** The type string for provider options map entries */ referenceProviderOptionsEntry: string /** Whether this provider has both name AND id fields */ hasBothNameAndId: boolean /** Whether the provider options type is a mapped type (skip insertion) */ providerOptionsIsMappedType: boolean /** Model ID patterns to always skip (matched against stripped ID) */ skipPatterns: Array } const PROVIDER_MAP: Record = { 'openai/': { packageName: '@tanstack/ai-openai', metaFile: resolve(ROOT, 'packages/ai-openai/src/model-meta.ts'), arrayRef: '.name', contextField: 'context_window', chatArrayName: 'OPENAI_CHAT_MODELS', providerOptionsTypeName: 'OpenAIChatModelProviderOptionsByName', inputModalitiesTypeName: 'OpenAIModelInputModalitiesByName', toolCapabilitiesTypeName: 'OpenAIChatModelToolCapabilitiesByName', validInputModalities: ['text', 'image', 'audio', 'video'], kind: 'openai', referenceSatisfies: 'ModelMeta', referenceProviderOptionsEntry: 'OpenAIBaseOptions & OpenAIStructuredOutputOptions & OpenAIToolsOptions & OpenAIStreamingOptions & OpenAIMetadataOptions', hasBothNameAndId: false, providerOptionsIsMappedType: false, skipPatterns: [ 'gpt-3.5-', // Legacy GPT-3.5 models 'gpt-4-', // Legacy GPT-4 base models (not 4.1+) 'gpt-4o', // GPT-4o variants (4o, 4o-mini, 4o-audio, etc.) 'gpt-oss-', // Open-source/experimental models 'chatgpt-', // ChatGPT branded models ], }, 'anthropic/': { packageName: '@tanstack/ai-anthropic', metaFile: resolve(ROOT, 'packages/ai-anthropic/src/model-meta.ts'), arrayRef: '.id', contextField: 'context_window', chatArrayName: 'ANTHROPIC_MODELS', providerOptionsTypeName: 'AnthropicChatModelProviderOptionsByName', inputModalitiesTypeName: 'AnthropicModelInputModalitiesByName', toolCapabilitiesTypeName: 'AnthropicChatModelToolCapabilitiesByName', maxOutputTokensMapName: 'ANTHROPIC_MODEL_MAX_OUTPUT_TOKENS', combinedToolsAndSchemaSetName: 'ANTHROPIC_COMBINED_TOOLS_AND_SCHEMA_MODELS', validInputModalities: ['text', 'image', 'audio', 'video', 'document'], kind: 'anthropic', referenceSatisfies: 'ModelMeta', referenceProviderOptionsEntry: 'AnthropicContainerOptions & AnthropicContextManagementOptions & AnthropicMCPOptions & AnthropicServiceTierOptions & AnthropicStopSequencesOptions & AnthropicToolChoiceOptions & AnthropicSamplingOptions', hasBothNameAndId: true, providerOptionsIsMappedType: false, skipPatterns: [], }, 'google/': { packageName: '@tanstack/ai-gemini', metaFile: resolve(ROOT, 'packages/ai-gemini/src/model-meta.ts'), arrayRef: '.name', contextField: 'max_input_tokens', chatArrayName: 'GEMINI_MODELS', providerOptionsTypeName: 'GeminiChatModelProviderOptionsByName', inputModalitiesTypeName: 'GeminiModelInputModalitiesByName', toolCapabilitiesTypeName: 'GeminiChatModelToolCapabilitiesByName', validInputModalities: ['text', 'image', 'audio', 'video', 'document'], kind: 'gemini', referenceSatisfies: 'ModelMeta', referenceProviderOptionsEntry: 'GeminiToolConfigOptions & GeminiSafetyOptions & GeminiCommonConfigOptions & GeminiCachedContentOptions & GeminiStructuredOutputOptions', hasBothNameAndId: false, providerOptionsIsMappedType: false, skipPatterns: [ 'gemma-', // Gemma open-source models (not Gemini API models) ], }, 'x-ai/': { packageName: '@tanstack/ai-grok', metaFile: resolve(ROOT, 'packages/ai-grok/src/model-meta.ts'), arrayRef: '.name', contextField: 'context_window', chatArrayName: 'GROK_CHAT_MODELS', providerOptionsTypeName: 'GrokChatModelProviderOptionsByName', inputModalitiesTypeName: 'GrokModelInputModalitiesByName', toolCapabilitiesTypeName: 'GrokChatModelToolCapabilitiesByName', validInputModalities: ['text', 'image', 'audio', 'video', 'document'], kind: 'grok', referenceSatisfies: 'ModelMeta', referenceProviderOptionsEntry: 'GrokProviderOptions', hasBothNameAndId: false, providerOptionsIsMappedType: true, skipPatterns: [], }, } // --------------------------------------------------------------------------- // Utility functions // --------------------------------------------------------------------------- type InputModality = 'text' | 'image' | 'audio' | 'video' | 'document' const MODALITY_MAP: Record = { text: 'text', image: 'image', audio: 'audio', video: 'video', file: 'document', document: 'document', } /** * Map OpenRouter input modalities to our standard modality types. * Same mapping as the existing convert-openrouter-models.ts script. */ function mapInputModalities(modalities: Array): Array { const mapped = modalities .map((m) => MODALITY_MAP[m.toLowerCase()]) .filter((m): m is InputModality => m !== undefined) // Ensure at least 'text' is present if (!mapped.includes('text')) { mapped.unshift('text') } return mapped } /** Strip the provider prefix from an OpenRouter model ID */ function stripPrefix(prefix: string, modelId: string): string { return modelId.slice(prefix.length) } /** * Convert an OpenRouter price string to per-million-token pricing. * Same logic as the existing convert script. */ function convertPrice(priceStr: string | undefined): number { const price = parseFloat(priceStr ?? '0') if (isNaN(price)) return 0 const result = price * 1_000_000 return Math.round(result * 1e10) / 1e10 } function anthropicOptionsType(model: OpenRouterModel): string { return buildAnthropicProviderOptionsType({ supportedParameters: model.supported_parameters, hasCachedPricing: convertPrice(model.pricing.input_cache_read) > 0, }) } function providerOptionsEntryFor( model: OpenRouterModel, config: ProviderConfig, ): string { if (config.kind === 'anthropic') return anthropicOptionsType(model) return config.referenceProviderOptionsEntry } function satisfiesClause( model: OpenRouterModel, config: ProviderConfig, ): string { if (config.kind === 'anthropic') { return `ModelMeta<${anthropicOptionsType(model)}>` } return config.referenceSatisfies } /** * Normalize a model ID for comparison. * Handles cases where the same model uses dots vs dashes * (e.g., Anthropic's 'claude-3-5-haiku' vs OpenRouter's 'claude-3.5-haiku'). */ function normalizeId(id: string): string { return id.replace(/[.]/g, '-') } /** * Extract existing model IDs from a model-meta file. * Matches BOTH name: 'xxx' AND id: 'xxx' lines to get a complete set, * since providers like Anthropic use different naming between name and id. * Returns a normalized set for comparison purposes. */ function extractExistingModelIds(content: string): Set { const ids = new Set() // Match both name and id fields inside const blocks const nameRegex = /^\s+name:\s*'([^']+)'/gm const idRegex = /^\s+id:\s*'([^']+)'/gm let match while ((match = nameRegex.exec(content)) !== null) { ids.add(normalizeId(match[1]!)) } while ((match = idRegex.exec(content)) !== null) { ids.add(normalizeId(match[1]!)) } return ids } /** * Extract existing constant names from a model-meta file. * Matches: const UPPER_CASE_NAME = */ function extractExistingConstNames(content: string): Set { const names = new Set() const regex = /^const\s+([A-Z][A-Z0-9_]+)\s*=/gm let match while ((match = regex.exec(content)) !== null) { names.add(match[1]!) } return names } /** * Check if an OpenRouter model outputs text (for chat model array). */ function outputsText(model: OpenRouterModel): boolean { return model.architecture.output_modalities.includes('text') } /** * Check if an OpenRouter model produces image output. * * Image-generation models are skipped entirely by the sync — regardless of * whether they ALSO output text — because image model arrays require manual * curation with specialized type maps (sizes, provider options, the native * image union). This covers text+image "native" image models such as the * Gemini "Nano Banana" family (`gemini-*-image`, e.g. * `gemini-3.1-flash-lite-image`), which would otherwise be misclassified as * chat models (they output text) and inserted with a bogus `output: ['text']`. */ function outputsImage(model: OpenRouterModel): boolean { return model.architecture.output_modalities.includes('image') } /** * Non-chat model family prefixes to exclude from chat model arrays. * These are audio/music/video/image generation models that happen to * include 'text' in their output modalities but are not chat models. */ const NON_CHAT_MODEL_PREFIXES = [ 'lyria-', // Google music generation 'veo-', // Google video generation 'gemini-omni-', // Gemini Omni video (Interactions API), not chat 'imagen-', // Google image generation 'sora-', // OpenAI video generation 'dall-e-', // OpenAI image generation 'tts-', // Text-to-speech models ] function isNonChatModel(strippedId: string): boolean { return NON_CHAT_MODEL_PREFIXES.some((p) => strippedId.startsWith(p)) } /** * Check if a model should be skipped based on provider-specific patterns. */ function matchesSkipPattern( strippedId: string, patterns: Array, ): boolean { return patterns.some((p) => strippedId.startsWith(p)) } /** * Read the last sync run timestamp. Returns epoch seconds, or null if no previous run. */ async function readLastRunTimestamp(): Promise { try { const content = await readFile(LAST_RUN_FILE, 'utf-8') const ts = parseInt(content.trim(), 10) return isNaN(ts) ? null : ts } catch { return null } } /** * Write the current timestamp as the last sync run. */ async function writeLastRunTimestamp(): Promise { const now = Math.floor(Date.now() / 1000) await writeFile(LAST_RUN_FILE, String(now) + '\n', 'utf-8') } /** * Check if a model is too old to sync. Models created more than 30 days * before the last sync run are considered deprecated/legacy and skipped. */ function isModelTooOld( model: OpenRouterModel, cutoffTimestamp: number, ): boolean { if (!model.created) return false // No date = don't skip return model.created < cutoffTimestamp } // --------------------------------------------------------------------------- // Model constant generation // --------------------------------------------------------------------------- function generateModelConstant( model: OpenRouterModel, prefix: string, config: ProviderConfig, ): string { const strippedId = stripPrefix(prefix, model.id) const nativeId = toNativeProviderId(strippedId, config.kind) const constName = toModelConstName(nativeId) const inputNormal = convertPrice(model.pricing.prompt) const inputCached = convertPrice(model.pricing.input_cache_read) const outputNormal = convertPrice(model.pricing.completion) // Use actual input modalities from OpenRouter data, filtered to what this provider supports const inputModalities = mapInputModalities( model.architecture.input_modalities, ).filter((m) => config.validInputModalities.includes(m)) const lines: Array = [] lines.push(`const ${constName} = {`) // name field lines.push(` name: '${nativeId}',`) // id field (Anthropic has both name and id, set to same value for new models) if (config.hasBothNameAndId) { lines.push(` id: '${nativeId}',`) } // context / max_input_tokens if (model.context_length > 0) { lines.push( ` ${config.contextField}: ${formatNumber(model.context_length)},`, ) } // max_output_tokens if (model.top_provider.max_completion_tokens) { lines.push( ` max_output_tokens: ${formatNumber(model.top_provider.max_completion_tokens)},`, ) } lines.push(` supports: {`) lines.push( buildProviderSupportsBody({ provider: config.kind, inputModalities, supportedParameters: model.supported_parameters, reasoningMandatory: model.reasoning?.mandatory === true, }), ) lines.push(` },`) // pricing lines.push(` pricing: {`) lines.push(` input: {`) lines.push(` normal: ${inputNormal},`) if (inputCached > 0) { lines.push(` cached: ${inputCached},`) } lines.push(` },`) lines.push(` output: {`) lines.push(` normal: ${outputNormal},`) lines.push(` },`) lines.push(` },`) lines.push(`} as const satisfies ${satisfiesClause(model, config)}`) return lines.join('\n') } /** * Format a number with underscore separators for readability. * E.g. 131072 -> '131_072', 200000 -> '200_000' */ function formatNumber(n: number): string { if (n < 1000) return String(n) const str = String(n) // Insert underscores every 3 digits from the right const parts: Array = [] let remaining = str while (remaining.length > 3) { parts.unshift(remaining.slice(-3)) remaining = remaining.slice(0, -3) } parts.unshift(remaining) return parts.join('_') } // --------------------------------------------------------------------------- // Git-based change detection // --------------------------------------------------------------------------- /** * Detect packages with uncommitted changes by running `git diff`. * This captures changes from ALL prior pipeline steps (e.g. convert-openrouter-models.ts) * not just the models added by this script. */ function detectChangedPackages(): Set { const changed = new Set() try { const diff = execFileSync( 'git', ['diff', 'HEAD', '--name-only', '--', 'packages/'], { encoding: 'utf-8', cwd: ROOT }, ).trim() if (!diff) return changed for (const line of diff.split('\n')) { // packages/ai-openrouter/... → @tanstack/ai-openrouter const match = line.match(/^packages\/([\w-]+)\//) if (match) { changed.add(`@tanstack/${match[1]}`) } } } catch { // git not available (e.g. running outside a repo) — fall back to empty set } return changed } // --------------------------------------------------------------------------- // Main // --------------------------------------------------------------------------- async function main() { let totalAdded = 0 const changedPackages = new Set() // Determine age cutoff: skip models created >30 days before last run const lastRun = await readLastRunTimestamp() const now = Math.floor(Date.now() / 1000) const cutoffTimestamp = (lastRun ?? now) - MAX_MODEL_AGE_SECONDS const cutoffDate = new Date(cutoffTimestamp * 1000) .toISOString() .split('T')[0] console.log( `Model age cutoff: ${cutoffDate} (skipping models created before this date)`, ) for (const [prefix, config] of Object.entries(PROVIDER_MAP)) { console.log(`\nProcessing provider: ${prefix}`) // Filter OpenRouter models for this prefix const providerModels = models.filter((m) => m.id.startsWith(prefix)) console.log( ` Found ${providerModels.length} OpenRouter models with prefix '${prefix}'`, ) // Read the provider's model-meta.ts let content: string try { content = await readFile(config.metaFile, 'utf-8') } catch { console.warn(` Skipping: could not read ${config.metaFile}`) continue } // Extract existing model IDs (normalized) and constant names const existingIds = extractExistingModelIds(content) const existingConstNames = extractExistingConstNames(content) console.log( ` Existing models in file: ${existingIds.size} IDs, ${existingConstNames.size} constants`, ) // Find new models const newModels: Array<{ model: OpenRouterModel constName: string strippedId: string }> = [] for (const model of providerModels) { if (isRoutingAlias(model.id)) { continue } const strippedId = stripPrefix(prefix, model.id) const nativeId = toNativeProviderId(strippedId, config.kind) const constName = toModelConstName(nativeId) // Skip models with ':' variants (e.g., 'anthropic/claude-3.7-sonnet:thinking') // These are routing variants, not separate models if (strippedId.includes(':')) { continue } // Skip non-chat model families (audio/music/video/image generation) if (isNonChatModel(strippedId)) { continue } // Skip provider-specific patterns (deprecated/legacy model families) if (matchesSkipPattern(strippedId, config.skipPatterns)) { continue } // Skip models that are too old (created >30 days before last sync) if (isModelTooOld(model, cutoffTimestamp)) { continue } // Normalize for comparison to handle dots-vs-dashes naming differences if ( !existingIds.has(normalizeId(strippedId)) && !existingConstNames.has(constName) ) { newModels.push({ model, constName, strippedId: nativeId }) } } if (newModels.length === 0) { console.log(' No new models to add.') continue } console.log(` Adding ${newModels.length} new models:`) for (const { strippedId, constName } of newModels) { console.log(` - ${strippedId} (${constName})`) } // Filter out image-generation models (they need manual curation for // size/provider type maps + the native image union). This includes // text+image models like the Gemini "Nano Banana" native image family, // not just image-only models. const filteredModels = newModels.filter(({ model }) => !outputsImage(model)) const skippedImageModels = newModels.length - filteredModels.length if (skippedImageModels > 0) { console.log( ` Skipping ${skippedImageModels} image-generation models (require manual curation)`, ) } if (filteredModels.length === 0) { console.log(' No eligible models to add after filtering.') continue } // Generate constants const constants = filteredModels.map(({ model }) => generateModelConstant(model, prefix, config), ) // Insert constants before first export content = insertConstants(content, constants) // NOTE: We intentionally do NOT add models to image arrays. // Image model arrays have specialized type maps (sizes, provider options) // that require manual curation. const chatModels = filteredModels.filter(({ model }) => outputsText(model)) content = applyChatModelCatalogInserts( content, { chatArrayName: config.chatArrayName, arrayRef: config.arrayRef, providerOptionsTypeName: config.providerOptionsTypeName, inputModalitiesTypeName: config.inputModalitiesTypeName, toolCapabilitiesTypeName: config.toolCapabilitiesTypeName, maxOutputTokensMapName: config.maxOutputTokensMapName, combinedToolsAndSchemaSetName: config.combinedToolsAndSchemaSetName, providerOptionsIsMappedType: config.providerOptionsIsMappedType, }, chatModels.map(({ model, constName, strippedId }) => ({ constName, providerOptionsEntry: providerOptionsEntryFor(model, config), hasMaxOutputTokens: Boolean(model.top_provider.max_completion_tokens), acceptsCombinedToolsAndSchema: !strippedId.endsWith('-fast'), })), ) // Write the modified file await writeFile(config.metaFile, content, 'utf-8') console.log(` Wrote updated file: ${config.metaFile}`) totalAdded += filteredModels.length changedPackages.add(config.packageName) } console.log(`\nDone. Added ${totalAdded} new models total.`) // Record this run's timestamp for future age-based filtering await writeLastRunTimestamp() // Detect all packages with uncommitted changes (includes changes from // convert-openrouter-models.ts which runs before this script) const allChangedPackages = detectChangedPackages() for (const pkg of changedPackages) { allChangedPackages.add(pkg) } if (allChangedPackages.size > 0) { await createChangeset(allChangedPackages) } } /** * Create or update the sync-models changeset file. * If one already exists, merges the package lists. Otherwise creates a new one. */ async function createChangeset(changedPackages: Set) { const changesetDir = resolve(ROOT, '.changeset') const { readdir } = await import('node:fs/promises') const files = await readdir(changesetDir) const existing = files.find( (f) => f.startsWith('sync-models') && f.endsWith('.md'), ) if (existing) { const existingPath = resolve(changesetDir, existing) const existingContent = await readFile(existingPath, 'utf-8') // Merge existing packages into the set const pkgRegex = /'([^']+)':\s*patch/g let match while ((match = pkgRegex.exec(existingContent)) !== null) { changedPackages.add(match[1]!) } const content = buildChangesetContent(changedPackages) await writeFile(existingPath, content, 'utf-8') console.log(`\nChangeset updated: ${existingPath}`) } else { const changesetFile = resolve(changesetDir, 'sync-models.md') const content = buildChangesetContent(changedPackages) await writeFile(changesetFile, content, 'utf-8') console.log(`\nChangeset created: ${changesetFile}`) } console.log(` Packages: ${Array.from(changedPackages).sort().join(', ')}`) } function buildChangesetContent(packages: Set): string { const packageLines = Array.from(packages) .sort() .map((pkg) => `'${pkg}': patch`) .join('\n') return `---\n${packageLines}\n---\n\nUpdate model metadata from OpenRouter API\n` } main().catch((err) => { console.error(err) process.exit(1) })