diff --git a/libs/langgraph/src/constants.ts b/libs/langgraph/src/constants.ts index 3f742391..334232e8 100644 --- a/libs/langgraph/src/constants.ts +++ b/libs/langgraph/src/constants.ts @@ -5,25 +5,31 @@ export const END = "__end__"; export const INPUT = "__input__"; export const ERROR = "__error__"; export const CONFIG_KEY_SEND = "__pregel_send"; +/** config key containing function used to call a node (push task) */ export const CONFIG_KEY_CALL = "__pregel_call"; export const CONFIG_KEY_READ = "__pregel_read"; export const CONFIG_KEY_CHECKPOINTER = "__pregel_checkpointer"; export const CONFIG_KEY_RESUMING = "__pregel_resuming"; export const CONFIG_KEY_TASK_ID = "__pregel_task_id"; export const CONFIG_KEY_STREAM = "__pregel_stream"; -export const CONFIG_KEY_GENERATOR_WRITER = "__pregel_generator_writer"; export const CONFIG_KEY_RESUME_VALUE = "__pregel_resume_value"; export const CONFIG_KEY_SCRATCHPAD = "__pregel_scratchpad"; -export const CONFIG_KEY_PREVIOUS = "__pregel_previous"; +/** config key containing state from previous invocation of graph for the given thread */ +export const CONFIG_KEY_PREVIOUS_STATE = "__pregel_previous"; export const CONFIG_KEY_CHECKPOINT_NS = "checkpoint_ns"; // this one is part of public API export const CONFIG_KEY_CHECKPOINT_MAP = "checkpoint_map"; +/** Special channel reserved for graph interrupts */ export const INTERRUPT = "__interrupt__"; +/** Special channel reserved for graph resume */ export const RESUME = "__resume__"; +/** Special channel reserved for cases when a task exits without any writes */ export const NO_WRITES = "__no_writes__"; +/** Special channel reserved for graph return */ export const RETURN = "__return__"; +/** Special channel reserved for graph previous state */ export const PREVIOUS = "__previous__"; export const RUNTIME_PLACEHOLDER = "__pregel_runtime_placeholder__"; export const RECURSION_LIMIT_DEFAULT = 25; diff --git a/libs/langgraph/src/func/index.ts b/libs/langgraph/src/func/index.ts index b721e97d..179ef930 100644 --- a/libs/langgraph/src/func/index.ts +++ b/libs/langgraph/src/func/index.ts @@ -5,7 +5,12 @@ import { import { AsyncLocalStorageProviderSingleton } from "@langchain/core/singletons"; import { Pregel } from "../pregel/index.js"; import { PregelNode } from "../pregel/read.js"; -import { CONFIG_KEY_PREVIOUS, END, PREVIOUS, START } from "../constants.js"; +import { + CONFIG_KEY_PREVIOUS_STATE, + END, + PREVIOUS, + START, +} from "../constants.js"; import { EphemeralValue } from "../channels/ephemeral_value.js"; import { call, getRunnableForEntrypoint } from "../pregel/call.js"; import { RetryPolicy } from "../pregel/utils/index.js"; @@ -173,5 +178,5 @@ entrypoint.final = function final({ export function getPreviousState(): StateT { const config: LangGraphRunnableConfig = AsyncLocalStorageProviderSingleton.getRunnableConfig(); - return config.configurable?.[CONFIG_KEY_PREVIOUS] as StateT; + return config.configurable?.[CONFIG_KEY_PREVIOUS_STATE] as StateT; } diff --git a/libs/langgraph/src/pregel/algo.ts b/libs/langgraph/src/pregel/algo.ts index 1629d97d..a3363157 100644 --- a/libs/langgraph/src/pregel/algo.ts +++ b/libs/langgraph/src/pregel/algo.ts @@ -49,9 +49,8 @@ import { RETURN, ERROR, NO_WRITES, - CONFIG_KEY_PREVIOUS, + CONFIG_KEY_PREVIOUS_STATE, PREVIOUS, - CONFIG_KEY_GENERATOR_WRITER, } from "../constants.js"; import { Call, @@ -644,14 +643,9 @@ export function _prepareSingleTask< ], id ), - [CONFIG_KEY_PREVIOUS]: checkpoint.channel_values[PREVIOUS], + [CONFIG_KEY_PREVIOUS_STATE]: checkpoint.channel_values[PREVIOUS], checkpoint_id: undefined, checkpoint_ns: taskCheckpointNamespace, - // Used by the functional API iff the entrypoint is a generator function. It's necessary to set it regardless - // of streammode, because we can't tell when initializing the graph whether the entrypoint is a function that - // returns a generator. - [CONFIG_KEY_GENERATOR_WRITER]: (chunk: unknown) => - extra.stream?.push([[], "custom", chunk]), }, } ), @@ -786,14 +780,10 @@ export function _prepareSingleTask< ], taskId ), - [CONFIG_KEY_PREVIOUS]: checkpoint.channel_values[PREVIOUS], + [CONFIG_KEY_PREVIOUS_STATE]: + checkpoint.channel_values[PREVIOUS], checkpoint_id: undefined, checkpoint_ns: taskCheckpointNamespace, - // Used by the functional API iff the entrypoint is a generator function. It's necessary to set it regardless - // of streammode, because we can't tell when initializing the graph whether the entrypoint is a function that - // returns a generator. - [CONFIG_KEY_GENERATOR_WRITER]: (chunk: unknown) => - extra.stream?.push([[], "custom", chunk]), }, } ), @@ -940,14 +930,10 @@ export function _prepareSingleTask< ], taskId ), - [CONFIG_KEY_PREVIOUS]: checkpoint.channel_values[PREVIOUS], + [CONFIG_KEY_PREVIOUS_STATE]: + checkpoint.channel_values[PREVIOUS], checkpoint_id: undefined, checkpoint_ns: taskCheckpointNamespace, - // Used by the functional API iff the entrypoint is a generator function. It's necessary to set it regardless - // of streammode, because we can't tell when initializing the graph whether the entrypoint is a function that - // returns a generator. - [CONFIG_KEY_GENERATOR_WRITER]: (chunk: unknown) => - extra.stream?.push([[], "custom", chunk]), }, } ), diff --git a/libs/langgraph/src/pregel/utils/config.ts b/libs/langgraph/src/pregel/utils/config.ts index c9ea1e82..0062d578 100644 --- a/libs/langgraph/src/pregel/utils/config.ts +++ b/libs/langgraph/src/pregel/utils/config.ts @@ -2,7 +2,6 @@ import { RunnableConfig } from "@langchain/core/runnables"; import { AsyncLocalStorageProviderSingleton } from "@langchain/core/singletons"; import { BaseStore } from "@langchain/langgraph-checkpoint"; import { LangGraphRunnableConfig } from "../runnable_types.js"; -import { CONFIG_KEY_GENERATOR_WRITER } from "../../constants.js"; const COPIABLE_KEYS = ["tags", "metadata", "callbacks", "configurable"]; @@ -114,7 +113,7 @@ export function getStore(): BaseStore | undefined { export function getWriter(): ((chunk: unknown) => void) | undefined { const config: LangGraphRunnableConfig = AsyncLocalStorageProviderSingleton.getRunnableConfig(); - return config?.configurable?.[CONFIG_KEY_GENERATOR_WRITER]; + return config?.configurable?.writer; } export function getConfig(): LangGraphRunnableConfig {