@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.
package/dist/index.mjs CHANGED
@@ -11,6 +11,7 @@ import {
11
11
  InvalidResponseDataError
12
12
  } from "@ai-sdk/provider";
13
13
  import {
14
+ StreamingToolCallTracker,
14
15
  combineHeaders,
15
16
  createEventSourceResponseHandler,
16
17
  createJsonResponseHandler,
@@ -1230,7 +1231,7 @@ var OpenAIChatLanguageModel = class {
1230
1231
  requestBodyValues: body,
1231
1232
  responseHeaders
1232
1233
  });
1233
- const toolCalls = [];
1234
+ let toolCallTracker;
1234
1235
  let finishReason = {
1235
1236
  unified: "other",
1236
1237
  raw: void 0
@@ -1243,10 +1244,14 @@ var OpenAIChatLanguageModel = class {
1243
1244
  stream: checkedResponse.pipeThrough(
1244
1245
  new TransformStream({
1245
1246
  start(controller) {
1247
+ toolCallTracker = new StreamingToolCallTracker(controller, {
1248
+ generateId,
1249
+ typeValidation: "if-present"
1250
+ });
1246
1251
  controller.enqueue({ type: "stream-start", warnings });
1247
1252
  },
1248
1253
  transform(chunk, controller) {
1249
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
1254
+ var _a2, _b, _c, _d, _e;
1250
1255
  if (options.includeRawChunks) {
1251
1256
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1252
1257
  }
@@ -1307,64 +1312,7 @@ var OpenAIChatLanguageModel = class {
1307
1312
  }
1308
1313
  if (delta.tool_calls != null) {
1309
1314
  for (const toolCallDelta of delta.tool_calls) {
1310
- const index = toolCallDelta.index;
1311
- if (toolCalls[index] == null) {
1312
- if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
1313
- throw new InvalidResponseDataError({
1314
- data: toolCallDelta,
1315
- message: `Expected 'function' type.`
1316
- });
1317
- }
1318
- if (toolCallDelta.id == null) {
1319
- throw new InvalidResponseDataError({
1320
- data: toolCallDelta,
1321
- message: `Expected 'id' to be a string.`
1322
- });
1323
- }
1324
- if (((_f = toolCallDelta.function) == null ? void 0 : _f.name) == null) {
1325
- throw new InvalidResponseDataError({
1326
- data: toolCallDelta,
1327
- message: `Expected 'function.name' to be a string.`
1328
- });
1329
- }
1330
- controller.enqueue({
1331
- type: "tool-input-start",
1332
- id: toolCallDelta.id,
1333
- toolName: toolCallDelta.function.name
1334
- });
1335
- toolCalls[index] = {
1336
- id: toolCallDelta.id,
1337
- type: "function",
1338
- function: {
1339
- name: toolCallDelta.function.name,
1340
- arguments: (_g = toolCallDelta.function.arguments) != null ? _g : ""
1341
- },
1342
- hasFinished: false
1343
- };
1344
- const toolCall2 = toolCalls[index];
1345
- if (((_h = toolCall2.function) == null ? void 0 : _h.name) != null && ((_i = toolCall2.function) == null ? void 0 : _i.arguments) != null) {
1346
- if (toolCall2.function.arguments.length > 0) {
1347
- controller.enqueue({
1348
- type: "tool-input-delta",
1349
- id: toolCall2.id,
1350
- delta: toolCall2.function.arguments
1351
- });
1352
- }
1353
- }
1354
- continue;
1355
- }
1356
- const toolCall = toolCalls[index];
1357
- if (toolCall.hasFinished) {
1358
- continue;
1359
- }
1360
- if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
1361
- toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
1362
- }
1363
- controller.enqueue({
1364
- type: "tool-input-delta",
1365
- id: toolCall.id,
1366
- delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
1367
- });
1315
+ toolCallTracker.processDelta(toolCallDelta);
1368
1316
  }
1369
1317
  }
1370
1318
  if (delta.annotations != null) {
@@ -1380,25 +1328,10 @@ var OpenAIChatLanguageModel = class {
1380
1328
  }
1381
1329
  },
1382
1330
  flush(controller) {
1383
- var _a2;
1384
1331
  if (isActiveText) {
1385
1332
  controller.enqueue({ type: "text-end", id: "0" });
1386
1333
  }
1387
- for (const toolCall of toolCalls) {
1388
- if (!toolCall.hasFinished) {
1389
- controller.enqueue({
1390
- type: "tool-input-end",
1391
- id: toolCall.id
1392
- });
1393
- controller.enqueue({
1394
- type: "tool-call",
1395
- toolCallId: (_a2 = toolCall.id) != null ? _a2 : generateId(),
1396
- toolName: toolCall.function.name,
1397
- input: toolCall.function.arguments
1398
- });
1399
- toolCall.hasFinished = true;
1400
- }
1401
- }
1334
+ toolCallTracker.flush();
1402
1335
  controller.enqueue({
1403
1336
  type: "finish",
1404
1337
  finishReason,
@@ -8330,7 +8263,7 @@ var OpenAITranscriptionModel = class {
8330
8263
  };
8331
8264
 
8332
8265
  // src/version.ts
8333
- var VERSION = true ? "3.0.119" : "0.0.0-test";
8266
+ var VERSION = true ? "3.0.120" : "0.0.0-test";
8334
8267
 
8335
8268
  // src/openai-provider.ts
8336
8269
  function createOpenAI(options = {}) {