# virattt/ai-financial-agent architecture

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

- Interactive diagram: https://gitdiagram.com/virattt/ai-financial-agent
- Repository: https://github.com/virattt/ai-financial-agent

## Overview

##### Main Components

a) Frontend Layer:
- Client-side Components (under /components)
- Authentication UI (/app/(auth))
- Chat Interface (/app/(chat))
- Theme and UI Components (/components/ui)

b) Backend Layer:
- API Routes (/app/api)
- Authentication System
- Database Layer (using Drizzle ORM)
- AI Integration Layer (/lib/ai)

c) External Services:
- OpenAI API Integration
- Financial Datasets API
- Authentication Provider
- Database (PostgreSQL)

**┌─ Client Layer ─────────────────────┐**
- ┌─ UI Components ─┐ ┌─ Pages ────┐ │
- Chat         │ │ - Login    │ │
- Editor       │ │ - Register │ │
- Documents    │ │ - Chat     │ │
- ┘ └────────────┘ │
- ┬────────────────────┘

**┌──────────────▼────────────────────┐**
- API Layer (/app/api)              │
- ┌─────────────┐ ┌───────────────┐ │
- Auth Routes │ │ Chat Routes   │ │
- Files API   │ │ History API   │ │
- ┘ └───────────────┘ │
- ┬────────────────────┘

**┌──────────────▼────────────────────┐**
- Service Layer                      │
- ┌─────────────┐ ┌───────────────┐ │
- AI SDK      │ │ DB Queries    │ │
- Middleware  │ │ Auth Service  │ │
- ┘ └───────────────┘ │
- ┬────────────────────┘

**┌──────────────▼────────────────────┐**
- External Services                  │
- ┌─────────────┐ ┌───────────────┐ │
- OpenAI API  │ │ Financial     │ │
- Auth Provider│ │ Datasets API │ │
- ┘ └───────────────┘ │
- ┘

##### Important Relationships to Highlight

- Data flow from Financial Datasets API through the backend to the frontend
- Authentication flow across all layers
- Database interactions for chat history and user data

##### Additional Elements

- Version information
- Notes about scalability points
- Potential future integration points

##### Technical Stack Indicators

- Next.js 13+ (App Router)
- Vercel AI SDK
- DrizzleORM
- PostgreSQL
- Tailwind CSS
- shadcn/ui components

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client["Client Layer"]
        direction TB
        UI["UI Components"]
        Pages["Pages"]
        
        subgraph UIComponents["UI Components Layer"]
            direction TB
            Chat["Chat Interface"]
            Editor["Editor System"]
            Documents["Documents"]
            BaseUI["Base UI Components"]
            Theme["Theme Provider"]
        end
        
        subgraph AuthPages["Authentication Pages"]
            direction TB
            Login["Login Page"]
            Register["Register Page"]
        end
    end

    subgraph API["API Layer"]
        direction TB
        AuthRoutes["Auth Routes"]
        ChatRoutes["Chat Routes"]
        DocRoutes["Document Routes"]
        HistoryRoutes["History Routes"]
    end

    subgraph Services["Service Layer"]
        direction TB
        AISDK["AI SDK"]
        DBQueries["Database Queries"]
        AuthService["Auth Service"]
        Security["Security Layer"]
    end

    subgraph External["External Services"]
        direction TB
        OpenAI["OpenAI API"]
        FinanceAPI["Financial Datasets API"]
        AuthProvider["Auth Provider"]
        DB[(PostgreSQL)]
    end

    Client --> API
    API --> Services
    Services --> External

    %% Component Mappings
    click Chat "https://github.com/virattt/ai-financial-agent/blob/main//components/chat.tsx"
    click Editor "https://github.com/virattt/ai-financial-agent/tree/main//lib/editor"
    click Theme "https://github.com/virattt/ai-financial-agent/blob/main//components/theme-provider.tsx"
    click BaseUI "https://github.com/virattt/ai-financial-agent/tree/main//components/ui"
    click Login "https://github.com/virattt/ai-financial-agent/blob/main//app/(auth)/login/page.tsx"
    click Register "https://github.com/virattt/ai-financial-agent/blob/main//app/(auth)/register/page.tsx"
    click AuthRoutes "https://github.com/virattt/ai-financial-agent/tree/main//app/(auth)/api/auth"
    click ChatRoutes "https://github.com/virattt/ai-financial-agent/tree/main//app/(chat)/api/chat"
    click DocRoutes "https://github.com/virattt/ai-financial-agent/tree/main//app/(chat)/api/document"
    click HistoryRoutes "https://github.com/virattt/ai-financial-agent/tree/main//app/(chat)/api/history"
    click AISDK "https://github.com/virattt/ai-financial-agent/tree/main//lib/ai"
    click DBQueries "https://github.com/virattt/ai-financial-agent/blob/main//lib/db/queries.ts"
    click Security "https://github.com/virattt/ai-financial-agent/blob/main//lib/utils/api-key-validation.ts"

    classDef frontend fill:#3b82f6,color:#fff
    classDef api fill:#22c55e,color:#fff
    classDef service fill:#eab308,color:#000
    classDef external fill:#a855f7,color:#fff
    classDef security fill:#ef4444,color:#fff

    class UIComponents,AuthPages frontend
    class AuthRoutes,ChatRoutes,DocRoutes,HistoryRoutes api
    class AISDK,DBQueries,AuthService service
    class OpenAI,FinanceAPI,AuthProvider,DB external
    class Security security
```
