fix message reversing
This commit is contained in:
@@ -1,13 +1,43 @@
|
||||
import { ReactNode } from "react";
|
||||
import { ReactNode, useEffect } from "react";
|
||||
import {
|
||||
useExternalStoreRuntime,
|
||||
AppendMessage,
|
||||
AssistantRuntimeProvider,
|
||||
} from "@assistant-ui/react";
|
||||
import { HumanMessage } from "@langchain/langgraph-sdk";
|
||||
import { HumanMessage, Message, ToolMessage } from "@langchain/langgraph-sdk";
|
||||
import { useStreamContext } from "./Stream";
|
||||
import { convertLangChainMessages } from "./convert-messages";
|
||||
|
||||
function ensureToolCallsHaveResponses(messages: Message[]): Message[] {
|
||||
const newMessages: ToolMessage[] = [];
|
||||
|
||||
messages.forEach((message, index) => {
|
||||
if (message.type !== "ai" || message.tool_calls?.length === 0) {
|
||||
// If it's not an AI message, or it doesn't have tool calls, we can ignore.
|
||||
return;
|
||||
}
|
||||
// If it has tool calls, ensure the message which follows this is a tool message
|
||||
const followingMessage = messages[index + 1];
|
||||
if (followingMessage && followingMessage.type === "tool") {
|
||||
// Following message is a tool message, so we can ignore.
|
||||
return;
|
||||
}
|
||||
|
||||
// Since the following message is not a tool message, we must create a new tool message
|
||||
newMessages.push(
|
||||
...(message.tool_calls?.map((tc) => ({
|
||||
type: "tool" as const,
|
||||
tool_call_id: tc.id ?? "",
|
||||
id: tc.id ?? "",
|
||||
name: tc.name,
|
||||
content: "Successfully handled tool call.",
|
||||
})) ?? []),
|
||||
);
|
||||
});
|
||||
|
||||
return newMessages;
|
||||
}
|
||||
|
||||
export function RuntimeProvider({
|
||||
children,
|
||||
}: Readonly<{
|
||||
@@ -21,9 +51,20 @@ export function RuntimeProvider({
|
||||
|
||||
const input = message.content[0].text;
|
||||
const humanMessage: HumanMessage = { type: "human", content: input };
|
||||
stream.submit({ messages: [humanMessage] });
|
||||
const newMessages = [
|
||||
...ensureToolCallsHaveResponses(stream.messages),
|
||||
humanMessage,
|
||||
];
|
||||
console.log("Sending new messages", newMessages);
|
||||
stream.submit({
|
||||
messages: newMessages,
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
console.log("useEffect - stream.messages", stream.messages);
|
||||
}, [stream.messages]);
|
||||
|
||||
const runtime = useExternalStoreRuntime({
|
||||
isRunning: stream.isLoading,
|
||||
messages: stream.messages,
|
||||
|
||||
Reference in New Issue
Block a user