ποΈ System Architecture
This document provides a detailed overview of Wegent's system architecture, component design, and technology stack.
π Table of Contentsβ
- Architecture Overview
- Core Components
- Data Flow and Communication Patterns
- Technology Stack
- Design Principles
- Scalability and Deployment
π Architecture Overviewβ
Wegent adopts a modern layered architecture design based on Kubernetes-style declarative API and CRD (Custom Resource Definition) design patterns, providing a standardized framework for creating and managing AI agent ecosystems.
System Architecture Diagramβ
Architecture Layersβ
| Layer | Responsibilities | Core Technologies |
|---|---|---|
| Management Platform Layer | User interaction, resource management, API services | Next.js 15, FastAPI, React 19 |
| Data Layer | Data persistence, cache management | MySQL 9.4, Redis 7 |
| Execution Layer | Task scheduling, container orchestration, resource isolation | Docker, Python |
| Agent Layer | AI capabilities, code execution, chat processing, external API integration | Claude Code, Agno, Dify |
π§ Core Componentsβ
1. π Frontendβ
Responsibilities:
- Provide user interface for resource definition and management
- Implement task creation, monitoring, and result display
- Provide real-time interaction and status updates
Technology Stack:
- Framework: Next.js 15 (App Router)
- UI Library: React 19, Ant Design 5
- Styling: Tailwind CSS 3
- State Management: React Hooks
- Internationalization: i18next
- Icons: Heroicons, Tabler Icons
Core Features:
- π¨ Configuration-driven UI with YAML visualization
- π Real-time task status updates
- π Multi-language support (Chinese/English)
- π± Responsive design
Key File Structure:
frontend/
βββ app/ # Next.js App Router
βββ components/ # React components
βββ public/ # Static assets
βββ package.json # Dependencies
2. βοΈ Backendβ
Responsibilities:
- Implement declarative API for resource CRUD operations
- Manage user authentication and authorization
- Coordinate execution layer for task scheduling
- Provide WebSocket support for real-time chat communication (Socket.IO)
Technology Stack:
- Framework: FastAPI 0.68+
- ORM: SQLAlchemy 2.0
- Database Driver: PyMySQL
- Authentication: JWT (PyJWT), OAuth (Authlib)
- Async Support: asyncio, aiohttp
- Cache: Redis client
- Real-time Communication: Socket.IO (python-socketio) with Redis adapter
Core Features:
- π High-performance async API
- π JWT-based authentication
- π Complete CRUD operation support
- π Real-time status synchronization
- π‘οΈ Data encryption (AES)
- π₯ Role-based access control (admin/user)
API Design:
/api/v1/
βββ /ghosts # Ghost resource management
βββ /models # Model resource management
βββ /shells # Shell resource management
βββ /bots # Bot resource management
βββ /teams # Team resource management
βββ /workspaces # Workspace resource management
βββ /tasks # Task resource management
βββ /admin # Admin operations (user management, public models)
Key Dependencies:
FastAPI >= 0.68.0 # Web framework
SQLAlchemy >= 2.0.28 # ORM
PyJWT >= 2.8.0 # JWT authentication
Redis >= 4.5.0 # Cache
httpx >= 0.19.0 # HTTP client
3. π― Executor Managerβ
Responsibilities:
- Manage Executor lifecycle
- Task queue and scheduling
- Resource allocation and rate limiting
- Callback handling
Technology Stack:
- Language: Python
- Container Management: Docker SDK
- Networking: Docker bridge network
Core Features:
- π― Maximum concurrent task control (default: 5)
- π§ Dynamic port allocation (10001-10100)
- π³ Docker container orchestration
- π Task status tracking
Configuration Parameters:
MAX_CONCURRENT_TASKS: 5 # Maximum concurrent tasks
EXECUTOR_PORT_RANGE_MIN: 10001 # Port range start
EXECUTOR_PORT_RANGE_MAX: 10100 # Port range end
NETWORK: wegent-network # Docker network
EXECUTOR_IMAGE: wegent-executor:latest # Executor image
4. π Executorβ
Responsibilities:
- Provide isolated sandbox environment
- Execute agent tasks
- Manage workspace and code repositories
- Report execution results
Technology Stack:
- Container: Docker
- Runtime: Claude Code, Agno, Dify
- Version Control: Git
Core Features:
- π Fully isolated execution environment
- πΌ Independent workspace
- π Automatic cleanup mechanism
- π Real-time log output
Lifecycle:
5. πΎ Database (MySQL)β
Responsibilities:
- Persistent storage of all resource definitions
- Manage user data and authentication information
- Record task execution history
Version: MySQL 9.4
Core Table Structure:
wegent_db/
βββ ghosts # Ghost definitions
βββ models # Model configurations
βββ shells # Shell configurations
βββ bots # Bot instances
βββ teams # Team definitions
βββ workspaces # Workspace configurations
βββ tasks # Task records
βββ users # User information (with role field)
βββ public_models # System-wide public models
Data Model Features:
- Uses SQLAlchemy ORM
- Supports transactions and relational queries
- Automatic timestamp management
- Soft delete support
6. π΄ Cache (Redis)β
Responsibilities:
- Task status caching
- Session management
- Temporary real-time data storage
- Task expiration management
Version: Redis 7
Use Cases:
- π Chat task context caching (2-hour expiration)
- π» Code task status caching (2-hour expiration)
- π― Executor deletion delay control
- π Real-time status updates
π Data Flow and Communication Patternsβ
Task Execution Flowβ
Communication Protocolsβ
| Communication Type | Protocol | Purpose |
|---|---|---|
| Frontend β Backend | HTTP/HTTPS, WebSocket (Socket.IO) | API calls, real-time chat streaming |
| Backend β Database | MySQL Protocol | Data persistence |
| Backend β Redis | Redis Protocol | Cache operations, Socket.IO adapter |
| Backend β Executor Manager | HTTP | Task scheduling |
| Executor Manager β Executor | Docker API | Container management |
| Executor β Agent | Process invocation | Task execution |
WebSocket Architecture (Socket.IO)β
The chat system uses Socket.IO for bidirectional real-time communication:
Namespace: /chat
Path: /socket.io
Client β Server Events:
| Event | Purpose |
|---|---|
chat:send | Send a chat message |
chat:cancel | Cancel ongoing stream |
chat:resume | Resume stream after reconnect |
task:join | Join a task room |
task:leave | Leave a task room |
history:sync | Sync message history |
Server β Client Events:
| Event | Purpose |
|---|---|
chat:start | AI started generating response |
chat:chunk | Streaming content chunk |
chat:done | AI response completed |
chat:error | Error occurred |
chat:cancelled | Stream was cancelled |
chat:message | Non-streaming message (group chat) |
task:created | New task created |
task:status | Task status update |
Room-based Message Routing:
- User Room:
user:{user_id}- For personal notifications - Task Room:
task:{task_id}- For chat streaming and group chat
Redis Adapter: Enables multi-worker support for horizontal scaling
π οΈ Technology Stackβ
Frontend Stackβ
{
"framework": "Next.js 15",
"runtime": "React 19",
"language": "TypeScript 5.7",
"ui": [
"Ant Design 5.27",
"Tailwind CSS 3.4",
"Heroicons 2.2"
],
"i18n": "i18next 25.5",
"markdown": "react-markdown",
"devTools": [
"ESLint 9.17",
"Prettier 3.4",
"Husky 9.1"
]
}
Backend Stackβ
{
"framework": "FastAPI >= 0.68.0",
"language": "Python 3.10+",
"orm": "SQLAlchemy >= 2.0.28",
"database": "PyMySQL 1.1.0",
"auth": [
"PyJWT >= 2.8.0",
"python-jose 3.3.0",
"passlib 1.7.4"
],
"async": [
"asyncio >= 3.4.3",
"aiohttp >= 3.8.0",
"httpx >= 0.19.0"
],
"cache": "redis >= 4.5.0",
"security": [
"cryptography >= 41.0.5",
"pycryptodome >= 3.20.0"
],
"testing": [
"pytest >= 7.4.0",
"pytest-asyncio >= 0.21.0"
]
}
Infrastructureβ
database:
mysql: "9.4"
cache:
redis: "7"
container:
docker: "latest"
docker-compose: "latest"
executor_engines:
- "Claude Code (Anthropic)"
- "Agno"
- "Dify"
π― Design Principlesβ
1. Declarative API Designβ
Following Kubernetes CRD design patterns:
- β Resources defined declaratively in YAML
- β Clear resource hierarchy
- β Unified API version management
- β Separation of status and desired state
Example:
apiVersion: agent.wecode.io/v1
kind: Bot
metadata:
name: developer-bot
namespace: default
spec:
# Desired state
ghostRef:
name: developer-ghost
status:
# Actual state
state: "Available"
2. Separation of Concernsβ
- π¨ Frontend: Focused on user interaction and presentation
- βοΈ Backend: Focused on business logic and data management
- π Execution Layer: Focused on task scheduling and resource isolation
- π€ Agent Layer: Focused on AI capability provision
3. Microservices Architectureβ
- π§ Each component deployed independently
- π¦ Containerized packaging
- π Loose coupling between services
- π Independent scaling capability
4. Security Firstβ
- π JWT authentication mechanism
- π‘οΈ AES encryption for sensitive data
- π Sandbox environment isolation
- π« Principle of least privilege
- π₯ Role-based access control (admin/user roles)
5. Observabilityβ
- π Structured logging (structlog)
- π Status tracking and monitoring
- π Detailed error information
- π Performance metrics collection
π Scalability and Deploymentβ
Horizontal Scalingβ
Frontend Scalingβ
# Multi-instance deployment
frontend:
replicas: 3
load_balancer: nginx
Backend Scalingβ
# Stateless design, supports multiple instances
backend:
replicas: 5
session: redis
Executor Scalingβ
# Dynamic creation and destruction
executor_manager:
max_concurrent_tasks: 20
auto_scaling: true
Vertical Scalingβ
Database Optimizationβ
- Read-write separation
- Index optimization
- Query caching
Redis Optimizationβ
- Memory optimization
- Persistence strategy
- Cluster mode
Deployment Modesβ
1. Single-Machine Deployment (Development/Testing)β
docker-compose up -d
Use Cases:
- Local development
- Feature testing
- Small-scale usage
2. Distributed Deployment (Production)β
architecture:
frontend: "Multi-instance + Nginx load balancing"
backend: "Multi-instance + API gateway"
mysql: "Master-slave replication + read-write separation"
redis: "Redis Cluster"
executor: "Dynamic scaling"
Use Cases:
- Production environment
- High concurrency requirements
- Large-scale teams
3. Cloud-Native Deployment (Kubernetes)β
apiVersion: apps/v1
kind: Deployment
metadata:
name: wegent-backend
spec:
replicas: 3
template:
spec:
containers:
- name: backend
image: wegent-backend:latest
Use Cases:
- Cloud environments
- Auto-scaling
- High availability requirements
Performance Metricsβ
| Metric | Target Value | Description |
|---|---|---|
| API Response Time | < 200ms | P95 latency |
| Task Startup Time | < 5s | From creation to execution |
| Concurrent Tasks | 5-100 | Configurable |
| Database Connection Pool | 20 | Default configuration |
| WebSocket Connections | 1000+ | Concurrent online |
Monitoring and Alertingβ
Key Metricsβ
- π Task success rate
- β±οΈ Task execution time
- πΎ Database performance
- π΄ Redis cache hit rate
- π³ Container resource usage
Log Collectionβ
import structlog
logger = structlog.get_logger()
logger.info("task.created",
task_id=task.id,
team=task.team_ref.name)
π Related Resourcesβ
- Core Concepts - Understand Wegent's core concepts
- Collaboration Models - Deep dive into collaboration patterns
- YAML Specification - Complete configuration guide
- CRD Architecture - CRD design details
Understanding the architecture is key to mastering Wegent! π