@helllo-ai/agent-chat-widget 0.1.17 → 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.
Files changed (2) hide show
  1. package/agent-chat.prod.js +75 -48
  2. package/package.json +1 -1
@@ -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
- async function submitCustomerInfo() {
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
- const baseUrl = voiceServiceUrl || apiBaseUrl || new URL(document.currentScript?.src || window.location.href).origin
269
- let apiBase = (baseUrl || '').replace(/\/$/, '')
270
- // Convert ws:// to http:// for HTTP requests
271
- if (apiBase.startsWith('ws://')) {
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
- const response = await fetch(endpoint, {
280
- method: 'POST',
281
- headers: {
282
- 'Content-Type': 'application/json',
283
- },
284
- body: JSON.stringify({
285
- session_id: sessionId,
286
- phone_number: phone,
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
- if (response.ok) {
292
- customerInfoSubmitted = true
293
- customerForm.style.display = 'none'
294
- messages.style.display = 'flex'
295
- inputWrap.style.display = 'flex'
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 submitting customer info:', 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
- // Generate session ID when websocket connects
423
- sessionId = 'session_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9)
424
-
425
- // Show customer info form if enabled and not yet submitted
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) appendMessage(text, role === 'user' ? 'user' : 'bot')
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) appendMessage(String(event.data), 'bot')
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@helllo-ai/agent-chat-widget",
3
- "version": "0.1.17",
3
+ "version": "0.1.18",
4
4
  "description": "Bot Swarm Agent Chat Widget - Embeddable chat widget for AI agents",
5
5
  "main": "agent-chat.latest.js",
6
6
  "files": [