OpenCode 使用文档
介绍
OpenCode 是一款开源 AI 编码代理工具,支持终端界面、桌面应用程序或 IDE 扩展三种使用形式。
前置条件
要在终端中使用 OpenCode,你需要满足以下条件:
- 一款现代终端模拟器,例如:
- 你想要使用的大语言模型(LLM)提供商的 API 密钥
安装
安装 OpenCode 最简便的方式是通过安装脚本,也可根据自身系统和工具偏好选择以下其他安装方式:
通用安装脚本
curl -fsSL https://opencode.ai/install | bash
使用 Node.js 相关包管理器
- npm:
npm install -g opencode-ai
- Bun:
bun install -g opencode-ai
- pnpm:
pnpm install -g opencode-ai
- Yarn:
yarn global add opencode-ai
macOS 和 Linux 系统(使用 Homebrew)
brew install anomalyco/tap/opencode
建议使用 OpenCode 专属仓库源以获取最新版本,官方brew install opencode公式由 Homebrew 团队维护,更新频率较低。
Arch Linux 系统(使用 Paru)
paru -S opencode-bin
Windows 系统
- Chocolatey:
choco install opencode
- Scoop:
scoop install opencode
- NPM:
npm install -g opencode-ai
- Mise:
mise use -g github:anomalyco/opencode
Docker 安装
docker run -it --rm ghcr.io/anomalyco/opencode
注意:目前正在开发通过 Bun 在 Windows 系统上安装 OpenCode 的功能。你也可以从 Releases 中直接获取二进制文件。
配置
通过配置对应 API 密钥,OpenCode 可支持任意 LLM 提供商。如果是首次使用 LLM 提供商,推荐使用 OpenCode Zen——这是由 OpenCode 团队测试验证过的精选模型列表。配置步骤如下:
- 在 TUI 中运行
/connect命令,选择 opencode 并访问 opencode.ai/auth - 登录账号,添加账单信息并复制 API 密钥
- 粘贴 API 密钥并回车确认
┌ API key
│
│
└ enter
你也可以选择其他提供商,了解更多相关信息可查看官方详细说明。
初始化
配置好提供商后,按以下步骤初始化项目:
- 导航至目标项目目录
cd /path/to/project
- 运行 OpenCode
opencode
- 执行初始化命令,OpenCode 将分析项目并在项目根目录创建
AGENTS.md文件
/init
提示:建议将项目的AGENTS.md文件提交至 Git,这有助于 OpenCode 理解项目结构和编码模式。
使用方法
咨询问题
可向 OpenCode 咨询代码库相关问题,使用
@ 键可模糊搜索项目文件How is authentication handled in @packages/functions/src/api/index.ts
该功能适用于不熟悉的代码部分查询。
添加功能
1. 创建方案
OpenCode 的计划模式会禁用直接修改功能,仅提供实现建议。按 Tab 键切换至该模式(右下角将显示对应标识),然后描述需求:
When a user deletes a note, we'd like to flag it as deleted in the database.Then create a screen that shows all the recently deleted notes.From this screen, the user can undelete a note or permanently delete it.
提示:提供足够详细的上下文和示例,可按与团队初级开发沟通的方式描述需求,帮助 OpenCode 准确理解。
2. 迭代方案
收到方案后,可提供反馈或补充细节,支持拖放图片至终端添加到提示中
We'd like to design this new screen using a design I've used before.[Image #1] Take a look at this image and use it as a reference.
3. 实现功能
方案确认后,再次按 Tab 键切换回构建模式,下达执行命令:
Sounds good! Go ahead and make the changes.
直接修改
对于简单修改,可跳过方案审核直接要求 OpenCode 实现,需提供详细需求确保修改准确性:
We need to add authentication to the /settings route. Take a look at how this is handled in the /notes route in @packages/functions/src/notes.ts and implement the same logic in @packages/functions/src/settings.ts
撤销/重做修改
- 撤销修改:执行
/undo命令可还原更改并显示原始请求,支持多次执行撤销多步操作
/undo
- 重做修改:使用
/redo命令可恢复之前撤销的更改
/redo
分享对话
通过
/share 命令可生成当前对话链接并复制到剪贴板,方便与团队共享/share
注意:对话默认不会自动共享,点击示例对话可查看 OpenCode 交互演示。
自定义设置
可通过以下方式个性化配置 OpenCode:
文档更新时间:2026年1月28日