Files
simstudioai--sim/apps/sim/providers/vertex/index.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

80 lines
2.5 KiB
TypeScript

import { GoogleGenAI } from '@google/genai'
import { createLogger } from '@sim/logger'
import { OAuth2Client } from 'google-auth-library'
import { env } from '@/lib/core/config/env'
import type { StreamingExecution } from '@/executor/types'
import { executeGeminiRequest } from '@/providers/gemini/core'
import { getProviderDefaultModel, getProviderModels } from '@/providers/models'
import type { ProviderConfig, ProviderRequest, ProviderResponse } from '@/providers/types'
const logger = createLogger('VertexProvider')
/**
* Vertex AI provider
*
* Uses the @google/genai SDK with Vertex AI backend and OAuth authentication.
* Shares core execution logic with Google Gemini provider.
*
* Authentication:
* - Uses OAuth access token passed via googleAuthOptions.authClient
* - Token refresh is handled at the OAuth layer before calling this provider
*/
export const vertexProvider: ProviderConfig = {
id: 'vertex',
name: 'Vertex AI',
description: "Google's Vertex AI platform for Gemini models",
version: '1.0.0',
models: getProviderModels('vertex'),
defaultModel: getProviderDefaultModel('vertex'),
executeRequest: async (
request: ProviderRequest
): Promise<ProviderResponse | StreamingExecution> => {
const vertexProject = request.vertexProject || env.VERTEX_PROJECT
const vertexLocation = request.vertexLocation || env.VERTEX_LOCATION || 'us-central1'
if (!vertexProject) {
throw new Error(
'Vertex AI project is required. Please provide it via VERTEX_PROJECT environment variable or vertexProject parameter.'
)
}
if (!request.apiKey) {
throw new Error(
'Access token is required for Vertex AI. Run `gcloud auth print-access-token` to get one, or use a service account.'
)
}
// Strip 'vertex/' prefix from model name if present
const model = request.model.replace('vertex/', '')
logger.info('Creating Vertex AI client', {
project: vertexProject,
location: vertexLocation,
model,
})
// Create an OAuth2Client and set the access token
// This allows us to use an OAuth access token with the SDK
const authClient = new OAuth2Client()
authClient.setCredentials({ access_token: request.apiKey })
// Create client with Vertex AI configuration
const ai = new GoogleGenAI({
vertexai: true,
project: vertexProject,
location: vertexLocation,
googleAuthOptions: {
authClient,
},
})
return executeGeminiRequest({
ai,
model,
request,
providerType: 'vertex',
})
},
}