An intelligent web application that uses AI to analyze and refactor code, making it more maintainable and efficient.
This project provides a robust backend service for AI-powered code analysis and refactoring.
Core Features:
- Asynchronous Code Refactoring: Submit code and get a refactored version with a detailed explanation.
- Code Analysis: Get a quality report on your code, including complexity and readability scores.
- Improvement Suggestions: Receive specific, actionable suggestions to improve your code.
- Code Explanation: Get a detailed, human-readable explanation of what a piece of code does.
- Mock Service Included: Run and test the full API without needing an AI provider API key.
- Database Migrations: Uses Alembic to manage the database schema.
- Async API: Built with FastAPI for high performance.
- Python 3.8+
- PostgreSQL
- Node.js 16+
- Git
-
Clone the repository:
git clone [repository-url] cd semantic-code-refactorer
-
Set up the backend:
# Create and activate virtual environment python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate # Install dependencies pip install -r requirements.txt
-
(Optional) Install development dependencies:
pip install -r requirements-dev.txt
The
requirements-dev.txt
file contains tools for testing, linting, formatting, type checking, and pre-commit hooks. Use it if you want to contribute or run tests/linting locally. -
Set up PostgreSQL:
# Create a new database createdb code_refactorer
-
Configure Environment Variables: Create a
.env
file in the project root. You can leaveOPENAI_API_KEY
blank to use the mock service.DATABASE_URL=postgresql://postgres:your_password@localhost:5432/code_refactorer SECRET_KEY=a-very-secret-key OPENAI_API_KEY= FRONTEND_URL=http://localhost:3000
-
Run Database Migrations:
alembic upgrade head
-
Start the Backend Server:
uvicorn app.main:app --reload
The API will be available at
http://127.0.0.1:8000
.
From the project root:
uvicorn app.main:app --reload
The backend will be available at http://127.0.0.1:8000.
Open a new terminal, then:
cd frontend
npm install # Only needed the first time
npm run dev
The frontend will be available at http://localhost:5173 (or the port Vite prints).
- Open http://localhost:5173 in your browser.
- Enter your code in the left editor.
- Use the buttons to refactor, analyze, or get suggestions.
- The backend and frontend communicate via HTTP (CORS is already enabled).
To ensure everything is working correctly, run the test suite:
pytest
Once the backend is running, interactive API documentation is available at:
- Swagger UI:
http://localhost:8000/docs
- ReDoc:
http://localhost:8000/redoc
POST /api/refactoring/
: Submit code for refactoring. This is an asynchronous operation.GET /api/refactoring/{refactoring_id}
: Check the status and retrieve the result of a refactoring request.POST /api/refactoring/analyze
: Analyze a piece of code and receive a quality report.POST /api/refactoring/suggestions
: Get a list of specific improvement suggestions for your code.POST /api/refactoring/explain
: Get a detailed explanation of what a piece of code does.
semantic-code-refactorer/
├── alembic/ # Database migrations
├── app/ # Backend FastAPI application
│ ├── core/ # Core functionality (config, database)
│ ├── models/ # SQLAlchemy database models
│ ├── schemas/ # Pydantic data models
│ ├── services/ # Business logic (AI and mock services)
│ ├── routes/ # API endpoint definitions
│ └── main.py # Main FastAPI app entrypoint
├── frontend/ # React/Vite frontend
└── tests/ # Pytest test suite
- Fork the repository.
- Create your feature branch (
git checkout -b feature/amazing-feature
). - Commit your changes (
git commit -m 'Add some amazing feature'
). - Push to the branch (
git push origin feature/amazing-feature
). - Open a Pull Request.
This project is licensed under the MIT License.