Skip to content

Commit 1a09518

Browse files
committed
Refactor prompt builder API and Code at Cursor instruction handling, update callers, and add file exclusion support
- should use prompt builder instead of `apps/editor/src/views/prompt/backend/message-handlers/utils/build-cli-prompt.ts` - assess param names of PromptBuilder.build_prompt, change if needed - remove separator param, always add it. For code at cursor, adjust instructions so it '# Task' appears above 'Find correct...', but above 'Your response must begin...' we place '# Output formatting' heading - in 'code at cursor' current file is repeated (when in context) - in 'code at cursor' if context is empty, there is too much space below '# Files' and the current file with cursor └── apps └── editor └── src ├── commands │ ├── code-at-cursor-command │ │ └── utils │ │ └── perform-code-at-cursor.ts │ ├── copy-markdown-command.ts │ └── generate-commit-message-commands │ └── utils │ └── build-commit-message-prompt.ts ├── constants │ ├── edit-format-instructions.ts │ └── instructions.ts ├── utils │ ├── agentic-cli-invocation │ │ └── invoke-agentic-cli.ts │ ├── files-collector.ts │ ├── git-repository-utils.ts │ └── prompt-builder.ts └── views └── prompt └── backend └── message-handlers ├── handle-autofill.ts ├── handle-copy-prompt.ts ├── handle-get-edit-format-instructions.ts ├── handle-invoke-agentic-cli.ts ├── handle-make-api-call.ts ├── handle-preview-prompt.ts ├── handle-preview-web-configuration.ts └── utils ├── build-cli-prompt.ts └── build-prompt-payload.ts
1 parent 4ca3b5e commit 1a09518

11 files changed

Lines changed: 69 additions & 76 deletions

File tree

‎apps/editor/src/commands/code-at-cursor-command/utils/perform-code-at-cursor.ts‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import axios from 'axios'
44
import he from 'he'
55
import { send_llm_message } from '../../../utils/send-llm-message'
66
import {
7-
code_at_cursor_instructions,
7+
code_at_cursor_system_instructions,
8+
code_at_cursor_user_instructions,
89
code_at_cursor_instructions_for_chatbots
910
} from '../../../constants/instructions'
1011
import { FilesCollector } from '../../../utils/files-collector'
@@ -225,12 +226,14 @@ export const perform_code_at_cursor = async (params: {
225226
)
226227

227228
const active_file_path = vscode.workspace.asRelativePath(document.uri)
229+
const active_file_path_fs = document.uri.fsPath
228230
const row = position.line
229231
const column = position.character
230232

231233
const collected = await FilesCollector.collect_files({
232234
workspace_provider: params.workspace_provider,
233-
open_editors_provider: params.open_editors_provider
235+
open_editors_provider: params.open_editors_provider,
236+
exclude_paths: [active_file_path_fs]
234237
})
235238

236239
if (action == 'copy' || action == 'autofill') {
@@ -240,9 +243,9 @@ export const perform_code_at_cursor = async (params: {
240243
column
241244
})
242245

243-
const { part1, part2 } = PromptBuilder.build_prompt({
244-
other_files: collected.other_files,
245-
recent_files: collected.recent_files,
246+
const { full_prompt: chatbot_prompt } = PromptBuilder.build_prompt({
247+
files_context_part1: collected.other_files,
248+
files_context_part2: collected.recent_files,
246249
active_file: {
247250
filepath: active_file_path,
248251
content: `${text_before_cursor}${
@@ -251,11 +254,10 @@ export const perform_code_at_cursor = async (params: {
251254
: '<missing_text>'
252255
}${text_after_cursor}`
253256
},
254-
system_instructions: chatbot_instructions
257+
system_instructions: chatbot_instructions,
258+
user_instructions: code_at_cursor_user_instructions
255259
})
256260

257-
const chatbot_prompt = `${part1}\n\n${part2}`
258-
259261
if (action === 'copy') {
260262
await vscode.env.clipboard.writeText(chatbot_prompt)
261263
vscode.window.showInformationMessage(
@@ -379,8 +381,8 @@ export const perform_code_at_cursor = async (params: {
379381
const abort_controller = new AbortController()
380382

381383
const { part1, part2 } = PromptBuilder.build_prompt({
382-
other_files: collected.other_files,
383-
recent_files: collected.recent_files,
384+
files_context_part1: collected.other_files,
385+
files_context_part2: collected.recent_files,
384386
active_file: {
385387
filepath: active_file_path,
386388
content: `${text_before_cursor}${
@@ -389,7 +391,8 @@ export const perform_code_at_cursor = async (params: {
389391
: '<missing_text>'
390392
}${text_after_cursor}`
391393
},
392-
system_instructions: code_at_cursor_instructions
394+
system_instructions: code_at_cursor_system_instructions,
395+
user_instructions: code_at_cursor_user_instructions
393396
})
394397

395398
const user_content = build_user_content({
@@ -496,7 +499,6 @@ export const perform_code_at_cursor = async (params: {
496499

497500
decoded_completion = decoded_completion.trim()
498501

499-
const active_file_path_fs = document.uri.fsPath
500502
const workspace_root =
501503
params.workspace_provider.get_workspace_root_for_file(
502504
active_file_path_fs

‎apps/editor/src/commands/copy-markdown-command.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ export const copy_markdown_commands = (
4141

4242
const context_text =
4343
PromptBuilder.build_prompt({
44-
other_files,
45-
recent_files,
46-
separator: true
44+
files_context_part1: other_files,
45+
files_context_part2: recent_files,
4746
}).full_prompt + '\n'
4847
await vscode.env.clipboard.writeText(context_text)
4948
vscode.window.showInformationMessage(
@@ -125,8 +124,7 @@ export const copy_markdown_commands = (
125124

126125
context_text =
127126
PromptBuilder.build_prompt({
128-
other_files: context_text,
129-
separator: true
127+
files_context: context_text,
130128
}).full_prompt + '\n'
131129
await vscode.env.clipboard.writeText(context_text)
132130
vscode.window.showInformationMessage(

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
export const code_at_cursor_instructions =
2-
'Find correct replacement for the <missing_text> symbol. Respond with replacement text within "replacement" XML tags, without explanations or any other text.\nExample:\n<replacement>!== undefined</replacement>'
1+
export const code_at_cursor_system_instructions =
2+
'# Output formatting\n\nRespond with replacement text within "replacement" XML tags, without explanations or any other text.\nExample:\n<replacement>!== undefined</replacement>'
3+
4+
export const code_at_cursor_user_instructions =
5+
'Find correct replacement text for the <missing_text> symbol.'
36

47
export const code_at_cursor_instructions_for_chatbots = (params: {
58
file_path: string
69
row: number
710
column: number
8-
}) => `Your response must begin with a markdown heading identifying the file and the cursor position, followed by a markdown code block containing the replacement text, followed by a brief explanation. The heading must be: "### Code at cursor: \`${
11+
}) => `# Output formatting\n\nYour response must begin with a markdown heading identifying the file and the cursor position, followed by a markdown code block containing the replacement text, followed by a brief explanation. The heading must be: "### Code at cursor: \`${
912
params.file_path
1013
}\` (${params.row + 1}:${
1114
params.column + 1
@@ -17,11 +20,7 @@ export const code_at_cursor_instructions_for_chatbots = (params: {
1720
!== undefined
1821
\`\`\`
1922
20-
The variable is possibly not defined.
21-
22-
---
23-
24-
Find correct replacement text for the <missing_text> symbol.`
23+
The variable is possibly not defined.`
2524

2625
export const patch_repair_task_instructions =
2726
'Apply the attached changes to the file without explanations or any other text.'

‎apps/editor/src/utils/files-collector.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export namespace FilesCollector {
1212
workspace_provider: WorkspaceProvider
1313
open_editors_provider?: OpenEditorsProvider
1414
additional_paths?: string[]
15+
exclude_paths?: string[]
1516
shrink?: boolean
1617
}): Promise<{ other_files: string; recent_files: string }> => {
1718
const workspace_roots = params.workspace_provider.get_workspace_roots()
@@ -33,7 +34,9 @@ export namespace FilesCollector {
3334
...additional_paths
3435
)
3536

36-
const context_files = [...new Set(context_files_list)]
37+
const context_files = [...new Set(context_files_list)].filter(
38+
(p) => !(params.exclude_paths ?? []).includes(p)
39+
)
3740

3841
// Sort context files based on modification time and selection timestamp
3942
const { other_files: other_paths, recent_files: recent_paths } =

‎apps/editor/src/utils/prompt-builder.ts‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,13 @@ export namespace PromptBuilder {
6060
}
6161

6262
export const build_prompt = (params: {
63-
other_files?: string
64-
recent_files?: string
65-
context_text?: string
63+
files_context_part1?: string
64+
files_context_part2?: string
65+
files_context?: string
6666
active_file?: { filepath: string; content: string }
6767
skill_definitions?: string
6868
system_instructions?: string
6969
user_instructions?: string
70-
separator?: boolean
7170
}): { part1: string; part2: string; full_prompt: string } => {
7271
let part1 = ''
7372
let part2 = ''
@@ -78,25 +77,29 @@ export namespace PromptBuilder {
7877
active_file_context = build_file_context(params.active_file)
7978
}
8079

81-
if (params.context_text !== undefined) {
82-
if (params.context_text || active_file_context) {
80+
if (params.files_context !== undefined) {
81+
if (params.files_context || active_file_context) {
8382
full_prompt += `# Files\n\n`
84-
if (params.context_text) {
85-
full_prompt += params.context_text
83+
if (params.files_context) {
84+
full_prompt += params.files_context
8685
}
8786
if (active_file_context) {
8887
full_prompt += active_file_context
8988
}
9089
}
9190
} else {
9291
const has_files = !!(
93-
params.other_files ||
94-
params.recent_files ||
92+
params.files_context_part1 ||
93+
params.files_context_part2 ||
9594
active_file_context
9695
)
9796
if (has_files) {
98-
part1 = `# Files\n\n${params.other_files || ''}`
99-
part2 += `${params.recent_files || ''}${active_file_context}`
97+
if (params.files_context_part1) {
98+
part1 = `# Files\n\n${params.files_context_part1}`
99+
part2 += `${params.files_context_part2 || ''}${active_file_context}`
100+
} else {
101+
part2 += `# Files\n\n${params.files_context_part2 || ''}${active_file_context}`
102+
}
100103
full_prompt = part1 + part2
101104
}
102105
}
@@ -115,8 +118,7 @@ export namespace PromptBuilder {
115118
}
116119

117120
if (
118-
params.separator &&
119-
(part1.length > 0 || part2.length > 0 || full_prompt.length > 0)
121+
part1.length > 0 || part2.length > 0 || full_prompt.length > 0
120122
) {
121123
part2 = part2.trimEnd() + '\n\n# Task\n\n'
122124
full_prompt = full_prompt.trimEnd() + '\n\n# Task\n\n'

‎apps/editor/src/views/prompt/backend/message-handlers/handle-autofill.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,11 @@ export const handle_autofill = async (params: {
9393
}
9494

9595
const { full_prompt: text } = PromptBuilder.build_prompt({
96-
other_files,
97-
recent_files,
96+
files_context_part1: other_files,
97+
files_context_part2: recent_files,
9898
skill_definitions,
9999
system_instructions: formatted_system_instructions,
100100
user_instructions,
101-
separator: true
102101
})
103102

104103
const prompt_type = params.prompt_view_provider.web_prompt_type

‎apps/editor/src/views/prompt/backend/message-handlers/handle-copy-prompt.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ export const handle_copy_prompt = async (params: {
4343
}
4444

4545
const build_result = PromptBuilder.build_prompt({
46-
other_files,
47-
recent_files,
46+
files_context_part1: other_files,
47+
files_context_part2: recent_files,
4848
skill_definitions,
4949
system_instructions: formatted_system_instructions,
5050
user_instructions,
51-
separator: true
5251
})
5352
text = build_result.full_prompt
5453

‎apps/editor/src/views/prompt/backend/message-handlers/handle-make-api-call.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,11 @@ export const handle_make_api_call = async (
108108
''
109109

110110
const { part1, part2 } = PromptBuilder.build_prompt({
111-
other_files,
112-
recent_files,
111+
files_context_part1: other_files,
112+
files_context_part2: recent_files,
113113
skill_definitions,
114114
system_instructions: formatted_system_instructions,
115115
user_instructions: processed_instructions,
116-
separator: true
117116
})
118117
user_content = build_user_content({
119118
provider,

‎apps/editor/src/views/prompt/backend/message-handlers/handle-preview-prompt.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ export const handle_preview_prompt = async (params: {
6767
}
6868

6969
const build_result = PromptBuilder.build_prompt({
70-
other_files,
71-
recent_files,
70+
files_context_part1: other_files,
71+
files_context_part2: recent_files,
7272
skill_definitions,
7373
system_instructions: formatted_system_instructions,
7474
user_instructions,
75-
separator: true
7675
})
7776
text = build_result.full_prompt
7877
}

‎apps/editor/src/views/prompt/backend/message-handlers/handle-preview-web-configuration.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ export const handle_preview_web_configuration = async (
4545
}
4646

4747
const { full_prompt: built_prompt } = PromptBuilder.build_prompt({
48-
other_files,
49-
recent_files,
48+
files_context_part1: other_files,
49+
files_context_part2: recent_files,
5050
skill_definitions,
5151
system_instructions: formatted_system_instructions,
5252
user_instructions,
53-
separator: true
5453
})
5554
const text_to_send = built_prompt
5655

0 commit comments

Comments
 (0)