@ai-sdk/anthropic 3.0.39 → 3.0.41

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.
@@ -100,6 +100,10 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
100
100
  type: import_v42.z.literal("redacted_thinking"),
101
101
  data: import_v42.z.string()
102
102
  }),
103
+ import_v42.z.object({
104
+ type: import_v42.z.literal("compaction"),
105
+ content: import_v42.z.string()
106
+ }),
103
107
  import_v42.z.object({
104
108
  type: import_v42.z.literal("tool_use"),
105
109
  id: import_v42.z.string(),
@@ -298,7 +302,14 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
298
302
  input_tokens: import_v42.z.number(),
299
303
  output_tokens: import_v42.z.number(),
300
304
  cache_creation_input_tokens: import_v42.z.number().nullish(),
301
- cache_read_input_tokens: import_v42.z.number().nullish()
305
+ cache_read_input_tokens: import_v42.z.number().nullish(),
306
+ iterations: import_v42.z.array(
307
+ import_v42.z.object({
308
+ type: import_v42.z.union([import_v42.z.literal("compaction"), import_v42.z.literal("message")]),
309
+ input_tokens: import_v42.z.number(),
310
+ output_tokens: import_v42.z.number()
311
+ })
312
+ ).nullish()
302
313
  }),
303
314
  container: import_v42.z.object({
304
315
  expires_at: import_v42.z.string(),
@@ -323,6 +334,9 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
323
334
  type: import_v42.z.literal("clear_thinking_20251015"),
324
335
  cleared_thinking_turns: import_v42.z.number(),
325
336
  cleared_input_tokens: import_v42.z.number()
337
+ }),
338
+ import_v42.z.object({
339
+ type: import_v42.z.literal("compact_20260112")
326
340
  })
327
341
  ])
328
342
  )
@@ -404,6 +418,10 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
404
418
  type: import_v42.z.literal("redacted_thinking"),
405
419
  data: import_v42.z.string()
406
420
  }),
421
+ import_v42.z.object({
422
+ type: import_v42.z.literal("compaction"),
423
+ content: import_v42.z.string().nullish()
424
+ }),
407
425
  import_v42.z.object({
408
426
  type: import_v42.z.literal("server_tool_use"),
409
427
  id: import_v42.z.string(),
@@ -600,6 +618,10 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
600
618
  type: import_v42.z.literal("signature_delta"),
601
619
  signature: import_v42.z.string()
602
620
  }),
621
+ import_v42.z.object({
622
+ type: import_v42.z.literal("compaction_delta"),
623
+ content: import_v42.z.string()
624
+ }),
603
625
  import_v42.z.object({
604
626
  type: import_v42.z.literal("citations_delta"),
605
627
  citation: import_v42.z.discriminatedUnion("type", [
@@ -665,7 +687,14 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
665
687
  input_tokens: import_v42.z.number().nullish(),
666
688
  output_tokens: import_v42.z.number(),
667
689
  cache_creation_input_tokens: import_v42.z.number().nullish(),
668
- cache_read_input_tokens: import_v42.z.number().nullish()
690
+ cache_read_input_tokens: import_v42.z.number().nullish(),
691
+ iterations: import_v42.z.array(
692
+ import_v42.z.object({
693
+ type: import_v42.z.union([import_v42.z.literal("compaction"), import_v42.z.literal("message")]),
694
+ input_tokens: import_v42.z.number(),
695
+ output_tokens: import_v42.z.number()
696
+ })
697
+ ).nullish()
669
698
  }),
670
699
  context_management: import_v42.z.object({
671
700
  applied_edits: import_v42.z.array(
@@ -679,6 +708,9 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
679
708
  type: import_v42.z.literal("clear_thinking_20251015"),
680
709
  cleared_thinking_turns: import_v42.z.number(),
681
710
  cleared_input_tokens: import_v42.z.number()
711
+ }),
712
+ import_v42.z.object({
713
+ type: import_v42.z.literal("compact_20260112")
682
714
  })
683
715
  ])
684
716
  )
@@ -858,6 +890,15 @@ var anthropicProviderOptions = import_v43.z.object({
858
890
  value: import_v43.z.number()
859
891
  })
860
892
  ]).optional()
893
+ }),
894
+ import_v43.z.object({
895
+ type: import_v43.z.literal("compact_20260112"),
896
+ trigger: import_v43.z.object({
897
+ type: import_v43.z.literal("input_tokens"),
898
+ value: import_v43.z.number()
899
+ }).optional(),
900
+ pauseAfterCompaction: import_v43.z.boolean().optional(),
901
+ instructions: import_v43.z.string().optional()
861
902
  })
862
903
  ])
863
904
  )
@@ -1342,12 +1383,29 @@ async function prepareTools({
1342
1383
  }
1343
1384
 
1344
1385
  // src/convert-anthropic-messages-usage.ts
1345
- function convertAnthropicMessagesUsage(usage) {
1386
+ function convertAnthropicMessagesUsage({
1387
+ usage,
1388
+ rawUsage
1389
+ }) {
1346
1390
  var _a, _b;
1347
- const inputTokens = usage.input_tokens;
1348
- const outputTokens = usage.output_tokens;
1349
1391
  const cacheCreationTokens = (_a = usage.cache_creation_input_tokens) != null ? _a : 0;
1350
1392
  const cacheReadTokens = (_b = usage.cache_read_input_tokens) != null ? _b : 0;
1393
+ let inputTokens;
1394
+ let outputTokens;
1395
+ if (usage.iterations && usage.iterations.length > 0) {
1396
+ const totals = usage.iterations.reduce(
1397
+ (acc, iter) => ({
1398
+ input: acc.input + iter.input_tokens,
1399
+ output: acc.output + iter.output_tokens
1400
+ }),
1401
+ { input: 0, output: 0 }
1402
+ );
1403
+ inputTokens = totals.input;
1404
+ outputTokens = totals.output;
1405
+ } else {
1406
+ inputTokens = usage.input_tokens;
1407
+ outputTokens = usage.output_tokens;
1408
+ }
1351
1409
  return {
1352
1410
  inputTokens: {
1353
1411
  total: inputTokens + cacheCreationTokens + cacheReadTokens,
@@ -1360,7 +1418,7 @@ function convertAnthropicMessagesUsage(usage) {
1360
1418
  text: void 0,
1361
1419
  reasoning: void 0
1362
1420
  },
1363
- raw: usage
1421
+ raw: rawUsage != null ? rawUsage : usage
1364
1422
  };
1365
1423
  }
1366
1424
 
@@ -1589,7 +1647,7 @@ async function convertToAnthropicMessagesPrompt({
1589
1647
  cacheControlValidator,
1590
1648
  toolNameMapping
1591
1649
  }) {
1592
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1650
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
1593
1651
  const betas = /* @__PURE__ */ new Set();
1594
1652
  const blocks = groupIntoBlocks(prompt);
1595
1653
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -1882,16 +1940,25 @@ async function convertToAnthropicMessagesPrompt({
1882
1940
  }) : void 0;
1883
1941
  switch (part.type) {
1884
1942
  case "text": {
1885
- anthropicContent.push({
1886
- type: "text",
1887
- text: (
1888
- // trim the last text part if it's the last message in the block
1889
- // because Anthropic does not allow trailing whitespace
1890
- // in pre-filled assistant responses
1891
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1892
- ),
1893
- cache_control: cacheControl
1894
- });
1943
+ const textMetadata = (_g = part.providerOptions) == null ? void 0 : _g.anthropic;
1944
+ if ((textMetadata == null ? void 0 : textMetadata.type) === "compaction") {
1945
+ anthropicContent.push({
1946
+ type: "compaction",
1947
+ content: part.text,
1948
+ cache_control: cacheControl
1949
+ });
1950
+ } else {
1951
+ anthropicContent.push({
1952
+ type: "text",
1953
+ text: (
1954
+ // trim the last text part if it's the last message in the block
1955
+ // because Anthropic does not allow trailing whitespace
1956
+ // in pre-filled assistant responses
1957
+ isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1958
+ ),
1959
+ cache_control: cacheControl
1960
+ });
1961
+ }
1895
1962
  break;
1896
1963
  }
1897
1964
  case "reasoning": {
@@ -1946,10 +2013,10 @@ async function convertToAnthropicMessagesPrompt({
1946
2013
  const providerToolName = toolNameMapping.toProviderToolName(
1947
2014
  part.toolName
1948
2015
  );
1949
- const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
2016
+ const isMcpToolUse = ((_i = (_h = part.providerOptions) == null ? void 0 : _h.anthropic) == null ? void 0 : _i.type) === "mcp-tool-use";
1950
2017
  if (isMcpToolUse) {
1951
2018
  mcpToolUseIds.add(part.toolCallId);
1952
- const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
2019
+ const serverName = (_k = (_j = part.providerOptions) == null ? void 0 : _j.anthropic) == null ? void 0 : _k.serverName;
1953
2020
  if (serverName == null || typeof serverName !== "string") {
1954
2021
  warnings.push({
1955
2022
  type: "other",
@@ -2017,7 +2084,7 @@ async function convertToAnthropicMessagesPrompt({
2017
2084
  }
2018
2085
  break;
2019
2086
  }
2020
- const callerOptions = (_k = part.providerOptions) == null ? void 0 : _k.anthropic;
2087
+ const callerOptions = (_l = part.providerOptions) == null ? void 0 : _l.anthropic;
2021
2088
  const caller = (callerOptions == null ? void 0 : callerOptions.caller) ? callerOptions.caller.type === "code_execution_20250825" && callerOptions.caller.toolId ? {
2022
2089
  type: "code_execution_20250825",
2023
2090
  tool_id: callerOptions.caller.toolId
@@ -2070,7 +2137,7 @@ async function convertToAnthropicMessagesPrompt({
2070
2137
  tool_use_id: part.toolCallId,
2071
2138
  content: {
2072
2139
  type: "code_execution_tool_result_error",
2073
- error_code: (_l = errorInfo.errorCode) != null ? _l : "unknown"
2140
+ error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2074
2141
  },
2075
2142
  cache_control: cacheControl
2076
2143
  });
@@ -2081,7 +2148,7 @@ async function convertToAnthropicMessagesPrompt({
2081
2148
  cache_control: cacheControl,
2082
2149
  content: {
2083
2150
  type: "bash_code_execution_tool_result_error",
2084
- error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2151
+ error_code: (_n = errorInfo.errorCode) != null ? _n : "unknown"
2085
2152
  }
2086
2153
  });
2087
2154
  }
@@ -2114,7 +2181,7 @@ async function convertToAnthropicMessagesPrompt({
2114
2181
  stdout: codeExecutionOutput.stdout,
2115
2182
  stderr: codeExecutionOutput.stderr,
2116
2183
  return_code: codeExecutionOutput.return_code,
2117
- content: (_n = codeExecutionOutput.content) != null ? _n : []
2184
+ content: (_o = codeExecutionOutput.content) != null ? _o : []
2118
2185
  },
2119
2186
  cache_control: cacheControl
2120
2187
  });
@@ -2132,7 +2199,7 @@ async function convertToAnthropicMessagesPrompt({
2132
2199
  stdout: codeExecutionOutput.stdout,
2133
2200
  stderr: codeExecutionOutput.stderr,
2134
2201
  return_code: codeExecutionOutput.return_code,
2135
- content: (_o = codeExecutionOutput.content) != null ? _o : []
2202
+ content: (_p = codeExecutionOutput.content) != null ? _p : []
2136
2203
  },
2137
2204
  cache_control: cacheControl
2138
2205
  });
@@ -2165,7 +2232,7 @@ async function convertToAnthropicMessagesPrompt({
2165
2232
  errorValue = output.value;
2166
2233
  }
2167
2234
  } catch (e) {
2168
- const extractedErrorCode = (_p = output.value) == null ? void 0 : _p.errorCode;
2235
+ const extractedErrorCode = (_q = output.value) == null ? void 0 : _q.errorCode;
2169
2236
  errorValue = {
2170
2237
  errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unknown"
2171
2238
  };
@@ -2175,7 +2242,7 @@ async function convertToAnthropicMessagesPrompt({
2175
2242
  tool_use_id: part.toolCallId,
2176
2243
  content: {
2177
2244
  type: "web_fetch_tool_result_error",
2178
- error_code: (_q = errorValue.errorCode) != null ? _q : "unknown"
2245
+ error_code: (_r = errorValue.errorCode) != null ? _r : "unknown"
2179
2246
  },
2180
2247
  cache_control: cacheControl
2181
2248
  });
@@ -2356,6 +2423,8 @@ function mapAnthropicStopReason({
2356
2423
  case "max_tokens":
2357
2424
  case "model_context_window_exceeded":
2358
2425
  return "length";
2426
+ case "compaction":
2427
+ return "other";
2359
2428
  default:
2360
2429
  return "other";
2361
2430
  }
@@ -2638,6 +2707,19 @@ var AnthropicMessagesLanguageModel = class {
2638
2707
  type: edit.type,
2639
2708
  ...edit.keep !== void 0 && { keep: edit.keep }
2640
2709
  };
2710
+ case "compact_20260112":
2711
+ return {
2712
+ type: edit.type,
2713
+ ...edit.trigger !== void 0 && {
2714
+ trigger: edit.trigger
2715
+ },
2716
+ ...edit.pauseAfterCompaction !== void 0 && {
2717
+ pause_after_compaction: edit.pauseAfterCompaction
2718
+ },
2719
+ ...edit.instructions !== void 0 && {
2720
+ instructions: edit.instructions
2721
+ }
2722
+ };
2641
2723
  default:
2642
2724
  warnings.push({
2643
2725
  type: "other",
@@ -2712,6 +2794,9 @@ var AnthropicMessagesLanguageModel = class {
2712
2794
  }
2713
2795
  if (contextManagement) {
2714
2796
  betas.add("context-management-2025-06-27");
2797
+ if (contextManagement.edits.some((e) => e.type === "compact_20260112")) {
2798
+ betas.add("compact-2026-01-12");
2799
+ }
2715
2800
  }
2716
2801
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2717
2802
  betas.add("code-execution-2025-08-25");
@@ -2910,6 +2995,18 @@ var AnthropicMessagesLanguageModel = class {
2910
2995
  });
2911
2996
  break;
2912
2997
  }
2998
+ case "compaction": {
2999
+ content.push({
3000
+ type: "text",
3001
+ text: part.content,
3002
+ providerMetadata: {
3003
+ anthropic: {
3004
+ type: "compaction"
3005
+ }
3006
+ }
3007
+ });
3008
+ break;
3009
+ }
2913
3010
  case "tool_use": {
2914
3011
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2915
3012
  if (isJsonResponseTool) {
@@ -3178,7 +3275,7 @@ var AnthropicMessagesLanguageModel = class {
3178
3275
  }),
3179
3276
  raw: (_d = response.stop_reason) != null ? _d : void 0
3180
3277
  },
3181
- usage: convertAnthropicMessagesUsage(response.usage),
3278
+ usage: convertAnthropicMessagesUsage({ usage: response.usage }),
3182
3279
  request: { body: args },
3183
3280
  response: {
3184
3281
  id: (_e = response.id) != null ? _e : void 0,
@@ -3193,6 +3290,11 @@ var AnthropicMessagesLanguageModel = class {
3193
3290
  usage: response.usage,
3194
3291
  cacheCreationInputTokens: (_a2 = response.usage.cache_creation_input_tokens) != null ? _a2 : null,
3195
3292
  stopSequence: (_b2 = response.stop_sequence) != null ? _b2 : null,
3293
+ iterations: response.usage.iterations ? response.usage.iterations.map((iter) => ({
3294
+ type: iter.type,
3295
+ inputTokens: iter.input_tokens,
3296
+ outputTokens: iter.output_tokens
3297
+ })) : null,
3196
3298
  container: response.container ? {
3197
3299
  expiresAt: response.container.expires_at,
3198
3300
  id: response.container.id,
@@ -3254,7 +3356,8 @@ var AnthropicMessagesLanguageModel = class {
3254
3356
  input_tokens: 0,
3255
3357
  output_tokens: 0,
3256
3358
  cache_creation_input_tokens: 0,
3257
- cache_read_input_tokens: 0
3359
+ cache_read_input_tokens: 0,
3360
+ iterations: null
3258
3361
  };
3259
3362
  const contentBlocks = {};
3260
3363
  const mcpToolCalls = {};
@@ -3323,6 +3426,19 @@ var AnthropicMessagesLanguageModel = class {
3323
3426
  });
3324
3427
  return;
3325
3428
  }
3429
+ case "compaction": {
3430
+ contentBlocks[value.index] = { type: "text" };
3431
+ controller.enqueue({
3432
+ type: "text-start",
3433
+ id: String(value.index),
3434
+ providerMetadata: {
3435
+ anthropic: {
3436
+ type: "compaction"
3437
+ }
3438
+ }
3439
+ });
3440
+ return;
3441
+ }
3326
3442
  case "tool_use": {
3327
3443
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
3328
3444
  if (isJsonResponseTool) {
@@ -3709,6 +3825,14 @@ var AnthropicMessagesLanguageModel = class {
3709
3825
  }
3710
3826
  return;
3711
3827
  }
3828
+ case "compaction_delta": {
3829
+ controller.enqueue({
3830
+ type: "text-delta",
3831
+ id: String(value.index),
3832
+ delta: value.delta.content
3833
+ });
3834
+ return;
3835
+ }
3712
3836
  case "input_json_delta": {
3713
3837
  const contentBlock = contentBlocks[value.index];
3714
3838
  let delta = value.delta.partial_json;
@@ -3844,6 +3968,9 @@ var AnthropicMessagesLanguageModel = class {
3844
3968
  usage.cache_creation_input_tokens = value.usage.cache_creation_input_tokens;
3845
3969
  cacheCreationInputTokens = value.usage.cache_creation_input_tokens;
3846
3970
  }
3971
+ if (value.usage.iterations != null) {
3972
+ usage.iterations = value.usage.iterations;
3973
+ }
3847
3974
  finishReason = {
3848
3975
  unified: mapAnthropicStopReason({
3849
3976
  finishReason: value.delta.stop_reason,
@@ -3877,6 +4004,11 @@ var AnthropicMessagesLanguageModel = class {
3877
4004
  usage: rawUsage != null ? rawUsage : null,
3878
4005
  cacheCreationInputTokens,
3879
4006
  stopSequence,
4007
+ iterations: usage.iterations ? usage.iterations.map((iter) => ({
4008
+ type: iter.type,
4009
+ inputTokens: iter.input_tokens,
4010
+ outputTokens: iter.output_tokens
4011
+ })) : null,
3880
4012
  container,
3881
4013
  contextManagement
3882
4014
  };
@@ -3889,7 +4021,7 @@ var AnthropicMessagesLanguageModel = class {
3889
4021
  controller.enqueue({
3890
4022
  type: "finish",
3891
4023
  finishReason,
3892
- usage: convertAnthropicMessagesUsage(usage),
4024
+ usage: convertAnthropicMessagesUsage({ usage, rawUsage }),
3893
4025
  providerMetadata
3894
4026
  });
3895
4027
  return;
@@ -4006,6 +4138,10 @@ function mapAnthropicResponseContextManagement(contextManagement) {
4006
4138
  clearedThinkingTurns: edit.cleared_thinking_turns,
4007
4139
  clearedInputTokens: edit.cleared_input_tokens
4008
4140
  };
4141
+ case "compact_20260112":
4142
+ return {
4143
+ type: edit.type
4144
+ };
4009
4145
  }
4010
4146
  }).filter((edit) => edit !== void 0)
4011
4147
  } : null;