@@ -10,6 +10,7 @@ import {
1010 filterModelsByPrefix ,
1111 getOpenPlatformById ,
1212 isOpenPlatformId ,
13+ removeCustomRegistryProvider ,
1314 resolveKimiCodeRuntimeAuth ,
1415 type CustomRegistrySource ,
1516 type ManagedKimiConfigShape ,
@@ -42,7 +43,7 @@ export interface RefreshResult {
4243function readCustomRegistrySource ( provider : ProviderConfig ) : CustomRegistrySource | undefined {
4344 const source = provider . source ;
4445 if ( typeof source !== 'object' || source === null ) return undefined ;
45- const candidate = source as Record < string , unknown > ;
46+ const candidate = source ;
4647 if ( candidate [ 'kind' ] !== 'apiJson' ) return undefined ;
4748 const url = candidate [ 'url' ] ;
4849 const apiKey = candidate [ 'apiKey' ] ;
@@ -51,6 +52,36 @@ function readCustomRegistrySource(provider: ProviderConfig): CustomRegistrySourc
5152 return { kind : 'apiJson' , url, apiKey } ;
5253}
5354
55+ function customRegistrySourceKey ( source : CustomRegistrySource ) : string {
56+ return JSON . stringify ( [ source . url ] ) ;
57+ }
58+
59+ function customRegistrySourceCredentialKey ( source : CustomRegistrySource ) : string {
60+ return JSON . stringify ( [ source . url , source . apiKey ] ) ;
61+ }
62+
63+ async function fetchCustomRegistryFromSources (
64+ sources : readonly CustomRegistrySource [ ] ,
65+ ) : Promise < {
66+ readonly entries : Awaited < ReturnType < typeof fetchCustomRegistry > > ;
67+ readonly source : CustomRegistrySource ;
68+ } > {
69+ let lastError : unknown ;
70+ for ( const source of sources ) {
71+ try {
72+ return {
73+ entries : await fetchCustomRegistry ( source ) ,
74+ source,
75+ } ;
76+ } catch ( error ) {
77+ lastError = error ;
78+ }
79+ }
80+ if ( lastError instanceof Error ) throw lastError ;
81+ if ( typeof lastError === 'string' ) throw new Error ( lastError ) ;
82+ throw new Error ( 'No custom registry sources configured.' ) ;
83+ }
84+
5485function asManaged ( config : KimiConfig ) : ManagedKimiConfigShape {
5586 return config as unknown as ManagedKimiConfigShape ;
5687}
@@ -143,6 +174,14 @@ function providerModelsEqual(
143174 ) ;
144175}
145176
177+ function providerConfigSnapshot ( config : KimiConfig , providerId : string ) : string {
178+ return JSON . stringify ( config . providers [ providerId ] ?? null ) ;
179+ }
180+
181+ function providerConfigEqual ( config : KimiConfig , nextConfig : KimiConfig , providerId : string ) : boolean {
182+ return providerConfigSnapshot ( config , providerId ) === providerConfigSnapshot ( nextConfig , providerId ) ;
183+ }
184+
146185function providerRefreshAliasKeys (
147186 config : KimiConfig ,
148187 nextConfig : KimiConfig ,
@@ -199,6 +238,15 @@ function clampDanglingDefault(config: KimiConfig): void {
199238 }
200239}
201240
241+ function clearDefaultThinkingWhenDefaultRemoved (
242+ config : KimiConfig ,
243+ previousDefaultModel : string | undefined ,
244+ ) : void {
245+ if ( previousDefaultModel !== undefined && config . defaultModel === undefined ) {
246+ config . defaultThinking = undefined ;
247+ }
248+ }
249+
202250function pickDefaultModel ( config : KimiConfig , providerId : string , models : Array < { id : string } > ) : string {
203251 const firstModel = models [ 0 ] ;
204252 if ( firstModel === undefined ) return '' ;
@@ -263,6 +311,7 @@ export async function refreshAllProviderModels(host: RefreshProviderHost): Promi
263311 ) ;
264312 restoreDefaultSelection ( next , config . defaultModel , config . defaultThinking ) ;
265313 clampDanglingDefault ( next ) ;
314+ clearDefaultThinkingWhenDefaultRemoved ( next , config . defaultModel ) ;
266315
267316 if ( providerModelsEqual ( config , next , KIMI_CODE_PROVIDER_NAME , refreshedAliasKeys ) ) {
268317 unchanged . push ( KIMI_CODE_PROVIDER_NAME ) ;
@@ -332,6 +381,7 @@ export async function refreshAllProviderModels(host: RefreshProviderHost): Promi
332381 restoreProviderAliases ( next , preserveUserProviderAliases ( config , providerId , refreshedAliasKeys ) ) ;
333382 restoreDefaultSelection ( next , config . defaultModel , config . defaultThinking ) ;
334383 clampDanglingDefault ( next ) ;
384+ clearDefaultThinkingWhenDefaultRemoved ( next , config . defaultModel ) ;
335385
336386 if ( providerModelsEqual ( config , next , providerId , refreshedAliasKeys ) ) {
337387 unchanged . push ( providerId ) ;
@@ -363,26 +413,42 @@ export async function refreshAllProviderModels(host: RefreshProviderHost): Promi
363413 }
364414
365415 // -------------------------------------------------------------------------
366- // 3. Custom Registry providers (grouped by {url, apiKey} )
416+ // 3. Custom Registry providers (grouped by URL, with API-key candidates )
367417 // -------------------------------------------------------------------------
368- const customSources = new Map < string , { source : CustomRegistrySource ; providerIds : string [ ] } > ( ) ;
418+ const customSources = new Map <
419+ string ,
420+ {
421+ readonly sources : CustomRegistrySource [ ] ;
422+ readonly sourceKeys : Set < string > ;
423+ readonly providerIds : string [ ] ;
424+ }
425+ > ( ) ;
369426 for ( const [ providerId , providerConfig ] of Object . entries ( config . providers ) ) {
370427 if ( providerId === KIMI_CODE_PROVIDER_NAME ) continue ;
371428 if ( isOpenPlatformId ( providerId ) ) continue ;
372429 const source = readCustomRegistrySource ( providerConfig ) ;
373430 if ( source === undefined ) continue ;
374- const key = `${ source . url } ${ source . apiKey } ` ;
431+ const key = customRegistrySourceKey ( source ) ;
432+ const sourceKey = customRegistrySourceCredentialKey ( source ) ;
375433 const entry = customSources . get ( key ) ;
376434 if ( entry !== undefined ) {
435+ if ( ! entry . sourceKeys . has ( sourceKey ) ) {
436+ entry . sources . push ( source ) ;
437+ entry . sourceKeys . add ( sourceKey ) ;
438+ }
377439 entry . providerIds . push ( providerId ) ;
378440 } else {
379- customSources . set ( key , { source, providerIds : [ providerId ] } ) ;
441+ customSources . set ( key , {
442+ sources : [ source ] ,
443+ sourceKeys : new Set ( [ sourceKey ] ) ,
444+ providerIds : [ providerId ] ,
445+ } ) ;
380446 }
381447 }
382448
383- for ( const { source , providerIds } of customSources . values ( ) ) {
449+ for ( const { sources , providerIds } of customSources . values ( ) ) {
384450 try {
385- const entries = await fetchCustomRegistry ( source ) ;
451+ const { entries, source } = await fetchCustomRegistryFromSources ( sources ) ;
386452 // Build the whole batch on one clone so that several changed providers
387453 // from the same source do not overwrite each other's aliases, and so the
388454 // config we compare is exactly the config we persist.
@@ -393,17 +459,47 @@ export async function refreshAllProviderModels(host: RefreshProviderHost): Promi
393459 readonly added : number ;
394460 readonly removed : number ;
395461 } > = [ ] ;
462+ const providersToRemoveBeforeSet = new Set < string > ( ) ;
463+ let hasUnreportedConfigChange = false ;
464+ const remoteEntries = Object . values ( entries ) ;
465+ const remoteEntriesByProviderId = new Map (
466+ remoteEntries . map ( ( entry ) => [ entry . id , entry ] ) ,
467+ ) ;
468+ const providerIdsToSync = new Set ( providerIds ) ;
469+ for ( const entry of remoteEntries ) providerIdsToSync . add ( entry . id ) ;
470+
471+ for ( const providerId of providerIdsToSync ) {
472+ const entry = remoteEntriesByProviderId . get ( providerId ) ;
473+ if ( entry === undefined ) {
474+ const oldIds = collectModelIdsForAliases ( config , providerAliasKeys ( config , providerId ) ) ;
475+ removeCustomRegistryProvider ( asManaged ( next ) , providerId ) ;
476+ changedProviders . push ( {
477+ providerId,
478+ providerName : providerId ,
479+ added : 0 ,
480+ removed : oldIds . size ,
481+ } ) ;
482+ providersToRemoveBeforeSet . add ( providerId ) ;
483+ continue ;
484+ }
396485
397- for ( const providerId of providerIds ) {
398- const entry = entries [ providerId ] ;
399- if ( entry === undefined ) continue ;
400-
486+ const existed = config . providers [ providerId ] !== undefined ;
401487 applyCustomRegistryProvider ( asManaged ( next ) , entry , source ) ;
402488 const refreshedAliasKeys = providerRefreshAliasKeys ( config , next , providerId , `${ providerId } /` ) ;
403- restoreProviderAliases ( next , preserveUserProviderAliases ( config , providerId , refreshedAliasKeys ) ) ;
489+ if ( existed ) {
490+ restoreProviderAliases ( next , preserveUserProviderAliases ( config , providerId , refreshedAliasKeys ) ) ;
491+ }
404492
405- if ( providerModelsEqual ( config , next , providerId , refreshedAliasKeys ) ) {
493+ if (
494+ existed &&
495+ providerModelsEqual ( config , next , providerId , refreshedAliasKeys ) &&
496+ providerConfigEqual ( config , next , providerId )
497+ ) {
498+ unchanged . push ( providerId ) ;
499+ } else if ( existed && providerModelsEqual ( config , next , providerId , refreshedAliasKeys ) ) {
406500 unchanged . push ( providerId ) ;
501+ providersToRemoveBeforeSet . add ( providerId ) ;
502+ hasUnreportedConfigChange = true ;
407503 } else {
408504 const { added, removed } = computeChanges (
409505 collectModelIdsForAliases ( config , refreshedAliasKeys ) ,
@@ -415,13 +511,15 @@ export async function refreshAllProviderModels(host: RefreshProviderHost): Promi
415511 added,
416512 removed,
417513 } ) ;
514+ if ( existed ) providersToRemoveBeforeSet . add ( providerId ) ;
418515 }
419516 }
420517
421- if ( changedProviders . length > 0 ) {
518+ if ( changedProviders . length > 0 || hasUnreportedConfigChange ) {
422519 restoreDefaultSelection ( next , config . defaultModel , config . defaultThinking ) ;
423520 clampDanglingDefault ( next ) ;
424- for ( const { providerId } of changedProviders ) {
521+ clearDefaultThinkingWhenDefaultRemoved ( next , config . defaultModel ) ;
522+ for ( const providerId of providersToRemoveBeforeSet ) {
425523 await host . removeProvider ( providerId ) ;
426524 }
427525 config = await host . setConfig ( {
@@ -431,7 +529,12 @@ export async function refreshAllProviderModels(host: RefreshProviderHost): Promi
431529 defaultThinking : next . defaultThinking ,
432530 } ) ;
433531 for ( const change of changedProviders ) {
434- changed . push ( change ) ;
532+ changed . push ( {
533+ providerId : change . providerId ,
534+ providerName : change . providerName ,
535+ added : change . added ,
536+ removed : change . removed ,
537+ } ) ;
435538 }
436539 }
437540 } catch ( error ) {
0 commit comments