ai 4.1.12 → 4.1.14

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
+ ## 4.1.14
4
+
5
+ ### Patch Changes
6
+
7
+ - ca89615: fix (ai/core): only append assistant response at the end when there is a final user message
8
+
9
+ ## 4.1.13
10
+
11
+ ### Patch Changes
12
+
13
+ - 999085e: feat (ai/core): add write function to DataStreamWriter
14
+
3
15
  ## 4.1.12
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -10,6 +10,10 @@ import { z } from 'zod';
10
10
  import { ServerResponse as ServerResponse$1 } from 'http';
11
11
 
12
12
  interface DataStreamWriter {
13
+ /**
14
+ * Appends a data part to the stream.
15
+ */
16
+ write(data: DataStreamString): void;
13
17
  /**
14
18
  * Appends a data part to the stream.
15
19
  */
package/dist/index.d.ts CHANGED
@@ -10,6 +10,10 @@ import { z } from 'zod';
10
10
  import { ServerResponse as ServerResponse$1 } from 'http';
11
11
 
12
12
  interface DataStreamWriter {
13
+ /**
14
+ * Appends a data part to the stream.
15
+ */
16
+ write(data: DataStreamString): void;
13
17
  /**
14
18
  * Appends a data part to the stream.
15
19
  */
package/dist/index.js CHANGED
@@ -115,6 +115,9 @@ function createDataStream({
115
115
  }
116
116
  try {
117
117
  const result = execute({
118
+ write(data) {
119
+ safeEnqueue(data);
120
+ },
118
121
  writeData(data) {
119
122
  safeEnqueue((0, import_ui_utils.formatDataStreamPart)("data", [data]));
120
123
  },
@@ -1593,7 +1596,9 @@ function convertToCoreMessages(messages, options) {
1593
1596
  var _a15;
1594
1597
  const tools = (_a15 = options == null ? void 0 : options.tools) != null ? _a15 : {};
1595
1598
  const coreMessages = [];
1596
- for (const message of messages) {
1599
+ for (let i = 0; i < messages.length; i++) {
1600
+ const message = messages[i];
1601
+ const isLastMessage = i === messages.length - 1;
1597
1602
  const { role, content, toolInvocations, experimental_attachments } = message;
1598
1603
  switch (role) {
1599
1604
  case "system": {
@@ -1622,11 +1627,11 @@ function convertToCoreMessages(messages, options) {
1622
1627
  var _a16;
1623
1628
  return Math.max(max, (_a16 = toolInvocation.step) != null ? _a16 : 0);
1624
1629
  }, 0);
1625
- for (let i = 0; i <= maxStep; i++) {
1630
+ for (let i2 = 0; i2 <= maxStep; i2++) {
1626
1631
  const stepInvocations = toolInvocations.filter(
1627
1632
  (toolInvocation) => {
1628
1633
  var _a16;
1629
- return ((_a16 = toolInvocation.step) != null ? _a16 : 0) === i;
1634
+ return ((_a16 = toolInvocation.step) != null ? _a16 : 0) === i2;
1630
1635
  }
1631
1636
  );
1632
1637
  if (stepInvocations.length === 0) {
@@ -1635,6 +1640,7 @@ function convertToCoreMessages(messages, options) {
1635
1640
  coreMessages.push({
1636
1641
  role: "assistant",
1637
1642
  content: [
1643
+ ...isLastMessage && content && i2 === 0 ? [{ type: "text", text: content }] : [],
1638
1644
  ...stepInvocations.map(
1639
1645
  ({ toolCallId, toolName, args }) => ({
1640
1646
  type: "tool-call",
@@ -1671,7 +1677,7 @@ function convertToCoreMessages(messages, options) {
1671
1677
  })
1672
1678
  });
1673
1679
  }
1674
- if (content) {
1680
+ if (content && !isLastMessage) {
1675
1681
  coreMessages.push({ role: "assistant", content });
1676
1682
  }
1677
1683
  break;