Agent run logger Responsible for recording the complete interaction process of each agent run, including: - LLM requests and responses - Tool calls and results
| 9 | |
| 10 | |
| 11 | class AgentLogger: |
| 12 | """Agent run logger |
| 13 | |
| 14 | Responsible for recording the complete interaction process of each agent run, including: |
| 15 | - LLM requests and responses |
| 16 | - Tool calls and results |
| 17 | """ |
| 18 | |
| 19 | def __init__(self): |
| 20 | """Initialize logger |
| 21 | |
| 22 | Logs are stored in ~/.mini-agent/log/ directory |
| 23 | """ |
| 24 | # Use ~/.mini-agent/log/ directory for logs |
| 25 | self.log_dir = Path.home() / ".mini-agent" / "log" |
| 26 | self.log_dir.mkdir(parents=True, exist_ok=True) |
| 27 | self.log_file = None |
| 28 | self.log_index = 0 |
| 29 | |
| 30 | def start_new_run(self): |
| 31 | """Start new run, create new log file""" |
| 32 | timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
| 33 | log_filename = f"agent_run_{timestamp}.log" |
| 34 | self.log_file = self.log_dir / log_filename |
| 35 | self.log_index = 0 |
| 36 | |
| 37 | # Write log header |
| 38 | with open(self.log_file, "w", encoding="utf-8") as f: |
| 39 | f.write("=" * 80 + "\n") |
| 40 | f.write(f"Agent Run Log - {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n") |
| 41 | f.write("=" * 80 + "\n\n") |
| 42 | |
| 43 | def log_request(self, messages: list[Message], tools: list[Any] | None = None): |
| 44 | """Log LLM request |
| 45 | |
| 46 | Args: |
| 47 | messages: Message list |
| 48 | tools: Tool list (optional) |
| 49 | """ |
| 50 | self.log_index += 1 |
| 51 | |
| 52 | # Build complete request data structure |
| 53 | request_data = { |
| 54 | "messages": [], |
| 55 | "tools": [], |
| 56 | } |
| 57 | |
| 58 | # Convert messages to JSON serializable format |
| 59 | for msg in messages: |
| 60 | msg_dict = { |
| 61 | "role": msg.role, |
| 62 | "content": msg.content, |
| 63 | } |
| 64 | if msg.thinking: |
| 65 | msg_dict["thinking"] = msg.thinking |
| 66 | if msg.tool_calls: |
| 67 | msg_dict["tool_calls"] = [tc.model_dump() for tc in msg.tool_calls] |
| 68 | if msg.tool_call_id: |