ai 7.0.11 → 7.0.12

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.
@@ -83,7 +83,7 @@ import {
83
83
  } from "@ai-sdk/provider-utils";
84
84
 
85
85
  // src/version.ts
86
- var VERSION = true ? "7.0.11" : "0.0.0-test";
86
+ var VERSION = true ? "7.0.12" : "0.0.0-test";
87
87
 
88
88
  // src/util/download/download.ts
89
89
  var download = async ({
@@ -3328,6 +3328,7 @@ async function toResponseMessages({
3328
3328
  tools
3329
3329
  }) {
3330
3330
  const responseMessages = [];
3331
+ const toolCallOrder = /* @__PURE__ */ new Map();
3331
3332
  const content = [];
3332
3333
  for (const part of inputContent) {
3333
3334
  if (part.type === "source") {
@@ -3378,6 +3379,9 @@ async function toResponseMessages({
3378
3379
  });
3379
3380
  break;
3380
3381
  case "tool-call":
3382
+ if (!toolCallOrder.has(part.toolCallId)) {
3383
+ toolCallOrder.set(part.toolCallId, toolCallOrder.size);
3384
+ }
3381
3385
  content.push({
3382
3386
  type: "tool-call",
3383
3387
  toolCallId: part.toolCallId,
@@ -3485,11 +3489,37 @@ async function toResponseMessages({
3485
3489
  if (toolResultContent.length > 0) {
3486
3490
  responseMessages.push({
3487
3491
  role: "tool",
3488
- content: toolResultContent
3492
+ content: sortToolResultContentByToolCallOrder({
3493
+ toolResultContent,
3494
+ toolCallOrder
3495
+ })
3489
3496
  });
3490
3497
  }
3491
3498
  return responseMessages;
3492
3499
  }
3500
+ function sortToolResultContentByToolCallOrder({
3501
+ toolResultContent,
3502
+ toolCallOrder
3503
+ }) {
3504
+ const sortedToolResults = toolResultContent.filter((part) => part.type === "tool-result").map((part, index) => ({ part, index })).sort((a, b) => {
3505
+ const aOrder = toolCallOrder.get(a.part.toolCallId);
3506
+ const bOrder = toolCallOrder.get(b.part.toolCallId);
3507
+ if (aOrder == null && bOrder == null) {
3508
+ return a.index - b.index;
3509
+ }
3510
+ if (aOrder == null) {
3511
+ return 1;
3512
+ }
3513
+ if (bOrder == null) {
3514
+ return -1;
3515
+ }
3516
+ return aOrder - bOrder || a.index - b.index;
3517
+ }).map(({ part }) => part);
3518
+ let toolResultIndex = 0;
3519
+ return toolResultContent.map(
3520
+ (part) => part.type === "tool-result" ? sortedToolResults[toolResultIndex++] : part
3521
+ );
3522
+ }
3493
3523
  export {
3494
3524
  DefaultStepResult,
3495
3525
  addLanguageModelUsage,