@epam/ai-dial-chat-hooks 1.3.0-dev.356 → 1.3.0-dev.358

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 (26) hide show
  1. package/conversation/useConversationStream/conversation-stream.types.d.ts +136 -0
  2. package/conversation/useConversationStream/conversation-stream.types.d.ts.map +1 -0
  3. package/conversation/useConversationStream/generation-resume.d.ts +1 -1
  4. package/conversation/useConversationStream/generation-resume.d.ts.map +1 -1
  5. package/conversation/useConversationStream/stream-errors.d.ts +10 -0
  6. package/conversation/useConversationStream/stream-errors.d.ts.map +1 -0
  7. package/conversation/useConversationStream/stream-errors.js +10 -0
  8. package/conversation/useConversationStream/stream-session.d.ts +51 -0
  9. package/conversation/useConversationStream/stream-session.d.ts.map +1 -0
  10. package/conversation/useConversationStream/stream-session.js +179 -0
  11. package/conversation/useConversationStream/useConversationReloadErrors.d.ts +25 -0
  12. package/conversation/useConversationStream/useConversationReloadErrors.d.ts.map +1 -0
  13. package/conversation/useConversationStream/useConversationReloadErrors.js +38 -0
  14. package/conversation/useConversationStream/useConversationStream.d.ts +2 -135
  15. package/conversation/useConversationStream/useConversationStream.d.ts.map +1 -1
  16. package/conversation/useConversationStream/useConversationStream.js +134 -332
  17. package/conversation/useConversationStream/useStopGeneration.d.ts +29 -0
  18. package/conversation/useConversationStream/useStopGeneration.d.ts.map +1 -0
  19. package/conversation/useConversationStream/useStopGeneration.js +48 -0
  20. package/conversation/useConversationStream/useStreamPathState.d.ts +39 -0
  21. package/conversation/useConversationStream/useStreamPathState.d.ts.map +1 -0
  22. package/conversation/useConversationStream/useStreamPathState.js +55 -0
  23. package/package.json +17 -17
  24. package/project/use-project-actions.d.ts +1 -0
  25. package/project/use-project-actions.d.ts.map +1 -1
  26. package/project/use-project-actions.js +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"useConversationStream.d.ts","sourceRoot":"","sources":["../../../src/conversation/useConversationStream/useConversationStream.ts"],"names":[],"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EAAE,OAAO,CAAC;IACrB,gBAAgB,EAAE,OAAO,CAAC;IAC1B,iEAAiE;IACjE,0BAA0B,EAAE,OAAO,CAAC;IACpC,iDAAiD;IACjD,uBAAuB,EAAE,OAAO,CAAC;IACjC,yEAAyE;IACzE,uBAAuB,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9C;AAkBD;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB,GAAI,mQAYnC,2BAA2B,KAAG,2BAo+BhC,CAAC"}
1
+ {"version":3,"file":"useConversationStream.d.ts","sourceRoot":"","sources":["../../../src/conversation/useConversationStream/useConversationStream.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAEV,2BAA2B,EAC3B,2BAA2B,EAC5B,MAAM,6BAA6B,CAAC;AAYrC,YAAY,EACV,+BAA+B,EAC/B,yBAAyB,EACzB,yBAAyB,EACzB,iCAAiC,EACjC,2BAA2B,EAC3B,kBAAkB,EAClB,uBAAuB,EACvB,2BAA2B,EAC3B,2BAA2B,GAC5B,MAAM,6BAA6B,CAAC;AAErC;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB,GAAI,mQAYnC,2BAA2B,KAAG,2BAqRhC,CAAC"}
@@ -1,345 +1,147 @@
1
- import { safeDecodeURI as e } from "../../shared/string-utils.js";
2
- import { DEFAULT_GENERATION_CONFLICT_MESSAGE as t, DEFAULT_GENERATION_PERSISTENCE_ERROR_MESSAGE as n, GenerationConflictError as r, GenerationPersistenceError as i, StopCompletionError as a, StreamInterruptedError as o, StreamUpstreamError as s } from "../create-chat-stream-api.js";
3
- import { getConversationPath as c } from "./conversation-path.js";
4
- import { applyChunkToMessages as l } from "./apply-chunk.js";
5
- import { restoreBufferedMessage as u } from "./buffered-generation.js";
6
- import { createResumeIfAwaitingGeneration as ee, fetchConversationForRecovery as te, findPendingBackgroundMessageIndex as d, hasGeneratedPayload as ne, isAwaitingGenerationResume as f } from "./generation-resume.js";
7
- import { createFrameScheduler as re } from "./frame-scheduler.js";
8
- import { SendCompletionDtoModeEnum as ie } from "@epam/ai-dial-chat-api-client";
9
- import { MessageRole as ae, generateUUID as oe } from "@epam/ai-dial-chat-shared";
10
- import { useCallback as p, useEffect as m, useMemo as h, useRef as g, useState as _ } from "react";
1
+ import { DEFAULT_GENERATION_CONFLICT_MESSAGE as e, DEFAULT_GENERATION_PERSISTENCE_ERROR_MESSAGE as t } from "../create-chat-stream-api.js";
2
+ import { getConversationPath as n } from "./conversation-path.js";
3
+ import { restoreBufferedMessage as r } from "./buffered-generation.js";
4
+ import { createResumeIfAwaitingGeneration as i, isAwaitingGenerationResume as a } from "./generation-resume.js";
5
+ import { createFrameScheduler as o } from "./frame-scheduler.js";
6
+ import { findResumedBackgroundGenerationId as s } from "./stream-errors.js";
7
+ import { runStreamSession as ee } from "./stream-session.js";
8
+ import { useConversationReloadErrors as te } from "./useConversationReloadErrors.js";
9
+ import { useStopGeneration as c } from "./useStopGeneration.js";
10
+ import { useStreamPathState as l } from "./useStreamPathState.js";
11
+ import { MessageRole as u } from "@epam/ai-dial-chat-shared";
12
+ import { useCallback as d, useEffect as f, useMemo as p, useRef as m, useState as ne } from "react";
11
13
  //#region src/conversation/useConversationStream/useConversationStream.ts
12
- var se = 2e4, ce = 401, v = 404, y = 2e3, b = 100, le = (e, t, n) => e instanceof i ? n : e instanceof r ? t : e instanceof s ? e.message : "", x = (e) => {
13
- if (!e) return;
14
- let t = d(e);
15
- return t === -1 ? void 0 : e.messages[t].backgroundGeneration?.generationId;
16
- }, S = ({ conversationId: s, state: { setConversation: S, conversationRef: C }, transport: w, generation: { startGeneration: ue, completeGeneration: de }, channel: T, overlay: E, onStopError: fe, generationConflictMessage: pe = t, generationPersistenceErrorMessage: D = n, onStreamError: O, batchChunksPerFrame: k = !1 }) => {
17
- let A = g(O);
18
- m(() => {
19
- A.current = O;
20
- }, [O]);
21
- let me = g(k);
22
- m(() => {
23
- me.current = k;
24
- }, [k]);
25
- let [j] = _(re), [M, N] = _(() => /* @__PURE__ */ new Set()), P = g(M);
26
- P.current = M;
27
- let [F, he] = _(() => /* @__PURE__ */ new Set()), I = g(/* @__PURE__ */ new Map()), L = g(/* @__PURE__ */ new Map()), R = g(/* @__PURE__ */ new Set()), z = g(/* @__PURE__ */ new Map()), B = g(/* @__PURE__ */ new Set()), V = g(/* @__PURE__ */ new Set()), ge = g(/* @__PURE__ */ new Map()), H = g(!0), U = g(/* @__PURE__ */ new Map()), [_e, ve] = _(U.current), W = g(/* @__PURE__ */ new Set()), [ye, be] = _(W.current);
28
- m(() => (H.current = !0, () => {
29
- H.current = !1;
14
+ var h = ({ conversationId: h, state: { setConversation: g, conversationRef: _ }, transport: v, generation: { startGeneration: y, completeGeneration: b }, channel: x, overlay: S, onStopError: re, generationConflictMessage: C = e, generationPersistenceErrorMessage: w = t, onStreamError: T, batchChunksPerFrame: E = !1 }) => {
15
+ let D = m(T);
16
+ f(() => {
17
+ D.current = T;
18
+ }, [T]);
19
+ let O = m(E);
20
+ f(() => {
21
+ O.current = E;
22
+ }, [E]);
23
+ let [k] = ne(o), A = m(!0);
24
+ f(() => (A.current = !0, () => {
25
+ A.current = !1;
30
26
  }), []);
31
- let G = p((e) => {
32
- if (!U.current.has(e)) return;
33
- let t = new Map(U.current);
34
- t.delete(e), U.current = t, H.current && ve(t);
35
- }, []), K = p((e, t, n) => {
36
- if (!H.current || !n()) return;
37
- let r = new Map(U.current);
38
- r.set(e, {
39
- retry: t,
40
- isCurrent: n
41
- }), U.current = r, ve(r);
42
- }, []), xe = p(async () => {
43
- if (!s) return;
44
- let e = U.current.get(c(s));
45
- if (e && e.isCurrent() && !W.current.has(e)) {
46
- W.current.add(e), be(new Set(W.current));
47
- try {
48
- await e.retry();
49
- } finally {
50
- W.current.delete(e), H.current && be(new Set(W.current));
51
- }
52
- }
53
- }, [s]), Se = g(s);
54
- m(() => (Se.current = s, () => j.cancelAll()), [s, j]);
55
- let q = p((e) => H.current && c(Se.current ?? "") === e, []), J = p((e) => {
56
- G(e), N((t) => {
57
- if (t.has(e)) return t;
58
- let n = new Set(t);
59
- return n.add(e), n;
60
- });
61
- }, [G]), Y = p((e) => {
62
- N((t) => {
63
- if (!t.has(e)) return t;
64
- let n = new Set(t);
65
- return n.delete(e), n;
66
- });
67
- }, []), X = p((e, t) => {
68
- he((n) => {
69
- if (n.has(e) === t) return n;
70
- let r = new Set(n);
71
- return t ? r.add(e) : r.delete(e), r;
72
- });
73
- }, []), Ce = p((e, t) => {
74
- I.current.get(e) === t && (I.current.delete(e), X(e, !1));
75
- }, [X]), we = p((e, t) => {
76
- I.current.has(e) || (L.current.set(e, t), X(e, !0));
77
- }, [X]), Te = p((e, t) => {
78
- L.current.get(e) === t && (L.current.delete(e), I.current.has(e) || X(e, !1));
79
- }, [X]), Z = h(() => ee({
80
- transport: w,
81
- setConversation: S,
82
- conversationRef: C,
83
- resumingPathsRef: R,
84
- bufferedGenerationsRef: z,
85
- addStreamingPath: J,
86
- removeStreamingPath: Y,
87
- isPathDisplayed: q,
88
- generationPersistenceErrorMessage: D,
89
- frameScheduler: k ? j : void 0,
90
- stoppedGenerationIdsRef: B,
91
- rememberResumedGenerationId: we,
92
- forgetResumedGenerationId: Te,
93
- onReloadError: K,
94
- onReloadSuccess: G
27
+ let { reloadFailuresRef: j, reloadFailures: M, retrying: N, clearReloadError: P, reportReloadError: F, retryConversationReload: ie } = te({
28
+ conversationId: h,
29
+ mountedRef: A
30
+ }), { streamingPaths: ae, streamingPathsRef: oe, stoppablePaths: se, activeGenerationIdsRef: I, resumedGenerationIdsRef: ce, resumingPathsRef: L, bufferedGenerationsRef: R, stoppedGenerationIdsRef: z, stopRequestedIdsRef: B, latestGenerationIdsRef: V, isPathDisplayed: H, addStreamingPath: U, removeStreamingPath: W, setPathStoppable: G, releaseActiveGeneration: K, rememberResumedGenerationId: q, forgetResumedGenerationId: J } = l({
31
+ conversationId: h,
32
+ frameScheduler: k,
33
+ mountedRef: A,
34
+ clearReloadError: P
35
+ }), Y = p(() => i({
36
+ transport: v,
37
+ setConversation: g,
38
+ conversationRef: _,
39
+ resumingPathsRef: L,
40
+ bufferedGenerationsRef: R,
41
+ addStreamingPath: U,
42
+ removeStreamingPath: W,
43
+ isPathDisplayed: H,
44
+ generationPersistenceErrorMessage: w,
45
+ frameScheduler: E ? k : void 0,
46
+ stoppedGenerationIdsRef: z,
47
+ rememberResumedGenerationId: q,
48
+ forgetResumedGenerationId: J,
49
+ onReloadError: F,
50
+ onReloadSuccess: P
95
51
  }), [
96
- J,
97
- Y,
98
- q,
52
+ U,
53
+ W,
54
+ H,
55
+ v,
99
56
  w,
100
- D,
57
+ E,
101
58
  k,
102
- j,
103
- we,
104
- Te,
105
- K,
106
- G
107
- ]), Ee = p((t, n, a, s, ee, re, p = ie.Append, m = {}) => {
108
- let h = re ?? oe(), g = c(t);
109
- j.flush(g), R.current.delete(g), I.current.set(g, h), ge.current.set(g, h), X(g, !0);
110
- let _;
111
- p === ie.Regenerate ? _ = a : p === ie.Edit && (_ = a - 1);
112
- let v = () => ge.current.get(g) !== h, y = ue(g, h), b = C.current?.messages[a];
113
- b ? z.current.set(g, {
114
- generationId: h,
115
- messageIndex: a,
116
- message: b
117
- }) : z.current.delete(g), J(g), E?.notifyGenerationStart?.(), T?.ensureConnected();
118
- let x = () => {
119
- v() || Y(g), Ce(g, h), de(g, h), T?.notifyGenerationSettled?.();
120
- }, fe = () => {
121
- if (v()) return;
122
- let e = z.current.get(g);
123
- e && e.generationId === h && (e.message = {
124
- ...e.message,
125
- streamErrorMessage: D
126
- }, A.current?.(new i()), q(g) && S((t) => {
127
- if (!t || v() || !q(g)) return t;
128
- let n = u(t, e);
129
- return C.current = n, n;
130
- }));
131
- }, O = z.current.get(g), k = () => H.current && !v() && z.current.get(g) === O, M = async () => {
132
- if (k()) {
133
- if (!q(g)) O && z.current.delete(g);
134
- else try {
135
- let n = await w.getConversation(e(t));
136
- if (!k()) return;
137
- if (G(g), !q(g)) {
138
- O && z.current.delete(g);
139
- return;
140
- }
141
- if (d(n) !== -1) {
142
- Z(t, n, { seedMessage: O?.message });
143
- return;
144
- }
145
- if (O && O.messageIndex === n.messages.length - 1 && f(n) && ne(O.message)) {
146
- fe();
147
- return;
148
- }
149
- O && z.current.delete(g), S(n), C.current = n;
150
- } catch {
151
- k() && K(g, M, k);
152
- }
153
- }
154
- }, N = {
155
- signal: y.signal,
156
- onChunk: (e) => {
157
- if (v()) return;
158
- let t = z.current.get(g);
159
- if (t?.generationId === h) {
160
- let n = l([t.message], 0, e);
161
- n && (t.message = n[0]);
162
- }
163
- q(g) && (me.current && t?.generationId === h ? j.schedule(g, () => {
164
- let e = z.current.get(g);
165
- if (v() || !q(g) || e?.generationId !== h) return;
166
- let t = { ...e };
167
- S((e) => {
168
- if (!e) return e;
169
- let n = u(e, t);
170
- return C.current = n, n;
171
- });
172
- }) : S((t) => {
173
- if (!t) return t;
174
- let n = z.current.get(g), r;
175
- if (n?.generationId === h) r = u(t, n);
176
- else {
177
- let n = l(t.messages, a, e);
178
- if (!n) return t;
179
- r = {
180
- ...t,
181
- messages: n
182
- };
183
- }
184
- return C.current = r, r;
185
- }));
186
- },
187
- onComplete: async () => {
188
- j.flush(g), x(), B.current.has(h) ? B.current.delete(h) : v() || E?.notifyGenerationEnd?.(), await M();
189
- },
190
- onError: (e) => {
191
- j.flush(g), A.current?.(e), e instanceof o && !v() ? V(e) : e instanceof r && m.resumeOnConflict && !v() ? U(e) : P(e);
192
- }
193
- }, P = (e) => {
194
- let t = z.current.get(g), n = t?.generationId === h ? t : void 0;
195
- n && !(e instanceof i) && z.current.delete(g), x(), n && w.saveConversation && e instanceof i && e.status === ce ? he(e, n, w.saveConversation.bind(w)) : F(e, n);
196
- }, F = (e, t) => {
197
- if (t && e instanceof i && (t.message = {
198
- ...t.message,
199
- streamErrorMessage: D
200
- }), v() || !q(g)) return;
201
- let n = le(e, pe, D);
202
- S((e) => {
203
- if (!e || v() || !q(g)) return e;
204
- let r = t?.generationId === h ? u(e, t) : e, i = {
205
- ...r,
206
- messages: r.messages.map((e, t) => t === a ? {
207
- ...e,
208
- streamErrorMessage: n
209
- } : e)
210
- };
211
- return C.current = i, i;
212
- });
213
- }, he = async (n, r, i) => {
214
- let a = () => H.current && !v() && z.current.get(g) === r;
215
- try {
216
- let o = await w.getConversation(e(t));
217
- if (!a()) return;
218
- if (r.messageIndex !== o.messages.length - 1 || !f(o)) {
219
- F(n, r);
220
- return;
221
- }
222
- let s = n.terminalMarker ? {
223
- ...r,
224
- message: {
225
- ...r.message,
226
- ...n.terminalMarker
227
- }
228
- } : r, c = await i(g, u(o, s));
229
- if (!a() || (z.current.delete(g), !q(g))) return;
230
- S(c), C.current = c;
231
- } catch {
232
- a() && F(n, r);
233
- }
234
- }, L = () => {
235
- x(), B.current.has(h) ? B.current.delete(h) : v() || E?.notifyGenerationEnd?.();
236
- }, V = async (n) => {
237
- R.current.add(g);
238
- let r = await te(() => w.getConversation(e(t)), v);
239
- _e(r, n, !1);
240
- }, U = async (n) => {
241
- I.current.get(g) === h && X(g, !1), R.current.add(g);
242
- let r = await te(() => w.getConversation(e(t)), v, (e) => e.messages.length === a && e.messages.at(-1)?.role === ae.User);
243
- _e(r, n, !0);
244
- }, _e = (e, n, r) => {
245
- if (v()) {
246
- x();
247
- return;
248
- }
249
- let i = e?.messages[e.messages.length - 1], o = e != null && e.messages.length - 1 === a && i?.role === ae.Assistant, s = r && e != null && d(e) !== -1;
250
- if (!e || !o && !s) {
251
- R.current.delete(g), P(n);
252
- return;
253
- }
254
- let c = z.current.get(g), l = c?.generationId === h ? c : void 0;
255
- f(e) ? Z(t, e, {
256
- seedMessage: o ? l?.message : void 0,
257
- skipDedupe: !0,
258
- onSettled: L
259
- }) : (R.current.delete(g), l && z.current.delete(g), q(g) && (S(e), C.current = e), L());
260
- };
261
- (async () => {
262
- try {
263
- let e = T?.channelId ?? await T?.waitForChannel(se) ?? void 0, t = B.current.has(h);
264
- if (y.signal.aborted || v() || t) {
265
- z.current.get(g)?.generationId === h && z.current.delete(g), v() || Y(g), Ce(g, h), de(g, h), T?.notifyGenerationSettled?.(), t ? B.current.delete(h) : v() || E?.notifyGenerationEnd?.();
266
- return;
267
- }
268
- w.streamCompletion(g, n, s, N, ee, h, p, _, e);
269
- } catch (e) {
270
- N.onError(e instanceof Error ? e : Error(String(e)));
271
- }
272
- })();
273
- }, [
274
- ue,
275
- de,
59
+ q,
276
60
  J,
61
+ F,
62
+ P
63
+ ]), le = d((e, t, n, r, i, a, o, s) => ee({
64
+ transport: v,
65
+ channel: x,
66
+ overlay: S,
67
+ startGeneration: y,
68
+ completeGeneration: b,
69
+ setConversation: g,
70
+ conversationRef: _,
71
+ generationConflictMessage: C,
72
+ generationPersistenceErrorMessage: w,
73
+ onStreamErrorRef: D,
74
+ batchChunksPerFrameRef: O,
75
+ mountedRef: A,
76
+ frameScheduler: k,
77
+ resumeGeneration: Y,
78
+ activeGenerationIdsRef: I,
79
+ latestGenerationIdsRef: V,
80
+ resumingPathsRef: L,
81
+ bufferedGenerationsRef: R,
82
+ stoppedGenerationIdsRef: z,
83
+ addStreamingPath: U,
84
+ removeStreamingPath: W,
85
+ setPathStoppable: G,
86
+ releaseActiveGeneration: K,
87
+ isPathDisplayed: H,
88
+ reportReloadError: F,
89
+ clearReloadError: P
90
+ }, e, t, n, r, i, a, o, s), [
91
+ y,
92
+ b,
93
+ U,
94
+ W,
95
+ G,
96
+ K,
97
+ H,
98
+ x?.channelId,
99
+ x?.ensureConnected,
100
+ x?.waitForChannel,
101
+ x?.notifyGenerationSettled,
102
+ S,
103
+ v,
277
104
  Y,
278
- X,
279
- Ce,
280
- q,
281
- T?.channelId,
282
- T?.ensureConnected,
283
- T?.waitForChannel,
284
- T?.notifyGenerationSettled,
285
- E,
105
+ C,
286
106
  w,
287
- Z,
288
- pe,
289
- D,
290
- j,
291
- K,
292
- G
293
- ]), De = p((e, t) => {
294
- let n = c(e), r = z.current.get(n);
295
- return U.current.get(n)?.isCurrent() && r && t.messages.length - 1 >= r.messageIndex && t.messages[r.messageIndex].role === ae.Assistant && !f(t) ? (G(n), z.current.delete(n), t) : r ? u(t, r) : t;
296
- }, [G]), Oe = p(() => {
297
- if (!s) return;
298
- let e = c(s), t = I.current.get(e), n = t ?? x(C.current) ?? L.current.get(e);
299
- if (!n || V.current.has(n)) return;
300
- V.current.add(n), B.current.add(n), j.flush(e), E?.notifyStopGenerating?.();
301
- let r = z.current.get(e)?.message.content, i = () => I.current.get(e) === n || t == null && P.current.has(e), o = async () => {
302
- let e = y / b;
303
- for (let t = 0; t < e; t++) {
304
- if (!i() || !H.current) return !0;
305
- await new Promise((e) => setTimeout(e, b));
306
- }
307
- return !i();
308
- };
309
- (async () => {
310
- try {
311
- await w.stopCompletion({
312
- generationId: n,
313
- path: e,
314
- content: r
315
- });
316
- } catch (e) {
317
- let t = e instanceof Error ? e : Error(String(e));
318
- if (t instanceof a && t.status === v && await o()) return;
319
- V.current.delete(n), fe?.(t);
320
- }
321
- })();
107
+ k,
108
+ F,
109
+ P
110
+ ]), ue = d((e, t) => {
111
+ let i = n(e), o = R.current.get(i);
112
+ return j.current.get(i)?.isCurrent() && o && t.messages.length - 1 >= o.messageIndex && t.messages[o.messageIndex].role === u.Assistant && !a(t) ? (P(i), R.current.delete(i), t) : o ? r(t, o) : t;
322
113
  }, [
323
- s,
324
- C,
325
- j,
326
- fe,
327
- E,
328
- w
329
- ]), ke = p((e, t) => {
330
- I.current.has(c(e)) || Z(e, t, { trackAttachGenerationId: !0 });
331
- }, [Z]), Ae = s != null && M.has(c(s)), Q = s == null ? null : c(s), je = Q != null && (F.has(Q) || Ae && x(C.current) != null), $ = Q ? _e.get(Q) : void 0, Me = $?.isCurrent() ?? !1;
114
+ R,
115
+ P,
116
+ j
117
+ ]), de = c({
118
+ conversationId: h,
119
+ conversationRef: _,
120
+ transport: v,
121
+ overlay: S,
122
+ onStopError: re,
123
+ frameScheduler: k,
124
+ mountedRef: A,
125
+ activeGenerationIdsRef: I,
126
+ resumedGenerationIdsRef: ce,
127
+ bufferedGenerationsRef: R,
128
+ stoppedGenerationIdsRef: z,
129
+ stopRequestedIdsRef: B,
130
+ streamingPathsRef: oe
131
+ }), fe = d((e, t) => {
132
+ I.current.has(n(e)) || Y(e, t, { trackAttachGenerationId: !0 });
133
+ }, [I, Y]), X = h != null && ae.has(n(h)), Z = h == null ? null : n(h), pe = Z != null && (se.has(Z) || X && s(_.current) != null), Q = Z ? M.get(Z) : void 0, $ = Q?.isCurrent() ?? !1;
332
134
  return {
333
- hasConversationReloadError: Me,
334
- isReloadingConversation: !!$ && Me && ye.has($),
335
- retryConversationReload: xe,
336
- startStream: Ee,
337
- handleStop: Oe,
338
- resumeIfAwaitingGeneration: ke,
339
- restoreBufferedGeneration: De,
340
- isStreaming: Ae,
341
- canStopStreaming: je
135
+ hasConversationReloadError: $,
136
+ isReloadingConversation: !!Q && $ && N.has(Q),
137
+ retryConversationReload: ie,
138
+ startStream: le,
139
+ handleStop: de,
140
+ resumeIfAwaitingGeneration: fe,
141
+ restoreBufferedGeneration: ue,
142
+ isStreaming: X,
143
+ canStopStreaming: pe
342
144
  };
343
145
  };
344
146
  //#endregion
345
- export { S as useConversationStream };
147
+ export { h as useConversationStream };
@@ -0,0 +1,29 @@
1
+ import { MutableRefObject } from 'react';
2
+ import { ConversationStateAccessor, ConversationStreamOverlayNotifier, ConversationStreamTransport } from './conversation-stream.types';
3
+ import { FrameScheduler } from './frame-scheduler';
4
+ import { useStreamPathState } from './useStreamPathState';
5
+ type PathState = ReturnType<typeof useStreamPathState>;
6
+ interface UseStopGenerationParams {
7
+ conversationId: string | undefined;
8
+ conversationRef: ConversationStateAccessor['conversationRef'];
9
+ transport: ConversationStreamTransport;
10
+ overlay: ConversationStreamOverlayNotifier | undefined;
11
+ onStopError: ((error: Error) => void) | undefined;
12
+ frameScheduler: FrameScheduler;
13
+ mountedRef: MutableRefObject<boolean>;
14
+ activeGenerationIdsRef: PathState['activeGenerationIdsRef'];
15
+ resumedGenerationIdsRef: PathState['resumedGenerationIdsRef'];
16
+ bufferedGenerationsRef: PathState['bufferedGenerationsRef'];
17
+ stoppedGenerationIdsRef: PathState['stoppedGenerationIdsRef'];
18
+ stopRequestedIdsRef: PathState['stopRequestedIdsRef'];
19
+ streamingPathsRef: PathState['streamingPathsRef'];
20
+ }
21
+ /**
22
+ * Stop for the displayed conversation's generation: resolves its id (local,
23
+ * then a pending background generation, then a resumed one), flushes pending
24
+ * display writes, and asks the backend to stop it. A `404` within a short
25
+ * grace window is the generation ending on its own and is not reported.
26
+ */
27
+ export declare const useStopGeneration: ({ conversationId, conversationRef, transport, overlay, onStopError, frameScheduler, mountedRef, activeGenerationIdsRef, resumedGenerationIdsRef, bufferedGenerationsRef, stoppedGenerationIdsRef, stopRequestedIdsRef, streamingPathsRef, }: UseStopGenerationParams) => () => void;
28
+ export {};
29
+ //# sourceMappingURL=useStopGeneration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStopGeneration.d.ts","sourceRoot":"","sources":["../../../src/conversation/useConversationStream/useStopGeneration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,gBAAgB,EAAe,MAAM,OAAO,CAAC;AAG3D,OAAO,KAAK,EACV,yBAAyB,EACzB,iCAAiC,EACjC,2BAA2B,EAC5B,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAOxD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,KAAK,SAAS,GAAG,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAEvD,UAAU,uBAAuB;IAC/B,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,eAAe,EAAE,yBAAyB,CAAC,iBAAiB,CAAC,CAAC;IAC9D,SAAS,EAAE,2BAA2B,CAAC;IACvC,OAAO,EAAE,iCAAiC,GAAG,SAAS,CAAC;IACvD,WAAW,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;IAClD,cAAc,EAAE,cAAc,CAAC;IAE/B,UAAU,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACtC,sBAAsB,EAAE,SAAS,CAAC,wBAAwB,CAAC,CAAC;IAC5D,uBAAuB,EAAE,SAAS,CAAC,yBAAyB,CAAC,CAAC;IAC9D,sBAAsB,EAAE,SAAS,CAAC,wBAAwB,CAAC,CAAC;IAC5D,uBAAuB,EAAE,SAAS,CAAC,yBAAyB,CAAC,CAAC;IAC9D,mBAAmB,EAAE,SAAS,CAAC,qBAAqB,CAAC,CAAC;IACtD,iBAAiB,EAAE,SAAS,CAAC,mBAAmB,CAAC,CAAC;CACnD;AAED;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAAI,6OAc/B,uBAAuB,eAiFzB,CAAC"}
@@ -0,0 +1,48 @@
1
+ import { StopCompletionError as e } from "../create-chat-stream-api.js";
2
+ import { getConversationPath as t } from "./conversation-path.js";
3
+ import { STOP_NOT_FOUND_GRACE_MS as n, findResumedBackgroundGenerationId as r } from "./stream-errors.js";
4
+ import { useCallback as i } from "react";
5
+ //#region src/conversation/useConversationStream/useStopGeneration.ts
6
+ var a = ({ conversationId: a, conversationRef: o, transport: s, overlay: c, onStopError: l, frameScheduler: u, mountedRef: d, activeGenerationIdsRef: f, resumedGenerationIdsRef: p, bufferedGenerationsRef: m, stoppedGenerationIdsRef: h, stopRequestedIdsRef: g, streamingPathsRef: _ }) => i(() => {
7
+ if (!a) return;
8
+ let i = t(a), v = f.current.get(i), y = v ?? r(o.current) ?? p.current.get(i);
9
+ if (!y || g.current.has(y)) return;
10
+ g.current.add(y), h.current.add(y), u.flush(i), c?.notifyStopGenerating?.();
11
+ let b = m.current.get(i)?.message.content, x = () => f.current.get(i) === y || v == null && _.current.has(i), S = async () => {
12
+ let e = n / 100;
13
+ for (let t = 0; t < e; t++) {
14
+ if (!x() || !d.current) return !0;
15
+ await new Promise((e) => setTimeout(e, 100));
16
+ }
17
+ return !x();
18
+ };
19
+ (async () => {
20
+ try {
21
+ await s.stopCompletion({
22
+ generationId: y,
23
+ path: i,
24
+ content: b
25
+ });
26
+ } catch (t) {
27
+ let n = t instanceof Error ? t : Error(String(t));
28
+ if (n instanceof e && n.status === 404 && await S()) return;
29
+ g.current.delete(y), l?.(n);
30
+ }
31
+ })();
32
+ }, [
33
+ f,
34
+ m,
35
+ a,
36
+ o,
37
+ u,
38
+ d,
39
+ l,
40
+ c,
41
+ p,
42
+ g,
43
+ h,
44
+ _,
45
+ s
46
+ ]);
47
+ //#endregion
48
+ export { a as useStopGeneration };
@@ -0,0 +1,39 @@
1
+ import { MutableRefObject } from 'react';
2
+ import { BufferedGeneration } from './buffered-generation';
3
+ import { FrameScheduler } from './frame-scheduler';
4
+ interface UseStreamPathStateParams {
5
+ conversationId: string | undefined;
6
+ frameScheduler: FrameScheduler;
7
+ mountedRef: MutableRefObject<boolean>;
8
+ clearReloadError: (path: string) => void;
9
+ }
10
+ /**
11
+ * Per-path generation state of `useConversationStream`: which paths are
12
+ * streaming and which offer Stop, and the refs that own each path's local,
13
+ * latest, resumed and stopped generation ids, its live buffered message and
14
+ * its resume reservation, plus the displayed-conversation guard that keeps a
15
+ * stream from writing into a conversation that is no longer shown.
16
+ * `streamingPathsRef` is synced during render so Stop polling reads the
17
+ * latest set.
18
+ */
19
+ export declare const useStreamPathState: ({ conversationId, frameScheduler, mountedRef, clearReloadError, }: UseStreamPathStateParams) => {
20
+ streamingPaths: Set<string>;
21
+ streamingPathsRef: import('react').RefObject<Set<string>>;
22
+ stoppablePaths: Set<string>;
23
+ activeGenerationIdsRef: import('react').RefObject<Map<string, string>>;
24
+ resumedGenerationIdsRef: import('react').RefObject<Map<string, string>>;
25
+ resumingPathsRef: import('react').RefObject<Set<string>>;
26
+ bufferedGenerationsRef: import('react').RefObject<Map<string, BufferedGeneration>>;
27
+ stoppedGenerationIdsRef: import('react').RefObject<Set<string>>;
28
+ stopRequestedIdsRef: import('react').RefObject<Set<string>>;
29
+ latestGenerationIdsRef: import('react').RefObject<Map<string, string>>;
30
+ isPathDisplayed: (path: string) => boolean;
31
+ addStreamingPath: (path: string) => void;
32
+ removeStreamingPath: (path: string) => void;
33
+ setPathStoppable: (path: string, isStoppable: boolean) => void;
34
+ releaseActiveGeneration: (path: string, genId: string) => void;
35
+ rememberResumedGenerationId: (path: string, generationId: string) => void;
36
+ forgetResumedGenerationId: (path: string, generationId: string) => void;
37
+ };
38
+ export {};
39
+ //# sourceMappingURL=useStreamPathState.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useStreamPathState.d.ts","sourceRoot":"","sources":["../../../src/conversation/useConversationStream/useStreamPathState.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,gBAAgB,EAKtB,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,UAAU,wBAAwB;IAChC,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC;IAEnC,cAAc,EAAE,cAAc,CAAC;IAE/B,UAAU,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC;IAEtC,gBAAgB,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC1C;AAED;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,mEAKhC,wBAAwB;;;;;;;;;;;4BAwDhB,MAAM,KAAG,OAAO;6BAOhB,MAAM;gCAYgC,MAAM;6BAST,MAAM,eAAe,OAAO;oCAY/D,MAAM,SAAS,MAAM;wCAarB,MAAM,gBAAgB,MAAM;sCAU5B,MAAM,gBAAgB,MAAM;CA6BtC,CAAC"}