@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.
184 lines (179 loc) • 7.31 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/index.ts
var anthropic_exports = {};
__export(anthropic_exports, {
createVertexAnthropic: () => createVertexAnthropic2,
vertexAnthropic: () => vertexAnthropic
});
module.exports = __toCommonJS(anthropic_exports);
// src/anthropic/google-vertex-anthropic-provider-node.ts
var import_provider_utils2 = require("@ai-sdk/provider-utils");
// src/google-vertex-auth-google-auth-library.ts
var import_google_auth_library = require("google-auth-library");
var authInstance = null;
var authOptions = null;
function getAuth(options) {
if (!authInstance || options !== authOptions) {
authInstance = new import_google_auth_library.GoogleAuth({
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
...options
});
authOptions = options;
}
return authInstance;
}
async function generateAuthToken(options) {
const auth = getAuth(options || {});
const client = await auth.getClient();
const token = await client.getAccessToken();
return (token == null ? void 0 : token.token) || null;
}
// src/anthropic/google-vertex-anthropic-provider.ts
var import_provider = require("@ai-sdk/provider");
var import_provider_utils = 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
};
function createVertexAnthropic(options = {}) {
const getBaseURL = () => {
var _a;
const location = (0, import_provider_utils.loadOptionalSetting)({
settingValue: options.location,
environmentVariableName: "GOOGLE_VERTEX_LOCATION"
});
const project = (0, import_provider_utils.loadOptionalSetting)({
settingValue: options.project,
environmentVariableName: "GOOGLE_VERTEX_PROJECT"
});
return (_a = (0, import_provider_utils.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
});
};
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/google-vertex-anthropic-provider-node.ts
function createVertexAnthropic2(options = {}) {
return createVertexAnthropic({
...options,
headers: async () => ({
Authorization: `Bearer ${await generateAuthToken(
options.googleAuthOptions
)}`,
...await (0, import_provider_utils2.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