@ai-sdk/anthropic 2.0.32 → 2.0.33

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
@@ -10,7 +10,7 @@ import {
10
10
  } from "@ai-sdk/provider-utils";
11
11
 
12
12
  // src/version.ts
13
- var VERSION = true ? "2.0.32" : "0.0.0-test";
13
+ var VERSION = true ? "2.0.33" : "0.0.0-test";
14
14
 
15
15
  // src/anthropic-messages-language-model.ts
16
16
  import {
@@ -560,12 +560,46 @@ import {
560
560
  } from "@ai-sdk/provider";
561
561
 
562
562
  // src/get-cache-control.ts
563
+ var MAX_CACHE_BREAKPOINTS = 4;
563
564
  function getCacheControl(providerMetadata) {
564
565
  var _a;
565
566
  const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
566
567
  const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control;
567
568
  return cacheControlValue;
568
569
  }
570
+ var CacheControlValidator = class {
571
+ constructor() {
572
+ this.breakpointCount = 0;
573
+ this.warnings = [];
574
+ }
575
+ getCacheControl(providerMetadata, context) {
576
+ const cacheControlValue = getCacheControl(providerMetadata);
577
+ if (!cacheControlValue) {
578
+ return void 0;
579
+ }
580
+ if (!context.canCache) {
581
+ this.warnings.push({
582
+ type: "unsupported-setting",
583
+ setting: "cacheControl",
584
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
585
+ });
586
+ return void 0;
587
+ }
588
+ this.breakpointCount++;
589
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
590
+ this.warnings.push({
591
+ type: "unsupported-setting",
592
+ setting: "cacheControl",
593
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
594
+ });
595
+ return void 0;
596
+ }
597
+ return cacheControlValue;
598
+ }
599
+ getWarnings() {
600
+ return this.warnings;
601
+ }
602
+ };
569
603
 
570
604
  // src/tool/text-editor_20250728.ts
571
605
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -719,11 +753,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
719
753
  async function prepareTools({
720
754
  tools,
721
755
  toolChoice,
722
- disableParallelToolUse
756
+ disableParallelToolUse,
757
+ cacheControlValidator
723
758
  }) {
724
759
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
725
760
  const toolWarnings = [];
726
761
  const betas = /* @__PURE__ */ new Set();
762
+ const validator = cacheControlValidator || new CacheControlValidator();
727
763
  if (tools == null) {
728
764
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
729
765
  }
@@ -731,7 +767,10 @@ async function prepareTools({
731
767
  for (const tool of tools) {
732
768
  switch (tool.type) {
733
769
  case "function": {
734
- const cacheControl = getCacheControl(tool.providerOptions);
770
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
771
+ type: "tool definition",
772
+ canCache: true
773
+ });
735
774
  anthropicTools2.push({
736
775
  name: tool.name,
737
776
  description: tool.description,
@@ -746,7 +785,8 @@ async function prepareTools({
746
785
  betas.add("code-execution-2025-05-22");
747
786
  anthropicTools2.push({
748
787
  type: "code_execution_20250522",
749
- name: "code_execution"
788
+ name: "code_execution",
789
+ cache_control: void 0
750
790
  });
751
791
  break;
752
792
  }
@@ -765,7 +805,8 @@ async function prepareTools({
765
805
  type: "computer_20250124",
766
806
  display_width_px: tool.args.displayWidthPx,
767
807
  display_height_px: tool.args.displayHeightPx,
768
- display_number: tool.args.displayNumber
808
+ display_number: tool.args.displayNumber,
809
+ cache_control: void 0
769
810
  });
770
811
  break;
771
812
  }
@@ -776,7 +817,8 @@ async function prepareTools({
776
817
  type: "computer_20241022",
777
818
  display_width_px: tool.args.displayWidthPx,
778
819
  display_height_px: tool.args.displayHeightPx,
779
- display_number: tool.args.displayNumber
820
+ display_number: tool.args.displayNumber,
821
+ cache_control: void 0
780
822
  });
781
823
  break;
782
824
  }
@@ -784,7 +826,8 @@ async function prepareTools({
784
826
  betas.add("computer-use-2025-01-24");
785
827
  anthropicTools2.push({
786
828
  name: "str_replace_editor",
787
- type: "text_editor_20250124"
829
+ type: "text_editor_20250124",
830
+ cache_control: void 0
788
831
  });
789
832
  break;
790
833
  }
@@ -792,7 +835,8 @@ async function prepareTools({
792
835
  betas.add("computer-use-2024-10-22");
793
836
  anthropicTools2.push({
794
837
  name: "str_replace_editor",
795
- type: "text_editor_20241022"
838
+ type: "text_editor_20241022",
839
+ cache_control: void 0
796
840
  });
797
841
  break;
798
842
  }
@@ -800,7 +844,8 @@ async function prepareTools({
800
844
  betas.add("computer-use-2025-01-24");
801
845
  anthropicTools2.push({
802
846
  name: "str_replace_based_edit_tool",
803
- type: "text_editor_20250429"
847
+ type: "text_editor_20250429",
848
+ cache_control: void 0
804
849
  });
805
850
  break;
806
851
  }
@@ -812,7 +857,8 @@ async function prepareTools({
812
857
  anthropicTools2.push({
813
858
  name: "str_replace_based_edit_tool",
814
859
  type: "text_editor_20250728",
815
- max_characters: args.maxCharacters
860
+ max_characters: args.maxCharacters,
861
+ cache_control: void 0
816
862
  });
817
863
  break;
818
864
  }
@@ -820,7 +866,8 @@ async function prepareTools({
820
866
  betas.add("computer-use-2025-01-24");
821
867
  anthropicTools2.push({
822
868
  name: "bash",
823
- type: "bash_20250124"
869
+ type: "bash_20250124",
870
+ cache_control: void 0
824
871
  });
825
872
  break;
826
873
  }
@@ -828,7 +875,8 @@ async function prepareTools({
828
875
  betas.add("computer-use-2024-10-22");
829
876
  anthropicTools2.push({
830
877
  name: "bash",
831
- type: "bash_20241022"
878
+ type: "bash_20241022",
879
+ cache_control: void 0
832
880
  });
833
881
  break;
834
882
  }
@@ -853,7 +901,8 @@ async function prepareTools({
853
901
  allowed_domains: args.allowedDomains,
854
902
  blocked_domains: args.blockedDomains,
855
903
  citations: args.citations,
856
- max_content_tokens: args.maxContentTokens
904
+ max_content_tokens: args.maxContentTokens,
905
+ cache_control: void 0
857
906
  });
858
907
  break;
859
908
  }
@@ -868,7 +917,8 @@ async function prepareTools({
868
917
  max_uses: args.maxUses,
869
918
  allowed_domains: args.allowedDomains,
870
919
  blocked_domains: args.blockedDomains,
871
- user_location: args.userLocation
920
+ user_location: args.userLocation,
921
+ cache_control: void 0
872
922
  });
873
923
  break;
874
924
  }
@@ -1094,11 +1144,13 @@ function convertToString(data) {
1094
1144
  async function convertToAnthropicMessagesPrompt({
1095
1145
  prompt,
1096
1146
  sendReasoning,
1097
- warnings
1147
+ warnings,
1148
+ cacheControlValidator
1098
1149
  }) {
1099
1150
  var _a, _b, _c, _d, _e;
1100
1151
  const betas = /* @__PURE__ */ new Set();
1101
1152
  const blocks = groupIntoBlocks(prompt);
1153
+ const validator = cacheControlValidator || new CacheControlValidator();
1102
1154
  let system = void 0;
1103
1155
  const messages = [];
1104
1156
  async function shouldEnableCitations(providerMetadata) {
@@ -1135,7 +1187,10 @@ async function convertToAnthropicMessagesPrompt({
1135
1187
  system = block.messages.map(({ content, providerOptions }) => ({
1136
1188
  type: "text",
1137
1189
  text: content,
1138
- cache_control: getCacheControl(providerOptions)
1190
+ cache_control: validator.getCacheControl(providerOptions, {
1191
+ type: "system message",
1192
+ canCache: true
1193
+ })
1139
1194
  }));
1140
1195
  break;
1141
1196
  }
@@ -1148,7 +1203,13 @@ async function convertToAnthropicMessagesPrompt({
1148
1203
  for (let j = 0; j < content.length; j++) {
1149
1204
  const part = content[j];
1150
1205
  const isLastPart = j === content.length - 1;
1151
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1206
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1207
+ type: "user message part",
1208
+ canCache: true
1209
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1210
+ type: "user message",
1211
+ canCache: true
1212
+ }) : void 0;
1152
1213
  switch (part.type) {
1153
1214
  case "text": {
1154
1215
  anthropicContent.push({
@@ -1236,7 +1297,13 @@ async function convertToAnthropicMessagesPrompt({
1236
1297
  for (let i2 = 0; i2 < content.length; i2++) {
1237
1298
  const part = content[i2];
1238
1299
  const isLastPart = i2 === content.length - 1;
1239
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1300
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1301
+ type: "tool result part",
1302
+ canCache: true
1303
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1304
+ type: "tool result message",
1305
+ canCache: true
1306
+ }) : void 0;
1240
1307
  const output = part.output;
1241
1308
  let contentValue;
1242
1309
  switch (output.type) {
@@ -1246,8 +1313,7 @@ async function convertToAnthropicMessagesPrompt({
1246
1313
  case "text":
1247
1314
  return {
1248
1315
  type: "text",
1249
- text: contentPart.text,
1250
- cache_control: void 0
1316
+ text: contentPart.text
1251
1317
  };
1252
1318
  case "media": {
1253
1319
  if (contentPart.mediaType.startsWith("image/")) {
@@ -1257,8 +1323,7 @@ async function convertToAnthropicMessagesPrompt({
1257
1323
  type: "base64",
1258
1324
  media_type: contentPart.mediaType,
1259
1325
  data: contentPart.data
1260
- },
1261
- cache_control: void 0
1326
+ }
1262
1327
  };
1263
1328
  }
1264
1329
  if (contentPart.mediaType === "application/pdf") {
@@ -1269,8 +1334,7 @@ async function convertToAnthropicMessagesPrompt({
1269
1334
  type: "base64",
1270
1335
  media_type: contentPart.mediaType,
1271
1336
  data: contentPart.data
1272
- },
1273
- cache_control: void 0
1337
+ }
1274
1338
  };
1275
1339
  }
1276
1340
  throw new UnsupportedFunctionalityError2({
@@ -1318,7 +1382,13 @@ async function convertToAnthropicMessagesPrompt({
1318
1382
  for (let k = 0; k < content.length; k++) {
1319
1383
  const part = content[k];
1320
1384
  const isLastContentPart = k === content.length - 1;
1321
- const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1385
+ const cacheControl = (_e = validator.getCacheControl(part.providerOptions, {
1386
+ type: "assistant message part",
1387
+ canCache: true
1388
+ })) != null ? _e : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1389
+ type: "assistant message",
1390
+ canCache: true
1391
+ }) : void 0;
1322
1392
  switch (part.type) {
1323
1393
  case "text": {
1324
1394
  anthropicContent.push({
@@ -1342,17 +1412,23 @@ async function convertToAnthropicMessagesPrompt({
1342
1412
  });
1343
1413
  if (reasoningMetadata != null) {
1344
1414
  if (reasoningMetadata.signature != null) {
1415
+ validator.getCacheControl(part.providerOptions, {
1416
+ type: "thinking block",
1417
+ canCache: false
1418
+ });
1345
1419
  anthropicContent.push({
1346
1420
  type: "thinking",
1347
1421
  thinking: part.text,
1348
- signature: reasoningMetadata.signature,
1349
- cache_control: cacheControl
1422
+ signature: reasoningMetadata.signature
1350
1423
  });
1351
1424
  } else if (reasoningMetadata.redactedData != null) {
1425
+ validator.getCacheControl(part.providerOptions, {
1426
+ type: "redacted thinking block",
1427
+ canCache: false
1428
+ });
1352
1429
  anthropicContent.push({
1353
1430
  type: "redacted_thinking",
1354
- data: reasoningMetadata.redactedData,
1355
- cache_control: cacheControl
1431
+ data: reasoningMetadata.redactedData
1356
1432
  });
1357
1433
  } else {
1358
1434
  warnings.push({
@@ -1727,10 +1803,12 @@ var AnthropicMessagesLanguageModel = class {
1727
1803
  providerOptions,
1728
1804
  schema: anthropicProviderOptions
1729
1805
  });
1806
+ const cacheControlValidator = new CacheControlValidator();
1730
1807
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1731
1808
  prompt,
1732
1809
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1733
- warnings
1810
+ warnings,
1811
+ cacheControlValidator
1734
1812
  });
1735
1813
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1736
1814
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1828,20 +1906,23 @@ var AnthropicMessagesLanguageModel = class {
1828
1906
  jsonResponseTool != null ? {
1829
1907
  tools: [jsonResponseTool],
1830
1908
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1831
- disableParallelToolUse: true
1909
+ disableParallelToolUse: true,
1910
+ cacheControlValidator
1832
1911
  } : {
1833
1912
  tools: tools != null ? tools : [],
1834
1913
  toolChoice,
1835
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
1914
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
1915
+ cacheControlValidator
1836
1916
  }
1837
1917
  );
1918
+ const cacheWarnings = cacheControlValidator.getWarnings();
1838
1919
  return {
1839
1920
  args: {
1840
1921
  ...baseArgs,
1841
1922
  tools: anthropicTools2,
1842
1923
  tool_choice: anthropicToolChoice
1843
1924
  },
1844
- warnings: [...warnings, ...toolWarnings],
1925
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1845
1926
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1846
1927
  usesJsonResponseTool: jsonResponseTool != null
1847
1928
  };