# senbo1/chat0 architecture

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

- Interactive diagram: https://gitdiagram.com/senbo1/chat0
- Repository: https://github.com/senbo1/chat0

## Overview

##### Project type and purpose
- Chat0 is a full-stack, privacy-first AI chat application built with Next.js (App Router) and React.
- It provides a local browser database (Dexie/IndexedDB) for chat history, and exposes lightweight API routes (`/api/chat`, `/api/completion`) that forward user queries to external AI services (OpenAI, Google Gemini, etc.) using the user’s own API key.

##### File structure
Top-level directories and files:
- app/ – Next.js “App Router” folder containing server-side pages, layouts, global styles, and the two API endpoints.
- frontend/ – Client-side React component library broken down into UI, hooks, routes, and Dexie wrappers.
- lib/ – Shared utility functions and type definitions used both on server and client.
- hooks/ – Miscellaneous React hooks used across the client.
- next.config.ts & tsconfig.json – Next.js and TypeScript configuration.
- public/ – Static assets.
- package.json & pnpm-lock.yaml – Dependency management.

Architectural hints:
- Separation of concerns:
– Server layer: Next.js API routes (in app/api) handle HTTP requests, interact with external AI services.
– Client layer: React UI components (frontend/components), state stores (frontend/stores), local database (frontend/dexie).
- Persistance: Dexie (IndexedDB) for offline chat storage.
- State management: simple stores (APIKeyStore, ModelStore), React context & hooks.

##### README for additional insights
- Privacy-first: all data stays in browser, no server-side database.
- BYO API keys: user provides and stores keys locally.
- Multi-model support & optimized React performance.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Tier
    subgraph "Client (Browser)"
        direction TB
        subgraph "UI Components" 
            direction TB
            ChatPage["Chat Page"]:::frontend
            ChatInput["Chat Input Box"]:::frontend
            ChatNavigator["Chat Navigator"]:::frontend
            ChatSidebar["Chat Sidebar"]:::frontend
            Message["Individual Message"]:::frontend
            MessageControls["Message Controls"]:::frontend
            MessageEditor["Message Editor"]:::frontend
            MessageReasoning["Message Reasoning View"]:::frontend
            MessagesList["Messages List"]:::frontend
            MessageLoading["Streaming Loader"]:::frontend
            MemoizedMarkdown["Markdown Renderer"]:::frontend
            ErrorDisplay["Error Display"]:::frontend
            APIKeyForm["API Key Form"]:::frontend
            KeyPrompt["Key Prompt"]:::frontend
        end
        subgraph "UI Primitives & Theme"
            direction TB
            ThemeProvider["Theme Provider"]:::frontend
            ThemeToggler["Theme Toggler"]:::frontend
            UIPrimitives["UI Primitives"]:::frontend
        end
        subgraph "Client Routes"
            direction TB
            HomeRoute["Home Route"]:::frontend
            IndexRoute["Index Route"]:::frontend
            SettingsRoute["Settings Route"]:::frontend
            ThreadRoute["Thread Route"]:::frontend
        end
        subgraph "State & Persistence"
            direction TB
            APIKeyStore["API Key Store"]:::frontend
            ModelStore["Model Store"]:::frontend
            DexieDB[(Dexie IndexedDB)]:::database
            DexieQueries["IndexedDB Queries"]:::frontend
            useChatNavigator["useChatNavigator Hook"]:::frontend
            useMessageSummary["useMessageSummary Hook"]:::frontend
        end
        subgraph "Global Hooks"
            direction TB
            MobileHook["use-mobile Hook"]:::frontend
            AutoResizeHook["useAutoResizeTextArea Hook"]:::frontend
        end
    end

    %% Server Tier
    subgraph "Server (Next.js App Router)"
        direction TB
        GlobalLayout["Global Layout & Styles"]:::server
        StaticShell["Static Shell Fallback"]:::server
        ChatAPI["/api/chat Route"]:::server
        CompletionAPI["/api/completion Route"]:::server
    end

    %% External Services
    subgraph "External AI Services"
        direction TB
        AIProviders["AI Providers (OpenAI, Gemini)"]:::external
    end

    %% Shared Utilities & Config
    subgraph "Shared & Config"
        direction TB
        Models["Type Definitions & Models"]:::shared
        Utils["Utility Functions"]:::shared
        NextConfig["next.config.ts"]:::shared
        TSConfig["tsconfig.json"]:::shared
        PublicAssets["public/"]:::shared
    end

    %% Data Flows
    ChatPage -->|uses| APIKeyStore
    ChatPage -->|uses| ModelStore
    ChatPage -->|renders| MessagesList
    ChatPage -->|renders| ChatInput
    ChatInput -->|onSend| ChatAPI
    ChatNavigator -->|select thread| useChatNavigator
    ChatSidebar -->|navigate| SettingsRoute
    SettingsRoute -->|updates| APIKeyStore
    APIKeyStore -->|persist/read| DexieDB
    ModelStore -->|persist/read| DexieDB
    DexieQueries -->|queries| DexieDB
    ChatAPI -->|forwards POST chat request| AIProviders
    AIProviders -->|stream tokens| ChatAPI
    ChatAPI -->|streams tokens| ChatInput
    ChatInput -->|append message| DexieDB
    CompletionAPI -->|one-off completion| AIProviders
    CompletionAPI -->|returns| ChatPage

    GlobalLayout -->|wraps| ChatPage
    StaticShell -->|fallback| ChatPage

    %% Click Events
    click ChatPage "https://github.com/senbo1/chat0/blob/main/frontend/components/Chat.tsx"
    click ChatInput "https://github.com/senbo1/chat0/blob/main/frontend/components/ChatInput.tsx"
    click ChatNavigator "https://github.com/senbo1/chat0/blob/main/frontend/components/ChatNavigator.tsx"
    click ChatSidebar "https://github.com/senbo1/chat0/blob/main/frontend/components/ChatSidebar.tsx"
    click Message "https://github.com/senbo1/chat0/blob/main/frontend/components/Message.tsx"
    click MessageControls "https://github.com/senbo1/chat0/blob/main/frontend/components/MessageControls.tsx"
    click MessageEditor "https://github.com/senbo1/chat0/blob/main/frontend/components/MessageEditor.tsx"
    click MessageReasoning "https://github.com/senbo1/chat0/blob/main/frontend/components/MessageReasoning.tsx"
    click MessagesList "https://github.com/senbo1/chat0/blob/main/frontend/components/Messages.tsx"
    click MessageLoading "https://github.com/senbo1/chat0/blob/main/frontend/components/ui/MessageLoading.tsx"
    click MemoizedMarkdown "https://github.com/senbo1/chat0/blob/main/frontend/components/MemoizedMarkdown.tsx"
    click ErrorDisplay "https://github.com/senbo1/chat0/blob/main/frontend/components/Error.tsx"
    click APIKeyForm "https://github.com/senbo1/chat0/blob/main/frontend/components/APIKeyForm.tsx"
    click KeyPrompt "https://github.com/senbo1/chat0/blob/main/frontend/components/KeyPrompt.tsx"
    click ThemeProvider "https://github.com/senbo1/chat0/blob/main/frontend/components/ui/ThemeProvider.tsx"
    click ThemeToggler "https://github.com/senbo1/chat0/blob/main/frontend/components/ui/ThemeToggler.tsx"
    click HomeRoute "https://github.com/senbo1/chat0/blob/main/frontend/routes/Home.tsx"
    click IndexRoute "https://github.com/senbo1/chat0/blob/main/frontend/routes/Index.tsx"
    click SettingsRoute "https://github.com/senbo1/chat0/blob/main/frontend/routes/Settings.tsx"
    click ThreadRoute "https://github.com/senbo1/chat0/blob/main/frontend/routes/Thread.tsx"
    click APIKeyStore "https://github.com/senbo1/chat0/blob/main/frontend/stores/APIKeyStore.ts"
    click ModelStore "https://github.com/senbo1/chat0/blob/main/frontend/stores/ModelStore.ts"
    click DexieDB "https://github.com/senbo1/chat0/blob/main/frontend/dexie/db.ts"
    click DexieQueries "https://github.com/senbo1/chat0/blob/main/frontend/dexie/queries.ts"
    click useChatNavigator "https://github.com/senbo1/chat0/blob/main/frontend/hooks/useChatNavigator.ts"
    click useMessageSummary "https://github.com/senbo1/chat0/blob/main/frontend/hooks/useMessageSummary.ts"
    click MobileHook "https://github.com/senbo1/chat0/blob/main/hooks/use-mobile.ts"
    click AutoResizeHook "https://github.com/senbo1/chat0/blob/main/hooks/useAutoResizeTextArea.ts"
    click GlobalLayout "https://github.com/senbo1/chat0/blob/main/app/layout.tsx"
    click StaticShell "https://github.com/senbo1/chat0/blob/main/app/static-app-shell/page.tsx"
    click ChatAPI "https://github.com/senbo1/chat0/blob/main/app/api/chat/route.ts"
    click CompletionAPI "https://github.com/senbo1/chat0/blob/main/app/api/completion/route.ts"
    click Models "https://github.com/senbo1/chat0/blob/main/lib/models.ts"
    click Utils "https://github.com/senbo1/chat0/blob/main/lib/utils.ts"

    %% Styles
    classDef frontend fill:#E3F2FD,stroke:#2196F3,stroke-width:1px
    classDef server fill:#E8F5E9,stroke:#4CAF50,stroke-width:1px
    classDef external fill:#FFF3E0,stroke:#FB8C00,stroke-width:1px
    classDef database fill:#F3E5F5,stroke:#7E57C2,stroke-width:1px,shape:cylinder
    classDef shared fill:#ECEFF1,stroke:#607D8B,stroke-width:1px
```
