import {z} from 'zod' export type ContentItem = {type: 'text' | 'image_url' | 'video_url' | 'audio_url', text?: string | undefined, image_url?: {url: string} | undefined, video_url?: {url: string} | undefined, audio_url?: {url: string} | undefined, role?: ('first_frame' | 'last_frame' | 'reference_image' | 'reference_video' | 'reference_audio') | undefined}; export type VideoGenerationV2Req = {model: 'MiniMax-H3' | 'MiniMax-H3-Highspeed' | 'MiniMax-H3-Lite' | 'MiniMax-H3-Max', content: Array, resolution: '480P' | '768P' | '2K', duration: 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15, ratio?: ('adaptive' | '21:9' | '16:9' | '4:3' | '1:1' | '3:4' | '9:16') | undefined, extra?: ({prompt_expansion_mode?: ('disabled' | 'balanced' | 'quality') | undefined}) | undefined, callback_url?: string | undefined}; export type CreateApiV2VideoGenerationArgs = {'Content-Type': 'application/json', body: VideoGenerationV2Req}; export type CreateApiV2VideoGenerationOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const videoGenerationV2Resp = z.object({task_id: z.string().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 }; /** * Create video generation task * * POST /v2/video_generation */ export const createApiV2VideoGeneration = async (args: CreateApiV2VideoGenerationArgs, options?: CreateApiV2VideoGenerationOptions) => { const {baseUrl = "https://api.minimax.io", 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['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/video_generation${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(videoGenerationV2Resp, await res.json()) };