Skip to main content
Component: CodeToolsMixin Module: gaia_agent_code.tools.code_tools Import: from gaia_agent_code.tools.code_tools import CodeToolsMixin

Overview

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

  1. 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
  2. Code Analysis
    • Parse Python code using AST
    • Extract functions, classes, and imports
    • Validate Python syntax
    • List symbols with line numbers
    • Generate code structure reports
  3. Code Quality
    • Run pylint analysis with confidence filtering
    • Parse and categorize pylint issues
    • Generate actionable error reports
    • Support file and directory analysis
  4. 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

  1. Code Quality
    • All generated code must be syntactically valid
    • Proper indentation (4 spaces)
    • Comprehensive docstrings
    • Type hints where appropriate
  2. Performance
    • Streaming generation for large files
    • Maximum 3 retry attempts
    • Progressive timeout reduction
    • Efficient AST parsing
  3. 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 name
  • params (str, optional): Parameter list (e.g., “x, y=0”)
  • docstring (str, optional): Function documentation
  • body (str, optional): Function implementation
  • return_type (str, optional): Return type hint
  • write_to_file (bool, optional): Save to file (default: True)
Returns:
Example:

2. generate_class

Generate a Python class with methods. Parameters:
  • name (str, required): Class name
  • docstring (str, optional): Class documentation
  • base_classes (str, optional): Base classes (e.g., “BaseClass, Mixin”)
  • methods (List[Dict], optional): Methods to include
  • write_to_file (bool, optional): Save to file (default: True)
Method Dict Format:
Returns:

3. generate_test

Generate comprehensive Python unit tests. Parameters:
  • class_name (str, optional): Test class name
  • module_name (str, optional): Module being tested
  • function_name (str, optional): Specific function to test
  • source_code (str, optional): Source code to analyze
  • test_cases (List[str], optional): Manual test case names
  • source_file (str, optional): Path to source file
  • write_to_file (bool, optional): Save to file (default: True)
Returns:
Generated Test Structure:

4. parse_python_code

Parse Python code and extract structure. Parameters:
  • code (str, required): Python source code
Returns:

5. validate_syntax

Validate Python code syntax. Parameters:
  • code (str, required): Python code to validate
Returns:

6. list_symbols

List symbols (functions, classes, variables) in Python code. Parameters:
  • code (str, required): Python source code
  • symbol_type (str, optional): Filter by type (‘function’, ‘class’, ‘variable’, ‘import’)
Returns:

7. analyze_with_pylint

Analyze Python code with pylint. Parameters:
  • file_path (str, optional): Path to Python file
  • code (str, optional): Python code string
  • confidence (str, optional): Minimum confidence level (default: “HIGH”)
Returns:

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