@ai-sdk/anthropic
Advanced tools
Comparing version 0.0.53 to 0.0.54
@@ -33,3 +33,3 @@ "use strict"; | ||
// src/anthropic-messages-language-model.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
@@ -61,2 +61,3 @@ var import_zod2 = require("zod"); | ||
var _a, _b, _c, _d; | ||
const betas = /* @__PURE__ */ new Set(); | ||
const blocks = groupIntoBlocks(prompt); | ||
@@ -127,2 +128,25 @@ let system = void 0; | ||
} | ||
case "file": { | ||
if (part.data instanceof URL) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Image URLs in user messages" | ||
}); | ||
} | ||
if (part.mimeType !== "application/pdf") { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Non-PDF files in user messages" | ||
}); | ||
} | ||
betas.add("pdfs-2024-09-25"); | ||
anthropicContent.push({ | ||
type: "document", | ||
source: { | ||
type: "base64", | ||
media_type: "application/pdf", | ||
data: part.data | ||
}, | ||
cache_control: cacheControl | ||
}); | ||
break; | ||
} | ||
} | ||
@@ -222,4 +246,4 @@ } | ||
return { | ||
system, | ||
messages | ||
prompt: { system, messages }, | ||
betas | ||
}; | ||
@@ -290,2 +314,3 @@ } | ||
// src/anthropic-prepare-tools.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
function prepareTools(mode) { | ||
@@ -295,4 +320,5 @@ var _a; | ||
const toolWarnings = []; | ||
const betas = /* @__PURE__ */ new Set(); | ||
if (tools == null) { | ||
return { tools: void 0, tool_choice: void 0, toolWarnings }; | ||
return { tools: void 0, tool_choice: void 0, toolWarnings, betas }; | ||
} | ||
@@ -310,2 +336,3 @@ const anthropicTools2 = []; | ||
case "provider-defined": | ||
betas.add("computer-use-2024-10-22"); | ||
switch (tool.id) { | ||
@@ -345,3 +372,8 @@ case "anthropic.computer_20241022": | ||
if (toolChoice == null) { | ||
return { tools: anthropicTools2, tool_choice: void 0, toolWarnings }; | ||
return { | ||
tools: anthropicTools2, | ||
tool_choice: void 0, | ||
toolWarnings, | ||
betas | ||
}; | ||
} | ||
@@ -354,3 +386,4 @@ const type = toolChoice.type; | ||
tool_choice: { type: "auto" }, | ||
toolWarnings | ||
toolWarnings, | ||
betas | ||
}; | ||
@@ -361,6 +394,7 @@ case "required": | ||
tool_choice: { type: "any" }, | ||
toolWarnings | ||
toolWarnings, | ||
betas | ||
}; | ||
case "none": | ||
return { tools: void 0, tool_choice: void 0, toolWarnings }; | ||
return { tools: void 0, tool_choice: void 0, toolWarnings, betas }; | ||
case "tool": | ||
@@ -370,7 +404,10 @@ return { | ||
tool_choice: { type: "tool", name: toolChoice.toolName }, | ||
toolWarnings | ||
toolWarnings, | ||
betas | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`); | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}` | ||
}); | ||
} | ||
@@ -434,3 +471,3 @@ } | ||
} | ||
const messagesPrompt = convertToAnthropicMessagesPrompt({ | ||
const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({ | ||
prompt, | ||
@@ -456,10 +493,16 @@ cacheControl: (_a = this.settings.cacheControl) != null ? _a : false | ||
case "regular": { | ||
const { tools, tool_choice, toolWarnings } = prepareTools(mode); | ||
const { | ||
tools, | ||
tool_choice, | ||
toolWarnings, | ||
betas: toolsBetas | ||
} = prepareTools(mode); | ||
return { | ||
args: { ...baseArgs, tools, tool_choice }, | ||
warnings: [...warnings, ...toolWarnings] | ||
warnings: [...warnings, ...toolWarnings], | ||
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]) | ||
}; | ||
} | ||
case "object-json": { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "json-mode object generation" | ||
@@ -476,3 +519,4 @@ }); | ||
}, | ||
warnings | ||
warnings, | ||
betas: messagesBetas | ||
}; | ||
@@ -486,7 +530,13 @@ } | ||
} | ||
getHeaders(optionHeaders) { | ||
getHeaders({ | ||
betas, | ||
headers | ||
}) { | ||
if (this.settings.cacheControl) { | ||
betas.add("prompt-caching-2024-07-31"); | ||
} | ||
return (0, import_provider_utils3.combineHeaders)( | ||
this.config.headers(), | ||
this.settings.cacheControl ? { "anthropic-beta": "prompt-caching-2024-07-31" } : {}, | ||
optionHeaders | ||
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {}, | ||
headers | ||
); | ||
@@ -496,6 +546,6 @@ } | ||
var _a, _b, _c, _d; | ||
const { args, warnings } = await this.getArgs(options); | ||
const { args, warnings, betas } = await this.getArgs(options); | ||
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({ | ||
url: `${this.config.baseURL}/messages`, | ||
headers: this.getHeaders(options.headers), | ||
headers: this.getHeaders({ betas, headers: options.headers }), | ||
body: args, | ||
@@ -555,7 +605,7 @@ failedResponseHandler: anthropicFailedResponseHandler, | ||
async doStream(options) { | ||
const { args, warnings } = await this.getArgs(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.config.baseURL}/messages`, | ||
headers: this.getHeaders(options.headers), | ||
headers: this.getHeaders({ betas, headers: options.headers }), | ||
body, | ||
@@ -842,3 +892,3 @@ failedResponseHandler: anthropicFailedResponseHandler, | ||
// src/anthropic-provider.ts | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
var import_provider_utils5 = require("@ai-sdk/provider-utils"); | ||
@@ -923,3 +973,2 @@ | ||
"anthropic-version": "2023-06-01", | ||
"anthropic-beta": "computer-use-2024-10-22", | ||
"x-api-key": (0, import_provider_utils5.loadApiKey)({ | ||
@@ -950,3 +999,3 @@ apiKey: options.apiKey, | ||
provider.textEmbeddingModel = (modelId) => { | ||
throw new import_provider3.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" }); | ||
throw new import_provider4.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" }); | ||
}; | ||
@@ -953,0 +1002,0 @@ provider.tools = anthropicTools; |
# @ai-sdk/anthropic | ||
## 0.0.54 | ||
### Patch Changes | ||
- 4d2e53b: feat (provider/anthropic): pdf support | ||
- c8afcb5: feat (provider/anthropic): allow using computer use and cache control at the same time | ||
## 0.0.53 | ||
@@ -4,0 +11,0 @@ |
@@ -33,3 +33,3 @@ "use strict"; | ||
// src/anthropic-messages-language-model.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider_utils3 = require("@ai-sdk/provider-utils"); | ||
@@ -61,2 +61,3 @@ var import_zod2 = require("zod"); | ||
var _a, _b, _c, _d; | ||
const betas = /* @__PURE__ */ new Set(); | ||
const blocks = groupIntoBlocks(prompt); | ||
@@ -127,2 +128,25 @@ let system = void 0; | ||
} | ||
case "file": { | ||
if (part.data instanceof URL) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Image URLs in user messages" | ||
}); | ||
} | ||
if (part.mimeType !== "application/pdf") { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Non-PDF files in user messages" | ||
}); | ||
} | ||
betas.add("pdfs-2024-09-25"); | ||
anthropicContent.push({ | ||
type: "document", | ||
source: { | ||
type: "base64", | ||
media_type: "application/pdf", | ||
data: part.data | ||
}, | ||
cache_control: cacheControl | ||
}); | ||
break; | ||
} | ||
} | ||
@@ -222,4 +246,4 @@ } | ||
return { | ||
system, | ||
messages | ||
prompt: { system, messages }, | ||
betas | ||
}; | ||
@@ -290,2 +314,3 @@ } | ||
// src/anthropic-prepare-tools.ts | ||
var import_provider2 = require("@ai-sdk/provider"); | ||
function prepareTools(mode) { | ||
@@ -295,4 +320,5 @@ var _a; | ||
const toolWarnings = []; | ||
const betas = /* @__PURE__ */ new Set(); | ||
if (tools == null) { | ||
return { tools: void 0, tool_choice: void 0, toolWarnings }; | ||
return { tools: void 0, tool_choice: void 0, toolWarnings, betas }; | ||
} | ||
@@ -310,2 +336,3 @@ const anthropicTools2 = []; | ||
case "provider-defined": | ||
betas.add("computer-use-2024-10-22"); | ||
switch (tool.id) { | ||
@@ -345,3 +372,8 @@ case "anthropic.computer_20241022": | ||
if (toolChoice == null) { | ||
return { tools: anthropicTools2, tool_choice: void 0, toolWarnings }; | ||
return { | ||
tools: anthropicTools2, | ||
tool_choice: void 0, | ||
toolWarnings, | ||
betas | ||
}; | ||
} | ||
@@ -354,3 +386,4 @@ const type = toolChoice.type; | ||
tool_choice: { type: "auto" }, | ||
toolWarnings | ||
toolWarnings, | ||
betas | ||
}; | ||
@@ -361,6 +394,7 @@ case "required": | ||
tool_choice: { type: "any" }, | ||
toolWarnings | ||
toolWarnings, | ||
betas | ||
}; | ||
case "none": | ||
return { tools: void 0, tool_choice: void 0, toolWarnings }; | ||
return { tools: void 0, tool_choice: void 0, toolWarnings, betas }; | ||
case "tool": | ||
@@ -370,7 +404,10 @@ return { | ||
tool_choice: { type: "tool", name: toolChoice.toolName }, | ||
toolWarnings | ||
toolWarnings, | ||
betas | ||
}; | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported tool choice type: ${_exhaustiveCheck}`); | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}` | ||
}); | ||
} | ||
@@ -434,3 +471,3 @@ } | ||
} | ||
const messagesPrompt = convertToAnthropicMessagesPrompt({ | ||
const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({ | ||
prompt, | ||
@@ -456,10 +493,16 @@ cacheControl: (_a = this.settings.cacheControl) != null ? _a : false | ||
case "regular": { | ||
const { tools, tool_choice, toolWarnings } = prepareTools(mode); | ||
const { | ||
tools, | ||
tool_choice, | ||
toolWarnings, | ||
betas: toolsBetas | ||
} = prepareTools(mode); | ||
return { | ||
args: { ...baseArgs, tools, tool_choice }, | ||
warnings: [...warnings, ...toolWarnings] | ||
warnings: [...warnings, ...toolWarnings], | ||
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]) | ||
}; | ||
} | ||
case "object-json": { | ||
throw new import_provider2.UnsupportedFunctionalityError({ | ||
throw new import_provider3.UnsupportedFunctionalityError({ | ||
functionality: "json-mode object generation" | ||
@@ -476,3 +519,4 @@ }); | ||
}, | ||
warnings | ||
warnings, | ||
betas: messagesBetas | ||
}; | ||
@@ -486,7 +530,13 @@ } | ||
} | ||
getHeaders(optionHeaders) { | ||
getHeaders({ | ||
betas, | ||
headers | ||
}) { | ||
if (this.settings.cacheControl) { | ||
betas.add("prompt-caching-2024-07-31"); | ||
} | ||
return (0, import_provider_utils3.combineHeaders)( | ||
this.config.headers(), | ||
this.settings.cacheControl ? { "anthropic-beta": "prompt-caching-2024-07-31" } : {}, | ||
optionHeaders | ||
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {}, | ||
headers | ||
); | ||
@@ -496,6 +546,6 @@ } | ||
var _a, _b, _c, _d; | ||
const { args, warnings } = await this.getArgs(options); | ||
const { args, warnings, betas } = await this.getArgs(options); | ||
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({ | ||
url: `${this.config.baseURL}/messages`, | ||
headers: this.getHeaders(options.headers), | ||
headers: this.getHeaders({ betas, headers: options.headers }), | ||
body: args, | ||
@@ -555,7 +605,7 @@ failedResponseHandler: anthropicFailedResponseHandler, | ||
async doStream(options) { | ||
const { args, warnings } = await this.getArgs(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.config.baseURL}/messages`, | ||
headers: this.getHeaders(options.headers), | ||
headers: this.getHeaders({ betas, headers: options.headers }), | ||
body, | ||
@@ -842,3 +892,3 @@ failedResponseHandler: anthropicFailedResponseHandler, | ||
// src/anthropic-provider.ts | ||
var import_provider3 = require("@ai-sdk/provider"); | ||
var import_provider4 = require("@ai-sdk/provider"); | ||
var import_provider_utils5 = require("@ai-sdk/provider-utils"); | ||
@@ -923,3 +973,2 @@ | ||
"anthropic-version": "2023-06-01", | ||
"anthropic-beta": "computer-use-2024-10-22", | ||
"x-api-key": (0, import_provider_utils5.loadApiKey)({ | ||
@@ -950,3 +999,3 @@ apiKey: options.apiKey, | ||
provider.textEmbeddingModel = (modelId) => { | ||
throw new import_provider3.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" }); | ||
throw new import_provider4.NoSuchModelError({ modelId, modelType: "textEmbeddingModel" }); | ||
}; | ||
@@ -953,0 +1002,0 @@ provider.tools = anthropicTools; |
{ | ||
"name": "@ai-sdk/anthropic", | ||
"version": "0.0.53", | ||
"version": "0.0.54", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
266236
3230