ada-assistant / src /agents /code_analyzer.py
syum-af's picture
refactor
294a450
Raw
History Blame Contribute Delete
4.3 kB
"""
Code Analyzer Agent for Ada Conversion Assistant
This agent is responsible for:
1. Parsing Ada code files
2. Extracting project structure and dependencies
3. Identifying business logic patterns
4. Generating analysis reports for conversion
"""
from typing import Any
from smolagents import ToolCallingAgent, FinalAnswerTool
from .utils.agent_utils import extract_response_text
class CodeAnalyzerAgent:
"""Agent specialized in analyzing Ada codebases"""
def __init__(self, model: Any):
# Model passed, create ToolCallingAgent internally with Ada-specific configuration
# I would have used CodeAgent but kept throwing error no matter what model I used
self.model = model
self.code_agent = ToolCallingAgent(
model=self.model,
tools=[FinalAnswerTool()],
)
self.supported_extensions = ['.ads', '.adb', '.ada']
self.analysis_results = {}
def extract_business_logic(self, ada_code: str) -> str:
"""
Extract business logic patterns from Ada code using LLM
Args:
ada_code: Ada code content as string
Returns:
Business logic analysis with conversion recommendations
"""
try:
# Create business logic analysis prompt
analysis_prompt = self._create_business_logic_prompt(ada_code)
# Use the code agent to analyze business logic
result = self.code_agent.run(analysis_prompt)
# Extract response text from RunResult (now markdown)
response_text = extract_response_text(result)
return response_text
except Exception as e:
print(f"LLM business logic analysis failed: {e}")
return self._create_fallback_business_logic_with_error(f"LLM analysis failed: {str(e)}")
def _create_business_logic_prompt(self, ada_code: str) -> str:
"""Create structured business logic analysis prompt for LLM"""
return f"""
Analyze the business logic of this Ada code and return a well-formatted markdown report.
Do not write code or execute anything - just analyze and respond with markdown.
Ada Code Content:
{ada_code}
Structure your analysis as follows in markdown format:
# Business Logic Analysis
## Core Algorithms
- List the main algorithms and computational logic found in the code
- Describe the key processing steps and workflows
## Data Structures & Types
- Identify important data structures, types, and their relationships
- Note any custom types or complex data arrangements
## Business Rules & Constraints
- List business rules, validation logic, and constraints
- Pay special attention to SPARK contracts and formal specifications
- Note any domain-specific rules or policies
## Domain Concepts
- Identify domain-specific concepts and terminology
- Explain the business context and purpose of the code
## Conversion Complexity
**Level**: Low/Medium/High
**Reasoning**: Explain why this complexity level was assigned
## Recommended Approach
Describe the recommended strategy for converting this code to modern languages like Python/TypeScript, including:
- Key architectural considerations
- Potential challenges and solutions
- Suggested modernization patterns
Focus on providing clear, actionable insights for code modernization and conversion.
Return ONLY the markdown report, nothing else.
"""
def _create_fallback_business_logic_with_error(self, error_msg: str) -> str:
"""Create fallback business logic response when analysis fails"""
return f"""# Business Logic Analysis
## Error
Analysis failed with error: {error_msg}
## Recommended Approach
Manual analysis recommended due to analysis failure. Please review the Ada code manually or try again.
## Next Steps
- Check if the file is valid Ada code
- Verify network connectivity for LLM access
- Consider using a different analysis approach
"""