gemi 0.225.1 → 0.226.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.
- package/dist/ai/Agent.d.ts +48 -1
- package/dist/ai/Agent.d.ts.map +1 -1
- package/dist/ai/AgentController.d.ts +160 -17
- package/dist/ai/AgentController.d.ts.map +1 -1
- package/dist/ai/client/index.d.ts +1 -1
- package/dist/ai/client/index.d.ts.map +1 -1
- package/dist/ai/client/index.js +235 -58
- package/dist/ai/client/index.js.map +1 -1
- package/dist/ai/client/reducer.d.ts +46 -0
- package/dist/ai/client/reducer.d.ts.map +1 -1
- package/dist/ai/contextWindow.d.ts +9 -2
- package/dist/ai/contextWindow.d.ts.map +1 -1
- package/dist/ai/index.d.ts +3 -2
- package/dist/ai/index.d.ts.map +1 -1
- package/dist/ai/index.js +80 -70
- package/dist/ai/index.js.map +7 -6
- package/dist/ai/store/LiveRuns.d.ts +21 -0
- package/dist/ai/store/LiveRuns.d.ts.map +1 -1
- package/dist/ai/store/index.d.ts +1 -1
- package/dist/ai/store/index.d.ts.map +1 -1
- package/dist/ai/testing/index.js +2 -2
- package/dist/ai/testing/index.js.map +1 -1
- package/dist/ai/types.d.ts +14 -0
- package/dist/ai/types.d.ts.map +1 -1
- package/dist/ai/useChat.d.ts +30 -0
- package/dist/ai/useChat.d.ts.map +1 -1
- package/dist/app/index.js +1 -1
- package/dist/bun/plugin.js +1 -1
- package/dist/bun/preload.js +1 -1
- package/dist/{chunk-enqrjcdk.js → chunk-07rrezpq.js} +2 -2
- package/dist/{chunk-enqrjcdk.js.map → chunk-07rrezpq.js.map} +1 -1
- package/dist/{chunk-zy56kaws.js → chunk-0anqpm98.js} +2 -2
- package/dist/{chunk-zy56kaws.js.map → chunk-0anqpm98.js.map} +1 -1
- package/dist/{chunk-v5sg62xm.js → chunk-1p82h0y2.js} +2 -2
- package/dist/{chunk-v5sg62xm.js.map → chunk-1p82h0y2.js.map} +1 -1
- package/dist/{chunk-5kgqfczk.js → chunk-26ngc9ed.js} +3 -3
- package/dist/{chunk-5kgqfczk.js.map → chunk-26ngc9ed.js.map} +1 -1
- package/dist/{chunk-me70vszk.js → chunk-2jx1r4m7.js} +2 -2
- package/dist/{chunk-me70vszk.js.map → chunk-2jx1r4m7.js.map} +1 -1
- package/dist/{chunk-t5aft2ra.js → chunk-2xzf4nqq.js} +4 -4
- package/dist/{chunk-t5aft2ra.js.map → chunk-2xzf4nqq.js.map} +1 -1
- package/dist/{chunk-j0rhz378.js → chunk-42mm1g89.js} +2 -2
- package/dist/{chunk-j0rhz378.js.map → chunk-42mm1g89.js.map} +1 -1
- package/dist/{chunk-k657v3ed.js → chunk-4m7srgh5.js} +3 -3
- package/dist/{chunk-k657v3ed.js.map → chunk-4m7srgh5.js.map} +1 -1
- package/dist/{chunk-ng6pbd7f.js → chunk-53hg89g5.js} +4 -4
- package/dist/{chunk-ng6pbd7f.js.map → chunk-53hg89g5.js.map} +1 -1
- package/dist/{chunk-tp1gxcke.js → chunk-58gyj15y.js} +3 -3
- package/dist/{chunk-tp1gxcke.js.map → chunk-58gyj15y.js.map} +1 -1
- package/dist/{chunk-zvg2qsa5.js → chunk-5tf6nm65.js} +2 -2
- package/dist/{chunk-zvg2qsa5.js.map → chunk-5tf6nm65.js.map} +1 -1
- package/dist/{chunk-m7v4erdg.js → chunk-5yxyd1vb.js} +2 -2
- package/dist/{chunk-m7v4erdg.js.map → chunk-5yxyd1vb.js.map} +1 -1
- package/dist/{chunk-6h1hjzj7.js → chunk-5zbqh32n.js} +2 -2
- package/dist/{chunk-6h1hjzj7.js.map → chunk-5zbqh32n.js.map} +1 -1
- package/dist/{chunk-444g812v.js → chunk-603twf6k.js} +2 -2
- package/dist/{chunk-444g812v.js.map → chunk-603twf6k.js.map} +1 -1
- package/dist/{chunk-qtz4g4kv.js → chunk-6e12tbqc.js} +3 -3
- package/dist/{chunk-qtz4g4kv.js.map → chunk-6e12tbqc.js.map} +1 -1
- package/dist/{chunk-81y96dax.js → chunk-6n3wadx7.js} +1 -1
- package/dist/{chunk-ryh6rfts.js → chunk-72k87qz8.js} +2 -2
- package/dist/{chunk-ryh6rfts.js.map → chunk-72k87qz8.js.map} +1 -1
- package/dist/{chunk-7mvfk0q9.js → chunk-77pms0y3.js} +2 -2
- package/dist/{chunk-7mvfk0q9.js.map → chunk-77pms0y3.js.map} +1 -1
- package/dist/{chunk-mvb8m247.js → chunk-7g3r55sj.js} +2 -2
- package/dist/{chunk-mvb8m247.js.map → chunk-7g3r55sj.js.map} +1 -1
- package/dist/{chunk-we65r9fh.js → chunk-8s8bjrjp.js} +3 -3
- package/dist/{chunk-we65r9fh.js.map → chunk-8s8bjrjp.js.map} +1 -1
- package/dist/{chunk-1jyzg1ex.js → chunk-8xta8133.js} +3 -3
- package/dist/{chunk-1jyzg1ex.js.map → chunk-8xta8133.js.map} +1 -1
- package/dist/{chunk-v0fqq5z7.js → chunk-9kcjxtqg.js} +2 -2
- package/dist/{chunk-v0fqq5z7.js.map → chunk-9kcjxtqg.js.map} +1 -1
- package/dist/chunk-9rkjhnx2.js +5 -0
- package/dist/{chunk-qjn6a2ed.js.map → chunk-9rkjhnx2.js.map} +1 -1
- package/dist/{chunk-pdymdjpq.js → chunk-a2y481gh.js} +2 -2
- package/dist/{chunk-pdymdjpq.js.map → chunk-a2y481gh.js.map} +1 -1
- package/dist/{chunk-21hhvphe.js → chunk-a95pp5kn.js} +2 -2
- package/dist/{chunk-21hhvphe.js.map → chunk-a95pp5kn.js.map} +1 -1
- package/dist/{chunk-mca5s0bf.js → chunk-acsyr6wq.js} +3 -3
- package/dist/{chunk-mca5s0bf.js.map → chunk-acsyr6wq.js.map} +1 -1
- package/dist/{chunk-eydww2nn.js → chunk-atn9z9hx.js} +2 -2
- package/dist/{chunk-eydww2nn.js.map → chunk-atn9z9hx.js.map} +1 -1
- package/dist/{chunk-3e55yw7v.js → chunk-axjw9g4s.js} +3 -3
- package/dist/{chunk-3e55yw7v.js.map → chunk-axjw9g4s.js.map} +1 -1
- package/dist/{chunk-v979k0be.js → chunk-ay5raj98.js} +3 -3
- package/dist/{chunk-v979k0be.js.map → chunk-ay5raj98.js.map} +1 -1
- package/dist/{chunk-pn3egz75.js → chunk-bpfc31b9.js} +1 -1
- package/dist/{chunk-z6x78wb3.js → chunk-bv9kqnj1.js} +2 -2
- package/dist/{chunk-z6x78wb3.js.map → chunk-bv9kqnj1.js.map} +1 -1
- package/dist/{chunk-hvyzp9p6.js → chunk-dp2xrc54.js} +2 -2
- package/dist/{chunk-hvyzp9p6.js.map → chunk-dp2xrc54.js.map} +1 -1
- package/dist/{chunk-7d1f5gyv.js → chunk-dvsafvww.js} +2 -2
- package/dist/{chunk-7d1f5gyv.js.map → chunk-dvsafvww.js.map} +1 -1
- package/dist/{chunk-n2c748aw.js → chunk-e1qssbfa.js} +2 -2
- package/dist/{chunk-n2c748aw.js.map → chunk-e1qssbfa.js.map} +1 -1
- package/dist/{chunk-7901z26g.js → chunk-eb03m143.js} +2 -2
- package/dist/{chunk-7901z26g.js.map → chunk-eb03m143.js.map} +1 -1
- package/dist/{chunk-ads3c8m0.js → chunk-f1pyzfw0.js} +2 -2
- package/dist/{chunk-ads3c8m0.js.map → chunk-f1pyzfw0.js.map} +1 -1
- package/dist/{chunk-bwjaq694.js → chunk-f3jf9bk8.js} +3 -3
- package/dist/{chunk-bwjaq694.js.map → chunk-f3jf9bk8.js.map} +1 -1
- package/dist/{chunk-j7pzdvxr.js → chunk-fhe8c8zy.js} +2 -2
- package/dist/{chunk-j7pzdvxr.js.map → chunk-fhe8c8zy.js.map} +1 -1
- package/dist/{chunk-dxesf8bt.js → chunk-gae8a3jk.js} +2 -2
- package/dist/{chunk-dxesf8bt.js.map → chunk-gae8a3jk.js.map} +1 -1
- package/dist/{chunk-6et7jwp8.js → chunk-grxz4ecb.js} +2 -2
- package/dist/{chunk-6et7jwp8.js.map → chunk-grxz4ecb.js.map} +1 -1
- package/dist/{chunk-crk4ddqa.js → chunk-h4179pzx.js} +3 -3
- package/dist/{chunk-crk4ddqa.js.map → chunk-h4179pzx.js.map} +1 -1
- package/dist/{chunk-bch7xbsr.js → chunk-hqxbxk1f.js} +2 -2
- package/dist/{chunk-bch7xbsr.js.map → chunk-hqxbxk1f.js.map} +1 -1
- package/dist/{chunk-2450kxym.js → chunk-jc5nbacx.js} +2 -2
- package/dist/{chunk-2450kxym.js.map → chunk-jc5nbacx.js.map} +1 -1
- package/dist/{chunk-3vgjrn2r.js → chunk-jg94sqmx.js} +2 -2
- package/dist/{chunk-3vgjrn2r.js.map → chunk-jg94sqmx.js.map} +1 -1
- package/dist/{chunk-ftc9292d.js → chunk-jkftpj8g.js} +2 -2
- package/dist/{chunk-ftc9292d.js.map → chunk-jkftpj8g.js.map} +1 -1
- package/dist/{chunk-8yxjq6cm.js → chunk-jm4908xm.js} +2 -2
- package/dist/{chunk-8yxjq6cm.js.map → chunk-jm4908xm.js.map} +1 -1
- package/dist/{chunk-kxc2mzzr.js → chunk-jy26hy7z.js} +2 -2
- package/dist/{chunk-kxc2mzzr.js.map → chunk-jy26hy7z.js.map} +1 -1
- package/dist/{chunk-tgy75n24.js → chunk-k4v5e6ww.js} +2 -2
- package/dist/{chunk-tgy75n24.js.map → chunk-k4v5e6ww.js.map} +1 -1
- package/dist/{chunk-4qyp7w89.js → chunk-kn2hk1n3.js} +1 -1
- package/dist/{chunk-9xxgj5zk.js → chunk-mcntwhp7.js} +3 -3
- package/dist/{chunk-9xxgj5zk.js.map → chunk-mcntwhp7.js.map} +1 -1
- package/dist/{chunk-wnmpwb4s.js → chunk-mdby3yea.js} +3 -3
- package/dist/{chunk-wnmpwb4s.js.map → chunk-mdby3yea.js.map} +1 -1
- package/dist/{chunk-qx9p15em.js → chunk-mmns1ske.js} +2 -2
- package/dist/{chunk-qx9p15em.js.map → chunk-mmns1ske.js.map} +1 -1
- package/dist/{chunk-ja3n968b.js → chunk-mzmv1zmn.js} +2 -2
- package/dist/{chunk-ja3n968b.js.map → chunk-mzmv1zmn.js.map} +1 -1
- package/dist/{chunk-v4ecxbgd.js → chunk-n1dcwz8z.js} +2 -2
- package/dist/{chunk-v4ecxbgd.js.map → chunk-n1dcwz8z.js.map} +1 -1
- package/dist/{chunk-mvp6fkg2.js → chunk-n6rf9tnb.js} +3 -3
- package/dist/{chunk-mvp6fkg2.js.map → chunk-n6rf9tnb.js.map} +1 -1
- package/dist/{chunk-kzz2nfzf.js → chunk-nfmsyjf4.js} +2 -2
- package/dist/{chunk-kzz2nfzf.js.map → chunk-nfmsyjf4.js.map} +1 -1
- package/dist/{chunk-z4fhhtjh.js → chunk-ngk1tka6.js} +2 -2
- package/dist/{chunk-z4fhhtjh.js.map → chunk-ngk1tka6.js.map} +1 -1
- package/dist/{chunk-j13jmjkr.js → chunk-p7s80mhn.js} +2 -2
- package/dist/{chunk-j13jmjkr.js.map → chunk-p7s80mhn.js.map} +1 -1
- package/dist/{chunk-awryyak8.js → chunk-pdxmd8bd.js} +2 -2
- package/dist/{chunk-awryyak8.js.map → chunk-pdxmd8bd.js.map} +1 -1
- package/dist/{chunk-421hq1rw.js → chunk-ppx8p2g0.js} +1 -1
- package/dist/{chunk-64yf0shq.js → chunk-q26d0qa3.js} +2 -2
- package/dist/{chunk-64yf0shq.js.map → chunk-q26d0qa3.js.map} +1 -1
- package/dist/{chunk-40yd1n60.js → chunk-q67c5zyw.js} +2 -2
- package/dist/{chunk-40yd1n60.js.map → chunk-q67c5zyw.js.map} +1 -1
- package/dist/{chunk-gj0bwm41.js → chunk-q902m8c6.js} +2 -2
- package/dist/{chunk-gj0bwm41.js.map → chunk-q902m8c6.js.map} +1 -1
- package/dist/{chunk-t9fh0ptd.js → chunk-qmezsfxd.js} +2 -2
- package/dist/{chunk-t9fh0ptd.js.map → chunk-qmezsfxd.js.map} +1 -1
- package/dist/{chunk-8szsd6vy.js → chunk-r8zp4khs.js} +2 -2
- package/dist/{chunk-8szsd6vy.js.map → chunk-r8zp4khs.js.map} +1 -1
- package/dist/{chunk-70jhw0ft.js → chunk-rk3xgyd0.js} +1 -1
- package/dist/{chunk-xpzdwhjg.js → chunk-se6ktpm5.js} +3 -3
- package/dist/{chunk-xpzdwhjg.js.map → chunk-se6ktpm5.js.map} +1 -1
- package/dist/{chunk-x93dpv6e.js → chunk-szx9z0zm.js} +2 -2
- package/dist/{chunk-x93dpv6e.js.map → chunk-szx9z0zm.js.map} +1 -1
- package/dist/{chunk-3dkaz47e.js → chunk-t956xcy4.js} +2 -2
- package/dist/{chunk-3dkaz47e.js.map → chunk-t956xcy4.js.map} +1 -1
- package/dist/{chunk-fjnhqbtx.js → chunk-v1d1xc2r.js} +1 -1
- package/dist/{chunk-qrc2r0ce.js → chunk-vd8a3g3n.js} +3 -3
- package/dist/{chunk-qrc2r0ce.js.map → chunk-vd8a3g3n.js.map} +1 -1
- package/dist/{chunk-x47kwv2x.js → chunk-vf4tn1yd.js} +2 -2
- package/dist/{chunk-x47kwv2x.js.map → chunk-vf4tn1yd.js.map} +1 -1
- package/dist/{chunk-8pdke84p.js → chunk-vre98hts.js} +2 -2
- package/dist/{chunk-8pdke84p.js.map → chunk-vre98hts.js.map} +1 -1
- package/dist/{chunk-svrvb47q.js → chunk-w6j7q0dc.js} +2 -2
- package/dist/{chunk-svrvb47q.js.map → chunk-w6j7q0dc.js.map} +1 -1
- package/dist/{chunk-bzrta2sw.js → chunk-w7511nk3.js} +3 -3
- package/dist/{chunk-bzrta2sw.js.map → chunk-w7511nk3.js.map} +1 -1
- package/dist/{chunk-wzr8tzv0.js → chunk-xg2ctnwf.js} +2 -2
- package/dist/{chunk-wzr8tzv0.js.map → chunk-xg2ctnwf.js.map} +1 -1
- package/dist/{chunk-9znec0we.js → chunk-ydgtkgf9.js} +24 -24
- package/dist/chunk-ydgtkgf9.js.map +27 -0
- package/dist/{chunk-d3a29hyg.js → chunk-yg7sra7w.js} +3 -3
- package/dist/{chunk-d3a29hyg.js.map → chunk-yg7sra7w.js.map} +1 -1
- package/dist/{chunk-tbvrv4h2.js → chunk-zq4jbwvn.js} +2 -2
- package/dist/{chunk-tbvrv4h2.js.map → chunk-zq4jbwvn.js.map} +1 -1
- package/dist/{chunk-rq0dpv7r.js → chunk-ztpenack.js} +2 -2
- package/dist/{chunk-rq0dpv7r.js.map → chunk-ztpenack.js.map} +1 -1
- package/dist/config/index.js +1 -1
- package/dist/console/run.js +2 -2
- package/dist/console/run.js.map +1 -1
- package/dist/container/index.js +2 -2
- package/dist/container/index.js.map +1 -1
- package/dist/database/index.js +2 -2
- package/dist/database/index.js.map +1 -1
- package/dist/email/Email.d.ts.map +1 -1
- package/dist/email/index.js +3 -4
- package/dist/email/index.js.map +4 -15
- package/dist/facades/index.js +2 -2
- package/dist/facades/index.js.map +1 -1
- package/dist/foundation/index.js +2 -2
- package/dist/foundation/index.js.map +1 -1
- package/dist/http/index.js +2 -2
- package/dist/http/index.js.map +1 -1
- package/dist/i18n/dictionaryRuntime.js +2 -2
- package/dist/i18n/dictionaryRuntime.js.map +1 -1
- package/dist/i18n/index.js +2 -2
- package/dist/i18n/index.js.map +1 -1
- package/dist/kernel/index.js +2 -2
- package/dist/kernel/index.js.map +1 -1
- package/dist/orm/index.js +2 -2
- package/dist/orm/index.js.map +1 -1
- package/dist/probe/index.js +2 -2
- package/dist/probe/index.js.map +1 -1
- package/dist/server/index.js +2 -2
- package/dist/server/index.js.map +1 -1
- package/dist/services/index.js +2 -2
- package/dist/services/index.js.map +1 -1
- package/dist/services/queue/work.js +2 -2
- package/dist/services/queue/work.js.map +1 -1
- package/dist/stats/route-table.js +1 -1
- package/dist/support/index.js +2 -2
- package/dist/support/index.js.map +1 -1
- package/dist/vitest/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-9znec0we.js.map +0 -27
- package/dist/chunk-qjn6a2ed.js +0 -5
- /package/dist/{chunk-81y96dax.js.map → chunk-6n3wadx7.js.map} +0 -0
- /package/dist/{chunk-pn3egz75.js.map → chunk-bpfc31b9.js.map} +0 -0
- /package/dist/{chunk-4qyp7w89.js.map → chunk-kn2hk1n3.js.map} +0 -0
- /package/dist/{chunk-421hq1rw.js.map → chunk-ppx8p2g0.js.map} +0 -0
- /package/dist/{chunk-70jhw0ft.js.map → chunk-rk3xgyd0.js.map} +0 -0
- /package/dist/{chunk-fjnhqbtx.js.map → chunk-v1d1xc2r.js.map} +0 -0
package/dist/ai/client/index.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
982
|
-
|
|
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,
|
|
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 =
|
|
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 =
|
|
1085
|
-
superseded
|
|
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
|
-
|
|
1238
|
+
const flight = {
|
|
1089
1239
|
controller,
|
|
1090
1240
|
clientRunId
|
|
1091
1241
|
};
|
|
1092
|
-
|
|
1093
|
-
|
|
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
|
-
...
|
|
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 ?
|
|
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 ||
|
|
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,
|
|
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
|
-
|
|
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
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
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 (
|
|
1324
|
-
const
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
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
|
|
1412
|
-
|
|
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
|
-
|
|
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,
|
|
1602
|
+
await consume(response, flight);
|
|
1428
1603
|
return true;
|
|
1429
1604
|
} catch {
|
|
1430
1605
|
return false;
|
|
1431
1606
|
} finally {
|
|
1432
|
-
|
|
1433
|
-
abortRef.current = null;
|
|
1434
|
-
setPhaseSafe("idle");
|
|
1435
|
-
}
|
|
1607
|
+
land(flight);
|
|
1436
1608
|
}
|
|
1437
1609
|
}, [
|
|
1438
1610
|
consume,
|
|
1439
|
-
|
|
1440
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1468
|
-
if (
|
|
1469
|
-
|
|
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 ===
|
|
1473
|
-
if (
|
|
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
|
|
1479
|
-
|
|
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 {
|