@shop-os/modelkit 0.21.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/README.md +3 -2
- package/dist/catalog.cjs +23 -1
- package/dist/catalog.cjs.map +1 -1
- package/dist/catalog.d.cts +7 -1
- package/dist/catalog.d.ts +7 -1
- package/dist/catalog.js +23 -1
- package/dist/catalog.js.map +1 -1
- package/dist/index.cjs +136 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +13 -1
- package/dist/index.d.ts +13 -1
- package/dist/index.js +136 -59
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -166,13 +166,16 @@ function hasAwsCredentials(aws) {
|
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
// src/errors.ts
|
|
169
|
-
var ModelKitError = class extends Error {
|
|
169
|
+
var ModelKitError = class _ModelKitError extends Error {
|
|
170
170
|
code;
|
|
171
171
|
provider;
|
|
172
172
|
model;
|
|
173
173
|
status;
|
|
174
174
|
body;
|
|
175
175
|
retryable;
|
|
176
|
+
finishReason;
|
|
177
|
+
/** Set when the provider billed a call that still failed; never sent to `onUsage`. */
|
|
178
|
+
usage;
|
|
176
179
|
constructor(init) {
|
|
177
180
|
super(init.message, { cause: init.cause });
|
|
178
181
|
this.name = "ModelKitError";
|
|
@@ -182,6 +185,25 @@ var ModelKitError = class extends Error {
|
|
|
182
185
|
this.status = init.status;
|
|
183
186
|
this.body = init.body;
|
|
184
187
|
this.retryable = init.retryable ?? defaultRetryable(init.code);
|
|
188
|
+
this.finishReason = init.finishReason;
|
|
189
|
+
this.usage = init.usage;
|
|
190
|
+
}
|
|
191
|
+
withUsage(usage5) {
|
|
192
|
+
const init = {
|
|
193
|
+
code: this.code,
|
|
194
|
+
message: this.message,
|
|
195
|
+
cause: this.cause,
|
|
196
|
+
retryable: this.retryable,
|
|
197
|
+
usage: usage5
|
|
198
|
+
};
|
|
199
|
+
if (this.provider !== void 0) init.provider = this.provider;
|
|
200
|
+
if (this.model !== void 0) init.model = this.model;
|
|
201
|
+
if (this.status !== void 0) init.status = this.status;
|
|
202
|
+
if (this.body !== void 0) init.body = this.body;
|
|
203
|
+
if (this.finishReason !== void 0) init.finishReason = this.finishReason;
|
|
204
|
+
const copy = new _ModelKitError(init);
|
|
205
|
+
if (this.stack !== void 0) copy.stack = this.stack;
|
|
206
|
+
return copy;
|
|
185
207
|
}
|
|
186
208
|
};
|
|
187
209
|
function defaultRetryable(code) {
|
|
@@ -9404,19 +9426,42 @@ async function withRetry(fn, opts = {}) {
|
|
|
9404
9426
|
const base = opts.baseDelayMs ?? 1e3;
|
|
9405
9427
|
const max = opts.maxDelayMs ?? 1e4;
|
|
9406
9428
|
const factor = opts.factor ?? 2;
|
|
9429
|
+
const rateLimited = opts.rateLimited;
|
|
9430
|
+
let limitedFailures = 0;
|
|
9431
|
+
let otherFailures = 0;
|
|
9407
9432
|
let last;
|
|
9408
|
-
|
|
9433
|
+
const ceiling = attempts + (rateLimited?.attempts ?? 0);
|
|
9434
|
+
for (let attempt = 1; attempt <= ceiling; attempt++) {
|
|
9409
9435
|
if (opts.signal?.aborted) throw throwAborted();
|
|
9410
9436
|
try {
|
|
9411
9437
|
return await fn(attempt);
|
|
9412
9438
|
} catch (cause) {
|
|
9413
|
-
last = cause
|
|
9414
|
-
|
|
9415
|
-
|
|
9439
|
+
last = cause;
|
|
9440
|
+
const err = cause instanceof Error ? cause : new Error(String(cause), { cause });
|
|
9441
|
+
if (!shouldRetry(err) || opts.signal?.aborted) throw cause;
|
|
9442
|
+
let delay;
|
|
9443
|
+
if (rateLimited !== void 0 && err instanceof ModelKitError && err.code === "rate_limited") {
|
|
9444
|
+
limitedFailures += 1;
|
|
9445
|
+
if (limitedFailures >= rateLimited.attempts) throw cause;
|
|
9446
|
+
delay = jitter(
|
|
9447
|
+
Math.min(
|
|
9448
|
+
rateLimited.maxDelayMs,
|
|
9449
|
+
rateLimited.baseDelayMs * factor ** (limitedFailures - 1)
|
|
9450
|
+
)
|
|
9451
|
+
);
|
|
9452
|
+
} else {
|
|
9453
|
+
otherFailures += 1;
|
|
9454
|
+
if (otherFailures >= attempts) throw cause;
|
|
9455
|
+
delay = Math.min(max, base * factor ** (otherFailures - 1));
|
|
9456
|
+
}
|
|
9457
|
+
await sleep(delay, opts.signal);
|
|
9416
9458
|
}
|
|
9417
9459
|
}
|
|
9418
9460
|
throw last;
|
|
9419
9461
|
}
|
|
9462
|
+
function jitter(ms) {
|
|
9463
|
+
return Math.round(ms * (0.75 + Math.random() * 0.25));
|
|
9464
|
+
}
|
|
9420
9465
|
function combineSignals(a, b) {
|
|
9421
9466
|
return a && b ? AbortSignal.any([a, b]) : a ?? b;
|
|
9422
9467
|
}
|
|
@@ -14196,6 +14241,12 @@ async function fishFetch(ctx, path, init) {
|
|
|
14196
14241
|
if (!res.ok) throw classifyHttpError(res.status, await readBody(res), "fish", ctx.model.id);
|
|
14197
14242
|
return res;
|
|
14198
14243
|
}
|
|
14244
|
+
var FISH_RATE_LIMIT = { attempts: 6, baseDelayMs: 2e3, maxDelayMs: 2e4 };
|
|
14245
|
+
function fishRetryOptions(ctx) {
|
|
14246
|
+
const options = mediaRetryOptions(ctx.call, ctx.signal);
|
|
14247
|
+
if (ctx.call.maxRetries !== void 0) return options;
|
|
14248
|
+
return { ...options, rateLimited: FISH_RATE_LIMIT };
|
|
14249
|
+
}
|
|
14199
14250
|
function usage3(ctx) {
|
|
14200
14251
|
return {
|
|
14201
14252
|
model: ctx.model.id,
|
|
@@ -14219,23 +14270,20 @@ async function generateSpeech2(req, ctx) {
|
|
|
14219
14270
|
body.reference_id = checkVoiceId2(ctx, req.customVoice ? "customVoice" : "voice", voice);
|
|
14220
14271
|
}
|
|
14221
14272
|
if (req.language !== void 0) body.language = req.language;
|
|
14222
|
-
const { bytes, mime } = await withRetry(
|
|
14223
|
-
|
|
14224
|
-
|
|
14225
|
-
|
|
14226
|
-
|
|
14227
|
-
|
|
14228
|
-
|
|
14229
|
-
|
|
14230
|
-
|
|
14231
|
-
|
|
14232
|
-
|
|
14233
|
-
|
|
14234
|
-
|
|
14235
|
-
|
|
14236
|
-
},
|
|
14237
|
-
mediaRetryOptions(ctx.call, ctx.signal)
|
|
14238
|
-
);
|
|
14273
|
+
const { bytes, mime } = await withRetry(async () => {
|
|
14274
|
+
const res = await fishFetch(ctx, "/v1/tts", {
|
|
14275
|
+
method: "POST",
|
|
14276
|
+
headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
|
|
14277
|
+
body: JSON.stringify(body)
|
|
14278
|
+
});
|
|
14279
|
+
let audio;
|
|
14280
|
+
try {
|
|
14281
|
+
audio = new Uint8Array(await res.arrayBuffer());
|
|
14282
|
+
} catch (cause) {
|
|
14283
|
+
throw classifyThrown("fish", cause, ctx.model.id);
|
|
14284
|
+
}
|
|
14285
|
+
return { bytes: audio, mime: audioMime2(res, format) };
|
|
14286
|
+
}, fishRetryOptions(ctx));
|
|
14239
14287
|
if (bytes.byteLength === 0)
|
|
14240
14288
|
throw fishError(ctx, "provider_rejected", "Fish TTS returned no audio");
|
|
14241
14289
|
return { audio: { bytes, mime }, usage: usage3(ctx) };
|
|
@@ -14311,7 +14359,7 @@ async function createVoice2(req, ctx) {
|
|
|
14311
14359
|
const form = await cloneForm2(req, ctx);
|
|
14312
14360
|
const res = await withRetry(
|
|
14313
14361
|
() => onlyRateLimitRetries2(() => fishFetch(ctx, "/model", { method: "POST", body: form })),
|
|
14314
|
-
|
|
14362
|
+
fishRetryOptions(ctx)
|
|
14315
14363
|
);
|
|
14316
14364
|
const raw = await readBody(res);
|
|
14317
14365
|
const parsed = createdModelSchema.safeParse(raw);
|
|
@@ -14348,17 +14396,14 @@ async function designVoice(req, ctx) {
|
|
|
14348
14396
|
if (req.language !== void 0) body.language = req.language;
|
|
14349
14397
|
if (req.count !== void 0) body.n = req.count;
|
|
14350
14398
|
if (req.seed !== void 0) body.seed = req.seed;
|
|
14351
|
-
const raw = await withRetry(
|
|
14352
|
-
|
|
14353
|
-
|
|
14354
|
-
|
|
14355
|
-
|
|
14356
|
-
|
|
14357
|
-
|
|
14358
|
-
|
|
14359
|
-
},
|
|
14360
|
-
mediaRetryOptions(ctx.call, ctx.signal)
|
|
14361
|
-
);
|
|
14399
|
+
const raw = await withRetry(async () => {
|
|
14400
|
+
const res = await fishFetch(ctx, "/v1/voice-design", {
|
|
14401
|
+
method: "POST",
|
|
14402
|
+
headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
|
|
14403
|
+
body: JSON.stringify(body)
|
|
14404
|
+
});
|
|
14405
|
+
return readBody(res);
|
|
14406
|
+
}, fishRetryOptions(ctx));
|
|
14362
14407
|
const parsed = designSchema.safeParse(raw);
|
|
14363
14408
|
if (!parsed.success) {
|
|
14364
14409
|
throw fishError(ctx, "provider_rejected", "Fish voice design returned no candidates");
|
|
@@ -14376,27 +14421,24 @@ async function deleteVoice2(req, ctx) {
|
|
|
14376
14421
|
checkRow2(ctx, "voice-clone");
|
|
14377
14422
|
const id = encodeURIComponent(checkVoiceId2(ctx, "voiceId", req.voiceId));
|
|
14378
14423
|
let attempts = 0;
|
|
14379
|
-
await withRetry(
|
|
14380
|
-
|
|
14381
|
-
|
|
14382
|
-
|
|
14383
|
-
|
|
14384
|
-
|
|
14385
|
-
|
|
14386
|
-
|
|
14387
|
-
|
|
14388
|
-
|
|
14389
|
-
|
|
14390
|
-
|
|
14391
|
-
|
|
14392
|
-
|
|
14393
|
-
|
|
14394
|
-
|
|
14395
|
-
|
|
14396
|
-
|
|
14397
|
-
},
|
|
14398
|
-
mediaRetryOptions(ctx.call, ctx.signal)
|
|
14399
|
-
);
|
|
14424
|
+
await withRetry(async () => {
|
|
14425
|
+
attempts += 1;
|
|
14426
|
+
try {
|
|
14427
|
+
await fishFetch(ctx, `/model/${id}`, { method: "DELETE" });
|
|
14428
|
+
} catch (cause) {
|
|
14429
|
+
if (!isModelKitError(cause) || cause.status !== 404) throw cause;
|
|
14430
|
+
if (attempts > 1) return;
|
|
14431
|
+
throw new ModelKitError({
|
|
14432
|
+
code: "provider_rejected",
|
|
14433
|
+
message: `Fish has no voice ${req.voiceId} under this key set; delete with the tier that created it`,
|
|
14434
|
+
provider: "fish",
|
|
14435
|
+
model: ctx.model.id,
|
|
14436
|
+
status: 404,
|
|
14437
|
+
cause,
|
|
14438
|
+
retryable: false
|
|
14439
|
+
});
|
|
14440
|
+
}
|
|
14441
|
+
}, fishRetryOptions(ctx));
|
|
14400
14442
|
}
|
|
14401
14443
|
var fishAdapter = {
|
|
14402
14444
|
id: "fish",
|
|
@@ -15352,6 +15394,21 @@ function extractBlockReason(cause) {
|
|
|
15352
15394
|
current = parsed.data.cause ?? null;
|
|
15353
15395
|
}
|
|
15354
15396
|
}
|
|
15397
|
+
function billedTokens(model, usage5) {
|
|
15398
|
+
const inputTokens = usage5?.inputTokens;
|
|
15399
|
+
const outputTokens = usage5?.outputTokens;
|
|
15400
|
+
if (inputTokens === void 0 && outputTokens === void 0) return void 0;
|
|
15401
|
+
const event = {
|
|
15402
|
+
model,
|
|
15403
|
+
provider: "google-image",
|
|
15404
|
+
modality: "TEXT",
|
|
15405
|
+
unit: "tokens",
|
|
15406
|
+
quantity: (inputTokens ?? 0) + (outputTokens ?? 0)
|
|
15407
|
+
};
|
|
15408
|
+
if (inputTokens !== void 0) event.inputTokens = inputTokens;
|
|
15409
|
+
if (outputTokens !== void 0) event.outputTokens = outputTokens;
|
|
15410
|
+
return event;
|
|
15411
|
+
}
|
|
15355
15412
|
function toProviderError(cause, ctx) {
|
|
15356
15413
|
const blockReason = extractBlockReason(cause);
|
|
15357
15414
|
if (blockReason) {
|
|
@@ -15423,14 +15480,22 @@ function createGoogleImageAdapter(deps = defaultGoogleImageDeps) {
|
|
|
15423
15480
|
const images = (result.files ?? []).filter((file) => file.mediaType.startsWith("image/")).map((file) => ({ bytes: file.uint8Array, mime: file.mediaType }));
|
|
15424
15481
|
if (images.length === 0) {
|
|
15425
15482
|
const filtered = result.finishReason === "content-filter" || result.finishReason === "other";
|
|
15426
|
-
|
|
15483
|
+
const init = {
|
|
15427
15484
|
code: filtered ? "content_blocked" : "provider_rejected",
|
|
15428
15485
|
message: `google-image returned no image (finishReason: ${result.finishReason}). ${result.text}`.trim(),
|
|
15429
15486
|
provider: "google-image",
|
|
15430
15487
|
model: ctx.model.id,
|
|
15431
|
-
body: {
|
|
15488
|
+
body: {
|
|
15489
|
+
finishReason: result.finishReason ?? null,
|
|
15490
|
+
rawFinishReason: result.rawFinishReason ?? null,
|
|
15491
|
+
text: result.text ?? ""
|
|
15492
|
+
},
|
|
15432
15493
|
retryable: false
|
|
15433
|
-
}
|
|
15494
|
+
};
|
|
15495
|
+
if (result.finishReason !== void 0) init.finishReason = result.finishReason;
|
|
15496
|
+
const billed = billedTokens(ctx.model.id, result.usage);
|
|
15497
|
+
if (billed !== void 0) init.usage = billed;
|
|
15498
|
+
throw new ModelKitError(init);
|
|
15434
15499
|
}
|
|
15435
15500
|
const usage5 = {
|
|
15436
15501
|
model: ctx.model.id,
|
|
@@ -20618,6 +20683,11 @@ function createModelKit(config, adapters) {
|
|
|
20618
20683
|
};
|
|
20619
20684
|
return startedAt === void 0 ? stamped : timed(stamped, startedAt);
|
|
20620
20685
|
}
|
|
20686
|
+
function billedFailure(ctx, err, billed, startedAt) {
|
|
20687
|
+
return err.withUsage(
|
|
20688
|
+
timed({ ...billed, model: ctx.model.id, ...keySourceField(ctx.keySource) }, startedAt)
|
|
20689
|
+
);
|
|
20690
|
+
}
|
|
20621
20691
|
function textTimed(event, stepCount, startedAt) {
|
|
20622
20692
|
return stepCount === 1 ? timed(event, startedAt) : event;
|
|
20623
20693
|
}
|
|
@@ -20902,7 +20972,14 @@ function createModelKit(config, adapters) {
|
|
|
20902
20972
|
model: model.id
|
|
20903
20973
|
});
|
|
20904
20974
|
const startedAt = Date.now();
|
|
20905
|
-
|
|
20975
|
+
let result;
|
|
20976
|
+
try {
|
|
20977
|
+
result = await adapter.generateImage(request, ctx);
|
|
20978
|
+
} catch (err) {
|
|
20979
|
+
if (isModelKitError(err) && err.usage !== void 0)
|
|
20980
|
+
throw billedFailure(ctx, err, err.usage, startedAt);
|
|
20981
|
+
throw err;
|
|
20982
|
+
}
|
|
20906
20983
|
const usage5 = rowUsage(ctx, result.usage, startedAt);
|
|
20907
20984
|
await emit(usage5);
|
|
20908
20985
|
const images = result.images.map(toGeneratedImage);
|