@@ -23,6 +23,7 @@ import {
2323} from '@/constants/edit-format-instructions'
2424import { handle_update_last_used_web_configuration_or_group } from './handle-update-last-used-web-configuration-or-group'
2525import { replace_symbols } from '@/views/panel/backend/utils/symbols/replace-symbols'
26+ import { split_recent_and_rest_configurations } from '@/views/panel/backend/utils/split-recent-and-rest-configurations'
2627
2728export const handle_send_to_browser = async ( params : {
2829 panel_provider : PanelProvider
@@ -227,6 +228,14 @@ const show_web_configuration_quick_pick = async (params: {
227228} ) : Promise < { web_configuration_name : string | undefined } | null > => {
228229 const { web_configurations, context, prompt_type, panel_provider } = params
229230
231+ const valid_web_configurations = web_configurations . filter ( ( c ) => c . chatbot )
232+
233+ if ( valid_web_configurations . length == 0 ) {
234+ vscode . commands . executeCommand ( 'codeWebChat.settings' )
235+ vscode . window . showInformationMessage ( 'No configurations found.' )
236+ return null
237+ }
238+
230239 const quick_pick = vscode . window . createQuickPick <
231240 vscode . QuickPickItem & { web_configuration_name ?: string }
232241 > ( )
@@ -237,50 +246,55 @@ const show_web_configuration_quick_pick = async (params: {
237246 context . globalState . get < string [ ] > ( recents_key ) ??
238247 [ ]
239248
240- if ( recent_names . length == 0 ) {
241- vscode . window . showWarningMessage (
242- dictionary . warning_message . NO_RECENTLY_USED_PRESETS_OR_GROUPS // the dictionary key is kept, string may differ
243- )
244- return null
249+ const { recent : matched_recent , rest : remaining } = split_recent_and_rest_configurations (
250+ valid_web_configurations ,
251+ recent_names ,
252+ ( c ) => c . name
253+ )
254+
255+ const map_web_configuration_to_item = ( web_configuration : ConfigWebConfigurationFormat ) => {
256+ const is_unnamed = ! web_configuration . name || / ^ \( \d + \) $ / . test ( web_configuration . name . trim ( ) )
257+ const chatbot_models =
258+ CHATBOTS [ web_configuration . chatbot as keyof typeof CHATBOTS ] ?. models
259+ const model = web_configuration . model
260+ ? chatbot_models ?. [ web_configuration . model ] ?. label || web_configuration . model
261+ : ''
262+ return {
263+ label : `${
264+ is_unnamed
265+ ? web_configuration . chatbot !
266+ : web_configuration . name ! . replace ( / \s * \( \d + \) $ / , '' )
267+ } `,
268+ web_configuration_name : web_configuration . name ,
269+ description : is_unnamed
270+ ? model
271+ : `${ web_configuration . chatbot } ${ model ? ` · ${ model } ` : '' } `
272+ }
245273 }
246274
247- const items = recent_names
248- . map ( ( name ) => {
249- const item = web_configurations . find ( ( p ) => p . name == name )
250-
251- if ( item && item . chatbot ) {
252- const web_configuration = item
253- const is_unnamed = ! web_configuration . name || / ^ \( \d + \) $ / . test ( web_configuration . name . trim ( ) )
254- const chatbot_models =
255- CHATBOTS [ web_configuration . chatbot as keyof typeof CHATBOTS ] ?. models
256- const model = web_configuration . model
257- ? chatbot_models ?. [ web_configuration . model ] ?. label || web_configuration . model
258- : ''
259- return {
260- label : `${
261- is_unnamed
262- ? web_configuration . chatbot !
263- : web_configuration . name ! . replace ( / \s * \( \d + \) $ / , '' )
264- } `,
265- web_configuration_name : web_configuration . name ,
266- description : is_unnamed
267- ? model
268- : `${ web_configuration . chatbot } ${ model ? ` · ${ model } ` : '' } `
269- }
270- }
271- return null
275+ const items : ( vscode . QuickPickItem & { web_configuration_name ?: string } ) [ ] = [ ]
276+
277+ if ( matched_recent . length > 0 ) {
278+ items . push ( {
279+ label : 'recently used' ,
280+ kind : vscode . QuickPickItemKind . Separator
272281 } )
273- . filter ( ( i ) : i is NonNullable < typeof i > => i !== null )
282+ items . push ( ...matched_recent . map ( map_web_configuration_to_item ) )
283+ }
274284
275- quick_pick . items = items
276- if ( items . length == 0 ) {
277- vscode . window . showWarningMessage (
278- dictionary . warning_message . NO_RECENTLY_USED_PRESETS_OR_GROUPS
279- )
280- return null
285+ if ( remaining . length > 0 ) {
286+ if ( matched_recent . length > 0 ) {
287+ items . push ( {
288+ label : 'other configurations' ,
289+ kind : vscode . QuickPickItemKind . Separator
290+ } )
291+ }
292+ items . push ( ...remaining . map ( map_web_configuration_to_item ) )
281293 }
282- quick_pick . placeholder = 'Search recently used presets'
283- quick_pick . title = 'Recently Used Presets'
294+
295+ quick_pick . items = items
296+ quick_pick . placeholder = 'Select a configuration'
297+ quick_pick . title = 'Configurations'
284298 quick_pick . matchOnDescription = true
285299
286300 const close_button = {
@@ -297,17 +311,27 @@ const show_web_configuration_quick_pick = async (params: {
297311
298312 const last_selected_name = recent_names [ 0 ]
299313
300- let last_selected_web_configuration_name : string | undefined
301314 if ( last_selected_name ) {
302- const item = web_configurations . find ( ( p ) => p . name == last_selected_name )
303- if ( item && item . chatbot ) last_selected_web_configuration_name = last_selected_name
304- }
305-
306- if ( last_selected_web_configuration_name ) {
307315 const last_item = quick_pick . items . find (
308- ( item ) => item . web_configuration_name == last_selected_web_configuration_name
316+ ( item ) => item . web_configuration_name == last_selected_name
309317 )
310- if ( last_item ) quick_pick . activeItems = [ last_item ]
318+ if ( last_item ) {
319+ quick_pick . activeItems = [ last_item ]
320+ } else if ( items . length > 0 ) {
321+ const first_selectable = items . find (
322+ ( i ) => i . kind != vscode . QuickPickItemKind . Separator
323+ )
324+ if ( first_selectable ) {
325+ quick_pick . activeItems = [ first_selectable ]
326+ }
327+ }
328+ } else if ( items . length > 0 ) {
329+ const first_selectable = items . find (
330+ ( i ) => i . kind != vscode . QuickPickItemKind . Separator
331+ )
332+ if ( first_selectable ) {
333+ quick_pick . activeItems = [ first_selectable ]
334+ }
311335 }
312336
313337 return new Promise < { web_configuration_name : string | undefined } | null > ( ( resolve ) => {
@@ -322,36 +346,31 @@ const show_web_configuration_quick_pick = async (params: {
322346 quick_pick . onDidAccept ( async ( ) => {
323347 const selected = quick_pick . selectedItems [ 0 ] as any
324348
325- if ( ! selected ) {
349+ if ( ! selected || ! selected . web_configuration_name ) {
326350 quick_pick . hide ( )
327351 do_resolve ( null )
328352 return
329353 }
330354
331- if ( selected . web_configuration_name ) {
332- const web_configuration = web_configurations . find ( ( p ) => p . name == selected . web_configuration_name ) !
333- if ( params . get_is_web_configuration_disabled ( web_configuration ) ) {
334- if (
335- ! params . is_in_code_completions_mode &&
336- ! params . current_instructions
337- ) {
338- params . panel_provider . send_message ( {
339- command : 'SHOW_AUTO_CLOSING_MODAL' ,
340- title : 'Type something to use this web configuration' ,
341- type : 'warning'
342- } )
343- }
344- do_resolve ( null )
345- } else {
346- handle_update_last_used_web_configuration_or_group ( {
347- panel_provider,
348- web_configuration_name : selected . web_configuration_name
355+ const web_configuration = valid_web_configurations . find ( ( p ) => p . name == selected . web_configuration_name ) !
356+ if ( params . get_is_web_configuration_disabled ( web_configuration ) ) {
357+ if (
358+ ! params . is_in_code_completions_mode &&
359+ ! params . current_instructions
360+ ) {
361+ params . panel_provider . send_message ( {
362+ command : 'SHOW_AUTO_CLOSING_MODAL' ,
363+ title : 'Type something to use this web configuration' ,
364+ type : 'warning'
349365 } )
350- do_resolve ( { web_configuration_name : selected . web_configuration_name } )
351366 }
367+ do_resolve ( null )
352368 } else {
353- quick_pick . hide ( )
354- do_resolve ( { web_configuration_name : undefined } )
369+ handle_update_last_used_web_configuration_or_group ( {
370+ panel_provider,
371+ web_configuration_name : selected . web_configuration_name
372+ } )
373+ do_resolve ( { web_configuration_name : selected . web_configuration_name } )
355374 }
356375 } )
357376
0 commit comments