@langchain/langgraph 0.2.3-rc.0 → 0.2.4

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 (54) 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 +7 -6
  11. package/dist/graph/state.d.ts +5 -2
  12. package/dist/graph/state.js +6 -5
  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/pregel/algo.cjs +111 -50
  26. package/dist/pregel/algo.d.ts +7 -6
  27. package/dist/pregel/algo.js +112 -51
  28. package/dist/pregel/index.cjs +65 -6
  29. package/dist/pregel/index.d.ts +9 -2
  30. package/dist/pregel/index.js +67 -8
  31. package/dist/pregel/loop.cjs +40 -3
  32. package/dist/pregel/loop.d.ts +10 -0
  33. package/dist/pregel/loop.js +40 -3
  34. package/dist/pregel/types.d.ts +8 -2
  35. package/dist/pregel/validate.d.ts +3 -2
  36. package/dist/store/base.cjs +12 -0
  37. package/dist/store/base.d.ts +7 -0
  38. package/dist/store/base.js +8 -0
  39. package/dist/store/batch.cjs +126 -0
  40. package/dist/store/batch.d.ts +55 -0
  41. package/dist/store/batch.js +122 -0
  42. package/dist/store/index.cjs +19 -0
  43. package/dist/store/index.d.ts +3 -0
  44. package/dist/store/index.js +3 -0
  45. package/dist/store/memory.cjs +43 -0
  46. package/dist/store/memory.d.ts +6 -0
  47. package/dist/store/memory.js +39 -0
  48. package/dist/utils.cjs +26 -1
  49. package/dist/utils.d.ts +1 -0
  50. package/dist/utils.js +24 -0
  51. package/dist/web.cjs +2 -0
  52. package/dist/web.d.ts +2 -0
  53. package/dist/web.js +2 -0
  54. package/package.json +8 -8
@@ -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.
@@ -173,7 +175,8 @@ class StateGraph extends graph_js_1.Graph {
173
175
  }
174
176
  if (this.channels[key] !== undefined) {
175
177
  if (this.channels[key] !== channel) {
176
- if (channel.lc_graph_name !== "LastValue") {
178
+ if (!(0, base_js_2.isConfiguredManagedValue)(channel) &&
179
+ channel.lc_graph_name !== "LastValue") {
177
180
  throw new Error(`Channel "${key}" already exists with a different type.`);
178
181
  }
179
182
  }
@@ -234,7 +237,7 @@ class StateGraph extends graph_js_1.Graph {
234
237
  this.waitingEdges.add([startKey, endKey]);
235
238
  return this;
236
239
  }
237
- compile({ checkpointer, interruptBefore, interruptAfter, } = {}) {
240
+ compile({ checkpointer, store, interruptBefore, interruptAfter, } = {}) {
238
241
  // validate the graph
239
242
  this.validate([
240
243
  ...(Array.isArray(interruptBefore) ? interruptBefore : []),
@@ -261,6 +264,7 @@ class StateGraph extends graph_js_1.Graph {
261
264
  outputChannels,
262
265
  streamChannels,
263
266
  streamMode: "updates",
267
+ store,
264
268
  });
265
269
  // attach nodes, edges and branches
266
270
  compiled.attachNode(graph_js_1.START);
@@ -424,15 +428,12 @@ class CompiledStateGraph extends graph_js_1.CompiledGraph {
424
428
  }
425
429
  }
426
430
  exports.CompiledStateGraph = CompiledStateGraph;
427
- function isBaseChannel(obj) {
428
- return obj != null && typeof obj.lc_graph_name === "string";
429
- }
430
431
  function isStateDefinition(obj) {
431
432
  return (typeof obj === "object" &&
432
433
  obj !== null &&
433
434
  !Array.isArray(obj) &&
434
435
  Object.keys(obj).length > 0 &&
435
- Object.values(obj).every((v) => typeof v === "function" || isBaseChannel(v)));
436
+ Object.values(obj).every((v) => typeof v === "function" || (0, base_js_1.isBaseChannel)(v)));
436
437
  }
437
438
  function isAnnotationRoot(obj) {
438
439
  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
  };
@@ -94,7 +96,7 @@ export type StateGraphArgsWithInputOutputSchemas<SD extends StateDefinition, O e
94
96
  * ```
95
97
  */
96
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> extends Graph<N, S, U, StateGraphNodeSpec<S, U>> {
97
- channels: Record<string, BaseChannel>;
99
+ channels: Record<string, BaseChannel | ManagedValueSpec>;
98
100
  waitingEdges: Set<[N[], N]>;
99
101
  /** @internal */
100
102
  _schemaDefinition: StateDefinition;
@@ -112,8 +114,9 @@ export declare class StateGraph<SD extends StateDefinition | unknown, S = SD ext
112
114
  _addSchema(stateDefinition: StateDefinition): void;
113
115
  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>;
114
116
  addEdge(startKey: typeof START | N | N[], endKey: N | typeof END): this;
115
- compile({ checkpointer, interruptBefore, interruptAfter, }?: {
117
+ compile({ checkpointer, store, interruptBefore, interruptAfter, }?: {
116
118
  checkpointer?: BaseCheckpointSaver;
119
+ store?: BaseStore;
117
120
  interruptBefore?: N[] | All;
118
121
  interruptAfter?: N[] | All;
119
122
  }): CompiledStateGraph<S, U, N, I, O>;
@@ -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.
@@ -170,7 +172,8 @@ export class StateGraph extends Graph {
170
172
  }
171
173
  if (this.channels[key] !== undefined) {
172
174
  if (this.channels[key] !== channel) {
173
- if (channel.lc_graph_name !== "LastValue") {
175
+ if (!isConfiguredManagedValue(channel) &&
176
+ channel.lc_graph_name !== "LastValue") {
174
177
  throw new Error(`Channel "${key}" already exists with a different type.`);
175
178
  }
176
179
  }
@@ -231,7 +234,7 @@ export class StateGraph extends Graph {
231
234
  this.waitingEdges.add([startKey, endKey]);
232
235
  return this;
233
236
  }
234
- compile({ checkpointer, interruptBefore, interruptAfter, } = {}) {
237
+ compile({ checkpointer, store, interruptBefore, interruptAfter, } = {}) {
235
238
  // validate the graph
236
239
  this.validate([
237
240
  ...(Array.isArray(interruptBefore) ? interruptBefore : []),
@@ -258,6 +261,7 @@ export class StateGraph extends Graph {
258
261
  outputChannels,
259
262
  streamChannels,
260
263
  streamMode: "updates",
264
+ store,
261
265
  });
262
266
  // attach nodes, edges and branches
263
267
  compiled.attachNode(START);
@@ -419,9 +423,6 @@ export class CompiledStateGraph extends CompiledGraph {
419
423
  }
420
424
  }
421
425
  }
422
- function isBaseChannel(obj) {
423
- return obj != null && typeof obj.lc_graph_name === "string";
424
- }
425
426
  function isStateDefinition(obj) {
426
427
  return (typeof obj === "object" &&
427
428
  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;