Skip to content

Commit 41e3a99

Browse files
committed
Refactor prompt utility functions into a PromptBuilder namespace and update all call sites
- `apps/editor/src/utils/prompt-builder.ts` should export from namespace PromptBuilder. Update function names if necessary.
1 parent 4e3a10d commit 41e3a99

9 files changed

Lines changed: 88 additions & 84 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { t } from '@/i18n'
1313
import { build_user_content } from '../../../utils/build-user-content'
1414
import { get_code_at_cursor_api_configuration } from './get-code-at-cursor-config'
1515
import { show_ghost_text } from './show-ghost-text'
16-
import { build_prompt } from '../../../utils/prompt-builder'
16+
import { PromptBuilder } from '../../../utils/prompt-builder'
1717

1818
export const perform_code_at_cursor = async (params: {
1919
file_tree_provider: any
@@ -129,7 +129,7 @@ export const perform_code_at_cursor = async (params: {
129129

130130
const collected = await files_collector.collect_files()
131131

132-
const { part1, part2 } = build_prompt({
132+
const { part1, part2 } = PromptBuilder.build_prompt({
133133
other_files: collected.other_files,
134134
recent_files: collected.recent_files,
135135
active_file: {

‎apps/editor/src/context/context-initialization.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from '../constants/state-keys'
1919
import { SelectedFilesProvider } from './providers/selected-files/selected-files-provider'
2020
import { is_binary_file } from '../utils/is-binary'
21-
import { build_prompt, build_file_context } from '../utils/prompt-builder'
21+
import { PromptBuilder } from '../utils/prompt-builder'
2222

2323
export const token_count_emitter = new EventEmitter()
2424

@@ -230,7 +230,8 @@ export const context_initialization = async (
230230
return
231231
}
232232

233-
context_text = build_prompt({ context_text }).full_prompt + '\n'
233+
context_text =
234+
PromptBuilder.build_prompt({ context_text }).full_prompt + '\n'
234235
await vscode.env.clipboard.writeText(context_text)
235236
vscode.window.showInformationMessage(
236237
dictionary.information_message.CONTEXT_COPIED_TO_CLIPBOARD
@@ -275,7 +276,7 @@ export const context_initialization = async (
275276
}
276277

277278
if (is_binary_file(file_path, content_uint8_array)) {
278-
context_text += build_file_context({
279+
context_text += PromptBuilder.build_file_context({
279280
filepath: display_path,
280281
is_binary: true
281282
})
@@ -292,7 +293,7 @@ export const context_initialization = async (
292293
)
293294
}
294295

295-
context_text += build_file_context({
296+
context_text += PromptBuilder.build_file_context({
296297
filepath: display_path,
297298
content
298299
})
@@ -308,7 +309,8 @@ export const context_initialization = async (
308309

309310
if (context_text == '') return
310311

311-
context_text = build_prompt({ context_text }).full_prompt + '\n'
312+
context_text =
313+
PromptBuilder.build_prompt({ context_text }).full_prompt + '\n'
312314
await vscode.env.clipboard.writeText(context_text)
313315
vscode.window.showInformationMessage(
314316
dictionary.information_message
Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,77 @@
1-
export const build_file_context = (params: {
2-
filepath: string
3-
content?: string
4-
is_binary?: boolean
5-
}): string => {
6-
const display_path = params.filepath.replace(/\\/g, '/')
7-
if (params.is_binary || params.content === undefined) {
8-
return `### File: \`${display_path}\`\n\nBinary file\n\n`
1+
export namespace PromptBuilder {
2+
export const build_file_context = (params: {
3+
filepath: string
4+
content?: string
5+
is_binary?: boolean
6+
}): string => {
7+
const display_path = params.filepath.replace(/\\/g, '/')
8+
if (params.is_binary || params.content === undefined) {
9+
return `### File: \`${display_path}\`\n\nBinary file\n\n`
10+
}
11+
return `### File: \`${display_path}\`\n\n\`\`\`\n${params.content}\n\`\`\`\n\n`
912
}
10-
return `### File: \`${display_path}\`\n\n\`\`\`\n${params.content}\n\`\`\`\n\n`
11-
}
1213

13-
export const build_prompt = (params: {
14-
other_files?: string
15-
recent_files?: string
16-
context_text?: string
17-
active_file?: { filepath: string; content: string }
18-
skill_definitions?: string
19-
system_instructions?: string
20-
user_instructions?: string
21-
separator?: boolean
22-
}): { part1: string; part2: string; full_prompt: string } => {
23-
let part1 = ''
24-
let part2 = ''
25-
let full_prompt = ''
26-
27-
let active_file_context = ''
28-
if (params.active_file) {
29-
active_file_context = build_file_context(params.active_file)
30-
}
14+
export const build_prompt = (params: {
15+
other_files?: string
16+
recent_files?: string
17+
context_text?: string
18+
active_file?: { filepath: string; content: string }
19+
skill_definitions?: string
20+
system_instructions?: string
21+
user_instructions?: string
22+
separator?: boolean
23+
}): { part1: string; part2: string; full_prompt: string } => {
24+
let part1 = ''
25+
let part2 = ''
26+
let full_prompt = ''
3127

32-
if (params.context_text !== undefined) {
33-
if (params.context_text) {
34-
full_prompt += `# Files\n\n${params.context_text}`
28+
let active_file_context = ''
29+
if (params.active_file) {
30+
active_file_context = build_file_context(params.active_file)
3531
}
36-
if (active_file_context) {
37-
full_prompt += active_file_context
32+
33+
if (params.context_text !== undefined) {
34+
if (params.context_text) {
35+
full_prompt += `# Files\n\n${params.context_text}`
36+
}
37+
if (active_file_context) {
38+
full_prompt += active_file_context
39+
}
40+
} else {
41+
part1 = `# Files\n\n${params.other_files || ''}`
42+
part2 += `${params.recent_files || ''}${active_file_context}`
43+
full_prompt = part1 + part2
3844
}
39-
} else {
40-
part1 = `# Files\n\n${params.other_files || ''}`
41-
part2 += `${params.recent_files || ''}${active_file_context}`
42-
full_prompt = part1 + part2
43-
}
4445

45-
if (params.skill_definitions) {
46-
part2 += params.skill_definitions
47-
full_prompt += params.skill_definitions
48-
}
46+
if (params.skill_definitions) {
47+
part2 += params.skill_definitions
48+
full_prompt += params.skill_definitions
49+
}
4950

50-
const has_system = !!params.system_instructions
51-
const has_user = !!params.user_instructions
52-
const separator = params.separator !== false && has_system && has_user
51+
const has_system = !!params.system_instructions
52+
const has_user = !!params.user_instructions
53+
const separator = params.separator !== false && has_system && has_user
5354

54-
if (params.system_instructions) {
55-
const sys = params.system_instructions.trimEnd()
56-
if (sys) {
57-
part2 += `${sys}\n`
58-
full_prompt += `${sys}\n`
59-
if (separator) {
60-
part2 += `\n---\n\n`
61-
full_prompt += `\n---\n\n`
62-
} else {
63-
part2 += `\n\n`
64-
full_prompt += `\n\n`
55+
if (params.system_instructions) {
56+
const sys = params.system_instructions.trimEnd()
57+
if (sys) {
58+
part2 += `${sys}\n`
59+
full_prompt += `${sys}\n`
60+
if (separator) {
61+
part2 += `\n---\n\n`
62+
full_prompt += `\n---\n\n`
63+
} else {
64+
part2 += `\n\n`
65+
full_prompt += `\n\n`
66+
}
6567
}
6668
}
67-
}
6869

69-
if (params.user_instructions) {
70-
part2 += params.user_instructions
71-
full_prompt += params.user_instructions
72-
}
70+
if (params.user_instructions) {
71+
part2 += params.user_instructions
72+
full_prompt += params.user_instructions
73+
}
7374

74-
return { part1, part2, full_prompt }
75+
return { part1, part2, full_prompt }
76+
}
7577
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
show_configuration_quick_pick,
2222
map_api_configuration_to_item
2323
} from '@/utils/show-configuration-quick-pick'
24-
import { build_prompt } from '@/utils/prompt-builder'
24+
import { PromptBuilder } from '@/utils/prompt-builder'
2525

2626
const get_code_at_cursor_api_configuration = async (
2727
model_providers_manager: ModelProvidersManager,
@@ -228,7 +228,7 @@ export const handle_code_at_cursor = async (
228228

229229
const collected = await files_collector.collect_files()
230230

231-
const { part1, part2 } = build_prompt({
231+
const { part1, part2 } = PromptBuilder.build_prompt({
232232
other_files: collected.other_files,
233233
recent_files: collected.recent_files,
234234
active_file: {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from '@/constants/edit-format-instructions'
1717
import { FIND_RELEVANT_FILES_SHRINK_SOURCE_CODE_STATE_KEY } from '@/constants/state-keys'
1818
import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
19-
import { build_prompt } from '@/utils/prompt-builder'
19+
import { PromptBuilder } from '@/utils/prompt-builder'
2020

2121
export const handle_copy_prompt = async (params: {
2222
panel_provider: PanelProvider
@@ -86,7 +86,7 @@ export const handle_copy_prompt = async (params: {
8686
? `<missing_text>${processed_completion_instructions}</missing_text>`
8787
: '<missing_text>'
8888

89-
const { full_prompt: text } = build_prompt({
89+
const { full_prompt: text } = PromptBuilder.build_prompt({
9090
context_text,
9191
active_file: {
9292
filepath: relative_path,
@@ -153,7 +153,7 @@ export const handle_copy_prompt = async (params: {
153153
formatted_system_instructions = `${find_relevant_files_format_for_panel}\n\n${find_relevant_files_instructions}`
154154
}
155155

156-
const { full_prompt: text } = build_prompt({
156+
const { full_prompt: text } = PromptBuilder.build_prompt({
157157
context_text,
158158
skill_definitions,
159159
system_instructions: formatted_system_instructions,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
show_configuration_quick_pick,
3131
map_api_configuration_to_item
3232
} from '@/utils/show-configuration-quick-pick'
33-
import { build_prompt } from '@/utils/prompt-builder'
33+
import { PromptBuilder } from '@/utils/prompt-builder'
3434

3535
const get_edit_files_api_configuration = async (params: {
3636
model_providers_manager: ModelProvidersManager
@@ -250,7 +250,7 @@ export const handle_edit_files = async (
250250
.get<string>('editFilesSystemInstructions') ||
251251
default_system_instructions
252252

253-
const { part1, part2 } = build_prompt({
253+
const { part1, part2 } = PromptBuilder.build_prompt({
254254
other_files: collected.other_files,
255255
recent_files: collected.recent_files,
256256
skill_definitions,

‎apps/editor/src/views/panel/backend/message-handlers/handle-find-relevant-files.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
show_configuration_quick_pick,
2727
map_api_configuration_to_item
2828
} from '@/utils/show-configuration-quick-pick'
29-
import { build_prompt } from '@/utils/prompt-builder'
29+
import { PromptBuilder } from '@/utils/prompt-builder'
3030

3131
const get_find_relevant_files_api_configuration = async (params: {
3232
model_providers_manager: ModelProvidersManager
@@ -218,7 +218,7 @@ export const handle_find_relevant_files = async (
218218

219219
const formatted_system_instructions = `${find_relevant_files_format_for_panel}\n\n${find_relevant_files_instructions}`
220220

221-
const { part1, part2 } = build_prompt({
221+
const { part1, part2 } = PromptBuilder.build_prompt({
222222
other_files: collected.other_files,
223223
recent_files: collected.recent_files,
224224
skill_definitions,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
EDIT_FORMAT_INSTRUCTIONS_DIFF
1212
} from '@/constants/edit-format-instructions'
1313
import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
14-
import { build_prompt } from '@/utils/prompt-builder'
14+
import { PromptBuilder } from '@/utils/prompt-builder'
1515

1616
export const handle_preview_web_configuration = async (
1717
panel_provider: PanelProvider,
@@ -65,7 +65,7 @@ export const handle_preview_web_configuration = async (
6565
? `<missing_text>${processed_completion_instructions}</missing_text>`
6666
: '<missing_text>'
6767

68-
const { full_prompt } = build_prompt({
68+
const { full_prompt } = PromptBuilder.build_prompt({
6969
context_text,
7070
active_file: {
7171
filepath: relative_path,
@@ -112,7 +112,7 @@ export const handle_preview_web_configuration = async (
112112
}
113113
}
114114

115-
const { full_prompt: built_prompt } = build_prompt({
115+
const { full_prompt: built_prompt } = PromptBuilder.build_prompt({
116116
context_text,
117117
skill_definitions,
118118
system_instructions: formatted_system_instructions,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import { handle_update_last_used_web_configuration_or_group } from './handle-update-last-used-web-configuration-or-group'
2525
import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
2626
import { show_configuration_quick_pick } from '@/utils/show-configuration-quick-pick'
27-
import { build_prompt } from '@/utils/prompt-builder'
27+
import { PromptBuilder } from '@/utils/prompt-builder'
2828

2929
export const handle_send_to_browser = async (params: {
3030
panel_provider: PanelProvider
@@ -112,7 +112,7 @@ export const handle_send_to_browser = async (params: {
112112
column: position.character
113113
})
114114

115-
const { full_prompt: text } = build_prompt({
115+
const { full_prompt: text } = PromptBuilder.build_prompt({
116116
context_text,
117117
active_file: {
118118
filepath: relative_path,
@@ -184,7 +184,7 @@ export const handle_send_to_browser = async (params: {
184184
formatted_system_instructions = `${find_relevant_files_format_for_panel}\n\n${find_relevant_files_instructions}`
185185
}
186186

187-
const { full_prompt: text } = build_prompt({
187+
const { full_prompt: text } = PromptBuilder.build_prompt({
188188
context_text,
189189
skill_definitions,
190190
system_instructions: formatted_system_instructions,

0 commit comments

Comments
 (0)