Component: CodeToolsMixin
Module:
gaia_agent_code.tools.code_tools
Import: from gaia_agent_code.tools.code_tools import CodeToolsMixinOverview
CodeToolsMixin provides comprehensive code generation, analysis, and helper methods for Python development. It combines AI-powered code generation with Python AST analysis to create well-structured, validated code with proper documentation and type hints. Key Features:- Generate Python functions, classes, and tests
- Parse and analyze Python code structure
- Validate Python syntax
- Extract code symbols (functions, classes, imports)
- Run pylint analysis on code
- LLM-powered code generation with streaming
- Automatic error recovery and retry logic
Requirements
Functional Requirements
-
Code Generation
- Generate Python functions with docstrings
- Generate Python classes with methods
- Generate comprehensive unit tests
- Support type hints and return types
- Validate generated code syntax
- Optional file output
-
Code Analysis
- Parse Python code using AST
- Extract functions, classes, and imports
- Validate Python syntax
- List symbols with line numbers
- Generate code structure reports
-
Code Quality
- Run pylint analysis with confidence filtering
- Parse and categorize pylint issues
- Generate actionable error reports
- Support file and directory analysis
-
LLM Integration
- Stream code generation with live preview
- Automatic retry on timeout
- Progressive token reduction on failure
- Fallback to placeholder code
- Markdown code block extraction
Non-Functional Requirements
-
Code Quality
- All generated code must be syntactically valid
- Proper indentation (4 spaces)
- Comprehensive docstrings
- Type hints where appropriate
-
Performance
- Streaming generation for large files
- Maximum 3 retry attempts
- Progressive timeout reduction
- Efficient AST parsing
-
Reliability
- Syntax validation before file write
- Graceful degradation on LLM timeout
- Clear error messages
API Specification
File Location
Public Interface
Tool Specifications
1. generate_function
Generate a Python function with docstring and body. Parameters:name(str, required): Function nameparams(str, optional): Parameter list (e.g., “x, y=0”)docstring(str, optional): Function documentationbody(str, optional): Function implementationreturn_type(str, optional): Return type hintwrite_to_file(bool, optional): Save to file (default: True)
2. generate_class
Generate a Python class with methods. Parameters:name(str, required): Class namedocstring(str, optional): Class documentationbase_classes(str, optional): Base classes (e.g., “BaseClass, Mixin”)methods(List[Dict], optional): Methods to includewrite_to_file(bool, optional): Save to file (default: True)
3. generate_test
Generate comprehensive Python unit tests. Parameters:class_name(str, optional): Test class namemodule_name(str, optional): Module being testedfunction_name(str, optional): Specific function to testsource_code(str, optional): Source code to analyzetest_cases(List[str], optional): Manual test case namessource_file(str, optional): Path to source filewrite_to_file(bool, optional): Save to file (default: True)
4. parse_python_code
Parse Python code and extract structure. Parameters:code(str, required): Python source code
5. validate_syntax
Validate Python code syntax. Parameters:code(str, required): Python code to validate
6. list_symbols
List symbols (functions, classes, variables) in Python code. Parameters:code(str, required): Python source codesymbol_type(str, optional): Filter by type (‘function’, ‘class’, ‘variable’, ‘import’)
7. analyze_with_pylint
Analyze Python code with pylint. Parameters:file_path(str, optional): Path to Python filecode(str, optional): Python code stringconfidence(str, optional): Minimum confidence level (default: “HIGH”)
Usage Examples
Example 1: Generate a Function
Example 2: Generate a Class with Methods
Example 3: Generate Unit Tests
Example 4: Analyze Code with Pylint
Example 5: Parse Code Structure
Code Generation Constants
Testing Requirements
Unit Tests
File:tests/agents/code/test_code_tools.py
Dependencies
Required Packages
Import Dependencies
LLM Integration
Code Generation Prompt Template
Streaming with Retry Logic
CodeToolsMixin Technical Specification