@langgraph-js/sdk 1.10.4 → 1.11.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.
|
@@ -57,6 +57,7 @@ export type SendMessageOptions = {
|
|
|
57
57
|
streamResponse?: any;
|
|
58
58
|
};
|
|
59
59
|
command?: Command;
|
|
60
|
+
joinRunId?: string;
|
|
60
61
|
};
|
|
61
62
|
export interface LangGraphClientConfig {
|
|
62
63
|
apiUrl?: string;
|
|
@@ -115,7 +116,8 @@ export declare class LangGraphClient extends Client {
|
|
|
115
116
|
* @zh 从历史中恢复 Thread 数据。
|
|
116
117
|
* @en Resets the Thread data from history.
|
|
117
118
|
*/
|
|
118
|
-
resetThread(agent: string, threadId: string): Promise<
|
|
119
|
+
resetThread(agent: string, threadId: string): Promise<Thread<import("@langchain/langgraph-sdk").DefaultValues>>;
|
|
120
|
+
resetStream(): Promise<void>;
|
|
119
121
|
streamingMessage: RenderMessage[];
|
|
120
122
|
/** 图发过来的更新信息 */
|
|
121
123
|
graphMessages: RenderMessage[];
|
|
@@ -168,7 +170,7 @@ export declare class LangGraphClient extends Client {
|
|
|
168
170
|
* @zh 发送消息到 LangGraph 后端。
|
|
169
171
|
* @en Sends a message to the LangGraph backend.
|
|
170
172
|
*/
|
|
171
|
-
sendMessage(input: string | Message[], { extraParams, _debug, command }?: SendMessageOptions): Promise<any[]>;
|
|
173
|
+
sendMessage(input: string | Message[], { joinRunId, extraParams, _debug, command }?: SendMessageOptions): Promise<any[]>;
|
|
172
174
|
/** 当前子图位置,但是依赖 stream,不太适合稳定使用*/
|
|
173
175
|
private graphPosition;
|
|
174
176
|
getGraphPosition(): {
|
package/dist/LangGraphClient.js
CHANGED
|
@@ -112,10 +112,11 @@ export class LangGraphClient extends Client {
|
|
|
112
112
|
* @en Resets the Thread data from history.
|
|
113
113
|
*/
|
|
114
114
|
async resetThread(agent, threadId) {
|
|
115
|
+
var _a;
|
|
115
116
|
await this.initAssistant(agent);
|
|
116
117
|
this.currentThread = await this.threads.get(threadId);
|
|
117
118
|
this.graphState = this.currentThread.values;
|
|
118
|
-
this.graphMessages = this.graphState.messages;
|
|
119
|
+
this.graphMessages = ((_a = this.graphState) === null || _a === void 0 ? void 0 : _a.messages) || [];
|
|
119
120
|
this.emitStreamingUpdate({
|
|
120
121
|
type: "value",
|
|
121
122
|
data: {
|
|
@@ -125,6 +126,15 @@ export class LangGraphClient extends Client {
|
|
|
125
126
|
},
|
|
126
127
|
},
|
|
127
128
|
});
|
|
129
|
+
return this.currentThread;
|
|
130
|
+
}
|
|
131
|
+
// 从历史中恢复时,应该恢复流式状态
|
|
132
|
+
async resetStream() {
|
|
133
|
+
const runs = await this.runs.list(this.currentThread.thread_id);
|
|
134
|
+
const runningRun = runs === null || runs === void 0 ? void 0 : runs.find((run) => run.status === "running" || run.status === "pending");
|
|
135
|
+
if (runningRun) {
|
|
136
|
+
await this.sendMessage([], { joinRunId: runningRun.run_id });
|
|
137
|
+
}
|
|
128
138
|
}
|
|
129
139
|
cloneMessage(message) {
|
|
130
140
|
return JSON.parse(JSON.stringify(message));
|
|
@@ -352,7 +362,7 @@ export class LangGraphClient extends Client {
|
|
|
352
362
|
* @zh 发送消息到 LangGraph 后端。
|
|
353
363
|
* @en Sends a message to the LangGraph backend.
|
|
354
364
|
*/
|
|
355
|
-
async sendMessage(input, { extraParams, _debug, command } = {}) {
|
|
365
|
+
async sendMessage(input, { joinRunId, extraParams, _debug, command } = {}) {
|
|
356
366
|
var _a;
|
|
357
367
|
if (!this.currentAssistant) {
|
|
358
368
|
throw new Error("Thread or Assistant not initialized");
|
|
@@ -377,8 +387,14 @@ export class LangGraphClient extends Client {
|
|
|
377
387
|
content: input,
|
|
378
388
|
},
|
|
379
389
|
];
|
|
380
|
-
const
|
|
381
|
-
|
|
390
|
+
const createStreamResponse = async () => {
|
|
391
|
+
if (_debug === null || _debug === void 0 ? void 0 : _debug.streamResponse) {
|
|
392
|
+
return _debug.streamResponse;
|
|
393
|
+
}
|
|
394
|
+
if (joinRunId) {
|
|
395
|
+
return this.runs.joinStream(this.currentThread.thread_id, joinRunId);
|
|
396
|
+
}
|
|
397
|
+
return this.runs.stream(this.currentThread.thread_id, this.currentAssistant.assistant_id, {
|
|
382
398
|
input: {
|
|
383
399
|
...this.graphState,
|
|
384
400
|
...this.extraParams,
|
|
@@ -390,6 +406,8 @@ export class LangGraphClient extends Client {
|
|
|
390
406
|
streamSubgraphs: true,
|
|
391
407
|
command,
|
|
392
408
|
});
|
|
409
|
+
};
|
|
410
|
+
const streamResponse = await createStreamResponse();
|
|
393
411
|
const streamRecord = [];
|
|
394
412
|
this.emitStreamingUpdate({
|
|
395
413
|
type: "start",
|
|
@@ -84,7 +84,7 @@ export declare const createChatStore: (initClientName: string, config: LangGraph
|
|
|
84
84
|
*/
|
|
85
85
|
toHistoryChat(thread: Thread<{
|
|
86
86
|
messages: Message[];
|
|
87
|
-
}>):
|
|
87
|
+
}>): Promise<Thread<import("@langchain/langgraph-sdk").DefaultValues> | undefined>;
|
|
88
88
|
/**
|
|
89
89
|
* @zh 删除指定的历史聊天会话。
|
|
90
90
|
* @en Deletes the specified historical chat session.
|
|
@@ -283,11 +283,15 @@ export const createChatStore = (initClientName, config, context = {}) => {
|
|
|
283
283
|
* @zh 切换到指定的历史聊天会话。
|
|
284
284
|
* @en Switches to the specified historical chat session.
|
|
285
285
|
*/
|
|
286
|
-
toHistoryChat(thread) {
|
|
287
|
-
var _a, _b;
|
|
286
|
+
async toHistoryChat(thread) {
|
|
287
|
+
var _a, _b, _c;
|
|
288
288
|
inChatError.set(null);
|
|
289
289
|
loading.set(false);
|
|
290
|
-
(_a = client.get()) === null || _a === void 0 ? void 0 : _a.resetThread((_b = thread.metadata) === null || _b === void 0 ? void 0 : _b.graph_id, thread.thread_id);
|
|
290
|
+
const nowThread = await ((_a = client.get()) === null || _a === void 0 ? void 0 : _a.resetThread((_b = thread.metadata) === null || _b === void 0 ? void 0 : _b.graph_id, thread.thread_id));
|
|
291
|
+
if (nowThread) {
|
|
292
|
+
(_c = client.get()) === null || _c === void 0 ? void 0 : _c.resetStream();
|
|
293
|
+
}
|
|
294
|
+
return nowThread;
|
|
291
295
|
},
|
|
292
296
|
/**
|
|
293
297
|
* @zh 删除指定的历史聊天会话。
|
package/package.json
CHANGED
package/src/LangGraphClient.ts
CHANGED
|
@@ -55,6 +55,7 @@ export type SendMessageOptions = {
|
|
|
55
55
|
extraParams?: Record<string, any>;
|
|
56
56
|
_debug?: { streamResponse?: any };
|
|
57
57
|
command?: Command;
|
|
58
|
+
joinRunId?: string;
|
|
58
59
|
};
|
|
59
60
|
export interface LangGraphClientConfig {
|
|
60
61
|
apiUrl?: string;
|
|
@@ -182,7 +183,7 @@ export class LangGraphClient extends Client {
|
|
|
182
183
|
await this.initAssistant(agent);
|
|
183
184
|
this.currentThread = await this.threads.get(threadId);
|
|
184
185
|
this.graphState = this.currentThread.values;
|
|
185
|
-
this.graphMessages = this.graphState
|
|
186
|
+
this.graphMessages = this.graphState?.messages || [];
|
|
186
187
|
this.emitStreamingUpdate({
|
|
187
188
|
type: "value",
|
|
188
189
|
data: {
|
|
@@ -192,6 +193,15 @@ export class LangGraphClient extends Client {
|
|
|
192
193
|
},
|
|
193
194
|
},
|
|
194
195
|
});
|
|
196
|
+
return this.currentThread;
|
|
197
|
+
}
|
|
198
|
+
// 从历史中恢复时,应该恢复流式状态
|
|
199
|
+
async resetStream() {
|
|
200
|
+
const runs = await this.runs.list(this.currentThread!.thread_id);
|
|
201
|
+
const runningRun = runs?.find((run) => run.status === "running" || run.status === "pending");
|
|
202
|
+
if (runningRun) {
|
|
203
|
+
await this.sendMessage([], { joinRunId: runningRun.run_id });
|
|
204
|
+
}
|
|
195
205
|
}
|
|
196
206
|
|
|
197
207
|
streamingMessage: RenderMessage[] = [];
|
|
@@ -432,7 +442,7 @@ export class LangGraphClient extends Client {
|
|
|
432
442
|
* @zh 发送消息到 LangGraph 后端。
|
|
433
443
|
* @en Sends a message to the LangGraph backend.
|
|
434
444
|
*/
|
|
435
|
-
async sendMessage(input: string | Message[], { extraParams, _debug, command }: SendMessageOptions = {}) {
|
|
445
|
+
async sendMessage(input: string | Message[], { joinRunId, extraParams, _debug, command }: SendMessageOptions = {}) {
|
|
436
446
|
if (!this.currentAssistant) {
|
|
437
447
|
throw new Error("Thread or Assistant not initialized");
|
|
438
448
|
}
|
|
@@ -457,20 +467,29 @@ export class LangGraphClient extends Client {
|
|
|
457
467
|
content: input,
|
|
458
468
|
} as HumanMessage,
|
|
459
469
|
];
|
|
460
|
-
const
|
|
461
|
-
_debug?.streamResponse
|
|
462
|
-
|
|
470
|
+
const createStreamResponse = async () => {
|
|
471
|
+
if (_debug?.streamResponse) {
|
|
472
|
+
return _debug.streamResponse;
|
|
473
|
+
}
|
|
474
|
+
if (joinRunId) {
|
|
475
|
+
return this.runs.joinStream(this.currentThread!.thread_id, joinRunId);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return this.runs.stream(this.currentThread!.thread_id, this.currentAssistant!.assistant_id, {
|
|
463
479
|
input: {
|
|
464
480
|
...this.graphState,
|
|
465
481
|
...this.extraParams,
|
|
466
482
|
...(extraParams || {}),
|
|
467
483
|
messages: messagesToSend,
|
|
468
|
-
fe_tools: await this.tools.toJSON(this.currentAssistant
|
|
484
|
+
fe_tools: await this.tools.toJSON(this.currentAssistant!.graph_id),
|
|
469
485
|
},
|
|
470
486
|
streamMode: ["messages", "values"],
|
|
471
487
|
streamSubgraphs: true,
|
|
472
488
|
command,
|
|
473
489
|
});
|
|
490
|
+
};
|
|
491
|
+
const streamResponse = await createStreamResponse();
|
|
492
|
+
|
|
474
493
|
const streamRecord: any[] = [];
|
|
475
494
|
this.emitStreamingUpdate({
|
|
476
495
|
type: "start",
|
|
@@ -289,14 +289,18 @@ export const createChatStore = (
|
|
|
289
289
|
* @zh 切换到指定的历史聊天会话。
|
|
290
290
|
* @en Switches to the specified historical chat session.
|
|
291
291
|
*/
|
|
292
|
-
toHistoryChat(
|
|
292
|
+
async toHistoryChat(
|
|
293
293
|
thread: Thread<{
|
|
294
294
|
messages: Message[];
|
|
295
295
|
}>
|
|
296
296
|
) {
|
|
297
297
|
inChatError.set(null);
|
|
298
298
|
loading.set(false);
|
|
299
|
-
client.get()?.resetThread(thread.metadata?.graph_id as string, thread.thread_id);
|
|
299
|
+
const nowThread = await client.get()?.resetThread(thread.metadata?.graph_id as string, thread.thread_id);
|
|
300
|
+
if (nowThread) {
|
|
301
|
+
client.get()?.resetStream();
|
|
302
|
+
}
|
|
303
|
+
return nowThread;
|
|
300
304
|
},
|
|
301
305
|
/**
|
|
302
306
|
* @zh 删除指定的历史聊天会话。
|