@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.
@@ -1269,7 +1269,7 @@ var OpenAIChatLanguageModel = class {
1269
1269
  requestBodyValues: body,
1270
1270
  responseHeaders
1271
1271
  });
1272
- const toolCalls = [];
1272
+ let toolCallTracker;
1273
1273
  let finishReason = {
1274
1274
  unified: "other",
1275
1275
  raw: void 0
@@ -1282,10 +1282,14 @@ var OpenAIChatLanguageModel = class {
1282
1282
  stream: checkedResponse.pipeThrough(
1283
1283
  new TransformStream({
1284
1284
  start(controller) {
1285
+ toolCallTracker = new import_provider_utils5.StreamingToolCallTracker(controller, {
1286
+ generateId: import_provider_utils5.generateId,
1287
+ typeValidation: "if-present"
1288
+ });
1285
1289
  controller.enqueue({ type: "stream-start", warnings });
1286
1290
  },
1287
1291
  transform(chunk, controller) {
1288
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
1292
+ var _a2, _b, _c, _d, _e;
1289
1293
  if (options.includeRawChunks) {
1290
1294
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1291
1295
  }
@@ -1346,64 +1350,7 @@ var OpenAIChatLanguageModel = class {
1346
1350
  }
1347
1351
  if (delta.tool_calls != null) {
1348
1352
  for (const toolCallDelta of delta.tool_calls) {
1349
- const index = toolCallDelta.index;
1350
- if (toolCalls[index] == null) {
1351
- if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
1352
- throw new import_provider4.InvalidResponseDataError({
1353
- data: toolCallDelta,
1354
- message: `Expected 'function' type.`
1355
- });
1356
- }
1357
- if (toolCallDelta.id == null) {
1358
- throw new import_provider4.InvalidResponseDataError({
1359
- data: toolCallDelta,
1360
- message: `Expected 'id' to be a string.`
1361
- });
1362
- }
1363
- if (((_f = toolCallDelta.function) == null ? void 0 : _f.name) == null) {
1364
- throw new import_provider4.InvalidResponseDataError({
1365
- data: toolCallDelta,
1366
- message: `Expected 'function.name' to be a string.`
1367
- });
1368
- }
1369
- controller.enqueue({
1370
- type: "tool-input-start",
1371
- id: toolCallDelta.id,
1372
- toolName: toolCallDelta.function.name
1373
- });
1374
- toolCalls[index] = {
1375
- id: toolCallDelta.id,
1376
- type: "function",
1377
- function: {
1378
- name: toolCallDelta.function.name,
1379
- arguments: (_g = toolCallDelta.function.arguments) != null ? _g : ""
1380
- },
1381
- hasFinished: false
1382
- };
1383
- const toolCall2 = toolCalls[index];
1384
- if (((_h = toolCall2.function) == null ? void 0 : _h.name) != null && ((_i = toolCall2.function) == null ? void 0 : _i.arguments) != null) {
1385
- if (toolCall2.function.arguments.length > 0) {
1386
- controller.enqueue({
1387
- type: "tool-input-delta",
1388
- id: toolCall2.id,
1389
- delta: toolCall2.function.arguments
1390
- });
1391
- }
1392
- }
1393
- continue;
1394
- }
1395
- const toolCall = toolCalls[index];
1396
- if (toolCall.hasFinished) {
1397
- continue;
1398
- }
1399
- if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
1400
- toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
1401
- }
1402
- controller.enqueue({
1403
- type: "tool-input-delta",
1404
- id: toolCall.id,
1405
- delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
1406
- });
1353
+ toolCallTracker.processDelta(toolCallDelta);
1407
1354
  }
1408
1355
  }
1409
1356
  if (delta.annotations != null) {
@@ -1419,25 +1366,10 @@ var OpenAIChatLanguageModel = class {
1419
1366
  }
1420
1367
  },
1421
1368
  flush(controller) {
1422
- var _a2;
1423
1369
  if (isActiveText) {
1424
1370
  controller.enqueue({ type: "text-end", id: "0" });
1425
1371
  }
1426
- for (const toolCall of toolCalls) {
1427
- if (!toolCall.hasFinished) {
1428
- controller.enqueue({
1429
- type: "tool-input-end",
1430
- id: toolCall.id
1431
- });
1432
- controller.enqueue({
1433
- type: "tool-call",
1434
- toolCallId: (_a2 = toolCall.id) != null ? _a2 : (0, import_provider_utils5.generateId)(),
1435
- toolName: toolCall.function.name,
1436
- input: toolCall.function.arguments
1437
- });
1438
- toolCall.hasFinished = true;
1439
- }
1440
- }
1372
+ toolCallTracker.flush();
1441
1373
  controller.enqueue({
1442
1374
  type: "finish",
1443
1375
  finishReason,