@opencode/ai 2.2.7 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +114 -96
- package/dist/effect.d.ts +8 -7
- package/dist/effect.js +2 -1
- package/dist/experimental.d.ts +0 -4
- package/dist/experimental.js +0 -3
- package/dist/image-client.d.ts +1 -1
- package/dist/image-client.js +2 -6
- package/dist/index.d.ts +11 -48
- package/dist/index.js +30 -13
- package/dist/media-model.d.ts +2 -0
- package/dist/media-model.js +3 -1
- package/dist/media.d.ts +9 -3
- package/dist/media.js +27 -7
- package/dist/protocols/google-decisions.d.ts +13 -0
- package/dist/protocols/google-decisions.js +133 -0
- package/dist/protocols/google-interactions-video.d.ts +43 -0
- package/dist/protocols/google-interactions-video.js +186 -0
- package/dist/protocols/google-interactions.d.ts +14 -1
- package/dist/protocols/google-interactions.js +1 -1
- package/dist/protocols/index.d.ts +1 -0
- package/dist/protocols/index.js +1 -0
- package/dist/protocols/minimax-video.d.ts +54 -0
- package/dist/protocols/minimax-video.js +292 -0
- package/dist/protocols/openrouter-video.d.ts +41 -0
- package/dist/protocols/openrouter-video.js +138 -0
- package/dist/protocols/seedance-video.d.ts +50 -0
- package/dist/protocols/seedance-video.js +216 -0
- package/dist/protocols/system-one.d.ts +45 -1
- package/dist/protocols/system-one.js +42 -7
- package/dist/protocols/utils/responses-compaction.js +1 -1
- package/dist/protocols/xai-video.d.ts +8 -2
- package/dist/protocols/xai-video.js +20 -15
- package/dist/provider-error.js +7 -1
- package/dist/providers/byteplus/videos.d.ts +2 -0
- package/dist/providers/byteplus/videos.js +1 -0
- package/dist/providers/byteplus.d.ts +26 -0
- package/dist/providers/byteplus.js +21 -0
- package/dist/providers/google/decisions.d.ts +2 -0
- package/dist/providers/google/decisions.js +1 -0
- package/dist/providers/google/videos.d.ts +2 -0
- package/dist/providers/google/videos.js +1 -0
- package/dist/providers/google.d.ts +11 -4
- package/dist/providers/google.js +13 -3
- package/dist/providers/index.d.ts +2 -1
- package/dist/providers/index.js +2 -1
- package/dist/providers/minimax/videos.d.ts +3 -0
- package/dist/providers/minimax/videos.js +1 -0
- package/dist/providers/minimax.d.ts +6 -1
- package/dist/providers/minimax.js +10 -1
- package/dist/providers/openai-compatible/decisions.d.ts +2 -0
- package/dist/providers/openai-compatible/decisions.js +1 -0
- package/dist/providers/openai-compatible.d.ts +2 -0
- package/dist/providers/openai-compatible.js +14 -0
- package/dist/providers/openrouter/videos.d.ts +2 -0
- package/dist/providers/openrouter/videos.js +1 -0
- package/dist/providers/openrouter.d.ts +5 -0
- package/dist/providers/openrouter.js +8 -0
- package/dist/providers/volcengine/videos.d.ts +2 -0
- package/dist/providers/volcengine/videos.js +1 -0
- package/dist/providers/volcengine.d.ts +26 -0
- package/dist/providers/volcengine.js +21 -0
- package/dist/public.d.ts +2 -0
- package/dist/public.js +1 -0
- package/dist/route/executor-service.d.ts +6 -0
- package/dist/route/executor-service.js +7 -0
- package/dist/route/executor.d.ts +7 -1
- package/dist/route/executor.js +7 -2
- package/dist/route/media-protocol.d.ts +12 -11
- package/dist/route/media.d.ts +2 -0
- package/dist/route/media.js +31 -22
- package/dist/route/transport/http.js +1 -1
- package/dist/schema/messages.d.ts +1 -1
- package/dist/schema/messages.js +1 -5
- package/dist/schema/options.d.ts +8 -0
- package/dist/schema/options.js +1 -0
- package/dist/speech-client.d.ts +1 -1
- package/dist/speech-client.js +4 -4
- package/dist/transcription-client.d.ts +1 -1
- package/dist/transcription-client.js +1 -2
- package/dist/video-client.d.ts +16 -84
- package/dist/video-client.js +28 -13
- package/dist/video.d.ts +56 -1072
- package/dist/video.js +28 -65
- package/package.json +55 -7
- package/dist/protocols/google-video.d.ts +0 -26
- package/dist/protocols/google-video.js +0 -146
- package/dist/protocols/runway-video.d.ts +0 -38
- package/dist/protocols/runway-video.js +0 -141
- package/dist/providers/runway.d.ts +0 -20
- package/dist/providers/runway.js +0 -17
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { Duration, Effect, Schema } from "effect";
|
|
2
|
+
import { Media } from "../media.js";
|
|
3
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
|
+
import { MediaRoute } from "../route/media.js";
|
|
5
|
+
import { AIError, QuotaExceededError, mergeJsonRecords } from "../schema/index.js";
|
|
6
|
+
import { VideoModel, VideoResponse, seconds } from "../video.js";
|
|
7
|
+
import { ProviderShared, optionalNull } from "./shared.js";
|
|
8
|
+
// MiniMax runs two video APIs with different bodies, statuses and results: H3 on v2 and Hailuo on v1.
|
|
9
|
+
const route = MediaProtocol.identity({ id: "minimax-video", name: "MiniMax Video", provider: "minimax" });
|
|
10
|
+
const hailuoRoute = MediaProtocol.identity({
|
|
11
|
+
id: "minimax-hailuo-video",
|
|
12
|
+
name: "MiniMax Hailuo Video",
|
|
13
|
+
provider: "minimax",
|
|
14
|
+
});
|
|
15
|
+
/** The API host without a version: H3 is served under `/v2` and Hailuo under `/v1`. */
|
|
16
|
+
export const DEFAULT_BASE_URL = "https://api.minimax.io";
|
|
17
|
+
export const PATH = "/v2/video_generation";
|
|
18
|
+
export const STATUS_PATH = "/v2/query/video_generation";
|
|
19
|
+
export const HAILUO_PATH = "/v1/video_generation";
|
|
20
|
+
export const HAILUO_STATUS_PATH = "/v1/query/video_generation";
|
|
21
|
+
export const FILE_PATH = "/v1/files/retrieve";
|
|
22
|
+
const DOWNLOAD_RETENTION = Duration.hours(1);
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// 2. Token and response schemas
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
export const Token = Schema.Struct({ taskID: Schema.String });
|
|
27
|
+
const BaseResp = optionalNull(Schema.Struct({
|
|
28
|
+
status_code: Schema.Number,
|
|
29
|
+
status_msg: optionalNull(Schema.String),
|
|
30
|
+
}));
|
|
31
|
+
const StartResponse = Schema.Struct({ task_id: Schema.String });
|
|
32
|
+
const Task = Schema.Struct({
|
|
33
|
+
id: optionalNull(Schema.String),
|
|
34
|
+
model: optionalNull(Schema.String),
|
|
35
|
+
status: optionalNull(Schema.String),
|
|
36
|
+
error: optionalNull(Schema.Struct({
|
|
37
|
+
code: optionalNull(Schema.Union([Schema.String, Schema.Number])),
|
|
38
|
+
message: optionalNull(Schema.String),
|
|
39
|
+
})),
|
|
40
|
+
content: optionalNull(Schema.Struct({ url: optionalNull(Schema.String) })),
|
|
41
|
+
resolution: optionalNull(Schema.String),
|
|
42
|
+
duration: optionalNull(Schema.Number),
|
|
43
|
+
ratio: optionalNull(Schema.String),
|
|
44
|
+
usage: optionalNull(Schema.Record(Schema.String, Schema.Unknown)),
|
|
45
|
+
});
|
|
46
|
+
// `response_format: "flat"` moves the task fields from `task` to the top level.
|
|
47
|
+
const TaskResponse = Schema.Struct({ ...Task.fields, task: optionalNull(Task) });
|
|
48
|
+
const HailuoStartResponse = Schema.Struct({ task_id: optionalNull(Schema.String), base_resp: BaseResp });
|
|
49
|
+
const HailuoTask = Schema.Struct({
|
|
50
|
+
status: optionalNull(Schema.String),
|
|
51
|
+
file_id: optionalNull(Schema.String),
|
|
52
|
+
video_width: optionalNull(Schema.Number),
|
|
53
|
+
video_height: optionalNull(Schema.Number),
|
|
54
|
+
base_resp: BaseResp,
|
|
55
|
+
});
|
|
56
|
+
const HailuoFile = Schema.Struct({
|
|
57
|
+
file: optionalNull(Schema.Struct({
|
|
58
|
+
download_url: optionalNull(Schema.String),
|
|
59
|
+
})),
|
|
60
|
+
base_resp: BaseResp,
|
|
61
|
+
});
|
|
62
|
+
const STATUS = {
|
|
63
|
+
queued: "queued",
|
|
64
|
+
running: "running",
|
|
65
|
+
succeeded: "completed",
|
|
66
|
+
failed: "failed",
|
|
67
|
+
cancelled: "cancelled",
|
|
68
|
+
};
|
|
69
|
+
const HAILUO_STATUS = {
|
|
70
|
+
Preparing: "queued",
|
|
71
|
+
Queueing: "queued",
|
|
72
|
+
Processing: "running",
|
|
73
|
+
Success: "completed",
|
|
74
|
+
Fail: "failed",
|
|
75
|
+
};
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// 3. Failures
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
// Error codes from https://platform.minimax.io/docs/api-reference/errorcode; unknown codes are provider-side.
|
|
80
|
+
const FAILURE = {
|
|
81
|
+
1002: "RateLimit",
|
|
82
|
+
1004: "Authentication",
|
|
83
|
+
1039: "RateLimit",
|
|
84
|
+
2013: "InvalidRequest",
|
|
85
|
+
2049: "Authentication",
|
|
86
|
+
};
|
|
87
|
+
const INSUFFICIENT_BALANCE = 1008;
|
|
88
|
+
// 1026 flags the input and 1027 the generated video.
|
|
89
|
+
const CONTENT_POLICY = new Set(["1026", "1027"]);
|
|
90
|
+
const failed = (output, name, code, detail) => {
|
|
91
|
+
const message = `${name} generation failed${code === undefined ? "" : ` (${code})`}${detail === undefined ? "" : `: ${detail}`}`;
|
|
92
|
+
if (code !== undefined && CONTENT_POLICY.has(String(code)))
|
|
93
|
+
return output.contentPolicy(message);
|
|
94
|
+
if (code !== undefined && Number(code) === INSUFFICIENT_BALANCE)
|
|
95
|
+
return new AIError({ reason: new QuotaExceededError({ message, body: output.body, http: output.http }) });
|
|
96
|
+
return output.ended("failed", message, MediaProtocol.failure(FAILURE, code));
|
|
97
|
+
};
|
|
98
|
+
// v1 answers errors with HTTP 200 and a non-zero `base_resp.status_code`.
|
|
99
|
+
const baseError = (output, base) => base === undefined || base === null || base.status_code === 0
|
|
100
|
+
? undefined
|
|
101
|
+
: failed(output, hailuoRoute.name, base.status_code, base.status_msg ?? undefined);
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
// 5. Request body construction
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
/** H3 takes URLs, data URIs and `mm_file://{file_id}` references to MiniMax files. */
|
|
106
|
+
const mediaUrl = (asset) => ProviderShared.mediaReference(asset, route.provider, route.name).pipe(Effect.map((reference) => (reference.type === "ref" ? `mm_file://${reference.value}` : reference.value)));
|
|
107
|
+
const ROLES = { image: "reference_image", video: "reference_video", audio: "reference_audio" };
|
|
108
|
+
const item = (asset, role) => mediaUrl(asset).pipe(Effect.map((url) => {
|
|
109
|
+
const type = asset.kind === "video" ? "video_url" : asset.kind === "audio" ? "audio_url" : "image_url";
|
|
110
|
+
return { type, [type]: { url }, role };
|
|
111
|
+
}));
|
|
112
|
+
const reference = (asset) => item(asset, asset.kind === "video" || asset.kind === "audio" ? ROLES[asset.kind] : ROLES.image);
|
|
113
|
+
/** MiniMax spells resolutions in uppercase (`480P`, `768P`, `2K`). */
|
|
114
|
+
const resolution = (request) => request.resolution?.toUpperCase();
|
|
115
|
+
const fromRequest = Effect.fn("MiniMaxVideo.fromRequest")(function* (request) {
|
|
116
|
+
// H3 always generates a soundtrack and has no field to turn it off.
|
|
117
|
+
if (request.sound === false)
|
|
118
|
+
return yield* route.unsupported("video.sound", `${route.name} always generates sound and can't be silenced`);
|
|
119
|
+
const content = [
|
|
120
|
+
{ type: "text", text: request.prompt },
|
|
121
|
+
...(request.start === undefined ? [] : [yield* item(request.start, "first_frame")]),
|
|
122
|
+
...(request.end === undefined ? [] : [yield* item(request.end, "last_frame")]),
|
|
123
|
+
...(yield* Effect.forEach(request.references ?? [], reference)),
|
|
124
|
+
];
|
|
125
|
+
return MediaProtocol.json(mergeJsonRecords({
|
|
126
|
+
model: request.model.id,
|
|
127
|
+
content,
|
|
128
|
+
resolution: resolution(request),
|
|
129
|
+
duration: request.duration === undefined ? undefined : seconds(request.duration),
|
|
130
|
+
ratio: request.aspect === "auto" ? "adaptive" : request.aspect,
|
|
131
|
+
}, request.providerOptions, request.http?.body) ?? {});
|
|
132
|
+
});
|
|
133
|
+
/** v1 documents URLs and data URLs only, so MiniMax file references aren't sent. */
|
|
134
|
+
const hailuoUrl = (asset) => ProviderShared.mediaReference(asset, undefined, hailuoRoute.name).pipe(Effect.map((reference) => reference.value));
|
|
135
|
+
const hailuoFromRequest = Effect.fn("MiniMaxHailuoVideo.fromRequest")(function* (request) {
|
|
136
|
+
const references = request.references ?? [];
|
|
137
|
+
if (references.some((asset) => asset.kind === "video" || asset.kind === "audio"))
|
|
138
|
+
return yield* hailuoRoute.unsupported("video.references", `${hailuoRoute.name} takes only image (subject) references`);
|
|
139
|
+
// Hailuo output is always silent and its shape follows the model or the first frame, with no field for either.
|
|
140
|
+
if (request.sound === true)
|
|
141
|
+
return yield* hailuoRoute.unsupported("video.sound", `${hailuoRoute.name} generates silent video only`);
|
|
142
|
+
if (request.aspect !== undefined && request.aspect !== "auto")
|
|
143
|
+
return yield* hailuoRoute.unsupported("video.aspect", `${hailuoRoute.name} has no aspect ratio input`);
|
|
144
|
+
const subjects = yield* Effect.forEach(references, (asset) => hailuoUrl(asset).pipe(Effect.map((url) => ({ type: "character", image: [url] }))));
|
|
145
|
+
return MediaProtocol.json(mergeJsonRecords({
|
|
146
|
+
model: request.model.id,
|
|
147
|
+
prompt: request.prompt,
|
|
148
|
+
first_frame_image: request.start === undefined ? undefined : yield* hailuoUrl(request.start),
|
|
149
|
+
last_frame_image: request.end === undefined ? undefined : yield* hailuoUrl(request.end),
|
|
150
|
+
subject_reference: subjects.length === 0 ? undefined : subjects,
|
|
151
|
+
duration: request.duration === undefined ? undefined : seconds(request.duration),
|
|
152
|
+
resolution: resolution(request),
|
|
153
|
+
}, request.providerOptions, request.http?.body) ?? {});
|
|
154
|
+
});
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
// 6. Response decoding
|
|
157
|
+
// ---------------------------------------------------------------------------
|
|
158
|
+
const decodeStart = route.decodeStarted(StartResponse, (value) => ({
|
|
159
|
+
token: { taskID: value.task_id },
|
|
160
|
+
snapshot: { id: value.task_id, status: "queued" },
|
|
161
|
+
}));
|
|
162
|
+
const decodeTask = route.decodeJson(TaskResponse);
|
|
163
|
+
const decodeStatus = Effect.fn("MiniMaxVideo.decodeStatus")(function* (response, context) {
|
|
164
|
+
const output = yield* decodeTask(response);
|
|
165
|
+
const task = output.value.task ?? output.value;
|
|
166
|
+
return { id: context.token.taskID, status: MediaProtocol.status(STATUS, task.status ?? "") };
|
|
167
|
+
});
|
|
168
|
+
const decodeResult = Effect.fn("MiniMaxVideo.decodeResult")(function* (response, context) {
|
|
169
|
+
const output = yield* decodeTask(response);
|
|
170
|
+
const task = output.value.task ?? output.value;
|
|
171
|
+
const status = MediaProtocol.status(STATUS, task.status ?? "");
|
|
172
|
+
if (status === "queued" || status === "running")
|
|
173
|
+
return yield* output.pending(context.token.taskID);
|
|
174
|
+
if (status === "failed")
|
|
175
|
+
return yield* failed(output, route.name, task.error?.code ?? undefined, task.error?.message ?? undefined);
|
|
176
|
+
if (status !== "completed")
|
|
177
|
+
return yield* output.ended("cancelled", `${route.name} task ${context.token.taskID} was cancelled`);
|
|
178
|
+
const url = task.content?.url ?? undefined;
|
|
179
|
+
if (url === undefined)
|
|
180
|
+
return yield* output.invalid(`${route.name} completed without a video URL`);
|
|
181
|
+
const duration = task.duration ?? undefined;
|
|
182
|
+
const total = task.usage?.total_seconds;
|
|
183
|
+
return new VideoResponse({
|
|
184
|
+
// The URL is time-limited without a documented window; reading the result again returns a fresh one.
|
|
185
|
+
videos: [
|
|
186
|
+
Media.url(url, {
|
|
187
|
+
mediaType: "video/mp4",
|
|
188
|
+
info: duration === undefined ? undefined : { durationSeconds: duration },
|
|
189
|
+
}),
|
|
190
|
+
],
|
|
191
|
+
usage: typeof total === "number" ? { type: "seconds", seconds: total } : undefined,
|
|
192
|
+
providerMetadata: {
|
|
193
|
+
minimax: {
|
|
194
|
+
taskId: context.token.taskID,
|
|
195
|
+
model: task.model ?? undefined,
|
|
196
|
+
resolution: task.resolution ?? undefined,
|
|
197
|
+
ratio: task.ratio ?? undefined,
|
|
198
|
+
usage: task.usage ?? undefined,
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
const decodeHailuoStart = Effect.fn("MiniMaxHailuoVideo.decodeStart")(function* (response) {
|
|
204
|
+
const output = yield* hailuoRoute.decodeJson(HailuoStartResponse)(response);
|
|
205
|
+
const error = baseError(output, output.value.base_resp);
|
|
206
|
+
if (error)
|
|
207
|
+
return yield* error;
|
|
208
|
+
const taskID = output.value.task_id ?? "";
|
|
209
|
+
if (taskID === "")
|
|
210
|
+
return yield* output.invalid(`${hailuoRoute.name} returned no task_id`);
|
|
211
|
+
return { token: { taskID }, snapshot: { id: taskID, status: "queued" } };
|
|
212
|
+
});
|
|
213
|
+
const decodeHailuoTask = hailuoRoute.decodeJson(HailuoTask);
|
|
214
|
+
const decodeHailuoStatus = Effect.fn("MiniMaxHailuoVideo.decodeStatus")(function* (response, context) {
|
|
215
|
+
const output = yield* decodeHailuoTask(response);
|
|
216
|
+
const status = MediaProtocol.status(HAILUO_STATUS, output.value.status ?? "");
|
|
217
|
+
// A failed task also carries its reason in `base_resp`; any other non-zero code is a failed read.
|
|
218
|
+
const error = status === "failed" ? undefined : baseError(output, output.value.base_resp);
|
|
219
|
+
if (error)
|
|
220
|
+
return yield* error;
|
|
221
|
+
return { id: context.token.taskID, status };
|
|
222
|
+
});
|
|
223
|
+
const decodeHailuoFile = hailuoRoute.decodeJson(HailuoFile);
|
|
224
|
+
const decodeHailuoResult = Effect.fn("MiniMaxHailuoVideo.decodeResult")(function* (response, context) {
|
|
225
|
+
const output = yield* decodeHailuoTask(response);
|
|
226
|
+
const task = output.value;
|
|
227
|
+
const status = MediaProtocol.status(HAILUO_STATUS, task.status ?? "");
|
|
228
|
+
const error = baseError(output, task.base_resp);
|
|
229
|
+
if (status === "failed")
|
|
230
|
+
return yield* error ?? failed(output, hailuoRoute.name, undefined, undefined);
|
|
231
|
+
if (error)
|
|
232
|
+
return yield* error;
|
|
233
|
+
if (status !== "completed")
|
|
234
|
+
return yield* output.pending(context.token.taskID);
|
|
235
|
+
const fileID = task.file_id ?? "";
|
|
236
|
+
if (fileID === "")
|
|
237
|
+
return yield* output.invalid(`${hailuoRoute.name} completed without a file_id`);
|
|
238
|
+
// The task names a file; its download URL takes a second lookup and is valid for one hour.
|
|
239
|
+
const file = yield* decodeHailuoFile(yield* context.get(`${FILE_PATH}?file_id=${encodeURIComponent(fileID)}`));
|
|
240
|
+
const fileError = baseError(file, file.value.base_resp);
|
|
241
|
+
if (fileError)
|
|
242
|
+
return yield* fileError;
|
|
243
|
+
const url = file.value.file?.download_url ?? undefined;
|
|
244
|
+
if (url === undefined)
|
|
245
|
+
return yield* file.invalid(`${hailuoRoute.name} returned no download_url for ${fileID}`);
|
|
246
|
+
const width = task.video_width ?? undefined;
|
|
247
|
+
const height = task.video_height ?? undefined;
|
|
248
|
+
return new VideoResponse({
|
|
249
|
+
videos: [
|
|
250
|
+
yield* MediaProtocol.expiringUrl(url, DOWNLOAD_RETENTION, {
|
|
251
|
+
mediaType: "video/mp4",
|
|
252
|
+
info: width === undefined && height === undefined ? undefined : { width, height },
|
|
253
|
+
}),
|
|
254
|
+
],
|
|
255
|
+
providerMetadata: {
|
|
256
|
+
minimax: { taskId: context.token.taskID, fileId: fileID },
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
// ---------------------------------------------------------------------------
|
|
261
|
+
// 7. Protocol and route
|
|
262
|
+
// ---------------------------------------------------------------------------
|
|
263
|
+
export const protocol = MediaProtocol.queued(route, {
|
|
264
|
+
token: Token,
|
|
265
|
+
start: { body: { from: fromRequest }, decode: decodeStart },
|
|
266
|
+
status: { path: (token) => `${STATUS_PATH}/${token.taskID}`, decode: decodeStatus },
|
|
267
|
+
result: { path: (token) => `${STATUS_PATH}/${token.taskID}`, decode: decodeResult },
|
|
268
|
+
// Cancels a queued task; MiniMax rejects it once the task is running.
|
|
269
|
+
cancel: { method: "DELETE", path: (token) => `${PATH}/${token.taskID}` },
|
|
270
|
+
});
|
|
271
|
+
const hailuoStatusPath = (token) => `${HAILUO_STATUS_PATH}?task_id=${encodeURIComponent(token.taskID)}`;
|
|
272
|
+
export const hailuoProtocol = MediaProtocol.queued(hailuoRoute, {
|
|
273
|
+
token: Token,
|
|
274
|
+
start: { body: { from: hailuoFromRequest }, decode: decodeHailuoStart },
|
|
275
|
+
status: { path: hailuoStatusPath, decode: decodeHailuoStatus },
|
|
276
|
+
result: { path: hailuoStatusPath, decode: decodeHailuoResult },
|
|
277
|
+
});
|
|
278
|
+
/** H3 and later models on the v2 API. */
|
|
279
|
+
export const h3Model = (input) => VideoModel.fromRoute({ protocol, baseURL: DEFAULT_BASE_URL, path: PATH }, input);
|
|
280
|
+
/** Hailuo and the `*-01` models on the v1 API. */
|
|
281
|
+
export const hailuoModel = (input) => VideoModel.fromRoute({ protocol: hailuoProtocol, baseURL: DEFAULT_BASE_URL, path: HAILUO_PATH }, input);
|
|
282
|
+
// v1 is MiniMax's legacy API with a fixed model list; every newer model ships on v2.
|
|
283
|
+
const isHailuo = (modelID) => /^(minimax-hailuo-|[tis]2v-01|video-01)/i.test(modelID);
|
|
284
|
+
/** Picks the API by model family: Hailuo and `*-01` models use v1, everything else v2. */
|
|
285
|
+
export const model = (input) => (isHailuo(input.id) ? hailuoModel(input) : h3Model(input));
|
|
286
|
+
export const MiniMaxVideo = {
|
|
287
|
+
protocol,
|
|
288
|
+
hailuoProtocol,
|
|
289
|
+
model,
|
|
290
|
+
h3Model,
|
|
291
|
+
hailuoModel,
|
|
292
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
|
+
import { MediaRoute } from "../route/media.js";
|
|
4
|
+
import { VideoModel, VideoResponse, type VideoRequest } from "../video.js";
|
|
5
|
+
export declare const DEFAULT_BASE_URL = "https://openrouter.ai/api/v1";
|
|
6
|
+
export declare const PATH = "/videos";
|
|
7
|
+
/**
|
|
8
|
+
* Provider-native options. Common fields (`start`, `end`, `references`, `duration`, `aspect`, `resolution`, `sound`)
|
|
9
|
+
* live on the request. `provider` routes the request and forwards the upstream provider's own parameters under
|
|
10
|
+
* `provider.options.<slug>` (see each model's `allowed_passthrough_parameters`); `previous_job_id` edits or extends a
|
|
11
|
+
* finished OpenRouter job on the same model.
|
|
12
|
+
*/
|
|
13
|
+
export type OpenRouterVideoOptions = {
|
|
14
|
+
readonly provider?: {
|
|
15
|
+
readonly order?: ReadonlyArray<string>;
|
|
16
|
+
readonly only?: ReadonlyArray<string>;
|
|
17
|
+
readonly ignore?: ReadonlyArray<string>;
|
|
18
|
+
readonly options?: Readonly<Record<string, Record<string, unknown>>>;
|
|
19
|
+
} & Record<string, unknown>;
|
|
20
|
+
readonly previous_job_id?: string;
|
|
21
|
+
readonly seed?: number;
|
|
22
|
+
readonly size?: string;
|
|
23
|
+
readonly callback_url?: string;
|
|
24
|
+
readonly user?: string;
|
|
25
|
+
readonly session_id?: string;
|
|
26
|
+
} & Record<string, unknown>;
|
|
27
|
+
export type Request = VideoRequest<VideoModel<OpenRouterVideoOptions>>;
|
|
28
|
+
export declare const Token: Schema.Struct<{
|
|
29
|
+
readonly id: Schema.String;
|
|
30
|
+
}>;
|
|
31
|
+
export type Token = Schema.Schema.Type<typeof Token>;
|
|
32
|
+
export declare const protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
33
|
+
readonly id: string;
|
|
34
|
+
}>;
|
|
35
|
+
export declare const model: (input: MediaRoute.ModelInput) => VideoModel<OpenRouterVideoOptions>;
|
|
36
|
+
export declare const OpenRouterVideo: {
|
|
37
|
+
readonly protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
38
|
+
readonly id: string;
|
|
39
|
+
}>;
|
|
40
|
+
readonly model: typeof model;
|
|
41
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Media } from "../media.js";
|
|
3
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
|
+
import { MediaRoute } from "../route/media.js";
|
|
5
|
+
import { mergeJsonRecords } from "../schema/index.js";
|
|
6
|
+
import { VideoModel, VideoResponse, seconds } from "../video.js";
|
|
7
|
+
import { ProviderShared, optionalNull } from "./shared.js";
|
|
8
|
+
const route = MediaProtocol.identity({ id: "openrouter-video", name: "OpenRouter Video", provider: "openrouter" });
|
|
9
|
+
export const DEFAULT_BASE_URL = "https://openrouter.ai/api/v1";
|
|
10
|
+
export const PATH = "/videos";
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// 2. Token and response schemas
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
export const Token = Schema.Struct({ id: Schema.String });
|
|
15
|
+
const StartResponse = Schema.Struct({ id: Schema.String, status: optionalNull(Schema.String) });
|
|
16
|
+
const Job = Schema.Struct({
|
|
17
|
+
id: optionalNull(Schema.String),
|
|
18
|
+
generation_id: optionalNull(Schema.String),
|
|
19
|
+
status: Schema.String,
|
|
20
|
+
unsigned_urls: optionalNull(Schema.Array(Schema.String)),
|
|
21
|
+
expires_at: optionalNull(Schema.Number),
|
|
22
|
+
error: optionalNull(Schema.String),
|
|
23
|
+
usage: optionalNull(Schema.Struct({
|
|
24
|
+
cost: optionalNull(Schema.Number),
|
|
25
|
+
is_byok: optionalNull(Schema.Boolean),
|
|
26
|
+
})),
|
|
27
|
+
});
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// 3. Status
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
const STATUS = {
|
|
32
|
+
pending: "queued",
|
|
33
|
+
in_progress: "running",
|
|
34
|
+
completed: "completed",
|
|
35
|
+
failed: "failed",
|
|
36
|
+
cancelled: "cancelled",
|
|
37
|
+
expired: "expired",
|
|
38
|
+
};
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// 4. Body construction
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
const imagePart = (asset) => ProviderShared.mediaReference(asset, undefined, route.name).pipe(Effect.map((reference) => ({ type: "image_url", image_url: { url: reference.value } })));
|
|
43
|
+
const referencePart = Effect.fn("OpenRouterVideo.referencePart")(function* (asset) {
|
|
44
|
+
// OpenRouter rejects inline video and audio ("Only HTTPS URLs are allowed", verified live); inline images work.
|
|
45
|
+
if ((asset.kind === "video" || asset.kind === "audio") && asset.source.type !== "url")
|
|
46
|
+
return yield* route.unsupported("video.references", `${route.name} only takes reference ${asset.kind} as http(s) URLs`);
|
|
47
|
+
const reference = yield* ProviderShared.mediaReference(asset, undefined, route.name);
|
|
48
|
+
if (asset.kind === "video")
|
|
49
|
+
return { type: "video_url", video_url: { url: reference.value } };
|
|
50
|
+
if (asset.kind === "audio")
|
|
51
|
+
return { type: "audio_url", audio_url: { url: reference.value } };
|
|
52
|
+
return { type: "image_url", image_url: { url: reference.value } };
|
|
53
|
+
});
|
|
54
|
+
const fromRequest = Effect.fn("OpenRouterVideo.fromRequest")(function* (request) {
|
|
55
|
+
const frames = [
|
|
56
|
+
...(request.start === undefined
|
|
57
|
+
? []
|
|
58
|
+
: [{ ...(yield* imagePart(request.start)), frame_type: "first_frame" }]),
|
|
59
|
+
...(request.end === undefined ? [] : [{ ...(yield* imagePart(request.end)), frame_type: "last_frame" }]),
|
|
60
|
+
];
|
|
61
|
+
const inputReferences = yield* Effect.forEach(request.references ?? [], referencePart);
|
|
62
|
+
return MediaProtocol.json(mergeJsonRecords({
|
|
63
|
+
model: request.model.id,
|
|
64
|
+
prompt: request.prompt,
|
|
65
|
+
frame_images: frames.length === 0 ? undefined : frames,
|
|
66
|
+
input_references: inputReferences.length === 0 ? undefined : inputReferences,
|
|
67
|
+
duration: request.duration === undefined ? undefined : seconds(request.duration),
|
|
68
|
+
// Leaving the aspect ratio out is how OpenRouter follows the input image on models that can.
|
|
69
|
+
aspect_ratio: request.aspect === "auto" ? undefined : request.aspect,
|
|
70
|
+
resolution: request.resolution === undefined ? undefined : resolution(request.resolution),
|
|
71
|
+
generate_audio: request.sound,
|
|
72
|
+
}, request.providerOptions, request.http?.body) ?? {});
|
|
73
|
+
});
|
|
74
|
+
// OpenRouter spells the K resolutions in upper case (`1K`, `2K`, `4K`).
|
|
75
|
+
const resolution = (value) => (/^\dk$/.test(value) ? value.toUpperCase() : value);
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// 5. Response decoding
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
const decodeStart = route.decodeStarted(StartResponse, (value) => ({
|
|
80
|
+
token: { id: value.id },
|
|
81
|
+
snapshot: { id: value.id, status: MediaProtocol.status(STATUS, value.status ?? "pending") },
|
|
82
|
+
}));
|
|
83
|
+
const decodeJob = route.decodeJson(Job);
|
|
84
|
+
const decodeStatus = Effect.fn("OpenRouterVideo.decodeStatus")(function* (response, context) {
|
|
85
|
+
const output = yield* decodeJob(response);
|
|
86
|
+
return { id: context.token.id, status: MediaProtocol.status(STATUS, output.value.status) };
|
|
87
|
+
});
|
|
88
|
+
const decodeResult = Effect.fn("OpenRouterVideo.decodeResult")(function* (response, context) {
|
|
89
|
+
const output = yield* decodeJob(response);
|
|
90
|
+
const job = output.value;
|
|
91
|
+
const status = MediaProtocol.status(STATUS, job.status);
|
|
92
|
+
if (status === "queued" || status === "running")
|
|
93
|
+
return yield* output.pending(context.token.id);
|
|
94
|
+
if (status === "failed") {
|
|
95
|
+
const message = `${route.name} generation failed${job.error ? `: ${job.error}` : ""}`;
|
|
96
|
+
if (job.error && /content policy|moderat/i.test(job.error))
|
|
97
|
+
return yield* output.contentPolicy(message);
|
|
98
|
+
return yield* output.ended("failed", message);
|
|
99
|
+
}
|
|
100
|
+
if (status !== "completed")
|
|
101
|
+
return yield* output.ended(status, `${route.name} job ${context.token.id} ${status}${job.error ? `: ${job.error}` : ""}`);
|
|
102
|
+
const urls = job.unsigned_urls ?? [];
|
|
103
|
+
if (urls.length === 0)
|
|
104
|
+
return yield* output.invalid(`${route.name} completed without a video URL`);
|
|
105
|
+
// The content URLs are OpenRouter's own and only download with the API key.
|
|
106
|
+
const videos = yield* Effect.forEach(urls, (url) => context.headers(url).pipe(Effect.map((headers) => Media.url(url, {
|
|
107
|
+
mediaType: "video/mp4",
|
|
108
|
+
expiresAt: job.expires_at === undefined || job.expires_at === null ? undefined : job.expires_at * 1000,
|
|
109
|
+
headers,
|
|
110
|
+
}))));
|
|
111
|
+
return new VideoResponse({
|
|
112
|
+
videos,
|
|
113
|
+
providerMetadata: {
|
|
114
|
+
openrouter: {
|
|
115
|
+
id: context.token.id,
|
|
116
|
+
generationId: job.generation_id ?? undefined,
|
|
117
|
+
cost: job.usage?.cost ?? undefined,
|
|
118
|
+
isByok: job.usage?.is_byok ?? undefined,
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
// ---------------------------------------------------------------------------
|
|
124
|
+
// 6. Protocol and route
|
|
125
|
+
// ---------------------------------------------------------------------------
|
|
126
|
+
const jobPath = (token) => `${PATH}/${encodeURIComponent(token.id)}`;
|
|
127
|
+
// OpenRouter has no cancel endpoint; `cancelled` only appears when it stops a job itself.
|
|
128
|
+
export const protocol = MediaProtocol.queued(route, {
|
|
129
|
+
token: Token,
|
|
130
|
+
start: { body: { from: fromRequest }, decode: decodeStart },
|
|
131
|
+
status: { path: jobPath, decode: decodeStatus },
|
|
132
|
+
result: { path: jobPath, decode: decodeResult },
|
|
133
|
+
});
|
|
134
|
+
export const model = (input) => VideoModel.fromRoute({ protocol, baseURL: DEFAULT_BASE_URL, path: PATH }, input);
|
|
135
|
+
export const OpenRouterVideo = {
|
|
136
|
+
protocol,
|
|
137
|
+
model,
|
|
138
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
3
|
+
import { MediaRoute } from "../route/media.js";
|
|
4
|
+
import { VideoModel, VideoResponse, type VideoRequest } from "../video.js";
|
|
5
|
+
export declare const VOLCENGINE_BASE_URL = "https://ark.cn-beijing.volces.com/api/v3";
|
|
6
|
+
export declare const BYTEPLUS_BASE_URL = "https://ark.ap-southeast.bytepluses.com/api/v3";
|
|
7
|
+
export declare const PATH = "/contents/generations/tasks";
|
|
8
|
+
/**
|
|
9
|
+
* Provider-native options, merged into the task body. Common fields (`start`, `end`, `references`, `duration`,
|
|
10
|
+
* `aspect`, `resolution`, `sound`) live on the request. `draft_task` adds a `draft_task` content item that turns a
|
|
11
|
+
* Draft video into the final one.
|
|
12
|
+
*/
|
|
13
|
+
export type SeedanceVideoOptions = {
|
|
14
|
+
readonly draft_task?: {
|
|
15
|
+
readonly id: string;
|
|
16
|
+
};
|
|
17
|
+
readonly draft?: boolean;
|
|
18
|
+
readonly frames?: number;
|
|
19
|
+
readonly seed?: number;
|
|
20
|
+
readonly camera_fixed?: boolean;
|
|
21
|
+
readonly watermark?: boolean;
|
|
22
|
+
readonly return_last_frame?: boolean;
|
|
23
|
+
readonly omni_reference_task_type?: "auto" | "reference" | "edit" | "extend" | (string & {});
|
|
24
|
+
readonly output_format?: "mp4" | "mov" | (string & {});
|
|
25
|
+
readonly service_tier?: "default" | "flex" | (string & {});
|
|
26
|
+
readonly callback_url?: string;
|
|
27
|
+
readonly execution_expires_after?: number;
|
|
28
|
+
readonly priority?: number;
|
|
29
|
+
readonly safety_identifier?: string;
|
|
30
|
+
} & Record<string, unknown>;
|
|
31
|
+
export type Request = VideoRequest<VideoModel<SeedanceVideoOptions>>;
|
|
32
|
+
export declare const Token: Schema.Struct<{
|
|
33
|
+
readonly taskID: Schema.String;
|
|
34
|
+
}>;
|
|
35
|
+
export type Token = Schema.Schema.Type<typeof Token>;
|
|
36
|
+
export type Provider = "volcengine" | "byteplus";
|
|
37
|
+
export declare const SeedanceVideo: {
|
|
38
|
+
readonly volcengine: {
|
|
39
|
+
protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
40
|
+
readonly taskID: string;
|
|
41
|
+
}>;
|
|
42
|
+
model: (input: MediaRoute.ModelInput) => VideoModel<SeedanceVideoOptions>;
|
|
43
|
+
};
|
|
44
|
+
readonly byteplus: {
|
|
45
|
+
protocol: MediaProtocol.Queued<Request, VideoResponse, {
|
|
46
|
+
readonly taskID: string;
|
|
47
|
+
}>;
|
|
48
|
+
model: (input: MediaRoute.ModelInput) => VideoModel<SeedanceVideoOptions>;
|
|
49
|
+
};
|
|
50
|
+
};
|