| 1 | import { minimatch } from "minimatch"; |
| 2 | import { |
| 3 | ContextItemWithId, |
| 4 | RuleWithSource, |
| 5 | ToolResultChatMessage, |
| 6 | UserChatMessage, |
| 7 | } from "../.."; |
| 8 | import { renderChatMessage } from "../../util/messageContent"; |
| 9 | import { extractPathsFromCodeBlocks } from "../utils/extractPathsFromCodeBlocks"; |
| 10 | |
| 11 | /** |
| 12 | * Checks if a path matches any of the provided globs |
| 13 | */ |
| 14 | const matchesGlobs = ( |
| 15 | path: string, |
| 16 | globs: string | string[] | undefined, |
| 17 | ): boolean => { |
| 18 | if (!globs) return true; |
| 19 | |
| 20 | if (typeof globs === "string") { |
| 21 | return minimatch(path, globs); |
| 22 | } |
| 23 | |
| 24 | if (Array.isArray(globs)) { |
| 25 | return globs.some((glob) => minimatch(path, glob)); |
| 26 | } |
| 27 | |
| 28 | return false; |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * Filters rules that apply to the given message and/or context items |
| 33 | * |
| 34 | * @param userMessage - The user or tool message to check for file paths in code blocks |
| 35 | * @param rules - The list of rules to filter |
| 36 | * @param contextItems - Context items to check for file paths |
| 37 | * @returns List of applicable rules |
| 38 | */ |
| 39 | export const getApplicableRules = ( |
| 40 | userMessage: UserChatMessage | ToolResultChatMessage | undefined, |
| 41 | rules: RuleWithSource[], |
| 42 | contextItems: ContextItemWithId[], |
| 43 | ): RuleWithSource[] => { |
| 44 | const filePathsFromMessage = userMessage |
| 45 | ? extractPathsFromCodeBlocks(renderChatMessage(userMessage)) |
| 46 | : []; |
| 47 | |
| 48 | // Extract file paths from context items |
| 49 | const filePathsFromContextItems = contextItems |
| 50 | .filter((item) => item.uri?.type === "file" && item.uri?.value) |
| 51 | .map((item) => item.uri!.value); |
| 52 | // Combine file paths from both sources |
| 53 | const allFilePaths = [...filePathsFromMessage, ...filePathsFromContextItems]; |
| 54 | |
| 55 | return rules.filter((rule) => { |
| 56 | // A rule is active if it has no globs (applies to all files) |
| 57 | // or if at least one file path matches its globs |
| 58 | const hasNoGlobs = !rule.globs; |
| 59 | const matchesAnyFilePath = allFilePaths.some((path) => |
| 60 | matchesGlobs(path, rule.globs), |
| 61 | ); |
| 62 | |
| 63 | return hasNoGlobs || matchesAnyFilePath; |
| 64 | }); |
| 65 | }; |
| 66 | |
| 67 | /** |
| 68 | * Creates a system message string with applicable rules appended |
| 69 | * |
| 70 | * @param baseSystemMessage - The base system message to start with |
| 71 | * @param userMessage - The user message to check for file paths |
| 72 | * @param rules - The list of rules to filter |
| 73 | * @param contextItems - Context items to check for file paths |
| 74 | * @returns System message with applicable rules appended |
| 75 | */ |
| 76 | export const getSystemMessageWithRules = ({ |
| 77 | baseSystemMessage, |
| 78 | userMessage, |
| 79 | rules, |
| 80 | contextItems, |
| 81 | }: { |
| 82 | baseSystemMessage?: string; |
| 83 | userMessage: UserChatMessage | ToolResultChatMessage | undefined; |
| 84 | rules: RuleWithSource[]; |
| 85 | contextItems: ContextItemWithId[]; |
| 86 | }) => { |
| 87 | const applicableRules = getApplicableRules(userMessage, rules, contextItems); |
| 88 | let systemMessage = baseSystemMessage ?? ""; |
| 89 | |
| 90 | for (const rule of applicableRules) { |
| 91 | systemMessage += `\n\n${rule.rule}`; |
| 92 | } |
| 93 | |
| 94 | return systemMessage; |
| 95 | }; |