langchain 1.5.17 → 1.5.19
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 +14 -0
- package/dist/agents/ReactAgent.cjs +8 -14
- package/dist/agents/ReactAgent.cjs.map +1 -1
- package/dist/agents/ReactAgent.d.cts.map +1 -1
- package/dist/agents/ReactAgent.d.ts.map +1 -1
- package/dist/agents/ReactAgent.js +8 -14
- package/dist/agents/ReactAgent.js.map +1 -1
- package/dist/agents/middleware/hitl.cjs +184 -14
- package/dist/agents/middleware/hitl.cjs.map +1 -1
- package/dist/agents/middleware/hitl.d.cts +113 -9
- package/dist/agents/middleware/hitl.d.cts.map +1 -1
- package/dist/agents/middleware/hitl.d.ts +145 -41
- package/dist/agents/middleware/hitl.d.ts.map +1 -1
- package/dist/agents/middleware/hitl.js +196 -27
- package/dist/agents/middleware/hitl.js.map +1 -1
- package/dist/agents/middleware/index.cjs +1 -0
- package/dist/agents/middleware/index.d.cts +2 -2
- package/dist/agents/middleware/index.d.ts +2 -2
- package/dist/agents/middleware/index.js +2 -2
- package/dist/agents/middleware/todoListMiddleware.cjs +41 -183
- package/dist/agents/middleware/todoListMiddleware.cjs.map +1 -1
- package/dist/agents/middleware/todoListMiddleware.d.cts +1 -1
- package/dist/agents/middleware/todoListMiddleware.d.cts.map +1 -1
- package/dist/agents/middleware/todoListMiddleware.d.ts +1 -1
- package/dist/agents/middleware/todoListMiddleware.d.ts.map +1 -1
- package/dist/agents/middleware/todoListMiddleware.js +41 -183
- package/dist/agents/middleware/todoListMiddleware.js.map +1 -1
- package/dist/agents/nodes/ToolNode.cjs +14 -2
- package/dist/agents/nodes/ToolNode.cjs.map +1 -1
- package/dist/agents/nodes/ToolNode.js +14 -2
- package/dist/agents/nodes/ToolNode.js.map +1 -1
- package/dist/agents/tests/utils.cjs.map +1 -1
- package/dist/agents/tests/utils.d.cts +1 -1
- package/dist/agents/tests/utils.d.ts +1 -1
- package/dist/agents/tests/utils.js.map +1 -1
- package/dist/agents/types.d.cts +6 -0
- package/dist/agents/types.d.cts.map +1 -1
- package/dist/agents/types.d.ts +6 -0
- package/dist/agents/types.d.ts.map +1 -1
- package/dist/browser.cjs +2 -0
- package/dist/browser.d.cts +2 -2
- package/dist/browser.d.ts +2 -2
- package/dist/browser.js +3 -2
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -2
- package/package.json +6 -6
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
import { createMiddleware } from "../middleware.js";
|
|
2
2
|
import { AIMessage, ToolMessage } from "@langchain/core/messages";
|
|
3
|
-
import { interrupt } from "@langchain/langgraph";
|
|
4
|
-
import { interopParse } from "@langchain/core/utils/types";
|
|
5
|
-
import { z } from "zod/
|
|
3
|
+
import { Command, interrupt, isCommand } from "@langchain/langgraph";
|
|
4
|
+
import { interopParse, interopZodTransformInputSchema, isZodSchemaV4 } from "@langchain/core/utils/types";
|
|
5
|
+
import { z } from "zod/v4";
|
|
6
|
+
import { z as z$1 } from "zod/v3";
|
|
6
7
|
//#region src/agents/middleware/hitl.ts
|
|
7
|
-
const WhenFunctionSchema = z.function().args(z.custom()).returns(z.union([z.boolean(), z.promise(z.boolean())]));
|
|
8
|
-
const DescriptionFunctionSchema = z.function().args(z.custom(), z.custom(), z.custom()).returns(z.union([z.string(), z.promise(z.string())]));
|
|
9
|
-
|
|
8
|
+
const WhenFunctionSchema = z$1.function().args(z$1.custom()).returns(z$1.union([z$1.boolean(), z$1.promise(z$1.boolean())]));
|
|
9
|
+
const DescriptionFunctionSchema = z$1.function().args(z$1.custom(), z$1.custom(), z$1.custom()).returns(z$1.union([z$1.string(), z$1.promise(z$1.string())]));
|
|
10
|
+
/**
|
|
11
|
+
* The type of decision a human can make.
|
|
12
|
+
*/
|
|
13
|
+
const ALLOWED_DECISIONS = [
|
|
10
14
|
"approve",
|
|
11
15
|
"edit",
|
|
12
16
|
"reject",
|
|
13
17
|
"respond"
|
|
14
|
-
]);
|
|
15
|
-
const DEFAULT_ALLOWED_DECISIONS = [
|
|
16
|
-
"approve",
|
|
17
|
-
"edit",
|
|
18
|
-
"reject"
|
|
19
18
|
];
|
|
20
|
-
const
|
|
19
|
+
const DecisionType = z$1.enum(ALLOWED_DECISIONS);
|
|
20
|
+
const InterruptOnConfigSchema = z$1.object({
|
|
21
21
|
/**
|
|
22
22
|
* The decisions that are allowed for this action.
|
|
23
23
|
*/
|
|
24
|
-
allowedDecisions: z.array(DecisionType),
|
|
24
|
+
allowedDecisions: z$1.array(DecisionType),
|
|
25
25
|
/**
|
|
26
26
|
* The description attached to the request for human input.
|
|
27
27
|
* Can be either:
|
|
@@ -65,11 +65,14 @@ const InterruptOnConfigSchema = z.object({
|
|
|
65
65
|
* };
|
|
66
66
|
* ```
|
|
67
67
|
*/
|
|
68
|
-
description: z.union([z.string(), DescriptionFunctionSchema]).optional(),
|
|
68
|
+
description: z$1.union([z$1.string(), DescriptionFunctionSchema]).optional(),
|
|
69
69
|
/**
|
|
70
70
|
* JSON schema for the arguments associated with the action, if edits are allowed.
|
|
71
|
+
*
|
|
72
|
+
* Not used in `"per_call"` mode: an edit's `args` are checked against the tool's
|
|
73
|
+
* own schema there.
|
|
71
74
|
*/
|
|
72
|
-
argsSchema: z.record(z.any()).optional(),
|
|
75
|
+
argsSchema: z$1.record(z$1.any()).optional(),
|
|
73
76
|
/**
|
|
74
77
|
* Optional predicate controlling whether to interrupt for a given tool call.
|
|
75
78
|
*
|
|
@@ -77,7 +80,9 @@ const InterruptOnConfigSchema = z.object({
|
|
|
77
80
|
* `false` to auto-approve the tool call.
|
|
78
81
|
*
|
|
79
82
|
* The request is constructed with `tool` set to `undefined` and `runtime` set
|
|
80
|
-
* to the node-level {@link Runtime}, so `request.tool` is not available.
|
|
83
|
+
* to the node-level {@link Runtime}, so `request.tool` is not available. In
|
|
84
|
+
* `"per_call"` mode the predicate runs in `wrapToolCall` and receives the real
|
|
85
|
+
* request, with `tool` set.
|
|
81
86
|
*
|
|
82
87
|
* @example
|
|
83
88
|
* ```typescript
|
|
@@ -93,16 +98,81 @@ const InterruptOnConfigSchema = z.object({
|
|
|
93
98
|
*/
|
|
94
99
|
when: WhenFunctionSchema.optional()
|
|
95
100
|
});
|
|
96
|
-
const
|
|
101
|
+
const ToolApprovalRequestSchema = z.object({
|
|
102
|
+
/** Always `"tool_approval"`; tells clients how to read this interrupt. */
|
|
103
|
+
type: z.literal("tool_approval"),
|
|
104
|
+
/** ID of the model's tool call this approval is about. */
|
|
105
|
+
tool_call_id: z.string(),
|
|
106
|
+
/** Tool name, as the model requested it. */
|
|
107
|
+
name: z.string(),
|
|
108
|
+
/** Tool arguments, as the model requested them. */
|
|
109
|
+
args: z.record(z.string(), z.unknown()),
|
|
110
|
+
/** Text shown to the reviewer. */
|
|
111
|
+
description: z.string()
|
|
112
|
+
});
|
|
113
|
+
/**
|
|
114
|
+
* What an edit's `args` must look like: the input side of the tool's schema, if it's
|
|
115
|
+
* a Zod v4 object. Transforms are left out, so they run once, when the tool does.
|
|
116
|
+
* Args the tool doesn't declare are handled as the tool handles them from the model.
|
|
117
|
+
* Any other tool takes any object, and checks it when it runs.
|
|
118
|
+
*/
|
|
119
|
+
function editArgs(tool) {
|
|
120
|
+
const schema = tool && "schema" in tool ? tool.schema : void 0;
|
|
121
|
+
const input = isZodSchemaV4(schema) ? interopZodTransformInputSchema(schema, true) : void 0;
|
|
122
|
+
return input instanceof z.ZodObject ? input : z.record(z.string(), z.unknown());
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The answers a reviewer can give to a `"per_call"` approval of tool `name`, by type.
|
|
126
|
+
*
|
|
127
|
+
* The edit can't switch tools: its `name` is pinned.
|
|
128
|
+
*/
|
|
129
|
+
const toolApprovalDecisions = (name, tool) => ({
|
|
130
|
+
approve: z.object({ type: z.literal("approve") }),
|
|
131
|
+
edit: z.object({
|
|
132
|
+
type: z.literal("edit"),
|
|
133
|
+
edited_action: z.object({
|
|
134
|
+
name: z.literal(name),
|
|
135
|
+
args: editArgs(tool)
|
|
136
|
+
})
|
|
137
|
+
}),
|
|
138
|
+
reject: z.object({
|
|
139
|
+
type: z.literal("reject"),
|
|
140
|
+
message: z.string().optional()
|
|
141
|
+
}),
|
|
142
|
+
respond: z.object({
|
|
143
|
+
type: z.literal("respond"),
|
|
144
|
+
message: z.string()
|
|
145
|
+
})
|
|
146
|
+
});
|
|
147
|
+
/**
|
|
148
|
+
* Whether an interrupt is a `"per_call"` tool approval, e.g.
|
|
149
|
+
* `result.__interrupt__?.filter(isToolApprovalInterrupt)`.
|
|
150
|
+
*/
|
|
151
|
+
function isToolApprovalInterrupt(interrupt) {
|
|
152
|
+
return ToolApprovalRequestSchema.safeParse(interrupt?.value).success;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The per-call `responseSchema`: the `allowed` decisions for tool `name`.
|
|
156
|
+
*
|
|
157
|
+
* An answer is checked only against the branch its `type` names, so a bad one gets a
|
|
158
|
+
* single error. A one-decision tool gets that decision's plain object schema.
|
|
159
|
+
*/
|
|
160
|
+
function decisionSchema(allowed, name, tool) {
|
|
161
|
+
const byType = toolApprovalDecisions(name, tool);
|
|
162
|
+
const [first, ...rest] = [...new Set(allowed)].map((d) => byType[d]);
|
|
163
|
+
if (first === void 0) throw new Error("allowedDecisions must list at least one decision.");
|
|
164
|
+
return rest.length ? z.discriminatedUnion("type", [first, ...rest]) : first;
|
|
165
|
+
}
|
|
166
|
+
const contextSchema = z$1.object({
|
|
97
167
|
/**
|
|
98
168
|
* Mapping of tool name to allowed reviewer responses.
|
|
99
169
|
* If a tool doesn't have an entry, it's auto-approved by default.
|
|
100
170
|
*
|
|
101
|
-
* - `true` -> pause for approval and allow approve/edit/reject
|
|
171
|
+
* - `true` -> pause for approval and allow every decision (approve/edit/reject/respond)
|
|
102
172
|
* - `false` -> auto-approve (no human review)
|
|
103
173
|
* - `InterruptOnConfig` -> explicitly specify which decisions are allowed for this tool
|
|
104
174
|
*/
|
|
105
|
-
interruptOn: z.record(z.union([z.boolean(), InterruptOnConfigSchema])).optional(),
|
|
175
|
+
interruptOn: z$1.record(z$1.union([z$1.boolean(), InterruptOnConfigSchema])).optional(),
|
|
106
176
|
/**
|
|
107
177
|
* Prefix used when constructing human-facing approval messages.
|
|
108
178
|
* Provides context about the tool call being reviewed; does not change the underlying action.
|
|
@@ -111,8 +181,58 @@ const contextSchema = z.object({
|
|
|
111
181
|
* `description` via their {@link InterruptOnConfig}. If a tool specifies a custom
|
|
112
182
|
* `description`, that per-tool text is used and this prefix is ignored.
|
|
113
183
|
*/
|
|
114
|
-
descriptionPrefix: z.string().default("Tool execution requires approval")
|
|
184
|
+
descriptionPrefix: z$1.string().default("Tool execution requires approval")
|
|
115
185
|
});
|
|
186
|
+
const DEFAULT_EDIT_NOTICE = "Note: a human reviewer replaced this tool call before it ran. The call recorded in your message is the one you produced, not the one that executed. This was intentional and authorized. Do not re-issue your original call.";
|
|
187
|
+
/**
|
|
188
|
+
* Resolve `interruptOn`: `true` allows every decision; `false` and missing entries
|
|
189
|
+
* auto-approve.
|
|
190
|
+
*/
|
|
191
|
+
function resolveInterruptOn(interruptOn) {
|
|
192
|
+
const resolved = {};
|
|
193
|
+
for (const [toolName, toolConfig] of Object.entries(interruptOn ?? {})) if (typeof toolConfig === "boolean") {
|
|
194
|
+
if (toolConfig === true) resolved[toolName] = { allowedDecisions: [...ALLOWED_DECISIONS] };
|
|
195
|
+
} else if (toolConfig.allowedDecisions) resolved[toolName] = toolConfig;
|
|
196
|
+
return resolved;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Return `message` with the reviewer-edit notice prepended, stating the call that ran.
|
|
200
|
+
*/
|
|
201
|
+
function prependNotice(message, notice, executed) {
|
|
202
|
+
const hasContent = message.content.length > 0;
|
|
203
|
+
const text = `${notice} Executed instead: ${executed.name} with arguments ${JSON.stringify(executed.args)}.${hasContent ? "\n\nTool response:" : ""}`;
|
|
204
|
+
return new ToolMessage({
|
|
205
|
+
content: typeof message.content === "string" ? `${text}${hasContent ? "\n" : ""}${message.content}` : [{
|
|
206
|
+
type: "text",
|
|
207
|
+
text
|
|
208
|
+
}, ...message.content],
|
|
209
|
+
tool_call_id: message.tool_call_id,
|
|
210
|
+
name: message.name,
|
|
211
|
+
status: message.status,
|
|
212
|
+
artifact: message.artifact,
|
|
213
|
+
id: message.id,
|
|
214
|
+
additional_kwargs: message.additional_kwargs,
|
|
215
|
+
response_metadata: message.response_metadata
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Tell the model a reviewer replaced the call, and with what.
|
|
220
|
+
*/
|
|
221
|
+
function withEditNotice(result, toolCallId, executed, notice) {
|
|
222
|
+
if (!notice) return result;
|
|
223
|
+
if (ToolMessage.isInstance(result)) return prependNotice(result, notice, executed);
|
|
224
|
+
const update = isCommand(result) ? result.update : void 0;
|
|
225
|
+
if (typeof update !== "object" || update === null || !("messages" in update) || !Array.isArray(update.messages)) return result;
|
|
226
|
+
return new Command({
|
|
227
|
+
update: {
|
|
228
|
+
...update,
|
|
229
|
+
messages: update.messages.map((message) => ToolMessage.isInstance(message) && message.tool_call_id === toolCallId ? prependNotice(message, notice, executed) : message)
|
|
230
|
+
},
|
|
231
|
+
resume: result.resume,
|
|
232
|
+
goto: result.goto,
|
|
233
|
+
graph: result.graph
|
|
234
|
+
});
|
|
235
|
+
}
|
|
116
236
|
/**
|
|
117
237
|
* Creates a Human-in-the-Loop (HITL) middleware for tool approval and oversight.
|
|
118
238
|
*
|
|
@@ -315,8 +435,9 @@ const contextSchema = z.object({
|
|
|
315
435
|
* @remarks
|
|
316
436
|
* - Tool calls are processed in the order they appear in the AI message
|
|
317
437
|
* - Auto-approved tools execute immediately without interruption
|
|
318
|
-
* -
|
|
319
|
-
*
|
|
438
|
+
* - In the default `"batched"` mode, multiple tools requiring approval are bundled into a
|
|
439
|
+
* single interrupt request, raised in the `afterModel` phase before any tool runs. In
|
|
440
|
+
* `"per_call"` mode, each gated call raises its own interrupt from `wrapToolCall`
|
|
320
441
|
* - Requires a checkpointer to maintain state across interruptions
|
|
321
442
|
*
|
|
322
443
|
* @see {@link createAgent} for agent creation
|
|
@@ -422,12 +543,63 @@ function humanInTheLoopMiddleware(options) {
|
|
|
422
543
|
const msg = `Unexpected human decision: ${JSON.stringify(decision)}. Decision type '${decision.type}' is not allowed for tool '${toolCall.name}'. Expected one of ${JSON.stringify(allowedDecisions)} based on the tool's configuration.`;
|
|
423
544
|
throw new Error(msg);
|
|
424
545
|
};
|
|
546
|
+
const interruptMode = options.interruptMode ?? "batched";
|
|
547
|
+
if (interruptMode !== "batched" && interruptMode !== "per_call") throw new Error(`interruptMode must be "batched" or "per_call", got ${JSON.stringify(interruptMode)}.`);
|
|
548
|
+
if (interruptMode === "per_call") {
|
|
549
|
+
for (const [toolName, toolConfig] of Object.entries(options.interruptOn ?? {})) if (typeof toolConfig === "object" && !toolConfig.allowedDecisions?.length) throw new Error(`Invalid interruptOn config for tool "${toolName}": allowedDecisions must list at least one decision.`);
|
|
550
|
+
}
|
|
551
|
+
const editNotice = options.editNotice === void 0 ? DEFAULT_EDIT_NOTICE : options.editNotice;
|
|
425
552
|
return createMiddleware({
|
|
426
553
|
name: "HumanInTheLoopMiddleware",
|
|
427
554
|
contextSchema,
|
|
555
|
+
/**
|
|
556
|
+
* Per-call mode: pause for each gated tool call as it's about to run.
|
|
557
|
+
*/
|
|
558
|
+
wrapToolCall: interruptMode === "per_call" ? async (request, handler) => {
|
|
559
|
+
const { toolCall } = request;
|
|
560
|
+
const { interruptOn } = interopParse(contextSchema, {
|
|
561
|
+
...options,
|
|
562
|
+
...request.runtime.context || {}
|
|
563
|
+
});
|
|
564
|
+
const toolConfig = resolveInterruptOn(interruptOn)[toolCall.name];
|
|
565
|
+
if (!toolConfig || toolConfig.when && !await toolConfig.when(request)) return handler(request);
|
|
566
|
+
if (toolCall.id == null) throw new Error(`Tool call \`${toolCall.name}\` has no ID, so its result can't be matched to it. Make sure the chat model returns tool call IDs.`);
|
|
567
|
+
const { actionRequest } = await createActionAndConfig(toolCall, toolConfig, request.state, request.runtime);
|
|
568
|
+
const value = {
|
|
569
|
+
type: "tool_approval",
|
|
570
|
+
tool_call_id: toolCall.id,
|
|
571
|
+
name: toolCall.name,
|
|
572
|
+
args: toolCall.args,
|
|
573
|
+
description: actionRequest.description ?? ""
|
|
574
|
+
};
|
|
575
|
+
const responseSchema = decisionSchema(toolConfig.allowedDecisions, toolCall.name, request.tool);
|
|
576
|
+
const decision = interrupt(value, { responseSchema });
|
|
577
|
+
if (decision.type === "approve") return handler(request);
|
|
578
|
+
if (decision.type === "reject") return new ToolMessage({
|
|
579
|
+
content: decision.message ?? `User rejected the tool call for \`${toolCall.name}\` with id ${toolCall.id}`,
|
|
580
|
+
name: toolCall.name,
|
|
581
|
+
tool_call_id: toolCall.id,
|
|
582
|
+
status: "error"
|
|
583
|
+
});
|
|
584
|
+
if (decision.type === "respond") return new ToolMessage({
|
|
585
|
+
content: decision.message,
|
|
586
|
+
name: toolCall.name,
|
|
587
|
+
tool_call_id: toolCall.id,
|
|
588
|
+
status: "success"
|
|
589
|
+
});
|
|
590
|
+
const executed = decision.edited_action;
|
|
591
|
+
return withEditNotice(await handler({
|
|
592
|
+
...request,
|
|
593
|
+
toolCall: {
|
|
594
|
+
...toolCall,
|
|
595
|
+
args: executed.args
|
|
596
|
+
}
|
|
597
|
+
}), toolCall.id, executed, editNotice);
|
|
598
|
+
} : void 0,
|
|
428
599
|
afterModel: {
|
|
429
600
|
canJumpTo: ["model"],
|
|
430
601
|
hook: async (state, runtime) => {
|
|
602
|
+
if (interruptMode === "per_call") return;
|
|
431
603
|
const config = interopParse(contextSchema, {
|
|
432
604
|
...options,
|
|
433
605
|
...runtime.context || {}
|
|
@@ -447,10 +619,7 @@ function humanInTheLoopMiddleware(options) {
|
|
|
447
619
|
/**
|
|
448
620
|
* Resolve per-tool configs (boolean true -> all decisions allowed; false -> auto-approve)
|
|
449
621
|
*/
|
|
450
|
-
const resolvedConfigs =
|
|
451
|
-
for (const [toolName, toolConfig] of Object.entries(config.interruptOn)) if (typeof toolConfig === "boolean") {
|
|
452
|
-
if (toolConfig === true) resolvedConfigs[toolName] = { allowedDecisions: [...DEFAULT_ALLOWED_DECISIONS] };
|
|
453
|
-
} else if (toolConfig.allowedDecisions) resolvedConfigs[toolName] = toolConfig;
|
|
622
|
+
const resolvedConfigs = resolveInterruptOn(config.interruptOn);
|
|
454
623
|
const interruptToolCalls = [];
|
|
455
624
|
const autoApprovedToolCalls = [];
|
|
456
625
|
for (const toolCall of lastMessage.tool_calls) {
|
|
@@ -523,6 +692,6 @@ function humanInTheLoopMiddleware(options) {
|
|
|
523
692
|
});
|
|
524
693
|
}
|
|
525
694
|
//#endregion
|
|
526
|
-
export { humanInTheLoopMiddleware };
|
|
695
|
+
export { humanInTheLoopMiddleware, isToolApprovalInterrupt };
|
|
527
696
|
|
|
528
697
|
//# sourceMappingURL=hitl.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hitl.js","names":[],"sources":["../../../src/agents/middleware/hitl.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v3\";\nimport { AIMessage, ToolMessage, ToolCall } from \"@langchain/core/messages\";\nimport {\n InferInteropZodInput,\n interopParse,\n} from \"@langchain/core/utils/types\";\nimport { interrupt } from \"@langchain/langgraph\";\n\nimport { createMiddleware } from \"../middleware.js\";\nimport type { AgentBuiltInState, Runtime } from \"../runtime.js\";\nimport type { JumpToTarget } from \"../constants.js\";\nimport type { ToolCallRequest } from \"./types.js\";\n\nconst WhenFunctionSchema = z\n .function()\n .args(z.custom<ToolCallRequest<AgentBuiltInState>>()) // request\n .returns(z.union([z.boolean(), z.promise(z.boolean())]));\n\n/**\n * Predicate controlling whether a given tool call triggers an interrupt.\n *\n * Receives a {@link ToolCallRequest} and returns `true` to interrupt or `false`\n * to auto-approve the tool call.\n *\n * The request is constructed with `tool` set to `undefined` and `runtime` set to\n * the node-level {@link Runtime}, so it reflects the batch (`afterModel`) context\n * rather than a per-call tool execution.\n *\n * @param request - The tool call request being evaluated\n * @returns `true` to interrupt for the tool call, `false` to auto-approve it.\n * May also return a promise resolving to a boolean.\n *\n * @example\n * ```typescript\n * import { type WhenPredicate } from \"langchain\";\n *\n * // Only interrupt delete_file calls targeting /etc\n * const when: WhenPredicate = (request) =>\n * String(request.toolCall.args.path ?? \"\").startsWith(\"/etc\");\n * ```\n */\nexport type WhenPredicate = z.infer<typeof WhenFunctionSchema>;\n\nconst DescriptionFunctionSchema = z\n .function()\n .args(\n z.custom<ToolCall>(), // toolCall\n z.custom<AgentBuiltInState>(), // state\n z.custom<Runtime<unknown>>() // runtime\n )\n .returns(z.union([z.string(), z.promise(z.string())]));\n\n/**\n * Function type that dynamically generates a description for a tool call approval request.\n *\n * @param toolCall - The tool call being reviewed\n * @param state - The current agent state\n * @param runtime - The agent runtime context\n * @returns A string description or Promise that resolves to a string description\n *\n * @example\n * ```typescript\n * import { type DescriptionFactory, type ToolCall } from \"langchain\";\n *\n * const descriptionFactory: DescriptionFactory = (toolCall, state, runtime) => {\n * return `Please review: ${toolCall.name}(${JSON.stringify(toolCall.args)})`;\n * };\n * ```\n */\nexport type DescriptionFactory = z.infer<typeof DescriptionFunctionSchema>;\n\n/**\n * The type of decision a human can make.\n */\nconst ALLOWED_DECISIONS = [\"approve\", \"edit\", \"reject\", \"respond\"] as const;\nconst DecisionType = z.enum(ALLOWED_DECISIONS);\nexport type DecisionType = z.infer<typeof DecisionType>;\nconst DEFAULT_ALLOWED_DECISIONS: DecisionType[] = [\"approve\", \"edit\", \"reject\"];\n\nconst InterruptOnConfigSchema = z.object({\n /**\n * The decisions that are allowed for this action.\n */\n allowedDecisions: z.array(DecisionType),\n /**\n * The description attached to the request for human input.\n * Can be either:\n * - A static string describing the approval request\n * - A callable that dynamically generates the description based on agent state,\n * runtime, and tool call information\n *\n * @example\n * Static string description\n * ```typescript\n * import type { InterruptOnConfig } from \"langchain\";\n *\n * const config: InterruptOnConfig = {\n * allowedDecisions: [\"approve\", \"reject\"],\n * description: \"Please review this tool execution\"\n * };\n * ```\n *\n * @example\n * Dynamic callable description\n * ```typescript\n * import type {\n * AgentBuiltInState,\n * Runtime,\n * DescriptionFactory,\n * ToolCall,\n * InterruptOnConfig\n * } from \"langchain\";\n *\n * const formatToolDescription: DescriptionFactory = (\n * toolCall: ToolCall,\n * state: AgentBuiltInState,\n * runtime: Runtime<unknown>\n * ) => {\n * return `Tool: ${toolCall.name}\\nArguments:\\n${JSON.stringify(toolCall.args, null, 2)}`;\n * };\n *\n * const config: InterruptOnConfig = {\n * allowedDecisions: [\"approve\", \"edit\"],\n * description: formatToolDescription\n * };\n * ```\n */\n description: z.union([z.string(), DescriptionFunctionSchema]).optional(),\n /**\n * JSON schema for the arguments associated with the action, if edits are allowed.\n */\n argsSchema: z.record(z.any()).optional(),\n /**\n * Optional predicate controlling whether to interrupt for a given tool call.\n *\n * Receives a {@link ToolCallRequest} and returns `true` to interrupt or\n * `false` to auto-approve the tool call.\n *\n * The request is constructed with `tool` set to `undefined` and `runtime` set\n * to the node-level {@link Runtime}, so `request.tool` is not available.\n *\n * @example\n * ```typescript\n * import type { InterruptOnConfig } from \"langchain\";\n *\n * // Only interrupt delete_file calls targeting /etc\n * const config: InterruptOnConfig = {\n * allowedDecisions: [\"approve\", \"reject\"],\n * when: (request) =>\n * String(request.toolCall.args.path ?? \"\").startsWith(\"/etc\"),\n * };\n * ```\n */\n when: WhenFunctionSchema.optional(),\n});\nexport type InterruptOnConfig = z.input<typeof InterruptOnConfigSchema>;\n\n/**\n * Represents an action with a name and arguments.\n */\nexport interface Action {\n /**\n * The type or name of action being requested (e.g., \"add_numbers\").\n */\n name: string;\n /**\n * Key-value pairs of arguments needed for the action (e.g., {\"a\": 1, \"b\": 2}).\n */\n args: Record<string, any>;\n}\n\n/**\n * Represents an action request with a name, arguments, and description.\n */\nexport interface ActionRequest {\n /**\n * The name of the action being requested.\n */\n name: string;\n /**\n * Key-value pairs of arguments needed for the action (e.g., {\"a\": 1, \"b\": 2}).\n */\n args: Record<string, any>;\n /**\n * The description of the action to be reviewed.\n */\n description?: string;\n}\n\n/**\n * Policy for reviewing a HITL request.\n */\nexport interface ReviewConfig {\n /**\n * Name of the action associated with this review configuration.\n */\n actionName: string;\n /**\n * The decisions that are allowed for this request.\n */\n allowedDecisions: DecisionType[];\n /**\n * JSON schema for the arguments associated with the action, if edits are allowed.\n */\n argsSchema?: Record<string, any>;\n}\n\n/**\n * Request for human feedback on a sequence of actions requested by a model.\n *\n * @example\n * ```ts\n * const hitlRequest: HITLRequest = {\n * actionRequests: [\n * { name: \"send_email\", args: { to: \"user@example.com\", subject: \"Hello\" } }\n * ],\n * reviewConfigs: [\n * {\n * actionName: \"send_email\",\n * allowedDecisions: [\"approve\", \"edit\", \"reject\"],\n * description: \"Please review the email before sending\"\n * }\n * ]\n * };\n * const response = interrupt(hitlRequest);\n * ```\n */\nexport interface HITLRequest {\n /**\n * A list of agent actions for human review.\n */\n actionRequests: ActionRequest[];\n /**\n * Review configuration for all possible actions.\n */\n reviewConfigs: ReviewConfig[];\n}\n\n/**\n * Response when a human approves the action.\n */\nexport interface ApproveDecision {\n type: \"approve\";\n}\n\n/**\n * Response when a human edits the action.\n */\nexport interface EditDecision {\n type: \"edit\";\n /**\n * Edited action for the agent to perform.\n * Ex: for a tool call, a human reviewer can edit the tool name and args.\n */\n editedAction: Action;\n}\n\n/**\n * Response when a human rejects the action.\n */\nexport interface RejectDecision {\n type: \"reject\";\n /**\n * The message sent to the model explaining why the action was rejected.\n */\n message?: string;\n}\n\n/**\n * Response when a human answers on behalf of the tool.\n */\nexport interface RespondDecision {\n type: \"respond\";\n /**\n * The message sent to the model as the tool result.\n */\n message: string;\n}\n\n/**\n * Union of all possible decision types.\n */\nexport type Decision =\n | ApproveDecision\n | EditDecision\n | RejectDecision\n | RespondDecision;\n\n/**\n * Response payload for a HITLRequest.\n */\nexport interface HITLResponse {\n /**\n * The decisions made by the human.\n */\n decisions: Decision[];\n}\n\nconst contextSchema = z.object({\n /**\n * Mapping of tool name to allowed reviewer responses.\n * If a tool doesn't have an entry, it's auto-approved by default.\n *\n * - `true` -> pause for approval and allow approve/edit/reject decisions\n * - `false` -> auto-approve (no human review)\n * - `InterruptOnConfig` -> explicitly specify which decisions are allowed for this tool\n */\n interruptOn: z\n .record(z.union([z.boolean(), InterruptOnConfigSchema]))\n .optional(),\n /**\n * Prefix used when constructing human-facing approval messages.\n * Provides context about the tool call being reviewed; does not change the underlying action.\n *\n * Note: This prefix is only applied for tools that do not provide a custom\n * `description` via their {@link InterruptOnConfig}. If a tool specifies a custom\n * `description`, that per-tool text is used and this prefix is ignored.\n */\n descriptionPrefix: z.string().default(\"Tool execution requires approval\"),\n});\nexport type HumanInTheLoopMiddlewareConfig = InferInteropZodInput<\n typeof contextSchema\n>;\n\n/**\n * Creates a Human-in-the-Loop (HITL) middleware for tool approval and oversight.\n *\n * This middleware intercepts tool calls made by an AI agent and provides human oversight\n * capabilities before execution. It enables selective approval workflows where certain tools\n * require human intervention while others can execute automatically.\n *\n * A invocation result that has been interrupted by the middleware will have a `__interrupt__`\n * property that contains the interrupt request.\n *\n * ```ts\n * import { type HITLRequest, type HITLResponse } from \"langchain\";\n * import { type Interrupt } from \"langchain\";\n *\n * const result = await agent.invoke(request);\n * const interruptRequest = result.__interrupt__?.[0] as Interrupt<HITLRequest>;\n *\n * // Examine the action requests and review configs\n * const actionRequests = interruptRequest.value.actionRequests;\n * const reviewConfigs = interruptRequest.value.reviewConfigs;\n *\n * // Create decisions for each action\n * const resume: HITLResponse = {\n * decisions: actionRequests.map((action, i) => {\n * if (action.name === \"calculator\") {\n * return { type: \"approve\" };\n * } else if (action.name === \"write_file\") {\n * return {\n * type: \"edit\",\n * editedAction: { name: \"write_file\", args: { filename: \"safe.txt\", content: \"Safe content\" } }\n * };\n * }\n * return { type: \"reject\", message: \"Action not allowed\" };\n * })\n * };\n *\n * // Resume with decisions\n * await agent.invoke(new Command({ resume }), config);\n * ```\n *\n * ## Features\n *\n * - **Selective Tool Approval**: Configure which tools require human approval\n * - **Multiple Decision Types**: Approve, edit, or reject tool calls\n * - **Asynchronous Workflow**: Uses LangGraph's interrupt mechanism for non-blocking approval\n * - **Custom Approval Messages**: Provide context-specific descriptions for approval requests\n *\n * ## Decision Types\n *\n * When a tool requires approval, the human operator can respond with:\n * - `approve`: Execute the tool with original arguments\n * - `edit`: Modify the tool name and/or arguments before execution\n * - `reject`: Provide a manual response instead of executing the tool\n *\n * @param options - Configuration options for the middleware\n * @param options.interruptOn - Per-tool configuration mapping tool names to their settings\n * @param options.interruptOn[toolName].allowedDecisions - Array of decision types allowed for this tool (e.g., [\"approve\", \"edit\", \"reject\"])\n * @param options.interruptOn[toolName].description - Custom approval message for the tool. Can be either a static string or a callable that dynamically generates the description based on agent state, runtime, and tool call information\n * @param options.interruptOn[toolName].argsSchema - JSON schema for the arguments associated with the action, if edits are allowed\n * @param options.interruptOn[toolName].when - Optional predicate that dynamically controls whether a tool call triggers an interrupt. Returns `true` to interrupt or `false` to auto-approve the tool call.\n * @param options.descriptionPrefix - Default prefix for approval messages (default: \"Tool execution requires approval\"). Only used for tools that do not define a custom `description` in their InterruptOnConfig.\n *\n * @returns A middleware instance that can be passed to `createAgent`\n *\n * @example\n * Basic usage with selective tool approval\n * ```typescript\n * import { humanInTheLoopMiddleware } from \"langchain\";\n * import { createAgent } from \"langchain\";\n *\n * const hitlMiddleware = humanInTheLoopMiddleware({\n * interruptOn: {\n * // Interrupt write_file tool and allow edits or approvals\n * \"write_file\": {\n * allowedDecisions: [\"approve\", \"edit\"],\n * description: \"⚠️ File write operation requires approval\"\n * },\n * // Auto-approve read_file tool\n * \"read_file\": false\n * }\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4\",\n * tools: [writeFileTool, readFileTool],\n * middleware: [hitlMiddleware]\n * });\n * ```\n *\n * @example\n * Handling approval requests\n * ```typescript\n * import { type HITLRequest, type HITLResponse, type Interrupt } from \"langchain\";\n * import { Command } from \"@langchain/langgraph\";\n *\n * // Initial agent invocation\n * const result = await agent.invoke({\n * messages: [new HumanMessage(\"Write 'Hello' to output.txt\")]\n * }, config);\n *\n * // Check if agent is paused for approval\n * if (result.__interrupt__) {\n * const interruptRequest = result.__interrupt__?.[0] as Interrupt<HITLRequest>;\n *\n * // Show tool call details to user\n * console.log(\"Actions:\", interruptRequest.value.actionRequests);\n * console.log(\"Review configs:\", interruptRequest.value.reviewConfigs);\n *\n * // Resume with approval\n * const resume: HITLResponse = {\n * decisions: [{ type: \"approve\" }]\n * };\n * await agent.invoke(\n * new Command({ resume }),\n * config\n * );\n * }\n * ```\n *\n * @example\n * Different decision types\n * ```typescript\n * import { type HITLResponse } from \"langchain\";\n *\n * // Approve the tool call as-is\n * const resume: HITLResponse = {\n * decisions: [{ type: \"approve\" }]\n * };\n *\n * // Edit the tool arguments\n * const resume: HITLResponse = {\n * decisions: [{\n * type: \"edit\",\n * editedAction: { name: \"write_file\", args: { filename: \"safe.txt\", content: \"Modified\" } }\n * }]\n * };\n *\n * // Reject with feedback\n * const resume: HITLResponse = {\n * decisions: [{\n * type: \"reject\",\n * message: \"File operation not allowed in demo mode\"\n * }]\n * };\n * ```\n *\n * @example\n * Production use case with database operations\n * ```typescript\n * const hitlMiddleware = humanInTheLoopMiddleware({\n * interruptOn: {\n * \"execute_sql\": {\n * allowedDecisions: [\"approve\", \"edit\", \"reject\"],\n * description: \"🚨 SQL query requires DBA approval\\nPlease review for safety and performance\"\n * },\n * \"read_schema\": false, // Reading metadata is safe\n * \"delete_records\": {\n * allowedDecisions: [\"approve\", \"reject\"],\n * description: \"⛔ DESTRUCTIVE OPERATION - Requires manager approval\"\n * }\n * },\n * descriptionPrefix: \"Database operation pending approval\"\n * });\n * ```\n *\n * @example\n * Using dynamic callable descriptions\n * ```typescript\n * import { type DescriptionFactory, type ToolCall } from \"langchain\";\n * import type { AgentBuiltInState, Runtime } from \"langchain/agents\";\n *\n * // Define a dynamic description factory\n * const formatToolDescription: DescriptionFactory = (\n * toolCall: ToolCall,\n * state: AgentBuiltInState,\n * runtime: Runtime<unknown>\n * ) => {\n * return `Tool: ${toolCall.name}\\nArguments:\\n${JSON.stringify(toolCall.args, null, 2)}`;\n * };\n *\n * const hitlMiddleware = humanInTheLoopMiddleware({\n * interruptOn: {\n * \"write_file\": {\n * allowedDecisions: [\"approve\", \"edit\"],\n * // Use dynamic description that can access tool call, state, and runtime\n * description: formatToolDescription\n * },\n * // Or use an inline function\n * \"send_email\": {\n * allowedDecisions: [\"approve\", \"reject\"],\n * description: (toolCall, state, runtime) => {\n * const { to, subject } = toolCall.args;\n * return `Email to ${to}\\nSubject: ${subject}\\n\\nRequires approval before sending`;\n * }\n * }\n * }\n * });\n * ```\n *\n * @remarks\n * - Tool calls are processed in the order they appear in the AI message\n * - Auto-approved tools execute immediately without interruption\n * - Multiple tools requiring approval are bundled into a single interrupt request\n * - The middleware operates in the `afterModel` phase, intercepting before tool execution\n * - Requires a checkpointer to maintain state across interruptions\n *\n * @see {@link createAgent} for agent creation\n * @see {@link Command} for resuming interrupted execution\n * @public\n */\nexport function humanInTheLoopMiddleware(\n options: NonNullable<HumanInTheLoopMiddlewareConfig>\n) {\n const createActionAndConfig = async (\n toolCall: ToolCall,\n config: InterruptOnConfig,\n state: AgentBuiltInState,\n runtime: Runtime<unknown>\n ): Promise<{\n actionRequest: ActionRequest;\n reviewConfig: ReviewConfig;\n }> => {\n const toolName = toolCall.name;\n const toolArgs = toolCall.args;\n\n // Generate description using the description field (str or callable)\n const descriptionValue = config.description;\n let description: string;\n if (typeof descriptionValue === \"function\") {\n description = await descriptionValue(toolCall, state, runtime);\n } else if (descriptionValue !== undefined) {\n description = descriptionValue;\n } else {\n description = `${\n options.descriptionPrefix ?? \"Tool execution requires approval\"\n }\\n\\nTool: ${toolName}\\nArgs: ${JSON.stringify(toolArgs, null, 2)}`;\n }\n\n /**\n * Create ActionRequest with description\n */\n const actionRequest: ActionRequest = {\n name: toolName,\n args: toolArgs,\n description,\n };\n\n /**\n * Create ReviewConfig\n */\n const reviewConfig: ReviewConfig = {\n actionName: toolName,\n allowedDecisions: config.allowedDecisions,\n };\n\n if (config.argsSchema) {\n reviewConfig.argsSchema = config.argsSchema;\n }\n\n return { actionRequest, reviewConfig };\n };\n\n /**\n * Return `false` if the `when` predicate rejects this tool call, `true` otherwise.\n *\n * When no `when` predicate is configured the tool call always interrupts.\n */\n const shouldInterrupt = async (\n toolCall: ToolCall,\n config: InterruptOnConfig,\n state: AgentBuiltInState,\n runtime: Runtime<unknown>\n ): Promise<boolean> => {\n const { when } = config;\n if (when == null) {\n return true;\n }\n const request: ToolCallRequest<AgentBuiltInState> = {\n toolCall,\n tool: undefined,\n state,\n runtime,\n };\n return when(request);\n };\n\n const processDecision = (\n decision: Decision,\n toolCall: ToolCall,\n config: InterruptOnConfig\n ): { revisedToolCall: ToolCall | null; toolMessage: ToolMessage | null } => {\n const allowedDecisions = config.allowedDecisions;\n if (decision.type === \"approve\" && allowedDecisions.includes(\"approve\")) {\n return { revisedToolCall: toolCall, toolMessage: null };\n }\n\n if (decision.type === \"edit\" && allowedDecisions.includes(\"edit\")) {\n const editedAction = decision.editedAction;\n\n /**\n * Validate edited action structure\n */\n if (!editedAction || typeof editedAction.name !== \"string\") {\n throw new Error(\n `Invalid edited action for tool \"${toolCall.name}\": name must be a string`\n );\n }\n if (!editedAction.args || typeof editedAction.args !== \"object\") {\n throw new Error(\n `Invalid edited action for tool \"${toolCall.name}\": args must be an object`\n );\n }\n\n return {\n revisedToolCall: {\n type: \"tool_call\",\n name: editedAction.name,\n args: editedAction.args,\n id: toolCall.id,\n },\n toolMessage: null,\n };\n }\n\n if (decision.type === \"reject\" && allowedDecisions.includes(\"reject\")) {\n /**\n * Validate that message is a string if provided\n */\n if (\n decision.message !== undefined &&\n typeof decision.message !== \"string\"\n ) {\n throw new Error(\n `Tool call response for \"${\n toolCall.name\n }\" must be a string, got ${typeof decision.message}`\n );\n }\n\n // Create a tool message with the human's text response\n const content =\n decision.message ??\n `User rejected the tool call for \\`${toolCall.name}\\` with id ${toolCall.id}`;\n\n const toolMessage = new ToolMessage({\n content,\n name: toolCall.name,\n tool_call_id: toolCall.id!,\n status: \"error\",\n });\n\n return { revisedToolCall: toolCall, toolMessage };\n }\n\n if (decision.type === \"respond\" && allowedDecisions.includes(\"respond\")) {\n if (typeof decision.message !== \"string\") {\n throw new Error(\n `Tool call response for \"${\n toolCall.name\n }\" must be a string, got ${typeof decision.message}`\n );\n }\n\n const toolMessage = new ToolMessage({\n content: decision.message,\n name: toolCall.name,\n tool_call_id: toolCall.id!,\n status: \"success\",\n });\n\n return { revisedToolCall: toolCall, toolMessage };\n }\n\n const msg = `Unexpected human decision: ${JSON.stringify(\n decision\n )}. Decision type '${decision.type}' is not allowed for tool '${\n toolCall.name\n }'. Expected one of ${JSON.stringify(\n allowedDecisions\n )} based on the tool's configuration.`;\n throw new Error(msg);\n };\n\n return createMiddleware({\n name: \"HumanInTheLoopMiddleware\",\n contextSchema,\n afterModel: {\n canJumpTo: [\"model\"],\n hook: async (state, runtime) => {\n const config = interopParse(contextSchema, {\n ...options,\n ...(runtime.context || {}),\n });\n if (!config) {\n return;\n }\n\n const { messages } = state;\n if (!messages.length) {\n return;\n }\n\n /**\n * Don't do anything if the last message isn't an AI message with tool calls.\n */\n const lastMessage = [...messages]\n .reverse()\n .find((msg) => AIMessage.isInstance(msg)) as AIMessage;\n if (!lastMessage || !lastMessage.tool_calls?.length) {\n return;\n }\n\n /**\n * If the user omits the interruptOn config, we don't do anything.\n */\n if (!config.interruptOn) {\n return;\n }\n\n /**\n * Resolve per-tool configs (boolean true -> all decisions allowed; false -> auto-approve)\n */\n const resolvedConfigs: Record<string, InterruptOnConfig> = {};\n for (const [toolName, toolConfig] of Object.entries(\n config.interruptOn\n )) {\n if (typeof toolConfig === \"boolean\") {\n if (toolConfig === true) {\n resolvedConfigs[toolName] = {\n allowedDecisions: [...DEFAULT_ALLOWED_DECISIONS],\n };\n }\n } else if (toolConfig.allowedDecisions) {\n resolvedConfigs[toolName] = toolConfig as InterruptOnConfig;\n }\n }\n\n const interruptToolCalls: ToolCall[] = [];\n const autoApprovedToolCalls: ToolCall[] = [];\n\n for (const toolCall of lastMessage.tool_calls) {\n const interruptConfig = resolvedConfigs[toolCall.name];\n /**\n * A tool call is interrupted only when it has a resolved config and its\n * optional `when` predicate doesn't opt it out. Otherwise it is\n * auto-approved.\n */\n if (\n interruptConfig &&\n (await shouldInterrupt(toolCall, interruptConfig, state, runtime))\n ) {\n interruptToolCalls.push(toolCall);\n } else {\n autoApprovedToolCalls.push(toolCall);\n }\n }\n\n /**\n * No interrupt tool calls, so we can just return.\n */\n if (!interruptToolCalls.length) {\n return;\n }\n\n /**\n * Create action requests and review configs for all tools that need approval\n */\n const actionRequests: ActionRequest[] = [];\n const reviewConfigs: ReviewConfig[] = [];\n\n for (const toolCall of interruptToolCalls) {\n const interruptConfig = resolvedConfigs[toolCall.name]!;\n\n /**\n * Create ActionRequest and ReviewConfig using helper method\n */\n const { actionRequest, reviewConfig } = await createActionAndConfig(\n toolCall,\n interruptConfig,\n state,\n runtime\n );\n actionRequests.push(actionRequest);\n reviewConfigs.push(reviewConfig);\n }\n\n /**\n * Create single HITLRequest with all actions and configs\n */\n const hitlRequest: HITLRequest = {\n actionRequests,\n reviewConfigs,\n };\n\n /**\n * Send interrupt and get response\n */\n const hitlResponse = (await interrupt(hitlRequest)) as HITLResponse;\n const decisions = hitlResponse.decisions;\n\n /**\n * Validate that decisions is a valid array before checking length\n */\n if (!decisions || !Array.isArray(decisions)) {\n throw new Error(\n \"Invalid HITLResponse: decisions must be a non-empty array\"\n );\n }\n\n /**\n * Validate that the number of decisions matches the number of interrupt tool calls\n */\n if (decisions.length !== interruptToolCalls.length) {\n throw new Error(\n `Number of human decisions (${decisions.length}) does not match number of hanging tool calls (${interruptToolCalls.length}).`\n );\n }\n\n const revisedToolCalls: ToolCall[] = [...autoApprovedToolCalls];\n const artificialToolMessages: ToolMessage[] = [];\n const hasRejectedToolCalls = decisions.some(\n (decision) => decision.type === \"reject\"\n );\n const hasRespondedToolCalls = decisions.some(\n (decision) => decision.type === \"respond\"\n );\n\n /**\n * Process each decision using helper method\n */\n for (let i = 0; i < decisions.length; i++) {\n const decision = decisions[i]!;\n const toolCall = interruptToolCalls[i]!;\n const interruptConfig = resolvedConfigs[toolCall.name]!;\n\n const { revisedToolCall, toolMessage } = processDecision(\n decision,\n toolCall,\n interruptConfig\n );\n\n if (\n revisedToolCall &&\n /**\n * If any decision is a rejected, we are going back to the model\n * with only the tool calls that were rejected as we don't know\n * the results of the approved/updated tool calls at this point.\n */\n (!hasRejectedToolCalls ||\n decision.type === \"reject\" ||\n decision.type === \"respond\")\n ) {\n revisedToolCalls.push(revisedToolCall);\n }\n if (toolMessage) {\n artificialToolMessages.push(toolMessage);\n }\n }\n\n /**\n * Update the AI message to only include approved tool calls\n */\n if (AIMessage.isInstance(lastMessage)) {\n lastMessage.tool_calls = revisedToolCalls;\n }\n\n const hasPendingToolCalls = revisedToolCalls.some(\n (toolCall) =>\n !artificialToolMessages.some(\n (message) => message.tool_call_id === toolCall.id\n )\n );\n const jumpTo: JumpToTarget | undefined =\n hasRejectedToolCalls ||\n (hasRespondedToolCalls && !hasPendingToolCalls)\n ? \"model\"\n : undefined;\n return {\n messages: [lastMessage, ...artificialToolMessages],\n jumpTo,\n };\n },\n },\n });\n}\n"],"mappings":";;;;;;AAcA,MAAM,qBAAqB,EACxB,SAAS,CAAC,CACV,KAAK,EAAE,OAA2C,CAAC,CAAC,CACpD,QAAQ,EAAE,MAAM,CAAC,EAAE,QAAQ,GAAG,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;AA2BzD,MAAM,4BAA4B,EAC/B,SAAS,CAAC,CACV,KACC,EAAE,OAAiB,GACnB,EAAE,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C,MAAM,WAAW,UAAU;KAC3B,MAAM,WAAW,mBAAmB;KACpC,MAAM,kBAAkB,gBAAgB,SAAS;KAEjD,MAAM,EAAE,iBAAiB,gBAAgB,gBACvC,UACA,UACA,eACF;KAEA,IACE,oBAMC,CAAC,wBACA,SAAS,SAAS,YAClB,SAAS,SAAS,YAEpB,iBAAiB,KAAK,eAAe;KAEvC,IAAI,aACF,uBAAuB,KAAK,WAAW;IAE3C;;;;IAKA,IAAI,UAAU,WAAW,WAAW,GAClC,YAAY,aAAa;IAG3B,MAAM,sBAAsB,iBAAiB,MAC1C,aACC,CAAC,uBAAuB,MACrB,YAAY,QAAQ,iBAAiB,SAAS,EACjD,CACJ;IACA,MAAM,SACJ,wBACC,yBAAyB,CAAC,sBACvB,UACA,KAAA;IACN,OAAO;KACL,UAAU,CAAC,aAAa,GAAG,sBAAsB;KACjD;IACF;GACF;EACF;CACF,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"hitl.js","names":["z","z4"],"sources":["../../../src/agents/middleware/hitl.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v3\";\nimport { z as z4 } from \"zod/v4\";\nimport { AIMessage, ToolMessage, ToolCall } from \"@langchain/core/messages\";\nimport {\n InferInteropZodInput,\n interopParse,\n interopZodTransformInputSchema,\n isZodSchemaV4,\n} from \"@langchain/core/utils/types\";\nimport { Command, interrupt, isCommand } from \"@langchain/langgraph\";\n\nimport { createMiddleware } from \"../middleware.js\";\nimport type { AgentBuiltInState, Runtime } from \"../runtime.js\";\nimport type { JumpToTarget } from \"../constants.js\";\nimport type { Interrupt } from \"../types.js\";\nimport type { ToolCallRequest } from \"./types.js\";\n\nconst WhenFunctionSchema = z\n .function()\n .args(z.custom<ToolCallRequest<AgentBuiltInState>>()) // request\n .returns(z.union([z.boolean(), z.promise(z.boolean())]));\n\n/**\n * Predicate controlling whether a given tool call triggers an interrupt.\n *\n * Receives a {@link ToolCallRequest} and returns `true` to interrupt or `false`\n * to auto-approve the tool call.\n *\n * The request is constructed with `tool` set to `undefined` and `runtime` set to\n * the node-level {@link Runtime}, so it reflects the batch (`afterModel`) context\n * rather than a per-call tool execution. In `\"per_call\"` mode the predicate runs\n * in `wrapToolCall` and receives the real request, with `tool` set.\n *\n * In both modes the predicate runs again when the run resumes, so it must return\n * the same answer for the same call. If it returns `false` on resume, the\n * reviewer's answer is skipped and the tool runs, even if they rejected it.\n *\n * @param request - The tool call request being evaluated\n * @returns `true` to interrupt for the tool call, `false` to auto-approve it.\n * May also return a promise resolving to a boolean.\n *\n * @example\n * ```typescript\n * import { type WhenPredicate } from \"langchain\";\n *\n * // Only interrupt delete_file calls targeting /etc\n * const when: WhenPredicate = (request) =>\n * String(request.toolCall.args.path ?? \"\").startsWith(\"/etc\");\n * ```\n */\nexport type WhenPredicate = z.infer<typeof WhenFunctionSchema>;\n\nconst DescriptionFunctionSchema = z\n .function()\n .args(\n z.custom<ToolCall>(), // toolCall\n z.custom<AgentBuiltInState>(), // state\n z.custom<Runtime<unknown>>() // runtime\n )\n .returns(z.union([z.string(), z.promise(z.string())]));\n\n/**\n * Function type that dynamically generates a description for a tool call approval request.\n *\n * @param toolCall - The tool call being reviewed\n * @param state - The current agent state\n * @param runtime - The agent runtime context\n * @returns A string description or Promise that resolves to a string description\n *\n * @example\n * ```typescript\n * import { type DescriptionFactory, type ToolCall } from \"langchain\";\n *\n * const descriptionFactory: DescriptionFactory = (toolCall, state, runtime) => {\n * return `Please review: ${toolCall.name}(${JSON.stringify(toolCall.args)})`;\n * };\n * ```\n */\nexport type DescriptionFactory = z.infer<typeof DescriptionFunctionSchema>;\n\n/**\n * The type of decision a human can make.\n */\nconst ALLOWED_DECISIONS = [\"approve\", \"edit\", \"reject\", \"respond\"] as const;\nconst DecisionType = z.enum(ALLOWED_DECISIONS);\nexport type DecisionType = z.infer<typeof DecisionType>;\n\nconst InterruptOnConfigSchema = z.object({\n /**\n * The decisions that are allowed for this action.\n */\n allowedDecisions: z.array(DecisionType),\n /**\n * The description attached to the request for human input.\n * Can be either:\n * - A static string describing the approval request\n * - A callable that dynamically generates the description based on agent state,\n * runtime, and tool call information\n *\n * @example\n * Static string description\n * ```typescript\n * import type { InterruptOnConfig } from \"langchain\";\n *\n * const config: InterruptOnConfig = {\n * allowedDecisions: [\"approve\", \"reject\"],\n * description: \"Please review this tool execution\"\n * };\n * ```\n *\n * @example\n * Dynamic callable description\n * ```typescript\n * import type {\n * AgentBuiltInState,\n * Runtime,\n * DescriptionFactory,\n * ToolCall,\n * InterruptOnConfig\n * } from \"langchain\";\n *\n * const formatToolDescription: DescriptionFactory = (\n * toolCall: ToolCall,\n * state: AgentBuiltInState,\n * runtime: Runtime<unknown>\n * ) => {\n * return `Tool: ${toolCall.name}\\nArguments:\\n${JSON.stringify(toolCall.args, null, 2)}`;\n * };\n *\n * const config: InterruptOnConfig = {\n * allowedDecisions: [\"approve\", \"edit\"],\n * description: formatToolDescription\n * };\n * ```\n */\n description: z.union([z.string(), DescriptionFunctionSchema]).optional(),\n /**\n * JSON schema for the arguments associated with the action, if edits are allowed.\n *\n * Not used in `\"per_call\"` mode: an edit's `args` are checked against the tool's\n * own schema there.\n */\n argsSchema: z.record(z.any()).optional(),\n /**\n * Optional predicate controlling whether to interrupt for a given tool call.\n *\n * Receives a {@link ToolCallRequest} and returns `true` to interrupt or\n * `false` to auto-approve the tool call.\n *\n * The request is constructed with `tool` set to `undefined` and `runtime` set\n * to the node-level {@link Runtime}, so `request.tool` is not available. In\n * `\"per_call\"` mode the predicate runs in `wrapToolCall` and receives the real\n * request, with `tool` set.\n *\n * @example\n * ```typescript\n * import type { InterruptOnConfig } from \"langchain\";\n *\n * // Only interrupt delete_file calls targeting /etc\n * const config: InterruptOnConfig = {\n * allowedDecisions: [\"approve\", \"reject\"],\n * when: (request) =>\n * String(request.toolCall.args.path ?? \"\").startsWith(\"/etc\"),\n * };\n * ```\n */\n when: WhenFunctionSchema.optional(),\n});\nexport type InterruptOnConfig = z.input<typeof InterruptOnConfigSchema>;\n\n/**\n * Represents an action with a name and arguments.\n */\nexport interface Action {\n /**\n * The type or name of action being requested (e.g., \"add_numbers\").\n */\n name: string;\n /**\n * Key-value pairs of arguments needed for the action (e.g., {\"a\": 1, \"b\": 2}).\n */\n args: Record<string, any>;\n}\n\n/**\n * Represents an action request with a name, arguments, and description.\n */\nexport interface ActionRequest {\n /**\n * The name of the action being requested.\n */\n name: string;\n /**\n * Key-value pairs of arguments needed for the action (e.g., {\"a\": 1, \"b\": 2}).\n */\n args: Record<string, any>;\n /**\n * The description of the action to be reviewed.\n */\n description?: string;\n}\n\n/**\n * Policy for reviewing a HITL request.\n */\nexport interface ReviewConfig {\n /**\n * Name of the action associated with this review configuration.\n */\n actionName: string;\n /**\n * The decisions that are allowed for this request.\n */\n allowedDecisions: DecisionType[];\n /**\n * JSON schema for the arguments associated with the action, if edits are allowed.\n */\n argsSchema?: Record<string, any>;\n}\n\n/**\n * Request for human feedback on a sequence of actions requested by a model.\n *\n * @example\n * ```ts\n * const hitlRequest: HITLRequest = {\n * actionRequests: [\n * { name: \"send_email\", args: { to: \"user@example.com\", subject: \"Hello\" } }\n * ],\n * reviewConfigs: [\n * {\n * actionName: \"send_email\",\n * allowedDecisions: [\"approve\", \"edit\", \"reject\"],\n * description: \"Please review the email before sending\"\n * }\n * ]\n * };\n * const response = interrupt(hitlRequest);\n * ```\n */\nexport interface HITLRequest {\n /**\n * A list of agent actions for human review.\n */\n actionRequests: ActionRequest[];\n /**\n * Review configuration for all possible actions.\n */\n reviewConfigs: ReviewConfig[];\n}\n\n/**\n * Response when a human approves the action.\n */\nexport interface ApproveDecision {\n type: \"approve\";\n}\n\n/**\n * Response when a human edits the action.\n */\nexport interface EditDecision {\n type: \"edit\";\n /**\n * Edited action for the agent to perform.\n * Ex: for a tool call, a human reviewer can edit the tool name and args.\n */\n editedAction: Action;\n}\n\n/**\n * Response when a human rejects the action.\n */\nexport interface RejectDecision {\n type: \"reject\";\n /**\n * The message sent to the model explaining why the action was rejected.\n */\n message?: string;\n}\n\n/**\n * Response when a human answers on behalf of the tool.\n */\nexport interface RespondDecision {\n type: \"respond\";\n /**\n * The message sent to the model as the tool result.\n */\n message: string;\n}\n\n/**\n * Union of all possible decision types.\n */\nexport type Decision =\n | ApproveDecision\n | EditDecision\n | RejectDecision\n | RespondDecision;\n\n/**\n * Response payload for a HITLRequest.\n */\nexport interface HITLResponse {\n /**\n * The decisions made by the human.\n */\n decisions: Decision[];\n}\n\nconst ToolApprovalRequestSchema = z4.object({\n /** Always `\"tool_approval\"`; tells clients how to read this interrupt. */\n type: z4.literal(\"tool_approval\"),\n /** ID of the model's tool call this approval is about. */\n tool_call_id: z4.string(),\n /** Tool name, as the model requested it. */\n name: z4.string(),\n /** Tool arguments, as the model requested them. */\n args: z4.record(z4.string(), z4.unknown()),\n /** Text shown to the reviewer. */\n description: z4.string(),\n});\n\n/**\n * Interrupt value raised once per gated tool call in `\"per_call\"` mode. Keys are\n * snake_case, as in Python.\n */\nexport type ToolApprovalRequest = z4.infer<typeof ToolApprovalRequestSchema>;\n\n/**\n * What an edit's `args` must look like: the input side of the tool's schema, if it's\n * a Zod v4 object. Transforms are left out, so they run once, when the tool does.\n * Args the tool doesn't declare are handled as the tool handles them from the model.\n * Any other tool takes any object, and checks it when it runs.\n */\nfunction editArgs(\n tool?: ToolCallRequest[\"tool\"]\n): z4.ZodType<Record<string, unknown>> {\n const schema = tool && \"schema\" in tool ? tool.schema : undefined;\n const input = isZodSchemaV4(schema)\n ? interopZodTransformInputSchema(schema, true)\n : undefined;\n return input instanceof z4.ZodObject\n ? input\n : z4.record(z4.string(), z4.unknown());\n}\n\n/**\n * The answers a reviewer can give to a `\"per_call\"` approval of tool `name`, by type.\n *\n * The edit can't switch tools: its `name` is pinned.\n */\nconst toolApprovalDecisions = (\n name: string,\n tool?: ToolCallRequest[\"tool\"]\n) => ({\n approve: z4.object({ type: z4.literal(\"approve\") }),\n edit: z4.object({\n type: z4.literal(\"edit\"),\n edited_action: z4.object({\n name: z4.literal(name),\n args: editArgs(tool),\n }),\n }),\n reject: z4.object({\n type: z4.literal(\"reject\"),\n message: z4.string().optional(),\n }),\n respond: z4.object({\n type: z4.literal(\"respond\"),\n message: z4.string(),\n }),\n});\n\n/**\n * A reviewer's answer to a `\"per_call\"` tool approval.\n */\nexport type ToolApprovalDecision = z4.infer<\n ReturnType<typeof toolApprovalDecisions>[DecisionType]\n>;\n\n/**\n * Whether an interrupt is a `\"per_call\"` tool approval, e.g.\n * `result.__interrupt__?.filter(isToolApprovalInterrupt)`.\n */\nexport function isToolApprovalInterrupt(\n interrupt: Interrupt | undefined\n): interrupt is Interrupt<ToolApprovalRequest> {\n return ToolApprovalRequestSchema.safeParse(interrupt?.value).success;\n}\n\n/**\n * The per-call `responseSchema`: the `allowed` decisions for tool `name`.\n *\n * An answer is checked only against the branch its `type` names, so a bad one gets a\n * single error. A one-decision tool gets that decision's plain object schema.\n */\nfunction decisionSchema(\n allowed: readonly DecisionType[],\n name: string,\n tool?: ToolCallRequest[\"tool\"]\n): z4.ZodType<ToolApprovalDecision> {\n const byType = toolApprovalDecisions(name, tool);\n // Drop duplicates, keeping order: a discriminated union can't repeat a `type`.\n const [first, ...rest] = [...new Set(allowed)].map((d) => byType[d]);\n if (first === undefined) {\n throw new Error(\"allowedDecisions must list at least one decision.\");\n }\n return rest.length ? z4.discriminatedUnion(\"type\", [first, ...rest]) : first;\n}\n\nconst contextSchema = z.object({\n /**\n * Mapping of tool name to allowed reviewer responses.\n * If a tool doesn't have an entry, it's auto-approved by default.\n *\n * - `true` -> pause for approval and allow every decision (approve/edit/reject/respond)\n * - `false` -> auto-approve (no human review)\n * - `InterruptOnConfig` -> explicitly specify which decisions are allowed for this tool\n */\n interruptOn: z\n .record(z.union([z.boolean(), InterruptOnConfigSchema]))\n .optional(),\n /**\n * Prefix used when constructing human-facing approval messages.\n * Provides context about the tool call being reviewed; does not change the underlying action.\n *\n * Note: This prefix is only applied for tools that do not provide a custom\n * `description` via their {@link InterruptOnConfig}. If a tool specifies a custom\n * `description`, that per-tool text is used and this prefix is ignored.\n */\n descriptionPrefix: z.string().default(\"Tool execution requires approval\"),\n});\nexport type HumanInTheLoopMiddlewareConfig = InferInteropZodInput<\n typeof contextSchema\n> & {\n /**\n * How the middleware pauses for review. Set at construction only.\n *\n * - `\"batched\"` (default): one interrupt per model turn for all gated tool calls.\n * - `\"per_call\"`: one {@link ToolApprovalRequest} interrupt per gated call, with a\n * typed `responseSchema`, answered with a single decision keyed by interrupt ID.\n *\n * In `\"per_call\"` mode, LangGraph checks each answer before saving it:\n *\n * - An edit can't switch tools, and its args must match the tool's argument\n * types. Args the tool doesn't declare are handled as they are when the model\n * sends them. For a tool without a Zod v4 object schema, any object is accepted\n * and the tool checks it when it runs.\n * - An invalid answer throws a `ZodError` and isn't saved, so the same interrupt\n * can be answered again.\n *\n * If one of several answers sent together is invalid, the others' tools may\n * already have run even if they still show as pending, and answering them again\n * would run them twice. Check answers against `responseSchema` before sending them\n * together, or send one at a time.\n *\n * The review happens as the tool call starts, inside the tool-call middleware\n * chain. List this middleware before tool retry or error-handling middleware so it\n * wraps them. On resume, each paused tool call runs the middleware listed before\n * this one again up to the pause, so that middleware must not have side effects\n * before calling `handler`. The tool itself runs only after the answer.\n */\n interruptMode?: \"batched\" | \"per_call\";\n /**\n * `\"per_call\"` mode only: text prepended to the result of a tool call a reviewer\n * edited, so the model knows the call it made isn't the one that ran. Pass `null`\n * to add nothing.\n */\n editNotice?: string | null;\n};\n\nconst DEFAULT_EDIT_NOTICE =\n \"Note: a human reviewer replaced this tool call before it ran. The call recorded in \" +\n \"your message is the one you produced, not the one that executed. This was \" +\n \"intentional and authorized. Do not re-issue your original call.\";\n\n/**\n * Resolve `interruptOn`: `true` allows every decision; `false` and missing entries\n * auto-approve.\n */\nfunction resolveInterruptOn(\n interruptOn: NonNullable<HumanInTheLoopMiddlewareConfig>[\"interruptOn\"]\n): Record<string, InterruptOnConfig> {\n const resolved: Record<string, InterruptOnConfig> = {};\n for (const [toolName, toolConfig] of Object.entries(interruptOn ?? {})) {\n if (typeof toolConfig === \"boolean\") {\n if (toolConfig === true) {\n resolved[toolName] = { allowedDecisions: [...ALLOWED_DECISIONS] };\n }\n } else if (toolConfig.allowedDecisions) {\n resolved[toolName] = toolConfig;\n }\n }\n return resolved;\n}\n\n/**\n * Return `message` with the reviewer-edit notice prepended, stating the call that ran.\n */\nfunction prependNotice(\n message: ToolMessage,\n notice: string,\n executed: Action\n): ToolMessage {\n const hasContent = message.content.length > 0;\n const text = `${notice} Executed instead: ${executed.name} with arguments ${JSON.stringify(\n executed.args\n )}.${hasContent ? \"\\n\\nTool response:\" : \"\"}`;\n return new ToolMessage({\n content:\n typeof message.content === \"string\"\n ? `${text}${hasContent ? \"\\n\" : \"\"}${message.content}`\n : [{ type: \"text\", text }, ...message.content],\n tool_call_id: message.tool_call_id,\n name: message.name,\n status: message.status,\n artifact: message.artifact,\n id: message.id,\n additional_kwargs: message.additional_kwargs,\n response_metadata: message.response_metadata,\n });\n}\n\n/**\n * Tell the model a reviewer replaced the call, and with what.\n */\nfunction withEditNotice(\n result: ToolMessage | Command,\n toolCallId: string,\n executed: Action,\n notice: string | null\n): ToolMessage | Command {\n if (!notice) {\n return result;\n }\n if (ToolMessage.isInstance(result)) {\n return prependNotice(result, notice, executed);\n }\n // A `Command` carries the `ToolMessage` in its state update.\n const update: unknown = isCommand(result) ? result.update : undefined;\n if (\n typeof update !== \"object\" ||\n update === null ||\n !(\"messages\" in update) ||\n !Array.isArray(update.messages)\n ) {\n return result;\n }\n return new Command({\n update: {\n ...update,\n messages: update.messages.map((message: unknown) =>\n ToolMessage.isInstance(message) && message.tool_call_id === toolCallId\n ? prependNotice(message, notice, executed)\n : message\n ),\n },\n resume: result.resume,\n goto: result.goto,\n graph: result.graph,\n });\n}\n\n/**\n * Creates a Human-in-the-Loop (HITL) middleware for tool approval and oversight.\n *\n * This middleware intercepts tool calls made by an AI agent and provides human oversight\n * capabilities before execution. It enables selective approval workflows where certain tools\n * require human intervention while others can execute automatically.\n *\n * A invocation result that has been interrupted by the middleware will have a `__interrupt__`\n * property that contains the interrupt request.\n *\n * ```ts\n * import { type HITLRequest, type HITLResponse } from \"langchain\";\n * import { type Interrupt } from \"langchain\";\n *\n * const result = await agent.invoke(request);\n * const interruptRequest = result.__interrupt__?.[0] as Interrupt<HITLRequest>;\n *\n * // Examine the action requests and review configs\n * const actionRequests = interruptRequest.value.actionRequests;\n * const reviewConfigs = interruptRequest.value.reviewConfigs;\n *\n * // Create decisions for each action\n * const resume: HITLResponse = {\n * decisions: actionRequests.map((action, i) => {\n * if (action.name === \"calculator\") {\n * return { type: \"approve\" };\n * } else if (action.name === \"write_file\") {\n * return {\n * type: \"edit\",\n * editedAction: { name: \"write_file\", args: { filename: \"safe.txt\", content: \"Safe content\" } }\n * };\n * }\n * return { type: \"reject\", message: \"Action not allowed\" };\n * })\n * };\n *\n * // Resume with decisions\n * await agent.invoke(new Command({ resume }), config);\n * ```\n *\n * ## Features\n *\n * - **Selective Tool Approval**: Configure which tools require human approval\n * - **Multiple Decision Types**: Approve, edit, or reject tool calls\n * - **Asynchronous Workflow**: Uses LangGraph's interrupt mechanism for non-blocking approval\n * - **Custom Approval Messages**: Provide context-specific descriptions for approval requests\n *\n * ## Decision Types\n *\n * When a tool requires approval, the human operator can respond with:\n * - `approve`: Execute the tool with original arguments\n * - `edit`: Modify the tool name and/or arguments before execution\n * - `reject`: Provide a manual response instead of executing the tool\n *\n * @param options - Configuration options for the middleware\n * @param options.interruptOn - Per-tool configuration mapping tool names to their settings\n * @param options.interruptOn[toolName].allowedDecisions - Array of decision types allowed for this tool (e.g., [\"approve\", \"edit\", \"reject\"])\n * @param options.interruptOn[toolName].description - Custom approval message for the tool. Can be either a static string or a callable that dynamically generates the description based on agent state, runtime, and tool call information\n * @param options.interruptOn[toolName].argsSchema - JSON schema for the arguments associated with the action, if edits are allowed\n * @param options.interruptOn[toolName].when - Optional predicate that dynamically controls whether a tool call triggers an interrupt. Returns `true` to interrupt or `false` to auto-approve the tool call.\n * @param options.descriptionPrefix - Default prefix for approval messages (default: \"Tool execution requires approval\"). Only used for tools that do not define a custom `description` in their InterruptOnConfig.\n *\n * @returns A middleware instance that can be passed to `createAgent`\n *\n * @example\n * Basic usage with selective tool approval\n * ```typescript\n * import { humanInTheLoopMiddleware } from \"langchain\";\n * import { createAgent } from \"langchain\";\n *\n * const hitlMiddleware = humanInTheLoopMiddleware({\n * interruptOn: {\n * // Interrupt write_file tool and allow edits or approvals\n * \"write_file\": {\n * allowedDecisions: [\"approve\", \"edit\"],\n * description: \"⚠️ File write operation requires approval\"\n * },\n * // Auto-approve read_file tool\n * \"read_file\": false\n * }\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4\",\n * tools: [writeFileTool, readFileTool],\n * middleware: [hitlMiddleware]\n * });\n * ```\n *\n * @example\n * Handling approval requests\n * ```typescript\n * import { type HITLRequest, type HITLResponse, type Interrupt } from \"langchain\";\n * import { Command } from \"@langchain/langgraph\";\n *\n * // Initial agent invocation\n * const result = await agent.invoke({\n * messages: [new HumanMessage(\"Write 'Hello' to output.txt\")]\n * }, config);\n *\n * // Check if agent is paused for approval\n * if (result.__interrupt__) {\n * const interruptRequest = result.__interrupt__?.[0] as Interrupt<HITLRequest>;\n *\n * // Show tool call details to user\n * console.log(\"Actions:\", interruptRequest.value.actionRequests);\n * console.log(\"Review configs:\", interruptRequest.value.reviewConfigs);\n *\n * // Resume with approval\n * const resume: HITLResponse = {\n * decisions: [{ type: \"approve\" }]\n * };\n * await agent.invoke(\n * new Command({ resume }),\n * config\n * );\n * }\n * ```\n *\n * @example\n * Different decision types\n * ```typescript\n * import { type HITLResponse } from \"langchain\";\n *\n * // Approve the tool call as-is\n * const resume: HITLResponse = {\n * decisions: [{ type: \"approve\" }]\n * };\n *\n * // Edit the tool arguments\n * const resume: HITLResponse = {\n * decisions: [{\n * type: \"edit\",\n * editedAction: { name: \"write_file\", args: { filename: \"safe.txt\", content: \"Modified\" } }\n * }]\n * };\n *\n * // Reject with feedback\n * const resume: HITLResponse = {\n * decisions: [{\n * type: \"reject\",\n * message: \"File operation not allowed in demo mode\"\n * }]\n * };\n * ```\n *\n * @example\n * Production use case with database operations\n * ```typescript\n * const hitlMiddleware = humanInTheLoopMiddleware({\n * interruptOn: {\n * \"execute_sql\": {\n * allowedDecisions: [\"approve\", \"edit\", \"reject\"],\n * description: \"🚨 SQL query requires DBA approval\\nPlease review for safety and performance\"\n * },\n * \"read_schema\": false, // Reading metadata is safe\n * \"delete_records\": {\n * allowedDecisions: [\"approve\", \"reject\"],\n * description: \"⛔ DESTRUCTIVE OPERATION - Requires manager approval\"\n * }\n * },\n * descriptionPrefix: \"Database operation pending approval\"\n * });\n * ```\n *\n * @example\n * Using dynamic callable descriptions\n * ```typescript\n * import { type DescriptionFactory, type ToolCall } from \"langchain\";\n * import type { AgentBuiltInState, Runtime } from \"langchain/agents\";\n *\n * // Define a dynamic description factory\n * const formatToolDescription: DescriptionFactory = (\n * toolCall: ToolCall,\n * state: AgentBuiltInState,\n * runtime: Runtime<unknown>\n * ) => {\n * return `Tool: ${toolCall.name}\\nArguments:\\n${JSON.stringify(toolCall.args, null, 2)}`;\n * };\n *\n * const hitlMiddleware = humanInTheLoopMiddleware({\n * interruptOn: {\n * \"write_file\": {\n * allowedDecisions: [\"approve\", \"edit\"],\n * // Use dynamic description that can access tool call, state, and runtime\n * description: formatToolDescription\n * },\n * // Or use an inline function\n * \"send_email\": {\n * allowedDecisions: [\"approve\", \"reject\"],\n * description: (toolCall, state, runtime) => {\n * const { to, subject } = toolCall.args;\n * return `Email to ${to}\\nSubject: ${subject}\\n\\nRequires approval before sending`;\n * }\n * }\n * }\n * });\n * ```\n *\n * @remarks\n * - Tool calls are processed in the order they appear in the AI message\n * - Auto-approved tools execute immediately without interruption\n * - In the default `\"batched\"` mode, multiple tools requiring approval are bundled into a\n * single interrupt request, raised in the `afterModel` phase before any tool runs. In\n * `\"per_call\"` mode, each gated call raises its own interrupt from `wrapToolCall`\n * - Requires a checkpointer to maintain state across interruptions\n *\n * @see {@link createAgent} for agent creation\n * @see {@link Command} for resuming interrupted execution\n * @public\n */\nexport function humanInTheLoopMiddleware(\n options: NonNullable<HumanInTheLoopMiddlewareConfig>\n) {\n const createActionAndConfig = async (\n toolCall: ToolCall,\n config: InterruptOnConfig,\n state: AgentBuiltInState,\n runtime: Runtime<unknown>\n ): Promise<{\n actionRequest: ActionRequest;\n reviewConfig: ReviewConfig;\n }> => {\n const toolName = toolCall.name;\n const toolArgs = toolCall.args;\n\n // Generate description using the description field (str or callable)\n const descriptionValue = config.description;\n let description: string;\n if (typeof descriptionValue === \"function\") {\n description = await descriptionValue(toolCall, state, runtime);\n } else if (descriptionValue !== undefined) {\n description = descriptionValue;\n } else {\n description = `${\n options.descriptionPrefix ?? \"Tool execution requires approval\"\n }\\n\\nTool: ${toolName}\\nArgs: ${JSON.stringify(toolArgs, null, 2)}`;\n }\n\n /**\n * Create ActionRequest with description\n */\n const actionRequest: ActionRequest = {\n name: toolName,\n args: toolArgs,\n description,\n };\n\n /**\n * Create ReviewConfig\n */\n const reviewConfig: ReviewConfig = {\n actionName: toolName,\n allowedDecisions: config.allowedDecisions,\n };\n\n if (config.argsSchema) {\n reviewConfig.argsSchema = config.argsSchema;\n }\n\n return { actionRequest, reviewConfig };\n };\n\n /**\n * Return `false` if the `when` predicate rejects this tool call, `true` otherwise.\n *\n * When no `when` predicate is configured the tool call always interrupts.\n */\n const shouldInterrupt = async (\n toolCall: ToolCall,\n config: InterruptOnConfig,\n state: AgentBuiltInState,\n runtime: Runtime<unknown>\n ): Promise<boolean> => {\n const { when } = config;\n if (when == null) {\n return true;\n }\n const request: ToolCallRequest<AgentBuiltInState> = {\n toolCall,\n tool: undefined,\n state,\n runtime,\n };\n return when(request);\n };\n\n const processDecision = (\n decision: Decision,\n toolCall: ToolCall,\n config: InterruptOnConfig\n ): { revisedToolCall: ToolCall | null; toolMessage: ToolMessage | null } => {\n const allowedDecisions = config.allowedDecisions;\n if (decision.type === \"approve\" && allowedDecisions.includes(\"approve\")) {\n return { revisedToolCall: toolCall, toolMessage: null };\n }\n\n if (decision.type === \"edit\" && allowedDecisions.includes(\"edit\")) {\n const editedAction = decision.editedAction;\n\n /**\n * Validate edited action structure\n */\n if (!editedAction || typeof editedAction.name !== \"string\") {\n throw new Error(\n `Invalid edited action for tool \"${toolCall.name}\": name must be a string`\n );\n }\n if (!editedAction.args || typeof editedAction.args !== \"object\") {\n throw new Error(\n `Invalid edited action for tool \"${toolCall.name}\": args must be an object`\n );\n }\n\n return {\n revisedToolCall: {\n type: \"tool_call\",\n name: editedAction.name,\n args: editedAction.args,\n id: toolCall.id,\n },\n toolMessage: null,\n };\n }\n\n if (decision.type === \"reject\" && allowedDecisions.includes(\"reject\")) {\n /**\n * Validate that message is a string if provided\n */\n if (\n decision.message !== undefined &&\n typeof decision.message !== \"string\"\n ) {\n throw new Error(\n `Tool call response for \"${\n toolCall.name\n }\" must be a string, got ${typeof decision.message}`\n );\n }\n\n // Create a tool message with the human's text response\n const content =\n decision.message ??\n `User rejected the tool call for \\`${toolCall.name}\\` with id ${toolCall.id}`;\n\n const toolMessage = new ToolMessage({\n content,\n name: toolCall.name,\n tool_call_id: toolCall.id!,\n status: \"error\",\n });\n\n return { revisedToolCall: toolCall, toolMessage };\n }\n\n if (decision.type === \"respond\" && allowedDecisions.includes(\"respond\")) {\n if (typeof decision.message !== \"string\") {\n throw new Error(\n `Tool call response for \"${\n toolCall.name\n }\" must be a string, got ${typeof decision.message}`\n );\n }\n\n const toolMessage = new ToolMessage({\n content: decision.message,\n name: toolCall.name,\n tool_call_id: toolCall.id!,\n status: \"success\",\n });\n\n return { revisedToolCall: toolCall, toolMessage };\n }\n\n const msg = `Unexpected human decision: ${JSON.stringify(\n decision\n )}. Decision type '${decision.type}' is not allowed for tool '${\n toolCall.name\n }'. Expected one of ${JSON.stringify(\n allowedDecisions\n )} based on the tool's configuration.`;\n throw new Error(msg);\n };\n\n const interruptMode = options.interruptMode ?? \"batched\";\n if (interruptMode !== \"batched\" && interruptMode !== \"per_call\") {\n throw new Error(\n `interruptMode must be \"batched\" or \"per_call\", got ${JSON.stringify(interruptMode)}.`\n );\n }\n if (interruptMode === \"per_call\") {\n for (const [toolName, toolConfig] of Object.entries(\n options.interruptOn ?? {}\n )) {\n if (\n typeof toolConfig === \"object\" &&\n !toolConfig.allowedDecisions?.length\n ) {\n throw new Error(\n `Invalid interruptOn config for tool \"${toolName}\": allowedDecisions must list at least one decision.`\n );\n }\n }\n }\n const editNotice =\n options.editNotice === undefined ? DEFAULT_EDIT_NOTICE : options.editNotice;\n\n return createMiddleware({\n name: \"HumanInTheLoopMiddleware\",\n contextSchema,\n /**\n * Per-call mode: pause for each gated tool call as it's about to run.\n */\n wrapToolCall:\n interruptMode === \"per_call\"\n ? async (request, handler) => {\n const { toolCall } = request;\n const { interruptOn } = interopParse(contextSchema, {\n ...options,\n ...(request.runtime.context || {}),\n });\n const toolConfig = resolveInterruptOn(interruptOn)[toolCall.name];\n if (\n !toolConfig ||\n (toolConfig.when && !(await toolConfig.when(request)))\n ) {\n return handler(request);\n }\n // Only a missing ID: an empty one still runs, as in batched mode.\n if (toolCall.id == null) {\n throw new Error(\n `Tool call \\`${toolCall.name}\\` has no ID, so its result can't be matched to it. Make sure the chat model returns tool call IDs.`\n );\n }\n const { actionRequest } = await createActionAndConfig(\n toolCall,\n toolConfig,\n request.state,\n request.runtime\n );\n const value: ToolApprovalRequest = {\n type: \"tool_approval\",\n tool_call_id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n description: actionRequest.description ?? \"\",\n };\n const responseSchema = decisionSchema(\n toolConfig.allowedDecisions,\n toolCall.name,\n request.tool\n );\n // LangGraph parses the answer against `responseSchema` before saving it, so\n // it comes back as one of this tool's allowed decisions.\n const decision = interrupt<\n ToolApprovalRequest,\n ToolApprovalDecision\n >(value, { responseSchema });\n if (decision.type === \"approve\") {\n return handler(request);\n }\n if (decision.type === \"reject\") {\n return new ToolMessage({\n content:\n decision.message ??\n `User rejected the tool call for \\`${toolCall.name}\\` with id ${toolCall.id}`,\n name: toolCall.name,\n tool_call_id: toolCall.id,\n status: \"error\",\n });\n }\n if (decision.type === \"respond\") {\n // The reviewer answers in place of the tool, which doesn't run.\n return new ToolMessage({\n content: decision.message,\n name: toolCall.name,\n tool_call_id: toolCall.id,\n status: \"success\",\n });\n }\n // The schema pinned the tool name, so this is always the same tool.\n const executed = decision.edited_action;\n const result = await handler({\n ...request,\n toolCall: { ...toolCall, args: executed.args },\n });\n return withEditNotice(result, toolCall.id, executed, editNotice);\n }\n : undefined,\n afterModel: {\n canJumpTo: [\"model\"],\n hook: async (state, runtime) => {\n if (interruptMode === \"per_call\") {\n // Interrupts are raised per tool call, in `wrapToolCall`.\n return;\n }\n const config = interopParse(contextSchema, {\n ...options,\n ...(runtime.context || {}),\n });\n if (!config) {\n return;\n }\n\n const { messages } = state;\n if (!messages.length) {\n return;\n }\n\n /**\n * Don't do anything if the last message isn't an AI message with tool calls.\n */\n const lastMessage = [...messages]\n .reverse()\n .find((msg) => AIMessage.isInstance(msg)) as AIMessage;\n if (!lastMessage || !lastMessage.tool_calls?.length) {\n return;\n }\n\n /**\n * If the user omits the interruptOn config, we don't do anything.\n */\n if (!config.interruptOn) {\n return;\n }\n\n /**\n * Resolve per-tool configs (boolean true -> all decisions allowed; false -> auto-approve)\n */\n const resolvedConfigs = resolveInterruptOn(config.interruptOn);\n\n const interruptToolCalls: ToolCall[] = [];\n const autoApprovedToolCalls: ToolCall[] = [];\n\n for (const toolCall of lastMessage.tool_calls) {\n const interruptConfig = resolvedConfigs[toolCall.name];\n /**\n * A tool call is interrupted only when it has a resolved config and its\n * optional `when` predicate doesn't opt it out. Otherwise it is\n * auto-approved.\n */\n if (\n interruptConfig &&\n (await shouldInterrupt(toolCall, interruptConfig, state, runtime))\n ) {\n interruptToolCalls.push(toolCall);\n } else {\n autoApprovedToolCalls.push(toolCall);\n }\n }\n\n /**\n * No interrupt tool calls, so we can just return.\n */\n if (!interruptToolCalls.length) {\n return;\n }\n\n /**\n * Create action requests and review configs for all tools that need approval\n */\n const actionRequests: ActionRequest[] = [];\n const reviewConfigs: ReviewConfig[] = [];\n\n for (const toolCall of interruptToolCalls) {\n const interruptConfig = resolvedConfigs[toolCall.name]!;\n\n /**\n * Create ActionRequest and ReviewConfig using helper method\n */\n const { actionRequest, reviewConfig } = await createActionAndConfig(\n toolCall,\n interruptConfig,\n state,\n runtime\n );\n actionRequests.push(actionRequest);\n reviewConfigs.push(reviewConfig);\n }\n\n /**\n * Create single HITLRequest with all actions and configs\n */\n const hitlRequest: HITLRequest = {\n actionRequests,\n reviewConfigs,\n };\n\n /**\n * Send interrupt and get response\n */\n const hitlResponse = (await interrupt(hitlRequest)) as HITLResponse;\n const decisions = hitlResponse.decisions;\n\n /**\n * Validate that decisions is a valid array before checking length\n */\n if (!decisions || !Array.isArray(decisions)) {\n throw new Error(\n \"Invalid HITLResponse: decisions must be a non-empty array\"\n );\n }\n\n /**\n * Validate that the number of decisions matches the number of interrupt tool calls\n */\n if (decisions.length !== interruptToolCalls.length) {\n throw new Error(\n `Number of human decisions (${decisions.length}) does not match number of hanging tool calls (${interruptToolCalls.length}).`\n );\n }\n\n const revisedToolCalls: ToolCall[] = [...autoApprovedToolCalls];\n const artificialToolMessages: ToolMessage[] = [];\n const hasRejectedToolCalls = decisions.some(\n (decision) => decision.type === \"reject\"\n );\n const hasRespondedToolCalls = decisions.some(\n (decision) => decision.type === \"respond\"\n );\n\n /**\n * Process each decision using helper method\n */\n for (let i = 0; i < decisions.length; i++) {\n const decision = decisions[i]!;\n const toolCall = interruptToolCalls[i]!;\n const interruptConfig = resolvedConfigs[toolCall.name]!;\n\n const { revisedToolCall, toolMessage } = processDecision(\n decision,\n toolCall,\n interruptConfig\n );\n\n if (\n revisedToolCall &&\n /**\n * If any decision is a rejected, we are going back to the model\n * with only the tool calls that were rejected as we don't know\n * the results of the approved/updated tool calls at this point.\n */\n (!hasRejectedToolCalls ||\n decision.type === \"reject\" ||\n decision.type === \"respond\")\n ) {\n revisedToolCalls.push(revisedToolCall);\n }\n if (toolMessage) {\n artificialToolMessages.push(toolMessage);\n }\n }\n\n /**\n * Update the AI message to only include approved tool calls\n */\n if (AIMessage.isInstance(lastMessage)) {\n lastMessage.tool_calls = revisedToolCalls;\n }\n\n const hasPendingToolCalls = revisedToolCalls.some(\n (toolCall) =>\n !artificialToolMessages.some(\n (message) => message.tool_call_id === toolCall.id\n )\n );\n const jumpTo: JumpToTarget | undefined =\n hasRejectedToolCalls ||\n (hasRespondedToolCalls && !hasPendingToolCalls)\n ? \"model\"\n : undefined;\n return {\n messages: [lastMessage, ...artificialToolMessages],\n jumpTo,\n };\n },\n },\n });\n}\n"],"mappings":";;;;;;;AAkBA,MAAM,qBAAqBA,IACxB,SAAS,CAAC,CACV,KAAKA,IAAE,OAA2C,CAAC,CAAC,CACpD,QAAQA,IAAE,MAAM,CAACA,IAAE,QAAQ,GAAGA,IAAE,QAAQA,IAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;AAgCzD,MAAM,4BAA4BA,IAC/B,SAAS,CAAC,CACV,KACCA,IAAE,OAAiB,GACnBA,IAAE,OAA0B,GAC5BA,IAAE,OAAyB,CAC7B,CAAC,CACA,QAAQA,IAAE,MAAM,CAACA,IAAE,OAAO,GAAGA,IAAE,QAAQA,IAAE,OAAO,CAAC,CAAC,CAAC,CAAC;;;;AAwBvD,MAAM,oBAAoB;CAAC;CAAW;CAAQ;CAAU;AAAS;AACjE,MAAM,eAAeA,IAAE,KAAK,iBAAiB;AAG7C,MAAM,0BAA0BA,IAAE,OAAO;;;;CAIvC,kBAAkBA,IAAE,MAAM,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4CtC,aAAaA,IAAE,MAAM,CAACA,IAAE,OAAO,GAAG,yBAAyB,CAAC,CAAC,CAAC,SAAS;;;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KAAK;KACzC,MAAM,WAAW,UAAU;KAC3B,MAAM,WAAW,mBAAmB;KACpC,MAAM,kBAAkB,gBAAgB,SAAS;KAEjD,MAAM,EAAE,iBAAiB,gBAAgB,gBACvC,UACA,UACA,eACF;KAEA,IACE,oBAMC,CAAC,wBACA,SAAS,SAAS,YAClB,SAAS,SAAS,YAEpB,iBAAiB,KAAK,eAAe;KAEvC,IAAI,aACF,uBAAuB,KAAK,WAAW;IAE3C;;;;IAKA,IAAI,UAAU,WAAW,WAAW,GAClC,YAAY,aAAa;IAG3B,MAAM,sBAAsB,iBAAiB,MAC1C,aACC,CAAC,uBAAuB,MACrB,YAAY,QAAQ,iBAAiB,SAAS,EACjD,CACJ;IACA,MAAM,SACJ,wBACC,yBAAyB,CAAC,sBACvB,UACA,KAAA;IACN,OAAO;KACL,UAAU,CAAC,aAAa,GAAG,sBAAsB;KACjD;IACF;GACF;EACF;CACF,CAAC;AACH"}
|
|
@@ -34,6 +34,7 @@ exports.detectMacAddress = require_pii.detectMacAddress;
|
|
|
34
34
|
exports.detectUrl = require_pii.detectUrl;
|
|
35
35
|
exports.dynamicSystemPromptMiddleware = require_dynamicSystemPrompt.dynamicSystemPromptMiddleware;
|
|
36
36
|
exports.humanInTheLoopMiddleware = require_hitl.humanInTheLoopMiddleware;
|
|
37
|
+
exports.isToolApprovalInterrupt = require_hitl.isToolApprovalInterrupt;
|
|
37
38
|
exports.llmToolSelectorMiddleware = require_llmToolSelector.llmToolSelectorMiddleware;
|
|
38
39
|
exports.modelCallLimitMiddleware = require_modelCallLimit.modelCallLimitMiddleware;
|
|
39
40
|
exports.modelFallbackMiddleware = require_modelFallback.modelFallbackMiddleware;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AgentMiddleware } from "./types.cjs";
|
|
2
|
-
import { Action, ActionRequest, ApproveDecision, Decision, DecisionType, DescriptionFactory, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, RejectDecision, RespondDecision, ReviewConfig, WhenPredicate, humanInTheLoopMiddleware } from "./hitl.cjs";
|
|
2
|
+
import { Action, ActionRequest, ApproveDecision, Decision, DecisionType, DescriptionFactory, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, RejectDecision, RespondDecision, ReviewConfig, ToolApprovalDecision, ToolApprovalRequest, WhenPredicate, humanInTheLoopMiddleware, isToolApprovalInterrupt } from "./hitl.cjs";
|
|
3
3
|
import { SummarizationMiddlewareConfig, TokenCounter, summarizationMiddleware } from "./summarization.cjs";
|
|
4
4
|
import { DynamicSystemPromptMiddlewareConfig, dynamicSystemPromptMiddleware } from "./dynamicSystemPrompt.cjs";
|
|
5
5
|
import { LLMToolSelectorConfig, llmToolSelectorMiddleware } from "./llmToolSelector.cjs";
|
|
@@ -19,4 +19,4 @@ import { OpenAIModerationMiddlewareOptions, openAIModerationMiddleware } from ".
|
|
|
19
19
|
import { PromptCachingMiddlewareConfig, anthropicPromptCachingMiddleware } from "./provider/anthropic/promptCaching.cjs";
|
|
20
20
|
import { BedrockConversePromptCachingMiddlewareConfig, bedrockPromptCachingMiddleware } from "./provider/aws/promptCaching.cjs";
|
|
21
21
|
import { countTokensApproximately } from "./utils.cjs";
|
|
22
|
-
export { Action, ActionRequest, ApproveDecision, type BedrockConversePromptCachingMiddlewareConfig, type BuiltInPIIType, ClearToolUsesEdit, type ClearToolUsesEditConfig, type ContextEdit, type ContextEditingMiddlewareConfig, Decision, DecisionType, DescriptionFactory, type DynamicSystemPromptMiddlewareConfig, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, type LLMToolSelectorConfig, type ModelCallLimitMiddlewareConfig, type ModelRetryMiddlewareConfig, type OpenAIModerationMiddlewareOptions, PIIDetectionError, type PIIDetector, type PIIMatch, type PIIMiddlewareConfig, type PIIRedactionMiddlewareConfig, type PIIStrategy, type PromptCachingMiddlewareConfig, type ProviderToolSearchMiddlewareConfig, type RedactionRuleConfig, RejectDecision, type ResolvedRedactionRule, RespondDecision, ReviewConfig, type SummarizationMiddlewareConfig, TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT, type Todo, type TodoListMiddlewareOptions, type TokenCounter, type ToolCallLimitConfig, ToolCallLimitExceededError, type ToolEmulatorOptions, type ToolErrorHandler, type ToolErrorMiddlewareConfig, type ToolRetryMiddlewareConfig, WhenPredicate, anthropicPromptCachingMiddleware, applyStrategy, bedrockPromptCachingMiddleware, contextEditingMiddleware, countTokensApproximately, detectCreditCard, detectEmail, detectIP, detectMacAddress, detectUrl, dynamicSystemPromptMiddleware, humanInTheLoopMiddleware, llmToolSelectorMiddleware, modelCallLimitMiddleware, modelFallbackMiddleware, modelRetryMiddleware, openAIModerationMiddleware, piiMiddleware, piiRedactionMiddleware, providerToolSearchMiddleware, resolveRedactionRule, summarizationMiddleware, todoListMiddleware, toolCallLimitMiddleware, toolEmulatorMiddleware, toolErrorMiddleware, toolRetryMiddleware };
|
|
22
|
+
export { Action, ActionRequest, ApproveDecision, type BedrockConversePromptCachingMiddlewareConfig, type BuiltInPIIType, ClearToolUsesEdit, type ClearToolUsesEditConfig, type ContextEdit, type ContextEditingMiddlewareConfig, Decision, DecisionType, DescriptionFactory, type DynamicSystemPromptMiddlewareConfig, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, type LLMToolSelectorConfig, type ModelCallLimitMiddlewareConfig, type ModelRetryMiddlewareConfig, type OpenAIModerationMiddlewareOptions, PIIDetectionError, type PIIDetector, type PIIMatch, type PIIMiddlewareConfig, type PIIRedactionMiddlewareConfig, type PIIStrategy, type PromptCachingMiddlewareConfig, type ProviderToolSearchMiddlewareConfig, type RedactionRuleConfig, RejectDecision, type ResolvedRedactionRule, RespondDecision, ReviewConfig, type SummarizationMiddlewareConfig, TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT, type Todo, type TodoListMiddlewareOptions, type TokenCounter, ToolApprovalDecision, ToolApprovalRequest, type ToolCallLimitConfig, ToolCallLimitExceededError, type ToolEmulatorOptions, type ToolErrorHandler, type ToolErrorMiddlewareConfig, type ToolRetryMiddlewareConfig, WhenPredicate, anthropicPromptCachingMiddleware, applyStrategy, bedrockPromptCachingMiddleware, contextEditingMiddleware, countTokensApproximately, detectCreditCard, detectEmail, detectIP, detectMacAddress, detectUrl, dynamicSystemPromptMiddleware, humanInTheLoopMiddleware, isToolApprovalInterrupt, llmToolSelectorMiddleware, modelCallLimitMiddleware, modelFallbackMiddleware, modelRetryMiddleware, openAIModerationMiddleware, piiMiddleware, piiRedactionMiddleware, providerToolSearchMiddleware, resolveRedactionRule, summarizationMiddleware, todoListMiddleware, toolCallLimitMiddleware, toolEmulatorMiddleware, toolErrorMiddleware, toolRetryMiddleware };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AgentMiddleware } from "./types.js";
|
|
2
|
-
import { Action, ActionRequest, ApproveDecision, Decision, DecisionType, DescriptionFactory, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, RejectDecision, RespondDecision, ReviewConfig, WhenPredicate, humanInTheLoopMiddleware } from "./hitl.js";
|
|
2
|
+
import { Action, ActionRequest, ApproveDecision, Decision, DecisionType, DescriptionFactory, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, RejectDecision, RespondDecision, ReviewConfig, ToolApprovalDecision, ToolApprovalRequest, WhenPredicate, humanInTheLoopMiddleware, isToolApprovalInterrupt } from "./hitl.js";
|
|
3
3
|
import { SummarizationMiddlewareConfig, TokenCounter, summarizationMiddleware } from "./summarization.js";
|
|
4
4
|
import { DynamicSystemPromptMiddlewareConfig, dynamicSystemPromptMiddleware } from "./dynamicSystemPrompt.js";
|
|
5
5
|
import { LLMToolSelectorConfig, llmToolSelectorMiddleware } from "./llmToolSelector.js";
|
|
@@ -19,4 +19,4 @@ import { OpenAIModerationMiddlewareOptions, openAIModerationMiddleware } from ".
|
|
|
19
19
|
import { PromptCachingMiddlewareConfig, anthropicPromptCachingMiddleware } from "./provider/anthropic/promptCaching.js";
|
|
20
20
|
import { BedrockConversePromptCachingMiddlewareConfig, bedrockPromptCachingMiddleware } from "./provider/aws/promptCaching.js";
|
|
21
21
|
import { countTokensApproximately } from "./utils.js";
|
|
22
|
-
export { Action, ActionRequest, ApproveDecision, type BedrockConversePromptCachingMiddlewareConfig, type BuiltInPIIType, ClearToolUsesEdit, type ClearToolUsesEditConfig, type ContextEdit, type ContextEditingMiddlewareConfig, Decision, DecisionType, DescriptionFactory, type DynamicSystemPromptMiddlewareConfig, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, type LLMToolSelectorConfig, type ModelCallLimitMiddlewareConfig, type ModelRetryMiddlewareConfig, type OpenAIModerationMiddlewareOptions, PIIDetectionError, type PIIDetector, type PIIMatch, type PIIMiddlewareConfig, type PIIRedactionMiddlewareConfig, type PIIStrategy, type PromptCachingMiddlewareConfig, type ProviderToolSearchMiddlewareConfig, type RedactionRuleConfig, RejectDecision, type ResolvedRedactionRule, RespondDecision, ReviewConfig, type SummarizationMiddlewareConfig, TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT, type Todo, type TodoListMiddlewareOptions, type TokenCounter, type ToolCallLimitConfig, ToolCallLimitExceededError, type ToolEmulatorOptions, type ToolErrorHandler, type ToolErrorMiddlewareConfig, type ToolRetryMiddlewareConfig, WhenPredicate, anthropicPromptCachingMiddleware, applyStrategy, bedrockPromptCachingMiddleware, contextEditingMiddleware, countTokensApproximately, detectCreditCard, detectEmail, detectIP, detectMacAddress, detectUrl, dynamicSystemPromptMiddleware, humanInTheLoopMiddleware, llmToolSelectorMiddleware, modelCallLimitMiddleware, modelFallbackMiddleware, modelRetryMiddleware, openAIModerationMiddleware, piiMiddleware, piiRedactionMiddleware, providerToolSearchMiddleware, resolveRedactionRule, summarizationMiddleware, todoListMiddleware, toolCallLimitMiddleware, toolEmulatorMiddleware, toolErrorMiddleware, toolRetryMiddleware };
|
|
22
|
+
export { Action, ActionRequest, ApproveDecision, type BedrockConversePromptCachingMiddlewareConfig, type BuiltInPIIType, ClearToolUsesEdit, type ClearToolUsesEditConfig, type ContextEdit, type ContextEditingMiddlewareConfig, Decision, DecisionType, DescriptionFactory, type DynamicSystemPromptMiddlewareConfig, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, type LLMToolSelectorConfig, type ModelCallLimitMiddlewareConfig, type ModelRetryMiddlewareConfig, type OpenAIModerationMiddlewareOptions, PIIDetectionError, type PIIDetector, type PIIMatch, type PIIMiddlewareConfig, type PIIRedactionMiddlewareConfig, type PIIStrategy, type PromptCachingMiddlewareConfig, type ProviderToolSearchMiddlewareConfig, type RedactionRuleConfig, RejectDecision, type ResolvedRedactionRule, RespondDecision, ReviewConfig, type SummarizationMiddlewareConfig, TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT, type Todo, type TodoListMiddlewareOptions, type TokenCounter, ToolApprovalDecision, ToolApprovalRequest, type ToolCallLimitConfig, ToolCallLimitExceededError, type ToolEmulatorOptions, type ToolErrorHandler, type ToolErrorMiddlewareConfig, type ToolRetryMiddlewareConfig, WhenPredicate, anthropicPromptCachingMiddleware, applyStrategy, bedrockPromptCachingMiddleware, contextEditingMiddleware, countTokensApproximately, detectCreditCard, detectEmail, detectIP, detectMacAddress, detectUrl, dynamicSystemPromptMiddleware, humanInTheLoopMiddleware, isToolApprovalInterrupt, llmToolSelectorMiddleware, modelCallLimitMiddleware, modelFallbackMiddleware, modelRetryMiddleware, openAIModerationMiddleware, piiMiddleware, piiRedactionMiddleware, providerToolSearchMiddleware, resolveRedactionRule, summarizationMiddleware, todoListMiddleware, toolCallLimitMiddleware, toolEmulatorMiddleware, toolErrorMiddleware, toolRetryMiddleware };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { countTokensApproximately } from "./utils.js";
|
|
2
|
-
import { humanInTheLoopMiddleware } from "./hitl.js";
|
|
2
|
+
import { humanInTheLoopMiddleware, isToolApprovalInterrupt } from "./hitl.js";
|
|
3
3
|
import { summarizationMiddleware } from "./summarization.js";
|
|
4
4
|
import { dynamicSystemPromptMiddleware } from "./dynamicSystemPrompt.js";
|
|
5
5
|
import { llmToolSelectorMiddleware } from "./llmToolSelector.js";
|
|
@@ -18,4 +18,4 @@ import { providerToolSearchMiddleware } from "./providerToolSearch.js";
|
|
|
18
18
|
import { openAIModerationMiddleware } from "./provider/openai/moderation.js";
|
|
19
19
|
import { anthropicPromptCachingMiddleware } from "./provider/anthropic/promptCaching.js";
|
|
20
20
|
import { bedrockPromptCachingMiddleware } from "./provider/aws/promptCaching.js";
|
|
21
|
-
export { ClearToolUsesEdit, PIIDetectionError, TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT, ToolCallLimitExceededError, anthropicPromptCachingMiddleware, applyStrategy, bedrockPromptCachingMiddleware, contextEditingMiddleware, countTokensApproximately, detectCreditCard, detectEmail, detectIP, detectMacAddress, detectUrl, dynamicSystemPromptMiddleware, humanInTheLoopMiddleware, llmToolSelectorMiddleware, modelCallLimitMiddleware, modelFallbackMiddleware, modelRetryMiddleware, openAIModerationMiddleware, piiMiddleware, piiRedactionMiddleware, providerToolSearchMiddleware, resolveRedactionRule, summarizationMiddleware, todoListMiddleware, toolCallLimitMiddleware, toolEmulatorMiddleware, toolErrorMiddleware, toolRetryMiddleware };
|
|
21
|
+
export { ClearToolUsesEdit, PIIDetectionError, TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT, ToolCallLimitExceededError, anthropicPromptCachingMiddleware, applyStrategy, bedrockPromptCachingMiddleware, contextEditingMiddleware, countTokensApproximately, detectCreditCard, detectEmail, detectIP, detectMacAddress, detectUrl, dynamicSystemPromptMiddleware, humanInTheLoopMiddleware, isToolApprovalInterrupt, llmToolSelectorMiddleware, modelCallLimitMiddleware, modelFallbackMiddleware, modelRetryMiddleware, openAIModerationMiddleware, piiMiddleware, piiRedactionMiddleware, providerToolSearchMiddleware, resolveRedactionRule, summarizationMiddleware, todoListMiddleware, toolCallLimitMiddleware, toolEmulatorMiddleware, toolErrorMiddleware, toolRetryMiddleware };
|