@@ -136,6 +136,7 @@ export class ProviderSettingsManager {
136136 return next
137137 }
138138
139+ async kiloRepairExistingIds ( ) { }
139140 // kilocode_change: private & renamed:
140141 async init_runMigrations ( ) {
141142 try {
@@ -174,79 +175,74 @@ export class ProviderSettingsManager {
174175 }
175176 }
176177
177- // Repair duplicated IDs (keep the first occurrence based on apiConfigs insertion order).
178- // This must be idempotent and must follow the same canonical ordering the backend uses
179- // when building the profile list (Object.entries on apiConfigs).
180- {
181- const existingIds = new Set (
182- Object . values ( providerProfiles . apiConfigs )
183- . map ( ( c ) => c . id )
184- . filter ( ( id ) : id is string => Boolean ( id ) ) ,
185- )
186-
187- const seenIds = new Set < string > ( )
188- let updatedCloudProfileIds : Set < string > | undefined
189- const duplicateIdRepairReport : Record < string , string [ ] > = { }
178+ // kilocode_change start: Repair duplicated IDs (keep the first occurrence based on apiConfigs insertion order).
179+ const existingIds = new Set (
180+ Object . values ( providerProfiles . apiConfigs )
181+ . map ( ( c ) => c . id )
182+ . filter ( ( id ) : id is string => Boolean ( id ) ) ,
183+ )
190184
191- for ( const [ name , apiConfig ] of Object . entries ( providerProfiles . apiConfigs ) ) {
192- const id = apiConfig . id
193- if ( ! id ) continue
185+ const seenIds = new Set < string > ( )
186+ let updatedCloudProfileIds : Set < string > | undefined
187+ const duplicateIdRepairReport : Record < string , string [ ] > = { }
194188
195- // first profile keeps its id
196- if ( ! seenIds . has ( id ) ) {
197- seenIds . add ( id )
198- continue
199- }
189+ for ( const [ name , apiConfig ] of Object . entries ( providerProfiles . apiConfigs ) ) {
190+ const id = apiConfig . id
191+ if ( ! id ) continue
200192
201- const newId = this . generateUniqueId ( existingIds )
202- apiConfig . id = newId
203- isDirty = true
193+ // first profile keeps its id
194+ if ( ! seenIds . has ( id ) ) {
195+ seenIds . add ( id )
196+ continue
197+ }
204198
205- duplicateIdRepairReport [ id ] ??= [ ]
206- duplicateIdRepairReport [ id ] . push ( newId )
199+ const newId = this . generateUniqueId ( existingIds )
200+ apiConfig . id = newId
201+ isDirty = true
207202
208- // If this was considered cloud-managed before (by virtue of its old id being listed),
209- // keep it cloud-managed by adding the new id as well.
210- if ( providerProfiles . cloudProfileIds ?. includes ( id ) ) {
211- updatedCloudProfileIds ??= new Set ( providerProfiles . cloudProfileIds )
212- updatedCloudProfileIds . add ( newId )
213- }
203+ duplicateIdRepairReport [ id ] ??= [ ]
204+ duplicateIdRepairReport [ id ] . push ( newId )
214205
215- console . warn (
216- `[ProviderSettingsManager] Deduped duplicate provider profile id '${ id } ' for '${ name } ', new id '${ newId } '` ,
217- )
206+ // If this was considered cloud-managed before (by virtue of its old id being listed),
207+ // keep it cloud-managed by adding the new id as well.
208+ if ( providerProfiles . cloudProfileIds ?. includes ( id ) ) {
209+ updatedCloudProfileIds ??= new Set ( providerProfiles . cloudProfileIds )
210+ updatedCloudProfileIds . add ( newId )
218211 }
219212
220- if ( updatedCloudProfileIds ) {
221- providerProfiles . cloudProfileIds = Array . from ( updatedCloudProfileIds )
222- isDirty = true
223- }
213+ console . warn (
214+ `[ProviderSettingsManager] Deduped duplicate provider profile id ' ${ id } ' for ' ${ name } ', new id ' ${ newId } '` ,
215+ )
216+ }
224217
225- if ( Object . keys ( duplicateIdRepairReport ) . length > 0 ) {
226- this . pendingDuplicateIdRepairReport = duplicateIdRepairReport
227- }
218+ if ( updatedCloudProfileIds ) {
219+ providerProfiles . cloudProfileIds = Array . from ( updatedCloudProfileIds )
220+ isDirty = true
221+ }
222+
223+ if ( Object . keys ( duplicateIdRepairReport ) . length > 0 ) {
224+ this . pendingDuplicateIdRepairReport = duplicateIdRepairReport
228225 }
229226
230227 // Keep secrets-side references consistent (post-dedupe).
231- {
232- const validProfileIds = new Set (
233- Object . values ( providerProfiles . apiConfigs )
234- . map ( ( c ) => c . id )
235- . filter ( ( id ) : id is string => Boolean ( id ) ) ,
236- )
228+ const validProfileIds = new Set (
229+ Object . values ( providerProfiles . apiConfigs )
230+ . map ( ( c ) => c . id )
231+ . filter ( ( id ) : id is string => Boolean ( id ) ) ,
232+ )
237233
238- const firstProfileId = Object . values ( providerProfiles . apiConfigs ) [ 0 ] ?. id
234+ const firstProfileId = Object . values ( providerProfiles . apiConfigs ) [ 0 ] ?. id
239235
240- // Fix modeApiConfigs stored inside providerProfiles (secrets) if they point to a missing id.
241- if ( providerProfiles . modeApiConfigs && firstProfileId ) {
242- for ( const [ mode , configId ] of Object . entries ( providerProfiles . modeApiConfigs ) ) {
243- if ( ! validProfileIds . has ( configId ) ) {
244- providerProfiles . modeApiConfigs [ mode ] = firstProfileId
245- isDirty = true
246- }
236+ // Fix modeApiConfigs stored inside providerProfiles (secrets) if they point to a missing id.
237+ if ( providerProfiles . modeApiConfigs && firstProfileId ) {
238+ for ( const [ mode , configId ] of Object . entries ( providerProfiles . modeApiConfigs ) ) {
239+ if ( ! validProfileIds . has ( configId ) ) {
240+ providerProfiles . modeApiConfigs [ mode ] = firstProfileId
241+ isDirty = true
247242 }
248243 }
249244 }
245+ // kilocode_Change end
250246
251247 // Ensure migrations field exists
252248 if ( ! providerProfiles . migrations ) {
0 commit comments