jeopi-ai 16.2.13

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 (598) hide show
  1. package/CHANGELOG.md +4347 -0
  2. package/README.md +1193 -0
  3. package/dist/types/api-registry.d.ts +30 -0
  4. package/dist/types/auth-broker/client.d.ts +73 -0
  5. package/dist/types/auth-broker/discover.d.ts +35 -0
  6. package/dist/types/auth-broker/index.d.ts +7 -0
  7. package/dist/types/auth-broker/refresher.d.ts +25 -0
  8. package/dist/types/auth-broker/remote-store.d.ts +102 -0
  9. package/dist/types/auth-broker/server.d.ts +43 -0
  10. package/dist/types/auth-broker/snapshot-cache.d.ts +17 -0
  11. package/dist/types/auth-broker/types.d.ts +107 -0
  12. package/dist/types/auth-broker/wire-schemas.d.ts +411 -0
  13. package/dist/types/auth-gateway/http.d.ts +39 -0
  14. package/dist/types/auth-gateway/index.d.ts +3 -0
  15. package/dist/types/auth-gateway/server.d.ts +36 -0
  16. package/dist/types/auth-gateway/types.d.ts +123 -0
  17. package/dist/types/auth-retry.d.ts +124 -0
  18. package/dist/types/auth-storage.d.ts +1026 -0
  19. package/dist/types/dialect/anthropic.d.ts +15 -0
  20. package/dist/types/dialect/catalog.d.ts +3 -0
  21. package/dist/types/dialect/coercion.d.ts +23 -0
  22. package/dist/types/dialect/deepseek.d.ts +14 -0
  23. package/dist/types/dialect/demotion.d.ts +23 -0
  24. package/dist/types/dialect/examples.d.ts +2 -0
  25. package/dist/types/dialect/factory.d.ts +3 -0
  26. package/dist/types/dialect/fenced-thinking.d.ts +53 -0
  27. package/dist/types/dialect/gemini.d.ts +17 -0
  28. package/dist/types/dialect/gemma.d.ts +15 -0
  29. package/dist/types/dialect/glm.d.ts +9 -0
  30. package/dist/types/dialect/harmony.d.ts +8 -0
  31. package/dist/types/dialect/hermes.d.ts +9 -0
  32. package/dist/types/dialect/history.d.ts +3 -0
  33. package/dist/types/dialect/index.d.ts +11 -0
  34. package/dist/types/dialect/inventory.d.ts +12 -0
  35. package/dist/types/dialect/kimi.d.ts +14 -0
  36. package/dist/types/dialect/minimax.d.ts +3 -0
  37. package/dist/types/dialect/owned-stream.d.ts +4 -0
  38. package/dist/types/dialect/qwen3.d.ts +9 -0
  39. package/dist/types/dialect/rendering.d.ts +45 -0
  40. package/dist/types/dialect/thinking.d.ts +6 -0
  41. package/dist/types/dialect/types.d.ts +69 -0
  42. package/dist/types/dialect/xml.d.ts +9 -0
  43. package/dist/types/error/abort.d.ts +14 -0
  44. package/dist/types/error/auth-classify.d.ts +16 -0
  45. package/dist/types/error/auth.d.ts +27 -0
  46. package/dist/types/error/aws.d.ts +23 -0
  47. package/dist/types/error/classes.d.ts +102 -0
  48. package/dist/types/error/finalize.d.ts +39 -0
  49. package/dist/types/error/flags.d.ts +79 -0
  50. package/dist/types/error/format.d.ts +20 -0
  51. package/dist/types/error/gateway.d.ts +20 -0
  52. package/dist/types/error/index.d.ts +13 -0
  53. package/dist/types/error/oauth.d.ts +43 -0
  54. package/dist/types/error/provider.d.ts +42 -0
  55. package/dist/types/error/rate-limit.d.ts +59 -0
  56. package/dist/types/error/retryable.d.ts +27 -0
  57. package/dist/types/error/validation.d.ts +32 -0
  58. package/dist/types/index.d.ts +49 -0
  59. package/dist/types/provider-details.d.ts +24 -0
  60. package/dist/types/providers/__tests__/google-auth.test.d.ts +1 -0
  61. package/dist/types/providers/__tests__/kimi-code-thinking.test.d.ts +1 -0
  62. package/dist/types/providers/__tests__/openai-codex-error.test.d.ts +1 -0
  63. package/dist/types/providers/amazon-bedrock.d.ts +39 -0
  64. package/dist/types/providers/anthropic-client.d.ts +94 -0
  65. package/dist/types/providers/anthropic-messages-server-schema.d.ts +497 -0
  66. package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
  67. package/dist/types/providers/anthropic-wire.d.ts +318 -0
  68. package/dist/types/providers/anthropic.d.ts +248 -0
  69. package/dist/types/providers/aws-credentials.d.ts +53 -0
  70. package/dist/types/providers/aws-eventstream.d.ts +39 -0
  71. package/dist/types/providers/aws-sigv4.d.ts +55 -0
  72. package/dist/types/providers/azure-openai-responses.d.ts +16 -0
  73. package/dist/types/providers/cursor.d.ts +91 -0
  74. package/dist/types/providers/devin.d.ts +12 -0
  75. package/dist/types/providers/error-message.d.ts +27 -0
  76. package/dist/types/providers/github-copilot-headers.d.ts +40 -0
  77. package/dist/types/providers/gitlab-duo-workflow.d.ts +254 -0
  78. package/dist/types/providers/gitlab-duo.d.ts +27 -0
  79. package/dist/types/providers/google-auth.d.ts +32 -0
  80. package/dist/types/providers/google-gemini-cli.d.ts +118 -0
  81. package/dist/types/providers/google-interactions.d.ts +65 -0
  82. package/dist/types/providers/google-shared.d.ts +203 -0
  83. package/dist/types/providers/google-types.d.ts +155 -0
  84. package/dist/types/providers/google-vertex.d.ts +7 -0
  85. package/dist/types/providers/google.d.ts +4 -0
  86. package/dist/types/providers/grammar.d.ts +1 -0
  87. package/dist/types/providers/kimi.d.ts +27 -0
  88. package/dist/types/providers/mock.d.ts +178 -0
  89. package/dist/types/providers/ollama.d.ts +7 -0
  90. package/dist/types/providers/openai-anthropic-shim.d.ts +31 -0
  91. package/dist/types/providers/openai-chat-server-schema.d.ts +695 -0
  92. package/dist/types/providers/openai-chat-server.d.ts +16 -0
  93. package/dist/types/providers/openai-chat-wire.d.ts +644 -0
  94. package/dist/types/providers/openai-codex/request-transformer.d.ts +54 -0
  95. package/dist/types/providers/openai-codex/response-handler.d.ts +26 -0
  96. package/dist/types/providers/openai-codex-responses.d.ts +108 -0
  97. package/dist/types/providers/openai-completions.d.ts +45 -0
  98. package/dist/types/providers/openai-reasoning-fallback.d.ts +25 -0
  99. package/dist/types/providers/openai-responses-server-schema.d.ts +349 -0
  100. package/dist/types/providers/openai-responses-server.d.ts +17 -0
  101. package/dist/types/providers/openai-responses-wire.d.ts +6065 -0
  102. package/dist/types/providers/openai-responses.d.ts +126 -0
  103. package/dist/types/providers/openai-shared.d.ts +506 -0
  104. package/dist/types/providers/pi-native-client.d.ts +13 -0
  105. package/dist/types/providers/pi-native-server.d.ts +69 -0
  106. package/dist/types/providers/register-builtins.d.ts +32 -0
  107. package/dist/types/providers/synthetic.d.ts +26 -0
  108. package/dist/types/providers/transform-messages.d.ts +11 -0
  109. package/dist/types/providers/vision-guard.d.ts +20 -0
  110. package/dist/types/registry/aimlapi.d.ts +4 -0
  111. package/dist/types/registry/alibaba-coding-plan.d.ts +8 -0
  112. package/dist/types/registry/amazon-bedrock.d.ts +5 -0
  113. package/dist/types/registry/anthropic.d.ts +10 -0
  114. package/dist/types/registry/api-key-login.d.ts +42 -0
  115. package/dist/types/registry/api-key-validation.d.ts +43 -0
  116. package/dist/types/registry/azure.d.ts +4 -0
  117. package/dist/types/registry/cerebras.d.ts +7 -0
  118. package/dist/types/registry/cloudflare-ai-gateway.d.ts +13 -0
  119. package/dist/types/registry/coreweave.d.ts +7 -0
  120. package/dist/types/registry/cursor.d.ts +7 -0
  121. package/dist/types/registry/deepseek.d.ts +8 -0
  122. package/dist/types/registry/derived.d.ts +5 -0
  123. package/dist/types/registry/devin.d.ts +8 -0
  124. package/dist/types/registry/firepass.d.ts +16 -0
  125. package/dist/types/registry/fireworks.d.ts +7 -0
  126. package/dist/types/registry/github-copilot.d.ts +7 -0
  127. package/dist/types/registry/gitlab-duo-workflow.d.ts +10 -0
  128. package/dist/types/registry/gitlab-duo.d.ts +9 -0
  129. package/dist/types/registry/google-antigravity.d.ts +9 -0
  130. package/dist/types/registry/google-gemini-cli.d.ts +9 -0
  131. package/dist/types/registry/google-vertex.d.ts +5 -0
  132. package/dist/types/registry/google.d.ts +4 -0
  133. package/dist/types/registry/groq.d.ts +4 -0
  134. package/dist/types/registry/huggingface.d.ts +7 -0
  135. package/dist/types/registry/index.d.ts +4 -0
  136. package/dist/types/registry/kagi.d.ts +14 -0
  137. package/dist/types/registry/kilo.d.ts +7 -0
  138. package/dist/types/registry/kimi-code.d.ts +7 -0
  139. package/dist/types/registry/litellm.d.ts +13 -0
  140. package/dist/types/registry/llama-cpp.d.ts +8 -0
  141. package/dist/types/registry/lm-studio.d.ts +8 -0
  142. package/dist/types/registry/minimax-code-cn.d.ts +6 -0
  143. package/dist/types/registry/minimax-code.d.ts +6 -0
  144. package/dist/types/registry/minimax.d.ts +4 -0
  145. package/dist/types/registry/mistral.d.ts +4 -0
  146. package/dist/types/registry/moonshot.d.ts +7 -0
  147. package/dist/types/registry/nanogpt.d.ts +7 -0
  148. package/dist/types/registry/nvidia.d.ts +7 -0
  149. package/dist/types/registry/oauth/__tests__/xai-oauth.test.d.ts +1 -0
  150. package/dist/types/registry/oauth/anthropic.d.ts +23 -0
  151. package/dist/types/registry/oauth/callback-server.d.ts +72 -0
  152. package/dist/types/registry/oauth/cursor.d.ts +15 -0
  153. package/dist/types/registry/oauth/devin.d.ts +5 -0
  154. package/dist/types/registry/oauth/github-copilot.d.ts +30 -0
  155. package/dist/types/registry/oauth/gitlab-duo-workflow.d.ts +6 -0
  156. package/dist/types/registry/oauth/gitlab-duo.d.ts +3 -0
  157. package/dist/types/registry/oauth/google-antigravity.d.ts +11 -0
  158. package/dist/types/registry/oauth/google-gemini-cli.d.ts +10 -0
  159. package/dist/types/registry/oauth/google-oauth-shared.d.ts +22 -0
  160. package/dist/types/registry/oauth/index.d.ts +64 -0
  161. package/dist/types/registry/oauth/kimi.d.ts +21 -0
  162. package/dist/types/registry/oauth/minimax-code.d.ts +27 -0
  163. package/dist/types/registry/oauth/openai-codex.d.ts +33 -0
  164. package/dist/types/registry/oauth/opencode.d.ts +18 -0
  165. package/dist/types/registry/oauth/perplexity.d.ts +9 -0
  166. package/dist/types/registry/oauth/pkce.d.ts +8 -0
  167. package/dist/types/registry/oauth/types.d.ts +56 -0
  168. package/dist/types/registry/oauth/wafer.d.ts +1 -0
  169. package/dist/types/registry/oauth/xai-oauth.d.ts +52 -0
  170. package/dist/types/registry/oauth/xiaomi.d.ts +25 -0
  171. package/dist/types/registry/ollama-cloud.d.ts +7 -0
  172. package/dist/types/registry/ollama.d.ts +12 -0
  173. package/dist/types/registry/openai-codex-device.d.ts +8 -0
  174. package/dist/types/registry/openai-codex.d.ts +9 -0
  175. package/dist/types/registry/openai.d.ts +4 -0
  176. package/dist/types/registry/opencode-go.d.ts +6 -0
  177. package/dist/types/registry/opencode-zen.d.ts +6 -0
  178. package/dist/types/registry/openrouter.d.ts +13 -0
  179. package/dist/types/registry/parallel.d.ts +14 -0
  180. package/dist/types/registry/perplexity.d.ts +7 -0
  181. package/dist/types/registry/qianfan.d.ts +7 -0
  182. package/dist/types/registry/qwen-portal.d.ts +7 -0
  183. package/dist/types/registry/registry.d.ts +303 -0
  184. package/dist/types/registry/sakana.d.ts +7 -0
  185. package/dist/types/registry/synthetic.d.ts +6 -0
  186. package/dist/types/registry/tavily.d.ts +14 -0
  187. package/dist/types/registry/together.d.ts +6 -0
  188. package/dist/types/registry/types.d.ts +51 -0
  189. package/dist/types/registry/umans.d.ts +7 -0
  190. package/dist/types/registry/venice.d.ts +13 -0
  191. package/dist/types/registry/vercel-ai-gateway.d.ts +7 -0
  192. package/dist/types/registry/vllm.d.ts +7 -0
  193. package/dist/types/registry/wafer-serverless.d.ts +6 -0
  194. package/dist/types/registry/xai-oauth.d.ts +7 -0
  195. package/dist/types/registry/xai.d.ts +4 -0
  196. package/dist/types/registry/xiaomi-token-plan-ams.d.ts +6 -0
  197. package/dist/types/registry/xiaomi-token-plan-cn.d.ts +6 -0
  198. package/dist/types/registry/xiaomi-token-plan-sgp.d.ts +6 -0
  199. package/dist/types/registry/xiaomi.d.ts +6 -0
  200. package/dist/types/registry/zai.d.ts +7 -0
  201. package/dist/types/registry/zenmux.d.ts +7 -0
  202. package/dist/types/registry/zhipu-coding-plan.d.ts +7 -0
  203. package/dist/types/stream.d.ts +44 -0
  204. package/dist/types/types.d.ts +715 -0
  205. package/dist/types/usage/claude.d.ts +4 -0
  206. package/dist/types/usage/gemini.d.ts +2 -0
  207. package/dist/types/usage/github-copilot.d.ts +7 -0
  208. package/dist/types/usage/google-antigravity.d.ts +15 -0
  209. package/dist/types/usage/kimi.d.ts +2 -0
  210. package/dist/types/usage/minimax-code.d.ts +2 -0
  211. package/dist/types/usage/ollama.d.ts +5 -0
  212. package/dist/types/usage/openai-codex-base-url.d.ts +18 -0
  213. package/dist/types/usage/openai-codex-reset.d.ts +79 -0
  214. package/dist/types/usage/openai-codex.d.ts +3 -0
  215. package/dist/types/usage/opencode-go.d.ts +2 -0
  216. package/dist/types/usage/shared.d.ts +1 -0
  217. package/dist/types/usage/zai.d.ts +2 -0
  218. package/dist/types/usage.d.ts +346 -0
  219. package/dist/types/utils/abort.d.ts +25 -0
  220. package/dist/types/utils/anthropic-auth.d.ts +35 -0
  221. package/dist/types/utils/block-symbols.d.ts +20 -0
  222. package/dist/types/utils/deterministic-id.d.ts +16 -0
  223. package/dist/types/utils/empty-completion-retry.d.ts +21 -0
  224. package/dist/types/utils/event-stream.d.ts +30 -0
  225. package/dist/types/utils/foundry.d.ts +1 -0
  226. package/dist/types/utils/google-validation.d.ts +2 -0
  227. package/dist/types/utils/harmony-leak.d.ts +118 -0
  228. package/dist/types/utils/http-inspector.d.ts +30 -0
  229. package/dist/types/utils/idle-iterator.d.ts +137 -0
  230. package/dist/types/utils/leaked-thinking-stream.d.ts +29 -0
  231. package/dist/types/utils/openai-http.d.ts +54 -0
  232. package/dist/types/utils/openrouter-headers.d.ts +1 -0
  233. package/dist/types/utils/parse-bind.d.ts +23 -0
  234. package/dist/types/utils/provider-response.d.ts +3 -0
  235. package/dist/types/utils/proxy.d.ts +29 -0
  236. package/dist/types/utils/request-debug.d.ts +29 -0
  237. package/dist/types/utils/retry-after.d.ts +4 -0
  238. package/dist/types/utils/retry.d.ts +14 -0
  239. package/dist/types/utils/schema/adapt.d.ts +24 -0
  240. package/dist/types/utils/schema/compatibility.d.ts +30 -0
  241. package/dist/types/utils/schema/dereference.d.ts +11 -0
  242. package/dist/types/utils/schema/draft.d.ts +10 -0
  243. package/dist/types/utils/schema/equality.d.ts +4 -0
  244. package/dist/types/utils/schema/fields.d.ts +54 -0
  245. package/dist/types/utils/schema/index.d.ts +15 -0
  246. package/dist/types/utils/schema/json-schema-validator.d.ts +20 -0
  247. package/dist/types/utils/schema/meta-validator.d.ts +2 -0
  248. package/dist/types/utils/schema/normalize.d.ts +124 -0
  249. package/dist/types/utils/schema/spill.d.ts +8 -0
  250. package/dist/types/utils/schema/stamps.d.ts +17 -0
  251. package/dist/types/utils/schema/strict-tool-validation.d.ts +16 -0
  252. package/dist/types/utils/schema/types.d.ts +4 -0
  253. package/dist/types/utils/schema/typescript.d.ts +18 -0
  254. package/dist/types/utils/schema/wire.d.ts +92 -0
  255. package/dist/types/utils/schema/zod-decontaminate.d.ts +31 -0
  256. package/dist/types/utils/sdk-stream-timeout.d.ts +33 -0
  257. package/dist/types/utils/sse-debug.d.ts +5 -0
  258. package/dist/types/utils/stream-markup-healing.d.ts +87 -0
  259. package/dist/types/utils/thinking-loop.d.ts +102 -0
  260. package/dist/types/utils/tool-call-loop-guard.d.ts +26 -0
  261. package/dist/types/utils/tool-choice.d.ts +50 -0
  262. package/dist/types/utils/validation.d.ts +42 -0
  263. package/dist/types/utils.d.ts +24 -0
  264. package/package.json +139 -0
  265. package/src/api-registry.ts +109 -0
  266. package/src/auth-broker/client.ts +359 -0
  267. package/src/auth-broker/discover.ts +222 -0
  268. package/src/auth-broker/index.ts +7 -0
  269. package/src/auth-broker/refresher.ts +117 -0
  270. package/src/auth-broker/remote-store.ts +657 -0
  271. package/src/auth-broker/server.ts +646 -0
  272. package/src/auth-broker/snapshot-cache.ts +191 -0
  273. package/src/auth-broker/types.ts +130 -0
  274. package/src/auth-broker/wire-schemas.ts +249 -0
  275. package/src/auth-gateway/http.ts +194 -0
  276. package/src/auth-gateway/index.ts +3 -0
  277. package/src/auth-gateway/server.ts +802 -0
  278. package/src/auth-gateway/types.ts +151 -0
  279. package/src/auth-retry.ts +250 -0
  280. package/src/auth-storage.ts +5576 -0
  281. package/src/dialect/anthropic.md +31 -0
  282. package/src/dialect/anthropic.ts +608 -0
  283. package/src/dialect/catalog.ts +29 -0
  284. package/src/dialect/coercion.ts +136 -0
  285. package/src/dialect/deepseek.md +24 -0
  286. package/src/dialect/deepseek.ts +609 -0
  287. package/src/dialect/demotion.ts +36 -0
  288. package/src/dialect/examples.ts +33 -0
  289. package/src/dialect/factory.ts +34 -0
  290. package/src/dialect/fenced-thinking.ts +184 -0
  291. package/src/dialect/gemini.md +44 -0
  292. package/src/dialect/gemini.ts +597 -0
  293. package/src/dialect/gemma.md +33 -0
  294. package/src/dialect/gemma.ts +387 -0
  295. package/src/dialect/glm.md +32 -0
  296. package/src/dialect/glm.ts +456 -0
  297. package/src/dialect/harmony.md +31 -0
  298. package/src/dialect/harmony.ts +346 -0
  299. package/src/dialect/hermes.md +25 -0
  300. package/src/dialect/hermes.ts +206 -0
  301. package/src/dialect/history.ts +81 -0
  302. package/src/dialect/index.ts +15 -0
  303. package/src/dialect/inventory.ts +73 -0
  304. package/src/dialect/kimi.md +24 -0
  305. package/src/dialect/kimi.ts +340 -0
  306. package/src/dialect/minimax.md +31 -0
  307. package/src/dialect/minimax.ts +95 -0
  308. package/src/dialect/owned-stream.ts +470 -0
  309. package/src/dialect/prompt-template.md +12 -0
  310. package/src/dialect/qwen3.md +28 -0
  311. package/src/dialect/qwen3.ts +240 -0
  312. package/src/dialect/rendering.ts +249 -0
  313. package/src/dialect/thinking.ts +122 -0
  314. package/src/dialect/types.ts +57 -0
  315. package/src/dialect/xml.md +22 -0
  316. package/src/dialect/xml.ts +90 -0
  317. package/src/error/abort.ts +18 -0
  318. package/src/error/auth-classify.ts +30 -0
  319. package/src/error/auth.ts +48 -0
  320. package/src/error/aws.ts +31 -0
  321. package/src/error/classes.ts +186 -0
  322. package/src/error/finalize.ts +69 -0
  323. package/src/error/flags.ts +506 -0
  324. package/src/error/format.ts +45 -0
  325. package/src/error/gateway.ts +96 -0
  326. package/src/error/index.ts +13 -0
  327. package/src/error/oauth.ts +58 -0
  328. package/src/error/provider.ts +62 -0
  329. package/src/error/rate-limit.ts +161 -0
  330. package/src/error/retryable.ts +70 -0
  331. package/src/error/validation.ts +44 -0
  332. package/src/index.ts +49 -0
  333. package/src/provider-details.ts +90 -0
  334. package/src/providers/__tests__/google-auth.test.ts +144 -0
  335. package/src/providers/__tests__/kimi-code-thinking.test.ts +112 -0
  336. package/src/providers/__tests__/openai-codex-error.test.ts +84 -0
  337. package/src/providers/amazon-bedrock.ts +1042 -0
  338. package/src/providers/anthropic-client.ts +295 -0
  339. package/src/providers/anthropic-messages-server-schema.ts +252 -0
  340. package/src/providers/anthropic-messages-server.ts +756 -0
  341. package/src/providers/anthropic-wire.ts +318 -0
  342. package/src/providers/anthropic.ts +4078 -0
  343. package/src/providers/aws-credentials.ts +586 -0
  344. package/src/providers/aws-eventstream.ts +181 -0
  345. package/src/providers/aws-sigv4.ts +218 -0
  346. package/src/providers/azure-openai-responses.ts +382 -0
  347. package/src/providers/cursor/proto/agent.proto +3526 -0
  348. package/src/providers/cursor/proto/buf.gen.yaml +6 -0
  349. package/src/providers/cursor/proto/buf.yaml +17 -0
  350. package/src/providers/cursor.ts +2695 -0
  351. package/src/providers/devin/proto/buf/validate/validate.proto +468 -0
  352. package/src/providers/devin/proto/buf.gen.yaml +33 -0
  353. package/src/providers/devin/proto/buf.yaml +17 -0
  354. package/src/providers/devin/proto/cel/expr/checked.proto +103 -0
  355. package/src/providers/devin/proto/cel/expr/eval.proto +38 -0
  356. package/src/providers/devin/proto/cel/expr/explain.proto +15 -0
  357. package/src/providers/devin/proto/cel/expr/syntax.proto +113 -0
  358. package/src/providers/devin/proto/cel/expr/value.proto +41 -0
  359. package/src/providers/devin/proto/connectext/grpc/status/v1/status.proto +11 -0
  360. package/src/providers/devin/proto/errorspb/errors.proto +56 -0
  361. package/src/providers/devin/proto/errorspb/hintdetail.proto +7 -0
  362. package/src/providers/devin/proto/errorspb/markers.proto +10 -0
  363. package/src/providers/devin/proto/errorspb/tags.proto +12 -0
  364. package/src/providers/devin/proto/errorspb/testing.proto +6 -0
  365. package/src/providers/devin/proto/exa/analytics_pb/analytics.proto +188 -0
  366. package/src/providers/devin/proto/exa/api_server_pb/api_server.proto +2461 -0
  367. package/src/providers/devin/proto/exa/auth_pb/auth.proto +19 -0
  368. package/src/providers/devin/proto/exa/auto_cascade_common_pb/auto_cascade_common.proto +79 -0
  369. package/src/providers/devin/proto/exa/browser_preview_pb/browser_preview.proto +32 -0
  370. package/src/providers/devin/proto/exa/bug_checker_pb/bug_checker.proto +22 -0
  371. package/src/providers/devin/proto/exa/cascade_plugins_pb/cascade_plugins.proto +262 -0
  372. package/src/providers/devin/proto/exa/chat_client_server_pb/chat_client_server.proto +57 -0
  373. package/src/providers/devin/proto/exa/chat_pb/chat.proto +449 -0
  374. package/src/providers/devin/proto/exa/code_edit/code_edit_pb/code_edit.proto +186 -0
  375. package/src/providers/devin/proto/exa/codeium_common_pb/codeium_common.proto +4157 -0
  376. package/src/providers/devin/proto/exa/context_module_pb/context_module.proto +175 -0
  377. package/src/providers/devin/proto/exa/cortex_pb/cortex.proto +3268 -0
  378. package/src/providers/devin/proto/exa/dev_pb/dev.proto +26 -0
  379. package/src/providers/devin/proto/exa/diff_action_pb/diff_action.proto +75 -0
  380. package/src/providers/devin/proto/exa/eval/pr_eval/datasets_pb/datasets.proto +103 -0
  381. package/src/providers/devin/proto/exa/eval_pb/eval.proto +1315 -0
  382. package/src/providers/devin/proto/exa/extension_server_pb/extension_server.proto +556 -0
  383. package/src/providers/devin/proto/exa/file_system_provider_pb/file_system_provider.proto +75 -0
  384. package/src/providers/devin/proto/exa/index_pb/index.proto +461 -0
  385. package/src/providers/devin/proto/exa/knowledge_base_pb/knowledge_base.proto +144 -0
  386. package/src/providers/devin/proto/exa/language_server_pb/language_server.proto +2385 -0
  387. package/src/providers/devin/proto/exa/model_management_pb/model_management.proto +186 -0
  388. package/src/providers/devin/proto/exa/opensearch_clients_pb/opensearch_clients.proto +503 -0
  389. package/src/providers/devin/proto/exa/product_analytics_pb/product_analytics.proto +37 -0
  390. package/src/providers/devin/proto/exa/prompt_pb/prompt.proto +92 -0
  391. package/src/providers/devin/proto/exa/reactive_component_pb/reactive_component.proto +96 -0
  392. package/src/providers/devin/proto/exa/seat_management_pb/seat_management.proto +2680 -0
  393. package/src/providers/devin/proto/exa/tokenizer_pb/tokenizer.proto +37 -0
  394. package/src/providers/devin/proto/exa/trainer_pb/config.proto +647 -0
  395. package/src/providers/devin/proto/exa/tree_sitter/language_data_pb/language_data.proto +14 -0
  396. package/src/providers/devin/proto/exa/trust_pb/trust.proto +157 -0
  397. package/src/providers/devin/proto/exa/user_analytics_pb/user_analytics.proto +519 -0
  398. package/src/providers/devin/proto/google.golang.org/appengine/internal/base/api_base.proto +28 -0
  399. package/src/providers/devin/proto/google.golang.org/appengine/internal/datastore/datastore_v3.proto +484 -0
  400. package/src/providers/devin/proto/google.golang.org/appengine/internal/log/log_service.proto +136 -0
  401. package/src/providers/devin/proto/google.golang.org/appengine/internal/remote_api/remote_api.proto +42 -0
  402. package/src/providers/devin/proto/google.golang.org/appengine/internal/urlfetch/urlfetch_service.proto +61 -0
  403. package/src/providers/devin/proto/grpc/binlog/v1/binarylog.proto +84 -0
  404. package/src/providers/devin/proto/io/prometheus/client/metrics.proto +98 -0
  405. package/src/providers/devin.ts +577 -0
  406. package/src/providers/error-message.ts +21 -0
  407. package/src/providers/github-copilot-headers.ts +141 -0
  408. package/src/providers/gitlab-duo-workflow-chatml-note.md +1 -0
  409. package/src/providers/gitlab-duo-workflow.ts +3058 -0
  410. package/src/providers/gitlab-duo.ts +395 -0
  411. package/src/providers/google-auth.ts +350 -0
  412. package/src/providers/google-gemini-cli.ts +1362 -0
  413. package/src/providers/google-interactions.ts +753 -0
  414. package/src/providers/google-shared.ts +1103 -0
  415. package/src/providers/google-types.ts +180 -0
  416. package/src/providers/google-vertex.ts +183 -0
  417. package/src/providers/google.ts +87 -0
  418. package/src/providers/grammar.ts +70 -0
  419. package/src/providers/kimi.ts +52 -0
  420. package/src/providers/mock.ts +507 -0
  421. package/src/providers/ollama.ts +773 -0
  422. package/src/providers/openai-anthropic-shim.ts +152 -0
  423. package/src/providers/openai-chat-server-schema.ts +242 -0
  424. package/src/providers/openai-chat-server.ts +715 -0
  425. package/src/providers/openai-chat-wire.ts +847 -0
  426. package/src/providers/openai-codex/request-transformer.ts +295 -0
  427. package/src/providers/openai-codex/response-handler.ts +102 -0
  428. package/src/providers/openai-codex-responses.ts +3468 -0
  429. package/src/providers/openai-completions.ts +2173 -0
  430. package/src/providers/openai-reasoning-fallback.ts +269 -0
  431. package/src/providers/openai-responses-reasoning-suppression.md +1 -0
  432. package/src/providers/openai-responses-server-schema.ts +282 -0
  433. package/src/providers/openai-responses-server.ts +1280 -0
  434. package/src/providers/openai-responses-wire.ts +6391 -0
  435. package/src/providers/openai-responses.ts +1022 -0
  436. package/src/providers/openai-shared.ts +2648 -0
  437. package/src/providers/pi-native-client.ts +266 -0
  438. package/src/providers/pi-native-server.ts +242 -0
  439. package/src/providers/register-builtins.ts +475 -0
  440. package/src/providers/synthetic.ts +50 -0
  441. package/src/providers/transform-messages.ts +787 -0
  442. package/src/providers/vision-guard.ts +54 -0
  443. package/src/registry/aimlapi.ts +6 -0
  444. package/src/registry/alibaba-coding-plan.ts +95 -0
  445. package/src/registry/amazon-bedrock.ts +22 -0
  446. package/src/registry/anthropic.ts +26 -0
  447. package/src/registry/api-key-login.ts +112 -0
  448. package/src/registry/api-key-validation.ts +161 -0
  449. package/src/registry/azure.ts +6 -0
  450. package/src/registry/cerebras.ts +23 -0
  451. package/src/registry/cloudflare-ai-gateway.ts +45 -0
  452. package/src/registry/coreweave.ts +40 -0
  453. package/src/registry/cursor.ts +20 -0
  454. package/src/registry/deepseek.ts +46 -0
  455. package/src/registry/derived.ts +9 -0
  456. package/src/registry/devin.ts +15 -0
  457. package/src/registry/firepass.ts +32 -0
  458. package/src/registry/fireworks.ts +28 -0
  459. package/src/registry/github-copilot.ts +22 -0
  460. package/src/registry/gitlab-duo-workflow.ts +20 -0
  461. package/src/registry/gitlab-duo.ts +19 -0
  462. package/src/registry/google-antigravity.ts +22 -0
  463. package/src/registry/google-gemini-cli.ts +22 -0
  464. package/src/registry/google-vertex.ts +38 -0
  465. package/src/registry/google.ts +6 -0
  466. package/src/registry/groq.ts +6 -0
  467. package/src/registry/huggingface.ts +29 -0
  468. package/src/registry/index.ts +4 -0
  469. package/src/registry/kagi.ts +46 -0
  470. package/src/registry/kilo.ts +114 -0
  471. package/src/registry/kimi-code.ts +17 -0
  472. package/src/registry/litellm.ts +45 -0
  473. package/src/registry/llama-cpp.ts +35 -0
  474. package/src/registry/lm-studio.ts +31 -0
  475. package/src/registry/minimax-code-cn.ts +12 -0
  476. package/src/registry/minimax-code.ts +12 -0
  477. package/src/registry/minimax.ts +6 -0
  478. package/src/registry/mistral.ts +6 -0
  479. package/src/registry/moonshot.ts +22 -0
  480. package/src/registry/nanogpt.ts +22 -0
  481. package/src/registry/nvidia.ts +61 -0
  482. package/src/registry/oauth/__tests__/xai-oauth.test.ts +104 -0
  483. package/src/registry/oauth/anthropic.ts +311 -0
  484. package/src/registry/oauth/callback-server.ts +315 -0
  485. package/src/registry/oauth/cursor.ts +171 -0
  486. package/src/registry/oauth/devin.ts +124 -0
  487. package/src/registry/oauth/github-copilot.ts +369 -0
  488. package/src/registry/oauth/gitlab-duo-workflow.ts +146 -0
  489. package/src/registry/oauth/gitlab-duo.ts +222 -0
  490. package/src/registry/oauth/google-antigravity.ts +209 -0
  491. package/src/registry/oauth/google-gemini-cli.ts +273 -0
  492. package/src/registry/oauth/google-oauth-shared.ts +125 -0
  493. package/src/registry/oauth/index.ts +269 -0
  494. package/src/registry/oauth/kimi.ts +289 -0
  495. package/src/registry/oauth/minimax-code.ts +53 -0
  496. package/src/registry/oauth/oauth.html +311 -0
  497. package/src/registry/oauth/openai-codex.ts +364 -0
  498. package/src/registry/oauth/opencode.ts +50 -0
  499. package/src/registry/oauth/perplexity.ts +228 -0
  500. package/src/registry/oauth/pkce.ts +18 -0
  501. package/src/registry/oauth/types.ts +65 -0
  502. package/src/registry/oauth/wafer.ts +24 -0
  503. package/src/registry/oauth/xai-oauth.ts +394 -0
  504. package/src/registry/oauth/xiaomi.ts +211 -0
  505. package/src/registry/ollama-cloud.ts +36 -0
  506. package/src/registry/ollama.ts +43 -0
  507. package/src/registry/openai-codex-device.ts +18 -0
  508. package/src/registry/openai-codex.ts +19 -0
  509. package/src/registry/openai.ts +6 -0
  510. package/src/registry/opencode-go.ts +12 -0
  511. package/src/registry/opencode-zen.ts +12 -0
  512. package/src/registry/openrouter.ts +28 -0
  513. package/src/registry/parallel.ts +45 -0
  514. package/src/registry/perplexity.ts +13 -0
  515. package/src/registry/qianfan.ts +27 -0
  516. package/src/registry/qwen-portal.ts +50 -0
  517. package/src/registry/registry.ts +161 -0
  518. package/src/registry/sakana.ts +22 -0
  519. package/src/registry/synthetic.ts +21 -0
  520. package/src/registry/tavily.ts +45 -0
  521. package/src/registry/together.ts +22 -0
  522. package/src/registry/types.ts +56 -0
  523. package/src/registry/umans.ts +23 -0
  524. package/src/registry/venice.ts +33 -0
  525. package/src/registry/vercel-ai-gateway.ts +38 -0
  526. package/src/registry/vllm.ts +34 -0
  527. package/src/registry/wafer-serverless.ts +12 -0
  528. package/src/registry/xai-oauth.ts +17 -0
  529. package/src/registry/xai.ts +6 -0
  530. package/src/registry/xiaomi-token-plan-ams.ts +12 -0
  531. package/src/registry/xiaomi-token-plan-cn.ts +12 -0
  532. package/src/registry/xiaomi-token-plan-sgp.ts +12 -0
  533. package/src/registry/xiaomi.ts +12 -0
  534. package/src/registry/zai.ts +27 -0
  535. package/src/registry/zenmux.ts +22 -0
  536. package/src/registry/zhipu-coding-plan.ts +27 -0
  537. package/src/stream.ts +1778 -0
  538. package/src/types.ts +856 -0
  539. package/src/usage/claude.ts +485 -0
  540. package/src/usage/gemini.ts +258 -0
  541. package/src/usage/github-copilot.ts +424 -0
  542. package/src/usage/google-antigravity.ts +497 -0
  543. package/src/usage/kimi.ts +271 -0
  544. package/src/usage/minimax-code.ts +30 -0
  545. package/src/usage/ollama.ts +41 -0
  546. package/src/usage/openai-codex-base-url.ts +35 -0
  547. package/src/usage/openai-codex-reset.ts +174 -0
  548. package/src/usage/openai-codex.ts +535 -0
  549. package/src/usage/opencode-go.ts +89 -0
  550. package/src/usage/shared.ts +10 -0
  551. package/src/usage/zai.ts +321 -0
  552. package/src/usage.ts +333 -0
  553. package/src/utils/abort.ts +67 -0
  554. package/src/utils/anthropic-auth.ts +93 -0
  555. package/src/utils/block-symbols.ts +32 -0
  556. package/src/utils/deterministic-id.ts +20 -0
  557. package/src/utils/empty-completion-retry.ts +159 -0
  558. package/src/utils/event-stream.ts +171 -0
  559. package/src/utils/foundry.ts +8 -0
  560. package/src/utils/google-validation.ts +25 -0
  561. package/src/utils/harmony-leak.ts +456 -0
  562. package/src/utils/http-inspector.ts +168 -0
  563. package/src/utils/idle-iterator.ts +473 -0
  564. package/src/utils/leaked-thinking-stream.ts +294 -0
  565. package/src/utils/openai-http.ts +122 -0
  566. package/src/utils/openrouter-headers.ts +12 -0
  567. package/src/utils/parse-bind.ts +56 -0
  568. package/src/utils/provider-response.ts +30 -0
  569. package/src/utils/proxy.ts +240 -0
  570. package/src/utils/request-debug.ts +351 -0
  571. package/src/utils/retry-after.ts +110 -0
  572. package/src/utils/retry.ts +59 -0
  573. package/src/utils/schema/CONSTRAINTS.md +166 -0
  574. package/src/utils/schema/adapt.ts +36 -0
  575. package/src/utils/schema/compatibility.ts +435 -0
  576. package/src/utils/schema/dereference.ts +98 -0
  577. package/src/utils/schema/draft.ts +341 -0
  578. package/src/utils/schema/equality.ts +97 -0
  579. package/src/utils/schema/fields.ts +207 -0
  580. package/src/utils/schema/index.ts +15 -0
  581. package/src/utils/schema/json-schema-validator.ts +595 -0
  582. package/src/utils/schema/meta-validator.ts +167 -0
  583. package/src/utils/schema/normalize.ts +1901 -0
  584. package/src/utils/schema/spill.ts +43 -0
  585. package/src/utils/schema/stamps.ts +109 -0
  586. package/src/utils/schema/strict-tool-validation.ts +117 -0
  587. package/src/utils/schema/types.ts +10 -0
  588. package/src/utils/schema/typescript.ts +198 -0
  589. package/src/utils/schema/wire.ts +789 -0
  590. package/src/utils/schema/zod-decontaminate.ts +331 -0
  591. package/src/utils/sdk-stream-timeout.ts +43 -0
  592. package/src/utils/sse-debug.ts +18 -0
  593. package/src/utils/stream-markup-healing.ts +247 -0
  594. package/src/utils/thinking-loop.ts +552 -0
  595. package/src/utils/tool-call-loop-guard.ts +107 -0
  596. package/src/utils/tool-choice.ts +99 -0
  597. package/src/utils/validation.ts +1507 -0
  598. package/src/utils.ts +171 -0
@@ -0,0 +1,4078 @@
1
+ import * as nodeCrypto from "node:crypto";
2
+ import * as fs from "node:fs";
3
+ import { scheduler } from "node:timers/promises";
4
+ import * as tls from "node:tls";
5
+ import { isOfficialAnthropicApiUrl } from "jeopi-catalog/compat/anthropic";
6
+ import { mapEffortToAnthropicAdaptiveEffort } from "jeopi-catalog/model-thinking";
7
+ import { calculateCost, getBundledModel } from "jeopi-catalog/models";
8
+ import { isAnthropicOAuthToken } from "jeopi-catalog/utils";
9
+ import { parseGitHubCopilotApiKey } from "jeopi-catalog/wire/github-copilot";
10
+ import {
11
+ $env,
12
+ getInstallId,
13
+ isEnoent,
14
+ logger,
15
+ parseJsonWithRepair,
16
+ parseStreamingJsonThrottled,
17
+ readSseEvents,
18
+ } from "jeopi-utils";
19
+ import { renderDemotedThinking } from "../dialect/demotion";
20
+ import * as AIError from "../error";
21
+ import { getEnvApiKey, OUTPUT_FALLBACK_BUFFER } from "../stream";
22
+ import type {
23
+ AnthropicFallbackContent,
24
+ Api,
25
+ AssistantMessage,
26
+ CacheRetention,
27
+ Context,
28
+ FetchImpl,
29
+ ImageContent,
30
+ Message,
31
+ Model,
32
+ ProviderSessionState,
33
+ RawSseEvent,
34
+ RedactedThinkingContent,
35
+ ServiceTier,
36
+ SimpleStreamOptions,
37
+ StopReason,
38
+ StreamFunction,
39
+ StreamOptions,
40
+ TextContent,
41
+ ThinkingContent,
42
+ Tool,
43
+ ToolCall,
44
+ ToolResultMessage,
45
+ Usage,
46
+ } from "../types";
47
+ import { isRecord, normalizeSystemPrompts, normalizeToolCallId, resolveCacheRetention } from "../utils";
48
+ import { createAbortSourceTracker } from "../utils/abort";
49
+ import {
50
+ clearStreamingPartialJson,
51
+ kStreamingBlockIndex,
52
+ kStreamingLastParseLen,
53
+ kStreamingPartialJson,
54
+ } from "../utils/block-symbols";
55
+ import { withEmptyCompletionRetry } from "../utils/empty-completion-retry";
56
+ import { AssistantMessageEventStream } from "../utils/event-stream";
57
+ import { isFoundryEnabled } from "../utils/foundry";
58
+ import { finalizeErrorMessage, type RawHttpRequestDump } from "../utils/http-inspector";
59
+ import { getStreamFirstEventTimeoutMs, getStreamIdleTimeoutMs, iterateWithIdleTimeout } from "../utils/idle-iterator";
60
+ import { notifyProviderResponse } from "../utils/provider-response";
61
+ import { COMBINATOR_KEYS, NO_STRICT, toolWireSchema } from "../utils/schema";
62
+ import { spillToDescription } from "../utils/schema/spill";
63
+ import { createSdkStreamRequestOptions } from "../utils/sdk-stream-timeout";
64
+ import { notifyRawSseEvent } from "../utils/sse-debug";
65
+ import {
66
+ AnthropicApiError,
67
+ AnthropicConnectionTimeoutError,
68
+ type AnthropicFetchOptions,
69
+ AnthropicMessagesClient,
70
+ type AnthropicMessagesClientLike,
71
+ calculateAnthropicRetryDelayMs,
72
+ retryDelayFromHeaders,
73
+ } from "./anthropic-client";
74
+ import type {
75
+ ToolInputSchema as AnthropicToolInputSchema,
76
+ Tool as AnthropicWireTool,
77
+ Usage as AnthropicWireUsage,
78
+ ContentBlockParam,
79
+ FallbackParam,
80
+ MessageCreateParamsStreaming,
81
+ MessageParam,
82
+ RawMessageStreamEvent,
83
+ TextBlockParam,
84
+ } from "./anthropic-wire";
85
+ import {
86
+ buildCopilotDynamicHeaders,
87
+ hasCopilotVisionInput,
88
+ resolveGitHubCopilotBaseUrl,
89
+ } from "./github-copilot-headers";
90
+ import { transformMessages } from "./transform-messages";
91
+ import { NON_VISION_IMAGE_PLACEHOLDER } from "./vision-guard";
92
+
93
+ export type AnthropicHeaderOptions = {
94
+ apiKey: string;
95
+ baseUrl?: string;
96
+ isOAuth?: boolean;
97
+ extraBetas?: string[];
98
+ stream?: boolean;
99
+ modelHeaders?: Record<string, string>;
100
+ isCloudflareAiGateway?: boolean;
101
+ claudeCodeSessionId?: string;
102
+ claudeCodeBetas?: readonly string[];
103
+ };
104
+
105
+ export function normalizeAnthropicBaseUrl(baseUrl?: string): string | undefined {
106
+ const trimmed = baseUrl?.trim();
107
+ if (!trimmed) {
108
+ return undefined;
109
+ }
110
+ const withoutTrailingSlashes = trimmed.replace(/\/+$/, "");
111
+ return withoutTrailingSlashes.endsWith("/v1") ? withoutTrailingSlashes.slice(0, -3) : withoutTrailingSlashes;
112
+ }
113
+
114
+ // Build deduplicated beta header string
115
+ export function buildBetaHeader(baseBetas: readonly string[], extraBetas: readonly string[]): string {
116
+ const seen = new Set<string>();
117
+ const result: string[] = [];
118
+ for (const beta of [...baseBetas, ...extraBetas]) {
119
+ const trimmed = beta.trim();
120
+ if (trimmed && !seen.has(trimmed)) {
121
+ seen.add(trimmed);
122
+ result.push(trimmed);
123
+ }
124
+ }
125
+ return result.join(",");
126
+ }
127
+
128
+ const midConversationSystemBeta = "mid-conversation-system-2026-04-07";
129
+ const contextManagementBeta = "context-management-2025-06-27";
130
+ const claudeCodeUtilityBetaDefaults = [
131
+ "oauth-2025-04-20",
132
+ "interleaved-thinking-2025-05-14",
133
+ contextManagementBeta,
134
+ "prompt-caching-scope-2026-01-05",
135
+ "structured-outputs-2025-12-15",
136
+ ] as const;
137
+ const claudeCodeAgentBetaDefaults = [
138
+ "claude-code-20250219",
139
+ "oauth-2025-04-20",
140
+ "interleaved-thinking-2025-05-14",
141
+ contextManagementBeta,
142
+ "prompt-caching-scope-2026-01-05",
143
+ midConversationSystemBeta,
144
+ "advanced-tool-use-2025-11-20",
145
+ ] as const;
146
+ const claudeCodeAgentPostEffortBetas = ["extended-cache-ttl-2025-04-11"] as const;
147
+ const fineGrainedToolStreamingBeta = "fine-grained-tool-streaming-2025-05-14";
148
+ const interleavedThinkingBeta = "interleaved-thinking-2025-05-14";
149
+ // Asks the API to redact thinking blocks from responses. Only sent when the
150
+ // caller explicitly hides thinking (`thinkingDisplay: "omitted"`); sending it
151
+ // by default suppresses the thinking traces callers expect to stream.
152
+ const redactThinkingBeta = "redact-thinking-2026-02-12";
153
+ const fastModeBeta = "fast-mode-2026-02-01";
154
+ const taskBudgetBeta = "task-budgets-2026-03-13";
155
+ const effortBeta = "effort-2025-11-24";
156
+ const serverSideFallbackBeta = "server-side-fallback-2026-06-01";
157
+
158
+ function buildClaudeCodeBetas(
159
+ agentRequest: boolean,
160
+ thinkingRequest: boolean,
161
+ redactThinking: boolean,
162
+ ): readonly string[] {
163
+ if (!agentRequest && !redactThinking) return claudeCodeUtilityBetaDefaults;
164
+ const betas: string[] = [];
165
+ for (const beta of agentRequest ? claudeCodeAgentBetaDefaults : claudeCodeUtilityBetaDefaults) {
166
+ betas.push(beta);
167
+ // Match CC's header order: redact-thinking immediately follows interleaved-thinking.
168
+ if (redactThinking && beta === interleavedThinkingBeta) betas.push(redactThinkingBeta);
169
+ }
170
+ if (!agentRequest) return betas;
171
+ if (thinkingRequest) betas.push(effortBeta);
172
+ betas.push(...claudeCodeAgentPostEffortBetas);
173
+ return betas;
174
+ }
175
+
176
+ function getHeaderCaseInsensitive(headers: Record<string, string> | undefined, headerName: string): string | undefined {
177
+ if (!headers) return undefined;
178
+ const normalizedName = headerName.toLowerCase();
179
+ for (const [key, value] of Object.entries(headers)) {
180
+ if (key.toLowerCase() === normalizedName) return value;
181
+ }
182
+ return undefined;
183
+ }
184
+
185
+ function isClaudeCodeClientUserAgent(userAgent: string | undefined): userAgent is string {
186
+ if (!userAgent) return false;
187
+ return userAgent.toLowerCase().startsWith("claude-cli");
188
+ }
189
+
190
+ const sharedHeaders = {
191
+ "Accept-Encoding": "gzip, deflate, br, zstd",
192
+ Connection: "keep-alive",
193
+ "Content-Type": "application/json",
194
+ "anthropic-version": "2023-06-01",
195
+ "anthropic-dangerous-direct-browser-access": "true",
196
+ "x-app": "cli",
197
+ };
198
+
199
+ export function buildAnthropicHeaders(options: AnthropicHeaderOptions): Record<string, string> {
200
+ const oauthToken = options.isOAuth ?? isAnthropicOAuthToken(options.apiKey);
201
+ const extraBetas = options.extraBetas ?? [];
202
+ const stream = options.stream ?? false;
203
+ // `enforcedHeaderKeys` strips User-Agent / X-Api-Key / Authorization out of
204
+ // modelHeaders so a case-insensitive spread can't produce duplicate keys; each
205
+ // branch re-adds the caller's value explicitly. User-Agent and X-Api-Key are
206
+ // always honored (with branch-specific defaults filling in when absent), while
207
+ // Authorization is honored for every non-OAuth, non-Cloudflare-gateway branch —
208
+ // OAuth requests MUST carry `Authorization: Bearer <oauth-token>` (the OAuth
209
+ // credential itself) and Cloudflare AI Gateway authenticates via
210
+ // `cf-aig-authorization`, so user-supplied auth there would just leak. Both of
211
+ // those cases drop + log the caller value (#3391).
212
+ const incomingUserAgent = getHeaderCaseInsensitive(options.modelHeaders, "User-Agent");
213
+ const incomingAuthorization = getHeaderCaseInsensitive(options.modelHeaders, "Authorization");
214
+ const incomingApiKey = getHeaderCaseInsensitive(options.modelHeaders, "X-Api-Key");
215
+ // Claude Code betas (oauth-2025-04-20, claude-code-20250219, …) are part of
216
+ // the OAuth fingerprint; API-key requests default to extras only, matching
217
+ // the streaming path (buildAnthropicClientOptions passes [] for non-OAuth).
218
+ const betaHeader = buildBetaHeader(
219
+ options.claudeCodeBetas ?? (oauthToken ? buildClaudeCodeBetas(true, true, false) : []),
220
+ extraBetas,
221
+ );
222
+ const acceptHeader = oauthToken ? "application/json" : stream ? "text/event-stream" : "application/json";
223
+ const isCloudflare = options.isCloudflareAiGateway ?? false;
224
+ const honorAuthorization = !oauthToken && !isCloudflare;
225
+ const honorApiKey = !isCloudflare;
226
+ const modelHeaders: Record<string, string> = {};
227
+ const filteredEnforcedKeys: string[] = [];
228
+ for (const [key, value] of Object.entries(options.modelHeaders ?? {})) {
229
+ const lowerKey = key.toLowerCase();
230
+ if (enforcedHeaderKeys.has(lowerKey)) {
231
+ // user-agent is always re-applied explicitly. authorization / x-api-key
232
+ // are silently re-applied in honoring branches and dropped + logged
233
+ // where the branch enforces its own credential.
234
+ if (lowerKey === "user-agent") continue;
235
+ if (lowerKey === "authorization" && honorAuthorization) continue;
236
+ if (lowerKey === "x-api-key" && honorApiKey) continue;
237
+ filteredEnforcedKeys.push(key);
238
+ continue;
239
+ }
240
+ modelHeaders[key] = value;
241
+ }
242
+ if (filteredEnforcedKeys.length > 0) {
243
+ // Caller/env-supplied values (options.headers, ANTHROPIC_CUSTOM_HEADERS)
244
+ // for enforced headers are replaced by our own values; say so instead of
245
+ // dropping them silently. Keys only — values may carry credentials.
246
+ logger.debug("anthropic: ignoring caller-supplied enforced headers", {
247
+ headers: filteredEnforcedKeys,
248
+ });
249
+ }
250
+
251
+ if (isCloudflare) {
252
+ return {
253
+ ...modelHeaders,
254
+ Accept: acceptHeader,
255
+ ...sharedHeaders,
256
+ ...(incomingUserAgent ? { "User-Agent": incomingUserAgent } : {}),
257
+ ...(betaHeader ? { "anthropic-beta": betaHeader } : {}),
258
+ "cf-aig-authorization": `Bearer ${options.apiKey}`,
259
+ };
260
+ }
261
+
262
+ if (oauthToken) {
263
+ const userAgent = isClaudeCodeClientUserAgent(incomingUserAgent)
264
+ ? incomingUserAgent
265
+ : `claude-cli/${claudeCodeVersion} (external, local-agent, agent-sdk/${claudeAgentSdkVersion})`;
266
+ return {
267
+ ...modelHeaders,
268
+ ...claudeCodeHeaders,
269
+ Accept: acceptHeader,
270
+ Authorization: `Bearer ${options.apiKey}`,
271
+ ...sharedHeaders,
272
+ ...(betaHeader ? { "anthropic-beta": betaHeader } : {}),
273
+ ...(options.claudeCodeSessionId ? { "X-Claude-Code-Session-Id": options.claudeCodeSessionId } : {}),
274
+ "x-client-request-id": nodeCrypto.randomUUID(),
275
+ "User-Agent": userAgent,
276
+ ...(incomingApiKey ? { "X-Api-Key": incomingApiKey } : {}),
277
+ };
278
+ } else if (!isOfficialAnthropicApiUrl(options.baseUrl)) {
279
+ return {
280
+ ...modelHeaders,
281
+ Accept: acceptHeader,
282
+ Authorization: incomingAuthorization ?? `Bearer ${options.apiKey}`,
283
+ ...sharedHeaders,
284
+ ...(incomingUserAgent ? { "User-Agent": incomingUserAgent } : {}),
285
+ ...(betaHeader ? { "anthropic-beta": betaHeader } : {}),
286
+ ...(incomingApiKey ? { "X-Api-Key": incomingApiKey } : {}),
287
+ };
288
+ } else {
289
+ return {
290
+ ...modelHeaders,
291
+ Accept: acceptHeader,
292
+ ...sharedHeaders,
293
+ ...(incomingUserAgent ? { "User-Agent": incomingUserAgent } : {}),
294
+ ...(betaHeader ? { "anthropic-beta": betaHeader } : {}),
295
+ ...(incomingAuthorization ? { Authorization: incomingAuthorization } : {}),
296
+ "X-Api-Key": incomingApiKey ?? options.apiKey,
297
+ };
298
+ }
299
+ }
300
+
301
+ type AnthropicCacheControl = NonNullable<TextBlockParam["cache_control"]>;
302
+ type AnthropicImageMediaType = "image/jpeg" | "image/png" | "image/gif" | "image/webp";
303
+
304
+ function normalizeAnthropicImageMediaType(mimeType: string): AnthropicImageMediaType | undefined {
305
+ const normalized = mimeType.trim().toLowerCase();
306
+ if (normalized === "image/jpg") return "image/jpeg";
307
+ if (
308
+ normalized === "image/jpeg" ||
309
+ normalized === "image/png" ||
310
+ normalized === "image/gif" ||
311
+ normalized === "image/webp"
312
+ ) {
313
+ return normalized;
314
+ }
315
+ return undefined;
316
+ }
317
+
318
+ function cloneAnthropicCacheControl(cacheControl: AnthropicCacheControl): AnthropicCacheControl {
319
+ return { ...cacheControl };
320
+ }
321
+
322
+ type AnthropicOutputConfig = NonNullable<MessageCreateParamsStreaming["output_config"]>;
323
+
324
+ const ANTHROPIC_STOP_SEQUENCES_MAX = 4;
325
+ let warnedStopSequencesTrim = false;
326
+
327
+ const ANTHROPIC_PROVIDER_SESSION_STATE_KEY = "anthropic-messages";
328
+
329
+ type AnthropicProviderSessionState = ProviderSessionState & {
330
+ strictToolsDisabled: boolean;
331
+ fastModeDisabled: boolean;
332
+ };
333
+
334
+ function createAnthropicProviderSessionState(): AnthropicProviderSessionState {
335
+ const state: AnthropicProviderSessionState = {
336
+ strictToolsDisabled: false,
337
+ fastModeDisabled: false,
338
+ close: () => {
339
+ state.strictToolsDisabled = false;
340
+ state.fastModeDisabled = false;
341
+ },
342
+ };
343
+ return state;
344
+ }
345
+
346
+ /**
347
+ * Key the sticky strict-tools / fast-mode learning per endpoint+model. A
348
+ * grammar-too-large 400 or a fast-mode rejection is specific to the model (its
349
+ * tool grammar / entitlement) and the endpoint (direct Anthropic vs a gateway /
350
+ * Foundry / Bedrock proxy), so it MUST NOT bleed onto unrelated anthropic-messages
351
+ * requests in the same session. NUL separates the two components so neither can
352
+ * forge the boundary.
353
+ */
354
+ function anthropicProviderSessionStateKey(baseUrl: string, modelId: string): string {
355
+ return `${ANTHROPIC_PROVIDER_SESSION_STATE_KEY}:${baseUrl}\u0000${modelId}`;
356
+ }
357
+
358
+ function getAnthropicProviderSessionState(
359
+ providerSessionState: Map<string, ProviderSessionState> | undefined,
360
+ baseUrl: string,
361
+ modelId: string,
362
+ ): AnthropicProviderSessionState | undefined {
363
+ if (!providerSessionState) return undefined;
364
+ const key = anthropicProviderSessionStateKey(baseUrl, modelId);
365
+ const existing = providerSessionState.get(key) as AnthropicProviderSessionState | undefined;
366
+ if (existing) return existing;
367
+ const created = createAnthropicProviderSessionState();
368
+ providerSessionState.set(key, created);
369
+ return created;
370
+ }
371
+
372
+ /**
373
+ * Clears the in-session "server rejected fast mode" sticky flag. Call when the
374
+ * caller is explicitly re-arming `serviceTier: "priority"` (e.g. user toggled
375
+ * `/fast on` after a previous turn auto-disabled it) so the next request
376
+ * actually carries `speed: "fast"` again. No-op when the map or state entry
377
+ * hasn't been materialized yet.
378
+ */
379
+ export function clearAnthropicFastModeFallback(
380
+ providerSessionState: Map<string, ProviderSessionState> | undefined,
381
+ ): void {
382
+ if (!providerSessionState) return;
383
+ // Fast mode is re-armed session-wide (user toggled `/fast on`), so clear the
384
+ // sticky flag on every per-endpoint/model Anthropic entry — plus the legacy
385
+ // unscoped key — rather than a single shared object.
386
+ const prefix = `${ANTHROPIC_PROVIDER_SESSION_STATE_KEY}:`;
387
+ for (const [key, value] of providerSessionState) {
388
+ if (key !== ANTHROPIC_PROVIDER_SESSION_STATE_KEY && !key.startsWith(prefix)) continue;
389
+ (value as AnthropicProviderSessionState).fastModeDisabled = false;
390
+ }
391
+ }
392
+
393
+ function hasStrictAnthropicTools(params: MessageCreateParamsStreaming): boolean {
394
+ return params.tools?.some(tool => tool.strict === true) ?? false;
395
+ }
396
+
397
+ function dropAnthropicFastMode(params: MessageCreateParamsStreaming): void {
398
+ delete params.speed;
399
+ }
400
+
401
+ function dropAnthropicStrictTools(params: MessageCreateParamsStreaming): void {
402
+ if (!params.tools) return;
403
+ for (const tool of params.tools) {
404
+ delete tool.strict;
405
+ }
406
+ }
407
+
408
+ function getCacheControl(
409
+ model: Model<"anthropic-messages">,
410
+ cacheRetention: CacheRetention | undefined,
411
+ isOAuthToken: boolean,
412
+ ): { retention: CacheRetention; cacheControl?: AnthropicCacheControl } {
413
+ const retention = cacheRetention ?? (isOAuthToken ? "long" : resolveCacheRetention(undefined));
414
+ if (retention === "none") {
415
+ return { retention };
416
+ }
417
+ const ttl = retention === "long" && model.compat.supportsLongCacheRetention ? "1h" : undefined;
418
+ return {
419
+ retention,
420
+ cacheControl: { type: "ephemeral", ...(ttl && { ttl }) },
421
+ };
422
+ }
423
+
424
+ // Stealth mode: mimic Claude Code's request fingerprint.
425
+ export const claudeCodeVersion = "2.1.165";
426
+ export const claudeAgentSdkVersion = "0.3.165";
427
+ export const claudeClientVersion = "1.11187.4";
428
+ export const claudeToolPrefix: string = "_";
429
+ export const claudeCodeSystemInstruction = "You are a Claude agent, built on Anthropic's Claude Agent SDK.";
430
+ // Claude Code caps requested output at 64k tokens even when the model ceiling is
431
+ // higher (e.g. Opus 4.8 supports 128k); OAuth requests clamp to match the wire
432
+ // fingerprint. API-key requests keep the full model ceiling.
433
+ export const CLAUDE_CODE_MAX_OUTPUT_TOKENS = 64000;
434
+
435
+ export function mapStainlessOs(platform: string): "MacOS" | "Windows" | "Linux" | "FreeBSD" | `Other::${string}` {
436
+ switch (platform.toLowerCase()) {
437
+ case "darwin":
438
+ return "MacOS";
439
+ case "windows":
440
+ case "win32":
441
+ return "Windows";
442
+ case "linux":
443
+ return "Linux";
444
+ case "freebsd":
445
+ return "FreeBSD";
446
+ default:
447
+ return `Other::${platform.toLowerCase()}`;
448
+ }
449
+ }
450
+
451
+ export function mapStainlessArch(arch: string): "x64" | "arm64" | "x86" | `other::${string}` {
452
+ switch (arch.toLowerCase()) {
453
+ case "amd64":
454
+ case "x64":
455
+ return "x64";
456
+ case "arm64":
457
+ case "aarch64":
458
+ return "arm64";
459
+ case "386":
460
+ case "x86":
461
+ case "ia32":
462
+ return "x86";
463
+ default:
464
+ return `other::${arch.toLowerCase()}`;
465
+ }
466
+ }
467
+
468
+ export const claudeCodeHeaders = {
469
+ "X-Stainless-Retry-Count": "0",
470
+ "X-Stainless-Runtime-Version": "v24.3.0",
471
+ "X-Stainless-Package-Version": "0.94.0",
472
+ "X-Stainless-Runtime": "node",
473
+ "X-Stainless-Lang": "js",
474
+ "X-Stainless-Arch": mapStainlessArch(process.arch),
475
+ "X-Stainless-OS": mapStainlessOs(process.platform),
476
+ "X-Stainless-Timeout": "900",
477
+ "anthropic-client-platform": "desktop_app",
478
+ "anthropic-client-version": claudeClientVersion,
479
+ };
480
+
481
+ const enforcedHeaderKeys = new Set(
482
+ [
483
+ ...Object.keys(claudeCodeHeaders),
484
+ "Accept",
485
+ "Accept-Encoding",
486
+ "Connection",
487
+ "Content-Type",
488
+ "anthropic-version",
489
+ "anthropic-dangerous-direct-browser-access",
490
+ "anthropic-beta",
491
+ "User-Agent",
492
+ "x-app",
493
+ "Authorization",
494
+ "X-Api-Key",
495
+ "X-Claude-Code-Session-Id",
496
+ "x-client-request-id",
497
+ "cf-aig-authorization",
498
+ ].map(key => key.toLowerCase()),
499
+ );
500
+
501
+ const CLAUDE_BILLING_HEADER_PREFIX = "x-anthropic-billing-header:";
502
+
503
+ function createClaudeBillingHeader(firstUserMessageText: string): string {
504
+ // Fingerprint: SHA256(salt + msg[4] + msg[7] + msg[20] + version)[:3]
505
+ // Matches CC's computeFingerprint in utils/fingerprint.ts.
506
+ // Uses chars from the first user message (not the system prompt).
507
+ const k = [4, 7, 20].map(i => firstUserMessageText[i] ?? "0").join("");
508
+ const versionSuffix = nodeCrypto
509
+ .createHash("sha256")
510
+ .update(`59cf53e54c78${k}${claudeCodeVersion}`)
511
+ .digest("hex")
512
+ .slice(0, 3);
513
+ // cch=00000: placeholder replaced with the real attestation hash by wrapFetchForCch
514
+ // before the request hits the wire (see below).
515
+ return `${CLAUDE_BILLING_HEADER_PREFIX} cc_version=${claudeCodeVersion}.${versionSuffix}; cc_entrypoint=local-agent; ${CCH_PLACEHOLDER_STR};`;
516
+ }
517
+
518
+ // cch attestation: XXHash64(body_with_placeholder, seed) low-20-bits, 5 hex chars.
519
+ const CCH_SEED = 0x4d659218e32a3268n;
520
+ const CCH_PLACEHOLDER_STR = "cch=00000";
521
+ const cchEncoder = new TextEncoder();
522
+ const CCH_PLACEHOLDER = cchEncoder.encode(CCH_PLACEHOLDER_STR);
523
+ // Combined anchor for the billing-header placeholder inside system[0].
524
+ // "system":[{"type":"text","text":"x-anthropic-billing-header:
525
+ // Matches the exact JSON prefix of the first system block when
526
+ // createClaudeBillingHeader injects system[0]. "messages" serializes before
527
+ // "system" in Anthropic SDK payloads (~byte 29 vs ~byte 4705), so user content
528
+ // in the messages array can never match this sequence. User system prompt text
529
+ // lives in system[2] and therefore also cannot match.
530
+ const BILLING_SYSTEM_MARKER = cchEncoder.encode(`"system":[{"type":"text","text":"${CLAUDE_BILLING_HEADER_PREFIX}`);
531
+ const CCH_BILLING_SEARCH_WINDOW = 150;
532
+
533
+ function patchCch(body: Uint8Array): "patched" | "no-billing-header" | "unanchored" {
534
+ // Zero-copy Buffer view over the same memory; its `indexOf` is a native memmem,
535
+ // ~7.5x faster than a hand-rolled byte loop here — the marker sits ~99% through
536
+ // the body because `messages` serializes before `system`, so a JS scan would
537
+ // walk almost the entire payload (benchmarked: 563µs -> 75µs on a 1MB body).
538
+ const view = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
539
+
540
+ // Find the combined system[0] + billing-header prefix marker.
541
+ const markerIdx = view.indexOf(BILLING_SYSTEM_MARKER);
542
+ if (markerIdx === -1) return "no-billing-header"; // no CC billing header injected
543
+
544
+ // Placeholder must sit within CCH_BILLING_SEARCH_WINDOW bytes after the marker.
545
+ const searchFrom = markerIdx + BILLING_SYSTEM_MARKER.length;
546
+ const idx = view.indexOf(CCH_PLACEHOLDER, searchFrom);
547
+ if (idx === -1 || idx - searchFrom > CCH_BILLING_SEARCH_WINDOW) return "unanchored";
548
+
549
+ // Hash the body with the placeholder in place (matches CC's in-place behaviour).
550
+ const h = Bun.hash.xxHash64(body, CCH_SEED);
551
+ const cch = (h & 0xfffffn).toString(16).padStart(5, "0");
552
+
553
+ for (let i = 0; i < 5; i++) body[idx + 4 + i] = cch.charCodeAt(i);
554
+ return "patched";
555
+ }
556
+
557
+ /**
558
+ * Wraps a fetch implementation to patch the Claude Code billing-header `cch`
559
+ * attestation into outgoing request bodies. Bodies without the placeholder
560
+ * pass through untouched, so installing it on every OAuth flow is safe.
561
+ */
562
+ export function wrapFetchForCch(base: FetchImpl): FetchImpl {
563
+ return (input, init) => {
564
+ if (init?.body && typeof init.body === "string" && init.body.includes(CCH_PLACEHOLDER_STR)) {
565
+ const encoded = cchEncoder.encode(init.body);
566
+ if (patchCch(encoded) === "unanchored") {
567
+ // The OAuth billing placeholder is anchored to system[0] but we couldn't
568
+ // patch it — e.g. an `onPayload` hook reordered the first system block's keys
569
+ // so BILLING_SYSTEM_MARKER no longer matches. Send the body as-is (cch stays
570
+ // `00000`, the prior behaviour) rather than failing the request, but surface the
571
+ // fingerprint regression instead of letting it ship silently. A `cch=00000`
572
+ // literal in user content alone ("no-billing-header") is not a regression.
573
+ logger.warn("anthropic: cch billing placeholder present but not patched; sending unattested request");
574
+ }
575
+ return base(input, { ...init, body: encoded });
576
+ }
577
+ return base(input, init);
578
+ };
579
+ }
580
+
581
+ const CLAUDE_CLOAKING_USER_ID_REGEX =
582
+ /^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}$/;
583
+
584
+ export function isClaudeCloakingUserId(userId: string): boolean {
585
+ return CLAUDE_CLOAKING_USER_ID_REGEX.test(userId);
586
+ }
587
+
588
+ /**
589
+ * Real Claude Code sends `metadata.user_id` as a JSON-stringified object of the
590
+ * shape `{ device_id, account_uuid, session_id, ...extra }` (see
591
+ * services/api/claude.ts → getAPIMetadata). Accept that shape so callers that
592
+ * supply a stable `session_id` aren't silently overwritten with fresh entropy
593
+ * on every request, which would inflate the backend session count.
594
+ */
595
+ function isClaudeJsonUserId(userId: string): boolean {
596
+ if (userId.length === 0 || userId[0] !== "{") return false;
597
+ let parsed: unknown;
598
+ try {
599
+ parsed = JSON.parse(userId);
600
+ } catch {
601
+ return false;
602
+ }
603
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return false;
604
+ const obj = parsed as Record<string, unknown>;
605
+ return typeof obj.session_id === "string" && obj.session_id.length > 0;
606
+ }
607
+
608
+ function extractClaudeMetadataSessionId(userId: unknown): string | undefined {
609
+ if (typeof userId !== "string") return undefined;
610
+ if (isClaudeCloakingUserId(userId)) {
611
+ return userId.slice(userId.lastIndexOf("_session_") + "_session_".length);
612
+ }
613
+ if (userId.length === 0 || userId[0] !== "{") return undefined;
614
+ let parsed: unknown;
615
+ try {
616
+ parsed = JSON.parse(userId);
617
+ } catch {
618
+ return undefined;
619
+ }
620
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return undefined;
621
+ const sessionId = (parsed as Record<string, unknown>).session_id;
622
+ return typeof sessionId === "string" && sessionId.length > 0 ? sessionId : undefined;
623
+ }
624
+
625
+ export function generateClaudeCloakingUserId(): string {
626
+ const userHash = nodeCrypto.randomBytes(32).toString("hex");
627
+ const accountId = nodeCrypto.randomUUID().toLowerCase();
628
+ const sessionId = nodeCrypto.randomUUID().toLowerCase();
629
+ return `user_${userHash}_account_${accountId}_session_${sessionId}`;
630
+ }
631
+
632
+ const CLAUDE_DEVICE_ID_INSTALL_HASH_DOMAIN = "omp-claude-device-id-v1:";
633
+ const CLAUDE_DEVICE_ID_ACCOUNT_HASH_DOMAIN = "omp-claude-device-id-v2";
634
+
635
+ export function deriveClaudeDeviceId(installId: string, accountId?: string): string {
636
+ const hash = nodeCrypto.createHash("sha256");
637
+ if (accountId && accountId.length > 0) {
638
+ return hash
639
+ .update(CLAUDE_DEVICE_ID_ACCOUNT_HASH_DOMAIN)
640
+ .update("\0")
641
+ .update(installId)
642
+ .update("\0")
643
+ .update(accountId)
644
+ .digest("hex");
645
+ }
646
+ return hash.update(CLAUDE_DEVICE_ID_INSTALL_HASH_DOMAIN).update(installId).digest("hex");
647
+ }
648
+
649
+ function readMetadataString(metadata: Record<string, unknown> | undefined, key: string): string | undefined {
650
+ const value = metadata?.[key];
651
+ return typeof value === "string" && value.length > 0 ? value : undefined;
652
+ }
653
+
654
+ function readAnthropicMetadataAccountId(metadata: Record<string, unknown> | undefined): string | undefined {
655
+ return (
656
+ readMetadataString(metadata, "account_uuid") ??
657
+ readMetadataString(metadata, "accountId") ??
658
+ readMetadataString(metadata, "account_id")
659
+ );
660
+ }
661
+
662
+ function deriveClaudeDeviceIdFromInstallId(accountId?: string): string {
663
+ return deriveClaudeDeviceId(getInstallId(), accountId);
664
+ }
665
+
666
+ function generateClaudeJsonUserId(sessionId?: string, accountId?: string): string {
667
+ const userId: Record<string, string> = {
668
+ device_id: deriveClaudeDeviceIdFromInstallId(accountId),
669
+ session_id: sessionId ?? nodeCrypto.randomUUID().toLowerCase(),
670
+ };
671
+ if (accountId && accountId.length > 0) userId.account_uuid = accountId;
672
+ return JSON.stringify(userId);
673
+ }
674
+
675
+ /**
676
+ * Resolve the `metadata.user_id` field for an Anthropic Messages request.
677
+ *
678
+ * For API-key tokens, an explicit caller-supplied `userId` is forwarded
679
+ * verbatim and `undefined` yields no metadata. For OAuth tokens the value
680
+ * must match the Claude Code attribution shape (`isClaudeCloakingUserId` or
681
+ * the `{session_id, account_uuid?, device_id?}` JSON envelope) — anything
682
+ * else is dropped and a fresh Claude-Code-style JSON id is generated from
683
+ * `sessionId`/`accountId` so attribution stays consistent across the main
684
+ * streaming path and provider-specific request builders (e.g. web search).
685
+ */
686
+ export function resolveAnthropicMetadataUserId(
687
+ userId: unknown,
688
+ isOAuthToken: boolean,
689
+ sessionId?: string,
690
+ accountId?: string,
691
+ ): string | undefined {
692
+ if (typeof userId === "string") {
693
+ if (!isOAuthToken || isClaudeCloakingUserId(userId) || isClaudeJsonUserId(userId)) {
694
+ return userId;
695
+ }
696
+ }
697
+
698
+ if (!isOAuthToken) return undefined;
699
+ return generateClaudeJsonUserId(sessionId, accountId);
700
+ }
701
+ const ANTHROPIC_BUILTIN_TOOL_NAMES = new Set(["web_search", "code_execution", "text_editor", "computer"]);
702
+ const UMANS_WEBSEARCH_PROVIDER_HEADER = "X-Umans-Websearch-Provider";
703
+ const UMANS_WEBSEARCH_TOOL_NAME = "web_search";
704
+ export const applyClaudeToolPrefix = (name: string): string => {
705
+ if (!claudeToolPrefix) return name;
706
+ if (ANTHROPIC_BUILTIN_TOOL_NAMES.has(name.toLowerCase())) return name;
707
+ // Always prepend (no "already prefixed" short-circuit): the prefix is a wire
708
+ // transport detail applied once to internal tool names, and `stripClaudeToolPrefix`
709
+ // removes exactly one prefix on receive. Skipping names that already start with the
710
+ // prefix would make a tool literally named `_foo` lose its leading underscore on the
711
+ // return trip (`_foo` → wire `_foo` → strip → `foo`), so the agent loop can't find it.
712
+ return `${claudeToolPrefix}${name}`;
713
+ };
714
+
715
+ export const stripClaudeToolPrefix = (name: string): string => {
716
+ if (!claudeToolPrefix) return name;
717
+ if (!name.toLowerCase().startsWith(claudeToolPrefix.toLowerCase())) return name;
718
+ return name.slice(claudeToolPrefix.length);
719
+ };
720
+
721
+ function normalizeUmansWebSearchProvider(value: string | undefined): "native" | "exa" | undefined {
722
+ const normalized = value?.trim().toLowerCase();
723
+ return normalized === "native" || normalized === "exa" ? normalized : undefined;
724
+ }
725
+
726
+ function getUmansWebSearchProvider(headers: Record<string, string> | undefined): "native" | "exa" | undefined {
727
+ const explicit = getHeaderCaseInsensitive(headers, UMANS_WEBSEARCH_PROVIDER_HEADER);
728
+ if (explicit !== undefined) return normalizeUmansWebSearchProvider(explicit);
729
+ return normalizeUmansWebSearchProvider($env.UMANS_WEBSEARCH_PROVIDER);
730
+ }
731
+
732
+ function isUmansAnthropicModel(model: Model<"anthropic-messages">): boolean {
733
+ return model.provider === "umans" || model.baseUrl.toLowerCase().includes("api.code.umans.ai");
734
+ }
735
+
736
+ function getUmansWebSearchHeader(
737
+ model: Model<"anthropic-messages">,
738
+ headers: Record<string, string> | undefined,
739
+ ): Record<string, string> | undefined {
740
+ if (!isUmansAnthropicModel(model)) return undefined;
741
+ const provider = getUmansWebSearchProvider(headers);
742
+ return provider ? { [UMANS_WEBSEARCH_PROVIDER_HEADER]: provider } : undefined;
743
+ }
744
+
745
+ function shouldUseUmansGatewayWebSearch(name: string, enabled: boolean): boolean {
746
+ return enabled && name.toLowerCase() === UMANS_WEBSEARCH_TOOL_NAME;
747
+ }
748
+
749
+ function encodeAnthropicToolName(
750
+ name: string,
751
+ isOAuthToken: boolean,
752
+ escapeBuiltinToolNames: boolean,
753
+ useUmansGatewayWebSearch = false,
754
+ ): string {
755
+ if (shouldUseUmansGatewayWebSearch(name, useUmansGatewayWebSearch)) return name;
756
+ if (escapeBuiltinToolNames) return `${claudeToolPrefix}${name}`;
757
+ return isOAuthToken ? applyClaudeToolPrefix(name) : name;
758
+ }
759
+
760
+ function decodeAnthropicToolName(name: string, isOAuthToken: boolean, escapeBuiltinToolNames: boolean): string {
761
+ if (isOAuthToken || escapeBuiltinToolNames) return stripClaudeToolPrefix(name);
762
+ return name;
763
+ }
764
+
765
+ const ANTHROPIC_MANY_IMAGE_THRESHOLD = 20;
766
+ const ANTHROPIC_MANY_IMAGE_MAX_DIMENSION = 2000;
767
+
768
+ function countAnthropicImageBlocks(messages: Message[]): number {
769
+ let count = 0;
770
+ for (const message of messages) {
771
+ if (message.role !== "user" && message.role !== "developer" && message.role !== "toolResult") continue;
772
+ if (!Array.isArray(message.content)) continue;
773
+ for (const block of message.content) {
774
+ if (block.type === "image") count++;
775
+ }
776
+ }
777
+ return count;
778
+ }
779
+
780
+ const ANTHROPIC_IMAGE_RESIZE_CONCURRENCY = 4;
781
+
782
+ /**
783
+ * Memoized resize results keyed on ImageContent identity. Callers keep message
784
+ * objects stable across turns, so without this every request (and every
785
+ * in-provider retry of a fresh turn) re-decodes and re-encodes the same
786
+ * oversized screenshots. A cached value identical to the key means "already
787
+ * within bounds / unresizable — skip the decode".
788
+ */
789
+ const anthropicManyImageResizeCache = new WeakMap<ImageContent, ImageContent>();
790
+
791
+ type ResizeLimiter = <R>(fn: () => Promise<R>) => Promise<R>;
792
+
793
+ /**
794
+ * Bounded-concurrency gate for image decode/encode work. The many-image path
795
+ * fans out over every block of every message; unbounded, 100+ oversized images
796
+ * would decode concurrently (two encode pipelines each) and spike memory by
797
+ * gigabytes. Slots are handed off directly to the next waiter on release.
798
+ */
799
+ function createResizeLimiter(limit: number): ResizeLimiter {
800
+ let active = 0;
801
+ const queue: (() => void)[] = [];
802
+ return async fn => {
803
+ if (active >= limit) {
804
+ const { promise, resolve } = Promise.withResolvers<void>();
805
+ queue.push(resolve);
806
+ await promise;
807
+ } else {
808
+ active++;
809
+ }
810
+ try {
811
+ return await fn();
812
+ } finally {
813
+ const next = queue.shift();
814
+ if (next) next();
815
+ else active--;
816
+ }
817
+ };
818
+ }
819
+
820
+ async function resizeAnthropicManyImageBlock(block: ImageContent): Promise<ImageContent> {
821
+ try {
822
+ const inputBuffer = Buffer.from(block.data, "base64");
823
+ const { width, height } = await new Bun.Image(inputBuffer).metadata();
824
+ if (!width || !height) return block;
825
+ if (width <= ANTHROPIC_MANY_IMAGE_MAX_DIMENSION && height <= ANTHROPIC_MANY_IMAGE_MAX_DIMENSION) return block;
826
+
827
+ const scale = Math.min(ANTHROPIC_MANY_IMAGE_MAX_DIMENSION / width, ANTHROPIC_MANY_IMAGE_MAX_DIMENSION / height);
828
+ const targetWidth = Math.max(1, Math.min(ANTHROPIC_MANY_IMAGE_MAX_DIMENSION, Math.round(width * scale)));
829
+ const targetHeight = Math.max(1, Math.min(ANTHROPIC_MANY_IMAGE_MAX_DIMENSION, Math.round(height * scale)));
830
+
831
+ const [png, jpeg] = await Promise.all([
832
+ new Bun.Image(inputBuffer).resize(targetWidth, targetHeight).png().bytes(),
833
+ new Bun.Image(inputBuffer).resize(targetWidth, targetHeight).jpeg({ quality: 85 }).bytes(),
834
+ ]);
835
+ const best =
836
+ png.length <= jpeg.length ? { buffer: png, mimeType: "image/png" } : { buffer: jpeg, mimeType: "image/jpeg" };
837
+
838
+ return {
839
+ type: "image",
840
+ data: Buffer.from(best.buffer).toString("base64"),
841
+ mimeType: best.mimeType,
842
+ };
843
+ } catch (error) {
844
+ logger.warn("anthropic: failed to resize oversized image for many-image request", {
845
+ mimeType: block.mimeType,
846
+ error: error instanceof Error ? error.message : String(error),
847
+ });
848
+ return block;
849
+ }
850
+ }
851
+
852
+ async function resizeAnthropicManyImageContent(
853
+ content: (TextContent | ImageContent)[],
854
+ state: { resized: number },
855
+ limit: ResizeLimiter,
856
+ ): Promise<(TextContent | ImageContent)[]> {
857
+ let changed = false;
858
+ const next = await Promise.all(
859
+ content.map(async block => {
860
+ if (block.type !== "image") return block;
861
+ let resized = anthropicManyImageResizeCache.get(block);
862
+ if (resized === undefined) {
863
+ resized = await limit(() => resizeAnthropicManyImageBlock(block));
864
+ anthropicManyImageResizeCache.set(block, resized);
865
+ }
866
+ if (resized !== block) {
867
+ changed = true;
868
+ state.resized++;
869
+ }
870
+ return resized;
871
+ }),
872
+ );
873
+ return changed ? next : content;
874
+ }
875
+
876
+ async function resizeAnthropicManyImageMessage(
877
+ message: Message,
878
+ state: { resized: number },
879
+ limit: ResizeLimiter,
880
+ ): Promise<Message> {
881
+ if (message.role === "user" || message.role === "developer") {
882
+ if (!Array.isArray(message.content)) return message;
883
+ const content = await resizeAnthropicManyImageContent(message.content, state, limit);
884
+ return content === message.content ? message : { ...message, content };
885
+ }
886
+ if (message.role === "toolResult") {
887
+ const content = await resizeAnthropicManyImageContent(message.content, state, limit);
888
+ return content === message.content ? message : { ...message, content };
889
+ }
890
+ return message;
891
+ }
892
+
893
+ async function prepareAnthropicManyImageContext(context: Context, supportsImages: boolean): Promise<Context> {
894
+ if (!supportsImages) return context;
895
+ const imageCount = countAnthropicImageBlocks(context.messages);
896
+ if (imageCount <= ANTHROPIC_MANY_IMAGE_THRESHOLD) return context;
897
+
898
+ let changed = false;
899
+ const state = { resized: 0 };
900
+ const limit = createResizeLimiter(ANTHROPIC_IMAGE_RESIZE_CONCURRENCY);
901
+ const messages = await Promise.all(
902
+ context.messages.map(async message => {
903
+ const next = await resizeAnthropicManyImageMessage(message, state, limit);
904
+ if (next !== message) changed = true;
905
+ return next;
906
+ }),
907
+ );
908
+ if (!changed) return context;
909
+ logger.debug("anthropic: resized oversized images for many-image request", {
910
+ imageCount,
911
+ resized: state.resized,
912
+ maxDimension: ANTHROPIC_MANY_IMAGE_MAX_DIMENSION,
913
+ });
914
+ return { ...context, messages };
915
+ }
916
+
917
+ type AnthropicToolResultContent =
918
+ | string
919
+ | Array<
920
+ | { type: "text"; text: string }
921
+ | {
922
+ type: "image";
923
+ source: {
924
+ type: "base64";
925
+ media_type: AnthropicImageMediaType;
926
+ data: string;
927
+ };
928
+ }
929
+ >;
930
+
931
+ /**
932
+ * Convert content blocks to Anthropic API format
933
+ */
934
+ function convertContentBlocks(
935
+ content: (TextContent | ImageContent)[],
936
+ supportsImages = true,
937
+ ): AnthropicToolResultContent {
938
+ const blocks: Array<
939
+ | { type: "text"; text: string }
940
+ | {
941
+ type: "image";
942
+ source: {
943
+ type: "base64";
944
+ media_type: AnthropicImageMediaType;
945
+ data: string;
946
+ };
947
+ }
948
+ > = [];
949
+ let sawText = false;
950
+ let sawImage = false;
951
+
952
+ for (const block of content) {
953
+ if (block.type === "text") {
954
+ const text = block.text.toWellFormed();
955
+ if (text.trim().length === 0) continue;
956
+ sawText = true;
957
+ blocks.push({ type: "text", text });
958
+ continue;
959
+ }
960
+
961
+ if (!supportsImages) {
962
+ blocks.push({ type: "text", text: NON_VISION_IMAGE_PLACEHOLDER });
963
+ continue;
964
+ }
965
+
966
+ const mediaType = normalizeAnthropicImageMediaType(block.mimeType);
967
+ if (!mediaType) {
968
+ blocks.push({ type: "text", text: `[unsupported image: ${block.mimeType}]` });
969
+ continue;
970
+ }
971
+
972
+ sawImage = true;
973
+ blocks.push({
974
+ type: "image",
975
+ source: {
976
+ type: "base64",
977
+ media_type: mediaType,
978
+ data: block.data,
979
+ },
980
+ });
981
+ }
982
+
983
+ if (!supportsImages) {
984
+ return blocks
985
+ .filter((block): block is { type: "text"; text: string } => block.type === "text")
986
+ .map(block => block.text)
987
+ .join("\n")
988
+ .toWellFormed();
989
+ }
990
+
991
+ if (sawImage && !sawText) {
992
+ blocks.unshift({
993
+ type: "text",
994
+ text: "(see attached image)",
995
+ });
996
+ }
997
+
998
+ return blocks;
999
+ }
1000
+
1001
+ export type AnthropicOutputEffort = "low" | "medium" | "high" | "xhigh" | "max";
1002
+ export type AnthropicEffort = AnthropicOutputEffort | "adaptive";
1003
+ export type AnthropicThinkingDisplay = "summarized" | "omitted";
1004
+
1005
+ export interface AnthropicOptions extends StreamOptions {
1006
+ /**
1007
+ * Enable extended thinking.
1008
+ * For adaptive-capable models (Opus 4.6+, Sonnet 4.6+, Fable/Mythos 5):
1009
+ * uses adaptive thinking (Claude decides when/how much to think). For older
1010
+ * models: uses budget-based thinking with thinkingBudgetTokens.
1011
+ */
1012
+ thinkingEnabled?: boolean;
1013
+ /**
1014
+ * Token budget for extended thinking (older models only).
1015
+ * Ignored for adaptive-capable models.
1016
+ */
1017
+ thinkingBudgetTokens?: number;
1018
+ /**
1019
+ * Upstream wire model id override for collapsed effort-tier variants.
1020
+ * Serialized as `requestModelId ?? model.requestModelId ?? model.id`.
1021
+ */
1022
+ requestModelId?: string;
1023
+ /**
1024
+ * Effort level for adaptive thinking.
1025
+ * Controls how much Claude allocates, or uses "adaptive" for MiniMax's
1026
+ * binary adaptive-thinking tag:
1027
+ * - "max": Always thinks with no constraints
1028
+ * - "high": Always thinks, deep reasoning (default)
1029
+ * - "medium": Moderate thinking, may skip for simple queries
1030
+ * - "low": Minimal thinking, skips for simple tasks
1031
+ * - "adaptive": Sends `thinking.type: "adaptive"` without `output_config.effort`
1032
+ * Ignored for older models.
1033
+ */
1034
+ effort?: AnthropicEffort;
1035
+ /**
1036
+ * Optional reasoning level fallback for direct Anthropic provider usage.
1037
+ * Converted to adaptive effort when effort is not explicitly provided.
1038
+ */
1039
+ reasoning?: SimpleStreamOptions["reasoning"];
1040
+ /**
1041
+ * Controls how Anthropic returns thinking content when the selected thinking
1042
+ * transport supports a display option. Defaults to "summarized" where the
1043
+ * API accepts it.
1044
+ */
1045
+ thinkingDisplay?: AnthropicThinkingDisplay;
1046
+ interleavedThinking?: boolean;
1047
+ toolChoice?: "auto" | "any" | "none" | { type: "tool"; name: string };
1048
+ betas?: string[] | string;
1049
+ /**
1050
+ * Realization of `serviceTier: "priority"` on Anthropic models. When
1051
+ * `"priority"`, sets `speed: "fast"` on the request and appends the
1052
+ * `fast-mode-2026-02-01` beta header. Anthropic rejects unsupported models
1053
+ * with `invalid_request_error`, which triggers an in-provider one-shot
1054
+ * fallback (see `fastModeDisabled` provider state).
1055
+ *
1056
+ * Other `ServiceTier` values are currently ignored on this provider.
1057
+ */
1058
+ serviceTier?: ServiceTier;
1059
+ /** Force OAuth bearer auth mode for proxy tokens that don't match Anthropic token prefixes. */
1060
+ isOAuth?: boolean;
1061
+ /**
1062
+ * Pre-built Anthropic Messages client. When provided, skips internal client
1063
+ * construction entirely. Accepts any structurally compatible client,
1064
+ * including SDK clients such as `AnthropicVertex`.
1065
+ */
1066
+ client?: AnthropicMessagesClientLike;
1067
+ /**
1068
+ * Server-side fallback beta chain (`server-side-fallback-2026-06-01`).
1069
+ * When set, `fallbacks` is forwarded on the request body and the beta
1070
+ * header is auto-attached; the response parser then honors mid-stream
1071
+ * `fallback` content blocks and `usage.iterations` for served-model
1072
+ * promotion and per-attempt pricing. Opt-in ONLY — leaving this
1073
+ * undefined preserves the pre-fallback behavior on every code path.
1074
+ */
1075
+ fallbacks?: FallbackParam[];
1076
+ }
1077
+
1078
+ export type AnthropicClientOptionsArgs = {
1079
+ model: Model<"anthropic-messages">;
1080
+ apiKey: string;
1081
+ extraBetas?: string[];
1082
+ stream?: boolean;
1083
+ interleavedThinking?: boolean;
1084
+ headers?: Record<string, string>;
1085
+ dynamicHeaders?: Record<string, string>;
1086
+ isOAuth?: boolean;
1087
+ hasTools?: boolean;
1088
+ thinkingEnabled?: boolean;
1089
+ thinkingDisplay?: AnthropicThinkingDisplay;
1090
+ fetch?: FetchImpl;
1091
+ claudeCodeSessionId?: string;
1092
+ };
1093
+
1094
+ export type AnthropicClientOptionsResult = {
1095
+ isOAuthToken: boolean;
1096
+ apiKey: string | null;
1097
+ authToken?: string | null;
1098
+ baseURL?: string;
1099
+ maxRetries: number;
1100
+ defaultHeaders: Record<string, string>;
1101
+ fetch?: FetchImpl;
1102
+ fetchOptions?: AnthropicFetchOptions;
1103
+ };
1104
+
1105
+ const CLAUDE_CODE_TLS_CIPHERS = tls.DEFAULT_CIPHERS;
1106
+
1107
+ type FoundryTlsOptions = {
1108
+ ca?: string | string[];
1109
+ cert?: string;
1110
+ key?: string;
1111
+ };
1112
+
1113
+ const foundryTlsOptionsCache = new Map<string, FoundryTlsOptions | undefined>();
1114
+
1115
+ function foundryTlsCacheKeyComponent(value: string | undefined): string | null {
1116
+ if (!value) return null;
1117
+ const trimmed = value.trim();
1118
+ // For path-valued vars, fold the file mtime into the key so on-disk cert
1119
+ // rotation (common for short-lived corporate mTLS certs) invalidates the
1120
+ // cached TLS options instead of pinning the first read forever.
1121
+ if (trimmed && !trimmed.includes("-----BEGIN") && looksLikeFilePath(trimmed)) {
1122
+ try {
1123
+ return `${trimmed}@${fs.statSync(trimmed).mtimeMs}`;
1124
+ } catch {
1125
+ return trimmed;
1126
+ }
1127
+ }
1128
+ return value;
1129
+ }
1130
+
1131
+ function foundryTlsOptionsCacheKey(): string {
1132
+ return JSON.stringify([
1133
+ foundryTlsCacheKeyComponent($env.NODE_EXTRA_CA_CERTS),
1134
+ foundryTlsCacheKeyComponent($env.CLAUDE_CODE_CLIENT_CERT),
1135
+ foundryTlsCacheKeyComponent($env.CLAUDE_CODE_CLIENT_KEY),
1136
+ ]);
1137
+ }
1138
+
1139
+ function resolveAnthropicBaseUrl(model: Model<"anthropic-messages">, apiKey?: string): string | undefined {
1140
+ if (model.provider === "github-copilot") {
1141
+ return normalizeAnthropicBaseUrl(resolveGitHubCopilotBaseUrl(model.baseUrl, apiKey) ?? model.baseUrl);
1142
+ }
1143
+ if (model.provider === "anthropic" && isFoundryEnabled()) {
1144
+ const foundryBaseUrl = normalizeAnthropicBaseUrl($env.FOUNDRY_BASE_URL);
1145
+ if (foundryBaseUrl) {
1146
+ return foundryBaseUrl;
1147
+ }
1148
+ }
1149
+ if (model.provider === "anthropic") {
1150
+ return normalizeAnthropicBaseUrl(model.baseUrl) ?? "https://api.anthropic.com";
1151
+ }
1152
+ return normalizeAnthropicBaseUrl(model.baseUrl);
1153
+ }
1154
+
1155
+ function parseAnthropicCustomHeaders(rawHeaders: string | undefined): Record<string, string> | undefined {
1156
+ const source = rawHeaders?.trim();
1157
+ if (!source) return undefined;
1158
+
1159
+ const parsed: Record<string, string> = {};
1160
+ for (const token of source.split(/\r?\n|,/)) {
1161
+ const entry = token.trim();
1162
+ if (!entry) continue;
1163
+ const separatorIndex = entry.indexOf(":");
1164
+ if (separatorIndex <= 0) continue;
1165
+ const key = entry.slice(0, separatorIndex).trim();
1166
+ const value = entry.slice(separatorIndex + 1).trim();
1167
+ if (!key || !value) continue;
1168
+ parsed[key] = value;
1169
+ }
1170
+
1171
+ return Object.keys(parsed).length > 0 ? parsed : undefined;
1172
+ }
1173
+
1174
+ /**
1175
+ * Returns env-supplied custom headers (`ANTHROPIC_CUSTOM_HEADERS`) when they
1176
+ * should be forwarded to the upstream endpoint.
1177
+ *
1178
+ * Foundry mode forwards them unconditionally. Outside Foundry, they're applied
1179
+ * only when the configured base URL is a non-Anthropic host — i.e. an
1180
+ * enterprise/corporate gateway that may require its own proprietary auth
1181
+ * header. Stock `api.anthropic.com` would reject unknown headers, so they're
1182
+ * omitted there.
1183
+ */
1184
+ export function resolveAnthropicCustomHeadersForBaseUrl(
1185
+ baseUrl: string | undefined,
1186
+ ): Record<string, string> | undefined {
1187
+ if (!isFoundryEnabled() && isOfficialAnthropicApiUrl(baseUrl)) return undefined;
1188
+ return parseAnthropicCustomHeaders($env.ANTHROPIC_CUSTOM_HEADERS);
1189
+ }
1190
+
1191
+ function resolveAnthropicCustomHeaders(model: Model<"anthropic-messages">): Record<string, string> | undefined {
1192
+ if (model.provider !== "anthropic") return undefined;
1193
+ return resolveAnthropicCustomHeadersForBaseUrl(model.baseUrl);
1194
+ }
1195
+
1196
+ function looksLikeFilePath(value: string): boolean {
1197
+ return value.includes("/") || value.includes("\\") || /\.(pem|crt|cer|key)$/i.test(value);
1198
+ }
1199
+
1200
+ function resolvePemValue(value: string | undefined, name: string): string | undefined {
1201
+ const trimmed = value?.trim();
1202
+ if (!trimmed) return undefined;
1203
+
1204
+ const inline = trimmed.replace(/\\n/g, "\n");
1205
+ if (inline.includes("-----BEGIN")) {
1206
+ return inline;
1207
+ }
1208
+
1209
+ if (looksLikeFilePath(trimmed)) {
1210
+ try {
1211
+ return fs.readFileSync(trimmed, "utf8");
1212
+ } catch (error) {
1213
+ if (isEnoent(error)) {
1214
+ throw new AIError.ValidationError(`${name} path does not exist: ${trimmed}`);
1215
+ }
1216
+ throw error;
1217
+ }
1218
+ }
1219
+
1220
+ return inline;
1221
+ }
1222
+
1223
+ function resolveFoundryTlsOptions(model: Model<"anthropic-messages">): FoundryTlsOptions | undefined {
1224
+ if (model.provider !== "anthropic") return undefined;
1225
+ if (!isFoundryEnabled()) return undefined;
1226
+
1227
+ const cacheKey = foundryTlsOptionsCacheKey();
1228
+ if (foundryTlsOptionsCache.has(cacheKey)) return foundryTlsOptionsCache.get(cacheKey);
1229
+
1230
+ const ca = resolvePemValue($env.NODE_EXTRA_CA_CERTS, "NODE_EXTRA_CA_CERTS");
1231
+ const cert = resolvePemValue($env.CLAUDE_CODE_CLIENT_CERT, "CLAUDE_CODE_CLIENT_CERT");
1232
+ const key = resolvePemValue($env.CLAUDE_CODE_CLIENT_KEY, "CLAUDE_CODE_CLIENT_KEY");
1233
+
1234
+ if ((cert && !key) || (!cert && key)) {
1235
+ throw new AIError.ConfigurationError(
1236
+ "Both CLAUDE_CODE_CLIENT_CERT and CLAUDE_CODE_CLIENT_KEY must be set for mTLS.",
1237
+ );
1238
+ }
1239
+
1240
+ const options: FoundryTlsOptions = {};
1241
+ if (ca) options.ca = [...tls.rootCertificates, ca];
1242
+ if (cert) options.cert = cert;
1243
+ if (key) options.key = key;
1244
+ const resolved = Object.keys(options).length > 0 ? options : undefined;
1245
+ foundryTlsOptionsCache.set(cacheKey, resolved);
1246
+ return resolved;
1247
+ }
1248
+
1249
+ function buildClaudeCodeTlsFetchOptions(
1250
+ model: Model<"anthropic-messages">,
1251
+ baseUrl: string | undefined,
1252
+ ): AnthropicFetchOptions | undefined {
1253
+ if (model.provider !== "anthropic") return undefined;
1254
+ if (!baseUrl) return undefined;
1255
+
1256
+ let serverName: string;
1257
+ try {
1258
+ serverName = new URL(baseUrl).hostname;
1259
+ } catch {
1260
+ return undefined;
1261
+ }
1262
+
1263
+ if (!serverName) return undefined;
1264
+
1265
+ const foundryTlsOptions = resolveFoundryTlsOptions(model);
1266
+
1267
+ return {
1268
+ tls: {
1269
+ rejectUnauthorized: true,
1270
+ serverName,
1271
+ ...(CLAUDE_CODE_TLS_CIPHERS ? { ciphers: CLAUDE_CODE_TLS_CIPHERS } : {}),
1272
+ ...(foundryTlsOptions ?? {}),
1273
+ },
1274
+ };
1275
+ }
1276
+ function mergeHeaders(...headerSources: (Record<string, string> | undefined)[]): Record<string, string> {
1277
+ // Case-insensitive merge: later sources win and keep their casing. A plain
1278
+ // Object.assign would let `authorization` and `Authorization` coexist, and
1279
+ // the Headers constructor then joins both values comma-separated on the wire.
1280
+ const merged: Record<string, string> = {};
1281
+ const keyByLower = new Map<string, string>();
1282
+ for (const headers of headerSources) {
1283
+ if (!headers) continue;
1284
+ for (const [key, value] of Object.entries(headers)) {
1285
+ const lower = key.toLowerCase();
1286
+ const existing = keyByLower.get(lower);
1287
+ if (existing !== undefined && existing !== key) delete merged[existing];
1288
+ keyByLower.set(lower, key);
1289
+ merged[key] = value;
1290
+ }
1291
+ }
1292
+ return merged;
1293
+ }
1294
+
1295
+ const ANTHROPIC_MESSAGE_EVENTS: ReadonlySet<string> = new Set([
1296
+ "message_start",
1297
+ "message_delta",
1298
+ "message_stop",
1299
+ "content_block_start",
1300
+ "content_block_delta",
1301
+ "content_block_stop",
1302
+ ]);
1303
+
1304
+ /**
1305
+ * Iterate over Anthropic SSE events from a raw Response, preserving ping events
1306
+ * for liveness. Malformed event envelopes are logged and skipped (non-fatal)
1307
+ * rather than aborting the stream.
1308
+ */
1309
+ type RawMessagePingEvent = { type: "ping" };
1310
+ type AnthropicStreamEvent = RawMessageStreamEvent | RawMessagePingEvent;
1311
+ const ANTHROPIC_PING_EVENT: RawMessagePingEvent = { type: "ping" };
1312
+
1313
+ /**
1314
+ * In-stream `error` SSE frames carry an Anthropic error envelope:
1315
+ * `{"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}`.
1316
+ * Surface the structured type + message instead of the raw JSON blob; the
1317
+ * error type token (e.g. `overloaded_error`, `rate_limit_error`) is kept in
1318
+ * the message so `isProviderRetryableError`'s classification keys off the
1319
+ * structured type rather than incidental JSON substrings.
1320
+ */
1321
+ function createAnthropicSseStreamError(data: string): Error {
1322
+ try {
1323
+ const parsed = JSON.parse(data) as { error?: { type?: unknown; message?: unknown } };
1324
+ const errorType = typeof parsed?.error?.type === "string" ? parsed.error.type : undefined;
1325
+ const message = typeof parsed?.error?.message === "string" ? parsed.error.message : undefined;
1326
+ if (message) {
1327
+ return new AIError.ProviderResponseError(
1328
+ errorType ? `Anthropic stream error (${errorType}): ${message}` : `Anthropic stream error: ${message}`,
1329
+ { provider: "anthropic", kind: "output" },
1330
+ );
1331
+ }
1332
+ } catch {
1333
+ // Not a JSON envelope; fall through to the raw payload.
1334
+ }
1335
+ return new AIError.ProviderResponseError(data, { provider: "anthropic", kind: "output" });
1336
+ }
1337
+
1338
+ async function* iterateAnthropicEvents(
1339
+ response: Response,
1340
+ signal?: AbortSignal,
1341
+ onSseEvent?: AnthropicOptions["onSseEvent"],
1342
+ ): AsyncGenerator<AnthropicStreamEvent> {
1343
+ if (!response.body) {
1344
+ throw new AIError.AnthropicStreamEnvelopeError("Attempted to iterate over an Anthropic response with no body");
1345
+ }
1346
+
1347
+ let sawMessageStart = false;
1348
+ let sawMessageEnd = false;
1349
+
1350
+ for await (const sse of readSseEvents(response.body, signal)) {
1351
+ notifyRawSseEvent(onSseEvent, sse);
1352
+ if (sse.event === "error") {
1353
+ throw createAnthropicSseStreamError(sse.data);
1354
+ }
1355
+
1356
+ if (sse.event === "ping") {
1357
+ // Surface keepalives so the idle watchdog treats them as liveness.
1358
+ yield ANTHROPIC_PING_EVENT;
1359
+ continue;
1360
+ }
1361
+
1362
+ if (!ANTHROPIC_MESSAGE_EVENTS.has(sse.event ?? "")) {
1363
+ continue;
1364
+ }
1365
+
1366
+ try {
1367
+ const event = JSON.parse(sse.data) as RawMessageStreamEvent;
1368
+ if (event.type !== sse.event) {
1369
+ reportAnthropicEnvelopeAnomaly(`event type ${event.type} does not match SSE event ${sse.event}`);
1370
+ }
1371
+ if (event.type === "message_start") {
1372
+ sawMessageStart = true;
1373
+ } else if (event.type === "message_stop") {
1374
+ sawMessageEnd = true;
1375
+ }
1376
+ yield event;
1377
+ } catch (error) {
1378
+ const message = error instanceof Error ? error.message : String(error);
1379
+ reportAnthropicEnvelopeAnomaly(
1380
+ `could not parse SSE event ${sse.event}: ${message}; skipping frame; data=${sse.data}`,
1381
+ );
1382
+ }
1383
+ }
1384
+
1385
+ if (sawMessageStart && !sawMessageEnd && !signal?.aborted) {
1386
+ reportAnthropicEnvelopeAnomaly("stream ended before message_stop");
1387
+ }
1388
+ }
1389
+
1390
+ type AnthropicRawResponseRequest = {
1391
+ asResponse(): Promise<Response>;
1392
+ };
1393
+
1394
+ function hasAnthropicRawResponseRequest(request: unknown): request is AnthropicRawResponseRequest {
1395
+ return isRecord(request) && typeof request.asResponse === "function";
1396
+ }
1397
+
1398
+ type AnthropicStreamWithResponseRequest = {
1399
+ withResponse(): Promise<{
1400
+ data: AsyncIterable<RawMessageStreamEvent>;
1401
+ response: Response;
1402
+ request_id: string | null;
1403
+ }>;
1404
+ };
1405
+
1406
+ function hasAnthropicStreamWithResponseRequest(request: unknown): request is AnthropicStreamWithResponseRequest {
1407
+ return isRecord(request) && typeof request.withResponse === "function";
1408
+ }
1409
+
1410
+ async function getAnthropicStreamResponse(
1411
+ request: unknown,
1412
+ signal?: AbortSignal,
1413
+ onSseEvent?: AnthropicOptions["onSseEvent"],
1414
+ ): Promise<{
1415
+ events: AsyncIterable<AnthropicStreamEvent>;
1416
+ response: Response;
1417
+ requestId: string | null;
1418
+ recordsRawSseEvents: boolean;
1419
+ }> {
1420
+ if (hasAnthropicRawResponseRequest(request)) {
1421
+ const response = await request.asResponse();
1422
+ return {
1423
+ events: iterateAnthropicEvents(response, signal, onSseEvent),
1424
+ response,
1425
+ requestId: response.headers.get("request-id"),
1426
+ recordsRawSseEvents: true,
1427
+ };
1428
+ }
1429
+ if (hasAnthropicStreamWithResponseRequest(request)) {
1430
+ const { data, response, request_id } = await request.withResponse();
1431
+ return { events: data, response, requestId: request_id, recordsRawSseEvents: false };
1432
+ }
1433
+ throw new AIError.AnthropicStreamEnvelopeError("Anthropic SDK request did not expose a stream response");
1434
+ }
1435
+
1436
+ async function* observeDecodedAnthropicSdkEvents(
1437
+ events: AsyncIterable<AnthropicStreamEvent>,
1438
+ observer: (event: RawSseEvent) => void,
1439
+ ): AsyncGenerator<AnthropicStreamEvent> {
1440
+ for await (const event of events) {
1441
+ const data = JSON.stringify(event);
1442
+ // Reconstructed from decoded SDK event; not literal wire bytes.
1443
+ notifyRawSseEvent(observer, { event: event.type, data, raw: [`event: ${event.type}`, `data: ${data}`] });
1444
+ yield event;
1445
+ }
1446
+ }
1447
+
1448
+ const PROVIDER_MAX_RETRIES = 10;
1449
+
1450
+ /**
1451
+ * Log a malformed-stream-envelope anomaly without aborting the turn. The strict
1452
+ * parser would `throw new AnthropicStreamEnvelopeError(...)` here; we instead
1453
+ * surface a warning and let the caller skip the offending event (or finalize what
1454
+ * already streamed) so a non-conforming endpoint degrades to best-effort content
1455
+ * rather than failing the request.
1456
+ */
1457
+ function reportAnthropicEnvelopeAnomaly(detail: string): void {
1458
+ logger.warn(`anthropic: ignoring malformed stream envelope: ${detail}`);
1459
+ }
1460
+
1461
+ function shouldIgnoreAnthropicPreambleEvent(eventType: unknown): boolean {
1462
+ if (typeof eventType !== "string") return false;
1463
+ if (eventType === "ping") return true;
1464
+ return !ANTHROPIC_MESSAGE_EVENTS.has(eventType);
1465
+ }
1466
+
1467
+ /**
1468
+ * Whether an Anthropic (or Copilot-over-Anthropic) stream error should be
1469
+ * retried. The classification lives in {@link AIError.isProviderRetryableError};
1470
+ * this wrapper injects the Copilot-specific `model_not_supported` transient
1471
+ * check, which the error module must not import directly.
1472
+ */
1473
+ export function isProviderRetryableError(error: unknown, provider?: string): boolean {
1474
+ return AIError.isProviderRetryableError(error, {
1475
+ provider,
1476
+ isProviderTransient:
1477
+ provider === "github-copilot" ? (err): boolean => AIError.isCopilotTransientModelError(err) : undefined,
1478
+ });
1479
+ }
1480
+
1481
+ const THINKING_ENVELOPE_OPEN = "<thinking>";
1482
+ const THINKING_ENVELOPE_CLOSE = "</thinking>";
1483
+
1484
+ function unwrapAnthropicThinkingEnvelope(text: string): string | undefined {
1485
+ let current = text.trim();
1486
+ let stripped = false;
1487
+ while (current.startsWith(THINKING_ENVELOPE_OPEN) && current.endsWith(THINKING_ENVELOPE_CLOSE)) {
1488
+ current = current.slice(THINKING_ENVELOPE_OPEN.length, current.length - THINKING_ENVELOPE_CLOSE.length).trim();
1489
+ stripped = true;
1490
+ }
1491
+ return stripped ? current : undefined;
1492
+ }
1493
+
1494
+ function createEmptyUsage(premiumRequests?: number): Usage {
1495
+ return {
1496
+ input: 0,
1497
+ output: 0,
1498
+ cacheRead: 0,
1499
+ cacheWrite: 0,
1500
+ totalTokens: 0,
1501
+ ...(premiumRequests === undefined ? {} : { premiumRequests }),
1502
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
1503
+ };
1504
+ }
1505
+
1506
+ export type AnthropicUsageLike = {
1507
+ cache_creation?: { ephemeral_5m_input_tokens?: number | null; ephemeral_1h_input_tokens?: number | null } | null;
1508
+ server_tool_use?: { web_search_requests?: number | null; web_fetch_requests?: number | null } | null;
1509
+ };
1510
+
1511
+ /**
1512
+ * Capture Anthropic's optional cache-creation TTL breakdown and server-tool-use
1513
+ * counters into the harness Usage shape. Omitted/null fields are no-ops; explicit
1514
+ * zero-valued objects clear prior extras from earlier stream usage snapshots.
1515
+ */
1516
+ export function applyAnthropicUsageExtras(usage: Usage, source: AnthropicUsageLike): void {
1517
+ const cacheCreation = source.cache_creation;
1518
+ if (cacheCreation != null) {
1519
+ const fiveMinute = cacheCreation.ephemeral_5m_input_tokens ?? 0;
1520
+ const oneHour = cacheCreation.ephemeral_1h_input_tokens ?? 0;
1521
+ if (fiveMinute > 0 || oneHour > 0) {
1522
+ usage.cttl = {
1523
+ ...(fiveMinute > 0 ? { ephemeral5m: fiveMinute } : {}),
1524
+ ...(oneHour > 0 ? { ephemeral1h: oneHour } : {}),
1525
+ };
1526
+ } else {
1527
+ delete usage.cttl;
1528
+ }
1529
+ }
1530
+ const serverToolUse = source.server_tool_use;
1531
+ if (serverToolUse != null) {
1532
+ const webSearch = serverToolUse.web_search_requests ?? 0;
1533
+ const webFetch = serverToolUse.web_fetch_requests ?? 0;
1534
+ if (webSearch > 0 || webFetch > 0) {
1535
+ usage.server = {
1536
+ ...(webSearch > 0 ? { webSearch } : {}),
1537
+ ...(webFetch > 0 ? { webFetch } : {}),
1538
+ };
1539
+ } else {
1540
+ delete usage.server;
1541
+ }
1542
+ }
1543
+ }
1544
+
1545
+ function parseAnthropicFallbackWireBlock(value: unknown): AnthropicFallbackContent | undefined {
1546
+ if (!isRecord(value) || value.type !== "fallback") return undefined;
1547
+ const from = isRecord(value.from) && typeof value.from.model === "string" ? value.from.model : undefined;
1548
+ const to = isRecord(value.to) && typeof value.to.model === "string" ? value.to.model : undefined;
1549
+ if (!from?.trim() || !to?.trim()) return undefined;
1550
+ return { type: "fallback", from: { model: from }, to: { model: to } };
1551
+ }
1552
+
1553
+ /**
1554
+ * The definitive "served by fallback" signal per Anthropic's fallback
1555
+ * billing cookbook (§4): a `fallback_message` iteration in `usage.iterations`.
1556
+ * Any other iteration type is per-attempt bookkeeping for the requested model
1557
+ * (including its dated snapshot alias) and MUST NOT retag the assistant turn.
1558
+ */
1559
+ function fallbackServedModelFromUsage(source: AnthropicWireUsage): string | undefined {
1560
+ const iterations = source.iterations ?? [];
1561
+ for (let index = iterations.length - 1; index >= 0; index -= 1) {
1562
+ const iteration = iterations[index];
1563
+ if (iteration?.type === "fallback_message" && iteration.model?.trim()) return iteration.model;
1564
+ }
1565
+ return undefined;
1566
+ }
1567
+
1568
+ /**
1569
+ * Price a fallback turn per the fallback billing cookbook §4:
1570
+ * • A pre-served attempt with zero output/cache-creation is not billed
1571
+ * (waived classifier block); its iteration is skipped.
1572
+ * • Mid-stream refusals bill their attempting model's input+output at
1573
+ * that model's normal rates.
1574
+ * • The `fallback_message` attempt's input tokens are rebilled at the
1575
+ * served model's cache-read rate (fallback credit — 10% of base input).
1576
+ *
1577
+ * Top-level `usage.input/output/cacheRead/cacheWrite` stay Anthropic's raw
1578
+ * served-attempt counts; `usage.cost` reflects the per-iteration attributed
1579
+ * total. Non-fallback turns skip this path entirely and use the requested
1580
+ * model at the normal `calculateCost` call.
1581
+ */
1582
+ /**
1583
+ * Resolve a served/iteration model id to its bundled catalog entry when
1584
+ * possible so the per-iteration cost uses the served model's pricing
1585
+ * (e.g. Opus 4.8 rates for a Fable→Opus fallback). Falls back to
1586
+ * `requestModel` when the id is empty, matches the request, or the
1587
+ * catalog has no entry under it — the caller keeps the requested-model
1588
+ * pricing as the safe default and logs at the source.
1589
+ */
1590
+ function resolveIterationModel(
1591
+ requestModel: Model<"anthropic-messages">,
1592
+ iterationModelId: string | null | undefined,
1593
+ ): Model<Api> {
1594
+ const id = iterationModelId?.trim();
1595
+ if (!id || id === requestModel.id) return requestModel;
1596
+ // Bundled catalog lookup: only Anthropic provider entries are safe to
1597
+ // reference (dated snapshots resolve to their alias entry when present).
1598
+ if (requestModel.provider === "anthropic") {
1599
+ const bundled = getBundledModel("anthropic", id);
1600
+ if (bundled?.api === "anthropic-messages") return bundled;
1601
+ }
1602
+ return requestModel;
1603
+ }
1604
+
1605
+ function calculateFallbackTurnCost(
1606
+ requestModel: Model<"anthropic-messages">,
1607
+ usage: Usage,
1608
+ source: AnthropicWireUsage,
1609
+ ): boolean {
1610
+ const iterations = source.iterations ?? [];
1611
+ if (iterations.length === 0) return false;
1612
+ const cost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 };
1613
+ const hasFallbackMessage = iterations.some(iter => iter.type === "fallback_message");
1614
+ let applied = false;
1615
+ for (const iteration of iterations) {
1616
+ const inputTokens = iteration.input_tokens ?? 0;
1617
+ const outputTokens = iteration.output_tokens ?? 0;
1618
+ const cacheReadTokens = iteration.cache_read_input_tokens ?? 0;
1619
+ const cacheWriteTokens = iteration.cache_creation_input_tokens ?? 0;
1620
+ const isFallback = iteration.type === "fallback_message";
1621
+ if (hasFallbackMessage && !isFallback && outputTokens === 0 && cacheWriteTokens === 0) continue;
1622
+ const iterationUsage = createEmptyUsage();
1623
+ if (isFallback) {
1624
+ iterationUsage.input = 0;
1625
+ iterationUsage.cacheRead = cacheReadTokens + inputTokens;
1626
+ } else {
1627
+ iterationUsage.input = inputTokens;
1628
+ iterationUsage.cacheRead = cacheReadTokens;
1629
+ }
1630
+ iterationUsage.output = outputTokens;
1631
+ iterationUsage.cacheWrite = cacheWriteTokens;
1632
+ iterationUsage.totalTokens =
1633
+ iterationUsage.input + iterationUsage.output + iterationUsage.cacheRead + iterationUsage.cacheWrite;
1634
+ calculateCost(resolveIterationModel(requestModel, iteration.model), iterationUsage);
1635
+ cost.input += iterationUsage.cost.input;
1636
+ cost.output += iterationUsage.cost.output;
1637
+ cost.cacheRead += iterationUsage.cost.cacheRead;
1638
+ cost.cacheWrite += iterationUsage.cost.cacheWrite;
1639
+ cost.total += iterationUsage.cost.total;
1640
+ applied = true;
1641
+ }
1642
+ if (!applied) return false;
1643
+ usage.cost = cost;
1644
+ return true;
1645
+ }
1646
+
1647
+ const streamAnthropicOnce = (
1648
+ model: Model<"anthropic-messages">,
1649
+ context: Context,
1650
+ options?: AnthropicOptions,
1651
+ ): AssistantMessageEventStream => {
1652
+ const stream = new AssistantMessageEventStream();
1653
+
1654
+ (async () => {
1655
+ const startTime = performance.now();
1656
+ let firstTokenTime: number | undefined;
1657
+
1658
+ const output: AssistantMessage = {
1659
+ role: "assistant",
1660
+ content: [],
1661
+ api: model.api as Api,
1662
+ provider: model.provider,
1663
+ model: model.id,
1664
+ usage: createEmptyUsage(),
1665
+ stopReason: "stop",
1666
+ timestamp: Date.now(),
1667
+ };
1668
+ let rawRequestDump: RawHttpRequestDump | undefined;
1669
+ let activeAbortTracker = createAbortSourceTracker(options?.signal);
1670
+
1671
+ const onSseEvent = options?.onSseEvent;
1672
+ const rawSseObserver = onSseEvent ? (event: RawSseEvent) => onSseEvent(event, model) : undefined;
1673
+
1674
+ try {
1675
+ // Built inside the try so a copilot credential/header failure surfaces as
1676
+ // an error event instead of an unhandled rejection that leaves the stream
1677
+ // (and any consumer awaiting `result()`) hanging forever.
1678
+ const copilotDynamicHeaders =
1679
+ model.provider === "github-copilot"
1680
+ ? buildCopilotDynamicHeaders({
1681
+ messages: context.messages,
1682
+ hasImages: hasCopilotVisionInput(context.messages),
1683
+ premiumMultiplier: model.premiumMultiplier,
1684
+ headers: { ...(model.headers ?? {}), ...(options?.headers ?? {}) },
1685
+ initiatorOverride: options?.initiatorOverride,
1686
+ })
1687
+ : undefined;
1688
+ if (copilotDynamicHeaders?.premiumRequests !== undefined) {
1689
+ output.usage.premiumRequests = copilotDynamicHeaders.premiumRequests;
1690
+ }
1691
+ const apiKey = options?.apiKey ?? getEnvApiKey(model.provider) ?? "";
1692
+ const baseUrl = resolveAnthropicBaseUrl(model, apiKey) ?? "https://api.anthropic.com";
1693
+ const providerSessionState = getAnthropicProviderSessionState(
1694
+ options?.providerSessionState,
1695
+ baseUrl,
1696
+ model.id,
1697
+ );
1698
+ let disableStrictTools =
1699
+ (providerSessionState?.strictToolsDisabled ?? false) || (model.compat?.disableStrictTools ?? false);
1700
+ let dropFastMode = providerSessionState?.fastModeDisabled ?? false;
1701
+ const mergedCallerHeaders = mergeHeaders(model.headers, options?.headers);
1702
+ const umansGatewayWebSearchHeader = getUmansWebSearchHeader(model, mergedCallerHeaders);
1703
+
1704
+ let client: AnthropicMessagesClientLike;
1705
+ let isOAuthToken: boolean;
1706
+
1707
+ if (options?.client) {
1708
+ client = options.client;
1709
+ isOAuthToken = false;
1710
+ } else {
1711
+ const extraBetas = normalizeExtraBetas(options?.betas);
1712
+ const wantsAnthropicPriority = model.provider === "anthropic" && options?.serviceTier === "priority";
1713
+ // Skip the fast-mode beta when this session already learned the
1714
+ // endpoint+model rejects fast mode; `speed` is dropped from the params
1715
+ // too (dropFastMode), so the request stays a faithful non-fast request.
1716
+ if (wantsAnthropicPriority && !dropFastMode && !extraBetas.includes(fastModeBeta)) {
1717
+ extraBetas.push(fastModeBeta);
1718
+ }
1719
+ if (options?.taskBudget && !extraBetas.includes(taskBudgetBeta)) {
1720
+ extraBetas.push(taskBudgetBeta);
1721
+ }
1722
+ // `output_config.effort` ships on thinking-on requests AND on the
1723
+ // thinking-off adaptive pin (adaptive-only models get effort:"low" so
1724
+ // the toggle cannot 400); the beta must accompany the field in both.
1725
+ // MiniMax uses `thinking.type:"adaptive"` itself as the control surface,
1726
+ // so the sentinel "adaptive" value intentionally sends no output_config.
1727
+ const sendsAdaptiveEffortPin =
1728
+ options?.thinkingEnabled === false &&
1729
+ model.thinking?.mode === "anthropic-adaptive" &&
1730
+ !model.compat.disableAdaptiveThinking &&
1731
+ !usesAdaptiveThinkingTagOnly(model);
1732
+ if (
1733
+ model.reasoning &&
1734
+ ((options?.thinkingEnabled && options.effort !== "adaptive") || sendsAdaptiveEffortPin) &&
1735
+ !extraBetas.includes(effortBeta)
1736
+ ) {
1737
+ extraBetas.push(effortBeta);
1738
+ }
1739
+ if (model.compat.supportsMidConversationSystem && !extraBetas.includes(midConversationSystemBeta)) {
1740
+ // convertAnthropicMessages may upgrade developer turns to the
1741
+ // mid-conversation `system` role on these models; API-key requests
1742
+ // need the beta alongside the role (OAuth agent requests already
1743
+ // carry it in the Claude Code list).
1744
+ extraBetas.push(midConversationSystemBeta);
1745
+ }
1746
+ // `context_management.clear_thinking_20251015` requires this beta. OAuth
1747
+ // requests carry it in `claudeCodeAgentBetaDefaults`; API-key requests
1748
+ // need it added explicitly so the field is honored instead of rejected
1749
+ // (#3288). Skip transports where this package cannot deliver the beta
1750
+ // in the form their adapter accepts: Copilot strips Anthropic betas,
1751
+ // and Vertex rawPredict needs betas in the body (`anthropic_beta`),
1752
+ // not as an `anthropic-beta` HTTP header.
1753
+ if (
1754
+ model.reasoning &&
1755
+ options?.thinkingEnabled &&
1756
+ model.provider !== "github-copilot" &&
1757
+ model.provider !== "google-vertex" &&
1758
+ !extraBetas.includes(contextManagementBeta)
1759
+ ) {
1760
+ extraBetas.push(contextManagementBeta);
1761
+ }
1762
+ // Server-side fallback beta chain: opt-in via `options.fallbacks`.
1763
+ // Nested overrides (`speed`, `output_config.effort`,
1764
+ // `output_config.task_budget`) reuse the same top-level betas
1765
+ // Anthropic requires for the primary request, so scan the chain
1766
+ // and add every companion beta the fallback entries touch.
1767
+ if (options?.fallbacks?.length) {
1768
+ if (!extraBetas.includes(serverSideFallbackBeta)) {
1769
+ extraBetas.push(serverSideFallbackBeta);
1770
+ }
1771
+ for (const entry of options.fallbacks) {
1772
+ if (entry.speed === "fast" && !extraBetas.includes(fastModeBeta)) {
1773
+ extraBetas.push(fastModeBeta);
1774
+ }
1775
+ if (entry.output_config?.effort && !extraBetas.includes(effortBeta)) {
1776
+ extraBetas.push(effortBeta);
1777
+ }
1778
+ if (entry.output_config?.task_budget && !extraBetas.includes(taskBudgetBeta)) {
1779
+ extraBetas.push(taskBudgetBeta);
1780
+ }
1781
+ }
1782
+ }
1783
+
1784
+ const created = createClient(model, {
1785
+ model,
1786
+ apiKey,
1787
+ extraBetas,
1788
+ stream: true,
1789
+ interleavedThinking: options?.interleavedThinking ?? true,
1790
+ headers: options?.headers,
1791
+ dynamicHeaders: copilotDynamicHeaders?.headers,
1792
+ isOAuth: options?.isOAuth,
1793
+ hasTools: !!context.tools?.length,
1794
+ thinkingEnabled: options?.thinkingEnabled,
1795
+ thinkingDisplay: options?.thinkingDisplay,
1796
+ fetch: options?.fetch,
1797
+ claudeCodeSessionId: options?.sessionId ?? extractClaudeMetadataSessionId(options?.metadata?.user_id),
1798
+ });
1799
+ client = created.client;
1800
+ isOAuthToken = created.isOAuthToken;
1801
+ }
1802
+ const preparedContext = await prepareAnthropicManyImageContext(context, model.input.includes("image"));
1803
+ const prepareParams = async (): Promise<MessageCreateParamsStreaming> => {
1804
+ let nextParams = buildParams(
1805
+ model,
1806
+ preparedContext,
1807
+ isOAuthToken,
1808
+ options,
1809
+ disableStrictTools,
1810
+ umansGatewayWebSearchHeader !== undefined,
1811
+ );
1812
+ if (disableStrictTools) {
1813
+ dropAnthropicStrictTools(nextParams);
1814
+ }
1815
+ if (dropFastMode) {
1816
+ dropAnthropicFastMode(nextParams);
1817
+ }
1818
+ const replacementPayload = await options?.onPayload?.(nextParams, model);
1819
+ if (replacementPayload !== undefined) {
1820
+ nextParams = replacementPayload as typeof nextParams;
1821
+ }
1822
+ nextParams = toWellFormedDeep(nextParams) as typeof nextParams;
1823
+ rawRequestDump = {
1824
+ provider: model.provider,
1825
+ api: output.api,
1826
+ model: model.id,
1827
+ method: "POST",
1828
+ url: `${baseUrl}/v1/messages${isOAuthToken ? "?beta=true" : ""}`,
1829
+ body: nextParams,
1830
+ };
1831
+ return nextParams;
1832
+ };
1833
+ let params = await prepareParams();
1834
+
1835
+ // Opt-in flag: the response parser only honors `fallback` content
1836
+ // blocks and `usage.iterations` when the current request opted into
1837
+ // the server-side-fallback beta chain. Leaving `options.fallbacks`
1838
+ // unset preserves the pre-fallback stream shape on every event.
1839
+ const serverSideFallback = !!options?.fallbacks?.length;
1840
+ type Block = (
1841
+ | ThinkingContent
1842
+ | RedactedThinkingContent
1843
+ | TextContent
1844
+ | AnthropicFallbackContent
1845
+ | (ToolCall & { [kStreamingPartialJson]: string; [kStreamingLastParseLen]?: number })
1846
+ ) & { [kStreamingBlockIndex]: number };
1847
+ const idleTimeoutMs = options?.streamIdleTimeoutMs ?? getStreamIdleTimeoutMs();
1848
+ const firstEventTimeoutMs = options?.streamFirstEventTimeoutMs ?? getStreamFirstEventTimeoutMs(idleTimeoutMs);
1849
+ const requestTimeoutMs =
1850
+ firstEventTimeoutMs !== undefined && firstEventTimeoutMs > 0 ? firstEventTimeoutMs : undefined;
1851
+ const blocks = output.content as Block[];
1852
+ const finalizeStreamBlock = (block: Block, contentIndex: number): void => {
1853
+ if (block.type === "text") {
1854
+ stream.push({ type: "text_end", contentIndex, content: block.text, partial: output });
1855
+ } else if (block.type === "thinking") {
1856
+ const unwrappedThinking = unwrapAnthropicThinkingEnvelope(block.thinking);
1857
+ if (unwrappedThinking !== undefined) {
1858
+ block.thinking = unwrappedThinking;
1859
+ block.thinkingSignature = undefined;
1860
+ }
1861
+ stream.push({ type: "thinking_end", contentIndex, content: block.thinking, partial: output });
1862
+ } else if (block.type === "toolCall") {
1863
+ const finalJson =
1864
+ block[kStreamingPartialJson].length > 0
1865
+ ? block[kStreamingPartialJson]
1866
+ : JSON.stringify(block.arguments ?? {});
1867
+ try {
1868
+ block.arguments = parseJsonWithRepair(finalJson) as ToolCall["arguments"];
1869
+ } catch (parseError) {
1870
+ // Non-fatal: keep the best-effort arguments recovered by the throttled streaming
1871
+ // parser instead of failing the turn on malformed/truncated tool-argument JSON.
1872
+ reportAnthropicEnvelopeAnomaly(
1873
+ `tool_use ${block.id} arguments are not valid JSON: ${parseError instanceof Error ? parseError.message : String(parseError)}`,
1874
+ );
1875
+ const recoveredKeys = Object.keys(block.arguments ?? {});
1876
+ if (recoveredKeys.length === 0) {
1877
+ const maxLen = 512;
1878
+ const truncatedJson =
1879
+ finalJson.length <= maxLen
1880
+ ? finalJson
1881
+ : `${finalJson.slice(0, maxLen)}… [truncated ${finalJson.length - maxLen} chars]`;
1882
+ block.arguments = {
1883
+ __parseError: parseError instanceof Error ? parseError.message : String(parseError),
1884
+ __rawJson: truncatedJson,
1885
+ };
1886
+ }
1887
+ }
1888
+ clearStreamingPartialJson(block);
1889
+ stream.push({ type: "toolcall_end", contentIndex, toolCall: block, partial: output });
1890
+ }
1891
+ };
1892
+ stream.push({ type: "start", partial: output });
1893
+ // Retry loop for transient errors from the stream.
1894
+ // Provider-level transport/rate-limit failures: only before any streamed content starts.
1895
+ // Malformed envelopes/JSON: only before replay-unsafe text/tool events are visible on this stream.
1896
+ let providerRetryAttempt = 0;
1897
+ const firstEventTimeoutAbortError = new AIError.StreamTimeoutError(
1898
+ "Anthropic stream timed out while waiting for the first event",
1899
+ );
1900
+ const idleTimeoutAbortError = new AIError.StreamTimeoutError(
1901
+ "Anthropic stream stalled while waiting for the next event",
1902
+ );
1903
+ while (true) {
1904
+ activeAbortTracker = createAbortSourceTracker(options?.signal);
1905
+ const { requestSignal } = activeAbortTracker;
1906
+ // The provider loop owns retries: pin the client's internal retry loop
1907
+ // to zero even when no watchdog timeout is configured (the helper only
1908
+ // pins it alongside a timeout; a client retry budget of 5 would otherwise
1909
+ // multiply with PROVIDER_MAX_RETRIES into up to 66 wire attempts).
1910
+ const requestOptions = {
1911
+ ...createSdkStreamRequestOptions(requestSignal, requestTimeoutMs),
1912
+ maxRetries: 0,
1913
+ ...(umansGatewayWebSearchHeader ? { headers: umansGatewayWebSearchHeader } : {}),
1914
+ };
1915
+ const anthropicRequest: unknown =
1916
+ isOAuthToken && client.beta
1917
+ ? client.beta.messages.create({ ...params, stream: true }, requestOptions)
1918
+ : client.messages.create({ ...params, stream: true }, requestOptions);
1919
+ let streamedReplayUnsafeContent = false;
1920
+
1921
+ try {
1922
+ let requestTimeout: NodeJS.Timeout | undefined;
1923
+ if (requestTimeoutMs !== undefined) {
1924
+ requestTimeout = setTimeout(
1925
+ () => activeAbortTracker.abortLocally(firstEventTimeoutAbortError),
1926
+ requestTimeoutMs,
1927
+ );
1928
+ }
1929
+ let anthropicStream: AsyncIterable<AnthropicStreamEvent>;
1930
+ let response: Response;
1931
+ let requestId: string | null;
1932
+ let recordsRawSseEvents: boolean;
1933
+ try {
1934
+ ({
1935
+ events: anthropicStream,
1936
+ response,
1937
+ requestId,
1938
+ recordsRawSseEvents,
1939
+ } = await getAnthropicStreamResponse(anthropicRequest, requestSignal, rawSseObserver));
1940
+ } catch (error) {
1941
+ if (error instanceof AnthropicConnectionTimeoutError && !activeAbortTracker.wasCallerAbort()) {
1942
+ throw firstEventTimeoutAbortError;
1943
+ }
1944
+ throw error;
1945
+ } finally {
1946
+ if (requestTimeout !== undefined) clearTimeout(requestTimeout);
1947
+ }
1948
+ await notifyProviderResponse(options, response, model, requestId);
1949
+ let sawEvent = false;
1950
+ let sawMessageStart = false;
1951
+ let sawTerminalEnvelope = false;
1952
+ let sawMessageStop = false;
1953
+ // Set when a duplicate message_start splices a second envelope onto
1954
+ // the stream; closed indexes then refuse to reopen so replayed
1955
+ // content cannot duplicate (see content_block_start guard).
1956
+ let sawSplicedEnvelope = false;
1957
+ const closedBlockIndexes = new Set<number>();
1958
+ const openBlocks = new Map<
1959
+ number,
1960
+ {
1961
+ contentIndex: number;
1962
+ kind: "text" | "thinking" | "redactedThinking" | "fallback" | "toolCall" | "ignored";
1963
+ }
1964
+ >();
1965
+
1966
+ // Pings keep the idle deadline alive once content is flowing, but a
1967
+ // ping before message_start must not consume the first-event watchdog:
1968
+ // it would flip the (retryable) pre-content stall classification into
1969
+ // a terminal mid-stream idle timeout.
1970
+ let sawNonPingEvent = false;
1971
+ const timedAnthropicStream = iterateWithIdleTimeout(anthropicStream, {
1972
+ idleTimeoutMs,
1973
+ firstItemTimeoutMs: firstEventTimeoutMs,
1974
+ errorMessage: idleTimeoutAbortError.message,
1975
+ firstItemErrorMessage: firstEventTimeoutAbortError.message,
1976
+ onIdle: () => activeAbortTracker.abortLocally(idleTimeoutAbortError),
1977
+ onFirstItemTimeout: () => activeAbortTracker.abortLocally(firstEventTimeoutAbortError),
1978
+ abortSignal: options?.signal,
1979
+ isProgressItem: item => {
1980
+ if ((item as AnthropicStreamEvent).type === "ping") return sawNonPingEvent;
1981
+ sawNonPingEvent = true;
1982
+ return true;
1983
+ },
1984
+ });
1985
+ const observedAnthropicStream =
1986
+ rawSseObserver && !recordsRawSseEvents
1987
+ ? observeDecodedAnthropicSdkEvents(timedAnthropicStream, rawSseObserver)
1988
+ : timedAnthropicStream;
1989
+ for await (const event of observedAnthropicStream) {
1990
+ sawEvent = true;
1991
+
1992
+ if (event.type === "message_start") {
1993
+ if (sawMessageStart) {
1994
+ // Transparent reconnects can splice a fresh envelope onto the same
1995
+ // stream; keep the original message but surface the anomaly. Events
1996
+ // for blocks still open from the first envelope continue to apply,
1997
+ // but replayed blocks are dropped below (see closedBlockIndexes).
1998
+ reportAnthropicEnvelopeAnomaly("duplicate message_start event");
1999
+ sawSplicedEnvelope = true;
2000
+ continue;
2001
+ }
2002
+ sawMessageStart = true;
2003
+ const startMessage = event.message;
2004
+ if (startMessage?.id) output.responseId = startMessage.id;
2005
+ const startUsage = startMessage?.usage;
2006
+ if (startUsage) {
2007
+ applyAnthropicUsageExtras(output.usage, startUsage);
2008
+ output.usage.input = startUsage.input_tokens || 0;
2009
+ output.usage.output = startUsage.output_tokens || 0;
2010
+ output.usage.cacheRead = startUsage.cache_read_input_tokens || 0;
2011
+ output.usage.cacheWrite = startUsage.cache_creation_input_tokens || 0;
2012
+ output.usage.totalTokens =
2013
+ output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
2014
+ if (serverSideFallback) {
2015
+ const served = fallbackServedModelFromUsage(startUsage);
2016
+ if (served) output.model = served;
2017
+ if (!calculateFallbackTurnCost(model, output.usage, startUsage)) {
2018
+ calculateCost(model, output.usage);
2019
+ }
2020
+ } else {
2021
+ calculateCost(model, output.usage);
2022
+ }
2023
+ } else {
2024
+ reportAnthropicEnvelopeAnomaly("message_start missing usage");
2025
+ }
2026
+ continue;
2027
+ }
2028
+
2029
+ if (!sawMessageStart) {
2030
+ if (shouldIgnoreAnthropicPreambleEvent(event.type)) {
2031
+ continue;
2032
+ }
2033
+ throw new AIError.AnthropicStreamEnvelopeError(`received ${event.type} before message_start`);
2034
+ }
2035
+
2036
+ if (event.type === "content_block_start") {
2037
+ if (sawTerminalEnvelope) {
2038
+ reportAnthropicEnvelopeAnomaly(`received ${event.type} after terminal stop signal`);
2039
+ continue;
2040
+ }
2041
+ if (openBlocks.has(event.index)) {
2042
+ reportAnthropicEnvelopeAnomaly(`duplicate content_block_start index ${event.index}`);
2043
+ continue;
2044
+ }
2045
+ if (sawSplicedEnvelope && closedBlockIndexes.has(event.index)) {
2046
+ // A spliced envelope replaying an index this stream already
2047
+ // completed would append duplicate text/tool calls; consume its
2048
+ // events silently instead.
2049
+ reportAnthropicEnvelopeAnomaly(
2050
+ `replayed content_block_start index ${event.index} after duplicate message_start`,
2051
+ );
2052
+ openBlocks.set(event.index, { contentIndex: -1, kind: "ignored" });
2053
+ continue;
2054
+ }
2055
+ if (!event.content_block?.type) {
2056
+ reportAnthropicEnvelopeAnomaly("content_block_start missing content_block payload");
2057
+ continue;
2058
+ }
2059
+ if (!firstTokenTime) firstTokenTime = performance.now();
2060
+ if (event.content_block.type === "fallback") {
2061
+ // Fallback boundary is only meaningful when the request
2062
+ // opted into the beta chain — silently drop otherwise so
2063
+ // unopted-in sessions never see the block persisted or
2064
+ // influence downstream converters.
2065
+ const fallback = parseAnthropicFallbackWireBlock(event.content_block);
2066
+ if (!serverSideFallback || !fallback) {
2067
+ if (!fallback) {
2068
+ reportAnthropicEnvelopeAnomaly("fallback content_block missing model refs");
2069
+ }
2070
+ openBlocks.set(event.index, { contentIndex: -1, kind: "ignored" });
2071
+ continue;
2072
+ }
2073
+ const block: Block = { ...fallback, [kStreamingBlockIndex]: event.index };
2074
+ output.content.push(block);
2075
+ openBlocks.set(event.index, {
2076
+ contentIndex: output.content.length - 1,
2077
+ kind: "fallback",
2078
+ });
2079
+ // A fallback content block is the mid-stream signal that a
2080
+ // classifier block on the primary was retried on the
2081
+ // fallback model. Adopt the served id immediately so
2082
+ // pricing decisions downstream (final usage.iterations may
2083
+ // arrive before/after) see the right model.
2084
+ output.model = fallback.to.model;
2085
+ continue;
2086
+ }
2087
+ if (event.content_block.type === "text") {
2088
+ streamedReplayUnsafeContent = true;
2089
+ const block: Block = {
2090
+ type: "text",
2091
+ text: "",
2092
+ [kStreamingBlockIndex]: event.index,
2093
+ };
2094
+ output.content.push(block);
2095
+ const contentIndex = output.content.length - 1;
2096
+ openBlocks.set(event.index, { contentIndex, kind: "text" });
2097
+ stream.push({
2098
+ type: "text_start",
2099
+ contentIndex,
2100
+ partial: output,
2101
+ });
2102
+ } else if (event.content_block.type === "thinking") {
2103
+ streamedReplayUnsafeContent = true;
2104
+ const block: Block = {
2105
+ type: "thinking",
2106
+ thinking: "",
2107
+ thinkingSignature: "",
2108
+ [kStreamingBlockIndex]: event.index,
2109
+ };
2110
+ output.content.push(block);
2111
+ const contentIndex = output.content.length - 1;
2112
+ openBlocks.set(event.index, { contentIndex, kind: "thinking" });
2113
+ stream.push({
2114
+ type: "thinking_start",
2115
+ contentIndex,
2116
+ partial: output,
2117
+ });
2118
+ } else if (event.content_block.type === "redacted_thinking") {
2119
+ streamedReplayUnsafeContent = true;
2120
+ const block: Block = {
2121
+ type: "redactedThinking",
2122
+ data: event.content_block.data,
2123
+ [kStreamingBlockIndex]: event.index,
2124
+ };
2125
+ output.content.push(block);
2126
+ openBlocks.set(event.index, {
2127
+ contentIndex: output.content.length - 1,
2128
+ kind: "redactedThinking",
2129
+ });
2130
+ } else if (event.content_block.type === "tool_use") {
2131
+ streamedReplayUnsafeContent = true;
2132
+ const block: Block = {
2133
+ type: "toolCall",
2134
+ id: event.content_block.id,
2135
+ name: decodeAnthropicToolName(
2136
+ event.content_block.name,
2137
+ isOAuthToken,
2138
+ model.compat.escapeBuiltinToolNames,
2139
+ ),
2140
+ arguments: event.content_block.input ?? {},
2141
+ [kStreamingPartialJson]: "",
2142
+ [kStreamingBlockIndex]: event.index,
2143
+ };
2144
+ output.content.push(block);
2145
+ const contentIndex = output.content.length - 1;
2146
+ openBlocks.set(event.index, { contentIndex, kind: "toolCall" });
2147
+ stream.push({
2148
+ type: "toolcall_start",
2149
+ contentIndex,
2150
+ partial: output,
2151
+ });
2152
+ } else {
2153
+ openBlocks.set(event.index, { contentIndex: -1, kind: "ignored" });
2154
+ }
2155
+ } else if (event.type === "content_block_delta") {
2156
+ if (sawTerminalEnvelope) {
2157
+ reportAnthropicEnvelopeAnomaly(`received ${event.type} after terminal stop signal`);
2158
+ continue;
2159
+ }
2160
+ const openBlock = openBlocks.get(event.index);
2161
+ if (!openBlock) {
2162
+ reportAnthropicEnvelopeAnomaly(
2163
+ `received content_block_delta for unopened index ${event.index}`,
2164
+ );
2165
+ continue;
2166
+ }
2167
+ if (openBlock.kind === "ignored") continue;
2168
+ if (!event.delta?.type) {
2169
+ reportAnthropicEnvelopeAnomaly("content_block_delta missing delta payload");
2170
+ continue;
2171
+ }
2172
+ const block = blocks[openBlock.contentIndex];
2173
+ if (event.delta.type === "text_delta") {
2174
+ if (openBlock.kind !== "text" || block?.type !== "text") {
2175
+ reportAnthropicEnvelopeAnomaly(`received text_delta for ${openBlock.kind} block`);
2176
+ continue;
2177
+ }
2178
+ streamedReplayUnsafeContent = true;
2179
+ block.text += event.delta.text;
2180
+ stream.push({
2181
+ type: "text_delta",
2182
+ contentIndex: openBlock.contentIndex,
2183
+ delta: event.delta.text,
2184
+ partial: output,
2185
+ });
2186
+ } else if (event.delta.type === "thinking_delta") {
2187
+ if (openBlock.kind !== "thinking" || block?.type !== "thinking") {
2188
+ reportAnthropicEnvelopeAnomaly(`received thinking_delta for ${openBlock.kind} block`);
2189
+ continue;
2190
+ }
2191
+ streamedReplayUnsafeContent = true;
2192
+ block.thinking += event.delta.thinking;
2193
+ stream.push({
2194
+ type: "thinking_delta",
2195
+ contentIndex: openBlock.contentIndex,
2196
+ delta: event.delta.thinking,
2197
+ partial: output,
2198
+ });
2199
+ } else if (event.delta.type === "input_json_delta") {
2200
+ if (openBlock.kind !== "toolCall" || block?.type !== "toolCall") {
2201
+ reportAnthropicEnvelopeAnomaly(`received input_json_delta for ${openBlock.kind} block`);
2202
+ continue;
2203
+ }
2204
+ streamedReplayUnsafeContent = true;
2205
+ block[kStreamingPartialJson] += event.delta.partial_json;
2206
+ const throttled = parseStreamingJsonThrottled(
2207
+ block[kStreamingPartialJson],
2208
+ block[kStreamingLastParseLen] ?? 0,
2209
+ );
2210
+ if (throttled) {
2211
+ block.arguments = throttled.value;
2212
+ block[kStreamingLastParseLen] = throttled.parsedLen;
2213
+ }
2214
+ stream.push({
2215
+ type: "toolcall_delta",
2216
+ contentIndex: openBlock.contentIndex,
2217
+ delta: event.delta.partial_json,
2218
+ partial: output,
2219
+ });
2220
+ } else if (event.delta.type === "signature_delta") {
2221
+ if (openBlock.kind !== "thinking" || block?.type !== "thinking") {
2222
+ reportAnthropicEnvelopeAnomaly(`received signature_delta for ${openBlock.kind} block`);
2223
+ continue;
2224
+ }
2225
+ streamedReplayUnsafeContent = true;
2226
+ block.thinkingSignature = block.thinkingSignature || "";
2227
+ block.thinkingSignature += event.delta.signature;
2228
+ }
2229
+ } else if (event.type === "content_block_stop") {
2230
+ if (sawTerminalEnvelope) {
2231
+ reportAnthropicEnvelopeAnomaly(`received ${event.type} after terminal stop signal`);
2232
+ continue;
2233
+ }
2234
+ const openBlock = openBlocks.get(event.index);
2235
+ if (!openBlock) {
2236
+ reportAnthropicEnvelopeAnomaly(`received content_block_stop for unopened index ${event.index}`);
2237
+ continue;
2238
+ }
2239
+ if (openBlock.kind === "ignored") {
2240
+ openBlocks.delete(event.index);
2241
+ continue;
2242
+ }
2243
+ const block = blocks[openBlock.contentIndex];
2244
+ if (!block || block.type !== openBlock.kind) {
2245
+ reportAnthropicEnvelopeAnomaly(`content_block_stop kind mismatch for index ${event.index}`);
2246
+ openBlocks.delete(event.index);
2247
+ continue;
2248
+ }
2249
+ openBlocks.delete(event.index);
2250
+ closedBlockIndexes.add(event.index);
2251
+ finalizeStreamBlock(block, openBlock.contentIndex);
2252
+ } else if (event.type === "message_delta") {
2253
+ if (sawTerminalEnvelope) {
2254
+ // A spliced reconnect's second envelope must not overwrite the
2255
+ // completed message's stop reason or usage.
2256
+ reportAnthropicEnvelopeAnomaly("received message_delta after terminal stop signal");
2257
+ continue;
2258
+ }
2259
+ const delta = event.delta;
2260
+ const rawStopReason = delta?.stop_reason;
2261
+ if (rawStopReason) {
2262
+ output.stopReason = mapStopReason(rawStopReason);
2263
+ sawTerminalEnvelope = true;
2264
+ }
2265
+ if (output.stopReason === "error") {
2266
+ const stopDetails = delta?.stop_details;
2267
+ output.stopDetails = stopDetails ?? (rawStopReason ? { type: rawStopReason } : null);
2268
+ if (stopDetails?.type === "refusal") {
2269
+ const explanation = stopDetails.explanation?.trim();
2270
+ const category = stopDetails.category;
2271
+ const label = category ? `Refusal (${category})` : "Refusal";
2272
+ output.errorMessage = explanation ? `${label}: ${explanation}` : label;
2273
+ } else if (!output.errorMessage) {
2274
+ // Anthropic flagged an error-class stop (refusal / sensitive) without
2275
+ // populating stop_details. Surface the raw reason instead of falling
2276
+ // through to the generic "unknown error" string when we throw below.
2277
+ output.errorMessage =
2278
+ rawStopReason === "refusal"
2279
+ ? "Refusal (no details provided)"
2280
+ : rawStopReason === "sensitive"
2281
+ ? "Content flagged by safety filters"
2282
+ : `Anthropic stream ended with stop_reason: ${rawStopReason ?? "unknown"}`;
2283
+ }
2284
+ }
2285
+ const deltaUsage = event.usage;
2286
+ if (deltaUsage) {
2287
+ if (deltaUsage.input_tokens != null) {
2288
+ output.usage.input = deltaUsage.input_tokens;
2289
+ }
2290
+ if (deltaUsage.output_tokens != null) {
2291
+ output.usage.output = deltaUsage.output_tokens;
2292
+ }
2293
+ if (deltaUsage.cache_read_input_tokens != null) {
2294
+ output.usage.cacheRead = deltaUsage.cache_read_input_tokens;
2295
+ }
2296
+ if (deltaUsage.cache_creation_input_tokens != null) {
2297
+ output.usage.cacheWrite = deltaUsage.cache_creation_input_tokens;
2298
+ }
2299
+ applyAnthropicUsageExtras(output.usage, deltaUsage);
2300
+ output.usage.totalTokens =
2301
+ output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
2302
+ if (serverSideFallback) {
2303
+ const served = fallbackServedModelFromUsage(deltaUsage);
2304
+ if (served) output.model = served;
2305
+ if (!calculateFallbackTurnCost(model, output.usage, deltaUsage)) {
2306
+ calculateCost(model, output.usage);
2307
+ }
2308
+ } else {
2309
+ calculateCost(model, output.usage);
2310
+ }
2311
+ }
2312
+ } else if (event.type === "message_stop") {
2313
+ sawTerminalEnvelope = true;
2314
+ sawMessageStop = true;
2315
+ }
2316
+ }
2317
+
2318
+ const firstEventTimeoutError = activeAbortTracker.getLocalAbortReason();
2319
+ if (firstEventTimeoutError) {
2320
+ throw firstEventTimeoutError;
2321
+ }
2322
+ if (activeAbortTracker.wasCallerAbort()) {
2323
+ throw new AIError.AbortError();
2324
+ }
2325
+ if (!sawEvent || !sawMessageStart) {
2326
+ throw new AIError.AnthropicStreamEnvelopeError("stream ended before message_start");
2327
+ }
2328
+ if (!sawMessageStop) {
2329
+ reportAnthropicEnvelopeAnomaly("stream ended before message_stop");
2330
+ }
2331
+ if (openBlocks.size > 0) {
2332
+ for (const [openIndex, openBlock] of openBlocks) {
2333
+ reportAnthropicEnvelopeAnomaly(
2334
+ `stream ended with an unterminated ${openBlock.kind} block at index ${openIndex}`,
2335
+ );
2336
+ if (openBlock.kind === "ignored" || openBlock.contentIndex < 0) continue;
2337
+ const danglingBlock = blocks[openBlock.contentIndex];
2338
+ if (danglingBlock) finalizeStreamBlock(danglingBlock, openBlock.contentIndex);
2339
+ }
2340
+ openBlocks.clear();
2341
+ }
2342
+
2343
+ if (output.stopReason === "aborted" || output.stopReason === "error") {
2344
+ throw new AIError.ProviderResponseError(output.errorMessage ?? "An unknown error occurred", {
2345
+ provider: model.provider,
2346
+ kind: "output",
2347
+ });
2348
+ }
2349
+ break;
2350
+ } catch (streamError) {
2351
+ const streamFailure = activeAbortTracker.getLocalAbortReason() ?? streamError;
2352
+ if (
2353
+ !disableStrictTools &&
2354
+ firstTokenTime === undefined &&
2355
+ hasStrictAnthropicTools(params) &&
2356
+ AIError.isGrammarError(streamFailure)
2357
+ ) {
2358
+ // Log-only: the retried turn must not carry an errorMessage on
2359
+ // success (consumers treat its presence as failure).
2360
+ logger.warn("anthropic: strict tool grammar rejected, retrying without strict tools", {
2361
+ model: model.id,
2362
+ error: await finalizeErrorMessage(streamFailure, rawRequestDump),
2363
+ });
2364
+ if (providerSessionState) {
2365
+ providerSessionState.strictToolsDisabled = true;
2366
+ }
2367
+ disableStrictTools = true;
2368
+ params = await prepareParams();
2369
+ providerRetryAttempt = 0;
2370
+ output.content.length = 0;
2371
+ output.model = model.id;
2372
+ output.responseId = undefined;
2373
+ output.errorMessage = undefined;
2374
+ output.providerPayload = undefined;
2375
+ output.usage = createEmptyUsage(copilotDynamicHeaders?.premiumRequests);
2376
+ output.stopReason = "stop";
2377
+ firstTokenTime = undefined;
2378
+ continue;
2379
+ }
2380
+ if (
2381
+ !dropFastMode &&
2382
+ model.provider === "anthropic" &&
2383
+ options?.serviceTier === "priority" &&
2384
+ firstTokenTime === undefined &&
2385
+ AIError.isFastModeUnsupported(streamFailure)
2386
+ ) {
2387
+ logger.debug("anthropic: fast mode unsupported, retrying without speed", {
2388
+ model: model.id,
2389
+ error: streamFailure instanceof Error ? streamFailure.message : String(streamFailure),
2390
+ });
2391
+ if (providerSessionState) {
2392
+ providerSessionState.fastModeDisabled = true;
2393
+ }
2394
+ dropFastMode = true;
2395
+ params = await prepareParams();
2396
+ providerRetryAttempt = 0;
2397
+ output.content.length = 0;
2398
+ output.model = model.id;
2399
+ output.responseId = undefined;
2400
+ output.errorMessage = undefined;
2401
+ output.providerPayload = undefined;
2402
+ output.usage = createEmptyUsage(copilotDynamicHeaders?.premiumRequests);
2403
+ output.stopReason = "stop";
2404
+ firstTokenTime = undefined;
2405
+ continue;
2406
+ }
2407
+ const isTransientEnvelopeFailure =
2408
+ AIError.isTransientStreamParseError(streamFailure) || AIError.isStreamEnvelopeError(streamFailure);
2409
+ const isLocalIdleTimeout =
2410
+ streamFailure === idleTimeoutAbortError ||
2411
+ (streamFailure instanceof Error && streamFailure.message === idleTimeoutAbortError.message);
2412
+ const canRetryTransientEnvelopeFailure = isTransientEnvelopeFailure && !streamedReplayUnsafeContent;
2413
+ const canRetryProviderFailure =
2414
+ !isLocalIdleTimeout &&
2415
+ firstTokenTime === undefined &&
2416
+ !streamedReplayUnsafeContent &&
2417
+ isProviderRetryableError(streamFailure, model.provider);
2418
+ if (
2419
+ activeAbortTracker.wasCallerAbort() ||
2420
+ providerRetryAttempt >= PROVIDER_MAX_RETRIES ||
2421
+ (!canRetryTransientEnvelopeFailure && !canRetryProviderFailure)
2422
+ ) {
2423
+ throw streamFailure;
2424
+ }
2425
+ providerRetryAttempt++;
2426
+ const backoffDelayMs = calculateAnthropicRetryDelayMs(providerRetryAttempt - 1);
2427
+ // Honor the server's retry hint (`retry-after-ms`/`retry-after`) on
2428
+ // 429/529-style failures: retrying sooner than the server asked is a
2429
+ // guaranteed failure that just burns the retry budget.
2430
+ const headerDelayMs =
2431
+ streamFailure instanceof Error && streamFailure instanceof AnthropicApiError
2432
+ ? retryDelayFromHeaders(streamFailure.headers)
2433
+ : undefined;
2434
+ const delayMs = headerDelayMs !== undefined ? Math.max(headerDelayMs, backoffDelayMs) : backoffDelayMs;
2435
+ if (options?.providerRetryWait) {
2436
+ await options.providerRetryWait(delayMs, options.signal);
2437
+ } else {
2438
+ await scheduler.wait(delayMs, { signal: options?.signal });
2439
+ }
2440
+ output.content.length = 0;
2441
+ output.model = model.id;
2442
+ output.responseId = undefined;
2443
+ output.errorMessage = undefined;
2444
+ output.stopDetails = undefined;
2445
+ output.providerPayload = undefined;
2446
+ output.usage = createEmptyUsage(copilotDynamicHeaders?.premiumRequests);
2447
+ output.stopReason = "stop";
2448
+ firstTokenTime = undefined;
2449
+ }
2450
+ }
2451
+ output.duration = performance.now() - startTime;
2452
+ if (firstTokenTime) output.ttft = firstTokenTime - startTime;
2453
+ if (dropFastMode && model.provider === "anthropic" && options?.serviceTier === "priority") {
2454
+ output.disabledFeatures = [...(output.disabledFeatures ?? []), "priority"];
2455
+ }
2456
+ stream.push({ type: "done", reason: output.stopReason, message: output });
2457
+ stream.end();
2458
+ } catch (error) {
2459
+ for (const block of output.content) {
2460
+ if (block.type === "toolCall") clearStreamingPartialJson(block);
2461
+ }
2462
+ const result = await AIError.finalize(error, {
2463
+ api: model.api,
2464
+ provider: model.provider,
2465
+ abortTracker: activeAbortTracker,
2466
+ rawRequestDump,
2467
+ });
2468
+ output.stopReason = result.stopReason;
2469
+ output.errorStatus = result.status;
2470
+ output.errorId = result.id;
2471
+ output.errorMessage = result.message;
2472
+ output.duration = performance.now() - startTime;
2473
+ if (firstTokenTime) output.ttft = firstTokenTime - startTime;
2474
+ stream.push({ type: "error", reason: output.stopReason, error: output });
2475
+ stream.end();
2476
+ }
2477
+ })();
2478
+
2479
+ return stream;
2480
+ };
2481
+
2482
+ /**
2483
+ * Public entry: wrap the single-attempt streamer with bounded empty-completion
2484
+ * retries (a benign terminal stop carrying no content/usage would otherwise
2485
+ * stall the agent loop). The inner attempt keeps its own provider-failure retry
2486
+ * loop; this layer only re-issues a fresh request on an empty success. Shared
2487
+ * with the OpenAI-completions provider via `withEmptyCompletionRetry`.
2488
+ */
2489
+ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (model, context, options) =>
2490
+ withEmptyCompletionRetry(model, context, options, streamAnthropicOnce);
2491
+
2492
+ export type AnthropicSystemBlock = {
2493
+ type: "text";
2494
+ text: string;
2495
+ cache_control?: AnthropicCacheControl;
2496
+ };
2497
+ type SystemBlockOptions = {
2498
+ includeClaudeCodeInstruction?: boolean;
2499
+ extraInstructions?: string[];
2500
+ /** Text of the first user message — used as fingerprint seed for the billing header. */
2501
+ firstUserMessageText?: string;
2502
+ cacheControl?: AnthropicCacheControl;
2503
+ };
2504
+
2505
+ function applyClaudeCodeSystemCache(
2506
+ blocks: AnthropicSystemBlock[],
2507
+ cacheControl: AnthropicCacheControl | undefined,
2508
+ ): number {
2509
+ if (!cacheControl || blocks.length === 0) return 0;
2510
+ const lastIndex = blocks.length - 1;
2511
+ if (blocks[lastIndex].cache_control != null) return 0;
2512
+ blocks[lastIndex] = { ...blocks[lastIndex], cache_control: cloneAnthropicCacheControl(cacheControl) };
2513
+ return 1;
2514
+ }
2515
+
2516
+ export function buildAnthropicSystemBlocks(
2517
+ systemPrompt: readonly string[] | undefined,
2518
+ options: SystemBlockOptions = {},
2519
+ ): AnthropicSystemBlock[] | undefined {
2520
+ const { includeClaudeCodeInstruction = false, extraInstructions = [], firstUserMessageText, cacheControl } = options;
2521
+ const sanitizedPrompts = normalizeSystemPrompts(systemPrompt);
2522
+ const trimmedInstructions = extraInstructions.map(instruction => instruction.trim()).filter(Boolean);
2523
+ const hasBillingHeader = sanitizedPrompts.some(prompt => prompt.startsWith(CLAUDE_BILLING_HEADER_PREFIX));
2524
+
2525
+ if (includeClaudeCodeInstruction && !hasBillingHeader) {
2526
+ const blocks: AnthropicSystemBlock[] = [
2527
+ { type: "text", text: createClaudeBillingHeader(firstUserMessageText ?? "") },
2528
+ { type: "text", text: claudeCodeSystemInstruction },
2529
+ ];
2530
+
2531
+ for (const instruction of trimmedInstructions) {
2532
+ blocks.push({ type: "text", text: instruction });
2533
+ }
2534
+ for (const prompt of sanitizedPrompts) {
2535
+ blocks.push({ type: "text", text: prompt });
2536
+ }
2537
+ applyClaudeCodeSystemCache(blocks, cacheControl);
2538
+
2539
+ return blocks;
2540
+ }
2541
+
2542
+ const blocks: AnthropicSystemBlock[] = [];
2543
+ for (const instruction of trimmedInstructions) {
2544
+ blocks.push({ type: "text", text: instruction });
2545
+ }
2546
+ for (const prompt of sanitizedPrompts) {
2547
+ blocks.push({ type: "text", text: prompt });
2548
+ }
2549
+ const lastIndex = blocks.length - 1;
2550
+ if (cacheControl && lastIndex >= 0 && blocks[lastIndex].cache_control == null) {
2551
+ blocks[lastIndex] = { ...blocks[lastIndex], cache_control: cloneAnthropicCacheControl(cacheControl) };
2552
+ }
2553
+ return blocks.length > 0 ? blocks : undefined;
2554
+ }
2555
+
2556
+ export function normalizeExtraBetas(betas?: string[] | string): string[] {
2557
+ if (!betas) return [];
2558
+ const raw = Array.isArray(betas) ? betas : betas.split(",");
2559
+ return raw.map(beta => beta.trim()).filter(beta => beta.length > 0);
2560
+ }
2561
+
2562
+ export function buildAnthropicClientOptions(args: AnthropicClientOptionsArgs): AnthropicClientOptionsResult {
2563
+ const {
2564
+ model,
2565
+ apiKey,
2566
+ extraBetas = [],
2567
+ stream = true,
2568
+ interleavedThinking = true,
2569
+ headers,
2570
+ dynamicHeaders,
2571
+ hasTools = false,
2572
+ thinkingEnabled = false,
2573
+ thinkingDisplay,
2574
+ isOAuth,
2575
+ claudeCodeSessionId,
2576
+ } = args;
2577
+ const compat = model.compat;
2578
+ const needsInterleavedBeta = interleavedThinking && !model.thinking?.supportsDisplay;
2579
+ const needsFineGrainedToolStreamingBeta = hasTools && !compat.supportsEagerToolInputStreaming;
2580
+ const oauthToken = isOAuth ?? isAnthropicOAuthToken(apiKey);
2581
+ const baseUrl = resolveAnthropicBaseUrl(model, apiKey);
2582
+ const foundryCustomHeaders = resolveAnthropicCustomHeaders(model);
2583
+ const tlsFetchOptions = buildClaudeCodeTlsFetchOptions(model, baseUrl);
2584
+ // Disable Bun's native ~300s pre-response fetch timeout (issue #2422).
2585
+ // `AnthropicMessagesClient` already arms its own DEFAULT_TIMEOUT_MS timer
2586
+ // per request, so the native ceiling can only short-circuit slow-prefill
2587
+ // streams before the configured watchdog gets to govern them.
2588
+ const fetchOptions: AnthropicFetchOptions = { ...(tlsFetchOptions ?? {}), timeout: false };
2589
+ const baseFetch = args.fetch ?? fetch;
2590
+ // Only OAuth requests inject the CC billing header; no API-key request can ever
2591
+ // contain it, so there is no need to install the rewriter for those.
2592
+ const cchFetch = oauthToken ? wrapFetchForCch(baseFetch) : baseFetch;
2593
+ if (model.provider === "github-copilot") {
2594
+ const copilotApiKey = parseGitHubCopilotApiKey(apiKey).accessToken;
2595
+ // The GitHub Copilot Anthropic proxy doesn't accept Anthropic beta
2596
+ // features (and the catalog already forces `supportsEagerToolInputStreaming
2597
+ // = false` for this host, so `needsFineGrainedToolStreamingBeta` is true
2598
+ // whenever tools are present). Forward only caller-supplied betas.
2599
+ const betaFeatures = [...extraBetas];
2600
+ const defaultHeaders = mergeHeaders(
2601
+ {
2602
+ Accept: stream ? "text/event-stream" : "application/json",
2603
+ "Content-Type": "application/json",
2604
+ "anthropic-version": "2023-06-01",
2605
+ "Anthropic-Dangerous-Direct-Browser-Access": "true",
2606
+ Authorization: `Bearer ${copilotApiKey}`,
2607
+ ...(betaFeatures.length > 0 ? { "anthropic-beta": buildBetaHeader([], betaFeatures) } : {}),
2608
+ },
2609
+ model.headers,
2610
+ dynamicHeaders,
2611
+ headers,
2612
+ );
2613
+
2614
+ return {
2615
+ isOAuthToken: false,
2616
+ apiKey: null,
2617
+ authToken: copilotApiKey,
2618
+ baseURL: baseUrl,
2619
+ maxRetries: 5,
2620
+ defaultHeaders,
2621
+ fetch: cchFetch,
2622
+ fetchOptions,
2623
+ };
2624
+ }
2625
+
2626
+ const betaFeatures = [...extraBetas];
2627
+ if (needsFineGrainedToolStreamingBeta) {
2628
+ betaFeatures.push(fineGrainedToolStreamingBeta);
2629
+ }
2630
+ if (needsInterleavedBeta) {
2631
+ betaFeatures.push(interleavedThinkingBeta);
2632
+ }
2633
+
2634
+ const defaultHeaders = buildAnthropicHeaders({
2635
+ apiKey,
2636
+ baseUrl,
2637
+ isOAuth: oauthToken,
2638
+ extraBetas: betaFeatures,
2639
+ stream,
2640
+ modelHeaders: mergeHeaders(
2641
+ model.headers,
2642
+ foundryCustomHeaders,
2643
+ getUmansWebSearchHeader(model, mergeHeaders(model.headers, headers)),
2644
+ headers,
2645
+ dynamicHeaders,
2646
+ ),
2647
+ isCloudflareAiGateway: model.provider === "cloudflare-ai-gateway",
2648
+ claudeCodeSessionId,
2649
+ claudeCodeBetas: oauthToken
2650
+ ? buildClaudeCodeBetas(hasTools || thinkingEnabled, thinkingEnabled, thinkingDisplay === "omitted")
2651
+ : [],
2652
+ });
2653
+
2654
+ if (model.provider === "cloudflare-ai-gateway") {
2655
+ return {
2656
+ isOAuthToken: false,
2657
+ apiKey: null,
2658
+ authToken: null,
2659
+ baseURL: baseUrl,
2660
+ maxRetries: 5,
2661
+ defaultHeaders,
2662
+ fetch: cchFetch,
2663
+ fetchOptions,
2664
+ };
2665
+ }
2666
+
2667
+ // OpenCode Go and Umans validate Anthropic-compatible API-key auth through
2668
+ // `X-Api-Key`; bearer-only requests reach the endpoint but fail auth.
2669
+ if (model.provider === "opencode-go" || model.provider === "umans") {
2670
+ delete defaultHeaders.Authorization;
2671
+ return {
2672
+ isOAuthToken: false,
2673
+ apiKey,
2674
+ authToken: null,
2675
+ baseURL: baseUrl,
2676
+ maxRetries: 5,
2677
+ defaultHeaders,
2678
+ fetch: cchFetch,
2679
+ fetchOptions,
2680
+ };
2681
+ }
2682
+ // OpenCode Zen's Anthropic-compatible gateway accepts bearer auth only;
2683
+ // leaving apiKey set lets the client add X-Api-Key, which upstream Alibaba rejects.
2684
+ if (model.provider === "opencode-zen") {
2685
+ return {
2686
+ isOAuthToken: false,
2687
+ apiKey: null,
2688
+ authToken: null,
2689
+ baseURL: baseUrl,
2690
+ maxRetries: 5,
2691
+ defaultHeaders,
2692
+ fetch: cchFetch,
2693
+ fetchOptions,
2694
+ };
2695
+ }
2696
+
2697
+ // Suppress the client-level `X-Api-Key` whenever an `Authorization` header
2698
+ // already sits in `defaultHeaders` for a non-official, non-OAuth endpoint —
2699
+ // either our auto-built `Bearer <apiKey>` or a caller-supplied custom auth
2700
+ // scheme via `model.headers` (#3391). Adding a bonus `X-Api-Key` would force
2701
+ // the proxy to deal with two competing credentials when the user explicitly
2702
+ // asked for one.
2703
+ const authorizationHeader = getHeaderCaseInsensitive(defaultHeaders, "Authorization");
2704
+ const shouldSuppressClientApiKey =
2705
+ !oauthToken && !model.compat.officialEndpoint && typeof authorizationHeader === "string";
2706
+
2707
+ return {
2708
+ isOAuthToken: oauthToken,
2709
+ apiKey: oauthToken || shouldSuppressClientApiKey ? null : apiKey,
2710
+ authToken: oauthToken ? apiKey : undefined,
2711
+ baseURL: baseUrl,
2712
+ maxRetries: 5,
2713
+ defaultHeaders,
2714
+ fetch: cchFetch,
2715
+ fetchOptions,
2716
+ };
2717
+ }
2718
+
2719
+ function createClient(
2720
+ model: Model<"anthropic-messages">,
2721
+ args: AnthropicClientOptionsArgs,
2722
+ ): { client: AnthropicMessagesClient; isOAuthToken: boolean } {
2723
+ const { isOAuthToken: oauthToken, ...clientOptions } = buildAnthropicClientOptions({ ...args, model });
2724
+ const client = new AnthropicMessagesClient(clientOptions);
2725
+ return { client, isOAuthToken: oauthToken };
2726
+ }
2727
+
2728
+ function disableThinkingIfToolChoiceForced(params: MessageCreateParamsStreaming): void {
2729
+ const toolChoice = params.tool_choice;
2730
+ if (!toolChoice) return;
2731
+ if (toolChoice.type !== "any" && toolChoice.type !== "tool") return;
2732
+
2733
+ delete params.thinking;
2734
+ delete params.context_management;
2735
+ const outputConfig = params.output_config as AnthropicOutputConfig | undefined;
2736
+ if (!outputConfig) return;
2737
+
2738
+ delete outputConfig.effort;
2739
+ if (Object.keys(outputConfig).length === 0) {
2740
+ delete params.output_config;
2741
+ }
2742
+ }
2743
+
2744
+ function ensureMaxTokensForThinking(params: MessageCreateParamsStreaming, maxAllowedTokens: number): void {
2745
+ const thinking = params.thinking;
2746
+ if (thinking?.type !== "enabled") return;
2747
+
2748
+ const budgetTokens = thinking.budget_tokens ?? 0;
2749
+ if (budgetTokens <= 0) return;
2750
+
2751
+ const currentMaxTokens = Math.min(params.max_tokens ?? maxAllowedTokens, maxAllowedTokens);
2752
+ const raisedMaxTokens = Math.min(
2753
+ Math.max(currentMaxTokens, budgetTokens + OUTPUT_FALLBACK_BUFFER),
2754
+ maxAllowedTokens,
2755
+ );
2756
+ params.max_tokens = raisedMaxTokens;
2757
+
2758
+ if (budgetTokens + OUTPUT_FALLBACK_BUFFER <= raisedMaxTokens) return;
2759
+
2760
+ const clampedBudget = raisedMaxTokens - OUTPUT_FALLBACK_BUFFER;
2761
+ if (clampedBudget <= 0) {
2762
+ throw new AIError.ConfigurationError(
2763
+ `Anthropic thinking budget requires max_tokens greater than ${OUTPUT_FALLBACK_BUFFER}; got ${raisedMaxTokens}`,
2764
+ );
2765
+ }
2766
+ thinking.budget_tokens = clampedBudget;
2767
+ }
2768
+
2769
+ type CacheControlBlock = {
2770
+ cache_control?: AnthropicCacheControl | null;
2771
+ };
2772
+
2773
+ function applyCacheControlToLastBlock<T extends CacheControlBlock>(
2774
+ blocks: T[],
2775
+ cacheControl: AnthropicCacheControl,
2776
+ ): boolean {
2777
+ if (blocks.length === 0) return false;
2778
+ const lastIndex = blocks.length - 1;
2779
+ if (blocks[lastIndex].cache_control != null) return false;
2780
+ blocks[lastIndex] = { ...blocks[lastIndex], cache_control: cloneAnthropicCacheControl(cacheControl) };
2781
+ return true;
2782
+ }
2783
+
2784
+ function applyCacheControlToLastTextBlock(
2785
+ blocks: Array<ContentBlockParam & CacheControlBlock>,
2786
+ cacheControl: AnthropicCacheControl,
2787
+ ): boolean {
2788
+ if (blocks.length === 0) return false;
2789
+ for (let i = blocks.length - 1; i >= 0; i--) {
2790
+ if (blocks[i].type === "text") {
2791
+ if (blocks[i].cache_control != null) return false;
2792
+ blocks[i] = { ...blocks[i], cache_control: cloneAnthropicCacheControl(cacheControl) };
2793
+ return true;
2794
+ }
2795
+ }
2796
+ // No text block — fall back to the last block that accepts cache_control;
2797
+ // thinking/redacted_thinking blocks reject the field with a 400.
2798
+ for (let i = blocks.length - 1; i >= 0; i--) {
2799
+ const type = blocks[i].type;
2800
+ if (type === "thinking" || type === "redacted_thinking") continue;
2801
+ if (blocks[i].cache_control != null) return false;
2802
+ blocks[i] = { ...blocks[i], cache_control: cloneAnthropicCacheControl(cacheControl) };
2803
+ return true;
2804
+ }
2805
+ return false;
2806
+ }
2807
+
2808
+ function applyPromptCaching(params: MessageCreateParamsStreaming, cacheControl?: AnthropicCacheControl): void {
2809
+ if (!cacheControl) return;
2810
+
2811
+ const MAX_CACHE_BREAKPOINTS = 4;
2812
+ let cacheBreakpointsUsed = countCacheControlBreakpoints(params);
2813
+ if (cacheBreakpointsUsed >= MAX_CACHE_BREAKPOINTS) return;
2814
+ let isCCLayout = false;
2815
+
2816
+ if (params.system && Array.isArray(params.system) && params.system.length > 0) {
2817
+ isCCLayout =
2818
+ params.system.length >= 3 &&
2819
+ (params.system[0] as { text?: string }).text?.startsWith(CLAUDE_BILLING_HEADER_PREFIX) === true;
2820
+ if (isCCLayout) {
2821
+ const placed = Math.min(
2822
+ MAX_CACHE_BREAKPOINTS - cacheBreakpointsUsed,
2823
+ applyClaudeCodeSystemCache(params.system as AnthropicSystemBlock[], cacheControl),
2824
+ );
2825
+ cacheBreakpointsUsed += placed;
2826
+ } else if (applyCacheControlToLastBlock(params.system, cacheControl)) {
2827
+ cacheBreakpointsUsed++;
2828
+ }
2829
+ }
2830
+
2831
+ if (cacheBreakpointsUsed >= MAX_CACHE_BREAKPOINTS) return;
2832
+
2833
+ const start = isCCLayout ? Math.max(0, params.messages.length - 1) : Math.max(0, params.messages.length - 2);
2834
+ for (let i = start; i < params.messages.length; i++) {
2835
+ if (cacheBreakpointsUsed >= MAX_CACHE_BREAKPOINTS) break;
2836
+ const message = params.messages[i];
2837
+ if (!message) continue;
2838
+ if (typeof message.content === "string") {
2839
+ message.content = [
2840
+ { type: "text", text: message.content, cache_control: cloneAnthropicCacheControl(cacheControl) },
2841
+ ];
2842
+ cacheBreakpointsUsed++;
2843
+ } else if (Array.isArray(message.content) && message.content.length > 0) {
2844
+ if (
2845
+ applyCacheControlToLastTextBlock(
2846
+ message.content as Array<ContentBlockParam & CacheControlBlock>,
2847
+ cacheControl,
2848
+ )
2849
+ ) {
2850
+ cacheBreakpointsUsed++;
2851
+ }
2852
+ }
2853
+ }
2854
+ }
2855
+
2856
+ function normalizeCacheControlBlockTtl(block: CacheControlBlock, seenFiveMinute: { value: boolean }): void {
2857
+ const cacheControl = block.cache_control;
2858
+ if (!cacheControl) return;
2859
+ if (cacheControl.ttl !== "1h") {
2860
+ seenFiveMinute.value = true;
2861
+ return;
2862
+ }
2863
+ if (seenFiveMinute.value) {
2864
+ const normalized = cloneAnthropicCacheControl(cacheControl);
2865
+ delete normalized.ttl;
2866
+ block.cache_control = normalized;
2867
+ }
2868
+ }
2869
+
2870
+ function normalizeCacheControlTtlOrdering(params: MessageCreateParamsStreaming): void {
2871
+ const seenFiveMinute = { value: false };
2872
+ if (params.tools) {
2873
+ for (const tool of params.tools as Array<AnthropicWireTool & CacheControlBlock>) {
2874
+ normalizeCacheControlBlockTtl(tool, seenFiveMinute);
2875
+ }
2876
+ }
2877
+ if (params.system && Array.isArray(params.system)) {
2878
+ for (const block of params.system as Array<AnthropicSystemBlock & CacheControlBlock>) {
2879
+ normalizeCacheControlBlockTtl(block, seenFiveMinute);
2880
+ }
2881
+ }
2882
+ for (const message of params.messages) {
2883
+ if (!Array.isArray(message.content)) continue;
2884
+ for (const block of message.content as Array<ContentBlockParam & CacheControlBlock>) {
2885
+ normalizeCacheControlBlockTtl(block, seenFiveMinute);
2886
+ }
2887
+ }
2888
+ }
2889
+
2890
+ function findLastCacheControlIndex<T extends CacheControlBlock>(blocks: T[]): number {
2891
+ for (let index = blocks.length - 1; index >= 0; index--) {
2892
+ if (blocks[index]?.cache_control != null) return index;
2893
+ }
2894
+ return -1;
2895
+ }
2896
+
2897
+ function stripCacheControlExceptIndex<T extends CacheControlBlock>(
2898
+ blocks: T[],
2899
+ preserveIndex: number,
2900
+ excessCounter: { value: number },
2901
+ ): void {
2902
+ for (let index = 0; index < blocks.length && excessCounter.value > 0; index++) {
2903
+ if (index === preserveIndex) continue;
2904
+ if (!blocks[index]?.cache_control) continue;
2905
+ delete blocks[index].cache_control;
2906
+ excessCounter.value--;
2907
+ }
2908
+ }
2909
+
2910
+ function stripAllCacheControl<T extends CacheControlBlock>(blocks: T[], excessCounter: { value: number }): void {
2911
+ for (const block of blocks) {
2912
+ if (excessCounter.value <= 0) return;
2913
+ if (!block.cache_control) continue;
2914
+ delete block.cache_control;
2915
+ excessCounter.value--;
2916
+ }
2917
+ }
2918
+
2919
+ function stripMessageCacheControl(
2920
+ messages: MessageCreateParamsStreaming["messages"],
2921
+ excessCounter: { value: number },
2922
+ ): void {
2923
+ for (const message of messages) {
2924
+ if (excessCounter.value <= 0) return;
2925
+ if (!Array.isArray(message.content)) continue;
2926
+ for (const block of message.content as Array<ContentBlockParam & CacheControlBlock>) {
2927
+ if (excessCounter.value <= 0) return;
2928
+ if (!block.cache_control) continue;
2929
+ delete block.cache_control;
2930
+ excessCounter.value--;
2931
+ }
2932
+ }
2933
+ }
2934
+
2935
+ function countCacheControlBreakpoints(params: MessageCreateParamsStreaming): number {
2936
+ let total = 0;
2937
+ if (params.tools) {
2938
+ for (const tool of params.tools as Array<AnthropicWireTool & CacheControlBlock>) {
2939
+ if (tool.cache_control) total++;
2940
+ }
2941
+ }
2942
+ if (params.system && Array.isArray(params.system)) {
2943
+ for (const block of params.system as Array<AnthropicSystemBlock & CacheControlBlock>) {
2944
+ if (block.cache_control) total++;
2945
+ }
2946
+ }
2947
+ for (const message of params.messages) {
2948
+ if (!Array.isArray(message.content)) continue;
2949
+ for (const block of message.content as Array<ContentBlockParam & CacheControlBlock>) {
2950
+ if (block.cache_control) total++;
2951
+ }
2952
+ }
2953
+ return total;
2954
+ }
2955
+
2956
+ function enforceCacheControlLimit(params: MessageCreateParamsStreaming, maxBreakpoints: number): void {
2957
+ const total = countCacheControlBreakpoints(params);
2958
+ if (total <= maxBreakpoints) return;
2959
+ const excessCounter = { value: total - maxBreakpoints };
2960
+ const systemBlocks =
2961
+ params.system && Array.isArray(params.system)
2962
+ ? (params.system as Array<AnthropicSystemBlock & CacheControlBlock>)
2963
+ : [];
2964
+ const toolBlocks = (params.tools ?? []) as Array<AnthropicWireTool & CacheControlBlock>;
2965
+ const lastSystemIndex = findLastCacheControlIndex(systemBlocks);
2966
+ const lastToolIndex = findLastCacheControlIndex(toolBlocks);
2967
+ if (systemBlocks.length > 0) {
2968
+ stripCacheControlExceptIndex(systemBlocks, lastSystemIndex, excessCounter);
2969
+ }
2970
+ if (excessCounter.value <= 0) return;
2971
+ if (toolBlocks.length > 0) {
2972
+ stripCacheControlExceptIndex(toolBlocks, lastToolIndex, excessCounter);
2973
+ }
2974
+ if (excessCounter.value <= 0) return;
2975
+ stripMessageCacheControl(params.messages, excessCounter);
2976
+ if (excessCounter.value <= 0) return;
2977
+ if (systemBlocks.length > 0) {
2978
+ stripAllCacheControl(systemBlocks, excessCounter);
2979
+ }
2980
+ if (excessCounter.value <= 0) return;
2981
+ if (toolBlocks.length > 0) {
2982
+ stripAllCacheControl(toolBlocks, excessCounter);
2983
+ }
2984
+ }
2985
+
2986
+ function usesAdaptiveThinkingTagOnly(model: Model<"anthropic-messages">): boolean {
2987
+ const thinking = model.thinking;
2988
+ if (thinking?.mode !== "anthropic-adaptive") return false;
2989
+ const effortMap = thinking.effortMap;
2990
+ if (!effortMap) return false;
2991
+ for (const effort of thinking.efforts) {
2992
+ if (effortMap[effort] !== "adaptive") return false;
2993
+ }
2994
+ return thinking.efforts.length > 0;
2995
+ }
2996
+
2997
+ function resolveAnthropicAdaptiveEffort(
2998
+ model: Model<"anthropic-messages">,
2999
+ options: AnthropicOptions,
3000
+ ): AnthropicEffort | undefined {
3001
+ if (options.effort) return usesAdaptiveThinkingTagOnly(model) ? "adaptive" : options.effort;
3002
+ const requestedEffort = options.reasoning;
3003
+ if (!requestedEffort) return undefined;
3004
+ return mapEffortToAnthropicAdaptiveEffort(model, requestedEffort);
3005
+ }
3006
+
3007
+ function extractClaudeCodeFirstUserMessageText(messages: readonly Message[]): string {
3008
+ for (const message of messages) {
3009
+ if (message.role !== "user") continue;
3010
+ const { content } = message;
3011
+ if (typeof content === "string") return content;
3012
+ if (!Array.isArray(content)) return "";
3013
+ for (const block of content) {
3014
+ if (block.type === "text") return block.text;
3015
+ }
3016
+ return "";
3017
+ }
3018
+ return "";
3019
+ }
3020
+
3021
+ function buildParams(
3022
+ model: Model<"anthropic-messages">,
3023
+ context: Context,
3024
+ isOAuthToken: boolean,
3025
+ options?: AnthropicOptions,
3026
+ disableStrictTools = false,
3027
+ useUmansGatewayWebSearch = false,
3028
+ ): MessageCreateParamsStreaming {
3029
+ const { cacheControl } = getCacheControl(model, options?.cacheRetention, isOAuthToken);
3030
+
3031
+ // Pre-compute system blocks so they occupy the right slot in the serialized body.
3032
+ const shouldInjectClaudeCodeInstruction = isOAuthToken && !model.id.startsWith("claude-3-5-haiku");
3033
+ const firstUserMessageText = shouldInjectClaudeCodeInstruction
3034
+ ? extractClaudeCodeFirstUserMessageText(context.messages)
3035
+ : "";
3036
+ const systemBlocks = buildAnthropicSystemBlocks(context.systemPrompt, {
3037
+ includeClaudeCodeInstruction: shouldInjectClaudeCodeInstruction,
3038
+ firstUserMessageText,
3039
+ });
3040
+
3041
+ // Pre-compute tools.
3042
+ let tools: AnthropicWireTool[] | undefined;
3043
+ if (context.tools) {
3044
+ tools = convertTools(
3045
+ context.tools,
3046
+ isOAuthToken,
3047
+ disableStrictTools || model.provider === "github-copilot",
3048
+ model.compat.supportsEagerToolInputStreaming,
3049
+ model.compat.escapeBuiltinToolNames,
3050
+ useUmansGatewayWebSearch,
3051
+ );
3052
+ } else if (isOAuthToken) {
3053
+ tools = [];
3054
+ }
3055
+
3056
+ // Pre-compute metadata.
3057
+ const metadataAccountId = readAnthropicMetadataAccountId(options?.metadata);
3058
+ const metadataUserId = resolveAnthropicMetadataUserId(
3059
+ options?.metadata?.user_id,
3060
+ isOAuthToken,
3061
+ options?.sessionId,
3062
+ metadataAccountId,
3063
+ );
3064
+ const metadata = metadataUserId ? { user_id: metadataUserId } : undefined;
3065
+
3066
+ // Pre-compute thinking + output_config effort.
3067
+ let thinking: MessageCreateParamsStreaming["thinking"] | undefined;
3068
+ let outputConfigEffort: AnthropicOutputEffort | undefined;
3069
+ if (model.reasoning) {
3070
+ if (options?.thinkingEnabled || model.compat.requiresThinkingEnabled) {
3071
+ const thinkingOptions = options ?? {};
3072
+ const mode = model.thinking?.mode;
3073
+ const effort = resolveAnthropicAdaptiveEffort(model, thinkingOptions);
3074
+ const compat = model.compat;
3075
+ if (mode === "anthropic-adaptive" && !compat.disableAdaptiveThinking) {
3076
+ const adaptive: { type: "adaptive"; display?: AnthropicThinkingDisplay } = { type: "adaptive" };
3077
+ // Starting with Claude Opus 4.7 and Claude Fable/Mythos 5, adaptive thinking
3078
+ // content is omitted from the response by default. Opt into summarized
3079
+ // reasoning so thinking deltas keep streaming with human-readable content for
3080
+ // callers that rely on it. The `display` field is gated strictly on model
3081
+ // support: Opus 4.6 / Sonnet 4.6+ reject it with a 400, so an explicit
3082
+ // `thinkingDisplay` MUST NOT force it onto a model that can't accept it.
3083
+ if (model.thinking?.supportsDisplay) {
3084
+ adaptive.display = thinkingOptions.thinkingDisplay ?? "summarized";
3085
+ }
3086
+ thinking = adaptive;
3087
+ if (effort && effort !== "adaptive") outputConfigEffort = effort;
3088
+ } else {
3089
+ thinking = {
3090
+ type: "enabled",
3091
+ budget_tokens: thinkingOptions.thinkingBudgetTokens || 1024,
3092
+ display: thinkingOptions.thinkingDisplay ?? "summarized",
3093
+ };
3094
+ if (mode === "anthropic-budget-effort" && effort && effort !== "adaptive") outputConfigEffort = effort;
3095
+ }
3096
+ } else if (options?.thinkingEnabled === false) {
3097
+ const compat = model.compat;
3098
+ if (
3099
+ model.thinking?.mode === "anthropic-adaptive" &&
3100
+ !compat.disableAdaptiveThinking &&
3101
+ !usesAdaptiveThinkingTagOnly(model)
3102
+ ) {
3103
+ // Adaptive-only Claude models (Opus 4.6+, Sonnet 4.6+, Fable/Mythos 5) reject
3104
+ // `thinking.type: "disabled"` — adaptive thinking cannot be switched off.
3105
+ // Omit the thinking field (the API defaults to adaptive) and pin the
3106
+ // lowest effort so "thinking off" calls stay cheap instead of failing
3107
+ // the request with a 400 (a hidden-thinking toggle must never break it).
3108
+ outputConfigEffort = "low";
3109
+ } else {
3110
+ thinking = { type: "disabled" };
3111
+ }
3112
+ }
3113
+ }
3114
+
3115
+ // Pre-compute context_management. Send keep: "all" for every enabled or
3116
+ // adaptive thinking request (OAuth + API-key) — not just OAuth. Without
3117
+ // this directive Anthropic-compatible backends (Z.AI, Kimi, DeepSeek, …)
3118
+ // strip the replayed thinking blocks `replayUnsignedThinking` puts back
3119
+ // on the wire, so the model loses the prior reasoning chain across turns
3120
+ // and the KV cache misses every turn (#3288). Narrowing this guard back
3121
+ // to `isOAuthToken` regresses every API-key thinking provider. Skip
3122
+ // injected clients because this code cannot add the required
3123
+ // `context-management-2025-06-27` beta to caller-owned SDK clients. Skip
3124
+ // Copilot because its proxy strips Anthropic betas and demotes thinking
3125
+ // blocks to text upstream, so `keep: "all"` is a no-op that risks proxy
3126
+ // rejection of an unrecognized field. Skip Vertex rawPredict because that
3127
+ // adapter requires betas in the JSON body (`anthropic_beta`) instead of the
3128
+ // Anthropic HTTP beta header this code can add.
3129
+ const shouldKeepThinkingContext =
3130
+ !options?.client &&
3131
+ model.provider !== "github-copilot" &&
3132
+ model.provider !== "google-vertex" &&
3133
+ (thinking?.type === "adaptive" || thinking?.type === "enabled");
3134
+ const contextManagement = shouldKeepThinkingContext
3135
+ ? { edits: [{ type: "clear_thinking_20251015" as const, keep: "all" as const }] }
3136
+ : undefined;
3137
+
3138
+ // Pre-compute output_config.
3139
+ const outputConfigEntries: AnthropicOutputConfig = {};
3140
+ if (outputConfigEffort) outputConfigEntries.effort = outputConfigEffort;
3141
+ if (options?.taskBudget) outputConfigEntries.task_budget = options.taskBudget;
3142
+ const outputConfig = Object.keys(outputConfigEntries).length ? outputConfigEntries : undefined;
3143
+
3144
+ // Claude Code requests at most 64k output tokens; clamp only OAuth requests,
3145
+ // where the wire fingerprint must match. API-key callers keep the full model
3146
+ // ceiling (e.g. 128k on Opus 4.8).
3147
+ const modelMaxTokens = model.maxTokens ?? CLAUDE_CODE_MAX_OUTPUT_TOKENS;
3148
+ const maxOutputTokens = isOAuthToken ? Math.min(CLAUDE_CODE_MAX_OUTPUT_TOKENS, modelMaxTokens) : modelMaxTokens;
3149
+
3150
+ // Build params in the canonical field order: model → messages → system → tools →
3151
+ // metadata → max_tokens → thinking → context_management → output_config → stream.
3152
+ const params: MessageCreateParamsStreaming = {
3153
+ model: options?.requestModelId ?? model.requestModelId ?? model.id,
3154
+ messages: convertAnthropicMessages(context.messages, model, isOAuthToken, {
3155
+ serverSideFallbackEnabled: !!options?.fallbacks?.length,
3156
+ }),
3157
+ ...(systemBlocks && { system: systemBlocks }),
3158
+ ...(tools !== undefined && { tools }),
3159
+ ...(metadata && { metadata }),
3160
+ max_tokens: Math.min(maxOutputTokens, options?.maxTokens || modelMaxTokens),
3161
+ ...(thinking && { thinking }),
3162
+ ...(contextManagement && { context_management: contextManagement }),
3163
+ ...(outputConfig && { output_config: outputConfig }),
3164
+ ...(options?.fallbacks?.length ? { fallbacks: options.fallbacks } : {}),
3165
+ stream: true,
3166
+ };
3167
+
3168
+ // Opus 4.7+ and Fable/Mythos 5 reject non-default sampling parameters with 400 error.
3169
+ const thinkingType = params.thinking?.type;
3170
+ const allowSamplingParams =
3171
+ model.compat.supportsSamplingParams && (thinkingType === undefined || thinkingType === "disabled");
3172
+ if (allowSamplingParams && options?.temperature !== undefined) {
3173
+ params.temperature = options.temperature;
3174
+ }
3175
+ if (allowSamplingParams && options?.topP !== undefined) {
3176
+ params.top_p = options.topP;
3177
+ }
3178
+ if (allowSamplingParams && options?.topK !== undefined) {
3179
+ params.top_k = options.topK;
3180
+ }
3181
+ if (options?.stopSequences?.length) {
3182
+ const seqs = options.stopSequences;
3183
+ if (seqs.length > ANTHROPIC_STOP_SEQUENCES_MAX && !warnedStopSequencesTrim) {
3184
+ warnedStopSequencesTrim = true;
3185
+ logger.warn("anthropic: stop_sequences exceeds 4; extra entries dropped", {
3186
+ received: seqs.length,
3187
+ kept: ANTHROPIC_STOP_SEQUENCES_MAX,
3188
+ });
3189
+ }
3190
+ params.stop_sequences =
3191
+ seqs.length > ANTHROPIC_STOP_SEQUENCES_MAX ? seqs.slice(0, ANTHROPIC_STOP_SEQUENCES_MAX) : seqs;
3192
+ }
3193
+
3194
+ if (model.provider === "anthropic" && options?.serviceTier === "priority") {
3195
+ params.speed = "fast";
3196
+ }
3197
+
3198
+ if (options?.toolChoice) {
3199
+ if (typeof options.toolChoice === "string") {
3200
+ params.tool_choice = { type: options.toolChoice };
3201
+ } else if (options.toolChoice.name) {
3202
+ params.tool_choice = {
3203
+ ...options.toolChoice,
3204
+ name: encodeAnthropicToolName(
3205
+ options.toolChoice.name,
3206
+ isOAuthToken,
3207
+ model.compat.escapeBuiltinToolNames,
3208
+ useUmansGatewayWebSearch,
3209
+ ),
3210
+ };
3211
+ }
3212
+ // Claude Fable/Mythos 5 reject forced tool use outright ("tool_choice forces
3213
+ // tool use is not compatible with this model"). Downgrade any/tool → auto so the
3214
+ // request succeeds; the tool stays available and the caller's prompt steers
3215
+ // the model toward it.
3216
+ const choiceType = params.tool_choice?.type;
3217
+ if ((choiceType === "any" || choiceType === "tool") && !model.compat.supportsForcedToolChoice) {
3218
+ params.tool_choice = { type: "auto" };
3219
+ }
3220
+ }
3221
+
3222
+ disableThinkingIfToolChoiceForced(params);
3223
+ ensureMaxTokensForThinking(params, maxOutputTokens);
3224
+ applyPromptCaching(params, cacheControl);
3225
+ enforceCacheControlLimit(params, 4);
3226
+ normalizeCacheControlTtlOrdering(params);
3227
+
3228
+ return params;
3229
+ }
3230
+
3231
+ const EMPTY_ERROR_TOOL_RESULT_TEXT = "Tool failed with no output.";
3232
+
3233
+ function isEmptyToolResultWireContent(content: AnthropicToolResultContent): boolean {
3234
+ if (typeof content === "string") {
3235
+ return content.trim().length === 0;
3236
+ }
3237
+ return content.length === 0;
3238
+ }
3239
+
3240
+ function ensureErrorToolResultWireContent(
3241
+ content: AnthropicToolResultContent,
3242
+ isError: boolean | undefined,
3243
+ ): AnthropicToolResultContent {
3244
+ if (!isError || !isEmptyToolResultWireContent(content)) {
3245
+ return content;
3246
+ }
3247
+ return typeof content === "string"
3248
+ ? EMPTY_ERROR_TOOL_RESULT_TEXT
3249
+ : [{ type: "text", text: EMPTY_ERROR_TOOL_RESULT_TEXT }];
3250
+ }
3251
+
3252
+ function buildToolResultBlock(
3253
+ model: Model<"anthropic-messages">,
3254
+ msg: ToolResultMessage,
3255
+ hoistedImages: ContentBlockParam[],
3256
+ ): ContentBlockParam {
3257
+ let content = convertContentBlocks(msg.content, model.input.includes("image"));
3258
+ // Anthropic rejects images inside error tool results ("all content must be
3259
+ // type `text` if `is_error` is true") — keep the text in the block and
3260
+ // hoist the images after the message's tool_result run.
3261
+ if (msg.isError && typeof content !== "string" && content.some(block => block.type === "image")) {
3262
+ for (const block of content) {
3263
+ if (block.type === "image") hoistedImages.push(block);
3264
+ }
3265
+ content = content.filter(block => block.type === "text");
3266
+ }
3267
+ content = ensureErrorToolResultWireContent(content, msg.isError);
3268
+ const block: ContentBlockParam = {
3269
+ type: "tool_result",
3270
+ tool_use_id: msg.toolCallId,
3271
+ content,
3272
+ is_error: msg.isError,
3273
+ };
3274
+ if (model.compat.requiresToolResultId) {
3275
+ // Z.AI workaround (issue #814): include `id` aliased to `tool_use_id`.
3276
+ (block as unknown as Record<string, unknown>).id = msg.toolCallId;
3277
+ }
3278
+ return block;
3279
+ }
3280
+
3281
+ /**
3282
+ * A single Anthropic conversation turn, including the mid-conversation
3283
+ * `system` role (Opus 4.8+ and Fable/Mythos 5).
3284
+ */
3285
+ export type AnthropicMessageParam = MessageParam;
3286
+
3287
+ /**
3288
+ * Recursively replace lone surrogates in string leaves. Identity-preserving:
3289
+ * returns the input object/array when nothing changed.
3290
+ */
3291
+ function toWellFormedDeep(value: unknown): unknown {
3292
+ if (typeof value === "string") {
3293
+ const wellFormed = value.toWellFormed();
3294
+ return wellFormed === value ? value : wellFormed;
3295
+ }
3296
+ if (Array.isArray(value)) {
3297
+ let changed = false;
3298
+ const next = value.map(entry => {
3299
+ const sanitized = toWellFormedDeep(entry);
3300
+ if (sanitized !== entry) changed = true;
3301
+ return sanitized;
3302
+ });
3303
+ return changed ? next : value;
3304
+ }
3305
+ if (isRecord(value)) {
3306
+ let changed = false;
3307
+ const next: Record<string, unknown> = {};
3308
+ for (const [key, entry] of Object.entries(value)) {
3309
+ const sanitized = toWellFormedDeep(entry);
3310
+ if (sanitized !== entry) changed = true;
3311
+ next[key] = sanitized;
3312
+ }
3313
+ return changed ? next : value;
3314
+ }
3315
+ return value;
3316
+ }
3317
+
3318
+ /**
3319
+ * Serialize omp {@link Message}s to Anthropic wire messages.
3320
+ *
3321
+ * `opts.serverSideFallbackEnabled` — when the CURRENT request itself
3322
+ * opts into the server-side-fallback beta chain. Only then may a persisted
3323
+ * `fallback` content block from a prior turn be replayed on the wire;
3324
+ * otherwise the block is dropped to avoid a 400 on non-fallback requests
3325
+ * that don't send the beta.
3326
+ */
3327
+ export function convertAnthropicMessages(
3328
+ messages: Message[],
3329
+ model: Model<"anthropic-messages">,
3330
+ isOAuthToken: boolean,
3331
+ opts?: { serverSideFallbackEnabled?: boolean },
3332
+ ): AnthropicMessageParam[] {
3333
+ // Indices of params emitted from `developer` messages. After the main pass,
3334
+ // the ones whose placement satisfies Anthropic's mid-conversation rules are
3335
+ // upgraded from the `user` role to the authoritative `system` role.
3336
+ const developerParamIndices: number[] = [];
3337
+ const params: AnthropicMessageParam[] = [];
3338
+
3339
+ const transformedMessages = transformMessages(messages, model, normalizeToolCallId);
3340
+
3341
+ for (let i = 0; i < transformedMessages.length; i++) {
3342
+ const msg = transformedMessages[i];
3343
+
3344
+ if (msg.role === "user" || msg.role === "developer") {
3345
+ if (!msg.content) continue;
3346
+
3347
+ let content: string | ContentBlockParam[];
3348
+ if (typeof msg.content === "string") {
3349
+ if (msg.content.trim().length === 0) continue;
3350
+ content = msg.content.toWellFormed();
3351
+ } else {
3352
+ const contentBlocks = convertContentBlocks(msg.content, model.input.includes("image"));
3353
+ if (typeof contentBlocks === "string") {
3354
+ if (contentBlocks.trim().length === 0) continue;
3355
+ content = contentBlocks;
3356
+ } else {
3357
+ if (contentBlocks.length === 0) continue;
3358
+ content = contentBlocks;
3359
+ }
3360
+ }
3361
+ if (msg.role === "developer") developerParamIndices.push(params.length);
3362
+ params.push({ role: "user", content });
3363
+ } else if (msg.role === "assistant") {
3364
+ const blocks: ContentBlockParam[] = [];
3365
+ const hasSignedThinking = msg.content.some(
3366
+ block =>
3367
+ block.type === "thinking" && !!block.thinkingSignature && block.thinkingSignature.trim().length > 0,
3368
+ );
3369
+
3370
+ for (const block of msg.content) {
3371
+ if (block.type === "text") {
3372
+ if (block.text.trim().length === 0) continue;
3373
+ blocks.push({
3374
+ type: "text",
3375
+ text: block.text.toWellFormed(),
3376
+ });
3377
+ } else if (block.type === "thinking") {
3378
+ if (hasSignedThinking) {
3379
+ if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
3380
+ if (block.thinking.trim().length === 0) continue;
3381
+ blocks.push({
3382
+ type: "text",
3383
+ text: renderDemotedThinking(model.id, block.thinking),
3384
+ });
3385
+ continue;
3386
+ }
3387
+ blocks.push({
3388
+ type: "thinking",
3389
+ thinking: block.thinking,
3390
+ signature: block.thinkingSignature,
3391
+ });
3392
+ continue;
3393
+ }
3394
+ if (block.thinking.trim().length === 0) continue;
3395
+ if (!block.thinkingSignature || block.thinkingSignature.trim().length === 0) {
3396
+ if (model.compat.replayUnsignedThinking) {
3397
+ blocks.push({
3398
+ type: "thinking",
3399
+ thinking: block.thinking.toWellFormed(),
3400
+ signature: "",
3401
+ });
3402
+ } else {
3403
+ blocks.push({
3404
+ type: "text",
3405
+ text: renderDemotedThinking(model.id, block.thinking),
3406
+ });
3407
+ }
3408
+ } else {
3409
+ blocks.push({
3410
+ type: "thinking",
3411
+ thinking: block.thinking.toWellFormed(),
3412
+ signature: block.thinkingSignature,
3413
+ });
3414
+ }
3415
+ } else if (block.type === "redactedThinking") {
3416
+ if (block.data.trim().length === 0) continue;
3417
+ blocks.push({
3418
+ type: "redacted_thinking",
3419
+ data: block.data,
3420
+ });
3421
+ } else if (block.type === "fallback") {
3422
+ // Replay ONLY when both sides are aligned: the current
3423
+ // request opted into the beta chain, and the target is
3424
+ // official Anthropic (the only endpoint that accepts the
3425
+ // block on the wire). `transformMessages` already drops
3426
+ // the block for cross-provider / non-official replays, so
3427
+ // this is defense-in-depth for direct convert calls.
3428
+ if (!opts?.serverSideFallbackEnabled || !model.compat.officialEndpoint) continue;
3429
+ blocks.push({
3430
+ type: "fallback",
3431
+ from: block.from,
3432
+ to: block.to,
3433
+ });
3434
+ } else if (block.type === "toolCall") {
3435
+ blocks.push({
3436
+ type: "tool_use",
3437
+ id: block.id,
3438
+ name: encodeAnthropicToolName(block.name, isOAuthToken, model.compat.escapeBuiltinToolNames),
3439
+ // Always sanitize: the model itself can emit lone-surrogate escapes
3440
+ // in tool-argument JSON (streamed out fine, rejected with a 400 on
3441
+ // replay by Anthropic's strict UTF-8 validation). toWellFormedDeep
3442
+ // is identity-preserving, so well-formed arguments stay
3443
+ // byte-identical and prompt-cache prefixes are unaffected.
3444
+ input: toWellFormedDeep(block.arguments ?? {}),
3445
+ });
3446
+ }
3447
+ }
3448
+ if (blocks.length === 0) continue;
3449
+ params.push({
3450
+ role: "assistant",
3451
+ content: blocks,
3452
+ });
3453
+ } else if (msg.role === "toolResult") {
3454
+ // Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint
3455
+ const toolResults: ContentBlockParam[] = [];
3456
+ // Images stripped out of error tool results, re-attached after the run.
3457
+ const hoistedImages: ContentBlockParam[] = [];
3458
+
3459
+ // Add the current tool result
3460
+ toolResults.push(buildToolResultBlock(model, msg, hoistedImages));
3461
+
3462
+ // Look ahead for consecutive toolResult messages
3463
+ let j = i + 1;
3464
+ while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
3465
+ const nextMsg = transformedMessages[j] as ToolResultMessage; // We know it's a toolResult
3466
+ toolResults.push(buildToolResultBlock(model, nextMsg, hoistedImages));
3467
+ j++;
3468
+ }
3469
+
3470
+ // Skip the messages we've already processed
3471
+ i = j - 1;
3472
+
3473
+ if (hoistedImages.length > 0) {
3474
+ toolResults.push(
3475
+ { type: "text", text: "Attached image(s) from the tool result(s) above:" },
3476
+ ...hoistedImages,
3477
+ );
3478
+ }
3479
+
3480
+ // Add a single user message with all tool results
3481
+ params.push({
3482
+ role: "user",
3483
+ content: toolResults,
3484
+ });
3485
+ }
3486
+ }
3487
+
3488
+ // Upgrade developer-origin params to mid-conversation `system` messages where
3489
+ // Anthropic's placement rules allow it (Opus 4.8+ / Fable/Mythos 5 on first-party API).
3490
+ // Rules: a system message must immediately follow a `user` turn and must be
3491
+ // the last entry or be followed by an `assistant` turn — never first, and
3492
+ // never consecutive. Requiring the next param to be `assistant` (or absent)
3493
+ // covers both the "followed by assistant / last" and "no consecutive system"
3494
+ // constraints. Anything that does not qualify stays a `user` message.
3495
+ if (developerParamIndices.length > 0 && model.compat.supportsMidConversationSystem) {
3496
+ for (const idx of developerParamIndices) {
3497
+ const followsUser = idx > 0 && params[idx - 1]?.role === "user";
3498
+ const next = params[idx + 1];
3499
+ const lastOrBeforeAssistant = idx === params.length - 1 || next?.role === "assistant";
3500
+ // System content is text-only on the wire; a developer turn carrying
3501
+ // image blocks must stay a `user` message or the API rejects it.
3502
+ const content = params[idx].content;
3503
+ const textOnly = typeof content === "string" || content.every(block => block.type === "text");
3504
+ if (followsUser && lastOrBeforeAssistant && textOnly) {
3505
+ params[idx] = { role: "system", content };
3506
+ }
3507
+ }
3508
+ }
3509
+ // Dropped empty user/developer turns can leave two assistant params adjacent;
3510
+ // the API rejects consecutive assistant messages. Repair with the same neutral
3511
+ // nudge used for trailing-assistant prefill below.
3512
+ for (let i = params.length - 1; i > 0; i--) {
3513
+ if (params[i].role === "assistant" && params[i - 1]?.role === "assistant") {
3514
+ params.splice(i, 0, { role: "user", content: "Continue." });
3515
+ }
3516
+ }
3517
+ if (params.length > 0 && params[params.length - 1]?.role === "assistant") {
3518
+ params.push({ role: "user", content: "Continue." });
3519
+ }
3520
+
3521
+ return params;
3522
+ }
3523
+
3524
+ /**
3525
+ * JSON Schema whitelist for Anthropic tool `input_schema` nodes.
3526
+ *
3527
+ * Tracks the Anthropic Python SDK's `lib/_parse/_transform.py::transform_schema`,
3528
+ * with live Messages API guardrails for keywords the SDK preserves but the API rejects.
3529
+ * We keep only structural/metadata keywords Anthropic's validator honors, and demote
3530
+ * anything else into the node's `description` as `\n\n{key: value, ...}` so the model
3531
+ * still sees the constraint as a natural-language hint.
3532
+ *
3533
+ * `Set` (not `Record<string, true>`) because membership is probed against arbitrary
3534
+ * user/Zod-derived schema keys: a literal Record would falsely match prototype names
3535
+ * like `"toString"` and silently strip valid properties.
3536
+ */
3537
+ const ANTHROPIC_TOOL_SCHEMA_UNIVERSAL_KEEP = new Set([
3538
+ "$ref",
3539
+ "$defs",
3540
+ "$schema",
3541
+ "definitions",
3542
+ "type",
3543
+ "anyOf",
3544
+ "allOf",
3545
+ "enum",
3546
+ "const",
3547
+ "description",
3548
+ "title",
3549
+ "default",
3550
+ "nullable",
3551
+ ]);
3552
+ /** Keys preserved on `type: "object"` nodes (in addition to the universal set). */
3553
+ const ANTHROPIC_TOOL_SCHEMA_OBJECT_KEEP = new Set(["properties", "required", "additionalProperties"]);
3554
+ /** Keys preserved on `type: "array"` nodes; `minItems` only when its value is 0 or 1. */
3555
+ const ANTHROPIC_TOOL_SCHEMA_ARRAY_KEEP = new Set(["items", "prefixItems", "minItems"]);
3556
+ /** Keys preserved on `type: "string"` nodes; `format` only when its value is in the supported list. */
3557
+ const ANTHROPIC_TOOL_SCHEMA_STRING_KEEP = new Set(["format"]);
3558
+ /**
3559
+ * String `format` values Anthropic accepts; everything else (including `pattern`-style
3560
+ * format hints) gets demoted into `description`. Matches `SupportedStringFormats` in the
3561
+ * Anthropic SDK's `_transform.py`.
3562
+ */
3563
+ const ANTHROPIC_TOOL_SCHEMA_STRING_FORMATS = new Set([
3564
+ "date-time",
3565
+ "time",
3566
+ "date",
3567
+ "duration",
3568
+ "email",
3569
+ "hostname",
3570
+ "uri",
3571
+ "ipv4",
3572
+ "ipv6",
3573
+ "uuid",
3574
+ ]);
3575
+ const ANTHROPIC_STRICT_TOOL_ALLOWLIST = new Set(["bash", "python", "edit", "find"]);
3576
+ const MAX_ANTHROPIC_STRICT_TOOLS = 20;
3577
+ const MAX_ANTHROPIC_STRICT_OPTIONAL_PARAMETERS = 24;
3578
+ const MAX_ANTHROPIC_STRICT_UNION_PARAMETERS = 16;
3579
+
3580
+ /** `minItems` / `maxItems` apply to arrays; Anthropic rejects them on `type: "object"` (including `minItems: 0`/`1`). */
3581
+ function isJsonSchemaArrayNode(schema: Record<string, unknown>): boolean {
3582
+ const t = schema.type;
3583
+ if (t === "array") return true;
3584
+ if (Array.isArray(t) && t.includes("array") && !t.includes("object")) return true;
3585
+ if (schema.items !== undefined || Array.isArray(schema.prefixItems)) return true;
3586
+ return false;
3587
+ }
3588
+
3589
+ function isJsonSchemaObjectNode(schema: Record<string, unknown>): boolean {
3590
+ if (isJsonSchemaArrayNode(schema)) return false;
3591
+ if (schema.type === "object") return true;
3592
+ if (Array.isArray(schema.type) && schema.type.includes("object")) return true;
3593
+ if (isRecord(schema.properties)) return true;
3594
+ return false;
3595
+ }
3596
+
3597
+ /**
3598
+ * Pick the principal non-null scalar type from a `type` keyword. Anthropic accepts
3599
+ * `type` as either a single string or an array (e.g. `["number", "null"]` for a
3600
+ * nullable value); the SDK whitelist is keyed off the scalar type, with `"null"`
3601
+ * ignored so nullable variants are normalized as their underlying type.
3602
+ */
3603
+ function pickAnthropicScalarType(type: unknown): string | undefined {
3604
+ if (typeof type === "string") return type;
3605
+ if (Array.isArray(type)) {
3606
+ for (const entry of type) {
3607
+ if (typeof entry === "string" && entry !== "null") return entry;
3608
+ }
3609
+ }
3610
+ return undefined;
3611
+ }
3612
+ function pickAnthropicEffectiveScalarType(schema: Record<string, unknown>): string | undefined {
3613
+ const explicit = pickAnthropicScalarType(schema.type);
3614
+ if (explicit) return explicit;
3615
+ if (isRecord(schema.properties)) return "object";
3616
+ if (schema.items !== undefined || Array.isArray(schema.prefixItems)) return "array";
3617
+ return undefined;
3618
+ }
3619
+
3620
+ function anthropicPerTypeKeep(scalarType: string | undefined): Set<string> | undefined {
3621
+ switch (scalarType) {
3622
+ case "object":
3623
+ return ANTHROPIC_TOOL_SCHEMA_OBJECT_KEEP;
3624
+ case "array":
3625
+ return ANTHROPIC_TOOL_SCHEMA_ARRAY_KEEP;
3626
+ case "string":
3627
+ return ANTHROPIC_TOOL_SCHEMA_STRING_KEEP;
3628
+ default:
3629
+ return undefined;
3630
+ }
3631
+ }
3632
+
3633
+ /**
3634
+ * Normalize a JSON Schema node for Anthropic tool `input_schema`.
3635
+ *
3636
+ * Applies the full whitelist semantics from the Anthropic Python SDK's
3637
+ * `lib/_parse/_transform.py::transform_schema`:
3638
+ *
3639
+ * 1. Universal keys (`$ref`, `$defs`, `type`, `anyOf`, `allOf`, `enum`, `const`,
3640
+ * `description`, `title`, `default`, `nullable`) are preserved on every node, with
3641
+ * one position-dependent exception: the combinator keys. Root `anyOf`/`allOf` are
3642
+ * spilled (recent Anthropic Messages validators reject combinators at the tool
3643
+ * `input_schema` root) but kept when nested; `oneOf` is spilled at every position
3644
+ * (it is not in the documented supported subset).
3645
+ * 2. Per-type keys are kept additively (object → `properties`/`required`/`additionalProperties`,
3646
+ * array → `items`/`prefixItems` plus `minItems` only when 0 or 1, string → `format`
3647
+ * only when in the supported value set).
3648
+ * 3. Everything else is demoted into the node's `description` as `\n\n{key: value, ...}`
3649
+ *
3650
+ * Object nodes default to `additionalProperties: false`, but explicit open-map
3651
+ * declarations (`additionalProperties: true` or a schema literal — Zod's
3652
+ * `z.record(z.string(), z.unknown())` produces `{}`) are preserved. The strict-mode
3653
+ * pass downstream demotes those shapes to non-strict instead of fabricating a closed
3654
+ * object, so callers like the resolve tool keep working open-map semantics.
3655
+ */
3656
+ function normalizeAnthropicToolSchemaNode(
3657
+ schema: unknown,
3658
+ cache: WeakMap<Record<string, unknown>, Record<string, unknown>>,
3659
+ isRoot = false,
3660
+ ): unknown {
3661
+ if (Array.isArray(schema)) return schema.map(entry => normalizeAnthropicToolSchemaNode(entry, cache));
3662
+ if (!isRecord(schema)) return schema;
3663
+
3664
+ const existing = cache.get(schema);
3665
+ if (existing !== undefined) return existing;
3666
+
3667
+ const result: Record<string, unknown> = {};
3668
+ cache.set(schema, result);
3669
+
3670
+ const scalarType = pickAnthropicEffectiveScalarType(schema);
3671
+ const perTypeKeep = anthropicPerTypeKeep(scalarType);
3672
+ const spill: Array<[string, unknown]> = [];
3673
+
3674
+ for (const key in schema) {
3675
+ if (!Object.hasOwn(schema, key)) continue;
3676
+ const value = schema[key];
3677
+ const isRootCombinator = isRoot && COMBINATOR_KEYS.includes(key as (typeof COMBINATOR_KEYS)[number]);
3678
+ if (!isRootCombinator && (ANTHROPIC_TOOL_SCHEMA_UNIVERSAL_KEEP.has(key) || perTypeKeep?.has(key))) {
3679
+ result[key] = value;
3680
+ } else {
3681
+ spill.push([key, value]);
3682
+ }
3683
+ }
3684
+
3685
+ // Per-type conditional keys: prune within the kept set.
3686
+ if (scalarType === "string") {
3687
+ const format = result.format;
3688
+ if (typeof format === "string" && !ANTHROPIC_TOOL_SCHEMA_STRING_FORMATS.has(format)) {
3689
+ spill.push(["format", format]);
3690
+ delete result.format;
3691
+ }
3692
+ }
3693
+ if (scalarType === "array" && result.minItems !== undefined) {
3694
+ const minItems = result.minItems;
3695
+ if (!(typeof minItems === "number" && (minItems === 0 || minItems === 1))) {
3696
+ spill.push(["minItems", minItems]);
3697
+ delete result.minItems;
3698
+ }
3699
+ }
3700
+ if (scalarType === "object" && result.additionalProperties === undefined) {
3701
+ result.additionalProperties = false;
3702
+ }
3703
+
3704
+ // Recurse on structural keys.
3705
+ if (isRecord(result.properties)) {
3706
+ const normalizedProperties: Record<string, unknown> = {};
3707
+ const sourceProperties = result.properties as Record<string, unknown>;
3708
+ for (const propName in sourceProperties) {
3709
+ if (!Object.hasOwn(sourceProperties, propName)) continue;
3710
+ normalizedProperties[propName] = normalizeAnthropicToolSchemaNode(sourceProperties[propName], cache);
3711
+ }
3712
+ result.properties = normalizedProperties;
3713
+ }
3714
+ if (isRecord(result.additionalProperties)) {
3715
+ const normalized = normalizeAnthropicToolSchemaNode(result.additionalProperties, cache);
3716
+ if (isRecord(normalized) && Object.keys(normalized).length === 0) {
3717
+ result.additionalProperties = true;
3718
+ } else {
3719
+ result.additionalProperties = normalized;
3720
+ }
3721
+ }
3722
+ if (Array.isArray(result.items)) {
3723
+ result.items = result.items.map(item => normalizeAnthropicToolSchemaNode(item, cache));
3724
+ } else if (isRecord(result.items)) {
3725
+ result.items = normalizeAnthropicToolSchemaNode(result.items, cache);
3726
+ }
3727
+ if (Array.isArray(result.prefixItems)) {
3728
+ result.prefixItems = result.prefixItems.map(item => normalizeAnthropicToolSchemaNode(item, cache));
3729
+ }
3730
+ for (const key of COMBINATOR_KEYS) {
3731
+ const variants = result[key];
3732
+ if (Array.isArray(variants)) {
3733
+ result[key] = variants.map(variant => normalizeAnthropicToolSchemaNode(variant, cache));
3734
+ }
3735
+ }
3736
+ for (const defsKey of ["$defs", "definitions"] as const) {
3737
+ const definitions = result[defsKey];
3738
+ if (!isRecord(definitions)) continue;
3739
+ const normalizedDefs: Record<string, unknown> = {};
3740
+ const sourceDefs = definitions as Record<string, unknown>;
3741
+ for (const name in sourceDefs) {
3742
+ if (!Object.hasOwn(sourceDefs, name)) continue;
3743
+ normalizedDefs[name] = normalizeAnthropicToolSchemaNode(sourceDefs[name], cache);
3744
+ }
3745
+ result[defsKey] = normalizedDefs;
3746
+ }
3747
+
3748
+ spillToDescription(result, spill);
3749
+ return result;
3750
+ }
3751
+
3752
+ export function normalizeAnthropicToolSchema(schema: unknown): unknown {
3753
+ return normalizeAnthropicToolSchemaNode(schema, new WeakMap(), true);
3754
+ }
3755
+
3756
+ type AnthropicToolSchemaPlan = {
3757
+ inputSchema: AnthropicToolInputSchema;
3758
+ strict: boolean;
3759
+ };
3760
+
3761
+ type AnthropicStrictBudget = {
3762
+ optionalRemaining: number;
3763
+ unionRemaining: number;
3764
+ optionalCount: number;
3765
+ unionCount: number;
3766
+ };
3767
+
3768
+ function hasAnthropicUnionType(schema: Record<string, unknown>): boolean {
3769
+ return Array.isArray(schema.type) || Array.isArray(schema.anyOf);
3770
+ }
3771
+
3772
+ function hasNullVariant(schema: Record<string, unknown>): boolean {
3773
+ if (Array.isArray(schema.type) && schema.type.includes("null")) return true;
3774
+ return Array.isArray(schema.anyOf) && schema.anyOf.some(variant => isRecord(variant) && variant.type === "null");
3775
+ }
3776
+ function hasAnthropicSchemaDefiningKeyword(schema: Record<string, unknown>): boolean {
3777
+ if (
3778
+ schema.type !== undefined ||
3779
+ schema.properties !== undefined ||
3780
+ schema.additionalProperties !== undefined ||
3781
+ schema.items !== undefined ||
3782
+ schema.prefixItems !== undefined ||
3783
+ schema.enum !== undefined ||
3784
+ schema.const !== undefined ||
3785
+ schema.$ref !== undefined
3786
+ ) {
3787
+ return true;
3788
+ }
3789
+ for (const key of COMBINATOR_KEYS) {
3790
+ if (schema[key] !== undefined) return true;
3791
+ }
3792
+ return schema.$defs !== undefined || schema.definitions !== undefined;
3793
+ }
3794
+
3795
+ function makeAnthropicNullableSchema(schema: unknown, budget: AnthropicStrictBudget): unknown | undefined {
3796
+ if (isRecord(schema)) {
3797
+ if (hasNullVariant(schema)) return schema;
3798
+ if (Array.isArray(schema.anyOf)) {
3799
+ return { ...schema, anyOf: [...schema.anyOf, { type: "null" }] };
3800
+ }
3801
+ if (Array.isArray(schema.type)) {
3802
+ return { ...schema, type: [...schema.type, "null"] };
3803
+ }
3804
+ }
3805
+
3806
+ if (budget.unionRemaining <= 0) return undefined;
3807
+ budget.unionRemaining--;
3808
+ budget.unionCount++;
3809
+ return { anyOf: [schema, { type: "null" }] };
3810
+ }
3811
+
3812
+ function normalizeAnthropicStrictSchemaNode(
3813
+ schema: unknown,
3814
+ budget: AnthropicStrictBudget,
3815
+ cache: WeakMap<Record<string, unknown>, Record<string, unknown>>,
3816
+ ): unknown | undefined {
3817
+ if (Array.isArray(schema)) {
3818
+ const result: unknown[] = [];
3819
+ for (const entry of schema) {
3820
+ const normalized = normalizeAnthropicStrictSchemaNode(entry, budget, cache);
3821
+ if (normalized === undefined) return undefined;
3822
+ result.push(normalized);
3823
+ }
3824
+ return result;
3825
+ }
3826
+
3827
+ if (!isRecord(schema)) return schema;
3828
+
3829
+ const cached = cache.get(schema);
3830
+ if (cached) return cached;
3831
+
3832
+ if (!hasAnthropicSchemaDefiningKeyword(schema)) return undefined;
3833
+
3834
+ // Strict tool use only supports closed objects. Open maps stay available on
3835
+ // the non-strict schema plan instead of producing an Anthropic 400.
3836
+ if (isJsonSchemaObjectNode(schema) && schema.additionalProperties !== false) {
3837
+ return undefined;
3838
+ }
3839
+
3840
+ const result: Record<string, unknown> = { ...schema };
3841
+ cache.set(schema, result);
3842
+
3843
+ if (hasAnthropicUnionType(result)) {
3844
+ if (budget.unionRemaining <= 0) return undefined;
3845
+ budget.unionRemaining--;
3846
+ budget.unionCount++;
3847
+ }
3848
+
3849
+ if (isRecord(result.properties)) {
3850
+ const originalRequired = new Set(
3851
+ Array.isArray(result.required)
3852
+ ? result.required.filter((entry): entry is string => typeof entry === "string")
3853
+ : [],
3854
+ );
3855
+ const properties: Record<string, unknown> = {};
3856
+ const required: string[] = [];
3857
+
3858
+ for (const [propertyName, propertySchema] of Object.entries(result.properties)) {
3859
+ const normalizedProperty = normalizeAnthropicStrictSchemaNode(propertySchema, budget, cache);
3860
+ if (normalizedProperty === undefined) return undefined;
3861
+
3862
+ if (originalRequired.has(propertyName)) {
3863
+ properties[propertyName] = normalizedProperty;
3864
+ required.push(propertyName);
3865
+ continue;
3866
+ }
3867
+
3868
+ if (budget.optionalRemaining > 0) {
3869
+ budget.optionalRemaining--;
3870
+ budget.optionalCount++;
3871
+ properties[propertyName] = normalizedProperty;
3872
+ continue;
3873
+ }
3874
+
3875
+ const nullableProperty = makeAnthropicNullableSchema(normalizedProperty, budget);
3876
+ if (nullableProperty === undefined) return undefined;
3877
+ properties[propertyName] = nullableProperty;
3878
+ required.push(propertyName);
3879
+ }
3880
+
3881
+ result.properties = properties;
3882
+ result.required = required;
3883
+ }
3884
+
3885
+ if (Array.isArray(result.items)) {
3886
+ const items = normalizeAnthropicStrictSchemaNode(result.items, budget, cache);
3887
+ if (items === undefined) return undefined;
3888
+ result.items = items;
3889
+ } else if (isRecord(result.items)) {
3890
+ const items = normalizeAnthropicStrictSchemaNode(result.items, budget, cache);
3891
+ if (items === undefined) return undefined;
3892
+ result.items = items;
3893
+ }
3894
+ if (Array.isArray(result.prefixItems)) {
3895
+ const prefixItems = normalizeAnthropicStrictSchemaNode(result.prefixItems, budget, cache);
3896
+ if (prefixItems === undefined) return undefined;
3897
+ result.prefixItems = prefixItems;
3898
+ }
3899
+
3900
+ for (const key of COMBINATOR_KEYS) {
3901
+ const variants = result[key];
3902
+ if (!Array.isArray(variants)) continue;
3903
+ const normalizedVariants = normalizeAnthropicStrictSchemaNode(variants, budget, cache);
3904
+ if (normalizedVariants === undefined) return undefined;
3905
+ result[key] = normalizedVariants;
3906
+ }
3907
+
3908
+ for (const defsKey of ["$defs", "definitions"] as const) {
3909
+ const definitions = result[defsKey];
3910
+ if (!isRecord(definitions)) continue;
3911
+ const normalizedDefinitions: Record<string, unknown> = {};
3912
+ for (const [definitionName, definitionSchema] of Object.entries(definitions)) {
3913
+ const normalizedDefinition = normalizeAnthropicStrictSchemaNode(definitionSchema, budget, cache);
3914
+ if (normalizedDefinition === undefined) return undefined;
3915
+ normalizedDefinitions[definitionName] = normalizedDefinition;
3916
+ }
3917
+ result[defsKey] = normalizedDefinitions;
3918
+ }
3919
+
3920
+ return result;
3921
+ }
3922
+
3923
+ const ANTHROPIC_STRICT_INCOMPATIBLE_KEYWORDS = [
3924
+ "oneOf",
3925
+ "allOf",
3926
+ "$ref",
3927
+ "patternProperties",
3928
+ "propertyNames",
3929
+ ] as const;
3930
+
3931
+ /**
3932
+ * Anthropic's strict grammar subset supports anyOf/type-array unions only.
3933
+ * oneOf/allOf/$ref compile unpredictably (rejections arrive as 400s the
3934
+ * grammar-too-large fallback does not recognize, so they would hard-fail the
3935
+ * turn), and patternProperties/propertyNames describe open key sets that the
3936
+ * strict pipeline's injected `additionalProperties: false` would contradict.
3937
+ * Runs against the raw wire schema — the base normalizer spills several of
3938
+ * these keywords into the description, erasing the evidence.
3939
+ */
3940
+ function hasAnthropicStrictIncompatibleKeyword(schema: unknown, seen = new Set<object>()): boolean {
3941
+ if (Array.isArray(schema)) {
3942
+ if (seen.has(schema)) return false;
3943
+ seen.add(schema);
3944
+ return schema.some(entry => hasAnthropicStrictIncompatibleKeyword(entry, seen));
3945
+ }
3946
+ if (!isRecord(schema)) return false;
3947
+ if (seen.has(schema)) return false;
3948
+ seen.add(schema);
3949
+ for (const keyword of ANTHROPIC_STRICT_INCOMPATIBLE_KEYWORDS) {
3950
+ if (schema[keyword] !== undefined) return true;
3951
+ }
3952
+ return Object.values(schema).some(value => hasAnthropicStrictIncompatibleKeyword(value, seen));
3953
+ }
3954
+
3955
+ function normalizeAnthropicStrictSchema(
3956
+ schema: Record<string, unknown>,
3957
+ optionalRemaining: number,
3958
+ unionRemaining: number,
3959
+ ): { schema: Record<string, unknown>; optionalCount: number; unionCount: number } | undefined {
3960
+ const budget: AnthropicStrictBudget = {
3961
+ optionalRemaining,
3962
+ unionRemaining,
3963
+ optionalCount: 0,
3964
+ unionCount: 0,
3965
+ };
3966
+ const normalized = normalizeAnthropicStrictSchemaNode(schema, budget, new WeakMap());
3967
+ if (!isRecord(normalized)) return undefined;
3968
+ return { schema: normalized, optionalCount: budget.optionalCount, unionCount: budget.unionCount };
3969
+ }
3970
+
3971
+ function buildAnthropicBaseToolInputSchema(tool: Tool): Record<string, unknown> {
3972
+ const jsonSchema = toolWireSchema(tool);
3973
+ return normalizeAnthropicToolSchema({
3974
+ ...jsonSchema,
3975
+ type: "object",
3976
+ properties: isRecord(jsonSchema.properties) ? jsonSchema.properties : {},
3977
+ required: Array.isArray(jsonSchema.required)
3978
+ ? jsonSchema.required.filter((entry): entry is string => typeof entry === "string")
3979
+ : [],
3980
+ }) as Record<string, unknown>;
3981
+ }
3982
+
3983
+ function buildAnthropicToolSchemaPlans(tools: Tool[], disableStrictTools = false): AnthropicToolSchemaPlan[] {
3984
+ const plans = tools.map(
3985
+ (tool): AnthropicToolSchemaPlan => ({
3986
+ inputSchema: buildAnthropicBaseToolInputSchema(tool) as AnthropicToolInputSchema,
3987
+ strict: false,
3988
+ }),
3989
+ );
3990
+ if (NO_STRICT || disableStrictTools) return plans;
3991
+
3992
+ const candidateIndexes = tools.flatMap((tool, index) => {
3993
+ if (!ANTHROPIC_STRICT_TOOL_ALLOWLIST.has(tool.name)) return [];
3994
+ if (tool.strict === false) return [];
3995
+ if (hasAnthropicStrictIncompatibleKeyword(toolWireSchema(tool))) return [];
3996
+ return [index];
3997
+ });
3998
+
3999
+ let strictToolCount = 0;
4000
+ let strictOptionalParameterCount = 0;
4001
+ let strictUnionParameterCount = 0;
4002
+ for (const index of candidateIndexes) {
4003
+ if (strictToolCount >= MAX_ANTHROPIC_STRICT_TOOLS) break;
4004
+
4005
+ const strictResult = normalizeAnthropicStrictSchema(
4006
+ plans[index].inputSchema as Record<string, unknown>,
4007
+ MAX_ANTHROPIC_STRICT_OPTIONAL_PARAMETERS - strictOptionalParameterCount,
4008
+ MAX_ANTHROPIC_STRICT_UNION_PARAMETERS - strictUnionParameterCount,
4009
+ );
4010
+ if (!strictResult) continue;
4011
+
4012
+ plans[index] = {
4013
+ inputSchema: strictResult.schema as AnthropicToolInputSchema,
4014
+ strict: true,
4015
+ };
4016
+ strictToolCount++;
4017
+ strictOptionalParameterCount += strictResult.optionalCount;
4018
+ strictUnionParameterCount += strictResult.unionCount;
4019
+ }
4020
+
4021
+ return plans;
4022
+ }
4023
+
4024
+ function convertTools(
4025
+ tools: Tool[],
4026
+ isOAuthToken: boolean,
4027
+ disableStrictTools = false,
4028
+ supportsEagerToolInputStreaming = true,
4029
+ escapeBuiltinToolNames = false,
4030
+ useUmansGatewayWebSearch = false,
4031
+ ): AnthropicWireTool[] {
4032
+ if (!tools) return [];
4033
+ const schemaPlans = buildAnthropicToolSchemaPlans(tools, disableStrictTools);
4034
+
4035
+ return tools.map((tool, index) => {
4036
+ const plan = schemaPlans[index];
4037
+ const baseTool = {
4038
+ name: encodeAnthropicToolName(tool.name, isOAuthToken, escapeBuiltinToolNames, useUmansGatewayWebSearch),
4039
+ description: tool.description || "",
4040
+ input_schema: plan.inputSchema,
4041
+ };
4042
+ return {
4043
+ ...baseTool,
4044
+ ...(supportsEagerToolInputStreaming ? { eager_input_streaming: true } : {}),
4045
+ ...(plan.strict ? { strict: true } : {}),
4046
+ };
4047
+ });
4048
+ }
4049
+
4050
+ function mapStopReason(reason: string): StopReason {
4051
+ switch (reason) {
4052
+ case "end_turn":
4053
+ return "stop";
4054
+ case "max_tokens":
4055
+ return "length";
4056
+ // Generation ran into the model's context window (default behavior on
4057
+ // Sonnet 4.5+); the streamed content is valid, just truncated.
4058
+ case "model_context_window_exceeded":
4059
+ return "length";
4060
+ case "tool_use":
4061
+ return "toolUse";
4062
+ case "refusal":
4063
+ return "error";
4064
+ case "pause_turn": // Stop is good enough -> resubmit
4065
+ return "stop";
4066
+ case "stop_sequence":
4067
+ return "stop"; // A caller-supplied stop_sequences entry matched; the turn completed normally.
4068
+ case "sensitive": // Content flagged by safety filters (not yet in SDK types)
4069
+ return "error";
4070
+ default:
4071
+ // New stop reasons ship server-side first ("sensitive",
4072
+ // "model_context_window_exceeded") and arrive on the trailing
4073
+ // message_delta after all content has streamed. Degrade to a normal
4074
+ // stop instead of failing the fully streamed turn.
4075
+ reportAnthropicEnvelopeAnomaly(`unhandled stop reason: ${reason}`);
4076
+ return "stop";
4077
+ }
4078
+ }