@aomi-labs/widget-lib 0.2.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.
package/dist/index.js ADDED
@@ -0,0 +1,3289 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function getDefaultExportFromCjs (x) {
6
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
7
+ }
8
+
9
+ var eventemitter3 = {exports: {}};
10
+
11
+ (function (module) {
12
+
13
+ var has = Object.prototype.hasOwnProperty
14
+ , prefix = '~';
15
+
16
+ /**
17
+ * Constructor to create a storage for our `EE` objects.
18
+ * An `Events` instance is a plain object whose properties are event names.
19
+ *
20
+ * @constructor
21
+ * @private
22
+ */
23
+ function Events() {}
24
+
25
+ //
26
+ // We try to not inherit from `Object.prototype`. In some engines creating an
27
+ // instance in this way is faster than calling `Object.create(null)` directly.
28
+ // If `Object.create(null)` is not supported we prefix the event names with a
29
+ // character to make sure that the built-in object properties are not
30
+ // overridden or used as an attack vector.
31
+ //
32
+ if (Object.create) {
33
+ Events.prototype = Object.create(null);
34
+
35
+ //
36
+ // This hack is needed because the `__proto__` property is still inherited in
37
+ // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
38
+ //
39
+ if (!new Events().__proto__) prefix = false;
40
+ }
41
+
42
+ /**
43
+ * Representation of a single event listener.
44
+ *
45
+ * @param {Function} fn The listener function.
46
+ * @param {*} context The context to invoke the listener with.
47
+ * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
48
+ * @constructor
49
+ * @private
50
+ */
51
+ function EE(fn, context, once) {
52
+ this.fn = fn;
53
+ this.context = context;
54
+ this.once = once || false;
55
+ }
56
+
57
+ /**
58
+ * Add a listener for a given event.
59
+ *
60
+ * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
61
+ * @param {(String|Symbol)} event The event name.
62
+ * @param {Function} fn The listener function.
63
+ * @param {*} context The context to invoke the listener with.
64
+ * @param {Boolean} once Specify if the listener is a one-time listener.
65
+ * @returns {EventEmitter}
66
+ * @private
67
+ */
68
+ function addListener(emitter, event, fn, context, once) {
69
+ if (typeof fn !== 'function') {
70
+ throw new TypeError('The listener must be a function');
71
+ }
72
+
73
+ var listener = new EE(fn, context || emitter, once)
74
+ , evt = prefix ? prefix + event : event;
75
+
76
+ if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
77
+ else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
78
+ else emitter._events[evt] = [emitter._events[evt], listener];
79
+
80
+ return emitter;
81
+ }
82
+
83
+ /**
84
+ * Clear event by name.
85
+ *
86
+ * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
87
+ * @param {(String|Symbol)} evt The Event name.
88
+ * @private
89
+ */
90
+ function clearEvent(emitter, evt) {
91
+ if (--emitter._eventsCount === 0) emitter._events = new Events();
92
+ else delete emitter._events[evt];
93
+ }
94
+
95
+ /**
96
+ * Minimal `EventEmitter` interface that is molded against the Node.js
97
+ * `EventEmitter` interface.
98
+ *
99
+ * @constructor
100
+ * @public
101
+ */
102
+ function EventEmitter() {
103
+ this._events = new Events();
104
+ this._eventsCount = 0;
105
+ }
106
+
107
+ /**
108
+ * Return an array listing the events for which the emitter has registered
109
+ * listeners.
110
+ *
111
+ * @returns {Array}
112
+ * @public
113
+ */
114
+ EventEmitter.prototype.eventNames = function eventNames() {
115
+ var names = []
116
+ , events
117
+ , name;
118
+
119
+ if (this._eventsCount === 0) return names;
120
+
121
+ for (name in (events = this._events)) {
122
+ if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
123
+ }
124
+
125
+ if (Object.getOwnPropertySymbols) {
126
+ return names.concat(Object.getOwnPropertySymbols(events));
127
+ }
128
+
129
+ return names;
130
+ };
131
+
132
+ /**
133
+ * Return the listeners registered for a given event.
134
+ *
135
+ * @param {(String|Symbol)} event The event name.
136
+ * @returns {Array} The registered listeners.
137
+ * @public
138
+ */
139
+ EventEmitter.prototype.listeners = function listeners(event) {
140
+ var evt = prefix ? prefix + event : event
141
+ , handlers = this._events[evt];
142
+
143
+ if (!handlers) return [];
144
+ if (handlers.fn) return [handlers.fn];
145
+
146
+ for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
147
+ ee[i] = handlers[i].fn;
148
+ }
149
+
150
+ return ee;
151
+ };
152
+
153
+ /**
154
+ * Return the number of listeners listening to a given event.
155
+ *
156
+ * @param {(String|Symbol)} event The event name.
157
+ * @returns {Number} The number of listeners.
158
+ * @public
159
+ */
160
+ EventEmitter.prototype.listenerCount = function listenerCount(event) {
161
+ var evt = prefix ? prefix + event : event
162
+ , listeners = this._events[evt];
163
+
164
+ if (!listeners) return 0;
165
+ if (listeners.fn) return 1;
166
+ return listeners.length;
167
+ };
168
+
169
+ /**
170
+ * Calls each of the listeners registered for a given event.
171
+ *
172
+ * @param {(String|Symbol)} event The event name.
173
+ * @returns {Boolean} `true` if the event had listeners, else `false`.
174
+ * @public
175
+ */
176
+ EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
177
+ var evt = prefix ? prefix + event : event;
178
+
179
+ if (!this._events[evt]) return false;
180
+
181
+ var listeners = this._events[evt]
182
+ , len = arguments.length
183
+ , args
184
+ , i;
185
+
186
+ if (listeners.fn) {
187
+ if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
188
+
189
+ switch (len) {
190
+ case 1: return listeners.fn.call(listeners.context), true;
191
+ case 2: return listeners.fn.call(listeners.context, a1), true;
192
+ case 3: return listeners.fn.call(listeners.context, a1, a2), true;
193
+ case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
194
+ case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
195
+ case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
196
+ }
197
+
198
+ for (i = 1, args = new Array(len -1); i < len; i++) {
199
+ args[i - 1] = arguments[i];
200
+ }
201
+
202
+ listeners.fn.apply(listeners.context, args);
203
+ } else {
204
+ var length = listeners.length
205
+ , j;
206
+
207
+ for (i = 0; i < length; i++) {
208
+ if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
209
+
210
+ switch (len) {
211
+ case 1: listeners[i].fn.call(listeners[i].context); break;
212
+ case 2: listeners[i].fn.call(listeners[i].context, a1); break;
213
+ case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
214
+ case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
215
+ default:
216
+ if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
217
+ args[j - 1] = arguments[j];
218
+ }
219
+
220
+ listeners[i].fn.apply(listeners[i].context, args);
221
+ }
222
+ }
223
+ }
224
+
225
+ return true;
226
+ };
227
+
228
+ /**
229
+ * Add a listener for a given event.
230
+ *
231
+ * @param {(String|Symbol)} event The event name.
232
+ * @param {Function} fn The listener function.
233
+ * @param {*} [context=this] The context to invoke the listener with.
234
+ * @returns {EventEmitter} `this`.
235
+ * @public
236
+ */
237
+ EventEmitter.prototype.on = function on(event, fn, context) {
238
+ return addListener(this, event, fn, context, false);
239
+ };
240
+
241
+ /**
242
+ * Add a one-time listener for a given event.
243
+ *
244
+ * @param {(String|Symbol)} event The event name.
245
+ * @param {Function} fn The listener function.
246
+ * @param {*} [context=this] The context to invoke the listener with.
247
+ * @returns {EventEmitter} `this`.
248
+ * @public
249
+ */
250
+ EventEmitter.prototype.once = function once(event, fn, context) {
251
+ return addListener(this, event, fn, context, true);
252
+ };
253
+
254
+ /**
255
+ * Remove the listeners of a given event.
256
+ *
257
+ * @param {(String|Symbol)} event The event name.
258
+ * @param {Function} fn Only remove the listeners that match this function.
259
+ * @param {*} context Only remove the listeners that have this context.
260
+ * @param {Boolean} once Only remove one-time listeners.
261
+ * @returns {EventEmitter} `this`.
262
+ * @public
263
+ */
264
+ EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
265
+ var evt = prefix ? prefix + event : event;
266
+
267
+ if (!this._events[evt]) return this;
268
+ if (!fn) {
269
+ clearEvent(this, evt);
270
+ return this;
271
+ }
272
+
273
+ var listeners = this._events[evt];
274
+
275
+ if (listeners.fn) {
276
+ if (
277
+ listeners.fn === fn &&
278
+ (!once || listeners.once) &&
279
+ (!context || listeners.context === context)
280
+ ) {
281
+ clearEvent(this, evt);
282
+ }
283
+ } else {
284
+ for (var i = 0, events = [], length = listeners.length; i < length; i++) {
285
+ if (
286
+ listeners[i].fn !== fn ||
287
+ (once && !listeners[i].once) ||
288
+ (context && listeners[i].context !== context)
289
+ ) {
290
+ events.push(listeners[i]);
291
+ }
292
+ }
293
+
294
+ //
295
+ // Reset the array, or remove it completely if we have no more listeners.
296
+ //
297
+ if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
298
+ else clearEvent(this, evt);
299
+ }
300
+
301
+ return this;
302
+ };
303
+
304
+ /**
305
+ * Remove all listeners, or those of the specified event.
306
+ *
307
+ * @param {(String|Symbol)} [event] The event name.
308
+ * @returns {EventEmitter} `this`.
309
+ * @public
310
+ */
311
+ EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
312
+ var evt;
313
+
314
+ if (event) {
315
+ evt = prefix ? prefix + event : event;
316
+ if (this._events[evt]) clearEvent(this, evt);
317
+ } else {
318
+ this._events = new Events();
319
+ this._eventsCount = 0;
320
+ }
321
+
322
+ return this;
323
+ };
324
+
325
+ //
326
+ // Alias methods names because people roll like that.
327
+ //
328
+ EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
329
+ EventEmitter.prototype.addListener = EventEmitter.prototype.on;
330
+
331
+ //
332
+ // Expose the prefix.
333
+ //
334
+ EventEmitter.prefixed = prefix;
335
+
336
+ //
337
+ // Allow `EventEmitter` to be imported as module namespace.
338
+ //
339
+ EventEmitter.EventEmitter = EventEmitter;
340
+
341
+ //
342
+ // Expose the module.
343
+ //
344
+ {
345
+ module.exports = EventEmitter;
346
+ }
347
+ } (eventemitter3));
348
+
349
+ var eventemitter3Exports = eventemitter3.exports;
350
+ var EventEmitter = /*@__PURE__*/getDefaultExportFromCjs(eventemitter3Exports);
351
+
352
+ // Constants for the Aomi Chat Widget
353
+ /*
354
+ * ============================================================================
355
+ * WIDGET DEFAULTS
356
+ * ============================================================================
357
+ */
358
+ const DEFAULT_WIDGET_WIDTH = '400px';
359
+ const DEFAULT_WIDGET_HEIGHT = '600px';
360
+ const DEFAULT_MAX_HEIGHT = 800;
361
+ const DEFAULT_MESSAGE_LENGTH = 2000;
362
+ const DEFAULT_RECONNECT_ATTEMPTS = 5;
363
+ const DEFAULT_RECONNECT_DELAY = 3000;
364
+ /*
365
+ * ============================================================================
366
+ * NETWORK CONSTANTS
367
+ * ============================================================================
368
+ */
369
+ const SUPPORTED_CHAINS = {
370
+ 1: 'Ethereum',
371
+ 5: 'Goerli',
372
+ 10: 'Optimism',
373
+ 100: 'Gnosis',
374
+ 1337: 'Localhost',
375
+ 31337: 'Anvil',
376
+ 137: 'Polygon',
377
+ 42161: 'Arbitrum One',
378
+ 59140: 'Linea Sepolia',
379
+ 59144: 'Linea',
380
+ 8453: 'Base',
381
+ 11155111: 'Sepolia',
382
+ };
383
+ const DEFAULT_CHAIN_ID = 1;
384
+ /*
385
+ * ============================================================================
386
+ * THEME PALETTES
387
+ * ============================================================================
388
+ */
389
+ const THEME_PALETTES = {
390
+ light: {
391
+ primary: '#007bff',
392
+ background: '#ffffff',
393
+ surface: '#f8f9fa',
394
+ text: '#212529',
395
+ textSecondary: '#6c757d',
396
+ border: '#dee2e6',
397
+ success: '#28a745',
398
+ error: '#dc3545',
399
+ warning: '#ffc107',
400
+ accent: '#6f42c1',
401
+ },
402
+ dark: {
403
+ primary: '#0d6efd',
404
+ background: '#121212',
405
+ surface: '#1e1e1e',
406
+ text: '#ffffff',
407
+ textSecondary: '#b3b3b3',
408
+ border: '#404040',
409
+ success: '#00d26a',
410
+ error: '#ff4757',
411
+ warning: '#ffa500',
412
+ accent: '#8b5cf6',
413
+ },
414
+ terminal: {
415
+ primary: '#00ff00',
416
+ background: '#0a0a0a',
417
+ surface: '#161b22',
418
+ text: '#00ff00',
419
+ textSecondary: '#22c55e',
420
+ border: '#30363d',
421
+ success: '#00ff88',
422
+ error: '#ff5555',
423
+ warning: '#ffff55',
424
+ accent: '#ff00ff',
425
+ },
426
+ neon: {
427
+ primary: '#ff007f',
428
+ background: '#0d0015',
429
+ surface: '#1a0033',
430
+ text: '#ffffff',
431
+ textSecondary: '#cc99ff',
432
+ border: '#4a0066',
433
+ success: '#00ffaa',
434
+ error: '#ff3366',
435
+ warning: '#ffaa00',
436
+ accent: '#00aaff',
437
+ },
438
+ minimal: {
439
+ primary: '#000000',
440
+ background: '#ffffff',
441
+ surface: '#fafafa',
442
+ text: '#333333',
443
+ textSecondary: '#888888',
444
+ border: '#e0e0e0',
445
+ success: '#4caf50',
446
+ error: '#f44336',
447
+ warning: '#ff9800',
448
+ accent: '#9c27b0',
449
+ },
450
+ };
451
+ /*
452
+ * ============================================================================
453
+ * COMPLETE THEME DEFINITIONS
454
+ * ============================================================================
455
+ */
456
+ const PREDEFINED_THEMES = {
457
+ light: {
458
+ name: 'Light',
459
+ palette: THEME_PALETTES.light,
460
+ fonts: {
461
+ primary: '"Segoe UI", "Roboto", "Helvetica Neue", Arial, sans-serif',
462
+ monospace: '"SF Mono", "Monaco", "Inconsolata", "Roboto Mono", monospace',
463
+ },
464
+ spacing: {
465
+ xs: '4px',
466
+ sm: '8px',
467
+ md: '16px',
468
+ lg: '24px',
469
+ xl: '32px',
470
+ },
471
+ borderRadius: {
472
+ sm: '4px',
473
+ md: '8px',
474
+ lg: '12px',
475
+ },
476
+ shadows: {
477
+ sm: '0 1px 3px rgba(0, 0, 0, 0.1)',
478
+ md: '0 4px 12px rgba(0, 0, 0, 0.15)',
479
+ lg: '0 8px 24px rgba(0, 0, 0, 0.2)',
480
+ },
481
+ },
482
+ dark: {
483
+ name: 'Dark',
484
+ palette: THEME_PALETTES.dark,
485
+ fonts: {
486
+ primary: '"Segoe UI", "Roboto", "Helvetica Neue", Arial, sans-serif',
487
+ monospace: '"SF Mono", "Monaco", "Inconsolata", "Roboto Mono", monospace',
488
+ },
489
+ spacing: {
490
+ xs: '4px',
491
+ sm: '8px',
492
+ md: '16px',
493
+ lg: '24px',
494
+ xl: '32px',
495
+ },
496
+ borderRadius: {
497
+ sm: '4px',
498
+ md: '8px',
499
+ lg: '12px',
500
+ },
501
+ shadows: {
502
+ sm: '0 1px 3px rgba(0, 0, 0, 0.3)',
503
+ md: '0 4px 12px rgba(0, 0, 0, 0.4)',
504
+ lg: '0 8px 24px rgba(0, 0, 0, 0.5)',
505
+ },
506
+ },
507
+ terminal: {
508
+ name: 'Terminal',
509
+ palette: THEME_PALETTES.terminal,
510
+ fonts: {
511
+ primary: '"SF Mono", "Monaco", "Inconsolata", "Roboto Mono", monospace',
512
+ monospace: '"SF Mono", "Monaco", "Inconsolata", "Roboto Mono", monospace',
513
+ },
514
+ spacing: {
515
+ xs: '2px',
516
+ sm: '4px',
517
+ md: '8px',
518
+ lg: '16px',
519
+ xl: '24px',
520
+ },
521
+ borderRadius: {
522
+ sm: '0px',
523
+ md: '2px',
524
+ lg: '4px',
525
+ },
526
+ shadows: {
527
+ sm: '0 0 8px rgba(0, 255, 0, 0.3)',
528
+ md: '0 0 16px rgba(0, 255, 0, 0.5)',
529
+ lg: '0 0 24px rgba(0, 255, 0, 0.7)',
530
+ },
531
+ },
532
+ neon: {
533
+ name: 'Neon',
534
+ palette: THEME_PALETTES.neon,
535
+ fonts: {
536
+ primary: '"Orbitron", "Rajdhani", "Exo 2", sans-serif',
537
+ monospace: '"Share Tech Mono", "Courier New", monospace',
538
+ },
539
+ spacing: {
540
+ xs: '4px',
541
+ sm: '8px',
542
+ md: '16px',
543
+ lg: '24px',
544
+ xl: '32px',
545
+ },
546
+ borderRadius: {
547
+ sm: '2px',
548
+ md: '6px',
549
+ lg: '10px',
550
+ },
551
+ shadows: {
552
+ sm: '0 0 8px rgba(255, 0, 127, 0.4)',
553
+ md: '0 0 16px rgba(255, 0, 127, 0.6)',
554
+ lg: '0 0 24px rgba(255, 0, 127, 0.8)',
555
+ },
556
+ },
557
+ minimal: {
558
+ name: 'Minimal',
559
+ palette: THEME_PALETTES.minimal,
560
+ fonts: {
561
+ primary: '"Inter", "Helvetica Neue", Arial, sans-serif',
562
+ monospace: '"JetBrains Mono", "SF Mono", monospace',
563
+ },
564
+ spacing: {
565
+ xs: '4px',
566
+ sm: '8px',
567
+ md: '16px',
568
+ lg: '24px',
569
+ xl: '32px',
570
+ },
571
+ borderRadius: {
572
+ sm: '2px',
573
+ md: '4px',
574
+ lg: '6px',
575
+ },
576
+ shadows: {
577
+ sm: '0 1px 2px rgba(0, 0, 0, 0.05)',
578
+ md: '0 2px 8px rgba(0, 0, 0, 0.1)',
579
+ lg: '0 4px 16px rgba(0, 0, 0, 0.15)',
580
+ },
581
+ },
582
+ };
583
+ /*
584
+ * ============================================================================
585
+ * ERROR CODES
586
+ * ============================================================================
587
+ */
588
+ const ERROR_CODES = {
589
+ // Configuration errors
590
+ INVALID_CONFIG: 'INVALID_CONFIG',
591
+ MISSING_APP_CODE: 'MISSING_APP_CODE',
592
+ INVALID_THEME: 'INVALID_THEME',
593
+ INVALID_DIMENSIONS: 'INVALID_DIMENSIONS',
594
+ // Connection errors
595
+ CONNECTION_FAILED: 'CONNECTION_FAILED',
596
+ CONNECTION_TIMEOUT: 'CONNECTION_TIMEOUT',
597
+ BACKEND_UNAVAILABLE: 'BACKEND_UNAVAILABLE',
598
+ AUTHENTICATION_FAILED: 'AUTHENTICATION_FAILED',
599
+ // Wallet errors
600
+ WALLET_NOT_CONNECTED: 'WALLET_NOT_CONNECTED',
601
+ WALLET_CONNECTION_FAILED: 'WALLET_CONNECTION_FAILED',
602
+ UNSUPPORTED_NETWORK: 'UNSUPPORTED_NETWORK',
603
+ TRANSACTION_FAILED: 'TRANSACTION_FAILED',
604
+ TRANSACTION_REJECTED: 'TRANSACTION_REJECTED',
605
+ // Chat errors
606
+ MESSAGE_TOO_LONG: 'MESSAGE_TOO_LONG',
607
+ RATE_LIMITED: 'RATE_LIMITED',
608
+ INVALID_MESSAGE: 'INVALID_MESSAGE',
609
+ SESSION_EXPIRED: 'SESSION_EXPIRED',
610
+ // General errors
611
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR',
612
+ INITIALIZATION_FAILED: 'INITIALIZATION_FAILED',
613
+ PROVIDER_ERROR: 'PROVIDER_ERROR',
614
+ };
615
+ /*
616
+ * ============================================================================
617
+ * EVENT NAMES
618
+ * ============================================================================
619
+ */
620
+ const WIDGET_EVENTS = {
621
+ // Widget lifecycle
622
+ READY: 'ready',
623
+ DESTROY: 'destroy',
624
+ RESIZE: 'resize',
625
+ // Chat events
626
+ MESSAGE: 'message',
627
+ TYPING_CHANGE: 'typingChange',
628
+ PROCESSING_CHANGE: 'processingChange',
629
+ // Connection events
630
+ CONNECTION_CHANGE: 'connectionChange',
631
+ SESSION_START: 'sessionStart',
632
+ SESSION_END: 'sessionEnd',
633
+ // Wallet events
634
+ WALLET_CONNECT: 'walletConnect',
635
+ WALLET_DISCONNECT: 'walletDisconnect',
636
+ NETWORK_CHANGE: 'networkChange',
637
+ TRANSACTION_REQUEST: 'transactionRequest',
638
+ // Error events
639
+ ERROR: 'error',
640
+ };
641
+ /*
642
+ * ============================================================================
643
+ * CSS CLASS NAMES
644
+ * ============================================================================
645
+ */
646
+ const CSS_CLASSES = {
647
+ // Root classes
648
+ WIDGET_ROOT: 'aomi-chat-widget',
649
+ WIDGET_CONTAINER: 'aomi-chat-container',
650
+ WIDGET_IFRAME: 'aomi-chat-iframe',
651
+ // Theme classes
652
+ THEME_LIGHT: 'aomi-theme-light',
653
+ THEME_DARK: 'aomi-theme-dark',
654
+ THEME_TERMINAL: 'aomi-theme-terminal',
655
+ THEME_NEON: 'aomi-theme-neon',
656
+ THEME_MINIMAL: 'aomi-theme-minimal',
657
+ // Mode classes
658
+ MODE_FULL: 'aomi-mode-full',
659
+ MODE_MINIMAL: 'aomi-mode-minimal',
660
+ MODE_COMPACT: 'aomi-mode-compact',
661
+ MODE_TERMINAL: 'aomi-mode-terminal',
662
+ // Component classes
663
+ CHAT_INTERFACE: 'aomi-chat-interface',
664
+ CHAT_HEADER: 'aomi-chat-header',
665
+ CHAT_TITLE: 'aomi-chat-title',
666
+ CHAT_BODY: 'aomi-chat-body',
667
+ STATUS_BADGE: 'aomi-status-badge',
668
+ MESSAGE_LIST: 'aomi-message-list',
669
+ MESSAGE_CONTAINER: 'aomi-message-container',
670
+ MESSAGE_BUBBLE: 'aomi-message',
671
+ MESSAGE_USER: 'aomi-message-user',
672
+ MESSAGE_ASSISTANT: 'aomi-message-assistant',
673
+ MESSAGE_SYSTEM: 'aomi-message-system',
674
+ ACTION_BAR: 'aomi-action-bar',
675
+ MESSAGE_INPUT: 'aomi-message-input',
676
+ INPUT_FORM: 'aomi-chat-input-form',
677
+ INPUT_FIELD: 'aomi-chat-input-field',
678
+ SEND_BUTTON: 'aomi-chat-send-button',
679
+ WALLET_STATUS: 'aomi-wallet-status',
680
+ TYPING_INDICATOR: 'aomi-typing-indicator',
681
+ // State classes
682
+ LOADING: 'aomi-loading',
683
+ ERROR: 'aomi-error',
684
+ DISABLED: 'aomi-disabled',
685
+ CONNECTED: 'aomi-connected',
686
+ DISCONNECTED: 'aomi-disconnected',
687
+ };
688
+ /*
689
+ * ============================================================================
690
+ * API ENDPOINTS
691
+ * ============================================================================
692
+ */
693
+ const API_ENDPOINTS = {
694
+ CHAT: '/api/chat',
695
+ CHAT_STREAM: '/api/chat/stream',
696
+ STATE: '/api/state',
697
+ INTERRUPT: '/api/interrupt',
698
+ SYSTEM: '/api/system',
699
+ MCP_COMMAND: '/api/mcp-command',
700
+ HEALTH: '/health',
701
+ };
702
+ /*
703
+ * ============================================================================
704
+ * TIMING CONSTANTS
705
+ * ============================================================================
706
+ */
707
+ const TIMING = {
708
+ TYPING_INDICATOR_DELAY: 100,
709
+ MESSAGE_ANIMATION_DURATION: 300,
710
+ CONNECTION_TIMEOUT: 10000,
711
+ RETRY_DELAY: 1000,
712
+ HEARTBEAT_INTERVAL: 30000,
713
+ SESSION_TIMEOUT: 3600000, // 1 hour
714
+ };
715
+
716
+ // Error types and classes for the Aomi Chat Widget
717
+ /*
718
+ * ============================================================================
719
+ * BASE ERROR CLASS
720
+ * ============================================================================
721
+ */
722
+ class AomiChatError extends Error {
723
+ constructor(code, message, details) {
724
+ super(message);
725
+ this.name = 'AomiChatError';
726
+ this.code = code;
727
+ this.timestamp = new Date();
728
+ this.details = details;
729
+ // Maintains proper stack trace for where our error was thrown (only available on V8)
730
+ if (Error.captureStackTrace) {
731
+ Error.captureStackTrace(this, AomiChatError);
732
+ }
733
+ }
734
+ toJSON() {
735
+ return {
736
+ name: this.name,
737
+ code: this.code,
738
+ message: this.message,
739
+ timestamp: this.timestamp.toISOString(),
740
+ details: this.details,
741
+ stack: this.stack,
742
+ };
743
+ }
744
+ toString() {
745
+ return `${this.name} [${this.code}]: ${this.message}`;
746
+ }
747
+ }
748
+ /*
749
+ * ============================================================================
750
+ * SPECIFIC ERROR CLASSES
751
+ * ============================================================================
752
+ */
753
+ class ConfigurationError extends AomiChatError {
754
+ constructor(message, details) {
755
+ super(ERROR_CODES.INVALID_CONFIG, message, details);
756
+ this.name = 'ConfigurationError';
757
+ }
758
+ }
759
+ class ConnectionError extends AomiChatError {
760
+ constructor(message, details) {
761
+ super(ERROR_CODES.CONNECTION_FAILED, message, details);
762
+ this.name = 'ConnectionError';
763
+ }
764
+ }
765
+ class WalletError extends AomiChatError {
766
+ constructor(code, message, details) {
767
+ super(code, message, details);
768
+ this.name = 'WalletError';
769
+ }
770
+ }
771
+ class TransactionError extends WalletError {
772
+ constructor(message, details) {
773
+ super(ERROR_CODES.TRANSACTION_FAILED, message, details);
774
+ this.name = 'TransactionError';
775
+ }
776
+ }
777
+ class ChatError extends AomiChatError {
778
+ constructor(code, message, details) {
779
+ super(code, message, details);
780
+ this.name = 'ChatError';
781
+ }
782
+ }
783
+ class RateLimitError extends ChatError {
784
+ constructor(message, details) {
785
+ super(ERROR_CODES.RATE_LIMITED, message, details);
786
+ this.name = 'RateLimitError';
787
+ }
788
+ }
789
+ class SessionError extends AomiChatError {
790
+ constructor(message, details) {
791
+ super(ERROR_CODES.SESSION_EXPIRED, message, details);
792
+ this.name = 'SessionError';
793
+ }
794
+ }
795
+ /*
796
+ * ============================================================================
797
+ * ERROR FACTORY FUNCTIONS
798
+ * ============================================================================
799
+ */
800
+ function createConfigurationError(message, details) {
801
+ return new ConfigurationError(message, details);
802
+ }
803
+ function createConnectionError(message, details) {
804
+ return new ConnectionError(message, details);
805
+ }
806
+ function createWalletError(code, message, details) {
807
+ return new WalletError(code, message, details);
808
+ }
809
+ function createTransactionError(message, details) {
810
+ return new TransactionError(message, details);
811
+ }
812
+ function createChatError(code, message, details) {
813
+ return new ChatError(code, message, details);
814
+ }
815
+ function createRateLimitError(message, details) {
816
+ return new RateLimitError(message, details);
817
+ }
818
+ function createSessionError(message, details) {
819
+ return new SessionError(message, details);
820
+ }
821
+ /*
822
+ * ============================================================================
823
+ * ERROR HANDLING UTILITIES
824
+ * ============================================================================
825
+ */
826
+ function isAomiChatError(error) {
827
+ return error instanceof AomiChatError;
828
+ }
829
+ function isConfigurationError(error) {
830
+ return error instanceof ConfigurationError;
831
+ }
832
+ function isConnectionError(error) {
833
+ return error instanceof ConnectionError;
834
+ }
835
+ function isWalletError(error) {
836
+ return error instanceof WalletError;
837
+ }
838
+ function isTransactionError(error) {
839
+ return error instanceof TransactionError;
840
+ }
841
+ function isChatError(error) {
842
+ return error instanceof ChatError;
843
+ }
844
+ function isRateLimitError(error) {
845
+ return error instanceof RateLimitError;
846
+ }
847
+ function isSessionError(error) {
848
+ return error instanceof SessionError;
849
+ }
850
+ /*
851
+ * ============================================================================
852
+ * ERROR SEVERITY LEVELS
853
+ * ============================================================================
854
+ */
855
+ /* eslint-disable no-unused-vars, @typescript-eslint/no-unused-vars */
856
+ var ErrorSeverity;
857
+ (function (ErrorSeverity) {
858
+ ErrorSeverity["LOW"] = "low";
859
+ ErrorSeverity["MEDIUM"] = "medium";
860
+ ErrorSeverity["HIGH"] = "high";
861
+ ErrorSeverity["CRITICAL"] = "critical";
862
+ })(ErrorSeverity || (ErrorSeverity = {}));
863
+ /*
864
+ * ============================================================================
865
+ * ERROR MESSAGES
866
+ * ============================================================================
867
+ */
868
+ ({
869
+ [ERROR_CODES.INVALID_CONFIG]: 'Invalid widget configuration provided',
870
+ [ERROR_CODES.MISSING_APP_CODE]: 'App code is required for widget initialization',
871
+ [ERROR_CODES.INVALID_THEME]: 'Invalid theme configuration',
872
+ [ERROR_CODES.INVALID_DIMENSIONS]: 'Invalid widget dimensions',
873
+ [ERROR_CODES.CONNECTION_FAILED]: 'Failed to connect to the chat backend',
874
+ [ERROR_CODES.CONNECTION_TIMEOUT]: 'Connection timed out',
875
+ [ERROR_CODES.BACKEND_UNAVAILABLE]: 'Chat backend is currently unavailable',
876
+ [ERROR_CODES.AUTHENTICATION_FAILED]: 'Authentication failed',
877
+ [ERROR_CODES.WALLET_NOT_CONNECTED]: 'Wallet is not connected',
878
+ [ERROR_CODES.WALLET_CONNECTION_FAILED]: 'Failed to connect to wallet',
879
+ [ERROR_CODES.UNSUPPORTED_NETWORK]: 'Unsupported network',
880
+ [ERROR_CODES.TRANSACTION_FAILED]: 'Transaction failed',
881
+ [ERROR_CODES.TRANSACTION_REJECTED]: 'Transaction was rejected by user',
882
+ [ERROR_CODES.MESSAGE_TOO_LONG]: 'Message exceeds maximum length',
883
+ [ERROR_CODES.RATE_LIMITED]: 'Rate limit exceeded, please slow down',
884
+ [ERROR_CODES.INVALID_MESSAGE]: 'Invalid message format',
885
+ [ERROR_CODES.SESSION_EXPIRED]: 'Chat session has expired',
886
+ [ERROR_CODES.UNKNOWN_ERROR]: 'An unknown error occurred',
887
+ [ERROR_CODES.INITIALIZATION_FAILED]: 'Widget initialization failed',
888
+ [ERROR_CODES.PROVIDER_ERROR]: 'Provider error occurred',
889
+ });
890
+ /*
891
+ * ============================================================================
892
+ * ERROR RECOVERY STRATEGIES
893
+ * ============================================================================
894
+ */
895
+ var RecoveryStrategy;
896
+ (function (RecoveryStrategy) {
897
+ RecoveryStrategy["RETRY"] = "retry";
898
+ RecoveryStrategy["RECONNECT"] = "reconnect";
899
+ RecoveryStrategy["REFRESH"] = "refresh";
900
+ RecoveryStrategy["FALLBACK"] = "fallback";
901
+ RecoveryStrategy["MANUAL"] = "manual";
902
+ RecoveryStrategy["NONE"] = "none";
903
+ })(RecoveryStrategy || (RecoveryStrategy = {}));
904
+
905
+ /* eslint-disable no-unused-vars, @typescript-eslint/no-unused-vars */
906
+ // Core type definitions for Aomi Chat Widget
907
+ /*
908
+ * ============================================================================
909
+ * WIDGET STATE TYPES
910
+ * ============================================================================
911
+ */
912
+ var ConnectionStatus;
913
+ (function (ConnectionStatus) {
914
+ ConnectionStatus["CONNECTING"] = "connecting";
915
+ ConnectionStatus["CONNECTED"] = "connected";
916
+ ConnectionStatus["DISCONNECTED"] = "disconnected";
917
+ ConnectionStatus["ERROR"] = "error";
918
+ ConnectionStatus["RECONNECTING"] = "reconnecting";
919
+ })(ConnectionStatus || (ConnectionStatus = {}));
920
+ var ReadinessPhase;
921
+ (function (ReadinessPhase) {
922
+ ReadinessPhase["INITIALIZING"] = "initializing";
923
+ ReadinessPhase["CONNECTING_MCP"] = "connecting_mcp";
924
+ ReadinessPhase["VALIDATING_ANTHROPIC"] = "validating_anthropic";
925
+ ReadinessPhase["READY"] = "ready";
926
+ ReadinessPhase["MISSING_API_KEY"] = "missing_api_key";
927
+ ReadinessPhase["ERROR"] = "error";
928
+ })(ReadinessPhase || (ReadinessPhase = {}));
929
+ /* eslint-enable no-unused-vars, @typescript-eslint/no-unused-vars */
930
+
931
+ // Utility functions for the Aomi Chat Widget
932
+ /*
933
+ * ============================================================================
934
+ * CONFIGURATION UTILITIES
935
+ * ============================================================================
936
+ */
937
+ /**
938
+ * Resolves a flexible configuration value based on chain ID and mode
939
+ */
940
+ function resolveFlexibleConfig(config, chainId, mode) {
941
+ // If it's a simple value, return it
942
+ if (typeof config !== 'object' || config === null) {
943
+ return config;
944
+ }
945
+ // Check if it's a per-mode configuration
946
+ if (isPerModeConfig(config)) {
947
+ const modeValue = config[mode];
948
+ if (modeValue === undefined) {
949
+ return undefined;
950
+ }
951
+ // If the mode value is itself a per-network config
952
+ if (isPerNetworkConfig(modeValue)) {
953
+ return modeValue[chainId];
954
+ }
955
+ return modeValue;
956
+ }
957
+ // Check if it's a per-network configuration
958
+ if (isPerNetworkConfig(config)) {
959
+ const networkValue = config[chainId];
960
+ if (networkValue === undefined) {
961
+ return undefined;
962
+ }
963
+ // If the network value is itself a per-mode config
964
+ if (isPerModeConfig(networkValue)) {
965
+ return networkValue[mode];
966
+ }
967
+ return networkValue;
968
+ }
969
+ return config;
970
+ }
971
+ function isPerModeConfig(config) {
972
+ if (typeof config !== 'object' || config === null)
973
+ return false;
974
+ const modes = ['full', 'minimal', 'compact', 'terminal'];
975
+ const keys = Object.keys(config);
976
+ return keys.length > 0 && keys.every(key => modes.includes(key));
977
+ }
978
+ function isPerNetworkConfig(config) {
979
+ if (typeof config !== 'object' || config === null)
980
+ return false;
981
+ const chainIds = Object.keys(SUPPORTED_CHAINS).map(id => parseInt(id));
982
+ const keys = Object.keys(config).map(key => parseInt(key));
983
+ return keys.length > 0 && keys.every(key => chainIds.includes(key));
984
+ }
985
+ /*
986
+ * ============================================================================
987
+ * VALIDATION UTILITIES
988
+ * ============================================================================
989
+ */
990
+ /**
991
+ * Validates widget configuration parameters
992
+ */
993
+ function validateWidgetParams(params) {
994
+ const errors = [];
995
+ // Required fields
996
+ if (!params.appCode || typeof params.appCode !== 'string') {
997
+ errors.push('appCode is required and must be a string');
998
+ }
999
+ // Validate dimensions
1000
+ if (params.width && !isValidDimension(params.width)) {
1001
+ errors.push('width must be a valid CSS dimension (e.g., "400px", "100%")');
1002
+ }
1003
+ if (params.height && !isValidDimension(params.height)) {
1004
+ errors.push('height must be a valid CSS dimension (e.g., "600px", "100vh")');
1005
+ }
1006
+ if (params.maxHeight && (typeof params.maxHeight !== 'number' || params.maxHeight <= 0)) {
1007
+ errors.push('maxHeight must be a positive number');
1008
+ }
1009
+ // Validate chain ID
1010
+ if (params.chainId && !Object.keys(SUPPORTED_CHAINS).includes(params.chainId.toString())) {
1011
+ errors.push(`chainId must be one of: ${Object.keys(SUPPORTED_CHAINS).join(', ')}`);
1012
+ }
1013
+ // Validate supported chains
1014
+ if (params.supportedChains) {
1015
+ const invalidChains = params.supportedChains.filter(id => !Object.keys(SUPPORTED_CHAINS).includes(id.toString()));
1016
+ if (invalidChains.length > 0) {
1017
+ errors.push(`supportedChains contains invalid chain IDs: ${invalidChains.join(', ')}`);
1018
+ }
1019
+ }
1020
+ // Validate theme
1021
+ if (params.theme && !isValidTheme(params.theme)) {
1022
+ errors.push('theme must be a valid theme name or theme palette object');
1023
+ }
1024
+ // Validate mode
1025
+ if (params.mode && !['full', 'minimal', 'compact', 'terminal'].includes(params.mode)) {
1026
+ errors.push('mode must be one of: full, minimal, compact, terminal');
1027
+ }
1028
+ return errors;
1029
+ }
1030
+ function isValidDimension(dimension) {
1031
+ // Simple regex to validate CSS dimensions
1032
+ return /^(\d+(\.\d+)?(px|%|em|rem|vh|vw)|auto|inherit)$/.test(dimension);
1033
+ }
1034
+ function isValidTheme(theme) {
1035
+ if (typeof theme === 'string') {
1036
+ return Object.keys(PREDEFINED_THEMES).includes(theme);
1037
+ }
1038
+ if (typeof theme === 'object' && theme !== null) {
1039
+ return 'baseTheme' in theme && typeof theme.baseTheme === 'string';
1040
+ }
1041
+ return false;
1042
+ }
1043
+ /*
1044
+ * ============================================================================
1045
+ * SESSION UTILITIES
1046
+ * ============================================================================
1047
+ */
1048
+ /**
1049
+ * Generates a unique session ID
1050
+ */
1051
+ function generateSessionId() {
1052
+ // Use crypto.randomUUID if available (modern browsers)
1053
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
1054
+ return crypto.randomUUID();
1055
+ }
1056
+ // Fallback UUID v4 implementation
1057
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
1058
+ const r = Math.random() * 16 | 0;
1059
+ const v = c === 'x' ? r : (r & 0x3 | 0x8);
1060
+ return v.toString(16);
1061
+ });
1062
+ }
1063
+ /**
1064
+ * Validates a session ID format
1065
+ */
1066
+ function isValidSessionId(sessionId) {
1067
+ // UUID v4 format
1068
+ const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
1069
+ return uuidRegex.test(sessionId);
1070
+ }
1071
+ /*
1072
+ * ============================================================================
1073
+ * URL UTILITIES
1074
+ * ============================================================================
1075
+ */
1076
+ /**
1077
+ * Builds widget URL with parameters
1078
+ */
1079
+ function buildWidgetUrl(baseUrl, params) {
1080
+ const url = new URL('/widget', baseUrl);
1081
+ // Add essential parameters
1082
+ url.searchParams.set('appCode', params.appCode);
1083
+ if (params.sessionId) {
1084
+ url.searchParams.set('sessionId', params.sessionId);
1085
+ }
1086
+ if (params.theme) {
1087
+ if (typeof params.theme === 'string') {
1088
+ url.searchParams.set('theme', params.theme);
1089
+ }
1090
+ else {
1091
+ // For complex theme objects, encode as JSON
1092
+ url.searchParams.set('palette', JSON.stringify(params.theme));
1093
+ }
1094
+ }
1095
+ if (params.mode) {
1096
+ url.searchParams.set('mode', params.mode);
1097
+ }
1098
+ if (params.chainId) {
1099
+ url.searchParams.set('chainId', params.chainId.toString());
1100
+ }
1101
+ if (params.welcomeMessage) {
1102
+ url.searchParams.set('welcomeMessage', params.welcomeMessage);
1103
+ }
1104
+ if (params.placeholder) {
1105
+ url.searchParams.set('placeholder', params.placeholder);
1106
+ }
1107
+ // Boolean flags
1108
+ if (params.showWalletStatus === false) {
1109
+ url.searchParams.set('showWalletStatus', 'false');
1110
+ }
1111
+ if (params.showNetworkSelector === false) {
1112
+ url.searchParams.set('showNetworkSelector', 'false');
1113
+ }
1114
+ if (params.hideHeader) {
1115
+ url.searchParams.set('hideHeader', 'true');
1116
+ }
1117
+ if (params.hideFooter) {
1118
+ url.searchParams.set('hideFooter', 'true');
1119
+ }
1120
+ if (params.enableTransactions === false) {
1121
+ url.searchParams.set('enableTransactions', 'false');
1122
+ }
1123
+ if (params.requireWalletConnection) {
1124
+ url.searchParams.set('requireWalletConnection', 'true');
1125
+ }
1126
+ if (params.standaloneMode === false) {
1127
+ url.searchParams.set('standaloneMode', 'false');
1128
+ }
1129
+ return url.toString();
1130
+ }
1131
+ /**
1132
+ * Parses widget parameters from URL search params
1133
+ */
1134
+ function parseWidgetParams(searchParams) {
1135
+ const params = {};
1136
+ const appCode = searchParams.get('appCode');
1137
+ if (appCode)
1138
+ params.appCode = appCode;
1139
+ const sessionId = searchParams.get('sessionId');
1140
+ if (sessionId)
1141
+ params.sessionId = sessionId;
1142
+ const theme = searchParams.get('theme');
1143
+ if (theme)
1144
+ params.theme = theme;
1145
+ const palette = searchParams.get('palette');
1146
+ if (palette) {
1147
+ try {
1148
+ params.theme = JSON.parse(palette);
1149
+ }
1150
+ catch {
1151
+ // Ignore invalid JSON
1152
+ }
1153
+ }
1154
+ const mode = searchParams.get('mode');
1155
+ if (mode)
1156
+ params.mode = mode;
1157
+ const chainId = searchParams.get('chainId');
1158
+ if (chainId) {
1159
+ const parsed = parseInt(chainId);
1160
+ if (!isNaN(parsed))
1161
+ params.chainId = parsed;
1162
+ }
1163
+ const welcomeMessage = searchParams.get('welcomeMessage');
1164
+ if (welcomeMessage)
1165
+ params.welcomeMessage = welcomeMessage;
1166
+ const placeholder = searchParams.get('placeholder');
1167
+ if (placeholder)
1168
+ params.placeholder = placeholder;
1169
+ // Boolean parameters
1170
+ const showWalletStatus = searchParams.get('showWalletStatus');
1171
+ if (showWalletStatus === 'false')
1172
+ params.showWalletStatus = false;
1173
+ const showNetworkSelector = searchParams.get('showNetworkSelector');
1174
+ if (showNetworkSelector === 'false')
1175
+ params.showNetworkSelector = false;
1176
+ const hideHeader = searchParams.get('hideHeader');
1177
+ if (hideHeader === 'true')
1178
+ params.hideHeader = true;
1179
+ const hideFooter = searchParams.get('hideFooter');
1180
+ if (hideFooter === 'true')
1181
+ params.hideFooter = true;
1182
+ const enableTransactions = searchParams.get('enableTransactions');
1183
+ if (enableTransactions === 'false')
1184
+ params.enableTransactions = false;
1185
+ const requireWalletConnection = searchParams.get('requireWalletConnection');
1186
+ if (requireWalletConnection === 'true')
1187
+ params.requireWalletConnection = true;
1188
+ const standaloneMode = searchParams.get('standaloneMode');
1189
+ if (standaloneMode === 'false')
1190
+ params.standaloneMode = false;
1191
+ return params;
1192
+ }
1193
+ /*
1194
+ * ============================================================================
1195
+ * DOM UTILITIES
1196
+ * ============================================================================
1197
+ */
1198
+ /**
1199
+ * Creates a DOM element with classes and attributes
1200
+ */
1201
+ function createElement(tagName, options = {}) {
1202
+ const element = document.createElement(tagName);
1203
+ // Set classes
1204
+ if (options.className) {
1205
+ if (Array.isArray(options.className)) {
1206
+ element.classList.add(...options.className);
1207
+ }
1208
+ else {
1209
+ element.className = options.className;
1210
+ }
1211
+ }
1212
+ // Set attributes
1213
+ if (options.attributes) {
1214
+ Object.entries(options.attributes).forEach(([key, value]) => {
1215
+ element.setAttribute(key, value);
1216
+ });
1217
+ }
1218
+ // Set styles
1219
+ if (options.styles) {
1220
+ Object.assign(element.style, options.styles);
1221
+ }
1222
+ // Add children
1223
+ if (options.children) {
1224
+ options.children.forEach(child => {
1225
+ if (typeof child === 'string') {
1226
+ element.appendChild(document.createTextNode(child));
1227
+ }
1228
+ else {
1229
+ element.appendChild(child);
1230
+ }
1231
+ });
1232
+ }
1233
+ return element;
1234
+ }
1235
+ /**
1236
+ * Checks if an element is visible in the viewport
1237
+ */
1238
+ function isElementVisible(element) {
1239
+ const rect = element.getBoundingClientRect();
1240
+ return (rect.top >= 0 &&
1241
+ rect.left >= 0 &&
1242
+ rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
1243
+ rect.right <= (window.innerWidth || document.documentElement.clientWidth));
1244
+ }
1245
+ /*
1246
+ * ============================================================================
1247
+ * TYPE GUARDS
1248
+ * ============================================================================
1249
+ */
1250
+ /**
1251
+ * Type guard to check if a value is a valid Ethereum address
1252
+ */
1253
+ function isEthereumAddress(value) {
1254
+ if (typeof value !== 'string')
1255
+ return false;
1256
+ return /^0x[a-fA-F0-9]{40}$/.test(value);
1257
+ }
1258
+ /**
1259
+ * Type guard to check if a value is a valid transaction hash
1260
+ */
1261
+ function isTransactionHash(value) {
1262
+ if (typeof value !== 'string')
1263
+ return false;
1264
+ return /^0x[a-fA-F0-9]{64}$/.test(value);
1265
+ }
1266
+ /**
1267
+ * Type guard to check if an object has a specific property
1268
+ */
1269
+ function hasProperty(obj, prop) {
1270
+ return prop in obj;
1271
+ }
1272
+ /*
1273
+ * ============================================================================
1274
+ * ASYNC UTILITIES
1275
+ * ============================================================================
1276
+ */
1277
+ /**
1278
+ * Delays execution for a specified number of milliseconds
1279
+ */
1280
+ function delay(ms) {
1281
+ return new Promise(resolve => setTimeout(resolve, ms));
1282
+ }
1283
+ /**
1284
+ * Creates a promise that rejects after a timeout
1285
+ */
1286
+ function withTimeout(promise, timeoutMs) {
1287
+ return Promise.race([
1288
+ promise,
1289
+ new Promise((_, reject) => setTimeout(() => reject(new Error('Operation timed out')), timeoutMs)),
1290
+ ]);
1291
+ }
1292
+ /**
1293
+ * Retries a function with exponential backoff
1294
+ */
1295
+ async function retry(fn, options = {}) {
1296
+ const { maxAttempts = 3, delay: initialDelay = 1000, backoffFactor = 2, shouldRetry = () => true, } = options;
1297
+ let lastError;
1298
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
1299
+ try {
1300
+ return await fn();
1301
+ }
1302
+ catch (error) {
1303
+ lastError = error;
1304
+ if (attempt === maxAttempts || !shouldRetry(error)) {
1305
+ throw error;
1306
+ }
1307
+ const delayMs = initialDelay * Math.pow(backoffFactor, attempt - 1);
1308
+ await delay(delayMs);
1309
+ }
1310
+ }
1311
+ throw lastError;
1312
+ }
1313
+ /*
1314
+ * ============================================================================
1315
+ * ENVIRONMENT UTILITIES
1316
+ * ============================================================================
1317
+ */
1318
+ /**
1319
+ * Detects if running in a browser environment
1320
+ */
1321
+ function isBrowser() {
1322
+ return typeof window !== 'undefined' && typeof document !== 'undefined';
1323
+ }
1324
+ /**
1325
+ * Detects if running on a mobile device
1326
+ */
1327
+ function isMobile() {
1328
+ if (!isBrowser())
1329
+ return false;
1330
+ return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
1331
+ }
1332
+ /**
1333
+ * Gets the current viewport dimensions
1334
+ */
1335
+ function getViewportDimensions() {
1336
+ if (!isBrowser()) {
1337
+ return { width: 0, height: 0 };
1338
+ }
1339
+ return {
1340
+ width: window.innerWidth || document.documentElement.clientWidth,
1341
+ height: window.innerHeight || document.documentElement.clientHeight,
1342
+ };
1343
+ }
1344
+ /*
1345
+ * ============================================================================
1346
+ * FORMATTING UTILITIES
1347
+ * ============================================================================
1348
+ */
1349
+ /**
1350
+ * Formats a timestamp as a human-readable string
1351
+ */
1352
+ function formatTimestamp(timestamp) {
1353
+ const now = new Date();
1354
+ const diff = now.getTime() - timestamp.getTime();
1355
+ if (diff < 60000) { // Less than 1 minute
1356
+ return 'just now';
1357
+ }
1358
+ else if (diff < 3600000) { // Less than 1 hour
1359
+ const minutes = Math.floor(diff / 60000);
1360
+ return `${minutes} minute${minutes > 1 ? 's' : ''} ago`;
1361
+ }
1362
+ else if (diff < 86400000) { // Less than 1 day
1363
+ const hours = Math.floor(diff / 3600000);
1364
+ return `${hours} hour${hours > 1 ? 's' : ''} ago`;
1365
+ }
1366
+ else {
1367
+ return timestamp.toLocaleDateString();
1368
+ }
1369
+ }
1370
+ /**
1371
+ * Truncates an Ethereum address for display
1372
+ */
1373
+ function truncateAddress(address, startChars = 6, endChars = 4) {
1374
+ if (!isEthereumAddress(address))
1375
+ return address;
1376
+ if (address.length <= startChars + endChars)
1377
+ return address;
1378
+ return `${address.slice(0, startChars)}...${address.slice(-endChars)}`;
1379
+ }
1380
+ /**
1381
+ * Formats a number with appropriate decimal places
1382
+ */
1383
+ function formatNumber(value, options = {}) {
1384
+ const { decimals = 2, compact = false, currency } = options;
1385
+ const formatter = new Intl.NumberFormat('en-US', {
1386
+ minimumFractionDigits: decimals,
1387
+ maximumFractionDigits: decimals,
1388
+ notation: compact ? 'compact' : 'standard',
1389
+ style: currency ? 'currency' : 'decimal',
1390
+ currency: currency || 'USD',
1391
+ });
1392
+ return formatter.format(value);
1393
+ }
1394
+
1395
+ /*
1396
+ * ChatManager - Aligns widget behaviour with the updated frontend core logic
1397
+ */
1398
+ class ChatManager extends EventEmitter {
1399
+ constructor(config = {}) {
1400
+ super();
1401
+ this.eventSource = null;
1402
+ this.reconnectAttempt = 0;
1403
+ this.reconnectTimer = null;
1404
+ this.heartbeatTimer = null;
1405
+ this.lastPendingTransactionRaw = null;
1406
+ this.config = {
1407
+ backendUrl: config.backendUrl || 'http://localhost:8080',
1408
+ sessionId: config.sessionId || generateSessionId(),
1409
+ maxMessageLength: config.maxMessageLength || 2000,
1410
+ reconnectAttempts: config.reconnectAttempts || 5,
1411
+ reconnectDelay: config.reconnectDelay || 3000,
1412
+ };
1413
+ this.state = {
1414
+ messages: [],
1415
+ isTyping: false,
1416
+ isProcessing: false,
1417
+ connectionStatus: ConnectionStatus.DISCONNECTED,
1418
+ readiness: {
1419
+ phase: ReadinessPhase.INITIALIZING,
1420
+ },
1421
+ walletState: {
1422
+ isConnected: false,
1423
+ },
1424
+ sessionId: this.config.sessionId,
1425
+ pendingTransaction: undefined,
1426
+ };
1427
+ }
1428
+ /*
1429
+ * ============================================================================
1430
+ * PUBLIC API
1431
+ * ============================================================================
1432
+ */
1433
+ getState() {
1434
+ return {
1435
+ ...this.state,
1436
+ messages: [...this.state.messages],
1437
+ walletState: { ...this.state.walletState },
1438
+ };
1439
+ }
1440
+ getSessionId() {
1441
+ return this.config.sessionId;
1442
+ }
1443
+ setSessionId(sessionId) {
1444
+ this.config.sessionId = sessionId;
1445
+ this.state.sessionId = sessionId;
1446
+ if (this.state.connectionStatus === ConnectionStatus.CONNECTED) {
1447
+ void this.connectSSE();
1448
+ }
1449
+ }
1450
+ async connectSSE() {
1451
+ this.setConnectionStatus(ConnectionStatus.CONNECTING);
1452
+ this.updateReadiness({ phase: ReadinessPhase.CONNECTING_MCP });
1453
+ this.teardownEventSource();
1454
+ try {
1455
+ const streamUrl = `${this.config.backendUrl}${API_ENDPOINTS.CHAT_STREAM}?session_id=${encodeURIComponent(this.config.sessionId)}`;
1456
+ this.eventSource = new EventSource(streamUrl);
1457
+ this.eventSource.onopen = () => {
1458
+ this.setConnectionStatus(ConnectionStatus.CONNECTED);
1459
+ this.reconnectAttempt = 0;
1460
+ this.startHeartbeat();
1461
+ void this.refreshState();
1462
+ };
1463
+ this.eventSource.onmessage = (event) => {
1464
+ try {
1465
+ const payload = JSON.parse(event.data);
1466
+ this.processBackendPayload(payload);
1467
+ }
1468
+ catch (error) {
1469
+ console.error('Failed to parse SSE payload:', error);
1470
+ this.emit('error', createChatError(ERROR_CODES.INVALID_MESSAGE, 'Invalid message format'));
1471
+ }
1472
+ };
1473
+ this.eventSource.onerror = (event) => {
1474
+ console.error('SSE connection error:', event);
1475
+ if (this.state.isProcessing) {
1476
+ this.state.isProcessing = false;
1477
+ this.state.isTyping = false;
1478
+ this.emitStateChange();
1479
+ }
1480
+ this.handleConnectionError();
1481
+ void this.refreshState();
1482
+ };
1483
+ }
1484
+ catch (error) {
1485
+ console.error('Failed to create SSE connection:', error);
1486
+ this.emit('error', createConnectionError('Failed to establish connection'));
1487
+ this.handleConnectionError();
1488
+ }
1489
+ }
1490
+ disconnectSSE() {
1491
+ this.teardownEventSource();
1492
+ this.setConnectionStatus(ConnectionStatus.DISCONNECTED);
1493
+ }
1494
+ async sendMessage(message) {
1495
+ const content = message.trim();
1496
+ if (!content) {
1497
+ throw createChatError(ERROR_CODES.INVALID_MESSAGE, 'Message cannot be empty');
1498
+ }
1499
+ if (content.length > (this.config.maxMessageLength ?? 0)) {
1500
+ throw createChatError(ERROR_CODES.MESSAGE_TOO_LONG, `Message exceeds ${this.config.maxMessageLength} characters`);
1501
+ }
1502
+ if (this.state.connectionStatus !== ConnectionStatus.CONNECTED) {
1503
+ throw createConnectionError('Not connected to backend');
1504
+ }
1505
+ try {
1506
+ const response = await this.postToBackend(API_ENDPOINTS.CHAT, {
1507
+ message: content,
1508
+ session_id: this.config.sessionId,
1509
+ });
1510
+ this.processBackendPayload(response);
1511
+ }
1512
+ catch (error) {
1513
+ console.error('Failed to send message:', error);
1514
+ throw createChatError(ERROR_CODES.UNKNOWN_ERROR, 'Failed to send message');
1515
+ }
1516
+ }
1517
+ async sendSystemMessage(message) {
1518
+ try {
1519
+ const response = await this.postToBackend(API_ENDPOINTS.SYSTEM, {
1520
+ message,
1521
+ session_id: this.config.sessionId,
1522
+ });
1523
+ this.processBackendPayload(response);
1524
+ }
1525
+ catch (error) {
1526
+ console.error('Failed to send system message:', error);
1527
+ throw createChatError(ERROR_CODES.UNKNOWN_ERROR, 'Failed to send system message');
1528
+ }
1529
+ }
1530
+ async interrupt() {
1531
+ try {
1532
+ const response = await this.postToBackend(API_ENDPOINTS.INTERRUPT, {
1533
+ session_id: this.config.sessionId,
1534
+ });
1535
+ this.processBackendPayload(response);
1536
+ }
1537
+ catch (error) {
1538
+ console.error('Failed to interrupt:', error);
1539
+ throw createChatError(ERROR_CODES.UNKNOWN_ERROR, 'Failed to interrupt processing');
1540
+ }
1541
+ }
1542
+ async sendNetworkSwitchRequest(networkName) {
1543
+ try {
1544
+ await this.sendSystemMessage(`Detected user's wallet connected to ${networkName} network`);
1545
+ return {
1546
+ success: true,
1547
+ message: `Network switch system message sent for ${networkName}`,
1548
+ data: { network: networkName },
1549
+ };
1550
+ }
1551
+ catch (error) {
1552
+ const err = error instanceof Error ? error.message : String(error);
1553
+ return {
1554
+ success: false,
1555
+ message: err,
1556
+ };
1557
+ }
1558
+ }
1559
+ async sendTransactionResult(success, transactionHash, error) {
1560
+ const message = success
1561
+ ? `Transaction sent: ${transactionHash}`
1562
+ : `Transaction rejected by user${error ? `: ${error}` : ''}`;
1563
+ try {
1564
+ await this.sendSystemMessage(message);
1565
+ }
1566
+ catch (err) {
1567
+ console.error('Failed to send transaction result:', err);
1568
+ }
1569
+ finally {
1570
+ this.clearPendingTransaction();
1571
+ }
1572
+ }
1573
+ clearPendingTransaction() {
1574
+ if (this.state.pendingTransaction) {
1575
+ this.state.pendingTransaction = undefined;
1576
+ this.lastPendingTransactionRaw = null;
1577
+ this.emitStateChange();
1578
+ }
1579
+ }
1580
+ clearMessages() {
1581
+ if (this.state.messages.length === 0)
1582
+ return;
1583
+ this.state.messages = [];
1584
+ this.emitStateChange();
1585
+ }
1586
+ updateWalletState(walletState) {
1587
+ this.state.walletState = { ...this.state.walletState, ...walletState };
1588
+ this.emitStateChange();
1589
+ }
1590
+ destroy() {
1591
+ this.disconnectSSE();
1592
+ this.removeAllListeners();
1593
+ }
1594
+ /*
1595
+ * ============================================================================
1596
+ * INTERNAL STATE HANDLING
1597
+ * ============================================================================
1598
+ */
1599
+ processBackendPayload(payload) {
1600
+ if (!payload) {
1601
+ return;
1602
+ }
1603
+ const previousMessages = this.state.messages;
1604
+ let stateChanged = false;
1605
+ if (Array.isArray(payload.messages)) {
1606
+ const nextMessages = this.buildMessages(payload.messages, previousMessages);
1607
+ this.state.messages = nextMessages;
1608
+ stateChanged = true;
1609
+ nextMessages.forEach((message, index) => {
1610
+ const previous = previousMessages[index];
1611
+ const isNewMessage = !previous || previous.id !== message.id;
1612
+ const contentChanged = previous && previous.content !== message.content;
1613
+ const toolStreamChanged = previous && !areToolStreamsEqual(previous.toolStream, message.toolStream);
1614
+ if (isNewMessage || contentChanged || toolStreamChanged) {
1615
+ this.emit('message', message);
1616
+ }
1617
+ });
1618
+ }
1619
+ else if (payload.messages === null) {
1620
+ if (previousMessages.length > 0) {
1621
+ this.state.messages = [];
1622
+ stateChanged = true;
1623
+ }
1624
+ }
1625
+ else if (payload.messages !== undefined) {
1626
+ console.error('Received malformed messages payload from backend:', payload.messages);
1627
+ }
1628
+ if (payload.is_processing !== undefined) {
1629
+ const processing = Boolean(payload.is_processing);
1630
+ if (processing !== this.state.isProcessing) {
1631
+ this.state.isProcessing = processing;
1632
+ stateChanged = true;
1633
+ }
1634
+ if (this.state.isTyping !== processing) {
1635
+ this.state.isTyping = processing;
1636
+ stateChanged = true;
1637
+ }
1638
+ }
1639
+ if (payload.readiness !== undefined) {
1640
+ const readiness = this.normaliseReadiness(payload.readiness);
1641
+ if (readiness) {
1642
+ const current = this.state.readiness;
1643
+ if (current.phase !== readiness.phase ||
1644
+ current.detail !== readiness.detail) {
1645
+ this.state.readiness = readiness;
1646
+ stateChanged = true;
1647
+ }
1648
+ }
1649
+ }
1650
+ if (payload.pending_wallet_tx !== undefined) {
1651
+ if (payload.pending_wallet_tx === null) {
1652
+ if (this.state.pendingTransaction) {
1653
+ this.state.pendingTransaction = undefined;
1654
+ this.lastPendingTransactionRaw = null;
1655
+ stateChanged = true;
1656
+ }
1657
+ }
1658
+ else if (payload.pending_wallet_tx !== this.lastPendingTransactionRaw) {
1659
+ try {
1660
+ const transaction = JSON.parse(payload.pending_wallet_tx);
1661
+ this.state.pendingTransaction = transaction;
1662
+ this.lastPendingTransactionRaw = payload.pending_wallet_tx;
1663
+ this.emit('transactionRequest', transaction);
1664
+ stateChanged = true;
1665
+ }
1666
+ catch (error) {
1667
+ console.error('Failed to parse wallet transaction payload:', error);
1668
+ }
1669
+ }
1670
+ }
1671
+ if ('pending_wallet_tx' in payload) {
1672
+ const raw = payload.pending_wallet_tx ?? null;
1673
+ if (raw === null) {
1674
+ if (this.state.pendingTransaction) {
1675
+ this.state.pendingTransaction = undefined;
1676
+ this.lastPendingTransactionRaw = null;
1677
+ stateChanged = true;
1678
+ }
1679
+ }
1680
+ else if (raw !== this.lastPendingTransactionRaw) {
1681
+ this.lastPendingTransactionRaw = raw;
1682
+ try {
1683
+ const parsed = JSON.parse(raw);
1684
+ this.state.pendingTransaction = parsed;
1685
+ stateChanged = true;
1686
+ this.emit('transactionRequest', parsed);
1687
+ }
1688
+ catch (error) {
1689
+ console.error('Failed to parse wallet transaction request:', error);
1690
+ }
1691
+ }
1692
+ }
1693
+ if (stateChanged) {
1694
+ this.emitStateChange();
1695
+ }
1696
+ }
1697
+ buildMessages(messages, previousMessages) {
1698
+ return messages
1699
+ .filter((msg) => Boolean(msg))
1700
+ .map((msg, index) => {
1701
+ const type = this.normaliseSender(msg.sender);
1702
+ const content = msg.content ?? '';
1703
+ const timestamp = this.parseTimestamp(msg.timestamp);
1704
+ const toolStream = normaliseToolStream(msg.tool_stream);
1705
+ const previous = previousMessages[index];
1706
+ const id = previous ? previous.id : generateSessionId();
1707
+ return {
1708
+ id,
1709
+ type,
1710
+ content,
1711
+ timestamp,
1712
+ toolStream,
1713
+ };
1714
+ });
1715
+ }
1716
+ normaliseSender(sender) {
1717
+ switch (sender) {
1718
+ case 'user':
1719
+ return 'user';
1720
+ case 'system':
1721
+ return 'system';
1722
+ default:
1723
+ return 'assistant';
1724
+ }
1725
+ }
1726
+ parseTimestamp(timestamp) {
1727
+ if (!timestamp) {
1728
+ return new Date();
1729
+ }
1730
+ const parsed = new Date(timestamp);
1731
+ if (Number.isNaN(parsed.valueOf())) {
1732
+ return new Date();
1733
+ }
1734
+ return parsed;
1735
+ }
1736
+ normaliseReadiness(payload) {
1737
+ if (!payload || typeof payload.phase !== 'string') {
1738
+ return null;
1739
+ }
1740
+ const detailRaw = typeof payload.detail === 'string' && payload.detail.trim().length > 0
1741
+ ? payload.detail.trim()
1742
+ : typeof payload.message === 'string' && payload.message.trim().length > 0
1743
+ ? payload.message.trim()
1744
+ : undefined;
1745
+ switch (payload.phase) {
1746
+ case 'connecting_mcp':
1747
+ return { phase: ReadinessPhase.CONNECTING_MCP, detail: detailRaw };
1748
+ case 'validating_anthropic':
1749
+ return { phase: ReadinessPhase.VALIDATING_ANTHROPIC, detail: detailRaw };
1750
+ case 'ready':
1751
+ return { phase: ReadinessPhase.READY, detail: detailRaw };
1752
+ case 'missing_api_key':
1753
+ return { phase: ReadinessPhase.MISSING_API_KEY, detail: detailRaw };
1754
+ case 'error':
1755
+ return { phase: ReadinessPhase.ERROR, detail: detailRaw };
1756
+ default:
1757
+ return { phase: ReadinessPhase.ERROR, detail: detailRaw };
1758
+ }
1759
+ }
1760
+ updateReadiness(readiness) {
1761
+ this.state.readiness = readiness;
1762
+ this.emitStateChange();
1763
+ }
1764
+ setConnectionStatus(status) {
1765
+ if (this.state.connectionStatus !== status) {
1766
+ this.state.connectionStatus = status;
1767
+ this.emit('connectionChange', status);
1768
+ this.emitStateChange();
1769
+ }
1770
+ }
1771
+ emitStateChange() {
1772
+ this.emit('stateChange', this.getState());
1773
+ }
1774
+ /*
1775
+ * ============================================================================
1776
+ * BACKEND COMMUNICATION
1777
+ * ============================================================================
1778
+ */
1779
+ async postToBackend(endpoint, data) {
1780
+ const url = `${this.config.backendUrl}${endpoint}`;
1781
+ try {
1782
+ const response = await withTimeout(fetch(url, {
1783
+ method: 'POST',
1784
+ headers: {
1785
+ 'Content-Type': 'application/json',
1786
+ },
1787
+ body: JSON.stringify(data),
1788
+ }), TIMING.CONNECTION_TIMEOUT);
1789
+ if (!response.ok) {
1790
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1791
+ }
1792
+ return await response.json();
1793
+ }
1794
+ catch (error) {
1795
+ if (error instanceof Error && error.message.includes('timed out')) {
1796
+ throw createConnectionError('Request timed out');
1797
+ }
1798
+ throw createConnectionError(`Request failed: ${error}`);
1799
+ }
1800
+ }
1801
+ async refreshState() {
1802
+ try {
1803
+ const response = await withTimeout(fetch(`${this.config.backendUrl}${API_ENDPOINTS.STATE}?session_id=${encodeURIComponent(this.config.sessionId)}`), TIMING.CONNECTION_TIMEOUT);
1804
+ if (!response.ok) {
1805
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
1806
+ }
1807
+ const payload = await response.json();
1808
+ this.processBackendPayload(payload);
1809
+ }
1810
+ catch (error) {
1811
+ console.warn('Failed to refresh chat state:', error);
1812
+ }
1813
+ }
1814
+ /*
1815
+ * ============================================================================
1816
+ * CONNECTION MANAGEMENT
1817
+ * ============================================================================
1818
+ */
1819
+ handleConnectionError() {
1820
+ this.setConnectionStatus(ConnectionStatus.ERROR);
1821
+ this.stopHeartbeat();
1822
+ if (this.reconnectAttempt < (this.config.reconnectAttempts ?? 0)) {
1823
+ this.scheduleReconnect();
1824
+ }
1825
+ else {
1826
+ this.updateReadiness({ phase: ReadinessPhase.ERROR, detail: 'Connection lost' });
1827
+ this.setConnectionStatus(ConnectionStatus.DISCONNECTED);
1828
+ this.emit('error', createConnectionError('Max reconnection attempts reached'));
1829
+ this.setConnectionStatus(ConnectionStatus.DISCONNECTED);
1830
+ }
1831
+ }
1832
+ scheduleReconnect() {
1833
+ this.clearReconnectTimer();
1834
+ this.setConnectionStatus(ConnectionStatus.RECONNECTING);
1835
+ this.updateReadiness({ phase: ReadinessPhase.CONNECTING_MCP });
1836
+ const delay = (this.config.reconnectDelay ?? 0) * Math.pow(2, this.reconnectAttempt);
1837
+ this.reconnectAttempt += 1;
1838
+ this.reconnectTimer = setTimeout(() => {
1839
+ void this.connectSSE();
1840
+ }, delay);
1841
+ }
1842
+ clearReconnectTimer() {
1843
+ if (this.reconnectTimer) {
1844
+ clearTimeout(this.reconnectTimer);
1845
+ this.reconnectTimer = null;
1846
+ }
1847
+ }
1848
+ startHeartbeat() {
1849
+ this.stopHeartbeat();
1850
+ this.heartbeatTimer = setInterval(() => {
1851
+ if (this.state.connectionStatus === ConnectionStatus.CONNECTED && this.eventSource) {
1852
+ if (this.eventSource.readyState !== EventSource.OPEN) {
1853
+ console.warn('SSE connection lost, attempting to reconnect');
1854
+ this.handleConnectionError();
1855
+ }
1856
+ }
1857
+ }, TIMING.HEARTBEAT_INTERVAL);
1858
+ }
1859
+ stopHeartbeat() {
1860
+ if (this.heartbeatTimer) {
1861
+ clearInterval(this.heartbeatTimer);
1862
+ this.heartbeatTimer = null;
1863
+ }
1864
+ }
1865
+ teardownEventSource() {
1866
+ if (this.eventSource) {
1867
+ this.eventSource.close();
1868
+ this.eventSource = null;
1869
+ }
1870
+ this.stopHeartbeat();
1871
+ this.clearReconnectTimer();
1872
+ }
1873
+ }
1874
+ function normaliseToolStream(raw) {
1875
+ if (!raw) {
1876
+ return undefined;
1877
+ }
1878
+ if (Array.isArray(raw)) {
1879
+ const [topic, content] = raw;
1880
+ return typeof topic === 'string'
1881
+ ? { topic, content: typeof content === 'string' ? content : '' }
1882
+ : undefined;
1883
+ }
1884
+ if (typeof raw === 'object') {
1885
+ const { topic, content } = raw;
1886
+ return typeof topic === 'string'
1887
+ ? { topic, content: typeof content === 'string' ? content : '' }
1888
+ : undefined;
1889
+ }
1890
+ return undefined;
1891
+ }
1892
+ function areToolStreamsEqual(a, b) {
1893
+ if (!a && !b) {
1894
+ return true;
1895
+ }
1896
+ if (!a || !b) {
1897
+ return false;
1898
+ }
1899
+ return a.topic === b.topic && a.content === b.content;
1900
+ }
1901
+
1902
+ // ThemeManager - Handles theme configuration and application
1903
+ /*
1904
+ * ============================================================================
1905
+ * THEME MANAGER CLASS
1906
+ * ============================================================================
1907
+ */
1908
+ class ThemeManager {
1909
+ constructor(theme) {
1910
+ this.customPalette = null;
1911
+ this.currentTheme = this.resolveTheme(theme);
1912
+ }
1913
+ /*
1914
+ * ============================================================================
1915
+ * PUBLIC API
1916
+ * ============================================================================
1917
+ */
1918
+ /**
1919
+ * Updates the current theme
1920
+ */
1921
+ updateTheme(theme) {
1922
+ this.currentTheme = this.resolveTheme(theme);
1923
+ this.customPalette = this.isCustomPalette(theme) ? theme : null;
1924
+ }
1925
+ /**
1926
+ * Gets the current computed theme
1927
+ */
1928
+ getComputedTheme() {
1929
+ if (this.customPalette) {
1930
+ return {
1931
+ ...this.currentTheme,
1932
+ palette: this.customPalette,
1933
+ };
1934
+ }
1935
+ return this.currentTheme;
1936
+ }
1937
+ /**
1938
+ * Gets a specific color from the current theme
1939
+ */
1940
+ getColor(colorKey) {
1941
+ const theme = this.getComputedTheme();
1942
+ return theme.palette[colorKey] || THEME_PALETTES.light[colorKey];
1943
+ }
1944
+ /**
1945
+ * Gets the CSS class name for the current theme
1946
+ */
1947
+ getThemeClass() {
1948
+ const baseTheme = this.customPalette?.baseTheme || this.getBaseThemeName();
1949
+ switch (baseTheme) {
1950
+ case 'light':
1951
+ return CSS_CLASSES.THEME_LIGHT;
1952
+ case 'dark':
1953
+ return CSS_CLASSES.THEME_DARK;
1954
+ case 'terminal':
1955
+ return CSS_CLASSES.THEME_TERMINAL;
1956
+ case 'neon':
1957
+ return CSS_CLASSES.THEME_NEON;
1958
+ case 'minimal':
1959
+ return CSS_CLASSES.THEME_MINIMAL;
1960
+ default:
1961
+ return CSS_CLASSES.THEME_LIGHT;
1962
+ }
1963
+ }
1964
+ /**
1965
+ * Gets the font family for the current theme
1966
+ */
1967
+ getFontFamily() {
1968
+ return this.currentTheme.fonts?.primary || 'system-ui, sans-serif';
1969
+ }
1970
+ /**
1971
+ * Gets the monospace font family for the current theme
1972
+ */
1973
+ getMonospaceFontFamily() {
1974
+ return this.currentTheme.fonts?.monospace || 'monospace';
1975
+ }
1976
+ /**
1977
+ * Gets a spacing value for the current theme
1978
+ */
1979
+ getSpacing(size) {
1980
+ return this.currentTheme.spacing?.[size] || '8px';
1981
+ }
1982
+ /**
1983
+ * Gets a border radius value for the current theme
1984
+ */
1985
+ getBorderRadius(size) {
1986
+ return this.currentTheme.borderRadius?.[size] || '4px';
1987
+ }
1988
+ /**
1989
+ * Gets a shadow value for the current theme
1990
+ */
1991
+ getShadow(size) {
1992
+ return this.currentTheme.shadows?.[size] || 'none';
1993
+ }
1994
+ /**
1995
+ * Generates CSS custom properties for the current theme
1996
+ */
1997
+ getCSSCustomProperties() {
1998
+ const theme = this.getComputedTheme();
1999
+ const properties = {};
2000
+ // Color properties
2001
+ Object.entries(theme.palette).forEach(([key, value]) => {
2002
+ properties[`--aomi-color-${key}`] = value;
2003
+ });
2004
+ // Font properties
2005
+ if (theme.fonts?.primary) {
2006
+ properties['--aomi-font-primary'] = theme.fonts.primary;
2007
+ }
2008
+ if (theme.fonts?.monospace) {
2009
+ properties['--aomi-font-monospace'] = theme.fonts.monospace;
2010
+ }
2011
+ // Spacing properties
2012
+ if (theme.spacing) {
2013
+ Object.entries(theme.spacing).forEach(([key, value]) => {
2014
+ properties[`--aomi-spacing-${key}`] = value;
2015
+ });
2016
+ }
2017
+ // Border radius properties
2018
+ if (theme.borderRadius) {
2019
+ Object.entries(theme.borderRadius).forEach(([key, value]) => {
2020
+ properties[`--aomi-radius-${key}`] = value;
2021
+ });
2022
+ }
2023
+ // Shadow properties
2024
+ if (theme.shadows) {
2025
+ Object.entries(theme.shadows).forEach(([key, value]) => {
2026
+ properties[`--aomi-shadow-${key}`] = value;
2027
+ });
2028
+ }
2029
+ return properties;
2030
+ }
2031
+ /**
2032
+ * Applies the theme to a DOM element
2033
+ */
2034
+ applyThemeToElement(element) {
2035
+ const properties = this.getCSSCustomProperties();
2036
+ Object.entries(properties).forEach(([property, value]) => {
2037
+ element.style.setProperty(property, value);
2038
+ });
2039
+ // Add theme class
2040
+ element.classList.add(this.getThemeClass());
2041
+ }
2042
+ /**
2043
+ * Generates CSS string for the current theme
2044
+ */
2045
+ generateCSS(selector = '.aomi-chat-widget') {
2046
+ const properties = this.getCSSCustomProperties();
2047
+ const cssRules = [];
2048
+ // Main theme CSS
2049
+ const mainRule = Object.entries(properties)
2050
+ .map(([property, value]) => ` ${property}: ${value};`)
2051
+ .join('\n');
2052
+ cssRules.push(`${selector} {\n${mainRule}\n}`);
2053
+ // Add component-specific styles
2054
+ cssRules.push(this.generateComponentCSS(selector));
2055
+ return cssRules.join('\n\n');
2056
+ }
2057
+ /**
2058
+ * Destroys the theme manager
2059
+ */
2060
+ destroy() {
2061
+ // Clean up any resources if needed
2062
+ }
2063
+ /*
2064
+ * ============================================================================
2065
+ * PRIVATE METHODS
2066
+ * ============================================================================
2067
+ */
2068
+ resolveTheme(theme) {
2069
+ if (!theme) {
2070
+ return PREDEFINED_THEMES.light;
2071
+ }
2072
+ if (typeof theme === 'string') {
2073
+ const predefinedTheme = PREDEFINED_THEMES[theme];
2074
+ if (!predefinedTheme) {
2075
+ throw createConfigurationError(`Unknown theme: ${theme}`);
2076
+ }
2077
+ return predefinedTheme;
2078
+ }
2079
+ if (this.isCustomPalette(theme)) {
2080
+ const baseTheme = PREDEFINED_THEMES[theme.baseTheme];
2081
+ if (!baseTheme) {
2082
+ throw createConfigurationError(`Unknown base theme: ${theme.baseTheme}`);
2083
+ }
2084
+ return {
2085
+ ...baseTheme,
2086
+ palette: {
2087
+ primary: theme.primary,
2088
+ background: theme.background,
2089
+ surface: theme.surface,
2090
+ text: theme.text,
2091
+ textSecondary: theme.textSecondary,
2092
+ border: theme.border,
2093
+ success: theme.success,
2094
+ error: theme.error,
2095
+ warning: theme.warning,
2096
+ accent: theme.accent,
2097
+ },
2098
+ };
2099
+ }
2100
+ throw createConfigurationError('Invalid theme configuration');
2101
+ }
2102
+ isCustomPalette(theme) {
2103
+ if (typeof theme !== 'object' || theme === null)
2104
+ return false;
2105
+ const palette = theme;
2106
+ return (typeof palette.baseTheme === 'string' &&
2107
+ typeof palette.primary === 'string' &&
2108
+ typeof palette.background === 'string' &&
2109
+ typeof palette.surface === 'string' &&
2110
+ typeof palette.text === 'string' &&
2111
+ typeof palette.textSecondary === 'string' &&
2112
+ typeof palette.border === 'string' &&
2113
+ typeof palette.success === 'string' &&
2114
+ typeof palette.error === 'string' &&
2115
+ typeof palette.warning === 'string' &&
2116
+ typeof palette.accent === 'string');
2117
+ }
2118
+ getBaseThemeName() {
2119
+ // Find the base theme name by comparing palettes
2120
+ for (const [themeName, themeDefinition] of Object.entries(PREDEFINED_THEMES)) {
2121
+ if (this.isPaletteEqual(themeDefinition.palette, this.currentTheme.palette)) {
2122
+ return themeName;
2123
+ }
2124
+ }
2125
+ return 'light'; // fallback
2126
+ }
2127
+ isPaletteEqual(palette1, palette2) {
2128
+ const keys = Object.keys(palette1);
2129
+ return keys.every(key => palette1[key] === palette2[key]);
2130
+ }
2131
+ generateComponentCSS(selector) {
2132
+ return `
2133
+ /* Chat Interface */
2134
+ ${selector} .${CSS_CLASSES.CHAT_INTERFACE} {
2135
+ background-color: var(--aomi-color-background);
2136
+ color: var(--aomi-color-text);
2137
+ border: 1px solid var(--aomi-color-border);
2138
+ border-radius: var(--aomi-radius-md);
2139
+ font-family: var(--aomi-font-primary);
2140
+ }
2141
+
2142
+ /* Message List */
2143
+ ${selector} .${CSS_CLASSES.MESSAGE_LIST} {
2144
+ padding: var(--aomi-spacing-md);
2145
+ overflow-y: auto;
2146
+ }
2147
+
2148
+ /* Message Input */
2149
+ ${selector} .${CSS_CLASSES.MESSAGE_INPUT} {
2150
+ background-color: var(--aomi-color-surface);
2151
+ border: 1px solid var(--aomi-color-border);
2152
+ border-radius: var(--aomi-radius-sm);
2153
+ color: var(--aomi-color-text);
2154
+ padding: var(--aomi-spacing-sm);
2155
+ font-family: var(--aomi-font-primary);
2156
+ }
2157
+
2158
+ ${selector} .${CSS_CLASSES.MESSAGE_INPUT}:focus {
2159
+ outline: none;
2160
+ border-color: var(--aomi-color-primary);
2161
+ box-shadow: 0 0 0 2px var(--aomi-color-primary)25;
2162
+ }
2163
+
2164
+ /* Wallet Status */
2165
+ ${selector} .${CSS_CLASSES.WALLET_STATUS} {
2166
+ background-color: var(--aomi-color-surface);
2167
+ color: var(--aomi-color-textSecondary);
2168
+ padding: var(--aomi-spacing-sm);
2169
+ border-radius: var(--aomi-radius-sm);
2170
+ font-size: 12px;
2171
+ }
2172
+
2173
+ ${selector} .${CSS_CLASSES.WALLET_STATUS}.${CSS_CLASSES.CONNECTED} {
2174
+ color: var(--aomi-color-success);
2175
+ }
2176
+
2177
+ ${selector} .${CSS_CLASSES.WALLET_STATUS}.${CSS_CLASSES.DISCONNECTED} {
2178
+ color: var(--aomi-color-error);
2179
+ }
2180
+
2181
+ /* Typing Indicator */
2182
+ ${selector} .${CSS_CLASSES.TYPING_INDICATOR} {
2183
+ color: var(--aomi-color-textSecondary);
2184
+ font-style: italic;
2185
+ padding: var(--aomi-spacing-sm);
2186
+ }
2187
+
2188
+ /* State Classes */
2189
+ ${selector}.${CSS_CLASSES.LOADING} {
2190
+ opacity: 0.7;
2191
+ pointer-events: none;
2192
+ }
2193
+
2194
+ ${selector}.${CSS_CLASSES.ERROR} {
2195
+ border-color: var(--aomi-color-error);
2196
+ }
2197
+
2198
+ ${selector}.${CSS_CLASSES.DISABLED} {
2199
+ opacity: 0.5;
2200
+ pointer-events: none;
2201
+ }
2202
+
2203
+ /* Terminal Theme Specific */
2204
+ ${selector}.${CSS_CLASSES.THEME_TERMINAL} {
2205
+ font-family: var(--aomi-font-monospace);
2206
+ background-color: var(--aomi-color-background);
2207
+ }
2208
+
2209
+ ${selector}.${CSS_CLASSES.THEME_TERMINAL} .${CSS_CLASSES.CHAT_INTERFACE} {
2210
+ border: 1px solid var(--aomi-color-primary);
2211
+ box-shadow: var(--aomi-shadow-md);
2212
+ }
2213
+
2214
+ /* Neon Theme Specific */
2215
+ ${selector}.${CSS_CLASSES.THEME_NEON} {
2216
+ background: linear-gradient(135deg, var(--aomi-color-background), var(--aomi-color-surface));
2217
+ }
2218
+
2219
+ ${selector}.${CSS_CLASSES.THEME_NEON} .${CSS_CLASSES.CHAT_INTERFACE} {
2220
+ border: 1px solid var(--aomi-color-primary);
2221
+ box-shadow: 0 0 20px var(--aomi-color-primary)50;
2222
+ }`;
2223
+ }
2224
+ }
2225
+ /*
2226
+ * ============================================================================
2227
+ * UTILITY FUNCTIONS
2228
+ * ============================================================================
2229
+ */
2230
+ /**
2231
+ * Creates a theme manager instance
2232
+ */
2233
+ function createThemeManager(theme) {
2234
+ return new ThemeManager(theme);
2235
+ }
2236
+ /**
2237
+ * Gets all available predefined themes
2238
+ */
2239
+ function getAvailableThemes() {
2240
+ return { ...PREDEFINED_THEMES };
2241
+ }
2242
+ /**
2243
+ * Validates a custom palette
2244
+ */
2245
+ function validateCustomPalette(palette) {
2246
+ if (typeof palette !== 'object' || palette === null)
2247
+ return false;
2248
+ const p = palette;
2249
+ const requiredKeys = [
2250
+ 'baseTheme', 'primary', 'background', 'surface', 'text', 'textSecondary',
2251
+ 'border', 'success', 'error', 'warning', 'accent',
2252
+ ];
2253
+ return requiredKeys.every(key => typeof p[key] === 'string');
2254
+ }
2255
+ /**
2256
+ * Creates a custom palette from a base theme
2257
+ */
2258
+ function createCustomPalette(baseTheme, overrides) {
2259
+ const basePalette = THEME_PALETTES[baseTheme];
2260
+ if (!basePalette) {
2261
+ throw createConfigurationError(`Unknown base theme: ${baseTheme}`);
2262
+ }
2263
+ return {
2264
+ baseTheme,
2265
+ ...basePalette,
2266
+ ...overrides,
2267
+ };
2268
+ }
2269
+
2270
+ // WalletManager - Handles wallet connection and transactions
2271
+ /*
2272
+ * ============================================================================
2273
+ * WALLET MANAGER CLASS
2274
+ * ============================================================================
2275
+ */
2276
+ class WalletManager extends EventEmitter {
2277
+ constructor(provider) {
2278
+ super();
2279
+ this.currentAccount = null;
2280
+ this.currentChainId = null;
2281
+ this.isConnected = false;
2282
+ this.provider = provider;
2283
+ this.setupEventListeners();
2284
+ this.initializeState();
2285
+ }
2286
+ /*
2287
+ * ============================================================================
2288
+ * PUBLIC API
2289
+ * ============================================================================
2290
+ */
2291
+ /**
2292
+ * Gets the current connected account
2293
+ */
2294
+ getCurrentAccount() {
2295
+ return this.currentAccount;
2296
+ }
2297
+ /**
2298
+ * Gets the current chain ID
2299
+ */
2300
+ getCurrentChainId() {
2301
+ return this.currentChainId;
2302
+ }
2303
+ /**
2304
+ * Gets the current network name
2305
+ */
2306
+ getCurrentNetworkName() {
2307
+ if (!this.currentChainId)
2308
+ return null;
2309
+ return SUPPORTED_CHAINS[this.currentChainId] || 'Unknown Network';
2310
+ }
2311
+ /**
2312
+ * Checks if wallet is connected
2313
+ */
2314
+ getIsConnected() {
2315
+ return this.isConnected;
2316
+ }
2317
+ /**
2318
+ * Connects to the wallet
2319
+ */
2320
+ async connect() {
2321
+ try {
2322
+ const accounts = await this.provider.request({
2323
+ id: Date.now(),
2324
+ method: 'eth_requestAccounts',
2325
+ params: [],
2326
+ });
2327
+ if (!accounts || accounts.length === 0) {
2328
+ throw createWalletError(ERROR_CODES.WALLET_CONNECTION_FAILED, 'No accounts returned from wallet');
2329
+ }
2330
+ const account = accounts[0];
2331
+ if (!isEthereumAddress(account)) {
2332
+ throw createWalletError(ERROR_CODES.WALLET_CONNECTION_FAILED, 'Invalid account address');
2333
+ }
2334
+ this.currentAccount = account;
2335
+ this.isConnected = true;
2336
+ // Get chain ID
2337
+ await this.updateChainId();
2338
+ this.emit('connect', account);
2339
+ return account;
2340
+ }
2341
+ catch (error) {
2342
+ const walletError = error instanceof Error
2343
+ ? createWalletError(ERROR_CODES.WALLET_CONNECTION_FAILED, error.message)
2344
+ : createWalletError(ERROR_CODES.WALLET_CONNECTION_FAILED, 'Unknown error');
2345
+ this.emit('error', walletError);
2346
+ throw walletError;
2347
+ }
2348
+ }
2349
+ /**
2350
+ * Disconnects from the wallet
2351
+ */
2352
+ disconnect() {
2353
+ this.currentAccount = null;
2354
+ this.currentChainId = null;
2355
+ this.isConnected = false;
2356
+ this.emit('disconnect');
2357
+ }
2358
+ /**
2359
+ * Switches to a specific network
2360
+ */
2361
+ async switchNetwork(chainId) {
2362
+ try {
2363
+ await this.provider.request({
2364
+ id: Date.now(),
2365
+ method: 'wallet_switchEthereumChain',
2366
+ params: [{ chainId: `0x${chainId.toString(16)}` }],
2367
+ });
2368
+ }
2369
+ catch (error) {
2370
+ // If the chain is not added, try to add it
2371
+ if (error.code === 4902) {
2372
+ await this.addNetwork(chainId);
2373
+ }
2374
+ else {
2375
+ throw createWalletError(ERROR_CODES.UNSUPPORTED_NETWORK, `Failed to switch to network ${chainId}: ${error.message}`);
2376
+ }
2377
+ }
2378
+ }
2379
+ /**
2380
+ * Sends a transaction
2381
+ */
2382
+ async sendTransaction(transaction) {
2383
+ if (!this.isConnected || !this.currentAccount) {
2384
+ throw createWalletError(ERROR_CODES.WALLET_NOT_CONNECTED, 'Wallet is not connected');
2385
+ }
2386
+ try {
2387
+ // Validate transaction
2388
+ this.validateTransaction(transaction);
2389
+ // Prepare transaction parameters
2390
+ const txParams = {
2391
+ from: this.currentAccount,
2392
+ to: transaction.to,
2393
+ value: transaction.value,
2394
+ data: transaction.data,
2395
+ };
2396
+ if (transaction.gas) {
2397
+ txParams.gas = transaction.gas;
2398
+ }
2399
+ // Send transaction
2400
+ const txHash = await this.provider.request({
2401
+ id: Date.now(),
2402
+ method: 'eth_sendTransaction',
2403
+ params: [txParams],
2404
+ });
2405
+ if (!isTransactionHash(txHash)) {
2406
+ throw createTransactionError('Invalid transaction hash returned');
2407
+ }
2408
+ return txHash;
2409
+ }
2410
+ catch (error) {
2411
+ // Handle user rejection
2412
+ if (error.code === 4001) {
2413
+ throw createWalletError(ERROR_CODES.TRANSACTION_REJECTED, 'Transaction was rejected by user');
2414
+ }
2415
+ const message = error.message || 'Transaction failed';
2416
+ throw createTransactionError(message);
2417
+ }
2418
+ }
2419
+ /**
2420
+ * Signs a message
2421
+ */
2422
+ async signMessage(message) {
2423
+ if (!this.isConnected || !this.currentAccount) {
2424
+ throw createWalletError(ERROR_CODES.WALLET_NOT_CONNECTED, 'Wallet is not connected');
2425
+ }
2426
+ try {
2427
+ const signature = await this.provider.request({
2428
+ id: Date.now(),
2429
+ method: 'personal_sign',
2430
+ params: [message, this.currentAccount],
2431
+ });
2432
+ return signature;
2433
+ }
2434
+ catch (error) {
2435
+ if (error.code === 4001) {
2436
+ throw createWalletError(ERROR_CODES.TRANSACTION_REJECTED, 'Message signing was rejected by user');
2437
+ }
2438
+ throw createWalletError(ERROR_CODES.UNKNOWN_ERROR, `Failed to sign message: ${error.message}`);
2439
+ }
2440
+ }
2441
+ /**
2442
+ * Gets account balance
2443
+ */
2444
+ async getBalance(address) {
2445
+ const accountAddress = address || this.currentAccount;
2446
+ if (!accountAddress) {
2447
+ throw createWalletError(ERROR_CODES.WALLET_NOT_CONNECTED, 'No account available');
2448
+ }
2449
+ try {
2450
+ const balance = await this.provider.request({
2451
+ id: Date.now(),
2452
+ method: 'eth_getBalance',
2453
+ params: [accountAddress, 'latest'],
2454
+ });
2455
+ return balance;
2456
+ }
2457
+ catch (error) {
2458
+ throw createWalletError(ERROR_CODES.UNKNOWN_ERROR, `Failed to get balance: ${error.message}`);
2459
+ }
2460
+ }
2461
+ /**
2462
+ * Updates the provider
2463
+ */
2464
+ updateProvider(provider) {
2465
+ // Clean up old provider
2466
+ this.removeProviderListeners();
2467
+ // Set new provider
2468
+ this.provider = provider;
2469
+ // Setup new listeners
2470
+ this.setupEventListeners();
2471
+ // Re-initialize state
2472
+ this.initializeState();
2473
+ }
2474
+ /**
2475
+ * Destroys the wallet manager
2476
+ */
2477
+ destroy() {
2478
+ this.removeProviderListeners();
2479
+ this.disconnect();
2480
+ this.removeAllListeners();
2481
+ }
2482
+ /*
2483
+ * ============================================================================
2484
+ * PRIVATE METHODS
2485
+ * ============================================================================
2486
+ */
2487
+ async initializeState() {
2488
+ try {
2489
+ // Check if already connected
2490
+ const accounts = await this.provider.request({
2491
+ id: Date.now(),
2492
+ method: 'eth_accounts',
2493
+ params: [],
2494
+ });
2495
+ if (accounts && accounts.length > 0 && isEthereumAddress(accounts[0])) {
2496
+ this.currentAccount = accounts[0];
2497
+ this.isConnected = true;
2498
+ await this.updateChainId();
2499
+ this.emit('connect', accounts[0]);
2500
+ }
2501
+ }
2502
+ catch (error) {
2503
+ // Ignore errors during initialization
2504
+ console.warn('Failed to initialize wallet state:', error);
2505
+ }
2506
+ }
2507
+ async updateChainId() {
2508
+ try {
2509
+ const chainId = await this.provider.request({
2510
+ id: Date.now(),
2511
+ method: 'eth_chainId',
2512
+ params: [],
2513
+ });
2514
+ const numericChainId = parseInt(chainId, 16);
2515
+ if (this.currentChainId !== numericChainId) {
2516
+ this.currentChainId = numericChainId;
2517
+ this.emit('chainChange', numericChainId);
2518
+ }
2519
+ }
2520
+ catch (error) {
2521
+ console.warn('Failed to get chain ID:', error);
2522
+ }
2523
+ }
2524
+ setupEventListeners() {
2525
+ if (this.provider.on) {
2526
+ this.provider.on('accountsChanged', this.handleAccountsChanged.bind(this));
2527
+ this.provider.on('chainChanged', this.handleChainChanged.bind(this));
2528
+ this.provider.on('disconnect', this.handleDisconnect.bind(this));
2529
+ }
2530
+ }
2531
+ removeProviderListeners() {
2532
+ if (this.provider.removeListener) {
2533
+ this.provider.removeListener?.('accountsChanged', this.handleAccountsChanged.bind(this));
2534
+ this.provider.removeListener?.('chainChanged', this.handleChainChanged.bind(this));
2535
+ this.provider.removeListener('disconnect', this.handleDisconnect.bind(this));
2536
+ }
2537
+ }
2538
+ handleAccountsChanged(accounts) {
2539
+ if (!accounts || accounts.length === 0) {
2540
+ this.disconnect();
2541
+ }
2542
+ else if (accounts[0] !== this.currentAccount) {
2543
+ this.currentAccount = accounts[0];
2544
+ this.isConnected = true;
2545
+ this.emit('accountsChange', accounts);
2546
+ this.emit('connect', accounts[0]);
2547
+ }
2548
+ }
2549
+ handleChainChanged(chainId) {
2550
+ const numericChainId = parseInt(chainId, 16);
2551
+ this.currentChainId = numericChainId;
2552
+ this.emit('chainChange', numericChainId);
2553
+ }
2554
+ handleDisconnect() {
2555
+ this.disconnect();
2556
+ }
2557
+ validateTransaction(transaction) {
2558
+ if (!isEthereumAddress(transaction.to)) {
2559
+ throw createTransactionError('Invalid recipient address');
2560
+ }
2561
+ // Validate value (should be hex string)
2562
+ if (!/^0x[0-9a-fA-F]*$/.test(transaction.value)) {
2563
+ throw createTransactionError('Invalid transaction value');
2564
+ }
2565
+ // Validate data (should be hex string)
2566
+ if (transaction.data && !/^0x[0-9a-fA-F]*$/.test(transaction.data)) {
2567
+ throw createTransactionError('Invalid transaction data');
2568
+ }
2569
+ // Validate gas (should be hex string if provided)
2570
+ if (transaction.gas && !/^0x[0-9a-fA-F]*$/.test(transaction.gas)) {
2571
+ throw createTransactionError('Invalid gas value');
2572
+ }
2573
+ }
2574
+ async addNetwork(chainId) {
2575
+ const networkConfig = this.getNetworkConfig(chainId);
2576
+ if (!networkConfig) {
2577
+ throw createWalletError(ERROR_CODES.UNSUPPORTED_NETWORK, `Network ${chainId} is not supported`);
2578
+ }
2579
+ try {
2580
+ await this.provider.request({
2581
+ id: Date.now(),
2582
+ method: 'wallet_addEthereumChain',
2583
+ params: [networkConfig],
2584
+ });
2585
+ }
2586
+ catch (error) {
2587
+ throw createWalletError(ERROR_CODES.UNSUPPORTED_NETWORK, `Failed to add network ${chainId}: ${error.message}`);
2588
+ }
2589
+ }
2590
+ getNetworkConfig(chainId) {
2591
+ const configs = {
2592
+ 1: null, // Mainnet is built-in
2593
+ 5: null, // Goerli is built-in
2594
+ 10: {
2595
+ chainId: '0xa',
2596
+ chainName: 'Optimism',
2597
+ nativeCurrency: {
2598
+ name: 'Ether',
2599
+ symbol: 'ETH',
2600
+ decimals: 18,
2601
+ },
2602
+ rpcUrls: ['https://mainnet.optimism.io'],
2603
+ blockExplorerUrls: ['https://optimistic.etherscan.io'],
2604
+ },
2605
+ 11155111: null, // Sepolia is built-in
2606
+ 100: {
2607
+ chainId: '0x64',
2608
+ chainName: 'Gnosis',
2609
+ nativeCurrency: {
2610
+ name: 'xDai',
2611
+ symbol: 'XDAI',
2612
+ decimals: 18,
2613
+ },
2614
+ rpcUrls: ['https://rpc.gnosischain.com'],
2615
+ blockExplorerUrls: ['https://gnosisscan.io'],
2616
+ },
2617
+ 1337: null, // Local development
2618
+ 31337: null, // Anvil/Hardhat
2619
+ 137: {
2620
+ chainId: '0x89',
2621
+ chainName: 'Polygon',
2622
+ nativeCurrency: {
2623
+ name: 'MATIC',
2624
+ symbol: 'MATIC',
2625
+ decimals: 18,
2626
+ },
2627
+ rpcUrls: ['https://polygon-rpc.com'],
2628
+ blockExplorerUrls: ['https://polygonscan.com'],
2629
+ },
2630
+ 42161: {
2631
+ chainId: '0xa4b1',
2632
+ chainName: 'Arbitrum One',
2633
+ nativeCurrency: {
2634
+ name: 'Ether',
2635
+ symbol: 'ETH',
2636
+ decimals: 18,
2637
+ },
2638
+ rpcUrls: ['https://arb1.arbitrum.io/rpc'],
2639
+ blockExplorerUrls: ['https://arbiscan.io'],
2640
+ },
2641
+ 8453: {
2642
+ chainId: '0x2105',
2643
+ chainName: 'Base',
2644
+ nativeCurrency: {
2645
+ name: 'Ether',
2646
+ symbol: 'ETH',
2647
+ decimals: 18,
2648
+ },
2649
+ rpcUrls: ['https://mainnet.base.org'],
2650
+ blockExplorerUrls: ['https://basescan.org'],
2651
+ },
2652
+ 59140: {
2653
+ chainId: '0xe704',
2654
+ chainName: 'Linea Sepolia',
2655
+ nativeCurrency: {
2656
+ name: 'Linea Ether',
2657
+ symbol: 'ETH',
2658
+ decimals: 18,
2659
+ },
2660
+ rpcUrls: ['https://rpc-sepolia.linea.build'],
2661
+ blockExplorerUrls: ['https://sepolia.lineascan.build'],
2662
+ },
2663
+ 59144: {
2664
+ chainId: '0xe708',
2665
+ chainName: 'Linea',
2666
+ nativeCurrency: {
2667
+ name: 'Linea Ether',
2668
+ symbol: 'ETH',
2669
+ decimals: 18,
2670
+ },
2671
+ rpcUrls: ['https://rpc.linea.build'],
2672
+ blockExplorerUrls: ['https://lineascan.build'],
2673
+ },
2674
+ };
2675
+ return configs[chainId];
2676
+ }
2677
+ }
2678
+ /*
2679
+ * ============================================================================
2680
+ * UTILITY FUNCTIONS
2681
+ * ============================================================================
2682
+ */
2683
+ /**
2684
+ * Creates a wallet manager instance
2685
+ */
2686
+ function createWalletManager(provider) {
2687
+ return new WalletManager(provider);
2688
+ }
2689
+ /**
2690
+ * Checks if a provider supports the required methods
2691
+ */
2692
+ function isValidProvider(provider) {
2693
+ if (!provider || typeof provider !== 'object')
2694
+ return false;
2695
+ const p = provider;
2696
+ return (typeof p.request === 'function' &&
2697
+ typeof p.on === 'function');
2698
+ }
2699
+ /**
2700
+ * Detects available wallets
2701
+ */
2702
+ function detectWallets() {
2703
+ if (typeof window === 'undefined')
2704
+ return [];
2705
+ const wallets = [];
2706
+ // MetaMask
2707
+ if (window.ethereum?.isMetaMask) {
2708
+ wallets.push({
2709
+ name: 'MetaMask',
2710
+ provider: window.ethereum,
2711
+ });
2712
+ }
2713
+ // WalletConnect
2714
+ if (window.ethereum?.isWalletConnect) {
2715
+ wallets.push({
2716
+ name: 'WalletConnect',
2717
+ provider: window.ethereum,
2718
+ });
2719
+ }
2720
+ // Generic ethereum provider
2721
+ if (window.ethereum && !window.ethereum.isMetaMask && !window.ethereum.isWalletConnect) {
2722
+ wallets.push({
2723
+ name: 'Injected Wallet',
2724
+ provider: window.ethereum,
2725
+ });
2726
+ }
2727
+ return wallets;
2728
+ }
2729
+
2730
+ // AomiChatWidget - Main widget factory and management class
2731
+ /*
2732
+ * ============================================================================
2733
+ * WIDGET HANDLER IMPLEMENTATION
2734
+ * ============================================================================
2735
+ */
2736
+ class AomiChatWidgetHandlerImpl {
2737
+ constructor(container, config) {
2738
+ this.walletManager = null;
2739
+ this.widgetElement = null;
2740
+ this.isDestroyed = false;
2741
+ this.hasPromptedNetworkSwitch = false;
2742
+ this.eventEmitter = new EventEmitter();
2743
+ this.container = container;
2744
+ this.config = config;
2745
+ // Initialize managers
2746
+ this.chatManager = new ChatManager({
2747
+ backendUrl: config.params.baseUrl || 'http://localhost:8080',
2748
+ sessionId: config.params.sessionId || generateSessionId(),
2749
+ maxMessageLength: 2000,
2750
+ reconnectAttempts: 5,
2751
+ reconnectDelay: 3000,
2752
+ });
2753
+ this.themeManager = new ThemeManager(config.params.theme);
2754
+ // Initialize wallet manager if provider is available
2755
+ if (config.provider) {
2756
+ this.walletManager = new WalletManager(config.provider);
2757
+ }
2758
+ this.setupEventListeners();
2759
+ this.render();
2760
+ this.initialize();
2761
+ }
2762
+ /*
2763
+ * ============================================================================
2764
+ * PUBLIC API METHODS
2765
+ * ============================================================================
2766
+ */
2767
+ async sendMessage(message) {
2768
+ if (this.isDestroyed) {
2769
+ throw new AomiChatError(ERROR_CODES.INITIALIZATION_FAILED, 'Widget has been destroyed');
2770
+ }
2771
+ return this.chatManager.sendMessage(message);
2772
+ }
2773
+ updateParams(params) {
2774
+ if (this.isDestroyed) {
2775
+ throw new AomiChatError(ERROR_CODES.INITIALIZATION_FAILED, 'Widget has been destroyed');
2776
+ }
2777
+ // Validate new parameters
2778
+ const previousParams = this.config.params;
2779
+ const mergedParams = { ...previousParams, ...params };
2780
+ const errors = validateWidgetParams(mergedParams);
2781
+ if (errors.length > 0) {
2782
+ throw createConfigurationError(`Invalid parameters: ${errors.join(', ')}`);
2783
+ }
2784
+ // Update configuration
2785
+ this.config.params = mergedParams;
2786
+ if (params.chainId && params.chainId !== previousParams.chainId) {
2787
+ this.hasPromptedNetworkSwitch = false;
2788
+ }
2789
+ // Update theme if changed
2790
+ if (params.theme) {
2791
+ this.themeManager.updateTheme(params.theme);
2792
+ this.applyTheme();
2793
+ }
2794
+ // Update dimensions if changed
2795
+ if (params.width || params.height) {
2796
+ this.updateDimensions();
2797
+ }
2798
+ // Re-render if necessary
2799
+ this.render();
2800
+ }
2801
+ updateProvider(provider) {
2802
+ if (this.isDestroyed) {
2803
+ throw new AomiChatError(ERROR_CODES.INITIALIZATION_FAILED, 'Widget has been destroyed');
2804
+ }
2805
+ this.config.provider = provider;
2806
+ this.hasPromptedNetworkSwitch = false;
2807
+ if (provider) {
2808
+ if (this.walletManager) {
2809
+ this.walletManager.updateProvider(provider);
2810
+ }
2811
+ else {
2812
+ this.walletManager = new WalletManager(provider);
2813
+ this.setupWalletEventListeners();
2814
+ }
2815
+ }
2816
+ else {
2817
+ if (this.walletManager) {
2818
+ this.walletManager.destroy();
2819
+ this.walletManager = null;
2820
+ }
2821
+ }
2822
+ }
2823
+ getState() {
2824
+ return this.chatManager.getState();
2825
+ }
2826
+ getSessionId() {
2827
+ return this.chatManager.getSessionId();
2828
+ }
2829
+ isReady() {
2830
+ const state = this.getState();
2831
+ return state.connectionStatus === ConnectionStatus.CONNECTED;
2832
+ }
2833
+ on(event, listener) {
2834
+ this.eventEmitter.on(event, listener);
2835
+ return this;
2836
+ }
2837
+ off(event, listener) {
2838
+ this.eventEmitter.off(event, listener);
2839
+ return this;
2840
+ }
2841
+ clearChat() {
2842
+ if (this.isDestroyed)
2843
+ return;
2844
+ this.chatManager.clearMessages();
2845
+ }
2846
+ exportChat() {
2847
+ return this.getState().messages;
2848
+ }
2849
+ resize(width, height) {
2850
+ if (this.isDestroyed)
2851
+ return;
2852
+ if (width)
2853
+ this.config.params.width = width;
2854
+ if (height)
2855
+ this.config.params.height = height;
2856
+ this.updateDimensions();
2857
+ this.eventEmitter.emit(WIDGET_EVENTS.RESIZE, {
2858
+ width: parseInt(width || this.config.params.width || '0'),
2859
+ height: parseInt(height || this.config.params.height || '0'),
2860
+ });
2861
+ }
2862
+ focus() {
2863
+ if (this.isDestroyed || !this.widgetElement)
2864
+ return;
2865
+ const input = this.widgetElement.querySelector('input, textarea');
2866
+ if (input) {
2867
+ input.focus();
2868
+ }
2869
+ }
2870
+ destroy() {
2871
+ if (this.isDestroyed)
2872
+ return;
2873
+ this.isDestroyed = true;
2874
+ // Clean up managers
2875
+ this.chatManager.destroy();
2876
+ this.themeManager.destroy();
2877
+ if (this.walletManager) {
2878
+ this.walletManager.destroy();
2879
+ }
2880
+ // Remove widget from DOM
2881
+ if (this.widgetElement && this.widgetElement.parentNode) {
2882
+ this.widgetElement.parentNode.removeChild(this.widgetElement);
2883
+ }
2884
+ // Clear container
2885
+ this.container.innerHTML = '';
2886
+ // Remove all event listeners
2887
+ this.eventEmitter.removeAllListeners();
2888
+ this.eventEmitter.emit(WIDGET_EVENTS.DESTROY);
2889
+ }
2890
+ /*
2891
+ * ============================================================================
2892
+ * PRIVATE METHODS
2893
+ * ============================================================================
2894
+ */
2895
+ async initialize() {
2896
+ try {
2897
+ // Connect to backend
2898
+ await this.chatManager.connectSSE();
2899
+ // Send welcome message if configured
2900
+ if (this.config.params.welcomeMessage) {
2901
+ await this.chatManager.sendSystemMessage(this.config.params.welcomeMessage);
2902
+ }
2903
+ this.eventEmitter.emit(WIDGET_EVENTS.READY);
2904
+ }
2905
+ catch (error) {
2906
+ const chatError = error instanceof AomiChatError
2907
+ ? error
2908
+ : createConnectionError('Failed to initialize widget');
2909
+ this.eventEmitter.emit(WIDGET_EVENTS.ERROR, chatError);
2910
+ }
2911
+ }
2912
+ setupEventListeners() {
2913
+ // Chat manager events
2914
+ this.chatManager.on('stateChange', (state) => {
2915
+ // Forward state changes to widget listeners
2916
+ this.forwardStateEvents(state);
2917
+ });
2918
+ this.chatManager.on('message', (message) => {
2919
+ this.eventEmitter.emit(WIDGET_EVENTS.MESSAGE, message);
2920
+ });
2921
+ this.chatManager.on('error', (error) => {
2922
+ this.eventEmitter.emit(WIDGET_EVENTS.ERROR, error);
2923
+ });
2924
+ this.chatManager.on('connectionChange', (status) => {
2925
+ this.eventEmitter.emit(WIDGET_EVENTS.CONNECTION_CHANGE, status);
2926
+ });
2927
+ this.chatManager.on('transactionRequest', (transaction) => {
2928
+ this.eventEmitter.emit(WIDGET_EVENTS.TRANSACTION_REQUEST, transaction);
2929
+ this.handleTransactionRequest(transaction);
2930
+ });
2931
+ // Set up wallet event listeners if wallet manager exists
2932
+ if (this.walletManager) {
2933
+ this.setupWalletEventListeners();
2934
+ }
2935
+ }
2936
+ setupWalletEventListeners() {
2937
+ if (!this.walletManager)
2938
+ return;
2939
+ this.walletManager.on('connect', (address) => {
2940
+ const chainId = this.walletManager?.getCurrentChainId() ?? null;
2941
+ const networkName = this.getNetworkName(chainId);
2942
+ this.chatManager.updateWalletState({
2943
+ isConnected: true,
2944
+ address,
2945
+ chainId: chainId ?? undefined,
2946
+ networkName,
2947
+ });
2948
+ this.eventEmitter.emit(WIDGET_EVENTS.WALLET_CONNECT, address);
2949
+ const chainLabel = chainId ?? 'unknown';
2950
+ const connectMessage = `User connected wallet with address ${address} on ${networkName} network ` +
2951
+ `(Chain ID: ${chainLabel}). Ready to help with transactions.`;
2952
+ void this.safeSendSystemMessage(connectMessage);
2953
+ void this.maybePromptNetworkSwitch();
2954
+ });
2955
+ this.walletManager.on('disconnect', () => {
2956
+ this.chatManager.updateWalletState({
2957
+ isConnected: false,
2958
+ address: undefined,
2959
+ chainId: undefined,
2960
+ networkName: undefined,
2961
+ });
2962
+ this.hasPromptedNetworkSwitch = false;
2963
+ this.eventEmitter.emit(WIDGET_EVENTS.WALLET_DISCONNECT);
2964
+ void this.safeSendSystemMessage('Wallet disconnected. Confirm to switch to testnet');
2965
+ });
2966
+ this.walletManager.on('chainChange', (chainId) => {
2967
+ const networkName = this.getNetworkName(chainId);
2968
+ this.chatManager.updateWalletState({ chainId, networkName });
2969
+ this.hasPromptedNetworkSwitch = false;
2970
+ this.eventEmitter.emit(WIDGET_EVENTS.NETWORK_CHANGE, chainId);
2971
+ void this.safeSendSystemMessage(`User switched wallet to ${networkName} network (Chain ID: ${chainId}).`);
2972
+ void this.maybePromptNetworkSwitch();
2973
+ });
2974
+ this.walletManager.on('error', (error) => {
2975
+ this.eventEmitter.emit(WIDGET_EVENTS.ERROR, error);
2976
+ });
2977
+ }
2978
+ forwardStateEvents(state) {
2979
+ // Forward typing and processing changes
2980
+ this.eventEmitter.emit(WIDGET_EVENTS.TYPING_CHANGE, state.isTyping);
2981
+ this.eventEmitter.emit(WIDGET_EVENTS.PROCESSING_CHANGE, state.isProcessing);
2982
+ }
2983
+ async handleTransactionRequest(transaction) {
2984
+ if (!this.walletManager) {
2985
+ await this.chatManager.sendTransactionResult(false, undefined, 'No wallet connected');
2986
+ return;
2987
+ }
2988
+ try {
2989
+ const txHash = await this.walletManager.sendTransaction({
2990
+ to: transaction.to,
2991
+ value: transaction.value,
2992
+ data: transaction.data,
2993
+ gas: transaction.gas,
2994
+ });
2995
+ await this.chatManager.sendTransactionResult(true, txHash);
2996
+ }
2997
+ catch (error) {
2998
+ const message = error instanceof Error ? error.message : 'Transaction failed';
2999
+ await this.chatManager.sendTransactionResult(false, undefined, message);
3000
+ }
3001
+ }
3002
+ async safeSendSystemMessage(message) {
3003
+ try {
3004
+ await this.chatManager.sendSystemMessage(message);
3005
+ }
3006
+ catch (error) {
3007
+ this.eventEmitter.emit(WIDGET_EVENTS.ERROR, error);
3008
+ }
3009
+ }
3010
+ async maybePromptNetworkSwitch() {
3011
+ if (!this.walletManager)
3012
+ return;
3013
+ const chainId = this.walletManager.getCurrentChainId();
3014
+ if (!chainId)
3015
+ return;
3016
+ if (this.hasPromptedNetworkSwitch)
3017
+ return;
3018
+ const walletNetwork = this.getNetworkName(chainId);
3019
+ const backendNetwork = this.getNetworkName(this.config.params.chainId ?? DEFAULT_CHAIN_ID);
3020
+ if (walletNetwork === backendNetwork)
3021
+ return;
3022
+ this.hasPromptedNetworkSwitch = true;
3023
+ const promptMessage = `New wallet connection: ${walletNetwork}, System configuration: ${backendNetwork}. ` +
3024
+ 'Prompt user to confirm network switch';
3025
+ await this.safeSendSystemMessage(promptMessage);
3026
+ }
3027
+ getNetworkName(chainId) {
3028
+ switch (chainId) {
3029
+ case 1:
3030
+ return 'mainnet';
3031
+ case 5:
3032
+ case 11155111:
3033
+ case 1337:
3034
+ case 31337:
3035
+ return 'testnet';
3036
+ case 10:
3037
+ return 'optimism';
3038
+ case 100:
3039
+ return 'gnosis';
3040
+ case 137:
3041
+ return 'polygon';
3042
+ case 42161:
3043
+ return 'arbitrum';
3044
+ case 8453:
3045
+ return 'base';
3046
+ case 59140:
3047
+ return 'linea-sepolia';
3048
+ case 59144:
3049
+ return 'linea';
3050
+ default:
3051
+ return 'testnet';
3052
+ }
3053
+ }
3054
+ render() {
3055
+ // Clear existing content
3056
+ this.container.innerHTML = '';
3057
+ // Create widget element
3058
+ this.widgetElement = createElement('div', {
3059
+ className: [
3060
+ CSS_CLASSES.WIDGET_ROOT,
3061
+ CSS_CLASSES.WIDGET_CONTAINER,
3062
+ this.themeManager.getThemeClass(),
3063
+ this.getModeClass(),
3064
+ ],
3065
+ styles: {
3066
+ width: this.config.params.width || DEFAULT_WIDGET_WIDTH,
3067
+ height: this.config.params.height || DEFAULT_WIDGET_HEIGHT,
3068
+ position: 'relative',
3069
+ overflow: 'hidden',
3070
+ fontFamily: this.themeManager.getFontFamily(),
3071
+ },
3072
+ });
3073
+ // Apply theme
3074
+ this.applyTheme();
3075
+ // Render chat interface
3076
+ this.renderChatInterface();
3077
+ // Add to container
3078
+ this.container.appendChild(this.widgetElement);
3079
+ }
3080
+ renderChatInterface() {
3081
+ if (!this.widgetElement)
3082
+ return;
3083
+ /*
3084
+ * This would render the actual chat UI
3085
+ * For now, create a placeholder
3086
+ */
3087
+ const chatInterface = createElement('div', {
3088
+ className: CSS_CLASSES.CHAT_INTERFACE,
3089
+ styles: {
3090
+ width: '100%',
3091
+ height: '100%',
3092
+ display: 'flex',
3093
+ flexDirection: 'column',
3094
+ backgroundColor: this.themeManager.getColor('background'),
3095
+ color: this.themeManager.getColor('text'),
3096
+ border: `1px solid ${this.themeManager.getColor('border')}`,
3097
+ borderRadius: '8px',
3098
+ },
3099
+ children: [
3100
+ createElement('div', {
3101
+ styles: {
3102
+ padding: '16px',
3103
+ textAlign: 'center',
3104
+ fontSize: '14px',
3105
+ color: this.themeManager.getColor('textSecondary'),
3106
+ },
3107
+ children: ['Chat interface will be implemented here'],
3108
+ }),
3109
+ ],
3110
+ });
3111
+ this.widgetElement.appendChild(chatInterface);
3112
+ }
3113
+ applyTheme() {
3114
+ if (!this.widgetElement)
3115
+ return;
3116
+ const theme = this.themeManager.getComputedTheme();
3117
+ // Apply CSS custom properties for theme colors
3118
+ Object.entries(theme.palette).forEach(([key, value]) => {
3119
+ this.widgetElement.style.setProperty(`--aomi-${key}`, value);
3120
+ });
3121
+ // Apply theme class
3122
+ this.widgetElement.className = [
3123
+ CSS_CLASSES.WIDGET_ROOT,
3124
+ CSS_CLASSES.WIDGET_CONTAINER,
3125
+ this.themeManager.getThemeClass(),
3126
+ this.getModeClass(),
3127
+ ].join(' ');
3128
+ }
3129
+ getModeClass() {
3130
+ switch (this.config.params.mode) {
3131
+ case 'minimal':
3132
+ return CSS_CLASSES.MODE_MINIMAL;
3133
+ case 'compact':
3134
+ return CSS_CLASSES.MODE_COMPACT;
3135
+ case 'terminal':
3136
+ return CSS_CLASSES.MODE_TERMINAL;
3137
+ default:
3138
+ return CSS_CLASSES.MODE_FULL;
3139
+ }
3140
+ }
3141
+ updateDimensions() {
3142
+ if (!this.widgetElement)
3143
+ return;
3144
+ this.widgetElement.style.width = this.config.params.width || DEFAULT_WIDGET_WIDTH;
3145
+ this.widgetElement.style.height = this.config.params.height || DEFAULT_WIDGET_HEIGHT;
3146
+ }
3147
+ }
3148
+ /*
3149
+ * ============================================================================
3150
+ * WIDGET FACTORY FUNCTION
3151
+ * ============================================================================
3152
+ */
3153
+ /**
3154
+ * Creates and initializes an Aomi Chat Widget
3155
+ */
3156
+ function createAomiChatWidget(container, config) {
3157
+ // Validate environment
3158
+ if (!isBrowser()) {
3159
+ throw createConfigurationError('Widget can only be created in a browser environment');
3160
+ }
3161
+ // Validate container
3162
+ if (!container || !(container instanceof HTMLElement)) {
3163
+ throw createConfigurationError('Container must be a valid HTML element');
3164
+ }
3165
+ // Validate configuration
3166
+ const errors = validateWidgetParams(config.params);
3167
+ if (errors.length > 0) {
3168
+ throw createConfigurationError(`Configuration errors: ${errors.join(', ')}`);
3169
+ }
3170
+ // Create and return widget handler
3171
+ return new AomiChatWidgetHandlerImpl(container, config);
3172
+ }
3173
+
3174
+ // Main entry point for the Aomi Chat Widget Library
3175
+ // Package version (this would be set during build)
3176
+ const VERSION = '0.1.0';
3177
+ /* eslint-enable no-unused-vars, @typescript-eslint/no-unused-vars */
3178
+ // Simple convenience function for basic usage
3179
+ function createChatWidget(containerId, options) {
3180
+ // Get container element
3181
+ const container = typeof containerId === 'string'
3182
+ ? document.getElementById(containerId)
3183
+ : containerId;
3184
+ if (!container) {
3185
+ throw createConfigurationError(typeof containerId === 'string'
3186
+ ? `Element with id "${containerId}" not found`
3187
+ : 'Invalid container element');
3188
+ }
3189
+ // Create widget configuration
3190
+ const config = {
3191
+ params: {
3192
+ appCode: options.appCode,
3193
+ theme: options.theme,
3194
+ width: options.width,
3195
+ height: options.height,
3196
+ baseUrl: options.baseUrl,
3197
+ },
3198
+ provider: options.provider,
3199
+ listeners: {
3200
+ onReady: options.onReady,
3201
+ onMessage: options.onMessage,
3202
+ onError: options.onError,
3203
+ },
3204
+ };
3205
+ return createAomiChatWidget(container, config);
3206
+ }
3207
+ // Default export for convenience
3208
+ var index = {
3209
+ createChatWidget,
3210
+ createAomiChatWidget,
3211
+ VERSION,
3212
+ SUPPORTED_CHAINS,
3213
+ PREDEFINED_THEMES,
3214
+ ERROR_CODES,
3215
+ WIDGET_EVENTS,
3216
+ };
3217
+
3218
+ exports.API_ENDPOINTS = API_ENDPOINTS;
3219
+ exports.CSS_CLASSES = CSS_CLASSES;
3220
+ exports.ChatError = ChatError;
3221
+ exports.ChatManager = ChatManager;
3222
+ exports.ConfigurationError = ConfigurationError;
3223
+ exports.ConnectionError = ConnectionError;
3224
+ exports.DEFAULT_CHAIN_ID = DEFAULT_CHAIN_ID;
3225
+ exports.DEFAULT_MAX_HEIGHT = DEFAULT_MAX_HEIGHT;
3226
+ exports.DEFAULT_MESSAGE_LENGTH = DEFAULT_MESSAGE_LENGTH;
3227
+ exports.DEFAULT_RECONNECT_ATTEMPTS = DEFAULT_RECONNECT_ATTEMPTS;
3228
+ exports.DEFAULT_RECONNECT_DELAY = DEFAULT_RECONNECT_DELAY;
3229
+ exports.DEFAULT_WIDGET_HEIGHT = DEFAULT_WIDGET_HEIGHT;
3230
+ exports.DEFAULT_WIDGET_WIDTH = DEFAULT_WIDGET_WIDTH;
3231
+ exports.ERROR_CODES = ERROR_CODES;
3232
+ exports.PREDEFINED_THEMES = PREDEFINED_THEMES;
3233
+ exports.RateLimitError = RateLimitError;
3234
+ exports.SUPPORTED_CHAINS = SUPPORTED_CHAINS;
3235
+ exports.SessionError = SessionError;
3236
+ exports.THEME_PALETTES = THEME_PALETTES;
3237
+ exports.TIMING = TIMING;
3238
+ exports.ThemeManager = ThemeManager;
3239
+ exports.TransactionError = TransactionError;
3240
+ exports.VERSION = VERSION;
3241
+ exports.WIDGET_EVENTS = WIDGET_EVENTS;
3242
+ exports.WalletError = WalletError;
3243
+ exports.WalletManager = WalletManager;
3244
+ exports.buildWidgetUrl = buildWidgetUrl;
3245
+ exports.createAomiChatWidget = createAomiChatWidget;
3246
+ exports.createChatError = createChatError;
3247
+ exports.createChatWidget = createChatWidget;
3248
+ exports.createConfigurationError = createConfigurationError;
3249
+ exports.createConnectionError = createConnectionError;
3250
+ exports.createCustomPalette = createCustomPalette;
3251
+ exports.createElement = createElement;
3252
+ exports.createRateLimitError = createRateLimitError;
3253
+ exports.createSessionError = createSessionError;
3254
+ exports.createThemeManager = createThemeManager;
3255
+ exports.createTransactionError = createTransactionError;
3256
+ exports.createWalletError = createWalletError;
3257
+ exports.createWalletManager = createWalletManager;
3258
+ exports.default = index;
3259
+ exports.delay = delay;
3260
+ exports.detectWallets = detectWallets;
3261
+ exports.formatNumber = formatNumber;
3262
+ exports.formatTimestamp = formatTimestamp;
3263
+ exports.generateSessionId = generateSessionId;
3264
+ exports.getAvailableThemes = getAvailableThemes;
3265
+ exports.getViewportDimensions = getViewportDimensions;
3266
+ exports.hasProperty = hasProperty;
3267
+ exports.isAomiChatError = isAomiChatError;
3268
+ exports.isBrowser = isBrowser;
3269
+ exports.isChatError = isChatError;
3270
+ exports.isConfigurationError = isConfigurationError;
3271
+ exports.isConnectionError = isConnectionError;
3272
+ exports.isElementVisible = isElementVisible;
3273
+ exports.isEthereumAddress = isEthereumAddress;
3274
+ exports.isMobile = isMobile;
3275
+ exports.isRateLimitError = isRateLimitError;
3276
+ exports.isSessionError = isSessionError;
3277
+ exports.isTransactionError = isTransactionError;
3278
+ exports.isTransactionHash = isTransactionHash;
3279
+ exports.isValidProvider = isValidProvider;
3280
+ exports.isValidSessionId = isValidSessionId;
3281
+ exports.isWalletError = isWalletError;
3282
+ exports.parseWidgetParams = parseWidgetParams;
3283
+ exports.resolveFlexibleConfig = resolveFlexibleConfig;
3284
+ exports.retry = retry;
3285
+ exports.truncateAddress = truncateAddress;
3286
+ exports.validateCustomPalette = validateCustomPalette;
3287
+ exports.validateWidgetParams = validateWidgetParams;
3288
+ exports.withTimeout = withTimeout;
3289
+ //# sourceMappingURL=index.js.map