11import { db } from '@sim/db'
2- import { mcpServers } from '@sim/db/schema'
2+ import { account , mcpServers } from '@sim/db/schema'
33import { and , eq , inArray , isNull } from 'drizzle-orm'
44import { createLogger } from '@/lib/logs/console/logger'
55import { createMcpToolId } from '@/lib/mcp/utils'
6+ import { refreshTokenIfNeeded } from '@/app/api/auth/oauth/utils'
67import { getAllBlocks } from '@/blocks'
78import type { BlockOutput } from '@/blocks/types'
89import { AGENT , BlockType , DEFAULTS , HTTP } from '@/executor/constants'
@@ -919,6 +920,7 @@ export class AgentBlockHandler implements BlockHandler {
919920 azureApiVersion : inputs . azureApiVersion ,
920921 vertexProject : inputs . vertexProject ,
921922 vertexLocation : inputs . vertexLocation ,
923+ vertexCredential : inputs . vertexCredential ,
922924 responseFormat,
923925 workflowId : ctx . workflowId ,
924926 workspaceId : ctx . workspaceId ,
@@ -997,7 +999,17 @@ export class AgentBlockHandler implements BlockHandler {
997999 responseFormat : any ,
9981000 providerStartTime : number
9991001 ) {
1000- const finalApiKey = this . getApiKey ( providerId , model , providerRequest . apiKey )
1002+ let finalApiKey : string
1003+
1004+ // For Vertex AI, resolve OAuth credential to access token
1005+ if ( providerId === 'vertex' && providerRequest . vertexCredential ) {
1006+ finalApiKey = await this . resolveVertexCredential (
1007+ providerRequest . vertexCredential ,
1008+ ctx . workflowId
1009+ )
1010+ } else {
1011+ finalApiKey = this . getApiKey ( providerId , model , providerRequest . apiKey )
1012+ }
10011013
10021014 const { blockData, blockNameMapping } = collectBlockData ( ctx )
10031015
@@ -1024,7 +1036,6 @@ export class AgentBlockHandler implements BlockHandler {
10241036 blockNameMapping,
10251037 } )
10261038
1027- this . logExecutionSuccess ( providerId , model , ctx , block , providerStartTime , response )
10281039 return this . processProviderResponse ( response , block , responseFormat )
10291040 }
10301041
@@ -1049,15 +1060,6 @@ export class AgentBlockHandler implements BlockHandler {
10491060 throw new Error ( errorMessage )
10501061 }
10511062
1052- this . logExecutionSuccess (
1053- providerRequest . provider ,
1054- providerRequest . model ,
1055- ctx ,
1056- block ,
1057- providerStartTime ,
1058- 'HTTP response'
1059- )
1060-
10611063 const contentType = response . headers . get ( 'Content-Type' )
10621064 if ( contentType ?. includes ( HTTP . CONTENT_TYPE . EVENT_STREAM ) ) {
10631065 return this . handleStreamingResponse ( response , block , ctx , inputs )
@@ -1117,21 +1119,33 @@ export class AgentBlockHandler implements BlockHandler {
11171119 }
11181120 }
11191121
1120- private logExecutionSuccess (
1121- provider : string ,
1122- model : string ,
1123- ctx : ExecutionContext ,
1124- block : SerializedBlock ,
1125- startTime : number ,
1126- response : any
1127- ) {
1128- const executionTime = Date . now ( ) - startTime
1129- const responseType =
1130- response instanceof ReadableStream
1131- ? 'stream'
1132- : response && typeof response === 'object' && 'stream' in response
1133- ? 'streaming-execution'
1134- : 'json'
1122+ /**
1123+ * Resolves a Vertex AI OAuth credential to an access token
1124+ */
1125+ private async resolveVertexCredential ( credentialId : string , workflowId : string ) : Promise < string > {
1126+ const requestId = `vertex-${ Date . now ( ) } `
1127+
1128+ logger . info ( `[${ requestId } ] Resolving Vertex AI credential: ${ credentialId } ` )
1129+
1130+ // Get the credential - we need to find the owner
1131+ // Since we're in a workflow context, we can query the credential directly
1132+ const credential = await db . query . account . findFirst ( {
1133+ where : eq ( account . id , credentialId ) ,
1134+ } )
1135+
1136+ if ( ! credential ) {
1137+ throw new Error ( `Vertex AI credential not found: ${ credentialId } ` )
1138+ }
1139+
1140+ // Refresh the token if needed
1141+ const { accessToken } = await refreshTokenIfNeeded ( requestId , credential , credentialId )
1142+
1143+ if ( ! accessToken ) {
1144+ throw new Error ( 'Failed to get Vertex AI access token' )
1145+ }
1146+
1147+ logger . info ( `[${ requestId } ] Successfully resolved Vertex AI credential` )
1148+ return accessToken
11351149 }
11361150
11371151 private handleExecutionError (
0 commit comments