From 2f7bd6e10a6e9129a50216f8a46f77e6652b6bbf Mon Sep 17 00:00:00 2001 From: Ferris Lucas Date: Sat, 2 Nov 2024 20:33:53 -0500 Subject: [PATCH] Fix auto context to exclude file paths inside liquid comment tags Related to #59 Exclude file paths inside liquid comment tags from being sent to the LLM. * Update `src/services/AutoContext.js` to filter out file paths inside liquid comment tags when extracting file paths from the prompt. * Add a helper function `isInsideLiquidComment` to check if a file path is inside a liquid comment tag. * Add test cases in `test/AutoContext.test.js` to verify that file paths inside liquid comment tags are excluded. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/ferrislucas/promptr/issues/59?shareId=XXXX-XXXX-XXXX-XXXX). --- src/services/AutoContext.js | 19 +++++++++++++++++-- test/AutoContext.test.js | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/services/AutoContext.js b/src/services/AutoContext.js index c1ae52b..2861860 100644 --- a/src/services/AutoContext.js +++ b/src/services/AutoContext.js @@ -11,11 +11,26 @@ export default class AutoContext { let match; while ((match = regex.exec(prompt)) !== null) { - if (!filePaths.includes(match[1])) { + if (!filePaths.includes(match[1]) && !this.isInsideLiquidComment(prompt, match.index)) { filePaths.push(match[1]); } } return filePaths; } -} \ No newline at end of file + + /** + * Checks if a file path is inside a liquid comment tag. + * + * @param {string} prompt - The input prompt containing file paths. + * @param {number} index - The index of the file path in the prompt. + * @returns {boolean} True if the file path is inside a liquid comment tag, false otherwise. + */ + static isInsideLiquidComment(prompt, index) { + const before = prompt.slice(0, index); + const after = prompt.slice(index); + const openTag = before.lastIndexOf('{% comment %}'); + const closeTag = before.lastIndexOf('{% endcomment %}'); + return openTag > closeTag && after.includes('{% endcomment %}'); + } +} diff --git a/test/AutoContext.test.js b/test/AutoContext.test.js index c5ef023..e207fd6 100644 --- a/test/AutoContext.test.js +++ b/test/AutoContext.test.js @@ -51,6 +51,21 @@ describe('AutoContext.call', () => { assert.deepStrictEqual([ '/src/services/AutoContext.js' ], result) + }) + + describe('when the prompt mentions paths inside liquid comment tags', () => { + let prompt = ` + Instructions here + + {% comment %} + // this file shouldn't be included in the prompt sent to the LLM + See file /abc/test.txt + {% endcomment %} + ` + + it('excludes paths inside liquid comment tags', () => { + const result = AutoContext.call(prompt) + assert.deepStrictEqual([], result) }) }) })