@langgraph-js/sdk 1.12.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 +175 -68
- package/dist/LangGraphClient.js +50 -309
- 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 +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 +104 -344
- 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;
|
|
@@ -63,38 +46,188 @@ 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;
|
|
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;
|
|
94
116
|
stopController: AbortController | null;
|
|
95
117
|
/** 用于存储 subAgent 状态数据的键 */
|
|
96
118
|
subAgentsKey: string;
|
|
119
|
+
/** Message 处理器 */
|
|
120
|
+
private messageProcessor;
|
|
97
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
|
+
};
|
|
98
231
|
availableAssistants: Assistant[];
|
|
99
232
|
private listAssistants;
|
|
100
233
|
/**
|
|
@@ -108,43 +241,26 @@ export declare class LangGraphClient extends Client {
|
|
|
108
241
|
*/
|
|
109
242
|
createThread({ threadId, }?: {
|
|
110
243
|
threadId?: string;
|
|
111
|
-
}): Promise<Thread<
|
|
244
|
+
}): Promise<Thread<TStateType>>;
|
|
112
245
|
graphVisualize(): Promise<import("@langchain/langgraph-sdk").AssistantGraph>;
|
|
113
246
|
/**
|
|
114
247
|
* @zh 列出所有的 Thread。
|
|
115
248
|
* @en Lists all Threads.
|
|
116
249
|
*/
|
|
117
|
-
listThreads
|
|
250
|
+
listThreads(): Promise<Thread<TStateType>[]>;
|
|
251
|
+
deleteThread(threadId: string): Promise<void>;
|
|
118
252
|
/**
|
|
119
253
|
* @zh 从历史中恢复 Thread 数据。
|
|
120
254
|
* @en Resets the Thread data from history.
|
|
121
255
|
*/
|
|
122
|
-
resetThread(agent: string, threadId: string): Promise<Thread<
|
|
256
|
+
resetThread(agent: string, threadId: string): Promise<Thread<TStateType>>;
|
|
123
257
|
resetStream(): Promise<void>;
|
|
124
|
-
streamingMessage: RenderMessage[];
|
|
125
|
-
/** 图发过来的更新信息 */
|
|
126
|
-
graphMessages: RenderMessage[];
|
|
127
258
|
cloneMessage(message: Message): Message;
|
|
128
|
-
private updateStreamingMessage;
|
|
129
|
-
/** 将 graphMessages 和 streamingMessage 合并,并返回新的消息数组 */
|
|
130
|
-
private combineGraphMessagesWithStreamingMessages;
|
|
131
259
|
/**
|
|
132
260
|
* @zh 用于 UI 中的流式渲染中的消息。
|
|
133
261
|
* @en Messages used for streaming rendering in the UI.
|
|
134
262
|
*/
|
|
135
263
|
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
264
|
/**
|
|
149
265
|
* @zh 获取 Token 计数器信息。
|
|
150
266
|
* @en Gets the Token counter information.
|
|
@@ -154,12 +270,6 @@ export declare class LangGraphClient extends Client {
|
|
|
154
270
|
input_tokens: number;
|
|
155
271
|
output_tokens: number;
|
|
156
272
|
};
|
|
157
|
-
/**
|
|
158
|
-
* @zh 注册流式更新的回调函数。
|
|
159
|
-
* @en Registers a callback function for streaming updates.
|
|
160
|
-
*/
|
|
161
|
-
onStreamingUpdate(callback: StreamingUpdateCallback): () => void;
|
|
162
|
-
private emitStreamingUpdate;
|
|
163
273
|
/** 前端工具人机交互时,锁住面板 */
|
|
164
274
|
isFELocking(messages: RenderMessage[]): boolean | undefined;
|
|
165
275
|
graphState: any;
|
|
@@ -186,8 +296,6 @@ export declare class LangGraphClient extends Client {
|
|
|
186
296
|
id: string;
|
|
187
297
|
name: string;
|
|
188
298
|
};
|
|
189
|
-
/** 子图的数据需要通过 merge 的方式重新进行合并更新 */
|
|
190
|
-
private mergeSubGraphMessagesToStreamingMessages;
|
|
191
299
|
private runFETool;
|
|
192
300
|
private callFETool;
|
|
193
301
|
extraParams: Record<string, any>;
|
|
@@ -205,7 +313,7 @@ export declare class LangGraphClient extends Client {
|
|
|
205
313
|
* @zh 获取当前的 Thread。
|
|
206
314
|
* @en Gets the current Thread.
|
|
207
315
|
*/
|
|
208
|
-
getCurrentThread(): Thread<
|
|
316
|
+
getCurrentThread(): Thread<TStateType> | null;
|
|
209
317
|
/**
|
|
210
318
|
* @zh 获取当前的 Assistant。
|
|
211
319
|
* @en Gets the current Assistant.
|
|
@@ -217,4 +325,3 @@ export declare class LangGraphClient extends Client {
|
|
|
217
325
|
*/
|
|
218
326
|
reset(): Promise<void>;
|
|
219
327
|
}
|
|
220
|
-
export {};
|