@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.
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.39" : "0.0.0-test";
15
+ var VERSION = true ? "3.0.41" : "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
  )
@@ -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
  )
@@ -1355,12 +1396,29 @@ async function prepareTools({
1355
1396
  }
1356
1397
 
1357
1398
  // src/convert-anthropic-messages-usage.ts
1358
- function convertAnthropicMessagesUsage(usage) {
1399
+ function convertAnthropicMessagesUsage({
1400
+ usage,
1401
+ rawUsage
1402
+ }) {
1359
1403
  var _a, _b;
1360
- const inputTokens = usage.input_tokens;
1361
- const outputTokens = usage.output_tokens;
1362
1404
  const cacheCreationTokens = (_a = usage.cache_creation_input_tokens) != null ? _a : 0;
1363
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
+ }
1364
1422
  return {
1365
1423
  inputTokens: {
1366
1424
  total: inputTokens + cacheCreationTokens + cacheReadTokens,
@@ -1373,7 +1431,7 @@ function convertAnthropicMessagesUsage(usage) {
1373
1431
  text: void 0,
1374
1432
  reasoning: void 0
1375
1433
  },
1376
- raw: usage
1434
+ raw: rawUsage != null ? rawUsage : usage
1377
1435
  };
1378
1436
  }
1379
1437
 
@@ -1621,7 +1679,7 @@ async function convertToAnthropicMessagesPrompt({
1621
1679
  cacheControlValidator,
1622
1680
  toolNameMapping
1623
1681
  }) {
1624
- 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;
1625
1683
  const betas = /* @__PURE__ */ new Set();
1626
1684
  const blocks = groupIntoBlocks(prompt);
1627
1685
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -1914,16 +1972,25 @@ async function convertToAnthropicMessagesPrompt({
1914
1972
  }) : void 0;
1915
1973
  switch (part.type) {
1916
1974
  case "text": {
1917
- anthropicContent.push({
1918
- type: "text",
1919
- text: (
1920
- // trim the last text part if it's the last message in the block
1921
- // because Anthropic does not allow trailing whitespace
1922
- // in pre-filled assistant responses
1923
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1924
- ),
1925
- cache_control: cacheControl
1926
- });
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
+ }
1927
1994
  break;
1928
1995
  }
1929
1996
  case "reasoning": {
@@ -1978,10 +2045,10 @@ async function convertToAnthropicMessagesPrompt({
1978
2045
  const providerToolName = toolNameMapping.toProviderToolName(
1979
2046
  part.toolName
1980
2047
  );
1981
- 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";
1982
2049
  if (isMcpToolUse) {
1983
2050
  mcpToolUseIds.add(part.toolCallId);
1984
- 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;
1985
2052
  if (serverName == null || typeof serverName !== "string") {
1986
2053
  warnings.push({
1987
2054
  type: "other",
@@ -2049,7 +2116,7 @@ async function convertToAnthropicMessagesPrompt({
2049
2116
  }
2050
2117
  break;
2051
2118
  }
2052
- const callerOptions = (_k = part.providerOptions) == null ? void 0 : _k.anthropic;
2119
+ const callerOptions = (_l = part.providerOptions) == null ? void 0 : _l.anthropic;
2053
2120
  const caller = (callerOptions == null ? void 0 : callerOptions.caller) ? callerOptions.caller.type === "code_execution_20250825" && callerOptions.caller.toolId ? {
2054
2121
  type: "code_execution_20250825",
2055
2122
  tool_id: callerOptions.caller.toolId
@@ -2102,7 +2169,7 @@ async function convertToAnthropicMessagesPrompt({
2102
2169
  tool_use_id: part.toolCallId,
2103
2170
  content: {
2104
2171
  type: "code_execution_tool_result_error",
2105
- error_code: (_l = errorInfo.errorCode) != null ? _l : "unknown"
2172
+ error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2106
2173
  },
2107
2174
  cache_control: cacheControl
2108
2175
  });
@@ -2113,7 +2180,7 @@ async function convertToAnthropicMessagesPrompt({
2113
2180
  cache_control: cacheControl,
2114
2181
  content: {
2115
2182
  type: "bash_code_execution_tool_result_error",
2116
- error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2183
+ error_code: (_n = errorInfo.errorCode) != null ? _n : "unknown"
2117
2184
  }
2118
2185
  });
2119
2186
  }
@@ -2146,7 +2213,7 @@ async function convertToAnthropicMessagesPrompt({
2146
2213
  stdout: codeExecutionOutput.stdout,
2147
2214
  stderr: codeExecutionOutput.stderr,
2148
2215
  return_code: codeExecutionOutput.return_code,
2149
- content: (_n = codeExecutionOutput.content) != null ? _n : []
2216
+ content: (_o = codeExecutionOutput.content) != null ? _o : []
2150
2217
  },
2151
2218
  cache_control: cacheControl
2152
2219
  });
@@ -2164,7 +2231,7 @@ async function convertToAnthropicMessagesPrompt({
2164
2231
  stdout: codeExecutionOutput.stdout,
2165
2232
  stderr: codeExecutionOutput.stderr,
2166
2233
  return_code: codeExecutionOutput.return_code,
2167
- content: (_o = codeExecutionOutput.content) != null ? _o : []
2234
+ content: (_p = codeExecutionOutput.content) != null ? _p : []
2168
2235
  },
2169
2236
  cache_control: cacheControl
2170
2237
  });
@@ -2197,7 +2264,7 @@ async function convertToAnthropicMessagesPrompt({
2197
2264
  errorValue = output.value;
2198
2265
  }
2199
2266
  } catch (e) {
2200
- const extractedErrorCode = (_p = output.value) == null ? void 0 : _p.errorCode;
2267
+ const extractedErrorCode = (_q = output.value) == null ? void 0 : _q.errorCode;
2201
2268
  errorValue = {
2202
2269
  errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unknown"
2203
2270
  };
@@ -2207,7 +2274,7 @@ async function convertToAnthropicMessagesPrompt({
2207
2274
  tool_use_id: part.toolCallId,
2208
2275
  content: {
2209
2276
  type: "web_fetch_tool_result_error",
2210
- error_code: (_q = errorValue.errorCode) != null ? _q : "unknown"
2277
+ error_code: (_r = errorValue.errorCode) != null ? _r : "unknown"
2211
2278
  },
2212
2279
  cache_control: cacheControl
2213
2280
  });
@@ -2388,6 +2455,8 @@ function mapAnthropicStopReason({
2388
2455
  case "max_tokens":
2389
2456
  case "model_context_window_exceeded":
2390
2457
  return "length";
2458
+ case "compaction":
2459
+ return "other";
2391
2460
  default:
2392
2461
  return "other";
2393
2462
  }
@@ -2670,6 +2739,19 @@ var AnthropicMessagesLanguageModel = class {
2670
2739
  type: edit.type,
2671
2740
  ...edit.keep !== void 0 && { keep: edit.keep }
2672
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
+ };
2673
2755
  default:
2674
2756
  warnings.push({
2675
2757
  type: "other",
@@ -2744,6 +2826,9 @@ var AnthropicMessagesLanguageModel = class {
2744
2826
  }
2745
2827
  if (contextManagement) {
2746
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
+ }
2747
2832
  }
2748
2833
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2749
2834
  betas.add("code-execution-2025-08-25");
@@ -2942,6 +3027,18 @@ var AnthropicMessagesLanguageModel = class {
2942
3027
  });
2943
3028
  break;
2944
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
+ }
2945
3042
  case "tool_use": {
2946
3043
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2947
3044
  if (isJsonResponseTool) {
@@ -3210,7 +3307,7 @@ var AnthropicMessagesLanguageModel = class {
3210
3307
  }),
3211
3308
  raw: (_d = response.stop_reason) != null ? _d : void 0
3212
3309
  },
3213
- usage: convertAnthropicMessagesUsage(response.usage),
3310
+ usage: convertAnthropicMessagesUsage({ usage: response.usage }),
3214
3311
  request: { body: args },
3215
3312
  response: {
3216
3313
  id: (_e = response.id) != null ? _e : void 0,
@@ -3225,6 +3322,11 @@ var AnthropicMessagesLanguageModel = class {
3225
3322
  usage: response.usage,
3226
3323
  cacheCreationInputTokens: (_a2 = response.usage.cache_creation_input_tokens) != null ? _a2 : null,
3227
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,
3228
3330
  container: response.container ? {
3229
3331
  expiresAt: response.container.expires_at,
3230
3332
  id: response.container.id,
@@ -3286,7 +3388,8 @@ var AnthropicMessagesLanguageModel = class {
3286
3388
  input_tokens: 0,
3287
3389
  output_tokens: 0,
3288
3390
  cache_creation_input_tokens: 0,
3289
- cache_read_input_tokens: 0
3391
+ cache_read_input_tokens: 0,
3392
+ iterations: null
3290
3393
  };
3291
3394
  const contentBlocks = {};
3292
3395
  const mcpToolCalls = {};
@@ -3355,6 +3458,19 @@ var AnthropicMessagesLanguageModel = class {
3355
3458
  });
3356
3459
  return;
3357
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
+ }
3358
3474
  case "tool_use": {
3359
3475
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
3360
3476
  if (isJsonResponseTool) {
@@ -3741,6 +3857,14 @@ var AnthropicMessagesLanguageModel = class {
3741
3857
  }
3742
3858
  return;
3743
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
+ }
3744
3868
  case "input_json_delta": {
3745
3869
  const contentBlock = contentBlocks[value.index];
3746
3870
  let delta = value.delta.partial_json;
@@ -3876,6 +4000,9 @@ var AnthropicMessagesLanguageModel = class {
3876
4000
  usage.cache_creation_input_tokens = value.usage.cache_creation_input_tokens;
3877
4001
  cacheCreationInputTokens = value.usage.cache_creation_input_tokens;
3878
4002
  }
4003
+ if (value.usage.iterations != null) {
4004
+ usage.iterations = value.usage.iterations;
4005
+ }
3879
4006
  finishReason = {
3880
4007
  unified: mapAnthropicStopReason({
3881
4008
  finishReason: value.delta.stop_reason,
@@ -3909,6 +4036,11 @@ var AnthropicMessagesLanguageModel = class {
3909
4036
  usage: rawUsage != null ? rawUsage : null,
3910
4037
  cacheCreationInputTokens,
3911
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,
3912
4044
  container,
3913
4045
  contextManagement
3914
4046
  };
@@ -3921,7 +4053,7 @@ var AnthropicMessagesLanguageModel = class {
3921
4053
  controller.enqueue({
3922
4054
  type: "finish",
3923
4055
  finishReason,
3924
- usage: convertAnthropicMessagesUsage(usage),
4056
+ usage: convertAnthropicMessagesUsage({ usage, rawUsage }),
3925
4057
  providerMetadata
3926
4058
  });
3927
4059
  return;
@@ -4038,6 +4170,10 @@ function mapAnthropicResponseContextManagement(contextManagement) {
4038
4170
  clearedThinkingTurns: edit.cleared_thinking_turns,
4039
4171
  clearedInputTokens: edit.cleared_input_tokens
4040
4172
  };
4173
+ case "compact_20260112":
4174
+ return {
4175
+ type: edit.type
4176
+ };
4041
4177
  }
4042
4178
  }).filter((edit) => edit !== void 0)
4043
4179
  } : null;