ai 3.3.21 → 3.3.23

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # ai
2
2
 
3
+ ## 3.3.23
4
+
5
+ ### Patch Changes
6
+
7
+ - b55e6f7: fix (ai/core): streamObject text stream in array mode must not include elements: prefix.
8
+
9
+ ## 3.3.22
10
+
11
+ ### Patch Changes
12
+
13
+ - a5a56fd: fix (ai/core): only send roundtrip-finish event after async tool calls are done
14
+
3
15
  ## 3.3.21
4
16
 
5
17
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1491,8 +1491,8 @@ function createAsyncIterableStream(source, transformer) {
1491
1491
  var noSchemaOutputStrategy = {
1492
1492
  type: "no-schema",
1493
1493
  jsonSchema: void 0,
1494
- validatePartialResult({ value }) {
1495
- return { success: true, value };
1494
+ validatePartialResult({ value, textDelta }) {
1495
+ return { success: true, value: { partial: value, textDelta } };
1496
1496
  },
1497
1497
  validateFinalResult(value) {
1498
1498
  return value === void 0 ? { success: false, error: new NoObjectGeneratedError() } : { success: true, value };
@@ -1506,8 +1506,15 @@ var noSchemaOutputStrategy = {
1506
1506
  var objectOutputStrategy = (schema) => ({
1507
1507
  type: "object",
1508
1508
  jsonSchema: schema.jsonSchema,
1509
- validatePartialResult({ value }) {
1510
- return { success: true, value };
1509
+ validatePartialResult({ value, textDelta }) {
1510
+ return {
1511
+ success: true,
1512
+ value: {
1513
+ // Note: currently no validation of partial results:
1514
+ partial: value,
1515
+ textDelta
1516
+ }
1517
+ };
1511
1518
  },
1512
1519
  validateFinalResult(value) {
1513
1520
  return (0, import_provider_utils5.safeValidateTypes)({ value, schema });
@@ -1521,7 +1528,7 @@ var objectOutputStrategy = (schema) => ({
1521
1528
  var arrayOutputStrategy = (schema) => {
1522
1529
  const { $schema, ...itemSchema } = schema.jsonSchema;
1523
1530
  return {
1524
- type: "object",
1531
+ type: "array",
1525
1532
  // wrap in object that contains array of elements, since most LLMs will not
1526
1533
  // be able to generate an array directly:
1527
1534
  // possible future optimization: use arrays directly when model supports grammar-guided generation
@@ -1534,10 +1541,8 @@ var arrayOutputStrategy = (schema) => {
1534
1541
  required: ["elements"],
1535
1542
  additionalProperties: false
1536
1543
  },
1537
- validatePartialResult({
1538
- value,
1539
- parseState
1540
- }) {
1544
+ validatePartialResult({ value, latestObject, isFirstDelta, isFinalDelta }) {
1545
+ var _a11;
1541
1546
  if (!(0, import_provider9.isJSONObject)(value) || !(0, import_provider9.isJSONArray)(value.elements)) {
1542
1547
  return {
1543
1548
  success: false,
@@ -1552,7 +1557,7 @@ var arrayOutputStrategy = (schema) => {
1552
1557
  for (let i = 0; i < inputArray.length; i++) {
1553
1558
  const element = inputArray[i];
1554
1559
  const result = (0, import_provider_utils5.safeValidateTypes)({ value: element, schema });
1555
- if (i === inputArray.length - 1 && (!result.success || parseState !== "successful-parse")) {
1560
+ if (i === inputArray.length - 1 && !isFinalDelta) {
1556
1561
  continue;
1557
1562
  }
1558
1563
  if (!result.success) {
@@ -1560,7 +1565,25 @@ var arrayOutputStrategy = (schema) => {
1560
1565
  }
1561
1566
  resultArray.push(result.value);
1562
1567
  }
1563
- return { success: true, value: resultArray };
1568
+ const publishedElementCount = (_a11 = latestObject == null ? void 0 : latestObject.length) != null ? _a11 : 0;
1569
+ let textDelta = "";
1570
+ if (isFirstDelta) {
1571
+ textDelta += "[";
1572
+ }
1573
+ if (publishedElementCount > 0) {
1574
+ textDelta += ",";
1575
+ }
1576
+ textDelta += resultArray.slice(publishedElementCount).map((element) => JSON.stringify(element)).join(",");
1577
+ if (isFinalDelta) {
1578
+ textDelta += "]";
1579
+ }
1580
+ return {
1581
+ success: true,
1582
+ value: {
1583
+ partial: resultArray,
1584
+ textDelta
1585
+ }
1586
+ };
1564
1587
  },
1565
1588
  validateFinalResult(value) {
1566
1589
  if (!(0, import_provider9.isJSONObject)(value) || !(0, import_provider9.isJSONArray)(value.elements)) {
@@ -2301,17 +2324,18 @@ var DefaultStreamObjectResult = class {
2301
2324
  let object;
2302
2325
  let error;
2303
2326
  let accumulatedText = "";
2304
- let delta = "";
2327
+ let textDelta = "";
2305
2328
  let latestObjectJson = void 0;
2306
2329
  let latestObject = void 0;
2307
- let firstChunk = true;
2330
+ let isFirstChunk = true;
2331
+ let isFirstDelta = true;
2308
2332
  const self = this;
2309
2333
  this.originalStream = stream.pipeThrough(
2310
2334
  new TransformStream({
2311
2335
  async transform(chunk, controller) {
2312
- if (firstChunk) {
2336
+ if (isFirstChunk) {
2313
2337
  const msToFirstChunk = performance.now() - startTimestamp;
2314
- firstChunk = false;
2338
+ isFirstChunk = false;
2315
2339
  doStreamSpan.addEvent("ai.stream.firstChunk", {
2316
2340
  "ai.stream.msToFirstChunk": msToFirstChunk
2317
2341
  });
@@ -2321,36 +2345,37 @@ var DefaultStreamObjectResult = class {
2321
2345
  }
2322
2346
  if (typeof chunk === "string") {
2323
2347
  accumulatedText += chunk;
2324
- delta += chunk;
2348
+ textDelta += chunk;
2325
2349
  const { value: currentObjectJson, state: parseState } = (0, import_ui_utils2.parsePartialJson)(accumulatedText);
2326
2350
  if (currentObjectJson !== void 0 && !(0, import_ui_utils2.isDeepEqualData)(latestObjectJson, currentObjectJson)) {
2327
2351
  const validationResult = outputStrategy.validatePartialResult({
2328
2352
  value: currentObjectJson,
2329
- parseState
2353
+ textDelta,
2354
+ latestObject,
2355
+ isFirstDelta,
2356
+ isFinalDelta: parseState === "successful-parse"
2330
2357
  });
2331
- if (validationResult.success && !(0, import_ui_utils2.isDeepEqualData)(latestObject, validationResult.value)) {
2358
+ if (validationResult.success && !(0, import_ui_utils2.isDeepEqualData)(latestObject, validationResult.value.partial)) {
2332
2359
  latestObjectJson = currentObjectJson;
2333
- latestObject = validationResult.value;
2360
+ latestObject = validationResult.value.partial;
2334
2361
  controller.enqueue({
2335
2362
  type: "object",
2336
2363
  object: latestObject
2337
2364
  });
2338
2365
  controller.enqueue({
2339
2366
  type: "text-delta",
2340
- textDelta: delta
2367
+ textDelta: validationResult.value.textDelta
2341
2368
  });
2342
- delta = "";
2369
+ textDelta = "";
2370
+ isFirstDelta = false;
2343
2371
  }
2344
2372
  }
2345
2373
  return;
2346
2374
  }
2347
2375
  switch (chunk.type) {
2348
2376
  case "finish": {
2349
- if (delta !== "") {
2350
- controller.enqueue({
2351
- type: "text-delta",
2352
- textDelta: delta
2353
- });
2377
+ if (textDelta !== "") {
2378
+ controller.enqueue({ type: "text-delta", textDelta });
2354
2379
  }
2355
2380
  finishReason = chunk.finishReason;
2356
2381
  usage = calculateCompletionTokenUsage(chunk.usage);
@@ -3560,13 +3585,6 @@ var DefaultStreamTextResult = class {
3560
3585
  roundtripFinishReason = chunk.finishReason;
3561
3586
  roundtripProviderMetadata = chunk.experimental_providerMetadata;
3562
3587
  roundtripLogProbs = chunk.logprobs;
3563
- controller.enqueue({
3564
- type: "roundtrip-finish",
3565
- finishReason: chunk.finishReason,
3566
- usage: chunk.usage,
3567
- experimental_providerMetadata: chunk.experimental_providerMetadata,
3568
- logprobs: chunk.logprobs
3569
- });
3570
3588
  break;
3571
3589
  case "tool-call-streaming-start":
3572
3590
  case "tool-call-delta": {
@@ -3586,6 +3604,13 @@ var DefaultStreamTextResult = class {
3586
3604
  },
3587
3605
  // invoke onFinish callback and resolve toolResults promise when the stream is about to close:
3588
3606
  async flush(controller) {
3607
+ controller.enqueue({
3608
+ type: "roundtrip-finish",
3609
+ finishReason: roundtripFinishReason,
3610
+ usage: roundtripUsage,
3611
+ experimental_providerMetadata: roundtripProviderMetadata,
3612
+ logprobs: roundtripLogProbs
3613
+ });
3589
3614
  const telemetryToolCalls = roundtripToolCalls.length > 0 ? JSON.stringify(roundtripToolCalls) : void 0;
3590
3615
  try {
3591
3616
  doStreamSpan2.setAttributes(