@tanstack/ai-byteplus 0.0.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/LICENSE +21 -0
- package/README.md +202 -0
- package/dist/esm/adapters/image.d.ts +89 -0
- package/dist/esm/adapters/image.js +229 -0
- package/dist/esm/adapters/image.js.map +1 -0
- package/dist/esm/adapters/text.d.ts +163 -0
- package/dist/esm/adapters/text.js +347 -0
- package/dist/esm/adapters/text.js.map +1 -0
- package/dist/esm/adapters/transcription.d.ts +102 -0
- package/dist/esm/adapters/transcription.js +274 -0
- package/dist/esm/adapters/transcription.js.map +1 -0
- package/dist/esm/adapters/tts.d.ts +143 -0
- package/dist/esm/adapters/tts.js +307 -0
- package/dist/esm/adapters/tts.js.map +1 -0
- package/dist/esm/adapters/video.d.ts +182 -0
- package/dist/esm/adapters/video.js +442 -0
- package/dist/esm/adapters/video.js.map +1 -0
- package/dist/esm/audio/transcription-provider-options.d.ts +46 -0
- package/dist/esm/audio/tts-provider-options.d.ts +114 -0
- package/dist/esm/audio/wire-types.d.ts +261 -0
- package/dist/esm/audio/wire-types.js +28 -0
- package/dist/esm/audio/wire-types.js.map +1 -0
- package/dist/esm/image/image-provider-options.d.ts +165 -0
- package/dist/esm/image/image-provider-options.js +134 -0
- package/dist/esm/image/image-provider-options.js.map +1 -0
- package/dist/esm/image/wire-types.d.ts +149 -0
- package/dist/esm/index.d.ts +25 -0
- package/dist/esm/index.js +11 -0
- package/dist/esm/message-types.d.ts +154 -0
- package/dist/esm/model-meta.d.ts +594 -0
- package/dist/esm/model-meta.js +619 -0
- package/dist/esm/model-meta.js.map +1 -0
- package/dist/esm/text/text-provider-options.d.ts +109 -0
- package/dist/esm/utils/client.d.ts +183 -0
- package/dist/esm/utils/client.js +253 -0
- package/dist/esm/utils/client.js.map +1 -0
- package/dist/esm/video/video-provider-options.d.ts +197 -0
- package/dist/esm/video/video-provider-options.js +191 -0
- package/dist/esm/video/video-provider-options.js.map +1 -0
- package/dist/esm/video/wire-types.d.ts +248 -0
- package/package.json +77 -0
- package/src/adapters/image.ts +409 -0
- package/src/adapters/text.ts +539 -0
- package/src/adapters/transcription.ts +479 -0
- package/src/adapters/tts.ts +447 -0
- package/src/adapters/video.ts +732 -0
- package/src/audio/transcription-provider-options.ts +46 -0
- package/src/audio/tts-provider-options.ts +122 -0
- package/src/audio/wire-types.ts +290 -0
- package/src/image/image-provider-options.ts +288 -0
- package/src/image/wire-types.ts +169 -0
- package/src/index.ts +222 -0
- package/src/message-types.ts +169 -0
- package/src/model-meta.ts +954 -0
- package/src/text/text-provider-options.ts +151 -0
- package/src/utils/client.ts +377 -0
- package/src/video/video-provider-options.ts +361 -0
- package/src/video/wire-types.ts +293 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import { bytePlusArkError, bytePlusArkHeaders, bytePlusTimeoutSignal, getBytePlusArkApiKeyFromEnv, readJsonBody, toHeaderRecord, withBytePlusArkDefaults } from "../utils/client.js";
|
|
2
|
+
import { getBytePlusVideoDurationOptions, isKnownBytePlusVideoModel } from "../model-meta.js";
|
|
3
|
+
import { resolveBytePlusVideoResolution, resolveBytePlusVideoSize, supportsLastFrame, supportsReferenceMedia } from "../video/video-provider-options.js";
|
|
4
|
+
import { resolveMediaPrompt } from "@tanstack/ai";
|
|
5
|
+
import { BaseVideoAdapter, snapToDurationOption } from "@tanstack/ai/adapters";
|
|
6
|
+
import { toRunErrorPayload } from "@tanstack/ai/adapter-internals";
|
|
7
|
+
//#region src/adapters/video.ts
|
|
8
|
+
/** Path of the Seedance task API, relative to the Ark base URL. */
|
|
9
|
+
var TASKS_PATH = "/contents/generations/tasks";
|
|
10
|
+
/**
|
|
11
|
+
* `content.video_url` and `content.last_frame_url` are deleted 24 hours after
|
|
12
|
+
* the task produces them.
|
|
13
|
+
*/
|
|
14
|
+
var VIDEO_URL_TTL_MS = 1440 * 60 * 1e3;
|
|
15
|
+
/**
|
|
16
|
+
* Converts a media prompt part into the URL string Seedance's `content[]`
|
|
17
|
+
* takes: public URLs pass through (BytePlus fetches them server-side), data
|
|
18
|
+
* sources become base64 data URIs.
|
|
19
|
+
*/
|
|
20
|
+
function mediaPartToUrl(part) {
|
|
21
|
+
const { source } = part;
|
|
22
|
+
if (source.type === "url") return source.value;
|
|
23
|
+
if (source.value.startsWith("data:")) return source.value;
|
|
24
|
+
return `data:${source.mimeType.toLowerCase()};base64,${source.value}`;
|
|
25
|
+
}
|
|
26
|
+
/** Coerces a usage count that the API types as a string but sends as a number. */
|
|
27
|
+
function toTokenCount(value) {
|
|
28
|
+
if (typeof value === "number") return value;
|
|
29
|
+
if (typeof value === "string") {
|
|
30
|
+
const parsed = Number(value);
|
|
31
|
+
return Number.isFinite(parsed) ? parsed : void 0;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Maps a finished task's usage onto `TokenUsage`.
|
|
36
|
+
*
|
|
37
|
+
* Seedance bills output only — the API documents input tokens as always 0 and
|
|
38
|
+
* `total_tokens` as equal to `completion_tokens` — so `promptTokens` is 0 and
|
|
39
|
+
* the completion count doubles as `unitsBilled`.
|
|
40
|
+
*/
|
|
41
|
+
function buildBytePlusVideoUsage(usage) {
|
|
42
|
+
if (!usage) return void 0;
|
|
43
|
+
const completionTokens = toTokenCount(usage.completion_tokens);
|
|
44
|
+
const totalTokens = toTokenCount(usage.total_tokens);
|
|
45
|
+
if (completionTokens === void 0 && totalTokens === void 0) return;
|
|
46
|
+
const completion = completionTokens ?? totalTokens ?? 0;
|
|
47
|
+
return {
|
|
48
|
+
promptTokens: 0,
|
|
49
|
+
completionTokens: completion,
|
|
50
|
+
totalTokens: totalTokens ?? completion,
|
|
51
|
+
unitsBilled: completion
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Formats a terminal task's error detail for a status / failure message.
|
|
56
|
+
*
|
|
57
|
+
* Always returns a string. Core surfaces a failed job as
|
|
58
|
+
* `throw new Error(statusResult.error || 'Video generation failed')`, so
|
|
59
|
+
* returning `undefined` for a failure Ark reported without an `error` block
|
|
60
|
+
* would hand the caller an unattributable error. The final fallback is a
|
|
61
|
+
* snapshot of the identifying fields instead.
|
|
62
|
+
*/
|
|
63
|
+
function describeTaskFailure(task) {
|
|
64
|
+
const { code, message } = task.error ?? {};
|
|
65
|
+
if (code && message) return `${code}: ${message}`;
|
|
66
|
+
if (message) return message;
|
|
67
|
+
if (code) return code;
|
|
68
|
+
if (task.status === "expired") return "Task expired before it finished (execution_expires_after elapsed).";
|
|
69
|
+
if (task.status === "cancelled") return "Task was cancelled.";
|
|
70
|
+
return `Task reported status "${task.status ?? "unknown"}" with no error detail (id=${task.id ?? "unknown"}, model=${task.model ?? "unknown"}).`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* BytePlus Seedance video generation adapter.
|
|
74
|
+
*
|
|
75
|
+
* Drives Ark's asynchronous task API — `POST /contents/generations/tasks` to
|
|
76
|
+
* submit, `GET /contents/generations/tasks/{id}` to poll and to read the
|
|
77
|
+
* finished video URL. Core owns the polling loop; this adapter implements the
|
|
78
|
+
* three primitives plus the duration metadata.
|
|
79
|
+
*
|
|
80
|
+
* Prompt parts map onto Seedance's `content[]` roles, which the API sorts into
|
|
81
|
+
* mutually exclusive task types:
|
|
82
|
+
*
|
|
83
|
+
* - `'start_frame'` (or a single un-roled image) → `first_frame` — the frame
|
|
84
|
+
* the video opens on (`i2v`).
|
|
85
|
+
* - `'end_frame'` → `last_frame` — the frame it closes on (`flf2v`); Seedance
|
|
86
|
+
* requires a `first_frame` alongside it, and
|
|
87
|
+
* `seedance-1-0-pro-fast-251015` does not support it at all.
|
|
88
|
+
* - `'reference'` / `'character'` → `reference_image`, video parts →
|
|
89
|
+
* `reference_video`, audio parts → `reference_audio` — subject and style
|
|
90
|
+
* references the model draws on (`r2v`, Seedance 2.0 family only).
|
|
91
|
+
*
|
|
92
|
+
* Frame roles and reference roles cannot be combined in one request, so the
|
|
93
|
+
* adapter rejects a mix up front rather than surfacing a raw 400.
|
|
94
|
+
*
|
|
95
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const adapter = byteplusVideo('seedance-1-0-pro-fast-251015')
|
|
100
|
+
*
|
|
101
|
+
* const { jobId } = await generateVideo({
|
|
102
|
+
* adapter,
|
|
103
|
+
* prompt: 'a guitar being played in a store',
|
|
104
|
+
* size: '16:9_720p',
|
|
105
|
+
* duration: 4,
|
|
106
|
+
* modelOptions: { service_tier: 'flex' },
|
|
107
|
+
* })
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
var BytePlusVideoAdapter = class extends BaseVideoAdapter {
|
|
111
|
+
name = "byteplus";
|
|
112
|
+
/** Config with the Ark base URL resolved and its trailing slashes trimmed. */
|
|
113
|
+
clientConfig;
|
|
114
|
+
constructor(config, model) {
|
|
115
|
+
super({}, model);
|
|
116
|
+
this.clientConfig = withBytePlusArkDefaults(config);
|
|
117
|
+
}
|
|
118
|
+
async request(path, init) {
|
|
119
|
+
const fetchImpl = this.clientConfig.fetch ?? fetch;
|
|
120
|
+
const signal = bytePlusTimeoutSignal(this.clientConfig.timeout);
|
|
121
|
+
const response = await fetchImpl(`${this.clientConfig.baseURL}${path}`, {
|
|
122
|
+
...init,
|
|
123
|
+
...signal && { signal },
|
|
124
|
+
headers: bytePlusArkHeaders(this.clientConfig.apiKey, toHeaderRecord(this.clientConfig.defaultHeaders))
|
|
125
|
+
});
|
|
126
|
+
return {
|
|
127
|
+
response,
|
|
128
|
+
body: await readJsonBody(response)
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Builds the `content[]` array from the resolved prompt, enforcing
|
|
133
|
+
* Seedance's role vocabulary and mode exclusivity.
|
|
134
|
+
*/
|
|
135
|
+
buildContent(resolved) {
|
|
136
|
+
const model = this.model;
|
|
137
|
+
const content = [];
|
|
138
|
+
if (resolved.text) content.push({
|
|
139
|
+
type: "text",
|
|
140
|
+
text: resolved.text
|
|
141
|
+
});
|
|
142
|
+
const gated = isKnownBytePlusVideoModel(model);
|
|
143
|
+
let firstFrames = 0;
|
|
144
|
+
let lastFrames = 0;
|
|
145
|
+
let visualReferences = 0;
|
|
146
|
+
let audioReferences = 0;
|
|
147
|
+
for (const part of resolved.images) {
|
|
148
|
+
const role = part.metadata?.role;
|
|
149
|
+
switch (role) {
|
|
150
|
+
case "mask":
|
|
151
|
+
case "control": throw new Error(`byteplus: Seedance has no '${role}' image input on model ${model}. Use 'start_frame', 'end_frame' or 'reference'.`);
|
|
152
|
+
case "end_frame":
|
|
153
|
+
if (gated && !supportsLastFrame(model)) throw new Error(`byteplus: ${model} does not support a closing frame — it does text-to-video and first-frame image-to-video only. Drop the 'end_frame' image or switch to a model with first-and-last-frame support.`);
|
|
154
|
+
lastFrames++;
|
|
155
|
+
content.push({
|
|
156
|
+
type: "image_url",
|
|
157
|
+
image_url: { url: mediaPartToUrl(part) },
|
|
158
|
+
role: "last_frame"
|
|
159
|
+
});
|
|
160
|
+
break;
|
|
161
|
+
case "reference":
|
|
162
|
+
case "character":
|
|
163
|
+
if (gated && !supportsReferenceMedia(model)) throw new Error(`byteplus: ${model} does not support reference images. Reference media is available on the Seedance 2.0 family; on this model use 'start_frame' / 'end_frame' images instead.`);
|
|
164
|
+
visualReferences++;
|
|
165
|
+
content.push({
|
|
166
|
+
type: "image_url",
|
|
167
|
+
image_url: { url: mediaPartToUrl(part) },
|
|
168
|
+
role: "reference_image"
|
|
169
|
+
});
|
|
170
|
+
break;
|
|
171
|
+
case "start_frame":
|
|
172
|
+
case void 0:
|
|
173
|
+
firstFrames++;
|
|
174
|
+
content.push({
|
|
175
|
+
type: "image_url",
|
|
176
|
+
image_url: { url: mediaPartToUrl(part) },
|
|
177
|
+
role: "first_frame"
|
|
178
|
+
});
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
for (const part of resolved.videos) {
|
|
183
|
+
if (gated && !supportsReferenceMedia(model)) throw new Error(`byteplus: ${model} does not accept video prompt parts. Reference video is available on the Seedance 2.0 family only.`);
|
|
184
|
+
visualReferences++;
|
|
185
|
+
content.push({
|
|
186
|
+
type: "video_url",
|
|
187
|
+
video_url: { url: mediaPartToUrl(part) },
|
|
188
|
+
role: "reference_video"
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
for (const part of resolved.audios) {
|
|
192
|
+
if (gated && !supportsReferenceMedia(model)) throw new Error(`byteplus: ${model} does not accept audio prompt parts. Reference audio is available on the Seedance 2.0 family only.`);
|
|
193
|
+
audioReferences++;
|
|
194
|
+
content.push({
|
|
195
|
+
type: "audio_url",
|
|
196
|
+
audio_url: { url: mediaPartToUrl(part) },
|
|
197
|
+
role: "reference_audio"
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const frames = firstFrames + lastFrames;
|
|
201
|
+
if (gated && frames > 0 && visualReferences + audioReferences > 0) throw new Error(`byteplus: first/last frame inputs cannot be combined with reference media on model ${model}. Use either frame roles ('start_frame', 'end_frame') or reference roles ('reference', 'character', video, audio) — not both.`);
|
|
202
|
+
if (gated && firstFrames > 1) throw new Error(`byteplus: ${model} accepts at most one opening frame; received ${firstFrames} un-roled or 'start_frame' images. Use metadata.role ('end_frame', 'reference') to disambiguate the others.`);
|
|
203
|
+
if (gated && lastFrames > 1) throw new Error(`byteplus: ${model} accepts at most one closing frame; received ${lastFrames} 'end_frame' images.`);
|
|
204
|
+
if (gated && lastFrames > 0 && firstFrames === 0) throw new Error(`byteplus: a closing frame needs an opening frame alongside it on model ${model}. Add a 'start_frame' image, or drop the 'end_frame' role.`);
|
|
205
|
+
if (gated && audioReferences > 0 && visualReferences === 0) throw new Error(`byteplus: a reference audio input cannot be the only reference on model ${model}. Pair it with a reference image or video.`);
|
|
206
|
+
if (content.length === 0) throw new Error(`byteplus: a video prompt must carry text or at least one media input (model: ${model}).`);
|
|
207
|
+
return content;
|
|
208
|
+
}
|
|
209
|
+
async createVideoJob(options) {
|
|
210
|
+
const { size, modelOptions, logger } = options;
|
|
211
|
+
const model = this.model;
|
|
212
|
+
const content = this.buildContent(resolveMediaPrompt(options.prompt));
|
|
213
|
+
const parsedSize = size !== void 0 ? resolveBytePlusVideoSize(model, size) : void 0;
|
|
214
|
+
const duration = options.duration !== void 0 ? isKnownBytePlusVideoModel(model) ? this.snapDuration(options.duration) : options.duration : void 0;
|
|
215
|
+
const request = {
|
|
216
|
+
...parsedSize && {
|
|
217
|
+
ratio: parsedSize.ratio,
|
|
218
|
+
...parsedSize.resolution !== void 0 && { resolution: parsedSize.resolution }
|
|
219
|
+
},
|
|
220
|
+
...duration !== void 0 && { duration },
|
|
221
|
+
...modelOptions,
|
|
222
|
+
model,
|
|
223
|
+
content
|
|
224
|
+
};
|
|
225
|
+
if (request.resolution !== void 0) request.resolution = resolveBytePlusVideoResolution(model, request.resolution);
|
|
226
|
+
try {
|
|
227
|
+
logger.request(`activity=video.create provider=${this.name} model=${model} size=${size ?? "default"} duration=${request.duration ?? "default"}`, {
|
|
228
|
+
provider: this.name,
|
|
229
|
+
model
|
|
230
|
+
});
|
|
231
|
+
const { response, body } = await this.request(TASKS_PATH, {
|
|
232
|
+
method: "POST",
|
|
233
|
+
body: JSON.stringify(request)
|
|
234
|
+
});
|
|
235
|
+
if (!response.ok) throw bytePlusArkError(response.status, body, "video task creation");
|
|
236
|
+
const { id } = body ?? {};
|
|
237
|
+
if (!id) throw new Error("byteplus: video task creation returned no task id.");
|
|
238
|
+
return {
|
|
239
|
+
jobId: id,
|
|
240
|
+
model
|
|
241
|
+
};
|
|
242
|
+
} catch (error) {
|
|
243
|
+
logger.errors(`${this.name}.createVideoJob fatal`, {
|
|
244
|
+
error: toRunErrorPayload(error, `${this.name}.createVideoJob failed`),
|
|
245
|
+
source: `${this.name}.createVideoJob`
|
|
246
|
+
});
|
|
247
|
+
throw error;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Fetches a task, tagging the thrown error with the HTTP status.
|
|
252
|
+
*
|
|
253
|
+
* The 200 body is validated rather than cast. `readJsonBody` returns
|
|
254
|
+
* `undefined` for an empty body and the raw text for a non-JSON one — both
|
|
255
|
+
* documented failure modes of these hosts (an HTML error page from a proxy
|
|
256
|
+
* in front of the API). Casting either to `BytePlusVideoTask` yields a task
|
|
257
|
+
* whose `status` is `undefined`, which {@link mapStatus} would have to
|
|
258
|
+
* interpret; the honest answer is that the response was not a task at all,
|
|
259
|
+
* so say so while the body is still in hand.
|
|
260
|
+
*/
|
|
261
|
+
async retrieveTask(jobId) {
|
|
262
|
+
const { response, body } = await this.request(`${TASKS_PATH}/${encodeURIComponent(jobId)}`);
|
|
263
|
+
if (!response.ok) {
|
|
264
|
+
const error = bytePlusArkError(response.status, body, "video task lookup");
|
|
265
|
+
error.status = response.status;
|
|
266
|
+
throw error;
|
|
267
|
+
}
|
|
268
|
+
if (typeof body !== "object" || body === null) throw bytePlusArkError(response.status, body, `video task lookup (job ${jobId}) returned a non-object body`);
|
|
269
|
+
return body;
|
|
270
|
+
}
|
|
271
|
+
async getVideoStatus(jobId) {
|
|
272
|
+
let task;
|
|
273
|
+
try {
|
|
274
|
+
task = await this.retrieveTask(jobId);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if (error.status === 404) return {
|
|
277
|
+
jobId,
|
|
278
|
+
status: "failed",
|
|
279
|
+
error: `Job not found: ${jobId} (${error.message})`
|
|
280
|
+
};
|
|
281
|
+
throw error;
|
|
282
|
+
}
|
|
283
|
+
const status = this.mapStatus(task.status);
|
|
284
|
+
const failure = status === "failed" ? describeTaskFailure(task) : void 0;
|
|
285
|
+
return {
|
|
286
|
+
jobId,
|
|
287
|
+
status,
|
|
288
|
+
...failure !== void 0 && { error: failure }
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
async getVideoUrl(jobId) {
|
|
292
|
+
let task;
|
|
293
|
+
try {
|
|
294
|
+
task = await this.retrieveTask(jobId);
|
|
295
|
+
} catch (error) {
|
|
296
|
+
if (error.status === 404) throw new Error(`Video job not found: ${jobId} (${error.message})`);
|
|
297
|
+
throw error;
|
|
298
|
+
}
|
|
299
|
+
if (this.mapStatus(task.status) === "failed") throw new Error(`Video generation failed: ${describeTaskFailure(task)}. Job ID: ${jobId}`);
|
|
300
|
+
const url = task.content?.video_url;
|
|
301
|
+
if (!url) throw new Error(`Video is not ready for download. Check status first. Job ID: ${jobId}`);
|
|
302
|
+
const anchorSeconds = task.updated_at ?? task.created_at;
|
|
303
|
+
const expiresAt = anchorSeconds !== void 0 ? /* @__PURE__ */ new Date(anchorSeconds * 1e3 + VIDEO_URL_TTL_MS) : void 0;
|
|
304
|
+
const usage = buildBytePlusVideoUsage(task.usage);
|
|
305
|
+
return {
|
|
306
|
+
jobId,
|
|
307
|
+
url,
|
|
308
|
+
...expiresAt && { expiresAt },
|
|
309
|
+
...usage && { usage }
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Maps Seedance task states onto the generic video status set. `expired`
|
|
314
|
+
* (the task outlived `execution_expires_after`) and `cancelled` are
|
|
315
|
+
* terminal non-successes, so both report as failed.
|
|
316
|
+
*
|
|
317
|
+
* An unrecognized state throws rather than defaulting to `processing`.
|
|
318
|
+
* Core's poll loop treats `processing` as "keep waiting", so mapping an
|
|
319
|
+
* unknown state — a missing `status`, or a terminal one Ark adds later such
|
|
320
|
+
* as `rejected` — onto it means polling until `maxDuration` and then
|
|
321
|
+
* reporting a generic timeout, with the state Ark actually sent never
|
|
322
|
+
* reaching the caller. Failing here names it.
|
|
323
|
+
*/
|
|
324
|
+
mapStatus(apiStatus) {
|
|
325
|
+
switch (apiStatus) {
|
|
326
|
+
case "queued": return "pending";
|
|
327
|
+
case "running": return "processing";
|
|
328
|
+
case "succeeded": return "completed";
|
|
329
|
+
case "failed":
|
|
330
|
+
case "expired":
|
|
331
|
+
case "cancelled": return "failed";
|
|
332
|
+
case void 0:
|
|
333
|
+
default: throw new Error(`byteplus: unrecognized Seedance task status ${apiStatus === void 0 ? "(missing)" : `"${apiStatus}"`}. Known states: queued, running, succeeded, failed, expired, cancelled.`);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Seedance accepts any whole second inside a per-model range: 4–15s on the
|
|
338
|
+
* 2.0 family, 4–12s on 1.5-pro, 2–12s on the 1.0 models. An unknown model
|
|
339
|
+
* reports the union of those ranges as a UI hint — see
|
|
340
|
+
* `BYTEPLUS_VIDEO_FALLBACK_DURATIONS`, which `createVideoJob` does not snap
|
|
341
|
+
* against.
|
|
342
|
+
*/
|
|
343
|
+
availableDurations() {
|
|
344
|
+
return getBytePlusVideoDurationOptions(this.model);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Coerce a raw seconds value to the closest duration this model accepts
|
|
348
|
+
* (clamped to its range and rounded to whole seconds).
|
|
349
|
+
*/
|
|
350
|
+
snapDuration(seconds) {
|
|
351
|
+
return snapToDurationOption(seconds, this.availableDurations());
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
/**
|
|
355
|
+
* Creates a BytePlus Seedance video adapter with an explicit API key.
|
|
356
|
+
* Type resolution happens here at the call site.
|
|
357
|
+
*
|
|
358
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
359
|
+
*
|
|
360
|
+
* @param model - The model name (e.g., 'seedance-1-0-pro-fast-251015')
|
|
361
|
+
* @param apiKey - Your BytePlus Ark API key
|
|
362
|
+
* @param config - Optional additional configuration
|
|
363
|
+
* @returns Configured BytePlus video adapter instance with resolved types
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* ```typescript
|
|
367
|
+
* const adapter = createBytePlusVideo('seedance-1-5-pro-251215', 'ark-...')
|
|
368
|
+
*
|
|
369
|
+
* const { jobId } = await generateVideo({
|
|
370
|
+
* adapter,
|
|
371
|
+
* prompt: 'a guitar being played in a store',
|
|
372
|
+
* size: '16:9_1080p',
|
|
373
|
+
* duration: 5,
|
|
374
|
+
* })
|
|
375
|
+
* ```
|
|
376
|
+
*/
|
|
377
|
+
function createBytePlusVideo(model, apiKey, config) {
|
|
378
|
+
return new BytePlusVideoAdapter({
|
|
379
|
+
apiKey,
|
|
380
|
+
...config
|
|
381
|
+
}, model);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Creates a BytePlus Seedance video adapter, reading `ARK_API_KEY` from the
|
|
385
|
+
* environment. Type resolution happens here at the call site.
|
|
386
|
+
*
|
|
387
|
+
* Note that Ark keys are region-isolated and Seedance is only served from the
|
|
388
|
+
* Asia-Pacific endpoint — an EU key will not work here.
|
|
389
|
+
*
|
|
390
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
391
|
+
*
|
|
392
|
+
* @param model - The model name (e.g., 'dreamina-seedance-2-0-260128')
|
|
393
|
+
* @param config - Optional configuration (excluding apiKey, auto-detected)
|
|
394
|
+
* @returns Configured BytePlus video adapter instance with resolved types
|
|
395
|
+
* @throws Error if ARK_API_KEY is not found in environment
|
|
396
|
+
*
|
|
397
|
+
* @example
|
|
398
|
+
* ```typescript
|
|
399
|
+
* const adapter = byteplusVideo('dreamina-seedance-2-0-260128')
|
|
400
|
+
*
|
|
401
|
+
* // Image-to-video: an un-roled image is the opening frame.
|
|
402
|
+
* const { jobId } = await generateVideo({
|
|
403
|
+
* adapter,
|
|
404
|
+
* prompt: [
|
|
405
|
+
* { type: 'text', content: 'the guitarist starts playing' },
|
|
406
|
+
* { type: 'image', source: { type: 'url', value: 'https://example.com/shop.jpg' } },
|
|
407
|
+
* ],
|
|
408
|
+
* })
|
|
409
|
+
*
|
|
410
|
+
* const status = await getVideoJobStatus({ adapter, jobId })
|
|
411
|
+
* ```
|
|
412
|
+
*
|
|
413
|
+
* ## Models this package does not know yet
|
|
414
|
+
*
|
|
415
|
+
* `model` also accepts any string, so a Seedance id BytePlus publishes after
|
|
416
|
+
* this release works without upgrading. **Seedance 2.5 is the case this exists
|
|
417
|
+
* for**: `dreamina-seedance-2-5-260628` is real and reachable, but its
|
|
418
|
+
* capability cells could not be probed from this repo's account (Ark answers
|
|
419
|
+
* 404 `ModelNotOpen` until the model is activated in the Ark Console), so it
|
|
420
|
+
* is deliberately absent from the narrowed model tables. Passing it here works
|
|
421
|
+
* for an account that has activated it.
|
|
422
|
+
*
|
|
423
|
+
* An unknown id relaxes both halves of the adapter: the `size` type widens to
|
|
424
|
+
* any string, provider options are ungated, and the runtime guards that encode
|
|
425
|
+
* per-model capabilities — resolution tiers, closing-frame and reference-media
|
|
426
|
+
* support, frame cardinality and mode exclusivity, duration snapping — stand
|
|
427
|
+
* down so Ark decides. Known ids are unaffected. See
|
|
428
|
+
* {@link BytePlusVideoModelOrString} for how to discover and probe an id.
|
|
429
|
+
*
|
|
430
|
+
* @example
|
|
431
|
+
* ```typescript
|
|
432
|
+
* // Seedance 2.5, before this package ships probe-verified metadata for it:
|
|
433
|
+
* const adapter = byteplusVideo('dreamina-seedance-2-5-260628')
|
|
434
|
+
* ```
|
|
435
|
+
*/
|
|
436
|
+
function byteplusVideo(model, config) {
|
|
437
|
+
return createBytePlusVideo(model, getBytePlusArkApiKeyFromEnv(), config);
|
|
438
|
+
}
|
|
439
|
+
//#endregion
|
|
440
|
+
export { BytePlusVideoAdapter, byteplusVideo, createBytePlusVideo };
|
|
441
|
+
|
|
442
|
+
//# sourceMappingURL=video.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"video.js","names":[],"sources":["../../../src/adapters/video.ts"],"sourcesContent":["import { resolveMediaPrompt } from '@tanstack/ai'\nimport { BaseVideoAdapter, snapToDurationOption } from '@tanstack/ai/adapters'\nimport { toRunErrorPayload } from '@tanstack/ai/adapter-internals'\nimport {\n bytePlusArkError,\n bytePlusArkHeaders,\n bytePlusTimeoutSignal,\n getBytePlusArkApiKeyFromEnv,\n readJsonBody,\n toHeaderRecord,\n withBytePlusArkDefaults,\n} from '../utils/client'\nimport {\n getBytePlusVideoDurationOptions,\n isKnownBytePlusVideoModel,\n} from '../model-meta'\nimport {\n resolveBytePlusVideoResolution,\n resolveBytePlusVideoSize,\n supportsLastFrame,\n supportsReferenceMedia,\n} from '../video/video-provider-options'\nimport type { DurationOptions } from '@tanstack/ai/adapters'\nimport type {\n AudioPart,\n ImagePart,\n MediaInputMetadata,\n TokenUsage,\n VideoGenerationOptions,\n VideoJobResult,\n VideoPart,\n VideoStatusResult,\n VideoUrlResult,\n} from '@tanstack/ai'\nimport type {\n BytePlusVideoContentPart,\n BytePlusVideoCreateRequest,\n BytePlusVideoCreateResponse,\n BytePlusVideoTask,\n BytePlusVideoTaskStatus,\n BytePlusVideoTaskUsage,\n} from '../video/wire-types'\nimport type { BytePlusVideoProviderOptions } from '../video/video-provider-options'\nimport type {\n BytePlusVideoModelOrString,\n ResolveBytePlusVideoInputModalities,\n ResolveBytePlusVideoSize,\n} from '../model-meta'\nimport type { BytePlusArkConfig } from '../utils/client'\n\n/**\n * Configuration for the BytePlus Seedance video adapter.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport interface BytePlusVideoConfig extends BytePlusArkConfig {}\n\n/** Path of the Seedance task API, relative to the Ark base URL. */\nconst TASKS_PATH = '/contents/generations/tasks'\n\n/**\n * `content.video_url` and `content.last_frame_url` are deleted 24 hours after\n * the task produces them.\n */\nconst VIDEO_URL_TTL_MS = 24 * 60 * 60 * 1000\n\n/**\n * Converts a media prompt part into the URL string Seedance's `content[]`\n * takes: public URLs pass through (BytePlus fetches them server-side), data\n * sources become base64 data URIs.\n */\nfunction mediaPartToUrl(\n part:\n | ImagePart<MediaInputMetadata>\n | VideoPart<MediaInputMetadata>\n | AudioPart<MediaInputMetadata>,\n): string {\n const { source } = part\n if (source.type === 'url') return source.value\n if (source.value.startsWith('data:')) return source.value\n return `data:${source.mimeType.toLowerCase()};base64,${source.value}`\n}\n\n/** Coerces a usage count that the API types as a string but sends as a number. */\nfunction toTokenCount(value: number | string | undefined): number | undefined {\n if (typeof value === 'number') return value\n if (typeof value === 'string') {\n const parsed = Number(value)\n return Number.isFinite(parsed) ? parsed : undefined\n }\n return undefined\n}\n\n/**\n * Maps a finished task's usage onto `TokenUsage`.\n *\n * Seedance bills output only — the API documents input tokens as always 0 and\n * `total_tokens` as equal to `completion_tokens` — so `promptTokens` is 0 and\n * the completion count doubles as `unitsBilled`.\n */\nfunction buildBytePlusVideoUsage(\n usage: BytePlusVideoTaskUsage | undefined,\n): TokenUsage | undefined {\n if (!usage) return undefined\n\n const completionTokens = toTokenCount(usage.completion_tokens)\n const totalTokens = toTokenCount(usage.total_tokens)\n if (completionTokens === undefined && totalTokens === undefined) {\n return undefined\n }\n\n const completion = completionTokens ?? totalTokens ?? 0\n return {\n promptTokens: 0,\n completionTokens: completion,\n totalTokens: totalTokens ?? completion,\n unitsBilled: completion,\n }\n}\n\n/**\n * Formats a terminal task's error detail for a status / failure message.\n *\n * Always returns a string. Core surfaces a failed job as\n * `throw new Error(statusResult.error || 'Video generation failed')`, so\n * returning `undefined` for a failure Ark reported without an `error` block\n * would hand the caller an unattributable error. The final fallback is a\n * snapshot of the identifying fields instead.\n */\nfunction describeTaskFailure(task: BytePlusVideoTask): string {\n const { code, message } = task.error ?? {}\n if (code && message) return `${code}: ${message}`\n if (message) return message\n if (code) return code\n // `expired` and `cancelled` are terminal without an `error` block.\n if (task.status === 'expired') {\n return 'Task expired before it finished (execution_expires_after elapsed).'\n }\n if (task.status === 'cancelled') return 'Task was cancelled.'\n return (\n `Task reported status \"${task.status ?? 'unknown'}\" with no error detail ` +\n `(id=${task.id ?? 'unknown'}, model=${task.model ?? 'unknown'}).`\n )\n}\n\n/**\n * BytePlus Seedance video generation adapter.\n *\n * Drives Ark's asynchronous task API — `POST /contents/generations/tasks` to\n * submit, `GET /contents/generations/tasks/{id}` to poll and to read the\n * finished video URL. Core owns the polling loop; this adapter implements the\n * three primitives plus the duration metadata.\n *\n * Prompt parts map onto Seedance's `content[]` roles, which the API sorts into\n * mutually exclusive task types:\n *\n * - `'start_frame'` (or a single un-roled image) → `first_frame` — the frame\n * the video opens on (`i2v`).\n * - `'end_frame'` → `last_frame` — the frame it closes on (`flf2v`); Seedance\n * requires a `first_frame` alongside it, and\n * `seedance-1-0-pro-fast-251015` does not support it at all.\n * - `'reference'` / `'character'` → `reference_image`, video parts →\n * `reference_video`, audio parts → `reference_audio` — subject and style\n * references the model draws on (`r2v`, Seedance 2.0 family only).\n *\n * Frame roles and reference roles cannot be combined in one request, so the\n * adapter rejects a mix up front rather than surfacing a raw 400.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * @example\n * ```typescript\n * const adapter = byteplusVideo('seedance-1-0-pro-fast-251015')\n *\n * const { jobId } = await generateVideo({\n * adapter,\n * prompt: 'a guitar being played in a store',\n * size: '16:9_720p',\n * duration: 4,\n * modelOptions: { service_tier: 'flex' },\n * })\n * ```\n */\nexport class BytePlusVideoAdapter<\n TModel extends BytePlusVideoModelOrString,\n> extends BaseVideoAdapter<\n TModel,\n BytePlusVideoProviderOptions,\n Record<TModel, BytePlusVideoProviderOptions>,\n Record<TModel, ResolveBytePlusVideoSize<TModel>>,\n Record<TModel, ResolveBytePlusVideoInputModalities<TModel>>,\n Record<TModel, number>\n> {\n readonly name = 'byteplus' as const\n\n /** Config with the Ark base URL resolved and its trailing slashes trimmed. */\n private readonly clientConfig: Omit<BytePlusVideoConfig, 'baseURL'> & {\n baseURL: string\n }\n\n constructor(config: BytePlusVideoConfig, model: TModel) {\n super({}, model)\n this.clientConfig = withBytePlusArkDefaults(config)\n }\n\n private async request(\n path: string,\n init?: Omit<RequestInit, 'headers'>,\n ): Promise<{ response: Response; body: unknown }> {\n const fetchImpl = this.clientConfig.fetch ?? fetch\n const signal = bytePlusTimeoutSignal(this.clientConfig.timeout)\n const response = await fetchImpl(`${this.clientConfig.baseURL}${path}`, {\n ...init,\n ...(signal && { signal }),\n headers: bytePlusArkHeaders(\n this.clientConfig.apiKey,\n toHeaderRecord(this.clientConfig.defaultHeaders),\n ),\n })\n return { response, body: await readJsonBody(response) }\n }\n\n /**\n * Builds the `content[]` array from the resolved prompt, enforcing\n * Seedance's role vocabulary and mode exclusivity.\n */\n private buildContent(\n resolved: ReturnType<typeof resolveMediaPrompt>,\n ): Array<BytePlusVideoContentPart> {\n const model = this.model\n const content: Array<BytePlusVideoContentPart> = []\n if (resolved.text) content.push({ type: 'text', text: resolved.text })\n\n // Every rule below except the role vocabulary itself is a claim about a\n // *specific* model's capabilities, drawn from probing the six models that\n // exist today. None of it can be true of a model that does not exist yet,\n // so for an unknown id the guards stand down and Ark rules — otherwise the\n // escape hatch would block exactly the requests it exists to enable (see\n // BytePlusVideoModelOrString). 'mask' / 'control' still throw: Seedance's\n // wire format has no field to carry them on any model.\n const gated = isKnownBytePlusVideoModel(model)\n\n let firstFrames = 0\n let lastFrames = 0\n // Audio counts as a reference for the mode-exclusivity check but not for\n // the \"audio can't be the only reference\" rule, which wants a visual.\n let visualReferences = 0\n let audioReferences = 0\n\n for (const part of resolved.images) {\n const role = part.metadata?.role\n switch (role) {\n case 'mask':\n case 'control':\n throw new Error(\n `byteplus: Seedance has no '${role}' image input on model ${model}. ` +\n `Use 'start_frame', 'end_frame' or 'reference'.`,\n )\n case 'end_frame': {\n if (gated && !supportsLastFrame(model)) {\n throw new Error(\n `byteplus: ${model} does not support a closing frame — it does ` +\n `text-to-video and first-frame image-to-video only. Drop the ` +\n `'end_frame' image or switch to a model with first-and-last-frame support.`,\n )\n }\n lastFrames++\n content.push({\n type: 'image_url',\n image_url: { url: mediaPartToUrl(part) },\n role: 'last_frame',\n })\n break\n }\n case 'reference':\n case 'character': {\n if (gated && !supportsReferenceMedia(model)) {\n throw new Error(\n `byteplus: ${model} does not support reference images. Reference ` +\n `media is available on the Seedance 2.0 family; on this model use ` +\n `'start_frame' / 'end_frame' images instead.`,\n )\n }\n visualReferences++\n content.push({\n type: 'image_url',\n image_url: { url: mediaPartToUrl(part) },\n role: 'reference_image',\n })\n break\n }\n // An un-roled image is the opening frame, matching the API's own\n // default and the fal / Veo adapters' positional convention.\n case 'start_frame':\n case undefined: {\n firstFrames++\n content.push({\n type: 'image_url',\n image_url: { url: mediaPartToUrl(part) },\n role: 'first_frame',\n })\n break\n }\n }\n }\n\n // Video and audio parts only exist in reference mode: Seedance rejects an\n // un-roled video with \"reference media mode requires video role to be\n // reference_video\", and has no frame-style role for either modality.\n for (const part of resolved.videos) {\n if (gated && !supportsReferenceMedia(model)) {\n throw new Error(\n `byteplus: ${model} does not accept video prompt parts. Reference ` +\n `video is available on the Seedance 2.0 family only.`,\n )\n }\n visualReferences++\n content.push({\n type: 'video_url',\n video_url: { url: mediaPartToUrl(part) },\n role: 'reference_video',\n })\n }\n\n for (const part of resolved.audios) {\n if (gated && !supportsReferenceMedia(model)) {\n throw new Error(\n `byteplus: ${model} does not accept audio prompt parts. Reference ` +\n `audio is available on the Seedance 2.0 family only.`,\n )\n }\n audioReferences++\n content.push({\n type: 'audio_url',\n audio_url: { url: mediaPartToUrl(part) },\n role: 'reference_audio',\n })\n }\n\n const frames = firstFrames + lastFrames\n if (gated && frames > 0 && visualReferences + audioReferences > 0) {\n throw new Error(\n `byteplus: first/last frame inputs cannot be combined with reference ` +\n `media on model ${model}. Use either frame roles ('start_frame', ` +\n `'end_frame') or reference roles ('reference', 'character', video, ` +\n `audio) — not both.`,\n )\n }\n\n if (gated && firstFrames > 1) {\n throw new Error(\n `byteplus: ${model} accepts at most one opening frame; received ` +\n `${firstFrames} un-roled or 'start_frame' images. Use metadata.role ` +\n `('end_frame', 'reference') to disambiguate the others.`,\n )\n }\n\n if (gated && lastFrames > 1) {\n throw new Error(\n `byteplus: ${model} accepts at most one closing frame; received ` +\n `${lastFrames} 'end_frame' images.`,\n )\n }\n\n // Seedance treats a closing frame as the second half of first-and-last-\n // frame mode: on its own it fails with \"last frame image content cannot be\n // mixed with first frame or reference image content\".\n if (gated && lastFrames > 0 && firstFrames === 0) {\n throw new Error(\n `byteplus: a closing frame needs an opening frame alongside it on ` +\n `model ${model}. Add a 'start_frame' image, or drop the 'end_frame' role.`,\n )\n }\n\n if (gated && audioReferences > 0 && visualReferences === 0) {\n throw new Error(\n `byteplus: a reference audio input cannot be the only reference on ` +\n `model ${model}. Pair it with a reference image or video.`,\n )\n }\n\n if (content.length === 0) {\n throw new Error(\n `byteplus: a video prompt must carry text or at least one media input ` +\n `(model: ${model}).`,\n )\n }\n\n return content\n }\n\n async createVideoJob(\n options: VideoGenerationOptions<\n BytePlusVideoProviderOptions,\n ResolveBytePlusVideoSize<TModel>,\n number\n >,\n ): Promise<VideoJobResult> {\n const { size, modelOptions, logger } = options\n const model = this.model\n\n const content = this.buildContent(resolveMediaPrompt(options.prompt))\n\n // The generic `size` carries a \"ratio_resolution\" template and splits back\n // into Seedance's separate fields. Explicit modelOptions win below.\n const parsedSize =\n size !== undefined ? resolveBytePlusVideoSize(model, size) : undefined\n\n // Coerce the requested duration into the model's range rather than letting\n // the API reject it. `modelOptions.duration` is deliberately not snapped:\n // it is the escape hatch for `-1` (model picks the length).\n //\n // An unknown model's duration goes through verbatim. Snapping it would\n // mean clamping against the ranges today's models happen to have, so a\n // future model's legitimate 20-second request would silently become 15 —\n // corrupting the request instead of protecting it.\n const duration =\n options.duration !== undefined\n ? isKnownBytePlusVideoModel(model)\n ? this.snapDuration(options.duration)\n : options.duration\n : undefined\n\n const request: BytePlusVideoCreateRequest = {\n ...(parsedSize && {\n ratio: parsedSize.ratio,\n ...(parsedSize.resolution !== undefined && {\n resolution: parsedSize.resolution,\n }),\n }),\n ...(duration !== undefined && { duration }),\n // Explicit provider options win over everything derived above.\n ...modelOptions,\n model,\n content,\n }\n\n // Validate what actually ships, not just what `size` contributed: a\n // `modelOptions.resolution` overriding an already-checked size would\n // otherwise reach Ark unchecked.\n if (request.resolution !== undefined) {\n request.resolution = resolveBytePlusVideoResolution(\n model,\n request.resolution,\n )\n }\n\n try {\n logger.request(\n `activity=video.create provider=${this.name} model=${model} size=${size ?? 'default'} duration=${request.duration ?? 'default'}`,\n { provider: this.name, model },\n )\n\n const { response, body } = await this.request(TASKS_PATH, {\n method: 'POST',\n body: JSON.stringify(request),\n })\n if (!response.ok) {\n throw bytePlusArkError(response.status, body, 'video task creation')\n }\n\n const { id } = (body ?? {}) as BytePlusVideoCreateResponse\n if (!id) {\n throw new Error('byteplus: video task creation returned no task id.')\n }\n\n return { jobId: id, model }\n } catch (error: unknown) {\n logger.errors(`${this.name}.createVideoJob fatal`, {\n error: toRunErrorPayload(error, `${this.name}.createVideoJob failed`),\n source: `${this.name}.createVideoJob`,\n })\n throw error\n }\n }\n\n /**\n * Fetches a task, tagging the thrown error with the HTTP status.\n *\n * The 200 body is validated rather than cast. `readJsonBody` returns\n * `undefined` for an empty body and the raw text for a non-JSON one — both\n * documented failure modes of these hosts (an HTML error page from a proxy\n * in front of the API). Casting either to `BytePlusVideoTask` yields a task\n * whose `status` is `undefined`, which {@link mapStatus} would have to\n * interpret; the honest answer is that the response was not a task at all,\n * so say so while the body is still in hand.\n */\n private async retrieveTask(jobId: string): Promise<BytePlusVideoTask> {\n const { response, body } = await this.request(\n `${TASKS_PATH}/${encodeURIComponent(jobId)}`,\n )\n if (!response.ok) {\n const error = bytePlusArkError(response.status, body, 'video task lookup')\n ;(error as { status?: number }).status = response.status\n throw error\n }\n if (typeof body !== 'object' || body === null) {\n throw bytePlusArkError(\n response.status,\n body,\n `video task lookup (job ${jobId}) returned a non-object body`,\n )\n }\n return body as BytePlusVideoTask\n }\n\n async getVideoStatus(jobId: string): Promise<VideoStatusResult> {\n let task: BytePlusVideoTask\n try {\n task = await this.retrieveTask(jobId)\n } catch (error) {\n // A task record lives 7 days from creation; past that the id 404s. Keep\n // Ark's own code/message: a 404 from a wrong baseURL, a proxy, or a\n // region mismatch is not an expired job id, and collapsing them all to\n // \"Job not found\" sends the caller hunting the wrong thing.\n if ((error as { status?: number }).status === 404) {\n return {\n jobId,\n status: 'failed',\n error: `Job not found: ${jobId} (${(error as Error).message})`,\n }\n }\n throw error\n }\n\n const status = this.mapStatus(task.status)\n const failure = status === 'failed' ? describeTaskFailure(task) : undefined\n return {\n jobId,\n status,\n ...(failure !== undefined && { error: failure }),\n }\n }\n\n async getVideoUrl(jobId: string): Promise<VideoUrlResult> {\n let task: BytePlusVideoTask\n try {\n task = await this.retrieveTask(jobId)\n } catch (error) {\n // See getVideoStatus: Ark's detail distinguishes an expired id from a\n // misrouted request.\n if ((error as { status?: number }).status === 404) {\n throw new Error(\n `Video job not found: ${jobId} (${(error as Error).message})`,\n )\n }\n throw error\n }\n\n const status = this.mapStatus(task.status)\n if (status === 'failed') {\n throw new Error(\n `Video generation failed: ${describeTaskFailure(task)}. Job ID: ${jobId}`,\n )\n }\n\n const url = task.content?.video_url\n if (!url) {\n throw new Error(\n `Video is not ready for download. Check status first. Job ID: ${jobId}`,\n )\n }\n\n // The 24-hour window runs from when the output was produced, which is the\n // last status change on a succeeded task — `created_at` anchors the\n // separate 7-day retention of the task record itself, and can be far\n // earlier (a live `flex` task sat queued ~15 minutes). Corroborated by the\n // signed TOS link itself, which carries `X-Tos-Expires=86400` from an\n // `X-Tos-Date` matching `updated_at`. Fall back to `created_at` only when\n // `updated_at` is missing.\n const anchorSeconds = task.updated_at ?? task.created_at\n const expiresAt =\n anchorSeconds !== undefined\n ? new Date(anchorSeconds * 1000 + VIDEO_URL_TTL_MS)\n : undefined\n\n const usage = buildBytePlusVideoUsage(task.usage)\n return {\n jobId,\n url,\n ...(expiresAt && { expiresAt }),\n ...(usage && { usage }),\n }\n }\n\n /**\n * Maps Seedance task states onto the generic video status set. `expired`\n * (the task outlived `execution_expires_after`) and `cancelled` are\n * terminal non-successes, so both report as failed.\n *\n * An unrecognized state throws rather than defaulting to `processing`.\n * Core's poll loop treats `processing` as \"keep waiting\", so mapping an\n * unknown state — a missing `status`, or a terminal one Ark adds later such\n * as `rejected` — onto it means polling until `maxDuration` and then\n * reporting a generic timeout, with the state Ark actually sent never\n * reaching the caller. Failing here names it.\n */\n protected mapStatus(\n apiStatus: BytePlusVideoTaskStatus | string | undefined,\n ): VideoStatusResult['status'] {\n switch (apiStatus) {\n case 'queued':\n return 'pending'\n case 'running':\n return 'processing'\n case 'succeeded':\n return 'completed'\n case 'failed':\n case 'expired':\n case 'cancelled':\n return 'failed'\n case undefined:\n default:\n throw new Error(\n `byteplus: unrecognized Seedance task status ` +\n `${apiStatus === undefined ? '(missing)' : `\"${apiStatus}\"`}. ` +\n `Known states: queued, running, succeeded, failed, expired, cancelled.`,\n )\n }\n }\n\n /**\n * Seedance accepts any whole second inside a per-model range: 4–15s on the\n * 2.0 family, 4–12s on 1.5-pro, 2–12s on the 1.0 models. An unknown model\n * reports the union of those ranges as a UI hint — see\n * `BYTEPLUS_VIDEO_FALLBACK_DURATIONS`, which `createVideoJob` does not snap\n * against.\n */\n override availableDurations(): DurationOptions<number> {\n return getBytePlusVideoDurationOptions(this.model)\n }\n\n /**\n * Coerce a raw seconds value to the closest duration this model accepts\n * (clamped to its range and rounded to whole seconds).\n */\n override snapDuration(seconds: number): number | undefined {\n return snapToDurationOption(seconds, this.availableDurations())\n }\n}\n\n/**\n * Creates a BytePlus Seedance video adapter with an explicit API key.\n * Type resolution happens here at the call site.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * @param model - The model name (e.g., 'seedance-1-0-pro-fast-251015')\n * @param apiKey - Your BytePlus Ark API key\n * @param config - Optional additional configuration\n * @returns Configured BytePlus video adapter instance with resolved types\n *\n * @example\n * ```typescript\n * const adapter = createBytePlusVideo('seedance-1-5-pro-251215', 'ark-...')\n *\n * const { jobId } = await generateVideo({\n * adapter,\n * prompt: 'a guitar being played in a store',\n * size: '16:9_1080p',\n * duration: 5,\n * })\n * ```\n */\nexport function createBytePlusVideo<TModel extends BytePlusVideoModelOrString>(\n model: TModel,\n apiKey: string,\n config?: Omit<BytePlusVideoConfig, 'apiKey'>,\n): BytePlusVideoAdapter<TModel> {\n return new BytePlusVideoAdapter({ apiKey, ...config }, model)\n}\n\n/**\n * Creates a BytePlus Seedance video adapter, reading `ARK_API_KEY` from the\n * environment. Type resolution happens here at the call site.\n *\n * Note that Ark keys are region-isolated and Seedance is only served from the\n * Asia-Pacific endpoint — an EU key will not work here.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * @param model - The model name (e.g., 'dreamina-seedance-2-0-260128')\n * @param config - Optional configuration (excluding apiKey, auto-detected)\n * @returns Configured BytePlus video adapter instance with resolved types\n * @throws Error if ARK_API_KEY is not found in environment\n *\n * @example\n * ```typescript\n * const adapter = byteplusVideo('dreamina-seedance-2-0-260128')\n *\n * // Image-to-video: an un-roled image is the opening frame.\n * const { jobId } = await generateVideo({\n * adapter,\n * prompt: [\n * { type: 'text', content: 'the guitarist starts playing' },\n * { type: 'image', source: { type: 'url', value: 'https://example.com/shop.jpg' } },\n * ],\n * })\n *\n * const status = await getVideoJobStatus({ adapter, jobId })\n * ```\n *\n * ## Models this package does not know yet\n *\n * `model` also accepts any string, so a Seedance id BytePlus publishes after\n * this release works without upgrading. **Seedance 2.5 is the case this exists\n * for**: `dreamina-seedance-2-5-260628` is real and reachable, but its\n * capability cells could not be probed from this repo's account (Ark answers\n * 404 `ModelNotOpen` until the model is activated in the Ark Console), so it\n * is deliberately absent from the narrowed model tables. Passing it here works\n * for an account that has activated it.\n *\n * An unknown id relaxes both halves of the adapter: the `size` type widens to\n * any string, provider options are ungated, and the runtime guards that encode\n * per-model capabilities — resolution tiers, closing-frame and reference-media\n * support, frame cardinality and mode exclusivity, duration snapping — stand\n * down so Ark decides. Known ids are unaffected. See\n * {@link BytePlusVideoModelOrString} for how to discover and probe an id.\n *\n * @example\n * ```typescript\n * // Seedance 2.5, before this package ships probe-verified metadata for it:\n * const adapter = byteplusVideo('dreamina-seedance-2-5-260628')\n * ```\n */\nexport function byteplusVideo<TModel extends BytePlusVideoModelOrString>(\n model: TModel,\n config?: Omit<BytePlusVideoConfig, 'apiKey'>,\n): BytePlusVideoAdapter<TModel> {\n const apiKey = getBytePlusArkAp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AK,gBAAgB,MAAO,gBAAgB,IAChD,KAAA;EAEN,MAAM,QAAQ,wBAAwB,KAAK,KAAK;EAChD,OAAO;GACL;GACA;GACA,GAAI,aAAa,EAAE,UAAU;GAC7B,GAAI,SAAS,EAAE,MAAM;EACvB;CACF;;;;;;;;;;;;;CAcA,UACE,WAC6B;EAC7B,QAAQ,WAAR;GACE,KAAK,UACH,OAAO;GACT,KAAK,WACH,OAAO;GACT,KAAK,aACH,OAAO;GACT,KAAK;GACL,KAAK;GACL,KAAK,aACH,OAAO;GACT,KAAK,KAAA;GACL,SACE,MAAM,IAAI,MACR,+CACK,cAAc,KAAA,IAAY,cAAc,IAAI,UAAU,GAAG,wEAEhE;EACJ;CACF;;;;;;;;CASA,qBAAuD;EACrD,OAAO,gCAAgC,KAAK,KAAK;CACnD;;;;;CAMA,aAAsB,SAAqC;EACzD,OAAO,qBAAqB,SAAS,KAAK,mBAAmB,CAAC;CAChE;AACF;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,oBACd,OACA,QACA,QAC8B;CAC9B,OAAO,IAAI,qBAAqB;EAAE;EAAQ,GAAG;CAAO,GAAG,KAAK;AAC9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuDA,SAAgB,cACd,OACA,QAC8B;CAE9B,OAAO,oBAAoB,OADZ,4BACmB,GAAQ,MAAM;AAClD"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider-specific options for BytePlus Seed Speech ASR
|
|
3
|
+
* (`POST /api/v3/auc/bigmodel/recognize/flash`).
|
|
4
|
+
*
|
|
5
|
+
* Field names mirror the wire format: everything except `uid` and
|
|
6
|
+
* `audio_format` is forwarded inside the request's `request` block.
|
|
7
|
+
*/
|
|
8
|
+
export interface BytePlusTranscriptionProviderOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Recognition model family. Defaults to `bigmodel`; the specific model is
|
|
11
|
+
* selected by the `X-Api-Resource-Id` header rather than by this value.
|
|
12
|
+
*/
|
|
13
|
+
model_name?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Container hint for the submitted audio (`mp3`, `wav`, `ogg`, …). Only
|
|
16
|
+
* needed when the format can't be inferred from the input — a `File`'s name
|
|
17
|
+
* or MIME type, or a data URL's MIME type, is used automatically.
|
|
18
|
+
*/
|
|
19
|
+
audio_format?: string;
|
|
20
|
+
/** Inverse text normalisation: render spoken numbers, dates etc. as digits. */
|
|
21
|
+
enable_itn?: boolean;
|
|
22
|
+
/** Insert punctuation into the transcript. */
|
|
23
|
+
enable_punc?: boolean;
|
|
24
|
+
/** Disfluency removal — drop fillers and stutters. */
|
|
25
|
+
enable_ddc?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Attach speaker labels to each utterance. When present they are surfaced
|
|
28
|
+
* as `segment.speaker` on the result.
|
|
29
|
+
*/
|
|
30
|
+
enable_speaker_info?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Return the per-utterance breakdown as well as the flat transcript.
|
|
33
|
+
* Defaults to `true` so `segments` and `words` are populated.
|
|
34
|
+
*/
|
|
35
|
+
show_utterances?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Spoken-language hint (e.g. `en-US`). Overrides the cross-provider
|
|
38
|
+
* `TranscriptionOptions.language`.
|
|
39
|
+
*/
|
|
40
|
+
language?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Caller identifier echoed back in BytePlus' request logs. Useful for
|
|
43
|
+
* correlating usage; defaults to `tanstack-ai`.
|
|
44
|
+
*/
|
|
45
|
+
uid?: string;
|
|
46
|
+
}
|