@ai-sdk/anthropic 3.0.78 → 3.0.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  } from "@ai-sdk/provider-utils";
13
13
 
14
14
  // src/version.ts
15
- var VERSION = true ? "3.0.78" : "0.0.0-test";
15
+ var VERSION = true ? "3.0.80" : "0.0.0-test";
16
16
 
17
17
  // src/anthropic-messages-language-model.ts
18
18
  import {
@@ -1853,6 +1853,7 @@ import {
1853
1853
  convertBase64ToUint8Array,
1854
1854
  convertToBase64,
1855
1855
  parseProviderOptions,
1856
+ safeParseJSON,
1856
1857
  validateTypes as validateTypes2,
1857
1858
  isNonNullable
1858
1859
  } from "@ai-sdk/provider-utils";
@@ -2204,6 +2205,21 @@ function isUrlString(data) {
2204
2205
  function getUrlString(data) {
2205
2206
  return data instanceof URL ? data.toString() : data;
2206
2207
  }
2208
+ async function extractErrorValue(value) {
2209
+ if (typeof value === "string") {
2210
+ const result = await safeParseJSON({ text: value });
2211
+ if (result.success && typeof result.value === "object" && result.value !== null) {
2212
+ return result.value;
2213
+ }
2214
+ return {
2215
+ errorCode: "unavailable"
2216
+ };
2217
+ }
2218
+ if (typeof value === "object" && value !== null) {
2219
+ return value;
2220
+ }
2221
+ return {};
2222
+ }
2207
2223
  async function convertToAnthropicMessagesPrompt({
2208
2224
  prompt,
2209
2225
  sendReasoning,
@@ -2821,25 +2837,12 @@ async function convertToAnthropicMessagesPrompt({
2821
2837
  if (providerToolName === "web_fetch") {
2822
2838
  const output = part.output;
2823
2839
  if (output.type === "error-json") {
2824
- let errorValue = {};
2825
- try {
2826
- if (typeof output.value === "string") {
2827
- errorValue = JSON.parse(output.value);
2828
- } else if (typeof output.value === "object" && output.value !== null) {
2829
- errorValue = output.value;
2830
- }
2831
- } catch (e) {
2832
- const extractedErrorCode = (_t = output.value) == null ? void 0 : _t.errorCode;
2833
- errorValue = {
2834
- errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unavailable"
2835
- };
2836
- }
2837
2840
  anthropicContent.push({
2838
2841
  type: "web_fetch_tool_result",
2839
2842
  tool_use_id: part.toolCallId,
2840
2843
  content: {
2841
2844
  type: "web_fetch_tool_result_error",
2842
- error_code: (_u = errorValue.errorCode) != null ? _u : "unavailable"
2845
+ error_code: (_t = (await extractErrorValue(output.value)).errorCode) != null ? _t : "unavailable"
2843
2846
  },
2844
2847
  cache_control: cacheControl
2845
2848
  });
@@ -2880,6 +2883,18 @@ async function convertToAnthropicMessagesPrompt({
2880
2883
  }
2881
2884
  if (providerToolName === "web_search") {
2882
2885
  const output = part.output;
2886
+ if (output.type === "error-json") {
2887
+ anthropicContent.push({
2888
+ type: "web_search_tool_result",
2889
+ tool_use_id: part.toolCallId,
2890
+ content: {
2891
+ type: "web_search_tool_result_error",
2892
+ error_code: (_u = (await extractErrorValue(output.value)).errorCode) != null ? _u : "unavailable"
2893
+ },
2894
+ cache_control: cacheControl
2895
+ });
2896
+ break;
2897
+ }
2883
2898
  if (output.type !== "json") {
2884
2899
  warnings.push({
2885
2900
  type: "other",
@@ -3890,12 +3905,18 @@ var AnthropicMessagesLanguageModel = class {
3890
3905
  }
3891
3906
  case "server_tool_use": {
3892
3907
  if (part.name === "text_editor_code_execution" || part.name === "bash_code_execution") {
3908
+ const providerToolName = "code_execution";
3893
3909
  content.push({
3894
3910
  type: "tool-call",
3895
3911
  toolCallId: part.id,
3896
3912
  toolName: toolNameMapping.toCustomToolName("code_execution"),
3897
3913
  input: JSON.stringify({ type: part.name, ...part.input }),
3898
- providerExecuted: true
3914
+ providerExecuted: true,
3915
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
3916
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
3917
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
3918
+ // tool as dynamic.
3919
+ ...markCodeExecutionDynamic && providerToolName === "code_execution" ? { dynamic: true } : {}
3899
3920
  });
3900
3921
  } else if (part.name === "web_search" || part.name === "code_execution" || part.name === "web_fetch") {
3901
3922
  const inputToSerialize = part.name === "code_execution" && part.input != null && typeof part.input === "object" && "code" in part.input && !("type" in part.input) ? { type: "programmatic-tool-call", ...part.input } : part.input;
@@ -3905,8 +3926,10 @@ var AnthropicMessagesLanguageModel = class {
3905
3926
  toolName: toolNameMapping.toCustomToolName(part.name),
3906
3927
  input: JSON.stringify(inputToSerialize),
3907
3928
  providerExecuted: true,
3908
- // We want this 'code_execution' tool call to be allowed even if the tool is not explicitly provided.
3909
- // Since the validation generally bypasses dynamic tools, we mark this specific tool as dynamic.
3929
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
3930
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
3931
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
3932
+ // tool as dynamic.
3910
3933
  ...markCodeExecutionDynamic && part.name === "code_execution" ? { dynamic: true } : {}
3911
3934
  });
3912
3935
  } else if (part.name === "tool_search_tool_regex" || part.name === "tool_search_tool_bm25") {
@@ -4426,15 +4449,23 @@ var AnthropicMessagesLanguageModel = class {
4426
4449
  toolName: customToolName,
4427
4450
  input: finalInput,
4428
4451
  providerExecuted: true,
4452
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
4453
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
4454
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
4455
+ // tool as dynamic.
4429
4456
  ...markCodeExecutionDynamic && providerToolName === "code_execution" ? { dynamic: true } : {},
4430
4457
  firstDelta: true,
4431
- providerToolName: part.name
4458
+ providerToolName
4432
4459
  };
4433
4460
  controller.enqueue({
4434
4461
  type: "tool-input-start",
4435
4462
  id: part.id,
4436
4463
  toolName: customToolName,
4437
4464
  providerExecuted: true,
4465
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
4466
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
4467
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
4468
+ // tool as dynamic.
4438
4469
  ...markCodeExecutionDynamic && providerToolName === "code_execution" ? { dynamic: true } : {}
4439
4470
  });
4440
4471
  } else if (part.name === "tool_search_tool_regex" || part.name === "tool_search_tool_bm25") {
@@ -4777,6 +4808,10 @@ var AnthropicMessagesLanguageModel = class {
4777
4808
  toolName: contentBlock.toolName,
4778
4809
  input: finalInput,
4779
4810
  providerExecuted: contentBlock.providerExecuted,
4811
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
4812
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
4813
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
4814
+ // tool as dynamic.
4780
4815
  ...markCodeExecutionDynamic && contentBlock.providerToolName === "code_execution" ? { dynamic: true } : {},
4781
4816
  ...contentBlock.caller && {
4782
4817
  providerMetadata: {
@@ -4860,8 +4895,8 @@ var AnthropicMessagesLanguageModel = class {
4860
4895
  if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
4861
4896
  return;
4862
4897
  }
4863
- if (contentBlock.firstDelta && (contentBlock.providerToolName === "bash_code_execution" || contentBlock.providerToolName === "text_editor_code_execution")) {
4864
- delta = `{"type": "${contentBlock.providerToolName}",${delta.substring(1)}`;
4898
+ if (contentBlock.firstDelta && contentBlock.providerToolName === "code_execution") {
4899
+ delta = `{"type": "programmatic-tool-call",${delta.substring(1)}`;
4865
4900
  }
4866
4901
  controller.enqueue({
4867
4902
  type: "tool-input-delta",