@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.
@@ -242,7 +242,18 @@ var anthropicMessagesResponseSchema = lazySchema2(
242
242
  output_tokens: z2.number(),
243
243
  cache_creation_input_tokens: z2.number().nullish(),
244
244
  cache_read_input_tokens: z2.number().nullish()
245
- })
245
+ }),
246
+ container: z2.object({
247
+ expires_at: z2.string(),
248
+ id: z2.string(),
249
+ skills: z2.array(
250
+ z2.object({
251
+ type: z2.union([z2.literal("anthropic"), z2.literal("custom")]),
252
+ skill_id: z2.string(),
253
+ version: z2.string()
254
+ })
255
+ ).nullish()
256
+ }).nullish()
246
257
  })
247
258
  )
248
259
  );
@@ -490,7 +501,21 @@ var anthropicMessagesChunkSchema = lazySchema2(
490
501
  type: z2.literal("message_delta"),
491
502
  delta: z2.object({
492
503
  stop_reason: z2.string().nullish(),
493
- stop_sequence: z2.string().nullish()
504
+ stop_sequence: z2.string().nullish(),
505
+ container: z2.object({
506
+ expires_at: z2.string(),
507
+ id: z2.string(),
508
+ skills: z2.array(
509
+ z2.object({
510
+ type: z2.union([
511
+ z2.literal("anthropic"),
512
+ z2.literal("custom")
513
+ ]),
514
+ skill_id: z2.string(),
515
+ version: z2.string()
516
+ })
517
+ ).nullish()
518
+ }).nullish()
494
519
  }),
495
520
  usage: z2.looseObject({
496
521
  output_tokens: z2.number(),
@@ -594,12 +619,46 @@ import {
594
619
  } from "@ai-sdk/provider";
595
620
 
596
621
  // src/get-cache-control.ts
622
+ var MAX_CACHE_BREAKPOINTS = 4;
597
623
  function getCacheControl(providerMetadata) {
598
624
  var _a;
599
625
  const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
600
626
  const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.cache_control;
601
627
  return cacheControlValue;
602
628
  }
629
+ var CacheControlValidator = class {
630
+ constructor() {
631
+ this.breakpointCount = 0;
632
+ this.warnings = [];
633
+ }
634
+ getCacheControl(providerMetadata, context) {
635
+ const cacheControlValue = getCacheControl(providerMetadata);
636
+ if (!cacheControlValue) {
637
+ return void 0;
638
+ }
639
+ if (!context.canCache) {
640
+ this.warnings.push({
641
+ type: "unsupported-setting",
642
+ setting: "cacheControl",
643
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
644
+ });
645
+ return void 0;
646
+ }
647
+ this.breakpointCount++;
648
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
649
+ this.warnings.push({
650
+ type: "unsupported-setting",
651
+ setting: "cacheControl",
652
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
653
+ });
654
+ return void 0;
655
+ }
656
+ return cacheControlValue;
657
+ }
658
+ getWarnings() {
659
+ return this.warnings;
660
+ }
661
+ };
603
662
 
604
663
  // src/tool/text-editor_20250728.ts
605
664
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -753,11 +812,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
753
812
  async function prepareTools({
754
813
  tools,
755
814
  toolChoice,
756
- disableParallelToolUse
815
+ disableParallelToolUse,
816
+ cacheControlValidator
757
817
  }) {
758
818
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
759
819
  const toolWarnings = [];
760
820
  const betas = /* @__PURE__ */ new Set();
821
+ const validator = cacheControlValidator || new CacheControlValidator();
761
822
  if (tools == null) {
762
823
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
763
824
  }
@@ -765,7 +826,10 @@ async function prepareTools({
765
826
  for (const tool of tools) {
766
827
  switch (tool.type) {
767
828
  case "function": {
768
- const cacheControl = getCacheControl(tool.providerOptions);
829
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
830
+ type: "tool definition",
831
+ canCache: true
832
+ });
769
833
  anthropicTools2.push({
770
834
  name: tool.name,
771
835
  description: tool.description,
@@ -780,7 +844,8 @@ async function prepareTools({
780
844
  betas.add("code-execution-2025-05-22");
781
845
  anthropicTools2.push({
782
846
  type: "code_execution_20250522",
783
- name: "code_execution"
847
+ name: "code_execution",
848
+ cache_control: void 0
784
849
  });
785
850
  break;
786
851
  }
@@ -799,7 +864,8 @@ async function prepareTools({
799
864
  type: "computer_20250124",
800
865
  display_width_px: tool.args.displayWidthPx,
801
866
  display_height_px: tool.args.displayHeightPx,
802
- display_number: tool.args.displayNumber
867
+ display_number: tool.args.displayNumber,
868
+ cache_control: void 0
803
869
  });
804
870
  break;
805
871
  }
@@ -810,7 +876,8 @@ async function prepareTools({
810
876
  type: "computer_20241022",
811
877
  display_width_px: tool.args.displayWidthPx,
812
878
  display_height_px: tool.args.displayHeightPx,
813
- display_number: tool.args.displayNumber
879
+ display_number: tool.args.displayNumber,
880
+ cache_control: void 0
814
881
  });
815
882
  break;
816
883
  }
@@ -818,7 +885,8 @@ async function prepareTools({
818
885
  betas.add("computer-use-2025-01-24");
819
886
  anthropicTools2.push({
820
887
  name: "str_replace_editor",
821
- type: "text_editor_20250124"
888
+ type: "text_editor_20250124",
889
+ cache_control: void 0
822
890
  });
823
891
  break;
824
892
  }
@@ -826,7 +894,8 @@ async function prepareTools({
826
894
  betas.add("computer-use-2024-10-22");
827
895
  anthropicTools2.push({
828
896
  name: "str_replace_editor",
829
- type: "text_editor_20241022"
897
+ type: "text_editor_20241022",
898
+ cache_control: void 0
830
899
  });
831
900
  break;
832
901
  }
@@ -834,7 +903,8 @@ async function prepareTools({
834
903
  betas.add("computer-use-2025-01-24");
835
904
  anthropicTools2.push({
836
905
  name: "str_replace_based_edit_tool",
837
- type: "text_editor_20250429"
906
+ type: "text_editor_20250429",
907
+ cache_control: void 0
838
908
  });
839
909
  break;
840
910
  }
@@ -846,7 +916,8 @@ async function prepareTools({
846
916
  anthropicTools2.push({
847
917
  name: "str_replace_based_edit_tool",
848
918
  type: "text_editor_20250728",
849
- max_characters: args.maxCharacters
919
+ max_characters: args.maxCharacters,
920
+ cache_control: void 0
850
921
  });
851
922
  break;
852
923
  }
@@ -854,7 +925,8 @@ async function prepareTools({
854
925
  betas.add("computer-use-2025-01-24");
855
926
  anthropicTools2.push({
856
927
  name: "bash",
857
- type: "bash_20250124"
928
+ type: "bash_20250124",
929
+ cache_control: void 0
858
930
  });
859
931
  break;
860
932
  }
@@ -862,7 +934,8 @@ async function prepareTools({
862
934
  betas.add("computer-use-2024-10-22");
863
935
  anthropicTools2.push({
864
936
  name: "bash",
865
- type: "bash_20241022"
937
+ type: "bash_20241022",
938
+ cache_control: void 0
866
939
  });
867
940
  break;
868
941
  }
@@ -887,7 +960,8 @@ async function prepareTools({
887
960
  allowed_domains: args.allowedDomains,
888
961
  blocked_domains: args.blockedDomains,
889
962
  citations: args.citations,
890
- max_content_tokens: args.maxContentTokens
963
+ max_content_tokens: args.maxContentTokens,
964
+ cache_control: void 0
891
965
  });
892
966
  break;
893
967
  }
@@ -902,7 +976,8 @@ async function prepareTools({
902
976
  max_uses: args.maxUses,
903
977
  allowed_domains: args.allowedDomains,
904
978
  blocked_domains: args.blockedDomains,
905
- user_location: args.userLocation
979
+ user_location: args.userLocation,
980
+ cache_control: void 0
906
981
  });
907
982
  break;
908
983
  }
@@ -1129,11 +1204,13 @@ function convertToString(data) {
1129
1204
  async function convertToAnthropicMessagesPrompt({
1130
1205
  prompt,
1131
1206
  sendReasoning,
1132
- warnings
1207
+ warnings,
1208
+ cacheControlValidator
1133
1209
  }) {
1134
1210
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1135
1211
  const betas = /* @__PURE__ */ new Set();
1136
1212
  const blocks = groupIntoBlocks(prompt);
1213
+ const validator = cacheControlValidator || new CacheControlValidator();
1137
1214
  let system = void 0;
1138
1215
  const messages = [];
1139
1216
  async function shouldEnableCitations(providerMetadata) {
@@ -1170,7 +1247,10 @@ async function convertToAnthropicMessagesPrompt({
1170
1247
  system = block.messages.map(({ content, providerOptions }) => ({
1171
1248
  type: "text",
1172
1249
  text: content,
1173
- cache_control: getCacheControl(providerOptions)
1250
+ cache_control: validator.getCacheControl(providerOptions, {
1251
+ type: "system message",
1252
+ canCache: true
1253
+ })
1174
1254
  }));
1175
1255
  break;
1176
1256
  }
@@ -1183,7 +1263,13 @@ async function convertToAnthropicMessagesPrompt({
1183
1263
  for (let j = 0; j < content.length; j++) {
1184
1264
  const part = content[j];
1185
1265
  const isLastPart = j === content.length - 1;
1186
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1266
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1267
+ type: "user message part",
1268
+ canCache: true
1269
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1270
+ type: "user message",
1271
+ canCache: true
1272
+ }) : void 0;
1187
1273
  switch (part.type) {
1188
1274
  case "text": {
1189
1275
  anthropicContent.push({
@@ -1271,7 +1357,13 @@ async function convertToAnthropicMessagesPrompt({
1271
1357
  for (let i2 = 0; i2 < content.length; i2++) {
1272
1358
  const part = content[i2];
1273
1359
  const isLastPart = i2 === content.length - 1;
1274
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1360
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1361
+ type: "tool result part",
1362
+ canCache: true
1363
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1364
+ type: "tool result message",
1365
+ canCache: true
1366
+ }) : void 0;
1275
1367
  const output = part.output;
1276
1368
  let contentValue;
1277
1369
  switch (output.type) {
@@ -1281,8 +1373,7 @@ async function convertToAnthropicMessagesPrompt({
1281
1373
  case "text":
1282
1374
  return {
1283
1375
  type: "text",
1284
- text: contentPart.text,
1285
- cache_control: cacheControl
1376
+ text: contentPart.text
1286
1377
  };
1287
1378
  case "image-data": {
1288
1379
  return {
@@ -1291,8 +1382,7 @@ async function convertToAnthropicMessagesPrompt({
1291
1382
  type: "base64",
1292
1383
  media_type: contentPart.mediaType,
1293
1384
  data: contentPart.data
1294
- },
1295
- cache_control: cacheControl
1385
+ }
1296
1386
  };
1297
1387
  }
1298
1388
  case "file-data": {
@@ -1304,8 +1394,7 @@ async function convertToAnthropicMessagesPrompt({
1304
1394
  type: "base64",
1305
1395
  media_type: contentPart.mediaType,
1306
1396
  data: contentPart.data
1307
- },
1308
- cache_control: cacheControl
1397
+ }
1309
1398
  };
1310
1399
  }
1311
1400
  warnings.push({
@@ -1366,7 +1455,13 @@ async function convertToAnthropicMessagesPrompt({
1366
1455
  for (let k = 0; k < content.length; k++) {
1367
1456
  const part = content[k];
1368
1457
  const isLastContentPart = k === content.length - 1;
1369
- const cacheControl = (_f = getCacheControl(part.providerOptions)) != null ? _f : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1458
+ const cacheControl = (_f = validator.getCacheControl(part.providerOptions, {
1459
+ type: "assistant message part",
1460
+ canCache: true
1461
+ })) != null ? _f : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1462
+ type: "assistant message",
1463
+ canCache: true
1464
+ }) : void 0;
1370
1465
  switch (part.type) {
1371
1466
  case "text": {
1372
1467
  anthropicContent.push({
@@ -1390,17 +1485,23 @@ async function convertToAnthropicMessagesPrompt({
1390
1485
  });
1391
1486
  if (reasoningMetadata != null) {
1392
1487
  if (reasoningMetadata.signature != null) {
1488
+ validator.getCacheControl(part.providerOptions, {
1489
+ type: "thinking block",
1490
+ canCache: false
1491
+ });
1393
1492
  anthropicContent.push({
1394
1493
  type: "thinking",
1395
1494
  thinking: part.text,
1396
- signature: reasoningMetadata.signature,
1397
- cache_control: cacheControl
1495
+ signature: reasoningMetadata.signature
1398
1496
  });
1399
1497
  } else if (reasoningMetadata.redactedData != null) {
1498
+ validator.getCacheControl(part.providerOptions, {
1499
+ type: "redacted thinking block",
1500
+ canCache: false
1501
+ });
1400
1502
  anthropicContent.push({
1401
1503
  type: "redacted_thinking",
1402
- data: reasoningMetadata.redactedData,
1403
- cache_control: cacheControl
1504
+ data: reasoningMetadata.redactedData
1404
1505
  });
1405
1506
  } else {
1406
1507
  warnings.push({
@@ -1813,10 +1914,12 @@ var AnthropicMessagesLanguageModel = class {
1813
1914
  providerOptions,
1814
1915
  schema: anthropicProviderOptions
1815
1916
  });
1917
+ const cacheControlValidator = new CacheControlValidator();
1816
1918
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1817
1919
  prompt,
1818
1920
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1819
- warnings
1921
+ warnings,
1922
+ cacheControlValidator
1820
1923
  });
1821
1924
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1822
1925
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1930,20 +2033,23 @@ var AnthropicMessagesLanguageModel = class {
1930
2033
  jsonResponseTool != null ? {
1931
2034
  tools: [jsonResponseTool],
1932
2035
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1933
- disableParallelToolUse: true
2036
+ disableParallelToolUse: true,
2037
+ cacheControlValidator
1934
2038
  } : {
1935
2039
  tools: tools != null ? tools : [],
1936
2040
  toolChoice,
1937
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
2041
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
2042
+ cacheControlValidator
1938
2043
  }
1939
2044
  );
2045
+ const cacheWarnings = cacheControlValidator.getWarnings();
1940
2046
  return {
1941
2047
  args: {
1942
2048
  ...baseArgs,
1943
2049
  tools: anthropicTools2,
1944
2050
  tool_choice: anthropicToolChoice
1945
2051
  },
1946
- warnings: [...warnings, ...toolWarnings],
2052
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1947
2053
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1948
2054
  usesJsonResponseTool: jsonResponseTool != null
1949
2055
  };
@@ -1990,7 +2096,7 @@ var AnthropicMessagesLanguageModel = class {
1990
2096
  });
1991
2097
  }
1992
2098
  async doGenerate(options) {
1993
- var _a, _b, _c, _d, _e, _f;
2099
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1994
2100
  const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
1995
2101
  const citationDocuments = this.extractCitationDocuments(options.prompt);
1996
2102
  const {
@@ -2273,7 +2379,16 @@ var AnthropicMessagesLanguageModel = class {
2273
2379
  anthropic: {
2274
2380
  usage: response.usage,
2275
2381
  cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null,
2276
- stopSequence: (_f = response.stop_sequence) != null ? _f : null
2382
+ stopSequence: (_f = response.stop_sequence) != null ? _f : null,
2383
+ container: response.container ? {
2384
+ expiresAt: response.container.expires_at,
2385
+ id: response.container.id,
2386
+ skills: (_h = (_g = response.container.skills) == null ? void 0 : _g.map((skill) => ({
2387
+ type: skill.type,
2388
+ skillId: skill.skill_id,
2389
+ version: skill.version
2390
+ }))) != null ? _h : null
2391
+ } : null
2277
2392
  }
2278
2393
  }
2279
2394
  };
@@ -2304,6 +2419,7 @@ var AnthropicMessagesLanguageModel = class {
2304
2419
  let rawUsage = void 0;
2305
2420
  let cacheCreationInputTokens = null;
2306
2421
  let stopSequence = null;
2422
+ let container = null;
2307
2423
  let blockType = void 0;
2308
2424
  const generateId2 = this.generateId;
2309
2425
  return {
@@ -2313,7 +2429,7 @@ var AnthropicMessagesLanguageModel = class {
2313
2429
  controller.enqueue({ type: "stream-start", warnings });
2314
2430
  },
2315
2431
  transform(chunk, controller) {
2316
- var _a, _b, _c, _d, _e, _f, _g, _h;
2432
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2317
2433
  if (options.includeRawChunks) {
2318
2434
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2319
2435
  }
@@ -2722,6 +2838,15 @@ var AnthropicMessagesLanguageModel = class {
2722
2838
  isJsonResponseFromTool: usesJsonResponseTool
2723
2839
  });
2724
2840
  stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2841
+ container = value.delta.container != null ? {
2842
+ expiresAt: value.delta.container.expires_at,
2843
+ id: value.delta.container.id,
2844
+ skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2845
+ type: skill.type,
2846
+ skillId: skill.skill_id,
2847
+ version: skill.version
2848
+ }))) != null ? _j : null
2849
+ } : null;
2725
2850
  rawUsage = {
2726
2851
  ...rawUsage,
2727
2852
  ...value.usage
@@ -2737,7 +2862,8 @@ var AnthropicMessagesLanguageModel = class {
2737
2862
  anthropic: {
2738
2863
  usage: rawUsage != null ? rawUsage : null,
2739
2864
  cacheCreationInputTokens,
2740
- stopSequence
2865
+ stopSequence,
2866
+ container
2741
2867
  }
2742
2868
  }
2743
2869
  });