@@ -481,6 +481,7 @@ export async function resolveProviderAuths(params: {
481481 config ?: OpenClawConfig ;
482482 env ?: NodeJS . ProcessEnv ;
483483 skipPluginAuthWithoutCredentialSource ?: boolean ;
484+ onError ?: ( provider : UsageProviderId , error : unknown ) => void ;
484485} ) : Promise < ProviderAuth [ ] > {
485486 if ( params . auth ) {
486487 return params . auth ;
@@ -501,80 +502,87 @@ export async function resolveProviderAuths(params: {
501502 const auths : ProviderAuth [ ] = [ ] ;
502503
503504 for ( const provider of params . providers ) {
504- if ( ! params . skipPluginAuthWithoutCredentialSource ) {
505- const pluginAuth = await resolveProviderUsageAuthViaPlugin ( {
506- state : authProfileSourceState ,
507- provider,
508- } ) ;
509- if ( pluginAuth . auth ) {
510- auths . push ( pluginAuth . auth ) ;
511- continue ;
512- }
513- if ( pluginAuth . handled ) {
505+ try {
506+ if ( ! params . skipPluginAuthWithoutCredentialSource ) {
507+ const pluginAuth = await resolveProviderUsageAuthViaPlugin ( {
508+ state : authProfileSourceState ,
509+ provider,
510+ } ) ;
511+ if ( pluginAuth . auth ) {
512+ auths . push ( pluginAuth . auth ) ;
513+ continue ;
514+ }
515+ if ( pluginAuth . handled ) {
516+ continue ;
517+ }
518+ const fallbackAuth = await resolveProviderUsageAuthFallback ( {
519+ state : authProfileSourceState ,
520+ provider,
521+ } ) ;
522+ if ( fallbackAuth ) {
523+ auths . push ( fallbackAuth ) ;
524+ }
514525 continue ;
515526 }
516- const fallbackAuth = await resolveProviderUsageAuthFallback ( {
517- state : authProfileSourceState ,
527+
528+ const directCredentialState = { ...stateBase , allowAuthProfileStore : false } ;
529+ const credentialProviderIds = resolveUsageCredentialProviderIds ( {
530+ state : directCredentialState ,
518531 provider,
519532 } ) ;
520- if ( fallbackAuth ) {
521- auths . push ( fallbackAuth ) ;
522- }
523- continue ;
524- }
525-
526- const directCredentialState = { ...stateBase , allowAuthProfileStore : false } ;
527- const credentialProviderIds = resolveUsageCredentialProviderIds ( {
528- state : directCredentialState ,
529- provider,
530- } ) ;
531- const hasDirectCredentialSource =
532- Boolean (
533- resolveProviderApiKeyFromConfig ( {
533+ const hasDirectCredentialSource =
534+ Boolean (
535+ resolveProviderApiKeyFromConfig ( {
536+ state : directCredentialState ,
537+ providerIds : credentialProviderIds ,
538+ } ) ,
539+ ) ||
540+ hasProviderAuthEnvCredentialSource ( {
534541 state : directCredentialState ,
535542 providerIds : credentialProviderIds ,
536- } ) ,
537- ) ||
538- hasProviderAuthEnvCredentialSource ( {
539- state : directCredentialState ,
540- providerIds : credentialProviderIds ,
541- } ) ||
542- hasProviderUsageAuthEnvCredentialSource ( {
543- state : directCredentialState ,
544- providerIds : credentialProviderIds ,
545- } ) ;
546- const allowAuthProfileStore =
547- hasDirectCredentialSource ||
548- ( hasAuthProfileStoreSource &&
549- hasAuthProfileCredentialSource ( {
550- state : authProfileSourceState ,
543+ } ) ||
544+ hasProviderUsageAuthEnvCredentialSource ( {
545+ state : directCredentialState ,
551546 providerIds : credentialProviderIds ,
552- } ) ) ;
553- const state : UsageAuthState = {
554- ...stateBase ,
555- allowAuthProfileStore,
556- } ;
557- const hasPluginCredentialSource = hasDirectCredentialSource || allowAuthProfileStore ;
547+ } ) ;
548+ const allowAuthProfileStore =
549+ hasDirectCredentialSource ||
550+ ( hasAuthProfileStoreSource &&
551+ hasAuthProfileCredentialSource ( {
552+ state : authProfileSourceState ,
553+ providerIds : credentialProviderIds ,
554+ } ) ) ;
555+ const state : UsageAuthState = {
556+ ...stateBase ,
557+ allowAuthProfileStore,
558+ } ;
559+ const hasPluginCredentialSource = hasDirectCredentialSource || allowAuthProfileStore ;
558560
559- if ( hasPluginCredentialSource ) {
560- const pluginAuth = await resolveProviderUsageAuthViaPlugin ( {
561+ if ( hasPluginCredentialSource ) {
562+ const pluginAuth = await resolveProviderUsageAuthViaPlugin ( {
563+ state,
564+ provider,
565+ } ) ;
566+ if ( pluginAuth . auth ) {
567+ auths . push ( pluginAuth . auth ) ;
568+ continue ;
569+ }
570+ if ( pluginAuth . handled ) {
571+ continue ;
572+ }
573+ }
574+ const fallbackAuth = await resolveProviderUsageAuthFallback ( {
561575 state,
562576 provider,
563577 } ) ;
564- if ( pluginAuth . auth ) {
565- auths . push ( pluginAuth . auth ) ;
566- continue ;
578+ if ( fallbackAuth ) {
579+ auths . push ( fallbackAuth ) ;
567580 }
568- if ( pluginAuth . handled ) {
569- continue ;
581+ } catch ( error ) {
582+ if ( ! params . onError ) {
583+ throw error ;
570584 }
571- }
572- const fallbackAuth = await resolveProviderUsageAuthFallback ( {
573- state,
574- provider,
575- } ) ;
576- if ( fallbackAuth ) {
577- auths . push ( fallbackAuth ) ;
585+ params . onError ( provider , error ) ;
578586 }
579587 }
580588
0 commit comments