@tanstack/ai-grok 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Grok Video Generation Provider Options (xAI Imagine API)
3
+ *
4
+ * Based on https://docs.x.ai/docs/guides/video-generations
5
+ *
6
+ * @experimental Video generation is an experimental feature and may change.
7
+ */
8
+
9
+ import type { DurationOptions } from '@tanstack/ai/adapters'
10
+ import type { GrokVideoModel } from '../model-meta'
11
+
12
+ /**
13
+ * Aspect ratios accepted by the grok-imagine video models.
14
+ *
15
+ * Note: this is a narrower set than the grok-imagine image models — the
16
+ * video endpoint rejects the phone-screen ratios ('9:19.5', '9:20', …) and
17
+ * 'auto'.
18
+ *
19
+ * @experimental Video generation is an experimental feature and may change.
20
+ */
21
+ export type GrokVideoAspectRatio =
22
+ | '1:1'
23
+ | '16:9'
24
+ | '9:16'
25
+ | '4:3'
26
+ | '3:4'
27
+ | '3:2'
28
+ | '2:3'
29
+
30
+ /**
31
+ * Resolution tiers for the grok-imagine video models.
32
+ *
33
+ * @experimental Video generation is an experimental feature and may change.
34
+ */
35
+ export type GrokVideoResolution = '480p' | '720p' | '1080p'
36
+
37
+ /**
38
+ * Size strings for grok-imagine video models. The Imagine API is
39
+ * aspect-ratio based rather than pixel-size based; like the grok-imagine
40
+ * image models, the generic `size` option uses an
41
+ * `aspectRatio_resolution` template ("16:9_720p") — the resolution suffix
42
+ * is optional ("16:9" uses the API default).
43
+ *
44
+ * @experimental Video generation is an experimental feature and may change.
45
+ */
46
+ export type GrokVideoSize =
47
+ | GrokVideoAspectRatio
48
+ | `${GrokVideoAspectRatio}_${GrokVideoResolution}`
49
+
50
+ const GROK_VIDEO_ASPECT_RATIOS: ReadonlyArray<string> = [
51
+ '1:1',
52
+ '16:9',
53
+ '9:16',
54
+ '4:3',
55
+ '3:4',
56
+ '3:2',
57
+ '2:3',
58
+ ]
59
+
60
+ const GROK_VIDEO_RESOLUTIONS: ReadonlyArray<string> = ['480p', '720p', '1080p']
61
+
62
+ /**
63
+ * Video duration limits enforced by the Imagine API (seconds).
64
+ */
65
+ export const GROK_VIDEO_MIN_DURATION = 1
66
+ export const GROK_VIDEO_MAX_DURATION = 15
67
+
68
+ /**
69
+ * Parses a grok video size string into its components.
70
+ * Format: "aspectRatio" or "aspectRatio_resolution",
71
+ * e.g. "16:9_720p" → { aspectRatio: "16:9", resolution: "720p" }.
72
+ * Returns undefined when the string doesn't match the template.
73
+ */
74
+ export function parseGrokVideoSize(
75
+ size: string,
76
+ ): { aspectRatio: string; resolution?: string } | undefined {
77
+ const match = size.match(/^([\d.]+:[\d.]+)(?:_(.+))?$/)
78
+ const [, aspectRatio, resolution] = match ?? []
79
+ if (aspectRatio === undefined) return undefined
80
+ return { aspectRatio, ...(resolution !== undefined && { resolution }) }
81
+ }
82
+
83
+ /**
84
+ * Validate the `size` template for a given grok video model.
85
+ *
86
+ * @experimental Video generation is an experimental feature and may change.
87
+ */
88
+ export function validateVideoSize(
89
+ model: string,
90
+ size?: string,
91
+ ): asserts size is GrokVideoSize | undefined {
92
+ if (size === undefined) return
93
+ const parsed = parseGrokVideoSize(size)
94
+ if (!parsed || !GROK_VIDEO_ASPECT_RATIOS.includes(parsed.aspectRatio)) {
95
+ throw new Error(
96
+ `Size "${size}" is not supported by model "${model}". Expected ` +
97
+ `"aspectRatio" or "aspectRatio_resolution" (e.g. "16:9_720p") with ` +
98
+ `aspect ratio one of: ${GROK_VIDEO_ASPECT_RATIOS.join(', ')}`,
99
+ )
100
+ }
101
+ if (
102
+ parsed.resolution !== undefined &&
103
+ !GROK_VIDEO_RESOLUTIONS.includes(parsed.resolution)
104
+ ) {
105
+ throw new Error(
106
+ `Resolution "${parsed.resolution}" is not supported by model "${model}". ` +
107
+ `Supported resolutions: ${GROK_VIDEO_RESOLUTIONS.join(', ')}`,
108
+ )
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Per-model duration type. The Imagine API accepts any integer second in the
114
+ * 1–15 range, so this is a continuous range expressed as `number` (a literal
115
+ * union can't represent it). `snapDuration()` coerces a raw seconds value into
116
+ * the valid range at runtime.
117
+ *
118
+ * @experimental Video generation is an experimental feature and may change.
119
+ */
120
+ export type GrokVideoModelDurationByName = {
121
+ 'grok-imagine-video': number
122
+ 'grok-imagine-video-1.5': number
123
+ }
124
+
125
+ /**
126
+ * Runtime duration table backing `availableDurations()` / `snapDuration()`.
127
+ * Both grok-imagine video models accept the same continuous 1–15 integer-second
128
+ * range.
129
+ *
130
+ * @experimental Video generation is an experimental feature and may change.
131
+ */
132
+ export const GROK_VIDEO_DURATIONS: {
133
+ readonly [TModel in GrokVideoModel]: DurationOptions<
134
+ GrokVideoModelDurationByName[TModel]
135
+ >
136
+ } = {
137
+ 'grok-imagine-video': {
138
+ kind: 'range',
139
+ min: GROK_VIDEO_MIN_DURATION,
140
+ max: GROK_VIDEO_MAX_DURATION,
141
+ step: 1,
142
+ unit: 'seconds',
143
+ },
144
+ 'grok-imagine-video-1.5': {
145
+ kind: 'range',
146
+ min: GROK_VIDEO_MIN_DURATION,
147
+ max: GROK_VIDEO_MAX_DURATION,
148
+ step: 1,
149
+ unit: 'seconds',
150
+ },
151
+ }
152
+
153
+ /**
154
+ * Look up the duration options for a grok video model.
155
+ *
156
+ * @experimental Video generation is an experimental feature and may change.
157
+ */
158
+ export function getGrokVideoDurationOptions<TModel extends GrokVideoModel>(
159
+ model: TModel,
160
+ ): DurationOptions<GrokVideoModelDurationByName[TModel]> {
161
+ return GROK_VIDEO_DURATIONS[model]
162
+ }
163
+
164
+ /**
165
+ * Provider-specific options for grok video generation. These map directly
166
+ * onto the Imagine API request body and take precedence over the generic
167
+ * `size` / `duration` options when both are provided.
168
+ *
169
+ * @experimental Video generation is an experimental feature and may change.
170
+ */
171
+ export interface GrokVideoProviderOptions {
172
+ /**
173
+ * Output aspect ratio.
174
+ */
175
+ aspect_ratio?: GrokVideoAspectRatio
176
+
177
+ /**
178
+ * Output resolution tier.
179
+ */
180
+ resolution?: GrokVideoResolution
181
+
182
+ /**
183
+ * Video duration in integer seconds (1–15).
184
+ */
185
+ duration?: number
186
+ }
187
+
188
+ /**
189
+ * Type-only map from model name to its specific provider options.
190
+ *
191
+ * @experimental Video generation is an experimental feature and may change.
192
+ */
193
+ export type GrokVideoModelProviderOptionsByName = {
194
+ 'grok-imagine-video': GrokVideoProviderOptions
195
+ 'grok-imagine-video-1.5': GrokVideoProviderOptions
196
+ }
197
+
198
+ /**
199
+ * Type-only map from model name to its supported `size` strings.
200
+ *
201
+ * @experimental Video generation is an experimental feature and may change.
202
+ */
203
+ export type GrokVideoModelSizeByName = {
204
+ 'grok-imagine-video': GrokVideoSize
205
+ 'grok-imagine-video-1.5': GrokVideoSize
206
+ }
207
+
208
+ /**
209
+ * Type-only map from model name to the non-text prompt modalities it accepts.
210
+ * Both models accept an `image` prompt part as the starting frame:
211
+ * `grok-imagine-video` (v1.0) does text-to-video and image-to-video, while
212
+ * `grok-imagine-video-1.5` is image-to-video only (the image is required).
213
+ *
214
+ * @experimental Video generation is an experimental feature and may change.
215
+ */
216
+ export type GrokVideoModelInputModalitiesByName = {
217
+ 'grok-imagine-video': readonly ['image']
218
+ 'grok-imagine-video-1.5': readonly ['image']
219
+ }
220
+
221
+ /**
222
+ * Models that only support image-to-video — a starting-frame image is
223
+ * required and text-to-video is rejected by the Imagine API. Used by the
224
+ * adapter to fail fast with a clear message instead of surfacing the raw
225
+ * "Text-to-video is not supported for this model" 400.
226
+ *
227
+ * @experimental Video generation is an experimental feature and may change.
228
+ */
229
+ const GROK_VIDEO_IMAGE_TO_VIDEO_ONLY: ReadonlySet<string> = new Set([
230
+ 'grok-imagine-video-1.5',
231
+ ])
232
+
233
+ /**
234
+ * True when the model only supports image-to-video (a starting frame is
235
+ * required).
236
+ *
237
+ * @experimental Video generation is an experimental feature and may change.
238
+ */
239
+ export function isImageToVideoOnlyModel(model: string): boolean {
240
+ return GROK_VIDEO_IMAGE_TO_VIDEO_ONLY.has(model)
241
+ }