UNPKG

@ai-sdk/google-vertex

Version:
200 lines (199 loc) • 8.18 kB
import { convertUint8ArrayToBase64, isValidHostnamePart, lazySchema, loadOptionalSetting, parseProviderOptions, resolve, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils"; import { GoogleAuth } from "google-auth-library"; import { InvalidArgumentError, NoSuchModelError } from "@ai-sdk/provider"; import { AnthropicLanguageModel, anthropicTools } from "@ai-sdk/anthropic/internal"; import { z } from "zod/v4"; //#region src/google-vertex-auth-google-auth-library.ts function createAuthTokenGenerator(options) { const auth = new GoogleAuth({ scopes: ["https://www.googleapis.com/auth/cloud-platform"], ...options }); return async function generateAuthToken() { return (await (await auth.getClient()).getAccessToken())?.token ?? null; }; } //#endregion //#region src/anthropic/google-vertex-anthropic-options.ts const googleVertexAnthropicProviderOptions = lazySchema(() => zodSchema(z.object({ labels: z.record(z.string(), z.string()).optional() }))); //#endregion //#region src/anthropic/google-vertex-anthropic-provider.ts /** * Tools supported by Google Vertex Anthropic. * This is a subset of the full Anthropic tools - only these are recognized by the Vertex API. */ const googleVertexAnthropicTools = { /** * The bash tool enables Claude to execute shell commands in a persistent bash session, * allowing system operations, script execution, and command-line automation. * * Image results are supported. */ bash_20241022: anthropicTools.bash_20241022, /** * The bash tool enables Claude to execute shell commands in a persistent bash session, * allowing system operations, script execution, and command-line automation. * * Image results are supported. */ bash_20250124: anthropicTools.bash_20250124, /** * Claude can use an Anthropic-defined text editor tool to view and modify text files, * helping you debug, fix, and improve your code or other text documents. * * Supported models: Claude Sonnet 3.5 */ textEditor_20241022: anthropicTools.textEditor_20241022, /** * Claude can use an Anthropic-defined text editor tool to view and modify text files, * helping you debug, fix, and improve your code or other text documents. * * Supported models: Claude Sonnet 3.7 */ textEditor_20250124: anthropicTools.textEditor_20250124, /** * Claude can use an Anthropic-defined text editor tool to view and modify text files. * Note: This version does not support the "undo_edit" command. * @deprecated Use textEditor_20250728 instead */ textEditor_20250429: anthropicTools.textEditor_20250429, /** * Claude can use an Anthropic-defined text editor tool to view and modify text files. * Note: This version does not support the "undo_edit" command and adds optional max_characters parameter. * Supported models: Claude Sonnet 4, Opus 4, and Opus 4.1 */ textEditor_20250728: anthropicTools.textEditor_20250728, /** * Claude can interact with computer environments through the computer use tool, which * provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction. * * Image results are supported. */ computer_20241022: anthropicTools.computer_20241022, /** * Creates a web search tool that gives Claude direct access to real-time web content. */ webSearch_20250305: anthropicTools.webSearch_20250305, /** * Creates a tool search tool that uses regex patterns to find tools. * * The tool search tool enables Claude to work with hundreds or thousands of tools * by dynamically discovering and loading them on-demand. * * Use `providerOptions: { anthropic: { deferLoading: true } }` on other tools * to mark them for deferred loading. */ toolSearchRegex_20251119: anthropicTools.toolSearchRegex_20251119, /** * Creates a tool search tool that uses BM25 (natural language) to find tools. * * The tool search tool enables Claude to work with hundreds or thousands of tools * by dynamically discovering and loading them on-demand. * * Use `providerOptions: { anthropic: { deferLoading: true } }` on other tools * to mark them for deferred loading. */ toolSearchBm25_20251119: anthropicTools.toolSearchBm25_20251119 }; /** * Create a Google Vertex Anthropic provider instance. */ function createGoogleVertexAnthropic$1(options = {}) { const getBaseURL = () => { const baseURL = withoutTrailingSlash(options.baseURL); if (baseURL != null) return baseURL; const location = loadOptionalSetting({ settingValue: options.location, environmentVariableName: "GOOGLE_VERTEX_LOCATION" }); if (location != null && !isValidHostnamePart(location)) throw new InvalidArgumentError({ argument: "location", message: "Invalid Google Vertex location. Expected a single DNS label (letters, digits, and hyphens). Use `baseURL` for custom endpoints." }); const project = loadOptionalSetting({ settingValue: options.project, environmentVariableName: "GOOGLE_VERTEX_PROJECT" }); const getHost = () => { if (location === "global") return "aiplatform.googleapis.com"; else if (location === "eu" || location === "us") return `aiplatform.${location}.rep.googleapis.com`; else return `${location}-aiplatform.googleapis.com`; }; return `https://${getHost()}/v1/projects/${project}/locations/${location}/publishers/anthropic/models`; }; const createChatModel = (modelId) => new AnthropicLanguageModel(modelId, { provider: "googleVertex.anthropic.messages", baseURL: getBaseURL(), headers: options.headers ?? {}, fetch: options.fetch, buildRequestUrl: (baseURL, isStreaming) => `${baseURL}/${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`, transformRequestBody: (args) => { const { model: _model, ...rest } = args; return { ...rest, anthropic_version: "vertex-2023-10-16" }; }, transformRequestHeaders: async (headers, providerOptions) => { const vertexOptions = await parseProviderOptions({ provider: "vertex", providerOptions, schema: googleVertexAnthropicProviderOptions }); if (vertexOptions?.labels == null || Object.keys(headers).some((name) => name.toLowerCase() === "x-vertex-ai-labels" && headers[name] != null)) return headers; return { ...headers, "x-vertex-ai-labels": convertUint8ArrayToBase64(new TextEncoder().encode(JSON.stringify(vertexOptions.labels))) }; }, supportedUrls: () => ({}), supportsNativeStructuredOutput: options.supportsNativeStructuredOutput === true && supportsNativeStructuredOutput(modelId), supportsStrictTools: false }); const provider = function(modelId) { if (new.target) throw new Error("The Anthropic model function cannot be called with the new keyword."); return createChatModel(modelId); }; provider.specificationVersion = "v4"; provider.languageModel = createChatModel; provider.chat = createChatModel; provider.messages = createChatModel; provider.embeddingModel = (modelId) => { throw new NoSuchModelError({ modelId, modelType: "embeddingModel" }); }; provider.textEmbeddingModel = provider.embeddingModel; provider.imageModel = (modelId) => { throw new NoSuchModelError({ modelId, modelType: "imageModel" }); }; provider.tools = googleVertexAnthropicTools; return provider; } function supportsNativeStructuredOutput(modelId) { const version = /^claude-[a-z]+-(\d+)(?:-(\d+))?(?:@|$)/.exec(modelId); return version != null && (Number(version[1]) > 4 || Number(version[1]) === 4 && Number(version[2]) >= 5); } //#endregion //#region src/anthropic/google-vertex-anthropic-provider-node.ts function createGoogleVertexAnthropic(options = {}) { const generateAuthToken = options.generateAuthToken ?? createAuthTokenGenerator(options.googleAuthOptions); return createGoogleVertexAnthropic$1({ ...options, headers: async () => ({ Authorization: `Bearer ${await generateAuthToken()}`, ...await resolve(options.headers) }) }); } /** * Default Google Vertex Anthropic provider instance. */ const googleVertexAnthropic = createGoogleVertexAnthropic(); //#endregion export { createGoogleVertexAnthropic, createGoogleVertexAnthropic as createVertexAnthropic, googleVertexAnthropic, googleVertexAnthropic as vertexAnthropic }; //# sourceMappingURL=index.js.map