ai 3.1.14 → 3.1.16

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
@@ -1168,6 +1168,33 @@ var StreamObjectResult = class {
1168
1168
  };
1169
1169
  var experimental_streamObject = streamObject;
1170
1170
 
1171
+ // core/util/is-non-empty-object.ts
1172
+ function isNonEmptyObject(object) {
1173
+ return object != null && Object.keys(object).length > 0;
1174
+ }
1175
+
1176
+ // core/prompt/prepare-tools-and-tool-choice.ts
1177
+ function prepareToolsAndToolChoice({
1178
+ tools,
1179
+ toolChoice
1180
+ }) {
1181
+ if (!isNonEmptyObject(tools)) {
1182
+ return {
1183
+ tools: void 0,
1184
+ toolChoice: void 0
1185
+ };
1186
+ }
1187
+ return {
1188
+ tools: Object.entries(tools).map(([name, tool2]) => ({
1189
+ type: "function",
1190
+ name,
1191
+ description: tool2.description,
1192
+ parameters: convertZodToJSONSchema(tool2.parameters)
1193
+ })),
1194
+ toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
1195
+ };
1196
+ }
1197
+
1171
1198
  // core/generate-text/tool-call.ts
1172
1199
  import {
1173
1200
  InvalidToolArgumentsError,
@@ -1212,6 +1239,7 @@ function parseToolCall({
1212
1239
  async function generateText({
1213
1240
  model,
1214
1241
  tools,
1242
+ toolChoice,
1215
1243
  system,
1216
1244
  prompt,
1217
1245
  messages,
@@ -1226,12 +1254,7 @@ async function generateText({
1226
1254
  return model.doGenerate({
1227
1255
  mode: {
1228
1256
  type: "regular",
1229
- tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1230
- type: "function",
1231
- name,
1232
- description: tool2.description,
1233
- parameters: convertZodToJSONSchema(tool2.parameters)
1234
- }))
1257
+ ...prepareToolsAndToolChoice({ tools, toolChoice })
1235
1258
  },
1236
1259
  ...prepareCallSettings(settings),
1237
1260
  inputFormat: validatedPrompt.type,
@@ -1444,11 +1467,13 @@ function runToolsTransformation({
1444
1467
  async function streamText({
1445
1468
  model,
1446
1469
  tools,
1470
+ toolChoice,
1447
1471
  system,
1448
1472
  prompt,
1449
1473
  messages,
1450
1474
  maxRetries,
1451
1475
  abortSignal,
1476
+ onFinish,
1452
1477
  ...settings
1453
1478
  }) {
1454
1479
  const retry = retryWithExponentialBackoff({ maxRetries });
@@ -1457,12 +1482,7 @@ async function streamText({
1457
1482
  () => model.doStream({
1458
1483
  mode: {
1459
1484
  type: "regular",
1460
- tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1461
- type: "function",
1462
- name,
1463
- description: tool2.description,
1464
- parameters: convertZodToJSONSchema(tool2.parameters)
1465
- }))
1485
+ ...prepareToolsAndToolChoice({ tools, toolChoice })
1466
1486
  },
1467
1487
  ...prepareCallSettings(settings),
1468
1488
  inputFormat: validatedPrompt.type,
@@ -1476,17 +1496,20 @@ async function streamText({
1476
1496
  generatorStream: stream
1477
1497
  }),
1478
1498
  warnings,
1479
- rawResponse
1499
+ rawResponse,
1500
+ onFinish
1480
1501
  });
1481
1502
  }
1482
1503
  var StreamTextResult = class {
1483
1504
  constructor({
1484
1505
  stream,
1485
1506
  warnings,
1486
- rawResponse
1507
+ rawResponse,
1508
+ onFinish
1487
1509
  }) {
1488
1510
  this.warnings = warnings;
1489
1511
  this.rawResponse = rawResponse;
1512
+ this.onFinish = onFinish;
1490
1513
  let resolveUsage;
1491
1514
  this.usage = new Promise((resolve) => {
1492
1515
  resolveUsage = resolve;
@@ -1495,13 +1518,70 @@ var StreamTextResult = class {
1495
1518
  this.finishReason = new Promise((resolve) => {
1496
1519
  resolveFinishReason = resolve;
1497
1520
  });
1521
+ let resolveText;
1522
+ this.text = new Promise((resolve) => {
1523
+ resolveText = resolve;
1524
+ });
1525
+ let resolveToolCalls;
1526
+ this.toolCalls = new Promise((resolve) => {
1527
+ resolveToolCalls = resolve;
1528
+ });
1529
+ let resolveToolResults;
1530
+ this.toolResults = new Promise((resolve) => {
1531
+ resolveToolResults = resolve;
1532
+ });
1533
+ let finishReason;
1534
+ let usage;
1535
+ let text = "";
1536
+ const toolCalls = [];
1537
+ const toolResults = [];
1538
+ const self = this;
1498
1539
  this.originalStream = stream.pipeThrough(
1499
1540
  new TransformStream({
1500
1541
  async transform(chunk, controller) {
1501
1542
  controller.enqueue(chunk);
1543
+ if (chunk.type === "text-delta") {
1544
+ text += chunk.textDelta;
1545
+ }
1546
+ if (chunk.type === "tool-call") {
1547
+ toolCalls.push(chunk);
1548
+ }
1549
+ if (chunk.type === "tool-result") {
1550
+ toolResults.push(chunk);
1551
+ }
1502
1552
  if (chunk.type === "finish") {
1503
- resolveUsage(chunk.usage);
1504
- resolveFinishReason(chunk.finishReason);
1553
+ usage = chunk.usage;
1554
+ finishReason = chunk.finishReason;
1555
+ resolveUsage(usage);
1556
+ resolveFinishReason(finishReason);
1557
+ resolveText(text);
1558
+ resolveToolCalls(toolCalls);
1559
+ }
1560
+ },
1561
+ // invoke onFinish callback and resolve toolResults promise when the stream is about to close:
1562
+ async flush(controller) {
1563
+ var _a;
1564
+ try {
1565
+ resolveToolResults(toolResults);
1566
+ await ((_a = self.onFinish) == null ? void 0 : _a.call(self, {
1567
+ finishReason: finishReason != null ? finishReason : "unknown",
1568
+ usage: usage != null ? usage : {
1569
+ promptTokens: NaN,
1570
+ completionTokens: NaN,
1571
+ totalTokens: NaN
1572
+ },
1573
+ text,
1574
+ toolCalls,
1575
+ // The tool results are inferred as a never[] type, because they are
1576
+ // optional and the execute method with an inferred result type is
1577
+ // optional as well. Therefore we need to cast the toolResults to any.
1578
+ // The type exposed to the users will be correctly inferred.
1579
+ toolResults,
1580
+ rawResponse,
1581
+ warnings
1582
+ }));
1583
+ } catch (error) {
1584
+ controller.error(error);
1505
1585
  }
1506
1586
  }
1507
1587
  })