@langchain/langgraph 0.0.12 → 0.0.13

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 (134) hide show
  1. package/dist/channels/any_value.cjs +57 -0
  2. package/dist/channels/any_value.d.ts +16 -0
  3. package/dist/channels/any_value.js +53 -0
  4. package/dist/channels/base.cjs +19 -28
  5. package/dist/channels/base.d.ts +13 -19
  6. package/dist/channels/base.js +17 -24
  7. package/dist/channels/binop.cjs +4 -3
  8. package/dist/channels/binop.d.ts +1 -1
  9. package/dist/channels/binop.js +3 -2
  10. package/dist/channels/dynamic_barrier_value.cjs +88 -0
  11. package/dist/channels/dynamic_barrier_value.d.ts +26 -0
  12. package/dist/channels/dynamic_barrier_value.js +84 -0
  13. package/dist/channels/ephemeral_value.cjs +64 -0
  14. package/dist/channels/ephemeral_value.d.ts +14 -0
  15. package/dist/channels/ephemeral_value.js +60 -0
  16. package/dist/channels/index.cjs +1 -3
  17. package/dist/channels/index.d.ts +1 -1
  18. package/dist/channels/index.js +1 -1
  19. package/dist/channels/last_value.cjs +11 -5
  20. package/dist/channels/last_value.d.ts +5 -1
  21. package/dist/channels/last_value.js +9 -3
  22. package/dist/channels/named_barrier_value.cjs +71 -0
  23. package/dist/channels/named_barrier_value.d.ts +18 -0
  24. package/dist/channels/named_barrier_value.js +66 -0
  25. package/dist/channels/topic.cjs +5 -3
  26. package/dist/channels/topic.d.ts +3 -3
  27. package/dist/channels/topic.js +5 -3
  28. package/dist/checkpoint/base.cjs +30 -12
  29. package/dist/checkpoint/base.d.ts +39 -22
  30. package/dist/checkpoint/base.js +28 -11
  31. package/dist/checkpoint/id.cjs +40 -0
  32. package/dist/checkpoint/id.d.ts +2 -0
  33. package/dist/checkpoint/id.js +35 -0
  34. package/dist/checkpoint/index.cjs +2 -2
  35. package/dist/checkpoint/index.d.ts +2 -2
  36. package/dist/checkpoint/index.js +2 -2
  37. package/dist/checkpoint/memory.cjs +63 -49
  38. package/dist/checkpoint/memory.d.ts +7 -10
  39. package/dist/checkpoint/memory.js +62 -47
  40. package/dist/checkpoint/sqlite.cjs +170 -0
  41. package/dist/checkpoint/sqlite.d.ts +14 -0
  42. package/dist/checkpoint/sqlite.js +163 -0
  43. package/dist/constants.cjs +3 -1
  44. package/dist/constants.d.ts +2 -0
  45. package/dist/constants.js +2 -0
  46. package/dist/errors.cjs +31 -0
  47. package/dist/errors.d.ts +12 -0
  48. package/dist/errors.js +24 -0
  49. package/dist/graph/graph.cjs +235 -95
  50. package/dist/graph/graph.d.ts +52 -23
  51. package/dist/graph/graph.js +234 -96
  52. package/dist/graph/index.cjs +2 -2
  53. package/dist/graph/index.d.ts +2 -2
  54. package/dist/graph/index.js +2 -2
  55. package/dist/graph/message.cjs +4 -3
  56. package/dist/graph/message.d.ts +4 -1
  57. package/dist/graph/message.js +4 -3
  58. package/dist/graph/state.cjs +237 -102
  59. package/dist/graph/state.d.ts +41 -18
  60. package/dist/graph/state.js +238 -104
  61. package/dist/index.cjs +6 -2
  62. package/dist/index.d.ts +3 -2
  63. package/dist/index.js +2 -1
  64. package/dist/prebuilt/agent_executor.cjs +22 -36
  65. package/dist/prebuilt/agent_executor.d.ts +7 -10
  66. package/dist/prebuilt/agent_executor.js +23 -37
  67. package/dist/prebuilt/chat_agent_executor.cjs +13 -13
  68. package/dist/prebuilt/chat_agent_executor.d.ts +3 -1
  69. package/dist/prebuilt/chat_agent_executor.js +15 -15
  70. package/dist/prebuilt/index.cjs +4 -1
  71. package/dist/prebuilt/index.d.ts +1 -0
  72. package/dist/prebuilt/index.js +1 -0
  73. package/dist/prebuilt/tool_node.cjs +59 -0
  74. package/dist/prebuilt/tool_node.d.ts +17 -0
  75. package/dist/prebuilt/tool_node.js +54 -0
  76. package/dist/pregel/debug.cjs +6 -8
  77. package/dist/pregel/debug.d.ts +2 -2
  78. package/dist/pregel/debug.js +5 -7
  79. package/dist/pregel/index.cjs +406 -236
  80. package/dist/pregel/index.d.ts +77 -41
  81. package/dist/pregel/index.js +408 -241
  82. package/dist/pregel/io.cjs +117 -30
  83. package/dist/pregel/io.d.ts +11 -3
  84. package/dist/pregel/io.js +111 -28
  85. package/dist/pregel/read.cjs +126 -46
  86. package/dist/pregel/read.d.ts +27 -18
  87. package/dist/pregel/read.js +125 -45
  88. package/dist/pregel/types.cjs +2 -0
  89. package/dist/pregel/types.d.ts +32 -0
  90. package/dist/pregel/types.js +1 -0
  91. package/dist/pregel/validate.cjs +58 -51
  92. package/dist/pregel/validate.d.ts +14 -13
  93. package/dist/pregel/validate.js +56 -50
  94. package/dist/pregel/write.cjs +46 -30
  95. package/dist/pregel/write.d.ts +18 -8
  96. package/dist/pregel/write.js +45 -29
  97. package/dist/serde/base.cjs +2 -0
  98. package/dist/serde/base.d.ts +4 -0
  99. package/dist/serde/base.js +1 -0
  100. package/dist/setup/async_local_storage.cjs +2 -2
  101. package/dist/setup/async_local_storage.js +1 -1
  102. package/dist/tests/channels.test.d.ts +1 -0
  103. package/dist/tests/channels.test.js +151 -0
  104. package/dist/tests/chatbot.int.test.d.ts +1 -0
  105. package/dist/tests/chatbot.int.test.js +61 -0
  106. package/dist/tests/checkpoints.test.d.ts +1 -0
  107. package/dist/tests/checkpoints.test.js +190 -0
  108. package/dist/tests/graph.test.d.ts +1 -0
  109. package/dist/tests/graph.test.js +15 -0
  110. package/dist/tests/prebuilt.int.test.d.ts +1 -0
  111. package/dist/tests/prebuilt.int.test.js +101 -0
  112. package/dist/tests/prebuilt.test.d.ts +1 -0
  113. package/dist/tests/prebuilt.test.js +195 -0
  114. package/dist/tests/pregel.io.test.d.ts +1 -0
  115. package/dist/tests/pregel.io.test.js +332 -0
  116. package/dist/tests/pregel.read.test.d.ts +1 -0
  117. package/dist/tests/pregel.read.test.js +109 -0
  118. package/dist/tests/pregel.test.d.ts +1 -0
  119. package/dist/tests/pregel.test.js +1879 -0
  120. package/dist/tests/pregel.validate.test.d.ts +1 -0
  121. package/dist/tests/pregel.validate.test.js +198 -0
  122. package/dist/tests/pregel.write.test.d.ts +1 -0
  123. package/dist/tests/pregel.write.test.js +44 -0
  124. package/dist/tests/tracing.int.test.d.ts +1 -0
  125. package/dist/tests/tracing.int.test.js +449 -0
  126. package/dist/tests/utils.d.ts +22 -0
  127. package/dist/tests/utils.js +76 -0
  128. package/dist/utils.cjs +74 -0
  129. package/dist/utils.d.ts +18 -0
  130. package/dist/utils.js +70 -0
  131. package/package.json +11 -7
  132. package/dist/pregel/reserved.cjs +0 -6
  133. package/dist/pregel/reserved.d.ts +0 -3
  134. package/dist/pregel/reserved.js +0 -3
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AnyValue = void 0;
4
+ const errors_js_1 = require("../errors.cjs");
5
+ const index_js_1 = require("./index.cjs");
6
+ /**
7
+ * Stores the last value received, assumes that if multiple values are received, they are all equal.
8
+ *
9
+ * Note: Unlike 'LastValue' if multiple nodes write to this channel in a single step, the values
10
+ * will be continuously overwritten.
11
+ */
12
+ class AnyValue extends index_js_1.BaseChannel {
13
+ constructor() {
14
+ super();
15
+ Object.defineProperty(this, "lc_graph_name", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: "AnyValue"
20
+ });
21
+ Object.defineProperty(this, "value", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ this.value = undefined;
28
+ }
29
+ fromCheckpoint(checkpoint) {
30
+ const empty = new AnyValue();
31
+ if (checkpoint) {
32
+ empty.value = checkpoint;
33
+ }
34
+ return empty;
35
+ }
36
+ update(values) {
37
+ if (values.length === 0) {
38
+ this.value = undefined;
39
+ return;
40
+ }
41
+ // eslint-disable-next-line prefer-destructuring
42
+ this.value = values[values.length - 1];
43
+ }
44
+ get() {
45
+ if (this.value === undefined) {
46
+ throw new errors_js_1.EmptyChannelError();
47
+ }
48
+ return this.value;
49
+ }
50
+ checkpoint() {
51
+ if (this.value === undefined) {
52
+ throw new errors_js_1.EmptyChannelError();
53
+ }
54
+ return this.value;
55
+ }
56
+ }
57
+ exports.AnyValue = AnyValue;
@@ -0,0 +1,16 @@
1
+ import { BaseChannel } from "./index.js";
2
+ /**
3
+ * Stores the last value received, assumes that if multiple values are received, they are all equal.
4
+ *
5
+ * Note: Unlike 'LastValue' if multiple nodes write to this channel in a single step, the values
6
+ * will be continuously overwritten.
7
+ */
8
+ export declare class AnyValue<Value> extends BaseChannel<Value, Value, Value> {
9
+ lc_graph_name: string;
10
+ value: Value | undefined;
11
+ constructor();
12
+ fromCheckpoint(checkpoint?: Value): this;
13
+ update(values: Value[]): void;
14
+ get(): Value;
15
+ checkpoint(): Value;
16
+ }
@@ -0,0 +1,53 @@
1
+ import { EmptyChannelError } from "../errors.js";
2
+ import { BaseChannel } from "./index.js";
3
+ /**
4
+ * Stores the last value received, assumes that if multiple values are received, they are all equal.
5
+ *
6
+ * Note: Unlike 'LastValue' if multiple nodes write to this channel in a single step, the values
7
+ * will be continuously overwritten.
8
+ */
9
+ export class AnyValue extends BaseChannel {
10
+ constructor() {
11
+ super();
12
+ Object.defineProperty(this, "lc_graph_name", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: "AnyValue"
17
+ });
18
+ Object.defineProperty(this, "value", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ this.value = undefined;
25
+ }
26
+ fromCheckpoint(checkpoint) {
27
+ const empty = new AnyValue();
28
+ if (checkpoint) {
29
+ empty.value = checkpoint;
30
+ }
31
+ return empty;
32
+ }
33
+ update(values) {
34
+ if (values.length === 0) {
35
+ this.value = undefined;
36
+ return;
37
+ }
38
+ // eslint-disable-next-line prefer-destructuring
39
+ this.value = values[values.length - 1];
40
+ }
41
+ get() {
42
+ if (this.value === undefined) {
43
+ throw new EmptyChannelError();
44
+ }
45
+ return this.value;
46
+ }
47
+ checkpoint() {
48
+ if (this.value === undefined) {
49
+ throw new EmptyChannelError();
50
+ }
51
+ return this.value;
52
+ }
53
+ }
@@ -1,49 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createCheckpoint = exports.emptyChannels = exports.InvalidUpdateError = exports.EmptyChannelError = exports.BaseChannel = void 0;
3
+ exports.createCheckpoint = exports.emptyChannels = exports.BaseChannel = void 0;
4
+ const base_js_1 = require("../checkpoint/base.cjs");
5
+ const id_js_1 = require("../checkpoint/id.cjs");
6
+ const errors_js_1 = require("../errors.cjs");
4
7
  class BaseChannel {
5
8
  }
6
9
  exports.BaseChannel = BaseChannel;
7
- class EmptyChannelError extends Error {
8
- constructor(message) {
9
- super(message);
10
- this.name = "EmptyChannelError";
11
- }
12
- }
13
- exports.EmptyChannelError = EmptyChannelError;
14
- class InvalidUpdateError extends Error {
15
- constructor(message) {
16
- super(message);
17
- this.name = "InvalidUpdateError";
18
- }
19
- }
20
- exports.InvalidUpdateError = InvalidUpdateError;
21
10
  function emptyChannels(channels, checkpoint) {
22
11
  const newChannels = {};
23
12
  for (const k in channels) {
24
13
  if (Object.prototype.hasOwnProperty.call(channels, k)) {
25
- const channelValue = checkpoint.channelValues[k];
26
- newChannels[k] = channels[k].empty(channelValue);
14
+ const channelValue = checkpoint.channel_values[k];
15
+ newChannels[k] = channels[k].fromCheckpoint(channelValue);
27
16
  }
28
17
  }
29
18
  return newChannels;
30
19
  }
31
20
  exports.emptyChannels = emptyChannels;
32
- async function createCheckpoint(checkpoint, channels) {
33
- const newCheckpoint = {
34
- v: 1,
35
- ts: new Date().toISOString(),
36
- channelValues: { ...checkpoint.channelValues },
37
- channelVersions: { ...checkpoint.channelVersions },
38
- versionsSeen: { ...checkpoint.versionsSeen },
39
- };
21
+ function createCheckpoint(checkpoint, channels, step) {
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ const values = {};
40
24
  for (const k of Object.keys(channels)) {
41
25
  try {
42
- newCheckpoint.channelValues[k] = await channels[k].checkpoint();
26
+ values[k] = channels[k].checkpoint();
43
27
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
28
  }
45
29
  catch (error) {
46
- if (error.name === EmptyChannelError.name) {
30
+ if (error.name === errors_js_1.EmptyChannelError.name) {
47
31
  // no-op
48
32
  }
49
33
  else {
@@ -51,6 +35,13 @@ async function createCheckpoint(checkpoint, channels) {
51
35
  }
52
36
  }
53
37
  }
54
- return newCheckpoint;
38
+ return {
39
+ v: 1,
40
+ id: (0, id_js_1.uuid6)(step),
41
+ ts: new Date().toISOString(),
42
+ channel_values: values,
43
+ channel_versions: { ...checkpoint.channel_versions },
44
+ versions_seen: (0, base_js_1.deepCopy)(checkpoint.versions_seen),
45
+ };
55
46
  }
56
47
  exports.createCheckpoint = createCheckpoint;
@@ -1,6 +1,6 @@
1
+ import { ReadonlyCheckpoint } from "../checkpoint/base.js";
1
2
  import { Checkpoint } from "../checkpoint/index.js";
2
- export declare abstract class BaseChannel<Value = unknown, Update = unknown, // Expected type of the parameter `update` is called with.
3
- C = unknown> {
3
+ export declare abstract class BaseChannel<ValueType = unknown, UpdateType = unknown, CheckpointType = unknown> {
4
4
  /**
5
5
  * The name of the channel.
6
6
  */
@@ -9,40 +9,34 @@ C = unknown> {
9
9
  * Return a new identical channel, optionally initialized from a checkpoint.
10
10
  * Can be thought of as a "restoration" from a checkpoint which is a "snapshot" of the channel's state.
11
11
  *
12
- * @param {C | undefined} checkpoint
13
- * @param {C | undefined} initialValue
12
+ * @param {CheckpointType | undefined} checkpoint
13
+ * @param {CheckpointType | undefined} initialValue
14
14
  * @returns {this}
15
15
  */
16
- abstract empty(checkpoint?: C, initialValueFactory?: () => C): BaseChannel<Value, Update, C>;
16
+ abstract fromCheckpoint(checkpoint?: CheckpointType): this;
17
17
  /**
18
18
  * Update the channel's value with the given sequence of updates.
19
19
  * The order of the updates in the sequence is arbitrary.
20
20
  *
21
21
  * @throws {InvalidUpdateError} if the sequence of updates is invalid.
22
- * @param {Array<Update>} values
22
+ * @param {Array<UpdateType>} values
23
23
  * @returns {void}
24
24
  */
25
- abstract update(values: Update[]): void;
25
+ abstract update(values: UpdateType[]): void;
26
26
  /**
27
27
  * Return the current value of the channel.
28
28
  *
29
29
  * @throws {EmptyChannelError} if the channel is empty (never updated yet).
30
- * @returns {Value}
30
+ * @returns {ValueType}
31
31
  */
32
- abstract get(): Value;
32
+ abstract get(): ValueType;
33
33
  /**
34
34
  * Return a string representation of the channel's current state.
35
35
  *
36
36
  * @throws {EmptyChannelError} if the channel is empty (never updated yet), or doesn't support checkpoints.
37
- * @returns {C | undefined}
37
+ * @returns {CheckpointType | undefined}
38
38
  */
39
- abstract checkpoint(): C | undefined;
39
+ abstract checkpoint(): CheckpointType | undefined;
40
40
  }
41
- export declare class EmptyChannelError extends Error {
42
- constructor(message?: string);
43
- }
44
- export declare class InvalidUpdateError extends Error {
45
- constructor(message?: string);
46
- }
47
- export declare function emptyChannels(channels: Record<string, BaseChannel>, checkpoint: Checkpoint): Record<string, BaseChannel>;
48
- export declare function createCheckpoint<Value>(checkpoint: Checkpoint, channels: Record<string, BaseChannel<Value>>): Promise<Checkpoint>;
41
+ export declare function emptyChannels<Cc extends Record<string, BaseChannel>>(channels: Cc, checkpoint: ReadonlyCheckpoint): Cc;
42
+ export declare function createCheckpoint<ValueType>(checkpoint: ReadonlyCheckpoint, channels: Record<string, BaseChannel<ValueType>>, step: number): Checkpoint;
@@ -1,38 +1,24 @@
1
+ import { deepCopy } from "../checkpoint/base.js";
2
+ import { uuid6 } from "../checkpoint/id.js";
3
+ import { EmptyChannelError } from "../errors.js";
1
4
  export class BaseChannel {
2
5
  }
3
- export class EmptyChannelError extends Error {
4
- constructor(message) {
5
- super(message);
6
- this.name = "EmptyChannelError";
7
- }
8
- }
9
- export class InvalidUpdateError extends Error {
10
- constructor(message) {
11
- super(message);
12
- this.name = "InvalidUpdateError";
13
- }
14
- }
15
6
  export function emptyChannels(channels, checkpoint) {
16
7
  const newChannels = {};
17
8
  for (const k in channels) {
18
9
  if (Object.prototype.hasOwnProperty.call(channels, k)) {
19
- const channelValue = checkpoint.channelValues[k];
20
- newChannels[k] = channels[k].empty(channelValue);
10
+ const channelValue = checkpoint.channel_values[k];
11
+ newChannels[k] = channels[k].fromCheckpoint(channelValue);
21
12
  }
22
13
  }
23
14
  return newChannels;
24
15
  }
25
- export async function createCheckpoint(checkpoint, channels) {
26
- const newCheckpoint = {
27
- v: 1,
28
- ts: new Date().toISOString(),
29
- channelValues: { ...checkpoint.channelValues },
30
- channelVersions: { ...checkpoint.channelVersions },
31
- versionsSeen: { ...checkpoint.versionsSeen },
32
- };
16
+ export function createCheckpoint(checkpoint, channels, step) {
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ const values = {};
33
19
  for (const k of Object.keys(channels)) {
34
20
  try {
35
- newCheckpoint.channelValues[k] = await channels[k].checkpoint();
21
+ values[k] = channels[k].checkpoint();
36
22
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
23
  }
38
24
  catch (error) {
@@ -44,5 +30,12 @@ export async function createCheckpoint(checkpoint, channels) {
44
30
  }
45
31
  }
46
32
  }
47
- return newCheckpoint;
33
+ return {
34
+ v: 1,
35
+ id: uuid6(step),
36
+ ts: new Date().toISOString(),
37
+ channel_values: values,
38
+ channel_versions: { ...checkpoint.channel_versions },
39
+ versions_seen: deepCopy(checkpoint.versions_seen),
40
+ };
48
41
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BinaryOperatorAggregate = void 0;
4
+ const errors_js_1 = require("../errors.cjs");
4
5
  const index_js_1 = require("./index.cjs");
5
6
  /**
6
7
  * Stores the result of applying a binary operator to the current value and each new value.
@@ -36,7 +37,7 @@ class BinaryOperatorAggregate extends index_js_1.BaseChannel {
36
37
  this.initialValueFactory = initialValueFactory;
37
38
  this.value = initialValueFactory?.();
38
39
  }
39
- empty(checkpoint) {
40
+ fromCheckpoint(checkpoint) {
40
41
  const empty = new BinaryOperatorAggregate(this.operator, this.initialValueFactory);
41
42
  if (checkpoint) {
42
43
  empty.value = checkpoint;
@@ -59,13 +60,13 @@ class BinaryOperatorAggregate extends index_js_1.BaseChannel {
59
60
  }
60
61
  get() {
61
62
  if (this.value === undefined) {
62
- throw new index_js_1.EmptyChannelError();
63
+ throw new errors_js_1.EmptyChannelError();
63
64
  }
64
65
  return this.value;
65
66
  }
66
67
  checkpoint() {
67
68
  if (this.value === undefined) {
68
- throw new index_js_1.EmptyChannelError();
69
+ throw new errors_js_1.EmptyChannelError();
69
70
  }
70
71
  return this.value;
71
72
  }
@@ -9,7 +9,7 @@ export declare class BinaryOperatorAggregate<Value> extends BaseChannel<Value, V
9
9
  operator: BinaryOperator<Value>;
10
10
  initialValueFactory?: () => Value;
11
11
  constructor(operator: BinaryOperator<Value>, initialValueFactory?: () => Value);
12
- empty(checkpoint?: Value): BinaryOperatorAggregate<Value>;
12
+ fromCheckpoint(checkpoint?: Value): this;
13
13
  update(values: Value[]): void;
14
14
  get(): Value;
15
15
  checkpoint(): Value;
@@ -1,4 +1,5 @@
1
- import { BaseChannel, EmptyChannelError } from "./index.js";
1
+ import { EmptyChannelError } from "../errors.js";
2
+ import { BaseChannel } from "./index.js";
2
3
  /**
3
4
  * Stores the result of applying a binary operator to the current value and each new value.
4
5
  */
@@ -33,7 +34,7 @@ export class BinaryOperatorAggregate extends BaseChannel {
33
34
  this.initialValueFactory = initialValueFactory;
34
35
  this.value = initialValueFactory?.();
35
36
  }
36
- empty(checkpoint) {
37
+ fromCheckpoint(checkpoint) {
37
38
  const empty = new BinaryOperatorAggregate(this.operator, this.initialValueFactory);
38
39
  if (checkpoint) {
39
40
  empty.value = checkpoint;
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DynamicBarrierValue = void 0;
4
+ const errors_js_1 = require("../errors.cjs");
5
+ const index_js_1 = require("./index.cjs");
6
+ const named_barrier_value_js_1 = require("./named_barrier_value.cjs");
7
+ /**
8
+ A channel that switches between two states
9
+
10
+ - in the "priming" state it can't be read from.
11
+ - if it receives a WaitForNames update, it switches to the "waiting" state.
12
+ - in the "waiting" state it collects named values until all are received.
13
+ - once all named values are received, it can be read once, and it switches
14
+ back to the "priming" state.
15
+ */
16
+ class DynamicBarrierValue extends index_js_1.BaseChannel {
17
+ constructor() {
18
+ super();
19
+ Object.defineProperty(this, "lc_graph_name", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: "DynamicBarrierValue"
24
+ });
25
+ Object.defineProperty(this, "names", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: void 0
30
+ }); // Names of nodes that we want to wait for.
31
+ Object.defineProperty(this, "seen", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
37
+ this.names = undefined;
38
+ this.seen = new Set();
39
+ }
40
+ fromCheckpoint(checkpoint) {
41
+ const empty = new DynamicBarrierValue();
42
+ if (checkpoint) {
43
+ empty.names = new Set(checkpoint[0]);
44
+ empty.seen = new Set(checkpoint[1]);
45
+ }
46
+ return empty;
47
+ }
48
+ update(values) {
49
+ // switch to priming state after reading it once
50
+ if (this.names && (0, named_barrier_value_js_1.areSetsEqual)(this.names, this.seen)) {
51
+ this.seen = new Set();
52
+ this.names = undefined;
53
+ }
54
+ const newNames = values.filter((v) => typeof v === "object" &&
55
+ !!v &&
56
+ "__names" in v &&
57
+ Object.keys(v).join(",") === "__names" &&
58
+ Array.isArray(v.__names));
59
+ if (newNames.length > 1) {
60
+ throw new errors_js_1.InvalidUpdateError(`Expected at most one WaitForNames object, got ${newNames.length}`);
61
+ }
62
+ else if (newNames.length === 1) {
63
+ this.names = new Set(newNames[0].__names);
64
+ }
65
+ else if (this.names) {
66
+ for (const value of values) {
67
+ if (this.names.has(value)) {
68
+ this.seen.add(value);
69
+ }
70
+ else {
71
+ throw new errors_js_1.InvalidUpdateError(`Value ${value} not in names ${this.names}`);
72
+ }
73
+ }
74
+ }
75
+ }
76
+ // If we have not yet seen all the node names we want to wait for,
77
+ // throw an error to prevent continuing.
78
+ get() {
79
+ if (!this.names || !(0, named_barrier_value_js_1.areSetsEqual)(this.names, this.seen)) {
80
+ throw new errors_js_1.EmptyChannelError();
81
+ }
82
+ return undefined;
83
+ }
84
+ checkpoint() {
85
+ return [this.names ? [...this.names] : undefined, [...this.seen]];
86
+ }
87
+ }
88
+ exports.DynamicBarrierValue = DynamicBarrierValue;
@@ -0,0 +1,26 @@
1
+ import { BaseChannel } from "./index.js";
2
+ export interface WaitForNames<Value> {
3
+ __names: Value[];
4
+ }
5
+ /**
6
+ A channel that switches between two states
7
+
8
+ - in the "priming" state it can't be read from.
9
+ - if it receives a WaitForNames update, it switches to the "waiting" state.
10
+ - in the "waiting" state it collects named values until all are received.
11
+ - once all named values are received, it can be read once, and it switches
12
+ back to the "priming" state.
13
+ */
14
+ export declare class DynamicBarrierValue<Value> extends BaseChannel<void, Value | WaitForNames<Value>, [
15
+ Value[] | undefined,
16
+ Value[]
17
+ ]> {
18
+ lc_graph_name: string;
19
+ names?: Set<Value>;
20
+ seen: Set<Value>;
21
+ constructor();
22
+ fromCheckpoint(checkpoint?: [Value[] | undefined, Value[]]): this;
23
+ update(values: (Value | WaitForNames<Value>)[]): void;
24
+ get(): void;
25
+ checkpoint(): [Value[] | undefined, Value[]];
26
+ }
@@ -0,0 +1,84 @@
1
+ import { EmptyChannelError, InvalidUpdateError } from "../errors.js";
2
+ import { BaseChannel } from "./index.js";
3
+ import { areSetsEqual } from "./named_barrier_value.js";
4
+ /**
5
+ A channel that switches between two states
6
+
7
+ - in the "priming" state it can't be read from.
8
+ - if it receives a WaitForNames update, it switches to the "waiting" state.
9
+ - in the "waiting" state it collects named values until all are received.
10
+ - once all named values are received, it can be read once, and it switches
11
+ back to the "priming" state.
12
+ */
13
+ export class DynamicBarrierValue extends BaseChannel {
14
+ constructor() {
15
+ super();
16
+ Object.defineProperty(this, "lc_graph_name", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: "DynamicBarrierValue"
21
+ });
22
+ Object.defineProperty(this, "names", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ }); // Names of nodes that we want to wait for.
28
+ Object.defineProperty(this, "seen", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ this.names = undefined;
35
+ this.seen = new Set();
36
+ }
37
+ fromCheckpoint(checkpoint) {
38
+ const empty = new DynamicBarrierValue();
39
+ if (checkpoint) {
40
+ empty.names = new Set(checkpoint[0]);
41
+ empty.seen = new Set(checkpoint[1]);
42
+ }
43
+ return empty;
44
+ }
45
+ update(values) {
46
+ // switch to priming state after reading it once
47
+ if (this.names && areSetsEqual(this.names, this.seen)) {
48
+ this.seen = new Set();
49
+ this.names = undefined;
50
+ }
51
+ const newNames = values.filter((v) => typeof v === "object" &&
52
+ !!v &&
53
+ "__names" in v &&
54
+ Object.keys(v).join(",") === "__names" &&
55
+ Array.isArray(v.__names));
56
+ if (newNames.length > 1) {
57
+ throw new InvalidUpdateError(`Expected at most one WaitForNames object, got ${newNames.length}`);
58
+ }
59
+ else if (newNames.length === 1) {
60
+ this.names = new Set(newNames[0].__names);
61
+ }
62
+ else if (this.names) {
63
+ for (const value of values) {
64
+ if (this.names.has(value)) {
65
+ this.seen.add(value);
66
+ }
67
+ else {
68
+ throw new InvalidUpdateError(`Value ${value} not in names ${this.names}`);
69
+ }
70
+ }
71
+ }
72
+ }
73
+ // If we have not yet seen all the node names we want to wait for,
74
+ // throw an error to prevent continuing.
75
+ get() {
76
+ if (!this.names || !areSetsEqual(this.names, this.seen)) {
77
+ throw new EmptyChannelError();
78
+ }
79
+ return undefined;
80
+ }
81
+ checkpoint() {
82
+ return [this.names ? [...this.names] : undefined, [...this.seen]];
83
+ }
84
+ }
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EphemeralValue = void 0;
4
+ const errors_js_1 = require("../errors.cjs");
5
+ const index_js_1 = require("./index.cjs");
6
+ /**
7
+ * Stores the value received in the step immediately preceding, clears after.
8
+ */
9
+ class EphemeralValue extends index_js_1.BaseChannel {
10
+ constructor(guard = true) {
11
+ super();
12
+ Object.defineProperty(this, "lc_graph_name", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: "EphemeralValue"
17
+ });
18
+ Object.defineProperty(this, "guard", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "value", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ this.guard = guard;
31
+ }
32
+ fromCheckpoint(checkpoint) {
33
+ const empty = new EphemeralValue();
34
+ if (checkpoint) {
35
+ empty.value = checkpoint;
36
+ }
37
+ return empty;
38
+ }
39
+ update(values) {
40
+ if (values.length === 0) {
41
+ // If there are no updates for this specific channel at the end of the step, wipe it.
42
+ this.value = undefined;
43
+ return;
44
+ }
45
+ if (values.length !== 1 && this.guard) {
46
+ throw new errors_js_1.InvalidUpdateError("EphemeralValue can only receive one value per step.");
47
+ }
48
+ // eslint-disable-next-line prefer-destructuring
49
+ this.value = values[values.length - 1];
50
+ }
51
+ get() {
52
+ if (this.value === undefined) {
53
+ throw new errors_js_1.EmptyChannelError();
54
+ }
55
+ return this.value;
56
+ }
57
+ checkpoint() {
58
+ if (this.value === undefined) {
59
+ throw new errors_js_1.EmptyChannelError();
60
+ }
61
+ return this.value;
62
+ }
63
+ }
64
+ exports.EphemeralValue = EphemeralValue;
@@ -0,0 +1,14 @@
1
+ import { BaseChannel } from "./index.js";
2
+ /**
3
+ * Stores the value received in the step immediately preceding, clears after.
4
+ */
5
+ export declare class EphemeralValue<Value> extends BaseChannel<Value, Value, Value> {
6
+ lc_graph_name: string;
7
+ guard: boolean;
8
+ value?: Value;
9
+ constructor(guard?: boolean);
10
+ fromCheckpoint(checkpoint?: Value): this;
11
+ update(values: Value[]): void;
12
+ get(): Value;
13
+ checkpoint(): Value;
14
+ }