|
1 | 1 | from typing import Optional |
2 | 2 | import datetime |
3 | 3 | import typer |
| 4 | +from pathlib import Path |
| 5 | +from functools import wraps |
4 | 6 | from rich.console import Console |
5 | 7 | from rich.panel import Panel |
6 | 8 | from rich.spinner import Spinner |
@@ -747,6 +749,53 @@ def run_analysis(): |
747 | 749 | [analyst.value for analyst in selections["analysts"]], config=config, debug=True |
748 | 750 | ) |
749 | 751 |
|
| 752 | + # Create result directory |
| 753 | + results_dir = Path(config["results_dir"]) / selections["ticker"] / selections["analysis_date"] |
| 754 | + results_dir.mkdir(parents=True, exist_ok=True) |
| 755 | + report_dir = results_dir / "reports" |
| 756 | + report_dir.mkdir(parents=True, exist_ok=True) |
| 757 | + log_file = results_dir / "message_tool.log" |
| 758 | + log_file.touch(exist_ok=True) |
| 759 | + |
| 760 | + def save_message_decorator(obj, func_name): |
| 761 | + func = getattr(obj, func_name) |
| 762 | + @wraps(func) |
| 763 | + def wrapper(*args, **kwargs): |
| 764 | + func(*args, **kwargs) |
| 765 | + timestamp, message_type, content = obj.messages[-1] |
| 766 | + content = content.replace("\n", " ") # Replace newlines with spaces |
| 767 | + with open(log_file, "a") as f: |
| 768 | + f.write(f"{timestamp} [{message_type}] {content}\n") |
| 769 | + return wrapper |
| 770 | + |
| 771 | + def save_tool_call_decorator(obj, func_name): |
| 772 | + func = getattr(obj, func_name) |
| 773 | + @wraps(func) |
| 774 | + def wrapper(*args, **kwargs): |
| 775 | + func(*args, **kwargs) |
| 776 | + timestamp, tool_name, args = obj.tool_calls[-1] |
| 777 | + args_str = ", ".join(f"{k}={v}" for k, v in args.items()) |
| 778 | + with open(log_file, "a") as f: |
| 779 | + f.write(f"{timestamp} [Tool Call] {tool_name}({args_str})\n") |
| 780 | + return wrapper |
| 781 | + |
| 782 | + def save_report_section_decorator(obj, func_name): |
| 783 | + func = getattr(obj, func_name) |
| 784 | + @wraps(func) |
| 785 | + def wrapper(section_name, content): |
| 786 | + func(section_name, content) |
| 787 | + if section_name in obj.report_sections and obj.report_sections[section_name] is not None: |
| 788 | + content = obj.report_sections[section_name] |
| 789 | + if content: |
| 790 | + file_name = f"{section_name}.md" |
| 791 | + with open(report_dir / file_name, "w") as f: |
| 792 | + f.write(content) |
| 793 | + return wrapper |
| 794 | + |
| 795 | + message_buffer.add_message = save_message_decorator(message_buffer, "add_message") |
| 796 | + message_buffer.add_tool_call = save_tool_call_decorator(message_buffer, "add_tool_call") |
| 797 | + message_buffer.update_report_section = save_report_section_decorator(message_buffer, "update_report_section") |
| 798 | + |
750 | 799 | # Now start the display layout |
751 | 800 | layout = create_layout() |
752 | 801 |
|
|
0 commit comments