跳转到内容

Introduction

开始使用 opencode。

opencode 是一个开源人工智能编码代理。它可用于基于终端的界面、桌面应用程序或 IDE 扩展。

具有 opencode 主题的 opencode TUI

让我们开始吧。


Prerequisites

要在终端中使用 opencode,您需要:

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

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


安装

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

Terminal window
curl -fsSL https://opencode.ai/install | bash

您还可以使用以下命令安装它:

  • 使用 Node.js

    Terminal window
    npm install -g opencode-ai
  • 在 macOS 和 Linux 上使用 Homebrew

    Terminal window
    brew install anomalyco/tap/opencode

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

  • 在 Arch Linux 上使用 Paru

    Terminal window
    paru -S opencode-bin

Windows

  • Using Chocolatey

    Terminal window
    choco install opencode
  • Using Scoop

    Terminal window
    scoop install opencode
  • 使用 npm

    Terminal window
    npm install -g opencode-ai
  • Using Mise

    Terminal window
    mise use -g github:anomalyco/opencode
  • 使用 Docker

    Terminal window
    docker run -it --rm ghcr.io/anomalyco/opencode

目前正在支持在 Windows 上安装 opencode 时使用 Bun。

您还可以从 Releases 获取二进制文件。


配置

借助 opencode,你可以通过配置 API 使用任意 LLM 提供商。

如果你刚开始使用 LLM 提供商,我们建议使用 OpenCode Zen。 这是经过 opencode 团队测试和验证的精选模型列表。

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

    /connect
  2. 登录,添加您的账单信息,然后复制您的详细 API 密钥。

  3. 粘贴您的 API 密钥。

    ┌ API key
    └ enter

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


Initialization

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

Terminal window
cd /path/to/project

并运行 opencode。

Terminal window
opencode

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

/init

这涉及 opencode 分析您的项目并在以下位置创建 AGENTS.md 文件 项目根。

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


用法

您现在已准备好使用 opencode 来处理您的项目。请轻松询问任何事物!

如果您不熟悉使用 AI 编码代理,以下是一些可能会有所帮助的示例。


Asking Questions

您可以要求 opencode 向您解释代码库。

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

如果您没有处理代码库的一部分,这会很有帮助。


Adding Features

您可以要求 opencode 向您的项目添加新功能。但是我们首先建议要求它制定一个计划。

  1. Create a plan

    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. Iterate on the plan

    一旦它为您提供了计划,您就可以提供反馈或添加更多详细信息。

    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. Build the feature

    一旦您对计划感到满意,请切换回 Build Mode 再次按 Tab 键。

    <TAB>

    并要求它做出改变。

    Sounds good! Go ahead and make the changes.

Making 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 配置