規則

透過建立 AGENTS.md 檔案為 OpenCode 設定自訂指令。瞭解如何設定專案層級和全域規則。

本頁內文若連結到尚未提供繁體版本的頁面(例如 /ecosystem/plugins/、/news/),會開啟英文原頁;導覽與頁尾標有 EN 的項目即為這類頁面。

OpenCode 允許你透過 AGENTS.md 檔案提供自訂指令,類似於 Cursor 的規則。這些指令會包含在 LLM 的上下文中,以根據你的特定專案自訂行為。

初始化

要建立新的 AGENTS.md 檔案,在 OpenCode 中執行 /init 命令:

/init

/init 會掃描 repo 中的重要檔案,當程式碼庫無法回答時可能會詢問有針對性的問題,然後建立或更新 AGENTS.md,其中包含簡潔的專案特定指導。

它專注於未來智慧代理工作階段最可能需要的內容:

  • 建置、lint 和測試命令
  • 命令順序和重點驗證步驟
  • 從檔名中不明顯的架構和 repo 結構
  • 專案特定的約定、設定怪癖和操作注意事項
  • 對現有指令源的引用,例如 Cursor 或 Copilot 規則

如果你已經有 AGENTS.md,/init 會就地改進它。

範例

你也可以手動建立此檔案。以下是一個範例:

# SST v3 Monorepo Project

This is an SST v3 monorepo with TypeScript. The project uses bun workspaces.

## Project Structure
- `packages/` - All workspace packages
- `infra/` - Infrastructure definitions
- `sst.config.ts` - Main SST configuration with dynamic imports

## Code Standards
- Use TypeScript with strict mode
- Shared code goes in `packages/core/`
- Functions go in `packages/functions/`
- Infrastructure should be split into logical files in `infra/`

## Monorepo Conventions
- Import shared modules using workspace names: `@my-app/core/example`

類型

OpenCode 支援從多個位置讀取 AGENTS.md,以用於不同目的。

專案

在專案根目錄放置 AGENTS.md 以取得專案特定規則。這些規則僅適用於該目錄或其子目錄。

全域

你也可以在 ~/.config/opencode/AGENTS.md 中設定全域規則。這適用於所有 OpenCode 工作階段。把它用於個人規則,因為它不會被提交到 Git。

Claude Code 相容性

對於從 Claude Code 遷移的使用者,OpenCode 支援 Claude Code 檔案約定作為後備:

  • 專案規則:專案目錄中的 CLAUDE.md(如果不存在 AGENTS.md 則使用)
  • 全域規則:~/.claude/CLAUDE.md(如果不存在 ~/.config/opencode/AGENTS.md 則使用)
  • Skills:~/.claude/skills/ —— 詳見 Agent Skills 文件

要停用 Claude Code 相容性:

export OPENCODE_DISABLE_CLAUDE_CODE=1        # 停用所有 .claude 支援
export OPENCODE_DISABLE_CLAUDE_CODE_PROMPT=1 # 僅停用 ~/.claude/CLAUDE.md
export OPENCODE_DISABLE_CLAUDE_CODE_SKILLS=1 # 僅停用 .claude/skills

優先順序

當 OpenCode 啟動時,它會按以下順序尋找規則檔案:

  1. 從目前目錄向上遍歷本機檔案(AGENTS.md,然後 CLAUDE.md)
  2. 全域檔案在 ~/.config/opencode/AGENTS.md
  3. Claude Code 檔案在 ~/.claude/CLAUDE.md(除非已停用)

每個類別中的第一個相符檔案勝出。

自訂指令

你可以在 opencode.json 中指定自訂指令檔案:

{
  "$schema": "https://opencode.ai/config.json",
  "instructions": ["CONTRIBUTING.md", "docs/guidelines.md", ".cursor/rules/*.md"]
}

你也可以使用遠端 URL:

{
  "$schema": "https://opencode.ai/config.json",
  "instructions": ["https://raw.githubusercontent.com/my-org/shared-rules/main/style.md"]
}

遠端指令的拉取設有 5 秒逾時。

所有指令檔案都會與你的 AGENTS.md 檔案合併。

引用外部檔案

雖然 OpenCode 不會自動解析 AGENTS.md 中的檔案引用,但你可以透過兩種方式做到這點:

使用 opencode.json

推薦的方法是使用 opencode.json 中的 instructions 欄位:

{
  "$schema": "https://opencode.ai/config.json",
  "instructions": ["docs/development-standards.md", "test/testing-guidelines.md"]
}

AGENTS.md 中的手動指令

你可以透過提供明確指令,教 OpenCode 讀取外部檔案:

# TypeScript Project Rules

## External File Loading

CRITICAL: When you encounter a file reference (e.g., @rules/general.md), use your Read tool to load it on a need-to-know basis. They're relevant to the SPECIFIC task at hand.

Instructions:
- Do NOT preemptively load all references - use lazy loading based on actual need
- When loaded, treat content as mandatory instructions that override defaults
- Follow references recursively when needed

## Development Guidelines
For TypeScript code style: @docs/typescript-guidelines.md
For React patterns: @docs/react-patterns.md

OpenCode 會按需載入檔案,而不是預先載入。