From d265ec01e280f2c36e9fe61be365de0d1c5e982b Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Wed, 21 May 2025 12:41:40 -0700 Subject: [PATCH] improv: clean up code --- core/llm/constructMessages.ts | 53 ++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/core/llm/constructMessages.ts b/core/llm/constructMessages.ts index 0a7ba5a03..1e217f57f 100644 --- a/core/llm/constructMessages.ts +++ b/core/llm/constructMessages.ts @@ -1,6 +1,7 @@ import { ChatHistoryItem, ChatMessage, + ContextItemWithId, RuleWithSource, TextMessagePart, ToolResultChatMessage, @@ -77,6 +78,31 @@ export const DEFAULT_AGENT_SYSTEM_MESSAGE = `\ ${EDIT_MESSAGE} `; +/** + * Helper function to get the context items for a user message + */ +function getUserContextItems( + userMsg: UserChatMessage | ToolResultChatMessage | undefined, + history: ChatHistoryItem[], +): ContextItemWithId[] | undefined { + if (!userMsg) return undefined; + + // Find the history item that contains the userMsg + const historyItem = history.find((item) => { + // Check if the message ID matches + if ("id" in userMsg && "id" in item.message) { + return (item.message as any).id === (userMsg as any).id; + } + // Fallback to content comparison + return ( + item.message.content === userMsg.content && + item.message.role === userMsg.role + ); + }); + + return historyItem?.contextItems; +} + export function constructMessages( messageMode: string, history: ChatHistoryItem[], @@ -128,28 +154,11 @@ export function constructMessages( | ToolResultChatMessage | undefined; - // Find the context items for the last user message - let lastUserContextItems; - if (lastUserMsg) { - const lastUserMsgIndex = msgs.findIndex((msg) => { - if (msg.role !== "user" && msg.role !== "tool") return false; - if ("id" in msg && "id" in lastUserMsg) { - return msg.id === lastUserMsg.id; - } - return msg === lastUserMsg; - }); - - if (lastUserMsgIndex !== -1) { - const historyIndex = filteredHistory.findIndex( - (item) => item.message === msgs[lastUserMsgIndex], - ); - - if (historyIndex !== -1) { - lastUserContextItems = filteredHistory[historyIndex].contextItems; - } - } - } - + // Get context items for the last user message + const lastUserContextItems = getUserContextItems( + lastUserMsg, + filteredHistory, + ); const systemMessage = getSystemMessageWithRules({ baseSystemMessage: baseChatOrAgentSystemMessage, rules,