File size: 4,296 Bytes
60e356c
 
 
 
 
 
 
 
 
 
294a450
60e356c
e3783fb
60e356c
 
 
 
 
 
 
 
 
 
0d91ffe
60e356c
 
 
 
 
 
294a450
60e356c
294a450
60e356c
 
294a450
60e356c
 
 
 
 
294a450
 
 
60e356c
 
 
7e1939b
e3783fb
60e356c
7e1939b
60e356c
 
 
 
 
294a450
60e356c
 
7e1939b
 
60e356c
 
294a450
60e356c
7e1939b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60e356c
 
 
7e1939b
60e356c
7e1939b
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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
        """