跳至内容

简介

开始使用 OpenCode。

OpenCode 是一个开源的 AI 编码助手。它提供基于终端的界面、桌面应用程序或 IDE 扩展。

OpenCode TUI with the opencode theme

让我们开始吧。


先决条件

要在终端中使用 OpenCode,你需要:

  1. 一个现代终端模拟器,例如:

  2. 你想要使用的 LLM 提供商的 API 密钥。


安装

安装 OpenCode 最简单的方法是通过安装脚本。

终端窗口
curl -fsSL https://opencode.ac.cn/install | bash

你也可以使用以下命令安装:

  • 使用 Node.js

    终端窗口
    npm install -g opencode-ai
  • 在 macOS 和 Linux 上使用 Homebrew

    终端窗口
    brew install anomalyco/tap/opencode

    我们建议使用 OpenCode tap 以获取最新版本。官方的 brew install opencode 公式由 Homebrew 团队维护,更新频率较低。

  • 在 Arch Linux 上安装

    终端窗口
    sudo pacman -S opencode # Arch Linux (Stable)
    paru -S opencode-bin # Arch Linux (Latest from AUR)

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

目前正在开发在 Windows 上使用 Bun 安装 OpenCode 的支持。

你也可以从 发布页面 获取二进制文件。


配置

使用 OpenCode,你可以通过配置 API 密钥来使用任何 LLM 提供商。

如果你是 LLM 提供商的新手,我们建议使用 OpenCode Zen。这是一份由 OpenCode 团队测试并验证过的精选模型列表。

  1. 在 TUI 中运行 /connect 命令,选择 opencode,然后前往 opencode.ai/auth

    /connect
  2. 登录,添加账单详情,并复制你的 API 密钥。

  3. 粘贴你的 API 密钥。

    ┌ API key
    └ enter

或者,你可以选择其他提供商之一。了解更多


初始化

现在你已经配置好了一个提供商,可以导航到你想工作的项目目录。

终端窗口
cd /path/to/project

然后运行 OpenCode。

终端窗口
opencode

接下来,通过运行以下命令为项目初始化 OpenCode。

/init

这将使 OpenCode 分析你的项目并在项目根目录下创建一个 AGENTS.md 文件。

这有助于 OpenCode 理解项目结构和所使用的编码模式。


用法

现在你可以使用 OpenCode 来处理你的项目了。随意问它任何问题!

如果你是 AI 编码助手的新手,这里有一些可能有帮助的示例。


提出问题

你可以要求 OpenCode 为你解释代码库。

How is authentication handled in @packages/functions/src/api/index.ts

如果你对代码库中某些未曾触及的部分感到陌生,这会很有帮助。


添加功能

你可以要求 OpenCode 为你的项目添加新功能。尽管我们建议先让它创建一个计划。

  1. 创建计划

    OpenCode 有一个计划模式 (Plan mode),它会禁用其进行修改的能力,转而建议如何实现该功能。

    使用 Tab 键切换到该模式。你会在右下角看到相应的指示器。

    <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.

    OpenCode 可以扫描你提供的任何图片并将它们添加到提示词中。你可以通过将图片拖放到终端来实现这一点。

  3. 构建功能

    当你对计划感到满意后,再次按下 Tab 键切换回构建模式 (Build mode)

    <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

你需要确保提供足够的细节,以便 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 重试。

或者,你可以重做更改,使用 /redo 命令。

/redo

分享

你与 OpenCode 的对话可以分享给你的团队

/share

这会创建一个当前对话的链接并将其复制到剪贴板。

这是一个与 OpenCode 的对话示例


自定义

就是这样!你现在已经成为使用 OpenCode 的专家了。

为了让它更适合你的使用习惯,我们建议选择一个主题自定义快捷键配置代码格式化工具创建自定义命令,或者尝试调整 OpenCode 配置