@@ -10,6 +10,7 @@ import {
1010 log ,
1111} from "./constants.js" ;
1212import { overlayExternalAuthProfiles , shouldPersistExternalAuthProfile } from "./external-auth.js" ;
13+ import type { ExternalCliAuthDiscovery } from "./external-cli-discovery.js" ;
1314import { isSafeToAdoptMainStoreOAuthIdentity } from "./oauth-shared.js" ;
1415import {
1516 ensureAuthStoreFile ,
@@ -38,6 +39,7 @@ import type { AuthProfileStore } from "./types.js";
3839type LoadAuthProfileStoreOptions = {
3940 allowKeychainPrompt ?: boolean ;
4041 config ?: OpenClawConfig ;
42+ externalCli ?: ExternalCliAuthDiscovery ;
4143 readOnly ?: boolean ;
4244 syncExternalCli ?: boolean ;
4345 externalCliProviderIds ?: Iterable < string > ;
@@ -49,6 +51,13 @@ type SaveAuthProfileStoreOptions = {
4951 syncExternalCli ?: boolean ;
5052} ;
5153
54+ type ResolvedExternalCliOverlayOptions = {
55+ allowKeychainPrompt ?: boolean ;
56+ config ?: OpenClawConfig ;
57+ externalCliProviderIds ?: Iterable < string > ;
58+ externalCliProfileIds ?: Iterable < string > ;
59+ } ;
60+
5261const loadedAuthStoreCache = new Map <
5362 string ,
5463 {
@@ -183,6 +192,51 @@ function writeCachedAuthProfileStore(params: {
183192 } ) ;
184193}
185194
195+ function resolveExternalCliOverlayOptions (
196+ options : LoadAuthProfileStoreOptions | undefined ,
197+ ) : ResolvedExternalCliOverlayOptions {
198+ const discovery = options ?. externalCli ;
199+ if ( ! discovery ) {
200+ return {
201+ ...( options ?. allowKeychainPrompt !== undefined
202+ ? { allowKeychainPrompt : options . allowKeychainPrompt }
203+ : { } ) ,
204+ ...( options ?. config ? { config : options . config } : { } ) ,
205+ ...( options ?. externalCliProviderIds
206+ ? { externalCliProviderIds : options . externalCliProviderIds }
207+ : { } ) ,
208+ ...( options ?. externalCliProfileIds
209+ ? { externalCliProfileIds : options . externalCliProfileIds }
210+ : { } ) ,
211+ } ;
212+ }
213+ if ( discovery . mode === "none" ) {
214+ const config = discovery . config ?? options ?. config ;
215+ return {
216+ allowKeychainPrompt : false ,
217+ ...( config ? { config } : { } ) ,
218+ externalCliProviderIds : [ ] ,
219+ externalCliProfileIds : [ ] ,
220+ } ;
221+ }
222+ if ( discovery . mode === "existing" ) {
223+ const allowKeychainPrompt = discovery . allowKeychainPrompt ?? options ?. allowKeychainPrompt ;
224+ const config = discovery . config ?? options ?. config ;
225+ return {
226+ ...( allowKeychainPrompt !== undefined ? { allowKeychainPrompt } : { } ) ,
227+ ...( config ? { config } : { } ) ,
228+ } ;
229+ }
230+ const allowKeychainPrompt = discovery . allowKeychainPrompt ?? options ?. allowKeychainPrompt ;
231+ const config = discovery . config ?? options ?. config ;
232+ return {
233+ ...( allowKeychainPrompt !== undefined ? { allowKeychainPrompt } : { } ) ,
234+ ...( config ? { config } : { } ) ,
235+ ...( discovery . providerIds ? { externalCliProviderIds : discovery . providerIds } : { } ) ,
236+ ...( discovery . profileIds ? { externalCliProfileIds : discovery . profileIds } : { } ) ,
237+ } ;
238+ }
239+
186240function shouldKeepProfileInLocalStore ( params : {
187241 store : AuthProfileStore ;
188242 profileId : string ;
@@ -384,23 +438,18 @@ export function loadAuthProfileStoreForRuntime(
384438 const store = loadAuthProfileStoreForAgent ( agentDir , options ) ;
385439 const authPath = resolveAuthStorePath ( agentDir ) ;
386440 const mainAuthPath = resolveAuthStorePath ( ) ;
441+ const externalCli = resolveExternalCliOverlayOptions ( options ) ;
387442 if ( ! agentDir || authPath === mainAuthPath ) {
388443 return overlayExternalAuthProfiles ( store , {
389444 agentDir,
390- allowKeychainPrompt : options ?. allowKeychainPrompt ,
391- config : options ?. config ,
392- externalCliProviderIds : options ?. externalCliProviderIds ,
393- externalCliProfileIds : options ?. externalCliProfileIds ,
445+ ...externalCli ,
394446 } ) ;
395447 }
396448
397449 const mainStore = loadAuthProfileStoreForAgent ( undefined , options ) ;
398450 return overlayExternalAuthProfiles ( mergeAuthProfileStores ( mainStore , store ) , {
399451 agentDir,
400- allowKeychainPrompt : options ?. allowKeychainPrompt ,
401- config : options ?. config ,
402- externalCliProviderIds : options ?. externalCliProviderIds ,
403- externalCliProfileIds : options ?. externalCliProfileIds ,
452+ ...externalCli ,
404453 } ) ;
405454}
406455
@@ -426,18 +475,17 @@ export function ensureAuthProfileStore(
426475 options ?: {
427476 allowKeychainPrompt ?: boolean ;
428477 config ?: OpenClawConfig ;
478+ externalCli ?: ExternalCliAuthDiscovery ;
429479 externalCliProviderIds ?: Iterable < string > ;
430480 externalCliProfileIds ?: Iterable < string > ;
431481 } ,
432482) : AuthProfileStore {
483+ const externalCli = resolveExternalCliOverlayOptions ( options ) ;
433484 return overlayExternalAuthProfiles (
434485 ensureAuthProfileStoreWithoutExternalProfiles ( agentDir , options ) ,
435486 {
436487 agentDir,
437- allowKeychainPrompt : options ?. allowKeychainPrompt ,
438- config : options ?. config ,
439- externalCliProviderIds : options ?. externalCliProviderIds ,
440- externalCliProfileIds : options ?. externalCliProfileIds ,
488+ ...externalCli ,
441489 } ,
442490 ) ;
443491}
0 commit comments