@@ -5,10 +5,11 @@ import {
55 ApiConfiguration
66} from '../../../services/model-providers-manager'
77import { Logger } from '@shared/utils/logger'
8- import { RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY } from '@/constants/state-keys'
8+ import { LAST_USED_CODE_AT_CURSOR_CONFIG_ID_STATE_KEY } from '@/constants/state-keys'
99import { PanelProvider } from '@/views/panel/backend/panel-provider'
1010import { dictionary } from '@shared/constants/dictionary'
1111import { t } from '@/i18n'
12+ import { show_api_configuration_quick_pick } from '@/utils/show-api-configuration-quick-pick'
1213
1314export const get_code_at_cursor_api_configuration = async ( params : {
1415 model_providers_manager : ModelProvidersManager
@@ -41,10 +42,9 @@ export const get_code_at_cursor_api_configuration = async (params: {
4142 if ( default_api_configuration ) {
4243 selected_api_configuration = default_api_configuration
4344 } else {
44- const recents = params . context . workspaceState . get < string [ ] > (
45- RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY
45+ const last_selected_id = params . context . workspaceState . get < string > (
46+ LAST_USED_CODE_AT_CURSOR_CONFIG_ID_STATE_KEY
4647 )
47- const last_selected_id = recents ?. [ 0 ]
4848 if ( last_selected_id ) {
4949 selected_api_configuration =
5050 code_at_cursor_api_configurations . find (
@@ -61,167 +61,36 @@ export const get_code_at_cursor_api_configuration = async (params: {
6161 }
6262
6363 if ( ! selected_api_configuration || params . show_quick_pick ) {
64- const create_items = ( ) => {
65- const recent_ids =
66- params . context . workspaceState . get < string [ ] > (
67- RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY
68- ) || [ ]
69-
70- const matched_recent_api_configurations : ApiConfiguration [ ] = [ ]
71- const remaining_api_configurations : ApiConfiguration [ ] = [ ]
72-
73- code_at_cursor_api_configurations . forEach ( ( api_configuration ) => {
74- const id = get_api_configuration_id ( api_configuration )
75- if ( recent_ids . includes ( id ) ) {
76- matched_recent_api_configurations . push ( api_configuration )
77- } else {
78- remaining_api_configurations . push ( api_configuration )
79- }
80- } )
81-
82- matched_recent_api_configurations . sort ( ( a , b ) => {
83- const id_a = get_api_configuration_id ( a )
84- const id_b = get_api_configuration_id ( b )
85- return recent_ids . indexOf ( id_a ) - recent_ids . indexOf ( id_b )
86- } )
87-
88- const recent_api_configurations = matched_recent_api_configurations
89- const other_api_configurations = remaining_api_configurations
90-
91- const map_api_configuration_to_item = (
92- api_configuration : ApiConfiguration
93- ) => {
94- const description_parts = [ api_configuration . model_provider_name ]
95- if ( api_configuration . reasoning_effort ) {
96- description_parts . push ( `${ api_configuration . reasoning_effort } ` )
97- }
98-
99- const buttons : vscode . QuickInputButton [ ] = [ ]
100-
101- return {
102- label : api_configuration . model ,
103- description : description_parts . join ( ' · ' ) ,
104- buttons,
105- api_configuration,
106- id : get_api_configuration_id ( api_configuration )
107- }
108- }
109-
110- const items : ( vscode . QuickPickItem & {
111- api_configuration ?: ApiConfiguration
112- id ?: string
113- } ) [ ] = [ ]
114-
115- if ( recent_api_configurations . length > 0 ) {
116- items . push ( {
117- label : t ( 'common.separator.recently-used' ) ,
118- kind : vscode . QuickPickItemKind . Separator
119- } )
120- items . push (
121- ...recent_api_configurations . map ( map_api_configuration_to_item )
122- )
123- }
124-
125- if ( other_api_configurations . length > 0 ) {
126- if ( recent_api_configurations . length > 0 ) {
127- items . push ( {
128- label : t ( 'common.config.other' ) ,
129- kind : vscode . QuickPickItemKind . Separator
130- } )
131- }
132- items . push (
133- ...other_api_configurations . map ( map_api_configuration_to_item )
134- )
135- }
136-
137- return items
138- }
139-
140- const quick_pick = vscode . window . createQuickPick ( )
141- quick_pick . buttons = [
142- {
143- iconPath : new vscode . ThemeIcon ( 'close' ) ,
144- tooltip : t ( 'common.close' )
145- }
146- ]
147- quick_pick . items = create_items ( )
148- quick_pick . title = t ( 'common.config.title' )
149- quick_pick . placeholder = t ( 'command.code-at-cursor.config.placeholder' )
150- quick_pick . matchOnDescription = true
64+ const last_selected_id = params . context . workspaceState . get < string > (
65+ LAST_USED_CODE_AT_CURSOR_CONFIG_ID_STATE_KEY
66+ )
15167
152- const items = quick_pick . items as ( vscode . QuickPickItem & { id : string } ) [ ]
68+ const result = await show_api_configuration_quick_pick ( {
69+ api_configurations : code_at_cursor_api_configurations ,
70+ last_selected_id,
71+ placeholder : t ( 'command.code-at-cursor.config.placeholder' )
72+ } )
15373
154- if ( items . length > 0 ) {
155- const first_selectable = items . find (
156- ( i ) => i . kind != vscode . QuickPickItemKind . Separator
157- )
158- if ( first_selectable ) {
159- quick_pick . activeItems = [ first_selectable ]
160- }
74+ if ( ! result || result === 'back' ) {
75+ return undefined
16176 }
16277
163- return new Promise <
164- { model_provider : any ; api_configuration : any } | undefined
165- > ( ( resolve ) => {
166- quick_pick . onDidTriggerButton ( ( button ) => {
167- if ( button . tooltip == t ( 'common.close' ) ) {
168- quick_pick . hide ( )
169- resolve ( undefined )
170- }
171- } )
172-
173- quick_pick . onDidAccept ( async ( ) => {
174- const selected = quick_pick . selectedItems [ 0 ] as any
175- quick_pick . hide ( )
78+ const { api_configuration, id } = result
17679
177- if ( ! selected || ! selected . api_configuration ) {
178- resolve ( undefined )
179- return
180- }
181-
182- let recents =
183- params . context . workspaceState . get < string [ ] > (
184- RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY
185- ) || [ ]
186- recents = [ selected . id , ...recents . filter ( ( id ) => id != selected . id ) ]
187- params . context . workspaceState . update (
188- RECENTLY_USED_CODE_AT_CURSOR_CONFIG_IDS_STATE_KEY ,
189- recents
190- )
191-
192- if ( params . panel_provider ) {
193- params . panel_provider . send_message ( {
194- command : 'SELECTED_API_CONFIGURATION_CHANGED' ,
195- prompt_type : 'code-at-cursor' ,
196- id : selected . id
197- } )
198- }
199-
200- const model_provider =
201- await params . model_providers_manager . get_model_provider (
202- selected . api_configuration . model_provider_name
203- )
204- if ( ! model_provider ) {
205- vscode . window . showErrorMessage (
206- dictionary . error_message . API_PROVIDER_NOT_FOUND
207- )
208- resolve ( undefined )
209- return
210- }
211-
212- resolve ( {
213- model_provider,
214- api_configuration : selected . api_configuration
215- } )
216- } )
80+ params . context . workspaceState . update (
81+ LAST_USED_CODE_AT_CURSOR_CONFIG_ID_STATE_KEY ,
82+ id
83+ )
21784
218- quick_pick . onDidHide ( ( ) => {
219- quick_pick . dispose ( )
220- resolve ( undefined )
85+ if ( params . panel_provider ) {
86+ params . panel_provider . send_message ( {
87+ command : 'SELECTED_API_CONFIGURATION_CHANGED' ,
88+ prompt_type : 'code-at-cursor' ,
89+ id : id
22190 } )
91+ }
22292
223- quick_pick . show ( )
224- } )
93+ selected_api_configuration = api_configuration
22594 }
22695
22796 const model_provider =
0 commit comments