@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/CHANGELOG.md CHANGED
@@ -9,6 +9,17 @@ header in the release PR.
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.21.1] - Unreleased
13
+
14
+ ### Fixed
15
+ - Fish calls wait out a concurrency 429 (ENG-3951). Fish limits requests in flight per account
16
+ and sends no Retry-After; the default retry (3 attempts, ~3 s) gave up first, so a burst of
17
+ parallel speech failed. With `maxRetries` unset, a `rate_limited` response to TTS, voice clone,
18
+ voice design or voice delete now retries up
19
+ to 6 attempts with jittered backoff from 2 s to 20 s (about 40-50 s in total). Other errors
20
+ keep the default. `RetryOptions.rateLimited` sets that schedule for `withRetry`; rate-limit and
21
+ other failures each keep their own budget and backoff step, whatever order they come in.
22
+
12
23
  ## [0.21.0] - Unreleased
13
24
 
14
25
  ### Added
package/README.md CHANGED
@@ -222,7 +222,8 @@ const result = await kit.generateImage(
222
222
 
223
223
  `maxRetries` counts retries after the first request. Zero disables adapter and SDK
224
224
  retries, failed status-read retries, and Google's Files-to-inline fallback. Successful
225
- pending-job responses still poll the same job. Unset retains existing adapter defaults.
225
+ pending-job responses still poll the same job. Unset retains existing adapter defaults;
226
+ Fish calls then wait out a concurrency 429 for up to 6 attempts (about 40-50 s, jittered).
226
227
  An explicit nonzero limit allows bounded submission retries; a later read failure never
227
228
  resubmits an already-created fal job. Text calls instead use AI SDK's first-argument
228
229
  `maxRetries: 0` option.
package/dist/index.cjs CHANGED
@@ -9404,19 +9404,42 @@ async function withRetry(fn, opts = {}) {
9404
9404
  const base = opts.baseDelayMs ?? 1e3;
9405
9405
  const max = opts.maxDelayMs ?? 1e4;
9406
9406
  const factor = opts.factor ?? 2;
9407
+ const rateLimited = opts.rateLimited;
9408
+ let limitedFailures = 0;
9409
+ let otherFailures = 0;
9407
9410
  let last;
9408
- for (let attempt = 1; attempt <= attempts; attempt++) {
9411
+ const ceiling = attempts + (rateLimited?.attempts ?? 0);
9412
+ for (let attempt = 1; attempt <= ceiling; attempt++) {
9409
9413
  if (opts.signal?.aborted) throw throwAborted();
9410
9414
  try {
9411
9415
  return await fn(attempt);
9412
9416
  } catch (cause) {
9413
- last = cause instanceof Error ? cause : new Error(String(cause), { cause });
9414
- if (attempt === attempts || !shouldRetry(last) || opts.signal?.aborted) throw cause;
9415
- await sleep(Math.min(max, base * factor ** (attempt - 1)), opts.signal);
9417
+ last = cause;
9418
+ const err = cause instanceof Error ? cause : new Error(String(cause), { cause });
9419
+ if (!shouldRetry(err) || opts.signal?.aborted) throw cause;
9420
+ let delay;
9421
+ if (rateLimited !== void 0 && err instanceof ModelKitError && err.code === "rate_limited") {
9422
+ limitedFailures += 1;
9423
+ if (limitedFailures >= rateLimited.attempts) throw cause;
9424
+ delay = jitter(
9425
+ Math.min(
9426
+ rateLimited.maxDelayMs,
9427
+ rateLimited.baseDelayMs * factor ** (limitedFailures - 1)
9428
+ )
9429
+ );
9430
+ } else {
9431
+ otherFailures += 1;
9432
+ if (otherFailures >= attempts) throw cause;
9433
+ delay = Math.min(max, base * factor ** (otherFailures - 1));
9434
+ }
9435
+ await sleep(delay, opts.signal);
9416
9436
  }
9417
9437
  }
9418
9438
  throw last;
9419
9439
  }
9440
+ function jitter(ms) {
9441
+ return Math.round(ms * (0.75 + Math.random() * 0.25));
9442
+ }
9420
9443
  function combineSignals(a, b) {
9421
9444
  return a && b ? AbortSignal.any([a, b]) : a ?? b;
9422
9445
  }
@@ -14196,6 +14219,12 @@ async function fishFetch(ctx, path, init) {
14196
14219
  if (!res.ok) throw classifyHttpError(res.status, await readBody(res), "fish", ctx.model.id);
14197
14220
  return res;
14198
14221
  }
14222
+ var FISH_RATE_LIMIT = { attempts: 6, baseDelayMs: 2e3, maxDelayMs: 2e4 };
14223
+ function fishRetryOptions(ctx) {
14224
+ const options = mediaRetryOptions(ctx.call, ctx.signal);
14225
+ if (ctx.call.maxRetries !== void 0) return options;
14226
+ return { ...options, rateLimited: FISH_RATE_LIMIT };
14227
+ }
14199
14228
  function usage3(ctx) {
14200
14229
  return {
14201
14230
  model: ctx.model.id,
@@ -14219,23 +14248,20 @@ async function generateSpeech2(req, ctx) {
14219
14248
  body.reference_id = checkVoiceId2(ctx, req.customVoice ? "customVoice" : "voice", voice);
14220
14249
  }
14221
14250
  if (req.language !== void 0) body.language = req.language;
14222
- const { bytes, mime } = await withRetry(
14223
- async () => {
14224
- const res = await fishFetch(ctx, "/v1/tts", {
14225
- method: "POST",
14226
- headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
14227
- body: JSON.stringify(body)
14228
- });
14229
- let audio;
14230
- try {
14231
- audio = new Uint8Array(await res.arrayBuffer());
14232
- } catch (cause) {
14233
- throw classifyThrown("fish", cause, ctx.model.id);
14234
- }
14235
- return { bytes: audio, mime: audioMime2(res, format) };
14236
- },
14237
- mediaRetryOptions(ctx.call, ctx.signal)
14238
- );
14251
+ const { bytes, mime } = await withRetry(async () => {
14252
+ const res = await fishFetch(ctx, "/v1/tts", {
14253
+ method: "POST",
14254
+ headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
14255
+ body: JSON.stringify(body)
14256
+ });
14257
+ let audio;
14258
+ try {
14259
+ audio = new Uint8Array(await res.arrayBuffer());
14260
+ } catch (cause) {
14261
+ throw classifyThrown("fish", cause, ctx.model.id);
14262
+ }
14263
+ return { bytes: audio, mime: audioMime2(res, format) };
14264
+ }, fishRetryOptions(ctx));
14239
14265
  if (bytes.byteLength === 0)
14240
14266
  throw fishError(ctx, "provider_rejected", "Fish TTS returned no audio");
14241
14267
  return { audio: { bytes, mime }, usage: usage3(ctx) };
@@ -14311,7 +14337,7 @@ async function createVoice2(req, ctx) {
14311
14337
  const form = await cloneForm2(req, ctx);
14312
14338
  const res = await withRetry(
14313
14339
  () => onlyRateLimitRetries2(() => fishFetch(ctx, "/model", { method: "POST", body: form })),
14314
- mediaRetryOptions(ctx.call, ctx.signal)
14340
+ fishRetryOptions(ctx)
14315
14341
  );
14316
14342
  const raw = await readBody(res);
14317
14343
  const parsed = createdModelSchema.safeParse(raw);
@@ -14348,17 +14374,14 @@ async function designVoice(req, ctx) {
14348
14374
  if (req.language !== void 0) body.language = req.language;
14349
14375
  if (req.count !== void 0) body.n = req.count;
14350
14376
  if (req.seed !== void 0) body.seed = req.seed;
14351
- const raw = await withRetry(
14352
- async () => {
14353
- const res = await fishFetch(ctx, "/v1/voice-design", {
14354
- method: "POST",
14355
- headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
14356
- body: JSON.stringify(body)
14357
- });
14358
- return readBody(res);
14359
- },
14360
- mediaRetryOptions(ctx.call, ctx.signal)
14361
- );
14377
+ const raw = await withRetry(async () => {
14378
+ const res = await fishFetch(ctx, "/v1/voice-design", {
14379
+ method: "POST",
14380
+ headers: { "Content-Type": "application/json", model: ctx.model.providerModelId },
14381
+ body: JSON.stringify(body)
14382
+ });
14383
+ return readBody(res);
14384
+ }, fishRetryOptions(ctx));
14362
14385
  const parsed = designSchema.safeParse(raw);
14363
14386
  if (!parsed.success) {
14364
14387
  throw fishError(ctx, "provider_rejected", "Fish voice design returned no candidates");
@@ -14376,27 +14399,24 @@ async function deleteVoice2(req, ctx) {
14376
14399
  checkRow2(ctx, "voice-clone");
14377
14400
  const id = encodeURIComponent(checkVoiceId2(ctx, "voiceId", req.voiceId));
14378
14401
  let attempts = 0;
14379
- await withRetry(
14380
- async () => {
14381
- attempts += 1;
14382
- try {
14383
- await fishFetch(ctx, `/model/${id}`, { method: "DELETE" });
14384
- } catch (cause) {
14385
- if (!isModelKitError(cause) || cause.status !== 404) throw cause;
14386
- if (attempts > 1) return;
14387
- throw new ModelKitError({
14388
- code: "provider_rejected",
14389
- message: `Fish has no voice ${req.voiceId} under this key set; delete with the tier that created it`,
14390
- provider: "fish",
14391
- model: ctx.model.id,
14392
- status: 404,
14393
- cause,
14394
- retryable: false
14395
- });
14396
- }
14397
- },
14398
- mediaRetryOptions(ctx.call, ctx.signal)
14399
- );
14402
+ await withRetry(async () => {
14403
+ attempts += 1;
14404
+ try {
14405
+ await fishFetch(ctx, `/model/${id}`, { method: "DELETE" });
14406
+ } catch (cause) {
14407
+ if (!isModelKitError(cause) || cause.status !== 404) throw cause;
14408
+ if (attempts > 1) return;
14409
+ throw new ModelKitError({
14410
+ code: "provider_rejected",
14411
+ message: `Fish has no voice ${req.voiceId} under this key set; delete with the tier that created it`,
14412
+ provider: "fish",
14413
+ model: ctx.model.id,
14414
+ status: 404,
14415
+ cause,
14416
+ retryable: false
14417
+ });
14418
+ }
14419
+ }, fishRetryOptions(ctx));
14400
14420
  }
14401
14421
  var fishAdapter = {
14402
14422
  id: "fish",