@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/dist/index.mjs CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  } from "@ai-sdk/provider-utils";
12
12
 
13
13
  // src/version.ts
14
- var VERSION = true ? "3.0.16" : "0.0.0-test";
14
+ var VERSION = true ? "3.0.18" : "0.0.0-test";
15
15
 
16
16
  // src/anthropic-messages-language-model.ts
17
17
  import {
@@ -1589,7 +1589,7 @@ async function convertToAnthropicMessagesPrompt({
1589
1589
  cacheControlValidator,
1590
1590
  toolNameMapping
1591
1591
  }) {
1592
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1592
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1593
1593
  const betas = /* @__PURE__ */ new Set();
1594
1594
  const blocks = groupIntoBlocks(prompt);
1595
1595
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -2142,13 +2142,25 @@ async function convertToAnthropicMessagesPrompt({
2142
2142
  if (providerToolName === "web_fetch") {
2143
2143
  const output = part.output;
2144
2144
  if (output.type === "error-json") {
2145
- const errorValue = JSON.parse(output.value);
2145
+ let errorValue = {};
2146
+ try {
2147
+ if (typeof output.value === "string") {
2148
+ errorValue = JSON.parse(output.value);
2149
+ } else if (typeof output.value === "object" && output.value !== null) {
2150
+ errorValue = output.value;
2151
+ }
2152
+ } catch (e) {
2153
+ const extractedErrorCode = (_p = output.value) == null ? void 0 : _p.errorCode;
2154
+ errorValue = {
2155
+ errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unknown"
2156
+ };
2157
+ }
2146
2158
  anthropicContent.push({
2147
2159
  type: "web_fetch_tool_result",
2148
2160
  tool_use_id: part.toolCallId,
2149
2161
  content: {
2150
2162
  type: "web_fetch_tool_result_error",
2151
- error_code: errorValue.errorCode
2163
+ error_code: (_q = errorValue.errorCode) != null ? _q : "unknown"
2152
2164
  },
2153
2165
  cache_control: cacheControl
2154
2166
  });
@@ -2337,6 +2349,21 @@ function mapAnthropicStopReason({
2337
2349
  // src/anthropic-messages-language-model.ts
2338
2350
  function createCitationSource(citation, citationDocuments, generateId3) {
2339
2351
  var _a;
2352
+ if (citation.type === "web_search_result_location") {
2353
+ return {
2354
+ type: "source",
2355
+ sourceType: "url",
2356
+ id: generateId3(),
2357
+ url: citation.url,
2358
+ title: citation.title,
2359
+ providerMetadata: {
2360
+ anthropic: {
2361
+ citedText: citation.cited_text,
2362
+ encryptedIndex: citation.encrypted_index
2363
+ }
2364
+ }
2365
+ };
2366
+ }
2340
2367
  if (citation.type !== "page_location" && citation.type !== "char_location") {
2341
2368
  return;
2342
2369
  }
@@ -2755,13 +2782,15 @@ var AnthropicMessagesLanguageModel = class {
2755
2782
  });
2756
2783
  }
2757
2784
  async doGenerate(options) {
2758
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
2785
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
2759
2786
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2760
2787
  ...options,
2761
2788
  stream: false,
2762
2789
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2763
2790
  });
2764
- const citationDocuments = this.extractCitationDocuments(options.prompt);
2791
+ const citationDocuments = [
2792
+ ...this.extractCitationDocuments(options.prompt)
2793
+ ];
2765
2794
  const {
2766
2795
  responseHeaders,
2767
2796
  value: response,
@@ -2917,6 +2946,10 @@ var AnthropicMessagesLanguageModel = class {
2917
2946
  }
2918
2947
  case "web_fetch_tool_result": {
2919
2948
  if (part.content.type === "web_fetch_result") {
2949
+ citationDocuments.push({
2950
+ title: (_a = part.content.content.title) != null ? _a : part.content.url,
2951
+ mediaType: part.content.content.source.media_type
2952
+ });
2920
2953
  content.push({
2921
2954
  type: "tool-result",
2922
2955
  toolCallId: part.tool_use_id,
@@ -2977,7 +3010,7 @@ var AnthropicMessagesLanguageModel = class {
2977
3010
  title: result.title,
2978
3011
  providerMetadata: {
2979
3012
  anthropic: {
2980
- pageAge: (_a = result.page_age) != null ? _a : null
3013
+ pageAge: (_b = result.page_age) != null ? _b : null
2981
3014
  }
2982
3015
  }
2983
3016
  });
@@ -3008,7 +3041,7 @@ var AnthropicMessagesLanguageModel = class {
3008
3041
  stdout: part.content.stdout,
3009
3042
  stderr: part.content.stderr,
3010
3043
  return_code: part.content.return_code,
3011
- content: (_b = part.content.content) != null ? _b : []
3044
+ content: (_c = part.content.content) != null ? _c : []
3012
3045
  }
3013
3046
  });
3014
3047
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3038,7 +3071,7 @@ var AnthropicMessagesLanguageModel = class {
3038
3071
  }
3039
3072
  // tool search tool results:
3040
3073
  case "tool_search_tool_result": {
3041
- const providerToolName = (_c = serverToolCalls[part.tool_use_id]) != null ? _c : "tool_search_tool_regex";
3074
+ const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3042
3075
  if (part.content.type === "tool_search_tool_search_result") {
3043
3076
  content.push({
3044
3077
  type: "tool-result",
@@ -3072,13 +3105,13 @@ var AnthropicMessagesLanguageModel = class {
3072
3105
  finishReason: response.stop_reason,
3073
3106
  isJsonResponseFromTool
3074
3107
  }),
3075
- raw: (_d = response.stop_reason) != null ? _d : void 0
3108
+ raw: (_e = response.stop_reason) != null ? _e : void 0
3076
3109
  },
3077
3110
  usage: convertAnthropicMessagesUsage(response.usage),
3078
3111
  request: { body: args },
3079
3112
  response: {
3080
- id: (_e = response.id) != null ? _e : void 0,
3081
- modelId: (_f = response.model) != null ? _f : void 0,
3113
+ id: (_f = response.id) != null ? _f : void 0,
3114
+ modelId: (_g = response.model) != null ? _g : void 0,
3082
3115
  headers: responseHeaders,
3083
3116
  body: rawResponse
3084
3117
  },
@@ -3086,20 +3119,20 @@ var AnthropicMessagesLanguageModel = class {
3086
3119
  providerMetadata: {
3087
3120
  anthropic: {
3088
3121
  usage: response.usage,
3089
- cacheCreationInputTokens: (_g = response.usage.cache_creation_input_tokens) != null ? _g : null,
3090
- stopSequence: (_h = response.stop_sequence) != null ? _h : null,
3122
+ cacheCreationInputTokens: (_h = response.usage.cache_creation_input_tokens) != null ? _h : null,
3123
+ stopSequence: (_i = response.stop_sequence) != null ? _i : null,
3091
3124
  container: response.container ? {
3092
3125
  expiresAt: response.container.expires_at,
3093
3126
  id: response.container.id,
3094
- skills: (_j = (_i = response.container.skills) == null ? void 0 : _i.map((skill) => ({
3127
+ skills: (_k = (_j = response.container.skills) == null ? void 0 : _j.map((skill) => ({
3095
3128
  type: skill.type,
3096
3129
  skillId: skill.skill_id,
3097
3130
  version: skill.version
3098
- }))) != null ? _j : null
3131
+ }))) != null ? _k : null
3099
3132
  } : null,
3100
- contextManagement: (_k = mapAnthropicResponseContextManagement(
3133
+ contextManagement: (_l = mapAnthropicResponseContextManagement(
3101
3134
  response.context_management
3102
- )) != null ? _k : null
3135
+ )) != null ? _l : null
3103
3136
  }
3104
3137
  }
3105
3138
  };
@@ -3117,7 +3150,9 @@ var AnthropicMessagesLanguageModel = class {
3117
3150
  stream: true,
3118
3151
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
3119
3152
  });
3120
- const citationDocuments = this.extractCitationDocuments(options.prompt);
3153
+ const citationDocuments = [
3154
+ ...this.extractCitationDocuments(options.prompt)
3155
+ ];
3121
3156
  const url = this.buildRequestUrl(true);
3122
3157
  const { responseHeaders, value: response } = await postJsonToApi({
3123
3158
  url,
@@ -3157,7 +3192,7 @@ var AnthropicMessagesLanguageModel = class {
3157
3192
  controller.enqueue({ type: "stream-start", warnings });
3158
3193
  },
3159
3194
  transform(chunk, controller) {
3160
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
3195
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
3161
3196
  if (options.includeRawChunks) {
3162
3197
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3163
3198
  }
@@ -3293,6 +3328,10 @@ var AnthropicMessagesLanguageModel = class {
3293
3328
  }
3294
3329
  case "web_fetch_tool_result": {
3295
3330
  if (part.content.type === "web_fetch_result") {
3331
+ citationDocuments.push({
3332
+ title: (_a2 = part.content.content.title) != null ? _a2 : part.content.url,
3333
+ mediaType: part.content.content.source.media_type
3334
+ });
3296
3335
  controller.enqueue({
3297
3336
  type: "tool-result",
3298
3337
  toolCallId: part.tool_use_id,
@@ -3353,7 +3392,7 @@ var AnthropicMessagesLanguageModel = class {
3353
3392
  title: result.title,
3354
3393
  providerMetadata: {
3355
3394
  anthropic: {
3356
- pageAge: (_a2 = result.page_age) != null ? _a2 : null
3395
+ pageAge: (_b2 = result.page_age) != null ? _b2 : null
3357
3396
  }
3358
3397
  }
3359
3398
  });
@@ -3384,7 +3423,7 @@ var AnthropicMessagesLanguageModel = class {
3384
3423
  stdout: part.content.stdout,
3385
3424
  stderr: part.content.stderr,
3386
3425
  return_code: part.content.return_code,
3387
- content: (_b2 = part.content.content) != null ? _b2 : []
3426
+ content: (_c = part.content.content) != null ? _c : []
3388
3427
  }
3389
3428
  });
3390
3429
  } else if (part.content.type === "code_execution_tool_result_error") {
@@ -3414,7 +3453,7 @@ var AnthropicMessagesLanguageModel = class {
3414
3453
  }
3415
3454
  // tool search tool results:
3416
3455
  case "tool_search_tool_result": {
3417
- const providerToolName = (_c = serverToolCalls[part.tool_use_id]) != null ? _c : "tool_search_tool_regex";
3456
+ const providerToolName = (_d = serverToolCalls[part.tool_use_id]) != null ? _d : "tool_search_tool_regex";
3418
3457
  if (part.content.type === "tool_search_tool_search_result") {
3419
3458
  controller.enqueue({
3420
3459
  type: "tool-result",
@@ -3628,12 +3667,12 @@ var AnthropicMessagesLanguageModel = class {
3628
3667
  }
3629
3668
  case "message_start": {
3630
3669
  usage.input_tokens = value.message.usage.input_tokens;
3631
- usage.cache_read_input_tokens = (_d = value.message.usage.cache_read_input_tokens) != null ? _d : 0;
3632
- usage.cache_creation_input_tokens = (_e = value.message.usage.cache_creation_input_tokens) != null ? _e : 0;
3670
+ usage.cache_read_input_tokens = (_e = value.message.usage.cache_read_input_tokens) != null ? _e : 0;
3671
+ usage.cache_creation_input_tokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : 0;
3633
3672
  rawUsage = {
3634
3673
  ...value.message.usage
3635
3674
  };
3636
- cacheCreationInputTokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : null;
3675
+ cacheCreationInputTokens = (_g = value.message.usage.cache_creation_input_tokens) != null ? _g : null;
3637
3676
  if (value.message.container != null) {
3638
3677
  container = {
3639
3678
  expiresAt: value.message.container.expires_at,
@@ -3652,8 +3691,8 @@ var AnthropicMessagesLanguageModel = class {
3652
3691
  }
3653
3692
  controller.enqueue({
3654
3693
  type: "response-metadata",
3655
- id: (_g = value.message.id) != null ? _g : void 0,
3656
- modelId: (_h = value.message.model) != null ? _h : void 0
3694
+ id: (_h = value.message.id) != null ? _h : void 0,
3695
+ modelId: (_i = value.message.model) != null ? _i : void 0
3657
3696
  });
3658
3697
  if (value.message.content != null) {
3659
3698
  for (let contentIndex = 0; contentIndex < value.message.content.length; contentIndex++) {
@@ -3669,7 +3708,7 @@ var AnthropicMessagesLanguageModel = class {
3669
3708
  id: part.id,
3670
3709
  toolName: part.name
3671
3710
  });
3672
- const inputStr = JSON.stringify((_i = part.input) != null ? _i : {});
3711
+ const inputStr = JSON.stringify((_j = part.input) != null ? _j : {});
3673
3712
  controller.enqueue({
3674
3713
  type: "tool-input-delta",
3675
3714
  id: part.id,
@@ -3707,17 +3746,17 @@ var AnthropicMessagesLanguageModel = class {
3707
3746
  finishReason: value.delta.stop_reason,
3708
3747
  isJsonResponseFromTool
3709
3748
  }),
3710
- raw: (_j = value.delta.stop_reason) != null ? _j : void 0
3749
+ raw: (_k = value.delta.stop_reason) != null ? _k : void 0
3711
3750
  };
3712
- stopSequence = (_k = value.delta.stop_sequence) != null ? _k : null;
3751
+ stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
3713
3752
  container = value.delta.container != null ? {
3714
3753
  expiresAt: value.delta.container.expires_at,
3715
3754
  id: value.delta.container.id,
3716
- skills: (_m = (_l = value.delta.container.skills) == null ? void 0 : _l.map((skill) => ({
3755
+ skills: (_n = (_m = value.delta.container.skills) == null ? void 0 : _m.map((skill) => ({
3717
3756
  type: skill.type,
3718
3757
  skillId: skill.skill_id,
3719
3758
  version: skill.version
3720
- }))) != null ? _m : null
3759
+ }))) != null ? _n : null
3721
3760
  } : null;
3722
3761
  if (value.delta.context_management) {
3723
3762
  contextManagement = mapAnthropicResponseContextManagement(