@helllo-ai/agent-chat-widget 0.1.15 → 0.1.17
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 +63 -60
- package/package.json +1 -1
package/agent-chat.staging.js
CHANGED
|
@@ -252,7 +252,7 @@
|
|
|
252
252
|
customerForm.appendChild(phoneField)
|
|
253
253
|
customerForm.appendChild(formActions)
|
|
254
254
|
|
|
255
|
-
|
|
255
|
+
function submitCustomerInfo() {
|
|
256
256
|
const name = nameInput.value.trim()
|
|
257
257
|
const phone = phoneInput.value.trim()
|
|
258
258
|
|
|
@@ -263,44 +263,33 @@
|
|
|
263
263
|
|
|
264
264
|
if (!sessionId) {
|
|
265
265
|
console.error('[AgentChatWidget] No session ID available')
|
|
266
|
+
alert('Session not established. Please wait for connection.')
|
|
266
267
|
return
|
|
267
268
|
}
|
|
268
269
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
apiBase = apiBase.replace(/^ws/, 'http')
|
|
274
|
-
} else if (apiBase.startsWith('wss://')) {
|
|
275
|
-
apiBase = apiBase.replace(/^wss/, 'https')
|
|
270
|
+
// Ensure WebSocket is connected
|
|
271
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
|
272
|
+
alert('WebSocket not connected. Please wait for connection.')
|
|
273
|
+
return
|
|
276
274
|
}
|
|
277
|
-
const endpoint = `${apiBase}/api/v1/agent-voice/agents/text-chat/customer-info`
|
|
278
275
|
|
|
279
276
|
try {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
customer_name: name,
|
|
289
|
-
}),
|
|
290
|
-
})
|
|
277
|
+
// Send customer info via WebSocket
|
|
278
|
+
ws.send(JSON.stringify({
|
|
279
|
+
type: 'customer_info',
|
|
280
|
+
session_id: sessionId,
|
|
281
|
+
phone_number: phone,
|
|
282
|
+
customer_name: name,
|
|
283
|
+
embed_key: embedKey
|
|
284
|
+
}))
|
|
291
285
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
} else {
|
|
298
|
-
const errorText = await response.text()
|
|
299
|
-
console.error('[AgentChatWidget] Failed to submit customer info:', errorText)
|
|
300
|
-
alert('Failed to submit information. Please try again.')
|
|
301
|
-
}
|
|
286
|
+
// Mark as submitted and show chat interface
|
|
287
|
+
customerInfoSubmitted = true
|
|
288
|
+
customerForm.style.display = 'none'
|
|
289
|
+
messages.style.display = 'flex'
|
|
290
|
+
inputWrap.style.display = 'flex'
|
|
302
291
|
} catch (error) {
|
|
303
|
-
console.error('[AgentChatWidget] Error
|
|
292
|
+
console.error('[AgentChatWidget] Error sending customer info:', error)
|
|
304
293
|
alert('Error submitting information. Please try again.')
|
|
305
294
|
}
|
|
306
295
|
}
|
|
@@ -421,9 +410,7 @@
|
|
|
421
410
|
connecting = false
|
|
422
411
|
updateStatus('Connected', true)
|
|
423
412
|
|
|
424
|
-
//
|
|
425
|
-
sessionId = 'session_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9)
|
|
426
|
-
|
|
413
|
+
// Session ID will be received from connection_established message
|
|
427
414
|
// Don't show customer form yet - wait for first message
|
|
428
415
|
messages.style.display = 'flex'
|
|
429
416
|
inputWrap.style.display = 'flex'
|
|
@@ -432,41 +419,57 @@
|
|
|
432
419
|
ws.onmessage = (event) => {
|
|
433
420
|
try {
|
|
434
421
|
const data = JSON.parse(event.data)
|
|
422
|
+
|
|
423
|
+
// Handle connection_established message
|
|
424
|
+
if (data.type === 'connection_established') {
|
|
425
|
+
// Extract session_id from connection message
|
|
426
|
+
if (data.session_id) {
|
|
427
|
+
sessionId = data.session_id
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Display welcome message if provided
|
|
431
|
+
if (data.welcome_message) {
|
|
432
|
+
appendMessage(data.welcome_message, 'bot')
|
|
433
|
+
|
|
434
|
+
// Show customer info form after welcome message
|
|
435
|
+
if (!firstMessageReceived && captureCustomerInfo && customerForm && !customerInfoSubmitted) {
|
|
436
|
+
firstMessageReceived = true
|
|
437
|
+
// Small delay to let the message render first
|
|
438
|
+
setTimeout(() => {
|
|
439
|
+
customerForm.style.display = 'flex'
|
|
440
|
+
messages.style.display = 'none'
|
|
441
|
+
inputWrap.style.display = 'none'
|
|
442
|
+
// Focus first input
|
|
443
|
+
const nameInput = customerForm.querySelector('#acw-customer-name')
|
|
444
|
+
if (nameInput) setTimeout(() => nameInput.focus(), 100)
|
|
445
|
+
}, 300)
|
|
446
|
+
}
|
|
447
|
+
} else {
|
|
448
|
+
// No welcome message, but still show form if needed
|
|
449
|
+
if (!firstMessageReceived && captureCustomerInfo && customerForm && !customerInfoSubmitted) {
|
|
450
|
+
firstMessageReceived = true
|
|
451
|
+
setTimeout(() => {
|
|
452
|
+
customerForm.style.display = 'flex'
|
|
453
|
+
messages.style.display = 'none'
|
|
454
|
+
inputWrap.style.display = 'none'
|
|
455
|
+
const nameInput = customerForm.querySelector('#acw-customer-name')
|
|
456
|
+
if (nameInput) setTimeout(() => nameInput.focus(), 100)
|
|
457
|
+
}, 100)
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return // Don't process as regular message
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// Handle regular chat messages
|
|
435
464
|
const text = data.text || data.content || data.message || ''
|
|
436
465
|
const role = data.role || 'assistant'
|
|
437
466
|
if (text) {
|
|
438
467
|
appendMessage(text, role === 'user' ? 'user' : 'bot')
|
|
439
|
-
|
|
440
|
-
// Show customer info form after first bot message (welcome message)
|
|
441
|
-
if (!firstMessageReceived && role !== 'user' && captureCustomerInfo && customerForm && !customerInfoSubmitted) {
|
|
442
|
-
firstMessageReceived = true
|
|
443
|
-
// Small delay to let the message render first
|
|
444
|
-
setTimeout(() => {
|
|
445
|
-
customerForm.style.display = 'flex'
|
|
446
|
-
messages.style.display = 'none'
|
|
447
|
-
inputWrap.style.display = 'none'
|
|
448
|
-
// Focus first input
|
|
449
|
-
const nameInput = customerForm.querySelector('#acw-customer-name')
|
|
450
|
-
if (nameInput) setTimeout(() => nameInput.focus(), 100)
|
|
451
|
-
}, 300)
|
|
452
|
-
}
|
|
453
468
|
}
|
|
454
469
|
} catch (e) {
|
|
455
470
|
// Fallback to raw text
|
|
456
471
|
if (event.data) {
|
|
457
472
|
appendMessage(String(event.data), 'bot')
|
|
458
|
-
|
|
459
|
-
// Show customer info form after first bot message (welcome message)
|
|
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
|
-
}, 300)
|
|
469
|
-
}
|
|
470
473
|
}
|
|
471
474
|
}
|
|
472
475
|
}
|