@ai-sdk/google-vertex
Version:
The **[Google Vertex provider](https://ai-sdk.dev/providers/ai-sdk-providers/google-vertex)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the [Google Vertex AI](https://cloud.google.com/vertex-ai) APIs.
296 lines (289 loc) • 11.3 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/anthropic/edge/index.ts
var index_exports = {};
__export(index_exports, {
createVertexAnthropic: () => createVertexAnthropic2,
vertexAnthropic: () => vertexAnthropic
});
module.exports = __toCommonJS(index_exports);
// src/anthropic/edge/google-vertex-anthropic-provider-edge.ts
var import_provider_utils3 = require("@ai-sdk/provider-utils");
// src/edge/google-vertex-auth-edge.ts
var import_provider_utils = require("@ai-sdk/provider-utils");
// src/version.ts
var VERSION = true ? "4.0.137" : "0.0.0-test";
// src/edge/google-vertex-auth-edge.ts
var loadCredentials = async () => {
try {
return {
clientEmail: (0, import_provider_utils.loadSetting)({
settingValue: void 0,
settingName: "clientEmail",
environmentVariableName: "GOOGLE_CLIENT_EMAIL",
description: "Google client email"
}),
privateKey: (0, import_provider_utils.loadSetting)({
settingValue: void 0,
settingName: "privateKey",
environmentVariableName: "GOOGLE_PRIVATE_KEY",
description: "Google private key"
}),
privateKeyId: (0, import_provider_utils.loadOptionalSetting)({
settingValue: void 0,
environmentVariableName: "GOOGLE_PRIVATE_KEY_ID"
})
};
} catch (error) {
throw new Error(`Failed to load Google credentials: ${error.message}`);
}
};
var base64url = (str) => {
return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
};
var importPrivateKey = async (pemKey) => {
const pemHeader = "-----BEGIN PRIVATE KEY-----";
const pemFooter = "-----END PRIVATE KEY-----";
const pemContents = pemKey.replace(pemHeader, "").replace(pemFooter, "").replace(/\s/g, "");
const binaryString = atob(pemContents);
const binaryData = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
binaryData[i] = binaryString.charCodeAt(i);
}
return await crypto.subtle.importKey(
"pkcs8",
binaryData,
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
true,
["sign"]
);
};
var buildJwt = async (credentials) => {
const now = Math.floor(Date.now() / 1e3);
const header = {
alg: "RS256",
typ: "JWT"
};
if (credentials.privateKeyId) {
header.kid = credentials.privateKeyId;
}
const payload = {
iss: credentials.clientEmail,
scope: "https://www.googleapis.com/auth/cloud-platform",
aud: "https://oauth2.googleapis.com/token",
exp: now + 3600,
iat: now
};
const privateKey = await importPrivateKey(credentials.privateKey);
const signingInput = `${base64url(JSON.stringify(header))}.${base64url(
JSON.stringify(payload)
)}`;
const encoder = new TextEncoder();
const data = encoder.encode(signingInput);
const signature = await crypto.subtle.sign(
"RSASSA-PKCS1-v1_5",
privateKey,
data
);
const signatureBase64 = base64url(
String.fromCharCode(...new Uint8Array(signature))
);
return `${base64url(JSON.stringify(header))}.${base64url(
JSON.stringify(payload)
)}.${signatureBase64}`;
};
async function generateAuthToken(credentials) {
try {
const creds = credentials || await loadCredentials();
const jwt = await buildJwt(creds);
const response = await fetch("https://oauth2.googleapis.com/token", {
method: "POST",
headers: (0, import_provider_utils.withUserAgentSuffix)(
{ "Content-Type": "application/x-www-form-urlencoded" },
`ai-sdk/google-vertex/${VERSION}`,
(0, import_provider_utils.getRuntimeEnvironmentUserAgent)()
),
body: new URLSearchParams({
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
assertion: jwt
})
});
if (!response.ok) {
throw new Error(`Token request failed: ${response.statusText}`);
}
const data = await response.json();
return data.access_token;
} catch (error) {
throw error;
}
}
// src/anthropic/google-vertex-anthropic-provider.ts
var import_provider = require("@ai-sdk/provider");
var import_provider_utils2 = require("@ai-sdk/provider-utils");
var import_internal = require("@ai-sdk/anthropic/internal");
var vertexAnthropicTools = {
/**
* 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: import_internal.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: import_internal.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: import_internal.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: import_internal.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: import_internal.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: import_internal.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: import_internal.anthropicTools.computer_20241022,
/**
* Creates a web search tool that gives Claude direct access to real-time web content.
*/
webSearch_20250305: import_internal.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: import_internal.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: import_internal.anthropicTools.toolSearchBm25_20251119
};
function createVertexAnthropic(options = {}) {
const getBaseURL = () => {
var _a;
const location = (0, import_provider_utils2.loadOptionalSetting)({
settingValue: options.location,
environmentVariableName: "GOOGLE_VERTEX_LOCATION"
});
const project = (0, import_provider_utils2.loadOptionalSetting)({
settingValue: options.project,
environmentVariableName: "GOOGLE_VERTEX_PROJECT"
});
return (_a = (0, import_provider_utils2.withoutTrailingSlash)(options.baseURL)) != null ? _a : `https://${location === "global" ? "" : location + "-"}aiplatform.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`;
};
const createChatModel = (modelId) => {
var _a;
return new import_internal.AnthropicMessagesLanguageModel(modelId, {
provider: "vertex.anthropic.messages",
baseURL: getBaseURL(),
headers: (_a = options.headers) != null ? _a : {},
fetch: options.fetch,
buildRequestUrl: (baseURL, isStreaming) => `${baseURL}/${modelId}:${isStreaming ? "streamRawPredict" : "rawPredict"}`,
transformRequestBody: (args) => {
const { model, ...rest } = args;
return {
...rest,
anthropic_version: "vertex-2023-10-16"
};
},
// Google Vertex Anthropic doesn't support URL sources, force download and base64 conversion
supportedUrls: () => ({}),
// force the use of JSON tool fallback for structured outputs since beta header isn't supported
supportsNativeStructuredOutput: false,
// Vertex Anthropic doesn't support strict mode on tool definitions.
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 = "v3";
provider.languageModel = createChatModel;
provider.chat = createChatModel;
provider.messages = createChatModel;
provider.embeddingModel = (modelId) => {
throw new import_provider.NoSuchModelError({ modelId, modelType: "embeddingModel" });
};
provider.textEmbeddingModel = provider.embeddingModel;
provider.imageModel = (modelId) => {
throw new import_provider.NoSuchModelError({ modelId, modelType: "imageModel" });
};
provider.tools = vertexAnthropicTools;
return provider;
}
// src/anthropic/edge/google-vertex-anthropic-provider-edge.ts
function createVertexAnthropic2(options = {}) {
var _a;
const generateAuthToken2 = (_a = options.generateAuthToken) != null ? _a : (() => generateAuthToken(options.googleCredentials));
return createVertexAnthropic({
...options,
headers: async () => ({
Authorization: `Bearer ${await generateAuthToken2()}`,
...await (0, import_provider_utils3.resolve)(options.headers)
})
});
}
var vertexAnthropic = createVertexAnthropic2();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createVertexAnthropic,
vertexAnthropic
});
//# sourceMappingURL=index.js.map