@langchain/langgraph 0.0.11 → 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.
- package/dist/channels/any_value.cjs +57 -0
- package/dist/channels/any_value.d.ts +16 -0
- package/dist/channels/any_value.js +53 -0
- package/dist/channels/base.cjs +19 -28
- package/dist/channels/base.d.ts +13 -19
- package/dist/channels/base.js +17 -24
- package/dist/channels/binop.cjs +4 -3
- package/dist/channels/binop.d.ts +1 -1
- package/dist/channels/binop.js +3 -2
- package/dist/channels/dynamic_barrier_value.cjs +88 -0
- package/dist/channels/dynamic_barrier_value.d.ts +26 -0
- package/dist/channels/dynamic_barrier_value.js +84 -0
- package/dist/channels/ephemeral_value.cjs +64 -0
- package/dist/channels/ephemeral_value.d.ts +14 -0
- package/dist/channels/ephemeral_value.js +60 -0
- package/dist/channels/index.cjs +1 -3
- package/dist/channels/index.d.ts +1 -1
- package/dist/channels/index.js +1 -1
- package/dist/channels/last_value.cjs +11 -5
- package/dist/channels/last_value.d.ts +5 -1
- package/dist/channels/last_value.js +9 -3
- package/dist/channels/named_barrier_value.cjs +71 -0
- package/dist/channels/named_barrier_value.d.ts +18 -0
- package/dist/channels/named_barrier_value.js +66 -0
- package/dist/channels/topic.cjs +5 -3
- package/dist/channels/topic.d.ts +3 -3
- package/dist/channels/topic.js +5 -3
- package/dist/checkpoint/base.cjs +30 -12
- package/dist/checkpoint/base.d.ts +39 -22
- package/dist/checkpoint/base.js +28 -11
- package/dist/checkpoint/id.cjs +40 -0
- package/dist/checkpoint/id.d.ts +2 -0
- package/dist/checkpoint/id.js +35 -0
- package/dist/checkpoint/index.cjs +2 -2
- package/dist/checkpoint/index.d.ts +2 -2
- package/dist/checkpoint/index.js +2 -2
- package/dist/checkpoint/memory.cjs +63 -49
- package/dist/checkpoint/memory.d.ts +7 -10
- package/dist/checkpoint/memory.js +62 -47
- package/dist/checkpoint/sqlite.cjs +170 -0
- package/dist/checkpoint/sqlite.d.ts +14 -0
- package/dist/checkpoint/sqlite.js +163 -0
- package/dist/constants.cjs +3 -1
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +2 -0
- package/dist/errors.cjs +31 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.js +24 -0
- package/dist/graph/graph.cjs +234 -96
- package/dist/graph/graph.d.ts +52 -23
- package/dist/graph/graph.js +233 -97
- package/dist/graph/index.cjs +2 -2
- package/dist/graph/index.d.ts +2 -2
- package/dist/graph/index.js +2 -2
- package/dist/graph/message.cjs +4 -3
- package/dist/graph/message.d.ts +4 -1
- package/dist/graph/message.js +4 -3
- package/dist/graph/state.cjs +237 -102
- package/dist/graph/state.d.ts +41 -18
- package/dist/graph/state.js +238 -104
- package/dist/index.cjs +6 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/prebuilt/agent_executor.cjs +22 -36
- package/dist/prebuilt/agent_executor.d.ts +7 -10
- package/dist/prebuilt/agent_executor.js +23 -37
- package/dist/prebuilt/chat_agent_executor.cjs +13 -13
- package/dist/prebuilt/chat_agent_executor.d.ts +3 -1
- package/dist/prebuilt/chat_agent_executor.js +15 -15
- package/dist/prebuilt/index.cjs +4 -1
- package/dist/prebuilt/index.d.ts +1 -0
- package/dist/prebuilt/index.js +1 -0
- package/dist/prebuilt/tool_node.cjs +59 -0
- package/dist/prebuilt/tool_node.d.ts +17 -0
- package/dist/prebuilt/tool_node.js +54 -0
- package/dist/pregel/debug.cjs +6 -8
- package/dist/pregel/debug.d.ts +2 -2
- package/dist/pregel/debug.js +5 -7
- package/dist/pregel/index.cjs +406 -236
- package/dist/pregel/index.d.ts +77 -41
- package/dist/pregel/index.js +408 -241
- package/dist/pregel/io.cjs +117 -30
- package/dist/pregel/io.d.ts +11 -3
- package/dist/pregel/io.js +111 -28
- package/dist/pregel/read.cjs +126 -46
- package/dist/pregel/read.d.ts +27 -18
- package/dist/pregel/read.js +125 -45
- package/dist/pregel/types.cjs +2 -0
- package/dist/pregel/types.d.ts +32 -0
- package/dist/pregel/types.js +1 -0
- package/dist/pregel/validate.cjs +58 -51
- package/dist/pregel/validate.d.ts +14 -13
- package/dist/pregel/validate.js +56 -50
- package/dist/pregel/write.cjs +46 -30
- package/dist/pregel/write.d.ts +18 -8
- package/dist/pregel/write.js +45 -29
- package/dist/serde/base.cjs +2 -0
- package/dist/serde/base.d.ts +4 -0
- package/dist/serde/base.js +1 -0
- package/dist/setup/async_local_storage.cjs +2 -2
- package/dist/setup/async_local_storage.js +1 -1
- package/dist/tests/channels.test.d.ts +1 -0
- package/dist/tests/channels.test.js +151 -0
- package/dist/tests/chatbot.int.test.d.ts +1 -0
- package/dist/tests/chatbot.int.test.js +61 -0
- package/dist/tests/checkpoints.test.d.ts +1 -0
- package/dist/tests/checkpoints.test.js +190 -0
- package/dist/tests/graph.test.d.ts +1 -0
- package/dist/tests/graph.test.js +15 -0
- package/dist/tests/prebuilt.int.test.d.ts +1 -0
- package/dist/tests/prebuilt.int.test.js +101 -0
- package/dist/tests/prebuilt.test.d.ts +1 -0
- package/dist/tests/prebuilt.test.js +195 -0
- package/dist/tests/pregel.io.test.d.ts +1 -0
- package/dist/tests/pregel.io.test.js +332 -0
- package/dist/tests/pregel.read.test.d.ts +1 -0
- package/dist/tests/pregel.read.test.js +109 -0
- package/dist/tests/pregel.test.d.ts +1 -0
- package/dist/tests/pregel.test.js +1879 -0
- package/dist/tests/pregel.validate.test.d.ts +1 -0
- package/dist/tests/pregel.validate.test.js +198 -0
- package/dist/tests/pregel.write.test.d.ts +1 -0
- package/dist/tests/pregel.write.test.js +44 -0
- package/dist/tests/tracing.int.test.d.ts +1 -0
- package/dist/tests/tracing.int.test.js +449 -0
- package/dist/tests/utils.d.ts +22 -0
- package/dist/tests/utils.js +76 -0
- package/dist/utils.cjs +74 -0
- package/dist/utils.d.ts +18 -0
- package/dist/utils.js +70 -0
- package/package.json +12 -8
- package/dist/pregel/reserved.cjs +0 -6
- package/dist/pregel/reserved.d.ts +0 -3
- package/dist/pregel/reserved.js +0 -3
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { v1 } from "uuid";
|
|
2
|
+
/**
|
|
3
|
+
* Returns an unsigned `x`-bit random integer.
|
|
4
|
+
* @param x - An unsigned integer ranging from 0 to 53, inclusive.
|
|
5
|
+
* @returns An unsigned `x`-bit random integer (`0 <= f(x) < 2^x`).
|
|
6
|
+
*/
|
|
7
|
+
function getRandomInt(x) {
|
|
8
|
+
if (x < 0 || x > 53) {
|
|
9
|
+
return NaN;
|
|
10
|
+
}
|
|
11
|
+
const n = 0 | (Math.random() * 0x40000000); // 1 << 30
|
|
12
|
+
return x > 30
|
|
13
|
+
? n + (0 | (Math.random() * (1 << (x - 30)))) * 0x40000000
|
|
14
|
+
: n >>> (30 - x);
|
|
15
|
+
}
|
|
16
|
+
export function uuid6(clockseq) {
|
|
17
|
+
const node = typeof crypto !== "undefined"
|
|
18
|
+
? crypto.getRandomValues(new Uint8Array(6))
|
|
19
|
+
: [
|
|
20
|
+
getRandomInt(8),
|
|
21
|
+
getRandomInt(8),
|
|
22
|
+
getRandomInt(8),
|
|
23
|
+
getRandomInt(8),
|
|
24
|
+
getRandomInt(8),
|
|
25
|
+
getRandomInt(8),
|
|
26
|
+
];
|
|
27
|
+
const uuid1 = v1({ node, clockseq });
|
|
28
|
+
return convert1to6(uuid1);
|
|
29
|
+
}
|
|
30
|
+
export function convert1to6(uuid1) {
|
|
31
|
+
// https://github.com/oittaa/uuid6-python/blob/main/src/uuid6/__init__.py#L81
|
|
32
|
+
const hex = uuid1.replace(/-/g, "");
|
|
33
|
+
const v6 = `${hex.slice(13, 16)}${hex.slice(8, 12)}${hex.slice(0, 1)}-${hex.slice(1, 5)}-6${hex.slice(5, 8)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
34
|
+
return v6;
|
|
35
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BaseCheckpointSaver = exports.emptyCheckpoint = exports.
|
|
3
|
+
exports.BaseCheckpointSaver = exports.emptyCheckpoint = exports.copyCheckpoint = exports.MemorySaver = void 0;
|
|
4
4
|
var memory_js_1 = require("./memory.cjs");
|
|
5
5
|
Object.defineProperty(exports, "MemorySaver", { enumerable: true, get: function () { return memory_js_1.MemorySaver; } });
|
|
6
|
-
Object.defineProperty(exports, "MemorySaverAssertImmutable", { enumerable: true, get: function () { return memory_js_1.MemorySaverAssertImmutable; } });
|
|
7
6
|
var base_js_1 = require("./base.cjs");
|
|
7
|
+
Object.defineProperty(exports, "copyCheckpoint", { enumerable: true, get: function () { return base_js_1.copyCheckpoint; } });
|
|
8
8
|
Object.defineProperty(exports, "emptyCheckpoint", { enumerable: true, get: function () { return base_js_1.emptyCheckpoint; } });
|
|
9
9
|
Object.defineProperty(exports, "BaseCheckpointSaver", { enumerable: true, get: function () { return base_js_1.BaseCheckpointSaver; } });
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { MemorySaver
|
|
2
|
-
export { type
|
|
1
|
+
export { MemorySaver } from "./memory.js";
|
|
2
|
+
export { type Checkpoint, type CheckpointMetadata, copyCheckpoint, emptyCheckpoint, BaseCheckpointSaver, } from "./base.js";
|
package/dist/checkpoint/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { MemorySaver
|
|
2
|
-
export { emptyCheckpoint, BaseCheckpointSaver, } from "./base.js";
|
|
1
|
+
export { MemorySaver } from "./memory.js";
|
|
2
|
+
export { copyCheckpoint, emptyCheckpoint, BaseCheckpointSaver, } from "./base.js";
|
|
@@ -1,65 +1,79 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.MemorySaver = void 0;
|
|
4
4
|
const base_js_1 = require("./base.cjs");
|
|
5
5
|
class MemorySaver extends base_js_1.BaseCheckpointSaver {
|
|
6
|
-
constructor() {
|
|
7
|
-
super(
|
|
6
|
+
constructor(serde) {
|
|
7
|
+
super(serde);
|
|
8
8
|
Object.defineProperty(this, "storage", {
|
|
9
9
|
enumerable: true,
|
|
10
10
|
configurable: true,
|
|
11
11
|
writable: true,
|
|
12
|
-
value:
|
|
12
|
+
value: void 0
|
|
13
13
|
});
|
|
14
|
+
this.storage = {};
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
16
|
+
async getTuple(config) {
|
|
17
|
+
const thread_id = config.configurable?.thread_id;
|
|
18
|
+
const checkpoint_id = config.configurable?.checkpoint_id;
|
|
19
|
+
const checkpoints = this.storage[thread_id];
|
|
20
|
+
if (checkpoint_id) {
|
|
21
|
+
const checkpoint = checkpoints[checkpoint_id];
|
|
22
|
+
if (checkpoint) {
|
|
23
|
+
return {
|
|
24
|
+
config,
|
|
25
|
+
checkpoint: this.serde.parse(checkpoint[0]),
|
|
26
|
+
metadata: this.serde.parse(checkpoint[1]),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
if (checkpoints) {
|
|
32
|
+
const maxThreadTs = Object.keys(checkpoints).sort((a, b) => b.localeCompare(a))[0];
|
|
33
|
+
const checkpoint = checkpoints[maxThreadTs];
|
|
34
|
+
return {
|
|
35
|
+
config: { configurable: { thread_id, checkpoint_id: maxThreadTs } },
|
|
36
|
+
checkpoint: this.serde.parse(checkpoint[0]),
|
|
37
|
+
metadata: this.serde.parse(checkpoint[1]),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
33
42
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
this.storageForCopies = {};
|
|
46
|
-
this.at = "end_of_step" /* CheckpointAt.END_OF_STEP */;
|
|
43
|
+
async *list(config) {
|
|
44
|
+
const thread_id = config.configurable?.thread_id;
|
|
45
|
+
const checkpoints = this.storage[thread_id] ?? {};
|
|
46
|
+
// sort in desc order
|
|
47
|
+
for (const [checkpoint_id, checkpoint] of Object.entries(checkpoints).sort((a, b) => b[0].localeCompare(a[0]))) {
|
|
48
|
+
yield {
|
|
49
|
+
config: { configurable: { thread_id, checkpoint_id } },
|
|
50
|
+
checkpoint: this.serde.parse(checkpoint[0]),
|
|
51
|
+
metadata: this.serde.parse(checkpoint[1]),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
47
54
|
}
|
|
48
|
-
put(config, checkpoint) {
|
|
49
|
-
const
|
|
50
|
-
if (
|
|
51
|
-
this.
|
|
55
|
+
async put(config, checkpoint, metadata) {
|
|
56
|
+
const thread_id = config.configurable?.thread_id;
|
|
57
|
+
if (this.storage[thread_id]) {
|
|
58
|
+
this.storage[thread_id][checkpoint.id] = [
|
|
59
|
+
this.serde.stringify(checkpoint),
|
|
60
|
+
this.serde.stringify(metadata),
|
|
61
|
+
];
|
|
52
62
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
63
|
+
else {
|
|
64
|
+
this.storage[thread_id] = {
|
|
65
|
+
[checkpoint.id]: [
|
|
66
|
+
this.serde.stringify(checkpoint),
|
|
67
|
+
this.serde.stringify(metadata),
|
|
68
|
+
],
|
|
69
|
+
};
|
|
60
70
|
}
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
return {
|
|
72
|
+
configurable: {
|
|
73
|
+
thread_id,
|
|
74
|
+
checkpoint_id: checkpoint.id,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
63
77
|
}
|
|
64
78
|
}
|
|
65
|
-
exports.
|
|
79
|
+
exports.MemorySaver = MemorySaver;
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { RunnableConfig } from "@langchain/core/runnables";
|
|
2
|
-
import { BaseCheckpointSaver, Checkpoint,
|
|
2
|
+
import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata, CheckpointTuple } from "./base.js";
|
|
3
|
+
import { SerializerProtocol } from "../serde/base.js";
|
|
3
4
|
export declare class MemorySaver extends BaseCheckpointSaver {
|
|
4
|
-
storage: Record<string,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export declare class MemorySaverAssertImmutable extends MemorySaver {
|
|
10
|
-
storageForCopies: Record<string, Record<string, Checkpoint>>;
|
|
11
|
-
constructor();
|
|
12
|
-
put(config: RunnableConfig, checkpoint: Checkpoint): void;
|
|
5
|
+
storage: Record<string, Record<string, [string, string]>>;
|
|
6
|
+
constructor(serde?: SerializerProtocol<unknown>);
|
|
7
|
+
getTuple(config: RunnableConfig): Promise<CheckpointTuple | undefined>;
|
|
8
|
+
list(config: RunnableConfig): AsyncGenerator<CheckpointTuple>;
|
|
9
|
+
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata): Promise<RunnableConfig>;
|
|
13
10
|
}
|
|
@@ -1,60 +1,75 @@
|
|
|
1
|
-
import { BaseCheckpointSaver,
|
|
1
|
+
import { BaseCheckpointSaver, } from "./base.js";
|
|
2
2
|
export class MemorySaver extends BaseCheckpointSaver {
|
|
3
|
-
constructor() {
|
|
4
|
-
super(
|
|
3
|
+
constructor(serde) {
|
|
4
|
+
super(serde);
|
|
5
5
|
Object.defineProperty(this, "storage", {
|
|
6
6
|
enumerable: true,
|
|
7
7
|
configurable: true,
|
|
8
8
|
writable: true,
|
|
9
|
-
value:
|
|
9
|
+
value: void 0
|
|
10
10
|
});
|
|
11
|
+
this.storage = {};
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
13
|
+
async getTuple(config) {
|
|
14
|
+
const thread_id = config.configurable?.thread_id;
|
|
15
|
+
const checkpoint_id = config.configurable?.checkpoint_id;
|
|
16
|
+
const checkpoints = this.storage[thread_id];
|
|
17
|
+
if (checkpoint_id) {
|
|
18
|
+
const checkpoint = checkpoints[checkpoint_id];
|
|
19
|
+
if (checkpoint) {
|
|
20
|
+
return {
|
|
21
|
+
config,
|
|
22
|
+
checkpoint: this.serde.parse(checkpoint[0]),
|
|
23
|
+
metadata: this.serde.parse(checkpoint[1]),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
if (checkpoints) {
|
|
29
|
+
const maxThreadTs = Object.keys(checkpoints).sort((a, b) => b.localeCompare(a))[0];
|
|
30
|
+
const checkpoint = checkpoints[maxThreadTs];
|
|
31
|
+
return {
|
|
32
|
+
config: { configurable: { thread_id, checkpoint_id: maxThreadTs } },
|
|
33
|
+
checkpoint: this.serde.parse(checkpoint[0]),
|
|
34
|
+
metadata: this.serde.parse(checkpoint[1]),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
30
39
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
Object.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this.at = "end_of_step" /* CheckpointAt.END_OF_STEP */;
|
|
40
|
+
async *list(config) {
|
|
41
|
+
const thread_id = config.configurable?.thread_id;
|
|
42
|
+
const checkpoints = this.storage[thread_id] ?? {};
|
|
43
|
+
// sort in desc order
|
|
44
|
+
for (const [checkpoint_id, checkpoint] of Object.entries(checkpoints).sort((a, b) => b[0].localeCompare(a[0]))) {
|
|
45
|
+
yield {
|
|
46
|
+
config: { configurable: { thread_id, checkpoint_id } },
|
|
47
|
+
checkpoint: this.serde.parse(checkpoint[0]),
|
|
48
|
+
metadata: this.serde.parse(checkpoint[1]),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
43
51
|
}
|
|
44
|
-
put(config, checkpoint) {
|
|
45
|
-
const
|
|
46
|
-
if (
|
|
47
|
-
this.
|
|
52
|
+
async put(config, checkpoint, metadata) {
|
|
53
|
+
const thread_id = config.configurable?.thread_id;
|
|
54
|
+
if (this.storage[thread_id]) {
|
|
55
|
+
this.storage[thread_id][checkpoint.id] = [
|
|
56
|
+
this.serde.stringify(checkpoint),
|
|
57
|
+
this.serde.stringify(metadata),
|
|
58
|
+
];
|
|
48
59
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
60
|
+
else {
|
|
61
|
+
this.storage[thread_id] = {
|
|
62
|
+
[checkpoint.id]: [
|
|
63
|
+
this.serde.stringify(checkpoint),
|
|
64
|
+
this.serde.stringify(metadata),
|
|
65
|
+
],
|
|
66
|
+
};
|
|
56
67
|
}
|
|
57
|
-
|
|
58
|
-
|
|
68
|
+
return {
|
|
69
|
+
configurable: {
|
|
70
|
+
thread_id,
|
|
71
|
+
checkpoint_id: checkpoint.id,
|
|
72
|
+
},
|
|
73
|
+
};
|
|
59
74
|
}
|
|
60
75
|
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SqliteSaver = void 0;
|
|
7
|
+
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
|
8
|
+
const base_js_1 = require("./base.cjs");
|
|
9
|
+
class SqliteSaver extends base_js_1.BaseCheckpointSaver {
|
|
10
|
+
constructor(db, serde) {
|
|
11
|
+
super(serde);
|
|
12
|
+
Object.defineProperty(this, "db", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: void 0
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "isSetup", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: void 0
|
|
23
|
+
});
|
|
24
|
+
this.db = db;
|
|
25
|
+
this.isSetup = false;
|
|
26
|
+
}
|
|
27
|
+
static fromConnString(connStringOrLocalPath) {
|
|
28
|
+
return new SqliteSaver(new better_sqlite3_1.default(connStringOrLocalPath));
|
|
29
|
+
}
|
|
30
|
+
setup() {
|
|
31
|
+
if (this.isSetup) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
this.db.pragma("journal_mode=WAL");
|
|
36
|
+
this.db.exec(`
|
|
37
|
+
CREATE TABLE IF NOT EXISTS checkpoints (
|
|
38
|
+
thread_id TEXT NOT NULL,
|
|
39
|
+
checkpoint_id TEXT NOT NULL,
|
|
40
|
+
parent_id TEXT,
|
|
41
|
+
checkpoint BLOB,
|
|
42
|
+
metadata BLOB,
|
|
43
|
+
PRIMARY KEY (thread_id, checkpoint_id)
|
|
44
|
+
);`);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
console.log("Error creating checkpoints table", error);
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
this.isSetup = true;
|
|
51
|
+
}
|
|
52
|
+
async getTuple(config) {
|
|
53
|
+
this.setup();
|
|
54
|
+
const thread_id = config.configurable?.thread_id;
|
|
55
|
+
const checkpoint_id = config.configurable?.checkpoint_id;
|
|
56
|
+
if (checkpoint_id) {
|
|
57
|
+
try {
|
|
58
|
+
const row = this.db
|
|
59
|
+
.prepare(`SELECT checkpoint, parent_id, metadata FROM checkpoints WHERE thread_id = ? AND checkpoint_id = ?`)
|
|
60
|
+
.get(thread_id, checkpoint_id);
|
|
61
|
+
if (row) {
|
|
62
|
+
return {
|
|
63
|
+
config,
|
|
64
|
+
checkpoint: this.serde.parse(row.checkpoint),
|
|
65
|
+
metadata: this.serde.parse(row.metadata),
|
|
66
|
+
parentConfig: row.parent_id
|
|
67
|
+
? {
|
|
68
|
+
configurable: {
|
|
69
|
+
thread_id,
|
|
70
|
+
checkpoint_id: row.parent_id,
|
|
71
|
+
},
|
|
72
|
+
}
|
|
73
|
+
: undefined,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
console.log("Error retrieving checkpoint", error);
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const row = this.db
|
|
84
|
+
.prepare(`SELECT thread_id, checkpoint_id, parent_id, checkpoint, metadata FROM checkpoints WHERE thread_id = ? ORDER BY checkpoint_id DESC LIMIT 1`)
|
|
85
|
+
.get(thread_id);
|
|
86
|
+
if (row) {
|
|
87
|
+
return {
|
|
88
|
+
config: {
|
|
89
|
+
configurable: {
|
|
90
|
+
thread_id: row.thread_id,
|
|
91
|
+
checkpoint_id: row.checkpoint_id,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
checkpoint: this.serde.parse(row.checkpoint),
|
|
95
|
+
metadata: this.serde.parse(row.metadata),
|
|
96
|
+
parentConfig: row.parent_id
|
|
97
|
+
? {
|
|
98
|
+
configurable: {
|
|
99
|
+
thread_id: row.thread_id,
|
|
100
|
+
checkpoint_id: row.parent_id,
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
: undefined,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
async *list(config) {
|
|
110
|
+
this.setup();
|
|
111
|
+
const thread_id = config.configurable?.thread_id;
|
|
112
|
+
try {
|
|
113
|
+
const rows = this.db
|
|
114
|
+
.prepare(`SELECT thread_id, checkpoint_id, parent_id, checkpoint, metadata FROM checkpoints WHERE thread_id = ? ORDER BY checkpoint_id DESC`)
|
|
115
|
+
.all(thread_id);
|
|
116
|
+
if (rows) {
|
|
117
|
+
for (const row of rows) {
|
|
118
|
+
yield {
|
|
119
|
+
config: {
|
|
120
|
+
configurable: {
|
|
121
|
+
thread_id: row.thread_id,
|
|
122
|
+
checkpoint_id: row.checkpoint_id,
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
checkpoint: this.serde.parse(row.checkpoint),
|
|
126
|
+
metadata: this.serde.parse(row.metadata),
|
|
127
|
+
parentConfig: row.parent_id
|
|
128
|
+
? {
|
|
129
|
+
configurable: {
|
|
130
|
+
thread_id: row.thread_id,
|
|
131
|
+
checkpoint_id: row.parent_id,
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
: undefined,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
console.log("Error listing checkpoints", error);
|
|
141
|
+
throw error;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async put(config, checkpoint, metadata) {
|
|
145
|
+
this.setup();
|
|
146
|
+
try {
|
|
147
|
+
const row = [
|
|
148
|
+
config.configurable?.thread_id,
|
|
149
|
+
checkpoint.id,
|
|
150
|
+
config.configurable?.checkpoint_id,
|
|
151
|
+
this.serde.stringify(checkpoint),
|
|
152
|
+
this.serde.stringify(metadata),
|
|
153
|
+
];
|
|
154
|
+
this.db
|
|
155
|
+
.prepare(`INSERT OR REPLACE INTO checkpoints (thread_id, checkpoint_id, parent_id, checkpoint, metadata) VALUES (?, ?, ?, ?, ?)`)
|
|
156
|
+
.run(...row);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
console.log("Error saving checkpoint", error);
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
configurable: {
|
|
164
|
+
thread_id: config.configurable?.thread_id,
|
|
165
|
+
checkpoint_id: checkpoint.id,
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
exports.SqliteSaver = SqliteSaver;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Database as DatabaseType } from "better-sqlite3";
|
|
2
|
+
import { RunnableConfig } from "@langchain/core/runnables";
|
|
3
|
+
import { BaseCheckpointSaver, Checkpoint, CheckpointMetadata, CheckpointTuple } from "./base.js";
|
|
4
|
+
import { SerializerProtocol } from "../serde/base.js";
|
|
5
|
+
export declare class SqliteSaver extends BaseCheckpointSaver {
|
|
6
|
+
db: DatabaseType;
|
|
7
|
+
protected isSetup: boolean;
|
|
8
|
+
constructor(db: DatabaseType, serde?: SerializerProtocol<Checkpoint>);
|
|
9
|
+
static fromConnString(connStringOrLocalPath: string): SqliteSaver;
|
|
10
|
+
private setup;
|
|
11
|
+
getTuple(config: RunnableConfig): Promise<CheckpointTuple | undefined>;
|
|
12
|
+
list(config: RunnableConfig): AsyncGenerator<CheckpointTuple>;
|
|
13
|
+
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata): Promise<RunnableConfig>;
|
|
14
|
+
}
|