# lathiya50/codereviewai architecture

> Architecture diagram of the GitHub repository lathiya50/codereviewai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lathiya50/codereviewai
- Repository: https://github.com/lathiya50/codereviewai

## Overview

This repository is a Next.js 16 application organized as a full-stack product with a clear split between presentation, typed application APIs, server-side domain services, and background processing.

#### High-level architecture

The system centers on one workflow: GitHub pull requests are ingested, analyzed by an AI service, stored in PostgreSQL, and then surfaced in a dashboard with analytics and review detail views.

There are four major architectural surfaces:

1. **Web UI**
   - Public marketing/landing experience
   - Authentication screens
   - Protected dashboard for repositories, reviews, pull requests, and analytics

2. **Application API layer**
   - tRPC endpoints for typed client-server communication
   - Auth callback/session routes
   - GitHub webhook route
   - Inngest webhook/worker route

3. **Server domain layer**
   - Database access via Prisma
   - GitHub integration services
   - AI review orchestration and provider abstraction
   - Background job functions

4. **Persistence and external services**
   - PostgreSQL database
   - GitHub API and webhooks
   - AI model providers
   - Inngest for asynchronous processing
   - Better Auth for GitHub OAuth/session management

#### Frontend structure

The app uses the Next.js App Router with route groups that separate unauthenticated and authenticated experiences:

- `src/app/(auth)/...`
  - Sign-in and sign-up pages
  - Auth layout
- `src/app/(dashboard)/...`
  - Analytics page
  - Repositories list and repository detail pages
  - Pull request detail and comparison views
  - Reviews page
  - Dashboard layout

The dashboard UI is built from reusable component modules under `src/components`, with specialized subdomains for:
- analytics charts and KPI cards
- code diff / comparison rendering
- review result display
- session protection and GitHub connection prompts
- shared UI primitives

This indicates a strong “page shell + domain component” pattern: route-level pages assemble reusable widgets rather than embedding business logic in the UI.

#### Authentication and session boundary

Authentication is handled through Better Auth with GitHub OAuth:

- `src/app/api/auth/[...all]/route.ts` handles the auth server routes
- `src/lib/auth-client.ts` provides client-side auth integration
- `src/server/auth/index.ts` contains server auth setup
- `src/components/session-guard.tsx` protects authenticated areas
- `src/app/(dashboard)/layout.tsx` is the main gated shell for signed-in users

The boundary here is important: the UI assumes authenticated access for dashboard routes, while auth state and session lifecycle are managed server-side and surfaced to the client through the auth client and session guard.

#### API layer

The application exposes multiple server entry points, each with a different responsibility:

##### tRPC
- `src/app/api/trpc/[trpc]/route.ts`
- Server routers under `src/server/api/routers/`
  - `analytics.ts`
  - `pull-request.ts`
  - `respository.ts`  "repository" appears misspelled in the tree
  - `review.ts`
- `src/server/api/root.ts` composes the routers
- `src/server/api/trpc.ts` configures the tRPC context/runtime
- `src/lib/trpc/client.ts`, `provider.tsx`, and `index.ts` connect the frontend to these endpoints

This is the main typed application API for dashboard data. The frontend likely uses it for repository lists, review history, analytics, and pull request metadata.

##### GitHub webhooks
- `src/app/api/webhooks/github/route.ts`

This is the external event ingress point. GitHub pushes PR events into the system here, and those events initiate asynchronous review processing.

##### Inngest
- `src/app/api/inngest/route.ts`
- `src/server/inngest/client.ts`
- `src/server/inngest/index.ts`
- `src/server/inngest/functions/review-pr.ts`

This is the background job boundary. Instead of doing review analysis synchronously inside the webhook request, the app offloads work into Inngest for reliability, retries, and non-blocking processing.

#### Background processing flow

The background review pipeline is one of the most important parts of the architecture:

1. A GitHub webhook is received for a repository or pull request event.
2. The event is translated into a background job or triggered review task.
3. Inngest runs the `review-pr` function.
4. That function fetches PR data and diff context from GitHub services.
5. The AI review service generates structured feedback and risk assessment.
6. Review results are persisted to the database.
7. Comments/suggestions may be posted back to GitHub.
8. The dashboard later reads the stored results for display and analytics.

This flow cleanly separates:
- external event ingestion
- asynchronous processing
- data persistence
- user-facing visualization

#### Domain services

##### GitHub integration
- `src/server/services/github.ts`

This service is the repository’s GitHub boundary. It likely encapsulates fetching repositories, PR metadata, diffs, file changes, and posting review comments. Keeping this as a dedicated service isolates GitHub API concerns from UI and AI logic.

##### AI review service
- `src/server/services/ai/`
  - `index.ts`
  - `review.ts`
  - `registry.ts`
  - `types.ts`
  - `providers/openai.ts`
  - `providers/groq.ts`

This layer abstracts model-specific behavior behind a provider registry. The important architecture point is that review generation is not hardwired to a single model API; it is mediated by a provider interface, allowing the system to switch or route between model backends.

The AI service is responsible for turning code context into:
- review comments
- suggestions
- risk scores or severity classifications
- structured result payloads suitable for persistence and UI rendering

The README mentions Gemini/OpenAI, while the tree shows OpenAI and Groq providers, so the stable architectural idea is “pluggable LLM providers” rather than a single vendor.

#### Persistence boundary

- `prisma/schema.prisma`
- `src/server/db/index.ts`

The database is PostgreSQL accessed through Prisma. The schema is the system of record for:
- users
- OAuth accounts and sessions
- repositories
- review records

This storage layer sits behind the tRPC routers, GitHub sync services, and background jobs. The frontend never talks directly to the database.

From the README and structure, the stored domain entities appear to drive most screens:
- repository list and detail pages
- pull request review history
- analytics dashboards
- review status tracking

#### Data access pattern

The likely data path is:

- UI components call tRPC procedures
- tRPC procedures call server domain services
- services read/write Prisma models
- responses are returned to the UI through typed contracts

This gives the app a clean boundary between presentation and server logic, and it makes the dashboard screens data-driven rather than client-state-heavy.

#### Dashboard and analytics subsystem

The analytics area is a distinct subsystem under `src/components/analytics` and `src/app/(dashboard)/analytics`.

It contains specialized visualizations such as:
- stats cards
- activity heatmap
- review charts
- risk distribution
- repository leaderboard
- top issues

This indicates the product is not just a review inbox; it also aggregates review history into operational metrics. Those metrics likely come from the analytics tRPC router backed by the review and repository tables.

#### Review and diff visualization subsystem

The app contains several components specifically for presenting AI findings alongside source changes:
- `code-diff.tsx`
- `diff-viewer.tsx`
- `code-comparison.tsx`
- `suggestion-diff-viewer.tsx`
- `review-result.tsx`
- `review-comparison-card.tsx`

These components suggest a user flow where a reviewer can inspect:
- raw code changes
- AI-generated commentary
- suggested fixes
- before/after comparison context

This is an important presentation boundary because the product is not merely listing reviews; it is rendering AI output in a developer-friendly diff-oriented format.

#### Route and navigation structure

The app is split into:
- a landing/home experience at `src/app/page.tsx`
- authenticated application routes under `(dashboard)`
- auth flows under `(auth)`

There are also app-level files for:
- global styles
- error handling
- not-found handling
- icon and Open Graph assets

This indicates a conventional consumer-facing SaaS layout with a public entry point and a protected product area.

#### Cross-cutting UI infrastructure

Several reusable support layers are present:
- `src/components/theme-provider.tsx` and `theme-toggle.tsx` for light/dark mode
- `src/components/header.tsx` and `user-menu.tsx` for app shell navigation
- `src/components/ui/*` for shared design system primitives
- `src/lib/motion.ts` and animated UI components for motion and transitions
- `src/lib/utils.ts` for shared UI helpers

These do not change the core business architecture, but they explain how the UI shell is assembled consistently across pages.

#### External integration summary

The repository depends on a few external systems that materially shape the architecture:

- **GitHub**: source of auth, repo metadata, pull request events, and comment posting
- **PostgreSQL**: durable storage for users, repositories, reviews, and sessions
- **Better Auth**: session and OAuth identity management
- **AI provider APIs**: review generation and risk scoring
- **Inngest**: asynchronous review execution and background reliability

#### Diagram-oriented mental model

If you were drawing this system, the clearest structure would be:

- A **browser/UI layer** at the top
- A **Next.js application layer** in the middle with:
  - App Router pages
  - tRPC routes
  - auth routes
  - webhook routes
  - Inngest route
- A **server domain layer** beneath that with:
  - auth
  - GitHub integration
  - AI review orchestration
  - review/background jobs
  - database access
- External systems on the side:
  - GitHub
  - AI provider APIs
  - PostgreSQL
  - Inngest

The dominant data flow is event-driven for PR reviews, while the dashboard flow is request/response via tRPC over persisted data.

#### Core architectural takeaway

This repo is a SaaS-style AI code review platform where the frontend is mostly a consumer of typed server APIs, and the real business logic lives in server services and background jobs. The two most important seams are:

1. **GitHub event ingestion to background AI review processing**
2. **Persisted review data back to dashboard analytics and PR detail views**

Those two seams define the system’s main runtime behavior and should be the primary focus of any architecture diagram.

Main entry points: [`src/server/api/root.ts`](https://github.com/lathiya50/codereviewai/blob/HEAD/src/server/api/root.ts) (API Routers), [`src/server/inngest/functions/review-pr.ts`](https://github.com/lathiya50/codereviewai/blob/HEAD/src/server/inngest/functions/review-pr.ts) (Review Job), [`src/app/(dashboard)`](https://github.com/lathiya50/codereviewai/tree/HEAD/src/app/(dashboard)) (Dashboard), [`src/components/header.tsx`](https://github.com/lathiya50/codereviewai/blob/HEAD/src/components/header.tsx) (App Shell)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph "Web UI"
  app_landing["Landing<br/>page shell<br/>[page.tsx]"]
  app_auth["Auth Pages<br/>auth routes"]
  app_dashboard["Dashboard<br/>protected app"]
  ui_analytics["Analytics UI<br/>dashboard views"]
  ui_review["Review UI<br/>diff views<br/>[review-result.tsx]"]
  ui_shell["App Shell<br/>shared UI<br/>[header.tsx]"]
end

subgraph "Application API"
  api_trpc_route["tRPC Route<br/>app route<br/>[route.ts]"]
  api_auth_route["Auth Route<br/>app route<br/>[route.ts]"]
  api_github_webhook["GitHub Webhook<br/>event ingress<br/>[route.ts]"]
  api_inngest_route["Inngest Route<br/>job ingress<br/>[route.ts]"]
end

subgraph "Server Domain"
  trpc_root["API Routers<br/>tRPC routers<br/>[root.ts]"]
  auth_server["Auth Setup<br/>auth server<br/>[index.ts]"]
  github_service["GitHub Service<br/>integration service<br/>[github.ts]"]
  ai_service["AI Review<br/>llm service<br/>[review.ts]"]
  ai_registry["Model Registry<br/>llm providers<br/>[registry.ts]"]
  review_job["Review Job<br/>background function<br/>[review-pr.ts]"]
end

subgraph "External Systems"
  db[("PostgreSQL<br/>prisma db<br/>[schema.prisma]")]
  github_ext(("GitHub<br/>external api"))
  model_ext(("AI Providers<br/>external api"))
  inngest_ext(("Inngest<br/>workflow service"))
end

app_landing -->|"renders"| ui_shell
app_auth -->|"auth"| api_auth_route
app_dashboard -->|"uses"| ui_analytics
app_dashboard -->|"uses"| ui_review
app_dashboard -->|"guards"| ui_shell
ui_shell -->|"session"| api_auth_route
api_trpc_route -->|"serves"| trpc_root
trpc_root -->|"reads/writes"| db
trpc_root -->|"fetches"| github_service
trpc_root -->|"returns review data"| ai_service
api_auth_route -->|"delegates"| auth_server
auth_server -->|"sessions"| db
api_github_webhook -->|"receives"| github_ext
api_github_webhook -->|"triggers"| api_inngest_route
api_inngest_route -->|"executes"| inngest_ext
review_job -->|"loads PRs"| github_service
review_job -->|"analyzes"| ai_service
ai_service -->|"routes"| ai_registry
ai_registry -->|"calls"| model_ext
review_job -->|"persists"| db
review_job -->|"comments"| github_ext
ui_analytics -->|"queries"| trpc_root
ui_review -->|"queries"| trpc_root

click app_landing "https://github.com/lathiya50/codereviewai/blob/main/src/app/page.tsx"
click app_auth "https://github.com/lathiya50/codereviewai/tree/main/src/app/(auth)"
click app_dashboard "https://github.com/lathiya50/codereviewai/tree/main/src/app/(dashboard)"
click ui_analytics "https://github.com/lathiya50/codereviewai/tree/main/src/components/analytics"
click ui_review "https://github.com/lathiya50/codereviewai/blob/main/src/components/review-result.tsx"
click ui_shell "https://github.com/lathiya50/codereviewai/blob/main/src/components/header.tsx"
click api_trpc_route "https://github.com/lathiya50/codereviewai/blob/main/src/app/api/trpc/[trpc]/route.ts"
click api_auth_route "https://github.com/lathiya50/codereviewai/blob/main/src/app/api/auth/[...all]/route.ts"
click api_github_webhook "https://github.com/lathiya50/codereviewai/blob/main/src/app/api/webhooks/github/route.ts"
click api_inngest_route "https://github.com/lathiya50/codereviewai/blob/main/src/app/api/inngest/route.ts"
click trpc_root "https://github.com/lathiya50/codereviewai/blob/main/src/server/api/root.ts"
click auth_server "https://github.com/lathiya50/codereviewai/blob/main/src/server/auth/index.ts"
click github_service "https://github.com/lathiya50/codereviewai/blob/main/src/server/services/github.ts"
click ai_service "https://github.com/lathiya50/codereviewai/blob/main/src/server/services/ai/review.ts"
click ai_registry "https://github.com/lathiya50/codereviewai/blob/main/src/server/services/ai/registry.ts"
click review_job "https://github.com/lathiya50/codereviewai/blob/main/src/server/inngest/functions/review-pr.ts"
click db "https://github.com/lathiya50/codereviewai/blob/main/prisma/schema.prisma"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class app_landing,app_auth,app_dashboard,ui_analytics,ui_review,ui_shell toneBlue
class api_trpc_route,api_auth_route,api_github_webhook,api_inngest_route toneAmber
class trpc_root,auth_server,github_service,ai_service,ai_registry,review_job toneMint
class db,github_ext,model_ext,inngest_ext toneRose
```

## Components

### Web UI

Public landing, auth screens, and protected dashboard surfaces.

- [Landing](https://github.com/lathiya50/codereviewai/blob/HEAD/src/app/page.tsx): page shell · Public home and marketing entry point. · `src/app/page.tsx`
- [Auth Pages](https://github.com/lathiya50/codereviewai/tree/HEAD/src/app/(auth)): auth routes · Sign-in and sign-up route group for unauthenticated users. · `src/app/(auth)`
- [Dashboard](https://github.com/lathiya50/codereviewai/tree/HEAD/src/app/(dashboard)): protected app · Signed-in shell for repositories, reviews, pull requests, and analytics. · `src/app/(dashboard)`
- [Analytics UI](https://github.com/lathiya50/codereviewai/tree/HEAD/src/components/analytics): dashboard views · Charts and KPI components for review metrics and repository ranking. · `src/components/analytics`
- [Review UI](https://github.com/lathiya50/codereviewai/blob/HEAD/src/components/review-result.tsx): diff views · Code diff, comparison, and AI review presentation components. · `src/components/review-result.tsx`
- [App Shell](https://github.com/lathiya50/codereviewai/blob/HEAD/src/components/header.tsx): shared UI · Header, session guard, theme, and shared design primitives. · `src/components/header.tsx`

### Application API

Next.js server entry points for typed APIs, auth, webhooks, and jobs.

- [tRPC Route](https://github.com/lathiya50/codereviewai/blob/HEAD/src/app/api/trpc/%5Btrpc%5D/route.ts): app route · Typed client-server entry point for dashboard data. · `src/app/api/trpc/[trpc]/route.ts`
- [Auth Route](https://github.com/lathiya50/codereviewai/blob/HEAD/src/app/api/auth/%5B...all%5D/route.ts): app route · Better Auth server route handling OAuth and session lifecycle. · `src/app/api/auth/[...all]/route.ts`
- [GitHub Webhook](https://github.com/lathiya50/codereviewai/blob/HEAD/src/app/api/webhooks/github/route.ts): event ingress · Receives pull request and repository events from GitHub. · `src/app/api/webhooks/github/route.ts`
- [Inngest Route](https://github.com/lathiya50/codereviewai/blob/HEAD/src/app/api/inngest/route.ts): job ingress · Webhook and worker entry point for background execution. · `src/app/api/inngest/route.ts`

### Server Domain

Auth, data access, GitHub integration, AI orchestration, and background jobs.

- [API Routers](https://github.com/lathiya50/codereviewai/blob/HEAD/src/server/api/root.ts): tRPC routers · Composed application routers for analytics, repositories, pull requests, and reviews. · `src/server/api/root.ts`
- [Auth Setup](https://github.com/lathiya50/codereviewai/blob/HEAD/src/server/auth/index.ts): auth server · Server-side authentication configuration and session handling. · `src/server/auth/index.ts`
- [GitHub Service](https://github.com/lathiya50/codereviewai/blob/HEAD/src/server/services/github.ts): integration service · Encapsulates GitHub API access, PR metadata, diffs, and comment posting. · `src/server/services/github.ts`
- [AI Review](https://github.com/lathiya50/codereviewai/blob/HEAD/src/server/services/ai/review.ts): llm service · Provider-agnostic review generation and risk scoring. · `src/server/services/ai/review.ts`
- [Model Registry](https://github.com/lathiya50/codereviewai/blob/HEAD/src/server/services/ai/registry.ts): llm providers · Routes review requests across model backends and provider implementations. · `src/server/services/ai/registry.ts`
- [Review Job](https://github.com/lathiya50/codereviewai/blob/HEAD/src/server/inngest/functions/review-pr.ts): background function · Inngest function that fetches context, runs AI review, persists results, and may post comments. · `src/server/inngest/functions/review-pr.ts`

### External Systems

Durable storage and third-party services used by the product.

- [PostgreSQL](https://github.com/lathiya50/codereviewai/blob/HEAD/prisma/schema.prisma): prisma db · Primary persistence for users, repositories, reviews, sessions, and analytics data. · `prisma/schema.prisma`
- **GitHub**: external api · Source of authentication, repository data, pull request events, and review commenting.
- **AI Providers**: external api · LLM backends used for structured code review generation.
- **Inngest**: workflow service · Asynchronous job orchestration and retry infrastructure.

## Connections

- Landing → App Shell: renders · Public pages compose shared UI primitives and navigation.
- Auth Pages → Auth Route: auth · Auth screens depend on the Better Auth server route.
- Dashboard → Analytics UI: uses · Dashboard pages assemble analytics widgets for metrics views.
- Dashboard → Review UI: uses · Dashboard pages render diffs and AI review results.
- Dashboard → App Shell: guards · Authenticated shell and session guard protect dashboard routes.
- App Shell → Auth Route: session · Client auth state is backed by server auth endpoints.
- tRPC Route → API Routers: serves · tRPC route dispatches into composed application routers.
- API Routers → PostgreSQL: reads/writes · Routers access persisted repositories, reviews, analytics, and sessions.
- API Routers → GitHub Service: fetches · Domain routers use GitHub data for repository and pull request views.
- API Routers → AI Review: returns review data · Review-related procedures surface AI-generated results and status.
- Auth Route → Auth Setup: delegates · Auth route is backed by server-side Better Auth setup.
- Auth Setup → PostgreSQL: sessions · Authentication relies on persisted users and session records.
- GitHub Webhook → GitHub: receives · Webhook route ingests GitHub repository and pull request events.
- GitHub Webhook → Inngest Route: triggers · Incoming events are handed off for asynchronous processing.
- Inngest Route → Inngest: executes · Inngest route connects the app to background job orchestration.
- Review Job → GitHub Service: loads PRs · Background review function fetches repository and diff context.
- Review Job → AI Review: analyzes · Review job invokes AI review generation.
- AI Review → Model Registry: routes · AI review layer selects provider implementations through a registry.
- Model Registry → AI Providers: calls · Provider implementations connect to external model APIs.
- Review Job → PostgreSQL: persists · Generated review results and status are stored durably.
- Review Job → GitHub: comments · Review output may be posted back to GitHub as comments or suggestions.
- Analytics UI → API Routers: queries · Charts and metrics read from typed application procedures.
- Review UI → API Routers: queries · Review detail views load repository and pull request data through tRPC.
