gemi 0.225.2 → 0.227.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (234) hide show
  1. package/dist/ai/Agent.d.ts +48 -1
  2. package/dist/ai/Agent.d.ts.map +1 -1
  3. package/dist/ai/AgentController.d.ts +161 -18
  4. package/dist/ai/AgentController.d.ts.map +1 -1
  5. package/dist/ai/client/index.d.ts +1 -1
  6. package/dist/ai/client/index.d.ts.map +1 -1
  7. package/dist/ai/client/index.js +235 -58
  8. package/dist/ai/client/index.js.map +1 -1
  9. package/dist/ai/client/reducer.d.ts +46 -0
  10. package/dist/ai/client/reducer.d.ts.map +1 -1
  11. package/dist/ai/contextWindow.d.ts +9 -2
  12. package/dist/ai/contextWindow.d.ts.map +1 -1
  13. package/dist/ai/index.d.ts +3 -2
  14. package/dist/ai/index.d.ts.map +1 -1
  15. package/dist/ai/index.js +80 -70
  16. package/dist/ai/index.js.map +7 -6
  17. package/dist/ai/store/LiveRuns.d.ts +21 -0
  18. package/dist/ai/store/LiveRuns.d.ts.map +1 -1
  19. package/dist/ai/store/index.d.ts +1 -1
  20. package/dist/ai/store/index.d.ts.map +1 -1
  21. package/dist/ai/testing/index.js +2 -2
  22. package/dist/ai/testing/index.js.map +1 -1
  23. package/dist/ai/types.d.ts +14 -0
  24. package/dist/ai/types.d.ts.map +1 -1
  25. package/dist/ai/useChat.d.ts +30 -0
  26. package/dist/ai/useChat.d.ts.map +1 -1
  27. package/dist/app/index.js +1 -1
  28. package/dist/bun/plugin.js +1 -1
  29. package/dist/bun/preload.js +1 -1
  30. package/dist/{chunk-tgy75n24.js → chunk-0dp6jvrg.js} +2 -2
  31. package/dist/{chunk-tgy75n24.js.map → chunk-0dp6jvrg.js.map} +1 -1
  32. package/dist/{chunk-xpzdwhjg.js → chunk-0zakpsep.js} +3 -3
  33. package/dist/{chunk-xpzdwhjg.js.map → chunk-0zakpsep.js.map} +1 -1
  34. package/dist/{chunk-m7v4erdg.js → chunk-0zf4dy4y.js} +2 -2
  35. package/dist/{chunk-m7v4erdg.js.map → chunk-0zf4dy4y.js.map} +1 -1
  36. package/dist/{chunk-40yd1n60.js → chunk-11yrxwkt.js} +2 -2
  37. package/dist/{chunk-40yd1n60.js.map → chunk-11yrxwkt.js.map} +1 -1
  38. package/dist/{chunk-j13jmjkr.js → chunk-1v1ek1ey.js} +2 -2
  39. package/dist/{chunk-j13jmjkr.js.map → chunk-1v1ek1ey.js.map} +1 -1
  40. package/dist/{chunk-ads3c8m0.js → chunk-30jcs49q.js} +2 -2
  41. package/dist/{chunk-ads3c8m0.js.map → chunk-30jcs49q.js.map} +1 -1
  42. package/dist/{chunk-bzrta2sw.js → chunk-30zs008n.js} +3 -3
  43. package/dist/{chunk-bzrta2sw.js.map → chunk-30zs008n.js.map} +1 -1
  44. package/dist/{chunk-z6x78wb3.js → chunk-3q0sbk92.js} +2 -2
  45. package/dist/{chunk-z6x78wb3.js.map → chunk-3q0sbk92.js.map} +1 -1
  46. package/dist/{chunk-421hq1rw.js → chunk-3v8b2kdr.js} +1 -1
  47. package/dist/{chunk-3e55yw7v.js → chunk-5p0gbwq1.js} +3 -3
  48. package/dist/{chunk-3e55yw7v.js.map → chunk-5p0gbwq1.js.map} +1 -1
  49. package/dist/{chunk-3dkaz47e.js → chunk-5sgwnrr8.js} +2 -2
  50. package/dist/{chunk-3dkaz47e.js.map → chunk-5sgwnrr8.js.map} +1 -1
  51. package/dist/{chunk-ng6pbd7f.js → chunk-65wnm25z.js} +4 -4
  52. package/dist/{chunk-ng6pbd7f.js.map → chunk-65wnm25z.js.map} +1 -1
  53. package/dist/{chunk-8pdke84p.js → chunk-69q9tk74.js} +2 -2
  54. package/dist/{chunk-8pdke84p.js.map → chunk-69q9tk74.js.map} +1 -1
  55. package/dist/{chunk-d3a29hyg.js → chunk-6fpjc2eg.js} +3 -3
  56. package/dist/{chunk-d3a29hyg.js.map → chunk-6fpjc2eg.js.map} +1 -1
  57. package/dist/{chunk-wnmpwb4s.js → chunk-6xt7w9bb.js} +3 -3
  58. package/dist/{chunk-wnmpwb4s.js.map → chunk-6xt7w9bb.js.map} +1 -1
  59. package/dist/{chunk-t9fh0ptd.js → chunk-76sp9knq.js} +2 -2
  60. package/dist/{chunk-t9fh0ptd.js.map → chunk-76sp9knq.js.map} +1 -1
  61. package/dist/{chunk-mvb8m247.js → chunk-7g3r55sj.js} +2 -2
  62. package/dist/{chunk-mvb8m247.js.map → chunk-7g3r55sj.js.map} +1 -1
  63. package/dist/{chunk-enqrjcdk.js → chunk-7ka1abkd.js} +2 -2
  64. package/dist/{chunk-enqrjcdk.js.map → chunk-7ka1abkd.js.map} +1 -1
  65. package/dist/{chunk-tbvrv4h2.js → chunk-7vkqcrfn.js} +2 -2
  66. package/dist/{chunk-tbvrv4h2.js.map → chunk-7vkqcrfn.js.map} +1 -1
  67. package/dist/{chunk-zy56kaws.js → chunk-7y2g8wbb.js} +2 -2
  68. package/dist/{chunk-zy56kaws.js.map → chunk-7y2g8wbb.js.map} +1 -1
  69. package/dist/{chunk-bwjaq694.js → chunk-8hr7z8cc.js} +3 -3
  70. package/dist/{chunk-bwjaq694.js.map → chunk-8hr7z8cc.js.map} +1 -1
  71. package/dist/{chunk-7d1f5gyv.js → chunk-8q1n4087.js} +2 -2
  72. package/dist/{chunk-7d1f5gyv.js.map → chunk-8q1n4087.js.map} +1 -1
  73. package/dist/{chunk-j7pzdvxr.js → chunk-9bjggtdt.js} +2 -2
  74. package/dist/{chunk-j7pzdvxr.js.map → chunk-9bjggtdt.js.map} +1 -1
  75. package/dist/{chunk-9znec0we.js → chunk-9jz5ts7h.js} +24 -24
  76. package/dist/chunk-9jz5ts7h.js.map +27 -0
  77. package/dist/{chunk-pdymdjpq.js → chunk-a2y481gh.js} +2 -2
  78. package/dist/{chunk-pdymdjpq.js.map → chunk-a2y481gh.js.map} +1 -1
  79. package/dist/{chunk-me70vszk.js → chunk-agc0e46c.js} +2 -2
  80. package/dist/{chunk-me70vszk.js.map → chunk-agc0e46c.js.map} +1 -1
  81. package/dist/{chunk-7mvfk0q9.js → chunk-b9gwjs4r.js} +2 -2
  82. package/dist/{chunk-7mvfk0q9.js.map → chunk-b9gwjs4r.js.map} +1 -1
  83. package/dist/{chunk-svrvb47q.js → chunk-bm4hjbbk.js} +2 -2
  84. package/dist/{chunk-svrvb47q.js.map → chunk-bm4hjbbk.js.map} +1 -1
  85. package/dist/{chunk-rq0dpv7r.js → chunk-bs8bq053.js} +2 -2
  86. package/dist/{chunk-rq0dpv7r.js.map → chunk-bs8bq053.js.map} +1 -1
  87. package/dist/{chunk-bch7xbsr.js → chunk-c3jg93sn.js} +2 -2
  88. package/dist/{chunk-bch7xbsr.js.map → chunk-c3jg93sn.js.map} +1 -1
  89. package/dist/{chunk-mca5s0bf.js → chunk-c5kd7eac.js} +3 -3
  90. package/dist/{chunk-mca5s0bf.js.map → chunk-c5kd7eac.js.map} +1 -1
  91. package/dist/chunk-cjj9cryr.js +10 -0
  92. package/dist/{chunk-v0fqq5z7.js.map → chunk-cjj9cryr.js.map} +4 -4
  93. package/dist/{chunk-v4ecxbgd.js → chunk-ct8eh241.js} +2 -2
  94. package/dist/{chunk-v4ecxbgd.js.map → chunk-ct8eh241.js.map} +1 -1
  95. package/dist/{chunk-6h1hjzj7.js → chunk-djhr379j.js} +2 -2
  96. package/dist/{chunk-6h1hjzj7.js.map → chunk-djhr379j.js.map} +1 -1
  97. package/dist/{chunk-we65r9fh.js → chunk-dpn78sq7.js} +3 -3
  98. package/dist/{chunk-we65r9fh.js.map → chunk-dpn78sq7.js.map} +1 -1
  99. package/dist/{chunk-awryyak8.js → chunk-dqb257xv.js} +2 -2
  100. package/dist/{chunk-awryyak8.js.map → chunk-dqb257xv.js.map} +1 -1
  101. package/dist/{chunk-wzr8tzv0.js → chunk-e30saw5t.js} +2 -2
  102. package/dist/{chunk-wzr8tzv0.js.map → chunk-e30saw5t.js.map} +1 -1
  103. package/dist/{chunk-64yf0shq.js → chunk-efnerp0c.js} +2 -2
  104. package/dist/{chunk-64yf0shq.js.map → chunk-efnerp0c.js.map} +1 -1
  105. package/dist/{chunk-z5am43fj.js → chunk-ehjae5fm.js} +2 -2
  106. package/dist/{chunk-z5am43fj.js.map → chunk-ehjae5fm.js.map} +1 -1
  107. package/dist/{chunk-81y96dax.js → chunk-ejvamy3q.js} +1 -1
  108. package/dist/{chunk-v5sg62xm.js → chunk-g0mxdh2x.js} +2 -2
  109. package/dist/{chunk-v5sg62xm.js.map → chunk-g0mxdh2x.js.map} +1 -1
  110. package/dist/{chunk-70jhw0ft.js → chunk-g0t49jec.js} +1 -1
  111. package/dist/{chunk-tp1gxcke.js → chunk-gd4qydsx.js} +3 -3
  112. package/dist/{chunk-tp1gxcke.js.map → chunk-gd4qydsx.js.map} +1 -1
  113. package/dist/{chunk-eydww2nn.js → chunk-j5fh6pjs.js} +2 -2
  114. package/dist/{chunk-eydww2nn.js.map → chunk-j5fh6pjs.js.map} +1 -1
  115. package/dist/{chunk-4qyp7w89.js → chunk-j7swq19b.js} +1 -1
  116. package/dist/{chunk-5kgqfczk.js → chunk-jkhtybga.js} +3 -3
  117. package/dist/{chunk-5kgqfczk.js.map → chunk-jkhtybga.js.map} +1 -1
  118. package/dist/{chunk-ja3n968b.js → chunk-jqqyj6cr.js} +2 -2
  119. package/dist/{chunk-ja3n968b.js.map → chunk-jqqyj6cr.js.map} +1 -1
  120. package/dist/{chunk-6et7jwp8.js → chunk-jrvqya5h.js} +2 -2
  121. package/dist/{chunk-6et7jwp8.js.map → chunk-jrvqya5h.js.map} +1 -1
  122. package/dist/{chunk-8szsd6vy.js → chunk-keqjdy8q.js} +2 -2
  123. package/dist/{chunk-8szsd6vy.js.map → chunk-keqjdy8q.js.map} +1 -1
  124. package/dist/{chunk-qx9p15em.js → chunk-kfcz4qa8.js} +2 -2
  125. package/dist/{chunk-qx9p15em.js.map → chunk-kfcz4qa8.js.map} +1 -1
  126. package/dist/{chunk-7901z26g.js → chunk-kspqqyv1.js} +2 -2
  127. package/dist/{chunk-7901z26g.js.map → chunk-kspqqyv1.js.map} +1 -1
  128. package/dist/{chunk-qrc2r0ce.js → chunk-kx5gycdv.js} +3 -3
  129. package/dist/{chunk-qrc2r0ce.js.map → chunk-kx5gycdv.js.map} +1 -1
  130. package/dist/{chunk-kxc2mzzr.js → chunk-m4wagwhr.js} +2 -2
  131. package/dist/{chunk-kxc2mzzr.js.map → chunk-m4wagwhr.js.map} +1 -1
  132. package/dist/{chunk-fjnhqbtx.js → chunk-mcgcgm6g.js} +1 -1
  133. package/dist/{chunk-ryh6rfts.js → chunk-mg9j9r9f.js} +2 -2
  134. package/dist/{chunk-ryh6rfts.js.map → chunk-mg9j9r9f.js.map} +1 -1
  135. package/dist/{chunk-crk4ddqa.js → chunk-mgvrfhy8.js} +3 -3
  136. package/dist/{chunk-crk4ddqa.js.map → chunk-mgvrfhy8.js.map} +1 -1
  137. package/dist/{chunk-qtz4g4kv.js → chunk-ngbrbeg2.js} +3 -3
  138. package/dist/{chunk-qtz4g4kv.js.map → chunk-ngbrbeg2.js.map} +1 -1
  139. package/dist/{chunk-ftc9292d.js → chunk-ph7cjs7e.js} +2 -2
  140. package/dist/{chunk-ftc9292d.js.map → chunk-ph7cjs7e.js.map} +1 -1
  141. package/dist/{chunk-gj0bwm41.js → chunk-q78n7dvm.js} +2 -2
  142. package/dist/{chunk-gj0bwm41.js.map → chunk-q78n7dvm.js.map} +1 -1
  143. package/dist/{chunk-mvp6fkg2.js → chunk-qgka1mvf.js} +3 -3
  144. package/dist/{chunk-mvp6fkg2.js.map → chunk-qgka1mvf.js.map} +1 -1
  145. package/dist/{chunk-w2yd62vs.js → chunk-qjd7pk3n.js} +2 -2
  146. package/dist/{chunk-w2yd62vs.js.map → chunk-qjd7pk3n.js.map} +1 -1
  147. package/dist/{chunk-9xxgj5zk.js → chunk-qqe19gtm.js} +3 -3
  148. package/dist/{chunk-9xxgj5zk.js.map → chunk-qqe19gtm.js.map} +1 -1
  149. package/dist/chunk-rqzpxeb5.js +5 -0
  150. package/dist/{chunk-qjn6a2ed.js.map → chunk-rqzpxeb5.js.map} +1 -1
  151. package/dist/{chunk-pn3egz75.js → chunk-tegjs0gg.js} +1 -1
  152. package/dist/{chunk-3vgjrn2r.js → chunk-v5b883zc.js} +2 -2
  153. package/dist/{chunk-3vgjrn2r.js.map → chunk-v5b883zc.js.map} +1 -1
  154. package/dist/{chunk-v979k0be.js → chunk-vjqpcet2.js} +3 -3
  155. package/dist/{chunk-v979k0be.js.map → chunk-vjqpcet2.js.map} +1 -1
  156. package/dist/{chunk-x93dpv6e.js → chunk-vnpfdr52.js} +2 -2
  157. package/dist/{chunk-x93dpv6e.js.map → chunk-vnpfdr52.js.map} +1 -1
  158. package/dist/{chunk-zvg2qsa5.js → chunk-vrtt8nvm.js} +2 -2
  159. package/dist/{chunk-zvg2qsa5.js.map → chunk-vrtt8nvm.js.map} +1 -1
  160. package/dist/{chunk-t5aft2ra.js → chunk-vsh9jck0.js} +26 -26
  161. package/dist/{chunk-t5aft2ra.js.map → chunk-vsh9jck0.js.map} +5 -5
  162. package/dist/{chunk-21hhvphe.js → chunk-wedj46ec.js} +2 -2
  163. package/dist/{chunk-21hhvphe.js.map → chunk-wedj46ec.js.map} +1 -1
  164. package/dist/{chunk-kzz2nfzf.js → chunk-wgxkr2jx.js} +2 -2
  165. package/dist/{chunk-kzz2nfzf.js.map → chunk-wgxkr2jx.js.map} +1 -1
  166. package/dist/{chunk-z4fhhtjh.js → chunk-x1p31k7q.js} +2 -2
  167. package/dist/{chunk-z4fhhtjh.js.map → chunk-x1p31k7q.js.map} +1 -1
  168. package/dist/{chunk-hvyzp9p6.js → chunk-x8by5j7p.js} +2 -2
  169. package/dist/{chunk-hvyzp9p6.js.map → chunk-x8by5j7p.js.map} +1 -1
  170. package/dist/{chunk-j0rhz378.js → chunk-xd4kvswv.js} +2 -2
  171. package/dist/{chunk-j0rhz378.js.map → chunk-xd4kvswv.js.map} +1 -1
  172. package/dist/{chunk-k657v3ed.js → chunk-xkrfxcm7.js} +3 -3
  173. package/dist/{chunk-k657v3ed.js.map → chunk-xkrfxcm7.js.map} +1 -1
  174. package/dist/{chunk-x47kwv2x.js → chunk-xmrd9460.js} +2 -2
  175. package/dist/{chunk-x47kwv2x.js.map → chunk-xmrd9460.js.map} +1 -1
  176. package/dist/{chunk-n2c748aw.js → chunk-xprhym1t.js} +2 -2
  177. package/dist/{chunk-n2c748aw.js.map → chunk-xprhym1t.js.map} +1 -1
  178. package/dist/{chunk-ehr5pnm8.js → chunk-xstbj0wa.js} +2 -2
  179. package/dist/{chunk-ehr5pnm8.js.map → chunk-xstbj0wa.js.map} +1 -1
  180. package/dist/{chunk-1jyzg1ex.js → chunk-xv46emqt.js} +3 -3
  181. package/dist/{chunk-1jyzg1ex.js.map → chunk-xv46emqt.js.map} +1 -1
  182. package/dist/{chunk-2450kxym.js → chunk-znh1mbqs.js} +2 -2
  183. package/dist/{chunk-2450kxym.js.map → chunk-znh1mbqs.js.map} +1 -1
  184. package/dist/config/index.js +1 -1
  185. package/dist/console/run.js +2 -2
  186. package/dist/console/run.js.map +1 -1
  187. package/dist/container/index.js +2 -2
  188. package/dist/container/index.js.map +1 -1
  189. package/dist/database/index.js +2 -2
  190. package/dist/database/index.js.map +1 -1
  191. package/dist/email/index.js +2 -2
  192. package/dist/email/index.js.map +1 -1
  193. package/dist/facades/index.js +2 -2
  194. package/dist/facades/index.js.map +1 -1
  195. package/dist/foundation/index.js +2 -2
  196. package/dist/foundation/index.js.map +1 -1
  197. package/dist/http/RateLimitMiddleware.d.ts +23 -6
  198. package/dist/http/RateLimitMiddleware.d.ts.map +1 -1
  199. package/dist/http/clientAddress.d.ts +13 -0
  200. package/dist/http/clientAddress.d.ts.map +1 -1
  201. package/dist/http/index.d.ts +1 -1
  202. package/dist/http/index.d.ts.map +1 -1
  203. package/dist/http/index.js +2 -2
  204. package/dist/http/index.js.map +1 -1
  205. package/dist/i18n/dictionaryRuntime.js +2 -2
  206. package/dist/i18n/dictionaryRuntime.js.map +1 -1
  207. package/dist/i18n/index.js +2 -2
  208. package/dist/i18n/index.js.map +1 -1
  209. package/dist/kernel/index.js +2 -2
  210. package/dist/kernel/index.js.map +1 -1
  211. package/dist/orm/index.js +2 -2
  212. package/dist/orm/index.js.map +1 -1
  213. package/dist/probe/index.js +2 -2
  214. package/dist/probe/index.js.map +1 -1
  215. package/dist/server/index.js +2 -2
  216. package/dist/server/index.js.map +1 -1
  217. package/dist/services/index.js +2 -2
  218. package/dist/services/index.js.map +3 -3
  219. package/dist/services/queue/work.js +2 -2
  220. package/dist/services/queue/work.js.map +1 -1
  221. package/dist/stats/route-table.js +1 -1
  222. package/dist/support/index.js +2 -2
  223. package/dist/support/index.js.map +1 -1
  224. package/dist/vitest/index.js +1 -1
  225. package/package.json +1 -1
  226. package/dist/chunk-9znec0we.js.map +0 -27
  227. package/dist/chunk-qjn6a2ed.js +0 -5
  228. package/dist/chunk-v0fqq5z7.js +0 -10
  229. /package/dist/{chunk-421hq1rw.js.map → chunk-3v8b2kdr.js.map} +0 -0
  230. /package/dist/{chunk-81y96dax.js.map → chunk-ejvamy3q.js.map} +0 -0
  231. /package/dist/{chunk-70jhw0ft.js.map → chunk-g0t49jec.js.map} +0 -0
  232. /package/dist/{chunk-4qyp7w89.js.map → chunk-j7swq19b.js.map} +0 -0
  233. /package/dist/{chunk-fjnhqbtx.js.map → chunk-mcgcgm6g.js.map} +0 -0
  234. /package/dist/{chunk-pn3egz75.js.map → chunk-tegjs0gg.js.map} +0 -0
@@ -1,6 +1,8 @@
1
1
  import { t as useParams } from "../../chunks/useParams-BN3XXfmG.js";
2
2
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
3
  //#region ai/client/reducer.ts
4
+ /** How many ended runs' lanes are kept for their cursors. */
5
+ var MAX_LANES = 16;
4
6
  function initialChatState(init = {}) {
5
7
  return {
6
8
  messages: init.messages ? [...init.messages] : [],
@@ -47,6 +49,138 @@ function applyFrame(state, frame, now = (/* @__PURE__ */ new Date()).toISOString
47
49
  seq: frame.seq
48
50
  };
49
51
  }
52
+ /**
53
+ * Apply one frame of the run `runId`, when several runs stream into one state
54
+ * at once: parallel turns on a thread, or a reply joined while it
55
+ * streams on two connections.
56
+ *
57
+ * `seq` and the run-scoped fields are per run, so each run is applied against
58
+ * its own: the primary run's are the state's own fields, every other run's a
59
+ * lane (`ChatState.lanes`). The transcript, `pending` and `error` are shared.
60
+ * A frame of a run the state has not seen starts a lane, or takes the primary
61
+ * place when the primary run is over. When the primary run ends and another is
62
+ * still going, that one becomes the primary. `runId` undefined (a stream that
63
+ * has not said which run it is) is the primary's, as `applyFrame` is.
64
+ *
65
+ * Which run a stream carries is learned from its `run-start`, so a caller
66
+ * applying a stream's frames passes that run's id from then on; a frame
67
+ * before it goes to the primary. Every gemi stream (a turn, an attach, a
68
+ * joined reply) opens with `run-start`.
69
+ */
70
+ function applyRunFrame(state, runId, frame, now = (/* @__PURE__ */ new Date()).toISOString()) {
71
+ if (runId === void 0 || runId === state.cursorRunId) {
72
+ const next = applyFrame(state, frame, now);
73
+ return next === state ? state : promote(next);
74
+ }
75
+ const lanes = state.lanes ?? {};
76
+ const lane = lanes[runId];
77
+ if (!lane && state.runId === void 0) {
78
+ const kept = state.cursorRunId ? keepLanes({
79
+ ...lanes,
80
+ [state.cursorRunId]: laneOf(state)
81
+ }) : lanes;
82
+ return promote(applyFrame({
83
+ ...state,
84
+ ...freshLane(runId),
85
+ lanes: kept
86
+ }, frame, now));
87
+ }
88
+ const own = lane ?? freshLane(runId);
89
+ const swapped = {
90
+ ...state,
91
+ ...own
92
+ };
93
+ const next = applyFrame(swapped, frame, now);
94
+ if (next === swapped) return state;
95
+ return promote({
96
+ ...next,
97
+ ...laneOf(state),
98
+ lanes: {
99
+ ...lanes,
100
+ [runId]: laneOf(next)
101
+ }
102
+ });
103
+ }
104
+ /**
105
+ * `markAborted` over every run the state is following: what `stop()` does
106
+ * when more than one is streaming.
107
+ */
108
+ function markAllAborted(state) {
109
+ let next = markAborted(state);
110
+ const lanes = { ...state.lanes };
111
+ for (const [runId, lane] of Object.entries(lanes)) {
112
+ if (lane.runId === void 0) continue;
113
+ const aborted = markAborted({
114
+ ...next,
115
+ ...lane
116
+ });
117
+ lanes[runId] = laneOf(aborted);
118
+ next = {
119
+ ...next,
120
+ messages: aborted.messages
121
+ };
122
+ }
123
+ return {
124
+ ...next,
125
+ lanes
126
+ };
127
+ }
128
+ /** The ids of the runs the state is following that are still going, the primary first. */
129
+ function liveRunIds(state) {
130
+ const ids = state.runId ? [state.runId] : [];
131
+ for (const lane of Object.values(state.lanes ?? {})) if (lane.runId !== void 0 && !ids.includes(lane.runId)) ids.push(lane.runId);
132
+ return ids;
133
+ }
134
+ /** The run's own position, if this state has followed it: what `/attach` is asked to resume from. */
135
+ function runCursor(state, runId) {
136
+ if (state.cursorRunId === runId) return state.seq;
137
+ return state.lanes?.[runId]?.seq;
138
+ }
139
+ function laneOf(state) {
140
+ return {
141
+ runId: state.runId,
142
+ seq: state.seq,
143
+ cursorRunId: state.cursorRunId,
144
+ runMessageIds: state.runMessageIds,
145
+ loadedTools: state.loadedTools,
146
+ finishReason: state.finishReason,
147
+ usage: state.usage
148
+ };
149
+ }
150
+ function freshLane(runId) {
151
+ return {
152
+ runId,
153
+ seq: -1,
154
+ cursorRunId: runId,
155
+ runMessageIds: [],
156
+ loadedTools: [],
157
+ finishReason: void 0,
158
+ usage: void 0
159
+ };
160
+ }
161
+ /** The primary run ended while another is going: that one becomes the primary. */
162
+ function promote(state) {
163
+ if (state.runId !== void 0 || !state.lanes) return state;
164
+ const live = Object.entries(state.lanes).find(([, lane]) => lane.runId !== void 0);
165
+ if (!live) return state;
166
+ const [runId, lane] = live;
167
+ const lanes = { ...state.lanes };
168
+ delete lanes[runId];
169
+ if (state.cursorRunId) lanes[state.cursorRunId] = laneOf(state);
170
+ return {
171
+ ...state,
172
+ ...lane,
173
+ lanes: keepLanes(lanes)
174
+ };
175
+ }
176
+ /** At most `MAX_LANES` ended lanes, the oldest dropped first; live ones are all kept. */
177
+ function keepLanes(lanes) {
178
+ const entries = Object.entries(lanes);
179
+ const ended = entries.filter(([, lane]) => lane.runId === void 0);
180
+ if (ended.length <= MAX_LANES) return lanes;
181
+ const drop = new Set(ended.slice(0, ended.length - MAX_LANES).map(([id]) => id));
182
+ return Object.fromEntries(entries.filter(([id]) => !drop.has(id)));
183
+ }
50
184
  function reduce(state, event, now) {
51
185
  switch (event.type) {
52
186
  case "run-start": return {
@@ -917,7 +1051,7 @@ var WATCH_RETRY_MAX_MS = 3e4;
917
1051
  * first boundary.
918
1052
  */
919
1053
  function useChat(path, params = {}) {
920
- const { threadId: initialThreadId, initialMessages, initialPending, cursor: initialCursor, attach: attachOnMount = true, reattachOnFocus = false, watch = false, body: extraBody, headers, onFinish, onToolResult, onToolProgress, onError, onAwaitingInput, onAttachMiss, onRetry } = params;
1054
+ const { threadId: initialThreadId, initialMessages, initialPending, cursor: initialCursor, attach: attachOnMount = true, reattachOnFocus = false, watch = false, ifBusy: hookIfBusy, body: extraBody, headers, onFinish, onToolResult, onToolProgress, onError, onAwaitingInput, onAttachMiss, onRetry } = params;
921
1055
  const routeParams = useParams();
922
1056
  const base = useMemo(() => `/api${applyRouteParams(String(path).replace(/^[A-Z]+:/, ""), routeParams)}`, [path, routeParams]);
923
1057
  /**
@@ -945,7 +1079,8 @@ function useChat(path, params = {}) {
945
1079
  const [state, setState] = useState(stateRef.current);
946
1080
  const [phase, setPhase] = useState("idle");
947
1081
  const mountedRef = useRef(true);
948
- const abortRef = useRef(null);
1082
+ const flightsRef = useRef(/* @__PURE__ */ new Set());
1083
+ const phaseRef = useRef("idle");
949
1084
  const handlers = useRef({
950
1085
  onFinish,
951
1086
  onToolResult,
@@ -967,19 +1102,21 @@ function useChat(path, params = {}) {
967
1102
  const requestRef = useRef({
968
1103
  base,
969
1104
  headers,
970
- extraBody
1105
+ extraBody,
1106
+ ifBusy: hookIfBusy
971
1107
  });
972
1108
  requestRef.current = {
973
1109
  base,
974
1110
  headers,
975
- extraBody
1111
+ extraBody,
1112
+ ifBusy: hookIfBusy
976
1113
  };
977
1114
  useEffect(() => {
978
1115
  mountedRef.current = true;
979
1116
  return () => {
980
1117
  mountedRef.current = false;
981
- abortRef.current?.controller.abort();
982
- abortRef.current = null;
1118
+ for (const flight of flightsRef.current) flight.controller.abort();
1119
+ flightsRef.current.clear();
983
1120
  };
984
1121
  }, []);
985
1122
  const commit = useCallback((next) => {
@@ -987,8 +1124,14 @@ function useChat(path, params = {}) {
987
1124
  if (mountedRef.current) setState(next);
988
1125
  }, []);
989
1126
  const setPhaseSafe = useCallback((next) => {
1127
+ phaseRef.current = next;
990
1128
  if (mountedRef.current) setPhase(next);
991
1129
  }, []);
1130
+ /** Ends a request: idle once none is left. */
1131
+ const land = useCallback((flight) => {
1132
+ if (!flightsRef.current.delete(flight)) return;
1133
+ if (flightsRef.current.size === 0) setPhaseSafe("idle");
1134
+ }, [setPhaseSafe]);
992
1135
  /**
993
1136
  * Report a failure without touching the conversation.
994
1137
  *
@@ -1044,12 +1187,14 @@ function useChat(path, params = {}) {
1044
1187
  });
1045
1188
  }
1046
1189
  }, [post]);
1047
- const consume = useCallback(async (response, signal) => {
1190
+ const consume = useCallback(async (response, flight) => {
1191
+ const signal = flight.controller.signal;
1048
1192
  for await (const frame of decodeSSE(response.body)) {
1049
1193
  if (!mountedRef.current || signal.aborted) return;
1194
+ if (frame.event.type === "run-start") flight.runId = frame.event.runId;
1050
1195
  setPhaseSafe("streaming");
1051
1196
  const previous = stateRef.current;
1052
- const next = applyFrame(previous, frame);
1197
+ const next = applyRunFrame(previous, flight.runId ?? flight.asked, frame);
1053
1198
  if (next === previous) continue;
1054
1199
  commit(next);
1055
1200
  const event = frame.event;
@@ -1076,28 +1221,36 @@ function useChat(path, params = {}) {
1076
1221
  }
1077
1222
  }, [commit, setPhaseSafe]);
1078
1223
  const send = useCallback(async (turn, options = {}) => {
1079
- const { base: url, extraBody } = requestRef.current;
1224
+ const { base: url, extraBody, ifBusy: defaultIfBusy } = requestRef.current;
1225
+ const ifBusy = stateRef.current.threadId ? options.ifBusy ?? defaultIfBusy : void 0;
1226
+ const cuts = ifBusy === void 0 || ifBusy === "supersede";
1080
1227
  const body = options.body ? {
1081
1228
  ...extraBody,
1082
1229
  ...options.body
1083
1230
  } : extraBody;
1084
- const superseded = abortRef.current;
1085
- superseded?.controller.abort();
1231
+ const superseded = cuts ? [...flightsRef.current] : [];
1232
+ for (const flight of superseded) {
1233
+ flight.controller.abort();
1234
+ flightsRef.current.delete(flight);
1235
+ }
1086
1236
  const clientRunId = localId();
1087
1237
  const controller = new AbortController();
1088
- abortRef.current = {
1238
+ const flight = {
1089
1239
  controller,
1090
1240
  clientRunId
1091
1241
  };
1092
- if (superseded) {
1093
- const { runId, threadId } = stateRef.current;
1242
+ const wasBusy = flightsRef.current.size > 0;
1243
+ flightsRef.current.add(flight);
1244
+ for (const cut of superseded) {
1245
+ const { threadId } = stateRef.current;
1246
+ const runId = cut.runId ?? (superseded.length === 1 ? stateRef.current.runId : void 0);
1094
1247
  const body = {
1095
1248
  ...runId ? { runId } : {},
1096
- ...superseded.clientRunId ? { clientRunId: superseded.clientRunId } : {}
1249
+ ...cut.clientRunId ? { clientRunId: cut.clientRunId } : {}
1097
1250
  };
1098
1251
  if (Object.keys(body).length > 0) postStop(body, threadId ? () => {} : (error) => handlers.current.onError?.(error));
1099
1252
  }
1100
- const previous = superseded ? markAborted(stateRef.current) : stateRef.current;
1253
+ const previous = superseded.length > 0 ? markAllAborted(stateRef.current) : stateRef.current;
1101
1254
  const history = previous.messages;
1102
1255
  const ownId = turn.localId || localId();
1103
1256
  const authored = turn.text || turn.files?.length ? [{
@@ -1117,7 +1270,7 @@ function useChat(path, params = {}) {
1117
1270
  const answered = turn.toolResults?.length ? new Set(previous.pending.map((call) => call.toolCallId)) : null;
1118
1271
  let settled = false;
1119
1272
  const settle = (taken) => {
1120
- if (!answered || settled || abortRef.current?.controller !== controller) return;
1273
+ if (!answered || settled || !flightsRef.current.has(flight)) return;
1121
1274
  settled = true;
1122
1275
  const current = stateRef.current;
1123
1276
  commit({
@@ -1131,7 +1284,7 @@ function useChat(path, params = {}) {
1131
1284
  error: null,
1132
1285
  pending: answered ? withAnswering(previous.pending, answered, true) : []
1133
1286
  });
1134
- setPhaseSafe("submitted");
1287
+ if (!wasBusy || phaseRef.current !== "streaming") setPhaseSafe("submitted");
1135
1288
  try {
1136
1289
  const payload = {
1137
1290
  ...body,
@@ -1142,7 +1295,8 @@ function useChat(path, params = {}) {
1142
1295
  clientRunId,
1143
1296
  threadId: stateRef.current.threadId,
1144
1297
  messages: stateRef.current.threadId ? void 0 : forWire(history),
1145
- ...options.regenerate && stateRef.current.threadId ? { regenerate: true } : {}
1298
+ ...options.regenerate && stateRef.current.threadId ? { regenerate: true } : {},
1299
+ ...ifBusy !== void 0 ? { ifBusy } : {}
1146
1300
  };
1147
1301
  const response = await post(url, payload, controller.signal);
1148
1302
  if (!response.ok) {
@@ -1152,7 +1306,7 @@ function useChat(path, params = {}) {
1152
1306
  return;
1153
1307
  }
1154
1308
  settle(true);
1155
- await consume(response, controller.signal);
1309
+ await consume(response, flight);
1156
1310
  } catch (error) {
1157
1311
  if (!isAbort(error)) {
1158
1312
  settle(false);
@@ -1163,22 +1317,21 @@ function useChat(path, params = {}) {
1163
1317
  });
1164
1318
  }
1165
1319
  } finally {
1166
- if (abortRef.current?.controller === controller) {
1167
- abortRef.current = null;
1168
- setPhaseSafe("idle");
1169
- }
1320
+ land(flight);
1170
1321
  }
1171
1322
  }, [
1172
1323
  commit,
1173
1324
  consume,
1174
1325
  fail,
1326
+ land,
1175
1327
  post,
1176
1328
  postStop,
1177
1329
  setPhaseSafe
1178
1330
  ]);
1179
1331
  const sendMessage = useCallback((turn, options = {}) => send(typeof turn === "string" ? { text: turn } : turn, {
1180
1332
  ...options.body ? { body: options.body } : {},
1181
- ...options.metadata ? { metadata: options.metadata } : {}
1333
+ ...options.metadata ? { metadata: options.metadata } : {},
1334
+ ...options.ifBusy ? { ifBusy: options.ifBusy } : {}
1182
1335
  }), [send]);
1183
1336
  /**
1184
1337
  * Answers queued within one tick become one turn.
@@ -1311,22 +1464,34 @@ function useChat(path, params = {}) {
1311
1464
  }, [submit]);
1312
1465
  const stop = useCallback(async () => {
1313
1466
  const { runId, threadId } = stateRef.current;
1314
- const inFlight = abortRef.current;
1315
- inFlight?.controller.abort();
1316
- abortRef.current = null;
1317
- const stopped = markAborted(stateRef.current);
1467
+ const live = liveRunIds(stateRef.current);
1468
+ const flights = [...flightsRef.current];
1469
+ for (const flight of flights) flight.controller.abort();
1470
+ flightsRef.current.clear();
1471
+ const stopped = markAllAborted(stateRef.current);
1318
1472
  commit({
1319
1473
  ...stopped,
1320
1474
  pending: withAnswering(stopped.pending, new Set(stopped.pending.map((call) => call.toolCallId)), false)
1321
1475
  });
1322
1476
  setPhaseSafe("idle");
1323
- if (!inFlight && !runId && !threadId) return;
1324
- const body = {
1325
- ...runId ? { runId } : {},
1326
- ...threadId ? { threadId } : {},
1327
- ...inFlight?.clientRunId ? { clientRunId: inFlight.clientRunId } : {}
1328
- };
1329
- await postStop(body, fail);
1477
+ if (flights.length === 0 && !runId && !threadId) return;
1478
+ const bodies = [];
1479
+ const named = /* @__PURE__ */ new Set();
1480
+ for (const flight of flights) {
1481
+ const id = flight.runId ?? (flights.length === 1 ? runId : void 0);
1482
+ if (id) named.add(id);
1483
+ bodies.push({
1484
+ ...id ? { runId: id } : {},
1485
+ ...threadId ? { threadId } : {},
1486
+ ...flight.clientRunId ? { clientRunId: flight.clientRunId } : {}
1487
+ });
1488
+ }
1489
+ for (const id of live) if (!named.has(id)) bodies.push({
1490
+ runId: id,
1491
+ ...threadId ? { threadId } : {}
1492
+ });
1493
+ if (bodies.length === 0) bodies.push(threadId ? { threadId } : {});
1494
+ await Promise.all(bodies.map((body) => postStop(body, fail)));
1330
1495
  }, [
1331
1496
  commit,
1332
1497
  fail,
@@ -1397,6 +1562,8 @@ function useChat(path, params = {}) {
1397
1562
  downgraded: data.downgraded
1398
1563
  };
1399
1564
  }, []);
1565
+ /** Whether a request in flight is streaming this run. */
1566
+ const following = useCallback((runId) => [...flightsRef.current].some((flight) => (flight.runId ?? flight.asked) === runId), []);
1400
1567
  /**
1401
1568
  * Ask `/attach` whether the thread has a run going, and stream it if it does.
1402
1569
  *
@@ -1407,11 +1574,19 @@ function useChat(path, params = {}) {
1407
1574
  * which only the automatic probes do. The phase is left alone until a frame
1408
1575
  * arrives, so a probe that finds nothing never shows as busy.
1409
1576
  */
1410
- const probe = useCallback(async (missed) => {
1411
- const { threadId, seq, cursorRunId } = stateRef.current;
1412
- if (!threadId || abortRef.current) return false;
1577
+ const probe = useCallback(async (missed, target) => {
1578
+ const state = stateRef.current;
1579
+ const { threadId } = state;
1580
+ if (!threadId) return false;
1581
+ if (target === void 0 ? flightsRef.current.size > 0 : following(target)) return false;
1582
+ const cursorRunId = target ?? state.cursorRunId;
1583
+ const seq = target === void 0 ? state.seq : runCursor(state, target) ?? -1;
1413
1584
  const controller = new AbortController();
1414
- abortRef.current = { controller };
1585
+ const flight = {
1586
+ controller,
1587
+ ...cursorRunId ? { asked: cursorRunId } : {}
1588
+ };
1589
+ flightsRef.current.add(flight);
1415
1590
  try {
1416
1591
  const body = {
1417
1592
  threadId,
@@ -1424,20 +1599,18 @@ function useChat(path, params = {}) {
1424
1599
  if (missed) handlers.current.onAttachMiss?.({ threadId });
1425
1600
  return false;
1426
1601
  }
1427
- await consume(response, controller.signal);
1602
+ await consume(response, flight);
1428
1603
  return true;
1429
1604
  } catch {
1430
1605
  return false;
1431
1606
  } finally {
1432
- if (abortRef.current?.controller === controller) {
1433
- abortRef.current = null;
1434
- setPhaseSafe("idle");
1435
- }
1607
+ land(flight);
1436
1608
  }
1437
1609
  }, [
1438
1610
  consume,
1439
- post,
1440
- setPhaseSafe
1611
+ following,
1612
+ land,
1613
+ post
1441
1614
  ]);
1442
1615
  const reattach = useCallback(() => probe(false), [probe]);
1443
1616
  useEffect(() => {
@@ -1461,26 +1634,29 @@ function useChat(path, params = {}) {
1461
1634
  useEffect(() => {
1462
1635
  if (!watch || !watchedThreadId || typeof fetch === "undefined") return;
1463
1636
  const controller = new AbortController();
1464
- let waiting = null;
1637
+ const waiting = /* @__PURE__ */ new Set();
1465
1638
  let waitTimer = null;
1639
+ const unnamed = () => [...flightsRef.current].some((flight) => flight.runId === void 0 && flight.asked === void 0);
1466
1640
  const attachTo = (runId) => {
1467
- if (runId === stateRef.current.cursorRunId) return;
1468
- if (abortRef.current) {
1469
- waiting = runId;
1641
+ const state = stateRef.current;
1642
+ if (following(runId)) return;
1643
+ if (runCursor(state, runId) !== void 0 && !liveRunIds(state).includes(runId)) return;
1644
+ if (unnamed()) {
1645
+ waiting.add(runId);
1470
1646
  waitTimer ??= setTimeout(function recheck() {
1471
1647
  waitTimer = null;
1472
- if (controller.signal.aborted || waiting === null) return;
1473
- if (abortRef.current) {
1648
+ if (controller.signal.aborted || waiting.size === 0) return;
1649
+ if (unnamed()) {
1474
1650
  waitTimer = setTimeout(recheck, WATCH_RECHECK_MS);
1475
1651
  return;
1476
1652
  }
1477
- const next = waiting;
1478
- waiting = null;
1479
- attachTo(next);
1653
+ const next = [...waiting];
1654
+ waiting.clear();
1655
+ for (const id of next) attachTo(id);
1480
1656
  }, WATCH_RECHECK_MS);
1481
1657
  return;
1482
1658
  }
1483
- probe(false);
1659
+ probe(false, runId);
1484
1660
  };
1485
1661
  (async () => {
1486
1662
  let delay = WATCH_RETRY_MS;
@@ -1508,7 +1684,8 @@ function useChat(path, params = {}) {
1508
1684
  watch,
1509
1685
  watchedThreadId,
1510
1686
  post,
1511
- probe
1687
+ probe,
1688
+ following
1512
1689
  ]);
1513
1690
  const status = state.pending.length > 0 ? "awaiting-input" : phase !== "idle" ? phase : state.error ? "error" : "idle";
1514
1691
  return {