@langgraph-js/pure-graph 1.0.1 → 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.
Files changed (89) 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 +6 -40
  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 +168 -0
  14. package/dist/adapter/{hono → nextjs}/zod.d.ts +5 -5
  15. package/dist/adapter/{hono → nextjs}/zod.js +22 -5
  16. package/dist/adapter/zod.d.ts +577 -0
  17. package/dist/adapter/zod.js +119 -0
  18. package/dist/createEndpoint.d.ts +1 -2
  19. package/dist/createEndpoint.js +5 -5
  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 +19 -11
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.js +1 -0
  26. package/dist/queue/event_message.js +1 -1
  27. package/dist/queue/stream_queue.d.ts +5 -3
  28. package/dist/queue/stream_queue.js +4 -2
  29. package/dist/storage/index.d.ts +9 -4
  30. package/dist/storage/index.js +38 -3
  31. package/dist/storage/memory/queue.js +13 -4
  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 +43 -0
  39. package/dist/storage/sqlite/threads.js +266 -0
  40. package/dist/storage/sqlite/type.d.ts +15 -0
  41. package/dist/storage/sqlite/type.js +1 -0
  42. package/dist/utils/createEntrypointGraph.d.ts +14 -0
  43. package/dist/utils/createEntrypointGraph.js +11 -0
  44. package/dist/utils/getGraph.js +3 -3
  45. package/examples/nextjs/README.md +36 -0
  46. package/examples/nextjs/app/api/langgraph/[...path]/route.ts +10 -0
  47. package/examples/nextjs/app/favicon.ico +0 -0
  48. package/examples/nextjs/app/globals.css +26 -0
  49. package/examples/nextjs/app/layout.tsx +34 -0
  50. package/examples/nextjs/app/page.tsx +211 -0
  51. package/examples/nextjs/next.config.ts +26 -0
  52. package/examples/nextjs/package.json +24 -0
  53. package/examples/nextjs/postcss.config.mjs +5 -0
  54. package/examples/nextjs/tsconfig.json +27 -0
  55. package/package.json +10 -4
  56. package/packages/agent-graph/demo.json +35 -0
  57. package/packages/agent-graph/package.json +18 -0
  58. package/packages/agent-graph/src/index.ts +47 -0
  59. package/packages/agent-graph/src/tools/tavily.ts +9 -0
  60. package/packages/agent-graph/src/tools.ts +38 -0
  61. package/packages/agent-graph/src/types.ts +42 -0
  62. package/pnpm-workspace.yaml +4 -0
  63. package/src/adapter/hono/assistants.ts +16 -33
  64. package/src/adapter/hono/endpoint.ts +1 -2
  65. package/src/adapter/hono/runs.ts +15 -51
  66. package/src/adapter/hono/threads.ts +15 -70
  67. package/src/adapter/nextjs/endpoint.ts +2 -0
  68. package/src/adapter/nextjs/index.ts +2 -0
  69. package/src/adapter/nextjs/router.ts +193 -0
  70. package/src/adapter/{hono → nextjs}/zod.ts +22 -5
  71. package/src/adapter/zod.ts +135 -0
  72. package/src/createEndpoint.ts +12 -5
  73. package/src/e.d.ts +3 -0
  74. package/src/global.ts +11 -6
  75. package/src/graph/stream.ts +20 -10
  76. package/src/index.ts +1 -0
  77. package/src/queue/stream_queue.ts +6 -5
  78. package/src/storage/index.ts +42 -4
  79. package/src/storage/redis/queue.ts +148 -0
  80. package/src/storage/sqlite/DB.ts +16 -0
  81. package/src/storage/sqlite/checkpoint.ts +503 -0
  82. package/src/storage/sqlite/threads.ts +366 -0
  83. package/src/storage/sqlite/type.ts +12 -0
  84. package/src/utils/createEntrypointGraph.ts +20 -0
  85. package/src/utils/getGraph.ts +3 -3
  86. package/test/graph/entrypoint.ts +21 -0
  87. package/test/graph/index.ts +45 -6
  88. package/test/hono.ts +5 -0
  89. package/test/test.ts +0 -10
@@ -0,0 +1,148 @@
1
+ import { CancelEventMessage, EventMessage } from '../../queue/event_message.js';
2
+ import { BaseStreamQueue } from '../../queue/stream_queue.js';
3
+ import { BaseStreamQueueInterface } from '../../queue/stream_queue.js';
4
+ import { createClient, RedisClientType } from 'redis';
5
+
6
+ /**
7
+ * Redis 实现的消息队列,用于存储消息
8
+ */
9
+ export class RedisStreamQueue extends BaseStreamQueue implements BaseStreamQueueInterface {
10
+ static redis: RedisClientType = createClient({ url: process.env.REDIS_URL! });
11
+ static subscriberRedis: RedisClientType = createClient({ url: process.env.REDIS_URL! });
12
+ private redis: RedisClientType;
13
+ private subscriberRedis: RedisClientType;
14
+ private queueKey: string;
15
+ private channelKey: string;
16
+ private isConnected = false;
17
+ public cancelSignal: AbortController;
18
+
19
+ constructor(readonly id: string = 'default') {
20
+ super(id, true);
21
+ this.queueKey = `queue:${this.id}`;
22
+ this.channelKey = `channel:${this.id}`;
23
+ this.redis = RedisStreamQueue.redis;
24
+ this.subscriberRedis = RedisStreamQueue.subscriberRedis;
25
+ this.cancelSignal = new AbortController();
26
+
27
+ // 连接 Redis 客户端
28
+ this.redis.connect();
29
+ this.subscriberRedis.connect();
30
+ this.isConnected = true;
31
+ }
32
+
33
+ /**
34
+ * 推送消息到 Redis 队列
35
+ */
36
+ async push(item: EventMessage): Promise<void> {
37
+ const data = await this.encodeData(item);
38
+ const serializedData = Buffer.from(data);
39
+
40
+ // 推送到队列
41
+ await this.redis.lPush(this.queueKey, serializedData);
42
+
43
+ // 设置队列 TTL 为 300 秒
44
+ await this.redis.expire(this.queueKey, 300);
45
+
46
+ // 发布到频道通知有新数据
47
+ await this.redis.publish(this.channelKey, serializedData);
48
+
49
+ this.emit('dataChange', data);
50
+ }
51
+
52
+ /**
53
+ * 异步生成器:支持 for await...of 方式消费队列数据
54
+ */
55
+ async *onDataReceive(): AsyncGenerator<EventMessage, void, unknown> {
56
+ let queue: EventMessage[] = [];
57
+ let pendingResolve: (() => void) | null = null;
58
+ let isStreamEnded = false;
59
+ const handleMessage = async (message: string) => {
60
+ const data = (await this.decodeData(message)) as EventMessage;
61
+ queue.push(data);
62
+ // 检查是否为流结束或错误信号
63
+ if (
64
+ data.event === '__stream_end__' ||
65
+ data.event === '__stream_error__' ||
66
+ data.event === '__stream_cancel__'
67
+ ) {
68
+ setTimeout(() => {
69
+ isStreamEnded = true;
70
+ if (pendingResolve) {
71
+ pendingResolve();
72
+ pendingResolve = null;
73
+ }
74
+ }, 300);
75
+
76
+ if (data.event === '__stream_cancel__') {
77
+ this.cancel();
78
+ }
79
+ }
80
+
81
+ if (pendingResolve) {
82
+ pendingResolve();
83
+ pendingResolve = null;
84
+ }
85
+ };
86
+
87
+ // 订阅 Redis 频道
88
+ await this.subscriberRedis.subscribe(this.channelKey, (message) => {
89
+ handleMessage(message);
90
+ });
91
+
92
+ try {
93
+ while (!isStreamEnded) {
94
+ if (queue.length > 0) {
95
+ for (const item of queue) {
96
+ yield item;
97
+ }
98
+ queue = [];
99
+ } else {
100
+ await new Promise((resolve) => {
101
+ pendingResolve = resolve as () => void;
102
+ });
103
+ }
104
+ }
105
+ } finally {
106
+ await this.subscriberRedis.unsubscribe(this.channelKey);
107
+ }
108
+ }
109
+
110
+ /**
111
+ * 获取队列中的所有数据
112
+ */
113
+ async getAll(): Promise<EventMessage[]> {
114
+ const data = await this.redis.lRange(this.queueKey, 0, -1);
115
+
116
+ if (!data || data.length === 0) {
117
+ return [];
118
+ }
119
+
120
+ if (this.compressMessages) {
121
+ return (await Promise.all(
122
+ data.map(async (item: string) => {
123
+ const parsed = JSON.parse(item) as EventMessage;
124
+ return (await this.decodeData(parsed as any)) as EventMessage;
125
+ }),
126
+ )) as EventMessage[];
127
+ } else {
128
+ return data.map((item: string) => JSON.parse(item) as EventMessage);
129
+ }
130
+ }
131
+
132
+ /**
133
+ * 清空队列
134
+ */
135
+ clear(): void {
136
+ if (this.isConnected) {
137
+ this.redis.del(this.queueKey);
138
+ }
139
+ }
140
+
141
+ /**
142
+ * 取消操作
143
+ */
144
+ cancel(): void {
145
+ this.push(new CancelEventMessage());
146
+ this.cancelSignal.abort('user cancel this run');
147
+ }
148
+ }
@@ -0,0 +1,16 @@
1
+ import { DatabaseType } from './type';
2
+
3
+ let Database: new (uri: string) => DatabaseType;
4
+ /** @ts-ignore */
5
+ if (globalThis.Bun) {
6
+ console.log('Using Bun Sqlite, pid:', process.pid);
7
+ const BunSqlite = await import('bun:sqlite');
8
+ /** @ts-ignore */
9
+ Database = BunSqlite.default;
10
+ } else {
11
+ /** @ts-ignore */
12
+ const CommonSqlite = await import('better-sqlite3');
13
+ Database = CommonSqlite.default;
14
+ }
15
+
16
+ export { Database };
@@ -0,0 +1,503 @@
1
+ import { DatabaseType } from './type.js';
2
+ import { Database } from './DB.js';
3
+ import type { RunnableConfig } from '@langchain/core/runnables';
4
+ import {
5
+ BaseCheckpointSaver,
6
+ type Checkpoint,
7
+ type CheckpointListOptions,
8
+ type CheckpointTuple,
9
+ type SerializerProtocol,
10
+ type PendingWrite,
11
+ type CheckpointMetadata,
12
+ TASKS,
13
+ copyCheckpoint,
14
+ maxChannelVersion,
15
+ } from '@langchain/langgraph-checkpoint';
16
+
17
+ interface CheckpointRow {
18
+ checkpoint: string;
19
+ metadata: string;
20
+ parent_checkpoint_id?: string;
21
+ thread_id: string;
22
+ checkpoint_id: string;
23
+ checkpoint_ns?: string;
24
+ type?: string;
25
+ pending_writes: string;
26
+ }
27
+
28
+ interface PendingWriteColumn {
29
+ task_id: string;
30
+ channel: string;
31
+ type: string;
32
+ value: string;
33
+ }
34
+
35
+ interface PendingSendColumn {
36
+ type: string;
37
+ value: string;
38
+ }
39
+
40
+ // In the `SqliteSaver.list` method, we need to sanitize the `options.filter` argument to ensure it only contains keys
41
+ // that are part of the `CheckpointMetadata` type. The lines below ensure that we get compile-time errors if the list
42
+ // of keys that we use is out of sync with the `CheckpointMetadata` type.
43
+ const checkpointMetadataKeys = ['source', 'step', 'parents'] as const;
44
+
45
+ type CheckKeys<T, K extends readonly (keyof T)[]> = [K[number]] extends [keyof T]
46
+ ? [keyof T] extends [K[number]]
47
+ ? K
48
+ : never
49
+ : never;
50
+
51
+ function validateKeys<T, K extends readonly (keyof T)[]>(keys: CheckKeys<T, K>): K {
52
+ return keys;
53
+ }
54
+
55
+ // If this line fails to compile, the list of keys that we use in the `SqliteSaver.list` method is out of sync with the
56
+ // `CheckpointMetadata` type. In that case, just update `checkpointMetadataKeys` to contain all the keys in
57
+ // `CheckpointMetadata`
58
+ const validCheckpointMetadataKeys = validateKeys<CheckpointMetadata, typeof checkpointMetadataKeys>(
59
+ checkpointMetadataKeys,
60
+ );
61
+
62
+ function prepareSql(db: DatabaseType, checkpointId: boolean) {
63
+ const sql = `
64
+ SELECT
65
+ thread_id,
66
+ checkpoint_ns,
67
+ checkpoint_id,
68
+ parent_checkpoint_id,
69
+ type,
70
+ checkpoint,
71
+ metadata,
72
+ (
73
+ SELECT
74
+ json_group_array(
75
+ json_object(
76
+ 'task_id', pw.task_id,
77
+ 'channel', pw.channel,
78
+ 'type', pw.type,
79
+ 'value', CAST(pw.value AS TEXT)
80
+ )
81
+ )
82
+ FROM writes as pw
83
+ WHERE pw.thread_id = checkpoints.thread_id
84
+ AND pw.checkpoint_ns = checkpoints.checkpoint_ns
85
+ AND pw.checkpoint_id = checkpoints.checkpoint_id
86
+ ) as pending_writes,
87
+ (
88
+ SELECT
89
+ json_group_array(
90
+ json_object(
91
+ 'type', ps.type,
92
+ 'value', CAST(ps.value AS TEXT)
93
+ )
94
+ )
95
+ FROM writes as ps
96
+ WHERE ps.thread_id = checkpoints.thread_id
97
+ AND ps.checkpoint_ns = checkpoints.checkpoint_ns
98
+ AND ps.checkpoint_id = checkpoints.parent_checkpoint_id
99
+ AND ps.channel = '${TASKS}'
100
+ ORDER BY ps.idx
101
+ ) as pending_sends
102
+ FROM checkpoints
103
+ WHERE thread_id = ? AND checkpoint_ns = ? ${
104
+ checkpointId ? 'AND checkpoint_id = ?' : 'ORDER BY checkpoint_id DESC LIMIT 1'
105
+ }`;
106
+
107
+ return db.prepare(sql);
108
+ }
109
+
110
+ export class SqliteSaver extends BaseCheckpointSaver {
111
+ db: DatabaseType;
112
+
113
+ protected isSetup: boolean;
114
+
115
+ protected withoutCheckpoint: any;
116
+
117
+ protected withCheckpoint: any;
118
+
119
+ constructor(db: DatabaseType, serde?: SerializerProtocol) {
120
+ super(serde);
121
+ this.db = db;
122
+ this.isSetup = false;
123
+ }
124
+
125
+ static fromConnString(connStringOrLocalPath: string): SqliteSaver {
126
+ return new SqliteSaver(new Database(connStringOrLocalPath));
127
+ }
128
+
129
+ protected setup(): void {
130
+ if (this.isSetup) {
131
+ return;
132
+ }
133
+
134
+ // this.db.pragma('journal_mode=WAL');
135
+ this.db.exec('PRAGMA journal_mode = WAL;');
136
+ this.db.exec(`
137
+ CREATE TABLE IF NOT EXISTS checkpoints (
138
+ thread_id TEXT NOT NULL,
139
+ checkpoint_ns TEXT NOT NULL DEFAULT '',
140
+ checkpoint_id TEXT NOT NULL,
141
+ parent_checkpoint_id TEXT,
142
+ type TEXT,
143
+ checkpoint BLOB,
144
+ metadata BLOB,
145
+ PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id)
146
+ );`);
147
+ this.db.exec(`
148
+ CREATE TABLE IF NOT EXISTS writes (
149
+ thread_id TEXT NOT NULL,
150
+ checkpoint_ns TEXT NOT NULL DEFAULT '',
151
+ checkpoint_id TEXT NOT NULL,
152
+ task_id TEXT NOT NULL,
153
+ idx INTEGER NOT NULL,
154
+ channel TEXT NOT NULL,
155
+ type TEXT,
156
+ value BLOB,
157
+ PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id, task_id, idx)
158
+ );`);
159
+
160
+ this.withoutCheckpoint = prepareSql(this.db, false);
161
+ this.withCheckpoint = prepareSql(this.db, true);
162
+
163
+ this.isSetup = true;
164
+ }
165
+
166
+ async getTuple(config: RunnableConfig): Promise<CheckpointTuple | undefined> {
167
+ this.setup();
168
+ const { thread_id, checkpoint_ns = '', checkpoint_id } = config.configurable ?? {};
169
+
170
+ const args = [thread_id, checkpoint_ns];
171
+ if (checkpoint_id) args.push(checkpoint_id);
172
+
173
+ const stm = checkpoint_id ? this.withCheckpoint : this.withoutCheckpoint;
174
+ const row = stm.get(...args) as CheckpointRow;
175
+ if (row === undefined || row === null) return undefined;
176
+
177
+ let finalConfig = config;
178
+
179
+ if (!checkpoint_id) {
180
+ finalConfig = {
181
+ configurable: {
182
+ thread_id: row.thread_id,
183
+ checkpoint_ns,
184
+ checkpoint_id: row.checkpoint_id,
185
+ },
186
+ };
187
+ }
188
+
189
+ if (
190
+ finalConfig.configurable?.thread_id === undefined ||
191
+ finalConfig.configurable?.checkpoint_id === undefined
192
+ ) {
193
+ throw new Error('Missing thread_id or checkpoint_id');
194
+ }
195
+
196
+ const pendingWrites = await Promise.all(
197
+ (JSON.parse(row.pending_writes) as PendingWriteColumn[]).map(async (write) => {
198
+ return [
199
+ write.task_id,
200
+ write.channel,
201
+ await this.serde.loadsTyped(write.type ?? 'json', write.value ?? ''),
202
+ ] as [string, string, unknown];
203
+ }),
204
+ );
205
+
206
+ const checkpoint = (await this.serde.loadsTyped(row.type ?? 'json', row.checkpoint)) as Checkpoint;
207
+
208
+ if (checkpoint.v < 4 && row.parent_checkpoint_id != null) {
209
+ await this.migratePendingSends(checkpoint, row.thread_id, row.parent_checkpoint_id);
210
+ }
211
+
212
+ return {
213
+ checkpoint,
214
+ config: finalConfig,
215
+ metadata: (await this.serde.loadsTyped(row.type ?? 'json', row.metadata)) as CheckpointMetadata,
216
+ parentConfig: row.parent_checkpoint_id
217
+ ? {
218
+ configurable: {
219
+ thread_id: row.thread_id,
220
+ checkpoint_ns,
221
+ checkpoint_id: row.parent_checkpoint_id,
222
+ },
223
+ }
224
+ : undefined,
225
+ pendingWrites,
226
+ };
227
+ }
228
+
229
+ async *list(config: RunnableConfig, options?: CheckpointListOptions): AsyncGenerator<CheckpointTuple> {
230
+ const { limit, before, filter } = options ?? {};
231
+ this.setup();
232
+ const thread_id = config.configurable?.thread_id;
233
+ const checkpoint_ns = config.configurable?.checkpoint_ns;
234
+ let sql = `
235
+ SELECT
236
+ thread_id,
237
+ checkpoint_ns,
238
+ checkpoint_id,
239
+ parent_checkpoint_id,
240
+ type,
241
+ checkpoint,
242
+ metadata,
243
+ (
244
+ SELECT
245
+ json_group_array(
246
+ json_object(
247
+ 'task_id', pw.task_id,
248
+ 'channel', pw.channel,
249
+ 'type', pw.type,
250
+ 'value', CAST(pw.value AS TEXT)
251
+ )
252
+ )
253
+ FROM writes as pw
254
+ WHERE pw.thread_id = checkpoints.thread_id
255
+ AND pw.checkpoint_ns = checkpoints.checkpoint_ns
256
+ AND pw.checkpoint_id = checkpoints.checkpoint_id
257
+ ) as pending_writes,
258
+ (
259
+ SELECT
260
+ json_group_array(
261
+ json_object(
262
+ 'type', ps.type,
263
+ 'value', CAST(ps.value AS TEXT)
264
+ )
265
+ )
266
+ FROM writes as ps
267
+ WHERE ps.thread_id = checkpoints.thread_id
268
+ AND ps.checkpoint_ns = checkpoints.checkpoint_ns
269
+ AND ps.checkpoint_id = checkpoints.parent_checkpoint_id
270
+ AND ps.channel = '${TASKS}'
271
+ ORDER BY ps.idx
272
+ ) as pending_sends
273
+ FROM checkpoints\n`;
274
+
275
+ const whereClause: string[] = [];
276
+
277
+ if (thread_id) {
278
+ whereClause.push('thread_id = ?');
279
+ }
280
+
281
+ if (checkpoint_ns !== undefined && checkpoint_ns !== null) {
282
+ whereClause.push('checkpoint_ns = ?');
283
+ }
284
+
285
+ if (before?.configurable?.checkpoint_id !== undefined) {
286
+ whereClause.push('checkpoint_id < ?');
287
+ }
288
+
289
+ const sanitizedFilter = Object.fromEntries(
290
+ Object.entries(filter ?? {}).filter(
291
+ ([key, value]) =>
292
+ value !== undefined && validCheckpointMetadataKeys.includes(key as keyof CheckpointMetadata),
293
+ ),
294
+ );
295
+
296
+ whereClause.push(
297
+ ...Object.entries(sanitizedFilter).map(([key]) => `jsonb(CAST(metadata AS TEXT))->'$.${key}' = ?`),
298
+ );
299
+
300
+ if (whereClause.length > 0) {
301
+ sql += `WHERE\n ${whereClause.join(' AND\n ')}\n`;
302
+ }
303
+
304
+ sql += '\nORDER BY checkpoint_id DESC';
305
+
306
+ if (limit) {
307
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
308
+ sql += ` LIMIT ${parseInt(limit as any, 10)}`; // parseInt here (with cast to make TS happy) to sanitize input, as limit may be user-provided
309
+ }
310
+
311
+ const args = [
312
+ thread_id,
313
+ checkpoint_ns,
314
+ before?.configurable?.checkpoint_id,
315
+ ...Object.values(sanitizedFilter).map((value) => JSON.stringify(value)),
316
+ ].filter((value) => value !== undefined && value !== null);
317
+
318
+ const rows: CheckpointRow[] = this.db.prepare(sql).all(...args) as CheckpointRow[];
319
+
320
+ if (rows) {
321
+ for (const row of rows) {
322
+ const pendingWrites = await Promise.all(
323
+ (JSON.parse(row.pending_writes) as PendingWriteColumn[]).map(async (write) => {
324
+ return [
325
+ write.task_id,
326
+ write.channel,
327
+ await this.serde.loadsTyped(write.type ?? 'json', write.value ?? ''),
328
+ ] as [string, string, unknown];
329
+ }),
330
+ );
331
+
332
+ const checkpoint = (await this.serde.loadsTyped(row.type ?? 'json', row.checkpoint)) as Checkpoint;
333
+
334
+ if (checkpoint.v < 4 && row.parent_checkpoint_id != null) {
335
+ await this.migratePendingSends(checkpoint, row.thread_id, row.parent_checkpoint_id);
336
+ }
337
+
338
+ yield {
339
+ config: {
340
+ configurable: {
341
+ thread_id: row.thread_id,
342
+ checkpoint_ns: row.checkpoint_ns,
343
+ checkpoint_id: row.checkpoint_id,
344
+ },
345
+ },
346
+ checkpoint,
347
+ metadata: (await this.serde.loadsTyped(row.type ?? 'json', row.metadata)) as CheckpointMetadata,
348
+ parentConfig: row.parent_checkpoint_id
349
+ ? {
350
+ configurable: {
351
+ thread_id: row.thread_id,
352
+ checkpoint_ns: row.checkpoint_ns,
353
+ checkpoint_id: row.parent_checkpoint_id,
354
+ },
355
+ }
356
+ : undefined,
357
+ pendingWrites,
358
+ };
359
+ }
360
+ }
361
+ }
362
+
363
+ async put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata): Promise<RunnableConfig> {
364
+ this.setup();
365
+
366
+ if (!config.configurable) {
367
+ throw new Error('Empty configuration supplied.');
368
+ }
369
+
370
+ const thread_id = config.configurable?.thread_id;
371
+ const checkpoint_ns = config.configurable?.checkpoint_ns ?? '';
372
+ const parent_checkpoint_id = config.configurable?.checkpoint_id;
373
+
374
+ if (!thread_id) {
375
+ throw new Error(`Missing "thread_id" field in passed "config.configurable".`);
376
+ }
377
+
378
+ const preparedCheckpoint: Partial<Checkpoint> = copyCheckpoint(checkpoint);
379
+
380
+ const [[type1, serializedCheckpoint], [type2, serializedMetadata]] = await Promise.all([
381
+ this.serde.dumpsTyped(preparedCheckpoint),
382
+ this.serde.dumpsTyped(metadata),
383
+ ]);
384
+
385
+ if (type1 !== type2) {
386
+ throw new Error('Failed to serialized checkpoint and metadata to the same type.');
387
+ }
388
+ const row = [
389
+ thread_id,
390
+ checkpoint_ns,
391
+ checkpoint.id,
392
+ parent_checkpoint_id,
393
+ type1,
394
+ serializedCheckpoint,
395
+ serializedMetadata,
396
+ ];
397
+
398
+ this.db
399
+ .prepare(
400
+ `INSERT OR REPLACE INTO checkpoints (thread_id, checkpoint_ns, checkpoint_id, parent_checkpoint_id, type, checkpoint, metadata) VALUES (?, ?, ?, ?, ?, ?, ?)`,
401
+ )
402
+ .run(...row);
403
+
404
+ return {
405
+ configurable: {
406
+ thread_id,
407
+ checkpoint_ns,
408
+ checkpoint_id: checkpoint.id,
409
+ },
410
+ };
411
+ }
412
+
413
+ async putWrites(config: RunnableConfig, writes: PendingWrite[], taskId: string): Promise<void> {
414
+ this.setup();
415
+
416
+ if (!config.configurable) {
417
+ throw new Error('Empty configuration supplied.');
418
+ }
419
+
420
+ if (!config.configurable?.thread_id) {
421
+ throw new Error('Missing thread_id field in config.configurable.');
422
+ }
423
+
424
+ if (!config.configurable?.checkpoint_id) {
425
+ throw new Error('Missing checkpoint_id field in config.configurable.');
426
+ }
427
+
428
+ const stmt = this.db.prepare(`
429
+ INSERT OR REPLACE INTO writes
430
+ (thread_id, checkpoint_ns, checkpoint_id, task_id, idx, channel, type, value)
431
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
432
+ `);
433
+
434
+ const transaction = this.db.transaction((rows) => {
435
+ for (const row of rows) {
436
+ stmt.run(...row);
437
+ }
438
+ });
439
+
440
+ const rows = await Promise.all(
441
+ writes.map(async (write, idx) => {
442
+ const [type, serializedWrite] = await this.serde.dumpsTyped(write[1]);
443
+ return [
444
+ config.configurable?.thread_id,
445
+ config.configurable?.checkpoint_ns,
446
+ config.configurable?.checkpoint_id,
447
+ taskId,
448
+ idx,
449
+ write[0],
450
+ type,
451
+ serializedWrite,
452
+ ];
453
+ }),
454
+ );
455
+
456
+ transaction(rows);
457
+ }
458
+
459
+ async deleteThread(threadId: string) {
460
+ const transaction = this.db.transaction(() => {
461
+ this.db.prepare(`DELETE FROM checkpoints WHERE thread_id = ?`).run(threadId);
462
+ this.db.prepare(`DELETE FROM writes WHERE thread_id = ?`).run(threadId);
463
+ });
464
+
465
+ transaction();
466
+ }
467
+
468
+ protected async migratePendingSends(checkpoint: Checkpoint, threadId: string, parentCheckpointId: string) {
469
+ const { pending_sends } = this.db
470
+ .prepare(
471
+ `
472
+ SELECT
473
+ checkpoint_id,
474
+ json_group_array(
475
+ json_object(
476
+ 'type', ps.type,
477
+ 'value', CAST(ps.value AS TEXT)
478
+ )
479
+ ) as pending_sends
480
+ FROM writes as ps
481
+ WHERE ps.thread_id = ?
482
+ AND ps.checkpoint_id = ?
483
+ AND ps.channel = '${TASKS}'
484
+ ORDER BY ps.idx
485
+ `,
486
+ )
487
+ .get(threadId, parentCheckpointId) as { pending_sends: string };
488
+
489
+ const mutableCheckpoint = checkpoint;
490
+
491
+ // add pending sends to checkpoint
492
+ mutableCheckpoint.channel_values ??= {};
493
+ mutableCheckpoint.channel_values[TASKS] = await Promise.all(
494
+ JSON.parse(pending_sends).map(({ type, value }: PendingSendColumn) => this.serde.loadsTyped(type, value)),
495
+ );
496
+
497
+ // add to versions
498
+ mutableCheckpoint.channel_versions[TASKS] =
499
+ Object.keys(checkpoint.channel_versions).length > 0
500
+ ? maxChannelVersion(...Object.values(checkpoint.channel_versions))
501
+ : this.getNextVersion(undefined);
502
+ }
503
+ }