@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/dist/index.mjs CHANGED
@@ -11,11 +11,11 @@ import {
11
11
  InvalidResponseDataError
12
12
  } from "@ai-sdk/provider";
13
13
  import {
14
+ StreamingToolCallTracker,
14
15
  combineHeaders,
15
16
  createEventSourceResponseHandler,
16
17
  createJsonResponseHandler,
17
18
  generateId,
18
- isParsableJson,
19
19
  parseProviderOptions,
20
20
  postJsonToApi
21
21
  } from "@ai-sdk/provider-utils";
@@ -1034,7 +1034,7 @@ var OpenAIChatLanguageModel = class {
1034
1034
  abortSignal: options.abortSignal,
1035
1035
  fetch: this.config.fetch
1036
1036
  });
1037
- const toolCalls = [];
1037
+ let toolCallTracker;
1038
1038
  let finishReason = "unknown";
1039
1039
  const usage = {
1040
1040
  inputTokens: void 0,
@@ -1048,10 +1048,14 @@ var OpenAIChatLanguageModel = class {
1048
1048
  stream: response.pipeThrough(
1049
1049
  new TransformStream({
1050
1050
  start(controller) {
1051
+ toolCallTracker = new StreamingToolCallTracker(controller, {
1052
+ generateId,
1053
+ typeValidation: "if-present"
1054
+ });
1051
1055
  controller.enqueue({ type: "stream-start", warnings });
1052
1056
  },
1053
1057
  transform(chunk, controller) {
1054
- 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;
1058
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
1055
1059
  if (options.includeRawChunks) {
1056
1060
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1057
1061
  }
@@ -1118,90 +1122,7 @@ var OpenAIChatLanguageModel = class {
1118
1122
  }
1119
1123
  if (delta.tool_calls != null) {
1120
1124
  for (const toolCallDelta of delta.tool_calls) {
1121
- const index = toolCallDelta.index;
1122
- if (toolCalls[index] == null) {
1123
- if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
1124
- throw new InvalidResponseDataError({
1125
- data: toolCallDelta,
1126
- message: `Expected 'function' type.`
1127
- });
1128
- }
1129
- if (toolCallDelta.id == null) {
1130
- throw new InvalidResponseDataError({
1131
- data: toolCallDelta,
1132
- message: `Expected 'id' to be a string.`
1133
- });
1134
- }
1135
- if (((_n = toolCallDelta.function) == null ? void 0 : _n.name) == null) {
1136
- throw new InvalidResponseDataError({
1137
- data: toolCallDelta,
1138
- message: `Expected 'function.name' to be a string.`
1139
- });
1140
- }
1141
- controller.enqueue({
1142
- type: "tool-input-start",
1143
- id: toolCallDelta.id,
1144
- toolName: toolCallDelta.function.name
1145
- });
1146
- toolCalls[index] = {
1147
- id: toolCallDelta.id,
1148
- type: "function",
1149
- function: {
1150
- name: toolCallDelta.function.name,
1151
- arguments: (_o = toolCallDelta.function.arguments) != null ? _o : ""
1152
- },
1153
- hasFinished: false
1154
- };
1155
- const toolCall2 = toolCalls[index];
1156
- if (((_p = toolCall2.function) == null ? void 0 : _p.name) != null && ((_q = toolCall2.function) == null ? void 0 : _q.arguments) != null) {
1157
- if (toolCall2.function.arguments.length > 0) {
1158
- controller.enqueue({
1159
- type: "tool-input-delta",
1160
- id: toolCall2.id,
1161
- delta: toolCall2.function.arguments
1162
- });
1163
- }
1164
- if (isParsableJson(toolCall2.function.arguments)) {
1165
- controller.enqueue({
1166
- type: "tool-input-end",
1167
- id: toolCall2.id
1168
- });
1169
- controller.enqueue({
1170
- type: "tool-call",
1171
- toolCallId: (_r = toolCall2.id) != null ? _r : generateId(),
1172
- toolName: toolCall2.function.name,
1173
- input: toolCall2.function.arguments
1174
- });
1175
- toolCall2.hasFinished = true;
1176
- }
1177
- }
1178
- continue;
1179
- }
1180
- const toolCall = toolCalls[index];
1181
- if (toolCall.hasFinished) {
1182
- continue;
1183
- }
1184
- if (((_s = toolCallDelta.function) == null ? void 0 : _s.arguments) != null) {
1185
- toolCall.function.arguments += (_u = (_t = toolCallDelta.function) == null ? void 0 : _t.arguments) != null ? _u : "";
1186
- }
1187
- controller.enqueue({
1188
- type: "tool-input-delta",
1189
- id: toolCall.id,
1190
- delta: (_v = toolCallDelta.function.arguments) != null ? _v : ""
1191
- });
1192
- if (((_w = toolCall.function) == null ? void 0 : _w.name) != null && ((_x = toolCall.function) == null ? void 0 : _x.arguments) != null && isParsableJson(toolCall.function.arguments)) {
1193
- controller.enqueue({
1194
- type: "tool-input-end",
1195
- id: toolCall.id
1196
- });
1197
- controller.enqueue({
1198
- type: "tool-call",
1199
- toolCallId: (_y = toolCall.id) != null ? _y : generateId(),
1200
- toolName: toolCall.function.name,
1201
- input: toolCall.function.arguments
1202
- });
1203
- toolCall.hasFinished = true;
1204
- }
1125
+ toolCallTracker.processDelta(toolCallDelta);
1205
1126
  }
1206
1127
  }
1207
1128
  if (delta.annotations != null) {
@@ -1220,6 +1141,7 @@ var OpenAIChatLanguageModel = class {
1220
1141
  if (isActiveText) {
1221
1142
  controller.enqueue({ type: "text-end", id: "0" });
1222
1143
  }
1144
+ toolCallTracker.flush();
1223
1145
  controller.enqueue({
1224
1146
  type: "finish",
1225
1147
  finishReason,
@@ -5313,7 +5235,7 @@ var OpenAITranscriptionModel = class {
5313
5235
  };
5314
5236
 
5315
5237
  // src/version.ts
5316
- var VERSION = true ? "2.0.130" : "0.0.0-test";
5238
+ var VERSION = true ? "2.0.131" : "0.0.0-test";
5317
5239
 
5318
5240
  // src/openai-provider.ts
5319
5241
  function createOpenAI(options = {}) {