@@ -82,12 +82,12 @@ export type StateGraphNodeSpec<RunInput, RunOutput> = NodeSpec<
82
82
retryPolicy ?: RetryPolicy ;
83
83
} ;
84
84
85
- export type StateGraphAddNodeOptions = {
85
+ export type StateGraphAddNodeOptions < Nodes extends string = string > = {
86
86
retryPolicy ?: RetryPolicy ;
87
87
// TODO: Fix generic typing for annotations
88
88
// eslint-disable-next-line @typescript-eslint/no-explicit-any
89
89
input ?: AnnotationRoot < any > | AnyZodObject ;
90
- } & AddNodeOptions ;
90
+ } & AddNodeOptions < Nodes > ;
91
91
92
92
export type StateGraphArgsWithStateSchema <
93
93
SD extends StateDefinition ,
@@ -237,7 +237,8 @@ export class StateGraph<
237
237
fields : SD extends StateDefinition
238
238
? StateGraphArgsWithInputOutputSchemas < SD , ToStateDefinition < O > >
239
239
: never ,
240
- configSchema ?: C | AnnotationRoot < ToStateDefinition < C > >
240
+ configSchema ?: C | AnnotationRoot < ToStateDefinition < C > > ,
241
+ options ?: { nodes ?: N [ ] }
241
242
) ;
242
243
243
244
constructor (
@@ -252,14 +253,16 @@ export class StateGraph<
252
253
ToStateDefinition < O >
253
254
>
254
255
: StateGraphArgs < S > ,
255
- configSchema ?: C | AnnotationRoot < ToStateDefinition < C > >
256
+ configSchema ?: C | AnnotationRoot < ToStateDefinition < C > > ,
257
+ options ?: { nodes ?: N [ ] }
256
258
) ;
257
259
258
260
constructor (
259
261
fields : SD extends AnyZodObject
260
262
? SD | ZodStateGraphArgsWithStateSchema < SD , I , O >
261
263
: never ,
262
- configSchema ?: C | AnnotationRoot < ToStateDefinition < C > >
264
+ configSchema ?: C | AnnotationRoot < ToStateDefinition < C > > ,
265
+ options ?: { nodes ?: N [ ] }
263
266
) ;
264
267
265
268
constructor (
@@ -277,7 +280,8 @@ export class StateGraph<
277
280
>
278
281
| StateGraphArgsWithInputOutputSchemas < SD , ToStateDefinition < O > >
279
282
: StateGraphArgs < S > ,
280
- configSchema ?: C | AnnotationRoot < ToStateDefinition < C > >
283
+ configSchema ?: C | AnnotationRoot < ToStateDefinition < C > > ,
284
+ _options ?: { nodes ?: N [ ] }
281
285
) {
282
286
super ( ) ;
283
287
@@ -434,7 +438,12 @@ export class StateGraph<
434
438
isMultipleNodes ( args ) // eslint-disable-line no-nested-ternary
435
439
? Array . isArray ( args [ 0 ] )
436
440
? args [ 0 ]
437
- : Object . entries ( args [ 0 ] )
441
+ : Object . entries ( args [ 0 ] ) . map ( ( [ key , action ] ) => [
442
+ key ,
443
+ action ,
444
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
445
+ ( action as any ) [ Symbol . for ( "langgraph.state.node" ) ] ?? undefined ,
446
+ ] )
438
447
: [ [ args [ 0 ] , args [ 1 ] , args [ 2 ] ] ]
439
448
) as [
440
449
K ,
@@ -574,7 +583,12 @@ export class StateGraph<
574
583
) : StateGraph < SD , S , U , N | K , I , O , C > {
575
584
const parsedNodes = Array . isArray ( nodes )
576
585
? nodes
577
- : ( Object . entries ( nodes ) as [ K , NodeAction < S , U , C > ] [ ] ) ;
586
+ : ( Object . entries ( nodes ) . map ( ( [ key , action ] ) => [
587
+ key ,
588
+ action ,
589
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
590
+ ( action as any ) [ Symbol . for ( "langgraph.state.node" ) ] ?? undefined ,
591
+ ] ) as [ K , NodeAction < S , U , C > , StateGraphAddNodeOptions | undefined ] [ ] ) ;
578
592
579
593
if ( parsedNodes . length === 0 ) {
580
594
throw new Error ( "Sequence requires at least one node." ) ;
@@ -1091,3 +1105,41 @@ function _getControlBranch() {
1091
1105
path : CONTROL_BRANCH_PATH ,
1092
1106
} ) ;
1093
1107
}
1108
+
1109
+ type TypedNodeAction < SD extends StateDefinition , Nodes extends string > = (
1110
+ state : StateType < SD > ,
1111
+ config : LangGraphRunnableConfig
1112
+ ) => UpdateType < SD > | Command < unknown , UpdateType < SD > , Nodes > ;
1113
+
1114
+ export function typedNode < SD extends SDZod , Nodes extends string > (
1115
+ _state : SD extends StateDefinition ? AnnotationRoot < SD > : never ,
1116
+ _options ?: { nodes ?: Nodes [ ] }
1117
+ ) : (
1118
+ func : TypedNodeAction < ToStateDefinition < SD > , Nodes > ,
1119
+ options ?: StateGraphAddNodeOptions < Nodes >
1120
+ ) => TypedNodeAction < ToStateDefinition < SD > , Nodes > ;
1121
+
1122
+ export function typedNode < SD extends SDZod , Nodes extends string > (
1123
+ _state : SD extends AnyZodObject ? SD : never ,
1124
+ _options ?: { nodes ?: Nodes [ ] }
1125
+ ) : (
1126
+ func : TypedNodeAction < ToStateDefinition < SD > , Nodes > ,
1127
+ options ?: StateGraphAddNodeOptions < Nodes >
1128
+ ) => TypedNodeAction < ToStateDefinition < SD > , Nodes > ;
1129
+
1130
+ export function typedNode < SD extends SDZod , Nodes extends string > (
1131
+ _state : SD extends AnyZodObject
1132
+ ? SD
1133
+ : SD extends StateDefinition
1134
+ ? AnnotationRoot < SD >
1135
+ : never ,
1136
+ _options ?: { nodes ?: Nodes [ ] }
1137
+ ) {
1138
+ return (
1139
+ func : TypedNodeAction < ToStateDefinition < SD > , Nodes > ,
1140
+ options ?: StateGraphAddNodeOptions < Nodes >
1141
+ ) => {
1142
+ Object . assign ( func , { [ Symbol . for ( "langgraph.state.node" ) ] : options } ) ;
1143
+ return func ;
1144
+ } ;
1145
+ }
0 commit comments