-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.py
More file actions
121 lines (84 loc) · 3.14 KB
/
debug.py
File metadata and controls
121 lines (84 loc) · 3.14 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
import subprocess
from pathlib import Path
import sys
import typer
app = typer.Typer()
# Ensure UTF-8 encoding for Windows terminals
sys.stdin.reconfigure(encoding="utf-8")
sys.stdout.reconfigure(encoding="utf-8")
def generate_debug_report(file_path: Path, model_name: str = "llama3.2:1b") -> str | None:
"""
Generate a structured debug report for the given code file using Ollama.
"""
try:
code_content = file_path.read_text(encoding="utf-8")
prompt = f"""You are a senior software engineer and debugging expert.
Carefully analyze the following code.
Your tasks:
1. Detect syntax errors.
2. Detect logical errors.
3. Detect runtime errors.
4. Identify bad practices or inefficiencies.
5. Clearly explain each issue.
6. Provide step-by-step solutions.
7. Provide a fully corrected version of the entire code at the end.
Format your response EXACTLY like this:
=== ERRORS FOUND ===
(List all issues clearly)
=== SOLUTIONS ===
(Explain how to fix them)
=== CORRECTED CODE ===
(Provide full corrected code)
Here is the code to debug:
{code_content}
"""
typer.echo(f"Debugging {file_path} using model '{model_name}'...")
process = subprocess.Popen(
["ollama", "run", model_name],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
stdout, stderr = process.communicate(input=prompt.encode("utf-8"))
stdout = stdout.decode("utf-8", errors="replace")
stderr = stderr.decode("utf-8", errors="replace")
if process.returncode != 0:
typer.secho(f"\n❌ Error running Ollama:\n{stderr}", fg=typer.colors.RED)
return None
return stdout
except Exception as e:
typer.secho(f"\n❌ An unexpected error occurred: {str(e)}", fg=typer.colors.RED)
return None
def save_debug_report(file_path: Path, report: str) -> Path:
"""
Save the debug report to a text file.
"""
output_file = file_path.with_stem(file_path.stem + "_debug").with_suffix(".txt")
output_file.write_text(report, encoding="utf-8")
typer.secho(f"\n Debug report saved to: {output_file}", fg=typer.colors.GREEN)
return output_file
@app.command()
def debug(
file: Path = typer.Argument(..., exists=True, readable=True, help="The code file to debug."),
model: str = typer.Option("llama3.2:1b", help="Ollama model to use.")
):
"""
Please review and debug the following code.
If it contains any syntax errors, logical mistakes, runtime issues, or potential bugs, identify and explain them clearly.
For each issue you find:
- Explain why it is a problem.
- Provide a clear solution.
- Suggest improvements if applicable.
After identifying and explaining all issues, provide a fully corrected version of the entire code.
Then briefly describe the code’s purpose, main functions, and overall structure.
Here is the code:
{code_content}
"""
report = generate_debug_report(file, model)
if report:
save_debug_report(file, report)
else:
typer.secho("⚠ Debugging failed.", fg=typer.colors.YELLOW)
if __name__ == "__main__":
app()