|
1 | 1 | import logging |
| 2 | +import os |
2 | 3 | import uuid |
3 | 4 | import webbrowser |
| 5 | +from pathlib import Path |
4 | 6 |
|
5 | 7 | from rich.console import Console |
6 | 8 | from rich.panel import Panel |
|
15 | 17 | logger = logging.getLogger(__name__) |
16 | 18 |
|
17 | 19 |
|
| 20 | +def _update_or_create_env_file(): |
| 21 | + """Update or create .env file with CREWAI_TRACING_ENABLED=true.""" |
| 22 | + env_path = Path(".env") |
| 23 | + env_content = "" |
| 24 | + variable_name = "CREWAI_TRACING_ENABLED" |
| 25 | + variable_value = "true" |
| 26 | + |
| 27 | + # Read existing content if file exists |
| 28 | + if env_path.exists(): |
| 29 | + with open(env_path, "r") as f: |
| 30 | + env_content = f.read() |
| 31 | + |
| 32 | + # Check if CREWAI_TRACING_ENABLED is already set |
| 33 | + lines = env_content.splitlines() |
| 34 | + variable_exists = False |
| 35 | + updated_lines = [] |
| 36 | + |
| 37 | + for line in lines: |
| 38 | + if line.strip().startswith(f"{variable_name}="): |
| 39 | + # Update existing variable |
| 40 | + updated_lines.append(f"{variable_name}={variable_value}") |
| 41 | + variable_exists = True |
| 42 | + else: |
| 43 | + updated_lines.append(line) |
| 44 | + |
| 45 | + # Add variable if it doesn't exist |
| 46 | + if not variable_exists: |
| 47 | + if updated_lines and not updated_lines[-1].strip(): |
| 48 | + # If last line is empty, replace it |
| 49 | + updated_lines[-1] = f"{variable_name}={variable_value}" |
| 50 | + else: |
| 51 | + # Add new line and then the variable |
| 52 | + updated_lines.append(f"{variable_name}={variable_value}") |
| 53 | + |
| 54 | + # Write updated content |
| 55 | + with open(env_path, "w") as f: |
| 56 | + f.write("\n".join(updated_lines)) |
| 57 | + if updated_lines: # Add final newline if there's content |
| 58 | + f.write("\n") |
| 59 | + |
| 60 | + |
18 | 61 | class FirstTimeTraceHandler: |
19 | 62 | """Handles the first-time user trace collection and display flow.""" |
20 | 63 |
|
@@ -49,6 +92,12 @@ def handle_execution_completion(self): |
49 | 92 | if user_wants_traces: |
50 | 93 | self._initialize_backend_and_send_events() |
51 | 94 |
|
| 95 | + # Enable tracing for future runs by updating .env file |
| 96 | + try: |
| 97 | + _update_or_create_env_file() |
| 98 | + except Exception as e: |
| 99 | + pass |
| 100 | + |
52 | 101 | if self.ephemeral_url: |
53 | 102 | self._display_ephemeral_trace_link() |
54 | 103 |
|
@@ -129,7 +178,8 @@ def _display_ephemeral_trace_link(self): |
129 | 178 | • Tool usage and results |
130 | 179 | • LLM calls and responses |
131 | 180 |
|
132 | | -To use traces add tracing=True to your Crew(tracing=True) / Flow(tracing=True) |
| 181 | +✅ Tracing has been enabled for future runs! (CREWAI_TRACING_ENABLED=true added to .env) |
| 182 | +You can also add tracing=True to your Crew(tracing=True) / Flow(tracing=True) for more control. |
133 | 183 |
|
134 | 184 | 📝 Note: This link will expire in 24 hours. |
135 | 185 | """.strip() |
@@ -164,8 +214,8 @@ def _show_local_trace_message(self): |
164 | 214 | • Execution duration: {self.batch_manager.calculate_duration("execution")}ms |
165 | 215 | • Batch ID: {self.batch_manager.trace_batch_id} |
166 | 216 |
|
| 217 | +Tracing has been enabled for future runs! (CREWAI_TRACING_ENABLED=true added to .env) |
167 | 218 | The traces include agent decisions, task execution, and tool usage. |
168 | | -Try running with CREWAI_TRACING_ENABLED=true next time for persistent traces. |
169 | 219 | """.strip() |
170 | 220 |
|
171 | 221 | panel = Panel( |
|
0 commit comments