@langchain/langgraph 0.2.3 → 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 +1 -1
@@ -0,0 +1,155 @@
1
+ import { RUNTIME_PLACEHOLDER } from "../constants.js";
2
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
+ export class ManagedValue {
4
+ constructor(config, _params) {
5
+ Object.defineProperty(this, "runtime", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: false
10
+ });
11
+ Object.defineProperty(this, "config", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: void 0
16
+ });
17
+ Object.defineProperty(this, "_promises", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: []
22
+ });
23
+ Object.defineProperty(this, "lg_is_managed_value", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: true
28
+ });
29
+ this.config = config;
30
+ }
31
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
+ static async initialize(_config,
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ _args) {
35
+ throw new Error("Not implemented");
36
+ }
37
+ async promises() {
38
+ return Promise.all(this._promises);
39
+ }
40
+ addPromise(promise) {
41
+ this._promises.push(promise);
42
+ }
43
+ }
44
+ export class WritableManagedValue extends ManagedValue {
45
+ }
46
+ export const ChannelKeyPlaceholder = "__channel_key_placeholder__";
47
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ export class ManagedValueMapping extends Map {
49
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
50
+ constructor(entries) {
51
+ super(entries ? Array.from(entries) : undefined);
52
+ }
53
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
+ replaceRuntimeValues(step, values) {
55
+ if (this.size === 0 || !values) {
56
+ return;
57
+ }
58
+ if (Array.from(this.values()).every((mv) => !mv.runtime)) {
59
+ return;
60
+ }
61
+ if (typeof values === "object" && !Array.isArray(values)) {
62
+ for (const [key, value] of Object.entries(values)) {
63
+ for (const [chan, mv] of this.entries()) {
64
+ if (mv.runtime && mv.call(step) === value) {
65
+ // eslint-disable-next-line no-param-reassign
66
+ values[key] = { [RUNTIME_PLACEHOLDER]: chan };
67
+ }
68
+ }
69
+ }
70
+ }
71
+ else if (typeof values === "object" && "constructor" in values) {
72
+ for (const key of Object.getOwnPropertyNames(Object.getPrototypeOf(values))) {
73
+ try {
74
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
75
+ const value = values[key];
76
+ for (const [chan, mv] of this.entries()) {
77
+ if (mv.runtime && mv.call(step) === value) {
78
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, no-param-reassign
79
+ values[key] = { [RUNTIME_PLACEHOLDER]: chan };
80
+ }
81
+ }
82
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
83
+ }
84
+ catch (error) {
85
+ // Ignore if TypeError
86
+ if (error.name !== TypeError.name) {
87
+ throw error;
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+ replaceRuntimePlaceholders(step,
94
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
+ values) {
96
+ if (this.size === 0 || !values) {
97
+ return;
98
+ }
99
+ if (Array.from(this.values()).every((mv) => !mv.runtime)) {
100
+ return;
101
+ }
102
+ if (typeof values === "object" && !Array.isArray(values)) {
103
+ for (const [key, value] of Object.entries(values)) {
104
+ if (typeof value === "object" &&
105
+ value !== null &&
106
+ RUNTIME_PLACEHOLDER in value) {
107
+ const placeholder = value[RUNTIME_PLACEHOLDER];
108
+ if (typeof placeholder === "string") {
109
+ // eslint-disable-next-line no-param-reassign
110
+ values[key] = this.get(placeholder)?.call(step);
111
+ }
112
+ }
113
+ }
114
+ }
115
+ else if (typeof values === "object" && "constructor" in values) {
116
+ for (const key of Object.getOwnPropertyNames(Object.getPrototypeOf(values))) {
117
+ try {
118
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
119
+ const value = values[key];
120
+ if (typeof value === "object" &&
121
+ value !== null &&
122
+ RUNTIME_PLACEHOLDER in value) {
123
+ const managedValue = this.get(value[RUNTIME_PLACEHOLDER]);
124
+ if (managedValue) {
125
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, no-param-reassign
126
+ values[key] = managedValue.call(step);
127
+ }
128
+ }
129
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
130
+ }
131
+ catch (error) {
132
+ // Ignore if TypeError
133
+ if (error.name !== TypeError.name) {
134
+ throw error;
135
+ }
136
+ }
137
+ }
138
+ }
139
+ }
140
+ }
141
+ export function isManagedValue(value) {
142
+ if (typeof value === "object" && value && "lg_is_managed_value" in value) {
143
+ return true;
144
+ }
145
+ return false;
146
+ }
147
+ export function isConfiguredManagedValue(value) {
148
+ if (typeof value === "object" &&
149
+ value &&
150
+ "cls" in value &&
151
+ "params" in value) {
152
+ return true;
153
+ }
154
+ return false;
155
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./base.cjs"), exports);
18
+ __exportStar(require("./is_last_step.cjs"), exports);
19
+ __exportStar(require("./shared_value.cjs"), exports);
@@ -0,0 +1,3 @@
1
+ export * from "./base.js";
2
+ export * from "./is_last_step.js";
3
+ export * from "./shared_value.js";
@@ -0,0 +1,3 @@
1
+ export * from "./base.js";
2
+ export * from "./is_last_step.js";
3
+ export * from "./shared_value.js";
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IsLastStepManager = void 0;
4
+ const constants_js_1 = require("../constants.cjs");
5
+ const base_js_1 = require("./base.cjs");
6
+ class IsLastStepManager extends base_js_1.ManagedValue {
7
+ call(step) {
8
+ return step === (this.config.recursionLimit ?? constants_js_1.RECURSION_LIMIT_DEFAULT) - 1;
9
+ }
10
+ }
11
+ exports.IsLastStepManager = IsLastStepManager;
@@ -0,0 +1,4 @@
1
+ import { ManagedValue } from "./base.js";
2
+ export declare class IsLastStepManager extends ManagedValue<boolean> {
3
+ call(step: number): boolean;
4
+ }
@@ -0,0 +1,7 @@
1
+ import { RECURSION_LIMIT_DEFAULT } from "../constants.js";
2
+ import { ManagedValue } from "./base.js";
3
+ export class IsLastStepManager extends ManagedValue {
4
+ call(step) {
5
+ return step === (this.config.recursionLimit ?? RECURSION_LIMIT_DEFAULT) - 1;
6
+ }
7
+ }
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SharedValue = void 0;
4
+ const base_js_1 = require("./base.cjs");
5
+ const constants_js_1 = require("../constants.cjs");
6
+ const errors_js_1 = require("../errors.cjs");
7
+ class SharedValue extends base_js_1.WritableManagedValue {
8
+ constructor(config, params) {
9
+ super(config, params);
10
+ Object.defineProperty(this, "scope", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ Object.defineProperty(this, "store", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ Object.defineProperty(this, "ns", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ Object.defineProperty(this, "value", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: {}
33
+ });
34
+ this.scope = params.scope;
35
+ this.store = config.configurable?.[constants_js_1.CONFIG_KEY_STORE] || null;
36
+ if (!this.store) {
37
+ this.ns = null;
38
+ }
39
+ else if (config.configurable?.[this.scope]) {
40
+ const scopeValue = config.configurable[this.scope];
41
+ const scopedValueString = typeof scopeValue === "string"
42
+ ? scopeValue
43
+ : JSON.stringify(scopeValue);
44
+ this.ns = `scoped:${this.scope}:${params.key}:${scopedValueString}`;
45
+ }
46
+ else {
47
+ throw new Error(`Scope ${this.scope} for shared state key not in config.configurable`);
48
+ }
49
+ }
50
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
+ static async initialize(config, args) {
52
+ const instance = new this(config, args);
53
+ await instance.loadStore();
54
+ return instance;
55
+ }
56
+ static on(scope) {
57
+ return {
58
+ cls: SharedValue,
59
+ params: {
60
+ scope,
61
+ key: base_js_1.ChannelKeyPlaceholder,
62
+ },
63
+ };
64
+ }
65
+ call(_step) {
66
+ return { ...this.value };
67
+ }
68
+ processUpdate(values) {
69
+ const writes = [];
70
+ for (const vv of values) {
71
+ for (const [k, v] of Object.entries(vv)) {
72
+ if (v === null) {
73
+ if (k in this.value) {
74
+ delete this.value[k];
75
+ if (this.ns) {
76
+ writes.push([this.ns, k, null]);
77
+ }
78
+ }
79
+ }
80
+ else if (typeof v !== "object" || v === null) {
81
+ throw new errors_js_1.InvalidUpdateError("Received a non-object value");
82
+ }
83
+ else {
84
+ this.value[k] = v;
85
+ if (this.ns) {
86
+ writes.push([this.ns, k, v]);
87
+ }
88
+ }
89
+ }
90
+ }
91
+ return writes;
92
+ }
93
+ async update(values) {
94
+ if (!this.store) {
95
+ this.processUpdate(values);
96
+ }
97
+ else {
98
+ await this.store.put(this.processUpdate(values));
99
+ }
100
+ }
101
+ async loadStore() {
102
+ if (this.store && this.ns) {
103
+ const saved = await this.store.list([this.ns]);
104
+ this.value = saved[this.ns] || {};
105
+ }
106
+ return false;
107
+ }
108
+ }
109
+ exports.SharedValue = SharedValue;
@@ -0,0 +1,23 @@
1
+ import { RunnableConfig } from "@langchain/core/runnables";
2
+ import { BaseStore, type Values } from "../store/base.js";
3
+ import { ConfiguredManagedValue, ManagedValue, ManagedValueParams, WritableManagedValue } from "./base.js";
4
+ type Value = Record<string, Values>;
5
+ type Update = Record<string, Values | null>;
6
+ export interface SharedValueParams extends ManagedValueParams {
7
+ scope: string;
8
+ key: string;
9
+ }
10
+ export declare class SharedValue extends WritableManagedValue<Value, Update> {
11
+ scope: string;
12
+ store: BaseStore | null;
13
+ ns: string | null;
14
+ value: Value;
15
+ constructor(config: RunnableConfig, params: SharedValueParams);
16
+ static initialize<Value = any>(config: RunnableConfig, args: SharedValueParams): Promise<ManagedValue<Value>>;
17
+ static on(scope: string): ConfiguredManagedValue<Value>;
18
+ call(_step: number): Value;
19
+ private processUpdate;
20
+ update(values: Update[]): Promise<void>;
21
+ private loadStore;
22
+ }
23
+ export {};
@@ -0,0 +1,105 @@
1
+ import { ChannelKeyPlaceholder, WritableManagedValue, } from "./base.js";
2
+ import { CONFIG_KEY_STORE } from "../constants.js";
3
+ import { InvalidUpdateError } from "../errors.js";
4
+ export class SharedValue extends WritableManagedValue {
5
+ constructor(config, params) {
6
+ super(config, params);
7
+ Object.defineProperty(this, "scope", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: void 0
12
+ });
13
+ Object.defineProperty(this, "store", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "ns", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ Object.defineProperty(this, "value", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: {}
30
+ });
31
+ this.scope = params.scope;
32
+ this.store = config.configurable?.[CONFIG_KEY_STORE] || null;
33
+ if (!this.store) {
34
+ this.ns = null;
35
+ }
36
+ else if (config.configurable?.[this.scope]) {
37
+ const scopeValue = config.configurable[this.scope];
38
+ const scopedValueString = typeof scopeValue === "string"
39
+ ? scopeValue
40
+ : JSON.stringify(scopeValue);
41
+ this.ns = `scoped:${this.scope}:${params.key}:${scopedValueString}`;
42
+ }
43
+ else {
44
+ throw new Error(`Scope ${this.scope} for shared state key not in config.configurable`);
45
+ }
46
+ }
47
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ static async initialize(config, args) {
49
+ const instance = new this(config, args);
50
+ await instance.loadStore();
51
+ return instance;
52
+ }
53
+ static on(scope) {
54
+ return {
55
+ cls: SharedValue,
56
+ params: {
57
+ scope,
58
+ key: ChannelKeyPlaceholder,
59
+ },
60
+ };
61
+ }
62
+ call(_step) {
63
+ return { ...this.value };
64
+ }
65
+ processUpdate(values) {
66
+ const writes = [];
67
+ for (const vv of values) {
68
+ for (const [k, v] of Object.entries(vv)) {
69
+ if (v === null) {
70
+ if (k in this.value) {
71
+ delete this.value[k];
72
+ if (this.ns) {
73
+ writes.push([this.ns, k, null]);
74
+ }
75
+ }
76
+ }
77
+ else if (typeof v !== "object" || v === null) {
78
+ throw new InvalidUpdateError("Received a non-object value");
79
+ }
80
+ else {
81
+ this.value[k] = v;
82
+ if (this.ns) {
83
+ writes.push([this.ns, k, v]);
84
+ }
85
+ }
86
+ }
87
+ }
88
+ return writes;
89
+ }
90
+ async update(values) {
91
+ if (!this.store) {
92
+ this.processUpdate(values);
93
+ }
94
+ else {
95
+ await this.store.put(this.processUpdate(values));
96
+ }
97
+ }
98
+ async loadStore() {
99
+ if (this.store && this.ns) {
100
+ const saved = await this.store.list([this.ns]);
101
+ this.value = saved[this.ns] || {};
102
+ }
103
+ return false;
104
+ }
105
+ }