--- title: 模式 description: 不同的模式適用於不同的用例。 --- :::caution 現在通過 opencode 配置中的 `agent` 選項配置模式。這 `mode` 選項現已棄用。 [了解更多](/docs/agents)。 ::: opencode 中的模式允許您自定義不同用例的行為、工具和提示。 它具有兩種內置模式:**構建**和**計劃**。您可以定制 這些或通過 opencode 配置配置您自己的。 您可以在會話期間在模式之間切換或在配置文件中配置它們。 --- ## 內建 opencode 有兩種內置模式。 --- ### 建造 構建是啟用所有工具的**默認**模式。這是開發工作的標準模式,您需要完全訪問文件操作和系統命令。 --- ### 計劃 專為規劃和分析而設計的受限模式。在計劃模式下,默認情況下禁用以下工具: - `write` - 無法創建新文件 - `edit` - 無法修改現有文件,位於 `.opencode/plans/*.md` 的用於詳細說明計劃本身的文件除外 - `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` 配置覆蓋此模式的默認模型。對於使用針對不同任務優化的不同模型很有用。例如,更快的規劃模型、更強大的實施模型。 ```json title="opencode.json" { "mode": { "plan": { "model": "anthropic/claude-haiku-4-20250514" } } } ``` --- ### 溫度 使用 `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` 配置為此模式指定自定義系統提示文件。提示文件應包含特定於該模式用途的指令。 ```json title="opencode.json" { "mode": { "review": { "prompt": "{file:./prompts/code-review.txt}" } } } ``` 該路徑是相對於配置文件所在位置的。所以這適用於 全局 opencode 配置和項目特定配置。 --- ### 工具 使用 `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` | 對文件應用補丁 | | `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 和讀取工具的調查 - **文檔模式**:使用文件操作但不使用系統命令的文檔編寫 您可能還會發現不同的模型適用於不同的用例。