Skip to content

Commit 3112f3e

Browse files
committed
Update intelligent file search prompt formatting to include folder paths and restrict result parsing to bulleted file paths
- considering intelligent_file_search_format_for_prompt_view: in case 'for folder' should get this folder path in param so it is used below in examples #Fragment(apps/editor/src/constants/instructions.ts:72:1-74:31) - in #Fragment(apps/editor/src/constants/instructions.ts:75:1-77:40) place src only when prefix is not used - `apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/intelligent-file-search-results-parser/intelligent-file-search-results-parser.ts` should not use `apps/editor/src/utils/extract-paths-from-text/extract-paths-from-text.ts` as it is too broad - we only want to extract paths from bullet lists there. └── apps └── editor └── src ├── commands │ └── apply-response-command │ └── utils │ └── response-parser │ └── parsers │ └── intelligent-file-search-results-parser │ └── intelligent-file-search-results-parser.ts ├── constants │ └── instructions.ts ├── features │ └── search-files │ └── search-modes │ └── perform-intelligent-search-mode.ts └── utils └── extract-paths-from-text ├── extract-paths-from-text.spec.ts └── extract-paths-from-text.ts
1 parent 647ff81 commit 3112f3e

3 files changed

Lines changed: 66 additions & 14 deletions

File tree

‎apps/editor/src/commands/apply-response-command/utils/response-parser/parsers/intelligent-file-search-results-parser/intelligent-file-search-results-parser.ts‎

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { extract_paths_from_text } from '@/utils/extract-paths-from-text'
2-
31
export type IntelligentFileSearchResultsItem = {
42
type: 'intelligent-file-search-results'
53
file_paths: string[]
@@ -31,10 +29,60 @@ export const parse_intelligent_file_search_results = (params: {
3129
folder_path = folder_match[1]
3230
}
3331

34-
const valid_paths = extract_paths_from_text({
35-
text: params.response,
36-
workspace_files: params.workspace_files
37-
})
32+
const found_paths = new Set<string>()
33+
const lines = params.response.split('\n')
34+
for (const line of lines) {
35+
const trimmed = line.trim()
36+
if (trimmed.startsWith('-') || trimmed.startsWith('*')) {
37+
let path_str = trimmed.substring(1).trim()
38+
if (path_str.startsWith('`') && path_str.endsWith('`')) {
39+
path_str = path_str.substring(1, path_str.length - 1).trim()
40+
}
41+
if (path_str) {
42+
found_paths.add(path_str.replace(/[.?!]+$/, ''))
43+
}
44+
}
45+
}
46+
47+
const matched_files_with_index = new Map<string, number>()
48+
49+
for (const original_p of found_paths) {
50+
let p = original_p
51+
if (p.startsWith('./')) {
52+
p = p.substring(2)
53+
} else if (p.startsWith('/')) {
54+
p = p.substring(1)
55+
}
56+
while (p.startsWith('/')) {
57+
p = p.substring(1)
58+
}
59+
60+
if (!p || p === '.' || p === '..') continue
61+
62+
let idx = params.response.indexOf(original_p)
63+
if (idx == -1) {
64+
idx = params.response.indexOf(p)
65+
}
66+
const safe_idx = idx != -1 ? idx : Infinity
67+
68+
for (const file of params.workspace_files) {
69+
if (file === p || file.endsWith('/' + p)) {
70+
const current_idx = matched_files_with_index.get(file) ?? Infinity
71+
matched_files_with_index.set(file, Math.min(current_idx, safe_idx))
72+
}
73+
}
74+
}
75+
76+
const valid_paths = Array.from(matched_files_with_index.keys()).sort(
77+
(a, b) => {
78+
const idx_a = matched_files_with_index.get(a) ?? Infinity
79+
const idx_b = matched_files_with_index.get(b) ?? Infinity
80+
if (idx_a == idx_b) {
81+
return a.localeCompare(b)
82+
}
83+
return idx_a - idx_b
84+
}
85+
)
3886

3987
if (valid_paths.length == 0) {
4088
return null

‎apps/editor/src/constants/instructions.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,27 @@ export const intelligent_file_search_format = `Your response must contain paths
5757
5858
<intelligent-file-search-results>
5959
<file-path>src/index.ts</file-path>
60-
<file-path>src/hello.ts</file-path>
61-
<file-path>src/welcome.ts</file-path>
60+
<file-path>src/greetings/hello.ts</file-path>
61+
<file-path>src/greetings/welcome.ts</file-path>
6262
</intelligent-file-search-results>`
6363

6464
export const intelligent_file_search_format_for_prompt_view = (
65-
metadata?: string
66-
) => `Your response must begin with "**Intelligent file search results${
65+
metadata?: string,
66+
folder_path?: string
67+
) => {
68+
const prefix = folder_path && folder_path != '.' ? `${folder_path}/` : ''
69+
return `Your response must begin with "**Intelligent file search results${
6770
metadata || ''
6871
}:**", then list paths one under another, followed by a brief explanation. Example:
6972
7073
**Intelligent file search results${metadata || ''}:**
7174
72-
- \`src/index.ts\`
73-
- \`src/hello.ts\`
74-
- \`src/welcome.ts\`
75+
- \`${prefix || 'src/'}index.ts\`
76+
- \`${prefix || 'src/'}greetings/hello.ts\`
77+
- \`${prefix || 'src/'}greetings/welcome.ts\`
7578
7679
These files contain the core greeting logic and module exports.`
80+
}
7781

7882
export const voice_input_instructions =
7983
'Respond with a transcription of the following audio recording or text "INAUDIBLE", and nothing else.'

‎apps/editor/src/features/search-files/search-modes/perform-intelligent-search-mode.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export const perform_intelligent_search_mode = async (params: {
274274
}
275275

276276
const format_instructions =
277-
intelligent_file_search_format_for_prompt_view(metadata)
277+
intelligent_file_search_format_for_prompt_view(metadata, display_folder_path)
278278

279279
const chatbot_prompt = `# Files\n\n${md_files}# Task\n\n${base_instructions}\n\n# Output formatting\n\n${format_instructions}\n\n# Query\n\n${search_term}`
280280

0 commit comments

Comments
 (0)