# sciphi-ai/r2r-application architecture

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

- Interactive diagram: https://gitdiagram.com/sciphi-ai/r2r-application
- Repository: https://github.com/sciphi-ai/r2r-application

## Overview

##### Main Components

Frontend Architecture:
- Client-Side Components (Next.js Pages and Components)
- State Management (Context API - seen in src/context)
- UI Component Library (Custom components in src/components/ui)
- Authentication Layer (auth pages present)
- Analytics Integration (PostHog client visible in src/lib)

Backend Integration:
- API Routes (Next.js API routes)
- Database Connection (Supabase integration visible in src/lib/supabase.ts)
- External Services Integration (R2R Framework connection)

- Pages (src/pages)
- Public Pages (landing, login)
- Protected Pages (chat, documents, analytics)
- Authentication Pages
- Components
- Layout Components
- Shared Components
- Feature-specific Components (ChatDemo, SearchResults)

- Context & State Management
- UserContext
- Hooks & Utils
- Type Definitions
- External Service Integrations

- Supabase Database
- R2R Framework Integration
- Analytics (PostHog)

##### Key Relationships to Highlight

- User Flow:
- Authentication flow through auth pages
- Protected routes and authorization
- Document management workflows
- Chat/RAG interaction patterns

- Data Flow:
- Client-server communication
- Database operations
- Analytics tracking
- Document processing pipeline

##### Implementation Details

Use these symbols and notations:
- Rectangles: Components and modules
- Diamonds: Decision points
- Clouds: External services

Create distinct sections for:
##### User Interface Layer
- Pages and routing
- Component hierarchy
- UI/UX elements

##### Business Logic Layer
- Document processing
- Search functionality
- Authentication logic
- State management

##### Data Access Layer
- Database interactions
- External API calls
- File storage

##### External Services
- R2R Framework
- Analytics
- Authentication providers

##### Additional Notes

- Real-time nature of the chat and document management features
- Separation between client and server-side code
- Security boundaries and authentication checkpoints
- Error handling and logging flows
- Configuration management (environment variables, settings)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Presentation Layer"
        UI["UI Components"]
        Pages["Pages"]
        Auth["Authentication Pages"]:::auth
        Layout["Layout Components"]:::layout
        SharedComp["Shared Components"]
        FeatureComp["Feature Components"]
        
        UI --> Pages
        UI --> Layout
        UI --> SharedComp
        UI --> FeatureComp
        Pages --> Auth
    end

    subgraph "Application Layer"
        StateManagement["State Management"]:::state
        Hooks["Hooks & Utils"]
        Types["Type Definitions"]:::types
        
        UI --> StateManagement
        StateManagement --> Hooks
        Hooks --> Types
    end

    subgraph "Data Layer"
        DB[(Supabase DB)]:::db
        R2R["R2R Framework"]
        Analytics["PostHog Analytics"]:::analytics
        
        StateManagement --> DB
        StateManagement --> R2R
        UI --> Analytics
    end

    subgraph "Feature Components"
        Chat["Chat/RAG Interface"]:::chat
        DocManagement["Document Management"]:::docs
        Search["Search Functionality"]:::search
        Collections["Collection Management"]:::collections
        AnalyticsDash["Analytics Dashboard"]:::analytics_dash
    end

    %% Component relationships
    Auth --> StateManagement
    FeatureComp --> StateManagement
    Chat --> R2R
    DocManagement --> DB
    Search --> R2R
    Collections --> DB
    AnalyticsDash --> Analytics

    %% Styling
    classDef auth fill:#FFA07A
    classDef state fill:#9370DB
    classDef db fill:#20B2AA
    classDef analytics fill:#DDA0DD
    classDef layout fill:#87CEEB
    classDef chat fill:#98FB98
    classDef docs fill:#F0E68C
    classDef search fill:#FFB6C1
    classDef collections fill:#E6E6FA
    classDef analytics_dash fill:#FFC0CB
    classDef types fill:#D3D3D3

    %% Click events
    click Auth "https://github.com/SciPhi-AI/R2R-Application/tree/main/src/pages/auth"
    click StateManagement "https://github.com/SciPhi-AI/R2R-Application/blob/main/src/context/UserContext.tsx"
    click UI "https://github.com/SciPhi-AI/R2R-Application/tree/main/src/components/ui"
    click Analytics "https://github.com/SciPhi-AI/R2R-Application/blob/main/src/lib/posthog-client.tsx"
    click DB "https://github.com/SciPhi-AI/R2R-Application/blob/main/src/lib/supabase.ts"
    click Chat "https://github.com/SciPhi-AI/R2R-Application/tree/main/src/components/ChatDemo"
    click DocManagement "https://github.com/SciPhi-AI/R2R-Application/blob/main/src/pages/documents.tsx"
    click Search "https://github.com/SciPhi-AI/R2R-Application/blob/main/src/pages/search.tsx"
    click Layout "https://github.com/SciPhi-AI/R2R-Application/tree/main/src/components/Layout"
    click AnalyticsDash "https://github.com/SciPhi-AI/R2R-Application/blob/main/src/pages/analytics.tsx"
    click Collections "https://github.com/SciPhi-AI/R2R-Application/blob/main/src/pages/collections/[id].tsx"
    click Types "https://github.com/SciPhi-AI/R2R-Application/blob/main/src/types.ts"
```
