@zyro-sec/ai 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (828) hide show
  1. package/AGENTS.md +23 -0
  2. package/README.md +1810 -2
  3. package/example/tutorial.ts +284 -0
  4. package/package.json +40 -4
  5. package/script/pack.ts +5 -0
  6. package/script/recording-cost-report.ts +250 -0
  7. package/script/setup-recording-env.ts +602 -0
  8. package/src/ai-client.ts +28 -0
  9. package/src/cache-policy.ts +249 -0
  10. package/src/decision-client.ts +88 -0
  11. package/src/decision.ts +335 -0
  12. package/src/effect.ts +44 -0
  13. package/src/effort-updates.ts +25 -0
  14. package/src/embedding-client.ts +46 -0
  15. package/src/embedding.ts +167 -0
  16. package/src/experimental/cloudflare-run-extraction.ts +112 -0
  17. package/src/experimental/extraction.ts +190 -0
  18. package/src/experimental/realtime.ts +211 -0
  19. package/src/experimental.ts +51 -0
  20. package/src/generation.ts +210 -0
  21. package/src/image-client.ts +89 -0
  22. package/src/image.ts +236 -0
  23. package/src/index.ts +363 -0
  24. package/src/llm.ts +364 -0
  25. package/src/loop.ts +214 -0
  26. package/src/media-client.ts +94 -0
  27. package/src/media-model.ts +129 -0
  28. package/src/media.ts +408 -0
  29. package/src/protocols/alibaba-chat.ts +95 -0
  30. package/src/protocols/alibaba-images.ts +126 -0
  31. package/src/protocols/alibaba-messages.ts +49 -0
  32. package/src/protocols/alibaba-responses.ts +86 -0
  33. package/src/protocols/anthropic-messages.ts +1664 -0
  34. package/src/protocols/assemblyai-transcription.ts +209 -0
  35. package/src/protocols/bedrock-converse.ts +984 -0
  36. package/src/protocols/bedrock-event-stream.ts +147 -0
  37. package/src/protocols/cartesia-speech.ts +182 -0
  38. package/src/protocols/cloudflare-run-decisions.ts +55 -0
  39. package/src/protocols/cloudflare-workers-ai-images.ts +124 -0
  40. package/src/protocols/cohere-chat.ts +353 -0
  41. package/src/protocols/deepgram-speech.ts +148 -0
  42. package/src/protocols/deepgram-transcription.ts +179 -0
  43. package/src/protocols/elevenlabs-speech.ts +207 -0
  44. package/src/protocols/elevenlabs-transcription.ts +211 -0
  45. package/src/protocols/gemini.ts +908 -0
  46. package/src/protocols/google-decisions.ts +179 -0
  47. package/src/protocols/google-embed-content.ts +165 -0
  48. package/src/protocols/google-images.ts +255 -0
  49. package/src/protocols/google-interactions-video.ts +264 -0
  50. package/src/protocols/google-interactions.ts +904 -0
  51. package/src/protocols/google-speech.ts +157 -0
  52. package/src/protocols/google-transcription.ts +206 -0
  53. package/src/protocols/google-vertex-predict-embeddings.ts +104 -0
  54. package/src/protocols/index.ts +21 -0
  55. package/src/protocols/meta-images.ts +162 -0
  56. package/src/protocols/meta-messages.ts +67 -0
  57. package/src/protocols/meta-responses.ts +249 -0
  58. package/src/protocols/minimax-images.ts +215 -0
  59. package/src/protocols/minimax-video.ts +406 -0
  60. package/src/protocols/mistral-chat.ts +768 -0
  61. package/src/protocols/open-responses-channel.ts +198 -0
  62. package/src/protocols/open-responses-continuation.ts +218 -0
  63. package/src/protocols/open-responses.ts +1636 -0
  64. package/src/protocols/openai-chat.ts +1431 -0
  65. package/src/protocols/openai-compatible-chat.ts +22 -0
  66. package/src/protocols/openai-compatible-responses.ts +21 -0
  67. package/src/protocols/openai-decisions.ts +174 -0
  68. package/src/protocols/openai-embeddings.ts +105 -0
  69. package/src/protocols/openai-freeform.ts +287 -0
  70. package/src/protocols/openai-images.ts +442 -0
  71. package/src/protocols/openai-responses.ts +442 -0
  72. package/src/protocols/openai-speech.ts +164 -0
  73. package/src/protocols/openai-transcription.ts +295 -0
  74. package/src/protocols/openrouter-video.ts +215 -0
  75. package/src/protocols/seedance-video.ts +301 -0
  76. package/src/protocols/shared.ts +552 -0
  77. package/src/protocols/system-one.ts +335 -0
  78. package/src/protocols/utils/bedrock-auth.ts +147 -0
  79. package/src/protocols/utils/bedrock-cache.ts +56 -0
  80. package/src/protocols/utils/bedrock-media.ts +121 -0
  81. package/src/protocols/utils/cache.ts +44 -0
  82. package/src/protocols/utils/claude-model.ts +19 -0
  83. package/src/protocols/utils/gateway-protocol.ts +92 -0
  84. package/src/protocols/utils/gemini-generate-content.ts +172 -0
  85. package/src/protocols/utils/gemini-json-schema.ts +77 -0
  86. package/src/protocols/utils/image-sizing.ts +117 -0
  87. package/src/protocols/utils/lifecycle.ts +120 -0
  88. package/src/protocols/utils/media-input.ts +98 -0
  89. package/src/protocols/utils/mistral-tool-id.ts +34 -0
  90. package/src/protocols/utils/open-responses-options.ts +82 -0
  91. package/src/protocols/utils/partial-json-options.ts +62 -0
  92. package/src/protocols/utils/partial-json.ts +283 -0
  93. package/src/protocols/utils/responses-checkpoint.ts +115 -0
  94. package/src/protocols/utils/responses-compaction.ts +210 -0
  95. package/src/protocols/utils/responses-hosted-tools.ts +104 -0
  96. package/src/protocols/utils/speaker-turns.ts +10 -0
  97. package/src/protocols/utils/speech-stream.ts +111 -0
  98. package/src/protocols/utils/tool-schema.ts +115 -0
  99. package/src/protocols/utils/tool-stream.ts +240 -0
  100. package/src/protocols/venice-chat.ts +356 -0
  101. package/src/protocols/xai-images.ts +137 -0
  102. package/src/protocols/xai-responses.ts +106 -0
  103. package/src/protocols/xai-video.ts +232 -0
  104. package/src/protocols/zai-chat.ts +71 -0
  105. package/src/protocols/zai-images.ts +120 -0
  106. package/src/protocols/zai-messages.ts +34 -0
  107. package/src/protocols.ts +1 -0
  108. package/src/provider-error.ts +291 -0
  109. package/src/provider-package.ts +32 -0
  110. package/src/providers/alibaba/chat.ts +1 -0
  111. package/src/providers/alibaba/embeddings.ts +2 -0
  112. package/src/providers/alibaba/images.ts +2 -0
  113. package/src/providers/alibaba/messages.ts +3 -0
  114. package/src/providers/alibaba/responses.ts +3 -0
  115. package/src/providers/alibaba.ts +177 -0
  116. package/src/providers/amazon-bedrock/mantle/chat.ts +2 -0
  117. package/src/providers/amazon-bedrock/mantle/messages.ts +2 -0
  118. package/src/providers/amazon-bedrock/mantle/responses.ts +2 -0
  119. package/src/providers/amazon-bedrock/mantle.ts +2 -0
  120. package/src/providers/amazon-bedrock-mantle.ts +192 -0
  121. package/src/providers/amazon-bedrock.ts +81 -0
  122. package/src/providers/anthropic-compatible.ts +80 -0
  123. package/src/providers/anthropic.ts +66 -0
  124. package/src/providers/assemblyai.ts +32 -0
  125. package/src/providers/azure/chat.ts +2 -0
  126. package/src/providers/azure/embeddings.ts +2 -0
  127. package/src/providers/azure/images.ts +2 -0
  128. package/src/providers/azure/messages.ts +2 -0
  129. package/src/providers/azure/responses.ts +2 -0
  130. package/src/providers/azure.ts +240 -0
  131. package/src/providers/baseten/decisions.ts +2 -0
  132. package/src/providers/baseten.ts +88 -0
  133. package/src/providers/byteplus/videos.ts +2 -0
  134. package/src/providers/byteplus.ts +39 -0
  135. package/src/providers/cartesia.ts +28 -0
  136. package/src/providers/cerebras.ts +56 -0
  137. package/src/providers/cloudflare-ai-gateway/decisions.ts +2 -0
  138. package/src/providers/cloudflare-ai-gateway.ts +199 -0
  139. package/src/providers/cloudflare-shared.ts +29 -0
  140. package/src/providers/cloudflare-workers-ai/decisions.ts +2 -0
  141. package/src/providers/cloudflare-workers-ai/images.ts +2 -0
  142. package/src/providers/cloudflare-workers-ai.ts +104 -0
  143. package/src/providers/cohere/chat.ts +8 -0
  144. package/src/providers/cohere/embeddings.ts +2 -0
  145. package/src/providers/cohere.ts +72 -0
  146. package/src/providers/deepgram.ts +33 -0
  147. package/src/providers/deepinfra/embeddings.ts +2 -0
  148. package/src/providers/deepinfra.ts +76 -0
  149. package/src/providers/deepseek.ts +54 -0
  150. package/src/providers/digitalocean/decisions.ts +2 -0
  151. package/src/providers/digitalocean/embeddings.ts +2 -0
  152. package/src/providers/digitalocean/images.ts +2 -0
  153. package/src/providers/digitalocean/responses.ts +4 -0
  154. package/src/providers/digitalocean.ts +144 -0
  155. package/src/providers/elevenlabs.ts +35 -0
  156. package/src/providers/fireworks/embeddings.ts +2 -0
  157. package/src/providers/fireworks.ts +66 -0
  158. package/src/providers/google/decisions.ts +2 -0
  159. package/src/providers/google/embeddings.ts +2 -0
  160. package/src/providers/google/images.ts +2 -0
  161. package/src/providers/google/interactions.ts +13 -0
  162. package/src/providers/google/videos.ts +2 -0
  163. package/src/providers/google-vertex/chat.ts +2 -0
  164. package/src/providers/google-vertex/embeddings.ts +2 -0
  165. package/src/providers/google-vertex/gemini.ts +2 -0
  166. package/src/providers/google-vertex/images.ts +2 -0
  167. package/src/providers/google-vertex/interactions.ts +2 -0
  168. package/src/providers/google-vertex/messages.ts +2 -0
  169. package/src/providers/google-vertex/mistral.ts +2 -0
  170. package/src/providers/google-vertex/responses.ts +2 -0
  171. package/src/providers/google-vertex-chat.ts +63 -0
  172. package/src/providers/google-vertex-interactions.ts +104 -0
  173. package/src/providers/google-vertex-messages.ts +99 -0
  174. package/src/providers/google-vertex-mistral.ts +79 -0
  175. package/src/providers/google-vertex-responses.ts +64 -0
  176. package/src/providers/google-vertex-shared.ts +176 -0
  177. package/src/providers/google-vertex.ts +228 -0
  178. package/src/providers/google.ts +121 -0
  179. package/src/providers/groq.ts +98 -0
  180. package/src/providers/index.ts +45 -0
  181. package/src/providers/meta/chat.ts +2 -0
  182. package/src/providers/meta/images.ts +2 -0
  183. package/src/providers/meta/messages.ts +2 -0
  184. package/src/providers/meta/responses.ts +2 -0
  185. package/src/providers/meta.ts +161 -0
  186. package/src/providers/minimax/chat.ts +8 -0
  187. package/src/providers/minimax/images.ts +4 -0
  188. package/src/providers/minimax/messages.ts +1 -0
  189. package/src/providers/minimax/responses.ts +8 -0
  190. package/src/providers/minimax/videos.ts +4 -0
  191. package/src/providers/minimax.ts +162 -0
  192. package/src/providers/mistral.ts +40 -0
  193. package/src/providers/moonshot/chat.ts +1 -0
  194. package/src/providers/moonshot/messages.ts +8 -0
  195. package/src/providers/moonshot/responses.ts +8 -0
  196. package/src/providers/moonshot.ts +134 -0
  197. package/src/providers/open-responses-options.ts +4 -0
  198. package/src/providers/openai/chat.ts +2 -0
  199. package/src/providers/openai/embeddings.ts +2 -0
  200. package/src/providers/openai/images.ts +2 -0
  201. package/src/providers/openai/responses.ts +2 -0
  202. package/src/providers/openai-compatible/decisions.ts +2 -0
  203. package/src/providers/openai-compatible/embeddings.ts +2 -0
  204. package/src/providers/openai-compatible/images.ts +2 -0
  205. package/src/providers/openai-compatible/responses.ts +1 -0
  206. package/src/providers/openai-compatible-responses.ts +52 -0
  207. package/src/providers/openai-compatible.ts +108 -0
  208. package/src/providers/openai-options.ts +66 -0
  209. package/src/providers/openai.ts +207 -0
  210. package/src/providers/opencode/decisions.ts +2 -0
  211. package/src/providers/opencode.ts +45 -0
  212. package/src/providers/openrouter/decisions.ts +2 -0
  213. package/src/providers/openrouter/embeddings.ts +2 -0
  214. package/src/providers/openrouter/images.ts +2 -0
  215. package/src/providers/openrouter/responses.ts +3 -0
  216. package/src/providers/openrouter/videos.ts +2 -0
  217. package/src/providers/openrouter.ts +347 -0
  218. package/src/providers/settings.ts +54 -0
  219. package/src/providers/togetherai.ts +53 -0
  220. package/src/providers/typesafe-ai/decisions.ts +2 -0
  221. package/src/providers/typesafe-ai.ts +45 -0
  222. package/src/providers/venice/responses.ts +4 -0
  223. package/src/providers/venice.ts +93 -0
  224. package/src/providers/vercel-ai-gateway/chat.ts +2 -0
  225. package/src/providers/vercel-ai-gateway/decisions.ts +2 -0
  226. package/src/providers/vercel-ai-gateway/embeddings.ts +2 -0
  227. package/src/providers/vercel-ai-gateway/images.ts +2 -0
  228. package/src/providers/vercel-ai-gateway/messages.ts +2 -0
  229. package/src/providers/vercel-ai-gateway/responses.ts +2 -0
  230. package/src/providers/vercel-ai-gateway.ts +300 -0
  231. package/src/providers/volcengine/videos.ts +2 -0
  232. package/src/providers/volcengine.ts +39 -0
  233. package/src/providers/xai/images.ts +2 -0
  234. package/src/providers/xai/videos.ts +2 -0
  235. package/src/providers/xai.ts +125 -0
  236. package/src/providers/zai/chat.ts +1 -0
  237. package/src/providers/zai/images.ts +2 -0
  238. package/src/providers/zai-coding-plan/chat.ts +1 -0
  239. package/src/providers/zai-coding-plan/messages.ts +8 -0
  240. package/src/providers/zai-coding-plan/responses.ts +8 -0
  241. package/src/providers/zai-coding-plan.ts +86 -0
  242. package/src/providers/zai.ts +73 -0
  243. package/src/providers.ts +1 -0
  244. package/src/public.ts +204 -0
  245. package/src/route/auth-options.ts +62 -0
  246. package/src/route/auth.ts +164 -0
  247. package/src/route/client.ts +700 -0
  248. package/src/route/decision.ts +150 -0
  249. package/src/route/embedding.ts +175 -0
  250. package/src/route/endpoint.ts +63 -0
  251. package/src/route/executor-service.ts +191 -0
  252. package/src/route/executor.ts +361 -0
  253. package/src/route/framing.ts +112 -0
  254. package/src/route/index.ts +47 -0
  255. package/src/route/media-protocol.ts +321 -0
  256. package/src/route/media.ts +507 -0
  257. package/src/route/protocol.ts +97 -0
  258. package/src/route/transport/http.ts +103 -0
  259. package/src/route/transport/index.ts +59 -0
  260. package/src/route/transport/websocket-channel.ts +51 -0
  261. package/src/route/transport/websocket.ts +443 -0
  262. package/src/route.ts +1 -0
  263. package/src/schema/errors.ts +246 -0
  264. package/src/schema/events.ts +744 -0
  265. package/src/schema/ids.ts +24 -0
  266. package/src/schema/index.ts +5 -0
  267. package/src/schema/messages.ts +529 -0
  268. package/src/schema/options.ts +445 -0
  269. package/src/speech-client.ts +25 -0
  270. package/src/speech.ts +211 -0
  271. package/src/testing.ts +218 -0
  272. package/src/tool-history.ts +91 -0
  273. package/src/tool-runtime.ts +107 -0
  274. package/src/tool-updates.ts +139 -0
  275. package/src/tool.ts +281 -0
  276. package/src/transcription-client.ts +19 -0
  277. package/src/transcription.ts +234 -0
  278. package/src/utils/bytes.ts +9 -0
  279. package/src/utils/json.ts +5 -0
  280. package/src/utils/media-type.ts +93 -0
  281. package/src/utils/provider-codes.ts +43 -0
  282. package/src/utils/record.ts +3 -0
  283. package/src/utils/sanitize.ts +33 -0
  284. package/src/video-client.ts +66 -0
  285. package/src/video.ts +181 -0
  286. package/test/adapter.test.ts +180 -0
  287. package/test/auth-options.types.ts +283 -0
  288. package/test/auth.test.ts +112 -0
  289. package/test/cache-policy.test.ts +578 -0
  290. package/test/compaction-http.test.ts +96 -0
  291. package/test/compaction.test.ts +107 -0
  292. package/test/compile.test.ts +600 -0
  293. package/test/continuation-scenarios.ts +115 -0
  294. package/test/decision.test.ts +983 -0
  295. package/test/decision.types.ts +117 -0
  296. package/test/effort-updates.test.ts +574 -0
  297. package/test/embedding.test.ts +455 -0
  298. package/test/endpoint.test.ts +58 -0
  299. package/test/error-partial.test.ts +140 -0
  300. package/test/executor.test.ts +1095 -0
  301. package/test/exports.test.ts +222 -0
  302. package/test/extraction.test.ts +227 -0
  303. package/test/fixtures/audio/dialog.mp3 +0 -0
  304. package/test/fixtures/audio/hello.mp3 +0 -0
  305. package/test/fixtures/images/edit-source.jpg +0 -0
  306. package/test/fixtures/media/restroom.png +0 -0
  307. package/test/fixtures/recordings/alibaba-chat/qwen-3-7-plus-streams-thinking-disabled.json +35 -0
  308. package/test/fixtures/recordings/alibaba-chat/qwen-3-7-plus-streams-thinking-enabled.json +35 -0
  309. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-flash-reads-image-bytes.json +34 -0
  310. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-obeys-named-tool-choice.json +35 -0
  311. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-replays-reasoning-through-a-tool-loop-and-follow-up.json +73 -0
  312. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-returns-a-json-object.json +34 -0
  313. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-default-effort.json +36 -0
  314. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-high-effort.json +36 -0
  315. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-low-effort.json +36 -0
  316. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-max-effort.json +36 -0
  317. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-medium-effort.json +36 -0
  318. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-minimal-effort.json +36 -0
  319. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-none-effort.json +36 -0
  320. package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-xhigh-effort.json +36 -0
  321. package/test/fixtures/recordings/alibaba-images/edits-an-image.json +33 -0
  322. package/test/fixtures/recordings/alibaba-images/generates-qwen-image-images.json +33 -0
  323. package/test/fixtures/recordings/alibaba-images/generates-wan-images.json +33 -0
  324. package/test/fixtures/recordings/alibaba-messages/qwen-3-7-plus-streams-thinking-disabled.json +35 -0
  325. package/test/fixtures/recordings/alibaba-messages/qwen-3-7-plus-streams-thinking-enabled.json +35 -0
  326. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-flash-reads-image-bytes.json +34 -0
  327. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-follows-a-json-schema.json +34 -0
  328. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-obeys-named-tool-choice.json +35 -0
  329. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-replays-reasoning-through-a-tool-loop-and-follow-up.json +73 -0
  330. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-default-effort.json +36 -0
  331. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-high-effort.json +36 -0
  332. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-low-effort.json +36 -0
  333. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-max-effort.json +36 -0
  334. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-medium-effort.json +36 -0
  335. package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-xhigh-effort.json +36 -0
  336. package/test/fixtures/recordings/alibaba-responses/qwen-3-7-plus-streams-thinking-disabled.json +35 -0
  337. package/test/fixtures/recordings/alibaba-responses/qwen-3-7-plus-streams-thinking-enabled.json +35 -0
  338. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-flash-reads-image-bytes.json +34 -0
  339. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-continues-a-stored-response.json +53 -0
  340. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-obeys-named-tool-choice.json +35 -0
  341. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-replays-reasoning-through-a-tool-loop-and-follow-up.json +73 -0
  342. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-default-effort.json +36 -0
  343. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-high-effort.json +36 -0
  344. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-low-effort.json +36 -0
  345. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-max-effort.json +36 -0
  346. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-medium-effort.json +36 -0
  347. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-minimal-effort.json +36 -0
  348. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-none-effort.json +36 -0
  349. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-xhigh-effort.json +36 -0
  350. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-uses-hosted-code-interpreter.json +35 -0
  351. package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-uses-hosted-web-search-and-extraction.json +36 -0
  352. package/test/fixtures/recordings/anthropic-compatible-messages/minimax-m3-anthropic-compatible-text.json +40 -0
  353. package/test/fixtures/recordings/anthropic-compatible-messages/minimax-m3-anthropic-compatible-tool-call.json +41 -0
  354. package/test/fixtures/recordings/anthropic-compatible-messages/minimax-m3-anthropic-compatible-tool-loop.json +60 -0
  355. package/test/fixtures/recordings/anthropic-messages/anthropic-opus-4-7-image-tool-result.json +43 -0
  356. package/test/fixtures/recordings/anthropic-messages-cache/keeps-a-long-tool-turn-inside-the-cache-lookback.json +48 -0
  357. package/test/fixtures/recordings/anthropic-messages-cache/writes-then-reads-cache-control-on-identical-second-call.json +48 -0
  358. package/test/fixtures/recordings/assemblyai-transcription/uploads-submits-and-polls-a-transcript.json +98 -0
  359. package/test/fixtures/recordings/azure/chat-streams-text-from-a-foundry-models-url.json +31 -0
  360. package/test/fixtures/recordings/azure/chat-streams-text.json +28 -0
  361. package/test/fixtures/recordings/azure/messages-calls-a-tool.json +31 -0
  362. package/test/fixtures/recordings/azure/messages-streams-text.json +31 -0
  363. package/test/fixtures/recordings/azure/responses-calls-a-tool.json +31 -0
  364. package/test/fixtures/recordings/azure/responses-continues-after-a-tool-result.json +31 -0
  365. package/test/fixtures/recordings/azure/responses-streams-text.json +31 -0
  366. package/test/fixtures/recordings/baseten-decision/decides-choice-score-and-predicate-questions.json +33 -0
  367. package/test/fixtures/recordings/bedrock-converse/continues-after-parallel-tool-results.json +36 -0
  368. package/test/fixtures/recordings/bedrock-converse/drives-a-tool-loop.json +55 -0
  369. package/test/fixtures/recordings/bedrock-converse/streams-a-tool-call.json +29 -0
  370. package/test/fixtures/recordings/bedrock-converse/streams-text.json +29 -0
  371. package/test/fixtures/recordings/bedrock-converse-cache/writes-then-reads-cachepoint-on-identical-second-call.json +48 -0
  372. package/test/fixtures/recordings/bedrock-converse-options/constrains-claude-output-to-a-json-schema.json +33 -0
  373. package/test/fixtures/recordings/bedrock-converse-options/sends-claude-effort-with-adaptive-thinking-service-tier-and-request-metadata.json +34 -0
  374. package/test/fixtures/recordings/bedrock-converse-options/sends-gpt-oss-reasoning-effort-with-the-priority-tier.json +34 -0
  375. package/test/fixtures/recordings/bedrock-converse-options/sends-nova-2-reasoning-effort.json +34 -0
  376. package/test/fixtures/recordings/bedrock-converse-thinking-binding/accepts-the-default-binding-with-no-thinking-configured.json +34 -0
  377. package/test/fixtures/recordings/bedrock-converse-thinking-binding/keeps-a-session-working-after-the-system-prompt-changes.json +71 -0
  378. package/test/fixtures/recordings/bedrock-mantle/streams-text.json +33 -0
  379. package/test/fixtures/recordings/bedrock-mantle-messages/applies-mid-conversation-effort-updates-and-thinking-block-binding-on-opus-5-5.json +57 -0
  380. package/test/fixtures/recordings/bedrock-mantle-messages/lowers-system-updates-to-wrapped-user-text-on-haiku-4-5-with-budget-thinking.json +37 -0
  381. package/test/fixtures/recordings/bedrock-mantle-messages/replays-signed-thinking-through-a-tool-loop-and-native-system-update.json +59 -0
  382. package/test/fixtures/recordings/bedrock-mantle-messages/strips-unsupported-mid-conversation-effort-updates-on-opus-5-0.json +37 -0
  383. package/test/fixtures/recordings/cartesia-speech/generates-speech.json +34 -0
  384. package/test/fixtures/recordings/cartesia-speech/streams-speech-with-timestamps.json +33 -0
  385. package/test/fixtures/recordings/cerebras-chat/cerebras-gpt-oss-120b-text.json +32 -0
  386. package/test/fixtures/recordings/cerebras-chat/cerebras-gpt-oss-120b-tool-call.json +32 -0
  387. package/test/fixtures/recordings/cerebras-chat/cerebras-gpt-oss-120b-tool-loop.json +50 -0
  388. package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-claude-haiku-4-5-text.json +38 -0
  389. package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-claude-haiku-4-5-tool-loop.json +58 -0
  390. package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-openai-gpt-5-nano-text.json +37 -0
  391. package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-openai-gpt-5-nano-tool-loop.json +56 -0
  392. package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
  393. package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-llama-3-1-8b-text.json +32 -0
  394. package/test/fixtures/recordings/cloudflare-ai-gateway-decision/decides-choice-score-and-predicate-questions.json +33 -0
  395. package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
  396. package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-llama-3-1-8b-text.json +32 -0
  397. package/test/fixtures/recordings/cloudflare-workers-ai-decision/decides-choice-score-and-predicate-questions.json +33 -0
  398. package/test/fixtures/recordings/cloudflare-workers-ai-extraction/extracts-invoice-fields.json +33 -0
  399. package/test/fixtures/recordings/cloudflare-workers-ai-images/generates-a-flux-2-image-from-multipart-fields.json +32 -0
  400. package/test/fixtures/recordings/cloudflare-workers-ai-images/generates-a-raw-binary-image.json +33 -0
  401. package/test/fixtures/recordings/cohere/continues-a-native-tool-call.json +49 -0
  402. package/test/fixtures/recordings/cohere/streams-compatible-chat-reasoning.json +31 -0
  403. package/test/fixtures/recordings/cohere/streams-native-text-and-usage.json +31 -0
  404. package/test/fixtures/recordings/cohere/streams-native-thinking-with-a-budget.json +31 -0
  405. package/test/fixtures/recordings/cohere-embedding/embeds-text-through-the-compatibility-api.json +33 -0
  406. package/test/fixtures/recordings/deepgram-speech/generates-speech.json +36 -0
  407. package/test/fixtures/recordings/deepgram-speech/streams-speech.json +36 -0
  408. package/test/fixtures/recordings/deepgram-transcription/splits-utterances-at-speaker-changes.json +32 -0
  409. package/test/fixtures/recordings/deepgram-transcription/transcribes-raw-audio-with-speakers-and-word-timestamps.json +50 -0
  410. package/test/fixtures/recordings/deepinfra-chat/deepinfra-llama-3-3-70b-text.json +32 -0
  411. package/test/fixtures/recordings/deepinfra-chat/deepinfra-llama-3-3-70b-tool-call.json +32 -0
  412. package/test/fixtures/recordings/deepinfra-chat/deepinfra-llama-3-3-70b-tool-loop.json +50 -0
  413. package/test/fixtures/recordings/digitalocean-chat/gpt-nano-continues-a-tool-call-with-cache-markers.json +54 -0
  414. package/test/fixtures/recordings/digitalocean-chat/gpt-nano-reuses-a-cached-prompt.json +53 -0
  415. package/test/fixtures/recordings/digitalocean-chat/haiku-continues-a-tool-call-with-cache-markers.json +54 -0
  416. package/test/fixtures/recordings/digitalocean-chat/haiku-reuses-a-cached-prompt.json +53 -0
  417. package/test/fixtures/recordings/digitalocean-decision/decides-choice-score-and-predicate-questions.json +33 -0
  418. package/test/fixtures/recordings/digitalocean-responses/calls-a-freeform-text-tool.json +35 -0
  419. package/test/fixtures/recordings/digitalocean-responses/continues-a-tool-call.json +53 -0
  420. package/test/fixtures/recordings/digitalocean-responses/streams-text.json +35 -0
  421. package/test/fixtures/recordings/elevenlabs-speech/generates-speech.json +35 -0
  422. package/test/fixtures/recordings/elevenlabs-speech/streams-speech-with-timestamps.json +34 -0
  423. package/test/fixtures/recordings/elevenlabs-transcription/groups-diarized-words-into-speaker-turns.json +32 -0
  424. package/test/fixtures/recordings/elevenlabs-transcription/transcribes-audio-with-word-timestamps.json +50 -0
  425. package/test/fixtures/recordings/gemini/gemini-2-5-flash-image.json +32 -0
  426. package/test/fixtures/recordings/gemini-cache/reports-cachedcontenttokencount-on-identical-second-call.json +46 -0
  427. package/test/fixtures/recordings/google-decision/decides-choice-score-and-predicate-questions.json +33 -0
  428. package/test/fixtures/recordings/google-embedding/embeds-an-image-and-text-into-one-vector-each.json +32 -0
  429. package/test/fixtures/recordings/google-embedding/embeds-text-through-batchembedcontents.json +32 -0
  430. package/test/fixtures/recordings/google-embedding/maps-inputtype-to-a-task-type-for-gemini-embedding-001.json +50 -0
  431. package/test/fixtures/recordings/google-images/edits-an-image.json +28 -0
  432. package/test/fixtures/recordings/google-images/generates-an-image.json +28 -0
  433. package/test/fixtures/recordings/google-interactions/continues-tool-results-with-previous-interaction-id.json +66 -0
  434. package/test/fixtures/recordings/google-interactions/replays-native-tool-results-and-signatures-statelessly.json +50 -0
  435. package/test/fixtures/recordings/google-interactions/streams-reasoning-and-retains-thought-signatures.json +32 -0
  436. package/test/fixtures/recordings/google-interactions/streams-text-and-reports-usage.json +32 -0
  437. package/test/fixtures/recordings/google-interactions-video/generates-a-video.json +96 -0
  438. package/test/fixtures/recordings/google-speech/generates-speech.json +32 -0
  439. package/test/fixtures/recordings/google-speech/streams-speech.json +32 -0
  440. package/test/fixtures/recordings/google-transcription/streams-speaker-turns.json +32 -0
  441. package/test/fixtures/recordings/google-transcription/transcribes-with-speakers-and-word-timestamps.json +32 -0
  442. package/test/fixtures/recordings/google-vertex/calls-a-tool.json +32 -0
  443. package/test/fixtures/recordings/google-vertex/continues-after-a-tool-result.json +32 -0
  444. package/test/fixtures/recordings/google-vertex/streams-text.json +32 -0
  445. package/test/fixtures/recordings/google-vertex-embedding/embeds-gemini-embedding-001-one-text-per-predict-request.json +50 -0
  446. package/test/fixtures/recordings/google-vertex-embedding/embeds-text-through-predict.json +32 -0
  447. package/test/fixtures/recordings/groq-chat/continues-qwen-parallel-tool-calls.json +47 -0
  448. package/test/fixtures/recordings/groq-chat/replays-gpt-oss-reasoning-through-a-tool-loop.json +47 -0
  449. package/test/fixtures/recordings/groq-chat/streams-qwen-parsed-reasoning.json +29 -0
  450. package/test/fixtures/recordings/groq-chat/streams-text-with-usage.json +29 -0
  451. package/test/fixtures/recordings/meta-chat/continues-a-generated-tool-call.json +56 -0
  452. package/test/fixtures/recordings/meta-chat/streams-text-with-default-reasoning.json +37 -0
  453. package/test/fixtures/recordings/meta-chat/streams-text-with-high-reasoning.json +29 -0
  454. package/test/fixtures/recordings/meta-chat/streams-text-with-low-reasoning.json +29 -0
  455. package/test/fixtures/recordings/meta-chat/streams-text-with-max-reasoning.json +29 -0
  456. package/test/fixtures/recordings/meta-chat/streams-text-with-medium-reasoning.json +37 -0
  457. package/test/fixtures/recordings/meta-chat/streams-text-with-minimal-reasoning.json +37 -0
  458. package/test/fixtures/recordings/meta-chat/streams-text-with-xhigh-reasoning.json +29 -0
  459. package/test/fixtures/recordings/meta-images/edits-image-bytes-and-returns-png.json +29 -0
  460. package/test/fixtures/recordings/meta-images/generates-default-webp-bytes.json +29 -0
  461. package/test/fixtures/recordings/meta-images/replays-a-signed-image-handle-for-a-responses-edit.json +55 -0
  462. package/test/fixtures/recordings/meta-messages/replays-encrypted-thinking-through-a-tool-loop.json +54 -0
  463. package/test/fixtures/recordings/meta-messages/streams-text-with-adaptive-thinking.json +36 -0
  464. package/test/fixtures/recordings/meta-messages/streams-text-with-enabled-thinking.json +36 -0
  465. package/test/fixtures/recordings/meta-responses/replays-encrypted-reasoning-through-a-tool-loop.json +56 -0
  466. package/test/fixtures/recordings/meta-responses/streams-text-with-default-reasoning.json +37 -0
  467. package/test/fixtures/recordings/meta-responses/streams-text-with-high-reasoning.json +37 -0
  468. package/test/fixtures/recordings/meta-responses/streams-text-with-low-reasoning.json +37 -0
  469. package/test/fixtures/recordings/meta-responses/streams-text-with-max-reasoning.json +37 -0
  470. package/test/fixtures/recordings/meta-responses/streams-text-with-medium-reasoning.json +37 -0
  471. package/test/fixtures/recordings/meta-responses/streams-text-with-minimal-reasoning.json +37 -0
  472. package/test/fixtures/recordings/meta-responses/streams-text-with-xhigh-reasoning.json +37 -0
  473. package/test/fixtures/recordings/meta-search/searches-and-replays-grounded-messages.json +56 -0
  474. package/test/fixtures/recordings/meta-search/searches-and-replays-grounded-responses.json +56 -0
  475. package/test/fixtures/recordings/minimax-chat/m3-continues-a-tool-loop-with-default-thinking.json +56 -0
  476. package/test/fixtures/recordings/minimax-chat/m3-streams-text-with-thinking-disabled.json +36 -0
  477. package/test/fixtures/recordings/minimax-images/generates-an-image.json +32 -0
  478. package/test/fixtures/recordings/minimax-images/generates-from-a-subject-reference.json +32 -0
  479. package/test/fixtures/recordings/minimax-messages/m2-7-streams-default-thinking.json +36 -0
  480. package/test/fixtures/recordings/minimax-messages/m3-continues-a-tool-loop-with-adaptive-thinking.json +56 -0
  481. package/test/fixtures/recordings/minimax-messages/m3-generates-a-named-tool-call-with-default-thinking-off.json +36 -0
  482. package/test/fixtures/recordings/minimax-messages/m3-streams-adaptive-thinking.json +36 -0
  483. package/test/fixtures/recordings/minimax-messages/m3-streams-text-with-thinking-disabled.json +36 -0
  484. package/test/fixtures/recordings/minimax-responses/m3-continues-a-tool-loop-with-effort-minimal.json +56 -0
  485. package/test/fixtures/recordings/minimax-responses/m3-streams-text-with-effort-none.json +36 -0
  486. package/test/fixtures/recordings/minimax-video/generates-a-hailuo-video-from-a-first-frame.json +176 -0
  487. package/test/fixtures/recordings/minimax-video/generates-an-h3-video.json +96 -0
  488. package/test/fixtures/recordings/mistral-chat/drives-a-tool-loop.json +54 -0
  489. package/test/fixtures/recordings/mistral-chat/replays-native-reasoning.json +47 -0
  490. package/test/fixtures/recordings/mistral-chat/streams-text-with-usage.json +29 -0
  491. package/test/fixtures/recordings/mistral-chat-glm/streams-an-indexed-tool-call.json +36 -0
  492. package/test/fixtures/recordings/moonshot-chat/k3-reads-image-bytes.json +29 -0
  493. package/test/fixtures/recordings/moonshot-chat/k3-respects-tool-choice-none.json +29 -0
  494. package/test/fixtures/recordings/moonshot-chat/k3-respects-tool-choice-required.json +29 -0
  495. package/test/fixtures/recordings/moonshot-chat/k3-returns-native-structured-output.json +29 -0
  496. package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-default-effort.json +29 -0
  497. package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-high-effort.json +29 -0
  498. package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-low-effort.json +29 -0
  499. package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-max-effort.json +29 -0
  500. package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
  501. package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-streams-text-with-default-thinking.json +29 -0
  502. package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-streams-text-with-disabled-thinking.json +29 -0
  503. package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-streams-text-with-enabled-thinking.json +29 -0
  504. package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-highspeed-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
  505. package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-highspeed-streams-text-with-default-thinking.json +29 -0
  506. package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
  507. package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-streams-text-with-default-thinking.json +29 -0
  508. package/test/fixtures/recordings/moonshot-chat/kimi-k3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
  509. package/test/fixtures/recordings/moonshot-messages/k3-reads-image-bytes.json +36 -0
  510. package/test/fixtures/recordings/moonshot-messages/k3-respects-tool-choice-none.json +36 -0
  511. package/test/fixtures/recordings/moonshot-messages/k3-respects-tool-choice-required.json +36 -0
  512. package/test/fixtures/recordings/moonshot-messages/k3-returns-native-structured-output.json +35 -0
  513. package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-default-effort.json +36 -0
  514. package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-high-effort.json +36 -0
  515. package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-low-effort.json +36 -0
  516. package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-max-effort.json +36 -0
  517. package/test/fixtures/recordings/moonshot-messages/kimi-k3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
  518. package/test/fixtures/recordings/moonshot-responses/k3-reads-image-bytes.json +36 -0
  519. package/test/fixtures/recordings/moonshot-responses/k3-rejects-unsupported-tool-choice-none.json +36 -0
  520. package/test/fixtures/recordings/moonshot-responses/k3-rejects-unsupported-tool-choice-required.json +36 -0
  521. package/test/fixtures/recordings/moonshot-responses/k3-returns-native-structured-output.json +35 -0
  522. package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-default-effort.json +36 -0
  523. package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-high-effort.json +36 -0
  524. package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-low-effort.json +36 -0
  525. package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-max-effort.json +36 -0
  526. package/test/fixtures/recordings/moonshot-responses/kimi-k3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
  527. package/test/fixtures/recordings/openai-compatible-chat/gemini-parallel-tool-signatures.json +54 -0
  528. package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-text.json +28 -0
  529. package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-tool-call.json +28 -0
  530. package/test/fixtures/recordings/openai-decision/decides-choice-score-and-predicate-questions-about-json-state.json +33 -0
  531. package/test/fixtures/recordings/openai-embedding/embeds-text-with-requested-dimensions.json +33 -0
  532. package/test/fixtures/recordings/openai-images/edits-an-image.json +28 -0
  533. package/test/fixtures/recordings/openai-images/generates-an-image.json +28 -0
  534. package/test/fixtures/recordings/openai-images/streams-a-partial-image-before-the-final-image.json +32 -0
  535. package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-image-tool-result.json +42 -0
  536. package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning-continuation.json +58 -0
  537. package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning.json +38 -0
  538. package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-tool-loop.json +57 -0
  539. package/test/fixtures/recordings/openai-responses-cache/caches-the-system-prompt-on-gpt-6-2-sol.json +51 -0
  540. package/test/fixtures/recordings/openai-responses-cache/reports-cached-tokens-on-identical-second-call.json +51 -0
  541. package/test/fixtures/recordings/openai-responses-images/generates-and-edits-an-image-with-the-hosted-tool.json +50 -0
  542. package/test/fixtures/recordings/openai-responses-websocket/continues-a-tool-call-over-one-socket.json +588 -0
  543. package/test/fixtures/recordings/openai-responses-websocket/reconstructs-full-context-after-reconnect.json +167 -0
  544. package/test/fixtures/recordings/openai-responses-websocket/recovers-from-explicit-continuation-rejection.json +167 -0
  545. package/test/fixtures/recordings/openai-speech/generates-speech.json +33 -0
  546. package/test/fixtures/recordings/openai-speech/streams-speech.json +32 -0
  547. package/test/fixtures/recordings/openai-transcription/diarizes-speakers-into-segments.json +32 -0
  548. package/test/fixtures/recordings/openai-transcription/returns-whisper-word-timestamps.json +32 -0
  549. package/test/fixtures/recordings/openai-transcription/streams-text-deltas.json +32 -0
  550. package/test/fixtures/recordings/openai-transcription/transcribes-with-a-language-hint.json +32 -0
  551. package/test/fixtures/recordings/opencode-decision/decides-choice-score-and-predicate-questions.json +33 -0
  552. package/test/fixtures/recordings/openrouter-decision/decides-choice-score-and-predicate-questions.json +33 -0
  553. package/test/fixtures/recordings/openrouter-images/edits-an-image-through-input-references.json +32 -0
  554. package/test/fixtures/recordings/openrouter-images/renders-flux-3-from-an-aspect-ratio-and-tier.json +32 -0
  555. package/test/fixtures/recordings/openrouter-images/renders-gemini-at-the-requested-2k-tier.json +32 -0
  556. package/test/fixtures/recordings/openrouter-reasoning-tool-loop.json +55 -0
  557. package/test/fixtures/recordings/openrouter-reasoning.json +29 -0
  558. package/test/fixtures/recordings/openrouter-responses/calls-a-freeform-text-tool.json +35 -0
  559. package/test/fixtures/recordings/openrouter-responses/continues-a-tool-call.json +53 -0
  560. package/test/fixtures/recordings/openrouter-responses/streams-text.json +35 -0
  561. package/test/fixtures/recordings/openrouter-video/generates-a-video.json +113 -0
  562. package/test/fixtures/recordings/pdf/anthropic-tool-result.json +28 -0
  563. package/test/fixtures/recordings/pdf/anthropic-user-input.json +28 -0
  564. package/test/fixtures/recordings/pdf/bedrock-tool-result.json +29 -0
  565. package/test/fixtures/recordings/pdf/bedrock-user-input.json +29 -0
  566. package/test/fixtures/recordings/pdf/gemini-tool-result.json +53 -0
  567. package/test/fixtures/recordings/pdf/gemini-user-input.json +28 -0
  568. package/test/fixtures/recordings/pdf/openai-tool-result.json +35 -0
  569. package/test/fixtures/recordings/pdf/openai-user-input.json +34 -0
  570. package/test/fixtures/recordings/pdf/xai-tool-result.json +35 -0
  571. package/test/fixtures/recordings/pdf/xai-user-input.json +34 -0
  572. package/test/fixtures/recordings/typesafe-decision/decides-choice-score-and-predicate-questions.json +33 -0
  573. package/test/fixtures/recordings/venice-chat/claude-opus-4-8-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
  574. package/test/fixtures/recordings/venice-chat/deepseek-v4-flash-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
  575. package/test/fixtures/recordings/venice-chat/gemini-3-1-pro-preview-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
  576. package/test/fixtures/recordings/venice-chat/gemini-preserves-per-call-signatures-through-parallel-tools.json +71 -0
  577. package/test/fixtures/recordings/venice-chat/openai-gpt-54-mini-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
  578. package/test/fixtures/recordings/venice-chat/qwen-disables-reasoning.json +35 -0
  579. package/test/fixtures/recordings/venice-chat/surfaces-venice-model-errors.json +33 -0
  580. package/test/fixtures/recordings/venice-responses/calls-a-freeform-text-tool.json +35 -0
  581. package/test/fixtures/recordings/venice-responses/continues-a-tool-call.json +53 -0
  582. package/test/fixtures/recordings/venice-responses/streams-text.json +35 -0
  583. package/test/fixtures/recordings/vercel-ai-gateway-images/generates-an-image.json +32 -0
  584. package/test/fixtures/recordings/vercel-ai-gateway-images/sizes-grok-through-xai-provider-options.json +32 -0
  585. package/test/fixtures/recordings/vercel-ai-gateway-reasoning-tool-loop.json +55 -0
  586. package/test/fixtures/recordings/vercel-ai-gateway-reasoning.json +29 -0
  587. package/test/fixtures/recordings/vercel-native/automatically-caches-distinct-claude-system-blocks.json +49 -0
  588. package/test/fixtures/recordings/vercel-native/continues-chat-claude-reasoning-and-tools.json +49 -0
  589. package/test/fixtures/recordings/vercel-native/continues-chat-muse-reasoning-and-tools.json +49 -0
  590. package/test/fixtures/recordings/vercel-native/continues-messages-claude-reasoning-and-tools.json +49 -0
  591. package/test/fixtures/recordings/vercel-native/continues-messages-deepseek-reasoning-and-tools.json +49 -0
  592. package/test/fixtures/recordings/vercel-native/continues-messages-gemini-reasoning-and-tools.json +49 -0
  593. package/test/fixtures/recordings/vercel-native/continues-responses-gpt-reasoning-and-tools.json +49 -0
  594. package/test/fixtures/recordings/vercel-native/continues-responses-grok-reasoning-and-tools.json +49 -0
  595. package/test/fixtures/recordings/vercel-native/rejects-invalid-credentials-without-retryable-classification.json +31 -0
  596. package/test/fixtures/recordings/xai-images/edits-an-image.json +28 -0
  597. package/test/fixtures/recordings/xai-images/generates-an-image.json +28 -0
  598. package/test/fixtures/recordings/xai-video/generates-a-video.json +129 -0
  599. package/test/fixtures/recordings/zai-chat/glm-4-5-calls-a-tool-without-streaming-arguments.json +28 -0
  600. package/test/fixtures/recordings/zai-chat/glm-4-7-streams-thinking-disabled.json +28 -0
  601. package/test/fixtures/recordings/zai-chat/glm-4-7-streams-thinking-enabled.json +28 -0
  602. package/test/fixtures/recordings/zai-chat/glm-5-2-streams-high-effort.json +28 -0
  603. package/test/fixtures/recordings/zai-chat/glm-5-2-streams-low-effort.json +28 -0
  604. package/test/fixtures/recordings/zai-chat/glm-5-2-streams-max-effort.json +28 -0
  605. package/test/fixtures/recordings/zai-chat/glm-5-2-streams-medium-effort.json +28 -0
  606. package/test/fixtures/recordings/zai-chat/glm-5-2-streams-minimal-effort.json +28 -0
  607. package/test/fixtures/recordings/zai-chat/glm-5-2-streams-none-effort.json +28 -0
  608. package/test/fixtures/recordings/zai-chat/glm-5-2-streams-thinking-disabled.json +28 -0
  609. package/test/fixtures/recordings/zai-chat/glm-5-2-streams-thinking-enabled.json +28 -0
  610. package/test/fixtures/recordings/zai-chat/glm-5-2-streams-xhigh-effort.json +28 -0
  611. package/test/fixtures/recordings/zai-chat/glm-5-3-flash-reads-image-bytes.json +28 -0
  612. package/test/fixtures/recordings/zai-chat/glm-5-3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +64 -0
  613. package/test/fixtures/recordings/zai-chat/glm-5-3-rejects-disabled-thinking.json +27 -0
  614. package/test/fixtures/recordings/zai-chat/glm-5-3-returns-a-json-object.json +28 -0
  615. package/test/fixtures/recordings/zai-chat/glm-5-3-streams-default-effort.json +28 -0
  616. package/test/fixtures/recordings/zai-chat/glm-5-3-streams-high-effort.json +28 -0
  617. package/test/fixtures/recordings/zai-chat/glm-5-3-streams-low-effort.json +28 -0
  618. package/test/fixtures/recordings/zai-chat/glm-5-3-streams-max-effort.json +28 -0
  619. package/test/fixtures/recordings/zai-images/generates-an-image.json +28 -0
  620. package/test/generate-object.test.ts +281 -0
  621. package/test/generation.test.ts +191 -0
  622. package/test/google-interactions-video.test.ts +351 -0
  623. package/test/http-hooks.test.ts +137 -0
  624. package/test/http-timeout.test.ts +287 -0
  625. package/test/image-job.test.ts +199 -0
  626. package/test/image-sizing.test.ts +47 -0
  627. package/test/image.test.ts +1114 -0
  628. package/test/image.types.ts +203 -0
  629. package/test/keyless-auth.test.ts +85 -0
  630. package/test/lib/effect.ts +50 -0
  631. package/test/lib/env.ts +30 -0
  632. package/test/lib/http.ts +141 -0
  633. package/test/lib/image.ts +29 -0
  634. package/test/lib/openai-chunks.ts +27 -0
  635. package/test/lib/sse.ts +20 -0
  636. package/test/lib/tool-runtime.ts +152 -0
  637. package/test/llm-option-types.types.ts +153 -0
  638. package/test/llm.test.ts +286 -0
  639. package/test/loop-steps.test.ts +77 -0
  640. package/test/loop-tools.types.ts +79 -0
  641. package/test/loop.test.ts +425 -0
  642. package/test/media-input.test.ts +98 -0
  643. package/test/media-usage.test.ts +13 -0
  644. package/test/media.test.ts +340 -0
  645. package/test/minimax-video.test.ts +553 -0
  646. package/test/openai-freeform.test.ts +252 -0
  647. package/test/openrouter-video.test.ts +294 -0
  648. package/test/partial-json.test.ts +58 -0
  649. package/test/promise-parity.types.ts +83 -0
  650. package/test/promise.test.ts +632 -0
  651. package/test/provider/alibaba-capabilities.recorded.test.ts +253 -0
  652. package/test/provider/alibaba-images.recorded.test.ts +71 -0
  653. package/test/provider/alibaba-images.test.ts +199 -0
  654. package/test/provider/alibaba.recorded.test.ts +165 -0
  655. package/test/provider/alibaba.test.ts +419 -0
  656. package/test/provider/anthropic-compaction.test.ts +171 -0
  657. package/test/provider/anthropic-messages-cache.recorded.test.ts +111 -0
  658. package/test/provider/anthropic-messages.recorded.test.ts +45 -0
  659. package/test/provider/anthropic-messages.test.ts +2609 -0
  660. package/test/provider/anthropic-thinking-binding.test.ts +65 -0
  661. package/test/provider/assemblyai-transcription.recorded.test.ts +39 -0
  662. package/test/provider/azure.recorded.test.ts +139 -0
  663. package/test/provider/azure.test.ts +48 -0
  664. package/test/provider/bedrock-converse-cache.recorded.test.ts +61 -0
  665. package/test/provider/bedrock-converse-cache.test.ts +168 -0
  666. package/test/provider/bedrock-converse-options.recorded.test.ts +119 -0
  667. package/test/provider/bedrock-converse-options.test.ts +149 -0
  668. package/test/provider/bedrock-converse-thinking-binding.recorded.test.ts +103 -0
  669. package/test/provider/bedrock-converse-thinking-binding.test.ts +107 -0
  670. package/test/provider/bedrock-converse.test.ts +2548 -0
  671. package/test/provider/bedrock-mantle.test.ts +444 -0
  672. package/test/provider/bedrock-mistral-ids.test.ts +86 -0
  673. package/test/provider/cartesia-speech.recorded.test.ts +47 -0
  674. package/test/provider/checkpoint.test.ts +324 -0
  675. package/test/provider/cloudflare-workers-ai-images.recorded.test.ts +57 -0
  676. package/test/provider/cloudflare-workers-ai-images.test.ts +155 -0
  677. package/test/provider/cloudflare.test.ts +330 -0
  678. package/test/provider/cohere.recorded.test.ts +94 -0
  679. package/test/provider/cohere.test.ts +301 -0
  680. package/test/provider/compaction-websocket.test.ts +50 -0
  681. package/test/provider/compaction.recorded.test.ts +89 -0
  682. package/test/provider/compaction.test.ts +178 -0
  683. package/test/provider/conversation-lowering.test.ts +31 -0
  684. package/test/provider/decision.recorded.test.ts +211 -0
  685. package/test/provider/deepgram-speech.recorded.test.ts +41 -0
  686. package/test/provider/deepgram-transcription.recorded.test.ts +47 -0
  687. package/test/provider/digitalocean.recorded.test.ts +78 -0
  688. package/test/provider/digitalocean.test.ts +123 -0
  689. package/test/provider/elevenlabs-speech.recorded.test.ts +54 -0
  690. package/test/provider/elevenlabs-transcription.recorded.test.ts +50 -0
  691. package/test/provider/embedding.recorded.test.ts +109 -0
  692. package/test/provider/empty-tool-choice.test.ts +80 -0
  693. package/test/provider/error-retention.test.ts +158 -0
  694. package/test/provider/explicit-compaction.test.ts +503 -0
  695. package/test/provider/extraction.recorded.test.ts +46 -0
  696. package/test/provider/freeform-tools.test.ts +446 -0
  697. package/test/provider/gemini-cache.recorded.test.ts +49 -0
  698. package/test/provider/gemini-openai-chat.recorded.test.ts +69 -0
  699. package/test/provider/gemini.test.ts +2336 -0
  700. package/test/provider/golden.recorded.test.ts +287 -0
  701. package/test/provider/google-images.recorded.test.ts +53 -0
  702. package/test/provider/google-interactions-video.recorded.test.ts +48 -0
  703. package/test/provider/google-interactions.recorded.test.ts +169 -0
  704. package/test/provider/google-interactions.test.ts +593 -0
  705. package/test/provider/google-speech.recorded.test.ts +45 -0
  706. package/test/provider/google-transcription.recorded.test.ts +54 -0
  707. package/test/provider/google-vertex-interactions.test.ts +307 -0
  708. package/test/provider/google-vertex.recorded.test.ts +75 -0
  709. package/test/provider/google-vertex.test.ts +595 -0
  710. package/test/provider/groq.recorded.test.ts +187 -0
  711. package/test/provider/groq.test.ts +134 -0
  712. package/test/provider/image-detail.test.ts +58 -0
  713. package/test/provider/media-entrypoints.test.ts +618 -0
  714. package/test/provider/meta-capabilities.test.ts +216 -0
  715. package/test/provider/meta-images.recorded.test.ts +119 -0
  716. package/test/provider/meta-messages.recorded.test.ts +121 -0
  717. package/test/provider/meta-search.recorded.test.ts +97 -0
  718. package/test/provider/meta.recorded.test.ts +182 -0
  719. package/test/provider/meta.test.ts +140 -0
  720. package/test/provider/minimax-images.recorded.test.ts +52 -0
  721. package/test/provider/minimax-images.test.ts +236 -0
  722. package/test/provider/minimax-video.recorded.test.ts +83 -0
  723. package/test/provider/minimax.recorded.test.ts +257 -0
  724. package/test/provider/minimax.test.ts +110 -0
  725. package/test/provider/missing-tool-input.test.ts +73 -0
  726. package/test/provider/mistral-chat.test.ts +757 -0
  727. package/test/provider/mistral.recorded.test.ts +170 -0
  728. package/test/provider/moonshot-capabilities.recorded.test.ts +131 -0
  729. package/test/provider/moonshot.recorded.test.ts +191 -0
  730. package/test/provider/moonshot.test.ts +218 -0
  731. package/test/provider/native-providers.test.ts +299 -0
  732. package/test/provider/open-responses-errors.test.ts +141 -0
  733. package/test/provider/open-responses-finals.test.ts +209 -0
  734. package/test/provider/open-responses-lifecycle.test.ts +526 -0
  735. package/test/provider/open-responses-replay.test.ts +120 -0
  736. package/test/provider/openai-chat-reasoning.recorded.test.ts +154 -0
  737. package/test/provider/openai-chat.test.ts +2505 -0
  738. package/test/provider/openai-compatible-chat.test.ts +819 -0
  739. package/test/provider/openai-compatible-responses.test.ts +1139 -0
  740. package/test/provider/openai-images.recorded.test.ts +91 -0
  741. package/test/provider/openai-responses-cache.recorded.test.ts +68 -0
  742. package/test/provider/openai-responses-images.recorded.test.ts +66 -0
  743. package/test/provider/openai-responses-websocket.recorded.test.ts +221 -0
  744. package/test/provider/openai-responses.test.ts +5414 -0
  745. package/test/provider/openai-speech.recorded.test.ts +38 -0
  746. package/test/provider/openai-transcription.recorded.test.ts +91 -0
  747. package/test/provider/openrouter-images.recorded.test.ts +76 -0
  748. package/test/provider/openrouter-video.recorded.test.ts +53 -0
  749. package/test/provider/openrouter.test.ts +656 -0
  750. package/test/provider/pdf.recorded.test.ts +211 -0
  751. package/test/provider/queued-recording.ts +10 -0
  752. package/test/provider/responses-routes.recorded.test.ts +91 -0
  753. package/test/provider/seedance-video.recorded.test.ts +52 -0
  754. package/test/provider/speech-recording.ts +19 -0
  755. package/test/provider/transcription-recording.ts +41 -0
  756. package/test/provider/venice.recorded.test.ts +237 -0
  757. package/test/provider/venice.test.ts +373 -0
  758. package/test/provider/vercel-ai-gateway-images.recorded.test.ts +55 -0
  759. package/test/provider/vercel-ai-gateway.recorded.test.ts +125 -0
  760. package/test/provider/vercel-ai-gateway.test.ts +303 -0
  761. package/test/provider/xai-images.recorded.test.ts +54 -0
  762. package/test/provider/xai-images.test.ts +109 -0
  763. package/test/provider/xai-responses.test.ts +339 -0
  764. package/test/provider/xai-video.recorded.test.ts +54 -0
  765. package/test/provider/zai-capabilities.recorded.test.ts +156 -0
  766. package/test/provider/zai-images.recorded.test.ts +33 -0
  767. package/test/provider/zai-images.test.ts +163 -0
  768. package/test/provider/zai.recorded.test.ts +115 -0
  769. package/test/provider/zai.test.ts +374 -0
  770. package/test/provider-api-methods.types.ts +8 -0
  771. package/test/provider-error.test.ts +800 -0
  772. package/test/provider-model.types.ts +37 -0
  773. package/test/provider-options/alibaba.types.ts +46 -0
  774. package/test/provider-options/amazon-bedrock.types.ts +33 -0
  775. package/test/provider-options/anthropic-compatible.types.ts +13 -0
  776. package/test/provider-options/anthropic.types.ts +30 -0
  777. package/test/provider-options/azure.types.ts +13 -0
  778. package/test/provider-options/checkpoint.types.ts +150 -0
  779. package/test/provider-options/cloudflare.types.ts +13 -0
  780. package/test/provider-options/compaction.types.ts +146 -0
  781. package/test/provider-options/google-vertex-chat.types.ts +7 -0
  782. package/test/provider-options/google-vertex-interactions.types.ts +29 -0
  783. package/test/provider-options/google-vertex-messages.types.ts +13 -0
  784. package/test/provider-options/google-vertex-responses.types.ts +14 -0
  785. package/test/provider-options/google-vertex.types.ts +17 -0
  786. package/test/provider-options/google.types.ts +43 -0
  787. package/test/provider-options/minimax.types.ts +33 -0
  788. package/test/provider-options/mistral.types.ts +24 -0
  789. package/test/provider-options/moonshot.types.ts +45 -0
  790. package/test/provider-options/openai-compatible-responses.types.ts +17 -0
  791. package/test/provider-options/openai-compatible.types.ts +13 -0
  792. package/test/provider-options/openai.types.ts +27 -0
  793. package/test/provider-options/openrouter.types.ts +55 -0
  794. package/test/provider-options/venice.types.ts +39 -0
  795. package/test/provider-options/vercel-ai-gateway.types.ts +30 -0
  796. package/test/provider-options/xai.types.ts +14 -0
  797. package/test/provider-options/zai.types.ts +42 -0
  798. package/test/provider-package.test.ts +586 -0
  799. package/test/raw-events.test.ts +75 -0
  800. package/test/realtime.test.ts +172 -0
  801. package/test/recorded-golden.ts +104 -0
  802. package/test/recorded-runner.ts +101 -0
  803. package/test/recorded-scenarios.ts +468 -0
  804. package/test/recorded-test.ts +127 -0
  805. package/test/recorded-utils.ts +56 -0
  806. package/test/request-encoding.test.ts +57 -0
  807. package/test/request-transport.test.ts +74 -0
  808. package/test/response.test.ts +203 -0
  809. package/test/route.test.ts +225 -0
  810. package/test/sanitize.test.ts +80 -0
  811. package/test/schema.test.ts +412 -0
  812. package/test/seedance-video.test.ts +418 -0
  813. package/test/speech.test.ts +297 -0
  814. package/test/speech.types.ts +33 -0
  815. package/test/testing.test.ts +372 -0
  816. package/test/tool-history.test.ts +162 -0
  817. package/test/tool-runtime.test.ts +926 -0
  818. package/test/tool-schema-projection.test.ts +427 -0
  819. package/test/tool-stream.test.ts +252 -0
  820. package/test/tool-updates.test.ts +849 -0
  821. package/test/tool.types.ts +131 -0
  822. package/test/transcription.test.ts +618 -0
  823. package/test/transcription.types.ts +44 -0
  824. package/test/video.test.ts +447 -0
  825. package/test/video.types.ts +110 -0
  826. package/tsconfig.build.json +8 -0
  827. package/tsconfig.json +17 -0
  828. package/tsconfig.types.json +16 -0
@@ -0,0 +1,2505 @@
1
+ import { describe, expect } from "bun:test"
2
+ import { Effect, Ref, Schema, Stream } from "effect"
3
+ import { HttpClientRequest } from "effect/http"
4
+ import {
5
+ ai,
6
+ AIError,
7
+ LanguageModel,
8
+ LLMEvent,
9
+ LLMResponse,
10
+ Media,
11
+ Message,
12
+ ToolCallPart,
13
+ ToolDefinition,
14
+ Usage,
15
+ } from "../../src/effect.js"
16
+ import * as Azure from "../../src/providers/azure.js"
17
+ import * as OpenAI from "../../src/providers/openai.js"
18
+ import * as OpenAICompatible from "../../src/providers/openai-compatible.js"
19
+ import * as XAI from "../../src/providers/xai.js"
20
+ import * as OpenAIChat from "../../src/protocols/openai-chat.js"
21
+ import { ProviderShared } from "../../src/protocols/shared.js"
22
+ import { Auth, LLMClient } from "../../src/route.js"
23
+ import { compileRequest } from "../../src/route/client.js"
24
+ import { it } from "../lib/effect.js"
25
+ import { dynamicResponse, fixedResponse, systemError, truncatedStream } from "../lib/http.js"
26
+ import { deltaChunk, usageChunk } from "../lib/openai-chunks.js"
27
+ import { sseEvents } from "../lib/sse.js"
28
+
29
+ const TargetJson = Schema.fromJsonString(Schema.Unknown)
30
+ const encodeJson = Schema.encodeSync(TargetJson)
31
+ const decodeJson = Schema.decodeUnknownSync(TargetJson)
32
+
33
+ const model = OpenAIChat.route
34
+ .with({ endpoint: { baseURL: "https://api.openai.test/v1/" }, auth: Auth.bearer("test") })
35
+ .model({ id: "gpt-4o-mini" })
36
+
37
+ const request = ai.request({
38
+ id: "req_1",
39
+ model,
40
+ system: "You are concise.",
41
+ prompt: "Say hello.",
42
+ maxTokens: 20,
43
+ temperature: 0,
44
+ })
45
+
46
+ describe("OpenAI Chat route", () => {
47
+ it.effect("prepares OpenAI Chat payload", () =>
48
+ Effect.gen(function* () {
49
+ const prepared = yield* compileRequest(request)
50
+
51
+ expect(prepared.body).toMatchObject({
52
+ model: "gpt-4o-mini",
53
+ messages: [
54
+ { role: "system", content: "You are concise." },
55
+ { role: "user", content: "Say hello." },
56
+ ],
57
+ stream: true,
58
+ stream_options: { include_usage: true },
59
+ store: false,
60
+ max_completion_tokens: 20,
61
+ temperature: 0,
62
+ })
63
+ }),
64
+ )
65
+
66
+ it.effect("replaces characters providers reject in replayed names of undeclared tool calls", () =>
67
+ Effect.gen(function* () {
68
+ const invented = ToolCallPart.make({ id: "call_1", name: "opencode.session.search", input: {} })
69
+ const declared = ToolCallPart.make({ id: "call_2", name: "weather.get", input: {} })
70
+ const prepared = yield* compileRequest(
71
+ ai.request({
72
+ model,
73
+ tools: [
74
+ ToolDefinition.make({ name: "weather.get", description: "Weather", inputSchema: { type: "object" } }),
75
+ ],
76
+ messages: [
77
+ Message.assistant([invented, declared]),
78
+ Message.tool({ id: invented.id, name: invented.name, result: "unknown tool", resultType: "error" }),
79
+ Message.tool({ id: declared.id, name: declared.name, result: "sunny", resultType: "text" }),
80
+ ],
81
+ }),
82
+ )
83
+
84
+ // A declared name stays as declared, for providers that accept it.
85
+ expect(prepared.body.messages[0]).toMatchObject({
86
+ role: "assistant",
87
+ tool_calls: [
88
+ { id: "call_1", function: { name: "opencode_session_search" } },
89
+ { id: "call_2", function: { name: "weather.get" } },
90
+ ],
91
+ })
92
+ }),
93
+ )
94
+
95
+ it.effect("omits stream usage options when compatibility disables them", () =>
96
+ Effect.gen(function* () {
97
+ const prepared = yield* compileRequest(
98
+ ai.request({
99
+ model: LanguageModel.update(model, { capabilities: { streamUsage: false } }),
100
+ prompt: "Hello",
101
+ }),
102
+ )
103
+
104
+ expect(prepared.body).not.toHaveProperty("stream_options")
105
+ }),
106
+ )
107
+
108
+ it.effect("sends store only to OpenAI and Azure unless compatibility opts in", () =>
109
+ Effect.gen(function* () {
110
+ const compatible = OpenAICompatible.configure({ baseURL: "https://api.compatible.test/v1", apiKey: "test" })
111
+ const omitted = yield* compileRequest(
112
+ ai.request({
113
+ model: compatible.model("compatible-model"),
114
+ prompt: "Hello",
115
+ providerOptions: { store: true },
116
+ }),
117
+ )
118
+ const optedIn = yield* compileRequest(
119
+ ai.request({
120
+ model: LanguageModel.update(compatible.model("compatible-model"), {
121
+ capabilities: { store: true },
122
+ }),
123
+ prompt: "Hello",
124
+ }),
125
+ )
126
+ const openaiHost = yield* compileRequest(
127
+ ai.request({
128
+ model: OpenAICompatible.configure({ baseURL: "https://api.openai.com/v1", apiKey: "test" }).model(
129
+ "gpt-4o-mini",
130
+ ),
131
+ prompt: "Hello",
132
+ }),
133
+ )
134
+ const azure = yield* compileRequest(
135
+ ai.request({
136
+ model: Azure.configure({ baseURL: "https://opencode-test.openai.azure.com/openai/", apiKey: "test" }).chat(
137
+ "gpt-4o-mini",
138
+ ),
139
+ prompt: "Hello",
140
+ }),
141
+ )
142
+
143
+ expect(omitted.body).not.toHaveProperty("store")
144
+ expect(optedIn.body.store).toBe(false)
145
+ expect(openaiHost.body.store).toBe(false)
146
+ expect(azure.body.store).toBe(false)
147
+ }),
148
+ )
149
+
150
+ it.effect("lowers chronological system updates to escaped user wrappers in order", () =>
151
+ Effect.gen(function* () {
152
+ const prepared = yield* compileRequest(
153
+ ai.request({
154
+ model,
155
+ messages: [
156
+ Message.user("Before."),
157
+ Message.system("Treat <admin> & data literally."),
158
+ Message.assistant("After."),
159
+ ],
160
+ }),
161
+ )
162
+
163
+ expect(prepared.body.messages).toEqual([
164
+ {
165
+ role: "user",
166
+ content: "Before.\n<system-update>\nTreat &lt;admin&gt; &amp; data literally.\n</system-update>",
167
+ },
168
+ { role: "assistant", content: "After." },
169
+ ])
170
+ }),
171
+ )
172
+
173
+ it.effect("omits empty and whitespace-only assistant messages", () =>
174
+ Effect.gen(function* () {
175
+ const prepared = yield* compileRequest(
176
+ ai.request({
177
+ model,
178
+ messages: [
179
+ Message.user("Before."),
180
+ Message.assistant([]),
181
+ Message.assistant(""),
182
+ Message.assistant(" \n\t "),
183
+ Message.assistant("After."),
184
+ ],
185
+ }),
186
+ )
187
+
188
+ expect(prepared.body.messages).toEqual([
189
+ { role: "user", content: "Before." },
190
+ { role: "assistant", content: "After." },
191
+ ])
192
+ }),
193
+ )
194
+
195
+ it.effect("replays canonical reasoning as OpenAI-compatible reasoning_content", () =>
196
+ Effect.gen(function* () {
197
+ const prepared = yield* compileRequest(
198
+ ai.request({
199
+ model,
200
+ messages: [
201
+ Message.assistant([
202
+ { type: "reasoning", text: "thinking" },
203
+ { type: "text", text: "Hello" },
204
+ ]),
205
+ ],
206
+ }),
207
+ )
208
+
209
+ expect(prepared.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_content: "thinking" }])
210
+ }),
211
+ )
212
+
213
+ it.effect("concatenates assistant text parts without adding separators", () =>
214
+ Effect.gen(function* () {
215
+ const prepared = yield* compileRequest(
216
+ ai.request({
217
+ model,
218
+ messages: [
219
+ Message.assistant([
220
+ { type: "text", text: "Hello" },
221
+ { type: "text", text: " world" },
222
+ ]),
223
+ ],
224
+ }),
225
+ )
226
+
227
+ expect(prepared.body.messages).toEqual([{ role: "assistant", content: "Hello world" }])
228
+ }),
229
+ )
230
+
231
+ it.effect("writes reasoning to a configured custom field on every assistant message", () =>
232
+ Effect.gen(function* () {
233
+ const prepared = yield* compileRequest(
234
+ ai.request({
235
+ model: LanguageModel.update(model, { compatibility: { reasoningField: "vendor_reasoning" } }),
236
+ messages: [
237
+ Message.assistant([
238
+ {
239
+ type: "reasoning",
240
+ text: "thinking",
241
+ providerMetadata: { openai: { reasoningField: "reasoning" } },
242
+ },
243
+ { type: "text", text: "Hello" },
244
+ ]),
245
+ Message.assistant("Done"),
246
+ ],
247
+ }),
248
+ )
249
+
250
+ expect(prepared.body.messages).toEqual([
251
+ { role: "assistant", content: "Hello", vendor_reasoning: "thinking" },
252
+ { role: "assistant", content: "Done", vendor_reasoning: "" },
253
+ ])
254
+ }),
255
+ )
256
+
257
+ it.effect("preserves observed reasoning fields when reasoning is required", () =>
258
+ Effect.gen(function* () {
259
+ const prepared = yield* compileRequest(
260
+ ai.request({
261
+ model: LanguageModel.update(model, { compatibility: { requireReasoning: true } }),
262
+ messages: [
263
+ Message.assistant([
264
+ {
265
+ type: "reasoning",
266
+ text: "thinking",
267
+ providerMetadata: { openai: { reasoningField: "reasoning_text" } },
268
+ },
269
+ { type: "text", text: "Hello" },
270
+ ]),
271
+ Message.assistant("Done"),
272
+ ],
273
+ }),
274
+ )
275
+
276
+ expect(prepared.body.messages).toEqual([
277
+ { role: "assistant", content: "Hello", reasoning_text: "thinking" },
278
+ { role: "assistant", content: "Done", reasoning_content: "" },
279
+ ])
280
+ }),
281
+ )
282
+
283
+ it.effect("omits empty configured reasoning fields when reasoning is explicitly optional", () =>
284
+ Effect.gen(function* () {
285
+ const prepared = yield* compileRequest(
286
+ ai.request({
287
+ model: LanguageModel.update(model, {
288
+ compatibility: { reasoningField: "reasoning_text", requireReasoning: false },
289
+ }),
290
+ messages: [
291
+ Message.assistant([
292
+ { type: "reasoning", text: "thinking" },
293
+ { type: "text", text: "Hello" },
294
+ ]),
295
+ Message.assistant("Done"),
296
+ ],
297
+ }),
298
+ )
299
+
300
+ expect(prepared.body.messages).toEqual([
301
+ { role: "assistant", content: "Hello", reasoning_text: "thinking" },
302
+ { role: "assistant", content: "Done" },
303
+ ])
304
+ }),
305
+ )
306
+
307
+ it.effect("rejects reasoning fields that conflict with assistant message fields", () =>
308
+ Effect.gen(function* () {
309
+ const error = yield* compileRequest(
310
+ ai.request({
311
+ model: LanguageModel.update(model, { compatibility: { reasoningField: "content" } }),
312
+ messages: [Message.assistant([{ type: "reasoning", text: "thinking" }])],
313
+ }),
314
+ ).pipe(Effect.flip)
315
+
316
+ expect(error.message).toContain("reserved field content")
317
+ }),
318
+ )
319
+
320
+ it.effect("maps OpenAI provider options to Chat options", () =>
321
+ Effect.gen(function* () {
322
+ const prepared = yield* compileRequest(
323
+ ai.request({
324
+ model: OpenAI.configure({ baseURL: "https://api.openai.test/v1/", apiKey: "test" }).chat("gpt-4o-mini"),
325
+ prompt: "think",
326
+ providerOptions: { reasoningEffort: "max" },
327
+ }),
328
+ )
329
+
330
+ expect(prepared.body.store).toBe(false)
331
+ expect(prepared.body.reasoning_effort).toBe("max")
332
+ }),
333
+ )
334
+
335
+ it.effect("keeps valid Chat options when a sibling option is malformed", () =>
336
+ Effect.gen(function* () {
337
+ const prepared = yield* compileRequest(
338
+ ai.request({
339
+ model: OpenAI.configure({ baseURL: "https://api.openai.test/v1/", apiKey: "test" }).chat("gpt-4o-mini"),
340
+ prompt: "think",
341
+ providerOptions: { store: true, reasoningEffort: "max", topLogprobs: 25 },
342
+ }),
343
+ )
344
+
345
+ expect(prepared.body.store).toBe(true)
346
+ expect(prepared.body.reasoning_effort).toBe("max")
347
+ }),
348
+ )
349
+
350
+ it.effect("maps shared OpenAI options to Chat fields", () =>
351
+ Effect.gen(function* () {
352
+ const prepared = yield* compileRequest(
353
+ ai.request({
354
+ model: OpenAI.configure({ baseURL: "https://api.openai.test/v1/", apiKey: "test" }).chat("gpt-5-nano"),
355
+ prompt: "Hello",
356
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
357
+ providerOptions: {
358
+ store: true,
359
+ serviceTier: "flex",
360
+ textVerbosity: "low",
361
+ parallelToolCalls: false,
362
+ metadata: { source: "test" },
363
+ safetyIdentifier: "user-hash",
364
+ },
365
+ }),
366
+ )
367
+
368
+ expect(prepared.body).toMatchObject({
369
+ store: true,
370
+ service_tier: "flex",
371
+ verbosity: "low",
372
+ parallel_tool_calls: false,
373
+ metadata: { source: "test" },
374
+ safety_identifier: "user-hash",
375
+ })
376
+ expect(prepared.body).not.toHaveProperty("text")
377
+ }),
378
+ )
379
+
380
+ it.effect("maps disableParallelToolUse to parallel_tool_calls only when tools are present", () =>
381
+ Effect.gen(function* () {
382
+ const tool = ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })
383
+ const withTools = yield* compileRequest({
384
+ ...request,
385
+ tools: [tool],
386
+ toolChoice: { type: "auto", disableParallelToolUse: true },
387
+ })
388
+ const withoutTools = yield* compileRequest({
389
+ ...request,
390
+ toolChoice: { type: "auto", disableParallelToolUse: true },
391
+ })
392
+ const unset = yield* compileRequest({ ...request, tools: [tool] })
393
+
394
+ expect(withTools.body.parallel_tool_calls).toBe(false)
395
+ expect(withoutTools.body).not.toHaveProperty("parallel_tool_calls")
396
+ expect(unset.body).not.toHaveProperty("parallel_tool_calls")
397
+ expect(unset.body).not.toHaveProperty("service_tier")
398
+ expect(unset.body).not.toHaveProperty("verbosity")
399
+ expect(unset.body).not.toHaveProperty("metadata")
400
+ expect(unset.body).not.toHaveProperty("safety_identifier")
401
+ }),
402
+ )
403
+
404
+ it.effect("maps the request prompt cache key when the compatibility flag is set", () =>
405
+ Effect.gen(function* () {
406
+ const prepared = yield* compileRequest(
407
+ ai.request({
408
+ model: OpenAIChat.route
409
+ .with({ endpoint: { baseURL: "https://api.compatible.test/v1" }, auth: Auth.bearer("test") })
410
+ .model({ id: "compatible-model", capabilities: { promptCacheKey: true } }),
411
+ prompt: "Hello",
412
+ promptCacheKey: "session_123",
413
+ }),
414
+ )
415
+
416
+ expect(prepared.body.prompt_cache_key).toBe("session_123")
417
+ }),
418
+ )
419
+
420
+ it.effect("sends the cache TTL as a request option when no markers carry it", () =>
421
+ Effect.gen(function* () {
422
+ const prepared = yield* compileRequest(
423
+ ai.request({
424
+ model: OpenAICompatible.configure({ baseURL: "https://api.compatible.test/v1", apiKey: "test" }).model(
425
+ "compatible-model",
426
+ ),
427
+ prompt: "Hello",
428
+ cache: { ttl: "1h" },
429
+ }),
430
+ )
431
+
432
+ expect(prepared.body).toMatchObject({ prompt_cache_options: { ttl: "1h" } })
433
+ }),
434
+ )
435
+
436
+ it.effect("omits the prompt cache key without the compatibility flag", () =>
437
+ Effect.gen(function* () {
438
+ const prepared = yield* compileRequest(
439
+ ai.request({
440
+ model: OpenAICompatible.configure({
441
+ baseURL: "https://api.compatible.test/v1",
442
+ apiKey: "test",
443
+ }).model("compatible-model"),
444
+ prompt: "Hello",
445
+ promptCacheKey: "session_123",
446
+ }),
447
+ )
448
+
449
+ expect(prepared.body).not.toHaveProperty("prompt_cache_key")
450
+ }),
451
+ )
452
+
453
+ it.effect("omits the prompt cache key when caching is disabled", () =>
454
+ Effect.gen(function* () {
455
+ const prepared = yield* compileRequest(
456
+ ai.request({
457
+ model,
458
+ prompt: "Hello",
459
+ promptCacheKey: "session_123",
460
+ cache: "none",
461
+ }),
462
+ )
463
+
464
+ expect(prepared.body).not.toHaveProperty("prompt_cache_key")
465
+ }),
466
+ )
467
+
468
+ it.effect("maps the xAI Chat prompt cache key to conversation affinity header only", () =>
469
+ LLMClient.generate(
470
+ ai.request({
471
+ model: XAI.configure({ apiKey: "test", baseURL: "https://api.x.ai/v1" }).chat("grok-4.5"),
472
+ prompt: "Hello",
473
+ promptCacheKey: "session_123",
474
+ }),
475
+ ).pipe(
476
+ Effect.provide(
477
+ dynamicResponse((input) =>
478
+ Effect.gen(function* () {
479
+ const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
480
+ expect(web.headers.get("x-grok-conv-id")).toBe("session_123")
481
+ const body = decodeJson(yield* Effect.promise(() => web.text()))
482
+ // Chat uses the header; prompt_cache_key is Responses-only.
483
+ expect(ProviderShared.isRecord(body) ? body.prompt_cache_key : undefined).toBeUndefined()
484
+ return input.respond(sseEvents(deltaChunk({}, "stop")), {
485
+ headers: { "content-type": "text/event-stream" },
486
+ })
487
+ }),
488
+ ),
489
+ ),
490
+ ),
491
+ )
492
+
493
+ it.effect("passes through custom OpenAI-compatible reasoning effort strings", () =>
494
+ Effect.gen(function* () {
495
+ const prepared = yield* compileRequest(
496
+ ai.request({
497
+ model,
498
+ prompt: "think",
499
+ providerOptions: { reasoningEffort: "experimental" },
500
+ }),
501
+ )
502
+
503
+ expect(prepared.body.reasoning_effort).toBe("experimental")
504
+ }),
505
+ )
506
+
507
+ it.effect("adds native query params to the Chat Completions URL", () =>
508
+ LLMClient.generate({
509
+ ...request,
510
+ model: LanguageModel.update(model, {
511
+ route: model.route.with({ endpoint: { query: { "api-version": "v1" } } }),
512
+ }),
513
+ }).pipe(
514
+ Effect.provide(
515
+ dynamicResponse((input) =>
516
+ Effect.gen(function* () {
517
+ const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
518
+ expect(web.url).toBe("https://api.openai.test/v1/chat/completions?api-version=v1")
519
+ return input.respond(sseEvents(deltaChunk({}, "stop")), {
520
+ headers: { "content-type": "text/event-stream" },
521
+ })
522
+ }),
523
+ ),
524
+ ),
525
+ ),
526
+ )
527
+
528
+ it.effect("uses Azure api-key header for static OpenAI Chat keys", () =>
529
+ LLMClient.generate({
530
+ ...request,
531
+ model: Azure.configure({
532
+ baseURL: "https://opencode-test.openai.azure.com/openai/",
533
+ apiKey: "azure-key",
534
+ headers: { authorization: "Bearer stale" },
535
+ }).chat("gpt-4o-mini"),
536
+ }).pipe(
537
+ Effect.provide(
538
+ dynamicResponse((input) =>
539
+ Effect.gen(function* () {
540
+ const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
541
+ expect(web.url).toBe("https://opencode-test.openai.azure.com/openai/v1/chat/completions?api-version=v1")
542
+ expect(web.headers.get("api-key")).toBe("azure-key")
543
+ expect(web.headers.get("authorization")).toBeNull()
544
+ return input.respond(sseEvents(deltaChunk({}, "stop")), {
545
+ headers: { "content-type": "text/event-stream" },
546
+ })
547
+ }),
548
+ ),
549
+ ),
550
+ ),
551
+ )
552
+
553
+ it.effect("applies serializable HTTP overlays after payload lowering", () =>
554
+ LLMClient.generate({
555
+ ...request,
556
+ model: model.route
557
+ .with({ auth: Auth.bearer("fresh-key"), headers: { authorization: "Bearer stale" } })
558
+ .model({ id: model.id }),
559
+ headers: { authorization: "Bearer request", "x-custom": "yes" },
560
+ http: { body: { metadata: { source: "test" } }, query: { debug: "1" } },
561
+ }).pipe(
562
+ Effect.provide(
563
+ dynamicResponse((input) =>
564
+ Effect.gen(function* () {
565
+ const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
566
+ expect(web.url).toBe("https://api.openai.test/v1/chat/completions?debug=1")
567
+ expect(web.headers.get("authorization")).toBe("Bearer fresh-key")
568
+ expect(web.headers.get("x-custom")).toBe("yes")
569
+ expect(decodeJson(input.text)).toMatchObject({
570
+ stream: true,
571
+ stream_options: { include_usage: true },
572
+ metadata: { source: "test" },
573
+ })
574
+ return input.respond(sseEvents(deltaChunk({}, "stop")), {
575
+ headers: { "content-type": "text/event-stream" },
576
+ })
577
+ }),
578
+ ),
579
+ ),
580
+ ),
581
+ )
582
+
583
+ it.effect("prepares assistant tool-call and tool-result messages", () =>
584
+ Effect.gen(function* () {
585
+ const prepared = yield* compileRequest(
586
+ ai.request({
587
+ id: "req_tool_result",
588
+ model,
589
+ messages: [
590
+ Message.user("What is the weather?"),
591
+ Message.assistant([ToolCallPart.make({ id: "call_1", name: "lookup", input: { query: "weather" } })]),
592
+ Message.tool({ id: "call_1", name: "lookup", result: { forecast: "sunny" } }),
593
+ ],
594
+ }),
595
+ )
596
+
597
+ expect(prepared.body).toMatchObject({
598
+ model: "gpt-4o-mini",
599
+ messages: [
600
+ { role: "user", content: "What is the weather?" },
601
+ {
602
+ role: "assistant",
603
+ content: null,
604
+ tool_calls: [
605
+ {
606
+ id: "call_1",
607
+ type: "function",
608
+ function: { name: "lookup", arguments: encodeJson({ query: "weather" }) },
609
+ },
610
+ ],
611
+ },
612
+ { role: "tool", tool_call_id: "call_1", content: encodeJson({ forecast: "sunny" }) },
613
+ ],
614
+ tools: [],
615
+ stream: true,
616
+ stream_options: { include_usage: true },
617
+ store: false,
618
+ })
619
+ }),
620
+ )
621
+
622
+ it.effect("replays Gemini thought signatures as tool call extra content", () =>
623
+ Effect.gen(function* () {
624
+ const prepared = yield* compileRequest(
625
+ ai.request({
626
+ model,
627
+ messages: [
628
+ Message.user("Weather in Paris and Tokyo?"),
629
+ Message.assistant([
630
+ ToolCallPart.make({
631
+ id: "call_1",
632
+ name: "lookup",
633
+ input: { city: "Paris" },
634
+ providerMetadata: { openai: { extraContent: { google: { thought_signature: "sig_1" } } } },
635
+ }),
636
+ ToolCallPart.make({ id: "call_2", name: "lookup", input: { city: "Tokyo" } }),
637
+ ]),
638
+ Message.tool({ id: "call_1", name: "lookup", result: "Sunny" }),
639
+ Message.tool({ id: "call_2", name: "lookup", result: "Rainy" }),
640
+ ],
641
+ }),
642
+ )
643
+
644
+ const assistant = prepared.body.messages[1]
645
+ expect(assistant?.role === "assistant" ? assistant.tool_calls : undefined).toEqual([
646
+ {
647
+ id: "call_1",
648
+ type: "function",
649
+ function: { name: "lookup", arguments: encodeJson({ city: "Paris" }) },
650
+ extra_content: { google: { thought_signature: "sig_1" } },
651
+ },
652
+ {
653
+ id: "call_2",
654
+ type: "function",
655
+ function: { name: "lookup", arguments: encodeJson({ city: "Tokyo" }) },
656
+ },
657
+ ])
658
+ }),
659
+ )
660
+
661
+ it.effect("limits OpenAI and Azure Chat tool call IDs to 40 characters", () =>
662
+ Effect.gen(function* () {
663
+ const id = `call_${"a".repeat(48)}`
664
+ const models = [
665
+ model,
666
+ Azure.configure({ baseURL: "https://opencode-test.openai.azure.com/openai/", apiKey: "test" }).chat("gpt-4o"),
667
+ ]
668
+
669
+ yield* Effect.forEach(models, (selected) =>
670
+ Effect.gen(function* () {
671
+ const prepared = yield* compileRequest(
672
+ ai.request({
673
+ model: selected,
674
+ messages: [
675
+ Message.assistant([ToolCallPart.make({ id, name: "lookup", input: {} })]),
676
+ Message.tool({ id, name: "lookup", result: "Sunny" }),
677
+ ],
678
+ }),
679
+ )
680
+
681
+ expect(prepared.body.messages).toMatchObject([
682
+ { role: "assistant", tool_calls: [{ id: id.slice(0, 40) }] },
683
+ { role: "tool", tool_call_id: id.slice(0, 40) },
684
+ ])
685
+ }),
686
+ )
687
+ }),
688
+ )
689
+
690
+ it.effect("preserves structured tool errors for the model", () =>
691
+ Effect.gen(function* () {
692
+ const error = { error: { type: "unknown", message: "Tool execution interrupted" } }
693
+ const prepared = yield* compileRequest(
694
+ ai.request({
695
+ model,
696
+ messages: [
697
+ Message.assistant([ToolCallPart.make({ id: "call_1", name: "bash", input: {} })]),
698
+ Message.tool({ id: "call_1", name: "bash", resultType: "error", result: error }),
699
+ ],
700
+ }),
701
+ )
702
+
703
+ expect(prepared.body.messages.at(-1)).toEqual({
704
+ role: "tool",
705
+ tool_call_id: "call_1",
706
+ content: ProviderShared.encodeJson(error),
707
+ })
708
+ }),
709
+ )
710
+
711
+ it.effect("continues image tool results as vision input without base64 text", () =>
712
+ Effect.gen(function* () {
713
+ const prepared = yield* compileRequest(
714
+ ai.request({
715
+ model,
716
+ messages: [
717
+ Message.assistant([ToolCallPart.make({ id: "call_image", name: "read", input: { path: "pixel.png" } })]),
718
+ Message.tool({
719
+ id: "call_image",
720
+ name: "read",
721
+ result: {
722
+ type: "content",
723
+ value: [
724
+ { type: "text", text: "Image read successfully" },
725
+ { type: "file", uri: "data:image/png;base64,AAECAw==", mime: "image/png", name: "pixel.png" },
726
+ ],
727
+ },
728
+ }),
729
+ ],
730
+ }),
731
+ )
732
+
733
+ expect(prepared.body.messages).toEqual([
734
+ {
735
+ role: "assistant",
736
+ content: null,
737
+ tool_calls: [
738
+ {
739
+ id: "call_image",
740
+ type: "function",
741
+ function: { name: "read", arguments: encodeJson({ path: "pixel.png" }) },
742
+ },
743
+ ],
744
+ },
745
+ { role: "tool", tool_call_id: "call_image", content: "Image read successfully" },
746
+ {
747
+ role: "user",
748
+ content: [{ type: "image_url", image_url: { url: "data:image/png;base64,AAECAw==" } }],
749
+ },
750
+ ])
751
+ expect(JSON.stringify(prepared.body.messages)).not.toContain('"content":"AAECAw=="')
752
+ }),
753
+ )
754
+
755
+ it.effect("bridges image tool results before their synthetic user message when required", () =>
756
+ Effect.gen(function* () {
757
+ const prepared = yield* compileRequest(
758
+ ai.request({
759
+ model: LanguageModel.update(model, { compatibility: { requireAssistantAfterTool: true } }),
760
+ messages: [
761
+ Message.assistant([ToolCallPart.make({ id: "call_image", name: "read", input: {} })]),
762
+ Message.tool({
763
+ id: "call_image",
764
+ name: "read",
765
+ result: {
766
+ type: "content",
767
+ value: [{ type: "file", uri: "data:image/png;base64,AAECAw==", mime: "image/png", name: "pixel.png" }],
768
+ },
769
+ }),
770
+ ],
771
+ }),
772
+ )
773
+
774
+ expect(prepared.body.messages.map((message) => message.role)).toEqual(["assistant", "tool", "assistant", "user"])
775
+ expect(prepared.body.messages[2]).toEqual({ role: "assistant", content: "Done." })
776
+ }),
777
+ )
778
+
779
+ it.effect("orders parallel tool responses before one aggregated vision message", () =>
780
+ Effect.gen(function* () {
781
+ const prepared = yield* compileRequest(
782
+ ai.request({
783
+ model,
784
+ messages: [
785
+ Message.assistant([
786
+ ToolCallPart.make({ id: "call_1", name: "read", input: {} }),
787
+ ToolCallPart.make({ id: "call_2", name: "read", input: {} }),
788
+ ]),
789
+ Message.make({
790
+ role: "tool",
791
+ content: [
792
+ {
793
+ type: "tool-result",
794
+ id: "call_1",
795
+ name: "read",
796
+ result: {
797
+ type: "content",
798
+ value: [{ type: "file", uri: "data:image/png;base64,AAEC", mime: "image/png" }],
799
+ },
800
+ },
801
+ {
802
+ type: "tool-result",
803
+ id: "call_2",
804
+ name: "read",
805
+ result: {
806
+ type: "content",
807
+ value: [{ type: "file", uri: "data:image/jpeg;base64,/9j/", mime: "image/jpeg" }],
808
+ },
809
+ },
810
+ ],
811
+ }),
812
+ ],
813
+ }),
814
+ )
815
+ expect(prepared.body.messages.slice(1)).toEqual([
816
+ { role: "tool", tool_call_id: "call_1", content: "" },
817
+ { role: "tool", tool_call_id: "call_2", content: "" },
818
+ {
819
+ role: "user",
820
+ content: [
821
+ { type: "image_url", image_url: { url: "data:image/png;base64,AAEC" } },
822
+ { type: "image_url", image_url: { url: "data:image/jpeg;base64,/9j/" } },
823
+ ],
824
+ },
825
+ ])
826
+ }),
827
+ )
828
+
829
+ it.effect("aggregates consecutive tool images with a following system update", () =>
830
+ Effect.gen(function* () {
831
+ const prepared = yield* compileRequest(
832
+ ai.request({
833
+ model,
834
+ messages: [
835
+ Message.tool({
836
+ id: "call_1",
837
+ name: "read",
838
+ result: {
839
+ type: "content",
840
+ value: [{ type: "file", uri: "data:image/png;base64,AAEC", mime: "image/png" }],
841
+ },
842
+ }),
843
+ Message.tool({
844
+ id: "call_2",
845
+ name: "read",
846
+ result: {
847
+ type: "content",
848
+ value: [{ type: "file", uri: "data:image/webp;base64,UklG", mime: "image/webp" }],
849
+ },
850
+ }),
851
+ Message.system("Inspect both images."),
852
+ ],
853
+ }),
854
+ )
855
+ expect(prepared.body.messages).toEqual([
856
+ { role: "tool", tool_call_id: "call_1", content: "" },
857
+ { role: "tool", tool_call_id: "call_2", content: "" },
858
+ {
859
+ role: "user",
860
+ content: [
861
+ { type: "image_url", image_url: { url: "data:image/png;base64,AAEC" } },
862
+ { type: "image_url", image_url: { url: "data:image/webp;base64,UklG" } },
863
+ { type: "text", text: "<system-update>\nInspect both images.\n</system-update>" },
864
+ ],
865
+ },
866
+ ])
867
+ }),
868
+ )
869
+
870
+ it.effect("appends system updates without replacing multipart user content", () =>
871
+ Effect.gen(function* () {
872
+ const prepared = yield* compileRequest(
873
+ ai.request({
874
+ model,
875
+ messages: [
876
+ Message.user({ type: "media", media: Media.base64("AAEC", "image/png") }),
877
+ Message.system("Keep the image."),
878
+ ],
879
+ }),
880
+ )
881
+ expect(prepared.body.messages).toEqual([
882
+ {
883
+ role: "user",
884
+ content: [
885
+ { type: "image_url", image_url: { url: "data:image/png;base64,AAEC" } },
886
+ { type: "text", text: "<system-update>\nKeep the image.\n</system-update>" },
887
+ ],
888
+ },
889
+ ])
890
+ }),
891
+ )
892
+
893
+ it.effect("passes encoded image media through without local validation", () =>
894
+ Effect.gen(function* () {
895
+ const prepared = yield* compileRequest(
896
+ ai.request({
897
+ model,
898
+ messages: [
899
+ Message.user([
900
+ { type: "media", media: Media.base64("not-base64", "image/png") },
901
+ { type: "media", media: Media.fromDataUrl("data:image/jpeg;base64,/9j/") },
902
+ { type: "media", media: Media.base64("PHN2Zz4=", "image/svg+xml") },
903
+ ]),
904
+ ],
905
+ }),
906
+ )
907
+ expect(prepared.body.messages).toEqual([
908
+ {
909
+ role: "user",
910
+ content: [
911
+ { type: "image_url", image_url: { url: "data:image/png;base64,not-base64" } },
912
+ { type: "image_url", image_url: { url: "data:image/jpeg;base64,/9j/" } },
913
+ { type: "image_url", image_url: { url: "data:image/svg+xml;base64,PHN2Zz4=" } },
914
+ ],
915
+ },
916
+ ])
917
+ }),
918
+ )
919
+
920
+ it.effect("preserves HTTP and HTTPS image URLs in user content", () =>
921
+ Effect.gen(function* () {
922
+ const urls = ["https://example.com/image.png?size=64#preview", "http://example.com/image.jpg"]
923
+ const prepared = yield* compileRequest(
924
+ ai.request({
925
+ model,
926
+ prompt: urls.map((url) => Message.media(Media.url(url, { mediaType: "image/png" }))),
927
+ }),
928
+ )
929
+ expect(prepared.body.messages).toEqual([
930
+ { role: "user", content: urls.map((url) => ({ type: "image_url", image_url: { url } })) },
931
+ ])
932
+ }),
933
+ )
934
+
935
+ it.effect("preserves remote image URLs from tool results", () =>
936
+ Effect.gen(function* () {
937
+ const url = "https://example.com/tool-image.png?version=2"
938
+ const prepared = yield* compileRequest(
939
+ ai.request({
940
+ model,
941
+ messages: [
942
+ Message.user("Describe the image."),
943
+ Message.assistant([ToolCallPart.make({ id: "call_image", name: "read_image", input: {} })]),
944
+ Message.tool({
945
+ id: "call_image",
946
+ name: "read_image",
947
+ resultType: "content",
948
+ result: [
949
+ { type: "text", text: "Image attached." },
950
+ { type: "file", mime: "image/png", uri: url },
951
+ ],
952
+ }),
953
+ ],
954
+ }),
955
+ )
956
+ expect(prepared.body.messages).toContainEqual({
957
+ role: "tool",
958
+ tool_call_id: "call_image",
959
+ content: "Image attached.",
960
+ })
961
+ expect(prepared.body.messages.at(-1)).toEqual({
962
+ role: "user",
963
+ content: [{ type: "image_url", image_url: { url } }],
964
+ })
965
+ }),
966
+ )
967
+
968
+ it.effect("rejects non-image media that cannot be lowered", () =>
969
+ Effect.gen(function* () {
970
+ const error = yield* compileRequest(
971
+ ai.request({
972
+ model,
973
+ messages: [Message.user({ type: "media", media: Media.base64("AAECAw==", "audio/mpeg") })],
974
+ }),
975
+ ).pipe(Effect.flip)
976
+ expect(error.message).toContain("OpenAI Chat does not support media type audio/mpeg")
977
+ }),
978
+ )
979
+
980
+ it.effect("lowers inline PDFs as file parts", () =>
981
+ Effect.gen(function* () {
982
+ const prepared = yield* compileRequest(
983
+ ai.request({
984
+ model,
985
+ messages: [
986
+ Message.user([
987
+ { type: "text", text: "Summarize these." },
988
+ { type: "media", media: Media.base64("JVBERi0=", "application/pdf"), filename: "report.pdf" },
989
+ { type: "media", media: Media.fromDataUrl("data:application/pdf;base64,JVBERi0=") },
990
+ ]),
991
+ ],
992
+ }),
993
+ )
994
+ expect(prepared.body.messages).toEqual([
995
+ {
996
+ role: "user",
997
+ content: [
998
+ { type: "text", text: "Summarize these." },
999
+ { type: "file", file: { filename: "report.pdf", file_data: "data:application/pdf;base64,JVBERi0=" } },
1000
+ { type: "file", file: { filename: "document.pdf", file_data: "data:application/pdf;base64,JVBERi0=" } },
1001
+ ],
1002
+ },
1003
+ ])
1004
+ }),
1005
+ )
1006
+
1007
+ it.effect("moves PDFs from tool results into a follow-up user message", () =>
1008
+ Effect.gen(function* () {
1009
+ const prepared = yield* compileRequest(
1010
+ ai.request({
1011
+ model,
1012
+ messages: [
1013
+ Message.user("Read the report."),
1014
+ Message.assistant([ToolCallPart.make({ id: "call_pdf", name: "read", input: {} })]),
1015
+ Message.tool({
1016
+ id: "call_pdf",
1017
+ name: "read",
1018
+ resultType: "content",
1019
+ result: [
1020
+ { type: "text", text: "PDF read successfully" },
1021
+ {
1022
+ type: "file",
1023
+ mime: "application/pdf",
1024
+ uri: "data:application/pdf;base64,JVBERi0=",
1025
+ name: "report.pdf",
1026
+ },
1027
+ ],
1028
+ }),
1029
+ ],
1030
+ }),
1031
+ )
1032
+ expect(prepared.body.messages).toContainEqual({
1033
+ role: "tool",
1034
+ tool_call_id: "call_pdf",
1035
+ content: "PDF read successfully",
1036
+ })
1037
+ expect(prepared.body.messages.at(-1)).toEqual({
1038
+ role: "user",
1039
+ content: [
1040
+ { type: "file", file: { filename: "report.pdf", file_data: "data:application/pdf;base64,JVBERi0=" } },
1041
+ ],
1042
+ })
1043
+ }),
1044
+ )
1045
+
1046
+ it.effect("requires inline data for PDF files", () =>
1047
+ Effect.gen(function* () {
1048
+ const error = yield* compileRequest(
1049
+ ai.request({
1050
+ model,
1051
+ messages: [
1052
+ Message.user({
1053
+ type: "media",
1054
+ media: Media.url("https://example.com/report.pdf", { mediaType: "application/pdf" }),
1055
+ }),
1056
+ ],
1057
+ }),
1058
+ ).pipe(Effect.flip)
1059
+ expect(error.message).toContain("OpenAI Chat requires inline media")
1060
+ }),
1061
+ )
1062
+
1063
+ it.effect("prepares raw and data URL image media as vision input", () =>
1064
+ Effect.gen(function* () {
1065
+ const prepared = yield* compileRequest(
1066
+ ai.request({
1067
+ id: "req_media",
1068
+ model,
1069
+ messages: [
1070
+ Message.user([
1071
+ { type: "media", media: Media.base64("AAECAw==", "image/png") },
1072
+ { type: "media", media: Media.fromDataUrl("data:image/jpeg;base64,/9j/") },
1073
+ ]),
1074
+ ],
1075
+ }),
1076
+ )
1077
+
1078
+ expect(prepared.body.messages).toEqual([
1079
+ {
1080
+ role: "user",
1081
+ content: [
1082
+ { type: "image_url", image_url: { url: "data:image/png;base64,AAECAw==" } },
1083
+ { type: "image_url", image_url: { url: "data:image/jpeg;base64,/9j/" } },
1084
+ ],
1085
+ },
1086
+ ])
1087
+ }),
1088
+ )
1089
+
1090
+ it.effect("lowers reasoning-only assistant history", () =>
1091
+ Effect.gen(function* () {
1092
+ const prepared = yield* compileRequest(
1093
+ ai.request({
1094
+ id: "req_reasoning",
1095
+ model,
1096
+ messages: [Message.assistant({ type: "reasoning", text: "hidden" })],
1097
+ }),
1098
+ )
1099
+
1100
+ expect(prepared.body.messages).toEqual([{ role: "assistant", content: "", reasoning_content: "hidden" }])
1101
+ }),
1102
+ )
1103
+
1104
+ it.effect("parses text and usage stream fixtures", () =>
1105
+ Effect.gen(function* () {
1106
+ const body = sseEvents(
1107
+ deltaChunk({ role: "assistant", content: "Hello" }),
1108
+ deltaChunk({ content: "!" }),
1109
+ deltaChunk({}, "stop"),
1110
+ usageChunk({
1111
+ prompt_tokens: 5,
1112
+ completion_tokens: 2,
1113
+ total_tokens: 7,
1114
+ prompt_tokens_details: { cached_tokens: 1, cache_write_tokens: 2 },
1115
+ completion_tokens_details: { reasoning_tokens: 0 },
1116
+ }),
1117
+ )
1118
+ const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
1119
+ const usage = new Usage({
1120
+ inputTokens: 5,
1121
+ outputTokens: 2,
1122
+ nonCachedInputTokens: 2,
1123
+ cacheReadInputTokens: 1,
1124
+ cacheWriteInputTokens: 2,
1125
+ reasoningTokens: 0,
1126
+ totalTokens: 7,
1127
+ providerMetadata: {
1128
+ openai: {
1129
+ prompt_tokens: 5,
1130
+ completion_tokens: 2,
1131
+ total_tokens: 7,
1132
+ prompt_tokens_details: { cached_tokens: 1, cache_write_tokens: 2 },
1133
+ completion_tokens_details: { reasoning_tokens: 0 },
1134
+ },
1135
+ },
1136
+ })
1137
+
1138
+ expect(response.text).toBe("Hello!")
1139
+ expect(response.events).toEqual([
1140
+ { type: "step-start", index: 0 },
1141
+ { type: "text-start", id: "text-0" },
1142
+ { type: "text-delta", id: "text-0", text: "Hello" },
1143
+ { type: "text-delta", id: "text-0", text: "!" },
1144
+ { type: "text-end", id: "text-0" },
1145
+ {
1146
+ type: "step-finish",
1147
+ index: 0,
1148
+ reason: { normalized: "stop", raw: "stop" },
1149
+ usage,
1150
+ providerMetadata: undefined,
1151
+ },
1152
+ {
1153
+ type: "finish",
1154
+ reason: { normalized: "stop", raw: "stop" },
1155
+ usage,
1156
+ },
1157
+ ])
1158
+ }),
1159
+ )
1160
+
1161
+ it.effect("finishes at the done sentinel without waiting for response EOF", () =>
1162
+ Effect.gen(function* () {
1163
+ const stream = new ReadableStream<Uint8Array>({
1164
+ start(controller) {
1165
+ controller.enqueue(
1166
+ new TextEncoder().encode(sseEvents(deltaChunk({ content: "Hello" }), deltaChunk({}, "stop"))),
1167
+ )
1168
+ },
1169
+ })
1170
+ const response = yield* LLMClient.generate(request).pipe(
1171
+ Effect.provide(
1172
+ fixedResponse(stream, {
1173
+ headers: { "content-type": "text/event-stream" },
1174
+ }),
1175
+ ),
1176
+ )
1177
+
1178
+ expect(response.text).toBe("Hello")
1179
+ expect(response.events.at(-1)?.type).toBe("finish")
1180
+ }),
1181
+ )
1182
+
1183
+ it.effect("preserves streamed refusals as ordinary assistant text", () =>
1184
+ Effect.gen(function* () {
1185
+ const response = yield* LLMClient.generate(request).pipe(
1186
+ Effect.provide(
1187
+ fixedResponse(
1188
+ sseEvents(
1189
+ deltaChunk({ role: "assistant", refusal: "I can't" }),
1190
+ deltaChunk({ refusal: " help with that." }),
1191
+ deltaChunk({}, "stop"),
1192
+ ),
1193
+ ),
1194
+ ),
1195
+ )
1196
+
1197
+ expect(response.text).toBe("I can't help with that.")
1198
+ expect(response.finishReason).toEqual({ normalized: "stop", raw: "stop" })
1199
+ expect(response.message.content).toEqual([{ type: "text", text: "I can't help with that." }])
1200
+
1201
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1202
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "I can't help with that." }])
1203
+ }),
1204
+ )
1205
+
1206
+ it.effect("orders metadata-only reasoning before refusal output", () =>
1207
+ Effect.gen(function* () {
1208
+ const response = yield* LLMClient.generate(request).pipe(
1209
+ Effect.provide(
1210
+ fixedResponse(
1211
+ sseEvents(
1212
+ { choices: [{ delta: { reasoning_details: [] } }] },
1213
+ deltaChunk({ refusal: "I can't help with that." }),
1214
+ deltaChunk({}, "stop"),
1215
+ ),
1216
+ ),
1217
+ ),
1218
+ )
1219
+
1220
+ expect(response.message.content).toEqual([
1221
+ { type: "reasoning", text: "", providerMetadata: { openai: { reasoningDetails: [] } } },
1222
+ {
1223
+ type: "text",
1224
+ text: "I can't help with that.",
1225
+ },
1226
+ ])
1227
+ }),
1228
+ )
1229
+
1230
+ it.effect("joins content and refusal deltas into ordinary assistant text", () =>
1231
+ Effect.gen(function* () {
1232
+ const response = yield* LLMClient.generate(request).pipe(
1233
+ Effect.provide(
1234
+ fixedResponse(
1235
+ sseEvents(
1236
+ deltaChunk({ refusal: "No." }),
1237
+ deltaChunk({ content: " Alternative." }),
1238
+ deltaChunk({ refusal: " Still no." }),
1239
+ deltaChunk({}, "stop"),
1240
+ ),
1241
+ ),
1242
+ ),
1243
+ )
1244
+
1245
+ expect(response.text).toBe("No. Alternative. Still no.")
1246
+ expect(response.events.filter(LLMEvent.is.textStart).map((event) => event.id)).toEqual(["text-0"])
1247
+ expect(response.events.filter(LLMEvent.is.textEnd).map((event) => event.id)).toEqual(["text-0"])
1248
+ }),
1249
+ )
1250
+
1251
+ it.effect("parses and replays OpenAI-compatible reasoning fields", () =>
1252
+ Effect.gen(function* () {
1253
+ const fields = ["reasoning_content", "reasoning", "reasoning_text"] as const
1254
+ for (const field of fields) {
1255
+ const response = yield* LLMClient.generate(request).pipe(
1256
+ Effect.provide(
1257
+ fixedResponse(
1258
+ sseEvents(
1259
+ { choices: [{ delta: { [field]: "thinking" } }] },
1260
+ { choices: [{ delta: { content: "Hello" } }] },
1261
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1262
+ ),
1263
+ ),
1264
+ ),
1265
+ )
1266
+
1267
+ expect(response.reasoning).toBe("thinking")
1268
+ expect(response.text).toBe("Hello")
1269
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1270
+ openai: { reasoningField: field },
1271
+ })
1272
+
1273
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1274
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", [field]: "thinking" }])
1275
+ }
1276
+ }),
1277
+ )
1278
+
1279
+ // A mid-session model switch can keep the same provider metadata key while the new model reads reasoning from a
1280
+ // declared field of its own; details replayed from the earlier model are refused by some endpoints.
1281
+ it.effect("does not replay reasoning details when the model declares another reasoning field", () =>
1282
+ Effect.gen(function* () {
1283
+ const details = [
1284
+ { type: "reasoning.text", text: "thinking", signature: "signed", format: "anthropic-claude-v1", index: 0 },
1285
+ ]
1286
+ const replay = yield* compileRequest(
1287
+ ai.request({
1288
+ model: LanguageModel.update(model, { compatibility: { reasoningField: "reasoning_content" } }),
1289
+ messages: [
1290
+ Message.assistant([
1291
+ {
1292
+ type: "reasoning",
1293
+ text: "thinking",
1294
+ providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: details } },
1295
+ },
1296
+ { type: "text", text: "Hello" },
1297
+ ]),
1298
+ ],
1299
+ }),
1300
+ )
1301
+
1302
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_content: "thinking" }])
1303
+ }),
1304
+ )
1305
+
1306
+ it.effect("keeps replayed reasoning details when the model declares reasoning_details", () =>
1307
+ Effect.gen(function* () {
1308
+ const details = [
1309
+ { type: "reasoning.text", text: "thinking", signature: "signed", format: "anthropic-claude-v1", index: 0 },
1310
+ ]
1311
+ const replay = yield* compileRequest(
1312
+ ai.request({
1313
+ model: LanguageModel.update(model, { compatibility: { reasoningField: "reasoning_details" } }),
1314
+ messages: [
1315
+ Message.assistant([
1316
+ {
1317
+ type: "reasoning",
1318
+ text: "thinking",
1319
+ providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: details } },
1320
+ },
1321
+ { type: "text", text: "Hello" },
1322
+ ]),
1323
+ ],
1324
+ }),
1325
+ )
1326
+
1327
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_details: details }])
1328
+ }),
1329
+ )
1330
+
1331
+ it.effect("keeps replayed reasoning details when the model declares no reasoning field", () =>
1332
+ Effect.gen(function* () {
1333
+ const details = [
1334
+ { type: "reasoning.text", text: "thinking", signature: "signed", format: "anthropic-claude-v1", index: 0 },
1335
+ ]
1336
+ const replay = yield* compileRequest(
1337
+ ai.request({
1338
+ model,
1339
+ messages: [
1340
+ Message.assistant([
1341
+ {
1342
+ type: "reasoning",
1343
+ text: "thinking",
1344
+ providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: details } },
1345
+ },
1346
+ { type: "text", text: "Hello" },
1347
+ ]),
1348
+ ],
1349
+ }),
1350
+ )
1351
+
1352
+ expect(replay.body.messages).toEqual([
1353
+ { role: "assistant", content: "Hello", reasoning: "thinking", reasoning_details: details },
1354
+ ])
1355
+ }),
1356
+ )
1357
+
1358
+ it.effect("reports the cost the endpoint billed", () =>
1359
+ Effect.gen(function* () {
1360
+ const response = yield* LLMClient.generate(request).pipe(
1361
+ Effect.provide(
1362
+ fixedResponse(
1363
+ sseEvents(
1364
+ deltaChunk({ content: "Hello" }, "stop"),
1365
+ usageChunk({ prompt_tokens: 5, completion_tokens: 2, cost: 0, opencode: { allowance: 0.0042 } }),
1366
+ ),
1367
+ ),
1368
+ ),
1369
+ )
1370
+
1371
+ expect(response.usage).toMatchObject({ inputTokens: 5, outputTokens: 2, cost: 0 })
1372
+ }),
1373
+ )
1374
+
1375
+ it.effect("reads top-level reasoning tokens whether or not completion tokens include them", () =>
1376
+ Effect.gen(function* () {
1377
+ const generate = (usage: object) =>
1378
+ LLMClient.generate(request).pipe(
1379
+ Effect.provide(fixedResponse(sseEvents(deltaChunk({ content: "Hi" }, "stop"), usageChunk(usage)))),
1380
+ Effect.map((response) => response.usage),
1381
+ )
1382
+
1383
+ // Copilot's Gemini: total = prompt + completion + reasoning.
1384
+ expect(
1385
+ yield* generate({ prompt_tokens: 7, completion_tokens: 2, total_tokens: 62, reasoning_tokens: 53 }),
1386
+ ).toMatchObject({ outputTokens: 55, reasoningTokens: 53, totalTokens: 62 })
1387
+ // Copilot's Kimi: total = prompt + completion, which already includes reasoning.
1388
+ expect(
1389
+ yield* generate({ prompt_tokens: 96, completion_tokens: 50, total_tokens: 146, reasoning_tokens: 35 }),
1390
+ ).toMatchObject({ outputTokens: 50, reasoningTokens: 35, totalTokens: 146 })
1391
+ }),
1392
+ )
1393
+
1394
+ it.effect("converts the GitHub Copilot billing beside usage to USD", () =>
1395
+ Effect.gen(function* () {
1396
+ const response = yield* LLMClient.generate(request).pipe(
1397
+ Effect.provide(
1398
+ fixedResponse(
1399
+ sseEvents(deltaChunk({ content: "Hello" }, "stop"), {
1400
+ ...usageChunk({ prompt_tokens: 5, completion_tokens: 2 }),
1401
+ copilot_usage: { total_nano_aiu: 4_473_525_000, token_details: [] },
1402
+ }),
1403
+ ),
1404
+ ),
1405
+ )
1406
+
1407
+ expect(response.usage).toMatchObject({ inputTokens: 5, outputTokens: 2, cost: 0.04473525 })
1408
+ }),
1409
+ )
1410
+
1411
+ it.effect("drops a malformed billed cost without failing the stream", () =>
1412
+ Effect.gen(function* () {
1413
+ const response = yield* LLMClient.generate(request).pipe(
1414
+ Effect.provide(
1415
+ fixedResponse(
1416
+ sseEvents(
1417
+ deltaChunk({ content: "Hello" }, "stop"),
1418
+ usageChunk({ prompt_tokens: 5, completion_tokens: 2, cost: -1, opencode: { allowance: "lots" } }),
1419
+ ),
1420
+ ),
1421
+ ),
1422
+ )
1423
+
1424
+ expect(response.text).toBe("Hello")
1425
+ expect(response.usage).toMatchObject({ inputTokens: 5, outputTokens: 2 })
1426
+ expect(response.usage?.cost).toBeUndefined()
1427
+ }),
1428
+ )
1429
+
1430
+ it.effect("uses the configured provider metadata namespace for reasoning and usage", () =>
1431
+ Effect.gen(function* () {
1432
+ const selected = LanguageModel.update(model, {
1433
+ route: { ...model.route, providerMetadataKey: "vendor" },
1434
+ })
1435
+ const details = [{ type: "reasoning.text", text: "thinking", signature: "signed" }]
1436
+ const response = yield* LLMClient.generate({ ...request, model: selected }).pipe(
1437
+ Effect.provide(
1438
+ fixedResponse(
1439
+ sseEvents(
1440
+ { choices: [{ delta: { reasoning: "thinking", reasoning_details: details } }] },
1441
+ deltaChunk({ content: "Hello" }),
1442
+ deltaChunk({}, "stop"),
1443
+ usageChunk({ prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 }),
1444
+ ),
1445
+ ),
1446
+ ),
1447
+ )
1448
+
1449
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1450
+ vendor: { reasoningField: "reasoning", reasoningDetails: details },
1451
+ })
1452
+ expect(response.usage?.providerMetadata).toEqual({
1453
+ vendor: { prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 },
1454
+ })
1455
+
1456
+ const replay = yield* compileRequest(ai.request({ model: selected, messages: [response.message] }))
1457
+ expect(replay.body.messages).toEqual([
1458
+ { role: "assistant", content: "Hello", reasoning: "thinking", reasoning_details: details },
1459
+ ])
1460
+ }),
1461
+ )
1462
+
1463
+ it.effect("falls back to the selected provider for the metadata namespace", () =>
1464
+ Effect.gen(function* () {
1465
+ const compatible = model.route.with({ provider: "deepseek" }).model({ id: "deepseek-chat" })
1466
+ const selected = LanguageModel.update(compatible, {
1467
+ route: { ...compatible.route, providerMetadataKey: undefined },
1468
+ })
1469
+ const response = yield* LLMClient.generate({ ...request, model: selected }).pipe(
1470
+ Effect.provide(
1471
+ fixedResponse(
1472
+ sseEvents(
1473
+ deltaChunk({ reasoning_content: "thinking" }),
1474
+ deltaChunk({ content: "Hello" }),
1475
+ deltaChunk({}, "stop"),
1476
+ usageChunk({ prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 }),
1477
+ ),
1478
+ ),
1479
+ ),
1480
+ )
1481
+
1482
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1483
+ deepseek: { reasoningField: "reasoning_content" },
1484
+ })
1485
+ expect(response.usage?.providerMetadata).toEqual({
1486
+ deepseek: { prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 },
1487
+ })
1488
+
1489
+ const replay = yield* compileRequest(ai.request({ model: selected, messages: [response.message] }))
1490
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_content: "thinking" }])
1491
+ }),
1492
+ )
1493
+
1494
+ it.effect("parses and replays a configured custom reasoning field", () =>
1495
+ Effect.gen(function* () {
1496
+ const custom = LanguageModel.update(model, { compatibility: { reasoningField: "vendor_reasoning" } })
1497
+ const response = yield* LLMClient.generate({ ...request, model: custom }).pipe(
1498
+ Effect.provide(
1499
+ fixedResponse(
1500
+ sseEvents(
1501
+ { choices: [{ delta: { vendor_reasoning: "thinking" } }] },
1502
+ { choices: [{ delta: { content: "Hello" } }] },
1503
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1504
+ ),
1505
+ ),
1506
+ ),
1507
+ )
1508
+
1509
+ expect(response.reasoning).toBe("thinking")
1510
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1511
+ openai: { reasoningField: "vendor_reasoning" },
1512
+ })
1513
+
1514
+ const replay = yield* compileRequest(ai.request({ model: custom, messages: [response.message] }))
1515
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", vendor_reasoning: "thinking" }])
1516
+ }),
1517
+ )
1518
+
1519
+ it.effect("preserves and replays reasoning details alongside scalar reasoning", () =>
1520
+ Effect.gen(function* () {
1521
+ const details = [
1522
+ { type: "reasoning.text", text: "thinking", format: "anthropic-claude-v1", index: 0 },
1523
+ { type: "reasoning.encrypted", data: "opaque", format: "anthropic-claude-v1", index: 1 },
1524
+ ]
1525
+ const response = yield* LLMClient.generate({
1526
+ ...request,
1527
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
1528
+ }).pipe(
1529
+ Effect.provide(
1530
+ fixedResponse(
1531
+ sseEvents(
1532
+ { choices: [{ delta: { reasoning: "thinking", reasoning_details: [details[0]] } }] },
1533
+ { choices: [{ delta: { reasoning_details: [details[1]] } }] },
1534
+ {
1535
+ choices: [
1536
+ {
1537
+ delta: {
1538
+ tool_calls: [
1539
+ { index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query":"weather"}' } },
1540
+ ],
1541
+ },
1542
+ finish_reason: "tool_calls",
1543
+ },
1544
+ ],
1545
+ },
1546
+ ),
1547
+ ),
1548
+ ),
1549
+ )
1550
+
1551
+ expect(response.reasoning).toBe("thinking")
1552
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1553
+ openai: { reasoningField: "reasoning", reasoningDetails: details },
1554
+ })
1555
+
1556
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1557
+ expect(replay.body.messages[0]).toEqual({
1558
+ role: "assistant",
1559
+ content: null,
1560
+ reasoning: "thinking",
1561
+ reasoning_details: details,
1562
+ tool_calls: [
1563
+ {
1564
+ id: "call_1",
1565
+ type: "function",
1566
+ function: { name: "lookup", arguments: '{"query":"weather"}' },
1567
+ },
1568
+ ],
1569
+ })
1570
+ }),
1571
+ )
1572
+
1573
+ it.effect("uses reasoning details as display fallback without inventing a scalar replay field", () =>
1574
+ Effect.gen(function* () {
1575
+ const details = [
1576
+ { type: "reasoning.summary", summary: "thinking", format: "openai-responses-v1", index: 0 },
1577
+ { type: "reasoning.encrypted", data: "opaque", format: "openai-responses-v1", index: 1 },
1578
+ ]
1579
+ const response = yield* LLMClient.generate(request).pipe(
1580
+ Effect.provide(
1581
+ fixedResponse(
1582
+ sseEvents(
1583
+ { choices: [{ delta: { reasoning_details: [details[0]] } }] },
1584
+ { choices: [{ delta: { reasoning_details: [details[1]] } }] },
1585
+ { choices: [{ delta: { content: "Hello" } }] },
1586
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1587
+ ),
1588
+ ),
1589
+ ),
1590
+ )
1591
+
1592
+ expect(response.reasoning).toBe("thinking")
1593
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1594
+ openai: { reasoningDetails: details },
1595
+ })
1596
+
1597
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1598
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_details: details }])
1599
+ }),
1600
+ )
1601
+
1602
+ // Only recognized detail shapes are retained and replayed; echoing an
1603
+ // undocumented provider payload is what breaks follow-up requests.
1604
+ it.effect("drops unknown reasoning details while using scalar display text", () =>
1605
+ Effect.gen(function* () {
1606
+ const details = [{ type: "reasoning.future", format: "provider-v2", state: { opaque: true } }]
1607
+ const response = yield* LLMClient.generate(request).pipe(
1608
+ Effect.provide(
1609
+ fixedResponse(
1610
+ sseEvents(
1611
+ { choices: [{ delta: { reasoning: "thinking", reasoning_details: details } }] },
1612
+ { choices: [{ delta: { content: "Hello" } }] },
1613
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1614
+ ),
1615
+ ),
1616
+ ),
1617
+ )
1618
+
1619
+ expect(response.reasoning).toBe("thinking")
1620
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1621
+ openai: { reasoningField: "reasoning", reasoningDetails: [] },
1622
+ })
1623
+
1624
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1625
+ expect(replay.body.messages).toEqual([
1626
+ { role: "assistant", content: "Hello", reasoning: "thinking", reasoning_details: [] },
1627
+ ])
1628
+ }),
1629
+ )
1630
+
1631
+ // Kimi's coding endpoint streams the full thinking through `reasoning_content`
1632
+ // and a separate summary + encrypted blob through its own `reasoning_details`
1633
+ // dialect. The stream-only `index` must not be echoed back.
1634
+ it.effect("merges Kimi summary deltas by index and replays details without the streaming index", () =>
1635
+ Effect.gen(function* () {
1636
+ const response = yield* LLMClient.generate(request).pipe(
1637
+ Effect.provide(
1638
+ fixedResponse(
1639
+ sseEvents(
1640
+ { choices: [{ delta: { reasoning_content: "Let me" } }] },
1641
+ { choices: [{ delta: { reasoning_content: " think" } }] },
1642
+ { choices: [{ delta: { reasoning_details: [{ index: 0, type: "summary", summary: "Plan" }] } }] },
1643
+ { choices: [{ delta: { reasoning_details: [{ index: 0, type: "summary", summary: " tools" }] } }] },
1644
+ { choices: [{ delta: { reasoning_details: [{ index: 1, type: "encrypted", encrypted: "opaque" }] } }] },
1645
+ {
1646
+ choices: [
1647
+ {
1648
+ delta: {
1649
+ tool_calls: [
1650
+ { index: 0, id: "call_1", type: "function", function: { name: "get_time", arguments: "{}" } },
1651
+ ],
1652
+ },
1653
+ },
1654
+ ],
1655
+ },
1656
+ { choices: [{ delta: {}, finish_reason: "tool_calls" }] },
1657
+ ),
1658
+ ),
1659
+ ),
1660
+ )
1661
+
1662
+ const stored = [
1663
+ { index: 0, type: "summary", summary: "Plan tools" },
1664
+ { index: 1, type: "encrypted", encrypted: "opaque" },
1665
+ ]
1666
+ expect(response.reasoning).toBe("Let me think")
1667
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1668
+ openai: { reasoningField: "reasoning_content", reasoningDetails: stored },
1669
+ })
1670
+
1671
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1672
+ expect(replay.body.messages[0]).toEqual({
1673
+ role: "assistant",
1674
+ content: null,
1675
+ tool_calls: [{ id: "call_1", type: "function", function: { name: "get_time", arguments: "{}" } }],
1676
+ reasoning_content: "Let me think",
1677
+ reasoning_details: [
1678
+ { type: "summary", summary: "Plan tools" },
1679
+ { type: "encrypted", encrypted: "opaque" },
1680
+ ],
1681
+ })
1682
+ }),
1683
+ )
1684
+
1685
+ it.effect("displays Kimi summaries and replays reasoning_content when no scalar reasoning streams", () =>
1686
+ Effect.gen(function* () {
1687
+ const response = yield* LLMClient.generate(request).pipe(
1688
+ Effect.provide(
1689
+ fixedResponse(
1690
+ sseEvents(
1691
+ { choices: [{ delta: { reasoning_details: [{ index: 0, type: "summary", summary: "Plan" }] } }] },
1692
+ { choices: [{ delta: { reasoning_details: [{ index: 0, type: "summary", summary: " tools" }] } }] },
1693
+ { choices: [{ delta: { reasoning_details: [{ index: 1, type: "encrypted", encrypted: "opaque" }] } }] },
1694
+ { choices: [{ delta: { content: "Hello" } }] },
1695
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1696
+ ),
1697
+ ),
1698
+ ),
1699
+ )
1700
+
1701
+ expect(response.reasoning).toBe("Plan tools")
1702
+
1703
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1704
+ expect(replay.body.messages).toEqual([
1705
+ {
1706
+ role: "assistant",
1707
+ content: "Hello",
1708
+ reasoning_content: "Plan tools",
1709
+ reasoning_details: [
1710
+ { type: "summary", summary: "Plan tools" },
1711
+ { type: "encrypted", encrypted: "opaque" },
1712
+ ],
1713
+ },
1714
+ ])
1715
+ }),
1716
+ )
1717
+
1718
+ // Sessions persisted before the fix already hold Kimi details with `index`.
1719
+ it.effect("strips the streaming index from previously stored Kimi details", () =>
1720
+ Effect.gen(function* () {
1721
+ const replay = yield* compileRequest(
1722
+ ai.request({
1723
+ model,
1724
+ messages: [
1725
+ Message.assistant([
1726
+ {
1727
+ type: "reasoning",
1728
+ text: "thinking",
1729
+ providerMetadata: {
1730
+ openai: {
1731
+ reasoningField: "reasoning_content",
1732
+ reasoningDetails: [
1733
+ { index: 0, type: "summary", summary: "thinking" },
1734
+ { index: 1, type: "encrypted", encrypted: "opaque" },
1735
+ ],
1736
+ },
1737
+ },
1738
+ },
1739
+ ]),
1740
+ ],
1741
+ }),
1742
+ )
1743
+
1744
+ expect(replay.body.messages).toEqual([
1745
+ {
1746
+ role: "assistant",
1747
+ content: "",
1748
+ reasoning_content: "thinking",
1749
+ reasoning_details: [
1750
+ { type: "summary", summary: "thinking" },
1751
+ { type: "encrypted", encrypted: "opaque" },
1752
+ ],
1753
+ },
1754
+ ])
1755
+ }),
1756
+ )
1757
+
1758
+ it.effect("merges consecutive OpenRouter summary deltas and replays them unmodified", () =>
1759
+ Effect.gen(function* () {
1760
+ const merged = [
1761
+ { type: "reasoning.summary", summary: "Plan tools", format: "openai-responses-v1", index: 0 },
1762
+ { type: "reasoning.encrypted", data: "opaque", format: "openai-responses-v1", index: 0 },
1763
+ ]
1764
+ const response = yield* LLMClient.generate(request).pipe(
1765
+ Effect.provide(
1766
+ fixedResponse(
1767
+ sseEvents(
1768
+ {
1769
+ choices: [
1770
+ {
1771
+ delta: {
1772
+ reasoning_details: [
1773
+ { type: "reasoning.summary", summary: "Plan", format: "openai-responses-v1", index: 0 },
1774
+ ],
1775
+ },
1776
+ },
1777
+ ],
1778
+ },
1779
+ {
1780
+ choices: [
1781
+ {
1782
+ delta: {
1783
+ reasoning_details: [
1784
+ { type: "reasoning.summary", summary: " tools", format: "openai-responses-v1", index: 0 },
1785
+ ],
1786
+ },
1787
+ },
1788
+ ],
1789
+ },
1790
+ { choices: [{ delta: { reasoning_details: [merged[1]] } }] },
1791
+ { choices: [{ delta: { content: "Hello" } }] },
1792
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1793
+ ),
1794
+ ),
1795
+ ),
1796
+ )
1797
+
1798
+ expect(response.reasoning).toBe("Plan tools")
1799
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1800
+ openai: { reasoningDetails: merged },
1801
+ })
1802
+
1803
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1804
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_details: merged }])
1805
+ }),
1806
+ )
1807
+
1808
+ it.effect("uses scalar display text for signature-only reasoning details", () =>
1809
+ Effect.gen(function* () {
1810
+ const details = [{ type: "reasoning.text", signature: "signed", format: "provider-v2", index: 0 }]
1811
+ const response = yield* LLMClient.generate(request).pipe(
1812
+ Effect.provide(
1813
+ fixedResponse(
1814
+ sseEvents(
1815
+ { choices: [{ delta: { reasoning: "thinking", reasoning_details: details } }] },
1816
+ { choices: [{ delta: { content: "Hello" } }] },
1817
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1818
+ ),
1819
+ ),
1820
+ ),
1821
+ )
1822
+
1823
+ expect(response.reasoning).toBe("thinking")
1824
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1825
+ openai: { reasoningField: "reasoning", reasoningDetails: details },
1826
+ })
1827
+ }),
1828
+ )
1829
+
1830
+ it.effect("preserves scalar reasoning after content starts in one lifecycle", () =>
1831
+ Effect.gen(function* () {
1832
+ const details = [{ type: "reasoning.text", text: "detail", format: "unknown", index: 0 }]
1833
+ const response = yield* LLMClient.generate(request).pipe(
1834
+ Effect.provide(
1835
+ fixedResponse(
1836
+ sseEvents(
1837
+ { choices: [{ delta: { reasoning_details: details } }] },
1838
+ { choices: [{ delta: { content: "Hello" } }] },
1839
+ { choices: [{ delta: { reasoning: "scalar" } }] },
1840
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1841
+ ),
1842
+ ),
1843
+ ),
1844
+ )
1845
+
1846
+ expect(response.reasoning).toBe("detailscalar")
1847
+ expect(response.events.filter(LLMEvent.is.reasoningStart)).toHaveLength(1)
1848
+ expect(response.events.filter(LLMEvent.is.reasoningEnd)).toHaveLength(1)
1849
+ expect(response.message.content.filter((part) => part.type === "reasoning")).toHaveLength(1)
1850
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1851
+ openai: { reasoningField: "reasoning", reasoningDetails: details },
1852
+ })
1853
+ }),
1854
+ )
1855
+
1856
+ it.effect("keeps one reasoning lifecycle across many content chunks", () =>
1857
+ Effect.gen(function* () {
1858
+ const details = [{ type: "reasoning.text", text: "thinking", format: "anthropic-claude-v1", index: 0 }]
1859
+ const response = yield* LLMClient.generate(request).pipe(
1860
+ Effect.provide(
1861
+ fixedResponse(
1862
+ sseEvents(
1863
+ { choices: [{ delta: { reasoning: "thinking", reasoning_details: details } }] },
1864
+ ...Array.from({ length: 25 }, (_, index) => deltaChunk({ content: `chunk-${index} ` })),
1865
+ deltaChunk({}, "stop"),
1866
+ ),
1867
+ ),
1868
+ ),
1869
+ )
1870
+
1871
+ expect(response.reasoning).toBe("thinking")
1872
+ expect(response.text).toBe(Array.from({ length: 25 }, (_, index) => `chunk-${index} `).join(""))
1873
+ expect(response.events.filter(LLMEvent.is.reasoningStart)).toHaveLength(1)
1874
+ expect(response.events.filter(LLMEvent.is.reasoningEnd)).toHaveLength(1)
1875
+ expect(response.message.content.filter((part) => part.type === "reasoning")).toHaveLength(1)
1876
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1877
+ openai: { reasoningField: "reasoning", reasoningDetails: details },
1878
+ })
1879
+ }),
1880
+ )
1881
+
1882
+ it.effect("preserves an explicitly empty reasoning details array", () =>
1883
+ Effect.gen(function* () {
1884
+ const response = yield* LLMClient.generate(request).pipe(
1885
+ Effect.provide(
1886
+ fixedResponse(
1887
+ sseEvents(
1888
+ { choices: [{ delta: { reasoning_details: [] } }] },
1889
+ { choices: [{ delta: { content: "Hello" } }] },
1890
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1891
+ ),
1892
+ ),
1893
+ ),
1894
+ )
1895
+
1896
+ expect(response.reasoning).toBe("")
1897
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1898
+ openai: { reasoningDetails: [] },
1899
+ })
1900
+
1901
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1902
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_details: [] }])
1903
+ }),
1904
+ )
1905
+
1906
+ it.effect("attaches signature-only details that arrive after content", () =>
1907
+ Effect.gen(function* () {
1908
+ const details = [
1909
+ { type: "reasoning.text", text: "thinking", format: "anthropic-claude-v1", index: 0 },
1910
+ { type: "reasoning.text", signature: "signed", format: "anthropic-claude-v1", index: 0 },
1911
+ ]
1912
+ const merged = [
1913
+ {
1914
+ type: "reasoning.text",
1915
+ text: "thinking",
1916
+ signature: "signed",
1917
+ format: "anthropic-claude-v1",
1918
+ index: 0,
1919
+ },
1920
+ ]
1921
+ // Snapshot reasoning-end metadata as each event is published so the
1922
+ // assertion cannot pass through later mutation of a shared array.
1923
+ const publishedEndMetadata: unknown[] = []
1924
+ const response = yield* LLMClient.stream(request).pipe(
1925
+ Stream.tap((event) =>
1926
+ Effect.sync(() => {
1927
+ if (LLMEvent.is.reasoningEnd(event))
1928
+ publishedEndMetadata.push(decodeJson(encodeJson(event.providerMetadata)))
1929
+ }),
1930
+ ),
1931
+ Stream.runCollect,
1932
+ Effect.map((events) => LLMResponse.fromEvents(events)!),
1933
+ Effect.provide(
1934
+ fixedResponse(
1935
+ sseEvents(
1936
+ { choices: [{ delta: { reasoning: "thinking", reasoning_details: [details[0]] } }] },
1937
+ { choices: [{ delta: { content: "Hello" } }] },
1938
+ { choices: [{ delta: { reasoning_details: [details[1]] } }] },
1939
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1940
+ ),
1941
+ ),
1942
+ ),
1943
+ )
1944
+
1945
+ expect(response.reasoning).toBe("thinking")
1946
+ expect(response.message.content.filter((part) => part.type === "reasoning")).toHaveLength(1)
1947
+ expect(response.message.content.find((part) => part.type === "reasoning")?.providerMetadata).toEqual({
1948
+ openai: { reasoningField: "reasoning", reasoningDetails: merged },
1949
+ })
1950
+ expect(response.events.filter(LLMEvent.is.reasoningStart)).toHaveLength(1)
1951
+ expect(response.events.filter(LLMEvent.is.reasoningDelta)).toHaveLength(1)
1952
+ expect(response.events.filter(LLMEvent.is.reasoningEnd)).toHaveLength(1)
1953
+ expect(publishedEndMetadata).toEqual([{ openai: { reasoningField: "reasoning", reasoningDetails: merged } }])
1954
+ expect(response.events.findIndex(LLMEvent.is.reasoningStart)).toBeLessThan(
1955
+ response.events.findIndex(LLMEvent.is.textStart),
1956
+ )
1957
+ // Reasoning stays open alongside text and closes once during finalization.
1958
+ expect(response.events.findIndex(LLMEvent.is.reasoningEnd)).toBeGreaterThan(
1959
+ response.events.findIndex(LLMEvent.is.textStart),
1960
+ )
1961
+
1962
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1963
+ expect(replay.body.messages).toEqual([
1964
+ { role: "assistant", content: "Hello", reasoning: "thinking", reasoning_details: merged },
1965
+ ])
1966
+ }),
1967
+ )
1968
+
1969
+ it.effect("preserves metadata-only reasoning arriving after the finish reason", () =>
1970
+ Effect.gen(function* () {
1971
+ const details = [{ type: "reasoning.encrypted", data: "opaque", format: "openai-responses-v1", index: 0 }]
1972
+ const response = yield* LLMClient.generate(request).pipe(
1973
+ Effect.provide(
1974
+ fixedResponse(
1975
+ sseEvents(
1976
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
1977
+ { choices: [{ delta: { reasoning_details: details } }] },
1978
+ ),
1979
+ ),
1980
+ ),
1981
+ )
1982
+
1983
+ expect(response.message.content).toEqual([
1984
+ { type: "reasoning", text: "", providerMetadata: { openai: { reasoningDetails: details } } },
1985
+ ])
1986
+ expect(response.events.filter(LLMEvent.is.reasoningStart)).toHaveLength(1)
1987
+ expect(response.events.filter(LLMEvent.is.reasoningEnd)).toHaveLength(1)
1988
+
1989
+ const replay = yield* compileRequest(ai.request({ model, messages: [response.message] }))
1990
+ expect(replay.body.messages).toEqual([{ role: "assistant", content: "", reasoning_details: details }])
1991
+ }),
1992
+ )
1993
+
1994
+ it.effect("flushes details-only display reasoning when the stream ends", () =>
1995
+ Effect.gen(function* () {
1996
+ const details = [{ type: "reasoning.summary", summary: "summary", format: "openai-responses-v1", index: 0 }]
1997
+ const response = yield* LLMClient.generate(request).pipe(
1998
+ Effect.provide(
1999
+ fixedResponse(
2000
+ sseEvents(
2001
+ { choices: [{ delta: { reasoning_details: details } }] },
2002
+ { choices: [{ delta: {}, finish_reason: "stop" }] },
2003
+ ),
2004
+ ),
2005
+ ),
2006
+ )
2007
+
2008
+ expect(response.reasoning).toBe("summary")
2009
+ expect(response.message.content).toEqual([
2010
+ { type: "reasoning", text: "summary", providerMetadata: { openai: { reasoningDetails: details } } },
2011
+ ])
2012
+ }),
2013
+ )
2014
+
2015
+ it.effect("replays details from multiple reasoning parts in order", () =>
2016
+ Effect.gen(function* () {
2017
+ const first = { type: "reasoning.text", text: "first", signature: "signed-0", index: 0 }
2018
+ const second = { type: "reasoning.text", text: "second", signature: "signed-1", index: 1 }
2019
+ const replay = yield* compileRequest(
2020
+ ai.request({
2021
+ model,
2022
+ messages: [
2023
+ Message.assistant([
2024
+ {
2025
+ type: "reasoning",
2026
+ text: "first",
2027
+ providerMetadata: { openai: { reasoningDetails: [first] } },
2028
+ },
2029
+ {
2030
+ type: "reasoning",
2031
+ text: "second",
2032
+ providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: [second] } },
2033
+ },
2034
+ ]),
2035
+ ],
2036
+ }),
2037
+ )
2038
+
2039
+ expect(replay.body.messages).toEqual([
2040
+ { role: "assistant", content: "", reasoning: "firstsecond", reasoning_details: [first, second] },
2041
+ ])
2042
+ }),
2043
+ )
2044
+
2045
+ it.effect("retains scalar replay for mixed structured reasoning parts", () =>
2046
+ Effect.gen(function* () {
2047
+ const detail = { type: "reasoning.encrypted", data: "opaque", index: 0 }
2048
+ const replay = yield* compileRequest(
2049
+ ai.request({
2050
+ model,
2051
+ messages: [
2052
+ Message.assistant([
2053
+ {
2054
+ type: "reasoning",
2055
+ text: "A",
2056
+ providerMetadata: { openai: { reasoningDetails: [detail] } },
2057
+ },
2058
+ { type: "reasoning", text: "B" },
2059
+ ]),
2060
+ ],
2061
+ }),
2062
+ )
2063
+
2064
+ expect(replay.body.messages).toEqual([
2065
+ { role: "assistant", content: "", reasoning_content: "AB", reasoning_details: [detail] },
2066
+ ])
2067
+ }),
2068
+ )
2069
+
2070
+ it.effect("assembles streamed tool call input", () =>
2071
+ Effect.gen(function* () {
2072
+ const body = sseEvents(
2073
+ deltaChunk({
2074
+ role: "assistant",
2075
+ tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query"' } }],
2076
+ }),
2077
+ deltaChunk({ tool_calls: [{ index: 0, function: { arguments: ':"weather"}' } }] }),
2078
+ deltaChunk({}, "tool_calls"),
2079
+ )
2080
+ const response = yield* LLMClient.generate({
2081
+ ...request,
2082
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
2083
+ }).pipe(Effect.provide(fixedResponse(body)))
2084
+
2085
+ expect(response.events).toEqual([
2086
+ { type: "step-start", index: 0 },
2087
+ { type: "tool-input-start", id: "call_1", name: "lookup", providerMetadata: undefined },
2088
+ { type: "tool-input-delta", id: "call_1", name: "lookup", text: '{"query"', input: {} },
2089
+ {
2090
+ type: "tool-input-delta",
2091
+ id: "call_1",
2092
+ name: "lookup",
2093
+ text: ':"weather"}',
2094
+ input: { query: "weather" },
2095
+ },
2096
+ { type: "tool-input-end", id: "call_1", name: "lookup", providerMetadata: undefined },
2097
+ {
2098
+ type: "tool-call",
2099
+ id: "call_1",
2100
+ name: "lookup",
2101
+ input: { query: "weather" },
2102
+ providerExecuted: undefined,
2103
+ providerMetadata: undefined,
2104
+ },
2105
+ {
2106
+ type: "step-finish",
2107
+ index: 0,
2108
+ reason: { normalized: "tool-calls", raw: "tool_calls" },
2109
+ usage: undefined,
2110
+ providerMetadata: undefined,
2111
+ },
2112
+ { type: "finish", reason: { normalized: "tool-calls", raw: "tool_calls" }, usage: undefined },
2113
+ ])
2114
+ }),
2115
+ )
2116
+
2117
+ it.effect("preserves Gemini thought signatures on streamed parallel tool calls", () =>
2118
+ Effect.gen(function* () {
2119
+ // Gemini's OpenAI-compatible endpoint omits `index`, streams each call whole,
2120
+ // and signs only the first call of a parallel batch.
2121
+ const body = sseEvents(
2122
+ deltaChunk({
2123
+ role: "assistant",
2124
+ tool_calls: [
2125
+ {
2126
+ extra_content: { google: { thought_signature: "sig_1" } },
2127
+ id: "call_1",
2128
+ type: "function",
2129
+ function: { name: "lookup", arguments: '{"city":"Paris"}' },
2130
+ },
2131
+ ],
2132
+ }),
2133
+ deltaChunk({
2134
+ role: "assistant",
2135
+ tool_calls: [{ id: "call_2", type: "function", function: { name: "lookup", arguments: '{"city":"Tokyo"}' } }],
2136
+ }),
2137
+ deltaChunk({}, "stop"),
2138
+ )
2139
+ const response = yield* LLMClient.generate({
2140
+ ...request,
2141
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
2142
+ }).pipe(Effect.provide(fixedResponse(body)))
2143
+
2144
+ expect(response.events.filter(LLMEvent.is.toolCall)).toEqual([
2145
+ {
2146
+ type: "tool-call",
2147
+ id: "call_1",
2148
+ name: "lookup",
2149
+ input: { city: "Paris" },
2150
+ providerExecuted: undefined,
2151
+ providerMetadata: { openai: { extraContent: { google: { thought_signature: "sig_1" } } } },
2152
+ },
2153
+ {
2154
+ type: "tool-call",
2155
+ id: "call_2",
2156
+ name: "lookup",
2157
+ input: { city: "Tokyo" },
2158
+ providerExecuted: undefined,
2159
+ providerMetadata: undefined,
2160
+ },
2161
+ ])
2162
+ }),
2163
+ )
2164
+
2165
+ it.effect("keeps extra content that arrives before the tool identity", () =>
2166
+ Effect.gen(function* () {
2167
+ const body = sseEvents(
2168
+ deltaChunk({
2169
+ tool_calls: [
2170
+ { index: 0, extra_content: { google: { thought_signature: "sig_1" } }, function: { arguments: "{" } },
2171
+ ],
2172
+ }),
2173
+ deltaChunk({ tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: "}" } }] }),
2174
+ deltaChunk({}, "tool_calls"),
2175
+ )
2176
+ const response = yield* LLMClient.generate({
2177
+ ...request,
2178
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
2179
+ }).pipe(Effect.provide(fixedResponse(body)))
2180
+
2181
+ expect(response.events.filter(LLMEvent.is.toolCall).map((event) => event.providerMetadata)).toEqual([
2182
+ { openai: { extraContent: { google: { thought_signature: "sig_1" } } } },
2183
+ ])
2184
+ }),
2185
+ )
2186
+
2187
+ it.effect("does not finalize streamed tool calls when content is filtered", () =>
2188
+ Effect.gen(function* () {
2189
+ const body = sseEvents(
2190
+ deltaChunk({
2191
+ tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query":"weather"' } }],
2192
+ }),
2193
+ deltaChunk({}, "content_filter"),
2194
+ )
2195
+ const response = yield* LLMClient.generate({
2196
+ ...request,
2197
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
2198
+ }).pipe(Effect.provide(fixedResponse(body)))
2199
+
2200
+ expect(response.events).toEqual([
2201
+ { type: "step-start", index: 0 },
2202
+ {
2203
+ type: "tool-input-start",
2204
+ id: "call_1",
2205
+ name: "lookup",
2206
+ providerExecuted: undefined,
2207
+ providerMetadata: undefined,
2208
+ },
2209
+ {
2210
+ type: "tool-input-delta",
2211
+ id: "call_1",
2212
+ name: "lookup",
2213
+ text: '{"query":"weather"',
2214
+ input: { query: "weather" },
2215
+ },
2216
+ {
2217
+ type: "step-finish",
2218
+ index: 0,
2219
+ reason: { normalized: "content-filter", raw: "content_filter" },
2220
+ usage: undefined,
2221
+ providerMetadata: undefined,
2222
+ },
2223
+ { type: "finish", reason: { normalized: "content-filter", raw: "content_filter" }, usage: undefined },
2224
+ ])
2225
+ expect(response.toolCalls).toEqual([])
2226
+
2227
+ const missingIdentity = yield* LLMClient.generate(request).pipe(
2228
+ Effect.provide(
2229
+ fixedResponse(
2230
+ sseEvents(
2231
+ deltaChunk({ tool_calls: [{ index: 0, id: "call_2", function: { arguments: "{}" } }] }),
2232
+ deltaChunk({}, "content_filter"),
2233
+ ),
2234
+ ),
2235
+ ),
2236
+ )
2237
+ expect(missingIdentity.finishReason).toEqual({ normalized: "content-filter", raw: "content_filter" })
2238
+ expect(missingIdentity.toolCalls).toEqual([])
2239
+ }),
2240
+ )
2241
+
2242
+ it.effect("does not finalize streamed tool calls when output is truncated", () =>
2243
+ Effect.gen(function* () {
2244
+ const body = sseEvents(
2245
+ deltaChunk({
2246
+ tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query":"weather"}' } }],
2247
+ }),
2248
+ deltaChunk({}, "length"),
2249
+ )
2250
+ const response = yield* LLMClient.generate({
2251
+ ...request,
2252
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
2253
+ }).pipe(Effect.provide(fixedResponse(body)))
2254
+
2255
+ expect(response.events).toEqual([
2256
+ { type: "step-start", index: 0 },
2257
+ {
2258
+ type: "tool-input-start",
2259
+ id: "call_1",
2260
+ name: "lookup",
2261
+ providerExecuted: undefined,
2262
+ providerMetadata: undefined,
2263
+ },
2264
+ {
2265
+ type: "tool-input-delta",
2266
+ id: "call_1",
2267
+ name: "lookup",
2268
+ text: '{"query":"weather"}',
2269
+ input: { query: "weather" },
2270
+ },
2271
+ {
2272
+ type: "step-finish",
2273
+ index: 0,
2274
+ reason: { normalized: "length", raw: "length" },
2275
+ usage: undefined,
2276
+ providerMetadata: undefined,
2277
+ },
2278
+ { type: "finish", reason: { normalized: "length", raw: "length" }, usage: undefined },
2279
+ ])
2280
+ expect(response.toolCalls).toEqual([])
2281
+
2282
+ const missingIdentity = yield* LLMClient.generate(request).pipe(
2283
+ Effect.provide(
2284
+ fixedResponse(
2285
+ sseEvents(
2286
+ deltaChunk({ tool_calls: [{ index: 0, id: "call_2", function: { arguments: "{}" } }] }),
2287
+ deltaChunk({}, "length"),
2288
+ ),
2289
+ ),
2290
+ ),
2291
+ )
2292
+ expect(missingIdentity.finishReason).toEqual({ normalized: "length", raw: "length" })
2293
+ expect(missingIdentity.toolCalls).toEqual([])
2294
+ }),
2295
+ )
2296
+
2297
+ it.effect("maps unknown finish reasons to unknown and keeps the raw value", () =>
2298
+ Effect.gen(function* () {
2299
+ const response = yield* LLMClient.generate(request).pipe(
2300
+ Effect.provide(fixedResponse(sseEvents(deltaChunk({ content: "Hello" }), deltaChunk({}, "future_reason")))),
2301
+ )
2302
+
2303
+ expect(response.text).toBe("Hello")
2304
+ expect(response.finishReason).toEqual({ normalized: "unknown", raw: "future_reason" })
2305
+ }),
2306
+ )
2307
+
2308
+ it.effect("ignores empty identity fields on later tool call deltas", () =>
2309
+ Effect.gen(function* () {
2310
+ const body = sseEvents(
2311
+ deltaChunk({
2312
+ tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: "{" } }],
2313
+ }),
2314
+ deltaChunk({
2315
+ tool_calls: [{ index: 0, id: "", function: { name: "", arguments: '\"query\":\"weather\"}' } }],
2316
+ }),
2317
+ deltaChunk({}, "tool_calls"),
2318
+ )
2319
+ const response = yield* LLMClient.generate({
2320
+ ...request,
2321
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
2322
+ }).pipe(Effect.provide(fixedResponse(body)))
2323
+
2324
+ expect(response.toolCalls).toMatchObject([{ id: "call_1", name: "lookup", input: { query: "weather" } }])
2325
+ }),
2326
+ )
2327
+
2328
+ it.effect("buffers tool call deltas until the function name arrives", () =>
2329
+ Effect.gen(function* () {
2330
+ const body = sseEvents(
2331
+ deltaChunk({
2332
+ tool_calls: [{ index: 0, id: "call_1", function: { arguments: "{" } }],
2333
+ }),
2334
+ deltaChunk({
2335
+ tool_calls: [{ index: 0, function: { name: "lookup", arguments: '\"query\":' } }],
2336
+ }),
2337
+ deltaChunk({ tool_calls: [{ index: 0, function: { arguments: '\"weather\"}' } }] }),
2338
+ deltaChunk({}, "tool_calls"),
2339
+ )
2340
+ const response = yield* LLMClient.generate({
2341
+ ...request,
2342
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
2343
+ }).pipe(Effect.provide(fixedResponse(body)))
2344
+
2345
+ expect(response.toolCalls).toMatchObject([{ id: "call_1", name: "lookup", input: { query: "weather" } }])
2346
+ }),
2347
+ )
2348
+
2349
+ it.effect("fails when a buffered tool call never receives a function name", () =>
2350
+ Effect.gen(function* () {
2351
+ const body = sseEvents(
2352
+ deltaChunk({
2353
+ tool_calls: [{ index: 0, id: "call_1", function: { arguments: "{}" } }],
2354
+ }),
2355
+ deltaChunk({}, "tool_calls"),
2356
+ )
2357
+ const error = yield* LLMClient.generate({
2358
+ ...request,
2359
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
2360
+ }).pipe(Effect.provide(fixedResponse(body)), Effect.flip)
2361
+
2362
+ expect(error.message).toContain("OpenAI Chat tool call delta is missing id or name")
2363
+ expect(error.reason._tag).toBe("InvalidProviderOutput")
2364
+ if (error.reason._tag !== "InvalidProviderOutput") return
2365
+ expect(decodeJson(error.reason.body ?? "")).toMatchObject({
2366
+ choices: [{ finish_reason: "tool_calls" }],
2367
+ })
2368
+ }),
2369
+ )
2370
+
2371
+ it.effect("finalizes a streamed tool call when the provider ends without a finish reason", () =>
2372
+ Effect.gen(function* () {
2373
+ const body = sseEvents(
2374
+ deltaChunk({
2375
+ role: "assistant",
2376
+ tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query"' } }],
2377
+ }),
2378
+ deltaChunk({ tool_calls: [{ index: 0, function: { arguments: ':"weather"}' } }] }),
2379
+ )
2380
+ const input = {
2381
+ ...request,
2382
+ model: LanguageModel.update(model, { compatibility: { requireFinishReason: false } }),
2383
+ tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
2384
+ }
2385
+ const response = yield* LLMClient.generate(input).pipe(Effect.provide(fixedResponse(body)))
2386
+
2387
+ expect(response.events).toEqual([
2388
+ { type: "step-start", index: 0 },
2389
+ { type: "tool-input-start", id: "call_1", name: "lookup", providerMetadata: undefined },
2390
+ { type: "tool-input-delta", id: "call_1", name: "lookup", text: '{"query"', input: {} },
2391
+ {
2392
+ type: "tool-input-delta",
2393
+ id: "call_1",
2394
+ name: "lookup",
2395
+ text: ':"weather"}',
2396
+ input: { query: "weather" },
2397
+ },
2398
+ { type: "tool-input-end", id: "call_1", name: "lookup", providerMetadata: undefined },
2399
+ {
2400
+ type: "tool-call",
2401
+ id: "call_1",
2402
+ name: "lookup",
2403
+ input: { query: "weather" },
2404
+ providerExecuted: undefined,
2405
+ providerMetadata: undefined,
2406
+ },
2407
+ {
2408
+ type: "step-finish",
2409
+ index: 0,
2410
+ reason: { normalized: "tool-calls" },
2411
+ usage: undefined,
2412
+ providerMetadata: undefined,
2413
+ },
2414
+ { type: "finish", reason: { normalized: "tool-calls" }, usage: undefined },
2415
+ ])
2416
+ }),
2417
+ )
2418
+
2419
+ it.effect("fails on malformed stream events", () =>
2420
+ Effect.gen(function* () {
2421
+ const body = sseEvents(deltaChunk({ content: 123 }))
2422
+ const error = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)), Effect.flip)
2423
+
2424
+ expect(error.message).toContain("Invalid openai/openai-chat stream event")
2425
+ }),
2426
+ )
2427
+
2428
+ it.effect("surfaces transport errors that occur mid-stream", () =>
2429
+ Effect.gen(function* () {
2430
+ const layer = truncatedStream(
2431
+ [`data: ${JSON.stringify(deltaChunk({ role: "assistant", content: "Hello" }))}\n\n`],
2432
+ systemError("ECONNRESET", "socket closed unexpectedly"),
2433
+ )
2434
+ const events = yield* Ref.make<ReadonlyArray<LLMEvent>>([])
2435
+ const error = yield* LLMClient.stream(request).pipe(
2436
+ Stream.tap((event) => Ref.update(events, (current) => [...current, event])),
2437
+ Stream.runDrain,
2438
+ Effect.provide(layer),
2439
+ Effect.flip,
2440
+ )
2441
+
2442
+ expect((yield* Ref.get(events)).some((event) => event.type === "text-delta")).toBeTrue()
2443
+ expect(error.message).toBe("Connection lost while reading the response: ECONNRESET: socket closed unexpectedly")
2444
+ expect(error.reason).toMatchObject({
2445
+ _tag: "Transport",
2446
+ transport: "http",
2447
+ operation: "read",
2448
+ code: "ECONNRESET",
2449
+ url: "https://api.openai.test/v1/chat/completions",
2450
+ })
2451
+ }),
2452
+ )
2453
+
2454
+ it.effect("surfaces transport errors before the first stream frame", () =>
2455
+ Effect.gen(function* () {
2456
+ const error = yield* LLMClient.generate(request).pipe(
2457
+ Effect.provide(truncatedStream([], systemError("ECONNRESET", "socket closed before output"))),
2458
+ Effect.flip,
2459
+ )
2460
+
2461
+ expect(error.message).toBe("Connection lost while reading the response: ECONNRESET: socket closed before output")
2462
+ expect(error.reason).toMatchObject({
2463
+ _tag: "Transport",
2464
+ transport: "http",
2465
+ operation: "read",
2466
+ code: "ECONNRESET",
2467
+ })
2468
+ }),
2469
+ )
2470
+
2471
+ it.effect("fails HTTP provider errors before stream parsing", () =>
2472
+ Effect.gen(function* () {
2473
+ const error = yield* LLMClient.generate(request).pipe(
2474
+ Effect.provide(
2475
+ fixedResponse('{"error":{"message":"Bad request","type":"invalid_request_error"}}', {
2476
+ status: 400,
2477
+ headers: { "content-type": "application/json" },
2478
+ }),
2479
+ ),
2480
+ Effect.flip,
2481
+ )
2482
+
2483
+ expect(error).toBeInstanceOf(AIError)
2484
+ expect(error).toMatchObject({ reason: { _tag: "InvalidRequest" }, message: "Bad request" })
2485
+ }),
2486
+ )
2487
+
2488
+ it.effect("short-circuits the upstream stream when the consumer takes a prefix", () =>
2489
+ Effect.gen(function* () {
2490
+ // The body has more chunks than we'll consume. If `Stream.take(1)` did
2491
+ // not interrupt the upstream HTTP body the test would hang waiting for
2492
+ // the rest of the stream to drain.
2493
+ const body = sseEvents(
2494
+ deltaChunk({ role: "assistant", content: "Hello" }),
2495
+ deltaChunk({ content: " world" }),
2496
+ deltaChunk({}, "stop"),
2497
+ )
2498
+
2499
+ const events = Array.from(
2500
+ yield* LLMClient.stream(request).pipe(Stream.take(1), Stream.runCollect, Effect.provide(fixedResponse(body))),
2501
+ )
2502
+ expect(events.map((event) => event.type)).toEqual(["step-start"])
2503
+ }),
2504
+ )
2505
+ })