AI 自动化工作流平台技术详解
OpenClaw 采用分层架构和模块化设计,遵循"高内聚、低耦合"原则。 整体架构分为四层:用户交互层、网关层、核心服务层、模型适配层。
Gateway 是 OpenClaw 的中枢神经系统,负责所有关键协调工作。
{
"gateway": {
"port": 18789,
"host": "127.0.0.1",
"cors": {
"enabled": true,
"origins": ["http://localhost:18789"]
},
"auth": {
"enabled": true,
"type": "bearer",
"token": "${OPENCLAW_AUTH_TOKEN}"
},
"rpc": {
"timeout": 30000,
"retries": 3,
"maxConnections": 100
}
}
}
Agent 是 AI 助手的"大脑",负责任务规划和决策。
async function executeTask(goal: string) {
const context = createContext();
while (!isGoalAchieved(context)) {
// Thought: 推理当前状态
const thought = await reason(context);
// Action: 选择并执行动作
const action = selectAction(thought);
const observation = await execute(action);
// Update context with observation
context.update({ thought, action, observation });
// Check for errors and retry if needed
if (hasError(observation)) {
await handleError(context);
}
}
return formatResult(context);
}
Tools 是 Agent 的"手",提供具体的执行能力。
| 工具类别 | 功能描述 | API 示例 | 使用场景 |
|---|---|---|---|
| Browser | 浏览器自动化控制 | browser.navigate() |
网页抓取、表单提交 |
| FileSystem | 文件读写和管理 | fs.readFile() |
批量处理、格式转换 |
| Terminal | 系统命令执行 | terminal.exec() |
安装包、运行脚本 |
| WebAPI | HTTP 请求调用 | http.get() |
REST API、Webhook |
| Memory | 记忆存储检索 | memory.search() |
向量搜索、上下文 |
Memory 系统让 OpenClaw 具备"越用越聪明"的能力。
当前会话的上下文,保留最近 10-20 轮对话,存储在内存中,会话结束即清除。
SQLite 向量数据库存储重要事实和用户偏好,支持语义搜索和模糊匹配。
当前任务的中间状态和临时变量,任务完成后选择性持久化。
memory:
short_term:
type: in-memory
max_messages: 20
token_limit: 8192
long_term:
type: sqlite-vector
path: ~/.openclaw/memory/
index_config:
dimensions: 1536
metric: cosine
ef_construction: 200
working:
type: task-scoped
persist_on_complete: true
完全运行在用户设备上,隐私最佳,适合个人使用。
运行在云服务器上,可通过互联网访问,适合团队使用。
敏感操作本地执行,计算密集型任务云端处理。
version: '3.8'
services:
openclaw:
image: openclaw/openclaw:latest
container_name: openclaw
restart: unless-stopped
ports:
- "18789:18789"
volumes:
- ./data:/root/.openclaw
- ./logs:/var/log/openclaw
- ./workspace:/app/workspace
environment:
- NODE_ENV=production
- OPENCLAW_API_KEY=${OPENCLAW_API_KEY}
- OPENCLAW_PORT=18789
- OPENCLAW_HOST=0.0.0.0
- TZ=Asia/Shanghai
networks:
- openclaw-net
healthcheck:
test: ["CMD", "openclaw", "health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./ssl:/etc/nginx/ssl
depends_on:
- openclaw
networks:
- openclaw-net
networks:
openclaw-net:
driver: bridge
OpenClaw 具备强大的系统执行能力,不当配置可能导致严重安全风险。 生产环境必须启用以下安全措施。
{
"security": {
"authentication": {
"enabled": true,
"method": "bearer",
"tokenRotationDays": 90
},
"authorization": {
"tools": {
"terminal": {
"blockedCommands": [
"rm -rf /",
"mkfs.*",
"dd if=/dev/zero"
],
"requireConfirmation": [
"sudo",
"rm -rf",
"chmod"
]
},
"filesystem": {
"readOnlyPaths": ["/etc", "/usr"],
"writablePaths": ["~/projects", "/tmp"],
"maxFileSizeMB": 100
}
}
},
"sandbox": {
"enabled": true,
"type": "docker",
"network": {
"enabled": true,
"allowedHosts": ["api.openai.com"]
},
"resources": {
"maxMemoryMB": 2048,
"maxCPUPercent": 50
}
}
}
}
| 缓存层级 | 缓存内容 | 过期策略 | 命中率目标 |
|---|---|---|---|
| L1: 内存缓存 | 会话上下文、常用配置 | LRU, 10 分钟 | > 90% |
| L2: Redis 缓存 | 工具执行结果、API 响应 | TTL, 1 小时 | > 70% |
| L3: 磁盘缓存 | 大型文件、历史数据 | 按需清理 | > 50% |
{
"performance": {
"concurrency": {
"maxParallelTasks": 10,
"maxQueueSize": 100,
"taskTimeout": 300000
},
"pooling": {
"browserPoolSize": 5,
"connectionPoolSize": 20,
"reuseConnections": true
},
"optimization": {
"enableCompression": true,
"minifyResponses": true,
"batchRequests": true
}
}
}