@langgraph-js/pure-graph 1.3.0 → 1.4.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 +22 -14
- package/dist/adapter/nextjs/router.js +6 -6
- package/dist/global.d.ts +3 -2
- package/dist/storage/index.d.ts +4 -3
- package/dist/storage/index.js +13 -1
- package/dist/storage/pg/checkpoint.d.ts +2 -0
- package/dist/storage/pg/checkpoint.js +9 -0
- package/dist/storage/pg/threads.d.ts +43 -0
- package/dist/storage/pg/threads.js +304 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +38 -5
- package/.prettierrc +0 -11
- package/bun.lock +0 -209
- package/dist/adapter/hono/zod.d.ts +0 -203
- package/dist/adapter/hono/zod.js +0 -43
- package/dist/adapter/nextjs/zod.d.ts +0 -203
- package/dist/adapter/nextjs/zod.js +0 -60
- package/examples/nextjs/README.md +0 -36
- package/examples/nextjs/app/api/langgraph/[...path]/route.ts +0 -10
- package/examples/nextjs/app/favicon.ico +0 -0
- package/examples/nextjs/app/globals.css +0 -26
- package/examples/nextjs/app/layout.tsx +0 -34
- package/examples/nextjs/app/page.tsx +0 -211
- package/examples/nextjs/next.config.ts +0 -26
- package/examples/nextjs/package.json +0 -24
- package/examples/nextjs/postcss.config.mjs +0 -5
- package/examples/nextjs/tsconfig.json +0 -27
- package/packages/agent-graph/demo.json +0 -35
- package/packages/agent-graph/package.json +0 -18
- package/packages/agent-graph/src/index.ts +0 -47
- package/packages/agent-graph/src/tools/tavily.ts +0 -9
- package/packages/agent-graph/src/tools.ts +0 -38
- package/packages/agent-graph/src/types.ts +0 -42
- package/pnpm-workspace.yaml +0 -4
- package/src/adapter/hono/assistants.ts +0 -24
- package/src/adapter/hono/endpoint.ts +0 -3
- package/src/adapter/hono/index.ts +0 -14
- package/src/adapter/hono/runs.ts +0 -92
- package/src/adapter/hono/threads.ts +0 -37
- package/src/adapter/nextjs/endpoint.ts +0 -2
- package/src/adapter/nextjs/index.ts +0 -2
- package/src/adapter/nextjs/router.ts +0 -206
- package/src/adapter/nextjs/zod.ts +0 -66
- package/src/adapter/zod.ts +0 -144
- package/src/createEndpoint.ts +0 -116
- package/src/e.d.ts +0 -3
- package/src/global.ts +0 -11
- package/src/graph/stream.ts +0 -263
- package/src/graph/stringify.ts +0 -219
- package/src/index.ts +0 -6
- package/src/queue/JsonPlusSerializer.ts +0 -143
- package/src/queue/event_message.ts +0 -30
- package/src/queue/stream_queue.ts +0 -237
- package/src/storage/index.ts +0 -52
- package/src/storage/memory/checkpoint.ts +0 -2
- package/src/storage/memory/queue.ts +0 -91
- package/src/storage/memory/threads.ts +0 -183
- package/src/storage/redis/queue.ts +0 -148
- package/src/storage/sqlite/DB.ts +0 -16
- package/src/storage/sqlite/checkpoint.ts +0 -502
- package/src/storage/sqlite/threads.ts +0 -405
- package/src/storage/sqlite/type.ts +0 -12
- package/src/threads/index.ts +0 -37
- package/src/types.ts +0 -118
- package/src/utils/createEntrypointGraph.ts +0 -20
- package/src/utils/getGraph.ts +0 -44
- package/src/utils/getLangGraphCommand.ts +0 -21
- package/test/graph/entrypoint.ts +0 -21
- package/test/graph/index.ts +0 -60
- package/test/hono.ts +0 -15
- package/tsconfig.json +0 -20
package/src/adapter/zod.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import z from 'zod';
|
|
2
|
-
|
|
3
|
-
export const AssistantConfigurable = z
|
|
4
|
-
.object({
|
|
5
|
-
thread_id: z.string().optional(),
|
|
6
|
-
thread_ts: z.string().optional(),
|
|
7
|
-
})
|
|
8
|
-
.catchall(z.unknown());
|
|
9
|
-
|
|
10
|
-
export const AssistantConfig = z
|
|
11
|
-
.object({
|
|
12
|
-
tags: z.array(z.string()).optional(),
|
|
13
|
-
recursion_limit: z.number().int().optional(),
|
|
14
|
-
configurable: AssistantConfigurable.optional(),
|
|
15
|
-
})
|
|
16
|
-
.catchall(z.unknown())
|
|
17
|
-
.describe('The configuration of an assistant.');
|
|
18
|
-
|
|
19
|
-
export const Assistant = z.object({
|
|
20
|
-
assistant_id: z.string().uuid(),
|
|
21
|
-
graph_id: z.string(),
|
|
22
|
-
config: AssistantConfig,
|
|
23
|
-
created_at: z.string(),
|
|
24
|
-
updated_at: z.string(),
|
|
25
|
-
metadata: z.object({}).catchall(z.any()),
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
export const MetadataSchema = z
|
|
29
|
-
.object({
|
|
30
|
-
source: z.union([z.literal('input'), z.literal('loop'), z.literal('update'), z.string()]).optional(),
|
|
31
|
-
step: z.number().optional(),
|
|
32
|
-
writes: z.record(z.unknown()).nullable().optional(),
|
|
33
|
-
parents: z.record(z.string()).optional(),
|
|
34
|
-
})
|
|
35
|
-
.catchall(z.unknown());
|
|
36
|
-
|
|
37
|
-
export const SendSchema = z.object({
|
|
38
|
-
node: z.string(),
|
|
39
|
-
input: z.unknown().nullable(),
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
export const CommandSchema = z.object({
|
|
43
|
-
update: z
|
|
44
|
-
.union([z.record(z.unknown()), z.array(z.tuple([z.string(), z.unknown()]))])
|
|
45
|
-
.nullable()
|
|
46
|
-
.optional(),
|
|
47
|
-
resume: z.unknown().optional(),
|
|
48
|
-
goto: z.union([SendSchema, z.array(SendSchema), z.string(), z.array(z.string())]).optional(),
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
// 公共的查询参数验证 schema
|
|
52
|
-
export const PaginationQuerySchema = z.object({
|
|
53
|
-
limit: z.number().int().optional(),
|
|
54
|
-
offset: z.number().int().optional(),
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
export const ThreadIdParamSchema = z.object({
|
|
58
|
-
thread_id: z.string().uuid(),
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
export const RunIdParamSchema = z.object({
|
|
62
|
-
thread_id: z.string().uuid(),
|
|
63
|
-
run_id: z.string().uuid(),
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// Assistants 相关的 schema
|
|
67
|
-
export const AssistantsSearchSchema = z.object({
|
|
68
|
-
graph_id: z.string().optional(),
|
|
69
|
-
metadata: MetadataSchema.optional(),
|
|
70
|
-
limit: z.number().int().optional(),
|
|
71
|
-
offset: z.number().int().optional(),
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
export const AssistantGraphQuerySchema = z.object({
|
|
75
|
-
xray: z.string().optional(),
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// Runs 相关的 schema
|
|
79
|
-
export const RunStreamPayloadSchema = z
|
|
80
|
-
.object({
|
|
81
|
-
assistant_id: z.union([z.string().uuid(), z.string()]),
|
|
82
|
-
checkpoint_id: z.string().optional(),
|
|
83
|
-
input: z.any().optional(),
|
|
84
|
-
command: CommandSchema.optional(),
|
|
85
|
-
metadata: MetadataSchema.optional(),
|
|
86
|
-
config: AssistantConfig.optional(),
|
|
87
|
-
webhook: z.string().optional(),
|
|
88
|
-
interrupt_before: z.union([z.literal('*'), z.array(z.string())]).optional(),
|
|
89
|
-
interrupt_after: z.union([z.literal('*'), z.array(z.string())]).optional(),
|
|
90
|
-
on_disconnect: z.enum(['cancel', 'continue']).optional().default('continue'),
|
|
91
|
-
multitask_strategy: z.enum(['reject', 'rollback', 'interrupt', 'enqueue']).optional(),
|
|
92
|
-
stream_mode: z
|
|
93
|
-
.array(z.enum(['values', 'messages', 'messages-tuple', 'updates', 'events', 'debug', 'custom']))
|
|
94
|
-
.optional(),
|
|
95
|
-
stream_subgraphs: z.boolean().optional(),
|
|
96
|
-
stream_resumable: z.boolean().optional(),
|
|
97
|
-
after_seconds: z.number().optional(),
|
|
98
|
-
if_not_exists: z.enum(['create', 'reject']).optional(),
|
|
99
|
-
on_completion: z.enum(['complete', 'continue']).optional(),
|
|
100
|
-
feedback_keys: z.array(z.string()).optional(),
|
|
101
|
-
langsmith_tracer: z.unknown().optional(),
|
|
102
|
-
})
|
|
103
|
-
.describe('Payload for creating a stateful run.');
|
|
104
|
-
|
|
105
|
-
export const RunListQuerySchema = z.object({
|
|
106
|
-
limit: z.coerce.number().int().optional(),
|
|
107
|
-
offset: z.coerce.number().int().optional(),
|
|
108
|
-
status: z.enum(['pending', 'running', 'error', 'success', 'timeout', 'interrupted']).optional(),
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
export const RunCancelQuerySchema = z.object({
|
|
112
|
-
wait: z.coerce.boolean().optional().default(false),
|
|
113
|
-
action: z.enum(['interrupt', 'rollback']).optional().default('interrupt'),
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
// Threads 相关的 schema
|
|
117
|
-
export const ThreadCreatePayloadSchema = z
|
|
118
|
-
.object({
|
|
119
|
-
thread_id: z.string().uuid().describe('The ID of the thread. If not provided, an ID is generated.').optional(),
|
|
120
|
-
metadata: MetadataSchema.optional(),
|
|
121
|
-
if_exists: z.union([z.literal('raise'), z.literal('do_nothing')]).optional(),
|
|
122
|
-
})
|
|
123
|
-
.describe('Payload for creating a thread.');
|
|
124
|
-
|
|
125
|
-
export const ThreadSearchPayloadSchema = z
|
|
126
|
-
.object({
|
|
127
|
-
metadata: z.record(z.unknown()).describe('Metadata to search for.').optional(),
|
|
128
|
-
status: z.enum(['idle', 'busy', 'interrupted', 'error']).describe('Filter by thread status.').optional(),
|
|
129
|
-
values: z.record(z.unknown()).describe('Filter by thread values.').optional(),
|
|
130
|
-
limit: z.number().int().gte(1).lte(1000).describe('Maximum number to return.').optional(),
|
|
131
|
-
offset: z.number().int().gte(0).describe('Offset to start from.').optional(),
|
|
132
|
-
sort_by: z.enum(['thread_id', 'status', 'created_at', 'updated_at']).describe('Sort by field.').optional(),
|
|
133
|
-
sort_order: z.enum(['asc', 'desc']).describe('Sort order.').optional(),
|
|
134
|
-
})
|
|
135
|
-
.describe('Payload for listing threads.');
|
|
136
|
-
|
|
137
|
-
export const ThreadStateUpdate = z
|
|
138
|
-
.object({
|
|
139
|
-
values: z.union([z.record(z.string(), z.unknown()), z.array(z.record(z.string(), z.unknown()))]).nullish(),
|
|
140
|
-
// as_node: z.string().optional(),
|
|
141
|
-
// checkpoint_id: z.string().optional(),
|
|
142
|
-
// checkpoint: CheckpointSchema.nullish(),
|
|
143
|
-
})
|
|
144
|
-
.describe('Payload for adding state to a thread.');
|
package/src/createEndpoint.ts
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { StreamEvent } from '@langchain/core/tracers/log_stream';
|
|
2
|
-
import { streamState } from './graph/stream.js';
|
|
3
|
-
import { Assistant, Run, StreamMode, Metadata, AssistantGraph } from '@langchain/langgraph-sdk';
|
|
4
|
-
import { getGraph, GRAPHS } from './utils/getGraph.js';
|
|
5
|
-
import { LangGraphGlobal } from './global.js';
|
|
6
|
-
import { AssistantSortBy, CancelAction, ILangGraphClient, RunStatus, SortOrder, StreamInputData } from './types.js';
|
|
7
|
-
export { registerGraph } from './utils/getGraph.js';
|
|
8
|
-
|
|
9
|
-
export const AssistantEndpoint: ILangGraphClient['assistants'] = {
|
|
10
|
-
async search(query?: {
|
|
11
|
-
graphId?: string;
|
|
12
|
-
metadata?: Metadata;
|
|
13
|
-
limit?: number;
|
|
14
|
-
offset?: number;
|
|
15
|
-
sortBy?: AssistantSortBy;
|
|
16
|
-
sortOrder?: SortOrder;
|
|
17
|
-
}): Promise<Assistant[]> {
|
|
18
|
-
if (query?.graphId) {
|
|
19
|
-
return [
|
|
20
|
-
{
|
|
21
|
-
assistant_id: query.graphId,
|
|
22
|
-
graph_id: query.graphId,
|
|
23
|
-
config: {},
|
|
24
|
-
created_at: new Date().toISOString(),
|
|
25
|
-
updated_at: new Date().toISOString(),
|
|
26
|
-
metadata: {},
|
|
27
|
-
version: 1,
|
|
28
|
-
name: query.graphId,
|
|
29
|
-
description: '',
|
|
30
|
-
} as Assistant,
|
|
31
|
-
];
|
|
32
|
-
}
|
|
33
|
-
return Object.entries(GRAPHS).map(
|
|
34
|
-
([graphId, _]) =>
|
|
35
|
-
({
|
|
36
|
-
assistant_id: graphId,
|
|
37
|
-
graph_id: graphId,
|
|
38
|
-
config: {},
|
|
39
|
-
metadata: {},
|
|
40
|
-
version: 1,
|
|
41
|
-
name: graphId,
|
|
42
|
-
description: '',
|
|
43
|
-
created_at: new Date().toISOString(),
|
|
44
|
-
updated_at: new Date().toISOString(),
|
|
45
|
-
} as Assistant),
|
|
46
|
-
);
|
|
47
|
-
},
|
|
48
|
-
async getGraph(assistantId: string, options?: { xray?: boolean | number }): Promise<AssistantGraph> {
|
|
49
|
-
const config = {};
|
|
50
|
-
const graph = await getGraph(assistantId, config);
|
|
51
|
-
const drawable = await graph.getGraphAsync({
|
|
52
|
-
...config,
|
|
53
|
-
xray: options?.xray ?? undefined,
|
|
54
|
-
});
|
|
55
|
-
return drawable.toJSON() as AssistantGraph;
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export const createEndpoint = (): ILangGraphClient => {
|
|
60
|
-
const threads = LangGraphGlobal.globalThreadsManager;
|
|
61
|
-
return {
|
|
62
|
-
assistants: AssistantEndpoint,
|
|
63
|
-
threads,
|
|
64
|
-
runs: {
|
|
65
|
-
list(
|
|
66
|
-
threadId: string,
|
|
67
|
-
options?: {
|
|
68
|
-
limit?: number;
|
|
69
|
-
offset?: number;
|
|
70
|
-
status?: RunStatus;
|
|
71
|
-
},
|
|
72
|
-
): Promise<Run[]> {
|
|
73
|
-
return threads.listRuns(threadId, options);
|
|
74
|
-
},
|
|
75
|
-
async cancel(threadId: string, runId: string, wait?: boolean, action?: CancelAction): Promise<void> {
|
|
76
|
-
return LangGraphGlobal.globalMessageQueue.cancelQueue(runId);
|
|
77
|
-
},
|
|
78
|
-
async *stream(threadId: string, assistantId: string, payload: StreamInputData) {
|
|
79
|
-
if (!payload.config) {
|
|
80
|
-
payload.config = {
|
|
81
|
-
configurable: {
|
|
82
|
-
graph_id: assistantId,
|
|
83
|
-
thread_id: threadId,
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
for await (const data of streamState(
|
|
89
|
-
threads,
|
|
90
|
-
threads.createRun(threadId, assistantId, payload),
|
|
91
|
-
payload,
|
|
92
|
-
{
|
|
93
|
-
attempt: 0,
|
|
94
|
-
getGraph,
|
|
95
|
-
},
|
|
96
|
-
)) {
|
|
97
|
-
yield data;
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
joinStream(
|
|
101
|
-
threadId: string,
|
|
102
|
-
runId: string,
|
|
103
|
-
options?:
|
|
104
|
-
| {
|
|
105
|
-
signal?: AbortSignal;
|
|
106
|
-
cancelOnDisconnect?: boolean;
|
|
107
|
-
lastEventId?: string;
|
|
108
|
-
streamMode?: StreamMode | StreamMode[];
|
|
109
|
-
}
|
|
110
|
-
| AbortSignal,
|
|
111
|
-
): AsyncGenerator<{ id?: string; event: StreamEvent; data: any }> {
|
|
112
|
-
throw new Error('Function not implemented.');
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
};
|
package/src/e.d.ts
DELETED
package/src/global.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { createCheckPointer, createMessageQueue, createThreadManager } from './storage/index.js';
|
|
2
|
-
import type { SqliteSaver } from './storage/sqlite/checkpoint.js';
|
|
3
|
-
const [globalMessageQueue, globalCheckPointer] = await Promise.all([createMessageQueue(), createCheckPointer()]);
|
|
4
|
-
const globalThreadsManager = await createThreadManager({
|
|
5
|
-
checkpointer: globalCheckPointer as SqliteSaver,
|
|
6
|
-
});
|
|
7
|
-
export class LangGraphGlobal {
|
|
8
|
-
static globalMessageQueue = globalMessageQueue;
|
|
9
|
-
static globalCheckPointer = globalCheckPointer;
|
|
10
|
-
static globalThreadsManager = globalThreadsManager;
|
|
11
|
-
}
|
package/src/graph/stream.ts
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
import { BaseMessageChunk, isBaseMessage } from '@langchain/core/messages';
|
|
2
|
-
import type { BaseCheckpointSaver, LangGraphRunnableConfig } from '@langchain/langgraph';
|
|
3
|
-
import type { Pregel } from '@langchain/langgraph/pregel';
|
|
4
|
-
import { getLangGraphCommand } from '../utils/getLangGraphCommand.js';
|
|
5
|
-
import type { BaseStreamQueueInterface } from '../queue/stream_queue.js';
|
|
6
|
-
|
|
7
|
-
import { LangGraphGlobal } from '../global.js';
|
|
8
|
-
import { Run } from '@langgraph-js/sdk';
|
|
9
|
-
import { EventMessage, StreamErrorEventMessage, StreamEndEventMessage } from '../queue/event_message.js';
|
|
10
|
-
|
|
11
|
-
import { BaseThreadsManager } from '../threads/index.js';
|
|
12
|
-
import { StreamInputData } from '../types.js';
|
|
13
|
-
|
|
14
|
-
export type LangGraphStreamMode = Pregel<any, any>['streamMode'][number];
|
|
15
|
-
|
|
16
|
-
export async function streamStateWithQueue(
|
|
17
|
-
threads: BaseThreadsManager,
|
|
18
|
-
run: Run,
|
|
19
|
-
queue: BaseStreamQueueInterface,
|
|
20
|
-
payload: StreamInputData,
|
|
21
|
-
options: {
|
|
22
|
-
attempt: number;
|
|
23
|
-
getGraph: (
|
|
24
|
-
graphId: string,
|
|
25
|
-
config: LangGraphRunnableConfig | undefined,
|
|
26
|
-
options?: { checkpointer?: BaseCheckpointSaver | null },
|
|
27
|
-
) => Promise<Pregel<any, any, any, any, any>>;
|
|
28
|
-
compressMessages?: boolean;
|
|
29
|
-
},
|
|
30
|
-
): Promise<void> {
|
|
31
|
-
const kwargs = payload;
|
|
32
|
-
const graphId = kwargs.config?.configurable?.graph_id;
|
|
33
|
-
|
|
34
|
-
if (!graphId || typeof graphId !== 'string') {
|
|
35
|
-
throw new Error('Invalid or missing graph_id');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const graph = await options.getGraph(graphId, payload.config, {
|
|
39
|
-
checkpointer: payload.temporary ? null : undefined,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
const userStreamMode = payload.stream_mode ?? [];
|
|
43
|
-
|
|
44
|
-
const libStreamMode: Set<LangGraphStreamMode> = new Set([
|
|
45
|
-
'values',
|
|
46
|
-
...userStreamMode.filter((mode) => mode !== 'events' && mode !== 'messages-tuple'),
|
|
47
|
-
]);
|
|
48
|
-
|
|
49
|
-
if (userStreamMode.includes('messages-tuple')) {
|
|
50
|
-
libStreamMode.add('messages');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (userStreamMode.includes('messages')) {
|
|
54
|
-
libStreamMode.add('values');
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
await queue.push(
|
|
58
|
-
new EventMessage('metadata', {
|
|
59
|
-
run_id: run.run_id,
|
|
60
|
-
attempt: options.attempt,
|
|
61
|
-
graph_id: graphId,
|
|
62
|
-
}),
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
const metadata = {
|
|
66
|
-
...payload.config?.metadata,
|
|
67
|
-
run_attempt: options.attempt,
|
|
68
|
-
};
|
|
69
|
-
const events = graph.streamEvents(
|
|
70
|
-
payload.command != null ? getLangGraphCommand(payload.command) : payload.input ?? null,
|
|
71
|
-
{
|
|
72
|
-
version: 'v2' as const,
|
|
73
|
-
|
|
74
|
-
interruptAfter: payload.interrupt_after,
|
|
75
|
-
interruptBefore: payload.interrupt_before,
|
|
76
|
-
|
|
77
|
-
tags: payload.config?.tags,
|
|
78
|
-
configurable: payload.config?.configurable,
|
|
79
|
-
recursionLimit: payload.config?.recursionLimit,
|
|
80
|
-
subgraphs: payload.stream_subgraphs,
|
|
81
|
-
metadata,
|
|
82
|
-
|
|
83
|
-
runId: run.run_id,
|
|
84
|
-
streamMode: [...libStreamMode],
|
|
85
|
-
signal: queue.cancelSignal.signal,
|
|
86
|
-
},
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
const messages: Record<string, BaseMessageChunk> = {};
|
|
90
|
-
const completedIds = new Set<string>();
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
for await (const event of events) {
|
|
94
|
-
// console.log(event);
|
|
95
|
-
if (event.tags?.includes('langsmith:hidden')) continue;
|
|
96
|
-
if (event.event === 'on_chain_stream' && event.run_id === run.run_id) {
|
|
97
|
-
const [ns, mode, chunk] = (
|
|
98
|
-
payload.stream_subgraphs ? event.data.chunk : [null, ...event.data.chunk]
|
|
99
|
-
) as [string[] | null, LangGraphStreamMode, unknown];
|
|
100
|
-
|
|
101
|
-
// Listen for debug events and capture checkpoint
|
|
102
|
-
let data: unknown = chunk;
|
|
103
|
-
|
|
104
|
-
if (mode === 'messages') {
|
|
105
|
-
if (userStreamMode.includes('messages-tuple')) {
|
|
106
|
-
await queue.push(new EventMessage('messages', data));
|
|
107
|
-
}
|
|
108
|
-
} else if (userStreamMode.includes(mode)) {
|
|
109
|
-
if (payload.stream_subgraphs && ns?.length) {
|
|
110
|
-
await queue.push(new EventMessage(`${mode}|${ns.join('|')}`, data));
|
|
111
|
-
} else {
|
|
112
|
-
await queue.push(new EventMessage(mode, data));
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (mode === 'values') {
|
|
116
|
-
await threads.set(run.thread_id, {
|
|
117
|
-
values: data ? JSON.parse(serialiseAsDict(data)) : '',
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
} else if (userStreamMode.includes('events')) {
|
|
121
|
-
await queue.push(new EventMessage('events', event));
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// TODO: we still rely on old messages mode based of streamMode=values
|
|
125
|
-
// In order to fully switch to library messages mode, we need to do ensure that
|
|
126
|
-
// `StreamMessagesHandler` sends the final message, which requires the following:
|
|
127
|
-
// - handleLLMEnd does not send the final message b/c handleLLMNewToken sets the this.emittedChatModelRunIds[runId] flag. Python does not do that
|
|
128
|
-
// - handleLLMEnd receives the final message as BaseMessageChunk rather than BaseMessage, which from the outside will become indistinguishable.
|
|
129
|
-
// - handleLLMEnd should not dedupe the message
|
|
130
|
-
// - Don't think there's an utility that would convert a BaseMessageChunk to a BaseMessage?
|
|
131
|
-
if (userStreamMode.includes('messages')) {
|
|
132
|
-
if (event.event === 'on_chain_stream' && event.run_id === run.run_id) {
|
|
133
|
-
const newMessages: Array<BaseMessageChunk> = [];
|
|
134
|
-
const [_, chunk]: [string, any] = event.data.chunk;
|
|
135
|
-
|
|
136
|
-
let chunkMessages: Array<BaseMessageChunk> = [];
|
|
137
|
-
if (typeof chunk === 'object' && chunk != null && 'messages' in chunk && !isBaseMessage(chunk)) {
|
|
138
|
-
chunkMessages = chunk?.messages;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (!Array.isArray(chunkMessages)) {
|
|
142
|
-
chunkMessages = [chunkMessages];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
for (const message of chunkMessages) {
|
|
146
|
-
if (!message.id || completedIds.has(message.id)) continue;
|
|
147
|
-
completedIds.add(message.id);
|
|
148
|
-
newMessages.push(message);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (newMessages.length > 0) {
|
|
152
|
-
await queue.push(new EventMessage('messages/complete', newMessages));
|
|
153
|
-
}
|
|
154
|
-
} else if (event.event === 'on_chat_model_stream' && !event.tags?.includes('nostream')) {
|
|
155
|
-
const message: BaseMessageChunk = event.data.chunk;
|
|
156
|
-
|
|
157
|
-
if (!message.id) continue;
|
|
158
|
-
|
|
159
|
-
if (messages[message.id] == null) {
|
|
160
|
-
messages[message.id] = message;
|
|
161
|
-
await queue.push(
|
|
162
|
-
new EventMessage('messages/metadata', {
|
|
163
|
-
[message.id]: { metadata: event.metadata },
|
|
164
|
-
}),
|
|
165
|
-
);
|
|
166
|
-
} else {
|
|
167
|
-
messages[message.id] = messages[message.id].concat(message);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
await queue.push(new EventMessage('messages/partial', [messages[message.id]]));
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
} finally {
|
|
175
|
-
// 发送流结束信号
|
|
176
|
-
await queue.push(new StreamEndEventMessage());
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* 从队列创建数据流生成器
|
|
182
|
-
* @param queueId 队列 ID
|
|
183
|
-
* @param signal 中止信号
|
|
184
|
-
* @returns 数据流生成器
|
|
185
|
-
*/
|
|
186
|
-
export async function* createStreamFromQueue(queueId: string): AsyncGenerator<{ event: string; data: unknown }> {
|
|
187
|
-
const queue = LangGraphGlobal.globalMessageQueue.getQueue(queueId);
|
|
188
|
-
return queue.onDataReceive();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
export const serialiseAsDict = (obj: unknown, indent = 2) => {
|
|
192
|
-
return JSON.stringify(
|
|
193
|
-
obj,
|
|
194
|
-
function (key: string | number, value: unknown) {
|
|
195
|
-
const rawValue = this[key];
|
|
196
|
-
if (
|
|
197
|
-
rawValue != null &&
|
|
198
|
-
typeof rawValue === 'object' &&
|
|
199
|
-
'toDict' in rawValue &&
|
|
200
|
-
typeof rawValue.toDict === 'function'
|
|
201
|
-
) {
|
|
202
|
-
// TODO: we need to upstream this to LangChainJS
|
|
203
|
-
const { type, data } = rawValue.toDict();
|
|
204
|
-
return { ...data, type };
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
return value;
|
|
208
|
-
},
|
|
209
|
-
indent,
|
|
210
|
-
);
|
|
211
|
-
};
|
|
212
|
-
/**
|
|
213
|
-
* 兼容性函数:保持原有 API,同时使用队列模式
|
|
214
|
-
* @param run 运行配置
|
|
215
|
-
* @param options 选项
|
|
216
|
-
* @returns 数据流生成器
|
|
217
|
-
*/
|
|
218
|
-
export async function* streamState(
|
|
219
|
-
threads: BaseThreadsManager,
|
|
220
|
-
run: Run | Promise<Run>,
|
|
221
|
-
payload: StreamInputData,
|
|
222
|
-
options: {
|
|
223
|
-
attempt: number;
|
|
224
|
-
getGraph: (
|
|
225
|
-
graphId: string,
|
|
226
|
-
config: LangGraphRunnableConfig | undefined,
|
|
227
|
-
options?: { checkpointer?: BaseCheckpointSaver | null },
|
|
228
|
-
) => Promise<Pregel<any, any, any, any, any>>;
|
|
229
|
-
compressMessages?: boolean;
|
|
230
|
-
},
|
|
231
|
-
) {
|
|
232
|
-
run = await run;
|
|
233
|
-
// 生成唯一的队列 ID
|
|
234
|
-
const queueId = run.run_id;
|
|
235
|
-
const threadId = run.thread_id;
|
|
236
|
-
try {
|
|
237
|
-
// 启动队列推送任务(在后台异步执行)
|
|
238
|
-
await threads.set(threadId, { status: 'busy' });
|
|
239
|
-
await threads.updateRun(run.run_id, { status: 'running' });
|
|
240
|
-
const queue = LangGraphGlobal.globalMessageQueue.createQueue(queueId);
|
|
241
|
-
const state = queue.onDataReceive();
|
|
242
|
-
streamStateWithQueue(threads, run, queue, payload, options).catch((error) => {
|
|
243
|
-
console.error('Queue task error:', error);
|
|
244
|
-
// 如果生产者出错,向队列推送错误信号
|
|
245
|
-
LangGraphGlobal.globalMessageQueue.pushToQueue(queueId, new StreamErrorEventMessage(error));
|
|
246
|
-
// TODO 不知道这里需不需要错误处理
|
|
247
|
-
});
|
|
248
|
-
for await (const data of state) {
|
|
249
|
-
yield data;
|
|
250
|
-
}
|
|
251
|
-
await threads.updateRun(run.run_id, { status: 'success' });
|
|
252
|
-
} catch (error) {
|
|
253
|
-
// 如果发生错误,确保清理资源
|
|
254
|
-
console.error('Stream error:', error);
|
|
255
|
-
await threads.updateRun(run.run_id, { status: 'error' });
|
|
256
|
-
await threads.set(threadId, { status: 'error' });
|
|
257
|
-
// throw error;
|
|
258
|
-
} finally {
|
|
259
|
-
// 在完成后清理队列
|
|
260
|
-
await threads.set(threadId, { status: 'idle' });
|
|
261
|
-
LangGraphGlobal.globalMessageQueue.removeQueue(queueId);
|
|
262
|
-
}
|
|
263
|
-
}
|