@ai-sdk/anthropic 2.0.31 → 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.
@@ -522,6 +522,21 @@ var anthropicProviderOptions = z3.object({
522
522
  cacheControl: z3.object({
523
523
  type: z3.literal("ephemeral"),
524
524
  ttl: z3.union([z3.literal("5m"), z3.literal("1h")]).optional()
525
+ }).optional(),
526
+ /**
527
+ * Agent Skills configuration. Skills enable Claude to perform specialized tasks
528
+ * like document processing (PPTX, DOCX, PDF, XLSX) and data analysis.
529
+ * Requires code execution tool to be enabled.
530
+ */
531
+ container: z3.object({
532
+ id: z3.string().optional(),
533
+ skills: z3.array(
534
+ z3.object({
535
+ type: z3.union([z3.literal("anthropic"), z3.literal("custom")]),
536
+ skillId: z3.string(),
537
+ version: z3.string().optional()
538
+ })
539
+ ).optional()
525
540
  }).optional()
526
541
  });
527
542
 
@@ -531,12 +546,46 @@ import {
531
546
  } from "@ai-sdk/provider";
532
547
 
533
548
  // src/get-cache-control.ts
549
+ var MAX_CACHE_BREAKPOINTS = 4;
534
550
  function getCacheControl(providerMetadata) {
535
551
  var _a;
536
552
  const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
537
553
  const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.cache_control;
538
554
  return cacheControlValue;
539
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
+ };
540
589
 
541
590
  // src/tool/text-editor_20250728.ts
542
591
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -690,11 +739,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
690
739
  async function prepareTools({
691
740
  tools,
692
741
  toolChoice,
693
- disableParallelToolUse
742
+ disableParallelToolUse,
743
+ cacheControlValidator
694
744
  }) {
695
745
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
696
746
  const toolWarnings = [];
697
747
  const betas = /* @__PURE__ */ new Set();
748
+ const validator = cacheControlValidator || new CacheControlValidator();
698
749
  if (tools == null) {
699
750
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
700
751
  }
@@ -702,7 +753,10 @@ async function prepareTools({
702
753
  for (const tool of tools) {
703
754
  switch (tool.type) {
704
755
  case "function": {
705
- const cacheControl = getCacheControl(tool.providerOptions);
756
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
757
+ type: "tool definition",
758
+ canCache: true
759
+ });
706
760
  anthropicTools2.push({
707
761
  name: tool.name,
708
762
  description: tool.description,
@@ -717,7 +771,8 @@ async function prepareTools({
717
771
  betas.add("code-execution-2025-05-22");
718
772
  anthropicTools2.push({
719
773
  type: "code_execution_20250522",
720
- name: "code_execution"
774
+ name: "code_execution",
775
+ cache_control: void 0
721
776
  });
722
777
  break;
723
778
  }
@@ -736,7 +791,8 @@ async function prepareTools({
736
791
  type: "computer_20250124",
737
792
  display_width_px: tool.args.displayWidthPx,
738
793
  display_height_px: tool.args.displayHeightPx,
739
- display_number: tool.args.displayNumber
794
+ display_number: tool.args.displayNumber,
795
+ cache_control: void 0
740
796
  });
741
797
  break;
742
798
  }
@@ -747,7 +803,8 @@ async function prepareTools({
747
803
  type: "computer_20241022",
748
804
  display_width_px: tool.args.displayWidthPx,
749
805
  display_height_px: tool.args.displayHeightPx,
750
- display_number: tool.args.displayNumber
806
+ display_number: tool.args.displayNumber,
807
+ cache_control: void 0
751
808
  });
752
809
  break;
753
810
  }
@@ -755,7 +812,8 @@ async function prepareTools({
755
812
  betas.add("computer-use-2025-01-24");
756
813
  anthropicTools2.push({
757
814
  name: "str_replace_editor",
758
- type: "text_editor_20250124"
815
+ type: "text_editor_20250124",
816
+ cache_control: void 0
759
817
  });
760
818
  break;
761
819
  }
@@ -763,7 +821,8 @@ async function prepareTools({
763
821
  betas.add("computer-use-2024-10-22");
764
822
  anthropicTools2.push({
765
823
  name: "str_replace_editor",
766
- type: "text_editor_20241022"
824
+ type: "text_editor_20241022",
825
+ cache_control: void 0
767
826
  });
768
827
  break;
769
828
  }
@@ -771,7 +830,8 @@ async function prepareTools({
771
830
  betas.add("computer-use-2025-01-24");
772
831
  anthropicTools2.push({
773
832
  name: "str_replace_based_edit_tool",
774
- type: "text_editor_20250429"
833
+ type: "text_editor_20250429",
834
+ cache_control: void 0
775
835
  });
776
836
  break;
777
837
  }
@@ -783,7 +843,8 @@ async function prepareTools({
783
843
  anthropicTools2.push({
784
844
  name: "str_replace_based_edit_tool",
785
845
  type: "text_editor_20250728",
786
- max_characters: args.maxCharacters
846
+ max_characters: args.maxCharacters,
847
+ cache_control: void 0
787
848
  });
788
849
  break;
789
850
  }
@@ -791,7 +852,8 @@ async function prepareTools({
791
852
  betas.add("computer-use-2025-01-24");
792
853
  anthropicTools2.push({
793
854
  name: "bash",
794
- type: "bash_20250124"
855
+ type: "bash_20250124",
856
+ cache_control: void 0
795
857
  });
796
858
  break;
797
859
  }
@@ -799,7 +861,8 @@ async function prepareTools({
799
861
  betas.add("computer-use-2024-10-22");
800
862
  anthropicTools2.push({
801
863
  name: "bash",
802
- type: "bash_20241022"
864
+ type: "bash_20241022",
865
+ cache_control: void 0
803
866
  });
804
867
  break;
805
868
  }
@@ -824,7 +887,8 @@ async function prepareTools({
824
887
  allowed_domains: args.allowedDomains,
825
888
  blocked_domains: args.blockedDomains,
826
889
  citations: args.citations,
827
- max_content_tokens: args.maxContentTokens
890
+ max_content_tokens: args.maxContentTokens,
891
+ cache_control: void 0
828
892
  });
829
893
  break;
830
894
  }
@@ -839,7 +903,8 @@ async function prepareTools({
839
903
  max_uses: args.maxUses,
840
904
  allowed_domains: args.allowedDomains,
841
905
  blocked_domains: args.blockedDomains,
842
- user_location: args.userLocation
906
+ user_location: args.userLocation,
907
+ cache_control: void 0
843
908
  });
844
909
  break;
845
910
  }
@@ -1065,11 +1130,13 @@ function convertToString(data) {
1065
1130
  async function convertToAnthropicMessagesPrompt({
1066
1131
  prompt,
1067
1132
  sendReasoning,
1068
- warnings
1133
+ warnings,
1134
+ cacheControlValidator
1069
1135
  }) {
1070
1136
  var _a, _b, _c, _d, _e;
1071
1137
  const betas = /* @__PURE__ */ new Set();
1072
1138
  const blocks = groupIntoBlocks(prompt);
1139
+ const validator = cacheControlValidator || new CacheControlValidator();
1073
1140
  let system = void 0;
1074
1141
  const messages = [];
1075
1142
  async function shouldEnableCitations(providerMetadata) {
@@ -1106,7 +1173,10 @@ async function convertToAnthropicMessagesPrompt({
1106
1173
  system = block.messages.map(({ content, providerOptions }) => ({
1107
1174
  type: "text",
1108
1175
  text: content,
1109
- cache_control: getCacheControl(providerOptions)
1176
+ cache_control: validator.getCacheControl(providerOptions, {
1177
+ type: "system message",
1178
+ canCache: true
1179
+ })
1110
1180
  }));
1111
1181
  break;
1112
1182
  }
@@ -1119,7 +1189,13 @@ async function convertToAnthropicMessagesPrompt({
1119
1189
  for (let j = 0; j < content.length; j++) {
1120
1190
  const part = content[j];
1121
1191
  const isLastPart = j === content.length - 1;
1122
- 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;
1123
1199
  switch (part.type) {
1124
1200
  case "text": {
1125
1201
  anthropicContent.push({
@@ -1207,7 +1283,13 @@ async function convertToAnthropicMessagesPrompt({
1207
1283
  for (let i2 = 0; i2 < content.length; i2++) {
1208
1284
  const part = content[i2];
1209
1285
  const isLastPart = i2 === content.length - 1;
1210
- 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;
1211
1293
  const output = part.output;
1212
1294
  let contentValue;
1213
1295
  switch (output.type) {
@@ -1217,8 +1299,7 @@ async function convertToAnthropicMessagesPrompt({
1217
1299
  case "text":
1218
1300
  return {
1219
1301
  type: "text",
1220
- text: contentPart.text,
1221
- cache_control: void 0
1302
+ text: contentPart.text
1222
1303
  };
1223
1304
  case "media": {
1224
1305
  if (contentPart.mediaType.startsWith("image/")) {
@@ -1228,8 +1309,7 @@ async function convertToAnthropicMessagesPrompt({
1228
1309
  type: "base64",
1229
1310
  media_type: contentPart.mediaType,
1230
1311
  data: contentPart.data
1231
- },
1232
- cache_control: void 0
1312
+ }
1233
1313
  };
1234
1314
  }
1235
1315
  if (contentPart.mediaType === "application/pdf") {
@@ -1240,8 +1320,7 @@ async function convertToAnthropicMessagesPrompt({
1240
1320
  type: "base64",
1241
1321
  media_type: contentPart.mediaType,
1242
1322
  data: contentPart.data
1243
- },
1244
- cache_control: void 0
1323
+ }
1245
1324
  };
1246
1325
  }
1247
1326
  throw new UnsupportedFunctionalityError2({
@@ -1289,7 +1368,13 @@ async function convertToAnthropicMessagesPrompt({
1289
1368
  for (let k = 0; k < content.length; k++) {
1290
1369
  const part = content[k];
1291
1370
  const isLastContentPart = k === content.length - 1;
1292
- 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;
1293
1378
  switch (part.type) {
1294
1379
  case "text": {
1295
1380
  anthropicContent.push({
@@ -1313,17 +1398,23 @@ async function convertToAnthropicMessagesPrompt({
1313
1398
  });
1314
1399
  if (reasoningMetadata != null) {
1315
1400
  if (reasoningMetadata.signature != null) {
1401
+ validator.getCacheControl(part.providerOptions, {
1402
+ type: "thinking block",
1403
+ canCache: false
1404
+ });
1316
1405
  anthropicContent.push({
1317
1406
  type: "thinking",
1318
1407
  thinking: part.text,
1319
- signature: reasoningMetadata.signature,
1320
- cache_control: cacheControl
1408
+ signature: reasoningMetadata.signature
1321
1409
  });
1322
1410
  } else if (reasoningMetadata.redactedData != null) {
1411
+ validator.getCacheControl(part.providerOptions, {
1412
+ type: "redacted thinking block",
1413
+ canCache: false
1414
+ });
1323
1415
  anthropicContent.push({
1324
1416
  type: "redacted_thinking",
1325
- data: reasoningMetadata.redactedData,
1326
- cache_control: cacheControl
1417
+ data: reasoningMetadata.redactedData
1327
1418
  });
1328
1419
  } else {
1329
1420
  warnings.push({
@@ -1652,7 +1743,7 @@ var AnthropicMessagesLanguageModel = class {
1652
1743
  toolChoice,
1653
1744
  providerOptions
1654
1745
  }) {
1655
- var _a, _b, _c;
1746
+ var _a, _b, _c, _d;
1656
1747
  const warnings = [];
1657
1748
  if (frequencyPenalty != null) {
1658
1749
  warnings.push({
@@ -1698,10 +1789,12 @@ var AnthropicMessagesLanguageModel = class {
1698
1789
  providerOptions,
1699
1790
  schema: anthropicProviderOptions
1700
1791
  });
1701
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1792
+ const cacheControlValidator = new CacheControlValidator();
1793
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1702
1794
  prompt,
1703
1795
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1704
- warnings
1796
+ warnings,
1797
+ cacheControlValidator
1705
1798
  });
1706
1799
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1707
1800
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1720,6 +1813,17 @@ var AnthropicMessagesLanguageModel = class {
1720
1813
  ...isThinking && {
1721
1814
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1722
1815
  },
1816
+ // container with agent skills:
1817
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.container) && {
1818
+ container: {
1819
+ id: anthropicOptions.container.id,
1820
+ skills: (_d = anthropicOptions.container.skills) == null ? void 0 : _d.map((skill) => ({
1821
+ type: skill.type,
1822
+ skill_id: skill.skillId,
1823
+ version: skill.version
1824
+ }))
1825
+ }
1826
+ },
1723
1827
  // prompt:
1724
1828
  system: messagesPrompt.system,
1725
1829
  messages: messagesPrompt.messages
@@ -1761,11 +1865,24 @@ var AnthropicMessagesLanguageModel = class {
1761
1865
  warnings.push({
1762
1866
  type: "unsupported-setting",
1763
1867
  setting: "maxOutputTokens",
1764
- details: `${maxTokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1868
+ details: `${baseArgs.max_tokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1765
1869
  });
1766
1870
  }
1767
1871
  baseArgs.max_tokens = maxOutputTokensForModel;
1768
1872
  }
1873
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
1874
+ betas.add("code-execution-2025-08-25");
1875
+ betas.add("skills-2025-10-02");
1876
+ betas.add("files-api-2025-04-14");
1877
+ if (!(tools == null ? void 0 : tools.some(
1878
+ (tool) => tool.type === "provider-defined" && tool.id === "anthropic.code_execution_20250825"
1879
+ ))) {
1880
+ warnings.push({
1881
+ type: "other",
1882
+ message: "code execution tool is required when using skills"
1883
+ });
1884
+ }
1885
+ }
1769
1886
  const {
1770
1887
  tools: anthropicTools2,
1771
1888
  toolChoice: anthropicToolChoice,
@@ -1775,21 +1892,24 @@ var AnthropicMessagesLanguageModel = class {
1775
1892
  jsonResponseTool != null ? {
1776
1893
  tools: [jsonResponseTool],
1777
1894
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1778
- disableParallelToolUse: true
1895
+ disableParallelToolUse: true,
1896
+ cacheControlValidator
1779
1897
  } : {
1780
1898
  tools: tools != null ? tools : [],
1781
1899
  toolChoice,
1782
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
1900
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
1901
+ cacheControlValidator
1783
1902
  }
1784
1903
  );
1904
+ const cacheWarnings = cacheControlValidator.getWarnings();
1785
1905
  return {
1786
1906
  args: {
1787
1907
  ...baseArgs,
1788
1908
  tools: anthropicTools2,
1789
1909
  tool_choice: anthropicToolChoice
1790
1910
  },
1791
- warnings: [...warnings, ...toolWarnings],
1792
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1911
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1912
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1793
1913
  usesJsonResponseTool: jsonResponseTool != null
1794
1914
  };
1795
1915
  }