@@ -6,76 +6,10 @@ import {
66 config_preset_to_ui_format ,
77 ConfigPresetFormat
88} from '@/views/panel/backend/utils/preset-format-converters'
9- import { CreatePresetGroupOrSeparatorMessage } from '@/views/panel/types/messages'
9+ import { CreatePresetMessage } from '@/views/panel/types/messages'
1010
11- const ITEM_NAME_GROUP = 'Group'
12- const ITEM_NAME_SEPARATOR = 'Separator'
1311const ITEM_NAME_PRESET = 'Preset'
1412
15- const create_group = async ( params : {
16- panel_provider : PanelProvider
17- insertion_index ?: number
18- } ) => {
19- const config = vscode . workspace . getConfiguration ( 'codeWebChat' )
20- const presets_config_key = params . panel_provider . get_presets_config_key ( )
21- const current_presets =
22- config . get < ConfigPresetFormat [ ] > ( presets_config_key , [ ] ) || [ ]
23-
24- let new_name = ''
25- let copy_number = 0
26- while ( new_name == '' || current_presets . some ( ( p ) => p . name == new_name ) ) {
27- new_name = `(${ copy_number ++ } )`
28- }
29-
30- const new_group = {
31- name : new_name
32- } as ConfigPresetFormat
33-
34- const updated_presets = [ ...current_presets ]
35- if ( params . insertion_index !== undefined ) {
36- updated_presets . splice ( params . insertion_index , 0 , new_group )
37- } else {
38- updated_presets . push ( new_group )
39- }
40-
41- try {
42- params . panel_provider . send_message ( {
43- command : 'PRESET_CREATED' ,
44- preset : config_preset_to_ui_format ( new_group )
45- } )
46- await config . update ( presets_config_key , updated_presets , true )
47- } catch ( error ) {
48- vscode . window . showErrorMessage (
49- dictionary . error_message . FAILED_TO_CREATE_ITEM ( ITEM_NAME_GROUP , error )
50- )
51- }
52- }
53-
54- const create_separator = async ( params : {
55- panel_provider : PanelProvider
56- insertion_index ?: number
57- } ) => {
58- const config = vscode . workspace . getConfiguration ( 'codeWebChat' )
59- const presets_config_key = params . panel_provider . get_presets_config_key ( )
60- const current_presets =
61- config . get < ConfigPresetFormat [ ] > ( presets_config_key , [ ] ) || [ ]
62-
63- const new_separator = { } as ConfigPresetFormat
64-
65- const updated_presets = [ ...current_presets ]
66- if ( params . insertion_index !== undefined ) {
67- updated_presets . splice ( params . insertion_index , 0 , new_separator )
68- } else {
69- updated_presets . push ( new_separator )
70- }
71-
72- try {
73- await config . update ( presets_config_key , updated_presets , true )
74- } catch ( error ) {
75- vscode . window . showErrorMessage ( `Failed to create ${ ITEM_NAME_SEPARATOR } ` )
76- }
77- }
78-
7913const create_preset = async ( params : {
8014 panel_provider : PanelProvider
8115 insertion_index ?: number
@@ -105,15 +39,7 @@ const create_preset = async (params: {
10539 if ( params . insertion_index !== undefined ) {
10640 updated_presets . splice ( params . insertion_index , 0 , new_preset )
10741 } else {
108- const last_group_or_separator = [ ...current_presets ]
109- . reverse ( )
110- . find ( ( p ) => ( ! p . chatbot && p . name ) || ! p . name )
111-
112- if ( last_group_or_separator ?. name && ! last_group_or_separator . chatbot ) {
113- updated_presets . push ( { } , new_preset )
114- } else {
115- updated_presets . push ( new_preset )
116- }
42+ updated_presets . push ( new_preset )
11743 }
11844
11945 try {
@@ -129,9 +55,9 @@ const create_preset = async (params: {
12955 }
13056}
13157
132- export const handle_create_preset_group_or_separator = async (
58+ export const handle_create_preset = async (
13359 panel_provider : PanelProvider ,
134- message : CreatePresetGroupOrSeparatorMessage
60+ message : CreatePresetMessage
13561) : Promise < void > => {
13662 let insertion_index : number | undefined
13763
@@ -184,43 +110,6 @@ export const handle_create_preset_group_or_separator = async (
184110 insertion_index = 0
185111 }
186112
187- const show_item_types_menu = ( ) => {
188- const quickPick = vscode . window . createQuickPick ( )
189- quickPick . items = [
190- { label : ITEM_NAME_PRESET } ,
191- { label : ITEM_NAME_GROUP } ,
192- { label : ITEM_NAME_SEPARATOR }
193- ]
194- quickPick . title = 'Item Types'
195- quickPick . placeholder = 'What would you like to create?'
196- quickPick . buttons = [
197- {
198- iconPath : new vscode . ThemeIcon ( 'close' ) ,
199- tooltip : 'Close'
200- }
201- ]
202-
203- quickPick . onDidTriggerButton ( ( ) => {
204- quickPick . hide ( )
205- } )
206-
207- quickPick . onDidAccept ( async ( ) => {
208- const selection = quickPick . selectedItems [ 0 ] ?. label
209- quickPick . hide ( )
210-
211- if ( selection == ITEM_NAME_GROUP ) {
212- await create_group ( { panel_provider, insertion_index } )
213- } else if ( selection == ITEM_NAME_SEPARATOR ) {
214- await create_separator ( { panel_provider, insertion_index } )
215- } else if ( selection == ITEM_NAME_PRESET ) {
216- show_chatbots_menu ( )
217- }
218- } )
219-
220- quickPick . onDidHide ( ( ) => quickPick . dispose ( ) )
221- quickPick . show ( )
222- }
223-
224113 const show_chatbots_menu = ( ) => {
225114 const chatbots = Object . entries ( CHATBOTS )
226115 const items : vscode . QuickPickItem [ ] = chatbots . map (
@@ -237,21 +126,20 @@ export const handle_create_preset_group_or_separator = async (
237126 quick_pick . items = items
238127 quick_pick . title = 'Chatbots'
239128 quick_pick . placeholder = 'Choose a chatbot for the new preset'
240- quick_pick . buttons = [ vscode . QuickInputButtons . Back ]
129+ quick_pick . buttons = [
130+ {
131+ iconPath : new vscode . ThemeIcon ( 'close' ) ,
132+ tooltip : 'Close'
133+ }
134+ ]
241135
242- let is_handled = false
243136 const disposables : vscode . Disposable [ ] = [ ]
244137
245138 disposables . push (
246- quick_pick . onDidTriggerButton ( ( button ) => {
247- if ( button === vscode . QuickInputButtons . Back ) {
248- is_handled = true
139+ quick_pick . onDidTriggerButton ( ( ) => {
249140 quick_pick . hide ( )
250- show_item_types_menu ( )
251- }
252141 } ) ,
253142 quick_pick . onDidAccept ( async ( ) => {
254- is_handled = true
255143 const selected_chatbot = quick_pick . selectedItems [ 0 ]
256144 ?. label as keyof typeof CHATBOTS
257145 quick_pick . hide ( )
@@ -264,9 +152,6 @@ export const handle_create_preset_group_or_separator = async (
264152 }
265153 } ) ,
266154 quick_pick . onDidHide ( ( ) => {
267- if ( ! is_handled ) {
268- show_item_types_menu ( )
269- }
270155 disposables . forEach ( ( d ) => d . dispose ( ) )
271156 quick_pick . dispose ( )
272157 } )
@@ -275,5 +160,5 @@ export const handle_create_preset_group_or_separator = async (
275160 quick_pick . show ( )
276161 }
277162
278- show_item_types_menu ( )
163+ show_chatbots_menu ( )
279164}
0 commit comments