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

convert-llmgateway-models.ts

159 lines5.0 KB

1/**
2 * Converts LLM Gateway catalog rows into packages/ai-llmgateway/src/model-meta.ts.
3 *
4 * Usage:
5 * node scripts/convert-llmgateway-models.ts
6 */
7​
8import { writeFile } from 'node:fs/promises'
9import { dirname, resolve } from 'node:path'
10import { fileURLToPath } from 'node:url'
11import { catalogRowToMeta } from './model-sync/llmgateway.ts'
12import {
13 reasoningShapeName,
14 reasoningShapeTable,
15} from './model-sync/reasoning-shapes.ts'
16import { models } from './llmgateway.models.ts'
17import type { LlmGatewayModelMeta } from './model-sync/llmgateway'
18import type { ReasoningShape } from './model-sync/reasoning-shapes'
19​
20const __dirname = dirname(fileURLToPath(import.meta.url))
21const OUTPUT_PATH = resolve(
22 __dirname,
23 '../packages/ai-llmgateway/src/model-meta.ts',
25​
26function formatStringArray(ids: Array<string>): string {
27 return `[\n${ids.map((id) => ` ${JSON.stringify(id)},`).join('\n')}\n] as const`
29​
30function formatModalitiesType(input: LlmGatewayModelMeta['input']): string {
31 return `readonly [${input.map((modality) => `'${modality}'`).join(', ')}]`
33​
34function formatModalitiesValue(input: LlmGatewayModelMeta['input']): string {
35 return `[${input.map((modality) => `'${modality}'`).join(', ')}] as const`
37​
38function convertModels(rows: Array<LlmGatewayModelMeta>): string {
39 const modalityEntries = rows
40 .map(
41 (row) =>
42 ` ${JSON.stringify(row.id)}: ${formatModalitiesType(row.input)}`,
43 )
44 .join('\n')
45​
46 const shapes: Array<ReasoningShape> = []
47 const reasoningEntries: Array<string> = []
48 for (const row of rows) {
49 if (!row.reasoning) continue
50 shapes.push(row.reasoning)
51 reasoningEntries.push(
52 ` ${JSON.stringify(row.id)}: ${reasoningShapeName(row.reasoning)},`,
53 )
54 }
55 const reasoningBlock =
56 reasoningEntries.length === 0
57 ? ''
58 : `
59${reasoningShapeTable(shapes).code}
60​
61const LLMGATEWAY_REASONING = {
62${reasoningEntries.join('\n')}
63} satisfies Partial<Record<LLMGatewayChatModels, ModelReasoning>>
64​
65/**
66 * Each chat model's reasoning levels for \`chat({ reasoning })\`.
67 * A model that is not here does not reason. No model takes a token budget.
68 */
69export type LLMGatewayModelReasoningByName = {
70 [K in keyof typeof LLMGATEWAY_REASONING]: ModelReasoningCapability<
71 (typeof LLMGATEWAY_REASONING)[K]
72 >
74​
75/**
76 * Runtime map from chat model name to its reasoning data.
77 * An unknown name gives \`undefined\`: the adapter sends no reasoning field.
78 */
79export const LLMGATEWAY_MODEL_REASONING: Readonly<
80 Record<string, ModelReasoning>
81> = LLMGATEWAY_REASONING
83​
84 return `/**
85 * This file is generated by scripts/convert-llmgateway-models.ts. Do not edit by hand.
86 *
87 * Source: https://api.llmgateway.io/v1/models (modelschemas has no llmgateway provider).
88 * Unprefixed ids only. A \`provider/\` prefix still works at runtime.
89 */
90​
91import type { Modality, ModelReasoning } from '@tanstack/ai'
92import type { ModelReasoningCapability } from '@tanstack/ai/adapter-internals'
93import type { LLMGatewayTextProviderOptions } from './text/text-provider-options'
94​
95export const LLMGATEWAY_CHAT_MODELS = ${formatStringArray(rows.map((row) => row.id))}
96​
97export type LLMGatewayChatModels = (typeof LLMGATEWAY_CHAT_MODELS)[number]
98​
99/**
100 * A catalog model name, or any other id. A \`provider/\` prefix pins routing.
101 * An id outside the catalog gets text-only input and the generic options.
102 */
103export type LLMGatewayModelId = LLMGatewayChatModels | (string & {})
104​
105export type LLMGatewayModelInputModalitiesByName = {
106${modalityEntries}
108​
109export const LLMGATEWAY_MODEL_INPUT_MODALITIES: Readonly<
110 Record<string, ReadonlyArray<Modality>>
111> = {
112${rows
113 .map(
114 (row) =>
115 ` ${JSON.stringify(row.id)}: ${formatModalitiesValue(row.input)},`,
116 )
117 .join('\n')}
118} satisfies LLMGatewayModelInputModalitiesByName
119​
120export type LLMGatewayChatModelProviderOptionsByName = {
121 [K in LLMGatewayChatModels]: LLMGatewayTextProviderOptions
123​
124/** LLM Gateway exposes no provider-specific tool factories. */
125export type LLMGatewayChatModelToolCapabilitiesByName = {
126 [K in LLMGatewayChatModels]: readonly []
128​
129export type ResolveProviderOptions<TModel extends string> =
130 TModel extends keyof LLMGatewayChatModelProviderOptionsByName
131 ? LLMGatewayChatModelProviderOptionsByName[TModel]
132 : LLMGatewayTextProviderOptions
133​
134export type ResolveInputModalities<TModel extends string> =
135 TModel extends keyof LLMGatewayModelInputModalitiesByName
136 ? LLMGatewayModelInputModalitiesByName[TModel]
137 : readonly ['text']
138${reasoningBlock}`
140​
141async function main() {
142 const rows = models
143 .map((model) => catalogRowToMeta(model))
144 .filter((model): model is LlmGatewayModelMeta => model !== null)
145 .sort((a, b) => a.id.localeCompare(b.id))
146 if (rows.length === 0) {
147 throw new Error(
148 'LLM Gateway catalog produced no chat models. Refusing to overwrite model-meta.ts.',
149 )
150 }
151 console.log(`Writing ${rows.length} LLM Gateway chat models`)
152 await writeFile(OUTPUT_PATH, convertModels(rows), 'utf-8')
153 console.log(`Model meta file written to ${OUTPUT_PATH}`)
155​
156main().catch((error: unknown) => {
157 console.error(error)
158 process.exit(1)
159})