@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.
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.32" : "0.0.0-test";
13
+ var VERSION = true ? "2.0.34" : "0.0.0-test";
14
14
 
15
15
  // src/anthropic-messages-language-model.ts
16
16
  import {
@@ -238,7 +238,18 @@ var anthropicMessagesResponseSchema = lazySchema2(
238
238
  output_tokens: z2.number(),
239
239
  cache_creation_input_tokens: z2.number().nullish(),
240
240
  cache_read_input_tokens: z2.number().nullish()
241
- })
241
+ }),
242
+ container: z2.object({
243
+ expires_at: z2.string(),
244
+ id: z2.string(),
245
+ skills: z2.array(
246
+ z2.object({
247
+ type: z2.union([z2.literal("anthropic"), z2.literal("custom")]),
248
+ skill_id: z2.string(),
249
+ version: z2.string()
250
+ })
251
+ ).nullish()
252
+ }).nullish()
242
253
  })
243
254
  )
244
255
  );
@@ -468,7 +479,21 @@ var anthropicMessagesChunkSchema = lazySchema2(
468
479
  type: z2.literal("message_delta"),
469
480
  delta: z2.object({
470
481
  stop_reason: z2.string().nullish(),
471
- stop_sequence: z2.string().nullish()
482
+ stop_sequence: z2.string().nullish(),
483
+ container: z2.object({
484
+ expires_at: z2.string(),
485
+ id: z2.string(),
486
+ skills: z2.array(
487
+ z2.object({
488
+ type: z2.union([
489
+ z2.literal("anthropic"),
490
+ z2.literal("custom")
491
+ ]),
492
+ skill_id: z2.string(),
493
+ version: z2.string()
494
+ })
495
+ ).nullish()
496
+ }).nullish()
472
497
  }),
473
498
  usage: z2.looseObject({
474
499
  output_tokens: z2.number(),
@@ -560,12 +585,46 @@ import {
560
585
  } from "@ai-sdk/provider";
561
586
 
562
587
  // src/get-cache-control.ts
588
+ var MAX_CACHE_BREAKPOINTS = 4;
563
589
  function getCacheControl(providerMetadata) {
564
590
  var _a;
565
591
  const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
566
592
  const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control;
567
593
  return cacheControlValue;
568
594
  }
595
+ var CacheControlValidator = class {
596
+ constructor() {
597
+ this.breakpointCount = 0;
598
+ this.warnings = [];
599
+ }
600
+ getCacheControl(providerMetadata, context) {
601
+ const cacheControlValue = getCacheControl(providerMetadata);
602
+ if (!cacheControlValue) {
603
+ return void 0;
604
+ }
605
+ if (!context.canCache) {
606
+ this.warnings.push({
607
+ type: "unsupported-setting",
608
+ setting: "cacheControl",
609
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
610
+ });
611
+ return void 0;
612
+ }
613
+ this.breakpointCount++;
614
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
615
+ this.warnings.push({
616
+ type: "unsupported-setting",
617
+ setting: "cacheControl",
618
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
619
+ });
620
+ return void 0;
621
+ }
622
+ return cacheControlValue;
623
+ }
624
+ getWarnings() {
625
+ return this.warnings;
626
+ }
627
+ };
569
628
 
570
629
  // src/tool/text-editor_20250728.ts
571
630
  import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
@@ -719,11 +778,13 @@ import { validateTypes } from "@ai-sdk/provider-utils";
719
778
  async function prepareTools({
720
779
  tools,
721
780
  toolChoice,
722
- disableParallelToolUse
781
+ disableParallelToolUse,
782
+ cacheControlValidator
723
783
  }) {
724
784
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
725
785
  const toolWarnings = [];
726
786
  const betas = /* @__PURE__ */ new Set();
787
+ const validator = cacheControlValidator || new CacheControlValidator();
727
788
  if (tools == null) {
728
789
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
729
790
  }
@@ -731,7 +792,10 @@ async function prepareTools({
731
792
  for (const tool of tools) {
732
793
  switch (tool.type) {
733
794
  case "function": {
734
- const cacheControl = getCacheControl(tool.providerOptions);
795
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
796
+ type: "tool definition",
797
+ canCache: true
798
+ });
735
799
  anthropicTools2.push({
736
800
  name: tool.name,
737
801
  description: tool.description,
@@ -746,7 +810,8 @@ async function prepareTools({
746
810
  betas.add("code-execution-2025-05-22");
747
811
  anthropicTools2.push({
748
812
  type: "code_execution_20250522",
749
- name: "code_execution"
813
+ name: "code_execution",
814
+ cache_control: void 0
750
815
  });
751
816
  break;
752
817
  }
@@ -765,7 +830,8 @@ async function prepareTools({
765
830
  type: "computer_20250124",
766
831
  display_width_px: tool.args.displayWidthPx,
767
832
  display_height_px: tool.args.displayHeightPx,
768
- display_number: tool.args.displayNumber
833
+ display_number: tool.args.displayNumber,
834
+ cache_control: void 0
769
835
  });
770
836
  break;
771
837
  }
@@ -776,7 +842,8 @@ async function prepareTools({
776
842
  type: "computer_20241022",
777
843
  display_width_px: tool.args.displayWidthPx,
778
844
  display_height_px: tool.args.displayHeightPx,
779
- display_number: tool.args.displayNumber
845
+ display_number: tool.args.displayNumber,
846
+ cache_control: void 0
780
847
  });
781
848
  break;
782
849
  }
@@ -784,7 +851,8 @@ async function prepareTools({
784
851
  betas.add("computer-use-2025-01-24");
785
852
  anthropicTools2.push({
786
853
  name: "str_replace_editor",
787
- type: "text_editor_20250124"
854
+ type: "text_editor_20250124",
855
+ cache_control: void 0
788
856
  });
789
857
  break;
790
858
  }
@@ -792,7 +860,8 @@ async function prepareTools({
792
860
  betas.add("computer-use-2024-10-22");
793
861
  anthropicTools2.push({
794
862
  name: "str_replace_editor",
795
- type: "text_editor_20241022"
863
+ type: "text_editor_20241022",
864
+ cache_control: void 0
796
865
  });
797
866
  break;
798
867
  }
@@ -800,7 +869,8 @@ async function prepareTools({
800
869
  betas.add("computer-use-2025-01-24");
801
870
  anthropicTools2.push({
802
871
  name: "str_replace_based_edit_tool",
803
- type: "text_editor_20250429"
872
+ type: "text_editor_20250429",
873
+ cache_control: void 0
804
874
  });
805
875
  break;
806
876
  }
@@ -812,7 +882,8 @@ async function prepareTools({
812
882
  anthropicTools2.push({
813
883
  name: "str_replace_based_edit_tool",
814
884
  type: "text_editor_20250728",
815
- max_characters: args.maxCharacters
885
+ max_characters: args.maxCharacters,
886
+ cache_control: void 0
816
887
  });
817
888
  break;
818
889
  }
@@ -820,7 +891,8 @@ async function prepareTools({
820
891
  betas.add("computer-use-2025-01-24");
821
892
  anthropicTools2.push({
822
893
  name: "bash",
823
- type: "bash_20250124"
894
+ type: "bash_20250124",
895
+ cache_control: void 0
824
896
  });
825
897
  break;
826
898
  }
@@ -828,7 +900,8 @@ async function prepareTools({
828
900
  betas.add("computer-use-2024-10-22");
829
901
  anthropicTools2.push({
830
902
  name: "bash",
831
- type: "bash_20241022"
903
+ type: "bash_20241022",
904
+ cache_control: void 0
832
905
  });
833
906
  break;
834
907
  }
@@ -853,7 +926,8 @@ async function prepareTools({
853
926
  allowed_domains: args.allowedDomains,
854
927
  blocked_domains: args.blockedDomains,
855
928
  citations: args.citations,
856
- max_content_tokens: args.maxContentTokens
929
+ max_content_tokens: args.maxContentTokens,
930
+ cache_control: void 0
857
931
  });
858
932
  break;
859
933
  }
@@ -868,7 +942,8 @@ async function prepareTools({
868
942
  max_uses: args.maxUses,
869
943
  allowed_domains: args.allowedDomains,
870
944
  blocked_domains: args.blockedDomains,
871
- user_location: args.userLocation
945
+ user_location: args.userLocation,
946
+ cache_control: void 0
872
947
  });
873
948
  break;
874
949
  }
@@ -1094,11 +1169,13 @@ function convertToString(data) {
1094
1169
  async function convertToAnthropicMessagesPrompt({
1095
1170
  prompt,
1096
1171
  sendReasoning,
1097
- warnings
1172
+ warnings,
1173
+ cacheControlValidator
1098
1174
  }) {
1099
1175
  var _a, _b, _c, _d, _e;
1100
1176
  const betas = /* @__PURE__ */ new Set();
1101
1177
  const blocks = groupIntoBlocks(prompt);
1178
+ const validator = cacheControlValidator || new CacheControlValidator();
1102
1179
  let system = void 0;
1103
1180
  const messages = [];
1104
1181
  async function shouldEnableCitations(providerMetadata) {
@@ -1135,7 +1212,10 @@ async function convertToAnthropicMessagesPrompt({
1135
1212
  system = block.messages.map(({ content, providerOptions }) => ({
1136
1213
  type: "text",
1137
1214
  text: content,
1138
- cache_control: getCacheControl(providerOptions)
1215
+ cache_control: validator.getCacheControl(providerOptions, {
1216
+ type: "system message",
1217
+ canCache: true
1218
+ })
1139
1219
  }));
1140
1220
  break;
1141
1221
  }
@@ -1148,7 +1228,13 @@ async function convertToAnthropicMessagesPrompt({
1148
1228
  for (let j = 0; j < content.length; j++) {
1149
1229
  const part = content[j];
1150
1230
  const isLastPart = j === content.length - 1;
1151
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1231
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1232
+ type: "user message part",
1233
+ canCache: true
1234
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1235
+ type: "user message",
1236
+ canCache: true
1237
+ }) : void 0;
1152
1238
  switch (part.type) {
1153
1239
  case "text": {
1154
1240
  anthropicContent.push({
@@ -1236,7 +1322,13 @@ async function convertToAnthropicMessagesPrompt({
1236
1322
  for (let i2 = 0; i2 < content.length; i2++) {
1237
1323
  const part = content[i2];
1238
1324
  const isLastPart = i2 === content.length - 1;
1239
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1325
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1326
+ type: "tool result part",
1327
+ canCache: true
1328
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1329
+ type: "tool result message",
1330
+ canCache: true
1331
+ }) : void 0;
1240
1332
  const output = part.output;
1241
1333
  let contentValue;
1242
1334
  switch (output.type) {
@@ -1246,8 +1338,7 @@ async function convertToAnthropicMessagesPrompt({
1246
1338
  case "text":
1247
1339
  return {
1248
1340
  type: "text",
1249
- text: contentPart.text,
1250
- cache_control: void 0
1341
+ text: contentPart.text
1251
1342
  };
1252
1343
  case "media": {
1253
1344
  if (contentPart.mediaType.startsWith("image/")) {
@@ -1257,8 +1348,7 @@ async function convertToAnthropicMessagesPrompt({
1257
1348
  type: "base64",
1258
1349
  media_type: contentPart.mediaType,
1259
1350
  data: contentPart.data
1260
- },
1261
- cache_control: void 0
1351
+ }
1262
1352
  };
1263
1353
  }
1264
1354
  if (contentPart.mediaType === "application/pdf") {
@@ -1269,8 +1359,7 @@ async function convertToAnthropicMessagesPrompt({
1269
1359
  type: "base64",
1270
1360
  media_type: contentPart.mediaType,
1271
1361
  data: contentPart.data
1272
- },
1273
- cache_control: void 0
1362
+ }
1274
1363
  };
1275
1364
  }
1276
1365
  throw new UnsupportedFunctionalityError2({
@@ -1318,7 +1407,13 @@ async function convertToAnthropicMessagesPrompt({
1318
1407
  for (let k = 0; k < content.length; k++) {
1319
1408
  const part = content[k];
1320
1409
  const isLastContentPart = k === content.length - 1;
1321
- const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1410
+ const cacheControl = (_e = validator.getCacheControl(part.providerOptions, {
1411
+ type: "assistant message part",
1412
+ canCache: true
1413
+ })) != null ? _e : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1414
+ type: "assistant message",
1415
+ canCache: true
1416
+ }) : void 0;
1322
1417
  switch (part.type) {
1323
1418
  case "text": {
1324
1419
  anthropicContent.push({
@@ -1342,17 +1437,23 @@ async function convertToAnthropicMessagesPrompt({
1342
1437
  });
1343
1438
  if (reasoningMetadata != null) {
1344
1439
  if (reasoningMetadata.signature != null) {
1440
+ validator.getCacheControl(part.providerOptions, {
1441
+ type: "thinking block",
1442
+ canCache: false
1443
+ });
1345
1444
  anthropicContent.push({
1346
1445
  type: "thinking",
1347
1446
  thinking: part.text,
1348
- signature: reasoningMetadata.signature,
1349
- cache_control: cacheControl
1447
+ signature: reasoningMetadata.signature
1350
1448
  });
1351
1449
  } else if (reasoningMetadata.redactedData != null) {
1450
+ validator.getCacheControl(part.providerOptions, {
1451
+ type: "redacted thinking block",
1452
+ canCache: false
1453
+ });
1352
1454
  anthropicContent.push({
1353
1455
  type: "redacted_thinking",
1354
- data: reasoningMetadata.redactedData,
1355
- cache_control: cacheControl
1456
+ data: reasoningMetadata.redactedData
1356
1457
  });
1357
1458
  } else {
1358
1459
  warnings.push({
@@ -1727,10 +1828,12 @@ var AnthropicMessagesLanguageModel = class {
1727
1828
  providerOptions,
1728
1829
  schema: anthropicProviderOptions
1729
1830
  });
1831
+ const cacheControlValidator = new CacheControlValidator();
1730
1832
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1731
1833
  prompt,
1732
1834
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1733
- warnings
1835
+ warnings,
1836
+ cacheControlValidator
1734
1837
  });
1735
1838
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1736
1839
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1828,20 +1931,23 @@ var AnthropicMessagesLanguageModel = class {
1828
1931
  jsonResponseTool != null ? {
1829
1932
  tools: [jsonResponseTool],
1830
1933
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1831
- disableParallelToolUse: true
1934
+ disableParallelToolUse: true,
1935
+ cacheControlValidator
1832
1936
  } : {
1833
1937
  tools: tools != null ? tools : [],
1834
1938
  toolChoice,
1835
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
1939
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
1940
+ cacheControlValidator
1836
1941
  }
1837
1942
  );
1943
+ const cacheWarnings = cacheControlValidator.getWarnings();
1838
1944
  return {
1839
1945
  args: {
1840
1946
  ...baseArgs,
1841
1947
  tools: anthropicTools2,
1842
1948
  tool_choice: anthropicToolChoice
1843
1949
  },
1844
- warnings: [...warnings, ...toolWarnings],
1950
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1845
1951
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1846
1952
  usesJsonResponseTool: jsonResponseTool != null
1847
1953
  };
@@ -1888,7 +1994,7 @@ var AnthropicMessagesLanguageModel = class {
1888
1994
  });
1889
1995
  }
1890
1996
  async doGenerate(options) {
1891
- var _a, _b, _c, _d, _e, _f;
1997
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1892
1998
  const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
1893
1999
  const citationDocuments = this.extractCitationDocuments(options.prompt);
1894
2000
  const {
@@ -2139,7 +2245,16 @@ var AnthropicMessagesLanguageModel = class {
2139
2245
  anthropic: {
2140
2246
  usage: response.usage,
2141
2247
  cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null,
2142
- stopSequence: (_f = response.stop_sequence) != null ? _f : null
2248
+ stopSequence: (_f = response.stop_sequence) != null ? _f : null,
2249
+ container: response.container ? {
2250
+ expiresAt: response.container.expires_at,
2251
+ id: response.container.id,
2252
+ skills: (_h = (_g = response.container.skills) == null ? void 0 : _g.map((skill) => ({
2253
+ type: skill.type,
2254
+ skillId: skill.skill_id,
2255
+ version: skill.version
2256
+ }))) != null ? _h : null
2257
+ } : null
2143
2258
  }
2144
2259
  }
2145
2260
  };
@@ -2169,6 +2284,7 @@ var AnthropicMessagesLanguageModel = class {
2169
2284
  let rawUsage = void 0;
2170
2285
  let cacheCreationInputTokens = null;
2171
2286
  let stopSequence = null;
2287
+ let container = null;
2172
2288
  let blockType = void 0;
2173
2289
  const generateId3 = this.generateId;
2174
2290
  return {
@@ -2178,7 +2294,7 @@ var AnthropicMessagesLanguageModel = class {
2178
2294
  controller.enqueue({ type: "stream-start", warnings });
2179
2295
  },
2180
2296
  transform(chunk, controller) {
2181
- var _a, _b, _c, _d, _e, _f, _g, _h;
2297
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2182
2298
  if (options.includeRawChunks) {
2183
2299
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2184
2300
  }
@@ -2559,6 +2675,15 @@ var AnthropicMessagesLanguageModel = class {
2559
2675
  isJsonResponseFromTool: usesJsonResponseTool
2560
2676
  });
2561
2677
  stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2678
+ container = value.delta.container != null ? {
2679
+ expiresAt: value.delta.container.expires_at,
2680
+ id: value.delta.container.id,
2681
+ skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2682
+ type: skill.type,
2683
+ skillId: skill.skill_id,
2684
+ version: skill.version
2685
+ }))) != null ? _j : null
2686
+ } : null;
2562
2687
  rawUsage = {
2563
2688
  ...rawUsage,
2564
2689
  ...value.usage
@@ -2574,7 +2699,8 @@ var AnthropicMessagesLanguageModel = class {
2574
2699
  anthropic: {
2575
2700
  usage: rawUsage != null ? rawUsage : null,
2576
2701
  cacheCreationInputTokens,
2577
- stopSequence
2702
+ stopSequence,
2703
+ container
2578
2704
  }
2579
2705
  }
2580
2706
  });