langchain 1.0.2 → 1.0.4
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 +18 -0
- package/dist/agents/ReactAgent.cjs +21 -41
- package/dist/agents/ReactAgent.cjs.map +1 -1
- package/dist/agents/ReactAgent.js +21 -41
- package/dist/agents/ReactAgent.js.map +1 -1
- package/dist/agents/annotation.cjs +7 -4
- package/dist/agents/annotation.cjs.map +1 -1
- package/dist/agents/annotation.js +8 -5
- package/dist/agents/annotation.js.map +1 -1
- package/dist/agents/middleware/contextEditing.d.cts.map +1 -1
- package/dist/agents/middleware/contextEditing.d.ts.map +1 -1
- package/dist/agents/middleware/dynamicSystemPrompt.d.cts.map +1 -1
- package/dist/agents/middleware/dynamicSystemPrompt.d.ts.map +1 -1
- package/dist/agents/middleware/hitl.cjs +7 -2
- package/dist/agents/middleware/hitl.cjs.map +1 -1
- package/dist/agents/middleware/hitl.d.cts.map +1 -1
- package/dist/agents/middleware/hitl.d.ts.map +1 -1
- package/dist/agents/middleware/hitl.js +7 -2
- package/dist/agents/middleware/hitl.js.map +1 -1
- package/dist/agents/middleware/index.cjs +2 -1
- package/dist/agents/middleware/index.js +2 -1
- package/dist/agents/middleware/llmToolSelector.d.cts +4 -4
- package/dist/agents/middleware/llmToolSelector.d.cts.map +1 -1
- package/dist/agents/middleware/{callLimit.cjs → modelCallLimit.cjs} +22 -7
- package/dist/agents/middleware/modelCallLimit.cjs.map +1 -0
- package/dist/agents/middleware/{callLimit.d.cts → modelCallLimit.d.cts} +12 -3
- package/dist/agents/middleware/modelCallLimit.d.cts.map +1 -0
- package/dist/agents/middleware/{callLimit.d.ts → modelCallLimit.d.ts} +12 -3
- package/dist/agents/middleware/modelCallLimit.d.ts.map +1 -0
- package/dist/agents/middleware/{callLimit.js → modelCallLimit.js} +22 -7
- package/dist/agents/middleware/modelCallLimit.js.map +1 -0
- package/dist/agents/middleware/promptCaching.d.cts.map +1 -1
- package/dist/agents/middleware/promptCaching.d.ts.map +1 -1
- package/dist/agents/middleware/summarization.cjs +274 -52
- package/dist/agents/middleware/summarization.cjs.map +1 -1
- package/dist/agents/middleware/summarization.d.cts +326 -30
- package/dist/agents/middleware/summarization.d.cts.map +1 -1
- package/dist/agents/middleware/summarization.d.ts +325 -29
- package/dist/agents/middleware/summarization.d.ts.map +1 -1
- package/dist/agents/middleware/summarization.js +276 -54
- package/dist/agents/middleware/summarization.js.map +1 -1
- package/dist/agents/middleware/todoListMiddleware.d.cts.map +1 -1
- package/dist/agents/middleware/todoListMiddleware.d.ts.map +1 -1
- package/dist/agents/middleware/toolCallLimit.cjs +215 -93
- package/dist/agents/middleware/toolCallLimit.cjs.map +1 -1
- package/dist/agents/middleware/toolCallLimit.d.cts +48 -37
- package/dist/agents/middleware/toolCallLimit.d.cts.map +1 -1
- package/dist/agents/middleware/toolCallLimit.d.ts +48 -37
- package/dist/agents/middleware/toolCallLimit.d.ts.map +1 -1
- package/dist/agents/middleware/toolCallLimit.js +216 -94
- package/dist/agents/middleware/toolCallLimit.js.map +1 -1
- package/dist/agents/middleware/toolRetry.cjs +224 -0
- package/dist/agents/middleware/toolRetry.cjs.map +1 -0
- package/dist/agents/middleware/toolRetry.d.cts +179 -0
- package/dist/agents/middleware/toolRetry.d.cts.map +1 -0
- package/dist/agents/middleware/toolRetry.d.ts +179 -0
- package/dist/agents/middleware/toolRetry.d.ts.map +1 -0
- package/dist/agents/middleware/toolRetry.js +223 -0
- package/dist/agents/middleware/toolRetry.js.map +1 -0
- package/dist/agents/middleware/types.d.cts +21 -19
- package/dist/agents/middleware/types.d.cts.map +1 -1
- package/dist/agents/middleware/types.d.ts +21 -19
- package/dist/agents/middleware/types.d.ts.map +1 -1
- package/dist/agents/middleware/utils.cjs +7 -0
- package/dist/agents/middleware/utils.cjs.map +1 -1
- package/dist/agents/middleware/utils.d.cts.map +1 -1
- package/dist/agents/middleware/utils.d.ts.map +1 -1
- package/dist/agents/middleware/utils.js +7 -1
- package/dist/agents/middleware/utils.js.map +1 -1
- package/dist/agents/middleware.cjs.map +1 -1
- package/dist/agents/middleware.d.cts +4 -4
- package/dist/agents/middleware.d.cts.map +1 -1
- package/dist/agents/middleware.d.ts +4 -4
- package/dist/agents/middleware.d.ts.map +1 -1
- package/dist/agents/middleware.js.map +1 -1
- package/dist/agents/nodes/AgentNode.cjs +21 -51
- package/dist/agents/nodes/AgentNode.cjs.map +1 -1
- package/dist/agents/nodes/AgentNode.js +23 -53
- package/dist/agents/nodes/AgentNode.js.map +1 -1
- package/dist/agents/nodes/ToolNode.cjs +12 -18
- package/dist/agents/nodes/ToolNode.cjs.map +1 -1
- package/dist/agents/nodes/ToolNode.js +12 -18
- package/dist/agents/nodes/ToolNode.js.map +1 -1
- package/dist/agents/nodes/middleware.cjs +9 -7
- package/dist/agents/nodes/middleware.cjs.map +1 -1
- package/dist/agents/nodes/middleware.js +10 -8
- package/dist/agents/nodes/middleware.js.map +1 -1
- package/dist/agents/nodes/types.d.cts +1 -1
- package/dist/agents/nodes/types.d.cts.map +1 -1
- package/dist/agents/nodes/types.d.ts +1 -1
- package/dist/agents/nodes/types.d.ts.map +1 -1
- package/dist/agents/nodes/utils.cjs +5 -1
- package/dist/agents/nodes/utils.cjs.map +1 -1
- package/dist/agents/nodes/utils.js +5 -1
- package/dist/agents/nodes/utils.js.map +1 -1
- package/dist/agents/responses.cjs +0 -1
- package/dist/agents/responses.js +1 -1
- package/dist/agents/runtime.d.cts +11 -27
- package/dist/agents/runtime.d.cts.map +1 -1
- package/dist/agents/runtime.d.ts +11 -27
- package/dist/agents/runtime.d.ts.map +1 -1
- package/dist/agents/state.cjs +45 -0
- package/dist/agents/state.cjs.map +1 -0
- package/dist/agents/state.js +44 -0
- package/dist/agents/state.js.map +1 -0
- package/dist/agents/types.d.cts +1 -1
- package/dist/agents/types.d.cts.map +1 -1
- package/dist/agents/types.d.ts +1 -1
- package/dist/agents/types.d.ts.map +1 -1
- package/dist/agents/utils.cjs +10 -2
- package/dist/agents/utils.cjs.map +1 -1
- package/dist/agents/utils.js +10 -2
- package/dist/agents/utils.js.map +1 -1
- package/dist/chat_models/universal.cjs +2 -2
- package/dist/chat_models/universal.cjs.map +1 -1
- package/dist/chat_models/universal.js +2 -2
- package/dist/chat_models/universal.js.map +1 -1
- package/dist/index.cjs +6 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -5
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/dist/agents/middleware/callLimit.cjs.map +0 -1
- package/dist/agents/middleware/callLimit.d.cts.map +0 -1
- package/dist/agents/middleware/callLimit.d.ts.map +0 -1
- package/dist/agents/middleware/callLimit.js.map +0 -1
- package/dist/agents/tools.d.cts +0 -9
- package/dist/agents/tools.d.cts.map +0 -1
- package/dist/agents/tools.d.ts +0 -9
- package/dist/agents/tools.d.ts.map +0 -1
|
@@ -2,64 +2,54 @@ const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
|
2
2
|
const require_middleware = require('../middleware.cjs');
|
|
3
3
|
const __langchain_core_messages = require_rolldown_runtime.__toESM(require("@langchain/core/messages"));
|
|
4
4
|
const zod_v3 = require_rolldown_runtime.__toESM(require("zod/v3"));
|
|
5
|
+
const zod_v4 = require_rolldown_runtime.__toESM(require("zod/v4"));
|
|
5
6
|
|
|
6
7
|
//#region src/agents/middleware/toolCallLimit.ts
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
* Build the error message content for ToolMessage when limit is exceeded.
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* If undefined, count all tool calls.
|
|
13
|
-
* @returns The total number of tool calls (optionally filtered by toolName).
|
|
14
|
-
*/
|
|
15
|
-
function countToolCallsInMessages(messages, toolName) {
|
|
16
|
-
let count = 0;
|
|
17
|
-
for (const message of messages) if (__langchain_core_messages.AIMessage.isInstance(message) && message.tool_calls) if (toolName === void 0) count += message.tool_calls.length;
|
|
18
|
-
else count += message.tool_calls.filter((tc) => tc.name === toolName).length;
|
|
19
|
-
return count;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Get messages from the current run (after the last HumanMessage).
|
|
11
|
+
* This message is sent to the model, so it should not reference thread/run concepts
|
|
12
|
+
* that the model has no notion of.
|
|
23
13
|
*
|
|
24
|
-
* @param
|
|
25
|
-
* @returns
|
|
14
|
+
* @param toolName - Tool name being limited (if specific tool), or undefined for all tools.
|
|
15
|
+
* @returns A concise message instructing the model not to call the tool again.
|
|
26
16
|
*/
|
|
27
|
-
function
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*/
|
|
31
|
-
let lastHumanIndex = -1;
|
|
32
|
-
for (let i = messages.length - 1; i >= 0; i--) if (__langchain_core_messages.HumanMessage.isInstance(messages[i])) {
|
|
33
|
-
lastHumanIndex = i;
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* If no HumanMessage found, return all messages
|
|
38
|
-
*/
|
|
39
|
-
if (lastHumanIndex === -1) return messages;
|
|
40
|
-
/**
|
|
41
|
-
* Return messages after the last HumanMessage
|
|
42
|
-
*/
|
|
43
|
-
return messages.slice(lastHumanIndex + 1);
|
|
17
|
+
function buildToolMessageContent(toolName) {
|
|
18
|
+
if (toolName) return `Tool call limit exceeded. Do not call '${toolName}' again.`;
|
|
19
|
+
return "Tool call limit exceeded. Do not make additional tool calls.";
|
|
44
20
|
}
|
|
21
|
+
const VALID_EXIT_BEHAVIORS = [
|
|
22
|
+
"continue",
|
|
23
|
+
"error",
|
|
24
|
+
"end"
|
|
25
|
+
];
|
|
26
|
+
const DEFAULT_EXIT_BEHAVIOR = "continue";
|
|
45
27
|
/**
|
|
46
|
-
* Build
|
|
28
|
+
* Build the final AI message content for 'end' behavior.
|
|
29
|
+
*
|
|
30
|
+
* This message is displayed to the user, so it should include detailed information
|
|
31
|
+
* about which limits were exceeded.
|
|
47
32
|
*
|
|
48
33
|
* @param threadCount - Current thread tool call count.
|
|
49
34
|
* @param runCount - Current run tool call count.
|
|
50
35
|
* @param threadLimit - Thread tool call limit (if set).
|
|
51
36
|
* @param runLimit - Run tool call limit (if set).
|
|
52
37
|
* @param toolName - Tool name being limited (if specific tool), or undefined for all tools.
|
|
53
|
-
* @returns A formatted message describing which limits were
|
|
38
|
+
* @returns A formatted message describing which limits were exceeded.
|
|
54
39
|
*/
|
|
55
|
-
function
|
|
56
|
-
const toolDesc = toolName ? `'${toolName}' tool
|
|
40
|
+
function buildFinalAIMessageContent(threadCount, runCount, threadLimit, runLimit, toolName) {
|
|
41
|
+
const toolDesc = toolName ? `'${toolName}' tool` : "Tool";
|
|
57
42
|
const exceededLimits = [];
|
|
58
|
-
if (threadLimit !== void 0 && threadCount
|
|
59
|
-
if (runLimit !== void 0 && runCount
|
|
60
|
-
|
|
43
|
+
if (threadLimit !== void 0 && threadCount > threadLimit) exceededLimits.push(`thread limit exceeded (${threadCount}/${threadLimit} calls)`);
|
|
44
|
+
if (runLimit !== void 0 && runCount > runLimit) exceededLimits.push(`run limit exceeded (${runCount}/${runLimit} calls)`);
|
|
45
|
+
const limitsText = exceededLimits.join(" and ");
|
|
46
|
+
return `${toolDesc} call limit reached: ${limitsText}.`;
|
|
61
47
|
}
|
|
62
48
|
/**
|
|
49
|
+
* Schema for the exit behavior.
|
|
50
|
+
*/
|
|
51
|
+
const exitBehaviorSchema = zod_v3.z.enum(VALID_EXIT_BEHAVIORS).default(DEFAULT_EXIT_BEHAVIOR);
|
|
52
|
+
/**
|
|
63
53
|
* Exception raised when tool call limits are exceeded.
|
|
64
54
|
*
|
|
65
55
|
* This exception is raised when the configured exit behavior is 'error'
|
|
@@ -87,7 +77,7 @@ var ToolCallLimitExceededError = class extends Error {
|
|
|
87
77
|
*/
|
|
88
78
|
toolName;
|
|
89
79
|
constructor(threadCount, runCount, threadLimit, runLimit, toolName = void 0) {
|
|
90
|
-
const message =
|
|
80
|
+
const message = buildFinalAIMessageContent(threadCount, runCount, threadLimit, runLimit, toolName);
|
|
91
81
|
super(message);
|
|
92
82
|
this.name = "ToolCallLimitExceededError";
|
|
93
83
|
this.threadCount = threadCount;
|
|
@@ -104,9 +94,17 @@ const ToolCallLimitOptionsSchema = zod_v3.z.object({
|
|
|
104
94
|
toolName: zod_v3.z.string().optional(),
|
|
105
95
|
threadLimit: zod_v3.z.number().optional(),
|
|
106
96
|
runLimit: zod_v3.z.number().optional(),
|
|
107
|
-
exitBehavior:
|
|
97
|
+
exitBehavior: exitBehaviorSchema
|
|
108
98
|
});
|
|
109
99
|
/**
|
|
100
|
+
* Middleware state schema to track the number of model calls made at the thread and run level.
|
|
101
|
+
*/
|
|
102
|
+
const stateSchema = zod_v3.z.object({
|
|
103
|
+
threadToolCallCount: zod_v3.z.record(zod_v3.z.string(), zod_v3.z.number()).default({}),
|
|
104
|
+
runToolCallCount: zod_v3.z.record(zod_v3.z.string(), zod_v3.z.number()).default({})
|
|
105
|
+
});
|
|
106
|
+
const DEFAULT_TOOL_COUNT_KEY = "__all__";
|
|
107
|
+
/**
|
|
110
108
|
* Middleware that tracks tool call counts and enforces limits.
|
|
111
109
|
*
|
|
112
110
|
* This middleware monitors the number of tool calls made during agent execution
|
|
@@ -124,68 +122,66 @@ const ToolCallLimitOptionsSchema = zod_v3.z.object({
|
|
|
124
122
|
* @param options.threadLimit - Maximum number of tool calls allowed per thread. undefined means no limit.
|
|
125
123
|
* @param options.runLimit - Maximum number of tool calls allowed per run. undefined means no limit.
|
|
126
124
|
* @param options.exitBehavior - What to do when limits are exceeded.
|
|
127
|
-
* - "
|
|
128
|
-
* - "error":
|
|
125
|
+
* - "continue": Block exceeded tools with error messages, let other tools continue. Model decides when to end. (default)
|
|
126
|
+
* - "error": Raise a ToolCallLimitExceededError exception
|
|
127
|
+
* - "end": Stop execution immediately with a ToolMessage + AI message for the single tool call that exceeded the limit. Raises NotImplementedError if there are multiple tool calls.
|
|
129
128
|
*
|
|
130
|
-
* @throws {Error} If both limits are undefined
|
|
129
|
+
* @throws {Error} If both limits are undefined, if exitBehavior is invalid, or if runLimit exceeds threadLimit.
|
|
130
|
+
* @throws {NotImplementedError} If exitBehavior is "end" and there are multiple tool calls.
|
|
131
131
|
*
|
|
132
|
-
* @example
|
|
132
|
+
* @example Continue execution with blocked tools (default)
|
|
133
133
|
* ```ts
|
|
134
134
|
* import { toolCallLimitMiddleware } from "@langchain/langchain/agents/middleware";
|
|
135
135
|
* import { createAgent } from "@langchain/langchain/agents";
|
|
136
136
|
*
|
|
137
|
-
*
|
|
137
|
+
* // Block exceeded tools but let other tools and model continue
|
|
138
|
+
* const limiter = toolCallLimitMiddleware({
|
|
138
139
|
* threadLimit: 20,
|
|
139
140
|
* runLimit: 10,
|
|
140
|
-
* exitBehavior: "
|
|
141
|
+
* exitBehavior: "continue", // default
|
|
141
142
|
* });
|
|
142
143
|
*
|
|
143
144
|
* const agent = createAgent({
|
|
144
145
|
* model: "openai:gpt-4o",
|
|
145
|
-
* middleware: [
|
|
146
|
+
* middleware: [limiter]
|
|
146
147
|
* });
|
|
147
148
|
* ```
|
|
148
149
|
*
|
|
149
|
-
* @example
|
|
150
|
+
* @example Stop immediately when limit exceeded
|
|
150
151
|
* ```ts
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
* const searchLimiter = toolCallLimitMiddleware({
|
|
155
|
-
* toolName: "search",
|
|
156
|
-
* threadLimit: 5,
|
|
157
|
-
* runLimit: 3,
|
|
152
|
+
* // End execution immediately with an AI message
|
|
153
|
+
* const limiter = toolCallLimitMiddleware({
|
|
154
|
+
* runLimit: 5,
|
|
158
155
|
* exitBehavior: "end"
|
|
159
156
|
* });
|
|
160
157
|
*
|
|
161
158
|
* const agent = createAgent({
|
|
162
159
|
* model: "openai:gpt-4o",
|
|
163
|
-
* middleware: [
|
|
160
|
+
* middleware: [limiter]
|
|
164
161
|
* });
|
|
165
162
|
* ```
|
|
166
163
|
*
|
|
167
|
-
* @example
|
|
164
|
+
* @example Raise exception on limit
|
|
168
165
|
* ```ts
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
* const globalLimiter = toolCallLimitMiddleware({
|
|
173
|
-
* threadLimit: 20,
|
|
174
|
-
* runLimit: 10,
|
|
175
|
-
* exitBehavior: "end"
|
|
176
|
-
* });
|
|
177
|
-
*
|
|
178
|
-
* const searchLimiter = toolCallLimitMiddleware({
|
|
166
|
+
* // Strict limit with exception handling
|
|
167
|
+
* const limiter = toolCallLimitMiddleware({
|
|
179
168
|
* toolName: "search",
|
|
180
169
|
* threadLimit: 5,
|
|
181
|
-
*
|
|
182
|
-
* exitBehavior: "end"
|
|
170
|
+
* exitBehavior: "error"
|
|
183
171
|
* });
|
|
184
172
|
*
|
|
185
173
|
* const agent = createAgent({
|
|
186
174
|
* model: "openai:gpt-4o",
|
|
187
|
-
* middleware: [
|
|
175
|
+
* middleware: [limiter]
|
|
188
176
|
* });
|
|
177
|
+
*
|
|
178
|
+
* try {
|
|
179
|
+
* const result = await agent.invoke({ messages: [new HumanMessage("Task")] });
|
|
180
|
+
* } catch (error) {
|
|
181
|
+
* if (error instanceof ToolCallLimitExceededError) {
|
|
182
|
+
* console.log(`Search limit exceeded: ${error}`);
|
|
183
|
+
* }
|
|
184
|
+
* }
|
|
189
185
|
* ```
|
|
190
186
|
*/
|
|
191
187
|
function toolCallLimitMiddleware(options) {
|
|
@@ -194,47 +190,173 @@ function toolCallLimitMiddleware(options) {
|
|
|
194
190
|
*/
|
|
195
191
|
if (options.threadLimit === void 0 && options.runLimit === void 0) throw new Error("At least one limit must be specified (threadLimit or runLimit)");
|
|
196
192
|
/**
|
|
197
|
-
*
|
|
193
|
+
* Validate exitBehavior (Zod schema already validates, but provide helpful error)
|
|
194
|
+
*/
|
|
195
|
+
const exitBehavior = options.exitBehavior ?? DEFAULT_EXIT_BEHAVIOR;
|
|
196
|
+
const parseResult = exitBehaviorSchema.safeParse(exitBehavior);
|
|
197
|
+
if (!parseResult.success) throw new Error(zod_v4.z.prettifyError(parseResult.error).slice(2));
|
|
198
|
+
/**
|
|
199
|
+
* Validate that runLimit does not exceed threadLimit
|
|
198
200
|
*/
|
|
199
|
-
|
|
200
|
-
if (exitBehavior !== "end" && exitBehavior !== "error") throw new Error(`Invalid exit behavior: ${exitBehavior}. Must be 'end' or 'error'`);
|
|
201
|
+
if (options.threadLimit !== void 0 && options.runLimit !== void 0 && options.runLimit > options.threadLimit) throw new Error(`runLimit (${options.runLimit}) cannot exceed threadLimit (${options.threadLimit}). The run limit should be less than or equal to the thread limit.`);
|
|
201
202
|
/**
|
|
202
203
|
* Generate the middleware name based on the tool name
|
|
203
204
|
*/
|
|
204
205
|
const middlewareName = options.toolName ? `ToolCallLimitMiddleware[${options.toolName}]` : "ToolCallLimitMiddleware";
|
|
205
206
|
return require_middleware.createMiddleware({
|
|
206
207
|
name: middlewareName,
|
|
207
|
-
|
|
208
|
+
stateSchema,
|
|
209
|
+
afterModel: {
|
|
208
210
|
canJumpTo: ["end"],
|
|
209
211
|
hook: (state) => {
|
|
210
|
-
const messages = state.messages;
|
|
211
212
|
/**
|
|
212
|
-
*
|
|
213
|
+
* Get the last AI message to check for tool calls
|
|
214
|
+
*/
|
|
215
|
+
const lastAIMessage = [...state.messages].reverse().find(__langchain_core_messages.AIMessage.isInstance);
|
|
216
|
+
if (!lastAIMessage || !lastAIMessage.tool_calls) return void 0;
|
|
217
|
+
/**
|
|
218
|
+
* Helper to check if limit would be exceeded by one more call
|
|
219
|
+
*/
|
|
220
|
+
const wouldExceedLimit = (threadCount, runCount) => {
|
|
221
|
+
return options.threadLimit !== void 0 && threadCount + 1 > options.threadLimit || options.runLimit !== void 0 && runCount + 1 > options.runLimit;
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* Helper to check if a tool call matches our filter
|
|
225
|
+
*/
|
|
226
|
+
const matchesToolFilter = (toolCall) => {
|
|
227
|
+
return options.toolName === void 0 || toolCall.name === options.toolName;
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Separate tool calls into allowed and blocked based on limits
|
|
231
|
+
*/
|
|
232
|
+
const separateToolCalls = (toolCalls, threadCount, runCount) => {
|
|
233
|
+
const allowed$1 = [];
|
|
234
|
+
const blocked$1 = [];
|
|
235
|
+
let tempThreadCount = threadCount;
|
|
236
|
+
let tempRunCount = runCount;
|
|
237
|
+
for (const toolCall of toolCalls) {
|
|
238
|
+
if (!matchesToolFilter(toolCall)) continue;
|
|
239
|
+
if (wouldExceedLimit(tempThreadCount, tempRunCount)) blocked$1.push(toolCall);
|
|
240
|
+
else {
|
|
241
|
+
allowed$1.push(toolCall);
|
|
242
|
+
tempThreadCount += 1;
|
|
243
|
+
tempRunCount += 1;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
allowed: allowed$1,
|
|
248
|
+
blocked: blocked$1,
|
|
249
|
+
finalThreadCount: tempThreadCount,
|
|
250
|
+
finalRunCount: tempRunCount + blocked$1.length
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* Get the count key for this middleware instance
|
|
255
|
+
*/
|
|
256
|
+
const countKey = options.toolName ?? DEFAULT_TOOL_COUNT_KEY;
|
|
257
|
+
/**
|
|
258
|
+
* Get current counts
|
|
259
|
+
*/
|
|
260
|
+
const threadCounts = { ...state.threadToolCallCount ?? {} };
|
|
261
|
+
const runCounts = { ...state.runToolCallCount ?? {} };
|
|
262
|
+
const currentThreadCount = threadCounts[countKey] ?? 0;
|
|
263
|
+
const currentRunCount = runCounts[countKey] ?? 0;
|
|
264
|
+
/**
|
|
265
|
+
* Separate tool calls into allowed and blocked
|
|
266
|
+
*/
|
|
267
|
+
const { allowed, blocked, finalThreadCount, finalRunCount } = separateToolCalls(lastAIMessage.tool_calls, currentThreadCount, currentRunCount);
|
|
268
|
+
/**
|
|
269
|
+
* Update counts:
|
|
270
|
+
* - Thread count includes only allowed calls (blocked calls don't count towards thread-level tracking)
|
|
271
|
+
* - Run count includes blocked calls since they were attempted in this run
|
|
272
|
+
*/
|
|
273
|
+
threadCounts[countKey] = finalThreadCount;
|
|
274
|
+
runCounts[countKey] = finalRunCount;
|
|
275
|
+
/**
|
|
276
|
+
* If no tool calls are blocked, just update counts
|
|
277
|
+
*/
|
|
278
|
+
if (blocked.length === 0) {
|
|
279
|
+
if (allowed.length > 0) return {
|
|
280
|
+
threadToolCallCount: threadCounts,
|
|
281
|
+
runToolCallCount: runCounts
|
|
282
|
+
};
|
|
283
|
+
return void 0;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Handle different exit behaviors
|
|
213
287
|
*/
|
|
214
|
-
|
|
288
|
+
if (exitBehavior === "error") {
|
|
289
|
+
const hypotheticalThreadCount = finalThreadCount + blocked.length;
|
|
290
|
+
throw new ToolCallLimitExceededError(hypotheticalThreadCount, finalRunCount, options.threadLimit, options.runLimit, options.toolName);
|
|
291
|
+
}
|
|
215
292
|
/**
|
|
216
|
-
*
|
|
293
|
+
* Build tool message content (sent to model - no thread/run details)
|
|
217
294
|
*/
|
|
218
|
-
const
|
|
219
|
-
const runCount = countToolCallsInMessages(runMessages, options.toolName);
|
|
295
|
+
const toolMsgContent = buildToolMessageContent(options.toolName);
|
|
220
296
|
/**
|
|
221
|
-
*
|
|
297
|
+
* Inject artificial error ToolMessages for blocked tool calls
|
|
222
298
|
*/
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
299
|
+
const artificialMessages = blocked.map((toolCall) => new __langchain_core_messages.ToolMessage({
|
|
300
|
+
content: toolMsgContent,
|
|
301
|
+
tool_call_id: toolCall.id,
|
|
302
|
+
name: toolCall.name,
|
|
303
|
+
status: "error"
|
|
304
|
+
}));
|
|
305
|
+
if (exitBehavior === "end") {
|
|
306
|
+
/**
|
|
307
|
+
* Check if there are tool calls to other tools that would continue executing
|
|
308
|
+
* For tool-specific limiters: check for calls to other tools
|
|
309
|
+
* For global limiters: check if there are multiple different tool types
|
|
310
|
+
*/
|
|
311
|
+
let otherTools = [];
|
|
312
|
+
if (options.toolName !== void 0)
|
|
313
|
+
/**
|
|
314
|
+
* Tool-specific limiter: check for calls to other tools
|
|
315
|
+
*/
|
|
316
|
+
otherTools = lastAIMessage.tool_calls.filter((tc) => tc.name !== options.toolName);
|
|
317
|
+
else {
|
|
318
|
+
/**
|
|
319
|
+
* Global limiter: check if there are multiple different tool types
|
|
320
|
+
* If there are allowed calls, those would execute
|
|
321
|
+
* But even if all are blocked, we can't handle multiple tool types with "end"
|
|
322
|
+
*/
|
|
323
|
+
const uniqueToolNames = new Set(lastAIMessage.tool_calls.map((tc) => tc.name).filter(Boolean));
|
|
324
|
+
if (uniqueToolNames.size > 1)
|
|
325
|
+
/**
|
|
326
|
+
* Multiple different tool types - use allowed calls to show which ones
|
|
327
|
+
*/
|
|
328
|
+
otherTools = allowed.length > 0 ? allowed : lastAIMessage.tool_calls;
|
|
329
|
+
}
|
|
330
|
+
if (otherTools.length > 0) {
|
|
331
|
+
const toolNames = Array.from(new Set(otherTools.map((tc) => tc.name).filter(Boolean))).join(", ");
|
|
332
|
+
throw new Error(`Cannot end execution with other tool calls pending. Found calls to: ${toolNames}. Use 'continue' or 'error' behavior instead.`);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Build final AI message content (displayed to user - includes thread/run details)
|
|
336
|
+
* Use hypothetical thread count (what it would have been if call wasn't blocked)
|
|
337
|
+
* to show which limit was actually exceeded
|
|
338
|
+
*/
|
|
339
|
+
const hypotheticalThreadCount = finalThreadCount + blocked.length;
|
|
340
|
+
const finalMsgContent = buildFinalAIMessageContent(hypotheticalThreadCount, finalRunCount, options.threadLimit, options.runLimit, options.toolName);
|
|
341
|
+
artificialMessages.push(new __langchain_core_messages.AIMessage(finalMsgContent));
|
|
342
|
+
return {
|
|
343
|
+
threadToolCallCount: threadCounts,
|
|
344
|
+
runToolCallCount: runCounts,
|
|
345
|
+
jumpTo: "end",
|
|
346
|
+
messages: artificialMessages
|
|
347
|
+
};
|
|
348
|
+
}
|
|
227
349
|
/**
|
|
228
|
-
*
|
|
350
|
+
* For exit_behavior="continue", return error messages to block exceeded tools
|
|
229
351
|
*/
|
|
230
|
-
const limitMessage = buildToolLimitExceededMessage(threadCount, runCount, options.threadLimit, options.runLimit, options.toolName);
|
|
231
|
-
const limitAiMessage = new __langchain_core_messages.AIMessage(limitMessage);
|
|
232
352
|
return {
|
|
233
|
-
|
|
234
|
-
|
|
353
|
+
threadToolCallCount: threadCounts,
|
|
354
|
+
runToolCallCount: runCounts,
|
|
355
|
+
messages: artificialMessages
|
|
235
356
|
};
|
|
236
357
|
}
|
|
237
|
-
}
|
|
358
|
+
},
|
|
359
|
+
afterAgent: () => ({ runToolCallCount: {} })
|
|
238
360
|
});
|
|
239
361
|
}
|
|
240
362
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toolCallLimit.cjs","names":["messages: BaseMessage[]","toolName?: string","AIMessage","HumanMessage","threadCount: number","runCount: number","threadLimit: number | undefined","runLimit: number | undefined","toolName: string | undefined","exceededLimits: string[]","z","options: ToolCallLimitConfig","createMiddleware"],"sources":["../../../src/agents/middleware/toolCallLimit.ts"],"sourcesContent":["/**\n * Tool call limit middleware for agents.\n */\n\nimport { AIMessage, BaseMessage, HumanMessage } from \"@langchain/core/messages\";\nimport { z } from \"zod/v3\";\nimport type { InferInteropZodInput } from \"@langchain/core/utils/types\";\n\nimport { createMiddleware } from \"../middleware.js\";\n\n/**\n * Count tool calls in a list of messages.\n *\n * @param messages - List of messages to count tool calls in.\n * @param toolName - If specified, only count calls to this specific tool.\n * If undefined, count all tool calls.\n * @returns The total number of tool calls (optionally filtered by toolName).\n */\nfunction countToolCallsInMessages(\n messages: BaseMessage[],\n toolName?: string\n): number {\n let count = 0;\n for (const message of messages) {\n if (AIMessage.isInstance(message) && message.tool_calls) {\n if (toolName === undefined) {\n // Count all tool calls\n count += message.tool_calls.length;\n } else {\n // Count only calls to the specified tool\n count += message.tool_calls.filter((tc) => tc.name === toolName).length;\n }\n }\n }\n return count;\n}\n\n/**\n * Get messages from the current run (after the last HumanMessage).\n *\n * @param messages - Full list of messages.\n * @returns Messages from the current run (after last HumanMessage).\n */\nfunction getRunMessages(messages: BaseMessage[]): BaseMessage[] {\n /**\n * Find the last HumanMessage\n */\n let lastHumanIndex = -1;\n for (let i = messages.length - 1; i >= 0; i--) {\n if (HumanMessage.isInstance(messages[i])) {\n lastHumanIndex = i;\n break;\n }\n }\n\n /**\n * If no HumanMessage found, return all messages\n */\n if (lastHumanIndex === -1) {\n return messages;\n }\n\n /**\n * Return messages after the last HumanMessage\n */\n return messages.slice(lastHumanIndex + 1);\n}\n\n/**\n * Build a message indicating which tool call limits were reached.\n *\n * @param threadCount - Current thread tool call count.\n * @param runCount - Current run tool call count.\n * @param threadLimit - Thread tool call limit (if set).\n * @param runLimit - Run tool call limit (if set).\n * @param toolName - Tool name being limited (if specific tool), or undefined for all tools.\n * @returns A formatted message describing which limits were reached.\n */\nfunction buildToolLimitExceededMessage(\n threadCount: number,\n runCount: number,\n threadLimit: number | undefined,\n runLimit: number | undefined,\n toolName: string | undefined\n): string {\n const toolDesc = toolName ? `'${toolName}' tool call` : \"Tool call\";\n const exceededLimits: string[] = [];\n\n if (threadLimit !== undefined && threadCount >= threadLimit) {\n exceededLimits.push(`thread limit reached (${threadCount}/${threadLimit})`);\n }\n if (runLimit !== undefined && runCount >= runLimit) {\n exceededLimits.push(`run limit reached (${runCount}/${runLimit})`);\n }\n\n return `${toolDesc} limit${\n exceededLimits.length > 1 ? \"s\" : \"\"\n }: ${exceededLimits.join(\", \")}. Stopping to prevent further tool calls.`;\n}\n\n/**\n * Exception raised when tool call limits are exceeded.\n *\n * This exception is raised when the configured exit behavior is 'error'\n * and either the thread or run tool call limit has been exceeded.\n */\nexport class ToolCallLimitExceededError extends Error {\n /**\n * Current thread tool call count.\n */\n threadCount: number;\n /**\n * Current run tool call count.\n */\n runCount: number;\n /**\n * Thread tool call limit (if set).\n */\n threadLimit: number | undefined;\n /**\n * Run tool call limit (if set).\n */\n runLimit: number | undefined;\n /**\n * Tool name being limited (if specific tool), or undefined for all tools.\n */\n toolName: string | undefined;\n\n constructor(\n threadCount: number,\n runCount: number,\n threadLimit: number | undefined,\n runLimit: number | undefined,\n toolName: string | undefined = undefined\n ) {\n const message = buildToolLimitExceededMessage(\n threadCount,\n runCount,\n threadLimit,\n runLimit,\n toolName\n );\n super(message);\n\n this.name = \"ToolCallLimitExceededError\";\n this.threadCount = threadCount;\n this.runCount = runCount;\n this.threadLimit = threadLimit;\n this.runLimit = runLimit;\n this.toolName = toolName;\n }\n}\n\n/**\n * Options for configuring the Tool Call Limit middleware.\n */\nexport const ToolCallLimitOptionsSchema = z.object({\n /**\n * Name of the specific tool to limit. If undefined, limits apply to all tools.\n */\n toolName: z.string().optional(),\n /**\n * Maximum number of tool calls allowed per thread.\n * undefined means no limit.\n */\n threadLimit: z.number().optional(),\n /**\n * Maximum number of tool calls allowed per run.\n * undefined means no limit.\n */\n runLimit: z.number().optional(),\n /**\n * What to do when limits are exceeded.\n * - \"end\": Jump to the end of the agent execution and inject an artificial\n * AI message indicating that the limit was exceeded.\n * - \"error\": throws a ToolCallLimitExceededError\n */\n exitBehavior: z.enum([\"end\", \"error\"]).default(\"end\"),\n});\n\nexport type ToolCallLimitConfig = InferInteropZodInput<\n typeof ToolCallLimitOptionsSchema\n>;\n\n/**\n * Middleware that tracks tool call counts and enforces limits.\n *\n * This middleware monitors the number of tool calls made during agent execution\n * and can terminate the agent when specified limits are reached. It supports\n * both thread-level and run-level call counting with configurable exit behaviors.\n *\n * Thread-level: The middleware counts all tool calls in the entire message history\n * and persists this count across multiple runs (invocations) of the agent.\n *\n * Run-level: The middleware counts tool calls made after the last HumanMessage,\n * representing the current run (invocation) of the agent.\n *\n * @param options - Configuration options for the middleware\n * @param options.toolName - Name of the specific tool to limit. If undefined, limits apply to all tools.\n * @param options.threadLimit - Maximum number of tool calls allowed per thread. undefined means no limit.\n * @param options.runLimit - Maximum number of tool calls allowed per run. undefined means no limit.\n * @param options.exitBehavior - What to do when limits are exceeded.\n * - \"end\": Jump to the end of the agent execution and inject an artificial AI message indicating that the limit was exceeded.\n * - \"error\": throws a ToolCallLimitExceededError\n *\n * @throws {Error} If both limits are undefined or if exitBehavior is invalid.\n *\n * @example Limit all tool calls globally\n * ```ts\n * import { toolCallLimitMiddleware } from \"@langchain/langchain/agents/middleware\";\n * import { createAgent } from \"@langchain/langchain/agents\";\n *\n * const globalLimiter = toolCallLimitMiddleware({\n * threadLimit: 20,\n * runLimit: 10,\n * exitBehavior: \"end\"\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\",\n * middleware: [globalLimiter]\n * });\n * ```\n *\n * @example Limit a specific tool\n * ```ts\n * import { toolCallLimitMiddleware } from \"@langchain/langchain/agents/middleware\";\n * import { createAgent } from \"@langchain/langchain/agents\";\n *\n * const searchLimiter = toolCallLimitMiddleware({\n * toolName: \"search\",\n * threadLimit: 5,\n * runLimit: 3,\n * exitBehavior: \"end\"\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\",\n * middleware: [searchLimiter]\n * });\n * ```\n *\n * @example Use both in the same agent\n * ```ts\n * import { toolCallLimitMiddleware } from \"@langchain/langchain/agents/middleware\";\n * import { createAgent } from \"@langchain/langchain/agents\";\n *\n * const globalLimiter = toolCallLimitMiddleware({\n * threadLimit: 20,\n * runLimit: 10,\n * exitBehavior: \"end\"\n * });\n *\n * const searchLimiter = toolCallLimitMiddleware({\n * toolName: \"search\",\n * threadLimit: 5,\n * runLimit: 3,\n * exitBehavior: \"end\"\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\",\n * middleware: [globalLimiter, searchLimiter]\n * });\n * ```\n */\nexport function toolCallLimitMiddleware(options: ToolCallLimitConfig) {\n /**\n * Validate that at least one limit is specified\n */\n if (options.threadLimit === undefined && options.runLimit === undefined) {\n throw new Error(\n \"At least one limit must be specified (threadLimit or runLimit)\"\n );\n }\n\n /**\n * Apply default for exitBehavior and validate\n */\n const exitBehavior = options.exitBehavior ?? \"end\";\n if (exitBehavior !== \"end\" && exitBehavior !== \"error\") {\n throw new Error(\n `Invalid exit behavior: ${exitBehavior}. Must be 'end' or 'error'`\n );\n }\n\n /**\n * Generate the middleware name based on the tool name\n */\n const middlewareName = options.toolName\n ? `ToolCallLimitMiddleware[${options.toolName}]`\n : \"ToolCallLimitMiddleware\";\n\n return createMiddleware({\n name: middlewareName,\n beforeModel: {\n canJumpTo: [\"end\"],\n hook: (state) => {\n const messages = state.messages;\n\n /**\n * Count tool calls in entire thread\n */\n const threadCount = countToolCallsInMessages(\n messages,\n options.toolName\n );\n\n /**\n * Count tool calls in current run (after last HumanMessage)\n */\n const runMessages = getRunMessages(messages);\n const runCount = countToolCallsInMessages(\n runMessages,\n options.toolName\n );\n\n /**\n * Check if any limits are exceeded\n */\n const threadLimitExceeded =\n options.threadLimit !== undefined &&\n threadCount >= options.threadLimit;\n const runLimitExceeded =\n options.runLimit !== undefined && runCount >= options.runLimit;\n\n if (!threadLimitExceeded && !runLimitExceeded) {\n return undefined;\n }\n\n if (exitBehavior === \"error\") {\n throw new ToolCallLimitExceededError(\n threadCount,\n runCount,\n options.threadLimit,\n options.runLimit,\n options.toolName\n );\n }\n\n /**\n * Create a message indicating the limit was exceeded\n */\n const limitMessage = buildToolLimitExceededMessage(\n threadCount,\n runCount,\n options.threadLimit,\n options.runLimit,\n options.toolName\n );\n const limitAiMessage = new AIMessage(limitMessage);\n\n return {\n jumpTo: \"end\",\n messages: [limitAiMessage],\n };\n },\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;AAkBA,SAAS,yBACPA,UACAC,UACQ;CACR,IAAI,QAAQ;AACZ,MAAK,MAAM,WAAW,SACpB,KAAIC,oCAAU,WAAW,QAAQ,IAAI,QAAQ,WAC3C,KAAI,aAAa,QAEf,SAAS,QAAQ,WAAW;MAG5B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AAQ;KACR,UAAU,CAAC,cAAe;IAC3B;GACF;EACF;CACF,EAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"toolCallLimit.cjs","names":["toolName: string | undefined","threadCount: number","runCount: number","threadLimit: number | undefined","runLimit: number | undefined","exceededLimits: string[]","z","options: ToolCallLimitConfig","z4","createMiddleware","AIMessage","toolCall: { name?: string }","toolCalls: ToolCall[]","allowed: ToolCall[]","blocked: ToolCall[]","blocked","allowed","artificialMessages: Array<ToolMessage | AIMessage>","ToolMessage","otherTools: ToolCall[]"],"sources":["../../../src/agents/middleware/toolCallLimit.ts"],"sourcesContent":["import { AIMessage, ToolMessage } from \"@langchain/core/messages\";\nimport { z as z4 } from \"zod/v4\";\nimport { z } from \"zod/v3\";\nimport type { InferInteropZodInput } from \"@langchain/core/utils/types\";\nimport type { ToolCall } from \"@langchain/core/messages/tool\";\n\nimport { createMiddleware } from \"../middleware.js\";\n\n/**\n * Build the error message content for ToolMessage when limit is exceeded.\n *\n * This message is sent to the model, so it should not reference thread/run concepts\n * that the model has no notion of.\n *\n * @param toolName - Tool name being limited (if specific tool), or undefined for all tools.\n * @returns A concise message instructing the model not to call the tool again.\n */\nfunction buildToolMessageContent(toolName: string | undefined): string {\n // Always instruct the model not to call again, regardless of which limit was hit\n if (toolName) {\n return `Tool call limit exceeded. Do not call '${toolName}' again.`;\n }\n return \"Tool call limit exceeded. Do not make additional tool calls.\";\n}\n\nconst VALID_EXIT_BEHAVIORS = [\"continue\", \"error\", \"end\"] as const;\nconst DEFAULT_EXIT_BEHAVIOR = \"continue\";\n\n/**\n * Build the final AI message content for 'end' behavior.\n *\n * This message is displayed to the user, so it should include detailed information\n * about which limits were exceeded.\n *\n * @param threadCount - Current thread tool call count.\n * @param runCount - Current run tool call count.\n * @param threadLimit - Thread tool call limit (if set).\n * @param runLimit - Run tool call limit (if set).\n * @param toolName - Tool name being limited (if specific tool), or undefined for all tools.\n * @returns A formatted message describing which limits were exceeded.\n */\nfunction buildFinalAIMessageContent(\n threadCount: number,\n runCount: number,\n threadLimit: number | undefined,\n runLimit: number | undefined,\n toolName: string | undefined\n): string {\n const toolDesc = toolName ? `'${toolName}' tool` : \"Tool\";\n const exceededLimits: string[] = [];\n\n if (threadLimit !== undefined && threadCount > threadLimit) {\n exceededLimits.push(\n `thread limit exceeded (${threadCount}/${threadLimit} calls)`\n );\n }\n if (runLimit !== undefined && runCount > runLimit) {\n exceededLimits.push(`run limit exceeded (${runCount}/${runLimit} calls)`);\n }\n\n const limitsText = exceededLimits.join(\" and \");\n return `${toolDesc} call limit reached: ${limitsText}.`;\n}\n\n/**\n * Schema for the exit behavior.\n */\nconst exitBehaviorSchema = z\n .enum(VALID_EXIT_BEHAVIORS)\n .default(DEFAULT_EXIT_BEHAVIOR);\n\n/**\n * Exception raised when tool call limits are exceeded.\n *\n * This exception is raised when the configured exit behavior is 'error'\n * and either the thread or run tool call limit has been exceeded.\n */\nexport class ToolCallLimitExceededError extends Error {\n /**\n * Current thread tool call count.\n */\n threadCount: number;\n /**\n * Current run tool call count.\n */\n runCount: number;\n /**\n * Thread tool call limit (if set).\n */\n threadLimit: number | undefined;\n /**\n * Run tool call limit (if set).\n */\n runLimit: number | undefined;\n /**\n * Tool name being limited (if specific tool), or undefined for all tools.\n */\n toolName: string | undefined;\n\n constructor(\n threadCount: number,\n runCount: number,\n threadLimit: number | undefined,\n runLimit: number | undefined,\n toolName: string | undefined = undefined\n ) {\n const message = buildFinalAIMessageContent(\n threadCount,\n runCount,\n threadLimit,\n runLimit,\n toolName\n );\n super(message);\n\n this.name = \"ToolCallLimitExceededError\";\n this.threadCount = threadCount;\n this.runCount = runCount;\n this.threadLimit = threadLimit;\n this.runLimit = runLimit;\n this.toolName = toolName;\n }\n}\n\n/**\n * Options for configuring the Tool Call Limit middleware.\n */\nexport const ToolCallLimitOptionsSchema = z.object({\n /**\n * Name of the specific tool to limit. If undefined, limits apply to all tools.\n */\n toolName: z.string().optional(),\n /**\n * Maximum number of tool calls allowed per thread.\n * undefined means no limit.\n */\n threadLimit: z.number().optional(),\n /**\n * Maximum number of tool calls allowed per run.\n * undefined means no limit.\n */\n runLimit: z.number().optional(),\n /**\n * What to do when limits are exceeded.\n * - \"continue\": Block exceeded tools with error messages, let other tools continue (default)\n * - \"error\": Raise a ToolCallLimitExceededError exception\n * - \"end\": Stop execution immediately, injecting a ToolMessage and an AI message\n * for the single tool call that exceeded the limit. Raises NotImplementedError\n * if there are multiple tool calls.\n *\n * @default \"continue\"\n */\n exitBehavior: exitBehaviorSchema,\n});\n\nexport type ToolCallLimitConfig = InferInteropZodInput<\n typeof ToolCallLimitOptionsSchema\n>;\n\n/**\n * Middleware state schema to track the number of model calls made at the thread and run level.\n */\nconst stateSchema = z.object({\n threadToolCallCount: z.record(z.string(), z.number()).default({}),\n runToolCallCount: z.record(z.string(), z.number()).default({}),\n});\n\nconst DEFAULT_TOOL_COUNT_KEY = \"__all__\";\n\n/**\n * Middleware that tracks tool call counts and enforces limits.\n *\n * This middleware monitors the number of tool calls made during agent execution\n * and can terminate the agent when specified limits are reached. It supports\n * both thread-level and run-level call counting with configurable exit behaviors.\n *\n * Thread-level: The middleware counts all tool calls in the entire message history\n * and persists this count across multiple runs (invocations) of the agent.\n *\n * Run-level: The middleware counts tool calls made after the last HumanMessage,\n * representing the current run (invocation) of the agent.\n *\n * @param options - Configuration options for the middleware\n * @param options.toolName - Name of the specific tool to limit. If undefined, limits apply to all tools.\n * @param options.threadLimit - Maximum number of tool calls allowed per thread. undefined means no limit.\n * @param options.runLimit - Maximum number of tool calls allowed per run. undefined means no limit.\n * @param options.exitBehavior - What to do when limits are exceeded.\n * - \"continue\": Block exceeded tools with error messages, let other tools continue. Model decides when to end. (default)\n * - \"error\": Raise a ToolCallLimitExceededError exception\n * - \"end\": Stop execution immediately with a ToolMessage + AI message for the single tool call that exceeded the limit. Raises NotImplementedError if there are multiple tool calls.\n *\n * @throws {Error} If both limits are undefined, if exitBehavior is invalid, or if runLimit exceeds threadLimit.\n * @throws {NotImplementedError} If exitBehavior is \"end\" and there are multiple tool calls.\n *\n * @example Continue execution with blocked tools (default)\n * ```ts\n * import { toolCallLimitMiddleware } from \"@langchain/langchain/agents/middleware\";\n * import { createAgent } from \"@langchain/langchain/agents\";\n *\n * // Block exceeded tools but let other tools and model continue\n * const limiter = toolCallLimitMiddleware({\n * threadLimit: 20,\n * runLimit: 10,\n * exitBehavior: \"continue\", // default\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\",\n * middleware: [limiter]\n * });\n * ```\n *\n * @example Stop immediately when limit exceeded\n * ```ts\n * // End execution immediately with an AI message\n * const limiter = toolCallLimitMiddleware({\n * runLimit: 5,\n * exitBehavior: \"end\"\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\",\n * middleware: [limiter]\n * });\n * ```\n *\n * @example Raise exception on limit\n * ```ts\n * // Strict limit with exception handling\n * const limiter = toolCallLimitMiddleware({\n * toolName: \"search\",\n * threadLimit: 5,\n * exitBehavior: \"error\"\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\",\n * middleware: [limiter]\n * });\n *\n * try {\n * const result = await agent.invoke({ messages: [new HumanMessage(\"Task\")] });\n * } catch (error) {\n * if (error instanceof ToolCallLimitExceededError) {\n * console.log(`Search limit exceeded: ${error}`);\n * }\n * }\n * ```\n */\nexport function toolCallLimitMiddleware(options: ToolCallLimitConfig) {\n /**\n * Validate that at least one limit is specified\n */\n if (options.threadLimit === undefined && options.runLimit === undefined) {\n throw new Error(\n \"At least one limit must be specified (threadLimit or runLimit)\"\n );\n }\n\n /**\n * Validate exitBehavior (Zod schema already validates, but provide helpful error)\n */\n const exitBehavior = options.exitBehavior ?? DEFAULT_EXIT_BEHAVIOR;\n const parseResult = exitBehaviorSchema.safeParse(exitBehavior);\n if (!parseResult.success) {\n throw new Error(z4.prettifyError(parseResult.error).slice(2));\n }\n\n /**\n * Validate that runLimit does not exceed threadLimit\n */\n if (\n options.threadLimit !== undefined &&\n options.runLimit !== undefined &&\n options.runLimit > options.threadLimit\n ) {\n throw new Error(\n `runLimit (${options.runLimit}) cannot exceed threadLimit (${options.threadLimit}). ` +\n \"The run limit should be less than or equal to the thread limit.\"\n );\n }\n\n /**\n * Generate the middleware name based on the tool name\n */\n const middlewareName = options.toolName\n ? `ToolCallLimitMiddleware[${options.toolName}]`\n : \"ToolCallLimitMiddleware\";\n\n return createMiddleware({\n name: middlewareName,\n stateSchema,\n afterModel: {\n canJumpTo: [\"end\"],\n hook: (state) => {\n /**\n * Get the last AI message to check for tool calls\n */\n const lastAIMessage = [...state.messages]\n .reverse()\n .find(AIMessage.isInstance);\n\n if (!lastAIMessage || !lastAIMessage.tool_calls) {\n return undefined;\n }\n\n /**\n * Helper to check if limit would be exceeded by one more call\n */\n const wouldExceedLimit = (\n threadCount: number,\n runCount: number\n ): boolean => {\n return (\n (options.threadLimit !== undefined &&\n threadCount + 1 > options.threadLimit) ||\n (options.runLimit !== undefined && runCount + 1 > options.runLimit)\n );\n };\n\n /**\n * Helper to check if a tool call matches our filter\n */\n const matchesToolFilter = (toolCall: { name?: string }): boolean => {\n return (\n options.toolName === undefined || toolCall.name === options.toolName\n );\n };\n\n /**\n * Separate tool calls into allowed and blocked based on limits\n */\n const separateToolCalls = (\n toolCalls: ToolCall[],\n threadCount: number,\n runCount: number\n ): {\n allowed: ToolCall[];\n blocked: ToolCall[];\n finalThreadCount: number;\n finalRunCount: number;\n } => {\n const allowed: ToolCall[] = [];\n const blocked: ToolCall[] = [];\n let tempThreadCount = threadCount;\n let tempRunCount = runCount;\n\n for (const toolCall of toolCalls) {\n if (!matchesToolFilter(toolCall)) {\n // Tool call doesn't match our filter, skip it\n continue;\n }\n\n if (wouldExceedLimit(tempThreadCount, tempRunCount)) {\n blocked.push(toolCall);\n } else {\n allowed.push(toolCall);\n tempThreadCount += 1;\n tempRunCount += 1;\n }\n }\n\n return {\n allowed,\n blocked,\n finalThreadCount: tempThreadCount,\n finalRunCount: tempRunCount + blocked.length,\n };\n };\n\n /**\n * Get the count key for this middleware instance\n */\n const countKey = options.toolName ?? DEFAULT_TOOL_COUNT_KEY;\n\n /**\n * Get current counts\n */\n const threadCounts = { ...(state.threadToolCallCount ?? {}) };\n const runCounts = { ...(state.runToolCallCount ?? {}) };\n const currentThreadCount = threadCounts[countKey] ?? 0;\n const currentRunCount = runCounts[countKey] ?? 0;\n\n /**\n * Separate tool calls into allowed and blocked\n */\n const { allowed, blocked, finalThreadCount, finalRunCount } =\n separateToolCalls(\n lastAIMessage.tool_calls,\n currentThreadCount,\n currentRunCount\n );\n\n /**\n * Update counts:\n * - Thread count includes only allowed calls (blocked calls don't count towards thread-level tracking)\n * - Run count includes blocked calls since they were attempted in this run\n */\n threadCounts[countKey] = finalThreadCount;\n runCounts[countKey] = finalRunCount;\n\n /**\n * If no tool calls are blocked, just update counts\n */\n if (blocked.length === 0) {\n if (allowed.length > 0) {\n return {\n threadToolCallCount: threadCounts,\n runToolCallCount: runCounts,\n };\n }\n return undefined;\n }\n\n /**\n * Handle different exit behaviors\n */\n if (exitBehavior === \"error\") {\n // Use hypothetical thread count to show which limit was exceeded\n const hypotheticalThreadCount = finalThreadCount + blocked.length;\n throw new ToolCallLimitExceededError(\n hypotheticalThreadCount,\n finalRunCount,\n options.threadLimit,\n options.runLimit,\n options.toolName\n );\n }\n\n /**\n * Build tool message content (sent to model - no thread/run details)\n */\n const toolMsgContent = buildToolMessageContent(options.toolName);\n\n /**\n * Inject artificial error ToolMessages for blocked tool calls\n */\n const artificialMessages: Array<ToolMessage | AIMessage> = blocked.map(\n (toolCall) =>\n new ToolMessage({\n content: toolMsgContent,\n tool_call_id: toolCall.id!,\n name: toolCall.name,\n status: \"error\",\n })\n );\n\n if (exitBehavior === \"end\") {\n /**\n * Check if there are tool calls to other tools that would continue executing\n * For tool-specific limiters: check for calls to other tools\n * For global limiters: check if there are multiple different tool types\n */\n let otherTools: ToolCall[] = [];\n if (options.toolName !== undefined) {\n /**\n * Tool-specific limiter: check for calls to other tools\n */\n otherTools = lastAIMessage.tool_calls.filter(\n (tc) => tc.name !== options.toolName\n );\n } else {\n /**\n * Global limiter: check if there are multiple different tool types\n * If there are allowed calls, those would execute\n * But even if all are blocked, we can't handle multiple tool types with \"end\"\n */\n const uniqueToolNames = new Set(\n lastAIMessage.tool_calls.map((tc) => tc.name).filter(Boolean)\n );\n if (uniqueToolNames.size > 1) {\n /**\n * Multiple different tool types - use allowed calls to show which ones\n */\n otherTools =\n allowed.length > 0 ? allowed : lastAIMessage.tool_calls;\n }\n }\n\n if (otherTools.length > 0) {\n const toolNames = Array.from(\n new Set(otherTools.map((tc) => tc.name).filter(Boolean))\n ).join(\", \");\n throw new Error(\n `Cannot end execution with other tool calls pending. Found calls to: ${toolNames}. Use 'continue' or 'error' behavior instead.`\n );\n }\n\n /**\n * Build final AI message content (displayed to user - includes thread/run details)\n * Use hypothetical thread count (what it would have been if call wasn't blocked)\n * to show which limit was actually exceeded\n */\n const hypotheticalThreadCount = finalThreadCount + blocked.length;\n const finalMsgContent = buildFinalAIMessageContent(\n hypotheticalThreadCount,\n finalRunCount,\n options.threadLimit,\n options.runLimit,\n options.toolName\n );\n artificialMessages.push(new AIMessage(finalMsgContent));\n\n return {\n threadToolCallCount: threadCounts,\n runToolCallCount: runCounts,\n jumpTo: \"end\" as const,\n messages: artificialMessages,\n };\n }\n\n /**\n * For exit_behavior=\"continue\", return error messages to block exceeded tools\n */\n return {\n threadToolCallCount: threadCounts,\n runToolCallCount: runCounts,\n messages: artificialMessages,\n };\n },\n },\n /**\n * reset the run tool call count after the agent execution completes\n */\n afterAgent: () => ({\n runToolCallCount: {},\n }),\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiBA,SAAS,wBAAwBA,UAAsC;AAErE,KAAI,SACF,QAAO,CAAC,uCAAuC,EAAE,SAAS,QAAQ,CAAC;AAErE,QAAO;AACR;AAED,MAAM,uBAAuB;CAAC;CAAY;CAAS;AAAM;AACzD,MAAM,wBAAwB;;;;;;;;;;;;;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,QAAQ,aAAa;;;;KAIvB,aAAa,cAAc,WAAW,OACpC,CAAC,OAAO,GAAG,SAAS,QAAQ,SAC7B;UACI;;;;;;MAML,MAAM,kBAAkB,IAAI,IAC1B,cAAc,WAAW,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,QAAQ;AAE/D,UAAI,gBAAgB,OAAO;;;;MAIzB,aACE,QAAQ,SAAS,IAAI,UAAU,cAAc;KAElD;AAED,SAAI,WAAW,SAAS,GAAG;MACzB,MAAM,YAAY,MAAM,KACtB,IAAI,IAAI,WAAW,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,OAAO,QAAQ,EACxD,CAAC,KAAK,KAAK;AACZ,YAAM,IAAI,MACR,CAAC,oEAAoE,EAAE,UAAU,6CAA6C,CAAC;KAElI;;;;;;KAOD,MAAM,0BAA0B,mBAAmB,QAAQ;KAC3D,MAAM,kBAAkB,2BACtB,yBACA,eACA,QAAQ,aACR,QAAQ,UACR,QAAQ,SACT;KACD,mBAAmB,KAAK,IAAIT,oCAAU,iBAAiB;AAEvD,YAAO;MACL,qBAAqB;MACrB,kBAAkB;MAClB,QAAQ;MACR,UAAU;KACX;IACF;;;;AAKD,WAAO;KACL,qBAAqB;KACrB,kBAAkB;KAClB,UAAU;IACX;GACF;EACF;EAID,YAAY,OAAO,EACjB,kBAAkB,CAAE,EACrB;CACF,EAAC;AACH"}
|
|
@@ -53,21 +53,25 @@ declare const ToolCallLimitOptionsSchema: z.ZodObject<{
|
|
|
53
53
|
runLimit: z.ZodOptional<z.ZodNumber>;
|
|
54
54
|
/**
|
|
55
55
|
* What to do when limits are exceeded.
|
|
56
|
-
* - "
|
|
57
|
-
*
|
|
58
|
-
* - "
|
|
56
|
+
* - "continue": Block exceeded tools with error messages, let other tools continue (default)
|
|
57
|
+
* - "error": Raise a ToolCallLimitExceededError exception
|
|
58
|
+
* - "end": Stop execution immediately, injecting a ToolMessage and an AI message
|
|
59
|
+
* for the single tool call that exceeded the limit. Raises NotImplementedError
|
|
60
|
+
* if there are multiple tool calls.
|
|
61
|
+
*
|
|
62
|
+
* @default "continue"
|
|
59
63
|
*/
|
|
60
|
-
exitBehavior: z.ZodDefault<z.ZodEnum<["
|
|
64
|
+
exitBehavior: z.ZodDefault<z.ZodEnum<["continue", "error", "end"]>>;
|
|
61
65
|
}, "strip", z.ZodTypeAny, {
|
|
62
66
|
toolName?: string | undefined;
|
|
63
67
|
threadLimit?: number | undefined;
|
|
64
68
|
runLimit?: number | undefined;
|
|
65
|
-
exitBehavior: "end" | "error";
|
|
69
|
+
exitBehavior: "continue" | "end" | "error";
|
|
66
70
|
}, {
|
|
67
71
|
toolName?: string | undefined;
|
|
68
72
|
threadLimit?: number | undefined;
|
|
69
73
|
runLimit?: number | undefined;
|
|
70
|
-
exitBehavior?: "end" | "error" | undefined;
|
|
74
|
+
exitBehavior?: "continue" | "end" | "error" | undefined;
|
|
71
75
|
}>;
|
|
72
76
|
type ToolCallLimitConfig = InferInteropZodInput<typeof ToolCallLimitOptionsSchema>;
|
|
73
77
|
/**
|
|
@@ -88,71 +92,78 @@ type ToolCallLimitConfig = InferInteropZodInput<typeof ToolCallLimitOptionsSchem
|
|
|
88
92
|
* @param options.threadLimit - Maximum number of tool calls allowed per thread. undefined means no limit.
|
|
89
93
|
* @param options.runLimit - Maximum number of tool calls allowed per run. undefined means no limit.
|
|
90
94
|
* @param options.exitBehavior - What to do when limits are exceeded.
|
|
91
|
-
* - "
|
|
92
|
-
* - "error":
|
|
95
|
+
* - "continue": Block exceeded tools with error messages, let other tools continue. Model decides when to end. (default)
|
|
96
|
+
* - "error": Raise a ToolCallLimitExceededError exception
|
|
97
|
+
* - "end": Stop execution immediately with a ToolMessage + AI message for the single tool call that exceeded the limit. Raises NotImplementedError if there are multiple tool calls.
|
|
93
98
|
*
|
|
94
|
-
* @throws {Error} If both limits are undefined
|
|
99
|
+
* @throws {Error} If both limits are undefined, if exitBehavior is invalid, or if runLimit exceeds threadLimit.
|
|
100
|
+
* @throws {NotImplementedError} If exitBehavior is "end" and there are multiple tool calls.
|
|
95
101
|
*
|
|
96
|
-
* @example
|
|
102
|
+
* @example Continue execution with blocked tools (default)
|
|
97
103
|
* ```ts
|
|
98
104
|
* import { toolCallLimitMiddleware } from "@langchain/langchain/agents/middleware";
|
|
99
105
|
* import { createAgent } from "@langchain/langchain/agents";
|
|
100
106
|
*
|
|
101
|
-
*
|
|
107
|
+
* // Block exceeded tools but let other tools and model continue
|
|
108
|
+
* const limiter = toolCallLimitMiddleware({
|
|
102
109
|
* threadLimit: 20,
|
|
103
110
|
* runLimit: 10,
|
|
104
|
-
* exitBehavior: "
|
|
111
|
+
* exitBehavior: "continue", // default
|
|
105
112
|
* });
|
|
106
113
|
*
|
|
107
114
|
* const agent = createAgent({
|
|
108
115
|
* model: "openai:gpt-4o",
|
|
109
|
-
* middleware: [
|
|
116
|
+
* middleware: [limiter]
|
|
110
117
|
* });
|
|
111
118
|
* ```
|
|
112
119
|
*
|
|
113
|
-
* @example
|
|
120
|
+
* @example Stop immediately when limit exceeded
|
|
114
121
|
* ```ts
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* const searchLimiter = toolCallLimitMiddleware({
|
|
119
|
-
* toolName: "search",
|
|
120
|
-
* threadLimit: 5,
|
|
121
|
-
* runLimit: 3,
|
|
122
|
+
* // End execution immediately with an AI message
|
|
123
|
+
* const limiter = toolCallLimitMiddleware({
|
|
124
|
+
* runLimit: 5,
|
|
122
125
|
* exitBehavior: "end"
|
|
123
126
|
* });
|
|
124
127
|
*
|
|
125
128
|
* const agent = createAgent({
|
|
126
129
|
* model: "openai:gpt-4o",
|
|
127
|
-
* middleware: [
|
|
130
|
+
* middleware: [limiter]
|
|
128
131
|
* });
|
|
129
132
|
* ```
|
|
130
133
|
*
|
|
131
|
-
* @example
|
|
134
|
+
* @example Raise exception on limit
|
|
132
135
|
* ```ts
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
* const globalLimiter = toolCallLimitMiddleware({
|
|
137
|
-
* threadLimit: 20,
|
|
138
|
-
* runLimit: 10,
|
|
139
|
-
* exitBehavior: "end"
|
|
140
|
-
* });
|
|
141
|
-
*
|
|
142
|
-
* const searchLimiter = toolCallLimitMiddleware({
|
|
136
|
+
* // Strict limit with exception handling
|
|
137
|
+
* const limiter = toolCallLimitMiddleware({
|
|
143
138
|
* toolName: "search",
|
|
144
139
|
* threadLimit: 5,
|
|
145
|
-
*
|
|
146
|
-
* exitBehavior: "end"
|
|
140
|
+
* exitBehavior: "error"
|
|
147
141
|
* });
|
|
148
142
|
*
|
|
149
143
|
* const agent = createAgent({
|
|
150
144
|
* model: "openai:gpt-4o",
|
|
151
|
-
* middleware: [
|
|
145
|
+
* middleware: [limiter]
|
|
152
146
|
* });
|
|
147
|
+
*
|
|
148
|
+
* try {
|
|
149
|
+
* const result = await agent.invoke({ messages: [new HumanMessage("Task")] });
|
|
150
|
+
* } catch (error) {
|
|
151
|
+
* if (error instanceof ToolCallLimitExceededError) {
|
|
152
|
+
* console.log(`Search limit exceeded: ${error}`);
|
|
153
|
+
* }
|
|
154
|
+
* }
|
|
153
155
|
* ```
|
|
154
156
|
*/
|
|
155
|
-
declare function toolCallLimitMiddleware(options: ToolCallLimitConfig): AgentMiddleware<
|
|
157
|
+
declare function toolCallLimitMiddleware(options: ToolCallLimitConfig): AgentMiddleware<z.ZodObject<{
|
|
158
|
+
threadToolCallCount: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodNumber>>;
|
|
159
|
+
runToolCallCount: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodNumber>>;
|
|
160
|
+
}, "strip", z.ZodTypeAny, {
|
|
161
|
+
threadToolCallCount: Record<string, number>;
|
|
162
|
+
runToolCallCount: Record<string, number>;
|
|
163
|
+
}, {
|
|
164
|
+
threadToolCallCount?: Record<string, number> | undefined;
|
|
165
|
+
runToolCallCount?: Record<string, number> | undefined;
|
|
166
|
+
}>, undefined, any>;
|
|
156
167
|
//#endregion
|
|
157
168
|
export { ToolCallLimitConfig, ToolCallLimitExceededError, toolCallLimitMiddleware };
|
|
158
169
|
//# sourceMappingURL=toolCallLimit.d.cts.map
|