@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.
@@ -1,7 +1,6 @@
1
1
  // src/anthropic-messages-language-model.ts
2
2
  import {
3
- APICallError,
4
- UnsupportedFunctionalityError as UnsupportedFunctionalityError3
3
+ APICallError
5
4
  } from "@ai-sdk/provider";
6
5
  import {
7
6
  combineHeaders,
@@ -275,6 +274,22 @@ var anthropicMessagesResponseSchema = lazySchema2(
275
274
  version: z2.string()
276
275
  })
277
276
  ).nullish()
277
+ }).nullish(),
278
+ context_management: z2.object({
279
+ applied_edits: z2.array(
280
+ z2.union([
281
+ z2.object({
282
+ type: z2.literal("clear_tool_uses_20250919"),
283
+ cleared_tool_uses: z2.number(),
284
+ cleared_input_tokens: z2.number()
285
+ }),
286
+ z2.object({
287
+ type: z2.literal("clear_thinking_20251015"),
288
+ cleared_thinking_turns: z2.number(),
289
+ cleared_input_tokens: z2.number()
290
+ })
291
+ ])
292
+ )
278
293
  }).nullish()
279
294
  })
280
295
  )
@@ -557,6 +572,22 @@ var anthropicMessagesChunkSchema = lazySchema2(
557
572
  version: z2.string()
558
573
  })
559
574
  ).nullish()
575
+ }).nullish(),
576
+ context_management: z2.object({
577
+ applied_edits: z2.array(
578
+ z2.union([
579
+ z2.object({
580
+ type: z2.literal("clear_tool_uses_20250919"),
581
+ cleared_tool_uses: z2.number(),
582
+ cleared_input_tokens: z2.number()
583
+ }),
584
+ z2.object({
585
+ type: z2.literal("clear_thinking_20251015"),
586
+ cleared_thinking_turns: z2.number(),
587
+ cleared_input_tokens: z2.number()
588
+ })
589
+ ])
590
+ )
560
591
  }).nullish()
561
592
  }),
562
593
  usage: z2.looseObject({
@@ -687,7 +718,46 @@ var anthropicProviderOptions = z3.object({
687
718
  /**
688
719
  * @default 'high'
689
720
  */
690
- effort: z3.enum(["low", "medium", "high"]).optional()
721
+ effort: z3.enum(["low", "medium", "high"]).optional(),
722
+ contextManagement: z3.object({
723
+ edits: z3.array(
724
+ z3.discriminatedUnion("type", [
725
+ z3.object({
726
+ type: z3.literal("clear_tool_uses_20250919"),
727
+ trigger: z3.discriminatedUnion("type", [
728
+ z3.object({
729
+ type: z3.literal("input_tokens"),
730
+ value: z3.number()
731
+ }),
732
+ z3.object({
733
+ type: z3.literal("tool_uses"),
734
+ value: z3.number()
735
+ })
736
+ ]).optional(),
737
+ keep: z3.object({
738
+ type: z3.literal("tool_uses"),
739
+ value: z3.number()
740
+ }).optional(),
741
+ clearAtLeast: z3.object({
742
+ type: z3.literal("input_tokens"),
743
+ value: z3.number()
744
+ }).optional(),
745
+ clearToolInputs: z3.boolean().optional(),
746
+ excludeTools: z3.array(z3.string()).optional()
747
+ }),
748
+ z3.object({
749
+ type: z3.literal("clear_thinking_20251015"),
750
+ keep: z3.union([
751
+ z3.literal("all"),
752
+ z3.object({
753
+ type: z3.literal("thinking_turns"),
754
+ value: z3.number()
755
+ })
756
+ ]).optional()
757
+ })
758
+ ])
759
+ )
760
+ }).optional()
691
761
  });
692
762
 
693
763
  // src/anthropic-prepare-tools.ts
@@ -2018,6 +2088,8 @@ function mapAnthropicStopReason({
2018
2088
  return isJsonResponseFromTool ? "stop" : "tool-calls";
2019
2089
  case "max_tokens":
2020
2090
  return "length";
2091
+ case "model_context_window_exceeded":
2092
+ return "length";
2021
2093
  default:
2022
2094
  return "unknown";
2023
2095
  }
@@ -2141,6 +2213,7 @@ var AnthropicMessagesLanguageModel = class {
2141
2213
  description: "Respond with a JSON object.",
2142
2214
  inputSchema: responseFormat.schema
2143
2215
  } : void 0;
2216
+ const contextManagement = anthropicOptions == null ? void 0 : anthropicOptions.contextManagement;
2144
2217
  const cacheControlValidator = new CacheControlValidator();
2145
2218
  const toolNameMapping = createToolNameMapping({
2146
2219
  tools,
@@ -2170,7 +2243,7 @@ var AnthropicMessagesLanguageModel = class {
2170
2243
  toolNameMapping
2171
2244
  });
2172
2245
  const isThinking = ((_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.type) === "enabled";
2173
- const thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2246
+ let thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2174
2247
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
2175
2248
  const baseArgs = {
2176
2249
  // model id:
@@ -2221,13 +2294,57 @@ var AnthropicMessagesLanguageModel = class {
2221
2294
  },
2222
2295
  // prompt:
2223
2296
  system: messagesPrompt.system,
2224
- messages: messagesPrompt.messages
2297
+ messages: messagesPrompt.messages,
2298
+ ...contextManagement && {
2299
+ context_management: {
2300
+ edits: contextManagement.edits.map((edit) => {
2301
+ const strategy = edit.type;
2302
+ switch (strategy) {
2303
+ case "clear_tool_uses_20250919":
2304
+ return {
2305
+ type: edit.type,
2306
+ ...edit.trigger !== void 0 && {
2307
+ trigger: edit.trigger
2308
+ },
2309
+ ...edit.keep !== void 0 && { keep: edit.keep },
2310
+ ...edit.clearAtLeast !== void 0 && {
2311
+ clear_at_least: edit.clearAtLeast
2312
+ },
2313
+ ...edit.clearToolInputs !== void 0 && {
2314
+ clear_tool_inputs: edit.clearToolInputs
2315
+ },
2316
+ ...edit.excludeTools !== void 0 && {
2317
+ exclude_tools: edit.excludeTools
2318
+ }
2319
+ };
2320
+ case "clear_thinking_20251015":
2321
+ return {
2322
+ type: edit.type,
2323
+ ...edit.keep !== void 0 && { keep: edit.keep }
2324
+ };
2325
+ default:
2326
+ warnings.push({
2327
+ type: "other",
2328
+ message: `Unknown context management strategy: ${strategy}`
2329
+ });
2330
+ return void 0;
2331
+ }
2332
+ }).filter((edit) => edit !== void 0)
2333
+ }
2334
+ }
2225
2335
  };
2226
2336
  if (isThinking) {
2227
2337
  if (thinkingBudget == null) {
2228
- throw new UnsupportedFunctionalityError3({
2229
- functionality: "thinking requires a budget"
2338
+ warnings.push({
2339
+ type: "compatibility",
2340
+ feature: "extended thinking",
2341
+ details: "thinking budget is required when thinking is enabled. using default budget of 1024 tokens."
2230
2342
  });
2343
+ baseArgs.thinking = {
2344
+ type: "enabled",
2345
+ budget_tokens: 1024
2346
+ };
2347
+ thinkingBudget = 1024;
2231
2348
  }
2232
2349
  if (baseArgs.temperature != null) {
2233
2350
  baseArgs.temperature = void 0;
@@ -2253,7 +2370,7 @@ var AnthropicMessagesLanguageModel = class {
2253
2370
  details: "topP is not supported when thinking is enabled"
2254
2371
  });
2255
2372
  }
2256
- baseArgs.max_tokens = maxTokens + thinkingBudget;
2373
+ baseArgs.max_tokens = maxTokens + (thinkingBudget != null ? thinkingBudget : 0);
2257
2374
  }
2258
2375
  if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2259
2376
  if (maxOutputTokens != null) {
@@ -2268,6 +2385,9 @@ var AnthropicMessagesLanguageModel = class {
2268
2385
  if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
2269
2386
  betas.add("mcp-client-2025-04-04");
2270
2387
  }
2388
+ if (contextManagement) {
2389
+ betas.add("context-management-2025-06-27");
2390
+ }
2271
2391
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2272
2392
  betas.add("code-execution-2025-08-25");
2273
2393
  betas.add("skills-2025-10-02");
@@ -2379,7 +2499,7 @@ var AnthropicMessagesLanguageModel = class {
2379
2499
  });
2380
2500
  }
2381
2501
  async doGenerate(options) {
2382
- var _a, _b, _c, _d, _e, _f, _g, _h;
2502
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2383
2503
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2384
2504
  ...options,
2385
2505
  stream: false,
@@ -2705,7 +2825,10 @@ var AnthropicMessagesLanguageModel = class {
2705
2825
  skillId: skill.skill_id,
2706
2826
  version: skill.version
2707
2827
  }))) != null ? _h : null
2708
- } : null
2828
+ } : null,
2829
+ contextManagement: (_i = mapAnthropicResponseContextManagement(
2830
+ response.context_management
2831
+ )) != null ? _i : null
2709
2832
  }
2710
2833
  }
2711
2834
  };
@@ -2744,6 +2867,7 @@ var AnthropicMessagesLanguageModel = class {
2744
2867
  };
2745
2868
  const contentBlocks = {};
2746
2869
  const mcpToolCalls = {};
2870
+ let contextManagement = null;
2747
2871
  let rawUsage = void 0;
2748
2872
  let cacheCreationInputTokens = null;
2749
2873
  let stopSequence = null;
@@ -3226,6 +3350,11 @@ var AnthropicMessagesLanguageModel = class {
3226
3350
  version: skill.version
3227
3351
  }))) != null ? _j : null
3228
3352
  } : null;
3353
+ if (value.delta.context_management) {
3354
+ contextManagement = mapAnthropicResponseContextManagement(
3355
+ value.delta.context_management
3356
+ );
3357
+ }
3229
3358
  rawUsage = {
3230
3359
  ...rawUsage,
3231
3360
  ...value.usage
@@ -3242,7 +3371,8 @@ var AnthropicMessagesLanguageModel = class {
3242
3371
  usage: rawUsage != null ? rawUsage : null,
3243
3372
  cacheCreationInputTokens,
3244
3373
  stopSequence,
3245
- container
3374
+ container,
3375
+ contextManagement
3246
3376
  }
3247
3377
  }
3248
3378
  });
@@ -3337,6 +3467,27 @@ function getModelCapabilities(modelId) {
3337
3467
  };
3338
3468
  }
3339
3469
  }
3470
+ function mapAnthropicResponseContextManagement(contextManagement) {
3471
+ return contextManagement ? {
3472
+ appliedEdits: contextManagement.applied_edits.map((edit) => {
3473
+ const strategy = edit.type;
3474
+ switch (strategy) {
3475
+ case "clear_tool_uses_20250919":
3476
+ return {
3477
+ type: edit.type,
3478
+ clearedToolUses: edit.cleared_tool_uses,
3479
+ clearedInputTokens: edit.cleared_input_tokens
3480
+ };
3481
+ case "clear_thinking_20251015":
3482
+ return {
3483
+ type: edit.type,
3484
+ clearedThinkingTurns: edit.cleared_thinking_turns,
3485
+ clearedInputTokens: edit.cleared_input_tokens
3486
+ };
3487
+ }
3488
+ }).filter((edit) => edit !== void 0)
3489
+ } : null;
3490
+ }
3340
3491
 
3341
3492
  // src/tool/bash_20241022.ts
3342
3493
  import {