This document details the system architecture design of NekoBot.
NekoBot adopts a layered architecture design, divided into the following core layers:
┌─────────────────────────────────────────────────────────┐
│ Web Dashboard │
│ (React + Vite) │
└─────────────────────┬───────────────────────────────┘
│ HTTP / WebSocket
▼
┌─────────────────────────────────────────────────────────┐
│ Quart Application Layer │
│ (packages/backend/app.py) │
├─────────────────────────────────────────────────────────┤
│ - Route Management │
│ - JWT Authentication Middleware │
│ - WebSocket Service │
│ - Static File Service │
└─────────────────────┬───────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ Core Service Layer │
│ (packages/backend/core/) │
├─────────────────────────────────────────────────────────┤
│ - Plugin Manager (plugin_manager.py) │
│ - Platform Manager (platform_manager.py) │
│ - Message Pipeline (pipeline/) │
│ - Event Queue (event_queue) │
└─────────────────────┬───────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ Platform Adapter Layer │
│ (packages/backend/platform/) │
├─────────────────────────────────────────────────────────┤
│ - QQ Adapter (aiocqhttp_platform.py) │
│ - Discord Adapter (discord_platform.py) │
│ - Telegram Adapter (telegram_platform.py) │
└─────────────────────┬───────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ Plugin Layer │
│ (packages/backend/plugins/) │
├─────────────────────────────────────────────────────────┤
│ - Base Plugin Class (base.py) │
│ - User Plugins (data/plugins/) │
│ - Plugin Data Manager (plugin_data_manager.py) │
└─────────────────────────────────────────────────────────┘The Quart application layer is located in app.py and serves as the entry point for the entire system.
Main responsibilities:
Route registration:
for route_class in [
bot_config_route,
plugin_route,
log_route,
personality_route,
mcp_route,
llm_route,
settings_route,
platform_route,
chat_route,
command_route,
]:
for path, (method, handler) in route_class.routes.items():
app.add_url_rule(path, view_func=handler, methods=[method])The core service layer is located at packages/backend/core/ and contains the following core components:
The plugin manager (plugin_manager.py) is responsible for loading, enabling, disabling, and dispatching messages to plugins.
Main methods:
load_plugins() - Load all pluginsenable_plugin() - Enable specified plugindisable_plugin() - Disable specified pluginreload_plugin() - Reload specified plugindispatch_message() - Dispatch messages to pluginsThe platform manager (platform/manager.py) manages all platform adapters.
Main methods:
load_platforms() - Load platform configurationstart_all() - Start all platformsstop_all() - Stop all platformssend_message() - Send message to specified platformThe message pipeline (pipeline/) is responsible for processing platform events.
Pipeline stages:
WhitelistCheckStage - Whitelist checkContentSafetyCheckStage - Content safety checkRateLimitStage - Rate limitingSessionStatusCheckStage - Session status checkWakingCheckStage - Waking checkProcessStage - Process stage (call plugins)ResultDecorateStage - Result decorationRespondStage - Response sendingThe platform adapter layer is located at packages/backend/platform/ and handles interaction with various chat platforms.
Each platform adapter needs to implement the following interface:
class BasePlatform(ABC):
@abstractmethod
async def connect(self):
pass
@abstractmethod
async def send_message(self, message_type, target_id, message):
pass
@abstractmethod
async def disconnect(self):
pass
@abstractmethod
def get_stats(self):
passThe plugin layer is located at packages/backend/plugins/ and is the main way for users to extend functionality.
The plugin base class (base.py) defines plugin lifecycle methods:
on_load() - Called when plugin is loadedon_unload() - Called when plugin is unloadedon_enable() - Called when plugin is enabledon_disable() - Called when plugin is disabledon_message() - Called when a message is receivedPlatform Adapter Receives Message
↓
Convert to Unified Message Model
↓
Push to Event Queue (event_queue)
↓
Pipeline Scheduler Retrieves Event from Queue
↓
Execute All Pipeline Stages
↓
ProcessStage Calls Plugin Handlers
↓
RespondStage Sends ResponseUser Sends Command
↓
Platform Adapter Receives
↓
Convert to Unified Message Model
↓
Push to Event Queue
↓
Pipeline Scheduler Processes
↓
ProcessStage Matches Command
↓
Call Corresponding Plugin Command Handler
↓
Return ResponseNekoBot fully adopts asynchronous architecture, based on the following technologies:
All plugin methods and platform adapter methods should be asynchronous:
async def my_command(self, args, message):
await self.send_group_message(...)Configuration files are located in the data/ directory:
cmd_config.json - Main configuration fileplatforms_sources.json - Platform configurationllm_providers.json - LLM provider configurationusers.json - User dataConfiguration loading uses config.py:
from packages.backend.core.config import load_config
CONFIG = load_config()NekoBot uses an event-driven architecture where all platform events are distributed through an event queue:
event_queue = asyncio.Queue()
async def handle_events():
while True:
event = await event_queue.get()
await pipeline_scheduler.execute(event, ctx)This design enables:
platform/sources/BasePlatform and implement required methodsplatform/register.pyllm/sources/llm/register.pypipeline/Stage and implement execute() method