@@ -20,15 +20,13 @@ import {
2020import { RunnableCallable } from "../RunnableCallable.js" ;
2121import { PreHookAnnotation } from "../annotation.js" ;
2222import { mergeAbortSignals } from "./utils.js" ;
23- import { wrapToolCall } from "../utils.js" ;
2423import { ToolInvocationError } from "../errors.js" ;
2524import type {
26- AnyAnnotationRoot ,
25+ WrapToolCallHook ,
2726 ToolCallRequest ,
2827 ToAnnotationRoot ,
2928} from "../middleware/types.js" ;
30- import type { AgentMiddleware } from "../middleware/types.js" ;
31- import type { StateManager } from "../state.js" ;
29+ import type { AgentBuiltInState } from "../runtime.js" ;
3230
3331export interface ToolNodeOptions {
3432 /**
@@ -66,13 +64,11 @@ export interface ToolNodeOptions {
6664 | boolean
6765 | ( ( error : unknown , toolCall : ToolCall ) => ToolMessage | undefined ) ;
6866 /**
69- * The middleware to use for tool execution.
67+ * Optional wrapper function for tool execution.
68+ * Allows middleware to intercept and modify tool calls before execution.
69+ * The wrapper receives the tool call request and a handler function to execute the tool.
7070 */
71- middleware ?: readonly AgentMiddleware [ ] ;
72- /**
73- * The state manager to use for tool execution.
74- */
75- stateManager ?: StateManager ;
71+ wrapToolCall ?: WrapToolCallHook ;
7672}
7773
7874const isBaseMessageArray = ( input : unknown ) : input is BaseMessage [ ] =>
@@ -165,8 +161,8 @@ function defaultHandleToolErrors(
165161 * ```
166162 */
167163export class ToolNode <
168- StateSchema extends AnyAnnotationRoot | InteropZodObject = any ,
169- ContextSchema extends AnyAnnotationRoot | InteropZodObject = any
164+ StateSchema extends InteropZodObject = any ,
165+ ContextSchema extends InteropZodObject = any
170166> extends RunnableCallable < StateSchema , ContextSchema > {
171167 tools : ( StructuredToolInterface | DynamicTool | RunnableToolLike ) [ ] ;
172168
@@ -179,15 +175,13 @@ export class ToolNode<
179175 | ( ( error : unknown , toolCall : ToolCall ) => ToolMessage | undefined ) =
180176 defaultHandleToolErrors ;
181177
182- middleware : readonly AgentMiddleware [ ] = [ ] ;
183-
184- stateManager ?: StateManager ;
178+ wrapToolCall : WrapToolCallHook | undefined ;
185179
186180 constructor (
187181 tools : ( StructuredToolInterface | DynamicTool | RunnableToolLike ) [ ] ,
188182 public options ?: ToolNodeOptions
189183 ) {
190- const { name, tags, handleToolErrors, middleware , stateManager , signal } =
184+ const { name, tags, handleToolErrors, signal , wrapToolCall } =
191185 options ?? { } ;
192186 super ( {
193187 name,
@@ -201,9 +195,8 @@ export class ToolNode<
201195 } ) ;
202196 this . tools = tools ;
203197 this . handleToolErrors = handleToolErrors ?? this . handleToolErrors ;
204- this . middleware = middleware ?? [ ] ;
205198 this . signal = signal ;
206- this . stateManager = stateManager ;
199+ this . wrapToolCall = wrapToolCall ;
207200 }
208201
209202 /**
@@ -279,7 +272,7 @@ export class ToolNode<
279272 protected async runTool (
280273 call : ToolCall ,
281274 config : RunnableConfig ,
282- state : ToAnnotationRoot < StateSchema > [ "State" ] & PreHookAnnotation [ "State" ]
275+ state : AgentBuiltInState
283276 ) : Promise < ToolMessage | Command > {
284277 /**
285278 * Define the base handler that executes the tool.
@@ -355,20 +348,12 @@ export class ToolNode<
355348 runtime,
356349 } ;
357350
358- /**
359- * Collect and compose wrapToolCall handlers from middleware
360- * Wrap each handler with error handling and validation
361- */
362- const wrapToolCallHandler = this . stateManager
363- ? wrapToolCall ( this . middleware , state )
364- : undefined ;
365-
366351 /**
367352 * If wrapToolCall is provided, use it to wrap the tool execution
368353 */
369- if ( wrapToolCallHandler ) {
354+ if ( this . wrapToolCall ) {
370355 try {
371- return await wrapToolCallHandler ( request , baseHandler ) ;
356+ return await this . wrapToolCall ( request , baseHandler ) ;
372357 } catch ( e : unknown ) {
373358 /**
374359 * Handle middleware errors
0 commit comments