@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
|
@@ -0,0 +1,253 @@
|
|
|
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 { globalMessageQueue } 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(new EventMessage('metadata', { run_id: run.run_id, attempt: options.attempt, graph_id: graphId }));
|
|
58
|
+
|
|
59
|
+
const metadata = {
|
|
60
|
+
...payload.config?.metadata,
|
|
61
|
+
run_attempt: options.attempt,
|
|
62
|
+
};
|
|
63
|
+
const events = graph.streamEvents(
|
|
64
|
+
payload.command != null ? getLangGraphCommand(payload.command) : payload.input ?? null,
|
|
65
|
+
{
|
|
66
|
+
version: 'v2' as const,
|
|
67
|
+
|
|
68
|
+
interruptAfter: payload.interrupt_after,
|
|
69
|
+
interruptBefore: payload.interrupt_before,
|
|
70
|
+
|
|
71
|
+
tags: payload.config?.tags,
|
|
72
|
+
configurable: payload.config?.configurable,
|
|
73
|
+
recursionLimit: payload.config?.recursionLimit,
|
|
74
|
+
subgraphs: payload.stream_subgraphs,
|
|
75
|
+
metadata,
|
|
76
|
+
|
|
77
|
+
runId: run.run_id,
|
|
78
|
+
streamMode: [...libStreamMode],
|
|
79
|
+
signal: queue.cancelSignal.signal,
|
|
80
|
+
},
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const messages: Record<string, BaseMessageChunk> = {};
|
|
84
|
+
const completedIds = new Set<string>();
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
for await (const event of events) {
|
|
88
|
+
// console.log(event);
|
|
89
|
+
if (event.tags?.includes('langsmith:hidden')) continue;
|
|
90
|
+
if (event.event === 'on_chain_stream' && event.run_id === run.run_id) {
|
|
91
|
+
const [ns, mode, chunk] = (
|
|
92
|
+
payload.stream_subgraphs ? event.data.chunk : [null, ...event.data.chunk]
|
|
93
|
+
) as [string[] | null, LangGraphStreamMode, unknown];
|
|
94
|
+
|
|
95
|
+
// Listen for debug events and capture checkpoint
|
|
96
|
+
let data: unknown = chunk;
|
|
97
|
+
|
|
98
|
+
if (mode === 'messages') {
|
|
99
|
+
if (userStreamMode.includes('messages-tuple')) {
|
|
100
|
+
await queue.push(new EventMessage('messages', data));
|
|
101
|
+
}
|
|
102
|
+
} else if (userStreamMode.includes(mode)) {
|
|
103
|
+
if (payload.stream_subgraphs && ns?.length) {
|
|
104
|
+
await queue.push(new EventMessage(`${mode}|${ns.join('|')}`, data));
|
|
105
|
+
} else {
|
|
106
|
+
await queue.push(new EventMessage(mode, data));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (mode === 'values') {
|
|
110
|
+
await threads.set(run.thread_id, { values: JSON.parse(serialiseAsDict(data)) });
|
|
111
|
+
}
|
|
112
|
+
} else if (userStreamMode.includes('events')) {
|
|
113
|
+
await queue.push(new EventMessage('events', event));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// TODO: we still rely on old messages mode based of streamMode=values
|
|
117
|
+
// In order to fully switch to library messages mode, we need to do ensure that
|
|
118
|
+
// `StreamMessagesHandler` sends the final message, which requires the following:
|
|
119
|
+
// - handleLLMEnd does not send the final message b/c handleLLMNewToken sets the this.emittedChatModelRunIds[runId] flag. Python does not do that
|
|
120
|
+
// - handleLLMEnd receives the final message as BaseMessageChunk rather than BaseMessage, which from the outside will become indistinguishable.
|
|
121
|
+
// - handleLLMEnd should not dedupe the message
|
|
122
|
+
// - Don't think there's an utility that would convert a BaseMessageChunk to a BaseMessage?
|
|
123
|
+
if (userStreamMode.includes('messages')) {
|
|
124
|
+
if (event.event === 'on_chain_stream' && event.run_id === run.run_id) {
|
|
125
|
+
const newMessages: Array<BaseMessageChunk> = [];
|
|
126
|
+
const [_, chunk]: [string, any] = event.data.chunk;
|
|
127
|
+
|
|
128
|
+
let chunkMessages: Array<BaseMessageChunk> = [];
|
|
129
|
+
if (typeof chunk === 'object' && chunk != null && 'messages' in chunk && !isBaseMessage(chunk)) {
|
|
130
|
+
chunkMessages = chunk?.messages;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (!Array.isArray(chunkMessages)) {
|
|
134
|
+
chunkMessages = [chunkMessages];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (const message of chunkMessages) {
|
|
138
|
+
if (!message.id || completedIds.has(message.id)) continue;
|
|
139
|
+
completedIds.add(message.id);
|
|
140
|
+
newMessages.push(message);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (newMessages.length > 0) {
|
|
144
|
+
await queue.push(new EventMessage('messages/complete', newMessages));
|
|
145
|
+
}
|
|
146
|
+
} else if (event.event === 'on_chat_model_stream' && !event.tags?.includes('nostream')) {
|
|
147
|
+
const message: BaseMessageChunk = event.data.chunk;
|
|
148
|
+
|
|
149
|
+
if (!message.id) continue;
|
|
150
|
+
|
|
151
|
+
if (messages[message.id] == null) {
|
|
152
|
+
messages[message.id] = message;
|
|
153
|
+
await queue.push(
|
|
154
|
+
new EventMessage('messages/metadata', { [message.id]: { metadata: event.metadata } }),
|
|
155
|
+
);
|
|
156
|
+
} else {
|
|
157
|
+
messages[message.id] = messages[message.id].concat(message);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
await queue.push(new EventMessage('messages/partial', [messages[message.id]]));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
} finally {
|
|
165
|
+
// 发送流结束信号
|
|
166
|
+
await queue.push(new StreamEndEventMessage());
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 从队列创建数据流生成器
|
|
172
|
+
* @param queueId 队列 ID
|
|
173
|
+
* @param signal 中止信号
|
|
174
|
+
* @returns 数据流生成器
|
|
175
|
+
*/
|
|
176
|
+
export async function* createStreamFromQueue(queueId: string): AsyncGenerator<{ event: string; data: unknown }> {
|
|
177
|
+
const queue = globalMessageQueue.getQueue(queueId);
|
|
178
|
+
return queue.onDataReceive();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export const serialiseAsDict = (obj: unknown) => {
|
|
182
|
+
return JSON.stringify(
|
|
183
|
+
obj,
|
|
184
|
+
function (key: string | number, value: unknown) {
|
|
185
|
+
const rawValue = this[key];
|
|
186
|
+
if (
|
|
187
|
+
rawValue != null &&
|
|
188
|
+
typeof rawValue === 'object' &&
|
|
189
|
+
'toDict' in rawValue &&
|
|
190
|
+
typeof rawValue.toDict === 'function'
|
|
191
|
+
) {
|
|
192
|
+
// TODO: we need to upstream this to LangChainJS
|
|
193
|
+
const { type, data } = rawValue.toDict();
|
|
194
|
+
return { ...data, type };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return value;
|
|
198
|
+
},
|
|
199
|
+
2,
|
|
200
|
+
);
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* 兼容性函数:保持原有 API,同时使用队列模式
|
|
204
|
+
* @param run 运行配置
|
|
205
|
+
* @param options 选项
|
|
206
|
+
* @returns 数据流生成器
|
|
207
|
+
*/
|
|
208
|
+
export async function* streamState(
|
|
209
|
+
threads: BaseThreadsManager,
|
|
210
|
+
run: Run | Promise<Run>,
|
|
211
|
+
payload: StreamInputData,
|
|
212
|
+
options: {
|
|
213
|
+
attempt: number;
|
|
214
|
+
getGraph: (
|
|
215
|
+
graphId: string,
|
|
216
|
+
config: LangGraphRunnableConfig | undefined,
|
|
217
|
+
options?: { checkpointer?: BaseCheckpointSaver | null },
|
|
218
|
+
) => Promise<Pregel<any, any, any, any, any>>;
|
|
219
|
+
compressMessages?: boolean;
|
|
220
|
+
},
|
|
221
|
+
) {
|
|
222
|
+
run = await run;
|
|
223
|
+
// 生成唯一的队列 ID
|
|
224
|
+
const queueId = run.run_id;
|
|
225
|
+
const threadId = run.thread_id;
|
|
226
|
+
try {
|
|
227
|
+
// 启动队列推送任务(在后台异步执行)
|
|
228
|
+
await threads.set(threadId, { status: 'busy' });
|
|
229
|
+
await threads.updateRun(run.run_id, { status: 'running' });
|
|
230
|
+
const queue = globalMessageQueue.createQueue(queueId);
|
|
231
|
+
const state = queue.onDataReceive();
|
|
232
|
+
streamStateWithQueue(threads, run, queue, payload, options).catch((error) => {
|
|
233
|
+
console.error('Queue task error:', error);
|
|
234
|
+
// 如果生产者出错,向队列推送错误信号
|
|
235
|
+
globalMessageQueue.pushToQueue(queueId, new StreamErrorEventMessage(error));
|
|
236
|
+
// TODO 不知道这里需不需要错误处理
|
|
237
|
+
});
|
|
238
|
+
for await (const data of state) {
|
|
239
|
+
yield data;
|
|
240
|
+
}
|
|
241
|
+
await threads.updateRun(run.run_id, { status: 'success' });
|
|
242
|
+
} catch (error) {
|
|
243
|
+
// 如果发生错误,确保清理资源
|
|
244
|
+
console.error('Stream error:', error);
|
|
245
|
+
await threads.updateRun(run.run_id, { status: 'error' });
|
|
246
|
+
await threads.set(threadId, { status: 'error' });
|
|
247
|
+
// throw error;
|
|
248
|
+
} finally {
|
|
249
|
+
// 在完成后立即清理队列,因为消费者已经完成
|
|
250
|
+
await threads.set(threadId, { status: 'idle' });
|
|
251
|
+
globalMessageQueue.removeQueue(queueId);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
// @ts-nocheck
|
|
3
|
+
|
|
4
|
+
// Stringify that can handle circular references.
|
|
5
|
+
// Inlined due to ESM import issues
|
|
6
|
+
// Source: https://www.npmjs.com/package/fast-safe-stringify
|
|
7
|
+
|
|
8
|
+
var LIMIT_REPLACE_NODE = '[...]';
|
|
9
|
+
var CIRCULAR_REPLACE_NODE = '[Circular]';
|
|
10
|
+
|
|
11
|
+
var arr = [];
|
|
12
|
+
var replacerStack = [];
|
|
13
|
+
|
|
14
|
+
function defaultOptions() {
|
|
15
|
+
return {
|
|
16
|
+
depthLimit: Number.MAX_SAFE_INTEGER,
|
|
17
|
+
edgesLimit: Number.MAX_SAFE_INTEGER,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Regular stringify
|
|
22
|
+
export function stringify(obj, replacer?, spacer?, options?) {
|
|
23
|
+
if (typeof options === 'undefined') {
|
|
24
|
+
options = defaultOptions();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
decirc(obj, '', 0, [], undefined, 0, options);
|
|
28
|
+
var res;
|
|
29
|
+
try {
|
|
30
|
+
if (replacerStack.length === 0) {
|
|
31
|
+
res = JSON.stringify(obj, replacer, spacer);
|
|
32
|
+
} else {
|
|
33
|
+
res = JSON.stringify(obj, replaceGetterValues(replacer), spacer);
|
|
34
|
+
}
|
|
35
|
+
} catch (_) {
|
|
36
|
+
return JSON.stringify('[unable to serialize, circular reference is too complex to analyze]');
|
|
37
|
+
} finally {
|
|
38
|
+
while (arr.length !== 0) {
|
|
39
|
+
var part = arr.pop();
|
|
40
|
+
if (part.length === 4) {
|
|
41
|
+
Object.defineProperty(part[0], part[1], part[3]);
|
|
42
|
+
} else {
|
|
43
|
+
part[0][part[1]] = part[2];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return res;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function setReplace(replace, val, k, parent) {
|
|
51
|
+
var propertyDescriptor = Object.getOwnPropertyDescriptor(parent, k);
|
|
52
|
+
if (propertyDescriptor.get !== undefined) {
|
|
53
|
+
if (propertyDescriptor.configurable) {
|
|
54
|
+
Object.defineProperty(parent, k, { value: replace });
|
|
55
|
+
arr.push([parent, k, val, propertyDescriptor]);
|
|
56
|
+
} else {
|
|
57
|
+
replacerStack.push([val, k, replace]);
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
parent[k] = replace;
|
|
61
|
+
arr.push([parent, k, val]);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function decirc(val, k, edgeIndex, stack, parent, depth, options) {
|
|
66
|
+
depth += 1;
|
|
67
|
+
var i;
|
|
68
|
+
if (typeof val === 'object' && val !== null) {
|
|
69
|
+
for (i = 0; i < stack.length; i++) {
|
|
70
|
+
if (stack[i] === val) {
|
|
71
|
+
setReplace(CIRCULAR_REPLACE_NODE, val, k, parent);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (typeof options.depthLimit !== 'undefined' && depth > options.depthLimit) {
|
|
77
|
+
setReplace(LIMIT_REPLACE_NODE, val, k, parent);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (typeof options.edgesLimit !== 'undefined' && edgeIndex + 1 > options.edgesLimit) {
|
|
82
|
+
setReplace(LIMIT_REPLACE_NODE, val, k, parent);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
stack.push(val);
|
|
87
|
+
// Optimize for Arrays. Big arrays could kill the performance otherwise!
|
|
88
|
+
if (Array.isArray(val)) {
|
|
89
|
+
for (i = 0; i < val.length; i++) {
|
|
90
|
+
decirc(val[i], i, i, stack, val, depth, options);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
var keys = Object.keys(val);
|
|
94
|
+
for (i = 0; i < keys.length; i++) {
|
|
95
|
+
var key = keys[i];
|
|
96
|
+
decirc(val[key], key, i, stack, val, depth, options);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
stack.pop();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Stable-stringify
|
|
104
|
+
function compareFunction(a, b) {
|
|
105
|
+
if (a < b) {
|
|
106
|
+
return -1;
|
|
107
|
+
}
|
|
108
|
+
if (a > b) {
|
|
109
|
+
return 1;
|
|
110
|
+
}
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function deterministicStringify(obj, replacer, spacer, options) {
|
|
115
|
+
if (typeof options === 'undefined') {
|
|
116
|
+
options = defaultOptions();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
var tmp = deterministicDecirc(obj, '', 0, [], undefined, 0, options) || obj;
|
|
120
|
+
var res;
|
|
121
|
+
try {
|
|
122
|
+
if (replacerStack.length === 0) {
|
|
123
|
+
res = JSON.stringify(tmp, replacer, spacer);
|
|
124
|
+
} else {
|
|
125
|
+
res = JSON.stringify(tmp, replaceGetterValues(replacer), spacer);
|
|
126
|
+
}
|
|
127
|
+
} catch (_) {
|
|
128
|
+
return JSON.stringify('[unable to serialize, circular reference is too complex to analyze]');
|
|
129
|
+
} finally {
|
|
130
|
+
// Ensure that we restore the object as it was.
|
|
131
|
+
while (arr.length !== 0) {
|
|
132
|
+
var part = arr.pop();
|
|
133
|
+
if (part.length === 4) {
|
|
134
|
+
Object.defineProperty(part[0], part[1], part[3]);
|
|
135
|
+
} else {
|
|
136
|
+
part[0][part[1]] = part[2];
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return res;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function deterministicDecirc(val, k, edgeIndex, stack, parent, depth, options) {
|
|
144
|
+
depth += 1;
|
|
145
|
+
var i;
|
|
146
|
+
if (typeof val === 'object' && val !== null) {
|
|
147
|
+
for (i = 0; i < stack.length; i++) {
|
|
148
|
+
if (stack[i] === val) {
|
|
149
|
+
setReplace(CIRCULAR_REPLACE_NODE, val, k, parent);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
if (typeof val.toJSON === 'function') {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
} catch (_) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (typeof options.depthLimit !== 'undefined' && depth > options.depthLimit) {
|
|
162
|
+
setReplace(LIMIT_REPLACE_NODE, val, k, parent);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (typeof options.edgesLimit !== 'undefined' && edgeIndex + 1 > options.edgesLimit) {
|
|
167
|
+
setReplace(LIMIT_REPLACE_NODE, val, k, parent);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
stack.push(val);
|
|
172
|
+
// Optimize for Arrays. Big arrays could kill the performance otherwise!
|
|
173
|
+
if (Array.isArray(val)) {
|
|
174
|
+
for (i = 0; i < val.length; i++) {
|
|
175
|
+
deterministicDecirc(val[i], i, i, stack, val, depth, options);
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
// Create a temporary object in the required way
|
|
179
|
+
var tmp = {};
|
|
180
|
+
var keys = Object.keys(val).sort(compareFunction);
|
|
181
|
+
for (i = 0; i < keys.length; i++) {
|
|
182
|
+
var key = keys[i];
|
|
183
|
+
deterministicDecirc(val[key], key, i, stack, val, depth, options);
|
|
184
|
+
tmp[key] = val[key];
|
|
185
|
+
}
|
|
186
|
+
if (typeof parent !== 'undefined') {
|
|
187
|
+
arr.push([parent, k, val]);
|
|
188
|
+
parent[k] = tmp;
|
|
189
|
+
} else {
|
|
190
|
+
return tmp;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
stack.pop();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// wraps replacer function to handle values we couldn't replace
|
|
198
|
+
// and mark them as replaced value
|
|
199
|
+
function replaceGetterValues(replacer) {
|
|
200
|
+
replacer =
|
|
201
|
+
typeof replacer !== 'undefined'
|
|
202
|
+
? replacer
|
|
203
|
+
: function (k, v) {
|
|
204
|
+
return v;
|
|
205
|
+
};
|
|
206
|
+
return function (key, val) {
|
|
207
|
+
if (replacerStack.length > 0) {
|
|
208
|
+
for (var i = 0; i < replacerStack.length; i++) {
|
|
209
|
+
var part = replacerStack[i];
|
|
210
|
+
if (part[1] === key && part[0] === val) {
|
|
211
|
+
val = part[2];
|
|
212
|
+
replacerStack.splice(i, 1);
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return replacer.call(this, key, val);
|
|
218
|
+
};
|
|
219
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/* eslint-disable no-instanceof/no-instanceof */
|
|
3
|
+
import { load } from '@langchain/core/load';
|
|
4
|
+
import type { SerializerProtocol } from '@langchain/langgraph-checkpoint';
|
|
5
|
+
import { stringify } from '../graph/stringify.js';
|
|
6
|
+
|
|
7
|
+
function isLangChainSerializedObject(value: Record<string, unknown>) {
|
|
8
|
+
return value !== null && value.lc === 1 && value.type === 'constructor' && Array.isArray(value.id);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The replacer in stringify does not allow delegation to built-in LangChain
|
|
13
|
+
* serialization methods, and instead immediately calls `.toJSON()` and
|
|
14
|
+
* continues to stringify subfields.
|
|
15
|
+
*
|
|
16
|
+
* We therefore must start from the most nested elements in the input and
|
|
17
|
+
* deserialize upwards rather than top-down.
|
|
18
|
+
*/
|
|
19
|
+
async function _reviver(value: any): Promise<any> {
|
|
20
|
+
if (value && typeof value === 'object') {
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
const revivedArray = await Promise.all(value.map((item) => _reviver(item)));
|
|
23
|
+
return revivedArray;
|
|
24
|
+
} else {
|
|
25
|
+
const revivedObj: any = {};
|
|
26
|
+
for (const [k, v] of Object.entries(value)) {
|
|
27
|
+
revivedObj[k] = await _reviver(v);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (revivedObj.lc === 2 && revivedObj.type === 'undefined') {
|
|
31
|
+
return undefined;
|
|
32
|
+
} else if (revivedObj.lc === 2 && revivedObj.type === 'constructor' && Array.isArray(revivedObj.id)) {
|
|
33
|
+
try {
|
|
34
|
+
const constructorName = revivedObj.id[revivedObj.id.length - 1];
|
|
35
|
+
let constructor: any;
|
|
36
|
+
|
|
37
|
+
switch (constructorName) {
|
|
38
|
+
case 'Set':
|
|
39
|
+
constructor = Set;
|
|
40
|
+
break;
|
|
41
|
+
case 'Map':
|
|
42
|
+
constructor = Map;
|
|
43
|
+
break;
|
|
44
|
+
case 'RegExp':
|
|
45
|
+
constructor = RegExp;
|
|
46
|
+
break;
|
|
47
|
+
case 'Error':
|
|
48
|
+
constructor = Error;
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
return revivedObj;
|
|
52
|
+
}
|
|
53
|
+
if (revivedObj.method) {
|
|
54
|
+
return (constructor as any)[revivedObj.method](...(revivedObj.args || []));
|
|
55
|
+
} else {
|
|
56
|
+
return new (constructor as any)(...(revivedObj.args || []));
|
|
57
|
+
}
|
|
58
|
+
} catch (error) {
|
|
59
|
+
return revivedObj;
|
|
60
|
+
}
|
|
61
|
+
} else if (isLangChainSerializedObject(revivedObj)) {
|
|
62
|
+
return load(JSON.stringify(revivedObj));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return revivedObj;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function _encodeConstructorArgs(
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
73
|
+
constructor: Function,
|
|
74
|
+
method?: string,
|
|
75
|
+
args?: any[],
|
|
76
|
+
kwargs?: Record<string, any>,
|
|
77
|
+
): object {
|
|
78
|
+
return {
|
|
79
|
+
lc: 2,
|
|
80
|
+
type: 'constructor',
|
|
81
|
+
id: [constructor.name],
|
|
82
|
+
method: method ?? null,
|
|
83
|
+
args: args ?? [],
|
|
84
|
+
kwargs: kwargs ?? {},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function _default(obj: any): any {
|
|
89
|
+
if (obj === undefined) {
|
|
90
|
+
return {
|
|
91
|
+
lc: 2,
|
|
92
|
+
type: 'undefined',
|
|
93
|
+
};
|
|
94
|
+
} else if (obj instanceof Set || obj instanceof Map) {
|
|
95
|
+
return _encodeConstructorArgs(obj.constructor, undefined, [Array.from(obj)]);
|
|
96
|
+
} else if (obj instanceof RegExp) {
|
|
97
|
+
return _encodeConstructorArgs(RegExp, undefined, [obj.source, obj.flags]);
|
|
98
|
+
} else if (obj instanceof Error) {
|
|
99
|
+
return _encodeConstructorArgs(obj.constructor, undefined, [obj.message]);
|
|
100
|
+
// TODO: Remove special case
|
|
101
|
+
} else if (obj?.lg_name === 'Send') {
|
|
102
|
+
return {
|
|
103
|
+
node: obj.node,
|
|
104
|
+
args: obj.args,
|
|
105
|
+
};
|
|
106
|
+
} else {
|
|
107
|
+
return obj;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export class JsonPlusSerializer implements SerializerProtocol {
|
|
112
|
+
protected _dumps(obj: any): Uint8Array {
|
|
113
|
+
const encoder = new TextEncoder();
|
|
114
|
+
return encoder.encode(
|
|
115
|
+
stringify(obj, (_: string, value: any) => {
|
|
116
|
+
return _default(value);
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async dumpsTyped(obj: any): Promise<[string, Uint8Array]> {
|
|
122
|
+
if (obj instanceof Uint8Array) {
|
|
123
|
+
return ['bytes', obj];
|
|
124
|
+
} else {
|
|
125
|
+
return ['json', this._dumps(obj)];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
protected async _loads(data: string): Promise<any> {
|
|
130
|
+
const parsed = JSON.parse(data);
|
|
131
|
+
return _reviver(parsed);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async loadsTyped(type: string, data: Uint8Array | string): Promise<any> {
|
|
135
|
+
if (type === 'bytes') {
|
|
136
|
+
return typeof data === 'string' ? new TextEncoder().encode(data) : data;
|
|
137
|
+
} else if (type === 'json') {
|
|
138
|
+
return this._loads(typeof data === 'string' ? data : new TextDecoder().decode(data));
|
|
139
|
+
} else {
|
|
140
|
+
throw new Error(`Unknown serialization type: ${type}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export class EventMessage {
|
|
2
|
+
event: string;
|
|
3
|
+
data: unknown;
|
|
4
|
+
id?: string;
|
|
5
|
+
constructor(event: string, data?: unknown) {
|
|
6
|
+
this.event = event;
|
|
7
|
+
this.data = data;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class CancelEventMessage extends EventMessage {
|
|
12
|
+
constructor() {
|
|
13
|
+
super('__system_cancel__', 'user cancel this run');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class StreamEndEventMessage extends EventMessage {
|
|
18
|
+
constructor() {
|
|
19
|
+
super('__stream_end__');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class StreamErrorEventMessage extends EventMessage {
|
|
24
|
+
public constructor(error: Error) {
|
|
25
|
+
super('__stream_error__', {
|
|
26
|
+
error: error.name,
|
|
27
|
+
message: error.message,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|