# vimal0156/algostockgpt-ai architecture

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

- Interactive diagram: https://gitdiagram.com/vimal0156/algostockgpt-ai
- Repository: https://github.com/vimal0156/algostockgpt-ai

## Overview

##### Project type and purpose
- From the file tree and README, this is a full-stack web application built on Next.js 15 with React and TypeScript.
- It’s an AI-powered financial platform (“AlgoStockGPT AI”) offering a conversational interface, real-time market data, and algorithmic trading insights.
- Key tech: Next.js App Router, Tailwind CSS, Shadcn/UI, OpenAI GPT-4, Financial Data APIs, PostgreSQL via Drizzle ORM, NextAuth.js, Vercel deployment.

##### File structure
- Top-level:
- app/ – Next.js App Router directory, segmented into (auth), (chat), and global API routes.
- components/ – UI building blocks (chat UI, tables, editors, modals, Shadcn/UI wrappers).
- lib/ – core logic modules: AI engine, database (Drizzle) code, editors, types, utilities.
- contexts, hooks – React contexts (theme) and custom hooks for chat state, mobile detection, etc.
- drizzle.config.ts + lib/db – Drizzle ORM setup, migrations, schema, queries.
- next.config.ts, middleware.ts, instrumentation.ts – configuration, middleware (auth checks, metrics).
- public/, styles, tailwind.config.ts – static assets and styling.
- Notice the MVC-like layering:
- View: React pages/components under app/ and components/
- Controller/API: Next.js route handlers under app/api/ and app/(chat)/api/
- Model/DB: Drizzle ORM under lib/db with migrations and queries.
- Authentication: NextAuth.js configured in app/(auth)/api/auth/[...nextauth]/route.ts.
- AI integration: lib/ai contains GPT model wrappers, custom middleware, tools (financial-tools.ts).
- External integrations: Financial data API calls in lib/api, OpenAI calls in lib/ai.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    Client["Browser/Mobile"]:::client

    %% Next.js Frontend
    subgraph "Next.js Frontend"
        NextjsPages["App Router Pages (app/)"]:::ui
        UIComp["React UI Components (components/)"]:::ui
        Context["React Contexts & Providers"]:::cross
        Hooks["Custom Hooks (hooks/)"]:::cross
        Styling["Styling & UI Library"]:::cross
    end

    %% API Layer
    subgraph "API Layer"
        AuthAutoLogin["Global Auth Auto-login\n(GET /api/auth/auto-login)"]:::api
        AuthServ["NextAuth.js Auth Service\n(app/(auth)/api/auth/[...nextauth]/route.ts)"]:::api
        AuthConfig["Auth Config\n(app/(auth)/auth.config.ts)"]:::api

        ChatPage["Chat Pages\n(app/(chat)/page.tsx,\nchat/[id]/page.tsx)"]:::api

        subgraph "Chat APIs"
            ChatAPI["Chat Controller\n(app/(chat)/api/chat/route.ts)"]:::api
            HistoryAPI["Chat History API\n(app/(chat)/api/history/route.ts)"]:::api
            MsgCountAPI["Messages Count API\n(app/(chat)/api/messages/count/route.ts)"]:::api
            SuggestionsAPI["Suggestions API\n(app/(chat)/api/suggestions/route.ts)"]:::api
            VoteAPI["Vote API\n(app/(chat)/api/vote/route.ts)"]:::api
            FileUpload["File Upload API\n(app/(chat)/api/files/upload/route.ts)"]:::api
            DocAPI["Document API\n(app/(chat)/api/document/route.ts)"]:::api
        end
    end

    %% Services Layer
    subgraph "Services"
        AIEngine["AI Engine\n(lib/ai)"]:::service
        FinData["Financial Data Services\n(lib/api)"]:::service
    end

    %% Persistence Layer
    subgraph "Database Layer"
        DBConfig["Drizzle Config\n(drizzle.config.ts)"]:::db
        Schema["Schema & Queries\n(lib/db/schema.ts,\nlib/db/queries.ts)"]:::db
        Migrations["Migrations\n(lib/db/migrations/)"]:::db
        ApiKeys["API Keys Management\n(lib/db/api-keys.ts)"]:::db
    end

    %% Cross-cutting Middleware
    Middleware["Middleware & Instrumentation\n(middleware.ts,\ninstrumentation.ts)"]:::cross

    %% Connections: Client to Frontend
    Client --> NextjsPages
    Client --> UIComp

    %% Frontend internal dependencies
    UIComp --> NextjsPages
    Context --> NextjsPages
    Hooks --> NextjsPages
    Styling --> NextjsPages

    %% Frontend to API Layer
    NextjsPages --> AuthAutoLogin
    NextjsPages --> ChatPage

    %% Auth Flow
    AuthAutoLogin --> AuthServ
    AuthServ --> Schema
    AuthServ --> ApiKeys
    AuthServ --> DBConfig

    AuthConfig --> AuthServ

    %% Chat Flow
    ChatPage --> ChatAPI
    ChatPage --> HistoryAPI
    ChatPage --> MsgCountAPI
    ChatPage --> SuggestionsAPI
    ChatPage --> VoteAPI
    ChatPage --> FileUpload
    ChatPage --> DocAPI

    %% API to Middleware
    AuthAutoLogin --> Middleware
    ChatAPI --> Middleware
    HistoryAPI --> Middleware
    MsgCountAPI --> Middleware
    SuggestionsAPI --> Middleware
    VoteAPI --> Middleware
    FileUpload --> Middleware
    DocAPI --> Middleware

    %% API to Services & DB
    ChatAPI --> AIEngine
    SuggestionsAPI --> FinData
    HistoryAPI --> Schema
    MsgCountAPI --> Schema
    VoteAPI --> Schema
    DocAPI --> Schema
    FileUpload --> Schema

    %% Services to External
    AIEngine --> OpenAI["OpenAI GPT-4"]:::service
    FinData --> MarketAPI["Financial Data API"]:::service

    %% DB Layer internal
    Schema --> Migrations
    Schema --> DBConfig

    %% Click Events
    click NextjsPages "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/layout.tsx"
    click UIComp "https://github.com/vimal0156/algostockgpt-ai/tree/main/components/"
    click Context "https://github.com/vimal0156/algostockgpt-ai/blob/main/contexts/theme-context.tsx"
    click Hooks "https://github.com/vimal0156/algostockgpt-ai/blob/main/hooks/use-mobile.tsx"
    click Styling "https://github.com/vimal0156/algostockgpt-ai/blob/main/tailwind.config.ts"
    click AuthAutoLogin "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/api/auth/auto-login/route.ts"
    click AuthServ "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(auth)/api/auth/[...nextauth]/route.ts"
    click AuthConfig "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(auth)/auth.config.ts"
    click ChatPage "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(chat)/page.tsx"
    click ChatAPI "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(chat)/api/chat/route.ts"
    click HistoryAPI "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(chat)/api/history/route.ts"
    click MsgCountAPI "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(chat)/api/messages/count/route.ts"
    click SuggestionsAPI "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(chat)/api/suggestions/route.ts"
    click VoteAPI "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(chat)/api/vote/route.ts"
    click FileUpload "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(chat)/api/files/upload/route.ts"
    click DocAPI "https://github.com/vimal0156/algostockgpt-ai/blob/main/app/(chat)/api/document/route.ts"
    click AIEngine "https://github.com/vimal0156/algostockgpt-ai/blob/main/lib/ai/index.ts"
    click FinData "https://github.com/vimal0156/algostockgpt-ai/blob/main/lib/api/stock-filters.ts"
    click DBConfig "https://github.com/vimal0156/algostockgpt-ai/blob/main/drizzle.config.ts"
    click Schema "https://github.com/vimal0156/algostockgpt-ai/blob/main/lib/db/schema.ts"
    click Migrations "https://github.com/vimal0156/algostockgpt-ai/tree/main/lib/db/migrations/"
    click ApiKeys "https://github.com/vimal0156/algostockgpt-ai/blob/main/lib/db/api-keys.ts"

    %% Styles for layers
    classDef client fill:#BBDEFB,stroke:#1976D2,color:#0D47A1
    classDef ui fill:#E3F2FD,stroke:#1565C0,color:#0D47A1
    classDef api fill:#F3E5F5,stroke:#8E24AA,color:#4A148C
    classDef service fill:#E8F5E9,stroke:#2E7D32,color:#1B5E20
    classDef db fill:#FFF3E0,stroke:#EF6C00,color:#E65100
    classDef cross fill:#ECEFF1,stroke:#546E7A,color:#263238
```
