Skip to content

Commit

Permalink
tidy up constants
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincburns committed Jan 28, 2025
1 parent d100e7e commit eaf1c85
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
10 changes: 8 additions & 2 deletions libs/langgraph/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions libs/langgraph/src/func/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -173,5 +178,5 @@ entrypoint.final = function final<ValueT, SaveT>({
export function getPreviousState<StateT>(): StateT {
const config: LangGraphRunnableConfig =
AsyncLocalStorageProviderSingleton.getRunnableConfig();
return config.configurable?.[CONFIG_KEY_PREVIOUS] as StateT;
return config.configurable?.[CONFIG_KEY_PREVIOUS_STATE] as StateT;
}
26 changes: 6 additions & 20 deletions libs/langgraph/src/pregel/algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]),
},
}
),
Expand Down Expand Up @@ -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]),
},
}
),
Expand Down Expand Up @@ -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]),
},
}
),
Expand Down
3 changes: 1 addition & 2 deletions libs/langgraph/src/pregel/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit eaf1c85

Please sign in to comment.