@helllo-ai/agent-chat-widget 0.1.14 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agent-chat.staging.js +55 -17
- package/package.json +1 -1
package/agent-chat.staging.js
CHANGED
|
@@ -199,6 +199,7 @@
|
|
|
199
199
|
let customerForm = null
|
|
200
200
|
let sessionId = null
|
|
201
201
|
let customerInfoSubmitted = false
|
|
202
|
+
let firstMessageReceived = false
|
|
202
203
|
|
|
203
204
|
if (captureCustomerInfo) {
|
|
204
205
|
customerForm = document.createElement('div')
|
|
@@ -404,6 +405,7 @@
|
|
|
404
405
|
function connect() {
|
|
405
406
|
if (connected || connecting) return
|
|
406
407
|
connecting = true
|
|
408
|
+
firstMessageReceived = false // Reset when connecting
|
|
407
409
|
const url = resolvedWsUrl
|
|
408
410
|
try {
|
|
409
411
|
ws = new WebSocket(url)
|
|
@@ -419,32 +421,67 @@
|
|
|
419
421
|
connecting = false
|
|
420
422
|
updateStatus('Connected', true)
|
|
421
423
|
|
|
422
|
-
//
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
if (captureCustomerInfo && customerForm && !customerInfoSubmitted) {
|
|
427
|
-
customerForm.style.display = 'flex'
|
|
428
|
-
messages.style.display = 'none'
|
|
429
|
-
inputWrap.style.display = 'none'
|
|
430
|
-
// Focus first input
|
|
431
|
-
const nameInput = customerForm.querySelector('#acw-customer-name')
|
|
432
|
-
if (nameInput) setTimeout(() => nameInput.focus(), 100)
|
|
433
|
-
} else {
|
|
434
|
-
messages.style.display = 'flex'
|
|
435
|
-
inputWrap.style.display = 'flex'
|
|
436
|
-
}
|
|
424
|
+
// Session ID will be received from connection_established message
|
|
425
|
+
// Don't show customer form yet - wait for first message
|
|
426
|
+
messages.style.display = 'flex'
|
|
427
|
+
inputWrap.style.display = 'flex'
|
|
437
428
|
}
|
|
438
429
|
|
|
439
430
|
ws.onmessage = (event) => {
|
|
440
431
|
try {
|
|
441
432
|
const data = JSON.parse(event.data)
|
|
433
|
+
|
|
434
|
+
// Handle connection_established message
|
|
435
|
+
if (data.type === 'connection_established') {
|
|
436
|
+
// Extract session_id from connection message
|
|
437
|
+
if (data.session_id) {
|
|
438
|
+
sessionId = data.session_id
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// Display welcome message if provided
|
|
442
|
+
if (data.welcome_message) {
|
|
443
|
+
appendMessage(data.welcome_message, 'bot')
|
|
444
|
+
|
|
445
|
+
// Show customer info form after welcome message
|
|
446
|
+
if (!firstMessageReceived && captureCustomerInfo && customerForm && !customerInfoSubmitted) {
|
|
447
|
+
firstMessageReceived = true
|
|
448
|
+
// Small delay to let the message render first
|
|
449
|
+
setTimeout(() => {
|
|
450
|
+
customerForm.style.display = 'flex'
|
|
451
|
+
messages.style.display = 'none'
|
|
452
|
+
inputWrap.style.display = 'none'
|
|
453
|
+
// Focus first input
|
|
454
|
+
const nameInput = customerForm.querySelector('#acw-customer-name')
|
|
455
|
+
if (nameInput) setTimeout(() => nameInput.focus(), 100)
|
|
456
|
+
}, 300)
|
|
457
|
+
}
|
|
458
|
+
} else {
|
|
459
|
+
// No welcome message, but still show form if needed
|
|
460
|
+
if (!firstMessageReceived && captureCustomerInfo && customerForm && !customerInfoSubmitted) {
|
|
461
|
+
firstMessageReceived = true
|
|
462
|
+
setTimeout(() => {
|
|
463
|
+
customerForm.style.display = 'flex'
|
|
464
|
+
messages.style.display = 'none'
|
|
465
|
+
inputWrap.style.display = 'none'
|
|
466
|
+
const nameInput = customerForm.querySelector('#acw-customer-name')
|
|
467
|
+
if (nameInput) setTimeout(() => nameInput.focus(), 100)
|
|
468
|
+
}, 100)
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return // Don't process as regular message
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// Handle regular chat messages
|
|
442
475
|
const text = data.text || data.content || data.message || ''
|
|
443
476
|
const role = data.role || 'assistant'
|
|
444
|
-
if (text)
|
|
477
|
+
if (text) {
|
|
478
|
+
appendMessage(text, role === 'user' ? 'user' : 'bot')
|
|
479
|
+
}
|
|
445
480
|
} catch (e) {
|
|
446
481
|
// Fallback to raw text
|
|
447
|
-
if (event.data)
|
|
482
|
+
if (event.data) {
|
|
483
|
+
appendMessage(String(event.data), 'bot')
|
|
484
|
+
}
|
|
448
485
|
}
|
|
449
486
|
}
|
|
450
487
|
|
|
@@ -475,6 +512,7 @@
|
|
|
475
512
|
updateStatus('Disconnected', false)
|
|
476
513
|
sessionId = null
|
|
477
514
|
customerInfoSubmitted = false
|
|
515
|
+
firstMessageReceived = false
|
|
478
516
|
if (customerForm) {
|
|
479
517
|
customerForm.style.display = 'none'
|
|
480
518
|
const nameInput = customerForm.querySelector('#acw-customer-name')
|