@ai-sdk/anthropic 3.0.0-beta.75 → 3.0.0-beta.77

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.
@@ -287,6 +287,22 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
287
287
  version: import_v42.z.string()
288
288
  })
289
289
  ).nullish()
290
+ }).nullish(),
291
+ context_management: import_v42.z.object({
292
+ applied_edits: import_v42.z.array(
293
+ import_v42.z.union([
294
+ import_v42.z.object({
295
+ type: import_v42.z.literal("clear_tool_uses_20250919"),
296
+ cleared_tool_uses: import_v42.z.number(),
297
+ cleared_input_tokens: import_v42.z.number()
298
+ }),
299
+ import_v42.z.object({
300
+ type: import_v42.z.literal("clear_thinking_20251015"),
301
+ cleared_thinking_turns: import_v42.z.number(),
302
+ cleared_input_tokens: import_v42.z.number()
303
+ })
304
+ ])
305
+ )
290
306
  }).nullish()
291
307
  })
292
308
  )
@@ -569,6 +585,22 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
569
585
  version: import_v42.z.string()
570
586
  })
571
587
  ).nullish()
588
+ }).nullish(),
589
+ context_management: import_v42.z.object({
590
+ applied_edits: import_v42.z.array(
591
+ import_v42.z.union([
592
+ import_v42.z.object({
593
+ type: import_v42.z.literal("clear_tool_uses_20250919"),
594
+ cleared_tool_uses: import_v42.z.number(),
595
+ cleared_input_tokens: import_v42.z.number()
596
+ }),
597
+ import_v42.z.object({
598
+ type: import_v42.z.literal("clear_thinking_20251015"),
599
+ cleared_thinking_turns: import_v42.z.number(),
600
+ cleared_input_tokens: import_v42.z.number()
601
+ })
602
+ ])
603
+ )
572
604
  }).nullish()
573
605
  }),
574
606
  usage: import_v42.z.looseObject({
@@ -699,7 +731,46 @@ var anthropicProviderOptions = import_v43.z.object({
699
731
  /**
700
732
  * @default 'high'
701
733
  */
702
- effort: import_v43.z.enum(["low", "medium", "high"]).optional()
734
+ effort: import_v43.z.enum(["low", "medium", "high"]).optional(),
735
+ contextManagement: import_v43.z.object({
736
+ edits: import_v43.z.array(
737
+ import_v43.z.discriminatedUnion("type", [
738
+ import_v43.z.object({
739
+ type: import_v43.z.literal("clear_tool_uses_20250919"),
740
+ trigger: import_v43.z.discriminatedUnion("type", [
741
+ import_v43.z.object({
742
+ type: import_v43.z.literal("input_tokens"),
743
+ value: import_v43.z.number()
744
+ }),
745
+ import_v43.z.object({
746
+ type: import_v43.z.literal("tool_uses"),
747
+ value: import_v43.z.number()
748
+ })
749
+ ]).optional(),
750
+ keep: import_v43.z.object({
751
+ type: import_v43.z.literal("tool_uses"),
752
+ value: import_v43.z.number()
753
+ }).optional(),
754
+ clearAtLeast: import_v43.z.object({
755
+ type: import_v43.z.literal("input_tokens"),
756
+ value: import_v43.z.number()
757
+ }).optional(),
758
+ clearToolInputs: import_v43.z.boolean().optional(),
759
+ excludeTools: import_v43.z.array(import_v43.z.string()).optional()
760
+ }),
761
+ import_v43.z.object({
762
+ type: import_v43.z.literal("clear_thinking_20251015"),
763
+ keep: import_v43.z.union([
764
+ import_v43.z.literal("all"),
765
+ import_v43.z.object({
766
+ type: import_v43.z.literal("thinking_turns"),
767
+ value: import_v43.z.number()
768
+ })
769
+ ]).optional()
770
+ })
771
+ ])
772
+ )
773
+ }).optional()
703
774
  });
704
775
 
705
776
  // src/anthropic-prepare-tools.ts
@@ -2001,6 +2072,8 @@ function mapAnthropicStopReason({
2001
2072
  return isJsonResponseFromTool ? "stop" : "tool-calls";
2002
2073
  case "max_tokens":
2003
2074
  return "length";
2075
+ case "model_context_window_exceeded":
2076
+ return "length";
2004
2077
  default:
2005
2078
  return "unknown";
2006
2079
  }
@@ -2124,6 +2197,7 @@ var AnthropicMessagesLanguageModel = class {
2124
2197
  description: "Respond with a JSON object.",
2125
2198
  inputSchema: responseFormat.schema
2126
2199
  } : void 0;
2200
+ const contextManagement = anthropicOptions == null ? void 0 : anthropicOptions.contextManagement;
2127
2201
  const cacheControlValidator = new CacheControlValidator();
2128
2202
  const toolNameMapping = (0, import_provider_utils12.createToolNameMapping)({
2129
2203
  tools,
@@ -2204,7 +2278,44 @@ var AnthropicMessagesLanguageModel = class {
2204
2278
  },
2205
2279
  // prompt:
2206
2280
  system: messagesPrompt.system,
2207
- messages: messagesPrompt.messages
2281
+ messages: messagesPrompt.messages,
2282
+ ...contextManagement && {
2283
+ context_management: {
2284
+ edits: contextManagement.edits.map((edit) => {
2285
+ const strategy = edit.type;
2286
+ switch (strategy) {
2287
+ case "clear_tool_uses_20250919":
2288
+ return {
2289
+ type: edit.type,
2290
+ ...edit.trigger !== void 0 && {
2291
+ trigger: edit.trigger
2292
+ },
2293
+ ...edit.keep !== void 0 && { keep: edit.keep },
2294
+ ...edit.clearAtLeast !== void 0 && {
2295
+ clear_at_least: edit.clearAtLeast
2296
+ },
2297
+ ...edit.clearToolInputs !== void 0 && {
2298
+ clear_tool_inputs: edit.clearToolInputs
2299
+ },
2300
+ ...edit.excludeTools !== void 0 && {
2301
+ exclude_tools: edit.excludeTools
2302
+ }
2303
+ };
2304
+ case "clear_thinking_20251015":
2305
+ return {
2306
+ type: edit.type,
2307
+ ...edit.keep !== void 0 && { keep: edit.keep }
2308
+ };
2309
+ default:
2310
+ warnings.push({
2311
+ type: "other",
2312
+ message: `Unknown context management strategy: ${strategy}`
2313
+ });
2314
+ return void 0;
2315
+ }
2316
+ }).filter((edit) => edit !== void 0)
2317
+ }
2318
+ }
2208
2319
  };
2209
2320
  if (isThinking) {
2210
2321
  if (thinkingBudget == null) {
@@ -2251,6 +2362,9 @@ var AnthropicMessagesLanguageModel = class {
2251
2362
  if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
2252
2363
  betas.add("mcp-client-2025-04-04");
2253
2364
  }
2365
+ if (contextManagement) {
2366
+ betas.add("context-management-2025-06-27");
2367
+ }
2254
2368
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2255
2369
  betas.add("code-execution-2025-08-25");
2256
2370
  betas.add("skills-2025-10-02");
@@ -2362,7 +2476,7 @@ var AnthropicMessagesLanguageModel = class {
2362
2476
  });
2363
2477
  }
2364
2478
  async doGenerate(options) {
2365
- var _a, _b, _c, _d, _e, _f, _g, _h;
2479
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2366
2480
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2367
2481
  ...options,
2368
2482
  stream: false,
@@ -2688,7 +2802,10 @@ var AnthropicMessagesLanguageModel = class {
2688
2802
  skillId: skill.skill_id,
2689
2803
  version: skill.version
2690
2804
  }))) != null ? _h : null
2691
- } : null
2805
+ } : null,
2806
+ contextManagement: (_i = mapAnthropicResponseContextManagement(
2807
+ response.context_management
2808
+ )) != null ? _i : null
2692
2809
  }
2693
2810
  }
2694
2811
  };
@@ -2727,6 +2844,7 @@ var AnthropicMessagesLanguageModel = class {
2727
2844
  };
2728
2845
  const contentBlocks = {};
2729
2846
  const mcpToolCalls = {};
2847
+ let contextManagement = null;
2730
2848
  let rawUsage = void 0;
2731
2849
  let cacheCreationInputTokens = null;
2732
2850
  let stopSequence = null;
@@ -3209,6 +3327,11 @@ var AnthropicMessagesLanguageModel = class {
3209
3327
  version: skill.version
3210
3328
  }))) != null ? _j : null
3211
3329
  } : null;
3330
+ if (value.delta.context_management) {
3331
+ contextManagement = mapAnthropicResponseContextManagement(
3332
+ value.delta.context_management
3333
+ );
3334
+ }
3212
3335
  rawUsage = {
3213
3336
  ...rawUsage,
3214
3337
  ...value.usage
@@ -3225,7 +3348,8 @@ var AnthropicMessagesLanguageModel = class {
3225
3348
  usage: rawUsage != null ? rawUsage : null,
3226
3349
  cacheCreationInputTokens,
3227
3350
  stopSequence,
3228
- container
3351
+ container,
3352
+ contextManagement
3229
3353
  }
3230
3354
  }
3231
3355
  });
@@ -3320,6 +3444,27 @@ function getModelCapabilities(modelId) {
3320
3444
  };
3321
3445
  }
3322
3446
  }
3447
+ function mapAnthropicResponseContextManagement(contextManagement) {
3448
+ return contextManagement ? {
3449
+ appliedEdits: contextManagement.applied_edits.map((edit) => {
3450
+ const strategy = edit.type;
3451
+ switch (strategy) {
3452
+ case "clear_tool_uses_20250919":
3453
+ return {
3454
+ type: edit.type,
3455
+ clearedToolUses: edit.cleared_tool_uses,
3456
+ clearedInputTokens: edit.cleared_input_tokens
3457
+ };
3458
+ case "clear_thinking_20251015":
3459
+ return {
3460
+ type: edit.type,
3461
+ clearedThinkingTurns: edit.cleared_thinking_turns,
3462
+ clearedInputTokens: edit.cleared_input_tokens
3463
+ };
3464
+ }
3465
+ }).filter((edit) => edit !== void 0)
3466
+ } : null;
3467
+ }
3323
3468
 
3324
3469
  // src/tool/bash_20241022.ts
3325
3470
  var import_provider_utils13 = require("@ai-sdk/provider-utils");