@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.
@@ -972,6 +972,15 @@ var anthropicLanguageModelOptions = lazySchema3(() => {
972
972
  * This allows you to deactivate reasoning inputs for models that do not support them.
973
973
  */
974
974
  sendReasoning: z3.boolean().optional(),
975
+ /**
976
+ * Keep a system message that follows user or assistant messages in the
977
+ * `messages` array, even when no system message comes before it. By
978
+ * default the first system message becomes the top-level `system`
979
+ * prompt wherever it appears, which models without mid-conversation
980
+ * system message support require. Enable this only for models that
981
+ * support mid-conversation system messages.
982
+ */
983
+ midConversationSystemMessages: z3.boolean().optional(),
975
984
  /**
976
985
  * Allow tool definitions supplied by value in system-message tool changes.
977
986
  * Defaults to false. Enable only for server-controlled messages: inline
@@ -2580,7 +2589,8 @@ async function convertToAnthropicMessagesPrompt({
2580
2589
  toolNameMapping,
2581
2590
  toolsetNames = {},
2582
2591
  convertMediaPart,
2583
- convertInlineTool
2592
+ convertInlineTool,
2593
+ keepMidConversationSystemMessages = false
2584
2594
  }) {
2585
2595
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
2586
2596
  const betas = /* @__PURE__ */ new Set();
@@ -2629,7 +2639,8 @@ async function convertToAnthropicMessagesPrompt({
2629
2639
  const clearAt = systemMessageOptions == null ? void 0 : systemMessageOptions.clearAt;
2630
2640
  const effort = systemMessageOptions == null ? void 0 : systemMessageOptions.effort;
2631
2641
  const content = [];
2632
- if (text !== "" || toolChanges.length === 0 && clearAt == null && effort == null) {
2642
+ const emitsText = text !== "" || toolChanges.length === 0 && clearAt == null && effort == null;
2643
+ if (emitsText) {
2633
2644
  content.push({
2634
2645
  type: "text",
2635
2646
  text,
@@ -2683,6 +2694,17 @@ async function convertToAnthropicMessagesPrompt({
2683
2694
  }
2684
2695
  });
2685
2696
  }
2697
+ const lastToolChange = content.at(-1);
2698
+ 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;
2699
+ if (!emitsText && lastToolChange != null && !definesOwnCacheControl) {
2700
+ const cacheControl = validator.getCacheControl(providerOptions, {
2701
+ type: "system message",
2702
+ canCache: true
2703
+ });
2704
+ if (cacheControl != null) {
2705
+ lastToolChange.cache_control = cacheControl;
2706
+ }
2707
+ }
2686
2708
  if (content.length === 0 && clearAt == null && effort == null) {
2687
2709
  continue;
2688
2710
  }
@@ -2694,7 +2716,7 @@ async function convertToAnthropicMessagesPrompt({
2694
2716
  ...effort != null && { output_config: { effort } }
2695
2717
  });
2696
2718
  }
2697
- if (i === 0 || system == null && toolChangeCount === 0 && !hasMidConversationOptions) {
2719
+ if (i === 0 || !keepMidConversationSystemMessages && system == null && toolChangeCount === 0 && !hasMidConversationOptions) {
2698
2720
  if (toolChangeCount > 0) {
2699
2721
  warnings.push({
2700
2722
  type: "other",
@@ -4205,7 +4227,7 @@ var AnthropicMessagesLanguageModel = class {
4205
4227
  providerOptions,
4206
4228
  stream
4207
4229
  }) {
4208
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
4230
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
4209
4231
  const warnings = [];
4210
4232
  if (frequencyPenalty != null) {
4211
4233
  warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
@@ -4384,6 +4406,7 @@ var AnthropicMessagesLanguageModel = class {
4384
4406
  toolNameMapping,
4385
4407
  toolsetNames,
4386
4408
  convertMediaPart: this.config.convertMediaPart,
4409
+ keepMidConversationSystemMessages: (_g = anthropicOptions == null ? void 0 : anthropicOptions.midConversationSystemMessages) != null ? _g : false,
4387
4410
  convertInlineTool: async (tool) => {
4388
4411
  const {
4389
4412
  tools: definitions,
@@ -4534,7 +4557,7 @@ var AnthropicMessagesLanguageModel = class {
4534
4557
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
4535
4558
  cache_control: anthropicOptions.cacheControl
4536
4559
  },
4537
- ...((_g = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _g.userId) != null && {
4560
+ ...((_h = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _h.userId) != null && {
4538
4561
  metadata: { user_id: anthropicOptions.metadata.userId }
4539
4562
  },
4540
4563
  // mcp servers:
@@ -4764,9 +4787,9 @@ var AnthropicMessagesLanguageModel = class {
4764
4787
  }
4765
4788
  );
4766
4789
  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) {
4767
- for (const server of (_h = baseArgs.mcp_servers) != null ? _h : []) {
4790
+ for (const server of (_i = baseArgs.mcp_servers) != null ? _i : []) {
4768
4791
  server.tool_configuration = {
4769
- allowed_tools: (_i = server.tool_configuration) == null ? void 0 : _i.allowed_tools,
4792
+ allowed_tools: (_j = server.tool_configuration) == null ? void 0 : _j.allowed_tools,
4770
4793
  enabled: false
4771
4794
  };
4772
4795
  }
@@ -4801,7 +4824,7 @@ var AnthropicMessagesLanguageModel = class {
4801
4824
  ...betas,
4802
4825
  ...toolsBetas,
4803
4826
  ...userSuppliedBetas,
4804
- ...(_j = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _j : []
4827
+ ...(_k = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _k : []
4805
4828
  ]),
4806
4829
  usesJsonResponseTool: jsonResponseTool != null,
4807
4830
  toolNameMapping,