@ai-sdk/anthropic 3.0.0-beta.76 → 3.0.0-beta.78

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,
@@ -2153,7 +2227,7 @@ var AnthropicMessagesLanguageModel = class {
2153
2227
  toolNameMapping
2154
2228
  });
2155
2229
  const isThinking = ((_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.type) === "enabled";
2156
- const thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2230
+ let thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2157
2231
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
2158
2232
  const baseArgs = {
2159
2233
  // model id:
@@ -2204,13 +2278,57 @@ 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) {
2211
- throw new import_provider3.UnsupportedFunctionalityError({
2212
- functionality: "thinking requires a budget"
2322
+ warnings.push({
2323
+ type: "compatibility",
2324
+ feature: "extended thinking",
2325
+ details: "thinking budget is required when thinking is enabled. using default budget of 1024 tokens."
2213
2326
  });
2327
+ baseArgs.thinking = {
2328
+ type: "enabled",
2329
+ budget_tokens: 1024
2330
+ };
2331
+ thinkingBudget = 1024;
2214
2332
  }
2215
2333
  if (baseArgs.temperature != null) {
2216
2334
  baseArgs.temperature = void 0;
@@ -2236,7 +2354,7 @@ var AnthropicMessagesLanguageModel = class {
2236
2354
  details: "topP is not supported when thinking is enabled"
2237
2355
  });
2238
2356
  }
2239
- baseArgs.max_tokens = maxTokens + thinkingBudget;
2357
+ baseArgs.max_tokens = maxTokens + (thinkingBudget != null ? thinkingBudget : 0);
2240
2358
  }
2241
2359
  if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2242
2360
  if (maxOutputTokens != null) {
@@ -2251,6 +2369,9 @@ var AnthropicMessagesLanguageModel = class {
2251
2369
  if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
2252
2370
  betas.add("mcp-client-2025-04-04");
2253
2371
  }
2372
+ if (contextManagement) {
2373
+ betas.add("context-management-2025-06-27");
2374
+ }
2254
2375
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2255
2376
  betas.add("code-execution-2025-08-25");
2256
2377
  betas.add("skills-2025-10-02");
@@ -2362,7 +2483,7 @@ var AnthropicMessagesLanguageModel = class {
2362
2483
  });
2363
2484
  }
2364
2485
  async doGenerate(options) {
2365
- var _a, _b, _c, _d, _e, _f, _g, _h;
2486
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2366
2487
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2367
2488
  ...options,
2368
2489
  stream: false,
@@ -2688,7 +2809,10 @@ var AnthropicMessagesLanguageModel = class {
2688
2809
  skillId: skill.skill_id,
2689
2810
  version: skill.version
2690
2811
  }))) != null ? _h : null
2691
- } : null
2812
+ } : null,
2813
+ contextManagement: (_i = mapAnthropicResponseContextManagement(
2814
+ response.context_management
2815
+ )) != null ? _i : null
2692
2816
  }
2693
2817
  }
2694
2818
  };
@@ -2727,6 +2851,7 @@ var AnthropicMessagesLanguageModel = class {
2727
2851
  };
2728
2852
  const contentBlocks = {};
2729
2853
  const mcpToolCalls = {};
2854
+ let contextManagement = null;
2730
2855
  let rawUsage = void 0;
2731
2856
  let cacheCreationInputTokens = null;
2732
2857
  let stopSequence = null;
@@ -3209,6 +3334,11 @@ var AnthropicMessagesLanguageModel = class {
3209
3334
  version: skill.version
3210
3335
  }))) != null ? _j : null
3211
3336
  } : null;
3337
+ if (value.delta.context_management) {
3338
+ contextManagement = mapAnthropicResponseContextManagement(
3339
+ value.delta.context_management
3340
+ );
3341
+ }
3212
3342
  rawUsage = {
3213
3343
  ...rawUsage,
3214
3344
  ...value.usage
@@ -3225,7 +3355,8 @@ var AnthropicMessagesLanguageModel = class {
3225
3355
  usage: rawUsage != null ? rawUsage : null,
3226
3356
  cacheCreationInputTokens,
3227
3357
  stopSequence,
3228
- container
3358
+ container,
3359
+ contextManagement
3229
3360
  }
3230
3361
  }
3231
3362
  });
@@ -3320,6 +3451,27 @@ function getModelCapabilities(modelId) {
3320
3451
  };
3321
3452
  }
3322
3453
  }
3454
+ function mapAnthropicResponseContextManagement(contextManagement) {
3455
+ return contextManagement ? {
3456
+ appliedEdits: contextManagement.applied_edits.map((edit) => {
3457
+ const strategy = edit.type;
3458
+ switch (strategy) {
3459
+ case "clear_tool_uses_20250919":
3460
+ return {
3461
+ type: edit.type,
3462
+ clearedToolUses: edit.cleared_tool_uses,
3463
+ clearedInputTokens: edit.cleared_input_tokens
3464
+ };
3465
+ case "clear_thinking_20251015":
3466
+ return {
3467
+ type: edit.type,
3468
+ clearedThinkingTurns: edit.cleared_thinking_turns,
3469
+ clearedInputTokens: edit.cleared_input_tokens
3470
+ };
3471
+ }
3472
+ }).filter((edit) => edit !== void 0)
3473
+ } : null;
3474
+ }
3323
3475
 
3324
3476
  // src/tool/bash_20241022.ts
3325
3477
  var import_provider_utils13 = require("@ai-sdk/provider-utils");