@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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @ai-sdk/openai
2
2
 
3
+ ## 2.0.131
4
+
5
+ ### Patch Changes
6
+
7
+ - 14810cb: fix(provider-utils): preserve streamed tool calls with unreliable IDs and indices
8
+ - Updated dependencies [14810cb]
9
+ - @ai-sdk/provider-utils@3.0.40
10
+
3
11
  ## 2.0.130
4
12
 
5
13
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1037,7 +1037,7 @@ var OpenAIChatLanguageModel = class {
1037
1037
  abortSignal: options.abortSignal,
1038
1038
  fetch: this.config.fetch
1039
1039
  });
1040
- const toolCalls = [];
1040
+ let toolCallTracker;
1041
1041
  let finishReason = "unknown";
1042
1042
  const usage = {
1043
1043
  inputTokens: void 0,
@@ -1051,10 +1051,14 @@ var OpenAIChatLanguageModel = class {
1051
1051
  stream: response.pipeThrough(
1052
1052
  new TransformStream({
1053
1053
  start(controller) {
1054
+ toolCallTracker = new import_provider_utils5.StreamingToolCallTracker(controller, {
1055
+ generateId: import_provider_utils5.generateId,
1056
+ typeValidation: "if-present"
1057
+ });
1054
1058
  controller.enqueue({ type: "stream-start", warnings });
1055
1059
  },
1056
1060
  transform(chunk, controller) {
1057
- 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;
1061
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
1058
1062
  if (options.includeRawChunks) {
1059
1063
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1060
1064
  }
@@ -1121,90 +1125,7 @@ var OpenAIChatLanguageModel = class {
1121
1125
  }
1122
1126
  if (delta.tool_calls != null) {
1123
1127
  for (const toolCallDelta of delta.tool_calls) {
1124
- const index = toolCallDelta.index;
1125
- if (toolCalls[index] == null) {
1126
- if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
1127
- throw new import_provider3.InvalidResponseDataError({
1128
- data: toolCallDelta,
1129
- message: `Expected 'function' type.`
1130
- });
1131
- }
1132
- if (toolCallDelta.id == null) {
1133
- throw new import_provider3.InvalidResponseDataError({
1134
- data: toolCallDelta,
1135
- message: `Expected 'id' to be a string.`
1136
- });
1137
- }
1138
- if (((_n = toolCallDelta.function) == null ? void 0 : _n.name) == null) {
1139
- throw new import_provider3.InvalidResponseDataError({
1140
- data: toolCallDelta,
1141
- message: `Expected 'function.name' to be a string.`
1142
- });
1143
- }
1144
- controller.enqueue({
1145
- type: "tool-input-start",
1146
- id: toolCallDelta.id,
1147
- toolName: toolCallDelta.function.name
1148
- });
1149
- toolCalls[index] = {
1150
- id: toolCallDelta.id,
1151
- type: "function",
1152
- function: {
1153
- name: toolCallDelta.function.name,
1154
- arguments: (_o = toolCallDelta.function.arguments) != null ? _o : ""
1155
- },
1156
- hasFinished: false
1157
- };
1158
- const toolCall2 = toolCalls[index];
1159
- if (((_p = toolCall2.function) == null ? void 0 : _p.name) != null && ((_q = toolCall2.function) == null ? void 0 : _q.arguments) != null) {
1160
- if (toolCall2.function.arguments.length > 0) {
1161
- controller.enqueue({
1162
- type: "tool-input-delta",
1163
- id: toolCall2.id,
1164
- delta: toolCall2.function.arguments
1165
- });
1166
- }
1167
- if ((0, import_provider_utils5.isParsableJson)(toolCall2.function.arguments)) {
1168
- controller.enqueue({
1169
- type: "tool-input-end",
1170
- id: toolCall2.id
1171
- });
1172
- controller.enqueue({
1173
- type: "tool-call",
1174
- toolCallId: (_r = toolCall2.id) != null ? _r : (0, import_provider_utils5.generateId)(),
1175
- toolName: toolCall2.function.name,
1176
- input: toolCall2.function.arguments
1177
- });
1178
- toolCall2.hasFinished = true;
1179
- }
1180
- }
1181
- continue;
1182
- }
1183
- const toolCall = toolCalls[index];
1184
- if (toolCall.hasFinished) {
1185
- continue;
1186
- }
1187
- if (((_s = toolCallDelta.function) == null ? void 0 : _s.arguments) != null) {
1188
- toolCall.function.arguments += (_u = (_t = toolCallDelta.function) == null ? void 0 : _t.arguments) != null ? _u : "";
1189
- }
1190
- controller.enqueue({
1191
- type: "tool-input-delta",
1192
- id: toolCall.id,
1193
- delta: (_v = toolCallDelta.function.arguments) != null ? _v : ""
1194
- });
1195
- if (((_w = toolCall.function) == null ? void 0 : _w.name) != null && ((_x = toolCall.function) == null ? void 0 : _x.arguments) != null && (0, import_provider_utils5.isParsableJson)(toolCall.function.arguments)) {
1196
- controller.enqueue({
1197
- type: "tool-input-end",
1198
- id: toolCall.id
1199
- });
1200
- controller.enqueue({
1201
- type: "tool-call",
1202
- toolCallId: (_y = toolCall.id) != null ? _y : (0, import_provider_utils5.generateId)(),
1203
- toolName: toolCall.function.name,
1204
- input: toolCall.function.arguments
1205
- });
1206
- toolCall.hasFinished = true;
1207
- }
1128
+ toolCallTracker.processDelta(toolCallDelta);
1208
1129
  }
1209
1130
  }
1210
1131
  if (delta.annotations != null) {
@@ -1223,6 +1144,7 @@ var OpenAIChatLanguageModel = class {
1223
1144
  if (isActiveText) {
1224
1145
  controller.enqueue({ type: "text-end", id: "0" });
1225
1146
  }
1147
+ toolCallTracker.flush();
1226
1148
  controller.enqueue({
1227
1149
  type: "finish",
1228
1150
  finishReason,
@@ -5216,7 +5138,7 @@ var OpenAITranscriptionModel = class {
5216
5138
  };
5217
5139
 
5218
5140
  // src/version.ts
5219
- var VERSION = true ? "2.0.130" : "0.0.0-test";
5141
+ var VERSION = true ? "2.0.131" : "0.0.0-test";
5220
5142
 
5221
5143
  // src/openai-provider.ts
5222
5144
  function createOpenAI(options = {}) {