@@ -57,6 +57,39 @@ export namespace Provider {
5757 return isGpt5OrLater ( modelID ) && ! modelID . startsWith ( "gpt-5-mini" )
5858 }
5959
60+ function googleVertexVars ( options : Record < string , any > ) {
61+ const project =
62+ Env . get ( "GOOGLE_VERTEX_PROJECT" ) ??
63+ options [ "project" ] ??
64+ Env . get ( "GOOGLE_CLOUD_PROJECT" ) ??
65+ Env . get ( "GCP_PROJECT" ) ??
66+ Env . get ( "GCLOUD_PROJECT" )
67+ const location =
68+ Env . get ( "GOOGLE_VERTEX_LOCATION" ) ??
69+ options [ "location" ] ??
70+ Env . get ( "GOOGLE_CLOUD_LOCATION" ) ??
71+ Env . get ( "VERTEX_LOCATION" ) ??
72+ "us-central1"
73+ const endpoint =
74+ Env . get ( "GOOGLE_VERTEX_ENDPOINT" ) ??
75+ ( location === "global" ? "aiplatform.googleapis.com" : `${ location } -aiplatform.googleapis.com` )
76+ return {
77+ GOOGLE_VERTEX_PROJECT : project ,
78+ GOOGLE_VERTEX_LOCATION : location ,
79+ GOOGLE_VERTEX_ENDPOINT : endpoint ,
80+ }
81+ }
82+
83+ function loadBaseURL ( model : Model , options : Record < string , any > ) {
84+ const raw = options [ "baseURL" ] ?? model . api . url
85+ if ( typeof raw !== "string" ) return raw
86+ const vars = model . providerID === "google-vertex" ? googleVertexVars ( options ) : undefined
87+ return raw . replace ( / \$ \{ ( [ ^ } ] + ) \} / g, ( match , key ) => {
88+ const val = Env . get ( String ( key ) ) ?? vars ?. [ String ( key ) as keyof typeof vars ]
89+ return val ?? match
90+ } )
91+ }
92+
6093 const BUNDLED_PROVIDERS : Record < string , ( options : any ) => SDK > = {
6194 "@ai-sdk/amazon-bedrock" : createAmazonBedrock ,
6295 "@ai-sdk/anthropic" : createAnthropic ,
@@ -353,16 +386,35 @@ export namespace Provider {
353386 } ,
354387 }
355388 } ,
356- "google-vertex" : async ( ) => {
357- const project = Env . get ( "GOOGLE_CLOUD_PROJECT" ) ?? Env . get ( "GCP_PROJECT" ) ?? Env . get ( "GCLOUD_PROJECT" )
358- const location = Env . get ( "GOOGLE_CLOUD_LOCATION" ) ?? Env . get ( "VERTEX_LOCATION" ) ?? "us-east5"
389+ "google-vertex" : async ( provider ) => {
390+ const project =
391+ provider . options ?. project ??
392+ Env . get ( "GOOGLE_CLOUD_PROJECT" ) ??
393+ Env . get ( "GCP_PROJECT" ) ??
394+ Env . get ( "GCLOUD_PROJECT" )
395+
396+ const location =
397+ provider . options ?. location ?? Env . get ( "GOOGLE_CLOUD_LOCATION" ) ?? Env . get ( "VERTEX_LOCATION" ) ?? "us-central1"
398+
359399 const autoload = Boolean ( project )
360400 if ( ! autoload ) return { autoload : false }
361401 return {
362402 autoload : true ,
363403 options : {
364404 project,
365405 location,
406+ fetch : async ( input : RequestInfo | URL , init ?: RequestInit ) => {
407+ const { GoogleAuth } = await import ( await BunProc . install ( "google-auth-library" ) )
408+ const auth = new GoogleAuth ( )
409+ const client = await auth . getApplicationDefault ( )
410+ const credentials = await client . credential
411+ const token = await credentials . getAccessToken ( )
412+
413+ const headers = new Headers ( init ?. headers )
414+ headers . set ( "Authorization" , `Bearer ${ token . token } ` )
415+
416+ return fetch ( input , { ...init , headers } )
417+ } ,
366418 } ,
367419 async getModel ( sdk : any , modelID : string ) {
368420 const id = String ( modelID ) . trim ( )
@@ -994,11 +1046,16 @@ export namespace Provider {
9941046 const provider = s . providers [ model . providerID ]
9951047 const options = { ...provider . options }
9961048
1049+ if ( model . providerID === "google-vertex" && ! model . api . npm . includes ( "@ai-sdk/openai-compatible" ) ) {
1050+ delete options . fetch
1051+ }
1052+
9971053 if ( model . api . npm . includes ( "@ai-sdk/openai-compatible" ) && options [ "includeUsage" ] !== false ) {
9981054 options [ "includeUsage" ] = true
9991055 }
10001056
1001- if ( ! options [ "baseURL" ] ) options [ "baseURL" ] = model . api . url
1057+ const baseURL = loadBaseURL ( model , options )
1058+ if ( baseURL !== undefined ) options [ "baseURL" ] = baseURL
10021059 if ( options [ "apiKey" ] === undefined && provider . key ) options [ "apiKey" ] = provider . key
10031060 if ( model . headers )
10041061 options [ "headers" ] = {
0 commit comments