ai 4.1.13 → 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,11 @@
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
+
3
9
  ## 4.1.13
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1596,7 +1596,9 @@ function convertToCoreMessages(messages, options) {
1596
1596
  var _a15;
1597
1597
  const tools = (_a15 = options == null ? void 0 : options.tools) != null ? _a15 : {};
1598
1598
  const coreMessages = [];
1599
- 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;
1600
1602
  const { role, content, toolInvocations, experimental_attachments } = message;
1601
1603
  switch (role) {
1602
1604
  case "system": {
@@ -1625,11 +1627,11 @@ function convertToCoreMessages(messages, options) {
1625
1627
  var _a16;
1626
1628
  return Math.max(max, (_a16 = toolInvocation.step) != null ? _a16 : 0);
1627
1629
  }, 0);
1628
- for (let i = 0; i <= maxStep; i++) {
1630
+ for (let i2 = 0; i2 <= maxStep; i2++) {
1629
1631
  const stepInvocations = toolInvocations.filter(
1630
1632
  (toolInvocation) => {
1631
1633
  var _a16;
1632
- return ((_a16 = toolInvocation.step) != null ? _a16 : 0) === i;
1634
+ return ((_a16 = toolInvocation.step) != null ? _a16 : 0) === i2;
1633
1635
  }
1634
1636
  );
1635
1637
  if (stepInvocations.length === 0) {
@@ -1638,6 +1640,7 @@ function convertToCoreMessages(messages, options) {
1638
1640
  coreMessages.push({
1639
1641
  role: "assistant",
1640
1642
  content: [
1643
+ ...isLastMessage && content && i2 === 0 ? [{ type: "text", text: content }] : [],
1641
1644
  ...stepInvocations.map(
1642
1645
  ({ toolCallId, toolName, args }) => ({
1643
1646
  type: "tool-call",
@@ -1674,7 +1677,7 @@ function convertToCoreMessages(messages, options) {
1674
1677
  })
1675
1678
  });
1676
1679
  }
1677
- if (content) {
1680
+ if (content && !isLastMessage) {
1678
1681
  coreMessages.push({ role: "assistant", content });
1679
1682
  }
1680
1683
  break;