75a0a3fde7
- Added `get_latest_email_date()` function in `database.py` to retrieve the most recent email date for a given account and folder. - Enhanced `fetch_folder_emails()` in `zoho_client.py` to intelligently determine the start date for fetching emails based on the latest email date in the database. - Introduced `analyze_and_update_threads_async()` for asynchronous analysis of email threads, allowing concurrent processing. - Created a synchronous wrapper `analyze_and_update_threads()` for easier integration. - Updated `fetch_emails()` to support database session and account email parameters. - Added comprehensive documentation in `AI_ANALYSIS_GUIDE.md` detailing the new AI analysis functionality. - Implemented tests for the new features, including `test_fetch_with_db.py`, `test_ai_analysis.py`, and `test_single_analysis.py`. - Added error handling and logging improvements throughout the codebase.
95 lines
2.6 KiB
Python
95 lines
2.6 KiB
Python
#!/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()
|