ai 7.0.11 → 7.0.13

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,22 @@
1
1
  # ai
2
2
 
3
+ ## 7.0.13
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [31abef7]
8
+ - @ai-sdk/gateway@4.0.10
9
+
10
+ ## 7.0.12
11
+
12
+ ### Patch Changes
13
+
14
+ - ecfeb6f: Sort tool results by their tool call order when converting generation output to response messages.
15
+ - a193137: Fix `extractJsonMiddleware` preserving leading whitespace in the final streamed text suffix when no markdown fence prefix was stripped.
16
+ - Updated dependencies [c6f5e62]
17
+ - @ai-sdk/gateway@4.0.9
18
+ - @ai-sdk/provider-utils@5.0.4
19
+
3
20
  ## 7.0.11
4
21
 
5
22
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1089,7 +1089,7 @@ import {
1089
1089
  } from "@ai-sdk/provider-utils";
1090
1090
 
1091
1091
  // src/version.ts
1092
- var VERSION = true ? "7.0.11" : "0.0.0-test";
1092
+ var VERSION = true ? "7.0.13" : "0.0.0-test";
1093
1093
 
1094
1094
  // src/util/download/download.ts
1095
1095
  var download = async ({
@@ -4589,6 +4589,7 @@ async function toResponseMessages({
4589
4589
  tools
4590
4590
  }) {
4591
4591
  const responseMessages = [];
4592
+ const toolCallOrder = /* @__PURE__ */ new Map();
4592
4593
  const content = [];
4593
4594
  for (const part of inputContent) {
4594
4595
  if (part.type === "source") {
@@ -4639,6 +4640,9 @@ async function toResponseMessages({
4639
4640
  });
4640
4641
  break;
4641
4642
  case "tool-call":
4643
+ if (!toolCallOrder.has(part.toolCallId)) {
4644
+ toolCallOrder.set(part.toolCallId, toolCallOrder.size);
4645
+ }
4642
4646
  content.push({
4643
4647
  type: "tool-call",
4644
4648
  toolCallId: part.toolCallId,
@@ -4746,11 +4750,37 @@ async function toResponseMessages({
4746
4750
  if (toolResultContent.length > 0) {
4747
4751
  responseMessages.push({
4748
4752
  role: "tool",
4749
- content: toolResultContent
4753
+ content: sortToolResultContentByToolCallOrder({
4754
+ toolResultContent,
4755
+ toolCallOrder
4756
+ })
4750
4757
  });
4751
4758
  }
4752
4759
  return responseMessages;
4753
4760
  }
4761
+ function sortToolResultContentByToolCallOrder({
4762
+ toolResultContent,
4763
+ toolCallOrder
4764
+ }) {
4765
+ const sortedToolResults = toolResultContent.filter((part) => part.type === "tool-result").map((part, index) => ({ part, index })).sort((a, b) => {
4766
+ const aOrder = toolCallOrder.get(a.part.toolCallId);
4767
+ const bOrder = toolCallOrder.get(b.part.toolCallId);
4768
+ if (aOrder == null && bOrder == null) {
4769
+ return a.index - b.index;
4770
+ }
4771
+ if (aOrder == null) {
4772
+ return 1;
4773
+ }
4774
+ if (bOrder == null) {
4775
+ return -1;
4776
+ }
4777
+ return aOrder - bOrder || a.index - b.index;
4778
+ }).map(({ part }) => part);
4779
+ let toolResultIndex = 0;
4780
+ return toolResultContent.map(
4781
+ (part) => part.type === "tool-result" ? sortedToolResults[toolResultIndex++] : part
4782
+ );
4783
+ }
4754
4784
 
4755
4785
  // src/generate-text/tool-approval-signature.ts
4756
4786
  import {
@@ -13628,6 +13658,9 @@ function defaultSettingsMiddleware({
13628
13658
  function defaultTransform(text2) {
13629
13659
  return text2.replace(/^```(?:json)?\s*\n?/, "").replace(/\n?```\s*$/, "").trim();
13630
13660
  }
13661
+ function stripMarkdownCodeFenceSuffix(text2) {
13662
+ return text2.replace(/\n?```\s*$/, "").trimEnd();
13663
+ }
13631
13664
  function extractJsonMiddleware(options) {
13632
13665
  var _a22;
13633
13666
  const transform = (_a22 = options == null ? void 0 : options.transform) != null ? _a22 : defaultTransform;
@@ -13722,9 +13755,11 @@ function extractJsonMiddleware(options) {
13722
13755
  if (block.phase === "buffering") {
13723
13756
  remaining = transform(remaining);
13724
13757
  } else if (block.prefixStripped) {
13725
- remaining = remaining.replace(/\n?```\s*$/, "").trimEnd();
13726
- } else {
13758
+ remaining = stripMarkdownCodeFenceSuffix(remaining);
13759
+ } else if (block.phase === "prefix") {
13727
13760
  remaining = transform(remaining);
13761
+ } else {
13762
+ remaining = stripMarkdownCodeFenceSuffix(remaining);
13728
13763
  }
13729
13764
  if (remaining.length > 0) {
13730
13765
  controller.enqueue({