@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.
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 ? "3.0.0-beta.30" : "0.0.0-test";
13
+ var VERSION = true ? "3.0.0-beta.31" : "0.0.0-test";
14
14
 
15
15
  // src/anthropic-messages-language-model.ts
16
16
  import {
@@ -608,12 +608,46 @@ import {
608
608
  } from "@ai-sdk/provider";
609
609
 
610
610
  // src/get-cache-control.ts
611
+ var MAX_CACHE_BREAKPOINTS = 4;
611
612
  function getCacheControl(providerMetadata) {
612
613
  var _a;
613
614
  const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
614
615
  const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control;
615
616
  return cacheControlValue;
616
617
  }
618
+ var CacheControlValidator = class {
619
+ constructor() {
620
+ this.breakpointCount = 0;
621
+ this.warnings = [];
622
+ }
623
+ getCacheControl(providerMetadata, context) {
624
+ const cacheControlValue = getCacheControl(providerMetadata);
625
+ if (!cacheControlValue) {
626
+ return void 0;
627
+ }
628
+ if (!context.canCache) {
629
+ this.warnings.push({
630
+ type: "unsupported-setting",
631
+ setting: "cacheControl",
632
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
633
+ });
634
+ return void 0;
635
+ }
636
+ this.breakpointCount++;
637
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
638
+ this.warnings.push({
639
+ type: "unsupported-setting",
640
+ setting: "cacheControl",
641
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
642
+ });
643
+ return void 0;
644
+ }
645
+ return cacheControlValue;
646
+ }
647
+ getWarnings() {
648
+ return this.warnings;
649
+ }
650
+ };
617
651
 
618
652
  // src/tool/text-editor_20250728.ts
619
653
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -767,11 +801,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
767
801
  async function prepareTools({
768
802
  tools,
769
803
  toolChoice,
770
- disableParallelToolUse
804
+ disableParallelToolUse,
805
+ cacheControlValidator
771
806
  }) {
772
807
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
773
808
  const toolWarnings = [];
774
809
  const betas = /* @__PURE__ */ new Set();
810
+ const validator = cacheControlValidator || new CacheControlValidator();
775
811
  if (tools == null) {
776
812
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
777
813
  }
@@ -779,7 +815,10 @@ async function prepareTools({
779
815
  for (const tool of tools) {
780
816
  switch (tool.type) {
781
817
  case "function": {
782
- const cacheControl = getCacheControl(tool.providerOptions);
818
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
819
+ type: "tool definition",
820
+ canCache: true
821
+ });
783
822
  anthropicTools2.push({
784
823
  name: tool.name,
785
824
  description: tool.description,
@@ -794,7 +833,8 @@ async function prepareTools({
794
833
  betas.add("code-execution-2025-05-22");
795
834
  anthropicTools2.push({
796
835
  type: "code_execution_20250522",
797
- name: "code_execution"
836
+ name: "code_execution",
837
+ cache_control: void 0
798
838
  });
799
839
  break;
800
840
  }
@@ -813,7 +853,8 @@ async function prepareTools({
813
853
  type: "computer_20250124",
814
854
  display_width_px: tool.args.displayWidthPx,
815
855
  display_height_px: tool.args.displayHeightPx,
816
- display_number: tool.args.displayNumber
856
+ display_number: tool.args.displayNumber,
857
+ cache_control: void 0
817
858
  });
818
859
  break;
819
860
  }
@@ -824,7 +865,8 @@ async function prepareTools({
824
865
  type: "computer_20241022",
825
866
  display_width_px: tool.args.displayWidthPx,
826
867
  display_height_px: tool.args.displayHeightPx,
827
- display_number: tool.args.displayNumber
868
+ display_number: tool.args.displayNumber,
869
+ cache_control: void 0
828
870
  });
829
871
  break;
830
872
  }
@@ -832,7 +874,8 @@ async function prepareTools({
832
874
  betas.add("computer-use-2025-01-24");
833
875
  anthropicTools2.push({
834
876
  name: "str_replace_editor",
835
- type: "text_editor_20250124"
877
+ type: "text_editor_20250124",
878
+ cache_control: void 0
836
879
  });
837
880
  break;
838
881
  }
@@ -840,7 +883,8 @@ async function prepareTools({
840
883
  betas.add("computer-use-2024-10-22");
841
884
  anthropicTools2.push({
842
885
  name: "str_replace_editor",
843
- type: "text_editor_20241022"
886
+ type: "text_editor_20241022",
887
+ cache_control: void 0
844
888
  });
845
889
  break;
846
890
  }
@@ -848,7 +892,8 @@ async function prepareTools({
848
892
  betas.add("computer-use-2025-01-24");
849
893
  anthropicTools2.push({
850
894
  name: "str_replace_based_edit_tool",
851
- type: "text_editor_20250429"
895
+ type: "text_editor_20250429",
896
+ cache_control: void 0
852
897
  });
853
898
  break;
854
899
  }
@@ -860,7 +905,8 @@ async function prepareTools({
860
905
  anthropicTools2.push({
861
906
  name: "str_replace_based_edit_tool",
862
907
  type: "text_editor_20250728",
863
- max_characters: args.maxCharacters
908
+ max_characters: args.maxCharacters,
909
+ cache_control: void 0
864
910
  });
865
911
  break;
866
912
  }
@@ -868,7 +914,8 @@ async function prepareTools({
868
914
  betas.add("computer-use-2025-01-24");
869
915
  anthropicTools2.push({
870
916
  name: "bash",
871
- type: "bash_20250124"
917
+ type: "bash_20250124",
918
+ cache_control: void 0
872
919
  });
873
920
  break;
874
921
  }
@@ -876,7 +923,8 @@ async function prepareTools({
876
923
  betas.add("computer-use-2024-10-22");
877
924
  anthropicTools2.push({
878
925
  name: "bash",
879
- type: "bash_20241022"
926
+ type: "bash_20241022",
927
+ cache_control: void 0
880
928
  });
881
929
  break;
882
930
  }
@@ -901,7 +949,8 @@ async function prepareTools({
901
949
  allowed_domains: args.allowedDomains,
902
950
  blocked_domains: args.blockedDomains,
903
951
  citations: args.citations,
904
- max_content_tokens: args.maxContentTokens
952
+ max_content_tokens: args.maxContentTokens,
953
+ cache_control: void 0
905
954
  });
906
955
  break;
907
956
  }
@@ -916,7 +965,8 @@ async function prepareTools({
916
965
  max_uses: args.maxUses,
917
966
  allowed_domains: args.allowedDomains,
918
967
  blocked_domains: args.blockedDomains,
919
- user_location: args.userLocation
968
+ user_location: args.userLocation,
969
+ cache_control: void 0
920
970
  });
921
971
  break;
922
972
  }
@@ -1143,11 +1193,13 @@ function convertToString(data) {
1143
1193
  async function convertToAnthropicMessagesPrompt({
1144
1194
  prompt,
1145
1195
  sendReasoning,
1146
- warnings
1196
+ warnings,
1197
+ cacheControlValidator
1147
1198
  }) {
1148
1199
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1149
1200
  const betas = /* @__PURE__ */ new Set();
1150
1201
  const blocks = groupIntoBlocks(prompt);
1202
+ const validator = cacheControlValidator || new CacheControlValidator();
1151
1203
  let system = void 0;
1152
1204
  const messages = [];
1153
1205
  async function shouldEnableCitations(providerMetadata) {
@@ -1184,7 +1236,10 @@ async function convertToAnthropicMessagesPrompt({
1184
1236
  system = block.messages.map(({ content, providerOptions }) => ({
1185
1237
  type: "text",
1186
1238
  text: content,
1187
- cache_control: getCacheControl(providerOptions)
1239
+ cache_control: validator.getCacheControl(providerOptions, {
1240
+ type: "system message",
1241
+ canCache: true
1242
+ })
1188
1243
  }));
1189
1244
  break;
1190
1245
  }
@@ -1197,7 +1252,13 @@ async function convertToAnthropicMessagesPrompt({
1197
1252
  for (let j = 0; j < content.length; j++) {
1198
1253
  const part = content[j];
1199
1254
  const isLastPart = j === content.length - 1;
1200
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1255
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1256
+ type: "user message part",
1257
+ canCache: true
1258
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1259
+ type: "user message",
1260
+ canCache: true
1261
+ }) : void 0;
1201
1262
  switch (part.type) {
1202
1263
  case "text": {
1203
1264
  anthropicContent.push({
@@ -1285,7 +1346,13 @@ async function convertToAnthropicMessagesPrompt({
1285
1346
  for (let i2 = 0; i2 < content.length; i2++) {
1286
1347
  const part = content[i2];
1287
1348
  const isLastPart = i2 === content.length - 1;
1288
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1349
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1350
+ type: "tool result part",
1351
+ canCache: true
1352
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1353
+ type: "tool result message",
1354
+ canCache: true
1355
+ }) : void 0;
1289
1356
  const output = part.output;
1290
1357
  let contentValue;
1291
1358
  switch (output.type) {
@@ -1295,8 +1362,7 @@ async function convertToAnthropicMessagesPrompt({
1295
1362
  case "text":
1296
1363
  return {
1297
1364
  type: "text",
1298
- text: contentPart.text,
1299
- cache_control: cacheControl
1365
+ text: contentPart.text
1300
1366
  };
1301
1367
  case "image-data": {
1302
1368
  return {
@@ -1305,8 +1371,7 @@ async function convertToAnthropicMessagesPrompt({
1305
1371
  type: "base64",
1306
1372
  media_type: contentPart.mediaType,
1307
1373
  data: contentPart.data
1308
- },
1309
- cache_control: cacheControl
1374
+ }
1310
1375
  };
1311
1376
  }
1312
1377
  case "file-data": {
@@ -1318,8 +1383,7 @@ async function convertToAnthropicMessagesPrompt({
1318
1383
  type: "base64",
1319
1384
  media_type: contentPart.mediaType,
1320
1385
  data: contentPart.data
1321
- },
1322
- cache_control: cacheControl
1386
+ }
1323
1387
  };
1324
1388
  }
1325
1389
  warnings.push({
@@ -1380,7 +1444,13 @@ async function convertToAnthropicMessagesPrompt({
1380
1444
  for (let k = 0; k < content.length; k++) {
1381
1445
  const part = content[k];
1382
1446
  const isLastContentPart = k === content.length - 1;
1383
- const cacheControl = (_f = getCacheControl(part.providerOptions)) != null ? _f : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1447
+ const cacheControl = (_f = validator.getCacheControl(part.providerOptions, {
1448
+ type: "assistant message part",
1449
+ canCache: true
1450
+ })) != null ? _f : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1451
+ type: "assistant message",
1452
+ canCache: true
1453
+ }) : void 0;
1384
1454
  switch (part.type) {
1385
1455
  case "text": {
1386
1456
  anthropicContent.push({
@@ -1404,17 +1474,23 @@ async function convertToAnthropicMessagesPrompt({
1404
1474
  });
1405
1475
  if (reasoningMetadata != null) {
1406
1476
  if (reasoningMetadata.signature != null) {
1477
+ validator.getCacheControl(part.providerOptions, {
1478
+ type: "thinking block",
1479
+ canCache: false
1480
+ });
1407
1481
  anthropicContent.push({
1408
1482
  type: "thinking",
1409
1483
  thinking: part.text,
1410
- signature: reasoningMetadata.signature,
1411
- cache_control: cacheControl
1484
+ signature: reasoningMetadata.signature
1412
1485
  });
1413
1486
  } else if (reasoningMetadata.redactedData != null) {
1487
+ validator.getCacheControl(part.providerOptions, {
1488
+ type: "redacted thinking block",
1489
+ canCache: false
1490
+ });
1414
1491
  anthropicContent.push({
1415
1492
  type: "redacted_thinking",
1416
- data: reasoningMetadata.redactedData,
1417
- cache_control: cacheControl
1493
+ data: reasoningMetadata.redactedData
1418
1494
  });
1419
1495
  } else {
1420
1496
  warnings.push({
@@ -1827,10 +1903,12 @@ var AnthropicMessagesLanguageModel = class {
1827
1903
  providerOptions,
1828
1904
  schema: anthropicProviderOptions
1829
1905
  });
1906
+ const cacheControlValidator = new CacheControlValidator();
1830
1907
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1831
1908
  prompt,
1832
1909
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1833
- warnings
1910
+ warnings,
1911
+ cacheControlValidator
1834
1912
  });
1835
1913
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1836
1914
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1944,20 +2022,23 @@ var AnthropicMessagesLanguageModel = class {
1944
2022
  jsonResponseTool != null ? {
1945
2023
  tools: [jsonResponseTool],
1946
2024
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1947
- disableParallelToolUse: true
2025
+ disableParallelToolUse: true,
2026
+ cacheControlValidator
1948
2027
  } : {
1949
2028
  tools: tools != null ? tools : [],
1950
2029
  toolChoice,
1951
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
2030
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
2031
+ cacheControlValidator
1952
2032
  }
1953
2033
  );
2034
+ const cacheWarnings = cacheControlValidator.getWarnings();
1954
2035
  return {
1955
2036
  args: {
1956
2037
  ...baseArgs,
1957
2038
  tools: anthropicTools2,
1958
2039
  tool_choice: anthropicToolChoice
1959
2040
  },
1960
- warnings: [...warnings, ...toolWarnings],
2041
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1961
2042
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1962
2043
  usesJsonResponseTool: jsonResponseTool != null
1963
2044
  };