@openclaw/ai 2026.7.2-beta.7 → 2026.8.1-beta.2

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 (91) hide show
  1. package/dist/{anthropic-CH4UUnZr.mjs → anthropic-B6dLpq5L.mjs} +55 -203
  2. package/dist/{src-QkygScBs.mjs → anthropic-JsNA5KCu.mjs} +0 -1
  3. package/dist/anthropic-compaction-replay-8lJNKXOE.mjs +840 -0
  4. package/dist/anthropic-payload-policy-CiEuQS72.d.mts +49 -0
  5. package/dist/{api-registry-DlMgPR39.d.mts → api-registry-k3zTz0cV.d.mts} +1 -1
  6. package/dist/{azure-openai-responses-CImcwB83.mjs → azure-openai-responses-mxIOtUnn.mjs} +23 -29
  7. package/dist/diagnostics.d.mts +24 -1
  8. package/dist/diagnostics.mjs +2 -1
  9. package/dist/{event-stream-YjaPW20U.d.mts → event-stream-BP6AWT8j.d.mts} +4 -1
  10. package/dist/{event-stream-D8n2uFee.mjs → event-stream-uSMZJ3FA.mjs} +26 -5
  11. package/dist/event-stream.d.mts +1 -1
  12. package/dist/event-stream.mjs +1 -1
  13. package/dist/{google-CtSg0iTS.mjs → google-C7h2QzDX.mjs} +5 -5
  14. package/dist/{google-shared-DNBz5rcD.mjs → google-shared-B0Qr9OR7.mjs} +146 -89
  15. package/dist/google-thinking-level-C-V3tecN.mjs +9 -0
  16. package/dist/{google-vertex-31f1uS9L.mjs → google-vertex-Bhc3TjDl.mjs} +11 -7
  17. package/dist/{llm-request-activity-BjtkplhG.mjs → headers-DdOQtGuU.mjs} +9 -1
  18. package/dist/host-DTqNc7ad.mjs +466 -0
  19. package/dist/{host-B9GUmcra.d.mts → host-vWgMMhiJ.d.mts} +9 -4
  20. package/dist/index.d.mts +6 -6
  21. package/dist/index.mjs +7 -5
  22. package/dist/internal/anthropic.d.mts +29 -5
  23. package/dist/internal/anthropic.mjs +5 -5
  24. package/dist/internal/openai-responses-payload-policy.d.mts +3 -0
  25. package/dist/internal/openai-responses-payload-policy.mjs +3 -0
  26. package/dist/internal/openai.d.mts +6 -6
  27. package/dist/internal/openai.mjs +8 -7
  28. package/dist/internal/runtime.d.mts +17 -5
  29. package/dist/internal/runtime.mjs +85 -73
  30. package/dist/internal/shared.d.mts +1 -6
  31. package/dist/internal/shared.mjs +3 -5
  32. package/dist/{json-parse-BvXNt1-7.mjs → json-parse-CDnesDM_.mjs} +4 -6
  33. package/dist/{mistral-CWmpvWYh.mjs → mistral-CEWoQI_g.mjs} +45 -48
  34. package/dist/number-coercion-H9qHik3g.mjs +71 -0
  35. package/dist/openai-chatgpt-jwt-KWcgd0d_.mjs +19 -0
  36. package/dist/{openai-chatgpt-responses-B84Ibtrd.mjs → openai-chatgpt-responses-CrPmqERt.mjs} +284 -240
  37. package/dist/{openai-completions-DsOxhOD1.mjs → openai-completions-BPnt4Sml.mjs} +65 -99
  38. package/dist/{openai-completions-compat-DBWjXoMZ.d.mts → openai-completions-compat-Dt3dcawL.d.mts} +2 -2
  39. package/dist/{openai-responses-BT7A3sLu.mjs → openai-responses-DhIKtOup.mjs} +18 -41
  40. package/dist/openai-responses-contracts-CyfIkQi5.mjs +253 -0
  41. package/dist/openai-responses-contracts-XpZJxrRG.d.mts +68 -0
  42. package/dist/openai-responses-payload-policy-BDxV-W0c.mjs +206 -0
  43. package/dist/openai-responses-payload-policy-BSs371VM.d.mts +40 -0
  44. package/dist/openai-responses-prompt-observer-internal-DgNTYnRY.mjs +46 -0
  45. package/dist/{openai-responses-stream-internal-Cw5txaGW.mjs → openai-responses-shared-DXIt3iY5.mjs} +1406 -940
  46. package/dist/{openai-reasoning-compat-YgeLncHw.mjs → openai-stop-reason-BkFkqqK0.mjs} +204 -37
  47. package/dist/openai-tool-projection-CY04OcvQ.mjs +338 -0
  48. package/dist/provider-error-BUwEnjXq.mjs +429 -0
  49. package/dist/provider-error-CzNw4BWX.d.mts +12 -0
  50. package/dist/{provider-options-D8bB3z9b.d.mts → provider-options-B96RdNpH.d.mts} +9 -3
  51. package/dist/provider-transcript-transform-ePx-Bbfr.mjs +155 -0
  52. package/dist/provider-types.d.mts +31 -0
  53. package/dist/provider-types.mjs +8 -0
  54. package/dist/providers.d.mts +1 -1
  55. package/dist/providers.mjs +17 -19
  56. package/dist/{reasoning-tag-text-partitioner-CGDyLWUR.mjs → reasoning-tag-text-partitioner-rnPwX2pg.mjs} +14 -8
  57. package/dist/record-coerce-DdXsgUd_.mjs +23 -0
  58. package/dist/sanitize-unicode-BYqrYtC_.mjs +90 -0
  59. package/dist/session-resources-CkR4WWy1.mjs +21 -0
  60. package/dist/simple-options-D58D5Kvw.mjs +117 -0
  61. package/dist/src-D2H6yKkH.mjs +2 -0
  62. package/dist/{stream-first-event-timeout-BBys9hSb.mjs → stream-first-event-timeout-MK28puvq.mjs} +2 -2
  63. package/dist/string-coerce-fsri9iCu.mjs +34 -0
  64. package/dist/{tool-schema-json-projection-BwNu3nDi.mjs → tool-schema-json-projection-q5d7QX5c.mjs} +32 -7
  65. package/dist/transport-utils-DJqkxbhC.mjs +138 -0
  66. package/dist/transports.d.mts +166 -241
  67. package/dist/transports.mjs +1979 -1825
  68. package/dist/types-BDdaOVi2.mjs +6 -0
  69. package/dist/{types-bzp5k29J.d.mts → types-BHNrPS1l.d.mts} +23 -1
  70. package/dist/types.d.mts +4 -4
  71. package/dist/types.mjs +6 -4
  72. package/dist/utf16-slice-CvGodqok.mjs +29 -0
  73. package/dist/{validation-DAa_yFOM.mjs → validation-B61OhAio.mjs} +6 -6
  74. package/dist/{validation-B-j7cOYp.d.mts → validation-DT9SrFn3.d.mts} +1 -1
  75. package/dist/validation.d.mts +1 -1
  76. package/dist/validation.mjs +1 -1
  77. package/package.json +15 -1
  78. package/dist/anthropic-usage-DWU-x8MI.mjs +0 -459
  79. package/dist/error-coercion-DgxlWC0n.mjs +0 -15
  80. package/dist/headers-B_e4-1J0.mjs +0 -9
  81. package/dist/host-Dog2WQiR.mjs +0 -369
  82. package/dist/number-coercion-DvG7SNMg.mjs +0 -129
  83. package/dist/openai-chatgpt-jwt-DhAAzLkj.mjs +0 -39
  84. package/dist/openai-responses-shared-pXl6Wd8S.mjs +0 -392
  85. package/dist/openai-tool-projection-OhX64DoP.mjs +0 -215
  86. package/dist/provider-error-CAEvRjry.mjs +0 -47
  87. package/dist/sanitize-unicode-DT5o51ur.mjs +0 -26
  88. package/dist/simple-options-9lhRrN73.mjs +0 -50
  89. package/dist/tool-result-text-CTpIRbYd.mjs +0 -225
  90. package/dist/transform-messages-C8mBqZxF.mjs +0 -2
  91. package/dist/transport-stream-shared-D81p90xq.mjs +0 -297
@@ -0,0 +1,840 @@
1
+ import { o as resolveClaudeFable5ModelIdentity, s as resolveClaudeModelIdentity, u as resolveClaudeOpus5ModelIdentity } from "./anthropic-JsNA5KCu.mjs";
2
+ import { i as normalizeOptionalLowercaseString, r as normalizeNullableString } from "./string-coerce-fsri9iCu.mjs";
3
+ import { n as getAiTransportHost } from "./host-DTqNc7ad.mjs";
4
+ import { a as isRecord } from "./record-coerce-DdXsgUd_.mjs";
5
+ import { a as estimateBase64DecodedBytes } from "./provider-error-BUwEnjXq.mjs";
6
+ import { r as asNonNegativeFiniteNumber, s as parseStrictPositiveInteger } from "./number-coercion-H9qHik3g.mjs";
7
+ import { t as resolveCacheRetention } from "./cache-retention-0x979a5V.mjs";
8
+ import { c as splitSystemPromptCacheBoundary, f as sortPromptCacheToolsByName, l as stripSystemPromptCacheBoundary } from "./simple-options-D58D5Kvw.mjs";
9
+ import { a as resolveProviderEndpoint, o as resolveProviderRequestCapabilities, t as projectRuntimeToolInputSchema } from "./tool-schema-json-projection-q5d7QX5c.mjs";
10
+ import { t as shortHash } from "./hash-CHgqbJmD.mjs";
11
+ //#region packages/ai/src/transports/anthropic-payload-policy.ts
12
+ /**
13
+ * Anthropic-family request payload policy helpers.
14
+ * Applies service-tier and cache-control markers only when provider endpoint
15
+ * capabilities allow them.
16
+ */
17
+ const ANTHROPIC_CACHE_CONTROL_LIMIT = 4;
18
+ const ANTHROPIC_COMPACT_THRESHOLD_MIN = 5e4;
19
+ function parsePositiveInteger(value) {
20
+ if (typeof value === "number" && Number.isFinite(value) && value > 0) return Math.floor(value);
21
+ return typeof value === "string" ? parseStrictPositiveInteger(value) : void 0;
22
+ }
23
+ /** Resolve the Anthropic input-token trigger, including the API's minimum. */
24
+ function resolveAnthropicCompactThreshold(contextWindow, configured) {
25
+ const configuredThreshold = parsePositiveInteger(configured);
26
+ if (configuredThreshold !== void 0) return Math.max(ANTHROPIC_COMPACT_THRESHOLD_MIN, configuredThreshold);
27
+ const resolvedContextWindow = parsePositiveInteger(contextWindow);
28
+ return Math.max(ANTHROPIC_COMPACT_THRESHOLD_MIN, resolvedContextWindow === void 0 ? ANTHROPIC_COMPACT_THRESHOLD_MIN : Math.floor(resolvedContextWindow * .7));
29
+ }
30
+ /** Resolve the server-compaction gate and effective threshold for an Anthropic route. */
31
+ function resolveAnthropicServerCompactionPlan(model, extraParams) {
32
+ const provider = normalizeOptionalLowercaseString(model.provider);
33
+ const api = normalizeOptionalLowercaseString(model.api);
34
+ const endpointClass = resolveProviderEndpoint(model.baseUrl).endpointClass;
35
+ const enabled = extraParams?.anthropicServerCompaction === true && provider === "anthropic" && api === "anthropic-messages" && (endpointClass === "default" || endpointClass === "anthropic-public");
36
+ return {
37
+ enabled,
38
+ ...enabled ? { threshold: resolveAnthropicCompactThreshold(model.contextWindow, extraParams?.anthropicCompactThreshold) } : {}
39
+ };
40
+ }
41
+ function resolveBaseUrlHostname(baseUrl) {
42
+ try {
43
+ return new URL(baseUrl).hostname;
44
+ } catch {
45
+ return;
46
+ }
47
+ }
48
+ function isLongTtlEligibleEndpoint(baseUrl) {
49
+ if (typeof baseUrl !== "string") return false;
50
+ const hostname = resolveBaseUrlHostname(baseUrl);
51
+ if (!hostname) return false;
52
+ 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");
53
+ }
54
+ /** Resolve Anthropic cache-control marker retention for a request endpoint. */
55
+ function resolveAnthropicEphemeralCacheControl(baseUrl, cacheRetention) {
56
+ const retention = resolveCacheRetention(cacheRetention);
57
+ if (retention === "none") return;
58
+ const ttl = retention === "long" && (cacheRetention === "long" || isLongTtlEligibleEndpoint(baseUrl)) ? "1h" : void 0;
59
+ return {
60
+ type: "ephemeral",
61
+ ...ttl ? { ttl } : {}
62
+ };
63
+ }
64
+ function applyAnthropicCacheControlToSystem(system, cacheControl) {
65
+ if (!Array.isArray(system)) return;
66
+ const normalizedBlocks = [];
67
+ for (const block of system) {
68
+ if (!block || typeof block !== "object") {
69
+ normalizedBlocks.push(block);
70
+ continue;
71
+ }
72
+ const record = block;
73
+ if (record.type !== "text" || typeof record.text !== "string") {
74
+ normalizedBlocks.push(block);
75
+ continue;
76
+ }
77
+ const split = splitSystemPromptCacheBoundary(record.text);
78
+ if (!split) {
79
+ if (record.cache_control === void 0) record.cache_control = cacheControl;
80
+ normalizedBlocks.push(record);
81
+ continue;
82
+ }
83
+ const { cache_control: existingCacheControl, ...rest } = record;
84
+ if (split.stablePrefix) normalizedBlocks.push({
85
+ ...rest,
86
+ text: split.stablePrefix,
87
+ cache_control: existingCacheControl ?? cacheControl
88
+ });
89
+ if (split.dynamicSuffix) normalizedBlocks.push({
90
+ ...rest,
91
+ text: split.dynamicSuffix
92
+ });
93
+ }
94
+ system.splice(0, system.length, ...normalizedBlocks);
95
+ }
96
+ function stripAnthropicSystemPromptBoundary(system) {
97
+ if (!Array.isArray(system)) return;
98
+ for (const block of system) {
99
+ if (!block || typeof block !== "object") continue;
100
+ const record = block;
101
+ if (record.type === "text" && typeof record.text === "string") record.text = stripSystemPromptCacheBoundary(record.text);
102
+ }
103
+ }
104
+ /** Apply one shared deepest-stable-message cache breakpoint policy. */
105
+ function applyAnthropicCacheControlToMessages(messages, cacheControl, markerLimit, cacheBreakpointOptOutMessageIndexes) {
106
+ if (!Array.isArray(messages) || messages.length === 0 || markerLimit <= 0) return;
107
+ let fallbackToolResult;
108
+ for (let i = messages.length - 1; i >= 0; i--) {
109
+ const message = messages[i];
110
+ if (!message || typeof message !== "object") continue;
111
+ const record = message;
112
+ if (record.role !== "user" || cacheBreakpointOptOutMessageIndexes.has(i)) continue;
113
+ const content = record.content;
114
+ if (typeof content === "string") {
115
+ if (fallbackToolResult && markerLimit === 1) {
116
+ fallbackToolResult.cache_control = cacheControl;
117
+ return;
118
+ }
119
+ record.content = [{
120
+ type: "text",
121
+ text: content,
122
+ cache_control: cacheControl
123
+ }];
124
+ if (fallbackToolResult && markerLimit > 1) fallbackToolResult.cache_control = cacheControl;
125
+ return;
126
+ }
127
+ if (!Array.isArray(content)) continue;
128
+ for (let j = content.length - 1; j >= 0; j--) {
129
+ const block = content[j];
130
+ if (!block || typeof block !== "object") continue;
131
+ const blockRecord = block;
132
+ if (blockRecord.type === "text" || blockRecord.type === "image") {
133
+ if (fallbackToolResult && markerLimit === 1) {
134
+ fallbackToolResult.cache_control = cacheControl;
135
+ return;
136
+ }
137
+ blockRecord.cache_control = cacheControl;
138
+ if (fallbackToolResult && markerLimit > 1) fallbackToolResult.cache_control = cacheControl;
139
+ return;
140
+ }
141
+ if (blockRecord.type === "tool_result" && fallbackToolResult === void 0) fallbackToolResult = blockRecord;
142
+ }
143
+ }
144
+ if (fallbackToolResult) fallbackToolResult.cache_control = cacheControl;
145
+ }
146
+ function countAnthropicCacheControlMarkers(blocks) {
147
+ if (!Array.isArray(blocks)) return 0;
148
+ let count = 0;
149
+ for (const block of blocks) if (block && typeof block === "object" && "cache_control" in block) count += 1;
150
+ return count;
151
+ }
152
+ /** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
153
+ function resolveAnthropicPayloadPolicy(input) {
154
+ const capabilities = resolveProviderRequestCapabilities({
155
+ provider: input.provider,
156
+ api: input.api,
157
+ baseUrl: input.baseUrl,
158
+ capability: "llm",
159
+ transport: "stream"
160
+ });
161
+ const serverCompactionPlan = resolveAnthropicServerCompactionPlan(input, input.extraParams);
162
+ return {
163
+ allowsServiceTier: capabilities.allowsAnthropicServiceTier,
164
+ cacheControl: input.enableCacheControl === true ? resolveAnthropicEphemeralCacheControl(input.baseUrl, input.cacheRetention) : void 0,
165
+ compactThreshold: serverCompactionPlan.threshold ?? resolveAnthropicCompactThreshold(input.contextWindow, input.extraParams?.anthropicCompactThreshold),
166
+ serviceTier: input.serviceTier,
167
+ useServerCompaction: input.enableServerCompaction === true && serverCompactionPlan.enabled
168
+ };
169
+ }
170
+ /** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
171
+ function applyAnthropicPayloadPolicyToParams(payloadObj, policy, cacheBreakpointOptOutMessageIndexes) {
172
+ if (policy.allowsServiceTier && policy.serviceTier !== void 0 && payloadObj.service_tier === void 0) payloadObj.service_tier = policy.serviceTier;
173
+ if (policy.cacheControl) applyAnthropicCacheControlToSystem(payloadObj.system, policy.cacheControl);
174
+ else stripAnthropicSystemPromptBoundary(payloadObj.system);
175
+ if (policy.useServerCompaction && payloadObj.context_management === void 0) payloadObj.context_management = { edits: [{
176
+ type: "compact_20260112",
177
+ trigger: {
178
+ type: "input_tokens",
179
+ value: policy.compactThreshold
180
+ }
181
+ }] };
182
+ if (!policy.cacheControl) return;
183
+ const usedMarkers = countAnthropicCacheControlMarkers(payloadObj.system) + countAnthropicCacheControlMarkers(payloadObj.tools);
184
+ applyAnthropicCacheControlToMessages(payloadObj.messages, policy.cacheControl, ANTHROPIC_CACHE_CONTROL_LIMIT - usedMarkers, cacheBreakpointOptOutMessageIndexes);
185
+ }
186
+ /** @deprecated Anthropic-family provider payload helper; do not use from third-party plugins. */
187
+ function applyAnthropicEphemeralCacheControlMarkers(payloadObj, cacheControl = { type: "ephemeral" }) {
188
+ const messages = payloadObj.messages;
189
+ if (!Array.isArray(messages)) return;
190
+ for (const message of messages) {
191
+ if (message.role === "system" || message.role === "developer") {
192
+ if (!cacheControl) continue;
193
+ if (typeof message.content === "string") {
194
+ message.content = [{
195
+ type: "text",
196
+ text: message.content,
197
+ cache_control: cacheControl
198
+ }];
199
+ continue;
200
+ }
201
+ if (Array.isArray(message.content) && message.content.length > 0) {
202
+ const last = message.content[message.content.length - 1];
203
+ if (last && typeof last === "object") {
204
+ const record = last;
205
+ if (record.type !== "thinking" && record.type !== "redacted_thinking") record.cache_control = cacheControl;
206
+ }
207
+ }
208
+ continue;
209
+ }
210
+ if (message.role === "assistant" && Array.isArray(message.content)) for (const block of message.content) {
211
+ if (!block || typeof block !== "object") continue;
212
+ const record = block;
213
+ if (record.type === "thinking" || record.type === "redacted_thinking") delete record.cache_control;
214
+ }
215
+ }
216
+ }
217
+ //#endregion
218
+ //#region packages/ai/src/internal/anthropic-inline-images.ts
219
+ const ANTHROPIC_IMAGE_MEDIA_TYPE_SET = /* @__PURE__ */ new Set([
220
+ "image/jpeg",
221
+ "image/png",
222
+ "image/gif",
223
+ "image/webp"
224
+ ]);
225
+ const ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES = 64 * 1024 * 1024;
226
+ function createAnthropicInlineImageBudget() {
227
+ return { totalBytes: 0 };
228
+ }
229
+ function resolveAnthropicImageMediaType(value) {
230
+ if (ANTHROPIC_IMAGE_MEDIA_TYPE_SET.has(value)) return value;
231
+ throw new Error(`Unsupported Anthropic image media type after normalization: ${value}`);
232
+ }
233
+ async function normalizeAnthropicInlineContent(content, budget) {
234
+ if (!content.some((block) => block.type === "image")) return content.filter((block) => block.type === "text");
235
+ const inputBytes = content.reduce((total, block) => block.type === "image" ? total + estimateBase64DecodedBytes(block.data) : total, 0);
236
+ if (budget.totalBytes + inputBytes > ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES) throw new Error("Anthropic inline images exceed the 64 MB aggregate decoded safety limit.");
237
+ const normalized = [];
238
+ for (const block of content) {
239
+ if (block.type !== "image") {
240
+ normalized.push(block);
241
+ continue;
242
+ }
243
+ const normalizedBlocks = await getAiTransportHost().normalizeAnthropicInlineContentBlocks([block]);
244
+ const outputBytes = normalizedBlocks.reduce((total, normalizedBlock) => normalizedBlock.type === "image" ? total + estimateBase64DecodedBytes(normalizedBlock.data) : total, 0);
245
+ if (budget.totalBytes + outputBytes > ANTHROPIC_INLINE_IMAGES_DECODE_SAFETY_BYTES) throw new Error("Anthropic inline images exceed the 64 MB aggregate decoded safety limit.");
246
+ budget.totalBytes += outputBytes;
247
+ normalized.push(...normalizedBlocks);
248
+ }
249
+ return normalized;
250
+ }
251
+ //#endregion
252
+ //#region packages/ai/src/providers/anthropic-auth-headers.ts
253
+ function usesFoundryBearerAuth(model) {
254
+ return model.provider === "microsoft-foundry" && (model.authHeader === true || hasBearerAuthorizationHeader(model.headers));
255
+ }
256
+ function hasBearerAuthorizationHeader(headers) {
257
+ if (!headers) return false;
258
+ return Object.entries(headers).some(([key, value]) => key.toLowerCase() === "authorization" && /^bearer\s+\S+/i.test(value.trim()));
259
+ }
260
+ function omitFoundryBearerCredentialHeaders(headers) {
261
+ if (!headers) return;
262
+ const next = {};
263
+ for (const [key, value] of Object.entries(headers)) {
264
+ const lower = key.toLowerCase();
265
+ if (lower === "authorization" || lower === "x-api-key" || lower === "api-key") continue;
266
+ next[key] = value;
267
+ }
268
+ return Object.keys(next).length > 0 ? next : void 0;
269
+ }
270
+ //#endregion
271
+ //#region packages/ai/src/providers/anthropic-refusal.ts
272
+ function readAnthropicRefusalDetails(value) {
273
+ if (!value || typeof value !== "object") return {
274
+ category: null,
275
+ explanation: null
276
+ };
277
+ const details = value;
278
+ return {
279
+ category: normalizeNullableString(details.category),
280
+ explanation: normalizeNullableString(details.explanation)
281
+ };
282
+ }
283
+ function formatAnthropicRefusalMessage(details) {
284
+ return `Anthropic refusal${details.category ? ` (category: ${details.category})` : ""}${details.explanation ? `: ${details.explanation}` : "."}`;
285
+ }
286
+ function applyAnthropicRefusal(output, stopDetails, provider) {
287
+ const details = readAnthropicRefusalDetails(stopDetails);
288
+ output.stopReason = "error";
289
+ output.errorMessage = formatAnthropicRefusalMessage(details);
290
+ output.diagnostics = [...output.diagnostics ?? [], {
291
+ type: "provider_refusal",
292
+ timestamp: Date.now(),
293
+ details: {
294
+ provider,
295
+ category: details.category,
296
+ explanation: details.explanation
297
+ }
298
+ }];
299
+ }
300
+ //#endregion
301
+ //#region packages/ai/src/providers/anthropic-server-fallback.ts
302
+ /** Anthropic beta that re-serves safety refusals on an allowed fallback model. */
303
+ const ANTHROPIC_SERVER_SIDE_FALLBACK_BETA = "server-side-fallback-2026-07-01";
304
+ /** Let Anthropic select the recommended model for each refusal category. */
305
+ const ANTHROPIC_SERVER_SIDE_FALLBACKS = "default";
306
+ const CLAUDE_OPUS_FALLBACK_MODEL_COST = {
307
+ input: 5,
308
+ output: 25,
309
+ cacheRead: .5,
310
+ cacheWrite: 6.25
311
+ };
312
+ function resolveFallbackModelIdentity(modelId) {
313
+ if (!modelId?.trim()) return null;
314
+ const ref = { id: modelId };
315
+ const normalized = resolveClaudeModelIdentity(ref);
316
+ if (normalized === "opus" || normalized === "opus-5" || resolveClaudeOpus5ModelIdentity(ref)) return "claude-opus-5";
317
+ if (resolveClaudeFable5ModelIdentity(ref)) return "claude-fable-5";
318
+ if (/^claude-opus-4-8(?=$|[^a-z0-9])/.test(normalized)) return "claude-opus-4-8";
319
+ return normalized || null;
320
+ }
321
+ function isClaudeOpusFallbackModel(modelId) {
322
+ return modelId === "claude-opus-5" || modelId === "claude-opus-4-8";
323
+ }
324
+ /** Resolve billed rates from the serving model reported by Anthropic's fallback stream. */
325
+ function resolveAnthropicFallbackServingModelCost(params) {
326
+ const requestedModelId = resolveFallbackModelIdentity(params.requestedModelId);
327
+ const servingModelId = resolveFallbackModelIdentity(params.servingModelId);
328
+ if (!servingModelId || servingModelId === requestedModelId || !isClaudeOpusFallbackModel(servingModelId)) return params.requestedCost;
329
+ if (requestedModelId && isClaudeOpusFallbackModel(requestedModelId)) return params.requestedCost;
330
+ return CLAUDE_OPUS_FALLBACK_MODEL_COST;
331
+ }
332
+ function readBoundaryModel(value) {
333
+ if (!value || typeof value !== "object") return null;
334
+ const model = value.model;
335
+ return typeof model === "string" && model.trim() ? model : null;
336
+ }
337
+ /** Reads a `fallback` content block marking where one model's output gives way to the next. */
338
+ function readAnthropicFallbackBoundary(block) {
339
+ if (!block || typeof block !== "object") return null;
340
+ const record = block;
341
+ if (record.type !== "fallback") return null;
342
+ return {
343
+ fromModel: readBoundaryModel(record.from),
344
+ toModel: readBoundaryModel(record.to)
345
+ };
346
+ }
347
+ /**
348
+ * Drops pre-fallback thinking/tool calls while preserving the text prefix that
349
+ * the serving model continued. Dropped tool calls must never execute or replay.
350
+ */
351
+ function applyAnthropicFallbackBoundary(params) {
352
+ const { output, boundary } = params;
353
+ const survivors = output.content.filter((block) => block.type === "text");
354
+ for (const survivor of survivors) delete survivor.textSignature;
355
+ output.content.splice(0, output.content.length, ...survivors);
356
+ if (boundary.toModel) output.responseModel = boundary.toModel;
357
+ output.diagnostics = [...output.diagnostics ?? [], {
358
+ type: "provider_fallback",
359
+ timestamp: Date.now(),
360
+ details: {
361
+ provider: params.provider,
362
+ fromModel: boundary.fromModel,
363
+ toModel: boundary.toModel
364
+ }
365
+ }];
366
+ }
367
+ //#endregion
368
+ //#region packages/ai/src/providers/anthropic-thinking-replay.ts
369
+ const ANTHROPIC_OMITTED_REASONING_TEXT = "[assistant reasoning omitted]";
370
+ function asReplayMessage(value) {
371
+ return value && typeof value === "object" ? value : void 0;
372
+ }
373
+ /**
374
+ * Anthropic tool results continue the preceding assistant turn. Preserve that
375
+ * turn's signed thinking even when the next request disables new thinking.
376
+ */
377
+ function findActiveAnthropicToolTurnAssistantIndex(messages) {
378
+ const toolResultIds = /* @__PURE__ */ new Set();
379
+ let index = messages.length - 1;
380
+ while (index >= 0) {
381
+ const message = asReplayMessage(messages[index]);
382
+ if (message?.role !== "toolResult") break;
383
+ if (typeof message.toolCallId === "string") toolResultIds.add(message.toolCallId);
384
+ index -= 1;
385
+ }
386
+ if (toolResultIds.size === 0) return -1;
387
+ const assistant = asReplayMessage(messages[index]);
388
+ if (assistant?.role !== "assistant" || !Array.isArray(assistant.content)) return -1;
389
+ const toolCallIds = /* @__PURE__ */ new Set();
390
+ for (const block of assistant.content) {
391
+ if (!block || typeof block !== "object") continue;
392
+ const record = block;
393
+ if ((record.type === "toolCall" || record.type === "tool_use" || record.type === "function_call") && typeof record.id === "string") toolCallIds.add(record.id);
394
+ }
395
+ return [...toolResultIds].every((toolCallId) => toolCallIds.has(toolCallId)) ? index : -1;
396
+ }
397
+ //#endregion
398
+ //#region packages/ai/src/providers/anthropic-tool-projection.ts
399
+ const CLAUDE_CODE_TOOL_LOOKUP = new Map([
400
+ "Read",
401
+ "Write",
402
+ "Edit",
403
+ "Bash",
404
+ "Grep",
405
+ "Glob",
406
+ "AskUserQuestion",
407
+ "EnterPlanMode",
408
+ "ExitPlanMode",
409
+ "KillShell",
410
+ "NotebookEdit",
411
+ "Skill",
412
+ "Task",
413
+ "TaskOutput",
414
+ "TodoWrite",
415
+ "WebFetch",
416
+ "WebSearch"
417
+ ].map((name) => [name.toLowerCase(), name]));
418
+ /** Preserve Claude Code's canonical tool casing for subscription OAuth requests. */
419
+ function toClaudeCodeToolName(name) {
420
+ return CLAUDE_CODE_TOOL_LOOKUP.get(name.toLowerCase()) ?? name;
421
+ }
422
+ /** Anthropic rejects forced tools while extended thinking is enabled. */
423
+ function normalizeAnthropicToolChoice(thinkingEnabled, toolChoice) {
424
+ if (thinkingEnabled && (toolChoice === "any" || typeof toolChoice === "object" && toolChoice.type === "tool")) return { type: "auto" };
425
+ return typeof toolChoice === "string" ? { type: toolChoice } : toolChoice;
426
+ }
427
+ /** Anthropic tool identifiers accept only ASCII word characters and dashes. */
428
+ function normalizeAnthropicToolCallId(id) {
429
+ return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
430
+ }
431
+ function isProviderSupportedViolation(violation) {
432
+ return violation.endsWith(".$dynamicRef") || violation.endsWith(".$dynamicAnchor");
433
+ }
434
+ const schemaValueKeywords = /* @__PURE__ */ new Set([
435
+ "additionalProperties",
436
+ "contains",
437
+ "contentSchema",
438
+ "else",
439
+ "if",
440
+ "items",
441
+ "not",
442
+ "propertyNames",
443
+ "then",
444
+ "unevaluatedItems",
445
+ "unevaluatedProperties"
446
+ ]);
447
+ const schemaArrayKeywords = /* @__PURE__ */ new Set([
448
+ "allOf",
449
+ "anyOf",
450
+ "oneOf",
451
+ "prefixItems"
452
+ ]);
453
+ const schemaMapKeywords = /* @__PURE__ */ new Set([
454
+ "$defs",
455
+ "definitions",
456
+ "dependencies",
457
+ "dependentSchemas",
458
+ "patternProperties",
459
+ "properties"
460
+ ]);
461
+ function normalizeAnthropicJsonSchema(schema) {
462
+ if (!isRecord(schema)) return schema;
463
+ let changed = false;
464
+ const normalized = { ...schema };
465
+ for (const [key, value] of Object.entries(schema)) {
466
+ if (schemaValueKeywords.has(key) && !Array.isArray(value)) {
467
+ const next = normalizeAnthropicJsonSchema(value);
468
+ normalized[key] = next;
469
+ changed ||= next !== value;
470
+ continue;
471
+ }
472
+ if (schemaArrayKeywords.has(key) && Array.isArray(value)) {
473
+ const next = value.map(normalizeAnthropicJsonSchema);
474
+ normalized[key] = next;
475
+ changed ||= next.some((entry, index) => entry !== value[index]);
476
+ continue;
477
+ }
478
+ if (schemaMapKeywords.has(key) && isRecord(value)) {
479
+ const next = Object.fromEntries(Object.entries(value).map(([entryKey, entryValue]) => [entryKey, normalizeAnthropicJsonSchema(entryValue)]));
480
+ normalized[key] = next;
481
+ changed ||= Object.entries(value).some(([entryKey, entryValue]) => next[entryKey] !== entryValue);
482
+ }
483
+ }
484
+ if (Array.isArray(schema.items)) {
485
+ normalized.prefixItems = schema.items.map(normalizeAnthropicJsonSchema);
486
+ const additionalItems = schema.additionalItems;
487
+ if (typeof additionalItems === "boolean" || isRecord(additionalItems)) normalized.items = normalizeAnthropicJsonSchema(additionalItems);
488
+ else delete normalized.items;
489
+ delete normalized.additionalItems;
490
+ changed = true;
491
+ }
492
+ return changed ? normalized : schema;
493
+ }
494
+ /** Snapshots direct/custom tool descriptors before Anthropic payload construction. */
495
+ function projectAnthropicTools(tools, toWireName) {
496
+ const projectedTools = [];
497
+ const unavailableOriginalNames = /* @__PURE__ */ new Set();
498
+ for (const tool of tools) {
499
+ let projectedTool;
500
+ let originalName;
501
+ try {
502
+ const name = tool.name;
503
+ originalName = name;
504
+ if (!name) continue;
505
+ const schemaProjection = projectRuntimeToolInputSchema(tool.parameters, `${name}.parameters`);
506
+ if (!isRecord(schemaProjection.schema) || schemaProjection.violations.some((violation) => !isProviderSupportedViolation(violation))) {
507
+ unavailableOriginalNames.add(name);
508
+ continue;
509
+ }
510
+ const anthropicSchema = normalizeAnthropicJsonSchema(schemaProjection.schema);
511
+ if (!isRecord(anthropicSchema)) {
512
+ unavailableOriginalNames.add(name);
513
+ continue;
514
+ }
515
+ const properties = anthropicSchema.properties;
516
+ const required = anthropicSchema.required;
517
+ if (properties !== void 0 && properties !== null && !isRecord(properties) || required !== void 0 && required !== null && (!Array.isArray(required) || required.some((entry) => typeof entry !== "string"))) {
518
+ unavailableOriginalNames.add(name);
519
+ continue;
520
+ }
521
+ let description;
522
+ try {
523
+ description = typeof tool.description === "string" ? tool.description : void 0;
524
+ } catch {}
525
+ projectedTool = {
526
+ originalName: name,
527
+ wireName: toWireName(name),
528
+ ...description ? { description } : {},
529
+ inputSchema: {
530
+ type: "object",
531
+ properties: properties ?? {},
532
+ required: required ?? []
533
+ }
534
+ };
535
+ } catch {
536
+ if (originalName) unavailableOriginalNames.add(originalName);
537
+ continue;
538
+ }
539
+ const conflictingTool = projectedTools.find((entry) => entry.wireName === projectedTool.wireName);
540
+ if (conflictingTool && conflictingTool.originalName !== projectedTool.originalName) throw new Error(`Anthropic tool names "${conflictingTool.originalName}" and "${projectedTool.originalName}" both map to "${projectedTool.wireName}"`);
541
+ projectedTools.push(projectedTool);
542
+ }
543
+ return {
544
+ inputToolCount: tools.length,
545
+ unavailableOriginalNames,
546
+ tools: sortPromptCacheToolsByName(projectedTools)
547
+ };
548
+ }
549
+ /** Keeps forced Anthropic tool choices aligned with the projected wire names. */
550
+ function reconcileAnthropicToolChoice(choice, projection) {
551
+ if (projection.inputToolCount === 0) return choice;
552
+ if (choice.type === "tool") {
553
+ const requestedName = choice.name;
554
+ const originalMatch = projection.tools.find((tool) => tool.originalName === requestedName);
555
+ if (originalMatch) return {
556
+ ...choice,
557
+ name: originalMatch.wireName
558
+ };
559
+ if (projection.unavailableOriginalNames.has(requestedName)) throw new Error(`Anthropic tool_choice requested unavailable tool "${requestedName}" after schema conversion`);
560
+ const matchedTool = projection.tools.find((tool) => tool.wireName === requestedName);
561
+ if (!matchedTool) throw new Error(`Anthropic tool_choice requested unavailable tool "${requestedName}" after schema conversion`);
562
+ return {
563
+ ...choice,
564
+ name: matchedTool.wireName
565
+ };
566
+ }
567
+ if (projection.tools.length === 0) {
568
+ if (choice.type === "auto") return;
569
+ if (choice.type === "any") throw new Error("Anthropic tool_choice requires a tool, but no tools survived schema conversion");
570
+ }
571
+ return choice;
572
+ }
573
+ /** Maps Claude Code wire names without trusting every direct/custom descriptor. */
574
+ function resolveOriginalAnthropicToolName(name, projection) {
575
+ return projection?.tools.find((tool) => tool.wireName === name)?.originalName ?? name;
576
+ }
577
+ //#endregion
578
+ //#region packages/ai/src/providers/anthropic-usage.ts
579
+ function readAnthropicUsageTokenCount(value) {
580
+ return asNonNegativeFiniteNumber(value);
581
+ }
582
+ function readAnthropicCacheWriteUsage(usage) {
583
+ if (!usage.cache_creation || typeof usage.cache_creation !== "object") return {};
584
+ const cacheCreation = usage.cache_creation;
585
+ const cacheWrite5m = readAnthropicUsageTokenCount(cacheCreation.ephemeral_5m_input_tokens);
586
+ const cacheWrite1h = readAnthropicUsageTokenCount(cacheCreation.ephemeral_1h_input_tokens);
587
+ return {
588
+ ...cacheWrite5m !== void 0 ? { cacheWrite5m } : {},
589
+ ...cacheWrite1h !== void 0 ? { cacheWrite1h } : {}
590
+ };
591
+ }
592
+ function readAnthropicPromptUsageSnapshot(usage) {
593
+ const input = readAnthropicUsageTokenCount(usage.input_tokens);
594
+ const cacheRead = usage.cache_read_input_tokens == null ? 0 : readAnthropicUsageTokenCount(usage.cache_read_input_tokens);
595
+ const cacheWrite = usage.cache_creation_input_tokens == null ? 0 : readAnthropicUsageTokenCount(usage.cache_creation_input_tokens);
596
+ if (input === void 0 || cacheRead === void 0 || cacheWrite === void 0) return;
597
+ return {
598
+ input,
599
+ cacheRead,
600
+ cacheWrite
601
+ };
602
+ }
603
+ function readLastAnthropicIterationUsage(usage) {
604
+ if (usage.iterations == null) return { state: "absent" };
605
+ if (!Array.isArray(usage.iterations) || usage.iterations.length === 0) return { state: "invalid" };
606
+ const iteration = usage.iterations.at(-1);
607
+ if (!iteration || typeof iteration !== "object" || Array.isArray(iteration)) return { state: "invalid" };
608
+ const record = iteration;
609
+ const input = readAnthropicUsageTokenCount(record.input_tokens);
610
+ const cacheRead = readAnthropicUsageTokenCount(record.cache_read_input_tokens);
611
+ const cacheWrite = readAnthropicUsageTokenCount(record.cache_creation_input_tokens);
612
+ const outputTokens = readAnthropicUsageTokenCount(record.output_tokens);
613
+ if (input === void 0 || cacheRead === void 0 || cacheWrite === void 0 || outputTokens === void 0) return { state: "invalid" };
614
+ const contextPromptTokens = input + cacheRead + cacheWrite;
615
+ return {
616
+ state: "valid",
617
+ usage: {
618
+ contextPromptTokens,
619
+ totalTokens: contextPromptTokens + outputTokens
620
+ }
621
+ };
622
+ }
623
+ function readAnthropicCompactionBilledUsage(iterations) {
624
+ if (!Array.isArray(iterations) || iterations.length === 0) return;
625
+ let sawCompaction = false;
626
+ const billed = {
627
+ input: 0,
628
+ output: 0,
629
+ cacheRead: 0,
630
+ cacheWrite: 0,
631
+ cacheWrite1h: 0
632
+ };
633
+ for (const iteration of iterations) {
634
+ if (!iteration || typeof iteration !== "object" || Array.isArray(iteration)) return;
635
+ const record = iteration;
636
+ const input = readAnthropicUsageTokenCount(record.input_tokens);
637
+ const output = readAnthropicUsageTokenCount(record.output_tokens);
638
+ const cacheRead = readAnthropicUsageTokenCount(record.cache_read_input_tokens);
639
+ const cacheWrite = readAnthropicUsageTokenCount(record.cache_creation_input_tokens);
640
+ if (input === void 0 || output === void 0 || cacheRead === void 0 || cacheWrite === void 0) return;
641
+ sawCompaction ||= record.type === "compaction";
642
+ billed.input += input;
643
+ billed.output += output;
644
+ billed.cacheRead += cacheRead;
645
+ billed.cacheWrite += cacheWrite;
646
+ billed.cacheWrite1h += readAnthropicCacheWriteUsage(record).cacheWrite1h ?? 0;
647
+ }
648
+ return sawCompaction ? billed : void 0;
649
+ }
650
+ /** Record independent billing buckets without treating zero placeholders as context proof. */
651
+ function applyAnthropicMessageStartUsage(target, payload) {
652
+ const promptUsage = readAnthropicPromptUsageSnapshot(payload);
653
+ const promptTokens = promptUsage ? promptUsage.input + promptUsage.cacheRead + promptUsage.cacheWrite : 0;
654
+ const inputTokens = readAnthropicUsageTokenCount(payload.input_tokens);
655
+ if (inputTokens !== void 0) target.input = inputTokens;
656
+ const outputTokens = readAnthropicUsageTokenCount(payload.output_tokens);
657
+ if (outputTokens !== void 0) target.output = outputTokens;
658
+ const cacheReadTokens = payload.cache_read_input_tokens == null ? 0 : readAnthropicUsageTokenCount(payload.cache_read_input_tokens);
659
+ if (cacheReadTokens !== void 0) target.cacheRead = cacheReadTokens;
660
+ const cacheWriteTokens = payload.cache_creation_input_tokens == null ? 0 : readAnthropicUsageTokenCount(payload.cache_creation_input_tokens);
661
+ if (cacheWriteTokens !== void 0) target.cacheWrite = cacheWriteTokens;
662
+ const { cacheWrite1h } = readAnthropicCacheWriteUsage(payload);
663
+ if (cacheWrite1h !== void 0) target.cacheWrite1h = cacheWrite1h;
664
+ target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
665
+ if (promptTokens > 0 && outputTokens !== void 0) target.contextUsage = {
666
+ state: "available",
667
+ promptTokens,
668
+ totalTokens: promptTokens + target.output
669
+ };
670
+ return promptTokens > 0 ? promptUsage : void 0;
671
+ }
672
+ /** Keep cumulative billing separate from the final server-side iteration context. */
673
+ function applyAnthropicMessageDeltaUsage(target, payload, messageStartPromptUsage) {
674
+ const usage = payload ?? {};
675
+ const billedIterations = readAnthropicCompactionBilledUsage(usage.iterations);
676
+ const inputTokens = readAnthropicUsageTokenCount(usage.input_tokens);
677
+ const outputTokens = readAnthropicUsageTokenCount(usage.output_tokens);
678
+ const cacheReadTokens = readAnthropicUsageTokenCount(usage.cache_read_input_tokens);
679
+ const cacheWriteTokens = readAnthropicUsageTokenCount(usage.cache_creation_input_tokens);
680
+ const resolved = billedIterations ?? {
681
+ input: inputTokens,
682
+ output: outputTokens,
683
+ cacheRead: cacheReadTokens,
684
+ cacheWrite: cacheWriteTokens,
685
+ cacheWrite1h: readAnthropicCacheWriteUsage(usage).cacheWrite1h
686
+ };
687
+ if (resolved.input !== void 0) target.input = resolved.input;
688
+ if (resolved.output !== void 0) target.output = resolved.output;
689
+ if (resolved.cacheRead !== void 0) target.cacheRead = resolved.cacheRead;
690
+ if (resolved.cacheWrite !== void 0) target.cacheWrite = resolved.cacheWrite;
691
+ if (resolved.cacheWrite1h !== void 0) target.cacheWrite1h = resolved.cacheWrite1h;
692
+ target.totalTokens = target.input + target.output + target.cacheRead + target.cacheWrite;
693
+ const iterationUsage = readLastAnthropicIterationUsage(usage);
694
+ if (iterationUsage.state === "valid") target.contextUsage = {
695
+ state: "available",
696
+ promptTokens: iterationUsage.usage.contextPromptTokens,
697
+ totalTokens: iterationUsage.usage.totalTokens
698
+ };
699
+ else if (iterationUsage.state === "invalid") target.contextUsage = { state: "unavailable" };
700
+ else if (outputTokens !== void 0 && (messageStartPromptUsage !== void 0 || inputTokens !== void 0 && cacheReadTokens !== void 0 && cacheWriteTokens !== void 0)) {
701
+ const promptTokens = target.input + target.cacheRead + target.cacheWrite;
702
+ target.contextUsage = {
703
+ state: "available",
704
+ promptTokens,
705
+ totalTokens: promptTokens + target.output
706
+ };
707
+ } else target.contextUsage = { state: "unavailable" };
708
+ }
709
+ //#endregion
710
+ //#region packages/ai/src/transports/anthropic-compaction-replay.ts
711
+ const ANTHROPIC_COMPACTION_REPLAY_TYPE = "anthropic-compaction";
712
+ const ANTHROPIC_COMPACTION_SUPPRESSION_TYPE = "anthropic-compaction-suppression";
713
+ const ANTHROPIC_COMPACTION_SUPPRESSION_DATA = "rejected";
714
+ function createCompactionCapture(output, model, options) {
715
+ const pending = /* @__PURE__ */ new Map();
716
+ return {
717
+ begin(index, block, replayIndex) {
718
+ if (block?.type !== "compaction") return false;
719
+ const content = typeof block.content === "string" ? block.content : "";
720
+ pending.set(index, {
721
+ content,
722
+ replayIndex
723
+ });
724
+ return true;
725
+ },
726
+ delta(index, delta) {
727
+ const capture = pending.get(index);
728
+ if (!capture || delta?.type !== "compaction_delta") return false;
729
+ capture.content = typeof delta.content === "string" ? delta.content : capture.content;
730
+ return true;
731
+ },
732
+ complete(index) {
733
+ const capture = pending.get(index);
734
+ if (!capture) return false;
735
+ pending.delete(index);
736
+ captureAnthropicCompaction(output, capture.content, capture.replayIndex, model, options);
737
+ return true;
738
+ }
739
+ };
740
+ }
741
+ function hashReplayValue(value) {
742
+ const normalized = value?.trim();
743
+ return normalized ? shortHash(normalized) : void 0;
744
+ }
745
+ function buildAnthropicReplayContext(model, options) {
746
+ return {
747
+ provider: model.provider,
748
+ api: model.api,
749
+ model: model.id,
750
+ baseUrlHash: hashReplayValue(model.baseUrl),
751
+ sessionHash: hashReplayValue(options?.sessionId),
752
+ authProfileHash: hashReplayValue(options?.authProfileId)
753
+ };
754
+ }
755
+ function readAnthropicCompactionState(value) {
756
+ if (!value || typeof value !== "object" || Array.isArray(value)) return;
757
+ const state = value;
758
+ if (!(state.v === 1 && typeof state.data === "string" && typeof state.provider === "string" && typeof state.api === "string" && typeof state.model === "string" && typeof state.baseUrlHash === "string" && (state.sessionHash === void 0 || typeof state.sessionHash === "string") && (state.authProfileHash === void 0 || typeof state.authProfileHash === "string"))) return;
759
+ const replayState = state;
760
+ if (replayState.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) return replayState.data === ANTHROPIC_COMPACTION_SUPPRESSION_DATA ? replayState : void 0;
761
+ if (replayState.type !== ANTHROPIC_COMPACTION_REPLAY_TYPE || replayState.data.length === 0 || replayState.replayIndex !== void 0 && (!Number.isSafeInteger(replayState.replayIndex) || replayState.replayIndex < 0)) return;
762
+ return replayState;
763
+ }
764
+ function replayContextMatches(state, context) {
765
+ return state.provider === context.provider && state.api === context.api && state.model === context.model && state.baseUrlHash === context.baseUrlHash && state.sessionHash === context.sessionHash && state.authProfileHash === context.authProfileHash;
766
+ }
767
+ function captureAnthropicCompaction(output, summary, replayIndex, model, options) {
768
+ const context = buildAnthropicReplayContext(model, options);
769
+ if (!summary || !context.baseUrlHash || !Number.isSafeInteger(replayIndex) || replayIndex < 0) return;
770
+ output.providerReplay = {
771
+ v: 1,
772
+ type: ANTHROPIC_COMPACTION_REPLAY_TYPE,
773
+ data: summary,
774
+ replayIndex,
775
+ ...context,
776
+ baseUrlHash: context.baseUrlHash
777
+ };
778
+ }
779
+ function suppressAnthropicCompaction(output, model, options) {
780
+ const context = buildAnthropicReplayContext(model, options);
781
+ if (!context.baseUrlHash) return;
782
+ output.providerReplay = {
783
+ v: 1,
784
+ type: ANTHROPIC_COMPACTION_SUPPRESSION_TYPE,
785
+ data: ANTHROPIC_COMPACTION_SUPPRESSION_DATA,
786
+ ...context,
787
+ baseUrlHash: context.baseUrlHash
788
+ };
789
+ }
790
+ function resolveNewestAnthropicCompaction(messages, model, options) {
791
+ const context = buildAnthropicReplayContext(model, options);
792
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
793
+ const message = messages[index];
794
+ if (message?.role !== "assistant") continue;
795
+ const replayState = readAnthropicCompactionState(message.providerReplay);
796
+ if (replayState?.type === ANTHROPIC_COMPACTION_SUPPRESSION_TYPE) {
797
+ if (replayContextMatches(replayState, context)) return;
798
+ continue;
799
+ }
800
+ if (message.providerReplay?.type !== ANTHROPIC_COMPACTION_REPLAY_TYPE) continue;
801
+ if (!replayState) return;
802
+ if (!replayContextMatches(replayState, context)) return;
803
+ return {
804
+ owner: message,
805
+ replayIndex: replayState.replayIndex ?? 0,
806
+ summary: replayState.data
807
+ };
808
+ }
809
+ }
810
+ function buildAnthropicReplayPlan(messages, model, options) {
811
+ if (options?.enabled !== true) return { messages };
812
+ const checkpoint = resolveNewestAnthropicCompaction(messages, model, options);
813
+ if (!checkpoint) return { messages };
814
+ const ownerIndex = messages.indexOf(checkpoint.owner);
815
+ return {
816
+ messages: [{
817
+ ...checkpoint.owner,
818
+ content: checkpoint.owner.content.slice(checkpoint.replayIndex)
819
+ }, ...messages.slice(ownerIndex + 1)],
820
+ compaction: {
821
+ type: "compaction",
822
+ content: checkpoint.summary
823
+ }
824
+ };
825
+ }
826
+ /** Match Anthropic's 400 response when a replay block is no longer accepted. */
827
+ function isAnthropicReplayRejection(error) {
828
+ if (!error || typeof error !== "object") return false;
829
+ const record = error;
830
+ const nested = record.error && typeof record.error === "object" && !Array.isArray(record.error) ? record.error : void 0;
831
+ const text = [
832
+ record.message,
833
+ record.code,
834
+ nested?.message,
835
+ nested?.code
836
+ ].filter((value) => typeof value === "string").join(" ").toLowerCase();
837
+ return (record.status === 400 || text.startsWith("http 400:")) && (text.includes("compaction") || text.includes("context_management"));
838
+ }
839
+ //#endregion
840
+ export { resolveAnthropicImageMediaType as A, readAnthropicFallbackBoundary as C, usesFoundryBearerAuth as D, omitFoundryBearerCredentialHeaders as E, resolveAnthropicPayloadPolicy as F, resolveAnthropicServerCompactionPlan as I, applyAnthropicEphemeralCacheControlMarkers as M, applyAnthropicPayloadPolicyToParams as N, createAnthropicInlineImageBudget as O, resolveAnthropicEphemeralCacheControl as P, applyAnthropicFallbackBoundary as S, applyAnthropicRefusal as T, ANTHROPIC_OMITTED_REASONING_TEXT as _, applyAnthropicMessageDeltaUsage as a, ANTHROPIC_SERVER_SIDE_FALLBACK_BETA as b, readAnthropicPromptUsageSnapshot as c, normalizeAnthropicToolCallId as d, normalizeAnthropicToolChoice as f, toClaudeCodeToolName as g, resolveOriginalAnthropicToolName as h, suppressAnthropicCompaction as i, applyAnthropicCacheControlToMessages as j, normalizeAnthropicInlineContent as k, readAnthropicUsageTokenCount as l, reconcileAnthropicToolChoice as m, createCompactionCapture as n, applyAnthropicMessageStartUsage as o, projectAnthropicTools as p, isAnthropicReplayRejection as r, readAnthropicCacheWriteUsage as s, buildAnthropicReplayPlan as t, readLastAnthropicIterationUsage as u, findActiveAnthropicToolTurnAssistantIndex as v, resolveAnthropicFallbackServingModelCost as w, CLAUDE_OPUS_FALLBACK_MODEL_COST as x, ANTHROPIC_SERVER_SIDE_FALLBACKS as y };