The Sentinel is a command-line interface (CLI) tool designed to leverage the power of Google's Gemini large language models for automated code analysis. It provides developers with AI-driven insights to improve code quality, generate documentation, create conventional commit messages, and automatically refactor code.
The tool is built with Python using the Typer framework for a robust and user-friendly CLI experience. It can operate in two primary modes:
- Git Integration: Analyzes changes in the Git staging area or all tracked files in the repository.
- Archive Analysis: Processes an entire codebase provided as a
.ziparchive.
This flexibility makes it a valuable asset for local development workflows and continuous integration (CI) pipelines.
The project is organized into a modular structure to separate concerns, promoting maintainability and scalability.
.
├── .env.example # Example environment variable file
├── .gitignore # Specifies files and directories to be ignored by Git
├── main.py # Main application entry point and CLI command definitions
├── project.bat # Optional Windows batch script to zip the project for testing
├── requirements.txt # Lists the project's Python dependencies
└── src/
├── __init__.py # Initializes the 'src' directory as a Python package
├── ai_analyzer.py # Handles all interactions with the Google Gemini API
├── file_handler.py # Manages file system operations, including reading and processing ZIP archives
└── git_utils.py # Contains utility functions for interacting with a Git repository
- Role: Application Entry Point & CLI Controller.
- Responsibilities:
- Defines the CLI commands and arguments/options (
task,--path,--output, etc.) using Typer. - Handles the primary application logic and orchestrates the workflow.
- Determines the context source (Git staged changes, all tracked files, or a
.zipfile) based on user input. - Invokes the appropriate functions from
git_utils.pyorfile_handler.pyto gather the code context. - Passes the collected context to the
ai_analyzer.pymodule for processing. - Writes the AI-generated output to the specified file.
- Implements CI mode (
--ci), which exits with a non-zero status code if improvements are found.
- Defines the CLI commands and arguments/options (
- Role: AI Interaction Layer.
- Responsibilities:
get_client(): Securely initializes the Google Gemini client using the provided API key.generate_ai_analysis():- Contains the core prompt engineering logic. It dynamically selects a system prompt tailored to the specific
task(e.g., "generate documentation", "suggest improvements"). - Constructs the final prompt by combining the system prompt with the provided code context.
- Sends the request to the specified Gemini model and returns the text response.
- Handles potential errors during the API call.
- Contains the core prompt engineering logic. It dynamically selects a system prompt tailored to the specific
- Role: Git Repository Interface.
- Responsibilities:
- Uses the GitPython library to interact with the local repository.
get_staged_diff(): Fetches thediffof staged changes, used for generating commit messages and applying improvements.get_staged_files_content(): Reads the full content of files that are currently in the staging area.get_all_tracked_files_content(): Reads the content of every file tracked by Git, used for generating whole-project documentation.
- Role: File System and Archive Processor.
- Responsibilities:
process_zip_file():- Validates the existence of the
.zipfile. - Extracts the archive's contents into a temporary directory for safe processing.
- Calls
read_project_files_from_dirto read the extracted project code.
- Validates the existence of the
read_project_files_from_dir():- Recursively traverses a project directory.
- Filters files based on a predefined list of allowed extensions (e.g.,
.py,.js,.md). - Skips common non-source directories (e.g.,
node_modules,.git,__pycache__). - Concatenates the content of all valid files into a single string to be used as context for the AI.
- Python 3.8+
- Git
-
Clone the repository:
git clone <repository_url> cd <repository_directory>
-
Create and activate a virtual environment (recommended):
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Get a Gemini API Key:
- Visit Google AI Studio to generate your free API key.
-
Set up the environment variable:
- Create a file named
.envin the project root by copying.env.example. - Add your API key to the
.envfile:GEMINI_API_KEY="YOUR_API_KEY_HERE"
- The application uses
python-dotenvto load this key automatically. Alternatively, you can pass the key directly via the--api-keyoption.
- Create a file named
The tool is invoked via the command, followed by a specific task and optional parameters.
General Syntax:
python main.py <TASK> [OPTIONS]
Available Tasks:
improvements: Reviews staged code and suggests improvements.documentation: Generates technical documentation for all tracked files.commits: Creates a Conventional Commit message from staged changes.apply-improvements: Generates a diff/patch file with suggested improvements.
Examples:
-
Generate a commit message for staged changes:
# Stage your changes first git add . # Run the command python main.py commits --output commit_message.txt
-
Get improvement suggestions for staged files:
git add src/main.py python main.py improvements --output suggestions.md
-
Generate documentation for the entire project:
python main.py documentation -o project_docs.md
-
Analyze a project from a
.ziparchive:python main.py improvements --path /path/to/my-project.zip
Note: The
commitsandapply-improvementstasks are not compatible with.zipanalysis. -
Generate a patch file to apply improvements:
git add . python main.py apply-improvements --output changes.diff # You can then apply the patch # git apply changes.diff
-
Use in a CI/CD Pipeline: The
--ciflag causes the script to exit with code 1 if theimprovementstask finds any issues, which will fail the pipeline step.git add . python main.py improvements --ci