#!/usr/bin/env python3 """ Interactive debugging script for VoiceCal.ai chat functionality This script allows you to test and debug chat functionality step by step with detailed logging and breakpoints for SSH Dev Mode sessions. Usage: cd /app && python3 scripts/debug_chat.py """ import sys import os import uuid from datetime import datetime # Add app to path sys.path.insert(0, '/app') from app.core.logging_config import ( setup_enhanced_logging, debug_breakpoint, debug_print, log_chat_request, log_function_entry, log_function_exit ) def debug_session_manager(): """Debug session manager initialization.""" debug_print("=== DEBUGGING SESSION MANAGER ===") try: from app.core.session_factory import session_manager debug_print("Session manager imported successfully") debug_print(f"Session manager type: {type(session_manager).__name__}") # Test session creation session_id = str(uuid.uuid4()) debug_print(f"Testing session creation with ID: {session_id}") # You can add a breakpoint here for interactive debugging # debug_breakpoint("Session manager ready", locals()) return session_manager, session_id except Exception as e: debug_print(f"Session manager error: {e}") import traceback debug_print(f"Traceback: {traceback.format_exc()}") return None, None def debug_agent_creation(session_id): """Debug agent creation process.""" debug_print("=== DEBUGGING AGENT CREATION ===") try: from app.core.agent import ChatCalAgent debug_print("ChatCalAgent imported successfully") debug_print(f"Creating agent for session: {session_id}") agent = ChatCalAgent(session_id) debug_print("Agent created successfully") # Check agent properties debug_print(f"Agent system prompt preview: {agent.base_system_prompt[:100]}...") debug_print(f"Agent user info: {agent.user_info}") # Breakpoint for agent inspection # debug_breakpoint("Agent created", locals()) return agent except Exception as e: debug_print(f"Agent creation error: {e}") import traceback debug_print(f"Traceback: {traceback.format_exc()}") return None def debug_calendar_auth(): """Debug calendar authentication.""" debug_print("=== DEBUGGING CALENDAR AUTH ===") try: from app.calendar.auth import CalendarAuth debug_print("CalendarAuth imported successfully") auth = CalendarAuth() debug_print(f"CalendarAuth created, redirect URI: {auth.redirect_uri}") # Test credential loading from app.config import settings debug_print("Environment variables:") debug_print(f" GOOGLE_CLIENT_ID: {settings.google_client_id[:10]}...") debug_print(f" APP_ENV: {settings.app_env}") # Test calendar service service = auth.get_calendar_service() debug_print("Calendar service created successfully") return True except Exception as e: debug_print(f"Calendar auth error: {e}") import traceback debug_print(f"Traceback: {traceback.format_exc()}") return False def debug_chat_flow(message="Hello, I need to book an appointment with Peter"): """Debug the complete chat flow.""" debug_print("=== DEBUGGING COMPLETE CHAT FLOW ===") # Step 1: Session manager session_manager, session_id = debug_session_manager() if not session_manager: return False # Step 2: Agent creation agent = debug_agent_creation(session_id) if not agent: return False # Step 3: Calendar auth calendar_ok = debug_calendar_auth() if not calendar_ok: debug_print("āš ļø Calendar auth failed, but continuing with chat test") # Step 4: Test conversation debug_print("=== TESTING CONVERSATION ===") try: conversation = session_manager.get_or_create_conversation(session_id) debug_print("Conversation manager created") debug_print(f"Sending message: {message}") # Add breakpoint before getting response for interactive debugging # debug_breakpoint("About to get agent response", locals()) response = conversation.get_response(message) debug_print(f"Response received: {response[:200]}...") return True except Exception as e: debug_print(f"Chat flow error: {e}") import traceback debug_print(f"Traceback: {traceback.format_exc()}") return False def main(): """Main debugging session.""" print("šŸ› VoiceCal.ai Interactive Debugging Session") print("=" * 60) # Setup enhanced logging setup_enhanced_logging(log_level="DEBUG", log_file="/tmp/debug_chat.log") print("\nšŸ“‹ Available debugging functions:") print("1. debug_session_manager() - Test session management") print("2. debug_agent_creation(session_id) - Test agent creation") print("3. debug_calendar_auth() - Test calendar authentication") print("4. debug_chat_flow() - Test complete chat flow") print("5. debug_breakpoint(msg, locals()) - Interactive breakpoint") print("\nšŸ”§ Starting automated debug flow...") success = debug_chat_flow() if success: print("\nāœ… All debugging tests passed!") else: print("\nāŒ Some debugging tests failed - check logs for details") print(f"\nšŸ“ Debug logs saved to: /tmp/debug_chat.log") print("šŸ’” For interactive debugging, uncomment debug_breakpoint() calls in this script") # Interactive mode print("\nšŸŽ® Entering interactive mode...") print("Available variables: session_manager, agent, conversation") print("Type 'exit()' to quit") import code code.interact(local=locals()) if __name__ == "__main__": main()