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
bashbrew install anomalyco/tap/opencodeNote我们建议使用 OpenCode tap 以获取最新的版本。官方的
brew install opencode公式由 Homebrew 团队维护,更新频率较低。 -
在 Arch Linux 上使用 Paru
bashparu -S opencode-bin
Windows
-
使用 Chocolatey
bashchoco install opencode -
使用 Scoop
bashscoop install opencode -
使用 NPM
bashnpm install -g opencode-ai -
使用 Mise
bashmise use -g github:anomalyco/opencode -
使用 Docker
bashdocker run -it --rm ghcr.io/anomalyco/opencode
目前正在开发使用 Bun 在 Windows 上安装 OpenCode 的支持。
您也可以从 Releases 获取二进制文件。
##配置
使用 OpenCode,您可以通过配置其 API 密钥来使用任何 LLM 提供商。
如果您是 LLM 提供商的新手,我们建议使用 OpenCode Zen。 它是 OpenCode 团队测试和验证过的模型的精选列表。
-
在 TUI 中运行
/connect命令,选择 opencode,然后前往 opencode.ai/auth。bash/connect -
登录,添加您的账单详细信息,然后复制您的 API 密钥。
-
粘贴您的 API 密钥。
bash┌ API key │ │ └ enter
或者,您可以选择其他提供商之一。了解更多。
##初始化
现在您已经配置了一个提供商,您可以导航到您想要处理的项目。
cd /path/to/project
并运行 OpenCode。
opencode
接下来,通过运行以下命令初始化项目的 OpenCode。
/init
这将使 OpenCode 分析您的项目并在项目根目录中创建一个 AGENTS.md 文件。
Note💡 提示
您应该将项目的
AGENTS.md文件提交到 Git。
这有助于 OpenCode 了解项目结构和使用的编码模式。
##用法
现在您可以准备好使用 OpenCode 来处理您的项目了。 随时问它任何问题!
如果您是 AI 编码助手的新手,这里有一些可能有所帮助的示例。
###提问
您可以要求 OpenCode 向您解释代码库。
Note💡 提示
使用
@键在项目中模糊搜索文件。
How is authentication handled in @packages/functions/src/api/index.ts
如果代码库的某些部分不是您处理的,这将很有帮助。
###添加功能
您可以要求 OpenCode 向您的项目添加新功能。 虽然我们首先建议您要求它创建一个计划。
-
创建计划
OpenCode 具有 计划模式,该模式禁用其进行更改的能力,而是建议 如何 实现该功能。
使用 Tab 键切换到它。 您将在右下角看到一个指示器。
bash现在让我们描述我们希望它做什么。
bashWhen 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 提供足够的详细信息以了解您想要的内容。 像与团队中的初级开发人员交谈一样与它交谈会有所帮助。
Note💡 提示
Note向 OpenCode 提供大量的上下文和示例,以帮助它了解您想要的内容。
-
迭代计划
一旦它为您提供了一个计划,您就可以向它提供反馈或添加更多详细信息。
bashWe'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.Note💡 提示
Note将图像拖放到终端中以将其添加到提示中。
OpenCode 可以扫描您提供的任何图像并将其添加到提示中。 您可以通过将图像拖放到终端中来执行此操作。
-
构建功能
一旦您对该计划感到满意,请再次点击 Tab 键切换回 构建模式。
bash并要求它进行更改。
bashSounds 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
您需要确保提供足够的详细信息,以便 OpenCode 进行正确的更改。
###撤消更改
假设您要求 OpenCode 进行一些更改。
Can you refactor the function in @packages/functions/src/api/index.ts?
但是您意识到这不是您想要的。 您可以使用 /undo 命令撤消更改。
/undo
OpenCode 现在将恢复您所做的更改并再次显示您的原始消息。
Can you refactor the function in @packages/functions/src/api/index.ts?
从这里,您可以调整提示并要求 OpenCode 再次尝试。
Note💡 提示
您可以多次运行
/undo以撤消多个更改。
或者您可以使用 /redo 命令重做更改。
/redo
##分享
您可以将与 OpenCode 的对话 与您的团队分享。
/share
这将创建一个指向当前对话的链接并将其复制到您的剪贴板。
Note📝 注意
默认情况下不共享对话。
这是一个与 OpenCode 的 示例对话。
##自定义
就这样! 您现在是使用 OpenCode 的专家了。
为了使其成为您自己的,我们建议 选择一个主题,自定义键绑定,配置代码格式化程序,创建自定义命令 或使用 OpenCode 配置。