@langgraph-js/pure-graph 1.0.2 → 1.2.0
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/.prettierrc +11 -0
- package/README.md +104 -10
- package/bun.lock +209 -0
- package/dist/adapter/hono/assistants.js +3 -9
- package/dist/adapter/hono/endpoint.js +1 -2
- package/dist/adapter/hono/runs.js +6 -39
- package/dist/adapter/hono/threads.js +5 -46
- package/dist/adapter/nextjs/endpoint.d.ts +1 -0
- package/dist/adapter/nextjs/endpoint.js +2 -0
- package/dist/adapter/nextjs/index.d.ts +1 -0
- package/dist/adapter/nextjs/index.js +2 -0
- package/dist/adapter/nextjs/router.d.ts +5 -0
- package/dist/adapter/nextjs/router.js +168 -0
- package/dist/adapter/{hono → nextjs}/zod.d.ts +5 -5
- package/dist/adapter/{hono → nextjs}/zod.js +22 -5
- package/dist/adapter/zod.d.ts +577 -0
- package/dist/adapter/zod.js +119 -0
- package/dist/createEndpoint.d.ts +1 -2
- package/dist/createEndpoint.js +4 -3
- package/dist/global.d.ts +6 -4
- package/dist/global.js +10 -5
- package/dist/graph/stream.d.ts +1 -1
- package/dist/graph/stream.js +18 -10
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/queue/stream_queue.d.ts +5 -3
- package/dist/queue/stream_queue.js +4 -2
- package/dist/storage/index.d.ts +9 -4
- package/dist/storage/index.js +38 -3
- package/dist/storage/redis/queue.d.ts +39 -0
- package/dist/storage/redis/queue.js +130 -0
- package/dist/storage/sqlite/DB.d.ts +3 -0
- package/dist/storage/sqlite/DB.js +14 -0
- package/dist/storage/sqlite/checkpoint.d.ts +18 -0
- package/dist/storage/sqlite/checkpoint.js +374 -0
- package/dist/storage/sqlite/threads.d.ts +43 -0
- package/dist/storage/sqlite/threads.js +266 -0
- package/dist/storage/sqlite/type.d.ts +15 -0
- package/dist/storage/sqlite/type.js +1 -0
- package/dist/utils/createEntrypointGraph.d.ts +14 -0
- package/dist/utils/createEntrypointGraph.js +11 -0
- package/dist/utils/getGraph.js +3 -3
- package/examples/nextjs/README.md +36 -0
- package/examples/nextjs/app/api/langgraph/[...path]/route.ts +10 -0
- package/examples/nextjs/app/favicon.ico +0 -0
- package/examples/nextjs/app/globals.css +26 -0
- package/examples/nextjs/app/layout.tsx +34 -0
- package/examples/nextjs/app/page.tsx +211 -0
- package/examples/nextjs/next.config.ts +26 -0
- package/examples/nextjs/package.json +24 -0
- package/examples/nextjs/postcss.config.mjs +5 -0
- package/examples/nextjs/tsconfig.json +27 -0
- package/package.json +9 -4
- package/packages/agent-graph/demo.json +35 -0
- package/packages/agent-graph/package.json +18 -0
- package/packages/agent-graph/src/index.ts +47 -0
- package/packages/agent-graph/src/tools/tavily.ts +9 -0
- package/packages/agent-graph/src/tools.ts +38 -0
- package/packages/agent-graph/src/types.ts +42 -0
- package/pnpm-workspace.yaml +4 -0
- package/src/adapter/hono/assistants.ts +16 -33
- package/src/adapter/hono/endpoint.ts +1 -2
- package/src/adapter/hono/runs.ts +15 -51
- package/src/adapter/hono/threads.ts +15 -70
- package/src/adapter/nextjs/endpoint.ts +2 -0
- package/src/adapter/nextjs/index.ts +2 -0
- package/src/adapter/nextjs/router.ts +193 -0
- package/src/adapter/{hono → nextjs}/zod.ts +22 -5
- package/src/adapter/zod.ts +135 -0
- package/src/createEndpoint.ts +12 -5
- package/src/e.d.ts +3 -0
- package/src/global.ts +11 -6
- package/src/graph/stream.ts +20 -10
- package/src/index.ts +1 -0
- package/src/queue/stream_queue.ts +6 -5
- package/src/storage/index.ts +42 -4
- package/src/storage/redis/queue.ts +148 -0
- package/src/storage/sqlite/DB.ts +16 -0
- package/src/storage/sqlite/checkpoint.ts +503 -0
- package/src/storage/sqlite/threads.ts +366 -0
- package/src/storage/sqlite/type.ts +12 -0
- package/src/utils/createEntrypointGraph.ts +20 -0
- package/src/utils/getGraph.ts +3 -3
- package/test/graph/entrypoint.ts +21 -0
- package/test/graph/index.ts +45 -6
- package/test/hono.ts +5 -0
- package/test/test.ts +0 -10
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { Database } from './DB.js';
|
|
2
|
+
import { BaseCheckpointSaver, TASKS, copyCheckpoint, maxChannelVersion, } from '@langchain/langgraph-checkpoint';
|
|
3
|
+
// In the `SqliteSaver.list` method, we need to sanitize the `options.filter` argument to ensure it only contains keys
|
|
4
|
+
// that are part of the `CheckpointMetadata` type. The lines below ensure that we get compile-time errors if the list
|
|
5
|
+
// of keys that we use is out of sync with the `CheckpointMetadata` type.
|
|
6
|
+
const checkpointMetadataKeys = ['source', 'step', 'parents'];
|
|
7
|
+
function validateKeys(keys) {
|
|
8
|
+
return keys;
|
|
9
|
+
}
|
|
10
|
+
// If this line fails to compile, the list of keys that we use in the `SqliteSaver.list` method is out of sync with the
|
|
11
|
+
// `CheckpointMetadata` type. In that case, just update `checkpointMetadataKeys` to contain all the keys in
|
|
12
|
+
// `CheckpointMetadata`
|
|
13
|
+
const validCheckpointMetadataKeys = validateKeys(checkpointMetadataKeys);
|
|
14
|
+
function prepareSql(db, checkpointId) {
|
|
15
|
+
const sql = `
|
|
16
|
+
SELECT
|
|
17
|
+
thread_id,
|
|
18
|
+
checkpoint_ns,
|
|
19
|
+
checkpoint_id,
|
|
20
|
+
parent_checkpoint_id,
|
|
21
|
+
type,
|
|
22
|
+
checkpoint,
|
|
23
|
+
metadata,
|
|
24
|
+
(
|
|
25
|
+
SELECT
|
|
26
|
+
json_group_array(
|
|
27
|
+
json_object(
|
|
28
|
+
'task_id', pw.task_id,
|
|
29
|
+
'channel', pw.channel,
|
|
30
|
+
'type', pw.type,
|
|
31
|
+
'value', CAST(pw.value AS TEXT)
|
|
32
|
+
)
|
|
33
|
+
)
|
|
34
|
+
FROM writes as pw
|
|
35
|
+
WHERE pw.thread_id = checkpoints.thread_id
|
|
36
|
+
AND pw.checkpoint_ns = checkpoints.checkpoint_ns
|
|
37
|
+
AND pw.checkpoint_id = checkpoints.checkpoint_id
|
|
38
|
+
) as pending_writes,
|
|
39
|
+
(
|
|
40
|
+
SELECT
|
|
41
|
+
json_group_array(
|
|
42
|
+
json_object(
|
|
43
|
+
'type', ps.type,
|
|
44
|
+
'value', CAST(ps.value AS TEXT)
|
|
45
|
+
)
|
|
46
|
+
)
|
|
47
|
+
FROM writes as ps
|
|
48
|
+
WHERE ps.thread_id = checkpoints.thread_id
|
|
49
|
+
AND ps.checkpoint_ns = checkpoints.checkpoint_ns
|
|
50
|
+
AND ps.checkpoint_id = checkpoints.parent_checkpoint_id
|
|
51
|
+
AND ps.channel = '${TASKS}'
|
|
52
|
+
ORDER BY ps.idx
|
|
53
|
+
) as pending_sends
|
|
54
|
+
FROM checkpoints
|
|
55
|
+
WHERE thread_id = ? AND checkpoint_ns = ? ${checkpointId ? 'AND checkpoint_id = ?' : 'ORDER BY checkpoint_id DESC LIMIT 1'}`;
|
|
56
|
+
return db.prepare(sql);
|
|
57
|
+
}
|
|
58
|
+
export class SqliteSaver extends BaseCheckpointSaver {
|
|
59
|
+
db;
|
|
60
|
+
isSetup;
|
|
61
|
+
withoutCheckpoint;
|
|
62
|
+
withCheckpoint;
|
|
63
|
+
constructor(db, serde) {
|
|
64
|
+
super(serde);
|
|
65
|
+
this.db = db;
|
|
66
|
+
this.isSetup = false;
|
|
67
|
+
}
|
|
68
|
+
static fromConnString(connStringOrLocalPath) {
|
|
69
|
+
return new SqliteSaver(new Database(connStringOrLocalPath));
|
|
70
|
+
}
|
|
71
|
+
setup() {
|
|
72
|
+
if (this.isSetup) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// this.db.pragma('journal_mode=WAL');
|
|
76
|
+
this.db.exec('PRAGMA journal_mode = WAL;');
|
|
77
|
+
this.db.exec(`
|
|
78
|
+
CREATE TABLE IF NOT EXISTS checkpoints (
|
|
79
|
+
thread_id TEXT NOT NULL,
|
|
80
|
+
checkpoint_ns TEXT NOT NULL DEFAULT '',
|
|
81
|
+
checkpoint_id TEXT NOT NULL,
|
|
82
|
+
parent_checkpoint_id TEXT,
|
|
83
|
+
type TEXT,
|
|
84
|
+
checkpoint BLOB,
|
|
85
|
+
metadata BLOB,
|
|
86
|
+
PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id)
|
|
87
|
+
);`);
|
|
88
|
+
this.db.exec(`
|
|
89
|
+
CREATE TABLE IF NOT EXISTS writes (
|
|
90
|
+
thread_id TEXT NOT NULL,
|
|
91
|
+
checkpoint_ns TEXT NOT NULL DEFAULT '',
|
|
92
|
+
checkpoint_id TEXT NOT NULL,
|
|
93
|
+
task_id TEXT NOT NULL,
|
|
94
|
+
idx INTEGER NOT NULL,
|
|
95
|
+
channel TEXT NOT NULL,
|
|
96
|
+
type TEXT,
|
|
97
|
+
value BLOB,
|
|
98
|
+
PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id, task_id, idx)
|
|
99
|
+
);`);
|
|
100
|
+
this.withoutCheckpoint = prepareSql(this.db, false);
|
|
101
|
+
this.withCheckpoint = prepareSql(this.db, true);
|
|
102
|
+
this.isSetup = true;
|
|
103
|
+
}
|
|
104
|
+
async getTuple(config) {
|
|
105
|
+
this.setup();
|
|
106
|
+
const { thread_id, checkpoint_ns = '', checkpoint_id } = config.configurable ?? {};
|
|
107
|
+
const args = [thread_id, checkpoint_ns];
|
|
108
|
+
if (checkpoint_id)
|
|
109
|
+
args.push(checkpoint_id);
|
|
110
|
+
const stm = checkpoint_id ? this.withCheckpoint : this.withoutCheckpoint;
|
|
111
|
+
const row = stm.get(...args);
|
|
112
|
+
if (row === undefined || row === null)
|
|
113
|
+
return undefined;
|
|
114
|
+
let finalConfig = config;
|
|
115
|
+
if (!checkpoint_id) {
|
|
116
|
+
finalConfig = {
|
|
117
|
+
configurable: {
|
|
118
|
+
thread_id: row.thread_id,
|
|
119
|
+
checkpoint_ns,
|
|
120
|
+
checkpoint_id: row.checkpoint_id,
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (finalConfig.configurable?.thread_id === undefined ||
|
|
125
|
+
finalConfig.configurable?.checkpoint_id === undefined) {
|
|
126
|
+
throw new Error('Missing thread_id or checkpoint_id');
|
|
127
|
+
}
|
|
128
|
+
const pendingWrites = await Promise.all(JSON.parse(row.pending_writes).map(async (write) => {
|
|
129
|
+
return [
|
|
130
|
+
write.task_id,
|
|
131
|
+
write.channel,
|
|
132
|
+
await this.serde.loadsTyped(write.type ?? 'json', write.value ?? ''),
|
|
133
|
+
];
|
|
134
|
+
}));
|
|
135
|
+
const checkpoint = (await this.serde.loadsTyped(row.type ?? 'json', row.checkpoint));
|
|
136
|
+
if (checkpoint.v < 4 && row.parent_checkpoint_id != null) {
|
|
137
|
+
await this.migratePendingSends(checkpoint, row.thread_id, row.parent_checkpoint_id);
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
checkpoint,
|
|
141
|
+
config: finalConfig,
|
|
142
|
+
metadata: (await this.serde.loadsTyped(row.type ?? 'json', row.metadata)),
|
|
143
|
+
parentConfig: row.parent_checkpoint_id
|
|
144
|
+
? {
|
|
145
|
+
configurable: {
|
|
146
|
+
thread_id: row.thread_id,
|
|
147
|
+
checkpoint_ns,
|
|
148
|
+
checkpoint_id: row.parent_checkpoint_id,
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
: undefined,
|
|
152
|
+
pendingWrites,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
async *list(config, options) {
|
|
156
|
+
const { limit, before, filter } = options ?? {};
|
|
157
|
+
this.setup();
|
|
158
|
+
const thread_id = config.configurable?.thread_id;
|
|
159
|
+
const checkpoint_ns = config.configurable?.checkpoint_ns;
|
|
160
|
+
let sql = `
|
|
161
|
+
SELECT
|
|
162
|
+
thread_id,
|
|
163
|
+
checkpoint_ns,
|
|
164
|
+
checkpoint_id,
|
|
165
|
+
parent_checkpoint_id,
|
|
166
|
+
type,
|
|
167
|
+
checkpoint,
|
|
168
|
+
metadata,
|
|
169
|
+
(
|
|
170
|
+
SELECT
|
|
171
|
+
json_group_array(
|
|
172
|
+
json_object(
|
|
173
|
+
'task_id', pw.task_id,
|
|
174
|
+
'channel', pw.channel,
|
|
175
|
+
'type', pw.type,
|
|
176
|
+
'value', CAST(pw.value AS TEXT)
|
|
177
|
+
)
|
|
178
|
+
)
|
|
179
|
+
FROM writes as pw
|
|
180
|
+
WHERE pw.thread_id = checkpoints.thread_id
|
|
181
|
+
AND pw.checkpoint_ns = checkpoints.checkpoint_ns
|
|
182
|
+
AND pw.checkpoint_id = checkpoints.checkpoint_id
|
|
183
|
+
) as pending_writes,
|
|
184
|
+
(
|
|
185
|
+
SELECT
|
|
186
|
+
json_group_array(
|
|
187
|
+
json_object(
|
|
188
|
+
'type', ps.type,
|
|
189
|
+
'value', CAST(ps.value AS TEXT)
|
|
190
|
+
)
|
|
191
|
+
)
|
|
192
|
+
FROM writes as ps
|
|
193
|
+
WHERE ps.thread_id = checkpoints.thread_id
|
|
194
|
+
AND ps.checkpoint_ns = checkpoints.checkpoint_ns
|
|
195
|
+
AND ps.checkpoint_id = checkpoints.parent_checkpoint_id
|
|
196
|
+
AND ps.channel = '${TASKS}'
|
|
197
|
+
ORDER BY ps.idx
|
|
198
|
+
) as pending_sends
|
|
199
|
+
FROM checkpoints\n`;
|
|
200
|
+
const whereClause = [];
|
|
201
|
+
if (thread_id) {
|
|
202
|
+
whereClause.push('thread_id = ?');
|
|
203
|
+
}
|
|
204
|
+
if (checkpoint_ns !== undefined && checkpoint_ns !== null) {
|
|
205
|
+
whereClause.push('checkpoint_ns = ?');
|
|
206
|
+
}
|
|
207
|
+
if (before?.configurable?.checkpoint_id !== undefined) {
|
|
208
|
+
whereClause.push('checkpoint_id < ?');
|
|
209
|
+
}
|
|
210
|
+
const sanitizedFilter = Object.fromEntries(Object.entries(filter ?? {}).filter(([key, value]) => value !== undefined && validCheckpointMetadataKeys.includes(key)));
|
|
211
|
+
whereClause.push(...Object.entries(sanitizedFilter).map(([key]) => `jsonb(CAST(metadata AS TEXT))->'$.${key}' = ?`));
|
|
212
|
+
if (whereClause.length > 0) {
|
|
213
|
+
sql += `WHERE\n ${whereClause.join(' AND\n ')}\n`;
|
|
214
|
+
}
|
|
215
|
+
sql += '\nORDER BY checkpoint_id DESC';
|
|
216
|
+
if (limit) {
|
|
217
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
218
|
+
sql += ` LIMIT ${parseInt(limit, 10)}`; // parseInt here (with cast to make TS happy) to sanitize input, as limit may be user-provided
|
|
219
|
+
}
|
|
220
|
+
const args = [
|
|
221
|
+
thread_id,
|
|
222
|
+
checkpoint_ns,
|
|
223
|
+
before?.configurable?.checkpoint_id,
|
|
224
|
+
...Object.values(sanitizedFilter).map((value) => JSON.stringify(value)),
|
|
225
|
+
].filter((value) => value !== undefined && value !== null);
|
|
226
|
+
const rows = this.db.prepare(sql).all(...args);
|
|
227
|
+
if (rows) {
|
|
228
|
+
for (const row of rows) {
|
|
229
|
+
const pendingWrites = await Promise.all(JSON.parse(row.pending_writes).map(async (write) => {
|
|
230
|
+
return [
|
|
231
|
+
write.task_id,
|
|
232
|
+
write.channel,
|
|
233
|
+
await this.serde.loadsTyped(write.type ?? 'json', write.value ?? ''),
|
|
234
|
+
];
|
|
235
|
+
}));
|
|
236
|
+
const checkpoint = (await this.serde.loadsTyped(row.type ?? 'json', row.checkpoint));
|
|
237
|
+
if (checkpoint.v < 4 && row.parent_checkpoint_id != null) {
|
|
238
|
+
await this.migratePendingSends(checkpoint, row.thread_id, row.parent_checkpoint_id);
|
|
239
|
+
}
|
|
240
|
+
yield {
|
|
241
|
+
config: {
|
|
242
|
+
configurable: {
|
|
243
|
+
thread_id: row.thread_id,
|
|
244
|
+
checkpoint_ns: row.checkpoint_ns,
|
|
245
|
+
checkpoint_id: row.checkpoint_id,
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
checkpoint,
|
|
249
|
+
metadata: (await this.serde.loadsTyped(row.type ?? 'json', row.metadata)),
|
|
250
|
+
parentConfig: row.parent_checkpoint_id
|
|
251
|
+
? {
|
|
252
|
+
configurable: {
|
|
253
|
+
thread_id: row.thread_id,
|
|
254
|
+
checkpoint_ns: row.checkpoint_ns,
|
|
255
|
+
checkpoint_id: row.parent_checkpoint_id,
|
|
256
|
+
},
|
|
257
|
+
}
|
|
258
|
+
: undefined,
|
|
259
|
+
pendingWrites,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
async put(config, checkpoint, metadata) {
|
|
265
|
+
this.setup();
|
|
266
|
+
if (!config.configurable) {
|
|
267
|
+
throw new Error('Empty configuration supplied.');
|
|
268
|
+
}
|
|
269
|
+
const thread_id = config.configurable?.thread_id;
|
|
270
|
+
const checkpoint_ns = config.configurable?.checkpoint_ns ?? '';
|
|
271
|
+
const parent_checkpoint_id = config.configurable?.checkpoint_id;
|
|
272
|
+
if (!thread_id) {
|
|
273
|
+
throw new Error(`Missing "thread_id" field in passed "config.configurable".`);
|
|
274
|
+
}
|
|
275
|
+
const preparedCheckpoint = copyCheckpoint(checkpoint);
|
|
276
|
+
const [[type1, serializedCheckpoint], [type2, serializedMetadata]] = await Promise.all([
|
|
277
|
+
this.serde.dumpsTyped(preparedCheckpoint),
|
|
278
|
+
this.serde.dumpsTyped(metadata),
|
|
279
|
+
]);
|
|
280
|
+
if (type1 !== type2) {
|
|
281
|
+
throw new Error('Failed to serialized checkpoint and metadata to the same type.');
|
|
282
|
+
}
|
|
283
|
+
const row = [
|
|
284
|
+
thread_id,
|
|
285
|
+
checkpoint_ns,
|
|
286
|
+
checkpoint.id,
|
|
287
|
+
parent_checkpoint_id,
|
|
288
|
+
type1,
|
|
289
|
+
serializedCheckpoint,
|
|
290
|
+
serializedMetadata,
|
|
291
|
+
];
|
|
292
|
+
this.db
|
|
293
|
+
.prepare(`INSERT OR REPLACE INTO checkpoints (thread_id, checkpoint_ns, checkpoint_id, parent_checkpoint_id, type, checkpoint, metadata) VALUES (?, ?, ?, ?, ?, ?, ?)`)
|
|
294
|
+
.run(...row);
|
|
295
|
+
return {
|
|
296
|
+
configurable: {
|
|
297
|
+
thread_id,
|
|
298
|
+
checkpoint_ns,
|
|
299
|
+
checkpoint_id: checkpoint.id,
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
async putWrites(config, writes, taskId) {
|
|
304
|
+
this.setup();
|
|
305
|
+
if (!config.configurable) {
|
|
306
|
+
throw new Error('Empty configuration supplied.');
|
|
307
|
+
}
|
|
308
|
+
if (!config.configurable?.thread_id) {
|
|
309
|
+
throw new Error('Missing thread_id field in config.configurable.');
|
|
310
|
+
}
|
|
311
|
+
if (!config.configurable?.checkpoint_id) {
|
|
312
|
+
throw new Error('Missing checkpoint_id field in config.configurable.');
|
|
313
|
+
}
|
|
314
|
+
const stmt = this.db.prepare(`
|
|
315
|
+
INSERT OR REPLACE INTO writes
|
|
316
|
+
(thread_id, checkpoint_ns, checkpoint_id, task_id, idx, channel, type, value)
|
|
317
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
318
|
+
`);
|
|
319
|
+
const transaction = this.db.transaction((rows) => {
|
|
320
|
+
for (const row of rows) {
|
|
321
|
+
stmt.run(...row);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
const rows = await Promise.all(writes.map(async (write, idx) => {
|
|
325
|
+
const [type, serializedWrite] = await this.serde.dumpsTyped(write[1]);
|
|
326
|
+
return [
|
|
327
|
+
config.configurable?.thread_id,
|
|
328
|
+
config.configurable?.checkpoint_ns,
|
|
329
|
+
config.configurable?.checkpoint_id,
|
|
330
|
+
taskId,
|
|
331
|
+
idx,
|
|
332
|
+
write[0],
|
|
333
|
+
type,
|
|
334
|
+
serializedWrite,
|
|
335
|
+
];
|
|
336
|
+
}));
|
|
337
|
+
transaction(rows);
|
|
338
|
+
}
|
|
339
|
+
async deleteThread(threadId) {
|
|
340
|
+
const transaction = this.db.transaction(() => {
|
|
341
|
+
this.db.prepare(`DELETE FROM checkpoints WHERE thread_id = ?`).run(threadId);
|
|
342
|
+
this.db.prepare(`DELETE FROM writes WHERE thread_id = ?`).run(threadId);
|
|
343
|
+
});
|
|
344
|
+
transaction();
|
|
345
|
+
}
|
|
346
|
+
async migratePendingSends(checkpoint, threadId, parentCheckpointId) {
|
|
347
|
+
const { pending_sends } = this.db
|
|
348
|
+
.prepare(`
|
|
349
|
+
SELECT
|
|
350
|
+
checkpoint_id,
|
|
351
|
+
json_group_array(
|
|
352
|
+
json_object(
|
|
353
|
+
'type', ps.type,
|
|
354
|
+
'value', CAST(ps.value AS TEXT)
|
|
355
|
+
)
|
|
356
|
+
) as pending_sends
|
|
357
|
+
FROM writes as ps
|
|
358
|
+
WHERE ps.thread_id = ?
|
|
359
|
+
AND ps.checkpoint_id = ?
|
|
360
|
+
AND ps.channel = '${TASKS}'
|
|
361
|
+
ORDER BY ps.idx
|
|
362
|
+
`)
|
|
363
|
+
.get(threadId, parentCheckpointId);
|
|
364
|
+
const mutableCheckpoint = checkpoint;
|
|
365
|
+
// add pending sends to checkpoint
|
|
366
|
+
mutableCheckpoint.channel_values ??= {};
|
|
367
|
+
mutableCheckpoint.channel_values[TASKS] = await Promise.all(JSON.parse(pending_sends).map(({ type, value }) => this.serde.loadsTyped(type, value)));
|
|
368
|
+
// add to versions
|
|
369
|
+
mutableCheckpoint.channel_versions[TASKS] =
|
|
370
|
+
Object.keys(checkpoint.channel_versions).length > 0
|
|
371
|
+
? maxChannelVersion(...Object.values(checkpoint.channel_versions))
|
|
372
|
+
: this.getNextVersion(undefined);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { BaseThreadsManager } from '../../threads/index.js';
|
|
2
|
+
import { Command, Metadata, OnConflictBehavior, Run, RunStatus, SortOrder, Thread, ThreadSortBy, ThreadStatus } from '@langgraph-js/sdk';
|
|
3
|
+
import type { SqliteSaver } from './checkpoint.js';
|
|
4
|
+
import type { DatabaseType } from './type.js';
|
|
5
|
+
export declare class SQLiteThreadsManager<ValuesType = unknown> extends BaseThreadsManager {
|
|
6
|
+
db: DatabaseType;
|
|
7
|
+
private isSetup;
|
|
8
|
+
constructor(checkpointer: SqliteSaver);
|
|
9
|
+
private setup;
|
|
10
|
+
create(payload?: {
|
|
11
|
+
metadata?: Metadata;
|
|
12
|
+
threadId?: string;
|
|
13
|
+
ifExists?: OnConflictBehavior;
|
|
14
|
+
graphId?: string;
|
|
15
|
+
supersteps?: Array<{
|
|
16
|
+
updates: Array<{
|
|
17
|
+
values: unknown;
|
|
18
|
+
command?: Command;
|
|
19
|
+
asNode: string;
|
|
20
|
+
}>;
|
|
21
|
+
}>;
|
|
22
|
+
}): Promise<Thread<ValuesType>>;
|
|
23
|
+
search(query?: {
|
|
24
|
+
metadata?: Metadata;
|
|
25
|
+
limit?: number;
|
|
26
|
+
offset?: number;
|
|
27
|
+
status?: ThreadStatus;
|
|
28
|
+
sortBy?: ThreadSortBy;
|
|
29
|
+
sortOrder?: SortOrder;
|
|
30
|
+
}): Promise<Thread<ValuesType>[]>;
|
|
31
|
+
get(threadId: string): Promise<Thread<ValuesType>>;
|
|
32
|
+
set(threadId: string, thread: Partial<Thread<ValuesType>>): Promise<void>;
|
|
33
|
+
delete(threadId: string): Promise<void>;
|
|
34
|
+
createRun(threadId: string, assistantId: string, payload?: {
|
|
35
|
+
metadata?: Metadata;
|
|
36
|
+
}): Promise<Run>;
|
|
37
|
+
listRuns(threadId: string, options?: {
|
|
38
|
+
limit?: number;
|
|
39
|
+
offset?: number;
|
|
40
|
+
status?: RunStatus;
|
|
41
|
+
}): Promise<Run[]>;
|
|
42
|
+
updateRun(runId: string, run: Partial<Run>): Promise<void>;
|
|
43
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { BaseThreadsManager } from '../../threads/index.js';
|
|
2
|
+
export class SQLiteThreadsManager extends BaseThreadsManager {
|
|
3
|
+
db;
|
|
4
|
+
isSetup = false;
|
|
5
|
+
constructor(checkpointer) {
|
|
6
|
+
super();
|
|
7
|
+
this.db = checkpointer.db;
|
|
8
|
+
this.setup();
|
|
9
|
+
}
|
|
10
|
+
setup() {
|
|
11
|
+
if (this.isSetup) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
// 创建 threads 表
|
|
15
|
+
this.db.exec(`
|
|
16
|
+
CREATE TABLE IF NOT EXISTS threads (
|
|
17
|
+
thread_id TEXT PRIMARY KEY,
|
|
18
|
+
created_at TEXT NOT NULL,
|
|
19
|
+
updated_at TEXT NOT NULL,
|
|
20
|
+
metadata TEXT NOT NULL DEFAULT '{}',
|
|
21
|
+
status TEXT NOT NULL DEFAULT 'idle',
|
|
22
|
+
[values] TEXT,
|
|
23
|
+
interrupts TEXT NOT NULL DEFAULT '{}'
|
|
24
|
+
)
|
|
25
|
+
`);
|
|
26
|
+
// 创建 runs 表
|
|
27
|
+
this.db.exec(`
|
|
28
|
+
CREATE TABLE IF NOT EXISTS runs (
|
|
29
|
+
run_id TEXT PRIMARY KEY,
|
|
30
|
+
thread_id TEXT NOT NULL,
|
|
31
|
+
assistant_id TEXT NOT NULL,
|
|
32
|
+
created_at TEXT NOT NULL,
|
|
33
|
+
updated_at TEXT NOT NULL,
|
|
34
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
35
|
+
metadata TEXT NOT NULL DEFAULT '{}',
|
|
36
|
+
multitask_strategy TEXT NOT NULL DEFAULT 'reject',
|
|
37
|
+
FOREIGN KEY (thread_id) REFERENCES threads(thread_id) ON DELETE CASCADE
|
|
38
|
+
)
|
|
39
|
+
`);
|
|
40
|
+
// 创建索引以提高查询性能
|
|
41
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_threads_status ON threads(status)`);
|
|
42
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_threads_created_at ON threads(created_at)`);
|
|
43
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_threads_updated_at ON threads(updated_at)`);
|
|
44
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_runs_thread_id ON runs(thread_id)`);
|
|
45
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_runs_status ON runs(status)`);
|
|
46
|
+
this.isSetup = true;
|
|
47
|
+
}
|
|
48
|
+
async create(payload) {
|
|
49
|
+
const threadId = payload?.threadId || crypto.randomUUID();
|
|
50
|
+
// 检查线程是否已存在
|
|
51
|
+
if (payload?.ifExists === 'raise') {
|
|
52
|
+
const existingThread = this.db.prepare('SELECT thread_id FROM threads WHERE thread_id = ?').get(threadId);
|
|
53
|
+
if (existingThread) {
|
|
54
|
+
throw new Error(`Thread with ID ${threadId} already exists.`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const now = new Date().toISOString();
|
|
58
|
+
const metadata = JSON.stringify(payload?.metadata || {});
|
|
59
|
+
const interrupts = JSON.stringify({});
|
|
60
|
+
const thread = {
|
|
61
|
+
thread_id: threadId,
|
|
62
|
+
created_at: now,
|
|
63
|
+
updated_at: now,
|
|
64
|
+
metadata: payload?.metadata || {},
|
|
65
|
+
status: 'idle',
|
|
66
|
+
values: null,
|
|
67
|
+
interrupts: {},
|
|
68
|
+
};
|
|
69
|
+
// 插入到数据库
|
|
70
|
+
this.db
|
|
71
|
+
.prepare(`
|
|
72
|
+
INSERT INTO threads (thread_id, created_at, updated_at, metadata, status, [values], interrupts)
|
|
73
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
74
|
+
`)
|
|
75
|
+
.run(threadId, now, now, metadata, 'idle', null, interrupts);
|
|
76
|
+
return thread;
|
|
77
|
+
}
|
|
78
|
+
async search(query) {
|
|
79
|
+
let sql = 'SELECT * FROM threads';
|
|
80
|
+
const whereConditions = [];
|
|
81
|
+
const params = [];
|
|
82
|
+
// 构建 WHERE 条件
|
|
83
|
+
if (query?.status) {
|
|
84
|
+
whereConditions.push('status = ?');
|
|
85
|
+
params.push(query.status);
|
|
86
|
+
}
|
|
87
|
+
if (query?.metadata) {
|
|
88
|
+
for (const [key, value] of Object.entries(query.metadata)) {
|
|
89
|
+
whereConditions.push(`json_extract(metadata, '$.${key}') = ?`);
|
|
90
|
+
params.push(JSON.stringify(value));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (whereConditions.length > 0) {
|
|
94
|
+
sql += ' WHERE ' + whereConditions.join(' AND ');
|
|
95
|
+
}
|
|
96
|
+
// 添加排序
|
|
97
|
+
if (query?.sortBy) {
|
|
98
|
+
sql += ` ORDER BY ${query.sortBy}`;
|
|
99
|
+
if (query.sortOrder === 'desc') {
|
|
100
|
+
sql += ' DESC';
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
sql += ' ASC';
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// 添加分页
|
|
107
|
+
if (query?.limit) {
|
|
108
|
+
sql += ` LIMIT ${query.limit}`;
|
|
109
|
+
if (query?.offset) {
|
|
110
|
+
sql += ` OFFSET ${query.offset}`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const rows = this.db.prepare(sql).all(...params);
|
|
114
|
+
return rows.map((row) => ({
|
|
115
|
+
thread_id: row.thread_id,
|
|
116
|
+
created_at: row.created_at,
|
|
117
|
+
updated_at: row.updated_at,
|
|
118
|
+
metadata: JSON.parse(row.metadata),
|
|
119
|
+
status: row.status,
|
|
120
|
+
values: row.values ? JSON.parse(row.values) : null,
|
|
121
|
+
interrupts: JSON.parse(row.interrupts),
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
async get(threadId) {
|
|
125
|
+
const row = this.db.prepare('SELECT * FROM threads WHERE thread_id = ?').get(threadId);
|
|
126
|
+
if (!row) {
|
|
127
|
+
throw new Error(`Thread with ID ${threadId} not found.`);
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
thread_id: row.thread_id,
|
|
131
|
+
created_at: row.created_at,
|
|
132
|
+
updated_at: row.updated_at,
|
|
133
|
+
metadata: JSON.parse(row.metadata),
|
|
134
|
+
status: row.status,
|
|
135
|
+
values: row.values ? JSON.parse(row.values) : null,
|
|
136
|
+
interrupts: JSON.parse(row.interrupts),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
async set(threadId, thread) {
|
|
140
|
+
// 检查线程是否存在
|
|
141
|
+
const existingThread = this.db.prepare('SELECT thread_id FROM threads WHERE thread_id = ?').get(threadId);
|
|
142
|
+
if (!existingThread) {
|
|
143
|
+
throw new Error(`Thread with ID ${threadId} not found.`);
|
|
144
|
+
}
|
|
145
|
+
const updateFields = [];
|
|
146
|
+
const values = [];
|
|
147
|
+
if (thread.metadata !== undefined) {
|
|
148
|
+
updateFields.push('metadata = ?');
|
|
149
|
+
values.push(JSON.stringify(thread.metadata));
|
|
150
|
+
}
|
|
151
|
+
if (thread.status !== undefined) {
|
|
152
|
+
updateFields.push('status = ?');
|
|
153
|
+
values.push(thread.status);
|
|
154
|
+
}
|
|
155
|
+
if (thread.values !== undefined) {
|
|
156
|
+
updateFields.push('[values] = ?');
|
|
157
|
+
values.push(thread.values ? JSON.stringify(thread.values) : null);
|
|
158
|
+
}
|
|
159
|
+
if (thread.interrupts !== undefined) {
|
|
160
|
+
updateFields.push('interrupts = ?');
|
|
161
|
+
values.push(JSON.stringify(thread.interrupts));
|
|
162
|
+
}
|
|
163
|
+
// 总是更新 updated_at
|
|
164
|
+
updateFields.push('updated_at = ?');
|
|
165
|
+
values.push(new Date().toISOString());
|
|
166
|
+
if (updateFields.length > 0) {
|
|
167
|
+
values.push(threadId);
|
|
168
|
+
this.db
|
|
169
|
+
.prepare(`
|
|
170
|
+
UPDATE threads
|
|
171
|
+
SET ${updateFields.join(', ')}
|
|
172
|
+
WHERE thread_id = ?
|
|
173
|
+
`)
|
|
174
|
+
.run(...values);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async delete(threadId) {
|
|
178
|
+
const result = this.db.prepare('DELETE FROM threads WHERE thread_id = ?').run(threadId);
|
|
179
|
+
if (result.changes === 0) {
|
|
180
|
+
throw new Error(`Thread with ID ${threadId} not found.`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async createRun(threadId, assistantId, payload) {
|
|
184
|
+
const runId = crypto.randomUUID();
|
|
185
|
+
const now = new Date().toISOString();
|
|
186
|
+
const metadata = JSON.stringify(payload?.metadata ?? {});
|
|
187
|
+
const run = {
|
|
188
|
+
run_id: runId,
|
|
189
|
+
thread_id: threadId,
|
|
190
|
+
assistant_id: assistantId,
|
|
191
|
+
created_at: now,
|
|
192
|
+
updated_at: now,
|
|
193
|
+
status: 'pending',
|
|
194
|
+
metadata: payload?.metadata ?? {},
|
|
195
|
+
multitask_strategy: 'reject',
|
|
196
|
+
};
|
|
197
|
+
// 插入到数据库
|
|
198
|
+
this.db
|
|
199
|
+
.prepare(`
|
|
200
|
+
INSERT INTO runs (run_id, thread_id, assistant_id, created_at, updated_at, status, metadata, multitask_strategy)
|
|
201
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
202
|
+
`)
|
|
203
|
+
.run(runId, threadId, assistantId, now, now, 'pending', metadata, 'reject');
|
|
204
|
+
return run;
|
|
205
|
+
}
|
|
206
|
+
async listRuns(threadId, options) {
|
|
207
|
+
let sql = 'SELECT * FROM runs WHERE thread_id = ?';
|
|
208
|
+
const params = [threadId];
|
|
209
|
+
if (options?.status) {
|
|
210
|
+
sql += ' AND status = ?';
|
|
211
|
+
params.push(options.status);
|
|
212
|
+
}
|
|
213
|
+
sql += ' ORDER BY created_at DESC';
|
|
214
|
+
if (options?.limit) {
|
|
215
|
+
sql += ` LIMIT ${options.limit}`;
|
|
216
|
+
if (options?.offset) {
|
|
217
|
+
sql += ` OFFSET ${options.offset}`;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const rows = this.db.prepare(sql).all(...params);
|
|
221
|
+
return rows.map((row) => ({
|
|
222
|
+
run_id: row.run_id,
|
|
223
|
+
thread_id: row.thread_id,
|
|
224
|
+
assistant_id: row.assistant_id,
|
|
225
|
+
created_at: row.created_at,
|
|
226
|
+
updated_at: row.updated_at,
|
|
227
|
+
status: row.status,
|
|
228
|
+
metadata: JSON.parse(row.metadata),
|
|
229
|
+
multitask_strategy: row.multitask_strategy,
|
|
230
|
+
}));
|
|
231
|
+
}
|
|
232
|
+
async updateRun(runId, run) {
|
|
233
|
+
// 检查运行是否存在
|
|
234
|
+
const existingRun = this.db.prepare('SELECT run_id FROM runs WHERE run_id = ?').get(runId);
|
|
235
|
+
if (!existingRun) {
|
|
236
|
+
throw new Error(`Run with ID ${runId} not found.`);
|
|
237
|
+
}
|
|
238
|
+
const updateFields = [];
|
|
239
|
+
const values = [];
|
|
240
|
+
if (run.status !== undefined) {
|
|
241
|
+
updateFields.push('status = ?');
|
|
242
|
+
values.push(run.status);
|
|
243
|
+
}
|
|
244
|
+
if (run.metadata !== undefined) {
|
|
245
|
+
updateFields.push('metadata = ?');
|
|
246
|
+
values.push(JSON.stringify(run.metadata));
|
|
247
|
+
}
|
|
248
|
+
if (run.multitask_strategy !== undefined) {
|
|
249
|
+
updateFields.push('multitask_strategy = ?');
|
|
250
|
+
values.push(run.multitask_strategy);
|
|
251
|
+
}
|
|
252
|
+
// 总是更新 updated_at
|
|
253
|
+
updateFields.push('updated_at = ?');
|
|
254
|
+
values.push(new Date().toISOString());
|
|
255
|
+
if (updateFields.length > 0) {
|
|
256
|
+
values.push(runId);
|
|
257
|
+
this.db
|
|
258
|
+
.prepare(`
|
|
259
|
+
UPDATE runs
|
|
260
|
+
SET ${updateFields.join(', ')}
|
|
261
|
+
WHERE run_id = ?
|
|
262
|
+
`)
|
|
263
|
+
.run(...values);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface DatabaseType {
|
|
2
|
+
prepare(sql: string): Statement;
|
|
3
|
+
exec(sql: string): void;
|
|
4
|
+
close(): void;
|
|
5
|
+
transaction<T extends any[]>(fn: (...args: T) => void): (...args: T) => void;
|
|
6
|
+
}
|
|
7
|
+
interface Statement {
|
|
8
|
+
run(...params: any[]): {
|
|
9
|
+
changes: number;
|
|
10
|
+
lastInsertRowid: number;
|
|
11
|
+
};
|
|
12
|
+
get(...params: any[]): any;
|
|
13
|
+
all(...params: any[]): any[];
|
|
14
|
+
}
|
|
15
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|