@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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 3.0.80
4
+
5
+ ### Patch Changes
6
+
7
+ - 263d3e6: fix(provider/anthropic): fix remaining errors with Anthropic `code_execution` tool dynamic calls from latest `web_fetch` or `web_search`
8
+
9
+ ## 3.0.79
10
+
11
+ ### Patch Changes
12
+
13
+ - d61a788: Handle errors from anthropic websearch tool
14
+
3
15
  ## 3.0.78
4
16
 
5
17
  ### Patch Changes
package/dist/index.js CHANGED
@@ -32,7 +32,7 @@ var import_provider4 = require("@ai-sdk/provider");
32
32
  var import_provider_utils26 = require("@ai-sdk/provider-utils");
33
33
 
34
34
  // src/version.ts
35
- var VERSION = true ? "3.0.78" : "0.0.0-test";
35
+ var VERSION = true ? "3.0.80" : "0.0.0-test";
36
36
 
37
37
  // src/anthropic-messages-language-model.ts
38
38
  var import_provider3 = require("@ai-sdk/provider");
@@ -2156,6 +2156,21 @@ function isUrlString(data) {
2156
2156
  function getUrlString(data) {
2157
2157
  return data instanceof URL ? data.toString() : data;
2158
2158
  }
2159
+ async function extractErrorValue(value) {
2160
+ if (typeof value === "string") {
2161
+ const result = await (0, import_provider_utils14.safeParseJSON)({ text: value });
2162
+ if (result.success && typeof result.value === "object" && result.value !== null) {
2163
+ return result.value;
2164
+ }
2165
+ return {
2166
+ errorCode: "unavailable"
2167
+ };
2168
+ }
2169
+ if (typeof value === "object" && value !== null) {
2170
+ return value;
2171
+ }
2172
+ return {};
2173
+ }
2159
2174
  async function convertToAnthropicMessagesPrompt({
2160
2175
  prompt,
2161
2176
  sendReasoning,
@@ -2773,25 +2788,12 @@ async function convertToAnthropicMessagesPrompt({
2773
2788
  if (providerToolName === "web_fetch") {
2774
2789
  const output = part.output;
2775
2790
  if (output.type === "error-json") {
2776
- let errorValue = {};
2777
- try {
2778
- if (typeof output.value === "string") {
2779
- errorValue = JSON.parse(output.value);
2780
- } else if (typeof output.value === "object" && output.value !== null) {
2781
- errorValue = output.value;
2782
- }
2783
- } catch (e) {
2784
- const extractedErrorCode = (_t = output.value) == null ? void 0 : _t.errorCode;
2785
- errorValue = {
2786
- errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unavailable"
2787
- };
2788
- }
2789
2791
  anthropicContent.push({
2790
2792
  type: "web_fetch_tool_result",
2791
2793
  tool_use_id: part.toolCallId,
2792
2794
  content: {
2793
2795
  type: "web_fetch_tool_result_error",
2794
- error_code: (_u = errorValue.errorCode) != null ? _u : "unavailable"
2796
+ error_code: (_t = (await extractErrorValue(output.value)).errorCode) != null ? _t : "unavailable"
2795
2797
  },
2796
2798
  cache_control: cacheControl
2797
2799
  });
@@ -2832,6 +2834,18 @@ async function convertToAnthropicMessagesPrompt({
2832
2834
  }
2833
2835
  if (providerToolName === "web_search") {
2834
2836
  const output = part.output;
2837
+ if (output.type === "error-json") {
2838
+ anthropicContent.push({
2839
+ type: "web_search_tool_result",
2840
+ tool_use_id: part.toolCallId,
2841
+ content: {
2842
+ type: "web_search_tool_result_error",
2843
+ error_code: (_u = (await extractErrorValue(output.value)).errorCode) != null ? _u : "unavailable"
2844
+ },
2845
+ cache_control: cacheControl
2846
+ });
2847
+ break;
2848
+ }
2835
2849
  if (output.type !== "json") {
2836
2850
  warnings.push({
2837
2851
  type: "other",
@@ -3842,12 +3856,18 @@ var AnthropicMessagesLanguageModel = class {
3842
3856
  }
3843
3857
  case "server_tool_use": {
3844
3858
  if (part.name === "text_editor_code_execution" || part.name === "bash_code_execution") {
3859
+ const providerToolName = "code_execution";
3845
3860
  content.push({
3846
3861
  type: "tool-call",
3847
3862
  toolCallId: part.id,
3848
3863
  toolName: toolNameMapping.toCustomToolName("code_execution"),
3849
3864
  input: JSON.stringify({ type: part.name, ...part.input }),
3850
- providerExecuted: true
3865
+ providerExecuted: true,
3866
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
3867
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
3868
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
3869
+ // tool as dynamic.
3870
+ ...markCodeExecutionDynamic && providerToolName === "code_execution" ? { dynamic: true } : {}
3851
3871
  });
3852
3872
  } else if (part.name === "web_search" || part.name === "code_execution" || part.name === "web_fetch") {
3853
3873
  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;
@@ -3857,8 +3877,10 @@ var AnthropicMessagesLanguageModel = class {
3857
3877
  toolName: toolNameMapping.toCustomToolName(part.name),
3858
3878
  input: JSON.stringify(inputToSerialize),
3859
3879
  providerExecuted: true,
3860
- // We want this 'code_execution' tool call to be allowed even if the tool is not explicitly provided.
3861
- // Since the validation generally bypasses dynamic tools, we mark this specific tool as dynamic.
3880
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
3881
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
3882
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
3883
+ // tool as dynamic.
3862
3884
  ...markCodeExecutionDynamic && part.name === "code_execution" ? { dynamic: true } : {}
3863
3885
  });
3864
3886
  } else if (part.name === "tool_search_tool_regex" || part.name === "tool_search_tool_bm25") {
@@ -4378,15 +4400,23 @@ var AnthropicMessagesLanguageModel = class {
4378
4400
  toolName: customToolName,
4379
4401
  input: finalInput,
4380
4402
  providerExecuted: true,
4403
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
4404
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
4405
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
4406
+ // tool as dynamic.
4381
4407
  ...markCodeExecutionDynamic && providerToolName === "code_execution" ? { dynamic: true } : {},
4382
4408
  firstDelta: true,
4383
- providerToolName: part.name
4409
+ providerToolName
4384
4410
  };
4385
4411
  controller.enqueue({
4386
4412
  type: "tool-input-start",
4387
4413
  id: part.id,
4388
4414
  toolName: customToolName,
4389
4415
  providerExecuted: true,
4416
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
4417
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
4418
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
4419
+ // tool as dynamic.
4390
4420
  ...markCodeExecutionDynamic && providerToolName === "code_execution" ? { dynamic: true } : {}
4391
4421
  });
4392
4422
  } else if (part.name === "tool_search_tool_regex" || part.name === "tool_search_tool_bm25") {
@@ -4729,6 +4759,10 @@ var AnthropicMessagesLanguageModel = class {
4729
4759
  toolName: contentBlock.toolName,
4730
4760
  input: finalInput,
4731
4761
  providerExecuted: contentBlock.providerExecuted,
4762
+ // Specific 'web_fetch' or 'web_search' tools may need code execution, which the Anthropic API
4763
+ // implicitly allows. In this scenario, we need to allow 'code_execution' tool calls even if the
4764
+ // tool was not explicitly provided. We therefore bypass the general validation by marking the
4765
+ // tool as dynamic.
4732
4766
  ...markCodeExecutionDynamic && contentBlock.providerToolName === "code_execution" ? { dynamic: true } : {},
4733
4767
  ...contentBlock.caller && {
4734
4768
  providerMetadata: {
@@ -4812,8 +4846,8 @@ var AnthropicMessagesLanguageModel = class {
4812
4846
  if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
4813
4847
  return;
4814
4848
  }
4815
- if (contentBlock.firstDelta && (contentBlock.providerToolName === "bash_code_execution" || contentBlock.providerToolName === "text_editor_code_execution")) {
4816
- delta = `{"type": "${contentBlock.providerToolName}",${delta.substring(1)}`;
4849
+ if (contentBlock.firstDelta && contentBlock.providerToolName === "code_execution") {
4850
+ delta = `{"type": "programmatic-tool-call",${delta.substring(1)}`;
4817
4851
  }
4818
4852
  controller.enqueue({
4819
4853
  type: "tool-input-delta",