How sreenivasyadav374/streaming-chatbot works
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: route.ts Chat API · ChatWindow.tsx Chat workspace · server.ts Supabase server · useChatWindow.tsx Chat state
Components 18
Browser UI
Authenticated and unauthenticated Next.js pages plus reusable chat/persona components.
- Auth pages · Login and auth shell under the app/(auth) route group.
app/(auth) - Dashboard pages · Post-login workspace shell and chat page under app/(dashboard).
app/(dashboard) - Chat workspace · Main chat UI orchestrator for the conversation experience.
components/chat/ChatWindow.tsx - Chat controls · Header, input, sidebar, new chat, and loading/empty states.
components/chat/ChatInput.tsx - Message rendering · Message list, bubbles, markdown, and part rendering.
components/chat/MessageList.tsx - Persona UI · Persona selection and custom persona dialog in the browser.
components/PersonaSelector.tsx - Chat state · Centralized chat lifecycle state, streaming status, and optimistic updates.
hooks/useChatWindow.tsx - App shell · 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 · Protects dashboard routes and coordinates session-aware access.
middleware.ts - Auth callback · Completes Supabase login/session handoff after authentication.
app/api/auth/callback/route.ts - Chat API · Streams model responses for chat requests.
app/api/chat/route.ts - Title update · Renames conversations after generation or follow-up actions.
app/api/chat/update-title/route.ts - Chat schema · Validates chat payloads and defines optional tool support.
app/api/chat/schema.ts - AI backends · 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 · Client-side Supabase session and auth helper.
lib/supabase/client.ts - Supabase server · Server-side Supabase session helper used by middleware and routes.
lib/supabase/server.ts - Personas · Persona definitions and prompt shaping logic shared by UI and server.
lib/personas.ts - Domain types · Shared chat and persona types used across UI and API handlers.
types/chat.ts
Connections 20
- 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.