@sayknow-cli/ai 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (361) hide show
  1. package/CHANGELOG.md +2788 -0
  2. package/README.md +1183 -0
  3. package/dist/types/api-registry.d.ts +30 -0
  4. package/dist/types/auth-broker/client.d.ts +66 -0
  5. package/dist/types/auth-broker/index.d.ts +5 -0
  6. package/dist/types/auth-broker/refresher.d.ts +25 -0
  7. package/dist/types/auth-broker/remote-store.d.ts +96 -0
  8. package/dist/types/auth-broker/server.d.ts +32 -0
  9. package/dist/types/auth-broker/types.d.ts +105 -0
  10. package/dist/types/auth-broker/wire-schemas.d.ts +412 -0
  11. package/dist/types/auth-gateway/http.d.ts +39 -0
  12. package/dist/types/auth-gateway/index.d.ts +3 -0
  13. package/dist/types/auth-gateway/server.d.ts +17 -0
  14. package/dist/types/auth-gateway/types.d.ts +115 -0
  15. package/dist/types/auth-storage.d.ts +660 -0
  16. package/dist/types/cli.d.ts +2 -0
  17. package/dist/types/index.d.ts +51 -0
  18. package/dist/types/model-cache.d.ts +17 -0
  19. package/dist/types/model-manager.d.ts +62 -0
  20. package/dist/types/model-thinking.d.ts +74 -0
  21. package/dist/types/models.d.ts +12 -0
  22. package/dist/types/provider-details.d.ts +24 -0
  23. package/dist/types/provider-models/bundled-references.d.ts +4 -0
  24. package/dist/types/provider-models/descriptors.d.ts +48 -0
  25. package/dist/types/provider-models/google.d.ts +20 -0
  26. package/dist/types/provider-models/index.d.ts +5 -0
  27. package/dist/types/provider-models/ollama.d.ts +7 -0
  28. package/dist/types/provider-models/openai-compat.d.ts +244 -0
  29. package/dist/types/provider-models/special.d.ts +16 -0
  30. package/dist/types/providers/amazon-bedrock.d.ts +60 -0
  31. package/dist/types/providers/anthropic-messages-server-schema.d.ts +450 -0
  32. package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
  33. package/dist/types/providers/anthropic.d.ts +198 -0
  34. package/dist/types/providers/aws-credentials.d.ts +43 -0
  35. package/dist/types/providers/aws-eventstream.d.ts +38 -0
  36. package/dist/types/providers/aws-sigv4.d.ts +55 -0
  37. package/dist/types/providers/azure-openai-responses.d.ts +15 -0
  38. package/dist/types/providers/composer-discipline.d.ts +26 -0
  39. package/dist/types/providers/cursor/gen/agent_pb.d.ts +13022 -0
  40. package/dist/types/providers/cursor.d.ts +44 -0
  41. package/dist/types/providers/error-message.d.ts +27 -0
  42. package/dist/types/providers/github-copilot-headers.d.ts +40 -0
  43. package/dist/types/providers/gitlab-duo.d.ts +27 -0
  44. package/dist/types/providers/google-auth.d.ts +24 -0
  45. package/dist/types/providers/google-gemini-cli.d.ts +72 -0
  46. package/dist/types/providers/google-gemini-headers.d.ts +18 -0
  47. package/dist/types/providers/google-shared.d.ts +173 -0
  48. package/dist/types/providers/google-types.d.ts +138 -0
  49. package/dist/types/providers/google-vertex.d.ts +7 -0
  50. package/dist/types/providers/google.d.ts +4 -0
  51. package/dist/types/providers/grammar.d.ts +1 -0
  52. package/dist/types/providers/kimi.d.ts +27 -0
  53. package/dist/types/providers/mock.d.ts +175 -0
  54. package/dist/types/providers/ollama.d.ts +41 -0
  55. package/dist/types/providers/openai-anthropic-shim.d.ts +31 -0
  56. package/dist/types/providers/openai-chat-server-schema.d.ts +815 -0
  57. package/dist/types/providers/openai-chat-server.d.ts +16 -0
  58. package/dist/types/providers/openai-codex/constants.d.ts +26 -0
  59. package/dist/types/providers/openai-codex/request-transformer.d.ts +49 -0
  60. package/dist/types/providers/openai-codex/response-handler.d.ts +17 -0
  61. package/dist/types/providers/openai-codex-responses.d.ts +67 -0
  62. package/dist/types/providers/openai-completions-compat.d.ts +27 -0
  63. package/dist/types/providers/openai-completions.d.ts +33 -0
  64. package/dist/types/providers/openai-request-transform.d.ts +4 -0
  65. package/dist/types/providers/openai-responses-server-schema.d.ts +392 -0
  66. package/dist/types/providers/openai-responses-server.d.ts +17 -0
  67. package/dist/types/providers/openai-responses-shared.d.ts +89 -0
  68. package/dist/types/providers/openai-responses.d.ts +32 -0
  69. package/dist/types/providers/pi-native-client.d.ts +13 -0
  70. package/dist/types/providers/pi-native-server.d.ts +68 -0
  71. package/dist/types/providers/register-builtins.d.ts +31 -0
  72. package/dist/types/providers/synthetic.d.ts +26 -0
  73. package/dist/types/providers/transform-messages.d.ts +14 -0
  74. package/dist/types/providers/vision-guard.d.ts +8 -0
  75. package/dist/types/rate-limit-utils.d.ts +19 -0
  76. package/dist/types/stream.d.ts +43 -0
  77. package/dist/types/types.d.ts +811 -0
  78. package/dist/types/usage/claude.d.ts +3 -0
  79. package/dist/types/usage/gemini.d.ts +2 -0
  80. package/dist/types/usage/github-copilot.d.ts +7 -0
  81. package/dist/types/usage/google-antigravity.d.ts +2 -0
  82. package/dist/types/usage/grok-cli.d.ts +10 -0
  83. package/dist/types/usage/kimi.d.ts +2 -0
  84. package/dist/types/usage/minimax-code.d.ts +2 -0
  85. package/dist/types/usage/openai-codex.d.ts +3 -0
  86. package/dist/types/usage/shared.d.ts +1 -0
  87. package/dist/types/usage/zai.d.ts +2 -0
  88. package/dist/types/usage.d.ts +258 -0
  89. package/dist/types/utils/abort.d.ts +19 -0
  90. package/dist/types/utils/anthropic-auth.d.ts +31 -0
  91. package/dist/types/utils/discovery/antigravity.d.ts +61 -0
  92. package/dist/types/utils/discovery/codex.d.ts +38 -0
  93. package/dist/types/utils/discovery/cursor.d.ts +23 -0
  94. package/dist/types/utils/discovery/gemini.d.ts +25 -0
  95. package/dist/types/utils/discovery/index.d.ts +4 -0
  96. package/dist/types/utils/discovery/openai-compatible.d.ts +74 -0
  97. package/dist/types/utils/event-stream.d.ts +33 -0
  98. package/dist/types/utils/fireworks-model-id.d.ts +10 -0
  99. package/dist/types/utils/foundry.d.ts +1 -0
  100. package/dist/types/utils/h2-fetch.d.ts +22 -0
  101. package/dist/types/utils/http-inspector.d.ts +35 -0
  102. package/dist/types/utils/idle-iterator.d.ts +67 -0
  103. package/dist/types/utils/json-parse.d.ts +10 -0
  104. package/dist/types/utils/oauth/alibaba-coding-plan.d.ts +18 -0
  105. package/dist/types/utils/oauth/anthropic.d.ts +22 -0
  106. package/dist/types/utils/oauth/api-key-login.d.ts +35 -0
  107. package/dist/types/utils/oauth/api-key-validation.d.ts +27 -0
  108. package/dist/types/utils/oauth/callback-server.d.ts +60 -0
  109. package/dist/types/utils/oauth/cerebras.d.ts +1 -0
  110. package/dist/types/utils/oauth/cloudflare-ai-gateway.d.ts +18 -0
  111. package/dist/types/utils/oauth/cursor.d.ts +15 -0
  112. package/dist/types/utils/oauth/deepseek.d.ts +10 -0
  113. package/dist/types/utils/oauth/firepass.d.ts +1 -0
  114. package/dist/types/utils/oauth/fireworks.d.ts +1 -0
  115. package/dist/types/utils/oauth/github-copilot.d.ts +38 -0
  116. package/dist/types/utils/oauth/gitlab-duo.d.ts +3 -0
  117. package/dist/types/utils/oauth/google-antigravity.d.ts +11 -0
  118. package/dist/types/utils/oauth/google-gemini-cli.d.ts +10 -0
  119. package/dist/types/utils/oauth/google-oauth-shared.d.ts +28 -0
  120. package/dist/types/utils/oauth/huggingface.d.ts +19 -0
  121. package/dist/types/utils/oauth/index.d.ts +38 -0
  122. package/dist/types/utils/oauth/kagi.d.ts +17 -0
  123. package/dist/types/utils/oauth/kilo.d.ts +5 -0
  124. package/dist/types/utils/oauth/kimi.d.ts +21 -0
  125. package/dist/types/utils/oauth/litellm.d.ts +18 -0
  126. package/dist/types/utils/oauth/lm-studio.d.ts +17 -0
  127. package/dist/types/utils/oauth/minimax-code.d.ts +28 -0
  128. package/dist/types/utils/oauth/moonshot.d.ts +1 -0
  129. package/dist/types/utils/oauth/nanogpt.d.ts +1 -0
  130. package/dist/types/utils/oauth/nvidia.d.ts +18 -0
  131. package/dist/types/utils/oauth/ollama-cloud.d.ts +2 -0
  132. package/dist/types/utils/oauth/ollama.d.ts +18 -0
  133. package/dist/types/utils/oauth/openai-codex.d.ts +21 -0
  134. package/dist/types/utils/oauth/opencode.d.ts +18 -0
  135. package/dist/types/utils/oauth/parallel.d.ts +17 -0
  136. package/dist/types/utils/oauth/perplexity.d.ts +9 -0
  137. package/dist/types/utils/oauth/pkce.d.ts +8 -0
  138. package/dist/types/utils/oauth/qianfan.d.ts +17 -0
  139. package/dist/types/utils/oauth/qwen-portal.d.ts +19 -0
  140. package/dist/types/utils/oauth/synthetic.d.ts +1 -0
  141. package/dist/types/utils/oauth/tavily.d.ts +17 -0
  142. package/dist/types/utils/oauth/together.d.ts +1 -0
  143. package/dist/types/utils/oauth/types.d.ts +45 -0
  144. package/dist/types/utils/oauth/venice.d.ts +18 -0
  145. package/dist/types/utils/oauth/vercel-ai-gateway.d.ts +18 -0
  146. package/dist/types/utils/oauth/vllm.d.ts +16 -0
  147. package/dist/types/utils/oauth/xai.d.ts +30 -0
  148. package/dist/types/utils/oauth/xiaomi.d.ts +25 -0
  149. package/dist/types/utils/oauth/zai.d.ts +18 -0
  150. package/dist/types/utils/oauth/zenmux.d.ts +1 -0
  151. package/dist/types/utils/overflow.d.ts +54 -0
  152. package/dist/types/utils/parse-bind.d.ts +23 -0
  153. package/dist/types/utils/provider-response.d.ts +3 -0
  154. package/dist/types/utils/retry-after.d.ts +3 -0
  155. package/dist/types/utils/retry-budget.d.ts +1 -0
  156. package/dist/types/utils/retry.d.ts +26 -0
  157. package/dist/types/utils/schema/adapt.d.ts +24 -0
  158. package/dist/types/utils/schema/compatibility.d.ts +30 -0
  159. package/dist/types/utils/schema/dereference.d.ts +11 -0
  160. package/dist/types/utils/schema/draft.d.ts +10 -0
  161. package/dist/types/utils/schema/equality.d.ts +4 -0
  162. package/dist/types/utils/schema/fields.d.ts +49 -0
  163. package/dist/types/utils/schema/index.d.ts +13 -0
  164. package/dist/types/utils/schema/json-schema-validator.d.ts +12 -0
  165. package/dist/types/utils/schema/meta-validator.d.ts +2 -0
  166. package/dist/types/utils/schema/normalize.d.ts +93 -0
  167. package/dist/types/utils/schema/spill.d.ts +8 -0
  168. package/dist/types/utils/schema/stamps.d.ts +25 -0
  169. package/dist/types/utils/schema/types.d.ts +4 -0
  170. package/dist/types/utils/schema/wire.d.ts +54 -0
  171. package/dist/types/utils/schema/zod-decontaminate.d.ts +31 -0
  172. package/dist/types/utils/sse-debug.d.ts +10 -0
  173. package/dist/types/utils/tool-call-healing.d.ts +71 -0
  174. package/dist/types/utils/tool-choice-capability.d.ts +41 -0
  175. package/dist/types/utils/tool-choice.d.ts +50 -0
  176. package/dist/types/utils/validation.d.ts +17 -0
  177. package/dist/types/utils.d.ts +34 -0
  178. package/package.json +146 -0
  179. package/src/api-registry.ts +96 -0
  180. package/src/auth-broker/client.ts +358 -0
  181. package/src/auth-broker/index.ts +5 -0
  182. package/src/auth-broker/refresher.ts +127 -0
  183. package/src/auth-broker/remote-store.ts +623 -0
  184. package/src/auth-broker/server.ts +644 -0
  185. package/src/auth-broker/types.ts +127 -0
  186. package/src/auth-broker/wire-schemas.ts +200 -0
  187. package/src/auth-gateway/http.ts +194 -0
  188. package/src/auth-gateway/index.ts +3 -0
  189. package/src/auth-gateway/server.ts +717 -0
  190. package/src/auth-gateway/types.ts +134 -0
  191. package/src/auth-storage.ts +4179 -0
  192. package/src/cli.ts +263 -0
  193. package/src/index.ts +56 -0
  194. package/src/model-cache.ts +129 -0
  195. package/src/model-manager.ts +486 -0
  196. package/src/model-thinking.ts +772 -0
  197. package/src/models.json +75437 -0
  198. package/src/models.json.d.ts +9 -0
  199. package/src/models.ts +82 -0
  200. package/src/prompts/turn-aborted-guidance.md +4 -0
  201. package/src/provider-details.ts +90 -0
  202. package/src/provider-models/bundled-references.ts +38 -0
  203. package/src/provider-models/descriptors.ts +327 -0
  204. package/src/provider-models/google.ts +91 -0
  205. package/src/provider-models/index.ts +5 -0
  206. package/src/provider-models/ollama.ts +153 -0
  207. package/src/provider-models/openai-compat.ts +2352 -0
  208. package/src/provider-models/special.ts +67 -0
  209. package/src/providers/amazon-bedrock.ts +937 -0
  210. package/src/providers/anthropic-messages-server-schema.ts +229 -0
  211. package/src/providers/anthropic-messages-server.ts +677 -0
  212. package/src/providers/anthropic.ts +2940 -0
  213. package/src/providers/aws-credentials.ts +501 -0
  214. package/src/providers/aws-eventstream.ts +185 -0
  215. package/src/providers/aws-sigv4.ts +218 -0
  216. package/src/providers/azure-openai-responses.ts +379 -0
  217. package/src/providers/composer-discipline.ts +41 -0
  218. package/src/providers/cursor/gen/agent_pb.ts +15274 -0
  219. package/src/providers/cursor/proto/agent.proto +3526 -0
  220. package/src/providers/cursor/proto/buf.gen.yaml +6 -0
  221. package/src/providers/cursor/proto/buf.yaml +17 -0
  222. package/src/providers/cursor.ts +2671 -0
  223. package/src/providers/error-message.ts +21 -0
  224. package/src/providers/github-copilot-headers.ts +140 -0
  225. package/src/providers/gitlab-duo.ts +372 -0
  226. package/src/providers/google-auth.ts +252 -0
  227. package/src/providers/google-gemini-cli.ts +856 -0
  228. package/src/providers/google-gemini-headers.ts +41 -0
  229. package/src/providers/google-shared.ts +951 -0
  230. package/src/providers/google-types.ts +167 -0
  231. package/src/providers/google-vertex.ts +88 -0
  232. package/src/providers/google.ts +41 -0
  233. package/src/providers/grammar.ts +70 -0
  234. package/src/providers/kimi.ts +52 -0
  235. package/src/providers/mock.ts +500 -0
  236. package/src/providers/ollama.ts +603 -0
  237. package/src/providers/openai-anthropic-shim.ts +138 -0
  238. package/src/providers/openai-chat-server-schema.ts +243 -0
  239. package/src/providers/openai-chat-server.ts +635 -0
  240. package/src/providers/openai-codex/constants.ts +43 -0
  241. package/src/providers/openai-codex/request-transformer.ts +161 -0
  242. package/src/providers/openai-codex/response-handler.ts +81 -0
  243. package/src/providers/openai-codex-responses.ts +2774 -0
  244. package/src/providers/openai-completions-compat.ts +289 -0
  245. package/src/providers/openai-completions.ts +1935 -0
  246. package/src/providers/openai-request-transform.ts +136 -0
  247. package/src/providers/openai-responses-server-schema.ts +290 -0
  248. package/src/providers/openai-responses-server.ts +1190 -0
  249. package/src/providers/openai-responses-shared.ts +800 -0
  250. package/src/providers/openai-responses.ts +738 -0
  251. package/src/providers/pi-native-client.ts +227 -0
  252. package/src/providers/pi-native-server.ts +210 -0
  253. package/src/providers/register-builtins.ts +411 -0
  254. package/src/providers/synthetic.ts +50 -0
  255. package/src/providers/transform-messages.ts +319 -0
  256. package/src/providers/vision-guard.ts +31 -0
  257. package/src/rate-limit-utils.ts +93 -0
  258. package/src/stream.ts +960 -0
  259. package/src/types.ts +967 -0
  260. package/src/usage/claude.ts +431 -0
  261. package/src/usage/gemini.ts +250 -0
  262. package/src/usage/github-copilot.ts +421 -0
  263. package/src/usage/google-antigravity.ts +201 -0
  264. package/src/usage/grok-cli.ts +163 -0
  265. package/src/usage/kimi.ts +271 -0
  266. package/src/usage/minimax-code.ts +31 -0
  267. package/src/usage/openai-codex.ts +503 -0
  268. package/src/usage/shared.ts +10 -0
  269. package/src/usage/zai.ts +247 -0
  270. package/src/usage.ts +183 -0
  271. package/src/utils/abort.ts +51 -0
  272. package/src/utils/anthropic-auth.ts +87 -0
  273. package/src/utils/discovery/antigravity.ts +261 -0
  274. package/src/utils/discovery/codex.ts +371 -0
  275. package/src/utils/discovery/cursor.ts +306 -0
  276. package/src/utils/discovery/gemini.ts +248 -0
  277. package/src/utils/discovery/index.ts +4 -0
  278. package/src/utils/discovery/openai-compatible.ts +230 -0
  279. package/src/utils/event-stream.ts +172 -0
  280. package/src/utils/fireworks-model-id.ts +30 -0
  281. package/src/utils/foundry.ts +8 -0
  282. package/src/utils/h2-fetch.ts +60 -0
  283. package/src/utils/http-inspector.ts +255 -0
  284. package/src/utils/idle-iterator.ts +257 -0
  285. package/src/utils/json-parse.ts +148 -0
  286. package/src/utils/oauth/alibaba-coding-plan.ts +59 -0
  287. package/src/utils/oauth/anthropic.ts +200 -0
  288. package/src/utils/oauth/api-key-login.ts +87 -0
  289. package/src/utils/oauth/api-key-validation.ts +92 -0
  290. package/src/utils/oauth/callback-server.ts +281 -0
  291. package/src/utils/oauth/cerebras.ts +16 -0
  292. package/src/utils/oauth/cloudflare-ai-gateway.ts +48 -0
  293. package/src/utils/oauth/cursor.ts +157 -0
  294. package/src/utils/oauth/deepseek.ts +53 -0
  295. package/src/utils/oauth/firepass.ts +24 -0
  296. package/src/utils/oauth/fireworks.ts +15 -0
  297. package/src/utils/oauth/github-copilot.ts +362 -0
  298. package/src/utils/oauth/gitlab-duo.ts +123 -0
  299. package/src/utils/oauth/google-antigravity.ts +200 -0
  300. package/src/utils/oauth/google-gemini-cli.ts +256 -0
  301. package/src/utils/oauth/google-oauth-shared.ts +110 -0
  302. package/src/utils/oauth/huggingface.ts +62 -0
  303. package/src/utils/oauth/index.ts +469 -0
  304. package/src/utils/oauth/kagi.ts +47 -0
  305. package/src/utils/oauth/kilo.ts +87 -0
  306. package/src/utils/oauth/kimi.ts +254 -0
  307. package/src/utils/oauth/litellm.ts +47 -0
  308. package/src/utils/oauth/lm-studio.ts +38 -0
  309. package/src/utils/oauth/minimax-code.ts +78 -0
  310. package/src/utils/oauth/moonshot.ts +16 -0
  311. package/src/utils/oauth/nanogpt.ts +15 -0
  312. package/src/utils/oauth/nvidia.ts +70 -0
  313. package/src/utils/oauth/oauth.html +199 -0
  314. package/src/utils/oauth/ollama-cloud.ts +28 -0
  315. package/src/utils/oauth/ollama.ts +47 -0
  316. package/src/utils/oauth/openai-codex.ts +299 -0
  317. package/src/utils/oauth/opencode.ts +49 -0
  318. package/src/utils/oauth/parallel.ts +46 -0
  319. package/src/utils/oauth/perplexity.ts +206 -0
  320. package/src/utils/oauth/pkce.ts +18 -0
  321. package/src/utils/oauth/qianfan.ts +58 -0
  322. package/src/utils/oauth/qwen-portal.ts +60 -0
  323. package/src/utils/oauth/synthetic.ts +16 -0
  324. package/src/utils/oauth/tavily.ts +46 -0
  325. package/src/utils/oauth/together.ts +16 -0
  326. package/src/utils/oauth/types.ts +99 -0
  327. package/src/utils/oauth/venice.ts +59 -0
  328. package/src/utils/oauth/vercel-ai-gateway.ts +47 -0
  329. package/src/utils/oauth/vllm.ts +40 -0
  330. package/src/utils/oauth/xai.ts +246 -0
  331. package/src/utils/oauth/xiaomi.ts +199 -0
  332. package/src/utils/oauth/zai.ts +60 -0
  333. package/src/utils/oauth/zenmux.ts +15 -0
  334. package/src/utils/overflow.ts +137 -0
  335. package/src/utils/parse-bind.ts +54 -0
  336. package/src/utils/provider-response.ts +30 -0
  337. package/src/utils/retry-after.ts +110 -0
  338. package/src/utils/retry-budget.ts +4 -0
  339. package/src/utils/retry.ts +54 -0
  340. package/src/utils/schema/CONSTRAINTS.md +164 -0
  341. package/src/utils/schema/adapt.ts +36 -0
  342. package/src/utils/schema/compatibility.ts +435 -0
  343. package/src/utils/schema/dereference.ts +98 -0
  344. package/src/utils/schema/draft.ts +341 -0
  345. package/src/utils/schema/equality.ts +97 -0
  346. package/src/utils/schema/fields.ts +190 -0
  347. package/src/utils/schema/index.ts +13 -0
  348. package/src/utils/schema/json-schema-validator.ts +577 -0
  349. package/src/utils/schema/meta-validator.ts +167 -0
  350. package/src/utils/schema/normalize.ts +1588 -0
  351. package/src/utils/schema/spill.ts +43 -0
  352. package/src/utils/schema/stamps.ts +97 -0
  353. package/src/utils/schema/types.ts +11 -0
  354. package/src/utils/schema/wire.ts +213 -0
  355. package/src/utils/schema/zod-decontaminate.ts +331 -0
  356. package/src/utils/sse-debug.ts +289 -0
  357. package/src/utils/tool-call-healing.ts +271 -0
  358. package/src/utils/tool-choice-capability.ts +220 -0
  359. package/src/utils/tool-choice.ts +99 -0
  360. package/src/utils/validation.ts +1019 -0
  361. package/src/utils.ts +178 -0
@@ -0,0 +1,218 @@
1
+ /**
2
+ * AWS Signature V4 signing for HTTP requests. WebCrypto-only — no node:crypto.
3
+ *
4
+ * Matches `@smithy/signature-v4` for our usage: header-based signing with a
5
+ * full SHA-256 payload hash (Bedrock requires `applyChecksum: true`).
6
+ *
7
+ * Returns the set of headers to attach to the request:
8
+ * - `host`
9
+ * - `x-amz-date`
10
+ * - `x-amz-content-sha256`
11
+ * - `x-amz-security-token` (only when credentials carry a sessionToken)
12
+ * - `authorization`
13
+ */
14
+
15
+ export interface AwsCredentials {
16
+ accessKeyId: string;
17
+ secretAccessKey: string;
18
+ sessionToken?: string;
19
+ }
20
+
21
+ export interface SignParams {
22
+ method: string;
23
+ /** Hostname only — used to build the `host` header and the canonical request. */
24
+ host: string;
25
+ /** URI path component, e.g. `/model/anthropic.Anthropic model/converse-stream`. */
26
+ path: string;
27
+ /** Optional pre-built query string (without leading `?`). */
28
+ query?: string;
29
+ /** Extra headers to sign in addition to `host`/`x-amz-*`. Names are case-insensitive. */
30
+ headers?: Record<string, string>;
31
+ body: Uint8Array;
32
+ region: string;
33
+ service: string;
34
+ credentials: AwsCredentials;
35
+ /** Override clock for deterministic tests. */
36
+ date?: Date;
37
+ }
38
+
39
+ const ALGORITHM = "AWS4-HMAC-SHA256";
40
+ const KEY_TYPE = "aws4_request";
41
+ // Headers the SDK never includes in the signature. Lowercased.
42
+ const UNSIGNABLE: Record<string, true> = {
43
+ authorization: true,
44
+ "cache-control": true,
45
+ connection: true,
46
+ expect: true,
47
+ from: true,
48
+ "keep-alive": true,
49
+ "max-forwards": true,
50
+ pragma: true,
51
+ referer: true,
52
+ te: true,
53
+ trailer: true,
54
+ "transfer-encoding": true,
55
+ upgrade: true,
56
+ "user-agent": true,
57
+ "x-amzn-trace-id": true,
58
+ };
59
+
60
+ /** Coerce a possibly-ArrayBufferLike-backed `Uint8Array` into one over a fresh
61
+ * `ArrayBuffer`, which is what `crypto.subtle.{digest,sign,importKey}` requires
62
+ * under the strict TS DOM typings. No-op when already strict.
63
+ */
64
+ function asStrict(bytes: Uint8Array): Uint8Array<ArrayBuffer> {
65
+ if (bytes.buffer instanceof ArrayBuffer && bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength) {
66
+ return bytes as Uint8Array<ArrayBuffer>;
67
+ }
68
+ const copy = new Uint8Array(bytes.byteLength);
69
+ copy.set(bytes);
70
+ return copy;
71
+ }
72
+ const subtle = globalThis.crypto.subtle;
73
+
74
+ const HEX = "0123456789abcdef";
75
+ export function toHex(bytes: Uint8Array): string {
76
+ let out = "";
77
+ for (let i = 0; i < bytes.length; i++) {
78
+ const b = bytes[i];
79
+ out += HEX[b >> 4] + HEX[b & 15];
80
+ }
81
+ return out;
82
+ }
83
+
84
+ export async function sha256(data: Uint8Array | string): Promise<Uint8Array> {
85
+ const bytes = typeof data === "string" ? new TextEncoder().encode(data) : asStrict(data);
86
+ const digest = await subtle.digest("SHA-256", bytes);
87
+ return new Uint8Array(digest);
88
+ }
89
+
90
+ export async function sha256Hex(data: Uint8Array | string): Promise<string> {
91
+ return toHex(await sha256(data));
92
+ }
93
+
94
+ async function hmac(key: Uint8Array, data: string | Uint8Array): Promise<Uint8Array> {
95
+ const cryptoKey = await subtle.importKey("raw", asStrict(key), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
96
+ const bytes = typeof data === "string" ? new TextEncoder().encode(data) : asStrict(data);
97
+ const sig = await subtle.sign("HMAC", cryptoKey, bytes);
98
+ return new Uint8Array(sig);
99
+ }
100
+
101
+ /**
102
+ * Derive a signing key: HMAC chain `kSecret → kDate → kRegion → kService → kSigning`.
103
+ */
104
+ export async function getSigningKey(
105
+ secretAccessKey: string,
106
+ shortDate: string,
107
+ region: string,
108
+ service: string,
109
+ ): Promise<Uint8Array> {
110
+ const kDate = await hmac(new TextEncoder().encode(`AWS4${secretAccessKey}`), shortDate);
111
+ const kRegion = await hmac(kDate, region);
112
+ const kService = await hmac(kRegion, service);
113
+ return hmac(kService, KEY_TYPE);
114
+ }
115
+
116
+ /** `YYYYMMDDTHHMMSSZ` + 8-char `YYYYMMDD`. */
117
+ export function formatAmzDate(d: Date): { longDate: string; shortDate: string } {
118
+ const iso = d.toISOString();
119
+ // `2025-05-17T12:34:56.789Z` -> `20250517T123456Z`
120
+ const longDate = `${iso.slice(0, 4)}${iso.slice(5, 7)}${iso.slice(8, 10)}T${iso.slice(11, 13)}${iso.slice(14, 16)}${iso.slice(17, 19)}Z`;
121
+ return { longDate, shortDate: longDate.slice(0, 8) };
122
+ }
123
+
124
+ /**
125
+ * Canonicalize a request path per RFC 3986: each segment is %-encoded but `/`
126
+ * stays literal. Matches the smithy default (`uriEscapePath: true`, then revert
127
+ * the double-encoding of `/`). Bedrock paths use no reserved characters in
128
+ * practice, but model IDs can include `:` and `.`.
129
+ */
130
+ function canonicalPath(path: string): string {
131
+ const segments = path.split("/");
132
+ const escaped = segments.map(seg => (seg.length === 0 ? "" : encodeRfc3986(seg)));
133
+ return escaped.join("/");
134
+ }
135
+
136
+ function encodeRfc3986(str: string): string {
137
+ return encodeURIComponent(str).replace(/[!'()*]/g, c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
138
+ }
139
+
140
+ function canonicalQuery(query: string | undefined): string {
141
+ if (!query) return "";
142
+ const pairs: Array<[string, string]> = [];
143
+ for (const part of query.split("&")) {
144
+ if (!part) continue;
145
+ const eq = part.indexOf("=");
146
+ const k = eq === -1 ? part : part.slice(0, eq);
147
+ const v = eq === -1 ? "" : part.slice(eq + 1);
148
+ pairs.push([decodeURIComponent(k), decodeURIComponent(v)]);
149
+ }
150
+ pairs.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0));
151
+ return pairs.map(([k, v]) => `${encodeRfc3986(k)}=${encodeRfc3986(v)}`).join("&");
152
+ }
153
+
154
+ export interface SignedHeaders {
155
+ host: string;
156
+ "x-amz-date": string;
157
+ "x-amz-content-sha256": string;
158
+ authorization: string;
159
+ "x-amz-security-token"?: string;
160
+ }
161
+
162
+ export async function signRequest(params: SignParams): Promise<SignedHeaders> {
163
+ const { method, host, path, query, body, region, service, credentials } = params;
164
+ const date = params.date ?? new Date();
165
+ const { longDate, shortDate } = formatAmzDate(date);
166
+ const payloadHash = await sha256Hex(body);
167
+
168
+ // Assemble the headers that will be signed. Always include host, x-amz-date,
169
+ // x-amz-content-sha256, plus x-amz-security-token when present, plus
170
+ // caller-provided signable headers (e.g. content-type, accept).
171
+ const signed: Record<string, string> = {
172
+ host,
173
+ "x-amz-date": longDate,
174
+ "x-amz-content-sha256": payloadHash,
175
+ };
176
+ if (credentials.sessionToken) signed["x-amz-security-token"] = credentials.sessionToken;
177
+ const extraHeaders = params.headers;
178
+ if (extraHeaders) {
179
+ for (const k in extraHeaders) {
180
+ const lk = k.toLowerCase();
181
+ if (UNSIGNABLE[lk]) continue;
182
+ if (lk.startsWith("proxy-") || lk.startsWith("sec-")) continue;
183
+ signed[lk] = extraHeaders[k].trim().replace(/\s+/g, " ");
184
+ }
185
+ }
186
+
187
+ const sortedNames = Object.keys(signed).sort();
188
+ const canonicalHeaders = `${sortedNames.map(n => `${n}:${signed[n]}`).join("\n")}\n`;
189
+ const signedHeadersStr = sortedNames.join(";");
190
+
191
+ const canonicalRequest = [
192
+ method.toUpperCase(),
193
+ canonicalPath(path),
194
+ canonicalQuery(query),
195
+ canonicalHeaders,
196
+ signedHeadersStr,
197
+ payloadHash,
198
+ ].join("\n");
199
+
200
+ const scope = `${shortDate}/${region}/${service}/${KEY_TYPE}`;
201
+ const stringToSign = [ALGORITHM, longDate, scope, await sha256Hex(canonicalRequest)].join("\n");
202
+
203
+ const signingKey = await getSigningKey(credentials.secretAccessKey, shortDate, region, service);
204
+ const signature = toHex(await hmac(signingKey, stringToSign));
205
+
206
+ const authorization =
207
+ `${ALGORITHM} Credential=${credentials.accessKeyId}/${scope}, ` +
208
+ `SignedHeaders=${signedHeadersStr}, Signature=${signature}`;
209
+
210
+ const out: SignedHeaders = {
211
+ host,
212
+ "x-amz-date": longDate,
213
+ "x-amz-content-sha256": payloadHash,
214
+ authorization,
215
+ };
216
+ if (credentials.sessionToken) out["x-amz-security-token"] = credentials.sessionToken;
217
+ return out;
218
+ }
@@ -0,0 +1,379 @@
1
+ import { $env, extractHttpStatusFromError, logger } from "@sayknow-cli/utils";
2
+ import { AzureOpenAI } from "openai";
3
+ import type {
4
+ Tool as OpenAITool,
5
+ ResponseCreateParamsStreaming,
6
+ ResponseInput,
7
+ } from "openai/resources/responses/responses";
8
+ import { getEnvApiKey } from "../stream";
9
+ import type {
10
+ AssistantMessage,
11
+ Context,
12
+ Model,
13
+ ServiceTier,
14
+ StreamFunction,
15
+ StreamOptions,
16
+ Tool,
17
+ ToolChoice,
18
+ } from "../types";
19
+ import { normalizeSystemPrompts } from "../utils";
20
+ import { createAbortSourceTracker } from "../utils/abort";
21
+ import { AssistantMessageEventStream } from "../utils/event-stream";
22
+ import { finalizeErrorMessage, type RawHttpRequestDump } from "../utils/http-inspector";
23
+ import {
24
+ createWatchdog,
25
+ getOpenAIStreamIdleTimeoutMs,
26
+ getStreamFirstEventTimeoutMs,
27
+ iterateWithIdleTimeout,
28
+ } from "../utils/idle-iterator";
29
+ import { resolveRetryBudget } from "../utils/retry-budget";
30
+ import { sanitizeSchemaForOpenAIResponses, toolWireSchema } from "../utils/schema";
31
+ import { wrapFetchForSseDebug } from "../utils/sse-debug";
32
+ import { mapToOpenAIResponsesToolChoice } from "../utils/tool-choice";
33
+ import {
34
+ isForcedToolChoiceUnsupportedError,
35
+ markToolChoiceIncapability,
36
+ resolveToolChoice,
37
+ } from "../utils/tool-choice-capability";
38
+ import { normalizeOpenAIResponsesPromptCacheKey, supportsDeveloperRole } from "./openai-responses";
39
+ import {
40
+ appendResponsesToolResultMessages,
41
+ applyCommonResponsesSamplingParams,
42
+ applyResponsesReasoningParams,
43
+ convertResponsesAssistantMessage,
44
+ convertResponsesInputContent,
45
+ createInitialResponsesAssistantMessage,
46
+ normalizeResponsesToolCallIdForTransform,
47
+ processResponsesStream,
48
+ } from "./openai-responses-shared";
49
+ import { transformMessages } from "./transform-messages";
50
+
51
+ const DEFAULT_AZURE_API_VERSION = "v1";
52
+ const AZURE_OPENAI_RESPONSES_FIRST_EVENT_TIMEOUT_MESSAGE =
53
+ "Azure OpenAI responses stream timed out while waiting for the first event";
54
+
55
+ function parseDeploymentNameMap(value: string | undefined): Map<string, string> {
56
+ const map = new Map<string, string>();
57
+ if (!value) return map;
58
+ for (const entry of value.split(",")) {
59
+ const trimmed = entry.trim();
60
+ if (!trimmed) continue;
61
+ const [modelId, deploymentName] = trimmed.split("=", 2);
62
+ if (!modelId || !deploymentName) continue;
63
+ map.set(modelId.trim(), deploymentName.trim());
64
+ }
65
+ return map;
66
+ }
67
+
68
+ function resolveDeploymentName(model: Model<"azure-openai-responses">, options?: AzureOpenAIResponsesOptions): string {
69
+ if (options?.azureDeploymentName) {
70
+ return options.azureDeploymentName;
71
+ }
72
+ const mappedDeployment = parseDeploymentNameMap($env.AZURE_OPENAI_DEPLOYMENT_NAME_MAP).get(model.id);
73
+ return mappedDeployment ?? model.id;
74
+ }
75
+
76
+ // Azure OpenAI Responses-specific options
77
+ export interface AzureOpenAIResponsesOptions extends StreamOptions {
78
+ reasoning?: "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
79
+ reasoningSummary?: "auto" | "detailed" | "concise" | null;
80
+ azureApiVersion?: string;
81
+ azureResourceName?: string;
82
+ azureBaseUrl?: string;
83
+ azureDeploymentName?: string;
84
+ toolChoice?: ToolChoice;
85
+ serviceTier?: ServiceTier;
86
+ }
87
+
88
+ type AzureOpenAIResponsesSamplingParams = ResponseCreateParamsStreaming & {
89
+ top_p?: number;
90
+ top_k?: number;
91
+ min_p?: number;
92
+ presence_penalty?: number;
93
+ repetition_penalty?: number;
94
+ };
95
+
96
+ /**
97
+ * Generate function for Azure OpenAI Responses API
98
+ */
99
+ export const streamAzureOpenAIResponses: StreamFunction<"azure-openai-responses"> = (
100
+ model: Model<"azure-openai-responses">,
101
+ context: Context,
102
+ options?: AzureOpenAIResponsesOptions,
103
+ ): AssistantMessageEventStream => {
104
+ const stream = new AssistantMessageEventStream();
105
+
106
+ // Start async processing
107
+ (async () => {
108
+ const startTime = Date.now();
109
+ let firstTokenTime: number | undefined;
110
+ const deploymentName = resolveDeploymentName(model, options);
111
+
112
+ const output: AssistantMessage = createInitialResponsesAssistantMessage(
113
+ "azure-openai-responses",
114
+ model.provider,
115
+ model.id,
116
+ );
117
+ let rawRequestDump: RawHttpRequestDump | undefined;
118
+ const abortTracker = createAbortSourceTracker(options?.signal);
119
+ const firstEventTimeoutAbortError = new Error(AZURE_OPENAI_RESPONSES_FIRST_EVENT_TIMEOUT_MESSAGE);
120
+ const { requestAbortController, requestSignal } = abortTracker;
121
+
122
+ try {
123
+ // Create Azure OpenAI client
124
+ const apiKey = options?.apiKey || getEnvApiKey(model.provider) || "";
125
+ const client = createClient(model, apiKey, options);
126
+ const { baseUrl } = resolveAzureConfig(model, options);
127
+ const params = buildParams(model, context, options, deploymentName, baseUrl);
128
+ const idleTimeoutMs = getOpenAIStreamIdleTimeoutMs();
129
+ options?.onPayload?.(params);
130
+ rawRequestDump = {
131
+ provider: model.provider,
132
+ api: output.api,
133
+ model: model.id,
134
+ method: "POST",
135
+ url: `${baseUrl}/responses`,
136
+ body: params,
137
+ };
138
+ let openaiStream: Awaited<ReturnType<typeof client.responses.create>>;
139
+ try {
140
+ openaiStream = await client.responses.create(params, { signal: requestSignal });
141
+ } catch (error) {
142
+ if (!isForcedToolChoiceUnsupportedError(error, isForcedAzureResponsesToolChoice(params.tool_choice))) {
143
+ throw error;
144
+ }
145
+ const reason = await finalizeErrorMessage(error, rawRequestDump);
146
+ markToolChoiceIncapability(model, "auto", reason);
147
+ const resolvedToolChoice = resolveToolChoice(model, options?.toolChoice);
148
+ stream.push({
149
+ type: "toolChoiceIncapability",
150
+ api: model.api,
151
+ provider: model.provider,
152
+ model: model.id,
153
+ requestedLevel: resolvedToolChoice.requestedLevel,
154
+ resolvedLevel: "auto",
155
+ reason,
156
+ registryKey: resolvedToolChoice.registryKey,
157
+ });
158
+ delete params.tool_choice;
159
+ rawRequestDump = { ...rawRequestDump, body: params };
160
+ openaiStream = await client.responses.create(params, { signal: requestSignal });
161
+ }
162
+ const firstEventWatchdog = createWatchdog(
163
+ options?.streamFirstEventTimeoutMs ?? getStreamFirstEventTimeoutMs(idleTimeoutMs),
164
+ () => abortTracker.abortLocally(firstEventTimeoutAbortError),
165
+ );
166
+ stream.push({ type: "start", partial: output });
167
+
168
+ await processResponsesStream(
169
+ iterateWithIdleTimeout(openaiStream, {
170
+ watchdog: firstEventWatchdog,
171
+ idleTimeoutMs,
172
+ errorMessage: "Azure OpenAI responses stream stalled while waiting for the next event",
173
+ onIdle: () => requestAbortController.abort(),
174
+ }),
175
+ output,
176
+ stream,
177
+ model,
178
+ {
179
+ onFirstToken: () => {
180
+ if (!firstTokenTime) firstTokenTime = Date.now();
181
+ },
182
+ },
183
+ );
184
+
185
+ const firstEventTimeoutError = abortTracker.getLocalAbortReason();
186
+ if (firstEventTimeoutError) {
187
+ throw firstEventTimeoutError;
188
+ }
189
+ if (abortTracker.wasCallerAbort()) {
190
+ throw new Error("Request was aborted");
191
+ }
192
+
193
+ if (output.stopReason === "aborted" || output.stopReason === "error") {
194
+ throw new Error(output.errorMessage ?? "An unknown error occurred");
195
+ }
196
+
197
+ output.duration = Date.now() - startTime;
198
+ if (firstTokenTime) output.ttft = firstTokenTime - startTime;
199
+ stream.push({ type: "done", reason: output.stopReason, message: output });
200
+ stream.end();
201
+ } catch (error) {
202
+ for (const block of output.content) delete (block as { index?: number }).index;
203
+ const firstEventTimeoutError = abortTracker.getLocalAbortReason();
204
+ output.stopReason = abortTracker.wasCallerAbort() ? "aborted" : "error";
205
+ output.errorStatus = extractHttpStatusFromError(error);
206
+ output.errorMessage = firstEventTimeoutError?.message ?? (await finalizeErrorMessage(error, rawRequestDump));
207
+ output.duration = Date.now() - startTime;
208
+ if (firstTokenTime) output.ttft = firstTokenTime - startTime;
209
+ stream.push({ type: "error", reason: output.stopReason, error: output });
210
+ stream.end();
211
+ }
212
+ })();
213
+
214
+ return stream;
215
+ };
216
+
217
+ function normalizeAzureBaseUrl(baseUrl: string): string {
218
+ return baseUrl.replace(/\/+$/, "");
219
+ }
220
+
221
+ function buildDefaultBaseUrl(resourceName: string): string {
222
+ return `https://${resourceName}.openai.azure.com/openai/v1`;
223
+ }
224
+
225
+ function resolveAzureConfig(
226
+ model: Model<"azure-openai-responses">,
227
+ options?: AzureOpenAIResponsesOptions,
228
+ ): { baseUrl: string; apiVersion: string } {
229
+ const apiVersion = options?.azureApiVersion || $env.AZURE_OPENAI_API_VERSION || DEFAULT_AZURE_API_VERSION;
230
+
231
+ const baseUrl = options?.azureBaseUrl?.trim() || $env.AZURE_OPENAI_BASE_URL?.trim() || undefined;
232
+ const resourceName = options?.azureResourceName || $env.AZURE_OPENAI_RESOURCE_NAME;
233
+
234
+ let resolvedBaseUrl = baseUrl;
235
+
236
+ if (!resolvedBaseUrl && resourceName) {
237
+ resolvedBaseUrl = buildDefaultBaseUrl(resourceName);
238
+ }
239
+
240
+ if (!resolvedBaseUrl && model.baseUrl) {
241
+ resolvedBaseUrl = model.baseUrl;
242
+ }
243
+
244
+ if (!resolvedBaseUrl) {
245
+ throw new Error(
246
+ "Azure OpenAI base URL is required. Set AZURE_OPENAI_BASE_URL or AZURE_OPENAI_RESOURCE_NAME, or pass azureBaseUrl, azureResourceName, or model.baseUrl.",
247
+ );
248
+ }
249
+
250
+ return {
251
+ baseUrl: normalizeAzureBaseUrl(resolvedBaseUrl),
252
+ apiVersion,
253
+ };
254
+ }
255
+
256
+ function createClient(model: Model<"azure-openai-responses">, apiKey: string, options?: AzureOpenAIResponsesOptions) {
257
+ if (!apiKey) {
258
+ const envKey = $env.AZURE_OPENAI_API_KEY;
259
+ if (!envKey) {
260
+ throw new Error(
261
+ "Azure OpenAI API key is required. Set AZURE_OPENAI_API_KEY environment variable or pass it as an argument.",
262
+ );
263
+ }
264
+ apiKey = envKey;
265
+ }
266
+
267
+ const headers = { ...(model.headers ?? {}) };
268
+
269
+ if (options?.headers) {
270
+ Object.assign(headers, options.headers);
271
+ }
272
+
273
+ const { baseUrl, apiVersion } = resolveAzureConfig(model, options);
274
+
275
+ const baseFetch = options?.fetch ?? fetch;
276
+ const onSseEvent = options?.onSseEvent;
277
+ return new AzureOpenAI({
278
+ apiKey,
279
+ apiVersion,
280
+ dangerouslyAllowBrowser: true,
281
+ maxRetries: resolveRetryBudget(options?.requestMaxRetries, 5),
282
+ defaultHeaders: headers,
283
+ baseURL: baseUrl,
284
+ fetch: onSseEvent ? wrapFetchForSseDebug(baseFetch, event => onSseEvent(event, model)) : baseFetch,
285
+ });
286
+ }
287
+
288
+ function buildParams(
289
+ model: Model<"azure-openai-responses">,
290
+ context: Context,
291
+ options: AzureOpenAIResponsesOptions | undefined,
292
+ deploymentName: string,
293
+ resolvedBaseUrl?: string,
294
+ ) {
295
+ const messages = convertMessages(model, context, true, resolvedBaseUrl);
296
+
297
+ const params: AzureOpenAIResponsesSamplingParams = {
298
+ model: deploymentName,
299
+ input: messages,
300
+ stream: true,
301
+ prompt_cache_key: normalizeOpenAIResponsesPromptCacheKey(options?.sessionId),
302
+ };
303
+
304
+ applyCommonResponsesSamplingParams(params, options, model.provider);
305
+
306
+ if (context.tools) {
307
+ params.tools = convertTools(context.tools);
308
+ if (options?.toolChoice) {
309
+ const toolChoice = resolveToolChoice(model, options.toolChoice);
310
+ if (toolChoice.degraded && toolChoice.supportSource === "runtime") {
311
+ logger.debug("azure-openai-responses: degraded tool_choice after runtime capability discovery", {
312
+ model: model.id,
313
+ requestedLevel: toolChoice.requestedLevel,
314
+ resolvedLevel: toolChoice.resolvedLevel,
315
+ reason: toolChoice.reason,
316
+ });
317
+ }
318
+ params.tool_choice = mapToOpenAIResponsesToolChoice(toolChoice.resolvedChoice);
319
+ }
320
+ }
321
+
322
+ applyResponsesReasoningParams(params, model, options, messages);
323
+
324
+ return params;
325
+ }
326
+
327
+ function isForcedAzureResponsesToolChoice(choice: AzureOpenAIResponsesSamplingParams["tool_choice"]): boolean {
328
+ return !!choice && choice !== "none" && choice !== "auto";
329
+ }
330
+
331
+ function convertMessages(
332
+ model: Model<"azure-openai-responses">,
333
+ context: Context,
334
+ strictResponsesPairing: boolean,
335
+ resolvedBaseUrl?: string,
336
+ ): ResponseInput {
337
+ const messages: ResponseInput = [];
338
+ const transformedMessages = transformMessages(context.messages, model, normalizeResponsesToolCallIdForTransform);
339
+ const knownCallIds = new Set<string>();
340
+
341
+ const systemPrompts = normalizeSystemPrompts(context.systemPrompt);
342
+ if (systemPrompts.length > 0) {
343
+ const role = model.reasoning && supportsDeveloperRole(resolvedBaseUrl ?? model) ? "developer" : "system";
344
+ for (const systemPrompt of systemPrompts) {
345
+ messages.push({ role, content: systemPrompt });
346
+ }
347
+ }
348
+
349
+ let msgIndex = 0;
350
+ for (const msg of transformedMessages) {
351
+ if (msg.role === "user" || msg.role === "developer") {
352
+ const content = convertResponsesInputContent(msg.content, model.input.includes("image"));
353
+ if (!content) continue;
354
+ messages.push({
355
+ role: "user",
356
+ content: msg.role === "developer" && typeof msg.content === "string" ? msg.content.toWellFormed() : content,
357
+ });
358
+ } else if (msg.role === "assistant") {
359
+ const outputItems = convertResponsesAssistantMessage(msg as AssistantMessage, model, msgIndex, knownCallIds);
360
+ if (outputItems.length === 0) continue;
361
+ messages.push(...outputItems);
362
+ } else if (msg.role === "toolResult") {
363
+ appendResponsesToolResultMessages(messages, msg, model, strictResponsesPairing, knownCallIds);
364
+ }
365
+ msgIndex++;
366
+ }
367
+
368
+ return messages;
369
+ }
370
+
371
+ function convertTools(tools: Tool[]): OpenAITool[] {
372
+ return tools.map(tool => ({
373
+ type: "function",
374
+ name: tool.name,
375
+ description: tool.description || "",
376
+ parameters: sanitizeSchemaForOpenAIResponses(toolWireSchema(tool)),
377
+ strict: false,
378
+ }));
379
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Anchor/edit discipline for composer-harness models (xai grok-composer-*,
3
+ * cursor composer-*).
4
+ *
5
+ * Composer models are trained on a proprietary coding-agent harness
6
+ * (Cursor / Grok Build) and carry habits that break this agent's hashline
7
+ * edit workflow when driven through a generic provider. Observed in live
8
+ * sessions with grok-composer-2.5-fast:
9
+ *
10
+ * - they print files with shell commands (`sed -n`, `cat`, `grep -n`) or
11
+ * python heredocs whose output carries NO line anchors, then FABRICATE the
12
+ * 2-char anchor hash the edit tool requires (e.g. guessed "617hp" where
13
+ * the file had "617ca" → "Edit rejected: N anchors do not match");
14
+ * - they mutate files out-of-band via python heredocs (pathlib write_text /
15
+ * str.replace), which invalidates every previously seen anchor and defeats
16
+ * the read-cache snapshot that powers stale-anchor recovery;
17
+ * - they arithmetically renumber anchors after their own edits instead of
18
+ * copying them from the latest tool output;
19
+ * - they leak reasoning prose into heredoc bodies, producing shell/python
20
+ * syntax errors.
21
+ *
22
+ * This prompt is the per-request countermeasure, pinned ahead of the host
23
+ * system prompt on openai-completions, openai-responses, and cursor RPC paths.
24
+ */
25
+
26
+ /** Matches composer-harness model ids on any provider (xai grok-composer-*, cursor composer-* / composer2.*). */
27
+ const COMPOSER_MODEL_ID_PATTERN = /(?:^|[/:._-])(?:grok-)?composer(?:[/:._-]|(?=\d)|$)/i;
28
+
29
+ export function isComposerHarnessModel(modelId: string): boolean {
30
+ return COMPOSER_MODEL_ID_PATTERN.test(modelId);
31
+ }
32
+
33
+ export const COMPOSER_EDIT_DISCIPLINE_PROMPT = `File-editing discipline for this Composer harness (this OVERRIDES contrary habits from your training):
34
+
35
+ - Discover file names ONLY with the find tool; search file contents ONLY with the search tool; read file bodies or line ranges ONLY with the read tool. NEVER inspect repository files through shell commands (ls, find, fd, cat, sed, awk, grep, rg, head, tail, less, more) or scripts — that output carries no hashline anchors and bypasses the agent's safety limits.
36
+ - Modify files ONLY with the edit/write tools. NEVER mutate files through shell redirection, tee, sed -i, perl -pi, inline python/node/bun scripts, or other out-of-band writes — those writes invalidate every known anchor and break edit recovery.
37
+ - A line anchor (e.g. "42sr") is a line number plus a 2-char content hash. You CANNOT compute the hash yourself: copy anchors verbatim from the MOST RECENT read/search/edit output of that exact file. NEVER guess, renumber, or arithmetically shift an anchor.
38
+ - After ANY edit to a file (including your own), anchors you saw earlier are stale. Re-read the edited region, or copy the fresh anchors printed in the edit result, before issuing the next edit.
39
+ - If an edit is rejected with "anchors do not match", the rejection message prints the current lines WITH fresh anchors. Retry using exactly those printed anchors.
40
+ - Tool-call arguments must be the exact JSON/schema object requested by the tool. Do not include Markdown, commentary, analysis text, or invented fields inside tool arguments.
41
+ - Use bash only for terminal operations such as tests, builds, package scripts, and git commands. A shell command string must contain only the command itself; NEVER interleave reasoning or commentary into command strings or heredocs.`;