# supermemoryai/opensearch-ai architecture

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

- Interactive diagram: https://gitdiagram.com/supermemoryai/opensearch-ai
- Repository: https://github.com/supermemoryai/opensearch-ai

## Overview

##### Project type and purpose
- This is a full-stack AI-powered search application built with Next.js.
- The README describes it as a personalized AI search engine (“OpenSearch GPT”) that remembers user context via Mem0 and powers chat queries with GPT-4-mini.

##### File structure
- Top-level files/configs: package.json, tsconfig.json, next.config.mjs, tailwind.config.ts, postcss.config.js, wrangler.toml (for Cloudflare Pages).
- “app/” directory: Next.js 13+ App Router pages and layouts (page.tsx, layout.tsx, not-found.tsx, chatpage.tsx).
- “app/api/”: serverless API routes for NextAuth (app/api/[...nextauth]/route.ts) and chat (app/api/chat/route.ts).
- “server/”: server-side logic (e.g., auth.ts).
- “components/”: reusable UI pieces (hooks, UI primitives, search-results etc.).
- “lib/”: shared utilities (utils.ts).
- “public/” & “app/assets/”: static assets (fonts, manifest).

##### Insights from README
- Core dependencies: Next.js, Vercel AI ADK, Tailwind CSS with Shadcn UI, Cobe for globe animation, Mem0 for memory retrieval, GPT-4-mini inference.
- Deployment target: Cloudflare Pages via wrangler.toml.

##### Main components
a. Frontend (Next.js App Router)
- UI layers: Layout, Pages, Glossy components (Blobs, Card, Globe).
- Styling: Tailwind + Shadcn UI.
b. API layer (serverless routes)
- Auth: NextAuth route (OAuth, session handling).
- Chat: /api/chat route invoking GPT-4-mini and Mem0 calls.
c. Memory store (Mem0)
- External service handling user memory ingestion & retrieval.
d. AI inference (OpenAI GPT-4-mini)
- External LLM endpoint.
e. Static assets & CDN
- Public assets served via Cloudflare Pages.
f. Deployment & hosting
- Cloudflare Pages + Wrangler for build & deploy.

##### Relationships & data flow
- User interacts with Frontend (browser).
- Frontend fetches data via Fetch/Axios to /api/chat or NextAuth endpoints.
- /api/chat:
1) receives user query
2) calls Mem0 to retrieve context
3) forwards context + query to GPT-4-mini
4) sends AI response back to client
- Authentication flows via NextAuth callbacks in /api/[...nextauth].
- Static files served directly from CDN.

##### Architectural patterns & design principles
- Full-stack serverless pattern (Next.js App Router + API routes).
- Separation of concerns: UI components vs. business logic (lib/utils & server/).
- Extensibility: modular components folder & hooks.
- Cloud-native deployment (Cloudflare Workers / Pages).

##### Final checklist
- Clear, consistent component boundaries
- Labels for protocols, framework names, and file-group origins (e.g., “app/api/chat/route.ts”)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User
    User((User Browser)):::external

    %% Frontend Layer
    subgraph "Frontend (Next.js App Router & UI)"
        direction TB
        Layout["Layout"]:::frontend
        HomePage["Home Page"]:::frontend
        ChatPage["Chat Page"]:::frontend
        NotFound["Not Found Page"]:::frontend
        Blobs["Blobs Component"]:::frontend
        Card["Card Component"]:::frontend
        Globe["Globe Animation"]:::frontend
        SearchResults["Search Results"]:::frontend
        UIPrimitives["UI Primitives"]:::frontend
        Hooks["Custom Hooks"]:::frontend
        GlobalsCSS["Global Styles"]:::frontend
    end

    %% API Layer
    subgraph "API Layer (Serverless Functions)"
        direction TB
        NextAuthRoute["NextAuth Route"]:::api
        ChatRoute["Chat Route"]:::api
    end

    %% Server-side Logic
    subgraph "Server-side Logic"
        direction TB
        AuthHelpers["Authentication Helpers"]:::api
        Utils["Shared Utilities"]:::api
    end

    %% External Services
    subgraph "External AI & Memory Services"
        direction TB
        Mem0["Mem0 Memory Service"]:::external
        OpenAI["GPT-4-mini Service"]:::external
    end

    %% CDN / Static Assets
    subgraph "CDN / Static Assets"
        direction TB
        Fonts["Font File"]:::external
        Manifest["Web Manifest"]:::external
    end

    %% Config & Deployment
    subgraph "Configuration & Deployment"
        direction TB
        NextJSConfig["Next.js Configs"]:::config
        StylingConfig["Styling Configs"]:::config
        DeploymentConfig["Deployment Config"]:::config
    end

    %% Relationships
    User -->|"HTTP/HTTPS"| Layout
    Layout -->|"renders"| HomePage
    Layout -->|"renders"| ChatPage
    Layout -->|"renders"| NotFound
    ChatPage -->|"uses UI"| Blobs
    ChatPage -->|"uses UI"| Card
    ChatPage -->|"uses UI"| Globe
    HomePage -->|"uses UI"| SearchResults
    HomePage -->|"uses hooks"| Hooks
    HomePage -->|"applies styles"| GlobalsCSS

    Layout -->|"fetches static"| Fonts
    Layout -->|"fetches static"| Manifest

    Layout -->|"calls API"| NextAuthRoute
    ChatPage -->|"calls API"| ChatRoute

    NextAuthRoute -->|"invokes"| AuthHelpers
    NextAuthRoute -->|"uses utils"| Utils
    ChatRoute -->|"uses utils"| Utils
    ChatRoute -->|"retrieve memory"| Mem0
    ChatRoute -->|"call LLM"| OpenAI

    %% Config & Deployment Relationships
    DeploymentConfig -->|"builds & deploys"| Layout
    DeploymentConfig -->|"builds & deploys"| NextAuthRoute
    DeploymentConfig -->|"builds & deploys"| ChatRoute

    %% Click Events
    click Layout "https://github.com/supermemoryai/opensearch-ai/blob/main/app/layout.tsx"
    click HomePage "https://github.com/supermemoryai/opensearch-ai/blob/main/app/page.tsx"
    click ChatPage "https://github.com/supermemoryai/opensearch-ai/blob/main/app/chatpage.tsx"
    click NotFound "https://github.com/supermemoryai/opensearch-ai/blob/main/app/not-found.tsx"
    click Blobs "https://github.com/supermemoryai/opensearch-ai/blob/main/app/Blobs.tsx"
    click Card "https://github.com/supermemoryai/opensearch-ai/blob/main/app/Card.tsx"
    click Globe "https://github.com/supermemoryai/opensearch-ai/blob/main/app/Globe.tsx"
    click GlobalsCSS "https://github.com/supermemoryai/opensearch-ai/blob/main/app/globals.css"
    click Fonts "https://github.com/supermemoryai/opensearch-ai/blob/main/app/assets/milling.otf"
    click Manifest "https://github.com/supermemoryai/opensearch-ai/blob/main/public/manifest.webmanifest"
    click SearchResults "https://github.com/supermemoryai/opensearch-ai/blob/main/components/search-results.tsx"
    click UIPrimitives "https://github.com/supermemoryai/opensearch-ai/tree/main/components/ui"
    click Hooks "https://github.com/supermemoryai/opensearch-ai/blob/main/components/hooks/useMediaQuery.tsx"
    click NextAuthRoute "https://github.com/supermemoryai/opensearch-ai/blob/main/app/api/[...nextauth]/route.ts"
    click ChatRoute "https://github.com/supermemoryai/opensearch-ai/blob/main/app/api/chat/route.ts"
    click AuthHelpers "https://github.com/supermemoryai/opensearch-ai/blob/main/server/auth.ts"
    click Utils "https://github.com/supermemoryai/opensearch-ai/blob/main/lib/utils.ts"
    click NextJSConfig "https://github.com/supermemoryai/opensearch-ai/blob/main/next.config.mjs"
    click StylingConfig "https://github.com/supermemoryai/opensearch-ai/blob/main/tailwind.config.ts"
    click DeploymentConfig "https://github.com/supermemoryai/opensearch-ai/blob/main/wrangler.toml"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366D6,stroke-width:1px
    classDef api fill:#E0FFE0,stroke:#34A853,stroke-width:1px
    classDef external fill:#FFE6CC,stroke:#FB8C00,stroke-width:1px
    classDef config fill:#F0E0FF,stroke:#A142F4,stroke-width:1px
    classDef datastore fill:#CCCCCC,stroke:#666666,stroke-width:1px
```
