微信扫码
添加专属顾问
我要投稿
用Go语言复现OpenClaw,打造更健壮、高性能的AI助手goclaw,兼容原生态技能系统。核心内容: 1. goclaw项目背景与OpenClaw的关系 2. Go语言实现的优势与特性 3. 系统架构与核心功能设计
这两周 openclaw 非常的火热,大家都在探索它的使用方法和用应用场景。我也对它产生了浓厚的兴趣,深入研究了它 也有一些网易在研究它的架构设计和实现细节,贡献了 nanbot、miniclaw 等个别的项目。我也花了一些时间研究了 openclaw 的代码,并基于它的设计理念,使用 Go 语言重新实现了一个类似的项目,命名为 goclaw。
goclaw 使用和 openclaw 相同的配置文件、技能系统和类似的 cli 命令,并且兼容 openclaw 的技能生态。同时,goclaw 也借鉴了 openclaw 的核心设计理念,但在实现细节上做了一些不同的选择,以充分利用 Go 语言的优势,构建了一个更加健壮、高性能的 AI 助手。
GoClaw 是一个用 Go 语言编写的个人 AI 助手,灵感来自 OpenClaw(原 Clawdbot/Moltbot)。它运行在本地服务器上,通过 WebSocket/HTTP 暴露服务,支持多种消息 channel(Telegram、WhatsApp、飞书、QQ、Slack 等)接入。
| 语言 | |
| 架构 | |
| 部署 | |
| 扩展性 | |
| 可靠性 |
┌──────────────────────────────────────────────────────────────────────────────┐
│ GoClaw 系统架构 │
├──────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 输入Channel │── →│ 网关服务器 │───→│ Agent Loop │───→│ LLM/工具 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │ │
│ │ │ │ │ │
│ v v v v │
│ Telegram/WhatsApp MessageBus SessionManager Providers │
│ 飞书/QQ/Slack 串行队列 JSONL存储 OpenAI/AI... │
│ │
└──────────────────────────────────────────────────────────────────────────────┘
用户消息 → Channel适配器 → 网关服务器 → Agent Loop → LLM 调用 → 工具执行 → 响应返回
Agent Loop 是 GoClaw 的大脑,负责思考、规划和执行。它受到 OpenClaw 的 PI Agent 架构启发。
PI = 极简编码代理(Mario Zechner 创建)
"LLMs 本身就擅长编写和运行代码,不需要过多的包装"
┌───────────────────────────────────────────────────────────────────┐
│ GoClaw Agent Loop │
├───────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ 接收消息 │────→│ 构建上下文 │────→│ LLM 调用 │────→│ 执行工具 │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
│ │ │
│ v │
│ ┌─────────────────┐ │
│ │ 工具结果返回 │ │
│ └─────────────────┘ │
│ │ │
│ v │
│ ┌─────────────────┐ │
│ │ 反思机制判断 │ │
│ │ (可选) │ │
│ └─────────────────┘ │
│ │ │
│ ┌─────────────┴────────────┐ │
│ v v │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ 继续迭代 │ │ 返回响应 │ │
│ └─────────────┘ └─────────────┘ │
│ │
└───────────────────────────────────────────────────────────────────┘
文件: agent/context.go
type ContextBuilder struct {
memory *MemoryStore
workspace string
agentID string
defaultModel string
provider string
}
职责:
上下文压缩策略:
// agent/loop.go:571
func (l *Loop) compressSession(sess *session.Session) {
// 保留最近 10 轮对话
// 保留所有系统消息
// 丢弃早期历史,保留关键上下文
}
文件: agent/tools/registry.go
核心工具集(对应 PI 的四个工具):
read_file |
read |
|
write_file |
write |
|
edit_file |
edit |
|
run_shell |
bash |
GoClaw 扩展工具:
browser_* - 浏览器操作 (Chrome DevTools Protocol)smart_search - 混合搜索 (向量 + FTS5)spawn - 子代理管理message - 消息发送工具执行与重试:
// agent/loop.go:526
func (l *Loop) executeToolWithRetry(ctx context.Context, toolName string, params map[string]interface{}) (string, error) {
// 网络错误自动重试
// 错误分类与降级建议
// 结构化错误返回给 LLM 自我校正
}
文件: agent/reflection.go
解决传统 Agent "何时停止" 的难题:
type Reflector struct {
config *ReflectionConfig
provider providers.Provider
workspace string
}
type TaskReflection struct {
Status TaskStatus // "completed", "in_progress", "failed", "blocked"
Confidence float64 // 0.0 - 1.0
CompletedSteps []string // 已完成步骤
RemainingSteps []string // 剩余步骤
Reasoning string // 思考过程
NextAction string // 下一步行动
}
反思流程:
// agent/loop.go:487
reflection, reflectErr := l.reflector.Reflect(ctx, userRequest, reflectionHistory)
if l.reflector.ShouldContinueIteration(reflection, iteration, l.maxIteration) {
continuePrompt = l.reflector.GenerateContinuePrompt(reflection)
continue
}
与传统 max-iterations 的区别:
文件: agent/error_classifier.go
type ErrorClassifier struct {
authPatterns []string
rateLimitPatterns []string
timeoutPatterns []string
billingPatterns []string
}
错误分类:
auth - 认证错误(需要故障转移)rate_limit - 限流错误(需要冷却)timeout - 超时错误(可重试)billing - 计费错误(需要故障转移)tool - 工具错误(返回给 LLM)智能降级策略:
// agent/loop.go:628
func (l *Loop) formatToolError(toolName string, params map[string]interface{}, err error) string {
// 根据工具类型和错误提供具体建议
// 例如:write_file 失败 → 建议输出到控制台
// 例如:browser 失败 → 建议使用 web_fetch
}
OpenClaw PI (TypeScript) - 极简循环:
while (true) {
const response = await streamCompletion(model, context);
if (!response.toolCalls?.length) break;
for (const call of response.toolCalls) {
const result = await executeToolCall(call, context);
context.messages.push(result);
}
}
GoClaw (Go) - 增强循环:
for iteration < l.maxIteration {
response, err := l.provider.Chat(ctx, messages, tools)
if len(response.ToolCalls) > 0 {
for _, tc := range response.ToolCalls {
result, err := l.executeToolWithRetry(ctx, tc.Name, tc.Params)
}
continue
}
if response.Content == "" && l.reflector != nil {
reflection := l.reflector.Reflect(ctx, userRequest, history)
if !l.reflector.ShouldContinueIteration(reflection, iteration, l.maxIteration) {
break
}
continue
}
break
}
OpenClaw PI - TypeScript Hooks:
hooks: {
onSessionStart: async (session) => { ... },
onBeforeTurn: async (session) => { ... },
onToolCall: async (tool, params) => { ... },
// 20+ 生命周期钩子
}
GoClaw - Skills System:
type Skill struct {
Name string
Description string
Triggers []string // 触发关键词
Content string // Markdown 指令
Requires []string // 环境依赖 (Gating)
}
文件: gateway/server.go
网关服务器是 GoClaw 的心脏,负责任务/会话协调:
type Server struct {
config *config.GatewayConfig
wsConfig *WebSocketConfig
bus *bus.MessageBus
channelMgr *channels.Manager
sessionMgr *session.Manager
connections map[string]*Connection
}
核心功能:
MEMORY.md 或 memory/ 文件夹| 向量搜索 | ||
| 关键词搜索 (FTS5) |
文件: memory/search.go
type MemoryManager struct {
store Store
provider EmbeddingProvider
cache map[string]*VectorEmbedding
}
|
沙箱 |
|
| 直接主机 | |
| 远程设备 |
read_file - 读取文件write_file - 写入文件edit_file - 编辑文件list_files - 列出目录browser_navigate - 导航到 URLbrowser_screenshot - 截取页面截图browser_execute_script - 执行 JavaScriptbrowser_click - 点击元素browser_fill_input - 填写输入框browser_get_text - 获取页面文本文件: agent/tools/browser.go
type BrowserTool struct {
headless bool
timeout time.Duration
outputDir string
}
{
"agents": {
"main": {
"allowlist": [
{ "pattern": "/usr/bin/npm", "lastUsedAt": 1706644800 },
{ "pattern": "/opt/homebrew/bin/git", "lastUsedAt": 1706644900 }
]
}
}
}
jq、grep、cut、sort、uniq、head、tail、tr、wc 等
# 这些在执行前会被拒绝:
npm install $(cat /etc/passwd) # 命令替换
cat file > /etc/hosts # 重定向
rm -rf / || echo "failed" # 用 || 链接
(sudo rm -rf /) # 子shell
文件: providers/failover.go
type FailoverProvider struct {
primary Provider
fallback Provider
circuitBreaker *CircuitBreaker
errorClassifier types.ErrorClassifier
}
特性:
workspace/skills/workspace/.goclaw/skills//skills/./skills/(当前目录,优先级最高)goclaw/
├── agent/ # Agent 核心逻辑
│ ├── loop.go # Agent 循环(含重试逻辑)
│ ├── context.go # 上下文构建器
│ ├── memory.go # 记忆系统
│ ├── skills.go # 技能加载器
│ ├── reflection.go # 反思机制
│ ├── error_classifier.go # 错误分类器
│ └── tools/ # 工具系统
│ ├── registry.go
│ ├── browser.go
│ ├── filesystem.go
│ ├── shell.go
│ └── ...
├── channels/ # 消息通道
├── bus/ # 消息总线
├── config/ # 配置管理
├── providers/ # LLM 提供商
│ ├── failover.go
│ ├── circuit.go
│ └── ...
├── session/ # 会话管理
├── memory/ # 记忆存储
├── cli/ # 命令行界面
└── gateway/ # 网关服务器
# 构建
go build -o goclaw .
# 配置
cat > config.json << EOF
{
"agents": {
"defaults": {
"model": "openrouter:anthropic/claude-opus-4-5",
"max_iterations": 15
}
},
"providers": {
"openrouter": {
"api_key": "your-key"
}
},
"channels": {
"telegram": {
"enabled": true,
"token": "your-bot-token"
}
}
}
EOF
# 启动
./goclaw start
GoClaw 继承了 OpenClaw 的核心设计理念,同时利用 Go 语言的特性构建了一个更加健壮、高性能的 AI 助手。
设计原则:
"这种简单性可能是优势也可能是陷阱,取决于你的视角。但我总是倾向于可解释的简单性,而不是复杂的意大利面条代码。"
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-02-16
刚刚,OpenClaw 被 OpenAI 收编!
2026-02-16
我与OpenClaw:从拒绝到皈依
2026-02-15
OpenClaw 的「中国套件」来了:飞书钉钉企微QQ一锅端
2026-02-15
🦞 OpenClaw 二月狂飙:从能用变成“真·好用”的7版连更
2026-02-14
让 OpenClaw 一键超简单部署,用 MonsterClaw 过年赚大钱
2026-02-14
MiniMax M2.5:龙虾御用,Agent 永不停机
2026-02-14
如果你还在犹豫要不要尝试 OpenClaw,试试这个 App 一键部署方案
2026-02-14
2.1K Star!这个 Claude Skills 技能库,给 AI 编程助手装上了 66 颗专家大脑!
2025-11-19
2026-01-27
2026-01-29
2026-01-30
2026-01-12
2026-02-06
2025-12-22
2025-12-10
2026-01-28
2025-12-23
2026-02-11
2026-02-05
2026-01-28
2026-01-26
2026-01-21
2026-01-21
2026-01-20
2026-01-16