@llmgateway/ai-sdk-provider 3.5.0 → 3.7.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.
@@ -2184,16 +2184,16 @@ var llmgatewayFailedResponseHandler = createJsonErrorResponseHandler({
2184
2184
  function mapLLMGatewayFinishReason(finishReason) {
2185
2185
  switch (finishReason) {
2186
2186
  case "stop":
2187
- return "stop";
2187
+ return { unified: "stop", raw: finishReason };
2188
2188
  case "length":
2189
- return "length";
2189
+ return { unified: "length", raw: finishReason };
2190
2190
  case "content_filter":
2191
- return "content-filter";
2191
+ return { unified: "content-filter", raw: finishReason };
2192
2192
  case "function_call":
2193
2193
  case "tool_calls":
2194
- return "tool-calls";
2194
+ return { unified: "tool-calls", raw: finishReason };
2195
2195
  default:
2196
- return "unknown";
2196
+ return { unified: "other", raw: finishReason != null ? finishReason : void 0 };
2197
2197
  }
2198
2198
  }
2199
2199
 
@@ -2396,6 +2396,7 @@ function convertToLLMGatewayChatMessages(prompt) {
2396
2396
  }
2397
2397
  case "tool": {
2398
2398
  for (const toolResponse of content) {
2399
+ if (toolResponse.type !== "tool-result") continue;
2399
2400
  const content2 = getToolResultContent(toolResponse);
2400
2401
  messages.push({
2401
2402
  role: "tool",
@@ -2414,7 +2415,14 @@ function convertToLLMGatewayChatMessages(prompt) {
2414
2415
  return messages;
2415
2416
  }
2416
2417
  function getToolResultContent(input) {
2417
- return input.output.type === "text" ? input.output.value : JSON.stringify(input.output.value);
2418
+ switch (input.output.type) {
2419
+ case "text":
2420
+ return input.output.value;
2421
+ case "json":
2422
+ return JSON.stringify(input.output.value);
2423
+ default:
2424
+ return JSON.stringify(input.output);
2425
+ }
2418
2426
  }
2419
2427
 
2420
2428
  // src/chat/get-tool-choice.ts
@@ -2584,9 +2592,8 @@ var LLMGatewayStreamChatCompletionChunkSchema = z6.union([
2584
2592
  // src/chat/index.ts
2585
2593
  var LLMGatewayChatLanguageModel = class {
2586
2594
  constructor(modelId, settings, config) {
2587
- this.specificationVersion = "v2";
2595
+ this.specificationVersion = "v3";
2588
2596
  this.provider = "llmgateway";
2589
- this.defaultObjectGenerationMode = "tool";
2590
2597
  this.supportedUrls = {
2591
2598
  "image/*": [
2592
2599
  /^data:image\/[a-zA-Z]+;base64,/,
@@ -2675,7 +2682,7 @@ var LLMGatewayChatLanguageModel = class {
2675
2682
  return baseArgs;
2676
2683
  }
2677
2684
  async doGenerate(options) {
2678
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
2685
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
2679
2686
  const providerOptions = options.providerOptions || {};
2680
2687
  const llmgatewayOptions = providerOptions.llmgateway || {};
2681
2688
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), llmgatewayOptions);
@@ -2699,19 +2706,31 @@ var LLMGatewayChatLanguageModel = class {
2699
2706
  throw new Error("No choice in response");
2700
2707
  }
2701
2708
  const usageInfo = response.usage ? {
2702
- inputTokens: (_b16 = response.usage.prompt_tokens) != null ? _b16 : 0,
2703
- outputTokens: (_c = response.usage.completion_tokens) != null ? _c : 0,
2704
- totalTokens: ((_d = response.usage.prompt_tokens) != null ? _d : 0) + ((_e = response.usage.completion_tokens) != null ? _e : 0),
2705
- reasoningTokens: (_g = (_f = response.usage.completion_tokens_details) == null ? void 0 : _f.reasoning_tokens) != null ? _g : 0,
2706
- cachedInputTokens: (_i = (_h = response.usage.prompt_tokens_details) == null ? void 0 : _h.cached_tokens) != null ? _i : 0
2709
+ inputTokens: {
2710
+ total: (_b16 = response.usage.prompt_tokens) != null ? _b16 : void 0,
2711
+ noCache: void 0,
2712
+ cacheRead: (_d = (_c = response.usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : void 0,
2713
+ cacheWrite: void 0
2714
+ },
2715
+ outputTokens: {
2716
+ total: (_e = response.usage.completion_tokens) != null ? _e : void 0,
2717
+ text: void 0,
2718
+ reasoning: (_g = (_f = response.usage.completion_tokens_details) == null ? void 0 : _f.reasoning_tokens) != null ? _g : void 0
2719
+ }
2707
2720
  } : {
2708
- inputTokens: 0,
2709
- outputTokens: 0,
2710
- totalTokens: 0,
2711
- reasoningTokens: 0,
2712
- cachedInputTokens: 0
2721
+ inputTokens: {
2722
+ total: void 0,
2723
+ noCache: void 0,
2724
+ cacheRead: void 0,
2725
+ cacheWrite: void 0
2726
+ },
2727
+ outputTokens: {
2728
+ total: void 0,
2729
+ text: void 0,
2730
+ reasoning: void 0
2731
+ }
2713
2732
  };
2714
- const reasoningDetails = (_j = choice.message.reasoning_details) != null ? _j : [];
2733
+ const reasoningDetails = (_h = choice.message.reasoning_details) != null ? _h : [];
2715
2734
  const reasoning = reasoningDetails.length > 0 ? reasoningDetails.map((detail) => {
2716
2735
  switch (detail.type) {
2717
2736
  case "reasoning.text" /* Text */: {
@@ -2767,7 +2786,7 @@ var LLMGatewayChatLanguageModel = class {
2767
2786
  for (const toolCall of choice.message.tool_calls) {
2768
2787
  content.push({
2769
2788
  type: "tool-call",
2770
- toolCallId: (_k = toolCall.id) != null ? _k : generateId(),
2789
+ toolCallId: (_i = toolCall.id) != null ? _i : generateId(),
2771
2790
  toolName: toolCall.function.name,
2772
2791
  input: toolCall.function.arguments
2773
2792
  });
@@ -2808,18 +2827,18 @@ var LLMGatewayChatLanguageModel = class {
2808
2827
  providerMetadata: includeUsageAccounting ? {
2809
2828
  llmgateway: {
2810
2829
  usage: {
2811
- promptTokens: (_l = usageInfo.inputTokens) != null ? _l : 0,
2812
- completionTokens: (_m = usageInfo.outputTokens) != null ? _m : 0,
2813
- totalTokens: (_n = usageInfo.totalTokens) != null ? _n : 0,
2814
- cost: typeof ((_o = response.usage) == null ? void 0 : _o.cost) === "number" ? response.usage.cost : (_q = (_p = response.usage) == null ? void 0 : _p.cost) == null ? void 0 : _q.total_cost,
2830
+ promptTokens: (_j = usageInfo.inputTokens.total) != null ? _j : 0,
2831
+ completionTokens: (_k = usageInfo.outputTokens.total) != null ? _k : 0,
2832
+ totalTokens: ((_l = usageInfo.inputTokens.total) != null ? _l : 0) + ((_m = usageInfo.outputTokens.total) != null ? _m : 0),
2833
+ cost: typeof ((_n = response.usage) == null ? void 0 : _n.cost) === "number" ? response.usage.cost : (_p = (_o = response.usage) == null ? void 0 : _o.cost) == null ? void 0 : _p.total_cost,
2815
2834
  promptTokensDetails: {
2816
- cachedTokens: (_t = (_s = (_r = response.usage) == null ? void 0 : _r.prompt_tokens_details) == null ? void 0 : _s.cached_tokens) != null ? _t : 0
2835
+ cachedTokens: (_s = (_r = (_q = response.usage) == null ? void 0 : _q.prompt_tokens_details) == null ? void 0 : _r.cached_tokens) != null ? _s : 0
2817
2836
  },
2818
2837
  completionTokensDetails: {
2819
- reasoningTokens: (_w = (_v = (_u = response.usage) == null ? void 0 : _u.completion_tokens_details) == null ? void 0 : _v.reasoning_tokens) != null ? _w : 0
2838
+ reasoningTokens: (_v = (_u = (_t = response.usage) == null ? void 0 : _t.completion_tokens_details) == null ? void 0 : _u.reasoning_tokens) != null ? _v : 0
2820
2839
  },
2821
2840
  costDetails: {
2822
- upstreamInferenceCost: (_z = (_y = (_x = response.usage) == null ? void 0 : _x.cost_details) == null ? void 0 : _y.upstream_inference_cost) != null ? _z : 0
2841
+ upstreamInferenceCost: (_y = (_x = (_w = response.usage) == null ? void 0 : _w.cost_details) == null ? void 0 : _x.upstream_inference_cost) != null ? _y : 0
2823
2842
  }
2824
2843
  }
2825
2844
  }
@@ -2855,13 +2874,19 @@ var LLMGatewayChatLanguageModel = class {
2855
2874
  fetch: this.config.fetch
2856
2875
  });
2857
2876
  const toolCalls = [];
2858
- let finishReason = "other";
2877
+ let finishReason = { unified: "other", raw: void 0 };
2859
2878
  const usage = {
2860
- inputTokens: Number.NaN,
2861
- outputTokens: Number.NaN,
2862
- totalTokens: Number.NaN,
2863
- reasoningTokens: Number.NaN,
2864
- cachedInputTokens: Number.NaN
2879
+ inputTokens: {
2880
+ total: void 0,
2881
+ noCache: void 0,
2882
+ cacheRead: void 0,
2883
+ cacheWrite: void 0
2884
+ },
2885
+ outputTokens: {
2886
+ total: void 0,
2887
+ text: void 0,
2888
+ reasoning: void 0
2889
+ }
2865
2890
  };
2866
2891
  const llmgatewayUsage = {};
2867
2892
  let textStarted = false;
@@ -2875,13 +2900,13 @@ var LLMGatewayChatLanguageModel = class {
2875
2900
  transform(chunk, controller) {
2876
2901
  var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
2877
2902
  if (!chunk.success) {
2878
- finishReason = "error";
2903
+ finishReason = { unified: "error", raw: void 0 };
2879
2904
  controller.enqueue({ type: "error", error: chunk.error });
2880
2905
  return;
2881
2906
  }
2882
2907
  const value = chunk.value;
2883
2908
  if ("error" in value) {
2884
- finishReason = "error";
2909
+ finishReason = { unified: "error", raw: void 0 };
2885
2910
  controller.enqueue({ type: "error", error: value.error });
2886
2911
  return;
2887
2912
  }
@@ -2899,13 +2924,12 @@ var LLMGatewayChatLanguageModel = class {
2899
2924
  });
2900
2925
  }
2901
2926
  if (value.usage != null) {
2902
- usage.inputTokens = value.usage.prompt_tokens;
2903
- usage.outputTokens = value.usage.completion_tokens;
2904
- usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
2927
+ usage.inputTokens.total = value.usage.prompt_tokens;
2928
+ usage.outputTokens.total = value.usage.completion_tokens;
2905
2929
  llmgatewayUsage.promptTokens = value.usage.prompt_tokens;
2906
2930
  if (value.usage.prompt_tokens_details) {
2907
2931
  const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
2908
- usage.cachedInputTokens = cachedInputTokens;
2932
+ usage.inputTokens.cacheRead = cachedInputTokens;
2909
2933
  llmgatewayUsage.promptTokensDetails = {
2910
2934
  cachedTokens: cachedInputTokens
2911
2935
  };
@@ -2913,7 +2937,7 @@ var LLMGatewayChatLanguageModel = class {
2913
2937
  llmgatewayUsage.completionTokens = value.usage.completion_tokens;
2914
2938
  if (value.usage.completion_tokens_details) {
2915
2939
  const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
2916
- usage.reasoningTokens = reasoningTokens;
2940
+ usage.outputTokens.reasoning = reasoningTokens;
2917
2941
  llmgatewayUsage.completionTokensDetails = {
2918
2942
  reasoningTokens
2919
2943
  };
@@ -3096,7 +3120,7 @@ var LLMGatewayChatLanguageModel = class {
3096
3120
  },
3097
3121
  flush(controller) {
3098
3122
  var _a16;
3099
- if (finishReason === "tool-calls") {
3123
+ if (finishReason.unified === "tool-calls") {
3100
3124
  for (const toolCall of toolCalls) {
3101
3125
  if (toolCall && !toolCall.sent) {
3102
3126
  controller.enqueue({
@@ -3135,7 +3159,6 @@ var LLMGatewayChatLanguageModel = class {
3135
3159
  }
3136
3160
  })
3137
3161
  ),
3138
- warnings: [],
3139
3162
  request: { body: args },
3140
3163
  response: { headers: responseHeaders }
3141
3164
  };
@@ -3287,7 +3310,7 @@ var LLMGatewayCompletionChunkSchema = z7.union([
3287
3310
  // src/completion/index.ts
3288
3311
  var LLMGatewayCompletionLanguageModel = class {
3289
3312
  constructor(modelId, settings, config) {
3290
- this.specificationVersion = "v2";
3313
+ this.specificationVersion = "v3";
3291
3314
  this.provider = "llmgateway";
3292
3315
  this.supportedUrls = {
3293
3316
  "image/*": [
@@ -3297,7 +3320,6 @@ var LLMGatewayCompletionLanguageModel = class {
3297
3320
  "text/*": [/^data:text\//, /^https?:\/\/.+$/],
3298
3321
  "application/*": [/^data:application\//, /^https?:\/\/.+$/]
3299
3322
  };
3300
- this.defaultObjectGenerationMode = void 0;
3301
3323
  this.modelId = modelId;
3302
3324
  this.settings = settings;
3303
3325
  this.config = config;
@@ -3357,7 +3379,7 @@ var LLMGatewayCompletionLanguageModel = class {
3357
3379
  }, this.config.extraBody), this.settings.extraBody);
3358
3380
  }
3359
3381
  async doGenerate(options) {
3360
- var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
3382
+ var _a16, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k;
3361
3383
  const providerOptions = options.providerOptions || {};
3362
3384
  const llmgatewayOptions = providerOptions.llmgateway || {};
3363
3385
  const args = __spreadValues(__spreadValues({}, this.getArgs(options)), llmgatewayOptions);
@@ -3391,11 +3413,17 @@ var LLMGatewayCompletionLanguageModel = class {
3391
3413
  ],
3392
3414
  finishReason: mapLLMGatewayFinishReason(choice.finish_reason),
3393
3415
  usage: {
3394
- inputTokens: (_c = (_b16 = response.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : 0,
3395
- outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : 0,
3396
- totalTokens: ((_g = (_f = response.usage) == null ? void 0 : _f.prompt_tokens) != null ? _g : 0) + ((_i = (_h = response.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : 0),
3397
- reasoningTokens: (_l = (_k = (_j = response.usage) == null ? void 0 : _j.completion_tokens_details) == null ? void 0 : _k.reasoning_tokens) != null ? _l : 0,
3398
- cachedInputTokens: (_o = (_n = (_m = response.usage) == null ? void 0 : _m.prompt_tokens_details) == null ? void 0 : _n.cached_tokens) != null ? _o : 0
3416
+ inputTokens: {
3417
+ total: (_c = (_b16 = response.usage) == null ? void 0 : _b16.prompt_tokens) != null ? _c : void 0,
3418
+ noCache: void 0,
3419
+ cacheRead: (_f = (_e = (_d = response.usage) == null ? void 0 : _d.prompt_tokens_details) == null ? void 0 : _e.cached_tokens) != null ? _f : void 0,
3420
+ cacheWrite: void 0
3421
+ },
3422
+ outputTokens: {
3423
+ total: (_h = (_g = response.usage) == null ? void 0 : _g.completion_tokens) != null ? _h : void 0,
3424
+ text: void 0,
3425
+ reasoning: (_k = (_j = (_i = response.usage) == null ? void 0 : _i.completion_tokens_details) == null ? void 0 : _j.reasoning_tokens) != null ? _k : void 0
3426
+ }
3399
3427
  },
3400
3428
  warnings: [],
3401
3429
  response: {
@@ -3425,13 +3453,19 @@ var LLMGatewayCompletionLanguageModel = class {
3425
3453
  abortSignal: options.abortSignal,
3426
3454
  fetch: this.config.fetch
3427
3455
  });
3428
- let finishReason = "other";
3456
+ let finishReason = { unified: "other", raw: void 0 };
3429
3457
  const usage = {
3430
- inputTokens: Number.NaN,
3431
- outputTokens: Number.NaN,
3432
- totalTokens: Number.NaN,
3433
- reasoningTokens: Number.NaN,
3434
- cachedInputTokens: Number.NaN
3458
+ inputTokens: {
3459
+ total: void 0,
3460
+ noCache: void 0,
3461
+ cacheRead: void 0,
3462
+ cacheWrite: void 0
3463
+ },
3464
+ outputTokens: {
3465
+ total: void 0,
3466
+ text: void 0,
3467
+ reasoning: void 0
3468
+ }
3435
3469
  };
3436
3470
  const llmgatewayUsage = {};
3437
3471
  return {
@@ -3440,24 +3474,23 @@ var LLMGatewayCompletionLanguageModel = class {
3440
3474
  transform(chunk, controller) {
3441
3475
  var _a16, _b16, _c;
3442
3476
  if (!chunk.success) {
3443
- finishReason = "error";
3477
+ finishReason = { unified: "error", raw: void 0 };
3444
3478
  controller.enqueue({ type: "error", error: chunk.error });
3445
3479
  return;
3446
3480
  }
3447
3481
  const value = chunk.value;
3448
3482
  if ("error" in value) {
3449
- finishReason = "error";
3483
+ finishReason = { unified: "error", raw: void 0 };
3450
3484
  controller.enqueue({ type: "error", error: value.error });
3451
3485
  return;
3452
3486
  }
3453
3487
  if (value.usage != null) {
3454
- usage.inputTokens = value.usage.prompt_tokens;
3455
- usage.outputTokens = value.usage.completion_tokens;
3456
- usage.totalTokens = value.usage.prompt_tokens + value.usage.completion_tokens;
3488
+ usage.inputTokens.total = value.usage.prompt_tokens;
3489
+ usage.outputTokens.total = value.usage.completion_tokens;
3457
3490
  llmgatewayUsage.promptTokens = value.usage.prompt_tokens;
3458
3491
  if (value.usage.prompt_tokens_details) {
3459
3492
  const cachedInputTokens = (_a16 = value.usage.prompt_tokens_details.cached_tokens) != null ? _a16 : 0;
3460
- usage.cachedInputTokens = cachedInputTokens;
3493
+ usage.inputTokens.cacheRead = cachedInputTokens;
3461
3494
  llmgatewayUsage.promptTokensDetails = {
3462
3495
  cachedTokens: cachedInputTokens
3463
3496
  };
@@ -3465,7 +3498,7 @@ var LLMGatewayCompletionLanguageModel = class {
3465
3498
  llmgatewayUsage.completionTokens = value.usage.completion_tokens;
3466
3499
  if (value.usage.completion_tokens_details) {
3467
3500
  const reasoningTokens = (_b16 = value.usage.completion_tokens_details.reasoning_tokens) != null ? _b16 : 0;
3468
- usage.reasoningTokens = reasoningTokens;
3501
+ usage.outputTokens.reasoning = reasoningTokens;
3469
3502
  llmgatewayUsage.completionTokensDetails = {
3470
3503
  reasoningTokens
3471
3504
  };
@@ -3558,13 +3591,19 @@ var LLMGatewayImageModel = class {
3558
3591
  if (options.aspectRatio != null) {
3559
3592
  body.aspect_ratio = options.aspectRatio;
3560
3593
  }
3594
+ if (options.quality != null) {
3595
+ body.quality = options.quality;
3596
+ }
3597
+ const providerOptions = options.providerOptions || {};
3598
+ const llmgatewayOptions = providerOptions.llmgateway || {};
3599
+ const requestBody = __spreadValues(__spreadValues(__spreadValues(__spreadValues({}, body), this.config.extraBody), this.settings.extraBody), llmgatewayOptions);
3561
3600
  const { value: response, responseHeaders } = await postJsonToApi({
3562
3601
  url: this.config.url({
3563
3602
  path: hasFiles ? "/images/edits" : "/images/generations",
3564
3603
  modelId: this.modelId
3565
3604
  }),
3566
3605
  headers: combineHeaders(this.config.headers(), options.headers),
3567
- body,
3606
+ body: requestBody,
3568
3607
  failedResponseHandler: llmgatewayFailedResponseHandler,
3569
3608
  successfulResponseHandler: createJsonResponseHandler(
3570
3609
  LLMGatewayImageResponseSchema