@ai-sdk/anthropic 3.0.0-beta.30 → 3.0.0-beta.31

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.
@@ -594,12 +594,46 @@ import {
594
594
  } from "@ai-sdk/provider";
595
595
 
596
596
  // src/get-cache-control.ts
597
+ var MAX_CACHE_BREAKPOINTS = 4;
597
598
  function getCacheControl(providerMetadata) {
598
599
  var _a;
599
600
  const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
600
601
  const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.cache_control;
601
602
  return cacheControlValue;
602
603
  }
604
+ var CacheControlValidator = class {
605
+ constructor() {
606
+ this.breakpointCount = 0;
607
+ this.warnings = [];
608
+ }
609
+ getCacheControl(providerMetadata, context) {
610
+ const cacheControlValue = getCacheControl(providerMetadata);
611
+ if (!cacheControlValue) {
612
+ return void 0;
613
+ }
614
+ if (!context.canCache) {
615
+ this.warnings.push({
616
+ type: "unsupported-setting",
617
+ setting: "cacheControl",
618
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
619
+ });
620
+ return void 0;
621
+ }
622
+ this.breakpointCount++;
623
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
624
+ this.warnings.push({
625
+ type: "unsupported-setting",
626
+ setting: "cacheControl",
627
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
628
+ });
629
+ return void 0;
630
+ }
631
+ return cacheControlValue;
632
+ }
633
+ getWarnings() {
634
+ return this.warnings;
635
+ }
636
+ };
603
637
 
604
638
  // src/tool/text-editor_20250728.ts
605
639
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -753,11 +787,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
753
787
  async function prepareTools({
754
788
  tools,
755
789
  toolChoice,
756
- disableParallelToolUse
790
+ disableParallelToolUse,
791
+ cacheControlValidator
757
792
  }) {
758
793
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
759
794
  const toolWarnings = [];
760
795
  const betas = /* @__PURE__ */ new Set();
796
+ const validator = cacheControlValidator || new CacheControlValidator();
761
797
  if (tools == null) {
762
798
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
763
799
  }
@@ -765,7 +801,10 @@ async function prepareTools({
765
801
  for (const tool of tools) {
766
802
  switch (tool.type) {
767
803
  case "function": {
768
- const cacheControl = getCacheControl(tool.providerOptions);
804
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
805
+ type: "tool definition",
806
+ canCache: true
807
+ });
769
808
  anthropicTools2.push({
770
809
  name: tool.name,
771
810
  description: tool.description,
@@ -780,7 +819,8 @@ async function prepareTools({
780
819
  betas.add("code-execution-2025-05-22");
781
820
  anthropicTools2.push({
782
821
  type: "code_execution_20250522",
783
- name: "code_execution"
822
+ name: "code_execution",
823
+ cache_control: void 0
784
824
  });
785
825
  break;
786
826
  }
@@ -799,7 +839,8 @@ async function prepareTools({
799
839
  type: "computer_20250124",
800
840
  display_width_px: tool.args.displayWidthPx,
801
841
  display_height_px: tool.args.displayHeightPx,
802
- display_number: tool.args.displayNumber
842
+ display_number: tool.args.displayNumber,
843
+ cache_control: void 0
803
844
  });
804
845
  break;
805
846
  }
@@ -810,7 +851,8 @@ async function prepareTools({
810
851
  type: "computer_20241022",
811
852
  display_width_px: tool.args.displayWidthPx,
812
853
  display_height_px: tool.args.displayHeightPx,
813
- display_number: tool.args.displayNumber
854
+ display_number: tool.args.displayNumber,
855
+ cache_control: void 0
814
856
  });
815
857
  break;
816
858
  }
@@ -818,7 +860,8 @@ async function prepareTools({
818
860
  betas.add("computer-use-2025-01-24");
819
861
  anthropicTools2.push({
820
862
  name: "str_replace_editor",
821
- type: "text_editor_20250124"
863
+ type: "text_editor_20250124",
864
+ cache_control: void 0
822
865
  });
823
866
  break;
824
867
  }
@@ -826,7 +869,8 @@ async function prepareTools({
826
869
  betas.add("computer-use-2024-10-22");
827
870
  anthropicTools2.push({
828
871
  name: "str_replace_editor",
829
- type: "text_editor_20241022"
872
+ type: "text_editor_20241022",
873
+ cache_control: void 0
830
874
  });
831
875
  break;
832
876
  }
@@ -834,7 +878,8 @@ async function prepareTools({
834
878
  betas.add("computer-use-2025-01-24");
835
879
  anthropicTools2.push({
836
880
  name: "str_replace_based_edit_tool",
837
- type: "text_editor_20250429"
881
+ type: "text_editor_20250429",
882
+ cache_control: void 0
838
883
  });
839
884
  break;
840
885
  }
@@ -846,7 +891,8 @@ async function prepareTools({
846
891
  anthropicTools2.push({
847
892
  name: "str_replace_based_edit_tool",
848
893
  type: "text_editor_20250728",
849
- max_characters: args.maxCharacters
894
+ max_characters: args.maxCharacters,
895
+ cache_control: void 0
850
896
  });
851
897
  break;
852
898
  }
@@ -854,7 +900,8 @@ async function prepareTools({
854
900
  betas.add("computer-use-2025-01-24");
855
901
  anthropicTools2.push({
856
902
  name: "bash",
857
- type: "bash_20250124"
903
+ type: "bash_20250124",
904
+ cache_control: void 0
858
905
  });
859
906
  break;
860
907
  }
@@ -862,7 +909,8 @@ async function prepareTools({
862
909
  betas.add("computer-use-2024-10-22");
863
910
  anthropicTools2.push({
864
911
  name: "bash",
865
- type: "bash_20241022"
912
+ type: "bash_20241022",
913
+ cache_control: void 0
866
914
  });
867
915
  break;
868
916
  }
@@ -887,7 +935,8 @@ async function prepareTools({
887
935
  allowed_domains: args.allowedDomains,
888
936
  blocked_domains: args.blockedDomains,
889
937
  citations: args.citations,
890
- max_content_tokens: args.maxContentTokens
938
+ max_content_tokens: args.maxContentTokens,
939
+ cache_control: void 0
891
940
  });
892
941
  break;
893
942
  }
@@ -902,7 +951,8 @@ async function prepareTools({
902
951
  max_uses: args.maxUses,
903
952
  allowed_domains: args.allowedDomains,
904
953
  blocked_domains: args.blockedDomains,
905
- user_location: args.userLocation
954
+ user_location: args.userLocation,
955
+ cache_control: void 0
906
956
  });
907
957
  break;
908
958
  }
@@ -1129,11 +1179,13 @@ function convertToString(data) {
1129
1179
  async function convertToAnthropicMessagesPrompt({
1130
1180
  prompt,
1131
1181
  sendReasoning,
1132
- warnings
1182
+ warnings,
1183
+ cacheControlValidator
1133
1184
  }) {
1134
1185
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1135
1186
  const betas = /* @__PURE__ */ new Set();
1136
1187
  const blocks = groupIntoBlocks(prompt);
1188
+ const validator = cacheControlValidator || new CacheControlValidator();
1137
1189
  let system = void 0;
1138
1190
  const messages = [];
1139
1191
  async function shouldEnableCitations(providerMetadata) {
@@ -1170,7 +1222,10 @@ async function convertToAnthropicMessagesPrompt({
1170
1222
  system = block.messages.map(({ content, providerOptions }) => ({
1171
1223
  type: "text",
1172
1224
  text: content,
1173
- cache_control: getCacheControl(providerOptions)
1225
+ cache_control: validator.getCacheControl(providerOptions, {
1226
+ type: "system message",
1227
+ canCache: true
1228
+ })
1174
1229
  }));
1175
1230
  break;
1176
1231
  }
@@ -1183,7 +1238,13 @@ async function convertToAnthropicMessagesPrompt({
1183
1238
  for (let j = 0; j < content.length; j++) {
1184
1239
  const part = content[j];
1185
1240
  const isLastPart = j === content.length - 1;
1186
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1241
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1242
+ type: "user message part",
1243
+ canCache: true
1244
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1245
+ type: "user message",
1246
+ canCache: true
1247
+ }) : void 0;
1187
1248
  switch (part.type) {
1188
1249
  case "text": {
1189
1250
  anthropicContent.push({
@@ -1271,7 +1332,13 @@ async function convertToAnthropicMessagesPrompt({
1271
1332
  for (let i2 = 0; i2 < content.length; i2++) {
1272
1333
  const part = content[i2];
1273
1334
  const isLastPart = i2 === content.length - 1;
1274
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1335
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1336
+ type: "tool result part",
1337
+ canCache: true
1338
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1339
+ type: "tool result message",
1340
+ canCache: true
1341
+ }) : void 0;
1275
1342
  const output = part.output;
1276
1343
  let contentValue;
1277
1344
  switch (output.type) {
@@ -1281,8 +1348,7 @@ async function convertToAnthropicMessagesPrompt({
1281
1348
  case "text":
1282
1349
  return {
1283
1350
  type: "text",
1284
- text: contentPart.text,
1285
- cache_control: cacheControl
1351
+ text: contentPart.text
1286
1352
  };
1287
1353
  case "image-data": {
1288
1354
  return {
@@ -1291,8 +1357,7 @@ async function convertToAnthropicMessagesPrompt({
1291
1357
  type: "base64",
1292
1358
  media_type: contentPart.mediaType,
1293
1359
  data: contentPart.data
1294
- },
1295
- cache_control: cacheControl
1360
+ }
1296
1361
  };
1297
1362
  }
1298
1363
  case "file-data": {
@@ -1304,8 +1369,7 @@ async function convertToAnthropicMessagesPrompt({
1304
1369
  type: "base64",
1305
1370
  media_type: contentPart.mediaType,
1306
1371
  data: contentPart.data
1307
- },
1308
- cache_control: cacheControl
1372
+ }
1309
1373
  };
1310
1374
  }
1311
1375
  warnings.push({
@@ -1366,7 +1430,13 @@ async function convertToAnthropicMessagesPrompt({
1366
1430
  for (let k = 0; k < content.length; k++) {
1367
1431
  const part = content[k];
1368
1432
  const isLastContentPart = k === content.length - 1;
1369
- const cacheControl = (_f = getCacheControl(part.providerOptions)) != null ? _f : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1433
+ const cacheControl = (_f = validator.getCacheControl(part.providerOptions, {
1434
+ type: "assistant message part",
1435
+ canCache: true
1436
+ })) != null ? _f : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1437
+ type: "assistant message",
1438
+ canCache: true
1439
+ }) : void 0;
1370
1440
  switch (part.type) {
1371
1441
  case "text": {
1372
1442
  anthropicContent.push({
@@ -1390,17 +1460,23 @@ async function convertToAnthropicMessagesPrompt({
1390
1460
  });
1391
1461
  if (reasoningMetadata != null) {
1392
1462
  if (reasoningMetadata.signature != null) {
1463
+ validator.getCacheControl(part.providerOptions, {
1464
+ type: "thinking block",
1465
+ canCache: false
1466
+ });
1393
1467
  anthropicContent.push({
1394
1468
  type: "thinking",
1395
1469
  thinking: part.text,
1396
- signature: reasoningMetadata.signature,
1397
- cache_control: cacheControl
1470
+ signature: reasoningMetadata.signature
1398
1471
  });
1399
1472
  } else if (reasoningMetadata.redactedData != null) {
1473
+ validator.getCacheControl(part.providerOptions, {
1474
+ type: "redacted thinking block",
1475
+ canCache: false
1476
+ });
1400
1477
  anthropicContent.push({
1401
1478
  type: "redacted_thinking",
1402
- data: reasoningMetadata.redactedData,
1403
- cache_control: cacheControl
1479
+ data: reasoningMetadata.redactedData
1404
1480
  });
1405
1481
  } else {
1406
1482
  warnings.push({
@@ -1813,10 +1889,12 @@ var AnthropicMessagesLanguageModel = class {
1813
1889
  providerOptions,
1814
1890
  schema: anthropicProviderOptions
1815
1891
  });
1892
+ const cacheControlValidator = new CacheControlValidator();
1816
1893
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1817
1894
  prompt,
1818
1895
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1819
- warnings
1896
+ warnings,
1897
+ cacheControlValidator
1820
1898
  });
1821
1899
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1822
1900
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1930,20 +2008,23 @@ var AnthropicMessagesLanguageModel = class {
1930
2008
  jsonResponseTool != null ? {
1931
2009
  tools: [jsonResponseTool],
1932
2010
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1933
- disableParallelToolUse: true
2011
+ disableParallelToolUse: true,
2012
+ cacheControlValidator
1934
2013
  } : {
1935
2014
  tools: tools != null ? tools : [],
1936
2015
  toolChoice,
1937
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
2016
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
2017
+ cacheControlValidator
1938
2018
  }
1939
2019
  );
2020
+ const cacheWarnings = cacheControlValidator.getWarnings();
1940
2021
  return {
1941
2022
  args: {
1942
2023
  ...baseArgs,
1943
2024
  tools: anthropicTools2,
1944
2025
  tool_choice: anthropicToolChoice
1945
2026
  },
1946
- warnings: [...warnings, ...toolWarnings],
2027
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1947
2028
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1948
2029
  usesJsonResponseTool: jsonResponseTool != null
1949
2030
  };