@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.
@@ -1837,6 +1837,7 @@ import {
1837
1837
  convertBase64ToUint8Array,
1838
1838
  convertToBase64,
1839
1839
  parseProviderOptions,
1840
+ safeParseJSON,
1840
1841
  validateTypes as validateTypes2,
1841
1842
  isNonNullable
1842
1843
  } from "@ai-sdk/provider-utils";
@@ -2188,6 +2189,21 @@ function isUrlString(data) {
2188
2189
  function getUrlString(data) {
2189
2190
  return data instanceof URL ? data.toString() : data;
2190
2191
  }
2192
+ async function extractErrorValue(value) {
2193
+ if (typeof value === "string") {
2194
+ const result = await safeParseJSON({ text: value });
2195
+ if (result.success && typeof result.value === "object" && result.value !== null) {
2196
+ return result.value;
2197
+ }
2198
+ return {
2199
+ errorCode: "unavailable"
2200
+ };
2201
+ }
2202
+ if (typeof value === "object" && value !== null) {
2203
+ return value;
2204
+ }
2205
+ return {};
2206
+ }
2191
2207
  async function convertToAnthropicMessagesPrompt({
2192
2208
  prompt,
2193
2209
  sendReasoning,
@@ -2805,25 +2821,12 @@ async function convertToAnthropicMessagesPrompt({
2805
2821
  if (providerToolName === "web_fetch") {
2806
2822
  const output = part.output;
2807
2823
  if (output.type === "error-json") {
2808
- let errorValue = {};
2809
- try {
2810
- if (typeof output.value === "string") {
2811
- errorValue = JSON.parse(output.value);
2812
- } else if (typeof output.value === "object" && output.value !== null) {
2813
- errorValue = output.value;
2814
- }
2815
- } catch (e) {
2816
- const extractedErrorCode = (_t = output.value) == null ? void 0 : _t.errorCode;
2817
- errorValue = {
2818
- errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unavailable"
2819
- };
2820
- }
2821
2824
  anthropicContent.push({
2822
2825
  type: "web_fetch_tool_result",
2823
2826
  tool_use_id: part.toolCallId,
2824
2827
  content: {
2825
2828
  type: "web_fetch_tool_result_error",
2826
- error_code: (_u = errorValue.errorCode) != null ? _u : "unavailable"
2829
+ error_code: (_t = (await extractErrorValue(output.value)).errorCode) != null ? _t : "unavailable"
2827
2830
  },
2828
2831
  cache_control: cacheControl
2829
2832
  });
@@ -2864,6 +2867,18 @@ async function convertToAnthropicMessagesPrompt({
2864
2867
  }
2865
2868
  if (providerToolName === "web_search") {
2866
2869
  const output = part.output;
2870
+ if (output.type === "error-json") {
2871
+ anthropicContent.push({
2872
+ type: "web_search_tool_result",
2873
+ tool_use_id: part.toolCallId,
2874
+ content: {
2875
+ type: "web_search_tool_result_error",
2876
+ error_code: (_u = (await extractErrorValue(output.value)).errorCode) != null ? _u : "unavailable"
2877
+ },
2878
+ cache_control: cacheControl
2879
+ });
2880
+ break;
2881
+ }
2867
2882
  if (output.type !== "json") {
2868
2883
  warnings.push({
2869
2884
  type: "other",
@@ -3874,12 +3889,18 @@ var AnthropicMessagesLanguageModel = class {
3874
3889
  }
3875
3890
  case "server_tool_use": {
3876
3891
  if (part.name === "text_editor_code_execution" || part.name === "bash_code_execution") {
3892
+ const providerToolName = "code_execution";
3877
3893
  content.push({
3878
3894
  type: "tool-call",
3879
3895
  toolCallId: part.id,
3880
3896
  toolName: toolNameMapping.toCustomToolName("code_execution"),
3881
3897
  input: JSON.stringify({ type: part.name, ...part.input }),
3882
- providerExecuted: true
3898
+ providerExecuted: true,
3899
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
3900
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
3901
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
3902
+ // tool as dynamic.
3903
+ ...markCodeExecutionDynamic && providerToolName === "code_execution" ? { dynamic: true } : {}
3883
3904
  });
3884
3905
  } else if (part.name === "web_search" || part.name === "code_execution" || part.name === "web_fetch") {
3885
3906
  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;
@@ -3889,8 +3910,10 @@ var AnthropicMessagesLanguageModel = class {
3889
3910
  toolName: toolNameMapping.toCustomToolName(part.name),
3890
3911
  input: JSON.stringify(inputToSerialize),
3891
3912
  providerExecuted: true,
3892
- // We want this 'code_execution' tool call to be allowed even if the tool is not explicitly provided.
3893
- // Since the validation generally bypasses dynamic tools, we mark this specific tool as dynamic.
3913
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
3914
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
3915
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
3916
+ // tool as dynamic.
3894
3917
  ...markCodeExecutionDynamic && part.name === "code_execution" ? { dynamic: true } : {}
3895
3918
  });
3896
3919
  } else if (part.name === "tool_search_tool_regex" || part.name === "tool_search_tool_bm25") {
@@ -4410,15 +4433,23 @@ var AnthropicMessagesLanguageModel = class {
4410
4433
  toolName: customToolName,
4411
4434
  input: finalInput,
4412
4435
  providerExecuted: true,
4436
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
4437
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
4438
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
4439
+ // tool as dynamic.
4413
4440
  ...markCodeExecutionDynamic && providerToolName === "code_execution" ? { dynamic: true } : {},
4414
4441
  firstDelta: true,
4415
- providerToolName: part.name
4442
+ providerToolName
4416
4443
  };
4417
4444
  controller.enqueue({
4418
4445
  type: "tool-input-start",
4419
4446
  id: part.id,
4420
4447
  toolName: customToolName,
4421
4448
  providerExecuted: true,
4449
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
4450
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
4451
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
4452
+ // tool as dynamic.
4422
4453
  ...markCodeExecutionDynamic && providerToolName === "code_execution" ? { dynamic: true } : {}
4423
4454
  });
4424
4455
  } else if (part.name === "tool_search_tool_regex" || part.name === "tool_search_tool_bm25") {
@@ -4761,6 +4792,10 @@ var AnthropicMessagesLanguageModel = class {
4761
4792
  toolName: contentBlock.toolName,
4762
4793
  input: finalInput,
4763
4794
  providerExecuted: contentBlock.providerExecuted,
4795
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
4796
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
4797
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
4798
+ // tool as dynamic.
4764
4799
  ...markCodeExecutionDynamic && contentBlock.providerToolName === "code_execution" ? { dynamic: true } : {},
4765
4800
  ...contentBlock.caller && {
4766
4801
  providerMetadata: {
@@ -4844,8 +4879,8 @@ var AnthropicMessagesLanguageModel = class {
4844
4879
  if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
4845
4880
  return;
4846
4881
  }
4847
- if (contentBlock.firstDelta && (contentBlock.providerToolName === "bash_code_execution" || contentBlock.providerToolName === "text_editor_code_execution")) {
4848
- delta = `{"type": "${contentBlock.providerToolName}",${delta.substring(1)}`;
4882
+ if (contentBlock.firstDelta && contentBlock.providerToolName === "code_execution") {
4883
+ delta = `{"type": "programmatic-tool-call",${delta.substring(1)}`;
4849
4884
  }
4850
4885
  controller.enqueue({
4851
4886
  type: "tool-input-delta",