ai 7.0.4 → 7.0.5

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,13 @@
1
1
  # ai
2
2
 
3
+ ## 7.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - a2750db: fix(ai): prune orphaned tool-approval responses in `pruneMessages`
8
+
9
+ When pruning a specific tool by name (`toolCalls: [{ type, tools: [...] }]`), `pruneMessages` left the tool's `tool-approval-response` in place while removing its `tool-approval-request` and `tool-call`. The tool name of an approval response was resolved per-message, but approval responses live in a separate `tool` message from their approval request, so the name could never be resolved and the response was always kept. Tool name resolution is now done across all messages, so approval requests and responses are pruned together.
10
+
3
11
  ## 7.0.4
4
12
 
5
13
  ### 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.4" : "0.0.0-test";
1092
+ var VERSION = true ? "7.0.5" : "0.0.0-test";
1093
1093
 
1094
1094
  // src/util/download/download.ts
1095
1095
  var download = async ({
@@ -13262,29 +13262,44 @@ function pruneMessages({
13262
13262
  }
13263
13263
  }
13264
13264
  }
13265
+ const toolCallIdToToolName = /* @__PURE__ */ new Map();
13266
+ for (const message of messages) {
13267
+ if ((message.role === "assistant" || message.role === "tool") && typeof message.content !== "string") {
13268
+ for (const part of message.content) {
13269
+ if (part.type === "tool-call" || part.type === "tool-result") {
13270
+ toolCallIdToToolName.set(part.toolCallId, part.toolName);
13271
+ }
13272
+ }
13273
+ }
13274
+ }
13275
+ const approvalIdToToolName = /* @__PURE__ */ new Map();
13276
+ for (const message of messages) {
13277
+ if ((message.role === "assistant" || message.role === "tool") && typeof message.content !== "string") {
13278
+ for (const part of message.content) {
13279
+ if (part.type === "tool-approval-request") {
13280
+ const toolName = toolCallIdToToolName.get(part.toolCallId);
13281
+ if (toolName != null) {
13282
+ approvalIdToToolName.set(part.approvalId, toolName);
13283
+ }
13284
+ }
13285
+ }
13286
+ }
13287
+ }
13265
13288
  messages = messages.map((message, messageIndex) => {
13266
13289
  if (message.role !== "assistant" && message.role !== "tool" || typeof message.content === "string" || keepLastMessagesCount && messageIndex >= messages.length - keepLastMessagesCount) {
13267
13290
  return message;
13268
13291
  }
13269
- const toolCallIdToToolName = {};
13270
- const approvalIdToToolName = {};
13271
13292
  return {
13272
13293
  ...message,
13273
13294
  content: message.content.filter((part) => {
13274
13295
  if (part.type !== "tool-call" && part.type !== "tool-result" && part.type !== "tool-approval-request" && part.type !== "tool-approval-response") {
13275
13296
  return true;
13276
13297
  }
13277
- if (part.type === "tool-call") {
13278
- toolCallIdToToolName[part.toolCallId] = part.toolName;
13279
- } else if (part.type === "tool-approval-request") {
13280
- approvalIdToToolName[part.approvalId] = toolCallIdToToolName[part.toolCallId];
13281
- }
13282
13298
  if ((part.type === "tool-call" || part.type === "tool-result") && keptToolCallIds.has(part.toolCallId) || (part.type === "tool-approval-request" || part.type === "tool-approval-response") && keptApprovalIds.has(part.approvalId)) {
13283
13299
  return true;
13284
13300
  }
13285
- return toolCall.tools != null && !toolCall.tools.includes(
13286
- part.type === "tool-call" || part.type === "tool-result" ? part.toolName : approvalIdToToolName[part.approvalId]
13287
- );
13301
+ const partToolName = part.type === "tool-call" || part.type === "tool-result" ? part.toolName : approvalIdToToolName.get(part.approvalId);
13302
+ return toolCall.tools != null && partToolName != null && !toolCall.tools.includes(partToolName);
13288
13303
  })
13289
13304
  };
13290
13305
  });