File size: 2,164 Bytes
1b963f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Logging utility for agent actions - Policy Summarizer
"""
import logging
import time
from typing import Optional, List
from functools import wraps

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s | %(levelname)s | %(name)s | %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

logger = logging.getLogger('PolicySummarizer')

# Store logs for UI display
_agent_logs: List[dict] = []


def get_logs() -> List[dict]:
    """Get all logged agent actions"""
    return _agent_logs.copy()


def clear_logs():
    """Clear all logs"""
    global _agent_logs
    _agent_logs = []


def log_agent_action(
    agent_name: str,
    action: str,
    input_summary: str,
    output_summary: str,
    duration_seconds: float,
    success: bool = True,
    error: Optional[str] = None
):
    """Log an agent action without sensitive data."""
    log_entry = {
        "agent_name": agent_name,
        "action": action,
        "input_summary": input_summary[:200] + "..." if len(input_summary) > 200 else input_summary,
        "output_summary": output_summary[:200] + "..." if len(output_summary) > 200 else output_summary,
        "duration_seconds": round(duration_seconds, 2),
        "success": success,
        "error": error
    }
    
    _agent_logs.append(log_entry)
    
    status = "✓" if success else "✗"
    logger.info(f"{status} [{agent_name}] {action} ({duration_seconds:.2f}s)")
    
    if error:
        logger.error(f"  Error: {error}")


def format_logs_for_display() -> str:
    """Format logs for display in UI"""
    if not _agent_logs:
        return "No logs yet."
    
    lines = ["## Agent Activity Log\n"]
    
    for i, log in enumerate(_agent_logs, 1):
        status = "✅" if log["success"] else "❌"
        lines.append(f"### Step {i}: {log['agent_name']}")
        lines.append(f"- **Action:** {log['action']}")
        lines.append(f"- **Status:** {status}")
        lines.append(f"- **Duration:** {log['duration_seconds']}s")
        
        if log.get("error"):
            lines.append(f"- **Error:** {log['error']}")
        
        lines.append("")
    
    return "\n".join(lines)