@langgraph-js/pure-graph 1.0.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/README.md +16 -0
- package/dist/adapter/hono/assistants.d.ts +3 -0
- package/dist/adapter/hono/assistants.js +27 -0
- package/dist/adapter/hono/endpoint.d.ts +1 -0
- package/dist/adapter/hono/endpoint.js +3 -0
- package/dist/adapter/hono/index.d.ts +3 -0
- package/dist/adapter/hono/index.js +11 -0
- package/dist/adapter/hono/runs.d.ts +3 -0
- package/dist/adapter/hono/runs.js +71 -0
- package/dist/adapter/hono/threads.d.ts +3 -0
- package/dist/adapter/hono/threads.js +71 -0
- package/dist/adapter/hono/zod.d.ts +203 -0
- package/dist/adapter/hono/zod.js +43 -0
- package/dist/createEndpoint.d.ts +5 -0
- package/dist/createEndpoint.js +77 -0
- package/dist/global.d.ts +4 -0
- package/dist/global.js +5 -0
- package/dist/graph/stream.d.ts +39 -0
- package/dist/graph/stream.js +187 -0
- package/dist/graph/stringify.d.ts +1 -0
- package/dist/graph/stringify.js +214 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/queue/JsonPlusSerializer.d.ts +7 -0
- package/dist/queue/JsonPlusSerializer.js +138 -0
- package/dist/queue/event_message.d.ts +15 -0
- package/dist/queue/event_message.js +27 -0
- package/dist/queue/stream_queue.d.ts +161 -0
- package/dist/queue/stream_queue.js +175 -0
- package/dist/storage/index.d.ts +5 -0
- package/dist/storage/index.js +11 -0
- package/dist/storage/memory/checkpoint.d.ts +2 -0
- package/dist/storage/memory/checkpoint.js +2 -0
- package/dist/storage/memory/queue.d.ts +17 -0
- package/dist/storage/memory/queue.js +72 -0
- package/dist/storage/memory/threads.d.ts +39 -0
- package/dist/storage/memory/threads.js +115 -0
- package/dist/threads/index.d.ts +36 -0
- package/dist/threads/index.js +26 -0
- package/dist/types.d.ts +94 -0
- package/dist/types.js +1 -0
- package/dist/utils/getGraph.d.ts +10 -0
- package/dist/utils/getGraph.js +18 -0
- package/dist/utils/getLangGraphCommand.d.ts +9 -0
- package/dist/utils/getLangGraphCommand.js +13 -0
- package/package.json +39 -0
- package/src/adapter/hono/assistants.ts +41 -0
- package/src/adapter/hono/endpoint.ts +4 -0
- package/src/adapter/hono/index.ts +14 -0
- package/src/adapter/hono/runs.ts +102 -0
- package/src/adapter/hono/threads.ts +92 -0
- package/src/adapter/hono/zod.ts +49 -0
- package/src/createEndpoint.ts +106 -0
- package/src/global.ts +6 -0
- package/src/graph/stream.ts +253 -0
- package/src/graph/stringify.ts +219 -0
- package/src/index.ts +5 -0
- package/src/queue/JsonPlusSerializer.ts +143 -0
- package/src/queue/event_message.ts +30 -0
- package/src/queue/stream_queue.ts +236 -0
- package/src/storage/index.ts +14 -0
- package/src/storage/memory/checkpoint.ts +2 -0
- package/src/storage/memory/queue.ts +83 -0
- package/src/storage/memory/threads.ts +154 -0
- package/src/threads/index.ts +51 -0
- package/src/types.ts +116 -0
- package/src/utils/getGraph.ts +44 -0
- package/src/utils/getLangGraphCommand.ts +21 -0
- package/test/graph/index.ts +21 -0
- package/test/hono.ts +10 -0
- package/tsconfig.json +20 -0
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Pure Graph
|
|
2
|
+
|
|
3
|
+
Pure Graph is a project that implements a Standard LangGraph Endpoint to many frameworks like NextJS and NuxtJS.
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
import { registerGraph } from '../src/createEndpoint';
|
|
7
|
+
import { graph } from './graph/index';
|
|
8
|
+
import { Hono } from 'hono';
|
|
9
|
+
import LangGraphApp from '../src/adapter/hono/index';
|
|
10
|
+
registerGraph('test', graph);
|
|
11
|
+
|
|
12
|
+
const app = new Hono();
|
|
13
|
+
app.route('/', LangGraphApp);
|
|
14
|
+
|
|
15
|
+
export default app;
|
|
16
|
+
```
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { zValidator } from '@hono/zod-validator';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { client } from './endpoint';
|
|
5
|
+
import { MetadataSchema } from './zod';
|
|
6
|
+
const api = new Hono();
|
|
7
|
+
api.post('/assistants/search', zValidator('json', z.object({
|
|
8
|
+
graph_id: z.string().optional(),
|
|
9
|
+
metadata: MetadataSchema.optional(),
|
|
10
|
+
limit: z.number().int().optional(),
|
|
11
|
+
offset: z.number().int().optional(),
|
|
12
|
+
})), async (c) => {
|
|
13
|
+
// Search Assistants
|
|
14
|
+
const payload = c.req.valid('json');
|
|
15
|
+
let total = 0;
|
|
16
|
+
const data = await client.assistants.search(payload);
|
|
17
|
+
c.res.headers.set('X-Pagination-Total', total.toString());
|
|
18
|
+
return c.json(data);
|
|
19
|
+
});
|
|
20
|
+
api.get('/assistants/:assistant_id/graph', zValidator('query', z.object({ xray: z.string().optional() })), async (c) => {
|
|
21
|
+
const xray = c.req.valid('query').xray;
|
|
22
|
+
const data = await client.assistants.getGraph(c.req.param('assistant_id'), {
|
|
23
|
+
xray: xray !== undefined ? xray === 'true' : undefined,
|
|
24
|
+
});
|
|
25
|
+
return c.json(data);
|
|
26
|
+
});
|
|
27
|
+
export default api;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const client: import("../../types.js").ILangGraphClient<unknown>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import Assistants from './assistants';
|
|
3
|
+
import Runs from './runs';
|
|
4
|
+
import Threads from './threads';
|
|
5
|
+
import { cors } from 'hono/cors';
|
|
6
|
+
const app = new Hono();
|
|
7
|
+
app.use(cors());
|
|
8
|
+
app.route('/', Assistants);
|
|
9
|
+
app.route('/', Runs);
|
|
10
|
+
app.route('/', Threads);
|
|
11
|
+
export default app;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { zValidator } from '@hono/zod-validator';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
|
+
import { streamSSE } from 'hono/streaming';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { client } from './endpoint';
|
|
6
|
+
import { AssistantConfig, CommandSchema, MetadataSchema } from './zod';
|
|
7
|
+
import { serialiseAsDict } from '../../graph/stream';
|
|
8
|
+
const api = new Hono();
|
|
9
|
+
// 最常用的对话接口
|
|
10
|
+
api.post('/threads/:thread_id/runs/stream', zValidator('param', z.object({ thread_id: z.string().uuid() })), zValidator('json', z
|
|
11
|
+
.object({
|
|
12
|
+
assistant_id: z.union([z.string().uuid(), z.string()]),
|
|
13
|
+
checkpoint_id: z.string().optional(),
|
|
14
|
+
// checkpoint: CheckpointSchema.optional(),
|
|
15
|
+
input: z.any().optional(),
|
|
16
|
+
command: CommandSchema.optional(),
|
|
17
|
+
metadata: MetadataSchema.optional(),
|
|
18
|
+
config: AssistantConfig.optional(),
|
|
19
|
+
webhook: z.string().optional(),
|
|
20
|
+
interrupt_before: z.union([z.literal('*'), z.array(z.string())]).optional(),
|
|
21
|
+
interrupt_after: z.union([z.literal('*'), z.array(z.string())]).optional(),
|
|
22
|
+
on_disconnect: z.enum(['cancel', 'continue']).optional().default('continue'),
|
|
23
|
+
multitask_strategy: z.enum(['reject', 'rollback', 'interrupt', 'enqueue']).optional(),
|
|
24
|
+
stream_mode: z
|
|
25
|
+
.array(z.enum(['values', 'messages', 'messages-tuple', 'updates', 'events', 'debug', 'custom']))
|
|
26
|
+
.optional(),
|
|
27
|
+
stream_subgraphs: z.boolean().optional(),
|
|
28
|
+
stream_resumable: z.boolean().optional(),
|
|
29
|
+
after_seconds: z.number().optional(),
|
|
30
|
+
if_not_exists: z.enum(['create', 'reject']).optional(),
|
|
31
|
+
on_completion: z.enum(['complete', 'continue']).optional(),
|
|
32
|
+
feedback_keys: z.array(z.string()).optional(),
|
|
33
|
+
langsmith_tracer: z.unknown().optional(),
|
|
34
|
+
})
|
|
35
|
+
.describe('Payload for creating a stateful run.')), async (c) => {
|
|
36
|
+
// Stream Run
|
|
37
|
+
const { thread_id } = c.req.valid('param');
|
|
38
|
+
const payload = c.req.valid('json');
|
|
39
|
+
// c.header('Content-Location', `/threads/${thread_id}/runs/${run.run_id}`);
|
|
40
|
+
return streamSSE(c, async (stream) => {
|
|
41
|
+
/** @ts-ignore zod v3 的问题,与 ts 类型不一致 */
|
|
42
|
+
for await (const { event, data } of client.runs.stream(thread_id, payload.assistant_id, payload)) {
|
|
43
|
+
await stream.writeSSE({ data: serialiseAsDict(data), event });
|
|
44
|
+
}
|
|
45
|
+
// await stream.sleep(500); // 不知为何要等
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
api.get('/threads/:thread_id/runs', zValidator('param', z.object({ thread_id: z.string().uuid() })), zValidator('query', z.object({
|
|
49
|
+
limit: z.string().optional(),
|
|
50
|
+
offset: z.string().optional(),
|
|
51
|
+
status: z.enum(['pending', 'running', 'error', 'success', 'timeout', 'interrupted']).optional(),
|
|
52
|
+
})), async (c) => {
|
|
53
|
+
const { thread_id } = c.req.valid('param');
|
|
54
|
+
const { limit, offset, status } = c.req.valid('query');
|
|
55
|
+
const runs = await client.runs.list(thread_id, { limit: Number(limit), offset: Number(offset), status });
|
|
56
|
+
return c.json(runs);
|
|
57
|
+
});
|
|
58
|
+
api.post('/threads/:thread_id/runs/:run_id/cancel', zValidator('param', z.object({ thread_id: z.string().uuid(), run_id: z.string().uuid() })), zValidator('query', z.object({
|
|
59
|
+
wait: z.coerce.boolean().optional().default(false),
|
|
60
|
+
action: z.enum(['interrupt', 'rollback']).optional().default('interrupt'),
|
|
61
|
+
})), async (c) => {
|
|
62
|
+
// Cancel Run Http
|
|
63
|
+
const { thread_id, run_id } = c.req.valid('param');
|
|
64
|
+
const { wait, action } = c.req.valid('query');
|
|
65
|
+
const cancel = client.runs.cancel(thread_id, run_id, wait, action);
|
|
66
|
+
if (wait) {
|
|
67
|
+
await cancel;
|
|
68
|
+
}
|
|
69
|
+
return c.body(null, wait ? 204 : 202);
|
|
70
|
+
});
|
|
71
|
+
export default api;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { zValidator } from '@hono/zod-validator';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { client } from './endpoint';
|
|
5
|
+
import { MetadataSchema } from './zod';
|
|
6
|
+
const api = new Hono();
|
|
7
|
+
// Threads Routes
|
|
8
|
+
api.post('/threads', zValidator('json', z
|
|
9
|
+
.object({
|
|
10
|
+
// supersteps: z
|
|
11
|
+
// .array(
|
|
12
|
+
// z.object({
|
|
13
|
+
// updates: z.array(
|
|
14
|
+
// z.object({
|
|
15
|
+
// values: z.unknown(),
|
|
16
|
+
// command: CommandSchema.optional(),
|
|
17
|
+
// as_node: z.string(),
|
|
18
|
+
// }),
|
|
19
|
+
// ),
|
|
20
|
+
// }),
|
|
21
|
+
// )
|
|
22
|
+
// .describe('The supersteps to apply to the thread.')
|
|
23
|
+
// .optional(),
|
|
24
|
+
thread_id: z
|
|
25
|
+
.string()
|
|
26
|
+
.uuid()
|
|
27
|
+
.describe('The ID of the thread. If not provided, an ID is generated.')
|
|
28
|
+
.optional(),
|
|
29
|
+
metadata: MetadataSchema.optional(),
|
|
30
|
+
if_exists: z.union([z.literal('raise'), z.literal('do_nothing')]).optional(),
|
|
31
|
+
})
|
|
32
|
+
.describe('Payload for creating a thread.')), async (c) => {
|
|
33
|
+
const payload = c.req.valid('json');
|
|
34
|
+
const thread = await client.threads.create(payload);
|
|
35
|
+
return c.json(thread);
|
|
36
|
+
});
|
|
37
|
+
api.post('/threads/search', zValidator('json', z
|
|
38
|
+
.object({
|
|
39
|
+
metadata: z.record(z.unknown()).describe('Metadata to search for.').optional(),
|
|
40
|
+
status: z
|
|
41
|
+
.enum(['idle', 'busy', 'interrupted', 'error'])
|
|
42
|
+
.describe('Filter by thread status.')
|
|
43
|
+
.optional(),
|
|
44
|
+
values: z.record(z.unknown()).describe('Filter by thread values.').optional(),
|
|
45
|
+
limit: z.number().int().gte(1).lte(1000).describe('Maximum number to return.').optional(),
|
|
46
|
+
offset: z.number().int().gte(0).describe('Offset to start from.').optional(),
|
|
47
|
+
sort_by: z
|
|
48
|
+
.enum(['thread_id', 'status', 'created_at', 'updated_at'])
|
|
49
|
+
.describe('Sort by field.')
|
|
50
|
+
.optional(),
|
|
51
|
+
sort_order: z.enum(['asc', 'desc']).describe('Sort order.').optional(),
|
|
52
|
+
})
|
|
53
|
+
.describe('Payload for listing threads.')), async (c) => {
|
|
54
|
+
// Search Threads
|
|
55
|
+
const payload = c.req.valid('json');
|
|
56
|
+
const result = await client.threads.search(payload);
|
|
57
|
+
c.res.headers.set('X-Pagination-Total', result.length.toString());
|
|
58
|
+
return c.json(result);
|
|
59
|
+
});
|
|
60
|
+
api.get('/threads/:thread_id', zValidator('param', z.object({ thread_id: z.string().uuid() })), async (c) => {
|
|
61
|
+
// Get Thread
|
|
62
|
+
const { thread_id } = c.req.valid('param');
|
|
63
|
+
return c.json(await client.threads.get(thread_id));
|
|
64
|
+
});
|
|
65
|
+
api.delete('/threads/:thread_id', zValidator('param', z.object({ thread_id: z.string().uuid() })), async (c) => {
|
|
66
|
+
// Delete Thread
|
|
67
|
+
const { thread_id } = c.req.valid('param');
|
|
68
|
+
await client.threads.delete(thread_id);
|
|
69
|
+
return new Response(null, { status: 204 });
|
|
70
|
+
});
|
|
71
|
+
export default api;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
export declare const AssistantConfigurable: z.ZodObject<{
|
|
3
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
4
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
5
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
6
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
7
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
8
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
9
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
10
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
11
|
+
}, z.ZodUnknown, "strip">>;
|
|
12
|
+
export declare const AssistantConfig: z.ZodObject<{
|
|
13
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
14
|
+
recursion_limit: z.ZodOptional<z.ZodNumber>;
|
|
15
|
+
configurable: z.ZodOptional<z.ZodObject<{
|
|
16
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
17
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
18
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
19
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
20
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
21
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
22
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
23
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
24
|
+
}, z.ZodUnknown, "strip">>>;
|
|
25
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
26
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
27
|
+
recursion_limit: z.ZodOptional<z.ZodNumber>;
|
|
28
|
+
configurable: z.ZodOptional<z.ZodObject<{
|
|
29
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
30
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
31
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
32
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
33
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
34
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
35
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
36
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
37
|
+
}, z.ZodUnknown, "strip">>>;
|
|
38
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
39
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
40
|
+
recursion_limit: z.ZodOptional<z.ZodNumber>;
|
|
41
|
+
configurable: z.ZodOptional<z.ZodObject<{
|
|
42
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
43
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
44
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
45
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
46
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
47
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
48
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
49
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
50
|
+
}, z.ZodUnknown, "strip">>>;
|
|
51
|
+
}, z.ZodUnknown, "strip">>;
|
|
52
|
+
export declare const Assistant: z.ZodObject<{
|
|
53
|
+
assistant_id: z.ZodString;
|
|
54
|
+
graph_id: z.ZodString;
|
|
55
|
+
config: z.ZodObject<{
|
|
56
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
57
|
+
recursion_limit: z.ZodOptional<z.ZodNumber>;
|
|
58
|
+
configurable: z.ZodOptional<z.ZodObject<{
|
|
59
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
60
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
61
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
62
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
63
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
64
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
65
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
66
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
67
|
+
}, z.ZodUnknown, "strip">>>;
|
|
68
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
69
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
70
|
+
recursion_limit: z.ZodOptional<z.ZodNumber>;
|
|
71
|
+
configurable: z.ZodOptional<z.ZodObject<{
|
|
72
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
73
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
74
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
75
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
76
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
77
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
78
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
79
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
80
|
+
}, z.ZodUnknown, "strip">>>;
|
|
81
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
82
|
+
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
83
|
+
recursion_limit: z.ZodOptional<z.ZodNumber>;
|
|
84
|
+
configurable: z.ZodOptional<z.ZodObject<{
|
|
85
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
86
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
87
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
88
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
89
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
90
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
91
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
92
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
93
|
+
}, z.ZodUnknown, "strip">>>;
|
|
94
|
+
}, z.ZodUnknown, "strip">>;
|
|
95
|
+
created_at: z.ZodString;
|
|
96
|
+
updated_at: z.ZodString;
|
|
97
|
+
metadata: z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>;
|
|
98
|
+
}, "strip", z.ZodTypeAny, {
|
|
99
|
+
created_at: string;
|
|
100
|
+
updated_at: string;
|
|
101
|
+
assistant_id: string;
|
|
102
|
+
graph_id: string;
|
|
103
|
+
metadata: {} & {
|
|
104
|
+
[k: string]: any;
|
|
105
|
+
};
|
|
106
|
+
config: {
|
|
107
|
+
configurable?: z.objectOutputType<{
|
|
108
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
109
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
110
|
+
}, z.ZodUnknown, "strip"> | undefined;
|
|
111
|
+
tags?: string[] | undefined;
|
|
112
|
+
recursion_limit?: number | undefined;
|
|
113
|
+
} & {
|
|
114
|
+
[k: string]: unknown;
|
|
115
|
+
};
|
|
116
|
+
}, {
|
|
117
|
+
created_at: string;
|
|
118
|
+
updated_at: string;
|
|
119
|
+
assistant_id: string;
|
|
120
|
+
graph_id: string;
|
|
121
|
+
metadata: {} & {
|
|
122
|
+
[k: string]: any;
|
|
123
|
+
};
|
|
124
|
+
config: {
|
|
125
|
+
configurable?: z.objectInputType<{
|
|
126
|
+
thread_id: z.ZodOptional<z.ZodString>;
|
|
127
|
+
thread_ts: z.ZodOptional<z.ZodString>;
|
|
128
|
+
}, z.ZodUnknown, "strip"> | undefined;
|
|
129
|
+
tags?: string[] | undefined;
|
|
130
|
+
recursion_limit?: number | undefined;
|
|
131
|
+
} & {
|
|
132
|
+
[k: string]: unknown;
|
|
133
|
+
};
|
|
134
|
+
}>;
|
|
135
|
+
export declare const MetadataSchema: z.ZodObject<{
|
|
136
|
+
source: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"input">, z.ZodLiteral<"loop">, z.ZodLiteral<"update">, z.ZodString]>>;
|
|
137
|
+
step: z.ZodOptional<z.ZodNumber>;
|
|
138
|
+
writes: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
139
|
+
parents: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
140
|
+
}, "strip", z.ZodUnknown, z.objectOutputType<{
|
|
141
|
+
source: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"input">, z.ZodLiteral<"loop">, z.ZodLiteral<"update">, z.ZodString]>>;
|
|
142
|
+
step: z.ZodOptional<z.ZodNumber>;
|
|
143
|
+
writes: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
144
|
+
parents: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
145
|
+
}, z.ZodUnknown, "strip">, z.objectInputType<{
|
|
146
|
+
source: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"input">, z.ZodLiteral<"loop">, z.ZodLiteral<"update">, z.ZodString]>>;
|
|
147
|
+
step: z.ZodOptional<z.ZodNumber>;
|
|
148
|
+
writes: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
149
|
+
parents: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
150
|
+
}, z.ZodUnknown, "strip">>;
|
|
151
|
+
export declare const SendSchema: z.ZodObject<{
|
|
152
|
+
node: z.ZodString;
|
|
153
|
+
input: z.ZodNullable<z.ZodUnknown>;
|
|
154
|
+
}, "strip", z.ZodTypeAny, {
|
|
155
|
+
node: string;
|
|
156
|
+
input?: unknown;
|
|
157
|
+
}, {
|
|
158
|
+
node: string;
|
|
159
|
+
input?: unknown;
|
|
160
|
+
}>;
|
|
161
|
+
export declare const CommandSchema: z.ZodObject<{
|
|
162
|
+
update: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodRecord<z.ZodString, z.ZodUnknown>, z.ZodArray<z.ZodTuple<[z.ZodString, z.ZodUnknown], null>, "many">]>>>;
|
|
163
|
+
resume: z.ZodOptional<z.ZodUnknown>;
|
|
164
|
+
goto: z.ZodOptional<z.ZodUnion<[z.ZodObject<{
|
|
165
|
+
node: z.ZodString;
|
|
166
|
+
input: z.ZodNullable<z.ZodUnknown>;
|
|
167
|
+
}, "strip", z.ZodTypeAny, {
|
|
168
|
+
node: string;
|
|
169
|
+
input?: unknown;
|
|
170
|
+
}, {
|
|
171
|
+
node: string;
|
|
172
|
+
input?: unknown;
|
|
173
|
+
}>, z.ZodArray<z.ZodObject<{
|
|
174
|
+
node: z.ZodString;
|
|
175
|
+
input: z.ZodNullable<z.ZodUnknown>;
|
|
176
|
+
}, "strip", z.ZodTypeAny, {
|
|
177
|
+
node: string;
|
|
178
|
+
input?: unknown;
|
|
179
|
+
}, {
|
|
180
|
+
node: string;
|
|
181
|
+
input?: unknown;
|
|
182
|
+
}>, "many">, z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
|
|
183
|
+
}, "strip", z.ZodTypeAny, {
|
|
184
|
+
update?: Record<string, unknown> | [string, unknown][] | null | undefined;
|
|
185
|
+
resume?: unknown;
|
|
186
|
+
goto?: string | string[] | {
|
|
187
|
+
node: string;
|
|
188
|
+
input?: unknown;
|
|
189
|
+
} | {
|
|
190
|
+
node: string;
|
|
191
|
+
input?: unknown;
|
|
192
|
+
}[] | undefined;
|
|
193
|
+
}, {
|
|
194
|
+
update?: Record<string, unknown> | [string, unknown][] | null | undefined;
|
|
195
|
+
resume?: unknown;
|
|
196
|
+
goto?: string | string[] | {
|
|
197
|
+
node: string;
|
|
198
|
+
input?: unknown;
|
|
199
|
+
} | {
|
|
200
|
+
node: string;
|
|
201
|
+
input?: unknown;
|
|
202
|
+
}[] | undefined;
|
|
203
|
+
}>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
export const AssistantConfigurable = z
|
|
3
|
+
.object({
|
|
4
|
+
thread_id: z.string().optional(),
|
|
5
|
+
thread_ts: z.string().optional(),
|
|
6
|
+
})
|
|
7
|
+
.catchall(z.unknown());
|
|
8
|
+
export const AssistantConfig = z
|
|
9
|
+
.object({
|
|
10
|
+
tags: z.array(z.string()).optional(),
|
|
11
|
+
recursion_limit: z.number().int().optional(),
|
|
12
|
+
configurable: AssistantConfigurable.optional(),
|
|
13
|
+
})
|
|
14
|
+
.catchall(z.unknown())
|
|
15
|
+
.describe('The configuration of an assistant.');
|
|
16
|
+
export const Assistant = z.object({
|
|
17
|
+
assistant_id: z.string().uuid(),
|
|
18
|
+
graph_id: z.string(),
|
|
19
|
+
config: AssistantConfig,
|
|
20
|
+
created_at: z.string(),
|
|
21
|
+
updated_at: z.string(),
|
|
22
|
+
metadata: z.object({}).catchall(z.any()),
|
|
23
|
+
});
|
|
24
|
+
export const MetadataSchema = z
|
|
25
|
+
.object({
|
|
26
|
+
source: z.union([z.literal('input'), z.literal('loop'), z.literal('update'), z.string()]).optional(),
|
|
27
|
+
step: z.number().optional(),
|
|
28
|
+
writes: z.record(z.unknown()).nullable().optional(),
|
|
29
|
+
parents: z.record(z.string()).optional(),
|
|
30
|
+
})
|
|
31
|
+
.catchall(z.unknown());
|
|
32
|
+
export const SendSchema = z.object({
|
|
33
|
+
node: z.string(),
|
|
34
|
+
input: z.unknown().nullable(),
|
|
35
|
+
});
|
|
36
|
+
export const CommandSchema = z.object({
|
|
37
|
+
update: z
|
|
38
|
+
.union([z.record(z.unknown()), z.array(z.tuple([z.string(), z.unknown()]))])
|
|
39
|
+
.nullable()
|
|
40
|
+
.optional(),
|
|
41
|
+
resume: z.unknown().optional(),
|
|
42
|
+
goto: z.union([SendSchema, z.array(SendSchema), z.string(), z.array(z.string())]).optional(),
|
|
43
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { BaseThreadsManager } from './threads/index.js';
|
|
2
|
+
import { ILangGraphClient } from './types.js';
|
|
3
|
+
export { registerGraph } from './utils/getGraph.js';
|
|
4
|
+
export declare const AssistantEndpoint: ILangGraphClient['assistants'];
|
|
5
|
+
export declare const createEndpoint: (threads: BaseThreadsManager) => ILangGraphClient;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { streamState } from './graph/stream.js';
|
|
2
|
+
import { getGraph, GRAPHS } from './utils/getGraph.js';
|
|
3
|
+
import { globalMessageQueue } from './global.js';
|
|
4
|
+
export { registerGraph } from './utils/getGraph.js';
|
|
5
|
+
export const AssistantEndpoint = {
|
|
6
|
+
async search(query) {
|
|
7
|
+
if (query?.graphId) {
|
|
8
|
+
return [
|
|
9
|
+
{
|
|
10
|
+
assistant_id: query.graphId,
|
|
11
|
+
graph_id: query.graphId,
|
|
12
|
+
config: {},
|
|
13
|
+
created_at: new Date().toISOString(),
|
|
14
|
+
updated_at: new Date().toISOString(),
|
|
15
|
+
metadata: {},
|
|
16
|
+
version: 1,
|
|
17
|
+
name: query.graphId,
|
|
18
|
+
description: '',
|
|
19
|
+
},
|
|
20
|
+
];
|
|
21
|
+
}
|
|
22
|
+
return Object.entries(GRAPHS).map(([graphId, _]) => ({
|
|
23
|
+
assistant_id: graphId,
|
|
24
|
+
graph_id: graphId,
|
|
25
|
+
config: {},
|
|
26
|
+
metadata: {},
|
|
27
|
+
version: 1,
|
|
28
|
+
name: graphId,
|
|
29
|
+
description: '',
|
|
30
|
+
created_at: new Date().toISOString(),
|
|
31
|
+
updated_at: new Date().toISOString(),
|
|
32
|
+
}));
|
|
33
|
+
},
|
|
34
|
+
async getGraph(assistantId, options) {
|
|
35
|
+
const config = {};
|
|
36
|
+
const graph = await getGraph(assistantId, config);
|
|
37
|
+
const drawable = await graph.getGraphAsync({
|
|
38
|
+
...config,
|
|
39
|
+
xray: options?.xray ?? undefined,
|
|
40
|
+
});
|
|
41
|
+
return drawable.toJSON();
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
export const createEndpoint = (threads) => {
|
|
45
|
+
return {
|
|
46
|
+
assistants: AssistantEndpoint,
|
|
47
|
+
threads,
|
|
48
|
+
runs: {
|
|
49
|
+
list(threadId, options) {
|
|
50
|
+
return threads.listRuns(threadId, options);
|
|
51
|
+
},
|
|
52
|
+
async cancel(threadId, runId, wait, action) {
|
|
53
|
+
return globalMessageQueue.cancelQueue(runId);
|
|
54
|
+
},
|
|
55
|
+
async *stream(threadId, assistantId, payload) {
|
|
56
|
+
if (!payload.config) {
|
|
57
|
+
payload.config = {
|
|
58
|
+
configurable: {
|
|
59
|
+
graph_id: assistantId,
|
|
60
|
+
thread_id: threadId,
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const run = threads.createRun(threadId, assistantId, payload);
|
|
65
|
+
for await (const data of streamState(threads, run, payload, {
|
|
66
|
+
attempt: 0,
|
|
67
|
+
getGraph,
|
|
68
|
+
})) {
|
|
69
|
+
yield data;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
joinStream(threadId, runId, options) {
|
|
73
|
+
throw new Error('Function not implemented.');
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
};
|
package/dist/global.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** 全局队列管理器 */
|
|
2
|
+
export declare const globalMessageQueue: import("./queue/stream_queue.js").StreamQueueManager<import("./storage/memory/queue.js").MemoryStreamQueue>;
|
|
3
|
+
/** 全局 Checkpointer */
|
|
4
|
+
export declare const globalCheckPointer: import("@langchain/langgraph-checkpoint").MemorySaver;
|
package/dist/global.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { BaseCheckpointSaver, LangGraphRunnableConfig } from '@langchain/langgraph';
|
|
2
|
+
import type { Pregel } from '@langchain/langgraph/pregel';
|
|
3
|
+
import type { BaseStreamQueueInterface } from '../queue/stream_queue.js';
|
|
4
|
+
import { Run } from '@langgraph-js/sdk';
|
|
5
|
+
import { EventMessage } from '../queue/event_message.js';
|
|
6
|
+
import { BaseThreadsManager } from '../threads/index.js';
|
|
7
|
+
import { StreamInputData } from '../types.js';
|
|
8
|
+
export type LangGraphStreamMode = Pregel<any, any>['streamMode'][number];
|
|
9
|
+
export declare function streamStateWithQueue(threads: BaseThreadsManager, run: Run, queue: BaseStreamQueueInterface, payload: StreamInputData, options: {
|
|
10
|
+
attempt: number;
|
|
11
|
+
getGraph: (graphId: string, config: LangGraphRunnableConfig | undefined, options?: {
|
|
12
|
+
checkpointer?: BaseCheckpointSaver | null;
|
|
13
|
+
}) => Promise<Pregel<any, any, any, any, any>>;
|
|
14
|
+
compressMessages?: boolean;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* 从队列创建数据流生成器
|
|
18
|
+
* @param queueId 队列 ID
|
|
19
|
+
* @param signal 中止信号
|
|
20
|
+
* @returns 数据流生成器
|
|
21
|
+
*/
|
|
22
|
+
export declare function createStreamFromQueue(queueId: string): AsyncGenerator<{
|
|
23
|
+
event: string;
|
|
24
|
+
data: unknown;
|
|
25
|
+
}>;
|
|
26
|
+
export declare const serialiseAsDict: (obj: unknown) => string;
|
|
27
|
+
/**
|
|
28
|
+
* 兼容性函数:保持原有 API,同时使用队列模式
|
|
29
|
+
* @param run 运行配置
|
|
30
|
+
* @param options 选项
|
|
31
|
+
* @returns 数据流生成器
|
|
32
|
+
*/
|
|
33
|
+
export declare function streamState(threads: BaseThreadsManager, run: Run | Promise<Run>, payload: StreamInputData, options: {
|
|
34
|
+
attempt: number;
|
|
35
|
+
getGraph: (graphId: string, config: LangGraphRunnableConfig | undefined, options?: {
|
|
36
|
+
checkpointer?: BaseCheckpointSaver | null;
|
|
37
|
+
}) => Promise<Pregel<any, any, any, any, any>>;
|
|
38
|
+
compressMessages?: boolean;
|
|
39
|
+
}): AsyncGenerator<EventMessage, void, unknown>;
|