Skip to content

Commit 6e1c848

Browse files
committed
Implement a feature to discard manual user changes in the response preview and revert to the AI-proposed content
1 parent 9869e25 commit 6e1c848

12 files changed

Lines changed: 92 additions & 3 deletions

File tree

‎packages/shared/src/types/file-in-preview.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type FileInPreview = {
88
lines_removed: number
99
diff_application_method?: 'recount' | 'search_and_replace'
1010
content?: string
11+
proposed_content?: string
1112
}
1213

1314
export type TextContentInPreview = {

‎packages/ui/src/components/editor/panel/ResponsePreview/ResponsePreview.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ type Props = {
2121
workspace_name?: string
2222
is_checked: boolean
2323
}) => void
24+
on_discard_user_changes?: (file: {
25+
file_path: string
26+
workspace_name?: string
27+
}) => void
2428
raw_instructions?: string
2529
}
2630

@@ -192,6 +196,21 @@ export const ResponsePreview: FC<Props> = (props) => {
192196
</div>
193197
<div className={styles.list__file__right}>
194198
<div className={styles['list__file__actions']}>
199+
{file.content !== undefined &&
200+
file.proposed_content !== undefined &&
201+
file.content !== file.proposed_content && (
202+
<IconButton
203+
codicon_icon="discard"
204+
title="Discard user changes"
205+
on_click={(e) => {
206+
e.stopPropagation()
207+
props.on_discard_user_changes?.({
208+
file_path: file.file_path,
209+
workspace_name: file.workspace_name
210+
})
211+
}}
212+
/>
213+
)}
195214
{file.file_state != 'new' &&
196215
file.file_state != 'deleted' && (
197216
<IconButton

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export const process_chat_response = async (
5858
)
5959
return {
6060
...state,
61-
ai_content: file_in_review?.content,
61+
ai_content:
62+
file_in_review?.proposed_content ?? file_in_review?.content,
6263
is_checked: file_in_review?.is_checked
6364
}
6465
})

‎packages/vscode/src/commands/apply-chat-response-command/utils/preview/file-preparer.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const prepare_files_from_original_states = async (params: {
8383
lines_added: diff_stats.lines_added,
8484
lines_removed: diff_stats.lines_removed,
8585
diff_application_method: state.diff_application_method,
86+
proposed_content: state.ai_content ?? current_content,
8687
is_checked: state.is_checked ?? true
8788
}
8889

@@ -128,6 +129,7 @@ export const prepare_files_from_original_states = async (params: {
128129
file_state: 'deleted',
129130
lines_added: restored_diff_stats.lines_added,
130131
lines_removed: restored_diff_stats.lines_removed,
132+
proposed_content: restored_current_content,
131133
is_checked: state.is_checked ?? true
132134
}
133135

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { ItemInPreview } from '@shared/types/file-in-preview'
1515

1616
export { response_preview_promise_resolve } from './vscode-ui'
1717
export { toggle_file_review_state } from './workspace-listener'
18+
export { discard_user_changes_in_preview } from './workspace-listener'
1819

1920
export const preview = async (params: {
2021
original_states: OriginalFileState[]

‎packages/vscode/src/commands/apply-chat-response-command/utils/preview/workspace-listener.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export let toggle_file_review_state:
1919
}) => Promise<void>)
2020
| undefined
2121

22+
export let discard_user_changes_in_preview:
23+
| ((file: { file_path: string; workspace_name?: string }) => Promise<void>)
24+
| undefined
25+
2226
const recalculate_history_item_totals = (item: ResponseHistoryItem) => {
2327
if (!item.files) {
2428
item.lines_added = 0
@@ -635,6 +639,28 @@ export const setup_workspace_listeners = (
635639
}
636640
)
637641

642+
discard_user_changes_in_preview = async ({ file_path, workspace_name }) => {
643+
const file_to_discard = prepared_files.find(
644+
(f) =>
645+
f.reviewable_file.file_path == file_path &&
646+
f.reviewable_file.workspace_name == workspace_name
647+
)
648+
649+
if (
650+
!file_to_discard ||
651+
file_to_discard.reviewable_file.proposed_content === undefined
652+
) {
653+
return
654+
}
655+
656+
const proposed = file_to_discard.reviewable_file.proposed_content
657+
658+
await vscode.workspace.fs.writeFile(
659+
vscode.Uri.file(file_to_discard.sanitized_path),
660+
Buffer.from(proposed, 'utf8')
661+
)
662+
}
663+
638664
toggle_file_review_state = async ({
639665
file_path,
640666
workspace_name,
@@ -737,6 +763,7 @@ export const setup_workspace_listeners = (
737763
file_created_listener.dispose()
738764
file_renamed_listener.dispose()
739765
toggle_file_review_state = undefined
766+
discard_user_changes_in_preview = undefined
740767
}
741768

742769
return { dispose }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { discard_user_changes_in_preview } from '@/commands/apply-chat-response-command/utils/preview/preview'
2+
import { DiscardUserChangesInPreviewMessage } from '../../types/messages'
3+
4+
export const handle_discard_user_changes_in_preview = async (
5+
message: DiscardUserChangesInPreviewMessage
6+
) => {
7+
if (discard_user_changes_in_preview) {
8+
await discard_user_changes_in_preview({
9+
file_path: message.file_path,
10+
workspace_name: message.workspace_name
11+
})
12+
}
13+
}

‎packages/vscode/src/views/panel/backend/message-handlers/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ export * from './handle-undo'
5252
export * from './handle-show-diff'
5353
export * from './handle-delete-checkpoint'
5454
export * from './handle-clear-all-checkpoints'
55+
export * from './handle-discard-user-changes-in-preview'

‎packages/vscode/src/views/panel/backend/panel-provider.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
handle_go_to_file,
5959
handle_show_diff,
6060
handle_toggle_file_in_preview,
61+
handle_discard_user_changes_in_preview,
6162
handle_intelligent_update_file_in_preview,
6263
handle_response_preview,
6364
handle_commit_changes,
@@ -606,6 +607,8 @@ export class PanelProvider implements vscode.WebviewViewProvider {
606607
handle_focus_on_file_in_preview(message)
607608
} else if (message.command == 'TOGGLE_FILE_IN_REVIEW') {
608609
await handle_toggle_file_in_preview(message)
610+
} else if (message.command == 'DISCARD_USER_CHANGES_IN_PREVIEW') {
611+
await handle_discard_user_changes_in_preview(message)
609612
} else if (message.command == 'INTELLIGENT_UPDATE_FILE_IN_PREVIEW') {
610613
await handle_intelligent_update_file_in_preview(this, message)
611614
} else if (message.command == 'RESPONSE_PREVIEW') {

‎packages/vscode/src/views/panel/frontend/Panel.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ export const Panel = () => {
9595
handle_mode_change,
9696
handle_presets_collapsed_change,
9797
handle_configurations_collapsed_change,
98-
handle_remove_response_history_item
98+
handle_remove_response_history_item,
99+
handle_discard_user_changes_in_preview
99100
} = use_panel(vscode)
100101

101102
const [checkpoint_to_edit, set_checkpoint_to_edit] = useState<
@@ -521,6 +522,7 @@ export const Panel = () => {
521522
is_checked: file.is_checked
522523
})
523524
}}
525+
on_discard_user_changes={handle_discard_user_changes_in_preview}
524526
on_intelligent_update={(file) => {
525527
post_message(vscode, {
526528
command: 'INTELLIGENT_UPDATE_FILE_IN_PREVIEW',

0 commit comments

Comments
 (0)