@epam/ai-dial-chat-hooks 1.1.0-dev.252 → 1.1.0-dev.253

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 (50) hide show
  1. package/attachment/useAttachmentValidation/useAttachmentValidation.d.ts +44 -0
  2. package/attachment/useAttachmentValidation/useAttachmentValidation.d.ts.map +1 -0
  3. package/conversation/conversation-transfer/async.d.ts +10 -0
  4. package/conversation/conversation-transfer/async.d.ts.map +1 -0
  5. package/conversation/conversation-transfer/attachment-refs.d.ts +12 -0
  6. package/conversation/conversation-transfer/attachment-refs.d.ts.map +1 -0
  7. package/conversation/conversation-transfer/build-upload-path.d.ts +40 -0
  8. package/conversation/conversation-transfer/build-upload-path.d.ts.map +1 -0
  9. package/conversation/conversation-transfer/date.d.ts +5 -0
  10. package/conversation/conversation-transfer/date.d.ts.map +1 -0
  11. package/conversation/conversation-transfer/dial-file-resolve.d.ts +10 -0
  12. package/conversation/conversation-transfer/dial-file-resolve.d.ts.map +1 -0
  13. package/conversation/conversation-transfer/export-conversation.d.ts +8 -0
  14. package/conversation/conversation-transfer/export-conversation.d.ts.map +1 -0
  15. package/conversation/conversation-transfer/import-conversation.d.ts +82 -0
  16. package/conversation/conversation-transfer/import-conversation.d.ts.map +1 -0
  17. package/conversation/conversation-transfer/queue.d.ts +31 -0
  18. package/conversation/conversation-transfer/queue.d.ts.map +1 -0
  19. package/conversation/conversation-transfer/types.d.ts +73 -0
  20. package/conversation/conversation-transfer/types.d.ts.map +1 -0
  21. package/conversation/conversation-transfer/zip-export.d.ts +31 -0
  22. package/conversation/conversation-transfer/zip-export.d.ts.map +1 -0
  23. package/conversation/conversation-transfer/zip-import.d.ts +17 -0
  24. package/conversation/conversation-transfer/zip-import.d.ts.map +1 -0
  25. package/conversation/useConversationExport/useConversationExport.d.ts +50 -0
  26. package/conversation/useConversationExport/useConversationExport.d.ts.map +1 -0
  27. package/conversation/useConversationHandlers/attachment-to-dto.d.ts +7 -0
  28. package/conversation/useConversationHandlers/attachment-to-dto.d.ts.map +1 -0
  29. package/conversation/useConversationHandlers/message-factory.d.ts +9 -0
  30. package/conversation/useConversationHandlers/message-factory.d.ts.map +1 -0
  31. package/conversation/useConversationHandlers/message-utils.d.ts +14 -0
  32. package/conversation/useConversationHandlers/message-utils.d.ts.map +1 -0
  33. package/conversation/useConversationHandlers/starter-option.d.ts +14 -0
  34. package/conversation/useConversationHandlers/starter-option.d.ts.map +1 -0
  35. package/conversation/useConversationHandlers/useConversationHandlers.d.ts +66 -0
  36. package/conversation/useConversationHandlers/useConversationHandlers.d.ts.map +1 -0
  37. package/conversation/useConversationImport/useConversationImport.d.ts +50 -0
  38. package/conversation/useConversationImport/useConversationImport.d.ts.map +1 -0
  39. package/conversation/useConversationStream/apply-chunk.d.ts +20 -0
  40. package/conversation/useConversationStream/apply-chunk.d.ts.map +1 -0
  41. package/conversation/useConversationStream/conversation-path.d.ts +9 -0
  42. package/conversation/useConversationStream/conversation-path.d.ts.map +1 -0
  43. package/conversation/useConversationStream/generation-resume.d.ts +10 -0
  44. package/conversation/useConversationStream/generation-resume.d.ts.map +1 -0
  45. package/conversation/useConversationStream/useConversationStream.d.ts +78 -0
  46. package/conversation/useConversationStream/useConversationStream.d.ts.map +1 -0
  47. package/index.d.ts +15 -0
  48. package/index.d.ts.map +1 -1
  49. package/index.js +1272 -92
  50. package/package.json +10 -7
package/index.js CHANGED
@@ -1,30 +1,32 @@
1
1
  import { AttachmentContentType as e, useAttachmentCanvas as t } from "@epam/ai-dial-attachment-canvas";
2
2
  import { AttachmentErrorReason as n, MIMEType as r, MessageRole as i, base64ToBlob as a, messageAttachmentToDisplayAttachment as o, triggerAnchorDownload as s, triggerBlobDownload as c } from "@epam/ai-dial-chat-shared";
3
- import { isReferenceOnlyAttachment as l, parsePdfPageReference as u, resolveMessageAnnotations as d } from "@epam/ai-dial-quotations";
4
- import { useCallback as f, useEffect as p, useLayoutEffect as m, useMemo as h, useRef as g, useState as _ } from "react";
5
- import { ShareLinkResponseDtoAccessEnum as v } from "@epam/ai-dial-chat-api-client";
6
- import { ShareLinkAccess as y } from "@epam/ai-dial-share";
3
+ import { isReferenceOnlyAttachment as l, normalizeRawAnnotations as u, parsePdfPageReference as d, resolveMessageAnnotations as f } from "@epam/ai-dial-quotations";
4
+ import { useCallback as p, useEffect as m, useLayoutEffect as h, useMemo as g, useRef as _, useState as v } from "react";
5
+ import { isMimeTypeAllowed as y, mimeTypesToExtensionLabels as b } from "@epam/ai-dial-attachment-input";
6
+ import { strFromU8 as x, strToU8 as S, unzipSync as C, zipSync as w } from "fflate";
7
+ import { SendCompletionDtoModeEnum as T, ShareLinkResponseDtoAccessEnum as E } from "@epam/ai-dial-chat-api-client";
8
+ import { ShareLinkAccess as D } from "@epam/ai-dial-share";
7
9
  //#region src/attachment/useAttachmentAction/useAttachmentAction.ts
8
- var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e.data != null, S = (e, t) => {
10
+ var O = (e) => e.startsWith("files/"), k = (e) => e.url != null && O(e.url) || e.data != null, A = (e, t) => {
9
11
  let { url: n, name: i, contentType: o, data: l } = e;
10
- if (n != null && b(n)) {
12
+ if (n != null && O(n)) {
11
13
  let e = t(n);
12
14
  return e == null ? !1 : (s(e, i), !0);
13
15
  }
14
16
  return l == null ? !1 : (c(a(l, o || r.Plain), i), !0);
15
- }, C = (e, t) => {
17
+ }, j = (e, t) => {
16
18
  let { url: n } = e;
17
19
  if (n == null) return;
18
20
  let r = n.split("#")[0];
19
- if (b(r)) {
21
+ if (O(r)) {
20
22
  let n = t(r);
21
23
  if (n == null) return;
22
24
  s(n, e.title ?? r.split("/").pop() ?? "");
23
25
  } else window.open(n, "_blank", "noopener,noreferrer");
24
- }, w = (t, n) => {
25
- let r = u(t.url);
26
+ }, M = (t, n) => {
27
+ let r = d(t.url);
26
28
  if (r == null) return null;
27
- let i = b(r.baseUrl) ? n(r.baseUrl) : r.baseUrl;
29
+ let i = O(r.baseUrl) ? n(r.baseUrl) : r.baseUrl;
28
30
  if (i == null) return null;
29
31
  if (r.page == null) return {
30
32
  type: e.Pdf,
@@ -50,12 +52,12 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
50
52
  }],
51
53
  selectedHighlightId: a
52
54
  };
53
- }, T = ({ resolveDownloadUrl: e }) => {
55
+ }, N = ({ resolveDownloadUrl: e }) => {
54
56
  let { openCanvas: n } = t();
55
- return { handleAttachmentClick: f((t) => {
57
+ return { handleAttachmentClick: p((t) => {
56
58
  let { url: r, referenceUrl: i, data: a, name: o } = t;
57
59
  if (r != null || a != null) {
58
- S(t, e);
60
+ A(t, e);
59
61
  return;
60
62
  }
61
63
  if (i == null) return;
@@ -63,21 +65,178 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
63
65
  type: t.contentType,
64
66
  url: i,
65
67
  title: o
66
- }, c = w(s, e);
68
+ }, c = M(s, e);
67
69
  if (c) {
68
70
  n(c, o);
69
71
  return;
70
72
  }
71
- C(s, e);
73
+ j(s, e);
72
74
  }, [n, e]) };
73
- }, E = 700, D = (e) => (e.split(/[\\/]/).filter(Boolean).pop() ?? "file").replace(/\.\.+/g, ".").replace(/^\.+/, "") || "file", O = (e, t = /* @__PURE__ */ new Date()) => `uploads/${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, "0")}/${encodeURIComponent(D(e))}`, k = ({ filesApi: e, bucket: t, onNetworkError: r, debounceMs: i = E }) => {
74
- let a = g([]), o = g(null);
75
- return { handleUploadAttachment: f(async (s) => {
75
+ }, P = 100, F = "*/*", I = (e) => e.startsWith(".") || e.includes("/"), ee = (e) => {
76
+ if (e != null) return e.map((e) => e === "*" ? F : e).filter(I);
77
+ }, te = (e) => {
78
+ let t = ee(e);
79
+ if (!(t == null || t.length === 0) && !t.some((e) => e === F)) return t.join(",");
80
+ }, ne = /* @__PURE__ */ function(e) {
81
+ return e.NoTypesAllowed = "noTypesAllowed", e.UnsupportedType = "unsupportedType", e;
82
+ }({}), re = ({ allowedMimeTypes: e, onValidationError: t, debounceMs: r = P }) => {
83
+ let i = e.length > 0, a = g(() => te(e), [e]), o = _(null);
84
+ return m(() => () => {
85
+ o.current != null && clearTimeout(o.current);
86
+ }, []), {
87
+ inputAttachmentTypes: e,
88
+ isAttachmentsAllowed: i,
89
+ validateAttachment: p((i) => {
90
+ if (!y(i.contentType, e)) return o.current != null && clearTimeout(o.current), o.current = setTimeout(() => {
91
+ let n = e.length === 0;
92
+ t?.({
93
+ reason: n ? "noTypesAllowed" : "unsupportedType",
94
+ allowedMimeTypes: e,
95
+ ...n ? {} : { formats: b(e) }
96
+ }), o.current = null;
97
+ }, r), n.UnsupportedType;
98
+ }, [
99
+ e,
100
+ r,
101
+ t
102
+ ]),
103
+ fileAccept: a
104
+ };
105
+ }, ie = (e) => `${e.getFullYear()}-${String(e.getMonth() + 1).padStart(2, "0")}-${String(e.getDate()).padStart(2, "0")}`, L = (e) => `${e.getFullYear()}-${String(e.getMonth() + 1).padStart(2, "0")}`, ae = /* @__PURE__ */ function(e) {
106
+ return e.WithAttachments = "withAttachments", e.WithoutAttachments = "withoutAttachments", e;
107
+ }({}), R = /* @__PURE__ */ function(e) {
108
+ return e.SingleConversation = "chat_conversation", e.SingleConversationWithAttachments = "chat_with_attachments", e.AllConversationsHistory = "chat_conversations_history", e;
109
+ }({}), z = /* @__PURE__ */ function(e) {
110
+ return e.InProgress = "inProgress", e.Success = "success", e.Failed = "failed", e;
111
+ }({}), B = /* @__PURE__ */ function(e) {
112
+ return e.Single = "single", e.All = "all", e;
113
+ }({}), V = /* @__PURE__ */ function(e) {
114
+ return e.Unauthorized = "unauthorized", e.NotFound = "notFound", e.UnsupportedFormat = "unsupportedFormat", e.MissingBucket = "missingBucket", e.Unknown = "unknown", e;
115
+ }({}), H = /* @__PURE__ */ function(e) {
116
+ return e.AttachmentSkipped = "attachmentSkipped", e;
117
+ }({}), U = "ai_dial", W = (e, t = []) => ({
118
+ version: 5,
119
+ history: e,
120
+ folders: t
121
+ }), oe = (e) => new Blob([JSON.stringify(e, null, 2)], { type: "application/json" }), se = {
122
+ [R.SingleConversation]: "json",
123
+ [R.SingleConversationWithAttachments]: "dial",
124
+ [R.AllConversationsHistory]: "json"
125
+ }, G = (e, t, n = /* @__PURE__ */ new Date()) => `${ie(n)}_${t}_${e}.${se[e]}`, ce = (e) => e.startsWith("files/"), le = (e) => {
126
+ let t = /* @__PURE__ */ new Set();
127
+ for (let n of e.messages) for (let e of n.custom_content?.attachments ?? []) {
128
+ let n = e.url ?? e.reference_url;
129
+ n != null && ce(n) && t.add(n);
130
+ }
131
+ return Array.from(t, (e) => ({ fileId: e }));
132
+ }, ue = (e) => e.startsWith("files/"), de = (e) => {
133
+ if (!ue(e)) return;
134
+ let t = e.slice(6), n = t.indexOf("/");
135
+ if (n < 0) return;
136
+ let r = t.slice(0, n), i = t.slice(n + 1), a;
137
+ try {
138
+ a = decodeURIComponent(i);
139
+ } catch {
140
+ a = i;
141
+ }
142
+ return {
143
+ bucket: r,
144
+ path: a
145
+ };
146
+ }, K = class extends Error {
147
+ constructor(e) {
148
+ super(e), this.name = "UnsupportedImportFormatError";
149
+ }
150
+ }, fe = (e) => typeof e == "object" && !!e, pe = 5, me = (e) => {
151
+ let t;
152
+ try {
153
+ t = JSON.parse(e);
154
+ } catch {
155
+ throw new K("Import file is not valid JSON");
156
+ }
157
+ if (!fe(t) || t.version !== pe || !Array.isArray(t.history)) throw new K("Import file is not a supported export format");
158
+ let n = Array.isArray(t.folders) ? t.folders : [];
159
+ return {
160
+ version: pe,
161
+ history: t.history,
162
+ folders: n
163
+ };
164
+ }, he = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i, ge = (e) => {
165
+ let t = e.lastIndexOf("__");
166
+ if (t < 0) return e;
167
+ let n = e.slice(t + 2);
168
+ return he.test(n) ? e.slice(0, t) : e;
169
+ }, _e = "conversations/", q = (e) => e.startsWith(_e) ? e.slice(14) : e, ve = (e, t) => {
170
+ let n = q(e.id), r = n.split("/"), i = q(e.folderId), a = r[0], o = i.startsWith(`${a}/`) ? i.slice(a.length + 1).split("/").filter(Boolean) : [], s = r.slice(1).slice(o.length), c = s.slice(0, -1), l = `${ge(s.at(-1) ?? r.at(-1) ?? n)}__${crypto.randomUUID()}`, u = [
171
+ ...o,
172
+ ...c,
173
+ l
174
+ ].join("/"), d = o.length ? `${t}/${o.join("/")}` : t;
175
+ return {
176
+ conversation: {
177
+ ...e,
178
+ id: `${t}/${u}`,
179
+ folderId: d
180
+ },
181
+ subPath: u
182
+ };
183
+ }, ye = (e) => {
184
+ let t = q(e.folderId).split("/").filter(Boolean).slice(1);
185
+ return t.length ? t.join(" / ") : void 0;
186
+ }, be = (e) => e.map((e) => `"${e}"`).join(", "), xe = (e, t) => ({
187
+ ...e,
188
+ messages: e.messages.map((e) => {
189
+ let n = e.custom_content?.attachments;
190
+ return n?.length ? {
191
+ ...e,
192
+ custom_content: {
193
+ ...e.custom_content,
194
+ attachments: n.map((e) => {
195
+ let n = e.url ? t.get(e.url) : void 0, r = e.reference_url ? t.get(e.reference_url) : void 0;
196
+ if (n == null && r == null) return e;
197
+ let i = n?.title ?? r?.title;
198
+ return {
199
+ ...e,
200
+ ...n == null ? {} : { url: n.url },
201
+ ...r == null ? {} : { reference_url: r.url },
202
+ ...i == null ? {} : { title: i }
203
+ };
204
+ })
205
+ }
206
+ } : e;
207
+ })
208
+ }), Se = (e) => e.split("/").pop() || e, Ce = (e, t, n) => {
209
+ let r = [], i = [];
210
+ for (let a of le(e)) {
211
+ let e = de(a.fileId);
212
+ if (!e) {
213
+ i.push(Se(a.fileId));
214
+ continue;
215
+ }
216
+ let o = e.path.split("/").pop() ?? e.path, s = t.get(e.path);
217
+ if (!s) {
218
+ i.push(o);
219
+ continue;
220
+ }
221
+ r.push({
222
+ fileId: a.fileId,
223
+ bytes: s,
224
+ originalFileName: o,
225
+ allocated: n.allocate(o)
226
+ });
227
+ }
228
+ return {
229
+ plan: r,
230
+ skippedNames: i
231
+ };
232
+ }, we = 700, Te = (e) => (e.split(/[\\/]/).filter(Boolean).pop() ?? "file").replace(/\.\.+/g, ".").replace(/^\.+/, "") || "file", Ee = (e, t = /* @__PURE__ */ new Date()) => `uploads/${t.getFullYear()}-${String(t.getMonth() + 1).padStart(2, "0")}/${encodeURIComponent(Te(e))}`, De = ({ filesApi: e, bucket: t, onNetworkError: r, debounceMs: i = we }) => {
233
+ let a = _([]), o = _(null);
234
+ return { handleUploadAttachment: p(async (s) => {
76
235
  if (!t) throw Error("User bucket is not available");
77
236
  try {
78
237
  return (await e.uploadFile({
79
238
  bucket: t,
80
- path: O(s.name),
239
+ path: Ee(s.name),
81
240
  file: s.file
82
241
  })).url;
83
242
  } catch (e) {
@@ -97,78 +256,878 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
97
256
  e,
98
257
  r
99
258
  ]) };
100
- }, A = 80, j = 1, M = ({ messages: e, isAssistantTyping: t, conversationId: n }) => {
101
- let [r, i] = _(!1), a = g(null), o = g(null), s = g(null), c = g(!1), l = g(!1), u = g(null), d = g(/* @__PURE__ */ new Map()), h = f((e, t) => {
259
+ }, Oe = async (e, t, n) => {
260
+ let r = [], i = 0, a = async () => {
261
+ let t = i;
262
+ if (i += 1, !(t >= e.length)) {
263
+ try {
264
+ let i = await n(e[t]);
265
+ i !== void 0 && r.push(i);
266
+ } catch (e) {
267
+ console.error("runWithConcurrency: worker failed for item", e);
268
+ }
269
+ await a();
270
+ }
271
+ };
272
+ return await Promise.all(Array.from({ length: Math.min(t, e.length) }, a)), r;
273
+ }, ke = () => {
274
+ let [e, t] = v([]), n = _(/* @__PURE__ */ new Map()), r = _(/* @__PURE__ */ new Map()), i = p((e, n) => {
275
+ t((t) => t.map((t) => t.id === e ? {
276
+ ...t,
277
+ ...n
278
+ } : t));
279
+ }, []), a = p((e) => {
280
+ let n = crypto.randomUUID();
281
+ return t((t) => [...t, {
282
+ id: n,
283
+ subject: e,
284
+ status: z.InProgress
285
+ }]), n;
286
+ }, []), o = p((e) => {
287
+ n.current.get(e)?.abort(), n.current.delete(e), r.current.delete(e), t((t) => t.filter((t) => t.id !== e));
288
+ }, []), s = p((e) => {
289
+ r.current.get(e)?.();
290
+ }, []), c = p(() => {
291
+ for (let e of n.current.values()) e.abort();
292
+ n.current.clear(), r.current.clear(), t([]);
293
+ }, []), l = p((e, t) => {
294
+ let a = () => {
295
+ let r = new AbortController();
296
+ return n.current.set(e, r), t(r.signal);
297
+ };
298
+ return r.current.set(e, () => (i(e, { status: z.InProgress }), a())), a();
299
+ }, [i]);
300
+ return m(() => {
301
+ let e = n.current;
302
+ return () => {
303
+ for (let t of e.values()) t.abort();
304
+ };
305
+ }, []), {
306
+ jobs: e,
307
+ addJob: a,
308
+ updateJob: i,
309
+ dismissJob: o,
310
+ retryJob: s,
311
+ dismissAll: c,
312
+ startJob: l
313
+ };
314
+ }, Ae = S("", !0).constructor, je = (e) => e instanceof Ae ? e : new Ae(e), Me = (e) => e.length > 0 && e.split("/").every((e) => e !== "" && e !== "." && e !== ".."), Ne = (e, t) => {
315
+ let n = [], r = { "conversation.json": je(S(JSON.stringify(e, null, 2))) };
316
+ for (let e of t) {
317
+ if (!Me(e.path)) {
318
+ n.push(e.path);
319
+ continue;
320
+ }
321
+ r[`res/${e.path}`] = je(e.data);
322
+ }
323
+ let i = w(r);
324
+ return {
325
+ blob: new Blob([new Uint8Array(i)], { type: "application/zip" }),
326
+ skippedPaths: n
327
+ };
328
+ }, Pe = 5, Fe = (e) => !e.sharedWithMe && !e.publishedWithMe, Ie = ({ conversationsApi: e, filesApi: t, normalizeConversationPath: n, classifyTransferError: r = () => ({}), resolveErrorTraceId: i = async () => void 0, onSuccess: a, onWarning: o, onError: s }) => {
329
+ let l = ke(), u = p(async (e, n) => {
330
+ let i = !1, a = !1;
331
+ return {
332
+ entries: await Oe(e, Pe, async (e) => {
333
+ if (n.aborted || a) return;
334
+ let o = de(e.fileId);
335
+ if (!o) {
336
+ i = !0;
337
+ return;
338
+ }
339
+ try {
340
+ let e = await t.downloadFileRaw({
341
+ bucket: o.bucket,
342
+ path: o.path
343
+ }, { signal: n }), r = new Uint8Array(await e.raw.arrayBuffer());
344
+ return {
345
+ path: o.path,
346
+ data: r
347
+ };
348
+ } catch (e) {
349
+ if (n.aborted) return;
350
+ if (r(e).isUnauthorized) {
351
+ a = !0;
352
+ return;
353
+ }
354
+ i = !0;
355
+ return;
356
+ }
357
+ }),
358
+ anySkipped: !n.aborted && !a && i,
359
+ isUnauthorized: !n.aborted && a
360
+ };
361
+ }, [r, t]), d = p(async (t, d, f, p, m) => {
362
+ let h;
363
+ try {
364
+ h = await e.getConversation({ path: n(d) }, { signal: m });
365
+ } catch (e) {
366
+ if (m.aborted) return;
367
+ if (r(e).isUnauthorized) s?.({
368
+ jobId: t,
369
+ code: V.Unauthorized
370
+ });
371
+ else {
372
+ let n = await i(e);
373
+ s?.({
374
+ jobId: t,
375
+ code: V.Unknown,
376
+ titles: [f],
377
+ traceId: n
378
+ });
379
+ }
380
+ l.updateJob(t, { status: z.Failed }), console.error("Failed to fetch conversation for export", e);
381
+ return;
382
+ }
383
+ if (!m.aborted) try {
384
+ if (p === ae.WithoutAttachments) {
385
+ c(oe(W([h], [])), G(R.SingleConversation, U)), a?.({
386
+ jobId: t,
387
+ titles: [f]
388
+ }), l.updateJob(t, { status: z.Success });
389
+ return;
390
+ }
391
+ let { entries: e, anySkipped: n, isUnauthorized: r } = await u(le(h), m);
392
+ if (m.aborted) return;
393
+ if (r) {
394
+ s?.({
395
+ jobId: t,
396
+ code: V.Unauthorized
397
+ }), l.updateJob(t, { status: z.Failed });
398
+ return;
399
+ }
400
+ let { blob: i, skippedPaths: d } = Ne(W([h], []), e);
401
+ (n || d.length > 0) && o?.({
402
+ jobId: t,
403
+ code: H.AttachmentSkipped
404
+ }), c(i, G(R.SingleConversationWithAttachments, U)), a?.({
405
+ jobId: t,
406
+ titles: [f]
407
+ }), l.updateJob(t, { status: z.Success });
408
+ } catch (e) {
409
+ if (m.aborted) return;
410
+ let n = await i(e);
411
+ s?.({
412
+ jobId: t,
413
+ code: V.Unknown,
414
+ titles: [f],
415
+ traceId: n
416
+ }), l.updateJob(t, { status: z.Failed }), console.error("Failed to build conversation export archive", e);
417
+ }
418
+ }, [
419
+ r,
420
+ e,
421
+ u,
422
+ n,
423
+ s,
424
+ a,
425
+ o,
426
+ l,
427
+ i
428
+ ]), f = p((e, t, n) => {
429
+ let r = l.addJob({
430
+ kind: B.Single,
431
+ title: t
432
+ });
433
+ return l.startJob(r, (i) => d(r, e, t, n, i));
434
+ }, [l, d]), m = p(async (t, o) => {
435
+ let u = [], d;
436
+ do {
437
+ if (o.aborted) return;
438
+ let n;
439
+ try {
440
+ n = await e.listConversations({ nextToken: d }, { signal: o });
441
+ } catch (e) {
442
+ if (o.aborted) return;
443
+ if (r(e).isUnauthorized) s?.({
444
+ jobId: t,
445
+ code: V.Unauthorized
446
+ });
447
+ else {
448
+ let n = await i(e);
449
+ s?.({
450
+ jobId: t,
451
+ code: V.Unknown,
452
+ traceId: n
453
+ });
454
+ }
455
+ l.updateJob(t, { status: z.Failed }), console.error("Failed to list conversations for export", e);
456
+ return;
457
+ }
458
+ u.push(...n.items.filter(Fe).map((e) => ({
459
+ id: e.id,
460
+ title: e.title
461
+ }))), d = n.nextToken;
462
+ } while (d);
463
+ let f = [];
464
+ for (let a of u) {
465
+ if (o.aborted) return;
466
+ try {
467
+ let t = await e.getConversation({ path: n(a.id) }, { signal: o });
468
+ f.push(t);
469
+ } catch (e) {
470
+ if (o.aborted) return;
471
+ let n = r(e);
472
+ if (n.isUnauthorized) {
473
+ s?.({
474
+ jobId: t,
475
+ code: V.Unauthorized
476
+ }), l.updateJob(t, { status: z.Failed });
477
+ return;
478
+ }
479
+ if (n.isNotFound) {
480
+ let n = await i(e);
481
+ s?.({
482
+ jobId: t,
483
+ code: V.NotFound,
484
+ titles: [a.title],
485
+ traceId: n
486
+ }), console.error("Skipped a conversation during export-all: not found", e);
487
+ continue;
488
+ }
489
+ let c = await i(e);
490
+ s?.({
491
+ jobId: t,
492
+ code: V.Unknown,
493
+ traceId: c
494
+ }), l.updateJob(t, { status: z.Failed }), console.error("Aborted export-all: failed to fetch conversation", e);
495
+ return;
496
+ }
497
+ }
498
+ if (!o.aborted) try {
499
+ c(oe(W(f, [])), G(R.AllConversationsHistory, U)), a?.({ jobId: t }), l.updateJob(t, { status: z.Success });
500
+ } catch (e) {
501
+ let n = await i(e);
502
+ s?.({
503
+ jobId: t,
504
+ code: V.Unknown,
505
+ traceId: n
506
+ }), l.updateJob(t, { status: z.Failed }), console.error("Failed to build export-all archive", e);
507
+ }
508
+ }, [
509
+ r,
510
+ e,
511
+ n,
512
+ s,
513
+ a,
514
+ l,
515
+ i
516
+ ]), h = p(() => {
517
+ let e = l.addJob({ kind: B.All });
518
+ return l.startJob(e, (t) => m(e, t));
519
+ }, [l, m]);
520
+ return {
521
+ jobs: l.jobs,
522
+ exportSingle: f,
523
+ exportAll: h,
524
+ dismissJob: l.dismissJob,
525
+ retryJob: l.retryJob,
526
+ dismissAll: l.dismissAll
527
+ };
528
+ }, Le = (e) => {
529
+ if (!e.url) throw Error(`Attachment "${e.name}" has not been uploaded`);
530
+ return {
531
+ type: e.contentType,
532
+ title: e.name,
533
+ url: e.url
534
+ };
535
+ }, J = (e) => {
536
+ if (e.length) return e.map(Le);
537
+ }, Y = (e, t, n) => {
538
+ let r = new Date(Date.now()).toISOString();
539
+ return {
540
+ userMessage: {
541
+ role: i.User,
542
+ content: e,
543
+ timestamp: r,
544
+ ...t && Object.keys(t).length ? { custom_content: t } : {}
545
+ },
546
+ assistantMessage: {
547
+ role: i.Assistant,
548
+ content: "",
549
+ timestamp: r,
550
+ ...n ? { deploymentId: n } : {}
551
+ }
552
+ };
553
+ }, X = (e) => e != null && Object.keys(e).length > 0, Re = (e, t, n, r) => {
554
+ if (t !== e.content || r.length > 0) return !0;
555
+ let i = e.custom_content?.attachments?.length ?? 0;
556
+ return n.length !== i;
557
+ }, ze = (e) => e["dial:widgetOptions"].populateText || e.title, Be = (e, t) => {
558
+ let { populateText: n } = e["dial:widgetOptions"];
559
+ return n == null && t ? t : ze(e);
560
+ }, Ve = (e, t) => {
561
+ let { populateText: n, submit: r } = e["dial:widgetOptions"];
562
+ return r && n == null ? "" : t ?? ze(e);
563
+ }, Z = (e) => {
564
+ try {
565
+ return decodeURIComponent(e);
566
+ } catch {
567
+ return e;
568
+ }
569
+ }, Q = (e) => {
570
+ let t = e.indexOf("/");
571
+ return Z(t === -1 ? e : e.substring(t + 1));
572
+ }, He = ({ conversation: e, conversationId: t, bucket: n, isStreaming: r, startStream: a, state: { setConversation: o, conversationRef: s }, filesApi: c, conversationsApi: l, rateApi: u, resolveModelId: d, onConversationDeleted: f, showNetworkError: m, toolConfigurationValue: h }) => {
573
+ let [g, _] = v(null), [y, b] = v(/* @__PURE__ */ new Set()), [x, S] = v(null), { handleUploadAttachment: C } = De({
574
+ filesApi: c,
575
+ bucket: n,
576
+ onNetworkError: m
577
+ }), w = p(async (n, r) => {
578
+ if (!t || !e) return;
579
+ let i = J(r), c = X(h), l = i?.length || c ? {
580
+ ...i?.length ? { attachments: i } : {},
581
+ ...c ? { configuration_value: h } : {}
582
+ } : void 0, u = d(), { userMessage: f, assistantMessage: p } = Y(n, l, u);
583
+ o((e) => {
584
+ if (!e) return e;
585
+ let t = {
586
+ ...e,
587
+ messages: [
588
+ ...e.messages,
589
+ f,
590
+ p
591
+ ]
592
+ };
593
+ return s.current = t, t;
594
+ }), a(t, n, e.messages.length + 1, u, l, crypto.randomUUID(), T.Append);
595
+ }, [
596
+ e,
597
+ t,
598
+ s,
599
+ d,
600
+ o,
601
+ a,
602
+ h
603
+ ]), E = p((n) => {
604
+ if (r || !t || !e || n === -1 || e.messages[n]?.role !== i.Assistant) return;
605
+ let c = e.messages[n - 1];
606
+ if (!c || c.role !== i.User) return;
607
+ let l = d();
608
+ o((e) => {
609
+ if (!e) return e;
610
+ let t = {
611
+ ...e.messages[n],
612
+ content: "",
613
+ custom_content: void 0,
614
+ wasStoppedByUser: void 0,
615
+ stoppedWithoutContent: void 0,
616
+ streamErrorMessage: void 0,
617
+ deploymentId: l
618
+ }, r = {
619
+ ...e,
620
+ messages: [...e.messages.slice(0, n), t]
621
+ };
622
+ return s.current = r, r;
623
+ }), b((e) => new Set([...e].filter((e) => e < n))), a(t, c.content, n, l, c.custom_content, crypto.randomUUID(), T.Regenerate);
624
+ }, [
625
+ e,
626
+ t,
627
+ s,
628
+ r,
629
+ d,
630
+ o,
631
+ a
632
+ ]), D = p((e) => {
633
+ r || _(e);
634
+ }, [r]), O = p(() => {
635
+ if (!t || g == null) return;
636
+ _(null);
637
+ let e = Q(t);
638
+ o((t) => {
639
+ if (!t) return t;
640
+ let n = g;
641
+ if (n === -1) return t;
642
+ let r = t.messages[n + 1]?.role === i.Assistant ? t.messages.filter((e, t) => t !== n && t !== n + 1) : t.messages.filter((e, t) => t !== n);
643
+ if (r.length === 0 || r.length === 1 && r[0].role === i.Status) return l.deleteConversation({ path: e }), f?.(), t;
644
+ let a = {
645
+ ...t,
646
+ messages: r
647
+ };
648
+ return s.current = a, l.saveConversation({
649
+ path: e,
650
+ saveConversationBodyDto: { conversation: a }
651
+ }), a;
652
+ });
653
+ }, [
654
+ t,
655
+ s,
656
+ l,
657
+ f,
658
+ g,
659
+ o
660
+ ]), k = p(async (n, r, i) => {
661
+ if (!t || !e) return !1;
662
+ let a = e.messages[n];
663
+ if (!a) return !1;
664
+ let c = a.rating, d = {
665
+ ...e,
666
+ messages: e.messages.map((e, t) => t === n ? {
667
+ ...e,
668
+ rating: r ?? void 0
669
+ } : e)
670
+ };
671
+ o(() => (s.current = d, d));
672
+ let f = Q(t), p = () => {
673
+ o((e) => e && {
674
+ ...e,
675
+ messages: e.messages.map((e, t) => t === n ? {
676
+ ...e,
677
+ rating: c
678
+ } : e)
679
+ });
680
+ }, m = () => l.saveConversation({
681
+ path: f,
682
+ saveConversationBodyDto: { conversation: d }
683
+ });
684
+ if (r != null) {
685
+ let t = a.responseId;
686
+ if (!t) return p(), !1;
687
+ try {
688
+ return await u.rateMessage({ rateMessageDto: {
689
+ conversationId: e.id,
690
+ responseId: t,
691
+ modelId: e.model.id,
692
+ rate: r,
693
+ ...i ? { comment: i } : {}
694
+ } }), await m(), !0;
695
+ } catch {
696
+ return p(), !1;
697
+ }
698
+ } else try {
699
+ return await m(), !0;
700
+ } catch {
701
+ return p(), !1;
702
+ }
703
+ }, [
704
+ e,
705
+ t,
706
+ s,
707
+ l,
708
+ u,
709
+ o
710
+ ]), A = p((n, r, i) => {
711
+ if (!t || !e) return;
712
+ let c = i ?? n.title, l = Ve(n, i), u = r ? { [r]: n.const } : void 0, f = X(h), p = u || f ? {
713
+ ...u ? { form_value: u } : {},
714
+ ...f ? { configuration_value: h } : {}
715
+ } : void 0, m = d(), { userMessage: g, assistantMessage: _ } = Y(c, p, m);
716
+ o((e) => {
717
+ if (!e) return e;
718
+ let t = {
719
+ ...e,
720
+ messages: [
721
+ ...e.messages,
722
+ g,
723
+ _
724
+ ]
725
+ };
726
+ return s.current = t, t;
727
+ }), a(t, l, e.messages.length + 1, m, p, crypto.randomUUID(), T.Append);
728
+ }, [
729
+ e,
730
+ t,
731
+ s,
732
+ d,
733
+ o,
734
+ a,
735
+ h
736
+ ]);
737
+ return {
738
+ handleSend: w,
739
+ handleUploadAttachment: C,
740
+ handleRegenerateMessage: E,
741
+ handleDeleteMessage: D,
742
+ handleConfirmDelete: O,
743
+ handleRateMessage: k,
744
+ handleButtonSelect: p((n, i, a) => {
745
+ !t || !e || r || (n["dial:widgetOptions"].confirmationMessage ? S({
746
+ starter: n,
747
+ propertyKey: i,
748
+ description: a
749
+ }) : A(n, i, a));
750
+ }, [
751
+ e,
752
+ t,
753
+ r,
754
+ A
755
+ ]),
756
+ handleConfirmStarter: p(() => {
757
+ if (!x) return;
758
+ let { starter: e, propertyKey: t, description: n } = x;
759
+ S(null), A(e, t, n);
760
+ }, [x, A]),
761
+ handleStartEdit: p((e) => {
762
+ b((t) => new Set([...t, e]));
763
+ }, []),
764
+ handleCancelEdit: p((e) => {
765
+ b((t) => {
766
+ let n = new Set(t);
767
+ return n.delete(e), n;
768
+ });
769
+ }, []),
770
+ handleEditMessage: p(async (n, c, l, u) => {
771
+ if (r || !t || !e) return;
772
+ let f = n;
773
+ if (f === -1 || e.messages[f].role !== i.User) return;
774
+ let p = e.messages[f];
775
+ if (!Re(p, c, l, u)) {
776
+ b((e) => {
777
+ let t = new Set(e);
778
+ return t.delete(f), t;
779
+ });
780
+ return;
781
+ }
782
+ let m = J(u), h = new Set(l.map((e) => e.id)), g = [...(p.custom_content?.attachments ?? []).filter((e) => {
783
+ let t = e.url ?? e.data ?? e.title;
784
+ return h.has(t);
785
+ }), ...m ?? []], { attachments: _, ...v } = p.custom_content ?? {}, y = g.length > 0 ? {
786
+ ...v,
787
+ attachments: g
788
+ } : Object.keys(v).length > 0 ? v : void 0, x = {
789
+ ...p,
790
+ content: c,
791
+ custom_content: y
792
+ }, S = Date.now(), C = {
793
+ role: i.Assistant,
794
+ content: "",
795
+ timestamp: new Date(S).toISOString()
796
+ }, w = [
797
+ ...e.messages.slice(0, f),
798
+ x,
799
+ C
800
+ ], E = {
801
+ ...e,
802
+ messages: w
803
+ };
804
+ o(() => (s.current = E, E));
805
+ let D = d();
806
+ a(t, c, w.length - 1, D, y, crypto.randomUUID(), T.Edit), b(/* @__PURE__ */ new Set());
807
+ }, [
808
+ e,
809
+ t,
810
+ s,
811
+ r,
812
+ d,
813
+ o,
814
+ a
815
+ ]),
816
+ editingMessageIndexes: y,
817
+ pendingDeleteIndex: g,
818
+ setPendingDeleteIndex: _,
819
+ pendingStarterContext: x,
820
+ setPendingStarterContext: S
821
+ };
822
+ }, Ue = (e) => {
823
+ let t = e.lastIndexOf("."), n = t > 0;
824
+ return {
825
+ base: n ? e.slice(0, t) : e,
826
+ extension: n ? e.slice(t) : ""
827
+ };
828
+ }, We = (e) => (e.split(/[\\/]/).filter(Boolean).pop() ?? "file").replace(/\.\.+/g, ".").replace(/^\.+/, "") || "file", Ge = (e, t, n) => n === 0 ? `${e}${t}` : `${e} (${n})${t}`, Ke = (e) => {
829
+ let t = e?.date ?? /* @__PURE__ */ new Date(), n = new Set(e?.existingNames);
830
+ return {
831
+ allocate: (e) => {
832
+ let { base: r, extension: i } = Ue(We(e)), a = 0, o = Ge(r, i, a);
833
+ for (; n.has(o);) a += 1, o = Ge(r, i, a);
834
+ return n.add(o), {
835
+ path: `uploads/${L(t)}/${encodeURIComponent(o)}`,
836
+ fileName: o,
837
+ isRenamed: a > 0
838
+ };
839
+ },
840
+ markTaken: (e) => {
841
+ n.add(e);
842
+ }
843
+ };
844
+ }, qe = "conversation.json", Je = /^conversations\/.*\.json$/, Ye = "res/", Xe = (e) => e.find((e) => e === qe) ?? e.find((e) => Je.test(e)) ?? e.find((e) => !e.includes("/") && e.toLowerCase().endsWith(".json")), Ze = (e) => new Promise((t, n) => {
845
+ let r = new FileReader();
846
+ r.onload = () => t(new Uint8Array(r.result)), r.onerror = () => n(r.error), r.readAsArrayBuffer(e);
847
+ }), Qe = async (e) => {
848
+ let t = await Ze(e), n;
849
+ try {
850
+ n = C(t);
851
+ } catch {
852
+ throw new K("Import file is not a valid archive");
853
+ }
854
+ let r = Object.keys(n), i = Xe(r);
855
+ if (!i) throw new K("Archive does not contain a readable conversation-JSON entry");
856
+ let a = me(x(n[i])), o = /* @__PURE__ */ new Map();
857
+ for (let e of r) {
858
+ if (e.endsWith("/") || !e.startsWith(Ye)) continue;
859
+ let t = e.slice(4);
860
+ !t || !Me(t) || o.set(t, n[e]);
861
+ }
862
+ return {
863
+ envelope: a,
864
+ attachments: o
865
+ };
866
+ }, $e = 5, et = 5, tt = (e) => /\.(dial|zip)$/i.test(e.name), nt = (e) => new Promise((t, n) => {
867
+ let r = new FileReader();
868
+ r.onload = () => t(r.result), r.onerror = () => n(r.error), r.readAsText(e);
869
+ }), rt = async (e) => {
870
+ if (tt(e)) {
871
+ let { envelope: t, attachments: n } = await Qe(e);
872
+ return {
873
+ history: t.history,
874
+ attachments: n,
875
+ isArchive: !0
876
+ };
877
+ }
878
+ return {
879
+ history: me(await nt(e)).history,
880
+ attachments: /* @__PURE__ */ new Map(),
881
+ isArchive: !1
882
+ };
883
+ }, it = async (e, t, n, r, i, a, o) => {
884
+ let { plan: s, skippedNames: c } = Ce(e, t, r), l = !1, u = await Oe(s, $e, async (e) => {
885
+ if (o.aborted || l) return;
886
+ let t = e.allocated;
887
+ for (let s = 0; s <= et; s += 1) try {
888
+ let r = new File([new Uint8Array(e.bytes)], t.fileName), a = await i.uploadFile({
889
+ bucket: n,
890
+ path: t.path,
891
+ file: r,
892
+ uploadMode: "create-only"
893
+ }, { signal: o });
894
+ return {
895
+ kind: "uploaded",
896
+ oldFileId: e.fileId,
897
+ newFileId: a.url,
898
+ newTitle: t.isRenamed ? t.fileName : void 0
899
+ };
900
+ } catch (n) {
901
+ if (o.aborted) return;
902
+ if (a(n)) {
903
+ l = !0;
904
+ return;
905
+ }
906
+ if (!(typeof n == "object" && n && "response" in n && n.response?.status === 409) || s === et) return {
907
+ kind: "skipped",
908
+ name: e.originalFileName
909
+ };
910
+ r.markTaken(t.fileName), t = r.allocate(e.originalFileName);
911
+ }
912
+ return {
913
+ kind: "skipped",
914
+ name: e.originalFileName
915
+ };
916
+ }), d = /* @__PURE__ */ new Map(), f = [...c];
917
+ for (let e of u) e.kind === "uploaded" ? d.set(e.oldFileId, {
918
+ url: e.newFileId,
919
+ ...e.newTitle == null ? {} : { title: e.newTitle }
920
+ }) : f.push(e.name);
921
+ return {
922
+ targetMap: d,
923
+ skippedNames: o.aborted || l ? [] : f,
924
+ isUnauthorized: !o.aborted && l
925
+ };
926
+ }, at = ({ conversationsApi: e, filesApi: t, bucket: n, onImported: r, classifyTransferError: i = () => ({}), resolveErrorTraceId: a = async () => void 0, onSuccess: o, onWarning: s, onError: c }) => {
927
+ let l = ke(), u = p(async (u, d, f) => {
928
+ if (!n) {
929
+ c?.({
930
+ jobId: u,
931
+ code: V.MissingBucket
932
+ }), l.updateJob(u, { status: z.Failed });
933
+ return;
934
+ }
935
+ let p = [], m = [], h = [], g = !1, _, v = /* @__PURE__ */ new Date(), y = (e) => !!i(e).isUnauthorized, b;
936
+ if (d.isArchive) {
937
+ let e = `uploads/${L(v)}`, r = [];
938
+ try {
939
+ r = (await t.listFiles({
940
+ bucket: n,
941
+ path: e
942
+ }, { signal: f })).items.map((e) => e.name);
943
+ } catch (e) {
944
+ if (f.aborted) return;
945
+ if (y(e)) {
946
+ c?.({
947
+ jobId: u,
948
+ code: V.Unauthorized
949
+ }), l.updateJob(u, { status: z.Failed });
950
+ return;
951
+ }
952
+ console.error("Failed to list the upload folder for attachment name disambiguation", e);
953
+ }
954
+ b = Ke({
955
+ date: v,
956
+ existingNames: r
957
+ });
958
+ }
959
+ for (let r of d.history) {
960
+ if (f.aborted) return;
961
+ if (g) break;
962
+ try {
963
+ let i = /* @__PURE__ */ new Map();
964
+ if (d.isArchive && b) {
965
+ let e = await it(r, d.attachments, n, b, t, y, f);
966
+ if (f.aborted) return;
967
+ if (e.isUnauthorized) {
968
+ g = !0;
969
+ break;
970
+ }
971
+ e.skippedNames.length > 0 && h.push(...e.skippedNames), i = e.targetMap;
972
+ }
973
+ let { conversation: a, subPath: o } = ve(xe(r, i), n);
974
+ if (await e.saveConversation({
975
+ path: o,
976
+ saveConversationBodyDto: { conversation: {
977
+ ...a,
978
+ llmNamingDone: !0
979
+ } }
980
+ }, { signal: f }), f.aborted) return;
981
+ p.push(r.name);
982
+ } catch (e) {
983
+ if (f.aborted) return;
984
+ if (y(e)) {
985
+ g = !0;
986
+ break;
987
+ }
988
+ m.push(r.name), _ === void 0 && (_ = await a(e)), console.error("Failed to import a conversation", e);
989
+ }
990
+ }
991
+ if (!f.aborted) {
992
+ if (g) {
993
+ c?.({
994
+ jobId: u,
995
+ code: V.Unauthorized
996
+ }), l.updateJob(u, { status: z.Failed });
997
+ return;
998
+ }
999
+ if (p.length > 0) {
1000
+ try {
1001
+ await r?.();
1002
+ } catch {}
1003
+ o?.({
1004
+ jobId: u,
1005
+ titles: p
1006
+ });
1007
+ }
1008
+ m.length > 0 && c?.({
1009
+ jobId: u,
1010
+ code: V.Unknown,
1011
+ titles: m,
1012
+ traceId: _
1013
+ }), h.length > 0 && s?.({
1014
+ jobId: u,
1015
+ code: H.AttachmentSkipped,
1016
+ names: h
1017
+ }), l.updateJob(u, { status: m.length > 0 ? z.Failed : z.Success });
1018
+ }
1019
+ }, [
1020
+ n,
1021
+ i,
1022
+ e,
1023
+ t,
1024
+ c,
1025
+ r,
1026
+ o,
1027
+ s,
1028
+ l,
1029
+ a
1030
+ ]), d = p(async (e) => {
1031
+ let t;
1032
+ try {
1033
+ t = await rt(e);
1034
+ } catch (e) {
1035
+ c?.({
1036
+ jobId: crypto.randomUUID(),
1037
+ code: V.UnsupportedFormat
1038
+ }), e instanceof K || console.error("Failed to parse import file", e);
1039
+ return;
1040
+ }
1041
+ let n = t.history.length > 1, r = t.history[0], i = n ? { kind: B.All } : {
1042
+ kind: B.Single,
1043
+ title: r?.name ?? e.name,
1044
+ sourceBreadcrumb: r ? ye(r) : void 0
1045
+ }, a = l.addJob(i);
1046
+ return l.startJob(a, (e) => u(a, t, e));
1047
+ }, [
1048
+ c,
1049
+ l,
1050
+ u
1051
+ ]);
1052
+ return {
1053
+ jobs: l.jobs,
1054
+ importConversations: d,
1055
+ dismissJob: l.dismissJob,
1056
+ retryJob: l.retryJob,
1057
+ dismissAll: l.dismissAll
1058
+ };
1059
+ }, ot = 80, st = 1, ct = ({ messages: e, isAssistantTyping: t, conversationId: n }) => {
1060
+ let [r, i] = v(!1), a = _(null), o = _(null), s = _(null), c = _(!1), l = _(!1), u = _(null), d = _(/* @__PURE__ */ new Map()), f = p((e, t) => {
102
1061
  t ? d.current.set(e, t) : d.current.delete(e);
103
- }, []), v = g(null), y = f((e) => {
104
- v.current = e;
105
- }, []), b = f((e, t) => {
1062
+ }, []), g = _(null), y = p((e) => {
1063
+ g.current = e;
1064
+ }, []), b = p((e, t) => {
106
1065
  let n = a.current;
107
1066
  n && n.scrollTo({
108
1067
  top: e,
109
1068
  behavior: t ? "instant" : "smooth"
110
1069
  });
111
- }, []), x = f(() => {
1070
+ }, []), x = p(() => {
112
1071
  s.current && (s.current.style.height = "0px"), c.current = !1, l.current = !1, u.current = null;
113
- }, []), S = f(() => {
1072
+ }, []), S = p(() => {
114
1073
  let e = a.current, t = o.current;
115
1074
  if (!e || !t) return null;
116
1075
  let n = e.getBoundingClientRect(), r = t.getBoundingClientRect();
117
1076
  return e.scrollTop + (r.bottom - n.top) - e.clientHeight;
118
- }, []), C = f((e = !1) => {
1077
+ }, []), C = p((e = !1) => {
119
1078
  let t = S();
120
1079
  t != null && b(Math.max(t, 0), e);
121
- }, [b, S]), w = f(() => {
1080
+ }, [b, S]), w = p(() => {
122
1081
  let e = a.current, t = o.current;
123
1082
  if (!e || !t) return null;
124
1083
  let n = e.getBoundingClientRect();
125
1084
  return t.getBoundingClientRect().bottom - n.top - e.clientHeight;
126
- }, []), T = f(() => {
1085
+ }, []), T = p(() => {
127
1086
  if (!l.current) return;
128
1087
  let e = w();
129
1088
  e == null || e < -1 || x();
130
- }, [x, w]), E = f((e) => {
1089
+ }, [x, w]), E = p((e) => {
131
1090
  let t = a.current, n = d.current.get(e);
132
1091
  if (!t || !n) return null;
133
1092
  let r = t.getBoundingClientRect(), i = n.getBoundingClientRect(), o = t.scrollTop + (i.top - r.top);
134
1093
  return Math.max(o, 0);
135
- }, []), D = f((e, t = !1) => {
1094
+ }, []), D = p((e, t = !1) => {
136
1095
  let n = E(e);
137
1096
  n != null && b(n, t);
138
- }, [E, b]), O = f(() => {
1097
+ }, [E, b]), O = p(() => {
139
1098
  let e = S();
140
1099
  return e == null ? null : Math.max(e, 0);
141
- }, [S]), k = f(() => {
1100
+ }, [S]), k = p(() => {
142
1101
  if (!c.current) return null;
143
1102
  let e = u.current, t = O();
144
1103
  return e == null || t == null ? null : Math.max(e, t);
145
- }, [O]), M = f(() => {
1104
+ }, [O]), A = p(() => {
146
1105
  let e = a.current, t = k();
147
- !e || t == null || e.scrollTop > t + j && (e.scrollTop = t);
148
- }, [k]), N = f((e) => {
1106
+ !e || t == null || e.scrollTop > t + st && (e.scrollTop = t);
1107
+ }, [k]), j = p((e) => {
149
1108
  let t = s.current, n = O();
150
1109
  if (!t || n == null) return;
151
1110
  let r = Math.max(e - n, 0);
152
1111
  t.style.height = `${r}px`, c.current = r > 0, l.current = !1, u.current = r > 0 ? e : null;
153
- }, [O]), P = f((e) => {
1112
+ }, [O]), M = p((e) => {
154
1113
  let t = E(e);
155
- t != null && (N(t), D(e, !1));
1114
+ t != null && (j(t), D(e, !1));
156
1115
  }, [
157
- N,
1116
+ j,
158
1117
  E,
159
1118
  D
160
- ]), F = f(() => {
1119
+ ]), N = p(() => {
161
1120
  let e = w();
162
- e != null && i(e >= A);
163
- }, [w]), I = g(e.length), L = g(null);
164
- m(() => {
165
- let r = e.length !== I.current, i = n !== L.current, a = () => {
166
- I.current = e.length, L.current = n;
1121
+ e != null && i(e >= ot);
1122
+ }, [w]), P = _(e.length), F = _(null);
1123
+ h(() => {
1124
+ let r = e.length !== P.current, i = n !== F.current, a = () => {
1125
+ P.current = e.length, F.current = n;
167
1126
  };
168
1127
  i && x();
169
- let o = v.current;
1128
+ let o = g.current;
170
1129
  if (o != null && d.current.has(o)) {
171
- v.current = null, P(o), a();
1130
+ g.current = null, M(o), a();
172
1131
  return;
173
1132
  }
174
1133
  t || ((r || i) && C(!1), a());
@@ -177,52 +1136,273 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
177
1136
  t,
178
1137
  n,
179
1138
  C,
180
- P,
1139
+ M,
181
1140
  x
182
- ]), m(() => {
1141
+ ]), h(() => {
183
1142
  !t && c.current && (l.current = !0, T());
184
1143
  }, [t, T]);
185
- let R = f(() => {
186
- M(), T(), F();
1144
+ let I = p(() => {
1145
+ A(), T(), N();
187
1146
  }, [
188
- M,
1147
+ A,
189
1148
  T,
190
- F
1149
+ N
191
1150
  ]);
192
- return p(() => {
1151
+ return m(() => {
193
1152
  let e = a.current;
194
- if (e) return e.addEventListener("scroll", R, { passive: !0 }), () => e.removeEventListener("scroll", R);
195
- }, [R]), p(() => {
1153
+ if (e) return e.addEventListener("scroll", I, { passive: !0 }), () => e.removeEventListener("scroll", I);
1154
+ }, [I]), m(() => {
196
1155
  let e = o.current;
197
1156
  if (!e) return;
198
1157
  let t = new ResizeObserver(() => {
199
- M(), T(), F();
1158
+ A(), T(), N();
200
1159
  });
201
1160
  return t.observe(e), () => t.disconnect();
202
1161
  }, [
203
- M,
1162
+ A,
204
1163
  T,
205
- F
1164
+ N
206
1165
  ]), {
207
1166
  containerRef: a,
208
1167
  contentRef: o,
209
1168
  spacerRef: s,
210
- setMessageRef: h,
1169
+ setMessageRef: f,
211
1170
  isScrollButtonVisible: r,
212
- scrollToBottom: f(() => {
1171
+ scrollToBottom: p(() => {
213
1172
  C(!1);
214
1173
  }, [C]),
215
1174
  armAnchor: y
216
1175
  };
217
- }, N = {}, P = (e, t = N) => h(() => {
1176
+ }, $ = (e) => {
1177
+ let t = e.messages[e.messages.length - 1];
1178
+ return !!t && t.role === i.Assistant && !t.content && t.streamErrorMessage == null && !t.wasStoppedByUser;
1179
+ }, lt = (e, t) => {
1180
+ let n = [...e];
1181
+ for (let e of t) {
1182
+ let t = n.findIndex((t) => t.index === e.index);
1183
+ if (t >= 0) {
1184
+ let r = n[t];
1185
+ n[t] = {
1186
+ ...r,
1187
+ ...e,
1188
+ body: {
1189
+ ...r.body,
1190
+ ...e.body,
1191
+ title: (r.body?.title ?? "") + (e.body?.title ?? "") || void 0,
1192
+ quote: (r.body?.quote ?? "") + (e.body?.quote ?? "") || void 0
1193
+ }
1194
+ };
1195
+ } else n.push(e);
1196
+ }
1197
+ return n;
1198
+ }, ut = (e, t) => {
1199
+ let n = [...e];
1200
+ for (let e of t) {
1201
+ let t = e.index == null ? -1 : n.findIndex((t) => t.index === e.index);
1202
+ t >= 0 ? n[t] = {
1203
+ ...n[t],
1204
+ ...e,
1205
+ title: (n[t].title ?? "") + (e.title ?? ""),
1206
+ data: e.data == null ? n[t].data : (n[t].data ?? "") + e.data
1207
+ } : n.push(e);
1208
+ }
1209
+ return n;
1210
+ }, dt = (e, t) => {
1211
+ let n = [...e];
1212
+ for (let e of t) {
1213
+ let t = n.findIndex((t) => t.index === e.index);
1214
+ t >= 0 ? n[t] = {
1215
+ ...n[t],
1216
+ ...e,
1217
+ name: (n[t].name ?? "") + (e.name ?? ""),
1218
+ content: (n[t].content ?? "") + (e.content ?? "") || void 0,
1219
+ attachments: e.attachments?.length ? ut(n[t].attachments ?? [], e.attachments) : n[t].attachments
1220
+ } : n.push({
1221
+ ...e,
1222
+ name: e.name ?? ""
1223
+ });
1224
+ }
1225
+ return n;
1226
+ }, ft = (e, t, n) => {
1227
+ let r = n.choices[0]?.delta, i = r?.content ?? "", a = r?.custom_content?.form_schema, o = r?.custom_content?.attachments, s = r?.custom_content?.stages, c = r?.custom_content?.annotations, l = r?.custom_fields?.annotations, d = r?.custom_content?.state, f = !!i || !!a || !!o?.length || !!s?.length || !!c?.length || !!l?.length || !!d, p = r?.responseId ?? (f ? n.id : void 0);
1228
+ return !f && !p ? null : e.map((e, n) => {
1229
+ if (n !== t) return e;
1230
+ let r = [...e.custom_content?.attachments ?? [], ...o ?? []], f = l?.length ? u(l, r) : [], m = [...c ?? [], ...f], h = a || o?.length || s?.length || m.length || d;
1231
+ return {
1232
+ ...e,
1233
+ content: i ? e.content + i : e.content,
1234
+ ...p && { responseId: p },
1235
+ ...h && { custom_content: {
1236
+ ...e.custom_content,
1237
+ ...a && { form_schema: a },
1238
+ ...o?.length && { attachments: r },
1239
+ ...s?.length && { stages: dt(e.custom_content?.stages ?? [], s) },
1240
+ ...m.length && { annotations: lt(e.custom_content?.annotations ?? [], m) },
1241
+ ...d && { state: d }
1242
+ } }
1243
+ };
1244
+ });
1245
+ }, pt = 300 * 1e3, mt = ({ conversationId: e, state: { setConversation: t, conversationRef: n }, transport: r, generation: { startGeneration: i, completeGeneration: a }, channel: o, overlay: s, onStopError: c }) => {
1246
+ let [l, u] = v(() => /* @__PURE__ */ new Set()), [d, f] = v(null), h = _(null), g = _(null), y = _(/* @__PURE__ */ new Set()), b = _(/* @__PURE__ */ new Set()), x = _(e);
1247
+ m(() => {
1248
+ x.current = e;
1249
+ }, [e]);
1250
+ let S = p((e) => Q(x.current ?? "") === e, []), C = p((e) => {
1251
+ u((t) => {
1252
+ if (t.has(e)) return t;
1253
+ let n = new Set(t);
1254
+ return n.add(e), n;
1255
+ });
1256
+ }, []), w = p((e) => {
1257
+ u((t) => {
1258
+ if (!t.has(e)) return t;
1259
+ let n = new Set(t);
1260
+ return n.delete(e), n;
1261
+ });
1262
+ }, []), E = p((e, c, l, u, d, p, m = T.Append) => {
1263
+ let _ = p ?? crypto.randomUUID(), v = Q(e);
1264
+ h.current = _, g.current = v, f(v);
1265
+ let y;
1266
+ m === T.Regenerate ? y = l : m === T.Edit && (y = l - 1);
1267
+ let x = i(v, _);
1268
+ C(v), s?.notifyGenerationStart?.(), o?.ensureConnected(), r.streamCompletion(v, c, u, {
1269
+ signal: x.signal,
1270
+ onChunk: (e) => {
1271
+ h.current === _ && S(v) && t((t) => {
1272
+ if (!t) return t;
1273
+ let r = ft(t.messages, l, e);
1274
+ if (!r) return t;
1275
+ let i = {
1276
+ ...t,
1277
+ messages: r
1278
+ };
1279
+ return n.current = i, i;
1280
+ });
1281
+ },
1282
+ onComplete: async () => {
1283
+ if (w(v), h.current === _ && (h.current = null, g.current = null, f(null)), a(v, _), b.current.has(_) ? b.current.delete(_) : s?.notifyGenerationEnd?.(), S(v)) try {
1284
+ let i = await r.getConversation(Z(e));
1285
+ if (!S(v)) return;
1286
+ t(i), n.current = i;
1287
+ } catch {}
1288
+ },
1289
+ onError: (e) => {
1290
+ w(v), h.current === _ && (h.current = null, g.current = null, f(null)), S(v) && t((t) => {
1291
+ if (!t) return t;
1292
+ let r = {
1293
+ ...t,
1294
+ messages: t.messages.map((t, n) => n === l ? {
1295
+ ...t,
1296
+ streamErrorMessage: e.message
1297
+ } : t)
1298
+ };
1299
+ return n.current = r, r;
1300
+ });
1301
+ }
1302
+ }, d, _, m, y, o?.channelId ?? void 0);
1303
+ }, [
1304
+ i,
1305
+ a,
1306
+ C,
1307
+ w,
1308
+ S,
1309
+ o?.channelId,
1310
+ o?.ensureConnected,
1311
+ s,
1312
+ r
1313
+ ]), D = p(() => {
1314
+ let t = h.current;
1315
+ if (!t || !e) return;
1316
+ let n = Q(e);
1317
+ g.current === n && (b.current.add(t), s?.notifyStopGenerating?.(), r.stopCompletion({
1318
+ generationId: t,
1319
+ path: n
1320
+ }).catch((e) => {
1321
+ let t = e instanceof Error ? e : Error(String(e));
1322
+ c?.(t);
1323
+ }));
1324
+ }, [
1325
+ e,
1326
+ c,
1327
+ s,
1328
+ r
1329
+ ]), O = p((e, i) => {
1330
+ if (!$(i)) return;
1331
+ let a = Q(e);
1332
+ if (y.current.has(a)) return;
1333
+ y.current.add(a), C(a);
1334
+ let o = new AbortController(), s = (e) => {
1335
+ y.current.delete(a), w(a), e && S(a) && (t(e), n.current = e);
1336
+ }, c = async () => {
1337
+ try {
1338
+ s(await r.getConversation(Z(e)));
1339
+ } catch {
1340
+ s();
1341
+ }
1342
+ };
1343
+ (async () => {
1344
+ let t;
1345
+ try {
1346
+ t = await r.watchConversation(a, o.signal);
1347
+ } catch {
1348
+ await c();
1349
+ return;
1350
+ }
1351
+ let n = t.getReader(), i = new TextDecoder(), l = "", u = window.setTimeout(() => {
1352
+ o.abort();
1353
+ }, pt);
1354
+ try {
1355
+ for (;;) {
1356
+ let { done: t, value: a } = await n.read();
1357
+ if (t) break;
1358
+ l += i.decode(a, { stream: !0 });
1359
+ let o = l.split("\n");
1360
+ l = o.pop() ?? "";
1361
+ for (let t of o) {
1362
+ let n = t.trim();
1363
+ if (!n.startsWith("data:")) continue;
1364
+ let i = n.slice(5).trim(), a = null;
1365
+ try {
1366
+ a = JSON.parse(i);
1367
+ } catch {
1368
+ continue;
1369
+ }
1370
+ if (a?.action === "UPDATE") try {
1371
+ let t = await r.getConversation(Z(e));
1372
+ if (!$(t)) {
1373
+ s(t);
1374
+ return;
1375
+ }
1376
+ } catch {}
1377
+ }
1378
+ }
1379
+ } catch {} finally {
1380
+ clearTimeout(u), n.releaseLock();
1381
+ }
1382
+ await c();
1383
+ })();
1384
+ }, [
1385
+ C,
1386
+ w,
1387
+ S,
1388
+ r
1389
+ ]), k = e != null && l.has(Q(e)), A = e == null ? null : Q(e);
1390
+ return {
1391
+ startStream: E,
1392
+ handleStop: D,
1393
+ resumeIfAwaitingGeneration: O,
1394
+ isStreaming: k,
1395
+ canStopStreaming: A != null && d === A
1396
+ };
1397
+ }, ht = {}, gt = (e, t = ht) => g(() => {
218
1398
  let n = [], r = [], a = [], s = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Set();
219
- for (let f of e) {
220
- let e = f.custom_content?.attachments;
221
- if (f.role === i.User) for (let r of e ?? []) {
1399
+ for (let d of e) {
1400
+ let e = d.custom_content?.attachments;
1401
+ if (d.role === i.User) for (let r of e ?? []) {
222
1402
  let e = o(r, t);
223
1403
  c.has(e.id) || (c.add(e.id), n.push(e));
224
1404
  }
225
- else if (f.role === i.Assistant) {
1405
+ else if (d.role === i.Assistant) {
226
1406
  let n = e?.filter((e) => !l(e));
227
1407
  for (let e of n ?? []) {
228
1408
  let n = o(e, t);
@@ -238,7 +1418,7 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
238
1418
  quote: t.data
239
1419
  }));
240
1420
  }
241
- for (let e of d(f)) {
1421
+ for (let e of f(d)) {
242
1422
  let t = e?.body?.source?.attachment, n = t?.url;
243
1423
  !n || s.has(n) || (s.add(n), a.push({
244
1424
  url: n,
@@ -254,15 +1434,15 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
254
1434
  generated: r,
255
1435
  sources: a
256
1436
  };
257
- }, [e, t]), F = (e = !0, t = !0) => {
258
- let [n, r] = _(!1), [i, a] = _([]), o = g(0), s = g(e), c = g(t);
259
- m(() => {
1437
+ }, [e, t]), _t = (e = !0, t = !0) => {
1438
+ let [n, r] = v(!1), [i, a] = v([]), o = _(0), s = _(e), c = _(t);
1439
+ h(() => {
260
1440
  s.current = e, c.current = t, t || (o.current = 0, r(!1));
261
1441
  }, [e, t]);
262
- let l = f(() => {
1442
+ let l = p(() => {
263
1443
  a([]);
264
1444
  }, []);
265
- return p(() => {
1445
+ return m(() => {
266
1446
  let e = (e) => !!e.dataTransfer?.types.includes("Files"), t = (t) => {
267
1447
  !c.current || !e(t) || (o.current += 1, r(!0));
268
1448
  }, n = (t) => {
@@ -283,21 +1463,21 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
283
1463
  pendingFiles: i,
284
1464
  onFilesConsumed: l
285
1465
  };
286
- }, I = () => typeof window < "u" ? window.innerWidth : 1024, L = () => {
287
- let [e, t] = _(I);
288
- return p(() => {
1466
+ }, vt = () => typeof window < "u" ? window.innerWidth : 1024, yt = () => {
1467
+ let [e, t] = v(vt);
1468
+ return m(() => {
289
1469
  if (typeof window > "u") return;
290
1470
  let e = () => t(window.innerWidth);
291
1471
  return window.addEventListener("resize", e), () => window.removeEventListener("resize", e);
292
1472
  }, []), e;
293
- }, R = (e) => {
294
- let t = L();
1473
+ }, bt = (e) => {
1474
+ let t = yt();
295
1475
  return Math.max(0, t - e);
296
- }, z = (e) => e.map((e) => e === v.Edit ? y.Edit : y.View), B = (e, t) => {
1476
+ }, xt = (e) => e.map((e) => e === E.Edit ? D.Edit : D.View), St = (e, t) => {
297
1477
  let n = new URL(e, t);
298
1478
  return `${t}${n.pathname}${n.search}${n.hash}`;
299
- }, V = (e, t, n, r = window.location.origin) => {
300
- let [i, a] = _(), [o, s] = _(!0), [c, l] = _(null), u = g(0), d = f(async (i = [y.View]) => {
1479
+ }, Ct = (e, t, n, r = window.location.origin) => {
1480
+ let [i, a] = v(), [o, s] = v(!0), [c, l] = v(null), u = _(0), d = p(async (i = [D.View]) => {
301
1481
  let o = ++u.current;
302
1482
  s(!0), l(null);
303
1483
  try {
@@ -307,9 +1487,9 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
307
1487
  access: i
308
1488
  } });
309
1489
  u.current === o && a({
310
- url: B(s.url, r),
1490
+ url: St(s.url, r),
311
1491
  expiresInDays: s.expiresInDays,
312
- access: z(s.access)
1492
+ access: xt(s.access)
313
1493
  });
314
1494
  } catch (e) {
315
1495
  u.current === o && l(e instanceof Error ? e : /* @__PURE__ */ Error("Failed to create share link"));
@@ -322,22 +1502,22 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
322
1502
  n,
323
1503
  e
324
1504
  ]);
325
- return p(() => {
1505
+ return m(() => {
326
1506
  a(void 0), d();
327
1507
  }, [t, d]), {
328
1508
  data: i,
329
1509
  isLoading: o,
330
1510
  error: c,
331
- setAccess: f((e) => {
1511
+ setAccess: p((e) => {
332
1512
  d(e);
333
1513
  }, [d])
334
1514
  };
335
- }, H = /* @__PURE__ */ function(e) {
1515
+ }, wt = /* @__PURE__ */ function(e) {
336
1516
  return e.Idle = "idle", e.Loading = "loading", e.Resolved = "resolved", e.Unknown = "unknown", e;
337
- }({}), U = { status: "idle" }, W = (e) => {
338
- let [t, n] = _({}), r = g(/* @__PURE__ */ new Set());
1517
+ }({}), Tt = { status: "idle" }, Et = (e) => {
1518
+ let [t, n] = v({}), r = _(/* @__PURE__ */ new Set());
339
1519
  return {
340
- requestRecipientsCount: f((t) => {
1520
+ requestRecipientsCount: p((t) => {
341
1521
  r.current.has(t) || (r.current.add(t), n((e) => ({
342
1522
  ...e,
343
1523
  [t]: { status: "loading" }
@@ -359,11 +1539,11 @@ var b = (e) => e.startsWith("files/"), x = (e) => e.url != null && b(e.url) || e
359
1539
  }
360
1540
  })());
361
1541
  }, [e]),
362
- getRecipientsCount: f((e) => t[e] ?? U, [t]),
363
- invalidateRecipientsCount: f((e) => {
1542
+ getRecipientsCount: p((e) => t[e] ?? Tt, [t]),
1543
+ invalidateRecipientsCount: p((e) => {
364
1544
  r.current.delete(e), n(({ [e]: t, ...n }) => n);
365
1545
  }, [])
366
1546
  };
367
1547
  };
368
1548
  //#endregion
369
- export { H as RecipientsCountStatus, S as downloadAttachment, b as isDialFileId, x as isDownloadableAttachment, T as useAttachmentAction, k as useAttachmentUpload, M as useConversationScroll, P as useConversationSources, F as usePageFileDrag, R as usePanelMaxWidth, V as useShareLink, W as useShareRecipientsCount, L as useViewportWidth };
1549
+ export { ne as AttachmentValidationErrorReason, ae as ConversationExportMode, V as ConversationTransferErrorCode, z as ConversationTransferJobStatus, B as ConversationTransferSubjectKind, H as ConversationTransferWarningCode, U as EXPORT_APP_NAME, R as ExportFileNameKind, wt as RecipientsCountStatus, Le as attachmentToDto, J as attachmentsToDtos, Y as createMessagePair, A as downloadAttachment, L as formatDateYM, ie as formatDateYMD, be as formatQuotedNameList, Q as getConversationPath, Be as getStarterConversationText, Ve as getStarterSubmitText, X as hasActiveToolConfig, $ as isAwaitingGenerationResume, O as isDialFileId, k as isDownloadableAttachment, Re as isMessageChanged, N as useAttachmentAction, De as useAttachmentUpload, re as useAttachmentValidation, Ie as useConversationExport, He as useConversationHandlers, at as useConversationImport, ct as useConversationScroll, gt as useConversationSources, mt as useConversationStream, _t as usePageFileDrag, bt as usePanelMaxWidth, Ct as useShareLink, Et as useShareRecipientsCount, yt as useViewportWidth };