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