@ai-sdk/anthropic 3.0.16 → 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.
@@ -1558,7 +1558,7 @@ async function convertToAnthropicMessagesPrompt({
1558
1558
  cacheControlValidator,
1559
1559
  toolNameMapping
1560
1560
  }) {
1561
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1561
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1562
1562
  const betas = /* @__PURE__ */ new Set();
1563
1563
  const blocks = groupIntoBlocks(prompt);
1564
1564
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -2111,13 +2111,25 @@ async function convertToAnthropicMessagesPrompt({
2111
2111
  if (providerToolName === "web_fetch") {
2112
2112
  const output = part.output;
2113
2113
  if (output.type === "error-json") {
2114
- const errorValue = JSON.parse(output.value);
2114
+ let errorValue = {};
2115
+ try {
2116
+ if (typeof output.value === "string") {
2117
+ errorValue = JSON.parse(output.value);
2118
+ } else if (typeof output.value === "object" && output.value !== null) {
2119
+ errorValue = output.value;
2120
+ }
2121
+ } catch (e) {
2122
+ const extractedErrorCode = (_p = output.value) == null ? void 0 : _p.errorCode;
2123
+ errorValue = {
2124
+ errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unknown"
2125
+ };
2126
+ }
2115
2127
  anthropicContent.push({
2116
2128
  type: "web_fetch_tool_result",
2117
2129
  tool_use_id: part.toolCallId,
2118
2130
  content: {
2119
2131
  type: "web_fetch_tool_result_error",
2120
- error_code: errorValue.errorCode
2132
+ error_code: (_q = errorValue.errorCode) != null ? _q : "unknown"
2121
2133
  },
2122
2134
  cache_control: cacheControl
2123
2135
  });
@@ -2306,6 +2318,21 @@ function mapAnthropicStopReason({
2306
2318
  // src/anthropic-messages-language-model.ts
2307
2319
  function createCitationSource(citation, citationDocuments, generateId2) {
2308
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
+ }
2309
2336
  if (citation.type !== "page_location" && citation.type !== "char_location") {
2310
2337
  return;
2311
2338
  }
@@ -2724,13 +2751,15 @@ var AnthropicMessagesLanguageModel = class {
2724
2751
  });
2725
2752
  }
2726
2753
  async doGenerate(options) {
2727
- 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;
2728
2755
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2729
2756
  ...options,
2730
2757
  stream: false,
2731
2758
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2732
2759
  });
2733
- const citationDocuments = this.extractCitationDocuments(options.prompt);
2760
+ const citationDocuments = [
2761
+ ...this.extractCitationDocuments(options.prompt)
2762
+ ];
2734
2763
  const {
2735
2764
  responseHeaders,
2736
2765
  value: response,
@@ -2886,6 +2915,10 @@ var AnthropicMessagesLanguageModel = class {
2886
2915
  }
2887
2916
  case "web_fetch_tool_result": {
2888
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
+ });
2889
2922
  content.push({
2890
2923
  type: "tool-result",
2891
2924
  toolCallId: part.tool_use_id,
@@ -2946,7 +2979,7 @@ var AnthropicMessagesLanguageModel = class {
2946
2979
  title: result.title,
2947
2980
  providerMetadata: {
2948
2981
  anthropic: {
2949
- pageAge: (_a = result.page_age) != null ? _a : null
2982
+ pageAge: (_b = result.page_age) != null ? _b : null
2950
2983
  }
2951
2984
  }
2952
2985
  });
@@ -2977,7 +3010,7 @@ var AnthropicMessagesLanguageModel = class {
2977
3010
  stdout: part.content.stdout,
2978
3011
  stderr: part.content.stderr,
2979
3012
  return_code: part.content.return_code,
2980
- content: (_b = part.content.content) != null ? _b : []
3013
+ content: (_c = part.content.content) != null ? _c : []
2981
3014
  }
2982
3015
  });
2983
3016
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3007,7 +3040,7 @@ var AnthropicMessagesLanguageModel = class {
3007
3040
  }
3008
3041
  // tool search tool results:
3009
3042
  case "tool_search_tool_result": {
3010
- 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";
3011
3044
  if (part.content.type === "tool_search_tool_search_result") {
3012
3045
  content.push({
3013
3046
  type: "tool-result",
@@ -3041,13 +3074,13 @@ var AnthropicMessagesLanguageModel = class {
3041
3074
  finishReason: response.stop_reason,
3042
3075
  isJsonResponseFromTool
3043
3076
  }),
3044
- raw: (_d = response.stop_reason) != null ? _d : void 0
3077
+ raw: (_e = response.stop_reason) != null ? _e : void 0
3045
3078
  },
3046
3079
  usage: convertAnthropicMessagesUsage(response.usage),
3047
3080
  request: { body: args },
3048
3081
  response: {
3049
- id: (_e = response.id) != null ? _e : void 0,
3050
- 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,
3051
3084
  headers: responseHeaders,
3052
3085
  body: rawResponse
3053
3086
  },
@@ -3055,20 +3088,20 @@ var AnthropicMessagesLanguageModel = class {
3055
3088
  providerMetadata: {
3056
3089
  anthropic: {
3057
3090
  usage: response.usage,
3058
- cacheCreationInputTokens: (_g = response.usage.cache_creation_input_tokens) != null ? _g : null,
3059
- 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,
3060
3093
  container: response.container ? {
3061
3094
  expiresAt: response.container.expires_at,
3062
3095
  id: response.container.id,
3063
- 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) => ({
3064
3097
  type: skill.type,
3065
3098
  skillId: skill.skill_id,
3066
3099
  version: skill.version
3067
- }))) != null ? _j : null
3100
+ }))) != null ? _k : null
3068
3101
  } : null,
3069
- contextManagement: (_k = mapAnthropicResponseContextManagement(
3102
+ contextManagement: (_l = mapAnthropicResponseContextManagement(
3070
3103
  response.context_management
3071
- )) != null ? _k : null
3104
+ )) != null ? _l : null
3072
3105
  }
3073
3106
  }
3074
3107
  };
@@ -3086,7 +3119,9 @@ var AnthropicMessagesLanguageModel = class {
3086
3119
  stream: true,
3087
3120
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3088
3121
  });
3089
- const citationDocuments = this.extractCitationDocuments(options.prompt);
3122
+ const citationDocuments = [
3123
+ ...this.extractCitationDocuments(options.prompt)
3124
+ ];
3090
3125
  const url = this.buildRequestUrl(true);
3091
3126
  const { responseHeaders, value: response } = await (0, import_provider_utils12.postJsonToApi)({
3092
3127
  url,
@@ -3126,7 +3161,7 @@ var AnthropicMessagesLanguageModel = class {
3126
3161
  controller.enqueue({ type: "stream-start", warnings });
3127
3162
  },
3128
3163
  transform(chunk, controller) {
3129
- 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;
3130
3165
  if (options.includeRawChunks) {
3131
3166
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3132
3167
  }
@@ -3262,6 +3297,10 @@ var AnthropicMessagesLanguageModel = class {
3262
3297
  }
3263
3298
  case "web_fetch_tool_result": {
3264
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
+ });
3265
3304
  controller.enqueue({
3266
3305
  type: "tool-result",
3267
3306
  toolCallId: part.tool_use_id,
@@ -3322,7 +3361,7 @@ var AnthropicMessagesLanguageModel = class {
3322
3361
  title: result.title,
3323
3362
  providerMetadata: {
3324
3363
  anthropic: {
3325
- pageAge: (_a2 = result.page_age) != null ? _a2 : null
3364
+ pageAge: (_b2 = result.page_age) != null ? _b2 : null
3326
3365
  }
3327
3366
  }
3328
3367
  });
@@ -3353,7 +3392,7 @@ var AnthropicMessagesLanguageModel = class {
3353
3392
  stdout: part.content.stdout,
3354
3393
  stderr: part.content.stderr,
3355
3394
  return_code: part.content.return_code,
3356
- content: (_b2 = part.content.content) != null ? _b2 : []
3395
+ content: (_c = part.content.content) != null ? _c : []
3357
3396
  }
3358
3397
  });
3359
3398
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3383,7 +3422,7 @@ var AnthropicMessagesLanguageModel = class {
3383
3422
  }
3384
3423
  // tool search tool results:
3385
3424
  case "tool_search_tool_result": {
3386
- 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";
3387
3426
  if (part.content.type === "tool_search_tool_search_result") {
3388
3427
  controller.enqueue({
3389
3428
  type: "tool-result",
@@ -3597,12 +3636,12 @@ var AnthropicMessagesLanguageModel = class {
3597
3636
  }
3598
3637
  case "message_start": {
3599
3638
  usage.input_tokens = value.message.usage.input_tokens;
3600
- usage.cache_read_input_tokens = (_d = value.message.usage.cache_read_input_tokens) != null ? _d : 0;
3601
- 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;
3602
3641
  rawUsage = {
3603
3642
  ...value.message.usage
3604
3643
  };
3605
- 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;
3606
3645
  if (value.message.container != null) {
3607
3646
  container = {
3608
3647
  expiresAt: value.message.container.expires_at,
@@ -3621,8 +3660,8 @@ var AnthropicMessagesLanguageModel = class {
3621
3660
  }
3622
3661
  controller.enqueue({
3623
3662
  type: "response-metadata",
3624
- id: (_g = value.message.id) != null ? _g : void 0,
3625
- 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
3626
3665
  });
3627
3666
  if (value.message.content != null) {
3628
3667
  for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
@@ -3638,7 +3677,7 @@ var AnthropicMessagesLanguageModel = class {
3638
3677
  id: part.id,
3639
3678
  toolName: part.name
3640
3679
  });
3641
- const inputStr = JSON.stringify((_i = part.input) != null ? _i : {});
3680
+ const inputStr = JSON.stringify((_j = part.input) != null ? _j : {});
3642
3681
  controller.enqueue({
3643
3682
  type: "tool-input-delta",
3644
3683
  id: part.id,
@@ -3676,17 +3715,17 @@ var AnthropicMessagesLanguageModel = class {
3676
3715
  finishReason: value.delta.stop_reason,
3677
3716
  isJsonResponseFromTool
3678
3717
  }),
3679
- raw: (_j = value.delta.stop_reason) != null ? _j : void 0
3718
+ raw: (_k = value.delta.stop_reason) != null ? _k : void 0
3680
3719
  };
3681
- stopSequence = (_k = value.delta.stop_sequence) != null ? _k : null;
3720
+ stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
3682
3721
  container = value.delta.container != null ? {
3683
3722
  expiresAt: value.delta.container.expires_at,
3684
3723
  id: value.delta.container.id,
3685
- 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) => ({
3686
3725
  type: skill.type,
3687
3726
  skillId: skill.skill_id,
3688
3727
  version: skill.version
3689
- }))) != null ? _m : null
3728
+ }))) != null ? _n : null
3690
3729
  } : null;
3691
3730
  if (value.delta.context_management) {
3692
3731
  contextManagement = mapAnthropicResponseContextManagement(