@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.
package/dist/index.mjs CHANGED
@@ -11,12 +11,11 @@ import {
11
11
  } from "@ai-sdk/provider-utils";
12
12
 
13
13
  // src/version.ts
14
- var VERSION = true ? "3.0.0-beta.76" : "0.0.0-test";
14
+ var VERSION = true ? "3.0.0-beta.78" : "0.0.0-test";
15
15
 
16
16
  // src/anthropic-messages-language-model.ts
17
17
  import {
18
- APICallError,
19
- UnsupportedFunctionalityError as UnsupportedFunctionalityError3
18
+ APICallError
20
19
  } from "@ai-sdk/provider";
21
20
  import {
22
21
  combineHeaders,
@@ -290,6 +289,22 @@ var anthropicMessagesResponseSchema = lazySchema2(
290
289
  version: z2.string()
291
290
  })
292
291
  ).nullish()
292
+ }).nullish(),
293
+ context_management: z2.object({
294
+ applied_edits: z2.array(
295
+ z2.union([
296
+ z2.object({
297
+ type: z2.literal("clear_tool_uses_20250919"),
298
+ cleared_tool_uses: z2.number(),
299
+ cleared_input_tokens: z2.number()
300
+ }),
301
+ z2.object({
302
+ type: z2.literal("clear_thinking_20251015"),
303
+ cleared_thinking_turns: z2.number(),
304
+ cleared_input_tokens: z2.number()
305
+ })
306
+ ])
307
+ )
293
308
  }).nullish()
294
309
  })
295
310
  )
@@ -572,6 +587,22 @@ var anthropicMessagesChunkSchema = lazySchema2(
572
587
  version: z2.string()
573
588
  })
574
589
  ).nullish()
590
+ }).nullish(),
591
+ context_management: z2.object({
592
+ applied_edits: z2.array(
593
+ z2.union([
594
+ z2.object({
595
+ type: z2.literal("clear_tool_uses_20250919"),
596
+ cleared_tool_uses: z2.number(),
597
+ cleared_input_tokens: z2.number()
598
+ }),
599
+ z2.object({
600
+ type: z2.literal("clear_thinking_20251015"),
601
+ cleared_thinking_turns: z2.number(),
602
+ cleared_input_tokens: z2.number()
603
+ })
604
+ ])
605
+ )
575
606
  }).nullish()
576
607
  }),
577
608
  usage: z2.looseObject({
@@ -702,7 +733,46 @@ var anthropicProviderOptions = z3.object({
702
733
  /**
703
734
  * @default 'high'
704
735
  */
705
- effort: z3.enum(["low", "medium", "high"]).optional()
736
+ effort: z3.enum(["low", "medium", "high"]).optional(),
737
+ contextManagement: z3.object({
738
+ edits: z3.array(
739
+ z3.discriminatedUnion("type", [
740
+ z3.object({
741
+ type: z3.literal("clear_tool_uses_20250919"),
742
+ trigger: z3.discriminatedUnion("type", [
743
+ z3.object({
744
+ type: z3.literal("input_tokens"),
745
+ value: z3.number()
746
+ }),
747
+ z3.object({
748
+ type: z3.literal("tool_uses"),
749
+ value: z3.number()
750
+ })
751
+ ]).optional(),
752
+ keep: z3.object({
753
+ type: z3.literal("tool_uses"),
754
+ value: z3.number()
755
+ }).optional(),
756
+ clearAtLeast: z3.object({
757
+ type: z3.literal("input_tokens"),
758
+ value: z3.number()
759
+ }).optional(),
760
+ clearToolInputs: z3.boolean().optional(),
761
+ excludeTools: z3.array(z3.string()).optional()
762
+ }),
763
+ z3.object({
764
+ type: z3.literal("clear_thinking_20251015"),
765
+ keep: z3.union([
766
+ z3.literal("all"),
767
+ z3.object({
768
+ type: z3.literal("thinking_turns"),
769
+ value: z3.number()
770
+ })
771
+ ]).optional()
772
+ })
773
+ ])
774
+ )
775
+ }).optional()
706
776
  });
707
777
 
708
778
  // src/anthropic-prepare-tools.ts
@@ -2033,6 +2103,8 @@ function mapAnthropicStopReason({
2033
2103
  return isJsonResponseFromTool ? "stop" : "tool-calls";
2034
2104
  case "max_tokens":
2035
2105
  return "length";
2106
+ case "model_context_window_exceeded":
2107
+ return "length";
2036
2108
  default:
2037
2109
  return "unknown";
2038
2110
  }
@@ -2156,6 +2228,7 @@ var AnthropicMessagesLanguageModel = class {
2156
2228
  description: "Respond with a JSON object.",
2157
2229
  inputSchema: responseFormat.schema
2158
2230
  } : void 0;
2231
+ const contextManagement = anthropicOptions == null ? void 0 : anthropicOptions.contextManagement;
2159
2232
  const cacheControlValidator = new CacheControlValidator();
2160
2233
  const toolNameMapping = createToolNameMapping({
2161
2234
  tools,
@@ -2185,7 +2258,7 @@ var AnthropicMessagesLanguageModel = class {
2185
2258
  toolNameMapping
2186
2259
  });
2187
2260
  const isThinking = ((_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.type) === "enabled";
2188
- const thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2261
+ let thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2189
2262
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
2190
2263
  const baseArgs = {
2191
2264
  // model id:
@@ -2236,13 +2309,57 @@ var AnthropicMessagesLanguageModel = class {
2236
2309
  },
2237
2310
  // prompt:
2238
2311
  system: messagesPrompt.system,
2239
- messages: messagesPrompt.messages
2312
+ messages: messagesPrompt.messages,
2313
+ ...contextManagement && {
2314
+ context_management: {
2315
+ edits: contextManagement.edits.map((edit) => {
2316
+ const strategy = edit.type;
2317
+ switch (strategy) {
2318
+ case "clear_tool_uses_20250919":
2319
+ return {
2320
+ type: edit.type,
2321
+ ...edit.trigger !== void 0 && {
2322
+ trigger: edit.trigger
2323
+ },
2324
+ ...edit.keep !== void 0 && { keep: edit.keep },
2325
+ ...edit.clearAtLeast !== void 0 && {
2326
+ clear_at_least: edit.clearAtLeast
2327
+ },
2328
+ ...edit.clearToolInputs !== void 0 && {
2329
+ clear_tool_inputs: edit.clearToolInputs
2330
+ },
2331
+ ...edit.excludeTools !== void 0 && {
2332
+ exclude_tools: edit.excludeTools
2333
+ }
2334
+ };
2335
+ case "clear_thinking_20251015":
2336
+ return {
2337
+ type: edit.type,
2338
+ ...edit.keep !== void 0 && { keep: edit.keep }
2339
+ };
2340
+ default:
2341
+ warnings.push({
2342
+ type: "other",
2343
+ message: `Unknown context management strategy: ${strategy}`
2344
+ });
2345
+ return void 0;
2346
+ }
2347
+ }).filter((edit) => edit !== void 0)
2348
+ }
2349
+ }
2240
2350
  };
2241
2351
  if (isThinking) {
2242
2352
  if (thinkingBudget == null) {
2243
- throw new UnsupportedFunctionalityError3({
2244
- functionality: "thinking requires a budget"
2353
+ warnings.push({
2354
+ type: "compatibility",
2355
+ feature: "extended thinking",
2356
+ details: "thinking budget is required when thinking is enabled. using default budget of 1024 tokens."
2245
2357
  });
2358
+ baseArgs.thinking = {
2359
+ type: "enabled",
2360
+ budget_tokens: 1024
2361
+ };
2362
+ thinkingBudget = 1024;
2246
2363
  }
2247
2364
  if (baseArgs.temperature != null) {
2248
2365
  baseArgs.temperature = void 0;
@@ -2268,7 +2385,7 @@ var AnthropicMessagesLanguageModel = class {
2268
2385
  details: "topP is not supported when thinking is enabled"
2269
2386
  });
2270
2387
  }
2271
- baseArgs.max_tokens = maxTokens + thinkingBudget;
2388
+ baseArgs.max_tokens = maxTokens + (thinkingBudget != null ? thinkingBudget : 0);
2272
2389
  }
2273
2390
  if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2274
2391
  if (maxOutputTokens != null) {
@@ -2283,6 +2400,9 @@ var AnthropicMessagesLanguageModel = class {
2283
2400
  if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
2284
2401
  betas.add("mcp-client-2025-04-04");
2285
2402
  }
2403
+ if (contextManagement) {
2404
+ betas.add("context-management-2025-06-27");
2405
+ }
2286
2406
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2287
2407
  betas.add("code-execution-2025-08-25");
2288
2408
  betas.add("skills-2025-10-02");
@@ -2394,7 +2514,7 @@ var AnthropicMessagesLanguageModel = class {
2394
2514
  });
2395
2515
  }
2396
2516
  async doGenerate(options) {
2397
- var _a, _b, _c, _d, _e, _f, _g, _h;
2517
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2398
2518
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2399
2519
  ...options,
2400
2520
  stream: false,
@@ -2720,7 +2840,10 @@ var AnthropicMessagesLanguageModel = class {
2720
2840
  skillId: skill.skill_id,
2721
2841
  version: skill.version
2722
2842
  }))) != null ? _h : null
2723
- } : null
2843
+ } : null,
2844
+ contextManagement: (_i = mapAnthropicResponseContextManagement(
2845
+ response.context_management
2846
+ )) != null ? _i : null
2724
2847
  }
2725
2848
  }
2726
2849
  };
@@ -2759,6 +2882,7 @@ var AnthropicMessagesLanguageModel = class {
2759
2882
  };
2760
2883
  const contentBlocks = {};
2761
2884
  const mcpToolCalls = {};
2885
+ let contextManagement = null;
2762
2886
  let rawUsage = void 0;
2763
2887
  let cacheCreationInputTokens = null;
2764
2888
  let stopSequence = null;
@@ -3241,6 +3365,11 @@ var AnthropicMessagesLanguageModel = class {
3241
3365
  version: skill.version
3242
3366
  }))) != null ? _j : null
3243
3367
  } : null;
3368
+ if (value.delta.context_management) {
3369
+ contextManagement = mapAnthropicResponseContextManagement(
3370
+ value.delta.context_management
3371
+ );
3372
+ }
3244
3373
  rawUsage = {
3245
3374
  ...rawUsage,
3246
3375
  ...value.usage
@@ -3257,7 +3386,8 @@ var AnthropicMessagesLanguageModel = class {
3257
3386
  usage: rawUsage != null ? rawUsage : null,
3258
3387
  cacheCreationInputTokens,
3259
3388
  stopSequence,
3260
- container
3389
+ container,
3390
+ contextManagement
3261
3391
  }
3262
3392
  }
3263
3393
  });
@@ -3352,6 +3482,27 @@ function getModelCapabilities(modelId) {
3352
3482
  };
3353
3483
  }
3354
3484
  }
3485
+ function mapAnthropicResponseContextManagement(contextManagement) {
3486
+ return contextManagement ? {
3487
+ appliedEdits: contextManagement.applied_edits.map((edit) => {
3488
+ const strategy = edit.type;
3489
+ switch (strategy) {
3490
+ case "clear_tool_uses_20250919":
3491
+ return {
3492
+ type: edit.type,
3493
+ clearedToolUses: edit.cleared_tool_uses,
3494
+ clearedInputTokens: edit.cleared_input_tokens
3495
+ };
3496
+ case "clear_thinking_20251015":
3497
+ return {
3498
+ type: edit.type,
3499
+ clearedThinkingTurns: edit.cleared_thinking_turns,
3500
+ clearedInputTokens: edit.cleared_input_tokens
3501
+ };
3502
+ }
3503
+ }).filter((edit) => edit !== void 0)
3504
+ } : null;
3505
+ }
3355
3506
 
3356
3507
  // src/tool/bash_20241022.ts
3357
3508
  import {