1
- import { z } from "zod" ;
1
+ import type { z } from "zod" ;
2
2
import { zodToJsonSchema as _zodToJsonSchema } from "zod-to-json-schema" ;
3
- import { getMeta } from "./state.js" ;
4
-
5
- const TYPE_CACHE : Record < string , WeakMap < z . AnyZodObject , z . AnyZodObject > > = { } ;
6
- const DESCRIPTION_PREFIX = "lg:" ;
3
+ import { applyZodPlugin , applyExtraFromDescription } from "./state.js" ;
7
4
8
5
const PartialStateSchema = Symbol . for ( "langgraph.state.partial" ) ;
9
6
type PartialStateSchema = typeof PartialStateSchema ;
10
-
11
- function applyPlugin (
12
- schema : z . AnyZodObject ,
13
- actions : {
14
- /** Apply .langgraph.reducer calls */
15
- reducer ?: boolean ;
16
-
17
- /** Apply .langgraph.metadata() calls */
18
- jsonSchemaExtra ?: boolean ;
19
-
20
- /** Apply .partial() */
21
- partial ?: boolean ;
22
- }
23
- ) {
24
- const cacheKey = [
25
- `reducer:${ actions . reducer ?? false } ` ,
26
- `jsonSchemaExtra:${ actions . jsonSchemaExtra ?? false } ` ,
27
- `partial:${ actions . partial ?? false } ` ,
28
- ] . join ( "|" ) ;
29
-
30
- TYPE_CACHE [ cacheKey ] ??= new WeakMap ( ) ;
31
- const cache = TYPE_CACHE [ cacheKey ] ;
32
-
33
- if ( cache . has ( schema ) ) return cache . get ( schema ) ! ;
34
- let shape = z . object ( {
35
- ...Object . fromEntries (
36
- Object . entries ( schema . shape as Record < string , z . ZodTypeAny > ) . map (
37
- ( [ key , input ] ) : [ string , z . ZodTypeAny ] => {
38
- const meta = getMeta ( input ) ;
39
- let output = actions . reducer ? meta ?. reducer ?. schema ?? input : input ;
40
-
41
- if ( actions . jsonSchemaExtra ) {
42
- const strMeta = JSON . stringify ( {
43
- ...meta ?. jsonSchemaExtra ,
44
- description : output . description ?? input . description ,
45
- } ) ;
46
-
47
- if ( strMeta !== "{}" ) {
48
- output = output . describe ( `${ DESCRIPTION_PREFIX } ${ strMeta } ` ) ;
49
- }
50
- }
51
-
52
- return [ key , output ] ;
53
- }
54
- )
55
- ) ,
56
- } ) ;
57
-
58
- if ( actions . partial ) shape = shape . partial ( ) ;
59
- cache . set ( schema , shape ) ;
60
- return shape ;
61
- }
7
+ type JsonSchema = ReturnType < typeof _zodToJsonSchema > ;
62
8
63
9
// Using a subset of types to avoid circular type import
64
10
interface GraphWithZodLike {
@@ -83,37 +29,6 @@ function isGraphWithZodLike(graph: unknown): graph is GraphWithZodLike {
83
29
return true ;
84
30
}
85
31
86
- type JsonSchema = ReturnType < typeof _zodToJsonSchema > ;
87
-
88
- function applyExtraFromDescription ( schema : unknown ) : unknown {
89
- if ( Array . isArray ( schema ) ) {
90
- return schema . map ( applyExtraFromDescription ) ;
91
- }
92
-
93
- if ( typeof schema === "object" && schema != null ) {
94
- const output = Object . fromEntries (
95
- Object . entries ( schema ) . map ( ( [ key , value ] ) => [
96
- key ,
97
- applyExtraFromDescription ( value ) ,
98
- ] )
99
- ) ;
100
-
101
- if (
102
- "description" in output &&
103
- typeof output . description === "string" &&
104
- output . description . startsWith ( DESCRIPTION_PREFIX )
105
- ) {
106
- const strMeta = output . description . slice ( DESCRIPTION_PREFIX . length ) ;
107
- delete output . description ;
108
- Object . assign ( output , JSON . parse ( strMeta ) ) ;
109
- }
110
-
111
- return output ;
112
- }
113
-
114
- return schema as JsonSchema ;
115
- }
116
-
117
32
function toJsonSchema ( schema : z . ZodType ) : JsonSchema {
118
33
return applyExtraFromDescription ( _zodToJsonSchema ( schema ) ) as JsonSchema ;
119
34
}
@@ -127,7 +42,7 @@ export function getStateTypeSchema(graph: unknown): JsonSchema | undefined {
127
42
if ( ! isGraphWithZodLike ( graph ) ) return undefined ;
128
43
const schemaDef = graph . builder . _schemaRuntimeDefinition ;
129
44
if ( ! schemaDef ) return undefined ;
130
- return toJsonSchema ( applyPlugin ( schemaDef , { jsonSchemaExtra : true } ) ) ;
45
+ return toJsonSchema ( applyZodPlugin ( schemaDef , { jsonSchemaExtra : true } ) ) ;
131
46
}
132
47
133
48
/**
@@ -141,7 +56,7 @@ export function getUpdateTypeSchema(graph: unknown): JsonSchema | undefined {
141
56
if ( ! schemaDef ) return undefined ;
142
57
143
58
return toJsonSchema (
144
- applyPlugin ( schemaDef , {
59
+ applyZodPlugin ( schemaDef , {
145
60
reducer : true ,
146
61
jsonSchemaExtra : true ,
147
62
partial : true ,
@@ -164,7 +79,7 @@ export function getInputTypeSchema(graph: unknown): JsonSchema | undefined {
164
79
165
80
if ( ! schemaDef ) return undefined ;
166
81
return toJsonSchema (
167
- applyPlugin ( schemaDef , {
82
+ applyZodPlugin ( schemaDef , {
168
83
reducer : true ,
169
84
jsonSchemaExtra : true ,
170
85
partial : true ,
@@ -181,7 +96,7 @@ export function getOutputTypeSchema(graph: unknown): JsonSchema | undefined {
181
96
if ( ! isGraphWithZodLike ( graph ) ) return undefined ;
182
97
const schemaDef = graph . builder . _outputRuntimeDefinition ;
183
98
if ( ! schemaDef ) return undefined ;
184
- return toJsonSchema ( applyPlugin ( schemaDef , { jsonSchemaExtra : true } ) ) ;
99
+ return toJsonSchema ( applyZodPlugin ( schemaDef , { jsonSchemaExtra : true } ) ) ;
185
100
}
186
101
187
102
/**
@@ -193,5 +108,5 @@ export function getConfigTypeSchema(graph: unknown): JsonSchema | undefined {
193
108
if ( ! isGraphWithZodLike ( graph ) ) return undefined ;
194
109
const configDef = graph . builder . _configRuntimeSchema ;
195
110
if ( ! configDef ) return undefined ;
196
- return toJsonSchema ( applyPlugin ( configDef , { jsonSchemaExtra : true } ) ) ;
111
+ return toJsonSchema ( applyZodPlugin ( configDef , { jsonSchemaExtra : true } ) ) ;
197
112
}
0 commit comments