Skip to content

Commit 7ad0267

Browse files
committed
Implement checkpoint lifespan setting and restore checkpoint operation in progress handling
1 parent 440b7f7 commit 7ad0267

16 files changed

Lines changed: 207 additions & 31 deletions

File tree

‎packages/ui/src/components/editor/common/Input/Input.module.scss‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
border-color: var(--vscode-focusBorder);
1616
}
1717
}
18+
19+
.invalid {
20+
border-color: var(--vscode-inputValidation-errorBorder) !important;
21+
background: var(--vscode-inputValidation-errorBackground);
22+
color: var(--vscode-errorForeground);
23+
}

‎packages/ui/src/components/editor/common/Input/Input.stories.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const Primary = () => {
99
const [value, set_value] = useState('')
1010

1111
return (
12-
<Input value={value} onChange={set_value} placeholder="Enter text here" />
12+
<Input value={value} on_change={set_value} placeholder="Enter text here" />
1313
)
1414
}
1515

@@ -19,7 +19,7 @@ export const WithValue = () => {
1919
return (
2020
<Input
2121
value={value}
22-
onChange={set_value}
22+
on_change={set_value}
2323
placeholder="Will be ignored because value is set"
2424
/>
2525
)
@@ -30,7 +30,7 @@ export const WithMaxWidth = () => {
3030
return (
3131
<Input
3232
value={value}
33-
onChange={set_value}
33+
on_change={set_value}
3434
placeholder="Max width is 200px"
3535
max_width={200}
3636
/>

‎packages/ui/src/components/editor/common/Input/Input.tsx‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ type Props = {
77
placeholder?: string
88
max_width?: number
99
width?: number
10-
onChange: (value: string) => void
11-
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => void
10+
on_change: (value: string) => void
11+
on_key_down?: (e: React.KeyboardEvent<HTMLInputElement>) => void
1212
min?: number
1313
max?: number
1414
step?: number
@@ -23,14 +23,25 @@ export const Input: React.FC<Props> = (props) => {
2323
style.width = `${props.width}px`
2424
}
2525

26+
const numeric_value = Number(props.value)
27+
const is_out_of_range =
28+
!Number.isNaN(numeric_value) &&
29+
((props.min !== undefined && numeric_value < props.min) ||
30+
(props.max !== undefined && numeric_value > props.max))
31+
32+
const class_names = [styles.input]
33+
if (is_out_of_range) {
34+
class_names.push(styles.invalid)
35+
}
36+
2637
return (
2738
<input
2839
id={props.id}
2940
type={props.type || 'text'}
3041
value={props.value}
31-
onChange={(e) => props.onChange(e.target.value)}
32-
onKeyDown={props.onKeyDown}
33-
className={styles.input}
42+
onChange={(e) => props.on_change(e.target.value)}
43+
onKeyDown={props.on_key_down}
44+
className={class_names.join(' ')}
3445
placeholder={props.placeholder}
3546
style={Object.keys(style).length > 0 ? style : undefined}
3647
min={props.min}

‎packages/ui/src/components/editor/panel/Field/Field.stories.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const TextInput = () => {
1414
<Field label="Username" html_for="username">
1515
<Input
1616
value={value}
17-
onChange={set_value}
17+
on_change={set_value}
1818
placeholder="Enter your username"
1919
/>
2020
</Field>
@@ -30,7 +30,7 @@ export const WithInfo = () => {
3030
html_for="api-key"
3131
info="Your API key will be stored securely."
3232
>
33-
<Input value={value} onChange={set_value} placeholder="Enter API key" />
33+
<Input value={value} on_change={set_value} placeholder="Enter API key" />
3434
</Field>
3535
)
3636
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const Slider: React.FC<Props> = (props) => {
3636
max={props.max}
3737
step={0.05}
3838
value={value}
39-
onChange={handleInputChange}
39+
on_change={handleInputChange}
4040
width={60}
4141
/>
4242
<input

‎packages/vscode/package.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,13 @@
553553
"default": 60000,
554554
"description": "Threshold for the context size to show a warning."
555555
},
556+
"codeWebChat.checkpointLifespan": {
557+
"type": "number",
558+
"scope": "resource",
559+
"default": 48,
560+
"description": "The lifespan of checkpoints in hours. Checkpoints older than this will be automatically deleted.",
561+
"minimum": 1
562+
},
556563
"codeWebChat.clearChecksInWorkspaceBehavior": {
557564
"type": "string",
558565
"enum": [

‎packages/vscode/src/commands/checkpoints-command/actions/create-checkpoint.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,52 @@ import { Logger } from '@shared/utils/logger'
2222
import { PanelProvider } from '@/views/panel/backend/panel-provider'
2323
import { dictionary } from '@shared/constants/dictionary'
2424

25+
const remove_old_checkpoints = async (
26+
checkpoints: Checkpoint[]
27+
): Promise<Checkpoint[]> => {
28+
const config = vscode.workspace.getConfiguration('codeWebChat')
29+
const checkpoint_lifespan_hours =
30+
config.get<number>('checkpointLifespan') || 48
31+
const checkpoint_lifespan_ms = checkpoint_lifespan_hours * 60 * 60 * 1000
32+
33+
const now = Date.now()
34+
const cutoff_time = now - checkpoint_lifespan_ms
35+
36+
const checkpoints_to_keep: Checkpoint[] = []
37+
const checkpoints_to_remove: Checkpoint[] = []
38+
39+
for (const checkpoint of checkpoints) {
40+
if (checkpoint.timestamp < cutoff_time) {
41+
checkpoints_to_remove.push(checkpoint)
42+
} else {
43+
checkpoints_to_keep.push(checkpoint)
44+
}
45+
}
46+
47+
// Delete old checkpoint files
48+
for (const checkpoint of checkpoints_to_remove) {
49+
try {
50+
const checkpoint_path = get_checkpoint_path(checkpoint.timestamp)
51+
await vscode.workspace.fs.delete(vscode.Uri.file(checkpoint_path), {
52+
recursive: true
53+
})
54+
Logger.info({
55+
function_name: 'remove_old_checkpoints',
56+
message: `Removed checkpoint older than 48 hours: ${checkpoint.title}`,
57+
data: { timestamp: checkpoint.timestamp }
58+
})
59+
} catch (error) {
60+
Logger.warn({
61+
function_name: 'remove_old_checkpoints',
62+
message: 'Could not delete old checkpoint file',
63+
data: error
64+
})
65+
}
66+
}
67+
68+
return checkpoints_to_keep
69+
}
70+
2571
export const create_checkpoint = async (
2672
workspace_provider: WorkspaceProvider,
2773
context: vscode.ExtensionContext,
@@ -159,7 +205,10 @@ export const create_checkpoint = async (
159205
}
160206
}
161207

162-
const checkpoints = await get_checkpoints(context)
208+
let checkpoints = await get_checkpoints(context)
209+
210+
// Remove checkpoints older than 48 hours
211+
checkpoints = await remove_old_checkpoints(checkpoints)
163212

164213
const final_description = get_incremented_description(
165214
title,

‎packages/vscode/src/commands/checkpoints-command/actions/restore-checkpoint.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ export const restore_checkpoint = async (params: {
2424
panel_provider: PanelProvider
2525
options?: { skip_confirmation?: boolean }
2626
}) => {
27-
const operation_in_progress = params.context.workspaceState.get<boolean>(
27+
const operation_in_progress = params.context.workspaceState.get<number>(
2828
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY
2929
)
30-
if (operation_in_progress) {
30+
if (
31+
operation_in_progress &&
32+
Date.now() - operation_in_progress < 60 * 1000
33+
) {
3134
vscode.window.showWarningMessage(
3235
dictionary.warning_message.CHECKPOINT_OPERATION_IN_PROGRESS
3336
)
@@ -116,7 +119,7 @@ export const restore_checkpoint = async (params: {
116119
try {
117120
await params.context.workspaceState.update(
118121
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
119-
true
122+
Date.now()
120123
)
121124
await vscode.window.withProgress(
122125
{
@@ -345,6 +348,11 @@ export const restore_checkpoint = async (params: {
345348
}
346349
}
347350

351+
await params.context.workspaceState.update(
352+
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
353+
undefined
354+
)
355+
348356
const message = params.options?.skip_confirmation
349357
? 'Successfully reverted changes.'
350358
: `Checkpoint restored successfully.`
@@ -378,7 +386,7 @@ export const restore_checkpoint = async (params: {
378386
} catch (err: any) {
379387
await params.context.workspaceState.update(
380388
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
381-
false
389+
undefined
382390
)
383391
vscode.window.showErrorMessage(
384392
`Failed to restore checkpoint: ${err.message}`
@@ -394,10 +402,6 @@ export const restore_checkpoint = async (params: {
394402
undefined
395403
)
396404
}
397-
} finally {
398-
await params.context.workspaceState.update(
399-
CHECKPOINT_OPERATION_IN_PROGRESS_STATE_KEY,
400-
false
401-
)
402405
}
403406
}
407+

‎packages/vscode/src/views/panel/frontend/components/edit-preset-form/EditPresetForm/EditPresetForm.tsx‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,14 @@ export const EditPresetForm: React.FC<Props> = (props) => {
284284
id="custom-model"
285285
type="text"
286286
value={model || ''}
287-
onChange={set_model}
287+
on_change={set_model}
288288
placeholder="Enter model name"
289289
/>
290290
</Field>
291291
)}
292292

293293
<Field label="Name" html_for="name">
294-
<Input id="name" type="text" value={name} onChange={set_name} />
294+
<Input id="name" type="text" value={name} on_change={set_name} />
295295
</Field>
296296

297297
{supports_port && (
@@ -311,12 +311,12 @@ export const EditPresetForm: React.FC<Props> = (props) => {
311311
id="port"
312312
type="text"
313313
value={String(port ?? '')}
314-
onChange={(value) => {
314+
on_change={(value) => {
315315
const num = parseInt(value, 10)
316316
set_port(isNaN(num) ? undefined : num)
317317
}}
318318
placeholder="e.g. 3000"
319-
onKeyDown={(e) =>
319+
on_key_down={(e) =>
320320
!/[0-9]/.test(e.key) &&
321321
e.key != 'Backspace' &&
322322
e.preventDefault()
@@ -335,7 +335,7 @@ export const EditPresetForm: React.FC<Props> = (props) => {
335335
id="new-url"
336336
type="text"
337337
value={new_url || ''}
338-
onChange={set_new_url}
338+
on_change={set_new_url}
339339
/>
340340
</Field>
341341
)}
@@ -413,12 +413,12 @@ export const EditPresetForm: React.FC<Props> = (props) => {
413413
id="thinking-budget"
414414
type="text"
415415
value={String(thinking_budget ?? '')}
416-
onChange={(value) => {
416+
on_change={(value) => {
417417
const num = parseInt(value, 10)
418418
set_thinking_budget(isNaN(num) ? undefined : num)
419419
}}
420420
placeholder="e.g. 8000"
421-
onKeyDown={(e) =>
421+
on_key_down={(e) =>
422422
!/[0-9]/.test(e.key) &&
423423
e.key != 'Backspace' &&
424424
e.preventDefault()

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,18 @@ export class SettingsProvider {
253253
break
254254
case 'UPDATE_GEMINI_USER_ID':
255255
await handle_update_gemini_user_id(this, message)
256+
break
257+
case 'GET_CHECKPOINT_LIFESPAN':
258+
{
259+
const config = vscode.workspace.getConfiguration('codeWebChat')
260+
const hours = config.get<number>('checkpointLifespan', 48)
261+
this.postMessage({ command: 'CHECKPOINT_LIFESPAN', hours })
262+
}
263+
break
264+
case 'UPDATE_CHECKPOINT_LIFESPAN':
265+
if (message.command == 'UPDATE_CHECKPOINT_LIFESPAN')
266+
await vscode.workspace.getConfiguration('codeWebChat').update('checkpointLifespan', message.hours, vscode.ConfigurationTarget.Global)
267+
256268
break
257269
case 'OPEN_EDITOR_SETTINGS':
258270
await vscode.commands.executeCommand(
@@ -279,6 +291,11 @@ export class SettingsProvider {
279291
void handle_get_commit_message_instructions(this)
280292
void handle_get_commit_message_auto_accept_after(this)
281293
void handle_get_clear_checks_in_workspace_behavior(this)
294+
{
295+
const config = vscode.workspace.getConfiguration('codeWebChat')
296+
const hours = config.get<number>('checkpointLifespan', 48)
297+
this.postMessage({ command: 'CHECKPOINT_LIFESPAN', hours })
298+
}
282299
void handle_get_gemini_user_id(this)
283300
}
284301
})

0 commit comments

Comments
 (0)