@ai-sdk/openai 2.0.130 → 2.0.131

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.
@@ -3,11 +3,11 @@ import {
3
3
  InvalidResponseDataError
4
4
  } from "@ai-sdk/provider";
5
5
  import {
6
+ StreamingToolCallTracker,
6
7
  combineHeaders,
7
8
  createEventSourceResponseHandler,
8
9
  createJsonResponseHandler,
9
10
  generateId,
10
- isParsableJson,
11
11
  parseProviderOptions,
12
12
  postJsonToApi
13
13
  } from "@ai-sdk/provider-utils";
@@ -1026,7 +1026,7 @@ var OpenAIChatLanguageModel = class {
1026
1026
  abortSignal: options.abortSignal,
1027
1027
  fetch: this.config.fetch
1028
1028
  });
1029
- const toolCalls = [];
1029
+ let toolCallTracker;
1030
1030
  let finishReason = "unknown";
1031
1031
  const usage = {
1032
1032
  inputTokens: void 0,
@@ -1040,10 +1040,14 @@ var OpenAIChatLanguageModel = class {
1040
1040
  stream: response.pipeThrough(
1041
1041
  new TransformStream({
1042
1042
  start(controller) {
1043
+ toolCallTracker = new StreamingToolCallTracker(controller, {
1044
+ generateId,
1045
+ typeValidation: "if-present"
1046
+ });
1043
1047
  controller.enqueue({ type: "stream-start", warnings });
1044
1048
  },
1045
1049
  transform(chunk, controller) {
1046
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
1050
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
1047
1051
  if (options.includeRawChunks) {
1048
1052
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1049
1053
  }
@@ -1110,90 +1114,7 @@ var OpenAIChatLanguageModel = class {
1110
1114
  }
1111
1115
  if (delta.tool_calls != null) {
1112
1116
  for (const toolCallDelta of delta.tool_calls) {
1113
- const index = toolCallDelta.index;
1114
- if (toolCalls[index] == null) {
1115
- if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
1116
- throw new InvalidResponseDataError({
1117
- data: toolCallDelta,
1118
- message: `Expected 'function' type.`
1119
- });
1120
- }
1121
- if (toolCallDelta.id == null) {
1122
- throw new InvalidResponseDataError({
1123
- data: toolCallDelta,
1124
- message: `Expected 'id' to be a string.`
1125
- });
1126
- }
1127
- if (((_n = toolCallDelta.function) == null ? void 0 : _n.name) == null) {
1128
- throw new InvalidResponseDataError({
1129
- data: toolCallDelta,
1130
- message: `Expected 'function.name' to be a string.`
1131
- });
1132
- }
1133
- controller.enqueue({
1134
- type: "tool-input-start",
1135
- id: toolCallDelta.id,
1136
- toolName: toolCallDelta.function.name
1137
- });
1138
- toolCalls[index] = {
1139
- id: toolCallDelta.id,
1140
- type: "function",
1141
- function: {
1142
- name: toolCallDelta.function.name,
1143
- arguments: (_o = toolCallDelta.function.arguments) != null ? _o : ""
1144
- },
1145
- hasFinished: false
1146
- };
1147
- const toolCall2 = toolCalls[index];
1148
- if (((_p = toolCall2.function) == null ? void 0 : _p.name) != null && ((_q = toolCall2.function) == null ? void 0 : _q.arguments) != null) {
1149
- if (toolCall2.function.arguments.length > 0) {
1150
- controller.enqueue({
1151
- type: "tool-input-delta",
1152
- id: toolCall2.id,
1153
- delta: toolCall2.function.arguments
1154
- });
1155
- }
1156
- if (isParsableJson(toolCall2.function.arguments)) {
1157
- controller.enqueue({
1158
- type: "tool-input-end",
1159
- id: toolCall2.id
1160
- });
1161
- controller.enqueue({
1162
- type: "tool-call",
1163
- toolCallId: (_r = toolCall2.id) != null ? _r : generateId(),
1164
- toolName: toolCall2.function.name,
1165
- input: toolCall2.function.arguments
1166
- });
1167
- toolCall2.hasFinished = true;
1168
- }
1169
- }
1170
- continue;
1171
- }
1172
- const toolCall = toolCalls[index];
1173
- if (toolCall.hasFinished) {
1174
- continue;
1175
- }
1176
- if (((_s = toolCallDelta.function) == null ? void 0 : _s.arguments) != null) {
1177
- toolCall.function.arguments += (_u = (_t = toolCallDelta.function) == null ? void 0 : _t.arguments) != null ? _u : "";
1178
- }
1179
- controller.enqueue({
1180
- type: "tool-input-delta",
1181
- id: toolCall.id,
1182
- delta: (_v = toolCallDelta.function.arguments) != null ? _v : ""
1183
- });
1184
- if (((_w = toolCall.function) == null ? void 0 : _w.name) != null && ((_x = toolCall.function) == null ? void 0 : _x.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1185
- controller.enqueue({
1186
- type: "tool-input-end",
1187
- id: toolCall.id
1188
- });
1189
- controller.enqueue({
1190
- type: "tool-call",
1191
- toolCallId: (_y = toolCall.id) != null ? _y : generateId(),
1192
- toolName: toolCall.function.name,
1193
- input: toolCall.function.arguments
1194
- });
1195
- toolCall.hasFinished = true;
1196
- }
1117
+ toolCallTracker.processDelta(toolCallDelta);
1197
1118
  }
1198
1119
  }
1199
1120
  if (delta.annotations != null) {
@@ -1212,6 +1133,7 @@ var OpenAIChatLanguageModel = class {
1212
1133
  if (isActiveText) {
1213
1134
  controller.enqueue({ type: "text-end", id: "0" });
1214
1135
  }
1136
+ toolCallTracker.flush();
1215
1137
  controller.enqueue({
1216
1138
  type: "finish",
1217
1139
  finishReason,