--- title: 模式 description: 不同的模式適用於不同的使用案例。 --- :::caution 現在透過 opencode 設定中的 `agent` 選項配置模式。這 `mode` 選項現已棄用。 [了解更多](/docs/agents)。 ::: opencode 中的模式允許您自定義不同使用案例的行為、工具和提示。 它具有兩種內建模式:**建置 (Build)**和**計畫 (Plan)**。您可以自定義 這些或透過 opencode 設定配置您自己的。 您可以在工作階段期間在模式之間切換或在設定檔中配置它們。 --- ## 內建 opencode 有兩種內建模式。 --- ### 建置 (Build) 建置是啟用所有工具的**預設**模式。這是開發工作的標準模式,您需要完全存取檔案操作和系統指令。 --- ### 計畫 (Plan) 專為規劃和分析而設計的受限模式。在計畫模式下,預設情況下禁用以下工具: - `write` - 無法建立新檔案 - `edit` - 無法修改現有檔案,位於 `.opencode/plans/*.md` 的用於詳細說明計畫本身的檔案除外 - `patch` - 無法套用 Patch - `bash` - 無法執行 shell 指令 當您希望 AI 分析程式碼、建議變更或建立計畫而不對程式碼庫進行任何實際修改時,此模式非常有用。 --- ## 切換 您可以在工作階段期間使用 _Tab_ 鍵在模式之間切換。或者您配置的 `switch_mode` 鍵綁定。 另請參閱:[格式化程式](/docs/formatters) 有關程式碼格式配置的資訊。 --- ## 設定 您可以自定義內建模式或透過配置建立自己的模式。可以透過兩種方式配置模式: ### JSON 配置 在 `opencode.json` 設定檔中配置模式: ```json title="opencode.json" { "$schema": "https://opencode.ai/config.json", "mode": { "build": { "model": "anthropic/claude-sonnet-4-20250514", "prompt": "{file:./prompts/build.txt}", "tools": { "write": true, "edit": true, "bash": true } }, "plan": { "model": "anthropic/claude-haiku-4-20250514", "tools": { "write": false, "edit": false, "bash": false } } } } ``` ### Markdown 配置 您還可以使用 Markdown 檔案定義模式。將它們放入: - 全域:`~/.config/opencode/modes/` - 專案:`.opencode/modes/` ```markdown title="~/.config/opencode/modes/review.md" --- model: anthropic/claude-sonnet-4-20250514 temperature: 0.1 tools: write: false edit: false bash: false --- You are in code review mode. Focus on: - Code quality and best practices - Potential bugs and edge cases - Performance implications - Security considerations Provide constructive feedback without making direct changes. ``` Markdown 檔名成為模式名稱(例如,`review.md` 建立 `review` 模式)。 讓我們詳細看看這些配置選項。 --- ### 模型 (Model) 使用 `model` 配置覆寫此模式的預設模型。對於使用針對不同任務最佳化的不同模型很有用。例如,更快的規劃模型、更強大的實作模型。 ```json title="opencode.json" { "mode": { "plan": { "model": "anthropic/claude-haiku-4-20250514" } } } ``` --- ### 溫度 (Temperature) 使用 `temperature` 配置控制 AI 回應的隨機性和創造性。較低的值使回應更加集中和確定,而較高的值則增加創造力和可變性。 ```json title="opencode.json" { "mode": { "plan": { "temperature": 0.1 }, "creative": { "temperature": 0.8 } } } ``` 溫度值的範圍通常為 0.0 到 1.0: - **0.0-0.2**:非常集中且確定的回應,非常適合程式碼分析和規劃 - **0.3-0.5**:具有一定創造力的平衡回應,適合一般開發任務 - **0.6-1.0**:更有創意和多樣化的反應,有助於腦力激盪和探索 ```json title="opencode.json" { "mode": { "analyze": { "temperature": 0.1, "prompt": "{file:./prompts/analysis.txt}" }, "build": { "temperature": 0.3 }, "brainstorm": { "temperature": 0.7, "prompt": "{file:./prompts/creative.txt}" } } } ``` 如果未指定溫度,opencode 將使用特定於模型的預設值(大多數模型通常為 0,Qwen 模型為 0.55)。 --- ### 提示 (Prompt) 使用 `prompt` 配置為此模式指定自定義系統提示檔案。提示檔案應包含特定於該模式用途的指令。 ```json title="opencode.json" { "mode": { "review": { "prompt": "{file:./prompts/code-review.txt}" } } } ``` 該路徑是相對於設定檔所在位置的。所以這適用於 全域 opencode 配置和專案特定配置。 --- ### 工具 (Tools) 使用 `tools` 配置控制在此模式下可用的工具。您可以透過將特定工具設定為 `true` 或 `false` 來啟用或禁用特定工具。 ```json { "mode": { "readonly": { "tools": { "write": false, "edit": false, "bash": false, "read": true, "grep": true, "glob": true } } } } ``` 如果未指定任何工具,則預設啟用所有工具。 --- #### 可用工具 這裡是所有可以透過模式配置控制的工具。 | 工具 | 描述 | | ----------- | ---------------- | | `bash` | 執行 shell 指令 | | `edit` | 修改現有檔案 | | `write` | 建立新檔案 | | `read` | 讀取檔案內容 | | `grep` | 搜尋檔案內容 | | `glob` | 按模式尋找檔案 | | `list` | 列出目錄內容 | | `patch` | 對檔案套用 Patch | | `todowrite` | 管理待辦事項清單 | | `todoread` | 閱讀待辦事項清單 | | `webfetch` | 取得網頁內容 | --- ## 自定義模式 您可以透過將自定義模式添加到配置來建立自己的自定義模式。以下是使用這兩種方法的範例: ### 使用 JSON 配置 ```json title="opencode.json" {4-14} { "$schema": "https://opencode.ai/config.json", "mode": { "docs": { "prompt": "{file:./prompts/documentation.txt}", "tools": { "write": true, "edit": true, "bash": false, "read": true, "grep": true, "glob": true } } } } ``` ### 使用 Markdown 檔案 在 `.opencode/modes/` 中為專案特定模式建立模式檔案,在 `~/.config/opencode/modes/` 中為全域模式建立模式檔案: ```markdown title=".opencode/modes/debug.md" --- temperature: 0.1 tools: bash: true read: true grep: true write: false edit: false --- You are in debug mode. Your primary goal is to help investigate and diagnose issues. Focus on: - Understanding the problem through careful analysis - Using bash commands to inspect system state - Reading relevant files and logs - Searching for patterns and anomalies - Providing clear explanations of findings Do not make any changes to files. Only investigate and report. ``` ```markdown title="~/.config/opencode/modes/refactor.md" --- model: anthropic/claude-sonnet-4-20250514 temperature: 0.2 tools: edit: true read: true grep: true glob: true --- You are in refactoring mode. Focus on improving code quality without changing functionality. Priorities: - Improve code readability and maintainability - Apply consistent naming conventions - Reduce code duplication - Optimize performance where appropriate - Ensure all tests continue to pass ``` --- ### 使用案例 以下是不同模式的一些常見使用案例。 - **建置模式**:啟用所有工具的完整開發工作 - **計畫模式**:分析和計畫,無需變更 - **審閱模式**:使用唯讀存取權限和文件工具進行程式碼審閱 - **除錯模式**:專注於啟用 bash 和讀取工具的調查 - **文件模式**:使用檔案操作但不使用系統指令的文件編寫 您可能還會發現不同的模型適用於不同的使用案例。