ai 7.0.4 → 7.0.6
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 +16 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +26 -11
- package/dist/index.js.map +1 -1
- package/dist/internal/index.js +1 -1
- package/package.json +4 -4
- package/src/agent/agent.ts +14 -0
- package/src/generate-text/prune-messages.ts +45 -16
package/dist/internal/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -42,9 +42,9 @@
|
|
|
42
42
|
}
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@ai-sdk/gateway": "4.0.
|
|
46
|
-
"@ai-sdk/provider": "
|
|
47
|
-
"@ai-sdk/provider
|
|
45
|
+
"@ai-sdk/gateway": "4.0.5",
|
|
46
|
+
"@ai-sdk/provider-utils": "5.0.1",
|
|
47
|
+
"@ai-sdk/provider": "4.0.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@edge-runtime/vm": "^5.0.0",
|
package/src/agent/agent.ts
CHANGED
|
@@ -106,11 +106,25 @@ export type AgentCallParameters<
|
|
|
106
106
|
*/
|
|
107
107
|
onToolExecutionStart?: OnToolExecutionStartCallback<TOOLS>;
|
|
108
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Callback that is called before each tool execution begins.
|
|
111
|
+
*
|
|
112
|
+
* @deprecated Use `onToolExecutionStart` instead.
|
|
113
|
+
*/
|
|
114
|
+
experimental_onToolCallStart?: OnToolExecutionStartCallback<TOOLS>;
|
|
115
|
+
|
|
109
116
|
/**
|
|
110
117
|
* Callback that is called after each tool execution completes.
|
|
111
118
|
*/
|
|
112
119
|
onToolExecutionEnd?: OnToolExecutionEndCallback<TOOLS>;
|
|
113
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Callback that is called after each tool execution completes.
|
|
123
|
+
*
|
|
124
|
+
* @deprecated Use `onToolExecutionEnd` instead.
|
|
125
|
+
*/
|
|
126
|
+
experimental_onToolCallFinish?: OnToolExecutionEndCallback<TOOLS>;
|
|
127
|
+
|
|
114
128
|
/**
|
|
115
129
|
* Callback that is called when each step (LLM call) ends, including intermediate steps.
|
|
116
130
|
*/
|
|
@@ -100,6 +100,44 @@ export function pruneMessages({
|
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
// Build global maps from tool call id and approval id to tool name.
|
|
104
|
+
// These must be global (not per-message) because a `tool-approval-response`
|
|
105
|
+
// lives in a separate `tool` message from its `tool-approval-request`
|
|
106
|
+
// (assistant message), so the tool name of a response can only be resolved
|
|
107
|
+
// by looking across messages. Resolving names per-message left responses
|
|
108
|
+
// unresolved, which caused them to be kept while their request was pruned,
|
|
109
|
+
// producing orphaned approval responses.
|
|
110
|
+
const toolCallIdToToolName = new Map<string, string>();
|
|
111
|
+
for (const message of messages) {
|
|
112
|
+
if (
|
|
113
|
+
(message.role === 'assistant' || message.role === 'tool') &&
|
|
114
|
+
typeof message.content !== 'string'
|
|
115
|
+
) {
|
|
116
|
+
for (const part of message.content) {
|
|
117
|
+
if (part.type === 'tool-call' || part.type === 'tool-result') {
|
|
118
|
+
toolCallIdToToolName.set(part.toolCallId, part.toolName);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const approvalIdToToolName = new Map<string, string>();
|
|
125
|
+
for (const message of messages) {
|
|
126
|
+
if (
|
|
127
|
+
(message.role === 'assistant' || message.role === 'tool') &&
|
|
128
|
+
typeof message.content !== 'string'
|
|
129
|
+
) {
|
|
130
|
+
for (const part of message.content) {
|
|
131
|
+
if (part.type === 'tool-approval-request') {
|
|
132
|
+
const toolName = toolCallIdToToolName.get(part.toolCallId);
|
|
133
|
+
if (toolName != null) {
|
|
134
|
+
approvalIdToToolName.set(part.approvalId, toolName);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
103
141
|
messages = messages.map((message, messageIndex) => {
|
|
104
142
|
if (
|
|
105
143
|
(message.role !== 'assistant' && message.role !== 'tool') ||
|
|
@@ -110,9 +148,6 @@ export function pruneMessages({
|
|
|
110
148
|
return message;
|
|
111
149
|
}
|
|
112
150
|
|
|
113
|
-
const toolCallIdToToolName: Record<string, string> = {};
|
|
114
|
-
const approvalIdToToolName: Record<string, string> = {};
|
|
115
|
-
|
|
116
151
|
return {
|
|
117
152
|
...message,
|
|
118
153
|
content: message.content.filter(part => {
|
|
@@ -126,14 +161,6 @@ export function pruneMessages({
|
|
|
126
161
|
return true;
|
|
127
162
|
}
|
|
128
163
|
|
|
129
|
-
// track tool calls and approvals:
|
|
130
|
-
if (part.type === 'tool-call') {
|
|
131
|
-
toolCallIdToToolName[part.toolCallId] = part.toolName;
|
|
132
|
-
} else if (part.type === 'tool-approval-request') {
|
|
133
|
-
approvalIdToToolName[part.approvalId] =
|
|
134
|
-
toolCallIdToToolName[part.toolCallId];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
164
|
// keep parts that are associated with a tool call or approval that needs to be kept:
|
|
138
165
|
if (
|
|
139
166
|
((part.type === 'tool-call' || part.type === 'tool-result') &&
|
|
@@ -146,13 +173,15 @@ export function pruneMessages({
|
|
|
146
173
|
}
|
|
147
174
|
|
|
148
175
|
// keep parts that are not associated with a tool that should be removed:
|
|
176
|
+
const partToolName =
|
|
177
|
+
part.type === 'tool-call' || part.type === 'tool-result'
|
|
178
|
+
? part.toolName
|
|
179
|
+
: approvalIdToToolName.get(part.approvalId);
|
|
180
|
+
|
|
149
181
|
return (
|
|
150
182
|
toolCall.tools != null &&
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
? part.toolName
|
|
154
|
-
: approvalIdToToolName[part.approvalId],
|
|
155
|
-
)
|
|
183
|
+
partToolName != null &&
|
|
184
|
+
!toolCall.tools.includes(partToolName)
|
|
156
185
|
);
|
|
157
186
|
}),
|
|
158
187
|
} as AssistantModelMessage | ToolModelMessage;
|