Opencode安装与使用

Keywords: #技术 #AI #Opencode
Release Date: 2026-02-12
Table of Contents

OpenCode 是最近很火的一款开源 AI 编程助手,是 Claude Code 的开源平替,支持 75+ 种大模型(包括 Claude、GPT、Kimi 等),提供终端、桌面和 IDE 插件多形态。

安装

先要安装 Node.js

OpenCode | 下载

介绍 | OpenCode

npm i -g opencode-ai

把以下路径加入环境变量,就可以在终端中使用 opencode 命令打开。

C:\Users\LiuChao\AppData\Roaming\npm\node_modules\opencode-ai\node_modules\opencode-windows-x64\bin\opencode.exe

命令

  • /models:列出并选择 AI 模型

  • /connect:列出并配置 AI 模型供应商

  • /session:列出并选择会话(在后台执行的任务)

  • /new:创建一个新会话(当前会话移动太后台执行)

  • /share:分享当前会话记录(会生成一个可公开访问的网页)

  • /unshare:取消分享(网页链接不可访问)

  • /export:将当前会话记录导出成文件

  • /timeline:查看当前会话中的每一次与 AI 的对话记录(可选择将代码和对话记录一起回退)

  • /init:让 AI 通读整个项目文件,了解整个项目并生成 AGENTS.md 作为整个项目的系统提示词

  • /compact:压缩之前对话的上下文,缩短对话,释放上下文窗口

也可以自定义命令,在下面这个文件中以 Markdown 格式定义命令:

C:\Users\用户名\.config\opencode\commands\

比如创建一个 test.md 文件,在其中写入以下内容

---
description: Run tests with coverage
agent: build
model: anthropic/claude-3-5-sonnet-20241022
---

Run the full test suite with coverage report and show any failures.
Focus on the failing tests and suggest fixes.

Skills

“带目录结果的说明书 (技能包)”,AI 根据目录选择需要的技能说明书,完成相对应的功能。

全局 Skills:

C:\Users\用户名\.config\opencode\skills\<skill-name>\

sickn33/antigravity-awesome-skills 汇总多个 Skills

使用 npx antigravity-awesome-skills 将 Skills 安装到 C:\Users\用户名\.agent\skills,再将 Skills 移动到指定位置。

项目 Skills:

项目目录/.opencode/skills/<skill-name>
  • <skill-name>/
    • SKILL. md
    • scripts/
      • main.py
    • references/
      • doc.md
    • assets/

将 Skills 放入项目文件夹中,AI 就可以识别访问这些 Skills,当某些需求需要这些 Skills 时,AI 会自动调用。

MCP

MCP 服务器 | OpenCode

有两种 MCP 配置,一种 local,一种 remote

在以下文件中配置 MCP

C:\Users\用户名\.config\opencode\opencode.json
{
    "$schema": "https://opencode.ai/config.json",
    "mcp": {
        "my-local-mcp-server": {
            "type": "local",
            // Or ["bun", "x", "my-mcp-command"]
            "command": ["npx", "-y", "my-mcp-command"],
            "enabled": true,
            "environment": {
                "MY_ENV_VAR": "my_env_var_value",
            }
        },
        "my-remote-mcp": {
            "type": "remote",
            "url": "https://my-mcp-server.com",
            "enabled": true,
            "headers": {
                "Authorization": "Bearer MY_API_KEY"
            }
        },
    },
}

配置 Context 7:

Context 7 是一个基于 MCP 的文档拉取服务。它能在你给 LLM(如 Claude、Cursor Copilot、Windsurf 等)的提示中,动态地注入从源头获取的、与你所用库版本完全对应的官方文档和代码示例

{
  "mcp": {
    "context7": {
      "type": "remote",
      "url": "https://mcp.context7.com/mcp",
      "headers": {
        "CONTEXT7_API_KEY": "YOUR_API_KEY"
      },
      "enabled": true
    }
  }
}

配置 GitHub MCP:

GitHub MCP 服务器将 AI 工具直接连接到 GitHub 平台。这赋予了 AI 代理、助理和聊天机器人读取代码库和代码文件、管理问题和公关、分析代码并自动化工作流程的能力。这一切都通过自然语言的互动。

{
  "mcp": {
    "github": {
      "type": "local",
      "command": [
        "npx",
        "-y",
        "@modelcontextprotocol/server-github"
      ],
      "enabled": true,
      "environment": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "your-token"
      }
    }
  }
}

Agent

代理 | OpenCode

opencode 内置了 build 和 plan 两个智能体,此外还可以自定义智能体,在以下文件中以 Markdown 形式创建智能体。

C:\Users\用户名\.config\opencode\agents\

如创建一个 review.md

---
description: Reviews code for quality and best practices
mode: subagent
model: anthropic/claude-sonnet-4-20250514
temperature: 0.1
tools:
  write: false
  edit: false
  bash: false
---

You are in code review mode. Focus on:

- Code quality and best practices
- Potential bugs and edge cases
- Performance implications
- Security considerations

Provide constructive feedback without making direct changes.

subagent 是子智能体可以由 primary 主智能体自由调度,在后台执行。主智能体可以在输入框使用 Tab 显式进行切换。

oh my opencode

code-yeongyu/oh-my-opencode: the best agent harness

oh my opencode 本质上是一系列工具MCP 与编程 Agent 的组合。

OMO 中有几个内置的 Agent:Sisyphus (the main agent), Prometheus (planner), Oracle (architecture/debugging), Librarian (docs/code search), Explore (fast codebase grep), Multimodal Looker …

在 opencode 输入框中输入以下指令进行安装:

Install and configure oh-my-opencode by following the instructions here:
https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/refs/heads/master/docs/guide/installation.md

根据问题进行模型配置,OMO 会根据已有的模型将其分配给适合的 Agent。

具体的配置信息会写入以下这个文件中,可以手动进行管理和修改。

C:\Users\用户名\.config\opencode\oh-my-opencode.json

具体用法:

  • @:指定 Agent 进行工作
  • ulw / ultra work:发挥 opencode 的全部潜能,自动把任务分配给几个智能体并行分别完成任务,由主智能体居中调度
  • /ralph-loop:强制 AI 进行长时间循环,对一个复杂困难的任务进行长时间工作

云端运行

可以在 Github 上输入 opencode 指令,可以用它来帮我们解释代码、自动修复问题、创建 PR 等。

将本地的项目上传至 Github,在本地项目文件中输入 opencode github install 进行安装和配置,并再次提交本地文件到 Github

在 Github 仓库中设置 Actions 仓库密钥,按照要求将 AI 模型的 API Key 填入其中。

配置成功后,在项目的 Issues 界面,当有人提出一个问题时,在 Comment 输入框中输入 /opencode 就可以调出 opencode 输入需求,提交评论即可。opencode 会通过 Actions 自动执行修改并提交。