@bastani/atomic-ai 0.10.2-alpha.1 → 0.10.3-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 (72) hide show
  1. package/CHANGELOG.md +34 -2
  2. package/dist/api/anthropic-messages.d.ts.map +1 -1
  3. package/dist/api/anthropic-messages.js +92 -16
  4. package/dist/api/anthropic-messages.js.map +1 -1
  5. package/dist/api/azure-openai-responses.d.ts.map +1 -1
  6. package/dist/api/azure-openai-responses.js +3 -1
  7. package/dist/api/azure-openai-responses.js.map +1 -1
  8. package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
  9. package/dist/api/bedrock-converse-stream.js +60 -9
  10. package/dist/api/bedrock-converse-stream.js.map +1 -1
  11. package/dist/api/google-generative-ai.d.ts.map +1 -1
  12. package/dist/api/google-generative-ai.js +3 -1
  13. package/dist/api/google-generative-ai.js.map +1 -1
  14. package/dist/api/google-vertex.d.ts.map +1 -1
  15. package/dist/api/google-vertex.js +3 -1
  16. package/dist/api/google-vertex.js.map +1 -1
  17. package/dist/api/mistral-conversations.d.ts.map +1 -1
  18. package/dist/api/mistral-conversations.js +3 -1
  19. package/dist/api/mistral-conversations.js.map +1 -1
  20. package/dist/api/openai-codex-responses.d.ts.map +1 -1
  21. package/dist/api/openai-codex-responses.js +41 -8
  22. package/dist/api/openai-codex-responses.js.map +1 -1
  23. package/dist/api/openai-completions.d.ts.map +1 -1
  24. package/dist/api/openai-completions.js +78 -14
  25. package/dist/api/openai-completions.js.map +1 -1
  26. package/dist/api/openai-responses.d.ts.map +1 -1
  27. package/dist/api/openai-responses.js +3 -1
  28. package/dist/api/openai-responses.js.map +1 -1
  29. package/dist/api/pi-messages.d.ts.map +1 -1
  30. package/dist/api/pi-messages.js +3 -1
  31. package/dist/api/pi-messages.js.map +1 -1
  32. package/dist/api/simple-options.d.ts +12 -0
  33. package/dist/api/simple-options.d.ts.map +1 -1
  34. package/dist/api/simple-options.js +52 -3
  35. package/dist/api/simple-options.js.map +1 -1
  36. package/dist/api/strict-tool-limits.d.ts +15 -6
  37. package/dist/api/strict-tool-limits.d.ts.map +1 -1
  38. package/dist/api/strict-tool-limits.js +21 -7
  39. package/dist/api/strict-tool-limits.js.map +1 -1
  40. package/dist/index.d.ts +2 -1
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +2 -1
  43. package/dist/index.js.map +1 -1
  44. package/dist/providers/data/.manifest.json +1 -1
  45. package/dist/providers/data/openrouter.json +1 -1
  46. package/dist/providers/data/vercel-ai-gateway.json +1 -1
  47. package/dist/types.d.ts +6 -0
  48. package/dist/types.d.ts.map +1 -1
  49. package/dist/types.js.map +1 -1
  50. package/dist/utils/estimate.d.ts +1 -0
  51. package/dist/utils/estimate.d.ts.map +1 -1
  52. package/dist/utils/estimate.js +1 -1
  53. package/dist/utils/estimate.js.map +1 -1
  54. package/dist/utils/provider-retry.d.ts.map +1 -1
  55. package/dist/utils/provider-retry.js +4 -0
  56. package/dist/utils/provider-retry.js.map +1 -1
  57. package/dist/utils/retry.d.ts +2 -1
  58. package/dist/utils/retry.d.ts.map +1 -1
  59. package/dist/utils/retry.js +6 -1
  60. package/dist/utils/retry.js.map +1 -1
  61. package/dist/utils/tls-certificate.d.ts +75 -0
  62. package/dist/utils/tls-certificate.d.ts.map +1 -0
  63. package/dist/utils/tls-certificate.js +343 -0
  64. package/dist/utils/tls-certificate.js.map +1 -0
  65. package/dist/utils/transcript.d.ts +8 -1
  66. package/dist/utils/transcript.d.ts.map +1 -1
  67. package/dist/utils/transcript.js +82 -7
  68. package/dist/utils/transcript.js.map +1 -1
  69. package/dist/utils/version.d.ts +1 -1
  70. package/dist/utils/version.js +1 -1
  71. package/dist/utils/version.js.map +1 -1
  72. package/package.json +3 -3
package/CHANGELOG.md CHANGED
@@ -4,6 +4,40 @@ This package is Atomic's fork of `@earendil-works/pi-ai`, published as `@bastani
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [0.10.3-alpha.1] - 2026-11-21
8
+
9
+ ### Added
10
+
11
+ - `fittedThinkingBudget()` returns the thinking budget a budget-based Claude request asks for and the budget it sends once fitted under the context window. Amazon Bedrock replies now record `thinkingBudgetTokens`, as Anthropic replies do ([#6117](https://github.com/bastani-inc/atomic/issues/6117)).
12
+ - `SystemMessage.strictToolsDisabled` records that the provider rejected the conversation's preferred strict tools for its strict-tool limits. Anthropic and Amazon Bedrock read it from the transcript before folding its system messages and send those tools without strict schemas, so a compaction that removes the replies recording the rejection can keep it ([#6119](https://github.com/bastani-inc/atomic/issues/6119)).
13
+ - `jsonSchemasEqual()` compares two JSON Schemas as `declarationsEqual()` compares tool parameters ([#6130](https://github.com/bastani-inc/atomic/issues/6130)).
14
+ - `findTlsCertificateRefusal()`, `getTlsCertificateRefusal()`, `isRefusedCertificate()`, `isTlsVerificationError()` and `recordTlsCertificateRefusal()` tell a request refused for its TLS certificate from other failures, from the failure's cause chain or a failed assistant message's `provider_tls_certificate_refused` diagnostic. `transportFetch()` and `transportFailure()` report a request's own connection failures, the only ones that count ([#6114](https://github.com/bastani-inc/atomic/issues/6114))
15
+
16
+ ### Fixed
17
+
18
+ - Fixed a turn that adds 20 or more content blocks, such as many parallel tool calls and their results, missing the prompt cache on Anthropic, Amazon Bedrock and OpenAI-compatible Claude routes: Claude looks back only 20 positions from a breakpoint. The request now also puts a breakpoint where the previous request put its newest one. OAuth requests give up the identity block's breakpoint for it, and an Anthropic message thread keeps continuing across such a request ([#6103](https://github.com/bastani-inc/atomic/issues/6103)).
19
+ - Fixed an Anthropic request with a `cacheBreakpoint` hint on tool results that later text joins, as a resumed session's prompt update and input do, putting the breakpoint after the joined text instead of after the tool results ([#6103](https://github.com/bastani-inc/atomic/issues/6103)).
20
+ - Fixed fitting a budget-based Claude thinking budget under the context window counting a tool declared again unchanged, or a removed tool, as new input on requests that declare their tools at the top ([#6117](https://github.com/bastani-inc/atomic/issues/6117)).
21
+ - `declarationsEqual()` and `getToolStateChanges()` treat the names in a schema's `required` list as a set, so a tool whose `required` names come back in another order is no longer declared again. Other arrays, such as `enum` values and tuple items, keep their order ([#6130](https://github.com/bastani-inc/atomic/issues/6130)).
22
+ - A request refused for its endpoint's TLS certificate, such as a self-signed certificate or one from a corporate TLS-inspecting proxy without `NODE_EXTRA_CA_CERTS`, is no longer retried as a network error by the provider SDK retries, the OpenAI Codex SSE retries or `retryAssistantCall()`, and OpenAI Codex no longer falls back from WebSocket to SSE for it. The failed message now names the host, the code and the runtime's reason, and how to trust a private CA, instead of `Connection error.` or `fetch failed`, on every chat adapter. Only a refusal the request's own connection reports counts: a connection reset during the handshake, a response or stream error event whose body names a certificate code, and a failure of a hook such as `onPayload` are still retried as before ([#6114](https://github.com/bastani-inc/atomic/issues/6114))
23
+
24
+ ## [0.10.2] - 2026-11-20
25
+
26
+ ### Added
27
+
28
+ - `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)).
29
+ - `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)).
30
+ - 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)).
31
+
32
+ ### Fixed
33
+
34
+ - 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)).
35
+ - 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)).
36
+ - 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)).
37
+ - 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)).
38
+ - 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)).
39
+ - 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)).
40
+
7
41
  ## [0.10.2-alpha.1] - 2026-11-20
8
42
 
9
43
  ### Added
@@ -778,7 +812,6 @@ Copilot discovery now preserves readable model names alongside exact request IDs
778
812
 
779
813
  - Treat any HTTP 413 from a provider as request-size overflow, so a GitHub Copilot request over its byte-size limit (`413 ... failed to parse request`) now compacts and retries instead of surfacing a raw API error ([pi 73f8de80](https://github.com/earendil-works/pi/commit/73f8de80bc87021375f8b525292ac07492253002))
780
814
 
781
-
782
815
  ## [0.9.38] - 2026-10-18
783
816
 
784
817
  ### Breaking Changes
@@ -1556,7 +1589,6 @@ Cumulative release of the `0.9.18-alpha.5` through `0.9.18-alpha.7` prereleases.
1556
1589
  - Fixed Anthropic OAuth requests for Claude Fable 5.1 being rejected with a 400 `claude_code_version_too_old`. The OAuth path impersonates Claude Code, and Anthropic gates newer models on the `claude-cli/<version>` user agent alone; the pinned `2.1.75` predated Fable 5.1's floor, so every OAuth turn for that model failed with "version 2.1.251 or newer is required" regardless of the betas or other identity headers sent. The advertised version is now `2.1.251`, the exact published minimum the API accepts, bisected against the live endpoint rather than read off the error text. It is a strict capability superset of the old value: every Claude model this package ships answers normally at the new version. API-key requests are unaffected, since only the OAuth branch sends a Claude Code user agent.
1557
1590
  - Fixed a `fallback` content block corrupting the content indices that follow it when a response is replayed through the faux provider. The block was skipped without occupying its slot in the streamed `partial.content`, so every later block was addressed one position short: a `[text, fallback, text]` message aborted the stream with an error and lost its second text block, and a tool call after a fallback was announced at an index holding something else. The marker now occupies its slot and simply receives no stream event, matching how the real Anthropic path already records it, so `contentIndex` continues to address `partial.content` directly for consumers that index it.
1558
1591
 
1559
-
1560
1592
  ## [0.9.16] - 2026-08-29
1561
1593
 
1562
1594
  Cumulative release of the `0.9.16-alpha.5` – `0.9.16-alpha.11` prereleases. The summary below covers the user-visible outcome of that work; the per-change detail remains in the prerelease sections below.
@@ -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;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"}
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;AAgMrB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAkgBhE,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;AAmOD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,oBAAoB,EAAE,gBAAgB,CAmczE,CAAC;AA+CF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,oBAAoB,EAAE,mBAAmB,CAkDlF,CAAC"}
@@ -8,6 +8,7 @@ import { raceWithAbortSignal } from "../utils/abort.js";
8
8
  import { appendAssistantMessageDiagnostic } from "../utils/diagnostics.js";
9
9
  import { assertSupportedDocumentMimeType } from "../utils/document-input.js";
10
10
  import { appendProviderResponseFailureDiagnostic, withCauseDetail } from "../utils/error-body.js";
11
+ import { recordTlsCertificateRefusal, transportFetch } from "../utils/tls-certificate.js";
11
12
  import { AssistantMessageEventStream } from "../utils/event-stream.js";
12
13
  import { shortHash } from "../utils/hash.js";
13
14
  import { headersToRecord } from "../utils/headers.js";
@@ -406,7 +407,7 @@ function hashThreadPrefix(model, params, messages) {
406
407
  model.provider,
407
408
  model.baseUrl,
408
409
  params.model,
409
- system,
410
+ normalizeThreadSystem(system),
410
411
  tools,
411
412
  thinking,
412
413
  output_config,
@@ -428,6 +429,24 @@ function hashThreadPrefix(model, params, messages) {
428
429
  * next request sends the same message as a string: "hi" vs [{ type: "text", text: "hi" }]. The
429
430
  * same holds for a tool result's content.
430
431
  */
432
+ /** The identity blocks OAuth requests send before the system prompt. */
433
+ const oauthIdentityBlocks = new WeakSet();
434
+ /**
435
+ * OAuth requests send the identity block before the system prompt. Its breakpoint gives way when a request needs one
436
+ * more breakpoint in its messages (see `fitCheckpointInBreakpointLimit()`); while the system prompt after it keeps the
437
+ * same breakpoint, both prompts mean the same, so the thread continues either way.
438
+ */
439
+ function normalizeThreadSystem(system) {
440
+ if (!Array.isArray(system) || system.length < 2)
441
+ return system;
442
+ const [identity, prompt] = system;
443
+ if (!identity || !oauthIdentityBlocks.has(identity) || !prompt?.cache_control)
444
+ return system;
445
+ if (identity.cache_control && JSON.stringify(identity.cache_control) !== JSON.stringify(prompt.cache_control))
446
+ return system;
447
+ const { cache_control: _cacheControl, ...rest } = identity;
448
+ return [rest, ...system.slice(1)];
449
+ }
431
450
  function normalizeThreadMessage(message) {
432
451
  if (message === null || typeof message !== "object")
433
452
  return message;
@@ -585,7 +604,8 @@ class AtomicAnthropic extends Anthropic {
585
604
  };
586
605
  super({
587
606
  ...options,
588
- fetch: admission?.fetch ?? options.fetch,
607
+ // Only this client's own requests can be refused a certificate (#6114).
608
+ fetch: transportFetch(admission?.fetch ?? options.fetch),
589
609
  middleware: admission ? [tokenAdmission, ...(options.middleware ?? [])] : options.middleware,
590
610
  });
591
611
  this.admission = admission;
@@ -810,7 +830,7 @@ export const stream = (model, context, options) => {
810
830
  retryDeadline: options?.retryDeadline,
811
831
  signal: streamDeadline.signal,
812
832
  }, retryTransport);
813
- let disablePreferredStrictTools = strictToolLimitsExceeded(options?.sessionId, normalizedContext.messages);
833
+ let disablePreferredStrictTools = strictToolLimitsExceeded(options?.sessionId, context.messages);
814
834
  let response;
815
835
  let params;
816
836
  // The thread request that produced the response; undefined when no `thread` was sent.
@@ -1126,6 +1146,7 @@ export const stream = (model, context, options) => {
1126
1146
  output.stopReason = options?.signal?.aborted || error instanceof RetryDeadlineError ? "aborted" : "error";
1127
1147
  output.errorMessage = error instanceof Error ? withCauseDetail(error) : JSON.stringify(error);
1128
1148
  appendProviderResponseFailureDiagnostic(output, error);
1149
+ recordTlsCertificateRefusal(output, error);
1129
1150
  output.retryAfterMs = error instanceof Error ? getProviderRetryAfterMs(error) : undefined;
1130
1151
  if (model.provider === "github-copilot")
1131
1152
  recordGitHubCopilotFailure(output, error, streamAttempt.snapshot());
@@ -1396,6 +1417,7 @@ function buildParams(model, context, isOAuthToken, options, { disablePreferredSt
1396
1417
  ...(cacheControl ? { cache_control: cacheControl } : {}),
1397
1418
  },
1398
1419
  ];
1420
+ oauthIdentityBlocks.add(params.system[0]);
1399
1421
  if (initialSystemText) {
1400
1422
  params.system.push({
1401
1423
  type: "text",
@@ -1566,12 +1588,20 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
1566
1588
  };
1567
1589
  // The input sent up to a message with a `cacheBreakpoint` hint ends with the last user or system message
1568
1590
  // 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.
1591
+ // messages are flushed, where the request it answered ended. The block count keeps that end when later blocks
1592
+ // join the same message.
1570
1593
  let checkpoint;
1594
+ // Where the request the last sent reply answered ended: its newest input got that request's last breakpoint.
1595
+ let replyCut;
1596
+ const inputCut = () => {
1597
+ const index = params.length - 1;
1598
+ const last = params[index];
1599
+ return last?.role === "user" || last?.role === "system"
1600
+ ? { message: last, index, blocks: blockCount(last) }
1601
+ : undefined;
1602
+ };
1571
1603
  const markCheckpoint = () => {
1572
- const last = params.at(-1);
1573
- if (last?.role === "user" || last?.role === "system")
1574
- checkpoint = last;
1604
+ checkpoint = inputCut() ?? checkpoint;
1575
1605
  };
1576
1606
  for (let i = 0; i < transformedMessages.length; i++) {
1577
1607
  const msg = transformedMessages[i];
@@ -1673,6 +1703,7 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
1673
1703
  flushPendingSystemMessages();
1674
1704
  if (msg.cacheBreakpoint)
1675
1705
  markCheckpoint();
1706
+ replyCut = inputCut();
1676
1707
  const messageIndex = params.length;
1677
1708
  params.push({
1678
1709
  role: "assistant",
@@ -1717,11 +1748,54 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
1717
1748
  flushPendingSystemMessages();
1718
1749
  // Add cache_control to the last user or system message to cache conversation history
1719
1750
  const lastMessage = params.at(-1);
1720
- if (cacheControl && lastMessage)
1721
- addCacheControl(lastMessage, cacheControl);
1722
- const checkpointBlock = cacheControl && checkpoint && checkpoint !== lastMessage ? addCacheControl(checkpoint, cacheControl) : undefined;
1751
+ const tailBlock = cacheControl && lastMessage ? addCacheControl(lastMessage, cacheControl) : undefined;
1752
+ let checkpointBlock;
1753
+ if (cacheControl && checkpoint) {
1754
+ checkpointBlock = addCacheControlAtCut(checkpoint, lastMessage, cacheControl);
1755
+ }
1756
+ else if (cacheControl &&
1757
+ tailBlock &&
1758
+ replyCut &&
1759
+ positionsAfterCut(params, replyCut, assistantLevels) >= CACHE_LOOKBACK_POSITIONS) {
1760
+ // One turn added so many blocks that the newest breakpoint no longer reaches back to the previous request's.
1761
+ checkpointBlock = addCacheControlAtCut(replyCut, lastMessage, cacheControl);
1762
+ }
1723
1763
  return { messages: params, assistantLevels, ...(checkpointBlock ? { checkpointBlock } : {}) };
1724
1764
  }
1765
+ function blockCount(message) {
1766
+ return typeof message.content === "string" ? 1 : message.content.length;
1767
+ }
1768
+ /**
1769
+ * Claude finds an earlier request's cached prefix from a breakpoint only within this many positions, the breakpoint
1770
+ * included. Every block counts as one position here, as on Bedrock; the Claude API also counts a run of tool calls
1771
+ * or tool results as one, so this never counts fewer positions than the provider does.
1772
+ */
1773
+ const CACHE_LOOKBACK_POSITIONS = 20;
1774
+ /**
1775
+ * Positions after the last block of `cut` through the last block of the request, counted until they reach the
1776
+ * lookback: blocks, and the effort marker the request sends before each assistant message with a recorded level.
1777
+ */
1778
+ function positionsAfterCut(messages, cut, assistantLevels) {
1779
+ let positions = blockCount(cut.message) - cut.blocks;
1780
+ for (let index = cut.index + 1; index < messages.length && positions < CACHE_LOOKBACK_POSITIONS; index++) {
1781
+ positions += blockCount(messages[index]) + (assistantLevels.has(index) ? 1 : 0);
1782
+ }
1783
+ return positions;
1784
+ }
1785
+ /**
1786
+ * Puts `cacheControl` on the last block `cut` sent, where that request's last breakpoint was, unless it is the last
1787
+ * block of `lastMessage`, which has one already. Returns the block, or undefined if it can't take one.
1788
+ */
1789
+ function addCacheControlAtCut(cut, lastMessage, cacheControl) {
1790
+ const { message, blocks } = cut;
1791
+ if (message === lastMessage && blocks === blockCount(message))
1792
+ return undefined;
1793
+ if (typeof message.content === "string" || blocks === message.content.length) {
1794
+ return addCacheControl(message, cacheControl);
1795
+ }
1796
+ const block = message.content[blocks - 1];
1797
+ return block && acceptsCacheControl(block) ? Object.assign(block, { cache_control: cacheControl }) : undefined;
1798
+ }
1725
1799
  /** Anthropic rejects a request with more cache breakpoints than this. */
1726
1800
  const MAX_CACHE_BREAKPOINTS = 4;
1727
1801
  function hasCacheControl(block) {
@@ -1760,18 +1834,20 @@ function addCacheControl(message, cacheControl) {
1760
1834
  return block;
1761
1835
  }
1762
1836
  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")) {
1837
+ if (lastBlock && acceptsCacheControl(lastBlock)) {
1769
1838
  const block = lastBlock;
1770
1839
  block.cache_control = cacheControl;
1771
1840
  return block;
1772
1841
  }
1773
1842
  return undefined;
1774
1843
  }
1844
+ function acceptsCacheControl(block) {
1845
+ return (block.type === "text" ||
1846
+ block.type === "image" ||
1847
+ block.type === "tool_result" ||
1848
+ block.type === "tool_addition" ||
1849
+ block.type === "tool_removal");
1850
+ }
1775
1851
  function isAnthropicEffort(value) {
1776
1852
  return value === "low" || value === "medium" || value === "high" || value === "xhigh" || value === "max";
1777
1853
  }