@ai-sdk/anthropic 3.0.144 → 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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 3.0.146
4
+
5
+ ### Patch Changes
6
+
7
+ - dcfd6e6: fix(anthropic): keep the cache control of mid-conversation system messages that carry only tool changes, and add the `midConversationSystemMessages` option to keep a later system message in place when no system message comes before it
8
+
9
+ ## 3.0.145
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [9e7f32b]
14
+ - @ai-sdk/provider-utils@4.0.69
15
+
3
16
  ## 3.0.144
4
17
 
5
18
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -252,6 +252,7 @@ declare const anthropicSystemMessageProviderOptions: _ai_sdk_provider_utils.Lazy
252
252
  type AnthropicSystemMessageProviderOptions = InferSchema<typeof anthropicSystemMessageProviderOptions>;
253
253
  declare const anthropicLanguageModelOptions: _ai_sdk_provider_utils.LazySchema<{
254
254
  sendReasoning?: boolean | undefined;
255
+ midConversationSystemMessages?: boolean | undefined;
255
256
  allowInlineToolDefinitions?: boolean | undefined;
256
257
  structuredOutputMode?: "auto" | "outputFormat" | "jsonTool" | undefined;
257
258
  thinking?: {
package/dist/index.d.ts CHANGED
@@ -252,6 +252,7 @@ declare const anthropicSystemMessageProviderOptions: _ai_sdk_provider_utils.Lazy
252
252
  type AnthropicSystemMessageProviderOptions = InferSchema<typeof anthropicSystemMessageProviderOptions>;
253
253
  declare const anthropicLanguageModelOptions: _ai_sdk_provider_utils.LazySchema<{
254
254
  sendReasoning?: boolean | undefined;
255
+ midConversationSystemMessages?: boolean | undefined;
255
256
  allowInlineToolDefinitions?: boolean | undefined;
256
257
  structuredOutputMode?: "auto" | "outputFormat" | "jsonTool" | undefined;
257
258
  thinking?: {
package/dist/index.js CHANGED
@@ -32,7 +32,7 @@ var import_provider4 = require("@ai-sdk/provider");
32
32
  var import_provider_utils28 = require("@ai-sdk/provider-utils");
33
33
 
34
34
  // src/version.ts
35
- var VERSION = true ? "3.0.144" : "0.0.0-test";
35
+ var VERSION = true ? "3.0.146" : "0.0.0-test";
36
36
 
37
37
  // src/anthropic-messages-language-model.ts
38
38
  var import_provider3 = require("@ai-sdk/provider");
@@ -984,6 +984,15 @@ var anthropicLanguageModelOptions = (0, import_provider_utils3.lazySchema)(() =>
984
984
  * This allows you to deactivate reasoning inputs for models that do not support them.
985
985
  */
986
986
  sendReasoning: import_v43.z.boolean().optional(),
987
+ /**
988
+ * Keep a system message that follows user or assistant messages in the
989
+ * `messages` array, even when no system message comes before it. By
990
+ * default the first system message becomes the top-level `system`
991
+ * prompt wherever it appears, which models without mid-conversation
992
+ * system message support require. Enable this only for models that
993
+ * support mid-conversation system messages.
994
+ */
995
+ midConversationSystemMessages: import_v43.z.boolean().optional(),
987
996
  /**
988
997
  * Allow tool definitions supplied by value in system-message tool changes.
989
998
  * Defaults to false. Enable only for server-controlled messages: inline
@@ -2535,7 +2544,8 @@ async function convertToAnthropicMessagesPrompt({
2535
2544
  toolNameMapping,
2536
2545
  toolsetNames = {},
2537
2546
  convertMediaPart,
2538
- convertInlineTool
2547
+ convertInlineTool,
2548
+ keepMidConversationSystemMessages = false
2539
2549
  }) {
2540
2550
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
2541
2551
  const betas = /* @__PURE__ */ new Set();
@@ -2584,7 +2594,8 @@ async function convertToAnthropicMessagesPrompt({
2584
2594
  const clearAt = systemMessageOptions == null ? void 0 : systemMessageOptions.clearAt;
2585
2595
  const effort = systemMessageOptions == null ? void 0 : systemMessageOptions.effort;
2586
2596
  const content = [];
2587
- if (text !== "" || toolChanges.length === 0 && clearAt == null && effort == null) {
2597
+ const emitsText = text !== "" || toolChanges.length === 0 && clearAt == null && effort == null;
2598
+ if (emitsText) {
2588
2599
  content.push({
2589
2600
  type: "text",
2590
2601
  text,
@@ -2638,6 +2649,17 @@ async function convertToAnthropicMessagesPrompt({
2638
2649
  }
2639
2650
  });
2640
2651
  }
2652
+ const lastToolChange = content.at(-1);
2653
+ 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;
2654
+ if (!emitsText && lastToolChange != null && !definesOwnCacheControl) {
2655
+ const cacheControl = validator.getCacheControl(providerOptions, {
2656
+ type: "system message",
2657
+ canCache: true
2658
+ });
2659
+ if (cacheControl != null) {
2660
+ lastToolChange.cache_control = cacheControl;
2661
+ }
2662
+ }
2641
2663
  if (content.length === 0 && clearAt == null && effort == null) {
2642
2664
  continue;
2643
2665
  }
@@ -2649,7 +2671,7 @@ async function convertToAnthropicMessagesPrompt({
2649
2671
  ...effort != null && { output_config: { effort } }
2650
2672
  });
2651
2673
  }
2652
- if (i === 0 || system == null && toolChangeCount === 0 && !hasMidConversationOptions) {
2674
+ if (i === 0 || !keepMidConversationSystemMessages && system == null && toolChangeCount === 0 && !hasMidConversationOptions) {
2653
2675
  if (toolChangeCount > 0) {
2654
2676
  warnings.push({
2655
2677
  type: "other",
@@ -4160,7 +4182,7 @@ var AnthropicMessagesLanguageModel = class {
4160
4182
  providerOptions,
4161
4183
  stream
4162
4184
  }) {
4163
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
4185
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
4164
4186
  const warnings = [];
4165
4187
  if (frequencyPenalty != null) {
4166
4188
  warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
@@ -4339,6 +4361,7 @@ var AnthropicMessagesLanguageModel = class {
4339
4361
  toolNameMapping,
4340
4362
  toolsetNames,
4341
4363
  convertMediaPart: this.config.convertMediaPart,
4364
+ keepMidConversationSystemMessages: (_g = anthropicOptions == null ? void 0 : anthropicOptions.midConversationSystemMessages) != null ? _g : false,
4342
4365
  convertInlineTool: async (tool) => {
4343
4366
  const {
4344
4367
  tools: definitions,
@@ -4489,7 +4512,7 @@ var AnthropicMessagesLanguageModel = class {
4489
4512
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
4490
4513
  cache_control: anthropicOptions.cacheControl
4491
4514
  },
4492
- ...((_g = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _g.userId) != null && {
4515
+ ...((_h = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _h.userId) != null && {
4493
4516
  metadata: { user_id: anthropicOptions.metadata.userId }
4494
4517
  },
4495
4518
  // mcp servers:
@@ -4719,9 +4742,9 @@ var AnthropicMessagesLanguageModel = class {
4719
4742
  }
4720
4743
  );
4721
4744
  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) {
4722
- for (const server of (_h = baseArgs.mcp_servers) != null ? _h : []) {
4745
+ for (const server of (_i = baseArgs.mcp_servers) != null ? _i : []) {
4723
4746
  server.tool_configuration = {
4724
- allowed_tools: (_i = server.tool_configuration) == null ? void 0 : _i.allowed_tools,
4747
+ allowed_tools: (_j = server.tool_configuration) == null ? void 0 : _j.allowed_tools,
4725
4748
  enabled: false
4726
4749
  };
4727
4750
  }
@@ -4756,7 +4779,7 @@ var AnthropicMessagesLanguageModel = class {
4756
4779
  ...betas,
4757
4780
  ...toolsBetas,
4758
4781
  ...userSuppliedBetas,
4759
- ...(_j = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _j : []
4782
+ ...(_k = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _k : []
4760
4783
  ]),
4761
4784
  usesJsonResponseTool: jsonResponseTool != null,
4762
4785
  toolNameMapping,