@ai-sdk/anthropic 3.0.40 → 3.0.42

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
@@ -12,7 +12,7 @@ import {
12
12
  } from "@ai-sdk/provider-utils";
13
13
 
14
14
  // src/version.ts
15
- var VERSION = true ? "3.0.40" : "0.0.0-test";
15
+ var VERSION = true ? "3.0.42" : "0.0.0-test";
16
16
 
17
17
  // src/anthropic-messages-language-model.ts
18
18
  import {
@@ -103,6 +103,10 @@ var anthropicMessagesResponseSchema = lazySchema2(
103
103
  type: z2.literal("redacted_thinking"),
104
104
  data: z2.string()
105
105
  }),
106
+ z2.object({
107
+ type: z2.literal("compaction"),
108
+ content: z2.string()
109
+ }),
106
110
  z2.object({
107
111
  type: z2.literal("tool_use"),
108
112
  id: z2.string(),
@@ -301,7 +305,14 @@ var anthropicMessagesResponseSchema = lazySchema2(
301
305
  input_tokens: z2.number(),
302
306
  output_tokens: z2.number(),
303
307
  cache_creation_input_tokens: z2.number().nullish(),
304
- cache_read_input_tokens: z2.number().nullish()
308
+ cache_read_input_tokens: z2.number().nullish(),
309
+ iterations: z2.array(
310
+ z2.object({
311
+ type: z2.union([z2.literal("compaction"), z2.literal("message")]),
312
+ input_tokens: z2.number(),
313
+ output_tokens: z2.number()
314
+ })
315
+ ).nullish()
305
316
  }),
306
317
  container: z2.object({
307
318
  expires_at: z2.string(),
@@ -326,6 +337,9 @@ var anthropicMessagesResponseSchema = lazySchema2(
326
337
  type: z2.literal("clear_thinking_20251015"),
327
338
  cleared_thinking_turns: z2.number(),
328
339
  cleared_input_tokens: z2.number()
340
+ }),
341
+ z2.object({
342
+ type: z2.literal("compact_20260112")
329
343
  })
330
344
  ])
331
345
  )
@@ -407,6 +421,10 @@ var anthropicMessagesChunkSchema = lazySchema2(
407
421
  type: z2.literal("redacted_thinking"),
408
422
  data: z2.string()
409
423
  }),
424
+ z2.object({
425
+ type: z2.literal("compaction"),
426
+ content: z2.string().nullish()
427
+ }),
410
428
  z2.object({
411
429
  type: z2.literal("server_tool_use"),
412
430
  id: z2.string(),
@@ -603,6 +621,10 @@ var anthropicMessagesChunkSchema = lazySchema2(
603
621
  type: z2.literal("signature_delta"),
604
622
  signature: z2.string()
605
623
  }),
624
+ z2.object({
625
+ type: z2.literal("compaction_delta"),
626
+ content: z2.string()
627
+ }),
606
628
  z2.object({
607
629
  type: z2.literal("citations_delta"),
608
630
  citation: z2.discriminatedUnion("type", [
@@ -668,7 +690,14 @@ var anthropicMessagesChunkSchema = lazySchema2(
668
690
  input_tokens: z2.number().nullish(),
669
691
  output_tokens: z2.number(),
670
692
  cache_creation_input_tokens: z2.number().nullish(),
671
- cache_read_input_tokens: z2.number().nullish()
693
+ cache_read_input_tokens: z2.number().nullish(),
694
+ iterations: z2.array(
695
+ z2.object({
696
+ type: z2.union([z2.literal("compaction"), z2.literal("message")]),
697
+ input_tokens: z2.number(),
698
+ output_tokens: z2.number()
699
+ })
700
+ ).nullish()
672
701
  }),
673
702
  context_management: z2.object({
674
703
  applied_edits: z2.array(
@@ -682,6 +711,9 @@ var anthropicMessagesChunkSchema = lazySchema2(
682
711
  type: z2.literal("clear_thinking_20251015"),
683
712
  cleared_thinking_turns: z2.number(),
684
713
  cleared_input_tokens: z2.number()
714
+ }),
715
+ z2.object({
716
+ type: z2.literal("compact_20260112")
685
717
  })
686
718
  ])
687
719
  )
@@ -730,7 +762,7 @@ var anthropicFilePartProviderOptions = z3.object({
730
762
  */
731
763
  context: z3.string().optional()
732
764
  });
733
- var anthropicProviderOptions = z3.object({
765
+ var anthropicLanguageModelOptions = z3.object({
734
766
  /**
735
767
  * Whether to send reasoning to the model.
736
768
  *
@@ -861,6 +893,15 @@ var anthropicProviderOptions = z3.object({
861
893
  value: z3.number()
862
894
  })
863
895
  ]).optional()
896
+ }),
897
+ z3.object({
898
+ type: z3.literal("compact_20260112"),
899
+ trigger: z3.object({
900
+ type: z3.literal("input_tokens"),
901
+ value: z3.number()
902
+ }).optional(),
903
+ pauseAfterCompaction: z3.boolean().optional(),
904
+ instructions: z3.string().optional()
864
905
  })
865
906
  ])
866
907
  )
@@ -1360,10 +1401,24 @@ function convertAnthropicMessagesUsage({
1360
1401
  rawUsage
1361
1402
  }) {
1362
1403
  var _a, _b;
1363
- const inputTokens = usage.input_tokens;
1364
- const outputTokens = usage.output_tokens;
1365
1404
  const cacheCreationTokens = (_a = usage.cache_creation_input_tokens) != null ? _a : 0;
1366
1405
  const cacheReadTokens = (_b = usage.cache_read_input_tokens) != null ? _b : 0;
1406
+ let inputTokens;
1407
+ let outputTokens;
1408
+ if (usage.iterations && usage.iterations.length > 0) {
1409
+ const totals = usage.iterations.reduce(
1410
+ (acc, iter) => ({
1411
+ input: acc.input + iter.input_tokens,
1412
+ output: acc.output + iter.output_tokens
1413
+ }),
1414
+ { input: 0, output: 0 }
1415
+ );
1416
+ inputTokens = totals.input;
1417
+ outputTokens = totals.output;
1418
+ } else {
1419
+ inputTokens = usage.input_tokens;
1420
+ outputTokens = usage.output_tokens;
1421
+ }
1367
1422
  return {
1368
1423
  inputTokens: {
1369
1424
  total: inputTokens + cacheCreationTokens + cacheReadTokens,
@@ -1624,7 +1679,7 @@ async function convertToAnthropicMessagesPrompt({
1624
1679
  cacheControlValidator,
1625
1680
  toolNameMapping
1626
1681
  }) {
1627
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1682
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
1628
1683
  const betas = /* @__PURE__ */ new Set();
1629
1684
  const blocks = groupIntoBlocks(prompt);
1630
1685
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -1917,16 +1972,25 @@ async function convertToAnthropicMessagesPrompt({
1917
1972
  }) : void 0;
1918
1973
  switch (part.type) {
1919
1974
  case "text": {
1920
- anthropicContent.push({
1921
- type: "text",
1922
- text: (
1923
- // trim the last text part if it's the last message in the block
1924
- // because Anthropic does not allow trailing whitespace
1925
- // in pre-filled assistant responses
1926
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1927
- ),
1928
- cache_control: cacheControl
1929
- });
1975
+ const textMetadata = (_g = part.providerOptions) == null ? void 0 : _g.anthropic;
1976
+ if ((textMetadata == null ? void 0 : textMetadata.type) === "compaction") {
1977
+ anthropicContent.push({
1978
+ type: "compaction",
1979
+ content: part.text,
1980
+ cache_control: cacheControl
1981
+ });
1982
+ } else {
1983
+ anthropicContent.push({
1984
+ type: "text",
1985
+ text: (
1986
+ // trim the last text part if it's the last message in the block
1987
+ // because Anthropic does not allow trailing whitespace
1988
+ // in pre-filled assistant responses
1989
+ isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1990
+ ),
1991
+ cache_control: cacheControl
1992
+ });
1993
+ }
1930
1994
  break;
1931
1995
  }
1932
1996
  case "reasoning": {
@@ -1981,10 +2045,10 @@ async function convertToAnthropicMessagesPrompt({
1981
2045
  const providerToolName = toolNameMapping.toProviderToolName(
1982
2046
  part.toolName
1983
2047
  );
1984
- const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
2048
+ const isMcpToolUse = ((_i = (_h = part.providerOptions) == null ? void 0 : _h.anthropic) == null ? void 0 : _i.type) === "mcp-tool-use";
1985
2049
  if (isMcpToolUse) {
1986
2050
  mcpToolUseIds.add(part.toolCallId);
1987
- const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
2051
+ const serverName = (_k = (_j = part.providerOptions) == null ? void 0 : _j.anthropic) == null ? void 0 : _k.serverName;
1988
2052
  if (serverName == null || typeof serverName !== "string") {
1989
2053
  warnings.push({
1990
2054
  type: "other",
@@ -2052,7 +2116,7 @@ async function convertToAnthropicMessagesPrompt({
2052
2116
  }
2053
2117
  break;
2054
2118
  }
2055
- const callerOptions = (_k = part.providerOptions) == null ? void 0 : _k.anthropic;
2119
+ const callerOptions = (_l = part.providerOptions) == null ? void 0 : _l.anthropic;
2056
2120
  const caller = (callerOptions == null ? void 0 : callerOptions.caller) ? callerOptions.caller.type === "code_execution_20250825" && callerOptions.caller.toolId ? {
2057
2121
  type: "code_execution_20250825",
2058
2122
  tool_id: callerOptions.caller.toolId
@@ -2105,7 +2169,7 @@ async function convertToAnthropicMessagesPrompt({
2105
2169
  tool_use_id: part.toolCallId,
2106
2170
  content: {
2107
2171
  type: "code_execution_tool_result_error",
2108
- error_code: (_l = errorInfo.errorCode) != null ? _l : "unknown"
2172
+ error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2109
2173
  },
2110
2174
  cache_control: cacheControl
2111
2175
  });
@@ -2116,7 +2180,7 @@ async function convertToAnthropicMessagesPrompt({
2116
2180
  cache_control: cacheControl,
2117
2181
  content: {
2118
2182
  type: "bash_code_execution_tool_result_error",
2119
- error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2183
+ error_code: (_n = errorInfo.errorCode) != null ? _n : "unknown"
2120
2184
  }
2121
2185
  });
2122
2186
  }
@@ -2149,7 +2213,7 @@ async function convertToAnthropicMessagesPrompt({
2149
2213
  stdout: codeExecutionOutput.stdout,
2150
2214
  stderr: codeExecutionOutput.stderr,
2151
2215
  return_code: codeExecutionOutput.return_code,
2152
- content: (_n = codeExecutionOutput.content) != null ? _n : []
2216
+ content: (_o = codeExecutionOutput.content) != null ? _o : []
2153
2217
  },
2154
2218
  cache_control: cacheControl
2155
2219
  });
@@ -2167,7 +2231,7 @@ async function convertToAnthropicMessagesPrompt({
2167
2231
  stdout: codeExecutionOutput.stdout,
2168
2232
  stderr: codeExecutionOutput.stderr,
2169
2233
  return_code: codeExecutionOutput.return_code,
2170
- content: (_o = codeExecutionOutput.content) != null ? _o : []
2234
+ content: (_p = codeExecutionOutput.content) != null ? _p : []
2171
2235
  },
2172
2236
  cache_control: cacheControl
2173
2237
  });
@@ -2200,7 +2264,7 @@ async function convertToAnthropicMessagesPrompt({
2200
2264
  errorValue = output.value;
2201
2265
  }
2202
2266
  } catch (e) {
2203
- const extractedErrorCode = (_p = output.value) == null ? void 0 : _p.errorCode;
2267
+ const extractedErrorCode = (_q = output.value) == null ? void 0 : _q.errorCode;
2204
2268
  errorValue = {
2205
2269
  errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unknown"
2206
2270
  };
@@ -2210,7 +2274,7 @@ async function convertToAnthropicMessagesPrompt({
2210
2274
  tool_use_id: part.toolCallId,
2211
2275
  content: {
2212
2276
  type: "web_fetch_tool_result_error",
2213
- error_code: (_q = errorValue.errorCode) != null ? _q : "unknown"
2277
+ error_code: (_r = errorValue.errorCode) != null ? _r : "unknown"
2214
2278
  },
2215
2279
  cache_control: cacheControl
2216
2280
  });
@@ -2391,6 +2455,8 @@ function mapAnthropicStopReason({
2391
2455
  case "max_tokens":
2392
2456
  case "model_context_window_exceeded":
2393
2457
  return "length";
2458
+ case "compaction":
2459
+ return "other";
2394
2460
  default:
2395
2461
  return "other";
2396
2462
  }
@@ -2524,12 +2590,12 @@ var AnthropicMessagesLanguageModel = class {
2524
2590
  const canonicalOptions = await parseProviderOptions2({
2525
2591
  provider: "anthropic",
2526
2592
  providerOptions,
2527
- schema: anthropicProviderOptions
2593
+ schema: anthropicLanguageModelOptions
2528
2594
  });
2529
2595
  const customProviderOptions = providerOptionsName !== "anthropic" ? await parseProviderOptions2({
2530
2596
  provider: providerOptionsName,
2531
2597
  providerOptions,
2532
- schema: anthropicProviderOptions
2598
+ schema: anthropicLanguageModelOptions
2533
2599
  }) : null;
2534
2600
  const usedCustomProviderKey = customProviderOptions != null;
2535
2601
  const anthropicOptions = Object.assign(
@@ -2673,6 +2739,19 @@ var AnthropicMessagesLanguageModel = class {
2673
2739
  type: edit.type,
2674
2740
  ...edit.keep !== void 0 && { keep: edit.keep }
2675
2741
  };
2742
+ case "compact_20260112":
2743
+ return {
2744
+ type: edit.type,
2745
+ ...edit.trigger !== void 0 && {
2746
+ trigger: edit.trigger
2747
+ },
2748
+ ...edit.pauseAfterCompaction !== void 0 && {
2749
+ pause_after_compaction: edit.pauseAfterCompaction
2750
+ },
2751
+ ...edit.instructions !== void 0 && {
2752
+ instructions: edit.instructions
2753
+ }
2754
+ };
2676
2755
  default:
2677
2756
  warnings.push({
2678
2757
  type: "other",
@@ -2747,6 +2826,9 @@ var AnthropicMessagesLanguageModel = class {
2747
2826
  }
2748
2827
  if (contextManagement) {
2749
2828
  betas.add("context-management-2025-06-27");
2829
+ if (contextManagement.edits.some((e) => e.type === "compact_20260112")) {
2830
+ betas.add("compact-2026-01-12");
2831
+ }
2750
2832
  }
2751
2833
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2752
2834
  betas.add("code-execution-2025-08-25");
@@ -2945,6 +3027,18 @@ var AnthropicMessagesLanguageModel = class {
2945
3027
  });
2946
3028
  break;
2947
3029
  }
3030
+ case "compaction": {
3031
+ content.push({
3032
+ type: "text",
3033
+ text: part.content,
3034
+ providerMetadata: {
3035
+ anthropic: {
3036
+ type: "compaction"
3037
+ }
3038
+ }
3039
+ });
3040
+ break;
3041
+ }
2948
3042
  case "tool_use": {
2949
3043
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2950
3044
  if (isJsonResponseTool) {
@@ -3228,6 +3322,11 @@ var AnthropicMessagesLanguageModel = class {
3228
3322
  usage: response.usage,
3229
3323
  cacheCreationInputTokens: (_a2 = response.usage.cache_creation_input_tokens) != null ? _a2 : null,
3230
3324
  stopSequence: (_b2 = response.stop_sequence) != null ? _b2 : null,
3325
+ iterations: response.usage.iterations ? response.usage.iterations.map((iter) => ({
3326
+ type: iter.type,
3327
+ inputTokens: iter.input_tokens,
3328
+ outputTokens: iter.output_tokens
3329
+ })) : null,
3231
3330
  container: response.container ? {
3232
3331
  expiresAt: response.container.expires_at,
3233
3332
  id: response.container.id,
@@ -3289,7 +3388,8 @@ var AnthropicMessagesLanguageModel = class {
3289
3388
  input_tokens: 0,
3290
3389
  output_tokens: 0,
3291
3390
  cache_creation_input_tokens: 0,
3292
- cache_read_input_tokens: 0
3391
+ cache_read_input_tokens: 0,
3392
+ iterations: null
3293
3393
  };
3294
3394
  const contentBlocks = {};
3295
3395
  const mcpToolCalls = {};
@@ -3358,6 +3458,19 @@ var AnthropicMessagesLanguageModel = class {
3358
3458
  });
3359
3459
  return;
3360
3460
  }
3461
+ case "compaction": {
3462
+ contentBlocks[value.index] = { type: "text" };
3463
+ controller.enqueue({
3464
+ type: "text-start",
3465
+ id: String(value.index),
3466
+ providerMetadata: {
3467
+ anthropic: {
3468
+ type: "compaction"
3469
+ }
3470
+ }
3471
+ });
3472
+ return;
3473
+ }
3361
3474
  case "tool_use": {
3362
3475
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
3363
3476
  if (isJsonResponseTool) {
@@ -3744,6 +3857,14 @@ var AnthropicMessagesLanguageModel = class {
3744
3857
  }
3745
3858
  return;
3746
3859
  }
3860
+ case "compaction_delta": {
3861
+ controller.enqueue({
3862
+ type: "text-delta",
3863
+ id: String(value.index),
3864
+ delta: value.delta.content
3865
+ });
3866
+ return;
3867
+ }
3747
3868
  case "input_json_delta": {
3748
3869
  const contentBlock = contentBlocks[value.index];
3749
3870
  let delta = value.delta.partial_json;
@@ -3879,6 +4000,9 @@ var AnthropicMessagesLanguageModel = class {
3879
4000
  usage.cache_creation_input_tokens = value.usage.cache_creation_input_tokens;
3880
4001
  cacheCreationInputTokens = value.usage.cache_creation_input_tokens;
3881
4002
  }
4003
+ if (value.usage.iterations != null) {
4004
+ usage.iterations = value.usage.iterations;
4005
+ }
3882
4006
  finishReason = {
3883
4007
  unified: mapAnthropicStopReason({
3884
4008
  finishReason: value.delta.stop_reason,
@@ -3912,6 +4036,11 @@ var AnthropicMessagesLanguageModel = class {
3912
4036
  usage: rawUsage != null ? rawUsage : null,
3913
4037
  cacheCreationInputTokens,
3914
4038
  stopSequence,
4039
+ iterations: usage.iterations ? usage.iterations.map((iter) => ({
4040
+ type: iter.type,
4041
+ inputTokens: iter.input_tokens,
4042
+ outputTokens: iter.output_tokens
4043
+ })) : null,
3915
4044
  container,
3916
4045
  contextManagement
3917
4046
  };
@@ -4041,6 +4170,10 @@ function mapAnthropicResponseContextManagement(contextManagement) {
4041
4170
  clearedThinkingTurns: edit.cleared_thinking_turns,
4042
4171
  clearedInputTokens: edit.cleared_input_tokens
4043
4172
  };
4173
+ case "compact_20260112":
4174
+ return {
4175
+ type: edit.type
4176
+ };
4044
4177
  }
4045
4178
  }).filter((edit) => edit !== void 0)
4046
4179
  } : null;