Skip to content

Commit

Permalink
feat: allow disabling auto cancellation of tool calls (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Sep 29, 2024
1 parent 3e1fc90 commit 46f91c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-starfishes-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react": patch
---

feat(langgraph): allow disabling autocancellation of pending tool calls
24 changes: 15 additions & 9 deletions packages/react-langgraph/src/useLangGraphRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ const getPendingToolCalls = (messages: LangChainMessage[]) => {

export const useLangGraphRuntime = ({
threadId,
autoCancelPendingToolCalls,
stream,
onSwitchToNewThread,
onSwitchToThread,
}: {
threadId?: string | undefined;
autoCancelPendingToolCalls?: boolean | undefined;
stream: (messages: LangChainMessage[]) => Promise<
AsyncGenerator<{
event: string;
Expand Down Expand Up @@ -71,15 +73,19 @@ export const useLangGraphRuntime = ({
if (msg.content.length !== 1 || msg.content[0]?.type !== "text")
throw new Error("Only text messages are supported");

const cancellations = getPendingToolCalls(messages).map(
(t) =>
({
type: "tool",
name: t.name,
tool_call_id: t.id,
content: JSON.stringify({ cancelled: true }),
}) satisfies LangChainMessage & { type: "tool" },
);
const cancellations =
autoCancelPendingToolCalls !== false
? getPendingToolCalls(messages).map(
(t) =>
({
type: "tool",
name: t.name,
tool_call_id: t.id,
content: JSON.stringify({ cancelled: true }),
}) satisfies LangChainMessage & { type: "tool" },
)
: [];

return handleSendMessage([
...cancellations,
{
Expand Down

0 comments on commit 46f91c2

Please sign in to comment.