#!/usr/bin/env python3 """ Simple test to debug the AI analysis function. """ import os import sys # Add the src directory to the path sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) from ai import analyze_thread from database import SessionLocal, Thread, get_thread_messages def test_single_analysis(): """Test analyzing a single thread.""" db = SessionLocal() try: # Get a thread to test with thread = ( db.query(Thread) .filter(Thread.account_email == "test-user@company.com") .first() ) if not thread: print("No threads found for test-user@company.com") return print(f"Testing thread {thread.id}: {thread.subject}") print( f"Before analysis: actionable={thread.actionable}, confidence={thread.ai_confidence}" ) # Get messages messages = get_thread_messages(db, thread.id) print(f"Found {len(messages)} messages") # Convert to dict format message_dicts = [] for msg in messages: message_dicts.append( { "date_sent": msg.date_sent, "is_incoming": msg.is_incoming, "subject": msg.subject, "from_email": msg.from_email, "to_email": msg.to_email, "body": msg.body, } ) # Run analysis print("Running AI analysis...") analysis_result = analyze_thread(thread.subject, message_dicts) print(f"Analysis result: {analysis_result}") # Update thread thread.actionable = analysis_result.get("actionable", False) thread.ai_summary = analysis_result.get("summary", "") thread.ai_confidence = float(analysis_result.get("confidence", 0.0)) from datetime import datetime thread.last_analyzed_at = datetime.now() print( f"Updated thread: actionable={thread.actionable}, confidence={thread.ai_confidence}" ) print(f"Summary: {thread.ai_summary}") # Commit changes db.commit() print("Changes committed") # Verify the changes db.refresh(thread) print( f"After refresh: actionable={thread.actionable}, confidence={thread.ai_confidence}" ) except Exception as e: print(f"Error: {e}") import traceback traceback.print_exc() db.rollback() finally: db.close() if __name__ == "__main__": test_single_analysis()