@langchain/langgraph 0.2.3 → 0.2.5-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/channels/base.cjs +16 -4
  2. package/dist/channels/base.d.ts +3 -0
  3. package/dist/channels/base.js +14 -3
  4. package/dist/constants.cjs +4 -1
  5. package/dist/constants.d.ts +3 -0
  6. package/dist/constants.js +3 -0
  7. package/dist/graph/annotation.cjs +5 -1
  8. package/dist/graph/annotation.d.ts +5 -4
  9. package/dist/graph/annotation.js +5 -1
  10. package/dist/graph/state.cjs +19 -7
  11. package/dist/graph/state.d.ts +13 -8
  12. package/dist/graph/state.js +18 -6
  13. package/dist/managed/base.cjs +163 -0
  14. package/dist/managed/base.d.ts +30 -0
  15. package/dist/managed/base.js +155 -0
  16. package/dist/managed/index.cjs +19 -0
  17. package/dist/managed/index.d.ts +3 -0
  18. package/dist/managed/index.js +3 -0
  19. package/dist/managed/is_last_step.cjs +11 -0
  20. package/dist/managed/is_last_step.d.ts +4 -0
  21. package/dist/managed/is_last_step.js +7 -0
  22. package/dist/managed/shared_value.cjs +109 -0
  23. package/dist/managed/shared_value.d.ts +23 -0
  24. package/dist/managed/shared_value.js +105 -0
  25. package/dist/prebuilt/agent_executor.d.ts +1 -1
  26. package/dist/pregel/algo.cjs +111 -50
  27. package/dist/pregel/algo.d.ts +7 -6
  28. package/dist/pregel/algo.js +112 -51
  29. package/dist/pregel/index.cjs +78 -6
  30. package/dist/pregel/index.d.ts +10 -2
  31. package/dist/pregel/index.js +80 -8
  32. package/dist/pregel/loop.cjs +40 -3
  33. package/dist/pregel/loop.d.ts +10 -0
  34. package/dist/pregel/loop.js +40 -3
  35. package/dist/pregel/types.d.ts +9 -2
  36. package/dist/pregel/validate.d.ts +3 -2
  37. package/dist/store/base.cjs +12 -0
  38. package/dist/store/base.d.ts +7 -0
  39. package/dist/store/base.js +8 -0
  40. package/dist/store/batch.cjs +126 -0
  41. package/dist/store/batch.d.ts +55 -0
  42. package/dist/store/batch.js +122 -0
  43. package/dist/store/index.cjs +19 -0
  44. package/dist/store/index.d.ts +3 -0
  45. package/dist/store/index.js +3 -0
  46. package/dist/store/memory.cjs +43 -0
  47. package/dist/store/memory.d.ts +6 -0
  48. package/dist/store/memory.js +39 -0
  49. package/dist/utils.cjs +26 -1
  50. package/dist/utils.d.ts +1 -0
  51. package/dist/utils.js +24 -0
  52. package/dist/web.cjs +2 -0
  53. package/dist/web.d.ts +2 -0
  54. package/dist/web.js +2 -0
  55. package/package.json +1 -1
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createCheckpoint = exports.emptyChannels = exports.BaseChannel = void 0;
3
+ exports.createCheckpoint = exports.emptyChannels = exports.BaseChannel = exports.isBaseChannel = void 0;
4
4
  const langgraph_checkpoint_1 = require("@langchain/langgraph-checkpoint");
5
5
  const errors_js_1 = require("../errors.cjs");
6
+ function isBaseChannel(obj) {
7
+ return obj != null && obj.lg_is_channel === true;
8
+ }
9
+ exports.isBaseChannel = isBaseChannel;
6
10
  class BaseChannel {
7
11
  constructor() {
8
12
  Object.defineProperty(this, "ValueType", {
@@ -17,6 +21,13 @@ class BaseChannel {
17
21
  writable: true,
18
22
  value: void 0
19
23
  });
24
+ /** @ignore */
25
+ Object.defineProperty(this, "lg_is_channel", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: true
30
+ });
20
31
  }
21
32
  /**
22
33
  * Mark the current value of the channel as consumed. By default, no-op.
@@ -29,11 +40,12 @@ class BaseChannel {
29
40
  }
30
41
  exports.BaseChannel = BaseChannel;
31
42
  function emptyChannels(channels, checkpoint) {
43
+ const filteredChannels = Object.fromEntries(Object.entries(channels).filter(([, value]) => isBaseChannel(value)));
32
44
  const newChannels = {};
33
- for (const k in channels) {
34
- if (Object.prototype.hasOwnProperty.call(channels, k)) {
45
+ for (const k in filteredChannels) {
46
+ if (Object.prototype.hasOwnProperty.call(filteredChannels, k)) {
35
47
  const channelValue = checkpoint.channel_values[k];
36
- newChannels[k] = channels[k].fromCheckpoint(channelValue);
48
+ newChannels[k] = filteredChannels[k].fromCheckpoint(channelValue);
37
49
  }
38
50
  }
39
51
  return newChannels;
@@ -1,4 +1,5 @@
1
1
  import { ReadonlyCheckpoint, Checkpoint } from "@langchain/langgraph-checkpoint";
2
+ export declare function isBaseChannel(obj: unknown): obj is BaseChannel;
2
3
  export declare abstract class BaseChannel<ValueType = unknown, UpdateType = unknown, CheckpointType = unknown> {
3
4
  ValueType: ValueType;
4
5
  UpdateType: UpdateType;
@@ -6,6 +7,8 @@ export declare abstract class BaseChannel<ValueType = unknown, UpdateType = unkn
6
7
  * The name of the channel.
7
8
  */
8
9
  abstract lc_graph_name: string;
10
+ /** @ignore */
11
+ lg_is_channel: boolean;
9
12
  /**
10
13
  * Return a new identical channel, optionally initialized from a checkpoint.
11
14
  * Can be thought of as a "restoration" from a checkpoint which is a "snapshot" of the channel's state.
@@ -1,5 +1,8 @@
1
1
  import { deepCopy, uuid6, } from "@langchain/langgraph-checkpoint";
2
2
  import { EmptyChannelError } from "../errors.js";
3
+ export function isBaseChannel(obj) {
4
+ return obj != null && obj.lg_is_channel === true;
5
+ }
3
6
  export class BaseChannel {
4
7
  constructor() {
5
8
  Object.defineProperty(this, "ValueType", {
@@ -14,6 +17,13 @@ export class BaseChannel {
14
17
  writable: true,
15
18
  value: void 0
16
19
  });
20
+ /** @ignore */
21
+ Object.defineProperty(this, "lg_is_channel", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: true
26
+ });
17
27
  }
18
28
  /**
19
29
  * Mark the current value of the channel as consumed. By default, no-op.
@@ -25,11 +35,12 @@ export class BaseChannel {
25
35
  }
26
36
  }
27
37
  export function emptyChannels(channels, checkpoint) {
38
+ const filteredChannels = Object.fromEntries(Object.entries(channels).filter(([, value]) => isBaseChannel(value)));
28
39
  const newChannels = {};
29
- for (const k in channels) {
30
- if (Object.prototype.hasOwnProperty.call(channels, k)) {
40
+ for (const k in filteredChannels) {
41
+ if (Object.prototype.hasOwnProperty.call(filteredChannels, k)) {
31
42
  const channelValue = checkpoint.channel_values[k];
32
- newChannels[k] = channels[k].fromCheckpoint(channelValue);
43
+ newChannels[k] = filteredChannels[k].fromCheckpoint(channelValue);
33
44
  }
34
45
  }
35
46
  return newChannels;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._isSend = exports.Send = exports._isSendInterface = exports.CHECKPOINT_NAMESPACE_SEPARATOR = exports.RESERVED = exports.TASK_NAMESPACE = exports.TASKS = exports.TAG_HIDDEN = exports.INTERRUPT = exports.CONFIG_KEY_RESUMING = exports.CONFIG_KEY_CHECKPOINTER = exports.CONFIG_KEY_READ = exports.CONFIG_KEY_SEND = exports.ERROR = exports.INPUT = void 0;
3
+ exports._isSend = exports.Send = exports._isSendInterface = exports.CHECKPOINT_NAMESPACE_SEPARATOR = exports.RESERVED = exports.TASK_NAMESPACE = exports.TASKS = exports.TAG_HIDDEN = exports.RECURSION_LIMIT_DEFAULT = exports.RUNTIME_PLACEHOLDER = exports.CONFIG_KEY_STORE = exports.INTERRUPT = exports.CONFIG_KEY_RESUMING = exports.CONFIG_KEY_CHECKPOINTER = exports.CONFIG_KEY_READ = exports.CONFIG_KEY_SEND = exports.ERROR = exports.INPUT = void 0;
4
4
  exports.INPUT = "__input__";
5
5
  exports.ERROR = "__error__";
6
6
  exports.CONFIG_KEY_SEND = "__pregel_send";
@@ -8,6 +8,9 @@ exports.CONFIG_KEY_READ = "__pregel_read";
8
8
  exports.CONFIG_KEY_CHECKPOINTER = "__pregel_checkpointer";
9
9
  exports.CONFIG_KEY_RESUMING = "__pregel_resuming";
10
10
  exports.INTERRUPT = "__interrupt__";
11
+ exports.CONFIG_KEY_STORE = "__pregel_store";
12
+ exports.RUNTIME_PLACEHOLDER = "__pregel_runtime_placeholder__";
13
+ exports.RECURSION_LIMIT_DEFAULT = 25;
11
14
  exports.TAG_HIDDEN = "langsmith:hidden";
12
15
  exports.TASKS = "__pregel_tasks";
13
16
  exports.TASK_NAMESPACE = "6ba7b831-9dad-11d1-80b4-00c04fd430c8";
@@ -5,6 +5,9 @@ export declare const CONFIG_KEY_READ = "__pregel_read";
5
5
  export declare const CONFIG_KEY_CHECKPOINTER = "__pregel_checkpointer";
6
6
  export declare const CONFIG_KEY_RESUMING = "__pregel_resuming";
7
7
  export declare const INTERRUPT = "__interrupt__";
8
+ export declare const CONFIG_KEY_STORE = "__pregel_store";
9
+ export declare const RUNTIME_PLACEHOLDER = "__pregel_runtime_placeholder__";
10
+ export declare const RECURSION_LIMIT_DEFAULT = 25;
8
11
  export declare const TAG_HIDDEN = "langsmith:hidden";
9
12
  export declare const TASKS = "__pregel_tasks";
10
13
  export declare const TASK_NAMESPACE = "6ba7b831-9dad-11d1-80b4-00c04fd430c8";
package/dist/constants.js CHANGED
@@ -5,6 +5,9 @@ export const CONFIG_KEY_READ = "__pregel_read";
5
5
  export const CONFIG_KEY_CHECKPOINTER = "__pregel_checkpointer";
6
6
  export const CONFIG_KEY_RESUMING = "__pregel_resuming";
7
7
  export const INTERRUPT = "__interrupt__";
8
+ export const CONFIG_KEY_STORE = "__pregel_store";
9
+ export const RUNTIME_PLACEHOLDER = "__pregel_runtime_placeholder__";
10
+ export const RECURSION_LIMIT_DEFAULT = 25;
8
11
  export const TAG_HIDDEN = "langsmith:hidden";
9
12
  export const TASKS = "__pregel_tasks";
10
13
  export const TASK_NAMESPACE = "6ba7b831-9dad-11d1-80b4-00c04fd430c8";
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getChannel = exports.Annotation = exports.AnnotationRoot = void 0;
4
4
  const binop_js_1 = require("../channels/binop.cjs");
5
5
  const last_value_js_1 = require("../channels/last_value.cjs");
6
+ const base_js_1 = require("../managed/base.cjs");
6
7
  /**
7
8
  * Should not be instantiated directly. See {@link Annotation}.
8
9
  * @internal
@@ -97,7 +98,10 @@ exports.AnnotationRoot = AnnotationRoot;
97
98
  * Helper function that instantiates a StateGraph state. See {@link Annotation} for usage.
98
99
  */
99
100
  exports.Annotation = function (annotation) {
100
- if (annotation) {
101
+ if ((0, base_js_1.isConfiguredManagedValue)(annotation)) {
102
+ return annotation;
103
+ }
104
+ else if (annotation) {
101
105
  return getChannel(annotation);
102
106
  }
103
107
  else {
@@ -2,6 +2,7 @@ import { RunnableLike } from "@langchain/core/runnables";
2
2
  import { BaseChannel } from "../channels/base.js";
3
3
  import { BinaryOperator, BinaryOperatorAggregate } from "../channels/binop.js";
4
4
  import { LastValue } from "../channels/last_value.js";
5
+ import { type ConfiguredManagedValue } from "../managed/base.js";
5
6
  export type SingleReducer<ValueType, UpdateType = ValueType> = {
6
7
  reducer: BinaryOperator<ValueType, UpdateType>;
7
8
  default?: () => ValueType;
@@ -13,17 +14,17 @@ export type SingleReducer<ValueType, UpdateType = ValueType> = {
13
14
  default?: () => ValueType;
14
15
  } | null;
15
16
  export interface StateDefinition {
16
- [key: string]: BaseChannel | (() => BaseChannel);
17
+ [key: string]: BaseChannel | (() => BaseChannel) | ConfiguredManagedValue;
17
18
  }
18
- type ExtractValueType<C> = C extends BaseChannel ? C["ValueType"] : C extends () => BaseChannel ? ReturnType<C>["ValueType"] : never;
19
- type ExtractUpdateType<C> = C extends BaseChannel ? C["UpdateType"] : C extends () => BaseChannel ? ReturnType<C>["UpdateType"] : never;
19
+ type ExtractValueType<C> = C extends BaseChannel ? C["ValueType"] : C extends () => BaseChannel ? ReturnType<C>["ValueType"] : C extends ConfiguredManagedValue<infer V> ? V : never;
20
+ type ExtractUpdateType<C> = C extends BaseChannel ? C["UpdateType"] : C extends () => BaseChannel ? ReturnType<C>["UpdateType"] : C extends ConfiguredManagedValue<infer V> ? V : never;
20
21
  export type StateType<SD extends StateDefinition> = {
21
22
  [key in keyof SD]: ExtractValueType<SD[key]>;
22
23
  };
23
24
  export type UpdateType<SD extends StateDefinition> = {
24
25
  [key in keyof SD]?: ExtractUpdateType<SD[key]>;
25
26
  };
26
- export type NodeType<SD extends StateDefinition> = RunnableLike<StateType<SD>, UpdateType<SD>>;
27
+ export type NodeType<SD extends StateDefinition> = RunnableLike<StateType<SD>, UpdateType<SD> | Partial<StateType<SD>>>;
27
28
  /** @ignore */
28
29
  export interface AnnotationFunction {
29
30
  <ValueType>(): LastValue<ValueType>;
@@ -1,5 +1,6 @@
1
1
  import { BinaryOperatorAggregate } from "../channels/binop.js";
2
2
  import { LastValue } from "../channels/last_value.js";
3
+ import { isConfiguredManagedValue, } from "../managed/base.js";
3
4
  /**
4
5
  * Should not be instantiated directly. See {@link Annotation}.
5
6
  * @internal
@@ -93,7 +94,10 @@ export class AnnotationRoot {
93
94
  * Helper function that instantiates a StateGraph state. See {@link Annotation} for usage.
94
95
  */
95
96
  export const Annotation = function (annotation) {
96
- if (annotation) {
97
+ if (isConfiguredManagedValue(annotation)) {
98
+ return annotation;
99
+ }
100
+ else if (annotation) {
97
101
  return getChannel(annotation);
98
102
  }
99
103
  else {
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CompiledStateGraph = exports.StateGraph = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-use-before-define */
5
5
  const runnables_1 = require("@langchain/core/runnables");
6
+ const base_js_1 = require("../channels/base.cjs");
6
7
  const graph_js_1 = require("./graph.cjs");
7
8
  const write_js_1 = require("../pregel/write.cjs");
8
9
  const read_js_1 = require("../pregel/read.cjs");
@@ -12,6 +13,7 @@ const utils_js_1 = require("../utils.cjs");
12
13
  const constants_js_1 = require("../constants.cjs");
13
14
  const errors_js_1 = require("../errors.cjs");
14
15
  const annotation_js_1 = require("./annotation.cjs");
16
+ const base_js_2 = require("../managed/base.cjs");
15
17
  const ROOT = "__root__";
16
18
  /**
17
19
  * A graph whose nodes communicate by reading and writing to a shared state.
@@ -76,7 +78,7 @@ const ROOT = "__root__";
76
78
  * ```
77
79
  */
78
80
  class StateGraph extends graph_js_1.Graph {
79
- constructor(fields) {
81
+ constructor(fields, configSchema) {
80
82
  super();
81
83
  Object.defineProperty(this, "channels", {
82
84
  enumerable: true,
@@ -122,6 +124,13 @@ class StateGraph extends graph_js_1.Graph {
122
124
  writable: true,
123
125
  value: new Map()
124
126
  });
127
+ /** @internal */
128
+ Object.defineProperty(this, "_configSchema", {
129
+ enumerable: true,
130
+ configurable: true,
131
+ writable: true,
132
+ value: void 0
133
+ });
125
134
  if (isStateGraphArgsWithInputOutputSchemas(fields)) {
126
135
  this._schemaDefinition = fields.input.spec;
127
136
  this._inputDefinition = fields.input.spec;
@@ -150,6 +159,7 @@ class StateGraph extends graph_js_1.Graph {
150
159
  this._addSchema(this._schemaDefinition);
151
160
  this._addSchema(this._inputDefinition);
152
161
  this._addSchema(this._outputDefinition);
162
+ this._configSchema = configSchema?.spec;
153
163
  }
154
164
  get allEdges() {
155
165
  return new Set([
@@ -173,7 +183,8 @@ class StateGraph extends graph_js_1.Graph {
173
183
  }
174
184
  if (this.channels[key] !== undefined) {
175
185
  if (this.channels[key] !== channel) {
176
- if (channel.lc_graph_name !== "LastValue") {
186
+ if (!(0, base_js_2.isConfiguredManagedValue)(channel) &&
187
+ channel.lc_graph_name !== "LastValue") {
177
188
  throw new Error(`Channel "${key}" already exists with a different type.`);
178
189
  }
179
190
  }
@@ -234,7 +245,7 @@ class StateGraph extends graph_js_1.Graph {
234
245
  this.waitingEdges.add([startKey, endKey]);
235
246
  return this;
236
247
  }
237
- compile({ checkpointer, interruptBefore, interruptAfter, } = {}) {
248
+ compile({ checkpointer, store, interruptBefore, interruptAfter, } = {}) {
238
249
  // validate the graph
239
250
  this.validate([
240
251
  ...(Array.isArray(interruptBefore) ? interruptBefore : []),
@@ -248,6 +259,9 @@ class StateGraph extends graph_js_1.Graph {
248
259
  // create empty compiled graph
249
260
  const compiled = new CompiledStateGraph({
250
261
  builder: this,
262
+ configKeys: this._configSchema
263
+ ? Object.keys(this._configSchema)
264
+ : undefined,
251
265
  checkpointer,
252
266
  interruptAfter,
253
267
  interruptBefore,
@@ -261,6 +275,7 @@ class StateGraph extends graph_js_1.Graph {
261
275
  outputChannels,
262
276
  streamChannels,
263
277
  streamMode: "updates",
278
+ store,
264
279
  });
265
280
  // attach nodes, edges and branches
266
281
  compiled.attachNode(graph_js_1.START);
@@ -424,15 +439,12 @@ class CompiledStateGraph extends graph_js_1.CompiledGraph {
424
439
  }
425
440
  }
426
441
  exports.CompiledStateGraph = CompiledStateGraph;
427
- function isBaseChannel(obj) {
428
- return obj != null && typeof obj.lc_graph_name === "string";
429
- }
430
442
  function isStateDefinition(obj) {
431
443
  return (typeof obj === "object" &&
432
444
  obj !== null &&
433
445
  !Array.isArray(obj) &&
434
446
  Object.keys(obj).length > 0 &&
435
- Object.values(obj).every((v) => typeof v === "function" || isBaseChannel(v)));
447
+ Object.values(obj).every((v) => typeof v === "function" || (0, base_js_1.isBaseChannel)(v)));
436
448
  }
437
449
  function isAnnotationRoot(obj) {
438
450
  return (typeof obj === "object" &&
@@ -4,6 +4,8 @@ import { BaseChannel } from "../channels/base.js";
4
4
  import { END, CompiledGraph, Graph, START, Branch, AddNodeOptions, NodeSpec } from "./graph.js";
5
5
  import { AnnotationRoot, SingleReducer, StateDefinition, StateType, UpdateType } from "./annotation.js";
6
6
  import type { RetryPolicy } from "../pregel/utils.js";
7
+ import { BaseStore } from "../store/base.js";
8
+ import { ManagedValueSpec } from "../managed/base.js";
7
9
  export type ChannelReducers<Channels extends object> = {
8
10
  [K in keyof Channels]: SingleReducer<Channels[K], any>;
9
11
  };
@@ -93,8 +95,8 @@ export type StateGraphArgsWithInputOutputSchemas<SD extends StateDefinition, O e
93
95
  * // }
94
96
  * ```
95
97
  */
96
- export declare class StateGraph<SD extends StateDefinition | unknown, S = SD extends StateDefinition ? StateType<SD> : SD, U = SD extends StateDefinition ? UpdateType<SD> : Partial<S>, N extends string = typeof START, I extends StateDefinition = SD extends StateDefinition ? SD : StateDefinition, O extends StateDefinition = SD extends StateDefinition ? SD : StateDefinition> extends Graph<N, S, U, StateGraphNodeSpec<S, U>> {
97
- channels: Record<string, BaseChannel>;
98
+ export declare class StateGraph<SD extends StateDefinition | unknown, S = SD extends StateDefinition ? StateType<SD> : SD, U = SD extends StateDefinition ? UpdateType<SD> : Partial<S>, N extends string = typeof START, I extends StateDefinition = SD extends StateDefinition ? SD : StateDefinition, O extends StateDefinition = SD extends StateDefinition ? SD : StateDefinition, C extends StateDefinition = StateDefinition> extends Graph<N, S, U, StateGraphNodeSpec<S, U>> {
99
+ channels: Record<string, BaseChannel | ManagedValueSpec>;
98
100
  waitingEdges: Set<[N[], N]>;
99
101
  /** @internal */
100
102
  _schemaDefinition: StateDefinition;
@@ -107,19 +109,22 @@ export declare class StateGraph<SD extends StateDefinition | unknown, S = SD ext
107
109
  * @internal
108
110
  */
109
111
  _schemaDefinitions: Map<any, any>;
110
- constructor(fields: SD extends StateDefinition ? SD | AnnotationRoot<SD> | StateGraphArgs<S> | StateGraphArgsWithStateSchema<SD, I, O> | StateGraphArgsWithInputOutputSchemas<SD, O> : StateGraphArgs<S>);
112
+ /** @internal */
113
+ _configSchema: C | undefined;
114
+ constructor(fields: SD extends StateDefinition ? SD | AnnotationRoot<SD> | StateGraphArgs<S> | StateGraphArgsWithStateSchema<SD, I, O> | StateGraphArgsWithInputOutputSchemas<SD, O> : StateGraphArgs<S>, configSchema?: AnnotationRoot<C>);
111
115
  get allEdges(): Set<[string, string]>;
112
116
  _addSchema(stateDefinition: StateDefinition): void;
113
- addNode<K extends string, NodeInput = S>(key: K, action: RunnableLike<NodeInput, U extends object ? U & Record<string, any> : U>, options?: StateGraphAddNodeOptions): StateGraph<SD, S, U, N | K, I, O>;
117
+ addNode<K extends string, NodeInput = S>(key: K, action: RunnableLike<NodeInput, U extends object ? U & Record<string, any> : U>, options?: StateGraphAddNodeOptions): StateGraph<SD, S, U, N | K, I, O, C>;
114
118
  addEdge(startKey: typeof START | N | N[], endKey: N | typeof END): this;
115
- compile({ checkpointer, interruptBefore, interruptAfter, }?: {
119
+ compile({ checkpointer, store, interruptBefore, interruptAfter, }?: {
116
120
  checkpointer?: BaseCheckpointSaver;
121
+ store?: BaseStore;
117
122
  interruptBefore?: N[] | All;
118
123
  interruptAfter?: N[] | All;
119
- }): CompiledStateGraph<S, U, N, I, O>;
124
+ }): CompiledStateGraph<S, U, N, I, O, C>;
120
125
  }
121
- export declare class CompiledStateGraph<S, U, N extends string = typeof START, I extends StateDefinition = StateDefinition, O extends StateDefinition = StateDefinition> extends CompiledGraph<N, S, U> {
122
- builder: StateGraph<unknown, S, U, N, I, O>;
126
+ export declare class CompiledStateGraph<S, U, N extends string = typeof START, I extends StateDefinition = StateDefinition, O extends StateDefinition = StateDefinition, C extends StateDefinition = StateDefinition> extends CompiledGraph<N, S, U> {
127
+ builder: StateGraph<unknown, S, U, N, I, O, C>;
123
128
  attachNode(key: typeof START, node?: never): void;
124
129
  attachNode(key: N, node: StateGraphNodeSpec<S, U>): void;
125
130
  attachEdge(start: N | N[] | "__start__", end: N | "__end__"): void;
@@ -1,5 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/no-use-before-define */
2
2
  import { _coerceToRunnable, } from "@langchain/core/runnables";
3
+ import { isBaseChannel } from "../channels/base.js";
3
4
  import { END, CompiledGraph, Graph, START, } from "./graph.js";
4
5
  import { ChannelWrite, PASSTHROUGH, SKIP_WRITE, } from "../pregel/write.js";
5
6
  import { ChannelRead, PregelNode } from "../pregel/read.js";
@@ -9,6 +10,7 @@ import { RunnableCallable } from "../utils.js";
9
10
  import { _isSend, CHECKPOINT_NAMESPACE_SEPARATOR, TAG_HIDDEN, } from "../constants.js";
10
11
  import { InvalidUpdateError } from "../errors.js";
11
12
  import { getChannel, } from "./annotation.js";
13
+ import { isConfiguredManagedValue } from "../managed/base.js";
12
14
  const ROOT = "__root__";
13
15
  /**
14
16
  * A graph whose nodes communicate by reading and writing to a shared state.
@@ -73,7 +75,7 @@ const ROOT = "__root__";
73
75
  * ```
74
76
  */
75
77
  export class StateGraph extends Graph {
76
- constructor(fields) {
78
+ constructor(fields, configSchema) {
77
79
  super();
78
80
  Object.defineProperty(this, "channels", {
79
81
  enumerable: true,
@@ -119,6 +121,13 @@ export class StateGraph extends Graph {
119
121
  writable: true,
120
122
  value: new Map()
121
123
  });
124
+ /** @internal */
125
+ Object.defineProperty(this, "_configSchema", {
126
+ enumerable: true,
127
+ configurable: true,
128
+ writable: true,
129
+ value: void 0
130
+ });
122
131
  if (isStateGraphArgsWithInputOutputSchemas(fields)) {
123
132
  this._schemaDefinition = fields.input.spec;
124
133
  this._inputDefinition = fields.input.spec;
@@ -147,6 +156,7 @@ export class StateGraph extends Graph {
147
156
  this._addSchema(this._schemaDefinition);
148
157
  this._addSchema(this._inputDefinition);
149
158
  this._addSchema(this._outputDefinition);
159
+ this._configSchema = configSchema?.spec;
150
160
  }
151
161
  get allEdges() {
152
162
  return new Set([
@@ -170,7 +180,8 @@ export class StateGraph extends Graph {
170
180
  }
171
181
  if (this.channels[key] !== undefined) {
172
182
  if (this.channels[key] !== channel) {
173
- if (channel.lc_graph_name !== "LastValue") {
183
+ if (!isConfiguredManagedValue(channel) &&
184
+ channel.lc_graph_name !== "LastValue") {
174
185
  throw new Error(`Channel "${key}" already exists with a different type.`);
175
186
  }
176
187
  }
@@ -231,7 +242,7 @@ export class StateGraph extends Graph {
231
242
  this.waitingEdges.add([startKey, endKey]);
232
243
  return this;
233
244
  }
234
- compile({ checkpointer, interruptBefore, interruptAfter, } = {}) {
245
+ compile({ checkpointer, store, interruptBefore, interruptAfter, } = {}) {
235
246
  // validate the graph
236
247
  this.validate([
237
248
  ...(Array.isArray(interruptBefore) ? interruptBefore : []),
@@ -245,6 +256,9 @@ export class StateGraph extends Graph {
245
256
  // create empty compiled graph
246
257
  const compiled = new CompiledStateGraph({
247
258
  builder: this,
259
+ configKeys: this._configSchema
260
+ ? Object.keys(this._configSchema)
261
+ : undefined,
248
262
  checkpointer,
249
263
  interruptAfter,
250
264
  interruptBefore,
@@ -258,6 +272,7 @@ export class StateGraph extends Graph {
258
272
  outputChannels,
259
273
  streamChannels,
260
274
  streamMode: "updates",
275
+ store,
261
276
  });
262
277
  // attach nodes, edges and branches
263
278
  compiled.attachNode(START);
@@ -419,9 +434,6 @@ export class CompiledStateGraph extends CompiledGraph {
419
434
  }
420
435
  }
421
436
  }
422
- function isBaseChannel(obj) {
423
- return obj != null && typeof obj.lc_graph_name === "string";
424
- }
425
437
  function isStateDefinition(obj) {
426
438
  return (typeof obj === "object" &&
427
439
  obj !== null &&
@@ -0,0 +1,163 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isConfiguredManagedValue = exports.isManagedValue = exports.ManagedValueMapping = exports.ChannelKeyPlaceholder = exports.WritableManagedValue = exports.ManagedValue = void 0;
4
+ const constants_js_1 = require("../constants.cjs");
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ class ManagedValue {
7
+ constructor(config, _params) {
8
+ Object.defineProperty(this, "runtime", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: false
13
+ });
14
+ Object.defineProperty(this, "config", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: void 0
19
+ });
20
+ Object.defineProperty(this, "_promises", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: []
25
+ });
26
+ Object.defineProperty(this, "lg_is_managed_value", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: true
31
+ });
32
+ this.config = config;
33
+ }
34
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
+ static async initialize(_config,
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ _args) {
38
+ throw new Error("Not implemented");
39
+ }
40
+ async promises() {
41
+ return Promise.all(this._promises);
42
+ }
43
+ addPromise(promise) {
44
+ this._promises.push(promise);
45
+ }
46
+ }
47
+ exports.ManagedValue = ManagedValue;
48
+ class WritableManagedValue extends ManagedValue {
49
+ }
50
+ exports.WritableManagedValue = WritableManagedValue;
51
+ exports.ChannelKeyPlaceholder = "__channel_key_placeholder__";
52
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
+ class ManagedValueMapping extends Map {
54
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
+ constructor(entries) {
56
+ super(entries ? Array.from(entries) : undefined);
57
+ }
58
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
+ replaceRuntimeValues(step, values) {
60
+ if (this.size === 0 || !values) {
61
+ return;
62
+ }
63
+ if (Array.from(this.values()).every((mv) => !mv.runtime)) {
64
+ return;
65
+ }
66
+ if (typeof values === "object" && !Array.isArray(values)) {
67
+ for (const [key, value] of Object.entries(values)) {
68
+ for (const [chan, mv] of this.entries()) {
69
+ if (mv.runtime && mv.call(step) === value) {
70
+ // eslint-disable-next-line no-param-reassign
71
+ values[key] = { [constants_js_1.RUNTIME_PLACEHOLDER]: chan };
72
+ }
73
+ }
74
+ }
75
+ }
76
+ else if (typeof values === "object" && "constructor" in values) {
77
+ for (const key of Object.getOwnPropertyNames(Object.getPrototypeOf(values))) {
78
+ try {
79
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
80
+ const value = values[key];
81
+ for (const [chan, mv] of this.entries()) {
82
+ if (mv.runtime && mv.call(step) === value) {
83
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, no-param-reassign
84
+ values[key] = { [constants_js_1.RUNTIME_PLACEHOLDER]: chan };
85
+ }
86
+ }
87
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
88
+ }
89
+ catch (error) {
90
+ // Ignore if TypeError
91
+ if (error.name !== TypeError.name) {
92
+ throw error;
93
+ }
94
+ }
95
+ }
96
+ }
97
+ }
98
+ replaceRuntimePlaceholders(step,
99
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
+ values) {
101
+ if (this.size === 0 || !values) {
102
+ return;
103
+ }
104
+ if (Array.from(this.values()).every((mv) => !mv.runtime)) {
105
+ return;
106
+ }
107
+ if (typeof values === "object" && !Array.isArray(values)) {
108
+ for (const [key, value] of Object.entries(values)) {
109
+ if (typeof value === "object" &&
110
+ value !== null &&
111
+ constants_js_1.RUNTIME_PLACEHOLDER in value) {
112
+ const placeholder = value[constants_js_1.RUNTIME_PLACEHOLDER];
113
+ if (typeof placeholder === "string") {
114
+ // eslint-disable-next-line no-param-reassign
115
+ values[key] = this.get(placeholder)?.call(step);
116
+ }
117
+ }
118
+ }
119
+ }
120
+ else if (typeof values === "object" && "constructor" in values) {
121
+ for (const key of Object.getOwnPropertyNames(Object.getPrototypeOf(values))) {
122
+ try {
123
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
124
+ const value = values[key];
125
+ if (typeof value === "object" &&
126
+ value !== null &&
127
+ constants_js_1.RUNTIME_PLACEHOLDER in value) {
128
+ const managedValue = this.get(value[constants_js_1.RUNTIME_PLACEHOLDER]);
129
+ if (managedValue) {
130
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, no-param-reassign
131
+ values[key] = managedValue.call(step);
132
+ }
133
+ }
134
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
135
+ }
136
+ catch (error) {
137
+ // Ignore if TypeError
138
+ if (error.name !== TypeError.name) {
139
+ throw error;
140
+ }
141
+ }
142
+ }
143
+ }
144
+ }
145
+ }
146
+ exports.ManagedValueMapping = ManagedValueMapping;
147
+ function isManagedValue(value) {
148
+ if (typeof value === "object" && value && "lg_is_managed_value" in value) {
149
+ return true;
150
+ }
151
+ return false;
152
+ }
153
+ exports.isManagedValue = isManagedValue;
154
+ function isConfiguredManagedValue(value) {
155
+ if (typeof value === "object" &&
156
+ value &&
157
+ "cls" in value &&
158
+ "params" in value) {
159
+ return true;
160
+ }
161
+ return false;
162
+ }
163
+ exports.isConfiguredManagedValue = isConfiguredManagedValue;
@@ -0,0 +1,30 @@
1
+ import { RunnableConfig } from "@langchain/core/runnables";
2
+ export interface ManagedValueParams extends Record<string, any> {
3
+ }
4
+ export declare abstract class ManagedValue<Value = any> {
5
+ runtime: boolean;
6
+ config: RunnableConfig;
7
+ private _promises;
8
+ lg_is_managed_value: boolean;
9
+ constructor(config: RunnableConfig, _params?: ManagedValueParams);
10
+ static initialize<Value = any>(_config: RunnableConfig, _args?: any): Promise<ManagedValue<Value>>;
11
+ abstract call(step: number): Value;
12
+ promises(): Promise<unknown>;
13
+ protected addPromise(promise: Promise<unknown>): void;
14
+ }
15
+ export declare abstract class WritableManagedValue<Value = any, Update = any> extends ManagedValue<Value> {
16
+ abstract update(writes: Update[]): Promise<void>;
17
+ }
18
+ export declare const ChannelKeyPlaceholder = "__channel_key_placeholder__";
19
+ export type ManagedValueSpec = typeof ManagedValue | ConfiguredManagedValue;
20
+ export interface ConfiguredManagedValue<Value = any> {
21
+ cls: typeof ManagedValue<Value>;
22
+ params: ManagedValueParams;
23
+ }
24
+ export declare class ManagedValueMapping extends Map<string, ManagedValue<any>> {
25
+ constructor(entries?: Iterable<[string, ManagedValue<any>]> | null);
26
+ replaceRuntimeValues(step: number, values: Record<string, any> | any): void;
27
+ replaceRuntimePlaceholders(step: number, values: Record<string, any> | any): void;
28
+ }
29
+ export declare function isManagedValue(value: unknown): value is typeof ManagedValue;
30
+ export declare function isConfiguredManagedValue(value: unknown): value is ConfiguredManagedValue;