@langgraph-js/pure-graph 1.4.2 → 1.4.5

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 (51) hide show
  1. package/dist/adapter/hono/index.js +104 -7
  2. package/dist/adapter/hono/index.js.map +1 -0
  3. package/dist/adapter/nextjs/index.js +175 -2
  4. package/dist/adapter/nextjs/index.js.map +1 -0
  5. package/dist/{checkpoint-BHKV54sL.js → checkpoint-BQME4ngM.js} +2 -2
  6. package/dist/{checkpoint-BHKV54sL.js.map → checkpoint-BQME4ngM.js.map} +1 -1
  7. package/dist/{index-DcXE-SZb.js → createEndpoint-BcVhF-uq.js} +6 -17
  8. package/dist/createEndpoint-BcVhF-uq.js.map +1 -0
  9. package/dist/index.js +16 -5
  10. package/dist/index.js.map +1 -1
  11. package/dist/{queue-C6iEVbd2.js → queue-pSZu1PVS.js} +2 -2
  12. package/dist/{queue-C6iEVbd2.js.map → queue-pSZu1PVS.js.map} +1 -1
  13. package/dist/{threads-BUgBiCiK.js → threads-amMaxKul.js} +2 -2
  14. package/dist/{threads-BUgBiCiK.js.map → threads-amMaxKul.js.map} +1 -1
  15. package/dist/zod-BaCzVUl8.js +107 -0
  16. package/dist/zod-BaCzVUl8.js.map +1 -0
  17. package/package.json +2 -2
  18. package/dist/__vite-browser-external-DGN5jhtd.js +0 -4
  19. package/dist/__vite-browser-external-DGN5jhtd.js.map +0 -1
  20. package/dist/adapter/hono/assistants.js +0 -21
  21. package/dist/adapter/hono/endpoint.js +0 -2
  22. package/dist/adapter/hono/runs.js +0 -54
  23. package/dist/adapter/hono/threads.js +0 -30
  24. package/dist/adapter/nextjs/endpoint.js +0 -2
  25. package/dist/adapter/nextjs/router.js +0 -179
  26. package/dist/adapter/zod.js +0 -127
  27. package/dist/createEndpoint.js +0 -77
  28. package/dist/global.js +0 -13
  29. package/dist/graph/stream.js +0 -195
  30. package/dist/graph/stringify.js +0 -214
  31. package/dist/index-DcXE-SZb.js.map +0 -1
  32. package/dist/queue/JsonPlusSerializer.js +0 -138
  33. package/dist/queue/event_message.js +0 -27
  34. package/dist/queue/stream_queue.js +0 -177
  35. package/dist/storage/index.js +0 -64
  36. package/dist/storage/memory/checkpoint.js +0 -2
  37. package/dist/storage/memory/queue.js +0 -81
  38. package/dist/storage/memory/threads.js +0 -145
  39. package/dist/storage/pg/checkpoint.js +0 -9
  40. package/dist/storage/pg/threads.js +0 -303
  41. package/dist/storage/redis/queue.js +0 -130
  42. package/dist/storage/sqlite/DB.js +0 -14
  43. package/dist/storage/sqlite/checkpoint.js +0 -374
  44. package/dist/storage/sqlite/threads.js +0 -299
  45. package/dist/storage/sqlite/type.js +0 -1
  46. package/dist/threads/index.js +0 -1
  47. package/dist/tsconfig.tsbuildinfo +0 -1
  48. package/dist/types.js +0 -1
  49. package/dist/utils/createEntrypointGraph.js +0 -11
  50. package/dist/utils/getGraph.js +0 -18
  51. package/dist/utils/getLangGraphCommand.js +0 -13
@@ -1,299 +0,0 @@
1
- import { getGraph } from '../../utils/getGraph.js';
2
- import { serialiseAsDict } from '../../graph/stream.js';
3
- export class SQLiteThreadsManager {
4
- db;
5
- isSetup = false;
6
- constructor(checkpointer) {
7
- this.db = checkpointer.db;
8
- }
9
- async setup() {
10
- if (this.isSetup) {
11
- return;
12
- }
13
- // 创建 threads 表
14
- this.db.exec(`
15
- CREATE TABLE IF NOT EXISTS threads (
16
- thread_id TEXT PRIMARY KEY,
17
- created_at TEXT NOT NULL,
18
- updated_at TEXT NOT NULL,
19
- metadata TEXT NOT NULL DEFAULT '{}',
20
- status TEXT NOT NULL DEFAULT 'idle',
21
- [values] TEXT,
22
- interrupts TEXT NOT NULL DEFAULT '{}'
23
- )
24
- `);
25
- // 创建 runs 表
26
- this.db.exec(`
27
- CREATE TABLE IF NOT EXISTS runs (
28
- run_id TEXT PRIMARY KEY,
29
- thread_id TEXT NOT NULL,
30
- assistant_id TEXT NOT NULL,
31
- created_at TEXT NOT NULL,
32
- updated_at TEXT NOT NULL,
33
- status TEXT NOT NULL DEFAULT 'pending',
34
- metadata TEXT NOT NULL DEFAULT '{}',
35
- multitask_strategy TEXT NOT NULL DEFAULT 'reject',
36
- FOREIGN KEY (thread_id) REFERENCES threads(thread_id) ON DELETE CASCADE
37
- )
38
- `);
39
- // 创建索引以提高查询性能
40
- this.db.exec(`CREATE INDEX IF NOT EXISTS idx_threads_status ON threads(status)`);
41
- this.db.exec(`CREATE INDEX IF NOT EXISTS idx_threads_created_at ON threads(created_at)`);
42
- this.db.exec(`CREATE INDEX IF NOT EXISTS idx_threads_updated_at ON threads(updated_at)`);
43
- this.db.exec(`CREATE INDEX IF NOT EXISTS idx_runs_thread_id ON runs(thread_id)`);
44
- this.db.exec(`CREATE INDEX IF NOT EXISTS idx_runs_status ON runs(status)`);
45
- this.isSetup = true;
46
- }
47
- async create(payload) {
48
- const threadId = payload?.threadId || crypto.randomUUID();
49
- // 检查线程是否已存在
50
- if (payload?.ifExists === 'raise') {
51
- const existingThread = this.db.prepare('SELECT thread_id FROM threads WHERE thread_id = ?').get(threadId);
52
- if (existingThread) {
53
- throw new Error(`Thread with ID ${threadId} already exists.`);
54
- }
55
- }
56
- const now = new Date().toISOString();
57
- const metadata = JSON.stringify(payload?.metadata || {});
58
- const interrupts = JSON.stringify({});
59
- const thread = {
60
- thread_id: threadId,
61
- created_at: now,
62
- updated_at: now,
63
- metadata: payload?.metadata || {},
64
- status: 'idle',
65
- values: null,
66
- interrupts: {},
67
- };
68
- // 插入到数据库
69
- this.db
70
- .prepare(`
71
- INSERT INTO threads (thread_id, created_at, updated_at, metadata, status, [values], interrupts)
72
- VALUES (?, ?, ?, ?, ?, ?, ?)
73
- `)
74
- .run(threadId, now, now, metadata, 'idle', null, interrupts);
75
- return thread;
76
- }
77
- async search(query) {
78
- let sql = 'SELECT * FROM threads';
79
- const whereConditions = [];
80
- const params = [];
81
- // 构建 WHERE 条件
82
- if (query?.status) {
83
- whereConditions.push('status = ?');
84
- params.push(query.status);
85
- }
86
- if (query?.metadata) {
87
- for (const [key, value] of Object.entries(query.metadata)) {
88
- whereConditions.push(`json_extract(metadata, '$.${key}') = ?`);
89
- params.push(JSON.stringify(value));
90
- }
91
- }
92
- if (whereConditions.length > 0) {
93
- sql += ' WHERE ' + whereConditions.join(' AND ');
94
- }
95
- // 添加排序
96
- if (query?.sortBy) {
97
- sql += ` ORDER BY ${query.sortBy}`;
98
- if (query.sortOrder === 'desc') {
99
- sql += ' DESC';
100
- }
101
- else {
102
- sql += ' ASC';
103
- }
104
- }
105
- // 添加分页
106
- if (query?.limit) {
107
- sql += ` LIMIT ${query.limit}`;
108
- if (query?.offset) {
109
- sql += ` OFFSET ${query.offset}`;
110
- }
111
- }
112
- const rows = this.db.prepare(sql).all(...params);
113
- return rows.map((row) => ({
114
- thread_id: row.thread_id,
115
- created_at: row.created_at,
116
- updated_at: row.updated_at,
117
- metadata: JSON.parse(row.metadata),
118
- status: row.status,
119
- values: row.values ? JSON.parse(row.values) : null,
120
- interrupts: JSON.parse(row.interrupts),
121
- }));
122
- }
123
- async get(threadId) {
124
- const row = this.db.prepare('SELECT * FROM threads WHERE thread_id = ?').get(threadId);
125
- if (!row) {
126
- throw new Error(`Thread with ID ${threadId} not found.`);
127
- }
128
- return {
129
- thread_id: row.thread_id,
130
- created_at: row.created_at,
131
- updated_at: row.updated_at,
132
- metadata: JSON.parse(row.metadata),
133
- status: row.status,
134
- values: row.values ? JSON.parse(row.values) : null,
135
- interrupts: JSON.parse(row.interrupts),
136
- };
137
- }
138
- async set(threadId, thread) {
139
- // 检查线程是否存在
140
- const existingThread = this.db.prepare('SELECT thread_id FROM threads WHERE thread_id = ?').get(threadId);
141
- if (!existingThread) {
142
- throw new Error(`Thread with ID ${threadId} not found.`);
143
- }
144
- const updateFields = [];
145
- const values = [];
146
- if (thread.metadata !== undefined) {
147
- updateFields.push('metadata = ?');
148
- values.push(JSON.stringify(thread.metadata));
149
- }
150
- if (thread.status !== undefined) {
151
- updateFields.push('status = ?');
152
- values.push(thread.status);
153
- }
154
- if (thread.values !== undefined) {
155
- updateFields.push('[values] = ?');
156
- values.push(thread.values ? JSON.stringify(thread.values) : null);
157
- }
158
- if (thread.interrupts !== undefined) {
159
- updateFields.push('interrupts = ?');
160
- values.push(JSON.stringify(thread.interrupts));
161
- }
162
- // 总是更新 updated_at
163
- updateFields.push('updated_at = ?');
164
- values.push(new Date().toISOString());
165
- if (updateFields.length > 0) {
166
- values.push(threadId);
167
- this.db
168
- .prepare(`
169
- UPDATE threads
170
- SET ${updateFields.join(', ')}
171
- WHERE thread_id = ?
172
- `)
173
- .run(...values);
174
- }
175
- }
176
- async updateState(threadId, thread) {
177
- // 从数据库查询线程信息
178
- const row = this.db.prepare('SELECT * FROM threads WHERE thread_id = ?').get(threadId);
179
- if (!row) {
180
- throw new Error(`Thread with ID ${threadId} not found.`);
181
- }
182
- const targetThread = {
183
- thread_id: row.thread_id,
184
- created_at: row.created_at,
185
- updated_at: row.updated_at,
186
- metadata: JSON.parse(row.metadata),
187
- status: row.status,
188
- values: row.values ? JSON.parse(row.values) : null,
189
- interrupts: JSON.parse(row.interrupts),
190
- };
191
- if (targetThread.status === 'busy') {
192
- throw new Error(`Thread with ID ${threadId} is busy, can't update state.`);
193
- }
194
- if (!targetThread.metadata?.graph_id) {
195
- throw new Error(`Thread with ID ${threadId} has no graph_id.`);
196
- }
197
- const graphId = targetThread.metadata?.graph_id;
198
- const config = {
199
- configurable: {
200
- thread_id: threadId,
201
- graph_id: graphId,
202
- },
203
- };
204
- const graph = await getGraph(graphId, config);
205
- const nextConfig = await graph.updateState(config, thread.values);
206
- const graphState = await graph.getState(config);
207
- await this.set(threadId, { values: JSON.parse(serialiseAsDict(graphState.values)) });
208
- return nextConfig;
209
- }
210
- async delete(threadId) {
211
- const result = this.db.prepare('DELETE FROM threads WHERE thread_id = ?').run(threadId);
212
- if (result.changes === 0) {
213
- throw new Error(`Thread with ID ${threadId} not found.`);
214
- }
215
- }
216
- async createRun(threadId, assistantId, payload) {
217
- const runId = crypto.randomUUID();
218
- const now = new Date().toISOString();
219
- const metadata = JSON.stringify(payload?.metadata ?? {});
220
- const run = {
221
- run_id: runId,
222
- thread_id: threadId,
223
- assistant_id: assistantId,
224
- created_at: now,
225
- updated_at: now,
226
- status: 'pending',
227
- metadata: payload?.metadata ?? {},
228
- multitask_strategy: 'reject',
229
- };
230
- // 插入到数据库
231
- this.db
232
- .prepare(`
233
- INSERT INTO runs (run_id, thread_id, assistant_id, created_at, updated_at, status, metadata, multitask_strategy)
234
- VALUES (?, ?, ?, ?, ?, ?, ?, ?)
235
- `)
236
- .run(runId, threadId, assistantId, now, now, 'pending', metadata, 'reject');
237
- return run;
238
- }
239
- async listRuns(threadId, options) {
240
- let sql = 'SELECT * FROM runs WHERE thread_id = ?';
241
- const params = [threadId];
242
- if (options?.status) {
243
- sql += ' AND status = ?';
244
- params.push(options.status);
245
- }
246
- sql += ' ORDER BY created_at DESC';
247
- if (options?.limit) {
248
- sql += ` LIMIT ${options.limit}`;
249
- if (options?.offset) {
250
- sql += ` OFFSET ${options.offset}`;
251
- }
252
- }
253
- const rows = this.db.prepare(sql).all(...params);
254
- return rows.map((row) => ({
255
- run_id: row.run_id,
256
- thread_id: row.thread_id,
257
- assistant_id: row.assistant_id,
258
- created_at: row.created_at,
259
- updated_at: row.updated_at,
260
- status: row.status,
261
- metadata: JSON.parse(row.metadata),
262
- multitask_strategy: row.multitask_strategy,
263
- }));
264
- }
265
- async updateRun(runId, run) {
266
- // 检查运行是否存在
267
- const existingRun = this.db.prepare('SELECT run_id FROM runs WHERE run_id = ?').get(runId);
268
- if (!existingRun) {
269
- throw new Error(`Run with ID ${runId} not found.`);
270
- }
271
- const updateFields = [];
272
- const values = [];
273
- if (run.status !== undefined) {
274
- updateFields.push('status = ?');
275
- values.push(run.status);
276
- }
277
- if (run.metadata !== undefined) {
278
- updateFields.push('metadata = ?');
279
- values.push(JSON.stringify(run.metadata));
280
- }
281
- if (run.multitask_strategy !== undefined) {
282
- updateFields.push('multitask_strategy = ?');
283
- values.push(run.multitask_strategy);
284
- }
285
- // 总是更新 updated_at
286
- updateFields.push('updated_at = ?');
287
- values.push(new Date().toISOString());
288
- if (updateFields.length > 0) {
289
- values.push(runId);
290
- this.db
291
- .prepare(`
292
- UPDATE runs
293
- SET ${updateFields.join(', ')}
294
- WHERE run_id = ?
295
- `)
296
- .run(...values);
297
- }
298
- }
299
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};