@langgraph-js/sdk 1.11.0 → 2.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/dist/LangGraphClient.d.ts +178 -66
- package/dist/LangGraphClient.js +52 -260
- package/dist/MessageProcessor.d.ts +94 -0
- package/dist/MessageProcessor.js +324 -0
- package/dist/TestKit.d.ts +5 -2
- package/dist/client/LanggraphServer.d.ts +2 -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 +130 -0
- package/dist/types.js +1 -0
- package/dist/ui-store/createChatStore.d.ts +3 -3
- package/dist/ui-store/createChatStore.js +37 -21
- package/package.json +2 -1
- package/src/LangGraphClient.ts +107 -297
- package/src/MessageProcessor.ts +352 -0
- package/src/TestKit.ts +1 -1
- package/src/client/LanggraphServer.ts +6 -0
- package/src/index.ts +2 -0
- package/src/types.ts +166 -0
- package/src/ui-store/createChatStore.ts +40 -20
|
@@ -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;
|
|
@@ -35,6 +18,7 @@ export type RenderMessage = Message & {
|
|
|
35
18
|
};
|
|
36
19
|
}[];
|
|
37
20
|
};
|
|
21
|
+
sub_agent_messages?: RenderMessage[];
|
|
38
22
|
usage_metadata?: {
|
|
39
23
|
total_tokens: number;
|
|
40
24
|
input_tokens: number;
|
|
@@ -62,36 +46,188 @@ export type SendMessageOptions = {
|
|
|
62
46
|
export interface LangGraphClientConfig {
|
|
63
47
|
apiUrl?: string;
|
|
64
48
|
apiKey?: string;
|
|
65
|
-
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
|
+
};
|
|
66
68
|
timeoutMs?: number;
|
|
67
69
|
defaultHeaders?: Record<string, string | null | undefined>;
|
|
70
|
+
/** 自定义客户端实现,如果不提供则使用官方 Client */
|
|
71
|
+
client: ILangGraphClient;
|
|
68
72
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
+
};
|
|
78
106
|
}
|
|
79
|
-
type StreamingUpdateEvent = {
|
|
80
|
-
type: "message" | "value" | "update" | "error" | "thread" | "done" | "start";
|
|
81
|
-
data: any;
|
|
82
|
-
};
|
|
83
|
-
type StreamingUpdateCallback = (event: StreamingUpdateEvent) => void;
|
|
84
107
|
/**
|
|
85
108
|
* @zh LangGraphClient 类是与 LangGraph 后端交互的主要客户端。
|
|
86
109
|
* @en The LangGraphClient class is the main client for interacting with the LangGraph backend.
|
|
87
110
|
*/
|
|
88
|
-
export declare class LangGraphClient extends
|
|
111
|
+
export declare class LangGraphClient<TStateType = unknown, TUpdateType = TStateType> extends EventEmitter<LangGraphEvents> {
|
|
112
|
+
private client;
|
|
89
113
|
private currentAssistant;
|
|
90
114
|
private currentThread;
|
|
91
|
-
private streamingCallbacks;
|
|
92
115
|
tools: ToolManager;
|
|
93
116
|
stopController: AbortController | null;
|
|
117
|
+
/** 用于存储 subAgent 状态数据的键 */
|
|
118
|
+
subAgentsKey: string;
|
|
119
|
+
/** Message 处理器 */
|
|
120
|
+
private messageProcessor;
|
|
94
121
|
constructor(config: LangGraphClientConfig);
|
|
122
|
+
/** 代理 assistants 属性到内部 client */
|
|
123
|
+
get assistants(): {
|
|
124
|
+
search(query?: {
|
|
125
|
+
graphId?: string;
|
|
126
|
+
metadata?: import("@langchain/langgraph-sdk").Metadata;
|
|
127
|
+
limit?: number;
|
|
128
|
+
offset?: number;
|
|
129
|
+
sortBy?: import("./types.js").AssistantSortBy;
|
|
130
|
+
sortOrder?: import("./types.js").SortOrder;
|
|
131
|
+
}): Promise<Assistant[]>;
|
|
132
|
+
getGraph(assistantId: string, options?: {
|
|
133
|
+
xray?: boolean | number;
|
|
134
|
+
}): Promise<import("@langchain/langgraph-sdk").AssistantGraph>;
|
|
135
|
+
};
|
|
136
|
+
/** 代理 threads 属性到内部 client */
|
|
137
|
+
get threads(): {
|
|
138
|
+
create<ValuesType = TStateType>(payload?: {
|
|
139
|
+
metadata?: import("@langchain/langgraph-sdk").Metadata;
|
|
140
|
+
threadId?: string;
|
|
141
|
+
ifExists?: import("@langchain/langgraph-sdk").OnConflictBehavior;
|
|
142
|
+
graphId?: string;
|
|
143
|
+
supersteps?: Array<{
|
|
144
|
+
updates: Array<{
|
|
145
|
+
values: unknown;
|
|
146
|
+
command?: Command;
|
|
147
|
+
asNode: string;
|
|
148
|
+
}>;
|
|
149
|
+
}>;
|
|
150
|
+
}): Promise<Thread<ValuesType>>;
|
|
151
|
+
search<ValuesType = TStateType>(query?: {
|
|
152
|
+
metadata?: import("@langchain/langgraph-sdk").Metadata;
|
|
153
|
+
limit?: number;
|
|
154
|
+
offset?: number;
|
|
155
|
+
status?: import("@langchain/langgraph-sdk").ThreadStatus;
|
|
156
|
+
sortBy?: import("./types.js").ThreadSortBy;
|
|
157
|
+
sortOrder?: import("./types.js").SortOrder;
|
|
158
|
+
}): Promise<Thread<ValuesType>[]>;
|
|
159
|
+
get<ValuesType = TStateType>(threadId: string): Promise<Thread<ValuesType>>;
|
|
160
|
+
delete(threadId: string): Promise<void>;
|
|
161
|
+
};
|
|
162
|
+
/** 代理 runs 属性到内部 client */
|
|
163
|
+
get runs(): {
|
|
164
|
+
list(threadId: string, options?: {
|
|
165
|
+
limit?: number;
|
|
166
|
+
offset?: number;
|
|
167
|
+
status?: import("./types.js").RunStatus;
|
|
168
|
+
}): Promise<import("@langchain/langgraph-sdk").Run[]>;
|
|
169
|
+
stream<TStreamMode extends import("@langchain/langgraph-sdk").StreamMode | import("@langchain/langgraph-sdk").StreamMode[] = import("@langchain/langgraph-sdk").StreamMode, TSubgraphs extends boolean = false>(threadId: null, assistantId: string, payload?: {
|
|
170
|
+
input?: Record<string, unknown> | null;
|
|
171
|
+
metadata?: import("@langchain/langgraph-sdk").Metadata;
|
|
172
|
+
config?: import("@langchain/langgraph-sdk").Config;
|
|
173
|
+
checkpointId?: string;
|
|
174
|
+
checkpoint?: Omit<import("@langchain/langgraph-sdk").Checkpoint, "thread_id">;
|
|
175
|
+
checkpointDuring?: boolean;
|
|
176
|
+
interruptBefore?: "*" | string[];
|
|
177
|
+
interruptAfter?: "*" | string[];
|
|
178
|
+
signal?: AbortController["signal"];
|
|
179
|
+
webhook?: string;
|
|
180
|
+
onDisconnect?: import("./types.js").DisconnectMode;
|
|
181
|
+
afterSeconds?: number;
|
|
182
|
+
ifNotExists?: "create" | "reject";
|
|
183
|
+
command?: Command;
|
|
184
|
+
onRunCreated?: (params: {
|
|
185
|
+
run_id: string;
|
|
186
|
+
thread_id?: string;
|
|
187
|
+
}) => void;
|
|
188
|
+
streamMode?: TStreamMode | undefined;
|
|
189
|
+
streamSubgraphs?: TSubgraphs | undefined;
|
|
190
|
+
streamResumable?: boolean;
|
|
191
|
+
feedbackKeys?: string[];
|
|
192
|
+
} | undefined): import("./types.js").TypedAsyncGenerator<TStreamMode, TSubgraphs, TStateType, TUpdateType, unknown>;
|
|
193
|
+
stream<TStreamMode extends import("@langchain/langgraph-sdk").StreamMode | import("@langchain/langgraph-sdk").StreamMode[] = import("@langchain/langgraph-sdk").StreamMode, TSubgraphs extends boolean = false>(threadId: string, assistantId: string, payload?: {
|
|
194
|
+
input?: Record<string, unknown> | null;
|
|
195
|
+
metadata?: import("@langchain/langgraph-sdk").Metadata;
|
|
196
|
+
config?: import("@langchain/langgraph-sdk").Config;
|
|
197
|
+
checkpointId?: string;
|
|
198
|
+
checkpoint?: Omit<import("@langchain/langgraph-sdk").Checkpoint, "thread_id">;
|
|
199
|
+
checkpointDuring?: boolean;
|
|
200
|
+
interruptBefore?: "*" | string[];
|
|
201
|
+
interruptAfter?: "*" | string[];
|
|
202
|
+
multitaskStrategy?: import("./types.js").MultitaskStrategy;
|
|
203
|
+
onCompletion?: import("./types.js").OnCompletionBehavior;
|
|
204
|
+
signal?: AbortController["signal"];
|
|
205
|
+
webhook?: string;
|
|
206
|
+
onDisconnect?: import("./types.js").DisconnectMode;
|
|
207
|
+
afterSeconds?: number;
|
|
208
|
+
ifNotExists?: "create" | "reject";
|
|
209
|
+
command?: Command;
|
|
210
|
+
onRunCreated?: (params: {
|
|
211
|
+
run_id: string;
|
|
212
|
+
thread_id?: string;
|
|
213
|
+
}) => void;
|
|
214
|
+
streamMode?: TStreamMode | undefined;
|
|
215
|
+
streamSubgraphs?: TSubgraphs | undefined;
|
|
216
|
+
streamResumable?: boolean;
|
|
217
|
+
feedbackKeys?: string[];
|
|
218
|
+
} | undefined): import("./types.js").TypedAsyncGenerator<TStreamMode, TSubgraphs, TStateType, TUpdateType, unknown>;
|
|
219
|
+
joinStream(threadId: string | undefined | null, runId: string, options?: {
|
|
220
|
+
signal?: AbortSignal;
|
|
221
|
+
cancelOnDisconnect?: boolean;
|
|
222
|
+
lastEventId?: string;
|
|
223
|
+
streamMode?: import("@langchain/langgraph-sdk").StreamMode | import("@langchain/langgraph-sdk").StreamMode[];
|
|
224
|
+
} | AbortSignal): AsyncGenerator<{
|
|
225
|
+
id?: string;
|
|
226
|
+
event: import("@langchain/core/tracers/log_stream").StreamEvent;
|
|
227
|
+
data: any;
|
|
228
|
+
}>;
|
|
229
|
+
cancel(threadId: string, runId: string, wait?: boolean, action?: import("./types.js").CancelAction): Promise<void>;
|
|
230
|
+
};
|
|
95
231
|
availableAssistants: Assistant[];
|
|
96
232
|
private listAssistants;
|
|
97
233
|
/**
|
|
@@ -105,41 +241,26 @@ export declare class LangGraphClient extends Client {
|
|
|
105
241
|
*/
|
|
106
242
|
createThread({ threadId, }?: {
|
|
107
243
|
threadId?: string;
|
|
108
|
-
}): Promise<Thread<
|
|
244
|
+
}): Promise<Thread<TStateType>>;
|
|
109
245
|
graphVisualize(): Promise<import("@langchain/langgraph-sdk").AssistantGraph>;
|
|
110
246
|
/**
|
|
111
247
|
* @zh 列出所有的 Thread。
|
|
112
248
|
* @en Lists all Threads.
|
|
113
249
|
*/
|
|
114
|
-
listThreads
|
|
250
|
+
listThreads(): Promise<Thread<TStateType>[]>;
|
|
251
|
+
deleteThread(threadId: string): Promise<void>;
|
|
115
252
|
/**
|
|
116
253
|
* @zh 从历史中恢复 Thread 数据。
|
|
117
254
|
* @en Resets the Thread data from history.
|
|
118
255
|
*/
|
|
119
|
-
resetThread(agent: string, threadId: string): Promise<Thread<
|
|
256
|
+
resetThread(agent: string, threadId: string): Promise<Thread<TStateType>>;
|
|
120
257
|
resetStream(): Promise<void>;
|
|
121
|
-
streamingMessage: RenderMessage[];
|
|
122
|
-
/** 图发过来的更新信息 */
|
|
123
|
-
graphMessages: RenderMessage[];
|
|
124
258
|
cloneMessage(message: Message): Message;
|
|
125
|
-
private updateStreamingMessage;
|
|
126
|
-
/** 将 graphMessages 和 streamingMessage 合并,并返回新的消息数组 */
|
|
127
|
-
private combineGraphMessagesWithStreamingMessages;
|
|
128
259
|
/**
|
|
129
260
|
* @zh 用于 UI 中的流式渲染中的消息。
|
|
130
261
|
* @en Messages used for streaming rendering in the UI.
|
|
131
262
|
*/
|
|
132
263
|
get renderMessage(): RenderMessage[];
|
|
133
|
-
/**
|
|
134
|
-
* @zh 为消息附加额外的信息,如耗时、唯一 ID 等。
|
|
135
|
-
* @en Attaches additional information to messages, such as spend time, unique ID, etc.
|
|
136
|
-
*/
|
|
137
|
-
private attachInfoForMessage;
|
|
138
|
-
/**
|
|
139
|
-
* @zh 组合工具消息,将 AI 的工具调用和工具的执行结果关联起来。
|
|
140
|
-
* @en Composes tool messages, associating AI tool calls with tool execution results.
|
|
141
|
-
*/
|
|
142
|
-
private composeToolMessages;
|
|
143
264
|
/**
|
|
144
265
|
* @zh 获取 Token 计数器信息。
|
|
145
266
|
* @en Gets the Token counter information.
|
|
@@ -149,12 +270,6 @@ export declare class LangGraphClient extends Client {
|
|
|
149
270
|
input_tokens: number;
|
|
150
271
|
output_tokens: number;
|
|
151
272
|
};
|
|
152
|
-
/**
|
|
153
|
-
* @zh 注册流式更新的回调函数。
|
|
154
|
-
* @en Registers a callback function for streaming updates.
|
|
155
|
-
*/
|
|
156
|
-
onStreamingUpdate(callback: StreamingUpdateCallback): () => void;
|
|
157
|
-
private emitStreamingUpdate;
|
|
158
273
|
/** 前端工具人机交互时,锁住面板 */
|
|
159
274
|
isFELocking(messages: RenderMessage[]): boolean | undefined;
|
|
160
275
|
graphState: any;
|
|
@@ -181,8 +296,6 @@ export declare class LangGraphClient extends Client {
|
|
|
181
296
|
id: string;
|
|
182
297
|
name: string;
|
|
183
298
|
};
|
|
184
|
-
/** 子图的数据需要通过 merge 的方式重新进行合并更新 */
|
|
185
|
-
private mergeSubGraphMessagesToStreamingMessages;
|
|
186
299
|
private runFETool;
|
|
187
300
|
private callFETool;
|
|
188
301
|
extraParams: Record<string, any>;
|
|
@@ -200,7 +313,7 @@ export declare class LangGraphClient extends Client {
|
|
|
200
313
|
* @zh 获取当前的 Thread。
|
|
201
314
|
* @en Gets the current Thread.
|
|
202
315
|
*/
|
|
203
|
-
getCurrentThread(): Thread<
|
|
316
|
+
getCurrentThread(): Thread<TStateType> | null;
|
|
204
317
|
/**
|
|
205
318
|
* @zh 获取当前的 Assistant。
|
|
206
319
|
* @en Gets the current Assistant.
|
|
@@ -212,4 +325,3 @@ export declare class LangGraphClient extends Client {
|
|
|
212
325
|
*/
|
|
213
326
|
reset(): Promise<void>;
|
|
214
327
|
}
|
|
215
|
-
export {};
|