@ai-sdk/anthropic 2.0.59 → 2.0.61

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
@@ -11,7 +11,7 @@ import {
11
11
  } from "@ai-sdk/provider-utils";
12
12
 
13
13
  // src/version.ts
14
- var VERSION = true ? "2.0.59" : "0.0.0-test";
14
+ var VERSION = true ? "2.0.61" : "0.0.0-test";
15
15
 
16
16
  // src/anthropic-messages-language-model.ts
17
17
  import {
@@ -102,6 +102,10 @@ var anthropicMessagesResponseSchema = lazySchema2(
102
102
  type: z2.literal("redacted_thinking"),
103
103
  data: z2.string()
104
104
  }),
105
+ z2.object({
106
+ type: z2.literal("compaction"),
107
+ content: z2.string()
108
+ }),
105
109
  z2.object({
106
110
  type: z2.literal("tool_use"),
107
111
  id: z2.string(),
@@ -246,8 +250,28 @@ var anthropicMessagesResponseSchema = lazySchema2(
246
250
  input_tokens: z2.number(),
247
251
  output_tokens: z2.number(),
248
252
  cache_creation_input_tokens: z2.number().nullish(),
249
- cache_read_input_tokens: z2.number().nullish()
253
+ cache_read_input_tokens: z2.number().nullish(),
254
+ iterations: z2.array(
255
+ z2.object({
256
+ type: z2.union([z2.literal("compaction"), z2.literal("message")]),
257
+ input_tokens: z2.number(),
258
+ output_tokens: z2.number()
259
+ })
260
+ ).nullish()
250
261
  }),
262
+ context_management: z2.object({
263
+ applied_edits: z2.array(
264
+ z2.union([
265
+ z2.object({
266
+ type: z2.literal("clear_01"),
267
+ cleared_input_tokens: z2.number()
268
+ }),
269
+ z2.object({
270
+ type: z2.literal("compact_20260112")
271
+ })
272
+ ])
273
+ )
274
+ }).nullish(),
251
275
  container: z2.object({
252
276
  expires_at: z2.string(),
253
277
  id: z2.string(),
@@ -298,6 +322,10 @@ var anthropicMessagesChunkSchema = lazySchema2(
298
322
  type: z2.literal("redacted_thinking"),
299
323
  data: z2.string()
300
324
  }),
325
+ z2.object({
326
+ type: z2.literal("compaction"),
327
+ content: z2.string().nullish()
328
+ }),
301
329
  z2.object({
302
330
  type: z2.literal("server_tool_use"),
303
331
  id: z2.string(),
@@ -450,6 +478,10 @@ var anthropicMessagesChunkSchema = lazySchema2(
450
478
  type: z2.literal("signature_delta"),
451
479
  signature: z2.string()
452
480
  }),
481
+ z2.object({
482
+ type: z2.literal("compaction_delta"),
483
+ content: z2.string()
484
+ }),
453
485
  z2.object({
454
486
  type: z2.literal("citations_delta"),
455
487
  citation: z2.discriminatedUnion("type", [
@@ -515,8 +547,28 @@ var anthropicMessagesChunkSchema = lazySchema2(
515
547
  input_tokens: z2.number().nullish(),
516
548
  output_tokens: z2.number(),
517
549
  cache_creation_input_tokens: z2.number().nullish(),
518
- cache_read_input_tokens: z2.number().nullish()
519
- })
550
+ cache_read_input_tokens: z2.number().nullish(),
551
+ iterations: z2.array(
552
+ z2.object({
553
+ type: z2.union([z2.literal("compaction"), z2.literal("message")]),
554
+ input_tokens: z2.number(),
555
+ output_tokens: z2.number()
556
+ })
557
+ ).nullish()
558
+ }),
559
+ context_management: z2.object({
560
+ applied_edits: z2.array(
561
+ z2.union([
562
+ z2.object({
563
+ type: z2.literal("clear_01"),
564
+ cleared_input_tokens: z2.number()
565
+ }),
566
+ z2.object({
567
+ type: z2.literal("compact_20260112")
568
+ })
569
+ ])
570
+ )
571
+ }).nullish()
520
572
  }),
521
573
  z2.object({
522
574
  type: z2.literal("message_stop")
@@ -622,7 +674,45 @@ var anthropicProviderOptions = z3.object({
622
674
  /**
623
675
  * @default 'high'
624
676
  */
625
- effort: z3.enum(["low", "medium", "high", "max"]).optional()
677
+ effort: z3.enum(["low", "medium", "high", "max"]).optional(),
678
+ /**
679
+ * Enable fast mode for faster inference (2.5x faster output token speeds).
680
+ * Only supported with claude-opus-4-6.
681
+ */
682
+ speed: z3.literal("fast").optional(),
683
+ /**
684
+ * Context management configuration for automatic context window management.
685
+ * Enables features like automatic compaction and clearing of tool uses/thinking blocks.
686
+ */
687
+ contextManagement: z3.object({
688
+ edits: z3.array(
689
+ z3.discriminatedUnion("type", [
690
+ z3.object({
691
+ type: z3.literal("clear_01"),
692
+ trigger: z3.object({
693
+ type: z3.literal("input_tokens"),
694
+ value: z3.number()
695
+ }).optional(),
696
+ keep: z3.union([
697
+ z3.literal("all"),
698
+ z3.object({
699
+ type: z3.literal("thinking_turns"),
700
+ value: z3.number()
701
+ })
702
+ ]).optional()
703
+ }),
704
+ z3.object({
705
+ type: z3.literal("compact_20260112"),
706
+ trigger: z3.object({
707
+ type: z3.literal("input_tokens"),
708
+ value: z3.number()
709
+ }).optional(),
710
+ pauseAfterCompaction: z3.boolean().optional(),
711
+ instructions: z3.string().optional()
712
+ })
713
+ ])
714
+ )
715
+ }).optional()
626
716
  });
627
717
 
628
718
  // src/anthropic-prepare-tools.ts
@@ -1218,7 +1308,7 @@ async function convertToAnthropicMessagesPrompt({
1218
1308
  warnings,
1219
1309
  cacheControlValidator
1220
1310
  }) {
1221
- var _a, _b, _c, _d, _e;
1311
+ var _a, _b, _c, _d, _e, _f;
1222
1312
  const betas = /* @__PURE__ */ new Set();
1223
1313
  const blocks = groupIntoBlocks(prompt);
1224
1314
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -1462,16 +1552,25 @@ async function convertToAnthropicMessagesPrompt({
1462
1552
  }) : void 0;
1463
1553
  switch (part.type) {
1464
1554
  case "text": {
1465
- anthropicContent.push({
1466
- type: "text",
1467
- text: (
1468
- // trim the last text part if it's the last message in the block
1469
- // because Anthropic does not allow trailing whitespace
1470
- // in pre-filled assistant responses
1471
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1472
- ),
1473
- cache_control: cacheControl
1474
- });
1555
+ const textMetadata = (_f = part.providerOptions) == null ? void 0 : _f.anthropic;
1556
+ if ((textMetadata == null ? void 0 : textMetadata.type) === "compaction") {
1557
+ anthropicContent.push({
1558
+ type: "compaction",
1559
+ content: part.text,
1560
+ cache_control: cacheControl
1561
+ });
1562
+ } else {
1563
+ anthropicContent.push({
1564
+ type: "text",
1565
+ text: (
1566
+ // trim the last text part if it's the last message in the block
1567
+ // because Anthropic does not allow trailing whitespace
1568
+ // in pre-filled assistant responses
1569
+ isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1570
+ ),
1571
+ cache_control: cacheControl
1572
+ });
1573
+ }
1475
1574
  break;
1476
1575
  }
1477
1576
  case "reasoning": {
@@ -1760,6 +1859,8 @@ function mapAnthropicStopReason({
1760
1859
  return isJsonResponseFromTool ? "stop" : "tool-calls";
1761
1860
  case "max_tokens":
1762
1861
  return "length";
1862
+ case "compaction":
1863
+ return "other";
1763
1864
  default:
1764
1865
  return "unknown";
1765
1866
  }
@@ -1927,6 +2028,9 @@ var AnthropicMessagesLanguageModel = class {
1927
2028
  ...(anthropicOptions == null ? void 0 : anthropicOptions.effort) && {
1928
2029
  output_config: { effort: anthropicOptions.effort }
1929
2030
  },
2031
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.speed) && {
2032
+ speed: anthropicOptions.speed
2033
+ },
1930
2034
  // structured output:
1931
2035
  ...useStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && {
1932
2036
  output_format: {
@@ -1945,6 +2049,48 @@ var AnthropicMessagesLanguageModel = class {
1945
2049
  }))
1946
2050
  }
1947
2051
  },
2052
+ // context management:
2053
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.contextManagement) && {
2054
+ context_management: {
2055
+ edits: anthropicOptions.contextManagement.edits.map((edit) => {
2056
+ const convertTrigger = (trigger) => trigger ? {
2057
+ type: trigger.type,
2058
+ value: trigger.value
2059
+ } : void 0;
2060
+ switch (edit.type) {
2061
+ case "clear_01":
2062
+ return {
2063
+ type: edit.type,
2064
+ ...edit.trigger !== void 0 && {
2065
+ trigger: convertTrigger(edit.trigger)
2066
+ },
2067
+ ...edit.keep !== void 0 && {
2068
+ keep: edit.keep
2069
+ }
2070
+ };
2071
+ case "compact_20260112":
2072
+ return {
2073
+ type: edit.type,
2074
+ ...edit.trigger !== void 0 && {
2075
+ trigger: convertTrigger(edit.trigger)
2076
+ },
2077
+ ...edit.pauseAfterCompaction !== void 0 && {
2078
+ pause_after_compaction: edit.pauseAfterCompaction
2079
+ },
2080
+ ...edit.instructions !== void 0 && {
2081
+ instructions: edit.instructions
2082
+ }
2083
+ };
2084
+ default:
2085
+ warnings.push({
2086
+ type: "other",
2087
+ message: `Unknown context management edit type: ${edit.type}`
2088
+ });
2089
+ return edit;
2090
+ }
2091
+ })
2092
+ }
2093
+ },
1948
2094
  // prompt:
1949
2095
  system: messagesPrompt.system,
1950
2096
  messages: messagesPrompt.messages
@@ -2007,9 +2153,19 @@ var AnthropicMessagesLanguageModel = class {
2007
2153
  if (anthropicOptions == null ? void 0 : anthropicOptions.effort) {
2008
2154
  betas.add("effort-2025-11-24");
2009
2155
  }
2156
+ if (anthropicOptions == null ? void 0 : anthropicOptions.speed) {
2157
+ betas.add("fast-mode-2026-02-01");
2158
+ }
2010
2159
  if (useStructuredOutput) {
2011
2160
  betas.add("structured-outputs-2025-11-13");
2012
2161
  }
2162
+ const contextManagement = anthropicOptions == null ? void 0 : anthropicOptions.contextManagement;
2163
+ if (contextManagement) {
2164
+ betas.add("context-management-2025-06-27");
2165
+ if (contextManagement.edits.some((e) => e.type === "compact_20260112")) {
2166
+ betas.add("compact-2026-01-12");
2167
+ }
2168
+ }
2013
2169
  const {
2014
2170
  tools: anthropicTools2,
2015
2171
  toolChoice: anthropicToolChoice,
@@ -2160,6 +2316,18 @@ var AnthropicMessagesLanguageModel = class {
2160
2316
  });
2161
2317
  break;
2162
2318
  }
2319
+ case "compaction": {
2320
+ content.push({
2321
+ type: "text",
2322
+ text: part.content,
2323
+ providerMetadata: {
2324
+ anthropic: {
2325
+ type: "compaction"
2326
+ }
2327
+ }
2328
+ });
2329
+ break;
2330
+ }
2163
2331
  case "tool_use": {
2164
2332
  content.push(
2165
2333
  // when a json response tool is used, the tool call becomes the text:
@@ -2324,6 +2492,22 @@ var AnthropicMessagesLanguageModel = class {
2324
2492
  }
2325
2493
  }
2326
2494
  }
2495
+ let inputTokens;
2496
+ let outputTokens;
2497
+ if (response.usage.iterations && response.usage.iterations.length > 0) {
2498
+ const totals = response.usage.iterations.reduce(
2499
+ (acc, iter) => ({
2500
+ input: acc.input + iter.input_tokens,
2501
+ output: acc.output + iter.output_tokens
2502
+ }),
2503
+ { input: 0, output: 0 }
2504
+ );
2505
+ inputTokens = totals.input;
2506
+ outputTokens = totals.output;
2507
+ } else {
2508
+ inputTokens = response.usage.input_tokens;
2509
+ outputTokens = response.usage.output_tokens;
2510
+ }
2327
2511
  return {
2328
2512
  content,
2329
2513
  finishReason: mapAnthropicStopReason({
@@ -2331,9 +2515,9 @@ var AnthropicMessagesLanguageModel = class {
2331
2515
  isJsonResponseFromTool: usesJsonResponseTool
2332
2516
  }),
2333
2517
  usage: {
2334
- inputTokens: response.usage.input_tokens,
2335
- outputTokens: response.usage.output_tokens,
2336
- totalTokens: response.usage.input_tokens + response.usage.output_tokens,
2518
+ inputTokens,
2519
+ outputTokens,
2520
+ totalTokens: inputTokens + outputTokens,
2337
2521
  cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
2338
2522
  },
2339
2523
  request: { body: args },
@@ -2349,6 +2533,11 @@ var AnthropicMessagesLanguageModel = class {
2349
2533
  usage: response.usage,
2350
2534
  cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null,
2351
2535
  stopSequence: (_f = response.stop_sequence) != null ? _f : null,
2536
+ iterations: response.usage.iterations ? response.usage.iterations.map((iter) => ({
2537
+ type: iter.type,
2538
+ inputTokens: iter.input_tokens,
2539
+ outputTokens: iter.output_tokens
2540
+ })) : null,
2352
2541
  container: response.container ? {
2353
2542
  expiresAt: response.container.expires_at,
2354
2543
  id: response.container.id,
@@ -2357,6 +2546,23 @@ var AnthropicMessagesLanguageModel = class {
2357
2546
  skillId: skill.skill_id,
2358
2547
  version: skill.version
2359
2548
  }))) != null ? _h : null
2549
+ } : null,
2550
+ contextManagement: response.context_management ? {
2551
+ appliedEdits: response.context_management.applied_edits.map(
2552
+ (edit) => {
2553
+ switch (edit.type) {
2554
+ case "clear_01":
2555
+ return {
2556
+ type: edit.type,
2557
+ clearedInputTokens: edit.cleared_input_tokens
2558
+ };
2559
+ case "compact_20260112":
2560
+ return {
2561
+ type: edit.type
2562
+ };
2563
+ }
2564
+ }
2565
+ )
2360
2566
  } : null
2361
2567
  }
2362
2568
  }
@@ -2393,6 +2599,8 @@ var AnthropicMessagesLanguageModel = class {
2393
2599
  let cacheCreationInputTokens = null;
2394
2600
  let stopSequence = null;
2395
2601
  let container = null;
2602
+ let iterations = null;
2603
+ let contextManagement = null;
2396
2604
  let blockType = void 0;
2397
2605
  const generateId3 = this.generateId;
2398
2606
  const transformedStream = response.pipeThrough(
@@ -2447,6 +2655,19 @@ var AnthropicMessagesLanguageModel = class {
2447
2655
  });
2448
2656
  return;
2449
2657
  }
2658
+ case "compaction": {
2659
+ contentBlocks[value.index] = { type: "text" };
2660
+ controller.enqueue({
2661
+ type: "text-start",
2662
+ id: String(value.index),
2663
+ providerMetadata: {
2664
+ anthropic: {
2665
+ type: "compaction"
2666
+ }
2667
+ }
2668
+ });
2669
+ return;
2670
+ }
2450
2671
  case "tool_use": {
2451
2672
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2452
2673
  type: "tool-call",
@@ -2709,6 +2930,14 @@ var AnthropicMessagesLanguageModel = class {
2709
2930
  }
2710
2931
  return;
2711
2932
  }
2933
+ case "compaction_delta": {
2934
+ controller.enqueue({
2935
+ type: "text-delta",
2936
+ id: String(value.index),
2937
+ delta: value.delta.content
2938
+ });
2939
+ return;
2940
+ }
2712
2941
  case "input_json_delta": {
2713
2942
  const contentBlock = contentBlocks[value.index];
2714
2943
  let delta = value.delta.partial_json;
@@ -2776,16 +3005,31 @@ var AnthropicMessagesLanguageModel = class {
2776
3005
  return;
2777
3006
  }
2778
3007
  case "message_delta": {
2779
- if (value.usage.input_tokens != null && usage.inputTokens !== value.usage.input_tokens) {
2780
- usage.inputTokens = value.usage.input_tokens;
2781
- }
2782
- usage.outputTokens = value.usage.output_tokens;
2783
3008
  if (value.usage.cache_read_input_tokens != null) {
2784
3009
  usage.cachedInputTokens = value.usage.cache_read_input_tokens;
2785
3010
  }
2786
3011
  if (value.usage.cache_creation_input_tokens != null) {
2787
3012
  cacheCreationInputTokens = value.usage.cache_creation_input_tokens;
2788
3013
  }
3014
+ if (value.usage.iterations != null) {
3015
+ iterations = value.usage.iterations;
3016
+ }
3017
+ if (value.usage.iterations && value.usage.iterations.length > 0) {
3018
+ const totals = value.usage.iterations.reduce(
3019
+ (acc, iter) => ({
3020
+ input: acc.input + iter.input_tokens,
3021
+ output: acc.output + iter.output_tokens
3022
+ }),
3023
+ { input: 0, output: 0 }
3024
+ );
3025
+ usage.inputTokens = totals.input;
3026
+ usage.outputTokens = totals.output;
3027
+ } else {
3028
+ if (value.usage.input_tokens != null && usage.inputTokens !== value.usage.input_tokens) {
3029
+ usage.inputTokens = value.usage.input_tokens;
3030
+ }
3031
+ usage.outputTokens = value.usage.output_tokens;
3032
+ }
2789
3033
  usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = usage.outputTokens) != null ? _g : 0);
2790
3034
  finishReason = mapAnthropicStopReason({
2791
3035
  finishReason: value.delta.stop_reason,
@@ -2801,6 +3045,25 @@ var AnthropicMessagesLanguageModel = class {
2801
3045
  version: skill.version
2802
3046
  }))) != null ? _j : null
2803
3047
  } : null;
3048
+ if (value.context_management != null) {
3049
+ contextManagement = {
3050
+ appliedEdits: value.context_management.applied_edits.map(
3051
+ (edit) => {
3052
+ switch (edit.type) {
3053
+ case "clear_01":
3054
+ return {
3055
+ type: edit.type,
3056
+ clearedInputTokens: edit.cleared_input_tokens
3057
+ };
3058
+ case "compact_20260112":
3059
+ return {
3060
+ type: edit.type
3061
+ };
3062
+ }
3063
+ }
3064
+ )
3065
+ };
3066
+ }
2804
3067
  rawUsage = {
2805
3068
  ...rawUsage,
2806
3069
  ...value.usage
@@ -2817,7 +3080,13 @@ var AnthropicMessagesLanguageModel = class {
2817
3080
  usage: rawUsage != null ? rawUsage : null,
2818
3081
  cacheCreationInputTokens,
2819
3082
  stopSequence,
2820
- container
3083
+ iterations: iterations ? iterations.map((iter) => ({
3084
+ type: iter.type,
3085
+ inputTokens: iter.input_tokens,
3086
+ outputTokens: iter.output_tokens
3087
+ })) : null,
3088
+ container,
3089
+ contextManagement
2821
3090
  }
2822
3091
  }
2823
3092
  });