@openclaw/ai 2026.10.1-beta.2 → 2026.10.5-beta.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 (104) hide show
  1. package/README.md +6 -0
  2. package/dist/{anthropic-CZv6XFER.mjs → anthropic-BAHKgWam.mjs} +30 -59
  3. package/dist/{anthropic-payload-policy-BhCcgNdm.d.mts → anthropic-payload-policy-DGblkHL5.d.mts} +19 -16
  4. package/dist/{anthropic-stream-reducer-BQ1tjd7x.mjs → anthropic-stream-reducer-g6w1YVii.mjs} +197 -285
  5. package/dist/{api-registry-DpU0Rj-I.d.mts → api-registry-BhXXYdRA.d.mts} +2 -2
  6. package/dist/assistant-message-events-CHMJgbhs.mjs +18 -0
  7. package/dist/{assistant-output-OLm-Oln0.mjs → assistant-output-CUEeSotf.mjs} +2 -2
  8. package/dist/{assistant-text-phase-Bl1aDCB7.mjs → assistant-text-phase-BrV8KdFO.mjs} +7 -6
  9. package/dist/{azure-openai-responses-DH7bd3QT.mjs → azure-openai-responses-C7qBuR23.mjs} +8 -9
  10. package/dist/{credential-redaction-BKv49aiv.d.mts → credential-redaction-BHxruEH6.d.mts} +3 -1
  11. package/dist/{diagnostics-Caj-4RBA.mjs → diagnostics-tQasYct1.mjs} +10 -3
  12. package/dist/diagnostics.d.mts +7 -7
  13. package/dist/diagnostics.mjs +3 -3
  14. package/dist/{env-api-keys-CyX4VKGO.mjs → env-api-keys-CrxwdylR.mjs} +24 -35
  15. package/dist/event-stream-6UNs-AhN.d.mts +1 -0
  16. package/dist/{event-stream-DrRM_90c.d.mts → event-stream-BcqWLo_u.d.mts} +1 -1
  17. package/dist/{event-stream-C5AbCAaZ.mjs → event-stream-X2gmsUWv.mjs} +5 -8
  18. package/dist/event-stream.d.mts +2 -2
  19. package/dist/event-stream.mjs +1 -1
  20. package/dist/google-Boz2QcpK.mjs +20 -0
  21. package/dist/{google-interactions-wrnE6MLw.mjs → google-interactions-tjAb1mc5.mjs} +52 -84
  22. package/dist/{google-messages-BwPPycw6.mjs → google-messages-C-RZzloH.mjs} +25 -35
  23. package/dist/google-provider-stream-CMImyxbX.mjs +58 -0
  24. package/dist/{google-shared-Eql1OMPU.mjs → google-shared-OyAtMTZv.mjs} +6 -15
  25. package/dist/google-vertex-hCxTJea7.mjs +40 -0
  26. package/dist/{host-BinBeGcQ.d.mts → host-CTDz7LXD.d.mts} +6 -3
  27. package/dist/{host-DCW52iHe.mjs → host-JWXZWa-c.mjs} +12 -8
  28. package/dist/{host-policy-B3jc4Rpn.mjs → host-policy-q4Azym9F.mjs} +1 -1
  29. package/dist/{index-a8e_oKnu.d.mts → index-BYDvopUB.d.mts} +27 -6
  30. package/dist/index.d.mts +7 -7
  31. package/dist/index.mjs +7 -6
  32. package/dist/internal/anthropic.d.mts +6 -6
  33. package/dist/internal/anthropic.mjs +5 -5
  34. package/dist/internal/openai-completions-compat.d.mts +1 -1
  35. package/dist/internal/openai-completions-compat.mjs +1 -1
  36. package/dist/internal/openai-responses-payload-policy.d.mts +8 -1
  37. package/dist/internal/openai-responses-payload-policy.mjs +3 -3
  38. package/dist/internal/openai.d.mts +9 -9
  39. package/dist/internal/openai.mjs +9 -11
  40. package/dist/internal/runtime.d.mts +14 -11
  41. package/dist/internal/runtime.mjs +10 -13
  42. package/dist/internal/shared.d.mts +5 -5
  43. package/dist/internal/shared.mjs +6 -7
  44. package/dist/internal/tool-schema.mjs +2 -2
  45. package/dist/{llm-request-activity-D8f_oxHj.mjs → llm-request-activity-CfMuh5cC.mjs} +2 -2
  46. package/dist/{mistral-DSt4vQAO.mjs → mistral-DUveP4hF.mjs} +71 -146
  47. package/dist/{model-transport-url-C5f59L3Y.d.mts → model-transport-url-BrsjjdjR.d.mts} +5 -1
  48. package/dist/{number-coercion-V8-BhME4.mjs → number-coercion-C3hztWoY.mjs} +5 -4
  49. package/dist/{openai-chatgpt-responses-BC2x0Oqx.mjs → openai-chatgpt-responses-OCwckGhY.mjs} +173 -242
  50. package/dist/{openai-completions-DNyHnXKg.mjs → openai-completions-DOltBUe9.mjs} +7 -6
  51. package/dist/{openai-completions-compat-BYdYpXhU.d.mts → openai-completions-compat-i-nLZxFy.d.mts} +10 -11
  52. package/dist/{openai-completions-compat-BdmlhD4u.mjs → openai-completions-compat-pg1q4uNG.mjs} +10 -6
  53. package/dist/openai-completions-transport-OMrn7gnZ.mjs +3319 -0
  54. package/dist/{openai-prompt-cache-I8YQTzf8.mjs → openai-prompt-cache-Cr2xKVrS.mjs} +1 -1
  55. package/dist/{openai-prompt-cache-45-AH_xv.d.mts → openai-prompt-cache-DIPMrMFy.d.mts} +2 -2
  56. package/dist/{openai-responses-uZ5oWSp7.mjs → openai-responses-BSBJUA19.mjs} +9 -10
  57. package/dist/{openai-responses-compaction-replay-BClApUIf.d.mts → openai-responses-compaction-replay-B2FE4Nw8.d.mts} +4 -4
  58. package/dist/{openai-responses-contracts-Br5IIw_h.d.mts → openai-responses-contracts-DKqMJ1Ct.d.mts} +9 -4
  59. package/dist/{openai-responses-contracts-BKu8O7l4.mjs → openai-responses-contracts-DlKMMTxK.mjs} +26 -6
  60. package/dist/{openai-responses-compaction-window-m878aVpU.mjs → openai-responses-endpoint-CinGh4VZ.mjs} +14 -4
  61. package/dist/{openai-responses-prompt-observer-internal-CCg7B6l4.mjs → openai-responses-request-lifecycle-D2IFiI0-.mjs} +39 -4
  62. package/dist/{openai-responses-shared-C7oVyjED.mjs → openai-responses-shared-DVGhq5xF.mjs} +1036 -327
  63. package/dist/{openai-responses-terminal-usage-DgImcyJu.d.mts → openai-responses-terminal-usage-BygjiAdi.d.mts} +2 -2
  64. package/dist/{openai-tool-schema-DYNePiF8.mjs → openai-tool-schema-DDPXsvJP.mjs} +236 -399
  65. package/dist/{positive-integer-CwLi8LUL.mjs → positive-integer-CqWjqSi9.mjs} +1 -1
  66. package/dist/{provider-error-DQBE1bUQ.mjs → provider-error-CF9GWCs2.mjs} +5 -3
  67. package/dist/{provider-options-D5ABCV_l.d.mts → provider-options-CVrXZBtA.d.mts} +3 -2
  68. package/dist/{provider-transport-turn-state-_M2kLdUE.mjs → provider-transport-turn-state-LuCaTsSO.mjs} +2 -2
  69. package/dist/{provider-types-F8Mqc9Ma.d.mts → provider-types-CHtRx0dI.d.mts} +3 -3
  70. package/dist/provider-types.d.mts +6 -6
  71. package/dist/providers.d.mts +2 -2
  72. package/dist/providers.mjs +12 -12
  73. package/dist/{reasoning-tag-text-partitioner-Ju9VpUGl.mjs → reasoning-tag-text-partitioner-0-Rsezog.mjs} +13 -12
  74. package/dist/{required-api-key-D4kn3252.mjs → required-api-key-CFJvcFy_.mjs} +1 -1
  75. package/dist/{simple-options-BkKTXxhp.mjs → simple-options-BARFAzk9.mjs} +9 -11
  76. package/dist/{src-B0NIt2UK.mjs → src-C_wfIUmV.mjs} +35 -10
  77. package/dist/{stream-DSdzKsDK.mjs → stream-DwrETW0_.mjs} +12 -21
  78. package/dist/{stream-first-event-timeout-D9ZmL_s9.mjs → stream-first-event-timeout-Dc42hKv6.mjs} +8 -21
  79. package/dist/{streaming-byte-guard-CC-HMn_u.mjs → streaming-byte-guard-DBXr1XYn.mjs} +16 -1
  80. package/dist/{string-normalization-PN10hf6d.mjs → string-normalization-I3ux50s9.mjs} +1 -1
  81. package/dist/{text-signature-MLwfi6fq.mjs → text-signature-B_MB6U_9.mjs} +32 -5
  82. package/dist/{tool-schema-json-projection-Ct8SeBeJ.mjs → tool-schema-json-projection-B8sG1I9v.mjs} +29 -23
  83. package/dist/{transport-stream-shared-BcAKh2XH.d.mts → transport-stream-shared-BmpzDtdQ.d.mts} +4 -4
  84. package/dist/{transport-stream-shared-CxXXz_Dd.mjs → transport-stream-shared-DH08b7Il.mjs} +31 -20
  85. package/dist/transports.d.mts +33 -22
  86. package/dist/transports.mjs +73 -1234
  87. package/dist/{types-DvzmWW8J.d.mts → types-Bd2T86VJ.d.mts} +5 -23
  88. package/dist/types-Kdfw_04V.d.mts +1 -0
  89. package/dist/types.d.mts +6 -6
  90. package/dist/types.mjs +5 -4
  91. package/dist/{validation-B3lQt_VE.d.mts → validation-DdGB1yy3.d.mts} +1 -1
  92. package/dist/{validation-DbqlIfDn.mjs → validation-opC2pjk9.mjs} +2 -3
  93. package/dist/validation.d.mts +1 -1
  94. package/dist/validation.mjs +1 -1
  95. package/package.json +4 -4
  96. package/dist/event-stream-DqDQWqiy.d.mts +0 -1
  97. package/dist/google-1WLOtb8w.mjs +0 -54
  98. package/dist/google-vertex-0LrlRoO3.mjs +0 -88
  99. package/dist/openai-completions-transport-DWM2mQDB.mjs +0 -2081
  100. package/dist/openai-responses-request-lifecycle-Fsag-IGZ.mjs +0 -36
  101. package/dist/openai-transport-params-JFsnIyto.mjs +0 -735
  102. package/dist/openai-web-search-tools-Cfu0uLyN.mjs +0 -14
  103. package/dist/provider-replay-context-C7Bmi84a.mjs +0 -25
  104. package/dist/types-BuiEBM3u.d.mts +0 -1
@@ -1,33 +1,28 @@
1
- import { E as supportsClaudeNativeXhighEffort, S as supportsClaudeAdaptiveThinking, _ as resolveClaudeOpus55ModelIdentity, b as resolveClaudeSonnet5ModelIdentity, l as bindsClaudeThinkingPrefix, m as resolveClaudeModelIdentity, p as resolveClaudeFable5ModelIdentity, u as requiresClaudeBetweenToolsThinking, v as resolveClaudeOpus5ModelIdentity, w as supportsClaudeInHistorySystemMessages, y as resolveClaudeSonnet55ModelIdentity } from "./src-B0NIt2UK.mjs";
1
+ import { E as supportsClaudeInHistorySystemMessages, O as supportsClaudeNativeXhighEffort, S as resolveClaudeSonnet5ModelIdentity, b as resolveClaudeOpus5ModelIdentity, d as requiresClaudeBetweenToolsThinking, g as resolveClaudeModelIdentity, h as resolveClaudeHaiku55ModelIdentity, m as resolveClaudeFable5ModelIdentity, u as bindsClaudeThinkingPrefix, w as supportsClaudeAdaptiveThinking, x as resolveClaudeSonnet55ModelIdentity, y as resolveClaudeOpus55ModelIdentity } from "./src-C_wfIUmV.mjs";
2
2
  import { f as hasRuntimeContextMarker } from "./types-CQc6JNG4.mjs";
3
- import { r as appendAssistantThinking } from "./event-stream-C5AbCAaZ.mjs";
3
+ import { r as appendAssistantThinking } from "./event-stream-X2gmsUWv.mjs";
4
4
  import { a as asRecord, o as isRecord, r as asOptionalObjectRecord, s as readStringField } from "./record-coerce-CNDovvft.mjs";
5
5
  import { i as normalizeOptionalLowercaseString, r as normalizeNullableString } from "./string-coerce-CtgOVOSv.mjs";
6
- import { n as asNonNegativeFiniteNumber } from "./number-coercion-V8-BhME4.mjs";
7
- import { N as isImageWithMediaPayload, O as calculateCost, S as resolveAnthropicThinkingEffort, g as applyClaudeRequestContract, h as ANTHROPIC_CLAUDE_CODE_VERSION, r as getAiTransportHost, u as STREAM_ERROR_FALLBACK_TEXT, v as defaultsClaudeAdaptiveThinking, w as usesClaudeFable5MessagesContract, x as requiresClaudeAdaptiveThinking, y as mapAnthropicStopReason } from "./host-DCW52iHe.mjs";
8
- import "./text-signature-MLwfi6fq.mjs";
6
+ import { n as asNonNegativeFiniteNumber } from "./number-coercion-C3hztWoY.mjs";
7
+ import { N as isImageWithMediaPayload, O as calculateCost, S as resolveAnthropicThinkingEffort, g as applyClaudeRequestContract, h as ANTHROPIC_CLAUDE_CODE_VERSION, r as getAiTransportHost, u as STREAM_ERROR_FALLBACK_TEXT, v as defaultsClaudeAdaptiveThinking, w as usesClaudeFable5MessagesContract, x as requiresClaudeAdaptiveThinking, y as mapAnthropicStopReason } from "./host-JWXZWa-c.mjs";
8
+ import { i as providerReplayContextMatches, n as buildProviderReplayContext, r as isProviderReplayContext } from "./text-signature-B_MB6U_9.mjs";
9
9
  import { n as estimateBase64DecodedBytes } from "./base64-C8CdcsjU.mjs";
10
- import { a as resolveProviderRequestCapabilities, i as resolveProviderEndpoint } from "./host-policy-B3jc4Rpn.mjs";
10
+ import { a as resolveProviderRequestCapabilities, i as resolveProviderEndpoint } from "./host-policy-q4Azym9F.mjs";
11
11
  import { i as resolveCacheRetention } from "./cloudflare-5qSGvIUH.mjs";
12
- import { C as parseJsonObjectPreservingUnsafeIntegers, E as createToolArgumentPreviewSchedule, O as parseStreamingJson, P as sanitizeSurrogates, _ as sanitizeNonEmptyTransportPayloadText, b as transportAbortError, d as iterateModelStream, i as copyProviderAcceptanceObserver, l as finalizeTerminalToolCallArguments, r as coerceTransportToolCallArguments, v as sanitizeTransportPayloadText } from "./transport-stream-shared-CxXXz_Dd.mjs";
13
- import { C as stripSystemPromptCacheBoundary, D as sortPromptCacheToolsByName, d as describeToolResultMediaPlaceholder, m as extractToolResultText, p as extractToolResultBlockText, t as adjustMaxTokensForThinking, w as stripSystemPromptRelocatableBoundary, x as splitSystemPromptCacheBoundary } from "./simple-options-BkKTXxhp.mjs";
14
- import { t as parsePositiveInteger } from "./positive-integer-CwLi8LUL.mjs";
15
- import { n as isProviderReplayContext, r as providerReplayContextMatches, t as buildProviderReplayContext } from "./provider-replay-context-C7Bmi84a.mjs";
16
- import { n as transformProviderMessages } from "./assistant-output-OLm-Oln0.mjs";
17
- import { i as SCHEMA_MAP_KEYS, n as projectRuntimeToolInputSchema } from "./tool-schema-json-projection-Ct8SeBeJ.mjs";
18
- import { i as tagPendingCommentaryText } from "./assistant-text-phase-Bl1aDCB7.mjs";
19
- import { t as notifyLlmRequestActivity } from "./llm-request-activity-D8f_oxHj.mjs";
12
+ import { C as parseJsonObjectPreservingUnsafeIntegers, E as createToolArgumentPreviewSchedule, O as parseStreamingJson, P as sanitizeSurrogates, b as transportAbortError, d as iterateModelStream, i as copyProviderAcceptanceObserver, l as finalizeTerminalToolCallArguments, r as coerceTransportToolCallArguments, v as sanitizeTransportPayloadText } from "./transport-stream-shared-DH08b7Il.mjs";
13
+ import { C as stripSystemPromptCacheBoundary, D as sortPromptCacheToolsByName, d as describeToolResultMediaPlaceholder, m as extractToolResultText, p as extractToolResultBlockText, t as adjustMaxTokensForThinking, w as stripSystemPromptRelocatableBoundary, x as splitSystemPromptCacheBoundary } from "./simple-options-BARFAzk9.mjs";
14
+ import { t as parsePositiveInteger } from "./positive-integer-CqWjqSi9.mjs";
15
+ import { n as transformProviderMessages } from "./assistant-output-CUEeSotf.mjs";
16
+ import { i as SCHEMA_MAP_KEYS, n as projectRuntimeToolInputSchema } from "./tool-schema-json-projection-B8sG1I9v.mjs";
17
+ import { i as tagPendingCommentaryText } from "./assistant-text-phase-BrV8KdFO.mjs";
18
+ import { t as notifyLlmRequestActivity } from "./llm-request-activity-CfMuh5cC.mjs";
20
19
  //#region packages/ai/src/providers/anthropic-auth-headers.ts
21
20
  /** Inspect resolved credential shape for auth routing without exposing its value. */
22
21
  function isAnthropicOAuthApiKey(apiKey) {
23
22
  return typeof apiKey === "string" && getAiTransportHost().resolveSecretSentinel(apiKey).includes("sk-ant-oat");
24
23
  }
25
24
  function usesFoundryBearerAuth(model) {
26
- return model.provider === "microsoft-foundry" && (model.authHeader === true || hasBearerAuthorizationHeader(model.headers));
27
- }
28
- function hasBearerAuthorizationHeader(headers) {
29
- if (!headers) return false;
30
- return Object.entries(headers).some(([key, value]) => key.toLowerCase() === "authorization" && /^bearer\s+\S+/i.test(value.trim()));
25
+ return model.provider === "microsoft-foundry" && (model.authHeader === true || Object.entries(model.headers ?? {}).some(([key, value]) => key.toLowerCase() === "authorization" && /^bearer\s+\S+/i.test(value.trim())));
31
26
  }
32
27
  function omitFoundryBearerCredentialHeaders(headers) {
33
28
  if (!headers) return;
@@ -148,7 +143,7 @@ function isDirectAnthropicModel(model, env = process.env) {
148
143
  baseUrl
149
144
  };
150
145
  const endpointClass = resolveProviderEndpoint(endpointModel).endpointClass;
151
- return normalizeOptionalLowercaseString(model.provider) === "anthropic" && (endpointClass === "anthropic-public" || endpointClass === "default" && (!baseUrl || resolveBaseUrlHostname(baseUrl) === "api.anthropic.com"));
146
+ return normalizeOptionalLowercaseString(model.provider) === "anthropic" && (endpointClass === "anthropic-public" || endpointClass === "default" && (!baseUrl || URL.parse(baseUrl)?.hostname === "api.anthropic.com"));
152
147
  }
153
148
  function supportsAnthropicServerSideFallback(model) {
154
149
  return (usesClaudeFable5MessagesContract(model) || resolveClaudeOpus5ModelIdentity(model) !== void 0 || resolveClaudeSonnet55ModelIdentity(model) !== void 0) && isDirectAnthropicModel(model);
@@ -157,12 +152,9 @@ function isAnthropicServerToolClearingEnabled(model, apiKey) {
157
152
  const resolvedApiKey = apiKey && getAiTransportHost().resolveSecretSentinel(apiKey);
158
153
  return Boolean(resolvedApiKey?.trim()) && !isAnthropicOAuthApiKey(resolvedApiKey) && normalizeOptionalLowercaseString(model.api) === "anthropic-messages" && isDirectAnthropicModel(model);
159
154
  }
160
- function resolveBaseUrlHostname(baseUrl) {
161
- return URL.parse(baseUrl)?.hostname;
162
- }
163
155
  function isLongTtlEligibleEndpoint(baseUrl) {
164
156
  if (typeof baseUrl !== "string") return false;
165
- const hostname = resolveBaseUrlHostname(baseUrl);
157
+ const hostname = URL.parse(baseUrl)?.hostname;
166
158
  if (!hostname) return false;
167
159
  return hostname === "api.anthropic.com" || hostname === "aiplatform.googleapis.com" || hostname === "aiplatform.us.rep.googleapis.com" || hostname === "aiplatform.eu.rep.googleapis.com" || hostname.endsWith("-aiplatform.googleapis.com");
168
160
  }
@@ -189,8 +181,7 @@ function buildAnthropicSystemBlocks(systemPrompt, isOAuthToken, cacheControl, cl
189
181
  type: "text",
190
182
  text: sanitizeSurrogates(systemPrompt)
191
183
  }] : [];
192
- if (cacheControl) applyAnthropicCacheControlToSystem(blocks, cacheControl);
193
- else stripAnthropicSystemPromptBoundary(blocks);
184
+ normalizeAnthropicSystemBlocks(blocks, cacheControl);
194
185
  if (systemPrompt && blocks.length === 0) blocks.push({
195
186
  type: "text",
196
187
  text: ""
@@ -218,7 +209,7 @@ function applyAnthropicRequestCacheControl(payload, cacheControl, supportsCacheC
218
209
  const usedMarkers = countAnthropicCacheControlMarkers(payload.system) + countAnthropicCacheControlMarkers(payload.tools);
219
210
  applyAnthropicCacheControlToMessages(payload.messages, cacheControl, ANTHROPIC_CACHE_CONTROL_LIMIT - usedMarkers, cacheBreakpointOptOutMessageIndexes);
220
211
  }
221
- function applyAnthropicCacheControlToSystem(system, cacheControl) {
212
+ function normalizeAnthropicSystemBlocks(system, cacheControl) {
222
213
  if (!Array.isArray(system)) return;
223
214
  const normalizedBlocks = [];
224
215
  for (const block of system) {
@@ -232,6 +223,10 @@ function applyAnthropicCacheControlToSystem(system, cacheControl) {
232
223
  normalizedBlocks.push(block);
233
224
  continue;
234
225
  }
226
+ if (!cacheControl) {
227
+ record.text = stripSystemPromptCacheBoundary(blockText);
228
+ continue;
229
+ }
235
230
  const text = stripSystemPromptRelocatableBoundary(blockText);
236
231
  record.text = text;
237
232
  const split = splitSystemPromptCacheBoundary(text);
@@ -251,15 +246,7 @@ function applyAnthropicCacheControlToSystem(system, cacheControl) {
251
246
  text: split.dynamicSuffix
252
247
  });
253
248
  }
254
- system.splice(0, system.length, ...normalizedBlocks);
255
- }
256
- function stripAnthropicSystemPromptBoundary(system) {
257
- if (!Array.isArray(system)) return;
258
- for (const block of system) {
259
- if (!block || typeof block !== "object") continue;
260
- const record = block;
261
- if (record.type === "text" && typeof record.text === "string") record.text = stripSystemPromptCacheBoundary(record.text);
262
- }
249
+ if (cacheControl) system.splice(0, system.length, ...normalizedBlocks);
263
250
  }
264
251
  /** Apply one shared deepest-stable-message cache breakpoint policy. */
265
252
  function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimit, cacheBreakpointOptOutMessageIndexes) {
@@ -271,22 +258,13 @@ function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimi
271
258
  const record = message;
272
259
  if (record.role !== "user" || cacheBreakpointOptOutMessageIndexes.has(i)) continue;
273
260
  const content = record.content;
274
- if (typeof content === "string") {
275
- if (fallbackToolResult && markerLimit === 1) {
276
- fallbackToolResult.cache_control = cacheControl;
277
- return;
278
- }
279
- record.content = [{
280
- type: "text",
281
- text: content,
282
- cache_control: cacheControl
283
- }];
284
- if (fallbackToolResult && markerLimit > 1) fallbackToolResult.cache_control = cacheControl;
285
- return;
286
- }
287
- if (!Array.isArray(content)) continue;
288
- for (let j = content.length - 1; j >= 0; j--) {
289
- const block = content[j];
261
+ const blocks = typeof content === "string" ? [{
262
+ type: "text",
263
+ text: content
264
+ }] : content;
265
+ if (!Array.isArray(blocks)) continue;
266
+ for (let j = blocks.length - 1; j >= 0; j--) {
267
+ const block = blocks[j];
290
268
  if (!block || typeof block !== "object") continue;
291
269
  const blockRecord = block;
292
270
  if (blockRecord.type === "text" || blockRecord.type === "image") {
@@ -295,6 +273,7 @@ function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimi
295
273
  return;
296
274
  }
297
275
  blockRecord.cache_control = cacheControl;
276
+ if (typeof content === "string") record.content = blocks;
298
277
  if (fallbackToolResult && markerLimit > 1) fallbackToolResult.cache_control = cacheControl;
299
278
  return;
300
279
  }
@@ -417,8 +396,7 @@ function logAnthropicContextEdits(event) {
417
396
  /** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
418
397
  function applyAnthropicPayloadPolicyToParams(payloadObj, policy, cacheBreakpointOptOutMessageIndexes) {
419
398
  if (policy.allowsServiceTier && policy.serviceTier !== void 0 && payloadObj.service_tier === void 0) payloadObj.service_tier = policy.serviceTier;
420
- if (policy.cacheControl) applyAnthropicCacheControlToSystem(payloadObj.system, policy.cacheControl);
421
- else stripAnthropicSystemPromptBoundary(payloadObj.system);
399
+ normalizeAnthropicSystemBlocks(payloadObj.system, policy.cacheControl);
422
400
  applyAnthropicContextManagementEdits(payloadObj, policy);
423
401
  applyAnthropicRequestCacheControl(payloadObj, policy.cacheControl, false, cacheBreakpointOptOutMessageIndexes);
424
402
  }
@@ -628,9 +606,6 @@ function logAnthropicThinkingDrops(transformations) {
628
606
  const paths = drops.slice(0, 5).map(({ reason, path }) => `${reason} at ${path}`);
629
607
  getAiTransportHost().logWarn("anthropic", `replayed thinking dropped: ${drops.length} block(s) (${paths.join(", ")}${drops.length > 5 ? ", …" : ""})`);
630
608
  }
631
- function asReplayMessage(value) {
632
- return value && typeof value === "object" ? value : void 0;
633
- }
634
609
  /**
635
610
  * Anthropic tool results continue the preceding assistant turn. Preserve that
636
611
  * turn's signed thinking even when the next request disables new thinking.
@@ -639,7 +614,7 @@ function findActiveAnthropicToolTurnAssistantIndex(messages) {
639
614
  const toolResultIds = /* @__PURE__ */ new Set();
640
615
  let index = messages.length - 1;
641
616
  while (index >= 0) {
642
- const message = asReplayMessage(messages[index]);
617
+ const message = asOptionalObjectRecord(messages[index]);
643
618
  if (message?.role === "user" && isRecord(message.operatorMessage)) {
644
619
  index -= 1;
645
620
  continue;
@@ -649,12 +624,12 @@ function findActiveAnthropicToolTurnAssistantIndex(messages) {
649
624
  index -= 1;
650
625
  }
651
626
  if (toolResultIds.size === 0) return -1;
652
- const assistant = asReplayMessage(messages[index]);
627
+ const assistant = asOptionalObjectRecord(messages[index]);
653
628
  if (assistant?.role !== "assistant" || !Array.isArray(assistant.content)) return -1;
654
629
  const toolCallIds = /* @__PURE__ */ new Set();
655
630
  for (const block of assistant.content) {
656
- if (!block || typeof block !== "object") continue;
657
- const record = block;
631
+ const record = asOptionalObjectRecord(block);
632
+ if (!record) continue;
658
633
  if ((record.type === "toolCall" || record.type === "tool_use" || record.type === "function_call") && typeof record.id === "string") toolCallIds.add(record.id);
659
634
  }
660
635
  return [...toolResultIds].every((toolCallId) => toolCallIds.has(toolCallId)) ? index : -1;
@@ -804,12 +779,7 @@ function reconcileAnthropicToolChoice(choice, projection) {
804
779
  if (projection.inputToolCount === 0) return choice;
805
780
  if (choice.type === "tool") {
806
781
  const requestedName = choice.name;
807
- const originalMatch = projection.tools.find((tool) => tool.originalName === requestedName);
808
- if (originalMatch) return {
809
- ...choice,
810
- name: originalMatch.wireName
811
- };
812
- const matchedTool = projection.unavailableOriginalNames.has(requestedName) ? void 0 : projection.tools.find((tool) => tool.wireName === requestedName);
782
+ const matchedTool = projection.tools.find((tool) => tool.originalName === requestedName) ?? (projection.unavailableOriginalNames.has(requestedName) ? void 0 : projection.tools.find((tool) => tool.wireName === requestedName));
813
783
  if (!matchedTool) throw new Error(`Anthropic tool_choice requested unavailable tool "${requestedName}" after schema conversion`);
814
784
  return {
815
785
  ...choice,
@@ -849,12 +819,9 @@ function resolveAnthropicTransportOptions(model, options, apiKey) {
849
819
  modelContextWindow: model.contextWindow,
850
820
  modelMaxTokens: model.maxTokens,
851
821
  requestedMaxTokens: options?.maxTokens,
852
- useModelDefault: resolveClaudeSonnet5ModelIdentity(model) !== void 0 || resolveClaudeOpus5ModelIdentity(model) !== void 0
822
+ useModelDefault: resolveClaudeHaiku55ModelIdentity(model) !== void 0 || resolveClaudeSonnet5ModelIdentity(model) !== void 0 || resolveClaudeOpus5ModelIdentity(model) !== void 0
853
823
  });
854
824
  if (baseMaxTokens === void 0) throw new Error(`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`);
855
- const reasoningModelMaxTokens = resolvePositiveAnthropicTokenLimit(model.maxTokens) ?? baseMaxTokens;
856
- const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
857
- const reasoning = options?.reasoning === "off" && mandatoryAdaptiveThinking ? "low" : options?.reasoning;
858
825
  const resolved = copyProviderAcceptanceObserver(options, {
859
826
  temperature: options?.temperature,
860
827
  stop: options?.stop,
@@ -872,56 +839,71 @@ function resolveAnthropicTransportOptions(model, options, apiKey) {
872
839
  toolChoice: options?.toolChoice,
873
840
  thinkingBudgets: options?.thinkingBudgets,
874
841
  thinkingDisplay: options?.thinkingDisplay,
875
- reasoning,
842
+ reasoning: options?.reasoning,
876
843
  anthropicServerCompaction: options?.anthropicServerCompaction,
877
844
  anthropicCompactThreshold: options?.anthropicCompactThreshold,
878
845
  cacheTtlPruning: options?.cacheTtlPruning,
879
846
  ...options?.authProfileId ? { authProfileId: options.authProfileId } : {}
880
847
  });
848
+ resolved.reasoning = applyAnthropicThinkingOptions(model, resolved, options, "transport");
849
+ return resolved;
850
+ }
851
+ /** Keep both entry points on one thinking mode while retaining their output-budget policies. */
852
+ function applyAnthropicThinkingOptions(model, resolved, options, profile) {
853
+ const managed = profile === "transport";
854
+ const reasoning = options?.reasoning === "off" && requiresClaudeAdaptiveThinking(model) ? "low" : options?.reasoning;
881
855
  if (reasoning === "off") {
882
856
  resolved.thinkingEnabled = false;
883
- return resolved;
857
+ return reasoning;
884
858
  }
885
- if (!reasoning) {
886
- resolved.thinkingEnabled = defaultsClaudeAdaptiveThinking(model);
859
+ if (!reasoning || supportsClaudeAdaptiveThinking(model) || !managed && defaultsClaudeAdaptiveThinking(model)) {
860
+ resolved.thinkingEnabled = Boolean(reasoning) || defaultsClaudeAdaptiveThinking(model);
887
861
  if (resolved.thinkingEnabled) resolved.effort = resolveAnthropicThinkingEffort(model, reasoning);
888
- return resolved;
889
- }
890
- if (supportsClaudeAdaptiveThinking(model)) {
891
- resolved.thinkingEnabled = true;
892
- resolved.effort = resolveAnthropicThinkingEffort(model, reasoning);
893
- return resolved;
862
+ return reasoning;
894
863
  }
895
- const adjusted = adjustMaxTokensForThinking(baseMaxTokens, reasoningModelMaxTokens, reasoning === "max" ? "high" : reasoning, options?.thinkingBudgets);
864
+ const adjusted = adjustMaxTokensForThinking(resolved.maxTokens, (managed ? resolvePositiveAnthropicTokenLimit(model.maxTokens) : model.maxTokens) ?? resolved.maxTokens, managed && reasoning === "max" ? "high" : reasoning, options?.thinkingBudgets);
896
865
  const thinkingEnabled = adjusted.thinkingBudget >= 1024;
897
- resolved.maxTokens = adjusted.maxTokens;
866
+ if (managed || thinkingEnabled) resolved.maxTokens = adjusted.maxTokens;
898
867
  resolved.thinkingEnabled = thinkingEnabled;
899
868
  resolved.thinkingBudgetTokens = thinkingEnabled ? adjusted.thinkingBudget : void 0;
900
- return resolved;
869
+ return reasoning;
870
+ }
871
+ /**
872
+ * Largest completion a request may ask for once transport thinking options
873
+ * apply, for callers that size the prompt before dispatch. Claude transports
874
+ * (Anthropic Messages, Vertex, Bedrock, Bedrock Mantle) may add a thinking
875
+ * budget to `maxTokens`, capped at the model's output limit; some add it even
876
+ * for adaptive models, and their Claude detection is plugin-owned, so every
877
+ * request on these APIs reserves the default budget for its level. Other
878
+ * transports keep `maxTokens` as the total output limit. Over-reserving only
879
+ * shrinks the prompt; it never overflows the window.
880
+ */
881
+ function resolveCompletionTokenReservation(model, maxTokens, reasoning) {
882
+ if (!reasoning || reasoning === "off" || model.api !== "anthropic-messages" && model.api !== "bedrock-converse-stream") return maxTokens;
883
+ return adjustMaxTokensForThinking(maxTokens, resolvePositiveAnthropicTokenLimit(model.maxTokens) ?? Number.POSITIVE_INFINITY, reasoning).maxTokens;
901
884
  }
902
885
  //#endregion
903
886
  //#region packages/ai/src/transports/anthropic-messages.ts
904
887
  const NON_VISION_USER_IMAGE_PLACEHOLDER = "(image omitted: model does not support images)";
905
888
  async function convertContentBlocks(content, model, imageBudget, profile, isError) {
906
889
  const mediaPlaceholder = describeToolResultMediaPlaceholder(content);
907
- if (!((profile === "provider" || model.input.includes("image")) && content.some(isImageWithMediaPayload))) return sanitizeNonEmptyTransportPayloadText(extractToolResultText(content), mediaPlaceholder ?? (profile === "transport" ? "(no output)" : isError ? "[tool error with no output]" : ""));
890
+ if (!((profile === "provider" || model.input.includes("image")) && content.some(isImageWithMediaPayload))) {
891
+ const text = extractToolResultText(content);
892
+ return text.trim() ? text : mediaPlaceholder ?? (profile === "transport" ? "(no output)" : isError ? "[tool error with no output]" : "");
893
+ }
908
894
  const blocks = [];
909
- let hasTextBlock = false;
910
895
  for (const block of content) {
911
896
  const record = asOptionalObjectRecord(block);
912
897
  if (!record) continue;
913
898
  const blockText = extractToolResultBlockText(block);
914
- if (blockText) {
915
- blocks.push({
916
- type: "text",
917
- text: sanitizeTransportPayloadText(blockText)
918
- });
919
- hasTextBlock = true;
920
- }
899
+ if (blockText) blocks.push({
900
+ type: "text",
901
+ text: blockText
902
+ });
921
903
  if (!isImageWithMediaPayload(record)) continue;
922
904
  const [normalizedImage] = await normalizeAnthropicInlineContent([{
923
905
  type: "image",
924
- data: typeof record.data === "string" ? record.data : "",
906
+ data: record.data,
925
907
  mimeType: typeof record.mimeType === "string" ? record.mimeType : profile === "provider" ? "image/jpeg" : "image/png"
926
908
  }], imageBudget);
927
909
  if (normalizedImage?.type !== "image") continue;
@@ -934,7 +916,7 @@ async function convertContentBlocks(content, model, imageBudget, profile, isErro
934
916
  }
935
917
  });
936
918
  }
937
- if (!hasTextBlock) blocks.unshift({
919
+ if (!blocks.some((block) => block.type === "text")) blocks.unshift({
938
920
  type: "text",
939
921
  text: mediaPlaceholder ?? "(see attached image)"
940
922
  });
@@ -1092,12 +1074,17 @@ async function convertAnthropicMessages(transformedMessages, model, isOAuthToken
1092
1074
  return params;
1093
1075
  }
1094
1076
  /** Shared generation contract, after each entry point resolves its defaults and tool policy. */
1095
- function buildAnthropicGenerationParams({ model, options, tools, toolProjection, profile }) {
1077
+ function buildAnthropicGenerationParams({ model, options, toolProjection, eagerToolInputStreaming, profile }) {
1096
1078
  const params = {};
1097
1079
  const mandatoryAdaptiveThinking = requiresClaudeAdaptiveThinking(model);
1098
1080
  if (options?.temperature !== void 0 && !options?.thinkingEnabled && !supportsClaudeNativeXhighEffort(model)) params.temperature = options.temperature;
1099
1081
  if (options?.stop !== void 0 && options.stop.length > 0) params.stop_sequences = options.stop;
1100
- if (tools && tools.length > 0) params.tools = tools;
1082
+ if (toolProjection && toolProjection.tools.length > 0) params.tools = toolProjection.tools.map((tool) => ({
1083
+ name: tool.wireName,
1084
+ description: tool.description,
1085
+ input_schema: tool.inputSchema,
1086
+ ...eagerToolInputStreaming ? { eager_input_streaming: true } : {}
1087
+ }));
1101
1088
  if (mandatoryAdaptiveThinking || model.reasoning || supportsClaudeAdaptiveThinking(model)) {
1102
1089
  if (mandatoryAdaptiveThinking || options?.thinkingEnabled) {
1103
1090
  const display = options?.thinkingDisplay ?? "summarized";
@@ -1120,27 +1107,12 @@ function buildAnthropicGenerationParams({ model, options, tools, toolProjection,
1120
1107
  if (typeof userId === "string") params.metadata = { user_id: userId };
1121
1108
  }
1122
1109
  if (options?.toolChoice) {
1123
- const normalizedToolChoice = normalizeAnthropicToolChoice(mandatoryAdaptiveThinking || options?.thinkingEnabled === true || resolveClaudeSonnet55ModelIdentity(model) !== void 0, options.toolChoice);
1110
+ const normalizedToolChoice = normalizeAnthropicToolChoice(resolveClaudeHaiku55ModelIdentity(model) === void 0 && (mandatoryAdaptiveThinking || options?.thinkingEnabled === true || resolveClaudeSonnet55ModelIdentity(model) !== void 0), options.toolChoice);
1124
1111
  const projectedToolChoice = toolProjection ? reconcileAnthropicToolChoice(normalizedToolChoice, toolProjection) : normalizedToolChoice;
1125
1112
  if (projectedToolChoice) params.tool_choice = projectedToolChoice;
1126
1113
  }
1127
1114
  return params;
1128
1115
  }
1129
- function convertAnthropicTools(tools, isOAuthTokenLocal, supportsEagerToolInputStreaming = false) {
1130
- const projection = projectAnthropicTools(tools, (name) => isOAuthTokenLocal ? toClaudeCodeToolName(name) : name);
1131
- return {
1132
- projection,
1133
- tools: projection.tools.map((tool) => {
1134
- const projected = {
1135
- name: tool.wireName,
1136
- description: tool.description,
1137
- input_schema: tool.inputSchema
1138
- };
1139
- if (supportsEagerToolInputStreaming) projected.eager_input_streaming = true;
1140
- return projected;
1141
- })
1142
- };
1143
- }
1144
1116
  /** Assemble both public Messages routes without changing their replay/default profiles. */
1145
1117
  async function buildAnthropicRequest(model, context, options, profile, isOAuthToken, serverSideFallback, claudeCodeVersion) {
1146
1118
  const managed = profile === "transport";
@@ -1152,8 +1124,13 @@ async function buildAnthropicRequest(model, context, options, profile, isOAuthTo
1152
1124
  if (managed && maxTokens === void 0) throw new Error(`Anthropic Messages transport requires a positive maxTokens value for ${model.provider}/${model.id}`);
1153
1125
  const { cacheControl, supportsCacheControlOnTools } = resolveAnthropicCacheOptions(model, options?.cacheRetention);
1154
1126
  const system = buildAnthropicSystemBlocks(context.systemPrompt, isOAuthToken, cacheControl, claudeCodeVersion);
1155
- const convertedTools = context.tools ? convertAnthropicTools(context.tools, isOAuthToken, !managed && (model.compat?.supportsEagerToolInputStreaming ?? model.provider !== "fireworks")) : void 0;
1156
- const toolProjection = convertedTools?.projection;
1127
+ let toolProjection;
1128
+ let eagerToolInputStreaming = false;
1129
+ if (context.tools) {
1130
+ const tools = context.tools;
1131
+ eagerToolInputStreaming = !managed && (model.compat?.supportsEagerToolInputStreaming ?? model.provider !== "fireworks");
1132
+ toolProjection = projectAnthropicTools(tools, (name) => isOAuthToken ? toClaudeCodeToolName(name) : name);
1133
+ }
1157
1134
  const replayPlan = buildAnthropicReplayPlan(context.messages, model, {
1158
1135
  enabled: !isOAuthToken && options?.anthropicServerCompaction === true,
1159
1136
  authProfileId: options?.authProfileId,
@@ -1183,8 +1160,8 @@ async function buildAnthropicRequest(model, context, options, profile, isOAuthTo
1183
1160
  Object.assign(params, buildAnthropicGenerationParams({
1184
1161
  model,
1185
1162
  options,
1186
- tools: convertedTools?.tools,
1187
1163
  toolProjection,
1164
+ eagerToolInputStreaming,
1188
1165
  profile
1189
1166
  }));
1190
1167
  applyAnthropicRequestCacheControl(params, cacheControl, supportsCacheControlOnTools, cacheBreakpointOptOutMessageIndexes);
@@ -1210,19 +1187,12 @@ async function prepareAnthropicRequest(initialParams, model, options, directApiK
1210
1187
  }
1211
1188
  //#endregion
1212
1189
  //#region packages/ai/src/providers/anthropic-refusal.ts
1213
- function readAnthropicRefusalDetails(value) {
1214
- if (!value || typeof value !== "object") return {
1215
- category: null,
1216
- explanation: null
1217
- };
1218
- const details = value;
1219
- return {
1220
- category: normalizeNullableString(details.category),
1221
- explanation: normalizeNullableString(details.explanation)
1222
- };
1223
- }
1224
1190
  function applyAnthropicRefusal(output, stopDetails, provider) {
1225
- const details = readAnthropicRefusalDetails(stopDetails);
1191
+ const record = asOptionalObjectRecord(stopDetails);
1192
+ const details = {
1193
+ category: normalizeNullableString(record?.category),
1194
+ explanation: normalizeNullableString(record?.explanation)
1195
+ };
1226
1196
  const category = details.category ? ` (category: ${details.category})` : "";
1227
1197
  const explanation = details.explanation ? `: ${details.explanation}` : ".";
1228
1198
  output.stopReason = "error";
@@ -1239,6 +1209,41 @@ function applyAnthropicRefusal(output, stopDetails, provider) {
1239
1209
  }
1240
1210
  //#endregion
1241
1211
  //#region packages/ai/src/providers/anthropic-usage.ts
1212
+ const BILLED_USAGE_KEYS = [
1213
+ "input",
1214
+ "output",
1215
+ "cacheRead",
1216
+ "cacheWrite",
1217
+ "cacheWrite1h"
1218
+ ];
1219
+ function readAnthropicBilledUsage(usage, missingCacheTokens) {
1220
+ return {
1221
+ input: readAnthropicUsageTokenCount(usage.input_tokens),
1222
+ output: readAnthropicUsageTokenCount(usage.output_tokens),
1223
+ cacheRead: readAnthropicUsageTokenCount(usage.cache_read_input_tokens ?? missingCacheTokens),
1224
+ cacheWrite: readAnthropicUsageTokenCount(usage.cache_creation_input_tokens ?? missingCacheTokens),
1225
+ cacheWrite1h: readAnthropicCacheWriteUsage(usage).cacheWrite1h
1226
+ };
1227
+ }
1228
+ function applyAnthropicBilledUsage(target, resolved) {
1229
+ for (const key of BILLED_USAGE_KEYS) {
1230
+ const value = resolved[key];
1231
+ if (value !== void 0) target[key] = value;
1232
+ }
1233
+ target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
1234
+ }
1235
+ function readAnthropicIterationUsage(value) {
1236
+ if (!value || typeof value !== "object" || Array.isArray(value)) return;
1237
+ const { input, output, cacheRead, cacheWrite, cacheWrite1h } = readAnthropicBilledUsage(value);
1238
+ if (input === void 0 || output === void 0 || cacheRead === void 0 || cacheWrite === void 0) return;
1239
+ return {
1240
+ input,
1241
+ output,
1242
+ cacheRead,
1243
+ cacheWrite,
1244
+ cacheWrite1h: cacheWrite1h ?? 0
1245
+ };
1246
+ }
1242
1247
  function readAnthropicUsageTokenCount(value) {
1243
1248
  return asNonNegativeFiniteNumber(value);
1244
1249
  }
@@ -1266,20 +1271,14 @@ function readAnthropicPromptUsageSnapshot(usage) {
1266
1271
  function readLastAnthropicIterationUsage(usage) {
1267
1272
  if (usage.iterations == null) return { state: "absent" };
1268
1273
  if (!Array.isArray(usage.iterations) || usage.iterations.length === 0) return { state: "invalid" };
1269
- const iteration = usage.iterations.at(-1);
1270
- if (!iteration || typeof iteration !== "object" || Array.isArray(iteration)) return { state: "invalid" };
1271
- const record = iteration;
1272
- const input = readAnthropicUsageTokenCount(record.input_tokens);
1273
- const cacheRead = readAnthropicUsageTokenCount(record.cache_read_input_tokens);
1274
- const cacheWrite = readAnthropicUsageTokenCount(record.cache_creation_input_tokens);
1275
- const outputTokens = readAnthropicUsageTokenCount(record.output_tokens);
1276
- if (input === void 0 || cacheRead === void 0 || cacheWrite === void 0 || outputTokens === void 0) return { state: "invalid" };
1277
- const contextPromptTokens = input + cacheRead + cacheWrite;
1274
+ const iteration = readAnthropicIterationUsage(usage.iterations.at(-1));
1275
+ if (!iteration) return { state: "invalid" };
1276
+ const contextPromptTokens = iteration.input + iteration.cacheRead + iteration.cacheWrite;
1278
1277
  return {
1279
1278
  state: "valid",
1280
1279
  usage: {
1281
1280
  contextPromptTokens,
1282
- totalTokens: contextPromptTokens + outputTokens
1281
+ totalTokens: contextPromptTokens + iteration.output
1283
1282
  }
1284
1283
  };
1285
1284
  }
@@ -1294,19 +1293,10 @@ function readAnthropicCompactionBilledUsage(iterations) {
1294
1293
  cacheWrite1h: 0
1295
1294
  };
1296
1295
  for (const iteration of iterations) {
1297
- if (!iteration || typeof iteration !== "object" || Array.isArray(iteration)) return;
1298
- const record = iteration;
1299
- const input = readAnthropicUsageTokenCount(record.input_tokens);
1300
- const output = readAnthropicUsageTokenCount(record.output_tokens);
1301
- const cacheRead = readAnthropicUsageTokenCount(record.cache_read_input_tokens);
1302
- const cacheWrite = readAnthropicUsageTokenCount(record.cache_creation_input_tokens);
1303
- if (input === void 0 || output === void 0 || cacheRead === void 0 || cacheWrite === void 0) return;
1304
- sawCompaction ||= record.type === "compaction";
1305
- billed.input += input;
1306
- billed.output += output;
1307
- billed.cacheRead += cacheRead;
1308
- billed.cacheWrite += cacheWrite;
1309
- billed.cacheWrite1h += readAnthropicCacheWriteUsage(record).cacheWrite1h ?? 0;
1296
+ const resolved = readAnthropicIterationUsage(iteration);
1297
+ if (!resolved) return;
1298
+ sawCompaction ||= iteration.type === "compaction";
1299
+ for (const key of BILLED_USAGE_KEYS) billed[key] += resolved[key];
1310
1300
  }
1311
1301
  return sawCompaction ? billed : void 0;
1312
1302
  }
@@ -1314,18 +1304,9 @@ function readAnthropicCompactionBilledUsage(iterations) {
1314
1304
  function applyAnthropicMessageStartUsage(target, payload) {
1315
1305
  const promptUsage = readAnthropicPromptUsageSnapshot(payload);
1316
1306
  const promptTokens = promptUsage ? promptUsage.input + promptUsage.cacheRead + promptUsage.cacheWrite : 0;
1317
- const inputTokens = readAnthropicUsageTokenCount(payload.input_tokens);
1318
- if (inputTokens !== void 0) target.input = inputTokens;
1319
- const outputTokens = readAnthropicUsageTokenCount(payload.output_tokens);
1320
- if (outputTokens !== void 0) target.output = outputTokens;
1321
- const cacheReadTokens = payload.cache_read_input_tokens == null ? 0 : readAnthropicUsageTokenCount(payload.cache_read_input_tokens);
1322
- if (cacheReadTokens !== void 0) target.cacheRead = cacheReadTokens;
1323
- const cacheWriteTokens = payload.cache_creation_input_tokens == null ? 0 : readAnthropicUsageTokenCount(payload.cache_creation_input_tokens);
1324
- if (cacheWriteTokens !== void 0) target.cacheWrite = cacheWriteTokens;
1325
- const { cacheWrite1h } = readAnthropicCacheWriteUsage(payload);
1326
- if (cacheWrite1h !== void 0) target.cacheWrite1h = cacheWrite1h;
1327
- target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
1328
- if (promptTokens > 0 && outputTokens !== void 0) target.contextUsage = {
1307
+ const resolved = readAnthropicBilledUsage(payload, 0);
1308
+ applyAnthropicBilledUsage(target, resolved);
1309
+ if (promptTokens > 0 && resolved.output !== void 0) target.contextUsage = {
1329
1310
  state: "available",
1330
1311
  promptTokens,
1331
1312
  totalTokens: promptTokens + target.output
@@ -1336,28 +1317,8 @@ function applyAnthropicMessageStartUsage(target, payload) {
1336
1317
  function applyAnthropicMessageDeltaUsage(target, usage, messageStartPromptUsage) {
1337
1318
  if (!usage) return;
1338
1319
  const billedIterations = readAnthropicCompactionBilledUsage(usage.iterations);
1339
- const inputTokens = readAnthropicUsageTokenCount(usage.input_tokens);
1340
- const outputTokens = readAnthropicUsageTokenCount(usage.output_tokens);
1341
- const cacheReadTokens = readAnthropicUsageTokenCount(usage.cache_read_input_tokens);
1342
- const cacheWriteTokens = readAnthropicUsageTokenCount(usage.cache_creation_input_tokens);
1343
- const resolved = billedIterations ?? {
1344
- input: inputTokens,
1345
- output: outputTokens,
1346
- cacheRead: cacheReadTokens,
1347
- cacheWrite: cacheWriteTokens,
1348
- cacheWrite1h: readAnthropicCacheWriteUsage(usage).cacheWrite1h
1349
- };
1350
- for (const key of [
1351
- "input",
1352
- "output",
1353
- "cacheRead",
1354
- "cacheWrite",
1355
- "cacheWrite1h"
1356
- ]) {
1357
- const value = resolved[key];
1358
- if (value !== void 0) target[key] = value;
1359
- }
1360
- target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
1320
+ const reported = readAnthropicBilledUsage(usage);
1321
+ applyAnthropicBilledUsage(target, billedIterations ?? reported);
1361
1322
  const iterationUsage = readLastAnthropicIterationUsage(usage);
1362
1323
  if (iterationUsage.state === "valid") target.contextUsage = {
1363
1324
  state: "available",
@@ -1365,7 +1326,7 @@ function applyAnthropicMessageDeltaUsage(target, usage, messageStartPromptUsage)
1365
1326
  totalTokens: iterationUsage.usage.totalTokens
1366
1327
  };
1367
1328
  else if (iterationUsage.state === "invalid") target.contextUsage = { state: "unavailable" };
1368
- else if (outputTokens !== void 0 && (messageStartPromptUsage !== void 0 || (cacheReadTokens !== void 0 || cacheWriteTokens !== void 0) && readAnthropicPromptUsageSnapshot(usage) !== void 0)) {
1329
+ else if (reported.output !== void 0 && (messageStartPromptUsage !== void 0 || (reported.cacheRead !== void 0 || reported.cacheWrite !== void 0) && readAnthropicPromptUsageSnapshot(usage) !== void 0)) {
1369
1330
  const promptTokens = target.input + target.cacheRead + target.cacheWrite;
1370
1331
  target.contextUsage = {
1371
1332
  state: "available",
@@ -1401,14 +1362,44 @@ async function consumeAnthropicStream(params) {
1401
1362
  let sawMessageStop = false;
1402
1363
  let sawStopReason = false;
1403
1364
  const pendingTextEnds = [];
1404
- const flushPendingTextEnds = () => {
1365
+ const flushPendingTextEnds = (classifyToolTurn = false) => {
1366
+ if (managed && classifyToolTurn && (output.stopReason === "toolUse" || output.content.some((block) => block.type === "toolCall"))) tagPendingCommentaryText(output.content);
1405
1367
  for (const event of pendingTextEnds) eventSink.push(event);
1406
1368
  pendingTextEnds.length = 0;
1407
1369
  };
1408
- const emitTextEnd = (event) => {
1409
- if (managed) pendingTextEnds.push(event);
1370
+ const emitContentEnd = (block, contentIndex, deferText) => {
1371
+ const event = {
1372
+ type: `${block.type}_end`,
1373
+ contentIndex,
1374
+ content: block.type === "thinking" ? block.thinking : block.text,
1375
+ partial: output
1376
+ };
1377
+ if (managed && deferText && event.type === "text_end") pendingTextEnds.push(event);
1410
1378
  else eventSink.push(event);
1411
1379
  };
1380
+ const emitContentStart = (kind, contentIndex, delta = "") => {
1381
+ eventSink.push({
1382
+ type: `${kind}_start`,
1383
+ contentIndex,
1384
+ partial: output
1385
+ });
1386
+ if (delta.length > 0) eventSink.push({
1387
+ type: `${kind}_delta`,
1388
+ contentIndex,
1389
+ delta,
1390
+ partial: output
1391
+ });
1392
+ };
1393
+ const appendContentDelta = (block, contentIndex, delta) => {
1394
+ if (block.type === "thinking") appendAssistantThinking(block, delta);
1395
+ else block.text += delta;
1396
+ eventSink.push({
1397
+ type: `${block.type}_delta`,
1398
+ contentIndex,
1399
+ delta,
1400
+ partial: output
1401
+ });
1402
+ };
1412
1403
  const eventIndexKey = (eventIndex) => typeof eventIndex === "number" ? eventIndex : -1;
1413
1404
  const appendReasoningContentDelta = (kind, eventIndex, rawText) => {
1414
1405
  if (typeof rawText !== "string") return false;
@@ -1430,22 +1421,10 @@ async function consumeAnthropicStream(params) {
1430
1421
  output.content.push(block);
1431
1422
  contentIndex = output.content.length - 1;
1432
1423
  indexes.set(key, contentIndex);
1433
- eventSink.push({
1434
- type: `${kind}_start`,
1435
- contentIndex,
1436
- partial: output
1437
- });
1424
+ emitContentStart(kind, contentIndex);
1438
1425
  }
1439
- if (block.type === "thinking") {
1440
- appendAssistantThinking(block, text);
1441
- block.thinkingSignature = "reasoning_content";
1442
- } else if (block.type === "text") block.text += text;
1443
- eventSink.push({
1444
- type: `${kind}_delta`,
1445
- contentIndex,
1446
- delta: text,
1447
- partial: output
1448
- });
1426
+ if (block.type === "thinking") block.thinkingSignature = "reasoning_content";
1427
+ appendContentDelta(block, contentIndex, text);
1449
1428
  return true;
1450
1429
  };
1451
1430
  const finishReasoningContentSidecars = (eventIndex) => {
@@ -1456,12 +1435,7 @@ async function consumeAnthropicStream(params) {
1456
1435
  if (contentIndex === void 0) continue;
1457
1436
  indexes.delete(key);
1458
1437
  const block = output.content[contentIndex];
1459
- if (block?.type === kind) eventSink.push({
1460
- type: `${kind}_end`,
1461
- contentIndex,
1462
- content: block.type === "thinking" ? block.thinking : block.text,
1463
- partial: output
1464
- });
1438
+ if (block?.type === kind) emitContentEnd(block, contentIndex, false);
1465
1439
  }
1466
1440
  };
1467
1441
  for await (const rawEvent of iterateModelStream(params.events, options.signal)) {
@@ -1522,23 +1496,8 @@ async function consumeAnthropicStream(params) {
1522
1496
  for (const [i, block] of blocks.entries()) {
1523
1497
  if (block.type !== "text") continue;
1524
1498
  delete block.index;
1525
- eventSink.push({
1526
- type: "text_start",
1527
- contentIndex: i,
1528
- partial: output
1529
- });
1530
- if (block.text) eventSink.push({
1531
- type: "text_delta",
1532
- contentIndex: i,
1533
- delta: block.text,
1534
- partial: output
1535
- });
1536
- emitTextEnd({
1537
- type: "text_end",
1538
- contentIndex: i,
1539
- content: block.text,
1540
- partial: output
1541
- });
1499
+ emitContentStart("text", i, block.text);
1500
+ emitContentEnd(block, i, true);
1542
1501
  }
1543
1502
  continue;
1544
1503
  }
@@ -1560,22 +1519,11 @@ async function consumeAnthropicStream(params) {
1560
1519
  redacted: true,
1561
1520
  index
1562
1521
  };
1563
- const kind = block.type;
1564
1522
  const delta = block.type === "text" ? block.text : block.redacted ? "" : block.thinking;
1565
1523
  output.content.push(block);
1566
1524
  const contentIndex = output.content.length - 1;
1567
1525
  blockIndexes.set(index, contentIndex);
1568
- eventSink.push({
1569
- type: `${kind}_start`,
1570
- contentIndex,
1571
- partial: output
1572
- });
1573
- if (delta.length > 0) eventSink.push({
1574
- type: `${kind}_delta`,
1575
- contentIndex,
1576
- delta,
1577
- partial: output
1578
- });
1526
+ emitContentStart(block.type, contentIndex, delta);
1579
1527
  continue;
1580
1528
  }
1581
1529
  if (contentBlock?.type === "tool_use") {
@@ -1615,13 +1563,7 @@ async function consumeAnthropicStream(params) {
1615
1563
  const text = sanitizeTransportPayloadText(delta.content);
1616
1564
  if (text.length > 0) {
1617
1565
  if (block?.type === "text" && index !== void 0) {
1618
- block.text += text;
1619
- eventSink.push({
1620
- type: "text_delta",
1621
- contentIndex: index,
1622
- delta: text,
1623
- partial: output
1624
- });
1566
+ appendContentDelta(block, index, text);
1625
1567
  appendedContent = true;
1626
1568
  } else appendedContent = appendReasoningContentDelta("text", event.index, text);
1627
1569
  }
@@ -1637,31 +1579,15 @@ async function consumeAnthropicStream(params) {
1637
1579
  output.content.push(block);
1638
1580
  index = output.content.length - 1;
1639
1581
  if (typeof event.index === "number") blockIndexes.set(event.index, index);
1640
- eventSink.push({
1641
- type: "text_start",
1642
- contentIndex: index,
1643
- partial: output
1644
- });
1582
+ emitContentStart("text", index);
1645
1583
  }
1646
1584
  if (index === void 0) continue;
1647
1585
  if (block?.type === "text" && delta?.type === "text_delta" && typeof delta.text === "string") {
1648
- block.text += delta.text;
1649
- eventSink.push({
1650
- type: "text_delta",
1651
- contentIndex: index,
1652
- delta: delta.text,
1653
- partial: output
1654
- });
1586
+ appendContentDelta(block, index, delta.text);
1655
1587
  continue;
1656
1588
  }
1657
1589
  if (block?.type === "thinking" && delta?.type === "thinking_delta" && typeof delta.thinking === "string") {
1658
- appendAssistantThinking(block, delta.thinking);
1659
- eventSink.push({
1660
- type: "thinking_delta",
1661
- contentIndex: index,
1662
- delta: delta.thinking,
1663
- partial: output
1664
- });
1590
+ appendContentDelta(block, index, delta.thinking);
1665
1591
  continue;
1666
1592
  }
1667
1593
  if (block?.type === "toolCall" && delta?.type === "input_json_delta" && typeof delta.partial_json === "string") {
@@ -1699,24 +1625,12 @@ async function consumeAnthropicStream(params) {
1699
1625
  }
1700
1626
  blockIndexes.delete(eventIndex);
1701
1627
  delete block.index;
1702
- if (block.type === "text") emitTextEnd({
1703
- type: "text_end",
1704
- contentIndex: index,
1705
- content: block.text,
1706
- partial: output
1707
- });
1708
- else if (block.type === "thinking") {
1709
- if (pendingSignature !== void 0) block.thinkingSignature = pendingSignature;
1710
- eventSink.push({
1711
- type: "thinking_end",
1712
- contentIndex: index,
1713
- content: block.thinking,
1714
- partial: output
1715
- });
1716
- } else if (block.type === "toolCall") sealedToolCalls.push({
1628
+ if (block.type === "thinking" && pendingSignature !== void 0) block.thinkingSignature = pendingSignature;
1629
+ if (block.type === "toolCall") sealedToolCalls.push({
1717
1630
  block,
1718
1631
  contentIndex: index
1719
1632
  });
1633
+ else emitContentEnd(block, index, true);
1720
1634
  finishReasoningContentSidecars(event.index);
1721
1635
  continue;
1722
1636
  }
@@ -1731,8 +1645,7 @@ async function consumeAnthropicStream(params) {
1731
1645
  }
1732
1646
  applyAnthropicMessageDeltaUsage(output.usage, usage, messageStartPromptUsage);
1733
1647
  calculateCost(costModel, output.usage);
1734
- if (managed && (output.stopReason === "toolUse" || output.content.some((block) => block.type === "toolCall"))) tagPendingCommentaryText(output.content);
1735
- flushPendingTextEnds();
1648
+ flushPendingTextEnds(true);
1736
1649
  }
1737
1650
  }
1738
1651
  if ((isDirectAnthropicModel(model) || !managed && refusalBuffer) && !sawMessageStop) throw new Error("Anthropic stream ended before message_stop");
@@ -1751,11 +1664,10 @@ async function consumeAnthropicStream(params) {
1751
1664
  });
1752
1665
  }
1753
1666
  refusalBuffer?.flush();
1754
- if (managed && (output.stopReason === "toolUse" || output.content.some((block) => block.type === "toolCall"))) tagPendingCommentaryText(output.content);
1755
- flushPendingTextEnds();
1667
+ flushPendingTextEnds(true);
1756
1668
  } finally {
1757
1669
  logAnthropicThinkingDrops(inputTransformations);
1758
1670
  }
1759
1671
  }
1760
1672
  //#endregion
1761
- export { buildAnthropicSystemBlocks as A, ANTHROPIC_SERVER_SIDE_FALLBACKS as B, isAnthropicReplayRejection as C, applyAnthropicEphemeralCacheControlMarkers as D, applyAnthropicContextManagementToRequest as E, resolveAnthropicEphemeralCacheControl as F, resolveAnthropicFallbackServingModelCost as G, CLAUDE_OPUS_FALLBACK_MODEL_COST as H, resolveAnthropicPayloadPolicy as I, usesFoundryBearerAuth as J, isAnthropicOAuthApiKey as K, resolveAnthropicRequestBetaHeader as L, isDirectAnthropicModel as M, logAnthropicContextEdits as N, applyAnthropicPayloadPolicyToParams as O, resolveAnthropicCacheOptions as P, resolveAnthropicServerCompactionPlan as R, readAnthropicInputTransformations as S, suppressAnthropicCompaction as T, applyAnthropicFallbackBoundary as U, ANTHROPIC_SERVER_SIDE_FALLBACK_BETA as V, readAnthropicFallbackBoundary as W, toClaudeCodeToolName as _, readAnthropicPromptUsageSnapshot as a, findActiveAnthropicToolTurnAssistantIndex as b, applyAnthropicRefusal as c, resolveAnthropicTransportOptions as d, normalizeAnthropicToolCallId as f, resolveOriginalAnthropicToolName as g, reconcileAnthropicToolChoice as h, readAnthropicCacheWriteUsage as i, isAnthropicServerToolClearingEnabled as j, applyAnthropicRequestCacheControl as k, buildAnthropicRequest as l, projectAnthropicTools as m, applyAnthropicMessageDeltaUsage as n, readAnthropicUsageTokenCount as o, normalizeAnthropicToolChoice as p, omitFoundryBearerCredentialHeaders as q, applyAnthropicMessageStartUsage as r, readLastAnthropicIterationUsage as s, consumeAnthropicStream as t, prepareAnthropicRequest as u, ANTHROPIC_OMITTED_REASONING_TEXT as v, resolveNewestAnthropicCompaction as w, logAnthropicThinkingDrops as x, applyAnthropicThinkingBindingControls as y, supportsAnthropicServerSideFallback as z };
1673
+ export { applyAnthropicPayloadPolicyToParams as A, resolveAnthropicServerCompactionPlan as B, logAnthropicThinkingDrops as C, suppressAnthropicCompaction as D, resolveNewestAnthropicCompaction as E, logAnthropicContextEdits as F, applyAnthropicFallbackBoundary as G, ANTHROPIC_SERVER_SIDE_FALLBACKS as H, resolveAnthropicCacheOptions as I, isAnthropicOAuthApiKey as J, readAnthropicFallbackBoundary as K, resolveAnthropicEphemeralCacheControl as L, buildAnthropicSystemBlocks as M, isAnthropicServerToolClearingEnabled as N, applyAnthropicContextManagementToRequest as O, isDirectAnthropicModel as P, resolveAnthropicPayloadPolicy as R, findActiveAnthropicToolTurnAssistantIndex as S, isAnthropicReplayRejection as T, ANTHROPIC_SERVER_SIDE_FALLBACK_BETA as U, supportsAnthropicServerSideFallback as V, CLAUDE_OPUS_FALLBACK_MODEL_COST as W, usesFoundryBearerAuth as X, omitFoundryBearerCredentialHeaders as Y, reconcileAnthropicToolChoice as _, readAnthropicPromptUsageSnapshot as a, ANTHROPIC_OMITTED_REASONING_TEXT as b, applyAnthropicRefusal as c, applyAnthropicThinkingOptions as d, resolveAnthropicTransportOptions as f, projectAnthropicTools as g, normalizeAnthropicToolChoice as h, readAnthropicCacheWriteUsage as i, applyAnthropicRequestCacheControl as j, applyAnthropicEphemeralCacheControlMarkers as k, buildAnthropicRequest as l, normalizeAnthropicToolCallId as m, applyAnthropicMessageDeltaUsage as n, readAnthropicUsageTokenCount as o, resolveCompletionTokenReservation as p, resolveAnthropicFallbackServingModelCost as q, applyAnthropicMessageStartUsage as r, readLastAnthropicIterationUsage as s, consumeAnthropicStream as t, prepareAnthropicRequest as u, resolveOriginalAnthropicToolName as v, readAnthropicInputTransformations as w, applyAnthropicThinkingBindingControls as x, toClaudeCodeToolName as y, resolveAnthropicRequestBetaHeader as z };