@shop-os/modelkit 0.15.1 → 0.17.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 +25 -0
- package/README.md +29 -2
- package/dist/catalog.cjs +90 -1
- package/dist/catalog.cjs.map +1 -1
- package/dist/catalog.d.cts +30 -2
- package/dist/catalog.d.ts +30 -2
- package/dist/catalog.js +90 -1
- package/dist/catalog.js.map +1 -1
- package/dist/index.cjs +413 -100
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +92 -2
- package/dist/index.d.ts +92 -2
- package/dist/index.js +408 -100
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -140,8 +140,8 @@ function normalizeExposure(value) {
|
|
|
140
140
|
if (value.every(
|
|
141
141
|
(entry) => SURFACES.includes(entry)
|
|
142
142
|
)) {
|
|
143
|
-
const
|
|
144
|
-
return SURFACES.filter((surface) =>
|
|
143
|
+
const present3 = new Set(value);
|
|
144
|
+
return SURFACES.filter((surface) => present3.has(surface));
|
|
145
145
|
}
|
|
146
146
|
return value;
|
|
147
147
|
}
|
|
@@ -5074,6 +5074,7 @@ var KLING_PROMPT = { referenceSyntax: "numbered", maxChars: 2500 };
|
|
|
5074
5074
|
var H3_PROMPT = { referenceSyntax: "numbered", maxChars: 5e4 };
|
|
5075
5075
|
var KLING_V4_PROMPT = { referenceSyntax: "numbered", maxChars: 2500 };
|
|
5076
5076
|
var KLING_V4_AUDIO = "always";
|
|
5077
|
+
var KLING_V4_FLASH_PROMPT = { referenceSyntax: "numbered", maxChars: 26e3 };
|
|
5077
5078
|
var NO_REFERENCES = { images: 0, videos: 0, audios: 0 };
|
|
5078
5079
|
function klingMotionControl(tier) {
|
|
5079
5080
|
const label = tier === "pro" ? "Pro" : "Standard";
|
|
@@ -5251,6 +5252,85 @@ var VIDEO_FAMILY_MODELS = [
|
|
|
5251
5252
|
},
|
|
5252
5253
|
capabilities: { acceptsSourceVideo: true, videoEdit: true }
|
|
5253
5254
|
},
|
|
5255
|
+
{
|
|
5256
|
+
// https://fal.ai/models/kling/v4/flash/reference-to-video/api (+ /text-to-video,
|
|
5257
|
+
// /image-to-video): routed like kling-4.0, at 720p only and 3-20 s. image-to-video takes
|
|
5258
|
+
// a start frame (300 px min, ratio 0.40-2.50) and no end frame; its prompt is optional.
|
|
5259
|
+
// Reference images: 10, the start frame included. Reference videos: 5, 3-20.05 s each
|
|
5260
|
+
// and 20 s combined, 700 px min. Prompts up to 26,000 characters. No sound switch.
|
|
5261
|
+
id: "kling-4.0-flash",
|
|
5262
|
+
aliases: ["kling-4-flash", "kling-v4-flash"],
|
|
5263
|
+
label: "Kling 4.0 Flash",
|
|
5264
|
+
type: "video",
|
|
5265
|
+
adapter: "fal",
|
|
5266
|
+
providerModelId: "kling/v4/flash",
|
|
5267
|
+
lifecycle: "active",
|
|
5268
|
+
exposure: ["cowork", "space_builder"],
|
|
5269
|
+
constraints: {
|
|
5270
|
+
aspectRatios: ["adaptive", "16:9", "9:16", "1:1", "21:9"],
|
|
5271
|
+
resolutions: ["720p"],
|
|
5272
|
+
durations: { min: 3, max: 20 },
|
|
5273
|
+
durationPresets: [5, 10, 15, 20],
|
|
5274
|
+
references: {
|
|
5275
|
+
...NO_REFERENCES,
|
|
5276
|
+
images: 10,
|
|
5277
|
+
videos: 5,
|
|
5278
|
+
videoSeconds: { min: 3, max: 20.05, total: 20 },
|
|
5279
|
+
videoPixels: { minWidth: 700, minHeight: 700 }
|
|
5280
|
+
},
|
|
5281
|
+
supports: [],
|
|
5282
|
+
prompt: KLING_V4_FLASH_PROMPT,
|
|
5283
|
+
audio: KLING_V4_AUDIO
|
|
5284
|
+
},
|
|
5285
|
+
defaults: { durationSeconds: 5, resolution: "720p", aspectRatio: "16:9" },
|
|
5286
|
+
usage: { ...VIDEO_USAGE },
|
|
5287
|
+
pricingKind: "video_per_second",
|
|
5288
|
+
display: {
|
|
5289
|
+
provider: "kuaishou",
|
|
5290
|
+
logo: KLING_LOGO,
|
|
5291
|
+
description: "Kuaishou Kling 4.0 Flash: fast text-to-video, a start frame, or reference-to-video from up to 10 images and 5 videos (20 seconds combined), 3 to 20 seconds at 720p. Prompts name references @Image1, @Video1."
|
|
5292
|
+
}
|
|
5293
|
+
},
|
|
5294
|
+
{
|
|
5295
|
+
// https://fal.ai/models/kling/v4/flash/video-to-video/api: source 3-20.05 s (700 px
|
|
5296
|
+
// min, 23.9-60.05 fps), up to 10 reference images and 4 reference videos; the source
|
|
5297
|
+
// and reference videos total 20 s at most. duration 3-20, or left out for the source's
|
|
5298
|
+
// length. The prompt names the source @Video1, reference videos @Video2 on, images @Image1.
|
|
5299
|
+
id: "kling-4.0-flash-edit",
|
|
5300
|
+
aliases: ["kling-4-flash-edit", "kling-v4-flash-edit"],
|
|
5301
|
+
label: "Kling 4.0 Flash Edit",
|
|
5302
|
+
type: "video",
|
|
5303
|
+
adapter: "fal",
|
|
5304
|
+
providerModelId: "kling/v4/flash/video-to-video",
|
|
5305
|
+
lifecycle: "active",
|
|
5306
|
+
exposure: ["cowork", "space_builder"],
|
|
5307
|
+
constraints: {
|
|
5308
|
+
resolutions: ["720p"],
|
|
5309
|
+
durations: { min: 3, max: 20 },
|
|
5310
|
+
references: {
|
|
5311
|
+
...NO_REFERENCES,
|
|
5312
|
+
images: 10,
|
|
5313
|
+
videos: 4,
|
|
5314
|
+
videoSeconds: { min: 3, max: 20.05, total: 20 },
|
|
5315
|
+
videoPixels: { minWidth: 700, minHeight: 700 }
|
|
5316
|
+
},
|
|
5317
|
+
requires: ["sourceVideo"],
|
|
5318
|
+
sourceVideoSeconds: { min: 3, max: 20.05, inReferenceTotal: true },
|
|
5319
|
+
sourceVideoPixels: { minWidth: 700, minHeight: 700 },
|
|
5320
|
+
supports: [],
|
|
5321
|
+
prompt: KLING_V4_FLASH_PROMPT,
|
|
5322
|
+
audio: KLING_V4_AUDIO
|
|
5323
|
+
},
|
|
5324
|
+
defaults: { resolution: "720p" },
|
|
5325
|
+
usage: { ...VIDEO_USAGE },
|
|
5326
|
+
pricingKind: "video_per_second",
|
|
5327
|
+
display: {
|
|
5328
|
+
provider: "kuaishou",
|
|
5329
|
+
logo: KLING_LOGO,
|
|
5330
|
+
description: "Kuaishou Kling 4.0 Flash video edit: change a 3-20 second clip with a prompt, up to 10 reference images and 4 reference videos (20 seconds in all with the source), at 720p, as long as the source or a set 3-20 seconds. Prompts name the source @Video1."
|
|
5331
|
+
},
|
|
5332
|
+
capabilities: { acceptsSourceVideo: true, videoEdit: true }
|
|
5333
|
+
},
|
|
5254
5334
|
{
|
|
5255
5335
|
// https://fal.ai/models/minimax/h3-max-turbo/text-to-video/api (+ /image-to-video).
|
|
5256
5336
|
// image-to-video takes a start frame, an end frame, or both, and no aspect_ratio.
|
|
@@ -8446,9 +8526,6 @@ var aiSdkTextAdapter = {
|
|
|
8446
8526
|
// src/adapters/ark.ts
|
|
8447
8527
|
import { z as z7 } from "zod";
|
|
8448
8528
|
|
|
8449
|
-
// src/retry.ts
|
|
8450
|
-
import { z as z6 } from "zod";
|
|
8451
|
-
|
|
8452
8529
|
// src/json.ts
|
|
8453
8530
|
import { z as z5 } from "zod";
|
|
8454
8531
|
var JsonValueSchema = z5.lazy(
|
|
@@ -8485,6 +8562,7 @@ function describeThrown(cause) {
|
|
|
8485
8562
|
}
|
|
8486
8563
|
|
|
8487
8564
|
// src/retry.ts
|
|
8565
|
+
import { z as z6 } from "zod";
|
|
8488
8566
|
var textBody = z6.string();
|
|
8489
8567
|
var BILLING_CODES = /* @__PURE__ */ new Set([
|
|
8490
8568
|
"insufficient_quota",
|
|
@@ -8781,10 +8859,36 @@ function arkKey(ctx) {
|
|
|
8781
8859
|
}
|
|
8782
8860
|
return key;
|
|
8783
8861
|
}
|
|
8784
|
-
|
|
8862
|
+
var ARK_CODES = /* @__PURE__ */ new Map([
|
|
8863
|
+
["AccountOverdueError", { status: 403, code: "provider_billing" }],
|
|
8864
|
+
["OperationDenied.ServiceOverdue", { status: 403, code: "provider_billing" }],
|
|
8865
|
+
["QuotaExceeded", { status: 429, code: "provider_billing" }],
|
|
8866
|
+
["ModelNotOpen", { status: 404, code: "provider_rejected" }],
|
|
8867
|
+
["InvalidEndpointOrModel.NotFound", { status: 404, code: "provider_rejected" }]
|
|
8868
|
+
]);
|
|
8869
|
+
var ArkErrorBodySchema = z7.object({ error: z7.object({ code: z7.string() }) });
|
|
8870
|
+
function arkErrorCode(body) {
|
|
8871
|
+
const text2 = z7.string().safeParse(body);
|
|
8872
|
+
let decoded = body;
|
|
8873
|
+
if (text2.success) {
|
|
8874
|
+
try {
|
|
8875
|
+
decoded = JsonValueSchema.parse(JSON.parse(text2.data));
|
|
8876
|
+
} catch {
|
|
8877
|
+
return void 0;
|
|
8878
|
+
}
|
|
8879
|
+
}
|
|
8880
|
+
const parsed = ArkErrorBodySchema.safeParse(decoded);
|
|
8881
|
+
return parsed.success ? parsed.data.error.code : void 0;
|
|
8882
|
+
}
|
|
8883
|
+
function arkCodeRule(status, code) {
|
|
8884
|
+
const rule = code === void 0 ? void 0 : ARK_CODES.get(code);
|
|
8885
|
+
if (!rule || status !== void 0 && status !== rule.status) return void 0;
|
|
8886
|
+
return rule;
|
|
8887
|
+
}
|
|
8888
|
+
function throwArkError(status, body, model, code = arkErrorCode(body)) {
|
|
8785
8889
|
const parsed = z7.string().safeParse(body);
|
|
8786
8890
|
const text2 = parsed.success ? parsed.data : JSON.stringify(body ?? "");
|
|
8787
|
-
if (/sensitive.?content|human.?face/i.test(text2)) {
|
|
8891
|
+
if (/sensitive.?content|human.?face/i.test(`${code ?? ""} ${text2}`)) {
|
|
8788
8892
|
const init = {
|
|
8789
8893
|
code: "content_blocked",
|
|
8790
8894
|
message: `ark rejected the request: ${text2.slice(0, 500)}`,
|
|
@@ -8796,15 +8900,18 @@ function throwArkError(status, body, model) {
|
|
|
8796
8900
|
if (status !== void 0) init.status = status;
|
|
8797
8901
|
throw new ModelKitError(init);
|
|
8798
8902
|
}
|
|
8799
|
-
|
|
8800
|
-
|
|
8801
|
-
|
|
8802
|
-
|
|
8903
|
+
const rule = arkCodeRule(status, code);
|
|
8904
|
+
if (rule || status === void 0) {
|
|
8905
|
+
const init = {
|
|
8906
|
+
code: rule?.code ?? "provider_rejected",
|
|
8907
|
+
message: `ark rejected the request${code ? ` (${code})` : ""}: ${text2.slice(0, 500)}`,
|
|
8803
8908
|
provider: "ark",
|
|
8804
8909
|
model,
|
|
8805
8910
|
body,
|
|
8806
8911
|
retryable: false
|
|
8807
|
-
}
|
|
8912
|
+
};
|
|
8913
|
+
if (status !== void 0) init.status = status;
|
|
8914
|
+
throw new ModelKitError(init);
|
|
8808
8915
|
}
|
|
8809
8916
|
throw classifyHttpError(status, body, "ark", model);
|
|
8810
8917
|
}
|
|
@@ -8920,9 +9027,15 @@ var SeedanceResponseSchema = z7.object({
|
|
|
8920
9027
|
duration: z7.number().optional(),
|
|
8921
9028
|
framespersecond: z7.number().optional(),
|
|
8922
9029
|
resolution: z7.string().optional(),
|
|
8923
|
-
error: z7.union([
|
|
9030
|
+
error: z7.union([
|
|
9031
|
+
z7.string(),
|
|
9032
|
+
z7.object({ code: z7.string().optional(), message: z7.string().optional() })
|
|
9033
|
+
]).optional()
|
|
9034
|
+
});
|
|
9035
|
+
var SeedanceErrorSchema = z7.object({
|
|
9036
|
+
code: z7.string().optional(),
|
|
9037
|
+
message: z7.string().optional()
|
|
8924
9038
|
});
|
|
8925
|
-
var SeedanceErrorMessageSchema = z7.object({ message: z7.string().optional() });
|
|
8926
9039
|
function seedanceResolution(req) {
|
|
8927
9040
|
return req.resolution ?? "720p";
|
|
8928
9041
|
}
|
|
@@ -8943,10 +9056,14 @@ function seedanceInitialDuration(req) {
|
|
|
8943
9056
|
function seedanceErrorMessage(error, status) {
|
|
8944
9057
|
const asString = z7.string().safeParse(error);
|
|
8945
9058
|
if (asString.success) return asString.data;
|
|
8946
|
-
const asObject =
|
|
9059
|
+
const asObject = SeedanceErrorSchema.safeParse(error);
|
|
8947
9060
|
if (asObject.success && asObject.data.message !== void 0) return asObject.data.message;
|
|
8948
9061
|
return `Seedance task ${status}`;
|
|
8949
9062
|
}
|
|
9063
|
+
function seedanceErrorCode(error) {
|
|
9064
|
+
const parsed = SeedanceErrorSchema.safeParse(error);
|
|
9065
|
+
return parsed.success ? parsed.data.code : void 0;
|
|
9066
|
+
}
|
|
8950
9067
|
function seedanceJob(ctx, key, jobId, usage5) {
|
|
8951
9068
|
const modelId = ctx.model.id;
|
|
8952
9069
|
const fetchStatus = async () => {
|
|
@@ -8954,9 +9071,9 @@ function seedanceJob(ctx, key, jobId, usage5) {
|
|
|
8954
9071
|
headers: { Authorization: `Bearer ${key}` }
|
|
8955
9072
|
});
|
|
8956
9073
|
if (!res.ok) {
|
|
8957
|
-
if (ctx.call.maxRetries !== 0 && (res.status === 429 || res.status >= 500))
|
|
8958
|
-
return void 0;
|
|
8959
9074
|
const text2 = await res.text().catch(() => "");
|
|
9075
|
+
const transient = (res.status === 429 || res.status >= 500) && !arkCodeRule(res.status, arkErrorCode(text2));
|
|
9076
|
+
if (ctx.call.maxRetries !== 0 && transient) return void 0;
|
|
8960
9077
|
throwArkError(res.status, text2, modelId);
|
|
8961
9078
|
}
|
|
8962
9079
|
const parsed = SeedanceResponseSchema.safeParse(await res.json());
|
|
@@ -9016,7 +9133,12 @@ function seedanceJob(ctx, key, jobId, usage5) {
|
|
|
9016
9133
|
};
|
|
9017
9134
|
}
|
|
9018
9135
|
if (status === "failed" || status === "expired" || status === "cancelled") {
|
|
9019
|
-
throwArkError(
|
|
9136
|
+
throwArkError(
|
|
9137
|
+
void 0,
|
|
9138
|
+
seedanceErrorMessage(polled.error, status),
|
|
9139
|
+
modelId,
|
|
9140
|
+
seedanceErrorCode(polled.error)
|
|
9141
|
+
);
|
|
9020
9142
|
}
|
|
9021
9143
|
return void 0;
|
|
9022
9144
|
}, pollOpts);
|
|
@@ -10799,8 +10921,8 @@ function falMediaError(row2, message, code) {
|
|
|
10799
10921
|
retryable: false
|
|
10800
10922
|
});
|
|
10801
10923
|
}
|
|
10802
|
-
function refuseMedia(row2,
|
|
10803
|
-
const extra = Object.entries(
|
|
10924
|
+
function refuseMedia(row2, present3) {
|
|
10925
|
+
const extra = Object.entries(present3).find(([, set]) => set);
|
|
10804
10926
|
if (extra) throw falMediaError(row2, `does not take ${extra[0]}`, "unsupported_option");
|
|
10805
10927
|
}
|
|
10806
10928
|
function setIf(input, key, value) {
|
|
@@ -11664,8 +11786,13 @@ var MOTION_PRO = "fal-ai/kling-video/v3/pro/motion-control";
|
|
|
11664
11786
|
var MOTION_STANDARD = "fal-ai/kling-video/v3/standard/motion-control";
|
|
11665
11787
|
var O3_REFERENCE = "fal-ai/kling-video/o3/pro/reference-to-video";
|
|
11666
11788
|
var O3_EDIT = "fal-ai/kling-video/o3/pro/video-to-video/edit";
|
|
11667
|
-
var
|
|
11668
|
-
var
|
|
11789
|
+
var V4 = { generate: "kling/v4", edit: "kling/v4/video-to-video/edit", flash: false };
|
|
11790
|
+
var V4_FLASH = {
|
|
11791
|
+
generate: "kling/v4/flash",
|
|
11792
|
+
edit: "kling/v4/flash/video-to-video",
|
|
11793
|
+
flash: true
|
|
11794
|
+
};
|
|
11795
|
+
var V4_VARIANTS = [V4, V4_FLASH];
|
|
11669
11796
|
function motionControl(endpoint, req, ctx) {
|
|
11670
11797
|
const [character] = req.referenceImages ?? [];
|
|
11671
11798
|
const [motion] = req.referenceVideos ?? [];
|
|
@@ -11725,15 +11852,18 @@ var klingV3Family = {
|
|
|
11725
11852
|
return motionControl(id, req, ctx);
|
|
11726
11853
|
}
|
|
11727
11854
|
};
|
|
11728
|
-
function v4Generate(req, ctx) {
|
|
11855
|
+
function v4Generate(variant, req, ctx) {
|
|
11729
11856
|
const images = urls(req.referenceImages);
|
|
11730
11857
|
const videos = urls(req.referenceVideos);
|
|
11731
11858
|
const duration = wholeSeconds(req, ctx);
|
|
11732
11859
|
const resolution = resolutionOf(req, ctx);
|
|
11733
|
-
const
|
|
11860
|
+
const references = images.length + videos.length > 0;
|
|
11861
|
+
const input = { duration, resolution };
|
|
11862
|
+
if (variant.flash && !references && req.firstFrame) optionalPrompt(input, req);
|
|
11863
|
+
else input["prompt"] = req.prompt.trim();
|
|
11734
11864
|
let endpoint;
|
|
11735
|
-
if (
|
|
11736
|
-
endpoint = `${
|
|
11865
|
+
if (references) {
|
|
11866
|
+
endpoint = `${variant.generate}/reference-to-video`;
|
|
11737
11867
|
if (images.length > 0) input["image_urls"] = images;
|
|
11738
11868
|
if (videos.length > 0) input["video_urls"] = videos;
|
|
11739
11869
|
if (req.firstFrame) input["start_image_url"] = refToUrl(req.firstFrame);
|
|
@@ -11741,17 +11871,17 @@ function v4Generate(req, ctx) {
|
|
|
11741
11871
|
input["aspect_ratio"] = req.aspectRatio === "adaptive" ? "auto" : req.aspectRatio;
|
|
11742
11872
|
}
|
|
11743
11873
|
} else if (req.firstFrame) {
|
|
11744
|
-
endpoint = `${
|
|
11874
|
+
endpoint = `${variant.generate}/image-to-video`;
|
|
11745
11875
|
input["start_image_url"] = refToUrl(req.firstFrame);
|
|
11746
11876
|
if (req.lastFrame) input["end_image_url"] = refToUrl(req.lastFrame);
|
|
11747
11877
|
} else {
|
|
11748
|
-
endpoint = `${
|
|
11878
|
+
endpoint = `${variant.generate}/text-to-video`;
|
|
11749
11879
|
const ratio = concreteRatio(req);
|
|
11750
11880
|
if (ratio !== void 0) input["aspect_ratio"] = ratio;
|
|
11751
11881
|
}
|
|
11752
11882
|
return { endpoint, input, resolution, seconds: requestedSeconds(duration) };
|
|
11753
11883
|
}
|
|
11754
|
-
function v4Edit(req, ctx) {
|
|
11884
|
+
function v4Edit(variant, req, ctx) {
|
|
11755
11885
|
if (req.sourceVideo === void 0) throw refusal(ctx, "needs a source video", "missing_input");
|
|
11756
11886
|
const resolution = resolutionOf(req, ctx);
|
|
11757
11887
|
const input = {
|
|
@@ -11761,9 +11891,11 @@ function v4Edit(req, ctx) {
|
|
|
11761
11891
|
};
|
|
11762
11892
|
const images = urls(req.referenceImages);
|
|
11763
11893
|
if (images.length > 0) input["image_urls"] = images;
|
|
11894
|
+
const videos = urls(req.referenceVideos);
|
|
11895
|
+
if (variant.flash && videos.length > 0) input["video_urls"] = videos;
|
|
11764
11896
|
if (req.durationSeconds === void 0) {
|
|
11765
11897
|
return {
|
|
11766
|
-
endpoint:
|
|
11898
|
+
endpoint: variant.edit,
|
|
11767
11899
|
input,
|
|
11768
11900
|
resolution,
|
|
11769
11901
|
seconds: inputLengthSeconds(req, req.sourceVideo)
|
|
@@ -11771,13 +11903,18 @@ function v4Edit(req, ctx) {
|
|
|
11771
11903
|
}
|
|
11772
11904
|
const duration = wholeSeconds(req, ctx);
|
|
11773
11905
|
input["duration"] = duration;
|
|
11774
|
-
return { endpoint:
|
|
11906
|
+
return { endpoint: variant.edit, input, resolution, seconds: requestedSeconds(duration) };
|
|
11775
11907
|
}
|
|
11776
|
-
var KLING_V4_VIDEO_IDS =
|
|
11908
|
+
var KLING_V4_VIDEO_IDS = new Set(
|
|
11909
|
+
V4_VARIANTS.flatMap((v) => [v.generate, v.edit])
|
|
11910
|
+
);
|
|
11777
11911
|
var klingV4Family = {
|
|
11778
11912
|
ids: KLING_V4_VIDEO_IDS,
|
|
11779
11913
|
plan(req, ctx) {
|
|
11780
|
-
|
|
11914
|
+
const id = ctx.model.providerModelId.toLowerCase();
|
|
11915
|
+
const edit2 = V4_VARIANTS.find((v) => v.edit === id);
|
|
11916
|
+
if (edit2 !== void 0) return v4Edit(edit2, req, ctx);
|
|
11917
|
+
return v4Generate(V4_VARIANTS.find((v) => v.generate === id) ?? V4, req, ctx);
|
|
11781
11918
|
}
|
|
11782
11919
|
};
|
|
11783
11920
|
|
|
@@ -11852,6 +11989,8 @@ var O3_REFERENCE2 = "fal-ai/kling-video/o3/pro/reference-to-video";
|
|
|
11852
11989
|
var O3_EDIT2 = "fal-ai/kling-video/o3/pro/video-to-video/edit";
|
|
11853
11990
|
var KLING_V4 = "kling/v4";
|
|
11854
11991
|
var KLING_V4_EDIT = "kling/v4/video-to-video/edit";
|
|
11992
|
+
var KLING_V4_FLASH = "kling/v4/flash";
|
|
11993
|
+
var KLING_V4_FLASH_EDIT = "kling/v4/flash/video-to-video";
|
|
11855
11994
|
var H3_TURBO = "minimax/h3-max-turbo";
|
|
11856
11995
|
var H3_RECAST = "minimax/h3-max/recast";
|
|
11857
11996
|
var H3_RELIGHT = "minimax/h3-max/relight";
|
|
@@ -11933,6 +12072,11 @@ function klingV4(req) {
|
|
|
11933
12072
|
}
|
|
11934
12073
|
return needsPrompt(req);
|
|
11935
12074
|
}
|
|
12075
|
+
function klingV4Flash(req) {
|
|
12076
|
+
if (req.lastFrame !== void 0) return unsupported2("takes no end frame");
|
|
12077
|
+
if (!hasReferences(req) && req.firstFrame !== void 0) return void 0;
|
|
12078
|
+
return klingV4(req);
|
|
12079
|
+
}
|
|
11936
12080
|
function h3Turbo(req) {
|
|
11937
12081
|
if (count(req.referenceImages) + count(req.referenceVideos) > 0) {
|
|
11938
12082
|
return unsupported2("takes no reference images or videos");
|
|
@@ -12013,6 +12157,8 @@ var RULES = /* @__PURE__ */ new Map([
|
|
|
12013
12157
|
[O3_EDIT2, (req) => needsSource(req) ?? needsPrompt(req)],
|
|
12014
12158
|
[KLING_V4, klingV4],
|
|
12015
12159
|
[KLING_V4_EDIT, (req) => needsSource(req) ?? needsPrompt(req)],
|
|
12160
|
+
[KLING_V4_FLASH, klingV4Flash],
|
|
12161
|
+
[KLING_V4_FLASH_EDIT, (req) => needsSource(req) ?? needsPrompt(req)],
|
|
12016
12162
|
[H3_TURBO, h3Turbo],
|
|
12017
12163
|
[MINIMAX_H3_INSERT, h3Insert],
|
|
12018
12164
|
[H3_RECAST, h3Recast],
|
|
@@ -12062,7 +12208,7 @@ function applyFalVideoRules(model, req, callerRatio, warnings) {
|
|
|
12062
12208
|
warnings.push(viduSilentWarning(model));
|
|
12063
12209
|
req.generateAudio = false;
|
|
12064
12210
|
}
|
|
12065
|
-
if (id === KLING_V4 && req.firstFrame !== void 0 && !hasReferences(req)) {
|
|
12211
|
+
if ((id === KLING_V4 || id === KLING_V4_FLASH) && req.firstFrame !== void 0 && !hasReferences(req)) {
|
|
12066
12212
|
if (req.aspectRatio !== void 0) {
|
|
12067
12213
|
if (callerRatio !== void 0) {
|
|
12068
12214
|
warnings.push({
|
|
@@ -17953,16 +18099,16 @@ function resolveVideoOptions(model, request, onUnsupported = "error") {
|
|
|
17953
18099
|
}
|
|
17954
18100
|
function checkVideoRequired(model, req) {
|
|
17955
18101
|
const c = model.constraints;
|
|
17956
|
-
const
|
|
18102
|
+
const present3 = (input) => input === "references" ? compact(req.referenceImages).length + compact(req.referenceVideos).length + compact(req.referenceAudios).length > 0 : input !== "sourceAudio" && req[input] !== void 0;
|
|
17957
18103
|
for (const input of c.requires ?? []) {
|
|
17958
|
-
if (!
|
|
18104
|
+
if (!present3(input))
|
|
17959
18105
|
throw new ModelKitError({
|
|
17960
18106
|
code: "missing_input",
|
|
17961
18107
|
message: `${model.id} requires ${input}`,
|
|
17962
18108
|
model: model.id
|
|
17963
18109
|
});
|
|
17964
18110
|
}
|
|
17965
|
-
if (c.requiresOneOf && !c.requiresOneOf.some(
|
|
18111
|
+
if (c.requiresOneOf && !c.requiresOneOf.some(present3)) {
|
|
17966
18112
|
throw new ModelKitError({
|
|
17967
18113
|
code: "missing_input",
|
|
17968
18114
|
message: `${model.id} requires one of: ${c.requiresOneOf.join(", ")}`,
|
|
@@ -18010,8 +18156,8 @@ function resolveAudioOptions(model, request, onUnsupported = "error") {
|
|
|
18010
18156
|
if (references.length > 0) merged.references = references;
|
|
18011
18157
|
else delete merged.references;
|
|
18012
18158
|
for (const input of c.requires ?? []) {
|
|
18013
|
-
const
|
|
18014
|
-
if (!
|
|
18159
|
+
const present3 = input === "references" ? references.length > 0 : input === "sourceVideo" && merged.sourceVideo !== void 0;
|
|
18160
|
+
if (!present3)
|
|
18015
18161
|
throw new ModelKitError({
|
|
18016
18162
|
code: "missing_input",
|
|
18017
18163
|
message: `${model.id} requires ${input}`,
|
|
@@ -18065,12 +18211,12 @@ function unsupported3(model, message) {
|
|
|
18065
18211
|
model: model.id
|
|
18066
18212
|
});
|
|
18067
18213
|
}
|
|
18068
|
-
function checkRequired(model,
|
|
18214
|
+
function checkRequired(model, present3) {
|
|
18069
18215
|
for (const input of model.constraints.requires ?? []) {
|
|
18070
|
-
if (
|
|
18216
|
+
if (present3[input] !== true) throw missing2(model, input);
|
|
18071
18217
|
}
|
|
18072
18218
|
const oneOf = model.constraints.requiresOneOf;
|
|
18073
|
-
if (oneOf !== void 0 && !oneOf.some((input) =>
|
|
18219
|
+
if (oneOf !== void 0 && !oneOf.some((input) => present3[input] === true)) {
|
|
18074
18220
|
throw missing2(model, `one of ${oneOf.join(", ")}`);
|
|
18075
18221
|
}
|
|
18076
18222
|
}
|
|
@@ -18402,6 +18548,39 @@ function resolveKeys(config, names, tier = "pro") {
|
|
|
18402
18548
|
}
|
|
18403
18549
|
return config.keys.pro;
|
|
18404
18550
|
}
|
|
18551
|
+
function present2(value) {
|
|
18552
|
+
return value !== void 0 && value.trim() !== "";
|
|
18553
|
+
}
|
|
18554
|
+
function overrideHolds(override, name) {
|
|
18555
|
+
if (!override || name === "aws" || !present2(override[name])) return false;
|
|
18556
|
+
return name !== "higgsfield" || present2(override.higgsfieldSecret);
|
|
18557
|
+
}
|
|
18558
|
+
function overrideCovers(override, names) {
|
|
18559
|
+
return names.filter((name) => overrideHolds(override, name));
|
|
18560
|
+
}
|
|
18561
|
+
function resolveCallKeys(config, names, tier = "pro", override) {
|
|
18562
|
+
const covered = overrideCovers(override, names);
|
|
18563
|
+
const tierNames = names.filter((name) => !covered.includes(name));
|
|
18564
|
+
const base = resolveKeys(config, tierNames, tier);
|
|
18565
|
+
if (!override || covered.length === 0)
|
|
18566
|
+
return { keys: base, source: tierSource(config, tierNames, tier), tierNames };
|
|
18567
|
+
const keys = { ...base };
|
|
18568
|
+
for (const name of covered) {
|
|
18569
|
+
const value = name === "aws" ? void 0 : override[name];
|
|
18570
|
+
if (name === "aws" || !present2(value)) continue;
|
|
18571
|
+
keys[name] = value;
|
|
18572
|
+
if (name === "higgsfield" && present2(override.higgsfieldSecret))
|
|
18573
|
+
keys.higgsfieldSecret = override.higgsfieldSecret;
|
|
18574
|
+
}
|
|
18575
|
+
return { keys, source: "byok", tierNames };
|
|
18576
|
+
}
|
|
18577
|
+
function keySourceField(source2) {
|
|
18578
|
+
return source2 === void 0 ? {} : { keySource: source2 };
|
|
18579
|
+
}
|
|
18580
|
+
function tierSource(config, names, tier) {
|
|
18581
|
+
if (tier === "enterprise") return hasAll(config.keys.enterprise, names) ? "enterprise" : "pro";
|
|
18582
|
+
return tier;
|
|
18583
|
+
}
|
|
18405
18584
|
function keysFromEnv(env, prefix = "") {
|
|
18406
18585
|
const pick2 = (...names) => names.map((n) => env[`${prefix}${n}`]).find((v) => v && v.length > 0);
|
|
18407
18586
|
const keys = {};
|
|
@@ -18426,10 +18605,10 @@ function keysFromEnv(env, prefix = "") {
|
|
|
18426
18605
|
set("fish", pick2("FISH_AUDIO_API_KEY"));
|
|
18427
18606
|
set("elevenlabs", pick2("ELEVENLABS_API_KEY"));
|
|
18428
18607
|
set("cloudflare", pick2("CF_AIG_TOKEN"));
|
|
18429
|
-
const
|
|
18608
|
+
const bearer2 = pick2("AWS_BEARER_TOKEN_BEDROCK");
|
|
18430
18609
|
const accessKeyId = pick2("AWS_ACCESS_KEY_ID");
|
|
18431
18610
|
const secretAccessKey = pick2("AWS_SECRET_ACCESS_KEY");
|
|
18432
|
-
if (
|
|
18611
|
+
if (bearer2 || accessKeyId && secretAccessKey) {
|
|
18433
18612
|
const aws = {
|
|
18434
18613
|
region: env[`${prefix}BEDROCK_REGION`] || "us-east-1"
|
|
18435
18614
|
};
|
|
@@ -18439,12 +18618,101 @@ function keysFromEnv(env, prefix = "") {
|
|
|
18439
18618
|
}
|
|
18440
18619
|
const sessionToken = pick2("AWS_SESSION_TOKEN");
|
|
18441
18620
|
if (sessionToken) aws.sessionToken = sessionToken;
|
|
18442
|
-
if (
|
|
18621
|
+
if (bearer2) aws.apiKey = bearer2;
|
|
18443
18622
|
keys.aws = aws;
|
|
18444
18623
|
}
|
|
18445
18624
|
return keys;
|
|
18446
18625
|
}
|
|
18447
18626
|
|
|
18627
|
+
// src/byok.ts
|
|
18628
|
+
var API_KEY = { id: "apiKey", label: "API key", required: true, secret: true };
|
|
18629
|
+
var BYOK_PROVIDERS = {
|
|
18630
|
+
ark: {
|
|
18631
|
+
id: "ark",
|
|
18632
|
+
label: "BytePlus ModelArk",
|
|
18633
|
+
keyName: "ark",
|
|
18634
|
+
fields: [
|
|
18635
|
+
API_KEY,
|
|
18636
|
+
{ id: "accessKeyId", label: "Access key ID", required: false, secret: true },
|
|
18637
|
+
{ id: "secretAccessKey", label: "Secret access key", required: false, secret: true },
|
|
18638
|
+
{ id: "assetGroupId", label: "Asset group ID", required: false, secret: false }
|
|
18639
|
+
]
|
|
18640
|
+
},
|
|
18641
|
+
fal: { id: "fal", label: "fal", keyName: "fal", fields: [API_KEY] },
|
|
18642
|
+
replicate: { id: "replicate", label: "Replicate", keyName: "replicate", fields: [API_KEY] },
|
|
18643
|
+
openai: { id: "openai", label: "OpenAI", keyName: "openai", fields: [API_KEY] },
|
|
18644
|
+
google: { id: "google", label: "Google AI Studio", keyName: "google", fields: [API_KEY] }
|
|
18645
|
+
};
|
|
18646
|
+
var BY_KEY_NAME = new Map(
|
|
18647
|
+
Object.values(BYOK_PROVIDERS).map((provider) => [provider.keyName, provider.id])
|
|
18648
|
+
);
|
|
18649
|
+
function byokProviderForModel(model) {
|
|
18650
|
+
const names = model.adapter === "ai-sdk-text" ? [textKeyName(model)] : ADAPTER_KEYS[model.adapter];
|
|
18651
|
+
const [name, ...rest] = names;
|
|
18652
|
+
if (name === void 0 || rest.length > 0) return null;
|
|
18653
|
+
return BY_KEY_NAME.get(name) ?? null;
|
|
18654
|
+
}
|
|
18655
|
+
function modelsPoweredBy(provider, catalog) {
|
|
18656
|
+
return [...catalog].filter((model) => byokProviderForModel(model) === provider);
|
|
18657
|
+
}
|
|
18658
|
+
function keyOverrideFor(secrets) {
|
|
18659
|
+
const override = {};
|
|
18660
|
+
for (const provider of Object.values(BYOK_PROVIDERS)) {
|
|
18661
|
+
const apiKey = secrets[provider.id]?.apiKey?.trim();
|
|
18662
|
+
if (apiKey) override[provider.keyName] = apiKey;
|
|
18663
|
+
}
|
|
18664
|
+
return override;
|
|
18665
|
+
}
|
|
18666
|
+
var bearer = (apiKey) => ({ Authorization: `Bearer ${apiKey}` });
|
|
18667
|
+
var PROBES = {
|
|
18668
|
+
ark: {
|
|
18669
|
+
url: (endpoints) => `${endpoints.ark ?? ARK_BASE_URL}/contents/generations/tasks?page_num=1&page_size=1`,
|
|
18670
|
+
headers: bearer
|
|
18671
|
+
},
|
|
18672
|
+
fal: {
|
|
18673
|
+
url: () => "https://api.fal.ai/v1/assets/collections?limit=1",
|
|
18674
|
+
headers: (apiKey) => ({ Authorization: `Key ${apiKey}` }),
|
|
18675
|
+
unverifiableStatus: 403
|
|
18676
|
+
},
|
|
18677
|
+
replicate: {
|
|
18678
|
+
url: () => "https://api.replicate.com/v1/account",
|
|
18679
|
+
headers: bearer
|
|
18680
|
+
},
|
|
18681
|
+
openai: {
|
|
18682
|
+
url: () => "https://api.openai.com/v1/models",
|
|
18683
|
+
headers: bearer,
|
|
18684
|
+
unverifiableStatus: 403
|
|
18685
|
+
},
|
|
18686
|
+
google: {
|
|
18687
|
+
url: () => "https://generativelanguage.googleapis.com/v1beta/models?pageSize=1",
|
|
18688
|
+
headers: (apiKey) => ({ "x-goog-api-key": apiKey }),
|
|
18689
|
+
unverifiableStatus: 403
|
|
18690
|
+
}
|
|
18691
|
+
};
|
|
18692
|
+
async function testProviderKey(provider, secret, opts = {}) {
|
|
18693
|
+
const apiKey = secret.apiKey?.trim();
|
|
18694
|
+
if (!apiKey) return { ok: false, reason: "invalid" };
|
|
18695
|
+
const probe = PROBES[provider];
|
|
18696
|
+
const fetchImpl = opts.fetch ?? globalThis.fetch;
|
|
18697
|
+
let res;
|
|
18698
|
+
try {
|
|
18699
|
+
res = await fetchImpl(probe.url(opts.endpoints ?? {}), {
|
|
18700
|
+
method: "GET",
|
|
18701
|
+
headers: probe.headers(apiKey),
|
|
18702
|
+
...opts.signal !== void 0 && { signal: opts.signal }
|
|
18703
|
+
});
|
|
18704
|
+
} catch (cause) {
|
|
18705
|
+
if (opts.signal?.aborted) throw cause;
|
|
18706
|
+
return { ok: false, reason: "unreachable" };
|
|
18707
|
+
}
|
|
18708
|
+
await res.body?.cancel().catch(() => void 0);
|
|
18709
|
+
if (res.ok) return { ok: true, credit: "unknown" };
|
|
18710
|
+
if (res.status === probe.unverifiableStatus) return { ok: false, reason: "unverifiable" };
|
|
18711
|
+
if (res.status === 400 || res.status === 401) return { ok: false, reason: "invalid" };
|
|
18712
|
+
if (res.status === 402 || res.status === 403) return { ok: false, reason: "denied" };
|
|
18713
|
+
return { ok: false, reason: "unreachable" };
|
|
18714
|
+
}
|
|
18715
|
+
|
|
18448
18716
|
// src/text.ts
|
|
18449
18717
|
import "ai";
|
|
18450
18718
|
import { z as z33 } from "zod";
|
|
@@ -18535,12 +18803,10 @@ function createTextSteps(initial, resolve, callerProviderOptions) {
|
|
|
18535
18803
|
const grouped = /* @__PURE__ */ new Map();
|
|
18536
18804
|
for (const [index, step] of results.entries()) {
|
|
18537
18805
|
const target = steps.get(index) ?? initial;
|
|
18538
|
-
const event =
|
|
18539
|
-
target.entry,
|
|
18540
|
-
target.
|
|
18541
|
-
|
|
18542
|
-
step.providerMetadata
|
|
18543
|
-
);
|
|
18806
|
+
const event = {
|
|
18807
|
+
...textUsageEvent(target.entry, target.provider, step.usage, step.providerMetadata),
|
|
18808
|
+
...keySourceField(target.keySource)
|
|
18809
|
+
};
|
|
18544
18810
|
const previous = grouped.get(target.entry.id);
|
|
18545
18811
|
if (previous) {
|
|
18546
18812
|
previous.quantity += event.quantity;
|
|
@@ -18598,8 +18864,9 @@ function preferredAnthropicTransport(model, routing) {
|
|
|
18598
18864
|
if (isAnthropicTransport(row2)) return row2;
|
|
18599
18865
|
return routing.anthropic ?? "direct";
|
|
18600
18866
|
}
|
|
18601
|
-
function resolveAnthropicTransport(model, config, tier, warn) {
|
|
18867
|
+
function resolveAnthropicTransport(model, config, tier, warn, keyOverride) {
|
|
18602
18868
|
if (model.adapter !== "ai-sdk-text" || textProviderOf(model) !== "anthropic") return void 0;
|
|
18869
|
+
if (overrideCovers(keyOverride, ["anthropic"]).length > 0) return "direct";
|
|
18603
18870
|
const rowValue = model.providerOptions?.["modelkit"]?.["transport"];
|
|
18604
18871
|
if (rowValue !== void 0 && !isAnthropicTransport(rowValue))
|
|
18605
18872
|
warn(
|
|
@@ -18688,8 +18955,7 @@ function providerJobMethods(kind, deps) {
|
|
|
18688
18955
|
const { methods, adapter, ctx } = target(sub.model, rest);
|
|
18689
18956
|
if (!methods.result) throw missing3(adapter, ctx.model);
|
|
18690
18957
|
const result = await methods.result(sub, ctx, webhook);
|
|
18691
|
-
const
|
|
18692
|
-
const usage5 = sub.submittedAt === void 0 ? base : deps.timed(base, sub.submittedAt);
|
|
18958
|
+
const usage5 = deps.rowUsage(ctx, result.usage, sub.submittedAt);
|
|
18693
18959
|
await deps.emit(usage5);
|
|
18694
18960
|
return { ...result, usage: usage5 };
|
|
18695
18961
|
},
|
|
@@ -18817,7 +19083,7 @@ function audioNodeMethods(deps) {
|
|
|
18817
19083
|
if (!adapter.generateSpeech) throw noMethod(adapter, model, "generate speech");
|
|
18818
19084
|
const startedAt = Date.now();
|
|
18819
19085
|
const result = await adapter.generateSpeech(resolved, ctx);
|
|
18820
|
-
return { ...result, usage: await deps.settle(
|
|
19086
|
+
return { ...result, usage: await deps.settle(ctx, result.usage, startedAt) };
|
|
18821
19087
|
},
|
|
18822
19088
|
async changeVoice(req, call = {}) {
|
|
18823
19089
|
mediaRetryOptions(call);
|
|
@@ -18832,7 +19098,7 @@ function audioNodeMethods(deps) {
|
|
|
18832
19098
|
if (!adapter.changeVoice) throw noMethod(adapter, model, "change a voice");
|
|
18833
19099
|
const startedAt = Date.now();
|
|
18834
19100
|
const result = await adapter.changeVoice(resolved, ctx);
|
|
18835
|
-
return { ...result, usage: await deps.settle(
|
|
19101
|
+
return { ...result, usage: await deps.settle(ctx, result.usage, startedAt) };
|
|
18836
19102
|
},
|
|
18837
19103
|
async lipsync(req, call = {}) {
|
|
18838
19104
|
mediaRetryOptions(call);
|
|
@@ -18847,7 +19113,7 @@ function audioNodeMethods(deps) {
|
|
|
18847
19113
|
if (!adapter.lipsync) throw noMethod(adapter, model, "lipsync");
|
|
18848
19114
|
const startedAt = Date.now();
|
|
18849
19115
|
const job = deps.wrapVideoJob(
|
|
18850
|
-
|
|
19116
|
+
ctx,
|
|
18851
19117
|
await adapter.lipsync(resolved, ctx),
|
|
18852
19118
|
warnings,
|
|
18853
19119
|
startedAt
|
|
@@ -18872,7 +19138,7 @@ function audioNodeMethods(deps) {
|
|
|
18872
19138
|
if (!adapter.transcribe) throw noMethod(adapter, model, "transcribe");
|
|
18873
19139
|
const startedAt = Date.now();
|
|
18874
19140
|
const result = await adapter.transcribe(resolved, ctx);
|
|
18875
|
-
return { ...result, usage: await deps.settle(
|
|
19141
|
+
return { ...result, usage: await deps.settle(ctx, result.usage, startedAt) };
|
|
18876
19142
|
},
|
|
18877
19143
|
async createVoice(req, call = {}) {
|
|
18878
19144
|
mediaRetryOptions(call);
|
|
@@ -18883,7 +19149,7 @@ function audioNodeMethods(deps) {
|
|
|
18883
19149
|
if (!adapter.createVoice) throw noMethod(adapter, model, "clone a voice");
|
|
18884
19150
|
const startedAt = Date.now();
|
|
18885
19151
|
const result = await adapter.createVoice(resolved, ctx);
|
|
18886
|
-
return { ...result, usage: await deps.settle(
|
|
19152
|
+
return { ...result, usage: await deps.settle(ctx, result.usage, startedAt) };
|
|
18887
19153
|
},
|
|
18888
19154
|
async designVoice(req, call = {}) {
|
|
18889
19155
|
mediaRetryOptions(call);
|
|
@@ -18898,7 +19164,7 @@ function audioNodeMethods(deps) {
|
|
|
18898
19164
|
if (!adapter.designVoice) throw noMethod(adapter, model, "design a voice");
|
|
18899
19165
|
const startedAt = Date.now();
|
|
18900
19166
|
const result = await adapter.designVoice(resolved, ctx);
|
|
18901
|
-
return { ...result, usage: await deps.settle(
|
|
19167
|
+
return { ...result, usage: await deps.settle(ctx, result.usage, startedAt) };
|
|
18902
19168
|
},
|
|
18903
19169
|
async deleteVoice(req, call = {}) {
|
|
18904
19170
|
mediaRetryOptions(call);
|
|
@@ -18934,11 +19200,11 @@ function audioNodeMethods(deps) {
|
|
|
18934
19200
|
const { audio, usage: usage5, raw } = outcome.result;
|
|
18935
19201
|
return {
|
|
18936
19202
|
audio,
|
|
18937
|
-
usage: await deps.settle(
|
|
19203
|
+
usage: await deps.settle(ctx, usage5, startedAt),
|
|
18938
19204
|
...raw !== void 0 && { raw }
|
|
18939
19205
|
};
|
|
18940
19206
|
}
|
|
18941
|
-
const job = deps.wrapVideoJob(
|
|
19207
|
+
const job = deps.wrapVideoJob(ctx, outcome.job, warnings, startedAt);
|
|
18942
19208
|
const out = await job.result(waitOptions(call, ctx.signal));
|
|
18943
19209
|
return {
|
|
18944
19210
|
video: out.video,
|
|
@@ -18950,14 +19216,28 @@ function audioNodeMethods(deps) {
|
|
|
18950
19216
|
}
|
|
18951
19217
|
|
|
18952
19218
|
// src/client.ts
|
|
18953
|
-
function
|
|
18954
|
-
const routing = config.routing ?? {};
|
|
18955
|
-
if (tier !== "sloosh") return routing;
|
|
19219
|
+
function directRouting(routing) {
|
|
18956
19220
|
const direct = { ...routing };
|
|
18957
19221
|
if (direct.text === "cloudflare") direct.text = "direct";
|
|
18958
19222
|
if (direct.fal === "cloudflare-passthrough") direct.fal = "direct";
|
|
18959
19223
|
return direct;
|
|
18960
19224
|
}
|
|
19225
|
+
function routingFor(config, model, call, transport) {
|
|
19226
|
+
const routing = config.routing ?? {};
|
|
19227
|
+
const direct = directRouting(routing);
|
|
19228
|
+
if (call.tier === "sloosh") return direct;
|
|
19229
|
+
const byok = overrideCovers(call.keyOverride, requiredKeys(model, direct, transport));
|
|
19230
|
+
return byok.length > 0 ? direct : routing;
|
|
19231
|
+
}
|
|
19232
|
+
function byokKeyMissing(provider, model) {
|
|
19233
|
+
return new ModelKitError({
|
|
19234
|
+
code: "byok_key_missing",
|
|
19235
|
+
message: `${model} needs the customer's ${provider} key and the call has none`,
|
|
19236
|
+
provider,
|
|
19237
|
+
model,
|
|
19238
|
+
retryable: false
|
|
19239
|
+
});
|
|
19240
|
+
}
|
|
18961
19241
|
function requiredKeys(model, routing, transport) {
|
|
18962
19242
|
if (model.adapter === "fal" && routing.fal === "cloudflare-passthrough") return ["cloudflare"];
|
|
18963
19243
|
if (model.adapter === "ai-sdk-text") {
|
|
@@ -19059,21 +19339,35 @@ function createModelKit(config, adapters) {
|
|
|
19059
19339
|
message: `No adapter registered for ${model.adapter}`,
|
|
19060
19340
|
model: model.id
|
|
19061
19341
|
});
|
|
19062
|
-
const transport = resolveAnthropicTransport(
|
|
19063
|
-
|
|
19342
|
+
const transport = resolveAnthropicTransport(
|
|
19343
|
+
model,
|
|
19344
|
+
config,
|
|
19345
|
+
call.tier,
|
|
19346
|
+
warnOnce,
|
|
19347
|
+
call.keyOverride
|
|
19348
|
+
);
|
|
19349
|
+
const routing = routingFor(config, model, call, transport);
|
|
19064
19350
|
const names = requiredKeys(model, routing, transport);
|
|
19351
|
+
if (call.requireOverride && overrideCovers(call.keyOverride, names).length === 0)
|
|
19352
|
+
throw byokKeyMissing(names[0] ?? model.adapter, model.id);
|
|
19353
|
+
const { keys, source: source2, tierNames } = resolveCallKeys(
|
|
19354
|
+
config,
|
|
19355
|
+
names,
|
|
19356
|
+
call.tier,
|
|
19357
|
+
call.keyOverride
|
|
19358
|
+
);
|
|
19065
19359
|
if (call.tier === "enterprise") {
|
|
19066
|
-
const missing3 = missingEnterpriseKeys(config,
|
|
19360
|
+
const missing3 = missingEnterpriseKeys(config, tierNames);
|
|
19067
19361
|
if (missing3.length > 0)
|
|
19068
19362
|
logger.warn(
|
|
19069
19363
|
`[modelkit] enterprise keys incomplete for ${model.adapter}; using pro`,
|
|
19070
19364
|
{ missing: missing3 }
|
|
19071
19365
|
);
|
|
19072
19366
|
}
|
|
19073
|
-
const keys = resolveKeys(config, names, call.tier);
|
|
19074
19367
|
const ctx = {
|
|
19075
19368
|
model,
|
|
19076
19369
|
keys,
|
|
19370
|
+
keySource: source2,
|
|
19077
19371
|
routing,
|
|
19078
19372
|
fetch: fetchImpl,
|
|
19079
19373
|
logger,
|
|
@@ -19088,7 +19382,7 @@ function createModelKit(config, adapters) {
|
|
|
19088
19382
|
const jobDeps = {
|
|
19089
19383
|
catalog,
|
|
19090
19384
|
context: (model, call) => context(model, call, []),
|
|
19091
|
-
|
|
19385
|
+
rowUsage,
|
|
19092
19386
|
emit
|
|
19093
19387
|
};
|
|
19094
19388
|
const videoToolJobs = providerJobMethods(VIDEO_TOOL_JOB, jobDeps);
|
|
@@ -19097,6 +19391,15 @@ function createModelKit(config, adapters) {
|
|
|
19097
19391
|
if (event.latencyMs !== void 0) return event;
|
|
19098
19392
|
return { ...event, latencyMs: Math.max(0, Date.now() - startedAt) };
|
|
19099
19393
|
}
|
|
19394
|
+
function rowUsage(ctx, event, startedAt) {
|
|
19395
|
+
const stamped = {
|
|
19396
|
+
...event,
|
|
19397
|
+
model: ctx.model.id,
|
|
19398
|
+
unit: ctx.model.usage.unit,
|
|
19399
|
+
...keySourceField(ctx.keySource)
|
|
19400
|
+
};
|
|
19401
|
+
return startedAt === void 0 ? stamped : timed(stamped, startedAt);
|
|
19402
|
+
}
|
|
19100
19403
|
function textTimed(event, stepCount, startedAt) {
|
|
19101
19404
|
return stepCount === 1 ? timed(event, startedAt) : event;
|
|
19102
19405
|
}
|
|
@@ -19113,6 +19416,7 @@ function createModelKit(config, adapters) {
|
|
|
19113
19416
|
}
|
|
19114
19417
|
function textTarget(model, call) {
|
|
19115
19418
|
if (!isCatalogId(model)) {
|
|
19419
|
+
if (call.requireOverride) throw byokKeyMissing(model.provider, model.modelId);
|
|
19116
19420
|
return {
|
|
19117
19421
|
entry: passthroughTextModel(model),
|
|
19118
19422
|
languageModel: model,
|
|
@@ -19136,7 +19440,8 @@ function createModelKit(config, adapters) {
|
|
|
19136
19440
|
return {
|
|
19137
19441
|
entry,
|
|
19138
19442
|
languageModel: adapter.languageModel(ctx),
|
|
19139
|
-
provider: textUsageProvider(entry, ctx.anthropicTransport)
|
|
19443
|
+
provider: textUsageProvider(entry, ctx.anthropicTransport),
|
|
19444
|
+
...keySourceField(ctx.keySource)
|
|
19140
19445
|
};
|
|
19141
19446
|
}
|
|
19142
19447
|
async function passthroughImage(req, call) {
|
|
@@ -19308,7 +19613,7 @@ function createModelKit(config, adapters) {
|
|
|
19308
19613
|
result: () => done
|
|
19309
19614
|
};
|
|
19310
19615
|
}
|
|
19311
|
-
function wrapVideoJob(
|
|
19616
|
+
function wrapVideoJob(ctx, job, warnings, startedAt) {
|
|
19312
19617
|
const result = job.result.bind(job);
|
|
19313
19618
|
let settled;
|
|
19314
19619
|
return {
|
|
@@ -19318,10 +19623,7 @@ function createModelKit(config, adapters) {
|
|
|
19318
19623
|
if (settled) return settled;
|
|
19319
19624
|
const pending = result(opts).then(
|
|
19320
19625
|
async (out) => {
|
|
19321
|
-
const usage5 =
|
|
19322
|
-
{ ...out.usage, model: model.id, unit: model.usage.unit },
|
|
19323
|
-
startedAt
|
|
19324
|
-
);
|
|
19626
|
+
const usage5 = rowUsage(ctx, out.usage, startedAt);
|
|
19325
19627
|
await emit(usage5);
|
|
19326
19628
|
return { ...out, usage: usage5 };
|
|
19327
19629
|
},
|
|
@@ -19342,8 +19644,8 @@ function createModelKit(config, adapters) {
|
|
|
19342
19644
|
const signal = combineSignals(abortSignal, call.signal);
|
|
19343
19645
|
return context(model, signal ? { ...call, signal } : call, warnings);
|
|
19344
19646
|
},
|
|
19345
|
-
async settle(
|
|
19346
|
-
const usage5 =
|
|
19647
|
+
async settle(ctx, event, startedAt) {
|
|
19648
|
+
const usage5 = rowUsage(ctx, event, startedAt);
|
|
19347
19649
|
await emit(usage5);
|
|
19348
19650
|
return usage5;
|
|
19349
19651
|
},
|
|
@@ -19356,6 +19658,8 @@ function createModelKit(config, adapters) {
|
|
|
19356
19658
|
async generateImage(req, call = {}) {
|
|
19357
19659
|
mediaRetryOptions(call);
|
|
19358
19660
|
if (!isCatalogId(req.model)) {
|
|
19661
|
+
if (call.requireOverride)
|
|
19662
|
+
throw byokKeyMissing(req.model.provider, req.model.modelId);
|
|
19359
19663
|
return passthroughImage({ ...req, model: req.model }, call);
|
|
19360
19664
|
}
|
|
19361
19665
|
const model = catalog.require(req.model);
|
|
@@ -19381,10 +19685,7 @@ function createModelKit(config, adapters) {
|
|
|
19381
19685
|
});
|
|
19382
19686
|
const startedAt = Date.now();
|
|
19383
19687
|
const result = await adapter.generateImage(request, ctx);
|
|
19384
|
-
const usage5 =
|
|
19385
|
-
{ ...result.usage, model: model.id, unit: model.usage.unit },
|
|
19386
|
-
startedAt
|
|
19387
|
-
);
|
|
19688
|
+
const usage5 = rowUsage(ctx, result.usage, startedAt);
|
|
19388
19689
|
await emit(usage5);
|
|
19389
19690
|
const images = result.images.map(toGeneratedImage);
|
|
19390
19691
|
const first = images[0];
|
|
@@ -19399,6 +19700,8 @@ function createModelKit(config, adapters) {
|
|
|
19399
19700
|
async generateVideo(req, call = {}) {
|
|
19400
19701
|
mediaRetryOptions(call);
|
|
19401
19702
|
if (!isCatalogId(req.model)) {
|
|
19703
|
+
if (call.requireOverride)
|
|
19704
|
+
throw byokKeyMissing(req.model.provider, req.model.modelId);
|
|
19402
19705
|
return passthroughVideo({ ...req, model: req.model }, call);
|
|
19403
19706
|
}
|
|
19404
19707
|
const model = catalog.require(req.model);
|
|
@@ -19424,7 +19727,7 @@ function createModelKit(config, adapters) {
|
|
|
19424
19727
|
});
|
|
19425
19728
|
const startedAt = Date.now();
|
|
19426
19729
|
const job = await adapter.generateVideo(request, ctx);
|
|
19427
|
-
return wrapVideoJob(
|
|
19730
|
+
return wrapVideoJob(ctx, job, warnings, startedAt);
|
|
19428
19731
|
},
|
|
19429
19732
|
async resumeVideo(req, jobId, call = {}) {
|
|
19430
19733
|
mediaRetryOptions(call);
|
|
@@ -19469,7 +19772,7 @@ function createModelKit(config, adapters) {
|
|
|
19469
19772
|
});
|
|
19470
19773
|
const startedAt = Date.now();
|
|
19471
19774
|
const job = adapter.resumeVideo(request, jobId, ctx);
|
|
19472
|
-
return wrapVideoJob(
|
|
19775
|
+
return wrapVideoJob(ctx, { ...job, jobId }, warnings, startedAt);
|
|
19473
19776
|
},
|
|
19474
19777
|
async generateAudio(req, call = {}) {
|
|
19475
19778
|
mediaRetryOptions(call);
|
|
@@ -19493,10 +19796,7 @@ function createModelKit(config, adapters) {
|
|
|
19493
19796
|
}
|
|
19494
19797
|
const startedAt = Date.now();
|
|
19495
19798
|
const result = await adapter.generateAudio(resolved, ctx);
|
|
19496
|
-
const usage5 =
|
|
19497
|
-
{ ...result.usage, model: model.id, unit: model.usage.unit },
|
|
19498
|
-
startedAt
|
|
19499
|
-
);
|
|
19799
|
+
const usage5 = rowUsage(ctx, result.usage, startedAt);
|
|
19500
19800
|
await emit(usage5);
|
|
19501
19801
|
return { ...result, usage: usage5 };
|
|
19502
19802
|
},
|
|
@@ -19509,7 +19809,7 @@ function createModelKit(config, adapters) {
|
|
|
19509
19809
|
return "url" in ref ? { type: "file", data: ref.url, mediaType } : { type: "file", data: ref.bytes, mediaType };
|
|
19510
19810
|
},
|
|
19511
19811
|
languageModel(modelId, call = {}) {
|
|
19512
|
-
const { entry, languageModel: model, provider } = textTarget(modelId, call);
|
|
19812
|
+
const { entry, languageModel: model, provider, keySource } = textTarget(modelId, call);
|
|
19513
19813
|
if (model.specificationVersion !== "v3")
|
|
19514
19814
|
throw new ModelKitError({
|
|
19515
19815
|
code: "unsupported_option",
|
|
@@ -19520,7 +19820,8 @@ function createModelKit(config, adapters) {
|
|
|
19520
19820
|
model,
|
|
19521
19821
|
providerOptions: withoutModelkit(entry.providerOptions),
|
|
19522
19822
|
catalog: entry,
|
|
19523
|
-
usageProvider: provider
|
|
19823
|
+
usageProvider: provider,
|
|
19824
|
+
...keySourceField(keySource)
|
|
19524
19825
|
};
|
|
19525
19826
|
},
|
|
19526
19827
|
submitVideoTool: videoToolJobs.submit,
|
|
@@ -19536,7 +19837,7 @@ function createModelKit(config, adapters) {
|
|
|
19536
19837
|
if (entry.type !== "text") return entry.adapter;
|
|
19537
19838
|
return textUsageProvider(
|
|
19538
19839
|
entry,
|
|
19539
|
-
resolveAnthropicTransport(entry, config, call.tier, warnOnce)
|
|
19840
|
+
resolveAnthropicTransport(entry, config, call.tier, warnOnce, call.keyOverride)
|
|
19540
19841
|
);
|
|
19541
19842
|
},
|
|
19542
19843
|
async generateText(options, call = {}) {
|
|
@@ -19585,7 +19886,7 @@ function createModelKit(config, adapters) {
|
|
|
19585
19886
|
});
|
|
19586
19887
|
},
|
|
19587
19888
|
async generateObject(options, call = {}) {
|
|
19588
|
-
const { entry, languageModel: languageModel2, provider } = textTarget(options.model, call);
|
|
19889
|
+
const { entry, languageModel: languageModel2, provider, keySource } = textTarget(options.model, call);
|
|
19589
19890
|
const sdkOptions = {
|
|
19590
19891
|
...options,
|
|
19591
19892
|
...textAbortOptions(options.abortSignal, call.signal),
|
|
@@ -19596,16 +19897,17 @@ function createModelKit(config, adapters) {
|
|
|
19596
19897
|
const result = await aiGenerateObject2(sdkOptions);
|
|
19597
19898
|
await emit(
|
|
19598
19899
|
timed(
|
|
19599
|
-
withRowUnit(
|
|
19600
|
-
textUsageEvent(entry, provider, result.usage, result.providerMetadata)
|
|
19601
|
-
|
|
19900
|
+
withRowUnit({
|
|
19901
|
+
...textUsageEvent(entry, provider, result.usage, result.providerMetadata),
|
|
19902
|
+
...keySourceField(keySource)
|
|
19903
|
+
}),
|
|
19602
19904
|
startedAt
|
|
19603
19905
|
)
|
|
19604
19906
|
);
|
|
19605
19907
|
return result;
|
|
19606
19908
|
},
|
|
19607
19909
|
streamObject(options, call = {}) {
|
|
19608
|
-
const { entry, languageModel: languageModel2, provider } = textTarget(options.model, call);
|
|
19910
|
+
const { entry, languageModel: languageModel2, provider, keySource } = textTarget(options.model, call);
|
|
19609
19911
|
const callerOnFinish = options.onFinish;
|
|
19610
19912
|
const startedAt = Date.now();
|
|
19611
19913
|
const sdkOptions = {
|
|
@@ -19616,14 +19918,15 @@ function createModelKit(config, adapters) {
|
|
|
19616
19918
|
onFinish: async (event) => {
|
|
19617
19919
|
await emit(
|
|
19618
19920
|
timed(
|
|
19619
|
-
withRowUnit(
|
|
19620
|
-
textUsageEvent(
|
|
19921
|
+
withRowUnit({
|
|
19922
|
+
...textUsageEvent(
|
|
19621
19923
|
entry,
|
|
19622
19924
|
provider,
|
|
19623
19925
|
event.usage,
|
|
19624
19926
|
event.providerMetadata
|
|
19625
|
-
)
|
|
19626
|
-
|
|
19927
|
+
),
|
|
19928
|
+
...keySourceField(keySource)
|
|
19929
|
+
}),
|
|
19627
19930
|
startedAt
|
|
19628
19931
|
)
|
|
19629
19932
|
);
|
|
@@ -19666,6 +19969,7 @@ export {
|
|
|
19666
19969
|
ADAPTER_KEYS,
|
|
19667
19970
|
ARRAY_INPUT_SPEC_KEYS,
|
|
19668
19971
|
AUDIO_INPUT_SPEC_KEYS,
|
|
19972
|
+
BYOK_PROVIDERS,
|
|
19669
19973
|
Catalog,
|
|
19670
19974
|
CatalogModelSchema,
|
|
19671
19975
|
CatalogModelSchemaBase,
|
|
@@ -19690,6 +19994,7 @@ export {
|
|
|
19690
19994
|
aiSdkTextAdapter,
|
|
19691
19995
|
arkAdapter,
|
|
19692
19996
|
aspectRatioToSize,
|
|
19997
|
+
byokProviderForModel,
|
|
19693
19998
|
classifyHttpError,
|
|
19694
19999
|
compareCatalogOrder,
|
|
19695
20000
|
createDefaultModelKit,
|
|
@@ -19717,6 +20022,7 @@ export {
|
|
|
19717
20022
|
isListed,
|
|
19718
20023
|
isListedOn,
|
|
19719
20024
|
isModelKitError,
|
|
20025
|
+
keyOverrideFor,
|
|
19720
20026
|
keysFromEnv,
|
|
19721
20027
|
languageModel,
|
|
19722
20028
|
liveCatalog,
|
|
@@ -19733,6 +20039,7 @@ export {
|
|
|
19733
20039
|
model3dSeedFits,
|
|
19734
20040
|
model3dSkus,
|
|
19735
20041
|
model3dTexture,
|
|
20042
|
+
modelsPoweredBy,
|
|
19736
20043
|
nearestAspectRatio,
|
|
19737
20044
|
normalizeExposure,
|
|
19738
20045
|
openaiImageAdapter,
|
|
@@ -19763,6 +20070,7 @@ export {
|
|
|
19763
20070
|
streamObject,
|
|
19764
20071
|
streamText,
|
|
19765
20072
|
takesTargetAspectRatio,
|
|
20073
|
+
testProviderKey,
|
|
19766
20074
|
textUsageEvent,
|
|
19767
20075
|
toGeneratedImage,
|
|
19768
20076
|
validateInputSpec,
|