@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.
@@ -1574,7 +1574,7 @@ async function convertToAnthropicMessagesPrompt({
1574
1574
  cacheControlValidator,
1575
1575
  toolNameMapping
1576
1576
  }) {
1577
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1577
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1578
1578
  const betas = /* @__PURE__ */ new Set();
1579
1579
  const blocks = groupIntoBlocks(prompt);
1580
1580
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -2127,13 +2127,25 @@ async function convertToAnthropicMessagesPrompt({
2127
2127
  if (providerToolName === "web_fetch") {
2128
2128
  const output = part.output;
2129
2129
  if (output.type === "error-json") {
2130
- const errorValue = JSON.parse(output.value);
2130
+ let errorValue = {};
2131
+ try {
2132
+ if (typeof output.value === "string") {
2133
+ errorValue = JSON.parse(output.value);
2134
+ } else if (typeof output.value === "object" && output.value !== null) {
2135
+ errorValue = output.value;
2136
+ }
2137
+ } catch (e) {
2138
+ const extractedErrorCode = (_p = output.value) == null ? void 0 : _p.errorCode;
2139
+ errorValue = {
2140
+ errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unknown"
2141
+ };
2142
+ }
2131
2143
  anthropicContent.push({
2132
2144
  type: "web_fetch_tool_result",
2133
2145
  tool_use_id: part.toolCallId,
2134
2146
  content: {
2135
2147
  type: "web_fetch_tool_result_error",
2136
- error_code: errorValue.errorCode
2148
+ error_code: (_q = errorValue.errorCode) != null ? _q : "unknown"
2137
2149
  },
2138
2150
  cache_control: cacheControl
2139
2151
  });
@@ -2322,6 +2334,21 @@ function mapAnthropicStopReason({
2322
2334
  // src/anthropic-messages-language-model.ts
2323
2335
  function createCitationSource(citation, citationDocuments, generateId2) {
2324
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
+ }
2325
2352
  if (citation.type !== "page_location" && citation.type !== "char_location") {
2326
2353
  return;
2327
2354
  }
@@ -2740,13 +2767,15 @@ var AnthropicMessagesLanguageModel = class {
2740
2767
  });
2741
2768
  }
2742
2769
  async doGenerate(options) {
2743
- 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;
2744
2771
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2745
2772
  ...options,
2746
2773
  stream: false,
2747
2774
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2748
2775
  });
2749
- const citationDocuments = this.extractCitationDocuments(options.prompt);
2776
+ const citationDocuments = [
2777
+ ...this.extractCitationDocuments(options.prompt)
2778
+ ];
2750
2779
  const {
2751
2780
  responseHeaders,
2752
2781
  value: response,
@@ -2902,6 +2931,10 @@ var AnthropicMessagesLanguageModel = class {
2902
2931
  }
2903
2932
  case "web_fetch_tool_result": {
2904
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
+ });
2905
2938
  content.push({
2906
2939
  type: "tool-result",
2907
2940
  toolCallId: part.tool_use_id,
@@ -2962,7 +2995,7 @@ var AnthropicMessagesLanguageModel = class {
2962
2995
  title: result.title,
2963
2996
  providerMetadata: {
2964
2997
  anthropic: {
2965
- pageAge: (_a = result.page_age) != null ? _a : null
2998
+ pageAge: (_b = result.page_age) != null ? _b : null
2966
2999
  }
2967
3000
  }
2968
3001
  });
@@ -2993,7 +3026,7 @@ var AnthropicMessagesLanguageModel = class {
2993
3026
  stdout: part.content.stdout,
2994
3027
  stderr: part.content.stderr,
2995
3028
  return_code: part.content.return_code,
2996
- content: (_b = part.content.content) != null ? _b : []
3029
+ content: (_c = part.content.content) != null ? _c : []
2997
3030
  }
2998
3031
  });
2999
3032
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3023,7 +3056,7 @@ var AnthropicMessagesLanguageModel = class {
3023
3056
  }
3024
3057
  // tool search tool results:
3025
3058
  case "tool_search_tool_result": {
3026
- 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";
3027
3060
  if (part.content.type === "tool_search_tool_search_result") {
3028
3061
  content.push({
3029
3062
  type: "tool-result",
@@ -3057,13 +3090,13 @@ var AnthropicMessagesLanguageModel = class {
3057
3090
  finishReason: response.stop_reason,
3058
3091
  isJsonResponseFromTool
3059
3092
  }),
3060
- raw: (_d = response.stop_reason) != null ? _d : void 0
3093
+ raw: (_e = response.stop_reason) != null ? _e : void 0
3061
3094
  },
3062
3095
  usage: convertAnthropicMessagesUsage(response.usage),
3063
3096
  request: { body: args },
3064
3097
  response: {
3065
- id: (_e = response.id) != null ? _e : void 0,
3066
- 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,
3067
3100
  headers: responseHeaders,
3068
3101
  body: rawResponse
3069
3102
  },
@@ -3071,20 +3104,20 @@ var AnthropicMessagesLanguageModel = class {
3071
3104
  providerMetadata: {
3072
3105
  anthropic: {
3073
3106
  usage: response.usage,
3074
- cacheCreationInputTokens: (_g = response.usage.cache_creation_input_tokens) != null ? _g : null,
3075
- 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,
3076
3109
  container: response.container ? {
3077
3110
  expiresAt: response.container.expires_at,
3078
3111
  id: response.container.id,
3079
- 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) => ({
3080
3113
  type: skill.type,
3081
3114
  skillId: skill.skill_id,
3082
3115
  version: skill.version
3083
- }))) != null ? _j : null
3116
+ }))) != null ? _k : null
3084
3117
  } : null,
3085
- contextManagement: (_k = mapAnthropicResponseContextManagement(
3118
+ contextManagement: (_l = mapAnthropicResponseContextManagement(
3086
3119
  response.context_management
3087
- )) != null ? _k : null
3120
+ )) != null ? _l : null
3088
3121
  }
3089
3122
  }
3090
3123
  };
@@ -3102,7 +3135,9 @@ var AnthropicMessagesLanguageModel = class {
3102
3135
  stream: true,
3103
3136
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3104
3137
  });
3105
- const citationDocuments = this.extractCitationDocuments(options.prompt);
3138
+ const citationDocuments = [
3139
+ ...this.extractCitationDocuments(options.prompt)
3140
+ ];
3106
3141
  const url = this.buildRequestUrl(true);
3107
3142
  const { responseHeaders, value: response } = await postJsonToApi({
3108
3143
  url,
@@ -3142,7 +3177,7 @@ var AnthropicMessagesLanguageModel = class {
3142
3177
  controller.enqueue({ type: "stream-start", warnings });
3143
3178
  },
3144
3179
  transform(chunk, controller) {
3145
- 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;
3146
3181
  if (options.includeRawChunks) {
3147
3182
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3148
3183
  }
@@ -3278,6 +3313,10 @@ var AnthropicMessagesLanguageModel = class {
3278
3313
  }
3279
3314
  case "web_fetch_tool_result": {
3280
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
+ });
3281
3320
  controller.enqueue({
3282
3321
  type: "tool-result",
3283
3322
  toolCallId: part.tool_use_id,
@@ -3338,7 +3377,7 @@ var AnthropicMessagesLanguageModel = class {
3338
3377
  title: result.title,
3339
3378
  providerMetadata: {
3340
3379
  anthropic: {
3341
- pageAge: (_a2 = result.page_age) != null ? _a2 : null
3380
+ pageAge: (_b2 = result.page_age) != null ? _b2 : null
3342
3381
  }
3343
3382
  }
3344
3383
  });
@@ -3369,7 +3408,7 @@ var AnthropicMessagesLanguageModel = class {
3369
3408
  stdout: part.content.stdout,
3370
3409
  stderr: part.content.stderr,
3371
3410
  return_code: part.content.return_code,
3372
- content: (_b2 = part.content.content) != null ? _b2 : []
3411
+ content: (_c = part.content.content) != null ? _c : []
3373
3412
  }
3374
3413
  });
3375
3414
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3399,7 +3438,7 @@ var AnthropicMessagesLanguageModel = class {
3399
3438
  }
3400
3439
  // tool search tool results:
3401
3440
  case "tool_search_tool_result": {
3402
- 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";
3403
3442
  if (part.content.type === "tool_search_tool_search_result") {
3404
3443
  controller.enqueue({
3405
3444
  type: "tool-result",
@@ -3613,12 +3652,12 @@ var AnthropicMessagesLanguageModel = class {
3613
3652
  }
3614
3653
  case "message_start": {
3615
3654
  usage.input_tokens = value.message.usage.input_tokens;
3616
- usage.cache_read_input_tokens = (_d = value.message.usage.cache_read_input_tokens) != null ? _d : 0;
3617
- 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;
3618
3657
  rawUsage = {
3619
3658
  ...value.message.usage
3620
3659
  };
3621
- 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;
3622
3661
  if (value.message.container != null) {
3623
3662
  container = {
3624
3663
  expiresAt: value.message.container.expires_at,
@@ -3637,8 +3676,8 @@ var AnthropicMessagesLanguageModel = class {
3637
3676
  }
3638
3677
  controller.enqueue({
3639
3678
  type: "response-metadata",
3640
- id: (_g = value.message.id) != null ? _g : void 0,
3641
- 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
3642
3681
  });
3643
3682
  if (value.message.content != null) {
3644
3683
  for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
@@ -3654,7 +3693,7 @@ var AnthropicMessagesLanguageModel = class {
3654
3693
  id: part.id,
3655
3694
  toolName: part.name
3656
3695
  });
3657
- const inputStr = JSON.stringify((_i = part.input) != null ? _i : {});
3696
+ const inputStr = JSON.stringify((_j = part.input) != null ? _j : {});
3658
3697
  controller.enqueue({
3659
3698
  type: "tool-input-delta",
3660
3699
  id: part.id,
@@ -3692,17 +3731,17 @@ var AnthropicMessagesLanguageModel = class {
3692
3731
  finishReason: value.delta.stop_reason,
3693
3732
  isJsonResponseFromTool
3694
3733
  }),
3695
- raw: (_j = value.delta.stop_reason) != null ? _j : void 0
3734
+ raw: (_k = value.delta.stop_reason) != null ? _k : void 0
3696
3735
  };
3697
- stopSequence = (_k = value.delta.stop_sequence) != null ? _k : null;
3736
+ stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
3698
3737
  container = value.delta.container != null ? {
3699
3738
  expiresAt: value.delta.container.expires_at,
3700
3739
  id: value.delta.container.id,
3701
- 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) => ({
3702
3741
  type: skill.type,
3703
3742
  skillId: skill.skill_id,
3704
3743
  version: skill.version
3705
- }))) != null ? _m : null
3744
+ }))) != null ? _n : null
3706
3745
  } : null;
3707
3746
  if (value.delta.context_management) {
3708
3747
  contextManagement = mapAnthropicResponseContextManagement(