@ai-sdk/anthropic 3.0.145 → 3.0.146

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.
@@ -979,6 +979,15 @@ var anthropicLanguageModelOptions = (0, import_provider_utils3.lazySchema)(() =>
979
979
  * This allows you to deactivate reasoning inputs for models that do not support them.
980
980
  */
981
981
  sendReasoning: import_v43.z.boolean().optional(),
982
+ /**
983
+ * Keep a system message that follows user or assistant messages in the
984
+ * `messages` array, even when no system message comes before it. By
985
+ * default the first system message becomes the top-level `system`
986
+ * prompt wherever it appears, which models without mid-conversation
987
+ * system message support require. Enable this only for models that
988
+ * support mid-conversation system messages.
989
+ */
990
+ midConversationSystemMessages: import_v43.z.boolean().optional(),
982
991
  /**
983
992
  * Allow tool definitions supplied by value in system-message tool changes.
984
993
  * Defaults to false. Enable only for server-controlled messages: inline
@@ -2530,7 +2539,8 @@ async function convertToAnthropicMessagesPrompt({
2530
2539
  toolNameMapping,
2531
2540
  toolsetNames = {},
2532
2541
  convertMediaPart,
2533
- convertInlineTool
2542
+ convertInlineTool,
2543
+ keepMidConversationSystemMessages = false
2534
2544
  }) {
2535
2545
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
2536
2546
  const betas = /* @__PURE__ */ new Set();
@@ -2579,7 +2589,8 @@ async function convertToAnthropicMessagesPrompt({
2579
2589
  const clearAt = systemMessageOptions == null ? void 0 : systemMessageOptions.clearAt;
2580
2590
  const effort = systemMessageOptions == null ? void 0 : systemMessageOptions.effort;
2581
2591
  const content = [];
2582
- if (text !== "" || toolChanges.length === 0 && clearAt == null && effort == null) {
2592
+ const emitsText = text !== "" || toolChanges.length === 0 && clearAt == null && effort == null;
2593
+ if (emitsText) {
2583
2594
  content.push({
2584
2595
  type: "text",
2585
2596
  text,
@@ -2633,6 +2644,17 @@ async function convertToAnthropicMessagesPrompt({
2633
2644
  }
2634
2645
  });
2635
2646
  }
2647
+ const lastToolChange = content.at(-1);
2648
+ const definesOwnCacheControl = (lastToolChange == null ? void 0 : lastToolChange.type) === "tool_addition" && lastToolChange.tool.type === "tool_definition" && "cache_control" in lastToolChange.tool.definition && lastToolChange.tool.definition.cache_control != null;
2649
+ if (!emitsText && lastToolChange != null && !definesOwnCacheControl) {
2650
+ const cacheControl = validator.getCacheControl(providerOptions, {
2651
+ type: "system message",
2652
+ canCache: true
2653
+ });
2654
+ if (cacheControl != null) {
2655
+ lastToolChange.cache_control = cacheControl;
2656
+ }
2657
+ }
2636
2658
  if (content.length === 0 && clearAt == null && effort == null) {
2637
2659
  continue;
2638
2660
  }
@@ -2644,7 +2666,7 @@ async function convertToAnthropicMessagesPrompt({
2644
2666
  ...effort != null && { output_config: { effort } }
2645
2667
  });
2646
2668
  }
2647
- if (i === 0 || system == null && toolChangeCount === 0 && !hasMidConversationOptions) {
2669
+ if (i === 0 || !keepMidConversationSystemMessages && system == null && toolChangeCount === 0 && !hasMidConversationOptions) {
2648
2670
  if (toolChangeCount > 0) {
2649
2671
  warnings.push({
2650
2672
  type: "other",
@@ -4155,7 +4177,7 @@ var AnthropicMessagesLanguageModel = class {
4155
4177
  providerOptions,
4156
4178
  stream
4157
4179
  }) {
4158
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
4180
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
4159
4181
  const warnings = [];
4160
4182
  if (frequencyPenalty != null) {
4161
4183
  warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
@@ -4334,6 +4356,7 @@ var AnthropicMessagesLanguageModel = class {
4334
4356
  toolNameMapping,
4335
4357
  toolsetNames,
4336
4358
  convertMediaPart: this.config.convertMediaPart,
4359
+ keepMidConversationSystemMessages: (_g = anthropicOptions == null ? void 0 : anthropicOptions.midConversationSystemMessages) != null ? _g : false,
4337
4360
  convertInlineTool: async (tool) => {
4338
4361
  const {
4339
4362
  tools: definitions,
@@ -4484,7 +4507,7 @@ var AnthropicMessagesLanguageModel = class {
4484
4507
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
4485
4508
  cache_control: anthropicOptions.cacheControl
4486
4509
  },
4487
- ...((_g = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _g.userId) != null && {
4510
+ ...((_h = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _h.userId) != null && {
4488
4511
  metadata: { user_id: anthropicOptions.metadata.userId }
4489
4512
  },
4490
4513
  // mcp servers:
@@ -4714,9 +4737,9 @@ var AnthropicMessagesLanguageModel = class {
4714
4737
  }
4715
4738
  );
4716
4739
  if ((toolChoice == null ? void 0 : toolChoice.type) === "tool" && ((anthropicToolChoice == null ? void 0 : anthropicToolChoice.type) === "auto" || jsonResponseTool != null) || (toolChoice == null ? void 0 : toolChoice.type) === "none" && jsonResponseTool != null) {
4717
- for (const server of (_h = baseArgs.mcp_servers) != null ? _h : []) {
4740
+ for (const server of (_i = baseArgs.mcp_servers) != null ? _i : []) {
4718
4741
  server.tool_configuration = {
4719
- allowed_tools: (_i = server.tool_configuration) == null ? void 0 : _i.allowed_tools,
4742
+ allowed_tools: (_j = server.tool_configuration) == null ? void 0 : _j.allowed_tools,
4720
4743
  enabled: false
4721
4744
  };
4722
4745
  }
@@ -4751,7 +4774,7 @@ var AnthropicMessagesLanguageModel = class {
4751
4774
  ...betas,
4752
4775
  ...toolsBetas,
4753
4776
  ...userSuppliedBetas,
4754
- ...(_j = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _j : []
4777
+ ...(_k = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _k : []
4755
4778
  ]),
4756
4779
  usesJsonResponseTool: jsonResponseTool != null,
4757
4780
  toolNameMapping,