Skip to content

ahmedk20/CodeMedic

Repository files navigation

CodeMedic

A lightweight agentic coding assistant powered by Google's Gemini API. This project is a toy implementation of tools like Cursor/Zed's Agentic Mode or Claude Code, demonstrating how AI agents can autonomously debug and fix code through function calling.

Features

  • Autonomous Debugging: AI agent analyzes bugs, reads relevant files, and applies fixes iteratively
  • Sandboxed Execution: All operations are restricted to a specified working directory for safety
  • Function Calling: Implements tool use for:
    • Listing files and directories
    • Reading file contents (with truncation protection)
    • Executing Python files with arguments
    • Writing/overwriting files
  • Iterative Agent Loop: Up to 20 iterations to solve problems autonomously
  • Verbose Mode: Optional detailed logging of function calls and token usage

How It Works

The agent follows a strict workflow:

  1. Understand the bug from the user's description
  2. Reproduce the bug by running the program
  3. Inspect relevant files to locate the root cause
  4. Make minimal changes to fix the bug
  5. Re-run the program to verify the fix
  6. Continue until the output matches expected behavior

Prerequisites

  • Python 3.12 or higher
  • Google Gemini API key

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/coding-agent.git
cd coding-agent
  1. Install dependencies using uv:
uv sync

Or using pip:

pip install -r requirements.txt
  1. Create a .env file in the project root:
GEMINI_API_KEY=your_gemini_api_key_here

Usage

Basic usage:

python main.py "Fix the bug in the calculator"

With verbose output:

python main.py "Fix the bug in the calculator" --verbose

Example Session

python main.py "The calculator crashes when dividing by zero. Fix it."

The agent will:

  1. List files to understand the codebase structure
  2. Read the calculator implementation
  3. Run the program to reproduce the bug
  4. Identify the division by zero issue
  5. Add error handling
  6. Re-run to verify the fix
  7. Report success

Project Structure

coding-agent/
├── main.py                 # Entry point and agent loop
├── config.py              # Configuration (file size limits, etc.)
├── prompts.py             # System prompt for the agent
├── functions/             # Function implementations
│   ├── call_function.py   # Function dispatcher
│   ├── get_files_info.py  # List directory contents
│   ├── get_file_content.py # Read file contents
│   ├── run_python_file.py  # Execute Python scripts
│   └── write_file.py      # Write/overwrite files
├── calculator/            # Example project for debugging
│   ├── main.py
│   └── tests.py
└── .env                   # API keys (not committed)

Configuration

Working Directory

The agent operates within a sandboxed directory specified in functions/call_function.py:

args["working_directory"] = "./calculator"

Change this to point to your target codebase.

File Size Limits

Maximum file read size is configurable in config.py:

MAX_FILE_CHARS = 10_000  # Truncates files larger than this

Iteration Limit

Maximum agent iterations can be adjusted in main.py:

for iteration in range(20):  # Change to desired max iterations

Available Functions

The agent has access to these tools:

get_files_info(directory=".")

Lists files in the specified directory with size and type information.

get_file_content(file_path)

Reads and returns file contents (with truncation for large files).

run_python_file(file_path, args=[])

Executes a Python file with optional command-line arguments, returns stdout/stderr.

write_file(file_path, content)

Writes content to a file, creating parent directories if needed.

Security

  • Path Traversal Protection: All file operations validate that paths remain within the working directory
  • Execution Timeout: Python file execution times out after 30 seconds
  • File Size Limits: Files are truncated when reading to prevent memory issues
  • No Shell Injection: Uses subprocess.run() with argument lists instead of shell strings

Limitations

  • Python-only execution (can be extended to other languages)
  • Limited to text files (no binary file support)
  • No git operations or version control integration
  • Maximum 20 iterations per request
  • Single-threaded operation

Extending the Agent

To add new capabilities:

  1. Create a new function in functions/ with:
    • Implementation function
    • Schema definition using types.FunctionDeclaration
  2. Add to call_function.py:
    • Import the function and schema
    • Add schema to available_functions
    • Add function to function_map

Example:

# functions/search_code.py
def search_code(working_directory, query):
    # Implementation
    pass

schema_search_code = types.FunctionDeclaration(
    name="search_code",
    description="Search for code patterns",
    parameters=...
)

Examples

See the calculator/ directory for an example project the agent can debug:

# Example bug fixes the agent can handle:
python main.py "The calculator doesn't handle division by zero"
python main.py "Tests are failing, fix them"
python main.py "Add input validation to prevent negative numbers"

Contributing

Contributions welcome! Areas for improvement:

  • Add support for more programming languages
  • Implement code search/grep functionality
  • Add git integration for committing fixes
  • Support for reading/writing binary files
  • Multi-file refactoring capabilities
  • Integration with other LLM providers

License

MIT License - see LICENSE file for details

Acknowledgments

Inspired by:

  • Cursor - AI-powered code editor
  • Zed - Collaborative code editor with AI features
  • Claude Code - Anthropic's coding assistant

Built with:

Troubleshooting

"GEMINI_API_KEY not found"

  • Ensure .env file exists with valid API key

"Maximum iterations reached"

  • Problem may be too complex
  • Try breaking into smaller requests
  • Increase iteration limit in main.py

"Cannot read/write file outside working directory"

  • All paths must be relative to working directory
  • Update working_directory in call_function.py

Function call errors

  • Enable --verbose flag to see detailed function calls
  • Check that file paths are relative, not absolute

About

a toy version of something like Cursor/Zed's Agentic Mode, or Claude Code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages