Chuyển đến nội dung chính
Backend

Xây dựng AI Agent Platform với xClaw — Hướng dẫn thực chiến từ A đến Z

Hướng dẫn chi tiết xây dựng AI Agent Platform hoàn chỉnh với xClaw — monorepo TypeScript hỗ trợ Multi-LLM, RAG Pipeline, Workflow Engine, 13 Domain Packs, Multi-tenant RBAC, MCP Protocol và 8 Chat Channels. Từ kiến trúc Dual-Database đến deploy Docker production.

Xây dựng AI Agent Platform với xClaw — Hướng dẫn thực chiến từ A đến Z

1. Giới thiệu

xClaw là một open-source AI Agent Platform được xây dựng dưới dạng TypeScript monorepo, cho phép xây dựng và triển khai AI agents phục vụ đa ngành nghề. Platform cung cấp:

  • Visual Workflow Builder với 16 loại node
  • RAG Pipeline cho semantic search trên documents
  • Multi-LLM — hỗ trợ 10+ providers (OpenAI, Anthropic, Google, Groq, Mistral, DeepSeek, xAI, OpenRouter, Perplexity, Ollama)
  • 13 Domain Packs chuyên biệt theo ngành
  • Multi-tenant RBAC với 60 permissions chi tiết
  • MCP Protocol — Model Context Protocol server discovery
  • 8 Chat Channels — Telegram, Discord, Slack, WhatsApp, Zalo OA, Microsoft Teams, WebChat, Webhook
  • 12 ML/AutoML algorithms tích hợp sẵn

Bài viết này sẽ đi qua từng phần cốt lõi của xClaw, từ kiến trúc hệ thống đến cách cài đặt, phát triển và deploy lên production.

Source code: https://github.com/xdev-asia-labs/xClaw


2. Kiến trúc hệ thống

2.1. Tổng quan

xClaw sử dụng kiến trúc Gateway + Monorepo, trong đó:

  • Hono làm HTTP server (API Gateway)
  • React 19 + Vite cho frontend
  • Dual-Database Design — PostgreSQL + MongoDB
  • Redis cho caching và rate limiting
┌─────────────────────────────────────────────────────┐
│                    Client (Browser)                  │
│              React 19 + Tailwind + Zustand           │
└─────────────────┬───────────────────────────────────┘
                  │ HTTP / WebSocket
                  ▼
┌─────────────────────────────────────────────────────┐
│              API Gateway (Hono - Port 3000)          │
│         Auth │ RBAC │ Rate Limit │ CORS              │
├─────────┬────┴──────┬───────────┬───────────────────┤
│  Chat   │ Workflow  │    RAG    │   Admin/RBAC      │
│ Engine  │  Engine   │ Pipeline  │   Management      │
├─────────┴───────┬───┴───────────┴───────────────────┤
│                 │                                     │
│    ┌────────────┼────────────┐                        │
│    ▼            ▼            ▼                        │
│ ┌──────┐  ┌─────────┐  ┌───────┐                    │
│ │ PG   │  │ MongoDB  │  │ Redis │                    │
│ │:5432 │  │  :27018  │  │ :6379 │                    │
│ └──────┘  └─────────┘  └───────┘                    │
└─────────────────────────────────────────────────────┘

2.2. Dual-Database Design

Một trong những quyết định kiến trúc quan trọng nhất của xClaw là tách database theo tính chất dữ liệu:

DatabaseVai tròCollections/Tables
PostgreSQLConfig & structured datatenants, tenantSettings, users, roles, permissions, rolePermissions, userRoles, oauthAccounts, workflows, workflowExecutions, integrationConnections, webhooks
MongoDBAI & conversational datasessions, messages, memory_entries, agent_configs, audit_logs, system_logs
RedisCache layerSession cache, rate limiting, real-time metrics

Tại sao Dual-Database?

  • PostgreSQL — mạnh về relational data, ACID transactions, schema validation → phù hợp cho config, users, RBAC
  • MongoDB — flexible schema, tốt cho document storage, time-series data → phù hợp cho chat messages, AI memory, logs
  • Redis — in-memory, sub-millisecond latency → phù hợp cho caching, session, rate limiting

2.3. Tech Stack chi tiết

LayerCông nghệ
RuntimeNode.js 20, TypeScript (ES2022, ESM)
API ServerHono
FrontendReact 19, Tailwind CSS, Zustand, Vite
PostgreSQL ORMDrizzle ORM
MongoDBOfficial Node.js Driver
CacheRedis 8
LLMOpenAI, Anthropic, DeepSeek, xAI, OpenRouter, Perplexity, Google, Groq, Mistral, Ollama
AuthJWT, OAuth2 (Google, GitHub, Discord)
BuildDocker multi-stage, npm workspaces
DocsFumadocs + Next.js

3. Cài đặt & Quick Start

3.1. Yêu cầu

  • Docker & Docker Compose
  • Ollama (tuỳ chọn, cho local LLM)

3.2. Clone & cấu hình

# Clone repo kèm submodules
git clone --recurse-submodules https://github.com/xdev-asia-labs/xClaw.git
cd xClaw

# Copy file env mẫu
cp .env.example .env

Mở file .env và cấu hình các API keys:

# Server
PORT=3000
HOST=0.0.0.0

# Database
DATABASE_URL=postgresql://xclaw:xclaw@postgres:5432/xclaw
MONGODB_URL=mongodb://xclaw:xclaw@mongodb:27018/xclaw
REDIS_URL=redis://redis:6379

# JWT
JWT_SECRET=your-super-secret-key-change-me-in-production

# LLM Provider (chọn 1 hoặc nhiều)
LLM_PROVIDER=openai
OPENAI_API_KEY=sk-xxxxx

# Hoặc dùng Anthropic
# LLM_PROVIDER=anthropic
# ANTHROPIC_API_KEY=sk-ant-xxxxx

# Hoặc dùng Ollama (local, miễn phí)
# LLM_PROVIDER=ollama
# OLLAMA_BASE_URL=http://host.docker.internal:11434/v1

# CORS
CORS_ORIGINS=http://localhost:3001,http://localhost:5173

3.3. Khởi chạy với Docker Compose

docker compose up --build

Docker Compose sẽ khởi chạy 5 services:

ServicePortMô tả
xclaw3000API server (Hono)
web3001Frontend (React + Nginx)
postgres5432PostgreSQL 18
mongodb27018MongoDB 7
redis6379Redis 8

3.4. Đăng nhập

Truy cập http://localhost:3001 và đăng nhập với tài khoản mặc định:

Email:    [email protected]
Password: password123

⚠️ Quan trọng: Đổi mật khẩu ngay sau khi đăng nhập lần đầu trong môi trường production.

3.5. (Tuỳ chọn) Cài Ollama cho Local LLM

Nếu muốn chạy LLM local mà không cần API key:

# Cài Ollama
curl -fsSL https://ollama.ai/install.sh | sh

# Pull model
ollama pull qwen2.5:14b

# Hoặc model nhỏ hơn
ollama pull qwen2.5:7b

Server sẽ tự động phát hiện Ollama tại http://localhost:11434.


4. Cấu trúc Monorepo

xClaw được tổ chức dưới dạng monorepo với npm workspaces. Mỗi package đảm nhận một trách nhiệm rõ ràng:

xClaw/
├── packages/
│   ├── shared/          # @xclaw-ai/shared — Foundation types & constants
│   ├── core/            # @xclaw-ai/core — Agent engine, LLM, RAG, workflow
│   ├── db/              # @xclaw-ai/db — Drizzle ORM (PG) + MongoDB driver
│   ├── gateway/         # @xclaw-ai/gateway — Hono HTTP server, REST API, auth
│   ├── server/          # @xclaw-ai/server — Entry point, startup orchestration
│   ├── integrations/    # @xclaw-ai/integrations — 11 service connectors
│   ├── domains/         # @xclaw-ai/domains — 13 industry domain packs
│   ├── skills/          # @xclaw-ai/skills — Built-in skills (defineSkill)
│   ├── skill-hub/       # @xclaw-ai/skill-hub — Marketplace, MCP adapters
│   ├── ml/              # @xclaw-ai/ml — 12 ML algorithms, AutoML
│   ├── cli/             # @xclaw-ai/cli — CLI (commander.js)
│   ├── doc-mcp/         # @xclaw-ai/doc-mcp — Dev Docs MCP server
│   ├── chat-sdk/        # @xclaw-ai/chat-sdk — Chat SDK
│   ├── sandbox/         # @xclaw-ai/sandbox — Sandboxed code execution
│   ├── web/             # React + Tailwind frontend
│   ├── zalo-miniapp/    # Zalo Mini App client
│   └── channels/        # Channel plugins
│       ├── telegram/    # Telegram bot
│       ├── discord/     # Discord bot
│       ├── slack/       # Slack workspace
│       ├── whatsapp/    # WhatsApp Business API
│       ├── zalo/        # Zalo Official Account
│       └── msteams/     # Microsoft Teams
├── xclaw-plugins/       # [submodule] Official plugins
├── xclaw-demo-integration-app/  # [submodule] HIS-Mini demo
├── data/
│   ├── dev-docs/        # Developer documentation knowledge base
│   └── knowledge-packs/ # Data-only plugin packages
├── docs/                # Documentation site (Fumadocs + Next.js)
├── deploy/              # Deployment configs
├── tests/               # Integration tests
├── scripts/             # Build & utility scripts
├── docker-compose.yml
├── Dockerfile
└── package.json

Build Order (Project References)

Các packages có dependency chain rõ ràng, được quản lý qua TypeScript project references:

shared → db → core → integrations → domains → ml → skills → skill-hub → gateway → server

Khi build lần đầu:

npm install
npm run build

Git Submodules

xClaw sử dụng 2 git submodules:

SubmoduleThư mụcMô tả
xclaw-pluginsxclaw-plugins/Official plugin packages (TeeForge.AI, Healthcare)
xclaw-demo-integration-appxclaw-demo-integration-app/HIS-Mini demo — Hospital Information System
# Nếu đã clone mà chưa có submodules
git submodule update --init --recursive

# Cập nhật submodules lên latest
git submodule update --remote --merge

5. Multi-LLM — Kết nối nhiều AI Models

5.1. Providers được hỗ trợ

xClaw hỗ trợ 10 LLM providers, cho phép chuyển đổi linh hoạt giữa các model:

ProviderBiến môi trườngGhi chú
OpenAIOPENAI_API_KEYGPT-4o, GPT-4, GPT-3.5
AnthropicANTHROPIC_API_KEYClaude 3.5 Sonnet, Claude 3 Opus
GoogleGOOGLE_API_KEYGemini Pro, Gemini Flash
GroqGROQ_API_KEYLlama, Mixtral (inference cực nhanh)
MistralMISTRAL_API_KEYMistral Large, Medium
DeepSeekDEEPSEEK_API_KEYDeepSeek V3, Coder
xAIXAI_API_KEYGrok
OpenRouterOPENROUTER_API_KEYGateway đến 100+ models
PerplexityPERPLEXITY_API_KEYSearch-augmented LLM
OllamaOLLAMA_BASE_URLLocal models (miễn phí)

5.2. Chuyển đổi Model qua API

# Xem danh sách models khả dụng
curl -H "Authorization: Bearer <token>" \
  http://localhost:3000/api/models

# Chuyển sang model khác
curl -X PUT -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"provider": "anthropic", "model": "claude-3-5-sonnet-20241022"}' \
  http://localhost:3000/api/models/active

# Pull model Ollama mới
curl -X POST -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen2.5:14b"}' \
  http://localhost:3000/api/models/pull

5.3. Chat với AI

# Gửi tin nhắn (streaming response)
curl -X POST -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "message": "Giải thích kiến trúc microservices",
    "sessionId": "optional-session-id"
  }' \
  http://localhost:3000/api/chat

Hệ thống hỗ trợ:

  • Multi-turn conversations với streaming responses
  • Domain-aware prompting — tự động điều chỉnh persona theo domain đang active
  • RAG-enhanced answers với source citations
  • Quick Start prompts — Summarize, Explain, Translate, Code Review, Write Email, Analyze Data

6. RAG Pipeline — Upload, Embedding, Semantic Search

6.1. Luồng hoạt động

RAG (Retrieval-Augmented Generation) pipeline trong xClaw hoạt động theo 3 bước:

┌──────────────┐    ┌──────────────┐    ┌──────────────┐
│  1. Upload   │───▶│ 2. Chunking  │───▶│ 3. Embedding │
│  Document    │    │ & Processing │    │ & Indexing   │
└──────────────┘    └──────────────┘    └──────────────┘
                                               │
                                               ▼
┌──────────────┐    ┌──────────────┐    ┌──────────────┐
│  6. Generate │◀───│ 5. Retrieve  │◀───│ 4. Query     │
│  Response    │    │ Top-K Chunks │    │ Embedding    │
└──────────────┘    └──────────────┘    └──────────────┘

6.2. Upload Document

# Upload một file (PDF, DOCX, TXT, MD)
curl -X POST -H "Authorization: Bearer <token>" \
  -F "[email protected]" \
  http://localhost:3000/api/knowledge/upload

Hệ thống sẽ tự động:

  1. Parse document (hỗ trợ PDF, DOCX, TXT, Markdown)
  2. Chunk nội dung thành các đoạn nhỏ (chunking strategies)
  3. Embed mỗi chunk thành vector (OpenAI embeddings hoặc local model)
  4. Index vectors để phục vụ semantic search

6.3. Semantic Search

# Tìm kiếm semantic trên knowledge base
curl -H "Authorization: Bearer <token>" \
  "http://localhost:3000/api/search?q=cách+triển+khai+microservices"

Kết quả trả về top-K chunks có độ tương đồng cao nhất, kèm source citations.

6.4. RAG trong Chat

Khi RAG được bật, mỗi câu hỏi trong chat sẽ:

  1. Được embed thành vector query
  2. Tìm các chunks liên quan trong knowledge base
  3. Inject context vào prompt trước khi gửi đến LLM
  4. LLM trả lời dựa trên context + knowledge → giảm hallucination

7. Workflow Engine — Visual Workflow Builder

7.1. Tổng quan

Workflow Engine cho phép xây dựng các pipeline tự động hóa bằng giao diện kéo-thả (drag-and-drop). Hỗ trợ 16 loại node:

Node TypeChức năng
triggerKhởi chạy thủ công, theo lịch, hoặc webhook
llm-callGọi LLM với prompt template
tool-callThực thi tool đã đăng ký
conditionRẽ nhánh dựa trên điều kiện
switchRẽ nhánh nhiều hướng với cases
loopLặp lại với giới hạn max iterations
mergeGộp nhiều nhánh
transformBiến đổi dữ liệu bằng JavaScript expressions
codeThực thi JavaScript trong sandbox (vm)
http-requestGọi HTTP external
sub-workflowChạy workflow con
waitDelay / sleep
notificationGửi thông báo
outputĐịnh nghĩa output
memory-readĐọc từ agent memory
memory-writeGhi vào agent memory

7.2. Tạo Workflow qua API

# Tạo workflow mới
curl -X POST -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Customer Support Auto-Reply",
    "description": "Tự động trả lời email khách hàng",
    "nodes": [
      {
        "id": "trigger-1",
        "type": "trigger",
        "config": { "triggerType": "webhook" }
      },
      {
        "id": "llm-1",
        "type": "llm-call",
        "config": {
          "prompt": "Phân tích email sau và tạo reply chuyên nghiệp:\n\n{{input.email_body}}",
          "model": "gpt-4o"
        }
      },
      {
        "id": "condition-1",
        "type": "condition",
        "config": {
          "expression": "output.sentiment === \"negative\""
        }
      },
      {
        "id": "notification-1",
        "type": "notification",
        "config": {
          "channel": "slack",
          "message": "⚠️ Khách hàng không hài lòng - cần xử lý thủ công"
        }
      },
      {
        "id": "output-1",
        "type": "output",
        "config": {
          "outputKey": "auto_reply"
        }
      }
    ],
    "edges": [
      { "from": "trigger-1", "to": "llm-1" },
      { "from": "llm-1", "to": "condition-1" },
      { "from": "condition-1", "to": "notification-1", "condition": "true" },
      { "from": "condition-1", "to": "output-1", "condition": "false" }
    ]
  }' \
  http://localhost:3000/api/workflows

7.3. Validate & Execute

# Validate workflow trước khi chạy
curl -X POST -H "Authorization: Bearer <token>" \
  http://localhost:3000/api/workflows/<workflow-id>/validate

# Execute workflow
curl -X POST -H "Authorization: Bearer <token>" \
  -H "Content-Type: application/json" \
  -d '{"input": {"email_body": "Tôi muốn hoàn tiền đơn hàng #12345"}}' \
  http://localhost:3000/api/workflows/<workflow-id>/execute

# Xem lịch sử executions
curl -H "Authorization: Bearer <token>" \
  http://localhost:3000/api/workflows/<workflow-id>/executions

8. Multi-tenant RBAC — Phân quyền đa tổ chức

8.1. Mô hình phân quyền

xClaw triển khai RBAC (Role-Based Access Control) đầy đủ với tenant isolation:

┌─────────────────────────────────────────┐
│              Tenant (Tổ chức)            │
├─────────────────────────────────────────┤
│  Owner ──── 60 permissions (toàn quyền) │
│  Admin ──── 52 permissions              │
│  Member ─── 14 permissions              │
│  Viewer ───  8 permissions (chỉ xem)    │
└─────────────────────────────────────────┘

8.2. 15 nhóm Permission

NhómMô tả
chatGửi tin nhắn, xem lịch sử
sessionsQuản lý phiên chat
knowledgeUpload, xoá documents (RAG)
workflowsTạo, sửa, chạy workflows
integrationsKết nối services bên ngoài
domainsChọn domain pack
settingsCấu hình hệ thống
usersQuản lý users
rolesQuản lý roles & permissions
tenantsQuản lý tenants
modelsQuản lý LLM models
mlMachine Learning features
agentsQuản lý agent configs
webhooksWebhook management
mcpMCP server management

8.3. Tenant Isolation

Mỗi tenant là một tổ chức độc lập:

  • Data isolation — dữ liệu giữa các tenants hoàn toàn tách biệt
  • Config isolation — mỗi tenant có settings riêng (LLM provider, active domain, etc.)
  • User management — users thuộc về tenant, có role riêng trong từng tenant

8.4. OAuth2 Authentication

Hỗ trợ 3 OAuth2 providers:

# Login thường
curl -X POST -H "Content-Type: application/json" \
  -d '{"email": "[email protected]", "password": "xxx"}' \
  http://localhost:3000/auth/login

# OAuth2 (Google, GitHub, Discord)
# Redirect user đến:
GET http://localhost:3000/auth/oauth2/google
GET http://localhost:3000/auth/oauth2/github
GET http://localhost:3000/auth/oauth2/discord

9. Domain Packs — 13 lĩnh vực chuyên biệt

Domain Packs cho phép AI agent chuyển đổi persona và chuyên môn theo từng ngành:

#DomainMô tảUse Case
1GeneralVersatile general-purpose assistantQ&A, tóm tắt, dịch thuật
2DeveloperCode review, debugging, architectureReview PR, giải thích code
3HealthcareClinical support, drug interactions, ICDTra cứu thuốc, mã ICD-10
4FinanceFinancial analysis, trading, riskPhân tích tài chính, dự báo
5MarketingCampaign planning, content, analyticsLên kế hoạch marketing
6EducationTutoring, curriculum, assessmentGia sư, thiết kế bài giảng
7ResearchLiterature review, methodologyTổng quan tài liệu nghiên cứu
8DevOpsCI/CD, infrastructure, monitoringTư vấn pipeline, K8s
9LegalContract review, complianceRà soát hợp đồng
10HRRecruitment, policiesTuyển dụng, chính sách HR
11SalesLead management, CRM, forecastingQuản lý leads, dự báo doanh thu
12E-commerceProduct, inventory, customer supportQuản lý sản phẩm, CSKH
13MLModel training, evaluation, deploymentTrain model, đánh giá metrics

Chuyển Domain qua API

# Xem danh sách domain packs
curl -H "Authorization: Bearer <token>" \
  http://localhost:3000/api/domains

# Domain pack sẽ tự động điều chỉnh:
# - System prompt phù hợp ngành
# - Bộ tools chuyên biệt
# - Knowledge base riêng (nếu có)
# - Response format tối ưu cho lĩnh vực

10. Chat Channels — Kết nối đa nền tảng

10.1. Channels được hỗ trợ

ChannelPhương thứcCấu hình cần thiết
TelegramBot pollingBot token từ @BotFather
DiscordBot + GatewayBot token từ Developer Portal
SlackWeb API + EventsBot token (xoxb-) + Signing Secret
WhatsAppCloud API webhookMeta Business Suite + Phone Number ID
Zalo OAOA API v3 webhookdevelopers.zalo.me + Access Token
Microsoft TeamsBot FrameworkAzure AD App + Bot Connector
WebChatEmbeddable widgetRoute /embed/chat
WebhookCustom HTTPPOST endpoint + secret key

10.2. Cấu hình Telegram Bot

Ví dụ kết nối Telegram:

  1. Tạo bot qua @BotFather trên Telegram
  2. Lấy bot token
  3. Thêm vào .env:
TELEGRAM_BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrsTUVwxyz
  1. Restart service:
docker compose restart xclaw

Bot sẽ tự động kết nối và sẵn sàng nhận tin nhắn.

10.3. Embeddable WebChat

Nhúng chat widget vào website bất kỳ:

<iframe
  src="http://your-xclaw-domain:3001/embed/chat"
  width="400"
  height="600"
  frameborder="0">
</iframe>

11. MCP Protocol — Model Context Protocol

11.1. MCP là gì?

MCP (Model Context Protocol) là một giao thức chuẩn hóa cho phép AI models kết nối và sử dụng tools/resources bên ngoài. xClaw triển khai:

  • MCP Server Discovery — tự động phát hiện MCP servers trong mạng
  • Tool Execution — gọi tools từ MCP servers
  • Dev Docs Knowledge Base — MCP-powered documentation với full-text search

11.2. Dev Docs MCP Server

Package @xclaw-ai/doc-mcp cung cấp MCP server cho developer documentation:

  • Upload documentation dạng Markdown
  • Full-text search trên knowledge base
  • Web management UI
  • Multi-category organization (Getting Started, API Reference, Guides, etc.)
# Xem MCP servers đã đăng ký
curl -H "Authorization: Bearer <token>" \
  http://localhost:3000/api/mcp/servers

12. ML/AutoML — 12 thuật toán ML tích hợp

12.1. Algorithms

Package @xclaw-ai/ml tích hợp sẵn 12 thuật toán ML:

Regression:

  • Linear Regression
  • Logistic Regression

Trees:

  • Decision Tree
  • Random Forest
  • Gradient Boosting

Instance-based:

  • K-Nearest Neighbors (KNN)
  • Support Vector Machine (SVM)

Probabilistic:

  • Naive Bayes

Clustering:

  • K-Means
  • DBSCAN

Dimensionality Reduction:

  • PCA (Principal Component Analysis)

Anomaly Detection:

  • Isolation Forest

12.2. Sử dụng qua API

# Liệt kê algorithms khả dụng
curl -H "Authorization: Bearer <token>" \
  http://localhost:3000/api/ml/algorithms

13. Integrations — 11 Service Connectors

xClaw cung cấp sẵn 11 integrations để kết nối với services phổ biến:

NhómServiceChức năng
EmailGmailGửi/nhận email qua API
ProductivityGoogle CalendarQuản lý sự kiện, lịch
ProductivityNotionDatabase & page management
DeveloperGitHubRepos, issues, pull requests
MessagingTelegram APIBot messaging
MessagingSlack APIChannel & DM messaging
MessagingiMessageApple iMessage bridge
SearchBrave SearchWeb search
SearchTavily SearchAI-optimized web search
AIHuggingFaceModel inference & datasets
AIWeights & BiasesExperiment tracking

Mỗi integration được đóng gói trong package @xclaw-ai/integrations với interface chuẩn — dễ dàng thêm connector mới.


14. Monitoring & Observability

14.1. System Metrics

# Lấy metrics real-time
curl -H "Authorization: Bearer <token>" \
  http://localhost:3000/api/monitoring/metrics

Metrics bao gồm:

  • Uptime — thời gian hoạt động
  • Memory & CPU — resource usage
  • Requests/minute — throughput
  • LLM call stats — số lượng calls, latency, tokens used
  • Workflow stats — executions, success/failure rate

14.2. Audit Logs

Mọi hành động của users đều được log với tenant isolation:

# Xem audit trail
curl -H "Authorization: Bearer <token>" \
  http://localhost:3000/api/monitoring/audit
  • TTL: 90 ngày (tự động cleanup)
  • Stored in MongoDB

14.3. System Logs

Structured application logs với text search:

# Tìm kiếm logs
curl -H "Authorization: Bearer <token>" \
  "http://localhost:3000/api/monitoring/logs?search=error&level=error"
  • TTL: 30 ngày
  • Hỗ trợ filtering theo level, timestamp, keyword

14.4. Dashboard API

# Combined dashboard — metrics + errors + audit trail
curl -H "Authorization: Bearer <token>" \
  http://localhost:3000/api/monitoring/dashboard

15. API Reference tổng hợp

Public Endpoints

GET  /health                       # Health check + uptime
POST /auth/login                   # Login → JWT token
POST /auth/register                # Đăng ký user mới
POST /auth/oauth2/:provider        # OAuth2 flow (google, github, discord)

Protected Endpoints (cần Authorization: Bearer <token>)

# Chat
POST /api/chat                     # Gửi tin nhắn, streaming response

# Knowledge (RAG)
GET  /api/knowledge                # Liệt kê documents
POST /api/knowledge/upload         # Upload document
GET  /api/search                   # Semantic search

# Models
GET  /api/models                   # Danh sách LLM models
POST /api/models/pull              # Pull Ollama model mới
PUT  /api/models/active            # Chuyển active model

# Workflows
GET  /api/workflows                # Liệt kê workflows
POST /api/workflows                # Tạo workflow
POST /api/workflows/:id/validate   # Validate workflow
POST /api/workflows/:id/execute    # Execute workflow
GET  /api/workflows/:id/executions # Lịch sử executions

# Monitoring
GET  /api/monitoring/metrics       # System metrics
GET  /api/monitoring/audit         # Audit logs
GET  /api/monitoring/logs          # System logs
GET  /api/monitoring/dashboard     # Combined dashboard

# Admin & RBAC
GET  /api/rbac/roles               # Roles & permissions
GET  /api/tenants                  # Tenants
GET  /api/settings                 # System settings
GET  /api/integrations             # Integrations
GET  /api/domains                  # Domain packs
GET  /api/ml/algorithms            # ML algorithms
GET  /api/mcp/servers              # MCP servers

16. Development — Phát triển local

16.1. Chạy với Docker (khuyến nghị)

# Build & start tất cả services
docker compose up --build

# Chỉ rebuild backend
docker compose up --build xclaw

# Xem logs
docker compose logs -f xclaw

# Dừng tất cả
docker compose down

16.2. Chạy không Docker

Yêu cầu: Node.js ≥ 20, npm ≥ 10, PostgreSQL, MongoDB, Redis đã chạy sẵn.

# 1. Install dependencies
npm install

# 2. Cấu hình
cp .env.example .env
# Sửa .env với database URLs và API keys

# 3. Build packages (lần đầu)
npm run build

# 4. Chạy database migrations
npm run db:migrate

# 5. Start dev servers
npm run dev              # API server + web frontend (concurrent)

# Hoặc chạy từng phần:
npm run dev:server       # API server (tsx --watch, hot reload)
npm run dev:web          # Vite dev server (HMR)
npm run dev:docs         # Documentation site (Next.js)

16.3. Các lệnh hữu ích

# Database
npm run db:generate      # Generate Drizzle migration từ schema changes
npm run db:migrate       # Chạy pending migrations
npm run db:studio        # Mở Drizzle Studio (visual DB browser)

# Quality
npm run test             # Chạy tests (vitest)
npm run lint             # Lint tất cả packages

# CLI
npm run cli -- <command> # Chạy xClaw CLI

17. Deploy Production với Docker

17.1. Docker multi-stage build

xClaw sử dụng Dockerfile multi-stage để tối ưu image size:

# Build production image
docker build -t xclaw:latest .

# Hoặc dùng docker-compose production
docker compose -f docker-compose.prod.yml up -d

17.2. Checklist trước khi deploy

  • Đổi JWT_SECRET thành chuỗi ngẫu nhiên mạnh (≥ 32 ký tự)
  • Đổi mật khẩu default [email protected]
  • Cấu hình CORS_ORIGINS chỉ cho phép domain production
  • Cấu hình database passwords mạnh
  • Bật HTTPS (reverse proxy: Nginx, Caddy, hoặc Cloudflare Tunnel)
  • Cấu hình backup cho PostgreSQL và MongoDB
  • Set NODE_ENV=production
  • Giới hạn CORS_ORIGINS chỉ domain thực tế

17.3. Reverse Proxy (Nginx)

server {
    listen 443 ssl http2;
    server_name xclaw.yourdomain.com;

    ssl_certificate     /etc/ssl/certs/fullchain.pem;
    ssl_certificate_key /etc/ssl/private/privkey.pem;

    # Frontend
    location / {
        proxy_pass http://localhost:3001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    # API
    location /api/ {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    # Auth
    location /auth/ {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # Health check
    location /health {
        proxy_pass http://localhost:3000;
    }
}

18. Kết luận

xClaw là một AI Agent Platform toàn diện, mang đến:

  • Flexibility — Multi-LLM, multi-domain, multi-channel
  • Extensibility — Plugin system, MCP Protocol, defineSkill API
  • Security — Multi-tenant RBAC, OAuth2, audit logging
  • Productivity — Visual Workflow Builder, RAG Pipeline, AutoML
  • Production-ready — Docker deployment, monitoring, structured logging

Platform phù hợp cho:

  • Xây dựng chatbot nội bộ doanh nghiệp
  • Customer support automation
  • AI-powered workflow cho từng ngành
  • Research & experimentation với nhiều LLM models
  • On-premise AI deployment (Ollama)

Links:

DUY TRAN
Tác giả

DUY TRAN

Pursuing an AI-first mindset and intelligent system architecture. I build solutions by combining technology, creativity, and the ability to see structure in chaos — the foundation for becoming a Solution Architect.

Bình luận