@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,366 @@
|
|
|
1
|
+
import { BaseThreadsManager } from '../../threads/index.js';
|
|
2
|
+
import {
|
|
3
|
+
Command,
|
|
4
|
+
Metadata,
|
|
5
|
+
OnConflictBehavior,
|
|
6
|
+
Run,
|
|
7
|
+
RunStatus,
|
|
8
|
+
SortOrder,
|
|
9
|
+
Thread,
|
|
10
|
+
ThreadSortBy,
|
|
11
|
+
ThreadStatus,
|
|
12
|
+
} from '@langgraph-js/sdk';
|
|
13
|
+
import type { SqliteSaver } from './checkpoint.js';
|
|
14
|
+
import type { DatabaseType } from './type.js';
|
|
15
|
+
interface ThreadRow {
|
|
16
|
+
thread_id: string;
|
|
17
|
+
created_at: string;
|
|
18
|
+
updated_at: string;
|
|
19
|
+
metadata: string;
|
|
20
|
+
status: string;
|
|
21
|
+
values: string;
|
|
22
|
+
interrupts: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface RunRow {
|
|
26
|
+
run_id: string;
|
|
27
|
+
thread_id: string;
|
|
28
|
+
assistant_id: string;
|
|
29
|
+
created_at: string;
|
|
30
|
+
updated_at: string;
|
|
31
|
+
status: string;
|
|
32
|
+
metadata: string;
|
|
33
|
+
multitask_strategy: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class SQLiteThreadsManager<ValuesType = unknown> extends BaseThreadsManager {
|
|
37
|
+
db: DatabaseType;
|
|
38
|
+
private isSetup: boolean = false;
|
|
39
|
+
|
|
40
|
+
constructor(checkpointer: SqliteSaver) {
|
|
41
|
+
super();
|
|
42
|
+
this.db = checkpointer.db;
|
|
43
|
+
this.setup();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private setup(): void {
|
|
47
|
+
if (this.isSetup) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 创建 threads 表
|
|
52
|
+
this.db.exec(`
|
|
53
|
+
CREATE TABLE IF NOT EXISTS threads (
|
|
54
|
+
thread_id TEXT PRIMARY KEY,
|
|
55
|
+
created_at TEXT NOT NULL,
|
|
56
|
+
updated_at TEXT NOT NULL,
|
|
57
|
+
metadata TEXT NOT NULL DEFAULT '{}',
|
|
58
|
+
status TEXT NOT NULL DEFAULT 'idle',
|
|
59
|
+
[values] TEXT,
|
|
60
|
+
interrupts TEXT NOT NULL DEFAULT '{}'
|
|
61
|
+
)
|
|
62
|
+
`);
|
|
63
|
+
|
|
64
|
+
// 创建 runs 表
|
|
65
|
+
this.db.exec(`
|
|
66
|
+
CREATE TABLE IF NOT EXISTS runs (
|
|
67
|
+
run_id TEXT PRIMARY KEY,
|
|
68
|
+
thread_id TEXT NOT NULL,
|
|
69
|
+
assistant_id TEXT NOT NULL,
|
|
70
|
+
created_at TEXT NOT NULL,
|
|
71
|
+
updated_at TEXT NOT NULL,
|
|
72
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
73
|
+
metadata TEXT NOT NULL DEFAULT '{}',
|
|
74
|
+
multitask_strategy TEXT NOT NULL DEFAULT 'reject',
|
|
75
|
+
FOREIGN KEY (thread_id) REFERENCES threads(thread_id) ON DELETE CASCADE
|
|
76
|
+
)
|
|
77
|
+
`);
|
|
78
|
+
|
|
79
|
+
// 创建索引以提高查询性能
|
|
80
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_threads_status ON threads(status)`);
|
|
81
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_threads_created_at ON threads(created_at)`);
|
|
82
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_threads_updated_at ON threads(updated_at)`);
|
|
83
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_runs_thread_id ON runs(thread_id)`);
|
|
84
|
+
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_runs_status ON runs(status)`);
|
|
85
|
+
|
|
86
|
+
this.isSetup = true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async create(payload?: {
|
|
90
|
+
metadata?: Metadata;
|
|
91
|
+
threadId?: string;
|
|
92
|
+
ifExists?: OnConflictBehavior;
|
|
93
|
+
graphId?: string;
|
|
94
|
+
supersteps?: Array<{ updates: Array<{ values: unknown; command?: Command; asNode: string }> }>;
|
|
95
|
+
}): Promise<Thread<ValuesType>> {
|
|
96
|
+
const threadId = payload?.threadId || crypto.randomUUID();
|
|
97
|
+
|
|
98
|
+
// 检查线程是否已存在
|
|
99
|
+
if (payload?.ifExists === 'raise') {
|
|
100
|
+
const existingThread = this.db.prepare('SELECT thread_id FROM threads WHERE thread_id = ?').get(threadId);
|
|
101
|
+
if (existingThread) {
|
|
102
|
+
throw new Error(`Thread with ID ${threadId} already exists.`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const now = new Date().toISOString();
|
|
107
|
+
const metadata = JSON.stringify(payload?.metadata || {});
|
|
108
|
+
const interrupts = JSON.stringify({});
|
|
109
|
+
|
|
110
|
+
const thread: Thread<ValuesType> = {
|
|
111
|
+
thread_id: threadId,
|
|
112
|
+
created_at: now,
|
|
113
|
+
updated_at: now,
|
|
114
|
+
metadata: payload?.metadata || {},
|
|
115
|
+
status: 'idle',
|
|
116
|
+
values: null as unknown as ValuesType,
|
|
117
|
+
interrupts: {},
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// 插入到数据库
|
|
121
|
+
this.db
|
|
122
|
+
.prepare(
|
|
123
|
+
`
|
|
124
|
+
INSERT INTO threads (thread_id, created_at, updated_at, metadata, status, [values], interrupts)
|
|
125
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
126
|
+
`,
|
|
127
|
+
)
|
|
128
|
+
.run(threadId, now, now, metadata, 'idle', null, interrupts);
|
|
129
|
+
|
|
130
|
+
return thread;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async search(query?: {
|
|
134
|
+
metadata?: Metadata;
|
|
135
|
+
limit?: number;
|
|
136
|
+
offset?: number;
|
|
137
|
+
status?: ThreadStatus;
|
|
138
|
+
sortBy?: ThreadSortBy;
|
|
139
|
+
sortOrder?: SortOrder;
|
|
140
|
+
}): Promise<Thread<ValuesType>[]> {
|
|
141
|
+
let sql = 'SELECT * FROM threads';
|
|
142
|
+
const whereConditions: string[] = [];
|
|
143
|
+
const params: any[] = [];
|
|
144
|
+
|
|
145
|
+
// 构建 WHERE 条件
|
|
146
|
+
if (query?.status) {
|
|
147
|
+
whereConditions.push('status = ?');
|
|
148
|
+
params.push(query.status);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (query?.metadata) {
|
|
152
|
+
for (const [key, value] of Object.entries(query.metadata)) {
|
|
153
|
+
whereConditions.push(`json_extract(metadata, '$.${key}') = ?`);
|
|
154
|
+
params.push(JSON.stringify(value));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (whereConditions.length > 0) {
|
|
159
|
+
sql += ' WHERE ' + whereConditions.join(' AND ');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// 添加排序
|
|
163
|
+
if (query?.sortBy) {
|
|
164
|
+
sql += ` ORDER BY ${query.sortBy}`;
|
|
165
|
+
if (query.sortOrder === 'desc') {
|
|
166
|
+
sql += ' DESC';
|
|
167
|
+
} else {
|
|
168
|
+
sql += ' ASC';
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 添加分页
|
|
173
|
+
if (query?.limit) {
|
|
174
|
+
sql += ` LIMIT ${query.limit}`;
|
|
175
|
+
if (query?.offset) {
|
|
176
|
+
sql += ` OFFSET ${query.offset}`;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const rows = this.db.prepare(sql).all(...params) as ThreadRow[];
|
|
181
|
+
|
|
182
|
+
return rows.map((row) => ({
|
|
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 as ThreadStatus,
|
|
188
|
+
values: row.values ? JSON.parse(row.values) : (null as unknown as ValuesType),
|
|
189
|
+
interrupts: JSON.parse(row.interrupts),
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async get(threadId: string): Promise<Thread<ValuesType>> {
|
|
194
|
+
const row = this.db.prepare('SELECT * FROM threads WHERE thread_id = ?').get(threadId) as ThreadRow;
|
|
195
|
+
if (!row) {
|
|
196
|
+
throw new Error(`Thread with ID ${threadId} not found.`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
thread_id: row.thread_id,
|
|
201
|
+
created_at: row.created_at,
|
|
202
|
+
updated_at: row.updated_at,
|
|
203
|
+
metadata: JSON.parse(row.metadata),
|
|
204
|
+
status: row.status as ThreadStatus,
|
|
205
|
+
values: row.values ? JSON.parse(row.values) : (null as unknown as ValuesType),
|
|
206
|
+
interrupts: JSON.parse(row.interrupts),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
async set(threadId: string, thread: Partial<Thread<ValuesType>>): Promise<void> {
|
|
210
|
+
// 检查线程是否存在
|
|
211
|
+
const existingThread = this.db.prepare('SELECT thread_id FROM threads WHERE thread_id = ?').get(threadId);
|
|
212
|
+
if (!existingThread) {
|
|
213
|
+
throw new Error(`Thread with ID ${threadId} not found.`);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const updateFields: string[] = [];
|
|
217
|
+
const values: any[] = [];
|
|
218
|
+
|
|
219
|
+
if (thread.metadata !== undefined) {
|
|
220
|
+
updateFields.push('metadata = ?');
|
|
221
|
+
values.push(JSON.stringify(thread.metadata));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (thread.status !== undefined) {
|
|
225
|
+
updateFields.push('status = ?');
|
|
226
|
+
values.push(thread.status);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (thread.values !== undefined) {
|
|
230
|
+
updateFields.push('[values] = ?');
|
|
231
|
+
values.push(thread.values ? JSON.stringify(thread.values) : null);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (thread.interrupts !== undefined) {
|
|
235
|
+
updateFields.push('interrupts = ?');
|
|
236
|
+
values.push(JSON.stringify(thread.interrupts));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// 总是更新 updated_at
|
|
240
|
+
updateFields.push('updated_at = ?');
|
|
241
|
+
values.push(new Date().toISOString());
|
|
242
|
+
|
|
243
|
+
if (updateFields.length > 0) {
|
|
244
|
+
values.push(threadId);
|
|
245
|
+
this.db
|
|
246
|
+
.prepare(
|
|
247
|
+
`
|
|
248
|
+
UPDATE threads
|
|
249
|
+
SET ${updateFields.join(', ')}
|
|
250
|
+
WHERE thread_id = ?
|
|
251
|
+
`,
|
|
252
|
+
)
|
|
253
|
+
.run(...values);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async delete(threadId: string): Promise<void> {
|
|
257
|
+
const result = this.db.prepare('DELETE FROM threads WHERE thread_id = ?').run(threadId);
|
|
258
|
+
if (result.changes === 0) {
|
|
259
|
+
throw new Error(`Thread with ID ${threadId} not found.`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
async createRun(threadId: string, assistantId: string, payload?: { metadata?: Metadata }): Promise<Run> {
|
|
263
|
+
const runId = crypto.randomUUID();
|
|
264
|
+
const now = new Date().toISOString();
|
|
265
|
+
const metadata = JSON.stringify(payload?.metadata ?? {});
|
|
266
|
+
|
|
267
|
+
const run: Run = {
|
|
268
|
+
run_id: runId,
|
|
269
|
+
thread_id: threadId,
|
|
270
|
+
assistant_id: assistantId,
|
|
271
|
+
created_at: now,
|
|
272
|
+
updated_at: now,
|
|
273
|
+
status: 'pending',
|
|
274
|
+
metadata: payload?.metadata ?? {},
|
|
275
|
+
multitask_strategy: 'reject',
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
// 插入到数据库
|
|
279
|
+
this.db
|
|
280
|
+
.prepare(
|
|
281
|
+
`
|
|
282
|
+
INSERT INTO runs (run_id, thread_id, assistant_id, created_at, updated_at, status, metadata, multitask_strategy)
|
|
283
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
284
|
+
`,
|
|
285
|
+
)
|
|
286
|
+
.run(runId, threadId, assistantId, now, now, 'pending', metadata, 'reject');
|
|
287
|
+
|
|
288
|
+
return run;
|
|
289
|
+
}
|
|
290
|
+
async listRuns(
|
|
291
|
+
threadId: string,
|
|
292
|
+
options?: { limit?: number; offset?: number; status?: RunStatus },
|
|
293
|
+
): Promise<Run[]> {
|
|
294
|
+
let sql = 'SELECT * FROM runs WHERE thread_id = ?';
|
|
295
|
+
const params: any[] = [threadId];
|
|
296
|
+
|
|
297
|
+
if (options?.status) {
|
|
298
|
+
sql += ' AND status = ?';
|
|
299
|
+
params.push(options.status);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
sql += ' ORDER BY created_at DESC';
|
|
303
|
+
|
|
304
|
+
if (options?.limit) {
|
|
305
|
+
sql += ` LIMIT ${options.limit}`;
|
|
306
|
+
if (options?.offset) {
|
|
307
|
+
sql += ` OFFSET ${options.offset}`;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const rows = this.db.prepare(sql).all(...params) as RunRow[];
|
|
312
|
+
|
|
313
|
+
return rows.map((row) => ({
|
|
314
|
+
run_id: row.run_id,
|
|
315
|
+
thread_id: row.thread_id,
|
|
316
|
+
assistant_id: row.assistant_id,
|
|
317
|
+
created_at: row.created_at,
|
|
318
|
+
updated_at: row.updated_at,
|
|
319
|
+
status: row.status as RunStatus,
|
|
320
|
+
metadata: JSON.parse(row.metadata),
|
|
321
|
+
multitask_strategy: row.multitask_strategy as 'reject',
|
|
322
|
+
}));
|
|
323
|
+
}
|
|
324
|
+
async updateRun(runId: string, run: Partial<Run>): Promise<void> {
|
|
325
|
+
// 检查运行是否存在
|
|
326
|
+
const existingRun = this.db.prepare('SELECT run_id FROM runs WHERE run_id = ?').get(runId);
|
|
327
|
+
if (!existingRun) {
|
|
328
|
+
throw new Error(`Run with ID ${runId} not found.`);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const updateFields: string[] = [];
|
|
332
|
+
const values: any[] = [];
|
|
333
|
+
|
|
334
|
+
if (run.status !== undefined) {
|
|
335
|
+
updateFields.push('status = ?');
|
|
336
|
+
values.push(run.status);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (run.metadata !== undefined) {
|
|
340
|
+
updateFields.push('metadata = ?');
|
|
341
|
+
values.push(JSON.stringify(run.metadata));
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (run.multitask_strategy !== undefined) {
|
|
345
|
+
updateFields.push('multitask_strategy = ?');
|
|
346
|
+
values.push(run.multitask_strategy);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// 总是更新 updated_at
|
|
350
|
+
updateFields.push('updated_at = ?');
|
|
351
|
+
values.push(new Date().toISOString());
|
|
352
|
+
|
|
353
|
+
if (updateFields.length > 0) {
|
|
354
|
+
values.push(runId);
|
|
355
|
+
this.db
|
|
356
|
+
.prepare(
|
|
357
|
+
`
|
|
358
|
+
UPDATE runs
|
|
359
|
+
SET ${updateFields.join(', ')}
|
|
360
|
+
WHERE run_id = ?
|
|
361
|
+
`,
|
|
362
|
+
)
|
|
363
|
+
.run(...values);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
|
|
8
|
+
interface Statement {
|
|
9
|
+
run(...params: any[]): { changes: number; lastInsertRowid: number };
|
|
10
|
+
get(...params: any[]): any;
|
|
11
|
+
all(...params: any[]): any[];
|
|
12
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AnnotationRoot, Pregel, StateGraph } from '@langchain/langgraph';
|
|
2
|
+
|
|
3
|
+
export const createEntrypointGraph = <StateType extends AnnotationRoot<any>, ConfigType extends AnnotationRoot<any>>({
|
|
4
|
+
stateSchema,
|
|
5
|
+
config,
|
|
6
|
+
graph,
|
|
7
|
+
}: {
|
|
8
|
+
stateSchema: StateType;
|
|
9
|
+
config?: ConfigType;
|
|
10
|
+
graph: Pregel<any, any>;
|
|
11
|
+
}) => {
|
|
12
|
+
const name = graph.getName();
|
|
13
|
+
return new StateGraph(stateSchema, config)
|
|
14
|
+
.addNode(name, (state, config) => graph.invoke(state, config))
|
|
15
|
+
.addEdge('__start__', name)
|
|
16
|
+
.addEdge(name, '__end__')
|
|
17
|
+
.compile({
|
|
18
|
+
name,
|
|
19
|
+
});
|
|
20
|
+
};
|
package/src/utils/getGraph.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
CompiledStateGraph,
|
|
6
6
|
LangGraphRunnableConfig,
|
|
7
7
|
} from '@langchain/langgraph';
|
|
8
|
-
import {
|
|
8
|
+
import { LangGraphGlobal } from '../global';
|
|
9
9
|
|
|
10
10
|
export type CompiledGraphFactory<T extends string> = (config: {
|
|
11
11
|
configurable?: Record<string, unknown>;
|
|
@@ -33,9 +33,9 @@ export async function getGraph(
|
|
|
33
33
|
typeof GRAPHS[graphId] === 'function' ? await GRAPHS[graphId](config ?? { configurable: {} }) : GRAPHS[graphId];
|
|
34
34
|
|
|
35
35
|
if (typeof options?.checkpointer !== 'undefined') {
|
|
36
|
-
compiled.checkpointer = options?.checkpointer ?? globalCheckPointer;
|
|
36
|
+
compiled.checkpointer = options?.checkpointer ?? LangGraphGlobal.globalCheckPointer;
|
|
37
37
|
} else {
|
|
38
|
-
compiled.checkpointer = globalCheckPointer;
|
|
38
|
+
compiled.checkpointer = LangGraphGlobal.globalCheckPointer;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
compiled.store = options?.store ?? undefined;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { entrypoint } from '@langchain/langgraph';
|
|
2
|
+
import { createReactAgent, createReactAgentAnnotation } from '@langchain/langgraph/prebuilt';
|
|
3
|
+
import { createState } from '@langgraph-js/pro';
|
|
4
|
+
import { createEntrypointGraph } from '../../src/';
|
|
5
|
+
import { ChatOpenAI } from '@langchain/openai';
|
|
6
|
+
const State = createState(createReactAgentAnnotation()).build({});
|
|
7
|
+
const workflow = entrypoint('test-entrypoint', async (state: typeof State.State) => {
|
|
8
|
+
const agent = createReactAgent({
|
|
9
|
+
llm: new ChatOpenAI({
|
|
10
|
+
model: 'qwen-plus',
|
|
11
|
+
}),
|
|
12
|
+
prompt: '你是一个智能助手',
|
|
13
|
+
tools: [],
|
|
14
|
+
});
|
|
15
|
+
return agent.invoke(state);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const graph = createEntrypointGraph({
|
|
19
|
+
stateSchema: State,
|
|
20
|
+
graph: workflow,
|
|
21
|
+
});
|
package/test/graph/index.ts
CHANGED
|
@@ -1,21 +1,60 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { interrupt, StateGraph } from '@langchain/langgraph';
|
|
2
|
+
import { HumanInterrupt } from '@langchain/langgraph/prebuilt';
|
|
3
|
+
import { createReactAgentAnnotation } from '@langchain/langgraph/prebuilt';
|
|
3
4
|
import { createState } from '@langgraph-js/pro';
|
|
4
5
|
const state = createState(createReactAgentAnnotation()).build({});
|
|
5
6
|
export const graph = new StateGraph(state)
|
|
6
7
|
.addNode('agent', async (state) => {
|
|
7
|
-
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
8
|
-
console.log('done');
|
|
9
8
|
return {
|
|
10
9
|
messages: [
|
|
11
10
|
...state.messages,
|
|
12
11
|
{
|
|
13
12
|
role: 'ai',
|
|
14
|
-
content: '
|
|
13
|
+
content: 'Processing...',
|
|
14
|
+
},
|
|
15
|
+
],
|
|
16
|
+
};
|
|
17
|
+
})
|
|
18
|
+
.addNode('interrupt', async (state) => {
|
|
19
|
+
// interrupt
|
|
20
|
+
const response = interrupt([
|
|
21
|
+
{
|
|
22
|
+
action_request: {
|
|
23
|
+
action: 'test',
|
|
24
|
+
args: {
|
|
25
|
+
a: 1,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
config: {
|
|
29
|
+
allow_ignore: true,
|
|
30
|
+
allow_respond: true,
|
|
31
|
+
allow_edit: true,
|
|
32
|
+
allow_accept: true,
|
|
33
|
+
},
|
|
34
|
+
description: 'Please review the tool call',
|
|
35
|
+
} as HumanInterrupt,
|
|
36
|
+
])[0];
|
|
37
|
+
return {
|
|
38
|
+
messages: [
|
|
39
|
+
...state.messages,
|
|
40
|
+
{
|
|
41
|
+
role: 'ai',
|
|
42
|
+
content: response,
|
|
15
43
|
},
|
|
16
44
|
],
|
|
17
45
|
};
|
|
18
46
|
})
|
|
19
47
|
.addEdge('__start__', 'agent')
|
|
20
|
-
.
|
|
48
|
+
.addConditionalEdges(
|
|
49
|
+
'agent',
|
|
50
|
+
(state) => {
|
|
51
|
+
// 条件:如果消息数量大于1,则走interrupt路径
|
|
52
|
+
return state.messages.length > 1 ? 'interrupt' : '__end__';
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
interrupt: 'interrupt',
|
|
56
|
+
__end__: '__end__',
|
|
57
|
+
},
|
|
58
|
+
)
|
|
59
|
+
.addEdge('interrupt', '__end__')
|
|
21
60
|
.compile();
|
package/test/hono.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { registerGraph } from '../src/createEndpoint';
|
|
2
2
|
import { graph } from './graph/index';
|
|
3
|
+
import { graph as entrypointGraph } from './graph/entrypoint';
|
|
4
|
+
// import { graph as agentGraph } from '../packages/agent-graph/src/index';
|
|
3
5
|
import { Hono } from 'hono';
|
|
4
6
|
import LangGraphApp from '../src/adapter/hono/index';
|
|
7
|
+
|
|
5
8
|
registerGraph('test', graph);
|
|
9
|
+
// registerGraph('agent-graph', agentGraph);
|
|
10
|
+
registerGraph('test-entrypoint', entrypointGraph);
|
|
6
11
|
|
|
7
12
|
const app = new Hono();
|
|
8
13
|
app.route('/', LangGraphApp);
|
package/test/test.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { registerGraph } from '../src/createEndpoint';
|
|
2
|
-
import { graph } from '/Users/konghayao/code/ai/code-graph/agents/code/graph';
|
|
3
|
-
import { Hono } from 'hono';
|
|
4
|
-
import LangGraphApp from '../src/adapter/hono/index';
|
|
5
|
-
registerGraph('code', graph);
|
|
6
|
-
|
|
7
|
-
const app = new Hono();
|
|
8
|
-
app.route('/', LangGraphApp);
|
|
9
|
-
|
|
10
|
-
export default app;
|