1158 lines
39 KiB
JavaScript
1158 lines
39 KiB
JavaScript
"use strict";
|
|
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/internal/index.ts
|
|
var internal_exports = {};
|
|
__export(internal_exports, {
|
|
AnthropicMessagesLanguageModel: () => AnthropicMessagesLanguageModel,
|
|
anthropicTools: () => anthropicTools
|
|
});
|
|
module.exports = __toCommonJS(internal_exports);
|
|
|
|
// src/anthropic-messages-language-model.ts
|
|
var import_provider3 = require("@ai-sdk/provider");
|
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
var import_zod2 = require("zod");
|
|
|
|
// src/anthropic-error.ts
|
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
var import_zod = require("zod");
|
|
var anthropicErrorDataSchema = import_zod.z.object({
|
|
type: import_zod.z.literal("error"),
|
|
error: import_zod.z.object({
|
|
type: import_zod.z.string(),
|
|
message: import_zod.z.string()
|
|
})
|
|
});
|
|
var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
errorSchema: anthropicErrorDataSchema,
|
|
errorToMessage: (data) => data.error.message
|
|
});
|
|
|
|
// src/anthropic-prepare-tools.ts
|
|
var import_provider = require("@ai-sdk/provider");
|
|
function prepareTools(mode) {
|
|
var _a;
|
|
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
const toolWarnings = [];
|
|
const betas = /* @__PURE__ */ new Set();
|
|
if (tools == null) {
|
|
return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
|
|
}
|
|
const anthropicTools2 = [];
|
|
for (const tool of tools) {
|
|
switch (tool.type) {
|
|
case "function":
|
|
anthropicTools2.push({
|
|
name: tool.name,
|
|
description: tool.description,
|
|
input_schema: tool.parameters
|
|
});
|
|
break;
|
|
case "provider-defined":
|
|
switch (tool.id) {
|
|
case "anthropic.computer_20250124":
|
|
betas.add("computer-use-2025-01-24");
|
|
anthropicTools2.push({
|
|
name: tool.name,
|
|
type: "computer_20250124",
|
|
display_width_px: tool.args.displayWidthPx,
|
|
display_height_px: tool.args.displayHeightPx,
|
|
display_number: tool.args.displayNumber
|
|
});
|
|
break;
|
|
case "anthropic.computer_20241022":
|
|
betas.add("computer-use-2024-10-22");
|
|
anthropicTools2.push({
|
|
name: tool.name,
|
|
type: "computer_20241022",
|
|
display_width_px: tool.args.displayWidthPx,
|
|
display_height_px: tool.args.displayHeightPx,
|
|
display_number: tool.args.displayNumber
|
|
});
|
|
break;
|
|
case "anthropic.text_editor_20250124":
|
|
betas.add("computer-use-2025-01-24");
|
|
anthropicTools2.push({
|
|
name: tool.name,
|
|
type: "text_editor_20250124"
|
|
});
|
|
break;
|
|
case "anthropic.text_editor_20241022":
|
|
betas.add("computer-use-2024-10-22");
|
|
anthropicTools2.push({
|
|
name: tool.name,
|
|
type: "text_editor_20241022"
|
|
});
|
|
break;
|
|
case "anthropic.bash_20250124":
|
|
betas.add("computer-use-2025-01-24");
|
|
anthropicTools2.push({
|
|
name: tool.name,
|
|
type: "bash_20250124"
|
|
});
|
|
break;
|
|
case "anthropic.bash_20241022":
|
|
betas.add("computer-use-2024-10-22");
|
|
anthropicTools2.push({
|
|
name: tool.name,
|
|
type: "bash_20241022"
|
|
});
|
|
break;
|
|
default:
|
|
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
break;
|
|
}
|
|
break;
|
|
default:
|
|
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
break;
|
|
}
|
|
}
|
|
const toolChoice = mode.toolChoice;
|
|
if (toolChoice == null) {
|
|
return {
|
|
tools: anthropicTools2,
|
|
tool_choice: void 0,
|
|
toolWarnings,
|
|
betas
|
|
};
|
|
}
|
|
const type = toolChoice.type;
|
|
switch (type) {
|
|
case "auto":
|
|
return {
|
|
tools: anthropicTools2,
|
|
tool_choice: { type: "auto" },
|
|
toolWarnings,
|
|
betas
|
|
};
|
|
case "required":
|
|
return {
|
|
tools: anthropicTools2,
|
|
tool_choice: { type: "any" },
|
|
toolWarnings,
|
|
betas
|
|
};
|
|
case "none":
|
|
return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
|
|
case "tool":
|
|
return {
|
|
tools: anthropicTools2,
|
|
tool_choice: { type: "tool", name: toolChoice.toolName },
|
|
toolWarnings,
|
|
betas
|
|
};
|
|
default: {
|
|
const _exhaustiveCheck = type;
|
|
throw new import_provider.UnsupportedFunctionalityError({
|
|
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// src/convert-to-anthropic-messages-prompt.ts
|
|
var import_provider2 = require("@ai-sdk/provider");
|
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
function convertToAnthropicMessagesPrompt({
|
|
prompt,
|
|
sendReasoning,
|
|
warnings
|
|
}) {
|
|
var _a, _b, _c, _d;
|
|
const betas = /* @__PURE__ */ new Set();
|
|
const blocks = groupIntoBlocks(prompt);
|
|
let system = void 0;
|
|
const messages = [];
|
|
function getCacheControl(providerMetadata) {
|
|
var _a2;
|
|
const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
const cacheControlValue = (_a2 = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a2 : anthropic == null ? void 0 : anthropic.cache_control;
|
|
return cacheControlValue;
|
|
}
|
|
for (let i = 0; i < blocks.length; i++) {
|
|
const block = blocks[i];
|
|
const isLastBlock = i === blocks.length - 1;
|
|
const type = block.type;
|
|
switch (type) {
|
|
case "system": {
|
|
if (system != null) {
|
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
functionality: "Multiple system messages that are separated by user/assistant messages"
|
|
});
|
|
}
|
|
system = block.messages.map(({ content, providerMetadata }) => ({
|
|
type: "text",
|
|
text: content,
|
|
cache_control: getCacheControl(providerMetadata)
|
|
}));
|
|
break;
|
|
}
|
|
case "user": {
|
|
const anthropicContent = [];
|
|
for (const message of block.messages) {
|
|
const { role, content } = message;
|
|
switch (role) {
|
|
case "user": {
|
|
for (let j = 0; j < content.length; j++) {
|
|
const part = content[j];
|
|
const isLastPart = j === content.length - 1;
|
|
const cacheControl = (_a = getCacheControl(part.providerMetadata)) != null ? _a : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
|
|
switch (part.type) {
|
|
case "text": {
|
|
anthropicContent.push({
|
|
type: "text",
|
|
text: part.text,
|
|
cache_control: cacheControl
|
|
});
|
|
break;
|
|
}
|
|
case "image": {
|
|
anthropicContent.push({
|
|
type: "image",
|
|
source: part.image instanceof URL ? {
|
|
type: "url",
|
|
url: part.image.toString()
|
|
} : {
|
|
type: "base64",
|
|
media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
|
data: (0, import_provider_utils2.convertUint8ArrayToBase64)(part.image)
|
|
},
|
|
cache_control: cacheControl
|
|
});
|
|
break;
|
|
}
|
|
case "file": {
|
|
if (part.mimeType !== "application/pdf") {
|
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
functionality: "Non-PDF files in user messages"
|
|
});
|
|
}
|
|
betas.add("pdfs-2024-09-25");
|
|
anthropicContent.push({
|
|
type: "document",
|
|
source: part.data instanceof URL ? {
|
|
type: "url",
|
|
url: part.data.toString()
|
|
} : {
|
|
type: "base64",
|
|
media_type: "application/pdf",
|
|
data: part.data
|
|
},
|
|
cache_control: cacheControl
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case "tool": {
|
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
const part = content[i2];
|
|
const isLastPart = i2 === content.length - 1;
|
|
const cacheControl = (_c = getCacheControl(part.providerMetadata)) != null ? _c : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
|
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
var _a2;
|
|
switch (part2.type) {
|
|
case "text":
|
|
return {
|
|
type: "text",
|
|
text: part2.text,
|
|
cache_control: void 0
|
|
};
|
|
case "image":
|
|
return {
|
|
type: "image",
|
|
source: {
|
|
type: "base64",
|
|
media_type: (_a2 = part2.mimeType) != null ? _a2 : "image/jpeg",
|
|
data: part2.data
|
|
},
|
|
cache_control: void 0
|
|
};
|
|
}
|
|
}) : JSON.stringify(part.result);
|
|
anthropicContent.push({
|
|
type: "tool_result",
|
|
tool_use_id: part.toolCallId,
|
|
content: toolResultContent,
|
|
is_error: part.isError,
|
|
cache_control: cacheControl
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
default: {
|
|
const _exhaustiveCheck = role;
|
|
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
}
|
|
}
|
|
}
|
|
messages.push({ role: "user", content: anthropicContent });
|
|
break;
|
|
}
|
|
case "assistant": {
|
|
const anthropicContent = [];
|
|
for (let j = 0; j < block.messages.length; j++) {
|
|
const message = block.messages[j];
|
|
const isLastMessage = j === block.messages.length - 1;
|
|
const { content } = message;
|
|
for (let k = 0; k < content.length; k++) {
|
|
const part = content[k];
|
|
const isLastContentPart = k === content.length - 1;
|
|
const cacheControl = (_d = getCacheControl(part.providerMetadata)) != null ? _d : isLastContentPart ? getCacheControl(message.providerMetadata) : void 0;
|
|
switch (part.type) {
|
|
case "text": {
|
|
anthropicContent.push({
|
|
type: "text",
|
|
text: (
|
|
// trim the last text part if it's the last message in the block
|
|
// because Anthropic does not allow trailing whitespace
|
|
// in pre-filled assistant responses
|
|
isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
|
|
),
|
|
cache_control: cacheControl
|
|
});
|
|
break;
|
|
}
|
|
case "reasoning": {
|
|
if (sendReasoning) {
|
|
anthropicContent.push({
|
|
type: "thinking",
|
|
thinking: part.text,
|
|
signature: part.signature,
|
|
cache_control: cacheControl
|
|
});
|
|
} else {
|
|
warnings.push({
|
|
type: "other",
|
|
message: "sending reasoning content is disabled for this model"
|
|
});
|
|
}
|
|
break;
|
|
}
|
|
case "redacted-reasoning": {
|
|
anthropicContent.push({
|
|
type: "redacted_thinking",
|
|
data: part.data,
|
|
cache_control: cacheControl
|
|
});
|
|
break;
|
|
}
|
|
case "tool-call": {
|
|
anthropicContent.push({
|
|
type: "tool_use",
|
|
id: part.toolCallId,
|
|
name: part.toolName,
|
|
input: part.args,
|
|
cache_control: cacheControl
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
messages.push({ role: "assistant", content: anthropicContent });
|
|
break;
|
|
}
|
|
default: {
|
|
const _exhaustiveCheck = type;
|
|
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
prompt: { system, messages },
|
|
betas
|
|
};
|
|
}
|
|
function groupIntoBlocks(prompt) {
|
|
const blocks = [];
|
|
let currentBlock = void 0;
|
|
for (const message of prompt) {
|
|
const { role } = message;
|
|
switch (role) {
|
|
case "system": {
|
|
if ((currentBlock == null ? void 0 : currentBlock.type) !== "system") {
|
|
currentBlock = { type: "system", messages: [] };
|
|
blocks.push(currentBlock);
|
|
}
|
|
currentBlock.messages.push(message);
|
|
break;
|
|
}
|
|
case "assistant": {
|
|
if ((currentBlock == null ? void 0 : currentBlock.type) !== "assistant") {
|
|
currentBlock = { type: "assistant", messages: [] };
|
|
blocks.push(currentBlock);
|
|
}
|
|
currentBlock.messages.push(message);
|
|
break;
|
|
}
|
|
case "user": {
|
|
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") {
|
|
currentBlock = { type: "user", messages: [] };
|
|
blocks.push(currentBlock);
|
|
}
|
|
currentBlock.messages.push(message);
|
|
break;
|
|
}
|
|
case "tool": {
|
|
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") {
|
|
currentBlock = { type: "user", messages: [] };
|
|
blocks.push(currentBlock);
|
|
}
|
|
currentBlock.messages.push(message);
|
|
break;
|
|
}
|
|
default: {
|
|
const _exhaustiveCheck = role;
|
|
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
}
|
|
}
|
|
}
|
|
return blocks;
|
|
}
|
|
|
|
// src/map-anthropic-stop-reason.ts
|
|
function mapAnthropicStopReason(finishReason) {
|
|
switch (finishReason) {
|
|
case "end_turn":
|
|
case "stop_sequence":
|
|
return "stop";
|
|
case "tool_use":
|
|
return "tool-calls";
|
|
case "max_tokens":
|
|
return "length";
|
|
default:
|
|
return "unknown";
|
|
}
|
|
}
|
|
|
|
// src/anthropic-messages-language-model.ts
|
|
var AnthropicMessagesLanguageModel = class {
|
|
constructor(modelId, settings, config) {
|
|
this.specificationVersion = "v1";
|
|
this.defaultObjectGenerationMode = "tool";
|
|
this.modelId = modelId;
|
|
this.settings = settings;
|
|
this.config = config;
|
|
}
|
|
supportsUrl(url) {
|
|
return url.protocol === "https:";
|
|
}
|
|
get provider() {
|
|
return this.config.provider;
|
|
}
|
|
get supportsImageUrls() {
|
|
return this.config.supportsImageUrls;
|
|
}
|
|
async getArgs({
|
|
mode,
|
|
prompt,
|
|
maxTokens = 4096,
|
|
// 4096: max model output tokens TODO update default in v5
|
|
temperature,
|
|
topP,
|
|
topK,
|
|
frequencyPenalty,
|
|
presencePenalty,
|
|
stopSequences,
|
|
responseFormat,
|
|
seed,
|
|
providerMetadata: providerOptions
|
|
}) {
|
|
var _a, _b, _c;
|
|
const type = mode.type;
|
|
const warnings = [];
|
|
if (frequencyPenalty != null) {
|
|
warnings.push({
|
|
type: "unsupported-setting",
|
|
setting: "frequencyPenalty"
|
|
});
|
|
}
|
|
if (presencePenalty != null) {
|
|
warnings.push({
|
|
type: "unsupported-setting",
|
|
setting: "presencePenalty"
|
|
});
|
|
}
|
|
if (seed != null) {
|
|
warnings.push({
|
|
type: "unsupported-setting",
|
|
setting: "seed"
|
|
});
|
|
}
|
|
if (responseFormat != null && responseFormat.type !== "text") {
|
|
warnings.push({
|
|
type: "unsupported-setting",
|
|
setting: "responseFormat",
|
|
details: "JSON response format is not supported."
|
|
});
|
|
}
|
|
const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
|
|
prompt,
|
|
sendReasoning: (_a = this.settings.sendReasoning) != null ? _a : true,
|
|
warnings
|
|
});
|
|
const anthropicOptions = (0, import_provider_utils3.parseProviderOptions)({
|
|
provider: "anthropic",
|
|
providerOptions,
|
|
schema: anthropicProviderOptionsSchema
|
|
});
|
|
const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
|
|
const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
|
|
const baseArgs = {
|
|
// model id:
|
|
model: this.modelId,
|
|
// standardized settings:
|
|
max_tokens: maxTokens,
|
|
temperature,
|
|
top_k: topK,
|
|
top_p: topP,
|
|
stop_sequences: stopSequences,
|
|
// provider specific settings:
|
|
...isThinking && {
|
|
thinking: { type: "enabled", budget_tokens: thinkingBudget }
|
|
},
|
|
// prompt:
|
|
system: messagesPrompt.system,
|
|
messages: messagesPrompt.messages
|
|
};
|
|
if (isThinking) {
|
|
if (thinkingBudget == null) {
|
|
throw new import_provider3.UnsupportedFunctionalityError({
|
|
functionality: "thinking requires a budget"
|
|
});
|
|
}
|
|
if (baseArgs.temperature != null) {
|
|
baseArgs.temperature = void 0;
|
|
warnings.push({
|
|
type: "unsupported-setting",
|
|
setting: "temperature",
|
|
details: "temperature is not supported when thinking is enabled"
|
|
});
|
|
}
|
|
if (topK != null) {
|
|
baseArgs.top_k = void 0;
|
|
warnings.push({
|
|
type: "unsupported-setting",
|
|
setting: "topK",
|
|
details: "topK is not supported when thinking is enabled"
|
|
});
|
|
}
|
|
if (topP != null) {
|
|
baseArgs.top_p = void 0;
|
|
warnings.push({
|
|
type: "unsupported-setting",
|
|
setting: "topP",
|
|
details: "topP is not supported when thinking is enabled"
|
|
});
|
|
}
|
|
baseArgs.max_tokens = maxTokens + thinkingBudget;
|
|
}
|
|
switch (type) {
|
|
case "regular": {
|
|
const {
|
|
tools,
|
|
tool_choice,
|
|
toolWarnings,
|
|
betas: toolsBetas
|
|
} = prepareTools(mode);
|
|
return {
|
|
args: { ...baseArgs, tools, tool_choice },
|
|
warnings: [...warnings, ...toolWarnings],
|
|
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
};
|
|
}
|
|
case "object-json": {
|
|
throw new import_provider3.UnsupportedFunctionalityError({
|
|
functionality: "json-mode object generation"
|
|
});
|
|
}
|
|
case "object-tool": {
|
|
const { name, description, parameters } = mode.tool;
|
|
return {
|
|
args: {
|
|
...baseArgs,
|
|
tools: [{ name, description, input_schema: parameters }],
|
|
tool_choice: { type: "tool", name }
|
|
},
|
|
warnings,
|
|
betas: messagesBetas
|
|
};
|
|
}
|
|
default: {
|
|
const _exhaustiveCheck = type;
|
|
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
}
|
|
}
|
|
}
|
|
async getHeaders({
|
|
betas,
|
|
headers
|
|
}) {
|
|
return (0, import_provider_utils3.combineHeaders)(
|
|
await (0, import_provider_utils3.resolve)(this.config.headers),
|
|
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
headers
|
|
);
|
|
}
|
|
buildRequestUrl(isStreaming) {
|
|
var _a, _b, _c;
|
|
return (_c = (_b = (_a = this.config).buildRequestUrl) == null ? void 0 : _b.call(_a, this.config.baseURL, isStreaming)) != null ? _c : `${this.config.baseURL}/messages`;
|
|
}
|
|
transformRequestBody(args) {
|
|
var _a, _b, _c;
|
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
}
|
|
async doGenerate(options) {
|
|
var _a, _b, _c, _d;
|
|
const { args, warnings, betas } = await this.getArgs(options);
|
|
const {
|
|
responseHeaders,
|
|
value: response,
|
|
rawValue: rawResponse
|
|
} = await (0, import_provider_utils3.postJsonToApi)({
|
|
url: this.buildRequestUrl(false),
|
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
body: this.transformRequestBody(args),
|
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
|
anthropicMessagesResponseSchema
|
|
),
|
|
abortSignal: options.abortSignal,
|
|
fetch: this.config.fetch
|
|
});
|
|
const { messages: rawPrompt, ...rawSettings } = args;
|
|
let text = "";
|
|
for (const content of response.content) {
|
|
if (content.type === "text") {
|
|
text += content.text;
|
|
}
|
|
}
|
|
let toolCalls = void 0;
|
|
if (response.content.some((content) => content.type === "tool_use")) {
|
|
toolCalls = [];
|
|
for (const content of response.content) {
|
|
if (content.type === "tool_use") {
|
|
toolCalls.push({
|
|
toolCallType: "function",
|
|
toolCallId: content.id,
|
|
toolName: content.name,
|
|
args: JSON.stringify(content.input)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
const reasoning = response.content.filter(
|
|
(content) => content.type === "redacted_thinking" || content.type === "thinking"
|
|
).map(
|
|
(content) => content.type === "thinking" ? {
|
|
type: "text",
|
|
text: content.thinking,
|
|
signature: content.signature
|
|
} : {
|
|
type: "redacted",
|
|
data: content.data
|
|
}
|
|
);
|
|
return {
|
|
text,
|
|
reasoning: reasoning.length > 0 ? reasoning : void 0,
|
|
toolCalls,
|
|
finishReason: mapAnthropicStopReason(response.stop_reason),
|
|
usage: {
|
|
promptTokens: response.usage.input_tokens,
|
|
completionTokens: response.usage.output_tokens
|
|
},
|
|
rawCall: { rawPrompt, rawSettings },
|
|
rawResponse: {
|
|
headers: responseHeaders,
|
|
body: rawResponse
|
|
},
|
|
response: {
|
|
id: (_a = response.id) != null ? _a : void 0,
|
|
modelId: (_b = response.model) != null ? _b : void 0
|
|
},
|
|
warnings,
|
|
providerMetadata: {
|
|
anthropic: {
|
|
cacheCreationInputTokens: (_c = response.usage.cache_creation_input_tokens) != null ? _c : null,
|
|
cacheReadInputTokens: (_d = response.usage.cache_read_input_tokens) != null ? _d : null
|
|
}
|
|
},
|
|
request: { body: JSON.stringify(args) }
|
|
};
|
|
}
|
|
async doStream(options) {
|
|
const { args, warnings, betas } = await this.getArgs(options);
|
|
const body = { ...args, stream: true };
|
|
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
url: this.buildRequestUrl(true),
|
|
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
body: this.transformRequestBody(body),
|
|
failedResponseHandler: anthropicFailedResponseHandler,
|
|
successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
|
|
anthropicMessagesChunkSchema
|
|
),
|
|
abortSignal: options.abortSignal,
|
|
fetch: this.config.fetch
|
|
});
|
|
const { messages: rawPrompt, ...rawSettings } = args;
|
|
let finishReason = "unknown";
|
|
const usage = {
|
|
promptTokens: Number.NaN,
|
|
completionTokens: Number.NaN
|
|
};
|
|
const toolCallContentBlocks = {};
|
|
let providerMetadata = void 0;
|
|
let blockType = void 0;
|
|
return {
|
|
stream: response.pipeThrough(
|
|
new TransformStream({
|
|
transform(chunk, controller) {
|
|
var _a, _b, _c, _d;
|
|
if (!chunk.success) {
|
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
return;
|
|
}
|
|
const value = chunk.value;
|
|
switch (value.type) {
|
|
case "ping": {
|
|
return;
|
|
}
|
|
case "content_block_start": {
|
|
const contentBlockType = value.content_block.type;
|
|
blockType = contentBlockType;
|
|
switch (contentBlockType) {
|
|
case "text":
|
|
case "thinking": {
|
|
return;
|
|
}
|
|
case "redacted_thinking": {
|
|
controller.enqueue({
|
|
type: "redacted-reasoning",
|
|
data: value.content_block.data
|
|
});
|
|
return;
|
|
}
|
|
case "tool_use": {
|
|
toolCallContentBlocks[value.index] = {
|
|
toolCallId: value.content_block.id,
|
|
toolName: value.content_block.name,
|
|
jsonText: ""
|
|
};
|
|
return;
|
|
}
|
|
default: {
|
|
const _exhaustiveCheck = contentBlockType;
|
|
throw new Error(
|
|
`Unsupported content block type: ${_exhaustiveCheck}`
|
|
);
|
|
}
|
|
}
|
|
}
|
|
case "content_block_stop": {
|
|
if (toolCallContentBlocks[value.index] != null) {
|
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
controller.enqueue({
|
|
type: "tool-call",
|
|
toolCallType: "function",
|
|
toolCallId: contentBlock.toolCallId,
|
|
toolName: contentBlock.toolName,
|
|
args: contentBlock.jsonText
|
|
});
|
|
delete toolCallContentBlocks[value.index];
|
|
}
|
|
blockType = void 0;
|
|
return;
|
|
}
|
|
case "content_block_delta": {
|
|
const deltaType = value.delta.type;
|
|
switch (deltaType) {
|
|
case "text_delta": {
|
|
controller.enqueue({
|
|
type: "text-delta",
|
|
textDelta: value.delta.text
|
|
});
|
|
return;
|
|
}
|
|
case "thinking_delta": {
|
|
controller.enqueue({
|
|
type: "reasoning",
|
|
textDelta: value.delta.thinking
|
|
});
|
|
return;
|
|
}
|
|
case "signature_delta": {
|
|
if (blockType === "thinking") {
|
|
controller.enqueue({
|
|
type: "reasoning-signature",
|
|
signature: value.delta.signature
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
case "input_json_delta": {
|
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
controller.enqueue({
|
|
type: "tool-call-delta",
|
|
toolCallType: "function",
|
|
toolCallId: contentBlock.toolCallId,
|
|
toolName: contentBlock.toolName,
|
|
argsTextDelta: value.delta.partial_json
|
|
});
|
|
contentBlock.jsonText += value.delta.partial_json;
|
|
return;
|
|
}
|
|
default: {
|
|
const _exhaustiveCheck = deltaType;
|
|
throw new Error(
|
|
`Unsupported delta type: ${_exhaustiveCheck}`
|
|
);
|
|
}
|
|
}
|
|
}
|
|
case "message_start": {
|
|
usage.promptTokens = value.message.usage.input_tokens;
|
|
usage.completionTokens = value.message.usage.output_tokens;
|
|
providerMetadata = {
|
|
anthropic: {
|
|
cacheCreationInputTokens: (_a = value.message.usage.cache_creation_input_tokens) != null ? _a : null,
|
|
cacheReadInputTokens: (_b = value.message.usage.cache_read_input_tokens) != null ? _b : null
|
|
}
|
|
};
|
|
controller.enqueue({
|
|
type: "response-metadata",
|
|
id: (_c = value.message.id) != null ? _c : void 0,
|
|
modelId: (_d = value.message.model) != null ? _d : void 0
|
|
});
|
|
return;
|
|
}
|
|
case "message_delta": {
|
|
usage.completionTokens = value.usage.output_tokens;
|
|
finishReason = mapAnthropicStopReason(value.delta.stop_reason);
|
|
return;
|
|
}
|
|
case "message_stop": {
|
|
controller.enqueue({
|
|
type: "finish",
|
|
finishReason,
|
|
usage,
|
|
providerMetadata
|
|
});
|
|
return;
|
|
}
|
|
case "error": {
|
|
controller.enqueue({ type: "error", error: value.error });
|
|
return;
|
|
}
|
|
default: {
|
|
const _exhaustiveCheck = value;
|
|
throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
|
|
}
|
|
}
|
|
}
|
|
})
|
|
),
|
|
rawCall: { rawPrompt, rawSettings },
|
|
rawResponse: { headers: responseHeaders },
|
|
warnings,
|
|
request: { body: JSON.stringify(body) }
|
|
};
|
|
}
|
|
};
|
|
var anthropicMessagesResponseSchema = import_zod2.z.object({
|
|
type: import_zod2.z.literal("message"),
|
|
id: import_zod2.z.string().nullish(),
|
|
model: import_zod2.z.string().nullish(),
|
|
content: import_zod2.z.array(
|
|
import_zod2.z.discriminatedUnion("type", [
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("text"),
|
|
text: import_zod2.z.string()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("thinking"),
|
|
thinking: import_zod2.z.string(),
|
|
signature: import_zod2.z.string()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("redacted_thinking"),
|
|
data: import_zod2.z.string()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("tool_use"),
|
|
id: import_zod2.z.string(),
|
|
name: import_zod2.z.string(),
|
|
input: import_zod2.z.unknown()
|
|
})
|
|
])
|
|
),
|
|
stop_reason: import_zod2.z.string().nullish(),
|
|
usage: import_zod2.z.object({
|
|
input_tokens: import_zod2.z.number(),
|
|
output_tokens: import_zod2.z.number(),
|
|
cache_creation_input_tokens: import_zod2.z.number().nullish(),
|
|
cache_read_input_tokens: import_zod2.z.number().nullish()
|
|
})
|
|
});
|
|
var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("message_start"),
|
|
message: import_zod2.z.object({
|
|
id: import_zod2.z.string().nullish(),
|
|
model: import_zod2.z.string().nullish(),
|
|
usage: import_zod2.z.object({
|
|
input_tokens: import_zod2.z.number(),
|
|
output_tokens: import_zod2.z.number(),
|
|
cache_creation_input_tokens: import_zod2.z.number().nullish(),
|
|
cache_read_input_tokens: import_zod2.z.number().nullish()
|
|
})
|
|
})
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("content_block_start"),
|
|
index: import_zod2.z.number(),
|
|
content_block: import_zod2.z.discriminatedUnion("type", [
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("text"),
|
|
text: import_zod2.z.string()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("thinking"),
|
|
thinking: import_zod2.z.string()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("tool_use"),
|
|
id: import_zod2.z.string(),
|
|
name: import_zod2.z.string()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("redacted_thinking"),
|
|
data: import_zod2.z.string()
|
|
})
|
|
])
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("content_block_delta"),
|
|
index: import_zod2.z.number(),
|
|
delta: import_zod2.z.discriminatedUnion("type", [
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("input_json_delta"),
|
|
partial_json: import_zod2.z.string()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("text_delta"),
|
|
text: import_zod2.z.string()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("thinking_delta"),
|
|
thinking: import_zod2.z.string()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("signature_delta"),
|
|
signature: import_zod2.z.string()
|
|
})
|
|
])
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("content_block_stop"),
|
|
index: import_zod2.z.number()
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("error"),
|
|
error: import_zod2.z.object({
|
|
type: import_zod2.z.string(),
|
|
message: import_zod2.z.string()
|
|
})
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("message_delta"),
|
|
delta: import_zod2.z.object({ stop_reason: import_zod2.z.string().nullish() }),
|
|
usage: import_zod2.z.object({ output_tokens: import_zod2.z.number() })
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("message_stop")
|
|
}),
|
|
import_zod2.z.object({
|
|
type: import_zod2.z.literal("ping")
|
|
})
|
|
]);
|
|
var anthropicProviderOptionsSchema = import_zod2.z.object({
|
|
thinking: import_zod2.z.object({
|
|
type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]),
|
|
budgetTokens: import_zod2.z.number().optional()
|
|
}).optional()
|
|
});
|
|
|
|
// src/anthropic-tools.ts
|
|
var import_zod3 = require("zod");
|
|
var Bash20241022Parameters = import_zod3.z.object({
|
|
command: import_zod3.z.string(),
|
|
restart: import_zod3.z.boolean().optional()
|
|
});
|
|
function bashTool_20241022(options = {}) {
|
|
return {
|
|
type: "provider-defined",
|
|
id: "anthropic.bash_20241022",
|
|
args: {},
|
|
parameters: Bash20241022Parameters,
|
|
execute: options.execute,
|
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
};
|
|
}
|
|
var Bash20250124Parameters = import_zod3.z.object({
|
|
command: import_zod3.z.string(),
|
|
restart: import_zod3.z.boolean().optional()
|
|
});
|
|
function bashTool_20250124(options = {}) {
|
|
return {
|
|
type: "provider-defined",
|
|
id: "anthropic.bash_20250124",
|
|
args: {},
|
|
parameters: Bash20250124Parameters,
|
|
execute: options.execute,
|
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
};
|
|
}
|
|
var TextEditor20241022Parameters = import_zod3.z.object({
|
|
command: import_zod3.z.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
path: import_zod3.z.string(),
|
|
file_text: import_zod3.z.string().optional(),
|
|
insert_line: import_zod3.z.number().int().optional(),
|
|
new_str: import_zod3.z.string().optional(),
|
|
old_str: import_zod3.z.string().optional(),
|
|
view_range: import_zod3.z.array(import_zod3.z.number().int()).optional()
|
|
});
|
|
function textEditorTool_20241022(options = {}) {
|
|
return {
|
|
type: "provider-defined",
|
|
id: "anthropic.text_editor_20241022",
|
|
args: {},
|
|
parameters: TextEditor20241022Parameters,
|
|
execute: options.execute,
|
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
};
|
|
}
|
|
var TextEditor20250124Parameters = import_zod3.z.object({
|
|
command: import_zod3.z.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
path: import_zod3.z.string(),
|
|
file_text: import_zod3.z.string().optional(),
|
|
insert_line: import_zod3.z.number().int().optional(),
|
|
new_str: import_zod3.z.string().optional(),
|
|
old_str: import_zod3.z.string().optional(),
|
|
view_range: import_zod3.z.array(import_zod3.z.number().int()).optional()
|
|
});
|
|
function textEditorTool_20250124(options = {}) {
|
|
return {
|
|
type: "provider-defined",
|
|
id: "anthropic.text_editor_20250124",
|
|
args: {},
|
|
parameters: TextEditor20250124Parameters,
|
|
execute: options.execute,
|
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
};
|
|
}
|
|
var Computer20241022Parameters = import_zod3.z.object({
|
|
action: import_zod3.z.enum([
|
|
"key",
|
|
"type",
|
|
"mouse_move",
|
|
"left_click",
|
|
"left_click_drag",
|
|
"right_click",
|
|
"middle_click",
|
|
"double_click",
|
|
"screenshot",
|
|
"cursor_position"
|
|
]),
|
|
coordinate: import_zod3.z.array(import_zod3.z.number().int()).optional(),
|
|
text: import_zod3.z.string().optional()
|
|
});
|
|
function computerTool_20241022(options) {
|
|
return {
|
|
type: "provider-defined",
|
|
id: "anthropic.computer_20241022",
|
|
args: {
|
|
displayWidthPx: options.displayWidthPx,
|
|
displayHeightPx: options.displayHeightPx,
|
|
displayNumber: options.displayNumber
|
|
},
|
|
parameters: Computer20241022Parameters,
|
|
execute: options.execute,
|
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
};
|
|
}
|
|
var Computer20250124Parameters = import_zod3.z.object({
|
|
action: import_zod3.z.enum([
|
|
"key",
|
|
"hold_key",
|
|
"type",
|
|
"cursor_position",
|
|
"mouse_move",
|
|
"left_mouse_down",
|
|
"left_mouse_up",
|
|
"left_click",
|
|
"left_click_drag",
|
|
"right_click",
|
|
"middle_click",
|
|
"double_click",
|
|
"triple_click",
|
|
"scroll",
|
|
"wait",
|
|
"screenshot"
|
|
]),
|
|
coordinate: import_zod3.z.tuple([import_zod3.z.number().int(), import_zod3.z.number().int()]).optional(),
|
|
duration: import_zod3.z.number().optional(),
|
|
scroll_amount: import_zod3.z.number().optional(),
|
|
scroll_direction: import_zod3.z.enum(["up", "down", "left", "right"]).optional(),
|
|
start_coordinate: import_zod3.z.tuple([import_zod3.z.number().int(), import_zod3.z.number().int()]).optional(),
|
|
text: import_zod3.z.string().optional()
|
|
});
|
|
function computerTool_20250124(options) {
|
|
return {
|
|
type: "provider-defined",
|
|
id: "anthropic.computer_20250124",
|
|
args: {
|
|
displayWidthPx: options.displayWidthPx,
|
|
displayHeightPx: options.displayHeightPx,
|
|
displayNumber: options.displayNumber
|
|
},
|
|
parameters: Computer20250124Parameters,
|
|
execute: options.execute,
|
|
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
};
|
|
}
|
|
var anthropicTools = {
|
|
bash_20241022: bashTool_20241022,
|
|
bash_20250124: bashTool_20250124,
|
|
textEditor_20241022: textEditorTool_20241022,
|
|
textEditor_20250124: textEditorTool_20250124,
|
|
computer_20241022: computerTool_20241022,
|
|
computer_20250124: computerTool_20250124
|
|
};
|
|
// Annotate the CommonJS export names for ESM import in node:
|
|
0 && (module.exports = {
|
|
AnthropicMessagesLanguageModel,
|
|
anthropicTools
|
|
});
|
|
//# sourceMappingURL=index.js.map
|