模式
不同用例的不同模式。
注意:模式现在通过
opencode配置中的 agent 选项进行配置。mode选项已弃用。了解更多。
opencode 中的模式允许你为不同用例自定义行为、工具和提示词。
它自带两个内置模式:build 和 plan。你可以自定义这些模式或通过 opencode 配置创建自己的模式。
你可以在会话期间切换模式,也可以在配置文件中进行配置。
内置模式
opencode 自带两个内置模式。
Build
Build 是默认模式,启用所有工具。这是标准的开发模式,你可以完全访问文件操作和系统命令。
Plan
一个受限模式,专为规划和分析设计。在 plan 模式下,以下工具默认被禁用:
write- 无法创建新文件edit- 无法修改现有文件,但可以编辑位于.opencode/plans/*.md的文件以详细记录计划本身patch- 无法应用补丁bash- 无法执行 shell 命令
当你希望 AI 分析代码、建议更改或创建计划而不对代码库进行任何实际修改时,此模式非常有用。
切换
你可以在会话期间使用 Tab 键切换模式。或者使用你配置的 switch_mode 快捷键。
另请参阅:格式化器了解代码格式化配置信息。
配置
你可以通过配置自定义内置模式或创建自己的模式。模式可以通过两种方式配置:
JSON 配置
在 opencode.json 配置文件中配置模式:
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/
~/.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 配置覆盖此模式的默认模型。适用于针对不同任务使用不同的优化模型。例如,规划使用更快的模型,实现使用更强大的模型。
opencode.json
{
"mode": {
"plan": {
"model": "anthropic/claude-haiku-4-20250514"
}
}
}温度
使用 temperature 配置控制 AI 响应的随机性和创造性。较低的值使响应更集中和确定性,较高的值增加创造性和多样性。
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:更具创造性和多样性的响应,适合头脑风暴和探索
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 配置为此模式指定自定义系统提示词文件。提示词文件应包含特定于该模式用途的指令。
opencode.json
{
"mode": {
"review": {
"prompt": "{file:./prompts/code-review.txt}"
}
}
}此路径相对于配置文件所在位置。因此这对全局 opencode 配置和项目特定配置都适用。
工具
使用 tools 配置控制此模式中可用的工具。你可以通过将特定工具设置为 true 或 false 来启用或禁用它们。
{
"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 配置
opencode.json
{
"$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/ 中创建全局模式文件:
.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.~/.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用例
以下是不同模式的一些常见用例。
- Build 模式:启用所有工具的完整开发工作
- Plan 模式:不进行更改的分析和规划
- Review 模式:只读访问加文档工具的代码审查
- Debug 模式:专注于调查,启用 bash 和 read 工具
- Docs 模式:文档编写,有文件操作但无系统命令
你可能还会发现不同的模型适合不同的用例。