11/* eslint-disable @typescript-eslint/no-explicit-any */
2- import { z } from "zod/v3" ;
2+ import type {
3+ InteropZodObject ,
4+ InteropZodDefault ,
5+ InteropZodOptional ,
6+ InferInteropZodInput ,
7+ } from "@langchain/core/utils/types" ;
38import type {
49 AgentMiddleware ,
510 Runtime ,
@@ -40,11 +45,11 @@ import type {
4045 * ```
4146 */
4247export function createMiddleware <
43- TSchema extends z . ZodObject < any > | undefined = undefined ,
48+ TSchema extends InteropZodObject | undefined = undefined ,
4449 TContextSchema extends
45- | z . ZodObject < any >
46- | z . ZodOptional < z . ZodObject < any > >
47- | z . ZodDefault < z . ZodObject < any > >
50+ | InteropZodObject
51+ | InteropZodOptional
52+ | InteropZodDefault
4853 | undefined = undefined
4954> ( config : {
5055 /**
@@ -90,18 +95,20 @@ export function createMiddleware<
9095 * @returns The modified model request or undefined to pass through
9196 */
9297 modifyModelRequest ?: (
93- request : ModelRequest ,
94- state : ( TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } ) &
98+ options : ModelRequest ,
99+ state : ( TSchema extends InteropZodObject
100+ ? InferInteropZodInput < TSchema >
101+ : { } ) &
95102 AgentBuiltInState ,
96103 runtime : Runtime <
97- ( TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } ) &
104+ ( TSchema extends InteropZodObject ? InferInteropZodInput < TSchema > : { } ) &
98105 AgentBuiltInState ,
99- TContextSchema extends z . ZodObject < any >
100- ? z . infer < TContextSchema >
101- : TContextSchema extends z . ZodDefault < z . ZodObject < any > >
102- ? z . infer < TContextSchema >
103- : TContextSchema extends z . ZodOptional < z . ZodObject < any > >
104- ? Partial < z . infer < TContextSchema > >
106+ TContextSchema extends InteropZodObject
107+ ? InferInteropZodInput < TContextSchema >
108+ : TContextSchema extends InteropZodDefault
109+ ? InferInteropZodInput < TContextSchema >
110+ : TContextSchema extends InteropZodOptional
111+ ? Partial < InferInteropZodInput < TContextSchema > >
105112 : never
106113 >
107114 ) => Promise < ModelRequest | void > | ModelRequest | void ;
@@ -114,27 +121,35 @@ export function createMiddleware<
114121 * @returns The modified middleware state or undefined to pass through
115122 */
116123 beforeModel ?: (
117- state : ( TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } ) &
124+ state : ( TSchema extends InteropZodObject
125+ ? InferInteropZodInput < TSchema >
126+ : { } ) &
118127 AgentBuiltInState ,
119128 runtime : Runtime <
120- ( TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } ) &
129+ ( TSchema extends InteropZodObject ? InferInteropZodInput < TSchema > : { } ) &
121130 AgentBuiltInState ,
122- TContextSchema extends z . ZodObject < any >
123- ? z . infer < TContextSchema >
124- : TContextSchema extends z . ZodDefault < z . ZodObject < any > >
125- ? z . infer < TContextSchema >
126- : TContextSchema extends z . ZodOptional < z . ZodObject < any > >
127- ? Partial < z . infer < TContextSchema > >
131+ TContextSchema extends InteropZodObject
132+ ? InferInteropZodInput < TContextSchema >
133+ : TContextSchema extends InteropZodDefault
134+ ? InferInteropZodInput < TContextSchema >
135+ : TContextSchema extends InteropZodOptional
136+ ? Partial < InferInteropZodInput < TContextSchema > >
128137 : never
129138 >
130139 ) =>
131140 | Promise <
132141 MiddlewareResult <
133- Partial < TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } >
142+ Partial <
143+ TSchema extends InteropZodObject
144+ ? InferInteropZodInput < TSchema >
145+ : { }
146+ >
134147 >
135148 >
136149 | MiddlewareResult <
137- Partial < TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } >
150+ Partial <
151+ TSchema extends InteropZodObject ? InferInteropZodInput < TSchema > : { }
152+ >
138153 > ;
139154 /**
140155 * The function to run after the model call. This function is called after the model is invoked and before any tools are called.
@@ -145,27 +160,35 @@ export function createMiddleware<
145160 * @returns The modified middleware state or undefined to pass through
146161 */
147162 afterModel ?: (
148- state : ( TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } ) &
163+ state : ( TSchema extends InteropZodObject
164+ ? InferInteropZodInput < TSchema >
165+ : { } ) &
149166 AgentBuiltInState ,
150167 runtime : Runtime <
151- ( TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } ) &
168+ ( TSchema extends InteropZodObject ? InferInteropZodInput < TSchema > : { } ) &
152169 AgentBuiltInState ,
153- TContextSchema extends z . ZodObject < any >
154- ? z . infer < TContextSchema >
155- : TContextSchema extends z . ZodDefault < z . ZodObject < any > >
156- ? z . infer < TContextSchema >
157- : TContextSchema extends z . ZodOptional < z . ZodObject < any > >
158- ? Partial < z . infer < TContextSchema > >
170+ TContextSchema extends InteropZodObject
171+ ? InferInteropZodInput < TContextSchema >
172+ : TContextSchema extends InteropZodDefault
173+ ? InferInteropZodInput < TContextSchema >
174+ : TContextSchema extends InteropZodOptional
175+ ? Partial < InferInteropZodInput < TContextSchema > >
159176 : never
160177 >
161178 ) =>
162179 | Promise <
163180 MiddlewareResult <
164- Partial < TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } >
181+ Partial <
182+ TSchema extends InteropZodObject
183+ ? InferInteropZodInput < TSchema >
184+ : { }
185+ >
165186 >
166187 >
167188 | MiddlewareResult <
168- Partial < TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } >
189+ Partial <
190+ TSchema extends InteropZodObject ? InferInteropZodInput < TSchema > : { }
191+ >
169192 > ;
170193} ) : AgentMiddleware < TSchema , TContextSchema , any > {
171194 const middleware : AgentMiddleware < TSchema , TContextSchema , any > = {
@@ -183,14 +206,16 @@ export function createMiddleware<
183206 options ,
184207 state ,
185208 runtime as Runtime <
186- ( TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } ) &
209+ ( TSchema extends InteropZodObject
210+ ? InferInteropZodInput < TSchema >
211+ : { } ) &
187212 AgentBuiltInState ,
188- TContextSchema extends z . ZodObject < any >
189- ? z . infer < TContextSchema >
190- : TContextSchema extends z . ZodDefault < z . ZodObject < any > >
191- ? z . infer < TContextSchema >
192- : TContextSchema extends z . ZodOptional < z . ZodObject < any > >
193- ? Partial < z . infer < TContextSchema > >
213+ TContextSchema extends InteropZodObject
214+ ? InferInteropZodInput < TContextSchema >
215+ : TContextSchema extends InteropZodDefault
216+ ? InferInteropZodInput < TContextSchema >
217+ : TContextSchema extends InteropZodOptional
218+ ? Partial < InferInteropZodInput < TContextSchema > >
194219 : never
195220 >
196221 )
@@ -203,14 +228,16 @@ export function createMiddleware<
203228 config . beforeModel ! (
204229 state ,
205230 runtime as Runtime <
206- ( TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } ) &
231+ ( TSchema extends InteropZodObject
232+ ? InferInteropZodInput < TSchema >
233+ : { } ) &
207234 AgentBuiltInState ,
208- TContextSchema extends z . ZodObject < any >
209- ? z . infer < TContextSchema >
210- : TContextSchema extends z . ZodDefault < z . ZodObject < any > >
211- ? z . infer < TContextSchema >
212- : TContextSchema extends z . ZodOptional < z . ZodObject < any > >
213- ? Partial < z . infer < TContextSchema > >
235+ TContextSchema extends InteropZodObject
236+ ? InferInteropZodInput < TContextSchema >
237+ : TContextSchema extends InteropZodDefault
238+ ? InferInteropZodInput < TContextSchema >
239+ : TContextSchema extends InteropZodOptional
240+ ? Partial < InferInteropZodInput < TContextSchema > >
214241 : never
215242 >
216243 )
@@ -223,14 +250,16 @@ export function createMiddleware<
223250 config . afterModel ! (
224251 state ,
225252 runtime as Runtime <
226- ( TSchema extends z . ZodObject < any > ? z . infer < TSchema > : { } ) &
253+ ( TSchema extends InteropZodObject
254+ ? InferInteropZodInput < TSchema >
255+ : { } ) &
227256 AgentBuiltInState ,
228- TContextSchema extends z . ZodObject < any >
229- ? z . infer < TContextSchema >
230- : TContextSchema extends z . ZodDefault < z . ZodObject < any > >
231- ? z . infer < TContextSchema >
232- : TContextSchema extends z . ZodOptional < z . ZodObject < any > >
233- ? Partial < z . infer < TContextSchema > >
257+ TContextSchema extends InteropZodObject
258+ ? InferInteropZodInput < TContextSchema >
259+ : TContextSchema extends InteropZodDefault
260+ ? InferInteropZodInput < TContextSchema >
261+ : TContextSchema extends InteropZodOptional
262+ ? Partial < InferInteropZodInput < TContextSchema > >
234263 : never
235264 >
236265 )
0 commit comments