@langgraph-js/sdk 1.12.0 → 2.0.1
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/dist/LangGraphClient.d.ts +158 -73
- package/dist/LangGraphClient.js +52 -311
- package/dist/MessageProcessor.d.ts +94 -0
- package/dist/MessageProcessor.js +324 -0
- package/dist/TestKit.d.ts +2 -2
- package/dist/client/LanggraphServer.d.ts +3 -0
- package/dist/client/LanggraphServer.js +4 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.js +1 -0
- package/dist/ui-store/createChatStore.d.ts +4 -4
- package/dist/ui-store/createChatStore.js +37 -21
- package/package.json +2 -1
- package/src/LangGraphClient.ts +108 -347
- package/src/MessageProcessor.ts +352 -0
- package/src/TestKit.ts +1 -1
- package/src/client/LanggraphServer.ts +7 -0
- package/src/index.ts +2 -0
- package/src/types.ts +129 -0
- package/src/ui-store/createChatStore.ts +41 -21
|
@@ -1,25 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Thread, Message, Assistant, Command } from "@langchain/langgraph-sdk";
|
|
2
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
3
|
import { ToolManager } from "./ToolManager.js";
|
|
3
4
|
import { CallToolResult } from "./tool/createTool.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* The maximum number of concurrent calls that can be made.
|
|
7
|
-
* Defaults to `Infinity`, which means no limit.
|
|
8
|
-
*/
|
|
9
|
-
maxConcurrency?: number;
|
|
10
|
-
/**
|
|
11
|
-
* The maximum number of retries that can be made for a single call,
|
|
12
|
-
* with an exponential backoff between each attempt. Defaults to 6.
|
|
13
|
-
*/
|
|
14
|
-
maxRetries?: number;
|
|
15
|
-
onFailedResponseHook?: any;
|
|
16
|
-
/**
|
|
17
|
-
* Specify a custom fetch implementation.
|
|
18
|
-
*
|
|
19
|
-
* By default we expect the `fetch` is available in the global scope.
|
|
20
|
-
*/
|
|
21
|
-
fetch?: typeof fetch | ((...args: any[]) => any);
|
|
22
|
-
}
|
|
5
|
+
import { ILangGraphClient } from "./types.js";
|
|
23
6
|
export type RenderMessage = Message & {
|
|
24
7
|
/** 对于 AIMessage 来说是节点名称,对于工具节点来说是工具名称 */
|
|
25
8
|
name?: string;
|
|
@@ -63,39 +46,171 @@ export type SendMessageOptions = {
|
|
|
63
46
|
export interface LangGraphClientConfig {
|
|
64
47
|
apiUrl?: string;
|
|
65
48
|
apiKey?: string;
|
|
66
|
-
callerOptions?:
|
|
49
|
+
callerOptions?: {
|
|
50
|
+
/**
|
|
51
|
+
* The maximum number of concurrent calls that can be made.
|
|
52
|
+
* Defaults to `Infinity`, which means no limit.
|
|
53
|
+
*/
|
|
54
|
+
maxConcurrency?: number;
|
|
55
|
+
/**
|
|
56
|
+
* The maximum number of retries that can be made for a single call,
|
|
57
|
+
* with an exponential backoff between each attempt. Defaults to 6.
|
|
58
|
+
*/
|
|
59
|
+
maxRetries?: number;
|
|
60
|
+
onFailedResponseHook?: any;
|
|
61
|
+
/**
|
|
62
|
+
* Specify a custom fetch implementation.
|
|
63
|
+
*
|
|
64
|
+
* By default we expect the `fetch` is available in the global scope.
|
|
65
|
+
*/
|
|
66
|
+
fetch?: typeof fetch | ((...args: any[]) => any);
|
|
67
|
+
};
|
|
67
68
|
timeoutMs?: number;
|
|
68
69
|
defaultHeaders?: Record<string, string | null | undefined>;
|
|
70
|
+
/** 自定义客户端实现,如果不提供则使用官方 Client */
|
|
71
|
+
client: ILangGraphClient<any, any>;
|
|
69
72
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
export interface LangGraphEvents {
|
|
74
|
+
/** 流开始事件 */
|
|
75
|
+
start: {
|
|
76
|
+
event: "start";
|
|
77
|
+
};
|
|
78
|
+
/** 消息部分更新事件 */
|
|
79
|
+
message: {
|
|
80
|
+
event: "messages/partial";
|
|
81
|
+
data: Message[];
|
|
82
|
+
};
|
|
83
|
+
/** 值更新事件 */
|
|
84
|
+
value: {
|
|
85
|
+
event: "messages/partial" | "values";
|
|
86
|
+
data: {
|
|
87
|
+
messages?: Message[];
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
/** 错误事件 */
|
|
91
|
+
error: {
|
|
92
|
+
event: "error";
|
|
93
|
+
data: any;
|
|
94
|
+
};
|
|
95
|
+
/** Thread 创建事件 */
|
|
96
|
+
thread: {
|
|
97
|
+
event: "thread/create";
|
|
98
|
+
data: {
|
|
99
|
+
thread: Thread;
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
/** 流完成事件 */
|
|
103
|
+
done: {
|
|
104
|
+
event: "done";
|
|
105
|
+
};
|
|
79
106
|
}
|
|
80
|
-
type StreamingUpdateEvent = {
|
|
81
|
-
type: "message" | "value" | "update" | "error" | "thread" | "done" | "start";
|
|
82
|
-
data: any;
|
|
83
|
-
};
|
|
84
|
-
type StreamingUpdateCallback = (event: StreamingUpdateEvent) => void;
|
|
85
107
|
/**
|
|
86
108
|
* @zh LangGraphClient 类是与 LangGraph 后端交互的主要客户端。
|
|
87
109
|
* @en The LangGraphClient class is the main client for interacting with the LangGraph backend.
|
|
88
110
|
*/
|
|
89
|
-
export declare class LangGraphClient extends
|
|
111
|
+
export declare class LangGraphClient<TStateType = unknown, TUpdateType = TStateType> extends EventEmitter<LangGraphEvents> {
|
|
112
|
+
private client;
|
|
90
113
|
private currentAssistant;
|
|
91
114
|
private currentThread;
|
|
92
|
-
private streamingCallbacks;
|
|
93
115
|
tools: ToolManager;
|
|
116
|
+
availableAssistants: Assistant[];
|
|
117
|
+
graphState: any;
|
|
118
|
+
currentRun?: {
|
|
119
|
+
run_id: string;
|
|
120
|
+
};
|
|
94
121
|
stopController: AbortController | null;
|
|
95
122
|
/** 用于存储 subAgent 状态数据的键 */
|
|
96
123
|
subAgentsKey: string;
|
|
124
|
+
/** Message 处理器 */
|
|
125
|
+
private messageProcessor;
|
|
97
126
|
constructor(config: LangGraphClientConfig);
|
|
98
|
-
|
|
127
|
+
/** 代理 assistants 属性到内部 client */
|
|
128
|
+
get assistants(): {
|
|
129
|
+
search(query?: {
|
|
130
|
+
graphId?: string;
|
|
131
|
+
metadata?: import("@langchain/langgraph-sdk").Metadata;
|
|
132
|
+
limit?: number;
|
|
133
|
+
offset?: number;
|
|
134
|
+
sortBy?: import("./types.js").AssistantSortBy;
|
|
135
|
+
sortOrder?: import("./types.js").SortOrder;
|
|
136
|
+
}): Promise<Assistant[]>;
|
|
137
|
+
getGraph(assistantId: string, options?: {
|
|
138
|
+
xray?: boolean | number;
|
|
139
|
+
}): Promise<import("@langchain/langgraph-sdk").AssistantGraph>;
|
|
140
|
+
};
|
|
141
|
+
/** 代理 threads 属性到内部 client */
|
|
142
|
+
get threads(): {
|
|
143
|
+
create(payload?: {
|
|
144
|
+
metadata?: import("@langchain/langgraph-sdk").Metadata;
|
|
145
|
+
threadId?: string;
|
|
146
|
+
ifExists?: import("@langchain/langgraph-sdk").OnConflictBehavior;
|
|
147
|
+
graphId?: string;
|
|
148
|
+
supersteps?: Array<{
|
|
149
|
+
updates: Array<{
|
|
150
|
+
values: unknown;
|
|
151
|
+
command? /** 值更新事件 */: Command;
|
|
152
|
+
asNode: string;
|
|
153
|
+
}>;
|
|
154
|
+
}>;
|
|
155
|
+
}): Promise<Thread<TStateType>>;
|
|
156
|
+
search(query?: {
|
|
157
|
+
metadata
|
|
158
|
+
/** Thread 创建事件 */
|
|
159
|
+
? /** Thread 创建事件 */: import("@langchain/langgraph-sdk").Metadata;
|
|
160
|
+
limit?: number;
|
|
161
|
+
offset?: number;
|
|
162
|
+
status?: import("@langchain/langgraph-sdk").ThreadStatus;
|
|
163
|
+
sortBy?: import("./types.js").ThreadSortBy;
|
|
164
|
+
sortOrder?: import("./types.js").SortOrder;
|
|
165
|
+
}): Promise<Thread<TStateType>[]>;
|
|
166
|
+
get(threadId: string): Promise<Thread<TStateType>>;
|
|
167
|
+
delete(threadId: string): Promise<void>;
|
|
168
|
+
};
|
|
169
|
+
/** 代理 runs 属性到内部 client */
|
|
170
|
+
get runs(): {
|
|
171
|
+
list(threadId: string, options?: {
|
|
172
|
+
limit?: number;
|
|
173
|
+
offset?: number;
|
|
174
|
+
status?: import("./types.js").RunStatus;
|
|
175
|
+
}): Promise<import("@langchain/langgraph-sdk").Run[]>;
|
|
176
|
+
stream<TSubgraphs extends boolean = false>(threadId: string, assistantId: string, payload?: {
|
|
177
|
+
input?: Record<string, unknown> | null;
|
|
178
|
+
metadata?: import("@langchain/langgraph-sdk").Metadata;
|
|
179
|
+
config?: import("@langchain/langgraph-sdk").Config;
|
|
180
|
+
checkpointId?: string;
|
|
181
|
+
checkpoint?: Omit<import("@langchain/langgraph-sdk").Checkpoint, "thread_id">;
|
|
182
|
+
checkpointDuring?: boolean;
|
|
183
|
+
interruptBefore?: "*" | string[];
|
|
184
|
+
interruptAfter?: "*" | string[];
|
|
185
|
+
multitaskStrategy?: import("./types.js").MultitaskStrategy;
|
|
186
|
+
onCompletion?: import("./types.js").OnCompletionBehavior;
|
|
187
|
+
signal?: AbortController["signal"];
|
|
188
|
+
webhook?: string;
|
|
189
|
+
onDisconnect?: import("./types.js").DisconnectMode;
|
|
190
|
+
afterSeconds?: number;
|
|
191
|
+
ifNotExists?: "create" | "reject";
|
|
192
|
+
command?: Command;
|
|
193
|
+
onRunCreated?: (params: {
|
|
194
|
+
run_id: string;
|
|
195
|
+
thread_id?: string;
|
|
196
|
+
}) => void;
|
|
197
|
+
streamMode?: import("@langchain/langgraph-sdk").StreamMode[];
|
|
198
|
+
streamSubgraphs?: TSubgraphs | undefined;
|
|
199
|
+
streamResumable?: boolean;
|
|
200
|
+
feedbackKeys?: string[];
|
|
201
|
+
} | undefined): import("./types.js").TypedAsyncGenerator<TSubgraphs, TStateType, TUpdateType>;
|
|
202
|
+
joinStream(threadId: string, runId: string, options?: {
|
|
203
|
+
signal?: AbortSignal;
|
|
204
|
+
cancelOnDisconnect?: boolean;
|
|
205
|
+
lastEventId?: string;
|
|
206
|
+
streamMode?: import("@langchain/langgraph-sdk").StreamMode | import("@langchain/langgraph-sdk").StreamMode[];
|
|
207
|
+
} | AbortSignal): AsyncGenerator<{
|
|
208
|
+
id?: string;
|
|
209
|
+
event: import("@langchain/core/tracers/log_stream").StreamEvent;
|
|
210
|
+
data: any;
|
|
211
|
+
}>;
|
|
212
|
+
cancel(threadId: string, runId: string, wait?: boolean, action?: import("./types.js").CancelAction): Promise<void>;
|
|
213
|
+
};
|
|
99
214
|
private listAssistants;
|
|
100
215
|
/**
|
|
101
216
|
* @zh 初始化 Assistant。
|
|
@@ -108,43 +223,26 @@ export declare class LangGraphClient extends Client {
|
|
|
108
223
|
*/
|
|
109
224
|
createThread({ threadId, }?: {
|
|
110
225
|
threadId?: string;
|
|
111
|
-
}): Promise<Thread<
|
|
226
|
+
}): Promise<Thread<TStateType>>;
|
|
112
227
|
graphVisualize(): Promise<import("@langchain/langgraph-sdk").AssistantGraph>;
|
|
113
228
|
/**
|
|
114
229
|
* @zh 列出所有的 Thread。
|
|
115
230
|
* @en Lists all Threads.
|
|
116
231
|
*/
|
|
117
|
-
listThreads
|
|
232
|
+
listThreads(): Promise<Thread<TStateType>[]>;
|
|
233
|
+
deleteThread(threadId: string): Promise<void>;
|
|
118
234
|
/**
|
|
119
235
|
* @zh 从历史中恢复 Thread 数据。
|
|
120
236
|
* @en Resets the Thread data from history.
|
|
121
237
|
*/
|
|
122
|
-
resetThread(agent: string, threadId: string): Promise<Thread<
|
|
238
|
+
resetThread(agent: string, threadId: string): Promise<Thread<TStateType>>;
|
|
123
239
|
resetStream(): Promise<void>;
|
|
124
|
-
streamingMessage: RenderMessage[];
|
|
125
|
-
/** 图发过来的更新信息 */
|
|
126
|
-
graphMessages: RenderMessage[];
|
|
127
240
|
cloneMessage(message: Message): Message;
|
|
128
|
-
private updateStreamingMessage;
|
|
129
|
-
/** 将 graphMessages 和 streamingMessage 合并,并返回新的消息数组 */
|
|
130
|
-
private combineGraphMessagesWithStreamingMessages;
|
|
131
241
|
/**
|
|
132
242
|
* @zh 用于 UI 中的流式渲染中的消息。
|
|
133
243
|
* @en Messages used for streaming rendering in the UI.
|
|
134
244
|
*/
|
|
135
245
|
get renderMessage(): RenderMessage[];
|
|
136
|
-
/** 转换 subAgent 消息为工具的子消息 */
|
|
137
|
-
private convertSubAgentMessages;
|
|
138
|
-
/**
|
|
139
|
-
* @zh 为消息附加额外的信息,如耗时、唯一 ID 等。
|
|
140
|
-
* @en Attaches additional information to messages, such as spend time, unique ID, etc.
|
|
141
|
-
*/
|
|
142
|
-
private attachInfoForMessage;
|
|
143
|
-
/**
|
|
144
|
-
* @zh 组合工具消息,将 AI 的工具调用和工具的执行结果关联起来。
|
|
145
|
-
* @en Composes tool messages, associating AI tool calls with tool execution results.
|
|
146
|
-
*/
|
|
147
|
-
private composeToolMessages;
|
|
148
246
|
/**
|
|
149
247
|
* @zh 获取 Token 计数器信息。
|
|
150
248
|
* @en Gets the Token counter information.
|
|
@@ -154,18 +252,8 @@ export declare class LangGraphClient extends Client {
|
|
|
154
252
|
input_tokens: number;
|
|
155
253
|
output_tokens: number;
|
|
156
254
|
};
|
|
157
|
-
/**
|
|
158
|
-
* @zh 注册流式更新的回调函数。
|
|
159
|
-
* @en Registers a callback function for streaming updates.
|
|
160
|
-
*/
|
|
161
|
-
onStreamingUpdate(callback: StreamingUpdateCallback): () => void;
|
|
162
|
-
private emitStreamingUpdate;
|
|
163
255
|
/** 前端工具人机交互时,锁住面板 */
|
|
164
256
|
isFELocking(messages: RenderMessage[]): boolean | undefined;
|
|
165
|
-
graphState: any;
|
|
166
|
-
currentRun?: {
|
|
167
|
-
run_id: string;
|
|
168
|
-
};
|
|
169
257
|
/**
|
|
170
258
|
* @zh 取消当前的 Run。
|
|
171
259
|
* @en Cancels the current Run.
|
|
@@ -186,8 +274,6 @@ export declare class LangGraphClient extends Client {
|
|
|
186
274
|
id: string;
|
|
187
275
|
name: string;
|
|
188
276
|
};
|
|
189
|
-
/** 子图的数据需要通过 merge 的方式重新进行合并更新 */
|
|
190
|
-
private mergeSubGraphMessagesToStreamingMessages;
|
|
191
277
|
private runFETool;
|
|
192
278
|
private callFETool;
|
|
193
279
|
extraParams: Record<string, any>;
|
|
@@ -205,7 +291,7 @@ export declare class LangGraphClient extends Client {
|
|
|
205
291
|
* @zh 获取当前的 Thread。
|
|
206
292
|
* @en Gets the current Thread.
|
|
207
293
|
*/
|
|
208
|
-
getCurrentThread(): Thread<
|
|
294
|
+
getCurrentThread(): Thread<TStateType> | null;
|
|
209
295
|
/**
|
|
210
296
|
* @zh 获取当前的 Assistant。
|
|
211
297
|
* @en Gets the current Assistant.
|
|
@@ -217,4 +303,3 @@ export declare class LangGraphClient extends Client {
|
|
|
217
303
|
*/
|
|
218
304
|
reset(): Promise<void>;
|
|
219
305
|
}
|
|
220
|
-
export {};
|