opencode 初尝试

最近opencode很火,我也在macOS上装了。这篇文章记录一下安装和使用过程。

官方说明

OpenCode is an open source agent that helps you write code in your terminal, IDE, or desktop.

  • [*]LSP enabled Automatically loads the right LSPs for the LLM。自动识别编程语言。
  • [*]Multi-session Start multiple agents in parallel on the same project。多个对话同时进行。
  • [*]Share links Share a link to any session for reference or to debug。分享编程过程。
  • [*]Claude Pro Log in with Anthropic to use your Claude Pro or Max account
  • [*]ChatGPT Plus/Pro Log in with OpenAI to use your ChatGPT Plus or Pro account
  • [*]Any model 75+ LLM providers through Models.dev, including local models。支持各种AI模型。
  • [*]Any editor Available as a terminal interface, desktop app, and IDE extension。什么编辑器,都能用OpenCode。

安装

最简单的方式是使用Homebrew:

brew install anomalyco/tap/opencode

启动OpenCode并配置模型:

cd ~/Workspace  # 进入你的工作目录
opencode        # 启动OpenCode

image-20260120下午60449579

在TUI界面中,输入 /connect 配置模型提供商:

  • 新手推荐: OpenCode Zen(经过验证的模型)
  • 进阶选择: Claude Sonnet 4.5(编程能力强)

首次使用,运行 /init 让OpenCode分析你的项目结构。

image-20260120下午60552057

基础操作

掌握这几个核心命令就能开始使用:

/help          # 查看所有命令
@文件名         # 引用文件内容
!命令           # 执行shell命令
/undo          # 撤销操作
/redo          # 重做操作
Tab键          # 切换Plan/Build模式
Esc            # 退出、打断当前操作

初上手

先拿我的blog项目进行练手:

请分析我这个blog项目的结构和功能,然后告诉我可以用OpenCode做什么改进?

41fec82fadb26603f326c6373bd72213

1d11a8e8c8630c94c2fb0b782642eed3

image-20260204下午12500986

595f8b14f35bbee299f4fbd2e0f73958

68166df70b8a2a8ec494d9dfcc02eab4

e8f331a93ec3795bbbd85cd39ea88c04

其他类似的上手问答工作都可以试试:

1. 请问你会做些什么
2. 检查macOS版本
3. 显示内存使用情况
4. 列出Homebrew安装的包
5. 检查磁盘空间

oh my opencode

Oh My OpenCode

Preview

模型配置说明

ef81cb0665572908a7c98468d5b9b818

OpenCode 云端模型:

在输入框输入 /models 即可切换models

image-20260206下午34548300

image-20260206下午33410577

添加本地 Ollama 模型

OpenCode 原生支持连接本地 Ollama 实例,无需 API Key。

安装 Ollama

# 启动服务(后台运行)
ollama serve

# 拉取代码专用模型
ollama pull qwen3-coder-next:q4_K_M

⚠️ 重要提示:截至 2026.2.4 macOS 正式版 Ollama不支持 qwen3-coder-next 等最新模型。 请从 GitHub 下载最新的 RC 版本:

  1. 访问 https://github.com/ollama/ollama/releases
  2. 下载 macOS RC 版本的 dmg 文件(不是 tar.gz!)
  3. 打开 dmg 文件,将 Ollama 拖到应用程序文件夹
  4. 启动 Ollama(Launchbar 或 Spotlight 搜索 “Ollama”)

2026-02-06下午2.17.29

先在 OpenCode 中配置 Ollama

vi ~/.config/opencode/opencode.json
{
  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Ollama(local)",
      "options": {
        "baseURL": "http://localhost:11434/v1"
      },
      "models": {
        "qwen3-coder-next:q4_K_M": {
          "name": "qwen3-coder-next:q4_K_M"
        }
      }
    }
  }
}

再在 Oh My OpenCode 中使用 Ollama

vi ~/.config/opencode/oh-my-opencode.json
{
  "agents": {
    "hephaestus": {
      "model": "ollama/qwen3-coder-next:q4_K_M"
    },
    "oracle": {
      "model": "ollama/qwen3-coder-next:q4_K_M"
    },
    "prometheus": {
      "model": "ollama/qwen3-coder-next:q4_K_M"
    },
    "sisyphus": {
      "model": "ollama/qwen3-coder-next:q4_K_M"
    }
  },
  "categories": {
    "visual-engineering": {
      "model": "opencode/minimax-m2.1-free"
    },
    "ultrabrain": {
      "model": "opencode/minimax-m2.1-free"
    },
    "quick": {
      "model": "opencode/minimax-m2.1-free"
    }
  }
}

删除所有 Task

如果需要清除所有 Sisyphus 任务,运行以下命令:

# 查看 Sisyphus 状态
ls ~/.sisyphus/

# 删除 boulder.json(清除任务状态)
rm ~/.sisyphus/boulder.json

# 删除所有计划文件
rm ~/.sisyphus/plans/*.md

# 删除 notepads
rm -rf ~/.sisyphus/notepads/*

与 Oh My OpenCode 对话的正确方式

不要让它直接执行命令,而是:

  1. 描述你的需求 → 它会生成工作计划
  2. 确认改动内容 → 它会展示修改对比
  3. 确认执行 → 运行 /start-work
  4. Sisyphus 执行 → 自动完成任务
  5. 验证结果 → 检查是否正确

在 Mac 下搞定了 Jekyll 环境:一次踩坑全记录 解决macOS应用程序“已损坏”警告