@@ -14,13 +14,11 @@ import type { ExternalCliAuthDiscovery } from "./auth-profiles/external-cli-disc
1414import {
1515 ensureAuthProfileStore ,
1616 ensureAuthProfileStoreWithoutExternalProfiles ,
17- loadAuthProfileStoreWithoutExternalProfiles ,
18- loadAuthProfileStoreForRuntime ,
19- loadAuthProfileStoreForSecretsRuntime ,
2017} from "./auth-profiles/store.js" ;
2118
2219/** Options for discovering credentials without prompting for secret material. */
2320export type DiscoverAuthStorageOptions = {
21+ ambientCredentials ?: Readonly < AgentCredentialMap > ;
2422 externalCli ?: ExternalCliAuthDiscovery ;
2523 inheritedAuthDir ?: string ;
2624 readOnly ?: boolean ;
@@ -29,73 +27,50 @@ export type DiscoverAuthStorageOptions = {
2927 syntheticAuthProviderRefs ?: Iterable < string > ;
3028} & AgentDiscoveryAuthLookupOptions ;
3129
32- /** Resolves agent credentials from auth profiles, env, and synthetic auth hooks. */
33- export function resolveAgentCredentialsForDiscovery (
34- agentDir : string ,
35- options ?: DiscoverAuthStorageOptions ,
30+ type AmbientAgentCredentialOptions = AgentDiscoveryAuthLookupOptions & {
31+ resolveSyntheticAuth ?: ( provider : string ) => { apiKey ?: string } | undefined ;
32+ syntheticAuthProviderRefs ?: Iterable < string > ;
33+ } ;
34+
35+ /** Resolves workspace/config/env-stable credentials independently of agent-local profiles. */
36+ export function resolveAmbientAgentCredentialsForDiscovery (
37+ options : AmbientAgentCredentialOptions = { } ,
3638) : AgentCredentialMap {
37- const storeOptions = {
38- allowKeychainPrompt : false ,
39- ...( options ?. config ? { config : options . config } : { } ) ,
40- ...( options ?. externalCli ? { externalCli : options . externalCli } : { } ) ,
41- ...( options ?. inheritedAuthDir ? { inheritedAuthDir : options . inheritedAuthDir } : { } ) ,
42- } ;
43- const store =
44- options ?. skipExternalAuthProfiles === true
45- ? options . readOnly === true
46- ? loadAuthProfileStoreWithoutExternalProfiles (
47- agentDir ,
48- options . inheritedAuthDir ? { inheritedAuthDir : options . inheritedAuthDir } : undefined ,
49- )
50- : ensureAuthProfileStoreWithoutExternalProfiles ( agentDir , {
51- allowKeychainPrompt : false ,
52- ...( options ?. inheritedAuthDir ? { inheritedAuthDir : options . inheritedAuthDir } : { } ) ,
53- } )
54- : options ?. readOnly === true
55- ? options . externalCli || options . config || options . inheritedAuthDir
56- ? loadAuthProfileStoreForRuntime ( agentDir , { readOnly : true , ...storeOptions } )
57- : loadAuthProfileStoreForSecretsRuntime ( agentDir )
58- : ensureAuthProfileStore ( agentDir , storeOptions ) ;
59- const credentials = addEnvBackedAgentCredentials (
60- resolveAgentCredentialMapFromStore ( store , {
61- includeSecretRefPlaceholders : options ?. readOnly === true ,
62- config : options ?. config ,
63- } ) ,
64- {
65- config : options ?. config ,
66- workspaceDir : options ?. workspaceDir ,
67- env : options ?. env ,
68- } ,
69- ) ;
39+ const credentials = addEnvBackedAgentCredentials ( { } , options ) ;
7040 const syntheticAuthProviderRefs =
71- options ?. syntheticAuthProviderRefs ?? resolveRuntimeSyntheticAuthProviderRefs ( ) ;
41+ options . syntheticAuthProviderRefs ?? resolveRuntimeSyntheticAuthProviderRefs ( ) ;
42+ const resolveSyntheticAuth =
43+ options . resolveSyntheticAuth ??
44+ ( ( provider : string ) =>
45+ resolveProviderSyntheticAuthWithPlugin ( {
46+ provider,
47+ config : options . config ,
48+ workspaceDir : options . workspaceDir ,
49+ env : options . env ,
50+ context : {
51+ config : options . config ,
52+ provider,
53+ providerConfig : options . config ?. models ?. providers ?. [ provider ] ,
54+ } ,
55+ } ) ) ;
7256 for ( const provider of syntheticAuthProviderRefs ) {
7357 if ( credentials [ provider ] ) {
7458 continue ;
7559 }
7660 if (
7761 ! isAmbientCredentialAllowedByProviderAuthPin ( {
78- config : options ? .config ,
62+ config : options . config ,
7963 authAliasLookupParams : {
80- ...( options ? .env ? { env : options . env } : { } ) ,
81- ...( options ? .workspaceDir ? { workspaceDir : options . workspaceDir } : { } ) ,
64+ ...( options . env ? { env : options . env } : { } ) ,
65+ ...( options . workspaceDir ? { workspaceDir : options . workspaceDir } : { } ) ,
8266 } ,
8367 provider,
8468 type : "api_key" ,
8569 } )
8670 ) {
8771 continue ;
8872 }
89- // Synthetic auth is a plugin/runtime fallback. Only fill empty providers so
90- // persisted profiles and env-backed credentials remain authoritative.
91- const resolved = resolveProviderSyntheticAuthWithPlugin ( {
92- provider,
93- context : {
94- config : undefined ,
95- provider,
96- providerConfig : undefined ,
97- } ,
98- } ) ;
73+ const resolved = resolveSyntheticAuth ( provider ) ;
9974 const apiKey = resolved ?. apiKey ?. trim ( ) ;
10075 if ( ! apiKey ) {
10176 continue ;
@@ -107,3 +82,47 @@ export function resolveAgentCredentialsForDiscovery(
10782 }
10883 return credentials ;
10984}
85+
86+ /** Resolves agent credentials from auth profiles, env, and synthetic auth hooks. */
87+ export function resolveAgentCredentialsForDiscovery (
88+ agentDir : string ,
89+ options ?: DiscoverAuthStorageOptions ,
90+ ) : AgentCredentialMap {
91+ const storeOptions = {
92+ allowKeychainPrompt : false ,
93+ ...( options ?. config ? { config : options . config } : { } ) ,
94+ ...( options ?. externalCli ? { externalCli : options . externalCli } : { } ) ,
95+ ...( options ?. inheritedAuthDir ? { inheritedAuthDir : options . inheritedAuthDir } : { } ) ,
96+ } ;
97+ const store =
98+ options ?. skipExternalAuthProfiles === true
99+ ? ensureAuthProfileStoreWithoutExternalProfiles ( agentDir , {
100+ allowKeychainPrompt : false ,
101+ ...( options ?. inheritedAuthDir ? { inheritedAuthDir : options . inheritedAuthDir } : { } ) ,
102+ ...( options ?. readOnly === true ? { readOnly : true } : { } ) ,
103+ } )
104+ : ensureAuthProfileStore ( agentDir , {
105+ ...storeOptions ,
106+ ...( options ?. readOnly === true ? { readOnly : true } : { } ) ,
107+ } ) ;
108+ const credentials = resolveAgentCredentialMapFromStore ( store , {
109+ includeSecretRefPlaceholders : options ?. readOnly === true ,
110+ config : options ?. config ,
111+ } ) ;
112+ const ambientCredentials =
113+ options ?. ambientCredentials ??
114+ resolveAmbientAgentCredentialsForDiscovery ( {
115+ config : options ?. config ,
116+ workspaceDir : options ?. workspaceDir ,
117+ env : options ?. env ,
118+ syntheticAuthProviderRefs : options ?. syntheticAuthProviderRefs ,
119+ } ) ;
120+ for ( const [ provider , credential ] of Object . entries ( ambientCredentials ) ) {
121+ if ( credentials [ provider ] ) {
122+ continue ;
123+ }
124+ // Ambient auth is a lifecycle-owned fallback. Agent-local profiles remain authoritative.
125+ credentials [ provider ] = credential ;
126+ }
127+ return credentials ;
128+ }
0 commit comments