summaryrefslogtreecommitdiffhomepage
path: root/packages/web/src/content/docs/zh-tw/agents.mdx
blob: 7d72695031d73ecc4b60873a86301d7dc70267e9 (plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
---
title: Agents
description: 配置和使用專門的代理。
---

代理是專門的AI助手,可以針對特定任務和工作流程進行配置。它們允許您創建具有自定義提示、模型和工具訪問權限的專用工具。

:::tip
使用計劃代理來分析程式碼並審查建議,而無需進行任何程式碼更改。
:::

您可以在會話期間在代理之間切換,或使用 `@` 提及來調用它們。

---

## 類型

opencode 中有兩種類型的代理;主要代理人和次代理人。

---

### 主要代理

主要代理是與您直接互動的主要助手。您可以使用 **Tab** 鍵或您配置的 `switch_agent` 鍵綁定循環瀏覽它們。這些代理處理您的主要對話。工具訪問是通過權限配置的 - 例如,“構建”啟用了所有工具,而“計劃”則受到限制。

:::tip
您可以在會話期間使用 **Tab** 鍵在主要代理之間切換。
:::

opencode 附帶兩個內置的主要代理:**Build** 和 **Plan**。出色地
看看下面這些。

---

### 子代理

子代理是主要代理可以調用​​來執行特定任務的專業助手。您還可以通過在消息中**@提及**它們來手動調用它們。

opencode 附帶兩個內置子代理:**General** 和 **Explore**。我們將在下面看看這個。

---

## 內建

opencode 附帶兩個內置主代理和兩個內置子代理。

---

### 使用構建

_模式_:`primary`

構建是啟用所有工具的**默認**主要代理。這是用於需要完全訪問文件操作和系統命令的開發工作的標準代理。

---

### 使用計劃

_模式_:`primary`

專為規劃和分析而設計的受限代理。我們使用權限系統為您提供更多控制並防止意外更改。
默認情況下,以下所有項均設置為`ask`:

- `file edits`:所有寫入、修補和編輯
- `bash`:所有 bash 命令

當您希望 LLM 分析程式碼、建議更改或創建計劃而不對程式碼庫進行任何實際修改時,此代理非常有用。

---

### 使用一般

_模式_:`subagent`

用於研究複雜問題和執行多步驟任務的通用代理。具有完整的工具訪問權限(待辦事項除外),因此可以在需要時更改文件。使用它可以並行運行多個工作單元。

---

### 使用探索

_模式_:`subagent`

用於探索程式碼庫的快速只讀代理。無法修改文件。當您需要按模式快速查找文件、搜索程式碼中的關鍵字或回答有關程式碼庫的問題時,請使用此功能。

---

### 使用壓實

_模式_:`primary`

隱藏的系統代理,將長上下文壓縮為較小的摘要。它會在需要時自動運行,並且無法在 UI 中選擇。

---

### 使用標題

_模式_:`primary`

生成短會話標題的隱藏系統代理。它會自動運行,並且無法在 UI 中選擇。

---

### 使用總結

_模式_:`primary`

創建會話摘要的隱藏系統代理。它會自動運行,並且無法在 UI 中選擇。

---

## 用法

1. 對於主要代理,請在會話期間使用 **Tab** 鍵循環瀏覽它們。您還可以使用配置的 `switch_agent` 鍵綁定。

2. 可以調用子代理:
   - **自動**由主要代理根據其描述執行專門任務。
   - 通過在消息中**@提及**子代理手動進行。例如。

     ```txt frame="none"
     @general help me search for this function
     ```

3. **會話之間導航**:當子代理創建自己的子會話時,您可以使用以下命令在父會話和所有子會話之間導航:
   - **\<Leader>+Right**(或您配置的 `session_child_cycle` 鍵綁定)向前循環父級 → 子級 1 → 子級 2 → ... → 父級
   - **\<Leader>+Left**(或您配置的 `session_child_cycle_reverse` 鍵綁定)向後循環父級 ← 子級 1 ← 子級 2 ← ... ← 父級

   這使您可以在主要對話和專門的子代理工作之間無縫切換。

---

## 配置

您可以自定義內置代理或通過配置創建您自己的代理。可以通過兩種方式配置代理:

---

### JSON

在 `opencode.json` 配置文件中配置代理:

```json title="opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "agent": {
    "build": {
      "mode": "primary",
      "model": "anthropic/claude-sonnet-4-20250514",
      "prompt": "{file:./prompts/build.txt}",
      "tools": {
        "write": true,
        "edit": true,
        "bash": true
      }
    },
    "plan": {
      "mode": "primary",
      "model": "anthropic/claude-haiku-4-20250514",
      "tools": {
        "write": false,
        "edit": false,
        "bash": false
      }
    },
    "code-reviewer": {
      "description": "Reviews code for best practices and potential issues",
      "mode": "subagent",
      "model": "anthropic/claude-sonnet-4-20250514",
      "prompt": "You are a code reviewer. Focus on security, performance, and maintainability.",
      "tools": {
        "write": false,
        "edit": false
      }
    }
  }
}
```

---

### Markdown

您還可以使用 Markdown 文件定義代理。將它們放入:

- 全球:`~/.config/opencode/agents/`
- 每個項目:`.opencode/agents/`

```markdown title="~/.config/opencode/agents/review.md"
---
description: Reviews code for quality and best practices
mode: subagent
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` 代理。

---

## 選項

讓我們詳細看看這些配置選項。

---

### 描述

使用 `description` 選項提供代理的作用以及何時使用它的簡要描述。

```json title="opencode.json"
{
  "agent": {
    "review": {
      "description": "Reviews code for best practices and potential issues"
    }
  }
}
```

這是一個**必需的**配置選項。

---

### 溫度

使用 `temperature` 配置控制 LLM 響應的隨機性和創造性。

較低的值使響應更加集中和確定,而較高的值則增加創造力和可變性。

```json title="opencode.json"
{
  "agent": {
    "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"
{
  "agent": {
    "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。

---

### 最大步數

控制代理在被迫僅使用文本響應之前可以執行的最大代理迭代次數。這允許希望控製成本的用戶對代理操作設置限制。

如果未設置,代理將繼續迭代,直到模型選擇停止或用戶中斷會話。

```json title="opencode.json"
{
  "agent": {
    "quick-thinker": {
      "description": "Fast reasoning with limited iterations",
      "prompt": "You are a quick thinker. Solve problems with minimal steps.",
      "steps": 5
    }
  }
}
```

當達到限制時,代理會收到特殊的系統提示,指示其響應其工作摘要和建議的剩餘任務。

:::caution
舊版 `maxSteps` 字段已棄用。請改用`steps`。
:::

---

### 禁用

設置為 `true` 以禁用代理。

```json title="opencode.json"
{
  "agent": {
    "review": {
      "disable": true
    }
  }
}
```

---

### 迅速的

使用 `prompt` 配置為此代理指定自定義系統提示文件。提示文件應包含特定於代理目的的說明。

```json title="opencode.json"
{
  "agent": {
    "review": {
      "prompt": "{file:./prompts/code-review.txt}"
    }
  }
}
```

該路徑是相對於配置文件所在位置的。因此,這適用於全局 opencode 配置和項目特定配置。

---

### 模型

使用 `model` 配置覆蓋此代理的模型。對於使用針對不同任務優化的不同模型很有用。例如,更快的規劃模型、更強大的實施模型。

:::tip
如果您不指定模型,主代理將使用[全局配置的模型](/docs/config#models),而子代理將使用調用子代理的主代理的模型。
:::

```json title="opencode.json"
{
  "agent": {
    "plan": {
      "model": "anthropic/claude-haiku-4-20250514"
    }
  }
}
```

opencode 配置中的模型 ID 使用格式`provider/model-id`。例如,如果您使用 [OpenCode Zen](/docs/zen),則您將使用 `opencode/gpt-5.1-codex` 來表示 GPT 5.1 Codex。

---

### 工具

使用 `tools` 配置控制此代理中可用的工具。您可以通過將特定工具設置為`true` 或`false` 來啟用或禁用特定工具。

```json title="opencode.json" {3-6,9-12}
{
  "$schema": "https://opencode.ai/config.json",
  "tools": {
    "write": true,
    "bash": true
  },
  "agent": {
    "plan": {
      "tools": {
        "write": false,
        "bash": false
      }
    }
  }
}
```

:::note
特定於代理的配置會覆蓋全局配置。
:::

您還可以使用通配符同時控制多個工具。例如,要禁用 MCP 服務器中的所有工具:

```json title="opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "agent": {
    "readonly": {
      "tools": {
        "mymcp_*": false,
        "write": false,
        "edit": false
      }
    }
  }
}
```

[了解有關工具的更多信息](/docs/tools)。

---

### 權限

您可以配置權限來管理代理可以執行的操作。目前,`edit`、`bash` 和`webfetch` 工具的權限可以配置為:

- `"ask"` — 運行工具之前提示批准
- `"allow"` — 未經批准允許所有操作
- `"deny"` — 禁用該工具

```json title="opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "permission": {
    "edit": "deny"
  }
}
```

您可以覆蓋每個代理的這些權限。

```json title="opencode.json" {3-5,8-10}
{
  "$schema": "https://opencode.ai/config.json",
  "permission": {
    "edit": "deny"
  },
  "agent": {
    "build": {
      "permission": {
        "edit": "ask"
      }
    }
  }
}
```

您還可以在 Markdown 代理中設置權限。

```markdown title="~/.config/opencode/agents/review.md"
---
description: Code review without edits
mode: subagent
permission:
  edit: deny
  bash:
    "*": ask
    "git diff": allow
    "git log*": allow
    "grep *": allow
  webfetch: deny
---

Only analyze code and suggest changes.
```

您可以設置特定 bash 命令的權限。

```json title="opencode.json" {7}
{
  "$schema": "https://opencode.ai/config.json",
  "agent": {
    "build": {
      "permission": {
        "bash": {
          "git push": "ask",
          "grep *": "allow"
        }
      }
    }
  }
}
```

這可以採用全局模式。

```json title="opencode.json" {7}
{
  "$schema": "https://opencode.ai/config.json",
  "agent": {
    "build": {
      "permission": {
        "bash": {
          "git *": "ask"
        }
      }
    }
  }
}
```

您還可以使用`*`通配符來管理所有命令的權限。
由於最後一個匹配規則優先,因此將 `*` 通配符放在前面,將特定規則放在後面。

```json title="opencode.json" {8}
{
  "$schema": "https://opencode.ai/config.json",
  "agent": {
    "build": {
      "permission": {
        "bash": {
          "*": "ask",
          "git status *": "allow"
        }
      }
    }
  }
}
```

[了解有關權限的更多信息](/docs/permissions)。

---

### 模式

使用`mode` 配置控制代理的模式。 `mode` 選項用於確定如何使用代理。

```json title="opencode.json"
{
  "agent": {
    "review": {
      "mode": "subagent"
    }
  }
}
```

`mode` 選項可設置為`primary`、`subagent` 或`all`。如果未指定`mode`,則默認為`all`。

---

### 隱

使用 `@` 從 `hidden: true` 自動完成菜單隱藏子代理。對於只能由其他代理通過任務工具以編程方式調用的內部子代理很有用。

```json title="opencode.json"
{
  "agent": {
    "internal-helper": {
      "mode": "subagent",
      "hidden": true
    }
  }
}
```

這僅影響自動完成菜單中的用戶可見性。如果權限允許,模型仍然可以通過任務工具調用隱藏代理。

:::note
僅適用於`mode: subagent` 代理。
:::

---

### 任務權限

使用 `permission.task` 控制代理可以通過任務工具調用哪些子代理。使用 glob 模式進行靈活匹配。

```json title="opencode.json"
{
  "agent": {
    "orchestrator": {
      "mode": "primary",
      "permission": {
        "task": {
          "*": "deny",
          "orchestrator-*": "allow",
          "code-reviewer": "ask"
        }
      }
    }
  }
}
```

當設置為 `deny` 時,子代理將從任務工具描述中完全刪除,因此模型不會嘗試調用它。

:::tip
規則按順序評估,**最後匹配的規則獲勝**。在上面的示例中,`orchestrator-planner` 匹配`*`(拒絕)和`orchestrator-*`(允許),但由於`orchestrator-*` 位於`*` 之後,因此結果為`allow`。
:::

:::tip
用戶始終可以通過 `@` 自動完成菜單直接調用任何子代理,即使代理的任務權限會拒絕它。
:::

---

### 顏色

使用 `color` 選項自定義代理在 UI 中的視覺外觀。這會影響代理在介面中的顯示方式。

使用有效的十六進制顏色(例如`#FF5733`)或主題顏色:`primary`、`secondary`、`accent`、`success`、`warning`、`error`、`info`。

```json title="opencode.json"
{
  "agent": {
    "creative": {
      "color": "#ff6b6b"
    },
    "code-reviewer": {
      "color": "accent"
    }
  }
}
```

---

### 頂P

使用 `top_p` 選項控制響應多樣性。控制隨機性的溫度替代方案。

```json title="opencode.json"
{
  "agent": {
    "brainstorm": {
      "top_p": 0.9
    }
  }
}
```

值範圍從 0.0 到 1.0。較低的值更加集中,較高的值更加多樣化。

---

### 額外的

您在代理配置中指定的任何其他選項都將作為模型選項**直接**傳遞給提供商。這允許您使用特定於提供商的功能和參數。

例如,使用 OpenAI 的推理模型,您可以控制推理工作:

```json title="opencode.json" {6,7}
{
  "agent": {
    "deep-thinker": {
      "description": "Agent that uses high reasoning effort for complex problems",
      "model": "openai/gpt-5",
      "reasoningEffort": "high",
      "textVerbosity": "low"
    }
  }
}
```

這些附加選項是特定於型號和提供商的。檢查提供商的文檔以獲取可用參數。

:::tip
運行 `opencode models` 查看可用模型的列表。
:::

---

## 創建代理

您可以使用以下命令創建新代理:

```bash
opencode agent create
```

此交互式命令將:

1. 詢問代理保存在哪裡;全局或特定項目。
2. 描述代理應該做什麼。
3. 生成適當的系統提示和標識符。
4. 讓您選擇代理可以訪問哪些工具。
5. 最後,使用代理配置創建一個 markdown 文件。

---

## 使用案例

以下是不同代理的一些常見用例。

- **構建代理**:啟用所有工具的完整開發工作
- **規劃代理**:分析規劃,不做改動
- **審查代理**:具有隻讀訪問權限和文檔工具的程式碼審查
- **調試代理**:專注於啟用 bash 和讀取工具的調查
- **文檔代理**:使用文件操作但不使用系統命令的文檔編寫

---

## 示例

以下是一些您可能會覺得有用的示例代理。

:::tip
您有想要分享的代理嗎? [提交 PR](https://github.com/anomalyco/opencode)。
:::

---

### 文件代理

```markdown title="~/.config/opencode/agents/docs-writer.md"
---
description: Writes and maintains project documentation
mode: subagent
tools:
  bash: false
---

You are a technical writer. Create clear, comprehensive documentation.

Focus on:

- Clear explanations
- Proper structure
- Code examples
- User-friendly language
```

---

### 安全審核員

```markdown title="~/.config/opencode/agents/security-auditor.md"
---
description: Performs security audits and identifies vulnerabilities
mode: subagent
tools:
  write: false
  edit: false
---

You are a security expert. Focus on identifying potential security issues.

Look for:

- Input validation vulnerabilities
- Authentication and authorization flaws
- Data exposure risks
- Dependency vulnerabilities
- Configuration security issues
```