mirror of
http://hblu.top:3000/st2411020106/111.git
synced 2026-01-27 22:43:24 +08:00
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
from services.ai_service import ai_service
|
|
from models.schemas import BugFix
|
|
from typing import Optional
|
|
import re
|
|
|
|
class BugFixer:
|
|
"""Bug修复智能体"""
|
|
|
|
def __init__(self):
|
|
self.service = ai_service
|
|
|
|
def fix(self, code: str, language: str, error_description: Optional[str] = None) -> BugFix:
|
|
"""修复代码问题"""
|
|
result = self.service.generate_fix(code, language, error_description)
|
|
|
|
fixed_code = result.get("fixed_code", "")
|
|
problems = self._extract_problems(result.get("analysis", ""))
|
|
fixes = self._extract_fixes(result.get("analysis", ""))
|
|
|
|
return BugFix(
|
|
original_code=code,
|
|
fixed_code=fixed_code or code,
|
|
problems_found=problems,
|
|
fixes_applied=fixes,
|
|
explanation=result.get("analysis", "")
|
|
)
|
|
|
|
def _extract_problems(self, analysis: str) -> list:
|
|
"""提取发现的问题"""
|
|
problems = []
|
|
lines = analysis.split('\n')
|
|
|
|
in_problems_section = False
|
|
for line in lines:
|
|
if any(keyword in line.lower() for keyword in ['问题', 'problem', '错误', 'issue', 'bug']):
|
|
if not in_problems_section:
|
|
in_problems_section = True
|
|
continue
|
|
|
|
if in_problems_section and line.strip():
|
|
if any(keyword in line.lower() for keyword in ['修复', 'fix', '建议', '建议', 'solution']):
|
|
break
|
|
problems.append(line.strip())
|
|
|
|
return problems if problems else ["代码可能存在问题"]
|
|
|
|
def _extract_fixes(self, analysis: str) -> list:
|
|
"""提取修复内容"""
|
|
fixes = []
|
|
lines = analysis.split('\n')
|
|
|
|
in_fixes_section = False
|
|
for line in lines:
|
|
if any(keyword in line.lower() for keyword in ['修复', 'fix', 'solution', '修改']):
|
|
in_fixes_section = True
|
|
continue
|
|
|
|
if in_fixes_section and line.strip():
|
|
if line.strip().startswith(('1.', '2.', '3.', '•', '-', '*')):
|
|
fixes.append(line.strip())
|
|
|
|
return fixes if fixes else None
|
|
|
|
bug_fixer = BugFixer()
|