@@ -133,11 +133,9 @@ describe("middleware types", () => {
133133 it ( "doesn't require users to pass in a context if a middleware has optional context schema" , async ( ) => {
134134 const middleware = createMiddleware ( {
135135 name : "Middleware" ,
136- contextSchema : z
137- . object ( {
138- customOptionalContextProp : z . string ( ) . default ( "default value" ) ,
139- } )
140- . optional ( ) ,
136+ contextSchema : z . object ( {
137+ customOptionalContextProp : z . string ( ) . optional ( ) ,
138+ } ) ,
141139 } ) ;
142140
143141 const agent = createAgent ( {
@@ -158,18 +156,13 @@ describe("middleware types", () => {
158156 ) ;
159157 } ) ;
160158
161- it ( "doesn't require users to pass in a context if a middleware has context schema with defaults" , async ( ) => {
159+ it ( "doesn't require users to pass in a context if a middleware has context schema with defaults or optional " , async ( ) => {
162160 const middleware = createMiddleware ( {
163161 name : "Middleware" ,
164- contextSchema : z
165- . object ( {
166- customDefaultContextProp : z . string ( ) . default ( "default value" ) ,
167- customOptionalContextProp : z . string ( ) . optional ( ) ,
168- customRequiredContextProp : z . string ( ) ,
169- } )
170- . default ( {
171- customRequiredContextProp : "default value" ,
172- } ) ,
162+ contextSchema : z . object ( {
163+ customDefaultContextProp : z . string ( ) . default ( "default value" ) ,
164+ customOptionalContextProp : z . string ( ) . optional ( ) ,
165+ } ) ,
173166 stateSchema : z . object ( {
174167 customDefaultStateProp : z . string ( ) . default ( "default value" ) ,
175168 customOptionalStateProp : z . string ( ) . optional ( ) ,
@@ -186,7 +179,6 @@ describe("middleware types", () => {
186179 expectTypeOf ( runtime . context ) . toEqualTypeOf < {
187180 customDefaultContextProp : string ;
188181 customOptionalContextProp ?: string ;
189- customRequiredContextProp : string ;
190182 } > ( ) ;
191183 } ,
192184 afterModel : async ( state , runtime ) => {
@@ -200,7 +192,6 @@ describe("middleware types", () => {
200192 expectTypeOf ( runtime . context ) . toEqualTypeOf < {
201193 customDefaultContextProp : string ;
202194 customOptionalContextProp ?: string ;
203- customRequiredContextProp : string ;
204195 } > ( ) ;
205196 } ,
206197 wrapModelCall : async ( request , handler ) => {
@@ -210,7 +201,6 @@ describe("middleware types", () => {
210201 expectTypeOf ( request . runtime . context ) . toEqualTypeOf < {
211202 customDefaultContextProp : string ;
212203 customOptionalContextProp ?: string ;
213- customRequiredContextProp : string ;
214204 } > ( ) ;
215205
216206 return handler ( {
@@ -242,40 +232,23 @@ describe("middleware types", () => {
242232 it ( "doesn't require users to pass in a context if a middleware has context schema as optional" , async ( ) => {
243233 const middleware = createMiddleware ( {
244234 name : "Middleware" ,
245- contextSchema : z
246- . object ( {
247- customOptionalContextProp : z . string ( ) . default ( "default value" ) ,
248- } )
249- . optional ( ) ,
235+ contextSchema : z . object ( {
236+ customOptionalContextProp : z . string ( ) . default ( "default value" ) ,
237+ } ) ,
250238 beforeModel : async ( _state , runtime ) => {
251- expectTypeOf ( runtime . context ) . toEqualTypeOf <
252- Partial <
253- | {
254- customOptionalContextProp : string ;
255- }
256- | undefined
257- >
258- > ( ) ;
239+ expectTypeOf ( runtime . context ) . toEqualTypeOf < {
240+ customOptionalContextProp : string ;
241+ } > ( ) ;
259242 } ,
260243 afterModel : async ( _state , runtime ) => {
261- expectTypeOf ( runtime . context ) . toEqualTypeOf <
262- Partial <
263- | {
264- customOptionalContextProp : string ;
265- }
266- | undefined
267- >
268- > ( ) ;
244+ expectTypeOf ( runtime . context ) . toEqualTypeOf < {
245+ customOptionalContextProp : string ;
246+ } > ( ) ;
269247 } ,
270248 wrapModelCall : async ( request ) => {
271- expectTypeOf ( request . runtime . context ) . toEqualTypeOf <
272- Partial <
273- | {
274- customOptionalContextProp : string ;
275- }
276- | undefined
277- >
278- > ( ) ;
249+ expectTypeOf ( request . runtime . context ) . toEqualTypeOf < {
250+ customOptionalContextProp : string ;
251+ } > ( ) ;
279252
280253 return new AIMessage ( "foobar" ) ;
281254 } ,
0 commit comments