@helllo-ai/agent-chat-widget 0.1.16 → 0.1.18
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.prod.js +75 -48
- package/agent-chat.staging.js +20 -31
- package/package.json +1 -1
package/agent-chat.prod.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')
|
|
@@ -251,7 +252,7 @@
|
|
|
251
252
|
customerForm.appendChild(phoneField)
|
|
252
253
|
customerForm.appendChild(formActions)
|
|
253
254
|
|
|
254
|
-
|
|
255
|
+
function submitCustomerInfo() {
|
|
255
256
|
const name = nameInput.value.trim()
|
|
256
257
|
const phone = phoneInput.value.trim()
|
|
257
258
|
|
|
@@ -262,44 +263,33 @@
|
|
|
262
263
|
|
|
263
264
|
if (!sessionId) {
|
|
264
265
|
console.error('[AgentChatWidget] No session ID available')
|
|
266
|
+
alert('Session not established. Please wait for connection.')
|
|
265
267
|
return
|
|
266
268
|
}
|
|
267
269
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
apiBase = apiBase.replace(/^ws/, 'http')
|
|
273
|
-
} else if (apiBase.startsWith('wss://')) {
|
|
274
|
-
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
|
|
275
274
|
}
|
|
276
|
-
const endpoint = `${apiBase}/api/v1/agents/text-chat/customer-info`
|
|
277
275
|
|
|
278
276
|
try {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
customer_name: name,
|
|
288
|
-
}),
|
|
289
|
-
})
|
|
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
|
+
}))
|
|
290
285
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
} else {
|
|
297
|
-
const errorText = await response.text()
|
|
298
|
-
console.error('[AgentChatWidget] Failed to submit customer info:', errorText)
|
|
299
|
-
alert('Failed to submit information. Please try again.')
|
|
300
|
-
}
|
|
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'
|
|
301
291
|
} catch (error) {
|
|
302
|
-
console.error('[AgentChatWidget] Error
|
|
292
|
+
console.error('[AgentChatWidget] Error sending customer info:', error)
|
|
303
293
|
alert('Error submitting information. Please try again.')
|
|
304
294
|
}
|
|
305
295
|
}
|
|
@@ -404,6 +394,7 @@
|
|
|
404
394
|
function connect() {
|
|
405
395
|
if (connected || connecting) return
|
|
406
396
|
connecting = true
|
|
397
|
+
firstMessageReceived = false // Reset when connecting
|
|
407
398
|
const url = resolvedWsUrl
|
|
408
399
|
try {
|
|
409
400
|
ws = new WebSocket(url)
|
|
@@ -419,32 +410,67 @@
|
|
|
419
410
|
connecting = false
|
|
420
411
|
updateStatus('Connected', true)
|
|
421
412
|
|
|
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
|
-
}
|
|
413
|
+
// Session ID will be received from connection_established message
|
|
414
|
+
// Don't show customer form yet - wait for first message
|
|
415
|
+
messages.style.display = 'flex'
|
|
416
|
+
inputWrap.style.display = 'flex'
|
|
437
417
|
}
|
|
438
418
|
|
|
439
419
|
ws.onmessage = (event) => {
|
|
440
420
|
try {
|
|
441
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
|
|
442
464
|
const text = data.text || data.content || data.message || ''
|
|
443
465
|
const role = data.role || 'assistant'
|
|
444
|
-
if (text)
|
|
466
|
+
if (text) {
|
|
467
|
+
appendMessage(text, role === 'user' ? 'user' : 'bot')
|
|
468
|
+
}
|
|
445
469
|
} catch (e) {
|
|
446
470
|
// Fallback to raw text
|
|
447
|
-
if (event.data)
|
|
471
|
+
if (event.data) {
|
|
472
|
+
appendMessage(String(event.data), 'bot')
|
|
473
|
+
}
|
|
448
474
|
}
|
|
449
475
|
}
|
|
450
476
|
|
|
@@ -475,6 +501,7 @@
|
|
|
475
501
|
updateStatus('Disconnected', false)
|
|
476
502
|
sessionId = null
|
|
477
503
|
customerInfoSubmitted = false
|
|
504
|
+
firstMessageReceived = false
|
|
478
505
|
if (customerForm) {
|
|
479
506
|
customerForm.style.display = 'none'
|
|
480
507
|
const nameInput = customerForm.querySelector('#acw-customer-name')
|
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
|
}
|