@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
@@ -2334,6 +2334,21 @@ function mapAnthropicStopReason({
2334
2334
  // src/anthropic-messages-language-model.ts
2335
2335
  function createCitationSource(citation, citationDocuments, generateId2) {
2336
2336
  var _a;
2337
+ if (citation.type === "web_search_result_location") {
2338
+ return {
2339
+ type: "source",
2340
+ sourceType: "url",
2341
+ id: generateId2(),
2342
+ url: citation.url,
2343
+ title: citation.title,
2344
+ providerMetadata: {
2345
+ anthropic: {
2346
+ citedText: citation.cited_text,
2347
+ encryptedIndex: citation.encrypted_index
2348
+ }
2349
+ }
2350
+ };
2351
+ }
2337
2352
  if (citation.type !== "page_location" && citation.type !== "char_location") {
2338
2353
  return;
2339
2354
  }
@@ -2752,13 +2767,15 @@ var AnthropicMessagesLanguageModel = class {
2752
2767
  });
2753
2768
  }
2754
2769
  async doGenerate(options) {
2755
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2770
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2756
2771
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2757
2772
  ...options,
2758
2773
  stream: false,
2759
2774
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2760
2775
  });
2761
- const citationDocuments = this.extractCitationDocuments(options.prompt);
2776
+ const citationDocuments = [
2777
+ ...this.extractCitationDocuments(options.prompt)
2778
+ ];
2762
2779
  const {
2763
2780
  responseHeaders,
2764
2781
  value: response,
@@ -2914,6 +2931,10 @@ var AnthropicMessagesLanguageModel = class {
2914
2931
  }
2915
2932
  case "web_fetch_tool_result": {
2916
2933
  if (part.content.type === "web_fetch_result") {
2934
+ citationDocuments.push({
2935
+ title: (_a = part.content.content.title) != null ? _a : part.content.url,
2936
+ mediaType: part.content.content.source.media_type
2937
+ });
2917
2938
  content.push({
2918
2939
  type: "tool-result",
2919
2940
  toolCallId: part.tool_use_id,
@@ -2974,7 +2995,7 @@ var AnthropicMessagesLanguageModel = class {
2974
2995
  title: result.title,
2975
2996
  providerMetadata: {
2976
2997
  anthropic: {
2977
- pageAge: (_a = result.page_age) != null ? _a : null
2998
+ pageAge: (_b = result.page_age) != null ? _b : null
2978
2999
  }
2979
3000
  }
2980
3001
  });
@@ -3005,7 +3026,7 @@ var AnthropicMessagesLanguageModel = class {
3005
3026
  stdout: part.content.stdout,
3006
3027
  stderr: part.content.stderr,
3007
3028
  return_code: part.content.return_code,
3008
- content: (_b = part.content.content) != null ? _b : []
3029
+ content: (_c = part.content.content) != null ? _c : []
3009
3030
  }
3010
3031
  });
3011
3032
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3035,7 +3056,7 @@ var AnthropicMessagesLanguageModel = class {
3035
3056
  }
3036
3057
  // tool search tool results:
3037
3058
  case "tool_search_tool_result": {
3038
- const providerToolName = (_c = serverToolCalls[part.tool_use_id]) != null ? _c : "tool_search_tool_regex";
3059
+ const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3039
3060
  if (part.content.type === "tool_search_tool_search_result") {
3040
3061
  content.push({
3041
3062
  type: "tool-result",
@@ -3069,13 +3090,13 @@ var AnthropicMessagesLanguageModel = class {
3069
3090
  finishReason: response.stop_reason,
3070
3091
  isJsonResponseFromTool
3071
3092
  }),
3072
- raw: (_d = response.stop_reason) != null ? _d : void 0
3093
+ raw: (_e = response.stop_reason) != null ? _e : void 0
3073
3094
  },
3074
3095
  usage: convertAnthropicMessagesUsage(response.usage),
3075
3096
  request: { body: args },
3076
3097
  response: {
3077
- id: (_e = response.id) != null ? _e : void 0,
3078
- modelId: (_f = response.model) != null ? _f : void 0,
3098
+ id: (_f = response.id) != null ? _f : void 0,
3099
+ modelId: (_g = response.model) != null ? _g : void 0,
3079
3100
  headers: responseHeaders,
3080
3101
  body: rawResponse
3081
3102
  },
@@ -3083,20 +3104,20 @@ var AnthropicMessagesLanguageModel = class {
3083
3104
  providerMetadata: {
3084
3105
  anthropic: {
3085
3106
  usage: response.usage,
3086
- cacheCreationInputTokens: (_g = response.usage.cache_creation_input_tokens) != null ? _g : null,
3087
- stopSequence: (_h = response.stop_sequence) != null ? _h : null,
3107
+ cacheCreationInputTokens: (_h = response.usage.cache_creation_input_tokens) != null ? _h : null,
3108
+ stopSequence: (_i = response.stop_sequence) != null ? _i : null,
3088
3109
  container: response.container ? {
3089
3110
  expiresAt: response.container.expires_at,
3090
3111
  id: response.container.id,
3091
- skills: (_j = (_i = response.container.skills) == null ? void 0 : _i.map((skill) => ({
3112
+ skills: (_k = (_j = response.container.skills) == null ? void 0 : _j.map((skill) => ({
3092
3113
  type: skill.type,
3093
3114
  skillId: skill.skill_id,
3094
3115
  version: skill.version
3095
- }))) != null ? _j : null
3116
+ }))) != null ? _k : null
3096
3117
  } : null,
3097
- contextManagement: (_k = mapAnthropicResponseContextManagement(
3118
+ contextManagement: (_l = mapAnthropicResponseContextManagement(
3098
3119
  response.context_management
3099
- )) != null ? _k : null
3120
+ )) != null ? _l : null
3100
3121
  }
3101
3122
  }
3102
3123
  };
@@ -3114,7 +3135,9 @@ var AnthropicMessagesLanguageModel = class {
3114
3135
  stream: true,
3115
3136
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3116
3137
  });
3117
- const citationDocuments = this.extractCitationDocuments(options.prompt);
3138
+ const citationDocuments = [
3139
+ ...this.extractCitationDocuments(options.prompt)
3140
+ ];
3118
3141
  const url = this.buildRequestUrl(true);
3119
3142
  const { responseHeaders, value: response } = await postJsonToApi({
3120
3143
  url,
@@ -3154,7 +3177,7 @@ var AnthropicMessagesLanguageModel = class {
3154
3177
  controller.enqueue({ type: "stream-start", warnings });
3155
3178
  },
3156
3179
  transform(chunk, controller) {
3157
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
3180
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
3158
3181
  if (options.includeRawChunks) {
3159
3182
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3160
3183
  }
@@ -3290,6 +3313,10 @@ var AnthropicMessagesLanguageModel = class {
3290
3313
  }
3291
3314
  case "web_fetch_tool_result": {
3292
3315
  if (part.content.type === "web_fetch_result") {
3316
+ citationDocuments.push({
3317
+ title: (_a2 = part.content.content.title) != null ? _a2 : part.content.url,
3318
+ mediaType: part.content.content.source.media_type
3319
+ });
3293
3320
  controller.enqueue({
3294
3321
  type: "tool-result",
3295
3322
  toolCallId: part.tool_use_id,
@@ -3350,7 +3377,7 @@ var AnthropicMessagesLanguageModel = class {
3350
3377
  title: result.title,
3351
3378
  providerMetadata: {
3352
3379
  anthropic: {
3353
- pageAge: (_a2 = result.page_age) != null ? _a2 : null
3380
+ pageAge: (_b2 = result.page_age) != null ? _b2 : null
3354
3381
  }
3355
3382
  }
3356
3383
  });
@@ -3381,7 +3408,7 @@ var AnthropicMessagesLanguageModel = class {
3381
3408
  stdout: part.content.stdout,
3382
3409
  stderr: part.content.stderr,
3383
3410
  return_code: part.content.return_code,
3384
- content: (_b2 = part.content.content) != null ? _b2 : []
3411
+ content: (_c = part.content.content) != null ? _c : []
3385
3412
  }
3386
3413
  });
3387
3414
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3411,7 +3438,7 @@ var AnthropicMessagesLanguageModel = class {
3411
3438
  }
3412
3439
  // tool search tool results:
3413
3440
  case "tool_search_tool_result": {
3414
- const providerToolName = (_c = serverToolCalls[part.tool_use_id]) != null ? _c : "tool_search_tool_regex";
3441
+ const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3415
3442
  if (part.content.type === "tool_search_tool_search_result") {
3416
3443
  controller.enqueue({
3417
3444
  type: "tool-result",
@@ -3625,12 +3652,12 @@ var AnthropicMessagesLanguageModel = class {
3625
3652
  }
3626
3653
  case "message_start": {
3627
3654
  usage.input_tokens = value.message.usage.input_tokens;
3628
- usage.cache_read_input_tokens = (_d = value.message.usage.cache_read_input_tokens) != null ? _d : 0;
3629
- usage.cache_creation_input_tokens = (_e = value.message.usage.cache_creation_input_tokens) != null ? _e : 0;
3655
+ usage.cache_read_input_tokens = (_e = value.message.usage.cache_read_input_tokens) != null ? _e : 0;
3656
+ usage.cache_creation_input_tokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : 0;
3630
3657
  rawUsage = {
3631
3658
  ...value.message.usage
3632
3659
  };
3633
- cacheCreationInputTokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : null;
3660
+ cacheCreationInputTokens = (_g = value.message.usage.cache_creation_input_tokens) != null ? _g : null;
3634
3661
  if (value.message.container != null) {
3635
3662
  container = {
3636
3663
  expiresAt: value.message.container.expires_at,
@@ -3649,8 +3676,8 @@ var AnthropicMessagesLanguageModel = class {
3649
3676
  }
3650
3677
  controller.enqueue({
3651
3678
  type: "response-metadata",
3652
- id: (_g = value.message.id) != null ? _g : void 0,
3653
- modelId: (_h = value.message.model) != null ? _h : void 0
3679
+ id: (_h = value.message.id) != null ? _h : void 0,
3680
+ modelId: (_i = value.message.model) != null ? _i : void 0
3654
3681
  });
3655
3682
  if (value.message.content != null) {
3656
3683
  for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
@@ -3666,7 +3693,7 @@ var AnthropicMessagesLanguageModel = class {
3666
3693
  id: part.id,
3667
3694
  toolName: part.name
3668
3695
  });
3669
- const inputStr = JSON.stringify((_i = part.input) != null ? _i : {});
3696
+ const inputStr = JSON.stringify((_j = part.input) != null ? _j : {});
3670
3697
  controller.enqueue({
3671
3698
  type: "tool-input-delta",
3672
3699
  id: part.id,
@@ -3704,17 +3731,17 @@ var AnthropicMessagesLanguageModel = class {
3704
3731
  finishReason: value.delta.stop_reason,
3705
3732
  isJsonResponseFromTool
3706
3733
  }),
3707
- raw: (_j = value.delta.stop_reason) != null ? _j : void 0
3734
+ raw: (_k = value.delta.stop_reason) != null ? _k : void 0
3708
3735
  };
3709
- stopSequence = (_k = value.delta.stop_sequence) != null ? _k : null;
3736
+ stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
3710
3737
  container = value.delta.container != null ? {
3711
3738
  expiresAt: value.delta.container.expires_at,
3712
3739
  id: value.delta.container.id,
3713
- skills: (_m = (_l = value.delta.container.skills) == null ? void 0 : _l.map((skill) => ({
3740
+ skills: (_n = (_m = value.delta.container.skills) == null ? void 0 : _m.map((skill) => ({
3714
3741
  type: skill.type,
3715
3742
  skillId: skill.skill_id,
3716
3743
  version: skill.version
3717
- }))) != null ? _m : null
3744
+ }))) != null ? _n : null
3718
3745
  } : null;
3719
3746
  if (value.delta.context_management) {
3720
3747
  contextManagement = mapAnthropicResponseContextManagement(