@ai-sdk/openai-compatible 1.0.48 → 1.0.52

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
@@ -1,5 +1,33 @@
1
1
  # @ai-sdk/openai-compatible
2
2
 
3
+ ## 1.0.52
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [77d33c0]
8
+ - @ai-sdk/provider-utils@3.0.35
9
+
10
+ ## 1.0.51
11
+
12
+ ### Patch Changes
13
+
14
+ - e264a35: Accept streaming tool calls without explicit indices and preserve choice-level usage with top-level precedence.
15
+
16
+ ## 1.0.50
17
+
18
+ ### Patch Changes
19
+
20
+ - Updated dependencies [2fff9f1]
21
+ - Updated dependencies [f364ea0]
22
+ - @ai-sdk/provider-utils@3.0.34
23
+
24
+ ## 1.0.49
25
+
26
+ ### Patch Changes
27
+
28
+ - Updated dependencies [9e8e087]
29
+ - @ai-sdk/provider-utils@3.0.33
30
+
3
31
  ## 1.0.48
4
32
 
5
33
  ### Patch Changes
package/dist/index.js CHANGED
@@ -512,12 +512,23 @@ var OpenAICompatibleChatLanguageModel = class {
512
512
  },
513
513
  totalTokens: void 0
514
514
  };
515
- let lastUsage = void 0;
515
+ let topLevelUsage = void 0;
516
+ let choiceUsage = void 0;
516
517
  let isFirstChunk = true;
517
518
  const providerOptionsName = this.providerOptionsName;
518
519
  let isActiveReasoning = false;
519
520
  let isActiveText = false;
520
521
  const convertUsage = (usage2) => this.convertUsage(usage2);
522
+ const setUsage = (tokenUsage) => {
523
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
524
+ usage.promptTokens = (_a2 = tokenUsage.prompt_tokens) != null ? _a2 : void 0;
525
+ usage.completionTokens = (_b = tokenUsage.completion_tokens) != null ? _b : void 0;
526
+ usage.totalTokens = (_c = tokenUsage.total_tokens) != null ? _c : void 0;
527
+ usage.completionTokensDetails.reasoningTokens = (_e = (_d = tokenUsage.completion_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : void 0;
528
+ usage.completionTokensDetails.acceptedPredictionTokens = (_g = (_f = tokenUsage.completion_tokens_details) == null ? void 0 : _f.accepted_prediction_tokens) != null ? _g : void 0;
529
+ usage.completionTokensDetails.rejectedPredictionTokens = (_i = (_h = tokenUsage.completion_tokens_details) == null ? void 0 : _h.rejected_prediction_tokens) != null ? _i : void 0;
530
+ usage.promptTokensDetails.cachedTokens = (_k = (_j = tokenUsage.prompt_tokens_details) == null ? void 0 : _j.cached_tokens) != null ? _k : void 0;
531
+ };
521
532
  return {
522
533
  stream: response.pipeThrough(
523
534
  new TransformStream({
@@ -526,7 +537,7 @@ var OpenAICompatibleChatLanguageModel = class {
526
537
  },
527
538
  // TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
528
539
  transform(chunk, controller) {
529
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
540
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
530
541
  if (options.includeRawChunks) {
531
542
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
532
543
  }
@@ -550,31 +561,16 @@ var OpenAICompatibleChatLanguageModel = class {
550
561
  });
551
562
  }
552
563
  if (value.usage != null) {
553
- lastUsage = value.usage;
554
- const {
555
- prompt_tokens,
556
- completion_tokens,
557
- total_tokens,
558
- prompt_tokens_details,
559
- completion_tokens_details
560
- } = value.usage;
561
- usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
562
- usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
563
- usage.totalTokens = total_tokens != null ? total_tokens : void 0;
564
- if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
565
- usage.completionTokensDetails.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
566
- }
567
- if ((completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens) != null) {
568
- usage.completionTokensDetails.acceptedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens;
569
- }
570
- if ((completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens) != null) {
571
- usage.completionTokensDetails.rejectedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens;
572
- }
573
- if ((prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens) != null) {
574
- usage.promptTokensDetails.cachedTokens = prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens;
575
- }
564
+ topLevelUsage = value.usage;
565
+ setUsage(value.usage);
576
566
  }
577
567
  const choice = value.choices[0];
568
+ if ((choice == null ? void 0 : choice.usage) != null) {
569
+ choiceUsage = choice.usage;
570
+ if (topLevelUsage == null) {
571
+ setUsage(choice.usage);
572
+ }
573
+ }
578
574
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
579
575
  finishReason = mapOpenAICompatibleFinishReason(
580
576
  choice.finish_reason
@@ -611,8 +607,11 @@ var OpenAICompatibleChatLanguageModel = class {
611
607
  });
612
608
  }
613
609
  if (delta.tool_calls != null) {
614
- for (const toolCallDelta of delta.tool_calls) {
615
- const index = toolCallDelta.index;
610
+ for (const [
611
+ fallbackIndex,
612
+ toolCallDelta
613
+ ] of delta.tool_calls.entries()) {
614
+ const index = (_b = toolCallDelta.index) != null ? _b : fallbackIndex;
616
615
  if (toolCalls[index] == null) {
617
616
  if (toolCallDelta.id == null) {
618
617
  throw new import_provider3.InvalidResponseDataError({
@@ -620,7 +619,7 @@ var OpenAICompatibleChatLanguageModel = class {
620
619
  message: `Expected 'id' to be a string.`
621
620
  });
622
621
  }
623
- if (((_b = toolCallDelta.function) == null ? void 0 : _b.name) == null) {
622
+ if (((_c = toolCallDelta.function) == null ? void 0 : _c.name) == null) {
624
623
  throw new import_provider3.InvalidResponseDataError({
625
624
  data: toolCallDelta,
626
625
  message: `Expected 'function.name' to be a string.`
@@ -636,12 +635,12 @@ var OpenAICompatibleChatLanguageModel = class {
636
635
  type: "function",
637
636
  function: {
638
637
  name: toolCallDelta.function.name,
639
- arguments: (_c = toolCallDelta.function.arguments) != null ? _c : ""
638
+ arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
640
639
  },
641
640
  hasFinished: false
642
641
  };
643
642
  const toolCall2 = toolCalls[index];
644
- if (((_d = toolCall2.function) == null ? void 0 : _d.name) != null && ((_e = toolCall2.function) == null ? void 0 : _e.arguments) != null) {
643
+ if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null) {
645
644
  if (toolCall2.function.arguments.length > 0) {
646
645
  controller.enqueue({
647
646
  type: "tool-input-delta",
@@ -656,7 +655,7 @@ var OpenAICompatibleChatLanguageModel = class {
656
655
  });
657
656
  controller.enqueue({
658
657
  type: "tool-call",
659
- toolCallId: (_f = toolCall2.id) != null ? _f : (0, import_provider_utils2.generateId)(),
658
+ toolCallId: (_g = toolCall2.id) != null ? _g : (0, import_provider_utils2.generateId)(),
660
659
  toolName: toolCall2.function.name,
661
660
  input: toolCall2.function.arguments
662
661
  });
@@ -669,22 +668,22 @@ var OpenAICompatibleChatLanguageModel = class {
669
668
  if (toolCall.hasFinished) {
670
669
  continue;
671
670
  }
672
- if (((_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null) {
673
- toolCall.function.arguments += (_i = (_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null ? _i : "";
671
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
672
+ toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
674
673
  }
675
674
  controller.enqueue({
676
675
  type: "tool-input-delta",
677
676
  id: toolCall.id,
678
- delta: (_j = toolCallDelta.function.arguments) != null ? _j : ""
677
+ delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
679
678
  });
680
- if (((_k = toolCall.function) == null ? void 0 : _k.name) != null && ((_l = toolCall.function) == null ? void 0 : _l.arguments) != null && (0, import_provider_utils2.isParsableJson)(toolCall.function.arguments)) {
679
+ if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && (0, import_provider_utils2.isParsableJson)(toolCall.function.arguments)) {
681
680
  controller.enqueue({
682
681
  type: "tool-input-end",
683
682
  id: toolCall.id
684
683
  });
685
684
  controller.enqueue({
686
685
  type: "tool-call",
687
- toolCallId: (_m = toolCall.id) != null ? _m : (0, import_provider_utils2.generateId)(),
686
+ toolCallId: (_n = toolCall.id) != null ? _n : (0, import_provider_utils2.generateId)(),
688
687
  toolName: toolCall.function.name,
689
688
  input: toolCall.function.arguments
690
689
  });
@@ -728,7 +727,7 @@ var OpenAICompatibleChatLanguageModel = class {
728
727
  controller.enqueue({
729
728
  type: "finish",
730
729
  finishReason,
731
- usage: convertUsage(lastUsage),
730
+ usage: convertUsage(topLevelUsage != null ? topLevelUsage : choiceUsage),
732
731
  providerMetadata
733
732
  });
734
733
  }
@@ -804,7 +803,7 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_v43.z.union(
804
803
  reasoning: import_v43.z.string().nullish(),
805
804
  tool_calls: import_v43.z.array(
806
805
  import_v43.z.object({
807
- index: import_v43.z.number(),
806
+ index: import_v43.z.number().nullish(),
808
807
  id: import_v43.z.string().nullish(),
809
808
  function: import_v43.z.object({
810
809
  name: import_v43.z.string().nullish(),
@@ -813,7 +812,8 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_v43.z.union(
813
812
  })
814
813
  ).nullish()
815
814
  }).nullish(),
816
- finish_reason: import_v43.z.string().nullish()
815
+ finish_reason: import_v43.z.string().nullish(),
816
+ usage: openaiCompatibleTokenUsageSchema
817
817
  })
818
818
  ),
819
819
  usage: openaiCompatibleTokenUsageSchema
@@ -1411,7 +1411,7 @@ var openaiCompatibleImageResponseSchema = import_v48.z.object({
1411
1411
  var import_provider_utils6 = require("@ai-sdk/provider-utils");
1412
1412
 
1413
1413
  // src/version.ts
1414
- var VERSION = true ? "1.0.48" : "0.0.0-test";
1414
+ var VERSION = true ? "1.0.52" : "0.0.0-test";
1415
1415
 
1416
1416
  // src/openai-compatible-provider.ts
1417
1417
  function createOpenAICompatible(options) {