@botonic/webchat-core 2.23.0

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 (88) hide show
  1. package/CHANGELOG.md +247 -0
  2. package/PR-LOCALE-SUPPORT.md +156 -0
  3. package/README.md +11 -0
  4. package/package.json +29 -0
  5. package/src/index.d.ts +11 -0
  6. package/src/index.js +15 -0
  7. package/src/index.js.map +1 -0
  8. package/src/lib/controllers/index.d.ts +2 -0
  9. package/src/lib/controllers/index.js +3 -0
  10. package/src/lib/controllers/index.js.map +1 -0
  11. package/src/lib/controllers/public.d.ts +383 -0
  12. package/src/lib/controllers/public.js +194 -0
  13. package/src/lib/controllers/public.js.map +1 -0
  14. package/src/lib/controllers/webchat.d.ts +414 -0
  15. package/src/lib/controllers/webchat.js +255 -0
  16. package/src/lib/controllers/webchat.js.map +1 -0
  17. package/src/lib/domain/index.d.ts +50 -0
  18. package/src/lib/domain/index.js +11 -0
  19. package/src/lib/domain/index.js.map +1 -0
  20. package/src/lib/infra/clients/hubtype-api.d.ts +53 -0
  21. package/src/lib/infra/clients/hubtype-api.js +189 -0
  22. package/src/lib/infra/clients/hubtype-api.js.map +1 -0
  23. package/src/lib/infra/clients/index.d.ts +3 -0
  24. package/src/lib/infra/clients/index.js +4 -0
  25. package/src/lib/infra/clients/index.js.map +1 -0
  26. package/src/lib/infra/clients/realtime.d.ts +92 -0
  27. package/src/lib/infra/clients/realtime.js +277 -0
  28. package/src/lib/infra/clients/realtime.js.map +1 -0
  29. package/src/lib/infra/clients/types.d.ts +21 -0
  30. package/src/lib/infra/clients/types.js +2 -0
  31. package/src/lib/infra/clients/types.js.map +1 -0
  32. package/src/lib/infra/repositories/index.d.ts +1 -0
  33. package/src/lib/infra/repositories/index.js +2 -0
  34. package/src/lib/infra/repositories/index.js.map +1 -0
  35. package/src/lib/infra/repositories/storage/base-storage.d.ts +23 -0
  36. package/src/lib/infra/repositories/storage/base-storage.js +102 -0
  37. package/src/lib/infra/repositories/storage/base-storage.js.map +1 -0
  38. package/src/lib/infra/repositories/storage/browser-storage.d.ts +9 -0
  39. package/src/lib/infra/repositories/storage/browser-storage.js +46 -0
  40. package/src/lib/infra/repositories/storage/browser-storage.js.map +1 -0
  41. package/src/lib/infra/repositories/storage/factory.d.ts +2 -0
  42. package/src/lib/infra/repositories/storage/factory.js +20 -0
  43. package/src/lib/infra/repositories/storage/factory.js.map +1 -0
  44. package/src/lib/infra/repositories/storage/in-memory.d.ts +9 -0
  45. package/src/lib/infra/repositories/storage/in-memory.js +21 -0
  46. package/src/lib/infra/repositories/storage/in-memory.js.map +1 -0
  47. package/src/lib/infra/repositories/storage/index.d.ts +7 -0
  48. package/src/lib/infra/repositories/storage/index.js +7 -0
  49. package/src/lib/infra/repositories/storage/index.js.map +1 -0
  50. package/src/lib/infra/repositories/storage/local-storage.d.ts +4 -0
  51. package/src/lib/infra/repositories/storage/local-storage.js +7 -0
  52. package/src/lib/infra/repositories/storage/local-storage.js.map +1 -0
  53. package/src/lib/infra/repositories/storage/session-storage.d.ts +4 -0
  54. package/src/lib/infra/repositories/storage/session-storage.js +7 -0
  55. package/src/lib/infra/repositories/storage/session-storage.js.map +1 -0
  56. package/src/lib/infra/repositories/storage/types.d.ts +30 -0
  57. package/src/lib/infra/repositories/storage/types.js +2 -0
  58. package/src/lib/infra/repositories/storage/types.js.map +1 -0
  59. package/src/lib/machines/message.d.ts +200 -0
  60. package/src/lib/machines/message.js +210 -0
  61. package/src/lib/machines/message.js.map +1 -0
  62. package/src/lib/machines/overlay.d.ts +299 -0
  63. package/src/lib/machines/overlay.js +217 -0
  64. package/src/lib/machines/overlay.js.map +1 -0
  65. package/src/lib/machines/webchat.d.ts +641 -0
  66. package/src/lib/machines/webchat.js +768 -0
  67. package/src/lib/machines/webchat.js.map +1 -0
  68. package/src/lib/services/webchat-service.d.ts +125 -0
  69. package/src/lib/services/webchat-service.js +339 -0
  70. package/src/lib/services/webchat-service.js.map +1 -0
  71. package/src/lib/utils/locale-detection.d.ts +2 -0
  72. package/src/lib/utils/locale-detection.js +20 -0
  73. package/src/lib/utils/locale-detection.js.map +1 -0
  74. package/src/lib/utils/timezone-to-country.d.ts +1 -0
  75. package/src/lib/utils/timezone-to-country.js +105 -0
  76. package/src/lib/utils/timezone-to-country.js.map +1 -0
  77. package/src/lib/webview/constants.d.ts +10 -0
  78. package/src/lib/webview/constants.js +10 -0
  79. package/src/lib/webview/constants.js.map +1 -0
  80. package/src/lib/webview/index.d.ts +3 -0
  81. package/src/lib/webview/index.js +4 -0
  82. package/src/lib/webview/index.js.map +1 -0
  83. package/src/lib/webview/types.d.ts +35 -0
  84. package/src/lib/webview/types.js +16 -0
  85. package/src/lib/webview/types.js.map +1 -0
  86. package/src/lib/webview/utils.d.ts +9 -0
  87. package/src/lib/webview/utils.js +33 -0
  88. package/src/lib/webview/utils.js.map +1 -0
@@ -0,0 +1,414 @@
1
+ import type { HubtypeUser, WebchatMessage } from '@botonic/shared';
2
+ import { ActorRefFrom, EmittedFrom } from 'xstate';
3
+ import { ConfirmCloseOption, OverlayState, WebchatConfig } from '../domain';
4
+ import { type WebchatControllerContext, WebchatMachine } from '../machines/webchat';
5
+ export type WebchatEmittedEvent = EmittedFrom<typeof WebchatMachine>;
6
+ export declare class WebchatController {
7
+ machine: ActorRefFrom<typeof WebchatMachine>;
8
+ constructor(webchatConfig: WebchatConfig);
9
+ initialize(): Promise<void>;
10
+ sendMessage(message: WebchatMessage): void;
11
+ toggleWebchat(isOpen?: boolean): void;
12
+ getContext(): WebchatControllerContext;
13
+ getOperationalContext(): WebchatControllerContext | null;
14
+ get user(): HubtypeUser | null;
15
+ get messages(): WebchatMessage[];
16
+ get machineRef(): ActorRefFrom<typeof WebchatMachine>;
17
+ subscribe(callback: (state: unknown) => void): import("xstate").Subscription;
18
+ updateUser(user: Partial<HubtypeUser>): void;
19
+ setUserLocale(locale: string): void;
20
+ setUserCountry(country: string): void;
21
+ setSystemLocale(systemLocale: string): void;
22
+ setUserExtraData(extraData: Record<string, any>): void;
23
+ clearMessages(): void;
24
+ updateMessage(messageId: string, updates: Partial<WebchatMessage>): void;
25
+ deleteMessage(messageId: string): void;
26
+ setTyping(isTyping: boolean): void;
27
+ reconnect(): void;
28
+ setOnline(online: boolean): void;
29
+ open(): void;
30
+ close(): void;
31
+ toggle(): void;
32
+ getMessages(): WebchatMessage[];
33
+ getConnectionStatus(): string;
34
+ isOnline(): boolean;
35
+ isOpen(): boolean;
36
+ getUser(): HubtypeUser | null;
37
+ /**
38
+ * Get the overlay actor from context
39
+ */
40
+ private getOverlayActor;
41
+ /**
42
+ * Get the current overlay state from the overlay actor
43
+ */
44
+ private getOverlayState;
45
+ /**
46
+ * Overlay API - supports both webviews (iframe) and cover components (inline React)
47
+ */
48
+ overlay: {
49
+ /**
50
+ * Open a webview overlay (iframe-based external content)
51
+ */
52
+ openWebview: (url: string, options?: {
53
+ title?: string;
54
+ icon?: string;
55
+ allowClose?: boolean;
56
+ }) => void;
57
+ /**
58
+ * Open a cover overlay (inline React component)
59
+ */
60
+ openCover: (componentId: string, options?: {
61
+ title?: string;
62
+ icon?: string;
63
+ props?: Record<string, unknown>;
64
+ allowClose?: boolean;
65
+ }) => void;
66
+ /**
67
+ * Close any open overlay (works for both webview and cover)
68
+ */
69
+ close: () => void;
70
+ /**
71
+ * Update the overlay title
72
+ */
73
+ setTitle: (title: string) => void;
74
+ /**
75
+ * Update the overlay icon
76
+ */
77
+ setIcon: (icon: string) => void;
78
+ /**
79
+ * Set whether the close button is shown/allowed
80
+ */
81
+ setAllowClose: (allow: boolean) => void;
82
+ /**
83
+ * Set confirmation dialog before closing
84
+ */
85
+ setConfirmClose: (config: ConfirmCloseOption) => void;
86
+ /**
87
+ * Navigate to a new URL in the webview (only works for webview type)
88
+ */
89
+ navigate: (url: string) => void;
90
+ /**
91
+ * Send a payload from webview to bot (only works for webview type)
92
+ */
93
+ sendPayload: (payload: unknown) => void;
94
+ /**
95
+ * Get the current overlay state
96
+ */
97
+ getState: () => OverlayState | null;
98
+ /**
99
+ * Check if any overlay is open
100
+ */
101
+ isOpen: () => boolean;
102
+ /**
103
+ * Get the type of the currently open overlay
104
+ */
105
+ getType: () => "webview" | "cover" | null;
106
+ /**
107
+ * Check if overlay is a webview
108
+ */
109
+ isWebview: () => boolean;
110
+ /**
111
+ * Check if overlay is a cover component
112
+ */
113
+ isCover: () => boolean;
114
+ /**
115
+ * Get the overlay actor for direct access
116
+ */
117
+ getActor: () => import("xstate").ActorRef<import("xstate").MachineSnapshot<{
118
+ overlayState: OverlayState | null;
119
+ }, {
120
+ type: "OPEN_WEBVIEW";
121
+ url: string;
122
+ title?: string;
123
+ icon?: string;
124
+ allowClose?: boolean;
125
+ } | {
126
+ type: "OPEN_COVER";
127
+ componentId: string;
128
+ title?: string;
129
+ icon?: string;
130
+ props?: Record<string, unknown>;
131
+ allowClose?: boolean;
132
+ } | {
133
+ type: "CLOSE";
134
+ } | {
135
+ type: "SET_TITLE";
136
+ title: string;
137
+ } | {
138
+ type: "SET_ICON";
139
+ icon: string;
140
+ } | {
141
+ type: "SET_ALLOW_CLOSE";
142
+ allow: boolean;
143
+ } | {
144
+ type: "SET_CONFIRM_CLOSE";
145
+ config: ConfirmCloseOption;
146
+ } | {
147
+ type: "NAVIGATE";
148
+ url: string;
149
+ } | {
150
+ type: "SEND_PAYLOAD";
151
+ payload: unknown;
152
+ }, {}, "idle" | "open", string, import("xstate").NonReducibleUnknown, import("xstate").MetaObject, {
153
+ id: "overlay";
154
+ states: {
155
+ readonly idle: {};
156
+ readonly open: {};
157
+ };
158
+ }>, {
159
+ type: "OPEN_WEBVIEW";
160
+ url: string;
161
+ title?: string;
162
+ icon?: string;
163
+ allowClose?: boolean;
164
+ } | {
165
+ type: "OPEN_COVER";
166
+ componentId: string;
167
+ title?: string;
168
+ icon?: string;
169
+ props?: Record<string, unknown>;
170
+ allowClose?: boolean;
171
+ } | {
172
+ type: "CLOSE";
173
+ } | {
174
+ type: "SET_TITLE";
175
+ title: string;
176
+ } | {
177
+ type: "SET_ICON";
178
+ icon: string;
179
+ } | {
180
+ type: "SET_ALLOW_CLOSE";
181
+ allow: boolean;
182
+ } | {
183
+ type: "SET_CONFIRM_CLOSE";
184
+ config: ConfirmCloseOption;
185
+ } | {
186
+ type: "NAVIGATE";
187
+ url: string;
188
+ } | {
189
+ type: "SEND_PAYLOAD";
190
+ payload: unknown;
191
+ }, {
192
+ type: "WEBVIEW_OPENED";
193
+ url: string;
194
+ title: string;
195
+ } | {
196
+ type: "WEBVIEW_CLOSED";
197
+ } | {
198
+ type: "WEBVIEW_PAYLOAD";
199
+ payload: unknown;
200
+ } | {
201
+ type: "COVER_OPENED";
202
+ componentId: string;
203
+ title: string;
204
+ } | {
205
+ type: "COVER_CLOSED";
206
+ } | {
207
+ type: "OVERLAY_CLOSED";
208
+ }>;
209
+ };
210
+ send(event: any): void;
211
+ getState(): import("xstate").MachineSnapshot<WebchatControllerContext, {
212
+ type: "INITIALIZE";
213
+ } | {
214
+ type: "RETRY_INITIALIZATION";
215
+ } | {
216
+ type: "RETRY_CONNECTION";
217
+ } | {
218
+ type: "CONNECTION_STATUS_CHANGED";
219
+ status: string;
220
+ } | {
221
+ type: "RECONNECT";
222
+ } | {
223
+ type: "CANCEL_RECONNECT";
224
+ } | {
225
+ type: "SET_ONLINE";
226
+ online: boolean;
227
+ } | {
228
+ type: "SET_TYPING";
229
+ isTypingOn: boolean;
230
+ } | {
231
+ type: "TOGGLE_WEBCHAT";
232
+ isOpen?: boolean;
233
+ } | {
234
+ type: "SEND_MESSAGE";
235
+ message: WebchatMessage;
236
+ } | {
237
+ type: "MESSAGE_RECEIVED";
238
+ message: WebchatMessage;
239
+ } | {
240
+ type: "MESSAGE_STATUS_CHANGED";
241
+ messageId: string;
242
+ status: import("../..").WebchatMessageStatus;
243
+ message: WebchatMessage;
244
+ } | {
245
+ type: "UPDATE_USER";
246
+ user: Partial<HubtypeUser>;
247
+ } | {
248
+ type: "CLEAR_MESSAGES";
249
+ } | {
250
+ type: "UPDATE_MESSAGE";
251
+ messageId: string;
252
+ updates: Partial<WebchatMessage>;
253
+ } | {
254
+ type: "DELETE_MESSAGE";
255
+ messageId: string;
256
+ } | {
257
+ type: "PERSISTENCE.SAVE_MESSAGE";
258
+ } | {
259
+ type: "WEBVIEW.OPEN";
260
+ url: string;
261
+ title?: string;
262
+ }, {
263
+ [x: string]: import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
264
+ webchatService: import("../..").WebchatService;
265
+ storageRepository: import("../..").StorageRepository;
266
+ }, {
267
+ config: WebchatConfig;
268
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<import("../..").StoredMessage[], {
269
+ storageRepository: import("../..").StorageRepository;
270
+ appId: string;
271
+ userId: string;
272
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<HubtypeUser | null, {
273
+ storageRepository: import("../..").StorageRepository;
274
+ appId: string;
275
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
276
+ success: boolean;
277
+ }, {
278
+ webchatService: import("../..").WebchatService;
279
+ user: HubtypeUser;
280
+ messages: WebchatMessage[];
281
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
282
+ success: boolean;
283
+ }, {
284
+ webchatService: import("../..").WebchatService;
285
+ user: HubtypeUser;
286
+ messages: WebchatMessage[];
287
+ attempt: number;
288
+ }, import("xstate").EventObject>> | undefined;
289
+ }, "idle" | "connectionFailed" | "reconnecting" | "operational" | {
290
+ initializing: "initializingServices" | "loadingSession" | "loadingMessages" | "connectingToRealtime" | "spawningPersistedMessages";
291
+ }, string, import("xstate").NonReducibleUnknown, import("xstate").MetaObject, {
292
+ id: "webchat";
293
+ states: {
294
+ readonly idle: {};
295
+ readonly initializing: {
296
+ states: {
297
+ readonly initializingServices: {};
298
+ readonly loadingSession: {};
299
+ readonly loadingMessages: {};
300
+ readonly spawningPersistedMessages: {};
301
+ readonly connectingToRealtime: {};
302
+ };
303
+ };
304
+ readonly connectionFailed: {};
305
+ readonly reconnecting: {};
306
+ readonly operational: {};
307
+ };
308
+ }>;
309
+ on<T extends WebchatEmittedEvent['type']>(eventType: T, callback: (event: Extract<WebchatEmittedEvent, {
310
+ type: T;
311
+ }>) => void): () => void;
312
+ debug: {
313
+ getState: () => import("xstate").MachineSnapshot<WebchatControllerContext, {
314
+ type: "INITIALIZE";
315
+ } | {
316
+ type: "RETRY_INITIALIZATION";
317
+ } | {
318
+ type: "RETRY_CONNECTION";
319
+ } | {
320
+ type: "CONNECTION_STATUS_CHANGED";
321
+ status: string;
322
+ } | {
323
+ type: "RECONNECT";
324
+ } | {
325
+ type: "CANCEL_RECONNECT";
326
+ } | {
327
+ type: "SET_ONLINE";
328
+ online: boolean;
329
+ } | {
330
+ type: "SET_TYPING";
331
+ isTypingOn: boolean;
332
+ } | {
333
+ type: "TOGGLE_WEBCHAT";
334
+ isOpen?: boolean;
335
+ } | {
336
+ type: "SEND_MESSAGE";
337
+ message: WebchatMessage;
338
+ } | {
339
+ type: "MESSAGE_RECEIVED";
340
+ message: WebchatMessage;
341
+ } | {
342
+ type: "MESSAGE_STATUS_CHANGED";
343
+ messageId: string;
344
+ status: import("../..").WebchatMessageStatus;
345
+ message: WebchatMessage;
346
+ } | {
347
+ type: "UPDATE_USER";
348
+ user: Partial<HubtypeUser>;
349
+ } | {
350
+ type: "CLEAR_MESSAGES";
351
+ } | {
352
+ type: "UPDATE_MESSAGE";
353
+ messageId: string;
354
+ updates: Partial<WebchatMessage>;
355
+ } | {
356
+ type: "DELETE_MESSAGE";
357
+ messageId: string;
358
+ } | {
359
+ type: "PERSISTENCE.SAVE_MESSAGE";
360
+ } | {
361
+ type: "WEBVIEW.OPEN";
362
+ url: string;
363
+ title?: string;
364
+ }, {
365
+ [x: string]: import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
366
+ webchatService: import("../..").WebchatService;
367
+ storageRepository: import("../..").StorageRepository;
368
+ }, {
369
+ config: WebchatConfig;
370
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<import("../..").StoredMessage[], {
371
+ storageRepository: import("../..").StorageRepository;
372
+ appId: string;
373
+ userId: string;
374
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<HubtypeUser | null, {
375
+ storageRepository: import("../..").StorageRepository;
376
+ appId: string;
377
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
378
+ success: boolean;
379
+ }, {
380
+ webchatService: import("../..").WebchatService;
381
+ user: HubtypeUser;
382
+ messages: WebchatMessage[];
383
+ }, import("xstate").EventObject>> | import("xstate").ActorRefFromLogic<import("xstate").PromiseActorLogic<{
384
+ success: boolean;
385
+ }, {
386
+ webchatService: import("../..").WebchatService;
387
+ user: HubtypeUser;
388
+ messages: WebchatMessage[];
389
+ attempt: number;
390
+ }, import("xstate").EventObject>> | undefined;
391
+ }, "idle" | "connectionFailed" | "reconnecting" | "operational" | {
392
+ initializing: "initializingServices" | "loadingSession" | "loadingMessages" | "connectingToRealtime" | "spawningPersistedMessages";
393
+ }, string, import("xstate").NonReducibleUnknown, import("xstate").MetaObject, {
394
+ id: "webchat";
395
+ states: {
396
+ readonly idle: {};
397
+ readonly initializing: {
398
+ states: {
399
+ readonly initializingServices: {};
400
+ readonly loadingSession: {};
401
+ readonly loadingMessages: {};
402
+ readonly spawningPersistedMessages: {};
403
+ readonly connectingToRealtime: {};
404
+ };
405
+ };
406
+ readonly connectionFailed: {};
407
+ readonly reconnecting: {};
408
+ readonly operational: {};
409
+ };
410
+ }>;
411
+ logState: () => void;
412
+ };
413
+ destroy(): void;
414
+ }
@@ -0,0 +1,255 @@
1
+ import { __awaiter } from "tslib";
2
+ import { createWebchatMachine, } from '../machines/webchat';
3
+ export class WebchatController {
4
+ constructor(webchatConfig) {
5
+ /**
6
+ * Overlay API - supports both webviews (iframe) and cover components (inline React)
7
+ */
8
+ this.overlay = {
9
+ // ========================================================================
10
+ // Opening overlays
11
+ // ========================================================================
12
+ /**
13
+ * Open a webview overlay (iframe-based external content)
14
+ */
15
+ openWebview: (url, options) => this.getOverlayActor().send({
16
+ type: 'OPEN_WEBVIEW',
17
+ url,
18
+ title: options === null || options === void 0 ? void 0 : options.title,
19
+ icon: options === null || options === void 0 ? void 0 : options.icon,
20
+ allowClose: options === null || options === void 0 ? void 0 : options.allowClose,
21
+ }),
22
+ /**
23
+ * Open a cover overlay (inline React component)
24
+ */
25
+ openCover: (componentId, options) => this.getOverlayActor().send({
26
+ type: 'OPEN_COVER',
27
+ componentId,
28
+ title: options === null || options === void 0 ? void 0 : options.title,
29
+ icon: options === null || options === void 0 ? void 0 : options.icon,
30
+ props: options === null || options === void 0 ? void 0 : options.props,
31
+ allowClose: options === null || options === void 0 ? void 0 : options.allowClose,
32
+ }),
33
+ // ========================================================================
34
+ // Closing overlays
35
+ // ========================================================================
36
+ /**
37
+ * Close any open overlay (works for both webview and cover)
38
+ */
39
+ close: () => this.getOverlayActor().send({ type: 'CLOSE' }),
40
+ // ========================================================================
41
+ // Updating overlay properties
42
+ // ========================================================================
43
+ /**
44
+ * Update the overlay title
45
+ */
46
+ setTitle: (title) => this.getOverlayActor().send({ type: 'SET_TITLE', title }),
47
+ /**
48
+ * Update the overlay icon
49
+ */
50
+ setIcon: (icon) => this.getOverlayActor().send({ type: 'SET_ICON', icon }),
51
+ /**
52
+ * Set whether the close button is shown/allowed
53
+ */
54
+ setAllowClose: (allow) => this.getOverlayActor().send({ type: 'SET_ALLOW_CLOSE', allow }),
55
+ /**
56
+ * Set confirmation dialog before closing
57
+ */
58
+ setConfirmClose: (config) => this.getOverlayActor().send({ type: 'SET_CONFIRM_CLOSE', config }),
59
+ // ========================================================================
60
+ // Webview-specific operations
61
+ // ========================================================================
62
+ /**
63
+ * Navigate to a new URL in the webview (only works for webview type)
64
+ */
65
+ navigate: (url) => this.getOverlayActor().send({ type: 'NAVIGATE', url }),
66
+ /**
67
+ * Send a payload from webview to bot (only works for webview type)
68
+ */
69
+ sendPayload: (payload) => this.getOverlayActor().send({ type: 'SEND_PAYLOAD', payload }),
70
+ // ========================================================================
71
+ // State queries
72
+ // ========================================================================
73
+ /**
74
+ * Get the current overlay state
75
+ */
76
+ getState: () => this.getOverlayState(),
77
+ /**
78
+ * Check if any overlay is open
79
+ */
80
+ isOpen: () => { var _a; return ((_a = this.getOverlayState()) === null || _a === void 0 ? void 0 : _a.isOpen) || false; },
81
+ /**
82
+ * Get the type of the currently open overlay
83
+ */
84
+ getType: () => { var _a; return ((_a = this.getOverlayState()) === null || _a === void 0 ? void 0 : _a.type) || null; },
85
+ /**
86
+ * Check if overlay is a webview
87
+ */
88
+ isWebview: () => { var _a; return ((_a = this.getOverlayState()) === null || _a === void 0 ? void 0 : _a.type) === 'webview'; },
89
+ /**
90
+ * Check if overlay is a cover component
91
+ */
92
+ isCover: () => { var _a; return ((_a = this.getOverlayState()) === null || _a === void 0 ? void 0 : _a.type) === 'cover'; },
93
+ /**
94
+ * Get the overlay actor for direct access
95
+ */
96
+ getActor: () => this.getOverlayActor(),
97
+ };
98
+ this.debug = {
99
+ getState: () => this.getState(),
100
+ logState: () => {
101
+ const state = this.getState();
102
+ console.log('🐛 Current machine state:', {
103
+ value: state.value,
104
+ context: state.context,
105
+ isOpen: state.context.isOpen,
106
+ connectionStatus: state.context.connectionStatus,
107
+ messagesCount: this.messages.length,
108
+ });
109
+ },
110
+ };
111
+ const config = Object.assign(Object.assign({}, webchatConfig), { apiUrl: webchatConfig.apiUrl || import.meta.env.VITE_HUBTYPE_API_URL });
112
+ this.machine = createWebchatMachine(config);
113
+ this.machine.start();
114
+ }
115
+ initialize() {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ this.machine.send({ type: 'INITIALIZE' });
118
+ return new Promise((resolve, reject) => {
119
+ const subscription = this.machine.subscribe(state => {
120
+ if (state.value === 'operational') {
121
+ subscription.unsubscribe();
122
+ resolve();
123
+ }
124
+ else if (state.value === 'connectionFailed') {
125
+ subscription.unsubscribe();
126
+ reject(new Error('Initialization failed'));
127
+ }
128
+ });
129
+ });
130
+ });
131
+ }
132
+ sendMessage(message) {
133
+ this.machine.send({ type: 'SEND_MESSAGE', message });
134
+ }
135
+ toggleWebchat(isOpen) {
136
+ this.machine.send({ type: 'TOGGLE_WEBCHAT', isOpen });
137
+ }
138
+ getContext() {
139
+ return this.machine.getSnapshot().context;
140
+ }
141
+ getOperationalContext() {
142
+ const context = this.getContext();
143
+ return context.user ? context : null;
144
+ }
145
+ get user() {
146
+ return this.getContext().user || null;
147
+ }
148
+ get messages() {
149
+ const messageActors = this.getContext().messageActors || new Map();
150
+ const messages = [];
151
+ for (const [, actor] of messageActors) {
152
+ const snapshot = actor.getSnapshot();
153
+ messages.push(snapshot.context.message);
154
+ }
155
+ messages.sort((a, b) => b.id.localeCompare(a.id));
156
+ return messages;
157
+ }
158
+ get machineRef() {
159
+ return this.machine;
160
+ }
161
+ subscribe(callback) {
162
+ return this.machine.subscribe(callback);
163
+ }
164
+ updateUser(user) {
165
+ this.machine.send({ type: 'UPDATE_USER', user });
166
+ }
167
+ setUserLocale(locale) {
168
+ this.machine.send({ type: 'UPDATE_USER', user: { locale } });
169
+ }
170
+ setUserCountry(country) {
171
+ this.machine.send({ type: 'UPDATE_USER', user: { country } });
172
+ }
173
+ setSystemLocale(systemLocale) {
174
+ this.machine.send({
175
+ type: 'UPDATE_USER',
176
+ user: { system_locale: systemLocale },
177
+ });
178
+ }
179
+ setUserExtraData(extraData) {
180
+ this.machine.send({ type: 'UPDATE_USER', user: { extra_data: extraData } });
181
+ }
182
+ clearMessages() {
183
+ this.machine.send({ type: 'CLEAR_MESSAGES' });
184
+ }
185
+ updateMessage(messageId, updates) {
186
+ this.machine.send({ type: 'UPDATE_MESSAGE', messageId, updates });
187
+ }
188
+ deleteMessage(messageId) {
189
+ this.machine.send({ type: 'DELETE_MESSAGE', messageId });
190
+ }
191
+ setTyping(isTyping) {
192
+ this.machine.send({ type: 'SET_TYPING', isTypingOn: isTyping });
193
+ }
194
+ reconnect() {
195
+ this.machine.send({ type: 'RECONNECT' });
196
+ }
197
+ setOnline(online) {
198
+ this.machine.send({ type: 'SET_ONLINE', online });
199
+ }
200
+ open() {
201
+ this.toggleWebchat(true);
202
+ }
203
+ close() {
204
+ this.toggleWebchat(false);
205
+ }
206
+ toggle() {
207
+ this.toggleWebchat();
208
+ }
209
+ getMessages() {
210
+ return this.messages;
211
+ }
212
+ getConnectionStatus() {
213
+ return this.getContext().connectionStatus || 'disconnected';
214
+ }
215
+ isOnline() {
216
+ return this.getConnectionStatus() === 'connected';
217
+ }
218
+ isOpen() {
219
+ return this.getContext().isOpen || false;
220
+ }
221
+ getUser() {
222
+ return this.user;
223
+ }
224
+ /**
225
+ * Get the overlay actor from context
226
+ */
227
+ getOverlayActor() {
228
+ return this.getContext().overlayActor;
229
+ }
230
+ /**
231
+ * Get the current overlay state from the overlay actor
232
+ */
233
+ getOverlayState() {
234
+ var _a, _b;
235
+ return (_b = (_a = this.getOverlayActor()) === null || _a === void 0 ? void 0 : _a.getSnapshot().context.overlayState) !== null && _b !== void 0 ? _b : null;
236
+ }
237
+ send(event) {
238
+ this.machine.send(event);
239
+ }
240
+ getState() {
241
+ return this.machine.getSnapshot();
242
+ }
243
+ on(eventType, callback) {
244
+ const subscription = this.machine.on(eventType, emittedEvent => {
245
+ callback(emittedEvent);
246
+ });
247
+ return typeof subscription === 'function'
248
+ ? subscription
249
+ : () => subscription.unsubscribe();
250
+ }
251
+ destroy() {
252
+ this.machine.stop();
253
+ }
254
+ }
255
+ //# sourceMappingURL=webchat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webchat.js","sourceRoot":"","sources":["../../../../../../../libs/botonic/webchat-core/src/lib/controllers/webchat.ts"],"names":[],"mappings":";AAIA,OAAO,EACL,oBAAoB,GAG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