import { lazySchema, zodSchema } from '@ai-sdk/provider-utils'; import { z } from 'zod/v4'; const nonEmptyStringSchema = () => z.string().min(1); const resolutionSchema = () => z.enum(['480p', '720p', '1080p']); const modeSchema = () => z.enum(['edit-video', 'extend-video', 'reference-to-video']); export type XaiVideoMode = z.infer>; type XaiVideoResolution = z.infer>; const keyframeSchema = () => z.object({ imageUrl: nonEmptyStringSchema(), timestampSeconds: z.number().positive(), }); const storageOptionsSchema = () => z.object({ filename: nonEmptyStringSchema(), expiresAfter: z.number().int().positive().max(2_592_000).optional(), publicUrl: z .union([ z.boolean(), z.object({ expiresAfter: z.number().int().min(3_600).max(2_592_000).optional(), }), ]) .optional(), }); export type XaiVideoKeyframe = z.infer>; export type XaiVideoStorageOptions = z.infer< ReturnType >; interface XaiVideoSharedOptions { /** Deliver directly to caller-managed storage using an HTTPS signed PUT URL. */ output?: { uploadUrl: string }; pollIntervalMs?: number | null; pollTimeoutMs?: number | null; resolution?: XaiVideoResolution | null; /** Store the generated video in the xAI Files API. */ storageOptions?: XaiVideoStorageOptions; } interface XaiVideoUserOptions { /** * A unique identifier representing the end user, for abuse monitoring. */ user?: string; } interface XaiVideoEditModeOptions extends XaiVideoSharedOptions, XaiVideoUserOptions { /** * Select edit-video mode explicitly for best autocomplete and narrowing. */ mode: 'edit-video'; /** Source video URL to edit. */ videoUrl: string; } interface XaiVideoExtendModeOptions extends XaiVideoSharedOptions { /** * Select extend-video mode explicitly for best autocomplete and narrowing. */ mode: 'extend-video'; /** Source video URL to extend from its last frame. */ videoUrl: string; } interface XaiVideoReferenceToVideoOptions extends XaiVideoSharedOptions, XaiVideoUserOptions { /** * Select reference-to-video mode explicitly for best autocomplete and narrowing. */ mode: 'reference-to-video'; /** Reference image URLs (1-7) for R2V generation. */ referenceImageUrls: string[]; /** * Preset voice ids (up to 3) that give the subject a voice. */ referenceVoiceIds?: string[]; } interface XaiVideoGenerationOptions extends XaiVideoSharedOptions, XaiVideoUserOptions { mode?: undefined; videoUrl?: undefined; referenceImageUrls?: undefined; /** Mid-video image anchors (up to 4). */ keyframes?: XaiVideoKeyframe[]; } interface XaiLegacyEditVideoOptions extends XaiVideoSharedOptions, XaiVideoUserOptions { /** * Legacy backward-compatible shape: omitting `mode` while providing * `videoUrl` behaves like edit-video. */ mode?: undefined; videoUrl: string; } interface XaiLegacyReferenceToVideoOptions extends XaiVideoSharedOptions, XaiVideoUserOptions { /** * Legacy backward-compatible shape: omitting `mode` while providing * `referenceImageUrls` behaves like reference-to-video. */ mode?: undefined; referenceImageUrls: string[]; /** * Preset voice ids (up to 3) that give the subject a voice. */ referenceVoiceIds?: string[]; } /** * Provider options for xAI video generation. * * Use the `mode` option to select the operation: * * - `'edit-video'` + `videoUrl` -- video editing (`POST /v1/videos/edits`) * - `'extend-video'` + `videoUrl` -- video extension (`POST /v1/videos/extensions`) * - `'reference-to-video'` + `referenceImageUrls` -- R2V generation (`POST /v1/videos/generations`) * - no `mode` -- standard generation from text prompts or image input * * Runtime remains backward compatible with legacy auto-detected provider * options, but the public TypeScript type is intentionally explicit so editors * can suggest valid modes and flag invalid field combinations. */ export type XaiVideoModelOptions = | XaiVideoGenerationOptions | XaiVideoEditModeOptions | XaiVideoExtendModeOptions | XaiVideoReferenceToVideoOptions | XaiLegacyEditVideoOptions | XaiLegacyReferenceToVideoOptions; // ── Runtime schemas ─────────────────────────────────────────────────── const baseFields = () => ({ output: z .object({ uploadUrl: z.url({ protocol: /^https$/ }).refine(value => { const url = new URL(value); return url.username === '' && url.password === '' && url.hash === ''; }), }) .optional(), pollIntervalMs: z.number().positive().nullish(), pollTimeoutMs: z.number().positive().nullish(), resolution: resolutionSchema().nullish(), }); const runtimeSchema = () => z.looseObject({ mode: modeSchema().optional(), videoUrl: nonEmptyStringSchema().optional(), referenceImageUrls: z .array(nonEmptyStringSchema()) .min(1) .max(7) .optional(), referenceVoiceIds: z.array(nonEmptyStringSchema()).max(3).optional(), keyframes: z.array(keyframeSchema()).max(4).optional(), storageOptions: storageOptionsSchema().optional(), user: z.string().optional(), ...baseFields(), }); export type XaiParsedVideoModelOptions = z.infer< ReturnType >; export const xaiVideoModelOptionsSchema = lazySchema(() => zodSchema(runtimeSchema()), );