Skip to content

Commit 239521e

Browse files
committed
Automate intelligent update for failed patch application and add a fallback patch application method
1 parent 86c8147 commit 239521e

2 files changed

Lines changed: 97 additions & 97 deletions

File tree

packages/vscode/src/commands/apply-chat-response-command/apply-chat-response-command.ts

Lines changed: 91 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ export function apply_chat_response_command(context: vscode.ExtensionContext) {
315315
let failure_count = 0
316316
let all_original_states: OriginalFileState[] = []
317317
const failed_patches: DiffPatch[] = []
318+
let any_patch_used_fallback = false
318319

319320
// Process patches
320321
const total_patches = clipboard_content.patches.length
@@ -336,6 +337,9 @@ export function apply_chat_response_command(context: vscode.ExtensionContext) {
336337
result.original_states
337338
)
338339
}
340+
if (result.used_fallback) {
341+
any_patch_used_fallback = true
342+
}
339343
} else {
340344
failure_count++
341345
failed_patches.push(patch)
@@ -352,127 +356,119 @@ export function apply_chat_response_command(context: vscode.ExtensionContext) {
352356

353357
// Handle results
354358
if (failure_count > 0) {
355-
const response = await vscode.window.showWarningMessage(
356-
success_count > 0
357-
? `Applied ${success_count} patch${
358-
success_count != 1 ? 'es' : ''
359-
} successfully, but ${failure_count} patch${
360-
failure_count != 1 ? 'es' : ''
361-
} failed.`
362-
: `Failed to apply ${failure_count} patch${
363-
failure_count != 1 ? 'es' : ''
364-
}.`,
365-
'Use intelligent update',
366-
...(success_count > 0 ? ['Revert'] : [])
359+
const api_providers_manager = new ApiProvidersManager(context)
360+
const config_result = await get_file_refactoring_config(
361+
api_providers_manager,
362+
false,
363+
context
367364
)
368365

369-
if (response == 'Revert' && all_original_states.length > 0) {
370-
await revert_files(all_original_states)
371-
context.workspaceState.update(LAST_APPLIED_CHANGES_STATE_KEY, null)
372-
} else if (response == 'Use intelligent update') {
373-
const api_providers_manager = new ApiProvidersManager(context)
374-
const config_result = await get_file_refactoring_config(
375-
api_providers_manager,
376-
false,
377-
context
378-
)
379-
380-
if (!config_result) {
381-
return
366+
if (!config_result) {
367+
// If we can't get the config, revert successful patches to maintain consistency
368+
if (success_count > 0 && all_original_states.length > 0) {
369+
await revert_files(all_original_states)
370+
context.workspaceState.update(
371+
LAST_APPLIED_CHANGES_STATE_KEY,
372+
null
373+
)
382374
}
375+
return
376+
}
383377

384-
const { provider, config: file_refactoring_config } = config_result
378+
const { provider, config: file_refactoring_config } = config_result
385379

386-
let endpoint_url = ''
387-
if (provider.type == 'built-in') {
388-
const provider_info =
389-
PROVIDERS[provider.name as keyof typeof PROVIDERS]
390-
endpoint_url = provider_info.base_url
391-
} else {
392-
endpoint_url = provider.base_url
393-
}
380+
let endpoint_url = ''
381+
if (provider.type == 'built-in') {
382+
const provider_info =
383+
PROVIDERS[provider.name as keyof typeof PROVIDERS]
384+
endpoint_url = provider_info.base_url
385+
} else {
386+
endpoint_url = provider.base_url
387+
}
394388

395-
const failed_patches_as_code_blocks = failed_patches
396-
.map(
397-
(patch) =>
398-
`\`\`\`\n// ${patch.file_path}\n${patch.content}\n\`\`\``
399-
)
400-
.join('\n')
389+
const failed_patches_as_code_blocks = failed_patches
390+
.map(
391+
(patch) =>
392+
`\`\`\`\n// ${patch.file_path}\n${patch.content}\n\`\`\``
393+
)
394+
.join('\n')
395+
396+
try {
397+
const intelligent_update_states = await handle_intelligent_update({
398+
endpoint_url,
399+
api_key: provider.api_key,
400+
model: file_refactoring_config.model,
401+
clipboard_text: failed_patches_as_code_blocks,
402+
context: context,
403+
is_single_root_folder_workspace
404+
})
401405

402-
try {
403-
const intelligent_update_states = await handle_intelligent_update(
404-
{
405-
endpoint_url,
406-
api_key: provider.api_key,
407-
model: file_refactoring_config.model,
408-
clipboard_text: failed_patches_as_code_blocks,
409-
context: context,
410-
is_single_root_folder_workspace
411-
}
406+
if (intelligent_update_states) {
407+
const combined_states = [
408+
...all_original_states,
409+
...intelligent_update_states
410+
]
411+
context.workspaceState.update(
412+
LAST_APPLIED_CHANGES_STATE_KEY,
413+
combined_states
414+
)
415+
const response = await vscode.window.showInformationMessage(
416+
`Successfully applied ${failed_patches.length} failed patch${
417+
failed_patches.length != 1 ? 'es' : ''
418+
} using intelligent update.`,
419+
'Revert'
412420
)
413421

414-
if (intelligent_update_states) {
415-
const combined_states = [
416-
...all_original_states,
417-
...intelligent_update_states
418-
]
422+
if (response == 'Revert') {
423+
await revert_files(combined_states)
419424
context.workspaceState.update(
420425
LAST_APPLIED_CHANGES_STATE_KEY,
421-
combined_states
422-
)
423-
const response = await vscode.window.showInformationMessage(
424-
`Successfully applied ${failed_patches.length} failed patch${
425-
failed_patches.length != 1 ? 'es' : ''
426-
}.`,
427-
'Revert'
426+
null
428427
)
429-
430-
if (response == 'Revert') {
431-
await revert_files(combined_states)
432-
context.workspaceState.update(
433-
LAST_APPLIED_CHANGES_STATE_KEY,
434-
null
435-
)
436-
}
437-
} else {
438-
if (success_count > 0 && all_original_states.length > 0) {
439-
await revert_files(all_original_states)
440-
context.workspaceState.update(
441-
LAST_APPLIED_CHANGES_STATE_KEY,
442-
null
443-
)
444-
}
445428
}
446-
} catch (error) {
447-
// Handle any errors during intelligent update
448-
Logger.error({
449-
function_name: 'apply_chat_response_command',
450-
message: 'Error during intelligent update of failed patches'
451-
})
452-
453-
const response = await vscode.window.showErrorMessage(
454-
'Error during fix attempt with the refactoring tool. Would you like to revert the successfully applied patches?',
455-
'Keep changes',
456-
'Revert'
457-
)
458-
459-
if (response == 'Revert' && all_original_states.length > 0) {
429+
} else {
430+
// Intelligent update failed or was canceled - revert successful patches
431+
if (success_count > 0 && all_original_states.length > 0) {
460432
await revert_files(all_original_states)
461433
context.workspaceState.update(
462434
LAST_APPLIED_CHANGES_STATE_KEY,
463435
null
464436
)
465437
}
466438
}
439+
} catch (error) {
440+
// Handle any errors during intelligent update
441+
Logger.error({
442+
function_name: 'apply_chat_response_command',
443+
message: 'Error during intelligent update of failed patches'
444+
})
445+
446+
const response = await vscode.window.showErrorMessage(
447+
'Error during fix attempt with the refactoring tool. Would you like to revert the successfully applied patches?',
448+
'Keep changes',
449+
'Revert'
450+
)
451+
452+
if (response == 'Revert' && all_original_states.length > 0) {
453+
await revert_files(all_original_states)
454+
context.workspaceState.update(
455+
LAST_APPLIED_CHANGES_STATE_KEY,
456+
null
457+
)
458+
}
467459
}
468460
} else if (success_count > 0) {
469-
// All patches applied successfully - show both options now
461+
// All patches applied successfully - show "Looks off" only if any used fallback
462+
const buttons = ['Revert']
463+
if (any_patch_used_fallback) {
464+
buttons.push('Looks off, use intelligent mode')
465+
}
466+
470467
const response = await vscode.window.showInformationMessage(
471468
`Successfully applied ${success_count} patch${
472469
success_count != 1 ? 'es' : ''
473470
}.`,
474-
'Revert',
475-
'Looks off, use intelligent mode'
471+
...buttons
476472
)
477473

478474
if (response == 'Revert' && all_original_states.length > 0) {

packages/vscode/src/commands/apply-chat-response-command/utils/patch-handler.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ async function process_modified_files(
184184
export async function apply_git_patch(
185185
patch_content: string,
186186
workspace_path: string
187-
): Promise<{ success: boolean; original_states?: OriginalFileState[] }> {
187+
): Promise<{
188+
success: boolean
189+
original_states?: OriginalFileState[]
190+
used_fallback?: boolean
191+
}> {
188192
let closed_files: vscode.Uri[] = []
189193

190194
try {
@@ -245,7 +249,7 @@ export async function apply_git_patch(
245249
// Clean up temp file
246250
await vscode.workspace.fs.delete(vscode.Uri.file(temp_file))
247251

248-
return { success: true, original_states }
252+
return { success: true, original_states, used_fallback }
249253
} catch (error: any) {
250254
// Reopen the closed files since the patch application failed
251255
await reopen_closed_files(closed_files)

0 commit comments

Comments
 (0)