@@ -98,6 +98,8 @@ export class GeminiAgent {
9898 private sessions : Map < string , Session > = new Map ( ) ;
9999 private clientCapabilities : acp . ClientCapabilities | undefined ;
100100 private apiKey : string | undefined ;
101+ private baseUrl : string | undefined ;
102+ private customHeaders : Record < string , string > | undefined ;
101103
102104 constructor (
103105 private config : Config ,
@@ -131,6 +133,17 @@ export class GeminiAgent {
131133 name : 'Vertex AI' ,
132134 description : 'Use an API key with Vertex AI GenAI API' ,
133135 } ,
136+ {
137+ id : AuthType . GATEWAY ,
138+ name : 'AI API Gateway' ,
139+ description : 'Use a custom AI API Gateway' ,
140+ _meta : {
141+ gateway : {
142+ protocol : 'google' ,
143+ restartRequired : 'false' ,
144+ } ,
145+ } ,
146+ } ,
134147 ] ;
135148
136149 await this . config . initialize ( ) ;
@@ -179,7 +192,38 @@ export class GeminiAgent {
179192 if ( apiKey ) {
180193 this . apiKey = apiKey ;
181194 }
182- await this . config . refreshAuth ( method , apiKey ?? this . apiKey ) ;
195+
196+ // Extract gateway details if present
197+ const gatewaySchema = z . object ( {
198+ baseUrl : z . string ( ) . optional ( ) ,
199+ headers : z . record ( z . string ( ) ) . optional ( ) ,
200+ } ) ;
201+
202+ let baseUrl : string | undefined ;
203+ let headers : Record < string , string > | undefined ;
204+
205+ if ( meta ?. [ 'gateway' ] ) {
206+ const result = gatewaySchema . safeParse ( meta [ 'gateway' ] ) ;
207+ if ( result . success ) {
208+ baseUrl = result . data . baseUrl ;
209+ headers = result . data . headers ;
210+ } else {
211+ throw new acp . RequestError (
212+ - 32602 ,
213+ `Malformed gateway payload: ${ result . error . message } ` ,
214+ ) ;
215+ }
216+ }
217+
218+ this . baseUrl = baseUrl ;
219+ this . customHeaders = headers ;
220+
221+ await this . config . refreshAuth (
222+ method ,
223+ apiKey ?? this . apiKey ,
224+ baseUrl ,
225+ headers ,
226+ ) ;
183227 } catch ( e ) {
184228 throw new acp . RequestError ( - 32000 , getAcpErrorMessage ( e ) ) ;
185229 }
@@ -209,7 +253,12 @@ export class GeminiAgent {
209253 let isAuthenticated = false ;
210254 let authErrorMessage = '' ;
211255 try {
212- await config . refreshAuth ( authType , this . apiKey ) ;
256+ await config . refreshAuth (
257+ authType ,
258+ this . apiKey ,
259+ this . baseUrl ,
260+ this . customHeaders ,
261+ ) ;
213262 isAuthenticated = true ;
214263
215264 // Extra validation for Gemini API key
@@ -371,7 +420,12 @@ export class GeminiAgent {
371420 // This satisfies the security requirement to verify the user before executing
372421 // potentially unsafe server definitions.
373422 try {
374- await config . refreshAuth ( selectedAuthType , this . apiKey ) ;
423+ await config . refreshAuth (
424+ selectedAuthType ,
425+ this . apiKey ,
426+ this . baseUrl ,
427+ this . customHeaders ,
428+ ) ;
375429 } catch ( e ) {
376430 debugLogger . error ( `Authentication failed: ${ e } ` ) ;
377431 throw acp . RequestError . authRequired ( ) ;
0 commit comments