AI Provider Integration
Relevant source files
The following files were used as context for generating this wiki page:
AI Provider Integration connects the pr-agent automation suite and autonomous scripts with Large Language Models (LLMs) to perform code analysis and automated repairs. The system primarily integrates with Google's Gemini API to process pull request findings and CI build failures.
This integration supports two primary workflows: code review analysis via PR-Agent and autonomous code repair via a specialized Python agent. Global settings are managed through a central configuration file, while runtime execution is handled by GitHub Actions workflows and the autofix.py script.
Sources: README.md:1-5, scripts/autofix.py:1-25
Gemini API Communication
The project interacts with Google AI Studio using the Gemini API. The communication logic resides in the call_gemini function, which transmits prompts and receives structured JSON responses.
Request Configuration
The system uses the following parameters for API calls:
- Endpoint:
https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent - Authentication: Requires a
GEMINI_API_KEYprovided via environment variables. - Model: Defaults to
gemini-3.8-flashif not explicitly specified. - Temperature: Set to
0.1to ensure deterministic and consistent code generation. - MIME Type: Enforces
application/jsonresponse format for programmatic parsing.
Sources: scripts/autofix.py:100-112
API Flow Diagram
The following diagram illustrates the interaction between the local autofix.py script and the Gemini API.
Sources: scripts/autofix.py:113-130
Context Management and Prompting
To ensure accurate code repairs, the system aggregates context from multiple sources before communicating with the AI provider.
Context Limits
The script enforces specific constraints to manage token usage and API limits:
- File Size: Skips files exceeding 80,000 bytes.
- File Count: Limits context to a maximum of 12 files.
- Prompt Length: Truncates review findings text to approximately 8,000 characters.
Sources: scripts/autofix.py:28-30, scripts/autofix.py:265-275
Prompt Types
The system utilizes two distinct system prompts based on the operational mode:
| Mode | System Prompt Objective | Derived From |
|---|---|---|
| Review-Fix | Fix genuine bugs and security issues flagged by PR-Agent while skipping style nitpicks. | scripts/autofix.py:186-200 |
| CI-Fix | Diagnose root causes of build/test failures and provide minimal, surgical repairs. | scripts/autofix.py:202-214 |
Data Schemas
The AI provider is instructed to return data in a strict JSON format. This allows the apply_repair_files function to safely write updates to the local filesystem.
Expected Response Schema
{
"summary": "Short explanation of the fix",
"files": [
{
"path": "relative/path/to/file",
"content": "Full, complete updated file contents"
}
]
}
Sources: scripts/autofix.py:194-199, scripts/autofix.py:210-213
Configuration and Environment
AI provider behavior is controlled via environment variables and global configuration files.
| Variable | Description | Default |
|---|---|---|
GEMINI_API_KEY | Required key for Google AI Studio authentication. | None |
GEMINI_MODEL | The specific model version to utilize. | gemini-3.8-flash |
GH_TOKEN | GitHub token for querying PR comments and posting feedback. | None |
REPO | The target owner/repository string. | None |
| Sources: scripts/autofix.py:15-26, scripts/autofix.py:350-360 |
Error Handling and Safety
The integration includes several safeguards to prevent improper code injection or API failures:
- Path Normalization: Validates file paths using
safe_normpathto ensure the AI does not attempt to write files outside the repository boundaries. - Verification Loop: Runs local test commands (e.g.,
npm test,pytest,cargo check) after applying AI-generated changes. - Rollback: Executes
git checkout -- .if the AI's proposed fix fails re-verification.
Sources: scripts/autofix.py:175-182, scripts/autofix.py:217-240
The project manages global settings for the PR-Agent tool via the .pr_agent.toml file, which is inherited by all repositories under the organization. Repository-level overrides are supported to allow specific customizations while maintaining central AI provider standards.
Sources: README.md:7-16