@shop-os/modelkit 0.21.1 → 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 CHANGED
@@ -9,6 +9,19 @@ header in the release PR.
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.22.0] - Unreleased
13
+
14
+ ### Added
15
+ - `ModelKitError` carries optional `finishReason` and `usage` (ENG-3975). google-image sets both
16
+ when Gemini answers with no image: `finishReason` is the SDK's (`"length"` when nano-banana 2.1
17
+ ran out of output before the image), and `usage` is a TEXT `tokens` event with the input and
18
+ output tokens Google still bills. The code stays `provider_rejected` (`content_blocked` when
19
+ filtered) and `retryable: false`, so consumers that branch on the code are unchanged.
20
+ - The client stamps that `usage` with the catalog id, the key that paid (`keySource`) and the call's
21
+ `latencyMs`, keeping its `tokens` unit. It is never sent to `onUsage`: a consumer records it
22
+ as a failed call. `body.rawFinishReason` keeps Gemini's own reason (`NO_IMAGE`, ...), which the
23
+ SDK folds into `"other"`.
24
+
12
25
  ## [0.21.1] - Unreleased
13
26
 
14
27
  ### Fixed
package/README.md CHANGED
@@ -342,7 +342,7 @@ const defaultKit = createDefaultModelKit({ keys: { pro: keysFromEnv(process.env)
342
342
  | Keys | Enterprise keys win only when every key the adapter (and its route, e.g. the cloudflare token for a vendor that really goes through the gateway) needs is set on the enterprise tier; a fallback to pro keys is logged once per call. |
343
343
  | Routing | Cloudflare AI Gateway for text only. Never for Gemini images or Omni. fal passthrough for geo-blocked regions; the fal direct route submits to the queue and polls, so `job.result({ timeoutMs, signal })` is live. |
344
344
  | Retry | One classifier, one policy. Never on content blocks, aborts, or 4xx except 429; SDKs with their own retry (Replicate) run under a single kit attempt; a 429 on a status poll keeps polling unless `CallContext.maxRetries` is `0`. See durable media execution above. |
345
- | Errors | `ModelKitError` with `code`, `provider`, `status`, `body`. Gemini `blockReason` and fal `body.detail` are lifted into `body`. A throw with no HTTP status (fetch failed, reset, DNS) is a retryable `network` error on every adapter. An out-of-credit provider account is `provider_billing` and is never retried: HTTP 402, a 4xx whose structured error code is a billing code (`insufficient_quota`, ...), or a 403/429 whose text names the exhausted account ("Exhausted balance", "Insufficient credit"). BytePlus Ark codes win over the status (body `{"error":{"code","message","type"}}`, and a failed task's `error.code`): 403 `AccountOverdueError` / `OperationDenied.ServiceOverdue` and 429 `QuotaExceeded` are `provider_billing`; 404 `ModelNotOpen` / `InvalidEndpointOrModel.NotFound` are `provider_rejected` with the code in the message; a plain 403 stays `auth` and another 429 `rate_limited`. Text calls go through `ai` and surface the SDK's own error instead. |
345
+ | Errors | `ModelKitError` with `code`, `provider`, `status`, `body`, and on google-image no-image answers `finishReason` plus `usage` (the tokens Google billed, a TEXT `tokens` event). Gemini `blockReason` and fal `body.detail` are lifted into `body`. A throw with no HTTP status (fetch failed, reset, DNS) is a retryable `network` error on every adapter. An out-of-credit provider account is `provider_billing` and is never retried: HTTP 402, a 4xx whose structured error code is a billing code (`insufficient_quota`, ...), or a 403/429 whose text names the exhausted account ("Exhausted balance", "Insufficient credit"). BytePlus Ark codes win over the status (body `{"error":{"code","message","type"}}`, and a failed task's `error.code`): 403 `AccountOverdueError` / `OperationDenied.ServiceOverdue` and 429 `QuotaExceeded` are `provider_billing`; 404 `ModelNotOpen` / `InvalidEndpointOrModel.NotFound` are `provider_rejected` with the code in the message; a plain 403 stays `auth` and another 429 `rate_limited`. Text calls go through `ai` and surface the SDK's own error instead. |
346
346
  | Usage | One event per call with the canonical model id and the row's `usage.unit` (adapters supply the quantity in that unit). Gemini image events carry the token counts too (`inputTokens`/`outputTokens`); Google's raw `usageMetadata` rides on `raw.providerMetadata.google`. Video usage is emitted on completion and carries the resolution the provider renders (the default it falls back to when the request has none), because pricing is keyed by it; Seedance keeps `usage`, `duration`, `framespersecond` and `resolution` from the poll on `raw`. Audio: Lyria 3 returns no duration, so it emits `unit: "images", quantity: 1` (token counts ride along as `inputTokens`/`outputTokens`); Replicate audio (Lyria 2, MMAudio) emits `unit: "compute_seconds"` with the prediction time. |
347
347
  | Media parts | `prepareMediaPart` turns a `MediaRef` into an AI SDK file part. Google inlines bytes up to 10 MiB (base64 makes them ~4/3 larger inside Gemini's 20 MB request cap) and uploads larger payloads through the Files API; call the returned `cleanup` after the request. Other providers get the reference inline. |
348
348
 
package/dist/catalog.cjs CHANGED
@@ -90,13 +90,16 @@ __export(catalog_entry_exports, {
90
90
  module.exports = __toCommonJS(catalog_entry_exports);
91
91
 
92
92
  // src/errors.ts
93
- var ModelKitError = class extends Error {
93
+ var ModelKitError = class _ModelKitError extends Error {
94
94
  code;
95
95
  provider;
96
96
  model;
97
97
  status;
98
98
  body;
99
99
  retryable;
100
+ finishReason;
101
+ /** Set when the provider billed a call that still failed; never sent to `onUsage`. */
102
+ usage;
100
103
  constructor(init) {
101
104
  super(init.message, { cause: init.cause });
102
105
  this.name = "ModelKitError";
@@ -106,6 +109,25 @@ var ModelKitError = class extends Error {
106
109
  this.status = init.status;
107
110
  this.body = init.body;
108
111
  this.retryable = init.retryable ?? defaultRetryable(init.code);
112
+ this.finishReason = init.finishReason;
113
+ this.usage = init.usage;
114
+ }
115
+ withUsage(usage) {
116
+ const init = {
117
+ code: this.code,
118
+ message: this.message,
119
+ cause: this.cause,
120
+ retryable: this.retryable,
121
+ usage
122
+ };
123
+ if (this.provider !== void 0) init.provider = this.provider;
124
+ if (this.model !== void 0) init.model = this.model;
125
+ if (this.status !== void 0) init.status = this.status;
126
+ if (this.body !== void 0) init.body = this.body;
127
+ if (this.finishReason !== void 0) init.finishReason = this.finishReason;
128
+ const copy = new _ModelKitError(init);
129
+ if (this.stack !== void 0) copy.stack = this.stack;
130
+ return copy;
109
131
  }
110
132
  };
111
133
  function defaultRetryable(code) {