Skip to content

Commit 00faed6

Browse files
committed
Refactor preset selection storage to be mode-specific for improved persistence across chat modes
1 parent 2457fc9 commit 00faed6

8 files changed

Lines changed: 64 additions & 49 deletions

File tree

packages/vscode/src/commands/chat-commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ export function chat_command(
287287
)
288288

289289
let selected_names = context.globalState.get<string[]>(
290-
'selectedPresets',
290+
'selectedPresets.edit',
291291
[]
292292
)
293293

@@ -315,7 +315,7 @@ export function chat_command(
315315
}
316316

317317
selected_names = selected_presets.map((preset) => preset.label)
318-
await context.globalState.update('selectedPresets', selected_names)
318+
await context.globalState.update('selectedPresets.edit', selected_names)
319319
}
320320

321321
await handle_chat_command(

packages/vscode/src/commands/code-completion-in-chat-commands.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export function code_completion_in_chat_command(
187187
}
188188

189189
let selected_names = context.globalState.get<string[]>(
190-
'selectedPresets',
190+
'selectedPresets.code-completions',
191191
[]
192192
)
193193

@@ -217,13 +217,16 @@ export function code_completion_in_chat_command(
217217
}
218218

219219
selected_names = selected_presets.map((preset) => preset.label)
220-
await context.globalState.update('selectedPresets', selected_names)
220+
await context.globalState.update(
221+
'selectedPresets.code-completions',
222+
selected_names
223+
)
221224
} else {
222225
selected_names = valid_selected_names
223226

224227
if (valid_selected_names.length !== selected_names.length) {
225228
await context.globalState.update(
226-
'selectedPresets',
229+
'selectedPresets.code-completions',
227230
valid_selected_names
228231
)
229232
}

packages/vscode/src/view/backend/message-handlers/handle-delete-preset.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,27 @@ export const handle_delete_preset = async (
7070

7171
provider.send_presets_to_webview(webview_view.webview)
7272

73-
const selected_chat_names = provider.context.globalState.get<string[]>(
74-
'selectedPresets',
75-
[]
76-
)
77-
if (selected_chat_names.includes(preset_name)) {
78-
const updated_selected = selected_chat_names.filter(
79-
(n) => n != preset_name
80-
)
81-
await provider.context.globalState.update(
82-
'selectedPresets',
83-
updated_selected
73+
const modes = ['ask', 'edit', 'code-completions', 'no-context']
74+
for (const mode of modes) {
75+
const state_key = `selectedPresets.${mode}`
76+
const selected_names = provider.context.globalState.get<string[]>(
77+
state_key,
78+
[]
8479
)
85-
provider.send_message<ExtensionMessage>({
86-
command: 'SELECTED_PRESETS',
87-
names: updated_selected
88-
})
80+
if (selected_names.includes(preset_name)) {
81+
const updated_selected = selected_names.filter((n) => n != preset_name)
82+
await provider.context.globalState.update(state_key, updated_selected)
83+
}
8984
}
85+
86+
const current_mode_state_key = provider.get_selected_presets_state_key()
87+
const current_mode_selected_presets = provider.context.globalState.get<
88+
string[]
89+
>(current_mode_state_key, [])
90+
provider.send_message<ExtensionMessage>({
91+
command: 'SELECTED_PRESETS',
92+
names: current_mode_selected_presets
93+
})
9094
} catch (error) {
9195
vscode.window.showErrorMessage(`Failed to delete preset: ${error}`)
9296
}

packages/vscode/src/view/backend/message-handlers/handle-get-selected-presets.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { ViewProvider } from '@/view/backend/view-provider'
22
import { ExtensionMessage } from '@/view/types/messages'
33

44
export const handle_get_selected_presets = (provider: ViewProvider): void => {
5-
const selected_names = provider.context.globalState.get<string[]>(
6-
'selectedPresets',
7-
[]
8-
)
5+
const state_key = provider.get_selected_presets_state_key()
6+
const selected_names =
7+
provider.context.globalState.get<string[]>(state_key, [])
98
provider.send_message<ExtensionMessage>({
109
command: 'SELECTED_PRESETS',
1110
names: selected_names

packages/vscode/src/view/backend/message-handlers/handle-show-preset-picker.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ export const handle_show_preset_picker = async (
1212
const web_chat_presets =
1313
config.get<ConfigPresetFormat[]>(presets_config_key, []) || []
1414

15-
const selected_preset_names_state_key =
16-
provider.web_mode == 'code-completions'
17-
? 'selectedCodeCompletionPresets'
18-
: 'selectedPresets'
19-
15+
const selected_preset_names_state_key = provider.get_selected_presets_state_key()
2016
const available_preset_names = web_chat_presets
2117

2218
.filter(
@@ -76,7 +72,7 @@ export const handle_show_preset_picker = async (
7672
)
7773

7874
if (selected_presets) {
79-
const selected_names = selected_presets.map((preset) => preset.label)
75+
const selected_names = selected_presets.map((preset: any) => preset.name)
8076
await provider.context.globalState.update(
8177
selected_preset_names_state_key,
8278
selected_names

packages/vscode/src/view/backend/message-handlers/handle-update-preset.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,30 @@ export const handle_update_preset = async (
127127
vscode.ConfigurationTarget.Global
128128
)
129129

130-
// Update selected (default) presets for both modes
131-
const selected_chat_names = provider.context.globalState.get<string[]>(
132-
'selectedPresets',
133-
[]
134-
)
135-
if (selected_chat_names.includes(message.updating_preset.name)) {
136-
const updated_selected_names = selected_chat_names.map((name) =>
137-
name == message.updating_preset.name ? final_name : name
138-
)
139-
await provider.context.globalState.update(
140-
'selectedPresets',
141-
updated_selected_names
130+
// Update selected (default) presets for all modes
131+
const modes = ['ask', 'edit', 'code-completions', 'no-context']
132+
for (const mode of modes) {
133+
const state_key = `selectedPresets.${mode}`
134+
const selected_names = provider.context.globalState.get<string[]>(
135+
state_key,
136+
[]
142137
)
143-
// Send updated selected presets to webview
138+
if (selected_names.includes(message.updating_preset.name)) {
139+
const updated_selected_names = selected_names.map((name) =>
140+
name == message.updating_preset.name ? final_name : name
141+
)
142+
await provider.context.globalState.update(state_key, updated_selected_names)
143+
}
144+
}
145+
146+
// Send updated selected presets to webview
147+
const current_mode_state_key = provider.get_selected_presets_state_key()
148+
const current_mode_selected_presets =
149+
provider.context.globalState.get<string[]>(current_mode_state_key, [])
144150
provider.send_message<SelectedPresetsMessage>({
145151
command: 'SELECTED_PRESETS',
146-
names: updated_selected_names
152+
names: current_mode_selected_presets
147153
})
148-
}
149154

150155
provider.send_presets_to_webview(webview_view.webview)
151156
provider.send_message<ExtensionMessage>({

packages/vscode/src/view/backend/view-provider.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ export class ViewProvider implements vscode.WebviewViewProvider {
8585
}
8686
}
8787

88+
public get_selected_presets_state_key(): string {
89+
return `selectedPresets.${this.web_mode}`
90+
}
91+
8892
constructor(
8993
public readonly extension_uri: vscode.Uri,
9094
public readonly workspace_provider: WorkspaceProvider,
@@ -343,10 +347,8 @@ export class ViewProvider implements vscode.WebviewViewProvider {
343347
} else if (message.command == 'GET_SELECTED_PRESETS') {
344348
handle_get_selected_presets(this)
345349
} else if (message.command == 'SAVE_SELECTED_PRESETS') {
346-
await this.context.globalState.update(
347-
'selectedPresets',
348-
message.names
349-
)
350+
const state_key = this.get_selected_presets_state_key()
351+
await this.context.globalState.update(state_key, message.names)
350352
} else if (message.command == 'SEND_PROMPT') {
351353
await handle_send_prompt(this, message.preset_names)
352354
} else if (message.command == 'PREVIEW_PRESET') {

packages/vscode/src/view/frontend/home/Home.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ export const Home: React.FC<Props> = (props) => {
142142
command: 'SAVE_WEB_MODE',
143143
mode: new_mode
144144
} as WebviewMessage)
145+
props.vscode.postMessage({
146+
command: 'GET_SELECTED_PRESETS'
147+
} as WebviewMessage)
145148
props.vscode.postMessage({
146149
command: 'GET_CURRENT_TOKEN_COUNT'
147150
} as WebviewMessage)
@@ -153,6 +156,9 @@ export const Home: React.FC<Props> = (props) => {
153156
command: 'SAVE_API_MODE',
154157
mode: new_mode
155158
} as WebviewMessage)
159+
props.vscode.postMessage({
160+
command: 'GET_SELECTED_PRESETS'
161+
} as WebviewMessage)
156162
props.vscode.postMessage({
157163
command: 'GET_CURRENT_TOKEN_COUNT'
158164
} as WebviewMessage)

0 commit comments

Comments
 (0)