Skip to main content
Component: FileIOToolsMixin Module: gaia.agents.tools.file_io_tools Import: from gaia.agents.tools.file_io_tools import FileIOToolsMixin

Overview

FileIOToolsMixin provides comprehensive file I/O operations with intelligent file type detection, syntax validation, and security controls. It handles Python files with full AST analysis, markdown with structure extraction, and generic text files with appropriate encoding. Key Features:
  • Read files with automatic type detection and analysis
  • Write Python files with syntax validation
  • Edit files with diff generation
  • Search code across directories
  • Generate diffs for changes
  • Markdown file operations
  • Generic file operations without validation
  • Security through path validation

Tool Specifications

1. read_file

Read any file and intelligently analyze based on file type. Parameters:
  • file_path (str, required): Path to the file to read
Returns:

2. write_python_file

Write Python code to a file with syntax validation. Parameters:
  • file_path (str, required): Path where to write the file
  • content (str, required): Python code content
  • validate (bool, optional): Validate syntax before writing (default: True)
  • create_dirs (bool, optional): Create parent directories (default: True)
Returns:

3. edit_python_file

Edit a Python file by replacing content with validation. Parameters:
  • file_path (str, required): Path to the file
  • old_content (str, required): Content to find and replace
  • new_content (str, required): New content to insert
  • backup (bool, optional): Create backup (default: True)
  • dry_run (bool, optional): Only simulate (default: False)
Returns:

4. search_code

Search for patterns in code files. Parameters:
  • directory (str, optional): Directory to search (default: ”.”)
  • pattern (str, optional): Pattern to search for
  • file_extension (str, optional): File extension filter (default: “.py”)
  • max_results (int, optional): Maximum results (default: 100)
Returns:

5. generate_diff

Generate a unified diff for a file. Parameters:
  • file_path (str, required): Path to original file
  • new_content (str, required): New content to compare
  • context_lines (int, optional): Context lines in diff (default: 3)
Returns:

6. write_file

Write content to any file without syntax validation. Parameters:
  • file_path (str, required): Path where to write
  • content (str, required): Content to write
  • create_dirs (bool, optional): Create parent directories (default: True)
  • project_dir (str, optional): Project root for resolving relative paths
Returns:

7. edit_file

Edit any file by replacing content without validation. Parameters:
  • file_path (str, required): Path to file
  • old_content (str, required): Exact content to find and replace
  • new_content (str, required): New content to replace with
  • project_dir (str, optional): Project root for resolving paths
Returns:

8. replace_function

Replace a specific function in a Python file. Parameters:
  • file_path (str, required): Path to Python file
  • function_name (str, required): Name of function to replace
  • new_implementation (str, required): New function implementation
  • backup (bool, optional): Create backup (default: True)
Returns:

9. write_markdown_file

Write (or overwrite) a Markdown file, optionally creating parent directories. Used by the code agent when generating READMEs, spec documents, or reports. Parameters:
  • file_path (str, required): Destination path
  • content (str, required): Markdown content to write
  • create_dirs (bool, optional): Create parent directories if missing (default: True)
Returns:

10. update_gaia_md

Refresh the project’s GAIA.md (or another project-level index file) with generated metadata — used by the code agent to keep project docs in sync with what it’s building. Parameters:
  • project_root (str, optional): Project root directory (default: ".")
  • project_name (str, optional): Display name to write into the file
  • description (str, optional): Short project description
  • structure (str or dict, optional): Formatted directory structure
  • instructions (str, optional): Agent-facing instructions section
Returns:

Usage Examples

Example 1: Read and Analyze Python File

Example 2: Write Validated Python Code

Example 3: Edit File with Diff

Example 4: Search Codebase

Example 5: TypeScript/Generic File Operations


Security Model

Path Validation

All file operations check paths against allowed directories:

Allowed Paths Configuration


File Type Detection

Python Files (.py)

  • Full AST parsing
  • Syntax validation
  • Symbol extraction (functions, classes)
  • Error reporting with line numbers

Markdown Files (.md)

  • Header extraction (# patterns)
  • Code block extraction with language tags
  • Link extraction (text patterns)

Binary Files

  • Detected via UnicodeDecodeError
  • Returns size and binary flag
  • No content parsing

Text Files

  • Raw content reading
  • Line count and size reporting
  • No special analysis

Dependencies


Testing Requirements

File: tests/agents/code/test_file_io.py

FileIOToolsMixin Technical Specification