@ai-sdk/openai-compatible 1.0.49 → 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/dist/index.mjs CHANGED
@@ -496,12 +496,23 @@ var OpenAICompatibleChatLanguageModel = class {
496
496
  },
497
497
  totalTokens: void 0
498
498
  };
499
- let lastUsage = void 0;
499
+ let topLevelUsage = void 0;
500
+ let choiceUsage = void 0;
500
501
  let isFirstChunk = true;
501
502
  const providerOptionsName = this.providerOptionsName;
502
503
  let isActiveReasoning = false;
503
504
  let isActiveText = false;
504
505
  const convertUsage = (usage2) => this.convertUsage(usage2);
506
+ const setUsage = (tokenUsage) => {
507
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
508
+ usage.promptTokens = (_a2 = tokenUsage.prompt_tokens) != null ? _a2 : void 0;
509
+ usage.completionTokens = (_b = tokenUsage.completion_tokens) != null ? _b : void 0;
510
+ usage.totalTokens = (_c = tokenUsage.total_tokens) != null ? _c : void 0;
511
+ usage.completionTokensDetails.reasoningTokens = (_e = (_d = tokenUsage.completion_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : void 0;
512
+ usage.completionTokensDetails.acceptedPredictionTokens = (_g = (_f = tokenUsage.completion_tokens_details) == null ? void 0 : _f.accepted_prediction_tokens) != null ? _g : void 0;
513
+ usage.completionTokensDetails.rejectedPredictionTokens = (_i = (_h = tokenUsage.completion_tokens_details) == null ? void 0 : _h.rejected_prediction_tokens) != null ? _i : void 0;
514
+ usage.promptTokensDetails.cachedTokens = (_k = (_j = tokenUsage.prompt_tokens_details) == null ? void 0 : _j.cached_tokens) != null ? _k : void 0;
515
+ };
505
516
  return {
506
517
  stream: response.pipeThrough(
507
518
  new TransformStream({
@@ -510,7 +521,7 @@ var OpenAICompatibleChatLanguageModel = class {
510
521
  },
511
522
  // TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
512
523
  transform(chunk, controller) {
513
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
524
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
514
525
  if (options.includeRawChunks) {
515
526
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
516
527
  }
@@ -534,31 +545,16 @@ var OpenAICompatibleChatLanguageModel = class {
534
545
  });
535
546
  }
536
547
  if (value.usage != null) {
537
- lastUsage = value.usage;
538
- const {
539
- prompt_tokens,
540
- completion_tokens,
541
- total_tokens,
542
- prompt_tokens_details,
543
- completion_tokens_details
544
- } = value.usage;
545
- usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
546
- usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
547
- usage.totalTokens = total_tokens != null ? total_tokens : void 0;
548
- if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
549
- usage.completionTokensDetails.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
550
- }
551
- if ((completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens) != null) {
552
- usage.completionTokensDetails.acceptedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens;
553
- }
554
- if ((completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens) != null) {
555
- usage.completionTokensDetails.rejectedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens;
556
- }
557
- if ((prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens) != null) {
558
- usage.promptTokensDetails.cachedTokens = prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens;
559
- }
548
+ topLevelUsage = value.usage;
549
+ setUsage(value.usage);
560
550
  }
561
551
  const choice = value.choices[0];
552
+ if ((choice == null ? void 0 : choice.usage) != null) {
553
+ choiceUsage = choice.usage;
554
+ if (topLevelUsage == null) {
555
+ setUsage(choice.usage);
556
+ }
557
+ }
562
558
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
563
559
  finishReason = mapOpenAICompatibleFinishReason(
564
560
  choice.finish_reason
@@ -595,8 +591,11 @@ var OpenAICompatibleChatLanguageModel = class {
595
591
  });
596
592
  }
597
593
  if (delta.tool_calls != null) {
598
- for (const toolCallDelta of delta.tool_calls) {
599
- const index = toolCallDelta.index;
594
+ for (const [
595
+ fallbackIndex,
596
+ toolCallDelta
597
+ ] of delta.tool_calls.entries()) {
598
+ const index = (_b = toolCallDelta.index) != null ? _b : fallbackIndex;
600
599
  if (toolCalls[index] == null) {
601
600
  if (toolCallDelta.id == null) {
602
601
  throw new InvalidResponseDataError({
@@ -604,7 +603,7 @@ var OpenAICompatibleChatLanguageModel = class {
604
603
  message: `Expected 'id' to be a string.`
605
604
  });
606
605
  }
607
- if (((_b = toolCallDelta.function) == null ? void 0 : _b.name) == null) {
606
+ if (((_c = toolCallDelta.function) == null ? void 0 : _c.name) == null) {
608
607
  throw new InvalidResponseDataError({
609
608
  data: toolCallDelta,
610
609
  message: `Expected 'function.name' to be a string.`
@@ -620,12 +619,12 @@ var OpenAICompatibleChatLanguageModel = class {
620
619
  type: "function",
621
620
  function: {
622
621
  name: toolCallDelta.function.name,
623
- arguments: (_c = toolCallDelta.function.arguments) != null ? _c : ""
622
+ arguments: (_d = toolCallDelta.function.arguments) != null ? _d : ""
624
623
  },
625
624
  hasFinished: false
626
625
  };
627
626
  const toolCall2 = toolCalls[index];
628
- if (((_d = toolCall2.function) == null ? void 0 : _d.name) != null && ((_e = toolCall2.function) == null ? void 0 : _e.arguments) != null) {
627
+ if (((_e = toolCall2.function) == null ? void 0 : _e.name) != null && ((_f = toolCall2.function) == null ? void 0 : _f.arguments) != null) {
629
628
  if (toolCall2.function.arguments.length > 0) {
630
629
  controller.enqueue({
631
630
  type: "tool-input-delta",
@@ -640,7 +639,7 @@ var OpenAICompatibleChatLanguageModel = class {
640
639
  });
641
640
  controller.enqueue({
642
641
  type: "tool-call",
643
- toolCallId: (_f = toolCall2.id) != null ? _f : generateId(),
642
+ toolCallId: (_g = toolCall2.id) != null ? _g : generateId(),
644
643
  toolName: toolCall2.function.name,
645
644
  input: toolCall2.function.arguments
646
645
  });
@@ -653,22 +652,22 @@ var OpenAICompatibleChatLanguageModel = class {
653
652
  if (toolCall.hasFinished) {
654
653
  continue;
655
654
  }
656
- if (((_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null) {
657
- toolCall.function.arguments += (_i = (_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null ? _i : "";
655
+ if (((_h = toolCallDelta.function) == null ? void 0 : _h.arguments) != null) {
656
+ toolCall.function.arguments += (_j = (_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null ? _j : "";
658
657
  }
659
658
  controller.enqueue({
660
659
  type: "tool-input-delta",
661
660
  id: toolCall.id,
662
- delta: (_j = toolCallDelta.function.arguments) != null ? _j : ""
661
+ delta: (_k = toolCallDelta.function.arguments) != null ? _k : ""
663
662
  });
664
- if (((_k = toolCall.function) == null ? void 0 : _k.name) != null && ((_l = toolCall.function) == null ? void 0 : _l.arguments) != null && isParsableJson(toolCall.function.arguments)) {
663
+ if (((_l = toolCall.function) == null ? void 0 : _l.name) != null && ((_m = toolCall.function) == null ? void 0 : _m.arguments) != null && isParsableJson(toolCall.function.arguments)) {
665
664
  controller.enqueue({
666
665
  type: "tool-input-end",
667
666
  id: toolCall.id
668
667
  });
669
668
  controller.enqueue({
670
669
  type: "tool-call",
671
- toolCallId: (_m = toolCall.id) != null ? _m : generateId(),
670
+ toolCallId: (_n = toolCall.id) != null ? _n : generateId(),
672
671
  toolName: toolCall.function.name,
673
672
  input: toolCall.function.arguments
674
673
  });
@@ -712,7 +711,7 @@ var OpenAICompatibleChatLanguageModel = class {
712
711
  controller.enqueue({
713
712
  type: "finish",
714
713
  finishReason,
715
- usage: convertUsage(lastUsage),
714
+ usage: convertUsage(topLevelUsage != null ? topLevelUsage : choiceUsage),
716
715
  providerMetadata
717
716
  });
718
717
  }
@@ -788,7 +787,7 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([
788
787
  reasoning: z3.string().nullish(),
789
788
  tool_calls: z3.array(
790
789
  z3.object({
791
- index: z3.number(),
790
+ index: z3.number().nullish(),
792
791
  id: z3.string().nullish(),
793
792
  function: z3.object({
794
793
  name: z3.string().nullish(),
@@ -797,7 +796,8 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([
797
796
  })
798
797
  ).nullish()
799
798
  }).nullish(),
800
- finish_reason: z3.string().nullish()
799
+ finish_reason: z3.string().nullish(),
800
+ usage: openaiCompatibleTokenUsageSchema
801
801
  })
802
802
  ),
803
803
  usage: openaiCompatibleTokenUsageSchema
@@ -1421,7 +1421,7 @@ import {
1421
1421
  } from "@ai-sdk/provider-utils";
1422
1422
 
1423
1423
  // src/version.ts
1424
- var VERSION = true ? "1.0.49" : "0.0.0-test";
1424
+ var VERSION = true ? "1.0.52" : "0.0.0-test";
1425
1425
 
1426
1426
  // src/openai-compatible-provider.ts
1427
1427
  function createOpenAICompatible(options) {