Skip to content

Pvnn/code-review-agent

Repository files navigation

Code Review AI Agent

An autonomous, real-time code review agent powered by Anthropic's Claude. This tool acts as an automated senior engineer on your team by fetching pull requests, analyzing code for bugs and anti-patterns, adhering to custom team style guides, and dropping inline code comments directly onto the GitHub PR timeline.

Features

  • Autonomous Code Analysis: Connects directly to GitHub to fetch PR diffs and analyzes them using Claude.
  • Real-Time Streaming: Features a modern, minimalist local dashboard built with Server-Sent Events (NDJSON) to stream the agent's internal thought process and execution states in real-time.
  • Inline GitHub Comments: Uses the GitHub PR Reviews API to attach targeted comments precisely to the offending lines of code in the "Files changed" tab.
  • Idempotency: Cross-references its findings with existing PR comments to ensure it never spams your repository with duplicate issues.
  • Custom Team Rules: Inject ad-hoc style guides or strict typing rules via the dashboard to enforce project-specific conventions.
  • Automated CI/CD Webhooks: Fully automated background mode. Configure a GitHub webhook to automatically trigger reviews the moment a PR is opened or updated.
  • Netra Observability: Fully instrumented with the Netra SDK for deep tracing of LLM tool usage and execution spans.

File Structure

code-review-agent/
├── public/                 # Frontend dashboard assets
│   ├── app.js              # Real-time NDJSON stream parser and UI logic
│   ├── index.html          # Minimalist dashboard UI
│   └── styles.css          # Custom styling and animations
├── src/
│   ├── agent/
│   │   └── loop.ts         # Anthropic tool-use iterative loop
│   ├── config/
│   │   └── prompt.ts       # Core system prompt and idempotency rules
│   ├── tools/
│   │   ├── addLabels.ts    # Tool to apply severity labels via GitHub API
│   │   ├── fetchPR.ts      # Tool to fetch diffs and parse existing PR comments
│   │   ├── index.ts        # Tool registry and Anthropic input schemas
│   │   └── postReview.ts   # Tool to post inline review comments via GitHub API
│   ├── types/
│   │   └── index.ts        # Shared TypeScript interfaces
│   └── server.ts           # Express backend and CI/CD webhook endpoint
├── package.json
├── tsconfig.json
└── README.md

Prerequisites

  • Node.js (v18+)
  • A GitHub Personal Access Token (PAT) with repository read/write permissions.
  • An Anthropic API Key.
  • A Netra API Key for observability instrumentation.

Installation

  1. Clone the repository:

    git clone https://github.com/Pvnn/code-review-agent.git
    cd code-review-agent
  2. Install dependencies:

    npm install
  3. Configure Environment Variables: Create a .env file in the root directory:

    ANTHROPIC_API_KEY=your_anthropic_api_key_here
    GITHUB_TOKEN=your_github_pat_here
    NETRA_API_KEY=your_netra_api_key_here
    NETRA_OTLP_ENDPOINT=your_netra_otlp_endpoint_here

Setting Up Netra Observability

This project uses netra-sdk to trace agent execution spans and monitor tool usage.

  1. Create an account and project on your Netra dashboard.
  2. Obtain your API key and add it to your .env file as NETRA_API_KEY.
  3. The SDK is already initialized asynchronously in src/server.ts via Netra.init(). Custom spans such as review_pr and webhook_review_pr will automatically begin syncing to your observability dashboard when the server runs.

Running the Dashboard Locally

To start the local dashboard:

npm run dev
# or manually via tsx:
npx tsx src/server.ts

Open http://localhost:3000 in your browser. Paste a Pull Request URL, optionally add custom rules (e.g., "Enforce strict TypeScript typings"), and click Generate Review.

Setting up GitHub Webhooks (CI/CD Mode)

You can set up the agent to automatically review PRs in the background using GitHub Webhooks.

1. Expose your local server

Use a webhook delivery service like Smee.io or ngrok to expose your local port 3000 to the internet.

Using Smee (Recommended):

npx smee-client -u https://smee.io/your-custom-channel -p 3000 -P /api/webhook

2. Configure GitHub

  1. Navigate to your target repository on GitHub.
  2. Go to Settings > Webhooks > Add webhook.
  3. Set Payload URL to your Smee or ngrok URL (e.g., https://smee.io/your-custom-channel).
  4. Set Content type to application/json.
  5. Under events, select Let me select individual events, uncheck Pushes, and check Pull requests.
  6. Save the webhook.

The agent will now autonomously review any new PRs or commits pushed to your repository.

Architecture

  • Backend: Express.js with TypeScript (tsx runtime).
  • Frontend: Vanilla HTML/JS/CSS with Server-Sent Events parsing.
  • AI: @anthropic-ai/sdk executing an iterative Tool Use loop.
  • Observability: netra-sdk for tracing asynchronous LLM operations.

About

An autonomous, real-time Code Review Agent powered by Claude. Automatically fetches PRs, enforces custom team rules, and drops inline comments on GitHub

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors