openclaw和opencode组合使用的完整步骤

Written by

in

文章目录
  • 2026 年初,AI 编程工具迎来了爆发式增长。OpenClaw 和 OpenCode 作为两大明星项目,分别代表了不同的技术路线: 工具 定位 核心能力 OpenClaw 自主 AI Agent 24/7 后台运行、多平台消息路由、任务编排 OpenCode 终端编程助手 代码生成、重构、LSP 支持、75+ 模型 单独使用的问题: OpenClaw 虽然能规划任务,但代码生成能力有限 OpenCode 虽然编码能力强,但需要人工触发 组合后的优势: OpenClaw(大脑)→ 规划任务 → 调用 OpenCode(执行器)→ 生成代码 → 自动验证 → 提交 PR 💡 核心思想:让 OpenClaw 做决策和编排,让 OpenCode 专注写代码,实现真正的自动化开发。
  • AI 编程工具正在重塑软件开发的方式。OpenClaw + OpenCode 的组合让我们看到了"AI 工程团队"的雏形。 但这套工具链也有局限——它适合执行明确的需求,不适合探索未知的问题。关键还是把需求想清楚——这是程序员的工作,AI 无法替代。 到此这篇关于openclaw和opencode组合使用的文章就介绍到这了,更多相关openclaw和opencode组合使用内容请搜索风君子博客以前的文章或继续浏览下面的相关文章,希望大家以后多多支持风君子博客!
  • 目录
    • 前言:为什么要组合使用?
    • 核心概念:大脑 + 执行器
      • 架构图解
      • 工作流程
    • 环境准备与安装
      • 系统要求
      • 步骤 1:安装 OpenClaw
      • 步骤 2:安装 OpenCode
      • 步骤 3:验证安装
    • OpenClaw 配置 OpenCode 技能
      • 安装 OpenCode Controller Skill
      • 配置技能参数
      • 直接编辑配置文件
      • 重启 Gateway
    • 实战:自动化开发工作流
      • 场景 1:从零创建项目
      • 场景 2:自动修复 Bug
      • 场景 3:代码审查与重构
    • 踩坑记录与解决方案
      • ❌ 问题 1:OpenCode Skill 显示 "not eligible"
      • ❌ 问题 2:任务执行超时
      • ❌ 问题 3:API 密钥错误
      • ❌ 问题 4:Docker 容器无法访问 OpenCode
      • ❌ 问题 5:代码生成质量不佳
    • 进阶:GitHub 自动化集成
      • 配置 GitHub 集成
      • 自动化工作流示例
        • 1. Issue 自动分类
        • 2. PR 自动审查
        • 3. 每日开发简报
    • 性能优化与成本控制
      • 模型路由策略
        • 成本监控
          • 缓存策略
          • 总结与展望
            • 核心收获
              • 最佳实践
                • 未来展望
                  • 参考资源
                  • 写在最后

                    2026 年初,AI 编程工具迎来了爆发式增长。OpenClaw 和 OpenCode 作为两大明星项目,分别代表了不同的技术路线:

                    工具 定位 核心能力
                    OpenClaw 自主 AI Agent 24/7 后台运行、多平台消息路由、任务编排
                    OpenCode 终端编程助手 代码生成、重构、LSP 支持、75+ 模型

                    单独使用的问题:

                    • OpenClaw 虽然能规划任务,但代码生成能力有限
                    • OpenCode 虽然编码能力强,但需要人工触发

                    组合后的优势:

                    OpenClaw(大脑)→ 规划任务 → 调用 OpenCode(执行器)→ 生成代码 → 自动验证 → 提交 PR

                    💡 核心思想:让 OpenClaw 做决策和编排,让 OpenCode 专注写代码,实现真正的自动化开发。

                    ┌─────────────────────────────────────────────────────────────┐
                    │                      OpenClaw Gateway                        │
                    │                    (任务路由与编排中心)                      │
                    └──────────────────────┬──────────────────────────────────────┘
                                           │
                            ┌──────────────┼──────────────┐
                            │              │              │
                       Telegram      WhatsApp        Slack
                       Discord       iMessage        ...
                            │              │              │
                            └──────────────┼──────────────┘
                                           │
                                  ┌────────▼────────┐
                                  │  OpenCode Skill │
                                  │  (代码执行器)  │
                                  └────────┬────────┘
                                           │
                            ┌──────────────┼──────────────┐
                            │              │              │
                        代码生成        测试运行        Git 操作
                        文件操作        错误修复        PR 创建

                    1. 需求输入:通过 Telegram/Slack 向 OpenClaw 发送任务
                    2. 任务规划:OpenClaw 分析需求,拆解为子任务
                    3. 技能调用:OpenClaw 调用 OpenCode Skill 执行编码
                    4. 代码生成:OpenCode 在终端完成代码编写
                    5. 结果验证:OpenClaw 检查执行结果
                    6. 反馈输出:通过消息平台返回结果

                    • Node.js: 22+(必需)
                    • 操作系统: macOS / Linux / Windows WSL2
                    • 内存: 建议 4GB+
                    • 网络: 稳定的互联网连接

                    # 方式 1:官方一键安装
                    curl -fsSL https://openclaw.ai/install.sh | bash
                    # 方式 2:npm 安装
                    npm install -g openclaw@latest
                    # 方式 3:pnpm 安装
                    pnpm add -g openclaw@latest

                    ⚠️ 安全警告:不要运行 npm install openclaw(缺少 -g),这不会正确安装 CLI 工具。

                    # 一键安装
                    curl -fsSL https://opencode.ai/install | bash
                    # 或使用包管理器
                    brew install opencode        # macOS
                    scoop install opencode       # Windows
                    npm install -g opencode-ai   # npm

                    # 检查版本
                    openclaw --version
                    # 输出: openclaw/2.5.1 linux-x64 node-v22.12.0
                    opencode --version
                    # 输出: 1.0.150

                    # 安装 OpenCode 控制器技能
                    openclaw plugins install opencode-controller
                    # 或者从 GitHub 安装最新版
                    openclaw plugins install github:openclaw/opencode-controller

                    # 设置 OpenCode 连接信息
                    openclaw config set plugins.opencode-controller.enabled true
                    openclaw config set plugins.opencode-controller.config.apiUrl "http://127.0.0.1:4096"
                    openclaw config set plugins.opencode-controller.config.username "opencode"
                    openclaw config set plugins.opencode-controller.config.password "your-password"
                    # 设置工作目录
                    openclaw config set plugins.opencode-controller.config.sandboxDir "/path/to/sandbox"
                    openclaw config set plugins.opencode-controller.config.credentialsDir "/path/to/credentials"

                    # 编辑配置文件
                    nano ~/.openclaw/config.json
                    {
                      "plugins": {
                        "opencode-controller": {
                          "enabled": true,
                          "config": {
                            "apiUrl": "http://127.0.0.1:4096",
                            "username": "opencode",
                            "password": "openclaw2026",
                            "sandboxDir": "/root/clawd/sandbox",
                            "credentialsDir": "/root/clawd/credentials",
                            "maxIterations": 50,
                            "timeoutMs": 180000
                          }
                        }
                      }
                    }

                    # 重启 OpenClaw Gateway
                    openclaw gateway restart
                    # 检查技能状态
                    openclaw plugins list

                    预期输出

                    ✓ opencode-controller    1.2.3    enabled    eligible and functional

                    需求:创建一个 React + TypeScript 的 CRM 仪表盘

                    操作步骤

                    1. 发送指令(Telegram/Slack): 帮我创建一个 React + TypeScript 的 CRM 仪表盘, 包含客户列表、订单统计、数据可视化图表。 使用 Tailwind CSS 进行样式设计。

                    2. OpenClaw 处理

                      • 分析需求,拆解任务
                      • 调用 OpenCode Skill
                      • 生成项目脚手架
                    3. OpenCode 执行

                      # 创建项目
                      npx create-react-app crm-dashboard --template typescript
                      cd crm-dashboard
                      # 安装依赖
                      npm install tailwindcss @headlessui/react recharts
                      npx tailwindcss init -p
                      # 生成组件代码...
                    4. 结果返回: ✅ 项目已创建: /sandbox/crm-dashboard ✅ 依赖安装完成 ✅ 组件生成: Dashboard, CustomerList, OrderStats, Charts ✅ 开发服务器已启动: http://localhost:3000

                    需求:修复登录页面在 Safari 上的白屏问题

                    操作步骤

                    1. 发送指令: 用户反馈登录页面在 Safari 浏览器显示白屏,请调查并修复。

                    2. OpenClaw 处理流程: 1. 检查 GitHub Issues 是否有相关报告 2. 读取登录页面相关代码 3. 识别 Safari 兼容性问题 4. 调用 OpenCode 生成修复代码 5. 运行测试验证 6. 创建 PR

                    3. OpenCode 执行修复

                      // 修复前(问题代码)
                      const login = async () => {
                        const response = await fetch('/api/login', {
                          credentials: 'same-origin'  // Safari 兼容性问题
                        });
                      };
                      // 修复后
                      const login = async () => {
                        const response = await fetch('/api/login', {
                          credentials: 'include',  // 修复 Safari 兼容性问题
                          headers: {
                            'Content-Type': 'application/json'
                          }
                        });
                      };
                    4. 自动化测试

                      npm test -- --watchAll=false
                      npm run build
                    5. 创建 PR: ✅ Bug 已定位: Safari 的 credentials 设置问题 ✅ 代码已修复 ✅ 测试通过 ✅ PR 已创建: #42 – Fix Safari login white screen 🔗 github.com/your-repo/p…

                    需求:对项目代码进行质量分析并重构

                    操作步骤

                    1. 发送指令: 分析项目代码质量,找出主要问题并建议重构方案。

                    2. OpenCode 分析

                      # 运行代码分析
                      npx eslint . --format json -o report.json
                      npx complexity-report --format json src/ > complexity.json
                    3. 生成报告

                      ## 代码质量分析报告
                      ### 发现的问题
                      1. **圈复杂度过高**: `utils/helpers.js` (复杂度: 23)
                      2. **重复代码**: `api/users.js` 和 `api/auth.js` 有 45% 重复
                      3. **未使用的变量**: `components/Header.tsx` 第 12 行
                      4. **缺少类型定义**: `types/index.ts` 不完整
                      ### 重构建议
                      - 将复杂函数拆分为小函数
                      - 提取公共逻辑到共享模块
                      - 添加完整的 TypeScript 类型
                    4. 执行重构: 是否执行自动重构?(yes/no)

                    错误现象

                    openclaw plugins list
                    # 输出: ✗ opencode-controller  1.2.3  enabled  not eligible

                    原因分析

                    1. OpenCode 未启动或端口未监听
                    2. API 认证信息错误
                    3. 配置文件格式错误

                    解决方案

                    # 1. 检查 OpenCode 是否运行
                    ps aux | grep opencode
                    # 2. 检查端口监听
                    netstat -tlnp | grep 4096
                    # 或
                    lsof -i :4096
                    # 3. 测试 API 连接
                    curl -u opencode:your-password http://127.0.0.1:4096/api/status
                    # 4. 验证配置文件语法
                    openclaw config validate
                    # 5. 重启服务
                    openclaw gateway restart
                    pkill opencode && opencode

                    错误现象

                    ⚠️ Task timeout after 180000ms
                    Error: OpenCode execution timeout

                    原因分析

                    1. 任务过于复杂,超出默认超时时间
                    2. 网络问题导致模型响应慢
                    3. 代码编译/测试耗时过长

                    解决方案

                    # 方案 1:增加超时时间
                    openclaw config set plugins.opencode-controller.config.timeoutMs 300000
                    # 方案 2:拆分复杂任务
                    # 将一个大任务拆分为多个小任务
                    # 方案 3:使用更快的模型
                    openclaw config set plugins.opencode-controller.config.model "gemini-flash"

                    错误现象

                    Error: 401 Unauthorized
                    Message: Invalid API key or insufficient credits

                    原因分析

                    1. API 密钥未设置或已过期
                    2. 账户余额不足
                    3. 密钥权限不够

                    解决方案

                    # 1. 检查 API 密钥设置
                    openclaw config get providers.anthropic.apiKey
                    # 2. 设置新的 API 密钥
                    openclaw config set providers.anthropic.apiKey "sk-ant-xxxxx"
                    # 3. 验证密钥有效性
                    curl https://api.anthropic.com/v1/models 
                      -H "x-api-key: sk-ant-xxxxx" 
                      -H "anthropic-version: 2023-06-01"
                    # 4. 使用 Google Vertex AI(免费选项)
                    openclaw plugins enable google-vertex-ai
                    openclaw auth login google

                    错误现象

                    Error: connect ECONNREFUSED 127.0.0.1:4096

                    原因分析

                    1. Docker 容器内无法访问宿主机的 localhost
                    2. 网络模式配置错误
                    3. 防火墙阻止连接

                    解决方案

                    # docker-compose.yml
                    version: '3.8'
                    services:
                      openclaw:
                        image: openclaw/openclaw:latest
                        container_name: openclaw
                        restart: unless-stopped
                        network_mode: host  # 使用 host 网络模式
                        # 或者使用自定义网络
                        # networks:
                        #   - openclaw-network
                        environment:
                          - OPENCLAW_API_KEY=${OPENCLAW_API_KEY}
                          # 使用宿主机的 IP 而不是 localhost
                          - OPENCODE_API_URL=http://host.docker.internal:4096
                        extra_hosts:
                          - "host.docker.internal:host-gateway"
                    # networks:
                    #   openclaw-network:
                    #     driver: bridge
                    # 获取宿主机 IP(Linux)
                    ip addr show docker0 | grep inet
                    # 修改配置使用正确的 IP
                    openclaw config set plugins.opencode-controller.config.apiUrl "http://172.17.0.1:4096"

                    错误现象

                    • 生成的代码不符合项目规范
                    • 缺少错误处理
                    • 使用了过时的 API

                    解决方案

                    # 1. 创建项目规范文件
                    cat > ~/.openclaw/presets/coding-standards.md << 'EOF'
                    # 代码规范
                    ## 通用原则
                    - 使用 TypeScript 严格模式
                    - 所有函数必须有返回类型
                    - 使用 async/await 而不是回调
                    - 错误必须被捕获和处理
                    ## 命名规范
                    - 组件: PascalCase (e.g., UserProfile)
                    - 函数: camelCase (e.g., getUserData)
                    - 常量: UPPER_SNAKE_CASE
                    ## 测试要求
                    - 所有新功能必须有单元测试
                    - 测试覆盖率不低于 80%
                    EOF
                    # 2. 配置 OpenClaw 使用规范
                    openclaw config set presets.codingStandards "~/.openclaw/presets/coding-standards.md"
                    # 3. 使用更强的模型
                    openclaw config set plugins.opencode-controller.config.model "claude-opus-4"
                    # 4. 启用代码审查 Agent
                    openclaw config set plugins.code-reviewer.enabled true

                    # 安装 GitHub MCP 插件
                    openclaw plugins install @anthropic/mcp-github
                    # 配置 GitHub Token
                    openclaw config set mcpServers.github.env.GITHUB_TOKEN "ghp_your_token_here"

                    # .github/workflows/openclaw-triage.yml
                    name: OpenClaw Issue Triage
                    on:
                      issues:
                        types: [opened]
                    jobs:
                      triage:
                        runs-on: ubuntu-latest
                        steps:
                          - name: Trigger OpenClaw
                            run: |
                              curl -X POST ${{ secrets.OPENCLAW_WEBHOOK }} 
                                -H "Content-Type: application/json" 
                                -d '{
                                  "action": "triage_issue",
                                  "issue_number": "${{ github.event.issue.number }}",
                                  "title": "${{ github.event.issue.title }}",
                                  "body": "${{ github.event.issue.body }}"
                                }'

                    # 在 Telegram 中发送
                    审查 PR #42,检查代码质量和潜在问题

                    OpenClaw 执行

                    1. 获取 PR 详情
                    2. 拉取代码变更
                    3. 运行静态分析
                    4. 检查测试覆盖率
                    5. 生成审查报告
                    6. 在 PR 中发表评论

                    # 配置定时任务(crontab)
                    0 9 * * * openclaw execute "send-daily-brief"

                    简报内容

                    📊 每日开发简报 - 2026-02-23
                    🔔 待办事项
                    - 3 个 PR 等待你的审查
                    - 2 个 Issue 分配给你
                    - 1 个 CI 构建失败
                    📈 昨日成果
                    - 合并 5 个 PR
                    - 关闭 8 个 Issue
                    - 代码提交: +1,234 -567
                    ⚠️ 需要注意
                    - main 分支构建失败(test-auth 超时)
                    - 依赖包 axios 有安全更新

                    {
                      "modelRouting": {
                        "simpleTasks": {
                          "model": "gemini-flash",
                          "maxTokens": 4096,
                          "costPer1K": 0.0001
                        },
                        "complexTasks": {
                          "model": "claude-opus-4",
                          "maxTokens": 8192,
                          "costPer1K": 0.015
                        },
                        "codeReview": {
                          "model": "claude-sonnet-4",
                          "maxTokens": 4096,
                          "costPer1K": 0.003
                        }
                      }
                    }

                    # 查看今日 API 调用统计
                    openclaw stats today
                    # 输出示例
                    Provider        Calls    Tokens    Cost
                    anthropic       45       125K      $1.87
                    google          120      340K      $0.34
                    openai          8        23K       $0.46
                    ─────────────────────────────────────
                    Total           173      488K      $2.67

                    # 启用响应缓存
                    openclaw config set cache.enabled true
                    openclaw config set cache.ttl 3600
                    # 清除缓存
                    openclaw cache clear

                    1. 组合威力:OpenClaw + OpenCode 实现了从需求到代码的端到端自动化
                    2. 灵活性:支持 75+ 模型,可根据任务选择最优方案
                    3. 安全性:本地运行,代码数据不上传云端
                    4. 可扩展:插件系统支持自定义技能和 Agent

                    场景 推荐配置
                    快速原型 Gemini Flash + 宽松超时
                    生产代码 Claude Opus + 代码审查 Agent
                    Bug 修复 Claude Sonnet + 测试驱动
                    代码重构 多模型投票 + 人工确认

                    • 多 Agent 协作:多个 OpenCode 实例并行工作
                    • 知识库集成:连接企业内部的文档和代码库
                    • CI/CD 深度集成:自动部署到测试/生产环境
                    • 自然语言编程:完全用自然语言描述需求

                    • OpenClaw 官方文档
                    • OpenCode 文档
                    • OpenClaw GitHub
                    • OpenCode GitHub

                    AI 编程工具正在重塑软件开发的方式。OpenClaw + OpenCode 的组合让我们看到了"AI 工程团队"的雏形。

                    但这套工具链也有局限——它适合执行明确的需求,不适合探索未知的问题。关键还是把需求想清楚——这是程序员的工作,AI 无法替代。

                    到此这篇关于openclaw和opencode组合使用的文章就介绍到这了,更多相关openclaw和opencode组合使用内容请搜索风君子博客以前的文章或继续浏览下面的相关文章,希望大家以后多多支持风君子博客!

                    站内搜索