Skip to main content

πŸ—οΈ System Architecture

This document provides a detailed overview of Wegent's system architecture, component design, and technology stack.


πŸ“‹ Table of Contents​


🌐 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​

LayerResponsibilitiesCore Technologies
Management Platform LayerUser interaction, resource management, API services, chat processingNext.js 15, FastAPI, React 19, Chat Shell
Data LayerData persistence, cache management, async task schedulingMySQL 9.4, Redis 7, Celery
Execution LayerTask scheduling, container orchestration, resource isolation, local device managementDocker, Python, WebSocket
Agent LayerAI capabilities, code execution, chat processing, external API integrationClaude Code, Agno, Dify
Knowledge LayerKnowledge base management, RAG retrieval, vectorizationKnowledgeOrchestrator, Embedding

πŸ”§ 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
  • Manage local devices and executors

Technology Stack:

  • Framework: Next.js 15 (App Router)
  • UI Library: React 19, shadcn/ui
  • Styling: Tailwind CSS 3.4
  • State Management: React Context + Hooks
  • Internationalization: i18next 25.5
  • Icons: Heroicons, Tabler Icons, Lucide React

Core Features:

  • 🎨 Configuration-driven UI with YAML visualization
  • πŸ”„ Real-time task status updates (WebSocket)
  • 🌍 Multi-language support (Chinese/English)
  • πŸ“± Responsive design (Mobile/Desktop component separation)
  • πŸ“± Local device management interface
  • πŸ’­ Thinking process visualization

Key File Structure:

frontend/src/
β”œβ”€β”€ app/ # Next.js App Router
β”‚ β”œβ”€β”€ (tasks)/ # Task-related pages
β”‚ β”œβ”€β”€ (settings)/ # Settings pages
β”‚ └── admin/ # Admin pages
β”œβ”€β”€ features/ # Feature modules
β”‚ β”œβ”€β”€ admin/ # Admin dashboard
β”‚ β”œβ”€β”€ devices/ # Device management (new)
β”‚ β”œβ”€β”€ feed/ # Discovery and subscriptions
β”‚ β”œβ”€β”€ knowledge/ # Knowledge base management
β”‚ β”œβ”€β”€ settings/ # Agent configuration
β”‚ └── tasks/ # Core task functionality
β”œβ”€β”€ components/ # Shared components
β”‚ β”œβ”€β”€ ui/ # shadcn/ui base components
β”‚ └── common/ # Business common components
└── hooks/ # Custom hooks

Feature Modules:

ModulePurpose
tasksTask creation, chat, group chat, workbench
devicesLocal device management, executor guide
knowledgeKnowledge base, documents, permissions
settingsAgent, model, shell, skill configuration
feedSubscription market, trigger management

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)
  • Unified knowledge management (KnowledgeOrchestrator)
  • Manage local device connections

Technology Stack:

  • Framework: FastAPI 0.68+
  • ORM: SQLAlchemy 2.0
  • Database Driver: PyMySQL
  • Authentication: JWT (PyJWT), OAuth (Authlib), OIDC
  • Async Support: asyncio, aiohttp
  • Cache: Redis client
  • Real-time Communication: Socket.IO (python-socketio) with Redis adapter
  • Async Tasks: Celery

Core Features:

  • πŸš€ High-performance async API
  • πŸ”’ JWT-based authentication
  • πŸ“ Complete CRUD operation support
  • πŸ”„ Real-time status synchronization
  • πŸ›‘οΈ Data encryption (AES-256-CBC)
  • πŸ‘₯ Role-based access control (admin/user)
  • 🎼 Unified knowledge management (KnowledgeOrchestrator)
  • πŸ“± Local device management (Device Provider)

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
β”œβ”€β”€ /devices # Device management (new)
β”œβ”€β”€ /knowledge # Knowledge base management
β”œβ”€β”€ /groups # Organization/group management
β”œβ”€β”€ /share # Share link management
└── /admin # Admin operations (user management, public models)

Service Layer Architecture:

ServiceResponsibility
KindServiceUnified CRD resource management
KnowledgeOrchestratorKnowledge management entry point (REST API + MCP tools)
DeviceServiceLocal device management
ChatServiceChat processing and RAG
SubtaskServiceSubtask management
GroupServiceMulti-tenant group management
UserServiceUser management

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
python-socketio >= 5.0 # Socket.IO server
celery >= 5.0 # Async tasks

3. πŸ’¬ Chat Shell (Conversation Engine)​

Responsibilities:

  • Provide lightweight AI conversation engine
  • Support multiple LLM models (Anthropic, OpenAI, Google)
  • Manage conversation context and session storage
  • Integrate MCP tools and skill system
  • Support knowledge base retrieval augmentation (RAG)

Technology Stack:

  • Framework: FastAPI
  • Agent Framework: LangGraph + LangChain
  • LLM: Anthropic, OpenAI, Google Gemini
  • Storage: SQLite, Remote API
  • Observability: OpenTelemetry

Three Deployment Modes:

ModeDescriptionUse Case
HTTPStandalone HTTP service /v1/responseProduction
PackagePython package, imported by BackendMonolithic deployment
CLICommand-line interactive interfaceDevelopment/Testing

Core Features:

  • πŸ€– Multi-LLM support (Anthropic, OpenAI, Google)
  • πŸ› οΈ MCP tool integration (Model Context Protocol)
  • πŸ“š Dynamic skill loading
  • πŸ’Ύ Multiple storage backends (SQLite, Remote)
  • πŸ“Š Message compression (auto-compress when exceeding context limit)
  • πŸ“ˆ OpenTelemetry integration

Module Structure:

chat_shell/chat_shell/
β”œβ”€β”€ main.py # FastAPI application entry
β”œβ”€β”€ agent.py # ChatAgent creation
β”œβ”€β”€ interface.py # Unified interface definitions
β”œβ”€β”€ agents/ # LangGraph agent building
β”œβ”€β”€ api/ # REST API endpoints
β”‚ └── v1/ # V1 version API
β”œβ”€β”€ services/ # Business logic layer
β”‚ β”œβ”€β”€ chat_service.py
β”‚ └── streaming/ # Streaming response
β”œβ”€β”€ tools/ # Tool system
β”‚ β”œβ”€β”€ builtin/ # Built-in tools (WebSearch, etc.)
β”‚ β”œβ”€β”€ mcp/ # MCP tool integration
β”‚ └── sandbox/ # Sandbox execution environment
β”œβ”€β”€ storage/ # Session storage
β”‚ β”œβ”€β”€ sqlite/ # SQLite storage
β”‚ └── remote/ # Remote storage
β”œβ”€β”€ models/ # LLM model factory
β”œβ”€β”€ messages/ # Message processing
β”œβ”€β”€ compression/ # Context compression
└── skills/ # Skill loading

4. πŸ’― Executor Manager​

Responsibilities:

  • Manage Executor lifecycle
  • Task queue and scheduling
  • Resource allocation and rate limiting
  • Callback handling
  • Support multiple deployment modes

Technology Stack:

  • Language: Python
  • Container Management: Docker SDK
  • Networking: Docker bridge network
  • Scheduling: APScheduler

Deployment Modes:

ModeDescriptionUse Case
DockerUse Docker SDK to manage local containersStandard deployment
Local DeviceConnect to local device for executionDevelopment environment

Core Features:

  • 🎯 Maximum concurrent task control (default: 5)
  • πŸ”§ Dynamic port allocation (10001-10100)
  • 🐳 Docker container orchestration
  • πŸ“Š Task status tracking
  • πŸ“± Local device support

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

5. πŸš€ 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

Agent Types:

AgentTypeDescription
ClaudeCodelocal_engineClaude Code SDK, supports Git, MCP, Skills
Agnolocal_engineMulti-agent collaboration, SQLite session management
Difyexternal_apiProxy to Dify platform
ImageValidatorvalidatorCustom base image validation

Core Features:

  • πŸ”’ Fully isolated execution environment
  • πŸ’Ό Independent workspace
  • πŸ”„ Automatic cleanup mechanism (can be preserved with preserveExecutor)
  • πŸ“ Real-time log output
  • πŸ› οΈ MCP tool support
  • πŸ“š Dynamic skill loading

Lifecycle:


6. πŸ’Ύ 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/
β”œβ”€β”€ kinds # CRD resources (Ghost, Model, Shell, Bot, Team, Skill, Device)
β”œβ”€β”€ tasks # Task and Workspace resources (separate table)
β”œβ”€β”€ skill_binaries # Skill binary packages
β”œβ”€β”€ users # User information (with role field)
β”œβ”€β”€ groups # Organizations/groups
β”œβ”€β”€ namespace_members # Namespace members
β”œβ”€β”€ knowledge_bases # Knowledge bases
β”œβ”€β”€ documents # Documents
└── public_models # System-wide public models

Data Model Features:

  • Uses SQLAlchemy ORM
  • Supports transactions and relational queries
  • Automatic timestamp management
  • Soft delete support
  • CRD resources uniquely identified by (namespace, name, user_id) tuple

7. πŸ”΄ Cache (Redis)​

Responsibilities:

  • Task status caching
  • Session management
  • Temporary real-time data storage
  • Task expiration management
  • Socket.IO multi-instance adapter

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
  • πŸ”Œ Socket.IO Redis adapter (multi-instance communication)

8. ⚑ Celery (Async Tasks)​

Responsibilities:

  • Knowledge base document indexing (async)
  • Document summary generation
  • Long-running task processing

Core Tasks:

TaskPurpose
index_document_taskDocument vectorization indexing
generate_document_summary_taskDocument summary generation

9. 🎼 KnowledgeOrchestrator​

Responsibilities:

  • Unify knowledge management for REST API and MCP tools
  • Automatically select retriever, embedding model, summary model
  • Coordinate Celery async tasks

Architecture:

Entry Layer (REST/MCP)
↓
KnowledgeOrchestrator
↓
Service Layer (knowledge_service.py)
↓
Celery Tasks (async processing)

Core Features:

  • πŸ”— Unified entry point: REST API and MCP tools share the same business logic
  • πŸ€– Auto model selection: Task β†’ Team β†’ Bot β†’ Model chain resolution
  • πŸ“š Multi-scope support: Personal, group, organization knowledge bases
  • ⚑ Async indexing: Handle large documents via Celery

πŸ”„ Data Flow and Communication Patterns​

Task Execution Flow​

Communication Protocols​

Communication TypeProtocolPurpose
Frontend ↔ BackendHTTP/HTTPS, WebSocket (Socket.IO)API calls, real-time chat streaming
Backend ↔ DatabaseMySQL ProtocolData persistence
Backend ↔ RedisRedis ProtocolCache operations, Socket.IO adapter
Backend ↔ Executor ManagerHTTPTask scheduling
Executor Manager ↔ ExecutorDocker APIContainer management
Executor ↔ AgentProcess invocationTask execution

WebSocket Architecture (Socket.IO)​

The chat system uses Socket.IO for bidirectional real-time communication:

Namespace: /chat Path: /socket.io

Client β†’ Server Events:

EventPurpose
chat:sendSend a chat message
chat:cancelCancel ongoing stream
chat:resumeResume stream after reconnect
task:joinJoin a task room
task:leaveLeave a task room
history:syncSync message history

Server β†’ Client Events:

EventPurpose
chat:startAI started generating response
chat:chunkStreaming content chunk
chat:doneAI response completed
chat:errorError occurred
chat:cancelledStream was cancelled
chat:messageNon-streaming message (group chat)
task:createdNew task created
task:statusTask 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": [
"shadcn/ui",
"Tailwind CSS 3.4",
"Lucide React",
"Heroicons 2.2"
],
"i18n": "i18next 25.5",
"markdown": "react-markdown",
"realtime": "socket.io-client",
"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",
"authlib" # OIDC support
],
"async": [
"asyncio >= 3.4.3",
"aiohttp >= 3.8.0",
"httpx >= 0.19.0"
],
"cache": "redis >= 4.5.0",
"realtime": "python-socketio >= 5.0",
"tasks": "celery >= 5.0",
"security": [
"cryptography >= 41.0.5",
"pycryptodome >= 3.20.0"
],
"telemetry": "opentelemetry-*",
"testing": [
"pytest >= 7.4.0",
"pytest-asyncio >= 0.21.0"
]
}

Chat Shell Stack​

{
"framework": "FastAPI",
"agent": "LangGraph + LangChain",
"llm": [
"langchain-anthropic",
"langchain-openai",
"langchain-google-genai"
],
"storage": "SQLite / Remote API",
"telemetry": "opentelemetry-*"
}

Infrastructure​

database:
mysql: "9.4"

cache:
redis: "7"

container:
docker: "latest"
docker-compose: "latest"

task_queue:
celery: "5.0+"
broker: "redis"

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-256-CBC encryption for sensitive data
  • πŸ” Sandbox environment isolation
  • 🚫 Principle of least privilege
  • πŸ‘₯ Role-based access control (admin/user roles)
  • πŸ”‘ OIDC enterprise single sign-on support

5. Observability​

  • πŸ“ Structured logging (structlog)
  • πŸ“Š Status tracking and monitoring
  • πŸ” Detailed error information
  • πŸ“ˆ Performance metrics collection
  • πŸ”­ OpenTelemetry integration (distributed tracing)

πŸ“ˆ 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
socket_adapter: redis # Socket.IO multi-instance support

Chat Shell Scaling​

# Standalone service, supports multiple instances
chat_shell:
replicas: 2
storage: remote # Remote storage for multi-instance

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
architecture:
frontend: "Multi-instance + Nginx load balancing"
backend: "Multi-instance + API gateway + Redis Socket.IO adapter"
chat_shell: "Multi-instance + Remote storage"
mysql: "Master-slave replication + read-write separation"
redis: "Redis Cluster"
celery: "Multi-worker"
executor: "Dynamic scaling"

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​

MetricTarget ValueDescription
API Response Time< 200msP95 latency
Task Startup Time< 5sFrom creation to execution
Concurrent Tasks5-100Configurable
Database Connection Pool20Default configuration
WebSocket Connections1000+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)


Understanding the architecture is key to mastering Wegent! πŸš€