@opencx/widget 3.0.84 → 3.0.85

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.
@@ -6,45 +6,45 @@ const F = (d) => {
6
6
  }, _ = (d) => {
7
7
  const n = M({
8
8
  baseUrl: d.baseUrl
9
- }), i = {
9
+ }), a = {
10
10
  onRequest: d.onRequest,
11
11
  onResponse: d.onResponse,
12
12
  onError: d.onError || F
13
13
  };
14
- return n.use(i), n;
14
+ return n.use(a), n;
15
15
  };
16
16
  class T {
17
17
  constructor({ config: n }) {
18
- var a, s;
18
+ var o, e;
19
19
  this.userToken = null, this.constructClientOptions = (t) => {
20
- const e = this.config.apiUrl || "https://api.open.cx", o = {
20
+ const s = this.config.apiUrl || "https://api.open.cx", i = {
21
21
  "X-Bot-Token": this.config.token,
22
22
  "Content-Type": "application/json",
23
23
  Accept: "application/json",
24
24
  Authorization: t ? `Bearer ${t}` : void 0
25
25
  };
26
- return { baseUrl: e, headers: o };
26
+ return { baseUrl: s, headers: i };
27
27
  }, this.createOpenAPIClient = ({
28
28
  baseUrl: t,
29
- headers: e
29
+ headers: s
30
30
  }) => _({
31
31
  baseUrl: t,
32
- onRequest: ({ request: o }) => {
33
- Object.entries(e).forEach(([l, r]) => {
34
- r && o.headers.set(l, r);
32
+ onRequest: ({ request: i }) => {
33
+ Object.entries(s).forEach(([c, r]) => {
34
+ r && i.headers.set(c, r);
35
35
  });
36
36
  }
37
37
  }), this.setAuthToken = (t) => {
38
38
  this.userToken = t;
39
- const { baseUrl: e, headers: o } = this.constructClientOptions(t);
40
- this.client = this.createOpenAPIClient({ baseUrl: e, headers: o });
39
+ const { baseUrl: s, headers: i } = this.constructClientOptions(t);
40
+ this.client = this.createOpenAPIClient({ baseUrl: s, headers: i });
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.widgetPrelude = async () => await this.client.GET("/backend/widget/v2/prelude", {
44
44
  params: { header: { "X-Bot-Token": this.config.token } }
45
- }), this.sendMessage = async (t, e) => await this.client.POST("/backend/widget/v2/chat/send", {
45
+ }), this.sendMessage = async (t, s) => await this.client.POST("/backend/widget/v2/chat/send", {
46
46
  body: t,
47
- signal: e
47
+ signal: s
48
48
  }), this.createUnverifiedContact = async (t) => await this.client.POST(
49
49
  "/backend/widget/v2/contact/create-unverified",
50
50
  {
@@ -55,88 +55,88 @@ class T {
55
55
  body: t
56
56
  }), this.pollSessionAndHistory = async ({
57
57
  sessionId: t,
58
- lastMessageTimestamp: e,
59
- abortSignal: o
58
+ lastMessageTimestamp: s,
59
+ abortSignal: i
60
60
  }) => {
61
- const l = e ? { lastMessageTimestamp: e } : void 0;
61
+ const c = s ? { lastMessageTimestamp: s } : void 0;
62
62
  return await this.client.GET("/backend/widget/v2/poll/{sessionId}", {
63
- params: { path: { sessionId: t }, query: l },
64
- signal: o
63
+ params: { path: { sessionId: t }, query: c },
64
+ signal: i
65
65
  });
66
66
  }, this.getSessions = async ({
67
67
  cursor: t,
68
- filters: e,
69
- abortSignal: o
68
+ filters: s,
69
+ abortSignal: i
70
70
  }) => await this.client.GET("/backend/widget/v2/sessions", {
71
- params: { query: { cursor: t, filters: JSON.stringify(e) } },
72
- signal: o
71
+ params: { query: { cursor: t, filters: JSON.stringify(s) } },
72
+ signal: i
73
73
  }), this.uploadFile = async ({
74
74
  file: t,
75
- abortSignal: e,
76
- onProgress: o
77
- }) => new Promise((l, r) => {
75
+ abortSignal: s,
76
+ onProgress: i
77
+ }) => new Promise((c, r) => {
78
78
  var v;
79
- const g = new FormData();
80
- g.append("file", t);
81
- const c = new XMLHttpRequest();
82
- if (e && (e.addEventListener("abort", () => {
83
- c.abort(), r(new DOMException("Aborted", "AbortError"));
84
- }), e.aborted)) {
79
+ const h = new FormData();
80
+ h.append("file", t);
81
+ const l = new XMLHttpRequest();
82
+ if (s && (s.addEventListener("abort", () => {
83
+ l.abort(), r(new DOMException("Aborted", "AbortError"));
84
+ }), s.aborted)) {
85
85
  r(new DOMException("Aborted", "AbortError"));
86
86
  return;
87
87
  }
88
- c.upload.addEventListener("progress", (u) => {
89
- if (u.lengthComputable && o) {
90
- const m = Math.round(u.loaded / u.total * 100);
91
- o(m);
88
+ l.upload.addEventListener("progress", (u) => {
89
+ if (u.lengthComputable && i) {
90
+ const S = Math.round(u.loaded / u.total * 100);
91
+ i(S);
92
92
  }
93
- }), c.addEventListener("load", () => {
94
- if (c.status >= 200 && c.status < 300)
93
+ }), l.addEventListener("load", () => {
94
+ if (l.status >= 200 && l.status < 300)
95
95
  try {
96
- const u = JSON.parse(c.responseText);
97
- l(u);
96
+ const u = JSON.parse(l.responseText);
97
+ c(u);
98
98
  } catch (u) {
99
99
  r(new Error(`Failed to parse response: ${u}`));
100
100
  }
101
101
  else
102
- r(new Error(`Upload failed with status: ${c.status}`));
103
- }), c.addEventListener("error", () => {
102
+ r(new Error(`Upload failed with status: ${l.status}`));
103
+ }), l.addEventListener("error", () => {
104
104
  r(new Error("Network error occurred"));
105
- }), c.addEventListener("timeout", () => {
105
+ }), l.addEventListener("timeout", () => {
106
106
  r(new Error("Upload timed out"));
107
107
  });
108
- const { baseUrl: C } = this.constructClientOptions(this.userToken), p = `${C}/backend/widget/v2/upload`;
109
- c.open("POST", p), c.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((v = this.config.user) == null ? void 0 : v.token) ? c.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), c.send(g);
110
- }), this.vote = async (t) => await this.client.POST("/backend/widget/v2/chat/vote", { body: t }), this.resolveSession = async (t, e) => await this.client.POST("/backend/widget/v2/session/resolve", {
108
+ const { baseUrl: f } = this.constructClientOptions(this.userToken), p = `${f}/backend/widget/v2/upload`;
109
+ l.open("POST", p), l.setRequestHeader("X-Bot-Token", this.config.token), this.userToken ?? ((v = this.config.user) == null ? void 0 : v.token) ? l.setRequestHeader("Authorization", `Bearer ${this.userToken}`) : console.error("User token not set"), l.send(h);
110
+ }), this.vote = async (t) => await this.client.POST("/backend/widget/v2/chat/vote", { body: t }), this.resolveSession = async (t, s) => await this.client.POST("/backend/widget/v2/session/resolve", {
111
111
  body: t,
112
- signal: e
113
- }), this.config = n, this.userToken = ((a = n.user) == null ? void 0 : a.token) || null;
114
- const { baseUrl: i, headers: h } = this.constructClientOptions(
115
- (s = n.user) == null ? void 0 : s.token
112
+ signal: s
113
+ }), this.config = n, this.userToken = ((o = n.user) == null ? void 0 : o.token) || null;
114
+ const { baseUrl: a, headers: g } = this.constructClientOptions(
115
+ (e = n.user) == null ? void 0 : e.token
116
116
  );
117
- this.client = this.createOpenAPIClient({ baseUrl: i, headers: h });
117
+ this.client = this.createOpenAPIClient({ baseUrl: a, headers: g });
118
118
  }
119
119
  }
120
120
  class P {
121
121
  constructor(n) {
122
- this.subscribers = /* @__PURE__ */ new Set(), this.get = () => this.state, this.set = (i) => {
123
- O(this.state, i) || (this.state = i, this.notifySubscribers(i));
124
- }, this.setPartial = (i) => {
125
- if (i == null) return;
126
- const h = { ...this.state, ...i };
127
- this.set(h);
122
+ this.subscribers = /* @__PURE__ */ new Set(), this.get = () => this.state, this.set = (a) => {
123
+ O(this.state, a) || (this.state = a, this.notifySubscribers(a));
124
+ }, this.setPartial = (a) => {
125
+ if (a == null) return;
126
+ const g = { ...this.state, ...a };
127
+ this.set(g);
128
128
  }, this.reset = () => {
129
129
  this.set(this.initialState);
130
- }, this.notifySubscribers = (i) => {
131
- Array.from(this.subscribers).forEach((a) => {
130
+ }, this.notifySubscribers = (a) => {
131
+ Array.from(this.subscribers).forEach((o) => {
132
132
  try {
133
- a(i);
134
- } catch (s) {
135
- console.error(s);
133
+ o(a);
134
+ } catch (e) {
135
+ console.error(e);
136
136
  }
137
137
  });
138
- }, this.subscribe = (i) => (this.subscribers.add(i), () => {
139
- this.subscribers.delete(i);
138
+ }, this.subscribe = (a) => (this.subscribers.add(a), () => {
139
+ this.subscribers.delete(a);
140
140
  }), this.state = n, this.initialState = n;
141
141
  }
142
142
  }
@@ -148,23 +148,23 @@ class A {
148
148
  }), this.abortController = new AbortController(), this.reset = () => {
149
149
  var n;
150
150
  this.abortController.abort("Resetting poller"), (n = this.stopPolling) == null || n.call(this), this.stopPolling = null;
151
- }, this.stopPolling = null, this.startPolling = (n, i) => {
151
+ }, this.stopPolling = null, this.startPolling = (n, a) => {
152
152
  if (this.stopPolling) return;
153
- const h = [], a = async () => {
153
+ const g = [], o = async () => {
154
154
  this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
155
155
  try {
156
156
  await n(this.abortController.signal);
157
- } catch (s) {
157
+ } catch (e) {
158
158
  if (this.abortController.signal.aborted)
159
159
  return;
160
- console.error("Failed to poll:", s), this.state.setPartial({ isError: !0 });
160
+ console.error("Failed to poll:", e), this.state.setPartial({ isError: !0 });
161
161
  } finally {
162
162
  this.state.setPartial({ isPolling: !1 });
163
163
  }
164
- this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : h.push(setTimeout(a, i));
164
+ this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : g.push(setTimeout(o, a));
165
165
  };
166
- a(), this.stopPolling = () => {
167
- h.forEach(clearTimeout), this.state.reset();
166
+ o(), this.stopPolling = () => {
167
+ g.forEach(clearTimeout), this.state.reset();
168
168
  };
169
169
  };
170
170
  }
@@ -172,7 +172,7 @@ class A {
172
172
  function D(d) {
173
173
  try {
174
174
  const n = d();
175
- return n instanceof Promise ? n.then((i) => ({ data: i })).catch((i) => ({ error: i })) : { data: n };
175
+ return n instanceof Promise ? n.then((a) => ({ data: a })).catch((a) => ({ error: a })) : { data: n };
176
176
  } catch (n) {
177
177
  return { error: n };
178
178
  }
@@ -180,51 +180,51 @@ function D(d) {
180
180
  class L {
181
181
  constructor({
182
182
  api: n,
183
- config: i,
184
- sessionCtx: h,
185
- messageCtx: a,
186
- sessionPollingIntervalSeconds: s
183
+ config: a,
184
+ sessionCtx: g,
185
+ messageCtx: o,
186
+ sessionPollingIntervalSeconds: e
187
187
  }) {
188
188
  this.poller = new A(), this.registerPolling = () => {
189
189
  this.sessionCtx.sessionState.subscribe(({ session: t }) => {
190
- t != null && t.id ? this.poller.startPolling(async (e) => {
191
- this.hackAndSlash(t.id, e);
190
+ t != null && t.id ? this.poller.startPolling(async (s) => {
191
+ this.hackAndSlash(t.id, s);
192
192
  }, this.sessionPollingIntervalSeconds * 1e3) : this.poller.reset();
193
193
  });
194
- }, this.hackAndSlash = async (t, e) => {
195
- var g;
194
+ }, this.hackAndSlash = async (t, s) => {
195
+ var h;
196
196
  this.messageCtx.state.get().messages.length === 0 && this.messageCtx.state.setPartial({ isInitialFetchLoading: !0 });
197
- const o = this.messageCtx.state.get().messages, l = o.length > 0 ? (g = o[o.length - 1]) == null ? void 0 : g.timestamp : void 0, { data: r } = await this.api.pollSessionAndHistory({
197
+ const i = this.messageCtx.state.get().messages, c = i.length > 0 ? (h = i[i.length - 1]) == null ? void 0 : h.timestamp : void 0, { data: r } = await this.api.pollSessionAndHistory({
198
198
  sessionId: t,
199
- abortSignal: e,
200
- lastMessageTimestamp: l
199
+ abortSignal: s,
200
+ lastMessageTimestamp: c
201
201
  });
202
202
  if (r != null && r.session && (this.sessionCtx.sessionState.setPartial({ session: r.session }), this.sessionCtx.setSessions([r.session])), r != null && r.history && r.history.length > 0) {
203
- const c = this.messageCtx.state.get().messages, C = r.history.map(this.mapHistoryToMessage).filter(
204
- (x) => !c.some((p) => p.id === x.id)
203
+ const l = this.messageCtx.state.get().messages, f = r.history.map(this.mapHistoryToMessage).filter(
204
+ (x) => !l.some((p) => p.id === x.id)
205
205
  );
206
206
  this.messageCtx.state.setPartial({
207
- messages: [...c, ...C]
207
+ messages: [...l, ...f]
208
208
  });
209
209
  }
210
210
  this.messageCtx.state.get().isInitialFetchLoading && this.messageCtx.state.setPartial({ isInitialFetchLoading: !1 });
211
211
  }, this.mapHistoryToMessage = (t) => {
212
- var l, r;
213
- const e = {
212
+ var c, r;
213
+ const s = {
214
214
  id: t.publicId,
215
215
  timestamp: t.sentAt || "",
216
216
  attachments: t.attachments || void 0
217
217
  };
218
218
  if (t.sender.kind === "user")
219
219
  return {
220
- ...e,
220
+ ...s,
221
221
  type: "FROM_USER",
222
222
  content: t.content.text || "",
223
223
  deliveredAt: t.sentAt || ""
224
224
  };
225
225
  if (t.sender.kind === "agent")
226
226
  return {
227
- ...e,
227
+ ...s,
228
228
  type: "FROM_AGENT",
229
229
  component: "agent_message",
230
230
  data: {
@@ -237,87 +237,87 @@ class L {
237
237
  isAi: !1
238
238
  }
239
239
  };
240
- const o = t.actionCalls && t.actionCalls.length > 0 ? t.actionCalls[t.actionCalls.length - 1] : void 0;
240
+ const i = t.actionCalls && t.actionCalls.length > 0 ? t.actionCalls[t.actionCalls.length - 1] : void 0;
241
241
  return {
242
- ...e,
242
+ ...s,
243
243
  type: "FROM_BOT",
244
244
  component: "bot_message",
245
245
  agent: {
246
246
  id: null,
247
- name: ((l = this.config.bot) == null ? void 0 : l.name) || "",
247
+ name: ((c = this.config.bot) == null ? void 0 : c.name) || "",
248
248
  isAi: !0,
249
249
  avatar: ((r = this.config.bot) == null ? void 0 : r.avatar) || ""
250
250
  },
251
251
  data: {
252
252
  message: t.content.text || "",
253
- action: o ? {
254
- name: o.actionName,
255
- data: this.extractActionResult(o)
253
+ action: i ? {
254
+ name: i.actionName,
255
+ data: this.extractActionResult(i)
256
256
  } : void 0
257
257
  }
258
258
  };
259
259
  }, this.extractActionResult = (t) => {
260
- const e = t.result;
261
- if (e === null || typeof e != "object") return e;
262
- if ("responseBodyText" in e && typeof e.responseBodyText == "string") {
263
- const o = e.responseBodyText, l = D(() => JSON.parse(o)).data;
264
- if (l) return l;
260
+ const s = t.result;
261
+ if (s === null || typeof s != "object") return s;
262
+ if ("responseBodyText" in s && typeof s.responseBodyText == "string") {
263
+ const i = s.responseBodyText, c = D(() => JSON.parse(i)).data;
264
+ if (c) return c;
265
265
  }
266
266
  return t.result;
267
- }, this.api = n, this.config = i, this.sessionCtx = h, this.messageCtx = a, this.sessionPollingIntervalSeconds = s, this.registerPolling();
267
+ }, this.api = n, this.config = a, this.sessionCtx = g, this.messageCtx = o, this.sessionPollingIntervalSeconds = e, this.registerPolling();
268
268
  }
269
269
  }
270
270
  class B {
271
271
  constructor({
272
272
  config: n,
273
- api: i,
274
- storageCtx: h
273
+ api: a,
274
+ storageCtx: g
275
275
  }) {
276
- var a;
276
+ var o;
277
277
  this.shouldCollectData = () => {
278
- var s;
279
- return !!(!((s = this.state.get().contact) != null && s.token) && this.config.collectUserData);
278
+ var e;
279
+ return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
280
280
  }, this.autoCreateUnverifiedUserIfNotExists = async () => {
281
- var s, t, e, o, l, r, g, c, C, x, p, b, v, u;
282
- if (!((s = this.config.user) != null && s.token)) {
283
- if (this.config.collectUserData && !((e = (t = this.config.user) == null ? void 0 : t.data) != null && e.email)) {
284
- if ((o = this.config.extraDataCollectionFields) != null && o.length)
281
+ var e, t, s, i, c, r, h, l, f, x, p, b, v, u;
282
+ if (!((e = this.config.user) != null && e.token)) {
283
+ if (this.config.collectUserData && !((s = (t = this.config.user) == null ? void 0 : t.data) != null && s.email)) {
284
+ if ((i = this.config.extraDataCollectionFields) != null && i.length)
285
285
  return;
286
- const m = await ((l = this.storageCtx) == null ? void 0 : l.getContactToken());
287
- m && await this.setUnverifiedContact(m);
286
+ const S = await ((c = this.storageCtx) == null ? void 0 : c.getContactToken());
287
+ S && await this.setUnverifiedContact(S);
288
288
  return;
289
289
  }
290
- if (!((g = (r = this.config.user) == null ? void 0 : r.data) != null && g.email)) {
291
- const m = await ((c = this.storageCtx) == null ? void 0 : c.getContactToken());
292
- if (m) {
293
- await this.setUnverifiedContact(m);
290
+ if (!((h = (r = this.config.user) == null ? void 0 : r.data) != null && h.email)) {
291
+ const S = await ((l = this.storageCtx) == null ? void 0 : l.getContactToken());
292
+ if (S) {
293
+ await this.setUnverifiedContact(S);
294
294
  return;
295
295
  }
296
296
  }
297
297
  await this.createUnverifiedContact({
298
- email: (x = (C = this.config.user) == null ? void 0 : C.data) == null ? void 0 : x.email,
298
+ email: (x = (f = this.config.user) == null ? void 0 : f.data) == null ? void 0 : x.email,
299
299
  non_verified_name: ((b = (p = this.config.user) == null ? void 0 : p.data) == null ? void 0 : b.name) || "Anonymous",
300
300
  non_verified_custom_data: (u = (v = this.config.user) == null ? void 0 : v.data) == null ? void 0 : u.customData
301
301
  });
302
302
  }
303
- }, this.createUnverifiedContact = async (s, t) => {
303
+ }, this.createUnverifiedContact = async (e, t) => {
304
304
  this.state.setPartial({ extraCollectedData: t });
305
305
  try {
306
306
  this.state.setPartial({
307
307
  isCreatingUnverifiedContact: !0,
308
308
  isErrorCreatingUnverifiedContact: !1
309
309
  });
310
- const { data: e } = await this.api.createUnverifiedContact(s);
311
- e != null && e.token ? await this.setUnverifiedContact(e.token) : this.state.setPartial({ isErrorCreatingUnverifiedContact: !0 });
310
+ const { data: s } = await this.api.createUnverifiedContact(e);
311
+ s != null && s.token ? await this.setUnverifiedContact(s.token) : this.state.setPartial({ isErrorCreatingUnverifiedContact: !0 });
312
312
  } finally {
313
313
  this.state.setPartial({ isCreatingUnverifiedContact: !1 });
314
314
  }
315
- }, this.setUnverifiedContact = async (s) => {
316
- var o, l, r, g;
317
- const t = await ((o = this.storageCtx) == null ? void 0 : o.getExternalContactId()), e = ((l = this.config.user) == null ? void 0 : l.externalId) || t || E();
318
- this.api.setAuthToken(s), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(s)), await ((g = this.storageCtx) == null ? void 0 : g.setExternalContactId(e)), this.state.setPartial({ contact: { token: s, externalId: e } });
319
- }, this.config = n, this.storageCtx = h, this.api = i, this.state = new P({
320
- contact: (a = n.user) != null && a.token ? {
315
+ }, this.setUnverifiedContact = async (e) => {
316
+ var i, c, r, h;
317
+ const t = await ((i = this.storageCtx) == null ? void 0 : i.getExternalContactId()), s = ((c = this.config.user) == null ? void 0 : c.externalId) || t || E();
318
+ this.api.setAuthToken(e), await ((r = this.storageCtx) == null ? void 0 : r.setContactToken(e)), await ((h = this.storageCtx) == null ? void 0 : h.setExternalContactId(s)), this.state.setPartial({ contact: { token: e, externalId: s } });
319
+ }, this.config = n, this.storageCtx = g, this.api = a, this.state = new P({
320
+ contact: (o = n.user) != null && o.token ? {
321
321
  token: n.user.token,
322
322
  // Set optional externalId from config... not local storage
323
323
  externalId: n.user.externalId
@@ -334,8 +334,8 @@ function y() {
334
334
  class q {
335
335
  constructor({
336
336
  api: n,
337
- contactCtx: i,
338
- sessionsPollingIntervalSeconds: h
337
+ contactCtx: a,
338
+ sessionsPollingIntervalSeconds: g
339
339
  }) {
340
340
  this.sessionsRefresher = new A(), this.sessionState = new P({
341
341
  session: null,
@@ -352,73 +352,73 @@ class q {
352
352
  }), this.reset = async () => {
353
353
  this.sessionState.reset();
354
354
  }, this.registerSessionsRefresherWrapper = () => {
355
- var a;
355
+ var o;
356
356
  // If the widget config was initially provided with a contact token, no state change would be triggered, so we just fetch
357
- (a = this.contactCtx.state.get().contact) != null && a.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: s }) => {
358
- s != null && s.token && !this.sessionsState.get().didStartInitialFetch && this.registerSessionsRefresher();
357
+ (o = this.contactCtx.state.get().contact) != null && o.token && !this.sessionsState.get().didStartInitialFetch ? this.registerSessionsRefresher() : this.contactCtx.state.subscribe(({ contact: e }) => {
358
+ e != null && e.token && !this.sessionsState.get().didStartInitialFetch && this.registerSessionsRefresher();
359
359
  });
360
360
  }, this.registerSessionsRefresher = () => {
361
361
  this.sessionsRefresher.startPolling(async () => {
362
362
  this.sessionsState.get().didStartInitialFetch === !1 && this.sessionsState.setPartial({ didStartInitialFetch: !0 }), await this.refreshSessions(), this.sessionsState.get().isInitialFetchLoading === !0 && this.sessionsState.setPartial({ isInitialFetchLoading: !1 });
363
363
  }, this.sessionsPollingIntervalSeconds * 1e3);
364
364
  }, this.createSession = async () => {
365
- var e;
365
+ var s;
366
366
  this.sessionState.setPartial({ session: null, isCreatingSession: !0 });
367
- const a = (e = this.contactCtx.state.get().contact) == null ? void 0 : e.externalId, { data: s, error: t } = await this.api.createSession({
368
- customData: a ? {
369
- external_id: a
367
+ const o = (s = this.contactCtx.state.get().contact) == null ? void 0 : s.externalId, { data: e, error: t } = await this.api.createSession({
368
+ customData: o ? {
369
+ external_id: o
370
370
  } : void 0
371
371
  });
372
- return s ? (this.sessionState.setPartial({ session: s, isCreatingSession: !1 }), s) : (console.error("Failed to create session:", t), null);
372
+ return e ? (this.sessionState.setPartial({ session: e, isCreatingSession: !1 }), e) : (console.error("Failed to create session:", t), null);
373
373
  }, this.loadMoreSessions = async () => {
374
374
  if (this.sessionsState.get().isLastPage) return;
375
- const { data: a } = await this.getSessions({
375
+ const { data: o } = await this.getSessions({
376
376
  cursor: this.sessionsState.get().cursor
377
377
  });
378
- if (a) {
379
- const t = [...this.sessionsState.get().data, ...a.items].filter(
380
- (e, o, l) => o === l.findIndex((r) => e.id === r.id)
378
+ if (o) {
379
+ const t = [...this.sessionsState.get().data, ...o.items].filter(
380
+ (s, i, c) => i === c.findIndex((r) => s.id === r.id)
381
381
  );
382
382
  this.sessionsState.setPartial({
383
383
  data: t,
384
- cursor: a.next || void 0,
385
- isLastPage: a.next === null
384
+ cursor: o.next || void 0,
385
+ isLastPage: o.next === null
386
386
  });
387
387
  }
388
- }, this.getSessions = async ({ cursor: a }) => {
389
- var t, e;
388
+ }, this.getSessions = async ({ cursor: o }) => {
389
+ var t, s;
390
390
  if (!((t = this.contactCtx.state.get().contact) != null && t.token)) return { data: null };
391
- const s = (e = this.contactCtx.state.get().contact) == null ? void 0 : e.externalId;
391
+ const e = (s = this.contactCtx.state.get().contact) == null ? void 0 : s.externalId;
392
392
  return await this.api.getSessions({
393
- cursor: a,
394
- filters: s ? {
395
- external_id: s
393
+ cursor: o,
394
+ filters: e ? {
395
+ external_id: e
396
396
  } : {}
397
397
  });
398
- }, this.setSessions = (a) => {
399
- const s = [...a, ...this.sessionsState.get().data].filter(
400
- (t, e, o) => e === o.findIndex((l) => t.id === l.id)
398
+ }, this.setSessions = (o) => {
399
+ const e = [...o, ...this.sessionsState.get().data].filter(
400
+ (t, s, i) => s === i.findIndex((c) => t.id === c.id)
401
401
  );
402
- this.sessionsState.setPartial({ data: s });
402
+ this.sessionsState.setPartial({ data: e });
403
403
  }, this.refreshSessions = async () => {
404
- const { data: a } = await this.getSessions({ cursor: void 0 });
405
- a && this.setSessions(a.items);
404
+ const { data: o } = await this.getSessions({ cursor: void 0 });
405
+ o && this.setSessions(o.items);
406
406
  }, this.resolveSession = async () => {
407
- const a = this.sessionState.get().session;
408
- if (!a || !a.isOpened) return;
409
- const { data: s } = await this.api.resolveSession({
410
- session_id: a.id
407
+ const o = this.sessionState.get().session;
408
+ if (!o || !o.isOpened) return;
409
+ const { data: e } = await this.api.resolveSession({
410
+ session_id: o.id
411
411
  });
412
- s && this.sessionState.setPartial({ session: s });
413
- }, this.api = n, this.contactCtx = i, this.sessionsPollingIntervalSeconds = h, this.registerSessionsRefresherWrapper();
412
+ e && this.sessionState.setPartial({ session: e });
413
+ }, this.api = n, this.contactCtx = a, this.sessionsPollingIntervalSeconds = g, this.registerSessionsRefresherWrapper();
414
414
  }
415
415
  }
416
416
  class $ {
417
417
  constructor({
418
418
  config: n,
419
- api: i,
420
- sessionCtx: h,
421
- contactCtx: a
419
+ api: a,
420
+ sessionCtx: g,
421
+ contactCtx: o
422
422
  }) {
423
423
  this.state = new P({
424
424
  messages: [],
@@ -427,15 +427,15 @@ class $ {
427
427
  isInitialFetchLoading: !1
428
428
  }), this.sendMessageAbortController = new AbortController(), this.reset = () => {
429
429
  this.sendMessageAbortController.abort("Resetting chat"), this.state.reset();
430
- }, this.sendMessage = async (s) => {
431
- var r, g, c, C, x, p, b, v;
432
- if (!s.content.trim() && (!s.attachments || s.attachments.length === 0)) {
430
+ }, this.sendMessage = async (e) => {
431
+ var r, h, l, f, x, p, b, v;
432
+ if (!e.content.trim() && (!e.attachments || e.attachments.length === 0)) {
433
433
  console.warn("Cannot send an empty message of no content or attachments");
434
434
  return;
435
435
  }
436
- const t = this.state.get().isSendingMessage, e = ((r = this.sessionCtx.sessionState.get().session) == null ? void 0 : r.assignee.kind) === "ai", o = this.state.get().messages, l = o.length > 0 ? o[o.length - 1] : void 0;
437
- if (e && t || // If last message is from user, then bot response did not arrive yet
438
- e && (l == null ? void 0 : l.type) === "FROM_USER") {
436
+ const t = this.state.get().isSendingMessage, s = ((r = this.sessionCtx.sessionState.get().session) == null ? void 0 : r.assignee.kind) === "ai", i = this.state.get().messages, c = i.length > 0 ? i[i.length - 1] : void 0;
437
+ if (s && t || // If last message is from user, then bot response did not arrive yet
438
+ s && (c == null ? void 0 : c.type) === "FROM_USER") {
439
439
  console.warn("Cannot send messages while awaiting AI response");
440
440
  return;
441
441
  }
@@ -443,21 +443,21 @@ class $ {
443
443
  try {
444
444
  this.state.setPartial({ isSendingMessage: !0 });
445
445
  const u = this.toUserMessage(
446
- s.content.trim(),
447
- s.attachments || void 0
448
- ), m = this.state.get().messages;
446
+ e.content.trim(),
447
+ e.attachments || void 0
448
+ ), S = this.state.get().messages;
449
449
  if (this.state.setPartial({
450
- messages: [...m, u]
451
- }), !((g = this.sessionCtx.sessionState.get().session) != null && g.id)) {
450
+ messages: [...S, u]
451
+ }), !((h = this.sessionCtx.sessionState.get().session) != null && h.id)) {
452
452
  if (!await this.sessionCtx.createSession()) {
453
453
  console.error("Failed to create session");
454
454
  return;
455
455
  }
456
456
  this.sessionCtx.refreshSessions();
457
457
  }
458
- const k = (c = this.sessionCtx.sessionState.get().session) == null ? void 0 : c.id;
458
+ const k = (l = this.sessionCtx.sessionState.get().session) == null ? void 0 : l.id;
459
459
  if (!k) return;
460
- const { data: f } = await this.api.sendMessage(
460
+ const { data: C } = await this.api.sendMessage(
461
461
  {
462
462
  uuid: u.id,
463
463
  bot_token: this.config.token,
@@ -466,36 +466,36 @@ class $ {
466
466
  body_properties: this.config.bodyProperties,
467
467
  session_id: k,
468
468
  content: u.content,
469
- attachments: s.attachments,
469
+ attachments: e.attachments,
470
470
  clientContext: this.config.context,
471
471
  custom_data: {
472
472
  ...this.config.messageCustomData || {},
473
- ...s.customData || {}
473
+ ...e.customData || {}
474
474
  },
475
475
  language: this.config.language
476
476
  },
477
477
  this.sendMessageAbortController.signal
478
478
  );
479
- if (f != null && f.success) {
480
- const w = this.toBotMessage(f);
479
+ if (C != null && C.success) {
480
+ const w = this.toBotMessage(C);
481
481
  if (w) {
482
482
  const I = this.state.get().messages;
483
483
  if (!!I.some(
484
484
  (R) => R.id === w.id
485
485
  )) {
486
486
  this.state.setPartial({
487
- lastAIResMightSolveUserIssue: ((C = f.autopilotResponse) == null ? void 0 : C.mightSolveUserIssue) || ((x = f.uiResponse) == null ? void 0 : x.mightSolveUserIssue)
487
+ lastAIResMightSolveUserIssue: ((f = C.autopilotResponse) == null ? void 0 : f.mightSolveUserIssue) || ((x = C.uiResponse) == null ? void 0 : x.mightSolveUserIssue)
488
488
  });
489
489
  return;
490
490
  }
491
491
  this.state.setPartial({
492
492
  messages: [...I, w],
493
- lastAIResMightSolveUserIssue: ((p = f.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((b = f.uiResponse) == null ? void 0 : b.mightSolveUserIssue)
493
+ lastAIResMightSolveUserIssue: ((p = C.autopilotResponse) == null ? void 0 : p.mightSolveUserIssue) || ((b = C.uiResponse) == null ? void 0 : b.mightSolveUserIssue)
494
494
  });
495
495
  }
496
496
  } else {
497
497
  const w = this.toBotErrorMessage(
498
- ((v = f == null ? void 0 : f.error) == null ? void 0 : v.message) || "Unknown error occurred"
498
+ ((v = C == null ? void 0 : C.error) == null ? void 0 : v.message) || "Unknown error occurred"
499
499
  ), I = this.state.get().messages;
500
500
  this.state.setPartial({
501
501
  messages: [...I, w]
@@ -506,27 +506,27 @@ class $ {
506
506
  } finally {
507
507
  this.state.setPartial({ isSendingMessage: !1 });
508
508
  }
509
- }, this.toUserMessage = (s, t) => {
510
- const e = (() => {
511
- const o = this.contactCtx.state.get().extraCollectedData;
512
- return this.state.get().messages.length === 0 && o && Object.keys(o).length > 0 ? `${Object.entries(o).filter(([r, g]) => !!g).map(([r, g]) => `${r}: ${g}`).join(`
509
+ }, this.toUserMessage = (e, t) => {
510
+ const s = (() => {
511
+ const i = this.contactCtx.state.get().extraCollectedData;
512
+ return this.state.get().messages.length === 0 && i && Object.keys(i).length > 0 ? `${Object.entries(i).filter(([r, h]) => !!h).map(([r, h]) => `${r}: ${h}`).join(`
513
513
  `)}
514
514
 
515
- ${s}` : s;
515
+ ${e}` : e;
516
516
  })();
517
517
  return {
518
518
  id: y(),
519
519
  type: "FROM_USER",
520
- content: e,
520
+ content: s,
521
521
  deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
522
522
  attachments: t,
523
523
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
524
524
  };
525
- }, this.toBotMessage = (s) => {
525
+ }, this.toBotMessage = (e) => {
526
526
  var t;
527
- return s.success && s.autopilotResponse ? {
527
+ return e.success && e.autopilotResponse ? {
528
528
  type: "FROM_BOT",
529
- id: s.autopilotResponse.id || y(),
529
+ id: e.autopilotResponse.id || y(),
530
530
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
531
531
  component: "bot_message",
532
532
  agent: this.config.bot ? {
@@ -536,54 +536,64 @@ ${s}` : s;
536
536
  id: null
537
537
  } : void 0,
538
538
  data: {
539
- message: s.autopilotResponse.value.content,
540
- action: (t = s.uiResponse) != null && t.value.name ? {
541
- name: s.uiResponse.value.name,
542
- data: s.uiResponse.value.request_response
539
+ message: e.autopilotResponse.value.content,
540
+ action: (t = e.uiResponse) != null && t.value.name ? {
541
+ name: e.uiResponse.value.name,
542
+ data: e.uiResponse.value.request_response
543
543
  } : void 0
544
544
  }
545
545
  } : null;
546
- }, this.toBotErrorMessage = (s) => ({
546
+ }, this.toBotErrorMessage = (e) => ({
547
547
  type: "FROM_BOT",
548
548
  id: y(),
549
549
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
550
550
  component: "TEXT",
551
551
  data: {
552
- message: s,
552
+ message: e,
553
553
  variant: "error",
554
554
  action: void 0
555
555
  }
556
- }), this.config = n, this.api = i, this.sessionCtx = h, this.contactCtx = a;
556
+ }), this.config = n, this.api = a, this.sessionCtx = g, this.contactCtx = o;
557
557
  }
558
558
  }
559
559
  class N {
560
560
  constructor({
561
561
  config: n,
562
- contactCtx: i,
563
- sessionCtx: h,
564
- resetChat: a
562
+ contactCtx: a,
563
+ sessionCtx: g,
564
+ resetChat: o
565
565
  }) {
566
+ var e;
566
567
  this.registerRoutingListener = () => {
567
- this.contactCtx.state.subscribe(({ contact: s }) => {
568
- s != null && s.token && this.state.get().screen === "welcome" && this.state.setPartial({ screen: "sessions" });
568
+ this.contactCtx.state.subscribe(({ contact: t }) => {
569
+ var s;
570
+ t != null && t.token && this.state.get().screen === "welcome" && this.state.setPartial({
571
+ screen: (s = this.config.router) != null && s.chatScreenOnly ? "chat" : "sessions"
572
+ });
569
573
  }), this.sessionCtx.sessionsState.subscribe(
570
- ({ isInitialFetchLoading: s, data: t }) => {
571
- var e;
572
- t.length || ((e = this.config.router) == null ? void 0 : e.goToChatIfNoSessions) !== !1 && !s && this.state.get().screen !== "chat" && this.toChatScreen();
574
+ ({ isInitialFetchLoading: t, data: s }) => {
575
+ var i, c, r, h;
576
+ if ((i = this.config.router) != null && i.chatScreenOnly && // Do not route to a chat if we are currently inside one already
577
+ // This also applies to newly created sessions; the new session will be in `sessionState` before it is refreshed and included in `sessionsState`
578
+ !((c = this.sessionCtx.sessionState.get().session) != null && c.id)) {
579
+ const l = (r = s.find((f) => f.isOpened)) == null ? void 0 : r.id;
580
+ return l ? this.toChatScreen(l) : void 0;
581
+ }
582
+ s.length || ((h = this.config.router) == null ? void 0 : h.goToChatIfNoSessions) !== !1 && !t && this.state.get().screen !== "chat" && this.toChatScreen();
573
583
  }
574
584
  );
575
585
  }, this.toSessionsScreen = () => {
576
586
  this.resetChat(), this.state.setPartial({ screen: "sessions" });
577
- }, this.toChatScreen = (s) => {
578
- if (this.resetChat(), s) {
579
- const t = this.sessionCtx.sessionsState.get().data.find((e) => e.id === s);
580
- if (!t) return;
581
- this.sessionCtx.sessionState.setPartial({ session: t });
587
+ }, this.toChatScreen = (t) => {
588
+ if (this.resetChat(), t) {
589
+ const s = this.sessionCtx.sessionsState.get().data.find((i) => i.id === t);
590
+ if (!s) return;
591
+ this.sessionCtx.sessionState.setPartial({ session: s });
582
592
  }
583
593
  this.state.setPartial({ screen: "chat" });
584
- }, this.state = new P({
585
- screen: i.shouldCollectData() ? "welcome" : "sessions"
586
- }), this.config = n, this.contactCtx = i, this.sessionCtx = h, this.resetChat = a, this.registerRoutingListener();
594
+ }, this.config = n, this.contactCtx = a, this.sessionCtx = g, this.resetChat = o, this.state = new P({
595
+ screen: this.contactCtx.shouldCollectData() ? "welcome" : (e = this.config.router) != null && e.chatScreenOnly ? "chat" : "sessions"
596
+ }), this.registerRoutingListener();
587
597
  }
588
598
  }
589
599
  class H {
@@ -591,32 +601,32 @@ class H {
591
601
  this.KEYS = {
592
602
  contactToken: "opencx__widget__contactToken",
593
603
  externalContactId: "opencx__widget__externalContactId"
594
- }, this.setContactToken = async (i) => {
595
- await this.storage.set(this.KEYS.contactToken, i);
596
- }, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken), this.setExternalContactId = async (i) => {
597
- await this.storage.set(this.KEYS.externalContactId, i);
604
+ }, this.setContactToken = async (a) => {
605
+ await this.storage.set(this.KEYS.contactToken, a);
606
+ }, this.getContactToken = async () => this.storage.get(this.KEYS.contactToken), this.setExternalContactId = async (a) => {
607
+ await this.storage.set(this.KEYS.externalContactId, a);
598
608
  }, this.getExternalContactId = async () => this.storage.get(this.KEYS.externalContactId), this.storage = n;
599
609
  }
600
610
  }
601
- const S = class S {
611
+ const m = class m {
602
612
  constructor({
603
613
  config: n,
604
- storage: i
614
+ storage: a
605
615
  }) {
606
616
  if (this.resetChat = () => {
607
617
  this.sessionCtx.reset(), this.messageCtx.reset();
608
- }, !S.pollingIntervalsSeconds)
618
+ }, !m.pollingIntervalsSeconds)
609
619
  throw Error(
610
620
  "Widget polling values are not defined, did you call WidgetCtx.initialize()"
611
621
  );
612
- this.config = n, this.api = new T({ config: n }), this.storageCtx = i ? new H({ storage: i }) : void 0, this.contactCtx = new B({
622
+ this.config = n, this.api = new T({ config: n }), this.storageCtx = a ? new H({ storage: a }) : void 0, this.contactCtx = new B({
613
623
  api: this.api,
614
624
  config: this.config,
615
625
  storageCtx: this.storageCtx
616
626
  }), this.sessionCtx = new q({
617
627
  api: this.api,
618
628
  contactCtx: this.contactCtx,
619
- sessionsPollingIntervalSeconds: S.pollingIntervalsSeconds.sessions
629
+ sessionsPollingIntervalSeconds: m.pollingIntervalsSeconds.sessions
620
630
  }), this.messageCtx = new $({
621
631
  config: this.config,
622
632
  api: this.api,
@@ -627,7 +637,7 @@ const S = class S {
627
637
  config: this.config,
628
638
  sessionCtx: this.sessionCtx,
629
639
  messageCtx: this.messageCtx,
630
- sessionPollingIntervalSeconds: S.pollingIntervalsSeconds.session
640
+ sessionPollingIntervalSeconds: m.pollingIntervalsSeconds.session
631
641
  }), this.routerCtx = new N({
632
642
  config: this.config,
633
643
  contactCtx: this.contactCtx,
@@ -636,25 +646,25 @@ const S = class S {
636
646
  });
637
647
  }
638
648
  };
639
- S.pollingIntervalsSeconds = null, S.initialize = async ({
649
+ m.pollingIntervalsSeconds = null, m.initialize = async ({
640
650
  config: n,
641
- storage: i
651
+ storage: a
642
652
  }) => {
643
- var a, s;
644
- const h = await new T({
653
+ var o, e;
654
+ const g = await new T({
645
655
  config: n
646
656
  }).getExternalWidgetConfig();
647
- return S.pollingIntervalsSeconds = {
648
- session: ((a = h.data) == null ? void 0 : a.sessionPollingIntervalSeconds) || 10,
649
- sessions: ((s = h.data) == null ? void 0 : s.sessionsPollingIntervalSeconds) || 60
650
- }, new S({
657
+ return m.pollingIntervalsSeconds = {
658
+ session: ((o = g.data) == null ? void 0 : o.sessionPollingIntervalSeconds) || 10,
659
+ sessions: ((e = g.data) == null ? void 0 : e.sessionsPollingIntervalSeconds) || 60
660
+ }, new m({
651
661
  config: n,
652
- storage: i
662
+ storage: a
653
663
  });
654
664
  };
655
- let U = S;
665
+ let U = m;
656
666
  export {
657
667
  P,
658
668
  U as W
659
669
  };
660
- //# sourceMappingURL=widget.ctx-r2Nzp00O.js.map
670
+ //# sourceMappingURL=widget.ctx-0z1bMkkT.js.map