-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket-server.ts
More file actions
828 lines (733 loc) · 20 KB
/
Copy pathwebsocket-server.ts
File metadata and controls
828 lines (733 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
import { WebSocketServer, WebSocket } from 'ws'
import http from 'http'
import {
getElevenLabsWebSocket,
cleanupElevenLabsWebSocket,
} from './lib/elevenlabs-websocket'
import {
streamAIToSpeech,
StreamingAIMessage,
} from './lib/streaming-ai-service'
import { transcribeAudio } from './lib/ai-service'
import { loadPrompts, buildSystemPrompt } from './lib/ai-service'
import { notepadService } from './lib/notepad-service'
import { vaultService } from './lib/vault-service'
import { checkConversationComplete } from './lib/message-service'
// Disable bufferutil to prevent bundler compatibility issues
process.env.WS_NO_BUFFER_UTIL = '1'
// WebSocket message types
interface ClientMessage {
type: 'audio' | 'text' | 'initialize' | 'disconnect' | 'heartbeat'
data?: string // Base64 audio or text content
contextId?: string
conversationId?: string
isWelcomeMessage?: boolean
elapsedTime?: string
usePaywallPrompt?: boolean
userName?: string // User's first name for personalization
}
interface ServerMessage {
type: 'audio' | 'text' | 'transcription' | 'error' | 'complete'
data?: string | Uint8Array
contextId?: string
conversationId?: string
isComplete?: boolean
transcribedText?: string
responseText?: string
}
// Create HTTP server (nginx handles SSL termination)
const server = http.createServer()
const port = parseInt(process.env.WS_PORT || '8080')
const protocol = 'ws'
const wss = new WebSocketServer({
server,
perMessageDeflate: false,
maxPayload: 50 * 1024 * 1024, // 50MB max payload
skipUTF8Validation: true, // Skip UTF8 validation for performance
})
// Connection management
const connections = new Map<
WebSocket,
{
userId?: string
contextId: string
elevenLabsService: any
isAuthenticated: boolean
hasSubscription: boolean
conversationId?: string
messageHistory: StreamingAIMessage[]
isProcessing: boolean
pingInterval?: NodeJS.Timeout
lastPong: number
isAlive: boolean
}
>()
// Add process-level error handlers to prevent crashes
process.on('uncaughtException', (error) => {
// Don't exit the process, just continue
})
process.on('unhandledRejection', (reason, promise) => {
// Continue despite unhandled rejection
})
process.on('exit', (code) => {
// Process exiting
})
process.on('SIGTERM', (signal) => {
// Signal received
})
process.on('SIGINT', (signal) => {
// Signal received
})
/**
* Start heartbeat for a WebSocket connection
*/
const startHeartbeat = (ws: WebSocket) => {
const connectionInfo = connections.get(ws)
if (!connectionInfo) return
// Clear any existing interval
if (connectionInfo.pingInterval) {
clearInterval(connectionInfo.pingInterval)
}
// Check for heartbeat every 30 seconds
connectionInfo.pingInterval = setInterval(() => {
// Validate WebSocket still exists and is in valid state
if (!ws || ws.readyState !== WebSocket.OPEN) {
stopHeartbeat(ws)
return
}
const now = Date.now()
const timeSinceLastActivity = now - connectionInfo.lastPong
// If no heartbeat received in 60 seconds, close connection
if (timeSinceLastActivity > 60000) {
stopHeartbeat(ws)
// Safely close the connection
try {
// Remove from connections map first
connections.delete(ws)
// Then close the WebSocket
if (
ws.readyState === WebSocket.OPEN ||
ws.readyState === WebSocket.CONNECTING
) {
ws.close(1001, 'Heartbeat timeout')
}
} catch (error) {
// Error terminating connection
}
return
}
// Request heartbeat from client
sendMessage(ws, {
type: 'complete',
data: 'heartbeat_request',
contextId: connectionInfo.contextId,
})
}, 30000) // 30 seconds
// Mark connection as alive initially
connectionInfo.isAlive = true
connectionInfo.lastPong = Date.now()
}
/**
* Stop heartbeat for a WebSocket connection
*/
const stopHeartbeat = (ws: WebSocket) => {
const connectionInfo = connections.get(ws)
if (connectionInfo && connectionInfo.pingInterval) {
clearInterval(connectionInfo.pingInterval)
connectionInfo.pingInterval = undefined
}
}
/**
* Send message to WebSocket client
*/
const sendMessage = (ws: WebSocket, message: ServerMessage) => {
if (ws.readyState === WebSocket.OPEN) {
try {
ws.send(JSON.stringify(message))
} catch (error) {
// Error sending message
}
}
}
/**
* Send error message to WebSocket client
*/
const sendErrorMessage = (ws: WebSocket, error: string) => {
sendMessage(ws, {
type: 'error',
data: error,
})
}
/**
* Handle messages from clients
*/
const handleClientMessage = async (ws: WebSocket, message: ClientMessage) => {
const connectionInfo = connections.get(ws)
if (!connectionInfo) {
sendErrorMessage(ws, 'Connection not found')
return
}
// Update last activity on any message
connectionInfo.lastPong = Date.now()
connectionInfo.isAlive = true
const {
type,
data,
contextId,
conversationId,
isWelcomeMessage,
elapsedTime,
usePaywallPrompt,
userName,
} = message
try {
switch (type) {
case 'initialize':
await handleInitialize(ws, connectionInfo)
break
case 'audio':
if (!data) {
sendErrorMessage(ws, 'Audio data is required')
return
}
await handleAudioMessage(ws, connectionInfo, data, {
conversationId,
isWelcomeMessage,
elapsedTime,
usePaywallPrompt,
userName,
})
break
case 'text':
if (!data) {
sendErrorMessage(ws, 'Text data is required')
return
}
await handleTextMessage(ws, connectionInfo, data, {
conversationId,
isWelcomeMessage,
elapsedTime,
usePaywallPrompt,
userName,
})
break
case 'disconnect':
await handleDisconnect(ws, connectionInfo)
break
case 'heartbeat':
// Update last activity time
connectionInfo.lastPong = Date.now()
connectionInfo.isAlive = true
sendMessage(ws, {
type: 'complete',
data: 'heartbeat_ack',
contextId: connectionInfo.contextId,
})
break
default:
sendErrorMessage(ws, `Unknown message type: ${type}`)
}
} catch (error) {
sendErrorMessage(
ws,
error instanceof Error ? error.message : 'Unknown error'
)
}
}
/**
* Handle connection initialization
*/
const handleInitialize = async (ws: WebSocket, connectionInfo: any) => {
try {
// Skip authentication for WebSocket connections
connectionInfo.userId = null
connectionInfo.isAuthenticated = false
connectionInfo.hasSubscription = true // Allow for preview/demo mode
// Initialize ElevenLabs WebSocket connection
if (!connectionInfo.elevenLabsService.connected) {
try {
await connectionInfo.elevenLabsService.connect()
// Wait a bit for connection to be fully ready
await new Promise((resolve) => setTimeout(resolve, 100))
} catch (error) {
console.error(
'🔌 [Voice Stream WS] Failed to connect to ElevenLabs:',
error
)
// Don't throw, just log - connection might work later
}
}
sendMessage(ws, {
type: 'complete',
contextId: connectionInfo.contextId,
data: 'Initialized successfully',
})
console.log(
`🔌 [Voice Stream WS] Initialized connection for user: anonymous`
)
} catch (error) {
sendErrorMessage(ws, 'Failed to initialize connection')
}
}
/**
* Handle audio input messages
*/
const handleAudioMessage = async (
ws: WebSocket,
connectionInfo: any,
audioData: string,
options: {
conversationId?: string
isWelcomeMessage?: boolean
elapsedTime?: string
usePaywallPrompt?: boolean
userName?: string
}
) => {
try {
// Convert base64 audio to Blob (works in server environments)
const audioBuffer = Buffer.from(audioData, 'base64')
// Create a Blob-like object that works with transcribeAudio
const audioFile = new Blob([audioBuffer], {
type: 'audio/webm',
}) as any
// Add name property for compatibility
;(audioFile as any).name = 'audio.webm'
// Transcribe audio
const transcribedText = await transcribeAudio(audioFile)
// Send transcription to client
sendMessage(ws, {
type: 'transcription',
data: transcribedText,
contextId: connectionInfo.contextId,
})
// Check if transcription is empty
if (!transcribedText || transcribedText.trim() === '') {
// Send completion message with empty transcription flag
sendMessage(ws, {
type: 'complete',
data: 'No speech detected',
contextId: connectionInfo.contextId,
transcribedText: '',
responseText: 'No speech detected',
})
return
}
// Process the transcribed text
await processUserInput(ws, connectionInfo, transcribedText, options)
} catch (error) {
sendErrorMessage(ws, 'Failed to process audio')
}
}
/**
* Handle text input messages
*/
const handleTextMessage = async (
ws: WebSocket,
connectionInfo: any,
text: string,
options: {
conversationId?: string
isWelcomeMessage?: boolean
elapsedTime?: string
usePaywallPrompt?: boolean
userName?: string
}
) => {
try {
await processUserInput(ws, connectionInfo, text, options)
} catch (error) {
sendErrorMessage(ws, 'Failed to process text')
}
}
/**
* Process user input and generate streaming response
*/
const processUserInput = async (
ws: WebSocket,
connectionInfo: any,
userInput: string,
options: {
conversationId?: string
isWelcomeMessage?: boolean
elapsedTime?: string
usePaywallPrompt?: boolean
userName?: string
}
) => {
const { userId, contextId, elevenLabsService } = connectionInfo
const {
conversationId,
isWelcomeMessage,
elapsedTime,
usePaywallPrompt,
userName,
} = options
// Check if already processing
if (connectionInfo.isProcessing) {
return
}
// Set processing flag
connectionInfo.isProcessing = true
try {
// Handle welcome message
if (isWelcomeMessage) {
const welcomeMessage = userId
? "Hey! I'm Dena, your AI advisor. I'm here to help you navigate the challenges of building and scaling your B2B startup. What's on your mind today?"
: "Hi there! It's great to meet you. Quick question—how did you learn about me?"
// Stream welcome message
await streamResponse(ws, connectionInfo, [], welcomeMessage)
return
}
// Build conversation context
const messages = await buildConversationContext(
userId,
userInput,
connectionInfo.messageHistory,
elapsedTime,
usePaywallPrompt,
userName
)
// Simple approach: just start the new request without complex cleanup
// Start streaming AI response with TTS
try {
await streamAIResponse(ws, connectionInfo, messages)
} catch (error) {
sendErrorMessage(ws, 'Failed to generate AI response')
}
} catch (error) {
sendErrorMessage(ws, 'Failed to process input')
} finally {
// Clear processing flag
connectionInfo.isProcessing = false
}
}
/**
* Build conversation context with prompts and user data
*/
const buildConversationContext = async (
userId: string | undefined,
userInput: string,
messageHistory: StreamingAIMessage[],
elapsedTime?: string,
usePaywallPrompt?: boolean,
userName?: string
): Promise<StreamingAIMessage[]> => {
// Load prompts - support paywall prompt for initial paywall conversations
const promptPaths = usePaywallPrompt
? ['base_prompt.txt', 'initial_paywall_prompt.txt'] // For paywall conversation
: userId
? ['base_prompt.txt', 'advisory_coach_prompt.txt']
: ['base_prompt.txt', 'preview_prompt.txt']
const prompts = await loadPrompts(promptPaths)
// Get notepad context if authenticated
let notepadContext = ''
if (userId) {
const notepadItems = await notepadService.getNotepadItems(userId)
if (notepadItems.length > 0) {
notepadContext = `\n\nCURRENT NOTEPAD ITEMS:\n${notepadItems
.map(
(item) =>
`- ${item.type.toUpperCase()}: ${item.content}${
item.type === 'action_item' && !item.completed
? ' [INCOMPLETE]'
: ''
}`
)
.join('\n')}`
} else {
notepadContext = '\n\nNOTEPAD: Currently empty'
}
}
// Get vault context if authenticated
let vaultContext = ''
if (userId) {
const vaultItems = await vaultService.getVaultItems(userId)
if (vaultItems.length > 0) {
vaultContext = `\n\nVAULT CLARITY SESSIONS:\n${vaultItems
.map(
(item, index) =>
`Session ${index + 1} (${new Date(
item.created_at
).toLocaleDateString()}):\n` +
` Challenge: ${item.original_challenge}\n` +
` Product: ${item.product_description}\n` +
` Problem: ${item.problem_statement}\n` +
` Target Customer: ${item.ideal_customer_profile}\n` +
` Value Prop: ${item.value_proposition}\n` +
` Attempted Solutions: ${item.attempted_solutions}\n` +
` Recommendations: ${item.recommended_solutions}`
)
.join('\n\n')}`
} else {
vaultContext = '\n\nVAULT: No clarity sessions saved yet'
}
}
// Build system prompt with user name for paywall conversations
let systemPromptContext = notepadContext + vaultContext
if (usePaywallPrompt && userName) {
systemPromptContext += `\n\nUSER'S NAME: ${userName} (use this name naturally in conversation)`
}
const systemPrompt = buildSystemPrompt({
basePrompt: prompts[0],
extensions: prompts.slice(1),
context: systemPromptContext,
elapsedTime,
})
// Build conversation with history
const messages: StreamingAIMessage[] = [
{ role: 'system', content: systemPrompt },
...messageHistory,
{ role: 'user', content: userInput },
]
return messages
}
/**
* Stream AI response with real-time TTS
*/
const streamAIResponse = async (
ws: WebSocket,
connectionInfo: any,
messages: StreamingAIMessage[]
) => {
const { contextId, elevenLabsService } = connectionInfo
try {
// Start the streaming pipeline
const { textStream, audioStream } = await streamAIToSpeech(
messages,
elevenLabsService,
contextId,
{
onTextChunk: (chunk: string) => {
// Send text chunks to client in real-time
sendMessage(ws, {
type: 'text',
data: chunk,
contextId,
})
},
onAudioChunk: (audioBuffer: Buffer) => {
// Optimize audio chunk delivery for seamless playback
const audioData = new Uint8Array(audioBuffer)
// Skip tiny chunks that can cause glitches
if (audioData.length < 64) {
return
}
// Send audio chunks to client with proper buffering info
sendMessage(ws, {
type: 'audio',
data: audioData,
contextId,
})
},
onComplete: (fullText: string) => {
// Add user message and AI response to conversation history
const userMessage = messages[messages.length - 1] // Last message is user input
connectionInfo.messageHistory.push(userMessage)
connectionInfo.messageHistory.push({
role: 'assistant',
content: fullText,
})
// Keep history manageable (last 20 messages)
if (connectionInfo.messageHistory.length > 20) {
connectionInfo.messageHistory =
connectionInfo.messageHistory.slice(-20)
}
// Check if this is a conversation completion (for preview mode)
const isComplete = checkConversationComplete(fullText)
console.log('[WebSocket Server] Checking conversation completion:', {
fullTextLength: fullText.length,
last100Chars: fullText.slice(-100),
isComplete,
hasEndingPhrase: fullText.includes("Let's keep talking"),
})
// Send completion message with isComplete flag
sendMessage(ws, {
type: 'complete',
data: fullText,
contextId,
responseText: fullText,
isComplete,
})
},
onError: (error: Error) => {
sendErrorMessage(ws, error.message)
},
}
)
} catch (error) {
sendErrorMessage(ws, 'Failed to generate response')
}
}
/**
* Stream a pre-generated response (for welcome messages)
*/
const streamResponse = async (
ws: WebSocket,
connectionInfo: any,
messages: StreamingAIMessage[],
responseText: string
) => {
const { contextId, elevenLabsService } = connectionInfo
try {
// Send text in chunks to simulate streaming
const words = responseText.split(' ')
let currentText = ''
for (const word of words) {
currentText += word + ' '
sendMessage(ws, {
type: 'text',
data: word + ' ',
contextId,
})
// Small delay to simulate streaming
await new Promise((resolve) => setTimeout(resolve, 50))
}
// Generate audio for the complete response
if (!elevenLabsService.connected) {
await elevenLabsService.connect()
}
// No listeners here - let ElevenLabs service handle everything internally
// Just register our callback for this context
elevenLabsService.setAudioCallback(contextId, (audioBuffer: Buffer) => {
sendMessage(ws, {
type: 'audio',
data: new Uint8Array(audioBuffer),
contextId,
})
})
elevenLabsService.setCompleteCallback(contextId, () => {
// Check if this is a conversation completion (for preview mode)
const isComplete = checkConversationComplete(responseText)
console.log('[WebSocket Server] Welcome message completion check:', {
responseTextLength: responseText.length,
isComplete,
})
sendMessage(ws, {
type: 'complete',
data: responseText,
contextId,
responseText,
isComplete,
})
})
// Send text to TTS
await elevenLabsService.sendText(responseText, contextId, true)
} catch (error) {
sendErrorMessage(ws, 'Failed to stream response')
}
}
/**
* Handle disconnect messages
*/
const handleDisconnect = async (ws: WebSocket, connectionInfo: any) => {
try {
// Stop heartbeat first
stopHeartbeat(ws)
// Clean up ElevenLabs context
if (connectionInfo.elevenLabsService) {
await connectionInfo.elevenLabsService.closeContext(
connectionInfo.contextId
)
}
// Close WebSocket connection if still open
if (ws.readyState === WebSocket.OPEN) {
ws.close(1000, 'Client disconnect')
}
} catch (error) {}
}
// Handle new WebSocket connections
wss.on('connection', async (ws: WebSocket, request: any) => {
// Generate unique context ID for this connection
const contextId = `voice_${Date.now()}_${Math.random()
.toString(36)
.substr(2, 9)}`
// Initialize ElevenLabs WebSocket service for this connection
const elevenLabsService = getElevenLabsWebSocket(contextId)
// Store connection info
connections.set(ws, {
contextId,
elevenLabsService,
isAuthenticated: false,
hasSubscription: true,
messageHistory: [],
isProcessing: false, // Add processing flag
isAlive: true,
lastPong: Date.now(),
})
// Set up message handler
ws.on('message', async (data: Buffer) => {
try {
const message: ClientMessage = JSON.parse(data.toString())
await handleClientMessage(ws, message)
} catch (error) {
sendErrorMessage(ws, 'Invalid message format')
}
})
// Note: Browser WebSocket clients don't expose ping/pong to JavaScript
// So we'll handle heartbeat at application level instead
// Handle connection close
ws.on('close', (code, reason) => {
const connectionInfo = connections.get(ws)
if (connectionInfo) {
// Stop heartbeat
stopHeartbeat(ws)
// Clean up ElevenLabs instance completely
try {
cleanupElevenLabsWebSocket(connectionInfo.contextId)
} catch (error) {}
}
connections.delete(ws)
})
// Handle errors
ws.on('error', (error) => {
const connectionInfo = connections.get(ws)
if (connectionInfo) {
stopHeartbeat(ws)
try {
cleanupElevenLabsWebSocket(connectionInfo.contextId)
} catch (cleanupError) {}
}
connections.delete(ws)
})
// Send initial connection confirmation
sendMessage(ws, {
type: 'complete',
contextId,
data: 'Connected to voice streaming service',
})
// Start heartbeat
startHeartbeat(ws)
})
// Add error handling for the WebSocket server
wss.on('error', (error) => {
// WebSocket server error
})
server.listen(port, '0.0.0.0', () => {
// WebSocket server running
})
// Graceful shutdown
const gracefulShutdown = (signal: string) => {
// Stop all heartbeats first
for (const [ws, connectionInfo] of connections.entries()) {
stopHeartbeat(ws)
try {
cleanupElevenLabsWebSocket(connectionInfo.contextId)
} catch (error) {}
}
// Clear connections
connections.clear()
// Close WebSocket server
wss.close((err) => {
// Close HTTP server
server.close((err) => {
process.exit(0)
})
})
// Force exit after 5 seconds if graceful shutdown fails
setTimeout(() => {
process.exit(1)
}, 5000)
}
process.on('SIGINT', () => gracefulShutdown('SIGINT'))
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'))