# discovai/discovai-search architecture

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

- Interactive diagram: https://gitdiagram.com/discovai/discovai-search
- Repository: https://github.com/discovai/discovai-search

## Overview

- Frontend (Next.js App):
– Pages, layouts, and various UI components in the src/app and src/components folders.
– This layer handles presentation, user interactions (e.g., search queries, navigation), and state management (stores and hooks exist in src/stores and src/hooks).
– Also includes tailored UI components from libraries like shadcn/ui and Radix UI with styling via Tailwind CSS.

- API / Backend Services:
– API endpoints under src/app/api (e.g., api/chat, api/join-wait) serve as the bridge between the frontend and backend logic.
– These API routes manage chat functionalities, waiting list processing, and perhaps other dynamic responses.

- Data Layer & External Services:
– Supabase and the SQL scripts (src/db/init.sql) indicate that data (like the AI tool database and user information) is housed in a Postgres/Supabase setup.
– Pgvector is used for vector similarity operations.
– Redis (through Upstash integration as seen in src/db/redis.ts and environment variables) handles caching of search results and LLM outputs.
– Integration with third-party services for LLM (gpt-4o-mini/OpenAI APIs) and embeddings (via Jina AI) that are orchestrated through modules in src/lib (e.g., src/lib/chat/embedding.ts, src/lib/chat/llm.ts).

##### Relationships and interactions
- Calls going from API endpoints to the database layer (Supabase) for fetching AI tool data.
- API interactions with Redis (using Upstash) for caching responses and quick lookups.
- Requests to external services: one path for embedding conversion (Jina AI) and one path for LLM responses (OpenAI’s gpt-4o-mini).

##### Underlying architectural patterns/design principles
- Separation of concerns – the clear separation between the presentational (frontend UI), API (backend logic), and data (database and caching) layers.
- Modular design – the codebase splits logic into distinct modules (e.g., chat-related functions, database connectors, UI components).

## Diagram (Mermaid)

```mermaid
flowchart TD
    User["User/Browser"]:::user

    subgraph "Frontend (Next.js)"
        FP["Pages & Layouts"]:::frontend
        UI["UI Components"]:::frontend
        SM["State Management"]:::frontend
        HK["Hooks"]:::frontend
        UI1["Ask Input Component"]:::frontend
        UI2["Chat Panel Component"]:::frontend
        UI3["Landing Hero Component"]:::frontend
        SM1["Message Slice (Store)"]:::frontend
    end

    subgraph "API / Backend Services"
        ChatAPI["Chat API Endpoint"]:::backend
        WaitAPI["Waitlist API Endpoint"]:::backend
    end

    subgraph "Data & External Services"
        Supa["Supabase/PostgreSQL"]:::database
        SQL["SQL Init Script"]:::database
        Red["Upstash Redis Cache"]:::database
        Embd["Embedding Service (Jina AI)"]:::external
        LLMs["LLM Service (OpenAI/gpt-4o-mini)"]:::external
        CU["Chat Prompts & Utils"]:::external
    end

    %% User interactions with Frontend
    User --> FP
    User --> UI
    User --> SM
    User --> HK

    %% Frontend internal details
    FP --- UI
    UI --> UI1
    UI --> UI2
    UI --> UI3
    SM --> SM1

    %% Frontend calling API endpoints
    FP -->|"calls"| ChatAPI
    FP -->|"calls"| WaitAPI

    %% API endpoints interacting with Data & External Services
    ChatAPI -->|"fetches"| Supa
    ChatAPI -->|"caches"| Red
    ChatAPI -->|"embeds"| Embd
    ChatAPI -->|"LLMcalls"| LLMs
    ChatAPI -->|"prompts"| CU

    WaitAPI -->|"updates"| Supa
    WaitAPI -->|"caches"| Red

    %% API responses back to Frontend
    ChatAPI -->|"responds"| FP
    WaitAPI -->|"responds"| FP

    %% Database initialization link
    Supa ---|"initialized_by"| SQL

    %% Click Events for Frontend Components
    click FP "https://github.com/discovai/discovai-search/blob/main/src/app/page.tsx"
    click UI "https://github.com/discovai/discovai-search/tree/main/src/components"
    click SM "https://github.com/discovai/discovai-search/blob/main/src/stores/index.ts"
    click HK "https://github.com/discovai/discovai-search/blob/main/src/hooks/chat.ts"
    click UI1 "https://github.com/discovai/discovai-search/blob/main/src/components/ask-input.tsx"
    click UI2 "https://github.com/discovai/discovai-search/blob/main/src/components/chat-panel.tsx"
    click UI3 "https://github.com/discovai/discovai-search/blob/main/src/components/landing/hero.tsx"
    click SM1 "https://github.com/discovai/discovai-search/blob/main/src/stores/slices/messageSlice.ts"

    %% Click Events for API Endpoints
    click ChatAPI "https://github.com/discovai/discovai-search/blob/main/src/app/api/chat/route.ts"
    click WaitAPI "https://github.com/discovai/discovai-search/blob/main/src/app/api/join-wait/route.ts"

    %% Click Events for Data & External Services
    click Supa "https://github.com/discovai/discovai-search/blob/main/src/db/supabase.ts"
    click SQL "https://github.com/discovai/discovai-search/blob/main/src/db/init.sql"
    click Red "https://github.com/discovai/discovai-search/blob/main/src/db/redis.ts"
    click Embd "https://github.com/discovai/discovai-search/blob/main/src/lib/chat/embedding.ts"
    click LLMs "https://github.com/discovai/discovai-search/blob/main/src/lib/chat/llm.ts"
    click CU "https://github.com/discovai/discovai-search/blob/main/src/lib/chat/prompts.ts"

    %% Style Definitions
    classDef user fill:#f9f,stroke:#333,stroke-width:2px;
    classDef frontend fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef backend fill:#d1e7dd,stroke:#333,stroke-width:2px;
    classDef database fill:#ffe5b4,stroke:#333,stroke-width:2px;
    classDef external fill:#f8d7da,stroke:#333,stroke-width:2px;
```
