@ai-sdk/anthropic 2.0.32 → 2.0.34

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.
@@ -224,7 +224,18 @@ var anthropicMessagesResponseSchema = lazySchema2(
224
224
  output_tokens: z2.number(),
225
225
  cache_creation_input_tokens: z2.number().nullish(),
226
226
  cache_read_input_tokens: z2.number().nullish()
227
- })
227
+ }),
228
+ container: z2.object({
229
+ expires_at: z2.string(),
230
+ id: z2.string(),
231
+ skills: z2.array(
232
+ z2.object({
233
+ type: z2.union([z2.literal("anthropic"), z2.literal("custom")]),
234
+ skill_id: z2.string(),
235
+ version: z2.string()
236
+ })
237
+ ).nullish()
238
+ }).nullish()
228
239
  })
229
240
  )
230
241
  );
@@ -454,7 +465,21 @@ var anthropicMessagesChunkSchema = lazySchema2(
454
465
  type: z2.literal("message_delta"),
455
466
  delta: z2.object({
456
467
  stop_reason: z2.string().nullish(),
457
- stop_sequence: z2.string().nullish()
468
+ stop_sequence: z2.string().nullish(),
469
+ container: z2.object({
470
+ expires_at: z2.string(),
471
+ id: z2.string(),
472
+ skills: z2.array(
473
+ z2.object({
474
+ type: z2.union([
475
+ z2.literal("anthropic"),
476
+ z2.literal("custom")
477
+ ]),
478
+ skill_id: z2.string(),
479
+ version: z2.string()
480
+ })
481
+ ).nullish()
482
+ }).nullish()
458
483
  }),
459
484
  usage: z2.looseObject({
460
485
  output_tokens: z2.number(),
@@ -546,12 +571,46 @@ import {
546
571
  } from "@ai-sdk/provider";
547
572
 
548
573
  // src/get-cache-control.ts
574
+ var MAX_CACHE_BREAKPOINTS = 4;
549
575
  function getCacheControl(providerMetadata) {
550
576
  var _a;
551
577
  const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
552
578
  const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.cache_control;
553
579
  return cacheControlValue;
554
580
  }
581
+ var CacheControlValidator = class {
582
+ constructor() {
583
+ this.breakpointCount = 0;
584
+ this.warnings = [];
585
+ }
586
+ getCacheControl(providerMetadata, context) {
587
+ const cacheControlValue = getCacheControl(providerMetadata);
588
+ if (!cacheControlValue) {
589
+ return void 0;
590
+ }
591
+ if (!context.canCache) {
592
+ this.warnings.push({
593
+ type: "unsupported-setting",
594
+ setting: "cacheControl",
595
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
596
+ });
597
+ return void 0;
598
+ }
599
+ this.breakpointCount++;
600
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
601
+ this.warnings.push({
602
+ type: "unsupported-setting",
603
+ setting: "cacheControl",
604
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
605
+ });
606
+ return void 0;
607
+ }
608
+ return cacheControlValue;
609
+ }
610
+ getWarnings() {
611
+ return this.warnings;
612
+ }
613
+ };
555
614
 
556
615
  // src/tool/text-editor_20250728.ts
557
616
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -705,11 +764,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
705
764
  async function prepareTools({
706
765
  tools,
707
766
  toolChoice,
708
- disableParallelToolUse
767
+ disableParallelToolUse,
768
+ cacheControlValidator
709
769
  }) {
710
770
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
711
771
  const toolWarnings = [];
712
772
  const betas = /* @__PURE__ */ new Set();
773
+ const validator = cacheControlValidator || new CacheControlValidator();
713
774
  if (tools == null) {
714
775
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
715
776
  }
@@ -717,7 +778,10 @@ async function prepareTools({
717
778
  for (const tool of tools) {
718
779
  switch (tool.type) {
719
780
  case "function": {
720
- const cacheControl = getCacheControl(tool.providerOptions);
781
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
782
+ type: "tool definition",
783
+ canCache: true
784
+ });
721
785
  anthropicTools2.push({
722
786
  name: tool.name,
723
787
  description: tool.description,
@@ -732,7 +796,8 @@ async function prepareTools({
732
796
  betas.add("code-execution-2025-05-22");
733
797
  anthropicTools2.push({
734
798
  type: "code_execution_20250522",
735
- name: "code_execution"
799
+ name: "code_execution",
800
+ cache_control: void 0
736
801
  });
737
802
  break;
738
803
  }
@@ -751,7 +816,8 @@ async function prepareTools({
751
816
  type: "computer_20250124",
752
817
  display_width_px: tool.args.displayWidthPx,
753
818
  display_height_px: tool.args.displayHeightPx,
754
- display_number: tool.args.displayNumber
819
+ display_number: tool.args.displayNumber,
820
+ cache_control: void 0
755
821
  });
756
822
  break;
757
823
  }
@@ -762,7 +828,8 @@ async function prepareTools({
762
828
  type: "computer_20241022",
763
829
  display_width_px: tool.args.displayWidthPx,
764
830
  display_height_px: tool.args.displayHeightPx,
765
- display_number: tool.args.displayNumber
831
+ display_number: tool.args.displayNumber,
832
+ cache_control: void 0
766
833
  });
767
834
  break;
768
835
  }
@@ -770,7 +837,8 @@ async function prepareTools({
770
837
  betas.add("computer-use-2025-01-24");
771
838
  anthropicTools2.push({
772
839
  name: "str_replace_editor",
773
- type: "text_editor_20250124"
840
+ type: "text_editor_20250124",
841
+ cache_control: void 0
774
842
  });
775
843
  break;
776
844
  }
@@ -778,7 +846,8 @@ async function prepareTools({
778
846
  betas.add("computer-use-2024-10-22");
779
847
  anthropicTools2.push({
780
848
  name: "str_replace_editor",
781
- type: "text_editor_20241022"
849
+ type: "text_editor_20241022",
850
+ cache_control: void 0
782
851
  });
783
852
  break;
784
853
  }
@@ -786,7 +855,8 @@ async function prepareTools({
786
855
  betas.add("computer-use-2025-01-24");
787
856
  anthropicTools2.push({
788
857
  name: "str_replace_based_edit_tool",
789
- type: "text_editor_20250429"
858
+ type: "text_editor_20250429",
859
+ cache_control: void 0
790
860
  });
791
861
  break;
792
862
  }
@@ -798,7 +868,8 @@ async function prepareTools({
798
868
  anthropicTools2.push({
799
869
  name: "str_replace_based_edit_tool",
800
870
  type: "text_editor_20250728",
801
- max_characters: args.maxCharacters
871
+ max_characters: args.maxCharacters,
872
+ cache_control: void 0
802
873
  });
803
874
  break;
804
875
  }
@@ -806,7 +877,8 @@ async function prepareTools({
806
877
  betas.add("computer-use-2025-01-24");
807
878
  anthropicTools2.push({
808
879
  name: "bash",
809
- type: "bash_20250124"
880
+ type: "bash_20250124",
881
+ cache_control: void 0
810
882
  });
811
883
  break;
812
884
  }
@@ -814,7 +886,8 @@ async function prepareTools({
814
886
  betas.add("computer-use-2024-10-22");
815
887
  anthropicTools2.push({
816
888
  name: "bash",
817
- type: "bash_20241022"
889
+ type: "bash_20241022",
890
+ cache_control: void 0
818
891
  });
819
892
  break;
820
893
  }
@@ -839,7 +912,8 @@ async function prepareTools({
839
912
  allowed_domains: args.allowedDomains,
840
913
  blocked_domains: args.blockedDomains,
841
914
  citations: args.citations,
842
- max_content_tokens: args.maxContentTokens
915
+ max_content_tokens: args.maxContentTokens,
916
+ cache_control: void 0
843
917
  });
844
918
  break;
845
919
  }
@@ -854,7 +928,8 @@ async function prepareTools({
854
928
  max_uses: args.maxUses,
855
929
  allowed_domains: args.allowedDomains,
856
930
  blocked_domains: args.blockedDomains,
857
- user_location: args.userLocation
931
+ user_location: args.userLocation,
932
+ cache_control: void 0
858
933
  });
859
934
  break;
860
935
  }
@@ -1080,11 +1155,13 @@ function convertToString(data) {
1080
1155
  async function convertToAnthropicMessagesPrompt({
1081
1156
  prompt,
1082
1157
  sendReasoning,
1083
- warnings
1158
+ warnings,
1159
+ cacheControlValidator
1084
1160
  }) {
1085
1161
  var _a, _b, _c, _d, _e;
1086
1162
  const betas = /* @__PURE__ */ new Set();
1087
1163
  const blocks = groupIntoBlocks(prompt);
1164
+ const validator = cacheControlValidator || new CacheControlValidator();
1088
1165
  let system = void 0;
1089
1166
  const messages = [];
1090
1167
  async function shouldEnableCitations(providerMetadata) {
@@ -1121,7 +1198,10 @@ async function convertToAnthropicMessagesPrompt({
1121
1198
  system = block.messages.map(({ content, providerOptions }) => ({
1122
1199
  type: "text",
1123
1200
  text: content,
1124
- cache_control: getCacheControl(providerOptions)
1201
+ cache_control: validator.getCacheControl(providerOptions, {
1202
+ type: "system message",
1203
+ canCache: true
1204
+ })
1125
1205
  }));
1126
1206
  break;
1127
1207
  }
@@ -1134,7 +1214,13 @@ async function convertToAnthropicMessagesPrompt({
1134
1214
  for (let j = 0; j < content.length; j++) {
1135
1215
  const part = content[j];
1136
1216
  const isLastPart = j === content.length - 1;
1137
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1217
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1218
+ type: "user message part",
1219
+ canCache: true
1220
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1221
+ type: "user message",
1222
+ canCache: true
1223
+ }) : void 0;
1138
1224
  switch (part.type) {
1139
1225
  case "text": {
1140
1226
  anthropicContent.push({
@@ -1222,7 +1308,13 @@ async function convertToAnthropicMessagesPrompt({
1222
1308
  for (let i2 = 0; i2 < content.length; i2++) {
1223
1309
  const part = content[i2];
1224
1310
  const isLastPart = i2 === content.length - 1;
1225
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1311
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1312
+ type: "tool result part",
1313
+ canCache: true
1314
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1315
+ type: "tool result message",
1316
+ canCache: true
1317
+ }) : void 0;
1226
1318
  const output = part.output;
1227
1319
  let contentValue;
1228
1320
  switch (output.type) {
@@ -1232,8 +1324,7 @@ async function convertToAnthropicMessagesPrompt({
1232
1324
  case "text":
1233
1325
  return {
1234
1326
  type: "text",
1235
- text: contentPart.text,
1236
- cache_control: void 0
1327
+ text: contentPart.text
1237
1328
  };
1238
1329
  case "media": {
1239
1330
  if (contentPart.mediaType.startsWith("image/")) {
@@ -1243,8 +1334,7 @@ async function convertToAnthropicMessagesPrompt({
1243
1334
  type: "base64",
1244
1335
  media_type: contentPart.mediaType,
1245
1336
  data: contentPart.data
1246
- },
1247
- cache_control: void 0
1337
+ }
1248
1338
  };
1249
1339
  }
1250
1340
  if (contentPart.mediaType === "application/pdf") {
@@ -1255,8 +1345,7 @@ async function convertToAnthropicMessagesPrompt({
1255
1345
  type: "base64",
1256
1346
  media_type: contentPart.mediaType,
1257
1347
  data: contentPart.data
1258
- },
1259
- cache_control: void 0
1348
+ }
1260
1349
  };
1261
1350
  }
1262
1351
  throw new UnsupportedFunctionalityError2({
@@ -1304,7 +1393,13 @@ async function convertToAnthropicMessagesPrompt({
1304
1393
  for (let k = 0; k < content.length; k++) {
1305
1394
  const part = content[k];
1306
1395
  const isLastContentPart = k === content.length - 1;
1307
- const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1396
+ const cacheControl = (_e = validator.getCacheControl(part.providerOptions, {
1397
+ type: "assistant message part",
1398
+ canCache: true
1399
+ })) != null ? _e : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1400
+ type: "assistant message",
1401
+ canCache: true
1402
+ }) : void 0;
1308
1403
  switch (part.type) {
1309
1404
  case "text": {
1310
1405
  anthropicContent.push({
@@ -1328,17 +1423,23 @@ async function convertToAnthropicMessagesPrompt({
1328
1423
  });
1329
1424
  if (reasoningMetadata != null) {
1330
1425
  if (reasoningMetadata.signature != null) {
1426
+ validator.getCacheControl(part.providerOptions, {
1427
+ type: "thinking block",
1428
+ canCache: false
1429
+ });
1331
1430
  anthropicContent.push({
1332
1431
  type: "thinking",
1333
1432
  thinking: part.text,
1334
- signature: reasoningMetadata.signature,
1335
- cache_control: cacheControl
1433
+ signature: reasoningMetadata.signature
1336
1434
  });
1337
1435
  } else if (reasoningMetadata.redactedData != null) {
1436
+ validator.getCacheControl(part.providerOptions, {
1437
+ type: "redacted thinking block",
1438
+ canCache: false
1439
+ });
1338
1440
  anthropicContent.push({
1339
1441
  type: "redacted_thinking",
1340
- data: reasoningMetadata.redactedData,
1341
- cache_control: cacheControl
1442
+ data: reasoningMetadata.redactedData
1342
1443
  });
1343
1444
  } else {
1344
1445
  warnings.push({
@@ -1713,10 +1814,12 @@ var AnthropicMessagesLanguageModel = class {
1713
1814
  providerOptions,
1714
1815
  schema: anthropicProviderOptions
1715
1816
  });
1817
+ const cacheControlValidator = new CacheControlValidator();
1716
1818
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1717
1819
  prompt,
1718
1820
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1719
- warnings
1821
+ warnings,
1822
+ cacheControlValidator
1720
1823
  });
1721
1824
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1722
1825
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1814,20 +1917,23 @@ var AnthropicMessagesLanguageModel = class {
1814
1917
  jsonResponseTool != null ? {
1815
1918
  tools: [jsonResponseTool],
1816
1919
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1817
- disableParallelToolUse: true
1920
+ disableParallelToolUse: true,
1921
+ cacheControlValidator
1818
1922
  } : {
1819
1923
  tools: tools != null ? tools : [],
1820
1924
  toolChoice,
1821
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
1925
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
1926
+ cacheControlValidator
1822
1927
  }
1823
1928
  );
1929
+ const cacheWarnings = cacheControlValidator.getWarnings();
1824
1930
  return {
1825
1931
  args: {
1826
1932
  ...baseArgs,
1827
1933
  tools: anthropicTools2,
1828
1934
  tool_choice: anthropicToolChoice
1829
1935
  },
1830
- warnings: [...warnings, ...toolWarnings],
1936
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1831
1937
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1832
1938
  usesJsonResponseTool: jsonResponseTool != null
1833
1939
  };
@@ -1874,7 +1980,7 @@ var AnthropicMessagesLanguageModel = class {
1874
1980
  });
1875
1981
  }
1876
1982
  async doGenerate(options) {
1877
- var _a, _b, _c, _d, _e, _f;
1983
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1878
1984
  const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
1879
1985
  const citationDocuments = this.extractCitationDocuments(options.prompt);
1880
1986
  const {
@@ -2125,7 +2231,16 @@ var AnthropicMessagesLanguageModel = class {
2125
2231
  anthropic: {
2126
2232
  usage: response.usage,
2127
2233
  cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null,
2128
- stopSequence: (_f = response.stop_sequence) != null ? _f : null
2234
+ stopSequence: (_f = response.stop_sequence) != null ? _f : null,
2235
+ container: response.container ? {
2236
+ expiresAt: response.container.expires_at,
2237
+ id: response.container.id,
2238
+ skills: (_h = (_g = response.container.skills) == null ? void 0 : _g.map((skill) => ({
2239
+ type: skill.type,
2240
+ skillId: skill.skill_id,
2241
+ version: skill.version
2242
+ }))) != null ? _h : null
2243
+ } : null
2129
2244
  }
2130
2245
  }
2131
2246
  };
@@ -2155,6 +2270,7 @@ var AnthropicMessagesLanguageModel = class {
2155
2270
  let rawUsage = void 0;
2156
2271
  let cacheCreationInputTokens = null;
2157
2272
  let stopSequence = null;
2273
+ let container = null;
2158
2274
  let blockType = void 0;
2159
2275
  const generateId2 = this.generateId;
2160
2276
  return {
@@ -2164,7 +2280,7 @@ var AnthropicMessagesLanguageModel = class {
2164
2280
  controller.enqueue({ type: "stream-start", warnings });
2165
2281
  },
2166
2282
  transform(chunk, controller) {
2167
- var _a, _b, _c, _d, _e, _f, _g, _h;
2283
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2168
2284
  if (options.includeRawChunks) {
2169
2285
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2170
2286
  }
@@ -2545,6 +2661,15 @@ var AnthropicMessagesLanguageModel = class {
2545
2661
  isJsonResponseFromTool: usesJsonResponseTool
2546
2662
  });
2547
2663
  stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2664
+ container = value.delta.container != null ? {
2665
+ expiresAt: value.delta.container.expires_at,
2666
+ id: value.delta.container.id,
2667
+ skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2668
+ type: skill.type,
2669
+ skillId: skill.skill_id,
2670
+ version: skill.version
2671
+ }))) != null ? _j : null
2672
+ } : null;
2548
2673
  rawUsage = {
2549
2674
  ...rawUsage,
2550
2675
  ...value.usage
@@ -2560,7 +2685,8 @@ var AnthropicMessagesLanguageModel = class {
2560
2685
  anthropic: {
2561
2686
  usage: rawUsage != null ? rawUsage : null,
2562
2687
  cacheCreationInputTokens,
2563
- stopSequence
2688
+ stopSequence,
2689
+ container
2564
2690
  }
2565
2691
  }
2566
2692
  });