@opencx/widget 2.6.3 → 3.0.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.
Files changed (154) hide show
  1. package/dist/basic.cjs +89 -89
  2. package/dist/basic.cjs.map +1 -1
  3. package/dist/basic.d.ts +1 -1
  4. package/dist/basic.js +34088 -34127
  5. package/dist/basic.js.map +1 -1
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +3 -22
  9. package/dist/react.cjs +1 -1
  10. package/dist/react.cjs.map +1 -1
  11. package/dist/react.d.ts +1 -1
  12. package/dist/react.js +20 -10
  13. package/dist/react.js.map +1 -1
  14. package/dist/src/designs/react/basic/index.d.ts +10 -0
  15. package/dist/src/designs/{basic → react/basic}/utils/group-messages-by-type.d.ts +1 -1
  16. package/dist/src/{@components → designs/react/components}/BotOrAgentMessage.d.ts +1 -1
  17. package/dist/src/{@components → designs/react/components}/BotOrAgentMessageGroup.d.ts +2 -2
  18. package/dist/src/designs/react/components/BotOrAgentMessageWrapper.d.ts +7 -0
  19. package/dist/src/{@components → designs/react/components}/Fallback.component.d.ts +2 -2
  20. package/dist/src/{components → designs/react/components}/RenderFile.d.ts +2 -2
  21. package/dist/src/{components/keyboard.d.ts → designs/react/components/SuggestedReplies.d.ts} +2 -2
  22. package/dist/src/{@components → designs/react/components}/Text.component.d.ts +1 -1
  23. package/dist/src/{components → designs/react/components}/UserMessageGroup.d.ts +1 -1
  24. package/dist/src/{components → designs/react/components}/VoteButtons.d.ts +1 -1
  25. package/dist/src/{components → designs/react/components/lib}/button.d.ts +1 -1
  26. package/dist/src/{components → designs/react/components/lib}/dialog.d.ts +7 -7
  27. package/dist/src/{components → designs/react/components/lib}/dropdown-menu.d.ts +2 -2
  28. package/dist/src/designs/{constants.d.ts → react/constants.d.ts} +2 -4
  29. package/dist/src/designs/react/hooks/useLocale.d.ts +6 -0
  30. package/dist/src/{index.d.ts → designs/react/index.d.ts} +2 -2
  31. package/dist/src/designs/translation/ar.locale.d.ts +2 -0
  32. package/dist/src/designs/translation/de.locale.d.ts +2 -0
  33. package/dist/src/designs/translation/en.locale.d.ts +2 -0
  34. package/dist/src/designs/translation/fr.locale.d.ts +2 -0
  35. package/dist/src/designs/translation/index.d.ts +14 -0
  36. package/dist/src/designs/translation/nl.locale.d.ts +2 -0
  37. package/dist/src/designs/translation/pt.locale.d.ts +2 -0
  38. package/dist/src/designs/translation/translation.types.d.ts +4 -0
  39. package/dist/{core/client → src/headless/core}/api.d.ts +49 -103
  40. package/dist/src/headless/core/context/contact.d.ts +24 -0
  41. package/dist/src/headless/core/context/message.d.ts +42 -0
  42. package/dist/src/headless/core/context/session.d.ts +74 -0
  43. package/dist/src/headless/core/context/widget.d.ts +16 -0
  44. package/dist/src/headless/core/index.d.ts +10 -0
  45. package/dist/src/headless/core/types/WidgetConfig.d.ts +34 -0
  46. package/dist/src/headless/core/types/agent-or-bot.d.ts +6 -0
  47. package/dist/src/headless/core/types/helpers.d.ts +4 -0
  48. package/dist/{core → src/headless/core}/types/messages.d.ts +11 -31
  49. package/dist/src/headless/core/types/schemas.d.ts +9 -0
  50. package/dist/src/headless/core/utils/Poller.d.ts +12 -0
  51. package/dist/{core/types/pub-sub.d.ts → src/headless/core/utils/PubSub.d.ts} +6 -9
  52. package/dist/{react-web/core-integration/components.d.ts → src/headless/react/ComponentRegistry.d.ts} +1 -1
  53. package/dist/src/headless/react/WidgetProvider.d.ts +17 -0
  54. package/dist/src/headless/react/hooks/useConfig.d.ts +1 -0
  55. package/dist/src/headless/react/hooks/useContact.d.ts +10 -0
  56. package/dist/src/headless/react/hooks/useIsAwaitingBotReply.d.ts +3 -0
  57. package/dist/src/headless/react/hooks/useMessages.d.ts +8 -0
  58. package/dist/{react-web/core-integration → src/headless/react}/hooks/usePreludeData.d.ts +2 -2
  59. package/dist/{react-web/core-integration → src/headless/react}/hooks/usePubsub.d.ts +1 -1
  60. package/dist/src/headless/react/hooks/useSession.d.ts +6 -0
  61. package/dist/{react-web/core-integration → src/headless/react}/hooks/useVote.d.ts +2 -2
  62. package/dist/src/headless/react/index.d.ts +10 -0
  63. package/dist/{react-web → src/headless/react}/types/components.d.ts +6 -5
  64. package/dist/style.css +1 -1
  65. package/dist/useUploadFiles-CEd5c5iE.js +1336 -0
  66. package/dist/useUploadFiles-CEd5c5iE.js.map +1 -0
  67. package/dist/useUploadFiles-CNkahO3s.cjs +18 -0
  68. package/dist/useUploadFiles-CNkahO3s.cjs.map +1 -0
  69. package/dist/widget-BeNOCqB5.js +517 -0
  70. package/dist/widget-BeNOCqB5.js.map +1 -0
  71. package/dist/widget-DlAUwHzU.cjs +2 -0
  72. package/dist/widget-DlAUwHzU.cjs.map +1 -0
  73. package/dist-embed/script.js +137 -157
  74. package/dist-embed/script.js.map +1 -1
  75. package/package.json +9 -4
  76. package/dist/api-CAm3rFZk.js +0 -1070
  77. package/dist/api-CAm3rFZk.js.map +0 -1
  78. package/dist/api-oIDR-KZx.cjs +0 -2
  79. package/dist/api-oIDR-KZx.cjs.map +0 -1
  80. package/dist/core/client/chat.d.ts +0 -101
  81. package/dist/core/client/config.d.ts +0 -43
  82. package/dist/core/client/contact.d.ts +0 -29
  83. package/dist/core/client/index.d.ts +0 -4
  84. package/dist/core/errors/index.d.ts +0 -27
  85. package/dist/core/index.d.ts +0 -9
  86. package/dist/core/platform/audio.d.ts +0 -38
  87. package/dist/core/platform/index.d.ts +0 -15
  88. package/dist/core/platform/logger.d.ts +0 -14
  89. package/dist/core/platform/storage.d.ts +0 -52
  90. package/dist/core/tests/platform/logger.test.d.ts +0 -1
  91. package/dist/core/tests/platform/storage.test.d.ts +0 -1
  92. package/dist/core/tests/test-utils.d.ts +0 -2
  93. package/dist/core/tests/types/pub-sub.test.d.ts +0 -1
  94. package/dist/core/types/contact.d.ts +0 -2
  95. package/dist/core/types/helpers.d.ts +0 -20
  96. package/dist/core/types/index.d.ts +0 -52
  97. package/dist/core/types/prelude.d.ts +0 -14
  98. package/dist/core/types/schemas-v2.d.ts +0 -10
  99. package/dist/index-CSptf_Dw.cjs +0 -18
  100. package/dist/index-CSptf_Dw.cjs.map +0 -1
  101. package/dist/index-cUkS-tdv.js +0 -1232
  102. package/dist/index-cUkS-tdv.js.map +0 -1
  103. package/dist/react-web/core-integration/ChatProvider.d.ts +0 -107
  104. package/dist/react-web/core-integration/hooks/useChatMessages.d.ts +0 -70
  105. package/dist/react-web/core-integration/hooks/useChatSession.d.ts +0 -83
  106. package/dist/react-web/core-integration/hooks/useConfig.d.ts +0 -1
  107. package/dist/react-web/core-integration/hooks/useContact.d.ts +0 -17
  108. package/dist/react-web/core-integration/hooks/useLocale.d.ts +0 -4
  109. package/dist/react-web/core-integration/hooks.d.ts +0 -9
  110. package/dist/react-web/core-integration/index.d.ts +0 -2
  111. package/dist/react-web/core-integration/locales/ar.locale.d.ts +0 -2
  112. package/dist/react-web/core-integration/locales/de.locale.d.ts +0 -2
  113. package/dist/react-web/core-integration/locales/en.locale.d.ts +0 -55
  114. package/dist/react-web/core-integration/locales/fr.locale.d.ts +0 -2
  115. package/dist/react-web/core-integration/locales/helper.d.ts +0 -65
  116. package/dist/react-web/core-integration/locales/index.d.ts +0 -3
  117. package/dist/react-web/core-integration/locales/nl.locale.d.ts +0 -2
  118. package/dist/react-web/core-integration/locales/pt.locale.d.ts +0 -2
  119. package/dist/react-web/hooks/useScrollTo.d.ts +0 -2
  120. package/dist/react-web/index.d.ts +0 -4
  121. package/dist/react-web/types/index.d.ts +0 -8
  122. package/dist/react-web/types/options.d.ts +0 -17
  123. package/dist/src/@components/BotOrAgentMessageWrapper.d.ts +0 -9
  124. package/dist/src/@components/ChatEvent.component.d.ts +0 -9
  125. package/dist/src/@components/index.d.ts +0 -6
  126. package/dist/src/designs/basic/index.d.ts +0 -8
  127. /package/dist/src/designs/{basic → react/basic}/WidgetPopoverTrigger.d.ts +0 -0
  128. /package/dist/src/designs/{basic → react/basic}/screens/chat-screen/ChatFooter.d.ts +0 -0
  129. /package/dist/src/designs/{basic → react/basic}/screens/chat-screen/ChatHeader.d.ts +0 -0
  130. /package/dist/src/designs/{basic → react/basic}/screens/chat-screen/ChatMain.d.ts +0 -0
  131. /package/dist/src/designs/{basic → react/basic}/screens/chat-screen/ChatScreen.d.ts +0 -0
  132. /package/dist/src/designs/{basic → react/basic}/screens/root-screen.d.ts +0 -0
  133. /package/dist/src/designs/{basic → react/basic}/screens/welcome-screen/WelcomeScreen.d.ts +0 -0
  134. /package/dist/{core/tests/client/integration-perisitance.test.d.ts → src/designs/react/basic/widget-interaction-tests/widget.test.d.ts} +0 -0
  135. /package/dist/src/{@components → designs/react/components}/Loading.component.d.ts +0 -0
  136. /package/dist/src/{@components → designs/react/components}/OpenLogoSvg.d.ts +0 -0
  137. /package/dist/src/{components → designs/react/components/lib}/MotionDiv.d.ts +0 -0
  138. /package/dist/src/{components → designs/react/components/lib}/avatar.d.ts +0 -0
  139. /package/dist/src/{components → designs/react/components/lib}/input.d.ts +0 -0
  140. /package/dist/src/{components → designs/react/components/lib}/popover.d.ts +0 -0
  141. /package/dist/src/{components → designs/react/components/lib}/skeleton.d.ts +0 -0
  142. /package/dist/src/{components → designs/react/components/lib}/switch.d.ts +0 -0
  143. /package/dist/src/{components → designs/react/components/lib}/tooltip.d.ts +0 -0
  144. /package/dist/src/{utils.d.ts → designs/react/components/lib/utils/cn.d.ts} +0 -0
  145. /package/dist/src/{components → designs/react/components/lib}/wobble.d.ts +0 -0
  146. /package/dist/src/{@components → designs/react/components}/markdown.d.ts +0 -0
  147. /package/dist/{react-web → src/designs/react}/hooks/useWidgetContentHeight.d.ts +0 -0
  148. /package/dist/src/{render.d.ts → designs/react/render.d.ts} +0 -0
  149. /package/dist/{core → src/headless/core}/sdk/index.d.ts +0 -0
  150. /package/dist/{core/tests/client/integration.test.d.ts → src/headless/core/utils/PubSub.test.d.ts} +0 -0
  151. /package/dist/{core/utils/genUuid.d.ts → src/headless/core/utils/uuid.d.ts} +0 -0
  152. /package/dist/{react-web/core-integration → src/headless/react}/hooks/useUploadFiles.d.ts +0 -0
  153. /package/dist/{react-web → src/headless/react}/utils/create-safe-context.d.ts +0 -0
  154. /package/dist/{core/tests/platform/audio.test.d.ts → vitest/setup.d.ts} +0 -0
@@ -0,0 +1,517 @@
1
+ var x = Object.defineProperty;
2
+ var A = (a) => {
3
+ throw TypeError(a);
4
+ };
5
+ var O = (a, t, e) => t in a ? x(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
6
+ var s = (a, t, e) => O(a, typeof t != "symbol" ? t + "" : t, e), U = (a, t, e) => t.has(a) || A("Cannot " + e);
7
+ var c = (a, t, e) => (U(a, t, "read from private field"), e ? e.call(a) : t.get(a)), S = (a, t, e) => t.has(a) ? A("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(a) : t.set(a, e), p = (a, t, e, i) => (U(a, t, "write to private field"), i ? i.call(a, e) : t.set(a, e), e);
8
+ import R from "axios";
9
+ import E from "openapi-fetch";
10
+ import F from "lodash.isequal";
11
+ import { v4 as L } from "uuid";
12
+ const D = (a) => {
13
+ console.log(a.error);
14
+ }, I = (a) => {
15
+ const t = E({
16
+ baseUrl: a.baseUrl
17
+ }), e = {
18
+ onRequest: a.onRequest,
19
+ onResponse: a.onResponse,
20
+ onError: a.onError || D
21
+ };
22
+ return t.use(e), t;
23
+ };
24
+ class B {
25
+ constructor({
26
+ config: t
27
+ }) {
28
+ s(this, "client");
29
+ s(this, "uploadFileClient");
30
+ s(this, "config");
31
+ s(this, "constructClientOptions", (t) => {
32
+ const e = this.config.apiUrl || "https://api.open.cx", i = {
33
+ "X-Bot-Token": this.config.token,
34
+ "Content-Type": "application/json",
35
+ Accept: "application/json",
36
+ Authorization: t ? `Bearer ${t}` : void 0
37
+ };
38
+ return { baseUrl: e, headers: i };
39
+ });
40
+ s(this, "createOpenAPIClient", ({
41
+ baseUrl: t,
42
+ headers: e
43
+ }) => I({
44
+ baseUrl: t,
45
+ onRequest: ({ request: i }) => {
46
+ Object.entries(e).forEach(([n, r]) => {
47
+ r && i.headers.set(n, r);
48
+ });
49
+ }
50
+ }));
51
+ s(this, "createAxiosUploadClient", ({
52
+ baseUrl: t,
53
+ headers: e
54
+ }) => R.create({
55
+ baseURL: `${t}/backend/widget/v2/upload`,
56
+ headers: e
57
+ }));
58
+ s(this, "setAuthToken", (t) => {
59
+ const { baseUrl: e, headers: i } = this.constructClientOptions(t);
60
+ this.client = this.createOpenAPIClient({ baseUrl: e, headers: i }), this.uploadFileClient = this.createAxiosUploadClient({ baseUrl: e, headers: i });
61
+ });
62
+ s(this, "widgetPrelude", async () => await this.client.GET("/backend/widget/v2/prelude", {
63
+ params: { header: { "X-Bot-Token": this.config.token } }
64
+ }));
65
+ s(this, "sendMessage", async (t, e) => await this.client.POST("/backend/widget/v2/chat/send", {
66
+ body: t,
67
+ signal: e
68
+ }));
69
+ s(this, "getSessionHistory", async ({
70
+ sessionId: t,
71
+ lastMessageTimestamp: e,
72
+ abortSignal: i
73
+ }) => {
74
+ const n = e ? { lastMessageTimestamp: e } : void 0;
75
+ return await this.client.GET(
76
+ "/backend/widget/v2/session/history/{sessionId}",
77
+ { params: { path: { sessionId: t }, query: n }, signal: i }
78
+ );
79
+ });
80
+ s(this, "createUnverifiedContact", async (t) => await this.client.POST(
81
+ "/backend/widget/v2/contact/create-unverified",
82
+ {
83
+ params: { header: { "x-bot-token": this.config.token } },
84
+ body: t
85
+ }
86
+ ));
87
+ s(this, "createSession", async () => await this.client.POST("/backend/widget/v2/create-session"));
88
+ s(this, "getSession", async ({
89
+ sessionId: t,
90
+ abortSignal: e
91
+ }) => await this.client.GET("/backend/widget/v2/session/{sessionId}", {
92
+ params: { path: { sessionId: t } },
93
+ signal: e
94
+ }));
95
+ s(this, "uploadFile", async (t, e = {}) => {
96
+ const i = new FormData();
97
+ i.append("file", t.file);
98
+ const { data: n } = await this.uploadFileClient.post("", i, {
99
+ headers: {
100
+ "Content-Type": "multipart/form-data"
101
+ },
102
+ ...e
103
+ });
104
+ return n;
105
+ });
106
+ s(this, "vote", async (t) => await this.client.POST("/backend/widget/v2/chat/vote", { body: t }));
107
+ this.config = t;
108
+ const { baseUrl: e, headers: i } = this.constructClientOptions(
109
+ t.contactToken
110
+ );
111
+ this.client = this.createOpenAPIClient({ baseUrl: e, headers: i }), this.uploadFileClient = this.createAxiosUploadClient({ baseUrl: e, headers: i });
112
+ }
113
+ }
114
+ var l, g;
115
+ class P {
116
+ constructor(t) {
117
+ s(this, "subscribers", /* @__PURE__ */ new Set());
118
+ S(this, l);
119
+ s(this, "initialState");
120
+ S(this, g);
121
+ s(this, "lifecycleListeners", /* @__PURE__ */ new Map());
122
+ s(this, "emitLifecycle", (t, e) => {
123
+ const i = this.lifecycleListeners.get(t);
124
+ if (i) {
125
+ const n = {
126
+ type: t,
127
+ timestamp: Date.now(),
128
+ data: e
129
+ };
130
+ i.forEach((r) => {
131
+ try {
132
+ r(n);
133
+ } catch {
134
+ }
135
+ });
136
+ }
137
+ });
138
+ s(this, "notifySubscribers", (t) => {
139
+ Array.from(this.subscribers).forEach((i) => {
140
+ try {
141
+ i(t);
142
+ } catch (n) {
143
+ this.emitLifecycle("error", { error: n });
144
+ }
145
+ });
146
+ });
147
+ /**
148
+ * Subscribe to state changes
149
+ * @param callback Function to call when state changes
150
+ * @returns Unsubscribe function
151
+ */
152
+ s(this, "subscribe", (t) => (this.subscribers.add(t), () => {
153
+ this.subscribers.delete(t);
154
+ }));
155
+ s(this, "onLifecycle", (t, e) => {
156
+ this.lifecycleListeners.has(t) || this.lifecycleListeners.set(t, /* @__PURE__ */ new Set());
157
+ const i = this.lifecycleListeners.get(t);
158
+ return i.add(e), () => {
159
+ i.delete(e), i.size === 0 && this.lifecycleListeners.delete(t);
160
+ };
161
+ });
162
+ /** Get the current state */
163
+ s(this, "get", () => c(this, l));
164
+ // TODO make this provide prev state
165
+ /**
166
+ * Set the state and notify subscribers if the state changes
167
+ * @param newState The new state to set
168
+ */
169
+ s(this, "set", (t) => {
170
+ this.emitLifecycle("beforeUpdate", {
171
+ previousState: c(this, l),
172
+ nextState: t
173
+ }), F(c(this, l), t) || (p(this, l, t), p(this, g, Date.now()), this.emitLifecycle("stateChange", { state: t }), this.notifySubscribers(t)), this.emitLifecycle("afterUpdate", { state: t });
174
+ });
175
+ // TODO make this provide prev state
176
+ s(this, "setPartial", (t) => {
177
+ if (t == null) return;
178
+ const e = { ...c(this, l), ...t };
179
+ this.set(e);
180
+ });
181
+ /**
182
+ * Clear all subscriptions
183
+ */
184
+ s(this, "clear", () => {
185
+ this.emitLifecycle(
186
+ "destroy"
187
+ /* DESTROY */
188
+ ), this.subscribers = /* @__PURE__ */ new Set(), this.lifecycleListeners = /* @__PURE__ */ new Map();
189
+ });
190
+ s(this, "reset", () => {
191
+ this.set(this.initialState);
192
+ });
193
+ s(this, "lastUpdated", () => c(this, g));
194
+ p(this, l, t), this.initialState = t, p(this, g, Date.now()), this.emitLifecycle("init", { initialState: c(this, l) });
195
+ }
196
+ }
197
+ l = new WeakMap(), g = new WeakMap();
198
+ class q {
199
+ constructor({
200
+ config: t,
201
+ api: e
202
+ }) {
203
+ s(this, "config");
204
+ s(this, "api");
205
+ s(this, "state");
206
+ s(this, "shouldCollectData", () => {
207
+ var e;
208
+ return !!(!((e = this.state.get().contact) != null && e.token) && this.config.collectUserData);
209
+ });
210
+ s(this, "autoCreateUnverifiedUser", async () => {
211
+ var t, e;
212
+ await this.createUnverifiedContact({
213
+ name: ((t = this.config.user) == null ? void 0 : t.name) || "Anonymous",
214
+ email: (e = this.config.user) == null ? void 0 : e.email
215
+ });
216
+ });
217
+ s(this, "createUnverifiedContact", async (t) => {
218
+ try {
219
+ this.state.setPartial({
220
+ isCreatingUnverifiedContact: !0,
221
+ isErrorCreatingUnverifiedContact: !1
222
+ });
223
+ const { data: e } = await this.api.createUnverifiedContact(t);
224
+ e != null && e.token ? (this.state.setPartial({ contact: { token: e.token } }), this.api.setAuthToken(e.token)) : this.state.setPartial({ isErrorCreatingUnverifiedContact: !0 });
225
+ } finally {
226
+ this.state.setPartial({ isCreatingUnverifiedContact: !1 });
227
+ }
228
+ });
229
+ this.config = t, this.api = e, this.state = new P({
230
+ contact: t.contactToken ? { token: t.contactToken } : null,
231
+ isCreatingUnverifiedContact: !1,
232
+ isErrorCreatingUnverifiedContact: !1
233
+ }), !t.contactToken && !t.collectUserData && this.autoCreateUnverifiedUser();
234
+ }
235
+ }
236
+ class M {
237
+ constructor() {
238
+ s(this, "state", new P({
239
+ isPolling: !1,
240
+ isError: !1
241
+ }));
242
+ s(this, "abortController", new AbortController());
243
+ s(this, "reset", () => {
244
+ var t;
245
+ this.abortController.abort("Resetting poller"), (t = this.stopPolling) == null || t.call(this), this.stopPolling = null;
246
+ });
247
+ s(this, "stopPolling", null);
248
+ s(this, "startPolling", (t, e) => {
249
+ if (this.stopPolling) return;
250
+ const i = [], n = async () => {
251
+ this.abortController = new AbortController(), this.state.setPartial({ isPolling: !0 });
252
+ try {
253
+ await t(this.abortController.signal);
254
+ } catch (r) {
255
+ if (this.abortController.signal.aborted)
256
+ return;
257
+ console.error("Failed to poll:", r), this.state.setPartial({ isError: !0 });
258
+ } finally {
259
+ this.state.setPartial({ isPolling: !1 });
260
+ }
261
+ this.abortController.signal.aborted ? console.log("Poller aborted, not scheduling anymore") : i.push(setTimeout(n, e));
262
+ };
263
+ n(), this.stopPolling = () => {
264
+ i.forEach(clearTimeout), this.state.reset();
265
+ };
266
+ });
267
+ }
268
+ }
269
+ function w() {
270
+ return L();
271
+ }
272
+ class _ {
273
+ constructor(t) {
274
+ s(this, "api");
275
+ s(this, "poller", new M());
276
+ s(this, "state", new P({
277
+ session: null,
278
+ isCreatingSession: !1
279
+ }));
280
+ /** Clears the session and stops polling */
281
+ s(this, "reset", async () => {
282
+ this.state.reset(), this.poller.reset();
283
+ });
284
+ s(this, "registerPolling", () => {
285
+ this.state.subscribe(({ session: t }) => {
286
+ t != null && t.id ? this.poller.startPolling(async (e) => {
287
+ const { data: i } = await this.fetch(t.id, e);
288
+ i && this.state.setPartial({ session: i });
289
+ }, 1e3) : this.poller.reset();
290
+ });
291
+ });
292
+ /**
293
+ * Creates a new session
294
+ * @returns The session
295
+ */
296
+ s(this, "createSession", async () => {
297
+ this.state.setPartial({ session: null, isCreatingSession: !0 });
298
+ const { data: t, error: e } = await this.api.createSession();
299
+ return t ? (this.state.setPartial({ session: t, isCreatingSession: !1 }), t) : (console.error("Failed to create session:", e), null);
300
+ });
301
+ /**
302
+ * Fetches the session from the API
303
+ * @param id - The ID of the session to fetch
304
+ * @returns The session
305
+ */
306
+ s(this, "fetch", async (t, e) => this.api.getSession({ sessionId: t, abortSignal: e }));
307
+ this.api = t, this.registerPolling();
308
+ }
309
+ }
310
+ class d {
311
+ constructor({
312
+ config: t,
313
+ api: e,
314
+ sessionCtx: i
315
+ }) {
316
+ s(this, "config");
317
+ s(this, "api");
318
+ s(this, "sessionCtx");
319
+ s(this, "poller", new M());
320
+ s(this, "state", new P({
321
+ messages: [],
322
+ isSendingMessage: !1,
323
+ suggestedReplies: null
324
+ }));
325
+ s(this, "sendMessageAbortController", new AbortController());
326
+ s(this, "reset", () => {
327
+ this.sendMessageAbortController.abort("Resetting chat"), this.state.reset(), this.poller.reset();
328
+ });
329
+ s(this, "registerPolling", () => {
330
+ this.sessionCtx.state.subscribe(({ session: t }) => {
331
+ t != null && t.id ? this.poller.startPolling(async (e) => {
332
+ await this.fetchAndSetHistory(t.id, e);
333
+ }, 1e3) : this.poller.reset();
334
+ });
335
+ });
336
+ s(this, "sendMessage", async (t) => {
337
+ var n, r, u, f, m, C;
338
+ this.sendMessageAbortController = new AbortController();
339
+ const e = this.state.get().isSendingMessage, i = ((n = this.sessionCtx.state.get().session) == null ? void 0 : n.assignee.kind) === "ai";
340
+ if (e && i) {
341
+ console.warn("Cannot send messages while awaiting AI response");
342
+ return;
343
+ }
344
+ this.state.setPartial({ suggestedReplies: null });
345
+ try {
346
+ this.state.setPartial({ isSendingMessage: !0 });
347
+ const b = d.toUserMessage(
348
+ t.content,
349
+ t.attachments || void 0
350
+ ), k = this.state.get().messages;
351
+ if (this.state.setPartial({
352
+ messages: [...k, b]
353
+ }), !((r = this.sessionCtx.state.get().session) != null && r.id) && !await this.sessionCtx.createSession()) {
354
+ console.error("Failed to create session");
355
+ return;
356
+ }
357
+ const v = (u = this.sessionCtx.state.get().session) == null ? void 0 : u.id;
358
+ if (!v) return;
359
+ const { data: o } = await this.api.sendMessage(
360
+ {
361
+ uuid: b.id,
362
+ bot_token: this.config.token,
363
+ headers: this.config.headers,
364
+ query_params: this.config.queryParams,
365
+ session_id: v,
366
+ user: this.config.user,
367
+ ...t
368
+ },
369
+ this.sendMessageAbortController.signal
370
+ );
371
+ if (o != null && o.success) {
372
+ const h = d.toBotMessage(o);
373
+ if (h) {
374
+ const y = this.state.get().messages;
375
+ if (!!y.some(
376
+ (T) => T.id === h.id
377
+ )) return;
378
+ this.state.setPartial({ messages: [...y, h] });
379
+ } else
380
+ (f = o.options) != null && f.value && ((m = o.options) == null ? void 0 : m.value.length) > 0 && this.state.setPartial({ suggestedReplies: o.options.value });
381
+ } else {
382
+ const h = d.toErrorMessage(
383
+ ((C = o == null ? void 0 : o.error) == null ? void 0 : C.message) || "Unknown error occurred"
384
+ ), y = this.state.get().messages;
385
+ this.state.setPartial({
386
+ messages: [...y, h]
387
+ });
388
+ }
389
+ } catch (b) {
390
+ this.sendMessageAbortController.signal.aborted || console.error("Failed to send message:", b);
391
+ } finally {
392
+ this.state.setPartial({ isSendingMessage: !1 });
393
+ }
394
+ });
395
+ s(this, "fetchAndSetHistory", async (t, e) => {
396
+ var r;
397
+ const i = (r = this.state.get().messages.at(-1)) == null ? void 0 : r.timestamp, { data: n } = await this.api.getSessionHistory({
398
+ sessionId: t,
399
+ lastMessageTimestamp: i,
400
+ abortSignal: e
401
+ });
402
+ if (n && n.length > 0) {
403
+ const u = this.state.get().messages, f = n.map(d.mapHistoryToMessage).filter(
404
+ (m) => !u.some((C) => C.id === m.id)
405
+ );
406
+ this.state.setPartial({
407
+ messages: [...u, ...f]
408
+ });
409
+ }
410
+ });
411
+ this.config = t, this.api = e, this.sessionCtx = i, this.registerPolling();
412
+ }
413
+ /** Not the best name but whatever */
414
+ static mapHistoryToMessage(t) {
415
+ const e = {
416
+ id: t.publicId,
417
+ timestamp: t.sentAt || "",
418
+ attachments: t.attachments || void 0
419
+ };
420
+ return t.sender.kind === "user" ? {
421
+ ...e,
422
+ type: "FROM_USER",
423
+ content: t.content.text || "",
424
+ deliveredAt: t.sentAt || ""
425
+ } : t.sender.kind === "agent" ? {
426
+ ...e,
427
+ type: "FROM_AGENT",
428
+ component: "agent_message",
429
+ data: {
430
+ message: t.content.text || ""
431
+ }
432
+ } : {
433
+ ...e,
434
+ type: "FROM_BOT",
435
+ component: "bot_message",
436
+ agent: {
437
+ id: null,
438
+ name: t.sender.name || "",
439
+ isAi: t.sender.kind === "ai",
440
+ avatar: t.sender.avatar || null
441
+ },
442
+ data: {
443
+ message: t.content.text
444
+ }
445
+ };
446
+ }
447
+ static toUserMessage(t, e) {
448
+ return {
449
+ id: w(),
450
+ type: "FROM_USER",
451
+ content: t,
452
+ deliveredAt: (/* @__PURE__ */ new Date()).toISOString(),
453
+ attachments: e,
454
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
455
+ };
456
+ }
457
+ static toBotMessage(t) {
458
+ if (t.success && t.autopilotResponse)
459
+ return {
460
+ type: "FROM_BOT",
461
+ id: t.autopilotResponse.id || w(),
462
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
463
+ component: "bot_message",
464
+ data: {
465
+ message: t.autopilotResponse.value.content
466
+ }
467
+ };
468
+ if (t.success && t.uiResponse) {
469
+ const e = t.uiResponse.value;
470
+ return {
471
+ type: "FROM_BOT",
472
+ id: w(),
473
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
474
+ component: e.name,
475
+ data: e.request_response
476
+ };
477
+ }
478
+ return null;
479
+ }
480
+ static toErrorMessage(t) {
481
+ return {
482
+ type: "FROM_BOT",
483
+ id: w(),
484
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
485
+ component: "TEXT",
486
+ data: {
487
+ message: t,
488
+ variant: "error"
489
+ }
490
+ };
491
+ }
492
+ }
493
+ class W {
494
+ constructor({ config: t }) {
495
+ s(this, "config");
496
+ s(this, "api");
497
+ s(this, "contactCtx");
498
+ s(this, "sessionCtx");
499
+ s(this, "messageCtx");
500
+ s(this, "resetChat", () => {
501
+ this.sessionCtx.reset(), this.messageCtx.reset();
502
+ });
503
+ this.config = t, this.api = new B({ config: t }), this.contactCtx = new q({
504
+ api: this.api,
505
+ config: this.config
506
+ }), this.sessionCtx = new _(this.api), this.messageCtx = new d({
507
+ config: this.config,
508
+ api: this.api,
509
+ sessionCtx: this.sessionCtx
510
+ });
511
+ }
512
+ }
513
+ export {
514
+ P,
515
+ W
516
+ };
517
+ //# sourceMappingURL=widget-BeNOCqB5.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"widget-BeNOCqB5.js","sources":["../src/headless/core/sdk/index.ts","../src/headless/core/api.ts","../src/headless/core/utils/PubSub.ts","../src/headless/core/context/contact.ts","../src/headless/core/utils/Poller.ts","../src/headless/core/utils/uuid.ts","../src/headless/core/context/session.ts","../src/headless/core/context/message.ts","../src/headless/core/context/widget.ts"],"sourcesContent":["import createClient, { type Middleware } from \"openapi-fetch\";\nimport type { paths } from \"./schema\";\nimport type { components } from \"./schema\";\n\ntype Options = {\n baseUrl: string;\n onRequest?: Middleware[\"onRequest\"];\n onResponse?: Middleware[\"onResponse\"];\n onError?: Middleware[\"onError\"];\n};\n\nconst defaultOnError: Middleware[\"onError\"] = (onErrorOptions) => {\n console.log(onErrorOptions.error);\n};\n\nexport const basicClient = (options: Options) => {\n const client = createClient<paths>({\n baseUrl: options.baseUrl,\n });\n\n const middlewares: Middleware = {\n onRequest: options.onRequest,\n onResponse: options.onResponse,\n onError: options.onError || defaultOnError,\n };\n\n client.use(middlewares);\n return client;\n};\n\nexport type Endpoint = keyof paths;\nexport type Dto = components[\"schemas\"];\n","import axios, { type AxiosInstance, type AxiosRequestConfig } from \"axios\";\nimport { type Dto, type Endpoint, basicClient } from \"./sdk\";\nimport type { WidgetConfig } from \"./types/WidgetConfig\";\nimport type { SendMessageDto, VoteInputDto } from \"./types/schemas\";\n\nexport class ApiCaller {\n private client: ReturnType<typeof basicClient>;\n private uploadFileClient: AxiosInstance;\n private config: WidgetConfig;\n\n constructor({\n config,\n }: {\n config: WidgetConfig;\n }) {\n this.config = config;\n const { baseUrl, headers } = this.constructClientOptions(\n config.contactToken,\n );\n this.client = this.createOpenAPIClient({ baseUrl, headers });\n this.uploadFileClient = this.createAxiosUploadClient({ baseUrl, headers });\n }\n\n private constructClientOptions = (token: string | null | undefined) => {\n const baseUrl = this.config.apiUrl || \"https://api.open.cx\";\n const headers = {\n \"X-Bot-Token\": this.config.token,\n \"Content-Type\": \"application/json\",\n Accept: \"application/json\",\n Authorization: token ? `Bearer ${token}` : undefined,\n };\n\n return { baseUrl, headers };\n };\n\n private createOpenAPIClient = ({\n baseUrl,\n headers,\n }: ReturnType<typeof this.constructClientOptions>) => {\n return basicClient({\n baseUrl,\n onRequest: ({ request }) => {\n Object.entries(headers).forEach(([key, value]) => {\n if (value) {\n request.headers.set(key, value);\n }\n });\n },\n });\n };\n private createAxiosUploadClient = ({\n baseUrl,\n headers,\n }: ReturnType<typeof this.constructClientOptions>) => {\n const uploadPath = \"/backend/widget/v2/upload\" satisfies Endpoint;\n return axios.create({\n baseURL: `${baseUrl}${uploadPath}`,\n headers,\n });\n };\n\n setAuthToken = (token: string) => {\n const { baseUrl, headers } = this.constructClientOptions(token);\n this.client = this.createOpenAPIClient({ baseUrl, headers });\n this.uploadFileClient = this.createAxiosUploadClient({ baseUrl, headers });\n };\n\n widgetPrelude = async () => {\n return await this.client.GET(\"/backend/widget/v2/prelude\", {\n params: { header: { \"X-Bot-Token\": this.config.token } },\n });\n };\n\n sendMessage = async (body: SendMessageDto, abortSignal?: AbortSignal) => {\n return await this.client.POST(\"/backend/widget/v2/chat/send\", {\n body,\n signal: abortSignal,\n });\n };\n\n getSessionHistory = async ({\n sessionId,\n lastMessageTimestamp,\n abortSignal,\n }: {\n sessionId: string;\n lastMessageTimestamp?: string;\n abortSignal: AbortSignal;\n }) => {\n const query = lastMessageTimestamp ? { lastMessageTimestamp } : undefined;\n return await this.client.GET(\n \"/backend/widget/v2/session/history/{sessionId}\",\n { params: { path: { sessionId }, query }, signal: abortSignal },\n );\n };\n\n createUnverifiedContact = async (body: Dto[\"CreateUnverifiedContactDto\"]) => {\n return await this.client.POST(\n \"/backend/widget/v2/contact/create-unverified\",\n {\n params: { header: { \"x-bot-token\": this.config.token } },\n body,\n },\n );\n };\n\n createSession = async () => {\n return await this.client.POST(\"/backend/widget/v2/create-session\");\n };\n\n getSession = async ({\n sessionId,\n abortSignal,\n }: { sessionId: string; abortSignal: AbortSignal }) => {\n return await this.client.GET(\"/backend/widget/v2/session/{sessionId}\", {\n params: { path: { sessionId } },\n signal: abortSignal,\n });\n };\n\n uploadFile = async (\n file: {\n id: string;\n file: File;\n },\n config: Partial<AxiosRequestConfig> = {},\n ) => {\n const formData = new FormData();\n formData.append(\"file\", file.file);\n\n // Couldn't get this to work with the openapi client... dunno why...\n const { data } = await this.uploadFileClient.post<\n Dto[\"UploadWidgetFileResponseDto\"]\n >(\"\", formData, {\n headers: {\n \"Content-Type\": \"multipart/form-data\",\n },\n ...config,\n });\n return data;\n };\n\n vote = async (body: VoteInputDto) => {\n return await this.client.POST(\"/backend/widget/v2/chat/vote\", { body });\n };\n}\n","import isEqual from \"lodash.isequal\";\n\nexport type Subscriber<T> = (data: T) => void;\n\nexport enum LifecycleEvent {\n INIT = \"init\",\n STATE_CHANGE = \"stateChange\",\n BEFORE_UPDATE = \"beforeUpdate\",\n AFTER_UPDATE = \"afterUpdate\",\n DESTROY = \"destroy\",\n ERROR = \"error\",\n}\n\ntype LifecycleListener = (event: {\n type: LifecycleEvent;\n timestamp: number;\n data?: any;\n}) => void;\n\nexport class PubSub<S> {\n private subscribers = new Set<Subscriber<S>>();\n #state: S;\n private initialState: S;\n #lastUpdated: number | null;\n private lifecycleListeners: Map<LifecycleEvent, Set<LifecycleListener>> =\n new Map();\n\n constructor(state: S) {\n this.#state = state;\n this.initialState = state;\n this.#lastUpdated = Date.now();\n this.emitLifecycle(LifecycleEvent.INIT, { initialState: this.#state });\n }\n\n private emitLifecycle = (event: LifecycleEvent, data?: any) => {\n const listeners = this.lifecycleListeners.get(event);\n if (listeners) {\n const eventData = {\n type: event,\n timestamp: Date.now(),\n data,\n };\n listeners.forEach((listener) => {\n try {\n listener(eventData);\n } catch {\n // ignore error\n }\n });\n }\n };\n\n private notifySubscribers = (state: S) => {\n const subscribersArray = Array.from(this.subscribers);\n subscribersArray.forEach((callback) => {\n try {\n callback(state);\n } catch (error) {\n this.emitLifecycle(LifecycleEvent.ERROR, { error });\n }\n });\n };\n\n /**\n * Subscribe to state changes\n * @param callback Function to call when state changes\n * @returns Unsubscribe function\n */\n subscribe = (callback: Subscriber<S>): (() => void) => {\n this.subscribers.add(callback);\n // Don't call the callback immediately with current state\n return () => {\n this.subscribers.delete(callback);\n };\n };\n\n onLifecycle = (\n event: LifecycleEvent,\n listener: LifecycleListener,\n ): (() => void) => {\n if (!this.lifecycleListeners.has(event)) {\n this.lifecycleListeners.set(event, new Set());\n }\n const listeners = this.lifecycleListeners.get(event)!;\n listeners.add(listener);\n\n return () => {\n listeners.delete(listener);\n if (listeners.size === 0) {\n this.lifecycleListeners.delete(event);\n }\n };\n };\n\n /** Get the current state */\n get = (): S => {\n return this.#state;\n };\n\n // TODO make this provide prev state\n /**\n * Set the state and notify subscribers if the state changes\n * @param newState The new state to set\n */\n set = (newState: S): void => {\n this.emitLifecycle(LifecycleEvent.BEFORE_UPDATE, {\n previousState: this.#state,\n nextState: newState,\n });\n\n if (!isEqual(this.#state, newState)) {\n this.#state = newState;\n this.#lastUpdated = Date.now();\n this.emitLifecycle(LifecycleEvent.STATE_CHANGE, { state: newState });\n this.notifySubscribers(newState);\n }\n\n this.emitLifecycle(LifecycleEvent.AFTER_UPDATE, { state: newState });\n };\n\n // TODO make this provide prev state\n setPartial = (_s: Partial<S>): void => {\n if (_s === undefined || _s === null) return;\n const newState = { ...this.#state, ..._s };\n this.set(newState);\n };\n\n /**\n * Clear all subscriptions\n */\n clear = (): void => {\n this.emitLifecycle(LifecycleEvent.DESTROY);\n this.subscribers = new Set(); // Create a new Set instead of just clearing\n this.lifecycleListeners = new Map();\n };\n\n reset = (): void => {\n this.set(this.initialState);\n };\n\n lastUpdated = (): number | null => {\n return this.#lastUpdated;\n };\n}\n","import { PubSub } from \"../utils/PubSub\";\nimport { ApiCaller } from \"../api\";\nimport { type WidgetConfig } from \"../types/WidgetConfig\";\nimport { type Dto } from \"../sdk\";\n\ntype ContactState = {\n contact: { token: string } | null;\n isCreatingUnverifiedContact: boolean;\n isErrorCreatingUnverifiedContact: boolean;\n};\n\nexport class ContactCtx {\n private config: WidgetConfig;\n private api: ApiCaller;\n state: PubSub<ContactState>;\n\n constructor({\n config,\n api,\n }: {\n api: ApiCaller;\n config: WidgetConfig;\n }) {\n this.config = config;\n this.api = api;\n\n this.state = new PubSub<ContactState>({\n contact: config.contactToken ? { token: config.contactToken } : null,\n isCreatingUnverifiedContact: false,\n isErrorCreatingUnverifiedContact: false,\n });\n\n if (!config.contactToken && !config.collectUserData) {\n this.autoCreateUnverifiedUser();\n }\n }\n\n shouldCollectData = (): boolean => {\n const currentState = this.state.get();\n\n if (!currentState.contact?.token && this.config.collectUserData) {\n return true;\n } else {\n return false;\n }\n };\n\n autoCreateUnverifiedUser = async () => {\n await this.createUnverifiedContact({\n name: this.config.user?.name || \"Anonymous\",\n email: this.config.user?.email,\n });\n };\n\n createUnverifiedContact = async (\n payload: Dto[\"CreateUnverifiedContactDto\"],\n ): Promise<void> => {\n try {\n this.state.setPartial({\n isCreatingUnverifiedContact: true,\n isErrorCreatingUnverifiedContact: false,\n });\n\n const { data } = await this.api.createUnverifiedContact(payload);\n if (data?.token) {\n this.state.setPartial({ contact: { token: data.token } });\n this.api.setAuthToken(data.token);\n } else {\n this.state.setPartial({ isErrorCreatingUnverifiedContact: true });\n }\n } finally {\n this.state.setPartial({ isCreatingUnverifiedContact: false });\n }\n };\n}\n","import { PubSub } from \"./PubSub\";\n\nexport type PollingState = {\n isPolling: boolean;\n isError: boolean;\n};\n\nexport class Poller {\n state = new PubSub<PollingState>({\n isPolling: false,\n isError: false,\n });\n private abortController = new AbortController();\n\n reset = () => {\n this.abortController.abort(\"Resetting poller\");\n this.stopPolling?.();\n this.stopPolling = null;\n };\n\n stopPolling: (() => void) | null = null;\n\n startPolling = (\n cb: (abortSignal: AbortSignal) => Promise<void>,\n interval: number,\n ) => {\n if (this.stopPolling) return;\n\n const timeouts: NodeJS.Timeout[] = [];\n\n const poll = async () => {\n this.abortController = new AbortController();\n this.state.setPartial({ isPolling: true });\n\n try {\n await cb(this.abortController.signal);\n } catch (error) {\n if (this.abortController.signal.aborted) {\n // If aborted, just return and do not schedule the nest poll\n return;\n } else {\n console.error(\"Failed to poll:\", error);\n this.state.setPartial({ isError: true });\n } \n } finally {\n this.state.setPartial({ isPolling: false });\n }\n\n // Another check to stop scheduling polls in case someone removes the early return in the catch above\n if (this.abortController.signal.aborted) {\n console.log(\"Poller aborted, not scheduling anymore\");\n } else {\n timeouts.push(setTimeout(poll, interval));\n }\n };\n\n poll();\n\n this.stopPolling = () => {\n timeouts.forEach(clearTimeout);\n this.state.reset();\n };\n };\n}\n","import { v4 as uuidv4 } from \"uuid\";\n\nexport function genUuid() {\n return uuidv4();\n}\n","import type { ApiCaller } from \"../api\";\nimport type { SessionDto } from \"../types/schemas\";\nimport { Poller } from \"../utils/Poller\";\nimport { PubSub } from \"../utils/PubSub\";\n\nexport class SessionCtx {\n private api: ApiCaller;\n private poller = new Poller();\n\n public state = new PubSub<{\n session: SessionDto | null;\n isCreatingSession: boolean;\n }>({\n session: null,\n isCreatingSession: false,\n });\n\n constructor(api: ApiCaller) {\n this.api = api;\n this.registerPolling();\n }\n\n /** Clears the session and stops polling */\n reset = async () => {\n this.state.reset();\n // The poller should automatically reset, since we're subscribed to the session state, and whenever it's null, the poller resets... but just in case, let's reset it here as well\n this.poller.reset();\n };\n\n registerPolling = () => {\n this.state.subscribe(({ session }) => {\n if (session?.id) {\n this.poller.startPolling(async (abortSignal) => {\n const { data } = await this.fetch(session.id, abortSignal);\n data && this.state.setPartial({ session: data });\n }, 1000);\n } else {\n this.poller.reset();\n }\n });\n };\n\n /**\n * Creates a new session\n * @returns The session\n */\n createSession = async () => {\n this.state.setPartial({ session: null, isCreatingSession: true });\n\n const { data: session, error } = await this.api.createSession();\n if (session) {\n this.state.setPartial({ session, isCreatingSession: false });\n return session;\n }\n\n console.error(\"Failed to create session:\", error);\n return null;\n };\n\n /**\n * Fetches the session from the API\n * @param id - The ID of the session to fetch\n * @returns The session\n */\n fetch = async (sessionId: string, abortSignal: AbortSignal) => {\n return this.api.getSession({ sessionId, abortSignal });\n };\n}\n","import { ApiCaller } from \"../api\";\nimport type { WidgetConfig } from \"../types/WidgetConfig\";\nimport type { SafeOmit, SomeOptional } from \"../types/helpers\";\nimport type { BotMessageType, MessageType, UserMessageType } from \"../types/messages\";\nimport type { MessageAttachmentType, MessageDto, SendMessageDto, SendMessageOutputDto } from \"../types/schemas\";\nimport { Poller } from \"../utils/Poller\";\nimport { PubSub } from \"../utils/PubSub\";\nimport { genUuid } from \"../utils/uuid\";\nimport { SessionCtx } from \"./session\";\n\nexport class MessageCtx {\n private config: WidgetConfig;\n private api: ApiCaller;\n private sessionCtx: SessionCtx;\n private poller = new Poller();\n\n public state = new PubSub<{\n messages: MessageType[];\n isSendingMessage: boolean;\n suggestedReplies: string[] | null;\n }>({\n messages: [],\n isSendingMessage: false,\n suggestedReplies: null,\n });\n\n private sendMessageAbortController = new AbortController();\n\n constructor({\n config,\n api,\n sessionCtx,\n }: { config: WidgetConfig; api: ApiCaller; sessionCtx: SessionCtx }) {\n this.config = config;\n this.api = api;\n this.sessionCtx = sessionCtx;\n\n this.registerPolling();\n }\n\n reset = () => {\n this.sendMessageAbortController.abort(\"Resetting chat\");\n this.state.reset();\n // The poller should automatically reset, since we're subscribed to the session state, and whenever it's null, the poller resets... but just in case, let's reset it here as well\n this.poller.reset();\n };\n\n registerPolling = () => {\n this.sessionCtx.state.subscribe(({ session }) => {\n if (session?.id) {\n this.poller.startPolling(async (abortSignal) => {\n await this.fetchAndSetHistory(session.id, abortSignal);\n }, 1000);\n } else {\n this.poller.reset();\n }\n });\n };\n\n sendMessage = async (\n input: SomeOptional<\n SafeOmit<SendMessageDto, \"bot_token\" | \"uuid\">,\n \"session_id\" | \"user\"\n >,\n ): Promise<void> => {\n this.sendMessageAbortController = new AbortController();\n /* ------------------------------------------------------ */\n /* Prevent sending while waiting for AI res */\n /* ------------------------------------------------------ */\n const isSending = this.state.get().isSendingMessage;\n const isAssignedToAI =\n this.sessionCtx.state.get().session?.assignee.kind === \"ai\";\n if (isSending && isAssignedToAI) {\n console.warn(\"Cannot send messages while awaiting AI response\");\n return;\n }\n\n /* ------------------------------------------------------ */\n /* Clear suggested replies */\n /* ------------------------------------------------------ */\n this.state.setPartial({ suggestedReplies: null });\n\n try {\n this.state.setPartial({ isSendingMessage: true });\n /* ------------------------------------------------------ */\n /* Optimistically add message to rendered messages */\n /* ------------------------------------------------------ */\n const userMessage = MessageCtx.toUserMessage(\n input.content,\n input.attachments || undefined,\n );\n const currentMessages = this.state.get().messages;\n this.state.setPartial({\n messages: [...currentMessages, userMessage],\n });\n\n /* ------------------------------------------------------ */\n /* Create session if not exists */\n /* ------------------------------------------------------ */\n if (!this.sessionCtx.state.get().session?.id) {\n const createdSession = await this.sessionCtx.createSession();\n\n // TODO: apply some retry logic here\n if (!createdSession) {\n console.error(\"Failed to create session\");\n return;\n }\n }\n const sessionId = this.sessionCtx.state.get().session?.id;\n if (!sessionId) return;\n\n /* ------------------------------------------------------ */\n /* Send and wait for bot response */\n /* ------------------------------------------------------ */\n const { data } = await this.api.sendMessage(\n {\n uuid: userMessage.id,\n bot_token: this.config.token,\n headers: this.config.headers,\n query_params: this.config.queryParams,\n session_id: sessionId,\n user: this.config.user,\n ...input,\n },\n this.sendMessageAbortController.signal,\n );\n\n if (data?.success) {\n /* ------------------------------------------------------ */\n /* Append bot reply if not fetched from polling */\n /* ------------------------------------------------------ */\n const botMessage = MessageCtx.toBotMessage(data);\n if (botMessage) {\n const prevMessages = this.state.get().messages;\n const shouldAppend = !prevMessages.some(\n (m) => m.id === botMessage.id,\n );\n if (!shouldAppend) return;\n this.state.setPartial({ messages: [...prevMessages, botMessage] });\n } else {\n /* ------------------------------------------------------ */\n /* Check if bot responded with suggested replies */\n /* ------------------------------------------------------ */\n if (data.options?.value && data.options?.value.length > 0) {\n this.state.setPartial({ suggestedReplies: data.options.value });\n }\n }\n } else {\n const errorMessage = MessageCtx.toErrorMessage(\n data?.error?.message || \"Unknown error occurred\",\n );\n const currentMessages = this.state.get().messages;\n this.state.setPartial({\n messages: [...currentMessages, errorMessage],\n });\n }\n } catch (error) {\n if (!this.sendMessageAbortController.signal.aborted) {\n console.error(\"Failed to send message:\", error);\n }\n } finally {\n this.state.setPartial({ isSendingMessage: false });\n }\n };\n\n fetchAndSetHistory = async (\n sessionId: string,\n abortSignal: AbortSignal,\n ): Promise<void> => {\n const lastMessageTimestamp = this.state.get().messages.at(-1)?.timestamp;\n\n const { data: response } = await this.api.getSessionHistory({\n sessionId,\n lastMessageTimestamp,\n abortSignal,\n });\n\n if (response && response.length > 0) {\n // Get a fresh reference to current messages after the poll is done\n const prevMessages = this.state.get().messages;\n const newMessages = response\n .map(MessageCtx.mapHistoryToMessage)\n .filter(\n (newMsg) =>\n !prevMessages.some((existingMsg) => existingMsg.id === newMsg.id),\n );\n this.state.setPartial({\n messages: [...prevMessages, ...newMessages],\n });\n\n // if (newMessages.length > 0) {\n // // const playSoundEffects = config?.settings?.playSoundEffects;\n // // Play notification sound for new messages if enabled\n // // if (\n // // playSoundEffects &&\n // // platform?.audio &&\n // // isAudioAvailable(platform.audio)\n // // ) {\n // // const botMessages = newMessages.filter(\n // // (msg) => msg.type === \"FROM_BOT\",\n // // );\n // // if (botMessages.length > 0) {\n // // await safeAudioOperation(\n // // () => platform.audio!.playNotification(),\n // // \"Failed to play notification sound for new messages\",\n // // );\n // // }\n // // }\n\n // }\n }\n };\n\n /** Not the best name but whatever */\n static mapHistoryToMessage(history: MessageDto): MessageType {\n const commonFields = {\n id: history.publicId,\n timestamp: history.sentAt || \"\",\n attachments: history.attachments || undefined,\n };\n\n if (history.sender.kind === \"user\") {\n return {\n ...commonFields,\n type: \"FROM_USER\",\n content: history.content.text || \"\",\n deliveredAt: history.sentAt || \"\",\n };\n }\n\n if (history.sender.kind === \"agent\") {\n return {\n ...commonFields,\n type: \"FROM_AGENT\",\n component: \"agent_message\",\n data: {\n message: history.content.text || \"\",\n },\n };\n }\n\n return {\n ...commonFields,\n type: \"FROM_BOT\",\n component: \"bot_message\",\n agent: {\n id: null,\n name: history.sender.name || \"\",\n isAi: history.sender.kind === \"ai\",\n avatar: history.sender.avatar || null,\n },\n data: {\n message: history.content.text,\n },\n };\n }\n\n static toUserMessage(\n content: string,\n attachments?: MessageAttachmentType[],\n ): UserMessageType {\n return {\n id: genUuid(),\n type: \"FROM_USER\",\n content,\n deliveredAt: new Date().toISOString(),\n attachments,\n timestamp: new Date().toISOString(),\n };\n }\n\n static toBotMessage(response: SendMessageOutputDto): BotMessageType | null {\n if (response.success && response.autopilotResponse) {\n return {\n type: \"FROM_BOT\",\n id: response.autopilotResponse.id || genUuid(),\n timestamp: new Date().toISOString(),\n component: \"bot_message\",\n data: {\n message: response.autopilotResponse.value.content,\n },\n };\n }\n\n if (response.success && response.uiResponse) {\n const uiVal = response.uiResponse.value;\n return {\n type: \"FROM_BOT\" as const,\n id: genUuid(),\n timestamp: new Date().toISOString(),\n component: uiVal.name,\n data: uiVal.request_response,\n };\n }\n\n return null;\n }\n\n static toErrorMessage(message: string) {\n return {\n type: \"FROM_BOT\" as const,\n id: genUuid(),\n timestamp: new Date().toISOString(),\n component: \"TEXT\",\n data: {\n message,\n variant: \"error\",\n },\n };\n }\n}\n","import { ApiCaller } from \"../api\";\nimport type { WidgetConfig } from \"../types/WidgetConfig\";\nimport { ContactCtx } from \"./contact\";\nimport { MessageCtx } from \"./message\";\nimport { SessionCtx } from \"./session\";\n\nexport class WidgetCtx {\n public config: WidgetConfig;\n public api: ApiCaller;\n public contactCtx: ContactCtx;\n public sessionCtx: SessionCtx;\n public messageCtx: MessageCtx;\n\n constructor({ config }: { config: WidgetConfig }) {\n this.config = config;\n this.api = new ApiCaller({ config });\n\n this.contactCtx = new ContactCtx({\n api: this.api,\n config: this.config,\n });\n\n this.sessionCtx = new SessionCtx(this.api);\n this.messageCtx = new MessageCtx({\n config: this.config,\n api: this.api,\n sessionCtx: this.sessionCtx,\n });\n }\n\n resetChat = () => {\n this.sessionCtx.reset();\n this.messageCtx.reset();\n };\n}\n"],"names":["defaultOnError","onErrorOptions","basicClient","options","client","createClient","middlewares","ApiCaller","config","__publicField","token","baseUrl","headers","request","key","value","axios","body","abortSignal","sessionId","lastMessageTimestamp","query","file","formData","data","PubSub","state","__privateAdd","_state","_lastUpdated","event","listeners","eventData","listener","callback","error","__privateGet","newState","isEqual","__privateSet","_s","ContactCtx","api","_a","_b","payload","Poller","cb","interval","timeouts","poll","genUuid","uuidv4","SessionCtx","session","MessageCtx","sessionCtx","input","isSending","isAssignedToAI","userMessage","currentMessages","_c","botMessage","prevMessages","m","_d","_e","errorMessage","_f","response","newMessages","newMsg","existingMsg","history","commonFields","content","attachments","uiVal","message","WidgetCtx"],"mappings":";;;;;;;;;;;AAWA,MAAMA,IAAwC,CAACC,MAAmB;AACxD,UAAA,IAAIA,EAAe,KAAK;AAClC,GAEaC,IAAc,CAACC,MAAqB;AAC/C,QAAMC,IAASC,EAAoB;AAAA,IACjC,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,KAAK,aAAa,IAAIY,EAAW,KAAK,GAAG,GACpC,KAAA,aAAa,IAAIE,EAAW;AAAA,MAC/B,QAAQ,KAAK;AAAA,MACb,KAAK,KAAK;AAAA,MACV,YAAY,KAAK;AAAA,IAAA,CAClB;AAAA,EACH;AAMF;"}
@@ -0,0 +1,2 @@
1
+ "use strict";var x=Object.defineProperty;var A=a=>{throw TypeError(a)};var O=(a,e,t)=>e in a?x(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var s=(a,e,t)=>O(a,typeof e!="symbol"?e+"":e,t),U=(a,e,t)=>e.has(a)||A("Cannot "+t);var c=(a,e,t)=>(U(a,e,"read from private field"),t?t.call(a):e.get(a)),S=(a,e,t)=>e.has(a)?A("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(a):e.set(a,t),p=(a,e,t,i)=>(U(a,e,"write to private field"),i?i.call(a,t):e.set(a,t),t);const R=require("axios"),E=require("openapi-fetch"),F=require("lodash.isequal"),L=require("uuid"),D=a=>{console.log(a.error)},I=a=>{const e=E({baseUrl:a.baseUrl}),t={onRequest:a.onRequest,onResponse:a.onResponse,onError:a.onError||D};return e.use(t),e};class q{constructor({config:e}){s(this,"client");s(this,"uploadFileClient");s(this,"config");s(this,"constructClientOptions",e=>{const t=this.config.apiUrl||"https://api.open.cx",i={"X-Bot-Token":this.config.token,"Content-Type":"application/json",Accept:"application/json",Authorization:e?`Bearer ${e}`:void 0};return{baseUrl:t,headers:i}});s(this,"createOpenAPIClient",({baseUrl:e,headers:t})=>I({baseUrl:e,onRequest:({request:i})=>{Object.entries(t).forEach(([n,r])=>{r&&i.headers.set(n,r)})}}));s(this,"createAxiosUploadClient",({baseUrl:e,headers:t})=>R.create({baseURL:`${e}/backend/widget/v2/upload`,headers:t}));s(this,"setAuthToken",e=>{const{baseUrl:t,headers:i}=this.constructClientOptions(e);this.client=this.createOpenAPIClient({baseUrl:t,headers:i}),this.uploadFileClient=this.createAxiosUploadClient({baseUrl:t,headers:i})});s(this,"widgetPrelude",async()=>await this.client.GET("/backend/widget/v2/prelude",{params:{header:{"X-Bot-Token":this.config.token}}}));s(this,"sendMessage",async(e,t)=>await this.client.POST("/backend/widget/v2/chat/send",{body:e,signal:t}));s(this,"getSessionHistory",async({sessionId:e,lastMessageTimestamp:t,abortSignal:i})=>{const n=t?{lastMessageTimestamp:t}:void 0;return await this.client.GET("/backend/widget/v2/session/history/{sessionId}",{params:{path:{sessionId:e},query:n},signal:i})});s(this,"createUnverifiedContact",async e=>await this.client.POST("/backend/widget/v2/contact/create-unverified",{params:{header:{"x-bot-token":this.config.token}},body:e}));s(this,"createSession",async()=>await this.client.POST("/backend/widget/v2/create-session"));s(this,"getSession",async({sessionId:e,abortSignal:t})=>await this.client.GET("/backend/widget/v2/session/{sessionId}",{params:{path:{sessionId:e}},signal:t}));s(this,"uploadFile",async(e,t={})=>{const i=new FormData;i.append("file",e.file);const{data:n}=await this.uploadFileClient.post("",i,{headers:{"Content-Type":"multipart/form-data"},...t});return n});s(this,"vote",async e=>await this.client.POST("/backend/widget/v2/chat/vote",{body:e}));this.config=e;const{baseUrl:t,headers:i}=this.constructClientOptions(e.contactToken);this.client=this.createOpenAPIClient({baseUrl:t,headers:i}),this.uploadFileClient=this.createAxiosUploadClient({baseUrl:t,headers:i})}}var l,g;class f{constructor(e){s(this,"subscribers",new Set);S(this,l);s(this,"initialState");S(this,g);s(this,"lifecycleListeners",new Map);s(this,"emitLifecycle",(e,t)=>{const i=this.lifecycleListeners.get(e);if(i){const n={type:e,timestamp:Date.now(),data:t};i.forEach(r=>{try{r(n)}catch{}})}});s(this,"notifySubscribers",e=>{Array.from(this.subscribers).forEach(i=>{try{i(e)}catch(n){this.emitLifecycle("error",{error:n})}})});s(this,"subscribe",e=>(this.subscribers.add(e),()=>{this.subscribers.delete(e)}));s(this,"onLifecycle",(e,t)=>{this.lifecycleListeners.has(e)||this.lifecycleListeners.set(e,new Set);const i=this.lifecycleListeners.get(e);return i.add(t),()=>{i.delete(t),i.size===0&&this.lifecycleListeners.delete(e)}});s(this,"get",()=>c(this,l));s(this,"set",e=>{this.emitLifecycle("beforeUpdate",{previousState:c(this,l),nextState:e}),F(c(this,l),e)||(p(this,l,e),p(this,g,Date.now()),this.emitLifecycle("stateChange",{state:e}),this.notifySubscribers(e)),this.emitLifecycle("afterUpdate",{state:e})});s(this,"setPartial",e=>{if(e==null)return;const t={...c(this,l),...e};this.set(t)});s(this,"clear",()=>{this.emitLifecycle("destroy"),this.subscribers=new Set,this.lifecycleListeners=new Map});s(this,"reset",()=>{this.set(this.initialState)});s(this,"lastUpdated",()=>c(this,g));p(this,l,e),this.initialState=e,p(this,g,Date.now()),this.emitLifecycle("init",{initialState:c(this,l)})}}l=new WeakMap,g=new WeakMap;class B{constructor({config:e,api:t}){s(this,"config");s(this,"api");s(this,"state");s(this,"shouldCollectData",()=>{var t;return!!(!((t=this.state.get().contact)!=null&&t.token)&&this.config.collectUserData)});s(this,"autoCreateUnverifiedUser",async()=>{var e,t;await this.createUnverifiedContact({name:((e=this.config.user)==null?void 0:e.name)||"Anonymous",email:(t=this.config.user)==null?void 0:t.email})});s(this,"createUnverifiedContact",async e=>{try{this.state.setPartial({isCreatingUnverifiedContact:!0,isErrorCreatingUnverifiedContact:!1});const{data:t}=await this.api.createUnverifiedContact(e);t!=null&&t.token?(this.state.setPartial({contact:{token:t.token}}),this.api.setAuthToken(t.token)):this.state.setPartial({isErrorCreatingUnverifiedContact:!0})}finally{this.state.setPartial({isCreatingUnverifiedContact:!1})}});this.config=e,this.api=t,this.state=new f({contact:e.contactToken?{token:e.contactToken}:null,isCreatingUnverifiedContact:!1,isErrorCreatingUnverifiedContact:!1}),!e.contactToken&&!e.collectUserData&&this.autoCreateUnverifiedUser()}}class M{constructor(){s(this,"state",new f({isPolling:!1,isError:!1}));s(this,"abortController",new AbortController);s(this,"reset",()=>{var e;this.abortController.abort("Resetting poller"),(e=this.stopPolling)==null||e.call(this),this.stopPolling=null});s(this,"stopPolling",null);s(this,"startPolling",(e,t)=>{if(this.stopPolling)return;const i=[],n=async()=>{this.abortController=new AbortController,this.state.setPartial({isPolling:!0});try{await e(this.abortController.signal)}catch(r){if(this.abortController.signal.aborted)return;console.error("Failed to poll:",r),this.state.setPartial({isError:!0})}finally{this.state.setPartial({isPolling:!1})}this.abortController.signal.aborted?console.log("Poller aborted, not scheduling anymore"):i.push(setTimeout(n,t))};n(),this.stopPolling=()=>{i.forEach(clearTimeout),this.state.reset()}})}}function P(){return L.v4()}class _{constructor(e){s(this,"api");s(this,"poller",new M);s(this,"state",new f({session:null,isCreatingSession:!1}));s(this,"reset",async()=>{this.state.reset(),this.poller.reset()});s(this,"registerPolling",()=>{this.state.subscribe(({session:e})=>{e!=null&&e.id?this.poller.startPolling(async t=>{const{data:i}=await this.fetch(e.id,t);i&&this.state.setPartial({session:i})},1e3):this.poller.reset()})});s(this,"createSession",async()=>{this.state.setPartial({session:null,isCreatingSession:!0});const{data:e,error:t}=await this.api.createSession();return e?(this.state.setPartial({session:e,isCreatingSession:!1}),e):(console.error("Failed to create session:",t),null)});s(this,"fetch",async(e,t)=>this.api.getSession({sessionId:e,abortSignal:t}));this.api=e,this.registerPolling()}}class d{constructor({config:e,api:t,sessionCtx:i}){s(this,"config");s(this,"api");s(this,"sessionCtx");s(this,"poller",new M);s(this,"state",new f({messages:[],isSendingMessage:!1,suggestedReplies:null}));s(this,"sendMessageAbortController",new AbortController);s(this,"reset",()=>{this.sendMessageAbortController.abort("Resetting chat"),this.state.reset(),this.poller.reset()});s(this,"registerPolling",()=>{this.sessionCtx.state.subscribe(({session:e})=>{e!=null&&e.id?this.poller.startPolling(async t=>{await this.fetchAndSetHistory(e.id,t)},1e3):this.poller.reset()})});s(this,"sendMessage",async e=>{var n,r,u,m,C,b;this.sendMessageAbortController=new AbortController;const t=this.state.get().isSendingMessage,i=((n=this.sessionCtx.state.get().session)==null?void 0:n.assignee.kind)==="ai";if(t&&i){console.warn("Cannot send messages while awaiting AI response");return}this.state.setPartial({suggestedReplies:null});try{this.state.setPartial({isSendingMessage:!0});const y=d.toUserMessage(e.content,e.attachments||void 0),k=this.state.get().messages;if(this.state.setPartial({messages:[...k,y]}),!((r=this.sessionCtx.state.get().session)!=null&&r.id)&&!await this.sessionCtx.createSession()){console.error("Failed to create session");return}const v=(u=this.sessionCtx.state.get().session)==null?void 0:u.id;if(!v)return;const{data:o}=await this.api.sendMessage({uuid:y.id,bot_token:this.config.token,headers:this.config.headers,query_params:this.config.queryParams,session_id:v,user:this.config.user,...e},this.sendMessageAbortController.signal);if(o!=null&&o.success){const h=d.toBotMessage(o);if(h){const w=this.state.get().messages;if(!!w.some(T=>T.id===h.id))return;this.state.setPartial({messages:[...w,h]})}else(m=o.options)!=null&&m.value&&((C=o.options)==null?void 0:C.value.length)>0&&this.state.setPartial({suggestedReplies:o.options.value})}else{const h=d.toErrorMessage(((b=o==null?void 0:o.error)==null?void 0:b.message)||"Unknown error occurred"),w=this.state.get().messages;this.state.setPartial({messages:[...w,h]})}}catch(y){this.sendMessageAbortController.signal.aborted||console.error("Failed to send message:",y)}finally{this.state.setPartial({isSendingMessage:!1})}});s(this,"fetchAndSetHistory",async(e,t)=>{var r;const i=(r=this.state.get().messages.at(-1))==null?void 0:r.timestamp,{data:n}=await this.api.getSessionHistory({sessionId:e,lastMessageTimestamp:i,abortSignal:t});if(n&&n.length>0){const u=this.state.get().messages,m=n.map(d.mapHistoryToMessage).filter(C=>!u.some(b=>b.id===C.id));this.state.setPartial({messages:[...u,...m]})}});this.config=e,this.api=t,this.sessionCtx=i,this.registerPolling()}static mapHistoryToMessage(e){const t={id:e.publicId,timestamp:e.sentAt||"",attachments:e.attachments||void 0};return e.sender.kind==="user"?{...t,type:"FROM_USER",content:e.content.text||"",deliveredAt:e.sentAt||""}:e.sender.kind==="agent"?{...t,type:"FROM_AGENT",component:"agent_message",data:{message:e.content.text||""}}:{...t,type:"FROM_BOT",component:"bot_message",agent:{id:null,name:e.sender.name||"",isAi:e.sender.kind==="ai",avatar:e.sender.avatar||null},data:{message:e.content.text}}}static toUserMessage(e,t){return{id:P(),type:"FROM_USER",content:e,deliveredAt:new Date().toISOString(),attachments:t,timestamp:new Date().toISOString()}}static toBotMessage(e){if(e.success&&e.autopilotResponse)return{type:"FROM_BOT",id:e.autopilotResponse.id||P(),timestamp:new Date().toISOString(),component:"bot_message",data:{message:e.autopilotResponse.value.content}};if(e.success&&e.uiResponse){const t=e.uiResponse.value;return{type:"FROM_BOT",id:P(),timestamp:new Date().toISOString(),component:t.name,data:t.request_response}}return null}static toErrorMessage(e){return{type:"FROM_BOT",id:P(),timestamp:new Date().toISOString(),component:"TEXT",data:{message:e,variant:"error"}}}}class H{constructor({config:e}){s(this,"config");s(this,"api");s(this,"contactCtx");s(this,"sessionCtx");s(this,"messageCtx");s(this,"resetChat",()=>{this.sessionCtx.reset(),this.messageCtx.reset()});this.config=e,this.api=new q({config:e}),this.contactCtx=new B({api:this.api,config:this.config}),this.sessionCtx=new _(this.api),this.messageCtx=new d({config:this.config,api:this.api,sessionCtx:this.sessionCtx})}}exports.PubSub=f;exports.WidgetCtx=H;
2
+ //# sourceMappingURL=widget-DlAUwHzU.cjs.map