@ai-sdk/openai 3.0.119 → 3.0.120

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,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
  createJsonResponseHandler,
@@ -1222,7 +1223,7 @@ var OpenAIChatLanguageModel = class {
1222
1223
  requestBodyValues: body,
1223
1224
  responseHeaders
1224
1225
  });
1225
- const toolCalls = [];
1226
+ let toolCallTracker;
1226
1227
  let finishReason = {
1227
1228
  unified: "other",
1228
1229
  raw: void 0
@@ -1235,10 +1236,14 @@ var OpenAIChatLanguageModel = class {
1235
1236
  stream: checkedResponse.pipeThrough(
1236
1237
  new TransformStream({
1237
1238
  start(controller) {
1239
+ toolCallTracker = new StreamingToolCallTracker(controller, {
1240
+ generateId,
1241
+ typeValidation: "if-present"
1242
+ });
1238
1243
  controller.enqueue({ type: "stream-start", warnings });
1239
1244
  },
1240
1245
  transform(chunk, controller) {
1241
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
1246
+ var _a2, _b, _c, _d, _e;
1242
1247
  if (options.includeRawChunks) {
1243
1248
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1244
1249
  }
@@ -1299,64 +1304,7 @@ var OpenAIChatLanguageModel = class {
1299
1304
  }
1300
1305
  if (delta.tool_calls != null) {
1301
1306
  for (const toolCallDelta of delta.tool_calls) {
1302
- const index = toolCallDelta.index;
1303
- if (toolCalls[index] == null) {
1304
- if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
1305
- throw new InvalidResponseDataError({
1306
- data: toolCallDelta,
1307
- message: `Expected 'function' type.`
1308
- });
1309
- }
1310
- if (toolCallDelta.id == null) {
1311
- throw new InvalidResponseDataError({
1312
- data: toolCallDelta,
1313
- message: `Expected 'id' to be a string.`
1314
- });
1315
- }
1316
- if (((_f = toolCallDelta.function) == null ? void 0 : _f.name) == null) {
1317
- throw new InvalidResponseDataError({
1318
- data: toolCallDelta,
1319
- message: `Expected 'function.name' to be a string.`
1320
- });
1321
- }
1322
- controller.enqueue({
1323
- type: "tool-input-start",
1324
- id: toolCallDelta.id,
1325
- toolName: toolCallDelta.function.name
1326
- });
1327
- toolCalls[index] = {
1328
- id: toolCallDelta.id,
1329
- type: "function",
1330
- function: {
1331
- name: toolCallDelta.function.name,
1332
- arguments: (_g = toolCallDelta.function.arguments) != null ? _g : ""
1333
- },
1334
- hasFinished: false
1335
- };
1336
- const toolCall2 = toolCalls[index];
1337
- if (((_h = toolCall2.function) == null ? void 0 : _h.name) != null && ((_i = toolCall2.function) == null ? void 0 : _i.arguments) != null) {
1338
- if (toolCall2.function.arguments.length > 0) {
1339
- controller.enqueue({
1340
- type: "tool-input-delta",
1341
- id: toolCall2.id,
1342
- delta: toolCall2.function.arguments
1343
- });
1344
- }
1345
- }
1346
- continue;
1347
- }
1348
- const toolCall = toolCalls[index];
1349
- if (toolCall.hasFinished) {
1350
- continue;
1351
- }
1352
- if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
1353
- toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
1354
- }
1355
- controller.enqueue({
1356
- type: "tool-input-delta",
1357
- id: toolCall.id,
1358
- delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
1359
- });
1307
+ toolCallTracker.processDelta(toolCallDelta);
1360
1308
  }
1361
1309
  }
1362
1310
  if (delta.annotations != null) {
@@ -1372,25 +1320,10 @@ var OpenAIChatLanguageModel = class {
1372
1320
  }
1373
1321
  },
1374
1322
  flush(controller) {
1375
- var _a2;
1376
1323
  if (isActiveText) {
1377
1324
  controller.enqueue({ type: "text-end", id: "0" });
1378
1325
  }
1379
- for (const toolCall of toolCalls) {
1380
- if (!toolCall.hasFinished) {
1381
- controller.enqueue({
1382
- type: "tool-input-end",
1383
- id: toolCall.id
1384
- });
1385
- controller.enqueue({
1386
- type: "tool-call",
1387
- toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
1388
- toolName: toolCall.function.name,
1389
- input: toolCall.function.arguments
1390
- });
1391
- toolCall.hasFinished = true;
1392
- }
1393
- }
1326
+ toolCallTracker.flush();
1394
1327
  controller.enqueue({
1395
1328
  type: "finish",
1396
1329
  finishReason,