|
9 | 9 | Message as LangGraphMessage,
|
10 | 10 | Config,
|
11 | 11 | Interrupt,
|
| 12 | + Thread |
12 | 13 | } from "@langchain/langgraph-sdk";
|
13 | 14 | import { randomUUID } from "node:crypto";
|
14 | 15 | import { RemoveMessage } from "@langchain/core/messages";
|
@@ -165,7 +166,9 @@ export class LangGraphAgent extends AbstractAgent {
|
165 | 166 | this.assistant = await this.getAssistant();
|
166 | 167 | }
|
167 | 168 |
|
168 |
| - let agentState = await this.getOrCreateThreadAndReturnState(threadId); |
| 169 | + const thread = await this.getOrCreateThread(threadId); |
| 170 | + this.activeRun!.threadId = thread.thread_id; |
| 171 | + const agentState = await this.client.threads.getState(thread.thread_id) ?? { values: {} } as ThreadState |
169 | 172 |
|
170 | 173 | const agentStateValues = agentState.values as State;
|
171 | 174 | const aguiToLangChainMessage = aguiMessagesToLangChain(messages);
|
@@ -414,7 +417,7 @@ export class LangGraphAgent extends AbstractAgent {
|
414 | 417 |
|
415 | 418 | const isToolCallStartEvent = !hasCurrentStream && toolCallData?.name;
|
416 | 419 | const isToolCallArgsEvent =
|
417 |
| - hasCurrentStream && currentStream?.toolCallId && toolCallData.args; |
| 420 | + hasCurrentStream && currentStream?.toolCallId && toolCallData?.args; |
418 | 421 | const isToolCallEndEvent = hasCurrentStream && currentStream?.toolCallId && !toolCallData;
|
419 | 422 |
|
420 | 423 | const reasoningData = resolveReasoningContent(event.data);
|
@@ -670,16 +673,27 @@ export class LangGraphAgent extends AbstractAgent {
|
670 | 673 | return state;
|
671 | 674 | }
|
672 | 675 |
|
673 |
| - async getOrCreateThreadAndReturnState(threadId: string): Promise<ThreadState<{}>> { |
674 |
| - let agentState = { values: {} } as ThreadState; |
| 676 | + async getOrCreateThread(threadId: string): Promise<Thread> { |
| 677 | + let thread: Thread; |
675 | 678 | try {
|
676 |
| - await this.client.threads.get(threadId); |
677 |
| - agentState = await this.client.threads.getState(threadId); |
678 |
| - } catch (error) { |
679 |
| - await this.client.threads.create({ threadId }); |
| 679 | + try { |
| 680 | + thread = await this.getThread(threadId); |
| 681 | + } catch (error) { |
| 682 | + thread = await this.createThread({ threadId }); |
| 683 | + } |
| 684 | + } catch (error: unknown) { |
| 685 | + throw new Error(`Failed to create thread: ${(error as Error).message}`); |
680 | 686 | }
|
681 | 687 |
|
682 |
| - return agentState; |
| 688 | + return thread; |
| 689 | + } |
| 690 | + |
| 691 | + async getThread(threadId: string) { |
| 692 | + return this.client.threads.get(threadId); |
| 693 | + } |
| 694 | + |
| 695 | + async createThread(payload?: Parameters<typeof this.client.threads.create>[0]) { |
| 696 | + return this.client.threads.create(payload); |
683 | 697 | }
|
684 | 698 |
|
685 | 699 | async mergeConfigs({
|
@@ -750,11 +764,11 @@ export class LangGraphAgent extends AbstractAgent {
|
750 | 764 | const assistants = await this.client.assistants.search();
|
751 | 765 | const retrievedAssistant = assistants.find(
|
752 | 766 | (searchResult) =>
|
753 |
| - searchResult.assistant_id === this.agentId || searchResult.graph_id === this.graphId, |
| 767 | + searchResult.graph_id === this.graphId, |
754 | 768 | );
|
755 | 769 | if (!retrievedAssistant) {
|
756 | 770 | console.error(`
|
757 |
| - No agent found with graph ID ${this.graphId} or agent ID ${this.agentId} found..\n |
| 771 | + No agent found with graph ID ${this.graphId} found..\n |
758 | 772 |
|
759 | 773 | These are the available agents: [${assistants.map((a) => `${a.graph_id} (ID: ${a.assistant_id})`).join(", ")}]
|
760 | 774 | `);
|
|
0 commit comments