Skip to content

Commit

Permalink
feat: change langgraph callback to accept an array for tool call canc…
Browse files Browse the repository at this point in the history
…ellation support (#818)
  • Loading branch information
Yonom authored Sep 11, 2024
1 parent 3b2a502 commit 184d836
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-kings-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@assistant-ui/react-langgraph": patch
---

feat: allow multiple message sends to support pending tool call cancellations
5 changes: 0 additions & 5 deletions packages/react-langgraph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,3 @@ export type {
LangChainToolCall,
LangChainToolCallChunk,
} from "./types";

/**
* @deprecated Use `useLangGraphRuntime` instead. This will be removed in 0.1.0.
*/
export { useLangGraphRuntime as useLangChainLangGraphRuntime } from "./useLangGraphRuntime";
10 changes: 5 additions & 5 deletions packages/react-langgraph/src/useLangGraphMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useCallback } from "react";
export const useLangGraphMessages = <TMessage>({
stream,
}: {
stream: (message: TMessage) => Promise<
stream: (messages: TMessage[]) => Promise<
AsyncGenerator<{
event: string;
data: any;
Expand All @@ -13,12 +13,12 @@ export const useLangGraphMessages = <TMessage>({
const [messages, setMessages] = useState<TMessage[]>([]);

const sendMessage = useCallback(
async (message: TMessage) => {
if (message !== null) {
setMessages((currentMessages) => [...currentMessages, message]);
async (messages: TMessage[]) => {
if (messages.length > 0) {
setMessages((currentMessages) => [...currentMessages, ...messages]);
}

const response = await stream(message);
const response = await stream(messages);

const completeMessages: TMessage[] = [];
let partialMessages: Map<string, TMessage> = new Map();
Expand Down
30 changes: 17 additions & 13 deletions packages/react-langgraph/src/useLangGraphRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useLangGraphRuntime = ({
stream,
}: {
threadId?: string | undefined;
stream: (message: LangChainMessage) => Promise<
stream: (messages: LangChainMessage[]) => Promise<
AsyncGenerator<{
event: string;
data: any;
Expand All @@ -25,10 +25,10 @@ export const useLangGraphRuntime = ({
});

const [isRunning, setIsRunning] = useState(false);
const handleSendMessage = async (message: LangChainMessage) => {
const handleSendMessage = async (messages: LangChainMessage[]) => {
try {
setIsRunning(true);
await sendMessage(message);
await sendMessage(messages);
} catch (error) {
console.error("Error streaming messages:", error);
} finally {
Expand All @@ -49,18 +49,22 @@ export const useLangGraphRuntime = ({
onNew: (msg) => {
if (msg.content.length !== 1 || msg.content[0]?.type !== "text")
throw new Error("Only text messages are supported");
return handleSendMessage({
type: "human",
content: msg.content[0].text,
});
return handleSendMessage([
{
type: "human",
content: msg.content[0].text,
},
]);
},
onAddToolResult: async ({ toolCallId, toolName, result }) => {
await handleSendMessage({
type: "tool",
name: toolName,
tool_call_id: toolCallId,
content: JSON.stringify(result),
});
await handleSendMessage([
{
type: "tool",
name: toolName,
tool_call_id: toolCallId,
content: JSON.stringify(result),
},
]);
},
});
};

0 comments on commit 184d836

Please sign in to comment.