From bdc2ec8374ac9e7d14fc440ea80fa438005735e8 Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Wed, 21 May 2025 12:37:43 -0700 Subject: [PATCH] fix: missing context items --- core/llm/constructMessages.ts | 28 +++++++++++++++++++-- core/llm/rules/getSystemMessageWithRules.ts | 26 ++++++++++++++++--- gui/src/redux/thunks/streamResponse.ts | 1 + 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/core/llm/constructMessages.ts b/core/llm/constructMessages.ts index 309429d4b..0a7ba5a03 100644 --- a/core/llm/constructMessages.ts +++ b/core/llm/constructMessages.ts @@ -58,7 +58,7 @@ const EDIT_MESSAGE = `\ at the beginning, middle, or end of files using these "lazy" comments. Only provide the complete file when explicitly requested. Include a concise explanation of changes unless the user specifically asks for code only. -` +`; export const DEFAULT_CHAT_SYSTEM_MESSAGE = `\ @@ -92,7 +92,8 @@ export function constructMessages( const historyItem = filteredHistory[i]; if (messageMode === "chat") { - const toolMessage: ToolResultChatMessage = historyItem.message as ToolResultChatMessage; + const toolMessage: ToolResultChatMessage = + historyItem.message as ToolResultChatMessage; if (historyItem.toolCallState?.toolCallId || toolMessage.toolCallId) { // remove all tool calls from the history continue; @@ -127,10 +128,33 @@ 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; + } + } + } + const systemMessage = getSystemMessageWithRules({ baseSystemMessage: baseChatOrAgentSystemMessage, rules, userMessage: lastUserMsg, + contextItems: lastUserContextItems, }); if (systemMessage.trim()) { diff --git a/core/llm/rules/getSystemMessageWithRules.ts b/core/llm/rules/getSystemMessageWithRules.ts index 3d7372fef..a6969248c 100644 --- a/core/llm/rules/getSystemMessageWithRules.ts +++ b/core/llm/rules/getSystemMessageWithRules.ts @@ -1,5 +1,10 @@ import { minimatch } from "minimatch"; -import { RuleWithSource, ToolResultChatMessage, UserChatMessage } from "../.."; +import { + ContextItemWithId, + RuleWithSource, + ToolResultChatMessage, + UserChatMessage, +} from "../.."; import { renderChatMessage } from "../../util/messageContent"; import { extractPathsFromCodeBlocks } from "../utils/extractPathsFromCodeBlocks"; @@ -24,21 +29,32 @@ const matchesGlobs = ( }; /** - * Filters rules that apply to the given message + * Filters rules that apply to the given message and/or context items */ export const getApplicableRules = ( userMessage: UserChatMessage | ToolResultChatMessage | undefined, rules: RuleWithSource[], + contextItems?: ContextItemWithId[], ): RuleWithSource[] => { const filePathsFromMessage = userMessage ? extractPathsFromCodeBlocks(renderChatMessage(userMessage)) : []; + // Extract file paths from context items + const filePathsFromContextItems = contextItems + ? contextItems + .filter((item) => item.uri?.type === "file" && item.uri?.value) + .map((item) => item.uri!.value) + : []; + + // Combine file paths from both sources + const allFilePaths = [...filePathsFromMessage, ...filePathsFromContextItems]; + return rules.filter((rule) => { // A rule is active if it has no globs (applies to all files) // or if at least one file path matches its globs const hasNoGlobs = !rule.globs; - const matchesAnyFilePath = filePathsFromMessage.some((path) => + const matchesAnyFilePath = allFilePaths.some((path) => matchesGlobs(path, rule.globs), ); @@ -50,12 +66,14 @@ export const getSystemMessageWithRules = ({ baseSystemMessage, userMessage, rules, + contextItems, }: { baseSystemMessage?: string; userMessage: UserChatMessage | ToolResultChatMessage | undefined; rules: RuleWithSource[]; + contextItems?: ContextItemWithId[]; }) => { - const applicableRules = getApplicableRules(userMessage, rules); + const applicableRules = getApplicableRules(userMessage, rules, contextItems); let systemMessage = baseSystemMessage ?? ""; for (const rule of applicableRules) { diff --git a/gui/src/redux/thunks/streamResponse.ts b/gui/src/redux/thunks/streamResponse.ts index c9a21a30c..8f11fc7f5 100644 --- a/gui/src/redux/thunks/streamResponse.ts +++ b/gui/src/redux/thunks/streamResponse.ts @@ -100,6 +100,7 @@ export const streamResponseThunk = createAsyncThunk< ? (userMsg as UserChatMessage | ToolResultChatMessage) : undefined, rules, + selectedContextItems, ); // Store in history for UI display