Fix/vertex auth service account (#1542)

This commit is contained in:
Momchil Dimitrov Georgevski
2025-12-22 10:29:49 +01:00
committed by GitHub
parent fc1a79f256
commit b817d6f9f2
4 changed files with 64 additions and 10 deletions

View File

@@ -0,0 +1,5 @@
---
"task-master-ai": patch
---
Fixed vertex-ai authentication when using service account and vertex location env variable.

View File

@@ -359,10 +359,14 @@ function _getVertexConfiguration(projectRoot, session) {
`Using Vertex AI configuration: Project ID=${projectId}, Location=${location}`
);
const credentials = credentialsPath
? { keyFile: credentialsPath }
: undefined;
return {
projectId,
location,
...(credentialsPath && { credentials: { credentialsFromEnv: true } })
...(credentials && { credentials })
};
}

View File

@@ -719,11 +719,11 @@ function getVertexProjectId(explicitRoot = null) {
/**
* Gets the Google Cloud location for Vertex AI from configuration
* @param {string|null} explicitRoot - Optional explicit path to the project root.
* @returns {string} The location or default value of "us-central1"
* @returns {string} The location
*/
function getVertexLocation(explicitRoot = null) {
// Return value from config or default
return getGlobalConfig(explicitRoot).vertexLocation || 'us-central1';
return getGlobalConfig(explicitRoot).vertexLocation;
}
function getResponseLanguage(explicitRoot = null) {

View File

@@ -48,30 +48,71 @@ export class VertexAIProvider extends BaseAIProvider {
return 'GOOGLE_API_KEY';
}
/**
* API key is optional, Service Account credentials can be used instead.
* @returns {boolean}
*/
isRequiredApiKey() {
return false;
}
/**
* API key or Service Account is mandatory.
* @returns {boolean}
*/
isAuthenticationRequired() {
return true;
}
/**
* Validates that a credential value is present and non-empty.
* @private
* @param {string|object|null|undefined} value
* @returns {boolean}
*/
isValidCredential(value) {
if (!value) return false;
if (typeof value === 'string') {
return value.trim().length > 0;
}
return typeof value === 'object';
}
/**
* Validates Vertex AI-specific authentication parameters
* @param {object} params - Parameters to validate
* @throws {Error} If required parameters are missing
* @throws {VertexAuthError|VertexConfigError}
*/
validateAuth(params) {
const { apiKey, projectId, location, credentials } = params;
// Check for API key OR service account credentials
if (!apiKey && !credentials) {
const hasValidApiKey = this.isValidCredential(apiKey);
const hasValidCredentials = this.isValidCredential(credentials);
if (!hasValidApiKey && !hasValidCredentials) {
throw new VertexAuthError(
'Either Google API key (GOOGLE_API_KEY) or service account credentials (GOOGLE_APPLICATION_CREDENTIALS) is required for Vertex AI'
'Vertex AI requires authentication. Provide one of the following:\n' +
' • GOOGLE_API_KEY environment variable (typical for API-based auth), OR\n' +
' • GOOGLE_APPLICATION_CREDENTIALS pointing to a service account JSON file (recommended for production)'
);
}
// Project ID is required for Vertex AI
if (!projectId) {
if (
!projectId ||
(typeof projectId === 'string' && projectId.trim().length === 0)
) {
throw new VertexConfigError(
'Google Cloud project ID is required for Vertex AI. Set VERTEX_PROJECT_ID environment variable.'
);
}
// Location is required for Vertex AI
if (!location) {
if (
!location ||
(typeof location === 'string' && location.trim().length === 0)
) {
throw new VertexConfigError(
'Google Cloud location is required for Vertex AI. Set VERTEX_LOCATION environment variable (e.g., "us-central1").'
);
@@ -97,7 +138,11 @@ export class VertexAIProvider extends BaseAIProvider {
// Configure auth options - either API key or service account
const authOptions = {};
if (apiKey) {
authOptions.apiKey = apiKey;
// Vercel AI SDK expects googleAuthOptions even when using apiKey for some configurations
authOptions.googleAuthOptions = {
...credentials,
apiKey
};
} else if (credentials) {
authOptions.googleAuthOptions = credentials;
}
@@ -105,7 +150,7 @@ export class VertexAIProvider extends BaseAIProvider {
// Return Vertex AI client
return createVertex({
...authOptions,
projectId,
project: projectId,
location,
...(baseURL && { baseURL }),
...(fetchImpl && { fetch: fetchImpl })