From 2f7bd6e10a6e9129a50216f8a46f77e6652b6bbf Mon Sep 17 00:00:00 2001 From: Ferris Lucas Date: Sat, 2 Nov 2024 20:33:53 -0500 Subject: [PATCH 1/2] 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) }) }) }) From 133eef1c6f06a16b8bb243ba5650ee1ca554d506 Mon Sep 17 00:00:00 2001 From: Ferris Lucas Date: Sat, 2 Nov 2024 21:05:11 -0500 Subject: [PATCH 2/2] --- test/AutoContext.test.js | 1 + test/ExtractOperationsService.test.js | 1 + test/FileService.test.js | 3 ++- test/Main.test.js | 1 + test/OpenAiGptService.test.js | 3 ++- test/PromptContext.test.js | 1 + test/PromptrService_call.test.js | 3 ++- test/PromptrService_executeMode.test.js | 3 ++- test/PromptrService_shouldRefactor.test.js | 3 ++- test/cliState.test.js | 3 ++- test/refactorResultProcessor.test.js | 3 ++- 11 files changed, 18 insertions(+), 7 deletions(-) diff --git a/test/AutoContext.test.js b/test/AutoContext.test.js index e207fd6..b2abdd1 100644 --- a/test/AutoContext.test.js +++ b/test/AutoContext.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import assert from 'assert'; import AutoContext from '../src/services/AutoContext.js'; diff --git a/test/ExtractOperationsService.test.js b/test/ExtractOperationsService.test.js index d823918..9f91113 100644 --- a/test/ExtractOperationsService.test.js +++ b/test/ExtractOperationsService.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import assert from 'assert' import { ExtractOperationsService } from '../src/services/ExtractOperationsService.js' diff --git a/test/FileService.test.js b/test/FileService.test.js index 0db5b16..178464e 100644 --- a/test/FileService.test.js +++ b/test/FileService.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import { FileService } from '../src/services/FileService.js'; import fs from 'fs'; import assert from 'assert'; @@ -57,4 +58,4 @@ describe('FileService', () => { assert.strictEqual(exists, false); }); }); -}); \ No newline at end of file +}); diff --git a/test/Main.test.js b/test/Main.test.js index 03fabd4..283c333 100644 --- a/test/Main.test.js +++ b/test/Main.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import assert from 'assert' import Main from '../src/Main.js' import PromptrService from '../src/services/PromptrService.js' diff --git a/test/OpenAiGptService.test.js b/test/OpenAiGptService.test.js index ec990b7..df35645 100644 --- a/test/OpenAiGptService.test.js +++ b/test/OpenAiGptService.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import assert from 'assert'; import sinon from 'sinon'; import OpenAiGptService from '../src/services/OpenAiGptService.js'; @@ -121,4 +122,4 @@ describe('OpenAiGptService', () => { sinon.assert.callCount(openaiStub, models.length); }); -}); \ No newline at end of file +}); diff --git a/test/PromptContext.test.js b/test/PromptContext.test.js index cebcf6b..280b49c 100644 --- a/test/PromptContext.test.js +++ b/test/PromptContext.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import assert from 'assert'; import sinon from 'sinon'; import PromptContext from '../src/services/PromptContext.js'; diff --git a/test/PromptrService_call.test.js b/test/PromptrService_call.test.js index 733f648..de2e1ad 100644 --- a/test/PromptrService_call.test.js +++ b/test/PromptrService_call.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import assert from 'assert'; import sinon from 'sinon'; import PromptrService from '../src/services/PromptrService.js'; @@ -103,4 +104,4 @@ describe('PromptrService', () => { }); }); -}); \ No newline at end of file +}); diff --git a/test/PromptrService_executeMode.test.js b/test/PromptrService_executeMode.test.js index 8dc7ec6..fda04a4 100644 --- a/test/PromptrService_executeMode.test.js +++ b/test/PromptrService_executeMode.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import assert from 'assert'; import sinon from 'sinon'; import PromptrService from '../src/services/PromptrService.js'; @@ -88,4 +89,4 @@ describe('PromptrService', () => { }); }); -}); \ No newline at end of file +}); diff --git a/test/PromptrService_shouldRefactor.test.js b/test/PromptrService_shouldRefactor.test.js index 72fd9c3..0dc1ec6 100644 --- a/test/PromptrService_shouldRefactor.test.js +++ b/test/PromptrService_shouldRefactor.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import assert from 'assert'; import sinon from 'sinon'; import PromptrService from '../src/services/PromptrService.js'; @@ -32,4 +33,4 @@ describe('PromptrService', () => { }) }) -}); \ No newline at end of file +}); diff --git a/test/cliState.test.js b/test/cliState.test.js index 5a27087..583a319 100644 --- a/test/cliState.test.js +++ b/test/cliState.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import assert from 'assert'; import CliState from '../src/CliState.js'; @@ -10,4 +11,4 @@ describe('CliState', () => { assert.strictEqual(opts.prompt, 'Test prompt'); assert.strictEqual(opts.verbose, true); }); -}); \ No newline at end of file +}); diff --git a/test/refactorResultProcessor.test.js b/test/refactorResultProcessor.test.js index 676f6dc..6e37756 100644 --- a/test/refactorResultProcessor.test.js +++ b/test/refactorResultProcessor.test.js @@ -1,3 +1,4 @@ +import { describe, it } from 'mocha' import fs from 'fs'; import path from 'path'; import assert from 'assert'; @@ -146,4 +147,4 @@ describe('RefactorResultProcessor', () => { if (fs.existsSync(path.resolve('test/testDirNew/nested'))) fs.rmdirSync(path.resolve('test/testDirNew'), { recursive: true }) }); }); -}); \ No newline at end of file +});