Skip to content

Commit 2258d1c

Browse files
committed
Update relevant files parser to allow code blocks containing valid file paths
- code block (Triple backticks) should be allowed under one condition: they contain valid paths - create case file for this [Pasted text]
1 parent 4815e14 commit 2258d1c

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/relevant-files-parser/relevant-files-parser.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ export const parse_relevant_files_from_response = (params: {
1010
const trimmed_response = params.response.trim()
1111

1212
if (trimmed_response.includes('```')) {
13-
return null
13+
const code_blocks = trimmed_response.match(/```[\s\S]*?(?:```|$)/g)
14+
const has_valid_path_in_code_blocks = code_blocks?.some((block) =>
15+
params.workspace_files.some((file) => block.includes(file))
16+
)
17+
18+
if (!has_valid_path_in_code_blocks) {
19+
return null
20+
}
1421
}
1522

1623
const found_paths = new Set<string>()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
`src/hello.ts`
2+
3+
```typescript
4+
const x = 1;
5+
6+
```
7+
8+
const x = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Here are files:
2+
3+
src/hello.ts

‎apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/relevant-files-parser/test/relevant-files-parser.spec.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parse_response } from '../../..'
22
import * as fs from 'fs'
33
import * as path from 'path'
4+
import { parse_relevant_files_from_response } from '../relevant-files-parser'
45

56
describe('response-parser', () => {
67
const load_test_case_file = (test_case: string, filename: string): string => {
@@ -42,5 +43,30 @@ describe('response-parser', () => {
4243
file_paths: ['src/hello.ts', 'src/welcome.ts', 'src/main.ts']
4344
})
4445
})
46+
47+
it('allows code blocks if they contain valid paths', () => {
48+
const test_case = 'code-blocks-valid'
49+
const text = load_test_case_file(test_case, 'relevant-files.txt')
50+
const result = parse_relevant_files_from_response({
51+
response: text,
52+
workspace_files: ['src/hello.ts', 'src/main.ts']
53+
})
54+
55+
expect(result).toMatchObject({
56+
type: 'relevant-files',
57+
file_paths: ['src/hello.ts']
58+
})
59+
})
60+
61+
it('rejects responses with code blocks that do not contain valid paths', () => {
62+
const test_case = 'code-blocks-invalid'
63+
const text = load_test_case_file(test_case, 'relevant-files.txt')
64+
const result = parse_relevant_files_from_response({
65+
response: text,
66+
workspace_files: ['src/hello.ts', 'src/main.ts']
67+
})
68+
69+
expect(result).toBeNull()
70+
})
4571
})
4672
})

0 commit comments

Comments
 (0)