langchain 1.4.5-dev-1781048185730 → 1.4.6-dev-1781485641139

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.
Files changed (166) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/agents/ReactAgent.cjs +5 -2
  3. package/dist/agents/ReactAgent.cjs.map +1 -1
  4. package/dist/agents/ReactAgent.d.cts +1 -1
  5. package/dist/agents/ReactAgent.d.cts.map +1 -1
  6. package/dist/agents/ReactAgent.d.ts +1 -1
  7. package/dist/agents/ReactAgent.d.ts.map +1 -1
  8. package/dist/agents/ReactAgent.js +4 -1
  9. package/dist/agents/ReactAgent.js.map +1 -1
  10. package/dist/agents/index.cjs +3 -1
  11. package/dist/agents/index.cjs.map +1 -1
  12. package/dist/agents/index.d.cts +3 -1
  13. package/dist/agents/index.d.cts.map +1 -1
  14. package/dist/agents/index.d.ts +3 -1
  15. package/dist/agents/index.d.ts.map +1 -1
  16. package/dist/agents/index.js +3 -1
  17. package/dist/agents/index.js.map +1 -1
  18. package/dist/agents/middleware/constants.cjs +25 -0
  19. package/dist/agents/middleware/constants.cjs.map +1 -1
  20. package/dist/agents/middleware/constants.js +25 -0
  21. package/dist/agents/middleware/constants.js.map +1 -1
  22. package/dist/agents/middleware/contextEditing.cjs.map +1 -1
  23. package/dist/agents/middleware/contextEditing.d.cts.map +1 -1
  24. package/dist/agents/middleware/contextEditing.d.ts.map +1 -1
  25. package/dist/agents/middleware/contextEditing.js.map +1 -1
  26. package/dist/agents/middleware/hitl.cjs +68 -29
  27. package/dist/agents/middleware/hitl.cjs.map +1 -1
  28. package/dist/agents/middleware/hitl.d.cts +2 -105
  29. package/dist/agents/middleware/hitl.d.cts.map +1 -1
  30. package/dist/agents/middleware/hitl.d.ts +2 -105
  31. package/dist/agents/middleware/hitl.d.ts.map +1 -1
  32. package/dist/agents/middleware/hitl.js +68 -29
  33. package/dist/agents/middleware/hitl.js.map +1 -1
  34. package/dist/agents/middleware/index.cjs +1 -0
  35. package/dist/agents/middleware/index.d.cts +2 -1
  36. package/dist/agents/middleware/index.d.ts +2 -1
  37. package/dist/agents/middleware/index.js +1 -0
  38. package/dist/agents/middleware/llmToolSelector.cjs +14 -0
  39. package/dist/agents/middleware/llmToolSelector.cjs.map +1 -1
  40. package/dist/agents/middleware/llmToolSelector.js +14 -0
  41. package/dist/agents/middleware/llmToolSelector.js.map +1 -1
  42. package/dist/agents/middleware/modelCallLimit.cjs +12 -0
  43. package/dist/agents/middleware/modelCallLimit.cjs.map +1 -1
  44. package/dist/agents/middleware/modelCallLimit.js +12 -0
  45. package/dist/agents/middleware/modelCallLimit.js.map +1 -1
  46. package/dist/agents/middleware/modelRetry.cjs +10 -1
  47. package/dist/agents/middleware/modelRetry.cjs.map +1 -1
  48. package/dist/agents/middleware/modelRetry.js +10 -1
  49. package/dist/agents/middleware/modelRetry.js.map +1 -1
  50. package/dist/agents/middleware/pii.cjs +9 -0
  51. package/dist/agents/middleware/pii.cjs.map +1 -1
  52. package/dist/agents/middleware/pii.js +9 -0
  53. package/dist/agents/middleware/pii.js.map +1 -1
  54. package/dist/agents/middleware/piiRedaction.cjs +6 -1
  55. package/dist/agents/middleware/piiRedaction.cjs.map +1 -1
  56. package/dist/agents/middleware/piiRedaction.js +6 -1
  57. package/dist/agents/middleware/piiRedaction.js.map +1 -1
  58. package/dist/agents/middleware/provider/anthropic/promptCaching.cjs +19 -0
  59. package/dist/agents/middleware/provider/anthropic/promptCaching.cjs.map +1 -1
  60. package/dist/agents/middleware/provider/anthropic/promptCaching.js +19 -0
  61. package/dist/agents/middleware/provider/anthropic/promptCaching.js.map +1 -1
  62. package/dist/agents/middleware/provider/openai/moderation.cjs.map +1 -1
  63. package/dist/agents/middleware/provider/openai/moderation.js.map +1 -1
  64. package/dist/agents/middleware/providerToolSearch.cjs +130 -0
  65. package/dist/agents/middleware/providerToolSearch.cjs.map +1 -0
  66. package/dist/agents/middleware/providerToolSearch.d.cts +75 -0
  67. package/dist/agents/middleware/providerToolSearch.d.cts.map +1 -0
  68. package/dist/agents/middleware/providerToolSearch.d.ts +75 -0
  69. package/dist/agents/middleware/providerToolSearch.d.ts.map +1 -0
  70. package/dist/agents/middleware/providerToolSearch.js +129 -0
  71. package/dist/agents/middleware/providerToolSearch.js.map +1 -0
  72. package/dist/agents/middleware/summarization.cjs +62 -1
  73. package/dist/agents/middleware/summarization.cjs.map +1 -1
  74. package/dist/agents/middleware/summarization.js +62 -1
  75. package/dist/agents/middleware/summarization.js.map +1 -1
  76. package/dist/agents/middleware/todoListMiddleware.cjs.map +1 -1
  77. package/dist/agents/middleware/todoListMiddleware.js.map +1 -1
  78. package/dist/agents/middleware/toolCallLimit.cjs +24 -0
  79. package/dist/agents/middleware/toolCallLimit.cjs.map +1 -1
  80. package/dist/agents/middleware/toolCallLimit.js +24 -0
  81. package/dist/agents/middleware/toolCallLimit.js.map +1 -1
  82. package/dist/agents/middleware/toolEmulator.cjs.map +1 -1
  83. package/dist/agents/middleware/toolEmulator.js.map +1 -1
  84. package/dist/agents/middleware/toolRetry.cjs +17 -0
  85. package/dist/agents/middleware/toolRetry.cjs.map +1 -1
  86. package/dist/agents/middleware/toolRetry.js +17 -0
  87. package/dist/agents/middleware/toolRetry.js.map +1 -1
  88. package/dist/agents/middleware.cjs.map +1 -1
  89. package/dist/agents/middleware.js.map +1 -1
  90. package/dist/agents/nodes/AfterAgentNode.cjs.map +1 -1
  91. package/dist/agents/nodes/AfterAgentNode.js.map +1 -1
  92. package/dist/agents/nodes/AfterModelNode.cjs.map +1 -1
  93. package/dist/agents/nodes/AfterModelNode.js.map +1 -1
  94. package/dist/agents/nodes/AgentNode.cjs +17 -0
  95. package/dist/agents/nodes/AgentNode.cjs.map +1 -1
  96. package/dist/agents/nodes/AgentNode.js +17 -0
  97. package/dist/agents/nodes/AgentNode.js.map +1 -1
  98. package/dist/agents/nodes/BeforeAgentNode.cjs.map +1 -1
  99. package/dist/agents/nodes/BeforeAgentNode.js.map +1 -1
  100. package/dist/agents/nodes/BeforeModelNode.cjs.map +1 -1
  101. package/dist/agents/nodes/BeforeModelNode.js.map +1 -1
  102. package/dist/agents/nodes/ToolNode.cjs +3 -0
  103. package/dist/agents/nodes/ToolNode.cjs.map +1 -1
  104. package/dist/agents/nodes/ToolNode.js +3 -0
  105. package/dist/agents/nodes/ToolNode.js.map +1 -1
  106. package/dist/agents/nodes/middleware.cjs +3 -0
  107. package/dist/agents/nodes/middleware.cjs.map +1 -1
  108. package/dist/agents/nodes/middleware.js +3 -0
  109. package/dist/agents/nodes/middleware.js.map +1 -1
  110. package/dist/agents/nodes/utils.cjs.map +1 -1
  111. package/dist/agents/nodes/utils.js.map +1 -1
  112. package/dist/agents/responses.cjs.map +1 -1
  113. package/dist/agents/responses.js.map +1 -1
  114. package/dist/agents/tests/utils.cjs.map +1 -1
  115. package/dist/agents/tests/utils.js.map +1 -1
  116. package/dist/agents/transformers/index.cjs +2 -0
  117. package/dist/agents/transformers/index.d.cts +3 -0
  118. package/dist/agents/transformers/index.d.ts +3 -0
  119. package/dist/agents/transformers/index.js +3 -0
  120. package/dist/agents/transformers/subagent.cjs +205 -0
  121. package/dist/agents/transformers/subagent.cjs.map +1 -0
  122. package/dist/agents/transformers/subagent.d.cts +34 -0
  123. package/dist/agents/transformers/subagent.d.cts.map +1 -0
  124. package/dist/agents/transformers/subagent.d.ts +34 -0
  125. package/dist/agents/transformers/subagent.d.ts.map +1 -0
  126. package/dist/agents/transformers/subagent.js +204 -0
  127. package/dist/agents/transformers/subagent.js.map +1 -0
  128. package/dist/agents/{stream.cjs → transformers/tool-call.cjs} +3 -15
  129. package/dist/agents/transformers/tool-call.cjs.map +1 -0
  130. package/dist/agents/transformers/tool-call.d.cts +17 -0
  131. package/dist/agents/transformers/tool-call.d.cts.map +1 -0
  132. package/dist/agents/transformers/tool-call.d.ts +17 -0
  133. package/dist/agents/transformers/tool-call.d.ts.map +1 -0
  134. package/dist/agents/{stream.js → transformers/tool-call.js} +2 -14
  135. package/dist/agents/transformers/tool-call.js.map +1 -0
  136. package/dist/agents/{stream.d.cts → transformers/types.d.cts} +50 -20
  137. package/dist/agents/transformers/types.d.cts.map +1 -0
  138. package/dist/agents/{stream.d.ts → transformers/types.d.ts} +50 -20
  139. package/dist/agents/transformers/types.d.ts.map +1 -0
  140. package/dist/agents/utils.cjs +3 -0
  141. package/dist/agents/utils.cjs.map +1 -1
  142. package/dist/agents/utils.js +3 -0
  143. package/dist/agents/utils.js.map +1 -1
  144. package/dist/browser.cjs +9 -3
  145. package/dist/browser.d.cts +6 -3
  146. package/dist/browser.d.ts +6 -3
  147. package/dist/browser.js +6 -2
  148. package/dist/chat_models/universal.cjs.map +1 -1
  149. package/dist/chat_models/universal.js.map +1 -1
  150. package/dist/hub/index.cjs.map +1 -1
  151. package/dist/hub/index.js.map +1 -1
  152. package/dist/hub/node.cjs.map +1 -1
  153. package/dist/hub/node.js.map +1 -1
  154. package/dist/index.cjs +9 -3
  155. package/dist/index.d.cts +6 -3
  156. package/dist/index.d.ts +6 -3
  157. package/dist/index.js +6 -2
  158. package/dist/storage/encoder_backed.cjs.map +1 -1
  159. package/dist/storage/encoder_backed.js.map +1 -1
  160. package/dist/storage/file_system.cjs.map +1 -1
  161. package/dist/storage/file_system.js.map +1 -1
  162. package/package.json +13 -13
  163. package/dist/agents/stream.cjs.map +0 -1
  164. package/dist/agents/stream.d.cts.map +0 -1
  165. package/dist/agents/stream.d.ts.map +0 -1
  166. package/dist/agents/stream.js.map +0 -1
@@ -5,7 +5,6 @@ let _langchain_langgraph = require("@langchain/langgraph");
5
5
  let _langchain_core_utils_types = require("@langchain/core/utils/types");
6
6
  let zod_v3 = require("zod/v3");
7
7
  //#region src/agents/middleware/hitl.ts
8
- const WhenFunctionSchema = zod_v3.z.function().args(zod_v3.z.custom()).returns(zod_v3.z.union([zod_v3.z.boolean(), zod_v3.z.promise(zod_v3.z.boolean())]));
9
8
  const DescriptionFunctionSchema = zod_v3.z.function().args(zod_v3.z.custom(), zod_v3.z.custom(), zod_v3.z.custom()).returns(zod_v3.z.union([zod_v3.z.string(), zod_v3.z.promise(zod_v3.z.string())]));
10
9
  /**
11
10
  * The type of decision a human can make.
@@ -17,13 +16,77 @@ const ALLOWED_DECISIONS = [
17
16
  ];
18
17
  const DecisionType = zod_v3.z.enum(ALLOWED_DECISIONS);
19
18
  const InterruptOnConfigSchema = zod_v3.z.object({
19
+ /**
20
+ * The decisions that are allowed for this action.
21
+ */
20
22
  allowedDecisions: zod_v3.z.array(DecisionType),
23
+ /**
24
+ * The description attached to the request for human input.
25
+ * Can be either:
26
+ * - A static string describing the approval request
27
+ * - A callable that dynamically generates the description based on agent state,
28
+ * runtime, and tool call information
29
+ *
30
+ * @example
31
+ * Static string description
32
+ * ```typescript
33
+ * import type { InterruptOnConfig } from "langchain";
34
+ *
35
+ * const config: InterruptOnConfig = {
36
+ * allowedDecisions: ["approve", "reject"],
37
+ * description: "Please review this tool execution"
38
+ * };
39
+ * ```
40
+ *
41
+ * @example
42
+ * Dynamic callable description
43
+ * ```typescript
44
+ * import type {
45
+ * AgentBuiltInState,
46
+ * Runtime,
47
+ * DescriptionFactory,
48
+ * ToolCall,
49
+ * InterruptOnConfig
50
+ * } from "langchain";
51
+ *
52
+ * const formatToolDescription: DescriptionFactory = (
53
+ * toolCall: ToolCall,
54
+ * state: AgentBuiltInState,
55
+ * runtime: Runtime<unknown>
56
+ * ) => {
57
+ * return `Tool: ${toolCall.name}\nArguments:\n${JSON.stringify(toolCall.args, null, 2)}`;
58
+ * };
59
+ *
60
+ * const config: InterruptOnConfig = {
61
+ * allowedDecisions: ["approve", "edit"],
62
+ * description: formatToolDescription
63
+ * };
64
+ * ```
65
+ */
21
66
  description: zod_v3.z.union([zod_v3.z.string(), DescriptionFunctionSchema]).optional(),
22
- argsSchema: zod_v3.z.record(zod_v3.z.any()).optional(),
23
- when: WhenFunctionSchema.optional()
67
+ /**
68
+ * JSON schema for the arguments associated with the action, if edits are allowed.
69
+ */
70
+ argsSchema: zod_v3.z.record(zod_v3.z.any()).optional()
24
71
  });
25
72
  const contextSchema = zod_v3.z.object({
73
+ /**
74
+ * Mapping of tool name to allowed reviewer responses.
75
+ * If a tool doesn't have an entry, it's auto-approved by default.
76
+ *
77
+ * - `true` -> pause for approval and allow approve/edit/reject decisions
78
+ * - `false` -> auto-approve (no human review)
79
+ * - `InterruptOnConfig` -> explicitly specify which decisions are allowed for this tool
80
+ */
26
81
  interruptOn: zod_v3.z.record(zod_v3.z.union([zod_v3.z.boolean(), InterruptOnConfigSchema])).optional(),
82
+ /**
83
+ * Prefix used when constructing human-facing approval messages.
84
+ * Provides context about the tool call being reviewed; does not change the underlying action.
85
+ *
86
+ * Note: This prefix is only applied for tools that do not provide a custom
87
+ * `description` via their {@link InterruptOnConfig}. If a tool specifies a custom
88
+ * `description`, that per-tool text is used and this prefix is ignored.
89
+ */
27
90
  descriptionPrefix: zod_v3.z.string().default("Tool execution requires approval")
28
91
  });
29
92
  /**
@@ -85,7 +148,6 @@ const contextSchema = zod_v3.z.object({
85
148
  * @param options.interruptOn[toolName].allowedDecisions - Array of decision types allowed for this tool (e.g., ["approve", "edit", "reject"])
86
149
  * @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
87
150
  * @param options.interruptOn[toolName].argsSchema - JSON schema for the arguments associated with the action, if edits are allowed
88
- * @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.
89
151
  * @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.
90
152
  *
91
153
  * @returns A middleware instance that can be passed to `createAgent`
@@ -266,21 +328,6 @@ function humanInTheLoopMiddleware(options) {
266
328
  reviewConfig
267
329
  };
268
330
  };
269
- /**
270
- * Return `false` if the `when` predicate rejects this tool call, `true` otherwise.
271
- *
272
- * When no `when` predicate is configured the tool call always interrupts.
273
- */
274
- const shouldInterrupt = async (toolCall, config, state, runtime) => {
275
- const { when } = config;
276
- if (when == null) return true;
277
- return when({
278
- toolCall,
279
- tool: void 0,
280
- state,
281
- runtime
282
- });
283
- };
284
331
  const processDecision = (decision, toolCall, config) => {
285
332
  const allowedDecisions = config.allowedDecisions;
286
333
  if (decision.type === "approve" && allowedDecisions.includes("approve")) return {
@@ -353,16 +400,8 @@ function humanInTheLoopMiddleware(options) {
353
400
  } else if (toolConfig.allowedDecisions) resolvedConfigs[toolName] = toolConfig;
354
401
  const interruptToolCalls = [];
355
402
  const autoApprovedToolCalls = [];
356
- for (const toolCall of lastMessage.tool_calls) {
357
- const interruptConfig = resolvedConfigs[toolCall.name];
358
- /**
359
- * A tool call is interrupted only when it has a resolved config and its
360
- * optional `when` predicate doesn't opt it out. Otherwise it is
361
- * auto-approved.
362
- */
363
- if (interruptConfig && await shouldInterrupt(toolCall, interruptConfig, state, runtime)) interruptToolCalls.push(toolCall);
364
- else autoApprovedToolCalls.push(toolCall);
365
- }
403
+ for (const toolCall of lastMessage.tool_calls) if (toolCall.name in resolvedConfigs) interruptToolCalls.push(toolCall);
404
+ else autoApprovedToolCalls.push(toolCall);
366
405
  /**
367
406
  * No interrupt tool calls, so we can just return.
368
407
  */
@@ -1 +1 @@
1
- {"version":3,"file":"hitl.cjs","names":["z","ToolMessage","createMiddleware","AIMessage"],"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\"] 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 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 * Union of all possible decision types.\n */\nexport type Decision = ApproveDecision | EditDecision | RejectDecision;\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 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: [...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\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 || decision.type === \"reject\")\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 jumpTo: JumpToTarget | undefined = hasRejectedToolCalls\n ? \"model\"\n : undefined;\n return {\n messages: [lastMessage, ...artificialToolMessages],\n jumpTo,\n };\n },\n },\n });\n}\n"],"mappings":";;;;;;;AAcA,MAAM,qBAAqBA,OAAAA,EACxB,UAAU,CACV,KAAKA,OAAAA,EAAE,QAA4C,CAAC,CACpD,QAAQA,OAAAA,EAAE,MAAM,CAACA,OAAAA,EAAE,SAAS,EAAEA,OAAAA,EAAE,QAAQA,OAAAA,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;AA2B1D,MAAM,4BAA4BA,OAAAA,EAC/B,UAAU,CACV,KACCA,OAAAA,EAAE,QAAkB,EACpBA,OAAAA,EAAE,QAA2B,EAC7BA,OAAAA,EAAE,QAA0B,CAC7B,CACA,QAAQA,OAAAA,EAAE,MAAM,CAACA,OAAAA,EAAE,QAAQ,EAAEA,OAAAA,EAAE,QAAQA,OAAAA,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;;;;AAwBxD,MAAM,oBAAoB;CAAC;CAAW;CAAQ;CAAS;AACvD,MAAM,eAAeA,OAAAA,EAAE,KAAK,kBAAkB;AAG9C,MAAM,0BAA0BA,OAAAA,EAAE,OAAO;CAIvC,kBAAkBA,OAAAA,EAAE,MAAM,aAAa;CA4CvC,aAAaA,OAAAA,EAAE,MAAM,CAACA,OAAAA,EAAE,QAAQ,EAAE,0BAA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ACA;KACD,CAKiD,EACnB;;;;AAK/B,QAAI,CAAC,aAAa,CAAC,MAAM,QAAQ,UAAU,CACzC,OAAM,IAAI,MACR,4DACD;;;;AAMH,QAAI,UAAU,WAAW,mBAAmB,OAC1C,OAAM,IAAI,MACR,8BAA8B,UAAU,OAAO,iDAAiD,mBAAmB,OAAO,IAC3H;IAGH,MAAM,mBAA+B,CAAC,GAAG,sBAAsB;IAC/D,MAAM,yBAAwC,EAAE;IAChD,MAAM,uBAAuB,UAAU,MACpC,aAAa,SAAS,SAAS,SACjC;;;;AAKD,SAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;KACzC,MAAM,WAAW,UAAU;KAC3B,MAAM,WAAW,mBAAmB;KACpC,MAAM,kBAAkB,gBAAgB,SAAS;KAEjD,MAAM,EAAE,iBAAiB,gBAAgB,gBACvC,UACA,UACA,gBACD;AAED,SACE,oBAMC,CAAC,wBAAwB,SAAS,SAAS,UAE5C,kBAAiB,KAAK,gBAAgB;AAExC,SAAI,YACF,wBAAuB,KAAK,YAAY;;;;;AAO5C,QAAIA,yBAAAA,UAAU,WAAW,YAAY,CACnC,aAAY,aAAa;IAG3B,MAAM,SAAmC,uBACrC,UACA,KAAA;AACJ,WAAO;KACL,UAAU,CAAC,aAAa,GAAG,uBAAuB;KAClD;KACD;;GAEJ;EACF,CAAC"}
1
+ {"version":3,"file":"hitl.cjs","names":["z","ToolMessage","createMiddleware","AIMessage"],"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\";\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\"] 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 argsSchema: z.record(z.any()).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 * Union of all possible decision types.\n */\nexport type Decision = ApproveDecision | EditDecision | RejectDecision;\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.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 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 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: [...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 if (toolCall.name in resolvedConfigs) {\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\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 || decision.type === \"reject\")\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 jumpTo: JumpToTarget | undefined = hasRejectedToolCalls\n ? \"model\"\n : undefined;\n return {\n messages: [lastMessage, ...artificialToolMessages],\n jumpTo,\n };\n },\n },\n });\n}\n"],"mappings":";;;;;;;AAaA,MAAM,4BAA4BA,OAAAA,EAC/B,UAAU,CACV,KACCA,OAAAA,EAAE,QAAkB,EACpBA,OAAAA,EAAE,QAA2B,EAC7BA,OAAAA,EAAE,QAA0B,CAC7B,CACA,QAAQA,OAAAA,EAAE,MAAM,CAACA,OAAAA,EAAE,QAAQ,EAAEA,OAAAA,EAAE,QAAQA,OAAAA,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;;;;AAwBxD,MAAM,oBAAoB;CAAC;CAAW;CAAQ;CAAS;AACvD,MAAM,eAAeA,OAAAA,EAAE,KAAK,kBAAkB;AAG9C,MAAM,0BAA0BA,OAAAA,EAAE,OAAO;;;;CAIvC,kBAAkBA,OAAAA,EAAE,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4CvC,aAAaA,OAAAA,EAAE,MAAM,CAACA,OAAAA,EAAE,QAAQ,EAAE,0BAA0B,CAAC,CAAC,UAAU;;;;CAIxE,YAAYA,OAAAA,EAAE,OAAOA,OAAAA,EAAE,KAAK,CAAC,CAAC,UAAU;CACzC,CAAC;AAiIF,MAAM,gBAAgBA,OAAAA,EAAE,OAAO;;;;;;;;;CAS7B,aAAaA,OAAAA,EACV,OAAOA,OAAAA,EAAE,MAAM,CAACA,OAAAA,EAAE,SAAS,EAAE,wBAAwB,CAAC,CAAC,CACvD,UAAU;;;;;;;;;CASb,mBAAmBA,OAAAA,EAAE,QAAQ,CAAC,QAAQ,mCAAmC;CAC1E,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AIA,yBAAAA,UAAU,WAAW,YAAY,CACnC,aAAY,aAAa;IAG3B,MAAM,SAAmC,uBACrC,UACA,KAAA;AACJ,WAAO;KACL,UAAU,CAAC,aAAa,GAAG,uBAAuB;KAClD;KACD;;GAEJ;EACF,CAAC"}
@@ -1,36 +1,11 @@
1
1
  import { AgentBuiltInState, Runtime } from "../runtime.cjs";
2
- import { AgentMiddleware, ToolCallRequest } from "./types.cjs";
2
+ import { AgentMiddleware } from "./types.cjs";
3
3
  import { ToolCall } from "@langchain/core/messages";
4
4
  import * as _$_langchain_core_tools0 from "@langchain/core/tools";
5
5
  import { InferInteropZodInput } from "@langchain/core/utils/types";
6
6
  import { z } from "zod/v3";
7
7
 
8
8
  //#region src/agents/middleware/hitl.d.ts
9
- declare const WhenFunctionSchema: z.ZodFunction<z.ZodTuple<[z.ZodType<ToolCallRequest<AgentBuiltInState, unknown>, z.ZodTypeDef, ToolCallRequest<AgentBuiltInState, unknown>>], z.ZodUnknown>, z.ZodUnion<[z.ZodBoolean, z.ZodPromise<z.ZodBoolean>]>>;
10
- /**
11
- * Predicate controlling whether a given tool call triggers an interrupt.
12
- *
13
- * Receives a {@link ToolCallRequest} and returns `true` to interrupt or `false`
14
- * to auto-approve the tool call.
15
- *
16
- * The request is constructed with `tool` set to `undefined` and `runtime` set to
17
- * the node-level {@link Runtime}, so it reflects the batch (`afterModel`) context
18
- * rather than a per-call tool execution.
19
- *
20
- * @param request - The tool call request being evaluated
21
- * @returns `true` to interrupt for the tool call, `false` to auto-approve it.
22
- * May also return a promise resolving to a boolean.
23
- *
24
- * @example
25
- * ```typescript
26
- * import { type WhenPredicate } from "langchain";
27
- *
28
- * // Only interrupt delete_file calls targeting /etc
29
- * const when: WhenPredicate = (request) =>
30
- * String(request.toolCall.args.path ?? "").startsWith("/etc");
31
- * ```
32
- */
33
- type WhenPredicate = z.infer<typeof WhenFunctionSchema>;
34
9
  declare const DescriptionFunctionSchema: z.ZodFunction<z.ZodTuple<[z.ZodType<ToolCall<string, Record<string, any>>, z.ZodTypeDef, ToolCall<string, Record<string, any>>>, z.ZodType<AgentBuiltInState, z.ZodTypeDef, AgentBuiltInState>, z.ZodType<Runtime<unknown>, z.ZodTypeDef, Runtime<unknown>>], z.ZodUnknown>, z.ZodUnion<[z.ZodString, z.ZodPromise<z.ZodString>]>>;
35
10
  /**
36
11
  * Function type that dynamically generates a description for a tool call approval request.
@@ -105,38 +80,14 @@ declare const InterruptOnConfigSchema: z.ZodObject<{
105
80
  * JSON schema for the arguments associated with the action, if edits are allowed.
106
81
  */
107
82
  argsSchema: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
108
- /**
109
- * Optional predicate controlling whether to interrupt for a given tool call.
110
- *
111
- * Receives a {@link ToolCallRequest} and returns `true` to interrupt or
112
- * `false` to auto-approve the tool call.
113
- *
114
- * The request is constructed with `tool` set to `undefined` and `runtime` set
115
- * to the node-level {@link Runtime}, so `request.tool` is not available.
116
- *
117
- * @example
118
- * ```typescript
119
- * import type { InterruptOnConfig } from "langchain";
120
- *
121
- * // Only interrupt delete_file calls targeting /etc
122
- * const config: InterruptOnConfig = {
123
- * allowedDecisions: ["approve", "reject"],
124
- * when: (request) =>
125
- * String(request.toolCall.args.path ?? "").startsWith("/etc"),
126
- * };
127
- * ```
128
- */
129
- when: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodType<ToolCallRequest<AgentBuiltInState, unknown>, z.ZodTypeDef, ToolCallRequest<AgentBuiltInState, unknown>>], z.ZodUnknown>, z.ZodUnion<[z.ZodBoolean, z.ZodPromise<z.ZodBoolean>]>>>;
130
83
  }, "strip", z.ZodTypeAny, {
131
84
  allowedDecisions: ("approve" | "edit" | "reject")[];
132
85
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
133
86
  argsSchema?: Record<string, any> | undefined;
134
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
135
87
  }, {
136
88
  allowedDecisions: ("approve" | "edit" | "reject")[];
137
89
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
138
90
  argsSchema?: Record<string, any> | undefined;
139
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
140
91
  }>;
141
92
  type InterruptOnConfig = z.input<typeof InterruptOnConfigSchema>;
142
93
  /**
@@ -318,38 +269,14 @@ declare const contextSchema: z.ZodObject<{
318
269
  * JSON schema for the arguments associated with the action, if edits are allowed.
319
270
  */
320
271
  argsSchema: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
321
- /**
322
- * Optional predicate controlling whether to interrupt for a given tool call.
323
- *
324
- * Receives a {@link ToolCallRequest} and returns `true` to interrupt or
325
- * `false` to auto-approve the tool call.
326
- *
327
- * The request is constructed with `tool` set to `undefined` and `runtime` set
328
- * to the node-level {@link Runtime}, so `request.tool` is not available.
329
- *
330
- * @example
331
- * ```typescript
332
- * import type { InterruptOnConfig } from "langchain";
333
- *
334
- * // Only interrupt delete_file calls targeting /etc
335
- * const config: InterruptOnConfig = {
336
- * allowedDecisions: ["approve", "reject"],
337
- * when: (request) =>
338
- * String(request.toolCall.args.path ?? "").startsWith("/etc"),
339
- * };
340
- * ```
341
- */
342
- when: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodType<ToolCallRequest<AgentBuiltInState, unknown>, z.ZodTypeDef, ToolCallRequest<AgentBuiltInState, unknown>>], z.ZodUnknown>, z.ZodUnion<[z.ZodBoolean, z.ZodPromise<z.ZodBoolean>]>>>;
343
272
  }, "strip", z.ZodTypeAny, {
344
273
  allowedDecisions: ("approve" | "edit" | "reject")[];
345
274
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
346
275
  argsSchema?: Record<string, any> | undefined;
347
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
348
276
  }, {
349
277
  allowedDecisions: ("approve" | "edit" | "reject")[];
350
278
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
351
279
  argsSchema?: Record<string, any> | undefined;
352
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
353
280
  }>]>>>;
354
281
  /**
355
282
  * Prefix used when constructing human-facing approval messages.
@@ -365,7 +292,6 @@ declare const contextSchema: z.ZodObject<{
365
292
  allowedDecisions: ("approve" | "edit" | "reject")[];
366
293
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
367
294
  argsSchema?: Record<string, any> | undefined;
368
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
369
295
  }> | undefined;
370
296
  descriptionPrefix: string;
371
297
  }, {
@@ -373,7 +299,6 @@ declare const contextSchema: z.ZodObject<{
373
299
  allowedDecisions: ("approve" | "edit" | "reject")[];
374
300
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
375
301
  argsSchema?: Record<string, any> | undefined;
376
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
377
302
  }> | undefined;
378
303
  descriptionPrefix?: string | undefined;
379
304
  }>;
@@ -437,7 +362,6 @@ type HumanInTheLoopMiddlewareConfig = InferInteropZodInput<typeof contextSchema>
437
362
  * @param options.interruptOn[toolName].allowedDecisions - Array of decision types allowed for this tool (e.g., ["approve", "edit", "reject"])
438
363
  * @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
439
364
  * @param options.interruptOn[toolName].argsSchema - JSON schema for the arguments associated with the action, if edits are allowed
440
- * @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.
441
365
  * @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.
442
366
  *
443
367
  * @returns A middleware instance that can be passed to `createAgent`
@@ -650,38 +574,14 @@ declare function humanInTheLoopMiddleware(options: NonNullable<HumanInTheLoopMid
650
574
  * JSON schema for the arguments associated with the action, if edits are allowed.
651
575
  */
652
576
  argsSchema: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
653
- /**
654
- * Optional predicate controlling whether to interrupt for a given tool call.
655
- *
656
- * Receives a {@link ToolCallRequest} and returns `true` to interrupt or
657
- * `false` to auto-approve the tool call.
658
- *
659
- * The request is constructed with `tool` set to `undefined` and `runtime` set
660
- * to the node-level {@link Runtime}, so `request.tool` is not available.
661
- *
662
- * @example
663
- * ```typescript
664
- * import type { InterruptOnConfig } from "langchain";
665
- *
666
- * // Only interrupt delete_file calls targeting /etc
667
- * const config: InterruptOnConfig = {
668
- * allowedDecisions: ["approve", "reject"],
669
- * when: (request) =>
670
- * String(request.toolCall.args.path ?? "").startsWith("/etc"),
671
- * };
672
- * ```
673
- */
674
- when: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodType<ToolCallRequest<AgentBuiltInState, unknown>, z.ZodTypeDef, ToolCallRequest<AgentBuiltInState, unknown>>], z.ZodUnknown>, z.ZodUnion<[z.ZodBoolean, z.ZodPromise<z.ZodBoolean>]>>>;
675
577
  }, "strip", z.ZodTypeAny, {
676
578
  allowedDecisions: ("approve" | "edit" | "reject")[];
677
579
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
678
580
  argsSchema?: Record<string, any> | undefined;
679
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
680
581
  }, {
681
582
  allowedDecisions: ("approve" | "edit" | "reject")[];
682
583
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
683
584
  argsSchema?: Record<string, any> | undefined;
684
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
685
585
  }>]>>>;
686
586
  /**
687
587
  * Prefix used when constructing human-facing approval messages.
@@ -697,7 +597,6 @@ declare function humanInTheLoopMiddleware(options: NonNullable<HumanInTheLoopMid
697
597
  allowedDecisions: ("approve" | "edit" | "reject")[];
698
598
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
699
599
  argsSchema?: Record<string, any> | undefined;
700
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
701
600
  }> | undefined;
702
601
  descriptionPrefix: string;
703
602
  }, {
@@ -705,7 +604,6 @@ declare function humanInTheLoopMiddleware(options: NonNullable<HumanInTheLoopMid
705
604
  allowedDecisions: ("approve" | "edit" | "reject")[];
706
605
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
707
606
  argsSchema?: Record<string, any> | undefined;
708
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
709
607
  }> | undefined;
710
608
  descriptionPrefix?: string | undefined;
711
609
  }>, {
@@ -713,10 +611,9 @@ declare function humanInTheLoopMiddleware(options: NonNullable<HumanInTheLoopMid
713
611
  allowedDecisions: ("approve" | "edit" | "reject")[];
714
612
  description?: string | ((args_0: ToolCall<string, Record<string, any>>, args_1: AgentBuiltInState, args_2: Runtime<unknown>, ...args: unknown[]) => string | Promise<string>) | undefined;
715
613
  argsSchema?: Record<string, any> | undefined;
716
- when?: ((args_0: ToolCallRequest<AgentBuiltInState, unknown>, ...args: unknown[]) => boolean | Promise<boolean>) | undefined;
717
614
  }> | undefined;
718
615
  descriptionPrefix: string;
719
616
  }, readonly (_$_langchain_core_tools0.ServerTool | _$_langchain_core_tools0.ClientTool)[], readonly []>;
720
617
  //#endregion
721
- export { Action, ActionRequest, ApproveDecision, Decision, DecisionType, DescriptionFactory, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, RejectDecision, ReviewConfig, WhenPredicate, humanInTheLoopMiddleware };
618
+ export { Action, ActionRequest, ApproveDecision, Decision, DecisionType, DescriptionFactory, EditDecision, HITLRequest, HITLResponse, HumanInTheLoopMiddlewareConfig, InterruptOnConfig, RejectDecision, ReviewConfig, humanInTheLoopMiddleware };
722
619
  //# sourceMappingURL=hitl.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hitl.d.cts","names":[],"sources":["../../../src/agents/middleware/hitl.ts"],"mappings":";;;;;;;;cAcM,kBAAA,EAAkB,CAAA,CAAA,WAAA,CAAA,CAAA,CAAA,QAAA,EAAA,CAAA,CAAA,OAAA,CAAA,eAAA,CAAA,iBAAA,YAAA,CAAA,CAAA,UAAA,EAAA,eAAA,CAAA,iBAAA,cAAA,CAAA,CAAA,UAAA,GAAA,CAAA,CAAA,QAAA,EAAA,CAAA,CAAA,UAAA,EAAA,CAAA,CAAA,UAAA,CAAA,CAAA,CAAA,UAAA;;;AAF0B;;;;;;;;;;;;;;;;;;;;;KA8B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