fix message reversing

This commit is contained in:
bracesproul
2025-02-28 14:15:37 -08:00
parent 3f4aad48e6
commit a15a9104b4
6 changed files with 57 additions and 129 deletions

View File

@@ -32,7 +32,8 @@ async function router(
const prompt = `You're a highly helpful AI assistant, tasked with routing the user's query to the appropriate tool.
You should analyze the user's input, and choose the appropriate tool to use.`;
const recentHumanMessage = state.messages
const messagesCopy = state.messages;
const recentHumanMessage = messagesCopy
.reverse()
.find((m) => m.getType() === "human");
@@ -65,7 +66,11 @@ function handleRoute(
async function handleGeneralInput(state: GenerativeUIState) {
const llm = new ChatOpenAI({ model: "gpt-4o-mini", temperature: 0 });
const response = await llm.invoke(state.messages);
const messagesCopy = state.messages;
messagesCopy.reverse();
console.log("messagesCopy", messagesCopy);
const response = await llm.invoke(messagesCopy);
return {
messages: [response],

View File

@@ -1,5 +1,4 @@
import { StockbrokerState } from "../types";
import { ToolMessage } from "@langchain/core/messages";
import { ChatOpenAI } from "@langchain/openai";
import { typedUi } from "@langchain/langgraph-sdk/react-ui/server";
import type ComponentMap from "../../uis/index";
@@ -64,19 +63,8 @@ export async function callTools(
ui.write("portfolio", {});
}
const toolMessages =
message.tool_calls?.map((tc) => {
return new ToolMessage({
name: tc.name,
tool_call_id: tc.id ?? "",
content: "Successfully handled tool call",
});
}) || [];
console.log("Returning", [message, ...toolMessages]);
return {
messages: [message, ...toolMessages],
messages: [message],
// TODO: Fix the ui return type.
ui: ui.collect as any[],
timestamp: Date.now(),

View File

@@ -15,7 +15,9 @@ export default function StockPrice(props: {
apiUrl: "http://localhost:3123",
});
const aiTool = thread.messages
const messagesCopy = thread.messages;
const aiTool = messagesCopy
.slice()
.reverse()
.find(