Skip to content

Commit 742add2

Browse files
authored
feat: separate thread creation for better extensibility (ag-ui-protocol#111)
1 parent a546db7 commit 742add2

File tree

1 file changed

+25
-11
lines changed
  • typescript-sdk/integrations/langgraph/src

1 file changed

+25
-11
lines changed

typescript-sdk/integrations/langgraph/src/index.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Message as LangGraphMessage,
1010
Config,
1111
Interrupt,
12+
Thread
1213
} from "@langchain/langgraph-sdk";
1314
import { randomUUID } from "node:crypto";
1415
import { RemoveMessage } from "@langchain/core/messages";
@@ -165,7 +166,9 @@ export class LangGraphAgent extends AbstractAgent {
165166
this.assistant = await this.getAssistant();
166167
}
167168

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
169172

170173
const agentStateValues = agentState.values as State;
171174
const aguiToLangChainMessage = aguiMessagesToLangChain(messages);
@@ -414,7 +417,7 @@ export class LangGraphAgent extends AbstractAgent {
414417

415418
const isToolCallStartEvent = !hasCurrentStream && toolCallData?.name;
416419
const isToolCallArgsEvent =
417-
hasCurrentStream && currentStream?.toolCallId && toolCallData.args;
420+
hasCurrentStream && currentStream?.toolCallId && toolCallData?.args;
418421
const isToolCallEndEvent = hasCurrentStream && currentStream?.toolCallId && !toolCallData;
419422

420423
const reasoningData = resolveReasoningContent(event.data);
@@ -670,16 +673,27 @@ export class LangGraphAgent extends AbstractAgent {
670673
return state;
671674
}
672675

673-
async getOrCreateThreadAndReturnState(threadId: string): Promise<ThreadState<{}>> {
674-
let agentState = { values: {} } as ThreadState;
676+
async getOrCreateThread(threadId: string): Promise<Thread> {
677+
let thread: Thread;
675678
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}`);
680686
}
681687

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);
683697
}
684698

685699
async mergeConfigs({
@@ -750,11 +764,11 @@ export class LangGraphAgent extends AbstractAgent {
750764
const assistants = await this.client.assistants.search();
751765
const retrievedAssistant = assistants.find(
752766
(searchResult) =>
753-
searchResult.assistant_id === this.agentId || searchResult.graph_id === this.graphId,
767+
searchResult.graph_id === this.graphId,
754768
);
755769
if (!retrievedAssistant) {
756770
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
758772
759773
These are the available agents: [${assistants.map((a) => `${a.graph_id} (ID: ${a.assistant_id})`).join(", ")}]
760774
`);

0 commit comments

Comments
 (0)