@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.
@@ -87,6 +87,10 @@ var anthropicMessagesResponseSchema = lazySchema2(
87
87
  type: z2.literal("redacted_thinking"),
88
88
  data: z2.string()
89
89
  }),
90
+ z2.object({
91
+ type: z2.literal("compaction"),
92
+ content: z2.string()
93
+ }),
90
94
  z2.object({
91
95
  type: z2.literal("tool_use"),
92
96
  id: z2.string(),
@@ -285,7 +289,14 @@ var anthropicMessagesResponseSchema = lazySchema2(
285
289
  input_tokens: z2.number(),
286
290
  output_tokens: z2.number(),
287
291
  cache_creation_input_tokens: z2.number().nullish(),
288
- cache_read_input_tokens: z2.number().nullish()
292
+ cache_read_input_tokens: z2.number().nullish(),
293
+ iterations: z2.array(
294
+ z2.object({
295
+ type: z2.union([z2.literal("compaction"), z2.literal("message")]),
296
+ input_tokens: z2.number(),
297
+ output_tokens: z2.number()
298
+ })
299
+ ).nullish()
289
300
  }),
290
301
  container: z2.object({
291
302
  expires_at: z2.string(),
@@ -310,6 +321,9 @@ var anthropicMessagesResponseSchema = lazySchema2(
310
321
  type: z2.literal("clear_thinking_20251015"),
311
322
  cleared_thinking_turns: z2.number(),
312
323
  cleared_input_tokens: z2.number()
324
+ }),
325
+ z2.object({
326
+ type: z2.literal("compact_20260112")
313
327
  })
314
328
  ])
315
329
  )
@@ -391,6 +405,10 @@ var anthropicMessagesChunkSchema = lazySchema2(
391
405
  type: z2.literal("redacted_thinking"),
392
406
  data: z2.string()
393
407
  }),
408
+ z2.object({
409
+ type: z2.literal("compaction"),
410
+ content: z2.string().nullish()
411
+ }),
394
412
  z2.object({
395
413
  type: z2.literal("server_tool_use"),
396
414
  id: z2.string(),
@@ -587,6 +605,10 @@ var anthropicMessagesChunkSchema = lazySchema2(
587
605
  type: z2.literal("signature_delta"),
588
606
  signature: z2.string()
589
607
  }),
608
+ z2.object({
609
+ type: z2.literal("compaction_delta"),
610
+ content: z2.string()
611
+ }),
590
612
  z2.object({
591
613
  type: z2.literal("citations_delta"),
592
614
  citation: z2.discriminatedUnion("type", [
@@ -652,7 +674,14 @@ var anthropicMessagesChunkSchema = lazySchema2(
652
674
  input_tokens: z2.number().nullish(),
653
675
  output_tokens: z2.number(),
654
676
  cache_creation_input_tokens: z2.number().nullish(),
655
- cache_read_input_tokens: z2.number().nullish()
677
+ cache_read_input_tokens: z2.number().nullish(),
678
+ iterations: z2.array(
679
+ z2.object({
680
+ type: z2.union([z2.literal("compaction"), z2.literal("message")]),
681
+ input_tokens: z2.number(),
682
+ output_tokens: z2.number()
683
+ })
684
+ ).nullish()
656
685
  }),
657
686
  context_management: z2.object({
658
687
  applied_edits: z2.array(
@@ -666,6 +695,9 @@ var anthropicMessagesChunkSchema = lazySchema2(
666
695
  type: z2.literal("clear_thinking_20251015"),
667
696
  cleared_thinking_turns: z2.number(),
668
697
  cleared_input_tokens: z2.number()
698
+ }),
699
+ z2.object({
700
+ type: z2.literal("compact_20260112")
669
701
  })
670
702
  ])
671
703
  )
@@ -845,6 +877,15 @@ var anthropicProviderOptions = z3.object({
845
877
  value: z3.number()
846
878
  })
847
879
  ]).optional()
880
+ }),
881
+ z3.object({
882
+ type: z3.literal("compact_20260112"),
883
+ trigger: z3.object({
884
+ type: z3.literal("input_tokens"),
885
+ value: z3.number()
886
+ }).optional(),
887
+ pauseAfterCompaction: z3.boolean().optional(),
888
+ instructions: z3.string().optional()
848
889
  })
849
890
  ])
850
891
  )
@@ -1339,12 +1380,29 @@ async function prepareTools({
1339
1380
  }
1340
1381
 
1341
1382
  // src/convert-anthropic-messages-usage.ts
1342
- function convertAnthropicMessagesUsage(usage) {
1383
+ function convertAnthropicMessagesUsage({
1384
+ usage,
1385
+ rawUsage
1386
+ }) {
1343
1387
  var _a, _b;
1344
- const inputTokens = usage.input_tokens;
1345
- const outputTokens = usage.output_tokens;
1346
1388
  const cacheCreationTokens = (_a = usage.cache_creation_input_tokens) != null ? _a : 0;
1347
1389
  const cacheReadTokens = (_b = usage.cache_read_input_tokens) != null ? _b : 0;
1390
+ let inputTokens;
1391
+ let outputTokens;
1392
+ if (usage.iterations && usage.iterations.length > 0) {
1393
+ const totals = usage.iterations.reduce(
1394
+ (acc, iter) => ({
1395
+ input: acc.input + iter.input_tokens,
1396
+ output: acc.output + iter.output_tokens
1397
+ }),
1398
+ { input: 0, output: 0 }
1399
+ );
1400
+ inputTokens = totals.input;
1401
+ outputTokens = totals.output;
1402
+ } else {
1403
+ inputTokens = usage.input_tokens;
1404
+ outputTokens = usage.output_tokens;
1405
+ }
1348
1406
  return {
1349
1407
  inputTokens: {
1350
1408
  total: inputTokens + cacheCreationTokens + cacheReadTokens,
@@ -1357,7 +1415,7 @@ function convertAnthropicMessagesUsage(usage) {
1357
1415
  text: void 0,
1358
1416
  reasoning: void 0
1359
1417
  },
1360
- raw: usage
1418
+ raw: rawUsage != null ? rawUsage : usage
1361
1419
  };
1362
1420
  }
1363
1421
 
@@ -1605,7 +1663,7 @@ async function convertToAnthropicMessagesPrompt({
1605
1663
  cacheControlValidator,
1606
1664
  toolNameMapping
1607
1665
  }) {
1608
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1666
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
1609
1667
  const betas = /* @__PURE__ */ new Set();
1610
1668
  const blocks = groupIntoBlocks(prompt);
1611
1669
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -1898,16 +1956,25 @@ async function convertToAnthropicMessagesPrompt({
1898
1956
  }) : void 0;
1899
1957
  switch (part.type) {
1900
1958
  case "text": {
1901
- anthropicContent.push({
1902
- type: "text",
1903
- text: (
1904
- // trim the last text part if it's the last message in the block
1905
- // because Anthropic does not allow trailing whitespace
1906
- // in pre-filled assistant responses
1907
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1908
- ),
1909
- cache_control: cacheControl
1910
- });
1959
+ const textMetadata = (_g = part.providerOptions) == null ? void 0 : _g.anthropic;
1960
+ if ((textMetadata == null ? void 0 : textMetadata.type) === "compaction") {
1961
+ anthropicContent.push({
1962
+ type: "compaction",
1963
+ content: part.text,
1964
+ cache_control: cacheControl
1965
+ });
1966
+ } else {
1967
+ anthropicContent.push({
1968
+ type: "text",
1969
+ text: (
1970
+ // trim the last text part if it's the last message in the block
1971
+ // because Anthropic does not allow trailing whitespace
1972
+ // in pre-filled assistant responses
1973
+ isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1974
+ ),
1975
+ cache_control: cacheControl
1976
+ });
1977
+ }
1911
1978
  break;
1912
1979
  }
1913
1980
  case "reasoning": {
@@ -1962,10 +2029,10 @@ async function convertToAnthropicMessagesPrompt({
1962
2029
  const providerToolName = toolNameMapping.toProviderToolName(
1963
2030
  part.toolName
1964
2031
  );
1965
- const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
2032
+ const isMcpToolUse = ((_i = (_h = part.providerOptions) == null ? void 0 : _h.anthropic) == null ? void 0 : _i.type) === "mcp-tool-use";
1966
2033
  if (isMcpToolUse) {
1967
2034
  mcpToolUseIds.add(part.toolCallId);
1968
- const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
2035
+ const serverName = (_k = (_j = part.providerOptions) == null ? void 0 : _j.anthropic) == null ? void 0 : _k.serverName;
1969
2036
  if (serverName == null || typeof serverName !== "string") {
1970
2037
  warnings.push({
1971
2038
  type: "other",
@@ -2033,7 +2100,7 @@ async function convertToAnthropicMessagesPrompt({
2033
2100
  }
2034
2101
  break;
2035
2102
  }
2036
- const callerOptions = (_k = part.providerOptions) == null ? void 0 : _k.anthropic;
2103
+ const callerOptions = (_l = part.providerOptions) == null ? void 0 : _l.anthropic;
2037
2104
  const caller = (callerOptions == null ? void 0 : callerOptions.caller) ? callerOptions.caller.type === "code_execution_20250825" && callerOptions.caller.toolId ? {
2038
2105
  type: "code_execution_20250825",
2039
2106
  tool_id: callerOptions.caller.toolId
@@ -2086,7 +2153,7 @@ async function convertToAnthropicMessagesPrompt({
2086
2153
  tool_use_id: part.toolCallId,
2087
2154
  content: {
2088
2155
  type: "code_execution_tool_result_error",
2089
- error_code: (_l = errorInfo.errorCode) != null ? _l : "unknown"
2156
+ error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2090
2157
  },
2091
2158
  cache_control: cacheControl
2092
2159
  });
@@ -2097,7 +2164,7 @@ async function convertToAnthropicMessagesPrompt({
2097
2164
  cache_control: cacheControl,
2098
2165
  content: {
2099
2166
  type: "bash_code_execution_tool_result_error",
2100
- error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2167
+ error_code: (_n = errorInfo.errorCode) != null ? _n : "unknown"
2101
2168
  }
2102
2169
  });
2103
2170
  }
@@ -2130,7 +2197,7 @@ async function convertToAnthropicMessagesPrompt({
2130
2197
  stdout: codeExecutionOutput.stdout,
2131
2198
  stderr: codeExecutionOutput.stderr,
2132
2199
  return_code: codeExecutionOutput.return_code,
2133
- content: (_n = codeExecutionOutput.content) != null ? _n : []
2200
+ content: (_o = codeExecutionOutput.content) != null ? _o : []
2134
2201
  },
2135
2202
  cache_control: cacheControl
2136
2203
  });
@@ -2148,7 +2215,7 @@ async function convertToAnthropicMessagesPrompt({
2148
2215
  stdout: codeExecutionOutput.stdout,
2149
2216
  stderr: codeExecutionOutput.stderr,
2150
2217
  return_code: codeExecutionOutput.return_code,
2151
- content: (_o = codeExecutionOutput.content) != null ? _o : []
2218
+ content: (_p = codeExecutionOutput.content) != null ? _p : []
2152
2219
  },
2153
2220
  cache_control: cacheControl
2154
2221
  });
@@ -2181,7 +2248,7 @@ async function convertToAnthropicMessagesPrompt({
2181
2248
  errorValue = output.value;
2182
2249
  }
2183
2250
  } catch (e) {
2184
- const extractedErrorCode = (_p = output.value) == null ? void 0 : _p.errorCode;
2251
+ const extractedErrorCode = (_q = output.value) == null ? void 0 : _q.errorCode;
2185
2252
  errorValue = {
2186
2253
  errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unknown"
2187
2254
  };
@@ -2191,7 +2258,7 @@ async function convertToAnthropicMessagesPrompt({
2191
2258
  tool_use_id: part.toolCallId,
2192
2259
  content: {
2193
2260
  type: "web_fetch_tool_result_error",
2194
- error_code: (_q = errorValue.errorCode) != null ? _q : "unknown"
2261
+ error_code: (_r = errorValue.errorCode) != null ? _r : "unknown"
2195
2262
  },
2196
2263
  cache_control: cacheControl
2197
2264
  });
@@ -2372,6 +2439,8 @@ function mapAnthropicStopReason({
2372
2439
  case "max_tokens":
2373
2440
  case "model_context_window_exceeded":
2374
2441
  return "length";
2442
+ case "compaction":
2443
+ return "other";
2375
2444
  default:
2376
2445
  return "other";
2377
2446
  }
@@ -2654,6 +2723,19 @@ var AnthropicMessagesLanguageModel = class {
2654
2723
  type: edit.type,
2655
2724
  ...edit.keep !== void 0 && { keep: edit.keep }
2656
2725
  };
2726
+ case "compact_20260112":
2727
+ return {
2728
+ type: edit.type,
2729
+ ...edit.trigger !== void 0 && {
2730
+ trigger: edit.trigger
2731
+ },
2732
+ ...edit.pauseAfterCompaction !== void 0 && {
2733
+ pause_after_compaction: edit.pauseAfterCompaction
2734
+ },
2735
+ ...edit.instructions !== void 0 && {
2736
+ instructions: edit.instructions
2737
+ }
2738
+ };
2657
2739
  default:
2658
2740
  warnings.push({
2659
2741
  type: "other",
@@ -2728,6 +2810,9 @@ var AnthropicMessagesLanguageModel = class {
2728
2810
  }
2729
2811
  if (contextManagement) {
2730
2812
  betas.add("context-management-2025-06-27");
2813
+ if (contextManagement.edits.some((e) => e.type === "compact_20260112")) {
2814
+ betas.add("compact-2026-01-12");
2815
+ }
2731
2816
  }
2732
2817
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2733
2818
  betas.add("code-execution-2025-08-25");
@@ -2926,6 +3011,18 @@ var AnthropicMessagesLanguageModel = class {
2926
3011
  });
2927
3012
  break;
2928
3013
  }
3014
+ case "compaction": {
3015
+ content.push({
3016
+ type: "text",
3017
+ text: part.content,
3018
+ providerMetadata: {
3019
+ anthropic: {
3020
+ type: "compaction"
3021
+ }
3022
+ }
3023
+ });
3024
+ break;
3025
+ }
2929
3026
  case "tool_use": {
2930
3027
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2931
3028
  if (isJsonResponseTool) {
@@ -3194,7 +3291,7 @@ var AnthropicMessagesLanguageModel = class {
3194
3291
  }),
3195
3292
  raw: (_d = response.stop_reason) != null ? _d : void 0
3196
3293
  },
3197
- usage: convertAnthropicMessagesUsage(response.usage),
3294
+ usage: convertAnthropicMessagesUsage({ usage: response.usage }),
3198
3295
  request: { body: args },
3199
3296
  response: {
3200
3297
  id: (_e = response.id) != null ? _e : void 0,
@@ -3209,6 +3306,11 @@ var AnthropicMessagesLanguageModel = class {
3209
3306
  usage: response.usage,
3210
3307
  cacheCreationInputTokens: (_a2 = response.usage.cache_creation_input_tokens) != null ? _a2 : null,
3211
3308
  stopSequence: (_b2 = response.stop_sequence) != null ? _b2 : null,
3309
+ iterations: response.usage.iterations ? response.usage.iterations.map((iter) => ({
3310
+ type: iter.type,
3311
+ inputTokens: iter.input_tokens,
3312
+ outputTokens: iter.output_tokens
3313
+ })) : null,
3212
3314
  container: response.container ? {
3213
3315
  expiresAt: response.container.expires_at,
3214
3316
  id: response.container.id,
@@ -3270,7 +3372,8 @@ var AnthropicMessagesLanguageModel = class {
3270
3372
  input_tokens: 0,
3271
3373
  output_tokens: 0,
3272
3374
  cache_creation_input_tokens: 0,
3273
- cache_read_input_tokens: 0
3375
+ cache_read_input_tokens: 0,
3376
+ iterations: null
3274
3377
  };
3275
3378
  const contentBlocks = {};
3276
3379
  const mcpToolCalls = {};
@@ -3339,6 +3442,19 @@ var AnthropicMessagesLanguageModel = class {
3339
3442
  });
3340
3443
  return;
3341
3444
  }
3445
+ case "compaction": {
3446
+ contentBlocks[value.index] = { type: "text" };
3447
+ controller.enqueue({
3448
+ type: "text-start",
3449
+ id: String(value.index),
3450
+ providerMetadata: {
3451
+ anthropic: {
3452
+ type: "compaction"
3453
+ }
3454
+ }
3455
+ });
3456
+ return;
3457
+ }
3342
3458
  case "tool_use": {
3343
3459
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
3344
3460
  if (isJsonResponseTool) {
@@ -3725,6 +3841,14 @@ var AnthropicMessagesLanguageModel = class {
3725
3841
  }
3726
3842
  return;
3727
3843
  }
3844
+ case "compaction_delta": {
3845
+ controller.enqueue({
3846
+ type: "text-delta",
3847
+ id: String(value.index),
3848
+ delta: value.delta.content
3849
+ });
3850
+ return;
3851
+ }
3728
3852
  case "input_json_delta": {
3729
3853
  const contentBlock = contentBlocks[value.index];
3730
3854
  let delta = value.delta.partial_json;
@@ -3860,6 +3984,9 @@ var AnthropicMessagesLanguageModel = class {
3860
3984
  usage.cache_creation_input_tokens = value.usage.cache_creation_input_tokens;
3861
3985
  cacheCreationInputTokens = value.usage.cache_creation_input_tokens;
3862
3986
  }
3987
+ if (value.usage.iterations != null) {
3988
+ usage.iterations = value.usage.iterations;
3989
+ }
3863
3990
  finishReason = {
3864
3991
  unified: mapAnthropicStopReason({
3865
3992
  finishReason: value.delta.stop_reason,
@@ -3893,6 +4020,11 @@ var AnthropicMessagesLanguageModel = class {
3893
4020
  usage: rawUsage != null ? rawUsage : null,
3894
4021
  cacheCreationInputTokens,
3895
4022
  stopSequence,
4023
+ iterations: usage.iterations ? usage.iterations.map((iter) => ({
4024
+ type: iter.type,
4025
+ inputTokens: iter.input_tokens,
4026
+ outputTokens: iter.output_tokens
4027
+ })) : null,
3896
4028
  container,
3897
4029
  contextManagement
3898
4030
  };
@@ -3905,7 +4037,7 @@ var AnthropicMessagesLanguageModel = class {
3905
4037
  controller.enqueue({
3906
4038
  type: "finish",
3907
4039
  finishReason,
3908
- usage: convertAnthropicMessagesUsage(usage),
4040
+ usage: convertAnthropicMessagesUsage({ usage, rawUsage }),
3909
4041
  providerMetadata
3910
4042
  });
3911
4043
  return;
@@ -4022,6 +4154,10 @@ function mapAnthropicResponseContextManagement(contextManagement) {
4022
4154
  clearedThinkingTurns: edit.cleared_thinking_turns,
4023
4155
  clearedInputTokens: edit.cleared_input_tokens
4024
4156
  };
4157
+ case "compact_20260112":
4158
+ return {
4159
+ type: edit.type
4160
+ };
4025
4161
  }
4026
4162
  }).filter((edit) => edit !== void 0)
4027
4163
  } : null;