Open History to see the last commit
1/**
2 * Map one LLM Gateway `/v1/models` row into the fields `model-meta.ts` needs.
3 * modelschemas has no `llmgateway` provider, so this catalog is the source.
4 */
5
6import type { ReasoningShape } from './reasoning-shapes.ts'
7
8export type LlmGatewayInput = 'text' | 'image' | 'audio' | 'video' | 'document'
9
10export type LlmGatewayFeature =
11 | 'streaming'
12 | 'tools'
13 | 'json_object'
14 | 'json_schema'
15 | 'reasoning'
16 | 'vision'
17
18const INPUT_ORDER: Array<LlmGatewayInput> = [
19 'text',
20 'image',
21 'document',
22 'audio',
23 'video',
24]
25
26const INPUT_ALIASES: Record<string, LlmGatewayInput> = {
27 text: 'text',
28 image: 'image',
29 audio: 'audio',
30 video: 'video',
31 pdf: 'document',
32 file: 'document',
33 document: 'document',
34}
35
36/** `none` is the gateway's off switch. The SDK level is `off`. */
37const EFFORT_LEVELS: Record<string, keyof ReasoningShape['levels']> = {
38 none: 'off',
39 minimal: 'minimal',
40 low: 'low',
41 medium: 'medium',
42 high: 'high',
43 xhigh: 'xhigh',
44 max: 'max',
45}
46
47const FEATURE_ORDER: Array<LlmGatewayFeature> = [
48 'streaming',
49 'tools',
50 'json_object',
51 'json_schema',
52 'reasoning',
53 'vision',
54]
55
56export interface LlmGatewayCatalogRow {
57 id?: unknown
58 architecture?: {
59 input_modalities?: unknown
60 output_modalities?: unknown
61 }
62 pricing?: Record<string, unknown>
63 context_length?: unknown
64 max_output?: unknown
65 supported_parameters?: unknown
66 json_output?: unknown
67 structured_outputs?: unknown
68 providers?: unknown
69}
70
71export interface LlmGatewayModelMeta {
72 id: string
73 contextWindow?: number
74 maxCompletionTokens?: number
75 pricing: {
76 input?: { normal: number; cached?: number }
77 output?: { normal: number }
78 }
79 input: Array<LlmGatewayInput>
80 features: Array<LlmGatewayFeature>
81 reasoning?: ReasoningShape
82}
83
84interface ProviderRow {
85 tools?: unknown
86 reasoning?: unknown
87 reasoning_efforts?: unknown
88 vision?: unknown
89 streaming?: unknown
90 max_output?: unknown
91}
92
93function stringList(value: unknown): Array<string> {
94 if (!Array.isArray(value)) return []
95 return value.filter((item): item is string => typeof item === 'string')
96}
97
98function providersOf(row: LlmGatewayCatalogRow): Array<ProviderRow> {
99 if (!Array.isArray(row.providers)) return []
100 return row.providers.filter(
101 (provider): provider is ProviderRow =>
102 typeof provider === 'object' && provider !== null,
103 )
104}
105
106function positiveInt(value: unknown): number | undefined {
107 const n = typeof value === 'number' ? value : Number(value)
108 if (!Number.isInteger(n) || n <= 0) return undefined
109 return n
110}
111
112/** Catalog prices are USD per token. Model meta stores USD per million. */
113export function pricePerMillion(value: unknown): number | undefined {
114 if (typeof value !== 'string' && typeof value !== 'number') return undefined
115 const perToken = typeof value === 'number' ? value : Number(value)
116 if (!Number.isFinite(perToken) || perToken <= 0) return undefined
117 return Math.round(perToken * 1e6 * 1e6) / 1e6
118}
119
120function inputOf(row: LlmGatewayCatalogRow): Array<LlmGatewayInput> {
121 const mapped = stringList(row.architecture?.input_modalities)
122 .map((modality) => INPUT_ALIASES[modality.toLowerCase()])
123 .filter((modality): modality is LlmGatewayInput => modality !== undefined)
124 if (!mapped.includes('text')) mapped.unshift('text')
125 return INPUT_ORDER.filter((modality) => mapped.includes(modality))
126}
127
128function reasoningOf(
129 providers: Array<ProviderRow>,
130): ReasoningShape | undefined {
131 const efforts = providers
132 .map((provider) => stringList(provider.reasoning_efforts))
133 .find((list) => list.length > 0)
134 if (!efforts) return undefined
135 const levels: ReasoningShape['levels'] = {}
136 for (const effort of efforts) {
137 const level = EFFORT_LEVELS[effort.toLowerCase()]
138 if (!level) continue
139 levels[level] = effort.toLowerCase() === 'none' ? 'none' : level
140 }
141 if (Object.keys(levels).length === 0) return undefined
142 return { levels, budget: false }
143}
144
145/**
146 * `null` when the row is not a chat model we emit: `custom`, a `provider/`
147 * id, or a model whose output is not text.
148 */
149export function catalogRowToMeta(
150 row: LlmGatewayCatalogRow,
151): LlmGatewayModelMeta | null {
152 if (typeof row.id !== 'string' || row.id.length === 0) return null
153 if (row.id === 'custom' || row.id.includes('/')) return null
154
155 const outputs = stringList(row.architecture?.output_modalities)
156 if (outputs.length > 0 && !outputs.includes('text')) return null
157
158 const providers = providersOf(row)
159 const params = new Set(stringList(row.supported_parameters))
160 const input = inputOf(row)
161 const reasoning = reasoningOf(providers)
162 const flags = new Set<LlmGatewayFeature>()
163 if (providers.some((provider) => provider.streaming === true)) {
164 flags.add('streaming')
165 }
166 if (
167 providers.some((provider) => provider.tools === true) ||
168 params.has('tools')
169 ) {
170 flags.add('tools')
171 }
172 if (row.json_output === true) flags.add('json_object')
173 if (row.structured_outputs === true) flags.add('json_schema')
174 if (reasoning || providers.some((provider) => provider.reasoning === true)) {
175 flags.add('reasoning')
176 }
177 if (
178 providers.some((provider) => provider.vision === true) ||
179 input.includes('image')
180 ) {
181 flags.add('vision')
182 }
183
184 const normal = pricePerMillion(row.pricing?.prompt)
185 const cached = pricePerMillion(row.pricing?.input_cache_read)
186 const output = pricePerMillion(row.pricing?.completion)
187 const maxFromProvider = providers
188 .map((provider) => positiveInt(provider.max_output))
189 .find((value) => value !== undefined)
190
191 return {
192 id: row.id,
193 contextWindow: positiveInt(row.context_length),
194 maxCompletionTokens: positiveInt(row.max_output) ?? maxFromProvider,
195 pricing: {
196 input:
197 normal === undefined
198 ? undefined
199 : { normal, ...(cached === undefined ? {} : { cached }) },
200 output: output === undefined ? undefined : { normal: output },
201 },
202 input,
203 features: FEATURE_ORDER.filter((feature) => flags.has(feature)),
204 reasoning,
205 }
206}