@bastani/atomic-ai 0.10.1-alpha.1 → 0.10.2-alpha.1

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.
Files changed (58) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/api/anthropic-messages.d.ts.map +1 -1
  3. package/dist/api/anthropic-messages.js +108 -40
  4. package/dist/api/anthropic-messages.js.map +1 -1
  5. package/dist/api/azure-openai-responses.js +2 -2
  6. package/dist/api/azure-openai-responses.js.map +1 -1
  7. package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
  8. package/dist/api/bedrock-converse-stream.js +51 -19
  9. package/dist/api/bedrock-converse-stream.js.map +1 -1
  10. package/dist/api/google-generative-ai.js +2 -2
  11. package/dist/api/google-generative-ai.js.map +1 -1
  12. package/dist/api/google-shared.js +2 -2
  13. package/dist/api/google-shared.js.map +1 -1
  14. package/dist/api/google-vertex.js +2 -2
  15. package/dist/api/google-vertex.js.map +1 -1
  16. package/dist/api/mistral-conversations.d.ts.map +1 -1
  17. package/dist/api/mistral-conversations.js +13 -7
  18. package/dist/api/mistral-conversations.js.map +1 -1
  19. package/dist/api/openai-codex-responses.js +2 -2
  20. package/dist/api/openai-codex-responses.js.map +1 -1
  21. package/dist/api/openai-completions.d.ts.map +1 -1
  22. package/dist/api/openai-completions.js +35 -11
  23. package/dist/api/openai-completions.js.map +1 -1
  24. package/dist/api/openai-responses-shared.d.ts.map +1 -1
  25. package/dist/api/openai-responses-shared.js +2 -2
  26. package/dist/api/openai-responses-shared.js.map +1 -1
  27. package/dist/api/openai-responses.js +2 -2
  28. package/dist/api/openai-responses.js.map +1 -1
  29. package/dist/api/simple-options.d.ts +13 -0
  30. package/dist/api/simple-options.d.ts.map +1 -1
  31. package/dist/api/simple-options.js +19 -0
  32. package/dist/api/simple-options.js.map +1 -1
  33. package/dist/api/strict-tool-limits.d.ts +9 -0
  34. package/dist/api/strict-tool-limits.d.ts.map +1 -1
  35. package/dist/api/strict-tool-limits.js +22 -0
  36. package/dist/api/strict-tool-limits.js.map +1 -1
  37. package/dist/index.d.ts +1 -0
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +1 -0
  40. package/dist/index.js.map +1 -1
  41. package/dist/providers/data/.manifest.json +1 -1
  42. package/dist/providers/data/openrouter.json +1 -1
  43. package/dist/providers/data/vercel-ai-gateway.json +1 -1
  44. package/dist/types.d.ts +23 -0
  45. package/dist/types.d.ts.map +1 -1
  46. package/dist/types.js.map +1 -1
  47. package/dist/utils/tool-results-bridge.d.ts +7 -0
  48. package/dist/utils/tool-results-bridge.d.ts.map +1 -0
  49. package/dist/utils/tool-results-bridge.js +7 -0
  50. package/dist/utils/tool-results-bridge.js.map +1 -0
  51. package/dist/utils/transcript.d.ts +35 -3
  52. package/dist/utils/transcript.d.ts.map +1 -1
  53. package/dist/utils/transcript.js +136 -5
  54. package/dist/utils/transcript.js.map +1 -1
  55. package/dist/utils/version.d.ts +1 -1
  56. package/dist/utils/version.js +1 -1
  57. package/dist/utils/version.js.map +1 -1
  58. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -4,6 +4,23 @@ This package is Atomic's fork of `@earendil-works/pi-ai`, published as `@bastani
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [0.10.2-alpha.1] - 2026-11-20
8
+
9
+ ### Added
10
+
11
+ - `appendSystemMessageUpdates()`, `resolveRequestTranscript()` and `followsToolResults()` build the request transcript for providers without mid-conversation system messages: the system messages that open the conversation fold into the leading prompt, and each later one is sent as a user message at its place in the transcript, so the prompt earlier requests sent never changes. `collapseSystemMessages()` and `resolveTranscript()` keep their behavior ([#6092](https://github.com/bastani-inc/atomic/pull/6092)).
12
+ - `UserMessage`, `AssistantMessage` and `ToolResultMessage` accept a request-time `cacheBreakpoint` hint. Anthropic, Amazon Bedrock and OpenAI-compatible Claude routes put a prompt-cache breakpoint at the end of the input sent up to that message, within the provider's limit of four breakpoints ([#6092](https://github.com/bastani-inc/atomic/pull/6092)).
13
+ - Assistant messages record `requestedThinkingBudget` (the budget the thinking level resolved to), `thinkingBudgetTokens` (the budget a budget-based Claude request sent) and `strictToolsDisabled` (the request sent preferred strict tools without strict schemas after an earlier rejection). `thinkingBudgetForLevel()` and `DEFAULT_THINKING_BUDGETS` are exported ([#6092](https://github.com/bastani-inc/atomic/pull/6092)).
14
+
15
+ ### Fixed
16
+
17
+ - Fixed providers without mid-conversation system messages (Google Gemini and Vertex, Amazon Bedrock, Anthropic and OpenAI models without that capability, OpenAI-compatible endpoints and Mistral) rewriting the cached system prompt whenever the prompt changed after the first request, such as on a new day or after a project instructions edit, which made the provider bill the whole conversation again. Prompt changes are now sent after the history as their own text, framed as system prompt updates and never inside a tool result: after the user message before them, after the tool results in the same user message on Anthropic and Amazon Bedrock, as a user turn after the function responses on Gemini and Vertex, or as a user message after the tool messages on OpenAI-compatible endpoints, with the fixed assistant message "I have processed the tool results." between them where the endpoint rejects a user message directly after a tool message, so user and assistant turns still alternate. A conversation whose earlier requests already merged such changes into the system prompt is rewritten once more, on its first request after updating ([#6092](https://github.com/bastani-inc/atomic/pull/6092)).
18
+ - Fixed a tool schema rebuilt with its object keys in another order counting as a new tool definition, which rewrote the tool list on providers that send it at the top of the request ([#6092](https://github.com/bastani-inc/atomic/pull/6092)).
19
+ - Fixed budget-based Claude thinking near the context window, on the Anthropic API and Amazon Bedrock. When fitting the budget under the context safety margin would leave less than the smallest budget, the request is now sent without thinking, and the budget returns once it fits again. Before, it was sent with a budget below the provider's 1,024-token minimum, or with a 1,024-token budget at or above `max_tokens`, both of which the provider rejects ([#6092](https://github.com/bastani-inc/atomic/pull/6092)).
20
+ - Fixed Mistral rejecting a request with a user message directly after a tool message, as after input steered in while tools ran: the fixed assistant message "I have processed the tool results." now goes between them, as on OpenAI-compatible endpoints configured with `requiresAssistantAfterToolResult` ([#6092](https://github.com/bastani-inc/atomic/pull/6092)).
21
+ - Fixed Amazon Bedrock rejecting a request with two user messages in a row, as after input steered in while tools ran: adjacent user messages are now joined into one ([#6092](https://github.com/bastani-inc/atomic/pull/6092)).
22
+ - Fixed Anthropic and Amazon Bedrock retrying strict tool schemas on the first request of a resumed session after the session's strict tools had been rejected for exceeding Claude's limits. A recorded rejection, or a reply sent without strict schemas for that reason, now keeps them off ([#6092](https://github.com/bastani-inc/atomic/pull/6092)).
23
+
7
24
  ## [0.10.0] - 2026-11-20
8
25
 
9
26
  ### Added
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic-messages.d.ts","sourceRoot":"","sources":["../../src/api/anthropic-messages.ts"],"names":[],"mappings":"AAAA,OAAO,SAAkD,MAAM,mBAAmB,CAAC;AAqBnF,OAAO,KAAK,EASX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAMb,MAAM,aAAa,CAAC;AA8LrB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAgfhE,MAAM,WAAW,gBAAiB,SAAQ,aAAa;IACtD;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB;;;;;;;;;;;OAWG;IACH,eAAe,CAAC,EAAE,wBAAwB,CAAC;IAC3C;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACtE;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB;AAkOD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,oBAAoB,EAAE,gBAAgB,CAgczE,CAAC;AA+CF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,oBAAoB,EAAE,mBAAmB,CAgDlF,CAAC"}
1
+ {"version":3,"file":"anthropic-messages.d.ts","sourceRoot":"","sources":["../../src/api/anthropic-messages.ts"],"names":[],"mappings":"AAAA,OAAO,SAAkD,MAAM,mBAAmB,CAAC;AAqBnF,OAAO,KAAK,EASX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAMb,MAAM,aAAa,CAAC;AA+LrB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAgfhE,MAAM,WAAW,gBAAiB,SAAQ,aAAa;IACtD;;;;;;OAMG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB;;;;;;;;;;;OAWG;IACH,eAAe,CAAC,EAAE,wBAAwB,CAAC;IAC3C;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACtE;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB;AAkOD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,oBAAoB,EAAE,gBAAgB,CAkczE,CAAC;AA+CF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,oBAAoB,EAAE,mBAAmB,CAkDlF,CAAC"}
@@ -26,13 +26,13 @@ import { createStreamDeadline, withStreamDeadline } from "../utils/stream-deadli
26
26
  import { ToolCallArgumentsStream } from "../utils/streaming-json.js";
27
27
  import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.js";
28
28
  import { appendThinkingOffFallbackDiagnostic, getThinkingOffFallbackLevel } from "../utils/thinking-off.js";
29
- import { getCurrentTools, getInitialSystemMessage, resolveTranscript, } from "../utils/transcript.js";
29
+ import { getCurrentTools, followsToolResults, getInitialSystemMessage, resolveRequestTranscript, } from "../utils/transcript.js";
30
30
  import { convertAnthropicAssistantContent } from "./anthropic-assistant-content.js";
31
31
  import { takeAnthropicSerializedBody } from "./anthropic-serialized-body.js";
32
32
  import { ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE, getJsonSchemaToolParameters, prefersJsonSchemaStrictSampling, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
33
33
  import { buildCopilotDynamicHeaders, hasCopilotVisionInput, preserveCopilotIntegrationHeader, } from "./github-copilot-headers.js";
34
- import { adjustMaxTokensForThinking, buildBaseOptions, clampMaxTokensToContext } from "./simple-options.js";
35
- import { hasExceededStrictToolLimits, isStrictToolLimitErrorMessage, markStrictToolLimitsExceeded, } from "./strict-tool-limits.js";
34
+ import { adjustMaxTokensForThinking, buildBaseOptions, fitThinkingBudget } from "./simple-options.js";
35
+ import { isStrictToolLimitErrorMessage, markStrictToolLimitsExceeded, strictToolLimitsExceeded, } from "./strict-tool-limits.js";
36
36
  import { splitErrorToolResultImages, transformMessages } from "./transform-messages.js";
37
37
  /**
38
38
  * Resolve cache retention preference.
@@ -728,7 +728,7 @@ async function* iterateAnthropicEvents(response, signal) {
728
728
  export const stream = (model, context, options) => {
729
729
  const stream = new AssistantMessageEventStream();
730
730
  const compat = getAnthropicCompat(model);
731
- const normalizedContext = resolveTranscript(context, compat.supportsMidConvoSystemMessages);
731
+ const normalizedContext = resolveRequestTranscript(context, compat.supportsMidConvoSystemMessages);
732
732
  const currentTools = getCurrentTools(normalizedContext.messages);
733
733
  (async () => {
734
734
  const output = {
@@ -810,7 +810,7 @@ export const stream = (model, context, options) => {
810
810
  retryDeadline: options?.retryDeadline,
811
811
  signal: streamDeadline.signal,
812
812
  }, retryTransport);
813
- let disablePreferredStrictTools = hasExceededStrictToolLimits(options?.sessionId);
813
+ let disablePreferredStrictTools = strictToolLimitsExceeded(options?.sessionId, normalizedContext.messages);
814
814
  let response;
815
815
  let params;
816
816
  // The thread request that produced the response; undefined when no `thread` was sent.
@@ -824,6 +824,8 @@ export const stream = (model, context, options) => {
824
824
  if (built.providerThinkingLevel)
825
825
  output.providerThinkingLevel = built.providerThinkingLevel;
826
826
  params = built.params;
827
+ if (params.thinking?.type === "enabled")
828
+ output.thinkingBudgetTokens = params.thinking.budget_tokens;
827
829
  const nextParams = await options?.onPayload?.(params, model);
828
830
  serializedBody = takeAnthropicSerializedBody(params);
829
831
  if (nextParams !== undefined)
@@ -859,6 +861,8 @@ export const stream = (model, context, options) => {
859
861
  });
860
862
  }
861
863
  }
864
+ if (disablePreferredStrictTools)
865
+ output.strictToolsDisabled = true;
862
866
  await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
863
867
  stream.push({ type: "start", partial: output });
864
868
  streamAttempt.respond(response.headers);
@@ -1195,12 +1199,15 @@ export const streamSimple = (model, context, options) => {
1195
1199
  // Undefined means the caller did not request an output cap; let the helper use the model cap.
1196
1200
  // Do not coerce to 0 here, or the thinking budget would become the entire max_tokens value.
1197
1201
  const adjusted = adjustMaxTokensForThinking(base.maxTokens, model.maxTokens, options.reasoning, options.thinkingBudgets);
1198
- const maxTokens = clampMaxTokensToContext(model, context, adjusted.maxTokens);
1202
+ const fitted = fitThinkingBudget(model, context, adjusted.maxTokens, adjusted.thinkingBudget);
1203
+ // Too close to the context window for the smallest budget: Anthropic rejects a budget at or above max_tokens.
1204
+ if (!fitted)
1205
+ return stream(model, context, { ...base, thinkingEnabled: false });
1199
1206
  return stream(model, context, {
1200
1207
  ...base,
1201
- maxTokens,
1208
+ maxTokens: fitted.maxTokens,
1202
1209
  thinkingEnabled: true,
1203
- thinkingBudgetTokens: Math.min(adjusted.thinkingBudget, Math.max(0, maxTokens - 1024)),
1210
+ thinkingBudgetTokens: fitted.thinkingBudget,
1204
1211
  });
1205
1212
  };
1206
1213
  function isOAuthToken(apiKey) {
@@ -1526,6 +1533,8 @@ function buildParams(model, context, isOAuthToken, options, { disablePreferredSt
1526
1533
  if (allowedFallbackModels && allowedFallbackModels.length > 0) {
1527
1534
  params.fallbacks = allowedFallbackModels.map((fallback) => ({ model: fallback.model }));
1528
1535
  }
1536
+ if (converted.checkpointBlock)
1537
+ fitCheckpointInBreakpointLimit(params, converted.checkpointBlock);
1529
1538
  return { params, hasPreferredStrictTools, providerThinkingLevel: effortMessages ? activeEffort : undefined };
1530
1539
  }
1531
1540
  // Normalize tool call IDs to match Anthropic's required pattern and length
@@ -1555,6 +1564,15 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
1555
1564
  params.push(...pendingSystemMessages);
1556
1565
  pendingSystemMessages.length = 0;
1557
1566
  };
1567
+ // The input sent up to a message with a `cacheBreakpoint` hint ends with the last user or system message
1568
+ // converted so far once that message is converted. For an assistant message, that is after the held-back system
1569
+ // messages are flushed, where the request it answered ended.
1570
+ let checkpoint;
1571
+ const markCheckpoint = () => {
1572
+ const last = params.at(-1);
1573
+ if (last?.role === "user" || last?.role === "system")
1574
+ checkpoint = last;
1575
+ };
1558
1576
  for (let i = 0; i < transformedMessages.length; i++) {
1559
1577
  const msg = transformedMessages[i];
1560
1578
  if (msg.role === "system") {
@@ -1623,22 +1641,38 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
1623
1641
  }
1624
1642
  return true;
1625
1643
  });
1626
- if (filteredBlocks.length === 0)
1627
- continue;
1628
- params.push({
1629
- role: "user",
1630
- content: filteredBlocks,
1631
- });
1644
+ const last = params.at(-1);
1645
+ if (filteredBlocks.length > 0 &&
1646
+ followsToolResults(msg) &&
1647
+ last?.role === "user" &&
1648
+ Array.isArray(last.content) &&
1649
+ last.content.some((block) => block.type === "tool_result")) {
1650
+ // A prompt update after tool results: its own text blocks after the tool results, in the same message.
1651
+ last.content.push(...filteredBlocks);
1652
+ }
1653
+ else if (filteredBlocks.length > 0) {
1654
+ params.push({
1655
+ role: "user",
1656
+ content: filteredBlocks,
1657
+ });
1658
+ }
1632
1659
  }
1660
+ if (msg.cacheBreakpoint)
1661
+ markCheckpoint();
1633
1662
  }
1634
1663
  else if (msg.role === "assistant") {
1635
1664
  const blocks = convertAnthropicAssistantContent(msg, {
1636
1665
  allowEmptySignature,
1637
1666
  toolName: isOAuthToken ? toClaudeCodeName : undefined,
1638
1667
  });
1639
- if (blocks.length === 0)
1668
+ if (blocks.length === 0) {
1669
+ if (msg.cacheBreakpoint)
1670
+ markCheckpoint();
1640
1671
  continue;
1672
+ }
1641
1673
  flushPendingSystemMessages();
1674
+ if (msg.cacheBreakpoint)
1675
+ markCheckpoint();
1642
1676
  const messageIndex = params.length;
1643
1677
  params.push({
1644
1678
  role: "assistant",
@@ -1658,6 +1692,7 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
1658
1692
  // all tool results of the turn, which must come first in the user message (#10794).
1659
1693
  const errorImages = [];
1660
1694
  let j = i;
1695
+ let breakpoint = false;
1661
1696
  while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
1662
1697
  const toolMsg = transformedMessages[j];
1663
1698
  const split = splitErrorToolResultImages(toolMsg);
@@ -1665,6 +1700,8 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
1665
1700
  if (split) {
1666
1701
  errorImages.push({ type: "text", text: split.caption }, ...split.images.map(convertImageBlock));
1667
1702
  }
1703
+ if (toolMsg.cacheBreakpoint)
1704
+ breakpoint = true;
1668
1705
  j++;
1669
1706
  }
1670
1707
  // Skip the messages we've already processed.
@@ -1673,36 +1710,67 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
1673
1710
  role: "user",
1674
1711
  content: [...toolResults, ...errorImages],
1675
1712
  });
1713
+ if (breakpoint)
1714
+ markCheckpoint();
1676
1715
  }
1677
1716
  }
1678
1717
  flushPendingSystemMessages();
1679
1718
  // Add cache_control to the last user or system message to cache conversation history
1680
- if (cacheControl && params.length > 0) {
1681
- const lastMessage = params[params.length - 1];
1682
- if (lastMessage.role === "user" || lastMessage.role === "system") {
1683
- if (Array.isArray(lastMessage.content)) {
1684
- const lastBlock = lastMessage.content[lastMessage.content.length - 1];
1685
- if (lastBlock &&
1686
- (lastBlock.type === "text" ||
1687
- lastBlock.type === "image" ||
1688
- lastBlock.type === "tool_result" ||
1689
- lastBlock.type === "tool_addition" ||
1690
- lastBlock.type === "tool_removal")) {
1691
- lastBlock.cache_control = cacheControl;
1692
- }
1693
- }
1694
- else if (typeof lastMessage.content === "string") {
1695
- lastMessage.content = [
1696
- {
1697
- type: "text",
1698
- text: lastMessage.content,
1699
- cache_control: cacheControl,
1700
- },
1701
- ];
1702
- }
1703
- }
1719
+ const lastMessage = params.at(-1);
1720
+ if (cacheControl && lastMessage)
1721
+ addCacheControl(lastMessage, cacheControl);
1722
+ const checkpointBlock = cacheControl && checkpoint && checkpoint !== lastMessage ? addCacheControl(checkpoint, cacheControl) : undefined;
1723
+ return { messages: params, assistantLevels, ...(checkpointBlock ? { checkpointBlock } : {}) };
1724
+ }
1725
+ /** Anthropic rejects a request with more cache breakpoints than this. */
1726
+ const MAX_CACHE_BREAKPOINTS = 4;
1727
+ function hasCacheControl(block) {
1728
+ return typeof block === "object" && block !== null && "cache_control" in block && block.cache_control != null;
1729
+ }
1730
+ /**
1731
+ * Keeps a request with a checkpoint breakpoint within Anthropic's breakpoint limit. OAuth requests mark the short
1732
+ * identity block before the system prompt; the breakpoint after the system prompt caches everything it would, so it
1733
+ * gives way first. Otherwise the checkpoint does.
1734
+ */
1735
+ function fitCheckpointInBreakpointLimit(params, checkpointBlock) {
1736
+ const system = Array.isArray(params.system) ? params.system : [];
1737
+ const blocks = [
1738
+ ...system,
1739
+ ...(params.tools ?? []),
1740
+ ...params.messages.flatMap((message) => (Array.isArray(message.content) ? message.content : [])),
1741
+ ];
1742
+ let count = blocks.filter(hasCacheControl).length;
1743
+ if (count <= MAX_CACHE_BREAKPOINTS)
1744
+ return;
1745
+ const identity = system.length > 1 ? system[0] : undefined;
1746
+ if (identity && hasCacheControl(identity)) {
1747
+ delete identity.cache_control;
1748
+ count--;
1749
+ }
1750
+ if (count > MAX_CACHE_BREAKPOINTS)
1751
+ delete checkpointBlock.cache_control;
1752
+ }
1753
+ /** Puts `cacheControl` on the last block of a user or system message; returns that block, or undefined if it can't take one. */
1754
+ function addCacheControl(message, cacheControl) {
1755
+ if (message.role !== "user" && message.role !== "system")
1756
+ return undefined;
1757
+ if (typeof message.content === "string") {
1758
+ const block = { type: "text", text: message.content, cache_control: cacheControl };
1759
+ message.content = [block];
1760
+ return block;
1761
+ }
1762
+ const lastBlock = message.content[message.content.length - 1];
1763
+ if (lastBlock &&
1764
+ (lastBlock.type === "text" ||
1765
+ lastBlock.type === "image" ||
1766
+ lastBlock.type === "tool_result" ||
1767
+ lastBlock.type === "tool_addition" ||
1768
+ lastBlock.type === "tool_removal")) {
1769
+ const block = lastBlock;
1770
+ block.cache_control = cacheControl;
1771
+ return block;
1704
1772
  }
1705
- return { messages: params, assistantLevels };
1773
+ return undefined;
1706
1774
  }
1707
1775
  function isAnthropicEffort(value) {
1708
1776
  return value === "low" || value === "medium" || value === "high" || value === "xhigh" || value === "max";