@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,168 @@
|
|
|
1
|
+
/** @ts-ignore */
|
|
2
|
+
import { NextResponse } from 'next/server';
|
|
3
|
+
import { client } from './endpoint';
|
|
4
|
+
import { AssistantsSearchSchema, AssistantGraphQuerySchema, RunStreamPayloadSchema, RunListQuerySchema, RunCancelQuerySchema, ThreadCreatePayloadSchema, ThreadSearchPayloadSchema, } from '../zod';
|
|
5
|
+
import { serialiseAsDict } from '../../graph/stream';
|
|
6
|
+
// Next.js App Router 的 SSE 响应实现
|
|
7
|
+
async function sseResponse(generator) {
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
const stream = new ReadableStream({
|
|
10
|
+
async start(controller) {
|
|
11
|
+
try {
|
|
12
|
+
for await (const { event, data } of generator) {
|
|
13
|
+
const line = `event: ${event}\n` + `data: ${serialiseAsDict(data, 0)}\n\n`;
|
|
14
|
+
controller.enqueue(encoder.encode(line));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
// ignore
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
controller.close();
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
return new Response(stream, {
|
|
26
|
+
headers: {
|
|
27
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
28
|
+
'Cache-Control': 'no-cache, no-transform',
|
|
29
|
+
Connection: 'keep-alive',
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
// 统一路由处理器
|
|
34
|
+
export async function GET(req) {
|
|
35
|
+
const url = new URL(req.url);
|
|
36
|
+
const pathname = url.pathname;
|
|
37
|
+
// Assistants routes
|
|
38
|
+
if (pathname.match(/\/assistants\/[^/]+\/graph$/)) {
|
|
39
|
+
const match = pathname.match(/\/assistants\/([^/]+)\/graph$/);
|
|
40
|
+
if (match) {
|
|
41
|
+
const assistant_id = match[1];
|
|
42
|
+
const xrayParam = url.searchParams.get('xray');
|
|
43
|
+
const queryParams = { xray: xrayParam };
|
|
44
|
+
const { xray } = AssistantGraphQuerySchema.parse(queryParams);
|
|
45
|
+
const data = await client.assistants.getGraph(assistant_id, {
|
|
46
|
+
xray: xray !== undefined ? xray === 'true' : undefined,
|
|
47
|
+
});
|
|
48
|
+
return NextResponse.json(data);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Threads routes
|
|
52
|
+
if (pathname.match(/\/threads\/[0-9a-fA-F-]{36}$/)) {
|
|
53
|
+
const match = pathname.match(/\/threads\/([0-9a-fA-F-]{36})$/);
|
|
54
|
+
if (match) {
|
|
55
|
+
const thread_id = match[1];
|
|
56
|
+
const data = await client.threads.get(thread_id);
|
|
57
|
+
return NextResponse.json(data);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
// Runs routes
|
|
61
|
+
if (pathname.match(/\/threads\/[0-9a-fA-F-]{36}\/runs$/)) {
|
|
62
|
+
const match = pathname.match(/\/threads\/([0-9a-fA-F-]{36})\/runs$/);
|
|
63
|
+
if (match) {
|
|
64
|
+
const thread_id = match[1];
|
|
65
|
+
const limit = url.searchParams.get('limit');
|
|
66
|
+
const offset = url.searchParams.get('offset');
|
|
67
|
+
const status = url.searchParams.get('status');
|
|
68
|
+
const queryParams = { limit, offset, status };
|
|
69
|
+
const { limit: parsedLimit, offset: parsedOffset, status: parsedStatus, } = RunListQuerySchema.parse(queryParams);
|
|
70
|
+
const runs = await client.runs.list(thread_id, {
|
|
71
|
+
limit: parsedLimit,
|
|
72
|
+
offset: parsedOffset,
|
|
73
|
+
status: parsedStatus,
|
|
74
|
+
});
|
|
75
|
+
return Response.json(runs);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return new NextResponse('Not Found', { status: 404 });
|
|
79
|
+
}
|
|
80
|
+
export async function POST(req) {
|
|
81
|
+
const url = new URL(req.url);
|
|
82
|
+
const pathname = url.pathname;
|
|
83
|
+
// Assistants routes
|
|
84
|
+
if (pathname.endsWith('/assistants/search')) {
|
|
85
|
+
const body = await req.json();
|
|
86
|
+
const payload = AssistantsSearchSchema.parse(body);
|
|
87
|
+
const data = await client.assistants.search({
|
|
88
|
+
graphId: payload.graph_id,
|
|
89
|
+
metadata: payload.metadata,
|
|
90
|
+
limit: payload.limit,
|
|
91
|
+
offset: payload.offset,
|
|
92
|
+
});
|
|
93
|
+
return NextResponse.json(data, {
|
|
94
|
+
headers: { 'X-Pagination-Total': String(data.length) },
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
// Threads routes
|
|
98
|
+
if (pathname.endsWith('/threads')) {
|
|
99
|
+
const body = await req.json();
|
|
100
|
+
const payload = ThreadCreatePayloadSchema.parse(body);
|
|
101
|
+
const thread = await client.threads.create({
|
|
102
|
+
thread_id: payload.thread_id,
|
|
103
|
+
metadata: payload.metadata,
|
|
104
|
+
if_exists: payload.if_exists ?? undefined,
|
|
105
|
+
});
|
|
106
|
+
return NextResponse.json(thread);
|
|
107
|
+
}
|
|
108
|
+
if (pathname.endsWith('/threads/search')) {
|
|
109
|
+
const body = await req.json();
|
|
110
|
+
const payload = ThreadSearchPayloadSchema.parse(body);
|
|
111
|
+
const result = await client.threads.search({
|
|
112
|
+
metadata: payload.metadata,
|
|
113
|
+
status: payload.status,
|
|
114
|
+
limit: payload.limit,
|
|
115
|
+
offset: payload.offset,
|
|
116
|
+
sortBy: payload.sort_by ?? undefined,
|
|
117
|
+
sortOrder: payload.sort_order ?? undefined,
|
|
118
|
+
});
|
|
119
|
+
return NextResponse.json(result, {
|
|
120
|
+
headers: { 'X-Pagination-Total': String(result.length) },
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
// Runs routes - stream
|
|
124
|
+
if (pathname.match(/\/threads\/[0-9a-fA-F-]{36}\/runs\/stream$/)) {
|
|
125
|
+
const match = pathname.match(/\/threads\/([0-9a-fA-F-]{36})\/runs\/stream$/);
|
|
126
|
+
if (match) {
|
|
127
|
+
const thread_id = match[1];
|
|
128
|
+
const body = await req.json();
|
|
129
|
+
const payload = RunStreamPayloadSchema.parse(body);
|
|
130
|
+
const generator = client.runs.stream(thread_id, payload.assistant_id, payload);
|
|
131
|
+
return sseResponse(generator);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Runs routes - cancel
|
|
135
|
+
if (pathname.match(/\/threads\/[0-9a-fA-F-]{36}\/runs\/[0-9a-fA-F-]{36}\/cancel$/)) {
|
|
136
|
+
const match = pathname.match(/\/threads\/([0-9a-fA-F-]{36})\/runs\/([0-9a-fA-F-]{36})\/cancel$/);
|
|
137
|
+
if (match) {
|
|
138
|
+
const thread_id = match[1];
|
|
139
|
+
const run_id = match[2];
|
|
140
|
+
const waitParam = url.searchParams.get('wait');
|
|
141
|
+
const actionParam = url.searchParams.get('action');
|
|
142
|
+
const queryParams = {
|
|
143
|
+
wait: waitParam ? waitParam === 'true' : false,
|
|
144
|
+
action: actionParam ?? 'interrupt',
|
|
145
|
+
};
|
|
146
|
+
const { wait, action } = RunCancelQuerySchema.parse(queryParams);
|
|
147
|
+
const promise = client.runs.cancel(thread_id, run_id, wait, action);
|
|
148
|
+
if (wait)
|
|
149
|
+
await promise;
|
|
150
|
+
return new Response(null, { status: wait ? 204 : 202 });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return new NextResponse('Not Found', { status: 404 });
|
|
154
|
+
}
|
|
155
|
+
export async function DELETE(req) {
|
|
156
|
+
const url = new URL(req.url);
|
|
157
|
+
const pathname = url.pathname;
|
|
158
|
+
// Threads routes
|
|
159
|
+
if (pathname.match(/\/threads\/[0-9a-fA-F-]{36}$/)) {
|
|
160
|
+
const match = pathname.match(/\/threads\/([0-9a-fA-F-]{36})$/);
|
|
161
|
+
if (match) {
|
|
162
|
+
const thread_id = match[1];
|
|
163
|
+
await client.threads.delete(thread_id);
|
|
164
|
+
return new NextResponse(null, { status: 204 });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return new NextResponse('Not Found', { status: 404 });
|
|
168
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import z from
|
|
1
|
+
import z from "zod";
|
|
2
2
|
export declare const AssistantConfigurable: z.ZodObject<{
|
|
3
3
|
thread_id: z.ZodOptional<z.ZodString>;
|
|
4
4
|
thread_ts: z.ZodOptional<z.ZodString>;
|
|
@@ -98,11 +98,10 @@ export declare const Assistant: z.ZodObject<{
|
|
|
98
98
|
}, "strip", z.ZodTypeAny, {
|
|
99
99
|
created_at: string;
|
|
100
100
|
updated_at: string;
|
|
101
|
-
assistant_id: string;
|
|
102
|
-
graph_id: string;
|
|
103
101
|
metadata: {} & {
|
|
104
102
|
[k: string]: any;
|
|
105
103
|
};
|
|
104
|
+
assistant_id: string;
|
|
106
105
|
config: {
|
|
107
106
|
configurable?: z.objectOutputType<{
|
|
108
107
|
thread_id: z.ZodOptional<z.ZodString>;
|
|
@@ -113,14 +112,14 @@ export declare const Assistant: z.ZodObject<{
|
|
|
113
112
|
} & {
|
|
114
113
|
[k: string]: unknown;
|
|
115
114
|
};
|
|
115
|
+
graph_id: string;
|
|
116
116
|
}, {
|
|
117
117
|
created_at: string;
|
|
118
118
|
updated_at: string;
|
|
119
|
-
assistant_id: string;
|
|
120
|
-
graph_id: string;
|
|
121
119
|
metadata: {} & {
|
|
122
120
|
[k: string]: any;
|
|
123
121
|
};
|
|
122
|
+
assistant_id: string;
|
|
124
123
|
config: {
|
|
125
124
|
configurable?: z.objectInputType<{
|
|
126
125
|
thread_id: z.ZodOptional<z.ZodString>;
|
|
@@ -131,6 +130,7 @@ export declare const Assistant: z.ZodObject<{
|
|
|
131
130
|
} & {
|
|
132
131
|
[k: string]: unknown;
|
|
133
132
|
};
|
|
133
|
+
graph_id: string;
|
|
134
134
|
}>;
|
|
135
135
|
export declare const MetadataSchema: z.ZodObject<{
|
|
136
136
|
source: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"input">, z.ZodLiteral<"loop">, z.ZodLiteral<"update">, z.ZodString]>>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import z from
|
|
1
|
+
import z from "zod";
|
|
2
2
|
export const AssistantConfigurable = z
|
|
3
3
|
.object({
|
|
4
4
|
thread_id: z.string().optional(),
|
|
@@ -12,7 +12,7 @@ export const AssistantConfig = z
|
|
|
12
12
|
configurable: AssistantConfigurable.optional(),
|
|
13
13
|
})
|
|
14
14
|
.catchall(z.unknown())
|
|
15
|
-
.describe(
|
|
15
|
+
.describe("The configuration of an assistant.");
|
|
16
16
|
export const Assistant = z.object({
|
|
17
17
|
assistant_id: z.string().uuid(),
|
|
18
18
|
graph_id: z.string(),
|
|
@@ -23,7 +23,14 @@ export const Assistant = z.object({
|
|
|
23
23
|
});
|
|
24
24
|
export const MetadataSchema = z
|
|
25
25
|
.object({
|
|
26
|
-
source: z
|
|
26
|
+
source: z
|
|
27
|
+
.union([
|
|
28
|
+
z.literal("input"),
|
|
29
|
+
z.literal("loop"),
|
|
30
|
+
z.literal("update"),
|
|
31
|
+
z.string(),
|
|
32
|
+
])
|
|
33
|
+
.optional(),
|
|
27
34
|
step: z.number().optional(),
|
|
28
35
|
writes: z.record(z.unknown()).nullable().optional(),
|
|
29
36
|
parents: z.record(z.string()).optional(),
|
|
@@ -35,9 +42,19 @@ export const SendSchema = z.object({
|
|
|
35
42
|
});
|
|
36
43
|
export const CommandSchema = z.object({
|
|
37
44
|
update: z
|
|
38
|
-
.union([
|
|
45
|
+
.union([
|
|
46
|
+
z.record(z.unknown()),
|
|
47
|
+
z.array(z.tuple([z.string(), z.unknown()])),
|
|
48
|
+
])
|
|
39
49
|
.nullable()
|
|
40
50
|
.optional(),
|
|
41
51
|
resume: z.unknown().optional(),
|
|
42
|
-
goto: z
|
|
52
|
+
goto: z
|
|
53
|
+
.union([
|
|
54
|
+
SendSchema,
|
|
55
|
+
z.array(SendSchema),
|
|
56
|
+
z.string(),
|
|
57
|
+
z.array(z.string()),
|
|
58
|
+
])
|
|
59
|
+
.optional(),
|
|
43
60
|
});
|