|
|
""" |
|
|
Event handlers for connecting UI to backend |
|
|
""" |
|
|
|
|
|
import logging |
|
|
import uuid |
|
|
from typing import Dict, Any |
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
class EventHandlers: |
|
|
def __init__(self, components: Dict[str, Any]): |
|
|
self.components = components |
|
|
self.sessions = {} |
|
|
|
|
|
async def handle_message_submit(self, message: str, chat_history: list, |
|
|
session_id: str, show_reasoning: bool, |
|
|
show_agent_trace: bool, request): |
|
|
"""Handle user message submission""" |
|
|
try: |
|
|
|
|
|
if session_id not in self.sessions: |
|
|
self.sessions[session_id] = { |
|
|
'history': [], |
|
|
'context': {}, |
|
|
'created_at': uuid.uuid4().hex |
|
|
} |
|
|
|
|
|
|
|
|
chat_history.append((message, None)) |
|
|
|
|
|
|
|
|
if self.components.get('mock_mode'): |
|
|
response = self._generate_mock_response(message) |
|
|
else: |
|
|
response = await self._generate_ai_response(message, session_id) |
|
|
|
|
|
|
|
|
chat_history[-1] = (message, response) |
|
|
|
|
|
|
|
|
reasoning_data = {} |
|
|
performance_data = {} |
|
|
|
|
|
if show_reasoning: |
|
|
reasoning_data = { |
|
|
"chain_of_thought": { |
|
|
"step_1": { |
|
|
"hypothesis": "Mock reasoning for demonstration", |
|
|
"evidence": ["Mock mode active", f"User input: {message[:50]}..."], |
|
|
"confidence": 0.5, |
|
|
"reasoning": "Demonstration mode - enhanced reasoning chain not available" |
|
|
} |
|
|
}, |
|
|
"alternative_paths": [], |
|
|
"uncertainty_areas": [ |
|
|
{ |
|
|
"aspect": "System mode", |
|
|
"confidence": 0.5, |
|
|
"mitigation": "Mock mode - full reasoning chain not available" |
|
|
} |
|
|
], |
|
|
"evidence_sources": [], |
|
|
"confidence_calibration": {"overall_confidence": 0.5, "mock_mode": True} |
|
|
} |
|
|
|
|
|
if show_agent_trace: |
|
|
performance_data = {"agents_used": ["intent", "synthesis", "safety"]} |
|
|
|
|
|
return "", chat_history, reasoning_data, performance_data |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Error handling message: {e}") |
|
|
error_response = "I apologize, but I'm experiencing technical difficulties. Please try again." |
|
|
chat_history.append((message, error_response)) |
|
|
return "", chat_history, {"error": str(e)}, {"status": "error"} |
|
|
|
|
|
def _generate_mock_response(self, message: str) -> str: |
|
|
"""Generate mock response for demonstration""" |
|
|
mock_responses = [ |
|
|
f"I understand you're asking about: {message}. This is a mock response while the AI system initializes.", |
|
|
f"Thank you for your question: '{message}'. The research assistant is currently in demonstration mode.", |
|
|
f"Interesting question about {message}. In a full implementation, I would analyze this using multiple AI agents.", |
|
|
f"I've received your query: '{message}'. The system is working properly in mock mode." |
|
|
] |
|
|
|
|
|
import random |
|
|
return random.choice(mock_responses) |
|
|
|
|
|
async def _generate_ai_response(self, message: str, session_id: str) -> str: |
|
|
"""Generate AI response using orchestrator""" |
|
|
try: |
|
|
if 'orchestrator' in self.components: |
|
|
result = await self.components['orchestrator'].process_request( |
|
|
session_id=session_id, |
|
|
user_input=message |
|
|
) |
|
|
return result.get('final_response', 'No response generated') |
|
|
else: |
|
|
return "Orchestrator not available. Using mock response." |
|
|
except Exception as e: |
|
|
logger.error(f"AI response generation failed: {e}") |
|
|
return f"AI processing error: {str(e)}" |
|
|
|
|
|
def handle_new_session(self): |
|
|
"""Handle new session creation""" |
|
|
new_session_id = uuid.uuid4().hex[:8] |
|
|
self.sessions[new_session_id] = { |
|
|
'history': [], |
|
|
'context': {}, |
|
|
'created_at': uuid.uuid4().hex |
|
|
} |
|
|
return new_session_id, [] |
|
|
|
|
|
def handle_settings_toggle(self, current_visibility: bool): |
|
|
"""Toggle settings panel visibility""" |
|
|
return not current_visibility |
|
|
|
|
|
def handle_tab_change(self, tab_name: str): |
|
|
"""Handle tab changes in mobile interface""" |
|
|
return tab_name, False |
|
|
|
|
|
|
|
|
def create_event_handlers(components: Dict[str, Any]): |
|
|
return EventHandlers(components) |
|
|
|