Elffuss BraidHub

kikocis/continue public

951aa1c26a3e · 3.4 KiBRawPermalink
1import { minimatch } from "minimatch";
2import {
3 ContextItemWithId,
4 RuleWithSource,
5 ToolResultChatMessage,
6 UserChatMessage,
7} from "../..";
8import { renderChatMessage } from "../../util/messageContent";
9import { extractPathsFromCodeBlocks } from "../utils/extractPathsFromCodeBlocks";
10
11/**
12 * Checks if a path matches any of the provided globs
13 */
14const 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 * Determines if a rule should be applied based on its alwaysApply property and file path matching
33 *
34 * @param rule - The rule to check
35 * @param filePaths - Array of file paths to check against the rule's globs
36 * @returns true if the rule should be applied, false otherwise
37 */
38export const shouldApplyRule = (
39 rule: RuleWithSource,
40 filePaths: string[],
41): boolean => {
42 if (rule.alwaysApply) {
43 return true;
44 }
45
46 // If alwaysApply is explicitly false, only apply if there are globs AND they match
47 if (rule.alwaysApply === false) {
48 if (!rule.globs) {
49 return false; // No globs specified, don't apply
50 }
51 return filePaths.some((path) => matchesGlobs(path, rule.globs));
52 }
53
54 // If alwaysApply is undefined, default behavior:
55 // - No globs: always apply
56 // - Has globs: only apply if they match
57 if (!rule.globs) {
58 return true;
59 }
60
61 return filePaths.some((path) => matchesGlobs(path, rule.globs));
62};
63
64/**
65 * Filters rules that apply to the given message and/or context items
66 *
67 * @param userMessage - The user or tool message to check for file paths in code blocks
68 * @param rules - The list of rules to filter
69 * @param contextItems - Context items to check for file paths
70 * @returns List of applicable rules
71 */
72export const getApplicableRules = (
73 userMessage: UserChatMessage | ToolResultChatMessage | undefined,
74 rules: RuleWithSource[],
75 contextItems: ContextItemWithId[],
76): RuleWithSource[] => {
77 const filePathsFromMessage = userMessage
78 ? extractPathsFromCodeBlocks(renderChatMessage(userMessage))
79 : [];
80
81 // Extract file paths from context items
82 const filePathsFromContextItems = contextItems
83 .filter((item) => item.uri?.type === "file" && item.uri?.value)
84 .map((item) => item.uri!.value);
85 // Combine file paths from both sources
86 const allFilePaths = [...filePathsFromMessage, ...filePathsFromContextItems];
87
88 return rules.filter((rule) => shouldApplyRule(rule, allFilePaths));
89};
90
91/**
92 * Creates a system message string with applicable rules appended
93 *
94 * @param baseSystemMessage - The base system message to start with
95 * @param userMessage - The user message to check for file paths
96 * @param rules - The list of rules to filter
97 * @param contextItems - Context items to check for file paths
98 * @returns System message with applicable rules appended
99 */
100export const getSystemMessageWithRules = ({
101 baseSystemMessage,
102 userMessage,
103 rules,
104 contextItems,
105}: {
106 baseSystemMessage?: string;
107 userMessage: UserChatMessage | ToolResultChatMessage | undefined;
108 rules: RuleWithSource[];
109 contextItems: ContextItemWithId[];
110}) => {
111 const applicableRules = getApplicableRules(userMessage, rules, contextItems);
112 let systemMessage = baseSystemMessage ?? "";
113
114 for (const rule of applicableRules) {
115 systemMessage += `\n\n${rule.rule}`;
116 }
117
118 return systemMessage;
119};