@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/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) {
@@ -15372,6 +15394,21 @@ function extractBlockReason(cause) {
15372
15394
  current = parsed.data.cause ?? null;
15373
15395
  }
15374
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
+ }
15375
15412
  function toProviderError(cause, ctx) {
15376
15413
  const blockReason = extractBlockReason(cause);
15377
15414
  if (blockReason) {
@@ -15443,14 +15480,22 @@ function createGoogleImageAdapter(deps = defaultGoogleImageDeps) {
15443
15480
  const images = (result.files ?? []).filter((file) => file.mediaType.startsWith("image/")).map((file) => ({ bytes: file.uint8Array, mime: file.mediaType }));
15444
15481
  if (images.length === 0) {
15445
15482
  const filtered = result.finishReason === "content-filter" || result.finishReason === "other";
15446
- throw new ModelKitError({
15483
+ const init = {
15447
15484
  code: filtered ? "content_blocked" : "provider_rejected",
15448
15485
  message: `google-image returned no image (finishReason: ${result.finishReason}). ${result.text}`.trim(),
15449
15486
  provider: "google-image",
15450
15487
  model: ctx.model.id,
15451
- body: { finishReason: result.finishReason ?? null, text: result.text ?? "" },
15488
+ body: {
15489
+ finishReason: result.finishReason ?? null,
15490
+ rawFinishReason: result.rawFinishReason ?? null,
15491
+ text: result.text ?? ""
15492
+ },
15452
15493
  retryable: false
15453
- });
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);
15454
15499
  }
15455
15500
  const usage5 = {
15456
15501
  model: ctx.model.id,
@@ -20638,6 +20683,11 @@ function createModelKit(config, adapters) {
20638
20683
  };
20639
20684
  return startedAt === void 0 ? stamped : timed(stamped, startedAt);
20640
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
+ }
20641
20691
  function textTimed(event, stepCount, startedAt) {
20642
20692
  return stepCount === 1 ? timed(event, startedAt) : event;
20643
20693
  }
@@ -20922,7 +20972,14 @@ function createModelKit(config, adapters) {
20922
20972
  model: model.id
20923
20973
  });
20924
20974
  const startedAt = Date.now();
20925
- const result = await adapter.generateImage(request, ctx);
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
+ }
20926
20983
  const usage5 = rowUsage(ctx, result.usage, startedAt);
20927
20984
  await emit(usage5);
20928
20985
  const images = result.images.map(toGeneratedImage);