1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
---
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 和讀取工具的調查
- **文檔模式**:使用文件操作但不使用系統命令的文檔編寫
您可能還會發現不同的模型適用於不同的用例。
|