# coderabbitai/ai-pr-reviewer architecture

> Architecture diagram of the GitHub repository coderabbitai/ai-pr-reviewer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/coderabbitai/ai-pr-reviewer
- Repository: https://github.com/coderabbitai/ai-pr-reviewer

## Overview

##### Main System Components

a) GitHub Integration Layer
- This as a top-level component that handles GitHub events
- Webhook receivers for PR events and comment events
- Integration with GitHub API (via Octokit)

b) Core Processing Layer
- Central controller component (main.ts)
- Review processor
- Comment processor
- Token management system

c) OpenAI Integration Layer
- API client interface
- Model selector (GPT-3.5-Turbo vs GPT-4)
- Token counting and management

##### Component Relationships

a) Primary Flow:
**GitHub Events → GitHub Integration → Core Processing → OpenAI Integration → GitHub Response**

b) Secondary Flows:
- Comment handling flow
- Token management flow
- Review skip logic flow

##### Specific Components

- Bot Controller (bot.ts)
- Comment Manager (commenter.ts)
- Input Handler (inputs.ts)
- Review Manager (review.ts)
- Tokenizer (tokenizer.ts)
- Prompt Manager (prompts.ts)

##### Key Interactions to Highlight

Crucial interactions:
- PR event triggering review process
- Comment event triggering conversation
- Token limit checks
- Model selection logic
- Review skip conditions

##### Technical Details

Annotations for:
- Used models (gpt-3.5-turbo, gpt-4)
- API endpoints
- Configuration options
- Concurrency handling

##### Specific Implementation Notes

Create sections showing:
- Environment variable handling
- Permission requirements
- Webhook event processing
- Error handling paths

**┌─ GitHub Layer ─────────────────────┐**
- PR Events                        │
- Comment Events                   │
- GitHub API Integration           │
- Core Processing Layer ────────────┤
- Review Logic                     │
- Comment Processing               │
- Token Management                 │
- AI Integration Layer ─────────────┤
- OpenAI API Client               │
- Model Selection                  │
- Response Processing             │
- ┘

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph GitHub_Layer
        GH_Events[GitHub Events]
        PR_Events[PR Events]
        Comment_Events[Comment Events]
        GH_API[GitHub API Integration]:::github
    end

    subgraph Core_Processing
        Main[Main Controller]:::core
        Bot[Bot Controller]:::core
        Review[Review Manager]:::core
        Comment[Comment Manager]:::core
        Input[Input Handler]:::core
        Token[Token Management]:::core
        Prompt[Prompt Manager]:::core
        RateLimit[Rate Limiting]:::core
        Config[Configuration Options]:::core
        ReviewComment[Review Comment Handler]:::core
    end

    subgraph AI_Layer
        OpenAI[OpenAI API Client]
        ModelSelect{Model Selection}
        TokenCheck{Token Limit Check}
    end

    %% Relationships
    GH_Events --> PR_Events & Comment_Events
    PR_Events & Comment_Events --> GH_API
    GH_API --> Main
    Main --> Bot
    Bot --> Input
    Bot --> Review
    Bot --> Comment
    Review --> Token
    Review --> Prompt
    Review --> ReviewComment
    Comment --> Token
    Token --> TokenCheck
    TokenCheck --> ModelSelect
    ModelSelect --> OpenAI
    RateLimit --> Bot
    Config --> Bot

    %% Click Events
    click Main "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/main.ts"
    click GH_API "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/octokit.ts"
    click Bot "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/bot.ts"
    click Comment "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/commenter.ts"
    click Input "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/inputs.ts"
    click Review "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/review.ts"
    click Token "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/tokenizer.ts"
    click Prompt "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/prompts.ts"
    click ReviewComment "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/review-comment.ts"
    click RateLimit "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/limits.ts"
    click Config "https://github.com/coderabbitai/ai-pr-reviewer/blob/main/src/options.ts"

    %% Styling
    classDef github fill:#2088FF,color:white
    classDef core fill:#FFB366,color:black
    classDef ai fill:#34A853,color:white

    %% Legend
    subgraph Legend
        L1[GitHub Components]:::github
        L2[Core Components]:::core
        L3[AI Components]:::ai
    end
```
