@spilki/widget 0.1.0 → 0.1.1

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/README.md CHANGED
@@ -96,4 +96,4 @@ Open [http://localhost:5174](http://localhost:5174) and use **Issue token** to o
96
96
 
97
97
  ## Publishing
98
98
 
99
- The GitHub Actions workflow publishes on tags that match `v*`. Ensure `NPM_TOKEN` is configured in the repository secrets before tagging `v0.1.0` or later.
99
+ The GitHub Actions workflow publishes on tags that match `v*`. Ensure `NPM_TOKEN` is configured in the repository secrets before tagging `v0.1.1` or later.
@@ -1,4 +1,4 @@
1
- const I = `
1
+ const M = `
2
2
  <style>
3
3
  :host {
4
4
  all: initial;
@@ -44,11 +44,11 @@ const I = `
44
44
  </span>
45
45
  </button>
46
46
  `;
47
- function T(r) {
47
+ function A(r) {
48
48
  const e = document.createElement("div");
49
49
  e.setAttribute("part", "bubble-root"), e.style.setProperty("--spilki-accent", r.color), e.style.position = "fixed", e.style.bottom = "24px", e.style[r.position === "bottom-right" ? "right" : "left"] = "24px";
50
50
  const t = e.attachShadow({ mode: "open" });
51
- t.innerHTML = I;
51
+ t.innerHTML = M;
52
52
  const s = t.querySelector("button");
53
53
  return s.addEventListener("click", () => r.onClick()), {
54
54
  element: e,
@@ -63,11 +63,11 @@ function T(r) {
63
63
  }
64
64
  };
65
65
  }
66
- const O = ':host{--spilki-bg-light: #ffffff;--spilki-bg-dark: #0f172a;--spilki-text-light: #0f172a;--spilki-text-dark: #f8fafc;--spilki-border-light: rgba(15, 23, 42, .1);--spilki-border-dark: rgba(148, 163, 184, .25);--spilki-shadow: 0 10px 40px rgba(15, 23, 42, .2);--spilki-radius: 16px;--spilki-font: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;color:inherit;font-family:var(--spilki-font)}.wrapper{width:100%;height:100%;display:flex;flex-direction:column;background:var(--spilki-surface);color:var(--spilki-text);border-radius:var(--spilki-radius);box-shadow:var(--spilki-shadow);border:1px solid var(--spilki-border);overflow:hidden}header{display:flex;align-items:center;justify-content:space-between;padding:.75rem 1rem;background:var(--spilki-surface);border-bottom:1px solid var(--spilki-border)}header h1{font-size:1rem;margin:0;display:flex;align-items:center;gap:.5rem}header button.close{border:none;background:transparent;color:inherit;font-size:1.25rem;cursor:pointer}header .status-dot{width:10px;height:10px;border-radius:999px;background:var(--spilki-accent)}.messages{flex:1;overflow-y:auto;padding:1rem;display:flex;flex-direction:column;gap:.5rem}.message{display:flex;flex-direction:column;gap:.25rem;max-width:85%;line-height:1.4;word-wrap:break-word;overflow-wrap:anywhere;white-space:pre-wrap}.message.user{align-self:flex-end;text-align:right}.message .bubble{padding:.6rem .8rem;border-radius:1rem;background:#6366f126}.message.user .bubble{background:var(--spilki-accent);color:#fff}.message.bot .bubble{background:#94a3b826}.input-area{display:flex;align-items:flex-end;gap:.5rem;padding:.75rem 1rem;border-top:1px solid var(--spilki-border)}.input-area textarea{flex:1;resize:none;min-height:2.5rem;max-height:6rem;border-radius:.75rem;border:1px solid var(--spilki-border);padding:.6rem .75rem;font-family:inherit;font-size:.95rem;background:var(--spilki-surface);color:var(--spilki-text)}.input-area button{border:none;border-radius:999px;padding:.6rem 1.1rem;background:var(--spilki-accent);color:#fff;font-weight:600;cursor:pointer}.typing{font-size:.75rem;color:#94a3b8e6;padding:0 1rem .75rem}.offline{font-size:.8rem;padding:0 1rem;color:#f97316}:host([data-theme="dark"]){--spilki-surface: var(--spilki-bg-dark);--spilki-text: var(--spilki-text-dark);--spilki-border: var(--spilki-border-dark)}:host([data-theme="light"]){--spilki-surface: var(--spilki-bg-light);--spilki-text: var(--spilki-text-light);--spilki-border: var(--spilki-border-light)}:host([data-theme="dark"]) .message .bubble{background:#94a3b81f}:host([data-theme="dark"]) .message.bot .bubble{background:#6366f126}:host([data-theme="dark"]) .input-area textarea{background:#0f172ad9}.messages::-webkit-scrollbar,.input-area textarea::-webkit-scrollbar{width:6px}.messages::-webkit-scrollbar-track,.input-area textarea::-webkit-scrollbar-track{background:transparent}.messages::-webkit-scrollbar-thumb,.input-area textarea::-webkit-scrollbar-thumb{background:#94a3b84d;border-radius:999px}.messages::-webkit-scrollbar-thumb:hover,.input-area textarea::-webkit-scrollbar-thumb:hover{background:#94a3b880}:host([data-theme="dark"]) .messages::-webkit-scrollbar-thumb,:host([data-theme="dark"]) .input-area textarea::-webkit-scrollbar-thumb{background:#fff3}:host([data-theme="dark"]) .messages::-webkit-scrollbar-thumb:hover,:host([data-theme="dark"]) .input-area textarea::-webkit-scrollbar-thumb:hover{background:#ffffff59}.messages{scroll-behavior:smooth}';
67
- class M {
66
+ const $ = ':host{--spilki-bg-light: #ffffff;--spilki-bg-dark: #0f172a;--spilki-text-light: #0f172a;--spilki-text-dark: #f8fafc;--spilki-border-light: rgba(15, 23, 42, .1);--spilki-border-dark: rgba(148, 163, 184, .25);--spilki-shadow: 0 10px 40px rgba(15, 23, 42, .2);--spilki-radius: 16px;--spilki-font: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;color:inherit;font-family:var(--spilki-font)}.wrapper{width:100%;height:100%;display:flex;flex-direction:column;background:var(--spilki-surface);color:var(--spilki-text);border-radius:var(--spilki-radius);box-shadow:var(--spilki-shadow);border:1px solid var(--spilki-border);overflow:hidden}header{display:flex;align-items:center;justify-content:space-between;padding:.75rem 1rem;background:var(--spilki-surface);border-bottom:1px solid var(--spilki-border)}header h1{font-size:1rem;margin:0;display:flex;align-items:center;gap:.5rem}header button.close{border:none;background:transparent;color:inherit;font-size:1.25rem;cursor:pointer}header .status-dot{width:10px;height:10px;border-radius:999px;background:var(--spilki-accent)}.messages{flex:1;overflow-y:auto;padding:1rem;display:flex;flex-direction:column;gap:.5rem}.message{display:flex;flex-direction:column;gap:.25rem;max-width:85%;line-height:1.4;word-wrap:break-word;overflow-wrap:anywhere;white-space:pre-wrap}.message.user{align-self:flex-end;text-align:right}.message .bubble{padding:.6rem .8rem;border-radius:1rem;background:#6366f126}.message.user .bubble{background:var(--spilki-accent);color:#fff}.message.bot .bubble{background:#94a3b826}.input-area{display:flex;align-items:flex-end;gap:.5rem;padding:.75rem 1rem;border-top:1px solid var(--spilki-border)}.input-area textarea{flex:1;resize:none;min-height:2.5rem;max-height:6rem;border-radius:.75rem;border:1px solid var(--spilki-border);padding:.6rem .75rem;font-family:inherit;font-size:.95rem;background:var(--spilki-surface);color:var(--spilki-text)}.input-area button{border:none;border-radius:999px;padding:.6rem 1.1rem;background:var(--spilki-accent);color:#fff;font-weight:600;cursor:pointer}.typing{font-size:.75rem;color:#94a3b8e6;padding:0 1rem .75rem}.offline{font-size:.8rem;padding:0 1rem;color:#f97316}:host([data-theme="dark"]){--spilki-surface: var(--spilki-bg-dark);--spilki-text: var(--spilki-text-dark);--spilki-border: var(--spilki-border-dark)}:host([data-theme="light"]){--spilki-surface: var(--spilki-bg-light);--spilki-text: var(--spilki-text-light);--spilki-border: var(--spilki-border-light)}:host([data-theme="dark"]) .message .bubble{background:#94a3b81f}:host([data-theme="dark"]) .message.bot .bubble{background:#6366f126}:host([data-theme="dark"]) .input-area textarea{background:#0f172ad9}.messages::-webkit-scrollbar,.input-area textarea::-webkit-scrollbar{width:6px}.messages::-webkit-scrollbar-track,.input-area textarea::-webkit-scrollbar-track{background:transparent}.messages::-webkit-scrollbar-thumb,.input-area textarea::-webkit-scrollbar-thumb{background:#94a3b84d;border-radius:999px}.messages::-webkit-scrollbar-thumb:hover,.input-area textarea::-webkit-scrollbar-thumb:hover{background:#94a3b880}:host([data-theme="dark"]) .messages::-webkit-scrollbar-thumb,:host([data-theme="dark"]) .input-area textarea::-webkit-scrollbar-thumb{background:#fff3}:host([data-theme="dark"]) .messages::-webkit-scrollbar-thumb:hover,:host([data-theme="dark"]) .input-area textarea::-webkit-scrollbar-thumb:hover{background:#ffffff59}.messages{scroll-behavior:smooth}';
67
+ class C {
68
68
  constructor(e) {
69
69
  this.options = e, this.focusable = [], this.open = !1, this.host = document.createElement("div"), this.host.setAttribute("part", "panel-root"), this.host.style.position = "fixed", this.host.style.bottom = "96px", this.host.style[e.position === "bottom-right" ? "right" : "left"] = "24px", this.host.style.width = "360px", this.host.style.maxWidth = "calc(100vw - 32px)", this.host.style.height = "520px", this.host.style.display = "none", this.host.style.zIndex = "2147483001", this.shadow = this.host.attachShadow({ mode: "open" }), this.shadow.innerHTML = `
70
- <style>${O}</style>
70
+ <style>${$}</style>
71
71
  <div class="wrapper" role="dialog" aria-modal="true" aria-label="${e.i18n.title}">
72
72
  <header>
73
73
  <h1><span class="status-dot" aria-hidden="true"></span>${e.i18n.title}</h1>
@@ -149,15 +149,15 @@ class M {
149
149
  e.shiftKey && i === t ? (e.preventDefault(), s.focus()) : !e.shiftKey && i === s && (e.preventDefault(), t.focus());
150
150
  }
151
151
  }
152
- const C = "https://api.spilki.ai", g = {
152
+ const W = "https://api.spilki.ai", g = {
153
153
  welcome: "Hi! I'm your assistant.",
154
154
  placeholder: "Type a message…",
155
155
  sendLabel: "Send",
156
156
  typing: "Assistant is typing…",
157
157
  offline: "Unable to connect. Please try again later.",
158
158
  title: "Spilki Assistant"
159
- }, c = {
160
- apiBase: C,
159
+ }, p = {
160
+ apiBase: W,
161
161
  position: "bottom-right",
162
162
  theme: "auto",
163
163
  color: "#6366f1",
@@ -165,43 +165,43 @@ const C = "https://api.spilki.ai", g = {
165
165
  persist: !0,
166
166
  i18n: g
167
167
  };
168
- function A(r) {
169
- var t, s, i, o, a, l, p;
168
+ function B(r) {
169
+ var t, s, i, o, l, a, h;
170
170
  const e = { ...g, ...(t = r.i18n) != null ? t : {} };
171
171
  return {
172
- ...c,
172
+ ...p,
173
173
  ...r,
174
- apiBase: (s = r.apiBase) != null ? s : c.apiBase,
174
+ apiBase: (s = r.apiBase) != null ? s : p.apiBase,
175
175
  i18n: e,
176
176
  welcome: (i = r.welcome) != null ? i : e.welcome,
177
- position: (o = r.position) != null ? o : c.position,
178
- theme: (a = r.theme) != null ? a : c.theme,
179
- color: (l = r.color) != null ? l : c.color,
180
- persist: (p = r.persist) != null ? p : c.persist
177
+ position: (o = r.position) != null ? o : p.position,
178
+ theme: (l = r.theme) != null ? l : p.theme,
179
+ color: (a = r.color) != null ? a : p.color,
180
+ persist: (h = r.persist) != null ? h : p.persist
181
181
  };
182
182
  }
183
- function $(r = "msg") {
183
+ function L(r = "msg") {
184
184
  return typeof crypto != "undefined" && crypto.randomUUID ? crypto.randomUUID() : `${r}-${Math.random().toString(16).slice(2)}`;
185
185
  }
186
- function W() {
186
+ function K() {
187
187
  var r, e;
188
188
  return (e = (r = window.matchMedia) == null ? void 0 : r.call(window, "(prefers-color-scheme: dark)").matches) != null ? e : !1;
189
189
  }
190
- function y(r) {
191
- return r === "light" || r === "dark" ? r : W() ? "dark" : "light";
190
+ function x(r) {
191
+ return r === "light" || r === "dark" ? r : K() ? "dark" : "light";
192
192
  }
193
193
  function u(r, e = 30) {
194
194
  return r.slice(-e);
195
195
  }
196
- function L(r) {
196
+ function P(r) {
197
197
  if (!r || r.length === 0) return;
198
198
  const e = window.location.origin;
199
199
  r.includes(e) || console.warn(
200
200
  `SpilkiWidget: current origin ${e} not in allowedOriginsHint: ${r.join(", ")}`
201
201
  );
202
202
  }
203
- const d = 1500, x = 8e3;
204
- class B {
203
+ const d = 1500, v = 8e3;
204
+ class U {
205
205
  constructor(e, t) {
206
206
  this.sessionId = null, this.currentKind = null, this.stopped = !1, this.backoff = d, this.options = e, this.handlers = t;
207
207
  }
@@ -213,9 +213,9 @@ class B {
213
213
  return (t = (e = this.sessionId) != null ? e : this.options.sessionId) != null ? t : null;
214
214
  }
215
215
  async connect() {
216
- var a, l;
216
+ var l, a;
217
217
  this.stopped = !1;
218
- const e = `${this.options.apiBase.replace(/\/$/, "")}/widget/connect`, t = (l = (a = this.sessionId) != null ? a : this.options.sessionId) != null ? l : void 0, s = {
218
+ const e = `${this.options.apiBase.replace(/\/$/, "")}/widget/connect`, t = (a = (l = this.sessionId) != null ? l : this.options.sessionId) != null ? a : void 0, s = {
219
219
  organisationId: this.options.org,
220
220
  assistantId: this.options.assistant,
221
221
  sessionId: t,
@@ -226,7 +226,7 @@ class B {
226
226
  method: "POST",
227
227
  headers: {
228
228
  "Content-Type": "application/json",
229
- ...this.options.token ? { Authorization: `Bearer ${this.options.token}` } : {}
229
+ ...this.options.accessToken ? { Authorization: `Bearer ${this.options.accessToken}` } : {}
230
230
  },
231
231
  body: JSON.stringify(s)
232
232
  });
@@ -236,9 +236,9 @@ class B {
236
236
  return this.sessionId = o.sessionId, this.options.sessionId = o.sessionId, this.backoff = d, await this.startTransport(o), o;
237
237
  }
238
238
  async send(e) {
239
- var o, a;
239
+ var o, l;
240
240
  const t = {
241
- sessionId: (a = (o = this.sessionId) != null ? o : this.options.sessionId) != null ? a : "",
241
+ sessionId: (l = (o = this.sessionId) != null ? o : this.options.sessionId) != null ? l : "",
242
242
  text: e
243
243
  };
244
244
  if (!t.sessionId)
@@ -251,7 +251,7 @@ class B {
251
251
  method: "POST",
252
252
  headers: {
253
253
  "Content-Type": "application/json",
254
- ...this.options.token ? { Authorization: `Bearer ${this.options.token}` } : {}
254
+ ...this.options.accessToken ? { Authorization: `Bearer ${this.options.accessToken}` } : {}
255
255
  },
256
256
  body: JSON.stringify(t)
257
257
  });
@@ -309,7 +309,7 @@ class B {
309
309
  return;
310
310
  }
311
311
  this.currentKind = "sse", this.handlers.onOpen("sse"), this.backoff = d, t();
312
- }), i.addEventListener("message", (a) => this.handleIncoming(a.data)), i.addEventListener("error", () => {
312
+ }), i.addEventListener("message", (l) => this.handleIncoming(l.data)), i.addEventListener("error", () => {
313
313
  if (!o) {
314
314
  s(new Error("SSE failed"));
315
315
  return;
@@ -328,7 +328,7 @@ class B {
328
328
  const i = await s.json();
329
329
  u(i).forEach((o) => this.handlers.onMessage(o)), this.backoff = d;
330
330
  } catch (s) {
331
- this.handlers.onError(s), this.backoff = Math.min(this.backoff * 1.5, x);
331
+ this.handlers.onError(s), this.backoff = Math.min(this.backoff * 1.5, v);
332
332
  } finally {
333
333
  this.stopped || (this.pollTimer = window.setTimeout(t, this.backoff));
334
334
  }
@@ -336,7 +336,7 @@ class B {
336
336
  await t();
337
337
  }
338
338
  retryFallback(e) {
339
- this.stopped || (this.stop(), this.backoff = Math.min(this.backoff * 1.5, x), setTimeout(() => {
339
+ this.stopped || (this.stop(), this.backoff = Math.min(this.backoff * 1.5, v), setTimeout(() => {
340
340
  this.handlers.onError(new Error(`SpilkiWidget: retrying after ${e}`)), this.connect().catch((t) => this.handlers.onError(t));
341
341
  }, this.backoff));
342
342
  }
@@ -368,14 +368,14 @@ class B {
368
368
  }
369
369
  }
370
370
  const f = 30;
371
- class U {
371
+ class N {
372
372
  constructor(e, t, s) {
373
373
  this.org = e, this.assistant = t, this.listeners = /* @__PURE__ */ new Set(), this.state = {
374
374
  isOpen: !1,
375
375
  isTyping: !1,
376
376
  isConnected: !1,
377
377
  messages: []
378
- }, this.historyKey = `spilki-history:${e}:${t}`, this.sessionKey = `spilki-session:${e}:${t}`, this.persist = s.persist, this.state.messages = this.loadMessages();
378
+ }, this.historyKey = `spilki-history:${e}:${t}`, this.sessionKey = `spilki-session:${e}:${t}`, this.tokenKey = `spilki-token:${e}:${t}`, this.persist = s.persist, this.state.messages = this.loadMessages();
379
379
  }
380
380
  get snapshot() {
381
381
  return { ...this.state, messages: [...this.state.messages] };
@@ -398,7 +398,7 @@ class U {
398
398
  addMessage(e) {
399
399
  var s, i;
400
400
  const t = {
401
- id: (s = e.id) != null ? s : $("msg"),
401
+ id: (s = e.id) != null ? s : L("msg"),
402
402
  ts: (i = e.ts) != null ? i : Date.now(),
403
403
  author: e.author,
404
404
  text: e.text
@@ -415,8 +415,8 @@ class U {
415
415
  if (!this.persist) return null;
416
416
  try {
417
417
  return localStorage.getItem(this.sessionKey);
418
- } catch {
419
- return null;
418
+ } catch (e) {
419
+ return console.error("SpilkiWidget: unable to get item", e), null;
420
420
  }
421
421
  }
422
422
  persistSession(e) {
@@ -424,7 +424,7 @@ class U {
424
424
  try {
425
425
  localStorage.setItem(this.sessionKey, e);
426
426
  } catch (t) {
427
- console.warn("SpilkiWidget: Unable to persist session", t);
427
+ console.error("SpilkiWidget: unable to set item", t);
428
428
  }
429
429
  }
430
430
  clearSession() {
@@ -432,7 +432,31 @@ class U {
432
432
  try {
433
433
  localStorage.removeItem(this.sessionKey);
434
434
  } catch (e) {
435
- console.warn("SpilkiWidget: Unable to clear session", e);
435
+ console.error("SpilkiWidget: unable to remove item", e);
436
+ }
437
+ }
438
+ get accessToken() {
439
+ if (!this.persist) return null;
440
+ try {
441
+ return localStorage.getItem(this.tokenKey);
442
+ } catch (e) {
443
+ return console.error("SpilkiWidget: unable to get item", e), null;
444
+ }
445
+ }
446
+ persistAccessToken(e) {
447
+ if (this.persist)
448
+ try {
449
+ localStorage.setItem(this.tokenKey, e);
450
+ } catch (t) {
451
+ console.error("SpilkiWidget: unable to set item", t);
452
+ }
453
+ }
454
+ clearAccessToken() {
455
+ if (this.persist)
456
+ try {
457
+ localStorage.removeItem(this.tokenKey);
458
+ } catch (e) {
459
+ console.error("SpilkiWidget: unable to remove item", e);
436
460
  }
437
461
  }
438
462
  emit() {
@@ -443,7 +467,7 @@ class U {
443
467
  try {
444
468
  localStorage.setItem(this.historyKey, JSON.stringify(this.state.messages));
445
469
  } catch (e) {
446
- console.warn("SpilkiWidget: Unable to persist messages", e);
470
+ console.error("SpilkiWidget: unable to set item", e);
447
471
  }
448
472
  }
449
473
  loadMessages() {
@@ -453,12 +477,12 @@ class U {
453
477
  if (!e) return [];
454
478
  const t = JSON.parse(e);
455
479
  return Array.isArray(t) ? u(t, f) : [];
456
- } catch {
457
- return [];
480
+ } catch (e) {
481
+ return console.error("SpilkiWidget: unable to load messages", e), [];
458
482
  }
459
483
  }
460
484
  }
461
- function K(r) {
485
+ function D(r) {
462
486
  const e = r.replace(/-/g, "+").replace(/_/g, "/"), t = e.padEnd(e.length + (4 - e.length % 4) % 4, "=");
463
487
  if (typeof atob == "function")
464
488
  return decodeURIComponent(
@@ -469,24 +493,24 @@ function K(r) {
469
493
  return s.from(t, "base64").toString("utf8");
470
494
  throw new Error("SpilkiWidget: no base64 decoder available");
471
495
  }
472
- function P(r) {
496
+ function F(r) {
473
497
  if (!r) return null;
474
498
  const e = r.split(".");
475
499
  if (e.length < 2) return null;
476
500
  try {
477
- const t = K(e[1]);
501
+ const t = D(e[1]);
478
502
  return JSON.parse(t);
479
503
  } catch (t) {
480
504
  return console.error("SpilkiWidget: unable to parse JWT", t), null;
481
505
  }
482
506
  }
483
- function D(r) {
484
- const e = P(r);
507
+ function z(r) {
508
+ const e = F(r);
485
509
  if (!(e != null && e.exp)) return !1;
486
510
  const t = Math.floor(Date.now() / 1e3);
487
511
  return e.exp < t;
488
512
  }
489
- const N = {
513
+ const H = {
490
514
  onOpen() {
491
515
  },
492
516
  onClose() {
@@ -498,81 +522,107 @@ const N = {
498
522
  onTransportChange() {
499
523
  }
500
524
  };
501
- function v(r) {
502
- var m, k, w;
525
+ async function q(r, e) {
526
+ const t = `${r.replace(/\/$/, "")}/widget/install`, s = await fetch(t, {
527
+ method: "POST",
528
+ headers: { "Content-Type": "application/json", Origin: window.location.origin },
529
+ body: JSON.stringify({ token: e })
530
+ });
531
+ if (!s.ok) throw new Error(`SpilkiWidget: install failed (${s.status})`);
532
+ return (await s.json()).accessToken;
533
+ }
534
+ async function J(r, e) {
535
+ const t = `${r.replace(/\/$/, "")}/widget/refresh`, s = await fetch(t, {
536
+ method: "POST",
537
+ headers: { Authorization: `Bearer ${e}`, Origin: window.location.origin }
538
+ });
539
+ if (!s.ok) throw new Error(`SpilkiWidget: refresh failed (${s.status})`);
540
+ return (await s.json()).accessToken;
541
+ }
542
+ function E(r) {
543
+ var k, w, y, S;
503
544
  if (!r.org || !r.assistant)
504
545
  throw new Error("SpilkiWidget: org and assistant are required");
505
- const e = A(r);
506
- if (e.token && D(e.token))
507
- throw new Error("SpilkiWidget: JWT token has expired");
508
- L(e.allowedOriginsHint);
509
- const t = { ...N, ...(m = e.hooks) != null ? m : {} }, s = new U(e.org, e.assistant, { persist: e.persist }), i = T({
546
+ const e = B(r);
547
+ P(e.allowedOriginsHint);
548
+ const t = { ...H, ...(k = e.hooks) != null ? k : {} }, s = new N(e.org, e.assistant, { persist: e.persist });
549
+ let i = (w = s.accessToken) != null ? w : void 0;
550
+ const o = async () => {
551
+ if (!i) {
552
+ if (!r.installationToken) throw new Error("SpilkiWidget: missing installationToken");
553
+ i = await q(e.apiBase, r.installationToken), s.persistAccessToken(i);
554
+ return;
555
+ }
556
+ z(i) && (i = await J(e.apiBase, i), s.persistAccessToken(i));
557
+ }, l = A({
510
558
  color: e.color,
511
- position: (k = e.position) != null ? k : "bottom-right",
559
+ position: (y = e.position) != null ? y : "bottom-right",
512
560
  onClick: () => {
513
561
  s.snapshot.isOpen ? (s.close(), t.onClose()) : (s.open(), t.onOpen());
514
562
  }
515
- }), o = new M({
563
+ }), a = new C({
516
564
  color: e.color,
517
- theme: y(e.theme),
518
- position: (w = e.position) != null ? w : "bottom-right",
565
+ theme: x(e.theme),
566
+ position: (S = e.position) != null ? S : "bottom-right",
519
567
  i18n: e.i18n,
520
568
  onClose: () => {
521
569
  s.close(), t.onClose();
522
570
  },
523
571
  onSend: (n) => {
524
- const h = s.addMessage({ author: "user", text: n });
525
- o.appendMessage(h), a.send(n).catch((S) => {
526
- t.onError(S), s.setConnected(!1), o.setOffline(!0);
572
+ const c = s.addMessage({ author: "user", text: n });
573
+ a.appendMessage(c), o().then(() => h.send(n)).catch((O) => {
574
+ t.onError(O), s.setConnected(!1), a.setOffline(!0);
527
575
  });
528
576
  }
529
- }), a = new B(
577
+ }), h = new U(
530
578
  {
531
579
  apiBase: e.apiBase,
532
- token: e.token,
580
+ accessToken: i,
533
581
  org: e.org,
534
582
  assistant: e.assistant,
535
583
  sessionId: s.sessionId
536
584
  },
537
585
  {
538
586
  onOpen(n) {
539
- b.transport = n, t.onTransportChange(n), s.setConnected(!0), o.setOffline(!1);
587
+ m.transport = n, t.onTransportChange(n), s.setConnected(!0), a.setOffline(!1);
540
588
  },
541
589
  onMessage(n) {
542
- const h = s.addMessage(n);
543
- o.appendMessage(h), t.onMessage(h);
590
+ const c = s.addMessage(n);
591
+ a.appendMessage(c), t.onMessage(c);
544
592
  },
545
593
  onTyping(n) {
546
594
  s.setTyping(n);
547
595
  },
548
596
  onError(n) {
549
- t.onError(n), s.setConnected(!1), s.snapshot.isOpen && o.setOffline(!0);
597
+ t.onError(n), s.setConnected(!1), s.snapshot.isOpen && a.setOffline(!0);
550
598
  }
551
599
  }
552
600
  );
553
- i.mount(), o.mount();
554
- const l = s.snapshot.messages;
555
- if (l.length === 0 && e.welcome) {
601
+ l.mount(), a.mount();
602
+ const b = s.snapshot.messages;
603
+ if (b.length === 0 && e.welcome) {
556
604
  const n = {
557
605
  id: "welcome",
558
606
  author: "bot",
559
607
  text: e.welcome,
560
608
  ts: Date.now()
561
609
  };
562
- s.addMessage(n), o.appendMessage(n);
610
+ s.addMessage(n), a.appendMessage(n);
563
611
  } else
564
- o.updateMessages(l);
565
- const p = s.subscribe(() => {
612
+ a.updateMessages(b);
613
+ const I = s.subscribe(() => {
566
614
  const n = s.snapshot;
567
- i.setOpen(n.isOpen), o.setTyping(n.isTyping), o.setOffline(!n.isConnected), o.updateTheme(y(e.theme)), n.isOpen ? o.show() : o.hide();
615
+ l.setOpen(n.isOpen), a.setTyping(n.isTyping), a.setOffline(!n.isConnected), a.updateTheme(x(e.theme)), n.isOpen ? a.show() : a.hide();
568
616
  });
569
- a.connect().then((n) => {
570
- var h;
571
- e.persist && s.persistSession(n.sessionId), s.setConnected(!0), t.onTransportChange((h = a.kind) != null ? h : "ws");
572
- }).catch((n) => {
573
- t.onError(n), s.setConnected(!1), o.setOffline(!0);
617
+ o().then(
618
+ () => h.connect().then((n) => {
619
+ var c;
620
+ e.persist && s.persistSession(n.sessionId), s.setConnected(!0), t.onTransportChange((c = h.kind) != null ? c : "ws");
621
+ })
622
+ ).catch((n) => {
623
+ t.onError(n), s.setConnected(!1), a.setOffline(!0);
574
624
  });
575
- const b = {
625
+ const m = {
576
626
  transport: null,
577
627
  open() {
578
628
  s.open(), t.onOpen();
@@ -581,12 +631,12 @@ function v(r) {
581
631
  s.close(), t.onClose();
582
632
  },
583
633
  destroy() {
584
- p(), a.stop(), i.destroy(), o.destroy();
634
+ I(), h.stop(), l.destroy(), a.destroy();
585
635
  }
586
636
  };
587
- return b;
637
+ return m;
588
638
  }
589
- function E() {
639
+ function T() {
590
640
  var i, o;
591
641
  if (typeof document == "undefined") return;
592
642
  const r = document.currentScript;
@@ -598,10 +648,10 @@ function E() {
598
648
  console.error("SpilkiWidget: data-org and data-assistant are required for auto init");
599
649
  return;
600
650
  }
601
- v({
651
+ E({
602
652
  org: t,
603
653
  assistant: s,
604
- token: e.token,
654
+ installationToken: e.installationToken,
605
655
  apiBase: e.apiBase,
606
656
  position: (i = e.position) != null ? i : void 0,
607
657
  theme: (o = e.theme) != null ? o : void 0
@@ -609,8 +659,8 @@ function E() {
609
659
  }
610
660
  if (typeof window != "undefined") {
611
661
  const r = window;
612
- r.SpilkiWidget = r.SpilkiWidget || {}, r.SpilkiWidget.init = (e) => v(e);
662
+ r.SpilkiWidget = r.SpilkiWidget || {}, r.SpilkiWidget.init = (e) => E(e);
613
663
  }
614
- E();
615
- E();
664
+ T();
665
+ T();
616
666
  //# sourceMappingURL=bootstrap.es.js.map