1+ import { db } from '@sim/db'
2+ import { account } from '@sim/db/schema'
13import { createLogger } from '@sim/logger'
4+ import { eq } from 'drizzle-orm'
25import { getBaseUrl } from '@/lib/core/utils/urls'
6+ import { refreshTokenIfNeeded } from '@/app/api/auth/oauth/utils'
37import { executeProviderRequest } from '@/providers'
48import { getApiKey , getProviderFromModel } from '@/providers/utils'
59
@@ -138,6 +142,26 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
138142
139143 const providerId = getProviderFromModel ( model )
140144
145+ // Resolve Vertex AI OAuth credential to access token if needed
146+ let resolvedApiKey = apiKey
147+ const resolvedCredentials = { ...providerCredentials }
148+ if ( providerId === 'vertex' && providerCredentials ?. vertexCredential ) {
149+ const credential = await db . query . account . findFirst ( {
150+ where : eq ( account . id , providerCredentials . vertexCredential ) ,
151+ } )
152+ if ( credential ) {
153+ const { accessToken } = await refreshTokenIfNeeded (
154+ requestId ,
155+ credential ,
156+ providerCredentials . vertexCredential
157+ )
158+ if ( accessToken ) {
159+ resolvedApiKey = accessToken
160+ }
161+ }
162+ resolvedCredentials . vertexCredential = undefined
163+ }
164+
141165 const response = await executeProviderRequest ( providerId , {
142166 model,
143167 systemPrompt,
@@ -148,8 +172,8 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
148172 } ,
149173 ] ,
150174 temperature : 0.1 , // Low temperature for consistent scoring
151- apiKey,
152- ...providerCredentials ,
175+ apiKey : resolvedApiKey ,
176+ ...resolvedCredentials ,
153177 } )
154178
155179 if ( response instanceof ReadableStream || ( 'stream' in response && 'execution' in response ) ) {
0 commit comments