Skip to content

Commit

Permalink
Fix auto context to exclude file paths inside liquid comment tags
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ferrislucas committed Nov 3, 2024
1 parent d566592 commit 2f7bd6e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/services/AutoContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
* 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 %}');
}
}
15 changes: 15 additions & 0 deletions test/AutoContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit 2f7bd6e

Please sign in to comment.