Skip to content

Commit a942a95

Browse files
committed
Implement robust error handling in the Edit Context flow, allowing users to reselect configurations and retry API calls upon failure
1 parent 7d30c8d commit a942a95

2 files changed

Lines changed: 144 additions & 126 deletions

File tree

‎packages/vscode/src/utils/make-api-request.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ type ThinkingStreamCallback = (text: string) => void
99
const DATA_PREFIX = 'data: '
1010
const DONE_TOKEN = '[DONE]'
1111

12-
async function process_stream_chunk(
12+
const process_stream_chunk = async (
1313
chunk: string,
1414
buffer: string
1515
): Promise<{
1616
updated_buffer: string
1717
new_content: string
18-
}> {
18+
}> => {
1919
let updated_buffer = buffer
2020
let new_content = ''
2121
try {
@@ -59,14 +59,14 @@ async function process_stream_chunk(
5959
}
6060
}
6161

62-
export async function make_api_request(params: {
62+
export const make_api_request = async (params: {
6363
endpoint_url: string
6464
api_key: string
6565
body: any
6666
cancellation_token: any
6767
on_chunk?: StreamCallback
6868
on_thinking_chunk?: ThinkingStreamCallback
69-
}): Promise<{ response: string; thoughts?: string } | null> {
69+
}): Promise<{ response: string; thoughts?: string } | null> => {
7070
Logger.info({
7171
function_name: 'make_api_request',
7272
message: 'API Request Body',
@@ -331,7 +331,7 @@ export async function make_api_request(params: {
331331
message: 'Request canceled',
332332
data: error.message
333333
})
334-
return null
334+
throw error
335335
} else if (axios.isAxiosError(error) && error.response?.status == 429) {
336336
vscode.window.showErrorMessage(
337337
dictionary.error_message.API_RATE_LIMIT_EXCEEDED

‎packages/vscode/src/views/panel/backend/message-handlers/handle-edit-context.ts‎

Lines changed: 139 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ const get_edit_context_config = async (
2424
api_providers_manager: ModelProvidersManager,
2525
show_quick_pick: boolean = false,
2626
context: vscode.ExtensionContext,
27-
config_id?: string,
28-
panel_provider?: PanelProvider
27+
panel_provider: PanelProvider,
28+
config_id?: string
2929
): Promise<{ provider: any; config: any } | undefined> => {
3030
const edit_context_configs =
3131
await api_providers_manager.get_edit_context_tool_configs()
@@ -278,145 +278,163 @@ const perform_context_editing = async (params: {
278278
return
279279
}
280280

281-
const config_result = await get_edit_context_config(
282-
api_providers_manager,
283-
params.show_quick_pick,
284-
params.context,
285-
params.config_id,
286-
params.panel_provider
287-
)
288-
289-
if (!config_result) {
290-
return
291-
}
281+
let current_config_id = params.config_id
282+
let should_show_quick_pick = params.show_quick_pick
283+
284+
// eslint-disable-next-line no-constant-condition
285+
while (true) {
286+
const config_result = await get_edit_context_config(
287+
api_providers_manager,
288+
should_show_quick_pick,
289+
params.context,
290+
params.panel_provider,
291+
current_config_id
292+
)
292293

293-
const { provider, config: edit_context_config } = config_result
294+
if (!config_result) {
295+
return
296+
}
294297

295-
if (!provider.api_key) {
296-
vscode.window.showErrorMessage(dictionary.error_message.API_KEY_MISSING)
297-
return
298-
}
298+
const { provider, config: edit_context_config } = config_result
299299

300-
let endpoint_url = ''
301-
if (provider.type == 'built-in') {
302-
const provider_info = PROVIDERS[provider.name as keyof typeof PROVIDERS]
303-
if (!provider_info) {
304-
vscode.window.showErrorMessage(
305-
dictionary.error_message.BUILT_IN_PROVIDER_NOT_FOUND(provider.name)
306-
)
307-
Logger.warn({
308-
function_name: 'perform_context_editing',
309-
message: `Built-in provider "${provider.name}" not found.`
310-
})
311-
return
300+
if (!provider.api_key) {
301+
vscode.window.showErrorMessage(dictionary.error_message.API_KEY_MISSING)
302+
should_show_quick_pick = true
303+
current_config_id = undefined
304+
continue
312305
}
313-
endpoint_url = provider_info.base_url
314-
} else {
315-
endpoint_url = provider.base_url
316-
}
317306

318-
const files = `<files>${collected_files}\n</files>`
307+
let endpoint_url = ''
308+
if (provider.type == 'built-in') {
309+
const provider_info = PROVIDERS[provider.name as keyof typeof PROVIDERS]
310+
if (!provider_info) {
311+
vscode.window.showErrorMessage(
312+
dictionary.error_message.BUILT_IN_PROVIDER_NOT_FOUND(provider.name)
313+
)
314+
Logger.warn({
315+
function_name: 'perform_context_editing',
316+
message: `Built-in provider "${provider.name}" not found.`
317+
})
318+
should_show_quick_pick = true
319+
current_config_id = undefined
320+
continue
321+
}
322+
endpoint_url = provider_info.base_url
323+
} else {
324+
endpoint_url = provider.base_url
325+
}
319326

320-
const edit_format = params.context.workspaceState.get<EditFormat>(
321-
'api-edit-format',
322-
'diff'
323-
)
324-
const all_instructions = vscode.workspace
325-
.getConfiguration('codeWebChat')
326-
.get<{ [key in EditFormat]: string }>('editFormatInstructions')
327-
const edit_format_instructions = all_instructions?.[edit_format] ?? ''
328-
329-
if (edit_format_instructions) {
330-
const system_instructions = `<system>\n${edit_format_instructions}\n</system>`
331-
pre_context_instructions += `\n${system_instructions}`
332-
post_context_instructions += `\n${system_instructions}`
333-
}
327+
const files = `<files>${collected_files}\n</files>`
334328

335-
// Use instructions placement setting to determine message structure
336-
const instructions_placement =
337-
edit_context_config.instructions_placement || 'above-and-below'
338-
let content: string
339-
if (instructions_placement == 'below-only') {
340-
// Place instructions only below context for better caching
341-
content = `${files}\n${post_context_instructions}`
342-
} else {
343-
// Default: place instructions above and below context for better adherence
344-
content = `${pre_context_instructions}\n${files}\n${post_context_instructions}`
345-
}
329+
const edit_format = params.context.workspaceState.get<EditFormat>(
330+
'api-edit-format',
331+
'diff'
332+
)
333+
const all_instructions = vscode.workspace
334+
.getConfiguration('codeWebChat')
335+
.get<{ [key in EditFormat]: string }>('editFormatInstructions')
336+
const edit_format_instructions = all_instructions?.[edit_format] ?? ''
337+
338+
let loop_pre_context_instructions = pre_context_instructions
339+
let loop_post_context_instructions = post_context_instructions
340+
341+
if (edit_format_instructions) {
342+
const system_instructions = `<system>\n${edit_format_instructions}\n</system>`
343+
loop_pre_context_instructions += `\n${system_instructions}`
344+
loop_post_context_instructions += `\n${system_instructions}`
345+
}
346346

347-
const messages = [
348-
{
349-
role: 'system',
350-
content: "You're a helpful coding assistant."
351-
},
352-
{
353-
role: 'user',
354-
content
347+
// Use instructions placement setting to determine message structure
348+
const instructions_placement =
349+
edit_context_config.instructions_placement || 'above-and-below'
350+
let content: string
351+
if (instructions_placement == 'below-only') {
352+
// Place instructions only below context for better caching
353+
content = `${files}\n${loop_post_context_instructions}`
354+
} else {
355+
// Default: place instructions above and below context for better adherence
356+
content = `${loop_pre_context_instructions}\n${files}\n${loop_post_context_instructions}`
355357
}
356-
]
357358

358-
const body: { [key: string]: any } = {
359-
messages,
360-
model: edit_context_config.model,
361-
temperature: edit_context_config.temperature
362-
}
359+
const messages = [
360+
{
361+
role: 'system',
362+
content: "You're a helpful coding assistant."
363+
},
364+
{
365+
role: 'user',
366+
content
367+
}
368+
]
363369

364-
apply_reasoning_effort(body, provider, edit_context_config.reasoning_effort)
370+
const body: { [key: string]: any } = {
371+
messages,
372+
model: edit_context_config.model,
373+
temperature: edit_context_config.temperature
374+
}
365375

366-
const cancel_token_source = axios.CancelToken.source()
376+
apply_reasoning_effort(body, provider, edit_context_config.reasoning_effort)
367377

368-
if (params.panel_provider) {
369-
params.panel_provider.api_call_cancel_token_source = cancel_token_source
370-
}
378+
const cancel_token_source = axios.CancelToken.source()
371379

372-
try {
373380
if (params.panel_provider) {
374-
params.panel_provider.send_message({
375-
command: 'SHOW_PROGRESS',
376-
title: `${dictionary.api_call.WAITING_FOR_API_RESPONSE}...`
377-
})
381+
params.panel_provider.api_call_cancel_token_source = cancel_token_source
378382
}
379-
const result = await make_api_request({
380-
endpoint_url,
381-
api_key: provider.api_key,
382-
body,
383-
cancellation_token: cancel_token_source.token,
384-
on_thinking_chunk: () => {
385-
if (params.panel_provider) {
386-
params.panel_provider.send_message({
387-
command: 'SHOW_PROGRESS',
388-
title: `${dictionary.api_call.THINKING}...`
389-
})
390-
}
391-
},
392-
on_chunk: (tokens_per_second) => {
393-
if (params.panel_provider) {
394-
params.panel_provider.send_message({
395-
command: 'SHOW_PROGRESS',
396-
title: 'Receiving response...',
397-
tokens_per_second
398-
})
399-
}
383+
384+
try {
385+
if (params.panel_provider) {
386+
params.panel_provider.send_message({
387+
command: 'SHOW_PROGRESS',
388+
title: `${dictionary.api_call.WAITING_FOR_API_RESPONSE}...`
389+
})
400390
}
401-
})
391+
const result = await make_api_request({
392+
endpoint_url,
393+
api_key: provider.api_key,
394+
body,
395+
cancellation_token: cancel_token_source.token,
396+
on_thinking_chunk: () => {
397+
if (params.panel_provider) {
398+
params.panel_provider.send_message({
399+
command: 'SHOW_PROGRESS',
400+
title: `${dictionary.api_call.THINKING}...`
401+
})
402+
}
403+
},
404+
on_chunk: (tokens_per_second) => {
405+
if (params.panel_provider) {
406+
params.panel_provider.send_message({
407+
command: 'SHOW_PROGRESS',
408+
title: 'Receiving response...',
409+
tokens_per_second
410+
})
411+
}
412+
}
413+
})
402414

403-
if (result) {
404-
vscode.commands.executeCommand('codeWebChat.applyChatResponse', {
405-
response: result.response,
406-
raw_instructions: instructions
415+
if (result) {
416+
vscode.commands.executeCommand('codeWebChat.applyChatResponse', {
417+
response: result.response,
418+
raw_instructions: instructions
419+
})
420+
return
421+
} else {
422+
should_show_quick_pick = true
423+
current_config_id = undefined
424+
}
425+
} catch (error) {
426+
if (axios.isCancel(error)) return
427+
Logger.error({
428+
function_name: 'perform_context_editing',
429+
message: 'refactor task error',
430+
data: error
407431
})
432+
vscode.window.showErrorMessage(dictionary.error_message.REFACTOR_ERROR)
433+
return
434+
} finally {
435+
params.panel_provider.send_message({ command: 'HIDE_PROGRESS' })
436+
params.panel_provider.api_call_cancel_token_source = null
408437
}
409-
} catch (error) {
410-
if (axios.isCancel(error)) return
411-
Logger.error({
412-
function_name: 'perform_context_editing',
413-
message: 'refactor task error',
414-
data: error
415-
})
416-
vscode.window.showErrorMessage(dictionary.error_message.REFACTOR_ERROR)
417-
} finally {
418-
params.panel_provider.send_message({ command: 'HIDE_PROGRESS' })
419-
params.panel_provider.api_call_cancel_token_source = null
420438
}
421439
}
422440

0 commit comments

Comments
 (0)