An intelligent help desk that lets businesses deploy a customizable AI chat widget on their website. Visitors chat with a LangGraph-powered AI agent backed by a knowledge base β and when needed, conversations seamlessly escalate to human agents in real-time.
To update this diagram, open .excalidraw in VSCode and export it as architecture.png to the root folder.
- LangGraph StateGraph orchestration with automatic tool-calling loops
- Google Gemini LLM (configurable model, temperature, tone, response length)
- MongoDB Atlas Vector Search for RAG-based knowledge retrieval
- Confidence thresholds β AI only answers when it's sure; otherwise escalates or admits uncertainty
- Persistent memory via
MongoDBSavercheckpointer β the AI remembers the full conversation
- Drop-in
<script>tag β works on any website - Real-time AI responses via Next.js Server Actions
- Automatic lead capture (name, email, phone)
- Seamless handoff to human agents via Socket.IO
- Continue / Start New Chat flows for returning visitors
- Inbox β Real-time human chat with visitors (claim, assign, resolve)
- Leads β Auto-captured visitor contacts with search & filtering
- Knowledge Base β Upload docs, scrape URLs, manage AI training data
- Team β Invite members, assign roles (owner / admin / agent)
- Settings β Configure AI agent personality, model, thresholds
- Widget Customizer β Customize colors, branding, welcome messages
- Custom Node.js server running Next.js + Socket.IO on a single port
- JWT-based auth for both visitors (tickets) and agents (tokens)
- Workspace-scoped rooms for multi-tenant isolation
- Routing service:
ai β waiting β human β resolved β ai
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Custom Node.js Server β
β (server.ts β port 3000) β
β βββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββββββ β
β β Next.js 16 β β Socket.IO Server β β
β β (App Router + RSC) β β (socket-server.ts) β β
β β β β β β
β β Server Actions: β β Events: β β
β β β’ sendMessageToAi() β β β’ message:send / message:created β β
β β β’ captureLeadAction()β β β’ conversation:claim / assign β β
β β β’ getChatHistory() β β β’ conversation:route-changed β β
β β β β β’ visitor:profile-updated β β
β βββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β LangGraph AI Agent β
β (graph.ts) β
β β
β START βββΊ chatBot ββββΊ tools βββΊ chatBot βββΊ END β
β β β β
β β ββ π search_knowledge_base β
β β β ββ MongoDB Atlas Vector Search β
β β ββ π§ capture_user_details β
β β β ββ Save lead + Socket.IO emit β
β β ββ π¨ escalate_to_human β
β β ββ Status β "waiting" β
β β β
β βΌ β
β βββββββββββββββββββββββ ββββββββββββββββββββββββββββ β
β β Google Gemini LLM β β MongoDBSaver β β
β β (ChatGoogle) β β (Checkpointer/Memory) β β
β βββββββββββββββββββββββ ββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β MongoDB Atlas β
β β
β Collections: β
β β’ workspaces β Multi-tenant workspace config β
β β’ conversations β Chat sessions (status, visitor info) β
β β’ messages β All chat messages (visitor/ai/agent/system) β
β β’ agents β AI agent configuration per workspace β
β β’ knowledge_entries β Uploaded knowledge base documents β
β β’ vectors β Embeddings for semantic search β
β β’ checkpoints β LangGraph conversation memory β
β β’ users / members β Auth & team management β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Layer | Technology |
|---|---|
| Framework | Next.js 16 (App Router, React 19, Server Actions) |
| AI Orchestration | LangGraph (@langchain/langgraph) |
| LLM | Google Gemini via @langchain/google |
| Embeddings | Gemini Embedding 001 (1536 dims) |
| Vector Search | MongoDB Atlas Vector Search |
| Database | MongoDB + Mongoose 9 |
| Real-Time | Socket.IO 4 |
| Auth | NextAuth v4 + JWT |
| Styling | Tailwind CSS 4 + shadcn/ui |
| State | Zustand |
| Storage | Cloudflare R2 (S3-compatible) |
| Background Jobs | Inngest |
| Nodemailer | |
| Runtime | Node.js with TSX loader |
help-desk/
βββ server.ts # Custom Node.js server (Next.js + Socket.IO)
βββ app/
β βββ actions/ # Server Actions (chat, leads, knowledge, team)
β βββ queries/ # Cached queries with "use cache" (leads, knowledge, dashboard)
β βββ dashboard/ # Admin dashboard pages
β β βββ inbox/ # Real-time human chat inbox
β β βββ leads/ # Captured leads table
β β βββ knowledge/ # Knowledge base management
β β βββ team/ # Team member management
β β βββ settings/ # AI agent & workspace settings
β β βββ widget/ # Widget customizer
β βββ widget/ # Embeddable chat widget (iframe)
β βββ login/ signup/ # Auth pages
β βββ onboarding/ # Workspace setup flow
βββ lib/
β βββ ai/
β β βββ graph.ts # LangGraph StateGraph definition
β β βββ tools.ts # AI tools (search KB, capture details, escalate)
β β βββ llm.ts # Google Gemini singleton factory
β β βββ vector-store.ts # MongoDB Atlas Vector Search client
β β βββ embeddings.ts # Gemini embedding model config
β β βββ checkpoint.ts # MongoDBSaver for conversation memory
β β βββ agent-cache.ts # Agent config caching
β β βββ agent-instructions.ts # Dynamic system prompt builder
β βββ chat/
β β βββ socket-server.ts # Socket.IO server (all real-time events)
β β βββ socket-auth.ts # JWT auth middleware (visitor + agent)
β β βββ socket-notify.ts # HTTP-based socket emit helper
β β βββ routing-service.ts # Conversation routing (claim/assign/resolve)
β β βββ use-socket.ts # React hook for Socket.IO client
β βββ db/
β β βββ models/ # Mongoose models
β β βββ connect.ts # MongoDB connection singleton
β βββ auth/ # Auth utilities & workspace resolver
βββ components/
β βββ widget/ # Chat widget UI components
β βββ inbox/ # Inbox sidebar & chat window
β βββ helpdesk/ # Dashboard UI (leads table, etc.)
β βββ knowledge/ # Knowledge base UI
β βββ dashboard/ # Dashboard overview components
βββ store/ # Zustand stores
- Node.js β₯ 20
- MongoDB Atlas cluster with Vector Search index
- Google API Key (Gemini)
- pnpm (recommended)
git clone https://github.com/sunil8521/help-desk.git
cd help-desk
pnpm installCreate a .env.local file:
# Database
MONGODB_URI=mongodb+srv://...
# Auth
NEXTAUTH_SECRET=your-secret
NEXTAUTH_URL=http://localhost:3000
# AI
GOOGLE_API_KEY=your-gemini-api-key
# Storage (Cloudflare R2)
CLOUDFLARE_R2_ACCESS_KEY_ID=...
CLOUDFLARE_R2_SECRET_ACCESS_KEY=...
CLOUDFLARE_R2_BUCKET_NAME=...
CLOUDFLARE_R2_ENDPOINT=...
CLOUDFLARE_R2_PUBLIC_URL=...
# Server
PORT=3000- Create a Vector Search Index named
vector_indexon thevectorscollection:
{
"fields": [
{
"type": "vector",
"path": "embedding",
"numDimensions": 1536,
"similarity": "cosine"
},
{
"type": "filter",
"path": "workspaceId"
}
]
}pnpm run devThis starts the custom Node.js server with both Next.js and Socket.IO on http://localhost:3000.
- Visitor sends message β
sendMessageToAi()server action - Message saved to MongoDB, then LangGraph invoked
- LangGraph loads conversation memory from
MongoDBSavercheckpointer - Gemini LLM receives system prompt (tone, role, threshold) + conversation history
- LLM decides whether to respond directly or call a tool:
search_knowledge_baseβ Vector search on MongoDB Atlas β Returns relevant docscapture_user_detailsβ Saves visitor name/email β Emits socket eventescalate_to_humanβ Sets status towaitingβ Notifies dashboard agents
- Tool results loop back to LLM via
toolsConditionedge - Final response saved as AI message β returned to widget
π€ ai βββΊ β³ waiting βββΊ π¨βπΌ human βββΊ β
resolved
β² β
ββββββββββββββββββββββββββββββββββββββββββββββββ
(return to AI / continue chat)
Add this to any website:
<script
src="https://your-domain.com/widget/loader.js"
data-workspace-id="ws_xxxxx"
async
></script>The widget loads in an iframe and communicates via server actions (AI mode) and Socket.IO (human mode).
MIT
Built with β€οΈ by Sunil Maharana