@xiaoso/dsh-tool-plus 0.1.16 → 0.1.17-beta.0

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 (109) hide show
  1. package/lib/{abort-DZ54CaKU.mjs → abort-BTblp0Rt.mjs} +2 -2
  2. package/lib/{alibaba-coding-plan-DODpkcqi.mjs → alibaba-coding-plan-t3R4YKCB.mjs} +3 -3
  3. package/lib/{alibaba-token-plan-C6lpKCda.mjs → alibaba-token-plan-CO4djG1y.mjs} +4 -4
  4. package/lib/{alibaba-token-plan-CHGyWOfX.mjs → alibaba-token-plan-CUjYJX0-.mjs} +1 -1
  5. package/lib/{anthropic-Dv_GGX6g.mjs → anthropic-nf7w6so1.mjs} +48 -21
  6. package/lib/{api-key-validation-MnMf_ihX.mjs → api-key-validation-C_tZ7Y9a.mjs} +2 -2
  7. package/lib/behavior-BwvTUtag.mjs +111 -0
  8. package/lib/{browser-render-BZXt548F.mjs → browser-render-BQaEVK_n.mjs} +4 -4
  9. package/lib/{reference-CtvM4zLU.mjs → bundled-references-CrQ4dQcT.mjs} +57 -4
  10. package/lib/cascade-BWjRTD1q.mjs +30265 -0
  11. package/lib/{cline-pass-BT9CBEOj.mjs → cline-pass-Bjm-O8GG.mjs} +2 -2
  12. package/lib/cloudflare-CKXRJ0l9.mjs +37 -0
  13. package/lib/{cloudflare-ai-gateway-DReN6ohJ.mjs → cloudflare-ai-gateway-BpnBSjwV.mjs} +8 -4
  14. package/lib/cloudflare-workers-ai-odj9XswL.mjs +32 -0
  15. package/lib/collapse-_nkb2dn7.mjs +1945 -0
  16. package/lib/{coreweave-C82HrLpb.mjs → coreweave-CGDZOD8-.mjs} +1 -1
  17. package/lib/{coreweave-C68UgRQO.mjs → coreweave-DwffcgW5.mjs} +4 -4
  18. package/lib/cursor-BgOuV7Qf.mjs +794 -0
  19. package/lib/{cursor-_gHHq-Dq.mjs → cursor-CF1iHHbv.mjs} +68 -19
  20. package/lib/{cursor-proto-Cr_WEMuv.mjs → cursor-proto-BuNTjX3L.mjs} +823 -23
  21. package/lib/{devin-QpfTSl9H.mjs → devin-7OEeEb80.mjs} +112 -31
  22. package/lib/{devin-proto-DYhp4Ty6.mjs → devin-proto-C-QreeAv.mjs} +71 -5
  23. package/lib/{dom-DG3sPESc.mjs → dom-BmbZv4v9.mjs} +598 -182
  24. package/lib/{error-O63pC8zg.mjs → error-8LVo7tug.mjs} +725 -104
  25. package/lib/factory-droid-ChhmsGWb.mjs +71 -0
  26. package/lib/{fs-promises-shim-B2YFWkcd.mjs → fs-promises-shim-CANAxFWh.mjs} +10 -1
  27. package/lib/{github-copilot-BMEcw8N1.mjs → github-copilot-QwevdT30.mjs} +4 -4
  28. package/lib/{github-copilot-headers-DnjGEbko.mjs → github-copilot-headers-CAUZ0rUD.mjs} +138 -26
  29. package/lib/{gitlab-duo-DnHqJjFY.mjs → gitlab-duo-DigV7k_o.mjs} +2 -2
  30. package/lib/{google-antigravity-B1mSckrK.mjs → google-antigravity-D-oe0r8A.mjs} +6 -7
  31. package/lib/{google-gemini-cli-DJwThCCn.mjs → google-gemini-cli-CQBHgGJT.mjs} +5 -6
  32. package/lib/{google-oauth-shared-BctLpVUN.mjs → google-oauth-shared-CvgVIUwJ.mjs} +2 -2
  33. package/lib/google-validation-CdvRj4ec.mjs +126 -0
  34. package/lib/id-EJwVLDHE.mjs +2548 -0
  35. package/lib/index.mjs +248 -258
  36. package/lib/{kilo-BEgEwMJQ.mjs → kilo-5r6OffCa.mjs} +2 -2
  37. package/lib/{kimi-T9NldEIJ.mjs → kimi-DOOtbXmm.mjs} +9 -10
  38. package/lib/{launch-BIWNyrBB.mjs → launch-DkAjib7h.mjs} +1 -1
  39. package/lib/{markit-Dvp2JPdT.mjs → markit-CCcyeAds.mjs} +359 -140
  40. package/lib/{models-DnXMYGcJ.mjs → models-DCpDT_im.mjs} +199203 -112275
  41. package/lib/{muse-code-CNvqqAp6.mjs → muse-code-CYf8zj_B.mjs} +5 -5
  42. package/lib/{openai-codex-D5-CyTxa.mjs → openai-codex-Cj-TvSFh.mjs} +13 -10
  43. package/lib/{perplexity-A2PEoGaC.mjs → perplexity-Dh_Qgsmx.mjs} +68 -25
  44. package/lib/{pkce-BYjmugmw.mjs → pkce-DO-jBJLV.mjs} +1 -1
  45. package/lib/pricing-DusBEhwm.mjs +593 -0
  46. package/lib/{readability-uGhqb4GC.mjs → readability-XHK0o3bv.mjs} +287 -27
  47. package/lib/{scrapers-C1ujjV8k.mjs → scrapers-B6PADOVm.mjs} +13 -13
  48. package/lib/slugify-ynYWBIA5.mjs +11 -0
  49. package/lib/snowflake-B_jap6lz.mjs +666 -0
  50. package/lib/snowflake-Cu1ZyOp6.mjs +151 -0
  51. package/lib/{src-AELUUb5U.mjs → src-BhinCSpi.mjs} +1172 -458
  52. package/lib/{stream-AVOoe5Hj.mjs → stream-D2ivg3GH.mjs} +3740 -5311
  53. package/lib/{turndown-D6fof1OX.mjs → turndown-Vya8SavL.mjs} +82 -30
  54. package/lib/types/tools/hashline/native/index.d.ts +2 -2
  55. package/lib/types/tools/hashline/native/policy.d.ts +19 -21
  56. package/lib/types/tools/omp/session/agent-storage.d.ts +3 -3
  57. package/lib/types/tools/shared/bun-named-shim.d.ts +5 -0
  58. package/lib/{types-BLrLohwK.mjs → types-tH1v9uRm.mjs} +2 -2
  59. package/lib/{utils-cZX3f_Ls.mjs → utils-C_Ncya1r.mjs} +1737 -509
  60. package/lib/utils-D2tK38xq.mjs +34006 -0
  61. package/lib/{utils-I4I8azHN.mjs → utils-DrDwmNtu.mjs} +8 -3
  62. package/lib/{xai-oauth-CbeldUSV.mjs → xai-oauth-DLLzX6my.mjs} +4 -4
  63. package/lib/{xiaomi-Dl3VSs04.mjs → xiaomi-CqK-KivK.mjs} +2 -2
  64. package/lib/{zai-FyjGSypr.mjs → zai-DoJ2xPIH.mjs} +2 -2
  65. package/lib/{zip-DRrycScC.mjs → zip-DG1mUV6X.mjs} +1 -1
  66. package/package.json +8 -8
  67. package/lib/amazon-bedrock-Dx0rr9dY.mjs +0 -996
  68. package/lib/anthropic-ClCw0PV_.mjs +0 -3624
  69. package/lib/anthropic-wire-lbGAFzYb.mjs +0 -927
  70. package/lib/azure-openai-responses-WE_bgFWp.mjs +0 -285
  71. package/lib/bundled-references-DY3EJxZV.mjs +0 -56
  72. package/lib/cloudflare-ai-gateway-CrMa97Ap.mjs +0 -44
  73. package/lib/collapse-CkMNRNr7.mjs +0 -2304
  74. package/lib/cursor-C6jaIJDg.mjs +0 -283
  75. package/lib/cursor-D-XZYDf_.mjs +0 -4544
  76. package/lib/deterministic-id-CUEIMSqR.mjs +0 -17
  77. package/lib/devin-ExGIghmx.mjs +0 -69
  78. package/lib/devin-u0ljw5P6.mjs +0 -628
  79. package/lib/effort-BdtQMN-_.mjs +0 -23
  80. package/lib/event-stream-BKEtb45Y.mjs +0 -167
  81. package/lib/gemini-headers-Co9cVvex.mjs +0 -54
  82. package/lib/google-auth-qAN-6DMW.mjs +0 -232
  83. package/lib/google-gemini-cli-B2Aw1S5w.mjs +0 -844
  84. package/lib/google-lTxuq28n.mjs +0 -38
  85. package/lib/google-shared-Y01j9xOr.mjs +0 -856
  86. package/lib/google-validation-BEkNs-Bx.mjs +0 -18
  87. package/lib/google-vertex-DcM-FRC5.mjs +0 -99
  88. package/lib/hosts-00rjRzUD.mjs +0 -203
  89. package/lib/id-AL5obQzR.mjs +0 -71
  90. package/lib/idle-iterator-D0ZXEcUq.mjs +0 -366
  91. package/lib/ollama-6R0oXZks.mjs +0 -574
  92. package/lib/openai-codex-responses-C8bnBWOe.mjs +0 -3232
  93. package/lib/openai-completions-DNr3mUsn.mjs +0 -1430
  94. package/lib/openai-configuration-update-xMYUjaK_.mjs +0 -150
  95. package/lib/openai-reasoning-fallback-Ju-5pSR8.mjs +0 -380
  96. package/lib/openai-responses-CjpafAEr.mjs +0 -861
  97. package/lib/protobuf-BMgDYu_U.mjs +0 -717
  98. package/lib/provider-response-SqKSkkUc.mjs +0 -22
  99. package/lib/proxy-DxvCQOCA.mjs +0 -230
  100. package/lib/request-debug-BwPOqN_j.mjs +0 -183
  101. package/lib/sanitize-text-BCf5KmCf.mjs +0 -33
  102. package/lib/sse-debug-B-tkLCPW.mjs +0 -11
  103. package/lib/stream-markup-healing-CN3RsqG9.mjs +0 -267
  104. package/lib/strict-tool-validation-CBTz1dAA.mjs +0 -105
  105. package/lib/taxonomy-DSIM99rS.mjs +0 -15392
  106. package/lib/thinking-ngMwf_1Y.mjs +0 -336
  107. package/lib/type-guards-CttAANEx.mjs +0 -11
  108. package/lib/utils-CE3DSebh.mjs +0 -11763
  109. package/lib/vision-guard-BCpXWUj3.mjs +0 -77
@@ -1,3624 +0,0 @@
1
- import { installBunShim as __installBunShim } from "./bun-shim.mjs";
2
- __installBunShim();
3
- import { r as __exportAll } from "./fs-promises-shim-B2YFWkcd.mjs";
4
- import { $ as isEnoent, C as warn, N as $env, a as readSseEvents, b as debug, g as parseJsonWithRepair, q as getInstallId, v as parseStreamingJsonThrottled } from "./src-AELUUb5U.mjs";
5
- import { A as isTransientStreamParseError, D as isFastModeUnsupported, F as AnthropicStreamEnvelopeError, M as AnthropicApiError, N as AnthropicConnectionError, O as isGrammarError, P as AnthropicConnectionTimeoutError, _ as AbortError, a as isTransientStatus, c as finalize, d as getRetryAfterMsFromHeaders, i as isProviderRetryableError, k as isStreamEnvelopeError, l as finalizeErrorMessage, n as StreamTimeoutError, r as ValidationError, s as ProviderResponseError, t as ConfigurationError, u as getHeadersFromError } from "./error-O63pC8zg.mjs";
6
- import { A as toolWireSchema, B as spillToDescription, C as kStreamingLastParseLen, T as renderDemotedThinking, V as COMBINATOR_KEYS, b as kStreamingBlockIndex, c as resolveCacheRetention, g as clearStreamingPartialJson, h as transformMessages, j as NO_STRICT, n as getHeaderCaseInsensitive, o as normalizeSystemPrompts, s as normalizeToolCallId, w as kStreamingPartialJson } from "./utils-cZX3f_Ls.mjs";
7
- import { n as isRecord } from "./type-guards-CttAANEx.mjs";
8
- import { At as withReplaySafeStreamRetry, Ct as applyInferenceHeaders, et as getOpenAIPromptCacheKey, f as isFoundryEnabled, n as getEnvApiKey, t as OUTPUT_FALLBACK_BUFFER, wt as isForcedToolChoice } from "./stream-AVOoe5Hj.mjs";
9
- import { r as isAnthropicOAuthToken } from "./utils-I4I8azHN.mjs";
10
- import { a as mapEffortToAnthropicAdaptiveEffort, n as isAnthropicServerToolHistoryBlock, r as parseAnthropicInputTransformations, t as THINKING_BINDING_CONTROLS_BETA } from "./anthropic-wire-lbGAFzYb.mjs";
11
- import { t as createAbortSourceTracker } from "./abort-DZ54CaKU.mjs";
12
- import { n as getBundledModel, t as calculateCost } from "./models-DnXMYGcJ.mjs";
13
- import { a as resolveGitHubCopilotBaseUrl, b as parseGitHubCopilotApiKey, i as resolveCopilotRequestIdentity, n as hasCopilotVisionInput, o as wrapFetchForCopilotFallback, t as buildCopilotDynamicHeaders } from "./github-copilot-headers-DnjGEbko.mjs";
14
- import { t as NON_VISION_IMAGE_PLACEHOLDER } from "./vision-guard-BCpXWUj3.mjs";
15
- import { t as AssistantMessageEventStream } from "./event-stream-BKEtb45Y.mjs";
16
- import { a as claudeCodeUserAgent, i as claudeCodeSystemInstruction, n as CLAUDE_CODE_MAX_OUTPUT_TOKENS, o as claudeCodeVersion, r as claudeCodeSdkVersion } from "./anthropic-Dv_GGX6g.mjs";
17
- import { n as isVertexRawPredictUrl, t as hostMatchesUrl } from "./hosts-00rjRzUD.mjs";
18
- import { a as isOfficialAnthropicApiUrl, i as isAnthropicSigningProxyUrl } from "./collapse-CkMNRNr7.mjs";
19
- import { a as getStreamIdleTimeoutMs, i as getStreamFirstEventTimeoutMs, o as iterateWithIdleTimeout } from "./idle-iterator-D0ZXEcUq.mjs";
20
- import { t as notifyProviderResponse } from "./provider-response-SqKSkkUc.mjs";
21
- import { t as notifyRawSseEvent } from "./sse-debug-B-tkLCPW.mjs";
22
- import * as fs from "node:fs";
23
- import { scheduler } from "node:timers/promises";
24
- import * as nodeCrypto from "node:crypto";
25
- import * as tls from "node:tls";
26
- //#region node_modules/.pnpm/@oh-my-pi+pi-ai@18.1.17/node_modules/@oh-my-pi/pi-ai/src/utils/sdk-stream-timeout.ts
27
- /**
28
- * Shared helpers for mapping `StreamOptions.streamFirstEventTimeoutMs` onto
29
- * underlying SDK request-timeout options.
30
- *
31
- * The hint is intentionally not a watchdog — it just narrows the SDK's
32
- * "transport timeout" window so a stuck pre-stream request fails fast
33
- * instead of hanging on the default (often multi-minute) SDK timeout. Once
34
- * the stream actually starts, silence is not failure; callers must abort
35
- * to interrupt a quiet stream.
36
- */
37
- /**
38
- * Coerce a caller-supplied `streamFirstEventTimeoutMs` into a positive integer suitable
39
- * for the SDK's `timeout` option. Returns `undefined` when the caller passed nothing,
40
- * a non-finite value, or a non-positive value (preserving the SDK's default).
41
- */
42
- function resolveSdkTimeoutMs(streamFirstEventTimeoutMs) {
43
- if (streamFirstEventTimeoutMs === void 0) return void 0;
44
- if (!Number.isFinite(streamFirstEventTimeoutMs)) return void 0;
45
- if (streamFirstEventTimeoutMs <= 0) return void 0;
46
- return Math.trunc(streamFirstEventTimeoutMs);
47
- }
48
- /**
49
- * Build per-request SDK options that combine an abort signal with the optional
50
- * `streamFirstEventTimeoutMs` request-timeout hint.
51
- *
52
- * The returned `{ signal, timeout?, maxRetries? }` shape is compatible with both
53
- * OpenAI's and Anthropic's `RequestOptions` (and any other SDK that follows the
54
- * Stainless conventions), so callers from any of those providers can spread the
55
- * result directly into `client.X.create(params, requestOptions)`.
56
- *
57
- * When the hint is set, retries are forced to zero so the SDK does not silently
58
- * extend the caller's explicit deadline by re-attempting after a timeout.
59
- */
60
- function createSdkStreamRequestOptions(signal, streamFirstEventTimeoutMs) {
61
- const timeout = resolveSdkTimeoutMs(streamFirstEventTimeoutMs);
62
- if (timeout === void 0) return { signal };
63
- return {
64
- signal,
65
- timeout,
66
- maxRetries: 0
67
- };
68
- }
69
- //#endregion
70
- //#region node_modules/.pnpm/@oh-my-pi+pi-ai@18.1.17/node_modules/@oh-my-pi/pi-ai/src/providers/anthropic-client.ts
71
- /**
72
- * Minimal HTTP client for the Anthropic Messages API.
73
- *
74
- * pi-ai builds every request header itself (`buildAnthropicHeaders`), serializes
75
- * the body itself (`buildParams`), and parses SSE frames itself
76
- * (`iterateAnthropicEvents`), so the only `@anthropic-ai/sdk` surface this
77
- * package ever exercised was URL assembly, auth-header injection, bounded
78
- * retries, the pre-response timeout, and HTTP-error-to-status mapping. This
79
- * module implements exactly that surface and nothing else.
80
- *
81
- * Behavioral contract (kept compatible with the SDK so downstream error
82
- * classification keeps working):
83
- * - Non-2xx responses throw {@link AnthropicApiError} whose `status` property
84
- * carries the HTTP status and whose message is `"<status> <body>"`.
85
- * - Pre-response timeouts throw {@link AnthropicConnectionTimeoutError}
86
- * ("Request timed out.").
87
- * - Caller aborts throw an `Error` with message "Request was aborted.".
88
- * - Retries: connection errors and 408/409/429/5xx (or `x-should-retry: true`)
89
- * are retried up to `maxRetries` times, honoring `retry-after-ms` /
90
- * `retry-after`, otherwise exponential backoff (0.5s * 2^n, capped at 8s,
91
- * with up to 25% jitter).
92
- */
93
- /** Default pre-response timeout, matching the SDK's 10-minute default. */
94
- const DEFAULT_TIMEOUT_MS = 6e5;
95
- /** Default retry budget, matching the SDK's default. */
96
- const DEFAULT_MAX_RETRIES = 2;
97
- const INITIAL_RETRY_DELAY_S = .5;
98
- const MAX_RETRY_DELAY_S = 8;
99
- function createAbortError() {
100
- return new AbortError("Request was aborted.");
101
- }
102
- /** `x-should-retry` override, then 408/409/429/5xx. */
103
- function shouldRetryResponse(response) {
104
- const shouldRetryHeader = response.headers.get("x-should-retry");
105
- if (shouldRetryHeader === "true") return true;
106
- if (shouldRetryHeader === "false") return false;
107
- const status = response.status;
108
- return isTransientStatus(status) || status === 409;
109
- }
110
- /** Server-suggested delay (`retry-after-ms`, then `retry-after` seconds or HTTP date). */
111
- function retryDelayFromHeaders(headers) {
112
- if (!headers) return void 0;
113
- const retryAfterMs = headers.get("retry-after-ms");
114
- if (retryAfterMs) {
115
- const ms = Number.parseFloat(retryAfterMs);
116
- if (Number.isFinite(ms) && ms >= 0) return ms;
117
- }
118
- const retryAfter = headers.get("retry-after");
119
- if (retryAfter) {
120
- const seconds = Number.parseFloat(retryAfter);
121
- if (Number.isFinite(seconds) && seconds >= 0) return seconds * 1e3;
122
- const dateMs = Date.parse(retryAfter) - Date.now();
123
- if (Number.isFinite(dateMs) && dateMs >= 0) return dateMs;
124
- }
125
- }
126
- function calculateAnthropicRetryDelayMs(attempt) {
127
- return Math.min(INITIAL_RETRY_DELAY_S * 2 ** attempt, MAX_RETRY_DELAY_S) * (1 - Math.random() * .25) * 1e3;
128
- }
129
- function hasHeaderCaseInsensitive(headers, lowerName) {
130
- for (const key in headers) if (key.toLowerCase() === lowerName) return true;
131
- return false;
132
- }
133
- /**
134
- * Lazy in-flight request handle. The HTTP request starts on the first
135
- * `asResponse()` call; subsequent calls return the same promise.
136
- *
137
- * Shape-compatible with the SDK's `APIPromise.asResponse()` so
138
- * `getAnthropicStreamResponse` treats internal and injected clients uniformly.
139
- */
140
- var AnthropicApiRequest = class {
141
- #start;
142
- #response;
143
- constructor(start) {
144
- this.#start = start;
145
- }
146
- asResponse() {
147
- this.#response ??= this.#start();
148
- return this.#response;
149
- }
150
- };
151
- /**
152
- * `messages` resource. `create` lives on the prototype so tests can intercept
153
- * every outgoing request with `vi.spyOn(AnthropicMessages.prototype, "create")`.
154
- */
155
- var AnthropicMessages = class {
156
- #client;
157
- #path;
158
- constructor(client, path) {
159
- this.#client = client;
160
- this.#path = path;
161
- }
162
- create(params, options) {
163
- return this.#client.request(this.#path, params, options);
164
- }
165
- };
166
- var AnthropicMessagesClient = class {
167
- messages;
168
- beta;
169
- #options;
170
- constructor(options) {
171
- this.#options = options;
172
- this.messages = new AnthropicMessages(this, "/v1/messages");
173
- this.beta = { messages: new AnthropicMessages(this, "/v1/messages?beta=true") };
174
- }
175
- request(path, params, options) {
176
- return new AnthropicApiRequest(() => this.#send(path, params, options));
177
- }
178
- #buildHeaders(requestHeaders) {
179
- const opts = this.#options;
180
- const defaults = opts.defaultHeaders ?? {};
181
- const headers = {};
182
- if (opts.apiKey != null && !hasHeaderCaseInsensitive(defaults, "x-api-key")) headers["X-Api-Key"] = opts.apiKey;
183
- if (opts.authToken != null && !hasHeaderCaseInsensitive(defaults, "authorization")) headers.Authorization = `Bearer ${opts.authToken}`;
184
- Object.assign(headers, defaults);
185
- Object.assign(headers, requestHeaders);
186
- return headers;
187
- }
188
- async #send(path, params, options) {
189
- const opts = this.#options;
190
- const fetchFn = opts.fetch ?? fetch;
191
- const callerSignal = options?.signal;
192
- const timeoutMs = options?.timeout ?? opts.timeout ?? DEFAULT_TIMEOUT_MS;
193
- const maxRetries = Math.max(0, options?.maxRetries ?? opts.maxRetries ?? DEFAULT_MAX_RETRIES);
194
- const maxRetryDelayMs = options?.maxRetryDelayMs ?? opts.maxRetryDelayMs ?? 6e4;
195
- const url = `${opts.baseURL ?? "https://api.anthropic.com"}${path}`;
196
- const headers = this.#buildHeaders(options?.headers);
197
- const body = JSON.stringify(params);
198
- for (let attempt = 0;; attempt++) {
199
- if (callerSignal?.aborted) throw createAbortError();
200
- let response;
201
- try {
202
- response = await this.#fetchOnce(fetchFn, url, headers, body, timeoutMs, callerSignal);
203
- } catch (error) {
204
- if (callerSignal?.aborted) throw createAbortError();
205
- if (attempt < maxRetries) {
206
- await this.#backoff(attempt, void 0, callerSignal);
207
- continue;
208
- }
209
- if (error instanceof AnthropicConnectionTimeoutError) throw error;
210
- throw new AnthropicConnectionError(error);
211
- }
212
- if (response.ok) return response;
213
- if (attempt < maxRetries && shouldRetryResponse(response)) {
214
- const headerDelayMs = retryDelayFromHeaders(response.headers);
215
- if (headerDelayMs !== void 0 && maxRetryDelayMs > 0 && headerDelayMs > maxRetryDelayMs) throw await AnthropicApiError.fromResponse(response, callerSignal);
216
- await response.body?.cancel().catch(() => {});
217
- await this.#backoff(attempt, response.headers, callerSignal);
218
- continue;
219
- }
220
- throw await AnthropicApiError.fromResponse(response, callerSignal);
221
- }
222
- }
223
- async #fetchOnce(fetchFn, url, headers, body, timeoutMs, callerSignal) {
224
- const controller = new AbortController();
225
- let timedOut = false;
226
- const timer = setTimeout(() => {
227
- timedOut = true;
228
- controller.abort();
229
- }, timeoutMs);
230
- const onAbort = () => controller.abort();
231
- callerSignal?.addEventListener("abort", onAbort, { once: true });
232
- try {
233
- return await fetchFn(url, {
234
- ...this.#options.fetchOptions,
235
- method: "POST",
236
- headers,
237
- body,
238
- signal: controller.signal
239
- });
240
- } catch (error) {
241
- if (timedOut && !callerSignal?.aborted) throw new AnthropicConnectionTimeoutError();
242
- throw error;
243
- } finally {
244
- clearTimeout(timer);
245
- callerSignal?.removeEventListener("abort", onAbort);
246
- }
247
- }
248
- async #backoff(attempt, responseHeaders, signal) {
249
- const delayMs = retryDelayFromHeaders(responseHeaders) ?? calculateAnthropicRetryDelayMs(attempt);
250
- try {
251
- await scheduler.wait(delayMs, { signal });
252
- } catch {
253
- throw createAbortError();
254
- }
255
- }
256
- };
257
- //#endregion
258
- //#region node_modules/.pnpm/@oh-my-pi+pi-ai@18.1.17/node_modules/@oh-my-pi/pi-ai/src/providers/anthropic.ts
259
- var anthropic_exports = /* @__PURE__ */ __exportAll({
260
- CLAUDE_CODE_MAX_OUTPUT_TOKENS: () => CLAUDE_CODE_MAX_OUTPUT_TOKENS,
261
- applyAnthropicUsageExtras: () => applyAnthropicUsageExtras,
262
- applyClaudeToolPrefix: () => applyClaudeToolPrefix,
263
- buildAnthropicClientOptions: () => buildAnthropicClientOptions,
264
- buildAnthropicHeaders: () => buildAnthropicHeaders,
265
- buildAnthropicSystemBlocks: () => buildAnthropicSystemBlocks,
266
- buildBetaHeader: () => buildBetaHeader,
267
- claudeCodeHeaders: () => claudeCodeHeaders,
268
- claudeCodeSdkVersion: () => claudeCodeSdkVersion,
269
- claudeCodeSystemInstruction: () => claudeCodeSystemInstruction,
270
- claudeCodeUserAgent: () => claudeCodeUserAgent,
271
- claudeCodeVersion: () => claudeCodeVersion,
272
- claudeToolPrefix: () => "_",
273
- convertAnthropicMessages: () => convertAnthropicMessages,
274
- deriveClaudeDeviceId: () => deriveClaudeDeviceId,
275
- isClaudeCloakingUserId: () => isClaudeCloakingUserId,
276
- isInvalidThinkingSignatureError: () => isInvalidThinkingSignatureError,
277
- isThinkingPrefixBindingError: () => isThinkingPrefixBindingError,
278
- mapStainlessArch: () => mapStainlessArch,
279
- mapStainlessOs: () => mapStainlessOs,
280
- maybeAddReplayUnsignedThinkingHint: () => maybeAddReplayUnsignedThinkingHint,
281
- normalizeAnthropicBaseUrl: () => normalizeAnthropicBaseUrl,
282
- normalizeAnthropicToolSchema: () => normalizeAnthropicToolSchema,
283
- normalizeExtraBetas: () => normalizeExtraBetas,
284
- resolveAnthropicCustomHeadersForBaseUrl: () => resolveAnthropicCustomHeadersForBaseUrl,
285
- resolveAnthropicMetadataUserId: () => resolveAnthropicMetadataUserId,
286
- streamAnthropic: () => streamAnthropic,
287
- stripClaudeToolPrefix: () => stripClaudeToolPrefix,
288
- wrapFetchForCch: () => wrapFetchForCch
289
- });
290
- function normalizeAnthropicBaseUrl(baseUrl) {
291
- const trimmed = baseUrl?.trim();
292
- if (!trimmed) return;
293
- const withoutTrailingSlashes = trimmed.replace(/\/+$/, "");
294
- return withoutTrailingSlashes.endsWith("/v1") ? withoutTrailingSlashes.slice(0, -3) : withoutTrailingSlashes;
295
- }
296
- function buildBetaHeader(baseBetas, extraBetas) {
297
- const seen = /* @__PURE__ */ new Set();
298
- const result = [];
299
- for (const beta of [...baseBetas, ...extraBetas]) {
300
- const trimmed = beta.trim();
301
- if (trimmed && !seen.has(trimmed)) {
302
- seen.add(trimmed);
303
- result.push(trimmed);
304
- }
305
- }
306
- return result.join(",");
307
- }
308
- /**
309
- * Merge an extra Anthropic beta into a caller-provided `anthropic-beta` header,
310
- * preserving the caller's key casing and deduping the tokens. Returns a
311
- * single-entry header record for a per-request `headers` override — used to
312
- * attach a required beta to injected SDK clients that bypass the client-level
313
- * beta construction.
314
- */
315
- function mergeAnthropicBetaHeader(callerHeaders, beta) {
316
- for (const key in callerHeaders) if (key.toLowerCase() === "anthropic-beta") return { [key]: buildBetaHeader(normalizeExtraBetas(callerHeaders[key]), [beta]) };
317
- return { "anthropic-beta": beta };
318
- }
319
- const oauthAuthBeta = "oauth-2025-04-20";
320
- const midConversationSystemBeta = "mid-conversation-system-2026-04-07";
321
- const midConversationSystemClearAtBeta = "mid-conversation-system-clear-at-2026-08-21";
322
- const midConversationToolChangesBeta = "mid-conversation-tool-changes-2026-07-01";
323
- const midConversationOutputConfigBeta = "mid-conversation-output-config-2026-07-01";
324
- const contextManagementBeta = "context-management-2025-06-27";
325
- const structuredOutputsBeta = "structured-outputs-2025-12-15";
326
- const thinkingTokenCountBeta = "thinking-token-count-2026-05-13";
327
- const fallbackCreditBeta = "fallback-credit-2026-06-01";
328
- const claudeCodeUtilityBetaDefaults = [
329
- oauthAuthBeta,
330
- "interleaved-thinking-2025-05-14",
331
- thinkingTokenCountBeta,
332
- contextManagementBeta,
333
- "prompt-caching-scope-2026-01-05",
334
- structuredOutputsBeta
335
- ];
336
- const claudeCodeAgentBetaDefaults = [
337
- "claude-code-20250219",
338
- oauthAuthBeta,
339
- "interleaved-thinking-2025-05-14",
340
- thinkingTokenCountBeta,
341
- contextManagementBeta,
342
- "prompt-caching-scope-2026-01-05",
343
- midConversationSystemBeta
344
- ];
345
- const extendedCacheTtlBeta = "extended-cache-ttl-2025-04-11";
346
- const fineGrainedToolStreamingBeta = "fine-grained-tool-streaming-2025-05-14";
347
- const interleavedThinkingBeta = "interleaved-thinking-2025-05-14";
348
- const fastModeBeta = "fast-mode-2026-02-01";
349
- const taskBudgetBeta = "task-budgets-2026-03-13";
350
- const effortBeta = "effort-2025-11-24";
351
- const serverSideFallbackBeta = "server-side-fallback-2026-06-01";
352
- function resolveAnthropicControlBetas(model, prefixMismatchBehavior) {
353
- const betas = [];
354
- if (prefixMismatchBehavior) betas.push(THINKING_BINDING_CONTROLS_BETA);
355
- if (model.compat.supportsTurnScopedSystem) betas.push(midConversationSystemClearAtBeta);
356
- if (model.compat.supportsMidConversationToolChanges) betas.push(midConversationToolChangesBeta);
357
- if (model.compat.supportsPerMessageEffort) betas.push(midConversationOutputConfigBeta);
358
- return betas;
359
- }
360
- function buildClaudeCodeBetas({ agentRequest, thinkingRequest, disableStrictTools = false, supportsContextManagement = true }) {
361
- if (!agentRequest && !disableStrictTools && supportsContextManagement) return claudeCodeUtilityBetaDefaults;
362
- const betas = [];
363
- for (const beta of agentRequest ? claudeCodeAgentBetaDefaults : claudeCodeUtilityBetaDefaults) {
364
- if (disableStrictTools && beta === structuredOutputsBeta) continue;
365
- if (!supportsContextManagement && beta === contextManagementBeta) continue;
366
- betas.push(beta);
367
- }
368
- if (!agentRequest) return betas;
369
- if (thinkingRequest) betas.push(effortBeta);
370
- betas.push(fallbackCreditBeta);
371
- return betas;
372
- }
373
- function isClaudeCodeClientUserAgent(userAgent) {
374
- if (!userAgent) return false;
375
- return userAgent.toLowerCase().startsWith("claude-cli");
376
- }
377
- const sharedHeaders = {
378
- "Accept-Encoding": "gzip, deflate, br, zstd",
379
- Connection: "keep-alive",
380
- "Content-Type": "application/json",
381
- "anthropic-version": "2023-06-01",
382
- "anthropic-dangerous-direct-browser-access": "true",
383
- "x-app": "cli"
384
- };
385
- function buildAnthropicHeaders(options) {
386
- const oauthToken = options.isOAuth ?? isAnthropicOAuthToken(options.apiKey);
387
- const extraBetas = options.extraBetas ?? [];
388
- const stream = options.stream ?? false;
389
- const incomingUserAgent = getHeaderCaseInsensitive(options.modelHeaders, "User-Agent");
390
- const incomingAuthorization = getHeaderCaseInsensitive(options.modelHeaders, "Authorization");
391
- const incomingApiKey = getHeaderCaseInsensitive(options.modelHeaders, "X-Api-Key");
392
- const betaHeader = buildBetaHeader(options.claudeCodeBetas ?? (oauthToken ? buildClaudeCodeBetas({
393
- agentRequest: true,
394
- thinkingRequest: true
395
- }) : []), extraBetas);
396
- const acceptHeader = oauthToken ? "application/json" : stream ? "text/event-stream" : "application/json";
397
- const isCloudflare = options.isCloudflareAiGateway ?? false;
398
- const honorAuthorization = !oauthToken && !isCloudflare;
399
- const allowAnthropicHeaderOverrides = oauthToken && options.allowAnthropicHeaderOverrides === true && !isCloudflare && !isOfficialAnthropicApiUrl(options.baseUrl);
400
- const honorApiKey = !isCloudflare;
401
- const modelHeaders = {};
402
- const anthropicHeaderOverrides = {};
403
- const filteredEnforcedKeys = [];
404
- const headerSource = options.modelHeaders;
405
- if (headerSource) for (const key in headerSource) {
406
- const value = headerSource[key];
407
- const lowerKey = key.toLowerCase();
408
- if (enforcedHeaderKeys.has(lowerKey)) {
409
- if (allowAnthropicHeaderOverrides && overridableAnthropicHeaderKeys.has(lowerKey)) {
410
- anthropicHeaderOverrides[key] = value;
411
- continue;
412
- }
413
- if (lowerKey === "user-agent") continue;
414
- if (lowerKey === "authorization" && honorAuthorization) continue;
415
- if (lowerKey === "x-api-key" && honorApiKey) continue;
416
- filteredEnforcedKeys.push(key);
417
- continue;
418
- }
419
- modelHeaders[key] = value;
420
- }
421
- if (filteredEnforcedKeys.length > 0) debug("anthropic: ignoring caller-supplied enforced headers", { headers: filteredEnforcedKeys });
422
- if (isCloudflare) return {
423
- ...modelHeaders,
424
- Accept: acceptHeader,
425
- ...sharedHeaders,
426
- ...incomingUserAgent ? { "User-Agent": incomingUserAgent } : {},
427
- ...betaHeader ? { "anthropic-beta": betaHeader } : {},
428
- "cf-aig-authorization": `Bearer ${options.apiKey}`
429
- };
430
- if (oauthToken) {
431
- const userAgent = isClaudeCodeClientUserAgent(incomingUserAgent) ? incomingUserAgent : claudeCodeUserAgent;
432
- const headers = {
433
- ...modelHeaders,
434
- Accept: acceptHeader,
435
- "Content-Type": "application/json",
436
- "User-Agent": userAgent,
437
- ...options.claudeCodeSessionId ? { "X-Claude-Code-Session-Id": options.claudeCodeSessionId } : {},
438
- ...claudeCodeHeaders,
439
- ...betaHeader ? { "anthropic-beta": betaHeader } : {},
440
- "anthropic-dangerous-direct-browser-access": "true",
441
- "anthropic-version": "2023-06-01",
442
- Authorization: `Bearer ${options.apiKey}`,
443
- "x-app": "cli",
444
- Connection: "keep-alive",
445
- "Accept-Encoding": "gzip, deflate, br, zstd",
446
- ...incomingApiKey ? { "X-Api-Key": incomingApiKey } : {}
447
- };
448
- return allowAnthropicHeaderOverrides ? mergeHeaders(headers, anthropicHeaderOverrides) : headers;
449
- } else if (!isOfficialAnthropicApiUrl(options.baseUrl)) return {
450
- ...modelHeaders,
451
- Accept: acceptHeader,
452
- Authorization: incomingAuthorization ?? `Bearer ${options.apiKey}`,
453
- ...sharedHeaders,
454
- ...incomingUserAgent ? { "User-Agent": incomingUserAgent } : {},
455
- ...betaHeader ? { "anthropic-beta": betaHeader } : {},
456
- ...incomingApiKey ? { "X-Api-Key": incomingApiKey } : {}
457
- };
458
- else return {
459
- ...modelHeaders,
460
- Accept: acceptHeader,
461
- ...sharedHeaders,
462
- ...incomingUserAgent ? { "User-Agent": incomingUserAgent } : {},
463
- ...betaHeader ? { "anthropic-beta": betaHeader } : {},
464
- ...incomingAuthorization ? { Authorization: incomingAuthorization } : {},
465
- "X-Api-Key": incomingApiKey ?? options.apiKey
466
- };
467
- }
468
- function normalizeAnthropicImageMediaType(mimeType) {
469
- const normalized = mimeType.trim().toLowerCase();
470
- if (normalized === "image/jpg") return "image/jpeg";
471
- if (normalized === "image/jpeg" || normalized === "image/png" || normalized === "image/gif" || normalized === "image/webp") return normalized;
472
- }
473
- function cloneAnthropicCacheControl(cacheControl) {
474
- return { ...cacheControl };
475
- }
476
- const ANTHROPIC_STOP_SEQUENCES_MAX = 4;
477
- let warnedStopSequencesTrim = false;
478
- const ANTHROPIC_PROVIDER_SESSION_STATE_KEY = "anthropic-messages";
479
- function createAnthropicControlState() {
480
- return {
481
- declaredTools: void 0,
482
- activeToolNames: /* @__PURE__ */ new Set(),
483
- stableSystemBlocks: void 0,
484
- systemFingerprint: void 0,
485
- controlTransitions: [],
486
- baseEffort: void 0,
487
- baseEffortWire: void 0,
488
- currentEffort: void 0
489
- };
490
- }
491
- function createAnthropicProviderSessionState() {
492
- const state = {
493
- strictToolsDisabled: false,
494
- fastModeDisabled: false,
495
- replayUnsignedThinkingDisabled: false,
496
- prefixDroppedThinkingBlocks: /* @__PURE__ */ new Set(),
497
- controlStates: /* @__PURE__ */ new Map(),
498
- close: () => {
499
- state.strictToolsDisabled = false;
500
- state.fastModeDisabled = false;
501
- state.replayUnsignedThinkingDisabled = false;
502
- state.prefixDroppedThinkingBlocks.clear();
503
- state.controlStates.clear();
504
- }
505
- };
506
- return state;
507
- }
508
- /**
509
- * Key the sticky strict-tools / fast-mode learning per endpoint+model. A
510
- * grammar-too-large 400 or a fast-mode rejection is specific to the model (its
511
- * tool grammar / entitlement) and the endpoint (direct Anthropic vs a gateway /
512
- * Foundry / Bedrock proxy), so it MUST NOT bleed onto unrelated anthropic-messages
513
- * requests in the same session. NUL separates the two components so neither can
514
- * forge the boundary.
515
- */
516
- function anthropicProviderSessionStateKey(baseUrl, modelId) {
517
- return `${ANTHROPIC_PROVIDER_SESSION_STATE_KEY}:${baseUrl}\u0000${modelId}`;
518
- }
519
- function getAnthropicProviderSessionState(providerSessionState, baseUrl, modelId) {
520
- if (!providerSessionState) return void 0;
521
- const key = anthropicProviderSessionStateKey(baseUrl, modelId);
522
- const existing = providerSessionState.get(key);
523
- if (existing) {
524
- existing.prefixDroppedThinkingBlocks ??= /* @__PURE__ */ new Set();
525
- existing.controlStates ??= /* @__PURE__ */ new Map();
526
- return existing;
527
- }
528
- const created = createAnthropicProviderSessionState();
529
- providerSessionState.set(key, created);
530
- return created;
531
- }
532
- function hasStrictAnthropicTools(params) {
533
- return params.tools?.some((tool) => tool.strict === true) ?? false;
534
- }
535
- function dropAnthropicFastMode(params) {
536
- delete params.speed;
537
- }
538
- function dropAnthropicStrictTools(params) {
539
- if (!params.tools) return;
540
- for (const tool of params.tools) delete tool.strict;
541
- }
542
- function getCacheControl(model, cacheRetention) {
543
- const retention = resolveCacheRetention(cacheRetention, "short");
544
- if (retention === "none") return { retention };
545
- const ttl = retention === "long" && model.compat.supportsLongCacheRetention ? "1h" : void 0;
546
- return {
547
- retention,
548
- cacheControl: {
549
- type: "ephemeral",
550
- ...ttl && { ttl }
551
- }
552
- };
553
- }
554
- /** Maps Node's platform identifier to the Stainless wire value. */
555
- function mapStainlessOs(platform) {
556
- switch (platform.toLowerCase()) {
557
- case "darwin": return "MacOS";
558
- case "windows":
559
- case "win32": return "Windows";
560
- case "linux": return "Linux";
561
- case "freebsd": return "FreeBSD";
562
- default: return `Other::${platform.toLowerCase()}`;
563
- }
564
- }
565
- /** Maps Node's architecture identifier to the Stainless wire value. */
566
- function mapStainlessArch(arch) {
567
- switch (arch.toLowerCase()) {
568
- case "amd64":
569
- case "x64": return "x64";
570
- case "arm64":
571
- case "aarch64": return "arm64";
572
- case "386":
573
- case "x86":
574
- case "ia32": return "x86";
575
- default: return `other::${arch.toLowerCase()}`;
576
- }
577
- }
578
- /** Static headers emitted by Claude Code's CLI runtime. */
579
- const claudeCodeHeaders = {
580
- "X-Stainless-Arch": mapStainlessArch(process.arch),
581
- "X-Stainless-Lang": "js",
582
- "X-Stainless-OS": mapStainlessOs(process.platform),
583
- "X-Stainless-Package-Version": claudeCodeSdkVersion,
584
- "X-Stainless-Retry-Count": "0",
585
- "X-Stainless-Runtime": "node",
586
- "X-Stainless-Runtime-Version": "v26.3.0",
587
- "X-Stainless-Timeout": "600"
588
- };
589
- const enforcedHeaderKeys = new Set([
590
- ...Object.keys(claudeCodeHeaders),
591
- "Accept",
592
- "Accept-Encoding",
593
- "Connection",
594
- "Content-Type",
595
- "anthropic-version",
596
- "anthropic-dangerous-direct-browser-access",
597
- "anthropic-beta",
598
- "User-Agent",
599
- "x-app",
600
- "Authorization",
601
- "X-Api-Key",
602
- "X-Claude-Code-Session-Id",
603
- "x-client-request-id",
604
- "cf-aig-authorization"
605
- ].map((key) => key.toLowerCase()));
606
- const overridableAnthropicHeaderKeys = new Set([
607
- ...Object.keys(claudeCodeHeaders),
608
- "anthropic-beta",
609
- "User-Agent",
610
- "x-app"
611
- ].map((key) => key.toLowerCase()));
612
- const CLAUDE_BILLING_HEADER_PREFIX = "x-anthropic-billing-header:";
613
- function createClaudeBillingHeader(firstUserMessageText) {
614
- const k = [
615
- 4,
616
- 7,
617
- 20
618
- ].map((i) => firstUserMessageText[i] ?? "0").join("");
619
- const versionSuffix = nodeCrypto.createHash("sha256").update(`59cf53e54c78${k}${claudeCodeVersion}`).digest("hex").slice(0, 3);
620
- return `${CLAUDE_BILLING_HEADER_PREFIX} cc_version=${claudeCodeVersion}.${versionSuffix}; cc_entrypoint=cli; ${CCH_PLACEHOLDER_STR};`;
621
- }
622
- const CCH_SEED = 5577024349156291176n;
623
- const CCH_PLACEHOLDER_STR = "cch=00000";
624
- const cchEncoder = new TextEncoder();
625
- const CCH_PLACEHOLDER = cchEncoder.encode(CCH_PLACEHOLDER_STR);
626
- const BILLING_SYSTEM_MARKER = cchEncoder.encode(`"system":[{"type":"text","text":"${CLAUDE_BILLING_HEADER_PREFIX}`);
627
- const CCH_BILLING_SEARCH_WINDOW = 150;
628
- function patchCch(body) {
629
- const view = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
630
- const markerIdx = view.indexOf(BILLING_SYSTEM_MARKER);
631
- if (markerIdx === -1) return "no-billing-header";
632
- const searchFrom = markerIdx + BILLING_SYSTEM_MARKER.length;
633
- const idx = view.indexOf(CCH_PLACEHOLDER, searchFrom);
634
- if (idx === -1 || idx - searchFrom > CCH_BILLING_SEARCH_WINDOW) return "unanchored";
635
- const cch = (Bun.hash.xxHash64(body, CCH_SEED) & 1048575n).toString(16).padStart(5, "0");
636
- for (let i = 0; i < 5; i++) body[idx + 4 + i] = cch.charCodeAt(i);
637
- return "patched";
638
- }
639
- /**
640
- * Wraps a fetch implementation to patch the Claude Code billing-header `cch`
641
- * attestation into outgoing request bodies. Bodies without the placeholder
642
- * pass through untouched, so installing it on every OAuth flow is safe.
643
- */
644
- function wrapFetchForCch(base) {
645
- return (input, init) => {
646
- if (init?.body && typeof init.body === "string" && init.body.includes(CCH_PLACEHOLDER_STR)) {
647
- const encoded = cchEncoder.encode(init.body);
648
- if (patchCch(encoded) === "unanchored") warn("anthropic: cch billing placeholder present but not patched; sending unattested request");
649
- return base(input, {
650
- ...init,
651
- body: encoded
652
- });
653
- }
654
- return base(input, init);
655
- };
656
- }
657
- const CLAUDE_CLOAKING_USER_ID_REGEX = /^user_[0-9a-fA-F]{64}_account_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}_session_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
658
- function isClaudeCloakingUserId(userId) {
659
- return CLAUDE_CLOAKING_USER_ID_REGEX.test(userId);
660
- }
661
- /**
662
- * Real Claude Code sends `metadata.user_id` as a JSON-stringified object of the
663
- * shape `{ device_id, account_uuid, session_id, ...extra }` (see
664
- * services/api/claude.ts → getAPIMetadata). Accept that shape so callers that
665
- * supply a stable `session_id` aren't silently overwritten with fresh entropy
666
- * on every request, which would inflate the backend session count.
667
- */
668
- function isClaudeJsonUserId(userId) {
669
- if (userId.length === 0 || userId[0] !== "{") return false;
670
- let parsed;
671
- try {
672
- parsed = JSON.parse(userId);
673
- } catch {
674
- return false;
675
- }
676
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return false;
677
- const obj = parsed;
678
- return typeof obj.session_id === "string" && obj.session_id.length > 0;
679
- }
680
- function extractClaudeMetadataSessionId(userId) {
681
- if (typeof userId !== "string") return void 0;
682
- if (isClaudeCloakingUserId(userId)) return userId.slice(userId.lastIndexOf("_session_") + 9);
683
- if (userId.length === 0 || userId[0] !== "{") return void 0;
684
- let parsed;
685
- try {
686
- parsed = JSON.parse(userId);
687
- } catch {
688
- return;
689
- }
690
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return void 0;
691
- const sessionId = parsed.session_id;
692
- return typeof sessionId === "string" && sessionId.length > 0 ? sessionId : void 0;
693
- }
694
- const CLAUDE_DEVICE_ID_INSTALL_HASH_DOMAIN = "omp-claude-device-id-v1:";
695
- const CLAUDE_DEVICE_ID_ACCOUNT_HASH_DOMAIN = "omp-claude-device-id-v2";
696
- function deriveClaudeDeviceId(installId, accountId) {
697
- const hash = nodeCrypto.createHash("sha256");
698
- if (accountId && accountId.length > 0) return hash.update(CLAUDE_DEVICE_ID_ACCOUNT_HASH_DOMAIN).update("\0").update(installId).update("\0").update(accountId).digest("hex");
699
- return hash.update(CLAUDE_DEVICE_ID_INSTALL_HASH_DOMAIN).update(installId).digest("hex");
700
- }
701
- function readMetadataString(metadata, key) {
702
- const value = metadata?.[key];
703
- return typeof value === "string" && value.length > 0 ? value : void 0;
704
- }
705
- function readAnthropicMetadataAccountId(metadata) {
706
- return readMetadataString(metadata, "account_uuid") ?? readMetadataString(metadata, "accountId") ?? readMetadataString(metadata, "account_id");
707
- }
708
- function deriveClaudeDeviceIdFromInstallId(accountId) {
709
- return deriveClaudeDeviceId(getInstallId(), accountId);
710
- }
711
- function generateClaudeJsonUserId(sessionId, accountId) {
712
- const userId = {
713
- device_id: deriveClaudeDeviceIdFromInstallId(accountId),
714
- session_id: sessionId ?? nodeCrypto.randomUUID().toLowerCase()
715
- };
716
- if (accountId && accountId.length > 0) userId.account_uuid = accountId;
717
- return JSON.stringify(userId);
718
- }
719
- /**
720
- * Resolve the `metadata.user_id` field for an Anthropic Messages request.
721
- *
722
- * For API-key tokens, an explicit caller-supplied `userId` is forwarded
723
- * verbatim and `undefined` yields no metadata. For OAuth tokens the value
724
- * must match the Claude Code attribution shape (`isClaudeCloakingUserId` or
725
- * the `{session_id, account_uuid?, device_id?}` JSON envelope) — anything
726
- * else is dropped and a fresh Claude-Code-style JSON id is generated from
727
- * `sessionId`/`accountId` so attribution stays consistent across the main
728
- * streaming path and provider-specific request builders (e.g. web search).
729
- */
730
- function resolveAnthropicMetadataUserId(userId, isOAuthToken, sessionId, accountId) {
731
- if (typeof userId === "string") {
732
- if (!isOAuthToken || isClaudeCloakingUserId(userId) || isClaudeJsonUserId(userId)) return userId;
733
- }
734
- if (!isOAuthToken) return void 0;
735
- return generateClaudeJsonUserId(sessionId, accountId);
736
- }
737
- const ANTHROPIC_BUILTIN_TOOL_NAMES = /* @__PURE__ */ new Set([
738
- "web_search",
739
- "code_execution",
740
- "text_editor",
741
- "computer"
742
- ]);
743
- const UMANS_WEBSEARCH_PROVIDER_HEADER = "X-Umans-Websearch-Provider";
744
- const UMANS_WEBSEARCH_TOOL_NAME = "web_search";
745
- const applyClaudeToolPrefix = (name) => {
746
- if (ANTHROPIC_BUILTIN_TOOL_NAMES.has(name.toLowerCase())) return name;
747
- return `_${name}`;
748
- };
749
- const stripClaudeToolPrefix = (name) => {
750
- if (!name.toLowerCase().startsWith("_".toLowerCase())) return name;
751
- return name.slice(1);
752
- };
753
- function normalizeUmansWebSearchProvider(value) {
754
- const normalized = value?.trim().toLowerCase();
755
- return normalized === "native" || normalized === "exa" ? normalized : void 0;
756
- }
757
- function getUmansWebSearchProvider(headers) {
758
- const explicit = getHeaderCaseInsensitive(headers, UMANS_WEBSEARCH_PROVIDER_HEADER);
759
- if (explicit !== void 0) return normalizeUmansWebSearchProvider(explicit);
760
- return normalizeUmansWebSearchProvider($env.UMANS_WEBSEARCH_PROVIDER);
761
- }
762
- function isUmansAnthropicModel(model) {
763
- return model.provider === "umans" || model.baseUrl.toLowerCase().includes("api.code.umans.ai");
764
- }
765
- function getUmansWebSearchHeader(model, headers) {
766
- if (!isUmansAnthropicModel(model)) return void 0;
767
- const provider = getUmansWebSearchProvider(headers);
768
- return provider ? { [UMANS_WEBSEARCH_PROVIDER_HEADER]: provider } : void 0;
769
- }
770
- function shouldUseUmansGatewayWebSearch(name, enabled) {
771
- return enabled && name.toLowerCase() === UMANS_WEBSEARCH_TOOL_NAME;
772
- }
773
- function encodeAnthropicToolName(name, isOAuthToken, escapeBuiltinToolNames, useUmansGatewayWebSearch = false) {
774
- if (shouldUseUmansGatewayWebSearch(name, useUmansGatewayWebSearch)) return name;
775
- if (escapeBuiltinToolNames) return `_${name}`;
776
- return isOAuthToken ? applyClaudeToolPrefix(name) : name;
777
- }
778
- function decodeAnthropicToolName(name, isOAuthToken, escapeBuiltinToolNames) {
779
- if (isOAuthToken || escapeBuiltinToolNames) return stripClaudeToolPrefix(name);
780
- return name;
781
- }
782
- const ANTHROPIC_MANY_IMAGE_THRESHOLD = 20;
783
- const ANTHROPIC_MANY_IMAGE_MAX_DIMENSION = 2e3;
784
- function countAnthropicImageBlocks(messages) {
785
- let count = 0;
786
- for (const message of messages) {
787
- if (message.role !== "user" && message.role !== "developer" && message.role !== "toolResult") continue;
788
- if (!Array.isArray(message.content)) continue;
789
- for (const block of message.content) if (block.type === "image") count++;
790
- }
791
- return count;
792
- }
793
- const ANTHROPIC_IMAGE_RESIZE_CONCURRENCY = 4;
794
- /**
795
- * Memoized resize results keyed on ImageContent identity. Callers keep message
796
- * objects stable across turns, so without this every request (and every
797
- * in-provider retry of a fresh turn) re-decodes and re-encodes the same
798
- * oversized screenshots. A cached value identical to the key means "already
799
- * within bounds / unresizable — skip the decode".
800
- */
801
- const anthropicManyImageResizeCache = /* @__PURE__ */ new WeakMap();
802
- /**
803
- * Bounded-concurrency gate for image decode/encode work. The many-image path
804
- * fans out over every block of every message; unbounded, 100+ oversized images
805
- * would decode concurrently (two encode pipelines each) and spike memory by
806
- * gigabytes. Slots are handed off directly to the next waiter on release.
807
- */
808
- function createResizeLimiter(limit) {
809
- let active = 0;
810
- const queue = [];
811
- return async (fn) => {
812
- if (active >= limit) {
813
- const { promise, resolve } = Promise.withResolvers();
814
- queue.push(resolve);
815
- await promise;
816
- } else active++;
817
- try {
818
- return await fn();
819
- } finally {
820
- const next = queue.shift();
821
- if (next) next();
822
- else active--;
823
- }
824
- };
825
- }
826
- async function resizeAnthropicManyImageBlock(block) {
827
- try {
828
- const inputBuffer = Buffer.from(block.data, "base64");
829
- const { width, height } = await new Bun.Image(inputBuffer).metadata();
830
- if (!width || !height) return block;
831
- if (width <= ANTHROPIC_MANY_IMAGE_MAX_DIMENSION && height <= ANTHROPIC_MANY_IMAGE_MAX_DIMENSION) return block;
832
- const scale = Math.min(ANTHROPIC_MANY_IMAGE_MAX_DIMENSION / width, ANTHROPIC_MANY_IMAGE_MAX_DIMENSION / height);
833
- const targetWidth = Math.max(1, Math.min(ANTHROPIC_MANY_IMAGE_MAX_DIMENSION, Math.round(width * scale)));
834
- const targetHeight = Math.max(1, Math.min(ANTHROPIC_MANY_IMAGE_MAX_DIMENSION, Math.round(height * scale)));
835
- const [png, jpeg] = await Promise.all([new Bun.Image(inputBuffer).resize(targetWidth, targetHeight).png().bytes(), new Bun.Image(inputBuffer).resize(targetWidth, targetHeight).jpeg({ quality: 85 }).bytes()]);
836
- const best = png.length <= jpeg.length ? {
837
- buffer: png,
838
- mimeType: "image/png"
839
- } : {
840
- buffer: jpeg,
841
- mimeType: "image/jpeg"
842
- };
843
- return {
844
- type: "image",
845
- data: Buffer.from(best.buffer).toString("base64"),
846
- mimeType: best.mimeType
847
- };
848
- } catch (error) {
849
- warn("anthropic: failed to resize oversized image for many-image request", {
850
- mimeType: block.mimeType,
851
- error: error instanceof Error ? error.message : String(error)
852
- });
853
- return block;
854
- }
855
- }
856
- async function resizeAnthropicManyImageContent(content, state, limit) {
857
- let changed = false;
858
- const next = await Promise.all(content.map(async (block) => {
859
- if (block.type !== "image" || block.url || block.providerFile?.provider === "anthropic" && block.providerFile.id) return block;
860
- let resized = anthropicManyImageResizeCache.get(block);
861
- if (resized === void 0) {
862
- resized = await limit(() => resizeAnthropicManyImageBlock(block));
863
- anthropicManyImageResizeCache.set(block, resized);
864
- }
865
- if (resized !== block) {
866
- changed = true;
867
- state.resized++;
868
- }
869
- return resized;
870
- }));
871
- return changed ? next : content;
872
- }
873
- async function resizeAnthropicManyImageMessage(message, state, limit) {
874
- if (message.role === "user" || message.role === "developer") {
875
- if (!Array.isArray(message.content)) return message;
876
- const content = await resizeAnthropicManyImageContent(message.content, state, limit);
877
- return content === message.content ? message : {
878
- ...message,
879
- content
880
- };
881
- }
882
- if (message.role === "toolResult") {
883
- const content = await resizeAnthropicManyImageContent(message.content, state, limit);
884
- return content === message.content ? message : {
885
- ...message,
886
- content
887
- };
888
- }
889
- return message;
890
- }
891
- async function prepareAnthropicManyImageContext(context, supportsImages) {
892
- if (!supportsImages) return context;
893
- const imageCount = countAnthropicImageBlocks(context.messages);
894
- if (imageCount <= ANTHROPIC_MANY_IMAGE_THRESHOLD) return context;
895
- let changed = false;
896
- const state = { resized: 0 };
897
- const limit = createResizeLimiter(ANTHROPIC_IMAGE_RESIZE_CONCURRENCY);
898
- const messages = await Promise.all(context.messages.map(async (message) => {
899
- const next = await resizeAnthropicManyImageMessage(message, state, limit);
900
- if (next !== message) changed = true;
901
- return next;
902
- }));
903
- if (!changed) return context;
904
- debug("anthropic: resized oversized images for many-image request", {
905
- imageCount,
906
- resized: state.resized,
907
- maxDimension: ANTHROPIC_MANY_IMAGE_MAX_DIMENSION
908
- });
909
- return {
910
- ...context,
911
- messages
912
- };
913
- }
914
- /**
915
- * Convert content blocks to Anthropic API format
916
- */
917
- function convertContentBlocks(content, supportsImages = true) {
918
- const blocks = [];
919
- let sawText = false;
920
- let sawImage = false;
921
- for (const block of content) {
922
- if (block.type === "text") {
923
- const text = block.text.toWellFormed();
924
- if (text.trim().length === 0) continue;
925
- sawText = true;
926
- blocks.push({
927
- type: "text",
928
- text
929
- });
930
- continue;
931
- }
932
- if (!supportsImages) {
933
- blocks.push({
934
- type: "text",
935
- text: NON_VISION_IMAGE_PLACEHOLDER
936
- });
937
- continue;
938
- }
939
- let source;
940
- if (block.providerFile?.provider === "anthropic" && block.providerFile.id) source = {
941
- type: "file",
942
- file_id: block.providerFile.id
943
- };
944
- else if (block.url) source = {
945
- type: "url",
946
- url: block.url
947
- };
948
- else {
949
- const mediaType = normalizeAnthropicImageMediaType(block.mimeType);
950
- if (!mediaType) {
951
- blocks.push({
952
- type: "text",
953
- text: `[unsupported image: ${block.mimeType}]`
954
- });
955
- continue;
956
- }
957
- source = {
958
- type: "base64",
959
- media_type: mediaType,
960
- data: block.data
961
- };
962
- }
963
- sawImage = true;
964
- blocks.push({
965
- type: "image",
966
- source
967
- });
968
- }
969
- if (!supportsImages) return blocks.filter((block) => block.type === "text").map((block) => block.text).join("\n").toWellFormed();
970
- if (sawImage && !sawText) blocks.unshift({
971
- type: "text",
972
- text: "(see attached image)"
973
- });
974
- return blocks;
975
- }
976
- const CLAUDE_CODE_TLS_CIPHERS = tls.DEFAULT_CIPHERS;
977
- const foundryTlsOptionsCache = /* @__PURE__ */ new Map();
978
- function foundryTlsCacheKeyComponent(value) {
979
- if (!value) return null;
980
- const trimmed = value.trim();
981
- if (trimmed && !trimmed.includes("-----BEGIN") && looksLikeFilePath(trimmed)) try {
982
- return `${trimmed}@${fs.statSync(trimmed).mtimeMs}`;
983
- } catch {
984
- return trimmed;
985
- }
986
- return value;
987
- }
988
- function foundryTlsOptionsCacheKey() {
989
- return JSON.stringify([
990
- foundryTlsCacheKeyComponent($env.NODE_EXTRA_CA_CERTS),
991
- foundryTlsCacheKeyComponent($env.CLAUDE_CODE_CLIENT_CERT),
992
- foundryTlsCacheKeyComponent($env.CLAUDE_CODE_CLIENT_KEY)
993
- ]);
994
- }
995
- function resolveAnthropicBaseUrl(model, apiKey) {
996
- if (model.provider === "github-copilot") return normalizeAnthropicBaseUrl(resolveGitHubCopilotBaseUrl(model.baseUrl, apiKey) ?? model.baseUrl);
997
- if (model.provider === "anthropic" && isFoundryEnabled()) {
998
- const foundryBaseUrl = normalizeAnthropicBaseUrl($env.FOUNDRY_BASE_URL);
999
- if (foundryBaseUrl) return foundryBaseUrl;
1000
- }
1001
- if (model.provider === "anthropic") {
1002
- const configured = normalizeAnthropicBaseUrl(model.baseUrl);
1003
- if (configured && !isOfficialAnthropicApiUrl(configured)) return configured;
1004
- return normalizeAnthropicBaseUrl($env.ANTHROPIC_BASE_URL) ?? configured ?? "https://api.anthropic.com";
1005
- }
1006
- return normalizeAnthropicBaseUrl(model.baseUrl);
1007
- }
1008
- function resolveEagerToolInputStreamingSupport(model, effectiveBaseUrl) {
1009
- if (!model.compat.supportsEagerToolInputStreaming) return false;
1010
- if (isOfficialAnthropicApiUrl(effectiveBaseUrl)) return true;
1011
- return !model.compat.officialEndpoint;
1012
- }
1013
- function parseAnthropicCustomHeaders(rawHeaders) {
1014
- const source = rawHeaders?.trim();
1015
- if (!source) return void 0;
1016
- const parsed = {};
1017
- for (const token of source.split(/\r?\n|,/)) {
1018
- const entry = token.trim();
1019
- if (!entry) continue;
1020
- const separatorIndex = entry.indexOf(":");
1021
- if (separatorIndex <= 0) continue;
1022
- const key = entry.slice(0, separatorIndex).trim();
1023
- const value = entry.slice(separatorIndex + 1).trim();
1024
- if (!key || !value) continue;
1025
- parsed[key] = value;
1026
- }
1027
- return Object.keys(parsed).length > 0 ? parsed : void 0;
1028
- }
1029
- /**
1030
- * Returns env-supplied custom headers (`ANTHROPIC_CUSTOM_HEADERS`) when they
1031
- * should be forwarded to the upstream endpoint.
1032
- *
1033
- * Foundry mode forwards them unconditionally. Outside Foundry, they're applied
1034
- * only when the configured base URL is a non-Anthropic host — i.e. an
1035
- * enterprise/corporate gateway that may require its own proprietary auth
1036
- * header. Stock `api.anthropic.com` would reject unknown headers, so they're
1037
- * omitted there.
1038
- */
1039
- function resolveAnthropicCustomHeadersForBaseUrl(baseUrl) {
1040
- if (!isFoundryEnabled() && isOfficialAnthropicApiUrl(baseUrl)) return void 0;
1041
- return parseAnthropicCustomHeaders($env.ANTHROPIC_CUSTOM_HEADERS);
1042
- }
1043
- function resolveAnthropicCustomHeaders(model, baseUrl) {
1044
- if (model.provider !== "anthropic") return void 0;
1045
- return resolveAnthropicCustomHeadersForBaseUrl(baseUrl);
1046
- }
1047
- function looksLikeFilePath(value) {
1048
- return value.includes("/") || value.includes("\\") || /\.(pem|crt|cer|key)$/i.test(value);
1049
- }
1050
- function resolvePemValue(value, name) {
1051
- const trimmed = value?.trim();
1052
- if (!trimmed) return void 0;
1053
- const inline = trimmed.replace(/\\n/g, "\n");
1054
- if (inline.includes("-----BEGIN")) return inline;
1055
- if (looksLikeFilePath(trimmed)) try {
1056
- return fs.readFileSync(trimmed, "utf8");
1057
- } catch (error) {
1058
- if (isEnoent(error)) throw new ValidationError(`${name} path does not exist: ${trimmed}`);
1059
- throw error;
1060
- }
1061
- return inline;
1062
- }
1063
- function resolveFoundryTlsOptions(model) {
1064
- if (model.provider !== "anthropic") return void 0;
1065
- if (!isFoundryEnabled()) return void 0;
1066
- const cacheKey = foundryTlsOptionsCacheKey();
1067
- if (foundryTlsOptionsCache.has(cacheKey)) return foundryTlsOptionsCache.get(cacheKey);
1068
- const ca = resolvePemValue($env.NODE_EXTRA_CA_CERTS, "NODE_EXTRA_CA_CERTS");
1069
- const cert = resolvePemValue($env.CLAUDE_CODE_CLIENT_CERT, "CLAUDE_CODE_CLIENT_CERT");
1070
- const key = resolvePemValue($env.CLAUDE_CODE_CLIENT_KEY, "CLAUDE_CODE_CLIENT_KEY");
1071
- if (cert && !key || !cert && key) throw new ConfigurationError("Both CLAUDE_CODE_CLIENT_CERT and CLAUDE_CODE_CLIENT_KEY must be set for mTLS.");
1072
- const options = {};
1073
- if (ca) options.ca = [...tls.rootCertificates, ca];
1074
- if (cert) options.cert = cert;
1075
- if (key) options.key = key;
1076
- const resolved = Object.keys(options).length > 0 ? options : void 0;
1077
- foundryTlsOptionsCache.set(cacheKey, resolved);
1078
- return resolved;
1079
- }
1080
- function buildClaudeCodeTlsFetchOptions(model, baseUrl) {
1081
- if (model.provider !== "anthropic") return void 0;
1082
- if (!baseUrl) return void 0;
1083
- let serverName;
1084
- try {
1085
- serverName = new URL(baseUrl).hostname;
1086
- } catch {
1087
- return;
1088
- }
1089
- if (!serverName) return void 0;
1090
- const foundryTlsOptions = resolveFoundryTlsOptions(model);
1091
- return { tls: {
1092
- rejectUnauthorized: true,
1093
- serverName,
1094
- ...CLAUDE_CODE_TLS_CIPHERS ? { ciphers: CLAUDE_CODE_TLS_CIPHERS } : {},
1095
- ...foundryTlsOptions
1096
- } };
1097
- }
1098
- function mergeHeaders(...headerSources) {
1099
- const merged = {};
1100
- const keyByLower = /* @__PURE__ */ new Map();
1101
- for (const headers of headerSources) {
1102
- if (!headers) continue;
1103
- for (const [key, value] of Object.entries(headers)) {
1104
- const lower = key.toLowerCase();
1105
- const existing = keyByLower.get(lower);
1106
- if (existing !== void 0 && existing !== key) delete merged[existing];
1107
- keyByLower.set(lower, key);
1108
- merged[key] = value;
1109
- }
1110
- }
1111
- return merged;
1112
- }
1113
- const ANTHROPIC_MESSAGE_EVENTS = /* @__PURE__ */ new Set([
1114
- "message_start",
1115
- "message_delta",
1116
- "message_stop",
1117
- "content_block_start",
1118
- "content_block_delta",
1119
- "content_block_stop"
1120
- ]);
1121
- const ANTHROPIC_PING_EVENT = { type: "ping" };
1122
- /**
1123
- * In-stream `error` SSE frames carry an Anthropic error envelope:
1124
- * `{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}`.
1125
- * Surface the structured type + message instead of the raw JSON blob; the
1126
- * error type token (e.g. `overloaded_error`, `rate_limit_error`) is kept in
1127
- * the message so `isProviderRetryableError`'s classification keys off the
1128
- * structured type rather than incidental JSON substrings.
1129
- */
1130
- function createAnthropicSseStreamError(data) {
1131
- try {
1132
- const parsed = JSON.parse(data);
1133
- const errorType = typeof parsed?.error?.type === "string" ? parsed.error.type : void 0;
1134
- const message = typeof parsed?.error?.message === "string" ? parsed.error.message : void 0;
1135
- if (message) return new ProviderResponseError(errorType ? `Anthropic stream error (${errorType}): ${message}` : `Anthropic stream error: ${message}`, {
1136
- provider: "anthropic",
1137
- kind: "output"
1138
- });
1139
- } catch {}
1140
- return new ProviderResponseError(data, {
1141
- provider: "anthropic",
1142
- kind: "output"
1143
- });
1144
- }
1145
- async function* iterateAnthropicEvents(response, signal, onSseEvent) {
1146
- if (!response.body) throw new AnthropicStreamEnvelopeError("Attempted to iterate over an Anthropic response with no body");
1147
- let sawMessageStart = false;
1148
- let sawMessageEnd = false;
1149
- for await (const sse of readSseEvents(response.body, signal)) {
1150
- notifyRawSseEvent(onSseEvent, sse);
1151
- if (sse.event === "error") throw createAnthropicSseStreamError(sse.data);
1152
- if (sse.event === "ping") {
1153
- yield ANTHROPIC_PING_EVENT;
1154
- continue;
1155
- }
1156
- if (!ANTHROPIC_MESSAGE_EVENTS.has(sse.event ?? "")) continue;
1157
- try {
1158
- const event = JSON.parse(sse.data);
1159
- if (event.type !== sse.event) reportAnthropicEnvelopeAnomaly(`event type ${event.type} does not match SSE event ${sse.event}`);
1160
- if (event.type === "message_start") sawMessageStart = true;
1161
- else if (event.type === "message_stop") sawMessageEnd = true;
1162
- yield event;
1163
- } catch (error) {
1164
- const message = error instanceof Error ? error.message : String(error);
1165
- reportAnthropicEnvelopeAnomaly(`could not parse SSE event ${sse.event}: ${message}; skipping frame; data=${sse.data}`);
1166
- }
1167
- }
1168
- if (sawMessageStart && !sawMessageEnd && !signal?.aborted) reportAnthropicEnvelopeAnomaly("stream ended before message_stop");
1169
- }
1170
- function hasAnthropicRawResponseRequest(request) {
1171
- return isRecord(request) && typeof request.asResponse === "function";
1172
- }
1173
- function hasAnthropicStreamWithResponseRequest(request) {
1174
- return isRecord(request) && typeof request.withResponse === "function";
1175
- }
1176
- async function getAnthropicStreamResponse(request, signal, onSseEvent) {
1177
- if (hasAnthropicRawResponseRequest(request)) {
1178
- const response = await request.asResponse();
1179
- return {
1180
- events: iterateAnthropicEvents(response, signal, onSseEvent),
1181
- response,
1182
- requestId: response.headers.get("request-id"),
1183
- recordsRawSseEvents: true
1184
- };
1185
- }
1186
- if (hasAnthropicStreamWithResponseRequest(request)) {
1187
- const { data, response, request_id } = await request.withResponse();
1188
- return {
1189
- events: data,
1190
- response,
1191
- requestId: request_id,
1192
- recordsRawSseEvents: false
1193
- };
1194
- }
1195
- throw new AnthropicStreamEnvelopeError("Anthropic SDK request did not expose a stream response");
1196
- }
1197
- async function* observeDecodedAnthropicSdkEvents(events, observer) {
1198
- for await (const event of events) {
1199
- const data = JSON.stringify(event);
1200
- notifyRawSseEvent(observer, {
1201
- event: event.type,
1202
- data,
1203
- raw: [`event: ${event.type}`, `data: ${data}`]
1204
- });
1205
- yield event;
1206
- }
1207
- }
1208
- const PROVIDER_MAX_RETRIES = 10;
1209
- /**
1210
- * How long `ping` keepalives may keep extending the idle deadline without any
1211
- * semantic stream progress, as a multiple of the idle timeout. Anthropic pings
1212
- * across legitimate generation gaps, so pings count as liveness — but a wedged
1213
- * upstream that pings forever while producing no events must eventually trip
1214
- * the idle watchdog instead of hanging an active tool-call stream without a
1215
- * recovery path (#4900).
1216
- */
1217
- const PING_PROGRESS_MAX_IDLE_MULTIPLIER = 3;
1218
- /**
1219
- * Log a malformed-stream-envelope anomaly without aborting the turn. The strict
1220
- * parser would `throw new AnthropicStreamEnvelopeError(...)` here; we instead
1221
- * surface a warning and let the caller skip the offending event (or finalize what
1222
- * already streamed) so a non-conforming endpoint degrades to best-effort content
1223
- * rather than failing the request.
1224
- */
1225
- function reportAnthropicEnvelopeAnomaly(detail) {
1226
- warn(`anthropic: ignoring malformed stream envelope: ${detail}`);
1227
- }
1228
- function shouldIgnoreAnthropicPreambleEvent(eventType) {
1229
- if (typeof eventType !== "string") return false;
1230
- if (eventType === "ping") return true;
1231
- return !ANTHROPIC_MESSAGE_EVENTS.has(eventType);
1232
- }
1233
- const THINKING_ENVELOPE_OPEN = "<thinking>";
1234
- const THINKING_ENVELOPE_CLOSE = "</thinking>";
1235
- function unwrapAnthropicThinkingEnvelope(text) {
1236
- let current = text.trim();
1237
- let stripped = false;
1238
- while (current.startsWith(THINKING_ENVELOPE_OPEN) && current.endsWith(THINKING_ENVELOPE_CLOSE)) {
1239
- current = current.slice(10, current.length - 11).trim();
1240
- stripped = true;
1241
- }
1242
- return stripped ? current : void 0;
1243
- }
1244
- function createEmptyUsage(premiumRequests) {
1245
- return {
1246
- input: 0,
1247
- output: 0,
1248
- cacheRead: 0,
1249
- cacheWrite: 0,
1250
- totalTokens: 0,
1251
- ...premiumRequests === void 0 ? {} : { premiumRequests },
1252
- cost: {
1253
- input: 0,
1254
- output: 0,
1255
- cacheRead: 0,
1256
- cacheWrite: 0,
1257
- total: 0
1258
- }
1259
- };
1260
- }
1261
- /**
1262
- * Capture Anthropic's optional cache-creation TTL breakdown and server-tool-use
1263
- * counters into the harness Usage shape. Omitted/null fields are no-ops; explicit
1264
- * zero-valued objects clear prior extras from earlier stream usage snapshots.
1265
- */
1266
- function applyAnthropicUsageExtras(usage, source) {
1267
- const cacheCreation = source.cache_creation;
1268
- if (cacheCreation != null) {
1269
- const fiveMinute = cacheCreation.ephemeral_5m_input_tokens ?? 0;
1270
- const oneHour = cacheCreation.ephemeral_1h_input_tokens ?? 0;
1271
- if (fiveMinute > 0 || oneHour > 0) usage.cttl = {
1272
- ...fiveMinute > 0 ? { ephemeral5m: fiveMinute } : {},
1273
- ...oneHour > 0 ? { ephemeral1h: oneHour } : {}
1274
- };
1275
- else delete usage.cttl;
1276
- }
1277
- const serverToolUse = source.server_tool_use;
1278
- if (serverToolUse != null) {
1279
- const webSearch = serverToolUse.web_search_requests ?? 0;
1280
- const webFetch = serverToolUse.web_fetch_requests ?? 0;
1281
- if (webSearch > 0 || webFetch > 0) usage.server = {
1282
- ...webSearch > 0 ? { webSearch } : {},
1283
- ...webFetch > 0 ? { webFetch } : {}
1284
- };
1285
- else delete usage.server;
1286
- }
1287
- }
1288
- function parseAnthropicWireUsage(value) {
1289
- if (!isRecord(value)) return void 0;
1290
- const cacheCreation = isRecord(value.cache_creation) ? {
1291
- ...typeof value.cache_creation.ephemeral_5m_input_tokens === "number" ? { ephemeral_5m_input_tokens: value.cache_creation.ephemeral_5m_input_tokens } : {},
1292
- ...typeof value.cache_creation.ephemeral_1h_input_tokens === "number" ? { ephemeral_1h_input_tokens: value.cache_creation.ephemeral_1h_input_tokens } : {}
1293
- } : void 0;
1294
- return {
1295
- ...typeof value.input_tokens === "number" ? { input_tokens: value.input_tokens } : {},
1296
- ...typeof value.output_tokens === "number" ? { output_tokens: value.output_tokens } : {},
1297
- ...typeof value.cache_read_input_tokens === "number" ? { cache_read_input_tokens: value.cache_read_input_tokens } : {},
1298
- ...typeof value.cache_creation_input_tokens === "number" ? { cache_creation_input_tokens: value.cache_creation_input_tokens } : {},
1299
- ...cacheCreation === void 0 ? {} : { cache_creation: cacheCreation }
1300
- };
1301
- }
1302
- function parseAnthropicFallbackWireBlock(value) {
1303
- if (!isRecord(value) || value.type !== "fallback") return void 0;
1304
- const from = isRecord(value.from) && typeof value.from.model === "string" ? value.from.model : void 0;
1305
- const to = isRecord(value.to) && typeof value.to.model === "string" ? value.to.model : void 0;
1306
- if (!from?.trim() || !to?.trim()) return void 0;
1307
- return {
1308
- type: "fallback",
1309
- from: { model: from },
1310
- to: { model: to }
1311
- };
1312
- }
1313
- /**
1314
- * The definitive "served by fallback" signal per Anthropic's fallback
1315
- * billing cookbook (§4): a `fallback_message` iteration in `usage.iterations`.
1316
- * Any other iteration type is per-attempt bookkeeping for the requested model
1317
- * (including its dated snapshot alias) and MUST NOT retag the assistant turn.
1318
- */
1319
- function fallbackServedModelFromUsage(source) {
1320
- const iterations = source.iterations ?? [];
1321
- for (let index = iterations.length - 1; index >= 0; index -= 1) {
1322
- const iteration = iterations[index];
1323
- if (iteration?.type === "fallback_message" && iteration.model?.trim()) return iteration.model;
1324
- }
1325
- }
1326
- /**
1327
- * Price a fallback turn per the fallback billing cookbook §4:
1328
- * • A pre-served attempt with zero output/cache-creation is not billed
1329
- * (waived classifier block); its iteration is skipped.
1330
- * • Mid-stream refusals bill their attempting model's input+output at
1331
- * that model's normal rates.
1332
- * • The `fallback_message` attempt's input tokens are rebilled at the
1333
- * served model's cache-read rate (fallback credit — 10% of base input).
1334
- *
1335
- * Top-level `usage.input/output/cacheRead/cacheWrite` stay Anthropic's raw
1336
- * served-attempt counts; `usage.cost` reflects the per-iteration attributed
1337
- * total. Non-fallback turns skip this path entirely and use the requested
1338
- * model at the normal `calculateCost` call.
1339
- */
1340
- /**
1341
- * Resolve a served/iteration model id to its bundled catalog entry when
1342
- * possible so the per-iteration cost uses the served model's pricing
1343
- * (e.g. Opus 4.8 rates for a Fable→Opus fallback). Falls back to
1344
- * `requestModel` when the id is empty, matches the request, or the
1345
- * catalog has no entry under it — the caller keeps the requested-model
1346
- * pricing as the safe default and logs at the source.
1347
- */
1348
- function resolveIterationModel(requestModel, iterationModelId) {
1349
- const id = iterationModelId?.trim();
1350
- if (!id || id === requestModel.id) return requestModel;
1351
- if (requestModel.provider === "anthropic") {
1352
- const bundled = getBundledModel("anthropic", id);
1353
- if (bundled?.api === "anthropic-messages") return bundled;
1354
- }
1355
- return requestModel;
1356
- }
1357
- function calculateFallbackTurnCost(requestModel, usage, source, timestamp) {
1358
- const iterations = source.iterations ?? [];
1359
- if (iterations.length === 0) return false;
1360
- const cost = {
1361
- input: 0,
1362
- output: 0,
1363
- cacheRead: 0,
1364
- cacheWrite: 0,
1365
- total: 0
1366
- };
1367
- const hasFallbackMessage = iterations.some((iter) => iter.type === "fallback_message");
1368
- let applied = false;
1369
- for (const iteration of iterations) {
1370
- const inputTokens = iteration.input_tokens ?? 0;
1371
- const outputTokens = iteration.output_tokens ?? 0;
1372
- const cacheReadTokens = iteration.cache_read_input_tokens ?? 0;
1373
- const cacheWriteTokens = iteration.cache_creation_input_tokens ?? 0;
1374
- const isFallback = iteration.type === "fallback_message";
1375
- if (hasFallbackMessage && !isFallback && outputTokens === 0 && cacheWriteTokens === 0) continue;
1376
- const iterationUsage = createEmptyUsage();
1377
- if (isFallback) {
1378
- iterationUsage.input = 0;
1379
- iterationUsage.cacheRead = cacheReadTokens + inputTokens;
1380
- } else {
1381
- iterationUsage.input = inputTokens;
1382
- iterationUsage.cacheRead = cacheReadTokens;
1383
- }
1384
- iterationUsage.output = outputTokens;
1385
- iterationUsage.cacheWrite = cacheWriteTokens;
1386
- iterationUsage.totalTokens = iterationUsage.input + iterationUsage.output + iterationUsage.cacheRead + iterationUsage.cacheWrite;
1387
- calculateCost(resolveIterationModel(requestModel, iteration.model), iterationUsage, timestamp);
1388
- cost.input += iterationUsage.cost.input;
1389
- cost.output += iterationUsage.cost.output;
1390
- cost.cacheRead += iterationUsage.cost.cacheRead;
1391
- cost.cacheWrite += iterationUsage.cost.cacheWrite;
1392
- cost.total += iterationUsage.cost.total;
1393
- applied = true;
1394
- }
1395
- if (!applied) return false;
1396
- usage.cost = cost;
1397
- return true;
1398
- }
1399
- /**
1400
- * Detects the two shapes a signature-enforcing endpoint uses to reject a
1401
- * replayed unsigned thinking block (sent as `signature: ""`):
1402
- *
1403
- * - Anthropic and Anthropic-fronting proxies: `400 Invalid `signature` in
1404
- * `thinking` block`.
1405
- * - Bedrock-backed proxies: the empty string fails schema validation before
1406
- * signature checking, so it comes back as `ValidationException: The model
1407
- * returned the following errors: messages.N.content.M.thinking.signature:
1408
- * Field required`.
1409
- *
1410
- * Exported for the compat tests.
1411
- */
1412
- const INVALID_THINKING_SIGNATURE_PATTERN = /invalid\s+`?signature`?\s+in\s+`?thinking`?(?:\s+block)?/i;
1413
- const MISSING_THINKING_SIGNATURE_PATTERN = /thinking\.signature\b[^"\n]{0,32}\brequired\b/i;
1414
- const THINKING_PREFIX_BINDING_PATTERN = /(?:bound to a different conversation|block_binding\.prefix_mismatch_behavior|prefix_mismatch_behavior)/i;
1415
- /** Detects the preserved-thinking error caused by rewriting a signed block's conversation prefix. */
1416
- function isThinkingPrefixBindingError(message) {
1417
- return INVALID_THINKING_SIGNATURE_PATTERN.test(message) && THINKING_PREFIX_BINDING_PATTERN.test(message);
1418
- }
1419
- function isInvalidThinkingSignatureError(message) {
1420
- return INVALID_THINKING_SIGNATURE_PATTERN.test(message) || MISSING_THINKING_SIGNATURE_PATTERN.test(message);
1421
- }
1422
- const INPUT_TRANSFORMATION_PATH_PATTERN = /^messages\.(\d+)\.content\.(\d+)$/;
1423
- const PREFIX_BINDING_ERROR_PATH_PATTERN = /messages\.(\d+)\.content\.(\d+)/;
1424
- function thinkingReplayKey(block) {
1425
- if (block.type === "thinking") return block.signature ? `thinking:${block.signature}` : void 0;
1426
- if (block.type === "redacted_thinking") return block.data ? `redacted:${block.data}` : void 0;
1427
- }
1428
- function rememberPrefixDroppedThinking(params, transformations, state) {
1429
- if (!state) return;
1430
- let firstMessageIndex;
1431
- let firstBlockIndex;
1432
- for (const transformation of transformations) {
1433
- if (transformation.reason !== "prefix_binding_mismatch" || typeof transformation.path !== "string") continue;
1434
- const match = INPUT_TRANSFORMATION_PATH_PATTERN.exec(transformation.path);
1435
- if (!match) continue;
1436
- const messageIndex = Number(match[1]);
1437
- const blockIndex = Number(match[2]);
1438
- if (firstMessageIndex === void 0 || messageIndex < firstMessageIndex || messageIndex === firstMessageIndex && blockIndex < (firstBlockIndex ?? Number.POSITIVE_INFINITY)) {
1439
- firstMessageIndex = messageIndex;
1440
- firstBlockIndex = blockIndex;
1441
- }
1442
- }
1443
- if (firstMessageIndex === void 0 || firstBlockIndex === void 0) return;
1444
- for (let messageIndex = firstMessageIndex; messageIndex < params.messages.length; messageIndex++) {
1445
- const message = params.messages[messageIndex];
1446
- if (!message || !Array.isArray(message.content)) continue;
1447
- const blockStart = messageIndex === firstMessageIndex ? firstBlockIndex : 0;
1448
- for (let blockIndex = blockStart; blockIndex < message.content.length; blockIndex++) {
1449
- const key = thinkingReplayKey(message.content[blockIndex]);
1450
- if (key) state.prefixDroppedThinkingBlocks.add(key);
1451
- }
1452
- }
1453
- }
1454
- function rememberPrefixBindingFailure(params, message, state) {
1455
- if (!state) return false;
1456
- const match = PREFIX_BINDING_ERROR_PATH_PATTERN.exec(message);
1457
- let path = match ? `messages.${match[1]}.content.${match[2]}` : void 0;
1458
- if (!path) for (let messageIndex = 0; messageIndex < params.messages.length && !path; messageIndex++) {
1459
- const candidate = params.messages[messageIndex];
1460
- if (!candidate || !Array.isArray(candidate.content)) continue;
1461
- const blockIndex = candidate.content.findIndex((block) => thinkingReplayKey(block) !== void 0);
1462
- if (blockIndex >= 0) path = `messages.${messageIndex}.content.${blockIndex}`;
1463
- }
1464
- if (!path) return false;
1465
- rememberPrefixDroppedThinking(params, [{
1466
- type: "thinking_dropped",
1467
- reason: "prefix_binding_mismatch",
1468
- path
1469
- }], state);
1470
- return true;
1471
- }
1472
- function applyReportedInputTransformations(output, params, state, value, seen, replace = false) {
1473
- if (value === void 0 || value === null) return;
1474
- if (replace) {
1475
- seen.clear();
1476
- output.inputTransformations = [];
1477
- }
1478
- const fresh = [];
1479
- for (const transformation of parseAnthropicInputTransformations(value)) {
1480
- const key = JSON.stringify(transformation);
1481
- if (seen.has(key)) continue;
1482
- seen.add(key);
1483
- fresh.push(transformation);
1484
- }
1485
- if (fresh.length === 0) return;
1486
- output.inputTransformations = [...output.inputTransformations ?? [], ...fresh];
1487
- rememberPrefixDroppedThinking(params, fresh, state);
1488
- for (const transformation of fresh) {
1489
- if (transformation.reason !== "prefix_binding_mismatch") continue;
1490
- warn("anthropic: dropped thinking block after conversation prefix changed", {
1491
- model: output.model,
1492
- path: transformation.path
1493
- });
1494
- }
1495
- }
1496
- /**
1497
- * Prepend a pointed remediation to a thinking-signature rejection 400 when the
1498
- * model looks like an unmarked custom signing proxy
1499
- * (opaque baseUrl, `spec.reasoning: true`, no explicit
1500
- * `compat.replayUnsignedThinking` override). The default is native replay for
1501
- * the 3p reasoning majority (#2005); this hint turns the misconfigured-proxy
1502
- * case into a one-line fix instead of a silent retry loop (#4297).
1503
- */
1504
- function maybeAddReplayUnsignedThinkingHint(model, message) {
1505
- if (!isInvalidThinkingSignatureError(message) || isThinkingPrefixBindingError(message)) return message;
1506
- if (model.compat.officialEndpoint) return message;
1507
- if (model.compatConfig?.replayUnsignedThinking !== void 0) return message;
1508
- return `${`Provider "${model.provider}" looks like an Anthropic-compatible signing proxy: it rejected a replayed unsigned thinking block. Set \`compat.replayUnsignedThinking: false\` under \`providers.${model.provider}\` in your models.yml and retry. See https://github.com/can1357/oh-my-pi/issues/4297.`}\n\n${message}`;
1509
- }
1510
- const streamAnthropicOnce = (model, context, options) => {
1511
- const stream = new AssistantMessageEventStream();
1512
- (async () => {
1513
- const startTime = performance.now();
1514
- let firstTokenTime;
1515
- const output = {
1516
- role: "assistant",
1517
- content: [],
1518
- api: model.api,
1519
- provider: model.provider,
1520
- model: model.id,
1521
- usage: createEmptyUsage(),
1522
- stopReason: "stop",
1523
- timestamp: Date.now()
1524
- };
1525
- let rawRequestDump;
1526
- let activeAbortTracker = createAbortSourceTracker(options?.signal);
1527
- const onSseEvent = options?.onSseEvent;
1528
- const rawSseObserver = onSseEvent ? (event) => onSseEvent(event, model) : void 0;
1529
- try {
1530
- const copilotDynamicHeaders = model.provider === "github-copilot" ? buildCopilotDynamicHeaders({
1531
- messages: context.messages,
1532
- hasImages: hasCopilotVisionInput(context.messages),
1533
- premiumMultiplier: model.premiumMultiplier,
1534
- headers: {
1535
- ...model.headers,
1536
- ...options?.headers
1537
- },
1538
- integrationId: resolveCopilotRequestIdentity(options?.headers),
1539
- initiatorOverride: options?.initiatorOverride
1540
- }) : void 0;
1541
- if (copilotDynamicHeaders?.premiumRequests !== void 0) output.usage.premiumRequests = copilotDynamicHeaders.premiumRequests;
1542
- const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
1543
- const baseUrl = resolveAnthropicBaseUrl(model, apiKey) ?? "https://api.anthropic.com";
1544
- const supportsEagerToolInputStreaming = resolveEagerToolInputStreamingSupport(model, baseUrl);
1545
- const providerSessionState = getAnthropicProviderSessionState(options?.providerSessionState, baseUrl, model.id);
1546
- let disableStrictTools = (providerSessionState?.strictToolsDisabled ?? false) || (model.compat?.disableStrictTools ?? false);
1547
- let dropFastMode = providerSessionState?.fastModeDisabled ?? false;
1548
- let forceDemoteUnsignedThinking = providerSessionState?.replayUnsignedThinkingDisabled ?? false;
1549
- let dropAllThinking = false;
1550
- let prefixBindingRetryAttempted = false;
1551
- let prefixMismatchBehavior = model.thinking?.prefixBinding && model.compat.supportsThinkingBindingControls ? options?.anthropicPrefixMismatchBehavior ?? "drop_block" : void 0;
1552
- const controlBetas = resolveAnthropicControlBetas(model, prefixMismatchBehavior);
1553
- const mergedCallerHeaders = mergeHeaders(model.headers, options?.headers);
1554
- const umansGatewayWebSearchHeader = getUmansWebSearchHeader(model, mergedCallerHeaders);
1555
- let fallbacks = options?.fallbacks;
1556
- if (!model.compat.supportsOutputEffort && fallbacks?.some((entry) => entry.output_config?.effort !== void 0)) fallbacks = fallbacks.map((entry) => {
1557
- const outputConfig = entry.output_config;
1558
- if (outputConfig?.effort === void 0) return entry;
1559
- return {
1560
- ...entry,
1561
- output_config: outputConfig.task_budget === void 0 ? void 0 : { task_budget: outputConfig.task_budget }
1562
- };
1563
- });
1564
- const zeroOutputCacheRefresh = options?.anthropicCacheRefreshRequest === true;
1565
- let client;
1566
- let isOAuthToken;
1567
- if (options?.client) {
1568
- client = options.client;
1569
- isOAuthToken = false;
1570
- } else {
1571
- const extraBetas = normalizeExtraBetas(options?.betas);
1572
- if (model.provider === "anthropic" && options?.serviceTier === "priority" && !dropFastMode && !extraBetas.includes(fastModeBeta)) extraBetas.push(fastModeBeta);
1573
- if (options?.taskBudget && !extraBetas.includes(taskBudgetBeta)) extraBetas.push(taskBudgetBeta);
1574
- const sendsAdaptiveEffortPin = isAdaptiveOnlyThinking(model) && (options?.thinkingEnabled === false || model.compat.supportsForcedToolChoice && isForcedToolChoice(options?.toolChoice));
1575
- if (model.reasoning && model.compat.supportsOutputEffort && (options?.thinkingEnabled && options.effort !== "adaptive" || sendsAdaptiveEffortPin) && !extraBetas.includes(effortBeta)) extraBetas.push(effortBeta);
1576
- if (!isVertexRawPredictUrl(baseUrl)) {
1577
- for (const beta of controlBetas) if (!extraBetas.includes(beta)) extraBetas.push(beta);
1578
- }
1579
- if (model.reasoning && options?.thinkingEnabled && model.compat.supportsContextManagement !== false && !extraBetas.includes(contextManagementBeta)) extraBetas.push(contextManagementBeta);
1580
- if (!(options?.isOAuth ?? isAnthropicOAuthToken(apiKey)) && getCacheControl(model, options?.cacheRetention).cacheControl?.ttl === "1h" && !extraBetas.includes(extendedCacheTtlBeta)) extraBetas.push(extendedCacheTtlBeta);
1581
- if (fallbacks?.length) {
1582
- if (!extraBetas.includes(serverSideFallbackBeta)) extraBetas.push(serverSideFallbackBeta);
1583
- for (const entry of fallbacks) {
1584
- if (entry.speed === "fast" && !extraBetas.includes(fastModeBeta)) extraBetas.push(fastModeBeta);
1585
- if (entry.output_config?.effort && !extraBetas.includes(effortBeta)) extraBetas.push(effortBeta);
1586
- if (entry.output_config?.task_budget && !extraBetas.includes(taskBudgetBeta)) extraBetas.push(taskBudgetBeta);
1587
- }
1588
- }
1589
- const created = createClient(model, {
1590
- model,
1591
- apiKey,
1592
- extraBetas,
1593
- stream: !zeroOutputCacheRefresh,
1594
- interleavedThinking: options?.interleavedThinking ?? true,
1595
- headers: options?.headers,
1596
- dynamicHeaders: copilotDynamicHeaders?.headers,
1597
- isOAuth: options?.isOAuth,
1598
- hasTools: !!context.tools?.length,
1599
- thinkingEnabled: options?.thinkingEnabled,
1600
- thinkingDisplay: options?.thinkingDisplay,
1601
- fetch: options?.fetch,
1602
- maxRetryDelayMs: options?.maxRetryDelayMs,
1603
- sessionId: options?.sessionId ?? extractClaudeMetadataSessionId(options?.metadata?.user_id) ?? options?.promptCacheKey,
1604
- disableStrictTools
1605
- });
1606
- client = created.client;
1607
- isOAuthToken = created.isOAuthToken;
1608
- }
1609
- const preparedContext = await prepareAnthropicManyImageContext(context, model.input.includes("image"));
1610
- const prepareParams = async () => {
1611
- let nextParams = buildParams(model, preparedContext, isOAuthToken, options, {
1612
- disableStrictTools,
1613
- useUmansGatewayWebSearch: umansGatewayWebSearchHeader !== void 0,
1614
- forceDemoteUnsignedThinking,
1615
- supportsEagerToolInputStreaming,
1616
- prefixMismatchBehavior,
1617
- dropAllThinking,
1618
- droppedThinkingBlocks: providerSessionState?.prefixDroppedThinkingBlocks,
1619
- providerSessionState,
1620
- fallbacks
1621
- });
1622
- if (disableStrictTools) dropAnthropicStrictTools(nextParams);
1623
- if (dropFastMode) dropAnthropicFastMode(nextParams);
1624
- const replacementPayload = await options?.onPayload?.(nextParams, model);
1625
- if (replacementPayload !== void 0) nextParams = replacementPayload;
1626
- nextParams = toWellFormedDeep(nextParams);
1627
- rawRequestDump = {
1628
- provider: model.provider,
1629
- api: output.api,
1630
- model: model.id,
1631
- method: "POST",
1632
- url: `${baseUrl}/v1/messages${isOAuthToken ? "?beta=true" : ""}`,
1633
- body: nextParams
1634
- };
1635
- return nextParams;
1636
- };
1637
- let params = await prepareParams();
1638
- const seenInputTransformations = /* @__PURE__ */ new Set();
1639
- const idleTimeoutMs = options?.streamIdleTimeoutMs ?? getStreamIdleTimeoutMs(model.compat.streamIdleTimeoutMs);
1640
- const firstEventTimeoutMs = options?.streamFirstEventTimeoutMs ?? getStreamFirstEventTimeoutMs(idleTimeoutMs);
1641
- const requestTimeoutMs = firstEventTimeoutMs !== void 0 && firstEventTimeoutMs > 0 ? firstEventTimeoutMs : void 0;
1642
- if (zeroOutputCacheRefresh) {
1643
- const refreshParams = {
1644
- ...params,
1645
- max_tokens: 0,
1646
- stream: false
1647
- };
1648
- rawRequestDump = {
1649
- provider: model.provider,
1650
- api: output.api,
1651
- model: model.id,
1652
- method: "POST",
1653
- url: `${baseUrl}/v1/messages${isOAuthToken ? "?beta=true" : ""}`,
1654
- body: refreshParams
1655
- };
1656
- const { requestSignal } = activeAbortTracker;
1657
- const requestOptions = {
1658
- ...createSdkStreamRequestOptions(requestSignal, requestTimeoutMs),
1659
- maxRetries: 0
1660
- };
1661
- const request = isOAuthToken && client.beta ? client.beta.messages.create(refreshParams, requestOptions) : client.messages.create(refreshParams, requestOptions);
1662
- if (!hasAnthropicRawResponseRequest(request)) throw new AnthropicStreamEnvelopeError("Anthropic cache refresh request did not expose a raw response");
1663
- const response = await request.asResponse();
1664
- await notifyProviderResponse(options, response, model, response.headers.get("request-id"));
1665
- const body = await response.json();
1666
- if (!isRecord(body)) throw new AnthropicStreamEnvelopeError("Anthropic cache refresh returned a malformed response");
1667
- const wireUsage = parseAnthropicWireUsage(body.usage);
1668
- if (!wireUsage) throw new AnthropicStreamEnvelopeError("Anthropic cache refresh response omitted usage");
1669
- if (typeof body.id === "string") output.responseId = body.id;
1670
- applyReportedInputTransformations(output, params, providerSessionState, body.input_transformations, seenInputTransformations);
1671
- output.usage.input = wireUsage.input_tokens ?? 0;
1672
- output.usage.output = wireUsage.output_tokens ?? 0;
1673
- output.usage.cacheRead = wireUsage.cache_read_input_tokens ?? 0;
1674
- output.usage.cacheWrite = wireUsage.cache_creation_input_tokens ?? 0;
1675
- applyAnthropicUsageExtras(output.usage, wireUsage);
1676
- output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
1677
- calculateCost(model, output.usage, output.timestamp);
1678
- output.duration = performance.now() - startTime;
1679
- stream.push({
1680
- type: "start",
1681
- partial: output
1682
- });
1683
- stream.push({
1684
- type: "done",
1685
- reason: "stop",
1686
- message: output
1687
- });
1688
- stream.end();
1689
- return;
1690
- }
1691
- const serverSideFallback = !!fallbacks?.length;
1692
- const blocks = output.content;
1693
- const finalizeStreamBlock = (block, contentIndex) => {
1694
- if (block.type === "text") stream.push({
1695
- type: "text_end",
1696
- contentIndex,
1697
- content: block.text,
1698
- partial: output
1699
- });
1700
- else if (block.type === "thinking") {
1701
- const unwrappedThinking = unwrapAnthropicThinkingEnvelope(block.thinking);
1702
- if (unwrappedThinking !== void 0) {
1703
- block.thinking = unwrappedThinking;
1704
- block.thinkingSignature = void 0;
1705
- }
1706
- stream.push({
1707
- type: "thinking_end",
1708
- contentIndex,
1709
- content: block.thinking,
1710
- partial: output
1711
- });
1712
- } else if (block.type === "anthropicServerTool" && block.block.type === "server_tool_use") {
1713
- const partialJson = block[kStreamingPartialJson];
1714
- if (partialJson) try {
1715
- const input = parseJsonWithRepair(partialJson);
1716
- if (isRecord(input)) block.block.input = input;
1717
- else reportAnthropicEnvelopeAnomaly("server_tool_use input is not a JSON object");
1718
- } catch (parseError) {
1719
- reportAnthropicEnvelopeAnomaly(`server_tool_use ${block.block.id} input is not valid JSON: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
1720
- }
1721
- clearStreamingPartialJson(block);
1722
- } else if (block.type === "toolCall") {
1723
- const finalJson = block[kStreamingPartialJson].length > 0 ? block[kStreamingPartialJson] : JSON.stringify(block.arguments ?? {});
1724
- try {
1725
- block.arguments = parseJsonWithRepair(finalJson);
1726
- } catch (parseError) {
1727
- reportAnthropicEnvelopeAnomaly(`tool_use ${block.id} arguments are not valid JSON: ${parseError instanceof Error ? parseError.message : String(parseError)}`);
1728
- if (Object.keys(block.arguments ?? {}).length === 0) {
1729
- const maxLen = 512;
1730
- const truncatedJson = finalJson.length <= maxLen ? finalJson : `${finalJson.slice(0, maxLen)}… [truncated ${finalJson.length - maxLen} chars]`;
1731
- block.arguments = {
1732
- __parseError: parseError instanceof Error ? parseError.message : String(parseError),
1733
- __rawJson: truncatedJson
1734
- };
1735
- }
1736
- }
1737
- clearStreamingPartialJson(block);
1738
- stream.push({
1739
- type: "toolcall_end",
1740
- contentIndex,
1741
- toolCall: block,
1742
- partial: output
1743
- });
1744
- }
1745
- };
1746
- stream.push({
1747
- type: "start",
1748
- partial: output
1749
- });
1750
- let providerRetryAttempt = 0;
1751
- const firstEventTimeoutAbortError = new StreamTimeoutError("Anthropic stream timed out while waiting for the first event");
1752
- const idleTimeoutAbortError = new StreamTimeoutError("Anthropic stream stalled while waiting for the next event");
1753
- while (true) {
1754
- activeAbortTracker = createAbortSourceTracker(options?.signal);
1755
- const { requestSignal } = activeAbortTracker;
1756
- let injectedClientBetaHeaders;
1757
- if (options?.client !== void 0 && !isVertexRawPredictUrl(baseUrl)) {
1758
- for (const beta of controlBetas) injectedClientBetaHeaders = mergeAnthropicBetaHeader(injectedClientBetaHeaders ?? mergedCallerHeaders, beta);
1759
- if (params.output_config?.effort !== void 0) injectedClientBetaHeaders = mergeAnthropicBetaHeader(injectedClientBetaHeaders ?? mergedCallerHeaders, effortBeta);
1760
- }
1761
- const perRequestHeaders = umansGatewayWebSearchHeader || injectedClientBetaHeaders ? {
1762
- ...umansGatewayWebSearchHeader,
1763
- ...injectedClientBetaHeaders
1764
- } : void 0;
1765
- const requestOptions = {
1766
- ...createSdkStreamRequestOptions(requestSignal, requestTimeoutMs),
1767
- maxRetries: 0,
1768
- ...perRequestHeaders ? { headers: perRequestHeaders } : {}
1769
- };
1770
- const anthropicRequest = isOAuthToken && client.beta ? client.beta.messages.create({
1771
- ...params,
1772
- stream: true
1773
- }, requestOptions) : client.messages.create({
1774
- ...params,
1775
- stream: true
1776
- }, requestOptions);
1777
- let streamedReplayUnsafeContent = false;
1778
- try {
1779
- let requestTimeout;
1780
- if (requestTimeoutMs !== void 0) requestTimeout = setTimeout(() => activeAbortTracker.abortLocally(firstEventTimeoutAbortError), requestTimeoutMs);
1781
- let anthropicStream;
1782
- let response;
1783
- let requestId;
1784
- let recordsRawSseEvents;
1785
- try {
1786
- ({events: anthropicStream, response, requestId, recordsRawSseEvents} = await getAnthropicStreamResponse(anthropicRequest, requestSignal, rawSseObserver));
1787
- } catch (error) {
1788
- if (error instanceof AnthropicConnectionTimeoutError && !activeAbortTracker.wasCallerAbort()) throw firstEventTimeoutAbortError;
1789
- throw error;
1790
- } finally {
1791
- if (requestTimeout !== void 0) clearTimeout(requestTimeout);
1792
- }
1793
- await notifyProviderResponse(options, response, model, requestId);
1794
- let sawEvent = false;
1795
- let sawMessageStart = false;
1796
- let sawTerminalEnvelope = false;
1797
- let sawMessageStop = false;
1798
- let sawSplicedEnvelope = false;
1799
- const closedBlockIndexes = /* @__PURE__ */ new Set();
1800
- const openBlocks = /* @__PURE__ */ new Map();
1801
- let sawNonPingEvent = false;
1802
- let lastNonPingProgressAtMs = 0;
1803
- const pingProgressCapMs = idleTimeoutMs !== void 0 && idleTimeoutMs > 0 ? idleTimeoutMs * PING_PROGRESS_MAX_IDLE_MULTIPLIER : void 0;
1804
- const timedAnthropicStream = iterateWithIdleTimeout(anthropicStream, {
1805
- idleTimeoutMs,
1806
- firstItemTimeoutMs: firstEventTimeoutMs,
1807
- errorMessage: idleTimeoutAbortError.message,
1808
- firstItemErrorMessage: firstEventTimeoutAbortError.message,
1809
- onIdle: () => activeAbortTracker.abortLocally(idleTimeoutAbortError),
1810
- onFirstItemTimeout: () => activeAbortTracker.abortLocally(firstEventTimeoutAbortError),
1811
- abortSignal: options?.signal,
1812
- isProgressItem: (item) => {
1813
- if (item.type === "ping") {
1814
- if (!sawNonPingEvent) return false;
1815
- if (pingProgressCapMs === void 0) return true;
1816
- return Date.now() - lastNonPingProgressAtMs < pingProgressCapMs;
1817
- }
1818
- sawNonPingEvent = true;
1819
- lastNonPingProgressAtMs = Date.now();
1820
- return true;
1821
- }
1822
- });
1823
- const observedAnthropicStream = rawSseObserver && !recordsRawSseEvents ? observeDecodedAnthropicSdkEvents(timedAnthropicStream, rawSseObserver) : timedAnthropicStream;
1824
- for await (const event of observedAnthropicStream) {
1825
- sawEvent = true;
1826
- if (event.type === "message_start") {
1827
- if (sawMessageStart) {
1828
- reportAnthropicEnvelopeAnomaly("duplicate message_start event");
1829
- sawSplicedEnvelope = true;
1830
- continue;
1831
- }
1832
- sawMessageStart = true;
1833
- const startMessage = event.message;
1834
- if (startMessage?.id) output.responseId = startMessage.id;
1835
- applyReportedInputTransformations(output, params, providerSessionState, startMessage?.input_transformations, seenInputTransformations);
1836
- const startUsage = startMessage?.usage;
1837
- if (startUsage) {
1838
- applyAnthropicUsageExtras(output.usage, startUsage);
1839
- output.usage.input = startUsage.input_tokens || 0;
1840
- output.usage.output = startUsage.output_tokens || 0;
1841
- output.usage.cacheRead = startUsage.cache_read_input_tokens || 0;
1842
- output.usage.cacheWrite = startUsage.cache_creation_input_tokens || 0;
1843
- output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
1844
- if (serverSideFallback) {
1845
- const served = fallbackServedModelFromUsage(startUsage);
1846
- if (served) output.model = served;
1847
- if (!calculateFallbackTurnCost(model, output.usage, startUsage, output.timestamp)) calculateCost(model, output.usage, output.timestamp);
1848
- } else calculateCost(model, output.usage, output.timestamp);
1849
- } else reportAnthropicEnvelopeAnomaly("message_start missing usage");
1850
- continue;
1851
- }
1852
- if (!sawMessageStart) {
1853
- if (shouldIgnoreAnthropicPreambleEvent(event.type)) continue;
1854
- throw new AnthropicStreamEnvelopeError(`received ${event.type} before message_start`);
1855
- }
1856
- if (event.type === "content_block_start") {
1857
- if (sawTerminalEnvelope) {
1858
- reportAnthropicEnvelopeAnomaly(`received ${event.type} after terminal stop signal`);
1859
- continue;
1860
- }
1861
- if (openBlocks.has(event.index)) {
1862
- reportAnthropicEnvelopeAnomaly(`duplicate content_block_start index ${event.index}`);
1863
- continue;
1864
- }
1865
- if (sawSplicedEnvelope && closedBlockIndexes.has(event.index)) {
1866
- reportAnthropicEnvelopeAnomaly(`replayed content_block_start index ${event.index} after duplicate message_start`);
1867
- openBlocks.set(event.index, {
1868
- contentIndex: -1,
1869
- kind: "ignored"
1870
- });
1871
- continue;
1872
- }
1873
- if (!event.content_block?.type) {
1874
- reportAnthropicEnvelopeAnomaly("content_block_start missing content_block payload");
1875
- continue;
1876
- }
1877
- if (!firstTokenTime) firstTokenTime = performance.now();
1878
- if (event.content_block.type === "fallback") {
1879
- const fallback = parseAnthropicFallbackWireBlock(event.content_block);
1880
- if (!serverSideFallback || !fallback) {
1881
- if (!fallback) reportAnthropicEnvelopeAnomaly("fallback content_block missing model refs");
1882
- openBlocks.set(event.index, {
1883
- contentIndex: -1,
1884
- kind: "ignored"
1885
- });
1886
- continue;
1887
- }
1888
- const block = {
1889
- ...fallback,
1890
- [kStreamingBlockIndex]: event.index
1891
- };
1892
- output.content.push(block);
1893
- openBlocks.set(event.index, {
1894
- contentIndex: output.content.length - 1,
1895
- kind: "fallback"
1896
- });
1897
- output.model = fallback.to.model;
1898
- continue;
1899
- }
1900
- if (event.content_block.type === "text") {
1901
- streamedReplayUnsafeContent = true;
1902
- const block = {
1903
- type: "text",
1904
- text: "",
1905
- [kStreamingBlockIndex]: event.index
1906
- };
1907
- output.content.push(block);
1908
- const contentIndex = output.content.length - 1;
1909
- openBlocks.set(event.index, {
1910
- contentIndex,
1911
- kind: "text"
1912
- });
1913
- stream.push({
1914
- type: "text_start",
1915
- contentIndex,
1916
- partial: output
1917
- });
1918
- } else if (event.content_block.type === "thinking") {
1919
- streamedReplayUnsafeContent = true;
1920
- const block = {
1921
- type: "thinking",
1922
- thinking: event.content_block.thinking ?? "",
1923
- thinkingSignature: "",
1924
- [kStreamingBlockIndex]: event.index
1925
- };
1926
- output.content.push(block);
1927
- const contentIndex = output.content.length - 1;
1928
- openBlocks.set(event.index, {
1929
- contentIndex,
1930
- kind: "thinking"
1931
- });
1932
- stream.push({
1933
- type: "thinking_start",
1934
- contentIndex,
1935
- partial: output
1936
- });
1937
- if (block.thinking) stream.push({
1938
- type: "thinking_delta",
1939
- contentIndex,
1940
- delta: block.thinking,
1941
- partial: output
1942
- });
1943
- } else if (event.content_block.type === "redacted_thinking") {
1944
- streamedReplayUnsafeContent = true;
1945
- const block = {
1946
- type: "redactedThinking",
1947
- data: event.content_block.data,
1948
- [kStreamingBlockIndex]: event.index
1949
- };
1950
- output.content.push(block);
1951
- openBlocks.set(event.index, {
1952
- contentIndex: output.content.length - 1,
1953
- kind: "redactedThinking"
1954
- });
1955
- } else if (isAnthropicServerToolHistoryBlock(event.content_block) && (umansGatewayWebSearchHeader === void 0 || (event.content_block.type === "server_tool_use" ? event.content_block.name !== "web_search" : event.content_block.type !== "web_search_tool_result"))) {
1956
- streamedReplayUnsafeContent = true;
1957
- const block = {
1958
- type: "anthropicServerTool",
1959
- block: { ...event.content_block },
1960
- [kStreamingPartialJson]: "",
1961
- [kStreamingBlockIndex]: event.index
1962
- };
1963
- output.content.push(block);
1964
- openBlocks.set(event.index, {
1965
- contentIndex: output.content.length - 1,
1966
- kind: "anthropicServerTool"
1967
- });
1968
- } else if (event.content_block.type === "tool_use") {
1969
- streamedReplayUnsafeContent = true;
1970
- const block = {
1971
- type: "toolCall",
1972
- id: event.content_block.id,
1973
- name: decodeAnthropicToolName(event.content_block.name, isOAuthToken, model.compat.escapeBuiltinToolNames),
1974
- arguments: event.content_block.input ?? {},
1975
- [kStreamingPartialJson]: "",
1976
- [kStreamingBlockIndex]: event.index
1977
- };
1978
- output.content.push(block);
1979
- const contentIndex = output.content.length - 1;
1980
- openBlocks.set(event.index, {
1981
- contentIndex,
1982
- kind: "toolCall"
1983
- });
1984
- stream.push({
1985
- type: "toolcall_start",
1986
- contentIndex,
1987
- partial: output
1988
- });
1989
- } else openBlocks.set(event.index, {
1990
- contentIndex: -1,
1991
- kind: "ignored"
1992
- });
1993
- } else if (event.type === "content_block_delta") {
1994
- if (sawTerminalEnvelope) {
1995
- reportAnthropicEnvelopeAnomaly(`received ${event.type} after terminal stop signal`);
1996
- continue;
1997
- }
1998
- const openBlock = openBlocks.get(event.index);
1999
- if (!openBlock) {
2000
- reportAnthropicEnvelopeAnomaly(`received content_block_delta for unopened index ${event.index}`);
2001
- continue;
2002
- }
2003
- if (openBlock.kind === "ignored") continue;
2004
- if (!event.delta?.type) {
2005
- reportAnthropicEnvelopeAnomaly("content_block_delta missing delta payload");
2006
- continue;
2007
- }
2008
- const block = blocks[openBlock.contentIndex];
2009
- if (event.delta.type === "text_delta") {
2010
- if (openBlock.kind !== "text" || block?.type !== "text") {
2011
- reportAnthropicEnvelopeAnomaly(`received text_delta for ${openBlock.kind} block`);
2012
- continue;
2013
- }
2014
- streamedReplayUnsafeContent = true;
2015
- block.text += event.delta.text;
2016
- stream.push({
2017
- type: "text_delta",
2018
- contentIndex: openBlock.contentIndex,
2019
- delta: event.delta.text,
2020
- partial: output
2021
- });
2022
- } else if (event.delta.type === "thinking_delta") {
2023
- if (openBlock.kind !== "thinking" || block?.type !== "thinking") {
2024
- reportAnthropicEnvelopeAnomaly(`received thinking_delta for ${openBlock.kind} block`);
2025
- continue;
2026
- }
2027
- streamedReplayUnsafeContent = true;
2028
- block.thinking += event.delta.thinking;
2029
- stream.push({
2030
- type: "thinking_delta",
2031
- contentIndex: openBlock.contentIndex,
2032
- delta: event.delta.thinking,
2033
- partial: output
2034
- });
2035
- } else if (event.delta.type === "input_json_delta") {
2036
- if (openBlock.kind === "anthropicServerTool" && block?.type === "anthropicServerTool" && block.block.type === "server_tool_use") {
2037
- block[kStreamingPartialJson] = (block[kStreamingPartialJson] ?? "") + event.delta.partial_json;
2038
- continue;
2039
- }
2040
- if (openBlock.kind !== "toolCall" || block?.type !== "toolCall") {
2041
- reportAnthropicEnvelopeAnomaly(`received input_json_delta for ${openBlock.kind} block`);
2042
- continue;
2043
- }
2044
- streamedReplayUnsafeContent = true;
2045
- block[kStreamingPartialJson] += event.delta.partial_json;
2046
- const throttled = parseStreamingJsonThrottled(block[kStreamingPartialJson], block[kStreamingLastParseLen] ?? 0);
2047
- if (throttled) {
2048
- block.arguments = throttled.value;
2049
- block[kStreamingLastParseLen] = throttled.parsedLen;
2050
- }
2051
- stream.push({
2052
- type: "toolcall_delta",
2053
- contentIndex: openBlock.contentIndex,
2054
- delta: event.delta.partial_json,
2055
- partial: output
2056
- });
2057
- } else if (event.delta.type === "signature_delta") {
2058
- if (openBlock.kind !== "thinking" || block?.type !== "thinking") {
2059
- reportAnthropicEnvelopeAnomaly(`received signature_delta for ${openBlock.kind} block`);
2060
- continue;
2061
- }
2062
- streamedReplayUnsafeContent = true;
2063
- block.thinkingSignature = block.thinkingSignature || "";
2064
- block.thinkingSignature += event.delta.signature;
2065
- }
2066
- } else if (event.type === "content_block_stop") {
2067
- if (sawTerminalEnvelope) {
2068
- reportAnthropicEnvelopeAnomaly(`received ${event.type} after terminal stop signal`);
2069
- continue;
2070
- }
2071
- const openBlock = openBlocks.get(event.index);
2072
- if (!openBlock) {
2073
- reportAnthropicEnvelopeAnomaly(`received content_block_stop for unopened index ${event.index}`);
2074
- continue;
2075
- }
2076
- if (openBlock.kind === "ignored") {
2077
- openBlocks.delete(event.index);
2078
- continue;
2079
- }
2080
- const block = blocks[openBlock.contentIndex];
2081
- if (!block || block.type !== openBlock.kind) {
2082
- reportAnthropicEnvelopeAnomaly(`content_block_stop kind mismatch for index ${event.index}`);
2083
- openBlocks.delete(event.index);
2084
- continue;
2085
- }
2086
- openBlocks.delete(event.index);
2087
- closedBlockIndexes.add(event.index);
2088
- finalizeStreamBlock(block, openBlock.contentIndex);
2089
- } else if (event.type === "message_delta") {
2090
- if (sawTerminalEnvelope) {
2091
- reportAnthropicEnvelopeAnomaly("received message_delta after terminal stop signal");
2092
- continue;
2093
- }
2094
- const delta = event.delta;
2095
- applyReportedInputTransformations(output, params, providerSessionState, event.input_transformations, seenInputTransformations, true);
2096
- const rawStopReason = delta?.stop_reason;
2097
- if (rawStopReason) {
2098
- output.stopReason = mapStopReason(rawStopReason);
2099
- sawTerminalEnvelope = true;
2100
- }
2101
- if (output.stopReason === "error") {
2102
- const stopDetails = delta?.stop_details;
2103
- output.stopDetails = stopDetails ?? (rawStopReason ? { type: rawStopReason } : null);
2104
- if (stopDetails?.type === "refusal") {
2105
- const explanation = stopDetails.explanation?.trim();
2106
- const category = stopDetails.category;
2107
- const label = category ? `Refusal (${category})` : "Refusal";
2108
- output.errorMessage = explanation ? `${label}: ${explanation}` : label;
2109
- } else if (!output.errorMessage) output.errorMessage = rawStopReason === "refusal" ? "Refusal (no details provided)" : rawStopReason === "sensitive" ? "Content flagged by safety filters" : `Anthropic stream ended with stop_reason: ${rawStopReason ?? "unknown"}`;
2110
- }
2111
- const deltaUsage = event.usage;
2112
- if (deltaUsage) {
2113
- if (deltaUsage.input_tokens != null) output.usage.input = deltaUsage.input_tokens;
2114
- if (deltaUsage.output_tokens != null) output.usage.output = deltaUsage.output_tokens;
2115
- if (deltaUsage.cache_read_input_tokens != null) output.usage.cacheRead = deltaUsage.cache_read_input_tokens;
2116
- if (deltaUsage.cache_creation_input_tokens != null) output.usage.cacheWrite = deltaUsage.cache_creation_input_tokens;
2117
- applyAnthropicUsageExtras(output.usage, deltaUsage);
2118
- output.usage.totalTokens = output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
2119
- if (serverSideFallback) {
2120
- const served = fallbackServedModelFromUsage(deltaUsage);
2121
- if (served) output.model = served;
2122
- if (!calculateFallbackTurnCost(model, output.usage, deltaUsage, output.timestamp)) calculateCost(model, output.usage, output.timestamp);
2123
- } else calculateCost(model, output.usage, output.timestamp);
2124
- }
2125
- } else if (event.type === "message_stop") {
2126
- sawTerminalEnvelope = true;
2127
- sawMessageStop = true;
2128
- break;
2129
- }
2130
- }
2131
- const firstEventTimeoutError = activeAbortTracker.getLocalAbortReason();
2132
- if (firstEventTimeoutError) throw firstEventTimeoutError;
2133
- if (activeAbortTracker.wasCallerAbort()) throw new AbortError();
2134
- if (!sawEvent || !sawMessageStart) throw new AnthropicStreamEnvelopeError("stream ended before message_start");
2135
- if (!sawTerminalEnvelope) throw new AnthropicStreamEnvelopeError("stream ended before message_stop");
2136
- if (!sawMessageStop) reportAnthropicEnvelopeAnomaly("stream ended before message_stop");
2137
- if (openBlocks.size > 0) {
2138
- for (const [openIndex, openBlock] of openBlocks) {
2139
- reportAnthropicEnvelopeAnomaly(`stream ended with an unterminated ${openBlock.kind} block at index ${openIndex}`);
2140
- if (openBlock.kind === "ignored" || openBlock.contentIndex < 0) continue;
2141
- const danglingBlock = blocks[openBlock.contentIndex];
2142
- if (danglingBlock) finalizeStreamBlock(danglingBlock, openBlock.contentIndex);
2143
- }
2144
- openBlocks.clear();
2145
- }
2146
- if (output.stopReason === "aborted" || output.stopReason === "error") throw new ProviderResponseError(output.errorMessage ?? "An unknown error occurred", {
2147
- provider: model.provider,
2148
- kind: "output"
2149
- });
2150
- break;
2151
- } catch (streamError) {
2152
- const streamFailure = activeAbortTracker.getLocalAbortReason() ?? streamError;
2153
- if (!disableStrictTools && firstTokenTime === void 0 && hasStrictAnthropicTools(params) && isGrammarError(streamFailure)) {
2154
- warn("anthropic: strict tools rejected, retrying without strict tools", {
2155
- model: model.id,
2156
- error: await finalizeErrorMessage(streamFailure, rawRequestDump)
2157
- });
2158
- if (providerSessionState) providerSessionState.strictToolsDisabled = true;
2159
- disableStrictTools = true;
2160
- params = await prepareParams();
2161
- providerRetryAttempt = 0;
2162
- output.content.length = 0;
2163
- output.model = model.id;
2164
- output.responseId = void 0;
2165
- output.errorMessage = void 0;
2166
- output.providerPayload = void 0;
2167
- output.usage = createEmptyUsage(copilotDynamicHeaders?.premiumRequests);
2168
- output.stopReason = "stop";
2169
- firstTokenTime = void 0;
2170
- continue;
2171
- }
2172
- const streamFailureMessage = streamFailure instanceof Error ? streamFailure.message : String(streamFailure);
2173
- if (!prefixBindingRetryAttempted && options?.anthropicPrefixMismatchBehavior !== "error" && firstTokenTime === void 0 && !streamedReplayUnsafeContent && isThinkingPrefixBindingError(streamFailureMessage)) {
2174
- warn("anthropic: thinking prefix changed, stripping bound thinking and retrying", {
2175
- provider: model.provider,
2176
- model: model.id,
2177
- baseUrl
2178
- });
2179
- prefixBindingRetryAttempted = true;
2180
- prefixMismatchBehavior = void 0;
2181
- dropAllThinking = !rememberPrefixBindingFailure(params, streamFailureMessage, providerSessionState);
2182
- params = await prepareParams();
2183
- providerRetryAttempt = 0;
2184
- output.content.length = 0;
2185
- output.model = model.id;
2186
- output.responseId = void 0;
2187
- output.errorMessage = void 0;
2188
- output.inputTransformations = void 0;
2189
- output.providerPayload = void 0;
2190
- output.usage = createEmptyUsage(copilotDynamicHeaders?.premiumRequests);
2191
- output.stopReason = "stop";
2192
- firstTokenTime = void 0;
2193
- continue;
2194
- }
2195
- if (!forceDemoteUnsignedThinking && firstTokenTime === void 0 && !streamedReplayUnsafeContent && !isThinkingPrefixBindingError(streamFailureMessage) && isInvalidThinkingSignatureError(streamFailureMessage)) {
2196
- warn("anthropic: signing proxy detected (thinking signature rejected), demoting unsigned thinking and retrying", {
2197
- provider: model.provider,
2198
- model: model.id,
2199
- baseUrl,
2200
- error: streamFailureMessage
2201
- });
2202
- if (providerSessionState) providerSessionState.replayUnsignedThinkingDisabled = true;
2203
- forceDemoteUnsignedThinking = true;
2204
- params = await prepareParams();
2205
- providerRetryAttempt = 0;
2206
- output.content.length = 0;
2207
- output.model = model.id;
2208
- output.responseId = void 0;
2209
- output.errorMessage = void 0;
2210
- output.providerPayload = void 0;
2211
- output.usage = createEmptyUsage(copilotDynamicHeaders?.premiumRequests);
2212
- output.stopReason = "stop";
2213
- firstTokenTime = void 0;
2214
- continue;
2215
- }
2216
- if (!dropFastMode && model.provider === "anthropic" && options?.serviceTier === "priority" && firstTokenTime === void 0 && isFastModeUnsupported(streamFailure)) {
2217
- debug("anthropic: fast mode unsupported, retrying without speed", {
2218
- model: model.id,
2219
- error: streamFailure instanceof Error ? streamFailure.message : String(streamFailure)
2220
- });
2221
- if (providerSessionState) providerSessionState.fastModeDisabled = true;
2222
- dropFastMode = true;
2223
- params = await prepareParams();
2224
- providerRetryAttempt = 0;
2225
- output.content.length = 0;
2226
- output.model = model.id;
2227
- output.responseId = void 0;
2228
- output.errorMessage = void 0;
2229
- output.providerPayload = void 0;
2230
- output.usage = createEmptyUsage(copilotDynamicHeaders?.premiumRequests);
2231
- output.stopReason = "stop";
2232
- firstTokenTime = void 0;
2233
- continue;
2234
- }
2235
- const isTransientEnvelopeFailure = isTransientStreamParseError(streamFailure) || isStreamEnvelopeError(streamFailure);
2236
- const isLocalIdleTimeout = streamFailure === idleTimeoutAbortError || streamFailure instanceof Error && streamFailure.message === idleTimeoutAbortError.message;
2237
- const canRetryTransientEnvelopeFailure = isTransientEnvelopeFailure && !streamedReplayUnsafeContent;
2238
- const canRetryProviderFailure = !isLocalIdleTimeout && firstTokenTime === void 0 && !streamedReplayUnsafeContent && isProviderRetryableError(streamFailure);
2239
- if (activeAbortTracker.wasCallerAbort() || providerRetryAttempt >= PROVIDER_MAX_RETRIES || !canRetryTransientEnvelopeFailure && !canRetryProviderFailure) throw streamFailure;
2240
- providerRetryAttempt++;
2241
- const backoffDelayMs = calculateAnthropicRetryDelayMs(providerRetryAttempt - 1);
2242
- const headerDelayMs = getRetryAfterMsFromHeaders(getHeadersFromError(streamFailure));
2243
- const maxRetryDelayMs = options?.maxRetryDelayMs ?? 6e4;
2244
- if (headerDelayMs !== void 0 && maxRetryDelayMs > 0 && headerDelayMs > maxRetryDelayMs) throw streamFailure;
2245
- const delayMs = headerDelayMs !== void 0 ? Math.max(headerDelayMs, backoffDelayMs) : backoffDelayMs;
2246
- if (options?.providerRetryWait) await options.providerRetryWait(delayMs, options.signal);
2247
- else await scheduler.wait(delayMs, { signal: options?.signal });
2248
- output.content.length = 0;
2249
- output.model = model.id;
2250
- output.responseId = void 0;
2251
- output.errorMessage = void 0;
2252
- output.stopDetails = void 0;
2253
- output.providerPayload = void 0;
2254
- output.usage = createEmptyUsage(copilotDynamicHeaders?.premiumRequests);
2255
- output.stopReason = "stop";
2256
- firstTokenTime = void 0;
2257
- }
2258
- }
2259
- output.duration = performance.now() - startTime;
2260
- if (firstTokenTime) output.ttft = firstTokenTime - startTime;
2261
- if (dropFastMode && model.provider === "anthropic" && options?.serviceTier === "priority") output.disabledFeatures = [...output.disabledFeatures ?? [], "priority"];
2262
- if (forceDemoteUnsignedThinking && model.compat.replayUnsignedThinking) output.disabledFeatures = [...output.disabledFeatures ?? [], "unsigned-thinking-replay"];
2263
- stream.push({
2264
- type: "done",
2265
- reason: output.stopReason,
2266
- message: output
2267
- });
2268
- stream.end();
2269
- } catch (error) {
2270
- for (const block of output.content) if (block.type === "toolCall") clearStreamingPartialJson(block);
2271
- const result = await finalize(error, {
2272
- api: model.api,
2273
- provider: model.provider,
2274
- abortTracker: activeAbortTracker,
2275
- rawRequestDump
2276
- });
2277
- output.stopReason = result.stopReason;
2278
- output.errorStatus = result.status;
2279
- output.errorId = result.id;
2280
- output.errorMessage = maybeAddReplayUnsignedThinkingHint(model, result.message);
2281
- output.duration = performance.now() - startTime;
2282
- if (firstTokenTime) output.ttft = firstTokenTime - startTime;
2283
- stream.push({
2284
- type: "error",
2285
- reason: output.stopReason,
2286
- error: output
2287
- });
2288
- stream.end();
2289
- }
2290
- })();
2291
- return stream;
2292
- };
2293
- /**
2294
- * Public entry: retry benign empty completions before they reach the agent
2295
- * loop. The inner attempt owns Anthropic provider-failure retries.
2296
- */
2297
- const streamAnthropic = (model, context, options) => withReplaySafeStreamRetry(model, context, options, streamAnthropicOnce, { retryEmptyCompletion: true });
2298
- function buildAnthropicSystemBlocks(systemPrompt, options = {}) {
2299
- const { includeClaudeCodeInstruction = false, extraInstructions = [], firstUserMessageText, cacheControl } = options;
2300
- const sanitizedPrompts = normalizeSystemPrompts(systemPrompt);
2301
- const trimmedInstructions = extraInstructions.map((instruction) => instruction.trim()).filter(Boolean);
2302
- const hasBillingHeader = sanitizedPrompts.some((prompt) => prompt.startsWith(CLAUDE_BILLING_HEADER_PREFIX));
2303
- if (includeClaudeCodeInstruction && !hasBillingHeader) {
2304
- const blocks = [{
2305
- type: "text",
2306
- text: createClaudeBillingHeader(firstUserMessageText ?? "")
2307
- }, {
2308
- type: "text",
2309
- text: claudeCodeSystemInstruction,
2310
- cache_control: cacheControl ? cloneAnthropicCacheControl(cacheControl) : { type: "ephemeral" }
2311
- }];
2312
- for (const instruction of trimmedInstructions) blocks.push({
2313
- type: "text",
2314
- text: instruction
2315
- });
2316
- for (const prompt of sanitizedPrompts) blocks.push({
2317
- type: "text",
2318
- text: prompt
2319
- });
2320
- return blocks;
2321
- }
2322
- const blocks = [];
2323
- for (const instruction of trimmedInstructions) blocks.push({
2324
- type: "text",
2325
- text: instruction
2326
- });
2327
- for (const prompt of sanitizedPrompts) blocks.push({
2328
- type: "text",
2329
- text: prompt
2330
- });
2331
- return blocks.length > 0 ? blocks : void 0;
2332
- }
2333
- function normalizeExtraBetas(betas) {
2334
- if (!betas) return [];
2335
- return (Array.isArray(betas) ? betas : betas.split(",")).map((beta) => beta.trim()).filter((beta) => beta.length > 0);
2336
- }
2337
- function buildAnthropicClientOptions(args) {
2338
- const { model, apiKey, extraBetas = [], stream = true, interleavedThinking = true, headers, dynamicHeaders, hasTools = false, thinkingEnabled = false, isOAuth, maxRetryDelayMs, sessionId, disableStrictTools: disableStrictToolsOverride } = args;
2339
- const compat = model.compat;
2340
- const disableStrictTools = disableStrictToolsOverride ?? compat.disableStrictTools;
2341
- const baseUrl = resolveAnthropicBaseUrl(model, apiKey);
2342
- const needsInterleavedBeta = interleavedThinking && (!model.thinking?.supportsDisplay || !isOfficialAnthropicApiUrl(baseUrl) && (isAnthropicSigningProxyUrl(baseUrl) || compat.signingEndpoint && !compat.officialEndpoint) && !isVertexRawPredictUrl(baseUrl ?? "") && !hostMatchesUrl(baseUrl, "githubCopilot"));
2343
- const oauthToken = isOAuth ?? isAnthropicOAuthToken(apiKey);
2344
- const supportsEagerToolInputStreaming = resolveEagerToolInputStreamingSupport(model, baseUrl);
2345
- const needsFineGrainedToolStreamingBeta = hasTools && isOfficialAnthropicApiUrl(baseUrl) && !supportsEagerToolInputStreaming;
2346
- const foundryCustomHeaders = resolveAnthropicCustomHeaders(model, baseUrl);
2347
- const fetchOptions = {
2348
- ...buildClaudeCodeTlsFetchOptions(model, baseUrl),
2349
- timeout: false
2350
- };
2351
- const baseFetch = args.fetch ?? fetch;
2352
- const cchFetch = oauthToken ? wrapFetchForCch(baseFetch) : baseFetch;
2353
- if (model.provider === "github-copilot") {
2354
- const copilotApiKey = parseGitHubCopilotApiKey(apiKey).accessToken;
2355
- const betaFeatures = [...extraBetas];
2356
- const defaultHeaders = mergeHeaders({
2357
- Accept: stream ? "text/event-stream" : "application/json",
2358
- "Content-Type": "application/json",
2359
- "anthropic-version": "2023-06-01",
2360
- "Anthropic-Dangerous-Direct-Browser-Access": "true",
2361
- Authorization: `Bearer ${copilotApiKey}`,
2362
- ...betaFeatures.length > 0 ? { "anthropic-beta": buildBetaHeader([], betaFeatures) } : {}
2363
- }, model.headers, dynamicHeaders, headers);
2364
- applyInferenceHeaders(defaultHeaders, {
2365
- provider: model.provider,
2366
- protocol: "anthropic",
2367
- sessionId
2368
- });
2369
- return {
2370
- isOAuthToken: false,
2371
- apiKey: null,
2372
- authToken: copilotApiKey,
2373
- baseURL: baseUrl,
2374
- maxRetries: 5,
2375
- maxRetryDelayMs,
2376
- defaultHeaders,
2377
- fetch: wrapFetchForCopilotFallback(cchFetch, true, resolveCopilotRequestIdentity(headers)),
2378
- fetchOptions
2379
- };
2380
- }
2381
- const betaFeatures = [...extraBetas];
2382
- if (needsFineGrainedToolStreamingBeta) betaFeatures.push(fineGrainedToolStreamingBeta);
2383
- if (needsInterleavedBeta) betaFeatures.push(interleavedThinkingBeta);
2384
- const defaultHeaders = buildAnthropicHeaders({
2385
- apiKey,
2386
- baseUrl,
2387
- isOAuth: oauthToken,
2388
- extraBetas: betaFeatures,
2389
- stream,
2390
- modelHeaders: mergeHeaders(model.headers, foundryCustomHeaders, getUmansWebSearchHeader(model, mergeHeaders(model.headers, headers)), headers, dynamicHeaders),
2391
- isCloudflareAiGateway: model.provider === "cloudflare-ai-gateway",
2392
- allowAnthropicHeaderOverrides: model.compat.allowAnthropicHeaderOverrides,
2393
- claudeCodeSessionId: sessionId,
2394
- claudeCodeBetas: oauthToken ? buildClaudeCodeBetas({
2395
- agentRequest: hasTools || thinkingEnabled,
2396
- thinkingRequest: thinkingEnabled,
2397
- disableStrictTools,
2398
- supportsContextManagement: model.compat.supportsContextManagement
2399
- }) : []
2400
- });
2401
- applyInferenceHeaders(defaultHeaders, {
2402
- provider: model.provider,
2403
- protocol: "anthropic",
2404
- sessionId
2405
- });
2406
- if (model.provider === "cloudflare-ai-gateway") return {
2407
- isOAuthToken: false,
2408
- apiKey: null,
2409
- authToken: null,
2410
- baseURL: baseUrl,
2411
- maxRetries: 5,
2412
- maxRetryDelayMs,
2413
- defaultHeaders,
2414
- fetch: cchFetch,
2415
- fetchOptions
2416
- };
2417
- if (model.provider === "opencode-go" || model.provider === "opencode-zen" || model.provider === "umans") {
2418
- delete defaultHeaders.Authorization;
2419
- return {
2420
- isOAuthToken: false,
2421
- apiKey,
2422
- authToken: null,
2423
- baseURL: baseUrl,
2424
- maxRetries: 5,
2425
- maxRetryDelayMs,
2426
- defaultHeaders,
2427
- fetch: cchFetch,
2428
- fetchOptions
2429
- };
2430
- }
2431
- const authorizationHeader = getHeaderCaseInsensitive(defaultHeaders, "Authorization");
2432
- const shouldSuppressClientApiKey = !oauthToken && !model.compat.officialEndpoint && typeof authorizationHeader === "string";
2433
- return {
2434
- isOAuthToken: oauthToken,
2435
- apiKey: oauthToken || shouldSuppressClientApiKey ? null : apiKey,
2436
- authToken: oauthToken ? apiKey : void 0,
2437
- baseURL: baseUrl,
2438
- maxRetries: 5,
2439
- maxRetryDelayMs,
2440
- defaultHeaders,
2441
- fetch: cchFetch,
2442
- fetchOptions
2443
- };
2444
- }
2445
- function createClient(model, args) {
2446
- const { isOAuthToken: oauthToken, ...clientOptions } = buildAnthropicClientOptions({
2447
- ...args,
2448
- model
2449
- });
2450
- return {
2451
- client: new AnthropicMessagesClient(clientOptions),
2452
- isOAuthToken: oauthToken
2453
- };
2454
- }
2455
- function disableThinkingIfToolChoiceForced(params, model) {
2456
- const toolChoice = params.tool_choice;
2457
- if (!toolChoice) return;
2458
- if (toolChoice.type !== "any" && toolChoice.type !== "tool") return;
2459
- delete params.thinking;
2460
- delete params.context_management;
2461
- if (isAdaptiveOnlyThinking(model) && model.compat.supportsOutputEffort) {
2462
- const outputConfig = params.output_config ?? {};
2463
- outputConfig.effort = "low";
2464
- params.output_config = outputConfig;
2465
- return;
2466
- }
2467
- const outputConfig = params.output_config;
2468
- if (!outputConfig) return;
2469
- delete outputConfig.effort;
2470
- if (Object.keys(outputConfig).length === 0) delete params.output_config;
2471
- }
2472
- function ensureMaxTokensForThinking(params, maxAllowedTokens) {
2473
- const thinking = params.thinking;
2474
- if (thinking?.type !== "enabled") return;
2475
- const budgetTokens = thinking.budget_tokens ?? 0;
2476
- if (budgetTokens <= 0) return;
2477
- const currentMaxTokens = Math.min(params.max_tokens ?? maxAllowedTokens, maxAllowedTokens);
2478
- const raisedMaxTokens = Math.min(Math.max(currentMaxTokens, budgetTokens + OUTPUT_FALLBACK_BUFFER), maxAllowedTokens);
2479
- params.max_tokens = raisedMaxTokens;
2480
- if (budgetTokens + 4e3 <= raisedMaxTokens) return;
2481
- const clampedBudget = raisedMaxTokens - OUTPUT_FALLBACK_BUFFER;
2482
- if (clampedBudget <= 0) throw new ConfigurationError(`Anthropic thinking budget requires max_tokens greater than ${OUTPUT_FALLBACK_BUFFER}; got ${raisedMaxTokens}`);
2483
- thinking.budget_tokens = clampedBudget;
2484
- }
2485
- function applyCacheControlToLastBlock(blocks, cacheControl) {
2486
- for (let index = blocks.length - 1; index >= 0; index--) {
2487
- const block = blocks[index];
2488
- if (block.type === "thinking" || block.type === "redacted_thinking" || block.type === "fallback" || block.type === "tool_addition" || block.type === "tool_removal") continue;
2489
- if ("cache_control" in block && block.cache_control != null) return false;
2490
- blocks[index] = {
2491
- ...block,
2492
- cache_control: cloneAnthropicCacheControl(cacheControl)
2493
- };
2494
- return true;
2495
- }
2496
- return false;
2497
- }
2498
- function applyPromptCaching(params, cacheControl) {
2499
- if (!cacheControl) return;
2500
- const trailingIndex = params.messages.length - 1;
2501
- const trailingMessage = params.messages[trailingIndex];
2502
- const messageEnd = trailingMessage?.role === "user" && trailingMessage.content === "Continue." && params.messages[trailingIndex - 1]?.role === "assistant" ? trailingIndex - 1 : trailingIndex;
2503
- let eligibleMessages = 0;
2504
- for (let index = messageEnd; index >= 0 && eligibleMessages < 2; index--) {
2505
- const message = params.messages[index];
2506
- if (!message || message.clear_at === "next_user_message") continue;
2507
- eligibleMessages++;
2508
- if (typeof message.content === "string") message.content = [{
2509
- type: "text",
2510
- text: message.content,
2511
- cache_control: cloneAnthropicCacheControl(cacheControl)
2512
- }];
2513
- else if (Array.isArray(message.content)) applyCacheControlToLastBlock(message.content, cacheControl);
2514
- }
2515
- }
2516
- /**
2517
- * Anchor cache_control on the stable request head — the last (non-deferred)
2518
- * tool definition and the last system block. The canonical cache order is
2519
- * tools → system → messages, so a breakpoint on the final system block caches
2520
- * the entire tools+system prefix, and the extra tool breakpoint keeps the tool
2521
- * definitions cached even when the system text changes. This guarantees the
2522
- * large, unchanging head is a cache hit on every turn regardless of how the
2523
- * message tail churns — the breakpoint placement first-party Anthropic clients
2524
- * (Claude Code, Pi) use. Without it, the general API-key path anchors only the
2525
- * moving message tail, so tail churn re-writes the whole head uncached.
2526
- *
2527
- * Anthropic allows at most 4 cache breakpoints per request. At most one is
2528
- * spent on tools and one on system here, leaving two for the message tail in
2529
- * `applyPromptCaching`. Head caching is skipped entirely when the head is
2530
- * already anchored — the OAuth Claude Code path caches its own instruction
2531
- * block at buildAnthropicSystemBlocks, and via the canonical tools → system
2532
- * order that single system breakpoint already caches every preceding tool. Re-
2533
- * anchoring there would be redundant, would change the OAuth wire, and could
2534
- * push a tool-heavy request over the 4-breakpoint budget, so the general
2535
- * API-key path (nothing cached upstream) is the only one decorated here.
2536
- *
2537
- * Runs after the byte-stability plane (planStableAnthropicSystem /
2538
- * planStableAnthropicTools), which hands back fresh block/tool copies each turn
2539
- * and keys tool identity off a fingerprint that excludes cache_control — so
2540
- * decorating here is byte-stable across turns and never forces a re-baseline.
2541
- */
2542
- function applyHeadCaching(systemBlocks, tools, cacheControl) {
2543
- if (!cacheControl) return;
2544
- if ((systemBlocks?.some((block) => block.cache_control != null) ?? false) || (tools?.some((tool) => tool.cache_control != null) ?? false)) return;
2545
- if (tools && tools.length > 0) for (let index = tools.length - 1; index >= 0; index--) {
2546
- const tool = tools[index];
2547
- if (!tool || tool.defer_loading) continue;
2548
- tool.cache_control = cloneAnthropicCacheControl(cacheControl);
2549
- break;
2550
- }
2551
- if (systemBlocks && systemBlocks.length > 0) {
2552
- const lastBlock = systemBlocks[systemBlocks.length - 1];
2553
- if (lastBlock) lastBlock.cache_control = cloneAnthropicCacheControl(cacheControl);
2554
- }
2555
- }
2556
- function usesAdaptiveThinkingTagOnly(model) {
2557
- const thinking = model.thinking;
2558
- if (thinking?.mode !== "anthropic-adaptive") return false;
2559
- const effortMap = thinking.effortMap;
2560
- if (!effortMap) return false;
2561
- for (const effort of thinking.efforts) if (effortMap[effort] !== "adaptive") return false;
2562
- return thinking.efforts.length > 0;
2563
- }
2564
- /**
2565
- * True for adaptive-only Claude models (Opus 4.6+, Sonnet 4.6+, Fable/Mythos 5)
2566
- * that reject `thinking.type: "disabled"`. Turning thinking off on these models
2567
- * means omitting the `thinking` field entirely and pinning the lowest adaptive
2568
- * effort — a bare omission defaults to adaptive thinking ON. Excludes MiniMax,
2569
- * which drives adaptive thinking through the `thinking.type: "adaptive"` tag
2570
- * itself rather than `output_config.effort`.
2571
- */
2572
- function isAdaptiveOnlyThinking(model) {
2573
- return model.thinking?.mode === "anthropic-adaptive" && !model.compat.disableAdaptiveThinking && !usesAdaptiveThinkingTagOnly(model);
2574
- }
2575
- function resolveAnthropicAdaptiveEffort(model, options) {
2576
- if (options.effort) return usesAdaptiveThinkingTagOnly(model) ? "adaptive" : options.effort;
2577
- const requestedEffort = options.reasoning;
2578
- if (!requestedEffort) return void 0;
2579
- return mapEffortToAnthropicAdaptiveEffort(model, requestedEffort);
2580
- }
2581
- function extractClaudeCodeFirstUserMessageText(messages) {
2582
- for (const message of messages) {
2583
- if (message.role !== "user") continue;
2584
- const { content } = message;
2585
- if (typeof content === "string") return content;
2586
- if (!Array.isArray(content)) return "";
2587
- for (const block of content) if (block.type === "text") return block.text;
2588
- return "";
2589
- }
2590
- return "";
2591
- }
2592
- const MAX_ANTHROPIC_CONTROL_STATES = 16;
2593
- function resetAnthropicControlState(state) {
2594
- state.declaredTools = void 0;
2595
- state.activeToolNames.clear();
2596
- state.stableSystemBlocks = void 0;
2597
- state.systemFingerprint = void 0;
2598
- state.controlTransitions = [];
2599
- state.baseEffort = void 0;
2600
- state.baseEffortWire = void 0;
2601
- state.currentEffort = void 0;
2602
- }
2603
- function anthropicControlMessageProjection(message) {
2604
- if (message.role !== "assistant" || !Array.isArray(message.content)) return message;
2605
- return {
2606
- ...message,
2607
- content: message.content.filter((block) => block.type !== "thinking" && block.type !== "redacted_thinking")
2608
- };
2609
- }
2610
- function getAnthropicControlState(state, sessionId, system, messages) {
2611
- if (!state) return void 0;
2612
- const root = messages[0];
2613
- const fingerprint = String(Bun.hash(JSON.stringify([
2614
- sessionId ?? "",
2615
- system?.map((block) => block.text) ?? null,
2616
- root ? anthropicControlMessageProjection(root) : null
2617
- ])));
2618
- const existing = state.controlStates.get(fingerprint);
2619
- if (existing) {
2620
- state.controlStates.delete(fingerprint);
2621
- state.controlStates.set(fingerprint, existing);
2622
- return existing;
2623
- }
2624
- const created = createAnthropicControlState();
2625
- state.controlStates.set(fingerprint, created);
2626
- if (state.controlStates.size > MAX_ANTHROPIC_CONTROL_STATES) {
2627
- const oldest = state.controlStates.keys().next().value;
2628
- if (oldest !== void 0) state.controlStates.delete(oldest);
2629
- }
2630
- return created;
2631
- }
2632
- /** Fingerprint of the wire message a control transition is attached after. */
2633
- function anthropicControlAnchor(messages, messageCount) {
2634
- if (messageCount === 0) return "";
2635
- const message = messages[messageCount - 1];
2636
- return message ? String(Bun.hash(JSON.stringify(anthropicControlMessageProjection(message)))) : "";
2637
- }
2638
- /**
2639
- * Discard the control baseline when the request no longer continues the
2640
- * conversation it was captured for: a different model, or a wire history that
2641
- * shrank or was rewritten under a recorded transition (compaction, branch
2642
- * switch, `/clear`). The next request re-baselines from its own payload.
2643
- */
2644
- function syncAnthropicControlState(state, messages) {
2645
- for (const transition of state.controlTransitions) if (transition.messageCount > messages.length || transition.anchor !== anthropicControlAnchor(messages, transition.messageCount)) {
2646
- resetAnthropicControlState(state);
2647
- return;
2648
- }
2649
- }
2650
- /**
2651
- * Keep the top-level `system` array byte-stable across a session. The blocks
2652
- * captured on the first request are replayed verbatim (with the current
2653
- * request's cache breakpoints) while their text is unchanged. A text change
2654
- * re-baselines instead of duplicating the prompt as a mid-conversation
2655
- * system message: omp's system prompt is one rendered segment that embeds
2656
- * the tool roster, so replaying a second copy on every later request would
2657
- * cost the full prompt again per change. The prefix rewrite is absorbed by
2658
- * `prefix_mismatch_behavior: "drop_block"` and one cache miss.
2659
- */
2660
- function planStableAnthropicSystem(current, state, enabled) {
2661
- if (!state || !enabled) return current;
2662
- const fingerprint = JSON.stringify(current?.map((block) => block.text) ?? null);
2663
- if (state.systemFingerprint !== fingerprint) {
2664
- resetAnthropicControlState(state);
2665
- state.systemFingerprint = fingerprint;
2666
- state.stableSystemBlocks = current?.map((block) => ({
2667
- type: block.type,
2668
- text: block.text
2669
- }));
2670
- }
2671
- return state.stableSystemBlocks?.map((block, index) => {
2672
- const cacheControl = current?.[index]?.cache_control;
2673
- return cacheControl ? {
2674
- ...block,
2675
- cache_control: cloneAnthropicCacheControl(cacheControl)
2676
- } : { ...block };
2677
- });
2678
- }
2679
- function anthropicToolDefinitionKey(tool) {
2680
- const stable = { ...tool };
2681
- delete stable.defer_loading;
2682
- delete stable.description;
2683
- return JSON.stringify(stable);
2684
- }
2685
- function cloneAnthropicTools(tools) {
2686
- return tools.map((tool) => ({ ...tool }));
2687
- }
2688
- function recordAnthropicControlTransition(state, messages, messageCount, content, effort) {
2689
- const existing = state.controlTransitions.findLast((transition) => transition.messageCount === messageCount);
2690
- if (existing) {
2691
- existing.content.push(...content);
2692
- if (effort !== void 0) existing.effort = effort;
2693
- return;
2694
- }
2695
- state.controlTransitions.push({
2696
- messageCount,
2697
- anchor: anthropicControlAnchor(messages, messageCount),
2698
- content,
2699
- effort
2700
- });
2701
- }
2702
- /**
2703
- * Keep the top-level `tools` array byte-stable across a session. Tools that
2704
- * leave the active set are withdrawn with `tool_removal`; tools that join are
2705
- * appended with `defer_loading: true` (not part of the checked prefix until
2706
- * referenced) and offered with `tool_addition`. A changed definition for an
2707
- * already-declared name cannot be expressed as a control and re-baselines.
2708
- */
2709
- function planStableAnthropicTools(current, messages, state, enabled) {
2710
- if (!state || !enabled || !current) return current;
2711
- if (!state.declaredTools) {
2712
- state.declaredTools = cloneAnthropicTools(current);
2713
- state.activeToolNames = new Set(current.map((tool) => tool.name));
2714
- return cloneAnthropicTools(state.declaredTools);
2715
- }
2716
- const declaredByName = new Map(state.declaredTools.map((tool) => [tool.name, tool]));
2717
- for (const tool of current) {
2718
- const declared = declaredByName.get(tool.name);
2719
- if (declared && anthropicToolDefinitionKey(declared) !== anthropicToolDefinitionKey(tool)) {
2720
- resetAnthropicControlState(state);
2721
- state.declaredTools = cloneAnthropicTools(current);
2722
- state.activeToolNames = new Set(current.map((candidate) => candidate.name));
2723
- return cloneAnthropicTools(state.declaredTools);
2724
- }
2725
- }
2726
- const nextActive = new Set(current.map((tool) => tool.name));
2727
- const changes = [];
2728
- for (const activeName of state.activeToolNames) {
2729
- if (nextActive.has(activeName)) continue;
2730
- changes.push({
2731
- type: "tool_removal",
2732
- tool: {
2733
- type: "tool_reference",
2734
- name: activeName
2735
- }
2736
- });
2737
- }
2738
- for (const tool of current) {
2739
- if (state.activeToolNames.has(tool.name)) continue;
2740
- if (!declaredByName.has(tool.name)) {
2741
- const deferred = {
2742
- ...tool,
2743
- defer_loading: true
2744
- };
2745
- state.declaredTools.push(deferred);
2746
- declaredByName.set(tool.name, deferred);
2747
- }
2748
- changes.push({
2749
- type: "tool_addition",
2750
- tool: {
2751
- type: "tool_reference",
2752
- name: tool.name
2753
- }
2754
- });
2755
- }
2756
- if (changes.length > 0) recordAnthropicControlTransition(state, messages, messages.length, changes);
2757
- state.activeToolNames = nextActive;
2758
- return cloneAnthropicTools(state.declaredTools);
2759
- }
2760
- /**
2761
- * Keep top-level `output_config.effort` byte-stable across a session and carry
2762
- * later changes as per-message effort. Anthropic applies a system message's
2763
- * `output_config.effort` from the next `user` turn on, so the control is
2764
- * anchored before the latest user message to take effect on this response.
2765
- */
2766
- function planStableAnthropicEffort(current, messages, state, enabled) {
2767
- if (!state || !enabled) return current;
2768
- const effective = current ?? "high";
2769
- if (state.baseEffort === void 0) {
2770
- state.baseEffort = effective;
2771
- state.baseEffortWire = current;
2772
- state.currentEffort = effective;
2773
- return current;
2774
- }
2775
- if (state.currentEffort !== effective) {
2776
- const lastUserIndex = messages.findLastIndex((message) => message.role === "user");
2777
- recordAnthropicControlTransition(state, messages, lastUserIndex >= 0 ? lastUserIndex : messages.length, [], effective);
2778
- state.currentEffort = effective;
2779
- }
2780
- return state.baseEffortWire;
2781
- }
2782
- function materializeAnthropicControlTransitions(messages, state) {
2783
- if (!state || state.controlTransitions.length === 0) return messages;
2784
- const result = messages.slice();
2785
- const ordered = state.controlTransitions.toSorted((a, b) => a.messageCount - b.messageCount);
2786
- let offset = 0;
2787
- for (const transition of ordered) {
2788
- const index = Math.min(transition.messageCount + offset, result.length);
2789
- const previous = result[index - 1];
2790
- if (previous?.role === "system" && previous.clear_at === void 0) {
2791
- const content = typeof previous.content === "string" ? [{
2792
- type: "text",
2793
- text: previous.content
2794
- }, ...transition.content] : [...previous.content, ...transition.content];
2795
- result[index - 1] = {
2796
- ...previous,
2797
- content,
2798
- ...transition.effort === void 0 ? {} : { output_config: { effort: transition.effort } }
2799
- };
2800
- continue;
2801
- }
2802
- result.splice(index, 0, {
2803
- role: "system",
2804
- content: transition.content.map((block) => ({ ...block })),
2805
- ...transition.effort === void 0 ? {} : { output_config: { effort: transition.effort } }
2806
- });
2807
- offset++;
2808
- }
2809
- return result;
2810
- }
2811
- function buildParams(model, context, isOAuthToken, options, buildOptions) {
2812
- const { disableStrictTools, useUmansGatewayWebSearch, forceDemoteUnsignedThinking, supportsEagerToolInputStreaming, prefixMismatchBehavior, dropAllThinking, droppedThinkingBlocks, providerSessionState, fallbacks = options?.fallbacks } = buildOptions;
2813
- const effectiveModel = forceDemoteUnsignedThinking && model.compat.replayUnsignedThinking ? {
2814
- ...model,
2815
- compat: {
2816
- ...model.compat,
2817
- replayUnsignedThinking: false
2818
- }
2819
- } : model;
2820
- const { cacheControl } = getCacheControl(model, options?.cacheRetention);
2821
- const shouldInjectClaudeCodeInstruction = isOAuthToken && model.compat.injectClaudeCodeInstruction !== false;
2822
- const firstUserMessageText = shouldInjectClaudeCodeInstruction ? extractClaudeCodeFirstUserMessageText(context.messages) : "";
2823
- let systemBlocks = buildAnthropicSystemBlocks(context.systemPrompt, {
2824
- includeClaudeCodeInstruction: shouldInjectClaudeCodeInstruction,
2825
- firstUserMessageText,
2826
- cacheControl
2827
- });
2828
- let tools;
2829
- if (context.tools) tools = convertTools(context.tools, isOAuthToken, disableStrictTools, supportsEagerToolInputStreaming, model.compat.escapeBuiltinToolNames, useUmansGatewayWebSearch);
2830
- else if (isOAuthToken) tools = [];
2831
- const metadataAccountId = readAnthropicMetadataAccountId(options?.metadata);
2832
- const metadataUserId = resolveAnthropicMetadataUserId(readMetadataString(options?.metadata, "user_id") ?? (model.provider === "kimi-code" ? getOpenAIPromptCacheKey(options) : void 0), isOAuthToken, options?.sessionId, metadataAccountId);
2833
- const metadata = metadataUserId ? { user_id: metadataUserId } : void 0;
2834
- let thinking;
2835
- let outputConfigEffort;
2836
- if (model.reasoning) {
2837
- if (options?.thinkingEnabled || model.compat.requiresThinkingEnabled) {
2838
- const thinkingOptions = options ?? {};
2839
- const mode = model.thinking?.mode;
2840
- const effort = resolveAnthropicAdaptiveEffort(model, thinkingOptions);
2841
- const compat = model.compat;
2842
- if (mode === "anthropic-adaptive" && !compat.disableAdaptiveThinking) {
2843
- const adaptive = { type: "adaptive" };
2844
- if (model.thinking?.supportsDisplay) adaptive.display = thinkingOptions.thinkingDisplay ?? "summarized";
2845
- thinking = adaptive;
2846
- if (effort && effort !== "adaptive") outputConfigEffort = effort;
2847
- } else {
2848
- thinking = {
2849
- type: "enabled",
2850
- budget_tokens: thinkingOptions.thinkingBudgetTokens || 1024,
2851
- display: thinkingOptions.thinkingDisplay ?? "summarized"
2852
- };
2853
- if (mode === "anthropic-budget-effort" && effort && effort !== "adaptive") outputConfigEffort = effort;
2854
- }
2855
- } else if (options?.thinkingEnabled === false) {
2856
- if (isAdaptiveOnlyThinking(model)) outputConfigEffort = "low";
2857
- else thinking = { type: "disabled" };
2858
- }
2859
- }
2860
- if (prefixMismatchBehavior) {
2861
- if (!thinking && model.thinking?.mode === "anthropic-adaptive") thinking = { type: "adaptive" };
2862
- if (thinking?.type === "adaptive" || thinking?.type === "enabled") thinking.block_binding = { prefix_mismatch_behavior: prefixMismatchBehavior };
2863
- }
2864
- const contextManagement = !options?.client && model.compat.supportsContextManagement !== false && (thinking?.type === "adaptive" || thinking?.type === "enabled") ? { edits: [{
2865
- type: "clear_thinking_20251015",
2866
- keep: "all"
2867
- }] } : void 0;
2868
- let wireMessages = convertAnthropicMessages(context.messages, effectiveModel, isOAuthToken, {
2869
- serverSideFallbackEnabled: !!fallbacks?.length,
2870
- dropAllThinking,
2871
- droppedThinkingBlocks
2872
- });
2873
- const controlState = getAnthropicControlState(providerSessionState, options?.sessionId, systemBlocks, wireMessages);
2874
- if (controlState) syncAnthropicControlState(controlState, wireMessages);
2875
- systemBlocks = planStableAnthropicSystem(systemBlocks, controlState, model.compat.supportsMidConversationSystem);
2876
- tools = planStableAnthropicTools(tools, wireMessages, controlState, model.compat.supportsMidConversationToolChanges);
2877
- applyHeadCaching(systemBlocks, tools, cacheControl);
2878
- const topLevelEffort = planStableAnthropicEffort(outputConfigEffort, wireMessages, controlState, model.compat.supportsPerMessageEffort);
2879
- wireMessages = materializeAnthropicControlTransitions(wireMessages, controlState);
2880
- const outputConfigEntries = {};
2881
- if (topLevelEffort && model.compat.supportsOutputEffort) outputConfigEntries.effort = topLevelEffort;
2882
- if (options?.taskBudget) outputConfigEntries.task_budget = options.taskBudget;
2883
- const outputConfig = Object.keys(outputConfigEntries).length ? outputConfigEntries : void 0;
2884
- const modelMaxTokens = model.maxTokens ?? 64e3;
2885
- const maxOutputTokens = isOAuthToken ? Math.min(CLAUDE_CODE_MAX_OUTPUT_TOKENS, modelMaxTokens) : modelMaxTokens;
2886
- const vertexControlBetas = isVertexRawPredictUrl(model.baseUrl) ? resolveAnthropicControlBetas(model, prefixMismatchBehavior) : [];
2887
- const params = {
2888
- model: options?.requestModelId ?? model.requestModelId ?? model.id,
2889
- messages: wireMessages,
2890
- ...systemBlocks && { system: systemBlocks },
2891
- ...tools !== void 0 && { tools },
2892
- ...metadata && { metadata },
2893
- max_tokens: Math.min(maxOutputTokens, options?.maxTokens ?? modelMaxTokens),
2894
- ...thinking && { thinking },
2895
- ...contextManagement && { context_management: contextManagement },
2896
- ...outputConfig && { output_config: outputConfig },
2897
- ...fallbacks?.length ? { fallbacks } : {},
2898
- ...vertexControlBetas.length > 0 ? { anthropic_beta: vertexControlBetas } : {},
2899
- stream: true
2900
- };
2901
- const thinkingType = params.thinking?.type;
2902
- const allowSamplingParams = model.compat.supportsSamplingParams && (thinkingType === void 0 || thinkingType === "disabled");
2903
- if (allowSamplingParams && options?.temperature !== void 0) params.temperature = options.temperature;
2904
- if (allowSamplingParams && options?.topP !== void 0) params.top_p = options.topP;
2905
- if (allowSamplingParams && options?.topK !== void 0) params.top_k = options.topK;
2906
- if (options?.stopSequences?.length) {
2907
- const seqs = options.stopSequences;
2908
- if (seqs.length > ANTHROPIC_STOP_SEQUENCES_MAX && !warnedStopSequencesTrim) {
2909
- warnedStopSequencesTrim = true;
2910
- warn("anthropic: stop_sequences exceeds 4; extra entries dropped", {
2911
- received: seqs.length,
2912
- kept: ANTHROPIC_STOP_SEQUENCES_MAX
2913
- });
2914
- }
2915
- params.stop_sequences = seqs.length > ANTHROPIC_STOP_SEQUENCES_MAX ? seqs.slice(0, ANTHROPIC_STOP_SEQUENCES_MAX) : seqs;
2916
- }
2917
- if (model.provider === "anthropic" && options?.serviceTier === "priority") params.speed = "fast";
2918
- if (options?.toolChoice) {
2919
- if (typeof options.toolChoice === "string") params.tool_choice = { type: options.toolChoice };
2920
- else if (options.toolChoice.name) params.tool_choice = {
2921
- ...options.toolChoice,
2922
- name: encodeAnthropicToolName(options.toolChoice.name, isOAuthToken, model.compat.escapeBuiltinToolNames, useUmansGatewayWebSearch)
2923
- };
2924
- const choiceType = params.tool_choice?.type;
2925
- if ((choiceType === "any" || choiceType === "tool") && !model.compat.supportsForcedToolChoice) params.tool_choice = { type: "auto" };
2926
- }
2927
- disableThinkingIfToolChoiceForced(params, model);
2928
- ensureMaxTokensForThinking(params, maxOutputTokens);
2929
- applyPromptCaching(params, cacheControl);
2930
- return params;
2931
- }
2932
- const EMPTY_ERROR_TOOL_RESULT_TEXT = "Tool failed with no output.";
2933
- function isEmptyToolResultWireContent(content) {
2934
- if (typeof content === "string") return content.trim().length === 0;
2935
- return content.length === 0;
2936
- }
2937
- function ensureErrorToolResultWireContent(content, isError) {
2938
- if (!isError || !isEmptyToolResultWireContent(content)) return content;
2939
- return typeof content === "string" ? EMPTY_ERROR_TOOL_RESULT_TEXT : [{
2940
- type: "text",
2941
- text: EMPTY_ERROR_TOOL_RESULT_TEXT
2942
- }];
2943
- }
2944
- function buildToolResultBlock(model, msg, hoistedImages) {
2945
- let content = convertContentBlocks(msg.content, model.input.includes("image"));
2946
- if (msg.isError && typeof content !== "string" && content.some((block) => block.type === "image")) {
2947
- for (const block of content) if (block.type === "image") hoistedImages.push(block);
2948
- content = content.filter((block) => block.type === "text");
2949
- }
2950
- if (Array.isArray(content) && content.length === 0) content = "";
2951
- content = ensureErrorToolResultWireContent(content, msg.isError);
2952
- const block = {
2953
- type: "tool_result",
2954
- tool_use_id: msg.toolCallId,
2955
- content,
2956
- is_error: msg.isError
2957
- };
2958
- if (model.compat.requiresToolResultId) block.id = msg.toolCallId;
2959
- return block;
2960
- }
2961
- /**
2962
- * Recursively replace lone surrogates in string leaves. Identity-preserving:
2963
- * returns the input object/array when nothing changed.
2964
- */
2965
- function toWellFormedDeep(value) {
2966
- if (typeof value === "string") {
2967
- const wellFormed = value.toWellFormed();
2968
- return wellFormed === value ? value : wellFormed;
2969
- }
2970
- if (Array.isArray(value)) {
2971
- let changed = false;
2972
- const next = value.map((entry) => {
2973
- const sanitized = toWellFormedDeep(entry);
2974
- if (sanitized !== entry) changed = true;
2975
- return sanitized;
2976
- });
2977
- return changed ? next : value;
2978
- }
2979
- if (isRecord(value)) {
2980
- let changed = false;
2981
- const next = {};
2982
- for (const [key, entry] of Object.entries(value)) {
2983
- const sanitized = toWellFormedDeep(entry);
2984
- if (sanitized !== entry) changed = true;
2985
- next[key] = sanitized;
2986
- }
2987
- return changed ? next : value;
2988
- }
2989
- return value;
2990
- }
2991
- /**
2992
- * Serialize omp {@link Message}s to Anthropic wire messages.
2993
- *
2994
- * `opts.serverSideFallbackEnabled` — when the CURRENT request itself
2995
- * opts into the server-side-fallback beta chain. Only then may a persisted
2996
- * `fallback` content block from a prior turn be replayed on the wire;
2997
- * otherwise the block is dropped to avoid a 400 on non-fallback requests
2998
- * that don't send the beta.
2999
- */
3000
- function convertAnthropicMessages(messages, model, isOAuthToken, opts) {
3001
- const developerParams = [];
3002
- const params = [];
3003
- const transformedMessages = transformMessages(messages, model, normalizeToolCallId);
3004
- for (let i = 0; i < transformedMessages.length; i++) {
3005
- const msg = transformedMessages[i];
3006
- if (msg.role === "user" || msg.role === "developer") {
3007
- const payload = msg.role === "developer" && msg.providerPayload?.type === "anthropicMessage" ? msg.providerPayload : void 0;
3008
- const hasProviderControls = payload?.clearAt !== void 0 || payload?.effort !== void 0 || payload?.toolChanges !== void 0 && payload.toolChanges.length > 0;
3009
- let content;
3010
- if (typeof msg.content === "string") {
3011
- if (msg.content.trim().length === 0) {
3012
- if (!hasProviderControls) continue;
3013
- content = [];
3014
- } else content = msg.content.toWellFormed();
3015
- } else {
3016
- const contentBlocks = convertContentBlocks(msg.content, model.input.includes("image"));
3017
- if (typeof contentBlocks === "string") {
3018
- if (contentBlocks.trim().length === 0) {
3019
- if (!hasProviderControls) continue;
3020
- content = [];
3021
- } else content = contentBlocks;
3022
- } else {
3023
- if (contentBlocks.length === 0 && !hasProviderControls) continue;
3024
- content = contentBlocks;
3025
- }
3026
- }
3027
- if (payload?.toolChanges && model.compat.supportsMidConversationToolChanges) {
3028
- const blocks = typeof content === "string" ? [{
3029
- type: "text",
3030
- text: content
3031
- }] : content;
3032
- for (const change of payload.toolChanges) blocks.push({
3033
- type: change.type,
3034
- tool: {
3035
- type: "tool_reference",
3036
- name: encodeAnthropicToolName(change.name, isOAuthToken, model.compat.escapeBuiltinToolNames)
3037
- }
3038
- });
3039
- content = blocks;
3040
- }
3041
- if (msg.role === "developer") developerParams.push({
3042
- index: params.length,
3043
- payload
3044
- });
3045
- params.push({
3046
- role: "user",
3047
- content
3048
- });
3049
- } else if (msg.role === "assistant") {
3050
- const blocks = [];
3051
- const hasSignedThinking = msg.content.some((block) => block.type === "thinking" && !!block.thinkingSignature && block.thinkingSignature.trim().length > 0);
3052
- for (const block of msg.content) if (block.type === "text") {
3053
- if (block.text.trim().length === 0) continue;
3054
- blocks.push({
3055
- type: "text",
3056
- text: block.text.toWellFormed()
3057
- });
3058
- } else if (block.type === "thinking") {
3059
- if (opts?.dropAllThinking || block.thinkingSignature && opts?.droppedThinkingBlocks?.has(`thinking:${block.thinkingSignature}`)) continue;
3060
- if (hasSignedThinking) {
3061
- if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
3062
- if (block.thinking.trim().length === 0) continue;
3063
- blocks.push({
3064
- type: "text",
3065
- text: renderDemotedThinking(model.id, block.thinking)
3066
- });
3067
- continue;
3068
- }
3069
- blocks.push({
3070
- type: "thinking",
3071
- thinking: block.thinking,
3072
- signature: block.thinkingSignature
3073
- });
3074
- continue;
3075
- }
3076
- if (block.thinking.trim().length === 0) continue;
3077
- if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
3078
- if (model.compat.replayUnsignedThinking) blocks.push({
3079
- type: "thinking",
3080
- thinking: block.thinking.toWellFormed(),
3081
- signature: ""
3082
- });
3083
- else blocks.push({
3084
- type: "text",
3085
- text: renderDemotedThinking(model.id, block.thinking)
3086
- });
3087
- } else blocks.push({
3088
- type: "thinking",
3089
- thinking: block.thinking.toWellFormed(),
3090
- signature: block.thinkingSignature
3091
- });
3092
- } else if (block.type === "redactedThinking") {
3093
- if (opts?.dropAllThinking || opts?.droppedThinkingBlocks?.has(`redacted:${block.data}`)) continue;
3094
- if (block.data.trim().length === 0) continue;
3095
- blocks.push({
3096
- type: "redacted_thinking",
3097
- data: block.data
3098
- });
3099
- } else if (block.type === "anthropicServerTool") blocks.push(block.block);
3100
- else if (block.type === "fallback") {
3101
- if (!opts?.serverSideFallbackEnabled || !model.compat.officialEndpoint) continue;
3102
- blocks.push({
3103
- type: "fallback",
3104
- from: block.from,
3105
- to: block.to
3106
- });
3107
- } else if (block.type === "toolCall") blocks.push({
3108
- type: "tool_use",
3109
- id: block.id,
3110
- name: encodeAnthropicToolName(block.name, isOAuthToken, model.compat.escapeBuiltinToolNames),
3111
- input: toWellFormedDeep(block.arguments ?? {})
3112
- });
3113
- let sawToolUse = false;
3114
- let needsPartition = false;
3115
- for (const block of blocks) if (block.type === "tool_use") sawToolUse = true;
3116
- else if (sawToolUse && block.type !== "thinking" && block.type !== "redacted_thinking") {
3117
- needsPartition = true;
3118
- break;
3119
- }
3120
- if (needsPartition) {
3121
- const nonToolUse = [];
3122
- const toolUse = [];
3123
- for (const block of blocks) if (block.type === "tool_use") toolUse.push(block);
3124
- else nonToolUse.push(block);
3125
- blocks.length = 0;
3126
- blocks.push(...nonToolUse, ...toolUse);
3127
- }
3128
- if (blocks.length === 0) continue;
3129
- params.push({
3130
- role: "assistant",
3131
- content: blocks
3132
- });
3133
- } else if (msg.role === "toolResult") {
3134
- const toolResults = [];
3135
- const hoistedImages = [];
3136
- toolResults.push(buildToolResultBlock(model, msg, hoistedImages));
3137
- let j = i + 1;
3138
- while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
3139
- const nextMsg = transformedMessages[j];
3140
- toolResults.push(buildToolResultBlock(model, nextMsg, hoistedImages));
3141
- j++;
3142
- }
3143
- i = j - 1;
3144
- if (hoistedImages.length > 0) toolResults.push({
3145
- type: "text",
3146
- text: "Attached image(s) from the tool result(s) above:"
3147
- }, ...hoistedImages);
3148
- params.push({
3149
- role: "user",
3150
- content: toolResults
3151
- });
3152
- }
3153
- }
3154
- if (developerParams.length > 0 && model.compat.supportsMidConversationSystem) for (const developer of developerParams.toReversed()) {
3155
- const idx = developer.index;
3156
- const followsUser = idx > 0 && params[idx - 1]?.role === "user";
3157
- const next = params[idx + 1];
3158
- const lastOrBeforeAssistant = idx === params.length - 1 || next?.role === "assistant";
3159
- const content = params[idx].content;
3160
- const systemCompatible = typeof content === "string" || content.every((block) => block.type === "text" || block.type === "tool_addition" || block.type === "tool_removal");
3161
- const effortOnly = developer.payload?.effort !== void 0 && Array.isArray(content) && content.length === 0;
3162
- if (!(followsUser && lastOrBeforeAssistant && systemCompatible || effortOnly)) continue;
3163
- const turnScoped = developer.payload?.clearAt === "next_user_message" && model.compat.supportsTurnScopedSystem;
3164
- const hasEffort = developer.payload?.effort !== void 0 && model.compat.supportsPerMessageEffort;
3165
- const hasToolChanges = (developer.payload?.toolChanges?.length ?? 0) > 0;
3166
- if (turnScoped && (hasEffort || hasToolChanges) && Array.isArray(content)) {
3167
- const scopedContent = content.filter((block) => block.type === "text");
3168
- const controlContent = content.filter((block) => block.type !== "text");
3169
- if (scopedContent.length > 0) {
3170
- params[idx] = {
3171
- role: "system",
3172
- content: scopedContent,
3173
- clear_at: "next_user_message"
3174
- };
3175
- params.splice(idx + 1, 0, {
3176
- role: "system",
3177
- content: controlContent,
3178
- ...hasEffort ? { output_config: { effort: developer.payload?.effort } } : {}
3179
- });
3180
- continue;
3181
- }
3182
- }
3183
- params[idx] = {
3184
- role: "system",
3185
- content,
3186
- ...turnScoped && !hasEffort && !hasToolChanges ? { clear_at: "next_user_message" } : {},
3187
- ...hasEffort ? { output_config: { effort: developer.payload?.effort } } : {}
3188
- };
3189
- }
3190
- for (let i = params.length - 1; i > 0; i--) if (params[i].role === "assistant" && params[i - 1]?.role === "assistant") params.splice(i, 0, {
3191
- role: "user",
3192
- content: "Continue."
3193
- });
3194
- if (params.length > 0 && params[params.length - 1]?.role === "assistant") params.push({
3195
- role: "user",
3196
- content: "Continue."
3197
- });
3198
- return params;
3199
- }
3200
- /**
3201
- * JSON Schema whitelist for Anthropic tool `input_schema` nodes.
3202
- *
3203
- * Tracks the Anthropic Python SDK's `lib/_parse/_transform.py::transform_schema`,
3204
- * with live Messages API guardrails for keywords the SDK preserves but the API rejects.
3205
- * We keep only structural/metadata keywords Anthropic's validator honors, and demote
3206
- * anything else into the node's `description` as `\n\n{key: value, ...}` so the model
3207
- * still sees the constraint as a natural-language hint.
3208
- *
3209
- * `Set` (not `Record<string, true>`) because membership is probed against arbitrary
3210
- * user/Zod-derived schema keys: a literal Record would falsely match prototype names
3211
- * like `"toString"` and silently strip valid properties.
3212
- */
3213
- const ANTHROPIC_TOOL_SCHEMA_UNIVERSAL_KEEP = /* @__PURE__ */ new Set([
3214
- "$ref",
3215
- "$defs",
3216
- "$schema",
3217
- "definitions",
3218
- "type",
3219
- "anyOf",
3220
- "allOf",
3221
- "enum",
3222
- "const",
3223
- "description",
3224
- "title",
3225
- "default",
3226
- "nullable"
3227
- ]);
3228
- /** Keys preserved on `type: "object"` nodes (in addition to the universal set). */
3229
- const ANTHROPIC_TOOL_SCHEMA_OBJECT_KEEP = /* @__PURE__ */ new Set([
3230
- "properties",
3231
- "required",
3232
- "additionalProperties"
3233
- ]);
3234
- /** Keys preserved on `type: "array"` nodes; `minItems` only when its value is 0 or 1. */
3235
- const ANTHROPIC_TOOL_SCHEMA_ARRAY_KEEP = /* @__PURE__ */ new Set([
3236
- "items",
3237
- "prefixItems",
3238
- "minItems"
3239
- ]);
3240
- /** Keys preserved on `type: "string"` nodes; `format` only when its value is in the supported list. */
3241
- const ANTHROPIC_TOOL_SCHEMA_STRING_KEEP = /* @__PURE__ */ new Set(["format"]);
3242
- /**
3243
- * String `format` values Anthropic accepts; everything else (including `pattern`-style
3244
- * format hints) gets demoted into `description`. Matches `SupportedStringFormats` in the
3245
- * Anthropic SDK's `_transform.py`.
3246
- */
3247
- const ANTHROPIC_TOOL_SCHEMA_STRING_FORMATS = /* @__PURE__ */ new Set([
3248
- "date-time",
3249
- "time",
3250
- "date",
3251
- "duration",
3252
- "email",
3253
- "hostname",
3254
- "uri",
3255
- "ipv4",
3256
- "ipv6",
3257
- "uuid"
3258
- ]);
3259
- const ANTHROPIC_STRICT_TOOL_ALLOWLIST = /* @__PURE__ */ new Set([
3260
- "bash",
3261
- "python",
3262
- "edit",
3263
- "find"
3264
- ]);
3265
- const MAX_ANTHROPIC_STRICT_TOOLS = 20;
3266
- const MAX_ANTHROPIC_STRICT_OPTIONAL_PARAMETERS = 24;
3267
- const MAX_ANTHROPIC_STRICT_UNION_PARAMETERS = 16;
3268
- /** `minItems` / `maxItems` apply to arrays; Anthropic rejects them on `type: "object"` (including `minItems: 0`/`1`). */
3269
- function isJsonSchemaArrayNode(schema) {
3270
- const t = schema.type;
3271
- if (t === "array") return true;
3272
- if (Array.isArray(t) && t.includes("array") && !t.includes("object")) return true;
3273
- if (schema.items !== void 0 || Array.isArray(schema.prefixItems)) return true;
3274
- return false;
3275
- }
3276
- function isJsonSchemaObjectNode(schema) {
3277
- if (isJsonSchemaArrayNode(schema)) return false;
3278
- if (schema.type === "object") return true;
3279
- if (Array.isArray(schema.type) && schema.type.includes("object")) return true;
3280
- if (isRecord(schema.properties)) return true;
3281
- return false;
3282
- }
3283
- /**
3284
- * Pick the principal non-null scalar type from a `type` keyword. Anthropic accepts
3285
- * `type` as either a single string or an array (e.g. `["number", "null"]` for a
3286
- * nullable value); the SDK whitelist is keyed off the scalar type, with `"null"`
3287
- * ignored so nullable variants are normalized as their underlying type.
3288
- */
3289
- function pickAnthropicScalarType(type) {
3290
- if (typeof type === "string") return type;
3291
- if (Array.isArray(type)) {
3292
- for (const entry of type) if (typeof entry === "string" && entry !== "null") return entry;
3293
- }
3294
- }
3295
- function pickAnthropicEffectiveScalarType(schema) {
3296
- const explicit = pickAnthropicScalarType(schema.type);
3297
- if (explicit) return explicit;
3298
- if (isRecord(schema.properties)) return "object";
3299
- if (schema.items !== void 0 || Array.isArray(schema.prefixItems)) return "array";
3300
- }
3301
- function anthropicPerTypeKeep(scalarType) {
3302
- switch (scalarType) {
3303
- case "object": return ANTHROPIC_TOOL_SCHEMA_OBJECT_KEEP;
3304
- case "array": return ANTHROPIC_TOOL_SCHEMA_ARRAY_KEEP;
3305
- case "string": return ANTHROPIC_TOOL_SCHEMA_STRING_KEEP;
3306
- default: return;
3307
- }
3308
- }
3309
- /**
3310
- * Normalize a JSON Schema node for Anthropic tool `input_schema`.
3311
- *
3312
- * Applies the full whitelist semantics from the Anthropic Python SDK's
3313
- * `lib/_parse/_transform.py::transform_schema`:
3314
- *
3315
- * 1. Universal keys (`$ref`, `$defs`, `type`, `anyOf`, `allOf`, `enum`, `const`,
3316
- * `description`, `title`, `default`, `nullable`) are preserved on every node, with
3317
- * one position-dependent exception: the combinator keys. Root `anyOf`/`allOf` are
3318
- * spilled (recent Anthropic Messages validators reject combinators at the tool
3319
- * `input_schema` root) but kept when nested; `oneOf` is spilled at every position
3320
- * (it is not in the documented supported subset).
3321
- * 2. Per-type keys are kept additively (object → `properties`/`required`/`additionalProperties`,
3322
- * array → `items`/`prefixItems` plus `minItems` only when 0 or 1, string → `format`
3323
- * only when in the supported value set).
3324
- * 3. Everything else is demoted into the node's `description` as `\n\n{key: value, ...}`
3325
- *
3326
- * Object nodes default to `additionalProperties: false`, but explicit open-map
3327
- * declarations (`additionalProperties: true` or a schema literal — Zod's
3328
- * `z.record(z.string(), z.unknown())` produces `{}`) are preserved. The strict-mode
3329
- * pass downstream demotes those shapes to non-strict instead of fabricating a closed
3330
- * object, so callers like the resolve tool keep working open-map semantics.
3331
- */
3332
- function normalizeAnthropicToolSchemaNode(schema, cache, isRoot = false) {
3333
- if (Array.isArray(schema)) return schema.map((entry) => normalizeAnthropicToolSchemaNode(entry, cache));
3334
- if (!isRecord(schema)) return schema;
3335
- const existing = cache.get(schema);
3336
- if (existing !== void 0) return existing;
3337
- const result = {};
3338
- cache.set(schema, result);
3339
- const scalarType = pickAnthropicEffectiveScalarType(schema);
3340
- const perTypeKeep = anthropicPerTypeKeep(scalarType);
3341
- const spill = [];
3342
- for (const key in schema) {
3343
- if (!Object.hasOwn(schema, key)) continue;
3344
- const value = schema[key];
3345
- if (!(isRoot && COMBINATOR_KEYS.includes(key)) && (ANTHROPIC_TOOL_SCHEMA_UNIVERSAL_KEEP.has(key) || perTypeKeep?.has(key))) result[key] = value;
3346
- else spill.push([key, value]);
3347
- }
3348
- if (scalarType === "string") {
3349
- const format = result.format;
3350
- if (typeof format === "string" && !ANTHROPIC_TOOL_SCHEMA_STRING_FORMATS.has(format)) {
3351
- spill.push(["format", format]);
3352
- delete result.format;
3353
- }
3354
- }
3355
- if (scalarType === "array" && result.minItems !== void 0) {
3356
- const minItems = result.minItems;
3357
- if (!(typeof minItems === "number" && (minItems === 0 || minItems === 1))) {
3358
- spill.push(["minItems", minItems]);
3359
- delete result.minItems;
3360
- }
3361
- }
3362
- if (scalarType === "object" && result.additionalProperties === void 0) result.additionalProperties = false;
3363
- if (isRecord(result.properties)) {
3364
- const normalizedProperties = {};
3365
- const sourceProperties = result.properties;
3366
- for (const propName in sourceProperties) {
3367
- if (!Object.hasOwn(sourceProperties, propName)) continue;
3368
- normalizedProperties[propName] = normalizeAnthropicToolSchemaNode(sourceProperties[propName], cache);
3369
- }
3370
- result.properties = normalizedProperties;
3371
- }
3372
- if (isRecord(result.additionalProperties)) {
3373
- const normalized = normalizeAnthropicToolSchemaNode(result.additionalProperties, cache);
3374
- if (isRecord(normalized) && Object.keys(normalized).length === 0) result.additionalProperties = true;
3375
- else result.additionalProperties = normalized;
3376
- }
3377
- if (Array.isArray(result.items)) result.items = result.items.map((item) => normalizeAnthropicToolSchemaNode(item, cache));
3378
- else if (isRecord(result.items)) result.items = normalizeAnthropicToolSchemaNode(result.items, cache);
3379
- if (Array.isArray(result.prefixItems)) result.prefixItems = result.prefixItems.map((item) => normalizeAnthropicToolSchemaNode(item, cache));
3380
- for (const key of COMBINATOR_KEYS) {
3381
- const variants = result[key];
3382
- if (Array.isArray(variants)) result[key] = variants.map((variant) => normalizeAnthropicToolSchemaNode(variant, cache));
3383
- }
3384
- for (const defsKey of ["$defs", "definitions"]) {
3385
- const definitions = result[defsKey];
3386
- if (!isRecord(definitions)) continue;
3387
- const normalizedDefs = {};
3388
- const sourceDefs = definitions;
3389
- for (const name in sourceDefs) {
3390
- if (!Object.hasOwn(sourceDefs, name)) continue;
3391
- normalizedDefs[name] = normalizeAnthropicToolSchemaNode(sourceDefs[name], cache);
3392
- }
3393
- result[defsKey] = normalizedDefs;
3394
- }
3395
- spillToDescription(result, spill);
3396
- return result;
3397
- }
3398
- function normalizeAnthropicToolSchema(schema) {
3399
- return normalizeAnthropicToolSchemaNode(schema, /* @__PURE__ */ new WeakMap(), true);
3400
- }
3401
- function hasAnthropicUnionType(schema) {
3402
- return Array.isArray(schema.type) || Array.isArray(schema.anyOf);
3403
- }
3404
- function hasNullVariant(schema) {
3405
- if (Array.isArray(schema.type) && schema.type.includes("null")) return true;
3406
- return Array.isArray(schema.anyOf) && schema.anyOf.some((variant) => isRecord(variant) && variant.type === "null");
3407
- }
3408
- function hasAnthropicSchemaDefiningKeyword(schema) {
3409
- if (schema.type !== void 0 || schema.properties !== void 0 || schema.additionalProperties !== void 0 || schema.items !== void 0 || schema.prefixItems !== void 0 || schema.enum !== void 0 || schema.const !== void 0 || schema.$ref !== void 0) return true;
3410
- for (const key of COMBINATOR_KEYS) if (schema[key] !== void 0) return true;
3411
- return schema.$defs !== void 0 || schema.definitions !== void 0;
3412
- }
3413
- function makeAnthropicNullableSchema(schema, budget) {
3414
- if (isRecord(schema)) {
3415
- if (hasNullVariant(schema)) return schema;
3416
- if (Array.isArray(schema.anyOf)) return {
3417
- ...schema,
3418
- anyOf: [...schema.anyOf, { type: "null" }]
3419
- };
3420
- if (Array.isArray(schema.type)) return {
3421
- ...schema,
3422
- type: [...schema.type, "null"]
3423
- };
3424
- }
3425
- if (budget.unionRemaining <= 0) return void 0;
3426
- budget.unionRemaining--;
3427
- budget.unionCount++;
3428
- return { anyOf: [schema, { type: "null" }] };
3429
- }
3430
- function normalizeAnthropicStrictSchemaNode(schema, budget, cache) {
3431
- if (Array.isArray(schema)) {
3432
- const result = [];
3433
- for (const entry of schema) {
3434
- const normalized = normalizeAnthropicStrictSchemaNode(entry, budget, cache);
3435
- if (normalized === void 0) return void 0;
3436
- result.push(normalized);
3437
- }
3438
- return result;
3439
- }
3440
- if (!isRecord(schema)) return schema;
3441
- const cached = cache.get(schema);
3442
- if (cached) return cached;
3443
- if (!hasAnthropicSchemaDefiningKeyword(schema)) return void 0;
3444
- if (isJsonSchemaObjectNode(schema) && schema.additionalProperties !== false) return;
3445
- const result = { ...schema };
3446
- cache.set(schema, result);
3447
- if (hasAnthropicUnionType(result)) {
3448
- if (budget.unionRemaining <= 0) return void 0;
3449
- budget.unionRemaining--;
3450
- budget.unionCount++;
3451
- }
3452
- if (isRecord(result.properties)) {
3453
- const originalRequired = new Set(Array.isArray(result.required) ? result.required.filter((entry) => typeof entry === "string") : []);
3454
- const properties = {};
3455
- const required = [];
3456
- for (const [propertyName, propertySchema] of Object.entries(result.properties)) {
3457
- const normalizedProperty = normalizeAnthropicStrictSchemaNode(propertySchema, budget, cache);
3458
- if (normalizedProperty === void 0) return void 0;
3459
- if (originalRequired.has(propertyName)) {
3460
- properties[propertyName] = normalizedProperty;
3461
- required.push(propertyName);
3462
- continue;
3463
- }
3464
- if (budget.optionalRemaining > 0) {
3465
- budget.optionalRemaining--;
3466
- budget.optionalCount++;
3467
- properties[propertyName] = normalizedProperty;
3468
- continue;
3469
- }
3470
- const nullableProperty = makeAnthropicNullableSchema(normalizedProperty, budget);
3471
- if (nullableProperty === void 0) return void 0;
3472
- properties[propertyName] = nullableProperty;
3473
- required.push(propertyName);
3474
- }
3475
- result.properties = properties;
3476
- result.required = required;
3477
- }
3478
- if (Array.isArray(result.items)) {
3479
- const items = normalizeAnthropicStrictSchemaNode(result.items, budget, cache);
3480
- if (items === void 0) return void 0;
3481
- result.items = items;
3482
- } else if (isRecord(result.items)) {
3483
- const items = normalizeAnthropicStrictSchemaNode(result.items, budget, cache);
3484
- if (items === void 0) return void 0;
3485
- result.items = items;
3486
- }
3487
- if (Array.isArray(result.prefixItems)) {
3488
- const prefixItems = normalizeAnthropicStrictSchemaNode(result.prefixItems, budget, cache);
3489
- if (prefixItems === void 0) return void 0;
3490
- result.prefixItems = prefixItems;
3491
- }
3492
- for (const key of COMBINATOR_KEYS) {
3493
- const variants = result[key];
3494
- if (!Array.isArray(variants)) continue;
3495
- const normalizedVariants = normalizeAnthropicStrictSchemaNode(variants, budget, cache);
3496
- if (normalizedVariants === void 0) return void 0;
3497
- result[key] = normalizedVariants;
3498
- }
3499
- for (const defsKey of ["$defs", "definitions"]) {
3500
- const definitions = result[defsKey];
3501
- if (!isRecord(definitions)) continue;
3502
- const normalizedDefinitions = {};
3503
- for (const [definitionName, definitionSchema] of Object.entries(definitions)) {
3504
- const normalizedDefinition = normalizeAnthropicStrictSchemaNode(definitionSchema, budget, cache);
3505
- if (normalizedDefinition === void 0) return void 0;
3506
- normalizedDefinitions[definitionName] = normalizedDefinition;
3507
- }
3508
- result[defsKey] = normalizedDefinitions;
3509
- }
3510
- return result;
3511
- }
3512
- const ANTHROPIC_STRICT_INCOMPATIBLE_KEYWORDS = [
3513
- "oneOf",
3514
- "allOf",
3515
- "$ref",
3516
- "patternProperties",
3517
- "propertyNames"
3518
- ];
3519
- /**
3520
- * Anthropic's strict grammar subset supports anyOf/type-array unions only.
3521
- * oneOf/allOf/$ref compile unpredictably (rejections arrive as 400s the
3522
- * grammar-too-large fallback does not recognize, so they would hard-fail the
3523
- * turn), and patternProperties/propertyNames describe open key sets that the
3524
- * strict pipeline's injected `additionalProperties: false` would contradict.
3525
- * Runs against the raw wire schema — the base normalizer spills several of
3526
- * these keywords into the description, erasing the evidence.
3527
- */
3528
- function hasAnthropicStrictIncompatibleKeyword(schema, seen = /* @__PURE__ */ new Set()) {
3529
- if (Array.isArray(schema)) {
3530
- if (seen.has(schema)) return false;
3531
- seen.add(schema);
3532
- return schema.some((entry) => hasAnthropicStrictIncompatibleKeyword(entry, seen));
3533
- }
3534
- if (!isRecord(schema)) return false;
3535
- if (seen.has(schema)) return false;
3536
- seen.add(schema);
3537
- for (const keyword of ANTHROPIC_STRICT_INCOMPATIBLE_KEYWORDS) if (schema[keyword] !== void 0) return true;
3538
- return Object.values(schema).some((value) => hasAnthropicStrictIncompatibleKeyword(value, seen));
3539
- }
3540
- function normalizeAnthropicStrictSchema(schema, optionalRemaining, unionRemaining) {
3541
- const budget = {
3542
- optionalRemaining,
3543
- unionRemaining,
3544
- optionalCount: 0,
3545
- unionCount: 0
3546
- };
3547
- const normalized = normalizeAnthropicStrictSchemaNode(schema, budget, /* @__PURE__ */ new WeakMap());
3548
- if (!isRecord(normalized)) return void 0;
3549
- return {
3550
- schema: normalized,
3551
- optionalCount: budget.optionalCount,
3552
- unionCount: budget.unionCount
3553
- };
3554
- }
3555
- function buildAnthropicBaseToolInputSchema(tool) {
3556
- const jsonSchema = toolWireSchema(tool);
3557
- return normalizeAnthropicToolSchema({
3558
- ...jsonSchema,
3559
- type: "object",
3560
- properties: isRecord(jsonSchema.properties) ? jsonSchema.properties : {},
3561
- required: Array.isArray(jsonSchema.required) ? jsonSchema.required.filter((entry) => typeof entry === "string") : []
3562
- });
3563
- }
3564
- function buildAnthropicToolSchemaPlans(tools, disableStrictTools = false) {
3565
- const plans = tools.map((tool) => ({
3566
- inputSchema: buildAnthropicBaseToolInputSchema(tool),
3567
- strict: false
3568
- }));
3569
- if (NO_STRICT || disableStrictTools) return plans;
3570
- const candidateIndexes = tools.flatMap((tool, index) => {
3571
- if (!ANTHROPIC_STRICT_TOOL_ALLOWLIST.has(tool.name)) return [];
3572
- if (tool.strict === false) return [];
3573
- if (hasAnthropicStrictIncompatibleKeyword(toolWireSchema(tool))) return [];
3574
- return [index];
3575
- });
3576
- let strictToolCount = 0;
3577
- let strictOptionalParameterCount = 0;
3578
- let strictUnionParameterCount = 0;
3579
- for (const index of candidateIndexes) {
3580
- if (strictToolCount >= MAX_ANTHROPIC_STRICT_TOOLS) break;
3581
- const strictResult = normalizeAnthropicStrictSchema(plans[index].inputSchema, MAX_ANTHROPIC_STRICT_OPTIONAL_PARAMETERS - strictOptionalParameterCount, MAX_ANTHROPIC_STRICT_UNION_PARAMETERS - strictUnionParameterCount);
3582
- if (!strictResult) continue;
3583
- plans[index] = {
3584
- inputSchema: strictResult.schema,
3585
- strict: true
3586
- };
3587
- strictToolCount++;
3588
- strictOptionalParameterCount += strictResult.optionalCount;
3589
- strictUnionParameterCount += strictResult.unionCount;
3590
- }
3591
- return plans;
3592
- }
3593
- function convertTools(tools, isOAuthToken, disableStrictTools = false, supportsEagerToolInputStreaming = true, escapeBuiltinToolNames = false, useUmansGatewayWebSearch = false) {
3594
- if (!tools) return [];
3595
- const schemaPlans = buildAnthropicToolSchemaPlans(tools, disableStrictTools);
3596
- return tools.map((tool, index) => {
3597
- const plan = schemaPlans[index];
3598
- return {
3599
- name: encodeAnthropicToolName(tool.name, isOAuthToken, escapeBuiltinToolNames, useUmansGatewayWebSearch),
3600
- description: tool.description || "",
3601
- input_schema: plan.inputSchema,
3602
- ...supportsEagerToolInputStreaming ? { eager_input_streaming: true } : {},
3603
- ...plan.strict ? { strict: true } : {},
3604
- ...tool.deferLoading ? { defer_loading: true } : {}
3605
- };
3606
- });
3607
- }
3608
- function mapStopReason(reason) {
3609
- switch (reason) {
3610
- case "end_turn": return "stop";
3611
- case "max_tokens": return "length";
3612
- case "model_context_window_exceeded": return "length";
3613
- case "tool_use": return "toolUse";
3614
- case "refusal": return "error";
3615
- case "pause_turn": return "stop";
3616
- case "stop_sequence": return "stop";
3617
- case "sensitive": return "error";
3618
- default:
3619
- reportAnthropicEnvelopeAnomaly(`unhandled stop reason: ${reason}`);
3620
- return "stop";
3621
- }
3622
- }
3623
- //#endregion
3624
- export { anthropic_exports as t };