From b0f0784a6b6f4bb735a6a3f9024b2166914260de Mon Sep 17 00:00:00 2001 From: Tat Dat Duong Date: Sun, 2 Mar 2025 19:09:59 +0100 Subject: [PATCH] Use findLast to avoid reversing --- agent/agent.tsx | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/agent/agent.tsx b/agent/agent.tsx index 76f3fb9..4368d9a 100644 --- a/agent/agent.tsx +++ b/agent/agent.tsx @@ -32,10 +32,9 @@ 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 messagesCopy = state.messages; - const recentHumanMessage = messagesCopy - .reverse() - .find((m) => m.getType() === "human"); + const recentHumanMessage = state.messages.findLast( + (m) => m.getType() === "human" + ); if (!recentHumanMessage) { throw new Error("No human message found in state"); @@ -66,11 +65,7 @@ function handleRoute( async function handleGeneralInput(state: GenerativeUIState) { const llm = new ChatOpenAI({ model: "gpt-4o-mini", temperature: 0 }); - const messagesCopy = state.messages; - messagesCopy.reverse(); - console.log("messagesCopy", messagesCopy); - - const response = await llm.invoke(messagesCopy); + const response = await llm.invoke(state.messages); return { messages: [response],