@@ -5,35 +5,51 @@ import {
55 get_api_configuration_id
66} from '@/services/model-providers-manager'
77import { ApiConfiguration } from '@/views/panel/types/messages'
8+ import { ToolType } from '@/views/settings/types/tools'
89
910export const update_api_configuration = async ( params : {
1011 context : vscode . ExtensionContext
1112 updating_api_configuration : ApiConfiguration
1213 updated_api_configuration : ApiConfiguration
1314 origin ?: 'cancel' | 'save'
15+ is_new ?: boolean
16+ insertion_index ?: number
17+ tool_type ?: ToolType
1418} ) : Promise < { success : boolean ; has_changes : boolean ; new_id ?: string } > => {
1519 const providers_manager = new ModelProvidersManager ( params . context )
20+
21+ if ( params . is_new && params . origin === 'cancel' ) {
22+ return { success : true , has_changes : false }
23+ }
24+
1625 const api_configurations = await providers_manager . get_api_configurations ( )
1726
18- const api_configuration_index = api_configurations . findIndex (
19- ( p ) => get_api_configuration_id ( p ) == params . updating_api_configuration . id
20- )
27+ let api_configuration_index = - 1
28+ if ( ! params . is_new ) {
29+ api_configuration_index = api_configurations . findIndex (
30+ ( p ) => get_api_configuration_id ( p ) == params . updating_api_configuration . id
31+ )
2132
22- if ( api_configuration_index == - 1 && params . origin != 'cancel' ) {
23- vscode . window . showErrorMessage (
24- dictionary . error_message . COULD_NOT_UPDATE_ITEM_NOT_FOUND (
25- 'API configuration' ,
26- params . updating_api_configuration . id
33+ if ( api_configuration_index == - 1 && params . origin != 'cancel' ) {
34+ vscode . window . showErrorMessage (
35+ dictionary . error_message . COULD_NOT_UPDATE_ITEM_NOT_FOUND (
36+ 'API configuration' ,
37+ params . updating_api_configuration . id
38+ )
2739 )
28- )
29- return { success : false , has_changes : false }
40+ return { success : false , has_changes : false }
41+ }
3042 }
3143
3244 const has_changes =
3345 params . updating_api_configuration . id !==
34- get_api_configuration_id ( params . updated_api_configuration )
46+ get_api_configuration_id ( params . updated_api_configuration ) ||
47+ params . updating_api_configuration . system_instructions_override !==
48+ params . updated_api_configuration . system_instructions_override ||
49+ params . updating_api_configuration . is_pinned !==
50+ params . updated_api_configuration . is_pinned
3551
36- if ( ! has_changes ) {
52+ if ( ! has_changes && ! params . is_new ) {
3753 return { success : true , has_changes : false }
3854 }
3955
@@ -59,36 +75,76 @@ export const update_api_configuration = async (params: {
5975 }
6076
6177 const new_config = {
62- ...api_configurations [ api_configuration_index ] ,
78+ ...( ! params . is_new && api_configuration_index !== - 1
79+ ? api_configurations [ api_configuration_index ]
80+ : { } ) ,
6381 model_provider_name : params . updated_api_configuration . model_provider_name ,
6482 model : params . updated_api_configuration . model ,
6583 temperature : params . updated_api_configuration . temperature ,
6684 reasoning_effort : params . updated_api_configuration . reasoning_effort ,
6785 system_instructions_override :
68- params . updated_api_configuration . system_instructions_override
86+ params . updated_api_configuration . system_instructions_override ,
87+ is_pinned : params . updated_api_configuration . is_pinned
6988 }
7089
71- const new_id = get_api_configuration_id ( new_config )
90+ const new_id = get_api_configuration_id ( new_config as any )
91+
92+ const is_duplicate = params . is_new
93+ ? api_configurations . some ( ( c ) => get_api_configuration_id ( c ) === new_id )
94+ : new_id != params . updating_api_configuration . id &&
95+ api_configurations . some ( ( c ) => get_api_configuration_id ( c ) === new_id )
7296
73- // check if id already exists AND it's not the one we're editing
74- if (
75- new_id !== params . updating_api_configuration . id &&
76- api_configurations . some ( ( c ) => get_api_configuration_id ( c ) === new_id )
77- ) {
78- vscode . window . showErrorMessage (
79- dictionary . error_message . CONFIGURATION_ALREADY_EXISTS
97+ if ( is_duplicate ) {
98+ const discard_button = 'Discard'
99+ const result = await vscode . window . showWarningMessage (
100+ dictionary . error_message . CONFIGURATION_ALREADY_EXISTS ,
101+ { modal : true } ,
102+ discard_button
80103 )
104+
105+ if ( result == discard_button ) {
106+ return { success : true , has_changes : false }
107+ }
108+
81109 return { success : false , has_changes : true }
82110 }
83111
84- if ( api_configuration_index !== - 1 ) {
85- api_configurations [ api_configuration_index ] = new_config
86- } else {
87- api_configurations . push ( new_config )
112+ if ( params . is_new ) {
113+ if ( params . insertion_index !== undefined ) {
114+ api_configurations . splice ( params . insertion_index , 0 , new_config as any )
115+ } else {
116+ api_configurations . push ( new_config as any )
117+ }
118+ } else if ( api_configuration_index !== - 1 ) {
119+ api_configurations [ api_configuration_index ] = new_config as any
88120 }
89121
90122 await providers_manager . save_api_configurations ( api_configurations )
91123
124+ if ( params . is_new && params . tool_type ) {
125+ if ( params . tool_type == 'code-at-cursor' ) {
126+ await providers_manager . set_default_code_completions_api_configuration (
127+ new_config as any
128+ )
129+ } else if ( params . tool_type == 'commit-messages' ) {
130+ await providers_manager . set_default_commit_messages_api_configuration (
131+ new_config as any
132+ )
133+ } else if ( params . tool_type == 'intelligent-update' ) {
134+ await providers_manager . set_default_intelligent_update_api_configuration (
135+ new_config as any
136+ )
137+ } else if ( params . tool_type == 'find-relevant-files' ) {
138+ await providers_manager . set_default_find_relevant_files_api_configuration (
139+ new_config as any
140+ )
141+ } else if ( params . tool_type == 'voice-input' ) {
142+ await providers_manager . set_default_voice_input_api_configuration (
143+ new_config as any
144+ )
145+ }
146+ }
147+
92148 return {
93149 success : true ,
94150 has_changes : true ,
0 commit comments