@ai-sdk/anthropic 3.0.17 → 3.0.18

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.
@@ -2318,6 +2318,21 @@ function mapAnthropicStopReason({
2318
2318
  // src/anthropic-messages-language-model.ts
2319
2319
  function createCitationSource(citation, citationDocuments, generateId2) {
2320
2320
  var _a;
2321
+ if (citation.type === "web_search_result_location") {
2322
+ return {
2323
+ type: "source",
2324
+ sourceType: "url",
2325
+ id: generateId2(),
2326
+ url: citation.url,
2327
+ title: citation.title,
2328
+ providerMetadata: {
2329
+ anthropic: {
2330
+ citedText: citation.cited_text,
2331
+ encryptedIndex: citation.encrypted_index
2332
+ }
2333
+ }
2334
+ };
2335
+ }
2321
2336
  if (citation.type !== "page_location" && citation.type !== "char_location") {
2322
2337
  return;
2323
2338
  }
@@ -2736,13 +2751,15 @@ var AnthropicMessagesLanguageModel = class {
2736
2751
  });
2737
2752
  }
2738
2753
  async doGenerate(options) {
2739
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2754
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2740
2755
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2741
2756
  ...options,
2742
2757
  stream: false,
2743
2758
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2744
2759
  });
2745
- const citationDocuments = this.extractCitationDocuments(options.prompt);
2760
+ const citationDocuments = [
2761
+ ...this.extractCitationDocuments(options.prompt)
2762
+ ];
2746
2763
  const {
2747
2764
  responseHeaders,
2748
2765
  value: response,
@@ -2898,6 +2915,10 @@ var AnthropicMessagesLanguageModel = class {
2898
2915
  }
2899
2916
  case "web_fetch_tool_result": {
2900
2917
  if (part.content.type === "web_fetch_result") {
2918
+ citationDocuments.push({
2919
+ title: (_a = part.content.content.title) != null ? _a : part.content.url,
2920
+ mediaType: part.content.content.source.media_type
2921
+ });
2901
2922
  content.push({
2902
2923
  type: "tool-result",
2903
2924
  toolCallId: part.tool_use_id,
@@ -2958,7 +2979,7 @@ var AnthropicMessagesLanguageModel = class {
2958
2979
  title: result.title,
2959
2980
  providerMetadata: {
2960
2981
  anthropic: {
2961
- pageAge: (_a = result.page_age) != null ? _a : null
2982
+ pageAge: (_b = result.page_age) != null ? _b : null
2962
2983
  }
2963
2984
  }
2964
2985
  });
@@ -2989,7 +3010,7 @@ var AnthropicMessagesLanguageModel = class {
2989
3010
  stdout: part.content.stdout,
2990
3011
  stderr: part.content.stderr,
2991
3012
  return_code: part.content.return_code,
2992
- content: (_b = part.content.content) != null ? _b : []
3013
+ content: (_c = part.content.content) != null ? _c : []
2993
3014
  }
2994
3015
  });
2995
3016
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3019,7 +3040,7 @@ var AnthropicMessagesLanguageModel = class {
3019
3040
  }
3020
3041
  // tool search tool results:
3021
3042
  case "tool_search_tool_result": {
3022
- const providerToolName = (_c = serverToolCalls[part.tool_use_id]) != null ? _c : "tool_search_tool_regex";
3043
+ const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3023
3044
  if (part.content.type === "tool_search_tool_search_result") {
3024
3045
  content.push({
3025
3046
  type: "tool-result",
@@ -3053,13 +3074,13 @@ var AnthropicMessagesLanguageModel = class {
3053
3074
  finishReason: response.stop_reason,
3054
3075
  isJsonResponseFromTool
3055
3076
  }),
3056
- raw: (_d = response.stop_reason) != null ? _d : void 0
3077
+ raw: (_e = response.stop_reason) != null ? _e : void 0
3057
3078
  },
3058
3079
  usage: convertAnthropicMessagesUsage(response.usage),
3059
3080
  request: { body: args },
3060
3081
  response: {
3061
- id: (_e = response.id) != null ? _e : void 0,
3062
- modelId: (_f = response.model) != null ? _f : void 0,
3082
+ id: (_f = response.id) != null ? _f : void 0,
3083
+ modelId: (_g = response.model) != null ? _g : void 0,
3063
3084
  headers: responseHeaders,
3064
3085
  body: rawResponse
3065
3086
  },
@@ -3067,20 +3088,20 @@ var AnthropicMessagesLanguageModel = class {
3067
3088
  providerMetadata: {
3068
3089
  anthropic: {
3069
3090
  usage: response.usage,
3070
- cacheCreationInputTokens: (_g = response.usage.cache_creation_input_tokens) != null ? _g : null,
3071
- stopSequence: (_h = response.stop_sequence) != null ? _h : null,
3091
+ cacheCreationInputTokens: (_h = response.usage.cache_creation_input_tokens) != null ? _h : null,
3092
+ stopSequence: (_i = response.stop_sequence) != null ? _i : null,
3072
3093
  container: response.container ? {
3073
3094
  expiresAt: response.container.expires_at,
3074
3095
  id: response.container.id,
3075
- skills: (_j = (_i = response.container.skills) == null ? void 0 : _i.map((skill) => ({
3096
+ skills: (_k = (_j = response.container.skills) == null ? void 0 : _j.map((skill) => ({
3076
3097
  type: skill.type,
3077
3098
  skillId: skill.skill_id,
3078
3099
  version: skill.version
3079
- }))) != null ? _j : null
3100
+ }))) != null ? _k : null
3080
3101
  } : null,
3081
- contextManagement: (_k = mapAnthropicResponseContextManagement(
3102
+ contextManagement: (_l = mapAnthropicResponseContextManagement(
3082
3103
  response.context_management
3083
- )) != null ? _k : null
3104
+ )) != null ? _l : null
3084
3105
  }
3085
3106
  }
3086
3107
  };
@@ -3098,7 +3119,9 @@ var AnthropicMessagesLanguageModel = class {
3098
3119
  stream: true,
3099
3120
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3100
3121
  });
3101
- const citationDocuments = this.extractCitationDocuments(options.prompt);
3122
+ const citationDocuments = [
3123
+ ...this.extractCitationDocuments(options.prompt)
3124
+ ];
3102
3125
  const url = this.buildRequestUrl(true);
3103
3126
  const { responseHeaders, value: response } = await (0, import_provider_utils12.postJsonToApi)({
3104
3127
  url,
@@ -3138,7 +3161,7 @@ var AnthropicMessagesLanguageModel = class {
3138
3161
  controller.enqueue({ type: "stream-start", warnings });
3139
3162
  },
3140
3163
  transform(chunk, controller) {
3141
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
3164
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
3142
3165
  if (options.includeRawChunks) {
3143
3166
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3144
3167
  }
@@ -3274,6 +3297,10 @@ var AnthropicMessagesLanguageModel = class {
3274
3297
  }
3275
3298
  case "web_fetch_tool_result": {
3276
3299
  if (part.content.type === "web_fetch_result") {
3300
+ citationDocuments.push({
3301
+ title: (_a2 = part.content.content.title) != null ? _a2 : part.content.url,
3302
+ mediaType: part.content.content.source.media_type
3303
+ });
3277
3304
  controller.enqueue({
3278
3305
  type: "tool-result",
3279
3306
  toolCallId: part.tool_use_id,
@@ -3334,7 +3361,7 @@ var AnthropicMessagesLanguageModel = class {
3334
3361
  title: result.title,
3335
3362
  providerMetadata: {
3336
3363
  anthropic: {
3337
- pageAge: (_a2 = result.page_age) != null ? _a2 : null
3364
+ pageAge: (_b2 = result.page_age) != null ? _b2 : null
3338
3365
  }
3339
3366
  }
3340
3367
  });
@@ -3365,7 +3392,7 @@ var AnthropicMessagesLanguageModel = class {
3365
3392
  stdout: part.content.stdout,
3366
3393
  stderr: part.content.stderr,
3367
3394
  return_code: part.content.return_code,
3368
- content: (_b2 = part.content.content) != null ? _b2 : []
3395
+ content: (_c = part.content.content) != null ? _c : []
3369
3396
  }
3370
3397
  });
3371
3398
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3395,7 +3422,7 @@ var AnthropicMessagesLanguageModel = class {
3395
3422
  }
3396
3423
  // tool search tool results:
3397
3424
  case "tool_search_tool_result": {
3398
- const providerToolName = (_c = serverToolCalls[part.tool_use_id]) != null ? _c : "tool_search_tool_regex";
3425
+ const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3399
3426
  if (part.content.type === "tool_search_tool_search_result") {
3400
3427
  controller.enqueue({
3401
3428
  type: "tool-result",
@@ -3609,12 +3636,12 @@ var AnthropicMessagesLanguageModel = class {
3609
3636
  }
3610
3637
  case "message_start": {
3611
3638
  usage.input_tokens = value.message.usage.input_tokens;
3612
- usage.cache_read_input_tokens = (_d = value.message.usage.cache_read_input_tokens) != null ? _d : 0;
3613
- usage.cache_creation_input_tokens = (_e = value.message.usage.cache_creation_input_tokens) != null ? _e : 0;
3639
+ usage.cache_read_input_tokens = (_e = value.message.usage.cache_read_input_tokens) != null ? _e : 0;
3640
+ usage.cache_creation_input_tokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : 0;
3614
3641
  rawUsage = {
3615
3642
  ...value.message.usage
3616
3643
  };
3617
- cacheCreationInputTokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : null;
3644
+ cacheCreationInputTokens = (_g = value.message.usage.cache_creation_input_tokens) != null ? _g : null;
3618
3645
  if (value.message.container != null) {
3619
3646
  container = {
3620
3647
  expiresAt: value.message.container.expires_at,
@@ -3633,8 +3660,8 @@ var AnthropicMessagesLanguageModel = class {
3633
3660
  }
3634
3661
  controller.enqueue({
3635
3662
  type: "response-metadata",
3636
- id: (_g = value.message.id) != null ? _g : void 0,
3637
- modelId: (_h = value.message.model) != null ? _h : void 0
3663
+ id: (_h = value.message.id) != null ? _h : void 0,
3664
+ modelId: (_i = value.message.model) != null ? _i : void 0
3638
3665
  });
3639
3666
  if (value.message.content != null) {
3640
3667
  for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
@@ -3650,7 +3677,7 @@ var AnthropicMessagesLanguageModel = class {
3650
3677
  id: part.id,
3651
3678
  toolName: part.name
3652
3679
  });
3653
- const inputStr = JSON.stringify((_i = part.input) != null ? _i : {});
3680
+ const inputStr = JSON.stringify((_j = part.input) != null ? _j : {});
3654
3681
  controller.enqueue({
3655
3682
  type: "tool-input-delta",
3656
3683
  id: part.id,
@@ -3688,17 +3715,17 @@ var AnthropicMessagesLanguageModel = class {
3688
3715
  finishReason: value.delta.stop_reason,
3689
3716
  isJsonResponseFromTool
3690
3717
  }),
3691
- raw: (_j = value.delta.stop_reason) != null ? _j : void 0
3718
+ raw: (_k = value.delta.stop_reason) != null ? _k : void 0
3692
3719
  };
3693
- stopSequence = (_k = value.delta.stop_sequence) != null ? _k : null;
3720
+ stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
3694
3721
  container = value.delta.container != null ? {
3695
3722
  expiresAt: value.delta.container.expires_at,
3696
3723
  id: value.delta.container.id,
3697
- skills: (_m = (_l = value.delta.container.skills) == null ? void 0 : _l.map((skill) => ({
3724
+ skills: (_n = (_m = value.delta.container.skills) == null ? void 0 : _m.map((skill) => ({
3698
3725
  type: skill.type,
3699
3726
  skillId: skill.skill_id,
3700
3727
  version: skill.version
3701
- }))) != null ? _m : null
3728
+ }))) != null ? _n : null
3702
3729
  } : null;
3703
3730
  if (value.delta.context_management) {
3704
3731
  contextManagement = mapAnthropicResponseContextManagement(