# supabase-community/nextjs-openai-doc-search architecture

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

- Interactive diagram: https://gitdiagram.com/supabase-community/nextjs-openai-doc-search
- Repository: https://github.com/supabase-community/nextjs-openai-doc-search

## Overview

##### Process Flows

A) Build-time Flow (Left Side):
**MDX Files → Document Processor → OpenAI Embeddings → Supabase Storage**

B) Runtime Flow (Right Side):
**User Query → Vector Search → Content Retrieval → OpenAI Completion → Client Stream**

##### Component Details

- SearchDialog
- UI Components
- Pages

- Hexagons (Green): API Services
- OpenAI API
- Vector Search API

- Supabase Database
- pgvector Extension

- Diamonds (Purple): Processing Units
- Embedding Generator
- Document Processor

##### Key Elements to Highlight

a) Frontend Architecture:
- Next.js Pages Structure
- Search Dialog Component
- UI Components Library

b) API Layer:
- Vector Search Endpoint
- OpenAI Integration
- SSE Implementation

c) Database Layer:
- pgvector Extension
- Embeddings Storage
- Document Sections Storage

##### Technical Stack Labels

Clearly label technologies:
- Next.js
- TypeScript
- Supabase
- OpenAI
- pgvector
- TailwindCSS

##### Build-time Processing
How MDX files are processed, embeddings are generated, and stored in the database.

##### Runtime Search
Flow from user query to streamed response, including vector similarity search and OpenAI completion.

Remember to:
- Version information
- Document all external service dependencies
- Error handling paths
- Scaling considerations

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        SD["Search Dialog"]:::frontend
        UI["UI Components"]:::frontend
        Pages["Frontend Pages"]:::frontend
        SSE["SSE Handler"]:::frontend
    end

    subgraph Server
        API["Vector Search API"]:::api
        DP["Document Processing"]:::process
        EH["Error Handler"]:::process
        Utils["Utility Functions"]:::process
    end

    subgraph Infrastructure
        DB[(Supabase DB)]:::database
        PGV[(pgvector)]:::database
        OAI["OpenAI API"]:::external
    end

    subgraph "Build-time Flow"
        MDX["MDX Files"]:::file
        DC["Database Config"]:::config
        BP["Build Pipeline"]:::process
    end

    %% Client Layer Connections
    SD --> API
    API --> SSE
    UI --> SD
    Pages --> UI

    %% Server Layer Connections
    API --> DB
    API --> OAI
    DP --> DB
    DP --> OAI
    API -.-> EH
    API --> Utils

    %% Infrastructure Layer Connections
    DB --> PGV
    
    %% Build-time Connections
    MDX -.-> DP
    DC -.-> DB
    BP -.-> DP

    %% Styling
    classDef frontend fill:#a7c7e7
    classDef api fill:#95d5b2
    classDef database fill:#f4a261
    classDef process fill:#b298dc
    classDef external fill:#ff9999
    classDef file fill:#ddd
    classDef config fill:#ffd700

    %% Click Events
    click SD "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/components/SearchDialog.tsx"
    click UI "https://github.com/supabase-community/nextjs-openai-doc-search/tree/main/components/ui/"
    click API "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/pages/api/vector-search.ts"
    click DP "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/lib/generate-embeddings.ts"
    click SSE "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/types/sse.d.ts"
    click EH "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/lib/errors.ts"
    click Utils "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/lib/utils.ts"
    click Pages "https://github.com/supabase-community/nextjs-openai-doc-search/tree/main/pages/"
    click MDX "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/pages/docs/openai_embeddings.mdx"
    click DC "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/supabase/config.toml"
    click BP "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/.github/workflows/build.yml"
    click DB "https://github.com/supabase-community/nextjs-openai-doc-search/blob/main/supabase/migrations/20230406025118_init.sql"
```
