@ai-sdk/anthropic 3.0.0-beta.29 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 3.0.0-beta.31
4
+
5
+ ### Patch Changes
6
+
7
+ - ca07285: feat(anthropic): add prompt caching validation
8
+
9
+ ## 3.0.0-beta.30
10
+
11
+ ### Patch Changes
12
+
13
+ - 9354297: feat(provider/anthropic): add support for Agent Skills
14
+
3
15
  ## 3.0.0-beta.29
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -6,7 +6,7 @@ The **[Anthropic provider](https://ai-sdk.dev/providers/ai-sdk-providers/anthrop
6
6
 
7
7
  The Anthropic provider is available in the `@ai-sdk/anthropic` module. You can install it with
8
8
 
9
- ```
9
+ ```bash
10
10
  npm i @ai-sdk/anthropic
11
11
  ```
12
12
 
package/dist/index.d.mts CHANGED
@@ -25,6 +25,14 @@ declare const anthropicProviderOptions: z.ZodObject<{
25
25
  allowedTools: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString>>>;
26
26
  }, z.core.$strip>>>;
27
27
  }, z.core.$strip>>>;
28
+ container: z.ZodOptional<z.ZodObject<{
29
+ id: z.ZodOptional<z.ZodString>;
30
+ skills: z.ZodOptional<z.ZodArray<z.ZodObject<{
31
+ type: z.ZodUnion<readonly [z.ZodLiteral<"anthropic">, z.ZodLiteral<"custom">]>;
32
+ skillId: z.ZodString;
33
+ version: z.ZodOptional<z.ZodString>;
34
+ }, z.core.$strip>>>;
35
+ }, z.core.$strip>>;
28
36
  }, z.core.$strip>;
29
37
  type AnthropicProviderOptions = z.infer<typeof anthropicProviderOptions>;
30
38
 
package/dist/index.d.ts CHANGED
@@ -25,6 +25,14 @@ declare const anthropicProviderOptions: z.ZodObject<{
25
25
  allowedTools: z.ZodOptional<z.ZodNullable<z.ZodArray<z.ZodString>>>;
26
26
  }, z.core.$strip>>>;
27
27
  }, z.core.$strip>>>;
28
+ container: z.ZodOptional<z.ZodObject<{
29
+ id: z.ZodOptional<z.ZodString>;
30
+ skills: z.ZodOptional<z.ZodArray<z.ZodObject<{
31
+ type: z.ZodUnion<readonly [z.ZodLiteral<"anthropic">, z.ZodLiteral<"custom">]>;
32
+ skillId: z.ZodString;
33
+ version: z.ZodOptional<z.ZodString>;
34
+ }, z.core.$strip>>>;
35
+ }, z.core.$strip>>;
28
36
  }, z.core.$strip>;
29
37
  type AnthropicProviderOptions = z.infer<typeof anthropicProviderOptions>;
30
38
 
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ var import_provider4 = require("@ai-sdk/provider");
31
31
  var import_provider_utils20 = require("@ai-sdk/provider-utils");
32
32
 
33
33
  // src/version.ts
34
- var VERSION = true ? "3.0.0-beta.29" : "0.0.0-test";
34
+ var VERSION = true ? "3.0.0-beta.31" : "0.0.0-test";
35
35
 
36
36
  // src/anthropic-messages-language-model.ts
37
37
  var import_provider3 = require("@ai-sdk/provider");
@@ -591,19 +591,68 @@ var anthropicProviderOptions = import_v43.z.object({
591
591
  allowedTools: import_v43.z.array(import_v43.z.string()).nullish()
592
592
  }).nullish()
593
593
  })
594
- ).optional()
594
+ ).optional(),
595
+ /**
596
+ * Agent Skills configuration. Skills enable Claude to perform specialized tasks
597
+ * like document processing (PPTX, DOCX, PDF, XLSX) and data analysis.
598
+ * Requires code execution tool to be enabled.
599
+ */
600
+ container: import_v43.z.object({
601
+ id: import_v43.z.string().optional(),
602
+ skills: import_v43.z.array(
603
+ import_v43.z.object({
604
+ type: import_v43.z.union([import_v43.z.literal("anthropic"), import_v43.z.literal("custom")]),
605
+ skillId: import_v43.z.string(),
606
+ version: import_v43.z.string().optional()
607
+ })
608
+ ).optional()
609
+ }).optional()
595
610
  });
596
611
 
597
612
  // src/anthropic-prepare-tools.ts
598
613
  var import_provider = require("@ai-sdk/provider");
599
614
 
600
615
  // src/get-cache-control.ts
616
+ var MAX_CACHE_BREAKPOINTS = 4;
601
617
  function getCacheControl(providerMetadata) {
602
618
  var _a;
603
619
  const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
604
620
  const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control;
605
621
  return cacheControlValue;
606
622
  }
623
+ var CacheControlValidator = class {
624
+ constructor() {
625
+ this.breakpointCount = 0;
626
+ this.warnings = [];
627
+ }
628
+ getCacheControl(providerMetadata, context) {
629
+ const cacheControlValue = getCacheControl(providerMetadata);
630
+ if (!cacheControlValue) {
631
+ return void 0;
632
+ }
633
+ if (!context.canCache) {
634
+ this.warnings.push({
635
+ type: "unsupported-setting",
636
+ setting: "cacheControl",
637
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
638
+ });
639
+ return void 0;
640
+ }
641
+ this.breakpointCount++;
642
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
643
+ this.warnings.push({
644
+ type: "unsupported-setting",
645
+ setting: "cacheControl",
646
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
647
+ });
648
+ return void 0;
649
+ }
650
+ return cacheControlValue;
651
+ }
652
+ getWarnings() {
653
+ return this.warnings;
654
+ }
655
+ };
607
656
 
608
657
  // src/tool/text-editor_20250728.ts
609
658
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
@@ -749,11 +798,13 @@ var import_provider_utils7 = require("@ai-sdk/provider-utils");
749
798
  async function prepareTools({
750
799
  tools,
751
800
  toolChoice,
752
- disableParallelToolUse
801
+ disableParallelToolUse,
802
+ cacheControlValidator
753
803
  }) {
754
804
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
755
805
  const toolWarnings = [];
756
806
  const betas = /* @__PURE__ */ new Set();
807
+ const validator = cacheControlValidator || new CacheControlValidator();
757
808
  if (tools == null) {
758
809
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
759
810
  }
@@ -761,7 +812,10 @@ async function prepareTools({
761
812
  for (const tool of tools) {
762
813
  switch (tool.type) {
763
814
  case "function": {
764
- const cacheControl = getCacheControl(tool.providerOptions);
815
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
816
+ type: "tool definition",
817
+ canCache: true
818
+ });
765
819
  anthropicTools2.push({
766
820
  name: tool.name,
767
821
  description: tool.description,
@@ -776,7 +830,8 @@ async function prepareTools({
776
830
  betas.add("code-execution-2025-05-22");
777
831
  anthropicTools2.push({
778
832
  type: "code_execution_20250522",
779
- name: "code_execution"
833
+ name: "code_execution",
834
+ cache_control: void 0
780
835
  });
781
836
  break;
782
837
  }
@@ -795,7 +850,8 @@ async function prepareTools({
795
850
  type: "computer_20250124",
796
851
  display_width_px: tool.args.displayWidthPx,
797
852
  display_height_px: tool.args.displayHeightPx,
798
- display_number: tool.args.displayNumber
853
+ display_number: tool.args.displayNumber,
854
+ cache_control: void 0
799
855
  });
800
856
  break;
801
857
  }
@@ -806,7 +862,8 @@ async function prepareTools({
806
862
  type: "computer_20241022",
807
863
  display_width_px: tool.args.displayWidthPx,
808
864
  display_height_px: tool.args.displayHeightPx,
809
- display_number: tool.args.displayNumber
865
+ display_number: tool.args.displayNumber,
866
+ cache_control: void 0
810
867
  });
811
868
  break;
812
869
  }
@@ -814,7 +871,8 @@ async function prepareTools({
814
871
  betas.add("computer-use-2025-01-24");
815
872
  anthropicTools2.push({
816
873
  name: "str_replace_editor",
817
- type: "text_editor_20250124"
874
+ type: "text_editor_20250124",
875
+ cache_control: void 0
818
876
  });
819
877
  break;
820
878
  }
@@ -822,7 +880,8 @@ async function prepareTools({
822
880
  betas.add("computer-use-2024-10-22");
823
881
  anthropicTools2.push({
824
882
  name: "str_replace_editor",
825
- type: "text_editor_20241022"
883
+ type: "text_editor_20241022",
884
+ cache_control: void 0
826
885
  });
827
886
  break;
828
887
  }
@@ -830,7 +889,8 @@ async function prepareTools({
830
889
  betas.add("computer-use-2025-01-24");
831
890
  anthropicTools2.push({
832
891
  name: "str_replace_based_edit_tool",
833
- type: "text_editor_20250429"
892
+ type: "text_editor_20250429",
893
+ cache_control: void 0
834
894
  });
835
895
  break;
836
896
  }
@@ -842,7 +902,8 @@ async function prepareTools({
842
902
  anthropicTools2.push({
843
903
  name: "str_replace_based_edit_tool",
844
904
  type: "text_editor_20250728",
845
- max_characters: args.maxCharacters
905
+ max_characters: args.maxCharacters,
906
+ cache_control: void 0
846
907
  });
847
908
  break;
848
909
  }
@@ -850,7 +911,8 @@ async function prepareTools({
850
911
  betas.add("computer-use-2025-01-24");
851
912
  anthropicTools2.push({
852
913
  name: "bash",
853
- type: "bash_20250124"
914
+ type: "bash_20250124",
915
+ cache_control: void 0
854
916
  });
855
917
  break;
856
918
  }
@@ -858,7 +920,8 @@ async function prepareTools({
858
920
  betas.add("computer-use-2024-10-22");
859
921
  anthropicTools2.push({
860
922
  name: "bash",
861
- type: "bash_20241022"
923
+ type: "bash_20241022",
924
+ cache_control: void 0
862
925
  });
863
926
  break;
864
927
  }
@@ -883,7 +946,8 @@ async function prepareTools({
883
946
  allowed_domains: args.allowedDomains,
884
947
  blocked_domains: args.blockedDomains,
885
948
  citations: args.citations,
886
- max_content_tokens: args.maxContentTokens
949
+ max_content_tokens: args.maxContentTokens,
950
+ cache_control: void 0
887
951
  });
888
952
  break;
889
953
  }
@@ -898,7 +962,8 @@ async function prepareTools({
898
962
  max_uses: args.maxUses,
899
963
  allowed_domains: args.allowedDomains,
900
964
  blocked_domains: args.blockedDomains,
901
- user_location: args.userLocation
965
+ user_location: args.userLocation,
966
+ cache_control: void 0
902
967
  });
903
968
  break;
904
969
  }
@@ -1110,11 +1175,13 @@ function convertToString(data) {
1110
1175
  async function convertToAnthropicMessagesPrompt({
1111
1176
  prompt,
1112
1177
  sendReasoning,
1113
- warnings
1178
+ warnings,
1179
+ cacheControlValidator
1114
1180
  }) {
1115
1181
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1116
1182
  const betas = /* @__PURE__ */ new Set();
1117
1183
  const blocks = groupIntoBlocks(prompt);
1184
+ const validator = cacheControlValidator || new CacheControlValidator();
1118
1185
  let system = void 0;
1119
1186
  const messages = [];
1120
1187
  async function shouldEnableCitations(providerMetadata) {
@@ -1151,7 +1218,10 @@ async function convertToAnthropicMessagesPrompt({
1151
1218
  system = block.messages.map(({ content, providerOptions }) => ({
1152
1219
  type: "text",
1153
1220
  text: content,
1154
- cache_control: getCacheControl(providerOptions)
1221
+ cache_control: validator.getCacheControl(providerOptions, {
1222
+ type: "system message",
1223
+ canCache: true
1224
+ })
1155
1225
  }));
1156
1226
  break;
1157
1227
  }
@@ -1164,7 +1234,13 @@ async function convertToAnthropicMessagesPrompt({
1164
1234
  for (let j = 0; j < content.length; j++) {
1165
1235
  const part = content[j];
1166
1236
  const isLastPart = j === content.length - 1;
1167
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1237
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1238
+ type: "user message part",
1239
+ canCache: true
1240
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1241
+ type: "user message",
1242
+ canCache: true
1243
+ }) : void 0;
1168
1244
  switch (part.type) {
1169
1245
  case "text": {
1170
1246
  anthropicContent.push({
@@ -1252,7 +1328,13 @@ async function convertToAnthropicMessagesPrompt({
1252
1328
  for (let i2 = 0; i2 < content.length; i2++) {
1253
1329
  const part = content[i2];
1254
1330
  const isLastPart = i2 === content.length - 1;
1255
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1331
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1332
+ type: "tool result part",
1333
+ canCache: true
1334
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1335
+ type: "tool result message",
1336
+ canCache: true
1337
+ }) : void 0;
1256
1338
  const output = part.output;
1257
1339
  let contentValue;
1258
1340
  switch (output.type) {
@@ -1262,8 +1344,7 @@ async function convertToAnthropicMessagesPrompt({
1262
1344
  case "text":
1263
1345
  return {
1264
1346
  type: "text",
1265
- text: contentPart.text,
1266
- cache_control: cacheControl
1347
+ text: contentPart.text
1267
1348
  };
1268
1349
  case "image-data": {
1269
1350
  return {
@@ -1272,8 +1353,7 @@ async function convertToAnthropicMessagesPrompt({
1272
1353
  type: "base64",
1273
1354
  media_type: contentPart.mediaType,
1274
1355
  data: contentPart.data
1275
- },
1276
- cache_control: cacheControl
1356
+ }
1277
1357
  };
1278
1358
  }
1279
1359
  case "file-data": {
@@ -1285,8 +1365,7 @@ async function convertToAnthropicMessagesPrompt({
1285
1365
  type: "base64",
1286
1366
  media_type: contentPart.mediaType,
1287
1367
  data: contentPart.data
1288
- },
1289
- cache_control: cacheControl
1368
+ }
1290
1369
  };
1291
1370
  }
1292
1371
  warnings.push({
@@ -1347,7 +1426,13 @@ async function convertToAnthropicMessagesPrompt({
1347
1426
  for (let k = 0; k < content.length; k++) {
1348
1427
  const part = content[k];
1349
1428
  const isLastContentPart = k === content.length - 1;
1350
- const cacheControl = (_f = getCacheControl(part.providerOptions)) != null ? _f : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1429
+ const cacheControl = (_f = validator.getCacheControl(part.providerOptions, {
1430
+ type: "assistant message part",
1431
+ canCache: true
1432
+ })) != null ? _f : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1433
+ type: "assistant message",
1434
+ canCache: true
1435
+ }) : void 0;
1351
1436
  switch (part.type) {
1352
1437
  case "text": {
1353
1438
  anthropicContent.push({
@@ -1371,17 +1456,23 @@ async function convertToAnthropicMessagesPrompt({
1371
1456
  });
1372
1457
  if (reasoningMetadata != null) {
1373
1458
  if (reasoningMetadata.signature != null) {
1459
+ validator.getCacheControl(part.providerOptions, {
1460
+ type: "thinking block",
1461
+ canCache: false
1462
+ });
1374
1463
  anthropicContent.push({
1375
1464
  type: "thinking",
1376
1465
  thinking: part.text,
1377
- signature: reasoningMetadata.signature,
1378
- cache_control: cacheControl
1466
+ signature: reasoningMetadata.signature
1379
1467
  });
1380
1468
  } else if (reasoningMetadata.redactedData != null) {
1469
+ validator.getCacheControl(part.providerOptions, {
1470
+ type: "redacted thinking block",
1471
+ canCache: false
1472
+ });
1381
1473
  anthropicContent.push({
1382
1474
  type: "redacted_thinking",
1383
- data: reasoningMetadata.redactedData,
1384
- cache_control: cacheControl
1475
+ data: reasoningMetadata.redactedData
1385
1476
  });
1386
1477
  } else {
1387
1478
  warnings.push({
@@ -1748,7 +1839,7 @@ var AnthropicMessagesLanguageModel = class {
1748
1839
  toolChoice,
1749
1840
  providerOptions
1750
1841
  }) {
1751
- var _a, _b, _c;
1842
+ var _a, _b, _c, _d;
1752
1843
  const warnings = [];
1753
1844
  if (frequencyPenalty != null) {
1754
1845
  warnings.push({
@@ -1794,10 +1885,12 @@ var AnthropicMessagesLanguageModel = class {
1794
1885
  providerOptions,
1795
1886
  schema: anthropicProviderOptions
1796
1887
  });
1888
+ const cacheControlValidator = new CacheControlValidator();
1797
1889
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1798
1890
  prompt,
1799
1891
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1800
- warnings
1892
+ warnings,
1893
+ cacheControlValidator
1801
1894
  });
1802
1895
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1803
1896
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1829,6 +1922,17 @@ var AnthropicMessagesLanguageModel = class {
1829
1922
  } : void 0
1830
1923
  }))
1831
1924
  },
1925
+ // container with agent skills:
1926
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.container) && {
1927
+ container: {
1928
+ id: anthropicOptions.container.id,
1929
+ skills: (_d = anthropicOptions.container.skills) == null ? void 0 : _d.map((skill) => ({
1930
+ type: skill.type,
1931
+ skill_id: skill.skillId,
1932
+ version: skill.version
1933
+ }))
1934
+ }
1935
+ },
1832
1936
  // prompt:
1833
1937
  system: messagesPrompt.system,
1834
1938
  messages: messagesPrompt.messages
@@ -1878,6 +1982,19 @@ var AnthropicMessagesLanguageModel = class {
1878
1982
  if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
1879
1983
  betas.add("mcp-client-2025-04-04");
1880
1984
  }
1985
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
1986
+ betas.add("code-execution-2025-08-25");
1987
+ betas.add("skills-2025-10-02");
1988
+ betas.add("files-api-2025-04-14");
1989
+ if (!(tools == null ? void 0 : tools.some(
1990
+ (tool) => tool.type === "provider-defined" && tool.id === "anthropic.code_execution_20250825"
1991
+ ))) {
1992
+ warnings.push({
1993
+ type: "other",
1994
+ message: "code execution tool is required when using skills"
1995
+ });
1996
+ }
1997
+ }
1881
1998
  const {
1882
1999
  tools: anthropicTools2,
1883
2000
  toolChoice: anthropicToolChoice,
@@ -1887,20 +2004,23 @@ var AnthropicMessagesLanguageModel = class {
1887
2004
  jsonResponseTool != null ? {
1888
2005
  tools: [jsonResponseTool],
1889
2006
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1890
- disableParallelToolUse: true
2007
+ disableParallelToolUse: true,
2008
+ cacheControlValidator
1891
2009
  } : {
1892
2010
  tools: tools != null ? tools : [],
1893
2011
  toolChoice,
1894
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
2012
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
2013
+ cacheControlValidator
1895
2014
  }
1896
2015
  );
2016
+ const cacheWarnings = cacheControlValidator.getWarnings();
1897
2017
  return {
1898
2018
  args: {
1899
2019
  ...baseArgs,
1900
2020
  tools: anthropicTools2,
1901
2021
  tool_choice: anthropicToolChoice
1902
2022
  },
1903
- warnings: [...warnings, ...toolWarnings],
2023
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1904
2024
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1905
2025
  usesJsonResponseTool: jsonResponseTool != null
1906
2026
  };