@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.
@@ -546,12 +546,46 @@ import {
546
546
  } from "@ai-sdk/provider";
547
547
 
548
548
  // src/get-cache-control.ts
549
+ var MAX_CACHE_BREAKPOINTS = 4;
549
550
  function getCacheControl(providerMetadata) {
550
551
  var _a;
551
552
  const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
552
553
  const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.cache_control;
553
554
  return cacheControlValue;
554
555
  }
556
+ var CacheControlValidator = class {
557
+ constructor() {
558
+ this.breakpointCount = 0;
559
+ this.warnings = [];
560
+ }
561
+ getCacheControl(providerMetadata, context) {
562
+ const cacheControlValue = getCacheControl(providerMetadata);
563
+ if (!cacheControlValue) {
564
+ return void 0;
565
+ }
566
+ if (!context.canCache) {
567
+ this.warnings.push({
568
+ type: "unsupported-setting",
569
+ setting: "cacheControl",
570
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
571
+ });
572
+ return void 0;
573
+ }
574
+ this.breakpointCount++;
575
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
576
+ this.warnings.push({
577
+ type: "unsupported-setting",
578
+ setting: "cacheControl",
579
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
580
+ });
581
+ return void 0;
582
+ }
583
+ return cacheControlValue;
584
+ }
585
+ getWarnings() {
586
+ return this.warnings;
587
+ }
588
+ };
555
589
 
556
590
  // src/tool/text-editor_20250728.ts
557
591
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -705,11 +739,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
705
739
  async function prepareTools({
706
740
  tools,
707
741
  toolChoice,
708
- disableParallelToolUse
742
+ disableParallelToolUse,
743
+ cacheControlValidator
709
744
  }) {
710
745
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
711
746
  const toolWarnings = [];
712
747
  const betas = /* @__PURE__ */ new Set();
748
+ const validator = cacheControlValidator || new CacheControlValidator();
713
749
  if (tools == null) {
714
750
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
715
751
  }
@@ -717,7 +753,10 @@ async function prepareTools({
717
753
  for (const tool of tools) {
718
754
  switch (tool.type) {
719
755
  case "function": {
720
- const cacheControl = getCacheControl(tool.providerOptions);
756
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
757
+ type: "tool definition",
758
+ canCache: true
759
+ });
721
760
  anthropicTools2.push({
722
761
  name: tool.name,
723
762
  description: tool.description,
@@ -732,7 +771,8 @@ async function prepareTools({
732
771
  betas.add("code-execution-2025-05-22");
733
772
  anthropicTools2.push({
734
773
  type: "code_execution_20250522",
735
- name: "code_execution"
774
+ name: "code_execution",
775
+ cache_control: void 0
736
776
  });
737
777
  break;
738
778
  }
@@ -751,7 +791,8 @@ async function prepareTools({
751
791
  type: "computer_20250124",
752
792
  display_width_px: tool.args.displayWidthPx,
753
793
  display_height_px: tool.args.displayHeightPx,
754
- display_number: tool.args.displayNumber
794
+ display_number: tool.args.displayNumber,
795
+ cache_control: void 0
755
796
  });
756
797
  break;
757
798
  }
@@ -762,7 +803,8 @@ async function prepareTools({
762
803
  type: "computer_20241022",
763
804
  display_width_px: tool.args.displayWidthPx,
764
805
  display_height_px: tool.args.displayHeightPx,
765
- display_number: tool.args.displayNumber
806
+ display_number: tool.args.displayNumber,
807
+ cache_control: void 0
766
808
  });
767
809
  break;
768
810
  }
@@ -770,7 +812,8 @@ async function prepareTools({
770
812
  betas.add("computer-use-2025-01-24");
771
813
  anthropicTools2.push({
772
814
  name: "str_replace_editor",
773
- type: "text_editor_20250124"
815
+ type: "text_editor_20250124",
816
+ cache_control: void 0
774
817
  });
775
818
  break;
776
819
  }
@@ -778,7 +821,8 @@ async function prepareTools({
778
821
  betas.add("computer-use-2024-10-22");
779
822
  anthropicTools2.push({
780
823
  name: "str_replace_editor",
781
- type: "text_editor_20241022"
824
+ type: "text_editor_20241022",
825
+ cache_control: void 0
782
826
  });
783
827
  break;
784
828
  }
@@ -786,7 +830,8 @@ async function prepareTools({
786
830
  betas.add("computer-use-2025-01-24");
787
831
  anthropicTools2.push({
788
832
  name: "str_replace_based_edit_tool",
789
- type: "text_editor_20250429"
833
+ type: "text_editor_20250429",
834
+ cache_control: void 0
790
835
  });
791
836
  break;
792
837
  }
@@ -798,7 +843,8 @@ async function prepareTools({
798
843
  anthropicTools2.push({
799
844
  name: "str_replace_based_edit_tool",
800
845
  type: "text_editor_20250728",
801
- max_characters: args.maxCharacters
846
+ max_characters: args.maxCharacters,
847
+ cache_control: void 0
802
848
  });
803
849
  break;
804
850
  }
@@ -806,7 +852,8 @@ async function prepareTools({
806
852
  betas.add("computer-use-2025-01-24");
807
853
  anthropicTools2.push({
808
854
  name: "bash",
809
- type: "bash_20250124"
855
+ type: "bash_20250124",
856
+ cache_control: void 0
810
857
  });
811
858
  break;
812
859
  }
@@ -814,7 +861,8 @@ async function prepareTools({
814
861
  betas.add("computer-use-2024-10-22");
815
862
  anthropicTools2.push({
816
863
  name: "bash",
817
- type: "bash_20241022"
864
+ type: "bash_20241022",
865
+ cache_control: void 0
818
866
  });
819
867
  break;
820
868
  }
@@ -839,7 +887,8 @@ async function prepareTools({
839
887
  allowed_domains: args.allowedDomains,
840
888
  blocked_domains: args.blockedDomains,
841
889
  citations: args.citations,
842
- max_content_tokens: args.maxContentTokens
890
+ max_content_tokens: args.maxContentTokens,
891
+ cache_control: void 0
843
892
  });
844
893
  break;
845
894
  }
@@ -854,7 +903,8 @@ async function prepareTools({
854
903
  max_uses: args.maxUses,
855
904
  allowed_domains: args.allowedDomains,
856
905
  blocked_domains: args.blockedDomains,
857
- user_location: args.userLocation
906
+ user_location: args.userLocation,
907
+ cache_control: void 0
858
908
  });
859
909
  break;
860
910
  }
@@ -1080,11 +1130,13 @@ function convertToString(data) {
1080
1130
  async function convertToAnthropicMessagesPrompt({
1081
1131
  prompt,
1082
1132
  sendReasoning,
1083
- warnings
1133
+ warnings,
1134
+ cacheControlValidator
1084
1135
  }) {
1085
1136
  var _a, _b, _c, _d, _e;
1086
1137
  const betas = /* @__PURE__ */ new Set();
1087
1138
  const blocks = groupIntoBlocks(prompt);
1139
+ const validator = cacheControlValidator || new CacheControlValidator();
1088
1140
  let system = void 0;
1089
1141
  const messages = [];
1090
1142
  async function shouldEnableCitations(providerMetadata) {
@@ -1121,7 +1173,10 @@ async function convertToAnthropicMessagesPrompt({
1121
1173
  system = block.messages.map(({ content, providerOptions }) => ({
1122
1174
  type: "text",
1123
1175
  text: content,
1124
- cache_control: getCacheControl(providerOptions)
1176
+ cache_control: validator.getCacheControl(providerOptions, {
1177
+ type: "system message",
1178
+ canCache: true
1179
+ })
1125
1180
  }));
1126
1181
  break;
1127
1182
  }
@@ -1134,7 +1189,13 @@ async function convertToAnthropicMessagesPrompt({
1134
1189
  for (let j = 0; j < content.length; j++) {
1135
1190
  const part = content[j];
1136
1191
  const isLastPart = j === content.length - 1;
1137
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1192
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1193
+ type: "user message part",
1194
+ canCache: true
1195
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1196
+ type: "user message",
1197
+ canCache: true
1198
+ }) : void 0;
1138
1199
  switch (part.type) {
1139
1200
  case "text": {
1140
1201
  anthropicContent.push({
@@ -1222,7 +1283,13 @@ async function convertToAnthropicMessagesPrompt({
1222
1283
  for (let i2 = 0; i2 < content.length; i2++) {
1223
1284
  const part = content[i2];
1224
1285
  const isLastPart = i2 === content.length - 1;
1225
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1286
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1287
+ type: "tool result part",
1288
+ canCache: true
1289
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1290
+ type: "tool result message",
1291
+ canCache: true
1292
+ }) : void 0;
1226
1293
  const output = part.output;
1227
1294
  let contentValue;
1228
1295
  switch (output.type) {
@@ -1232,8 +1299,7 @@ async function convertToAnthropicMessagesPrompt({
1232
1299
  case "text":
1233
1300
  return {
1234
1301
  type: "text",
1235
- text: contentPart.text,
1236
- cache_control: void 0
1302
+ text: contentPart.text
1237
1303
  };
1238
1304
  case "media": {
1239
1305
  if (contentPart.mediaType.startsWith("image/")) {
@@ -1243,8 +1309,7 @@ async function convertToAnthropicMessagesPrompt({
1243
1309
  type: "base64",
1244
1310
  media_type: contentPart.mediaType,
1245
1311
  data: contentPart.data
1246
- },
1247
- cache_control: void 0
1312
+ }
1248
1313
  };
1249
1314
  }
1250
1315
  if (contentPart.mediaType === "application/pdf") {
@@ -1255,8 +1320,7 @@ async function convertToAnthropicMessagesPrompt({
1255
1320
  type: "base64",
1256
1321
  media_type: contentPart.mediaType,
1257
1322
  data: contentPart.data
1258
- },
1259
- cache_control: void 0
1323
+ }
1260
1324
  };
1261
1325
  }
1262
1326
  throw new UnsupportedFunctionalityError2({
@@ -1304,7 +1368,13 @@ async function convertToAnthropicMessagesPrompt({
1304
1368
  for (let k = 0; k < content.length; k++) {
1305
1369
  const part = content[k];
1306
1370
  const isLastContentPart = k === content.length - 1;
1307
- const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1371
+ const cacheControl = (_e = validator.getCacheControl(part.providerOptions, {
1372
+ type: "assistant message part",
1373
+ canCache: true
1374
+ })) != null ? _e : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1375
+ type: "assistant message",
1376
+ canCache: true
1377
+ }) : void 0;
1308
1378
  switch (part.type) {
1309
1379
  case "text": {
1310
1380
  anthropicContent.push({
@@ -1328,17 +1398,23 @@ async function convertToAnthropicMessagesPrompt({
1328
1398
  });
1329
1399
  if (reasoningMetadata != null) {
1330
1400
  if (reasoningMetadata.signature != null) {
1401
+ validator.getCacheControl(part.providerOptions, {
1402
+ type: "thinking block",
1403
+ canCache: false
1404
+ });
1331
1405
  anthropicContent.push({
1332
1406
  type: "thinking",
1333
1407
  thinking: part.text,
1334
- signature: reasoningMetadata.signature,
1335
- cache_control: cacheControl
1408
+ signature: reasoningMetadata.signature
1336
1409
  });
1337
1410
  } else if (reasoningMetadata.redactedData != null) {
1411
+ validator.getCacheControl(part.providerOptions, {
1412
+ type: "redacted thinking block",
1413
+ canCache: false
1414
+ });
1338
1415
  anthropicContent.push({
1339
1416
  type: "redacted_thinking",
1340
- data: reasoningMetadata.redactedData,
1341
- cache_control: cacheControl
1417
+ data: reasoningMetadata.redactedData
1342
1418
  });
1343
1419
  } else {
1344
1420
  warnings.push({
@@ -1713,10 +1789,12 @@ var AnthropicMessagesLanguageModel = class {
1713
1789
  providerOptions,
1714
1790
  schema: anthropicProviderOptions
1715
1791
  });
1792
+ const cacheControlValidator = new CacheControlValidator();
1716
1793
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1717
1794
  prompt,
1718
1795
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1719
- warnings
1796
+ warnings,
1797
+ cacheControlValidator
1720
1798
  });
1721
1799
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1722
1800
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1814,20 +1892,23 @@ var AnthropicMessagesLanguageModel = class {
1814
1892
  jsonResponseTool != null ? {
1815
1893
  tools: [jsonResponseTool],
1816
1894
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1817
- disableParallelToolUse: true
1895
+ disableParallelToolUse: true,
1896
+ cacheControlValidator
1818
1897
  } : {
1819
1898
  tools: tools != null ? tools : [],
1820
1899
  toolChoice,
1821
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
1900
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
1901
+ cacheControlValidator
1822
1902
  }
1823
1903
  );
1904
+ const cacheWarnings = cacheControlValidator.getWarnings();
1824
1905
  return {
1825
1906
  args: {
1826
1907
  ...baseArgs,
1827
1908
  tools: anthropicTools2,
1828
1909
  tool_choice: anthropicToolChoice
1829
1910
  },
1830
- warnings: [...warnings, ...toolWarnings],
1911
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1831
1912
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1832
1913
  usesJsonResponseTool: jsonResponseTool != null
1833
1914
  };