@shop-os/modelkit 0.32.0 → 0.34.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/CHANGELOG.md +24 -0
- package/README.md +1 -1
- package/dist/catalog.cjs +61 -1
- package/dist/catalog.cjs.map +1 -1
- package/dist/catalog.d.cts +4 -3
- package/dist/catalog.d.ts +4 -3
- package/dist/catalog.js +61 -1
- package/dist/catalog.js.map +1 -1
- package/dist/index.cjs +599 -265
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.js +598 -265
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -152,6 +152,7 @@ __export(index_exports, {
|
|
|
152
152
|
videoLengthViolation: () => videoLengthViolation,
|
|
153
153
|
videoSizeViolation: () => videoSizeViolation,
|
|
154
154
|
videoToolConstraints: () => videoToolConstraints,
|
|
155
|
+
wavespeedAdapter: () => wavespeedAdapter,
|
|
155
156
|
withRetry: () => withRetry,
|
|
156
157
|
xaiVideoAdapter: () => xaiVideoAdapter
|
|
157
158
|
});
|
|
@@ -177,32 +178,32 @@ var ModelKitError = class _ModelKitError extends Error {
|
|
|
177
178
|
finishReason;
|
|
178
179
|
/** Set when the provider billed a call that still failed; never sent to `onUsage`. */
|
|
179
180
|
usage;
|
|
180
|
-
constructor(
|
|
181
|
-
super(
|
|
181
|
+
constructor(init2) {
|
|
182
|
+
super(init2.message, { cause: init2.cause });
|
|
182
183
|
this.name = "ModelKitError";
|
|
183
|
-
this.code =
|
|
184
|
-
this.provider =
|
|
185
|
-
this.model =
|
|
186
|
-
this.status =
|
|
187
|
-
this.body =
|
|
188
|
-
this.retryable =
|
|
189
|
-
this.finishReason =
|
|
190
|
-
this.usage =
|
|
191
|
-
}
|
|
192
|
-
withUsage(
|
|
193
|
-
const
|
|
184
|
+
this.code = init2.code;
|
|
185
|
+
this.provider = init2.provider;
|
|
186
|
+
this.model = init2.model;
|
|
187
|
+
this.status = init2.status;
|
|
188
|
+
this.body = init2.body;
|
|
189
|
+
this.retryable = init2.retryable ?? defaultRetryable(init2.code);
|
|
190
|
+
this.finishReason = init2.finishReason;
|
|
191
|
+
this.usage = init2.usage;
|
|
192
|
+
}
|
|
193
|
+
withUsage(usage7) {
|
|
194
|
+
const init2 = {
|
|
194
195
|
code: this.code,
|
|
195
196
|
message: this.message,
|
|
196
197
|
cause: this.cause,
|
|
197
198
|
retryable: this.retryable,
|
|
198
|
-
usage:
|
|
199
|
+
usage: usage7
|
|
199
200
|
};
|
|
200
|
-
if (this.provider !== void 0)
|
|
201
|
-
if (this.model !== void 0)
|
|
202
|
-
if (this.status !== void 0)
|
|
203
|
-
if (this.body !== void 0)
|
|
204
|
-
if (this.finishReason !== void 0)
|
|
205
|
-
const copy = new _ModelKitError(
|
|
201
|
+
if (this.provider !== void 0) init2.provider = this.provider;
|
|
202
|
+
if (this.model !== void 0) init2.model = this.model;
|
|
203
|
+
if (this.status !== void 0) init2.status = this.status;
|
|
204
|
+
if (this.body !== void 0) init2.body = this.body;
|
|
205
|
+
if (this.finishReason !== void 0) init2.finishReason = this.finishReason;
|
|
206
|
+
const copy = new _ModelKitError(init2);
|
|
206
207
|
if (this.stack !== void 0) copy.stack = this.stack;
|
|
207
208
|
return copy;
|
|
208
209
|
}
|
|
@@ -5315,6 +5316,22 @@ var TEXT_MODELS = [
|
|
|
5315
5316
|
contextWindow: 1e6
|
|
5316
5317
|
}
|
|
5317
5318
|
}),
|
|
5319
|
+
text("claude-sonnet-5-5", "Claude Sonnet 5.5", "claude-sonnet-5-5", {
|
|
5320
|
+
exposure: [],
|
|
5321
|
+
// Sonnet 5.5 rejects a forced tool_choice, which is how Bedrock's SDK asks for JSON on Sonnet 5.
|
|
5322
|
+
providerOptions: { modelkit: { structuredOutputMode: "outputFormat" } },
|
|
5323
|
+
display: {
|
|
5324
|
+
chat: { providerOptions: ANTHROPIC_ADAPTIVE },
|
|
5325
|
+
provider: "anthropic",
|
|
5326
|
+
costTier: 3,
|
|
5327
|
+
description: "Anthropic Claude Sonnet 5.5 balanced model; text and image input, 1M-token context."
|
|
5328
|
+
},
|
|
5329
|
+
capabilities: {
|
|
5330
|
+
reasoning: true,
|
|
5331
|
+
vision: true,
|
|
5332
|
+
contextWindow: 1e6
|
|
5333
|
+
}
|
|
5334
|
+
}),
|
|
5318
5335
|
text("claude-opus-4-8", "Claude Opus 4.8", "claude-opus-4-8", {
|
|
5319
5336
|
exposure: ["cowork", "space_builder"],
|
|
5320
5337
|
aliases: ["claude-opus-4.8", "claude-opus-4", "claude-opus-4.0", "claude-opus-4-20250514"],
|
|
@@ -6622,6 +6639,7 @@ var GROK_RATIOS = ["1:1", "16:9", "9:16", "4:3", "3:4", "3:2", "2:3"];
|
|
|
6622
6639
|
var H3_RATIOS2 = ["21:9", "16:9", "4:3", "1:1", "3:4", "9:16"];
|
|
6623
6640
|
var H3_REFERENCE_RATIOS = ["adaptive", ...H3_RATIOS2];
|
|
6624
6641
|
var WAN_RATIOS = ["adaptive", "16:9", "4:3", "1:1", "3:4", "9:16"];
|
|
6642
|
+
var MAGI_RATIOS = ["21:9", "16:9", "4:3", "1:1", "3:4", "9:16"];
|
|
6625
6643
|
var NEGATIVE_PROMPT = "blur, distort, and low quality";
|
|
6626
6644
|
var FRAMES_EXCLUDE_REFERENCES = { framesExcludeReferences: true };
|
|
6627
6645
|
var KLING_PROMPT2 = { referenceSyntax: "numbered", maxChars: 2500 };
|
|
@@ -7551,6 +7569,48 @@ var VIDEO_MODELS = [
|
|
|
7551
7569
|
},
|
|
7552
7570
|
capabilities: {}
|
|
7553
7571
|
},
|
|
7572
|
+
{
|
|
7573
|
+
// Sand AI MAGI-2 on WaveSpeed. One row for text-, image- (start/end frame) and
|
|
7574
|
+
// reference-to-video; the adapter picks the endpoint from the inputs. WaveSpeed bills
|
|
7575
|
+
// USD 0.08 per requested second, sound and references included.
|
|
7576
|
+
// https://wavespeed.ai/docs/docs-api/sand-ai/magi-2/reference-to-video
|
|
7577
|
+
id: "magi-2",
|
|
7578
|
+
aliases: ["magi2", "sand-magi-2"],
|
|
7579
|
+
label: "MAGI-2",
|
|
7580
|
+
type: "video",
|
|
7581
|
+
adapter: "wavespeed",
|
|
7582
|
+
providerModelId: "sand-ai/magi-2",
|
|
7583
|
+
lifecycle: "active",
|
|
7584
|
+
exposure: ["cowork", "space_builder"],
|
|
7585
|
+
constraints: {
|
|
7586
|
+
aspectRatios: [...MAGI_RATIOS],
|
|
7587
|
+
resolutions: ["1080p"],
|
|
7588
|
+
durations: { min: 4, max: 30 },
|
|
7589
|
+
preferDefaultDuration: true,
|
|
7590
|
+
references: {
|
|
7591
|
+
images: 10,
|
|
7592
|
+
videos: 5,
|
|
7593
|
+
audios: 5,
|
|
7594
|
+
videoSeconds: { min: 2, max: 30, total: 30 },
|
|
7595
|
+
audioSeconds: { min: 2, max: 30, total: 30 }
|
|
7596
|
+
},
|
|
7597
|
+
supports: ["seed", "lastFrame", "generateAudio"],
|
|
7598
|
+
prompt: { referenceSyntax: "descriptive", maxChars: 15e3 },
|
|
7599
|
+
audio: "optional"
|
|
7600
|
+
},
|
|
7601
|
+
defaults: {
|
|
7602
|
+
durationSeconds: 5,
|
|
7603
|
+
resolution: "1080p",
|
|
7604
|
+
generateAudio: true
|
|
7605
|
+
},
|
|
7606
|
+
usage: { ...VIDEO_USAGE3 },
|
|
7607
|
+
pricingKind: "video_per_second",
|
|
7608
|
+
display: {
|
|
7609
|
+
provider: "sand-ai",
|
|
7610
|
+
description: "Sand AI MAGI-2 video model for text-to-video, image-to-video (start/end frame), and reference-to-video from up to 10 images, 5 videos and 5 audio clips, with native audio; 4 to 30 seconds at 1080p."
|
|
7611
|
+
},
|
|
7612
|
+
capabilities: { ...FRAMES_EXCLUDE_REFERENCES, lastFrameRequiresFirstFrame: true }
|
|
7613
|
+
},
|
|
7554
7614
|
...VIDEO_FAMILY_MODELS,
|
|
7555
7615
|
...VIDEO_AVATAR_MODELS,
|
|
7556
7616
|
...LUMA_VIDEO_MODELS
|
|
@@ -8783,7 +8843,8 @@ var ADAPTER_IDS = [
|
|
|
8783
8843
|
"luma",
|
|
8784
8844
|
"alibaba",
|
|
8785
8845
|
"fish",
|
|
8786
|
-
"elevenlabs"
|
|
8846
|
+
"elevenlabs",
|
|
8847
|
+
"wavespeed"
|
|
8787
8848
|
];
|
|
8788
8849
|
var ModelTypeSchema = import_zod3.z.enum([
|
|
8789
8850
|
"text",
|
|
@@ -9468,9 +9529,20 @@ function missingKey(ctx, name) {
|
|
|
9468
9529
|
}
|
|
9469
9530
|
function bedrockMiddleware(ctx, canonicalModelId) {
|
|
9470
9531
|
let warned = false;
|
|
9532
|
+
const structuredOutputMode = ctx.model.providerOptions?.["modelkit"]?.["structuredOutputMode"];
|
|
9471
9533
|
return {
|
|
9472
9534
|
specificationVersion: "v3",
|
|
9473
|
-
transformParams: async ({ params }) => {
|
|
9535
|
+
transformParams: async ({ params: incoming }) => {
|
|
9536
|
+
const params = typeof structuredOutputMode === "string" && incoming.responseFormat?.type === "json" && incoming.providerOptions?.["anthropic"]?.["structuredOutputMode"] === void 0 ? {
|
|
9537
|
+
...incoming,
|
|
9538
|
+
providerOptions: {
|
|
9539
|
+
...incoming.providerOptions,
|
|
9540
|
+
anthropic: {
|
|
9541
|
+
...incoming.providerOptions?.["anthropic"],
|
|
9542
|
+
structuredOutputMode
|
|
9543
|
+
}
|
|
9544
|
+
}
|
|
9545
|
+
} : incoming;
|
|
9474
9546
|
const anthropic = params.providerOptions?.["anthropic"];
|
|
9475
9547
|
if (typeof anthropic !== "object" || anthropic === null || !("cacheControl" in anthropic))
|
|
9476
9548
|
return params;
|
|
@@ -9571,9 +9643,9 @@ var aiSdkTextAdapter = {
|
|
|
9571
9643
|
if (route2.client === "bedrock-anthropic") return bedrockLanguageModel(ctx, route2);
|
|
9572
9644
|
const { keyName } = route2;
|
|
9573
9645
|
if (keyName === "aws") throw missingKey(ctx, keyName);
|
|
9574
|
-
const
|
|
9575
|
-
if (!
|
|
9576
|
-
const settings = { apiKey, fetch: ctx.fetch };
|
|
9646
|
+
const apiKey2 = ctx.keys[keyName];
|
|
9647
|
+
if (!apiKey2) throw missingKey(ctx, keyName);
|
|
9648
|
+
const settings = { apiKey: apiKey2, fetch: ctx.fetch };
|
|
9577
9649
|
if (route2.baseURL !== void 0) settings.baseURL = route2.baseURL;
|
|
9578
9650
|
if (route2.headers !== void 0) settings.headers = route2.headers;
|
|
9579
9651
|
switch (route2.client) {
|
|
@@ -9592,7 +9664,7 @@ var aiSdkTextAdapter = {
|
|
|
9592
9664
|
case "openai-compatible": {
|
|
9593
9665
|
const model = (0, import_openai_compatible.createOpenAICompatible)({
|
|
9594
9666
|
name: route2.provider,
|
|
9595
|
-
apiKey,
|
|
9667
|
+
apiKey: apiKey2,
|
|
9596
9668
|
baseURL: route2.baseURL,
|
|
9597
9669
|
fetch: ctx.fetch,
|
|
9598
9670
|
...route2.headers ? { headers: route2.headers } : {}
|
|
@@ -10048,16 +10120,16 @@ function classifyHttpError(status, body, provider, model) {
|
|
|
10048
10120
|
else if (status === 408 || status === 504) code = "timeout";
|
|
10049
10121
|
else if (status !== void 0 && status >= 500) code = "network";
|
|
10050
10122
|
const retryable = code === "rate_limited" || code === "timeout" || code === "network";
|
|
10051
|
-
const
|
|
10123
|
+
const init2 = {
|
|
10052
10124
|
code,
|
|
10053
10125
|
message: `${provider} responded ${status ?? "?"}: ${text2.slice(0, 500)}`,
|
|
10054
10126
|
provider,
|
|
10055
10127
|
body,
|
|
10056
10128
|
retryable
|
|
10057
10129
|
};
|
|
10058
|
-
if (status !== void 0)
|
|
10059
|
-
if (model !== void 0)
|
|
10060
|
-
return new ModelKitError(
|
|
10130
|
+
if (status !== void 0) init2.status = status;
|
|
10131
|
+
if (model !== void 0) init2.model = model;
|
|
10132
|
+
return new ModelKitError(init2);
|
|
10061
10133
|
}
|
|
10062
10134
|
function shouldRetry(err) {
|
|
10063
10135
|
if (err instanceof ModelKitError) return err.retryable;
|
|
@@ -10148,15 +10220,15 @@ function throwAborted() {
|
|
|
10148
10220
|
function transportError(provider, cause, model) {
|
|
10149
10221
|
const message = cause instanceof Error ? cause.message : String(cause);
|
|
10150
10222
|
const aborted = cause instanceof Error && cause.name === "AbortError";
|
|
10151
|
-
const
|
|
10223
|
+
const init2 = {
|
|
10152
10224
|
code: aborted ? "timeout" : "network",
|
|
10153
10225
|
message: `${provider} request failed before a response: ${message}`,
|
|
10154
10226
|
provider,
|
|
10155
10227
|
retryable: !aborted,
|
|
10156
10228
|
cause
|
|
10157
10229
|
};
|
|
10158
|
-
if (model !== void 0)
|
|
10159
|
-
return new ModelKitError(
|
|
10230
|
+
if (model !== void 0) init2.model = model;
|
|
10231
|
+
return new ModelKitError(init2);
|
|
10160
10232
|
}
|
|
10161
10233
|
function classifyThrown(provider, cause, model, override = {}) {
|
|
10162
10234
|
if (cause instanceof ModelKitError) return cause;
|
|
@@ -10250,7 +10322,7 @@ function alibabaError(status, body, model, code = alibabaErrorCode(body)) {
|
|
|
10250
10322
|
}
|
|
10251
10323
|
const parsed = import_zod7.z.string().safeParse(body);
|
|
10252
10324
|
const text2 = parsed.success ? parsed.data : JSON.stringify(body ?? "");
|
|
10253
|
-
const
|
|
10325
|
+
const init2 = {
|
|
10254
10326
|
// A verdict with no HTTP status (a failed task) is not a transport failure.
|
|
10255
10327
|
code: rule ?? "provider_rejected",
|
|
10256
10328
|
message: `alibaba rejected the request${code ? ` (${code})` : ""}: ${text2.slice(0, 500)}`,
|
|
@@ -10259,11 +10331,11 @@ function alibabaError(status, body, model, code = alibabaErrorCode(body)) {
|
|
|
10259
10331
|
body,
|
|
10260
10332
|
retryable: false
|
|
10261
10333
|
};
|
|
10262
|
-
if (status !== void 0)
|
|
10263
|
-
return new ModelKitError(
|
|
10334
|
+
if (status !== void 0) init2.status = status;
|
|
10335
|
+
return new ModelKitError(init2);
|
|
10264
10336
|
}
|
|
10265
|
-
function alibabaFetch(ctx, url,
|
|
10266
|
-
const withSignal = ctx.signal === void 0 ?
|
|
10337
|
+
function alibabaFetch(ctx, url, init2) {
|
|
10338
|
+
const withSignal = ctx.signal === void 0 ? init2 : { ...init2, signal: ctx.signal };
|
|
10267
10339
|
return ctx.fetch(url, withSignal).catch((cause) => {
|
|
10268
10340
|
throw classifyThrown("alibaba", cause, ctx.model.id);
|
|
10269
10341
|
});
|
|
@@ -10320,9 +10392,9 @@ async function readBody(res) {
|
|
|
10320
10392
|
}
|
|
10321
10393
|
async function mediaRefToBytes(ref, fetch2, options = {}) {
|
|
10322
10394
|
if (!isUrlRef(ref)) return { bytes: ref.bytes, mime: ref.mime };
|
|
10323
|
-
const
|
|
10324
|
-
if (options.signal !== void 0)
|
|
10325
|
-
const res = await fetch2(ref.url,
|
|
10395
|
+
const init2 = {};
|
|
10396
|
+
if (options.signal !== void 0) init2.signal = options.signal;
|
|
10397
|
+
const res = await fetch2(ref.url, init2);
|
|
10326
10398
|
if (!res.ok) {
|
|
10327
10399
|
const text2 = await res.text().catch(() => "");
|
|
10328
10400
|
throw classifyHttpError(
|
|
@@ -10364,15 +10436,15 @@ function applyInputSpec(spec, req, refToUrl2, opts) {
|
|
|
10364
10436
|
out[providerField] = mapped ?? serialised;
|
|
10365
10437
|
}
|
|
10366
10438
|
for (const [providerField, keys] of Object.entries(spec.arrays ?? {})) {
|
|
10367
|
-
const
|
|
10439
|
+
const urls3 = [];
|
|
10368
10440
|
for (const key of keys) {
|
|
10369
10441
|
if (!isArrayInputSpecKey(key)) continue;
|
|
10370
10442
|
const value = canonicalValue(req, key);
|
|
10371
10443
|
if (value === void 0) continue;
|
|
10372
|
-
if (Array.isArray(value))
|
|
10373
|
-
else if (isMediaRef(value))
|
|
10444
|
+
if (Array.isArray(value)) urls3.push(...value.map(refToUrl2));
|
|
10445
|
+
else if (isMediaRef(value)) urls3.push(refToUrl2(value));
|
|
10374
10446
|
}
|
|
10375
|
-
if (
|
|
10447
|
+
if (urls3.length > 0) out[providerField] = urls3;
|
|
10376
10448
|
}
|
|
10377
10449
|
if (spec.dimensions !== void 0) {
|
|
10378
10450
|
const dims = dimensionsFor(spec.dimensions, req);
|
|
@@ -10540,19 +10612,19 @@ async function generateAlibabaImage(req, ctx) {
|
|
|
10540
10612
|
},
|
|
10541
10613
|
mediaRetryOptions(ctx.call, ctx.signal)
|
|
10542
10614
|
);
|
|
10543
|
-
const
|
|
10615
|
+
const urls3 = (data.output?.choices ?? []).flatMap(
|
|
10544
10616
|
(choice) => (choice.message?.content ?? []).flatMap((part) => part.image ? [part.image] : [])
|
|
10545
10617
|
);
|
|
10546
|
-
if (
|
|
10618
|
+
if (urls3.length === 0) throw alibabaError(void 0, "Qwen Image returned no image", modelId);
|
|
10547
10619
|
const images = await Promise.all(
|
|
10548
|
-
|
|
10620
|
+
urls3.map(async (url) => {
|
|
10549
10621
|
const res = await alibabaFetch(ctx, url, {});
|
|
10550
10622
|
if (!res.ok) throw alibabaError(res.status, await res.text().catch(() => ""), modelId);
|
|
10551
10623
|
const mime = res.headers.get("content-type") ?? "image/png";
|
|
10552
10624
|
return { bytes: new Uint8Array(await res.arrayBuffer()), mime };
|
|
10553
10625
|
})
|
|
10554
10626
|
);
|
|
10555
|
-
const
|
|
10627
|
+
const usage7 = {
|
|
10556
10628
|
model: modelId,
|
|
10557
10629
|
provider: "alibaba",
|
|
10558
10630
|
modality: "IMAGE",
|
|
@@ -10560,9 +10632,9 @@ async function generateAlibabaImage(req, ctx) {
|
|
|
10560
10632
|
quantity: data.usage?.output_image_count ?? images.length
|
|
10561
10633
|
};
|
|
10562
10634
|
const tier = billedTier(data.usage?.output_image_type, req.size);
|
|
10563
|
-
if (tier !== void 0)
|
|
10564
|
-
if (req.quality !== void 0)
|
|
10565
|
-
return { images, usage:
|
|
10635
|
+
if (tier !== void 0) usage7.imageSize = tier;
|
|
10636
|
+
if (req.quality !== void 0) usage7.imageQuality = req.quality;
|
|
10637
|
+
return { images, usage: usage7, raw: data };
|
|
10566
10638
|
}
|
|
10567
10639
|
|
|
10568
10640
|
// src/adapters/alibaba-video.ts
|
|
@@ -11024,17 +11096,17 @@ var RENDERED = /* @__PURE__ */ new Map([
|
|
|
11024
11096
|
[720, "720p"],
|
|
11025
11097
|
[1080, "1080p"]
|
|
11026
11098
|
]);
|
|
11027
|
-
function settleUsage(
|
|
11099
|
+
function settleUsage(usage7, task) {
|
|
11028
11100
|
const reported = task.usage;
|
|
11029
11101
|
if (reported?.duration !== void 0 && reported.duration > 0) {
|
|
11030
|
-
|
|
11102
|
+
usage7.quantity = reported.duration;
|
|
11031
11103
|
}
|
|
11032
11104
|
const output = reported?.output_video_duration;
|
|
11033
|
-
if (output !== void 0 && output > 0)
|
|
11105
|
+
if (output !== void 0 && output > 0) usage7.durationSeconds = output;
|
|
11034
11106
|
const rendered = reported?.SR === void 0 ? void 0 : RENDERED.get(reported.SR);
|
|
11035
|
-
if (rendered !== void 0)
|
|
11107
|
+
if (rendered !== void 0) usage7.resolution = rendered;
|
|
11036
11108
|
}
|
|
11037
|
-
function alibabaVideoJob(ctx, jobId,
|
|
11109
|
+
function alibabaVideoJob(ctx, jobId, usage7) {
|
|
11038
11110
|
const { key } = alibabaCredentials(ctx);
|
|
11039
11111
|
const modelId = ctx.model.id;
|
|
11040
11112
|
const fetchTask = async () => {
|
|
@@ -11078,10 +11150,10 @@ function alibabaVideoJob(ctx, jobId, usage6) {
|
|
|
11078
11150
|
if (!url) {
|
|
11079
11151
|
throw alibabaError(void 0, "task succeeded with no video_url", modelId);
|
|
11080
11152
|
}
|
|
11081
|
-
settleUsage(
|
|
11153
|
+
settleUsage(usage7, task);
|
|
11082
11154
|
return {
|
|
11083
11155
|
video: { url, expiresAt: new Date(Date.now() + VIDEO_URL_TTL_MS) },
|
|
11084
|
-
usage:
|
|
11156
|
+
usage: usage7,
|
|
11085
11157
|
raw: task
|
|
11086
11158
|
};
|
|
11087
11159
|
}
|
|
@@ -11255,7 +11327,7 @@ function throwArkError(status, body, model, code = arkErrorCode(body)) {
|
|
|
11255
11327
|
const parsed = import_zod10.z.string().safeParse(body);
|
|
11256
11328
|
const text2 = parsed.success ? parsed.data : JSON.stringify(body ?? "");
|
|
11257
11329
|
if (/sensitive.?content|human.?face/i.test(`${code ?? ""} ${text2}`)) {
|
|
11258
|
-
const
|
|
11330
|
+
const init2 = {
|
|
11259
11331
|
code: "content_blocked",
|
|
11260
11332
|
message: `ark rejected the request: ${text2.slice(0, 500)}`,
|
|
11261
11333
|
provider: "ark",
|
|
@@ -11263,12 +11335,12 @@ function throwArkError(status, body, model, code = arkErrorCode(body)) {
|
|
|
11263
11335
|
body,
|
|
11264
11336
|
retryable: false
|
|
11265
11337
|
};
|
|
11266
|
-
if (status !== void 0)
|
|
11267
|
-
throw new ModelKitError(
|
|
11338
|
+
if (status !== void 0) init2.status = status;
|
|
11339
|
+
throw new ModelKitError(init2);
|
|
11268
11340
|
}
|
|
11269
11341
|
const rule = arkCodeRule(status, code);
|
|
11270
11342
|
if (rule || status === void 0) {
|
|
11271
|
-
const
|
|
11343
|
+
const init2 = {
|
|
11272
11344
|
code: rule?.code ?? "provider_rejected",
|
|
11273
11345
|
message: `ark rejected the request${code ? ` (${code})` : ""}: ${text2.slice(0, 500)}`,
|
|
11274
11346
|
provider: "ark",
|
|
@@ -11276,8 +11348,8 @@ function throwArkError(status, body, model, code = arkErrorCode(body)) {
|
|
|
11276
11348
|
body,
|
|
11277
11349
|
retryable: false
|
|
11278
11350
|
};
|
|
11279
|
-
if (status !== void 0)
|
|
11280
|
-
throw new ModelKitError(
|
|
11351
|
+
if (status !== void 0) init2.status = status;
|
|
11352
|
+
throw new ModelKitError(init2);
|
|
11281
11353
|
}
|
|
11282
11354
|
throw classifyHttpError(status, body, "ark", model);
|
|
11283
11355
|
}
|
|
@@ -11301,12 +11373,12 @@ var SeedreamResponseSchema = import_zod10.z.object({
|
|
|
11301
11373
|
data: import_zod10.z.array(SeedreamImageSchema).optional(),
|
|
11302
11374
|
error: import_zod10.z.object({ message: import_zod10.z.string().optional(), code: import_zod10.z.string().optional() }).optional()
|
|
11303
11375
|
});
|
|
11304
|
-
function fetchInitFor(ctx,
|
|
11305
|
-
if (ctx.signal !== void 0)
|
|
11306
|
-
return
|
|
11376
|
+
function fetchInitFor(ctx, init2) {
|
|
11377
|
+
if (ctx.signal !== void 0) init2.signal = ctx.signal;
|
|
11378
|
+
return init2;
|
|
11307
11379
|
}
|
|
11308
|
-
function arkFetch(ctx, url,
|
|
11309
|
-
return ctx.fetch(url, fetchInitFor(ctx,
|
|
11380
|
+
function arkFetch(ctx, url, init2) {
|
|
11381
|
+
return ctx.fetch(url, fetchInitFor(ctx, init2)).catch((cause) => {
|
|
11310
11382
|
throw classifyThrown("ark", cause, ctx.model.id);
|
|
11311
11383
|
});
|
|
11312
11384
|
}
|
|
@@ -11383,16 +11455,16 @@ async function generateSeedreamImage(req, ctx) {
|
|
|
11383
11455
|
} else {
|
|
11384
11456
|
throwArkError(void 0, "Seedream returned no image URL or base64 data", modelId);
|
|
11385
11457
|
}
|
|
11386
|
-
const
|
|
11458
|
+
const usage7 = {
|
|
11387
11459
|
model: modelId,
|
|
11388
11460
|
provider: "ark",
|
|
11389
11461
|
modality: "IMAGE",
|
|
11390
11462
|
unit: "images",
|
|
11391
11463
|
quantity: 1
|
|
11392
11464
|
};
|
|
11393
|
-
if (req.size !== void 0)
|
|
11394
|
-
if (req.quality !== void 0)
|
|
11395
|
-
return { images: [{ bytes, mime: sniffedImageMime(bytes) }], usage:
|
|
11465
|
+
if (req.size !== void 0) usage7.imageSize = req.size;
|
|
11466
|
+
if (req.quality !== void 0) usage7.imageQuality = req.quality;
|
|
11467
|
+
return { images: [{ bytes, mime: sniffedImageMime(bytes) }], usage: usage7, raw: data };
|
|
11396
11468
|
}
|
|
11397
11469
|
var SeedanceUsageSchema = import_zod10.z.object({
|
|
11398
11470
|
total_tokens: import_zod10.z.number().optional(),
|
|
@@ -11447,7 +11519,7 @@ function seedanceErrorCode(error) {
|
|
|
11447
11519
|
const parsed = SeedanceErrorSchema.safeParse(error);
|
|
11448
11520
|
return parsed.success ? parsed.data.code : void 0;
|
|
11449
11521
|
}
|
|
11450
|
-
function seedanceJob(ctx, key, jobId,
|
|
11522
|
+
function seedanceJob(ctx, key, jobId, usage7) {
|
|
11451
11523
|
const modelId = ctx.model.id;
|
|
11452
11524
|
const fetchStatus = async () => {
|
|
11453
11525
|
const res = await arkFetch(ctx, `${arkBaseUrl(ctx)}${SUBMIT_PATH2}/${jobId}`, {
|
|
@@ -11497,21 +11569,21 @@ function seedanceJob(ctx, key, jobId, usage6) {
|
|
|
11497
11569
|
modelId
|
|
11498
11570
|
);
|
|
11499
11571
|
if (polled.duration !== void 0 && polled.duration > 0) {
|
|
11500
|
-
|
|
11501
|
-
|
|
11572
|
+
usage7.quantity = polled.duration;
|
|
11573
|
+
usage7.durationSeconds = polled.duration;
|
|
11502
11574
|
}
|
|
11503
11575
|
const rendered = VideoResolutionSchema.safeParse(
|
|
11504
11576
|
polled.resolution?.toLowerCase()
|
|
11505
11577
|
);
|
|
11506
|
-
if (rendered.success)
|
|
11578
|
+
if (rendered.success) usage7.resolution = rendered.data;
|
|
11507
11579
|
const tokens = polled.usage;
|
|
11508
11580
|
if (tokens?.total_tokens !== void 0 && tokens.completion_tokens !== void 0) {
|
|
11509
|
-
|
|
11510
|
-
|
|
11581
|
+
usage7.inputTokens = tokens.total_tokens - tokens.completion_tokens;
|
|
11582
|
+
usage7.outputTokens = tokens.completion_tokens;
|
|
11511
11583
|
}
|
|
11512
11584
|
return {
|
|
11513
11585
|
video: { url, expiresAt: new Date(Date.now() + VIDEO_URL_TTL_MS2) },
|
|
11514
|
-
usage:
|
|
11586
|
+
usage: usage7,
|
|
11515
11587
|
raw: polled
|
|
11516
11588
|
};
|
|
11517
11589
|
}
|
|
@@ -11664,8 +11736,8 @@ async function generateSeedanceVideo(req, ctx) {
|
|
|
11664
11736
|
if (req.watermark !== void 0) body.watermark = req.watermark;
|
|
11665
11737
|
if (ctx.model.constraints.draft) body.draft = true;
|
|
11666
11738
|
const jobId = await submitSeedanceTask(ctx, key, body);
|
|
11667
|
-
const
|
|
11668
|
-
return seedanceJob(ctx, key, jobId,
|
|
11739
|
+
const usage7 = seedanceUsage(ctx, req, taskType === "edit" ? requestedDuration : duration);
|
|
11740
|
+
return seedanceJob(ctx, key, jobId, usage7);
|
|
11669
11741
|
}
|
|
11670
11742
|
|
|
11671
11743
|
// src/adapters/fal.ts
|
|
@@ -11809,15 +11881,15 @@ function gatewayConfig(ctx) {
|
|
|
11809
11881
|
if (ctx.signal !== void 0) config.signal = ctx.signal;
|
|
11810
11882
|
return config;
|
|
11811
11883
|
}
|
|
11812
|
-
async function gatewayFetch(cfg, fetchImpl, targetUrl,
|
|
11884
|
+
async function gatewayFetch(cfg, fetchImpl, targetUrl, init2) {
|
|
11813
11885
|
const headers2 = { "x-fal-target-url": targetUrl };
|
|
11814
11886
|
if (cfg.falKey) headers2.Authorization = `Bearer ${cfg.falKey}`;
|
|
11815
|
-
if (
|
|
11887
|
+
if (init2.body) headers2["Content-Type"] = "application/json";
|
|
11816
11888
|
if (cfg.cfToken) headers2["cf-aig-authorization"] = `Bearer ${cfg.cfToken}`;
|
|
11817
11889
|
if (cfg.singleAttempt) headers2["cf-aig-max-attempts"] = "1";
|
|
11818
|
-
const request = { method:
|
|
11890
|
+
const request = { method: init2.method, headers: headers2 };
|
|
11819
11891
|
if (cfg.signal !== void 0) request.signal = cfg.signal;
|
|
11820
|
-
if (
|
|
11892
|
+
if (init2.body !== void 0) request.body = init2.body;
|
|
11821
11893
|
return fetchImpl(cfg.baseUrl, request);
|
|
11822
11894
|
}
|
|
11823
11895
|
|
|
@@ -12808,7 +12880,7 @@ async function collectFalPlanImages(payload, plan5, ctx, req) {
|
|
|
12808
12880
|
return { bytes, mime: mime || "image/png" };
|
|
12809
12881
|
})
|
|
12810
12882
|
);
|
|
12811
|
-
const
|
|
12883
|
+
const usage7 = {
|
|
12812
12884
|
model: ctx.model.id,
|
|
12813
12885
|
provider: "fal",
|
|
12814
12886
|
modality: "IMAGE",
|
|
@@ -12816,9 +12888,9 @@ async function collectFalPlanImages(payload, plan5, ctx, req) {
|
|
|
12816
12888
|
quantity: images.length
|
|
12817
12889
|
};
|
|
12818
12890
|
const billedSize = plan5.imageSize ?? req.size;
|
|
12819
|
-
if (billedSize !== void 0 && ctx.model.type === "image")
|
|
12820
|
-
if (plan5.imageQuality !== void 0)
|
|
12821
|
-
const result = { images, usage:
|
|
12891
|
+
if (billedSize !== void 0 && ctx.model.type === "image") usage7.imageSize = billedSize;
|
|
12892
|
+
if (plan5.imageQuality !== void 0) usage7.imageQuality = plan5.imageQuality;
|
|
12893
|
+
const result = { images, usage: usage7, raw: payload };
|
|
12822
12894
|
const remote = files.find((f) => !f.url.startsWith("data:"));
|
|
12823
12895
|
if (remote !== void 0) result.outputUrl = remote.url;
|
|
12824
12896
|
return result;
|
|
@@ -14559,14 +14631,14 @@ async function collectFalImages(payload, ctx, req, label) {
|
|
|
14559
14631
|
retryable: false
|
|
14560
14632
|
});
|
|
14561
14633
|
}
|
|
14562
|
-
const
|
|
14634
|
+
const usage7 = imageUsage(ctx, req, delivered.length);
|
|
14563
14635
|
if (isIdeogram46(ctx.model.providerModelId)) {
|
|
14564
14636
|
const billed = ideogramBilledSize(delivered, req.size);
|
|
14565
|
-
if (billed !== void 0)
|
|
14637
|
+
if (billed !== void 0) usage7.imageSize = billed;
|
|
14566
14638
|
}
|
|
14567
14639
|
return {
|
|
14568
14640
|
images: delivered.map(({ bytes, mime }) => ({ bytes, mime })),
|
|
14569
|
-
usage:
|
|
14641
|
+
usage: usage7,
|
|
14570
14642
|
raw: payload
|
|
14571
14643
|
};
|
|
14572
14644
|
}
|
|
@@ -14901,13 +14973,13 @@ function classifyResponse(ctx, status, body) {
|
|
|
14901
14973
|
}
|
|
14902
14974
|
return error;
|
|
14903
14975
|
}
|
|
14904
|
-
async function elevenLabsFetch(ctx, path,
|
|
14976
|
+
async function elevenLabsFetch(ctx, path, init2) {
|
|
14905
14977
|
throwIfAborted(ctx.signal);
|
|
14906
14978
|
const request = {
|
|
14907
|
-
method:
|
|
14908
|
-
headers: { "xi-api-key": elevenLabsKey(ctx), ...
|
|
14979
|
+
method: init2.method,
|
|
14980
|
+
headers: { "xi-api-key": elevenLabsKey(ctx), ...init2.headers }
|
|
14909
14981
|
};
|
|
14910
|
-
if (
|
|
14982
|
+
if (init2.body !== void 0) request.body = init2.body;
|
|
14911
14983
|
if (ctx.signal !== void 0) request.signal = ctx.signal;
|
|
14912
14984
|
let res;
|
|
14913
14985
|
try {
|
|
@@ -15197,13 +15269,13 @@ function fishKey(ctx) {
|
|
|
15197
15269
|
function fishUrl(ctx, path) {
|
|
15198
15270
|
return `${ctx.endpoints?.fish ?? DEFAULT_BASE_URL2}${path}`;
|
|
15199
15271
|
}
|
|
15200
|
-
async function fishFetch(ctx, path,
|
|
15272
|
+
async function fishFetch(ctx, path, init2) {
|
|
15201
15273
|
throwIfAborted(ctx.signal);
|
|
15202
15274
|
const request = {
|
|
15203
|
-
method:
|
|
15204
|
-
headers: { Authorization: `Bearer ${fishKey(ctx)}`, ...
|
|
15275
|
+
method: init2.method,
|
|
15276
|
+
headers: { Authorization: `Bearer ${fishKey(ctx)}`, ...init2.headers }
|
|
15205
15277
|
};
|
|
15206
|
-
if (
|
|
15278
|
+
if (init2.body !== void 0) request.body = init2.body;
|
|
15207
15279
|
if (ctx.signal !== void 0) request.signal = ctx.signal;
|
|
15208
15280
|
let res;
|
|
15209
15281
|
try {
|
|
@@ -15454,13 +15526,13 @@ function googleKey(ctx) {
|
|
|
15454
15526
|
if (!key) throw refuse(ctx, "auth", "Missing Google API key");
|
|
15455
15527
|
return key;
|
|
15456
15528
|
}
|
|
15457
|
-
async function googleFetch(ctx, path,
|
|
15529
|
+
async function googleFetch(ctx, path, init2) {
|
|
15458
15530
|
throwIfAborted(ctx.signal);
|
|
15459
15531
|
const request = {
|
|
15460
|
-
method:
|
|
15532
|
+
method: init2.method,
|
|
15461
15533
|
headers: { "x-goog-api-key": googleKey(ctx), "Content-Type": "application/json" }
|
|
15462
15534
|
};
|
|
15463
|
-
if (
|
|
15535
|
+
if (init2.body !== void 0) request.body = JSON.stringify(init2.body);
|
|
15464
15536
|
if (ctx.signal !== void 0) request.signal = ctx.signal;
|
|
15465
15537
|
let res;
|
|
15466
15538
|
try {
|
|
@@ -15680,20 +15752,20 @@ function fileNameFromUri(fileUri, model) {
|
|
|
15680
15752
|
return name;
|
|
15681
15753
|
}
|
|
15682
15754
|
function oversizeError(bytes, label, model) {
|
|
15683
|
-
const
|
|
15755
|
+
const init2 = {
|
|
15684
15756
|
code: "unsupported_option",
|
|
15685
15757
|
message: `${label}: source is ${bytes} bytes, over the Files API limit of ${GOOGLE_FILES_MAX_BYTES}`,
|
|
15686
15758
|
provider: "google-genai",
|
|
15687
15759
|
retryable: false
|
|
15688
15760
|
};
|
|
15689
|
-
if (model !== void 0)
|
|
15690
|
-
return new ModelKitError(
|
|
15761
|
+
if (model !== void 0) init2.model = model;
|
|
15762
|
+
return new ModelKitError(init2);
|
|
15691
15763
|
}
|
|
15692
15764
|
async function resolveSource(source2, fetchImpl, signal, label, model) {
|
|
15693
15765
|
if ("bytes" in source2) return { bytes: source2.bytes, mimeType: source2.mime };
|
|
15694
|
-
const
|
|
15695
|
-
if (signal !== void 0)
|
|
15696
|
-
const res = await fetchImpl(source2.url,
|
|
15766
|
+
const init2 = {};
|
|
15767
|
+
if (signal !== void 0) init2.signal = signal;
|
|
15768
|
+
const res = await fetchImpl(source2.url, init2);
|
|
15697
15769
|
if (!res.ok) {
|
|
15698
15770
|
const text2 = await res.text().catch(() => "");
|
|
15699
15771
|
throw classifyHttpError(res.status, text2, "google-genai", model);
|
|
@@ -15799,11 +15871,11 @@ async function waitForActive(fileName3, opts) {
|
|
|
15799
15871
|
throw classifyHttpError(res.status, text2, "google-genai", opts.model);
|
|
15800
15872
|
}, pollOptions2);
|
|
15801
15873
|
}
|
|
15802
|
-
async function deleteGoogleFile(fileName3,
|
|
15874
|
+
async function deleteGoogleFile(fileName3, apiKey2, fetchImpl) {
|
|
15803
15875
|
try {
|
|
15804
15876
|
await fetchImpl(`${GOOGLE_FILES_BASE}/v1beta/${fileName3}`, {
|
|
15805
15877
|
method: "DELETE",
|
|
15806
|
-
headers: { "x-goog-api-key":
|
|
15878
|
+
headers: { "x-goog-api-key": apiKey2 }
|
|
15807
15879
|
});
|
|
15808
15880
|
} catch {
|
|
15809
15881
|
}
|
|
@@ -15952,9 +16024,9 @@ function toBase64(bytes) {
|
|
|
15952
16024
|
}
|
|
15953
16025
|
async function resolveMedia(ref, fetchImpl, signal, defaultMime, model) {
|
|
15954
16026
|
if ("bytes" in ref) return { bytes: ref.bytes, mime: ref.mime };
|
|
15955
|
-
const
|
|
15956
|
-
if (signal !== void 0)
|
|
15957
|
-
const res = await fetchImpl(ref.url,
|
|
16027
|
+
const init2 = {};
|
|
16028
|
+
if (signal !== void 0) init2.signal = signal;
|
|
16029
|
+
const res = await fetchImpl(ref.url, init2);
|
|
15958
16030
|
if (!res.ok) {
|
|
15959
16031
|
const text2 = await res.text().catch(() => "");
|
|
15960
16032
|
throw classifyHttpError(res.status, text2, "google-genai", model);
|
|
@@ -15964,10 +16036,10 @@ async function resolveMedia(ref, fetchImpl, signal, defaultMime, model) {
|
|
|
15964
16036
|
mime: res.headers.get("content-type")?.split(";")[0]?.trim() || defaultMime
|
|
15965
16037
|
};
|
|
15966
16038
|
}
|
|
15967
|
-
async function downloadVideo(ctx, url,
|
|
15968
|
-
const
|
|
15969
|
-
if (signal !== void 0)
|
|
15970
|
-
const res = await ctx.fetch(url,
|
|
16039
|
+
async function downloadVideo(ctx, url, apiKey2, signal) {
|
|
16040
|
+
const init2 = { headers: { "x-goog-api-key": apiKey2 } };
|
|
16041
|
+
if (signal !== void 0) init2.signal = signal;
|
|
16042
|
+
const res = await ctx.fetch(url, init2);
|
|
15971
16043
|
if (!res.ok) {
|
|
15972
16044
|
const text2 = await res.text().catch(() => "");
|
|
15973
16045
|
throw classifyHttpError(res.status, text2, "google-genai", ctx.model.id);
|
|
@@ -15990,7 +16062,7 @@ function videoUsage2(ctx, duration, resolution) {
|
|
|
15990
16062
|
return event;
|
|
15991
16063
|
}
|
|
15992
16064
|
async function generateVeoVideo(req, ctx, createGenAIClient) {
|
|
15993
|
-
const
|
|
16065
|
+
const apiKey2 = googleKey2(ctx);
|
|
15994
16066
|
const modelId = ctx.model.id;
|
|
15995
16067
|
const refs = req.referenceImages ?? [];
|
|
15996
16068
|
if (req.firstFrame && refs.length > 0) {
|
|
@@ -16043,7 +16115,7 @@ async function generateVeoVideo(req, ctx, createGenAIClient) {
|
|
|
16043
16115
|
}));
|
|
16044
16116
|
}
|
|
16045
16117
|
const client2 = createGenAIClient({
|
|
16046
|
-
apiKey,
|
|
16118
|
+
apiKey: apiKey2,
|
|
16047
16119
|
...ctx.call.maxRetries !== void 0 ? { maxRetries: ctx.call.maxRetries } : {},
|
|
16048
16120
|
...ctx.signal !== void 0 ? { signal: ctx.signal } : {}
|
|
16049
16121
|
});
|
|
@@ -16080,7 +16152,7 @@ async function generateVeoVideo(req, ctx, createGenAIClient) {
|
|
|
16080
16152
|
}
|
|
16081
16153
|
};
|
|
16082
16154
|
const jobId = operation.name ?? crypto.randomUUID();
|
|
16083
|
-
const
|
|
16155
|
+
const usage7 = videoUsage2(ctx, duration, req.resolution ?? "720p");
|
|
16084
16156
|
return {
|
|
16085
16157
|
jobId,
|
|
16086
16158
|
async poll() {
|
|
@@ -16118,7 +16190,7 @@ async function generateVeoVideo(req, ctx, createGenAIClient) {
|
|
|
16118
16190
|
modelId
|
|
16119
16191
|
);
|
|
16120
16192
|
}
|
|
16121
|
-
return { video: await downloadVideo(ctx, uri,
|
|
16193
|
+
return { video: await downloadVideo(ctx, uri, apiKey2, signal), usage: usage7, raw: finished };
|
|
16122
16194
|
}
|
|
16123
16195
|
};
|
|
16124
16196
|
}
|
|
@@ -16155,7 +16227,7 @@ function extractOmniVideo(interaction, model) {
|
|
|
16155
16227
|
);
|
|
16156
16228
|
}
|
|
16157
16229
|
async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
16158
|
-
const
|
|
16230
|
+
const apiKey2 = googleKey2(ctx);
|
|
16159
16231
|
const modelId = ctx.model.id;
|
|
16160
16232
|
const providerModelId = ctx.model.providerModelId;
|
|
16161
16233
|
const extend = req.taskType === "extend";
|
|
@@ -16215,7 +16287,7 @@ async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
|
16215
16287
|
if (omniResolution !== void 0) responseFormat.resolution = omniResolution;
|
|
16216
16288
|
if (req.durationSeconds !== void 0) responseFormat.duration = `${req.durationSeconds}s`;
|
|
16217
16289
|
const client2 = createGenAIClient({
|
|
16218
|
-
apiKey,
|
|
16290
|
+
apiKey: apiKey2,
|
|
16219
16291
|
...ctx.call.maxRetries !== void 0 ? { maxRetries: ctx.call.maxRetries } : {},
|
|
16220
16292
|
...ctx.signal !== void 0 ? { signal: ctx.signal } : {}
|
|
16221
16293
|
});
|
|
@@ -16223,7 +16295,7 @@ async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
|
16223
16295
|
const uploadedNames = [];
|
|
16224
16296
|
const uploadOne = async (kind, bytes, mime) => {
|
|
16225
16297
|
const uploadOptions = {
|
|
16226
|
-
apiKey,
|
|
16298
|
+
apiKey: apiKey2,
|
|
16227
16299
|
fetchImpl: ctx.fetch,
|
|
16228
16300
|
model: modelId,
|
|
16229
16301
|
label: "gemini-omni"
|
|
@@ -16236,7 +16308,7 @@ async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
|
16236
16308
|
return file.fileUri;
|
|
16237
16309
|
};
|
|
16238
16310
|
const cleanupUploads = async () => {
|
|
16239
|
-
for (const fileName3 of uploadedNames) await deleteGoogleFile(fileName3,
|
|
16311
|
+
for (const fileName3 of uploadedNames) await deleteGoogleFile(fileName3, apiKey2, ctx.fetch);
|
|
16240
16312
|
};
|
|
16241
16313
|
const callCreate = (input) => client2.interactions.create(
|
|
16242
16314
|
previousId !== void 0 ? {
|
|
@@ -16330,7 +16402,7 @@ async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
|
16330
16402
|
if (!(extend && !chained)) await cleanupUploads();
|
|
16331
16403
|
}
|
|
16332
16404
|
const jobId = interaction.id ?? crypto.randomUUID();
|
|
16333
|
-
const
|
|
16405
|
+
const usage7 = videoUsage2(ctx, duration, omniResolution ?? "720p");
|
|
16334
16406
|
const getInteraction = async () => {
|
|
16335
16407
|
try {
|
|
16336
16408
|
const getParams = { stream: false };
|
|
@@ -16378,15 +16450,15 @@ async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
|
16378
16450
|
bytes: Buffer.from(video.data, "base64"),
|
|
16379
16451
|
mime: video.mime_type ?? "video/mp4"
|
|
16380
16452
|
},
|
|
16381
|
-
usage:
|
|
16453
|
+
usage: usage7,
|
|
16382
16454
|
raw: done,
|
|
16383
16455
|
providerRef
|
|
16384
16456
|
};
|
|
16385
16457
|
}
|
|
16386
16458
|
if (video.uri)
|
|
16387
16459
|
return {
|
|
16388
|
-
video: await downloadVideo(ctx, video.uri,
|
|
16389
|
-
usage:
|
|
16460
|
+
video: await downloadVideo(ctx, video.uri, apiKey2, signal),
|
|
16461
|
+
usage: usage7,
|
|
16390
16462
|
raw: done,
|
|
16391
16463
|
providerRef
|
|
16392
16464
|
};
|
|
@@ -16413,7 +16485,7 @@ function lyriaUsage(ctx, req, meta) {
|
|
|
16413
16485
|
return event;
|
|
16414
16486
|
}
|
|
16415
16487
|
async function generateLyriaAudio(req, ctx, createGenAIClient) {
|
|
16416
|
-
const
|
|
16488
|
+
const apiKey2 = googleKey2(ctx);
|
|
16417
16489
|
const modelId = ctx.model.id;
|
|
16418
16490
|
const parts = [{ text: req.prompt }];
|
|
16419
16491
|
for (const ref of req.references ?? []) {
|
|
@@ -16421,7 +16493,7 @@ async function generateLyriaAudio(req, ctx, createGenAIClient) {
|
|
|
16421
16493
|
parts.push({ inlineData: { mimeType: image.mime, data: toBase64(image.bytes) } });
|
|
16422
16494
|
}
|
|
16423
16495
|
const client2 = createGenAIClient({
|
|
16424
|
-
apiKey,
|
|
16496
|
+
apiKey: apiKey2,
|
|
16425
16497
|
...ctx.call.maxRetries !== void 0 ? { maxRetries: ctx.call.maxRetries } : {},
|
|
16426
16498
|
...ctx.signal !== void 0 ? { signal: ctx.signal } : {}
|
|
16427
16499
|
});
|
|
@@ -16475,14 +16547,14 @@ async function generateLyriaAudio(req, ctx, createGenAIClient) {
|
|
|
16475
16547
|
return result;
|
|
16476
16548
|
}
|
|
16477
16549
|
async function generateLyriaInteraction(req, ctx, createGenAIClient) {
|
|
16478
|
-
const
|
|
16550
|
+
const apiKey2 = googleKey2(ctx);
|
|
16479
16551
|
const modelId = ctx.model.id;
|
|
16480
16552
|
const input = [{ type: "text", text: req.prompt }];
|
|
16481
16553
|
for (const ref of req.references ?? []) {
|
|
16482
16554
|
const image = await resolveMedia(ref, ctx.fetch, ctx.signal, "image/jpeg", modelId);
|
|
16483
16555
|
input.push({ type: "image", mime_type: image.mime, data: toBase64(image.bytes) });
|
|
16484
16556
|
}
|
|
16485
|
-
const clientOptions = { apiKey };
|
|
16557
|
+
const clientOptions = { apiKey: apiKey2 };
|
|
16486
16558
|
if (ctx.call.maxRetries !== void 0) clientOptions.maxRetries = ctx.call.maxRetries;
|
|
16487
16559
|
if (ctx.signal !== void 0) clientOptions.signal = ctx.signal;
|
|
16488
16560
|
const client2 = createGenAIClient(clientOptions);
|
|
@@ -16535,9 +16607,9 @@ async function prepareGoogleMediaPart(ref, mediaType, ctx) {
|
|
|
16535
16607
|
if (ref.bytes.length <= MEDIA_PART_INLINE_LIMIT_BYTES) {
|
|
16536
16608
|
return { type: "file", data: ref.bytes, mediaType };
|
|
16537
16609
|
}
|
|
16538
|
-
const
|
|
16610
|
+
const apiKey2 = googleKey2(ctx);
|
|
16539
16611
|
const uploadOptions = {
|
|
16540
|
-
apiKey,
|
|
16612
|
+
apiKey: apiKey2,
|
|
16541
16613
|
fetchImpl: ctx.fetch,
|
|
16542
16614
|
model: ctx.model.id,
|
|
16543
16615
|
label: "media-part"
|
|
@@ -16549,7 +16621,7 @@ async function prepareGoogleMediaPart(ref, mediaType, ctx) {
|
|
|
16549
16621
|
type: "file",
|
|
16550
16622
|
data: file.fileUri,
|
|
16551
16623
|
mediaType: file.mimeType,
|
|
16552
|
-
cleanup: () => deleteGoogleFile(file.fileName,
|
|
16624
|
+
cleanup: () => deleteGoogleFile(file.fileName, apiKey2, ctx.fetch)
|
|
16553
16625
|
};
|
|
16554
16626
|
}
|
|
16555
16627
|
|
|
@@ -16584,9 +16656,9 @@ function extractBlockReason(cause) {
|
|
|
16584
16656
|
current = parsed.data.cause ?? null;
|
|
16585
16657
|
}
|
|
16586
16658
|
}
|
|
16587
|
-
function billedTokens(model,
|
|
16588
|
-
const inputTokens =
|
|
16589
|
-
const outputTokens =
|
|
16659
|
+
function billedTokens(model, usage7) {
|
|
16660
|
+
const inputTokens = usage7?.inputTokens;
|
|
16661
|
+
const outputTokens = usage7?.outputTokens;
|
|
16590
16662
|
if (inputTokens === void 0 && outputTokens === void 0) return void 0;
|
|
16591
16663
|
const event = {
|
|
16592
16664
|
model,
|
|
@@ -16620,8 +16692,8 @@ function createGoogleImageAdapter(deps = defaultGoogleImageDeps) {
|
|
|
16620
16692
|
return {
|
|
16621
16693
|
id: "google-image",
|
|
16622
16694
|
async generateImage(req, ctx) {
|
|
16623
|
-
const
|
|
16624
|
-
if (!
|
|
16695
|
+
const apiKey2 = ctx.keys.google;
|
|
16696
|
+
if (!apiKey2)
|
|
16625
16697
|
throw new ModelKitError({
|
|
16626
16698
|
code: "auth",
|
|
16627
16699
|
message: "Missing API key for google",
|
|
@@ -16629,7 +16701,7 @@ function createGoogleImageAdapter(deps = defaultGoogleImageDeps) {
|
|
|
16629
16701
|
model: ctx.model.id,
|
|
16630
16702
|
retryable: false
|
|
16631
16703
|
});
|
|
16632
|
-
const google = deps.createGoogleClient({ apiKey, fetch: ctx.fetch });
|
|
16704
|
+
const google = deps.createGoogleClient({ apiKey: apiKey2, fetch: ctx.fetch });
|
|
16633
16705
|
const model = google(ctx.model.providerModelId);
|
|
16634
16706
|
const content = [];
|
|
16635
16707
|
for (const ref of req.references ?? []) {
|
|
@@ -16670,7 +16742,7 @@ function createGoogleImageAdapter(deps = defaultGoogleImageDeps) {
|
|
|
16670
16742
|
const images = (result.files ?? []).filter((file) => file.mediaType.startsWith("image/")).map((file) => ({ bytes: file.uint8Array, mime: file.mediaType }));
|
|
16671
16743
|
if (images.length === 0) {
|
|
16672
16744
|
const filtered = result.finishReason === "content-filter" || result.finishReason === "other";
|
|
16673
|
-
const
|
|
16745
|
+
const init2 = {
|
|
16674
16746
|
code: filtered ? "content_blocked" : "provider_rejected",
|
|
16675
16747
|
message: `google-image returned no image (finishReason: ${result.finishReason}). ${result.text}`.trim(),
|
|
16676
16748
|
provider: "google-image",
|
|
@@ -16682,12 +16754,12 @@ function createGoogleImageAdapter(deps = defaultGoogleImageDeps) {
|
|
|
16682
16754
|
},
|
|
16683
16755
|
retryable: false
|
|
16684
16756
|
};
|
|
16685
|
-
if (result.finishReason !== void 0)
|
|
16757
|
+
if (result.finishReason !== void 0) init2.finishReason = result.finishReason;
|
|
16686
16758
|
const billed = billedTokens(ctx.model.id, result.usage);
|
|
16687
|
-
if (billed !== void 0)
|
|
16688
|
-
throw new ModelKitError(
|
|
16759
|
+
if (billed !== void 0) init2.usage = billed;
|
|
16760
|
+
throw new ModelKitError(init2);
|
|
16689
16761
|
}
|
|
16690
|
-
const
|
|
16762
|
+
const usage7 = {
|
|
16691
16763
|
model: ctx.model.id,
|
|
16692
16764
|
provider: "google-image",
|
|
16693
16765
|
modality: "IMAGE",
|
|
@@ -16695,12 +16767,12 @@ function createGoogleImageAdapter(deps = defaultGoogleImageDeps) {
|
|
|
16695
16767
|
quantity: images.length,
|
|
16696
16768
|
imageQuality: "auto"
|
|
16697
16769
|
};
|
|
16698
|
-
if (req.size !== void 0)
|
|
16770
|
+
if (req.size !== void 0) usage7.imageSize = req.size;
|
|
16699
16771
|
if (result.usage?.inputTokens !== void 0)
|
|
16700
|
-
|
|
16772
|
+
usage7.inputTokens = result.usage.inputTokens;
|
|
16701
16773
|
if (result.usage?.outputTokens !== void 0)
|
|
16702
|
-
|
|
16703
|
-
return { images, usage:
|
|
16774
|
+
usage7.outputTokens = result.usage.outputTokens;
|
|
16775
|
+
return { images, usage: usage7, raw: result };
|
|
16704
16776
|
}
|
|
16705
16777
|
};
|
|
16706
16778
|
}
|
|
@@ -16942,16 +17014,16 @@ function klingErrorDetail(error) {
|
|
|
16942
17014
|
if (asObject.success) return asObject.data.message ?? "unknown_error";
|
|
16943
17015
|
return "unknown_error";
|
|
16944
17016
|
}
|
|
16945
|
-
function requestInit(ctx,
|
|
16946
|
-
const request = { method:
|
|
16947
|
-
if (
|
|
17017
|
+
function requestInit(ctx, init2) {
|
|
17018
|
+
const request = { method: init2.method, headers: headers(ctx) };
|
|
17019
|
+
if (init2.body !== void 0) request.body = init2.body;
|
|
16948
17020
|
if (ctx.signal !== void 0) request.signal = ctx.signal;
|
|
16949
17021
|
return request;
|
|
16950
17022
|
}
|
|
16951
|
-
async function createRequest(ctx, url,
|
|
17023
|
+
async function createRequest(ctx, url, init2) {
|
|
16952
17024
|
let res;
|
|
16953
17025
|
try {
|
|
16954
|
-
res = await ctx.fetch(url, requestInit(ctx,
|
|
17026
|
+
res = await ctx.fetch(url, requestInit(ctx, init2));
|
|
16955
17027
|
} catch (cause) {
|
|
16956
17028
|
throw classifyThrown("higgsfield", cause, ctx.model.id);
|
|
16957
17029
|
}
|
|
@@ -17547,13 +17619,13 @@ function lumaFailure(ctx, code, reason) {
|
|
|
17547
17619
|
}
|
|
17548
17620
|
}
|
|
17549
17621
|
function requestInit2(ctx, key, body) {
|
|
17550
|
-
const
|
|
17622
|
+
const init2 = {
|
|
17551
17623
|
method: body !== void 0 ? "POST" : "GET",
|
|
17552
17624
|
headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" }
|
|
17553
17625
|
};
|
|
17554
|
-
if (body !== void 0)
|
|
17555
|
-
if (ctx.signal !== void 0)
|
|
17556
|
-
return
|
|
17626
|
+
if (body !== void 0) init2.body = body;
|
|
17627
|
+
if (ctx.signal !== void 0) init2.signal = ctx.signal;
|
|
17628
|
+
return init2;
|
|
17557
17629
|
}
|
|
17558
17630
|
async function lumaRequest(ctx, url, body) {
|
|
17559
17631
|
const key = lumaKey(ctx);
|
|
@@ -18042,8 +18114,8 @@ var openaiImageAdapter = {
|
|
|
18042
18114
|
id: "openai-image",
|
|
18043
18115
|
async generateImage(req, ctx) {
|
|
18044
18116
|
throwIfAborted(ctx.signal);
|
|
18045
|
-
const
|
|
18046
|
-
if (!
|
|
18117
|
+
const apiKey2 = ctx.keys.openai;
|
|
18118
|
+
if (!apiKey2)
|
|
18047
18119
|
throw new ModelKitError({
|
|
18048
18120
|
code: "auth",
|
|
18049
18121
|
message: "Missing API key for openai",
|
|
@@ -18058,7 +18130,7 @@ var openaiImageAdapter = {
|
|
|
18058
18130
|
message: "openai-image cannot express reference labels; ignoring them"
|
|
18059
18131
|
});
|
|
18060
18132
|
}
|
|
18061
|
-
const client2 = new import_openai2.default({ apiKey, fetch: ctx.fetch, maxRetries: 0 });
|
|
18133
|
+
const client2 = new import_openai2.default({ apiKey: apiKey2, fetch: ctx.fetch, maxRetries: 0 });
|
|
18062
18134
|
const size = req.exactSize !== void 0 ? req.exactSize : req.aspectRatio === void 0 || req.aspectRatio === "adaptive" ? void 0 : aspectRatioToSize(req.aspectRatio, req.size ?? "1K");
|
|
18063
18135
|
const refs = req.references ?? [];
|
|
18064
18136
|
const isEdit = refs.length > 0;
|
|
@@ -18148,7 +18220,7 @@ var openaiImageAdapter = {
|
|
|
18148
18220
|
const usageParsed = openaiUsageSchema.safeParse(response.usage);
|
|
18149
18221
|
const inputTokens = usageParsed.success ? usageParsed.data.input_tokens : void 0;
|
|
18150
18222
|
const outputTokens = usageParsed.success ? usageParsed.data.output_tokens : void 0;
|
|
18151
|
-
const
|
|
18223
|
+
const usage7 = {
|
|
18152
18224
|
model: ctx.model.id,
|
|
18153
18225
|
provider: "openai-image",
|
|
18154
18226
|
modality: "IMAGE",
|
|
@@ -18156,10 +18228,10 @@ var openaiImageAdapter = {
|
|
|
18156
18228
|
quantity: images.length,
|
|
18157
18229
|
imageQuality: quality
|
|
18158
18230
|
};
|
|
18159
|
-
if (inputTokens !== void 0)
|
|
18160
|
-
if (outputTokens !== void 0)
|
|
18161
|
-
if (req.size !== void 0)
|
|
18162
|
-
return { images, usage:
|
|
18231
|
+
if (inputTokens !== void 0) usage7.inputTokens = inputTokens;
|
|
18232
|
+
if (outputTokens !== void 0) usage7.outputTokens = outputTokens;
|
|
18233
|
+
if (req.size !== void 0) usage7.imageSize = req.size;
|
|
18234
|
+
return { images, usage: usage7, raw: response };
|
|
18163
18235
|
}
|
|
18164
18236
|
};
|
|
18165
18237
|
|
|
@@ -18392,13 +18464,13 @@ async function generateProdiaImage(req, ctx) {
|
|
|
18392
18464
|
res = await withRetry(async () => {
|
|
18393
18465
|
let attemptRes;
|
|
18394
18466
|
try {
|
|
18395
|
-
const
|
|
18467
|
+
const init2 = {
|
|
18396
18468
|
method: "POST",
|
|
18397
18469
|
headers: { Authorization: `Bearer ${key}`, Accept: accept },
|
|
18398
18470
|
body: form
|
|
18399
18471
|
};
|
|
18400
|
-
if (ctx.signal !== void 0)
|
|
18401
|
-
attemptRes = await ctx.fetch(PRODIA_JOB_URL,
|
|
18472
|
+
if (ctx.signal !== void 0) init2.signal = ctx.signal;
|
|
18473
|
+
attemptRes = await ctx.fetch(PRODIA_JOB_URL, init2);
|
|
18402
18474
|
} catch (err) {
|
|
18403
18475
|
throw classifyThrown("prodia", err, ctx.model.id);
|
|
18404
18476
|
}
|
|
@@ -18415,16 +18487,16 @@ async function generateProdiaImage(req, ctx) {
|
|
|
18415
18487
|
new Uint8Array(await res.arrayBuffer()),
|
|
18416
18488
|
res.headers.get("content-type")
|
|
18417
18489
|
);
|
|
18418
|
-
const
|
|
18490
|
+
const usage7 = {
|
|
18419
18491
|
model: ctx.model.id,
|
|
18420
18492
|
provider: "prodia",
|
|
18421
18493
|
modality: "IMAGE",
|
|
18422
18494
|
unit: "images",
|
|
18423
18495
|
quantity: 1
|
|
18424
18496
|
};
|
|
18425
|
-
if (req.size !== void 0)
|
|
18426
|
-
if (req.quality !== void 0)
|
|
18427
|
-
return { images: [{ bytes, mime }], usage:
|
|
18497
|
+
if (req.size !== void 0) usage7.imageSize = req.size;
|
|
18498
|
+
if (req.quality !== void 0) usage7.imageQuality = req.quality;
|
|
18499
|
+
return { images: [{ bytes, mime }], usage: usage7, raw: void 0 };
|
|
18428
18500
|
}
|
|
18429
18501
|
var prodiaAdapter = {
|
|
18430
18502
|
id: "prodia",
|
|
@@ -18880,9 +18952,9 @@ async function generateReplicateImage(req, ctx, deps) {
|
|
|
18880
18952
|
}
|
|
18881
18953
|
const { url, predictTime, raw } = await runPrediction(ctx, input, deps);
|
|
18882
18954
|
const file = await download(ctx, url);
|
|
18883
|
-
const
|
|
18884
|
-
|
|
18885
|
-
return { images: [{ bytes: file.bytes, mime: file.mime }], usage:
|
|
18955
|
+
const usage7 = imageUsage3(ctx, req);
|
|
18956
|
+
usage7.quantity = predictTime;
|
|
18957
|
+
return { images: [{ bytes: file.bytes, mime: file.mime }], usage: usage7, raw, outputUrl: url };
|
|
18886
18958
|
}
|
|
18887
18959
|
function klingInput(req) {
|
|
18888
18960
|
const images = (req.firstFrame ? [req.firstFrame] : []).map(refToUrl);
|
|
@@ -19006,9 +19078,9 @@ async function generateReplicateVideo(req, ctx, deps) {
|
|
|
19006
19078
|
return void 0;
|
|
19007
19079
|
};
|
|
19008
19080
|
const toResult = (done) => {
|
|
19009
|
-
const
|
|
19010
|
-
|
|
19011
|
-
return { video: { url: done.url }, usage:
|
|
19081
|
+
const usage7 = videoUsage5(ctx, req);
|
|
19082
|
+
usage7.quantity = done.predictTime;
|
|
19083
|
+
return { video: { url: done.url }, usage: usage7, raw: done.raw };
|
|
19012
19084
|
};
|
|
19013
19085
|
return {
|
|
19014
19086
|
jobId,
|
|
@@ -19115,23 +19187,277 @@ async function generateReplicateAudio(req, ctx, deps) {
|
|
|
19115
19187
|
input = specResult.input;
|
|
19116
19188
|
}
|
|
19117
19189
|
const { url, predictTime, raw } = await runPrediction(ctx, input, deps);
|
|
19118
|
-
const
|
|
19190
|
+
const usage7 = audioUsage(ctx, req);
|
|
19119
19191
|
if (ctx.model.usage.unit === "seconds") {
|
|
19120
19192
|
const seconds3 = elevenLabsMusicDuration(req.durationSeconds);
|
|
19121
|
-
|
|
19122
|
-
|
|
19193
|
+
usage7.quantity = seconds3;
|
|
19194
|
+
usage7.durationSeconds = seconds3;
|
|
19195
|
+
} else {
|
|
19196
|
+
usage7.quantity = predictTime;
|
|
19197
|
+
}
|
|
19198
|
+
return { audio: { url }, usage: usage7, raw: predictionRaw(raw) };
|
|
19199
|
+
}
|
|
19200
|
+
|
|
19201
|
+
// src/adapters/wavespeed.ts
|
|
19202
|
+
var import_zod35 = require("zod");
|
|
19203
|
+
var BASE = "https://api.wavespeed.ai/api/v3";
|
|
19204
|
+
var POLL_INTERVAL_MS3 = 3e3;
|
|
19205
|
+
var POLL_BACKOFF = 1.2;
|
|
19206
|
+
var POLL_MAX_INTERVAL_MS = 15e3;
|
|
19207
|
+
var POLL_TIMEOUT_MS2 = 30 * 6e4;
|
|
19208
|
+
var FAILED_STATUSES = /* @__PURE__ */ new Set(["failed", "cancelled", "timeout", "deleted"]);
|
|
19209
|
+
var VIDEO_MODEL_IDS = /* @__PURE__ */ new Set(["sand-ai/magi-2"]);
|
|
19210
|
+
function wavespeedRowRefusal(row2) {
|
|
19211
|
+
if (row2.type !== "video") return `wavespeed has no ${row2.type} rows`;
|
|
19212
|
+
return VIDEO_MODEL_IDS.has(row2.providerModelId.toLowerCase()) ? void 0 : `wavespeed video rows need providerModelId ${[...VIDEO_MODEL_IDS].join(", ")}`;
|
|
19213
|
+
}
|
|
19214
|
+
function fail4(ctx, code, message) {
|
|
19215
|
+
return new ModelKitError({
|
|
19216
|
+
code,
|
|
19217
|
+
message,
|
|
19218
|
+
provider: "wavespeed",
|
|
19219
|
+
model: ctx.model.id,
|
|
19220
|
+
retryable: false
|
|
19221
|
+
});
|
|
19222
|
+
}
|
|
19223
|
+
function ignore(ctx, field, why) {
|
|
19224
|
+
ctx.warnings.push({
|
|
19225
|
+
code: "option_ignored",
|
|
19226
|
+
field,
|
|
19227
|
+
message: `${ctx.model.id}: ${why}; ${field} ignored`
|
|
19228
|
+
});
|
|
19229
|
+
}
|
|
19230
|
+
function urls2(refs) {
|
|
19231
|
+
return (refs ?? []).map(refToUrl);
|
|
19232
|
+
}
|
|
19233
|
+
function magiRequest(req, ctx) {
|
|
19234
|
+
const images = urls2(req.referenceImages);
|
|
19235
|
+
const videos = urls2(req.referenceVideos);
|
|
19236
|
+
const audios = urls2(req.referenceAudios);
|
|
19237
|
+
const hasReferences2 = images.length + videos.length + audios.length > 0;
|
|
19238
|
+
let mode = "text-to-video";
|
|
19239
|
+
const body = {};
|
|
19240
|
+
if (req.firstFrame) {
|
|
19241
|
+
mode = "image-to-video";
|
|
19242
|
+
body.image = refToUrl(req.firstFrame);
|
|
19243
|
+
if (req.lastFrame) body.last_image = refToUrl(req.lastFrame);
|
|
19244
|
+
if (hasReferences2) ignore(ctx, "references", "image-to-video takes no references");
|
|
19123
19245
|
} else {
|
|
19124
|
-
|
|
19246
|
+
if (req.lastFrame) ignore(ctx, "lastFrame", "an end frame needs a start frame");
|
|
19247
|
+
if (hasReferences2) {
|
|
19248
|
+
mode = "reference-to-video";
|
|
19249
|
+
if (images.length > 0) body.images = images;
|
|
19250
|
+
if (videos.length > 0) body.videos = videos;
|
|
19251
|
+
if (audios.length > 0) body.audios = audios;
|
|
19252
|
+
}
|
|
19253
|
+
}
|
|
19254
|
+
const prompt = req.prompt.trim();
|
|
19255
|
+
if (prompt !== "") body.prompt = prompt;
|
|
19256
|
+
else if (mode !== "image-to-video") {
|
|
19257
|
+
throw fail4(ctx, "missing_input", `${ctx.model.id} ${mode} requires a prompt`);
|
|
19258
|
+
}
|
|
19259
|
+
if (req.durationSeconds !== void 0) body.duration = req.durationSeconds;
|
|
19260
|
+
if (req.aspectRatio !== void 0 && req.aspectRatio !== "adaptive") {
|
|
19261
|
+
body.aspect_ratio = req.aspectRatio;
|
|
19262
|
+
}
|
|
19263
|
+
if (req.generateAudio !== void 0) body.generate_audio = req.generateAudio;
|
|
19264
|
+
if (req.seed !== void 0) body.seed = req.seed;
|
|
19265
|
+
return { path: `${ctx.model.providerModelId.toLowerCase()}/${mode}`, body };
|
|
19266
|
+
}
|
|
19267
|
+
function apiKey(ctx) {
|
|
19268
|
+
const key = ctx.keys.wavespeed;
|
|
19269
|
+
if (!key) throw fail4(ctx, "auth", "Missing API key for wavespeed");
|
|
19270
|
+
return key;
|
|
19271
|
+
}
|
|
19272
|
+
function init(ctx, method, body) {
|
|
19273
|
+
const request = {
|
|
19274
|
+
method,
|
|
19275
|
+
headers: { Authorization: `Bearer ${apiKey(ctx)}`, "Content-Type": "application/json" }
|
|
19276
|
+
};
|
|
19277
|
+
if (body !== void 0) request.body = body;
|
|
19278
|
+
if (ctx.signal !== void 0) request.signal = ctx.signal;
|
|
19279
|
+
return request;
|
|
19280
|
+
}
|
|
19281
|
+
var taskFields = import_zod35.z.object({
|
|
19282
|
+
id: import_zod35.z.string().optional(),
|
|
19283
|
+
status: import_zod35.z.string().optional(),
|
|
19284
|
+
outputs: import_zod35.z.array(import_zod35.z.unknown()).optional(),
|
|
19285
|
+
error: JsonValueSchema.optional()
|
|
19286
|
+
}).refine((task) => task.id !== void 0 || task.status !== void 0);
|
|
19287
|
+
var taskSchema = import_zod35.z.union([
|
|
19288
|
+
import_zod35.z.object({ data: taskFields }).transform((wrapped) => wrapped.data),
|
|
19289
|
+
taskFields
|
|
19290
|
+
]);
|
|
19291
|
+
async function parseTask(res) {
|
|
19292
|
+
let raw;
|
|
19293
|
+
try {
|
|
19294
|
+
raw = await res.json();
|
|
19295
|
+
} catch {
|
|
19296
|
+
return "not_json";
|
|
19125
19297
|
}
|
|
19126
|
-
|
|
19298
|
+
const parsed = taskSchema.safeParse(raw);
|
|
19299
|
+
return parsed.success ? parsed.data : void 0;
|
|
19127
19300
|
}
|
|
19301
|
+
function finalSubmitError(err) {
|
|
19302
|
+
if (err.code === "rate_limited" || !err.retryable) return err;
|
|
19303
|
+
const init2 = {
|
|
19304
|
+
code: err.code,
|
|
19305
|
+
message: `${err.message} (the task may exist; not retried)`,
|
|
19306
|
+
cause: err,
|
|
19307
|
+
retryable: false
|
|
19308
|
+
};
|
|
19309
|
+
if (err.provider !== void 0) init2.provider = err.provider;
|
|
19310
|
+
if (err.model !== void 0) init2.model = err.model;
|
|
19311
|
+
if (err.status !== void 0) init2.status = err.status;
|
|
19312
|
+
if (err.body !== void 0) init2.body = err.body;
|
|
19313
|
+
return new ModelKitError(init2);
|
|
19314
|
+
}
|
|
19315
|
+
function failedTask(ctx, status, error) {
|
|
19316
|
+
const text2 = import_zod35.z.string().safeParse(error);
|
|
19317
|
+
const detail = text2.success ? text2.data || "unknown_error" : error == null ? "unknown_error" : JSON.stringify(error);
|
|
19318
|
+
const classified = classifyHttpError(void 0, detail, "wavespeed", ctx.model.id);
|
|
19319
|
+
return new ModelKitError({
|
|
19320
|
+
code: classified.code === "content_blocked" ? "content_blocked" : "provider_rejected",
|
|
19321
|
+
message: `WaveSpeed generation ${status}: ${detail}`,
|
|
19322
|
+
provider: "wavespeed",
|
|
19323
|
+
model: ctx.model.id,
|
|
19324
|
+
retryable: false
|
|
19325
|
+
});
|
|
19326
|
+
}
|
|
19327
|
+
var TaskFailed = class extends Error {
|
|
19328
|
+
constructor(error) {
|
|
19329
|
+
super(error.message);
|
|
19330
|
+
this.error = error;
|
|
19331
|
+
}
|
|
19332
|
+
error;
|
|
19333
|
+
};
|
|
19334
|
+
function usage5(ctx, req) {
|
|
19335
|
+
const event = {
|
|
19336
|
+
model: ctx.model.id,
|
|
19337
|
+
provider: "wavespeed",
|
|
19338
|
+
modality: "VIDEO",
|
|
19339
|
+
unit: "seconds",
|
|
19340
|
+
quantity: req.durationSeconds ?? 0
|
|
19341
|
+
};
|
|
19342
|
+
if (req.resolution !== void 0) event.resolution = req.resolution;
|
|
19343
|
+
if (req.durationSeconds !== void 0) event.durationSeconds = req.durationSeconds;
|
|
19344
|
+
return event;
|
|
19345
|
+
}
|
|
19346
|
+
function jobFor(req, ctx, taskId) {
|
|
19347
|
+
const resultUrl = `${BASE}/predictions/${encodeURIComponent(taskId)}/result`;
|
|
19348
|
+
const transient = ctx.call.maxRetries !== 0;
|
|
19349
|
+
const check = async (signal) => {
|
|
19350
|
+
const request = init(ctx, "GET");
|
|
19351
|
+
if (signal !== void 0) request.signal = signal;
|
|
19352
|
+
let res;
|
|
19353
|
+
try {
|
|
19354
|
+
res = await ctx.fetch(resultUrl, request);
|
|
19355
|
+
} catch (cause) {
|
|
19356
|
+
if (signal?.aborted || !transient) {
|
|
19357
|
+
throw classifyThrown("wavespeed", cause, ctx.model.id);
|
|
19358
|
+
}
|
|
19359
|
+
return void 0;
|
|
19360
|
+
}
|
|
19361
|
+
if (transient && (res.status === 429 || res.status >= 500)) {
|
|
19362
|
+
await res.body?.cancel().catch(() => void 0);
|
|
19363
|
+
return void 0;
|
|
19364
|
+
}
|
|
19365
|
+
if (!res.ok)
|
|
19366
|
+
throw classifyHttpError(res.status, await readBody(res), "wavespeed", ctx.model.id);
|
|
19367
|
+
const task = await parseTask(res);
|
|
19368
|
+
if (task === "not_json") {
|
|
19369
|
+
if (transient) return void 0;
|
|
19370
|
+
throw new ModelKitError({
|
|
19371
|
+
code: "network",
|
|
19372
|
+
message: "WaveSpeed returned a result response that is not JSON",
|
|
19373
|
+
provider: "wavespeed",
|
|
19374
|
+
model: ctx.model.id
|
|
19375
|
+
});
|
|
19376
|
+
}
|
|
19377
|
+
if (task === void 0) {
|
|
19378
|
+
throw fail4(ctx, "provider_rejected", "WaveSpeed returned an unreadable task");
|
|
19379
|
+
}
|
|
19380
|
+
if (task.status === "completed") {
|
|
19381
|
+
const url = import_zod35.z.string().safeParse(task.outputs?.[0]);
|
|
19382
|
+
if (!url.success)
|
|
19383
|
+
throw fail4(ctx, "provider_rejected", "WaveSpeed completed without a video URL");
|
|
19384
|
+
return url.data;
|
|
19385
|
+
}
|
|
19386
|
+
if (task.status !== void 0 && FAILED_STATUSES.has(task.status)) {
|
|
19387
|
+
throw new TaskFailed(failedTask(ctx, task.status, task.error));
|
|
19388
|
+
}
|
|
19389
|
+
return void 0;
|
|
19390
|
+
};
|
|
19391
|
+
const unwrap = async (signal) => {
|
|
19392
|
+
try {
|
|
19393
|
+
return await check(signal);
|
|
19394
|
+
} catch (cause) {
|
|
19395
|
+
throw cause instanceof TaskFailed ? cause.error : cause;
|
|
19396
|
+
}
|
|
19397
|
+
};
|
|
19398
|
+
return {
|
|
19399
|
+
jobId: taskId,
|
|
19400
|
+
async poll() {
|
|
19401
|
+
try {
|
|
19402
|
+
return await check(ctx.signal) ? { status: "succeeded" } : { status: "pending" };
|
|
19403
|
+
} catch (cause) {
|
|
19404
|
+
if (cause instanceof TaskFailed) return { status: "failed" };
|
|
19405
|
+
if (ctx.signal?.aborted || ctx.call.maxRetries === 0) throw cause;
|
|
19406
|
+
if (cause instanceof ModelKitError && !cause.retryable) return { status: "failed" };
|
|
19407
|
+
throw cause;
|
|
19408
|
+
}
|
|
19409
|
+
},
|
|
19410
|
+
async result(opts) {
|
|
19411
|
+
const signal = combineSignals(ctx.signal, opts?.signal);
|
|
19412
|
+
const settings = {
|
|
19413
|
+
intervalMs: opts?.pollIntervalMs ?? POLL_INTERVAL_MS3,
|
|
19414
|
+
backoff: POLL_BACKOFF,
|
|
19415
|
+
maxIntervalMs: POLL_MAX_INTERVAL_MS,
|
|
19416
|
+
timeoutMs: opts?.timeoutMs ?? POLL_TIMEOUT_MS2
|
|
19417
|
+
};
|
|
19418
|
+
if (signal !== void 0) settings.signal = signal;
|
|
19419
|
+
const url = await pollUntil(() => unwrap(signal), settings);
|
|
19420
|
+
const result = { video: { url }, usage: usage5(ctx, req) };
|
|
19421
|
+
return result;
|
|
19422
|
+
}
|
|
19423
|
+
};
|
|
19424
|
+
}
|
|
19425
|
+
var PROVIDER_DEFAULT_SECONDS = 5;
|
|
19426
|
+
function wholeSeconds2(req, ctx) {
|
|
19427
|
+
const seconds3 = req.durationSeconds ?? ctx.model.defaults.durationSeconds ?? PROVIDER_DEFAULT_SECONDS;
|
|
19428
|
+
return { ...req, durationSeconds: Math.round(seconds3) };
|
|
19429
|
+
}
|
|
19430
|
+
async function generateWavespeedVideo(req, ctx) {
|
|
19431
|
+
throwIfAborted(ctx.signal);
|
|
19432
|
+
const sent = wholeSeconds2(req, ctx);
|
|
19433
|
+
const { path, body } = magiRequest(sent, ctx);
|
|
19434
|
+
let res;
|
|
19435
|
+
try {
|
|
19436
|
+
res = await ctx.fetch(`${BASE}/${path}`, init(ctx, "POST", JSON.stringify(body)));
|
|
19437
|
+
} catch (cause) {
|
|
19438
|
+
throw finalSubmitError(classifyThrown("wavespeed", cause, ctx.model.id));
|
|
19439
|
+
}
|
|
19440
|
+
if (!res.ok) {
|
|
19441
|
+
const err = classifyHttpError(res.status, await readBody(res), "wavespeed", ctx.model.id);
|
|
19442
|
+
throw finalSubmitError(err);
|
|
19443
|
+
}
|
|
19444
|
+
const task = await parseTask(res);
|
|
19445
|
+
const taskId = task === "not_json" ? void 0 : task?.id;
|
|
19446
|
+
if (!taskId) throw fail4(ctx, "provider_rejected", "WaveSpeed submit returned no task id");
|
|
19447
|
+
return jobFor(sent, ctx, taskId);
|
|
19448
|
+
}
|
|
19449
|
+
var wavespeedAdapter = {
|
|
19450
|
+
id: "wavespeed",
|
|
19451
|
+
generateVideo: generateWavespeedVideo,
|
|
19452
|
+
resumeVideo: (req, jobId, ctx) => jobFor(wholeSeconds2(req, ctx), ctx, jobId)
|
|
19453
|
+
};
|
|
19128
19454
|
|
|
19129
19455
|
// src/adapters/xai-video.ts
|
|
19130
19456
|
var import_ai3 = require("ai");
|
|
19131
19457
|
var import_xai2 = require("@ai-sdk/xai");
|
|
19132
|
-
var
|
|
19133
|
-
var
|
|
19134
|
-
var
|
|
19458
|
+
var import_zod36 = require("zod");
|
|
19459
|
+
var POLL_INTERVAL_MS4 = 1e4;
|
|
19460
|
+
var POLL_TIMEOUT_MS3 = 15 * 6e4;
|
|
19135
19461
|
var defaultXaiDeps = {
|
|
19136
19462
|
createXaiClient: () => ({ generateVideo: import_ai3.experimental_generateVideo })
|
|
19137
19463
|
};
|
|
@@ -19142,8 +19468,8 @@ function createXaiVideoAdapter(deps = defaultXaiDeps) {
|
|
|
19142
19468
|
};
|
|
19143
19469
|
}
|
|
19144
19470
|
var xaiVideoAdapter = createXaiVideoAdapter();
|
|
19145
|
-
var xaiResponseStatusSchema =
|
|
19146
|
-
response:
|
|
19471
|
+
var xaiResponseStatusSchema = import_zod36.z.object({
|
|
19472
|
+
response: import_zod36.z.object({ status: import_zod36.z.number() }).partial()
|
|
19147
19473
|
});
|
|
19148
19474
|
function xaiError(cause, ctx) {
|
|
19149
19475
|
const info = describeThrown(cause);
|
|
@@ -19151,12 +19477,12 @@ function xaiError(cause, ctx) {
|
|
|
19151
19477
|
const status = info.status ?? (nested.success ? nested.data.response?.status : void 0);
|
|
19152
19478
|
return classifyThrown("xai-video", cause, ctx.model.id, { status, body: info.body });
|
|
19153
19479
|
}
|
|
19154
|
-
var xaiMetaSchema =
|
|
19480
|
+
var xaiMetaSchema = import_zod36.z.object({
|
|
19155
19481
|
videoUrl: JsonValueSchema.optional(),
|
|
19156
19482
|
costInUsdTicks: JsonValueSchema.optional(),
|
|
19157
19483
|
duration: JsonValueSchema.optional()
|
|
19158
19484
|
});
|
|
19159
|
-
var xaiProviderMetadataSchema =
|
|
19485
|
+
var xaiProviderMetadataSchema = import_zod36.z.object({ xai: xaiMetaSchema });
|
|
19160
19486
|
function metaOf(result) {
|
|
19161
19487
|
const parsed = xaiProviderMetadataSchema.partial().safeParse(result.providerMetadata);
|
|
19162
19488
|
if (!parsed.success) return {};
|
|
@@ -19164,8 +19490,8 @@ function metaOf(result) {
|
|
|
19164
19490
|
}
|
|
19165
19491
|
var SDK_REFERENCE_CAP = 7;
|
|
19166
19492
|
async function generateXaiVideo(req, ctx, deps) {
|
|
19167
|
-
const
|
|
19168
|
-
if (!
|
|
19493
|
+
const apiKey2 = ctx.keys.xai;
|
|
19494
|
+
if (!apiKey2) {
|
|
19169
19495
|
throw new ModelKitError({
|
|
19170
19496
|
code: "auth",
|
|
19171
19497
|
message: "Missing API key for xai",
|
|
@@ -19180,8 +19506,8 @@ async function generateXaiVideo(req, ctx, deps) {
|
|
|
19180
19506
|
const takesLastFrame = (ctx.model.constraints.supports ?? []).includes("lastFrame");
|
|
19181
19507
|
const lastFrame = takesLastFrame && req.lastFrame ? refToUrl(req.lastFrame) : void 0;
|
|
19182
19508
|
const xaiOptions = {
|
|
19183
|
-
pollIntervalMs:
|
|
19184
|
-
pollTimeoutMs:
|
|
19509
|
+
pollIntervalMs: POLL_INTERVAL_MS4,
|
|
19510
|
+
pollTimeoutMs: POLL_TIMEOUT_MS3
|
|
19185
19511
|
};
|
|
19186
19512
|
const resolution = xaiResolution(req, ctx, refs);
|
|
19187
19513
|
if (resolution !== void 0) xaiOptions.resolution = resolution;
|
|
@@ -19201,8 +19527,8 @@ async function generateXaiVideo(req, ctx, deps) {
|
|
|
19201
19527
|
message: "xai-video does not support lastFrame; ignored"
|
|
19202
19528
|
});
|
|
19203
19529
|
}
|
|
19204
|
-
const client2 = deps.createXaiClient({ apiKey });
|
|
19205
|
-
const model = (0, import_xai2.createXai)({ apiKey, fetch: ctx.fetch }).video(ctx.model.providerModelId);
|
|
19530
|
+
const client2 = deps.createXaiClient({ apiKey: apiKey2 });
|
|
19531
|
+
const model = (0, import_xai2.createXai)({ apiKey: apiKey2, fetch: ctx.fetch }).video(ctx.model.providerModelId);
|
|
19206
19532
|
const callBody = {
|
|
19207
19533
|
model,
|
|
19208
19534
|
prompt: req.prompt,
|
|
@@ -19255,8 +19581,8 @@ async function generateXaiVideo(req, ctx, deps) {
|
|
|
19255
19581
|
async result(opts) {
|
|
19256
19582
|
const resultSignal = opts?.signal ?? ctx.signal;
|
|
19257
19583
|
const resultOpts = {
|
|
19258
|
-
intervalMs: opts?.pollIntervalMs ??
|
|
19259
|
-
timeoutMs: opts?.timeoutMs ??
|
|
19584
|
+
intervalMs: opts?.pollIntervalMs ?? POLL_INTERVAL_MS4,
|
|
19585
|
+
timeoutMs: opts?.timeoutMs ?? POLL_TIMEOUT_MS3
|
|
19260
19586
|
};
|
|
19261
19587
|
if (resultSignal) resultOpts.signal = resultSignal;
|
|
19262
19588
|
return pollUntil(check, resultOpts);
|
|
@@ -19268,12 +19594,12 @@ async function mapResult(ctx, req, result) {
|
|
|
19268
19594
|
if (first) {
|
|
19269
19595
|
return {
|
|
19270
19596
|
video: { bytes: Uint8Array.from(first.uint8Array), mime: first.mediaType },
|
|
19271
|
-
usage:
|
|
19597
|
+
usage: usage6(ctx, req, metaOf(result)),
|
|
19272
19598
|
raw: result.providerMetadata
|
|
19273
19599
|
};
|
|
19274
19600
|
}
|
|
19275
19601
|
const meta = metaOf(result);
|
|
19276
|
-
const videoUrl =
|
|
19602
|
+
const videoUrl = import_zod36.z.string().safeParse(meta.videoUrl);
|
|
19277
19603
|
if (videoUrl.success && videoUrl.data) {
|
|
19278
19604
|
const res = await ctx.fetch(videoUrl.data);
|
|
19279
19605
|
if (!res.ok) {
|
|
@@ -19283,7 +19609,7 @@ async function mapResult(ctx, req, result) {
|
|
|
19283
19609
|
const mime = res.headers.get("content-type") ?? "video/mp4";
|
|
19284
19610
|
return {
|
|
19285
19611
|
video: { bytes: new Uint8Array(await res.arrayBuffer()), mime },
|
|
19286
|
-
usage:
|
|
19612
|
+
usage: usage6(ctx, req, meta),
|
|
19287
19613
|
raw: result.providerMetadata
|
|
19288
19614
|
};
|
|
19289
19615
|
}
|
|
@@ -19295,8 +19621,8 @@ async function mapResult(ctx, req, result) {
|
|
|
19295
19621
|
retryable: false
|
|
19296
19622
|
});
|
|
19297
19623
|
}
|
|
19298
|
-
function
|
|
19299
|
-
const costTicks =
|
|
19624
|
+
function usage6(ctx, req, meta) {
|
|
19625
|
+
const costTicks = import_zod36.z.number().safeParse(meta.costInUsdTicks);
|
|
19300
19626
|
const cost = costTicks.success ? costTicks.data / 1e10 : void 0;
|
|
19301
19627
|
const event = {
|
|
19302
19628
|
model: ctx.model.id,
|
|
@@ -19338,7 +19664,8 @@ var DEFAULT_ADAPTERS = {
|
|
|
19338
19664
|
luma: lumaAdapter,
|
|
19339
19665
|
alibaba: alibabaAdapter,
|
|
19340
19666
|
fish: fishAdapter,
|
|
19341
|
-
elevenlabs: elevenLabsAdapter
|
|
19667
|
+
elevenlabs: elevenLabsAdapter,
|
|
19668
|
+
wavespeed: wavespeedAdapter
|
|
19342
19669
|
};
|
|
19343
19670
|
|
|
19344
19671
|
// src/catalog/data-only.ts
|
|
@@ -19481,6 +19808,10 @@ function dataOnlyCapable(row2) {
|
|
|
19481
19808
|
const reason = elevenLabsRowRefusal(row2);
|
|
19482
19809
|
return reason === void 0 ? { ok: true } : { ok: false, reason };
|
|
19483
19810
|
}
|
|
19811
|
+
case "wavespeed": {
|
|
19812
|
+
const reason = wavespeedRowRefusal(row2);
|
|
19813
|
+
return reason === void 0 ? { ok: true } : { ok: false, reason };
|
|
19814
|
+
}
|
|
19484
19815
|
default: {
|
|
19485
19816
|
return assertNever(row2.adapter);
|
|
19486
19817
|
}
|
|
@@ -19500,22 +19831,22 @@ var liveState = store[KEY] ??= {
|
|
|
19500
19831
|
var liveCatalog = liveState.catalog;
|
|
19501
19832
|
|
|
19502
19833
|
// src/catalog/load.ts
|
|
19503
|
-
var
|
|
19834
|
+
var import_zod37 = require("zod");
|
|
19504
19835
|
function rowCount() {
|
|
19505
19836
|
return liveCatalog.list({ lifecycle: ["active", "deprecated", "retired"] }).length;
|
|
19506
19837
|
}
|
|
19507
19838
|
function authHeaders2(auth) {
|
|
19508
19839
|
return "bearer" in auth ? { Authorization: `Bearer ${auth.bearer}` } : { "x-api-key": auth.apiKey };
|
|
19509
19840
|
}
|
|
19510
|
-
var EnvelopeSchema =
|
|
19511
|
-
rows:
|
|
19512
|
-
revision:
|
|
19841
|
+
var EnvelopeSchema = import_zod37.z.object({
|
|
19842
|
+
rows: import_zod37.z.array(JsonValueSchema),
|
|
19843
|
+
revision: import_zod37.z.string().optional()
|
|
19513
19844
|
});
|
|
19514
19845
|
function dropReason(path, message) {
|
|
19515
19846
|
return path.length === 0 ? message : `${path.map((part) => String(part)).join(".")} ${message}`;
|
|
19516
19847
|
}
|
|
19517
19848
|
function rowId(raw) {
|
|
19518
|
-
const parsed =
|
|
19849
|
+
const parsed = import_zod37.z.object({ id: import_zod37.z.string().min(1) }).loose().safeParse(raw);
|
|
19519
19850
|
if (!parsed.success) return "?";
|
|
19520
19851
|
return parsed.data.id;
|
|
19521
19852
|
}
|
|
@@ -19732,7 +20063,7 @@ function fileCatalogCache(path) {
|
|
|
19732
20063
|
}
|
|
19733
20064
|
|
|
19734
20065
|
// src/catalog/input-billing.ts
|
|
19735
|
-
var
|
|
20066
|
+
var import_zod38 = require("zod");
|
|
19736
20067
|
var DATA_ONLY_INPUT_BILLING = /* @__PURE__ */ new Map([
|
|
19737
20068
|
["flux-3", "source-video-rate"]
|
|
19738
20069
|
]);
|
|
@@ -19741,7 +20072,7 @@ var KIT_INPUT_BILLING = new Map(
|
|
|
19741
20072
|
(row2) => row2.constraints.inputBilling ? [[row2.id, row2.constraints.inputBilling]] : []
|
|
19742
20073
|
)
|
|
19743
20074
|
);
|
|
19744
|
-
var ConstraintsReadView2 =
|
|
20075
|
+
var ConstraintsReadView2 = import_zod38.z.looseObject({
|
|
19745
20076
|
inputBilling: InputBillingSchema.optional().catch(void 0)
|
|
19746
20077
|
});
|
|
19747
20078
|
function inputBillingOf(row2) {
|
|
@@ -21046,7 +21377,8 @@ var ADAPTER_KEYS = {
|
|
|
21046
21377
|
luma: ["luma"],
|
|
21047
21378
|
alibaba: ["alibaba"],
|
|
21048
21379
|
fish: ["fish"],
|
|
21049
|
-
elevenlabs: ["elevenlabs"]
|
|
21380
|
+
elevenlabs: ["elevenlabs"],
|
|
21381
|
+
wavespeed: ["wavespeed"]
|
|
21050
21382
|
};
|
|
21051
21383
|
var PAIRED = {
|
|
21052
21384
|
higgsfield: "higgsfieldSecret",
|
|
@@ -21150,6 +21482,7 @@ function keysFromEnv(env, prefix = "") {
|
|
|
21150
21482
|
set("alibabaWorkspace", pick2("DASHSCOPE_WORKSPACE_ID"));
|
|
21151
21483
|
set("fish", pick2("FISH_AUDIO_API_KEY"));
|
|
21152
21484
|
set("elevenlabs", pick2("ELEVENLABS_API_KEY"));
|
|
21485
|
+
set("wavespeed", pick2("WAVESPEED_API_KEY"));
|
|
21153
21486
|
set("cloudflare", pick2("CF_AIG_TOKEN"));
|
|
21154
21487
|
const bearer2 = pick2("AWS_BEARER_TOKEN_BEDROCK");
|
|
21155
21488
|
const accessKeyId = pick2("AWS_ACCESS_KEY_ID");
|
|
@@ -21204,12 +21537,12 @@ function modelsPoweredBy(provider, catalog) {
|
|
|
21204
21537
|
function keyOverrideFor(secrets) {
|
|
21205
21538
|
const override = {};
|
|
21206
21539
|
for (const provider of Object.values(BYOK_PROVIDERS)) {
|
|
21207
|
-
const
|
|
21208
|
-
if (
|
|
21540
|
+
const apiKey2 = secrets[provider.id]?.apiKey?.trim();
|
|
21541
|
+
if (apiKey2) override[provider.keyName] = apiKey2;
|
|
21209
21542
|
}
|
|
21210
21543
|
return override;
|
|
21211
21544
|
}
|
|
21212
|
-
var bearer = (
|
|
21545
|
+
var bearer = (apiKey2) => ({ Authorization: `Bearer ${apiKey2}` });
|
|
21213
21546
|
var PROBES = {
|
|
21214
21547
|
ark: {
|
|
21215
21548
|
url: (endpoints) => `${endpoints.ark ?? ARK_BASE_URL}/contents/generations/tasks?page_num=1&page_size=1`,
|
|
@@ -21217,7 +21550,7 @@ var PROBES = {
|
|
|
21217
21550
|
},
|
|
21218
21551
|
fal: {
|
|
21219
21552
|
url: () => "https://api.fal.ai/v1/assets/collections?limit=1",
|
|
21220
|
-
headers: (
|
|
21553
|
+
headers: (apiKey2) => ({ Authorization: `Key ${apiKey2}` }),
|
|
21221
21554
|
unverifiableStatus: 403
|
|
21222
21555
|
},
|
|
21223
21556
|
replicate: {
|
|
@@ -21231,20 +21564,20 @@ var PROBES = {
|
|
|
21231
21564
|
},
|
|
21232
21565
|
google: {
|
|
21233
21566
|
url: () => "https://generativelanguage.googleapis.com/v1beta/models?pageSize=1",
|
|
21234
|
-
headers: (
|
|
21567
|
+
headers: (apiKey2) => ({ "x-goog-api-key": apiKey2 }),
|
|
21235
21568
|
unverifiableStatus: 403
|
|
21236
21569
|
}
|
|
21237
21570
|
};
|
|
21238
21571
|
async function testProviderKey(provider, secret, opts = {}) {
|
|
21239
|
-
const
|
|
21240
|
-
if (!
|
|
21572
|
+
const apiKey2 = secret.apiKey?.trim();
|
|
21573
|
+
if (!apiKey2) return { ok: false, reason: "invalid" };
|
|
21241
21574
|
const probe = PROBES[provider];
|
|
21242
21575
|
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
21243
21576
|
let res;
|
|
21244
21577
|
try {
|
|
21245
21578
|
res = await fetchImpl(probe.url(opts.endpoints ?? {}), {
|
|
21246
21579
|
method: "GET",
|
|
21247
|
-
headers: probe.headers(
|
|
21580
|
+
headers: probe.headers(apiKey2),
|
|
21248
21581
|
...opts.signal !== void 0 && { signal: opts.signal }
|
|
21249
21582
|
});
|
|
21250
21583
|
} catch (cause) {
|
|
@@ -21261,7 +21594,7 @@ async function testProviderKey(provider, secret, opts = {}) {
|
|
|
21261
21594
|
|
|
21262
21595
|
// src/text.ts
|
|
21263
21596
|
var import_ai4 = require("ai");
|
|
21264
|
-
var
|
|
21597
|
+
var import_zod39 = require("zod");
|
|
21265
21598
|
function mergeTextProviderOptions(catalog, caller) {
|
|
21266
21599
|
const merged = {};
|
|
21267
21600
|
const catalogOptions = catalog.providerOptions ?? {};
|
|
@@ -21300,17 +21633,17 @@ function passthroughTextModel(model) {
|
|
|
21300
21633
|
pricingKind: "token"
|
|
21301
21634
|
};
|
|
21302
21635
|
}
|
|
21303
|
-
var perplexityCostSchema =
|
|
21304
|
-
perplexity:
|
|
21636
|
+
var perplexityCostSchema = import_zod39.z.object({
|
|
21637
|
+
perplexity: import_zod39.z.object({ cost: import_zod39.z.object({ totalCost: import_zod39.z.number().finite().nonnegative() }) })
|
|
21305
21638
|
});
|
|
21306
21639
|
function reportedCostUsd(metadata) {
|
|
21307
21640
|
const parsed = perplexityCostSchema.safeParse(metadata);
|
|
21308
21641
|
return parsed.success ? parsed.data.perplexity.cost.totalCost : void 0;
|
|
21309
21642
|
}
|
|
21310
|
-
function textUsageEvent(model, provider,
|
|
21311
|
-
const inputTokens =
|
|
21312
|
-
const outputTokens =
|
|
21313
|
-
const total =
|
|
21643
|
+
function textUsageEvent(model, provider, usage7, providerMetadata) {
|
|
21644
|
+
const inputTokens = usage7.inputTokens ?? 0;
|
|
21645
|
+
const outputTokens = usage7.outputTokens ?? 0;
|
|
21646
|
+
const total = usage7.totalTokens ?? inputTokens + outputTokens;
|
|
21314
21647
|
const event = {
|
|
21315
21648
|
model: model.id,
|
|
21316
21649
|
provider,
|
|
@@ -21345,7 +21678,7 @@ function createTextSteps(initial, resolve, callerProviderOptions) {
|
|
|
21345
21678
|
return { ...prepared, providerOptions };
|
|
21346
21679
|
};
|
|
21347
21680
|
}
|
|
21348
|
-
function
|
|
21681
|
+
function usage7(results) {
|
|
21349
21682
|
const grouped = /* @__PURE__ */ new Map();
|
|
21350
21683
|
for (const [index, step] of results.entries()) {
|
|
21351
21684
|
const target = steps.get(index) ?? initial;
|
|
@@ -21367,12 +21700,12 @@ function createTextSteps(initial, resolve, callerProviderOptions) {
|
|
|
21367
21700
|
}
|
|
21368
21701
|
return [...grouped.values()];
|
|
21369
21702
|
}
|
|
21370
|
-
return { wrap, usage:
|
|
21703
|
+
return { wrap, usage: usage7 };
|
|
21371
21704
|
}
|
|
21372
21705
|
|
|
21373
21706
|
// src/client.ts
|
|
21374
21707
|
var import_ai5 = require("ai");
|
|
21375
|
-
var
|
|
21708
|
+
var import_zod40 = require("zod");
|
|
21376
21709
|
|
|
21377
21710
|
// src/generated-image.ts
|
|
21378
21711
|
function toGeneratedImage(image) {
|
|
@@ -21494,9 +21827,9 @@ function providerJobMethods(kind, deps) {
|
|
|
21494
21827
|
const { methods, adapter, ctx } = target(sub.model, rest);
|
|
21495
21828
|
if (!methods.result) throw missing3(adapter, ctx.model);
|
|
21496
21829
|
const result = await methods.result(sub, ctx, webhook);
|
|
21497
|
-
const
|
|
21498
|
-
await deps.emit(
|
|
21499
|
-
return { ...result, usage:
|
|
21830
|
+
const usage7 = deps.rowUsage(ctx, result.usage, sub.submittedAt);
|
|
21831
|
+
await deps.emit(usage7);
|
|
21832
|
+
return { ...result, usage: usage7 };
|
|
21500
21833
|
},
|
|
21501
21834
|
async cancel(sub, call = {}) {
|
|
21502
21835
|
const { methods, adapter, ctx } = target(sub.model, call);
|
|
@@ -21736,10 +22069,10 @@ function audioNodeMethods(deps) {
|
|
|
21736
22069
|
const startedAt = Date.now();
|
|
21737
22070
|
const outcome = await adapter.dub(resolved, ctx);
|
|
21738
22071
|
if (outcome.kind === "audio") {
|
|
21739
|
-
const { audio, usage:
|
|
22072
|
+
const { audio, usage: usage7, raw } = outcome.result;
|
|
21740
22073
|
return {
|
|
21741
22074
|
audio,
|
|
21742
|
-
usage: await deps.settle(ctx,
|
|
22075
|
+
usage: await deps.settle(ctx, usage7, startedAt),
|
|
21743
22076
|
...raw !== void 0 && { raw }
|
|
21744
22077
|
};
|
|
21745
22078
|
}
|
|
@@ -21791,10 +22124,10 @@ function textAbortOptions(sdk, context) {
|
|
|
21791
22124
|
if (abortSignal) return { abortSignal };
|
|
21792
22125
|
return {};
|
|
21793
22126
|
}
|
|
21794
|
-
var catalogId =
|
|
21795
|
-
var exactSize =
|
|
21796
|
-
var sdkRatio =
|
|
21797
|
-
var sizeTier =
|
|
22127
|
+
var catalogId = import_zod40.z.string();
|
|
22128
|
+
var exactSize = import_zod40.z.templateLiteral([import_zod40.z.number(), "x", import_zod40.z.number()]);
|
|
22129
|
+
var sdkRatio = import_zod40.z.templateLiteral([import_zod40.z.number(), ":", import_zod40.z.number()]);
|
|
22130
|
+
var sizeTier = import_zod40.z.enum(["512", "1K", "2K", "4K", "8K"]);
|
|
21798
22131
|
function isCatalogId(model) {
|
|
21799
22132
|
return catalogId.safeParse(model).success;
|
|
21800
22133
|
}
|
|
@@ -22037,7 +22370,7 @@ function createModelKit(config, adapters) {
|
|
|
22037
22370
|
const images = result.images.map(
|
|
22038
22371
|
(file) => toGeneratedImage({ bytes: file.uint8Array, mime: file.mediaType })
|
|
22039
22372
|
);
|
|
22040
|
-
const
|
|
22373
|
+
const usage7 = timed(
|
|
22041
22374
|
{
|
|
22042
22375
|
model: model.modelId,
|
|
22043
22376
|
provider: model.provider,
|
|
@@ -22047,8 +22380,8 @@ function createModelKit(config, adapters) {
|
|
|
22047
22380
|
},
|
|
22048
22381
|
startedAt
|
|
22049
22382
|
);
|
|
22050
|
-
await emit(
|
|
22051
|
-
return { image: images[0], images, usage:
|
|
22383
|
+
await emit(usage7);
|
|
22384
|
+
return { image: images[0], images, usage: usage7, raw: result, warnings };
|
|
22052
22385
|
}
|
|
22053
22386
|
function passthroughVideo(req, call) {
|
|
22054
22387
|
const {
|
|
@@ -22124,7 +22457,7 @@ function createModelKit(config, adapters) {
|
|
|
22124
22457
|
let settled;
|
|
22125
22458
|
let failed = false;
|
|
22126
22459
|
const done = pending.then(async (result) => {
|
|
22127
|
-
const
|
|
22460
|
+
const usage7 = timed(
|
|
22128
22461
|
{
|
|
22129
22462
|
model: model.modelId,
|
|
22130
22463
|
provider: model.provider,
|
|
@@ -22137,10 +22470,10 @@ function createModelKit(config, adapters) {
|
|
|
22137
22470
|
);
|
|
22138
22471
|
settled = {
|
|
22139
22472
|
video: { bytes: result.video.uint8Array, mime: result.video.mediaType },
|
|
22140
|
-
usage:
|
|
22473
|
+
usage: usage7,
|
|
22141
22474
|
raw: result
|
|
22142
22475
|
};
|
|
22143
|
-
await emit(
|
|
22476
|
+
await emit(usage7);
|
|
22144
22477
|
return settled;
|
|
22145
22478
|
});
|
|
22146
22479
|
done.catch(() => {
|
|
@@ -22167,9 +22500,9 @@ function createModelKit(config, adapters) {
|
|
|
22167
22500
|
if (settled) return settled;
|
|
22168
22501
|
const pending = result(opts).then(
|
|
22169
22502
|
async (out) => {
|
|
22170
|
-
const
|
|
22171
|
-
await emit(
|
|
22172
|
-
return { ...out, usage:
|
|
22503
|
+
const usage7 = rowUsage(ctx, out.usage, startedAt);
|
|
22504
|
+
await emit(usage7);
|
|
22505
|
+
return { ...out, usage: usage7 };
|
|
22173
22506
|
},
|
|
22174
22507
|
(cause) => {
|
|
22175
22508
|
settled = void 0;
|
|
@@ -22189,9 +22522,9 @@ function createModelKit(config, adapters) {
|
|
|
22189
22522
|
return context(model, signal ? { ...call, signal } : call, warnings);
|
|
22190
22523
|
},
|
|
22191
22524
|
async settle(ctx, event, startedAt) {
|
|
22192
|
-
const
|
|
22193
|
-
await emit(
|
|
22194
|
-
return
|
|
22525
|
+
const usage7 = rowUsage(ctx, event, startedAt);
|
|
22526
|
+
await emit(usage7);
|
|
22527
|
+
return usage7;
|
|
22195
22528
|
},
|
|
22196
22529
|
wrapVideoJob
|
|
22197
22530
|
});
|
|
@@ -22236,8 +22569,8 @@ function createModelKit(config, adapters) {
|
|
|
22236
22569
|
throw billedFailure(ctx, err, err.usage, startedAt);
|
|
22237
22570
|
throw err;
|
|
22238
22571
|
}
|
|
22239
|
-
const
|
|
22240
|
-
await emit(
|
|
22572
|
+
const usage7 = rowUsage(ctx, result.usage, startedAt);
|
|
22573
|
+
await emit(usage7);
|
|
22241
22574
|
const images = result.images.map(toGeneratedImage);
|
|
22242
22575
|
const first = images[0];
|
|
22243
22576
|
if (!first)
|
|
@@ -22246,7 +22579,7 @@ function createModelKit(config, adapters) {
|
|
|
22246
22579
|
message: `${model.id} returned no image`,
|
|
22247
22580
|
model: model.id
|
|
22248
22581
|
});
|
|
22249
|
-
return { ...result, usage:
|
|
22582
|
+
return { ...result, usage: usage7, image: first, images, warnings };
|
|
22250
22583
|
},
|
|
22251
22584
|
async generateVideo(req, call = {}) {
|
|
22252
22585
|
mediaRetryOptions(call);
|
|
@@ -22347,9 +22680,9 @@ function createModelKit(config, adapters) {
|
|
|
22347
22680
|
}
|
|
22348
22681
|
const startedAt = Date.now();
|
|
22349
22682
|
const result = await adapter.generateAudio(resolved, ctx);
|
|
22350
|
-
const
|
|
22351
|
-
await emit(
|
|
22352
|
-
return { ...result, usage:
|
|
22683
|
+
const usage7 = rowUsage(ctx, result.usage, startedAt);
|
|
22684
|
+
await emit(usage7);
|
|
22685
|
+
return { ...result, usage: usage7 };
|
|
22353
22686
|
},
|
|
22354
22687
|
async prepareMediaPart(modelId, ref, mediaType, call = {}) {
|
|
22355
22688
|
mediaRetryOptions(call);
|
|
@@ -22430,8 +22763,8 @@ function createModelKit(config, adapters) {
|
|
|
22430
22763
|
options.prepareStep ?? options.experimental_prepareStep
|
|
22431
22764
|
),
|
|
22432
22765
|
onFinish: async (event) => {
|
|
22433
|
-
for (const
|
|
22434
|
-
await emit(textTimed(withRowUnit(
|
|
22766
|
+
for (const usage7 of steps.usage(event.steps))
|
|
22767
|
+
await emit(textTimed(withRowUnit(usage7), event.steps.length, startedAt));
|
|
22435
22768
|
await callerOnFinish?.(event);
|
|
22436
22769
|
}
|
|
22437
22770
|
});
|
|
@@ -22638,6 +22971,7 @@ var describe = (modelId) => createDefaultModelKit().describe(modelId);
|
|
|
22638
22971
|
videoLengthViolation,
|
|
22639
22972
|
videoSizeViolation,
|
|
22640
22973
|
videoToolConstraints,
|
|
22974
|
+
wavespeedAdapter,
|
|
22641
22975
|
withRetry,
|
|
22642
22976
|
xaiVideoAdapter,
|
|
22643
22977
|
...require("ai")
|