Roleplay Bot
RolePlay Studio - Backend Server Express.js backend with MVC architecture for RolePlay Studio, powered by Dolphin Mistral via Ollama. Prerequisites 1. Install Ollama: Download from...
Technical Architecture & Engineering Overview
Detailed system design and core implementation
Core Architecture
The application follows a classic three‑tier architecture: a presentation layer built with React 18, TypeScript, and TailwindCSS; a service layer implemented in Express that handles routing, authentication (JWT), and AI request forwarding; and a data layer using MongoDB with Mongoose models. The entry point server/server.js wires middleware (cors, express.json, request logger) and mounts the /api/auth and /api routers, exposing endpoints documented in the repository.
Data Flow & Persistence
User registration and login flow through auth.routes.js, invoking auth.controller methods that validate input (via Joi schemas as noted in the project details) and persist user records. Authenticated requests carry a Bearer token processed by auth.middleware, which guards protected routes like /api/auth/me, /api/auth/sessions, and /api/chat. Chat sessions are stored as documents containing configuration objects (gender, role, scene, level) and message arrays, enabling later retrieval and replay.
AI Integration
The /api/chat endpoint forwards the user’s message payload to an Ollama model (dolphin‑mistral by default) over HTTP. Health checks (/api/health) verify both server uptime and Ollama connectivity, providing quick diagnostics for developers. Error handling middleware captures uncaught exceptions and returns sanitized JSON error objects, while the root endpoint enumerates available routes for discoverability.
Security & DevOps Guardrails
Security is enforced through JWT authentication, rate‑limiting placeholders, and Helmet headers (as recommended in the project notes). Environment variables (.env) store secrets like JWT_SECRET, OLLAMA_URL, and DB credentials, keeping them out of source control. The setup documentation outlines MongoDB Atlas IP whitelisting and port conflict resolution, ensuring smooth local development and production readiness.
Build & Deployment
Frontend assets are built with Vite (npm run build) and can be deployed to static hosts (Vercel, Netlify). The backend can be containerized or run directly via npm run dev. For desktop distribution, Electron wraps the web stack, leveraging IPC for native features while reusing the same codebase.
System Diagrams & Flow Visualizations
Interactive Mermaid architecture & execution flows
Core System Features
Key capabilities & architectural features
User registration and secure login with password hashing
User registration and secure login with password hashing
JWT protected routes for accessing user profile and chat sessions
JWT protected routes for accessing user profile and chat sessions
Save, retrieve, and delete chat sessions with custom configuration (gender, role, scene, intensity)
Save, retrieve, and delete chat sessions with custom configuration (gender, role, scene, intensity)
Real‑time AI chat powered by Ollama's `dolphin‑mistral` model
Real‑time AI chat powered by Ollama's `dolphin‑mistral` model
Health‑check endpoint that validates server and Ollama connectivity
Health‑check endpoint that validates server and Ollama connectivity
Rich‑text editor powered by Tiptap with server‑side XSS sanitization
Rich‑text editor powered by Tiptap with server‑side XSS sanitization
Drag‑and‑drop ordering of sessions using dnd‑kit with state synchronization
Drag‑and‑drop ordering of sessions using dnd‑kit with state synchronization
Responsive dark‑mode UI built with TailwindCSS and shadcn/ui components
Responsive dark‑mode UI built with TailwindCSS and shadcn/ui components
Cross‑platform desktop packaging via Electron while reusing web code
Cross‑platform desktop packaging via Electron while reusing web code
Environment‑driven configuration for MongoDB Atlas, Ollama URL, and JWT secret
Environment‑driven configuration for MongoDB Atlas, Ollama URL, and JWT secret
Comprehensive logging of requests and errors for debugging
Comprehensive logging of requests and errors for debugging
Modular Express router architecture separating auth and chat concerns
Modular Express router architecture separating auth and chat concerns
TypeScript typings throughout the frontend for compile‑time safety
TypeScript typings throughout the frontend for compile‑time safety
Automated scripts for building and deploying frontend to Vercel/Netlify
Automated scripts for building and deploying frontend to Vercel/Netlify
Engineering Challenges & Solutions
Problem statements & resolution strategies
Balancing real‑time UI responsiveness with database write consistency during rapid drag‑and‑drop reordering
Balancing real‑time UI responsiveness with database write consistency during rapid drag‑and‑drop reordering
Ensuring XSS‑free storage of complex HTML generated by Tiptap without stripping legitimate formatting
Ensuring XSS‑free storage of complex HTML generated by Tiptap without stripping legitimate formatting
Managing JWT expiration and token refresh in a single‑page application without degrading user experience
Managing JWT expiration and token refresh in a single‑page application without degrading user experience
Handling Ollama service failures gracefully while providing meaningful error messages to the frontend
Handling Ollama service failures gracefully while providing meaningful error messages to the frontend
Coordinating port usage and environment variable overrides to avoid conflicts during concurrent backend and frontend development
Coordinating port usage and environment variable overrides to avoid conflicts during concurrent backend and frontend development
API Reference & Endpoints
REST API surface & route documentation
| METHOD | ENDPOINT PATH | DESCRIPTION | COPY |
|---|---|---|---|
| POST | /register | Endpoint defined in server/routes/auth.routes.js | |
| POST | /login | Endpoint defined in server/routes/auth.routes.js | |
| GET | /me | Endpoint defined in server/routes/auth.routes.js | |
| POST | /sessions | Endpoint defined in server/routes/auth.routes.js | |
| DELETE | /sessions/:sessionId | Endpoint defined in server/routes/auth.routes.js | |
| POST | /chat | Endpoint defined in server/routes/chat.routes.js | |
| GET | /health | Endpoint defined in server/routes/chat.routes.js | |
| GET | / | Endpoint defined in server/server.js |
Key Code Modules & Architecture
Core file entrypoints and service responsibilities
serverModule component managing server source logic
server/configModule component managing config source logic
server/controllersModule component managing controllers source logic
server/middlewareModule component managing middleware source logic
server/modelsModule component managing models source logic
server/routesModule component managing routes source logic
srcModule component managing src source logic
src/componentsModule component managing components source logic
src/components/uiModule component managing ui source logic
src/hooksModule component managing hooks source logic
src/libModule component managing lib source logic
src/pagesModule component managing pages source logic
src/typesModule component managing types source logic
User Journey Pipeline
- 1User accesses application user interface via browser
- 2User triggers action invoking HTTP/WebSocket API endpoints
- 3Data persisted or retrieved from database storage layer
Execution Code Flow
- 1Client or caller initiates request to application entrypoint
- 2Backend router validates request parameters and applies middleware
- 3Database ORM/client executes storage query
- 4Application serializes JSON/audio/data response back to client
System FAQ & Technical Specifications
Frequently asked questions and core architecture details
Q: What technologies are used in Roleplay Bot?
Roleplay Bot is engineered using modern full-stack frameworks.
Q: What core engineering problem does Roleplay Bot solve?
RolePlay Studio - Backend Server Express.js backend with MVC architecture for RolePlay Studio, powered by Dolphin Mistral via Ollama. Prerequisites 1. Install Ollama: Download from...
Q: How is 'Balancing real‑time UI responsiveness with database write consistency during rapid drag‑and‑drop reordering' resolved in Roleplay Bot?
Resolved through specialized system architecture and resilient state handling.
Related Systems & Case Studies
Explore similar technical architectures and projects