import {z} from 'zod' export type VideoRegenerationSourceTaskReq = {model: 'MiniMax-H3', source_task_id: string, resolution: '2K', callback_url?: string | undefined, aigc_watermark?: boolean | undefined}; export type RegenContentItem = {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?: ('base_video' | 'first_frame' | 'last_frame' | 'reference_image' | 'reference_video' | 'reference_audio') | undefined}; export type VideoRegenerationBaseVideoReq = {model: 'MiniMax-H3', content: Array, resolution: '2K', callback_url?: string | undefined, aigc_watermark?: boolean | undefined}; export type CreateApiV2VideoRegenerationArgs = {'Content-Type': 'application/json', body: VideoRegenerationSourceTaskReq | VideoRegenerationBaseVideoReq}; export type CreateApiV2VideoRegenerationOptions = {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 Regeneration Task * * POST /v2/video_regeneration */ export const createApiV2VideoRegeneration = async (args: CreateApiV2VideoRegenerationArgs, options?: CreateApiV2VideoRegenerationOptions) => { 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_regeneration${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()) };