Skip to content

Commit a86473b

Browse files
committed
Persist response history and restore interrupted preview checkpoints on startup
- because response history is not stored in workspace state, it doesn't survive editor reloads - when editor was interrupted (reloaded) during the response preview, the fresh opening doesn't restore the temp checkpoint so that any changes made by this Response preview are reverted. - reverting changes made by an interrupted Response Preview should not clear response history - these changes [Changes] made a notification 'reverting...' to appear on startup ├── apps │ └── editor │ └── src │ ├── commands │ │ └── history-command │ │ └── actions │ │ └── restore-checkpoint.ts │ ├── constants │ │ └── state-keys.ts │ ├── extension.ts │ ├── features │ │ ├── apply-response │ │ │ ├── apply-response.ts │ │ │ ├── index.ts │ │ │ ├── response-processor │ │ │ │ └── response-processor.ts │ │ │ ├── types │ │ │ │ └── original-file-state.ts │ │ │ └── utils │ │ │ ├── preview │ │ │ │ ├── file-preparer.ts │ │ │ │ ├── index.ts │ │ │ │ ├── preview.ts │ │ │ │ └── workspace-listener.ts │ │ │ ├── preview-handler.ts │ │ │ └── state-manager.ts │ │ └── checkpoints │ │ ├── actions │ │ │ ├── accept-checkpoint.ts │ │ │ ├── create-checkpoint.ts │ │ │ ├── delete-checkpoint.ts │ │ │ └── get-checkpoints.ts │ │ ├── types │ │ │ └── checkpoint.ts │ │ └── utils │ │ ├── checkpoints-metadata.ts │ │ └── get-checkpoints-storage-key.ts │ ├── i18n │ │ └── translations │ │ └── commands │ │ └── history-command.ts │ ├── migrations │ │ └── migrate-checkpoints-to-global-storage.ts │ ├── utils │ │ └── workspace-snapshot.ts │ └── views │ └── prompt │ ├── backend │ │ ├── message-handlers │ │ │ ├── handle-apply-response-from-history.ts │ │ │ └── handle-response-preview.ts │ │ └── prompt-view-provider.ts │ ├── frontend │ │ ├── Main │ │ │ ├── Main.tsx │ │ │ └── MainView │ │ │ └── MainView.tsx │ │ ├── Prompt.tsx │ │ └── hooks │ │ ├── use-preview-manager.ts │ │ └── use-response-history.ts │ └── types │ └── messages.ts └── packages ├── shared │ └── src │ └── types │ ├── file-in-preview.ts │ └── response-history-item.ts └── ui └── src └── components └── editor └── prompt-view └── Responses └── Responses.tsx
1 parent c7ffadc commit a86473b

7 files changed

Lines changed: 136 additions & 77 deletions

File tree

‎apps/editor/src/commands/history-command/actions/restore-checkpoint.ts‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const restore_checkpoint = async (params: {
3232
prompt_view_provider: PromptViewProvider
3333
options?: {
3434
skip_confirmation?: boolean
35+
hide_notification?: boolean
3536
}
3637
}) => {
3738
const operation_in_progress =
@@ -172,16 +173,10 @@ export const restore_checkpoint = async (params: {
172173

173174
if (loaded_history.length > 0) {
174175
params.prompt_view_provider.response_history = loaded_history
175-
params.prompt_view_provider.send_message({
176-
command: 'RESPONSE_HISTORY',
177-
history: loaded_history
178-
})
179-
} else {
176+
params.prompt_view_provider.update_response_history()
177+
} else if (params.checkpoint.trigger !== 'before-response-previewed') {
180178
params.prompt_view_provider.response_history = []
181-
params.prompt_view_provider.send_message({
182-
command: 'RESPONSE_HISTORY',
183-
history: []
184-
})
179+
params.prompt_view_provider.update_response_history()
185180
}
186181

187182
let loaded_completed_tasks: string[] | undefined
@@ -329,14 +324,18 @@ export const restore_checkpoint = async (params: {
329324
: t('command.history-command.progress.restoring')
330325
let temp_check: Checkpoint | undefined
331326
try {
332-
temp_check = await vscode.window.withProgress(
333-
{
334-
location: vscode.ProgressLocation.Notification,
335-
title,
336-
cancellable: false
337-
},
338-
main_task
339-
)
327+
if (params.options?.hide_notification) {
328+
temp_check = await main_task({ report: () => {} })
329+
} else {
330+
temp_check = await vscode.window.withProgress(
331+
{
332+
location: vscode.ProgressLocation.Notification,
333+
title,
334+
cancellable: false
335+
},
336+
main_task
337+
)
338+
}
340339
} catch {
341340
return
342341
}
@@ -347,10 +346,10 @@ export const restore_checkpoint = async (params: {
347346

348347
if (temp_check) {
349348
const action_label = t('command.history-command.action.revert')
350-
const action = await vscode.window.showInformationMessage(
351-
message,
352-
action_label
353-
)
349+
let action: string | undefined
350+
if (!params.options?.hide_notification) {
351+
action = await vscode.window.showInformationMessage(message, action_label)
352+
}
354353
if (action === action_label) {
355354
await restore_checkpoint({
356355
checkpoint: temp_check,
@@ -370,6 +369,8 @@ export const restore_checkpoint = async (params: {
370369
})
371370
}
372371
} else {
373-
vscode.window.showInformationMessage(message)
372+
if (!params.options?.hide_notification) {
373+
vscode.window.showInformationMessage(message)
374+
}
374375
}
375376
}

‎apps/editor/src/constants/state-keys.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export const LAST_UPDATE_CHECK_TIMESTAMP_STATE_KEY =
2727
export const CACHED_UPDATE_INFO_STATE_KEY = 'cached-update-info'
2828

2929
export const CHATS_VIEW_CHAT_HISTORY_STATE_KEY = 'chats-view-chat-history'
30+
export const CHATS_VIEW_RESPONSE_HISTORY_STATE_KEY =
31+
'chats-view-response-history'
3032

3133
export const DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY =
3234
'duplicate-workspace-context'

‎apps/editor/src/features/apply-response/apply-response.ts‎

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import * as vscode from 'vscode'
22
import {
33
accept_checkpoint,
44
create_checkpoint,
5-
delete_checkpoint
5+
delete_checkpoint,
6+
get_checkpoints
67
} from '@/features/checkpoints/actions'
8+
import { restore_checkpoint } from '@/commands/history-command/actions/restore-checkpoint'
79
import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
810
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
911
import { get_response_preview_promise_resolve } from './utils/preview'
@@ -30,6 +32,9 @@ import {
3032
restore_tab_groups,
3133
SavedTabGroups
3234
} from './utils/tab-group-manager'
35+
import { CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY } from '@/constants/state-keys'
36+
import { update_undo_button_state } from './utils/state-manager'
37+
import { Logger } from '@shared/utils/logger'
3338

3439
let in_progress = false
3540
let initialization_mutex = Promise.resolve()
@@ -55,6 +60,82 @@ export const setup_apply_response = (params: {
5560
)
5661
)
5762

63+
initialization_mutex = initialization_mutex.then(async () => {
64+
try {
65+
await params.extension_context.workspaceState.update(
66+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
67+
undefined
68+
)
69+
70+
const checkpoints = await get_checkpoints(params.extension_context)
71+
const preview_checkpoints = checkpoints.filter(
72+
(c) => c.trigger === 'before-response-previewed'
73+
)
74+
75+
for (const tab_group of vscode.window.tabGroups.all) {
76+
for (const tab of tab_group.tabs) {
77+
if (
78+
(tab.input instanceof vscode.TabInputTextDiff &&
79+
(tab.input.original.scheme === 'cwc-preview' ||
80+
tab.input.modified.scheme === 'cwc-preview')) ||
81+
(tab.input instanceof vscode.TabInputText &&
82+
tab.input.uri.scheme === 'cwc-preview')
83+
) {
84+
try {
85+
await vscode.window.tabGroups.close(tab)
86+
} catch {
87+
// Ignore
88+
}
89+
}
90+
}
91+
}
92+
93+
preview_checkpoints.reverse()
94+
for (const cp of preview_checkpoints) {
95+
try {
96+
await restore_checkpoint({
97+
checkpoint: cp,
98+
workspace_provider: params.workspace_provider,
99+
extension_context: params.extension_context,
100+
prompt_view_provider: params.prompt_view_provider,
101+
options: { skip_confirmation: true, hide_notification: true }
102+
})
103+
} catch (err) {
104+
console.error('Failed to restore interrupted preview checkpoint', err)
105+
} finally {
106+
await delete_checkpoint({
107+
extension_context: params.extension_context,
108+
prompt_view_provider: params.prompt_view_provider,
109+
checkpoint_to_delete: cp
110+
})
111+
}
112+
}
113+
114+
update_undo_button_state({
115+
extension_context: params.extension_context,
116+
prompt_view_provider: params.prompt_view_provider,
117+
states: null
118+
})
119+
120+
let history_changed = false
121+
for (const item of params.prompt_view_provider.response_history) {
122+
if (item.is_not_looked_at) {
123+
item.is_not_looked_at = false
124+
history_changed = true
125+
}
126+
}
127+
if (history_changed) {
128+
params.prompt_view_provider.update_response_history()
129+
}
130+
} catch (err) {
131+
Logger.error({
132+
function_name: 'setup_apply_response',
133+
message: 'Error during initialization',
134+
data: err
135+
})
136+
}
137+
})
138+
58139
apply_response_instance = async (args: ApplyResponseArgs) => {
59140
const previous_mutex = initialization_mutex
60141
let release_mutex!: () => void
@@ -229,8 +310,6 @@ export const setup_apply_response = (params: {
229310
}
230311

231312
if (resolve_fn && !is_intelligent_file_search_results) {
232-
const history = params.prompt_view_provider.response_history
233-
234313
let created_at_for_switch: number
235314

236315
if (!args?.created_at) {
@@ -246,11 +325,8 @@ export const setup_apply_response = (params: {
246325
}
247326

248327
created_at_for_switch = new_item.created_at
249-
history.push(new_item)
250-
params.prompt_view_provider.send_message({
251-
command: 'RESPONSE_HISTORY',
252-
history
253-
})
328+
params.prompt_view_provider.response_history.push(new_item)
329+
params.prompt_view_provider.update_response_history()
254330
} else {
255331
created_at_for_switch = args.created_at
256332
}
@@ -304,14 +380,12 @@ export const setup_apply_response = (params: {
304380

305381
if (args?.created_at) {
306382
const target_created_at = args.created_at
307-
const history = params.prompt_view_provider.response_history
308-
const existing = history.find((i) => i.created_at === target_created_at)
383+
const existing = params.prompt_view_provider.response_history.find(
384+
(i) => i.created_at === target_created_at
385+
)
309386
if (existing && existing.is_not_looked_at !== false) {
310387
existing.is_not_looked_at = false
311-
params.prompt_view_provider.send_message({
312-
command: 'RESPONSE_HISTORY',
313-
history
314-
})
388+
params.prompt_view_provider.update_response_history()
315389
}
316390
}
317391

@@ -401,10 +475,7 @@ export const setup_apply_response = (params: {
401475
history.push(new_item)
402476
}
403477

404-
params.prompt_view_provider.send_message({
405-
command: 'RESPONSE_HISTORY',
406-
history
407-
})
478+
params.prompt_view_provider.update_response_history()
408479
}
409480

410481
const history_for_checkpoint = [

‎apps/editor/src/features/apply-response/utils/preview-handler.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ export const preview_handler = async (params: {
7474
)
7575

7676
params.prompt_view_provider.response_history = new_history
77-
params.prompt_view_provider.send_message({
78-
command: 'RESPONSE_HISTORY',
79-
history: new_history
80-
})
77+
params.prompt_view_provider.update_response_history()
8178
}
8279
if (params.original_editor_state) {
8380
try {
@@ -159,10 +156,7 @@ export const preview_handler = async (params: {
159156
if (accepted_states.length > 0) {
160157
if (preview_result.created_at) {
161158
params.prompt_view_provider.response_history = []
162-
params.prompt_view_provider.send_message({
163-
command: 'RESPONSE_HISTORY',
164-
history: []
165-
})
159+
params.prompt_view_provider.update_response_history()
166160
}
167161

168162
const { workspace_map, default_workspace } =

‎apps/editor/src/features/apply-response/utils/preview/workspace-listener.ts‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ const update_response_history = (params: {
7272
}
7373

7474
recalculate_history_item_totals(item_to_update)
75-
params.prompt_view_provider.send_message({
76-
command: 'RESPONSE_HISTORY',
77-
history
78-
})
75+
params.prompt_view_provider.update_response_history()
7976
}
8077
}
8178

@@ -120,10 +117,7 @@ export const setup_workspace_listeners = (params: {
120117
f.file_path !== file_path || f.workspace_name !== workspace_name
121118
)
122119
recalculate_history_item_totals(item_to_update)
123-
params.prompt_view_provider.send_message({
124-
command: 'RESPONSE_HISTORY',
125-
history
126-
})
120+
params.prompt_view_provider.update_response_history()
127121
}
128122
}
129123
}
@@ -959,10 +953,7 @@ export const setup_workspace_listeners = (params: {
959953
if (file_in_history) {
960954
file_in_history.is_checked = is_checked
961955
recalculate_history_item_totals(item_to_update)
962-
params.prompt_view_provider.send_message({
963-
command: 'RESPONSE_HISTORY',
964-
history
965-
})
956+
params.prompt_view_provider.update_response_history()
966957
}
967958
}
968959
}
@@ -982,10 +973,7 @@ export const setup_workspace_listeners = (params: {
982973
if (file_in_history) {
983974
file_in_history.is_checked = is_checked
984975
recalculate_history_item_totals(item_to_update)
985-
params.prompt_view_provider.send_message({
986-
command: 'RESPONSE_HISTORY',
987-
history
988-
})
976+
params.prompt_view_provider.update_response_history()
989977
}
990978
}
991979
}

‎apps/editor/src/features/checkpoints/actions/create-checkpoint.ts‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type { Checkpoint, CheckpointTab, CheckpointTrigger } from '../types'
88
import { get_checkpoints, remove_old_checkpoints } from './get-checkpoints'
99
import { get_checkpoint_path, get_incremented_description } from '../utils'
1010
import { create_workspace_snapshot } from '@/utils/workspace-snapshot'
11-
import { is_git_repository } from '@/utils/git-utils'
1211
import { Logger } from '@shared/utils/logger'
1312
import { PromptViewProvider } from '@/views/prompt/backend/prompt-view-provider'
1413
import { response_preview_promise_resolve } from '@/features/apply-response/utils/preview'
@@ -67,18 +66,6 @@ export const create_checkpoint = async (params: {
6766
)
6867
}
6968
const timestamp = Date.now()
70-
const workspace_folders = vscode.workspace.workspaceFolders!
71-
72-
const folder_git_statuses = await Promise.all(
73-
workspace_folders.map((folder) => is_git_repository(folder))
74-
)
75-
76-
if (
77-
trigger !== 'manual' &&
78-
folder_git_statuses.some((has_git) => !has_git)
79-
) {
80-
return undefined
81-
}
8269

8370
let new_checkpoint: Checkpoint | undefined
8471

‎apps/editor/src/views/prompt/backend/prompt-view-provider.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
8787
public prompt_view_api_calls_manager!: PromptViewApiCallsManager
8888
public response_history: ResponseHistoryItem[] = []
8989
public message_listeners: ((message: BackendMessage) => void)[] = []
90+
91+
public update_response_history() {
92+
this.extension_context.workspaceState.update(
93+
state_keys.CHATS_VIEW_RESPONSE_HISTORY_STATE_KEY,
94+
this.response_history
95+
)
96+
this.send_message({
97+
command: 'RESPONSE_HISTORY',
98+
history: this.response_history
99+
})
100+
}
90101
public symbols_cache = new SymbolCacheManager()
91102
public agentic_search_state: AgenticSearchState
92103
public completed_tasks: string[] = []
@@ -249,6 +260,10 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
249260
[]
250261
)
251262

263+
this.response_history = this.extension_context.workspaceState.get<
264+
ResponseHistoryItem[]
265+
>(state_keys.CHATS_VIEW_RESPONSE_HISTORY_STATE_KEY, [])
266+
252267
this.edit_format =
253268
this.extension_context.workspaceState.get<EditFormat>(
254269
state_keys.EDIT_FORMAT_STATE_KEY
@@ -792,6 +807,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
792807
this.response_history = this.response_history.filter(
793808
(item) => item.created_at !== message.created_at
794809
)
810+
this.update_response_history()
795811
} else if (message.command === 'GET_WORKSPACE_STATE') {
796812
handlers.handle_get_workspace_state(this)
797813
} else if (message.command === 'PICK_MODEL') {

0 commit comments

Comments
 (0)