# ayushgupta2408/brain architecture

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

- Interactive diagram: https://gitdiagram.com/ayushgupta2408/brain
- Repository: https://github.com/ayushgupta2408/brain

## Overview

#### Purpose and entry points
Brain is a MERN multi-agent AI chat platform. It accepts authenticated user prompts and runs them through a staged AI pipeline, streaming progress and results back to the client. Backend startup and HTTP composition are in `backend/server.js`; the React/Vite client starts from `frontend/src/main.jsx` and `frontend/src/App.jsx`.

#### Client application
The frontend provides authentication, chat interaction, and live pipeline visibility. `frontend/src/pages/ChatPage.jsx` composes the main chat experience; `frontend/src/components/ChatWindow.jsx` handles conversation display/input, while `frontend/src/components/AgentPipeline.jsx` renders stage-by-stage status from server-sent events. Redux state separates auth and chat concerns in `frontend/src/store/authSlice.js` and `frontend/src/store/chatSlice.js`.

#### API, identity, and persistence boundary
Express routes expose registration/login and chat endpoints through `backend/src/routes/authRoutes.js` and `backend/src/routes/chatRoutes.js`. JWT validation is centralized in `backend/src/middleware/auth.js`, protecting chat operations. MongoDB persists user accounts and chat history through `backend/src/models/User.js` and `backend/src/models/Chat.js`, with connection setup in `backend/src/config/db.js`.

#### Agent orchestration pipeline
`backend/src/agents/orchestrator.js` is the central control-flow boundary: it carries request state through Planner → Retriever → Coder → Executor → Reviewer and emits each stage’s progress for SSE consumers. The stages are implemented in `backend/src/agents/planner.js`, `backend/src/agents/retriever.js`, `backend/src/agents/coder.js`, `backend/src/agents/executor.js`, and `backend/src/agents/reviewer.js`. Groq-backed LLM access and model selection are isolated in `backend/src/config/groq.js`.

#### Retrieval and cache layers
The Retriever obtains contextual material through the vector-store abstraction in `backend/src/rag/vectorStore.js`. Qdrant integration is encapsulated by `backend/src/rag/qdrantClient.js`; the application can use an in-memory cosine-similarity fallback when Qdrant is unavailable. Redis connectivity, caching, and fixed-window rate-limit support are centralized in `backend/src/config/redis.js`, also with in-memory degradation. Repeated query pipeline results are cacheable independently of Mongo conversation persistence.

#### Specialized output modes and execution
The Executor runs JavaScript extracted from generated drafts in a demo-grade in-process `vm2` sandbox (`backend/src/agents/executor.js`), bounded by a timeout. Output formatting supports generation of PowerPoint artifacts via `backend/src/agents/formatter.js`, with produced files represented under `backend/generated/`. Image-oriented requests are delegated to Pollinations.ai through `backend/src/agents/imageProvider.js`; this is an external anonymous, rate-limited service.

#### Runtime and deployment
The runtime is Node.js/Express plus React 18/Vite. Docker definitions for each application tier live in `backend/Dockerfile` and `frontend/Dockerfile`; `docker-compose.yml` provisions the full local stack, including MongoDB, Redis, and Qdrant. Required operational secrets include Groq API credentials and JWT configuration; MongoDB is the durable system of record, while Redis and Qdrant are optional accelerators due to fallbacks.

Main entry points: [`backend/src/agents/orchestrator.js`](https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/orchestrator.js) (Orchestrator), [`backend/server.js`](https://github.com/ayushgupta2408/brain/blob/main/backend/server.js) (Express server), [`frontend/src/pages/ChatPage.jsx`](https://github.com/ayushgupta2408/brain/blob/main/frontend/src/pages/ChatPage.jsx) (Chat page), [`frontend/src/components/ChatWindow.jsx`](https://github.com/ayushgupta2408/brain/blob/main/frontend/src/components/ChatWindow.jsx) (Chat window)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React client"]
  node_web["React/Vite app<br/>client entry<br/>[main.jsx]"]
  node_chat_page["Chat page<br/>UI composition<br/>[ChatPage.jsx]"]
  node_chat_window["Chat window<br/>chat UI<br/>[ChatWindow.jsx]"]
  node_pipeline_ui["Pipeline status<br/>SSE UI<br/>[AgentPipeline.jsx]"]
  node_chat_state["Chat state<br/>Redux slice<br/>[chatSlice.js]"]
end

subgraph group_api["Express API"]
  node_server["Express server<br/>HTTP entry<br/>[server.js]"]
  node_auth_routes["Auth routes<br/>registration/login API<br/>[authRoutes.js]"]
  node_chat_routes["Chat routes<br/>chat and SSE API<br/>[chatRoutes.js]"]
  node_jwt["JWT middleware<br/>auth guard<br/>[auth.js]"]
end

subgraph group_agents["Agent pipeline"]
  node_orchestrator{{"Orchestrator<br/>pipeline control<br/>[orchestrator.js]"}}
  node_planner["Planner<br/>agent stage<br/>[planner.js]"]
  node_retriever["Retriever<br/>agent stage<br/>[retriever.js]"]
  node_coder["Coder<br/>agent stage<br/>[coder.js]"]
  node_executor["Executor<br/>vm2 sandbox stage<br/>[executor.js]"]
  node_reviewer["Reviewer<br/>agent stage<br/>[reviewer.js]"]
  node_groq["Groq access<br/>LLM gateway<br/>[groq.js]"]
  node_formatter["Artifact formatter<br/>PowerPoint output<br/>[formatter.js]"]
  node_image_provider["Image provider<br/>Pollinations adapter<br/>[imageProvider.js]"]
end

subgraph group_data["Data and acceleration"]
  node_mongo_models[("Mongo models<br/>user and chat persistence<br/>[User.js]")]
  node_vector_store["Vector store<br/>retrieval abstraction<br/>[vectorStore.js]"]
  node_qdrant[("Qdrant client<br/>vector backend<br/>[qdrantClient.js]")]
  node_redis[("Redis layer<br/>cache and rate limits<br/>[redis.js]")]
end

node_web -->|"renders"| node_chat_page
node_chat_page -->|"composes"| node_chat_window
node_chat_page -->|"composes"| node_pipeline_ui
node_chat_window -->|"reads and updates"| node_chat_state
node_chat_window -->|"authenticated chat requests"| node_server
node_pipeline_ui -->|"consumes SSE progress"| node_server
node_server -->|"mounts"| node_auth_routes
node_server -->|"mounts"| node_chat_routes
node_auth_routes -->|"stores accounts"| node_mongo_models
node_chat_routes -->|"requires"| node_jwt
node_jwt -->|"authorizes pipeline"| node_orchestrator
node_chat_routes -->|"persists conversations"| node_mongo_models
node_orchestrator -->|"plans"| node_planner
node_planner -->|"queries context"| node_retriever
node_retriever -->|"retrieves context"| node_vector_store
node_vector_store -->|"uses when available"| node_qdrant
node_retriever -->|"supplies context"| node_coder
node_coder -->|"sends generated code"| node_executor
node_executor -->|"returns execution result"| node_reviewer
node_orchestrator -->|"invokes LLM"| node_groq
node_reviewer -->|"formats artifacts"| node_formatter
node_orchestrator -->|"delegates image requests"| node_image_provider
node_orchestrator -->|"caches repeated results"| node_redis

click node_web "https://github.com/ayushgupta2408/brain/blob/main/frontend/src/main.jsx"
click node_chat_page "https://github.com/ayushgupta2408/brain/blob/main/frontend/src/pages/ChatPage.jsx"
click node_chat_window "https://github.com/ayushgupta2408/brain/blob/main/frontend/src/components/ChatWindow.jsx"
click node_pipeline_ui "https://github.com/ayushgupta2408/brain/blob/main/frontend/src/components/AgentPipeline.jsx"
click node_chat_state "https://github.com/ayushgupta2408/brain/blob/main/frontend/src/store/chatSlice.js"
click node_server "https://github.com/ayushgupta2408/brain/blob/main/backend/server.js"
click node_auth_routes "https://github.com/ayushgupta2408/brain/blob/main/backend/src/routes/authRoutes.js"
click node_chat_routes "https://github.com/ayushgupta2408/brain/blob/main/backend/src/routes/chatRoutes.js"
click node_jwt "https://github.com/ayushgupta2408/brain/blob/main/backend/src/middleware/auth.js"
click node_orchestrator "https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/orchestrator.js"
click node_planner "https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/planner.js"
click node_retriever "https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/retriever.js"
click node_coder "https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/coder.js"
click node_executor "https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/executor.js"
click node_reviewer "https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/reviewer.js"
click node_groq "https://github.com/ayushgupta2408/brain/blob/main/backend/src/config/groq.js"
click node_formatter "https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/formatter.js"
click node_image_provider "https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/imageProvider.js"
click node_mongo_models "https://github.com/ayushgupta2408/brain/blob/main/backend/src/models/User.js"
click node_vector_store "https://github.com/ayushgupta2408/brain/blob/main/backend/src/rag/vectorStore.js"
click node_qdrant "https://github.com/ayushgupta2408/brain/blob/main/backend/src/rag/qdrantClient.js"
click node_redis "https://github.com/ayushgupta2408/brain/blob/main/backend/src/config/redis.js"

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_web,node_chat_page,node_chat_window,node_pipeline_ui,node_chat_state toneBlue
class node_server,node_auth_routes,node_chat_routes,node_jwt toneAmber
class node_orchestrator,node_planner,node_retriever,node_coder,node_executor,node_reviewer,node_groq,node_formatter,node_image_provider toneMint
class node_mongo_models,node_vector_store,node_qdrant,node_redis toneRose
```

## Components

### React client

- [React/Vite app](https://github.com/ayushgupta2408/brain/blob/main/frontend/src/main.jsx): client entry · `frontend/src/main.jsx`
- [Chat page](https://github.com/ayushgupta2408/brain/blob/main/frontend/src/pages/ChatPage.jsx): UI composition · `frontend/src/pages/ChatPage.jsx`
- [Chat window](https://github.com/ayushgupta2408/brain/blob/main/frontend/src/components/ChatWindow.jsx): chat UI · `frontend/src/components/ChatWindow.jsx`
- [Pipeline status](https://github.com/ayushgupta2408/brain/blob/main/frontend/src/components/AgentPipeline.jsx): SSE UI · `frontend/src/components/AgentPipeline.jsx`
- [Chat state](https://github.com/ayushgupta2408/brain/blob/main/frontend/src/store/chatSlice.js): Redux slice · `frontend/src/store/chatSlice.js`

### Express API

- [Express server](https://github.com/ayushgupta2408/brain/blob/main/backend/server.js): HTTP entry · `backend/server.js`
- [Auth routes](https://github.com/ayushgupta2408/brain/blob/main/backend/src/routes/authRoutes.js): registration/login API · `backend/src/routes/authRoutes.js`
- [Chat routes](https://github.com/ayushgupta2408/brain/blob/main/backend/src/routes/chatRoutes.js): chat and SSE API · `backend/src/routes/chatRoutes.js`
- [JWT middleware](https://github.com/ayushgupta2408/brain/blob/main/backend/src/middleware/auth.js): auth guard · `backend/src/middleware/auth.js`

### Agent pipeline

- [Orchestrator](https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/orchestrator.js): pipeline control · Carries request state and emits stage progress. · `backend/src/agents/orchestrator.js`
- [Planner](https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/planner.js): agent stage · `backend/src/agents/planner.js`
- [Retriever](https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/retriever.js): agent stage · `backend/src/agents/retriever.js`
- [Coder](https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/coder.js): agent stage · `backend/src/agents/coder.js`
- [Executor](https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/executor.js): vm2 sandbox stage · Runs extracted JavaScript with a timeout. · `backend/src/agents/executor.js`
- [Reviewer](https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/reviewer.js): agent stage · `backend/src/agents/reviewer.js`
- [Groq access](https://github.com/ayushgupta2408/brain/blob/main/backend/src/config/groq.js): LLM gateway · `backend/src/config/groq.js`
- [Artifact formatter](https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/formatter.js): PowerPoint output · Produces files under backend/generated/. · `backend/src/agents/formatter.js`
- [Image provider](https://github.com/ayushgupta2408/brain/blob/main/backend/src/agents/imageProvider.js): Pollinations adapter · `backend/src/agents/imageProvider.js`

### Data and acceleration

- [Mongo models](https://github.com/ayushgupta2408/brain/blob/main/backend/src/models/User.js): user and chat persistence · `backend/src/models/User.js`
- [Vector store](https://github.com/ayushgupta2408/brain/blob/main/backend/src/rag/vectorStore.js): retrieval abstraction · `backend/src/rag/vectorStore.js`
- [Qdrant client](https://github.com/ayushgupta2408/brain/blob/main/backend/src/rag/qdrantClient.js): vector backend · Falls back to in-memory cosine similarity. · `backend/src/rag/qdrantClient.js`
- [Redis layer](https://github.com/ayushgupta2408/brain/blob/main/backend/src/config/redis.js): cache and rate limits · Falls back to in-memory storage. · `backend/src/config/redis.js`

## Connections

- React/Vite app → Chat page: renders
- Chat page → Chat window: composes
- Chat page → Pipeline status: composes
- Chat window → Chat state: reads and updates
- Chat window → Express server: authenticated chat requests
- Pipeline status → Express server: consumes SSE progress
- Express server → Auth routes: mounts
- Express server → Chat routes: mounts
- Auth routes → Mongo models: stores accounts
- Chat routes → JWT middleware: requires
- JWT middleware → Orchestrator: authorizes pipeline
- Chat routes → Mongo models: persists conversations
- Orchestrator → Planner: plans
- Planner → Retriever: queries context
- Retriever → Vector store: retrieves context
- Vector store → Qdrant client: uses when available
- Retriever → Coder: supplies context
- Coder → Executor: sends generated code
- Executor → Reviewer: returns execution result
- Orchestrator → Groq access: invokes LLM
- Reviewer → Artifact formatter: formats artifacts
- Orchestrator → Image provider: delegates image requests
- Orchestrator → Redis layer: caches repeated results
