@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.
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.31" : "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 {
@@ -536,6 +536,21 @@ var anthropicProviderOptions = z3.object({
536
536
  cacheControl: z3.object({
537
537
  type: z3.literal("ephemeral"),
538
538
  ttl: z3.union([z3.literal("5m"), z3.literal("1h")]).optional()
539
+ }).optional(),
540
+ /**
541
+ * Agent Skills configuration. Skills enable Claude to perform specialized tasks
542
+ * like document processing (PPTX, DOCX, PDF, XLSX) and data analysis.
543
+ * Requires code execution tool to be enabled.
544
+ */
545
+ container: z3.object({
546
+ id: z3.string().optional(),
547
+ skills: z3.array(
548
+ z3.object({
549
+ type: z3.union([z3.literal("anthropic"), z3.literal("custom")]),
550
+ skillId: z3.string(),
551
+ version: z3.string().optional()
552
+ })
553
+ ).optional()
539
554
  }).optional()
540
555
  });
541
556
 
@@ -545,12 +560,46 @@ import {
545
560
  } from "@ai-sdk/provider";
546
561
 
547
562
  // src/get-cache-control.ts
563
+ var MAX_CACHE_BREAKPOINTS = 4;
548
564
  function getCacheControl(providerMetadata) {
549
565
  var _a;
550
566
  const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
551
567
  const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control;
552
568
  return cacheControlValue;
553
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
+ };
554
603
 
555
604
  // src/tool/text-editor_20250728.ts
556
605
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -704,11 +753,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
704
753
  async function prepareTools({
705
754
  tools,
706
755
  toolChoice,
707
- disableParallelToolUse
756
+ disableParallelToolUse,
757
+ cacheControlValidator
708
758
  }) {
709
759
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
710
760
  const toolWarnings = [];
711
761
  const betas = /* @__PURE__ */ new Set();
762
+ const validator = cacheControlValidator || new CacheControlValidator();
712
763
  if (tools == null) {
713
764
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
714
765
  }
@@ -716,7 +767,10 @@ async function prepareTools({
716
767
  for (const tool of tools) {
717
768
  switch (tool.type) {
718
769
  case "function": {
719
- const cacheControl = getCacheControl(tool.providerOptions);
770
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
771
+ type: "tool definition",
772
+ canCache: true
773
+ });
720
774
  anthropicTools2.push({
721
775
  name: tool.name,
722
776
  description: tool.description,
@@ -731,7 +785,8 @@ async function prepareTools({
731
785
  betas.add("code-execution-2025-05-22");
732
786
  anthropicTools2.push({
733
787
  type: "code_execution_20250522",
734
- name: "code_execution"
788
+ name: "code_execution",
789
+ cache_control: void 0
735
790
  });
736
791
  break;
737
792
  }
@@ -750,7 +805,8 @@ async function prepareTools({
750
805
  type: "computer_20250124",
751
806
  display_width_px: tool.args.displayWidthPx,
752
807
  display_height_px: tool.args.displayHeightPx,
753
- display_number: tool.args.displayNumber
808
+ display_number: tool.args.displayNumber,
809
+ cache_control: void 0
754
810
  });
755
811
  break;
756
812
  }
@@ -761,7 +817,8 @@ async function prepareTools({
761
817
  type: "computer_20241022",
762
818
  display_width_px: tool.args.displayWidthPx,
763
819
  display_height_px: tool.args.displayHeightPx,
764
- display_number: tool.args.displayNumber
820
+ display_number: tool.args.displayNumber,
821
+ cache_control: void 0
765
822
  });
766
823
  break;
767
824
  }
@@ -769,7 +826,8 @@ async function prepareTools({
769
826
  betas.add("computer-use-2025-01-24");
770
827
  anthropicTools2.push({
771
828
  name: "str_replace_editor",
772
- type: "text_editor_20250124"
829
+ type: "text_editor_20250124",
830
+ cache_control: void 0
773
831
  });
774
832
  break;
775
833
  }
@@ -777,7 +835,8 @@ async function prepareTools({
777
835
  betas.add("computer-use-2024-10-22");
778
836
  anthropicTools2.push({
779
837
  name: "str_replace_editor",
780
- type: "text_editor_20241022"
838
+ type: "text_editor_20241022",
839
+ cache_control: void 0
781
840
  });
782
841
  break;
783
842
  }
@@ -785,7 +844,8 @@ async function prepareTools({
785
844
  betas.add("computer-use-2025-01-24");
786
845
  anthropicTools2.push({
787
846
  name: "str_replace_based_edit_tool",
788
- type: "text_editor_20250429"
847
+ type: "text_editor_20250429",
848
+ cache_control: void 0
789
849
  });
790
850
  break;
791
851
  }
@@ -797,7 +857,8 @@ async function prepareTools({
797
857
  anthropicTools2.push({
798
858
  name: "str_replace_based_edit_tool",
799
859
  type: "text_editor_20250728",
800
- max_characters: args.maxCharacters
860
+ max_characters: args.maxCharacters,
861
+ cache_control: void 0
801
862
  });
802
863
  break;
803
864
  }
@@ -805,7 +866,8 @@ async function prepareTools({
805
866
  betas.add("computer-use-2025-01-24");
806
867
  anthropicTools2.push({
807
868
  name: "bash",
808
- type: "bash_20250124"
869
+ type: "bash_20250124",
870
+ cache_control: void 0
809
871
  });
810
872
  break;
811
873
  }
@@ -813,7 +875,8 @@ async function prepareTools({
813
875
  betas.add("computer-use-2024-10-22");
814
876
  anthropicTools2.push({
815
877
  name: "bash",
816
- type: "bash_20241022"
878
+ type: "bash_20241022",
879
+ cache_control: void 0
817
880
  });
818
881
  break;
819
882
  }
@@ -838,7 +901,8 @@ async function prepareTools({
838
901
  allowed_domains: args.allowedDomains,
839
902
  blocked_domains: args.blockedDomains,
840
903
  citations: args.citations,
841
- max_content_tokens: args.maxContentTokens
904
+ max_content_tokens: args.maxContentTokens,
905
+ cache_control: void 0
842
906
  });
843
907
  break;
844
908
  }
@@ -853,7 +917,8 @@ async function prepareTools({
853
917
  max_uses: args.maxUses,
854
918
  allowed_domains: args.allowedDomains,
855
919
  blocked_domains: args.blockedDomains,
856
- user_location: args.userLocation
920
+ user_location: args.userLocation,
921
+ cache_control: void 0
857
922
  });
858
923
  break;
859
924
  }
@@ -1079,11 +1144,13 @@ function convertToString(data) {
1079
1144
  async function convertToAnthropicMessagesPrompt({
1080
1145
  prompt,
1081
1146
  sendReasoning,
1082
- warnings
1147
+ warnings,
1148
+ cacheControlValidator
1083
1149
  }) {
1084
1150
  var _a, _b, _c, _d, _e;
1085
1151
  const betas = /* @__PURE__ */ new Set();
1086
1152
  const blocks = groupIntoBlocks(prompt);
1153
+ const validator = cacheControlValidator || new CacheControlValidator();
1087
1154
  let system = void 0;
1088
1155
  const messages = [];
1089
1156
  async function shouldEnableCitations(providerMetadata) {
@@ -1120,7 +1187,10 @@ async function convertToAnthropicMessagesPrompt({
1120
1187
  system = block.messages.map(({ content, providerOptions }) => ({
1121
1188
  type: "text",
1122
1189
  text: content,
1123
- cache_control: getCacheControl(providerOptions)
1190
+ cache_control: validator.getCacheControl(providerOptions, {
1191
+ type: "system message",
1192
+ canCache: true
1193
+ })
1124
1194
  }));
1125
1195
  break;
1126
1196
  }
@@ -1133,7 +1203,13 @@ async function convertToAnthropicMessagesPrompt({
1133
1203
  for (let j = 0; j < content.length; j++) {
1134
1204
  const part = content[j];
1135
1205
  const isLastPart = j === content.length - 1;
1136
- 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;
1137
1213
  switch (part.type) {
1138
1214
  case "text": {
1139
1215
  anthropicContent.push({
@@ -1221,7 +1297,13 @@ async function convertToAnthropicMessagesPrompt({
1221
1297
  for (let i2 = 0; i2 < content.length; i2++) {
1222
1298
  const part = content[i2];
1223
1299
  const isLastPart = i2 === content.length - 1;
1224
- 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;
1225
1307
  const output = part.output;
1226
1308
  let contentValue;
1227
1309
  switch (output.type) {
@@ -1231,8 +1313,7 @@ async function convertToAnthropicMessagesPrompt({
1231
1313
  case "text":
1232
1314
  return {
1233
1315
  type: "text",
1234
- text: contentPart.text,
1235
- cache_control: void 0
1316
+ text: contentPart.text
1236
1317
  };
1237
1318
  case "media": {
1238
1319
  if (contentPart.mediaType.startsWith("image/")) {
@@ -1242,8 +1323,7 @@ async function convertToAnthropicMessagesPrompt({
1242
1323
  type: "base64",
1243
1324
  media_type: contentPart.mediaType,
1244
1325
  data: contentPart.data
1245
- },
1246
- cache_control: void 0
1326
+ }
1247
1327
  };
1248
1328
  }
1249
1329
  if (contentPart.mediaType === "application/pdf") {
@@ -1254,8 +1334,7 @@ async function convertToAnthropicMessagesPrompt({
1254
1334
  type: "base64",
1255
1335
  media_type: contentPart.mediaType,
1256
1336
  data: contentPart.data
1257
- },
1258
- cache_control: void 0
1337
+ }
1259
1338
  };
1260
1339
  }
1261
1340
  throw new UnsupportedFunctionalityError2({
@@ -1303,7 +1382,13 @@ async function convertToAnthropicMessagesPrompt({
1303
1382
  for (let k = 0; k < content.length; k++) {
1304
1383
  const part = content[k];
1305
1384
  const isLastContentPart = k === content.length - 1;
1306
- 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;
1307
1392
  switch (part.type) {
1308
1393
  case "text": {
1309
1394
  anthropicContent.push({
@@ -1327,17 +1412,23 @@ async function convertToAnthropicMessagesPrompt({
1327
1412
  });
1328
1413
  if (reasoningMetadata != null) {
1329
1414
  if (reasoningMetadata.signature != null) {
1415
+ validator.getCacheControl(part.providerOptions, {
1416
+ type: "thinking block",
1417
+ canCache: false
1418
+ });
1330
1419
  anthropicContent.push({
1331
1420
  type: "thinking",
1332
1421
  thinking: part.text,
1333
- signature: reasoningMetadata.signature,
1334
- cache_control: cacheControl
1422
+ signature: reasoningMetadata.signature
1335
1423
  });
1336
1424
  } else if (reasoningMetadata.redactedData != null) {
1425
+ validator.getCacheControl(part.providerOptions, {
1426
+ type: "redacted thinking block",
1427
+ canCache: false
1428
+ });
1337
1429
  anthropicContent.push({
1338
1430
  type: "redacted_thinking",
1339
- data: reasoningMetadata.redactedData,
1340
- cache_control: cacheControl
1431
+ data: reasoningMetadata.redactedData
1341
1432
  });
1342
1433
  } else {
1343
1434
  warnings.push({
@@ -1666,7 +1757,7 @@ var AnthropicMessagesLanguageModel = class {
1666
1757
  toolChoice,
1667
1758
  providerOptions
1668
1759
  }) {
1669
- var _a, _b, _c;
1760
+ var _a, _b, _c, _d;
1670
1761
  const warnings = [];
1671
1762
  if (frequencyPenalty != null) {
1672
1763
  warnings.push({
@@ -1712,10 +1803,12 @@ var AnthropicMessagesLanguageModel = class {
1712
1803
  providerOptions,
1713
1804
  schema: anthropicProviderOptions
1714
1805
  });
1715
- const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
1806
+ const cacheControlValidator = new CacheControlValidator();
1807
+ const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1716
1808
  prompt,
1717
1809
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1718
- warnings
1810
+ warnings,
1811
+ cacheControlValidator
1719
1812
  });
1720
1813
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1721
1814
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1734,6 +1827,17 @@ var AnthropicMessagesLanguageModel = class {
1734
1827
  ...isThinking && {
1735
1828
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1736
1829
  },
1830
+ // container with agent skills:
1831
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.container) && {
1832
+ container: {
1833
+ id: anthropicOptions.container.id,
1834
+ skills: (_d = anthropicOptions.container.skills) == null ? void 0 : _d.map((skill) => ({
1835
+ type: skill.type,
1836
+ skill_id: skill.skillId,
1837
+ version: skill.version
1838
+ }))
1839
+ }
1840
+ },
1737
1841
  // prompt:
1738
1842
  system: messagesPrompt.system,
1739
1843
  messages: messagesPrompt.messages
@@ -1775,11 +1879,24 @@ var AnthropicMessagesLanguageModel = class {
1775
1879
  warnings.push({
1776
1880
  type: "unsupported-setting",
1777
1881
  setting: "maxOutputTokens",
1778
- details: `${maxTokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1882
+ details: `${baseArgs.max_tokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
1779
1883
  });
1780
1884
  }
1781
1885
  baseArgs.max_tokens = maxOutputTokensForModel;
1782
1886
  }
1887
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
1888
+ betas.add("code-execution-2025-08-25");
1889
+ betas.add("skills-2025-10-02");
1890
+ betas.add("files-api-2025-04-14");
1891
+ if (!(tools == null ? void 0 : tools.some(
1892
+ (tool) => tool.type === "provider-defined" && tool.id === "anthropic.code_execution_20250825"
1893
+ ))) {
1894
+ warnings.push({
1895
+ type: "other",
1896
+ message: "code execution tool is required when using skills"
1897
+ });
1898
+ }
1899
+ }
1783
1900
  const {
1784
1901
  tools: anthropicTools2,
1785
1902
  toolChoice: anthropicToolChoice,
@@ -1789,21 +1906,24 @@ var AnthropicMessagesLanguageModel = class {
1789
1906
  jsonResponseTool != null ? {
1790
1907
  tools: [jsonResponseTool],
1791
1908
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1792
- disableParallelToolUse: true
1909
+ disableParallelToolUse: true,
1910
+ cacheControlValidator
1793
1911
  } : {
1794
1912
  tools: tools != null ? tools : [],
1795
1913
  toolChoice,
1796
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
1914
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
1915
+ cacheControlValidator
1797
1916
  }
1798
1917
  );
1918
+ const cacheWarnings = cacheControlValidator.getWarnings();
1799
1919
  return {
1800
1920
  args: {
1801
1921
  ...baseArgs,
1802
1922
  tools: anthropicTools2,
1803
1923
  tool_choice: anthropicToolChoice
1804
1924
  },
1805
- warnings: [...warnings, ...toolWarnings],
1806
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
1925
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1926
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1807
1927
  usesJsonResponseTool: jsonResponseTool != null
1808
1928
  };
1809
1929
  }