@caspian2026/agent-sdk 0.0.25 → 0.0.26

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 (362) hide show
  1. package/dist/gateway/index.js +98 -22
  2. package/dist/gateway/worker.js +86 -41
  3. package/dist/sdk/query.cjs +2 -362
  4. package/dist/sdk/query.js +2 -367
  5. package/package.json +4 -6
  6. package/dist/chunk-04n4fe72.js +0 -107
  7. package/dist/chunk-04ys66ej.js +0 -344
  8. package/dist/chunk-07069jq1.js +0 -30
  9. package/dist/chunk-0731m51q.js +0 -8
  10. package/dist/chunk-086x4ct4.js +0 -26
  11. package/dist/chunk-0btqb97n.js +0 -295
  12. package/dist/chunk-0paqc2yw.js +0 -15
  13. package/dist/chunk-0r1rjydx.js +0 -145
  14. package/dist/chunk-0r5bb9j4.js +0 -477
  15. package/dist/chunk-0t81d427.js +0 -41
  16. package/dist/chunk-0teq2mz0.js +0 -420
  17. package/dist/chunk-0vkfrmqm.js +0 -690
  18. package/dist/chunk-0vp3wfab.js +0 -12
  19. package/dist/chunk-0warp8dx.js +0 -253
  20. package/dist/chunk-0xjaqda8.js +0 -1124
  21. package/dist/chunk-10gzv316.js +0 -62
  22. package/dist/chunk-11pq9n08.js +0 -168
  23. package/dist/chunk-1ae3nt9j.js +0 -31
  24. package/dist/chunk-1fmb0njn.js +0 -396
  25. package/dist/chunk-1gx78081.js +0 -66
  26. package/dist/chunk-1h2famwb.js +0 -49
  27. package/dist/chunk-1ng8xwxz.js +0 -276
  28. package/dist/chunk-1v7jp29h.js +0 -206
  29. package/dist/chunk-1xxy5zhb.js +0 -673
  30. package/dist/chunk-20b73w7w.js +0 -117
  31. package/dist/chunk-20qxd83a.js +0 -1161
  32. package/dist/chunk-2239ny9q.js +0 -90
  33. package/dist/chunk-22d4rnef.js +0 -226
  34. package/dist/chunk-26wtxbdc.js +0 -13870
  35. package/dist/chunk-27225bjh.js +0 -618
  36. package/dist/chunk-2b0e32an.js +0 -5822
  37. package/dist/chunk-2bfa9w1y.js +0 -323
  38. package/dist/chunk-2epq04w1.js +0 -63
  39. package/dist/chunk-2fwze72g.js +0 -24
  40. package/dist/chunk-2gzv8nrw.js +0 -205
  41. package/dist/chunk-2mexte1r.js +0 -349
  42. package/dist/chunk-2mzaqh5g.js +0 -338
  43. package/dist/chunk-2tb6mzfc.js +0 -81
  44. package/dist/chunk-2xq0g43w.js +0 -558
  45. package/dist/chunk-334aeqg9.js +0 -90
  46. package/dist/chunk-34jx4rjd.js +0 -995
  47. package/dist/chunk-35w5nhex.js +0 -132
  48. package/dist/chunk-3c25bcsw.js +0 -17
  49. package/dist/chunk-3eaxj8dg.js +0 -151
  50. package/dist/chunk-3fdgrwd2.js +0 -111
  51. package/dist/chunk-3jw0r58d.js +0 -121
  52. package/dist/chunk-3mgywd32.js +0 -8
  53. package/dist/chunk-3nk79af8.js +0 -8
  54. package/dist/chunk-3vy1jt9p.js +0 -1010
  55. package/dist/chunk-3x78pd4j.js +0 -153
  56. package/dist/chunk-47x3dsmn.js +0 -95
  57. package/dist/chunk-49pqt48n.js +0 -108
  58. package/dist/chunk-4f7m9fjw.js +0 -317
  59. package/dist/chunk-4jm600zv.js +0 -13
  60. package/dist/chunk-4k9d7gxv.js +0 -118
  61. package/dist/chunk-4net8san.js +0 -155
  62. package/dist/chunk-4pbx3g7s.js +0 -26
  63. package/dist/chunk-4v125j4z.js +0 -58
  64. package/dist/chunk-4vxkv5vz.js +0 -1438
  65. package/dist/chunk-51fqpjy3.js +0 -121
  66. package/dist/chunk-52e64f2h.js +0 -439
  67. package/dist/chunk-52e9mdjv.js +0 -684
  68. package/dist/chunk-55raexh7.js +0 -14358
  69. package/dist/chunk-59pq5122.js +0 -269
  70. package/dist/chunk-5bsj67v1.js +0 -349
  71. package/dist/chunk-5ccdsxgy.js +0 -74
  72. package/dist/chunk-5ekyhb1w.js +0 -422
  73. package/dist/chunk-5gcqdn8x.js +0 -867
  74. package/dist/chunk-5h5kx2jd.js +0 -34
  75. package/dist/chunk-5hf3werb.js +0 -193
  76. package/dist/chunk-5n1jc02d.js +0 -87
  77. package/dist/chunk-5wfxvxm8.js +0 -127
  78. package/dist/chunk-5wjebwmn.js +0 -89
  79. package/dist/chunk-5z28bqne.js +0 -6748
  80. package/dist/chunk-5zwsxpjn.js +0 -671
  81. package/dist/chunk-60phw466.js +0 -29
  82. package/dist/chunk-64c1avct.js +0 -13
  83. package/dist/chunk-64r16v22.js +0 -162
  84. package/dist/chunk-65t1r7zs.js +0 -418
  85. package/dist/chunk-66zry909.js +0 -11
  86. package/dist/chunk-67hxjjgc.js +0 -731
  87. package/dist/chunk-68ffk5qa.js +0 -14
  88. package/dist/chunk-69dr6rrd.js +0 -177
  89. package/dist/chunk-69n11zng.js +0 -436
  90. package/dist/chunk-6dj5t602.js +0 -341
  91. package/dist/chunk-6dngjwkh.js +0 -63
  92. package/dist/chunk-6dp8sz55.js +0 -3561
  93. package/dist/chunk-6hqgg0sv.js +0 -103
  94. package/dist/chunk-6nsd9606.js +0 -790
  95. package/dist/chunk-6p0hbwz3.js +0 -65
  96. package/dist/chunk-6ywdv5vg.js +0 -97
  97. package/dist/chunk-6zc758qw.js +0 -640
  98. package/dist/chunk-7aw745vx.js +0 -40175
  99. package/dist/chunk-7fxaphbq.js +0 -116
  100. package/dist/chunk-7sgtxrsc.js +0 -1059
  101. package/dist/chunk-7wm5s02e.js +0 -216
  102. package/dist/chunk-7xdk4109.js +0 -442
  103. package/dist/chunk-7xyswgjh.js +0 -8
  104. package/dist/chunk-83hfzbx3.js +0 -10
  105. package/dist/chunk-87m69h0v.js +0 -219
  106. package/dist/chunk-8g5pe1gr.js +0 -37
  107. package/dist/chunk-8jbw0fnf.js +0 -167
  108. package/dist/chunk-8jf9se66.js +0 -321
  109. package/dist/chunk-8p65embj.js +0 -875
  110. package/dist/chunk-8qrmaff1.js +0 -2669
  111. package/dist/chunk-8r975mwc.js +0 -93
  112. package/dist/chunk-8yhf030a.js +0 -69
  113. package/dist/chunk-93emz367.js +0 -107
  114. package/dist/chunk-948mn975.js +0 -460
  115. package/dist/chunk-99chkt7r.js +0 -54
  116. package/dist/chunk-9f7c1hzz.js +0 -50
  117. package/dist/chunk-9fk9ps1g.js +0 -178
  118. package/dist/chunk-9rh0k0aq.js +0 -90
  119. package/dist/chunk-9x5zgy22.js +0 -12
  120. package/dist/chunk-a0k2vj5b.js +0 -460
  121. package/dist/chunk-a0m57ssq.js +0 -325
  122. package/dist/chunk-a0nsmmz1.js +0 -133
  123. package/dist/chunk-a25jh1v4.js +0 -105
  124. package/dist/chunk-a4j5ekv0.js +0 -83
  125. package/dist/chunk-a8k3zf58.js +0 -313
  126. package/dist/chunk-ah0zarbq.js +0 -260
  127. package/dist/chunk-ase86y73.js +0 -15
  128. package/dist/chunk-b0kf00ff.js +0 -637
  129. package/dist/chunk-b1kcd3ss.js +0 -27
  130. package/dist/chunk-b8kcqqds.js +0 -93
  131. package/dist/chunk-bg4x9ex0.js +0 -21
  132. package/dist/chunk-bh5zqvyc.js +0 -36
  133. package/dist/chunk-bqrkfekw.js +0 -267
  134. package/dist/chunk-brjd9zmk.js +0 -42
  135. package/dist/chunk-byfawrfx.js +0 -121
  136. package/dist/chunk-c6sjhj89.js +0 -1284
  137. package/dist/chunk-c7e5yv0q.js +0 -212
  138. package/dist/chunk-c7jwbe76.js +0 -29030
  139. package/dist/chunk-cb5v8w7k.js +0 -289
  140. package/dist/chunk-cdef9v7b.js +0 -27
  141. package/dist/chunk-cdz5yb0r.js +0 -57
  142. package/dist/chunk-cgfdkzhb.js +0 -12
  143. package/dist/chunk-cgqw4x5z.js +0 -155
  144. package/dist/chunk-cnsj3vq2.js +0 -35
  145. package/dist/chunk-cqxm7ggc.js +0 -123
  146. package/dist/chunk-cxrktb5g.js +0 -51
  147. package/dist/chunk-d089t7dk.js +0 -312
  148. package/dist/chunk-d1z934cp.js +0 -71
  149. package/dist/chunk-d4mdda98.js +0 -94
  150. package/dist/chunk-d7xw8v91.js +0 -332
  151. package/dist/chunk-dc06c3rw.js +0 -1154
  152. package/dist/chunk-ddsjkqs4.js +0 -39
  153. package/dist/chunk-dfzkwynj.js +0 -5368
  154. package/dist/chunk-dnmp7s85.js +0 -20
  155. package/dist/chunk-dqqwjkv3.js +0 -8583
  156. package/dist/chunk-dt2mr4vf.js +0 -2962
  157. package/dist/chunk-dztm97gg.js +0 -150
  158. package/dist/chunk-e0abab5b.js +0 -445
  159. package/dist/chunk-e0ragz46.js +0 -209386
  160. package/dist/chunk-e9w9zc0g.js +0 -392
  161. package/dist/chunk-ebwskmsr.js +0 -107
  162. package/dist/chunk-efztk52j.js +0 -114
  163. package/dist/chunk-ehd8wacx.js +0 -40
  164. package/dist/chunk-ehtwnxpg.js +0 -1591
  165. package/dist/chunk-eqp1rfft.js +0 -17
  166. package/dist/chunk-etdt1xz3.js +0 -19
  167. package/dist/chunk-eva785x8.js +0 -88
  168. package/dist/chunk-ewadzrm8.js +0 -103
  169. package/dist/chunk-f2ajmcav.js +0 -92
  170. package/dist/chunk-fajg8x36.js +0 -8158
  171. package/dist/chunk-fbv4apne.js +0 -51
  172. package/dist/chunk-fcynwjpm.js +0 -1072
  173. package/dist/chunk-fev86f95.js +0 -1052
  174. package/dist/chunk-feyzykye.js +0 -10
  175. package/dist/chunk-ff85rwvc.js +0 -180
  176. package/dist/chunk-fk6ktvg2.js +0 -275
  177. package/dist/chunk-fqak69ft.js +0 -458
  178. package/dist/chunk-fqwas4jk.js +0 -281
  179. package/dist/chunk-fs35fdpj.js +0 -439
  180. package/dist/chunk-fvr3f9s3.js +0 -55
  181. package/dist/chunk-fyc5fepv.js +0 -10
  182. package/dist/chunk-g2g9cwt9.js +0 -54
  183. package/dist/chunk-g4znz46v.js +0 -2335
  184. package/dist/chunk-g5ts4e6k.js +0 -697
  185. package/dist/chunk-g6een9wr.js +0 -654
  186. package/dist/chunk-g7cmtx8r.js +0 -77
  187. package/dist/chunk-g9f19ttm.js +0 -15
  188. package/dist/chunk-gc7ap41t.js +0 -516
  189. package/dist/chunk-gdwdqewd.js +0 -19
  190. package/dist/chunk-gg14h3n4.js +0 -131
  191. package/dist/chunk-gg55fns0.js +0 -6440
  192. package/dist/chunk-gq7zab4e.js +0 -418
  193. package/dist/chunk-gydw3qy3.js +0 -169
  194. package/dist/chunk-gz1e8y1w.js +0 -33
  195. package/dist/chunk-h19r628d.js +0 -91
  196. package/dist/chunk-h2k8mt3z.js +0 -329
  197. package/dist/chunk-h9n54swm.js +0 -27
  198. package/dist/chunk-hakdhagh.js +0 -16606
  199. package/dist/chunk-hc2chp3g.js +0 -308
  200. package/dist/chunk-hemcv9t7.js +0 -153
  201. package/dist/chunk-hm62ntv3.js +0 -20
  202. package/dist/chunk-hmq09ytj.js +0 -100
  203. package/dist/chunk-hmv4hhqt.js +0 -2718
  204. package/dist/chunk-hwfknpxv.js +0 -39
  205. package/dist/chunk-j5bth84e.js +0 -97
  206. package/dist/chunk-j64ga6ta.js +0 -8035
  207. package/dist/chunk-j6v3w4yf.js +0 -244
  208. package/dist/chunk-jc29d2nm.js +0 -130
  209. package/dist/chunk-jd32zbps.js +0 -15
  210. package/dist/chunk-jd4sx4wh.js +0 -594
  211. package/dist/chunk-jha8n8b9.js +0 -33118
  212. package/dist/chunk-jj71qx7m.js +0 -122
  213. package/dist/chunk-jka9vpak.js +0 -114
  214. package/dist/chunk-jkf6ayv0.js +0 -32
  215. package/dist/chunk-jpz6t2tt.js +0 -61
  216. package/dist/chunk-jqt3sw12.js +0 -9114
  217. package/dist/chunk-jswd1q63.js +0 -101
  218. package/dist/chunk-jtc4aa2y.js +0 -1543
  219. package/dist/chunk-jyx3h86z.js +0 -635
  220. package/dist/chunk-jzthz1bx.js +0 -15
  221. package/dist/chunk-k0p9w03v.js +0 -4957
  222. package/dist/chunk-k6x3s7x5.js +0 -211
  223. package/dist/chunk-k7kmvnnx.js +0 -942
  224. package/dist/chunk-k8vp0atq.js +0 -2331
  225. package/dist/chunk-k9vm6x6z.js +0 -257
  226. package/dist/chunk-kb1edvhs.js +0 -120
  227. package/dist/chunk-kbhbe33q.js +0 -113
  228. package/dist/chunk-kgcgpp7z.js +0 -140
  229. package/dist/chunk-kvywbvqs.js +0 -261
  230. package/dist/chunk-kwekc97v.js +0 -18
  231. package/dist/chunk-kwyk9zp1.js +0 -74
  232. package/dist/chunk-ky1z78ve.js +0 -119
  233. package/dist/chunk-mcws22k3.js +0 -42
  234. package/dist/chunk-mr2z79r3.js +0 -1385
  235. package/dist/chunk-mst9nmns.js +0 -642
  236. package/dist/chunk-mswx2nma.js +0 -103
  237. package/dist/chunk-n1e3fjpz.js +0 -266
  238. package/dist/chunk-n6vjp9yg.js +0 -138
  239. package/dist/chunk-n84tqtvv.js +0 -120
  240. package/dist/chunk-n8ssddbw.js +0 -105
  241. package/dist/chunk-na35whkd.js +0 -227
  242. package/dist/chunk-netzwgv1.js +0 -154
  243. package/dist/chunk-ng7j0yy1.js +0 -100
  244. package/dist/chunk-nmgj86tt.js +0 -24
  245. package/dist/chunk-ns7x6xam.js +0 -22820
  246. package/dist/chunk-p7vkh4wx.js +0 -15
  247. package/dist/chunk-p8na2wwp.js +0 -14
  248. package/dist/chunk-pe3rkfp2.js +0 -74
  249. package/dist/chunk-pf0152j3.js +0 -40
  250. package/dist/chunk-pgtnj1mt.js +0 -705
  251. package/dist/chunk-ppwjyveh.js +0 -8
  252. package/dist/chunk-pq5qp2y4.js +0 -388
  253. package/dist/chunk-pqqxdk5f.js +0 -543
  254. package/dist/chunk-ps49ymvj.js +0 -43
  255. package/dist/chunk-pws8gq7m.js +0 -7422
  256. package/dist/chunk-pwwa7s62.js +0 -11
  257. package/dist/chunk-pyq0fabn.js +0 -860
  258. package/dist/chunk-q0p1m40t.js +0 -79
  259. package/dist/chunk-q0vhv550.js +0 -393
  260. package/dist/chunk-q7r0kt08.js +0 -1809
  261. package/dist/chunk-qajrkk97.js +0 -298
  262. package/dist/chunk-qatfdxvd.js +0 -34
  263. package/dist/chunk-qb6k2afy.js +0 -153
  264. package/dist/chunk-qg2qgx8h.js +0 -266
  265. package/dist/chunk-qkczt8cr.js +0 -24
  266. package/dist/chunk-qnfx3qtx.js +0 -617
  267. package/dist/chunk-qp2qdcda.js +0 -100
  268. package/dist/chunk-qtwgzej9.js +0 -85
  269. package/dist/chunk-r1dh0ymt.js +0 -91
  270. package/dist/chunk-r36xvgvv.js +0 -407
  271. package/dist/chunk-r48he8g3.js +0 -251
  272. package/dist/chunk-r63tqmtk.js +0 -146
  273. package/dist/chunk-rbdd3q5k.js +0 -101
  274. package/dist/chunk-rmbbmd3s.js +0 -68
  275. package/dist/chunk-rn0v1hk8.js +0 -34
  276. package/dist/chunk-rnxctq9p.js +0 -1646
  277. package/dist/chunk-rrs2mchs.js +0 -183
  278. package/dist/chunk-rtrybvnp.js +0 -209
  279. package/dist/chunk-rw2wkn45.js +0 -494
  280. package/dist/chunk-s4kzkdhd.js +0 -847
  281. package/dist/chunk-s7a0zn7b.js +0 -55
  282. package/dist/chunk-s8gy4ky9.js +0 -122
  283. package/dist/chunk-s8kj6ajy.js +0 -32
  284. package/dist/chunk-sag785ad.js +0 -283
  285. package/dist/chunk-scqjyben.js +0 -174
  286. package/dist/chunk-sd36yzx8.js +0 -954
  287. package/dist/chunk-sdtqm0nm.js +0 -374
  288. package/dist/chunk-sg60xg0x.js +0 -52
  289. package/dist/chunk-sgpmpn90.js +0 -35
  290. package/dist/chunk-sh02d2qn.js +0 -38
  291. package/dist/chunk-sz9y6z13.js +0 -61
  292. package/dist/chunk-szj5wvdy.js +0 -28
  293. package/dist/chunk-t29a6zb5.js +0 -45
  294. package/dist/chunk-t2b49hcf.js +0 -87
  295. package/dist/chunk-t2wqdppz.js +0 -445
  296. package/dist/chunk-t4khqff1.js +0 -16
  297. package/dist/chunk-t70t2gne.js +0 -121
  298. package/dist/chunk-t9p85zff.js +0 -24
  299. package/dist/chunk-tkrstmwz.js +0 -94
  300. package/dist/chunk-tnyb4exc.js +0 -74
  301. package/dist/chunk-tpn8jsvw.js +0 -71
  302. package/dist/chunk-tt7x5j1q.js +0 -111
  303. package/dist/chunk-tw1p628z.js +0 -984
  304. package/dist/chunk-twvdafcz.js +0 -528
  305. package/dist/chunk-tzwjafgn.js +0 -24
  306. package/dist/chunk-v140t8ay.js +0 -152
  307. package/dist/chunk-v1kzp02e.js +0 -785
  308. package/dist/chunk-v4t6dyyh.js +0 -9076
  309. package/dist/chunk-v78fj8by.js +0 -145
  310. package/dist/chunk-v8kjswxt.js +0 -61
  311. package/dist/chunk-v9smspw2.js +0 -4301
  312. package/dist/chunk-vcfbjkk9.js +0 -201
  313. package/dist/chunk-ve167rcs.js +0 -148
  314. package/dist/chunk-vf612n57.js +0 -472
  315. package/dist/chunk-vhffyae8.js +0 -895
  316. package/dist/chunk-vkhrrvfq.js +0 -44
  317. package/dist/chunk-vybkmfhf.js +0 -91
  318. package/dist/chunk-vyjeh50y.js +0 -2149
  319. package/dist/chunk-w6m6trzq.js +0 -58
  320. package/dist/chunk-w9b6kngq.js +0 -234
  321. package/dist/chunk-wa8yn2p0.js +0 -2007
  322. package/dist/chunk-wanceff6.js +0 -2840
  323. package/dist/chunk-wcze2nea.js +0 -336
  324. package/dist/chunk-wgk9sxk7.js +0 -329
  325. package/dist/chunk-wnxcgfsd.js +0 -253
  326. package/dist/chunk-wyjerrr6.js +0 -565
  327. package/dist/chunk-wz9brkq5.js +0 -60
  328. package/dist/chunk-x47h283a.js +0 -56
  329. package/dist/chunk-x59j3x9c.js +0 -98
  330. package/dist/chunk-x6r4v44b.js +0 -8
  331. package/dist/chunk-x7fdwq3d.js +0 -33
  332. package/dist/chunk-x8d2ehd7.js +0 -19
  333. package/dist/chunk-xeb6075z.js +0 -3435
  334. package/dist/chunk-xfw2pxwd.js +0 -223
  335. package/dist/chunk-xh06jc72.js +0 -200
  336. package/dist/chunk-xhfmdq1y.js +0 -147
  337. package/dist/chunk-xkwyg1d2.js +0 -195
  338. package/dist/chunk-xv3era3s.js +0 -8
  339. package/dist/chunk-xyapc58m.js +0 -87
  340. package/dist/chunk-xzgnc76g.js +0 -107
  341. package/dist/chunk-y1784krc.js +0 -19
  342. package/dist/chunk-y4ca9kqz.js +0 -3830
  343. package/dist/chunk-y9bhjjdz.js +0 -145
  344. package/dist/chunk-yb3bxv3w.js +0 -30
  345. package/dist/chunk-yddek6xk.js +0 -146
  346. package/dist/chunk-ypaq3d12.js +0 -4265
  347. package/dist/chunk-yq8z040g.js +0 -111
  348. package/dist/chunk-yqs481qq.js +0 -1163
  349. package/dist/chunk-ys9a9c9h.js +0 -105
  350. package/dist/chunk-yyex5gsf.js +0 -269
  351. package/dist/chunk-yzn396hn.js +0 -89
  352. package/dist/chunk-z2dp53wn.js +0 -17
  353. package/dist/chunk-zay4xp4n.js +0 -251
  354. package/dist/chunk-zc8bwy57.js +0 -212
  355. package/dist/chunk-zhx88cfx.js +0 -97
  356. package/dist/chunk-zkh6f4ct.js +0 -385
  357. package/dist/chunk-zq56gcca.js +0 -206
  358. package/dist/chunk-zrkefd22.js +0 -490
  359. package/dist/chunk-zs5p0yq1.js +0 -1030
  360. package/dist/chunk-zxh62ved.js +0 -31
  361. package/dist/chunk-zzm33q8x.js +0 -8
  362. package/dist/cli.js +0 -220
@@ -1,942 +0,0 @@
1
- // @bun
2
- import {
3
- anthropicToolChoiceToOpenAI,
4
- anthropicToolsToOpenAI,
5
- createAssistantAPIErrorMessage,
6
- createUserMessage,
7
- extractDiscoveredToolNames,
8
- getEmptyToolPermissionContext,
9
- getOpenAIClient,
10
- getOpenAIMaxRetries,
11
- getOpenAIRequestTimeoutMs,
12
- init_Tool,
13
- init_api,
14
- init_client1 as init_client,
15
- init_convertTools,
16
- init_messages1 as init_messages,
17
- init_modelMapping,
18
- init_toolSearch,
19
- isDeferredToolsDeltaEnabled,
20
- isToolSearchEnabled,
21
- normalizeContentFromAPI,
22
- normalizeMessagesForAPI,
23
- resolveOpenAIModel,
24
- toolMatchesName,
25
- toolToAPISchema
26
- } from "./chunk-e0ragz46.js";
27
- import"./chunk-mcws22k3.js";
28
- import"./chunk-jzthz1bx.js";
29
- import"./chunk-jpz6t2tt.js";
30
- import"./chunk-hwfknpxv.js";
31
- import"./chunk-1gx78081.js";
32
- import"./chunk-ehtwnxpg.js";
33
- import"./chunk-q0p1m40t.js";
34
- import"./chunk-52e9mdjv.js";
35
- import"./chunk-x47h283a.js";
36
- import"./chunk-34jx4rjd.js";
37
- import"./chunk-2gzv8nrw.js";
38
- import"./chunk-e0abab5b.js";
39
- import"./chunk-cgfdkzhb.js";
40
- import"./chunk-4k9d7gxv.js";
41
- import"./chunk-j5bth84e.js";
42
- import"./chunk-67hxjjgc.js";
43
- import"./chunk-68ffk5qa.js";
44
- import"./chunk-ps49ymvj.js";
45
- import {
46
- addToTotalSessionCost,
47
- init_cost_tracker
48
- } from "./chunk-gc7ap41t.js";
49
- import {
50
- calculateUSDCost,
51
- getModelMaxOutputTokens,
52
- init_context,
53
- init_modelCost
54
- } from "./chunk-jtc4aa2y.js";
55
- import"./chunk-pgtnj1mt.js";
56
- import {
57
- TOOL_SEARCH_TOOL_NAME,
58
- formatDeferredToolLine,
59
- init_prompt,
60
- isDeferredTool
61
- } from "./chunk-6p0hbwz3.js";
62
- import"./chunk-p7vkh4wx.js";
63
- import"./chunk-y9bhjjdz.js";
64
- import"./chunk-v140t8ay.js";
65
- import"./chunk-99chkt7r.js";
66
- import"./chunk-dfzkwynj.js";
67
- import"./chunk-xeb6075z.js";
68
- import"./chunk-fcynwjpm.js";
69
- import"./chunk-v4t6dyyh.js";
70
- import"./chunk-h2k8mt3z.js";
71
- import"./chunk-v8kjswxt.js";
72
- import"./chunk-2b0e32an.js";
73
- import"./chunk-rnxctq9p.js";
74
- import"./chunk-pqqxdk5f.js";
75
- import"./chunk-6nsd9606.js";
76
- import"./chunk-64c1avct.js";
77
- import"./chunk-8g5pe1gr.js";
78
- import"./chunk-4pbx3g7s.js";
79
- import"./chunk-26wtxbdc.js";
80
- import"./chunk-6dngjwkh.js";
81
- import"./chunk-4jm600zv.js";
82
- import"./chunk-s8gy4ky9.js";
83
- import"./chunk-cxrktb5g.js";
84
- import"./chunk-3c25bcsw.js";
85
- import"./chunk-5ekyhb1w.js";
86
- import"./chunk-fqak69ft.js";
87
- import"./chunk-cdz5yb0r.js";
88
- import"./chunk-c7jwbe76.js";
89
- import"./chunk-0vkfrmqm.js";
90
- import"./chunk-7sgtxrsc.js";
91
- import"./chunk-0xjaqda8.js";
92
- import"./chunk-x8d2ehd7.js";
93
- import"./chunk-byfawrfx.js";
94
- import"./chunk-3vy1jt9p.js";
95
- import"./chunk-vkhrrvfq.js";
96
- import"./chunk-qnfx3qtx.js";
97
- import"./chunk-64r16v22.js";
98
- import"./chunk-s8kj6ajy.js";
99
- import"./chunk-8yhf030a.js";
100
- import"./chunk-zxh62ved.js";
101
- import"./chunk-5z28bqne.js";
102
- import"./chunk-qajrkk97.js";
103
- import {
104
- init_debug,
105
- logForDebugging
106
- } from "./chunk-gg55fns0.js";
107
- import {
108
- init_envUtils,
109
- isEnvDefinedFalsy,
110
- isEnvTruthy
111
- } from "./chunk-5n1jc02d.js";
112
- import {
113
- init_process,
114
- writeToStderr
115
- } from "./chunk-fbv4apne.js";
116
- import"./chunk-k8vp0atq.js";
117
- import"./chunk-vf612n57.js";
118
- import"./chunk-07069jq1.js";
119
- import"./chunk-d4mdda98.js";
120
- import"./chunk-55raexh7.js";
121
- import"./chunk-v9smspw2.js";
122
- import"./chunk-v1kzp02e.js";
123
- import"./chunk-7wm5s02e.js";
124
- import"./chunk-b0kf00ff.js";
125
- import"./chunk-netzwgv1.js";
126
- import {
127
- __esm,
128
- __require
129
- } from "./chunk-qp2qdcda.js";
130
-
131
- // src/services/api/openai/convertMessages.ts
132
- function anthropicMessagesToOpenAI(messages, systemPrompt, options) {
133
- const result = [];
134
- const enableThinking = options?.enableThinking ?? false;
135
- const systemText = systemPromptToText(systemPrompt);
136
- if (systemText) {
137
- result.push({
138
- role: "system",
139
- content: systemText
140
- });
141
- }
142
- const turnBoundaries = new Set;
143
- if (enableThinking) {
144
- let hasSeenAssistant = false;
145
- for (let i = 0;i < messages.length; i++) {
146
- const msg = messages[i];
147
- if (msg.type === "assistant") {
148
- hasSeenAssistant = true;
149
- }
150
- if (msg.type === "user" && hasSeenAssistant) {
151
- const content = msg.message.content;
152
- const startsNewUserTurn = typeof content === "string" ? content.length > 0 : Array.isArray(content) && content.some((b) => typeof b === "string" || b && typeof b === "object" && ("type" in b) && b.type !== "tool_result");
153
- if (startsNewUserTurn) {
154
- turnBoundaries.add(i);
155
- }
156
- }
157
- }
158
- }
159
- for (let i = 0;i < messages.length; i++) {
160
- const msg = messages[i];
161
- switch (msg.type) {
162
- case "user":
163
- result.push(...convertInternalUserMessage(msg));
164
- break;
165
- case "assistant":
166
- const preserveReasoning = enableThinking && !isBeforeAnyTurnBoundary(i, turnBoundaries);
167
- result.push(...convertInternalAssistantMessage(msg, preserveReasoning));
168
- break;
169
- default:
170
- break;
171
- }
172
- }
173
- return result;
174
- }
175
- function systemPromptToText(systemPrompt) {
176
- if (!systemPrompt || systemPrompt.length === 0)
177
- return "";
178
- return systemPrompt.filter(Boolean).join(`
179
-
180
- `);
181
- }
182
- function isBeforeAnyTurnBoundary(i, boundaries) {
183
- for (const b of boundaries) {
184
- if (i < b)
185
- return true;
186
- }
187
- return false;
188
- }
189
- function convertInternalUserMessage(msg) {
190
- const result = [];
191
- const content = msg.message.content;
192
- if (typeof content === "string") {
193
- result.push({
194
- role: "user",
195
- content
196
- });
197
- } else if (Array.isArray(content)) {
198
- const textParts = [];
199
- const imageParts = [];
200
- const toolResults = [];
201
- for (const block of content) {
202
- if (typeof block === "string") {
203
- textParts.push(block);
204
- } else if (block.type === "text") {
205
- textParts.push(block.text);
206
- } else if (block.type === "tool_result") {
207
- toolResults.push(block);
208
- } else if (block.type === "image") {
209
- if (block.source.type === "base64") {
210
- imageParts.push({
211
- type: "image_url",
212
- image_url: { url: `data:${block.source.media_type};base64,${block.source.data}` }
213
- });
214
- } else if (block.source.type === "url") {
215
- imageParts.push({
216
- type: "image_url",
217
- image_url: { url: block.source.url }
218
- });
219
- }
220
- }
221
- }
222
- const toolFollowUpMessages = [];
223
- for (const tr of toolResults) {
224
- const [toolMessage, ...followUpMessages] = convertToolResult(tr);
225
- result.push(toolMessage);
226
- toolFollowUpMessages.push(...followUpMessages);
227
- }
228
- result.push(...toolFollowUpMessages);
229
- if (textParts.length > 0 || imageParts.length > 0) {
230
- if (imageParts.length > 0) {
231
- const contentParts = [];
232
- if (textParts.length > 0) {
233
- contentParts.push({ type: "text", text: textParts.join(`
234
- `) });
235
- }
236
- contentParts.push(...imageParts);
237
- result.push({
238
- role: "user",
239
- content: contentParts
240
- });
241
- } else {
242
- result.push({
243
- role: "user",
244
- content: textParts.join(`
245
- `)
246
- });
247
- }
248
- }
249
- }
250
- return result;
251
- }
252
- function convertToolResult(block) {
253
- const textParts = [];
254
- if (typeof block.content === "string") {
255
- textParts.push(block.content);
256
- } else if (Array.isArray(block.content)) {
257
- for (const c of block.content) {
258
- if (typeof c === "string") {
259
- textParts.push(c);
260
- continue;
261
- }
262
- if ("text" in c && typeof c.text === "string") {
263
- textParts.push(c.text);
264
- continue;
265
- }
266
- if (c.type === "image") {
267
- textParts.push("[image returned by tool]");
268
- } else if (c.type === "document") {
269
- textParts.push("[document returned by tool]");
270
- }
271
- }
272
- }
273
- return [{
274
- role: "tool",
275
- tool_call_id: block.tool_use_id,
276
- content: textParts.filter(Boolean).join(`
277
- `) || ""
278
- }];
279
- }
280
- function convertInternalAssistantMessage(msg, preserveReasoning = false) {
281
- const content = msg.message.content;
282
- if (typeof content === "string") {
283
- return [
284
- {
285
- role: "assistant",
286
- content
287
- }
288
- ];
289
- }
290
- if (!Array.isArray(content)) {
291
- return [
292
- {
293
- role: "assistant",
294
- content: ""
295
- }
296
- ];
297
- }
298
- const textParts = [];
299
- const toolCalls = [];
300
- const reasoningParts = [];
301
- for (const block of content) {
302
- if (typeof block === "string") {
303
- textParts.push(block);
304
- } else if (block.type === "text") {
305
- textParts.push(block.text);
306
- } else if (block.type === "tool_use") {
307
- const tu = block;
308
- toolCalls.push({
309
- id: tu.id,
310
- type: "function",
311
- function: {
312
- name: tu.name,
313
- arguments: typeof tu.input === "string" ? tu.input : JSON.stringify(tu.input)
314
- }
315
- });
316
- } else if (block.type === "thinking" && preserveReasoning) {
317
- const thinkingText = block.thinking;
318
- if (typeof thinkingText === "string" && thinkingText) {
319
- reasoningParts.push(thinkingText);
320
- }
321
- }
322
- }
323
- const result = {
324
- role: "assistant",
325
- content: textParts.length > 0 ? textParts.join(`
326
- `) : null,
327
- ...toolCalls.length > 0 && { tool_calls: toolCalls },
328
- ...reasoningParts.length > 0 && { reasoning_content: reasoningParts.join(`
329
- `) }
330
- };
331
- return [result];
332
- }
333
- var init_convertMessages = () => {};
334
-
335
- // src/services/api/openai/streamAdapter.ts
336
- import { randomUUID } from "crypto";
337
- async function* adaptOpenAIStreamToAnthropic(stream, model) {
338
- const messageId = `msg_${randomUUID().replace(/-/g, "").slice(0, 24)}`;
339
- let started = false;
340
- let currentContentIndex = -1;
341
- const toolBlocks = new Map;
342
- let thinkingBlockOpen = false;
343
- let textBlockOpen = false;
344
- let rawInputTokens = 0;
345
- let inputTokens = 0;
346
- let outputTokens = 0;
347
- let cachedReadTokens = 0;
348
- const openBlockIndices = new Set;
349
- let pendingFinishReason = null;
350
- let pendingHasToolCalls = false;
351
- for await (const chunk of stream) {
352
- const choice = chunk.choices?.[0];
353
- const delta = choice?.delta;
354
- if (chunk.usage) {
355
- rawInputTokens = chunk.usage.prompt_tokens ?? rawInputTokens;
356
- const rawCached = chunk.usage.prompt_tokens_details?.cached_tokens ?? cachedReadTokens;
357
- inputTokens = Math.max(0, rawInputTokens - rawCached);
358
- outputTokens = chunk.usage.completion_tokens ?? outputTokens;
359
- cachedReadTokens = rawCached;
360
- }
361
- if (!started) {
362
- started = true;
363
- yield {
364
- type: "message_start",
365
- message: {
366
- id: messageId,
367
- type: "message",
368
- role: "assistant",
369
- content: [],
370
- model,
371
- stop_reason: null,
372
- stop_sequence: null,
373
- usage: {
374
- input_tokens: inputTokens,
375
- output_tokens: 0,
376
- cache_creation_input_tokens: 0,
377
- cache_read_input_tokens: cachedReadTokens
378
- }
379
- }
380
- };
381
- }
382
- if (!delta)
383
- continue;
384
- const reasoningContent = delta.reasoning_content;
385
- if (reasoningContent != null && reasoningContent !== "") {
386
- if (!thinkingBlockOpen) {
387
- currentContentIndex++;
388
- thinkingBlockOpen = true;
389
- openBlockIndices.add(currentContentIndex);
390
- yield {
391
- type: "content_block_start",
392
- index: currentContentIndex,
393
- content_block: {
394
- type: "thinking",
395
- thinking: "",
396
- signature: ""
397
- }
398
- };
399
- }
400
- yield {
401
- type: "content_block_delta",
402
- index: currentContentIndex,
403
- delta: {
404
- type: "thinking_delta",
405
- thinking: reasoningContent
406
- }
407
- };
408
- }
409
- if (delta.content != null && delta.content !== "") {
410
- if (!textBlockOpen) {
411
- if (thinkingBlockOpen) {
412
- yield {
413
- type: "content_block_stop",
414
- index: currentContentIndex
415
- };
416
- openBlockIndices.delete(currentContentIndex);
417
- thinkingBlockOpen = false;
418
- }
419
- currentContentIndex++;
420
- textBlockOpen = true;
421
- openBlockIndices.add(currentContentIndex);
422
- yield {
423
- type: "content_block_start",
424
- index: currentContentIndex,
425
- content_block: {
426
- type: "text",
427
- text: ""
428
- }
429
- };
430
- }
431
- yield {
432
- type: "content_block_delta",
433
- index: currentContentIndex,
434
- delta: {
435
- type: "text_delta",
436
- text: delta.content
437
- }
438
- };
439
- }
440
- if (delta.tool_calls) {
441
- for (const tc of delta.tool_calls) {
442
- const tcIndex = tc.index;
443
- if (!toolBlocks.has(tcIndex)) {
444
- if (thinkingBlockOpen) {
445
- yield {
446
- type: "content_block_stop",
447
- index: currentContentIndex
448
- };
449
- openBlockIndices.delete(currentContentIndex);
450
- thinkingBlockOpen = false;
451
- }
452
- if (textBlockOpen) {
453
- yield {
454
- type: "content_block_stop",
455
- index: currentContentIndex
456
- };
457
- openBlockIndices.delete(currentContentIndex);
458
- textBlockOpen = false;
459
- }
460
- currentContentIndex++;
461
- const toolId = tc.id || `toolu_${randomUUID().replace(/-/g, "").slice(0, 24)}`;
462
- const toolName = tc.function?.name || "";
463
- toolBlocks.set(tcIndex, {
464
- contentIndex: currentContentIndex,
465
- id: toolId,
466
- name: toolName,
467
- arguments: ""
468
- });
469
- openBlockIndices.add(currentContentIndex);
470
- yield {
471
- type: "content_block_start",
472
- index: currentContentIndex,
473
- content_block: {
474
- type: "tool_use",
475
- id: toolId,
476
- name: toolName,
477
- input: {}
478
- }
479
- };
480
- }
481
- const argFragment = tc.function?.arguments;
482
- if (argFragment) {
483
- toolBlocks.get(tcIndex).arguments += argFragment;
484
- yield {
485
- type: "content_block_delta",
486
- index: toolBlocks.get(tcIndex).contentIndex,
487
- delta: {
488
- type: "input_json_delta",
489
- partial_json: argFragment
490
- }
491
- };
492
- }
493
- }
494
- }
495
- if (choice?.finish_reason) {
496
- if (thinkingBlockOpen) {
497
- yield {
498
- type: "content_block_stop",
499
- index: currentContentIndex
500
- };
501
- openBlockIndices.delete(currentContentIndex);
502
- thinkingBlockOpen = false;
503
- }
504
- if (textBlockOpen) {
505
- yield {
506
- type: "content_block_stop",
507
- index: currentContentIndex
508
- };
509
- openBlockIndices.delete(currentContentIndex);
510
- textBlockOpen = false;
511
- }
512
- for (const [, block] of toolBlocks) {
513
- if (openBlockIndices.has(block.contentIndex)) {
514
- yield {
515
- type: "content_block_stop",
516
- index: block.contentIndex
517
- };
518
- openBlockIndices.delete(block.contentIndex);
519
- }
520
- }
521
- pendingFinishReason = choice.finish_reason;
522
- pendingHasToolCalls = toolBlocks.size > 0;
523
- }
524
- }
525
- for (const idx of openBlockIndices) {
526
- yield {
527
- type: "content_block_stop",
528
- index: idx
529
- };
530
- }
531
- if (pendingFinishReason !== null) {
532
- const stopReason = pendingFinishReason === "length" ? "max_tokens" : pendingHasToolCalls ? "tool_use" : mapFinishReason(pendingFinishReason);
533
- yield {
534
- type: "message_delta",
535
- delta: {
536
- stop_reason: stopReason,
537
- stop_sequence: null
538
- },
539
- usage: {
540
- input_tokens: inputTokens,
541
- output_tokens: outputTokens,
542
- cache_read_input_tokens: cachedReadTokens,
543
- cache_creation_input_tokens: 0
544
- }
545
- };
546
- yield {
547
- type: "message_stop"
548
- };
549
- }
550
- }
551
- function mapFinishReason(reason) {
552
- switch (reason) {
553
- case "stop":
554
- return "end_turn";
555
- case "tool_calls":
556
- return "tool_use";
557
- case "length":
558
- return "max_tokens";
559
- case "content_filter":
560
- return "end_turn";
561
- default:
562
- return "end_turn";
563
- }
564
- }
565
- var init_streamAdapter = () => {};
566
-
567
- // src/services/api/openai/index.ts
568
- import { randomUUID as randomUUID2 } from "crypto";
569
- function isOpenAIThinkingEnabled(model) {
570
- if (isEnvDefinedFalsy(process.env.OPENAI_ENABLE_THINKING))
571
- return false;
572
- if (isEnvTruthy(process.env.OPENAI_ENABLE_THINKING))
573
- return true;
574
- const modelLower = model.toLowerCase();
575
- return modelLower.includes("deepseek");
576
- }
577
- function buildOpenAIRequestBody(params) {
578
- const { model, messages, tools, toolChoice, enableThinking, maxTokens, temperatureOverride } = params;
579
- return {
580
- model,
581
- messages,
582
- max_tokens: maxTokens,
583
- ...tools.length > 0 && {
584
- tools,
585
- ...toolChoice && { tool_choice: toolChoice }
586
- },
587
- stream: true,
588
- stream_options: { include_usage: true },
589
- ...enableThinking && {
590
- thinking: { type: "enabled" },
591
- enable_thinking: true,
592
- chat_template_kwargs: { thinking: true }
593
- },
594
- ...!enableThinking && temperatureOverride !== undefined && {
595
- temperature: temperatureOverride
596
- }
597
- };
598
- }
599
- function resolveOpenAIMaxTokens(upperLimit, maxOutputTokensOverride) {
600
- return maxOutputTokensOverride ?? (process.env.OPENAI_MAX_TOKENS ? parseInt(process.env.OPENAI_MAX_TOKENS, 10) || undefined : undefined) ?? (process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS ? parseInt(process.env.CLAUDE_CODE_MAX_OUTPUT_TOKENS, 10) || undefined : undefined) ?? upperLimit;
601
- }
602
- function prependDeferredToolListIfNeeded(messages, tools, deferredToolNames, useToolSearch) {
603
- if (!useToolSearch || isDeferredToolsDeltaEnabled())
604
- return messages;
605
- const deferredToolList = tools.filter((tool) => deferredToolNames.has(tool.name)).map(formatDeferredToolLine).sort().join(`
606
- `);
607
- if (!deferredToolList)
608
- return messages;
609
- return [
610
- createUserMessage({
611
- content: `<available-deferred-tools>
612
- ${deferredToolList}
613
- </available-deferred-tools>`,
614
- isMeta: true
615
- }),
616
- ...messages
617
- ];
618
- }
619
- function isOpenAIConvertibleMessage(msg) {
620
- return msg.type === "assistant" || msg.type === "user";
621
- }
622
- function assembleFinalAssistantOutputs(params) {
623
- const { partialMessage, contentBlocks, tools, agentId, usage, stopReason, maxTokens } = params;
624
- const outputs = [];
625
- const allBlocks = Object.keys(contentBlocks).sort((a, b) => Number(a) - Number(b)).map((k) => contentBlocks[Number(k)]).filter(Boolean);
626
- if (allBlocks.length > 0) {
627
- outputs.push({
628
- message: {
629
- ...partialMessage,
630
- content: normalizeContentFromAPI(allBlocks, tools, agentId),
631
- usage,
632
- stop_reason: stopReason,
633
- stop_sequence: null
634
- },
635
- requestId: undefined,
636
- type: "assistant",
637
- uuid: randomUUID2(),
638
- timestamp: new Date().toISOString()
639
- });
640
- }
641
- if (stopReason === "max_tokens") {
642
- outputs.push(createAssistantAPIErrorMessage({
643
- content: `Output truncated: response exceeded the ${maxTokens} token limit. ` + `Set OPENAI_MAX_TOKENS or CLAUDE_CODE_MAX_OUTPUT_TOKENS to override.`,
644
- apiError: "max_output_tokens",
645
- error: "max_output_tokens"
646
- }));
647
- }
648
- return outputs;
649
- }
650
- async function* queryModelOpenAI(messages, systemPrompt, tools, signal, options) {
651
- const requestStartedAt = Date.now();
652
- let requestStage = "preparing_request";
653
- let sawStreamEvent = false;
654
- let diagnosticModel = options.model;
655
- let diagnosticMessageCount = messages.length;
656
- let diagnosticToolCount = tools.length;
657
- try {
658
- const openaiModel = resolveOpenAIModel(options.model);
659
- diagnosticModel = openaiModel;
660
- const messagesForAPI = normalizeMessagesForAPI(messages, tools);
661
- const useToolSearch = await isToolSearchEnabled(options.model, tools, options.getToolPermissionContext || (async () => getEmptyToolPermissionContext()), options.agents || [], options.querySource);
662
- const deferredToolNames = new Set;
663
- if (useToolSearch) {
664
- for (const t of tools) {
665
- if (isDeferredTool(t))
666
- deferredToolNames.add(t.name);
667
- }
668
- }
669
- let filteredTools = tools;
670
- if (useToolSearch && deferredToolNames.size > 0) {
671
- const discoveredToolNames = extractDiscoveredToolNames(messages);
672
- filteredTools = tools.filter((tool) => {
673
- if (!deferredToolNames.has(tool.name))
674
- return true;
675
- if (toolMatchesName(tool, TOOL_SEARCH_TOOL_NAME))
676
- return true;
677
- return discoveredToolNames.has(tool.name);
678
- });
679
- }
680
- const toolSchemas = await Promise.all(filteredTools.map((tool) => toolToAPISchema(tool, {
681
- getToolPermissionContext: options.getToolPermissionContext,
682
- tools,
683
- agents: options.agents,
684
- allowedAgentTypes: options.allowedAgentTypes,
685
- model: options.model,
686
- deferLoading: useToolSearch && deferredToolNames.has(tool.name)
687
- })));
688
- const standardTools = toolSchemas.filter((t) => {
689
- const anyT = t;
690
- return anyT.type !== "advisor_20260301" && anyT.type !== "computer_20250124";
691
- });
692
- const enableThinking = isOpenAIThinkingEnabled(openaiModel);
693
- const openAIConvertibleMessages = messagesForAPI.filter(isOpenAIConvertibleMessage);
694
- const messagesWithDeferredToolList = prependDeferredToolListIfNeeded(openAIConvertibleMessages, tools, deferredToolNames, useToolSearch);
695
- const openaiMessages = anthropicMessagesToOpenAI(messagesWithDeferredToolList, systemPrompt, { enableThinking });
696
- diagnosticMessageCount = openaiMessages.length;
697
- const openaiTools = anthropicToolsToOpenAI(standardTools);
698
- diagnosticToolCount = openaiTools.length;
699
- const openaiToolChoice = anthropicToolChoiceToOpenAI(options.toolChoice);
700
- if (useToolSearch) {
701
- const includedDeferredTools = filteredTools.filter((t) => deferredToolNames.has(t.name)).length;
702
- logForDebugging(`[OpenAI] Tool search enabled: ${includedDeferredTools}/${deferredToolNames.size} deferred tools included, total tools=${openaiTools.length}`);
703
- } else {
704
- logForDebugging(`[OpenAI] Tool search disabled, total tools=${openaiTools.length}`);
705
- }
706
- const { upperLimit } = getModelMaxOutputTokens(openaiModel);
707
- const maxTokens = resolveOpenAIMaxTokens(upperLimit, options.maxOutputTokensOverride);
708
- const client = getOpenAIClient({
709
- maxRetries: getOpenAIMaxRetries(),
710
- fetchOverride: options.fetchOverride,
711
- source: options.querySource
712
- });
713
- const debugTimestamp = Date.now();
714
- const debugRequestData = {
715
- model: openaiModel,
716
- systemPrompt_text: systemPrompt.join(`
717
-
718
- `),
719
- openai_messages_preview: openaiMessages.slice(0, 3).map((m) => ({
720
- role: m.role,
721
- content_preview: typeof m.content === "string" ? m.content.substring(0, 500) : JSON.stringify(m.content)?.substring(0, 500)
722
- })),
723
- tools_names: openaiTools.map((t) => t.function?.name ?? t.name)
724
- };
725
- logForDebugging(`[OpenAI] Calling model=${openaiModel}, messages=${openaiMessages.length}, tools=${openaiTools.length}, thinking=${enableThinking}`);
726
- const requestBody = buildOpenAIRequestBody({
727
- model: openaiModel,
728
- messages: openaiMessages,
729
- tools: openaiTools,
730
- toolChoice: openaiToolChoice,
731
- enableThinking,
732
- maxTokens,
733
- temperatureOverride: options.temperatureOverride
734
- });
735
- requestStage = "awaiting_stream";
736
- const stream = await client.chat.completions.create(requestBody, { signal });
737
- requestStage = "streaming";
738
- const adaptedStream = adaptOpenAIStreamToAnthropic(stream, openaiModel);
739
- const contentBlocks = {};
740
- const collectedMessages = [];
741
- let partialMessage;
742
- let stopReason = null;
743
- let usage = {
744
- input_tokens: 0,
745
- output_tokens: 0,
746
- cache_creation_input_tokens: 0,
747
- cache_read_input_tokens: 0
748
- };
749
- let ttftMs = 0;
750
- const start = Date.now();
751
- for await (const event of adaptedStream) {
752
- sawStreamEvent = true;
753
- switch (event.type) {
754
- case "message_start": {
755
- partialMessage = event.message;
756
- ttftMs = Date.now() - start;
757
- if (event.message?.usage) {
758
- usage = {
759
- ...usage,
760
- ...event.message.usage
761
- };
762
- }
763
- break;
764
- }
765
- case "content_block_start": {
766
- const idx = event.index;
767
- const cb = event.content_block;
768
- if (cb.type === "tool_use") {
769
- contentBlocks[idx] = { ...cb, input: "" };
770
- } else if (cb.type === "text") {
771
- contentBlocks[idx] = { ...cb, text: "" };
772
- } else if (cb.type === "thinking") {
773
- contentBlocks[idx] = { ...cb, thinking: "", signature: "" };
774
- } else {
775
- contentBlocks[idx] = { ...cb };
776
- }
777
- break;
778
- }
779
- case "content_block_delta": {
780
- const idx = event.index;
781
- const delta = event.delta;
782
- const block = contentBlocks[idx];
783
- if (!block)
784
- break;
785
- if (delta.type === "text_delta") {
786
- block.text = (block.text || "") + delta.text;
787
- } else if (delta.type === "input_json_delta") {
788
- block.input = (block.input || "") + delta.partial_json;
789
- } else if (delta.type === "thinking_delta") {
790
- block.thinking = (block.thinking || "") + delta.thinking;
791
- } else if (delta.type === "signature_delta") {
792
- block.signature = delta.signature;
793
- }
794
- break;
795
- }
796
- case "content_block_stop": {
797
- break;
798
- }
799
- case "message_delta": {
800
- const deltaUsage = event.usage;
801
- if (deltaUsage) {
802
- usage = { ...usage, ...deltaUsage };
803
- }
804
- if (event.delta?.stop_reason != null) {
805
- stopReason = event.delta.stop_reason;
806
- }
807
- break;
808
- }
809
- case "message_stop": {
810
- if (partialMessage) {
811
- for (const output of assembleFinalAssistantOutputs({
812
- partialMessage,
813
- contentBlocks,
814
- tools,
815
- agentId: options.agentId,
816
- usage,
817
- stopReason,
818
- maxTokens
819
- })) {
820
- if (output.type === "assistant") {
821
- collectedMessages.push(output);
822
- }
823
- yield output;
824
- }
825
- partialMessage = null;
826
- }
827
- if (usage.input_tokens + usage.output_tokens > 0) {
828
- const costUSD = calculateUSDCost(openaiModel, usage);
829
- addToTotalSessionCost(costUSD, usage, options.model);
830
- }
831
- break;
832
- }
833
- }
834
- yield {
835
- type: "stream_event",
836
- event,
837
- ...event.type === "message_start" ? { ttftMs } : undefined
838
- };
839
- }
840
- if (partialMessage) {
841
- for (const output of assembleFinalAssistantOutputs({
842
- partialMessage,
843
- contentBlocks,
844
- tools,
845
- agentId: options.agentId,
846
- usage,
847
- stopReason,
848
- maxTokens
849
- })) {
850
- yield output;
851
- }
852
- }
853
- try {
854
- const { writeFileSync, mkdirSync } = await import("fs");
855
- const { join } = await import("path");
856
- const { tmpdir } = await import("os");
857
- const debugDir = join(tmpdir(), "claude-code-debug");
858
- mkdirSync(debugDir, { recursive: true });
859
- const debugFile = join(debugDir, `openai-${debugTimestamp}.json`);
860
- const allBlocks = Object.keys(contentBlocks).sort((a, b) => Number(a) - Number(b)).map((k) => contentBlocks[Number(k)]).filter(Boolean);
861
- const debugPayload = {
862
- timestamp: new Date(debugTimestamp).toISOString(),
863
- model: openaiModel,
864
- request: debugRequestData,
865
- response: {
866
- stop_reason: stopReason,
867
- usage,
868
- content_blocks: allBlocks.map((b) => {
869
- if (b.type === "text")
870
- return { type: "text", text: b.text };
871
- if (b.type === "thinking")
872
- return { type: "thinking", thinking_preview: b.thinking?.substring(0, 200) };
873
- if (b.type === "tool_use")
874
- return { type: "tool_use", name: b.name, input_preview: typeof b.input === "string" ? b.input.substring(0, 500) : b.input };
875
- return { type: b.type };
876
- }),
877
- duration_ms: Date.now() - start
878
- }
879
- };
880
- writeFileSync(debugFile, JSON.stringify(debugPayload, null, 2), "utf-8");
881
- logForDebugging(`[DEBUG] OpenAI request+response dumped to ${debugFile}`);
882
- } catch (e) {
883
- logForDebugging(`[DEBUG] Failed to dump OpenAI debug file: ${e}`);
884
- }
885
- } catch (error) {
886
- const errorMessage = error instanceof Error ? error.message : String(error);
887
- const errorRecord = error && typeof error === "object" ? error : undefined;
888
- try {
889
- writeToStderr(`[OpenAIMainDiagnostic] ${JSON.stringify({
890
- phase: "error",
891
- querySource: options.querySource,
892
- model: diagnosticModel,
893
- stage: requestStage,
894
- sawStreamEvent,
895
- messageCount: diagnosticMessageCount,
896
- toolCount: diagnosticToolCount,
897
- timeoutMs: getOpenAIRequestTimeoutMs(),
898
- maxRetries: getOpenAIMaxRetries(),
899
- durationMs: Date.now() - requestStartedAt,
900
- errorName: error instanceof Error ? error.name : typeof error,
901
- errorMessage,
902
- status: typeof errorRecord?.status === "number" ? errorRecord.status : undefined,
903
- code: typeof errorRecord?.code === "string" || typeof errorRecord?.code === "number" ? errorRecord.code : undefined,
904
- type: typeof errorRecord?.type === "string" ? errorRecord.type : undefined
905
- })}
906
- `);
907
- } catch {}
908
- logForDebugging(`[OpenAI] Error: ${errorMessage}`, { level: "error" });
909
- yield createAssistantAPIErrorMessage({
910
- content: `API Error: ${errorMessage}`,
911
- apiError: "api_error",
912
- error: error instanceof Error ? error : new Error(String(error))
913
- });
914
- }
915
- }
916
- var init_openai = __esm(() => {
917
- init_client();
918
- init_convertMessages();
919
- init_convertTools();
920
- init_streamAdapter();
921
- init_modelMapping();
922
- init_messages();
923
- init_api();
924
- init_Tool();
925
- init_debug();
926
- init_cost_tracker();
927
- init_modelCost();
928
- init_envUtils();
929
- init_context();
930
- init_process();
931
- init_messages();
932
- init_toolSearch();
933
- init_prompt();
934
- });
935
- init_openai();
936
-
937
- export {
938
- resolveOpenAIMaxTokens,
939
- queryModelOpenAI,
940
- isOpenAIThinkingEnabled,
941
- buildOpenAIRequestBody
942
- };