All Toolsโ€บMulti-Agent Code Refactor
๐Ÿ”ง AI-Powered Code Review ToolsMay 21, 2026โœ… Tests passing

Multi-Agent Code Refactor

A Python library that utilizes multiple AI agents to collaboratively review and refactor code. Each agent is specialized in a specific aspect, such as optimization, readability, or security. The agents work together to propose and implement comprehensive improvements to the input code.

What It Does

  • Supports multiple agents with different roles (e.g., style, performance, security).
  • Accepts Python code as a string or from a file.
  • Outputs refactored code and a detailed change report.
  • Optionally saves the refactored code to a file.

Installation

Install the required dependencies:

pip install pytest

Usage

Run the tool from the command line:

python multi_agent_code_refactor.py "path_or_code" --agents style performance security --output_file output.py
  • path_or_code: Path to the Python script or Python code as a string.
  • --agents: List of agent roles (default: style, performance, security).
  • --output_file: (Optional) Path to save the refactored code.

Source Code

import argparse
import os
from typing import List, Union

class CodeRefactorAgent:
    def __init__(self, role: str):
        self.role = role

    def review_code(self, code: str) -> str:
        """
        Mock review and refactor method for the given code based on the agent's role.
        """
        # Simulate a response for testing purposes
        return f"# Refactored by {self.role} agent\n{code}"

def refactor_code(
    input_code: Union[str, os.PathLike], agents: List[str], output_file: Union[str, os.PathLike, None] = None
) -> str:
    """
    Refactor Python code using multiple AI agents with different expertise.

    Args:
        input_code (str or os.PathLike): Python code as a string or a file path to a Python script.
        agents (List[str]): List of agent roles (e.g., ['style', 'performance', 'security']).
        output_file (str or os.PathLike, optional): File path to save the refactored code. Defaults to None.

    Returns:
        str: Refactored Python code.
    """
    # Load the input code
    if os.path.isfile(input_code):
        with open(input_code, "r") as f:
            code = f.read()
    else:
        code = input_code

    # Initialize agents
    agent_objects = [CodeRefactorAgent(role) for role in agents]

    # Each agent reviews and refactors the code
    reports = []
    for agent in agent_objects:
        response = agent.review_code(code)
        reports.append(f"Agent ({agent.role}):\n{response}\n")
        # Assuming the response contains the refactored code
        code = response

    # Save the refactored code if output_file is provided
    if output_file:
        with open(output_file, "w") as f:
            f.write(code)

    # Return the refactored code and the reports
    return code, "\n".join(reports)

def main():
    parser = argparse.ArgumentParser(description="Multi-Agent Code Refactor Tool")
    parser.add_argument("input_code", help="Path to the Python script or Python code as a string.")
    parser.add_argument(
        "--agents",
        nargs="+",
        default=["style", "performance", "security"],
        help="List of agent roles (e.g., style, performance, security).",
    )
    parser.add_argument(
        "--output_file", help="Path to save the refactored code.", default=None
    )

    args = parser.parse_args()

    try:
        refactored_code, report = refactor_code(args.input_code, args.agents, args.output_file)
        print("Refactored Code:")
        print(refactored_code)
        print("\nChange Report:")
        print(report)
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()

Community

Downloads

ยทยทยท

Rate this tool

No ratings yet โ€” be the first!

Details

Tool Name
multi_agent_code_refactor
Category
AI-Powered Code Review Tools
Generated
May 21, 2026
Tests
Passing โœ…
Fix Loops
2

Quick Install

Clone just this tool:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/ptulin/autoaiforge.git
cd autoaiforge
git sparse-checkout set generated_tools/2026-05-21/multi_agent_code_refactor
cd generated_tools/2026-05-21/multi_agent_code_refactor
pip install -r requirements.txt 2>/dev/null || true
python multi_agent_code_refactor.py