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

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.32" : "0.0.0-test";
14
14
 
15
15
  // src/anthropic-messages-language-model.ts
16
16
  import {
@@ -256,7 +256,18 @@ var anthropicMessagesResponseSchema = lazySchema2(
256
256
  output_tokens: z2.number(),
257
257
  cache_creation_input_tokens: z2.number().nullish(),
258
258
  cache_read_input_tokens: z2.number().nullish()
259
- })
259
+ }),
260
+ container: z2.object({
261
+ expires_at: z2.string(),
262
+ id: z2.string(),
263
+ skills: z2.array(
264
+ z2.object({
265
+ type: z2.union([z2.literal("anthropic"), z2.literal("custom")]),
266
+ skill_id: z2.string(),
267
+ version: z2.string()
268
+ })
269
+ ).nullish()
270
+ }).nullish()
260
271
  })
261
272
  )
262
273
  );
@@ -504,7 +515,21 @@ var anthropicMessagesChunkSchema = lazySchema2(
504
515
  type: z2.literal("message_delta"),
505
516
  delta: z2.object({
506
517
  stop_reason: z2.string().nullish(),
507
- stop_sequence: z2.string().nullish()
518
+ stop_sequence: z2.string().nullish(),
519
+ container: z2.object({
520
+ expires_at: z2.string(),
521
+ id: z2.string(),
522
+ skills: z2.array(
523
+ z2.object({
524
+ type: z2.union([
525
+ z2.literal("anthropic"),
526
+ z2.literal("custom")
527
+ ]),
528
+ skill_id: z2.string(),
529
+ version: z2.string()
530
+ })
531
+ ).nullish()
532
+ }).nullish()
508
533
  }),
509
534
  usage: z2.looseObject({
510
535
  output_tokens: z2.number(),
@@ -608,12 +633,46 @@ import {
608
633
  } from "@ai-sdk/provider";
609
634
 
610
635
  // src/get-cache-control.ts
636
+ var MAX_CACHE_BREAKPOINTS = 4;
611
637
  function getCacheControl(providerMetadata) {
612
638
  var _a;
613
639
  const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
614
640
  const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control;
615
641
  return cacheControlValue;
616
642
  }
643
+ var CacheControlValidator = class {
644
+ constructor() {
645
+ this.breakpointCount = 0;
646
+ this.warnings = [];
647
+ }
648
+ getCacheControl(providerMetadata, context) {
649
+ const cacheControlValue = getCacheControl(providerMetadata);
650
+ if (!cacheControlValue) {
651
+ return void 0;
652
+ }
653
+ if (!context.canCache) {
654
+ this.warnings.push({
655
+ type: "unsupported-setting",
656
+ setting: "cacheControl",
657
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
658
+ });
659
+ return void 0;
660
+ }
661
+ this.breakpointCount++;
662
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
663
+ this.warnings.push({
664
+ type: "unsupported-setting",
665
+ setting: "cacheControl",
666
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
667
+ });
668
+ return void 0;
669
+ }
670
+ return cacheControlValue;
671
+ }
672
+ getWarnings() {
673
+ return this.warnings;
674
+ }
675
+ };
617
676
 
618
677
  // src/tool/text-editor_20250728.ts
619
678
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -767,11 +826,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
767
826
  async function prepareTools({
768
827
  tools,
769
828
  toolChoice,
770
- disableParallelToolUse
829
+ disableParallelToolUse,
830
+ cacheControlValidator
771
831
  }) {
772
832
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
773
833
  const toolWarnings = [];
774
834
  const betas = /* @__PURE__ */ new Set();
835
+ const validator = cacheControlValidator || new CacheControlValidator();
775
836
  if (tools == null) {
776
837
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
777
838
  }
@@ -779,7 +840,10 @@ async function prepareTools({
779
840
  for (const tool of tools) {
780
841
  switch (tool.type) {
781
842
  case "function": {
782
- const cacheControl = getCacheControl(tool.providerOptions);
843
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
844
+ type: "tool definition",
845
+ canCache: true
846
+ });
783
847
  anthropicTools2.push({
784
848
  name: tool.name,
785
849
  description: tool.description,
@@ -794,7 +858,8 @@ async function prepareTools({
794
858
  betas.add("code-execution-2025-05-22");
795
859
  anthropicTools2.push({
796
860
  type: "code_execution_20250522",
797
- name: "code_execution"
861
+ name: "code_execution",
862
+ cache_control: void 0
798
863
  });
799
864
  break;
800
865
  }
@@ -813,7 +878,8 @@ async function prepareTools({
813
878
  type: "computer_20250124",
814
879
  display_width_px: tool.args.displayWidthPx,
815
880
  display_height_px: tool.args.displayHeightPx,
816
- display_number: tool.args.displayNumber
881
+ display_number: tool.args.displayNumber,
882
+ cache_control: void 0
817
883
  });
818
884
  break;
819
885
  }
@@ -824,7 +890,8 @@ async function prepareTools({
824
890
  type: "computer_20241022",
825
891
  display_width_px: tool.args.displayWidthPx,
826
892
  display_height_px: tool.args.displayHeightPx,
827
- display_number: tool.args.displayNumber
893
+ display_number: tool.args.displayNumber,
894
+ cache_control: void 0
828
895
  });
829
896
  break;
830
897
  }
@@ -832,7 +899,8 @@ async function prepareTools({
832
899
  betas.add("computer-use-2025-01-24");
833
900
  anthropicTools2.push({
834
901
  name: "str_replace_editor",
835
- type: "text_editor_20250124"
902
+ type: "text_editor_20250124",
903
+ cache_control: void 0
836
904
  });
837
905
  break;
838
906
  }
@@ -840,7 +908,8 @@ async function prepareTools({
840
908
  betas.add("computer-use-2024-10-22");
841
909
  anthropicTools2.push({
842
910
  name: "str_replace_editor",
843
- type: "text_editor_20241022"
911
+ type: "text_editor_20241022",
912
+ cache_control: void 0
844
913
  });
845
914
  break;
846
915
  }
@@ -848,7 +917,8 @@ async function prepareTools({
848
917
  betas.add("computer-use-2025-01-24");
849
918
  anthropicTools2.push({
850
919
  name: "str_replace_based_edit_tool",
851
- type: "text_editor_20250429"
920
+ type: "text_editor_20250429",
921
+ cache_control: void 0
852
922
  });
853
923
  break;
854
924
  }
@@ -860,7 +930,8 @@ async function prepareTools({
860
930
  anthropicTools2.push({
861
931
  name: "str_replace_based_edit_tool",
862
932
  type: "text_editor_20250728",
863
- max_characters: args.maxCharacters
933
+ max_characters: args.maxCharacters,
934
+ cache_control: void 0
864
935
  });
865
936
  break;
866
937
  }
@@ -868,7 +939,8 @@ async function prepareTools({
868
939
  betas.add("computer-use-2025-01-24");
869
940
  anthropicTools2.push({
870
941
  name: "bash",
871
- type: "bash_20250124"
942
+ type: "bash_20250124",
943
+ cache_control: void 0
872
944
  });
873
945
  break;
874
946
  }
@@ -876,7 +948,8 @@ async function prepareTools({
876
948
  betas.add("computer-use-2024-10-22");
877
949
  anthropicTools2.push({
878
950
  name: "bash",
879
- type: "bash_20241022"
951
+ type: "bash_20241022",
952
+ cache_control: void 0
880
953
  });
881
954
  break;
882
955
  }
@@ -901,7 +974,8 @@ async function prepareTools({
901
974
  allowed_domains: args.allowedDomains,
902
975
  blocked_domains: args.blockedDomains,
903
976
  citations: args.citations,
904
- max_content_tokens: args.maxContentTokens
977
+ max_content_tokens: args.maxContentTokens,
978
+ cache_control: void 0
905
979
  });
906
980
  break;
907
981
  }
@@ -916,7 +990,8 @@ async function prepareTools({
916
990
  max_uses: args.maxUses,
917
991
  allowed_domains: args.allowedDomains,
918
992
  blocked_domains: args.blockedDomains,
919
- user_location: args.userLocation
993
+ user_location: args.userLocation,
994
+ cache_control: void 0
920
995
  });
921
996
  break;
922
997
  }
@@ -1143,11 +1218,13 @@ function convertToString(data) {
1143
1218
  async function convertToAnthropicMessagesPrompt({
1144
1219
  prompt,
1145
1220
  sendReasoning,
1146
- warnings
1221
+ warnings,
1222
+ cacheControlValidator
1147
1223
  }) {
1148
1224
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1149
1225
  const betas = /* @__PURE__ */ new Set();
1150
1226
  const blocks = groupIntoBlocks(prompt);
1227
+ const validator = cacheControlValidator || new CacheControlValidator();
1151
1228
  let system = void 0;
1152
1229
  const messages = [];
1153
1230
  async function shouldEnableCitations(providerMetadata) {
@@ -1184,7 +1261,10 @@ async function convertToAnthropicMessagesPrompt({
1184
1261
  system = block.messages.map(({ content, providerOptions }) => ({
1185
1262
  type: "text",
1186
1263
  text: content,
1187
- cache_control: getCacheControl(providerOptions)
1264
+ cache_control: validator.getCacheControl(providerOptions, {
1265
+ type: "system message",
1266
+ canCache: true
1267
+ })
1188
1268
  }));
1189
1269
  break;
1190
1270
  }
@@ -1197,7 +1277,13 @@ async function convertToAnthropicMessagesPrompt({
1197
1277
  for (let j = 0; j < content.length; j++) {
1198
1278
  const part = content[j];
1199
1279
  const isLastPart = j === content.length - 1;
1200
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1280
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1281
+ type: "user message part",
1282
+ canCache: true
1283
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1284
+ type: "user message",
1285
+ canCache: true
1286
+ }) : void 0;
1201
1287
  switch (part.type) {
1202
1288
  case "text": {
1203
1289
  anthropicContent.push({
@@ -1285,7 +1371,13 @@ async function convertToAnthropicMessagesPrompt({
1285
1371
  for (let i2 = 0; i2 < content.length; i2++) {
1286
1372
  const part = content[i2];
1287
1373
  const isLastPart = i2 === content.length - 1;
1288
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1374
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1375
+ type: "tool result part",
1376
+ canCache: true
1377
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1378
+ type: "tool result message",
1379
+ canCache: true
1380
+ }) : void 0;
1289
1381
  const output = part.output;
1290
1382
  let contentValue;
1291
1383
  switch (output.type) {
@@ -1295,8 +1387,7 @@ async function convertToAnthropicMessagesPrompt({
1295
1387
  case "text":
1296
1388
  return {
1297
1389
  type: "text",
1298
- text: contentPart.text,
1299
- cache_control: cacheControl
1390
+ text: contentPart.text
1300
1391
  };
1301
1392
  case "image-data": {
1302
1393
  return {
@@ -1305,8 +1396,7 @@ async function convertToAnthropicMessagesPrompt({
1305
1396
  type: "base64",
1306
1397
  media_type: contentPart.mediaType,
1307
1398
  data: contentPart.data
1308
- },
1309
- cache_control: cacheControl
1399
+ }
1310
1400
  };
1311
1401
  }
1312
1402
  case "file-data": {
@@ -1318,8 +1408,7 @@ async function convertToAnthropicMessagesPrompt({
1318
1408
  type: "base64",
1319
1409
  media_type: contentPart.mediaType,
1320
1410
  data: contentPart.data
1321
- },
1322
- cache_control: cacheControl
1411
+ }
1323
1412
  };
1324
1413
  }
1325
1414
  warnings.push({
@@ -1380,7 +1469,13 @@ async function convertToAnthropicMessagesPrompt({
1380
1469
  for (let k = 0; k < content.length; k++) {
1381
1470
  const part = content[k];
1382
1471
  const isLastContentPart = k === content.length - 1;
1383
- const cacheControl = (_f = getCacheControl(part.providerOptions)) != null ? _f : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1472
+ const cacheControl = (_f = validator.getCacheControl(part.providerOptions, {
1473
+ type: "assistant message part",
1474
+ canCache: true
1475
+ })) != null ? _f : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1476
+ type: "assistant message",
1477
+ canCache: true
1478
+ }) : void 0;
1384
1479
  switch (part.type) {
1385
1480
  case "text": {
1386
1481
  anthropicContent.push({
@@ -1404,17 +1499,23 @@ async function convertToAnthropicMessagesPrompt({
1404
1499
  });
1405
1500
  if (reasoningMetadata != null) {
1406
1501
  if (reasoningMetadata.signature != null) {
1502
+ validator.getCacheControl(part.providerOptions, {
1503
+ type: "thinking block",
1504
+ canCache: false
1505
+ });
1407
1506
  anthropicContent.push({
1408
1507
  type: "thinking",
1409
1508
  thinking: part.text,
1410
- signature: reasoningMetadata.signature,
1411
- cache_control: cacheControl
1509
+ signature: reasoningMetadata.signature
1412
1510
  });
1413
1511
  } else if (reasoningMetadata.redactedData != null) {
1512
+ validator.getCacheControl(part.providerOptions, {
1513
+ type: "redacted thinking block",
1514
+ canCache: false
1515
+ });
1414
1516
  anthropicContent.push({
1415
1517
  type: "redacted_thinking",
1416
- data: reasoningMetadata.redactedData,
1417
- cache_control: cacheControl
1518
+ data: reasoningMetadata.redactedData
1418
1519
  });
1419
1520
  } else {
1420
1521
  warnings.push({
@@ -1827,10 +1928,12 @@ var AnthropicMessagesLanguageModel = class {
1827
1928
  providerOptions,
1828
1929
  schema: anthropicProviderOptions
1829
1930
  });
1931
+ const cacheControlValidator = new CacheControlValidator();
1830
1932
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1831
1933
  prompt,
1832
1934
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1833
- warnings
1935
+ warnings,
1936
+ cacheControlValidator
1834
1937
  });
1835
1938
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1836
1939
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1944,20 +2047,23 @@ var AnthropicMessagesLanguageModel = class {
1944
2047
  jsonResponseTool != null ? {
1945
2048
  tools: [jsonResponseTool],
1946
2049
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1947
- disableParallelToolUse: true
2050
+ disableParallelToolUse: true,
2051
+ cacheControlValidator
1948
2052
  } : {
1949
2053
  tools: tools != null ? tools : [],
1950
2054
  toolChoice,
1951
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
2055
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
2056
+ cacheControlValidator
1952
2057
  }
1953
2058
  );
2059
+ const cacheWarnings = cacheControlValidator.getWarnings();
1954
2060
  return {
1955
2061
  args: {
1956
2062
  ...baseArgs,
1957
2063
  tools: anthropicTools2,
1958
2064
  tool_choice: anthropicToolChoice
1959
2065
  },
1960
- warnings: [...warnings, ...toolWarnings],
2066
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1961
2067
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1962
2068
  usesJsonResponseTool: jsonResponseTool != null
1963
2069
  };
@@ -2004,7 +2110,7 @@ var AnthropicMessagesLanguageModel = class {
2004
2110
  });
2005
2111
  }
2006
2112
  async doGenerate(options) {
2007
- var _a, _b, _c, _d, _e, _f;
2113
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2008
2114
  const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
2009
2115
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2010
2116
  const {
@@ -2287,7 +2393,16 @@ var AnthropicMessagesLanguageModel = class {
2287
2393
  anthropic: {
2288
2394
  usage: response.usage,
2289
2395
  cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null,
2290
- stopSequence: (_f = response.stop_sequence) != null ? _f : null
2396
+ stopSequence: (_f = response.stop_sequence) != null ? _f : null,
2397
+ container: response.container ? {
2398
+ expiresAt: response.container.expires_at,
2399
+ id: response.container.id,
2400
+ skills: (_h = (_g = response.container.skills) == null ? void 0 : _g.map((skill) => ({
2401
+ type: skill.type,
2402
+ skillId: skill.skill_id,
2403
+ version: skill.version
2404
+ }))) != null ? _h : null
2405
+ } : null
2291
2406
  }
2292
2407
  }
2293
2408
  };
@@ -2318,6 +2433,7 @@ var AnthropicMessagesLanguageModel = class {
2318
2433
  let rawUsage = void 0;
2319
2434
  let cacheCreationInputTokens = null;
2320
2435
  let stopSequence = null;
2436
+ let container = null;
2321
2437
  let blockType = void 0;
2322
2438
  const generateId3 = this.generateId;
2323
2439
  return {
@@ -2327,7 +2443,7 @@ var AnthropicMessagesLanguageModel = class {
2327
2443
  controller.enqueue({ type: "stream-start", warnings });
2328
2444
  },
2329
2445
  transform(chunk, controller) {
2330
- var _a, _b, _c, _d, _e, _f, _g, _h;
2446
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2331
2447
  if (options.includeRawChunks) {
2332
2448
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2333
2449
  }
@@ -2736,6 +2852,15 @@ var AnthropicMessagesLanguageModel = class {
2736
2852
  isJsonResponseFromTool: usesJsonResponseTool
2737
2853
  });
2738
2854
  stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2855
+ container = value.delta.container != null ? {
2856
+ expiresAt: value.delta.container.expires_at,
2857
+ id: value.delta.container.id,
2858
+ skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2859
+ type: skill.type,
2860
+ skillId: skill.skill_id,
2861
+ version: skill.version
2862
+ }))) != null ? _j : null
2863
+ } : null;
2739
2864
  rawUsage = {
2740
2865
  ...rawUsage,
2741
2866
  ...value.usage
@@ -2751,7 +2876,8 @@ var AnthropicMessagesLanguageModel = class {
2751
2876
  anthropic: {
2752
2877
  usage: rawUsage != null ? rawUsage : null,
2753
2878
  cacheCreationInputTokens,
2754
- stopSequence
2879
+ stopSequence,
2880
+ container
2755
2881
  }
2756
2882
  }
2757
2883
  });