@langchain/langgraph-checkpoint 0.0.5 → 0.0.7

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.
@@ -4,101 +4,71 @@ exports.JsonPlusSerializer = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-explicit-any */
5
5
  /* eslint-disable no-instanceof/no-instanceof */
6
6
  const load_1 = require("@langchain/core/load");
7
- function isLangChainSerializable(value) {
8
- return (typeof value.lc_serializable === "boolean" && Array.isArray(value.lc_id));
9
- }
7
+ const index_js_1 = require("./utils/fast-safe-stringify/index.cjs");
10
8
  function isLangChainSerializedObject(value) {
11
9
  return (value !== null &&
12
10
  value.lc === 1 &&
13
11
  value.type === "constructor" &&
14
12
  Array.isArray(value.id));
15
13
  }
16
- const _serialize = (value, seen = new WeakSet()) => {
17
- const defaultValue = _default("", value);
18
- if (defaultValue === null) {
19
- return "null";
20
- }
21
- else if (typeof defaultValue === "string") {
22
- return JSON.stringify(defaultValue);
23
- }
24
- else if (typeof defaultValue === "number" ||
25
- typeof defaultValue === "boolean") {
26
- return defaultValue.toString();
27
- }
28
- else if (typeof defaultValue === "object") {
29
- if (seen.has(defaultValue)) {
30
- throw new TypeError("Circular reference detected");
31
- }
32
- seen.add(defaultValue);
33
- if (Array.isArray(defaultValue)) {
34
- const result = `[${defaultValue
35
- .map((item) => _serialize(item, seen))
36
- .join(",")}]`;
37
- seen.delete(defaultValue);
38
- return result;
39
- }
40
- else if (isLangChainSerializable(defaultValue)) {
41
- return JSON.stringify(defaultValue);
42
- }
43
- else {
44
- const entries = Object.entries(defaultValue).map(([k, v]) => `${JSON.stringify(k)}:${_serialize(v, seen)}`);
45
- const result = `{${entries.join(",")}}`;
46
- seen.delete(defaultValue);
47
- return result;
48
- }
49
- }
50
- // Only be reached for functions or symbols
51
- return JSON.stringify(defaultValue);
52
- };
14
+ /**
15
+ * The replacer in stringify does not allow delegation to built-in LangChain
16
+ * serialization methods, and instead immediately calls `.toJSON()` and
17
+ * continues to stringify subfields.
18
+ *
19
+ * We therefore must start from the most nested elements in the input and
20
+ * deserialize upwards rather than top-down.
21
+ */
53
22
  async function _reviver(value) {
54
23
  if (value && typeof value === "object") {
55
- if (value.lc === 2 && value.type === "undefined") {
56
- return undefined;
57
- }
58
- else if (value.lc === 2 &&
59
- value.type === "constructor" &&
60
- Array.isArray(value.id)) {
61
- try {
62
- const constructorName = value.id[value.id.length - 1];
63
- let constructor;
64
- switch (constructorName) {
65
- case "Set":
66
- constructor = Set;
67
- break;
68
- case "Map":
69
- constructor = Map;
70
- break;
71
- case "RegExp":
72
- constructor = RegExp;
73
- break;
74
- case "Error":
75
- constructor = Error;
76
- break;
77
- default:
78
- return value;
79
- }
80
- if (value.method) {
81
- return constructor[value.method](...(value.args || []));
82
- }
83
- else {
84
- return new constructor(...(value.args || []));
85
- }
86
- }
87
- catch (error) {
88
- return value;
89
- }
90
- }
91
- else if (isLangChainSerializedObject(value)) {
92
- return (0, load_1.load)(JSON.stringify(value));
93
- }
94
- else if (Array.isArray(value)) {
95
- return Promise.all(value.map((item) => _reviver(item)));
24
+ if (Array.isArray(value)) {
25
+ const revivedArray = await Promise.all(value.map((item) => _reviver(item)));
26
+ return revivedArray;
96
27
  }
97
28
  else {
98
29
  const revivedObj = {};
99
30
  for (const [k, v] of Object.entries(value)) {
100
31
  revivedObj[k] = await _reviver(v);
101
32
  }
33
+ if (revivedObj.lc === 2 && revivedObj.type === "undefined") {
34
+ return undefined;
35
+ }
36
+ else if (revivedObj.lc === 2 &&
37
+ revivedObj.type === "constructor" &&
38
+ Array.isArray(revivedObj.id)) {
39
+ try {
40
+ const constructorName = revivedObj.id[revivedObj.id.length - 1];
41
+ let constructor;
42
+ switch (constructorName) {
43
+ case "Set":
44
+ constructor = Set;
45
+ break;
46
+ case "Map":
47
+ constructor = Map;
48
+ break;
49
+ case "RegExp":
50
+ constructor = RegExp;
51
+ break;
52
+ case "Error":
53
+ constructor = Error;
54
+ break;
55
+ default:
56
+ return revivedObj;
57
+ }
58
+ if (revivedObj.method) {
59
+ return constructor[revivedObj.method](...(revivedObj.args || []));
60
+ }
61
+ else {
62
+ return new constructor(...(revivedObj.args || []));
63
+ }
64
+ }
65
+ catch (error) {
66
+ return revivedObj;
67
+ }
68
+ }
69
+ else if (isLangChainSerializedObject(revivedObj)) {
70
+ return (0, load_1.load)(JSON.stringify(revivedObj));
71
+ }
102
72
  return revivedObj;
103
73
  }
104
74
  }
@@ -116,7 +86,7 @@ constructor, method, args, kwargs) {
116
86
  kwargs: kwargs ?? {},
117
87
  };
118
88
  }
119
- function _default(_key, obj) {
89
+ function _default(obj) {
120
90
  if (obj === undefined) {
121
91
  return {
122
92
  lc: 2,
@@ -148,7 +118,9 @@ function _default(_key, obj) {
148
118
  class JsonPlusSerializer {
149
119
  _dumps(obj) {
150
120
  const encoder = new TextEncoder();
151
- return encoder.encode(_serialize(obj));
121
+ return encoder.encode((0, index_js_1.stringify)(obj, (_, value) => {
122
+ return _default(value);
123
+ }));
152
124
  }
153
125
  dumpsTyped(obj) {
154
126
  if (obj instanceof Uint8Array) {
@@ -1,101 +1,71 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  /* eslint-disable no-instanceof/no-instanceof */
3
3
  import { load } from "@langchain/core/load";
4
- function isLangChainSerializable(value) {
5
- return (typeof value.lc_serializable === "boolean" && Array.isArray(value.lc_id));
6
- }
4
+ import { stringify } from "./utils/fast-safe-stringify/index.js";
7
5
  function isLangChainSerializedObject(value) {
8
6
  return (value !== null &&
9
7
  value.lc === 1 &&
10
8
  value.type === "constructor" &&
11
9
  Array.isArray(value.id));
12
10
  }
13
- const _serialize = (value, seen = new WeakSet()) => {
14
- const defaultValue = _default("", value);
15
- if (defaultValue === null) {
16
- return "null";
17
- }
18
- else if (typeof defaultValue === "string") {
19
- return JSON.stringify(defaultValue);
20
- }
21
- else if (typeof defaultValue === "number" ||
22
- typeof defaultValue === "boolean") {
23
- return defaultValue.toString();
24
- }
25
- else if (typeof defaultValue === "object") {
26
- if (seen.has(defaultValue)) {
27
- throw new TypeError("Circular reference detected");
28
- }
29
- seen.add(defaultValue);
30
- if (Array.isArray(defaultValue)) {
31
- const result = `[${defaultValue
32
- .map((item) => _serialize(item, seen))
33
- .join(",")}]`;
34
- seen.delete(defaultValue);
35
- return result;
36
- }
37
- else if (isLangChainSerializable(defaultValue)) {
38
- return JSON.stringify(defaultValue);
39
- }
40
- else {
41
- const entries = Object.entries(defaultValue).map(([k, v]) => `${JSON.stringify(k)}:${_serialize(v, seen)}`);
42
- const result = `{${entries.join(",")}}`;
43
- seen.delete(defaultValue);
44
- return result;
45
- }
46
- }
47
- // Only be reached for functions or symbols
48
- return JSON.stringify(defaultValue);
49
- };
11
+ /**
12
+ * The replacer in stringify does not allow delegation to built-in LangChain
13
+ * serialization methods, and instead immediately calls `.toJSON()` and
14
+ * continues to stringify subfields.
15
+ *
16
+ * We therefore must start from the most nested elements in the input and
17
+ * deserialize upwards rather than top-down.
18
+ */
50
19
  async function _reviver(value) {
51
20
  if (value && typeof value === "object") {
52
- if (value.lc === 2 && value.type === "undefined") {
53
- return undefined;
54
- }
55
- else if (value.lc === 2 &&
56
- value.type === "constructor" &&
57
- Array.isArray(value.id)) {
58
- try {
59
- const constructorName = value.id[value.id.length - 1];
60
- let constructor;
61
- switch (constructorName) {
62
- case "Set":
63
- constructor = Set;
64
- break;
65
- case "Map":
66
- constructor = Map;
67
- break;
68
- case "RegExp":
69
- constructor = RegExp;
70
- break;
71
- case "Error":
72
- constructor = Error;
73
- break;
74
- default:
75
- return value;
76
- }
77
- if (value.method) {
78
- return constructor[value.method](...(value.args || []));
79
- }
80
- else {
81
- return new constructor(...(value.args || []));
82
- }
83
- }
84
- catch (error) {
85
- return value;
86
- }
87
- }
88
- else if (isLangChainSerializedObject(value)) {
89
- return load(JSON.stringify(value));
90
- }
91
- else if (Array.isArray(value)) {
92
- return Promise.all(value.map((item) => _reviver(item)));
21
+ if (Array.isArray(value)) {
22
+ const revivedArray = await Promise.all(value.map((item) => _reviver(item)));
23
+ return revivedArray;
93
24
  }
94
25
  else {
95
26
  const revivedObj = {};
96
27
  for (const [k, v] of Object.entries(value)) {
97
28
  revivedObj[k] = await _reviver(v);
98
29
  }
30
+ if (revivedObj.lc === 2 && revivedObj.type === "undefined") {
31
+ return undefined;
32
+ }
33
+ else if (revivedObj.lc === 2 &&
34
+ revivedObj.type === "constructor" &&
35
+ Array.isArray(revivedObj.id)) {
36
+ try {
37
+ const constructorName = revivedObj.id[revivedObj.id.length - 1];
38
+ let constructor;
39
+ switch (constructorName) {
40
+ case "Set":
41
+ constructor = Set;
42
+ break;
43
+ case "Map":
44
+ constructor = Map;
45
+ break;
46
+ case "RegExp":
47
+ constructor = RegExp;
48
+ break;
49
+ case "Error":
50
+ constructor = Error;
51
+ break;
52
+ default:
53
+ return revivedObj;
54
+ }
55
+ if (revivedObj.method) {
56
+ return constructor[revivedObj.method](...(revivedObj.args || []));
57
+ }
58
+ else {
59
+ return new constructor(...(revivedObj.args || []));
60
+ }
61
+ }
62
+ catch (error) {
63
+ return revivedObj;
64
+ }
65
+ }
66
+ else if (isLangChainSerializedObject(revivedObj)) {
67
+ return load(JSON.stringify(revivedObj));
68
+ }
99
69
  return revivedObj;
100
70
  }
101
71
  }
@@ -113,7 +83,7 @@ constructor, method, args, kwargs) {
113
83
  kwargs: kwargs ?? {},
114
84
  };
115
85
  }
116
- function _default(_key, obj) {
86
+ function _default(obj) {
117
87
  if (obj === undefined) {
118
88
  return {
119
89
  lc: 2,
@@ -145,7 +115,9 @@ function _default(_key, obj) {
145
115
  export class JsonPlusSerializer {
146
116
  _dumps(obj) {
147
117
  const encoder = new TextEncoder();
148
- return encoder.encode(_serialize(obj));
118
+ return encoder.encode(stringify(obj, (_, value) => {
119
+ return _default(value);
120
+ }));
149
121
  }
150
122
  dumpsTyped(obj) {
151
123
  if (obj instanceof Uint8Array) {
@@ -0,0 +1,222 @@
1
+ "use strict";
2
+ /* eslint-disable */
3
+ // @ts-nocheck
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.stringify = void 0;
6
+ // Stringify that can handle circular references.
7
+ // Inlined due to ESM import issues
8
+ // Source: https://www.npmjs.com/package/fast-safe-stringify
9
+ var LIMIT_REPLACE_NODE = "[...]";
10
+ var CIRCULAR_REPLACE_NODE = "[Circular]";
11
+ var arr = [];
12
+ var replacerStack = [];
13
+ function defaultOptions() {
14
+ return {
15
+ depthLimit: Number.MAX_SAFE_INTEGER,
16
+ edgesLimit: Number.MAX_SAFE_INTEGER,
17
+ };
18
+ }
19
+ // Regular stringify
20
+ function stringify(obj, replacer, spacer, options) {
21
+ if (typeof options === "undefined") {
22
+ options = defaultOptions();
23
+ }
24
+ decirc(obj, "", 0, [], undefined, 0, options);
25
+ var res;
26
+ try {
27
+ if (replacerStack.length === 0) {
28
+ res = JSON.stringify(obj, replacer, spacer);
29
+ }
30
+ else {
31
+ res = JSON.stringify(obj, replaceGetterValues(replacer), spacer);
32
+ }
33
+ }
34
+ catch (_) {
35
+ return JSON.stringify("[unable to serialize, circular reference is too complex to analyze]");
36
+ }
37
+ finally {
38
+ while (arr.length !== 0) {
39
+ var part = arr.pop();
40
+ if (part.length === 4) {
41
+ Object.defineProperty(part[0], part[1], part[3]);
42
+ }
43
+ else {
44
+ part[0][part[1]] = part[2];
45
+ }
46
+ }
47
+ }
48
+ return res;
49
+ }
50
+ exports.stringify = stringify;
51
+ function setReplace(replace, val, k, parent) {
52
+ var propertyDescriptor = Object.getOwnPropertyDescriptor(parent, k);
53
+ if (propertyDescriptor.get !== undefined) {
54
+ if (propertyDescriptor.configurable) {
55
+ Object.defineProperty(parent, k, { value: replace });
56
+ arr.push([parent, k, val, propertyDescriptor]);
57
+ }
58
+ else {
59
+ replacerStack.push([val, k, replace]);
60
+ }
61
+ }
62
+ else {
63
+ parent[k] = replace;
64
+ arr.push([parent, k, val]);
65
+ }
66
+ }
67
+ function decirc(val, k, edgeIndex, stack, parent, depth, options) {
68
+ depth += 1;
69
+ var i;
70
+ if (typeof val === "object" && val !== null) {
71
+ for (i = 0; i < stack.length; i++) {
72
+ if (stack[i] === val) {
73
+ setReplace(CIRCULAR_REPLACE_NODE, val, k, parent);
74
+ return;
75
+ }
76
+ }
77
+ if (typeof options.depthLimit !== "undefined" &&
78
+ depth > options.depthLimit) {
79
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent);
80
+ return;
81
+ }
82
+ if (typeof options.edgesLimit !== "undefined" &&
83
+ edgeIndex + 1 > options.edgesLimit) {
84
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent);
85
+ return;
86
+ }
87
+ stack.push(val);
88
+ // Optimize for Arrays. Big arrays could kill the performance otherwise!
89
+ if (Array.isArray(val)) {
90
+ for (i = 0; i < val.length; i++) {
91
+ decirc(val[i], i, i, stack, val, depth, options);
92
+ }
93
+ }
94
+ else {
95
+ var keys = Object.keys(val);
96
+ for (i = 0; i < keys.length; i++) {
97
+ var key = keys[i];
98
+ decirc(val[key], key, i, stack, val, depth, options);
99
+ }
100
+ }
101
+ stack.pop();
102
+ }
103
+ }
104
+ // Stable-stringify
105
+ function compareFunction(a, b) {
106
+ if (a < b) {
107
+ return -1;
108
+ }
109
+ if (a > b) {
110
+ return 1;
111
+ }
112
+ return 0;
113
+ }
114
+ function deterministicStringify(obj, replacer, spacer, options) {
115
+ if (typeof options === "undefined") {
116
+ options = defaultOptions();
117
+ }
118
+ var tmp = deterministicDecirc(obj, "", 0, [], undefined, 0, options) || obj;
119
+ var res;
120
+ try {
121
+ if (replacerStack.length === 0) {
122
+ res = JSON.stringify(tmp, replacer, spacer);
123
+ }
124
+ else {
125
+ res = JSON.stringify(tmp, replaceGetterValues(replacer), spacer);
126
+ }
127
+ }
128
+ catch (_) {
129
+ return JSON.stringify("[unable to serialize, circular reference is too complex to analyze]");
130
+ }
131
+ finally {
132
+ // Ensure that we restore the object as it was.
133
+ while (arr.length !== 0) {
134
+ var part = arr.pop();
135
+ if (part.length === 4) {
136
+ Object.defineProperty(part[0], part[1], part[3]);
137
+ }
138
+ else {
139
+ part[0][part[1]] = part[2];
140
+ }
141
+ }
142
+ }
143
+ return res;
144
+ }
145
+ function deterministicDecirc(val, k, edgeIndex, stack, parent, depth, options) {
146
+ depth += 1;
147
+ var i;
148
+ if (typeof val === "object" && val !== null) {
149
+ for (i = 0; i < stack.length; i++) {
150
+ if (stack[i] === val) {
151
+ setReplace(CIRCULAR_REPLACE_NODE, val, k, parent);
152
+ return;
153
+ }
154
+ }
155
+ try {
156
+ if (typeof val.toJSON === "function") {
157
+ return;
158
+ }
159
+ }
160
+ catch (_) {
161
+ return;
162
+ }
163
+ if (typeof options.depthLimit !== "undefined" &&
164
+ depth > options.depthLimit) {
165
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent);
166
+ return;
167
+ }
168
+ if (typeof options.edgesLimit !== "undefined" &&
169
+ edgeIndex + 1 > options.edgesLimit) {
170
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent);
171
+ return;
172
+ }
173
+ stack.push(val);
174
+ // Optimize for Arrays. Big arrays could kill the performance otherwise!
175
+ if (Array.isArray(val)) {
176
+ for (i = 0; i < val.length; i++) {
177
+ deterministicDecirc(val[i], i, i, stack, val, depth, options);
178
+ }
179
+ }
180
+ else {
181
+ // Create a temporary object in the required way
182
+ var tmp = {};
183
+ var keys = Object.keys(val).sort(compareFunction);
184
+ for (i = 0; i < keys.length; i++) {
185
+ var key = keys[i];
186
+ deterministicDecirc(val[key], key, i, stack, val, depth, options);
187
+ tmp[key] = val[key];
188
+ }
189
+ if (typeof parent !== "undefined") {
190
+ arr.push([parent, k, val]);
191
+ parent[k] = tmp;
192
+ }
193
+ else {
194
+ return tmp;
195
+ }
196
+ }
197
+ stack.pop();
198
+ }
199
+ }
200
+ // wraps replacer function to handle values we couldn't replace
201
+ // and mark them as replaced value
202
+ function replaceGetterValues(replacer) {
203
+ replacer =
204
+ typeof replacer !== "undefined"
205
+ ? replacer
206
+ : function (k, v) {
207
+ return v;
208
+ };
209
+ return function (key, val) {
210
+ if (replacerStack.length > 0) {
211
+ for (var i = 0; i < replacerStack.length; i++) {
212
+ var part = replacerStack[i];
213
+ if (part[1] === key && part[0] === val) {
214
+ val = part[2];
215
+ replacerStack.splice(i, 1);
216
+ break;
217
+ }
218
+ }
219
+ }
220
+ return replacer.call(this, key, val);
221
+ };
222
+ }
@@ -0,0 +1 @@
1
+ export declare function stringify(obj: any, replacer?: any, spacer?: any, options?: any): string;
@@ -0,0 +1,218 @@
1
+ /* eslint-disable */
2
+ // @ts-nocheck
3
+ // Stringify that can handle circular references.
4
+ // Inlined due to ESM import issues
5
+ // Source: https://www.npmjs.com/package/fast-safe-stringify
6
+ var LIMIT_REPLACE_NODE = "[...]";
7
+ var CIRCULAR_REPLACE_NODE = "[Circular]";
8
+ var arr = [];
9
+ var replacerStack = [];
10
+ function defaultOptions() {
11
+ return {
12
+ depthLimit: Number.MAX_SAFE_INTEGER,
13
+ edgesLimit: Number.MAX_SAFE_INTEGER,
14
+ };
15
+ }
16
+ // Regular stringify
17
+ export function stringify(obj, replacer, spacer, options) {
18
+ if (typeof options === "undefined") {
19
+ options = defaultOptions();
20
+ }
21
+ decirc(obj, "", 0, [], undefined, 0, options);
22
+ var res;
23
+ try {
24
+ if (replacerStack.length === 0) {
25
+ res = JSON.stringify(obj, replacer, spacer);
26
+ }
27
+ else {
28
+ res = JSON.stringify(obj, replaceGetterValues(replacer), spacer);
29
+ }
30
+ }
31
+ catch (_) {
32
+ return JSON.stringify("[unable to serialize, circular reference is too complex to analyze]");
33
+ }
34
+ finally {
35
+ while (arr.length !== 0) {
36
+ var part = arr.pop();
37
+ if (part.length === 4) {
38
+ Object.defineProperty(part[0], part[1], part[3]);
39
+ }
40
+ else {
41
+ part[0][part[1]] = part[2];
42
+ }
43
+ }
44
+ }
45
+ return res;
46
+ }
47
+ function setReplace(replace, val, k, parent) {
48
+ var propertyDescriptor = Object.getOwnPropertyDescriptor(parent, k);
49
+ if (propertyDescriptor.get !== undefined) {
50
+ if (propertyDescriptor.configurable) {
51
+ Object.defineProperty(parent, k, { value: replace });
52
+ arr.push([parent, k, val, propertyDescriptor]);
53
+ }
54
+ else {
55
+ replacerStack.push([val, k, replace]);
56
+ }
57
+ }
58
+ else {
59
+ parent[k] = replace;
60
+ arr.push([parent, k, val]);
61
+ }
62
+ }
63
+ function decirc(val, k, edgeIndex, stack, parent, depth, options) {
64
+ depth += 1;
65
+ var i;
66
+ if (typeof val === "object" && val !== null) {
67
+ for (i = 0; i < stack.length; i++) {
68
+ if (stack[i] === val) {
69
+ setReplace(CIRCULAR_REPLACE_NODE, val, k, parent);
70
+ return;
71
+ }
72
+ }
73
+ if (typeof options.depthLimit !== "undefined" &&
74
+ depth > options.depthLimit) {
75
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent);
76
+ return;
77
+ }
78
+ if (typeof options.edgesLimit !== "undefined" &&
79
+ edgeIndex + 1 > options.edgesLimit) {
80
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent);
81
+ return;
82
+ }
83
+ stack.push(val);
84
+ // Optimize for Arrays. Big arrays could kill the performance otherwise!
85
+ if (Array.isArray(val)) {
86
+ for (i = 0; i < val.length; i++) {
87
+ decirc(val[i], i, i, stack, val, depth, options);
88
+ }
89
+ }
90
+ else {
91
+ var keys = Object.keys(val);
92
+ for (i = 0; i < keys.length; i++) {
93
+ var key = keys[i];
94
+ decirc(val[key], key, i, stack, val, depth, options);
95
+ }
96
+ }
97
+ stack.pop();
98
+ }
99
+ }
100
+ // Stable-stringify
101
+ function compareFunction(a, b) {
102
+ if (a < b) {
103
+ return -1;
104
+ }
105
+ if (a > b) {
106
+ return 1;
107
+ }
108
+ return 0;
109
+ }
110
+ function deterministicStringify(obj, replacer, spacer, options) {
111
+ if (typeof options === "undefined") {
112
+ options = defaultOptions();
113
+ }
114
+ var tmp = deterministicDecirc(obj, "", 0, [], undefined, 0, options) || obj;
115
+ var res;
116
+ try {
117
+ if (replacerStack.length === 0) {
118
+ res = JSON.stringify(tmp, replacer, spacer);
119
+ }
120
+ else {
121
+ res = JSON.stringify(tmp, replaceGetterValues(replacer), spacer);
122
+ }
123
+ }
124
+ catch (_) {
125
+ return JSON.stringify("[unable to serialize, circular reference is too complex to analyze]");
126
+ }
127
+ finally {
128
+ // Ensure that we restore the object as it was.
129
+ while (arr.length !== 0) {
130
+ var part = arr.pop();
131
+ if (part.length === 4) {
132
+ Object.defineProperty(part[0], part[1], part[3]);
133
+ }
134
+ else {
135
+ part[0][part[1]] = part[2];
136
+ }
137
+ }
138
+ }
139
+ return res;
140
+ }
141
+ function deterministicDecirc(val, k, edgeIndex, stack, parent, depth, options) {
142
+ depth += 1;
143
+ var i;
144
+ if (typeof val === "object" && val !== null) {
145
+ for (i = 0; i < stack.length; i++) {
146
+ if (stack[i] === val) {
147
+ setReplace(CIRCULAR_REPLACE_NODE, val, k, parent);
148
+ return;
149
+ }
150
+ }
151
+ try {
152
+ if (typeof val.toJSON === "function") {
153
+ return;
154
+ }
155
+ }
156
+ catch (_) {
157
+ return;
158
+ }
159
+ if (typeof options.depthLimit !== "undefined" &&
160
+ depth > options.depthLimit) {
161
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent);
162
+ return;
163
+ }
164
+ if (typeof options.edgesLimit !== "undefined" &&
165
+ edgeIndex + 1 > options.edgesLimit) {
166
+ setReplace(LIMIT_REPLACE_NODE, val, k, parent);
167
+ return;
168
+ }
169
+ stack.push(val);
170
+ // Optimize for Arrays. Big arrays could kill the performance otherwise!
171
+ if (Array.isArray(val)) {
172
+ for (i = 0; i < val.length; i++) {
173
+ deterministicDecirc(val[i], i, i, stack, val, depth, options);
174
+ }
175
+ }
176
+ else {
177
+ // Create a temporary object in the required way
178
+ var tmp = {};
179
+ var keys = Object.keys(val).sort(compareFunction);
180
+ for (i = 0; i < keys.length; i++) {
181
+ var key = keys[i];
182
+ deterministicDecirc(val[key], key, i, stack, val, depth, options);
183
+ tmp[key] = val[key];
184
+ }
185
+ if (typeof parent !== "undefined") {
186
+ arr.push([parent, k, val]);
187
+ parent[k] = tmp;
188
+ }
189
+ else {
190
+ return tmp;
191
+ }
192
+ }
193
+ stack.pop();
194
+ }
195
+ }
196
+ // wraps replacer function to handle values we couldn't replace
197
+ // and mark them as replaced value
198
+ function replaceGetterValues(replacer) {
199
+ replacer =
200
+ typeof replacer !== "undefined"
201
+ ? replacer
202
+ : function (k, v) {
203
+ return v;
204
+ };
205
+ return function (key, val) {
206
+ if (replacerStack.length > 0) {
207
+ for (var i = 0; i < replacerStack.length; i++) {
208
+ var part = replacerStack[i];
209
+ if (part[1] === key && part[0] === val) {
210
+ val = part[2];
211
+ replacerStack.splice(i, 1);
212
+ break;
213
+ }
214
+ }
215
+ }
216
+ return replacer.call(this, key, val);
217
+ };
218
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/langgraph-checkpoint",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Library with base interfaces for LangGraph checkpoint savers.",
5
5
  "type": "module",
6
6
  "engines": {
@@ -34,7 +34,7 @@
34
34
  "uuid": "^10.0.0"
35
35
  },
36
36
  "peerDependencies": {
37
- "@langchain/core": ">=0.2.31 <0.3.0"
37
+ "@langchain/core": ">=0.2.31 <0.4.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@jest/globals": "^29.5.0",