-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(vertex-anthropic): add support for custom Vertex AI Anthropic model #1651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Abmarne
wants to merge
4
commits into
eyaltoledano:next
Choose a base branch
from
Abmarne:main
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fa34d8e
feat(vertex-anthropic): add support for custom Vertex AI Anthropic mo…
Abmarne abf144a
fix(vertex-anthropic): address PR review feedback
Crunchyman-ralph 607c597
chore: add changeset for vertex-anthropic provider
Crunchyman-ralph eb5331b
fix: add VertexAnthropicProvider mock to ai-services-unified test
Crunchyman-ralph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /** | ||
| * google-vertex-anthropic.js | ||
| * AI provider implementation for Anthropic models on Google Vertex AI using Vercel AI SDK. | ||
| * This provider uses the createVertexAnthropic client to route requests to the | ||
| * publishers/anthropic endpoint instead of publishers/google. | ||
| */ | ||
|
|
||
| import { createVertexAnthropic } from '@ai-sdk/google-vertex/anthropic'; | ||
| import { resolveEnvVariable } from '../../scripts/modules/utils.js'; | ||
cursor[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import { log } from '../../scripts/modules/utils.js'; | ||
| import { BaseAIProvider } from './base-provider.js'; | ||
|
|
||
| // Vertex Anthropic-specific error classes | ||
| class VertexAnthropicAuthError extends Error { | ||
| constructor(message) { | ||
| super(message); | ||
| this.name = 'VertexAnthropicAuthError'; | ||
| this.code = 'vertex_anthropic_auth_error'; | ||
| } | ||
| } | ||
|
|
||
| class VertexAnthropicConfigError extends Error { | ||
| constructor(message) { | ||
| super(message); | ||
| this.name = 'VertexAnthropicConfigError'; | ||
| this.code = 'vertex_anthropic_config_error'; | ||
| } | ||
| } | ||
|
|
||
| class VertexAnthropicApiError extends Error { | ||
| constructor(message, statusCode) { | ||
| super(message); | ||
| this.name = 'VertexAnthropicApiError'; | ||
| this.code = 'vertex_anthropic_api_error'; | ||
| this.statusCode = statusCode; | ||
| } | ||
| } | ||
|
|
||
| export class VertexAnthropicProvider extends BaseAIProvider { | ||
| constructor() { | ||
| super(); | ||
| this.name = 'Google Vertex AI (Anthropic)'; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the required API key environment variable name for Vertex AI Anthropic. | ||
| * @returns {string} The environment variable name | ||
| */ | ||
| getRequiredApiKeyName() { | ||
| 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 Anthropic-specific authentication parameters | ||
| * @param {object} params - Parameters to validate | ||
| * @throws {VertexAnthropicAuthError|VertexAnthropicConfigError} | ||
| */ | ||
| validateAuth(params) { | ||
| const { apiKey, projectId, location, credentials } = params; | ||
|
|
||
| // Check for API key OR service account credentials | ||
| const hasValidApiKey = this.isValidCredential(apiKey); | ||
| const hasValidCredentials = this.isValidCredential(credentials); | ||
|
|
||
| if (!hasValidApiKey && !hasValidCredentials) { | ||
| throw new VertexAnthropicAuthError( | ||
| 'Vertex AI (Anthropic) 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 || | ||
| (typeof projectId === 'string' && projectId.trim().length === 0) | ||
| ) { | ||
| throw new VertexAnthropicConfigError( | ||
| 'Google Cloud project ID is required for Vertex AI. Set VERTEX_PROJECT_ID environment variable.' | ||
| ); | ||
| } | ||
|
|
||
| // Location is required for Vertex AI | ||
| if ( | ||
| !location || | ||
| (typeof location === 'string' && location.trim().length === 0) | ||
| ) { | ||
| throw new VertexAnthropicConfigError( | ||
| 'Google Cloud location is required for Vertex AI. Set VERTEX_LOCATION environment variable (e.g., "us-central1").' | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates and returns a Google Vertex AI Anthropic client instance. | ||
| * @param {object} params - Parameters for client initialization | ||
| * @param {string} [params.apiKey] - Google API key | ||
| * @param {string} params.projectId - Google Cloud project ID | ||
| * @param {string} params.location - Google Cloud location (e.g., "us-central1") | ||
| * @param {object} [params.credentials] - Service account credentials object | ||
| * @param {string} [params.baseURL] - Optional custom API endpoint | ||
| * @returns {Function} Google Vertex AI Anthropic client function | ||
| * @throws {Error} If required parameters are missing or initialization fails | ||
| */ | ||
| getClient(params) { | ||
| try { | ||
| const { apiKey, projectId, location, credentials, baseURL } = params; | ||
| const fetchImpl = this.createProxyFetch(); | ||
|
|
||
| // Configure auth options - either API key or service account | ||
| const authOptions = {}; | ||
| if (apiKey) { | ||
| // Vercel AI SDK expects googleAuthOptions even when using apiKey for some configurations | ||
| authOptions.googleAuthOptions = { | ||
| ...credentials, | ||
| apiKey | ||
| }; | ||
| } else if (credentials) { | ||
| authOptions.googleAuthOptions = credentials; | ||
| } | ||
|
|
||
| // Return Vertex AI Anthropic client | ||
| return createVertexAnthropic({ | ||
| ...authOptions, | ||
| project: projectId, | ||
| location, | ||
| ...(baseURL && { baseURL }), | ||
| ...(fetchImpl && { fetch: fetchImpl }) | ||
| }); | ||
| } catch (error) { | ||
| this.handleError('client initialization', error); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handle errors from Vertex AI Anthropic | ||
| * @param {string} operation - Description of the operation that failed | ||
| * @param {Error} error - The error object | ||
| * @throws {Error} Rethrows the error with additional context | ||
| */ | ||
| handleError(operation, error) { | ||
| log('error', `Vertex AI (Anthropic) ${operation} error:`, error); | ||
|
|
||
| // Handle known error types | ||
| if ( | ||
| error.name === 'VertexAnthropicAuthError' || | ||
| error.name === 'VertexAnthropicConfigError' || | ||
| error.name === 'VertexAnthropicApiError' | ||
| ) { | ||
| throw error; | ||
| } | ||
|
|
||
| // Handle network/API errors | ||
| if (error.response) { | ||
| const statusCode = error.response.status; | ||
| const errorMessage = error.response.data?.error?.message || error.message; | ||
|
|
||
| // Categorize by status code | ||
| if (statusCode === 401 || statusCode === 403) { | ||
| throw new VertexAnthropicAuthError(`Authentication failed: ${errorMessage}`); | ||
| } else if (statusCode === 400) { | ||
| throw new VertexAnthropicConfigError(`Invalid request: ${errorMessage}`); | ||
| } else { | ||
| throw new VertexAnthropicApiError( | ||
| `API error (${statusCode}): ${errorMessage}`, | ||
| statusCode | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Generic error handling | ||
| throw new Error(`Vertex AI (Anthropic) ${operation} failed: ${error.message}`); | ||
| } | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: eyaltoledano/claude-task-master
Length of output: 1990
🏁 Script executed:
Repository: eyaltoledano/claude-task-master
Length of output: 1228
🏁 Script executed:
Repository: eyaltoledano/claude-task-master
Length of output: 3425
🏁 Script executed:
Repository: eyaltoledano/claude-task-master
Length of output: 528
🏁 Script executed:
Repository: eyaltoledano/claude-task-master
Length of output: 353
Add
vertex-anthropicto config-manager.js and supported-models.json registries.Lines 586–590 allow users to set
VERTEX_ANTHROPICas the provider, but the integration is incomplete. The provider is missing from:isApiKeySet()andgetMcpApiKeyStatus()will fail for this provider.getAvailableModels()will not discoververtex-anthropicmodels, andMODEL_MAPvalidation will not recognize the provider.Users can persist the provider here but will encounter broken key-status checks and missing model metadata downstream. Add the provider to both registries with the appropriate API key mapping and model metadata.
🤖 Prompt for AI Agents