File size: 18,442 Bytes
3d67472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a865e9
 
 
3d67472
0a865e9
 
3d67472
 
 
 
 
 
 
 
0a865e9
3d67472
 
 
0a865e9
 
 
 
3d67472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d89cff8
 
 
 
 
 
 
 
 
 
3d67472
 
 
d89cff8
 
 
 
 
 
 
 
3d67472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
693f75f
3d67472
693f75f
d979b59
 
 
 
3d67472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
693f75f
 
 
 
 
d979b59
 
 
 
 
 
 
 
 
3d67472
 
 
 
 
 
 
 
 
 
693f75f
3d67472
 
 
 
 
693f75f
 
 
e3892d4
693f75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d67472
693f75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3892d4
693f75f
 
 
 
 
e3892d4
 
83cfdf5
59cbda6
e2e2d5a
 
402f4b1
eff3c3d
acf2bbe
693f75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd93e5e
83cfdf5
e2e2d5a
 
59cbda6
402f4b1
eff3c3d
acf2bbe
693f75f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d67472
 
 
d979b59
 
 
 
 
 
 
 
 
 
 
a6ea22d
83cfdf5
59cbda6
e2e2d5a
 
402f4b1
eff3c3d
acf2bbe
d979b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83cfdf5
e2e2d5a
 
59cbda6
402f4b1
eff3c3d
a6ea22d
acf2bbe
d979b59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d67472
e3892d4
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
"""WebSocket endpoint for real-time conversation streaming.

This module handles WebSocket connections for AI-to-AI conversations,
providing real-time message streaming between the frontend and backend.

Features:
- Real-time bidirectional messaging
- Connection management with heartbeat
- Message validation and formatting
- Error handling and graceful disconnections
- Conversation-specific channels

Usage:
    Connect to: /ws/conversation/{conversation_id}
"""

import json
import logging
from typing import Dict, Set
from datetime import datetime
from uuid import uuid4

from fastapi import WebSocket, WebSocketDisconnect
from fastapi.websockets import WebSocketState

# Setup logging
logger = logging.getLogger(__name__)

# Connection manager for active WebSocket connections
class ConnectionManager:
    """Manages active WebSocket connections for conversations."""
    
    def __init__(self):
        """Initialize connection manager."""
        self.active_connections: Dict[str, Set[WebSocket]] = {}
        
    async def connect(self, websocket: WebSocket, conversation_id: str):
        """Accept and register a new WebSocket connection.
        
        Args:
            websocket: The WebSocket connection
            conversation_id: ID of the conversation to join
        """
        await websocket.accept()
        
        if conversation_id not in self.active_connections:
            self.active_connections[conversation_id] = set()
        
        self.active_connections[conversation_id].add(websocket)
        logger.info(f"WebSocket connected to conversation {conversation_id}")
        
        # Send connection confirmation
        await websocket.send_json({
            "type": "connection_status",
            "status": "connected",
            "conversation_id": conversation_id,
            "timestamp": datetime.now().isoformat(),
            "message": "Connected to conversation"
        })
        
    def disconnect(self, websocket: WebSocket, conversation_id: str):
        """Remove a WebSocket connection.
        
        Args:
            websocket: The WebSocket connection to remove
            conversation_id: ID of the conversation
        """
        if conversation_id in self.active_connections:
            self.active_connections[conversation_id].discard(websocket)
            
            # Clean up empty conversation rooms
            if not self.active_connections[conversation_id]:
                del self.active_connections[conversation_id]
                
        logger.info(f"WebSocket disconnected from conversation {conversation_id}")
        
    async def send_to_conversation(self, conversation_id: str, message: dict):
        """Send message to all connections in a conversation.
        
        Args:
            conversation_id: Target conversation ID
            message: Message dict to send
        """
        if conversation_id in self.active_connections:
            connections_copy = list(self.active_connections[conversation_id])
            logger.info(f"Sending message to {len(connections_copy)} connections for conversation {conversation_id}")

            disconnected = []

            for websocket in connections_copy:
                try:
                    if websocket.client_state == WebSocketState.CONNECTED:
                        await websocket.send_json(message)
                    else:
                        disconnected.append(websocket)
                except Exception as e:
                    logger.error(f"Error sending message to WebSocket: {e}")
                    disconnected.append(websocket)

            # Clean up disconnected sockets
            for websocket in disconnected:
                self.active_connections[conversation_id].discard(websocket)

            logger.info(f"Message sent successfully to {len(connections_copy) - len(disconnected)} connections, {len(disconnected)} disconnected")
        else:
            logger.warning(f"No active connections found for conversation {conversation_id}")
                
    async def broadcast_to_all(self, message: dict):
        """Broadcast message to all active connections.
        
        Args:
            message: Message dict to broadcast
        """
        for conversation_id in self.active_connections:
            await self.send_to_conversation(conversation_id, message)


# Global connection manager instance
manager = ConnectionManager()


async def websocket_endpoint(websocket: WebSocket, conversation_id: str):
    """WebSocket endpoint for conversation streaming.
    
    Args:
        websocket: The WebSocket connection
        conversation_id: Unique identifier for the conversation
    """
    await manager.connect(websocket, conversation_id)
    
    try:
        while True:
            # Receive message from client
            data = await websocket.receive_json()
            
            # Validate message format
            if not validate_message(data):
                await websocket.send_json({
                    "type": "error",
                    "error": "Invalid message format",
                    "timestamp": datetime.now().isoformat()
                })
                continue
                
            # Add server-side metadata
            data["timestamp"] = datetime.now().isoformat()
            data["message_id"] = str(uuid4())
            
            # Log the message
            logger.info(f"Received message in conversation {conversation_id}: {data['type']}")
            
            # Handle different message types
            await handle_message(data, conversation_id)
            
    except WebSocketDisconnect:
        manager.disconnect(websocket, conversation_id)
        # If the last client disconnects, treat it as a stop signal so we don't keep
        # generating LLM calls with no listeners (e.g., frontend bridge closed).
        if conversation_id not in manager.active_connections:
            try:
                from .conversation_service import get_conversation_service

                service = get_conversation_service()
                await service.stop_conversation(conversation_id)
            except Exception as e:
                logger.error(f"Error stopping conversation after disconnect ({conversation_id}): {e}")
    except Exception as e:
        logger.error(f"WebSocket error in conversation {conversation_id}: {e}")
        manager.disconnect(websocket, conversation_id)
        if conversation_id not in manager.active_connections:
            try:
                from .conversation_service import get_conversation_service

                service = get_conversation_service()
                await service.stop_conversation(conversation_id)
            except Exception as stop_err:
                logger.error(f"Error stopping conversation after websocket error ({conversation_id}): {stop_err}")


def validate_message(data: dict) -> bool:
    """Validate incoming WebSocket message format.
    
    Args:
        data: Message data to validate
        
    Returns:
        True if message is valid
    """
    required_fields = ["type", "content"]
    
    if not isinstance(data, dict):
        return False
        
    for field in required_fields:
        if field not in data:
            logger.warning(f"Missing required field: {field}")
            return False
            
    # Validate message types
    valid_types = [
        "conversation_message",
        "typing_indicator",
        "conversation_control",
        "heartbeat",
        "start_conversation",
        "start_human_chat",
        "human_chat_message",
        "end_human_chat",
    ]
    
    if data["type"] not in valid_types:
        logger.warning(f"Invalid message type: {data['type']}")
        return False
        
    return True


async def handle_message(data: dict, conversation_id: str):
    """Handle different types of WebSocket messages.
    
    Args:
        data: Validated message data
        conversation_id: Target conversation ID
    """
    message_type = data["type"]
    
    if message_type == "conversation_message":
        # Forward conversation messages to all participants
        await manager.send_to_conversation(conversation_id, data)
        
    elif message_type == "typing_indicator":
        # Forward typing indicators
        await manager.send_to_conversation(conversation_id, data)
        
    elif message_type == "conversation_control":
        # Handle conversation control (start, pause, stop)
        await handle_conversation_control(data, conversation_id)

    elif message_type == "start_conversation":
        # Handle starting a new conversation
        await handle_start_conversation(data, conversation_id)

    elif message_type == "start_human_chat":
        await handle_start_human_chat(data, conversation_id)

    elif message_type == "human_chat_message":
        await handle_human_chat_message(data, conversation_id)

    elif message_type == "end_human_chat":
        await handle_end_human_chat(data, conversation_id)

    elif message_type == "heartbeat":
        # Respond to heartbeat
        await manager.send_to_conversation(conversation_id, {
            "type": "heartbeat_response",
            "timestamp": datetime.now().isoformat()
        })


async def handle_conversation_control(data: dict, conversation_id: str):
    """Handle conversation control messages.

    Args:
        data: Control message data
        conversation_id: Target conversation ID
    """
    control_action = data.get("action")

    try:
        # Import here to avoid circular imports
        from .conversation_service import get_conversation_service
        service = get_conversation_service()

        if control_action == "stop":
            success = await service.stop_conversation(conversation_id)
            if success:
                await manager.send_to_conversation(conversation_id, {
                    "type": "conversation_control",
                    "action": "stop",
                    "conversation_id": conversation_id,
                    "timestamp": datetime.now().isoformat(),
                    "message": "Conversation stopped"
                })
            else:
                await manager.send_to_conversation(conversation_id, {
                    "type": "error",
                    "error": "Failed to stop conversation",
                    "timestamp": datetime.now().isoformat()
                })

        elif control_action in ["pause", "resume"]:
            # For now, just broadcast the action (pause/resume not fully implemented)
            await manager.send_to_conversation(conversation_id, {
                "type": "conversation_control",
                "action": control_action,
                "conversation_id": conversation_id,
                "timestamp": datetime.now().isoformat(),
                "message": f"Conversation {control_action}d"
            })
            logger.info(f"Conversation {conversation_id} {control_action}d")

        else:
            logger.warning(f"Unknown control action: {control_action}")
            await manager.send_to_conversation(conversation_id, {
                "type": "error",
                "error": f"Unknown control action: {control_action}",
                "timestamp": datetime.now().isoformat()
            })

    except Exception as e:
        logger.error(f"Error handling conversation control: {e}")
        await manager.send_to_conversation(conversation_id, {
            "type": "error",
            "error": f"Control error: {str(e)}",
            "timestamp": datetime.now().isoformat()
        })


async def handle_start_conversation(data: dict, conversation_id: str):
    """Handle starting a new conversation via WebSocket.

    Args:
        data: Start conversation message data
        conversation_id: Target conversation ID
    """
    try:
        # Import here to avoid circular imports
        from .conversation_service import get_conversation_service
        service = get_conversation_service()

        # Extract required fields
        surveyor_persona_id = data.get("surveyor_persona_id")
        patient_persona_id = data.get("patient_persona_id")
        host = data.get("host")
        model = data.get("model")
        patient_attributes = data.get("patient_attributes")
        analysis_attributes = data.get("analysis_attributes")
        surveyor_system_prompt = data.get("surveyor_system_prompt")
        patient_system_prompt = data.get("patient_system_prompt")
        surveyor_attributes = data.get("surveyor_attributes")
        surveyor_question_bank = data.get("surveyor_question_bank")
        top_down_codebook_template_id = data.get("top_down_codebook_template_id")

        if not surveyor_persona_id or not patient_persona_id:
            await manager.send_to_conversation(conversation_id, {
                "type": "error",
                "error": "Missing required persona IDs",
                "timestamp": datetime.now().isoformat()
            })
            return

        # Start the conversation
        success = await service.start_conversation(
            conversation_id=conversation_id,
            surveyor_persona_id=surveyor_persona_id,
            patient_persona_id=patient_persona_id,
            host=host,
            model=model,
            patient_attributes=patient_attributes,
            surveyor_system_prompt=surveyor_system_prompt,
            patient_system_prompt=patient_system_prompt,
            analysis_attributes=analysis_attributes,
            surveyor_attributes=surveyor_attributes,
            surveyor_question_bank=surveyor_question_bank,
            top_down_codebook_template_id=top_down_codebook_template_id,
        )

        if success:
            logger.info(f"Started conversation {conversation_id} via WebSocket")
        else:
            await manager.send_to_conversation(conversation_id, {
                "type": "error",
                "error": "Failed to start conversation",
                "timestamp": datetime.now().isoformat()
            })

    except Exception as e:
        logger.error(f"Error starting conversation via WebSocket: {e}")
        await manager.send_to_conversation(conversation_id, {
            "type": "error",
            "error": f"Start error: {str(e)}",
            "timestamp": datetime.now().isoformat()
        })


async def handle_start_human_chat(data: dict, conversation_id: str):
    """Handle starting a new human-chat session via WebSocket."""
    try:
        from .conversation_service import get_conversation_service

        service = get_conversation_service()

        surveyor_persona_id = data.get("surveyor_persona_id")
        patient_persona_id = data.get("patient_persona_id")
        host = data.get("host")
        model = data.get("model")
        ai_role = data.get("ai_role")
        patient_attributes = data.get("patient_attributes")
        analysis_attributes = data.get("analysis_attributes")
        surveyor_system_prompt = data.get("surveyor_system_prompt")
        patient_system_prompt = data.get("patient_system_prompt")
        surveyor_attributes = data.get("surveyor_attributes")
        surveyor_question_bank = data.get("surveyor_question_bank")
        top_down_codebook_template_id = data.get("top_down_codebook_template_id")

        if not surveyor_persona_id or not patient_persona_id:
            await manager.send_to_conversation(conversation_id, {
                "type": "error",
                "error": "Missing required persona IDs",
                "timestamp": datetime.now().isoformat(),
            })
            return

        success = await service.start_human_chat(
            conversation_id=conversation_id,
            surveyor_persona_id=surveyor_persona_id,
            patient_persona_id=patient_persona_id,
            host=host,
            model=model,
            patient_attributes=patient_attributes,
            surveyor_system_prompt=surveyor_system_prompt,
            patient_system_prompt=patient_system_prompt,
            analysis_attributes=analysis_attributes,
            surveyor_attributes=surveyor_attributes,
            surveyor_question_bank=surveyor_question_bank,
            ai_role=ai_role,
            top_down_codebook_template_id=top_down_codebook_template_id,
        )

        if success:
            logger.info(f"Started human chat {conversation_id} via WebSocket")
        else:
            await manager.send_to_conversation(conversation_id, {
                "type": "error",
                "error": "Failed to start human chat",
                "timestamp": datetime.now().isoformat(),
            })

    except Exception as e:
        logger.error(f"Error starting human chat via WebSocket: {e}")
        await manager.send_to_conversation(conversation_id, {
            "type": "error",
            "error": f"Human chat start error: {str(e)}",
            "timestamp": datetime.now().isoformat(),
        })


async def handle_human_chat_message(data: dict, conversation_id: str):
    """Handle a human-provided patient message via WebSocket."""
    try:
        from .conversation_service import get_conversation_service

        service = get_conversation_service()
        text = (data.get("content") or "").strip()
        if not text:
            return
        await service.human_chat_message(conversation_id, text)
    except Exception as e:
        logger.error(f"Error handling human chat message: {e}")
        await manager.send_to_conversation(conversation_id, {
            "type": "error",
            "error": f"Human chat message error: {str(e)}",
            "timestamp": datetime.now().isoformat(),
        })


async def handle_end_human_chat(data: dict, conversation_id: str):
    """Handle ending a human-chat session via WebSocket."""
    try:
        from .conversation_service import get_conversation_service

        service = get_conversation_service()
        await service.end_human_chat(conversation_id)
    except Exception as e:
        logger.error(f"Error ending human chat: {e}")
        await manager.send_to_conversation(conversation_id, {
            "type": "error",
            "error": f"Human chat end error: {str(e)}",
            "timestamp": datetime.now().isoformat(),
        })


# Export the manager for use in other modules
__all__ = ["websocket_endpoint", "manager"]