Skip to content

Commit f7388cc

Browse files
committed
Refactor the custom model provider editing flow to use a single quick pick menu with save and redo buttons, eliminating intermediate quick picks and simplifying input box handling
1 parent 4a1509d commit f7388cc

1 file changed

Lines changed: 49 additions & 141 deletions

File tree

‎packages/vscode/src/views/settings/backend/message-handlers/handle-edit-custom-model-provider.ts‎

Lines changed: 49 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const handle_edit_custom_model_provider = async (
3131
return
3232
}
3333

34-
const provider_to_edit: CustomProvider = { ...original_provider }
34+
let provider_to_edit: CustomProvider = { ...original_provider }
3535

3636
const show_options_quick_pick = (): Promise<string | undefined> => {
3737
return new Promise((resolve) => {
@@ -59,6 +59,18 @@ export const handle_edit_custom_model_provider = async (
5959
quick_pick.items = items
6060
quick_pick.title = `Edit "${provider_to_edit.name}" Model Provider`
6161

62+
const redo_button: vscode.QuickInputButton = {
63+
iconPath: new vscode.ThemeIcon('redo'),
64+
tooltip: 'Cancel all changes'
65+
}
66+
67+
const save_button: vscode.QuickInputButton = {
68+
iconPath: new vscode.ThemeIcon('save'),
69+
tooltip: 'Save changes'
70+
}
71+
72+
quick_pick.buttons = [redo_button, save_button]
73+
6274
let is_accepted = false
6375
const disposables: vscode.Disposable[] = []
6476

@@ -68,6 +80,15 @@ export const handle_edit_custom_model_provider = async (
6880
}
6981

7082
disposables.push(
83+
quick_pick.onDidTriggerButton((button) => {
84+
is_accepted = true
85+
if (button === redo_button) {
86+
resolve('redo')
87+
} else if (button === save_button) {
88+
resolve('save')
89+
}
90+
quick_pick.hide()
91+
}),
7192
quick_pick.onDidAccept(() => {
7293
is_accepted = true
7394
const selected = quick_pick.selectedItems[0] as
@@ -96,8 +117,6 @@ export const handle_edit_custom_model_provider = async (
96117
input_box.value = provider_to_edit.name
97118
input_box.valueSelection = [0, input_box.value.length]
98119

99-
input_box.buttons = [vscode.QuickInputButtons.Back]
100-
101120
let is_accepted = false
102121
const disposables: vscode.Disposable[] = []
103122

@@ -107,13 +126,6 @@ export const handle_edit_custom_model_provider = async (
107126
}
108127

109128
disposables.push(
110-
input_box.onDidTriggerButton((button) => {
111-
if (button === vscode.QuickInputButtons.Back) {
112-
is_accepted = true
113-
resolve(input_box.value)
114-
cleanup()
115-
}
116-
}),
117129
input_box.onDidAccept(async () => {
118130
const new_name = input_box.value
119131

@@ -140,8 +152,8 @@ export const handle_edit_custom_model_provider = async (
140152
}),
141153
input_box.onDidHide(() => {
142154
if (!is_accepted) {
143-
// Escaped, preserve value
144-
resolve(input_box.value)
155+
// Escaped, cancel changes
156+
resolve(provider_to_edit.name)
145157
cleanup()
146158
}
147159
})
@@ -158,7 +170,6 @@ export const handle_edit_custom_model_provider = async (
158170
input_box.prompt = 'Enter the new base URL'
159171
input_box.value = provider_to_edit.base_url
160172
input_box.valueSelection = [0, input_box.value.length]
161-
input_box.buttons = [vscode.QuickInputButtons.Back]
162173

163174
let is_accepted = false
164175
const disposables: vscode.Disposable[] = []
@@ -169,13 +180,6 @@ export const handle_edit_custom_model_provider = async (
169180
}
170181

171182
disposables.push(
172-
input_box.onDidTriggerButton((button) => {
173-
if (button === vscode.QuickInputButtons.Back) {
174-
is_accepted = true
175-
resolve(input_box.value)
176-
cleanup()
177-
}
178-
}),
179183
input_box.onDidAccept(async () => {
180184
is_accepted = true
181185
const new_base_url = input_box.value
@@ -184,7 +188,7 @@ export const handle_edit_custom_model_provider = async (
184188
}),
185189
input_box.onDidHide(() => {
186190
if (!is_accepted) {
187-
resolve(input_box.value)
191+
resolve(provider_to_edit.base_url)
188192
cleanup()
189193
}
190194
})
@@ -193,55 +197,6 @@ export const handle_edit_custom_model_provider = async (
193197
})
194198
}
195199

196-
const show_api_key_options_quick_pick = (): Promise<
197-
'change' | 'clear' | undefined
198-
> => {
199-
return new Promise((resolve) => {
200-
const items: (vscode.QuickPickItem & { id: 'change' | 'clear' })[] = [
201-
{ label: 'Change', id: 'change' },
202-
{ label: 'Clear', id: 'clear' }
203-
]
204-
205-
const quick_pick = vscode.window.createQuickPick()
206-
quick_pick.items = items
207-
quick_pick.title = 'API Key'
208-
quick_pick.buttons = [vscode.QuickInputButtons.Back]
209-
210-
let is_accepted = false
211-
const disposables: vscode.Disposable[] = []
212-
213-
const cleanup = () => {
214-
disposables.forEach((d) => d.dispose())
215-
quick_pick.dispose()
216-
}
217-
218-
disposables.push(
219-
quick_pick.onDidTriggerButton((button) => {
220-
if (button === vscode.QuickInputButtons.Back) {
221-
is_accepted = true // prevent onDidHide from resolving again
222-
resolve(undefined)
223-
cleanup()
224-
}
225-
}),
226-
quick_pick.onDidAccept(() => {
227-
is_accepted = true
228-
const selected = quick_pick.selectedItems[0] as
229-
| (typeof items)[0]
230-
| undefined
231-
resolve(selected?.id)
232-
quick_pick.hide()
233-
}),
234-
quick_pick.onDidHide(() => {
235-
if (!is_accepted) {
236-
resolve(undefined)
237-
}
238-
cleanup()
239-
})
240-
)
241-
quick_pick.show()
242-
})
243-
}
244-
245200
const prompt_for_key = (): Promise<{
246201
value: string
247202
accepted: boolean
@@ -254,7 +209,6 @@ export const handle_edit_custom_model_provider = async (
254209
input_box.placeholder = provider_to_edit.api_key
255210
? `...${provider_to_edit.api_key.slice(-4)}`
256211
: 'No API key set'
257-
input_box.buttons = [vscode.QuickInputButtons.Back]
258212

259213
let is_accepted = false
260214
const disposables: vscode.Disposable[] = []
@@ -265,13 +219,6 @@ export const handle_edit_custom_model_provider = async (
265219
}
266220

267221
disposables.push(
268-
input_box.onDidTriggerButton((button) => {
269-
if (button === vscode.QuickInputButtons.Back) {
270-
is_accepted = true
271-
resolve({ value: input_box.value, accepted: false })
272-
cleanup()
273-
}
274-
}),
275222
input_box.onDidAccept(async () => {
276223
is_accepted = true
277224
resolve({ value: input_box.value, accepted: true })
@@ -292,10 +239,15 @@ export const handle_edit_custom_model_provider = async (
292239
while (true) {
293240
const selected_id = await show_options_quick_pick()
294241

295-
if (selected_id === undefined) {
242+
if (selected_id === undefined || selected_id == 'save') {
296243
break
297244
}
298245

246+
if (selected_id == 'redo') {
247+
provider_to_edit = { ...original_provider }
248+
continue
249+
}
250+
299251
if (selected_id == 'rename') {
300252
const new_name = await prompt_for_name()
301253
const trimmed_new_name = new_name.trim()
@@ -309,70 +261,26 @@ export const handle_edit_custom_model_provider = async (
309261
provider_to_edit.base_url = normalized_new_url
310262
}
311263
} else if (selected_id == 'change-key') {
312-
if (!provider_to_edit.api_key) {
313-
// API key is not set, prompt for it directly
314-
const { value: new_key, accepted } = await prompt_for_key()
315-
if (accepted || new_key) {
316-
provider_to_edit.api_key = new_key.trim()
317-
}
318-
} else {
319-
// API key is set, show change/clear options
320-
// eslint-disable-next-line no-constant-condition
321-
while (true) {
322-
const action = await show_api_key_options_quick_pick()
323-
324-
if (action === undefined) {
325-
break // User escaped from change/clear, go to main menu
326-
}
327-
328-
if (action == 'change') {
329-
const current_api_key = provider_to_edit.api_key
330-
const { value: new_key, accepted } = await prompt_for_key()
331-
const trimmed_api_key = new_key.trim()
332-
333-
if (!accepted) {
334-
if (new_key && trimmed_api_key !== current_api_key) {
335-
provider_to_edit.api_key = trimmed_api_key
336-
}
337-
continue // User cancelled from input, show change/clear again
338-
}
339-
340-
if (trimmed_api_key === current_api_key) {
341-
break // No change, back to main menu
342-
}
343-
344-
if (
345-
trimmed_api_key == '' &&
346-
current_api_key &&
347-
current_api_key != ''
348-
) {
349-
const confirmation = await vscode.window.showWarningMessage(
350-
`Are you sure you want to clear the API key for ${provider_to_edit.name}? This action cannot be undone.`,
351-
{ modal: true },
352-
'Clear API Key'
353-
)
354-
if (confirmation != 'Clear API Key') {
355-
break // Don't clear, back to main menu
356-
}
357-
}
358-
provider_to_edit.api_key = trimmed_api_key
359-
break // Key changed, back to main menu
360-
} else if (action == 'clear') {
361-
if (provider_to_edit.api_key && provider_to_edit.api_key != '') {
362-
const confirmation = await vscode.window.showWarningMessage(
363-
`Are you sure you want to clear the API key for ${provider_to_edit.name}? This action cannot be undone.`,
364-
{ modal: true },
365-
'Clear API Key'
366-
)
367-
368-
if (confirmation == 'Clear API Key') {
369-
provider_to_edit.api_key = ''
370-
}
371-
}
372-
break // Cleared or cancelled, back to main menu
373-
}
264+
const current_api_key = provider_to_edit.api_key
265+
const { value: new_key, accepted } = await prompt_for_key()
266+
if (!accepted) {
267+
continue
268+
}
269+
const trimmed_api_key = new_key.trim()
270+
if (trimmed_api_key === current_api_key) {
271+
continue
272+
}
273+
if (trimmed_api_key === '' && current_api_key) {
274+
const confirmation = await vscode.window.showWarningMessage(
275+
`Are you sure you want to clear the API key for ${provider_to_edit.name}? This action cannot be undone.`,
276+
{ modal: true },
277+
'Clear API Key'
278+
)
279+
if (confirmation !== 'Clear API Key') {
280+
continue
374281
}
375282
}
283+
provider_to_edit.api_key = trimmed_api_key
376284
}
377285
}
378286

0 commit comments

Comments
 (0)