# kevin-khoamai/chat-affiliate-hub architecture

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

- Interactive diagram: https://gitdiagram.com/kevin-khoamai/chat-affiliate-hub
- Repository: https://github.com/kevin-khoamai/chat-affiliate-hub

## Overview

##### Project Type & Purpose
- This is a full-stack affiliate-marketing Chat DApp with real-time group/1:1 messaging and an AI assistant powered by Retrieval-Augmented Generation (RAG).
- Frontend: React 18 + TypeScript, Vite, Tailwind CSS, Shadcn/ui.
- Backend: Supabase (Auth, PostgreSQL + pgvector, Edge Functions) implementing message storage, RLS, and RAG pipeline.
- External integrations: OpenAI embeddings API, n8n webhook service, and a future Ethereum integration via Wagmi/Hardhat/IPFS.

##### File Structure Analysis
- src/
– components/: UI building blocks (chat interfaces, AuthModal, RAG chat).
– integrations/supabase: supabase client wrapper & types.
– lib/: blockchain stubs, crypto utils, supabase utilities.
– services/: RAG services (embedding, LLM, vector store), n8n webhook.
– hooks/: mobile detection, toast notifications.
– pages/: entry points (Index, NotFound).
– main.tsx, App.tsx: application bootstrap.
- public/: static assets (robots.txt, index.html).
- supabase/:
– config.toml, migrations/: schema for users/messages/campaigns/academy.
– functions/: serverless endpoints (chat-rag, message-processor).
- Config files: tailwind.config.ts, vite.config.ts, tsconfig*.json, eslint.config.js.

##### README Highlights
- Real-time chat with Supabase subscriptions.
- RAG chat: mock now, production via pgvector + OpenAI embeddings.
- Auth: Email + Google OAuth.
- Database: PostgreSQL with RLS policies on messages table.
- Edge Functions placeholders for send-message, query-campaign, query-academy.
- Future blockchain module: smart contract for message hashes, Wagmi wallet, Hardhat tooling.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Frontend Layer
    subgraph "Frontend (Browser)" 
        direction TB
        ReactApp["React App"]:::frontend
        UIComponents["UI Components"]:::frontend
        AuthModal["AuthModal"]:::frontend
        ChatInterface["ChatInterface"]:::frontend
        ChatbotInterface["ChatbotInterface"]:::frontend
        RAGChatInterface["RAGChatInterface"]:::frontend
        OnlineUsers["OnlineUsers"]:::frontend
        UserProfile["UserProfile"]:::frontend
        BlockchainPlaceholder["BlockchainPlaceholder"]:::frontend
        UIBuildingBlocks["Shadcn/UI Building Blocks"]:::frontend
        Pages["Pages/Routes"]:::frontend
        Hooks["Hooks"]:::frontend
        StaticAssets["Static Assets"]:::frontend
    end

    %% Supabase Layer
    subgraph "Supabase Managed Services" 
        direction TB
        SupabaseAuthRealtime["Auth & Realtime"]:::backend
        SupabaseDB["PostgreSQL + pgvector"]:::database
        subgraph "Edge Functions" 
            direction TB
            EdgeMessageProcessor["Edge Fn: message-processor"]:::backend
            EdgeChatRag["Edge Fn: chat-rag"]:::backend
        end
        SupabaseConfig["Config & Migrations"]:::backend
    end

    %% RAG Microservices
    subgraph "RAG Services Pipeline"
        direction TB
        EmbeddingService["Embedding Service"]:::backend
        VectorStoreService["Vector Store Service"]:::backend
        LLMService["LLM Service"]:::backend
        OrchestrationService["Orchestration Service"]:::backend
    end

    %% External APIs
    subgraph "External APIs & Services"
        direction TB
        OpenAIEmbeddings["OpenAI Embeddings API"]:::external
        OpenAILLM["OpenAI LLM API"]:::external
        N8nWebhook["n8n Webhook Service"]:::external
    end

    %% Future Blockchain Module
    subgraph "Future Blockchain (Optional)" 
        direction TB
        class FutureBox future
        EthereumNetwork["Ethereum Network"]:::future
        IPFS["IPFS Storage"]:::future
        WalletConnector["Wagmi Wallet Connector"]:::future
    end

    %% Relationships
    ReactApp -->|login/jwt| SupabaseAuthRealtime
    ReactApp -->|WS subscribe| SupabaseAuthRealtime
    ReactApp -->|call| EdgeMessageProcessor
    EdgeMessageProcessor -->|write encrypted| SupabaseDB
    EdgeMessageProcessor -->|use crypto-lib| CryptoUtils
    ReactApp -->|call| EdgeChatRag
    EdgeChatRag -->|invoke| OrchestrationService
    OrchestrationService -->|embed| EmbeddingService
    EmbeddingService -->|store vector| SupabaseDB
    OrchestrationService -->|query vectors| VectorStoreService
    OrchestrationService -->|call LLM| OpenAILLM
    OpenAILLM -->|response| OrchestrationService
    OrchestrationService -->|return| EdgeChatRag
    EdgeChatRag -->|response| ReactApp
    ReactApp -->|optional tx| WalletConnector
    WalletConnector -->|store hash| EthereumNetwork
    EthereumNetwork -->|ipfs hash| IPFS
    ReactApp -->|display| StaticAssets
    ReactApp -->|use| Hooks
    ReactApp -->|render| UIComponents
    ReactApp -->|route| Pages
    ReactApp -->|use| UIBuildingBlocks
    EdgeMessageProcessor -->|use| SupabaseConfig
    SupabaseDB -. RLS .-> SupabaseAuthRealtime
    ReactApp -->|webhook| N8nWebhook
    EdgeChatRag -->|log events| N8nWebhook

    %% Utility Nodes
    CryptoUtils["Crypto Utilities"]:::backend
    SupabaseClient["Supabase Client Wrapper"]:::backend
    Utils["General Utilities"]:::backend
    BlockchainStubs["Blockchain Stubs"]:::backend

    %% Click Events
    click ReactApp "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/main.tsx"
    click ReactApp "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/App.tsx"
    click UIComponents "https://github.com/kevin-khoamai/chat-affiliate-hub/tree/main/src/components/"
    click AuthModal "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/components/AuthModal.tsx"
    click ChatInterface "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/components/ChatInterface.tsx"
    click ChatbotInterface "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/components/ChatbotInterface.tsx"
    click RAGChatInterface "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/components/ragChatInterface.tsx"
    click OnlineUsers "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/components/OnlineUsers.tsx"
    click UserProfile "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/components/UserProfile.tsx"
    click BlockchainPlaceholder "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/components/BlockchainPlaceholder.tsx"
    click UIBuildingBlocks "https://github.com/kevin-khoamai/chat-affiliate-hub/tree/main/src/components/ui/"
    click Pages "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/pages/Index.tsx"
    click Pages "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/pages/NotFound.tsx"
    click Hooks "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/hooks/use-mobile.tsx"
    click Hooks "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/hooks/use-toast.ts"
    click SupabaseClient "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/integrations/supabase/client.ts"
    click Utils "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/lib/utils.ts"
    click CryptoUtils "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/lib/crypto.ts"
    click BlockchainStubs "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/lib/blockchain.ts"
    click EmbeddingService "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/services/ragEmbedding.ts"
    click VectorStoreService "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/services/ragVectorStore.ts"
    click LLMService "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/services/ragLLM.ts"
    click OrchestrationService "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/services/ragService.ts"
    click N8nWebhook "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/services/n8nWebhookService.ts"
    click StaticAssets "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/public/index.html"
    click StaticAssets "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/public/robots.txt"
    click EdgeChatRag "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/supabase/functions/chat-rag/index.ts"
    click EdgeMessageProcessor "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/supabase/functions/message-processor/index.ts"
    click SupabaseConfig "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/supabase/config.toml"
    click SupabaseConfig "https://github.com/kevin-khoamai/chat-affiliate-hub/tree/main/supabase/migrations/"
    click SupabaseClient "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/integrations/supabase/types.ts"
    click BlockchainStubs "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/lib/blockchain.ts"
    click ReactApp "https://github.com/kevin-khoamai/chat-affiliate-hub/blob/main/src/index.css"
    click UIBuildingBlocks "https://github.com/kevin-khoamai/chat-affiliate-hub/tree/main/src/components/ui/"

    %% Styles
    classDef frontend fill:#cfe2ff,stroke:#339af0,stroke-width:1px
    classDef backend fill:#d4f5d4,stroke:#2f9e2e,stroke-width:1px
    classDef database fill:#e5d4f5,stroke:#6f42c1,stroke-width:1px
    classDef external fill:#e2e2e2,stroke:#6c757d,stroke-width:1px
    classDef future fill:#ffe5d4,stroke:#fd7e14,stroke-width:2px,stroke-dasharray: 5 5
```
