@shop-os/modelkit 0.21.0 → 0.21.1

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/dist/index.d.cts CHANGED
@@ -2259,6 +2259,12 @@ interface RetryOptions {
2259
2259
  maxDelayMs?: number;
2260
2260
  factor?: number;
2261
2261
  signal?: AbortSignal;
2262
+ /** A separate, jittered schedule and budget for `rate_limited` errors; other errors keep the one above. */
2263
+ rateLimited?: {
2264
+ attempts: number;
2265
+ baseDelayMs: number;
2266
+ maxDelayMs: number;
2267
+ };
2262
2268
  }
2263
2269
  declare function withRetry<T>(fn: (attempt: number) => Promise<T>, opts?: RetryOptions): Promise<T>;
2264
2270
  declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -2259,6 +2259,12 @@ interface RetryOptions {
2259
2259
  maxDelayMs?: number;
2260
2260
  factor?: number;
2261
2261
  signal?: AbortSignal;
2262
+ /** A separate, jittered schedule and budget for `rate_limited` errors; other errors keep the one above. */
2263
+ rateLimited?: {
2264
+ attempts: number;
2265
+ baseDelayMs: number;
2266
+ maxDelayMs: number;
2267
+ };
2262
2268
  }
2263
2269
  declare function withRetry<T>(fn: (attempt: number) => Promise<T>, opts?: RetryOptions): Promise<T>;
2264
2270
  declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
package/dist/index.js CHANGED
@@ -9248,19 +9248,42 @@ async function withRetry(fn, opts = {}) {
9248
9248
  const base = opts.baseDelayMs ?? 1e3;
9249
9249
  const max = opts.maxDelayMs ?? 1e4;
9250
9250
  const factor = opts.factor ?? 2;
9251
+ const rateLimited = opts.rateLimited;
9252
+ let limitedFailures = 0;
9253
+ let otherFailures = 0;
9251
9254
  let last;
9252
- for (let attempt = 1; attempt <= attempts; attempt++) {
9255
+ const ceiling = attempts + (rateLimited?.attempts ?? 0);
9256
+ for (let attempt = 1; attempt <= ceiling; attempt++) {
9253
9257
  if (opts.signal?.aborted) throw throwAborted();
9254
9258
  try {
9255
9259
  return await fn(attempt);
9256
9260
  } catch (cause) {
9257
- last = cause instanceof Error ? cause : new Error(String(cause), { cause });
9258
- if (attempt === attempts || !shouldRetry(last) || opts.signal?.aborted) throw cause;
9259
- await sleep(Math.min(max, base * factor ** (attempt - 1)), opts.signal);
9261
+ last = cause;
9262
+ const err = cause instanceof Error ? cause : new Error(String(cause), { cause });
9263
+ if (!shouldRetry(err) || opts.signal?.aborted) throw cause;
9264
+ let delay;
9265
+ if (rateLimited !== void 0 && err instanceof ModelKitError && err.code === "rate_limited") {
9266
+ limitedFailures += 1;
9267
+ if (limitedFailures >= rateLimited.attempts) throw cause;
9268
+ delay = jitter(
9269
+ Math.min(
9270
+ rateLimited.maxDelayMs,
9271
+ rateLimited.baseDelayMs * factor ** (limitedFailures - 1)
9272
+ )
9273
+ );
9274
+ } else {
9275
+ otherFailures += 1;
9276
+ if (otherFailures >= attempts) throw cause;
9277
+ delay = Math.min(max, base * factor ** (otherFailures - 1));
9278
+ }
9279
+ await sleep(delay, opts.signal);
9260
9280
  }
9261
9281
  }
9262
9282
  throw last;
9263
9283
  }
9284
+ function jitter(ms) {
9285
+ return Math.round(ms * (0.75 + Math.random() * 0.25));
9286
+ }
9264
9287
  function combineSignals(a, b) {
9265
9288
  return a && b ? AbortSignal.any([a, b]) : a ?? b;
9266
9289
  }
@@ -14040,6 +14063,12 @@ async function fishFetch(ctx, path, init) {
14040
14063
  if (!res.ok) throw classifyHttpError(res.status, await readBody(res), "fish", ctx.model.id);
14041
14064
  return res;
14042
14065
  }
14066
+ var FISH_RATE_LIMIT = { attempts: 6, baseDelayMs: 2e3, maxDelayMs: 2e4 };
14067
+ function fishRetryOptions(ctx) {
14068
+ const options = mediaRetryOptions(ctx.call, ctx.signal);
14069
+ if (ctx.call.maxRetries !== void 0) return options;
14070
+ return { ...options, rateLimited: FISH_RATE_LIMIT };
14071
+ }
14043
14072
  function usage3(ctx) {
14044
14073
  return {
14045
14074
  model: ctx.model.id,
@@ -14063,23 +14092,20 @@ async function generateSpeech2(req, ctx) {
14063
14092
  body.reference_id = checkVoiceId2(ctx, req.customVoice ? "customVoice" : "voice", voice);
14064
14093
  }
14065
14094
  if (req.language !== void 0) body.language = req.language;
14066
- const { bytes, mime } = await withRetry(
14067
- async () => {
14068
- const res = await fishFetch(ctx, "/v1/tts", {
14069
- method: "POST",
14070
- headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
14071
- body: JSON.stringify(body)
14072
- });
14073
- let audio;
14074
- try {
14075
- audio = new Uint8Array(await res.arrayBuffer());
14076
- } catch (cause) {
14077
- throw classifyThrown("fish", cause, ctx.model.id);
14078
- }
14079
- return { bytes: audio, mime: audioMime2(res, format) };
14080
- },
14081
- mediaRetryOptions(ctx.call, ctx.signal)
14082
- );
14095
+ const { bytes, mime } = await withRetry(async () => {
14096
+ const res = await fishFetch(ctx, "/v1/tts", {
14097
+ method: "POST",
14098
+ headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
14099
+ body: JSON.stringify(body)
14100
+ });
14101
+ let audio;
14102
+ try {
14103
+ audio = new Uint8Array(await res.arrayBuffer());
14104
+ } catch (cause) {
14105
+ throw classifyThrown("fish", cause, ctx.model.id);
14106
+ }
14107
+ return { bytes: audio, mime: audioMime2(res, format) };
14108
+ }, fishRetryOptions(ctx));
14083
14109
  if (bytes.byteLength === 0)
14084
14110
  throw fishError(ctx, "provider_rejected", "Fish TTS returned no audio");
14085
14111
  return { audio: { bytes, mime }, usage: usage3(ctx) };
@@ -14155,7 +14181,7 @@ async function createVoice2(req, ctx) {
14155
14181
  const form = await cloneForm2(req, ctx);
14156
14182
  const res = await withRetry(
14157
14183
  () => onlyRateLimitRetries2(() => fishFetch(ctx, "/model", { method: "POST", body: form })),
14158
- mediaRetryOptions(ctx.call, ctx.signal)
14184
+ fishRetryOptions(ctx)
14159
14185
  );
14160
14186
  const raw = await readBody(res);
14161
14187
  const parsed = createdModelSchema.safeParse(raw);
@@ -14192,17 +14218,14 @@ async function designVoice(req, ctx) {
14192
14218
  if (req.language !== void 0) body.language = req.language;
14193
14219
  if (req.count !== void 0) body.n = req.count;
14194
14220
  if (req.seed !== void 0) body.seed = req.seed;
14195
- const raw = await withRetry(
14196
- async () => {
14197
- const res = await fishFetch(ctx, "/v1/voice-design", {
14198
- method: "POST",
14199
- headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
14200
- body: JSON.stringify(body)
14201
- });
14202
- return readBody(res);
14203
- },
14204
- mediaRetryOptions(ctx.call, ctx.signal)
14205
- );
14221
+ const raw = await withRetry(async () => {
14222
+ const res = await fishFetch(ctx, "/v1/voice-design", {
14223
+ method: "POST",
14224
+ headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
14225
+ body: JSON.stringify(body)
14226
+ });
14227
+ return readBody(res);
14228
+ }, fishRetryOptions(ctx));
14206
14229
  const parsed = designSchema.safeParse(raw);
14207
14230
  if (!parsed.success) {
14208
14231
  throw fishError(ctx, "provider_rejected", "Fish voice design returned no candidates");
@@ -14220,27 +14243,24 @@ async function deleteVoice2(req, ctx) {
14220
14243
  checkRow2(ctx, "voice-clone");
14221
14244
  const id = encodeURIComponent(checkVoiceId2(ctx, "voiceId", req.voiceId));
14222
14245
  let attempts = 0;
14223
- await withRetry(
14224
- async () => {
14225
- attempts += 1;
14226
- try {
14227
- await fishFetch(ctx, `/model/${id}`, { method: "DELETE" });
14228
- } catch (cause) {
14229
- if (!isModelKitError(cause) || cause.status !== 404) throw cause;
14230
- if (attempts > 1) return;
14231
- throw new ModelKitError({
14232
- code: "provider_rejected",
14233
- message: `Fish has no voice ${req.voiceId} under this key set; delete with the tier that created it`,
14234
- provider: "fish",
14235
- model: ctx.model.id,
14236
- status: 404,
14237
- cause,
14238
- retryable: false
14239
- });
14240
- }
14241
- },
14242
- mediaRetryOptions(ctx.call, ctx.signal)
14243
- );
14246
+ await withRetry(async () => {
14247
+ attempts += 1;
14248
+ try {
14249
+ await fishFetch(ctx, `/model/${id}`, { method: "DELETE" });
14250
+ } catch (cause) {
14251
+ if (!isModelKitError(cause) || cause.status !== 404) throw cause;
14252
+ if (attempts > 1) return;
14253
+ throw new ModelKitError({
14254
+ code: "provider_rejected",
14255
+ message: `Fish has no voice ${req.voiceId} under this key set; delete with the tier that created it`,
14256
+ provider: "fish",
14257
+ model: ctx.model.id,
14258
+ status: 404,
14259
+ cause,
14260
+ retryable: false
14261
+ });
14262
+ }
14263
+ }, fishRetryOptions(ctx));
14244
14264
  }
14245
14265
  var fishAdapter = {
14246
14266
  id: "fish",