@useago/sdk 0.4.4 → 0.4.6

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