@epam/ai-dial-chat-hooks 1.1.5 → 1.1.7
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/conversation/create-chat-stream-api.js +85 -49
- package/conversation/message-utils.js +44 -2
- package/conversation/useConversationStream/generation-resume.js +104 -63
- package/conversation/useConversationStream/useConversationStream.js +181 -110
- package/conversation.d.ts +85 -3
- package/conversation.js +29 -29
- package/index.d.ts +85 -3
- package/index.js +89 -89
- package/package.json +15 -15
|
@@ -1,93 +1,129 @@
|
|
|
1
1
|
//#region src/conversation/create-chat-stream-api.ts
|
|
2
|
-
var e = { "Content-Type": "application/json" }, t = 409, n = "
|
|
3
|
-
|
|
2
|
+
var e = { "Content-Type": "application/json" }, t = 409, n = "The response could not be saved. It is still shown here, but may be lost if you reload or leave this page. Copy it before continuing.", r = class extends Error {
|
|
3
|
+
static type = "conversation_save_failed";
|
|
4
|
+
constructor() {
|
|
5
|
+
super(n), this.name = "GenerationPersistenceError";
|
|
6
|
+
}
|
|
7
|
+
}, i = "A response is already being generated in this conversation. Wait for it to finish or stop it before sending another message.", a = class extends Error {
|
|
8
|
+
constructor(e = i) {
|
|
4
9
|
super(e), this.name = "GenerationConflictError";
|
|
5
10
|
}
|
|
6
|
-
},
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
}, o = class extends Error {
|
|
12
|
+
constructor(e) {
|
|
13
|
+
super(e), this.name = "StreamUpstreamError";
|
|
14
|
+
}
|
|
15
|
+
}, s = class extends Error {
|
|
16
|
+
constructor(e) {
|
|
17
|
+
super("The completion stream was interrupted", { cause: e }), this.name = "StreamInterruptedError";
|
|
18
|
+
}
|
|
19
|
+
}, c = 45e3, l = (e, t) => {
|
|
20
|
+
let n = Date.now(), r, i = !1, a = () => {
|
|
21
|
+
if (i) return;
|
|
22
|
+
let o = Date.now() - n;
|
|
23
|
+
if (o >= e) {
|
|
24
|
+
t();
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
clearTimeout(r), r = setTimeout(a, e - o);
|
|
28
|
+
}, o = () => {
|
|
29
|
+
document.visibilityState === "visible" && a();
|
|
30
|
+
}, s = typeof window < "u" && typeof document < "u";
|
|
31
|
+
return s && (document.addEventListener("visibilitychange", o), window.addEventListener("online", a), window.addEventListener("pageshow", a)), r = setTimeout(a, e), {
|
|
32
|
+
touch: () => {
|
|
33
|
+
n = Date.now();
|
|
34
|
+
},
|
|
35
|
+
stop: () => {
|
|
36
|
+
i || (i = !0, clearTimeout(r), s && (document.removeEventListener("visibilitychange", o), window.removeEventListener("online", a), window.removeEventListener("pageshow", a)));
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}, u = (e) => typeof e == "object" && !!e && e.name === "AbortError", d = (e, t, n) => {
|
|
40
|
+
let i = e.trim();
|
|
41
|
+
if (!i || i.startsWith(":") || !i.startsWith("data: ")) return;
|
|
42
|
+
let a = i.slice(6);
|
|
43
|
+
if (a !== "[DONE]") try {
|
|
44
|
+
let e = JSON.parse(a);
|
|
12
45
|
if (e.error) {
|
|
13
|
-
n(
|
|
46
|
+
n(e.error.type === r.type ? new r() : new o(e.error.message));
|
|
14
47
|
return;
|
|
15
48
|
}
|
|
16
49
|
t(e);
|
|
17
50
|
} catch {}
|
|
18
|
-
},
|
|
19
|
-
let { completionsBasePath:
|
|
51
|
+
}, f = (n) => {
|
|
52
|
+
let { completionsBasePath: r } = n, i = (...e) => (n.fetchImpl ?? fetch)(...e);
|
|
20
53
|
return {
|
|
21
|
-
streamCompletion: (
|
|
22
|
-
let { onChunk:
|
|
54
|
+
streamCompletion: (o, c, f, p, m, h, g, _, v) => {
|
|
55
|
+
let { onChunk: y, onComplete: b, onError: x, signal: S } = p;
|
|
23
56
|
(async () => {
|
|
24
|
-
let
|
|
57
|
+
let p;
|
|
25
58
|
try {
|
|
26
59
|
let t = n.getTimezone?.();
|
|
27
|
-
|
|
60
|
+
p = await i(`${r}/completions`, {
|
|
28
61
|
method: "POST",
|
|
29
62
|
credentials: "include",
|
|
30
|
-
signal:
|
|
63
|
+
signal: S,
|
|
31
64
|
headers: {
|
|
32
65
|
...e,
|
|
33
66
|
...n.getCsrfToken() == null ? {} : { "X-CSRF-Token": n.getCsrfToken() },
|
|
34
67
|
...t ? { "X-Timezone": t } : {}
|
|
35
68
|
},
|
|
36
69
|
body: JSON.stringify({
|
|
37
|
-
path:
|
|
70
|
+
path: o,
|
|
38
71
|
message: c ?? "",
|
|
39
|
-
model:
|
|
40
|
-
custom_content:
|
|
41
|
-
...
|
|
42
|
-
...
|
|
43
|
-
...
|
|
44
|
-
...
|
|
72
|
+
model: f,
|
|
73
|
+
custom_content: m || {},
|
|
74
|
+
...h != null && { generationId: h },
|
|
75
|
+
...g != null && { mode: g },
|
|
76
|
+
..._ != null && { messageIndex: _ },
|
|
77
|
+
...v != null && { clientChannelId: v }
|
|
45
78
|
})
|
|
46
79
|
});
|
|
47
80
|
} catch (e) {
|
|
48
|
-
if (e
|
|
49
|
-
|
|
81
|
+
if (u(e)) return;
|
|
82
|
+
x(new s(e));
|
|
50
83
|
return;
|
|
51
84
|
}
|
|
52
|
-
let
|
|
53
|
-
if (
|
|
54
|
-
if (
|
|
55
|
-
|
|
85
|
+
let C = p.headers.get("x-csrf-token");
|
|
86
|
+
if (C && n.setCsrfToken(C), !p.ok) {
|
|
87
|
+
if (p.status === t) {
|
|
88
|
+
x(new a());
|
|
56
89
|
return;
|
|
57
90
|
}
|
|
58
|
-
|
|
91
|
+
x(/* @__PURE__ */ Error(`Stream request failed with status ${p.status}`));
|
|
59
92
|
return;
|
|
60
93
|
}
|
|
61
|
-
if (!
|
|
62
|
-
|
|
94
|
+
if (!p.body) {
|
|
95
|
+
x(/* @__PURE__ */ Error("No response body"));
|
|
63
96
|
return;
|
|
64
97
|
}
|
|
65
|
-
let
|
|
66
|
-
|
|
67
|
-
}
|
|
98
|
+
let w = p.body.getReader(), T = new TextDecoder(), E = "", D = !1, O = !1, k = (e) => {
|
|
99
|
+
D || (D = !0, A.stop(), x(e));
|
|
100
|
+
}, A = l(n.idleTimeoutMs ?? 45e3, () => {
|
|
101
|
+
O = !0, k(new s()), w.cancel().catch(() => void 0);
|
|
102
|
+
});
|
|
68
103
|
try {
|
|
69
104
|
for (;;) {
|
|
70
|
-
let { done: e, value: t } = await
|
|
105
|
+
let { done: e, value: t } = await w.read();
|
|
106
|
+
if (O) break;
|
|
71
107
|
if (e) {
|
|
72
|
-
|
|
108
|
+
E.trim() && d(E, y, k);
|
|
73
109
|
break;
|
|
74
110
|
}
|
|
75
|
-
|
|
76
|
-
let n =
|
|
77
|
-
|
|
78
|
-
for (let e of n)
|
|
111
|
+
A.touch(), E += T.decode(t, { stream: !0 });
|
|
112
|
+
let n = E.split("\n");
|
|
113
|
+
E = n.pop() ?? "";
|
|
114
|
+
for (let e of n) d(e, y, k);
|
|
79
115
|
}
|
|
80
|
-
|
|
116
|
+
D || (D = !0, b());
|
|
81
117
|
} catch (e) {
|
|
82
|
-
if (e
|
|
83
|
-
|
|
118
|
+
if (u(e)) return;
|
|
119
|
+
k(new s(e));
|
|
84
120
|
} finally {
|
|
85
|
-
|
|
121
|
+
A.stop(), w.releaseLock();
|
|
86
122
|
}
|
|
87
123
|
})();
|
|
88
124
|
},
|
|
89
125
|
stopCompletion: async (t) => {
|
|
90
|
-
let
|
|
126
|
+
let a = await i(`${r}/completions/stop`, {
|
|
91
127
|
method: "POST",
|
|
92
128
|
credentials: "include",
|
|
93
129
|
headers: {
|
|
@@ -95,10 +131,10 @@ var e = { "Content-Type": "application/json" }, t = 409, n = "A response is alre
|
|
|
95
131
|
...n.getCsrfToken() == null ? {} : { "X-CSRF-Token": n.getCsrfToken() }
|
|
96
132
|
},
|
|
97
133
|
body: JSON.stringify(t)
|
|
98
|
-
}),
|
|
99
|
-
if (
|
|
134
|
+
}), o = a.headers.get("x-csrf-token");
|
|
135
|
+
if (o && n.setCsrfToken(o), !a.ok) throw Error(`stopCompletion failed: ${a.status}`);
|
|
100
136
|
}
|
|
101
137
|
};
|
|
102
138
|
};
|
|
103
139
|
//#endregion
|
|
104
|
-
export {
|
|
140
|
+
export { i as DEFAULT_GENERATION_CONFLICT_MESSAGE, n as DEFAULT_GENERATION_PERSISTENCE_ERROR_MESSAGE, c as DEFAULT_STREAM_IDLE_TIMEOUT_MS, a as GenerationConflictError, r as GenerationPersistenceError, s as StreamInterruptedError, o as StreamUpstreamError, f as createChatStreamApi };
|
|
@@ -9,6 +9,48 @@ var i = (t, n, r, i) => i && n === r - 1 && t.role === e.Assistant, a = (e) => {
|
|
|
9
9
|
return null;
|
|
10
10
|
}, o = (t) => t.role === e.Assistant && (t.custom_content?.stages?.length ?? 0) > 0, s = (t) => {
|
|
11
11
|
for (let n = t.length - 1; n >= 0; n--) if (t[n].role === e.User) return t[n].custom_content?.configuration_value;
|
|
12
|
-
}, c = (e) =>
|
|
12
|
+
}, c = (e) => {
|
|
13
|
+
if (typeof e.default == "boolean") return e.default;
|
|
14
|
+
let t = e.oneOf;
|
|
15
|
+
if (Array.isArray(t) && t.length === 1) {
|
|
16
|
+
let e = t[0];
|
|
17
|
+
if (typeof e == "object" && e && "const" in e && typeof e.const == "boolean") return e.const;
|
|
18
|
+
}
|
|
19
|
+
}, l = (e) => {
|
|
20
|
+
let t = e?.properties;
|
|
21
|
+
if (t == null) return;
|
|
22
|
+
let n = Object.entries(t).flatMap(([e, t]) => {
|
|
23
|
+
let n = c(t);
|
|
24
|
+
return n === void 0 ? [] : [[e, n]];
|
|
25
|
+
});
|
|
26
|
+
return n.length > 0 ? Object.fromEntries(n) : void 0;
|
|
27
|
+
}, u = (t) => {
|
|
28
|
+
let n = -1;
|
|
29
|
+
for (let r = t.length - 1; r >= 0; r--) if (t[r].role === e.User) {
|
|
30
|
+
n = r;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
let r = t[n]?.custom_content?.configuration_value;
|
|
34
|
+
for (let i = n + 1; i < t.length; i++) {
|
|
35
|
+
let n = t[i];
|
|
36
|
+
if (n.role !== e.Assistant) continue;
|
|
37
|
+
let a = l(n.custom_content?.form_schema);
|
|
38
|
+
a && (r = {
|
|
39
|
+
...r,
|
|
40
|
+
...a
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return r;
|
|
44
|
+
}, d = (t, n, r) => {
|
|
45
|
+
let i = n.length - 1, a = n[i];
|
|
46
|
+
if (a?.role !== e.Assistant) return;
|
|
47
|
+
let o = l(a.custom_content?.form_schema);
|
|
48
|
+
if (!o) return;
|
|
49
|
+
let s = Object.fromEntries(r.filter((e) => e in o).sort().map((e) => [e, o[e]]));
|
|
50
|
+
if (Object.keys(s).length !== 0) return {
|
|
51
|
+
key: `${t}|${i}|${JSON.stringify(s)}`,
|
|
52
|
+
values: s
|
|
53
|
+
};
|
|
54
|
+
}, f = (e) => (e ?? "").toLowerCase().replace(/[^a-z]/g, "") === "plaintext" ? t.PlainText : t.Markdown;
|
|
13
55
|
//#endregion
|
|
14
|
-
export { a as getLastDeploymentId, s as getLastUserMessageToolConfiguration, i as isMessageStreaming, o as messageHasStages,
|
|
56
|
+
export { d as getFormSchemaToolSyncKey, a as getLastDeploymentId, s as getLastUserMessageToolConfiguration, u as getLatestToolConfiguration, l as getToolConfigurationFromFormSchema, i as isMessageStreaming, o as messageHasStages, f as normalizeResponseFormat };
|
|
@@ -1,16 +1,39 @@
|
|
|
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 { MessageRole as o } from "@epam/ai-dial-chat-shared";
|
|
6
7
|
//#region src/conversation/useConversationStream/generation-resume.ts
|
|
7
|
-
var
|
|
8
|
+
var s = (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
|
-
},
|
|
11
|
+
}, c = (e) => {
|
|
11
12
|
let t = e.messages[e.messages.length - 1];
|
|
12
|
-
return !!t && t.role ===
|
|
13
|
-
},
|
|
13
|
+
return !!t && t.role === o.Assistant && !s(t) && t.streamErrorMessage == null && !t.wasStoppedByUser;
|
|
14
|
+
}, l = 300 * 1e3, u = "awaiting-resume", d = [
|
|
15
|
+
1e3,
|
|
16
|
+
2e3,
|
|
17
|
+
4e3,
|
|
18
|
+
8e3,
|
|
19
|
+
16e3
|
|
20
|
+
], f = (e) => new Promise((t) => {
|
|
21
|
+
let n = typeof window < "u", r = () => {
|
|
22
|
+
clearTimeout(i), n && window.removeEventListener("online", r), t();
|
|
23
|
+
}, i = setTimeout(r, e);
|
|
24
|
+
n && window.addEventListener("online", r);
|
|
25
|
+
}), p = async (e, t) => {
|
|
26
|
+
for (let n = 0; n <= d.length; n++) {
|
|
27
|
+
if (t()) return null;
|
|
28
|
+
try {
|
|
29
|
+
return await e();
|
|
30
|
+
} catch {
|
|
31
|
+
if (n === d.length) return null;
|
|
32
|
+
await f(d[n]);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}, m = async (e, t) => {
|
|
14
37
|
let n = e.getReader(), r = new TextDecoder(), i = "";
|
|
15
38
|
try {
|
|
16
39
|
for (;;) {
|
|
@@ -39,88 +62,106 @@ var a = (e) => {
|
|
|
39
62
|
} catch {} finally {
|
|
40
63
|
n.releaseLock();
|
|
41
64
|
}
|
|
42
|
-
},
|
|
43
|
-
if (!
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
let
|
|
48
|
-
|
|
49
|
-
|
|
65
|
+
}, h = ({ transport: o, setConversation: d, conversationRef: f, resumingPathsRef: p, bufferedGenerationsRef: h, addStreamingPath: g, removeStreamingPath: _, isPathDisplayed: v, generationPersistenceErrorMessage: y = t }) => (t, b, x = {}) => {
|
|
66
|
+
if (!c(b)) {
|
|
67
|
+
x.onSettled?.();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
let S = r(t);
|
|
71
|
+
if (!x.skipDedupe && p.current.has(S)) return;
|
|
72
|
+
p.current.add(S), g(S);
|
|
73
|
+
let C = b.messages.length - 1, w = {
|
|
74
|
+
generationId: u,
|
|
75
|
+
messageIndex: C,
|
|
76
|
+
message: x.seedMessage ?? b.messages[C]
|
|
77
|
+
};
|
|
78
|
+
h.current.set(S, w);
|
|
79
|
+
let T = () => h.current.get(S) === w, E = (e, t = !1) => {
|
|
80
|
+
if (!T()) return;
|
|
81
|
+
p.current.delete(S), _(S);
|
|
82
|
+
let n = e && e.messages.length - 1 === C && c(e) && s(w.message);
|
|
83
|
+
if (t || n) {
|
|
84
|
+
w.message = {
|
|
85
|
+
...w.message,
|
|
86
|
+
streamErrorMessage: y
|
|
87
|
+
}, v(S) && d((e) => {
|
|
88
|
+
if (!e || !T() || !v(S)) return e;
|
|
89
|
+
let t = a(e, w);
|
|
90
|
+
return f.current = t, t;
|
|
91
|
+
});
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
h.current.delete(S), e && v(S) && (d(e), f.current = e);
|
|
95
|
+
}, D = async () => {
|
|
50
96
|
try {
|
|
51
|
-
|
|
97
|
+
E(await o.getConversation(e(t)));
|
|
52
98
|
} catch {
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
}, S = (e) => {
|
|
56
|
-
let t = {
|
|
57
|
-
generationId: c,
|
|
58
|
-
messageIndex: y,
|
|
59
|
-
message: e
|
|
60
|
-
};
|
|
61
|
-
f.current.set(v, t), h(v) && a((e) => {
|
|
62
|
-
if (!e) return e;
|
|
63
|
-
let n = r(e, t);
|
|
64
|
-
return u.current = n, n;
|
|
65
|
-
});
|
|
66
|
-
}, C = (e) => {
|
|
67
|
-
let t = f.current.get(v);
|
|
68
|
-
if (t?.generationId === c) {
|
|
69
|
-
let r = n([t.message], 0, e);
|
|
70
|
-
r && (t.message = r[0]);
|
|
99
|
+
E(void 0, s(w.message));
|
|
71
100
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
101
|
+
}, O = (e) => {
|
|
102
|
+
T() && (w.message = e, v(S) && d((e) => {
|
|
103
|
+
if (!e || !T() || !v(S)) return e;
|
|
104
|
+
let t = a(e, w);
|
|
105
|
+
return f.current = t, t;
|
|
106
|
+
}));
|
|
107
|
+
}, k = (e) => {
|
|
108
|
+
if (!T()) return;
|
|
109
|
+
let t = i([w.message], 0, e);
|
|
110
|
+
t && (w.message = t[0]), v(S) && d((e) => {
|
|
111
|
+
if (!e || !T() || !v(S)) return e;
|
|
112
|
+
let t = a(e, w);
|
|
113
|
+
return f.current = t, t;
|
|
78
114
|
});
|
|
79
|
-
},
|
|
80
|
-
let
|
|
115
|
+
}, A = async () => {
|
|
116
|
+
let n = new AbortController(), r;
|
|
81
117
|
try {
|
|
82
|
-
|
|
118
|
+
r = await o.watchConversation(S, n.signal);
|
|
83
119
|
} catch {
|
|
84
|
-
await
|
|
120
|
+
await D();
|
|
85
121
|
return;
|
|
86
122
|
}
|
|
87
|
-
let
|
|
88
|
-
|
|
89
|
-
},
|
|
123
|
+
let i = !1, a = window.setTimeout(() => {
|
|
124
|
+
n.abort();
|
|
125
|
+
}, l);
|
|
90
126
|
try {
|
|
91
|
-
await
|
|
92
|
-
if (
|
|
127
|
+
await m(r, async (n) => {
|
|
128
|
+
if (n?.action !== "UPDATE") return !1;
|
|
93
129
|
try {
|
|
94
|
-
let
|
|
95
|
-
if (!
|
|
130
|
+
let n = await o.getConversation(e(t));
|
|
131
|
+
if (!c(n)) return E(n), i = !0, !0;
|
|
96
132
|
} catch {}
|
|
97
133
|
return !1;
|
|
98
134
|
});
|
|
99
135
|
} finally {
|
|
100
136
|
clearTimeout(a);
|
|
101
137
|
}
|
|
102
|
-
|
|
103
|
-
},
|
|
138
|
+
i || await D();
|
|
139
|
+
}, j = async () => {
|
|
104
140
|
let e = new AbortController(), t;
|
|
105
141
|
try {
|
|
106
|
-
t = await
|
|
142
|
+
t = await o.attachToGeneration(S, e.signal);
|
|
107
143
|
} catch {
|
|
108
144
|
return !1;
|
|
109
145
|
}
|
|
110
|
-
let
|
|
111
|
-
return await
|
|
146
|
+
let r = !1, i = !1;
|
|
147
|
+
return await m(t, (e) => {
|
|
148
|
+
if (!T()) return !0;
|
|
112
149
|
switch (e.type) {
|
|
113
|
-
case "snapshot": return
|
|
114
|
-
case "chunk": return
|
|
150
|
+
case "snapshot": return O(e.message), !1;
|
|
151
|
+
case "chunk": return k(e.chunk), !1;
|
|
115
152
|
case "done":
|
|
116
153
|
case "error":
|
|
117
|
-
case "stopped": return n = !0, !0;
|
|
154
|
+
case "stopped": return i = e.type === "error" && e.errorType === n.type, r = !0, !0;
|
|
118
155
|
}
|
|
119
|
-
}),
|
|
156
|
+
}), r ? (i ? E(void 0, !0) : await D(), !0) : !T();
|
|
120
157
|
};
|
|
121
158
|
(async () => {
|
|
122
|
-
|
|
159
|
+
try {
|
|
160
|
+
await j() || await A();
|
|
161
|
+
} finally {
|
|
162
|
+
x.onSettled?.();
|
|
163
|
+
}
|
|
123
164
|
})();
|
|
124
165
|
};
|
|
125
166
|
//#endregion
|
|
126
|
-
export {
|
|
167
|
+
export { h as createResumeIfAwaitingGeneration, p as fetchConversationForRecovery, s as hasGeneratedPayload, c as isAwaitingGenerationResume };
|