@langgraph-js/pure-graph 1.0.2 → 1.3.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.
Files changed (95) hide show
  1. package/.prettierrc +11 -0
  2. package/README.md +104 -10
  3. package/bun.lock +209 -0
  4. package/dist/adapter/hono/assistants.js +3 -9
  5. package/dist/adapter/hono/endpoint.js +1 -2
  6. package/dist/adapter/hono/runs.js +23 -39
  7. package/dist/adapter/hono/threads.js +5 -46
  8. package/dist/adapter/nextjs/endpoint.d.ts +1 -0
  9. package/dist/adapter/nextjs/endpoint.js +2 -0
  10. package/dist/adapter/nextjs/index.d.ts +1 -0
  11. package/dist/adapter/nextjs/index.js +2 -0
  12. package/dist/adapter/nextjs/router.d.ts +5 -0
  13. package/dist/adapter/nextjs/router.js +179 -0
  14. package/dist/adapter/nextjs/zod.d.ts +203 -0
  15. package/dist/adapter/nextjs/zod.js +60 -0
  16. package/dist/adapter/zod.d.ts +584 -0
  17. package/dist/adapter/zod.js +127 -0
  18. package/dist/createEndpoint.d.ts +1 -2
  19. package/dist/createEndpoint.js +4 -3
  20. package/dist/global.d.ts +6 -4
  21. package/dist/global.js +10 -5
  22. package/dist/graph/stream.d.ts +1 -1
  23. package/dist/graph/stream.js +18 -10
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.js +1 -0
  26. package/dist/queue/stream_queue.d.ts +5 -3
  27. package/dist/queue/stream_queue.js +4 -2
  28. package/dist/storage/index.d.ts +9 -4
  29. package/dist/storage/index.js +38 -3
  30. package/dist/storage/memory/threads.d.ts +3 -2
  31. package/dist/storage/memory/threads.js +29 -2
  32. package/dist/storage/redis/queue.d.ts +39 -0
  33. package/dist/storage/redis/queue.js +130 -0
  34. package/dist/storage/sqlite/DB.d.ts +3 -0
  35. package/dist/storage/sqlite/DB.js +14 -0
  36. package/dist/storage/sqlite/checkpoint.d.ts +18 -0
  37. package/dist/storage/sqlite/checkpoint.js +374 -0
  38. package/dist/storage/sqlite/threads.d.ts +44 -0
  39. package/dist/storage/sqlite/threads.js +300 -0
  40. package/dist/storage/sqlite/type.d.ts +15 -0
  41. package/dist/storage/sqlite/type.js +1 -0
  42. package/dist/threads/index.d.ts +3 -2
  43. package/dist/threads/index.js +1 -26
  44. package/dist/types.d.ts +2 -1
  45. package/dist/utils/createEntrypointGraph.d.ts +14 -0
  46. package/dist/utils/createEntrypointGraph.js +11 -0
  47. package/dist/utils/getGraph.js +3 -3
  48. package/examples/nextjs/README.md +36 -0
  49. package/examples/nextjs/app/api/langgraph/[...path]/route.ts +10 -0
  50. package/examples/nextjs/app/favicon.ico +0 -0
  51. package/examples/nextjs/app/globals.css +26 -0
  52. package/examples/nextjs/app/layout.tsx +34 -0
  53. package/examples/nextjs/app/page.tsx +211 -0
  54. package/examples/nextjs/next.config.ts +26 -0
  55. package/examples/nextjs/package.json +24 -0
  56. package/examples/nextjs/postcss.config.mjs +5 -0
  57. package/examples/nextjs/tsconfig.json +27 -0
  58. package/package.json +9 -4
  59. package/packages/agent-graph/demo.json +35 -0
  60. package/packages/agent-graph/package.json +18 -0
  61. package/packages/agent-graph/src/index.ts +47 -0
  62. package/packages/agent-graph/src/tools/tavily.ts +9 -0
  63. package/packages/agent-graph/src/tools.ts +38 -0
  64. package/packages/agent-graph/src/types.ts +42 -0
  65. package/pnpm-workspace.yaml +4 -0
  66. package/src/adapter/hono/assistants.ts +16 -33
  67. package/src/adapter/hono/endpoint.ts +1 -2
  68. package/src/adapter/hono/runs.ts +42 -51
  69. package/src/adapter/hono/threads.ts +15 -70
  70. package/src/adapter/nextjs/endpoint.ts +2 -0
  71. package/src/adapter/nextjs/index.ts +2 -0
  72. package/src/adapter/nextjs/router.ts +206 -0
  73. package/src/adapter/{hono → nextjs}/zod.ts +22 -5
  74. package/src/adapter/zod.ts +144 -0
  75. package/src/createEndpoint.ts +12 -5
  76. package/src/e.d.ts +3 -0
  77. package/src/global.ts +11 -6
  78. package/src/graph/stream.ts +20 -10
  79. package/src/index.ts +1 -0
  80. package/src/queue/stream_queue.ts +6 -5
  81. package/src/storage/index.ts +42 -4
  82. package/src/storage/memory/threads.ts +30 -1
  83. package/src/storage/redis/queue.ts +148 -0
  84. package/src/storage/sqlite/DB.ts +16 -0
  85. package/src/storage/sqlite/checkpoint.ts +502 -0
  86. package/src/storage/sqlite/threads.ts +405 -0
  87. package/src/storage/sqlite/type.ts +12 -0
  88. package/src/threads/index.ts +11 -25
  89. package/src/types.ts +2 -0
  90. package/src/utils/createEntrypointGraph.ts +20 -0
  91. package/src/utils/getGraph.ts +3 -3
  92. package/test/graph/entrypoint.ts +21 -0
  93. package/test/graph/index.ts +45 -6
  94. package/test/hono.ts +5 -0
  95. package/test/test.ts +0 -10
@@ -0,0 +1,130 @@
1
+ import { CancelEventMessage } from '../../queue/event_message.js';
2
+ import { BaseStreamQueue } from '../../queue/stream_queue.js';
3
+ import { createClient } from 'redis';
4
+ /**
5
+ * Redis 实现的消息队列,用于存储消息
6
+ */
7
+ export class RedisStreamQueue extends BaseStreamQueue {
8
+ id;
9
+ static redis = createClient({ url: process.env.REDIS_URL });
10
+ static subscriberRedis = createClient({ url: process.env.REDIS_URL });
11
+ redis;
12
+ subscriberRedis;
13
+ queueKey;
14
+ channelKey;
15
+ isConnected = false;
16
+ cancelSignal;
17
+ constructor(id = 'default') {
18
+ super(id, true);
19
+ this.id = id;
20
+ this.queueKey = `queue:${this.id}`;
21
+ this.channelKey = `channel:${this.id}`;
22
+ this.redis = RedisStreamQueue.redis;
23
+ this.subscriberRedis = RedisStreamQueue.subscriberRedis;
24
+ this.cancelSignal = new AbortController();
25
+ // 连接 Redis 客户端
26
+ this.redis.connect();
27
+ this.subscriberRedis.connect();
28
+ this.isConnected = true;
29
+ }
30
+ /**
31
+ * 推送消息到 Redis 队列
32
+ */
33
+ async push(item) {
34
+ const data = await this.encodeData(item);
35
+ const serializedData = Buffer.from(data);
36
+ // 推送到队列
37
+ await this.redis.lPush(this.queueKey, serializedData);
38
+ // 设置队列 TTL 为 300 秒
39
+ await this.redis.expire(this.queueKey, 300);
40
+ // 发布到频道通知有新数据
41
+ await this.redis.publish(this.channelKey, serializedData);
42
+ this.emit('dataChange', data);
43
+ }
44
+ /**
45
+ * 异步生成器:支持 for await...of 方式消费队列数据
46
+ */
47
+ async *onDataReceive() {
48
+ let queue = [];
49
+ let pendingResolve = null;
50
+ let isStreamEnded = false;
51
+ const handleMessage = async (message) => {
52
+ const data = (await this.decodeData(message));
53
+ queue.push(data);
54
+ // 检查是否为流结束或错误信号
55
+ if (data.event === '__stream_end__' ||
56
+ data.event === '__stream_error__' ||
57
+ data.event === '__stream_cancel__') {
58
+ setTimeout(() => {
59
+ isStreamEnded = true;
60
+ if (pendingResolve) {
61
+ pendingResolve();
62
+ pendingResolve = null;
63
+ }
64
+ }, 300);
65
+ if (data.event === '__stream_cancel__') {
66
+ this.cancel();
67
+ }
68
+ }
69
+ if (pendingResolve) {
70
+ pendingResolve();
71
+ pendingResolve = null;
72
+ }
73
+ };
74
+ // 订阅 Redis 频道
75
+ await this.subscriberRedis.subscribe(this.channelKey, (message) => {
76
+ handleMessage(message);
77
+ });
78
+ try {
79
+ while (!isStreamEnded) {
80
+ if (queue.length > 0) {
81
+ for (const item of queue) {
82
+ yield item;
83
+ }
84
+ queue = [];
85
+ }
86
+ else {
87
+ await new Promise((resolve) => {
88
+ pendingResolve = resolve;
89
+ });
90
+ }
91
+ }
92
+ }
93
+ finally {
94
+ await this.subscriberRedis.unsubscribe(this.channelKey);
95
+ }
96
+ }
97
+ /**
98
+ * 获取队列中的所有数据
99
+ */
100
+ async getAll() {
101
+ const data = await this.redis.lRange(this.queueKey, 0, -1);
102
+ if (!data || data.length === 0) {
103
+ return [];
104
+ }
105
+ if (this.compressMessages) {
106
+ return (await Promise.all(data.map(async (item) => {
107
+ const parsed = JSON.parse(item);
108
+ return (await this.decodeData(parsed));
109
+ })));
110
+ }
111
+ else {
112
+ return data.map((item) => JSON.parse(item));
113
+ }
114
+ }
115
+ /**
116
+ * 清空队列
117
+ */
118
+ clear() {
119
+ if (this.isConnected) {
120
+ this.redis.del(this.queueKey);
121
+ }
122
+ }
123
+ /**
124
+ * 取消操作
125
+ */
126
+ cancel() {
127
+ this.push(new CancelEventMessage());
128
+ this.cancelSignal.abort('user cancel this run');
129
+ }
130
+ }
@@ -0,0 +1,3 @@
1
+ import { DatabaseType } from './type';
2
+ declare let Database: new (uri: string) => DatabaseType;
3
+ export { Database };
@@ -0,0 +1,14 @@
1
+ let Database;
2
+ /** @ts-ignore */
3
+ if (globalThis.Bun) {
4
+ console.log('Using Bun Sqlite, pid:', process.pid);
5
+ const BunSqlite = await import('bun:sqlite');
6
+ /** @ts-ignore */
7
+ Database = BunSqlite.default;
8
+ }
9
+ else {
10
+ /** @ts-ignore */
11
+ const CommonSqlite = await import('better-sqlite3');
12
+ Database = CommonSqlite.default;
13
+ }
14
+ export { Database };
@@ -0,0 +1,18 @@
1
+ import { DatabaseType } from './type.js';
2
+ import type { RunnableConfig } from '@langchain/core/runnables';
3
+ import { BaseCheckpointSaver, type Checkpoint, type CheckpointListOptions, type CheckpointTuple, type SerializerProtocol, type PendingWrite, type CheckpointMetadata } from '@langchain/langgraph-checkpoint';
4
+ export declare class SqliteSaver extends BaseCheckpointSaver {
5
+ db: DatabaseType;
6
+ protected isSetup: boolean;
7
+ protected withoutCheckpoint: any;
8
+ protected withCheckpoint: any;
9
+ constructor(db: DatabaseType, serde?: SerializerProtocol);
10
+ static fromConnString(connStringOrLocalPath: string): SqliteSaver;
11
+ protected setup(): void;
12
+ getTuple(config: RunnableConfig): Promise<CheckpointTuple | undefined>;
13
+ list(config: RunnableConfig, options?: CheckpointListOptions): AsyncGenerator<CheckpointTuple>;
14
+ put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata): Promise<RunnableConfig>;
15
+ putWrites(config: RunnableConfig, writes: PendingWrite[], taskId: string): Promise<void>;
16
+ deleteThread(threadId: string): Promise<void>;
17
+ protected migratePendingSends(checkpoint: Checkpoint, threadId: string, parentCheckpointId: string): Promise<void>;
18
+ }
@@ -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,44 @@
1
+ import { BaseThreadsManager } from '../../threads/index.js';
2
+ import { Command, Config, 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> implements BaseThreadsManager<ValuesType> {
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
+ updateState(threadId: string, thread: Partial<Thread<ValuesType>>): Promise<Pick<Config, 'configurable'>>;
34
+ delete(threadId: string): Promise<void>;
35
+ createRun(threadId: string, assistantId: string, payload?: {
36
+ metadata?: Metadata;
37
+ }): Promise<Run>;
38
+ listRuns(threadId: string, options?: {
39
+ limit?: number;
40
+ offset?: number;
41
+ status?: RunStatus;
42
+ }): Promise<Run[]>;
43
+ updateRun(runId: string, run: Partial<Run>): Promise<void>;
44
+ }