@epam/ai-dial-chat-hooks 1.2.0-dev.99 → 1.2.0-rc.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/api-error/api-error.js +7 -4
- package/catalog/catalog-derivations.js +1 -1
- package/catalog/deployment-id.js +8 -2
- package/catalog/map-deployment-limits-to-catalog.js +16 -24
- package/catalog/map-deployment-limits-to-input.js +81 -9
- package/catalog/map-deployment-to-catalog-item.js +34 -34
- package/catalog/map-entity-details-to-catalog.js +237 -192
- package/catalog/map-skill-to-catalog-item.js +61 -54
- package/catalog/publish.js +4 -4
- package/catalog/useCatalogEditNavigation/useCatalogEditNavigation.js +122 -101
- package/catalog/useCatalogItemDetails.js +9 -8
- package/catalog/usePublishFolders/usePublishFolders.js +1 -1
- package/catalog.d.ts +171 -34
- package/catalog.js +23 -23
- package/conversation/conversation-transfer/export-conversation.js +24 -16
- package/conversation/conversation-transfer/import-conversation.js +80 -31
- package/conversation/create-chat-stream-api.js +85 -49
- package/conversation/get-model-id-from-conversation-id.js +15 -9
- package/conversation/message-utils.js +44 -2
- package/conversation/stage.js +6 -2
- package/conversation/useActiveConversationSync/useActiveConversationSync.js +7 -8
- package/conversation/useConversationExport/useConversationExport.js +9 -9
- package/conversation/useConversationHandlers/message-utils.js +8 -5
- package/conversation/useConversationHandlers/useConversationHandlers.js +181 -167
- package/conversation/useConversationImport/useConversationImport.js +123 -121
- package/conversation/useConversationPanelItems/useConversationPanelItems.js +49 -31
- package/conversation/useConversationStream/frame-scheduler.js +39 -0
- package/conversation/useConversationStream/generation-resume.js +122 -64
- package/conversation/useConversationStream/useConversationStream.js +277 -118
- package/conversation/useMessageSelectionReply/useMessageSelectionReply.js +189 -0
- package/conversation-sources/useConversationSources/useConversationSources.js +31 -24
- package/conversation-sources.d.ts +1 -1
- package/conversation-transfer.d.ts +2 -1
- package/conversation.d.ts +205 -22
- package/conversation.js +32 -31
- package/file-manager-canvas.d.ts +27 -7
- package/file-manager.d.ts +87 -32
- package/file-manager.js +25 -24
- package/files/attachment-canvas.js +67 -44
- package/files/dial-file-manager-copy-move.util.js +9 -8
- package/files/dial-file-manager-mapping.util.js +2 -1
- package/files/dial-file-manager-path.util.js +24 -2
- package/files/dial-file-manager.model.js +23 -19
- package/files/dial-file-manager.types.js +3 -3
- package/files/file-manager-variant.js +1 -1
- package/files/resolve-dial-file-api-path.js +11 -12
- package/files/useDialFileListing/useDialFileListing.js +140 -127
- package/files/useDialFileManager/useDialFileManager.js +139 -135
- package/files/useDialFileManagerSections/useDialFileManagerSections.js +155 -0
- package/files/useDialFileManagerTabConfig/useDialFileManagerTabConfig.js +20 -15
- package/files/useDialFileMutations/useDialFileMutations.js +65 -60
- package/files/useDialFileUploadBatch/useDialFileUploadBatch.js +117 -110
- package/files/useFileAttachmentPicker/useFileAttachmentPicker.js +28 -23
- package/index.d.ts +630 -128
- package/index.js +136 -134
- package/mcp-apps/useMcpAppTools/useMcpAppTools.js +1 -1
- package/oauth/authorize-url.js +3 -3
- package/oauth/toolset-id.js +5 -3
- package/oauth/useToolsetLogin/useToolsetLogin.js +8 -8
- package/oauth.d.ts +34 -2
- package/oauth.js +9 -9
- package/package.json +19 -23
- package/scheduled-task/scheduled-task-description.js +43 -39
- package/scheduled-task/scheduled-task-trigger.js +74 -71
- package/scheduled-task/use-scheduled-task-runs.js +162 -50
- package/scheduled-task/use-scheduled-tasks.js +52 -48
- package/scheduled-tasks.d.ts +27 -6
- package/scheduled-tasks.js +2 -2
- package/shared/application-schema.js +5 -2
- package/shared/cron-day-of-month.js +13 -0
- package/skill/skill-archive.js +19 -0
- package/skill/skill-types.js +8 -7
- package/skill/skill.js +52 -29
- package/skill/useSkillArchiveImport/useSkillArchiveImport.js +38 -34
- package/skill/useSkillEditorLoad.js +65 -59
- package/skill/useSkillEditorSubmit.js +105 -93
- package/skill/useSkillFileActions.js +70 -58
- package/skill-editor.d.ts +62 -13
- package/skill-editor.js +9 -9
- package/usage/map-user-usage-to-model-limits.js +137 -95
- package/usage.d.ts +8 -3
- package/usePageFileDrag/usePageFileDrag.js +3 -3
- package/utils.d.ts +26 -6
- package/utils.js +10 -10
|
@@ -1,16 +1,40 @@
|
|
|
1
1
|
import { safeDecodeURI as e } from "../../shared/string-utils.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { DEFAULT_GENERATION_PERSISTENCE_ERROR_MESSAGE as t, GenerationPersistenceError as n } from "../create-chat-stream-api.js";
|
|
3
|
+
import { getConversationPath as r } from "./conversation-path.js";
|
|
4
|
+
import { applyChunkToMessages as i } from "./apply-chunk.js";
|
|
5
|
+
import { restoreBufferedMessage as a } from "./buffered-generation.js";
|
|
6
|
+
import { BackgroundGenerationStatus as o, MessageRole as s } from "@epam/ai-dial-chat-shared";
|
|
6
7
|
//#region src/conversation/useConversationStream/generation-resume.ts
|
|
7
|
-
var
|
|
8
|
+
var c = (e) => {
|
|
8
9
|
let t = e.custom_content;
|
|
9
10
|
return !!e.content || e.responseId != null || !!t?.attachments?.length || !!t?.stages?.length || !!t?.annotations?.length || !!t?.form_schema || t?.state != null;
|
|
10
|
-
}, o = (e) => {
|
|
11
|
+
}, l = (e) => e.messages.findIndex((e) => e.backgroundGeneration?.status === o.Pending), u = (e) => {
|
|
12
|
+
if (l(e) !== -1) return !0;
|
|
11
13
|
let t = e.messages[e.messages.length - 1];
|
|
12
|
-
return !!t && t.role ===
|
|
13
|
-
},
|
|
14
|
+
return !!t && t.role === s.Assistant && !c(t) && t.streamErrorMessage == null && !t.wasStoppedByUser;
|
|
15
|
+
}, d = 300 * 1e3, f = "awaiting-resume", p = [
|
|
16
|
+
1e3,
|
|
17
|
+
2e3,
|
|
18
|
+
4e3,
|
|
19
|
+
8e3,
|
|
20
|
+
16e3
|
|
21
|
+
], m = (e) => new Promise((t) => {
|
|
22
|
+
let n = typeof window < "u", r = () => {
|
|
23
|
+
clearTimeout(i), n && window.removeEventListener("online", r), t();
|
|
24
|
+
}, i = setTimeout(r, e);
|
|
25
|
+
n && window.addEventListener("online", r);
|
|
26
|
+
}), h = async (e, t, n) => {
|
|
27
|
+
for (let r = 0; r <= p.length; r++) {
|
|
28
|
+
if (t()) return null;
|
|
29
|
+
try {
|
|
30
|
+
let t = await e();
|
|
31
|
+
if (!n?.(t)) return t;
|
|
32
|
+
} catch {}
|
|
33
|
+
if (r === p.length) return null;
|
|
34
|
+
await m(p[r]);
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}, g = async (e, t) => {
|
|
14
38
|
let n = e.getReader(), r = new TextDecoder(), i = "";
|
|
15
39
|
try {
|
|
16
40
|
for (;;) {
|
|
@@ -39,88 +63,122 @@ var a = (e) => {
|
|
|
39
63
|
} catch {} finally {
|
|
40
64
|
n.releaseLock();
|
|
41
65
|
}
|
|
42
|
-
},
|
|
43
|
-
if (!
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
let
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
66
|
+
}, _ = ({ transport: o, setConversation: s, conversationRef: p, resumingPathsRef: m, bufferedGenerationsRef: h, addStreamingPath: _, removeStreamingPath: v, isPathDisplayed: y, generationPersistenceErrorMessage: b = t, frameScheduler: x, stoppedGenerationIdsRef: S, onReloadError: C, onReloadSuccess: w }) => (t, T, E = {}) => {
|
|
67
|
+
if (!u(T)) {
|
|
68
|
+
E.onSettled?.();
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
let D = r(t);
|
|
72
|
+
if (!E.skipDedupe && m.current.has(D)) return;
|
|
73
|
+
m.current.add(D), _(D);
|
|
74
|
+
let O = l(T), k = O !== -1, A = k ? O : T.messages.length - 1, j = {
|
|
75
|
+
generationId: f,
|
|
76
|
+
messageIndex: A,
|
|
77
|
+
message: E.seedMessage ?? T.messages[A]
|
|
78
|
+
};
|
|
79
|
+
h.current.set(D, j);
|
|
80
|
+
let M = () => h.current.get(D) === j, N = k ? T.messages[A].backgroundGeneration?.generationId : void 0, P = () => N != null && (S?.current.has(N) ?? !1), F = (e, t = !1) => {
|
|
81
|
+
if (x?.flush(D), N != null && S?.current.delete(N), !M()) return;
|
|
82
|
+
m.current.delete(D), v(D);
|
|
83
|
+
let n = !k && e && e.messages.length - 1 === A && u(e) && c(j.message);
|
|
84
|
+
if (t || n) {
|
|
85
|
+
j.message = {
|
|
86
|
+
...j.message,
|
|
87
|
+
streamErrorMessage: b
|
|
88
|
+
}, y(D) && s((e) => {
|
|
89
|
+
if (!e || !M() || !y(D)) return e;
|
|
90
|
+
let t = a(e, j);
|
|
91
|
+
return p.current = t, t;
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
h.current.delete(D), e && y(D) && (s(e), p.current = e);
|
|
96
|
+
}, I = async () => {
|
|
97
|
+
if (M()) try {
|
|
98
|
+
let n = await o.getConversation(e(t));
|
|
99
|
+
if (!M()) return;
|
|
100
|
+
w?.(D), F(n);
|
|
52
101
|
} catch {
|
|
53
|
-
|
|
102
|
+
if (!M()) return;
|
|
103
|
+
x?.flush(D), m.current.delete(D), v(D), N != null && S?.current.delete(N), C?.(D, I, M);
|
|
54
104
|
}
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
let t =
|
|
68
|
-
if (t
|
|
69
|
-
|
|
70
|
-
|
|
105
|
+
}, L = (e) => {
|
|
106
|
+
x?.cancel(D), M() && (j.message = e, y(D) && s((e) => {
|
|
107
|
+
if (!e || !M() || !y(D)) return e;
|
|
108
|
+
let t = a(e, j);
|
|
109
|
+
return p.current = t, t;
|
|
110
|
+
}));
|
|
111
|
+
}, R = () => s((e) => {
|
|
112
|
+
if (!e || !M() || !y(D)) return e;
|
|
113
|
+
let t = a(e, j);
|
|
114
|
+
return p.current = t, t;
|
|
115
|
+
}), z = (e) => {
|
|
116
|
+
if (!M() || P()) return;
|
|
117
|
+
let t = i([j.message], 0, e);
|
|
118
|
+
if (t && (j.message = t[0]), y(D)) {
|
|
119
|
+
if (x) {
|
|
120
|
+
x.schedule(D, R);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
R();
|
|
71
124
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
let t = f.current.get(v);
|
|
75
|
-
if (t?.generationId !== c) return e;
|
|
76
|
-
let n = r(e, t);
|
|
77
|
-
return u.current = n, n;
|
|
78
|
-
});
|
|
79
|
-
}, w = async () => {
|
|
80
|
-
let t = new AbortController(), n;
|
|
125
|
+
}, B = async () => {
|
|
126
|
+
let n = new AbortController(), r;
|
|
81
127
|
try {
|
|
82
|
-
|
|
128
|
+
r = await o.watchConversation(D, n.signal);
|
|
83
129
|
} catch {
|
|
84
|
-
await
|
|
130
|
+
await I();
|
|
85
131
|
return;
|
|
86
132
|
}
|
|
87
|
-
let r = !1, a = window.setTimeout(() => {
|
|
88
|
-
t.abort();
|
|
89
|
-
}, s);
|
|
90
133
|
try {
|
|
91
|
-
|
|
92
|
-
|
|
134
|
+
let i = await o.getConversation(e(t));
|
|
135
|
+
if (!u(i)) {
|
|
136
|
+
n.abort(), r.cancel().catch(() => void 0), F(i);
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
} catch {}
|
|
140
|
+
let i = !1, a = window.setTimeout(() => {
|
|
141
|
+
n.abort();
|
|
142
|
+
}, d);
|
|
143
|
+
try {
|
|
144
|
+
await g(r, async (n) => {
|
|
145
|
+
if (n?.action !== "UPDATE") return !1;
|
|
93
146
|
try {
|
|
94
|
-
let
|
|
95
|
-
if (!
|
|
147
|
+
let n = await o.getConversation(e(t));
|
|
148
|
+
if (!u(n)) return F(n), i = !0, !0;
|
|
96
149
|
} catch {}
|
|
97
150
|
return !1;
|
|
98
151
|
});
|
|
99
152
|
} finally {
|
|
100
153
|
clearTimeout(a);
|
|
101
154
|
}
|
|
102
|
-
|
|
103
|
-
},
|
|
155
|
+
i || await I();
|
|
156
|
+
}, V = async () => {
|
|
104
157
|
let e = new AbortController(), t;
|
|
105
158
|
try {
|
|
106
|
-
t = await
|
|
159
|
+
t = await o.attachToGeneration(D, e.signal);
|
|
107
160
|
} catch {
|
|
108
161
|
return !1;
|
|
109
162
|
}
|
|
110
|
-
let
|
|
111
|
-
return await
|
|
163
|
+
let r = !1, i = !1;
|
|
164
|
+
return await g(t, (e) => {
|
|
165
|
+
if (!M()) return !0;
|
|
112
166
|
switch (e.type) {
|
|
113
|
-
case "snapshot": return
|
|
114
|
-
case "chunk": return
|
|
167
|
+
case "snapshot": return L(e.message), !1;
|
|
168
|
+
case "chunk": return z(e.chunk), !1;
|
|
115
169
|
case "done":
|
|
116
170
|
case "error":
|
|
117
|
-
case "stopped": return n = !0, !0;
|
|
171
|
+
case "stopped": return i = e.type === "error" && e.errorType === n.type, r = !0, !0;
|
|
118
172
|
}
|
|
119
|
-
}),
|
|
173
|
+
}), r ? (i ? F(void 0, !0) : await I(), !0) : !M();
|
|
120
174
|
};
|
|
121
175
|
(async () => {
|
|
122
|
-
|
|
176
|
+
try {
|
|
177
|
+
await V() || await B();
|
|
178
|
+
} finally {
|
|
179
|
+
E.onSettled?.();
|
|
180
|
+
}
|
|
123
181
|
})();
|
|
124
182
|
};
|
|
125
183
|
//#endregion
|
|
126
|
-
export {
|
|
184
|
+
export { _ as createResumeIfAwaitingGeneration, h as fetchConversationForRecovery, l as findPendingBackgroundMessageIndex, c as hasGeneratedPayload, u as isAwaitingGenerationResume };
|
|
@@ -1,156 +1,315 @@
|
|
|
1
1
|
import { safeDecodeURI as e } from "../../shared/string-utils.js";
|
|
2
|
-
import { DEFAULT_GENERATION_CONFLICT_MESSAGE as t,
|
|
3
|
-
import { getConversationPath as
|
|
4
|
-
import { applyChunkToMessages as
|
|
5
|
-
import { restoreBufferedMessage as
|
|
6
|
-
import { createResumeIfAwaitingGeneration as
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
2
|
+
import { DEFAULT_GENERATION_CONFLICT_MESSAGE as t, DEFAULT_GENERATION_PERSISTENCE_ERROR_MESSAGE as n, GenerationConflictError as r, GenerationPersistenceError as i, StreamInterruptedError as ee, StreamUpstreamError as a } from "../create-chat-stream-api.js";
|
|
3
|
+
import { getConversationPath as o } from "./conversation-path.js";
|
|
4
|
+
import { applyChunkToMessages as s } from "./apply-chunk.js";
|
|
5
|
+
import { restoreBufferedMessage as c } from "./buffered-generation.js";
|
|
6
|
+
import { createResumeIfAwaitingGeneration as te, fetchConversationForRecovery as ne, findPendingBackgroundMessageIndex as l, hasGeneratedPayload as re, isAwaitingGenerationResume as u } from "./generation-resume.js";
|
|
7
|
+
import { createFrameScheduler as ie } from "./frame-scheduler.js";
|
|
8
|
+
import { MessageRole as d, generateUUID as ae } from "@epam/ai-dial-chat-shared";
|
|
9
|
+
import { useCallback as f, useEffect as p, useMemo as oe, useRef as m, useState as h } from "react";
|
|
10
|
+
import { SendCompletionDtoModeEnum as g } from "@epam/ai-dial-chat-api-client";
|
|
10
11
|
//#region src/conversation/useConversationStream/useConversationStream.ts
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
l(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
let
|
|
17
|
-
|
|
12
|
+
var se = 2e4, ce = (e, t, n) => e instanceof i ? n : e instanceof r ? t : e instanceof a ? e.message : "", _ = (e) => {
|
|
13
|
+
if (!e) return;
|
|
14
|
+
let t = l(e);
|
|
15
|
+
return t === -1 ? void 0 : e.messages[t].backgroundGeneration?.generationId;
|
|
16
|
+
}, v = ({ conversationId: a, state: { setConversation: v, conversationRef: y }, transport: b, generation: { startGeneration: le, completeGeneration: x }, channel: S, overlay: C, onStopError: w, generationConflictMessage: ue = t, generationPersistenceErrorMessage: T = n, onStreamError: E, batchChunksPerFrame: D = !1 }) => {
|
|
17
|
+
let O = m(E);
|
|
18
|
+
p(() => {
|
|
19
|
+
O.current = E;
|
|
20
|
+
}, [E]);
|
|
21
|
+
let de = m(D);
|
|
22
|
+
p(() => {
|
|
23
|
+
de.current = D;
|
|
24
|
+
}, [D]);
|
|
25
|
+
let [k] = h(ie), [A, j] = h(() => /* @__PURE__ */ new Set()), [M, N] = h(() => /* @__PURE__ */ new Set()), P = m(/* @__PURE__ */ new Map()), F = m(/* @__PURE__ */ new Set()), I = m(/* @__PURE__ */ new Map()), L = m(/* @__PURE__ */ new Set()), fe = m(/* @__PURE__ */ new Map()), R = m(!0), z = m(/* @__PURE__ */ new Map()), [B, V] = h(z.current), H = m(/* @__PURE__ */ new Set()), [pe, U] = h(H.current);
|
|
26
|
+
p(() => (R.current = !0, () => {
|
|
27
|
+
R.current = !1;
|
|
28
|
+
}), []);
|
|
29
|
+
let W = f((e) => {
|
|
30
|
+
if (!z.current.has(e)) return;
|
|
31
|
+
let t = new Map(z.current);
|
|
32
|
+
t.delete(e), z.current = t, R.current && V(t);
|
|
33
|
+
}, []), G = f((e, t, n) => {
|
|
34
|
+
if (!R.current || !n()) return;
|
|
35
|
+
let r = new Map(z.current);
|
|
36
|
+
r.set(e, {
|
|
37
|
+
retry: t,
|
|
38
|
+
isCurrent: n
|
|
39
|
+
}), z.current = r, V(r);
|
|
40
|
+
}, []), K = f(async () => {
|
|
41
|
+
if (!a) return;
|
|
42
|
+
let e = z.current.get(o(a));
|
|
43
|
+
if (!(!e || !e.isCurrent() || H.current.has(e))) {
|
|
44
|
+
H.current.add(e), U(new Set(H.current));
|
|
45
|
+
try {
|
|
46
|
+
await e.retry();
|
|
47
|
+
} finally {
|
|
48
|
+
H.current.delete(e), R.current && U(new Set(H.current));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}, [a]), me = m(a);
|
|
52
|
+
p(() => (me.current = a, () => k.cancelAll()), [a, k]);
|
|
53
|
+
let q = f((e) => R.current && o(me.current ?? "") === e, []), J = f((e) => {
|
|
54
|
+
W(e), j((t) => {
|
|
18
55
|
if (t.has(e)) return t;
|
|
19
56
|
let n = new Set(t);
|
|
20
57
|
return n.add(e), n;
|
|
21
58
|
});
|
|
22
|
-
}, []),
|
|
23
|
-
|
|
59
|
+
}, [W]), Y = f((e) => {
|
|
60
|
+
j((t) => {
|
|
24
61
|
if (!t.has(e)) return t;
|
|
25
62
|
let n = new Set(t);
|
|
26
63
|
return n.delete(e), n;
|
|
27
64
|
});
|
|
28
|
-
}, []),
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
65
|
+
}, []), X = f((e, t) => {
|
|
66
|
+
N((n) => {
|
|
67
|
+
if (n.has(e) === t) return n;
|
|
68
|
+
let r = new Set(n);
|
|
69
|
+
return t ? r.add(e) : r.delete(e), r;
|
|
70
|
+
});
|
|
71
|
+
}, []), he = f((e, t) => {
|
|
72
|
+
P.current.get(e) === t && (P.current.delete(e), X(e, !1));
|
|
73
|
+
}, [X]), Z = oe(() => te({
|
|
74
|
+
transport: b,
|
|
75
|
+
setConversation: v,
|
|
76
|
+
conversationRef: y,
|
|
77
|
+
resumingPathsRef: F,
|
|
78
|
+
bufferedGenerationsRef: I,
|
|
79
|
+
addStreamingPath: J,
|
|
80
|
+
removeStreamingPath: Y,
|
|
81
|
+
isPathDisplayed: q,
|
|
82
|
+
generationPersistenceErrorMessage: T,
|
|
83
|
+
frameScheduler: D ? k : void 0,
|
|
84
|
+
stoppedGenerationIdsRef: L,
|
|
85
|
+
onReloadError: G,
|
|
86
|
+
onReloadSuccess: W
|
|
87
|
+
}), [
|
|
88
|
+
J,
|
|
89
|
+
Y,
|
|
90
|
+
q,
|
|
91
|
+
b,
|
|
92
|
+
T,
|
|
93
|
+
D,
|
|
94
|
+
k,
|
|
95
|
+
G,
|
|
96
|
+
W
|
|
97
|
+
]), ge = f((t, n, a, te, ie, f, p = g.Append, oe = {}) => {
|
|
98
|
+
let m = f ?? ae(), h = o(t);
|
|
99
|
+
k.flush(h), F.current.delete(h), P.current.set(h, m), fe.current.set(h, m), X(h, !0);
|
|
100
|
+
let _;
|
|
101
|
+
p === g.Regenerate ? _ = a : p === g.Edit && (_ = a - 1);
|
|
102
|
+
let w = () => fe.current.get(h) !== m, E = le(h, m), D = y.current?.messages[a];
|
|
103
|
+
D ? I.current.set(h, {
|
|
104
|
+
generationId: m,
|
|
105
|
+
messageIndex: a,
|
|
106
|
+
message: D
|
|
107
|
+
}) : I.current.delete(h), J(h), C?.notifyGenerationStart?.(), S?.ensureConnected();
|
|
108
|
+
let A = () => {
|
|
109
|
+
w() || Y(h), he(h, m), x(h, m), S?.notifyGenerationSettled?.();
|
|
110
|
+
}, j = () => {
|
|
111
|
+
if (w()) return;
|
|
112
|
+
let e = I.current.get(h);
|
|
113
|
+
!e || e.generationId !== m || (e.message = {
|
|
114
|
+
...e.message,
|
|
115
|
+
streamErrorMessage: T
|
|
116
|
+
}, O.current?.(new i()), q(h) && v((t) => {
|
|
117
|
+
if (!t || w() || !q(h)) return t;
|
|
118
|
+
let n = c(t, e);
|
|
119
|
+
return y.current = n, n;
|
|
120
|
+
}));
|
|
121
|
+
}, M = I.current.get(h), N = () => R.current && !w() && I.current.get(h) === M, z = async () => {
|
|
122
|
+
if (N()) {
|
|
123
|
+
if (!q(h)) {
|
|
124
|
+
M && I.current.delete(h);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
let n = await b.getConversation(e(t));
|
|
129
|
+
if (!N()) return;
|
|
130
|
+
if (W(h), !q(h)) {
|
|
131
|
+
M && I.current.delete(h);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (l(n) !== -1) {
|
|
135
|
+
Z(t, n, { seedMessage: M?.message });
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (M && M.messageIndex === n.messages.length - 1 && u(n) && re(M.message)) {
|
|
139
|
+
j();
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
M && I.current.delete(h), v(n), y.current = n;
|
|
143
|
+
} catch {
|
|
144
|
+
N() && G(h, z, N);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}, B = {
|
|
148
|
+
signal: E.signal,
|
|
41
149
|
onChunk: (e) => {
|
|
42
|
-
if (
|
|
43
|
-
let t =
|
|
44
|
-
if (t?.generationId ===
|
|
45
|
-
let n =
|
|
150
|
+
if (w()) return;
|
|
151
|
+
let t = I.current.get(h);
|
|
152
|
+
if (t?.generationId === m) {
|
|
153
|
+
let n = s([t.message], 0, e);
|
|
46
154
|
n && (t.message = n[0]);
|
|
47
155
|
}
|
|
48
|
-
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
156
|
+
if (q(h)) {
|
|
157
|
+
if (de.current && t?.generationId === m) {
|
|
158
|
+
k.schedule(h, () => {
|
|
159
|
+
let e = I.current.get(h);
|
|
160
|
+
if (w() || !q(h) || e?.generationId !== m) return;
|
|
161
|
+
let t = { ...e };
|
|
162
|
+
v((e) => {
|
|
163
|
+
if (!e) return e;
|
|
164
|
+
let n = c(e, t);
|
|
165
|
+
return y.current = n, n;
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
return;
|
|
59
169
|
}
|
|
60
|
-
|
|
61
|
-
|
|
170
|
+
v((t) => {
|
|
171
|
+
if (!t) return t;
|
|
172
|
+
let n = I.current.get(h), r;
|
|
173
|
+
if (n?.generationId === m) r = c(t, n);
|
|
174
|
+
else {
|
|
175
|
+
let n = s(t.messages, a, e);
|
|
176
|
+
if (!n) return t;
|
|
177
|
+
r = {
|
|
178
|
+
...t,
|
|
179
|
+
messages: n
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
return y.current = r, r;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
62
185
|
},
|
|
63
186
|
onComplete: async () => {
|
|
64
|
-
|
|
65
|
-
let n = await v.getConversation(e(t));
|
|
66
|
-
if (E() || !I(C)) return;
|
|
67
|
-
g(n), _.current = n;
|
|
68
|
-
} catch {}
|
|
187
|
+
k.flush(h), A(), L.current.has(m) ? L.current.delete(m) : w() || C?.notifyGenerationEnd?.(), await z();
|
|
69
188
|
},
|
|
70
189
|
onError: (e) => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
190
|
+
if (k.flush(h), O.current?.(e), e instanceof ee && !w()) {
|
|
191
|
+
pe(e);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (e instanceof r && oe.resumeOnConflict && !w()) {
|
|
195
|
+
U(e);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
V(e);
|
|
199
|
+
}
|
|
200
|
+
}, V = (e) => {
|
|
201
|
+
let t = I.current.get(h), n = t?.generationId === m ? t : void 0;
|
|
202
|
+
if (n && (e instanceof i ? n.message = {
|
|
203
|
+
...n.message,
|
|
204
|
+
streamErrorMessage: T
|
|
205
|
+
} : I.current.delete(h)), A(), w() || !q(h)) return;
|
|
206
|
+
let r = ce(e, ue, T);
|
|
207
|
+
v((e) => {
|
|
208
|
+
if (!e || w() || !q(h)) return e;
|
|
209
|
+
let t = n?.generationId === m ? c(e, n) : e, i = {
|
|
210
|
+
...t,
|
|
211
|
+
messages: t.messages.map((e, t) => t === a ? {
|
|
212
|
+
...e,
|
|
213
|
+
streamErrorMessage: r
|
|
214
|
+
} : e)
|
|
215
|
+
};
|
|
216
|
+
return y.current = i, i;
|
|
217
|
+
});
|
|
218
|
+
}, H = () => {
|
|
219
|
+
A(), L.current.has(m) ? L.current.delete(m) : w() || C?.notifyGenerationEnd?.();
|
|
220
|
+
}, pe = async (n) => {
|
|
221
|
+
F.current.add(h), K(await ne(() => b.getConversation(e(t)), w), n, !1);
|
|
222
|
+
}, U = async (n) => {
|
|
223
|
+
P.current.get(h) === m && X(h, !1), F.current.add(h), K(await ne(() => b.getConversation(e(t)), w, (e) => e.messages.length === a && e.messages.at(-1)?.role === d.User), n, !0);
|
|
224
|
+
}, K = (e, n, r) => {
|
|
225
|
+
if (w()) {
|
|
226
|
+
A();
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
let i = e?.messages[e.messages.length - 1], ee = e != null && e.messages.length - 1 === a && i?.role === d.Assistant, o = r && e != null && l(e) !== -1;
|
|
230
|
+
if (!e || !ee && !o) {
|
|
231
|
+
F.current.delete(h), V(n);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
let s = I.current.get(h), c = s?.generationId === m ? s : void 0;
|
|
235
|
+
if (u(e)) {
|
|
236
|
+
Z(t, e, {
|
|
237
|
+
seedMessage: ee ? c?.message : void 0,
|
|
238
|
+
skipDedupe: !0,
|
|
239
|
+
onSettled: H
|
|
84
240
|
});
|
|
241
|
+
return;
|
|
85
242
|
}
|
|
243
|
+
F.current.delete(h), c && I.current.delete(h), q(h) && (v(e), y.current = e), H();
|
|
86
244
|
};
|
|
87
245
|
(async () => {
|
|
88
246
|
try {
|
|
89
|
-
let e =
|
|
90
|
-
if (
|
|
91
|
-
|
|
247
|
+
let e = S?.channelId ?? await S?.waitForChannel(se) ?? void 0, t = L.current.has(m);
|
|
248
|
+
if (E.signal.aborted || w() || t) {
|
|
249
|
+
I.current.get(h)?.generationId === m && I.current.delete(h), w() || Y(h), he(h, m), x(h, m), S?.notifyGenerationSettled?.(), t ? L.current.delete(m) : w() || C?.notifyGenerationEnd?.();
|
|
92
250
|
return;
|
|
93
251
|
}
|
|
94
|
-
|
|
252
|
+
b.streamCompletion(h, n, te, B, ie, m, p, _, e);
|
|
95
253
|
} catch (e) {
|
|
96
|
-
|
|
254
|
+
B.onError(e instanceof Error ? e : Error(String(e)));
|
|
97
255
|
}
|
|
98
256
|
})();
|
|
99
257
|
}, [
|
|
100
|
-
|
|
258
|
+
le,
|
|
259
|
+
x,
|
|
260
|
+
J,
|
|
261
|
+
Y,
|
|
262
|
+
X,
|
|
263
|
+
he,
|
|
264
|
+
q,
|
|
265
|
+
S?.channelId,
|
|
266
|
+
S?.ensureConnected,
|
|
267
|
+
S?.waitForChannel,
|
|
268
|
+
S?.notifyGenerationSettled,
|
|
269
|
+
C,
|
|
101
270
|
b,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
let
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
let
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
path: t
|
|
271
|
+
Z,
|
|
272
|
+
ue,
|
|
273
|
+
T,
|
|
274
|
+
k,
|
|
275
|
+
G,
|
|
276
|
+
W
|
|
277
|
+
]), _e = f((e, t) => {
|
|
278
|
+
let n = o(e), r = I.current.get(n);
|
|
279
|
+
return z.current.get(n)?.isCurrent() && r && t.messages.length - 1 >= r.messageIndex && t.messages[r.messageIndex].role === d.Assistant && !u(t) ? (W(n), I.current.delete(n), t) : r ? c(t, r) : t;
|
|
280
|
+
}, [W]), ve = f(() => {
|
|
281
|
+
if (!a) return;
|
|
282
|
+
let e = o(a), t = P.current.get(e) ?? _(y.current);
|
|
283
|
+
if (!t) return;
|
|
284
|
+
L.current.add(t), k.flush(e), C?.notifyStopGenerating?.();
|
|
285
|
+
let n = I.current.get(e)?.message.content;
|
|
286
|
+
b.stopCompletion({
|
|
287
|
+
generationId: t,
|
|
288
|
+
path: e,
|
|
289
|
+
content: n
|
|
122
290
|
}).catch((e) => {
|
|
123
291
|
let t = e instanceof Error ? e : Error(String(e));
|
|
124
|
-
|
|
125
|
-
})
|
|
292
|
+
w?.(t);
|
|
293
|
+
});
|
|
126
294
|
}, [
|
|
127
|
-
|
|
295
|
+
a,
|
|
296
|
+
y,
|
|
297
|
+
k,
|
|
298
|
+
w,
|
|
128
299
|
C,
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
]), H = u(() => o({
|
|
132
|
-
transport: v,
|
|
133
|
-
setConversation: g,
|
|
134
|
-
conversationRef: _,
|
|
135
|
-
resumingPathsRef: j,
|
|
136
|
-
bufferedGenerationsRef: M,
|
|
137
|
-
addStreamingPath: L,
|
|
138
|
-
removeStreamingPath: R,
|
|
139
|
-
isPathDisplayed: I
|
|
140
|
-
}), [
|
|
141
|
-
L,
|
|
142
|
-
R,
|
|
143
|
-
I,
|
|
144
|
-
v
|
|
145
|
-
]), U = h != null && T.has(r(h)), W = h == null ? null : r(h);
|
|
300
|
+
b
|
|
301
|
+
]), ye = f((e, t) => Z(e, t), [Z]), be = a != null && A.has(o(a)), Q = a == null ? null : o(a), xe = Q != null && (M.has(Q) || be && _(y.current) != null), $ = Q ? B.get(Q) : void 0, Se = $?.isCurrent() ?? !1;
|
|
146
302
|
return {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
303
|
+
hasConversationReloadError: Se,
|
|
304
|
+
isReloadingConversation: !!$ && Se && pe.has($),
|
|
305
|
+
retryConversationReload: K,
|
|
306
|
+
startStream: ge,
|
|
307
|
+
handleStop: ve,
|
|
308
|
+
resumeIfAwaitingGeneration: ye,
|
|
309
|
+
restoreBufferedGeneration: _e,
|
|
310
|
+
isStreaming: be,
|
|
311
|
+
canStopStreaming: xe
|
|
153
312
|
};
|
|
154
313
|
};
|
|
155
314
|
//#endregion
|
|
156
|
-
export {
|
|
315
|
+
export { v as useConversationStream };
|