@shop-os/modelkit 0.2.13 → 0.2.15
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 +16 -0
- package/README.md +31 -2
- package/dist/catalog.cjs +124 -13
- package/dist/catalog.cjs.map +1 -1
- package/dist/catalog.d.cts +59 -1
- package/dist/catalog.d.ts +59 -1
- package/dist/catalog.js +124 -13
- package/dist/catalog.js.map +1 -1
- package/dist/index.cjs +497 -176
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +59 -1
- package/dist/index.d.ts +59 -1
- package/dist/index.js +496 -176
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -104,6 +104,7 @@ __export(index_exports, {
|
|
|
104
104
|
textUsageEvent: () => textUsageEvent,
|
|
105
105
|
toGeneratedImage: () => toGeneratedImage,
|
|
106
106
|
validateInputSpec: () => validateInputSpec,
|
|
107
|
+
videoLengthViolation: () => videoLengthViolation,
|
|
107
108
|
withRetry: () => withRetry,
|
|
108
109
|
xaiVideoAdapter: () => xaiVideoAdapter
|
|
109
110
|
});
|
|
@@ -246,7 +247,9 @@ function normalizeExposure(value) {
|
|
|
246
247
|
if (value === "catalog") return ["cowork", "space_builder"];
|
|
247
248
|
if (value === "internal") return [];
|
|
248
249
|
if (Array.isArray(value)) {
|
|
249
|
-
if (value.every(
|
|
250
|
+
if (value.every(
|
|
251
|
+
(entry) => SURFACES.includes(entry)
|
|
252
|
+
)) {
|
|
250
253
|
const present = new Set(value);
|
|
251
254
|
return SURFACES.filter((surface) => present.has(surface));
|
|
252
255
|
}
|
|
@@ -1480,7 +1483,8 @@ var VIDEO_MODELS = [
|
|
|
1480
1483
|
// reference clip keeps its own framing.
|
|
1481
1484
|
aspectRatios: [...OMNI_RATIOS],
|
|
1482
1485
|
durations: [3, 4, 5, 6, 7, 8, 9, 10],
|
|
1483
|
-
references: { images: 6, videos: 1 },
|
|
1486
|
+
references: { images: 6, videos: 1, videoSeconds: { max: 10 } },
|
|
1487
|
+
sourceVideoSeconds: { max: 10 },
|
|
1484
1488
|
supports: []
|
|
1485
1489
|
},
|
|
1486
1490
|
defaults: {},
|
|
@@ -1506,7 +1510,8 @@ var VIDEO_MODELS = [
|
|
|
1506
1510
|
aspectRatios: [...OMNI_RATIOS],
|
|
1507
1511
|
resolutions: ["360p", "720p", "1080p", "4k"],
|
|
1508
1512
|
durations: [3, 4, 5, 6, 7, 8, 9, 10],
|
|
1509
|
-
references: { images: 6, videos: 1 },
|
|
1513
|
+
references: { images: 6, videos: 1, videoSeconds: { max: 10 } },
|
|
1514
|
+
sourceVideoSeconds: { max: 10 },
|
|
1510
1515
|
supports: []
|
|
1511
1516
|
},
|
|
1512
1517
|
defaults: {},
|
|
@@ -1534,7 +1539,14 @@ var VIDEO_MODELS = [
|
|
|
1534
1539
|
resolutions: ["480p", "720p", "1080p", "4k"],
|
|
1535
1540
|
durations: { min: 4, max: 15 },
|
|
1536
1541
|
durationPresets: [4, 6, 8, 10, 12, 15],
|
|
1537
|
-
references: {
|
|
1542
|
+
references: {
|
|
1543
|
+
images: 9,
|
|
1544
|
+
videos: 3,
|
|
1545
|
+
audios: 3,
|
|
1546
|
+
audioOnly: false,
|
|
1547
|
+
videoSeconds: { min: 2, max: 15, total: 15 }
|
|
1548
|
+
},
|
|
1549
|
+
sourceVideoSeconds: { min: 2, max: 15, inReferenceTotal: true },
|
|
1538
1550
|
ratioIgnoredWith: ["firstFrame", "sourceVideo"],
|
|
1539
1551
|
supports: ["generateAudio", "seed", "lastFrame", "watermark"]
|
|
1540
1552
|
},
|
|
@@ -1566,7 +1578,14 @@ var VIDEO_MODELS = [
|
|
|
1566
1578
|
aspectRatios: [...SEEDANCE_RATIOS],
|
|
1567
1579
|
resolutions: ["480p", "720p"],
|
|
1568
1580
|
durations: { min: 4, max: 15 },
|
|
1569
|
-
references: {
|
|
1581
|
+
references: {
|
|
1582
|
+
images: 9,
|
|
1583
|
+
videos: 3,
|
|
1584
|
+
audios: 3,
|
|
1585
|
+
audioOnly: false,
|
|
1586
|
+
videoSeconds: { min: 2, max: 15, total: 15 }
|
|
1587
|
+
},
|
|
1588
|
+
sourceVideoSeconds: { min: 2, max: 15, inReferenceTotal: true },
|
|
1570
1589
|
ratioIgnoredWith: ["firstFrame", "sourceVideo"],
|
|
1571
1590
|
supports: ["generateAudio", "seed", "lastFrame", "watermark"]
|
|
1572
1591
|
},
|
|
@@ -1600,7 +1619,13 @@ var VIDEO_MODELS = [
|
|
|
1600
1619
|
aspectRatios: [...SEEDANCE_RATIOS],
|
|
1601
1620
|
resolutions: ["480p", "720p", "1080p"],
|
|
1602
1621
|
durations: { min: 4, max: 30 },
|
|
1603
|
-
references: {
|
|
1622
|
+
references: {
|
|
1623
|
+
images: 30,
|
|
1624
|
+
videos: 10,
|
|
1625
|
+
audios: 10,
|
|
1626
|
+
videoSeconds: { min: 2, max: 30, total: 30, editMin: 4 }
|
|
1627
|
+
},
|
|
1628
|
+
sourceVideoSeconds: { min: 2, max: 30, editMin: 4, inReferenceTotal: true },
|
|
1604
1629
|
ratioIgnoredWith: ["firstFrame", "sourceVideo"],
|
|
1605
1630
|
supports: ["generateAudio", "seed", "lastFrame", "watermark", "taskType"]
|
|
1606
1631
|
},
|
|
@@ -1812,7 +1837,14 @@ var VIDEO_MODELS = [
|
|
|
1812
1837
|
aspectRatios: [...H3_REFERENCE_RATIOS],
|
|
1813
1838
|
resolutions: ["480p", "768p", "2k", "4k"],
|
|
1814
1839
|
durations: { min: 5, max: 15 },
|
|
1815
|
-
references: {
|
|
1840
|
+
references: {
|
|
1841
|
+
images: 9,
|
|
1842
|
+
videos: 3,
|
|
1843
|
+
audios: 3,
|
|
1844
|
+
total: 12,
|
|
1845
|
+
audioOnly: false,
|
|
1846
|
+
videoSeconds: { min: 2, max: 15, total: 15 }
|
|
1847
|
+
},
|
|
1816
1848
|
supports: ["seed", "promptExpansion", "safetyChecker"]
|
|
1817
1849
|
},
|
|
1818
1850
|
defaults: {
|
|
@@ -1843,7 +1875,14 @@ var VIDEO_MODELS = [
|
|
|
1843
1875
|
aspectRatios: [...H3_REFERENCE_RATIOS],
|
|
1844
1876
|
resolutions: ["480p", "768p"],
|
|
1845
1877
|
durations: { min: 5, max: 15 },
|
|
1846
|
-
references: {
|
|
1878
|
+
references: {
|
|
1879
|
+
images: 9,
|
|
1880
|
+
videos: 3,
|
|
1881
|
+
audios: 3,
|
|
1882
|
+
total: 12,
|
|
1883
|
+
audioOnly: false,
|
|
1884
|
+
videoSeconds: { min: 2, max: 15, total: 15 }
|
|
1885
|
+
},
|
|
1847
1886
|
supports: ["seed", "promptExpansion", "safetyChecker"]
|
|
1848
1887
|
},
|
|
1849
1888
|
defaults: {
|
|
@@ -1875,6 +1914,7 @@ var VIDEO_MODELS = [
|
|
|
1875
1914
|
aspectRatios: [...KLING_RATIOS],
|
|
1876
1915
|
durations: { min: 3, max: 10 },
|
|
1877
1916
|
requires: ["sourceVideo"],
|
|
1917
|
+
sourceVideoSeconds: { min: 3, max: 10.05 },
|
|
1878
1918
|
supports: []
|
|
1879
1919
|
},
|
|
1880
1920
|
defaults: { durationSeconds: 5, aspectRatio: "16:9" },
|
|
@@ -2234,13 +2274,20 @@ var ProviderOptionValueSchema = import_zod2.z.lazy(
|
|
|
2234
2274
|
])
|
|
2235
2275
|
);
|
|
2236
2276
|
var ProviderOptionsSchema = import_zod2.z.record(import_zod2.z.string(), import_zod2.z.record(import_zod2.z.string(), ProviderOptionValueSchema));
|
|
2277
|
+
var VideoSecondsLimitsSchema = import_zod2.z.looseObject({
|
|
2278
|
+
min: import_zod2.z.number().optional(),
|
|
2279
|
+
max: import_zod2.z.number().optional(),
|
|
2280
|
+
total: import_zod2.z.number().optional(),
|
|
2281
|
+
editMin: import_zod2.z.number().optional()
|
|
2282
|
+
});
|
|
2237
2283
|
var ReferenceLimitsSchema = import_zod2.z.looseObject({
|
|
2238
2284
|
images: import_zod2.z.number().optional(),
|
|
2239
2285
|
imagesWithVideo: import_zod2.z.number().optional(),
|
|
2240
2286
|
videos: import_zod2.z.number().optional(),
|
|
2241
2287
|
audios: import_zod2.z.number().optional(),
|
|
2242
2288
|
total: import_zod2.z.number().optional(),
|
|
2243
|
-
audioOnly: import_zod2.z.boolean().optional()
|
|
2289
|
+
audioOnly: import_zod2.z.boolean().optional(),
|
|
2290
|
+
videoSeconds: VideoSecondsLimitsSchema.optional()
|
|
2244
2291
|
});
|
|
2245
2292
|
var ModelConstraintsSchema = import_zod2.z.looseObject({
|
|
2246
2293
|
aspectRatios: import_zod2.z.array(AspectRatioSchema).optional(),
|
|
@@ -2253,14 +2300,20 @@ var ModelConstraintsSchema = import_zod2.z.looseObject({
|
|
|
2253
2300
|
references: ReferenceLimitsSchema.optional(),
|
|
2254
2301
|
requires: import_zod2.z.array(RequiredInputSchema).optional(),
|
|
2255
2302
|
supports: import_zod2.z.array(SupportedOptionSchema).optional(),
|
|
2256
|
-
ratioIgnoredWith: import_zod2.z.array(import_zod2.z.enum(["firstFrame", "sourceVideo"])).optional()
|
|
2303
|
+
ratioIgnoredWith: import_zod2.z.array(import_zod2.z.enum(["firstFrame", "sourceVideo"])).optional(),
|
|
2304
|
+
sourceVideoSeconds: VideoSecondsLimitsSchema.omit({ total: true }).extend({ inReferenceTotal: import_zod2.z.boolean().optional() }).optional()
|
|
2257
2305
|
});
|
|
2258
2306
|
var MediaRefSchema = import_zod2.z.union([
|
|
2259
|
-
import_zod2.z.looseObject({
|
|
2307
|
+
import_zod2.z.looseObject({
|
|
2308
|
+
url: import_zod2.z.string(),
|
|
2309
|
+
label: import_zod2.z.string().optional(),
|
|
2310
|
+
durationSeconds: import_zod2.z.number().optional()
|
|
2311
|
+
}),
|
|
2260
2312
|
import_zod2.z.looseObject({
|
|
2261
2313
|
bytes: import_zod2.z.instanceof(Uint8Array),
|
|
2262
2314
|
mime: import_zod2.z.string(),
|
|
2263
|
-
label: import_zod2.z.string().optional()
|
|
2315
|
+
label: import_zod2.z.string().optional(),
|
|
2316
|
+
durationSeconds: import_zod2.z.number().optional()
|
|
2264
2317
|
})
|
|
2265
2318
|
]);
|
|
2266
2319
|
var ImageSafetySettingSchema = import_zod2.z.looseObject({ category: import_zod2.z.string(), threshold: import_zod2.z.string() });
|
|
@@ -2787,6 +2840,19 @@ function shouldRetry(err) {
|
|
|
2787
2840
|
if (/ECONNRESET|ETIMEDOUT|fetch failed|socket hang up/i.test(err.message)) return true;
|
|
2788
2841
|
return false;
|
|
2789
2842
|
}
|
|
2843
|
+
function mediaRetryOptions(call, signal, defaultAttempts = 3) {
|
|
2844
|
+
if (call.maxRetries !== void 0 && (!Number.isSafeInteger(call.maxRetries + 1) || call.maxRetries < 0)) {
|
|
2845
|
+
throw new ModelKitError({
|
|
2846
|
+
code: "unsupported_option",
|
|
2847
|
+
message: "maxRetries must be a non-negative safe integer",
|
|
2848
|
+
retryable: false
|
|
2849
|
+
});
|
|
2850
|
+
}
|
|
2851
|
+
return {
|
|
2852
|
+
attempts: call.maxRetries === void 0 ? defaultAttempts : call.maxRetries + 1,
|
|
2853
|
+
...signal !== void 0 ? { signal } : {}
|
|
2854
|
+
};
|
|
2855
|
+
}
|
|
2790
2856
|
async function withRetry(fn, opts = {}) {
|
|
2791
2857
|
const attempts = opts.attempts ?? 3;
|
|
2792
2858
|
const base = opts.baseDelayMs ?? 1e3;
|
|
@@ -2794,6 +2860,7 @@ async function withRetry(fn, opts = {}) {
|
|
|
2794
2860
|
const factor = opts.factor ?? 2;
|
|
2795
2861
|
let last;
|
|
2796
2862
|
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
2863
|
+
if (opts.signal?.aborted) throw throwAborted();
|
|
2797
2864
|
try {
|
|
2798
2865
|
return await fn(attempt);
|
|
2799
2866
|
} catch (cause) {
|
|
@@ -2906,11 +2973,11 @@ async function readBody(res) {
|
|
|
2906
2973
|
return text2;
|
|
2907
2974
|
}
|
|
2908
2975
|
}
|
|
2909
|
-
async function mediaRefToBytes(ref,
|
|
2976
|
+
async function mediaRefToBytes(ref, fetch2, options = {}) {
|
|
2910
2977
|
if (!isUrlRef(ref)) return { bytes: ref.bytes, mime: ref.mime };
|
|
2911
2978
|
const init = {};
|
|
2912
2979
|
if (options.signal !== void 0) init.signal = options.signal;
|
|
2913
|
-
const res = await
|
|
2980
|
+
const res = await fetch2(ref.url, init);
|
|
2914
2981
|
if (!res.ok) {
|
|
2915
2982
|
const text2 = await res.text().catch(() => "");
|
|
2916
2983
|
throw classifyHttpError(
|
|
@@ -3047,11 +3114,6 @@ var SeedreamResponseSchema = import_zod6.z.object({
|
|
|
3047
3114
|
data: import_zod6.z.array(SeedreamImageSchema).optional(),
|
|
3048
3115
|
error: import_zod6.z.object({ message: import_zod6.z.string().optional(), code: import_zod6.z.string().optional() }).optional()
|
|
3049
3116
|
});
|
|
3050
|
-
function retryOptsFor(ctx, attempts = 3) {
|
|
3051
|
-
const opts = { attempts };
|
|
3052
|
-
if (ctx.signal !== void 0) opts.signal = ctx.signal;
|
|
3053
|
-
return opts;
|
|
3054
|
-
}
|
|
3055
3117
|
function fetchInitFor(ctx, init) {
|
|
3056
3118
|
if (ctx.signal !== void 0) init.signal = ctx.signal;
|
|
3057
3119
|
return init;
|
|
@@ -3089,20 +3151,23 @@ async function generateSeedreamImage(req, ctx) {
|
|
|
3089
3151
|
const firstRef = refs[0];
|
|
3090
3152
|
if (refs.length === 1 && firstRef !== void 0) body.image = firstRef;
|
|
3091
3153
|
else if (refs.length > 1) body.image = refs;
|
|
3092
|
-
const data = await withRetry(
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3154
|
+
const data = await withRetry(
|
|
3155
|
+
async () => {
|
|
3156
|
+
const res = await arkFetch(ctx, `${arkBaseUrl(ctx)}/images/generations`, {
|
|
3157
|
+
method: "POST",
|
|
3158
|
+
headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" },
|
|
3159
|
+
body: JSON.stringify(body)
|
|
3160
|
+
});
|
|
3161
|
+
if (!res.ok) {
|
|
3162
|
+
const text2 = await res.text().catch(() => "");
|
|
3163
|
+
throwArkError(res.status, text2, modelId);
|
|
3164
|
+
}
|
|
3165
|
+
const parsed = SeedreamResponseSchema.safeParse(await res.json());
|
|
3166
|
+
const decoded = parsed.success ? parsed.data : {};
|
|
3167
|
+
return decoded;
|
|
3168
|
+
},
|
|
3169
|
+
mediaRetryOptions(ctx.call, ctx.signal)
|
|
3170
|
+
);
|
|
3106
3171
|
const first = data.data?.[0];
|
|
3107
3172
|
let bytes;
|
|
3108
3173
|
if (first?.b64_json) {
|
|
@@ -3238,20 +3303,23 @@ async function generateSeedanceVideo(req, ctx) {
|
|
|
3238
3303
|
if (req.generateAudio !== void 0) body.generate_audio = req.generateAudio;
|
|
3239
3304
|
if (req.seed !== void 0) body.seed = req.seed;
|
|
3240
3305
|
if (req.watermark !== void 0) body.watermark = req.watermark;
|
|
3241
|
-
const submit = await withRetry(
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3306
|
+
const submit = await withRetry(
|
|
3307
|
+
async () => {
|
|
3308
|
+
const res = await arkFetch(ctx, `${arkBaseUrl(ctx)}${SUBMIT_PATH}`, {
|
|
3309
|
+
method: "POST",
|
|
3310
|
+
headers: { Authorization: `Bearer ${key}`, "Content-Type": "application/json" },
|
|
3311
|
+
body: JSON.stringify(body)
|
|
3312
|
+
});
|
|
3313
|
+
if (!res.ok) {
|
|
3314
|
+
const text2 = await res.text().catch(() => "");
|
|
3315
|
+
throwArkError(res.status, text2, modelId);
|
|
3316
|
+
}
|
|
3317
|
+
const parsed = SeedanceResponseSchema.safeParse(await res.json());
|
|
3318
|
+
const decoded = parsed.success ? parsed.data : {};
|
|
3319
|
+
return decoded;
|
|
3320
|
+
},
|
|
3321
|
+
mediaRetryOptions(ctx.call, ctx.signal)
|
|
3322
|
+
);
|
|
3255
3323
|
if (!submit.id) throwArkError(void 0, "Seedance task create returned no id", modelId);
|
|
3256
3324
|
const jobId = submit.id;
|
|
3257
3325
|
const usage2 = seedanceUsage(ctx, req, taskType === "edit" ? requestedDuration : duration);
|
|
@@ -3260,7 +3328,8 @@ async function generateSeedanceVideo(req, ctx) {
|
|
|
3260
3328
|
headers: { Authorization: `Bearer ${key}` }
|
|
3261
3329
|
});
|
|
3262
3330
|
if (!res.ok) {
|
|
3263
|
-
if (res.status === 429 || res.status >= 500)
|
|
3331
|
+
if (ctx.call.maxRetries !== 0 && (res.status === 429 || res.status >= 500))
|
|
3332
|
+
return void 0;
|
|
3264
3333
|
const text2 = await res.text().catch(() => "");
|
|
3265
3334
|
throwArkError(res.status, text2, modelId);
|
|
3266
3335
|
}
|
|
@@ -3326,16 +3395,109 @@ async function generateSeedanceVideo(req, ctx) {
|
|
|
3326
3395
|
}
|
|
3327
3396
|
|
|
3328
3397
|
// src/adapters/fal.ts
|
|
3329
|
-
var
|
|
3398
|
+
var import_zod8 = require("zod");
|
|
3330
3399
|
var import_client = require("@fal-ai/client");
|
|
3400
|
+
|
|
3401
|
+
// src/adapters/fal-http.ts
|
|
3402
|
+
var import_zod7 = require("zod");
|
|
3403
|
+
var queueUrl = import_zod7.z.url().refine((value) => new URL(value).origin === "https://queue.fal.run");
|
|
3404
|
+
var submissionSchema = import_zod7.z.object({
|
|
3405
|
+
request_id: import_zod7.z.string(),
|
|
3406
|
+
status_url: queueUrl,
|
|
3407
|
+
response_url: queueUrl
|
|
3408
|
+
});
|
|
3409
|
+
var statusSchema = import_zod7.z.object({ status: import_zod7.z.string() });
|
|
3410
|
+
function createFalHttpClient(opts) {
|
|
3411
|
+
const jobs = /* @__PURE__ */ new Map();
|
|
3412
|
+
async function request(url, input, signal) {
|
|
3413
|
+
let response;
|
|
3414
|
+
try {
|
|
3415
|
+
response = await opts.fetch(url, {
|
|
3416
|
+
method: input === void 0 ? "GET" : "POST",
|
|
3417
|
+
headers: {
|
|
3418
|
+
Authorization: `Key ${opts.credentials}`,
|
|
3419
|
+
"Content-Type": "application/json"
|
|
3420
|
+
},
|
|
3421
|
+
...input !== void 0 ? { body: JSON.stringify(input) } : {},
|
|
3422
|
+
...signal !== void 0 ? { signal } : {}
|
|
3423
|
+
});
|
|
3424
|
+
} catch (cause) {
|
|
3425
|
+
throw classifyThrown("fal", cause);
|
|
3426
|
+
}
|
|
3427
|
+
if (!response.ok) throw classifyHttpError(response.status, await response.text(), "fal");
|
|
3428
|
+
return JsonValueSchema.parse(await response.json());
|
|
3429
|
+
}
|
|
3430
|
+
function job(id) {
|
|
3431
|
+
const saved = jobs.get(id);
|
|
3432
|
+
if (!saved)
|
|
3433
|
+
throw new ModelKitError({
|
|
3434
|
+
code: "provider_rejected",
|
|
3435
|
+
provider: "fal",
|
|
3436
|
+
message: "Unknown fal queue request"
|
|
3437
|
+
});
|
|
3438
|
+
return saved;
|
|
3439
|
+
}
|
|
3440
|
+
const queue = {
|
|
3441
|
+
async submit(endpoint, options) {
|
|
3442
|
+
const submitted = submissionSchema.parse(
|
|
3443
|
+
await request(
|
|
3444
|
+
`https://queue.fal.run/${endpoint}`,
|
|
3445
|
+
options.input,
|
|
3446
|
+
options.abortSignal
|
|
3447
|
+
)
|
|
3448
|
+
);
|
|
3449
|
+
jobs.set(submitted.request_id, submitted);
|
|
3450
|
+
return { requestId: submitted.request_id };
|
|
3451
|
+
},
|
|
3452
|
+
async status(_endpoint, options) {
|
|
3453
|
+
return statusSchema.parse(
|
|
3454
|
+
await request(job(options.requestId).status_url, void 0, options.abortSignal)
|
|
3455
|
+
);
|
|
3456
|
+
},
|
|
3457
|
+
result: (_endpoint, options) => request(job(options.requestId).response_url, void 0, options.abortSignal)
|
|
3458
|
+
};
|
|
3459
|
+
return {
|
|
3460
|
+
queue,
|
|
3461
|
+
async subscribe(endpoint, options) {
|
|
3462
|
+
const submitted = await withRetry(
|
|
3463
|
+
() => queue.submit(endpoint, options),
|
|
3464
|
+
mediaRetryOptions(opts, options.abortSignal)
|
|
3465
|
+
);
|
|
3466
|
+
return pollUntil(
|
|
3467
|
+
async () => {
|
|
3468
|
+
const readOptions = {
|
|
3469
|
+
requestId: submitted.requestId,
|
|
3470
|
+
...options.abortSignal !== void 0 ? { abortSignal: options.abortSignal } : {}
|
|
3471
|
+
};
|
|
3472
|
+
const current = await queue.status(endpoint, readOptions);
|
|
3473
|
+
if (current.status !== "COMPLETED") return void 0;
|
|
3474
|
+
return queue.result(endpoint, readOptions);
|
|
3475
|
+
},
|
|
3476
|
+
{
|
|
3477
|
+
intervalMs: 1e3,
|
|
3478
|
+
timeoutMs: 18e4,
|
|
3479
|
+
...options.abortSignal !== void 0 ? { signal: options.abortSignal } : {}
|
|
3480
|
+
}
|
|
3481
|
+
);
|
|
3482
|
+
}
|
|
3483
|
+
};
|
|
3484
|
+
}
|
|
3485
|
+
|
|
3486
|
+
// src/adapters/fal.ts
|
|
3331
3487
|
var MUSE_T2I_ENDPOINT = "meta/muse-image/text-to-image";
|
|
3332
3488
|
var MUSE_EDIT_ENDPOINT = "meta/muse-image/edit";
|
|
3333
3489
|
var KLING_O1_ENDPOINT = "fal-ai/kling-video/o1/video-to-video/reference";
|
|
3334
3490
|
var FAL_QUEUE_HOST = "https://queue.fal.run";
|
|
3335
3491
|
var PASSTHROUGH_POLL_MS = 2e3;
|
|
3336
3492
|
var PASSTHROUGH_MAX_WAIT_MS = 18e4;
|
|
3337
|
-
var falSubscribePayloadSchema =
|
|
3493
|
+
var falSubscribePayloadSchema = import_zod8.z.object({ data: JsonValueSchema });
|
|
3338
3494
|
function defaultCreateFalClient(opts) {
|
|
3495
|
+
if (opts.maxRetries !== void 0)
|
|
3496
|
+
return createFalHttpClient({
|
|
3497
|
+
credentials: opts.credentials,
|
|
3498
|
+
fetch: opts.fetch ?? fetch,
|
|
3499
|
+
maxRetries: opts.maxRetries
|
|
3500
|
+
});
|
|
3339
3501
|
const client2 = (0, import_client.createFalClient)(opts);
|
|
3340
3502
|
return {
|
|
3341
3503
|
async subscribe(endpoint, options) {
|
|
@@ -3384,7 +3546,7 @@ function createFalAdapter(deps = defaultDeps) {
|
|
|
3384
3546
|
};
|
|
3385
3547
|
}
|
|
3386
3548
|
var falAdapter = createFalAdapter();
|
|
3387
|
-
var falDetailSchema =
|
|
3549
|
+
var falDetailSchema = import_zod8.z.object({ detail: JsonValueSchema });
|
|
3388
3550
|
function liftDetail(body) {
|
|
3389
3551
|
const parsed = falDetailSchema.safeParse(body);
|
|
3390
3552
|
const detail = parsed.success ? parsed.data.detail : void 0;
|
|
@@ -3393,11 +3555,11 @@ function liftDetail(body) {
|
|
|
3393
3555
|
function falError(status, body, model) {
|
|
3394
3556
|
return classifyHttpError(status, liftDetail(body), "fal", model);
|
|
3395
3557
|
}
|
|
3396
|
-
var falSdkThrownSchema =
|
|
3397
|
-
status:
|
|
3398
|
-
statusCode:
|
|
3558
|
+
var falSdkThrownSchema = import_zod8.z.object({
|
|
3559
|
+
status: import_zod8.z.number().optional(),
|
|
3560
|
+
statusCode: import_zod8.z.number().optional(),
|
|
3399
3561
|
body: JsonValueSchema.optional(),
|
|
3400
|
-
message:
|
|
3562
|
+
message: import_zod8.z.string().optional()
|
|
3401
3563
|
});
|
|
3402
3564
|
function falSdkError(cause, model) {
|
|
3403
3565
|
const parsed = falSdkThrownSchema.safeParse(cause);
|
|
@@ -3430,6 +3592,8 @@ function gatewayConfig(ctx) {
|
|
|
3430
3592
|
};
|
|
3431
3593
|
if (falKey) config.falKey = falKey;
|
|
3432
3594
|
if (cfToken) config.cfToken = cfToken;
|
|
3595
|
+
if (ctx.call.maxRetries !== void 0) config.singleAttempt = true;
|
|
3596
|
+
if (ctx.signal !== void 0) config.signal = ctx.signal;
|
|
3433
3597
|
return config;
|
|
3434
3598
|
}
|
|
3435
3599
|
async function gatewayFetch(cfg, fetchImpl, targetUrl, init) {
|
|
@@ -3437,16 +3601,18 @@ async function gatewayFetch(cfg, fetchImpl, targetUrl, init) {
|
|
|
3437
3601
|
if (cfg.falKey) headers2.Authorization = `Bearer ${cfg.falKey}`;
|
|
3438
3602
|
if (init.body) headers2["Content-Type"] = "application/json";
|
|
3439
3603
|
if (cfg.cfToken) headers2["cf-aig-authorization"] = `Bearer ${cfg.cfToken}`;
|
|
3604
|
+
if (cfg.singleAttempt) headers2["cf-aig-max-attempts"] = "1";
|
|
3440
3605
|
const request = { method: init.method, headers: headers2 };
|
|
3606
|
+
if (cfg.signal !== void 0) request.signal = cfg.signal;
|
|
3441
3607
|
if (init.body !== void 0) request.body = init.body;
|
|
3442
3608
|
return fetchImpl(cfg.baseUrl, request);
|
|
3443
3609
|
}
|
|
3444
|
-
var falQueueSubmitSchema =
|
|
3445
|
-
request_id:
|
|
3446
|
-
status_url:
|
|
3447
|
-
response_url:
|
|
3610
|
+
var falQueueSubmitSchema = import_zod8.z.object({
|
|
3611
|
+
request_id: import_zod8.z.string(),
|
|
3612
|
+
status_url: import_zod8.z.string(),
|
|
3613
|
+
response_url: import_zod8.z.string()
|
|
3448
3614
|
});
|
|
3449
|
-
var falQueueStatusSchema =
|
|
3615
|
+
var falQueueStatusSchema = import_zod8.z.object({ status: import_zod8.z.string() });
|
|
3450
3616
|
async function passthroughRequest(ctx, endpoint, input) {
|
|
3451
3617
|
const cfg = gatewayConfig(ctx);
|
|
3452
3618
|
const submitRes = await gatewayFetch(cfg, ctx.fetch, `${FAL_QUEUE_HOST}/${endpoint}`, {
|
|
@@ -3503,7 +3669,11 @@ function falClientOf(ctx, deps) {
|
|
|
3503
3669
|
retryable: false
|
|
3504
3670
|
});
|
|
3505
3671
|
}
|
|
3506
|
-
return deps.createFalClient({
|
|
3672
|
+
return deps.createFalClient({
|
|
3673
|
+
credentials: falKey,
|
|
3674
|
+
fetch: ctx.fetch,
|
|
3675
|
+
...ctx.call.maxRetries !== void 0 ? { maxRetries: ctx.call.maxRetries } : {}
|
|
3676
|
+
});
|
|
3507
3677
|
}
|
|
3508
3678
|
async function subscribe(ctx, deps, endpoint, input) {
|
|
3509
3679
|
const client2 = falClientOf(ctx, deps);
|
|
@@ -3520,7 +3690,10 @@ async function subscribe(ctx, deps, endpoint, input) {
|
|
|
3520
3690
|
throw falSdkError(cause, ctx.model.id);
|
|
3521
3691
|
}
|
|
3522
3692
|
},
|
|
3523
|
-
|
|
3693
|
+
mediaRetryOptions(
|
|
3694
|
+
ctx.call.maxRetries === void 0 ? {} : { maxRetries: 0 },
|
|
3695
|
+
ctx.signal
|
|
3696
|
+
)
|
|
3524
3697
|
);
|
|
3525
3698
|
} catch (cause) {
|
|
3526
3699
|
throw falSdkError(cause, ctx.model.id);
|
|
@@ -3572,11 +3745,11 @@ async function decodeImage(ctx, url, contentType) {
|
|
|
3572
3745
|
const mime = res.headers.get("content-type") ?? contentType ?? "image/png";
|
|
3573
3746
|
return { bytes: new Uint8Array(await res.arrayBuffer()), mime };
|
|
3574
3747
|
}
|
|
3575
|
-
var museImageResultSchema =
|
|
3576
|
-
images:
|
|
3577
|
-
|
|
3578
|
-
url:
|
|
3579
|
-
content_type:
|
|
3748
|
+
var museImageResultSchema = import_zod8.z.object({
|
|
3749
|
+
images: import_zod8.z.array(
|
|
3750
|
+
import_zod8.z.object({
|
|
3751
|
+
url: import_zod8.z.string().optional(),
|
|
3752
|
+
content_type: import_zod8.z.string().nullable().optional()
|
|
3580
3753
|
})
|
|
3581
3754
|
).optional()
|
|
3582
3755
|
});
|
|
@@ -3680,8 +3853,8 @@ function klingO1Input(req) {
|
|
|
3680
3853
|
if (refs.length > 0) input.image_urls = refs;
|
|
3681
3854
|
return input;
|
|
3682
3855
|
}
|
|
3683
|
-
var falVideoResultSchema =
|
|
3684
|
-
video:
|
|
3856
|
+
var falVideoResultSchema = import_zod8.z.object({
|
|
3857
|
+
video: import_zod8.z.object({ url: import_zod8.z.string().optional() }).optional()
|
|
3685
3858
|
});
|
|
3686
3859
|
function extractVideoUrl(result, endpoint, model) {
|
|
3687
3860
|
const parsed = falVideoResultSchema.safeParse(result);
|
|
@@ -3761,7 +3934,7 @@ async function submitQueueJob(ctx, req, deps, endpoint, input) {
|
|
|
3761
3934
|
throw falSdkError(cause, ctx.model.id);
|
|
3762
3935
|
}
|
|
3763
3936
|
},
|
|
3764
|
-
ctx.
|
|
3937
|
+
mediaRetryOptions(ctx.call, ctx.signal)
|
|
3765
3938
|
);
|
|
3766
3939
|
} catch (cause) {
|
|
3767
3940
|
throw falSdkError(cause, ctx.model.id);
|
|
@@ -3790,6 +3963,7 @@ function queuedVideoJob(ctx, req, client2, endpoint, requestId) {
|
|
|
3790
3963
|
if (status === "IN_QUEUE") return { status: "pending" };
|
|
3791
3964
|
return { status: "running" };
|
|
3792
3965
|
} catch (cause) {
|
|
3966
|
+
if (ctx.call.maxRetries === 0) throw cause;
|
|
3793
3967
|
if (isModelKitError(cause) && !cause.retryable)
|
|
3794
3968
|
return { status: "failed" };
|
|
3795
3969
|
return { status: "pending" };
|
|
@@ -3813,7 +3987,7 @@ function queuedVideoJob(ctx, req, client2, endpoint, requestId) {
|
|
|
3813
3987
|
return await client2.queue.result(endpoint, queueOptions);
|
|
3814
3988
|
} catch (cause) {
|
|
3815
3989
|
const err = falSdkError(cause, ctx.model.id);
|
|
3816
|
-
if (err.retryable) return void 0;
|
|
3990
|
+
if (ctx.call.maxRetries !== 0 && err.retryable) return void 0;
|
|
3817
3991
|
throw err;
|
|
3818
3992
|
}
|
|
3819
3993
|
}, pollOptions);
|
|
@@ -3861,6 +4035,7 @@ async function passthroughVideoJob(ctx, req, endpoint, input) {
|
|
|
3861
4035
|
const url = await checkStatus();
|
|
3862
4036
|
return url ? { status: "succeeded" } : { status: "pending" };
|
|
3863
4037
|
} catch (cause) {
|
|
4038
|
+
if (ctx.call.maxRetries === 0) throw cause;
|
|
3864
4039
|
if (isModelKitError(cause) && !cause.retryable)
|
|
3865
4040
|
return { status: "failed" };
|
|
3866
4041
|
return { status: "pending" };
|
|
@@ -3880,11 +4055,11 @@ async function passthroughVideoJob(ctx, req, endpoint, input) {
|
|
|
3880
4055
|
}
|
|
3881
4056
|
|
|
3882
4057
|
// src/adapters/google-genai.ts
|
|
3883
|
-
var
|
|
4058
|
+
var import_zod10 = require("zod");
|
|
3884
4059
|
var import_genai = require("@google/genai");
|
|
3885
4060
|
|
|
3886
4061
|
// src/adapters/google-files.ts
|
|
3887
|
-
var
|
|
4062
|
+
var import_zod9 = require("zod");
|
|
3888
4063
|
var GOOGLE_FILES_BASE = "https://generativelanguage.googleapis.com";
|
|
3889
4064
|
var GOOGLE_FILES_POLL_INTERVAL_MS = 2e3;
|
|
3890
4065
|
var GOOGLE_FILES_POLL_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
@@ -3894,17 +4069,17 @@ var GENERIC_MEDIA_TYPES = /* @__PURE__ */ new Set([
|
|
|
3894
4069
|
"binary/octet-stream",
|
|
3895
4070
|
"application/binary"
|
|
3896
4071
|
]);
|
|
3897
|
-
var uploadedFileSchema =
|
|
3898
|
-
file:
|
|
3899
|
-
uri:
|
|
3900
|
-
name:
|
|
3901
|
-
state:
|
|
3902
|
-
mimeType:
|
|
4072
|
+
var uploadedFileSchema = import_zod9.z.object({
|
|
4073
|
+
file: import_zod9.z.object({
|
|
4074
|
+
uri: import_zod9.z.string().optional(),
|
|
4075
|
+
name: import_zod9.z.string().optional(),
|
|
4076
|
+
state: import_zod9.z.string().optional(),
|
|
4077
|
+
mimeType: import_zod9.z.string().optional()
|
|
3903
4078
|
}).optional()
|
|
3904
4079
|
});
|
|
3905
|
-
var fileStateSchema =
|
|
3906
|
-
state:
|
|
3907
|
-
error:
|
|
4080
|
+
var fileStateSchema = import_zod9.z.object({
|
|
4081
|
+
state: import_zod9.z.string().optional(),
|
|
4082
|
+
error: import_zod9.z.object({ message: import_zod9.z.string().optional() }).optional()
|
|
3908
4083
|
});
|
|
3909
4084
|
function resolveMime(declared, fallback) {
|
|
3910
4085
|
const clean = declared?.split(";")[0]?.trim().toLowerCase();
|
|
@@ -4038,7 +4213,7 @@ async function waitForActive(fileName, opts) {
|
|
|
4038
4213
|
}
|
|
4039
4214
|
return void 0;
|
|
4040
4215
|
}
|
|
4041
|
-
if (res.status === 429 || res.status >= 500) return void 0;
|
|
4216
|
+
if (opts.maxRetries !== 0 && (res.status === 429 || res.status >= 500)) return void 0;
|
|
4042
4217
|
const text2 = await res.text().catch(() => "");
|
|
4043
4218
|
throw classifyHttpError(res.status, text2, "google-genai", opts.model);
|
|
4044
4219
|
}, pollOptions);
|
|
@@ -4062,8 +4237,7 @@ async function uploadGoogleFile(source, opts) {
|
|
|
4062
4237
|
opts.model
|
|
4063
4238
|
);
|
|
4064
4239
|
if (bytes.length > GOOGLE_FILES_MAX_BYTES) throw oversizeError(bytes.length, label, opts.model);
|
|
4065
|
-
const retryOptions =
|
|
4066
|
-
if (opts.signal !== void 0) retryOptions.signal = opts.signal;
|
|
4240
|
+
const retryOptions = mediaRetryOptions(opts, opts.signal);
|
|
4067
4241
|
const uploaded = await withRetry(() => startAndUpload(bytes, mimeType, opts), retryOptions);
|
|
4068
4242
|
if (uploaded.state !== "ACTIVE") {
|
|
4069
4243
|
try {
|
|
@@ -4078,7 +4252,12 @@ async function uploadGoogleFile(source, opts) {
|
|
|
4078
4252
|
|
|
4079
4253
|
// src/adapters/google-genai.ts
|
|
4080
4254
|
function defaultCreateGenAIClient(opts) {
|
|
4081
|
-
const sdk = new import_genai.GoogleGenAI({
|
|
4255
|
+
const sdk = new import_genai.GoogleGenAI({
|
|
4256
|
+
apiKey: opts.apiKey,
|
|
4257
|
+
vertexai: false,
|
|
4258
|
+
...opts.maxRetries !== void 0 ? { httpOptions: { retryOptions: { attempts: 1 } } } : {}
|
|
4259
|
+
});
|
|
4260
|
+
const interactionOptions = opts.maxRetries === void 0 ? void 0 : { maxRetries: 0, ...opts.signal !== void 0 ? { signal: opts.signal } : {} };
|
|
4082
4261
|
return {
|
|
4083
4262
|
models: {
|
|
4084
4263
|
generateVideos: async (body) => {
|
|
@@ -4099,11 +4278,11 @@ function defaultCreateGenAIClient(opts) {
|
|
|
4099
4278
|
interactions: {
|
|
4100
4279
|
create: async (args) => {
|
|
4101
4280
|
const params = args;
|
|
4102
|
-
const response = await sdk.interactions.create(params);
|
|
4281
|
+
const response = await sdk.interactions.create(params, interactionOptions);
|
|
4103
4282
|
return response;
|
|
4104
4283
|
},
|
|
4105
4284
|
get: async (jobId, params) => {
|
|
4106
|
-
const response = await sdk.interactions.get(jobId, params);
|
|
4285
|
+
const response = await sdk.interactions.get(jobId, params, interactionOptions);
|
|
4107
4286
|
return response;
|
|
4108
4287
|
}
|
|
4109
4288
|
}
|
|
@@ -4117,7 +4296,7 @@ var OMNI_POLL_INTERVAL_MS = 1e4;
|
|
|
4117
4296
|
var RESULT_TIMEOUT_MS2 = 15 * 60 * 1e3;
|
|
4118
4297
|
var OMNI_INLINE_LIMIT_BYTES = 4 * 1024 * 1024;
|
|
4119
4298
|
var MEDIA_PART_INLINE_LIMIT_BYTES = 20 * 1024 * 1024;
|
|
4120
|
-
var errorTextSchema =
|
|
4299
|
+
var errorTextSchema = import_zod10.z.string();
|
|
4121
4300
|
function isGoogleGenaiVideoKnown(providerModelId) {
|
|
4122
4301
|
return providerModelId.startsWith("veo-") || providerModelId.startsWith("gemini-omni");
|
|
4123
4302
|
}
|
|
@@ -4272,7 +4451,11 @@ async function generateVeoVideo(req, ctx, createGenAIClient) {
|
|
|
4272
4451
|
referenceType: "ASSET"
|
|
4273
4452
|
}));
|
|
4274
4453
|
}
|
|
4275
|
-
const client2 = createGenAIClient({
|
|
4454
|
+
const client2 = createGenAIClient({
|
|
4455
|
+
apiKey,
|
|
4456
|
+
...ctx.call.maxRetries !== void 0 ? { maxRetries: ctx.call.maxRetries } : {},
|
|
4457
|
+
...ctx.signal !== void 0 ? { signal: ctx.signal } : {}
|
|
4458
|
+
});
|
|
4276
4459
|
const config = { durationSeconds: duration, personGeneration };
|
|
4277
4460
|
if (req.aspectRatio !== void 0) config.aspectRatio = req.aspectRatio;
|
|
4278
4461
|
if (req.resolution !== void 0) config.resolution = req.resolution;
|
|
@@ -4285,8 +4468,7 @@ async function generateVeoVideo(req, ctx, createGenAIClient) {
|
|
|
4285
4468
|
config
|
|
4286
4469
|
};
|
|
4287
4470
|
if (image !== void 0) requestBody.image = image;
|
|
4288
|
-
const retryOptions =
|
|
4289
|
-
if (ctx.signal !== void 0) retryOptions.signal = ctx.signal;
|
|
4471
|
+
const retryOptions = mediaRetryOptions(ctx.call, ctx.signal);
|
|
4290
4472
|
let operation;
|
|
4291
4473
|
try {
|
|
4292
4474
|
operation = await withRetry(async () => {
|
|
@@ -4399,7 +4581,11 @@ async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
|
4399
4581
|
if (omniResolution !== void 0) responseFormat.resolution = omniResolution;
|
|
4400
4582
|
}
|
|
4401
4583
|
if (req.durationSeconds !== void 0) responseFormat.duration = `${req.durationSeconds}s`;
|
|
4402
|
-
const client2 = createGenAIClient({
|
|
4584
|
+
const client2 = createGenAIClient({
|
|
4585
|
+
apiKey,
|
|
4586
|
+
...ctx.call.maxRetries !== void 0 ? { maxRetries: ctx.call.maxRetries } : {},
|
|
4587
|
+
...ctx.signal !== void 0 ? { signal: ctx.signal } : {}
|
|
4588
|
+
});
|
|
4403
4589
|
const uploadedByUri = /* @__PURE__ */ new Map();
|
|
4404
4590
|
const uploadedNames = [];
|
|
4405
4591
|
const uploadOne = async (kind, bytes, mime) => {
|
|
@@ -4410,6 +4596,7 @@ async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
|
4410
4596
|
label: "gemini-omni"
|
|
4411
4597
|
};
|
|
4412
4598
|
if (ctx.signal !== void 0) uploadOptions.signal = ctx.signal;
|
|
4599
|
+
if (ctx.call.maxRetries !== void 0) uploadOptions.maxRetries = ctx.call.maxRetries;
|
|
4413
4600
|
const file = await uploadGoogleFile({ bytes, mime }, uploadOptions);
|
|
4414
4601
|
uploadedByUri.set(file.fileUri, { type: kind, bytes, mime: file.mimeType });
|
|
4415
4602
|
uploadedNames.push(file.fileName);
|
|
@@ -4425,8 +4612,7 @@ async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
|
4425
4612
|
generation_config: { video_config: { task } },
|
|
4426
4613
|
store: true
|
|
4427
4614
|
});
|
|
4428
|
-
const createRetryOptions =
|
|
4429
|
-
if (ctx.signal !== void 0) createRetryOptions.signal = ctx.signal;
|
|
4615
|
+
const createRetryOptions = mediaRetryOptions(ctx.call, ctx.signal);
|
|
4430
4616
|
let interaction;
|
|
4431
4617
|
try {
|
|
4432
4618
|
const parts = [];
|
|
@@ -4496,7 +4682,7 @@ async function generateOmniVideo(req, ctx, createGenAIClient) {
|
|
|
4496
4682
|
}
|
|
4497
4683
|
}, createRetryOptions);
|
|
4498
4684
|
} catch (cause) {
|
|
4499
|
-
if (uploadedByUri.size === 0 || !isFileReferenceRejected(cause))
|
|
4685
|
+
if (ctx.call.maxRetries !== void 0 || uploadedByUri.size === 0 || !isFileReferenceRejected(cause))
|
|
4500
4686
|
throwSdkError(cause, modelId);
|
|
4501
4687
|
const inlineParts = parts.map((part) => {
|
|
4502
4688
|
if (part.uri === void 0) return part;
|
|
@@ -4606,9 +4792,12 @@ async function generateLyriaAudio(req, ctx, createGenAIClient) {
|
|
|
4606
4792
|
const image = await resolveMedia(ref, ctx.fetch, ctx.signal, "image/jpeg", modelId);
|
|
4607
4793
|
parts.push({ inlineData: { mimeType: image.mime, data: toBase64(image.bytes) } });
|
|
4608
4794
|
}
|
|
4609
|
-
const client2 = createGenAIClient({
|
|
4610
|
-
|
|
4611
|
-
|
|
4795
|
+
const client2 = createGenAIClient({
|
|
4796
|
+
apiKey,
|
|
4797
|
+
...ctx.call.maxRetries !== void 0 ? { maxRetries: ctx.call.maxRetries } : {},
|
|
4798
|
+
...ctx.signal !== void 0 ? { signal: ctx.signal } : {}
|
|
4799
|
+
});
|
|
4800
|
+
const retryOptions = mediaRetryOptions(ctx.call, ctx.signal);
|
|
4612
4801
|
let response;
|
|
4613
4802
|
try {
|
|
4614
4803
|
response = await withRetry(async () => {
|
|
@@ -4670,6 +4859,7 @@ async function prepareGoogleMediaPart(ref, mediaType, ctx) {
|
|
|
4670
4859
|
label: "media-part"
|
|
4671
4860
|
};
|
|
4672
4861
|
if (ctx.signal !== void 0) uploadOptions.signal = ctx.signal;
|
|
4862
|
+
if (ctx.call.maxRetries !== void 0) uploadOptions.maxRetries = ctx.call.maxRetries;
|
|
4673
4863
|
const file = await uploadGoogleFile({ bytes: ref.bytes, mime: mediaType }, uploadOptions);
|
|
4674
4864
|
return {
|
|
4675
4865
|
type: "file",
|
|
@@ -4682,15 +4872,15 @@ async function prepareGoogleMediaPart(ref, mediaType, ctx) {
|
|
|
4682
4872
|
// src/adapters/google-image.ts
|
|
4683
4873
|
var import_google2 = require("@ai-sdk/google");
|
|
4684
4874
|
var import_ai2 = require("ai");
|
|
4685
|
-
var
|
|
4875
|
+
var import_zod11 = require("zod");
|
|
4686
4876
|
var GEMINI_IMAGE_TIMEOUT_MS = 6e5;
|
|
4687
4877
|
var defaultGoogleImageDeps = {
|
|
4688
4878
|
createGoogleClient: (opts) => (0, import_google2.createGoogleGenerativeAI)(opts),
|
|
4689
4879
|
runTextGeneration: (opts) => (0, import_ai2.generateText)(opts)
|
|
4690
4880
|
};
|
|
4691
|
-
var causeChainNodeSchema =
|
|
4692
|
-
message:
|
|
4693
|
-
cause:
|
|
4881
|
+
var causeChainNodeSchema = import_zod11.z.object({
|
|
4882
|
+
message: import_zod11.z.string().optional(),
|
|
4883
|
+
cause: import_zod11.z.unknown().optional()
|
|
4694
4884
|
});
|
|
4695
4885
|
function extractBlockReason(cause) {
|
|
4696
4886
|
let current = cause;
|
|
@@ -4758,8 +4948,7 @@ function createGoogleImageAdapter(deps = defaultGoogleImageDeps) {
|
|
|
4758
4948
|
responseModalities: ["IMAGE"]
|
|
4759
4949
|
};
|
|
4760
4950
|
if (req.safetySettings !== void 0) googleOptions.safetySettings = req.safetySettings;
|
|
4761
|
-
const retryOptions =
|
|
4762
|
-
if (ctx.signal !== void 0) retryOptions.signal = ctx.signal;
|
|
4951
|
+
const retryOptions = mediaRetryOptions(ctx.call, ctx.signal);
|
|
4763
4952
|
const result = await withRetry(async () => {
|
|
4764
4953
|
const timeout = AbortSignal.timeout(GEMINI_IMAGE_TIMEOUT_MS);
|
|
4765
4954
|
const signal = ctx.signal !== void 0 ? AbortSignal.any([ctx.signal, timeout]) : timeout;
|
|
@@ -4811,7 +5000,7 @@ function createGoogleImageAdapter(deps = defaultGoogleImageDeps) {
|
|
|
4811
5000
|
var googleImageAdapter = createGoogleImageAdapter();
|
|
4812
5001
|
|
|
4813
5002
|
// src/adapters/higgsfield.ts
|
|
4814
|
-
var
|
|
5003
|
+
var import_zod12 = require("zod");
|
|
4815
5004
|
|
|
4816
5005
|
// src/adapters/higgsfield-motions.ts
|
|
4817
5006
|
var MOTION_MAP = {
|
|
@@ -5010,14 +5199,14 @@ function videoUsage3(ctx, req, duration) {
|
|
|
5010
5199
|
if (req.durationSeconds !== void 0) event.durationSeconds = req.durationSeconds;
|
|
5011
5200
|
return event;
|
|
5012
5201
|
}
|
|
5013
|
-
var klingCreateSchema =
|
|
5014
|
-
var klingStatusSchema =
|
|
5015
|
-
status:
|
|
5016
|
-
video:
|
|
5202
|
+
var klingCreateSchema = import_zod12.z.object({ status_url: import_zod12.z.string() });
|
|
5203
|
+
var klingStatusSchema = import_zod12.z.object({
|
|
5204
|
+
status: import_zod12.z.string().optional(),
|
|
5205
|
+
video: import_zod12.z.object({ url: import_zod12.z.string().optional() }).optional(),
|
|
5017
5206
|
error: JsonValueSchema.optional()
|
|
5018
5207
|
});
|
|
5019
|
-
var errorMessageSchema =
|
|
5020
|
-
var textValueSchema =
|
|
5208
|
+
var errorMessageSchema = import_zod12.z.object({ message: import_zod12.z.string().optional() });
|
|
5209
|
+
var textValueSchema = import_zod12.z.string();
|
|
5021
5210
|
function klingErrorDetail(error) {
|
|
5022
5211
|
if (error === void 0) return "unknown_error";
|
|
5023
5212
|
const asText = textValueSchema.safeParse(error);
|
|
@@ -5066,8 +5255,7 @@ async function generateKlingVideo(req, ctx) {
|
|
|
5066
5255
|
body.aspect_ratio = req.aspectRatio;
|
|
5067
5256
|
if (req.lastFrame) body.last_image_url = refToUrl(req.lastFrame);
|
|
5068
5257
|
const endpoint = klingEndpoint(ctx.model.providerModelId, ctx.model.id);
|
|
5069
|
-
const retryOptions =
|
|
5070
|
-
if (ctx.signal !== void 0) retryOptions.signal = ctx.signal;
|
|
5258
|
+
const retryOptions = mediaRetryOptions(ctx.call, ctx.signal);
|
|
5071
5259
|
const createRes = await withRetry(
|
|
5072
5260
|
() => createRequest(ctx, endpoint, { method: "POST", body: JSON.stringify(body) }),
|
|
5073
5261
|
retryOptions
|
|
@@ -5087,10 +5275,12 @@ async function generateKlingVideo(req, ctx) {
|
|
|
5087
5275
|
let res;
|
|
5088
5276
|
try {
|
|
5089
5277
|
res = await ctx.fetch(statusUrl, requestInit(ctx, { method: "GET" }));
|
|
5090
|
-
} catch {
|
|
5278
|
+
} catch (cause) {
|
|
5279
|
+
if (ctx.call.maxRetries === 0) throw classifyThrown("higgsfield", cause, ctx.model.id);
|
|
5091
5280
|
return void 0;
|
|
5092
5281
|
}
|
|
5093
|
-
if (res.status === 429 || res.status >= 500)
|
|
5282
|
+
if (ctx.call.maxRetries !== 0 && (res.status === 429 || res.status >= 500))
|
|
5283
|
+
return void 0;
|
|
5094
5284
|
if (!res.ok) {
|
|
5095
5285
|
throw classifyHttpError(res.status, await readBody(res), "higgsfield", ctx.model.id);
|
|
5096
5286
|
}
|
|
@@ -5132,6 +5322,7 @@ async function generateKlingVideo(req, ctx) {
|
|
|
5132
5322
|
const url = await check();
|
|
5133
5323
|
return url ? { status: "succeeded" } : { status: "pending" };
|
|
5134
5324
|
} catch (cause) {
|
|
5325
|
+
if (ctx.call.maxRetries === 0) throw cause;
|
|
5135
5326
|
if (isModelKitError(cause) && !cause.retryable) return { status: "failed" };
|
|
5136
5327
|
throw cause;
|
|
5137
5328
|
}
|
|
@@ -5151,8 +5342,8 @@ async function generateKlingVideo(req, ctx) {
|
|
|
5151
5342
|
}
|
|
5152
5343
|
};
|
|
5153
5344
|
}
|
|
5154
|
-
var dopMotionOptionsSchema =
|
|
5155
|
-
modelkit:
|
|
5345
|
+
var dopMotionOptionsSchema = import_zod12.z.object({
|
|
5346
|
+
modelkit: import_zod12.z.object({ motion: import_zod12.z.string().optional() }).optional()
|
|
5156
5347
|
});
|
|
5157
5348
|
function dopMotionId(ctx) {
|
|
5158
5349
|
const parsed = dopMotionOptionsSchema.safeParse(ctx.model.providerOptions);
|
|
@@ -5168,7 +5359,7 @@ function dopMotionId(ctx) {
|
|
|
5168
5359
|
});
|
|
5169
5360
|
return id;
|
|
5170
5361
|
}
|
|
5171
|
-
var jsonRecordSchema =
|
|
5362
|
+
var jsonRecordSchema = import_zod12.z.record(import_zod12.z.string(), JsonValueSchema);
|
|
5172
5363
|
var DOP_URL_KEYS = ["url", "video_url", "output_url", "result_url", "download_url"];
|
|
5173
5364
|
var DOP_NESTED_KEYS = ["video", "result", "results"];
|
|
5174
5365
|
function dopUrl(body) {
|
|
@@ -5196,8 +5387,8 @@ function dopUrl(body) {
|
|
|
5196
5387
|
}
|
|
5197
5388
|
return void 0;
|
|
5198
5389
|
}
|
|
5199
|
-
var dopCreateSchema =
|
|
5200
|
-
var dopStatusSchema =
|
|
5390
|
+
var dopCreateSchema = import_zod12.z.object({ id: import_zod12.z.string() });
|
|
5391
|
+
var dopStatusSchema = import_zod12.z.object({ status: import_zod12.z.string().optional() });
|
|
5201
5392
|
async function generateDopVideo(req, ctx) {
|
|
5202
5393
|
const firstFrame = req.firstFrame ? refToUrl(req.firstFrame) : void 0;
|
|
5203
5394
|
if (!firstFrame) {
|
|
@@ -5222,8 +5413,7 @@ async function generateDopVideo(req, ctx) {
|
|
|
5222
5413
|
params.input_images_end = [{ type: "image_url", image_url: refToUrl(req.lastFrame) }];
|
|
5223
5414
|
}
|
|
5224
5415
|
if (req.seed !== void 0) params.seed = req.seed;
|
|
5225
|
-
const retryOptions =
|
|
5226
|
-
if (ctx.signal !== void 0) retryOptions.signal = ctx.signal;
|
|
5416
|
+
const retryOptions = mediaRetryOptions(ctx.call, ctx.signal);
|
|
5227
5417
|
const createRes = await withRetry(
|
|
5228
5418
|
() => createRequest(ctx, DOP_ENDPOINT, {
|
|
5229
5419
|
method: "POST",
|
|
@@ -5247,10 +5437,18 @@ async function generateDopVideo(req, ctx) {
|
|
|
5247
5437
|
let res;
|
|
5248
5438
|
try {
|
|
5249
5439
|
res = await ctx.fetch(statusUrl, requestInit(ctx, { method: "GET" }));
|
|
5250
|
-
} catch {
|
|
5440
|
+
} catch (cause) {
|
|
5441
|
+
if (ctx.call.maxRetries === 0) throw classifyThrown("higgsfield", cause, ctx.model.id);
|
|
5251
5442
|
return void 0;
|
|
5252
5443
|
}
|
|
5253
5444
|
if (!res.ok) {
|
|
5445
|
+
if (ctx.call.maxRetries === 0)
|
|
5446
|
+
throw classifyHttpError(
|
|
5447
|
+
res.status,
|
|
5448
|
+
await readBody(res),
|
|
5449
|
+
"higgsfield",
|
|
5450
|
+
ctx.model.id
|
|
5451
|
+
);
|
|
5254
5452
|
return void 0;
|
|
5255
5453
|
}
|
|
5256
5454
|
const payload = JsonValueSchema.parse(await res.json());
|
|
@@ -5287,6 +5485,7 @@ async function generateDopVideo(req, ctx) {
|
|
|
5287
5485
|
const url = await check();
|
|
5288
5486
|
return url ? { status: "succeeded" } : { status: "pending" };
|
|
5289
5487
|
} catch (cause) {
|
|
5488
|
+
if (ctx.call.maxRetries === 0) throw cause;
|
|
5290
5489
|
if (isModelKitError(cause) && !cause.retryable) return { status: "failed" };
|
|
5291
5490
|
throw cause;
|
|
5292
5491
|
}
|
|
@@ -5317,7 +5516,7 @@ var higgsfieldAdapter = {
|
|
|
5317
5516
|
|
|
5318
5517
|
// src/adapters/openai-image.ts
|
|
5319
5518
|
var import_openai2 = __toESM(require("openai"), 1);
|
|
5320
|
-
var
|
|
5519
|
+
var import_zod13 = require("zod");
|
|
5321
5520
|
var MAX_EDIT_REFERENCES = 16;
|
|
5322
5521
|
function aspectRatioToSize(ratio, tier) {
|
|
5323
5522
|
const TIER_LONG_EDGE = {
|
|
@@ -5400,9 +5599,9 @@ function toProviderError2(cause, ctx) {
|
|
|
5400
5599
|
ctx.model.id
|
|
5401
5600
|
);
|
|
5402
5601
|
}
|
|
5403
|
-
var openaiUsageSchema =
|
|
5404
|
-
input_tokens:
|
|
5405
|
-
output_tokens:
|
|
5602
|
+
var openaiUsageSchema = import_zod13.z.object({
|
|
5603
|
+
input_tokens: import_zod13.z.number().optional(),
|
|
5604
|
+
output_tokens: import_zod13.z.number().optional()
|
|
5406
5605
|
});
|
|
5407
5606
|
var openaiImageAdapter = {
|
|
5408
5607
|
id: "openai-image",
|
|
@@ -5444,8 +5643,7 @@ var openaiImageAdapter = {
|
|
|
5444
5643
|
}
|
|
5445
5644
|
const format = outputFormat !== void 0 ? { output_format: outputFormat } : {};
|
|
5446
5645
|
const mime = outputFormat === "jpeg" ? "image/jpeg" : "image/png";
|
|
5447
|
-
const retryOptions =
|
|
5448
|
-
if (ctx.signal !== void 0) retryOptions.signal = ctx.signal;
|
|
5646
|
+
const retryOptions = mediaRetryOptions(ctx.call, ctx.signal);
|
|
5449
5647
|
const response = await withRetry(async () => {
|
|
5450
5648
|
try {
|
|
5451
5649
|
if (!isEdit) {
|
|
@@ -5765,13 +5963,7 @@ async function generateProdiaImage(req, ctx) {
|
|
|
5765
5963
|
})
|
|
5766
5964
|
);
|
|
5767
5965
|
});
|
|
5768
|
-
const retryOpts =
|
|
5769
|
-
attempts: 3,
|
|
5770
|
-
baseDelayMs: 1e3,
|
|
5771
|
-
factor: 2,
|
|
5772
|
-
maxDelayMs: 1e4
|
|
5773
|
-
};
|
|
5774
|
-
if (ctx.signal !== void 0) retryOpts.signal = ctx.signal;
|
|
5966
|
+
const retryOpts = mediaRetryOptions(ctx.call, ctx.signal);
|
|
5775
5967
|
let res;
|
|
5776
5968
|
try {
|
|
5777
5969
|
res = await withRetry(async () => {
|
|
@@ -5818,7 +6010,66 @@ var prodiaAdapter = {
|
|
|
5818
6010
|
|
|
5819
6011
|
// src/adapters/replicate.ts
|
|
5820
6012
|
var import_replicate = __toESM(require("replicate"), 1);
|
|
5821
|
-
|
|
6013
|
+
|
|
6014
|
+
// src/adapters/replicate-http.ts
|
|
6015
|
+
var import_zod14 = require("zod");
|
|
6016
|
+
var predictionSchema = import_zod14.z.object({
|
|
6017
|
+
id: import_zod14.z.string(),
|
|
6018
|
+
status: import_zod14.z.string(),
|
|
6019
|
+
output: JsonValueSchema.optional(),
|
|
6020
|
+
error: JsonValueSchema.optional(),
|
|
6021
|
+
metrics: import_zod14.z.object({ predict_time: import_zod14.z.number().optional() }).optional()
|
|
6022
|
+
});
|
|
6023
|
+
function createReplicateHttpClient(opts) {
|
|
6024
|
+
async function request(path, body, signal) {
|
|
6025
|
+
let response;
|
|
6026
|
+
try {
|
|
6027
|
+
response = await opts.fetch(`https://api.replicate.com/v1${path}`, {
|
|
6028
|
+
method: body === void 0 ? "GET" : "POST",
|
|
6029
|
+
headers: {
|
|
6030
|
+
Authorization: `Bearer ${opts.auth}`,
|
|
6031
|
+
"Content-Type": "application/json"
|
|
6032
|
+
},
|
|
6033
|
+
...body !== void 0 ? { body: JSON.stringify(body) } : {},
|
|
6034
|
+
...signal !== void 0 ? { signal } : {}
|
|
6035
|
+
});
|
|
6036
|
+
} catch (cause) {
|
|
6037
|
+
throw classifyThrown("replicate", cause);
|
|
6038
|
+
}
|
|
6039
|
+
if (!response.ok)
|
|
6040
|
+
throw classifyHttpError(response.status, await response.text(), "replicate");
|
|
6041
|
+
const parsed = predictionSchema.parse(await response.json());
|
|
6042
|
+
return {
|
|
6043
|
+
id: parsed.id,
|
|
6044
|
+
status: parsed.status,
|
|
6045
|
+
...parsed.output !== void 0 ? { output: parsed.output } : {},
|
|
6046
|
+
...parsed.error !== void 0 ? { error: parsed.error } : {},
|
|
6047
|
+
...parsed.metrics?.predict_time !== void 0 ? { metrics: { predict_time: parsed.metrics.predict_time } } : {}
|
|
6048
|
+
};
|
|
6049
|
+
}
|
|
6050
|
+
return {
|
|
6051
|
+
predictions: {
|
|
6052
|
+
create(options) {
|
|
6053
|
+
if (options.version) {
|
|
6054
|
+
return request(
|
|
6055
|
+
"/predictions",
|
|
6056
|
+
{ input: options.input, version: options.version },
|
|
6057
|
+
options.signal
|
|
6058
|
+
);
|
|
6059
|
+
}
|
|
6060
|
+
return request(
|
|
6061
|
+
`/models/${options.model}/predictions`,
|
|
6062
|
+
{ input: options.input },
|
|
6063
|
+
options.signal
|
|
6064
|
+
);
|
|
6065
|
+
},
|
|
6066
|
+
get: (id, options) => request(`/predictions/${encodeURIComponent(id)}`, void 0, options?.signal)
|
|
6067
|
+
}
|
|
6068
|
+
};
|
|
6069
|
+
}
|
|
6070
|
+
|
|
6071
|
+
// src/adapters/replicate.ts
|
|
6072
|
+
var import_zod15 = require("zod");
|
|
5822
6073
|
|
|
5823
6074
|
// src/adapters/input-spec-mapper.ts
|
|
5824
6075
|
function canonicalValue(req, key) {
|
|
@@ -5857,7 +6108,7 @@ function applyInputSpec(spec, req, refToUrl2, opts) {
|
|
|
5857
6108
|
var POLL_INTERVAL_MS = 1e3;
|
|
5858
6109
|
var POLL_TIMEOUT_MS = 15 * 6e4;
|
|
5859
6110
|
var defaultReplicateDeps = {
|
|
5860
|
-
createReplicateClient: (opts) => new import_replicate.default(opts)
|
|
6111
|
+
createReplicateClient: (opts) => opts.maxRetries === void 0 ? new import_replicate.default(opts) : createReplicateHttpClient(opts)
|
|
5861
6112
|
};
|
|
5862
6113
|
function createReplicateAdapter(deps = defaultReplicateDeps) {
|
|
5863
6114
|
return {
|
|
@@ -5879,7 +6130,11 @@ function client(ctx, deps) {
|
|
|
5879
6130
|
retryable: false
|
|
5880
6131
|
});
|
|
5881
6132
|
}
|
|
5882
|
-
return deps.createReplicateClient({
|
|
6133
|
+
return deps.createReplicateClient({
|
|
6134
|
+
auth: token,
|
|
6135
|
+
fetch: ctx.fetch,
|
|
6136
|
+
...ctx.call.maxRetries !== void 0 ? { maxRetries: ctx.call.maxRetries } : {}
|
|
6137
|
+
});
|
|
5883
6138
|
}
|
|
5884
6139
|
function route(providerModelId) {
|
|
5885
6140
|
if (providerModelId.includes(":")) {
|
|
@@ -5889,8 +6144,8 @@ function route(providerModelId) {
|
|
|
5889
6144
|
if (!providerModelId.includes("/")) return { version: providerModelId };
|
|
5890
6145
|
return { model: providerModelId };
|
|
5891
6146
|
}
|
|
5892
|
-
var replicateResponseStatusSchema =
|
|
5893
|
-
response:
|
|
6147
|
+
var replicateResponseStatusSchema = import_zod15.z.object({
|
|
6148
|
+
response: import_zod15.z.object({ status: import_zod15.z.number() }).partial()
|
|
5894
6149
|
});
|
|
5895
6150
|
function replicateError(cause, ctx) {
|
|
5896
6151
|
if (isModelKitError(cause)) return cause;
|
|
@@ -5917,7 +6172,7 @@ function isUrlFn(value) {
|
|
|
5917
6172
|
function extractOutputUrl(output) {
|
|
5918
6173
|
const root = Array.isArray(output) ? output[0] : output;
|
|
5919
6174
|
if (root === void 0) throw noOutput();
|
|
5920
|
-
const asString =
|
|
6175
|
+
const asString = import_zod15.z.string().safeParse(root);
|
|
5921
6176
|
if (asString.success && asString.data) return asString.data;
|
|
5922
6177
|
if (root instanceof URL) return root.href;
|
|
5923
6178
|
if (isObjectOutput(root)) {
|
|
@@ -5926,15 +6181,15 @@ function extractOutputUrl(output) {
|
|
|
5926
6181
|
if (urlValue !== void 0 && isUrlFn(urlValue)) {
|
|
5927
6182
|
const called = urlValue();
|
|
5928
6183
|
if (called instanceof URL) return called.href;
|
|
5929
|
-
const calledString =
|
|
6184
|
+
const calledString = import_zod15.z.string().safeParse(called);
|
|
5930
6185
|
if (calledString.success && calledString.data) return calledString.data;
|
|
5931
6186
|
} else {
|
|
5932
|
-
const direct =
|
|
6187
|
+
const direct = import_zod15.z.string().safeParse(urlValue);
|
|
5933
6188
|
if (direct.success && direct.data) return direct.data;
|
|
5934
6189
|
}
|
|
5935
6190
|
}
|
|
5936
6191
|
if ("default" in root) {
|
|
5937
|
-
const fallback =
|
|
6192
|
+
const fallback = import_zod15.z.string().safeParse(root.default);
|
|
5938
6193
|
if (fallback.success && fallback.data) return fallback.data;
|
|
5939
6194
|
}
|
|
5940
6195
|
}
|
|
@@ -5964,7 +6219,7 @@ async function runPrediction(ctx, input, deps) {
|
|
|
5964
6219
|
throw replicateError(cause, ctx);
|
|
5965
6220
|
}
|
|
5966
6221
|
},
|
|
5967
|
-
|
|
6222
|
+
mediaRetryOptions(ctx.call, ctx.signal, 1)
|
|
5968
6223
|
);
|
|
5969
6224
|
} catch (cause) {
|
|
5970
6225
|
throw replicateError(cause, ctx);
|
|
@@ -5995,7 +6250,7 @@ async function runPrediction(ctx, input, deps) {
|
|
|
5995
6250
|
}
|
|
5996
6251
|
return void 0;
|
|
5997
6252
|
}, pollOpts);
|
|
5998
|
-
const predictTime =
|
|
6253
|
+
const predictTime = import_zod15.z.number().safeParse(done.metrics?.predict_time);
|
|
5999
6254
|
if (done.output === void 0) throw noOutput();
|
|
6000
6255
|
return {
|
|
6001
6256
|
url: extractOutputUrl(done.output),
|
|
@@ -6363,7 +6618,7 @@ async function generateReplicateVideo(req, ctx, deps) {
|
|
|
6363
6618
|
throw replicateError(cause, ctx);
|
|
6364
6619
|
}
|
|
6365
6620
|
},
|
|
6366
|
-
|
|
6621
|
+
mediaRetryOptions(ctx.call, ctx.signal, 1)
|
|
6367
6622
|
);
|
|
6368
6623
|
} catch (cause) {
|
|
6369
6624
|
throw replicateError(cause, ctx);
|
|
@@ -6380,7 +6635,7 @@ async function generateReplicateVideo(req, ctx, deps) {
|
|
|
6380
6635
|
throw replicateError(cause, ctx);
|
|
6381
6636
|
}
|
|
6382
6637
|
if (current.status === "succeeded") {
|
|
6383
|
-
const predictTime =
|
|
6638
|
+
const predictTime = import_zod15.z.number().safeParse(current.metrics?.predict_time);
|
|
6384
6639
|
if (current.output === void 0) throw noOutput();
|
|
6385
6640
|
return {
|
|
6386
6641
|
url: extractOutputUrl(current.output),
|
|
@@ -6443,8 +6698,8 @@ function lyriaAudioInput(req, ctx) {
|
|
|
6443
6698
|
if (negative) input.negative_prompt = negative;
|
|
6444
6699
|
return input;
|
|
6445
6700
|
}
|
|
6446
|
-
function elevenLabsMusicDuration(
|
|
6447
|
-
return Math.min(300, Math.max(5,
|
|
6701
|
+
function elevenLabsMusicDuration(seconds2) {
|
|
6702
|
+
return Math.min(300, Math.max(5, seconds2 ?? 15));
|
|
6448
6703
|
}
|
|
6449
6704
|
function elevenLabsMusicInput(req, ctx) {
|
|
6450
6705
|
const prompt = req.prompt.trim();
|
|
@@ -6509,9 +6764,9 @@ async function generateReplicateAudio(req, ctx, deps) {
|
|
|
6509
6764
|
const { url, predictTime, raw } = await runPrediction(ctx, input, deps);
|
|
6510
6765
|
const usage2 = audioUsage(ctx, req);
|
|
6511
6766
|
if (ctx.model.usage.unit === "seconds") {
|
|
6512
|
-
const
|
|
6513
|
-
usage2.quantity =
|
|
6514
|
-
usage2.durationSeconds =
|
|
6767
|
+
const seconds2 = elevenLabsMusicDuration(req.durationSeconds);
|
|
6768
|
+
usage2.quantity = seconds2;
|
|
6769
|
+
usage2.durationSeconds = seconds2;
|
|
6515
6770
|
} else {
|
|
6516
6771
|
usage2.quantity = predictTime;
|
|
6517
6772
|
}
|
|
@@ -6521,7 +6776,7 @@ async function generateReplicateAudio(req, ctx, deps) {
|
|
|
6521
6776
|
// src/adapters/xai-video.ts
|
|
6522
6777
|
var import_ai3 = require("ai");
|
|
6523
6778
|
var import_xai2 = require("@ai-sdk/xai");
|
|
6524
|
-
var
|
|
6779
|
+
var import_zod16 = require("zod");
|
|
6525
6780
|
var POLL_INTERVAL_MS2 = 1e4;
|
|
6526
6781
|
var POLL_TIMEOUT_MS2 = 15 * 6e4;
|
|
6527
6782
|
var defaultXaiDeps = {
|
|
@@ -6534,8 +6789,8 @@ function createXaiVideoAdapter(deps = defaultXaiDeps) {
|
|
|
6534
6789
|
};
|
|
6535
6790
|
}
|
|
6536
6791
|
var xaiVideoAdapter = createXaiVideoAdapter();
|
|
6537
|
-
var xaiResponseStatusSchema =
|
|
6538
|
-
response:
|
|
6792
|
+
var xaiResponseStatusSchema = import_zod16.z.object({
|
|
6793
|
+
response: import_zod16.z.object({ status: import_zod16.z.number() }).partial()
|
|
6539
6794
|
});
|
|
6540
6795
|
function xaiError(cause, ctx) {
|
|
6541
6796
|
const info = describeThrown(cause);
|
|
@@ -6543,12 +6798,12 @@ function xaiError(cause, ctx) {
|
|
|
6543
6798
|
const status = info.status ?? (nested.success ? nested.data.response?.status : void 0);
|
|
6544
6799
|
return classifyThrown("xai-video", cause, ctx.model.id, { status, body: info.body });
|
|
6545
6800
|
}
|
|
6546
|
-
var xaiMetaSchema =
|
|
6801
|
+
var xaiMetaSchema = import_zod16.z.object({
|
|
6547
6802
|
videoUrl: JsonValueSchema.optional(),
|
|
6548
6803
|
costInUsdTicks: JsonValueSchema.optional(),
|
|
6549
6804
|
duration: JsonValueSchema.optional()
|
|
6550
6805
|
});
|
|
6551
|
-
var xaiProviderMetadataSchema =
|
|
6806
|
+
var xaiProviderMetadataSchema = import_zod16.z.object({ xai: xaiMetaSchema });
|
|
6552
6807
|
function metaOf(result) {
|
|
6553
6808
|
const parsed = xaiProviderMetadataSchema.partial().safeParse(result.providerMetadata);
|
|
6554
6809
|
if (!parsed.success) return {};
|
|
@@ -6599,6 +6854,7 @@ async function generateXaiVideo(req, ctx, deps) {
|
|
|
6599
6854
|
}
|
|
6600
6855
|
if (req.durationSeconds !== void 0) callBody.duration = req.durationSeconds;
|
|
6601
6856
|
if (ctx.signal) callBody.abortSignal = ctx.signal;
|
|
6857
|
+
if (ctx.call.maxRetries !== void 0) callBody.maxRetries = ctx.call.maxRetries;
|
|
6602
6858
|
const pending = (async () => {
|
|
6603
6859
|
try {
|
|
6604
6860
|
return await client2.generateVideo(callBody);
|
|
@@ -6655,7 +6911,7 @@ async function mapResult(ctx, req, result) {
|
|
|
6655
6911
|
};
|
|
6656
6912
|
}
|
|
6657
6913
|
const meta = metaOf(result);
|
|
6658
|
-
const videoUrl =
|
|
6914
|
+
const videoUrl = import_zod16.z.string().safeParse(meta.videoUrl);
|
|
6659
6915
|
if (videoUrl.success && videoUrl.data) {
|
|
6660
6916
|
const res = await ctx.fetch(videoUrl.data);
|
|
6661
6917
|
if (!res.ok) {
|
|
@@ -6678,7 +6934,7 @@ async function mapResult(ctx, req, result) {
|
|
|
6678
6934
|
});
|
|
6679
6935
|
}
|
|
6680
6936
|
function usage(ctx, req, meta) {
|
|
6681
|
-
const costTicks =
|
|
6937
|
+
const costTicks = import_zod16.z.number().safeParse(meta.costInUsdTicks);
|
|
6682
6938
|
const cost = costTicks.success ? costTicks.data / 1e10 : void 0;
|
|
6683
6939
|
const event = {
|
|
6684
6940
|
model: ctx.model.id,
|
|
@@ -6811,22 +7067,22 @@ var liveState = store[KEY] ??= {
|
|
|
6811
7067
|
var liveCatalog = liveState.catalog;
|
|
6812
7068
|
|
|
6813
7069
|
// src/catalog/load.ts
|
|
6814
|
-
var
|
|
7070
|
+
var import_zod17 = require("zod");
|
|
6815
7071
|
function rowCount() {
|
|
6816
7072
|
return liveCatalog.list({ lifecycle: ["active", "deprecated", "retired"] }).length;
|
|
6817
7073
|
}
|
|
6818
7074
|
function authHeaders(auth) {
|
|
6819
7075
|
return "bearer" in auth ? { Authorization: `Bearer ${auth.bearer}` } : { "x-api-key": auth.apiKey };
|
|
6820
7076
|
}
|
|
6821
|
-
var EnvelopeSchema =
|
|
6822
|
-
rows:
|
|
6823
|
-
revision:
|
|
7077
|
+
var EnvelopeSchema = import_zod17.z.object({
|
|
7078
|
+
rows: import_zod17.z.array(JsonValueSchema),
|
|
7079
|
+
revision: import_zod17.z.string().optional()
|
|
6824
7080
|
});
|
|
6825
7081
|
function dropReason(path, message) {
|
|
6826
7082
|
return path.length === 0 ? message : `${path.map((part) => String(part)).join(".")} ${message}`;
|
|
6827
7083
|
}
|
|
6828
7084
|
function rowId(raw) {
|
|
6829
|
-
const parsed =
|
|
7085
|
+
const parsed = import_zod17.z.object({ id: import_zod17.z.string().min(1) }).loose().safeParse(raw);
|
|
6830
7086
|
if (!parsed.success) return "?";
|
|
6831
7087
|
return parsed.data.id;
|
|
6832
7088
|
}
|
|
@@ -7150,6 +7406,49 @@ function trim(field, refs, limit, model, warnings) {
|
|
|
7150
7406
|
});
|
|
7151
7407
|
return refs.slice(0, limit);
|
|
7152
7408
|
}
|
|
7409
|
+
function knownLength(value) {
|
|
7410
|
+
return value !== void 0 && Number.isFinite(value) ? value : void 0;
|
|
7411
|
+
}
|
|
7412
|
+
function seconds(len, limit) {
|
|
7413
|
+
let digits = 1;
|
|
7414
|
+
while (digits < 3 && Number(len.toFixed(digits)) === limit) digits++;
|
|
7415
|
+
return len.toFixed(digits);
|
|
7416
|
+
}
|
|
7417
|
+
function effectiveMin(limits, taskType) {
|
|
7418
|
+
if (taskType !== "edit" || limits?.editMin === void 0) return limits?.min;
|
|
7419
|
+
return Math.max(limits.min ?? 0, limits.editMin);
|
|
7420
|
+
}
|
|
7421
|
+
function videoLengthViolation(model, input) {
|
|
7422
|
+
const limits = model.constraints.references?.videoSeconds;
|
|
7423
|
+
const refMin = effectiveMin(limits, input.taskType);
|
|
7424
|
+
const lengths = (input.referenceVideos ?? []).map(knownLength);
|
|
7425
|
+
if (limits?.max !== void 0) {
|
|
7426
|
+
const max = limits.max;
|
|
7427
|
+
const i = lengths.findIndex((len) => len !== void 0 && len > max);
|
|
7428
|
+
if (i >= 0)
|
|
7429
|
+
return `${model.label} takes reference videos up to ${String(max)} s each; video ${i + 1} is ${seconds(lengths[i], max)} s.`;
|
|
7430
|
+
}
|
|
7431
|
+
if (refMin !== void 0) {
|
|
7432
|
+
const i = lengths.findIndex((len) => len !== void 0 && len < refMin);
|
|
7433
|
+
if (i >= 0)
|
|
7434
|
+
return `${model.label} takes reference videos of at least ${String(refMin)} s each; video ${i + 1} is ${seconds(lengths[i], refMin)} s.`;
|
|
7435
|
+
}
|
|
7436
|
+
const source = knownLength(input.sourceVideo);
|
|
7437
|
+
const sourceLimits = model.constraints.sourceVideoSeconds;
|
|
7438
|
+
if (limits?.total !== void 0) {
|
|
7439
|
+
const refSum = lengths.reduce((acc, len) => acc + (len ?? 0), 0);
|
|
7440
|
+
const sum = refSum + (sourceLimits?.inReferenceTotal ? source ?? 0 : 0);
|
|
7441
|
+
if (sum > limits.total)
|
|
7442
|
+
return `${model.label} takes reference videos up to ${String(limits.total)} s combined; these add up to ${seconds(sum, limits.total)} s.`;
|
|
7443
|
+
}
|
|
7444
|
+
if (source === void 0) return void 0;
|
|
7445
|
+
const sourceMin = effectiveMin(sourceLimits, input.taskType);
|
|
7446
|
+
if (sourceLimits?.max !== void 0 && source > sourceLimits.max)
|
|
7447
|
+
return `${model.label} takes a source video up to ${String(sourceLimits.max)} s; this one is ${seconds(source, sourceLimits.max)} s.`;
|
|
7448
|
+
if (sourceMin !== void 0 && source < sourceMin)
|
|
7449
|
+
return `${model.label} takes a source video of at least ${String(sourceMin)} s; this one is ${seconds(source, sourceMin)} s.`;
|
|
7450
|
+
return void 0;
|
|
7451
|
+
}
|
|
7153
7452
|
function resolveImageOptions(model, request, onUnsupported = "error") {
|
|
7154
7453
|
const warnings = [];
|
|
7155
7454
|
const c = model.constraints;
|
|
@@ -7335,6 +7634,21 @@ function resolveVideoOptions(model, request, onUnsupported = "error") {
|
|
|
7335
7634
|
model: model.id
|
|
7336
7635
|
});
|
|
7337
7636
|
}
|
|
7637
|
+
const lengthViolation = videoLengthViolation(model, {
|
|
7638
|
+
referenceVideos: videos.map((v) => v.durationSeconds),
|
|
7639
|
+
taskType: merged.taskType,
|
|
7640
|
+
...merged.sourceVideo?.durationSeconds !== void 0 && {
|
|
7641
|
+
sourceVideo: merged.sourceVideo.durationSeconds
|
|
7642
|
+
}
|
|
7643
|
+
});
|
|
7644
|
+
if (lengthViolation !== void 0) {
|
|
7645
|
+
throw new ModelKitError({
|
|
7646
|
+
code: "unsupported_option",
|
|
7647
|
+
message: lengthViolation,
|
|
7648
|
+
model: model.id,
|
|
7649
|
+
retryable: false
|
|
7650
|
+
});
|
|
7651
|
+
}
|
|
7338
7652
|
setOrDelete(merged, "referenceImages", images);
|
|
7339
7653
|
setOrDelete(merged, "referenceVideos", videos);
|
|
7340
7654
|
setOrDelete(merged, "referenceAudios", audios);
|
|
@@ -7579,7 +7893,7 @@ function createTextSteps(initial, resolve, callerProviderOptions) {
|
|
|
7579
7893
|
|
|
7580
7894
|
// src/client.ts
|
|
7581
7895
|
var import_ai5 = require("ai");
|
|
7582
|
-
var
|
|
7896
|
+
var import_zod18 = require("zod");
|
|
7583
7897
|
|
|
7584
7898
|
// src/generated-image.ts
|
|
7585
7899
|
function toGeneratedImage(image) {
|
|
@@ -7654,10 +7968,10 @@ function textAbortOptions(sdk, context) {
|
|
|
7654
7968
|
if (abortSignal) return { abortSignal };
|
|
7655
7969
|
return {};
|
|
7656
7970
|
}
|
|
7657
|
-
var catalogId =
|
|
7658
|
-
var exactSize =
|
|
7659
|
-
var sdkRatio =
|
|
7660
|
-
var sizeTier =
|
|
7971
|
+
var catalogId = import_zod18.z.string();
|
|
7972
|
+
var exactSize = import_zod18.z.templateLiteral([import_zod18.z.number(), "x", import_zod18.z.number()]);
|
|
7973
|
+
var sdkRatio = import_zod18.z.templateLiteral([import_zod18.z.number(), ":", import_zod18.z.number()]);
|
|
7974
|
+
var sizeTier = import_zod18.z.enum(["512", "1K", "2K", "4K"]);
|
|
7661
7975
|
function isCatalogId(model) {
|
|
7662
7976
|
return catalogId.safeParse(model).success;
|
|
7663
7977
|
}
|
|
@@ -7848,6 +8162,7 @@ function createModelKit(config, adapters) {
|
|
|
7848
8162
|
logCoercions(logger, model.modelId, warnings);
|
|
7849
8163
|
const abortSignal = combineSignals(req.abortSignal, call.signal);
|
|
7850
8164
|
if (abortSignal) options.abortSignal = abortSignal;
|
|
8165
|
+
if (call.maxRetries !== void 0) options.maxRetries = call.maxRetries;
|
|
7851
8166
|
const result = await (0, import_ai5.generateImage)(options);
|
|
7852
8167
|
const images = result.images.map(
|
|
7853
8168
|
(file) => toGeneratedImage({ bytes: file.uint8Array, mime: file.mediaType })
|
|
@@ -7884,15 +8199,16 @@ function createModelKit(config, adapters) {
|
|
|
7884
8199
|
promptExpansion: _promptExpansion,
|
|
7885
8200
|
mode: _mode
|
|
7886
8201
|
} = req;
|
|
7887
|
-
const
|
|
8202
|
+
const seconds2 = duration ?? durationSeconds;
|
|
7888
8203
|
const abortSignal = combineSignals(req.abortSignal, call.signal);
|
|
7889
8204
|
const options = { model, prompt };
|
|
8205
|
+
if (call.maxRetries !== void 0) options.maxRetries = call.maxRetries;
|
|
7890
8206
|
const warnings = [];
|
|
7891
8207
|
const ratio = sdkAspectRatio(aspectRatio) ?? (aspectRatio === "adaptive" ? "adaptive" : void 0);
|
|
7892
8208
|
if (ratio) options.aspectRatio = ratio;
|
|
7893
8209
|
else if (aspectRatio !== void 0)
|
|
7894
8210
|
pushIgnored(warnings, "aspectRatio", aspectRatio, model.modelId);
|
|
7895
|
-
if (
|
|
8211
|
+
if (seconds2 !== void 0) options.duration = seconds2;
|
|
7896
8212
|
if (seed !== void 0) options.seed = seed;
|
|
7897
8213
|
if (generateAudio2 !== void 0) options.generateAudio = generateAudio2;
|
|
7898
8214
|
const frames = [
|
|
@@ -7939,8 +8255,8 @@ function createModelKit(config, adapters) {
|
|
|
7939
8255
|
provider: model.provider,
|
|
7940
8256
|
modality: "VIDEO",
|
|
7941
8257
|
unit: "seconds",
|
|
7942
|
-
quantity:
|
|
7943
|
-
...
|
|
8258
|
+
quantity: seconds2 ?? 0,
|
|
8259
|
+
...seconds2 !== void 0 && { durationSeconds: seconds2 }
|
|
7944
8260
|
};
|
|
7945
8261
|
settled = {
|
|
7946
8262
|
video: { bytes: result.video.uint8Array, mime: result.video.mediaType },
|
|
@@ -7968,6 +8284,7 @@ function createModelKit(config, adapters) {
|
|
|
7968
8284
|
catalog,
|
|
7969
8285
|
describe: (id) => catalog.require(id),
|
|
7970
8286
|
async generateImage(req, call = {}) {
|
|
8287
|
+
mediaRetryOptions(call);
|
|
7971
8288
|
if (!isCatalogId(req.model)) {
|
|
7972
8289
|
return passthroughImage({ ...req, model: req.model }, call);
|
|
7973
8290
|
}
|
|
@@ -8011,6 +8328,7 @@ function createModelKit(config, adapters) {
|
|
|
8011
8328
|
return { ...result, usage: usage2, image: first, images, warnings };
|
|
8012
8329
|
},
|
|
8013
8330
|
async generateVideo(req, call = {}) {
|
|
8331
|
+
mediaRetryOptions(call);
|
|
8014
8332
|
if (!isCatalogId(req.model)) {
|
|
8015
8333
|
return passthroughVideo({ ...req, model: req.model }, call);
|
|
8016
8334
|
}
|
|
@@ -8065,6 +8383,7 @@ function createModelKit(config, adapters) {
|
|
|
8065
8383
|
};
|
|
8066
8384
|
},
|
|
8067
8385
|
async generateAudio(req, call = {}) {
|
|
8386
|
+
mediaRetryOptions(call);
|
|
8068
8387
|
const model = catalog.require(req.model);
|
|
8069
8388
|
if (model.type !== "audio") {
|
|
8070
8389
|
throw new ModelKitError({
|
|
@@ -8094,6 +8413,7 @@ function createModelKit(config, adapters) {
|
|
|
8094
8413
|
return result;
|
|
8095
8414
|
},
|
|
8096
8415
|
async prepareMediaPart(modelId, ref, mediaType, call = {}) {
|
|
8416
|
+
mediaRetryOptions(call);
|
|
8097
8417
|
const model = catalog.require(modelId);
|
|
8098
8418
|
const { adapter, ctx } = context(model, call, []);
|
|
8099
8419
|
const fileStore = model.adapter === "ai-sdk-text" && textKeyName(model) === "google" ? adapters["google-genai"] : adapter;
|
|
@@ -8287,6 +8607,7 @@ var describe = (modelId) => createDefaultModelKit().describe(modelId);
|
|
|
8287
8607
|
textUsageEvent,
|
|
8288
8608
|
toGeneratedImage,
|
|
8289
8609
|
validateInputSpec,
|
|
8610
|
+
videoLengthViolation,
|
|
8290
8611
|
withRetry,
|
|
8291
8612
|
xaiVideoAdapter,
|
|
8292
8613
|
...require("ai")
|