How pretend-to/mio-chat-backend works
Main Components
• Backend Server: Represented by the core entry point (app.js) that initializes the server.
• HTTP API Layer: Shows Express handling routes and controllers (found in lib/server/http/controllers and related service and middleware folders).
• WebSocket/Socket.IO Layer: Manage real-time chat interactions (located in lib/server/socket.io, including controllers, middlewares, and services).
• Chat Adapters: Two distinct channels for handling chat protocols:
• OpenAI: Managed in lib/chat/llm which provides adapters (e.g., gemini.js, openai.js).
• Onebot: Managed in lib/chat/onebot with adapters for handling different communication modes (e.g., stdin.js, websocket.js).
• Plugin System: Contains built-in plugins (in lib/plugins) and custom plugins (in plugins/custom). They allow extensibility (e.g., web-plugin functionality for web parsing and external API integrations).
• Configuration Module: Includes configuration files (in the config directory) for server, API keys, and protocol-specific settings provided by the user.
• Presets and Utilities: Additional support directories, such as presets (to define pre-set roles or chat configurations) and utilities (like logger, async queue, imgTools).
Relationships and Data Flows
• Incoming client requests are handled by the HTTP layer, which invokes appropriate controllers and services to process file uploads, OpenAI and Onebot messages, and plugin calls.
• Real-time communications are handled separately by the WebSocket/Socket.IO layer, allowing for asynchronous message exchanges.
• The Chat Adapters module converts incoming requests (via HTTP or WebSocket) into protocol-specific actions (either OpenAI or Onebot) and handles responses accordingly.
• The Plugin System intercepts or enhances functionality at key points (e.g., extending API calls, parsing web content) and communicates with both the HTTP and WebSocket layers as required.
• The Configuration Module feeds critical environment settings (such as API keys, server port, and protocol settings) into various components.
• Utility modules support logging, error handling, file operations, and asynchronous operations across all layers.
Architectural Patterns and Design Principles
• Layered Architecture: Distinct separation between HTTP routing, WebSocket communications, business logic, and plugin extensions.
• Adapter & Plugin Patterns: Chat adapter modules abstract differences between OpenAI and Onebot protocols; the plugin system provides extensibility to plug in new features dynamically.
• Configuration Management: Centralized management of configuration files ensures decoupling of runtime parameters from code.
Relevant Technologies/Frameworks
• Node.js – core runtime environment.
• Express – for HTTP server and route management.
• Socket.IO – handling WebSocket real-time data flow.
• WebSocket (ws) library – for direct WebSocket connections.
• OpenAI API and Gemini API – integration for AI models.
• Various middleware for logging, rate-limiting, and response formatting.