@@ -21,13 +21,24 @@ type PromiseOrValue<T> = T | Promise<T>;
2121
2222export type AnyAnnotationRoot = AnnotationRoot < any > ;
2323
24- type NormalizedSchemaInput < TSchema extends InteropZodObject | undefined = any > =
25- TSchema extends InteropZodObject ? InferInteropZodInput < TSchema > : { } ;
24+ type NormalizedSchemaInput <
25+ TSchema extends InteropZodObject | undefined | never = any
26+ > = [ TSchema ] extends [ never ]
27+ ? AgentBuiltInState
28+ : TSchema extends InteropZodObject
29+ ? InferInteropZodOutput < TSchema > & AgentBuiltInState
30+ : TSchema extends Record < string , unknown >
31+ ? TSchema & AgentBuiltInState
32+ : AgentBuiltInState ;
2633
2734/**
2835 * Result type for middleware functions.
2936 */
30- export type MiddlewareResult < TState > = TState | void ;
37+ export type MiddlewareResult < TState > =
38+ | ( TState & {
39+ jumpTo ?: JumpToTarget ;
40+ } )
41+ | void ;
3142
3243/**
3344 * Represents a tool call request for the wrapToolCall hook.
@@ -61,28 +72,22 @@ export interface ToolCallRequest<
6172 * Takes a tool call request and returns the tool result or a command.
6273 */
6374export type ToolCallHandler <
64- TSchema extends InteropZodObject | undefined = any ,
75+ TSchema extends Record < string , unknown > = AgentBuiltInState ,
6576 TContext = unknown
6677> = (
67- request : ToolCallRequest <
68- NormalizedSchemaInput < TSchema > & AgentBuiltInState ,
69- TContext
70- >
78+ request : ToolCallRequest < TSchema , TContext >
7179) => PromiseOrValue < ToolMessage | Command > ;
7280
7381/**
7482 * Wrapper function type for the wrapToolCall hook.
7583 * Allows middleware to intercept and modify tool execution.
7684 */
7785export type WrapToolCallHook <
78- TSchema extends InteropZodObject | undefined = any ,
86+ TSchema extends InteropZodObject | undefined = undefined ,
7987 TContext = unknown
8088> = (
81- request : ToolCallRequest <
82- NormalizedSchemaInput < TSchema > & AgentBuiltInState ,
83- TContext
84- > ,
85- handler : ToolCallHandler < TSchema , TContext >
89+ request : ToolCallRequest < NormalizedSchemaInput < TSchema > , TContext > ,
90+ handler : ToolCallHandler < NormalizedSchemaInput < TSchema > , TContext >
8691) => PromiseOrValue < ToolMessage | Command > ;
8792
8893/**
@@ -93,13 +98,10 @@ export type WrapToolCallHook<
9398 * @returns The AI message response from the model
9499 */
95100export type WrapModelCallHandler <
96- TSchema extends InteropZodObject | undefined = any ,
101+ TSchema extends InteropZodObject | undefined = undefined ,
97102 TContext = unknown
98103> = (
99- request : ModelRequest <
100- NormalizedSchemaInput < TSchema > & AgentBuiltInState ,
101- TContext
102- >
104+ request : ModelRequest < NormalizedSchemaInput < TSchema > , TContext >
103105) => PromiseOrValue < AIMessage > ;
104106
105107/**
@@ -116,13 +118,10 @@ export type WrapModelCallHandler<
116118 * @returns The AI message response from the model (or a modified version)
117119 */
118120export type WrapModelCallHook <
119- TSchema extends InteropZodObject | undefined = any ,
121+ TSchema extends InteropZodObject | undefined = undefined ,
120122 TContext = unknown
121123> = (
122- request : ModelRequest <
123- NormalizedSchemaInput < TSchema > & AgentBuiltInState ,
124- TContext
125- > ,
124+ request : ModelRequest < NormalizedSchemaInput < TSchema > , TContext > ,
126125 handler : WrapModelCallHandler < TSchema , TContext >
127126) => PromiseOrValue < AIMessage > ;
128127
@@ -134,26 +133,23 @@ export type WrapModelCallHook<
134133 * @param runtime - The runtime context containing metadata, signal, writer, interrupt, etc.
135134 * @returns A middleware result containing partial state updates or undefined to pass through
136135 */
137- export type BeforeAgentHandler <
138- TSchema extends InteropZodObject | undefined = any ,
139- TContext = unknown
140- > = (
141- state : NormalizedSchemaInput < TSchema > & AgentBuiltInState ,
136+ type BeforeAgentHandler < TSchema , TContext > = (
137+ state : TSchema ,
142138 runtime : Runtime < TContext >
143- ) => PromiseOrValue < MiddlewareResult < Partial < NormalizedSchemaInput < TSchema > > > > ;
139+ ) => PromiseOrValue < MiddlewareResult < Partial < TSchema > > > ;
144140
145141/**
146142 * Hook type for the beforeAgent lifecycle event.
147143 * Can be either a handler function or an object with a handler and optional jump targets.
148144 * This hook is called once at the start of the agent invocation.
149145 */
150146export type BeforeAgentHook <
151- TSchema extends InteropZodObject | undefined = any ,
147+ TSchema extends InteropZodObject | undefined = undefined ,
152148 TContext = unknown
153149> =
154- | BeforeAgentHandler < TSchema , TContext >
150+ | BeforeAgentHandler < NormalizedSchemaInput < TSchema > , TContext >
155151 | {
156- hook : BeforeAgentHandler < TSchema , TContext > ;
152+ hook : BeforeAgentHandler < NormalizedSchemaInput < TSchema > , TContext > ;
157153 canJumpTo ?: JumpToTarget [ ] ;
158154 } ;
159155
@@ -165,26 +161,23 @@ export type BeforeAgentHook<
165161 * @param runtime - The runtime context containing metadata, signal, writer, interrupt, etc.
166162 * @returns A middleware result containing partial state updates or undefined to pass through
167163 */
168- export type BeforeModelHandler <
169- TSchema extends InteropZodObject | undefined = any ,
170- TContext = unknown
171- > = (
172- state : NormalizedSchemaInput < TSchema > & AgentBuiltInState ,
164+ type BeforeModelHandler < TSchema , TContext > = (
165+ state : TSchema ,
173166 runtime : Runtime < TContext >
174- ) => PromiseOrValue < MiddlewareResult < Partial < NormalizedSchemaInput < TSchema > > > > ;
167+ ) => PromiseOrValue < MiddlewareResult < Partial < TSchema > > > ;
175168
176169/**
177170 * Hook type for the beforeModel lifecycle event.
178171 * Can be either a handler function or an object with a handler and optional jump targets.
179172 * This hook is called before each model invocation.
180173 */
181174export type BeforeModelHook <
182- TSchema extends InteropZodObject | undefined = any ,
175+ TSchema extends InteropZodObject | undefined = undefined ,
183176 TContext = unknown
184177> =
185- | BeforeModelHandler < TSchema , TContext >
178+ | BeforeModelHandler < NormalizedSchemaInput < TSchema > , TContext >
186179 | {
187- hook : BeforeModelHandler < TSchema , TContext > ;
180+ hook : BeforeModelHandler < NormalizedSchemaInput < TSchema > , TContext > ;
188181 canJumpTo ?: JumpToTarget [ ] ;
189182 } ;
190183
@@ -197,26 +190,23 @@ export type BeforeModelHook<
197190 * @param runtime - The runtime context containing metadata, signal, writer, interrupt, etc.
198191 * @returns A middleware result containing partial state updates or undefined to pass through
199192 */
200- export type AfterModelHandler <
201- TSchema extends InteropZodObject | undefined = any ,
202- TContext = unknown
203- > = (
204- state : NormalizedSchemaInput < TSchema > & AgentBuiltInState ,
193+ type AfterModelHandler < TSchema , TContext > = (
194+ state : TSchema ,
205195 runtime : Runtime < TContext >
206- ) => PromiseOrValue < MiddlewareResult < Partial < NormalizedSchemaInput < TSchema > > > > ;
196+ ) => PromiseOrValue < MiddlewareResult < Partial < TSchema > > > ;
207197
208198/**
209199 * Hook type for the afterModel lifecycle event.
210200 * Can be either a handler function or an object with a handler and optional jump targets.
211201 * This hook is called after each model invocation.
212202 */
213203export type AfterModelHook <
214- TSchema extends InteropZodObject | undefined = any ,
204+ TSchema extends InteropZodObject | undefined = undefined ,
215205 TContext = unknown
216206> =
217- | AfterModelHandler < TSchema , TContext >
207+ | AfterModelHandler < NormalizedSchemaInput < TSchema > , TContext >
218208 | {
219- hook : AfterModelHandler < TSchema , TContext > ;
209+ hook : AfterModelHandler < NormalizedSchemaInput < TSchema > , TContext > ;
220210 canJumpTo ?: JumpToTarget [ ] ;
221211 } ;
222212
@@ -228,26 +218,23 @@ export type AfterModelHook<
228218 * @param runtime - The runtime context containing metadata, signal, writer, interrupt, etc.
229219 * @returns A middleware result containing partial state updates or undefined to pass through
230220 */
231- export type AfterAgentHandler <
232- TSchema extends InteropZodObject | undefined = any ,
233- TContext = unknown
234- > = (
235- state : NormalizedSchemaInput < TSchema > & AgentBuiltInState ,
221+ type AfterAgentHandler < TSchema , TContext > = (
222+ state : TSchema ,
236223 runtime : Runtime < TContext >
237- ) => PromiseOrValue < MiddlewareResult < Partial < NormalizedSchemaInput < TSchema > > > > ;
224+ ) => PromiseOrValue < MiddlewareResult < Partial < TSchema > > > ;
238225
239226/**
240227 * Hook type for the afterAgent lifecycle event.
241228 * Can be either a handler function or an object with a handler and optional jump targets.
242229 * This hook is called once at the end of the agent invocation.
243230 */
244231export type AfterAgentHook <
245- TSchema extends InteropZodObject | undefined = any ,
232+ TSchema extends InteropZodObject | undefined = undefined ,
246233 TContext = unknown
247234> =
248- | AfterAgentHandler < TSchema , TContext >
235+ | AfterAgentHandler < NormalizedSchemaInput < TSchema > , TContext >
249236 | {
250- hook : AfterAgentHandler < TSchema , TContext > ;
237+ hook : AfterAgentHandler < NormalizedSchemaInput < TSchema > , TContext > ;
251238 canJumpTo ?: JumpToTarget [ ] ;
252239 } ;
253240
0 commit comments