@ai-sdk/openai 4.0.0-beta.35 → 4.0.0-beta.37

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.
@@ -1,14 +1,11 @@
1
1
  // src/chat/openai-chat-language-model.ts
2
2
  import {
3
- InvalidResponseDataError
4
- } from "@ai-sdk/provider";
5
- import {
3
+ StreamingToolCallTracker,
6
4
  combineHeaders,
7
5
  createEventSourceResponseHandler,
8
6
  createJsonResponseHandler,
9
7
  generateId,
10
8
  isCustomReasoning,
11
- isParsableJson,
12
9
  parseProviderOptions,
13
10
  postJsonToApi,
14
11
  serializeModelOptions,
@@ -950,7 +947,10 @@ var OpenAIChatLanguageModel = class _OpenAIChatLanguageModel {
950
947
  abortSignal: options.abortSignal,
951
948
  fetch: this.config.fetch
952
949
  });
953
- const toolCalls = [];
950
+ const toolCallTracker = new StreamingToolCallTracker({
951
+ generateId,
952
+ typeValidation: "if-present"
953
+ });
954
954
  let finishReason = {
955
955
  unified: "other",
956
956
  raw: void 0
@@ -966,7 +966,7 @@ var OpenAIChatLanguageModel = class _OpenAIChatLanguageModel {
966
966
  controller.enqueue({ type: "stream-start", warnings });
967
967
  },
968
968
  transform(chunk, controller) {
969
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
969
+ var _a2, _b2, _c, _d, _e;
970
970
  if (options.includeRawChunks) {
971
971
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
972
972
  }
@@ -1027,90 +1027,10 @@ var OpenAIChatLanguageModel = class _OpenAIChatLanguageModel {
1027
1027
  }
1028
1028
  if (delta.tool_calls != null) {
1029
1029
  for (const toolCallDelta of delta.tool_calls) {
1030
- const index = toolCallDelta.index;
1031
- if (toolCalls[index] == null) {
1032
- if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
1033
- throw new InvalidResponseDataError({
1034
- data: toolCallDelta,
1035
- message: `Expected 'function' type.`
1036
- });
1037
- }
1038
- if (toolCallDelta.id == null) {
1039
- throw new InvalidResponseDataError({
1040
- data: toolCallDelta,
1041
- message: `Expected 'id' to be a string.`
1042
- });
1043
- }
1044
- if (((_f = toolCallDelta.function) == null ? void 0 : _f.name) == null) {
1045
- throw new InvalidResponseDataError({
1046
- data: toolCallDelta,
1047
- message: `Expected 'function.name' to be a string.`
1048
- });
1049
- }
1050
- controller.enqueue({
1051
- type: "tool-input-start",
1052
- id: toolCallDelta.id,
1053
- toolName: toolCallDelta.function.name
1054
- });
1055
- toolCalls[index] = {
1056
- id: toolCallDelta.id,
1057
- type: "function",
1058
- function: {
1059
- name: toolCallDelta.function.name,
1060
- arguments: (_g = toolCallDelta.function.arguments) != null ? _g : ""
1061
- },
1062
- hasFinished: false
1063
- };
1064
- const toolCall2 = toolCalls[index];
1065
- if (((_h = toolCall2.function) == null ? void 0 : _h.name) != null && ((_i = toolCall2.function) == null ? void 0 : _i.arguments) != null) {
1066
- if (toolCall2.function.arguments.length > 0) {
1067
- controller.enqueue({
1068
- type: "tool-input-delta",
1069
- id: toolCall2.id,
1070
- delta: toolCall2.function.arguments
1071
- });
1072
- }
1073
- if (isParsableJson(toolCall2.function.arguments)) {
1074
- controller.enqueue({
1075
- type: "tool-input-end",
1076
- id: toolCall2.id
1077
- });
1078
- controller.enqueue({
1079
- type: "tool-call",
1080
- toolCallId: (_j = toolCall2.id) != null ? _j : generateId(),
1081
- toolName: toolCall2.function.name,
1082
- input: toolCall2.function.arguments
1083
- });
1084
- toolCall2.hasFinished = true;
1085
- }
1086
- }
1087
- continue;
1088
- }
1089
- const toolCall = toolCalls[index];
1090
- if (toolCall.hasFinished) {
1091
- continue;
1092
- }
1093
- if (((_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null) {
1094
- toolCall.function.arguments += (_m = (_l = toolCallDelta.function) == null ? void 0 : _l.arguments) != null ? _m : "";
1095
- }
1096
- controller.enqueue({
1097
- type: "tool-input-delta",
1098
- id: toolCall.id,
1099
- delta: (_n = toolCallDelta.function.arguments) != null ? _n : ""
1100
- });
1101
- if (((_o = toolCall.function) == null ? void 0 : _o.name) != null && ((_p = toolCall.function) == null ? void 0 : _p.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1102
- controller.enqueue({
1103
- type: "tool-input-end",
1104
- id: toolCall.id
1105
- });
1106
- controller.enqueue({
1107
- type: "tool-call",
1108
- toolCallId: (_q = toolCall.id) != null ? _q : generateId(),
1109
- toolName: toolCall.function.name,
1110
- input: toolCall.function.arguments
1111
- });
1112
- toolCall.hasFinished = true;
1113
- }
1030
+ toolCallTracker.processDelta(
1031
+ toolCallDelta,
1032
+ controller.enqueue.bind(controller)
1033
+ );
1114
1034
  }
1115
1035
  }
1116
1036
  if (delta.annotations != null) {
@@ -1129,6 +1049,7 @@ var OpenAIChatLanguageModel = class _OpenAIChatLanguageModel {
1129
1049
  if (isActiveText) {
1130
1050
  controller.enqueue({ type: "text-end", id: "0" });
1131
1051
  }
1052
+ toolCallTracker.flush(controller.enqueue.bind(controller));
1132
1053
  controller.enqueue({
1133
1054
  type: "finish",
1134
1055
  finishReason,