@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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 3.0.18
4
+
5
+ ### Patch Changes
6
+
7
+ - c10bd49: fix(anthropic): handle web_search_result_location citations and add webFetch documents to citationDocuments
8
+
9
+ ## 3.0.17
10
+
11
+ ### Patch Changes
12
+
13
+ - 4729bed: Fix JSON parsing crash when handling Anthropic web_fetch tool error results
14
+
3
15
  ## 3.0.16
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.16" : "0.0.0-test";
35
+ var VERSION = true ? "3.0.18" : "0.0.0-test";
36
36
 
37
37
  // src/anthropic-messages-language-model.ts
38
38
  var import_provider3 = require("@ai-sdk/provider");
@@ -1566,7 +1566,7 @@ async function convertToAnthropicMessagesPrompt({
1566
1566
  cacheControlValidator,
1567
1567
  toolNameMapping
1568
1568
  }) {
1569
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1569
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1570
1570
  const betas = /* @__PURE__ */ new Set();
1571
1571
  const blocks = groupIntoBlocks(prompt);
1572
1572
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -2119,13 +2119,25 @@ async function convertToAnthropicMessagesPrompt({
2119
2119
  if (providerToolName === "web_fetch") {
2120
2120
  const output = part.output;
2121
2121
  if (output.type === "error-json") {
2122
- const errorValue = JSON.parse(output.value);
2122
+ let errorValue = {};
2123
+ try {
2124
+ if (typeof output.value === "string") {
2125
+ errorValue = JSON.parse(output.value);
2126
+ } else if (typeof output.value === "object" && output.value !== null) {
2127
+ errorValue = output.value;
2128
+ }
2129
+ } catch (e) {
2130
+ const extractedErrorCode = (_p = output.value) == null ? void 0 : _p.errorCode;
2131
+ errorValue = {
2132
+ errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unknown"
2133
+ };
2134
+ }
2123
2135
  anthropicContent.push({
2124
2136
  type: "web_fetch_tool_result",
2125
2137
  tool_use_id: part.toolCallId,
2126
2138
  content: {
2127
2139
  type: "web_fetch_tool_result_error",
2128
- error_code: errorValue.errorCode
2140
+ error_code: (_q = errorValue.errorCode) != null ? _q : "unknown"
2129
2141
  },
2130
2142
  cache_control: cacheControl
2131
2143
  });
@@ -2314,6 +2326,21 @@ function mapAnthropicStopReason({
2314
2326
  // src/anthropic-messages-language-model.ts
2315
2327
  function createCitationSource(citation, citationDocuments, generateId3) {
2316
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
+ }
2317
2344
  if (citation.type !== "page_location" && citation.type !== "char_location") {
2318
2345
  return;
2319
2346
  }
@@ -2732,13 +2759,15 @@ var AnthropicMessagesLanguageModel = class {
2732
2759
  });
2733
2760
  }
2734
2761
  async doGenerate(options) {
2735
- 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;
2736
2763
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2737
2764
  ...options,
2738
2765
  stream: false,
2739
2766
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2740
2767
  });
2741
- const citationDocuments = this.extractCitationDocuments(options.prompt);
2768
+ const citationDocuments = [
2769
+ ...this.extractCitationDocuments(options.prompt)
2770
+ ];
2742
2771
  const {
2743
2772
  responseHeaders,
2744
2773
  value: response,
@@ -2894,6 +2923,10 @@ var AnthropicMessagesLanguageModel = class {
2894
2923
  }
2895
2924
  case "web_fetch_tool_result": {
2896
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
+ });
2897
2930
  content.push({
2898
2931
  type: "tool-result",
2899
2932
  toolCallId: part.tool_use_id,
@@ -2954,7 +2987,7 @@ var AnthropicMessagesLanguageModel = class {
2954
2987
  title: result.title,
2955
2988
  providerMetadata: {
2956
2989
  anthropic: {
2957
- pageAge: (_a = result.page_age) != null ? _a : null
2990
+ pageAge: (_b = result.page_age) != null ? _b : null
2958
2991
  }
2959
2992
  }
2960
2993
  });
@@ -2985,7 +3018,7 @@ var AnthropicMessagesLanguageModel = class {
2985
3018
  stdout: part.content.stdout,
2986
3019
  stderr: part.content.stderr,
2987
3020
  return_code: part.content.return_code,
2988
- content: (_b = part.content.content) != null ? _b : []
3021
+ content: (_c = part.content.content) != null ? _c : []
2989
3022
  }
2990
3023
  });
2991
3024
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3015,7 +3048,7 @@ var AnthropicMessagesLanguageModel = class {
3015
3048
  }
3016
3049
  // tool search tool results:
3017
3050
  case "tool_search_tool_result": {
3018
- 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";
3019
3052
  if (part.content.type === "tool_search_tool_search_result") {
3020
3053
  content.push({
3021
3054
  type: "tool-result",
@@ -3049,13 +3082,13 @@ var AnthropicMessagesLanguageModel = class {
3049
3082
  finishReason: response.stop_reason,
3050
3083
  isJsonResponseFromTool
3051
3084
  }),
3052
- raw: (_d = response.stop_reason) != null ? _d : void 0
3085
+ raw: (_e = response.stop_reason) != null ? _e : void 0
3053
3086
  },
3054
3087
  usage: convertAnthropicMessagesUsage(response.usage),
3055
3088
  request: { body: args },
3056
3089
  response: {
3057
- id: (_e = response.id) != null ? _e : void 0,
3058
- 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,
3059
3092
  headers: responseHeaders,
3060
3093
  body: rawResponse
3061
3094
  },
@@ -3063,20 +3096,20 @@ var AnthropicMessagesLanguageModel = class {
3063
3096
  providerMetadata: {
3064
3097
  anthropic: {
3065
3098
  usage: response.usage,
3066
- cacheCreationInputTokens: (_g = response.usage.cache_creation_input_tokens) != null ? _g : null,
3067
- 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,
3068
3101
  container: response.container ? {
3069
3102
  expiresAt: response.container.expires_at,
3070
3103
  id: response.container.id,
3071
- 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) => ({
3072
3105
  type: skill.type,
3073
3106
  skillId: skill.skill_id,
3074
3107
  version: skill.version
3075
- }))) != null ? _j : null
3108
+ }))) != null ? _k : null
3076
3109
  } : null,
3077
- contextManagement: (_k = mapAnthropicResponseContextManagement(
3110
+ contextManagement: (_l = mapAnthropicResponseContextManagement(
3078
3111
  response.context_management
3079
- )) != null ? _k : null
3112
+ )) != null ? _l : null
3080
3113
  }
3081
3114
  }
3082
3115
  };
@@ -3094,7 +3127,9 @@ var AnthropicMessagesLanguageModel = class {
3094
3127
  stream: true,
3095
3128
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3096
3129
  });
3097
- const citationDocuments = this.extractCitationDocuments(options.prompt);
3130
+ const citationDocuments = [
3131
+ ...this.extractCitationDocuments(options.prompt)
3132
+ ];
3098
3133
  const url = this.buildRequestUrl(true);
3099
3134
  const { responseHeaders, value: response } = await (0, import_provider_utils12.postJsonToApi)({
3100
3135
  url,
@@ -3134,7 +3169,7 @@ var AnthropicMessagesLanguageModel = class {
3134
3169
  controller.enqueue({ type: "stream-start", warnings });
3135
3170
  },
3136
3171
  transform(chunk, controller) {
3137
- 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;
3138
3173
  if (options.includeRawChunks) {
3139
3174
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3140
3175
  }
@@ -3270,6 +3305,10 @@ var AnthropicMessagesLanguageModel = class {
3270
3305
  }
3271
3306
  case "web_fetch_tool_result": {
3272
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
+ });
3273
3312
  controller.enqueue({
3274
3313
  type: "tool-result",
3275
3314
  toolCallId: part.tool_use_id,
@@ -3330,7 +3369,7 @@ var AnthropicMessagesLanguageModel = class {
3330
3369
  title: result.title,
3331
3370
  providerMetadata: {
3332
3371
  anthropic: {
3333
- pageAge: (_a2 = result.page_age) != null ? _a2 : null
3372
+ pageAge: (_b2 = result.page_age) != null ? _b2 : null
3334
3373
  }
3335
3374
  }
3336
3375
  });
@@ -3361,7 +3400,7 @@ var AnthropicMessagesLanguageModel = class {
3361
3400
  stdout: part.content.stdout,
3362
3401
  stderr: part.content.stderr,
3363
3402
  return_code: part.content.return_code,
3364
- content: (_b2 = part.content.content) != null ? _b2 : []
3403
+ content: (_c = part.content.content) != null ? _c : []
3365
3404
  }
3366
3405
  });
3367
3406
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3391,7 +3430,7 @@ var AnthropicMessagesLanguageModel = class {
3391
3430
  }
3392
3431
  // tool search tool results:
3393
3432
  case "tool_search_tool_result": {
3394
- 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";
3395
3434
  if (part.content.type === "tool_search_tool_search_result") {
3396
3435
  controller.enqueue({
3397
3436
  type: "tool-result",
@@ -3605,12 +3644,12 @@ var AnthropicMessagesLanguageModel = class {
3605
3644
  }
3606
3645
  case "message_start": {
3607
3646
  usage.input_tokens = value.message.usage.input_tokens;
3608
- usage.cache_read_input_tokens = (_d = value.message.usage.cache_read_input_tokens) != null ? _d : 0;
3609
- 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;
3610
3649
  rawUsage = {
3611
3650
  ...value.message.usage
3612
3651
  };
3613
- 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;
3614
3653
  if (value.message.container != null) {
3615
3654
  container = {
3616
3655
  expiresAt: value.message.container.expires_at,
@@ -3629,8 +3668,8 @@ var AnthropicMessagesLanguageModel = class {
3629
3668
  }
3630
3669
  controller.enqueue({
3631
3670
  type: "response-metadata",
3632
- id: (_g = value.message.id) != null ? _g : void 0,
3633
- 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
3634
3673
  });
3635
3674
  if (value.message.content != null) {
3636
3675
  for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
@@ -3646,7 +3685,7 @@ var AnthropicMessagesLanguageModel = class {
3646
3685
  id: part.id,
3647
3686
  toolName: part.name
3648
3687
  });
3649
- const inputStr = JSON.stringify((_i = part.input) != null ? _i : {});
3688
+ const inputStr = JSON.stringify((_j = part.input) != null ? _j : {});
3650
3689
  controller.enqueue({
3651
3690
  type: "tool-input-delta",
3652
3691
  id: part.id,
@@ -3684,17 +3723,17 @@ var AnthropicMessagesLanguageModel = class {
3684
3723
  finishReason: value.delta.stop_reason,
3685
3724
  isJsonResponseFromTool
3686
3725
  }),
3687
- raw: (_j = value.delta.stop_reason) != null ? _j : void 0
3726
+ raw: (_k = value.delta.stop_reason) != null ? _k : void 0
3688
3727
  };
3689
- stopSequence = (_k = value.delta.stop_sequence) != null ? _k : null;
3728
+ stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
3690
3729
  container = value.delta.container != null ? {
3691
3730
  expiresAt: value.delta.container.expires_at,
3692
3731
  id: value.delta.container.id,
3693
- 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) => ({
3694
3733
  type: skill.type,
3695
3734
  skillId: skill.skill_id,
3696
3735
  version: skill.version
3697
- }))) != null ? _m : null
3736
+ }))) != null ? _n : null
3698
3737
  } : null;
3699
3738
  if (value.delta.context_management) {
3700
3739
  contextManagement = mapAnthropicResponseContextManagement(