@opencx/widget-core 4.0.44 → 4.0.46

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/index.js CHANGED
@@ -4,40 +4,40 @@ import { v4 as E } from "uuid";
4
4
  const V = (_) => {
5
5
  console.log(_.error);
6
6
  }, F = (_) => {
7
- const a = R({
7
+ const o = R({
8
8
  baseUrl: _.baseUrl
9
- }), n = {
9
+ }), i = {
10
10
  onRequest: _.onRequest,
11
11
  onResponse: _.onResponse,
12
12
  onError: _.onError || V
13
13
  };
14
- return a.use(n), a;
14
+ return o.use(i), o;
15
15
  };
16
16
  class A {
17
- constructor({ config: a }) {
17
+ constructor({ config: o }) {
18
18
  var h, s;
19
19
  this.userToken = null, this.constructClientOptions = (e) => {
20
- const t = this.config.apiUrl || "https://api.open.cx", o = {
20
+ const t = this.config.apiUrl || "https://api.open.cx", a = {
21
21
  "X-Bot-Token": this.config.token,
22
22
  "Content-Type": "application/json",
23
23
  Accept: "application/json",
24
24
  Authorization: e ? `Bearer ${e}` : void 0
25
25
  };
26
- return { baseUrl: t, headers: o };
26
+ return { baseUrl: t, headers: a };
27
27
  }, this.createOpenAPIClient = ({
28
28
  baseUrl: e,
29
29
  headers: t
30
30
  }) => F({
31
31
  baseUrl: e,
32
- onRequest: ({ request: o }) => {
33
- Object.entries(t).forEach(([i, l]) => {
34
- l && o.headers.set(i, l);
32
+ onRequest: ({ request: a }) => {
33
+ Object.entries(t).forEach(([n, r]) => {
34
+ r && a.headers.set(n, r);
35
35
  });
36
36
  }
37
37
  }), this.setAuthToken = (e) => {
38
38
  this.userToken = e;
39
- const { baseUrl: t, headers: o } = this.constructClientOptions(e);
40
- this.client = this.createOpenAPIClient({ baseUrl: t, headers: o });
39
+ const { baseUrl: t, headers: a } = this.constructClientOptions(e);
40
+ this.client = this.createOpenAPIClient({ baseUrl: t, headers: a });
41
41
  }, this.getExternalWidgetConfig = async () => await this.client.GET("/backend/widget/v2/config", {
42
42
  params: { header: { "X-Bot-Token": this.config.token } }
43
43
  }), this.sendMessage = async (e, t) => await this.client.POST("/backend/widget/v2/chat/send", {
@@ -54,54 +54,54 @@ class A {
54
54
  }), this.pollSessionAndHistory = async ({
55
55
  sessionId: e,
56
56
  lastMessageTimestamp: t,
57
- abortSignal: o
57
+ abortSignal: a
58
58
  }) => {
59
- const i = t ? { lastMessageTimestamp: t } : void 0;
59
+ const n = t ? { lastMessageTimestamp: t } : void 0;
60
60
  return await this.client.GET("/backend/widget/v2/poll/{sessionId}", {
61
- params: { path: { sessionId: e }, query: i },
62
- signal: o
61
+ params: { path: { sessionId: e }, query: n },
62
+ signal: a
63
63
  });
64
64
  }, this.getSessions = async ({
65
65
  cursor: e,
66
66
  filters: t,
67
- abortSignal: o
67
+ abortSignal: a
68
68
  }) => await this.client.GET("/backend/widget/v2/sessions", {
69
69
  params: { query: { cursor: e, filters: JSON.stringify(t) } },
70
- signal: o
70
+ signal: a
71
71
  }), this.uploadFile = async ({
72
72
  file: e,
73
73
  abortSignal: t,
74
- onProgress: o
75
- }) => new Promise((i, l) => {
74
+ onProgress: a
75
+ }) => new Promise((n, r) => {
76
76
  var y;
77
77
  const c = new FormData();
78
78
  c.append("file", e);
79
79
  const u = new XMLHttpRequest();
80
80
  if (t && (t.addEventListener("abort", () => {
81
- u.abort(), l(new DOMException("Aborted", "AbortError"));
81
+ u.abort(), r(new DOMException("Aborted", "AbortError"));
82
82
  }), t.aborted)) {
83
- l(new DOMException("Aborted", "AbortError"));
83
+ r(new DOMException("Aborted", "AbortError"));
84
84
  return;
85
85
  }
86
86
  u.upload.addEventListener("progress", (v) => {
87
- if (v.lengthComputable && o) {
87
+ if (v.lengthComputable && a) {
88
88
  const b = Math.round(v.loaded / v.total * 100);
89
- o(b);
89
+ a(b);
90
90
  }
91
91
  }), u.addEventListener("load", () => {
92
92
  if (u.status >= 200 && u.status < 300)
93
93
  try {
94
94
  const v = JSON.parse(u.responseText);
95
- i(v);
95
+ n(v);
96
96
  } catch (v) {
97
- l(new Error(`Failed to parse response: ${v}`));
97
+ r(new Error(`Failed to parse response: ${v}`));
98
98
  }
99
99
  else
100
- l(new Error(`Upload failed with status: ${u.status}`));
100
+ r(new Error(`Upload failed with status: ${u.status}`));
101
101
  }), u.addEventListener("error", () => {
102
- l(new Error("Network error occurred"));
102
+ r(new Error("Network error occurred"));
103
103
  }), u.addEventListener("timeout", () => {
104
- l(new Error("Upload timed out"));
104
+ r(new Error("Upload timed out"));
105
105
  });
106
106
  const { baseUrl: p } = this.constructClientOptions(this.userToken), d = `${p}/backend/widget/v2/upload`;
107
107
  u.open("POST", d), u.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((y = this.config.user) == null ? void 0 : y.token) ? u.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), u.send(c);
@@ -110,37 +110,37 @@ class A {
110
110
  signal: t
111
111
  }), this.createStateCheckpoint = async (e) => await this.client.POST("/backend/widget/v2/checkpoint", {
112
112
  body: e
113
- }), this.submitCsat = async (e) => await this.client.POST("/backend/widget/v2/submit-csat", { body: e }), this.config = a, this.userToken = ((h = a.user) == null ? void 0 : h.token) || null;
114
- const { baseUrl: n, headers: r } = this.constructClientOptions(
115
- (s = a.user) == null ? void 0 : s.token
113
+ }), this.submitCsat = async (e) => await this.client.POST("/backend/widget/v2/submit-csat", { body: e }), this.config = o, this.userToken = ((h = o.user) == null ? void 0 : h.token) || null;
114
+ const { baseUrl: i, headers: l } = this.constructClientOptions(
115
+ (s = o.user) == null ? void 0 : s.token
116
116
  );
117
- this.client = this.createOpenAPIClient({ baseUrl: n, headers: r });
117
+ this.client = this.createOpenAPIClient({ baseUrl: i, headers: l });
118
118
  }
119
119
  }
120
- function B(_, a) {
121
- console.error(`Missing case for ${_} in ${a}`);
120
+ function B(_, o) {
121
+ console.error(`Missing case for ${_} in ${o}`);
122
122
  }
123
123
  class S {
124
- constructor(a) {
125
- this.subscribers = /* @__PURE__ */ new Set(), this.get = () => this.state, this.set = (n) => {
126
- D(this.state, n) || (this.state = n, this.notifySubscribers(n));
127
- }, this.setPartial = (n) => {
128
- if (n == null) return;
129
- const r = { ...this.state, ...n };
130
- this.set(r);
124
+ constructor(o) {
125
+ this.subscribers = /* @__PURE__ */ new Set(), this.get = () => this.state, this.set = (i) => {
126
+ D(this.state, i) || (this.state = i, this.notifySubscribers(i));
127
+ }, this.setPartial = (i) => {
128
+ if (i == null) return;
129
+ const l = { ...this.state, ...i };
130
+ this.set(l);
131
131
  }, this.reset = () => {
132
132
  this.set(this.initialState);
133
- }, this.notifySubscribers = (n) => {
133
+ }, this.notifySubscribers = (i) => {
134
134
  Array.from(this.subscribers).forEach((h) => {
135
135
  try {
136
- h(n);
136
+ h(i);
137
137
  } catch (s) {
138
138
  console.error(s);
139
139
  }
140
140
  });
141
- }, this.subscribe = (n) => (this.subscribers.add(n), () => {
142
- this.subscribers.delete(n);
143
- }), this.state = a, this.initialState = a;
141
+ }, this.subscribe = (i) => (this.subscribers.add(i), () => {
142
+ this.subscribers.delete(i);
143
+ }), this.state = o, this.initialState = o;
144
144
  }
145
145
  }
146
146
  class L {
@@ -149,14 +149,14 @@ class L {
149
149
  isPolling: !1,
150
150
  isError: !1
151
151
  }), this.abortController = new AbortController(), this.reset = () => {
152
- var a;
153
- this.abortController.abort("Resetting poller"), (a = this.stopPolling) == null || a.call(this), this.stopPolling = null;
154
- }, this.stopPolling = null, this.startPolling = (a, n) => {
152
+ var o;
153
+ this.abortController.abort("Resetting poller"), (o = this.stopPolling) == null || o.call(this), this.stopPolling = null;
154
+ }, this.stopPolling = null, this.startPolling = (o, i) => {
155
155
  if (this.stopPolling) return;
156
- const r = [], h = async () => {
156
+ const l = [], h = async () => {
157
157
  this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
158
158
  try {
159
- await a(this.abortController.signal);
159
+ await o(this.abortController.signal);
160
160
  } catch (s) {
161
161
  if (this.abortController.signal.aborted)
162
162
  return;
@@ -164,27 +164,27 @@ class L {
164
164
  } finally {
165
165
  this.state.setPartial({ isPolling: !1 });
166
166
  }
167
- this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : r.push(setTimeout(h, n));
167
+ this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : l.push(setTimeout(h, i));
168
168
  };
169
169
  h(), this.stopPolling = () => {
170
- r.forEach(clearTimeout), this.state.reset();
170
+ l.forEach(clearTimeout), this.state.reset();
171
171
  };
172
172
  };
173
173
  }
174
174
  }
175
175
  function U(_) {
176
176
  try {
177
- const a = _();
178
- return a instanceof Promise ? a.then((n) => ({ data: n })).catch((n) => ({ error: n })) : { data: a };
179
- } catch (a) {
180
- return { error: a };
177
+ const o = _();
178
+ return o instanceof Promise ? o.then((i) => ({ data: i })).catch((i) => ({ error: i })) : { data: o };
179
+ } catch (o) {
180
+ return { error: o };
181
181
  }
182
182
  }
183
183
  class K {
184
184
  constructor({
185
- api: a,
186
- config: n,
187
- sessionCtx: r,
185
+ api: o,
186
+ config: i,
187
+ sessionCtx: l,
188
188
  messageCtx: h,
189
189
  sessionPollingIntervalSeconds: s
190
190
  }) {
@@ -210,14 +210,14 @@ class K {
210
210
  }, this.fetchSessionAndHistory = async ({
211
211
  sessionId: e,
212
212
  abortSignal: t,
213
- fetchFullHistory: o = !1
213
+ fetchFullHistory: a = !1
214
214
  }) => {
215
215
  var u;
216
216
  this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
217
- const i = this.messageCtx.state.get().messages, l = i.length > 0 ? ((u = i[i.length - 1]) == null ? void 0 : u.timestamp) ?? void 0 : void 0, { data: c } = await this.api.pollSessionAndHistory({
217
+ const n = this.messageCtx.state.get().messages, r = n.length > 0 ? ((u = n[n.length - 1]) == null ? void 0 : u.timestamp) ?? void 0 : void 0, { data: c } = await this.api.pollSessionAndHistory({
218
218
  sessionId: e,
219
219
  abortSignal: t,
220
- lastMessageTimestamp: o ? void 0 : l
220
+ lastMessageTimestamp: a ? void 0 : r
221
221
  });
222
222
  if (c != null && c.session && (this.sessionCtx.sessionState.setPartial({ session: c.session }), this.sessionCtx.setSessions([c.session])), c != null && c.history && c.history.length > 0) {
223
223
  const p = this.messageCtx.state.get().messages, w = c.history.map(this.mapHistoryToMessage).filter((d) => d !== null).filter(
@@ -229,7 +229,7 @@ class K {
229
229
  }
230
230
  this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
231
231
  }, this.mapHistoryToMessage = (e) => {
232
- var o;
232
+ var a;
233
233
  const t = {
234
234
  id: e.publicId,
235
235
  timestamp: e.sentAt || "",
@@ -261,14 +261,14 @@ class K {
261
261
  }
262
262
  };
263
263
  if (e.sender.kind === "ai") {
264
- const i = e.actionCalls && e.actionCalls.length > 0 ? e.actionCalls[e.actionCalls.length - 1] : void 0;
264
+ const n = e.actionCalls && e.actionCalls.length > 0 ? e.actionCalls[e.actionCalls.length - 1] : void 0;
265
265
  return {
266
266
  ...t,
267
267
  type: "AI",
268
268
  component: "bot_message",
269
269
  agent: {
270
270
  id: null,
271
- name: ((o = this.config.bot) == null ? void 0 : o.name) || "",
271
+ name: ((a = this.config.bot) == null ? void 0 : a.name) || "",
272
272
  isAi: !0,
273
273
  // Do not set avatarUrl from config here... let it be taken from the config at render time
274
274
  avatarUrl: null,
@@ -276,16 +276,16 @@ class K {
276
276
  },
277
277
  data: {
278
278
  message: e.content.text || "",
279
- action: i ? {
280
- name: i.actionName,
281
- data: this.extractActionResult(i)
279
+ action: n ? {
280
+ name: n.actionName,
281
+ data: this.extractActionResult(n)
282
282
  } : void 0
283
283
  }
284
284
  };
285
285
  }
286
286
  if (e.sender.kind === "system") {
287
- const i = this.constructSystemMessage(e);
288
- return i === null ? null : { ...i };
287
+ const n = this.constructSystemMessage(e);
288
+ return n === null ? null : { ...n };
289
289
  }
290
290
  return null;
291
291
  }, this.constructSystemMessage = (e) => {
@@ -335,34 +335,34 @@ class K {
335
335
  const t = e.result;
336
336
  if (t === null || typeof t != "object") return t;
337
337
  if ("responseBodyText" in t && typeof t.responseBodyText == "string") {
338
- const o = t.responseBodyText, i = U(() => JSON.parse(o)).data;
339
- if (i) return i;
338
+ const a = t.responseBodyText, n = U(() => JSON.parse(a)).data;
339
+ if (n) return n;
340
340
  }
341
341
  return e.result;
342
- }, this.api = a, this.config = n, this.sessionCtx = r, this.messageCtx = h, this.sessionPollingIntervalSeconds = s, this.registerPolling();
342
+ }, this.api = o, this.config = i, this.sessionCtx = l, this.messageCtx = h, this.sessionPollingIntervalSeconds = s, this.registerPolling();
343
343
  }
344
344
  }
345
345
  class H {
346
346
  constructor({
347
- config: a,
348
- api: n,
349
- storageCtx: r
347
+ config: o,
348
+ api: i,
349
+ storageCtx: l
350
350
  }) {
351
351
  var h;
352
352
  this.shouldCollectData = () => {
353
353
  var s;
354
354
  return !!(!((s = this.state.get().contact) != null && s.token) && this.config.collectUserData);
355
355
  }, this.autoCreateUnverifiedUserIfNotExists = async () => {
356
- var s, e, t, o, i, l, c, u, p, w, d, k, y, v;
356
+ var s, e, t, a, n, r, c, u, p, w, d, k, y, v;
357
357
  if (!((s = this.config.user) != null && s.token)) {
358
358
  if (this.config.collectUserData && !((t = (e = this.config.user) == null ? void 0 : e.data) != null && t.email)) {
359
- if ((o = this.config.extraDataCollectionFields) != null && o.length)
359
+ if ((a = this.config.extraDataCollectionFields) != null && a.length)
360
360
  return;
361
- const b = await ((i = this.storageCtx) == null ? void 0 : i.getContactToken());
361
+ const b = await ((n = this.storageCtx) == null ? void 0 : n.getContactToken());
362
362
  b && await this.setUnverifiedContact(b);
363
363
  return;
364
364
  }
365
- if (!((c = (l = this.config.user) == null ? void 0 : l.data) != null && c.email)) {
365
+ if (!((c = (r = this.config.user) == null ? void 0 : r.data) != null && c.email)) {
366
366
  const b = await ((u = this.storageCtx) == null ? void 0 : u.getContactToken());
367
367
  if (b) {
368
368
  await this.setUnverifiedContact(b);
@@ -388,14 +388,14 @@ class H {
388
388
  this.state.setPartial({ isCreatingUnverifiedContact: !1 });
389
389
  }
390
390
  }, this.setUnverifiedContact = async (s) => {
391
- var o, i, l, c;
392
- const e = await ((o = this.storageCtx) == null ? void 0 : o.getExternalContactId()), t = ((i = this.config.user) == null ? void 0 : i.externalId) || e || E();
393
- this.api.setAuthToken(s), await ((l = this.storageCtx) == null ? void 0 : l.setContactToken(s)), await ((c = this.storageCtx) == null ? void 0 : c.setExternalContactId(t)), this.state.setPartial({ contact: { token: s, externalId: t } });
394
- }, this.config = a, this.storageCtx = r, this.api = n, this.state = new S({
395
- contact: (h = a.user) != null && h.token ? {
396
- token: a.user.token,
391
+ var a, n, r, c;
392
+ const e = await ((a = this.storageCtx) == null ? void 0 : a.getExternalContactId()), t = ((n = this.config.user) == null ? void 0 : n.externalId) || e || E();
393
+ this.api.setAuthToken(s), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(s)), await ((c = this.storageCtx) == null ? void 0 : c.setExternalContactId(t)), this.state.setPartial({ contact: { token: s, externalId: t } });
394
+ }, this.config = o, this.storageCtx = l, this.api = i, this.state = new S({
395
+ contact: (h = o.user) != null && h.token ? {
396
+ token: o.user.token,
397
397
  // Set optional externalId from config... not local storage
398
- externalId: a.user.externalId
398
+ externalId: o.user.externalId
399
399
  } : null,
400
400
  extraCollectedData: void 0,
401
401
  isCreatingUnverifiedContact: !1,
@@ -408,14 +408,14 @@ function C() {
408
408
  }
409
409
  class J {
410
410
  constructor({
411
- config: a,
412
- api: n,
413
- sessionCtx: r,
411
+ config: o,
412
+ api: i,
413
+ sessionCtx: l,
414
414
  messageCtx: h
415
415
  }) {
416
416
  this.submitCsat = async (s) => {
417
- var l;
418
- const e = (l = this.sessionCtx.sessionState.get().session) == null ? void 0 : l.id;
417
+ var r;
418
+ const e = (r = this.sessionCtx.sessionState.get().session) == null ? void 0 : r.id;
419
419
  if (!e)
420
420
  return { data: null, error: "No session id found" };
421
421
  const t = C();
@@ -436,20 +436,20 @@ class J {
436
436
  }
437
437
  ]
438
438
  });
439
- const { data: o, error: i } = await this.api.submitCsat({
439
+ const { data: a, error: n } = await this.api.submitCsat({
440
440
  ...s,
441
441
  system_message_uuid: t,
442
442
  session_id: e
443
443
  });
444
- return { data: o, error: i };
445
- }, this.config = a, this.api = n, this.sessionCtx = r, this.messageCtx = h;
444
+ return { data: a, error: n };
445
+ }, this.config = o, this.api = i, this.sessionCtx = l, this.messageCtx = h;
446
446
  }
447
447
  }
448
448
  class q {
449
449
  constructor({
450
- config: a,
451
- api: n,
452
- contactCtx: r,
450
+ config: o,
451
+ api: i,
452
+ contactCtx: l,
453
453
  sessionsPollingIntervalSeconds: h
454
454
  }) {
455
455
  this.sessionsRefresher = new L(), this.sessionState = new S({
@@ -485,15 +485,15 @@ class q {
485
485
  }
486
486
  )
487
487
  ), this.createSession = async () => {
488
- var i;
488
+ var n, r, c;
489
489
  this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
490
- const s = (i = this.contactCtx.state.get().contact) == null ? void 0 : i.externalId, e = {
490
+ const s = (n = this.contactCtx.state.get().contact) == null ? void 0 : n.externalId, e = {
491
491
  ...this.getParsedCustomData(),
492
492
  ...s ? { external_id: s } : {}
493
- }, { data: t, error: o } = await this.api.createSession({
493
+ }, { data: t, error: a } = await this.api.createSession({
494
494
  customData: Object.keys(e).length > 0 ? e : void 0
495
495
  });
496
- return t ? (this.sessionState.setPartial({ session: t, isCreatingSession: !1 }), t) : (this.sessionState.setPartial({ isCreatingSession: !1 }), console.error("Failed to create session:", o), null);
496
+ return t ? (this.sessionState.setPartial({ session: t, isCreatingSession: !1 }), (c = (r = this.config.hooks) == null ? void 0 : r.onSessionCreated) == null || c.call(r, { session: t }), t) : (this.sessionState.setPartial({ isCreatingSession: !1 }), console.error("Failed to create session:", a), null);
497
497
  }, this.loadMoreSessions = async () => {
498
498
  if (this.sessionsState.get().isLastPage) return;
499
499
  const { data: s } = await this.getSessions({
@@ -501,7 +501,7 @@ class q {
501
501
  });
502
502
  if (s) {
503
503
  const t = [...this.sessionsState.get().data, ...s.items].filter(
504
- (o, i, l) => i === l.findIndex((c) => o.id === c.id)
504
+ (a, n, r) => n === r.findIndex((c) => a.id === c.id)
505
505
  );
506
506
  this.sessionsState.setPartial({
507
507
  data: t,
@@ -510,9 +510,9 @@ class q {
510
510
  });
511
511
  }
512
512
  }, this.getSessions = async ({ cursor: s }) => {
513
- var t, o;
513
+ var t, a;
514
514
  if (!((t = this.contactCtx.state.get().contact) != null && t.token)) return { data: null };
515
- const e = (o = this.contactCtx.state.get().contact) == null ? void 0 : o.externalId;
515
+ const e = (a = this.contactCtx.state.get().contact) == null ? void 0 : a.externalId;
516
516
  return await this.api.getSessions({
517
517
  cursor: s,
518
518
  filters: e ? {
@@ -521,7 +521,7 @@ class q {
521
521
  });
522
522
  }, this.setSessions = (s) => {
523
523
  const e = [...s, ...this.sessionsState.get().data].filter(
524
- (t, o, i) => o === i.findIndex((l) => t.id === l.id)
524
+ (t, a, n) => a === n.findIndex((r) => t.id === r.id)
525
525
  );
526
526
  this.sessionsState.setPartial({ data: e });
527
527
  }, this.refreshSessions = async () => {
@@ -537,22 +537,22 @@ class q {
537
537
  });
538
538
  return e ? (this.sessionState.setPartial({ session: e, isResolvingSession: !1 }), { success: !0, data: e }) : (this.sessionState.setPartial({ isResolvingSession: !1 }), { success: !1, error: t });
539
539
  }, this.createStateCheckpoint = async (s) => {
540
- var i;
541
- const e = (i = this.sessionState.get().session) == null ? void 0 : i.id;
540
+ var n;
541
+ const e = (n = this.sessionState.get().session) == null ? void 0 : n.id;
542
542
  if (!e) return;
543
- const { data: t, error: o } = await this.api.createStateCheckpoint({
543
+ const { data: t, error: a } = await this.api.createStateCheckpoint({
544
544
  session_id: e,
545
545
  payload: s
546
546
  });
547
547
  return t != null && t.success ? { success: !0 } : { success: !1 };
548
- }, this.config = a, this.api = n, this.contactCtx = r, this.sessionsPollingIntervalSeconds = h, this.registerSessionsRefresherWrapper();
548
+ }, this.config = o, this.api = i, this.contactCtx = l, this.sessionsPollingIntervalSeconds = h, this.registerSessionsRefresherWrapper();
549
549
  }
550
550
  }
551
551
  class Y {
552
552
  constructor({
553
- config: a,
554
- api: n,
555
- sessionCtx: r,
553
+ config: o,
554
+ api: i,
555
+ sessionCtx: l,
556
556
  contactCtx: h
557
557
  }) {
558
558
  this.state = new S({
@@ -564,7 +564,7 @@ class Y {
564
564
  }), this.sendMessageAbortController = new AbortController(), this.reset = () => {
565
565
  this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
566
566
  }, this.sendMessage = async (s) => {
567
- var e, t, o, i, l, c, u, p, w;
567
+ var e, t, a, n, r, c, u, p, w;
568
568
  try {
569
569
  if (!s.content.trim() && (!s.attachments || s.attachments.length === 0)) {
570
570
  console.warn(
@@ -604,14 +604,14 @@ class Y {
604
604
  ...I,
605
605
  z
606
606
  ]
607
- }), !((o = this.sessionCtx.sessionState.get().session) != null && o.id)) {
607
+ }), !((a = this.sessionCtx.sessionState.get().session) != null && a.id)) {
608
608
  if (!await this.sessionCtx.createSession()) {
609
609
  console.error("Failed to create session");
610
610
  return;
611
611
  }
612
612
  this.sessionCtx.refreshSessions();
613
613
  }
614
- const T = (i = this.sessionCtx.sessionState.get().session) == null ? void 0 : i.id;
614
+ const T = (n = this.sessionCtx.sessionState.get().session) == null ? void 0 : n.id;
615
615
  if (!T) return;
616
616
  const { data: m } = await this.api.sendMessage(
617
617
  {
@@ -645,7 +645,7 @@ class Y {
645
645
  (O) => O.id === g.id
646
646
  )) {
647
647
  this.state.setPartial({
648
- lastAIResMightSolveUserIssue: ((l = m.autopilotResponse) == null ? void 0 : l.mightSolveUserIssue) || ((c = m.uiResponse) == null ? void 0 : c.mightSolveUserIssue)
648
+ lastAIResMightSolveUserIssue: ((r = m.autopilotResponse) == null ? void 0 : r.mightSolveUserIssue) || ((c = m.uiResponse) == null ? void 0 : c.mightSolveUserIssue)
649
649
  });
650
650
  return;
651
651
  }
@@ -673,8 +673,8 @@ class Y {
673
673
  }
674
674
  }, this.toUserMessage = (s, e) => {
675
675
  const t = (() => {
676
- const o = this.contactCtx.state.get().extraCollectedData;
677
- return this.state.get().messages.length === 0 && o && Object.keys(o).length > 0 ? `${Object.entries(o).filter(([l, c]) => !!c).map(([l, c]) => `${l}: ${c}`).join(`
676
+ const a = this.contactCtx.state.get().extraCollectedData;
677
+ return this.state.get().messages.length === 0 && a && Object.keys(a).length > 0 ? `${Object.entries(a).filter(([r, c]) => !!c).map(([r, c]) => `${r}: ${c}`).join(`
678
678
  `)}
679
679
 
680
680
  ${s}` : s;
@@ -720,14 +720,14 @@ ${s}` : s;
720
720
  variant: "error",
721
721
  action: void 0
722
722
  }
723
- }), this.config = a, this.api = n, this.sessionCtx = r, this.contactCtx = h;
723
+ }), this.config = o, this.api = i, this.sessionCtx = l, this.contactCtx = h;
724
724
  }
725
725
  }
726
726
  class G {
727
727
  constructor({
728
- config: a,
729
- contactCtx: n,
730
- sessionCtx: r,
728
+ config: o,
729
+ contactCtx: i,
730
+ sessionCtx: l,
731
731
  resetChat: h
732
732
  }) {
733
733
  var s;
@@ -739,11 +739,11 @@ class G {
739
739
  });
740
740
  }), this.sessionCtx.sessionsState.subscribe(
741
741
  ({ isInitialFetchLoading: e, data: t }) => {
742
- var o, i, l, c;
743
- if ((o = this.config.router) != null && o.chatScreenOnly && // Do not route to a chat if we are currently inside one already
742
+ var a, n, r, c;
743
+ if ((a = this.config.router) != null && a.chatScreenOnly && // Do not route to a chat if we are currently inside one already
744
744
  // This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
745
- !((i = this.sessionCtx.sessionState.get().session) != null && i.id)) {
746
- const u = (l = t.find((p) => p.isOpened)) == null ? void 0 : l.id;
745
+ !((n = this.sessionCtx.sessionState.get().session) != null && n.id)) {
746
+ const u = (r = t.find((p) => p.isOpened)) == null ? void 0 : r.id;
747
747
  return u ? this.toChatScreen(u) : void 0;
748
748
  }
749
749
  t.length || ((c = this.config.router) == null ? void 0 : c.goToChatIfNoSessions) !== !1 && !e && this.state.get().screen !== "chat" && this.toChatScreen();
@@ -752,37 +752,41 @@ class G {
752
752
  }, this.toSessionsScreen = () => {
753
753
  this.resetChat(), this.state.setPartial({ screen: "sessions" });
754
754
  }, this.toChatScreen = (e) => {
755
- if (this.resetChat(), e) {
756
- const t = this.sessionCtx.sessionsState.get().data.find((o) => o.id === e);
757
- if (!t) return;
755
+ var a, n;
756
+ this.resetChat();
757
+ let t;
758
+ if (e) {
759
+ if (t = this.sessionCtx.sessionsState.get().data.find((r) => r.id === e), !t) return;
758
760
  this.sessionCtx.sessionState.setPartial({ session: t });
759
761
  }
760
- this.state.setPartial({ screen: "chat" });
761
- }, this.config = a, this.contactCtx = n, this.sessionCtx = r, this.resetChat = h, this.state = new S({
762
+ this.state.setPartial({ screen: "chat" }), (n = (a = this.config.hooks) == null ? void 0 : a.onNavigateToChat) == null || n.call(a, {
763
+ session: t
764
+ });
765
+ }, this.config = o, this.contactCtx = i, this.sessionCtx = l, this.resetChat = h, this.state = new S({
762
766
  screen: this.contactCtx.shouldCollectData() ? "welcome" : (s = this.config.router) != null && s.chatScreenOnly ? "chat" : "sessions"
763
767
  }), this.registerRoutingListener();
764
768
  }
765
769
  }
766
770
  class $ {
767
771
  constructor({
768
- storage: a,
769
- config: n
772
+ storage: o,
773
+ config: i
770
774
  }) {
771
775
  this.KEYS = {
772
- contactToken: (r) => `opencx-widget:org-token-${r}:contact-token`,
773
- externalContactId: (r) => `opencx-widget:org-token-${r}:external-contact-id`
774
- }, this.setContactToken = async (r) => {
775
- await this.storage.set(this.KEYS.contactToken(this.config.token), r);
776
- }, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken(this.config.token)), this.setExternalContactId = async (r) => {
777
- await this.storage.set(this.KEYS.externalContactId(this.config.token), r);
778
- }, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId(this.config.token)), this.storage = a, this.config = n;
776
+ contactToken: (l) => `opencx-widget:org-token-${l}:contact-token`,
777
+ externalContactId: (l) => `opencx-widget:org-token-${l}:external-contact-id`
778
+ }, this.setContactToken = async (l) => {
779
+ await this.storage.set(this.KEYS.contactToken(this.config.token), l);
780
+ }, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken(this.config.token)), this.setExternalContactId = async (l) => {
781
+ await this.storage.set(this.KEYS.externalContactId(this.config.token), l);
782
+ }, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId(this.config.token)), this.storage = o, this.config = i;
779
783
  }
780
784
  }
781
785
  const f = class f {
782
786
  constructor({
783
- config: a,
784
- storage: n,
785
- modes: r,
787
+ config: o,
788
+ storage: i,
789
+ modes: l,
786
790
  org: h
787
791
  }) {
788
792
  if (this.modes = [], this.resetChat = () => {
@@ -791,7 +795,7 @@ const f = class f {
791
795
  throw Error(
792
796
  "Widget polling values are not defined, did you call WidgetCtx.initialize()"
793
797
  );
794
- this.config = a, this.org = h, this.api = new A({ config: a }), this.storageCtx = n ? new $({ storage: n, config: a }) : void 0, this.modes = r, this.contactCtx = new H({
798
+ this.config = o, this.org = h, this.api = new A({ config: o }), this.storageCtx = i ? new $({ storage: i, config: o }) : void 0, this.modes = l, this.contactCtx = new H({
795
799
  api: this.api,
796
800
  config: this.config,
797
801
  storageCtx: this.storageCtx
@@ -825,25 +829,25 @@ const f = class f {
825
829
  }
826
830
  };
827
831
  f.pollingIntervalsSeconds = null, f.initialize = async ({
828
- config: a,
829
- storage: n
832
+ config: o,
833
+ storage: i
830
834
  }) => {
831
835
  var h, s, e;
832
- const r = await new A({
833
- config: a
836
+ const l = await new A({
837
+ config: o
834
838
  }).getExternalWidgetConfig();
835
- if (!r.data)
839
+ if (!l.data)
836
840
  throw new Error("Failed to fetch widget config");
837
841
  return f.pollingIntervalsSeconds = {
838
- session: ((h = r.data) == null ? void 0 : h.sessionPollingIntervalSeconds) || 10,
839
- sessions: ((s = r.data) == null ? void 0 : s.sessionsPollingIntervalSeconds) || 60
842
+ session: ((h = l.data) == null ? void 0 : h.sessionPollingIntervalSeconds) || 10,
843
+ sessions: ((s = l.data) == null ? void 0 : s.sessionsPollingIntervalSeconds) || 60
840
844
  }, new f({
841
- config: a,
842
- storage: n,
843
- modes: ((e = r.data) == null ? void 0 : e.modes) || [],
845
+ config: o,
846
+ storage: i,
847
+ modes: ((e = l.data) == null ? void 0 : e.modes) || [],
844
848
  org: {
845
- id: r.data.org.id,
846
- name: r.data.org.name
849
+ id: l.data.org.id,
850
+ name: l.data.org.name
847
851
  }
848
852
  });
849
853
  };
@@ -1499,9 +1503,9 @@ const W = {
1499
1503
  function De(_) {
1500
1504
  return Le.includes(_);
1501
1505
  }
1502
- function Ve(_, a, n) {
1503
- var r;
1504
- return ((r = n == null ? void 0 : n[a]) == null ? void 0 : r[_]) || N[a][_] || "";
1506
+ function Ve(_, o, i) {
1507
+ var l;
1508
+ return ((l = i == null ? void 0 : i[o]) == null ? void 0 : l[_]) || N[o][_] || "";
1505
1509
  }
1506
1510
  export {
1507
1511
  S as PrimitiveState,