@@ -104,25 +104,52 @@ export function ensureLangGraphConfig(
104
104
*
105
105
* @returns a reference to the {@link BaseStore} that was set when the graph was initialized
106
106
*/
107
- export function getStore ( ) : BaseStore | undefined {
108
- const config : LangGraphRunnableConfig =
109
- AsyncLocalStorageProviderSingleton . getRunnableConfig ( ) ;
110
- return config ?. store ;
107
+ export function getStore (
108
+ config ?: LangGraphRunnableConfig
109
+ ) : BaseStore | undefined {
110
+ const runConfig : LangGraphRunnableConfig =
111
+ config ?? AsyncLocalStorageProviderSingleton . getRunnableConfig ( ) ;
112
+
113
+ if ( runConfig === undefined ) {
114
+ throw new Error (
115
+ [
116
+ "Config not retrievable. This is likely because you are running in an environment without support for AsyncLocalStorage." ,
117
+ "If you're running `getStore` in such environment, pass the `config` from the node function directly." ,
118
+ ] . join ( "\n" )
119
+ ) ;
120
+ }
121
+
122
+ return runConfig ?. store ;
111
123
}
112
124
113
125
/**
114
- * A helper utility function that returns the {@link LangGraphRunnableConfig#writer} if "custom" stream mode is enabled, otherwise undefined
126
+ * A helper utility function that returns the {@link LangGraphRunnableConfig#writer} if "custom" stream mode is enabled, otherwise undefined.
115
127
*
116
128
* @returns a reference to the {@link LangGraphRunnableConfig#writer} if "custom" stream mode is enabled, otherwise undefined
117
129
*/
118
- export function getWriter ( ) : ( ( chunk : unknown ) => void ) | undefined {
119
- const config : LangGraphRunnableConfig =
120
- AsyncLocalStorageProviderSingleton . getRunnableConfig ( ) ;
121
- return config ?. configurable ?. writer ;
130
+ export function getWriter (
131
+ config ?: LangGraphRunnableConfig
132
+ ) : ( ( chunk : unknown ) => void ) | undefined {
133
+ const runConfig : LangGraphRunnableConfig =
134
+ config ?? AsyncLocalStorageProviderSingleton . getRunnableConfig ( ) ;
135
+
136
+ if ( runConfig === undefined ) {
137
+ throw new Error (
138
+ [
139
+ "Config not retrievable. This is likely because you are running in an environment without support for AsyncLocalStorage." ,
140
+ "If you're running `getWriter` in such environment, pass the `config` from the node function directly." ,
141
+ ] . join ( "\n" )
142
+ ) ;
143
+ }
144
+
145
+ return runConfig ?. configurable ?. writer ;
122
146
}
123
147
124
148
/**
125
- * A helper utility function that returns the {@link LangGraphRunnableConfig} that was set when the graph was initialized
149
+ * A helper utility function that returns the {@link LangGraphRunnableConfig} that was set when the graph was initialized.
150
+ *
151
+ * Note: This only works when running in an environment that supports node:async_hooks and AsyncLocalStorage. If you're running this in a
152
+ * web environment, access the LangGraphRunnableConfig from the node function directly.
126
153
*
127
154
* @returns the {@link LangGraphRunnableConfig} that was set when the graph was initialized
128
155
*/
@@ -135,22 +162,29 @@ export function getConfig(): LangGraphRunnableConfig {
135
162
*
136
163
* @returns the input for the currently executing task
137
164
*/
138
- export function getCurrentTaskInput < T = unknown > ( ) : T {
139
- const config : LangGraphRunnableConfig =
140
- AsyncLocalStorageProviderSingleton . getRunnableConfig ( ) ;
141
- if ( config === undefined ) {
165
+ export function getCurrentTaskInput < T = unknown > (
166
+ config ?: LangGraphRunnableConfig
167
+ ) : T {
168
+ const runConfig : LangGraphRunnableConfig =
169
+ config ?? AsyncLocalStorageProviderSingleton . getRunnableConfig ( ) ;
170
+
171
+ if ( runConfig === undefined ) {
142
172
throw new Error (
143
- "Config not retrievable. This is likely because you are running in an environment without support for AsyncLocalStorage."
173
+ [
174
+ "Config not retrievable. This is likely because you are running in an environment without support for AsyncLocalStorage." ,
175
+ "If you're running `getCurrentTaskInput` in such environment, pass the `config` from the node function directly." ,
176
+ ] . join ( "\n" )
144
177
) ;
145
178
}
146
179
147
180
if (
148
- config . configurable ?. [ CONFIG_KEY_SCRATCHPAD ] ?. currentTaskInput === undefined
181
+ runConfig . configurable ?. [ CONFIG_KEY_SCRATCHPAD ] ?. currentTaskInput ===
182
+ undefined
149
183
) {
150
184
throw new Error ( "BUG: internal scratchpad not initialized." ) ;
151
185
}
152
186
153
- return config ! . configurable ! [ CONFIG_KEY_SCRATCHPAD ] ! . currentTaskInput as T ;
187
+ return runConfig ! . configurable ! [ CONFIG_KEY_SCRATCHPAD ] ! . currentTaskInput as T ;
154
188
}
155
189
156
190
export function recastCheckpointNamespace ( namespace : string ) : string {
0 commit comments