@ai-sdk/openai-compatible 2.0.79 → 2.0.80

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
@@ -3,6 +3,7 @@ import {
3
3
  InvalidResponseDataError
4
4
  } from "@ai-sdk/provider";
5
5
  import {
6
+ StreamingToolCallTracker,
6
7
  combineHeaders,
7
8
  createEventSourceResponseHandler,
8
9
  createJsonErrorResponseHandler,
@@ -658,12 +659,55 @@ var OpenAICompatibleChatLanguageModel = class {
658
659
  abortSignal: options.abortSignal,
659
660
  fetch: this.config.fetch
660
661
  });
661
- const toolCalls = [];
662
+ const providerOptionsName = metadataKey;
663
+ let toolCallTracker;
662
664
  const pendingToolCalls = /* @__PURE__ */ new Map();
665
+ const forwardedToolCallIndices = /* @__PURE__ */ new Set();
666
+ const processToolCallDelta = (toolCallDelta) => {
667
+ var _a2, _b, _c, _d, _e;
668
+ const index = toolCallDelta.index;
669
+ if (index == null || forwardedToolCallIndices.has(index)) {
670
+ toolCallTracker.processDelta(toolCallDelta);
671
+ return;
672
+ }
673
+ let pending = pendingToolCalls.get(index);
674
+ if (pending == null) {
675
+ pending = {
676
+ id: (_a2 = toolCallDelta.id) != null ? _a2 : null,
677
+ bufferedArguments: "",
678
+ extraContent: (_b = toolCallDelta.extra_content) != null ? _b : null
679
+ };
680
+ pendingToolCalls.set(index, pending);
681
+ } else {
682
+ if (pending.id == null && toolCallDelta.id != null) {
683
+ pending.id = toolCallDelta.id;
684
+ }
685
+ if (pending.extraContent == null && toolCallDelta.extra_content != null) {
686
+ pending.extraContent = toolCallDelta.extra_content;
687
+ }
688
+ }
689
+ const argumentsDelta = (_c = toolCallDelta.function) == null ? void 0 : _c.arguments;
690
+ if (argumentsDelta != null) {
691
+ pending.bufferedArguments += argumentsDelta;
692
+ }
693
+ const name = (_d = toolCallDelta.function) == null ? void 0 : _d.name;
694
+ if (name != null) {
695
+ toolCallTracker.processDelta({
696
+ index,
697
+ id: pending.id,
698
+ function: {
699
+ name,
700
+ arguments: pending.bufferedArguments
701
+ },
702
+ extra_content: (_e = pending.extraContent) != null ? _e : void 0
703
+ });
704
+ pendingToolCalls.delete(index);
705
+ forwardedToolCallIndices.add(index);
706
+ }
707
+ };
663
708
  let finishReason;
664
709
  let usage = void 0;
665
710
  let isFirstChunk = true;
666
- const providerOptionsName = metadataKey;
667
711
  let isActiveReasoning = false;
668
712
  let isActiveText = false;
669
713
  const convertUsage = (usage2) => this.convertUsage(usage2);
@@ -671,10 +715,22 @@ var OpenAICompatibleChatLanguageModel = class {
671
715
  stream: response.pipeThrough(
672
716
  new TransformStream({
673
717
  start(controller) {
718
+ toolCallTracker = new StreamingToolCallTracker(
719
+ controller,
720
+ {
721
+ generateId,
722
+ extractMetadata: (delta) => {
723
+ var _a2, _b;
724
+ const thoughtSignature = (_b = (_a2 = delta.extra_content) == null ? void 0 : _a2.google) == null ? void 0 : _b.thought_signature;
725
+ return thoughtSignature ? { [providerOptionsName]: { thoughtSignature } } : void 0;
726
+ },
727
+ buildToolCallProviderMetadata: (metadata) => metadata
728
+ }
729
+ );
674
730
  controller.enqueue({ type: "stream-start", warnings });
675
731
  },
676
732
  transform(chunk, controller) {
677
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
733
+ var _a2, _b;
678
734
  if (options.includeRawChunks) {
679
735
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
680
736
  }
@@ -772,113 +828,12 @@ var OpenAICompatibleChatLanguageModel = class {
772
828
  isActiveReasoning = false;
773
829
  }
774
830
  for (const toolCallDelta of delta.tool_calls) {
775
- const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length;
776
- if (toolCalls[index] == null) {
777
- if (toolCallDelta.index != null) {
778
- let pending = pendingToolCalls.get(index);
779
- if (pending == null) {
780
- pending = {
781
- id: (_d = toolCallDelta.id) != null ? _d : null,
782
- bufferedArguments: "",
783
- thoughtSignature: (_g = (_f = (_e = toolCallDelta.extra_content) == null ? void 0 : _e.google) == null ? void 0 : _f.thought_signature) != null ? _g : void 0
784
- };
785
- pendingToolCalls.set(index, pending);
786
- } else {
787
- if (pending.id == null && toolCallDelta.id != null) {
788
- pending.id = toolCallDelta.id;
789
- }
790
- if (pending.thoughtSignature == null && ((_i = (_h = toolCallDelta.extra_content) == null ? void 0 : _h.google) == null ? void 0 : _i.thought_signature) != null) {
791
- pending.thoughtSignature = toolCallDelta.extra_content.google.thought_signature;
792
- }
793
- }
794
- const argumentsDelta = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments;
795
- if (argumentsDelta != null) {
796
- pending.bufferedArguments += argumentsDelta;
797
- }
798
- const name = (_k = toolCallDelta.function) == null ? void 0 : _k.name;
799
- if (name == null) {
800
- continue;
801
- }
802
- pendingToolCalls.delete(index);
803
- if (pending.id == null) {
804
- throw new InvalidResponseDataError({
805
- data: toolCallDelta,
806
- message: `Expected 'id' to be a string.`
807
- });
808
- }
809
- controller.enqueue({
810
- type: "tool-input-start",
811
- id: pending.id,
812
- toolName: name
813
- });
814
- toolCalls[index] = {
815
- id: pending.id,
816
- type: "function",
817
- function: {
818
- name,
819
- arguments: pending.bufferedArguments
820
- },
821
- hasFinished: false,
822
- thoughtSignature: pending.thoughtSignature
823
- };
824
- } else {
825
- if (toolCallDelta.id == null) {
826
- throw new InvalidResponseDataError({
827
- data: toolCallDelta,
828
- message: `Expected 'id' to be a string.`
829
- });
830
- }
831
- if (((_l = toolCallDelta.function) == null ? void 0 : _l.name) == null) {
832
- throw new InvalidResponseDataError({
833
- data: toolCallDelta,
834
- message: `Expected 'function.name' to be a string.`
835
- });
836
- }
837
- controller.enqueue({
838
- type: "tool-input-start",
839
- id: toolCallDelta.id,
840
- toolName: toolCallDelta.function.name
841
- });
842
- toolCalls[index] = {
843
- id: toolCallDelta.id,
844
- type: "function",
845
- function: {
846
- name: toolCallDelta.function.name,
847
- arguments: (_m = toolCallDelta.function.arguments) != null ? _m : ""
848
- },
849
- hasFinished: false,
850
- thoughtSignature: (_p = (_o = (_n = toolCallDelta.extra_content) == null ? void 0 : _n.google) == null ? void 0 : _o.thought_signature) != null ? _p : void 0
851
- };
852
- }
853
- const toolCall2 = toolCalls[index];
854
- if (((_q = toolCall2.function) == null ? void 0 : _q.name) != null && ((_r = toolCall2.function) == null ? void 0 : _r.arguments) != null) {
855
- if (toolCall2.function.arguments.length > 0) {
856
- controller.enqueue({
857
- type: "tool-input-delta",
858
- id: toolCall2.id,
859
- delta: toolCall2.function.arguments
860
- });
861
- }
862
- }
863
- continue;
864
- }
865
- const toolCall = toolCalls[index];
866
- if (toolCall.hasFinished) {
867
- continue;
868
- }
869
- if (((_s = toolCallDelta.function) == null ? void 0 : _s.arguments) != null) {
870
- toolCall.function.arguments += (_u = (_t = toolCallDelta.function) == null ? void 0 : _t.arguments) != null ? _u : "";
871
- }
872
- controller.enqueue({
873
- type: "tool-input-delta",
874
- id: toolCall.id,
875
- delta: (_v = toolCallDelta.function.arguments) != null ? _v : ""
876
- });
831
+ processToolCallDelta(toolCallDelta);
877
832
  }
878
833
  }
879
834
  },
880
835
  flush(controller) {
881
- var _a2, _b, _c, _d, _e;
836
+ var _a2, _b, _c, _d;
882
837
  if (isActiveReasoning) {
883
838
  controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
884
839
  }
@@ -886,36 +841,14 @@ var OpenAICompatibleChatLanguageModel = class {
886
841
  controller.enqueue({ type: "text-end", id: "txt-0" });
887
842
  }
888
843
  for (const [index, pending] of pendingToolCalls) {
889
- throw new InvalidResponseDataError({
890
- data: {
891
- index,
892
- id: pending.id,
893
- function: { arguments: pending.bufferedArguments }
894
- },
895
- message: `Expected 'function.name' to be a string.`
896
- });
897
- }
898
- for (const toolCall of toolCalls.filter(
899
- (toolCall2) => !toolCall2.hasFinished
900
- )) {
901
- controller.enqueue({
902
- type: "tool-input-end",
903
- id: toolCall.id
904
- });
905
- controller.enqueue({
906
- type: "tool-call",
907
- toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
908
- toolName: toolCall.function.name,
909
- input: toolCall.function.arguments,
910
- ...toolCall.thoughtSignature ? {
911
- providerMetadata: {
912
- [providerOptionsName]: {
913
- thoughtSignature: toolCall.thoughtSignature
914
- }
915
- }
916
- } : {}
844
+ toolCallTracker.processDelta({
845
+ index,
846
+ id: pending.id,
847
+ function: { arguments: pending.bufferedArguments }
917
848
  });
918
849
  }
850
+ pendingToolCalls.clear();
851
+ toolCallTracker.flush();
919
852
  if (finishReason == null) {
920
853
  finishReason = { unified: "error", raw: void 0 };
921
854
  controller.enqueue({
@@ -930,11 +863,11 @@ var OpenAICompatibleChatLanguageModel = class {
930
863
  [providerOptionsName]: {},
931
864
  ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
932
865
  };
933
- if (((_b = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b.accepted_prediction_tokens) != null) {
934
- providerMetadata[providerOptionsName].acceptedPredictionTokens = (_c = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c.accepted_prediction_tokens;
866
+ if (((_a2 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _a2.accepted_prediction_tokens) != null) {
867
+ providerMetadata[providerOptionsName].acceptedPredictionTokens = (_b = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b.accepted_prediction_tokens;
935
868
  }
936
- if (((_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens) != null) {
937
- providerMetadata[providerOptionsName].rejectedPredictionTokens = (_e = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _e.rejected_prediction_tokens;
869
+ if (((_c = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c.rejected_prediction_tokens) != null) {
870
+ providerMetadata[providerOptionsName].rejectedPredictionTokens = (_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens;
938
871
  }
939
872
  controller.enqueue({
940
873
  type: "finish",
@@ -1792,7 +1725,7 @@ import {
1792
1725
  } from "@ai-sdk/provider-utils";
1793
1726
 
1794
1727
  // src/version.ts
1795
- var VERSION = true ? "2.0.79" : "0.0.0-test";
1728
+ var VERSION = true ? "2.0.80" : "0.0.0-test";
1796
1729
 
1797
1730
  // src/openai-compatible-provider.ts
1798
1731
  function createOpenAICompatible(options) {