# sreenivasyadav374/streaming-chatbot architecture

> Architecture diagram of the GitHub repository sreenivasyadav374/streaming-chatbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sreenivasyadav374/streaming-chatbot
- Repository: https://github.com/sreenivasyadav374/streaming-chatbot

## Overview

1. **Overall shape**
   This is a Next.js 14 App Router application with three main concerns: authenticated UI routing, a streaming chat backend, and a small data layer for personas/auth state. The UI is heavily componentized and most of the “product logic” lives in the chat feature and API routes.

2. **Route structure**
   The app is split into route groups:
   - `app/(auth)` for login-related pages
   - `app/(dashboard)` for the post-login product shell and chat pages
   - `app/api/*` for server endpoints
   This separation is important because it creates a clear boundary between unauthenticated access, authenticated workspace UI, and server-side AI/data operations.

3. **Authentication boundary**
   Supabase is the auth provider and session store. The presence of `lib/supabase/client.ts`, `lib/supabase/server.ts`, `middleware.ts`, and `app/api/auth/callback/route.ts` suggests a standard pattern:
   - browser uses the client Supabase helper
   - server routes and middleware use the server Supabase helper
   - middleware likely protects dashboard/chat routes
   - the auth callback finalizes login/session handoff

4. **Main user experience: chat workspace**
   The dashboard chat experience is centered in `components/chat/*` and `app/(dashboard)/chat/[chatId]/page.tsx`. The main UI pieces are:
   - `ChatWindow` as the orchestrator
   - `MessageList`, `MessageBubble`, `ChatInput`
   - `ChatHeader`, `SidebarChatList`, `NewChatButton`
   - supporting states like `EmptyState`, `TypingIndicator`, and `WorkspaceLoader`
   This is the primary interactive subsystem and likely owns local chat state and user interactions.

5. **Streaming message flow**
   The core data flow is browser → `/api/chat` → LLM provider → streamed response back to the UI. The README describes token-by-token streaming over SSE, and the repo includes `app/api/chat/route.ts` plus `lib/openai.ts` and `lib/gemini.ts` as server-side model helpers. The frontend likely consumes the stream incrementally, showing typing/loading state until completion.

6. **Chat API surface**
   There are three chat-related API routes:
   - `app/api/chat/route.ts` for sending messages and streaming responses
   - `app/api/chat/update-title/route.ts` for renaming conversations after generation
   - `app/api/auth/callback/route.ts` for auth session completion
   Supporting files `app/api/chat/schema.ts` and `app/api/chat/tools.ts` imply request validation and optional tool/function support in the chat backend.

7. **Persona system**
   Personas are a distinct feature, not just a UI toggle. The repo has:
   - `lib/personas.ts`
   - `components/PersonaSelector.tsx`
   - `components/CustomPersonaDialog.tsx`
   - Supabase migrations creating and updating a `personas` table
   This means chat behavior can be shaped by predefined or user-defined persona prompts, with persistence in Supabase and selection in the UI.

8. **State management and chat UX**
   `hooks/useChatWindow.tsx` appears to centralize chat lifecycle state: message history, streaming state, connection status, and possibly optimistic updates. This is backed by UI indicators like `ConnectionStatus`, `TypingIndicator`, and the chat-specific loading skeleton. The architecture favors a single hook/controller model rather than distributed state across many components.

9. **Types and message schema**
   Shared domain types live in `types/chat.ts` and `types/persona.ts`. These likely define the message, conversation, persona, and API payload shapes used by both the frontend and route handlers, which helps keep the stream protocol and persisted data consistent.

10. **UI/component system**
   The app uses a shadcn/ui-style component library under `components/ui/*`, plus custom visual components for messages, cards, and chat shell layout. This is a presentation layer boundary: reusable primitives are separated from feature-specific chat and auth components.

11. **Persistence and database layer**
   Supabase migrations under `supabase/migrations` show that the project stores application data beyond auth, specifically personas and persona prompt updates. That suggests the database is used for lightweight product state rather than full chat transcript persistence, at least from the visible tree.

12. **Deployment/runtime assumptions**
   The presence of `next.config.js`, `netlify.toml`, and `middleware.ts` indicates the app is designed for server-rendered deployment with edge/server route handling, likely on Netlify. Environment configuration is server-side via `.env.local`, with the AI provider key kept off the client.

13. **External AI integration**
   The README says OpenRouter is the primary AI backend, while the code tree includes provider helpers named `openai.ts` and `gemini.ts`. Architecturally, the important point is that model access is abstracted behind server-only modules, so the frontend never talks directly to the model provider and the provider can be swapped or extended.

14. **How to draw the diagram**
   A correct architecture diagram should show:
   - authenticated browser UI on the left
   - a chat controller/state layer in the middle
   - Next.js API routes and middleware as the server boundary
   - Supabase as auth + persona persistence
   - OpenRouter/LLM provider as the streaming inference dependency
   - the chat stream returning incrementally to the UI, with title updates and persona selection as secondary flows

Main entry points: [`app/api/chat/route.ts`](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/api/chat/route.ts) (Chat API), [`components/chat/ChatWindow.tsx`](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/components/chat/ChatWindow.tsx) (Chat workspace), [`lib/supabase/server.ts`](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/lib/supabase/server.ts) (Supabase server), [`hooks/useChatWindow.tsx`](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/hooks/useChatWindow.tsx) (Chat state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Browser UI"]
  node_auth_pages["Auth pages<br/>Next.js route group"]
  node_dashboard_pages["Dashboard pages<br/>Next.js route group"]
  node_chat_workspace["Chat workspace<br/>feature shell<br/>[ChatWindow.tsx]"]
  node_chat_controls["Chat controls<br/>chat UI<br/>[ChatInput.tsx]"]
  node_message_rendering["Message rendering<br/>chat view<br/>[MessageList.tsx]"]
  node_persona_ui["Persona UI<br/>persona picker"]
  node_chat_state["Chat state<br/>hook/controller<br/>[useChatWindow.tsx]"]
  node_app_shell["App shell<br/>Next.js layout<br/>[layout.tsx]"]
end

subgraph group_server["Server Layer"]
  node_middleware["Auth middleware<br/>route guard<br/>[middleware.ts]"]
  node_auth_callback["Auth callback<br/>API route<br/>[route.ts]"]
  node_chat_api["Chat API<br/>API route<br/>[route.ts]"]
  node_title_api["Title update<br/>API route<br/>[route.ts]"]
  node_chat_schema_tools["Chat schema<br/>request contract<br/>[schema.ts]"]
  node_ai_backends["AI backends<br/>LLM adapters<br/>[openai.ts]"]
end

subgraph group_data["Data Layer"]
  node_supabase_client["Supabase client<br/>browser auth helper<br/>[client.ts]"]
  node_supabase_server["Supabase server<br/>server auth helper<br/>[server.ts]"]
  node_personas["Personas<br/>persona model<br/>[personas.ts]"]
  node_types["Domain types<br/>shared contracts<br/>[chat.ts]"]
end

node_app_shell -->|"frames"| node_auth_pages
node_app_shell -->|"frames"| node_dashboard_pages
node_dashboard_pages -->|"renders"| node_chat_workspace
node_chat_workspace -->|"drives"| node_chat_state
node_chat_workspace -->|"composes"| node_chat_controls
node_chat_workspace -->|"composes"| node_message_rendering
node_chat_workspace -->|"selects"| node_persona_ui
node_chat_state -->|"streams to"| node_chat_api
node_chat_api -->|"validates"| node_chat_schema_tools
node_chat_api -->|"invokes"| node_ai_backends
node_chat_api -->|"shares contracts"| node_types
node_chat_state -->|"shares contracts"| node_types
node_chat_api -->|"updates"| node_title_api
node_middleware -->|"reads session"| node_supabase_server
node_auth_callback -->|"finalizes session"| node_supabase_server
node_supabase_client -->|"signs in"| node_auth_pages
node_personas -->|"powers"| node_persona_ui
node_personas -->|"shapes prompts"| node_chat_api
node_supabase_server -->|"persists"| node_personas
node_supabase_client -.->|"shares session"| node_supabase_server

click node_auth_pages "https://github.com/sreenivasyadav374/streaming-chatbot/tree/main/app/(auth)"
click node_dashboard_pages "https://github.com/sreenivasyadav374/streaming-chatbot/tree/main/app/(dashboard)"
click node_chat_workspace "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/components/chat/ChatWindow.tsx"
click node_chat_controls "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/components/chat/ChatInput.tsx"
click node_message_rendering "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/components/chat/MessageList.tsx"
click node_persona_ui "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/components/PersonaSelector.tsx"
click node_chat_state "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/hooks/useChatWindow.tsx"
click node_app_shell "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/layout.tsx"
click node_middleware "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/middleware.ts"
click node_auth_callback "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/api/auth/callback/route.ts"
click node_chat_api "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/api/chat/route.ts"
click node_title_api "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/api/chat/update-title/route.ts"
click node_chat_schema_tools "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/api/chat/schema.ts"
click node_ai_backends "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/lib/openai.ts"
click node_supabase_client "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/lib/supabase/client.ts"
click node_supabase_server "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/lib/supabase/server.ts"
click node_personas "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/lib/personas.ts"
click node_types "https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/types/chat.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_auth_pages,node_dashboard_pages,node_chat_workspace,node_chat_controls,node_message_rendering,node_persona_ui,node_chat_state,node_app_shell toneBlue
class node_middleware,node_auth_callback,node_chat_api,node_title_api,node_chat_schema_tools,node_ai_backends toneAmber
class node_supabase_client,node_supabase_server,node_personas,node_types toneMint
```

## Components

### Browser UI

Authenticated and unauthenticated Next.js pages plus reusable chat/persona components.

- [Auth pages](https://github.com/sreenivasyadav374/streaming-chatbot/tree/main/app/(auth)): Next.js route group · Login and auth shell under the app/(auth) route group. · `app/(auth)`
- [Dashboard pages](https://github.com/sreenivasyadav374/streaming-chatbot/tree/main/app/(dashboard)): Next.js route group · Post-login workspace shell and chat page under app/(dashboard). · `app/(dashboard)`
- [Chat workspace](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/components/chat/ChatWindow.tsx): feature shell · Main chat UI orchestrator for the conversation experience. · `components/chat/ChatWindow.tsx`
- [Chat controls](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/components/chat/ChatInput.tsx): chat UI · Header, input, sidebar, new chat, and loading/empty states. · `components/chat/ChatInput.tsx`
- [Message rendering](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/components/chat/MessageList.tsx): chat view · Message list, bubbles, markdown, and part rendering. · `components/chat/MessageList.tsx`
- [Persona UI](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/components/PersonaSelector.tsx): persona picker · Persona selection and custom persona dialog in the browser. · `components/PersonaSelector.tsx`
- [Chat state](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/hooks/useChatWindow.tsx): hook/controller · Centralized chat lifecycle state, streaming status, and optimistic updates. · `hooks/useChatWindow.tsx`
- [App shell](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/layout.tsx): Next.js layout · Root and route-group layouts that frame the authenticated and auth experiences. · `app/layout.tsx`

### Server Layer

Route handlers, middleware, and server-only integrations.

- [Auth middleware](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/middleware.ts): route guard · Protects dashboard routes and coordinates session-aware access. · `middleware.ts`
- [Auth callback](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/api/auth/callback/route.ts): API route · Completes Supabase login/session handoff after authentication. · `app/api/auth/callback/route.ts`
- [Chat API](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/api/chat/route.ts): API route · Streams model responses for chat requests. · `app/api/chat/route.ts`
- [Title update](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/api/chat/update-title/route.ts): API route · Renames conversations after generation or follow-up actions. · `app/api/chat/update-title/route.ts`
- [Chat schema](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/app/api/chat/schema.ts): request contract · Validates chat payloads and defines optional tool support. · `app/api/chat/schema.ts`
- [AI backends](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/lib/openai.ts): LLM adapters · Server-only provider helpers for model access and provider abstraction. · `lib/openai.ts`

### Data Layer

Supabase auth/session and persona persistence plus shared domain types.

- [Supabase client](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/lib/supabase/client.ts): browser auth helper · Client-side Supabase session and auth helper. · `lib/supabase/client.ts`
- [Supabase server](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/lib/supabase/server.ts): server auth helper · Server-side Supabase session helper used by middleware and routes. · `lib/supabase/server.ts`
- [Personas](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/lib/personas.ts): persona model · Persona definitions and prompt shaping logic shared by UI and server. · `lib/personas.ts`
- [Domain types](https://github.com/sreenivasyadav374/streaming-chatbot/blob/main/types/chat.ts): shared contracts · Shared chat and persona types used across UI and API handlers. · `types/chat.ts`

## Connections

- App shell → Auth pages: frames · Layouts wrap the auth route group.
- App shell → Dashboard pages: frames · Layouts wrap the dashboard route group.
- Dashboard pages → Chat workspace: renders · The chat page mounts the main workspace.
- Chat workspace → Chat state: drives · Workspace state is centralized in the chat hook/controller.
- Chat workspace → Chat controls: composes · The workspace assembles header, input, sidebar, and empty/loading states.
- Chat workspace → Message rendering: composes · Messages are rendered through list and bubble components.
- Chat workspace → Persona UI: selects · Persona selection influences the active conversation context.
- Chat state → Chat API: streams to · Chat state sends messages and consumes streamed responses.
- Chat API → Chat schema: validates · Request payloads and tool usage are validated before execution.
- Chat API → AI backends: invokes · The API routes model requests through server-only adapters.
- Chat API → Domain types: shares contracts · API and UI share message and persona payload shapes.
- Chat state → Domain types: shares contracts · The chat hook uses the same domain types as the API.
- Chat API → Title update: updates · Conversation metadata can be renamed after generation.
- Auth middleware → Supabase server: reads session · Route protection relies on server-side Supabase auth state.
- Auth callback → Supabase server: finalizes session · Callback completes login and session handoff on the server.
- Supabase client → Auth pages: signs in · Browser auth flow uses the client-side Supabase helper.
- Personas → Persona UI: powers · Persona definitions populate the selector and custom dialog.
- Personas → Chat API: shapes prompts · Selected persona context influences server-side chat generation.
- Supabase server → Personas: persists · Server-side Supabase access stores and reads persona records.
- Supabase client → Supabase server: shares session · Client and server helpers coordinate the same Supabase session.
