assistant-stream 0.0.24 → 0.0.26

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.js CHANGED
@@ -234,6 +234,13 @@ var ToolCallStreamControllerImpl = class {
234
234
  return this._argsTextController;
235
235
  }
236
236
  _argsTextController;
237
+ unstable_setArtifact(artifact) {
238
+ this._controller.enqueue({
239
+ type: "artifact",
240
+ path: [],
241
+ artifact
242
+ });
243
+ }
237
244
  setResult(result, isError) {
238
245
  this._controller.enqueue({
239
246
  type: "result",
@@ -455,7 +462,7 @@ var AssistantMetaTransformStream = class extends TransformStream {
455
462
  controller.enqueue(chunk);
456
463
  return;
457
464
  }
458
- if (chunk.type === "text-delta" || chunk.type === "result" || chunk.type === "part-finish" || chunk.type === "tool-call-args-text-finish") {
465
+ if (chunk.type === "text-delta" || chunk.type === "result" || chunk.type === "artifact" || chunk.type === "part-finish" || chunk.type === "tool-call-args-text-finish") {
459
466
  if (chunk.path.length !== 1) {
460
467
  controller.error(
461
468
  new Error(`${chunk.type} chunks must have a path of length 1`)
@@ -530,7 +537,7 @@ var DataStreamEncoder = class extends PipeableTransformStream {
530
537
  }
531
538
  case "tool-call": {
532
539
  controller.enqueue({
533
- type: "c" /* ToolCallDelta */,
540
+ type: "c" /* ToolCallArgsTextDelta */,
534
541
  value: {
535
542
  toolCallId: part.toolCallId,
536
543
  argsTextDelta: chunk.textDelta
@@ -561,6 +568,22 @@ var DataStreamEncoder = class extends PipeableTransformStream {
561
568
  });
562
569
  break;
563
570
  }
571
+ case "artifact": {
572
+ const part = chunk.meta;
573
+ if (part.type !== "tool-call") {
574
+ throw new Error(
575
+ `Artifact chunk on non-tool-call part not supported: ${part.type}`
576
+ );
577
+ }
578
+ controller.enqueue({
579
+ type: "aui-tool-artifact" /* AuiToolCallArtifact */,
580
+ value: {
581
+ toolCallId: part.toolCallId,
582
+ artifact: chunk.artifact
583
+ }
584
+ });
585
+ break;
586
+ }
564
587
  case "step-start": {
565
588
  const { type: type2, ...value } = chunk;
566
589
  controller.enqueue({
@@ -661,7 +684,7 @@ var DataStreamDecoder = class extends PipeableTransformStream {
661
684
  activeToolCallArgsText = toolCallController.argsText;
662
685
  break;
663
686
  }
664
- case "c" /* ToolCallDelta */: {
687
+ case "c" /* ToolCallArgsTextDelta */: {
665
688
  const { toolCallId, argsTextDelta } = value;
666
689
  const toolCallController = toolCallControllers.get(toolCallId);
667
690
  if (!toolCallController)
@@ -671,6 +694,16 @@ var DataStreamDecoder = class extends PipeableTransformStream {
671
694
  toolCallController.argsText.append(argsTextDelta);
672
695
  break;
673
696
  }
697
+ case "aui-tool-artifact" /* AuiToolCallArtifact */: {
698
+ const { toolCallId, artifact } = value;
699
+ const toolCallController = toolCallControllers.get(toolCallId);
700
+ if (!toolCallController)
701
+ throw new Error(
702
+ "Encountered tool call with unknown id: " + toolCallId
703
+ );
704
+ toolCallController.unstable_setArtifact(artifact);
705
+ break;
706
+ }
674
707
  case "a" /* ToolCallResult */: {
675
708
  const { toolCallId, result } = value;
676
709
  const toolCallController = toolCallControllers.get(toolCallId);
@@ -1066,7 +1099,7 @@ var ToolExecutionStream = class extends PipeableTransformStream {
1066
1099
  }
1067
1100
  },
1068
1101
  async flush() {
1069
- await Promise.all(toolCallPromises);
1102
+ await Promise.all(toolCallPromises.values());
1070
1103
  }
1071
1104
  });
1072
1105
  return readable.pipeThrough(new AssistantMetaTransformStream()).pipeThrough(transform);
@@ -1508,54 +1541,66 @@ var handlePartStart = (message, chunk) => {
1508
1541
  }
1509
1542
  };
1510
1543
  var handleToolCallArgsTextFinish = (message, chunk) => {
1511
- return updatePartForPath(message, chunk, (lastPart) => {
1512
- if (lastPart.type !== "tool-call") {
1513
- throw new Error("Last part is not a tool call");
1544
+ return updatePartForPath(message, chunk, (part) => {
1545
+ if (part.type !== "tool-call") {
1546
+ throw new Error("Last is not a tool call");
1514
1547
  }
1515
1548
  return {
1516
- ...lastPart,
1549
+ ...part,
1517
1550
  state: "call"
1518
1551
  };
1519
1552
  });
1520
1553
  };
1521
1554
  var handlePartFinish = (message, chunk) => {
1522
- return updatePartForPath(message, chunk, (lastPart) => ({
1523
- ...lastPart,
1555
+ return updatePartForPath(message, chunk, (part) => ({
1556
+ ...part,
1524
1557
  status: { type: "complete", reason: "unknown" }
1525
1558
  }));
1526
1559
  };
1527
1560
  var handleTextDelta = (message, chunk) => {
1528
- return updatePartForPath(message, chunk, (lastPart) => {
1529
- if (lastPart.type === "text") {
1530
- return { ...lastPart, text: lastPart.text + chunk.textDelta };
1531
- } else if (lastPart.type === "tool-call") {
1532
- const newArgsText = lastPart.argsText + chunk.textDelta;
1561
+ return updatePartForPath(message, chunk, (part) => {
1562
+ if (part.type === "text") {
1563
+ return { ...part, text: part.text + chunk.textDelta };
1564
+ } else if (part.type === "tool-call") {
1565
+ const newArgsText = part.argsText + chunk.textDelta;
1533
1566
  let newArgs;
1534
1567
  try {
1535
1568
  newArgs = parsePartialJson(newArgsText);
1536
1569
  } catch (err) {
1537
- newArgs = lastPart.args;
1570
+ newArgs = part.args;
1538
1571
  }
1539
- return { ...lastPart, argsText: newArgsText, args: newArgs };
1572
+ return { ...part, argsText: newArgsText, args: newArgs };
1540
1573
  } else {
1541
1574
  throw new Error(
1542
- "text-delta received but last part is neither text nor tool-call"
1575
+ "text-delta received but part is neither text nor tool-call"
1543
1576
  );
1544
1577
  }
1545
1578
  });
1546
1579
  };
1547
1580
  var handleResult = (message, chunk) => {
1548
- return updatePartForPath(message, chunk, (lastPart) => {
1549
- if (lastPart.type === "tool-call") {
1581
+ return updatePartForPath(message, chunk, (part) => {
1582
+ if (part.type === "tool-call") {
1550
1583
  return {
1551
- ...lastPart,
1584
+ ...part,
1552
1585
  state: "result",
1553
1586
  result: chunk.result,
1554
1587
  isError: chunk.isError ?? false,
1555
1588
  status: { type: "complete", reason: "stop" }
1556
1589
  };
1557
1590
  } else {
1558
- throw new Error("Result chunk received but last part is not a tool-call");
1591
+ throw new Error("Result chunk received but part is not a tool-call");
1592
+ }
1593
+ });
1594
+ };
1595
+ var handleArtifact = (message, chunk) => {
1596
+ return updatePartForPath(message, chunk, (part) => {
1597
+ if (part.type === "tool-call") {
1598
+ return {
1599
+ ...part,
1600
+ artifact: chunk.artifact
1601
+ };
1602
+ } else {
1603
+ throw new Error("Artifact chunk received but part is not a tool-call");
1559
1604
  }
1560
1605
  });
1561
1606
  };
@@ -1670,6 +1715,9 @@ var AssistantMessageAccumulator = class extends TransformStream {
1670
1715
  case "result":
1671
1716
  message = handleResult(message, chunk);
1672
1717
  break;
1718
+ case "artifact":
1719
+ message = handleArtifact(message, chunk);
1720
+ break;
1673
1721
  case "message-finish":
1674
1722
  message = handleMessageFinish(message, chunk);
1675
1723
  break;