@useago/sdk 0.4.5 → 0.4.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/dist/widget.js CHANGED
@@ -1,7 +1,190 @@
1
- import { t as ge } from "./AgoClient-rvTz2thR.js";
2
- import { t as ue } from "./createFormCollector-Cujr7hW4.js";
3
- import { t as xe } from "./createConversationSession-DbuVzvoX.js";
4
- var oe = 'var(--ago-font, "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif)', Z = {
1
+ import { t as Ie } from "./AgoClient-BrYhSO5M.js";
2
+ import { t as Te } from "./createFormCollector-CPS2phYq.js";
3
+ import { t as Ne } from "./createConversationSession-Dmqd80DX.js";
4
+ var Oe = {
5
+ "&": "&",
6
+ "<": "&lt;",
7
+ ">": "&gt;",
8
+ '"': "&quot;",
9
+ "'": "&#39;"
10
+ };
11
+ function H(o) {
12
+ return o.replace(/[&<>"']/g, (n) => Oe[n]);
13
+ }
14
+ function de(o) {
15
+ const n = o.trim();
16
+ return /^(https?:|mailto:|tel:|#|\/|\.\/|\.\.\/)/i.test(n) || !/^[a-z][a-z0-9+.-]*:/i.test(n) ? n : null;
17
+ }
18
+ var ge = '"IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace', j = "";
19
+ function L(o) {
20
+ const n = [], r = (g) => (n.push(g), `${j}${n.length - 1}${j}`);
21
+ let e = o;
22
+ return e = e.replace(/`([^`]+)`/g, (g, a) => r(`<code style="background:#f0f2f5;padding:2px 5px;border-radius:4px;font-size:13px;font-family:${ge}">${H(a)}</code>`)), e = e.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (g, a, p) => {
23
+ const i = de(p);
24
+ return i ? r(`<img alt="${H(a)}" src="${H(i)}" style="max-width:100%;border-radius:8px;margin:4px 0" />`) : g;
25
+ }), e = e.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (g, a, p) => {
26
+ const i = de(p);
27
+ return i ? r(`<a href="${H(i)}" target="_blank" rel="noopener noreferrer" style="color:#1b5fc4;text-decoration:underline">${L(a)}</a>`) : g;
28
+ }), e = H(e), e = e.replace(/\*\*([^*]+?)\*\*/g, "<strong>$1</strong>").replace(/__([^_]+?)__/g, "<strong>$1</strong>").replace(/~~([^~]+?)~~/g, "<del>$1</del>").replace(/(^|[^*])\*(?!\s)([^*]+?)\*/g, "$1<em>$2</em>").replace(/(^|[^\w_])_(?!\s)([^_]+?)_/g, "$1<em>$2</em>"), e = e.replace(new RegExp(`${j}(\\d+)${j}`, "g"), (g, a) => n[Number(a)]), e;
29
+ }
30
+ function w(o, n, r) {
31
+ const e = document.createElement(o);
32
+ return Object.assign(e.style, n), r !== void 0 && (e.innerHTML = r), e;
33
+ }
34
+ var ue = /^ {0,3}([-*_])( *\1){2,} *$/, me = /^ {0,3}(#{1,6})\s+(.*?)\s*#*\s*$/, he = /^ {0,3}(```+|~~~+)/, K = /^ {0,3}>\s?/, B = /^(\s*)([-*+]|\d+[.)])\s+(.*)$/, Ae = [
35
+ "20px",
36
+ "18px",
37
+ "16px",
38
+ "15px",
39
+ "14px",
40
+ "13px"
41
+ ], J = (o) => o.match(/^\s*/)?.[0].length ?? 0;
42
+ function Le(o) {
43
+ return o.trim() === "" || ue.test(o) || me.test(o) || he.test(o) || K.test(o) || B.test(o);
44
+ }
45
+ function xe(o) {
46
+ const n = o[0].match(B), r = n[1].length, e = /\d/.test(n[2]), g = w(e ? "ol" : "ul", {
47
+ margin: "0 0 8px 0",
48
+ paddingLeft: "20px",
49
+ listStyleType: e ? "decimal" : "disc",
50
+ listStylePosition: "outside"
51
+ });
52
+ let a = 0;
53
+ for (; a < o.length; ) {
54
+ const p = o[a].match(B), i = [];
55
+ for (a++; a < o.length && J(o[a]) > r; )
56
+ i.push(o[a]), a++;
57
+ const f = w("li", {
58
+ display: "list-item",
59
+ margin: "2px 0",
60
+ lineHeight: "1.6"
61
+ }), d = p[3].match(/^\[([ xX])\]\s+(.*)$/);
62
+ d ? (f.style.listStyleType = "none", f.style.marginLeft = "-20px", f.innerHTML = `<span style="margin-right:6px">${d[1].toLowerCase() === "x" ? "☑" : "☐"}</span>${L(d[2])}`) : f.innerHTML = L(p[3]), i.filter((b) => B.test(b)).length > 0 && f.appendChild(xe(i.filter((b) => b.trim() !== ""))), g.appendChild(f);
63
+ }
64
+ return g;
65
+ }
66
+ function De(o) {
67
+ const n = (i) => i.replace(/^\s*\|?/, "").replace(/\|?\s*$/, "").split("|").map((f) => f.trim()), r = w("div", {
68
+ overflowX: "auto",
69
+ margin: "0 0 8px 0"
70
+ }), e = w("table", {
71
+ borderCollapse: "collapse",
72
+ width: "100%",
73
+ fontSize: "13px"
74
+ }), g = document.createElement("thead"), a = document.createElement("tr");
75
+ for (const i of n(o[0])) a.appendChild(w("th", {
76
+ border: "1px solid #dee3e8",
77
+ padding: "6px 10px",
78
+ textAlign: "left",
79
+ backgroundColor: "#f5f7fa",
80
+ fontWeight: "600"
81
+ }, L(i)));
82
+ g.appendChild(a), e.appendChild(g);
83
+ const p = document.createElement("tbody");
84
+ for (const i of o.slice(2)) {
85
+ const f = document.createElement("tr");
86
+ for (const d of n(i)) f.appendChild(w("td", {
87
+ border: "1px solid #dee3e8",
88
+ padding: "6px 10px"
89
+ }, L(d)));
90
+ p.appendChild(f);
91
+ }
92
+ return e.appendChild(p), r.appendChild(e), r;
93
+ }
94
+ var Me = (o) => !!o && /^\s*\|?\s*:?-{1,}:?\s*(\|\s*:?-{1,}:?\s*)*\|?\s*$/.test(o);
95
+ function be(o) {
96
+ const n = document.createDocumentFragment(), r = o.replace(/\r\n?/g, `
97
+ `).split(`
98
+ `);
99
+ let e = 0;
100
+ for (; e < r.length; ) {
101
+ const a = r[e];
102
+ if (a.trim() === "") {
103
+ e++;
104
+ continue;
105
+ }
106
+ const p = a.match(he);
107
+ if (p) {
108
+ const d = p[1][0], b = [];
109
+ for (e++; e < r.length && !new RegExp(`^ {0,3}${d === "`" ? "`{3,}" : "~{3,}"}\\s*$`).test(r[e]); )
110
+ b.push(r[e]), e++;
111
+ e++;
112
+ const N = w("pre", { margin: "0 0 8px 0" });
113
+ N.appendChild(w("code", {
114
+ display: "block",
115
+ background: "#f5f5f5",
116
+ padding: "8px 12px",
117
+ borderRadius: "6px",
118
+ fontSize: "13px",
119
+ overflowX: "auto",
120
+ fontFamily: ge,
121
+ whiteSpace: "pre"
122
+ }, H(b.join(`
123
+ `)))), n.appendChild(N);
124
+ continue;
125
+ }
126
+ if (ue.test(a)) {
127
+ n.appendChild(w("hr", {
128
+ border: "none",
129
+ borderTop: "1px solid #dee3e8",
130
+ margin: "12px 0"
131
+ })), e++;
132
+ continue;
133
+ }
134
+ const i = a.match(me);
135
+ if (i) {
136
+ const d = i[1].length;
137
+ n.appendChild(w(`h${d}`, {
138
+ margin: "16px 0 8px 0",
139
+ fontSize: Ae[d - 1],
140
+ fontWeight: "600",
141
+ lineHeight: "1.3"
142
+ }, L(i[2]))), e++;
143
+ continue;
144
+ }
145
+ if (K.test(a)) {
146
+ const d = [];
147
+ for (; e < r.length && K.test(r[e]); )
148
+ d.push(r[e].replace(K, "")), e++;
149
+ const b = w("blockquote", {
150
+ borderLeft: "4px solid #dee3e8",
151
+ paddingLeft: "12px",
152
+ margin: "0 0 8px 0",
153
+ fontStyle: "italic",
154
+ color: "#6b6d6f"
155
+ });
156
+ b.appendChild(be(d.join(`
157
+ `))), n.appendChild(b);
158
+ continue;
159
+ }
160
+ if (a.includes("|") && Me(r[e + 1])) {
161
+ const d = [a, r[e + 1]];
162
+ for (e += 2; e < r.length && r[e].includes("|") && r[e].trim(); )
163
+ d.push(r[e]), e++;
164
+ n.appendChild(De(d));
165
+ continue;
166
+ }
167
+ if (B.test(a)) {
168
+ const d = [], b = J(a);
169
+ for (; e < r.length && r[e].trim() !== ""; )
170
+ if (B.test(r[e]) || J(r[e]) > b)
171
+ d.push(r[e]), e++;
172
+ else break;
173
+ n.appendChild(xe(d));
174
+ continue;
175
+ }
176
+ const f = [a];
177
+ for (e++; e < r.length && !Le(r[e]); )
178
+ f.push(r[e]), e++;
179
+ n.appendChild(w("p", {
180
+ margin: "0 0 8px 0",
181
+ lineHeight: "1.6"
182
+ }, f.map((d) => L(d)).join("<br>")));
183
+ }
184
+ const g = n.lastElementChild;
185
+ return g && (g.style.marginBottom = "0"), n;
186
+ }
187
+ var Ce = 'var(--ago-font, "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif)', ce = {
5
188
  font: "--ago-font",
6
189
  radius: "--ago-radius",
7
190
  messageRadius: "--ago-message-radius",
@@ -15,109 +198,109 @@ var oe = 'var(--ago-font, "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "S
15
198
  mutedText: "--ago-muted-text-color",
16
199
  border: "--ago-border-color",
17
200
  accent: "--ago-accent-color"
18
- }, ne = "var(--ago-brand-color, #03182f)", re = "var(--ago-brand-text-color, #fff)", me = "var(--ago-header-background, var(--ago-brand-color, #03182f))", he = "var(--ago-header-text-color, #e8f0fe)", L = "var(--ago-panel-background, #fff)", be = "var(--ago-messages-background, #fbfbfb)", H = "var(--ago-text-color, #30373e)", ve = "var(--ago-muted-text-color, #6b6d6f)", E = "var(--ago-border-color, #dee3e8)", K = "var(--ago-accent-color, #1b5fc4)", Ce = "var(--ago-radius, 16px)", ee = "var(--ago-message-radius, 16px)";
19
- function ye(i, o) {
20
- if (o)
21
- for (const g of Object.keys(Z)) {
22
- const w = o[g];
23
- w != null && i.style.setProperty(Z[g], w);
201
+ }, ve = "var(--ago-brand-color, #03182f)", Ee = "var(--ago-brand-text-color, #fff)", He = "var(--ago-header-background, var(--ago-brand-color, #03182f))", Be = "var(--ago-header-text-color, #e8f0fe)", z = "var(--ago-panel-background, #fff)", Ue = "var(--ago-messages-background, #fbfbfb)", X = "var(--ago-text-color, #30373e)", ze = "var(--ago-muted-text-color, #6b6d6f)", $ = "var(--ago-border-color, #dee3e8)", Z = "var(--ago-accent-color, #1b5fc4)", Fe = "var(--ago-radius, 16px)", pe = "var(--ago-message-radius, 16px)";
202
+ function We(o, n) {
203
+ if (n)
204
+ for (const r of Object.keys(ce)) {
205
+ const e = n[r];
206
+ e != null && o.style.setProperty(ce[r], e);
24
207
  }
25
208
  }
26
- function m(i, o) {
27
- Object.assign(i.style, o);
209
+ function R(o, n) {
210
+ Object.assign(o.style, n);
28
211
  }
29
- function c(i = {}) {
30
- const o = document.createElement("div");
31
- return m(o, i), o;
212
+ function x(o = {}) {
213
+ const n = document.createElement("div");
214
+ return R(n, o), n;
32
215
  }
33
- var te = "ago-chat-widget-keyframes";
34
- function Ee() {
35
- if (typeof document > "u" || document.getElementById(te)) return;
36
- const i = document.createElement("style");
37
- i.id = te, i.textContent = "@keyframes ago-pulse { 0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); } 40% { opacity: 1; transform: scale(1); } }", document.head.appendChild(i);
216
+ var fe = "ago-chat-widget-keyframes";
217
+ function Pe() {
218
+ if (typeof document > "u" || document.getElementById(fe)) return;
219
+ const o = document.createElement("style");
220
+ o.id = fe, o.textContent = "@keyframes ago-pulse { 0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); } 40% { opacity: 1; transform: scale(1); } }", document.head.appendChild(o);
38
221
  }
39
- function we(i) {
40
- const o = typeof i == "string" ? document.querySelector(i) : i;
41
- if (!o) throw new Error(`mountChatWidget: target "${String(i)}" not found.`);
42
- return o;
222
+ function Ge(o) {
223
+ const n = typeof o == "string" ? document.querySelector(o) : o;
224
+ if (!n) throw new Error(`mountChatWidget: target "${String(o)}" not found.`);
225
+ return n;
43
226
  }
44
- function Ie(i, o) {
45
- const { title: g = "Chat", welcomeMessage: w = "Hello! How can I help you today?", placeholder: O = "Type a message...", allowFiles: u = !1, height: x = 500, logoUrl: f, showAgentName: v = !1, theme: T, loadThreads: D = !1, forms: B, onFollowUpClick: s, onMessageSent: C, onMessageReceived: $ } = o;
46
- if (!o.client && !o.config?.baseUrl) throw new Error("mountChatWidget requires either `client` or `config` (with a baseUrl).");
47
- const l = o.client ?? new ge(o.config), k = we(i), U = o.persistConversation, I = U ? xe(U === !0 ? {} : U) : void 0;
48
- let h = o.conversationId ?? I?.getLastActiveThread() ?? void 0, n = [], _ = !1, N = null;
49
- const M = [], A = c({
227
+ function Ve(o, n) {
228
+ const { title: r = "Chat", welcomeMessage: e = "Hello! How can I help you today?", placeholder: g = "Type a message...", allowFiles: a = !1, height: p = 500, logoUrl: i, showAgentName: f = !1, theme: d, loadThreads: b = !1, forms: N, onFollowUpClick: u, onMessageSent: _, onMessageReceived: F } = n;
229
+ if (!n.client && !n.config?.baseUrl) throw new Error("mountChatWidget requires either `client` or `config` (with a baseUrl).");
230
+ const h = n.client ?? new Ie(n.config), O = Ge(o), W = n.persistConversation, D = W ? Ne(W === !0 ? {} : W) : void 0;
231
+ let S = n.conversationId ?? D?.getLastActiveThread() ?? void 0, l = [], I = !1, U = null;
232
+ const P = [], M = x({
50
233
  display: "flex",
51
234
  flexDirection: "column",
52
- height: typeof x == "number" ? `${x}px` : x,
53
- border: `1px solid ${E}`,
54
- borderRadius: Ce,
235
+ height: typeof p == "number" ? `${p}px` : p,
236
+ border: `1px solid ${$}`,
237
+ borderRadius: Fe,
55
238
  overflow: "hidden",
56
- backgroundColor: L,
57
- fontFamily: oe,
239
+ backgroundColor: z,
240
+ fontFamily: Ce,
58
241
  textAlign: "left",
59
242
  boxShadow: "rgba(15, 15, 15, 0.08) 0px 2px 16px 0px"
60
243
  });
61
- A.className = "ago-chat-widget", ye(A, T);
62
- const z = c({
244
+ M.className = "ago-chat-widget", We(M, d);
245
+ const G = x({
63
246
  padding: "14px 16px",
64
- borderBottom: `1px solid ${E}`,
65
- backgroundColor: me,
66
- color: he,
247
+ borderBottom: `1px solid ${$}`,
248
+ backgroundColor: He,
249
+ color: Be,
67
250
  display: "flex",
68
251
  alignItems: "center",
69
252
  gap: "10px"
70
253
  });
71
- if (z.className = "ago-chat-widget__header", f) {
72
- const e = document.createElement("img");
73
- e.src = f, e.alt = "Logo", m(e, {
254
+ if (G.className = "ago-chat-widget__header", i) {
255
+ const t = document.createElement("img");
256
+ t.src = i, t.alt = "Logo", R(t, {
74
257
  height: "24px",
75
258
  width: "auto"
76
- }), z.appendChild(e);
259
+ }), G.appendChild(t);
77
260
  }
78
- const W = document.createElement("h3");
79
- W.textContent = g, m(W, {
261
+ const q = document.createElement("h3");
262
+ q.textContent = r, R(q, {
80
263
  margin: "0",
81
264
  fontSize: "15px",
82
265
  fontWeight: "600"
83
- }), z.appendChild(W);
84
- const R = c({
266
+ }), G.appendChild(q);
267
+ const T = x({
85
268
  flex: "1",
86
269
  overflow: "auto",
87
270
  padding: "16px",
88
- backgroundColor: be
271
+ backgroundColor: Ue
89
272
  });
90
- R.className = "ago-chat-widget__messages";
91
- const { inputRow: ae, setDisabled: se, focus: ie } = Re({
92
- placeholder: O,
93
- allowFiles: u,
94
- onSend: (e, t) => {
95
- F(e, t);
273
+ T.className = "ago-chat-widget__messages";
274
+ const { inputRow: ye, setDisabled: we, focus: Re } = je({
275
+ placeholder: g,
276
+ allowFiles: a,
277
+ onSend: (t, s) => {
278
+ V(t, s);
96
279
  }
97
280
  });
98
- Ee(), A.append(z, R, ae), k.appendChild(A);
99
- const X = s !== !1, j = s === !1 ? void 0 : s ?? ((e) => {
100
- F(e);
281
+ Pe(), M.append(G, T, ye), O.appendChild(M);
282
+ const ee = u !== !1, te = u === !1 ? void 0 : u ?? ((t) => {
283
+ V(t);
101
284
  });
102
- function le(e) {
103
- const t = e.role === "user", d = c({
285
+ function Se(t, s) {
286
+ const c = t.role === "user", y = x({
104
287
  display: "flex",
105
288
  flexDirection: "column",
106
- alignItems: t ? "flex-end" : "flex-start",
289
+ alignItems: c ? "flex-end" : "flex-start",
107
290
  marginBottom: "16px"
108
291
  });
109
- if (d.className = `ago-message ago-message--${e.role}`, !t && v && e.agent) {
110
- const r = c({
292
+ if (y.className = `ago-message ago-message--${t.role}`, !c && f && t.agent) {
293
+ const m = x({
111
294
  fontSize: "13px",
112
295
  fontWeight: "500",
113
- color: H,
296
+ color: X,
114
297
  marginBottom: "6px",
115
298
  padding: "0 4px"
116
299
  });
117
- r.className = "ago-message__agent", r.textContent = e.agent.displayName || e.agent.name, d.appendChild(r);
300
+ m.className = "ago-message__agent", m.textContent = t.agent.displayName || t.agent.name, y.appendChild(m);
118
301
  }
119
- if (!t && e.sources && e.sources.length > 0) {
120
- const r = c({
302
+ if (!c && t.sources && t.sources.length > 0) {
303
+ const m = x({
121
304
  display: "grid",
122
305
  gridTemplateColumns: "1fr 1fr",
123
306
  gap: "6px",
@@ -125,28 +308,28 @@ function Ie(i, o) {
125
308
  width: "100%",
126
309
  maxWidth: "85%"
127
310
  });
128
- r.className = "ago-message__sources", e.sources.forEach((p, a) => {
129
- const y = p.url ? document.createElement("a") : document.createElement("div");
130
- p.url && y instanceof HTMLAnchorElement && (y.href = p.url, y.target = "_blank", y.rel = "noopener noreferrer"), m(y, {
311
+ m.className = "ago-message__sources", t.sources.forEach((C, E) => {
312
+ const A = C.url ? document.createElement("a") : document.createElement("div");
313
+ C.url && A instanceof HTMLAnchorElement && (A.href = C.url, A.target = "_blank", A.rel = "noopener noreferrer"), R(A, {
131
314
  display: "flex",
132
315
  alignItems: "center",
133
316
  gap: "8px",
134
317
  padding: "8px 10px",
135
- border: `1px solid ${E}`,
318
+ border: `1px solid ${$}`,
136
319
  borderRadius: "8px",
137
- backgroundColor: L,
320
+ backgroundColor: z,
138
321
  textDecoration: "none",
139
- color: H,
322
+ color: X,
140
323
  fontSize: "12px",
141
324
  overflow: "hidden"
142
325
  });
143
- const Q = c({
326
+ const le = x({
144
327
  flexShrink: "0",
145
328
  width: "18px",
146
329
  height: "18px",
147
330
  borderRadius: "3px",
148
- border: `1px solid ${K}`,
149
- color: K,
331
+ border: `1px solid ${Z}`,
332
+ color: Z,
150
333
  fontSize: "11px",
151
334
  fontWeight: "500",
152
335
  display: "flex",
@@ -154,68 +337,76 @@ function Ie(i, o) {
154
337
  justifyContent: "center",
155
338
  backgroundColor: "#f0f4ff"
156
339
  });
157
- Q.textContent = String(a + 1);
158
- const P = c({
340
+ le.textContent = String(E + 1);
341
+ const Y = x({
159
342
  overflow: "hidden",
160
343
  textOverflow: "ellipsis",
161
344
  whiteSpace: "nowrap"
162
345
  });
163
- P.textContent = p.title, P.title = p.title, y.append(Q, P), r.appendChild(y);
164
- }), d.appendChild(r);
346
+ Y.textContent = C.title, Y.title = C.title, A.append(le, Y), m.appendChild(A);
347
+ }), y.appendChild(m);
165
348
  }
166
- const b = c({
167
- maxWidth: t ? "75%" : "100%",
168
- padding: t ? "10px 14px" : "2px 8px",
169
- borderRadius: t ? ee : "0",
170
- backgroundColor: t ? ne : "transparent",
171
- color: t ? re : H,
349
+ const v = x({
350
+ maxWidth: c ? "75%" : "100%",
351
+ padding: c ? "10px 14px" : "2px 8px",
352
+ borderRadius: c ? pe : "0",
353
+ backgroundColor: c ? ve : "transparent",
354
+ color: c ? Ee : X,
172
355
  wordBreak: "break-word",
173
356
  fontSize: "14px",
174
- lineHeight: "1.6",
175
- whiteSpace: "pre-wrap"
357
+ lineHeight: "1.6"
176
358
  });
177
- if (b.className = "ago-message__content", e.content ? b.textContent = e.content : e.status === "IN_PROGRESS" && b.appendChild(fe()), d.appendChild(b), e.followUpReplies && e.followUpReplies.length > 0) {
178
- const r = c({
359
+ if (v.className = "ago-message__content", t.content ? v.appendChild(be(t.content)) : t.status === "IN_PROGRESS" && v.appendChild(ie()), y.appendChild(v), s && I && t.role === "assistant" && t.status === "DONE" && !(t.followUpReplies && t.followUpReplies.length > 0)) {
360
+ const m = x({
361
+ marginTop: "10px",
362
+ paddingLeft: "8px"
363
+ });
364
+ m.className = "ago-message__followups-loading", m.appendChild(ie()), y.appendChild(m);
365
+ }
366
+ if (s && t.followUpReplies && t.followUpReplies.length > 0) {
367
+ const m = x({
179
368
  display: "flex",
180
369
  flexWrap: "wrap",
181
370
  gap: "8px",
182
371
  marginTop: "10px"
183
372
  });
184
- r.className = "ago-message__followups";
185
- for (const p of e.followUpReplies) {
186
- const a = document.createElement("button");
187
- a.type = "button", a.className = "ago-message__followup-btn", a.textContent = p, a.disabled = !X, m(a, {
373
+ m.className = "ago-message__followups";
374
+ for (const C of t.followUpReplies) {
375
+ const E = document.createElement("button");
376
+ E.type = "button", E.className = "ago-message__followup-btn", E.textContent = C, E.disabled = !ee, R(E, {
188
377
  padding: "6px 14px",
189
378
  fontSize: "13px",
190
- borderRadius: ee,
191
- border: `1px solid ${E}`,
192
- backgroundColor: L,
193
- color: H,
194
- cursor: X ? "pointer" : "default",
379
+ borderRadius: pe,
380
+ border: `1px solid ${$}`,
381
+ backgroundColor: z,
382
+ color: X,
383
+ cursor: ee ? "pointer" : "default",
195
384
  transition: "border-color 0.15s"
196
- }), j && a.addEventListener("click", () => j(p)), a.addEventListener("mouseenter", () => {
197
- a.style.borderColor = K;
198
- }), a.addEventListener("mouseleave", () => {
199
- a.style.borderColor = E;
200
- }), r.appendChild(a);
385
+ }), te && E.addEventListener("click", () => te(C)), E.addEventListener("mouseenter", () => {
386
+ E.style.borderColor = Z;
387
+ }), E.addEventListener("mouseleave", () => {
388
+ E.style.borderColor = $;
389
+ }), m.appendChild(E);
201
390
  }
202
- d.appendChild(r);
391
+ y.appendChild(m);
203
392
  }
204
- return d;
393
+ return y;
205
394
  }
206
- function S() {
207
- if (R.replaceChildren(), n.length === 0) {
208
- const e = c({
395
+ function k() {
396
+ if (T.replaceChildren(), l.length === 0) {
397
+ const s = x({
209
398
  textAlign: "center",
210
- color: ve,
399
+ color: ze,
211
400
  padding: "24px 16px",
212
401
  fontSize: "14px",
213
402
  lineHeight: "1.5"
214
403
  });
215
- e.textContent = w, R.appendChild(e);
216
- } else for (const e of n) R.appendChild(le(e));
217
- if (N) {
218
- const e = c({
404
+ s.textContent = e, T.appendChild(s);
405
+ } else l.forEach((s, c) => {
406
+ T.appendChild(Se(s, c === l.length - 1));
407
+ });
408
+ if (U) {
409
+ const s = x({
219
410
  padding: "10px 14px",
220
411
  backgroundColor: "#fef2f2",
221
412
  color: "#dc2626",
@@ -224,217 +415,223 @@ function Ie(i, o) {
224
415
  fontSize: "13px",
225
416
  border: "1px solid #fecaca"
226
417
  });
227
- e.textContent = N, R.appendChild(e);
418
+ s.textContent = U, T.appendChild(s);
228
419
  }
229
- R.scrollTop = R.scrollHeight, se(_);
420
+ T.scrollTop = T.scrollHeight;
421
+ const t = l[l.length - 1];
422
+ we(I && t?.role === "assistant" && t.status === "IN_PROGRESS");
230
423
  }
231
- function de() {
232
- for (let e = n.length - 1; e >= 0; e--) {
233
- const t = n[e];
234
- if (t.role === "assistant" && t.status === "IN_PROGRESS") return t;
424
+ function _e() {
425
+ for (let t = l.length - 1; t >= 0; t--) {
426
+ const s = l[t];
427
+ if (s.role === "assistant" && s.status === "IN_PROGRESS") return s;
235
428
  }
236
429
  }
237
- const V = (e) => {
238
- h || (h = e.conversationId);
239
- }, q = (e) => {
240
- const t = de();
241
- t && (t.content += e.content, S());
242
- }, Y = (e) => {
243
- const t = n.findIndex((d) => d.id === e.id || d.role === "assistant" && d.status === "IN_PROGRESS");
244
- t >= 0 ? n[t] = e : n.push(e), _ = !1, S(), e.status === "DONE" && $?.({
245
- id: e.id,
246
- content: e.content
430
+ const ne = (t) => {
431
+ S || (S = t.conversationId);
432
+ }, oe = (t) => {
433
+ const s = _e();
434
+ s && (s.content += t.content, k());
435
+ }, re = (t) => {
436
+ const s = l.findIndex((c) => c.id === t.id || c.role === "assistant" && c.status === "IN_PROGRESS");
437
+ s >= 0 ? l[s] = t : l.push(t), k();
438
+ }, ae = (t) => {
439
+ const s = l.findIndex((c) => c.id === t.id || c.role === "assistant" && c.status === "IN_PROGRESS");
440
+ s >= 0 ? l[s] = t : l.push(t), I = !1, k(), t.status === "DONE" && F?.({
441
+ id: t.id,
442
+ content: t.content
247
443
  });
248
- }, J = (e) => {
249
- N = e.error, _ = !1, n = n.filter((t) => !t.id.startsWith("temp-")), S();
444
+ }, se = (t) => {
445
+ U = t.error, I = !1, l = l.filter((s) => !s.id.startsWith("temp-")), k();
250
446
  };
251
- l.on("message:start", V), l.on("message:chunk", q), l.on("message:complete", Y), l.on("message:error", J);
252
- const ce = (B ?? []).map((e) => ue(e).install(l));
253
- async function F(e, t) {
254
- const d = e.trim();
255
- if (!d && !t?.length || _) return;
256
- C?.(d), _ = !0, N = null;
257
- const b = Date.now();
258
- n.push({
259
- id: `temp-user-${b}`,
260
- conversationId: h || "",
261
- content: d,
447
+ h.on("message:start", ne), h.on("message:chunk", oe), h.on("message:answer-complete", re), h.on("message:complete", ae), h.on("message:error", se);
448
+ const ke = (N ?? []).map((t) => Te(t).install(h));
449
+ async function V(t, s) {
450
+ const c = t.trim();
451
+ if (!c && !s?.length || I) return;
452
+ _?.(c), I = !0, U = null;
453
+ const y = Date.now();
454
+ l.push({
455
+ id: `temp-user-${y}`,
456
+ conversationId: S || "",
457
+ content: c,
262
458
  role: "user",
263
459
  status: "DONE",
264
460
  createdAt: /* @__PURE__ */ new Date()
265
- }), n.push({
266
- id: `temp-assistant-${b}`,
267
- conversationId: h || "",
461
+ }), l.push({
462
+ id: `temp-assistant-${y}`,
463
+ conversationId: S || "",
268
464
  content: "",
269
465
  role: "assistant",
270
466
  status: "IN_PROGRESS",
271
467
  createdAt: /* @__PURE__ */ new Date()
272
- }), S();
468
+ }), k();
273
469
  try {
274
- const r = await l.sendMessage(d, {
275
- conversationId: h,
276
- files: t
470
+ const v = await h.sendMessage(c, {
471
+ conversationId: S,
472
+ files: s
277
473
  });
278
- r.conversationId && (h || (h = r.conversationId), I?.setActiveThread(r.conversationId, r.createdAt));
279
- const p = n.find((a) => a.id === `temp-user-${b}`);
280
- if (p && (p.id = `user-${b}`, p.conversationId = r.conversationId), !n.some((a) => a.id === r.id)) {
281
- const a = n.findIndex((y) => y.id === `temp-assistant-${b}`);
282
- a >= 0 ? n[a] = r : n.push(r);
474
+ v.conversationId && (S || (S = v.conversationId), D?.setActiveThread(v.conversationId, v.createdAt));
475
+ const m = l.find((C) => C.id === `temp-user-${y}`);
476
+ if (m && (m.id = `user-${y}`, m.conversationId = v.conversationId), !l.some((C) => C.id === v.id)) {
477
+ const C = l.findIndex((E) => E.id === `temp-assistant-${y}`);
478
+ C >= 0 ? l[C] = v : l.push(v);
283
479
  }
284
- n = n.filter((a) => !a.id.startsWith("temp-")), _ = !1, S(), D && G();
285
- } catch (r) {
286
- N = r instanceof Error ? r.message : "Failed to send message", _ = !1, n = n.filter((p) => !p.id.startsWith("temp-")), S();
480
+ l = l.filter((C) => !C.id.startsWith("temp-")), I = !1, k(), b && Q();
481
+ } catch (v) {
482
+ U = v instanceof Error ? v.message : "Failed to send message", I = !1, l = l.filter((m) => !m.id.startsWith("temp-")), k();
287
483
  }
288
484
  }
289
- async function G() {
485
+ async function Q() {
290
486
  try {
291
- const e = await l.getConversations();
292
- M.splice(0, M.length, ...e);
487
+ const t = await h.getConversations();
488
+ P.splice(0, P.length, ...t);
293
489
  } catch {
294
490
  }
295
- return M;
491
+ return P;
296
492
  }
297
- async function pe(e) {
493
+ async function $e(t) {
298
494
  try {
299
- const t = (await l.getConversation(e))?.messages ?? [];
300
- t.length > 0 && n.length === 0 && (n = t, S());
495
+ const s = (await h.getConversation(t))?.messages ?? [];
496
+ s.length > 0 && l.length === 0 && (l = s, k());
301
497
  } catch {
302
- n.length === 0 && (I?.clear(), h = void 0);
498
+ l.length === 0 && (D?.clear(), S = void 0);
303
499
  }
304
500
  }
305
- return S(), ie(), h && pe(h), D && G(), {
306
- client: l,
307
- element: A,
308
- sendMessage: F,
309
- session: I,
310
- threads: M,
311
- refreshThreads: G,
501
+ return k(), Re(), S && $e(S), b && Q(), {
502
+ client: h,
503
+ element: M,
504
+ sendMessage: V,
505
+ session: D,
506
+ threads: P,
507
+ refreshThreads: Q,
312
508
  destroy() {
313
- l.off("message:start", V), l.off("message:chunk", q), l.off("message:complete", Y), l.off("message:error", J), ce.forEach((e) => e()), A.remove(), o.client || l.destroy();
509
+ h.off("message:start", ne), h.off("message:chunk", oe), h.off("message:answer-complete", re), h.off("message:complete", ae), h.off("message:error", se), ke.forEach((t) => t()), M.remove(), n.client || h.destroy();
314
510
  }
315
511
  };
316
- function fe() {
317
- const e = c({
512
+ function ie() {
513
+ const t = x({
318
514
  display: "flex",
319
515
  gap: "4px",
320
516
  padding: "4px 0"
321
517
  });
322
- for (let t = 0; t < 3; t++) {
323
- const d = c({
518
+ for (let s = 0; s < 3; s++) {
519
+ const c = x({
324
520
  width: "6px",
325
521
  height: "6px",
326
522
  borderRadius: "50%",
327
523
  backgroundColor: "#b5bfc8",
328
524
  animation: "ago-pulse 1.2s ease-in-out infinite",
329
- animationDelay: `${t * 0.2}s`
525
+ animationDelay: `${s * 0.2}s`
330
526
  });
331
- e.appendChild(d);
527
+ t.appendChild(c);
332
528
  }
333
- return e;
529
+ return t;
334
530
  }
335
531
  }
336
- function Re(i) {
337
- let o = [];
338
- const g = document.createElement("form");
339
- m(g, {
532
+ function je(o) {
533
+ let n = [];
534
+ const r = document.createElement("form");
535
+ R(r, {
340
536
  display: "flex",
341
537
  flexDirection: "column",
342
538
  gap: "8px",
343
539
  padding: "12px",
344
- borderTop: `1px solid ${E}`,
345
- backgroundColor: L
346
- }), g.className = "ago-chat-input";
347
- const w = c({
540
+ borderTop: `1px solid ${$}`,
541
+ backgroundColor: z
542
+ }), r.className = "ago-chat-input";
543
+ const e = x({
348
544
  display: "flex",
349
545
  flexWrap: "wrap",
350
546
  gap: "6px"
351
- }), O = c({
547
+ }), g = x({
352
548
  display: "flex",
353
549
  gap: "8px",
354
550
  alignItems: "flex-end"
355
- }), u = document.createElement("textarea");
356
- u.placeholder = i.placeholder, u.rows = 1, m(u, {
551
+ }), a = document.createElement("textarea");
552
+ a.placeholder = o.placeholder, a.rows = 1, R(a, {
357
553
  flex: "1",
358
554
  resize: "none",
359
555
  padding: "10px 12px",
360
- border: `1px solid ${E}`,
556
+ border: `1px solid ${$}`,
361
557
  borderRadius: "12px",
362
558
  fontSize: "14px",
363
- fontFamily: oe,
559
+ fontFamily: Ce,
364
560
  lineHeight: "1.4",
365
561
  maxHeight: "120px",
366
562
  outline: "none"
367
563
  });
368
- const x = document.createElement("button");
369
- x.type = "submit", x.textContent = "Send", m(x, {
564
+ const p = document.createElement("button");
565
+ p.type = "submit", p.textContent = "Send", R(p, {
370
566
  padding: "10px 16px",
371
567
  border: "none",
372
568
  borderRadius: "12px",
373
- backgroundColor: ne,
374
- color: re,
569
+ backgroundColor: ve,
570
+ color: Ee,
375
571
  fontSize: "14px",
376
572
  fontWeight: "500",
377
573
  cursor: "pointer"
378
574
  });
379
- let f = null, v = null;
380
- i.allowFiles && (f = document.createElement("input"), f.type = "file", f.multiple = !0, m(f, { display: "none" }), v = document.createElement("button"), v.type = "button", v.textContent = "📎", m(v, {
575
+ let i = null, f = null;
576
+ o.allowFiles && (i = document.createElement("input"), i.type = "file", i.multiple = !0, R(i, { display: "none" }), f = document.createElement("button"), f.type = "button", f.textContent = "📎", R(f, {
381
577
  padding: "10px 12px",
382
- border: `1px solid ${E}`,
578
+ border: `1px solid ${$}`,
383
579
  borderRadius: "12px",
384
- backgroundColor: L,
580
+ backgroundColor: z,
385
581
  cursor: "pointer",
386
582
  fontSize: "14px"
387
- }), v.addEventListener("click", () => f?.click()), f.addEventListener("change", () => {
388
- o = [...o, ...Array.from(f?.files ?? [])], T();
583
+ }), f.addEventListener("click", () => i?.click()), i.addEventListener("change", () => {
584
+ n = [...n, ...Array.from(i?.files ?? [])], d();
389
585
  }));
390
- function T() {
391
- w.replaceChildren(), o.forEach((s, C) => {
392
- const $ = c({
586
+ function d() {
587
+ e.replaceChildren(), n.forEach((u, _) => {
588
+ const F = x({
393
589
  display: "flex",
394
590
  alignItems: "center",
395
591
  gap: "4px",
396
592
  padding: "4px 8px",
397
593
  backgroundColor: "#f0f4ff",
398
- border: `1px solid ${E}`,
594
+ border: `1px solid ${$}`,
399
595
  borderRadius: "8px",
400
596
  fontSize: "12px"
401
- }), l = document.createElement("span");
402
- l.textContent = s.name;
403
- const k = document.createElement("button");
404
- k.type = "button", k.textContent = "×", m(k, {
597
+ }), h = document.createElement("span");
598
+ h.textContent = u.name;
599
+ const O = document.createElement("button");
600
+ O.type = "button", O.textContent = "×", R(O, {
405
601
  border: "none",
406
602
  background: "transparent",
407
603
  cursor: "pointer",
408
604
  fontSize: "14px",
409
605
  lineHeight: "1"
410
- }), k.addEventListener("click", () => {
411
- o = o.filter((U, I) => I !== C), T();
412
- }), $.append(l, k), w.appendChild($);
606
+ }), O.addEventListener("click", () => {
607
+ n = n.filter((W, D) => D !== _), d();
608
+ }), F.append(h, O), e.appendChild(F);
413
609
  });
414
610
  }
415
- const D = () => {
416
- const s = u.value, C = o;
417
- return u.value = "", o = [], T(), {
418
- content: s,
419
- files: C
611
+ const b = () => {
612
+ const u = a.value, _ = n;
613
+ return a.value = "", n = [], d(), {
614
+ content: u,
615
+ files: _
420
616
  };
421
- }, B = () => {
422
- const { content: s, files: C } = D();
423
- (s.trim() || C.length > 0) && i.onSend(s.trim(), C.length > 0 ? C : void 0);
617
+ }, N = () => {
618
+ const { content: u, files: _ } = b();
619
+ (u.trim() || _.length > 0) && o.onSend(u.trim(), _.length > 0 ? _ : void 0);
424
620
  };
425
- return g.addEventListener("submit", (s) => {
426
- s.preventDefault(), B();
427
- }), u.addEventListener("keydown", (s) => {
428
- s.key === "Enter" && !s.shiftKey && (s.preventDefault(), B());
429
- }), v && O.appendChild(v), O.append(u, x), f && g.appendChild(f), g.append(w, O), {
430
- inputRow: g,
431
- getValueAndClear: D,
432
- setDisabled: (s) => {
433
- u.disabled = s, x.disabled = s, x.style.opacity = s ? "0.6" : "1", x.style.cursor = s ? "not-allowed" : "pointer";
621
+ return r.addEventListener("submit", (u) => {
622
+ u.preventDefault(), N();
623
+ }), a.addEventListener("keydown", (u) => {
624
+ u.key === "Enter" && !u.shiftKey && (u.preventDefault(), N());
625
+ }), f && g.appendChild(f), g.append(a, p), i && r.appendChild(i), r.append(e, g), {
626
+ inputRow: r,
627
+ getValueAndClear: b,
628
+ setDisabled: (u) => {
629
+ a.disabled = u, p.disabled = u, p.style.opacity = u ? "0.6" : "1", p.style.cursor = u ? "not-allowed" : "pointer";
434
630
  },
435
- focus: () => u.focus()
631
+ focus: () => a.focus()
436
632
  };
437
633
  }
438
634
  export {
439
- Ie as mountChatWidget
635
+ Ve as mountChatWidget,
636
+ be as renderMarkdown
440
637
  };