Nhảy tới nội dung

🏗️ Kiến trúc hệ thống

Khám phá cách OpenClaw vận hành "under the hood" - từ Gateway trung tâm đến các AI Agents độc lập.

🗺️ Tổng quan (High-Level)

OpenClaw hoạt động theo mô hình Local-First Architecture. Mọi xử lý logic, lưu trữ dữ liệu và thực thi lệnh đều diễn ra trên máy của bạn (hoặc server của bạn), không phụ thuộc vào cloud server của bên thứ 3 (ngoại trừ LLM API).

graph TD
User[👤 User / Client] -->|WebSocket / HTTP| Network[🌐 Channel Network]

subgraph "Local Machine / VPS (Safe Zone)"
direction TB
Network -->|Auth & Router| Gateway[🛡️ Gateway Core]

Gateway -->|Orchestrate| Agent1[🤖 Agent: Main]
Gateway -->|Orchestrate| Agent2[🤖 Agent: DevOps]

Agent1 <-->|Read/Write| FS[📂 File System]
Agent1 <-->|Store| DB[(🧠 Vector DB)]
Gateway -->|Monitor| Logs[📝 System Logs]
end

Agent1 -.->|Inference| Cloud[☁️ Claude API]

classDef safe fill:#e6fffa,stroke:#00b894,color:#000;
classDef cloud fill:#fff0f6,stroke:#e84393,color:#000;

class Gateway,Agent1,Agent2,FS,DB,Logs one;

🧩 Các thành phần cốt lõi

Bộ não trung tâm

Gateway là một process **Node.js** chạy daemon (ngầm) 24/7. Nó đóng vai trò như hệ điều hành của Bot.

  • 🔌 Connectivity: Quản lý WebSocket Server (port 18789).
  • 🚦 Routing: Điều phối tin nhắn từ Client đến đúng Agent.
  • 💾 Persistence: Tự động lưu trữ chat history vào SQLite/PostgreSQL.
  • 🔐 Security: Kiểm soát Permissions và Sandbox rules.
Technical Specs

Protocol: WebSocket + HTTP REST

DB: Better-SQLite3 (Default)

Process: Single-threaded Event Loop


🔄 Data Flow: Một tin nhắn đi đâu?

Hành trình của tin nhắn "Hello OpenClaw" từ lúc bạn gửi đến khi nhận phản hồi:

### 1. Reception (Tiếp nhận)
ghi chú
Channel Adapter (ví dụ Telegram Bot) nhận webhook từ Server Telegram -> Chuyển thành internal JSON format -> Gửi vào **Gateway WebSocket**.

2. Processing (Xử lý)

ghi chú
**Gateway** xác thực User ID -> Router check xem User đang nói chuyện với Agent nào -> Forward payload tới **Active Agent**.

3. Cognition (Suy nghĩ)

ghi chú
**Agent** thực hiện RAG (Retrieval Augmented Generation): 1. Lục lại Vector DB (Ký ức cũ). 2. Gom context hiện tại. 3. Gửi prompt + context lên **Claude API**.

4. Action & Response (Hành động)

ghi chú
Nếu cần hành động (ví dụ: Chạy lệnh), Agent sẽ yêu cầu **Gateway** thực thi (cần User Approval nếu nhạy cảm). Cuối cùng, câu trả lời (Text) được gửi ngược dòng: Agent -> Gateway -> Telegram -> **User**.

🔒 Security Model: "Pairing"

Tại sao OpenClaw an toàn hơn các giải pháp khác?

sequenceDiagram
participant U as 👤 User
participant G as 🛡️ Gateway
participant S as 🐚 System Shell

U->>G: "Xóa file data.txt"
G->>G: Check Permission

opt Dangerous Command detected
G-->>U: ⚠️ Command: rm data.txt. DUYỆT? (Y/n)
U->>G: Y (Approve)
end

G->>S: Execute rm data.txt
S-->>G: Done
G-->>U: ✅ Đã xóa
Human-in-the-loop
OpenClaw không bao giờ tự ý thực hiện các lệnh nguy hiểm (như delete file, upload data) mà không hỏi ý kiến bạn trước (trừ khi bạn cấu hình "Auto-Approve" cho safe commands).