Skip to content

Commit 840f588

Browse files
committed
Refactor prompt construction to move instructions after context and remove unused exclude_path parameter
1 parent 4e09ce1 commit 840f588

9 files changed

Lines changed: 42 additions & 66 deletions

File tree

apps/editor/src/commands/code-at-cursor-commands.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ const perform_code_at_cursor = async (params: {
363363
const document = editor.document
364364
const position = editor.selection.active
365365

366-
const document_path = document.uri.fsPath
367366
const text_before_cursor = document.getText(
368367
new vscode.Range(new vscode.Position(0, 0), position)
369368
)
@@ -376,9 +375,7 @@ const perform_code_at_cursor = async (params: {
376375
params.open_editors_provider
377376
)
378377

379-
const context_text = await files_collector.collect_files({
380-
exclude_path: document_path
381-
})
378+
const context_text = await files_collector.collect_files()
382379

383380
const payload = {
384381
before: `<files>\n${context_text}<file path="${vscode.workspace.asRelativePath(
@@ -387,7 +384,7 @@ const perform_code_at_cursor = async (params: {
387384
after: `${text_after_cursor}\n]]>\n</file>\n</files>`
388385
}
389386

390-
const content = `${code_at_cursor_instructions}\n${payload.before}${
387+
const content = `${payload.before}${
391388
completion_instructions
392389
? `<missing_text>${completion_instructions}</missing_text>`
393390
: '<missing_text>'

apps/editor/src/constants/instructions.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
export const code_at_cursor_instructions =
22
'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>\n'
33

4-
export const code_at_cursor_instructions_for_panel = (
5-
file_path: string,
6-
row: number,
4+
export const code_at_cursor_instructions_for_panel = (params: {
5+
file_path: string
6+
row: number
77
column: number
8-
) =>
9-
`Find correct replacement text for the <missing_text> symbol.
8+
}) => `Find correct replacement text for the <missing_text> symbol.
109
<system>
11-
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: \`${file_path}\` (${
12-
row + 1
13-
}:${
14-
column + 1
15-
})". Always refer to the symbol "<missing_text>" as "cursor position" and "replacement" as "completion". Example:
10+
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+
params.file_path
12+
}\` (${params.row + 1}:${
13+
params.column + 1
14+
})". Always refer to the symbol "<missing_text>" as "cursor position" and "replacement" as "completion". Example:
1615
17-
### Code at cursor: \`${file_path}\` (${row + 1}:${column + 1})
16+
### Code at cursor: \`${params.file_path}\` (${params.row + 1}:${params.column + 1})
1817
1918
\`\`\`typescript
2019
!== undefined

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export class FilesCollector {
2121
}
2222

2323
async collect_files(params?: {
24-
exclude_path?: string
2524
additional_paths?: string[]
2625
no_context?: boolean
2726
compact?: boolean
@@ -62,7 +61,6 @@ export class FilesCollector {
6261
let collected_text = ''
6362

6463
for (const file_path of sorted_context_files) {
65-
if (params?.exclude_path && params.exclude_path == file_path) continue
6664
try {
6765
if (!fs.existsSync(file_path)) continue
6866
const stats = fs.statSync(file_path)

apps/editor/src/views/panel/backend/message-handlers/handle-code-at-cursor.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ export const handle_code_at_cursor = async (
317317
const document = editor.document
318318
const position = editor.selection.active
319319

320-
const document_path = document.uri.fsPath
321320
const text_before_cursor = document.getText(
322321
new vscode.Range(new vscode.Position(0, 0), position)
323322
)
@@ -326,11 +325,11 @@ export const handle_code_at_cursor = async (
326325
)
327326

328327
const relative_path = vscode.workspace.asRelativePath(document.uri)
329-
const main_instructions = code_at_cursor_instructions_for_panel(
330-
relative_path,
331-
position.line,
332-
position.character
333-
)
328+
const main_instructions = code_at_cursor_instructions_for_panel({
329+
file_path: relative_path,
330+
row: position.line,
331+
column: position.character
332+
})
334333

335334
let processed_completion_instructions = completion_instructions
336335
let skill_definitions = ''
@@ -406,20 +405,18 @@ export const handle_code_at_cursor = async (
406405
panel_provider.open_editors_provider
407406
)
408407

409-
const context_text = await files_collector.collect_files({
410-
exclude_path: document_path
411-
})
408+
const context_text = await files_collector.collect_files()
412409

413410
const payload = {
414411
before: `<files>\n${context_text}<file path="${relative_path}">\n<![CDATA[\n${text_before_cursor}`,
415412
after: `${text_after_cursor}\n]]>\n</file>\n</files>`
416413
}
417414

418-
const content = `${main_instructions}\n${skill_definitions}${payload.before}${
415+
const content = `${payload.before}${
419416
processed_completion_instructions
420417
? `<missing_text>${processed_completion_instructions}</missing_text>`
421418
: '<missing_text>'
422-
}${payload.after}\n${main_instructions}`
419+
}${payload.after}\n${skill_definitions}${main_instructions}`
423420

424421
let user_content: any = content
425422

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,16 @@ export const handle_copy_prompt = async (params: {
7777
new vscode.Range(position, document.positionAt(document.getText().length))
7878
)
7979

80-
const context_text = await files_collector.collect_files({
81-
exclude_path: active_path
82-
})
80+
const context_text = await files_collector.collect_files()
8381

8482
const workspace_folder = vscode.workspace.workspaceFolders?.[0].uri.fsPath
8583
const relative_path = active_path.replace(workspace_folder + '/', '')
8684

87-
const system_instructions = code_at_cursor_instructions_for_panel(
88-
relative_path,
89-
position.line,
90-
position.character
91-
)
85+
const system_instructions = code_at_cursor_instructions_for_panel({
86+
file_path: relative_path,
87+
row: position.line,
88+
column: position.character
89+
})
9290

9391
let processed_completion_instructions = final_instruction
9492
let skill_definitions = ''
@@ -158,7 +156,7 @@ export const handle_copy_prompt = async (params: {
158156
? `<missing_text>${processed_completion_instructions}</missing_text>`
159157
: '<missing_text>'
160158

161-
const text = `${system_instructions}\n${skill_definitions}<files>\n${context_text}<file path="${relative_path}">\n<![CDATA[\n${text_before_cursor}${missing_text_tag}${text_after_cursor}\n]]>\n</file>\n</files>\n${system_instructions}`
159+
const text = `<files>\n${context_text}<file path="${relative_path}">\n<![CDATA[\n${text_before_cursor}${missing_text_tag}${text_after_cursor}\n]]>\n</file>\n</files>\n${skill_definitions}${system_instructions}`
162160

163161
vscode.env.clipboard.writeText(text.trim())
164162
} else if (!is_in_code_completions_prompt_type) {
@@ -268,9 +266,7 @@ export const handle_copy_prompt = async (params: {
268266
}
269267

270268
const text = context_text
271-
? `${
272-
system_instructions_xml ? system_instructions_xml + '\n' : ''
273-
}${skill_definitions}<files>\n${context_text}</files>\n${
269+
? `<files>\n${context_text}</files>\n${skill_definitions}${
274270
system_instructions_xml ? system_instructions_xml + '\n' : ''
275271
}${processed_instructions}`
276272
: `${

apps/editor/src/views/panel/backend/message-handlers/handle-edit-context.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,7 @@ export const handle_edit_context = async (
474474
system_instructions_xml = `<system>\n${edit_format_instructions}\n</system>`
475475
}
476476

477-
const content = `${
478-
system_instructions_xml ? system_instructions_xml + '\n' : ''
479-
}${skill_definitions}${files}\n${
477+
const content = `${files}\n${skill_definitions}${
480478
system_instructions_xml ? system_instructions_xml + '\n' : ''
481479
}${processed_instructions}`
482480

apps/editor/src/views/panel/backend/message-handlers/handle-preview-preset.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ export const handle_preview_preset = async (
5959
new vscode.Range(position, document.positionAt(document.getText().length))
6060
)
6161

62-
const context_text = await files_collector.collect_files({
63-
exclude_path: active_path
64-
})
62+
const context_text = await files_collector.collect_files()
6563

6664
const workspace_folder = vscode.workspace.workspaceFolders?.[0].uri.fsPath
6765
const relative_path = active_path!.replace(workspace_folder + '/', '')
@@ -138,7 +136,7 @@ export const handle_preview_preset = async (
138136
? `<missing_text>${processed_completion_instructions}</missing_text>`
139137
: '<missing_text>'
140138

141-
text_to_send = `${system_instructions}\n${skill_definitions}<files>\n${context_text}<file path="${relative_path}">\n<![CDATA[\n${text_before_cursor}${missing_text_tag}${text_after_cursor}\n]]>\n</file>\n</files>\n${system_instructions}`
139+
text_to_send = `<files>\n${context_text}<file path="${relative_path}">\n<![CDATA[\n${text_before_cursor}${missing_text_tag}${text_after_cursor}\n]]>\n</file>\n</files>\n${skill_definitions}${system_instructions}`
142140
} else if (panel_provider.web_prompt_type != 'code-at-cursor') {
143141
let instructions = apply_preset_affixes_to_instruction({
144142
instruction: current_instructions,
@@ -244,9 +242,7 @@ export const handle_preview_preset = async (
244242
}
245243

246244
text_to_send = context_text
247-
? `${
248-
system_instructions_xml ? system_instructions_xml + '\n' : ''
249-
}${skill_definitions}<files>\n${context_text}</files>\n${
245+
? `<files>\n${context_text}</files>\n${skill_definitions}${
250246
system_instructions_xml ? system_instructions_xml + '\n' : ''
251247
}${processed_instructions}`
252248
: `${

apps/editor/src/views/panel/backend/message-handlers/handle-prune-context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ export const handle_prune_context = async (
426426
config_prune_instructions_prefix || prune_context_instructions_prefix
427427
const system_instructions_xml = `${instructions_to_use}\n${prune_context_format}`
428428

429-
const content = `${system_instructions_xml}\n${skill_definitions}${files}\n${system_instructions_xml}\n${processed_instructions}`
429+
const content = `${files}\n${skill_definitions}${system_instructions_xml}\n${processed_instructions}`
430430

431431
let user_content: any = content
432432

apps/editor/src/views/panel/backend/message-handlers/handle-send-to-browser.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ export const handle_send_to_browser = async (params: {
6868
}
6969

7070
const active_editor = vscode.window.activeTextEditor
71-
const active_path = active_editor?.document.uri.fsPath
7271

7372
if (is_in_code_completions_mode && !active_editor) {
7473
vscode.window.showWarningMessage(dictionary.warning_message.NO_EDITOR_OPEN)
@@ -183,28 +182,26 @@ export const handle_send_to_browser = async (params: {
183182
})
184183
}
185184

186-
const context_text = await files_collector.collect_files({
187-
exclude_path: active_path
188-
})
185+
const context_text = await files_collector.collect_files()
189186

190187
const relative_path = vscode.workspace.asRelativePath(document.uri)
191188

192-
const main_instructions = code_at_cursor_instructions_for_panel(
193-
relative_path,
194-
position.line,
195-
position.character
196-
)
189+
const main_instructions = code_at_cursor_instructions_for_panel({
190+
file_path: relative_path,
191+
row: position.line,
192+
column: position.character
193+
})
197194

198195
const payload = {
199196
before: `<files>\n${context_text}<file path="${relative_path}">\n<![CDATA[\n${text_before_cursor}`,
200197
after: `${text_after_cursor}\n]]>\n</file>\n</files>`
201198
}
202199

203-
const text = `${main_instructions}\n${skill_definitions}${payload.before}${
200+
const text = `${payload.before}${
204201
processed_completion_instructions
205202
? `<missing_text>${processed_completion_instructions}</missing_text>`
206203
: '<missing_text>'
207-
}${payload.after}\n${main_instructions}`
204+
}${payload.after}\n${skill_definitions}${main_instructions}`
208205

209206
const chats = resolved_preset_names.flatMap((preset_name) => {
210207
return Array.from({ length: params.invocation_count }).map(() => ({
@@ -332,9 +329,7 @@ export const handle_send_to_browser = async (params: {
332329

333330
return {
334331
text: context_text
335-
? `${
336-
system_instructions_xml ? system_instructions_xml + '\n' : ''
337-
}${skill_definitions}<files>\n${context_text}</files>\n${
332+
? `<files>\n${context_text}</files>\n${skill_definitions}${
338333
system_instructions_xml ? system_instructions_xml + '\n' : ''
339334
}${processed_instructions}`
340335
: `${

0 commit comments

Comments
 (0)