@ai-sdk/anthropic 3.0.115 → 3.0.117

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
@@ -12,7 +12,7 @@ import {
12
12
  } from "@ai-sdk/provider-utils";
13
13
 
14
14
  // src/version.ts
15
- var VERSION = true ? "3.0.115" : "0.0.0-test";
15
+ var VERSION = true ? "3.0.117" : "0.0.0-test";
16
16
 
17
17
  // src/anthropic-messages-language-model.ts
18
18
  import {
@@ -866,6 +866,21 @@ var anthropicFilePartProviderOptions = z3.object({
866
866
  context: z3.string().optional()
867
867
  });
868
868
  var anthropicSystemMessageProviderOptions = z3.object({
869
+ /**
870
+ * Clears this mid-conversation system message after the current turn.
871
+ *
872
+ * Requires the `mid-conversation-system-clear-at-2026-08-21` beta,
873
+ * which is added automatically.
874
+ */
875
+ clearAt: z3.literal("next_user_message").optional(),
876
+ /**
877
+ * Overrides the effort level for the turn following this
878
+ * mid-conversation system message.
879
+ *
880
+ * Requires the `mid-conversation-effort-2026-08-01` beta,
881
+ * which is added automatically.
882
+ */
883
+ effort: z3.enum(["low", "medium", "high", "xhigh", "max"]).optional(),
869
884
  /**
870
885
  * Mid-conversation tool changes. Adds or removes tools from the
871
886
  * conversation's tool set between turns without invalidating the prompt
@@ -893,6 +908,9 @@ var anthropicSystemMessageProviderOptions = z3.object({
893
908
  ])
894
909
  ).optional()
895
910
  });
911
+ var anthropicThinkingBlockBinding = z3.object({
912
+ prefixMismatchBehavior: z3.literal("drop_block")
913
+ });
896
914
  var anthropicLanguageModelOptions = z3.object({
897
915
  /**
898
916
  * Whether to send reasoning to the model.
@@ -914,24 +932,41 @@ var anthropicLanguageModelOptions = z3.object({
914
932
  * When enabled, responses include thinking content blocks showing Claude's thinking process before the final answer.
915
933
  * Requires a minimum budget of 1,024 tokens and counts towards the `max_tokens` limit.
916
934
  */
917
- thinking: z3.discriminatedUnion("type", [
918
- z3.object({
919
- /** for Sonnet 4.6, Opus 4.6, and newer models */
920
- type: z3.literal("adaptive"),
921
- /**
922
- * Controls whether thinking content is included in the response.
923
- * - `"omitted"`: Thinking blocks are present but text is empty (default for Opus 4.7+).
924
- * - `"summarized"`: Thinking content is returned. Required to see reasoning output.
925
- */
926
- display: z3.enum(["omitted", "summarized"]).optional()
927
- }),
928
- z3.object({
929
- /** for models before Opus 4.6, except Sonnet 4.6 still supports it */
930
- type: z3.literal("enabled"),
931
- budgetTokens: z3.number().optional()
932
- }),
935
+ thinking: z3.union([
936
+ z3.discriminatedUnion("type", [
937
+ z3.object({
938
+ /** for Sonnet 4.6, Opus 4.6, and newer models */
939
+ type: z3.literal("adaptive"),
940
+ /**
941
+ * Controls whether thinking content is included in the response.
942
+ * - `"omitted"`: Thinking blocks are present but text is empty (default for Opus 4.7+).
943
+ * - `"summarized"`: Thinking content is returned. Required to see reasoning output.
944
+ * - `"updates"`: Thinking updates are returned between tool calls.
945
+ */
946
+ display: z3.enum(["omitted", "summarized", "updates"]).optional(),
947
+ /**
948
+ * Controls how thinking blocks are bound to an assistant prefix.
949
+ *
950
+ * Requires the `thinking-binding-controls-2026-08-01` beta,
951
+ * which is added automatically.
952
+ */
953
+ blockBinding: anthropicThinkingBlockBinding.optional()
954
+ }),
955
+ z3.object({
956
+ /** for models before Opus 4.6, except Sonnet 4.6 still supports it */
957
+ type: z3.literal("enabled"),
958
+ budgetTokens: z3.number().optional()
959
+ }),
960
+ z3.object({
961
+ type: z3.literal("disabled")
962
+ })
963
+ ]),
964
+ /**
965
+ * Configures prefix mismatch recovery without changing the model's
966
+ * default thinking mode.
967
+ */
933
968
  z3.object({
934
- type: z3.literal("disabled")
969
+ blockBinding: anthropicThinkingBlockBinding
935
970
  })
936
971
  ]).optional(),
937
972
  /**
@@ -2303,8 +2338,9 @@ async function convertToAnthropicMessagesPrompt({
2303
2338
  const type = block.type;
2304
2339
  switch (type) {
2305
2340
  case "system": {
2306
- const content = [];
2341
+ const systemMessages = [];
2307
2342
  let toolChangeCount = 0;
2343
+ let hasMidConversationOptions = false;
2308
2344
  for (const { content: text, providerOptions } of block.messages) {
2309
2345
  const systemMessageOptions = await parseProviderOptions({
2310
2346
  provider: "anthropic",
@@ -2312,7 +2348,10 @@ async function convertToAnthropicMessagesPrompt({
2312
2348
  schema: anthropicSystemMessageProviderOptions
2313
2349
  });
2314
2350
  const toolChanges = (_a = systemMessageOptions == null ? void 0 : systemMessageOptions.toolChanges) != null ? _a : [];
2315
- if (text !== "" || toolChanges.length === 0) {
2351
+ const clearAt = systemMessageOptions == null ? void 0 : systemMessageOptions.clearAt;
2352
+ const effort = systemMessageOptions == null ? void 0 : systemMessageOptions.effort;
2353
+ const content = [];
2354
+ if (text !== "" || toolChanges.length === 0 && clearAt == null && effort == null) {
2316
2355
  content.push({
2317
2356
  type: "text",
2318
2357
  text,
@@ -2332,23 +2371,44 @@ async function convertToAnthropicMessagesPrompt({
2332
2371
  }
2333
2372
  });
2334
2373
  }
2374
+ hasMidConversationOptions || (hasMidConversationOptions = clearAt != null || effort != null);
2375
+ systemMessages.push({
2376
+ role: "system",
2377
+ content,
2378
+ ...clearAt != null && { clear_at: clearAt },
2379
+ ...effort != null && { output_config: { effort } }
2380
+ });
2335
2381
  }
2336
- if (i === 0 || system == null && toolChangeCount === 0) {
2382
+ if (i === 0 || system == null && toolChangeCount === 0 && !hasMidConversationOptions) {
2337
2383
  if (toolChangeCount > 0) {
2338
2384
  warnings.push({
2339
2385
  type: "other",
2340
2386
  message: "tool changes on the initial system message are not supported by Anthropic. Configure the initial tool set via the tools option instead. The tool changes have been ignored."
2341
2387
  });
2342
2388
  }
2343
- system = content.filter(
2344
- (part) => part.type === "text"
2389
+ if (hasMidConversationOptions) {
2390
+ warnings.push({
2391
+ type: "other",
2392
+ message: "clearAt and effort on the initial system message are not supported by Anthropic. Configure these options on a mid-conversation system message instead. The options have been ignored."
2393
+ });
2394
+ }
2395
+ system = systemMessages.flatMap(
2396
+ (message) => message.content.filter(
2397
+ (part) => part.type === "text"
2398
+ )
2345
2399
  );
2346
2400
  } else {
2347
- messages.push({ role: "system", content });
2401
+ messages.push(...systemMessages);
2348
2402
  betas.add("mid-conversation-system-2026-04-07");
2349
2403
  if (toolChangeCount > 0) {
2350
2404
  betas.add("mid-conversation-tool-changes-2026-07-01");
2351
2405
  }
2406
+ if (systemMessages.some((message) => message.clear_at != null)) {
2407
+ betas.add("mid-conversation-system-clear-at-2026-08-21");
2408
+ }
2409
+ if (systemMessages.some((message) => message.output_config != null)) {
2410
+ betas.add("mid-conversation-effort-2026-08-01");
2411
+ }
2352
2412
  }
2353
2413
  break;
2354
2414
  }
@@ -3464,7 +3524,7 @@ var AnthropicMessagesLanguageModel = class {
3464
3524
  providerOptions,
3465
3525
  stream
3466
3526
  }) {
3467
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
3527
+ var _a, _b, _c, _d, _e, _f, _g;
3468
3528
  const warnings = [];
3469
3529
  if (frequencyPenalty != null) {
3470
3530
  warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
@@ -3607,7 +3667,9 @@ var AnthropicMessagesLanguageModel = class {
3607
3667
  cacheControlValidator,
3608
3668
  toolNameMapping
3609
3669
  });
3610
- if (rejectsThinkingDisabledAboveHighEffort && ((_e = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _e.type) === "disabled" && (anthropicOptions.effort === "xhigh" || anthropicOptions.effort === "max")) {
3670
+ const thinking = anthropicOptions == null ? void 0 : anthropicOptions.thinking;
3671
+ const thinkingType = thinking != null && "type" in thinking ? thinking.type : void 0;
3672
+ if (rejectsThinkingDisabledAboveHighEffort && thinkingType === "disabled" && (anthropicOptions.effort === "xhigh" || anthropicOptions.effort === "max")) {
3611
3673
  warnings.push({
3612
3674
  type: "unsupported",
3613
3675
  feature: "providerOptions.anthropic.effort",
@@ -3615,11 +3677,12 @@ var AnthropicMessagesLanguageModel = class {
3615
3677
  });
3616
3678
  anthropicOptions.effort = "high";
3617
3679
  }
3618
- const thinkingType = (_f = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _f.type;
3619
3680
  const isThinking = thinkingType === "enabled" || thinkingType === "adaptive";
3620
3681
  const sendThinking = isThinking || thinkingType === "disabled";
3621
- let thinkingBudget = thinkingType === "enabled" ? (_g = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _g.budgetTokens : void 0;
3622
- const thinkingDisplay = thinkingType === "adaptive" ? (_h = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _h.display : void 0;
3682
+ let thinkingBudget = thinkingType === "enabled" && thinking != null && "budgetTokens" in thinking ? thinking.budgetTokens : void 0;
3683
+ const thinkingDisplay = thinkingType === "adaptive" && thinking != null && "display" in thinking ? thinking.display : void 0;
3684
+ const thinkingBlockBinding = thinking != null && "blockBinding" in thinking ? thinking.blockBinding : void 0;
3685
+ const sendThinkingConfig = sendThinking || thinkingBlockBinding != null;
3623
3686
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
3624
3687
  const baseArgs = {
3625
3688
  // model id:
@@ -3631,11 +3694,16 @@ var AnthropicMessagesLanguageModel = class {
3631
3694
  top_p: topP,
3632
3695
  stop_sequences: stopSequences,
3633
3696
  // provider specific settings:
3634
- ...sendThinking && {
3697
+ ...sendThinkingConfig && {
3635
3698
  thinking: {
3636
- type: thinkingType,
3699
+ ...thinkingType != null && { type: thinkingType },
3637
3700
  ...thinkingBudget != null && { budget_tokens: thinkingBudget },
3638
- ...thinkingDisplay != null && { display: thinkingDisplay }
3701
+ ...thinkingDisplay != null && { display: thinkingDisplay },
3702
+ ...thinkingBlockBinding != null && {
3703
+ block_binding: {
3704
+ prefix_mismatch_behavior: thinkingBlockBinding.prefixMismatchBehavior
3705
+ }
3706
+ }
3639
3707
  }
3640
3708
  },
3641
3709
  ...((anthropicOptions == null ? void 0 : anthropicOptions.effort) || (anthropicOptions == null ? void 0 : anthropicOptions.taskBudget) || useStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) && {
@@ -3672,7 +3740,7 @@ var AnthropicMessagesLanguageModel = class {
3672
3740
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
3673
3741
  cache_control: anthropicOptions.cacheControl
3674
3742
  },
3675
- ...((_i = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _i.userId) != null && {
3743
+ ...((_e = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _e.userId) != null && {
3676
3744
  metadata: { user_id: anthropicOptions.metadata.userId }
3677
3745
  },
3678
3746
  // mcp servers:
@@ -3842,6 +3910,12 @@ var AnthropicMessagesLanguageModel = class {
3842
3910
  if (anthropicOptions == null ? void 0 : anthropicOptions.taskBudget) {
3843
3911
  betas.add("task-budgets-2026-03-13");
3844
3912
  }
3913
+ if (thinkingDisplay === "updates") {
3914
+ betas.add("thinking-display-updates-2026-08-18");
3915
+ }
3916
+ if (thinkingBlockBinding != null) {
3917
+ betas.add("thinking-binding-controls-2026-08-01");
3918
+ }
3845
3919
  if ((anthropicOptions == null ? void 0 : anthropicOptions.speed) === "fast") {
3846
3920
  betas.add("fast-mode-2026-02-01");
3847
3921
  }
@@ -3850,7 +3924,7 @@ var AnthropicMessagesLanguageModel = class {
3850
3924
  } else if ((anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0) {
3851
3925
  betas.add("server-side-fallback-2026-06-01");
3852
3926
  }
3853
- const defaultEagerInputStreaming = stream && ((_j = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _j : true);
3927
+ const defaultEagerInputStreaming = stream && ((_f = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _f : true);
3854
3928
  const {
3855
3929
  tools: anthropicTools2,
3856
3930
  toolChoice: anthropicToolChoice,
@@ -3889,7 +3963,7 @@ var AnthropicMessagesLanguageModel = class {
3889
3963
  ...betas,
3890
3964
  ...toolsBetas,
3891
3965
  ...userSuppliedBetas,
3892
- ...(_k = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _k : []
3966
+ ...(_g = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _g : []
3893
3967
  ]),
3894
3968
  usesJsonResponseTool: jsonResponseTool != null,
3895
3969
  toolNameMapping,