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

model-meta.ts

549 lines15.5 KB

1/**
2 * Model metadata interface for documentation and type inference
3 */
4import type { Modality, ModelReasoning } from '@tanstack/ai'
5import type { ModelReasoningCapability } from '@tanstack/ai/adapter-internals'
6import type { GrokTextProviderOptions } from './text/text-provider-options'
7​
8// Reasoning shapes: the levels a model has for `chat({ reasoning })`, and
9// the provider value to send for each. Each model points at one of them.
10const EFFORT_LOW_TO_HIGH = {
11 levels: { low: 'low', medium: 'medium', high: 'high' },
12 budget: false,
13} as const
14const EFFORT_LOW_TO_XHIGH = {
15 levels: { low: 'low', medium: 'medium', high: 'high', xhigh: 'xhigh' },
16 budget: false,
17} as const
18const EFFORT_NONE_LOW_TO_HIGH = {
19 levels: { off: 'none', low: 'low', medium: 'medium', high: 'high' },
20 budget: false,
21} as const
22​
23interface ModelMeta {
24 name: string
25 supports: {
26 input: Array<'text' | 'image' | 'audio' | 'video' | 'document'>
27 output: Array<'text' | 'image' | 'audio' | 'video'>
28 capabilities?: Array<'reasoning' | 'tool_calling' | 'structured_outputs'>
29 tools?: ReadonlyArray<GrokProviderToolKind>
30 }
31 max_input_tokens?: number
32 max_output_tokens?: number
33 context_window?: number
34 knowledge_cutoff?: string
35 pricing?: {
36 input: {
37 normal: number
38 cached?: number
39 }
40 output: {
41 normal: number
42 }
43 }
44 /** The model's data for `chat({ reasoning })`. None: it does not reason. */
45 reasoning?: ModelReasoning
47​
48const GROK_4_5 = {
49 name: 'grok-4.5',
50 context_window: 500_000,
51 supports: {
52 input: ['text', 'image', 'document'],
53 output: ['text'],
54 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
55 tools: [],
56 },
57 pricing: {
58 input: {
59 normal: 2,
60 cached: 0.3,
61 },
62 output: {
63 normal: 6,
64 },
65 },
66 reasoning: EFFORT_LOW_TO_HIGH,
67} as const satisfies ModelMeta
68​
69const GROK_4_6 = {
70 name: 'grok-4.6',
71 context_window: 500_000,
72 supports: {
73 input: ['text', 'image', 'document'],
74 output: ['text'],
75 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
76 tools: [],
77 },
78 pricing: {
79 input: {
80 normal: 2,
81 cached: 0.5,
82 },
83 output: {
84 normal: 6,
85 },
86 },
87 reasoning: EFFORT_LOW_TO_XHIGH,
88} as const satisfies ModelMeta
89​
90const GROK_4_7 = {
91 name: 'grok-4.7',
92 context_window: 500_000,
93 max_output_tokens: 450_000,
94 supports: {
95 input: ['text', 'image', 'document'],
96 output: ['text'],
97 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
98 tools: [],
99 },
100 pricing: {
101 input: {
102 normal: 1.6,
103 cached: 0.4,
104 },
105 output: {
106 normal: 4.8,
107 },
108 },
109 reasoning: EFFORT_LOW_TO_XHIGH,
110} as const satisfies ModelMeta
111​
112export type GrokProviderToolKind =
113 | 'web_search'
114 | 'x_search'
115 | 'file_search'
116 | 'mcp'
117​
118const GROK_RESPONSES_TOOLS = [
119 'web_search',
120 'x_search',
121 'file_search',
122 'mcp',
123] as const satisfies ReadonlyArray<GrokProviderToolKind>
124​
125// Imagine API image models. Pricing is per generated image (output only).
126const GROK_IMAGINE_IMAGE = {
127 name: 'grok-imagine-image',
128 supports: {
129 input: ['text', 'image'],
130 output: ['image'],
131 },
132 pricing: {
133 input: {
134 normal: 0,
135 },
136 output: {
137 normal: 0.02,
138 },
139 },
140} as const satisfies ModelMeta
141​
142const GROK_IMAGINE_IMAGE_QUALITY = {
143 name: 'grok-imagine-image-quality',
144 supports: {
145 input: ['text', 'image'],
146 output: ['image'],
147 },
148 pricing: {
149 input: {
150 normal: 0,
151 },
152 output: {
153 normal: 0.05,
154 },
155 },
156} as const satisfies ModelMeta
157​
158// xAI's recommended Imagine image model. Supports the 2.0-only `quality`
159// provider option ('low' | 'medium', default 'medium').
160const GROK_IMAGINE_IMAGE_2_0 = {
161 name: 'grok-imagine-image-2.0',
162 supports: {
163 input: ['text', 'image'],
164 output: ['image'],
165 },
166 pricing: {
167 input: {
168 normal: 0,
169 },
170 output: {
171 normal: 0.04,
172 },
173 },
174} as const satisfies ModelMeta
175​
176// Imagine API video models. Pricing is per second of generated video
177// (output only); generated videos carry an audio track.
178//
179// Both models support text-to-video and image-to-video (a starting-frame
180// image is optional). grok-imagine-video-1.5 is the documented default: it
181// adds native 1080p for text-to-video / image-to-video plus
182// reference-to-video (`reference_images` / `reference_audios`; reference
183// output is capped at 720p). Source-video edit (`/v1/videos/edits`) and
184// extend (`/v1/videos/extensions`) are grok-imagine-video only — xAI's
185// 1.5 model page lists text+image input, not video.
186const GROK_IMAGINE_VIDEO = {
187 name: 'grok-imagine-video',
188 supports: {
189 input: ['text', 'image', 'video'],
190 output: ['video', 'audio'],
191 },
192 pricing: {
193 input: {
194 normal: 0,
195 },
196 output: {
197 // per second of video
198 normal: 0.05,
199 },
200 },
201} as const satisfies ModelMeta
202​
203const GROK_IMAGINE_VIDEO_1_5 = {
204 name: 'grok-imagine-video-1.5',
205 supports: {
206 input: ['text', 'image'],
207 output: ['video', 'audio'],
208 },
209 pricing: {
210 input: {
211 normal: 0,
212 },
213 output: {
214 // per second of video
215 normal: 0.08,
216 },
217 },
218} as const satisfies ModelMeta
219​
220const GROK_4_3 = {
221 name: 'grok-4.3',
222 context_window: 1_000_000,
223 supports: {
224 input: ['text', 'image'],
225 output: ['text'],
226 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
227 tools: GROK_RESPONSES_TOOLS,
228 },
229 pricing: {
230 input: {
231 normal: 1.25,
232 cached: 0.2,
233 },
234 output: {
235 normal: 2.5,
236 },
237 },
238 reasoning: EFFORT_NONE_LOW_TO_HIGH,
239} as const satisfies ModelMeta
240​
241const GROK_BUILD_0_1 = {
242 name: 'grok-build-0.1',
243 context_window: 256_000,
244 supports: {
245 input: ['text', 'image'],
246 output: ['text'],
247 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
248 tools: GROK_RESPONSES_TOOLS,
249 },
250 pricing: {
251 input: {
252 normal: 1,
253 cached: 0.2,
254 },
255 output: {
256 normal: 2,
257 },
258 },
259} as const satisfies ModelMeta
260​
261// Vertex Model Garden IDs. These are not the xAI API catalog.
262// Source: https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/partner-models/grok
263const GROK_4_20_REASONING = {
264 name: 'grok-4.20-reasoning',
265 context_window: 1_000_000,
266 supports: {
267 input: ['text', 'image'],
268 output: ['text'],
269 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
270 tools: [] as const,
271 },
272} as const satisfies ModelMeta
273​
274const GROK_4_20_NON_REASONING = {
275 name: 'grok-4.20-non-reasoning',
276 context_window: 1_000_000,
277 supports: {
278 input: ['text', 'image'],
279 output: ['text'],
280 capabilities: ['structured_outputs', 'tool_calling'],
281 tools: [] as const,
282 },
283} as const satisfies ModelMeta
284​
285const GROK_4_1_FAST_REASONING = {
286 name: 'grok-4.1-fast-reasoning',
287 context_window: 2_000_000,
288 supports: {
289 input: ['text', 'image'],
290 output: ['text'],
291 capabilities: ['reasoning', 'structured_outputs', 'tool_calling'],
292 tools: [] as const,
293 },
294} as const satisfies ModelMeta
295​
296const GROK_4_1_FAST_NON_REASONING = {
297 name: 'grok-4.1-fast-non-reasoning',
298 context_window: 2_000_000,
299 supports: {
300 input: ['text', 'image'],
301 output: ['text'],
302 capabilities: ['structured_outputs', 'tool_calling'],
303 tools: [] as const,
304 },
305} as const satisfies ModelMeta
306​
307/**
308 * Grok chat models supported by the xAI Responses adapter.
309 */
310export const GROK_CHAT_MODELS = [
311 GROK_4_7.name,
312 GROK_4_5.name,
313 GROK_4_6.name,
314 GROK_BUILD_0_1.name,
315 GROK_4_3.name,
316] as const
317​
318/**
319 * Grok chat models on Vertex AI / Gemini Enterprise Agent Platform.
320 * This list is the Google partner catalog, not the xAI API catalog.
321 */
322export const GROK_VERTEX_CHAT_MODELS = [
323 GROK_4_3.name,
324 GROK_4_20_REASONING.name,
325 GROK_4_20_NON_REASONING.name,
326 GROK_4_1_FAST_REASONING.name,
327 GROK_4_1_FAST_NON_REASONING.name,
328] as const
329​
330/**
331 * Grok Image Generation Models
332 */
333export const GROK_IMAGE_MODELS = [
334 GROK_IMAGINE_IMAGE.name,
335 GROK_IMAGINE_IMAGE_2_0.name,
336 GROK_IMAGINE_IMAGE_QUALITY.name,
337] as const
338​
339/**
340 * Grok Video Generation Models (xAI Imagine API)
341 *
342 * @experimental Video generation is an experimental feature and may change.
343 */
344export const GROK_VIDEO_MODELS = [
345 GROK_IMAGINE_VIDEO.name,
346 GROK_IMAGINE_VIDEO_1_5.name,
347] as const
348​
349// xAI's `/v1/tts` endpoint is endpoint-addressed and does not take a `model`
350// parameter. This synthetic identifier satisfies the SDK's `TTSOptions.model`
351// contract and provides a stable value for logging and fixture matching.
352const GROK_TTS = {
353 name: 'grok-tts',
354 supports: {
355 input: ['text'],
356 output: ['audio'],
357 },
358} as const satisfies ModelMeta
359​
360// xAI's `/v1/stt` endpoint is endpoint-addressed and does not take a `model`
361// parameter. This synthetic identifier satisfies the SDK's
362// `TranscriptionOptions.model` contract.
363const GROK_STT = {
364 name: 'grok-stt',
365 supports: {
366 input: ['audio'],
367 output: ['text'],
368 },
369} as const satisfies ModelMeta
370​
371const GROK_VOICE_FAST_1 = {
372 name: 'grok-voice-fast-1.0',
373 supports: {
374 input: ['audio', 'text'],
375 output: ['audio', 'text'],
376 capabilities: ['tool_calling'],
377 tools: [] as const,
378 },
379} as const satisfies ModelMeta
380​
381// xAI's current recommended speech-to-speech model.
382const GROK_VOICE_THINK_FAST_2 = {
383 name: 'grok-voice-think-fast-2.0',
384 supports: {
385 input: ['audio', 'text'],
386 output: ['audio', 'text'],
387 capabilities: ['reasoning', 'tool_calling'],
388 tools: [] as const,
389 },
390} as const satisfies ModelMeta
391​
392// Rolling alias used by xAI's realtime docs examples; always points at the
393// latest speech-to-speech model.
394const GROK_VOICE_LATEST = {
395 name: 'grok-voice-latest',
396 supports: {
397 input: ['audio', 'text'],
398 output: ['audio', 'text'],
399 capabilities: ['reasoning', 'tool_calling'],
400 tools: [] as const,
401 },
402} as const satisfies ModelMeta
403​
404export const GROK_TTS_MODELS = [GROK_TTS.name] as const
405​
406export const GROK_TRANSCRIPTION_MODELS = [GROK_STT.name] as const
407​
408export const GROK_REALTIME_MODELS = [
409 GROK_VOICE_THINK_FAST_2.name,
410 GROK_VOICE_LATEST.name,
411 GROK_VOICE_FAST_1.name,
412] as const
413​
414/**
415 * Default speech-to-speech model used by the realtime token issuer and the
416 * realtime client adapter when no model is specified. Single source of truth
417 * so a future default bump cannot leave the two sides disagreeing.
418 */
419export const GROK_DEFAULT_REALTIME_MODEL: GrokRealtimeModel =
420 'grok-voice-think-fast-2.0'
421​
422export type GrokChatModel = (typeof GROK_CHAT_MODELS)[number]
423export type GrokVertexChatModel = (typeof GROK_VERTEX_CHAT_MODELS)[number]
424export type GrokTextAdapterModel = GrokChatModel | GrokVertexChatModel
425export type GrokImageModel = (typeof GROK_IMAGE_MODELS)[number]
426export type GrokVideoModel = (typeof GROK_VIDEO_MODELS)[number]
427export type GrokTTSModel = (typeof GROK_TTS_MODELS)[number]
428export type GrokTranscriptionModel = (typeof GROK_TRANSCRIPTION_MODELS)[number]
429export type GrokRealtimeModel = (typeof GROK_REALTIME_MODELS)[number]
430​
431/**
432 * Type-only map from Grok chat model name to its supported input modalities.
433 * Used for type inference when constructing multimodal messages.
434 */
435export type GrokModelInputModalitiesByName = {
436 [GROK_4_3.name]: typeof GROK_4_3.supports.input
437 [GROK_BUILD_0_1.name]: typeof GROK_BUILD_0_1.supports.input
438 [GROK_4_5.name]: typeof GROK_4_5.supports.input
439 [GROK_4_6.name]: typeof GROK_4_6.supports.input
440 [GROK_4_20_REASONING.name]: typeof GROK_4_20_REASONING.supports.input
441 [GROK_4_20_NON_REASONING.name]: typeof GROK_4_20_NON_REASONING.supports.input
442 [GROK_4_1_FAST_REASONING.name]: typeof GROK_4_1_FAST_REASONING.supports.input
443 [GROK_4_1_FAST_NON_REASONING.name]: typeof GROK_4_1_FAST_NON_REASONING.supports.input
444 [GROK_4_7.name]: typeof GROK_4_7.supports.input
446​
447/**
448 * Runtime map from chat model name to its supported input modalities, for the
449 * text adapter's `inputModalities`. `satisfies` keeps it equal to
450 * {@link GrokModelInputModalitiesByName}. An unknown name gives `undefined`.
451 */
452export const GROK_MODEL_INPUT_MODALITIES: Readonly<
453 Record<string, ReadonlyArray<Modality>>
454> = {
455 [GROK_4_3.name]: GROK_4_3.supports.input,
456 [GROK_BUILD_0_1.name]: GROK_BUILD_0_1.supports.input,
457 [GROK_4_5.name]: GROK_4_5.supports.input,
458 [GROK_4_6.name]: GROK_4_6.supports.input,
459 [GROK_4_20_REASONING.name]: GROK_4_20_REASONING.supports.input,
460 [GROK_4_20_NON_REASONING.name]: GROK_4_20_NON_REASONING.supports.input,
461 [GROK_4_1_FAST_REASONING.name]: GROK_4_1_FAST_REASONING.supports.input,
462 [GROK_4_1_FAST_NON_REASONING.name]:
463 GROK_4_1_FAST_NON_REASONING.supports.input,
464 [GROK_4_7.name]: GROK_4_7.supports.input,
465} satisfies GrokModelInputModalitiesByName
466​
467/**
468 * Type-only map from Grok chat model name to its supported provider tools.
469 * Keeps Grok provider-tool factories type-checked against the models that
470 * advertise xAI Responses server-side tools.
471 */
472export type GrokChatModelToolCapabilitiesByName = {
473 [GROK_4_3.name]: typeof GROK_4_3.supports.tools
474 [GROK_BUILD_0_1.name]: typeof GROK_BUILD_0_1.supports.tools
475 [GROK_4_20_REASONING.name]: typeof GROK_4_20_REASONING.supports.tools
476 [GROK_4_20_NON_REASONING.name]: typeof GROK_4_20_NON_REASONING.supports.tools
477 [GROK_4_1_FAST_REASONING.name]: typeof GROK_4_1_FAST_REASONING.supports.tools
478 [GROK_4_1_FAST_NON_REASONING.name]: typeof GROK_4_1_FAST_NON_REASONING.supports.tools
479 [GROK_4_7.name]: typeof GROK_4_7.supports.tools
481​
482export type GrokProviderOptions = GrokTextProviderOptions
483​
484/**
485 * Type-only map from Grok chat model name to its provider options type.
486 */
487export type GrokChatModelProviderOptionsByName = {
488 [GROK_4_3.name]: GrokProviderOptions
489 [GROK_BUILD_0_1.name]: GrokProviderOptions
490 [GROK_4_20_REASONING.name]: GrokProviderOptions
491 [GROK_4_20_NON_REASONING.name]: GrokProviderOptions
492 [GROK_4_1_FAST_REASONING.name]: GrokProviderOptions
493 [GROK_4_1_FAST_NON_REASONING.name]: GrokProviderOptions
495​
496// ===========================
497// Type Resolution Helpers
498// ===========================
499​
500/**
501 * Resolve provider options for a specific model.
502 * If the model has explicit options in the map, use those; otherwise use base options.
503 */
504export type ResolveProviderOptions<TModel extends string> =
505 TModel extends keyof GrokChatModelProviderOptionsByName
506 ? GrokChatModelProviderOptionsByName[TModel]
507 : GrokProviderOptions
508​
509/**
510 * Resolve input modalities for a specific model.
511 * If the model has explicit modalities in the map, use those; otherwise use text only.
512 */
513export type ResolveInputModalities<TModel extends string> =
514 TModel extends keyof GrokModelInputModalitiesByName
515 ? GrokModelInputModalitiesByName[TModel]
516 : readonly ['text']
517​
518// Reasoning
519//
520// Each model's `reasoning` field holds its data for `chat({ reasoning })`: the
521// levels it has, with the provider value for each, and whether it takes a
522// thinking token budget. The data comes from models.dev. The generator that
523// writes it comes with the model catalog later. Until then, the sync scripts
524// keep these fields as they are.
525​
526const GROK_REASONING = {
527 [GROK_4_3.name]: GROK_4_3.reasoning,
528 [GROK_4_5.name]: GROK_4_5.reasoning,
529 [GROK_4_6.name]: GROK_4_6.reasoning,
530 [GROK_4_7.name]: GROK_4_7.reasoning,
532​
533/**
534 * Each chat model's reasoning levels, and whether it takes a token budget,
535 * for `chat({ reasoning })`. Derived from the `reasoning` field. A model
536 * that is not here does not reason.
537 */
538export type GrokModelReasoningByName = {
539 [K in keyof typeof GROK_REASONING]: ModelReasoningCapability<
540 (typeof GROK_REASONING)[K]
541 >
543​
544/**
545 * Runtime map from chat model name to its reasoning data, for the text adapter.
546 * An unknown name gives `undefined`: the adapter sends no reasoning field.
547 */
548export const GROK_MODEL_REASONING: Readonly<Record<string, ModelReasoning>> =
549 GROK_REASONING