diff --git a/packages/ai/README.md b/packages/ai/README.md index 0f6584873a01..c44c9ed1d00d 100644 --- a/packages/ai/README.md +++ b/packages/ai/README.md @@ -1036,8 +1036,8 @@ The hosted result is represented as a provider-executed tool call and a tool res Video generation is a job: `start` submits it and returns at once, and the caller decides when to read its `status`, fetch its `result`, or `cancel` it. Nothing polls for you. Every call after `start` takes the model and the job's `token`, plain JSON you can store and read back from another process, so recovering after a restart is the same call -as a normal status read. Models come from `.video(...)` selectors on the `XAI`, `OpenRouter`, `Volcengine` and -`BytePlus` facades. +as a normal status read. Models come from `.video(...)` selectors on the `XAI`, `OpenRouter`, `Volcengine`, +`BytePlus` and `MiniMax` facades. ```ts import { Effect } from "effect" @@ -1104,6 +1104,18 @@ Provider notes: `omni_reference_task_type`, `return_last_frame` and the rest go into the body as-is. `cancel` sends `DELETE`, which only cancels a queued task: Ark refuses it for a running one and deletes the record of a finished one. Video URLs expire 24 hours after the task finished. +- **MiniMax** (`MiniMax.configure(...).video(id)`, or `@opencode/ai/providers/minimax/videos`) serves two APIs and picks + one by model family: Hailuo and `*-01` models use v1, everything else (H3) uses v2. Video builds its `/v1` and `/v2` + paths on the API host, so a `baseURL` shared with images or chat (`https://api.minimax.cn/v1`) loses its trailing + `/v1`, and a bare host (`https://api.minimax.io`) works as is. On H3, `start`, `end` and `references` become `content[]` items with `first_frame`, `last_frame` and `reference_image` / + `reference_video` / `reference_audio` roles, MiniMax file refs become `mm_file://` URLs, `aspect: "auto"` becomes + `ratio: "adaptive"`, and `resolution` is uppercased (`480P`, `2K`). H3 requires `resolution` and `duration`, plus a + `ratio` for text-only prompts; nothing is filled in, so MiniMax rejects a request without them. H3 always has sound, + so `sound: false` fails before sending. On Hailuo, `start` and `end` map to `first_frame_image` and + `last_frame_image`, image references to `subject_reference`, and the result looks up the file's download URL, which + expires after one hour. Hailuo is silent and has no aspect field, so `sound: true`, an explicit `aspect`, and video + or audio references fail before sending. Cancel works on H3 (only while queued); Hailuo has no cancel. Native fields + such as `extra.prompt_expansion_mode` (H3), `prompt_optimizer` (Hailuo) and `callback_url` go under `providerOptions`. The promise client has the same calls, each accepting `{ signal }`: diff --git a/packages/ai/package.json b/packages/ai/package.json index 765f0b1f1ffe..94da792f2f52 100644 --- a/packages/ai/package.json +++ b/packages/ai/package.json @@ -94,6 +94,7 @@ "./providers/minimax/images": "./src/providers/minimax/images.ts", "./providers/minimax/messages": "./src/providers/minimax/messages.ts", "./providers/minimax/responses": "./src/providers/minimax/responses.ts", + "./providers/minimax/videos": "./src/providers/minimax/videos.ts", "./providers/mistral": "./src/providers/mistral.ts", "./providers/moonshot": "./src/providers/moonshot.ts", "./providers/moonshot/chat": "./src/providers/moonshot/chat.ts", diff --git a/packages/ai/src/protocols/minimax-video.ts b/packages/ai/src/protocols/minimax-video.ts new file mode 100644 index 000000000000..a5ff152e3d1e --- /dev/null +++ b/packages/ai/src/protocols/minimax-video.ts @@ -0,0 +1,402 @@ +import { Duration, Effect, Schema } from "effect" +import type { HttpClientResponse } from "effect/http" +import type { Status } from "../generation.js" +import { Media } from "../media.js" +import { MediaProtocol } from "../route/media-protocol.js" +import { MediaRoute } from "../route/media.js" +import { AIError, QuotaExceededError, mergeJsonRecords, type OpenString } from "../schema/index.js" +import { VideoModel, VideoResponse, seconds, type VideoRequest } from "../video.js" +import { ProviderShared, optionalNull } from "./shared.js" + +// MiniMax runs two video APIs with different bodies, statuses and results: H3 on v2 and Hailuo on v1. +const route = MediaProtocol.identity({ id: "minimax-video", name: "MiniMax Video", provider: "minimax" }) +const hailuoRoute = MediaProtocol.identity({ + id: "minimax-hailuo-video", + name: "MiniMax Hailuo Video", + provider: "minimax", +}) +/** The API host without a version: H3 is served under `/v2` and Hailuo under `/v1`. */ +export const DEFAULT_BASE_URL = "https://api.minimax.io" +export const PATH = "/v2/video_generation" +export const STATUS_PATH = "/v2/query/video_generation" +export const HAILUO_PATH = "/v1/video_generation" +export const HAILUO_STATUS_PATH = "/v1/query/video_generation" +export const FILE_PATH = "/v1/files/retrieve" +const DOWNLOAD_RETENTION = Duration.hours(1) + +// --------------------------------------------------------------------------- +// 1. Public model input +// --------------------------------------------------------------------------- + +/** + * Provider-native options, merged into the body. Common fields (`start`, `end`, `references`, `duration`, `aspect`, + * `resolution`, `sound`) live on the request. `extra` and `response_format` are H3 (v2) fields; `prompt_optimizer` + * and `fast_pretreatment` are Hailuo (v1) fields; both APIs take `callback_url`. + */ +export type MiniMaxVideoOptions = { + readonly extra?: { readonly prompt_expansion_mode?: OpenString<"disabled" | "balanced" | "quality"> } + readonly response_format?: OpenString<"task" | "flat"> + readonly prompt_optimizer?: boolean + readonly fast_pretreatment?: boolean + readonly callback_url?: string +} & Record + +export type Request = VideoRequest> + +// --------------------------------------------------------------------------- +// 2. Token and response schemas +// --------------------------------------------------------------------------- + +export const Token = Schema.Struct({ taskID: Schema.String }) +export type Token = Schema.Schema.Type + +const BaseResp = optionalNull( + Schema.Struct({ + status_code: Schema.Number, + status_msg: optionalNull(Schema.String), + }), +) + +const StartResponse = Schema.Struct({ task_id: Schema.String }) + +const Task = Schema.Struct({ + id: optionalNull(Schema.String), + model: optionalNull(Schema.String), + status: optionalNull(Schema.String), + error: optionalNull( + Schema.Struct({ + code: optionalNull(Schema.Union([Schema.String, Schema.Number])), + message: optionalNull(Schema.String), + }), + ), + content: optionalNull(Schema.Struct({ url: optionalNull(Schema.String) })), + resolution: optionalNull(Schema.String), + duration: optionalNull(Schema.Number), + ratio: optionalNull(Schema.String), + usage: optionalNull(Schema.Record(Schema.String, Schema.Unknown)), +}) + +// `response_format: "flat"` moves the task fields from `task` to the top level. +const TaskResponse = Schema.Struct({ ...Task.fields, task: optionalNull(Task) }) + +const HailuoStartResponse = Schema.Struct({ task_id: optionalNull(Schema.String), base_resp: BaseResp }) + +const HailuoTask = Schema.Struct({ + status: optionalNull(Schema.String), + file_id: optionalNull(Schema.String), + video_width: optionalNull(Schema.Number), + video_height: optionalNull(Schema.Number), + base_resp: BaseResp, +}) + +const HailuoFile = Schema.Struct({ + file: optionalNull( + Schema.Struct({ + download_url: optionalNull(Schema.String), + }), + ), + base_resp: BaseResp, +}) + +const STATUS = { + queued: "queued", + running: "running", + succeeded: "completed", + failed: "failed", + cancelled: "cancelled", +} as const satisfies Record + +const HAILUO_STATUS = { + Preparing: "queued", + Queueing: "queued", + Processing: "running", + Success: "completed", + Fail: "failed", +} as const satisfies Record + +// --------------------------------------------------------------------------- +// 3. Failures +// --------------------------------------------------------------------------- + +// Error codes from https://platform.minimax.io/docs/api-reference/errorcode; unknown codes are provider-side. +const FAILURE: Readonly> = { + 1002: "RateLimit", + 1004: "Authentication", + 1039: "RateLimit", + 2013: "InvalidRequest", + 2049: "Authentication", +} +const INSUFFICIENT_BALANCE = 1008 +// 1026 flags the input and 1027 the generated video. +const CONTENT_POLICY = new Set(["1026", "1027"]) + +type Output = Effect.Success> + +const failed = (output: Output, name: string, code: string | number | undefined, detail: string | undefined) => { + const message = `${name} generation failed${code === undefined ? "" : ` (${code})`}${detail === undefined ? "" : `: ${detail}`}` + if (code !== undefined && CONTENT_POLICY.has(String(code))) return output.contentPolicy(message) + if (code !== undefined && Number(code) === INSUFFICIENT_BALANCE) + return new AIError({ reason: new QuotaExceededError({ message, body: output.body, http: output.http }) }) + return output.ended("failed", message, MediaProtocol.failure(FAILURE, code)) +} + +// v1 answers errors with HTTP 200 and a non-zero `base_resp.status_code`. +const baseError = ( + output: Output, + base: { readonly status_code: number; readonly status_msg?: string | null } | null | undefined, +) => + base === undefined || base === null || base.status_code === 0 + ? undefined + : failed(output, hailuoRoute.name, base.status_code, base.status_msg ?? undefined) + +// --------------------------------------------------------------------------- +// 5. Request body construction +// --------------------------------------------------------------------------- + +/** H3 takes URLs, data URIs and `mm_file://{file_id}` references to MiniMax files. */ +const mediaUrl = (asset: Media.Asset) => + ProviderShared.mediaReference(asset, route.provider, route.name).pipe( + Effect.map((reference) => (reference.type === "ref" ? `mm_file://${reference.value}` : reference.value)), + ) + +const ROLES = { image: "reference_image", video: "reference_video", audio: "reference_audio" } as const + +const item = (asset: Media.Asset, role: string) => + mediaUrl(asset).pipe( + Effect.map((url) => { + const type = asset.kind === "video" ? "video_url" : asset.kind === "audio" ? "audio_url" : "image_url" + return { type, [type]: { url }, role } + }), + ) + +const reference = (asset: Media.Asset) => + item(asset, asset.kind === "video" || asset.kind === "audio" ? ROLES[asset.kind] : ROLES.image) + +/** MiniMax spells resolutions in uppercase (`480P`, `768P`, `2K`). */ +const resolution = (request: Request) => request.resolution?.toUpperCase() + +const fromRequest = Effect.fn("MiniMaxVideo.fromRequest")(function* (request: Request) { + // H3 always generates a soundtrack and has no field to turn it off. + if (request.sound === false) + return yield* route.unsupported("video.sound", `${route.name} always generates sound and can't be silenced`) + const content = [ + { type: "text", text: request.prompt }, + ...(request.start === undefined ? [] : [yield* item(request.start, "first_frame")]), + ...(request.end === undefined ? [] : [yield* item(request.end, "last_frame")]), + ...(yield* Effect.forEach(request.references ?? [], reference)), + ] + return MediaProtocol.json( + mergeJsonRecords( + { + model: request.model.id, + content, + resolution: resolution(request), + duration: request.duration === undefined ? undefined : seconds(request.duration), + ratio: request.aspect === "auto" ? "adaptive" : request.aspect, + }, + request.providerOptions, + request.http?.body, + ) ?? {}, + ) +}) + +/** v1 documents URLs and data URLs only, so MiniMax file references aren't sent. */ +const hailuoUrl = (asset: Media.Asset) => + ProviderShared.mediaReference(asset, undefined, hailuoRoute.name).pipe(Effect.map((reference) => reference.value)) + +const hailuoFromRequest = Effect.fn("MiniMaxHailuoVideo.fromRequest")(function* (request: Request) { + const references = request.references ?? [] + if (references.some((asset) => asset.kind === "video" || asset.kind === "audio")) + return yield* hailuoRoute.unsupported( + "video.references", + `${hailuoRoute.name} takes only image (subject) references`, + ) + // Hailuo output is always silent and its shape follows the model or the first frame, with no field for either. + if (request.sound === true) + return yield* hailuoRoute.unsupported("video.sound", `${hailuoRoute.name} generates silent video only`) + if (request.aspect !== undefined && request.aspect !== "auto") + return yield* hailuoRoute.unsupported("video.aspect", `${hailuoRoute.name} has no aspect ratio input`) + const subjects = yield* Effect.forEach(references, (asset) => + hailuoUrl(asset).pipe(Effect.map((url) => ({ type: "character", image: [url] }))), + ) + return MediaProtocol.json( + mergeJsonRecords( + { + model: request.model.id, + prompt: request.prompt, + first_frame_image: request.start === undefined ? undefined : yield* hailuoUrl(request.start), + last_frame_image: request.end === undefined ? undefined : yield* hailuoUrl(request.end), + subject_reference: subjects.length === 0 ? undefined : subjects, + duration: request.duration === undefined ? undefined : seconds(request.duration), + resolution: resolution(request), + }, + request.providerOptions, + request.http?.body, + ) ?? {}, + ) +}) + +// --------------------------------------------------------------------------- +// 6. Response decoding +// --------------------------------------------------------------------------- + +const decodeStart = route.decodeStarted(StartResponse, (value) => ({ + token: { taskID: value.task_id }, + snapshot: { id: value.task_id, status: "queued" }, +})) + +const decodeTask = route.decodeJson(TaskResponse) + +const decodeStatus = Effect.fn("MiniMaxVideo.decodeStatus")(function* ( + response: HttpClientResponse.HttpClientResponse, + context: MediaProtocol.PollContext, +) { + const output = yield* decodeTask(response) + const task = output.value.task ?? output.value + return { id: context.token.taskID, status: MediaProtocol.status(STATUS, task.status ?? "") } +}) + +const decodeResult = Effect.fn("MiniMaxVideo.decodeResult")(function* ( + response: HttpClientResponse.HttpClientResponse, + context: MediaProtocol.PollContext, +) { + const output = yield* decodeTask(response) + const task = output.value.task ?? output.value + const status = MediaProtocol.status(STATUS, task.status ?? "") + if (status === "queued" || status === "running") return yield* output.pending(context.token.taskID) + if (status === "failed") + return yield* failed(output, route.name, task.error?.code ?? undefined, task.error?.message ?? undefined) + if (status !== "completed") + return yield* output.ended("cancelled", `${route.name} task ${context.token.taskID} was cancelled`) + const url = task.content?.url ?? undefined + if (url === undefined) return yield* output.invalid(`${route.name} completed without a video URL`) + const duration = task.duration ?? undefined + const total = task.usage?.total_seconds + return new VideoResponse({ + // The URL is time-limited without a documented window; reading the result again returns a fresh one. + videos: [ + Media.url(url, { + mediaType: "video/mp4", + info: duration === undefined ? undefined : { durationSeconds: duration }, + }), + ], + usage: typeof total === "number" ? { type: "seconds", seconds: total } : undefined, + providerMetadata: { + minimax: { + taskId: context.token.taskID, + model: task.model ?? undefined, + resolution: task.resolution ?? undefined, + ratio: task.ratio ?? undefined, + usage: task.usage ?? undefined, + }, + }, + }) +}) + +const decodeHailuoStart = Effect.fn("MiniMaxHailuoVideo.decodeStart")(function* ( + response: HttpClientResponse.HttpClientResponse, +) { + const output = yield* hailuoRoute.decodeJson(HailuoStartResponse)(response) + const error = baseError(output, output.value.base_resp) + if (error) return yield* error + const taskID = output.value.task_id ?? "" + if (taskID === "") return yield* output.invalid(`${hailuoRoute.name} returned no task_id`) + return { token: { taskID }, snapshot: { id: taskID, status: "queued" as const } } +}) + +const decodeHailuoTask = hailuoRoute.decodeJson(HailuoTask) + +const decodeHailuoStatus = Effect.fn("MiniMaxHailuoVideo.decodeStatus")(function* ( + response: HttpClientResponse.HttpClientResponse, + context: MediaProtocol.PollContext, +) { + const output = yield* decodeHailuoTask(response) + const status = MediaProtocol.status(HAILUO_STATUS, output.value.status ?? "") + // A failed task also carries its reason in `base_resp`; any other non-zero code is a failed read. + const error = status === "failed" ? undefined : baseError(output, output.value.base_resp) + if (error) return yield* error + return { id: context.token.taskID, status } +}) + +const decodeHailuoFile = hailuoRoute.decodeJson(HailuoFile) + +const decodeHailuoResult = Effect.fn("MiniMaxHailuoVideo.decodeResult")(function* ( + response: HttpClientResponse.HttpClientResponse, + context: MediaProtocol.PollContext, +) { + const output = yield* decodeHailuoTask(response) + const task = output.value + const status = MediaProtocol.status(HAILUO_STATUS, task.status ?? "") + const error = baseError(output, task.base_resp) + if (status === "failed") return yield* error ?? failed(output, hailuoRoute.name, undefined, undefined) + if (error) return yield* error + if (status !== "completed") return yield* output.pending(context.token.taskID) + const fileID = task.file_id ?? "" + if (fileID === "") return yield* output.invalid(`${hailuoRoute.name} completed without a file_id`) + // The task names a file; its download URL takes a second lookup and is valid for one hour. + const file = yield* decodeHailuoFile(yield* context.get(`${FILE_PATH}?file_id=${encodeURIComponent(fileID)}`)) + const fileError = baseError(file, file.value.base_resp) + if (fileError) return yield* fileError + const url = file.value.file?.download_url ?? undefined + if (url === undefined) return yield* file.invalid(`${hailuoRoute.name} returned no download_url for ${fileID}`) + const width = task.video_width ?? undefined + const height = task.video_height ?? undefined + return new VideoResponse({ + videos: [ + yield* MediaProtocol.expiringUrl(url, DOWNLOAD_RETENTION, { + mediaType: "video/mp4", + info: width === undefined && height === undefined ? undefined : { width, height }, + }), + ], + providerMetadata: { + minimax: { taskId: context.token.taskID, fileId: fileID }, + }, + }) +}) + +// --------------------------------------------------------------------------- +// 7. Protocol and route +// --------------------------------------------------------------------------- + +export const protocol = MediaProtocol.queued(route, { + token: Token, + start: { body: { from: fromRequest }, decode: decodeStart }, + status: { path: (token) => `${STATUS_PATH}/${token.taskID}`, decode: decodeStatus }, + result: { path: (token) => `${STATUS_PATH}/${token.taskID}`, decode: decodeResult }, + // Cancels a queued task; MiniMax rejects it once the task is running. + cancel: { method: "DELETE", path: (token) => `${PATH}/${token.taskID}` }, +}) + +const hailuoStatusPath = (token: Token) => `${HAILUO_STATUS_PATH}?task_id=${encodeURIComponent(token.taskID)}` + +export const hailuoProtocol = MediaProtocol.queued(hailuoRoute, { + token: Token, + start: { body: { from: hailuoFromRequest }, decode: decodeHailuoStart }, + status: { path: hailuoStatusPath, decode: decodeHailuoStatus }, + result: { path: hailuoStatusPath, decode: decodeHailuoResult }, +}) + +/** H3 and later models on the v2 API. */ +export const h3Model = (input: MediaRoute.ModelInput) => + VideoModel.fromRoute({ protocol, baseURL: DEFAULT_BASE_URL, path: PATH }, input) + +/** Hailuo and the `*-01` models on the v1 API. */ +export const hailuoModel = (input: MediaRoute.ModelInput) => + VideoModel.fromRoute( + { protocol: hailuoProtocol, baseURL: DEFAULT_BASE_URL, path: HAILUO_PATH }, + input, + ) + +// v1 is MiniMax's legacy API with a fixed model list; every newer model ships on v2. +const isHailuo = (modelID: string) => /^(minimax-hailuo-|[tis]2v-01|video-01)/i.test(modelID) + +/** Picks the API by model family: Hailuo and `*-01` models use v1, everything else v2. */ +export const model = (input: MediaRoute.ModelInput) => (isHailuo(input.id) ? hailuoModel(input) : h3Model(input)) + +export const MiniMaxVideo = { + protocol, + hailuoProtocol, + model, + h3Model, + hailuoModel, +} as const diff --git a/packages/ai/src/providers/minimax.ts b/packages/ai/src/providers/minimax.ts index 18f515194a3b..7d478e53795a 100644 --- a/packages/ai/src/providers/minimax.ts +++ b/packages/ai/src/providers/minimax.ts @@ -2,6 +2,7 @@ import { Effect, Schema } from "effect" import type { ProviderPackage } from "../provider-package.js" import { AnthropicMessages } from "../protocols/anthropic-messages.js" import { MiniMaxImages, type MiniMaxImageOptions } from "../protocols/minimax-images.js" +import { MiniMaxVideo, type MiniMaxVideoOptions } from "../protocols/minimax-video.js" import { OpenAIChat } from "../protocols/openai-chat.js" import { OpenResponses } from "../protocols/open-responses.js" import { ProviderShared } from "../protocols/shared.js" @@ -55,7 +56,7 @@ export type Settings = ProviderPackage.Settings readonly baseURL?: string } -export type { MiniMaxImageOptions } +export type { MiniMaxImageOptions, MiniMaxVideoOptions } const ChatOptions = Schema.Struct({ thinking: Schema.optional(Schema.Struct({ type: Schema.String })), @@ -132,7 +133,11 @@ export const configure = (input: Config = {}) => { const media = MediaRoute.deployment(input, defaults.auth) const image = (modelID: string | ModelID, providerOptions?: ProviderOptions) => MiniMaxImages.model({ ...media, id: modelID, providerOptions }) - return { id, model: messages, messages, chat, responses, image, configure } + // H3 is under /v2 and Hailuo under /v1, so video drops the version a shared `baseURL` carries for the other APIs. + const videoBaseURL = media.baseURL?.replace(/(\/v1)?\/*$/, "") + const video = (modelID: string | ModelID, providerOptions?: ProviderOptions) => + MiniMaxVideo.model({ ...media, baseURL: videoBaseURL, id: modelID, providerOptions }) + return { id, model: messages, messages, chat, responses, image, video, configure } } export const provider = configure() @@ -143,10 +148,15 @@ export const imageModel = (modelID: string, settings?: Settings>(settings) return configure(input).image(modelID, input.providerOptions) } +export const videoModel = (modelID: string, settings?: Settings) => { + const input = toConfig>(settings) + return configure(input).video(modelID, input.providerOptions) +} export const messages = provider.messages export const chat = provider.chat export const responses = provider.responses export const image = provider.image +export const video = provider.video export * as MiniMax from "./minimax.js" diff --git a/packages/ai/src/providers/minimax/videos.ts b/packages/ai/src/providers/minimax/videos.ts new file mode 100644 index 000000000000..bc18af4a8043 --- /dev/null +++ b/packages/ai/src/providers/minimax/videos.ts @@ -0,0 +1,4 @@ +import type { MiniMax } from "../minimax.js" + +export { videoModel as model } from "../minimax.js" +export type Settings = MiniMax.Settings diff --git a/packages/ai/src/route/media-protocol.ts b/packages/ai/src/route/media-protocol.ts index 49e7fda2a8c4..3de4b5935230 100644 --- a/packages/ai/src/route/media-protocol.ts +++ b/packages/ai/src/route/media-protocol.ts @@ -97,6 +97,11 @@ export interface PollContext { * the API key to download. URLs on another origin than the route's base URL get no headers. */ readonly headers: (url: string) => Effect.Effect, AIError> + /** + * A further GET with the same auth and headers, retried like the poll itself, for providers whose result needs a + * second lookup (MiniMax v1 answers with a file id that resolves to a download URL). + */ + readonly get: (path: string) => Effect.Effect } /** diff --git a/packages/ai/src/route/media.ts b/packages/ai/src/route/media.ts index e83b0d9f2795..5df443a09311 100644 --- a/packages/ai/src/route/media.ts +++ b/packages/ai/src/route/media.ts @@ -191,6 +191,11 @@ export const queued = ( const generationRoute = (token: Token, exchange: Exchange, execute: Execute) => { // Only the GET exchange retries: a decoded terminal failure (`output.ended`) can be a `ProviderInternal` too, and // re-reading it would spin until the caller's deadline. + const get = (path: string) => + transport.call("GET", path, exchange, execute).pipe( + Effect.retry({ schedule: READ_RETRY, while: isRetryable }), + Effect.map((sent) => sent.response), + ) const poll = (operation: { readonly path: (token: Token) => string readonly decode: ( @@ -198,10 +203,9 @@ export const queued = ( context: MediaProtocol.PollContext, ) => Effect.Effect }) => - transport.call("GET", operation.path(token), exchange, execute).pipe( - Effect.retry({ schedule: READ_RETRY, while: isRetryable }), - Effect.flatMap((sent) => - operation.decode(sent.response, { token, headers: (url) => transport.headers(url, exchange) }), + get(operation.path(token)).pipe( + Effect.flatMap((response) => + operation.decode(response, { token, get, headers: (url) => transport.headers(url, exchange) }), ), ) const status = poll(protocol.status) diff --git a/packages/ai/test/fixtures/recordings/minimax-video/generates-a-hailuo-video-from-a-first-frame.json b/packages/ai/test/fixtures/recordings/minimax-video/generates-a-hailuo-video-from-a-first-frame.json new file mode 100644 index 000000000000..ab3b80665f27 --- /dev/null +++ b/packages/ai/test/fixtures/recordings/minimax-video/generates-a-hailuo-video-from-a-first-frame.json @@ -0,0 +1,176 @@ +{ + "version": 1, + "metadata": { + "tags": [ + "prefix:minimax-video", + "provider:minimax", + "protocol:minimax-video" + ], + "name": "minimax-video/generates-a-hailuo-video-from-a-first-frame", + "recordedAt": "2026-11-13T05:22:58.775Z" + }, + "interactions": [ + { + "transport": "http", + "request": { + "method": "POST", + "url": "https://api.minimax.io/v1/video_generation", + "headers": { + "content-type": "application/json" + }, + "body": "{\"model\":\"MiniMax-Hailuo-02\",\"prompt\":\"The woman slowly turns her head and smiles.\",\"first_frame_image\":\"https://cdn.hailuoai.com/prod/2025-08-12-17/video_cover/1754990600020238321-411603868533342214-cover.jpg\",\"duration\":6,\"resolution\":\"512P\"}" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462825178886412\",\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v1/query/video_generation?task_id=462825178886412", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462825178886412\",\"status\":\"Preparing\",\"file_id\":\"\",\"video_width\":0,\"video_height\":0,\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v1/query/video_generation?task_id=462825178886412", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462825178886412\",\"status\":\"Preparing\",\"file_id\":\"\",\"video_width\":0,\"video_height\":0,\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v1/query/video_generation?task_id=462825178886412", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462825178886412\",\"status\":\"Processing\",\"file_id\":\"\",\"video_width\":0,\"video_height\":0,\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v1/query/video_generation?task_id=462825178886412", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462825178886412\",\"status\":\"Processing\",\"file_id\":\"\",\"video_width\":0,\"video_height\":0,\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v1/query/video_generation?task_id=462825178886412", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462825178886412\",\"status\":\"Processing\",\"file_id\":\"\",\"video_width\":0,\"video_height\":0,\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v1/query/video_generation?task_id=462825178886412", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462825178886412\",\"status\":\"Processing\",\"file_id\":\"\",\"video_width\":0,\"video_height\":0,\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v1/query/video_generation?task_id=462825178886412", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462825178886412\",\"status\":\"Success\",\"file_id\":\"462824926277972\",\"video_width\":512,\"video_height\":868,\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v1/query/video_generation?task_id=462825178886412", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462825178886412\",\"status\":\"Success\",\"file_id\":\"462824926277972\",\"video_width\":512,\"video_height\":868,\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v1/files/retrieve?file_id=462824926277972", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"file\":{\"file_id\":462824926277972,\"bytes\":0,\"created_at\":1794547367,\"filename\":\"output_aigc.mp4\",\"purpose\":\"video_generation\",\"download_url\":\"https://video-product.cdn.minimax.io/inference_output/video/2026-11-13/ca590eb9-b71a-4cfc-bc15-4ed164e3346c/output_aigc.mp4\"},\"base_resp\":{\"status_code\":0,\"status_msg\":\"success\"}}\n" + } + } + ] +} diff --git a/packages/ai/test/fixtures/recordings/minimax-video/generates-an-h3-video.json b/packages/ai/test/fixtures/recordings/minimax-video/generates-an-h3-video.json new file mode 100644 index 000000000000..c4ec73e5365e --- /dev/null +++ b/packages/ai/test/fixtures/recordings/minimax-video/generates-an-h3-video.json @@ -0,0 +1,96 @@ +{ + "version": 1, + "metadata": { + "tags": [ + "prefix:minimax-video", + "provider:minimax", + "protocol:minimax-video" + ], + "name": "minimax-video/generates-an-h3-video", + "recordedAt": "2026-11-13T05:21:52.796Z" + }, + "interactions": [ + { + "transport": "http", + "request": { + "method": "POST", + "url": "https://api.minimax.io/v2/video_generation", + "headers": { + "content-type": "application/json" + }, + "body": "{\"model\":\"MiniMax-H3-Lite\",\"content\":[{\"type\":\"text\",\"text\":\"A single red balloon drifting slowly upward against a clear blue sky.\"}],\"resolution\":\"480P\",\"duration\":4,\"ratio\":\"16:9\"}" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task_id\":\"462821491831079\"}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v2/query/video_generation/462821491831079", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task\":{\"id\":\"462821491831079\",\"model\":\"MiniMax-H3-Lite\",\"status\":\"running\",\"created_at\":1794547292,\"updated_at\":1794547292,\"resolution\":\"480P\",\"duration\":4,\"usage\":{},\"ratio\":\"16:9\",\"task_type\":\"generation\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v2/query/video_generation/462821491831079", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task\":{\"id\":\"462821491831079\",\"model\":\"MiniMax-H3-Lite\",\"status\":\"running\",\"created_at\":1794547292,\"updated_at\":1794547292,\"resolution\":\"480P\",\"duration\":4,\"usage\":{},\"ratio\":\"16:9\",\"task_type\":\"generation\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v2/query/video_generation/462821491831079", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task\":{\"id\":\"462821491831079\",\"model\":\"MiniMax-H3-Lite\",\"status\":\"succeeded\",\"created_at\":1794547292,\"updated_at\":1794547308,\"content\":{\"url\":\"https://video-product.cdn.minimax.io/h3-480p/2026/11/13/style1/462821491831079-1-8d955ea33e6c8868_aigc.mp4\"},\"resolution\":\"480P\",\"duration\":4,\"usage\":{\"total_seconds\":4,\"input_seconds\":0,\"output_seconds\":4,\"input_image_count\":0,\"total_tokens\":41664,\"prompt_tokens\":0,\"completion_tokens\":41664},\"ratio\":\"16:9\",\"task_type\":\"generation\"}}" + } + }, + { + "transport": "http", + "request": { + "method": "GET", + "url": "https://api.minimax.io/v2/query/video_generation/462821491831079", + "headers": {}, + "body": "" + }, + "response": { + "status": 200, + "headers": { + "content-type": "application/json; charset=utf-8" + }, + "body": "{\"task\":{\"id\":\"462821491831079\",\"model\":\"MiniMax-H3-Lite\",\"status\":\"succeeded\",\"created_at\":1794547292,\"updated_at\":1794547308,\"content\":{\"url\":\"https://video-product.cdn.minimax.io/h3-480p/2026/11/13/style1/462821491831079-1-8d955ea33e6c8868_aigc.mp4\"},\"resolution\":\"480P\",\"duration\":4,\"usage\":{\"total_seconds\":4,\"input_seconds\":0,\"output_seconds\":4,\"input_image_count\":0,\"total_tokens\":41664,\"prompt_tokens\":0,\"completion_tokens\":41664},\"ratio\":\"16:9\",\"task_type\":\"generation\"}}" + } + } + ] +} diff --git a/packages/ai/test/minimax-video.test.ts b/packages/ai/test/minimax-video.test.ts new file mode 100644 index 000000000000..85ea449d0663 --- /dev/null +++ b/packages/ai/test/minimax-video.test.ts @@ -0,0 +1,544 @@ +import { describe, expect } from "bun:test" +import { Effect, Layer } from "effect" +import { Media, VideoClient, ai } from "../src/effect.js" +import { MiniMax } from "../src/providers.js" +import { it } from "./lib/effect.js" +import { dynamicResponse, json, observe, type Call } from "./lib/http.js" + +const layer = (handler: Parameters[0]) => + VideoClient.layer.pipe(Layer.provideMerge(dynamicResponse(handler))) + +const minimax = MiniMax.configure({ apiKey: "test", baseURL: "https://minimax.test" }) +const h3 = minimax.video("MiniMax-H3-Lite") +const hailuo = minimax.video("MiniMax-Hailuo-02") +const token = { taskID: "task_1" } + +const task = (fields: Record) => ({ task: { id: "task_1", model: "MiniMax-H3-Lite", ...fields } }) +const succeeded = task({ + status: "succeeded", + content: { url: "https://cdn.minimax.test/out.mp4" }, + resolution: "480P", + duration: 4, + ratio: "16:9", + usage: { total_seconds: 4, output_seconds: 4, input_image_count: 0 }, +}) + +const hailuoTask = (fields: Record) => ({ + task_id: "task_1", + base_resp: { status_code: 0, status_msg: "success" }, + ...fields, +}) + +/** The calls and parsed bodies MiniMax receives for each submitted request. */ +const sent = (requests: ReadonlyArray[0], "prompt">>) => + Effect.gen(function* () { + const calls: Array = [] + yield* Effect.forEach(requests, (request) => ai.video.start({ prompt: "test", ...request })).pipe( + Effect.provide( + layer((input) => + observe(calls, input).pipe( + Effect.map(() => json(input, { task_id: "task_1", base_resp: { status_code: 0, status_msg: "success" } })), + ), + ), + ), + ) + return calls.map((call) => ({ url: call.url, body: JSON.parse(call.body) })) + }) + +// --------------------------------------------------------------------------- +// H3 (v2) start +// --------------------------------------------------------------------------- + +describe("MiniMax Video / H3 start", () => { + it.effect("maps every shared field onto the v2 content array and returns the job without polling", () => + Effect.gen(function* () { + const calls: Array = [] + const job = yield* ai.video + .start({ + model: h3, + prompt: "Make the water crash down", + references: [ + Media.fromDataUrl("data:image/png;base64,AQID"), + Media.url("https://example.test/motion.mp4", { mediaType: "video/mp4" }), + Media.url("https://example.test/voice.mp3", { mediaType: "audio/mpeg" }), + Media.ref("minimax", "file_1"), + ], + duration: "6 seconds", + aspect: "16:9", + resolution: "768p", + sound: true, + providerOptions: { extra: { prompt_expansion_mode: "disabled" }, future_option: true }, + headers: { "x-request": "yes" }, + http: { query: { trace: "1" } }, + }) + .pipe( + Effect.provide( + layer((input) => + Effect.gen(function* () { + const { call } = yield* observe(calls, input) + expect(call.headers.get("authorization")).toBe("Bearer test") + expect(call.headers.get("x-request")).toBe("yes") + return json(input, { task_id: "task_1" }) + }), + ), + ), + ) + expect(job).toEqual({ id: "task_1", token }) + expect(calls.map((call) => `${call.method} ${call.url}`)).toEqual([ + "POST https://minimax.test/v2/video_generation?trace=1", + ]) + expect(JSON.parse(calls[0].body)).toEqual({ + model: "MiniMax-H3-Lite", + content: [ + { type: "text", text: "Make the water crash down" }, + { type: "image_url", image_url: { url: "data:image/png;base64,AQID" }, role: "reference_image" }, + { type: "video_url", video_url: { url: "https://example.test/motion.mp4" }, role: "reference_video" }, + { type: "audio_url", audio_url: { url: "https://example.test/voice.mp3" }, role: "reference_audio" }, + { type: "image_url", image_url: { url: "mm_file://file_1" }, role: "reference_image" }, + ], + duration: 6, + ratio: "16:9", + resolution: "768P", + extra: { prompt_expansion_mode: "disabled" }, + future_option: true, + }) + }), + ) + + it.effect("sends first and last frames by role and maps auto to adaptive", () => + Effect.gen(function* () { + const requests = yield* sent([ + { + model: h3, + start: "https://example.test/first.png", + end: "https://example.test/last.png", + aspect: "auto", + resolution: "2k", + }, + ]) + expect(requests[0].body).toEqual({ + model: "MiniMax-H3-Lite", + content: [ + { type: "text", text: "test" }, + { type: "image_url", image_url: { url: "https://example.test/first.png" }, role: "first_frame" }, + { type: "image_url", image_url: { url: "https://example.test/last.png" }, role: "last_frame" }, + ], + ratio: "adaptive", + resolution: "2K", + }) + }), + ) + + it.effect("leaves resolution, duration and ratio unset for MiniMax to require", () => + Effect.gen(function* () { + const requests = yield* sent([{ model: h3 }]) + expect(requests[0].body).toEqual({ model: "MiniMax-H3-Lite", content: [{ type: "text", text: "test" }] }) + }), + ) + + it.effect("rejects silent output, which H3 can't produce, before sending", () => + Effect.gen(function* () { + const error = yield* ai.video.start({ model: h3, prompt: "x", sound: false }).pipe(Effect.flip) + expect(error.reason._tag).toBe("UnsupportedOperation") + expect(error.message).toContain("always generates sound") + }).pipe(Effect.provide(layer(() => Effect.die("an unsupported request reached the network")))), + ) + + it.effect("returns MiniMax's HTTP errors typed", () => + Effect.gen(function* () { + const body = { + type: "error", + error: { + type: "bad_request_error", + message: "invalid params, ratio is required for t2va (text-only) and cannot be 'adaptive' (2013)", + http_code: "400", + }, + } + const error = yield* ai.video + .start({ model: h3, prompt: "x", resolution: "480p", duration: "4 seconds" }) + .pipe(Effect.flip, Effect.provide(layer((input) => Effect.succeed(json(input, body, { status: 400 }))))) + expect(error.reason._tag).toBe("InvalidRequest") + }), + ) +}) + +// --------------------------------------------------------------------------- +// Hailuo (v1) start +// --------------------------------------------------------------------------- + +describe("MiniMax Video / Hailuo start", () => { + it.effect("routes Hailuo and 01-series models to the v1 API and H3 to v2", () => + Effect.gen(function* () { + const requests = yield* sent( + ["MiniMax-Hailuo-2.3", "MiniMax-Hailuo-2.3-Fast", "I2V-01-live", "S2V-01", "MiniMax-H3", "MiniMax-H4"].map( + (id) => ({ model: minimax.video(id) }), + ), + ) + expect(requests.map((request) => request.url)).toEqual([ + "https://minimax.test/v1/video_generation", + "https://minimax.test/v1/video_generation", + "https://minimax.test/v1/video_generation", + "https://minimax.test/v1/video_generation", + "https://minimax.test/v2/video_generation", + "https://minimax.test/v2/video_generation", + ]) + }), + ) + + it.effect("drops the version from a baseURL shared with the other MiniMax APIs", () => + Effect.gen(function* () { + const requests = yield* sent( + ["https://api.minimax.cn/v1", "https://api.minimax.cn/v1/", "https://api.minimax.cn/"].flatMap((baseURL) => { + const shared = MiniMax.configure({ apiKey: "test", baseURL }) + return [{ model: shared.video("MiniMax-H3") }, { model: shared.video("MiniMax-Hailuo-2.3") }] + }), + ) + expect(requests.map((request) => request.url)).toEqual( + Array.from({ length: 3 }).flatMap(() => [ + "https://api.minimax.cn/v2/video_generation", + "https://api.minimax.cn/v1/video_generation", + ]), + ) + }), + ) + + it.effect("maps the shared fields onto v1's flat body", () => + Effect.gen(function* () { + const requests = yield* sent([ + { + model: hailuo, + start: Media.fromDataUrl("data:image/png;base64,AQID"), + end: "https://example.test/last.png", + references: ["https://example.test/face.png"], + duration: "10 seconds", + aspect: "auto", + resolution: "768p", + sound: false, + providerOptions: { prompt_optimizer: false, fast_pretreatment: true }, + }, + ]) + expect(requests[0].body).toEqual({ + model: "MiniMax-Hailuo-02", + prompt: "test", + first_frame_image: "data:image/png;base64,AQID", + last_frame_image: "https://example.test/last.png", + subject_reference: [{ type: "character", image: ["https://example.test/face.png"] }], + duration: 10, + resolution: "768P", + prompt_optimizer: false, + fast_pretreatment: true, + }) + }), + ) + + for (const rejected of [ + { + name: "a reference video", + request: { references: [Media.url("https://example.test/m.mp4", { mediaType: "video/mp4" })] }, + tag: "UnsupportedOperation", + }, + { + name: "reference audio", + request: { references: [Media.url("https://example.test/v.mp3", { mediaType: "audio/mpeg" })] }, + tag: "UnsupportedOperation", + }, + { name: "sound", request: { sound: true }, tag: "UnsupportedOperation" }, + { name: "an aspect ratio", request: { aspect: "16:9" as const }, tag: "UnsupportedOperation" }, + { name: "a MiniMax file reference", request: { start: Media.ref("minimax", "file_1") }, tag: "InvalidRequest" }, + ]) { + it.effect(`rejects ${rejected.name}, which v1 has no field for, before sending`, () => + Effect.gen(function* () { + const error = yield* ai.video.start({ model: hailuo, prompt: "x", ...rejected.request }).pipe(Effect.flip) + expect(error.reason._tag).toBe(rejected.tag) + }).pipe(Effect.provide(layer(() => Effect.die("an unsupported request reached the network")))), + ) + } + + it.effect("fails a submit that v1 answers with a non-zero base_resp", () => + Effect.gen(function* () { + const body = { + task_id: "", + base_resp: { status_code: 2013, status_msg: "invalid params, model does not support Text-to-Video mode" }, + } + const error = yield* ai.video + .start({ model: hailuo, prompt: "x" }) + .pipe(Effect.flip, Effect.provide(layer((input) => Effect.succeed(json(input, body))))) + expect(error.reason._tag).toBe("InvalidRequest") + expect(error.message).toBe( + "MiniMax Hailuo Video generation failed (2013): invalid params, model does not support Text-to-Video mode", + ) + expect(error.reason.body).toBe(JSON.stringify(body)) + }), + ) +}) + +// --------------------------------------------------------------------------- +// H3 status, result, cancel +// --------------------------------------------------------------------------- + +describe("MiniMax Video / H3 status, result and cancel", () => { + it.effect("reads statuses from a JSON round-tripped token and keeps unknown ones running", () => + Effect.gen(function* () { + const calls: Array = [] + const raw: unknown = JSON.parse(JSON.stringify(token)) + const statuses = yield* Effect.forEach( + ["queued", "running", "succeeded", "failed", "cancelled", "warming_up", "constructor"], + (status) => + ai.video + .status(h3, raw) + .pipe( + Effect.provide( + layer((input) => observe(calls, input).pipe(Effect.map(() => json(input, task({ status }))))), + ), + ), + ) + expect(statuses.map((value) => value.status)).toEqual([ + "queued", + "running", + "completed", + "failed", + "cancelled", + "running", + "running", + ]) + expect(calls[0].url).toBe("https://minimax.test/v2/query/video_generation/task_1") + }), + ) + + it.effect("returns the finished video with duration, usage and metadata", () => + Effect.gen(function* () { + const response = yield* ai.video.result(h3, token) + expect(response.video.source).toEqual({ + type: "url", + url: "https://cdn.minimax.test/out.mp4", + mediaType: "video/mp4", + }) + expect(response.video.info).toEqual({ durationSeconds: 4 }) + expect(response.usage).toEqual({ type: "seconds", seconds: 4 }) + expect(response.providerMetadata).toEqual({ + minimax: { + taskId: "task_1", + model: "MiniMax-H3-Lite", + resolution: "480P", + ratio: "16:9", + usage: { total_seconds: 4, output_seconds: 4, input_image_count: 0 }, + }, + }) + }).pipe(Effect.provide(layer((input) => Effect.succeed(json(input, succeeded))))), + ) + + it.effect("reads the flat response format too", () => + Effect.gen(function* () { + const response = yield* ai.video.result(h3, token) + expect(response.video.source).toMatchObject({ url: "https://cdn.minimax.test/out.mp4" }) + }).pipe(Effect.provide(layer((input) => Effect.succeed(json(input, succeeded.task))))), + ) + + it.effect("fails a result read before the task finishes", () => + Effect.gen(function* () { + const error = yield* ai.video.result(h3, token).pipe(Effect.flip) + expect(error.reason._tag).toBe("InvalidRequest") + expect(error.message).toContain("has not finished") + }).pipe(Effect.provide(layer((input) => Effect.succeed(json(input, task({ status: "running" })))))), + ) + + for (const terminal of [ + { + body: task({ + status: "failed", + error: { code: "1026", message: "video description contains sensitive content" }, + }), + tag: "ContentPolicy", + message: "MiniMax Video generation failed (1026): video description contains sensitive content", + }, + { + body: task({ status: "failed", error: { code: "1027", message: "output contains sensitive content" } }), + tag: "ContentPolicy", + message: "MiniMax Video generation failed (1027): output contains sensitive content", + }, + { + body: task({ status: "failed", error: { code: "2013", message: "invalid params" } }), + tag: "InvalidRequest", + message: "MiniMax Video generation failed (2013): invalid params", + }, + { + body: task({ status: "failed", error: { code: "1008", message: "insufficient balance" } }), + tag: "QuotaExceeded", + message: "MiniMax Video generation failed (1008): insufficient balance", + }, + { + body: task({ status: "failed", error: { code: "constructor", message: "future" } }), + tag: "ProviderInternal", + message: "MiniMax Video generation failed (constructor): future", + }, + { + body: task({ status: "cancelled" }), + tag: "InvalidRequest", + message: "MiniMax Video task task_1 was cancelled", + }, + { + body: task({ status: "succeeded" }), + tag: "InvalidProviderOutput", + message: "MiniMax Video completed without a video URL", + }, + ]) { + it.effect(`fails the result of ${terminal.message} with the provider body`, () => + Effect.gen(function* () { + const error = yield* ai.video.result(h3, token).pipe(Effect.flip) + expect(error.reason._tag).toBe(terminal.tag) + expect(error.message).toBe(terminal.message) + expect(error.reason.body).toBe(JSON.stringify(terminal.body)) + }).pipe(Effect.provide(layer((input) => Effect.succeed(json(input, terminal.body))))), + ) + } + + it.effect("cancels with DELETE on the task", () => + Effect.gen(function* () { + const calls: Array = [] + yield* ai.video + .cancel(h3, token) + .pipe( + Effect.provide( + layer((input) => + observe(calls, input).pipe(Effect.map(() => json(input, { task_id: "task_1", action: "cancelled" }))), + ), + ), + ) + expect(calls.map((call) => `${call.method} ${call.url}`)).toEqual([ + "DELETE https://minimax.test/v2/video_generation/task_1", + ]) + }), + ) +}) + +// --------------------------------------------------------------------------- +// Hailuo status, result, cancel +// --------------------------------------------------------------------------- + +describe("MiniMax Video / Hailuo status, result and cancel", () => { + it.effect("maps v1 statuses and keeps unknown ones running", () => + Effect.gen(function* () { + const calls: Array = [] + const statuses = yield* Effect.forEach( + ["Preparing", "Queueing", "Processing", "Success", "Fail", "Warming"], + (status) => + ai.video + .status(hailuo, token) + .pipe( + Effect.provide( + layer((input) => observe(calls, input).pipe(Effect.map(() => json(input, hailuoTask({ status }))))), + ), + ), + ) + expect(statuses.map((value) => value.status)).toEqual([ + "queued", + "queued", + "running", + "completed", + "failed", + "running", + ]) + expect(calls[0].url).toBe("https://minimax.test/v1/query/video_generation?task_id=task_1") + }), + ) + + it.effect("fails a status read that v1 answers with a non-zero base_resp", () => + Effect.gen(function* () { + const error = yield* ai.video.status(hailuo, token).pipe(Effect.flip) + expect(error.reason._tag).toBe("InvalidRequest") + expect(error.message).toContain("This task_id does not exist") + }).pipe( + Effect.provide( + layer((input) => + Effect.succeed( + json( + input, + hailuoTask({ + status: "", + file_id: "", + base_resp: { status_code: 2013, status_msg: "invalid params, This task_id does not exist" }, + }), + ), + ), + ), + ), + ), + ) + + it.effect("looks up the file's download URL, which expires after one hour", () => + Effect.gen(function* () { + const calls: Array = [] + const response = yield* ai.video.result(hailuo, token).pipe( + Effect.provide( + layer((input) => + Effect.gen(function* () { + const { call } = yield* observe(calls, input) + if (call.url.includes("/query/")) + return json( + input, + hailuoTask({ status: "Success", file_id: "file_9", video_width: 752, video_height: 1360 }), + ) + return json(input, { + file: { file_id: 9, bytes: 1024, download_url: "https://cdn.minimax.test/out.mp4?sig=1" }, + base_resp: { status_code: 0, status_msg: "success" }, + }) + }), + ), + ), + ) + expect(calls.map((call) => `${call.method} ${call.url}`)).toEqual([ + "GET https://minimax.test/v1/query/video_generation?task_id=task_1", + "GET https://minimax.test/v1/files/retrieve?file_id=file_9", + ]) + expect(calls[1].headers.get("authorization")).toBe("Bearer test") + expect(response.video.source).toEqual({ + type: "url", + url: "https://cdn.minimax.test/out.mp4?sig=1", + mediaType: "video/mp4", + expiresAt: 60 * 60 * 1000, + }) + expect(response.video.info).toEqual({ width: 752, height: 1360 }) + expect(response.providerMetadata).toEqual({ minimax: { taskId: "task_1", fileId: "file_9" } }) + }), + ) + + for (const terminal of [ + { + body: hailuoTask({ status: "Fail", base_resp: { status_code: 1027, status_msg: "output sensitive" } }), + tag: "ContentPolicy", + message: "MiniMax Hailuo Video generation failed (1027): output sensitive", + }, + { + body: hailuoTask({ status: "Fail" }), + tag: "ProviderInternal", + message: "MiniMax Hailuo Video generation failed", + }, + { + body: hailuoTask({ status: "Processing" }), + tag: "InvalidRequest", + message: "MiniMax Hailuo Video generation task_1 has not finished; await it before reading the result", + }, + { + body: hailuoTask({ status: "Success", file_id: "" }), + tag: "InvalidProviderOutput", + message: "MiniMax Hailuo Video completed without a file_id", + }, + ]) { + it.effect(`fails the result with "${terminal.message}"`, () => + Effect.gen(function* () { + const error = yield* ai.video.result(hailuo, token).pipe(Effect.flip) + expect(error.reason._tag).toBe(terminal.tag) + expect(error.message).toBe(terminal.message) + expect(error.reason.body).toBe(JSON.stringify(terminal.body)) + }).pipe(Effect.provide(layer((input) => Effect.succeed(json(input, terminal.body))))), + ) + } + + it.effect("fails cancel as unsupported without sending a request, since v1 has no cancel endpoint", () => + Effect.gen(function* () { + const error = yield* ai.video.cancel(hailuo, token).pipe(Effect.flip) + expect(error.reason._tag).toBe("UnsupportedOperation") + expect(error.message).toContain("cancel") + }).pipe(Effect.provide(layer(() => Effect.die("cancel reached the network")))), + ) +}) diff --git a/packages/ai/test/provider/media-entrypoints.test.ts b/packages/ai/test/provider/media-entrypoints.test.ts index fb00a8ac75c9..8555be691f94 100644 --- a/packages/ai/test/provider/media-entrypoints.test.ts +++ b/packages/ai/test/provider/media-entrypoints.test.ts @@ -533,6 +533,24 @@ const videos: ReadonlyArray> = [ auth: ["authorization", "Bearer ark-key"], provider: "volcengine", }, + { + name: "minimax", + load: async () => { + const { model } = await import("@opencode/ai/providers/minimax/videos") + return model("MiniMax-H3-Lite", { ...shared, apiKey: "minimax-key" }) + }, + url: "https://api.minimax.io/v2/video_generation", + auth: ["authorization", "Bearer minimax-key"], + }, + { + name: "minimax (minimax-cn, Hailuo)", + load: async () => { + const { model } = await import("@opencode/ai/providers/minimax/videos") + return model("MiniMax-Hailuo-2.3", { ...shared, apiKey: "minimax-key", baseURL: "https://api.minimax.cn" }) + }, + url: "https://api.minimax.cn/v1/video_generation", + auth: ["authorization", "Bearer minimax-key"], + }, ] describe("video entrypoints", () => { diff --git a/packages/ai/test/provider/minimax-video.recorded.test.ts b/packages/ai/test/provider/minimax-video.recorded.test.ts new file mode 100644 index 000000000000..c8bf453a3224 --- /dev/null +++ b/packages/ai/test/provider/minimax-video.recorded.test.ts @@ -0,0 +1,83 @@ +import { describe, expect } from "bun:test" +import { Effect, Schedule } from "effect" +import { ai, type VideoModel, type VideoStatus } from "../../src/effect.js" +import { MiniMax } from "../../src/providers.js" +import { recordedTests } from "../recorded-test.js" +import { queuedPoll } from "./queued-recording.js" + +const minimax = MiniMax.configure({ apiKey: process.env.MINIMAX_API_KEY ?? "fixture" }) + +const recorded = recordedTests({ + prefix: "minimax-video", + provider: "minimax", + protocol: "minimax-video", + requires: ["MINIMAX_API_KEY"], +}) + +const settle = (model: VideoModel, token: unknown) => + Effect.gen(function* () { + const observed: Array = [] + yield* ai.video.status(model, token).pipe( + Effect.tap((status) => Effect.sync(() => observed.push(status))), + Effect.repeat({ + schedule: Schedule.spaced(queuedPoll.interval ?? "10 millis"), + until: (status) => status.status !== "queued" && status.status !== "running", + }), + ) + return observed + }) + +describe("MiniMax Video recorded", () => { + recorded.effect( + "generates an H3 video", + () => + Effect.gen(function* () { + const model = minimax.video("MiniMax-H3-Lite") + // H3 needs resolution and duration, and a ratio when the prompt is the only input. + const job = yield* ai.video.start({ + model, + prompt: "A single red balloon drifting slowly upward against a clear blue sky.", + aspect: "16:9", + resolution: "480p", + duration: "4 seconds", + }) + const observed = yield* settle(model, job.token) + expect(observed.at(-1)?.status).toBe("completed") + + const response = yield* ai.video.result(model, job.token) + expect(response.videos).toHaveLength(1) + expect(response.video.source.type).toBe("url") + expect(response.video.info?.durationSeconds).toBe(4) + expect(response.usage).toEqual({ type: "seconds", seconds: 4 }) + expect(response.providerMetadata?.minimax).toMatchObject({ model: "MiniMax-H3-Lite", resolution: "480P" }) + }), + { timeout: 15 * 60 * 1000 }, + ) + + recorded.effect( + "generates a Hailuo video from a first frame", + () => + Effect.gen(function* () { + const model = minimax.video("MiniMax-Hailuo-02") + const job = yield* ai.video.start({ + model, + prompt: "The woman slowly turns her head and smiles.", + // MiniMax's own documentation portrait. + start: + "https://cdn.hailuoai.com/prod/2025-08-12-17/video_cover/1754990600020238321-411603868533342214-cover.jpg", + resolution: "512p", + duration: "6 seconds", + }) + const observed = yield* settle(model, job.token) + expect(observed.at(-1)?.status).toBe("completed") + + // The task names a file; the result looks up its one-hour download URL. + const response = yield* ai.video.result(model, job.token) + expect(response.videos).toHaveLength(1) + expect(response.video.source.type).toBe("url") + expect(response.video.expiresAt).toBeGreaterThan(0) + expect(response.providerMetadata?.minimax).toMatchObject({ taskId: job.id }) + }), + { timeout: 15 * 60 * 1000 }, + ) +}) diff --git a/packages/codemode/src/tool-runtime.ts b/packages/codemode/src/tool-runtime.ts index 8910a44c6b83..fc6925576e56 100644 --- a/packages/codemode/src/tool-runtime.ts +++ b/packages/codemode/src/tool-runtime.ts @@ -545,7 +545,24 @@ const resolve = (root: ToolNode, path: ReadonlyArray, index: Reado ) } if (node.tool === undefined) { - throw new ToolRuntimeError("UnknownTool", `Tool '${segments.join(".")}' is not callable.`) + const name = segments.join(".") + const under = index.filter((entry) => entry.description.path.startsWith(`${name}.`)) + const ranked = rank(under, segments.at(-1) ?? "", index) + const shown = [...ranked, ...under.filter((entry) => !ranked.includes(entry))] + .slice(0, 3) + .map((entry) => toolExpression(entry.description.path)) + const more = under.length - shown.length + throw new ToolRuntimeError( + "UnknownTool", + shown.length === 0 + ? `Tool '${name}' is not callable.` + : `Tool '${name}' is not callable, it is a namespace. Did you mean ${new Intl.ListFormat("en", { type: "disjunction" }).format(shown)}?`, + [ + more > 0 + ? `${more} more under '${name}'. Use search({ namespace: ${JSON.stringify(name)} }) to list them.` + : "Use search to find available tools.", + ], + ) } return node.tool } diff --git a/packages/codemode/test/tool-paths.test.ts b/packages/codemode/test/tool-paths.test.ts index a739d6515109..33529514659c 100644 --- a/packages/codemode/test/tool-paths.test.ts +++ b/packages/codemode/test/tool-paths.test.ts @@ -118,7 +118,21 @@ describe("callable namespaces", () => { const nested = CodeMode.make({ tools: { "issues.list": echo("List issues", "list") } }) const diagnostic = await failure(nested, `return await tools.issues({})`) expect(diagnostic.kind).toBe("UnknownTool") - expect(diagnostic.message).toContain("Tool 'issues' is not callable") + expect(diagnostic.message).toBe("Tool 'issues' is not callable, it is a namespace. Did you mean tools.issues.list?") + expect(diagnostic.suggestions).toEqual(["Use search to find available tools."]) + }) + + test("calling a namespace names a few of its tools and counts the rest", async () => { + const nested = CodeMode.make({ + tools: Object.fromEntries( + ["archive", "create", "get", "issues", "list"].map((name) => [`issues.${name}`, echo(`Issue ${name}`, name)]), + ), + }) + const diagnostic = await failure(nested, `return await tools.issues({})`) + expect(diagnostic.message).toBe( + "Tool 'issues' is not callable, it is a namespace. Did you mean tools.issues.issues, tools.issues.archive, or tools.issues.create?", + ) + expect(diagnostic.suggestions).toEqual([`2 more under 'issues'. Use search({ namespace: "issues" }) to list them.`]) }) }) diff --git a/packages/core/src/provider.ts b/packages/core/src/provider.ts index 98d1ab3d6926..3ce9787c90cd 100644 --- a/packages/core/src/provider.ts +++ b/packages/core/src/provider.ts @@ -117,6 +117,7 @@ const builtins = new Map Promise>([ ["@opencode/ai/providers/minimax/images", () => import("@opencode/ai/providers/minimax/images")], ["@opencode/ai/providers/minimax/messages", () => import("@opencode/ai/providers/minimax/messages")], ["@opencode/ai/providers/minimax/responses", () => import("@opencode/ai/providers/minimax/responses")], + ["@opencode/ai/providers/minimax/videos", () => import("@opencode/ai/providers/minimax/videos")], ["@opencode/ai/providers/mistral", () => import("@opencode/ai/providers/mistral")], ["@opencode/ai/providers/moonshot", () => import("@opencode/ai/providers/moonshot")], ["@opencode/ai/providers/moonshot/chat", () => import("@opencode/ai/providers/moonshot/chat")], diff --git a/packages/plugin/src/effect/index.ts b/packages/plugin/src/effect/index.ts index 952ce71d24b0..c14c60caeb6c 100644 --- a/packages/plugin/src/effect/index.ts +++ b/packages/plugin/src/effect/index.ts @@ -15,6 +15,7 @@ export { Provider } from "@opencode/schema/provider" export { Reference } from "@opencode/schema/reference" export { Rpc } from "@opencode/schema/rpc" export { Skill } from "@opencode/schema/skill" +export { Tool } from "@opencode/schema/tool" export { Vcs } from "@opencode/schema/vcs" export { WebSearch } from "@opencode/schema/websearch" export { Worktree } from "@opencode/schema/worktree"