@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,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 {};
@@ -0,0 +1,14 @@
1
+ import { AnnotationRoot, Pregel } from '@langchain/langgraph';
2
+ export declare const createEntrypointGraph: <StateType extends AnnotationRoot<any>, ConfigType extends AnnotationRoot<any>>({ stateSchema, config, graph, }: {
3
+ stateSchema: StateType;
4
+ config?: ConfigType;
5
+ graph: Pregel<any, any>;
6
+ }) => import("@langchain/langgraph").CompiledStateGraph<{
7
+ [x: string]: any;
8
+ }, {
9
+ [x: string]: any;
10
+ } | {
11
+ [x: string]: any;
12
+ }, string, any, any, any, {
13
+ [x: string]: any;
14
+ }>;
@@ -0,0 +1,11 @@
1
+ import { StateGraph } from '@langchain/langgraph';
2
+ export const createEntrypointGraph = ({ stateSchema, config, graph, }) => {
3
+ const name = graph.getName();
4
+ return new StateGraph(stateSchema, config)
5
+ .addNode(name, (state, config) => graph.invoke(state, config))
6
+ .addEdge('__start__', name)
7
+ .addEdge(name, '__end__')
8
+ .compile({
9
+ name,
10
+ });
11
+ };
@@ -1,4 +1,4 @@
1
- import { globalCheckPointer } from '../global';
1
+ import { LangGraphGlobal } from '../global';
2
2
  export const GRAPHS = {};
3
3
  export async function registerGraph(graphId, graph) {
4
4
  GRAPHS[graphId] = graph;
@@ -8,10 +8,10 @@ export async function getGraph(graphId, config, options) {
8
8
  throw new Error(`Graph "${graphId}" not found`);
9
9
  const compiled = typeof GRAPHS[graphId] === 'function' ? await GRAPHS[graphId](config ?? { configurable: {} }) : GRAPHS[graphId];
10
10
  if (typeof options?.checkpointer !== 'undefined') {
11
- compiled.checkpointer = options?.checkpointer ?? globalCheckPointer;
11
+ compiled.checkpointer = options?.checkpointer ?? LangGraphGlobal.globalCheckPointer;
12
12
  }
13
13
  else {
14
- compiled.checkpointer = globalCheckPointer;
14
+ compiled.checkpointer = LangGraphGlobal.globalCheckPointer;
15
15
  }
16
16
  compiled.store = options?.store ?? undefined;
17
17
  return compiled;
@@ -0,0 +1,36 @@
1
+ This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
2
+
3
+ ## Getting Started
4
+
5
+ First, run the development server:
6
+
7
+ ```bash
8
+ npm run dev
9
+ # or
10
+ yarn dev
11
+ # or
12
+ pnpm dev
13
+ # or
14
+ bun dev
15
+ ```
16
+
17
+ Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
18
+
19
+ You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
20
+
21
+ This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
22
+
23
+ ## Learn More
24
+
25
+ To learn more about Next.js, take a look at the following resources:
26
+
27
+ - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28
+ - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
29
+
30
+ You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
31
+
32
+ ## Deploy on Vercel
33
+
34
+ The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
35
+
36
+ Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
@@ -0,0 +1,10 @@
1
+ import {
2
+ GET,
3
+ POST,
4
+ DELETE,
5
+ } from "@langgraph-js/pure-graph/dist/adapter/nextjs/router.js";
6
+ import { registerGraph } from "@langgraph-js/pure-graph";
7
+ import { graph } from "../../../../../../test/graph/index";
8
+ registerGraph("test", graph);
9
+
10
+ export { GET, POST, DELETE };
Binary file
@@ -0,0 +1,26 @@
1
+ @import "tailwindcss";
2
+
3
+ :root {
4
+ --background: #ffffff;
5
+ --foreground: #171717;
6
+ }
7
+
8
+ @theme inline {
9
+ --color-background: var(--background);
10
+ --color-foreground: var(--foreground);
11
+ --font-sans: var(--font-geist-sans);
12
+ --font-mono: var(--font-geist-mono);
13
+ }
14
+
15
+ @media (prefers-color-scheme: dark) {
16
+ :root {
17
+ --background: #0a0a0a;
18
+ --foreground: #ededed;
19
+ }
20
+ }
21
+
22
+ body {
23
+ background: var(--background);
24
+ color: var(--foreground);
25
+ font-family: Arial, Helvetica, sans-serif;
26
+ }
@@ -0,0 +1,34 @@
1
+ import type { Metadata } from "next";
2
+ import { Geist, Geist_Mono } from "next/font/google";
3
+ import "./globals.css";
4
+
5
+ const geistSans = Geist({
6
+ variable: "--font-geist-sans",
7
+ subsets: ["latin"],
8
+ });
9
+
10
+ const geistMono = Geist_Mono({
11
+ variable: "--font-geist-mono",
12
+ subsets: ["latin"],
13
+ });
14
+
15
+ export const metadata: Metadata = {
16
+ title: "Create Next App",
17
+ description: "Generated by create next app",
18
+ };
19
+
20
+ export default function RootLayout({
21
+ children,
22
+ }: Readonly<{
23
+ children: React.ReactNode;
24
+ }>) {
25
+ return (
26
+ <html lang="en">
27
+ <body
28
+ className={`${geistSans.variable} ${geistMono.variable} antialiased`}
29
+ >
30
+ {children}
31
+ </body>
32
+ </html>
33
+ );
34
+ }
@@ -0,0 +1,211 @@
1
+ "use client";
2
+
3
+ import { useState } from "react";
4
+
5
+ export default function Home() {
6
+ const [result, setResult] = useState<string>("");
7
+ const [loading, setLoading] = useState(false);
8
+
9
+ const testAPI = async (
10
+ endpoint: string,
11
+ method: string = "GET",
12
+ body?: any
13
+ ) => {
14
+ setLoading(true);
15
+ setResult("");
16
+
17
+ try {
18
+ const options: RequestInit = {
19
+ method,
20
+ headers: {
21
+ "Content-Type": "application/json",
22
+ },
23
+ };
24
+
25
+ if (body && method !== "GET") {
26
+ options.body = JSON.stringify(body);
27
+ }
28
+
29
+ const response = await fetch(`/api${endpoint}`, options);
30
+ const data = await response.text();
31
+
32
+ setResult(`状态: ${response.status}\n响应: ${data}`);
33
+ } catch (error) {
34
+ setResult(`错误: ${error}`);
35
+ } finally {
36
+ setLoading(false);
37
+ }
38
+ };
39
+
40
+ const testStream = async () => {
41
+ setLoading(true);
42
+ setResult("");
43
+
44
+ try {
45
+ // 先创建一个线程
46
+ const threadResponse = await fetch("/api/threads", {
47
+ method: "POST",
48
+ headers: { "Content-Type": "application/json" },
49
+ body: JSON.stringify({}),
50
+ });
51
+ const thread = await threadResponse.json();
52
+
53
+ // 测试流式接口
54
+ const response = await fetch(
55
+ `/api/threads/${thread.thread_id}/runs/stream`,
56
+ {
57
+ method: "POST",
58
+ headers: { "Content-Type": "application/json" },
59
+ body: JSON.stringify({
60
+ assistant_id: "test-assistant",
61
+ input: { message: "Hello" },
62
+ }),
63
+ }
64
+ );
65
+
66
+ if (response.body) {
67
+ const reader = response.body.getReader();
68
+ const decoder = new TextDecoder();
69
+ let result = "流式响应:\n";
70
+
71
+ while (true) {
72
+ const { done, value } = await reader.read();
73
+ if (done) break;
74
+
75
+ const chunk = decoder.decode(value);
76
+ result += chunk;
77
+ setResult(result);
78
+ }
79
+ }
80
+ } catch (error) {
81
+ setResult(`错误: ${error}`);
82
+ } finally {
83
+ setLoading(false);
84
+ }
85
+ };
86
+
87
+ return (
88
+ <div className="font-sans min-h-screen p-8">
89
+ <main className="max-w-4xl mx-auto">
90
+ <h1 className="text-3xl font-bold mb-8">
91
+ Pure Graph Next.js 统一路由适配器演示
92
+ </h1>
93
+
94
+ <div className="grid gap-6">
95
+ <div className="border rounded-lg p-6">
96
+ <h2 className="text-xl font-semibold mb-4">API 测试</h2>
97
+ <div className="grid gap-4">
98
+ <div className="flex gap-2">
99
+ <button
100
+ onClick={() =>
101
+ testAPI("/assistants/search", "POST", {
102
+ limit: 10,
103
+ })
104
+ }
105
+ disabled={loading}
106
+ className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 disabled:opacity-50">
107
+ 搜索助手
108
+ </button>
109
+ <button
110
+ onClick={() =>
111
+ testAPI("/threads", "POST", {})
112
+ }
113
+ disabled={loading}
114
+ className="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600 disabled:opacity-50">
115
+ 创建线程
116
+ </button>
117
+ <button
118
+ onClick={() =>
119
+ testAPI("/threads/search", "POST", {
120
+ limit: 5,
121
+ })
122
+ }
123
+ disabled={loading}
124
+ className="px-4 py-2 bg-purple-500 text-white rounded hover:bg-purple-600 disabled:opacity-50">
125
+ 搜索线程
126
+ </button>
127
+ </div>
128
+ <button
129
+ onClick={testStream}
130
+ disabled={loading}
131
+ className="px-4 py-2 bg-orange-500 text-white rounded hover:bg-orange-600 disabled:opacity-50">
132
+ 测试流式接口 (SSE)
133
+ </button>
134
+ </div>
135
+ </div>
136
+
137
+ <div className="border rounded-lg p-6">
138
+ <h2 className="text-xl font-semibold mb-4">API 响应</h2>
139
+ <pre className="bg-gray-100 dark:bg-gray-800 p-4 rounded text-sm overflow-auto max-h-96 whitespace-pre-wrap">
140
+ {loading
141
+ ? "加载中..."
142
+ : result || "点击上方按钮测试 API"}
143
+ </pre>
144
+ </div>
145
+
146
+ <div className="border rounded-lg p-6">
147
+ <h2 className="text-xl font-semibold mb-4">
148
+ 统一路由 API 端点 (所有请求都通过 /api/[...path])
149
+ </h2>
150
+ <div className="grid gap-2 text-sm">
151
+ <div>
152
+ <code className="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
153
+ POST /api/assistants/search
154
+ </code>{" "}
155
+ - 搜索助手
156
+ </div>
157
+ <div>
158
+ <code className="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
159
+ GET /api/assistants/[id]/graph
160
+ </code>{" "}
161
+ - 获取助手图谱
162
+ </div>
163
+ <div>
164
+ <code className="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
165
+ POST /api/threads
166
+ </code>{" "}
167
+ - 创建线程
168
+ </div>
169
+ <div>
170
+ <code className="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
171
+ POST /api/threads/search
172
+ </code>{" "}
173
+ - 搜索线程
174
+ </div>
175
+ <div>
176
+ <code className="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
177
+ GET /api/threads/[id]
178
+ </code>{" "}
179
+ - 获取线程
180
+ </div>
181
+ <div>
182
+ <code className="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
183
+ DELETE /api/threads/[id]
184
+ </code>{" "}
185
+ - 删除线程
186
+ </div>
187
+ <div>
188
+ <code className="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
189
+ GET /api/threads/[id]/runs
190
+ </code>{" "}
191
+ - 获取运行列表
192
+ </div>
193
+ <div>
194
+ <code className="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
195
+ POST /api/threads/[id]/runs/stream
196
+ </code>{" "}
197
+ - 流式运行 (SSE)
198
+ </div>
199
+ <div>
200
+ <code className="bg-gray-100 dark:bg-gray-800 px-2 py-1 rounded">
201
+ POST /api/threads/[id]/runs/[run_id]/cancel
202
+ </code>{" "}
203
+ - 取消运行
204
+ </div>
205
+ </div>
206
+ </div>
207
+ </div>
208
+ </main>
209
+ </div>
210
+ );
211
+ }
@@ -0,0 +1,26 @@
1
+ import type { NextConfig } from "next";
2
+
3
+ const nextConfig: NextConfig = {
4
+ async headers() {
5
+ return [
6
+ {
7
+ // matching all API routes
8
+ source: "/api/:path*",
9
+ headers: [
10
+ { key: "Access-Control-Allow-Credentials", value: "true" },
11
+ { key: "Access-Control-Allow-Origin", value: "*" }, // replace this your actual origin
12
+ {
13
+ key: "Access-Control-Allow-Methods",
14
+ value: "GET,DELETE,PATCH,POST,PUT",
15
+ },
16
+ {
17
+ key: "Access-Control-Allow-Headers",
18
+ value: "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, Authorization",
19
+ },
20
+ ],
21
+ },
22
+ ];
23
+ },
24
+ };
25
+
26
+ export default nextConfig;