Skip to content

Commit 3ca9be2

Browse files
committed
Rename Code Completions to Code at Cursor and refactor file context construction logic
- "### File:..." should be an implementation detail of prompt_builder, should not be set outside of it. - rename 'code completions' to 'code at cursor'
1 parent 698dbf2 commit 3ca9be2

21 files changed

Lines changed: 157 additions & 127 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const get_code_at_cursor_api_configuration = async (params: {
4141
) || null
4242
} else if (!params.show_quick_pick) {
4343
const default_api_configuration =
44-
await params.model_providers_manager.get_default_code_completions_api_configuration()
44+
await params.model_providers_manager.get_default_code_at_cursor_api_configuration()
4545
if (default_api_configuration) {
4646
selected_api_configuration = default_api_configuration
4747
} else {
@@ -108,7 +108,7 @@ export const get_code_at_cursor_api_configuration = async (params: {
108108
)
109109
Logger.warn({
110110
function_name: 'get_code_at_cursor_api_configuration',
111-
message: 'API provider not found for Code Completions tool.'
111+
message: 'API provider not found for Code at Cursor tool.'
112112
})
113113
return
114114
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { dictionary } from '@shared/constants/dictionary'
1111
import { apply_reasoning_effort } from '../../../utils/apply-reasoning-effort'
1212
import { t } from '@/i18n'
1313
import { build_user_content } from '../../../utils/build-user-content'
14-
1514
import { get_code_at_cursor_api_configuration } from './get-code-at-cursor-config'
1615
import { show_ghost_text } from './show-ghost-text'
1716
import { build_prompt } from '../../../utils/prompt-builder'
@@ -81,7 +80,7 @@ export const perform_code_at_cursor = async (params: {
8180
)
8281
Logger.warn({
8382
function_name: 'perform_code_at_cursor',
84-
message: 'API provider is not specified for Code Completions tool.'
83+
message: 'API provider is not specified for Code at Cursor tool.'
8584
})
8685
force_show_quick_pick = true
8786
continue
@@ -91,7 +90,7 @@ export const perform_code_at_cursor = async (params: {
9190
)
9291
Logger.warn({
9392
function_name: 'perform_code_at_cursor',
94-
message: 'Model is not specified for Code Completions tool.'
93+
message: 'Model is not specified for Code at Cursor tool.'
9594
})
9695
force_show_quick_pick = true
9796
continue
@@ -133,11 +132,14 @@ export const perform_code_at_cursor = async (params: {
133132
const { part1, part2 } = build_prompt({
134133
other_files: collected.other_files,
135134
recent_files: collected.recent_files,
136-
active_file_context: `### File: \`${vscode.workspace.asRelativePath(document.uri)}\`\n\n\`\`\`\n${text_before_cursor}${
137-
completion_instructions
138-
? `<missing_text>${completion_instructions}</missing_text>`
139-
: '<missing_text>'
140-
}${text_after_cursor}\n\`\`\`\n\n`,
135+
active_file: {
136+
filepath: vscode.workspace.asRelativePath(document.uri),
137+
content: `${text_before_cursor}${
138+
completion_instructions
139+
? `<missing_text>${completion_instructions}</missing_text>`
140+
: '<missing_text>'
141+
}${text_after_cursor}`
142+
},
141143
system_instructions: code_at_cursor_instructions
142144
})
143145

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

Lines changed: 9 additions & 9 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 } from '../utils/prompt-builder'
21+
import { build_prompt, build_file_context } from '../utils/prompt-builder'
2222

2323
export const token_count_emitter = new EventEmitter()
2424

@@ -275,10 +275,10 @@ export const context_initialization = async (
275275
}
276276

277277
if (is_binary_file(file_path, content_uint8_array)) {
278-
context_text += `### File: \`${display_path.replace(
279-
/\\/g,
280-
'/'
281-
)}\`\n\nBinary file\n\n`
278+
context_text += build_file_context({
279+
filepath: display_path,
280+
is_binary: true
281+
})
282282
continue
283283
}
284284

@@ -292,10 +292,10 @@ export const context_initialization = async (
292292
)
293293
}
294294

295-
context_text += `### File: \`${display_path.replace(
296-
/\\/g,
297-
'/'
298-
)}\`\n\n\`\`\`\n${content}\n\`\`\`\n\n`
295+
context_text += build_file_context({
296+
filepath: display_path,
297+
content
298+
})
299299
} catch (error: any) {
300300
vscode.window.showErrorMessage(
301301
dictionary.error_message.ERROR_READING_FILE(

‎apps/editor/src/services/model-providers-manager.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export class ModelProvidersManager {
257257
)
258258
}
259259

260-
public async get_default_code_completions_api_configuration(): Promise<
260+
public async get_default_code_at_cursor_api_configuration(): Promise<
261261
ApiConfiguration | undefined
262262
> {
263263
await this._load_promise
@@ -266,7 +266,7 @@ export class ModelProvidersManager {
266266
)
267267
}
268268

269-
public async set_default_code_completions_api_configuration(
269+
public async set_default_code_at_cursor_api_configuration(
270270
api_configuration: ApiConfiguration | null
271271
) {
272272
await this._set_default_api_configuration_in_settings(

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
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`
9+
}
10+
return `### File: \`${display_path}\`\n\n\`\`\`\n${params.content}\n\`\`\`\n\n`
11+
}
12+
113
export const build_prompt = (params: {
214
other_files?: string
315
recent_files?: string
416
context_text?: string
5-
active_file_context?: string
17+
active_file?: { filepath: string; content: string }
618
skill_definitions?: string
719
system_instructions?: string
820
user_instructions?: string
@@ -12,16 +24,21 @@ export const build_prompt = (params: {
1224
let part2 = ''
1325
let full_prompt = ''
1426

27+
let active_file_context = ''
28+
if (params.active_file) {
29+
active_file_context = build_file_context(params.active_file)
30+
}
31+
1532
if (params.context_text !== undefined) {
1633
if (params.context_text) {
1734
full_prompt += `# Files\n\n${params.context_text}`
1835
}
19-
if (params.active_file_context) {
20-
full_prompt += params.active_file_context
36+
if (active_file_context) {
37+
full_prompt += active_file_context
2138
}
2239
} else {
2340
part1 = `# Files\n\n${params.other_files || ''}`
24-
part2 += `${params.recent_files || ''}${params.active_file_context || ''}`
41+
part2 += `${params.recent_files || ''}${active_file_context}`
2542
full_prompt = part1 + part2
2643
}
2744

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ const get_code_at_cursor_api_configuration = async (
3333
| { model_provider: ModelProvider; api_configuration: ApiConfiguration }
3434
| undefined
3535
> => {
36-
const code_completions_api_configurations =
36+
const code_at_cursor_api_configurations =
3737
await model_providers_manager.get_api_configurations()
3838

39-
if (code_completions_api_configurations.length == 0) {
39+
if (code_at_cursor_api_configurations.length == 0) {
4040
vscode.commands.executeCommand('codeWebChat.settings')
4141
vscode.window.showInformationMessage(
4242
dictionary.information_message.NO_CODE_AT_CURSOR_CONFIGURATIONS_FOUND
@@ -48,7 +48,7 @@ const get_code_at_cursor_api_configuration = async (
4848

4949
if (api_configuration_id !== undefined) {
5050
selected_api_configuration =
51-
code_completions_api_configurations.find(
51+
code_at_cursor_api_configurations.find(
5252
(c) => get_api_configuration_id(c) == api_configuration_id
5353
) || null
5454
if (selected_api_configuration && panel_provider) {
@@ -64,16 +64,16 @@ const get_code_at_cursor_api_configuration = async (
6464
)
6565
if (last_selected_id) {
6666
selected_api_configuration =
67-
code_completions_api_configurations.find(
67+
code_at_cursor_api_configurations.find(
6868
(c) => get_api_configuration_id(c) === last_selected_id
6969
) || null
7070
}
7171

7272
if (
7373
!selected_api_configuration &&
74-
code_completions_api_configurations.length > 0
74+
code_at_cursor_api_configurations.length > 0
7575
) {
76-
selected_api_configuration = code_completions_api_configurations[0]
76+
selected_api_configuration = code_at_cursor_api_configurations[0]
7777
}
7878
}
7979

@@ -83,10 +83,10 @@ const get_code_at_cursor_api_configuration = async (
8383
)
8484

8585
const result = await show_configuration_quick_pick({
86-
items: code_completions_api_configurations,
86+
items: code_at_cursor_api_configurations,
8787
map_item: map_api_configuration_to_item,
8888
last_selected_id,
89-
placeholder: 'Select code completions API configuration'
89+
placeholder: 'Select code at cursor API configuration'
9090
})
9191

9292
if (panel_provider) {
@@ -160,25 +160,25 @@ export const handle_code_at_cursor = async (
160160

161161
const {
162162
model_provider,
163-
api_configuration: code_completions_api_configuration
163+
api_configuration: code_at_cursor_api_configuration
164164
} = api_configuration_result
165165

166-
if (!code_completions_api_configuration.model_provider_name) {
166+
if (!code_at_cursor_api_configuration.model_provider_name) {
167167
vscode.window.showErrorMessage(
168168
dictionary.error_message.API_PROVIDER_NOT_SPECIFIED_FOR_CODE_AT_CURSOR
169169
)
170170
Logger.warn({
171171
function_name: 'handle_code_at_cursor',
172-
message: 'API provider is not specified for Code Completions tool.'
172+
message: 'API provider is not specified for Code at Cursor tool.'
173173
})
174174
return
175-
} else if (!code_completions_api_configuration.model) {
175+
} else if (!code_at_cursor_api_configuration.model) {
176176
vscode.window.showErrorMessage(
177177
dictionary.error_message.MODEL_NOT_SPECIFIED_FOR_CODE_AT_CURSOR
178178
)
179179
Logger.warn({
180180
function_name: 'handle_code_at_cursor',
181-
message: 'Model is not specified for Code Completions tool.'
181+
message: 'Model is not specified for Code at Cursor tool.'
182182
})
183183
return
184184
}
@@ -231,11 +231,14 @@ export const handle_code_at_cursor = async (
231231
const { part1, part2 } = build_prompt({
232232
other_files: collected.other_files,
233233
recent_files: collected.recent_files,
234-
active_file_context: `### File: \`${relative_path}\`\n\n\`\`\`\n${text_before_cursor}${
235-
processed_completion_instructions
236-
? `<missing_text>${processed_completion_instructions}</missing_text>`
237-
: '<missing_text>'
238-
}${text_after_cursor}\n\`\`\`\n\n`,
234+
active_file: {
235+
filepath: relative_path,
236+
content: `${text_before_cursor}${
237+
processed_completion_instructions
238+
? `<missing_text>${processed_completion_instructions}</missing_text>`
239+
: '<missing_text>'
240+
}${text_after_cursor}`
241+
},
239242
skill_definitions,
240243
system_instructions: main_instructions
241244
})
@@ -255,14 +258,14 @@ export const handle_code_at_cursor = async (
255258

256259
const body: { [key: string]: any } = {
257260
messages,
258-
model: code_completions_api_configuration.model,
259-
temperature: code_completions_api_configuration.temperature
261+
model: code_at_cursor_api_configuration.model,
262+
temperature: code_at_cursor_api_configuration.temperature
260263
}
261264

262265
apply_reasoning_effort({
263266
body,
264267
model_provider,
265-
reasoning_effort: code_completions_api_configuration.reasoning_effort
268+
reasoning_effort: code_at_cursor_api_configuration.reasoning_effort
266269
})
267270

268271
let error_occurred = false
@@ -277,11 +280,9 @@ export const handle_code_at_cursor = async (
277280
api_key: model_provider.api_key,
278281
body,
279282
request_id,
280-
provider_name:
281-
code_completions_api_configuration.model_provider_name,
282-
model: code_completions_api_configuration.model,
283-
reasoning_effort:
284-
code_completions_api_configuration.reasoning_effort
283+
provider_name: code_at_cursor_api_configuration.model_provider_name,
284+
model: code_at_cursor_api_configuration.model,
285+
reasoning_effort: code_at_cursor_api_configuration.reasoning_effort
285286
})
286287

287288
if (result) {
@@ -299,10 +300,10 @@ export const handle_code_at_cursor = async (
299300
},
300301
recent_api_configuration: {
301302
model_provider:
302-
code_completions_api_configuration.model_provider_name,
303-
model: code_completions_api_configuration.model,
303+
code_at_cursor_api_configuration.model_provider_name,
304+
model: code_at_cursor_api_configuration.model,
304305
reasoning_effort:
305-
code_completions_api_configuration.reasoning_effort
306+
code_at_cursor_api_configuration.reasoning_effort
306307
}
307308
}
308309
)

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ export const handle_copy_prompt = async (params: {
3030

3131
const active_editor = vscode.window.activeTextEditor
3232

33-
const is_in_code_completions_prompt_type =
33+
const is_in_code_at_cursor_prompt_type =
3434
(params.panel_provider.mode == MODE.WEB &&
3535
params.panel_provider.web_prompt_type == 'code-at-cursor') ||
3636
(params.panel_provider.mode == MODE.API &&
3737
params.panel_provider.api_prompt_type == 'code-at-cursor')
3838

3939
if (
40-
is_in_code_completions_prompt_type &&
40+
is_in_code_at_cursor_prompt_type &&
4141
active_editor &&
4242
!active_editor.selection.isEmpty
4343
) {
@@ -48,7 +48,7 @@ export const handle_copy_prompt = async (params: {
4848
return
4949
}
5050

51-
if (is_in_code_completions_prompt_type && active_editor) {
51+
if (is_in_code_at_cursor_prompt_type && active_editor) {
5252
const document = active_editor.document
5353
const position = active_editor.selection.active
5454
const active_path = document.uri.fsPath
@@ -88,13 +88,16 @@ export const handle_copy_prompt = async (params: {
8888

8989
const { full_prompt: text } = build_prompt({
9090
context_text,
91-
active_file_context: `### File: \`${relative_path}\`\n\n\`\`\`\n${text_before_cursor}${missing_text_tag}${text_after_cursor}\n\`\`\`\n\n`,
91+
active_file: {
92+
filepath: relative_path,
93+
content: `${text_before_cursor}${missing_text_tag}${text_after_cursor}`
94+
},
9295
skill_definitions,
9396
system_instructions
9497
})
9598

9699
vscode.env.clipboard.writeText(text.trim())
97-
} else if (!is_in_code_completions_prompt_type) {
100+
} else if (!is_in_code_at_cursor_prompt_type) {
98101
const is_in_find_relevant_files_prompt_type =
99102
(params.panel_provider.mode == MODE.WEB &&
100103
params.panel_provider.web_prompt_type == 'find-relevant-files') ||

‎apps/editor/src/views/panel/backend/message-handlers/handle-get-history.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const handle_get_history = (panel_provider: PanelProvider) => {
1515
const ask_about_files_history = panel_provider.context.workspaceState.get<
1616
HistoryEntry[]
1717
>(HISTORY_ASK_ABOUT_FILES_STATE_KEY, [])
18-
const code_completions_history = panel_provider.context.workspaceState.get<
18+
const code_at_cursor_history = panel_provider.context.workspaceState.get<
1919
HistoryEntry[]
2020
>(HISTORY_CODE_AT_CURSOR_STATE_KEY, [])
2121
const without_files_history = panel_provider.context.workspaceState.get<
@@ -30,7 +30,7 @@ export const handle_get_history = (panel_provider: PanelProvider) => {
3030
ask_about_files: ask_about_files_history.map((h) => h.text),
3131
edit_files: edit_files_history.map((h) => h.text),
3232
without_files: without_files_history.map((h) => h.text),
33-
code_at_cursor: code_completions_history.map((h) => h.text),
33+
code_at_cursor: code_at_cursor_history.map((h) => h.text),
3434
find_relevant_files: find_relevant_files_history.map((h) => h.text)
3535
})
3636
}

0 commit comments

Comments
 (0)