Changesets · Branches · Tags · Compare
| Changeset | 6af5a28a34a536fedc3e3da76cfdfb210b34a526367a601f61c28eb27ac7a8c1 |
|---|---|
| Author | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-05-21 18:09 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-05-21 18:09 -07:00 |
| Parents | 45556d3ff455, 7b2461a167fb |
7 files changed
modified core/edit/streamDiffLines.ts +1 −0
| @@ -122,6 +122,7 @@ | ||
| 122 | 122 | (msg) => msg.role === "user" || msg.role === "tool", |
| 123 | 123 | ) as UserChatMessage | ToolResultChatMessage | undefined), |
| 124 | 124 | baseSystemMessage: undefined, |
| 125 | + contextItems: [], | |
| 125 | 126 | }) |
| 126 | 127 | : undefined; |
| 127 | 128 |
modified core/llm/constructMessages.ts +35 −2
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | import { |
| 2 | 2 | ChatHistoryItem, |
| 3 | 3 | ChatMessage, |
| 4 | + ContextItemWithId, | |
| 4 | 5 | RuleWithSource, |
| 5 | 6 | TextMessagePart, |
| 6 | 7 | ToolResultChatMessage, |
| @@ -58,7 +59,7 @@ | ||
| 58 | 59 | at the beginning, middle, or end of files using these "lazy" comments. Only |
| 59 | 60 | provide the complete file when explicitly requested. Include a concise explanation |
| 60 | 61 | of changes unless the user specifically asks for code only. |
| 61 | -` | |
| 62 | +`; | |
| 62 | 63 | |
| 63 | 64 | export const DEFAULT_CHAT_SYSTEM_MESSAGE = `\ |
| 64 | 65 | <important_rules> |
| @@ -77,6 +78,31 @@ | ||
| 77 | 78 | ${EDIT_MESSAGE} |
| 78 | 79 | </important_rules>`; |
| 79 | 80 | |
| 81 | +/** | |
| 82 | + * Helper function to get the context items for a user message | |
| 83 | + */ | |
| 84 | +function getUserContextItems( | |
| 85 | + userMsg: UserChatMessage | ToolResultChatMessage | undefined, | |
| 86 | + history: ChatHistoryItem[], | |
| 87 | +): ContextItemWithId[] { | |
| 88 | + if (!userMsg) return []; | |
| 89 | + | |
| 90 | + // Find the history item that contains the userMsg | |
| 91 | + const historyItem = history.find((item) => { | |
| 92 | + // Check if the message ID matches | |
| 93 | + if ("id" in userMsg && "id" in item.message) { | |
| 94 | + return (item.message as any).id === (userMsg as any).id; | |
| 95 | + } | |
| 96 | + // Fallback to content comparison | |
| 97 | + return ( | |
| 98 | + item.message.content === userMsg.content && | |
| 99 | + item.message.role === userMsg.role | |
| 100 | + ); | |
| 101 | + }); | |
| 102 | + | |
| 103 | + return historyItem?.contextItems || []; | |
| 104 | +} | |
| 105 | + | |
| 80 | 106 | export function constructMessages( |
| 81 | 107 | messageMode: string, |
| 82 | 108 | history: ChatHistoryItem[], |
| @@ -92,7 +118,8 @@ | ||
| 92 | 118 | const historyItem = filteredHistory[i]; |
| 93 | 119 | |
| 94 | 120 | if (messageMode === "chat") { |
| 95 | - const toolMessage: ToolResultChatMessage = historyItem.message as ToolResultChatMessage; | |
| 121 | + const toolMessage: ToolResultChatMessage = | |
| 122 | + historyItem.message as ToolResultChatMessage; | |
| 96 | 123 | if (historyItem.toolCallState?.toolCallId || toolMessage.toolCallId) { |
| 97 | 124 | // remove all tool calls from the history |
| 98 | 125 | continue; |
| @@ -127,10 +154,16 @@ | ||
| 127 | 154 | | ToolResultChatMessage |
| 128 | 155 | | undefined; |
| 129 | 156 | |
| 157 | + // Get context items for the last user message | |
| 158 | + const lastUserContextItems = getUserContextItems( | |
| 159 | + lastUserMsg, | |
| 160 | + filteredHistory, | |
| 161 | + ); | |
| 130 | 162 | const systemMessage = getSystemMessageWithRules({ |
| 131 | 163 | baseSystemMessage: baseChatOrAgentSystemMessage, |
| 132 | 164 | rules, |
| 133 | 165 | userMessage: lastUserMsg, |
| 166 | + contextItems: lastUserContextItems, | |
| 134 | 167 | }); |
| 135 | 168 | |
| 136 | 169 | if (systemMessage.trim()) { |
modified core/llm/rules/getSystemMessageWithRules.test.ts +207 −3
| @@ -1,4 +1,4 @@ | ||
| 1 | -import { RuleWithSource, UserChatMessage } from "../.."; | |
| 1 | +import { ContextItemWithId, RuleWithSource, UserChatMessage } from "../.."; | |
| 2 | 2 | import { getSystemMessageWithRules } from "./getSystemMessageWithRules"; |
| 3 | 3 | |
| 4 | 4 | describe("getSystemMessageWithRules", () => { |
| @@ -25,11 +25,48 @@ | ||
| 25 | 25 | source: "rules-block", |
| 26 | 26 | }; |
| 27 | 27 | |
| 28 | + // JavaScript rule | |
| 29 | + const jsRule: RuleWithSource = { | |
| 30 | + name: "JavaScript Rule", | |
| 31 | + rule: "Follow JavaScript best practices", | |
| 32 | + globs: "**/*.js", | |
| 33 | + source: "rules-block", | |
| 34 | + }; | |
| 35 | + | |
| 36 | + // Empty context items | |
| 37 | + const emptyContextItems: ContextItemWithId[] = []; | |
| 38 | + | |
| 39 | + // Context items with file paths | |
| 40 | + const tsContextItem: ContextItemWithId = { | |
| 41 | + content: "TypeScript file content", | |
| 42 | + name: "Component.tsx", | |
| 43 | + description: "A TypeScript component", | |
| 44 | + id: { providerTitle: "file", itemId: "src/Component.tsx" }, | |
| 45 | + uri: { type: "file", value: "src/Component.tsx" }, | |
| 46 | + }; | |
| 47 | + | |
| 48 | + const pyContextItem: ContextItemWithId = { | |
| 49 | + content: "Python file content", | |
| 50 | + name: "utils.py", | |
| 51 | + description: "A Python utility file", | |
| 52 | + id: { providerTitle: "file", itemId: "utils.py" }, | |
| 53 | + uri: { type: "file", value: "utils.py" }, | |
| 54 | + }; | |
| 55 | + | |
| 56 | + const jsContextItem: ContextItemWithId = { | |
| 57 | + content: "JavaScript file content", | |
| 58 | + name: "utils.js", | |
| 59 | + description: "A JavaScript utility file", | |
| 60 | + id: { providerTitle: "file", itemId: "src/utils.js" }, | |
| 61 | + uri: { type: "file", value: "src/utils.js" }, | |
| 62 | + }; | |
| 63 | + | |
| 28 | 64 | it("should not include pattern-matched rules when no file paths are mentioned", () => { |
| 29 | 65 | const result = getSystemMessageWithRules({ |
| 30 | 66 | baseSystemMessage, |
| 31 | 67 | userMessage: undefined, |
| 32 | 68 | rules: [tsRule], |
| 69 | + contextItems: emptyContextItems, | |
| 33 | 70 | }); |
| 34 | 71 | |
| 35 | 72 | expect(result).toBe(baseSystemMessage); |
| @@ -40,12 +77,13 @@ | ||
| 40 | 77 | baseSystemMessage, |
| 41 | 78 | userMessage: undefined, |
| 42 | 79 | rules: [generalRule], |
| 80 | + contextItems: emptyContextItems, | |
| 43 | 81 | }); |
| 44 | 82 | |
| 45 | 83 | expect(result).toBe(`${baseSystemMessage}\n\n${generalRule.rule}`); |
| 46 | 84 | }); |
| 47 | 85 | |
| 48 | - it("should include only matching rules based on file paths", () => { | |
| 86 | + it("should include only matching rules based on file paths in message", () => { | |
| 49 | 87 | const userMessage: UserChatMessage = { |
| 50 | 88 | role: "user", |
| 51 | 89 | content: |
| @@ -56,6 +94,20 @@ | ||
| 56 | 94 | baseSystemMessage, |
| 57 | 95 | userMessage, |
| 58 | 96 | rules: [tsRule, pythonRule, generalRule], |
| 97 | + contextItems: emptyContextItems, | |
| 98 | + }); | |
| 99 | + | |
| 100 | + // Should include TS rule and general rule, but not Python rule | |
| 101 | + const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${generalRule.rule}`; | |
| 102 | + expect(result).toBe(expected); | |
| 103 | + }); | |
| 104 | + | |
| 105 | + it("should include matching rules based on file paths in context items", () => { | |
| 106 | + const result = getSystemMessageWithRules({ | |
| 107 | + baseSystemMessage, | |
| 108 | + userMessage: undefined, | |
| 109 | + rules: [tsRule, pythonRule, generalRule], | |
| 110 | + contextItems: [tsContextItem], | |
| 59 | 111 | }); |
| 60 | 112 | |
| 61 | 113 | // Should include TS rule and general rule, but not Python rule |
| @@ -63,7 +115,25 @@ | ||
| 63 | 115 | expect(result).toBe(expected); |
| 64 | 116 | }); |
| 65 | 117 | |
| 66 | - it("should include multiple matching rules", () => { | |
| 118 | + it("should include matching rules from both message and context items", () => { | |
| 119 | + const userMessage: UserChatMessage = { | |
| 120 | + role: "user", | |
| 121 | + content: "```python test.py\nprint('hello')\n```", | |
| 122 | + }; | |
| 123 | + | |
| 124 | + const result = getSystemMessageWithRules({ | |
| 125 | + baseSystemMessage, | |
| 126 | + userMessage, | |
| 127 | + rules: [tsRule, pythonRule, generalRule], | |
| 128 | + contextItems: [tsContextItem], | |
| 129 | + }); | |
| 130 | + | |
| 131 | + // Should include TS rule, Python rule, and general rule | |
| 132 | + const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${pythonRule.rule}\n\n${generalRule.rule}`; | |
| 133 | + expect(result).toBe(expected); | |
| 134 | + }); | |
| 135 | + | |
| 136 | + it("should include multiple matching rules from message", () => { | |
| 67 | 137 | const userMessage: UserChatMessage = { |
| 68 | 138 | role: "user", |
| 69 | 139 | content: |
| @@ -74,6 +144,20 @@ | ||
| 74 | 144 | baseSystemMessage, |
| 75 | 145 | userMessage, |
| 76 | 146 | rules: [tsRule, pythonRule, generalRule], |
| 147 | + contextItems: emptyContextItems, | |
| 148 | + }); | |
| 149 | + | |
| 150 | + // Should include all rules | |
| 151 | + const expected = `${baseSystemMessage}\n\n${tsRule.rule}\n\n${pythonRule.rule}\n\n${generalRule.rule}`; | |
| 152 | + expect(result).toBe(expected); | |
| 153 | + }); | |
| 154 | + | |
| 155 | + it("should include multiple matching rules from context items", () => { | |
| 156 | + const result = getSystemMessageWithRules({ | |
| 157 | + baseSystemMessage, | |
| 158 | + userMessage: undefined, | |
| 159 | + rules: [tsRule, pythonRule, generalRule], | |
| 160 | + contextItems: [tsContextItem, pyContextItem], | |
| 77 | 161 | }); |
| 78 | 162 | |
| 79 | 163 | // Should include all rules |
| @@ -91,6 +175,7 @@ | ||
| 91 | 175 | baseSystemMessage, |
| 92 | 176 | userMessage, |
| 93 | 177 | rules: [tsRule, pythonRule, generalRule], |
| 178 | + contextItems: emptyContextItems, | |
| 94 | 179 | }); |
| 95 | 180 | |
| 96 | 181 | // Should only include the general rule |
| @@ -117,6 +202,7 @@ | ||
| 117 | 202 | baseSystemMessage, |
| 118 | 203 | userMessage, |
| 119 | 204 | rules: [commaRule], |
| 205 | + contextItems: emptyContextItems, | |
| 120 | 206 | }); |
| 121 | 207 | |
| 122 | 208 | // With the current implementation, the comma-separated pattern is treated as a single string, |
| @@ -143,6 +229,7 @@ | ||
| 143 | 229 | baseSystemMessage, |
| 144 | 230 | userMessage, |
| 145 | 231 | rules: [arrayGlobRule], |
| 232 | + contextItems: emptyContextItems, | |
| 146 | 233 | }); |
| 147 | 234 | |
| 148 | 235 | // With our new implementation, the array of patterns should match both src/main.ts and tests/example.test.ts |
| @@ -168,6 +255,7 @@ | ||
| 168 | 255 | baseSystemMessage, |
| 169 | 256 | userMessage, |
| 170 | 257 | rules: [arrayGlobRule], |
| 258 | + contextItems: emptyContextItems, | |
| 171 | 259 | }); |
| 172 | 260 | |
| 173 | 261 | // Should match src/main.ts but not config/settings.ts |
| @@ -193,9 +281,125 @@ | ||
| 193 | 281 | baseSystemMessage, |
| 194 | 282 | userMessage, |
| 195 | 283 | rules: [arrayGlobRule], |
| 284 | + contextItems: emptyContextItems, | |
| 196 | 285 | }); |
| 197 | 286 | |
| 198 | 287 | // Should not match any file paths |
| 199 | 288 | expect(result).toBe(baseSystemMessage); |
| 200 | 289 | }); |
| 290 | + | |
| 291 | + // New test for code block with filename only (no language) | |
| 292 | + it("should handle code blocks with filename only (no language identifier)", () => { | |
| 293 | + const userMessage: UserChatMessage = { | |
| 294 | + role: "user", | |
| 295 | + content: "```test.js\nclass Calculator { /* code */ }\n```", | |
| 296 | + }; | |
| 297 | + | |
| 298 | + const result = getSystemMessageWithRules({ | |
| 299 | + baseSystemMessage, | |
| 300 | + userMessage, | |
| 301 | + rules: [jsRule, tsRule, generalRule], | |
| 302 | + contextItems: emptyContextItems, | |
| 303 | + }); | |
| 304 | + | |
| 305 | + // Should include JS rule and general rule | |
| 306 | + const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${generalRule.rule}`; | |
| 307 | + expect(result).toBe(expected); | |
| 308 | + }); | |
| 309 | + | |
| 310 | + // New test for code blocks with line ranges | |
| 311 | + it("should handle code blocks with line ranges", () => { | |
| 312 | + const userMessage: UserChatMessage = { | |
| 313 | + role: "user", | |
| 314 | + content: "```js test.js (19-25)\ndivide(number) { /* code */ }\n```", | |
| 315 | + }; | |
| 316 | + | |
| 317 | + const result = getSystemMessageWithRules({ | |
| 318 | + baseSystemMessage, | |
| 319 | + userMessage, | |
| 320 | + rules: [jsRule, tsRule, generalRule], | |
| 321 | + contextItems: emptyContextItems, | |
| 322 | + }); | |
| 323 | + | |
| 324 | + // Should include JS rule and general rule | |
| 325 | + const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${generalRule.rule}`; | |
| 326 | + expect(result).toBe(expected); | |
| 327 | + }); | |
| 328 | + | |
| 329 | + // New test for mixed code block formats | |
| 330 | + it("should handle mixed code block formats in the same message", () => { | |
| 331 | + const userMessage: UserChatMessage = { | |
| 332 | + role: "user", | |
| 333 | + content: | |
| 334 | + "```test.js\nclass Calculator { /* code */ }\n```\n" + | |
| 335 | + "```js utils.js (19-25)\ndivide(number) { /* code */ }\n```\n" + | |
| 336 | + "```ts config/settings.ts\nconst config = {};\n```", | |
| 337 | + }; | |
| 338 | + | |
| 339 | + const result = getSystemMessageWithRules({ | |
| 340 | + baseSystemMessage, | |
| 341 | + userMessage, | |
| 342 | + rules: [jsRule, tsRule, generalRule], | |
| 343 | + contextItems: emptyContextItems, | |
| 344 | + }); | |
| 345 | + | |
| 346 | + // Should include JS rule, TS rule, and general rule | |
| 347 | + const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${tsRule.rule}\n\n${generalRule.rule}`; | |
| 348 | + expect(result).toBe(expected); | |
| 349 | + }); | |
| 350 | + | |
| 351 | + // Test for context items when there's no message | |
| 352 | + it("should apply rules based on context items only when no message is present", () => { | |
| 353 | + const result = getSystemMessageWithRules({ | |
| 354 | + baseSystemMessage, | |
| 355 | + userMessage: undefined, | |
| 356 | + rules: [jsRule, tsRule, generalRule], | |
| 357 | + contextItems: [jsContextItem], | |
| 358 | + }); | |
| 359 | + | |
| 360 | + // Should include JS rule and general rule | |
| 361 | + const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${generalRule.rule}`; | |
| 362 | + expect(result).toBe(expected); | |
| 363 | + }); | |
| 364 | + | |
| 365 | + // Test for non-file context items | |
| 366 | + it("should ignore non-file context items", () => { | |
| 367 | + const nonFileContextItem: ContextItemWithId = { | |
| 368 | + content: "Some search result", | |
| 369 | + name: "Search result", | |
| 370 | + description: "A search result", | |
| 371 | + id: { providerTitle: "search", itemId: "search-result" }, | |
| 372 | + // No uri with type "file" | |
| 373 | + }; | |
| 374 | + | |
| 375 | + const result = getSystemMessageWithRules({ | |
| 376 | + baseSystemMessage, | |
| 377 | + userMessage: undefined, | |
| 378 | + rules: [jsRule, tsRule, generalRule], | |
| 379 | + contextItems: [nonFileContextItem], | |
| 380 | + }); | |
| 381 | + | |
| 382 | + // Should only include general rule | |
| 383 | + const expected = `${baseSystemMessage}\n\n${generalRule.rule}`; | |
| 384 | + expect(result).toBe(expected); | |
| 385 | + }); | |
| 386 | + | |
| 387 | + // Test combining context items with message | |
| 388 | + it("should match rules from both message code blocks and context items", () => { | |
| 389 | + const userMessage: UserChatMessage = { | |
| 390 | + role: "user", | |
| 391 | + content: "```ts src/main.ts\nconsole.log('hello');\n```", | |
| 392 | + }; | |
| 393 | + | |
| 394 | + const result = getSystemMessageWithRules({ | |
| 395 | + baseSystemMessage, | |
| 396 | + userMessage, | |
| 397 | + rules: [jsRule, tsRule, pythonRule, generalRule], | |
| 398 | + contextItems: [jsContextItem, pyContextItem], | |
| 399 | + }); | |
| 400 | + | |
| 401 | + // Should include JS, TS, Python rules and general rule | |
| 402 | + const expected = `${baseSystemMessage}\n\n${jsRule.rule}\n\n${tsRule.rule}\n\n${pythonRule.rule}\n\n${generalRule.rule}`; | |
| 403 | + expect(result).toBe(expected); | |
| 404 | + }); | |
| 201 | 405 | }); |
modified core/llm/rules/getSystemMessageWithRules.ts +33 −4
| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | import { minimatch } from "minimatch"; |
| 2 | -import { RuleWithSource, ToolResultChatMessage, UserChatMessage } from "../.."; | |
| 2 | +import { | |
| 3 | + ContextItemWithId, | |
| 4 | + RuleWithSource, | |
| 5 | + ToolResultChatMessage, | |
| 6 | + UserChatMessage, | |
| 7 | +} from "../.."; | |
| 3 | 8 | import { renderChatMessage } from "../../util/messageContent"; |
| 4 | 9 | import { extractPathsFromCodeBlocks } from "../utils/extractPathsFromCodeBlocks"; |
| 5 | 10 | |
| @@ -24,21 +29,34 @@ | ||
| 24 | 29 | }; |
| 25 | 30 | |
| 26 | 31 | /** |
| 27 | - * Filters rules that apply to the given message | |
| 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 | |
| 28 | 38 | */ |
| 29 | 39 | export const getApplicableRules = ( |
| 30 | 40 | userMessage: UserChatMessage | ToolResultChatMessage | undefined, |
| 31 | 41 | rules: RuleWithSource[], |
| 42 | + contextItems: ContextItemWithId[], | |
| 32 | 43 | ): RuleWithSource[] => { |
| 33 | 44 | const filePathsFromMessage = userMessage |
| 34 | 45 | ? extractPathsFromCodeBlocks(renderChatMessage(userMessage)) |
| 35 | 46 | : []; |
| 36 | 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 | + | |
| 37 | 55 | return rules.filter((rule) => { |
| 38 | 56 | // A rule is active if it has no globs (applies to all files) |
| 39 | 57 | // or if at least one file path matches its globs |
| 40 | 58 | const hasNoGlobs = !rule.globs; |
| 41 | - const matchesAnyFilePath = filePathsFromMessage.some((path) => | |
| 59 | + const matchesAnyFilePath = allFilePaths.some((path) => | |
| 42 | 60 | matchesGlobs(path, rule.globs), |
| 43 | 61 | ); |
| 44 | 62 | |
| @@ -46,16 +64,27 @@ | ||
| 46 | 64 | }); |
| 47 | 65 | }; |
| 48 | 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 | + */ | |
| 49 | 76 | export const getSystemMessageWithRules = ({ |
| 50 | 77 | baseSystemMessage, |
| 51 | 78 | userMessage, |
| 52 | 79 | rules, |
| 80 | + contextItems, | |
| 53 | 81 | }: { |
| 54 | 82 | baseSystemMessage?: string; |
| 55 | 83 | userMessage: UserChatMessage | ToolResultChatMessage | undefined; |
| 56 | 84 | rules: RuleWithSource[]; |
| 85 | + contextItems: ContextItemWithId[]; | |
| 57 | 86 | }) => { |
| 58 | - const applicableRules = getApplicableRules(userMessage, rules); | |
| 87 | + const applicableRules = getApplicableRules(userMessage, rules, contextItems); | |
| 59 | 88 | let systemMessage = baseSystemMessage ?? ""; |
| 60 | 89 | |
| 61 | 90 | for (const rule of applicableRules) { |
added core/llm/utils/extractPathsFromCodeBlocks.test.ts +35 −0
| @@ -0,0 +1,35 @@ | ||
| 1 | +import { extractPathsFromCodeBlocks } from "./extractPathsFromCodeBlocks"; | |
| 2 | + | |
| 3 | +describe("extractPathsFromCodeBlocks", () => { | |
| 4 | + it("should extract paths from code blocks with language and filepath", () => { | |
| 5 | + const content = "```typescript src/main.ts\nconst x = 1;\n```"; | |
| 6 | + expect(extractPathsFromCodeBlocks(content)).toEqual(["src/main.ts"]); | |
| 7 | + }); | |
| 8 | + | |
| 9 | + it("should extract paths from code blocks with filepath only (no language)", () => { | |
| 10 | + const content = "```test.js\nclass Calculator { }\n```"; | |
| 11 | + expect(extractPathsFromCodeBlocks(content)).toEqual(["test.js"]); | |
| 12 | + }); | |
| 13 | + | |
| 14 | + it("should extract paths from code blocks with line ranges", () => { | |
| 15 | + const content = "```js test.js (19-25)\nfunction test() {}\n```"; | |
| 16 | + expect(extractPathsFromCodeBlocks(content)).toEqual(["test.js"]); | |
| 17 | + }); | |
| 18 | + | |
| 19 | + it("should handle multiple code blocks with different formats", () => { | |
| 20 | + const content = | |
| 21 | + "```typescript src/main.ts\nconst x = 1;\n```\n" + | |
| 22 | + "```test.js\nclass Calculator { }\n```\n" + | |
| 23 | + "```js utils.js (19-25)\nfunction test() {}\n```"; | |
| 24 | + const result = extractPathsFromCodeBlocks(content); | |
| 25 | + expect(result).toContain("src/main.ts"); | |
| 26 | + expect(result).toContain("test.js"); | |
| 27 | + expect(result).toContain("utils.js"); | |
| 28 | + expect(result.length).toBe(3); | |
| 29 | + }); | |
| 30 | + | |
| 31 | + it("should not extract paths from code blocks without file paths", () => { | |
| 32 | + const content = "```typescript\nconst x = 1;\n```"; | |
| 33 | + expect(extractPathsFromCodeBlocks(content)).toEqual([]); | |
| 34 | + }); | |
| 35 | +}); | |
| No newline at the end of the file |
modified core/llm/utils/extractPathsFromCodeBlocks.ts +29 −13
| @@ -2,21 +2,37 @@ | ||
| 2 | 2 | * Extracts file paths from markdown code blocks |
| 3 | 3 | */ |
| 4 | 4 | export function extractPathsFromCodeBlocks(content: string): string[] { |
| 5 | - // Match: | |
| 6 | - // 1. Starting ``` | |
| 7 | - // 2. Optional language identifier | |
| 8 | - // 3. Required whitespace | |
| 9 | - // 4. File path (captured) that must contain a dot and extension | |
| 10 | - const codeBlockRegex = /```(?:[\w-+#]+)?\s+([^\s\n()]+\.[a-zA-Z0-9]+)/g; | |
| 5 | + console.log("CONTENT", content); | |
| 6 | + | |
| 11 | 7 | const paths: string[] = []; |
| 12 | - let match; | |
| 13 | - | |
| 14 | - while ((match = codeBlockRegex.exec(content)) !== null) { | |
| 15 | - const path = match[1]; | |
| 16 | - if (path && !path.startsWith("```")) { | |
| 17 | - paths.push(path); | |
| 8 | + | |
| 9 | + // Match code block opening patterns: | |
| 10 | + // 1. ```language filepath | |
| 11 | + // 2. ```filepath | |
| 12 | + // 3. ```language filepath (range) | |
| 13 | + | |
| 14 | + // First match all code block starts | |
| 15 | + const codeBlockStarts = content.match(/```[^\n]+/g) || []; | |
| 16 | + | |
| 17 | + for (const blockStart of codeBlockStarts) { | |
| 18 | + // Try to extract a valid filename with extension | |
| 19 | + const filenameMatches = blockStart.match(/([^\s()```]+\.[a-zA-Z0-9]+)/); | |
| 20 | + | |
| 21 | + if (filenameMatches && filenameMatches[1]) { | |
| 22 | + const filename = filenameMatches[1]; | |
| 23 | + | |
| 24 | + // Verify this is a legitimate filename (not part of something else) | |
| 25 | + if ( | |
| 26 | + // Check if valid extension | |
| 27 | + /\.[a-zA-Z0-9]+$/.test(filename) && | |
| 28 | + // Make sure it's not a URL | |
| 29 | + !filename.includes("://") && | |
| 30 | + // Avoid duplicates | |
| 31 | + !paths.includes(filename) | |
| 32 | + ) { | |
| 33 | + paths.push(filename); | |
| 34 | + } | |
| 18 | 35 | } |
| 19 | 36 | } |
| 20 | - | |
| 21 | 37 | return paths; |
| 22 | 38 | } |
modified gui/src/redux/thunks/streamResponse.ts +1 −0
| @@ -100,6 +100,7 @@ | ||
| 100 | 100 | ? (userMsg as UserChatMessage | ToolResultChatMessage) |
| 101 | 101 | : undefined, |
| 102 | 102 | rules, |
| 103 | + selectedContextItems, | |
| 103 | 104 | ); |
| 104 | 105 | |
| 105 | 106 | // Store in history for UI display |