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

# OpenCode

> 将 OpenCode 连接到 AihubMax

## 概述

[OpenCode](https://github.com/opencode-ai/opencode) 是一个基于 Go 的终端 TUI 编程助手，支持多种 LLM 提供商。它提供了一个交互式终端界面，你可以在其中与 AI 讨论代码、生成修改和执行命令。

通过将 OpenCode 连接到 AihubMax，你可以通过单一 API 端点访问多种 AI 模型。

<Info>
  OpenCode 支持通过 JSON 配置自定义提供商，因此可以轻松集成 AihubMax。
</Info>

## 前置条件

<CardGroup cols={2}>
  <Card title="AihubMax API Key" icon="key">
    一个有效的 AihubMax API Key。
  </Card>

  <Card title="OpenCode" icon="terminal">
    已安装 OpenCode CLI 工具。
  </Card>
</CardGroup>

## 安装与配置

<Steps>
  <Step title="安装 OpenCode">
    <Tabs>
      <Tab title="macOS / Linux (curl)">
        ```bash theme={null}
        curl -fsSL https://opencode.ai/install | bash
        ```
      </Tab>

      <Tab title="Homebrew">
        ```bash theme={null}
        brew install anomalyco/tap/opencode
        ```
      </Tab>

      <Tab title="Windows">
        访问 [OpenCode 下载页面](https://opencode.ai) 下载 Windows 安装包。
      </Tab>
    </Tabs>

    验证安装：

    ```bash theme={null}
    opencode --version
    ```
  </Step>

  <Step title="注册自定义提供商">
    在编辑配置文件之前，你必须先通过 OpenCode CLI 注册自定义提供商：

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

    按提示操作：

    1. 选择 **"other"** 作为提供商类型。
    2. 输入 `foxapi` 作为提供商 ID。
    3. 输入任意占位符作为 token（后续会在配置文件中覆盖）。

    <Warning>
      此步骤是必需的。如果未通过 `opencode auth login` 注册，OpenCode 将无法识别配置文件中的 `foxapi` 提供商。
    </Warning>
  </Step>

  <Step title="配置 AihubMax">
    编辑 OpenCode 配置文件：

    <Tabs>
      <Tab title="macOS / Linux">
        ```
        ~/.config/opencode/opencode.json
        ```
      </Tab>

      <Tab title="Windows">
        ```
        C:\Users\{username}\.config\opencode\opencode.json
        ```
      </Tab>
    </Tabs>

    将内容设置为：

    ```json theme={null}
    {
      "$schema": "https://opencode.ai/config.json",
      "provider": {
        "foxapi": {
          "npm": "@ai-sdk/anthropic",
          "name": "AihubMax",
          "options": {
            "baseURL": "https://api.aihubmax.com/v1",
            "apiKey": "sk-your-foxapi-api-key"
          },
          "models": {
            "claude-sonnet-4-20250514": { "name": "Claude Sonnet 4" },
            "claude-opus-4-20250514": { "name": "Claude Opus 4" },
            "gpt-4o": { "name": "GPT-4o" }
          }
        }
      }
    }
    ```

    将 `sk-your-foxapi-api-key` 替换为你的实际 AihubMax API Key。
  </Step>

  <Step title="开始使用">
    在你的项目目录中启动 OpenCode：

    ```bash theme={null}
    cd /path/to/your/project
    opencode
    ```

    这将打开交互式 TUI 界面，你可以在其中与 AI 讨论代码。
  </Step>
</Steps>

## 验证连接

启动 OpenCode 后，使用 `/models` 命令验证已配置的模型是否可用：

```
/models
```

你应该能看到 AihubMax 模型（如 Claude Sonnet 4、Claude Opus 4、GPT-4o）。然后输入一条测试消息确认连接：

```
请解释一下这个项目的结构
```

如果回复正常生成，说明 AihubMax 连接已成功配置。

## 推荐模型

| 使用场景     | 模型                         | 说明           |
| -------- | -------------------------- | ------------ |
| **复杂任务** | `claude-opus-4-20250514`   | 最适合复杂推理和架构设计 |
| **均衡**   | `claude-sonnet-4-20250514` | 质量和速度的良好平衡   |
| **快速高效** | `gpt-4o`                   | 日常任务的快速响应    |

## 常见问题

<AccordionGroup>
  <Accordion title="提供商未识别">
    * 确保你已运行 `opencode auth login` 并在编辑配置文件前注册了 `foxapi` 提供商。
    * 确保配置文件中的提供商 ID 与注册时输入的完全一致。
    * 检查配置文件是否为有效的 JSON 格式。
  </Accordion>

  <Accordion title="认证失败">
    * 确认 `options.apiKey` 字段中的 AihubMax API Key 正确。
    * 确认账户余额充足且有模型访问权限。
  </Accordion>

  <Accordion title="配置文件未找到">
    * macOS/Linux 上配置文件位于 `~/.config/opencode/opencode.json`。
    * Windows 上配置文件位于 `C:\Users\{username}\.config\opencode\opencode.json`。
  </Accordion>

  <Accordion title="模型未显示">
    * 在 OpenCode 中使用 `/models` 命令检查可用模型。
    * 确认配置文件中 `models` 部分列出了正确的模型 ID。
    * 确认 `baseURL` 设置为 `https://api.aihubmax.com/v1`。
  </Accordion>
</AccordionGroup>
