> ## Documentation Index
> Fetch the complete documentation index at: https://docs.teio.me/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenCode

> 在终端中配置 Teio API 使用 OpenCode

> OpenCode 是一个开源的 AI 编码代理。它提供终端界面、桌面应用和 IDE 扩展等多种使用方式。

## 前置条件

* 已 [创建 API 密钥](/create-token)
* 如果通过 NPM 安装，需要已安装 Node.js 18+

## 安装 OpenCode

<Tabs>
  <Tab title="macOS">
    一键安装（推荐）：

    ```bash theme={null}
    curl -fsSL https://opencode.ai/install | bash
    ```

    或通过 Homebrew：

    ```bash theme={null}
    brew install anomalyco/tap/opencode
    ```

    或通过 NPM：

    ```bash theme={null}
    npm install -g opencode-ai
    ```
  </Tab>

  <Tab title="Windows">
    通过 NPM：

    ```bash theme={null}
    npm install -g opencode-ai
    ```
  </Tab>
</Tabs>

## 配置

OpenCode 需要两个配置文件：`opencode.json`（提供商配置）和 `auth.json`（认证信息）。

<Tabs>
  <Tab title="macOS">
    配置文件路径：

    * `~/.config/opencode/opencode.json`
    * `~/.local/share/opencode/auth.json`

    如果文件夹不存在，请手动创建：

    ```bash theme={null}
    mkdir -p ~/.config/opencode ~/.local/share/opencode
    ```

    写入 `opencode.json`（OpenCode 按模型厂商区分 provider，需要将用到的 provider 的 `baseURL` 都指向 Teio，这样无论使用 OpenAI 还是 Claude 系列模型，请求都会通过 Teio 转发）：

    ```bash theme={null}
    cat > ~/.config/opencode/opencode.json << 'EOF'
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "openai": {
          "options": {
            "baseURL": "https://teio.me/v1"
          }
        },
        "anthropic": {
          "options": {
            "baseURL": "https://teio.me/v1"
          }
        }
      }
    }
    EOF
    ```

    写入 `auth.json`：

    ```bash theme={null}
    cat > ~/.local/share/opencode/auth.json << 'EOF'
    {
      "openai": {
        "type": "api",
        "key": "你的OpenAI分组密钥"
      },
      "anthropic": {
        "type": "api",
        "key": "你的Claude分组密钥"
      }
    }
    EOF
    ```
  </Tab>

  <Tab title="Windows">
    配置文件路径：

    * `C:\Users\<您的用户名>\.config\opencode\opencode.json`
    * `C:\Users\<您的用户名>\.local\share\opencode\auth.json`

    如果文件夹不存在，请在文件资源管理器中手动创建对应目录。

    `opencode.json`（OpenCode 按模型厂商区分 provider，需要将用到的 provider 的 `baseURL` 都指向 Teio，这样无论使用 OpenAI 还是 Claude 系列模型，请求都会通过 Teio 转发）：

    ```json theme={null}
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "openai": {
          "options": {
            "baseURL": "https://teio.me/v1"
          }
        },
        "anthropic": {
          "options": {
            "baseURL": "https://teio.me/v1"
          }
        }
      }
    }
    ```

    `auth.json`：

    ```json theme={null}
    {
      "openai": {
        "type": "api",
        "key": "你的OpenAI分组密钥"
      },
      "anthropic": {
        "type": "api",
        "key": "你的Claude分组密钥"
      }
    }
    ```
  </Tab>
</Tabs>

## 启动

<Note>
  如果你刚完成上述配置，请先重启终端，确保配置文件已被加载。
</Note>

```bash theme={null}
opencode
```

如果配置正确，你会进入 OpenCode 的交互界面。

<Frame>
  <img src="https://mintcdn.com/cdhyzxwl/6tLCIfichwMPh1V0/images/macshot-clipboard-ED84E82A-A174-46E7-8291-93B5FBBD09EE.png?fit=max&auto=format&n=6tLCIfichwMPh1V0&q=85&s=4e9ca5d8fa0545b2d5c838415496ce27" alt="Macshot Clipboard ED84E82A A174 46E7 8291 93B5FBBD09EE" width="1692" height="998" data-path="images/macshot-clipboard-ED84E82A-A174-46E7-8291-93B5FBBD09EE.png" />
</Frame>

启动后可以通过 /models 命令切换到的模型。

<Frame>
  <img src="https://mintcdn.com/cdhyzxwl/6tLCIfichwMPh1V0/images/macshot-clipboard-73B526CE-7EFB-41AE-9418-8005D02D201D.png?fit=max&auto=format&n=6tLCIfichwMPh1V0&q=85&s=3bcd5ae793d0f1790a05d05609df1086" alt="Macshot Clipboard 73B526CE 7EFB 41AE 9418 8005D02D201D" width="1692" height="998" data-path="images/macshot-clipboard-73B526CE-7EFB-41AE-9418-8005D02D201D.png" />
</Frame>

根据需求选择对应的模型

<Frame>
  <img src="https://mintcdn.com/cdhyzxwl/6tLCIfichwMPh1V0/images/macshot-clipboard-A27C51A0-B8F4-4176-BDC0-32032F83440D-1.png?fit=max&auto=format&n=6tLCIfichwMPh1V0&q=85&s=3e58f5dceadc462cdbb4f48c7f2258bd" alt="Macshot Clipboard A27C51A0 B8F4 4176 BDC0 32032F83440D 1" width="1692" height="998" data-path="images/macshot-clipboard-A27C51A0-B8F4-4176-BDC0-32032F83440D-1.png" />
</Frame>

## 常见问题

<AccordionGroup>
  <Accordion title="提示模型不存在或 model not found">
    <Frame>
      <img src="https://mintcdn.com/cdhyzxwl/6tLCIfichwMPh1V0/images/macshot-clipboard-8A37AE2F-D5D8-46AE-A9D4-9AEB676DFAA1.png?fit=max&auto=format&n=6tLCIfichwMPh1V0&q=85&s=8ec374bd746df10de89fa950c54008f9" alt="Macshot Clipboard 8A37AE2F D5D8 46AE A9D4 9AEB676DFAA1" width="1692" height="998" data-path="images/macshot-clipboard-8A37AE2F-D5D8-46AE-A9D4-9AEB676DFAA1.png" />
    </Frame>

    由于 OpenAI 和 Anthropic 官方列举了所有模型，但个别模型 Teio 暂不支持。建议在 Teio 控制台的「操练场」中，选择对应分组查看当前可用的模型列表，确认你要使用的模型已被支持后再进行配置
  </Accordion>
</AccordionGroup>

遇到问题？请参考 [常见问题](/faq)。
