Skip to content

Commit 52e56c1

Browse files
committed
fix(langgraph): allow passing LangGraphRunnableConfig as an secondary argument to getStore(), getWriter() and getCurrentTaskInput()
Closes `getCurrentTaskInput` Error #1198
1 parent 6f2a391 commit 52e56c1

File tree

1 file changed

+51
-17
lines changed

1 file changed

+51
-17
lines changed

libs/langgraph/src/pregel/utils/config.ts

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,52 @@ export function ensureLangGraphConfig(
104104
*
105105
* @returns a reference to the {@link BaseStore} that was set when the graph was initialized
106106
*/
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;
111123
}
112124

113125
/**
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.
115127
*
116128
* @returns a reference to the {@link LangGraphRunnableConfig#writer} if "custom" stream mode is enabled, otherwise undefined
117129
*/
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;
122146
}
123147

124148
/**
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.
126153
*
127154
* @returns the {@link LangGraphRunnableConfig} that was set when the graph was initialized
128155
*/
@@ -135,22 +162,29 @@ export function getConfig(): LangGraphRunnableConfig {
135162
*
136163
* @returns the input for the currently executing task
137164
*/
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) {
142172
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")
144177
);
145178
}
146179

147180
if (
148-
config.configurable?.[CONFIG_KEY_SCRATCHPAD]?.currentTaskInput === undefined
181+
runConfig.configurable?.[CONFIG_KEY_SCRATCHPAD]?.currentTaskInput ===
182+
undefined
149183
) {
150184
throw new Error("BUG: internal scratchpad not initialized.");
151185
}
152186

153-
return config!.configurable![CONFIG_KEY_SCRATCHPAD]!.currentTaskInput as T;
187+
return runConfig!.configurable![CONFIG_KEY_SCRATCHPAD]!.currentTaskInput as T;
154188
}
155189

156190
export function recastCheckpointNamespace(namespace: string): string {

0 commit comments

Comments
 (0)