Skip to content

Commit aaf3983

Browse files
committed
Add close button and title to code-at-cursor config quick pick and implement retry loop for configuration errors
1 parent bee31d4 commit aaf3983

1 file changed

Lines changed: 189 additions & 145 deletions

File tree

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

Lines changed: 189 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,14 @@ const get_code_at_cursor_config = async (params: {
175175
}
176176

177177
const quick_pick = vscode.window.createQuickPick()
178+
quick_pick.buttons = [
179+
{
180+
iconPath: new vscode.ThemeIcon('close'),
181+
tooltip: t('common.close')
182+
}
183+
]
178184
quick_pick.items = create_items()
185+
quick_pick.title = t('common.config.title')
179186
quick_pick.placeholder = t('command.code-at-cursor.config.placeholder')
180187
quick_pick.matchOnDescription = true
181188

@@ -192,6 +199,13 @@ const get_code_at_cursor_config = async (params: {
192199

193200
return new Promise<{ provider: any; config: any } | undefined>(
194201
(resolve) => {
202+
quick_pick.onDidTriggerButton((button) => {
203+
if (button.tooltip == t('common.close')) {
204+
quick_pick.hide()
205+
resolve(undefined)
206+
}
207+
})
208+
195209
quick_pick.onDidAccept(async () => {
196210
const selected = quick_pick.selectedItems[0] as any
197211
quick_pick.hide()
@@ -300,181 +314,211 @@ const perform_code_at_cursor = async (params: {
300314
)
301315
}
302316

303-
const config_result = await get_code_at_cursor_config({
304-
api_providers_manager,
305-
show_quick_pick: params.show_quick_pick,
306-
context: params.context,
307-
config_id: params.config_id,
308-
panel_provider: params.panel_provider
309-
})
317+
let force_show_quick_pick = params.show_quick_pick || false
318+
let current_config_id = params.config_id
310319

311-
if (!config_result) {
312-
return
313-
}
320+
while (true) {
321+
const config_result = await get_code_at_cursor_config({
322+
api_providers_manager,
323+
show_quick_pick: force_show_quick_pick,
324+
context: params.context,
325+
config_id: current_config_id,
326+
panel_provider: params.panel_provider
327+
})
314328

315-
const { provider, config: code_at_cursor_config } = config_result
329+
if (!config_result) {
330+
return
331+
}
316332

317-
if (!code_at_cursor_config.provider_name) {
318-
vscode.window.showErrorMessage(
319-
dictionary.error_message.API_PROVIDER_NOT_SPECIFIED_FOR_CODE_AT_CURSOR
320-
)
321-
Logger.warn({
322-
function_name: 'perform_code_at_cursor',
323-
message: 'API provider is not specified for Code Completions tool.'
324-
})
325-
return
326-
} else if (!code_at_cursor_config.model) {
327-
vscode.window.showErrorMessage(
328-
dictionary.error_message.MODEL_NOT_SPECIFIED_FOR_CODE_AT_CURSOR
329-
)
330-
Logger.warn({
331-
function_name: 'perform_code_at_cursor',
332-
message: 'Model is not specified for Code Completions tool.'
333-
})
334-
return
335-
}
333+
force_show_quick_pick = false
334+
current_config_id = undefined
335+
336+
const { provider, config: code_at_cursor_config } = config_result
336337

337-
let endpoint_url = ''
338-
if (provider.type == 'built-in') {
339-
const provider_info = PROVIDERS[provider.name as keyof typeof PROVIDERS]
340-
if (!provider_info) {
338+
if (!code_at_cursor_config.provider_name) {
341339
vscode.window.showErrorMessage(
342-
dictionary.error_message.BUILT_IN_PROVIDER_NOT_FOUND(provider.name)
340+
dictionary.error_message.API_PROVIDER_NOT_SPECIFIED_FOR_CODE_AT_CURSOR
343341
)
344342
Logger.warn({
345343
function_name: 'perform_code_at_cursor',
346-
message: `Built-in provider "${provider.name}" not found.`
344+
message: 'API provider is not specified for Code Completions tool.'
347345
})
348-
return
346+
force_show_quick_pick = true
347+
continue
348+
} else if (!code_at_cursor_config.model) {
349+
vscode.window.showErrorMessage(
350+
dictionary.error_message.MODEL_NOT_SPECIFIED_FOR_CODE_AT_CURSOR
351+
)
352+
Logger.warn({
353+
function_name: 'perform_code_at_cursor',
354+
message: 'Model is not specified for Code Completions tool.'
355+
})
356+
force_show_quick_pick = true
357+
continue
349358
}
350-
endpoint_url = provider_info.base_url
351-
} else {
352-
endpoint_url = provider.base_url
353-
}
354359

355-
const editor = vscode.window.activeTextEditor
356-
if (editor) {
357-
await editor.document.save()
358-
359-
if (!editor.selection.isEmpty) {
360-
vscode.window.showWarningMessage(
361-
dictionary.warning_message.CODE_AT_CURSOR_NO_SELECTION
362-
)
363-
return
360+
let endpoint_url = ''
361+
if (provider.type == 'built-in') {
362+
const provider_info = PROVIDERS[provider.name as keyof typeof PROVIDERS]
363+
if (!provider_info) {
364+
vscode.window.showErrorMessage(
365+
dictionary.error_message.BUILT_IN_PROVIDER_NOT_FOUND(provider.name)
366+
)
367+
Logger.warn({
368+
function_name: 'perform_code_at_cursor',
369+
message: `Built-in provider "${provider.name}" not found.`
370+
})
371+
force_show_quick_pick = true
372+
continue
373+
}
374+
endpoint_url = provider_info.base_url
375+
} else {
376+
endpoint_url = provider.base_url
364377
}
365-
const cancel_token_source = axios.CancelToken.source()
366-
const document = editor.document
367-
const position = editor.selection.active
368378

369-
const text_before_cursor = document.getText(
370-
new vscode.Range(new vscode.Position(0, 0), position)
371-
)
372-
const text_after_cursor = document.getText(
373-
new vscode.Range(position, document.positionAt(document.getText().length))
374-
)
379+
const editor = vscode.window.activeTextEditor
380+
if (editor) {
381+
await editor.document.save()
375382

376-
const files_collector = new FilesCollector({
377-
workspace_provider: params.file_tree_provider,
378-
open_editors_provider: params.open_editors_provider
379-
})
383+
if (!editor.selection.isEmpty) {
384+
vscode.window.showWarningMessage(
385+
dictionary.warning_message.CODE_AT_CURSOR_NO_SELECTION
386+
)
387+
return
388+
}
389+
const cancel_token_source = axios.CancelToken.source()
390+
const document = editor.document
391+
const position = editor.selection.active
380392

381-
const context_text = await files_collector.collect_files()
393+
const text_before_cursor = document.getText(
394+
new vscode.Range(new vscode.Position(0, 0), position)
395+
)
396+
const text_after_cursor = document.getText(
397+
new vscode.Range(
398+
position,
399+
document.positionAt(document.getText().length)
400+
)
401+
)
382402

383-
const payload = {
384-
before: `<files>\n${context_text}<file path="${vscode.workspace.asRelativePath(
385-
document.uri
386-
)}">\n<![CDATA[\n${text_before_cursor}`,
387-
after: `${text_after_cursor}\n]]>\n</file>\n</files>`
388-
}
403+
const files_collector = new FilesCollector({
404+
workspace_provider: params.file_tree_provider,
405+
open_editors_provider: params.open_editors_provider
406+
})
389407

390-
const content = `${payload.before}${
391-
completion_instructions
392-
? `<missing_text>${completion_instructions}</missing_text>`
393-
: '<missing_text>'
394-
}${payload.after}\n${code_at_cursor_instructions}`
408+
const context_text = await files_collector.collect_files()
395409

396-
const messages = [
397-
{
398-
role: 'user',
399-
content
410+
const payload = {
411+
before: `<files>\n${context_text}<file path="${vscode.workspace.asRelativePath(
412+
document.uri
413+
)}">\n<![CDATA[\n${text_before_cursor}`,
414+
after: `${text_after_cursor}\n]]>\n</file>\n</files>`
400415
}
401-
]
402416

403-
const body: { [key: string]: any } = {
404-
messages,
405-
model: code_at_cursor_config.model,
406-
temperature: code_at_cursor_config.temperature
407-
}
417+
const content = `${payload.before}${
418+
completion_instructions
419+
? `<missing_text>${completion_instructions}</missing_text>`
420+
: '<missing_text>'
421+
}${payload.after}\n${code_at_cursor_instructions}`
408422

409-
apply_reasoning_effort({
410-
body,
411-
provider,
412-
reasoning_effort: code_at_cursor_config.reasoning_effort
413-
})
414-
415-
const cursor_listener = vscode.window.onDidChangeTextEditorSelection(() => {
416-
cancel_token_source.cancel(
417-
t('command.code-at-cursor.cancel.cursor-moved')
418-
)
419-
})
420-
421-
try {
422-
const completion_result = await vscode.window.withProgress(
423+
const messages = [
423424
{
424-
location: vscode.ProgressLocation.Notification,
425-
title: t('command.code-at-cursor.progress.title'),
426-
cancellable: true
427-
},
428-
async (progress, token) => {
429-
token.onCancellationRequested(() => {
430-
cancel_token_source.cancel(t('command.code-at-cursor.cancel.user'))
431-
})
425+
role: 'user',
426+
content
427+
}
428+
]
432429

433-
progress.report({
434-
message: t('common.progress.waiting-for-server')
435-
})
430+
const body: { [key: string]: any } = {
431+
messages,
432+
model: code_at_cursor_config.model,
433+
temperature: code_at_cursor_config.temperature
434+
}
436435

437-
return await make_api_request({
438-
endpoint_url,
439-
api_key: provider.api_key,
440-
body,
441-
cancellation_token: cancel_token_source.token,
442-
on_chunk: () => {
443-
progress.report({ message: t('common.progress.receiving') })
444-
},
445-
on_thinking_chunk: () => {
446-
progress.report({ message: t('common.progress.thinking') })
447-
}
448-
})
436+
apply_reasoning_effort({
437+
body,
438+
provider,
439+
reasoning_effort: code_at_cursor_config.reasoning_effort
440+
})
441+
442+
const cursor_listener = vscode.window.onDidChangeTextEditorSelection(
443+
() => {
444+
cancel_token_source.cancel(
445+
t('command.code-at-cursor.cancel.cursor-moved')
446+
)
449447
}
450448
)
451449

452-
if (completion_result) {
453-
const match = completion_result.response.match(
454-
/<replacement>([\s\S]*?)<\/replacement>/i
450+
try {
451+
const completion_result = await vscode.window.withProgress(
452+
{
453+
location: vscode.ProgressLocation.Notification,
454+
title: t('command.code-at-cursor.progress.title'),
455+
cancellable: true
456+
},
457+
async (progress, token) => {
458+
token.onCancellationRequested(() => {
459+
cancel_token_source.cancel(
460+
t('command.code-at-cursor.cancel.user')
461+
)
462+
})
463+
464+
progress.report({
465+
message: t('common.progress.waiting-for-server')
466+
})
467+
468+
return await make_api_request({
469+
endpoint_url,
470+
api_key: provider.api_key,
471+
body,
472+
cancellation_token: cancel_token_source.token,
473+
on_chunk: () => {
474+
progress.report({ message: t('common.progress.receiving') })
475+
},
476+
on_thinking_chunk: () => {
477+
progress.report({ message: t('common.progress.thinking') })
478+
}
479+
})
480+
}
455481
)
456-
if (match && match[1]) {
457-
let decoded_completion = he.decode(match[1].trim())
458-
decoded_completion = decoded_completion
459-
.replace(/<!\[CDATA\[/g, '')
460-
.replace(/\]\]>/g, '')
461-
.trim()
462-
463-
await show_ghost_text({
464-
editor,
465-
position,
466-
ghost_text: decoded_completion
467-
})
482+
483+
if (completion_result) {
484+
const match = completion_result.response.match(
485+
/<replacement>([\s\S]*?)<\/replacement>/i
486+
)
487+
if (match && match[1]) {
488+
let decoded_completion = he.decode(match[1].trim())
489+
decoded_completion = decoded_completion
490+
.replace(/<!\[CDATA\[/g, '')
491+
.replace(/\]\]>/g, '')
492+
.trim()
493+
494+
await show_ghost_text({
495+
editor,
496+
position,
497+
ghost_text: decoded_completion
498+
})
499+
}
500+
break
501+
} else {
502+
force_show_quick_pick = true
503+
continue
504+
}
505+
} catch (err: any) {
506+
if (axios.isCancel(err)) {
507+
break
468508
}
509+
510+
Logger.error({
511+
function_name: 'perform_code_at_cursor',
512+
message: 'Completion error',
513+
data: err
514+
})
515+
force_show_quick_pick = true
516+
continue
517+
} finally {
518+
cursor_listener.dispose()
469519
}
470-
} catch (err: any) {
471-
Logger.error({
472-
function_name: 'perform_code_at_cursor',
473-
message: 'Completion error',
474-
data: err
475-
})
476-
} finally {
477-
cursor_listener.dispose()
520+
} else {
521+
break
478522
}
479523
}
480524
}

0 commit comments

Comments
 (0)