Skip to content

Latest commit

 

History

History
227 lines (182 loc) · 6.42 KB

File metadata and controls

227 lines (182 loc) · 6.42 KB

🚀 Getting Started - PR Context Lens

What You've Built

PR Context Lens is a fully-scaffolded Chrome Extension that:

  • Detects GitHub PR pages
  • Parses unified diffs into structured format
  • Sends diffs to AI providers (OpenAI GPT-4o or Anthropic Claude)
  • Displays analysis results in a sidebar panel

Project Status ✅

Completed:

  • ✅ Full TypeScript project with strict type checking
  • ✅ React 18 UI with Tailwind CSS
  • ✅ Chrome Extension Manifest V3 configuration
  • ✅ Modular architecture with clear separation of concerns
  • ✅ Comprehensive test suite (21 passing tests)
  • ✅ All core modules implemented and tested
  • ✅ Build system configured (Vite + TypeScript)

Quick Start

1. Installation

# Dependencies are already installed, but if needed:
npm install

2. Development

# Watch mode for development
npm run dev

3. Build

# Compile TypeScript and build extension
npm run build

4. Testing

# Run all unit tests
npm run test

# Watch mode for tests
npm run test -- --watch

# UI mode for tests
npm run test:ui

Loading into Chrome

  1. Run npm run build to create the dist/ folder
  2. Open chrome://extensions/
  3. Toggle "Developer mode" (top right)
  4. Click "Load unpacked"
  5. Select the dist/ folder
  6. Visit a GitHub PR page and look for the "📊 PR Analysis" button!

Project Structure

src/
├── shared/              # Types and utilities
│   ├── types.ts        # Core TypeScript interfaces
│   └── cache.ts        # Diff caching with chrome.storage
│
├── analyzer/           # Diff parsing and prompt building
│   ├── diff-parser.ts  # Parse unified diff format
│   └── prompt-builder.ts # Build AI analysis prompts
│
├── providers/          # AI provider integrations
│   ├── openai.ts       # OpenAI API client
│   ├── anthropic.ts    # Anthropic API client
│   └── index.ts        # Provider factory & routing
│
├── background/         # Service worker
│   └── service-worker.ts # Message handling hub
│
├── content/            # Content script
│   └── github-detector.ts # Detect PRs, inject UI
│
└── panel/              # React UI components
    ├── App.tsx         # Main panel component
    ├── components/     # Reusable UI components
    └── index.tsx       # Panel entry point

Configuration (Setup Required)

Before using the extension, you need to set API keys:

  1. Click the extension icon on a GitHub PR page
  2. Click the ⚙️ settings icon
  3. Enter your OpenAI or Anthropic API key
  4. Select your preferred provider
  5. Click "Save Configuration"

Getting API Keys:

Key Features Implemented

Core Analysis Pipeline

  1. Content Script detects GitHub PR pages
  2. Service Worker fetches diffs from GitHub API
  3. Diff Parser converts raw diff to structured format
  4. Prompt Builder creates AI analysis prompts
  5. AI Provider (OpenAI or Anthropic) analyzes the prompt
  6. React Panel displays results with formatting

Data Flow

GitHub PR Page
    ↓
Content Script (github-detector.ts)
    ↓ Injects button, fetches diff
Service Worker (service-worker.ts)
    ↓ Caches & routes messages
Diff Parser (diff-parser.ts)
    ↓ Parses unified diff
Prompt Builder (prompt-builder.ts)
    ↓ Builds AI prompt
AI Provider (openai.ts or anthropic.ts)
    ↓ Analyzes PR
React Panel (App.tsx)
    ↓ Displays results

Code Standards

All code follows these standards:

  • ✅ TypeScript strict mode
  • ✅ Functional components only
  • ✅ JSDoc comments on public functions
  • ✅ Comprehensive unit tests
  • ✅ Type-only imports where appropriate
  • ✅ No external UI libraries beyond Tailwind
  • ✅ Async/await (no .then() chains)

Test Coverage

Current test suites:

  • diff-parser.test.ts - Unified diff parsing (3 tests)
  • prompt-builder.test.ts - Prompt construction (4 tests)
  • openai.test.ts - OpenAI API integration (4 tests)
  • anthropic.test.ts - Anthropic API integration (4 tests)
  • github-detector.test.ts - Content script utilities (3 tests)
  • service-worker.test.ts - Service worker basics (1 test)

Total: 21 passing tests

Next Steps to Extend

Add Features:

  1. Code Review Comments - Generate comments for specific lines
  2. Performance Analysis - Detect performance regressions
  3. Security Scanning - Flag security vulnerabilities
  4. Dependency Updates - Check for vulnerable dependencies
  5. Author Context - Learn from previous PRs by the author

Improvements:

  1. Add more comprehensive error handling
  2. Implement progress tracking for large diffs
  3. Add keyboard shortcuts
  4. Support PR comments and discussions
  5. Add dark/light theme toggle
  6. Persistent analysis history

Debugging

Chrome DevTools for Extensions

  1. Open chrome://extensions/
  2. Find "PR Context Lens"
  3. Click "Inspect views → service worker" to debug background script
  4. Open DevTools on any GitHub page to debug content script

View Chrome Storage

// In Chrome DevTools console on any page:
chrome.storage.local.get(['openai_api_key', 'anthropic_api_key'], console.log);

Clear Cache

// In Chrome DevTools console:
chrome.storage.local.remove('pr_context_lens_cache');

Common Issues

"API key not configured"

  • Make sure to set your API key in the extension settings (⚙️ icon)

"Failed to fetch diff"

  • Check if you're on a valid GitHub PR page (github.com/user/repo/pull/number)
  • Private repos may require authentication token

"Analysis failed"

  • Check your API key is valid and has credits
  • Try a smaller PR first
  • Check browser console for error details

Performance Notes

  • Diff Caching: Diffs are cached for 1 hour to avoid redundant API calls
  • Bundle Size: ~60.76 KB gzipped (React + Tailwind included)
  • API Costs: Each PR analysis uses ~100-200 tokens depending on diff size

Tech Stack Summary

  • Frontend: React 18 + Tailwind CSS
  • Bundler: Vite (modern, fast builds)
  • Language: TypeScript 5 (strict mode)
  • Testing: Vitest (fast, Vite-native)
  • APIs: OpenAI + Anthropic
  • Extension API: Chrome Manifest V3

Happy reviewing! 🚀

For more detailed information, see README.md