diff --git a/backend/main.py b/backend/main.py index 76f91fd..ac675df 100644 --- a/backend/main.py +++ b/backend/main.py @@ -192,7 +192,9 @@ async def websocket_endpoint(websocket: WebSocket): await manager.connect(websocket) # Start keepalive task if not already running - if not hasattr(app, 'keepalive_task'): + if not hasattr(app, 'keepalive_task') or app.keepalive_task.done(): + if hasattr(app, 'keepalive_task') and not app.keepalive_task.done(): + app.keepalive_task.cancel() app.keepalive_task = asyncio.create_task(keepalive_task()) while True: @@ -213,10 +215,18 @@ async def websocket_endpoint(websocket: WebSocket): await manager.send_message(client_id, json.dumps({"type": "pong"})) continue + # Parse JSON message to extract content + try: + parsed_message = json.loads(message) + message_content = parsed_message.get("content", message) + except json.JSONDecodeError: + # If not JSON, treat as plain text + message_content = message + # Process message with RAG agent try: response = await asyncio.wait_for( - asyncio.to_thread(rag_agent.chat, message), + asyncio.to_thread(rag_agent.chat, message_content), timeout=30 # 30 second timeout for RAG processing ) except asyncio.TimeoutError: diff --git a/mobile/App.js b/mobile/App.js index 52da755..a7ca325 100644 --- a/mobile/App.js +++ b/mobile/App.js @@ -180,11 +180,14 @@ export default function App() { } }; - const sendMessage = () => { - if (inputText.trim() && ws.current && ws.current.readyState === WebSocket.OPEN) { - setMessages(prev => [...prev, { type: 'user', content: inputText }]); - ws.current.send(JSON.stringify({ type: 'message', content: inputText })); - setInputText(''); + const sendMessage = (message = null) => { + const messageToSend = message || inputText.trim(); + if (messageToSend && ws.current && ws.current.readyState === WebSocket.OPEN) { + setMessages(prev => [...prev, { type: 'user', content: messageToSend }]); + ws.current.send(JSON.stringify({ type: 'message', content: messageToSend })); + if (!message) { + setInputText(''); + } setIsTyping(true); } else { Alert.alert('Connection Error', 'Cannot send message. Please check your connection.');