fix: missing context items
This commit is contained in:
parent
64fb928b8f
commit
bdc2ec8374
3 changed files with 49 additions and 6 deletions
|
|
@ -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 = `\
|
||||
<important_rules>
|
||||
|
|
@ -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()) {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -100,6 +100,7 @@ export const streamResponseThunk = createAsyncThunk<
|
|||
? (userMsg as UserChatMessage | ToolResultChatMessage)
|
||||
: undefined,
|
||||
rules,
|
||||
selectedContextItems,
|
||||
);
|
||||
|
||||
// Store in history for UI display
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue