Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto context to exclude file paths inside liquid comment tags #69

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %}');
}
}
16 changes: 16 additions & 0 deletions test/AutoContext.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import assert from 'assert';
import AutoContext from '../src/services/AutoContext.js';

Expand Down Expand Up @@ -51,6 +52,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)
})
})
})
1 change: 1 addition & 0 deletions test/ExtractOperationsService.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import assert from 'assert'
import { ExtractOperationsService } from '../src/services/ExtractOperationsService.js'

Expand Down
3 changes: 2 additions & 1 deletion test/FileService.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import { FileService } from '../src/services/FileService.js';
import fs from 'fs';
import assert from 'assert';
Expand Down Expand Up @@ -57,4 +58,4 @@ describe('FileService', () => {
assert.strictEqual(exists, false);
});
});
});
});
1 change: 1 addition & 0 deletions test/Main.test.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
3 changes: 2 additions & 1 deletion test/OpenAiGptService.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import assert from 'assert';
import sinon from 'sinon';
import OpenAiGptService from '../src/services/OpenAiGptService.js';
Expand Down Expand Up @@ -121,4 +122,4 @@ describe('OpenAiGptService', () => {

sinon.assert.callCount(openaiStub, models.length);
});
});
});
1 change: 1 addition & 0 deletions test/PromptContext.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import assert from 'assert';
import sinon from 'sinon';
import PromptContext from '../src/services/PromptContext.js';
Expand Down
3 changes: 2 additions & 1 deletion test/PromptrService_call.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import assert from 'assert';
import sinon from 'sinon';
import PromptrService from '../src/services/PromptrService.js';
Expand Down Expand Up @@ -103,4 +104,4 @@ describe('PromptrService', () => {
});
});

});
});
3 changes: 2 additions & 1 deletion test/PromptrService_executeMode.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import assert from 'assert';
import sinon from 'sinon';
import PromptrService from '../src/services/PromptrService.js';
Expand Down Expand Up @@ -88,4 +89,4 @@ describe('PromptrService', () => {
});
});

});
});
3 changes: 2 additions & 1 deletion test/PromptrService_shouldRefactor.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import assert from 'assert';
import sinon from 'sinon';
import PromptrService from '../src/services/PromptrService.js';
Expand Down Expand Up @@ -32,4 +33,4 @@ describe('PromptrService', () => {
})
})

});
});
3 changes: 2 additions & 1 deletion test/cliState.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import assert from 'assert';
import CliState from '../src/CliState.js';

Expand All @@ -10,4 +11,4 @@ describe('CliState', () => {
assert.strictEqual(opts.prompt, 'Test prompt');
assert.strictEqual(opts.verbose, true);
});
});
});
3 changes: 2 additions & 1 deletion test/refactorResultProcessor.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, it } from 'mocha'
import fs from 'fs';
import path from 'path';
import assert from 'assert';
Expand Down Expand Up @@ -146,4 +147,4 @@ describe('RefactorResultProcessor', () => {
if (fs.existsSync(path.resolve('test/testDirNew/nested'))) fs.rmdirSync(path.resolve('test/testDirNew'), { recursive: true })
});
});
});
});
Loading