import {z} from 'zod' export type CreateApiGenerateVideoXaiGrokVideoArgs = {'X-Webhook-URL'?: string | undefined, body: {prompt: string, start_image?: (string | null) | undefined, start_video?: (string | null) | undefined, duration?: number | undefined, resolution?: ('480p' | '720p') | undefined, aspect_ratio?: ('16:9' | '9:16' | '1:1' | '4:3' | '3:4' | '3:2' | '2:3') | undefined, reference_images?: Array | undefined, edit_video?: string | undefined}}; export type CreateApiGenerateVideoXaiGrokVideoOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiGenerateVideoXaiGrokVideoResponse = z.object({job_id: z.string(), status: z.enum(["backlogged", "queued", "scheduled", "processing", "sampling", "intermediate-complete", "completed", "failed", "cancelled"]), created_at: z.string(), completed_at: z.unknown(), result: z.unknown(), error: z.object({code: z.string(), message: z.string().optional(), param: z.string().optional()}).optional(), progress: z.number().gte(0).lte(1).nullable().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Grok Imagine * * POST /generate/video/xai/grok-video */ export const createApiGenerateVideoXaiGrokVideo = async (args: CreateApiGenerateVideoXaiGrokVideoArgs, options?: CreateApiGenerateVideoXaiGrokVideoOptions) => { const {baseUrl = "https://api.krea.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Webhook-URL'] != null) headers.set("X-Webhook-URL", String(args['X-Webhook-URL'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/generate/video/xai/grok-video${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiGenerateVideoXaiGrokVideoResponse, await res.json()) };