CodePhoenix

Refactoring Code Microservice ⚙️

Descrizione

Il microservizio di Refactoring utilizza un sistema multi-agente per analizzare e migliorare la qualità del codice. Due agenti principali collaborano per eseguire un’analisi approfondita e rifattorizzare il codice, garantendo una migliore struttura, leggibilità e manutenibilità.

Architettura del Microservizio

Il microservizio è progettato per eseguire i task in maniera sequenziale. I task sono assegnati rispettivamente a Code Analyzer e Refactoring Expert, garantendo che l’analisi del codice venga effettuata in due fasi distinte ma complementari.

Refactoring Microservice

Code Analyzer

Questo agente si occupa di analizzare il codice, applicando la sua vasta esperienza per identificare problemi, suggerire miglioramenti e garantire la qualità del codice.

  1. Input: Riceve il codice da analizzare.
  2. Analisi: Applica tecniche di analisi statica per identificare problemi nel codice, come bug, vulnerabilità di sicurezza e violazioni delle best practice.
  3. Output: Fornisce un report dettagliato con i problemi identificati e suggerimenti per miglioramenti.
code_analyzer = Agent(
    role='Code Analyzer',
    goal='Analyze and understand code structure and functionality',
    backstory="""You are an experienced developer with decades of experience in analyzing and refactoring systems.
    Your expertise lies in dissecting complex programs and understanding their core logic.""",
    verbose=True,
    allow_delegation=False,
    llm=os.environ["LLM"],
)

WorkFlow - Code Analyzer

WorkFlow - Code Analyzer

Refactoring Expert

Questo agente rifattorizza il codice, migliorando la sua struttura, leggibilità e manutenibilità, e applicando le best practice del settore.

  1. Input: Riceve il codice da analizzare insieme al report prodotto dal Code Analyzer.
  2. Refactoring: Migliora la qualità del codice, riduce la complessità e applica le best practice.
  3. Output: Fornisce il codice rifattorizzato con spiegazioni delle modifiche apportate.
refactoring_expert = Agent(
    role='Refactoring Expert',
    goal='Refactor the code to improve its structure and readability',
    backstory="""You are an expert in software refactoring techniques.
    You specialize in improving code quality, reducing complexity, and enhancing maintainability.""",
    verbose=True,
    allow_delegation=False,
    llm=os.environ["LLM"],
)

WorkFlow - Refactoring Expert

WorkFlow - Refactoring Expert

Parametri - Code Analyzer & Refactoring Expert

Task - Code Analyzer

task1 = Task(
    description=f"""Analyze the structure and functionality of the given code.
    Identify areas that need improvement in terms of design, efficiency, and readability.
    Code: {code}
    Analysis Result: {analysis_result}""",
    expected_output="Detailed report on code structure and areas for improvement",
    agent=code_analyzer,
)

Parametri - Task

Task - Refactoring Expert

task2 = Task(
    description=f"""Using the insights from the code analysis, refactor the code to improve its quality.
    Focus on enhancing readability, reducing complexity, and applying best practices.
    Code: {code}
    Analysis Result: {analysis_result}""",
    expected_output="Refactored code with explanations of improvements made",
    agent=refactoring_expert
)

Parametri - Task

Crew - System Agent Analysis

La crew risulta essere l’insieme degli agenti che vengono creati e a cui vengono assegnati i task.

# Instantiate the crew for code refactoring
refactoring_crew = Crew(
    agents=[code_analyzer, refactoring_expert],
    tasks=[task1, task2],
    verbose=True,
    process=Process.sequential
)

# Execute the refactoring
refactoring_result = refactoring_crew.kickoff()

Parametri - Crew