ai 3.1.22 → 3.1.24

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
@@ -1115,57 +1115,72 @@ var StreamObjectResult = class {
1115
1115
  warnings,
1116
1116
  rawResponse
1117
1117
  }) {
1118
- this.originalStream = stream;
1119
1118
  this.warnings = warnings;
1120
1119
  this.rawResponse = rawResponse;
1121
- }
1122
- get partialObjectStream() {
1123
- let accumulatedText = "";
1124
- let latestObject = void 0;
1125
- return createAsyncIterableStream(this.originalStream, {
1126
- transform(chunk, controller) {
1127
- if (typeof chunk === "string") {
1128
- accumulatedText += chunk;
1129
- const currentObject = parsePartialJson(
1130
- accumulatedText
1131
- );
1132
- if (!isDeepEqualData(latestObject, currentObject)) {
1133
- latestObject = currentObject;
1134
- controller.enqueue(currentObject);
1135
- }
1136
- } else if (chunk.type === "error") {
1137
- throw chunk.error;
1138
- }
1139
- }
1120
+ let resolveUsage;
1121
+ this.usage = new Promise((resolve) => {
1122
+ resolveUsage = resolve;
1140
1123
  });
1141
- }
1142
- get fullStream() {
1124
+ let usage;
1143
1125
  let accumulatedText = "";
1144
1126
  let latestObject = void 0;
1145
- return createAsyncIterableStream(this.originalStream, {
1146
- transform(chunk, controller) {
1147
- if (typeof chunk === "string") {
1148
- accumulatedText += chunk;
1149
- const currentObject = parsePartialJson(
1150
- accumulatedText
1151
- );
1152
- if (!isDeepEqualData(latestObject, currentObject)) {
1153
- latestObject = currentObject;
1154
- controller.enqueue({ type: "object", object: currentObject });
1127
+ this.originalStream = stream.pipeThrough(
1128
+ new TransformStream({
1129
+ async transform(chunk, controller) {
1130
+ if (typeof chunk === "string") {
1131
+ accumulatedText += chunk;
1132
+ const currentObject = parsePartialJson(
1133
+ accumulatedText
1134
+ );
1135
+ if (!isDeepEqualData(latestObject, currentObject)) {
1136
+ latestObject = currentObject;
1137
+ controller.enqueue({ type: "object", object: currentObject });
1138
+ }
1139
+ return;
1155
1140
  }
1156
- } else {
1157
1141
  switch (chunk.type) {
1158
- case "finish":
1142
+ case "finish": {
1143
+ usage = calculateTokenUsage(chunk.usage);
1159
1144
  controller.enqueue({
1160
1145
  ...chunk,
1161
- usage: calculateTokenUsage(chunk.usage)
1146
+ usage
1162
1147
  });
1148
+ resolveUsage(usage);
1163
1149
  break;
1164
- default:
1150
+ }
1151
+ default: {
1165
1152
  controller.enqueue(chunk);
1166
1153
  break;
1154
+ }
1167
1155
  }
1168
1156
  }
1157
+ })
1158
+ );
1159
+ }
1160
+ get partialObjectStream() {
1161
+ return createAsyncIterableStream(this.originalStream, {
1162
+ transform(chunk, controller) {
1163
+ switch (chunk.type) {
1164
+ case "object":
1165
+ controller.enqueue(chunk.object);
1166
+ break;
1167
+ case "finish":
1168
+ break;
1169
+ case "error":
1170
+ controller.error(chunk.error);
1171
+ break;
1172
+ default: {
1173
+ const _exhaustiveCheck = chunk;
1174
+ throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
1175
+ }
1176
+ }
1177
+ }
1178
+ });
1179
+ }
1180
+ get fullStream() {
1181
+ return createAsyncIterableStream(this.originalStream, {
1182
+ transform(chunk, controller) {
1183
+ controller.enqueue(chunk);
1169
1184
  }
1170
1185
  });
1171
1186
  }
@@ -1250,6 +1265,7 @@ async function generateText({
1250
1265
  maxRetries,
1251
1266
  abortSignal,
1252
1267
  maxAutomaticRoundtrips = 0,
1268
+ maxToolRoundtrips = maxAutomaticRoundtrips,
1253
1269
  ...settings
1254
1270
  }) {
1255
1271
  var _a, _b, _c;
@@ -1294,7 +1310,7 @@ async function generateText({
1294
1310
  // there are tool calls:
1295
1311
  currentToolCalls.length > 0 && // all current tool calls have results:
1296
1312
  currentToolResults.length === currentToolCalls.length && // the number of roundtrips is less than the maximum:
1297
- roundtrips++ < maxAutomaticRoundtrips
1313
+ roundtrips++ < maxToolRoundtrips
1298
1314
  );
1299
1315
  return new GenerateTextResult({
1300
1316
  // Always return a string so that the caller doesn't have to check for undefined.