@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.
@@ -275,6 +275,22 @@ var anthropicMessagesResponseSchema = lazySchema2(
275
275
  version: z2.string()
276
276
  })
277
277
  ).nullish()
278
+ }).nullish(),
279
+ context_management: z2.object({
280
+ applied_edits: z2.array(
281
+ z2.union([
282
+ z2.object({
283
+ type: z2.literal("clear_tool_uses_20250919"),
284
+ cleared_tool_uses: z2.number(),
285
+ cleared_input_tokens: z2.number()
286
+ }),
287
+ z2.object({
288
+ type: z2.literal("clear_thinking_20251015"),
289
+ cleared_thinking_turns: z2.number(),
290
+ cleared_input_tokens: z2.number()
291
+ })
292
+ ])
293
+ )
278
294
  }).nullish()
279
295
  })
280
296
  )
@@ -557,6 +573,22 @@ var anthropicMessagesChunkSchema = lazySchema2(
557
573
  version: z2.string()
558
574
  })
559
575
  ).nullish()
576
+ }).nullish(),
577
+ context_management: z2.object({
578
+ applied_edits: z2.array(
579
+ z2.union([
580
+ z2.object({
581
+ type: z2.literal("clear_tool_uses_20250919"),
582
+ cleared_tool_uses: z2.number(),
583
+ cleared_input_tokens: z2.number()
584
+ }),
585
+ z2.object({
586
+ type: z2.literal("clear_thinking_20251015"),
587
+ cleared_thinking_turns: z2.number(),
588
+ cleared_input_tokens: z2.number()
589
+ })
590
+ ])
591
+ )
560
592
  }).nullish()
561
593
  }),
562
594
  usage: z2.looseObject({
@@ -687,7 +719,46 @@ var anthropicProviderOptions = z3.object({
687
719
  /**
688
720
  * @default 'high'
689
721
  */
690
- effort: z3.enum(["low", "medium", "high"]).optional()
722
+ effort: z3.enum(["low", "medium", "high"]).optional(),
723
+ contextManagement: z3.object({
724
+ edits: z3.array(
725
+ z3.discriminatedUnion("type", [
726
+ z3.object({
727
+ type: z3.literal("clear_tool_uses_20250919"),
728
+ trigger: z3.discriminatedUnion("type", [
729
+ z3.object({
730
+ type: z3.literal("input_tokens"),
731
+ value: z3.number()
732
+ }),
733
+ z3.object({
734
+ type: z3.literal("tool_uses"),
735
+ value: z3.number()
736
+ })
737
+ ]).optional(),
738
+ keep: z3.object({
739
+ type: z3.literal("tool_uses"),
740
+ value: z3.number()
741
+ }).optional(),
742
+ clearAtLeast: z3.object({
743
+ type: z3.literal("input_tokens"),
744
+ value: z3.number()
745
+ }).optional(),
746
+ clearToolInputs: z3.boolean().optional(),
747
+ excludeTools: z3.array(z3.string()).optional()
748
+ }),
749
+ z3.object({
750
+ type: z3.literal("clear_thinking_20251015"),
751
+ keep: z3.union([
752
+ z3.literal("all"),
753
+ z3.object({
754
+ type: z3.literal("thinking_turns"),
755
+ value: z3.number()
756
+ })
757
+ ]).optional()
758
+ })
759
+ ])
760
+ )
761
+ }).optional()
691
762
  });
692
763
 
693
764
  // src/anthropic-prepare-tools.ts
@@ -2018,6 +2089,8 @@ function mapAnthropicStopReason({
2018
2089
  return isJsonResponseFromTool ? "stop" : "tool-calls";
2019
2090
  case "max_tokens":
2020
2091
  return "length";
2092
+ case "model_context_window_exceeded":
2093
+ return "length";
2021
2094
  default:
2022
2095
  return "unknown";
2023
2096
  }
@@ -2141,6 +2214,7 @@ var AnthropicMessagesLanguageModel = class {
2141
2214
  description: "Respond with a JSON object.",
2142
2215
  inputSchema: responseFormat.schema
2143
2216
  } : void 0;
2217
+ const contextManagement = anthropicOptions == null ? void 0 : anthropicOptions.contextManagement;
2144
2218
  const cacheControlValidator = new CacheControlValidator();
2145
2219
  const toolNameMapping = createToolNameMapping({
2146
2220
  tools,
@@ -2221,7 +2295,44 @@ var AnthropicMessagesLanguageModel = class {
2221
2295
  },
2222
2296
  // prompt:
2223
2297
  system: messagesPrompt.system,
2224
- messages: messagesPrompt.messages
2298
+ messages: messagesPrompt.messages,
2299
+ ...contextManagement && {
2300
+ context_management: {
2301
+ edits: contextManagement.edits.map((edit) => {
2302
+ const strategy = edit.type;
2303
+ switch (strategy) {
2304
+ case "clear_tool_uses_20250919":
2305
+ return {
2306
+ type: edit.type,
2307
+ ...edit.trigger !== void 0 && {
2308
+ trigger: edit.trigger
2309
+ },
2310
+ ...edit.keep !== void 0 && { keep: edit.keep },
2311
+ ...edit.clearAtLeast !== void 0 && {
2312
+ clear_at_least: edit.clearAtLeast
2313
+ },
2314
+ ...edit.clearToolInputs !== void 0 && {
2315
+ clear_tool_inputs: edit.clearToolInputs
2316
+ },
2317
+ ...edit.excludeTools !== void 0 && {
2318
+ exclude_tools: edit.excludeTools
2319
+ }
2320
+ };
2321
+ case "clear_thinking_20251015":
2322
+ return {
2323
+ type: edit.type,
2324
+ ...edit.keep !== void 0 && { keep: edit.keep }
2325
+ };
2326
+ default:
2327
+ warnings.push({
2328
+ type: "other",
2329
+ message: `Unknown context management strategy: ${strategy}`
2330
+ });
2331
+ return void 0;
2332
+ }
2333
+ }).filter((edit) => edit !== void 0)
2334
+ }
2335
+ }
2225
2336
  };
2226
2337
  if (isThinking) {
2227
2338
  if (thinkingBudget == null) {
@@ -2268,6 +2379,9 @@ var AnthropicMessagesLanguageModel = class {
2268
2379
  if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
2269
2380
  betas.add("mcp-client-2025-04-04");
2270
2381
  }
2382
+ if (contextManagement) {
2383
+ betas.add("context-management-2025-06-27");
2384
+ }
2271
2385
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2272
2386
  betas.add("code-execution-2025-08-25");
2273
2387
  betas.add("skills-2025-10-02");
@@ -2379,7 +2493,7 @@ var AnthropicMessagesLanguageModel = class {
2379
2493
  });
2380
2494
  }
2381
2495
  async doGenerate(options) {
2382
- var _a, _b, _c, _d, _e, _f, _g, _h;
2496
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2383
2497
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2384
2498
  ...options,
2385
2499
  stream: false,
@@ -2705,7 +2819,10 @@ var AnthropicMessagesLanguageModel = class {
2705
2819
  skillId: skill.skill_id,
2706
2820
  version: skill.version
2707
2821
  }))) != null ? _h : null
2708
- } : null
2822
+ } : null,
2823
+ contextManagement: (_i = mapAnthropicResponseContextManagement(
2824
+ response.context_management
2825
+ )) != null ? _i : null
2709
2826
  }
2710
2827
  }
2711
2828
  };
@@ -2744,6 +2861,7 @@ var AnthropicMessagesLanguageModel = class {
2744
2861
  };
2745
2862
  const contentBlocks = {};
2746
2863
  const mcpToolCalls = {};
2864
+ let contextManagement = null;
2747
2865
  let rawUsage = void 0;
2748
2866
  let cacheCreationInputTokens = null;
2749
2867
  let stopSequence = null;
@@ -3226,6 +3344,11 @@ var AnthropicMessagesLanguageModel = class {
3226
3344
  version: skill.version
3227
3345
  }))) != null ? _j : null
3228
3346
  } : null;
3347
+ if (value.delta.context_management) {
3348
+ contextManagement = mapAnthropicResponseContextManagement(
3349
+ value.delta.context_management
3350
+ );
3351
+ }
3229
3352
  rawUsage = {
3230
3353
  ...rawUsage,
3231
3354
  ...value.usage
@@ -3242,7 +3365,8 @@ var AnthropicMessagesLanguageModel = class {
3242
3365
  usage: rawUsage != null ? rawUsage : null,
3243
3366
  cacheCreationInputTokens,
3244
3367
  stopSequence,
3245
- container
3368
+ container,
3369
+ contextManagement
3246
3370
  }
3247
3371
  }
3248
3372
  });
@@ -3337,6 +3461,27 @@ function getModelCapabilities(modelId) {
3337
3461
  };
3338
3462
  }
3339
3463
  }
3464
+ function mapAnthropicResponseContextManagement(contextManagement) {
3465
+ return contextManagement ? {
3466
+ appliedEdits: contextManagement.applied_edits.map((edit) => {
3467
+ const strategy = edit.type;
3468
+ switch (strategy) {
3469
+ case "clear_tool_uses_20250919":
3470
+ return {
3471
+ type: edit.type,
3472
+ clearedToolUses: edit.cleared_tool_uses,
3473
+ clearedInputTokens: edit.cleared_input_tokens
3474
+ };
3475
+ case "clear_thinking_20251015":
3476
+ return {
3477
+ type: edit.type,
3478
+ clearedThinkingTurns: edit.cleared_thinking_turns,
3479
+ clearedInputTokens: edit.cleared_input_tokens
3480
+ };
3481
+ }
3482
+ }).filter((edit) => edit !== void 0)
3483
+ } : null;
3484
+ }
3340
3485
 
3341
3486
  // src/tool/bash_20241022.ts
3342
3487
  import {