@ai-sdk/anthropic 3.0.17 → 3.0.19

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.
Files changed (78) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/index.js +57 -30
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +57 -30
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/internal/index.js +56 -29
  7. package/dist/internal/index.js.map +1 -1
  8. package/dist/internal/index.mjs +56 -29
  9. package/dist/internal/index.mjs.map +1 -1
  10. package/package.json +3 -2
  11. package/src/__fixtures__/anthropic-code-execution-20250825.1.chunks.txt +248 -0
  12. package/src/__fixtures__/anthropic-code-execution-20250825.1.json +70 -0
  13. package/src/__fixtures__/anthropic-code-execution-20250825.2.chunks.txt +984 -0
  14. package/src/__fixtures__/anthropic-code-execution-20250825.2.json +111 -0
  15. package/src/__fixtures__/anthropic-code-execution-20250825.pptx-skill.chunks.txt +691 -0
  16. package/src/__fixtures__/anthropic-code-execution-20250825.pptx-skill.json +1801 -0
  17. package/src/__fixtures__/anthropic-json-other-tool.1.chunks.txt +13 -0
  18. package/src/__fixtures__/anthropic-json-other-tool.1.json +26 -0
  19. package/src/__fixtures__/anthropic-json-output-format.1.chunks.txt +120 -0
  20. package/src/__fixtures__/anthropic-json-output-format.1.json +25 -0
  21. package/src/__fixtures__/anthropic-json-tool.1.chunks.txt +9 -0
  22. package/src/__fixtures__/anthropic-json-tool.1.json +37 -0
  23. package/src/__fixtures__/anthropic-json-tool.2.chunks.txt +14 -0
  24. package/src/__fixtures__/anthropic-mcp.1.chunks.txt +17 -0
  25. package/src/__fixtures__/anthropic-mcp.1.json +39 -0
  26. package/src/__fixtures__/anthropic-memory-20250818.1.json +28 -0
  27. package/src/__fixtures__/anthropic-message-delta-input-tokens.chunks.txt +8 -0
  28. package/src/__fixtures__/anthropic-programmatic-tool-calling.1.chunks.txt +278 -0
  29. package/src/__fixtures__/anthropic-programmatic-tool-calling.1.json +106 -0
  30. package/src/__fixtures__/anthropic-tool-no-args.chunks.txt +13 -0
  31. package/src/__fixtures__/anthropic-tool-no-args.json +31 -0
  32. package/src/__fixtures__/anthropic-tool-search-bm25.1.chunks.txt +47 -0
  33. package/src/__fixtures__/anthropic-tool-search-bm25.1.json +67 -0
  34. package/src/__fixtures__/anthropic-tool-search-regex.1.chunks.txt +51 -0
  35. package/src/__fixtures__/anthropic-tool-search-regex.1.json +65 -0
  36. package/src/__fixtures__/anthropic-web-fetch-tool.1.chunks.txt +64 -0
  37. package/src/__fixtures__/anthropic-web-fetch-tool.1.json +54 -0
  38. package/src/__fixtures__/anthropic-web-fetch-tool.2.json +56 -0
  39. package/src/__fixtures__/anthropic-web-fetch-tool.error.json +46 -0
  40. package/src/__fixtures__/anthropic-web-search-tool.1.chunks.txt +120 -0
  41. package/src/__fixtures__/anthropic-web-search-tool.1.json +181 -0
  42. package/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +16719 -0
  43. package/src/anthropic-error.test.ts +42 -0
  44. package/src/anthropic-error.ts +26 -0
  45. package/src/anthropic-message-metadata.ts +105 -0
  46. package/src/anthropic-messages-api.ts +1188 -0
  47. package/src/anthropic-messages-language-model.test.ts +7170 -0
  48. package/src/anthropic-messages-language-model.ts +2067 -0
  49. package/src/anthropic-messages-options.ts +213 -0
  50. package/src/anthropic-prepare-tools.test.ts +1219 -0
  51. package/src/anthropic-prepare-tools.ts +341 -0
  52. package/src/anthropic-provider.test.ts +162 -0
  53. package/src/anthropic-provider.ts +152 -0
  54. package/src/anthropic-tools.ts +182 -0
  55. package/src/convert-anthropic-messages-usage.ts +32 -0
  56. package/src/convert-to-anthropic-messages-prompt.test.ts +2902 -0
  57. package/src/convert-to-anthropic-messages-prompt.ts +1050 -0
  58. package/src/forward-anthropic-container-id-from-last-step.ts +38 -0
  59. package/src/get-cache-control.ts +63 -0
  60. package/src/index.ts +10 -0
  61. package/src/internal/index.ts +4 -0
  62. package/src/map-anthropic-stop-reason.ts +28 -0
  63. package/src/tool/bash_20241022.ts +33 -0
  64. package/src/tool/bash_20250124.ts +33 -0
  65. package/src/tool/code-execution_20250522.ts +61 -0
  66. package/src/tool/code-execution_20250825.ts +281 -0
  67. package/src/tool/computer_20241022.ts +87 -0
  68. package/src/tool/computer_20250124.ts +130 -0
  69. package/src/tool/memory_20250818.ts +62 -0
  70. package/src/tool/text-editor_20241022.ts +63 -0
  71. package/src/tool/text-editor_20250124.ts +63 -0
  72. package/src/tool/text-editor_20250429.ts +64 -0
  73. package/src/tool/text-editor_20250728.ts +80 -0
  74. package/src/tool/tool-search-bm25_20251119.ts +98 -0
  75. package/src/tool/tool-search-regex_20251119.ts +110 -0
  76. package/src/tool/web-fetch-20250910.ts +145 -0
  77. package/src/tool/web-search_20250305.ts +136 -0
  78. package/src/version.ts +6 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 3.0.19
4
+
5
+ ### Patch Changes
6
+
7
+ - 8dc54db: chore: add src folders to package bundle
8
+
9
+ ## 3.0.18
10
+
11
+ ### Patch Changes
12
+
13
+ - c10bd49: fix(anthropic): handle web_search_result_location citations and add webFetch documents to citationDocuments
14
+
3
15
  ## 3.0.17
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_utils22 = require("@ai-sdk/provider-utils");
33
33
 
34
34
  // src/version.ts
35
- var VERSION = true ? "3.0.17" : "0.0.0-test";
35
+ var VERSION = true ? "3.0.19" : "0.0.0-test";
36
36
 
37
37
  // src/anthropic-messages-language-model.ts
38
38
  var import_provider3 = require("@ai-sdk/provider");
@@ -2326,6 +2326,21 @@ function mapAnthropicStopReason({
2326
2326
  // src/anthropic-messages-language-model.ts
2327
2327
  function createCitationSource(citation, citationDocuments, generateId3) {
2328
2328
  var _a;
2329
+ if (citation.type === "web_search_result_location") {
2330
+ return {
2331
+ type: "source",
2332
+ sourceType: "url",
2333
+ id: generateId3(),
2334
+ url: citation.url,
2335
+ title: citation.title,
2336
+ providerMetadata: {
2337
+ anthropic: {
2338
+ citedText: citation.cited_text,
2339
+ encryptedIndex: citation.encrypted_index
2340
+ }
2341
+ }
2342
+ };
2343
+ }
2329
2344
  if (citation.type !== "page_location" && citation.type !== "char_location") {
2330
2345
  return;
2331
2346
  }
@@ -2744,13 +2759,15 @@ var AnthropicMessagesLanguageModel = class {
2744
2759
  });
2745
2760
  }
2746
2761
  async doGenerate(options) {
2747
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2762
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2748
2763
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2749
2764
  ...options,
2750
2765
  stream: false,
2751
2766
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2752
2767
  });
2753
- const citationDocuments = this.extractCitationDocuments(options.prompt);
2768
+ const citationDocuments = [
2769
+ ...this.extractCitationDocuments(options.prompt)
2770
+ ];
2754
2771
  const {
2755
2772
  responseHeaders,
2756
2773
  value: response,
@@ -2906,6 +2923,10 @@ var AnthropicMessagesLanguageModel = class {
2906
2923
  }
2907
2924
  case "web_fetch_tool_result": {
2908
2925
  if (part.content.type === "web_fetch_result") {
2926
+ citationDocuments.push({
2927
+ title: (_a = part.content.content.title) != null ? _a : part.content.url,
2928
+ mediaType: part.content.content.source.media_type
2929
+ });
2909
2930
  content.push({
2910
2931
  type: "tool-result",
2911
2932
  toolCallId: part.tool_use_id,
@@ -2966,7 +2987,7 @@ var AnthropicMessagesLanguageModel = class {
2966
2987
  title: result.title,
2967
2988
  providerMetadata: {
2968
2989
  anthropic: {
2969
- pageAge: (_a = result.page_age) != null ? _a : null
2990
+ pageAge: (_b = result.page_age) != null ? _b : null
2970
2991
  }
2971
2992
  }
2972
2993
  });
@@ -2997,7 +3018,7 @@ var AnthropicMessagesLanguageModel = class {
2997
3018
  stdout: part.content.stdout,
2998
3019
  stderr: part.content.stderr,
2999
3020
  return_code: part.content.return_code,
3000
- content: (_b = part.content.content) != null ? _b : []
3021
+ content: (_c = part.content.content) != null ? _c : []
3001
3022
  }
3002
3023
  });
3003
3024
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3027,7 +3048,7 @@ var AnthropicMessagesLanguageModel = class {
3027
3048
  }
3028
3049
  // tool search tool results:
3029
3050
  case "tool_search_tool_result": {
3030
- const providerToolName = (_c = serverToolCalls[part.tool_use_id]) != null ? _c : "tool_search_tool_regex";
3051
+ const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3031
3052
  if (part.content.type === "tool_search_tool_search_result") {
3032
3053
  content.push({
3033
3054
  type: "tool-result",
@@ -3061,13 +3082,13 @@ var AnthropicMessagesLanguageModel = class {
3061
3082
  finishReason: response.stop_reason,
3062
3083
  isJsonResponseFromTool
3063
3084
  }),
3064
- raw: (_d = response.stop_reason) != null ? _d : void 0
3085
+ raw: (_e = response.stop_reason) != null ? _e : void 0
3065
3086
  },
3066
3087
  usage: convertAnthropicMessagesUsage(response.usage),
3067
3088
  request: { body: args },
3068
3089
  response: {
3069
- id: (_e = response.id) != null ? _e : void 0,
3070
- modelId: (_f = response.model) != null ? _f : void 0,
3090
+ id: (_f = response.id) != null ? _f : void 0,
3091
+ modelId: (_g = response.model) != null ? _g : void 0,
3071
3092
  headers: responseHeaders,
3072
3093
  body: rawResponse
3073
3094
  },
@@ -3075,20 +3096,20 @@ var AnthropicMessagesLanguageModel = class {
3075
3096
  providerMetadata: {
3076
3097
  anthropic: {
3077
3098
  usage: response.usage,
3078
- cacheCreationInputTokens: (_g = response.usage.cache_creation_input_tokens) != null ? _g : null,
3079
- stopSequence: (_h = response.stop_sequence) != null ? _h : null,
3099
+ cacheCreationInputTokens: (_h = response.usage.cache_creation_input_tokens) != null ? _h : null,
3100
+ stopSequence: (_i = response.stop_sequence) != null ? _i : null,
3080
3101
  container: response.container ? {
3081
3102
  expiresAt: response.container.expires_at,
3082
3103
  id: response.container.id,
3083
- skills: (_j = (_i = response.container.skills) == null ? void 0 : _i.map((skill) => ({
3104
+ skills: (_k = (_j = response.container.skills) == null ? void 0 : _j.map((skill) => ({
3084
3105
  type: skill.type,
3085
3106
  skillId: skill.skill_id,
3086
3107
  version: skill.version
3087
- }))) != null ? _j : null
3108
+ }))) != null ? _k : null
3088
3109
  } : null,
3089
- contextManagement: (_k = mapAnthropicResponseContextManagement(
3110
+ contextManagement: (_l = mapAnthropicResponseContextManagement(
3090
3111
  response.context_management
3091
- )) != null ? _k : null
3112
+ )) != null ? _l : null
3092
3113
  }
3093
3114
  }
3094
3115
  };
@@ -3106,7 +3127,9 @@ var AnthropicMessagesLanguageModel = class {
3106
3127
  stream: true,
3107
3128
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3108
3129
  });
3109
- const citationDocuments = this.extractCitationDocuments(options.prompt);
3130
+ const citationDocuments = [
3131
+ ...this.extractCitationDocuments(options.prompt)
3132
+ ];
3110
3133
  const url = this.buildRequestUrl(true);
3111
3134
  const { responseHeaders, value: response } = await (0, import_provider_utils12.postJsonToApi)({
3112
3135
  url,
@@ -3146,7 +3169,7 @@ var AnthropicMessagesLanguageModel = class {
3146
3169
  controller.enqueue({ type: "stream-start", warnings });
3147
3170
  },
3148
3171
  transform(chunk, controller) {
3149
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
3172
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
3150
3173
  if (options.includeRawChunks) {
3151
3174
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3152
3175
  }
@@ -3282,6 +3305,10 @@ var AnthropicMessagesLanguageModel = class {
3282
3305
  }
3283
3306
  case "web_fetch_tool_result": {
3284
3307
  if (part.content.type === "web_fetch_result") {
3308
+ citationDocuments.push({
3309
+ title: (_a2 = part.content.content.title) != null ? _a2 : part.content.url,
3310
+ mediaType: part.content.content.source.media_type
3311
+ });
3285
3312
  controller.enqueue({
3286
3313
  type: "tool-result",
3287
3314
  toolCallId: part.tool_use_id,
@@ -3342,7 +3369,7 @@ var AnthropicMessagesLanguageModel = class {
3342
3369
  title: result.title,
3343
3370
  providerMetadata: {
3344
3371
  anthropic: {
3345
- pageAge: (_a2 = result.page_age) != null ? _a2 : null
3372
+ pageAge: (_b2 = result.page_age) != null ? _b2 : null
3346
3373
  }
3347
3374
  }
3348
3375
  });
@@ -3373,7 +3400,7 @@ var AnthropicMessagesLanguageModel = class {
3373
3400
  stdout: part.content.stdout,
3374
3401
  stderr: part.content.stderr,
3375
3402
  return_code: part.content.return_code,
3376
- content: (_b2 = part.content.content) != null ? _b2 : []
3403
+ content: (_c = part.content.content) != null ? _c : []
3377
3404
  }
3378
3405
  });
3379
3406
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3403,7 +3430,7 @@ var AnthropicMessagesLanguageModel = class {
3403
3430
  }
3404
3431
  // tool search tool results:
3405
3432
  case "tool_search_tool_result": {
3406
- const providerToolName = (_c = serverToolCalls[part.tool_use_id]) != null ? _c : "tool_search_tool_regex";
3433
+ const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3407
3434
  if (part.content.type === "tool_search_tool_search_result") {
3408
3435
  controller.enqueue({
3409
3436
  type: "tool-result",
@@ -3617,12 +3644,12 @@ var AnthropicMessagesLanguageModel = class {
3617
3644
  }
3618
3645
  case "message_start": {
3619
3646
  usage.input_tokens = value.message.usage.input_tokens;
3620
- usage.cache_read_input_tokens = (_d = value.message.usage.cache_read_input_tokens) != null ? _d : 0;
3621
- usage.cache_creation_input_tokens = (_e = value.message.usage.cache_creation_input_tokens) != null ? _e : 0;
3647
+ usage.cache_read_input_tokens = (_e = value.message.usage.cache_read_input_tokens) != null ? _e : 0;
3648
+ usage.cache_creation_input_tokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : 0;
3622
3649
  rawUsage = {
3623
3650
  ...value.message.usage
3624
3651
  };
3625
- cacheCreationInputTokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : null;
3652
+ cacheCreationInputTokens = (_g = value.message.usage.cache_creation_input_tokens) != null ? _g : null;
3626
3653
  if (value.message.container != null) {
3627
3654
  container = {
3628
3655
  expiresAt: value.message.container.expires_at,
@@ -3641,8 +3668,8 @@ var AnthropicMessagesLanguageModel = class {
3641
3668
  }
3642
3669
  controller.enqueue({
3643
3670
  type: "response-metadata",
3644
- id: (_g = value.message.id) != null ? _g : void 0,
3645
- modelId: (_h = value.message.model) != null ? _h : void 0
3671
+ id: (_h = value.message.id) != null ? _h : void 0,
3672
+ modelId: (_i = value.message.model) != null ? _i : void 0
3646
3673
  });
3647
3674
  if (value.message.content != null) {
3648
3675
  for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
@@ -3658,7 +3685,7 @@ var AnthropicMessagesLanguageModel = class {
3658
3685
  id: part.id,
3659
3686
  toolName: part.name
3660
3687
  });
3661
- const inputStr = JSON.stringify((_i = part.input) != null ? _i : {});
3688
+ const inputStr = JSON.stringify((_j = part.input) != null ? _j : {});
3662
3689
  controller.enqueue({
3663
3690
  type: "tool-input-delta",
3664
3691
  id: part.id,
@@ -3696,17 +3723,17 @@ var AnthropicMessagesLanguageModel = class {
3696
3723
  finishReason: value.delta.stop_reason,
3697
3724
  isJsonResponseFromTool
3698
3725
  }),
3699
- raw: (_j = value.delta.stop_reason) != null ? _j : void 0
3726
+ raw: (_k = value.delta.stop_reason) != null ? _k : void 0
3700
3727
  };
3701
- stopSequence = (_k = value.delta.stop_sequence) != null ? _k : null;
3728
+ stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
3702
3729
  container = value.delta.container != null ? {
3703
3730
  expiresAt: value.delta.container.expires_at,
3704
3731
  id: value.delta.container.id,
3705
- skills: (_m = (_l = value.delta.container.skills) == null ? void 0 : _l.map((skill) => ({
3732
+ skills: (_n = (_m = value.delta.container.skills) == null ? void 0 : _m.map((skill) => ({
3706
3733
  type: skill.type,
3707
3734
  skillId: skill.skill_id,
3708
3735
  version: skill.version
3709
- }))) != null ? _m : null
3736
+ }))) != null ? _n : null
3710
3737
  } : null;
3711
3738
  if (value.delta.context_management) {
3712
3739
  contextManagement = mapAnthropicResponseContextManagement(