wip(docs): i18n (#12681)

This commit is contained in:
Adam
2026-02-09 11:34:35 -06:00
committed by GitHub
parent f74c0339cc
commit dc53086c1e
642 changed files with 192745 additions and 509 deletions

View File

@@ -0,0 +1,67 @@
---
title: 遷移到 1.0
description: OpenCode 1.0 中的新增內容。
---
OpenCode 1.0 完全重寫了 TUI。
我們從存在性能和功能問題的基於 go+bubbletea 的 TUI 遷移到用 zig+solidjs 編寫的內部框架 (OpenTUI)。
新 TUI 的工作方式與舊 TUI 類似,因為它連接到相同的開放代碼服務器。
---
## 升級中
如果您當前正在使用以前的版本,則不應自動升級到 1.0
版本。然而,一些舊版本的 OpenCode 總是獲取最新版本。
要手動升級,請運行
```bash
$ opencode upgrade 1.0.0
```
要降級回 0.x請運行
```bash
$ opencode upgrade 0.15.31
```
---
## 用戶體驗變化
會話歷史記錄更加壓縮,僅顯示編輯和 bash 工具的完整詳細信息。
我們添加了一個命令欄,幾乎所有內容都經過它。按 ctrl+p 可在任何上下文中調出它並查看您可以執行的所有操作。
添加了包含有用信息的會話側邊欄(可以切換)。
我們刪除了一些我們不確定是否有人實際使用的功能。如果缺少重要內容,請提出問題,我們會盡快將其添加回來。
---
## 重大變化
### 按鍵綁定已重命名
- messages_revert -> messages_undo
- switch_agent -> agent_cycle
- switch_agent_reverse -> agent_cycle_reverse
- switch_mode -> agent_cycle
- switch_mode_reverse -> agent_cycle_reverse
### 按鍵綁定已移除
- 消息佈局切換
- 下一條消息
- 上一條消息
- 文件差異切換
- 文件搜索
- 文件關閉
- 文件列表
- 應用程序幫助
- 項目初始化
- 工具詳細信息
- 思維塊

View File

@@ -0,0 +1,156 @@
---
title: ACP 支持
description: 在任何 ACP 兼容編輯器中使用 OpenCode。
---
OpenCode 支持 [代理客戶端協議](https://agentclientprotocol.com) 或 (ACP),允許您直接在兼容的編輯器和 IDE 中使用它。
:::tip
有關支持 ACP 的編輯器和工具的列表,請查看[非加太進程進度報告](https://zed.dev/blog/acp-progress-report#available-now)。
:::
ACP 是一種開放協議,用於標準化代碼編輯器和 AI 編碼代理之間的通信。
---
## 配置
要通過 ACP 使用 OpenCode請將編輯器配置為運行 `opencode acp` 命令。
該命令將 OpenCode 作為 ACP 兼容的子進程啟動,通過 stdio 通過 JSON-RPC 與您的編輯器進行通信。
以下是支持 ACP 的流行編輯器的示例。
---
### 澤德
添加到您的[澤德](https://zed.dev)配置(`~/.config/zed/settings.json`)
```json title="~/.config/zed/settings.json"
{
"agent_servers": {
"OpenCode": {
"command": "opencode",
"args": ["acp"]
}
}
}
```
要打開它,請使用 **命令面板** 中的 `agent: new thread` 操作。
您還可以通過編輯`keymap.json`來綁定鍵盤快捷鍵:
```json title="keymap.json"
[
{
"bindings": {
"cmd-alt-o": [
"agent::NewExternalAgentThread",
{
"agent": {
"custom": {
"name": "OpenCode",
"command": {
"command": "opencode",
"args": ["acp"]
}
}
}
}
]
}
}
]
```
---
### JetBrains IDE
根據[文件](https://www.jetbrains.com/help/ai-assistant/acp.html)添加到你的[JetBrains IDE](https://www.jetbrains.com/) acp.json
```json title="acp.json"
{
"agent_servers": {
"OpenCode": {
"command": "/absolute/path/bin/opencode",
"args": ["acp"]
}
}
}
```
要打開它,請在 AI Chat 代理選擇器中使用新的“OpenCode”代理。
---
### 前衛
添加到您的[前衛](https://github.com/yetone/avante.nvim)配置:
```lua
{
acp_providers = {
["opencode"] = {
command = "opencode",
args = { "acp" }
}
}
}
```
如果需要傳遞環境變量:
```lua {6-8}
{
acp_providers = {
["opencode"] = {
command = "opencode",
args = { "acp" },
env = {
OPENCODE_API_KEY = os.getenv("OPENCODE_API_KEY")
}
}
}
}
```
---
### 代碼伴侶.nvim
要將 OpenCode 用作 [代碼伴侶.nvim](https://github.com/olimorris/codecompanion.nvim) 中的 ACP 代理,請將以下內容添加到 Neovim 配置中:
```lua
require("codecompanion").setup({
interactions = {
chat = {
adapter = {
name = "opencode",
model = "claude-sonnet-4",
},
},
},
})
```
此配置將 CodeCompanion 設置為使用 OpenCode 作為聊天的 ACP 代理。
如果您需要傳遞環境變量(如 `OPENCODE_API_KEY`),請參閱 CodeCompanion.nvim 文檔中的 [配置適配器:環境變量](https://codecompanion.olimorris.dev/getting-started#setting-an-api-key) 了解完整詳細信息。
## 支持
OpenCode 通過 ACP 的工作方式與在終端中的工作方式相同。支持所有功能:
:::note
目前不支持某些內置斜杠命令,例如 `/undo` 和 `/redo`。
:::
- 內置工具(文件操作、終端命令等)
- 自定義工具和斜杠命令
- 在 OpenCode 配置中配置的 MCP 服務器
- `AGENTS.md` 的項目特定規則
- 自定義格式化程序和 linter
- 代理和權限系統

View File

@@ -0,0 +1,747 @@
---
title: 代理商
description: 配置和使用專門的代理。
---
代理是專門的人工智能助手,可以針對特定任務和工作流程進行配置。它們允許您創建具有自定義提示、模型和工具訪問權限的專用工具。
:::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 文件定義代理。將它們放入:
- 全球:`~/.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 將使用特定於型號的默認值;大多數型號通常為 0Qwen 型號為 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`。例如,如果您使用 [開放代碼禪](/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
```

View File

@@ -0,0 +1,601 @@
---
title: 命令行界面
description: OpenCode CLI 選項和命令。
---
import { Tabs, TabItem } from "@astrojs/starlight/components"
默認情況下OpenCode CLI 在不帶任何參數運行時啟動[途易](/docs/tui)。
```bash
opencode
```
但它也接受本頁記錄的命令。這允許您以編程方式與 OpenCode 交互。
```bash
opencode run "Explain how closures work in JavaScript"
```
---
### tui
啟動 OpenCode 終端用戶界面。
```bash
opencode [project]
```
#### 旗幟
|旗幟|短|描述 |
| ------------ | ----- | ------------------------------------------ |
| `--continue` | `-c` |繼續上一期 |
| `--session` | `-s` |會話 ID 繼續 |
| `--prompt` | |提示使用|
| `--model` | `-m` |以提供者/模型形式使用的模型 |
| `--agent` | |代理使用|
| `--port` | |監聽端口 |
| `--hostname` | |監聽的主機名 |
---
## 命令
OpenCode CLI 還具有以下命令。
---
### agent
管理 OpenCode 代理。
```bash
opencode agent [command]
```
---
### attach
將終端連接到通過 `serve` 或 `web` 命令啟動的已運行的 OpenCode 後端服務器。
```bash
opencode attach [url]
```
這允許將 TUI 與遠程 OpenCode 後端一起使用。例如:
```bash
# Start the backend server for web/mobile access
opencode web --port 4096 --hostname 0.0.0.0
# In another terminal, attach the TUI to the running backend
opencode attach http://10.20.30.40:4096
```
#### 旗幟
|旗幟|短|描述 |
| ----------- | ----- | --------------------------------- |
| `--dir` | |啟動 TUI 的工作目錄 |
| `--session` | `-s` |會話 ID 繼續 |
---
#### create
使用自定義配置創建新代理。
```bash
opencode agent create
```
此命令將指導您使用自定義系統提示和工具配置創建新代理。
---
#### list
列出所有可用的代理。
```bash
opencode agent list
```
---
### auth
用於管理提供商的憑據和登錄的命令。
```bash
opencode auth [command]
```
---
#### login
OpenCode 由 [模型.dev](https://models.dev) 上的提供商列表提供支持,因此您可以使用 `opencode auth login` 為您想要使用的任何提供商配置 API 密鑰。它存儲在`~/.local/share/opencode/auth.json` 中。
```bash
opencode auth login
```
當 OpenCode 啟動時,它會從憑據文件加載提供程序。如果您的環境中定義了任何鍵或項目中的 `.env` 文件。
---
#### list
列出憑證文件中存儲的所有經過身份驗證的提供程序。
```bash
opencode auth list
```
或者簡短的版本。
```bash
opencode auth ls
```
---
#### logout
通過從憑據文件中清除提供程序,將您從提供程序中註銷。
```bash
opencode auth logout
```
---
### github
管理 GitHub 代理以實現存儲庫自動化。
```bash
opencode github [command]
```
---
#### install
在您的存儲庫中安裝 GitHub 代理。
```bash
opencode github install
```
這將設置必要的 GitHub Actions 工作流程並指導您完成配置過程。 [了解更多](/docs/github)。
---
#### run
運行 GitHub 代理。這通常用在 GitHub Actions 中。
```bash
opencode github run
```
##### 旗幟
|旗幟|描述 |
| --------- | -------------------------------------- |
| `--event` |用於運行代理的 GitHub 模擬事件 |
| `--token` | GitHub 個人訪問令牌 |
---
### mcp
管理模型上下文協議服務器。
```bash
opencode mcp [command]
```
---
#### add
將 MCP 服務器添加到您的配置中。
```bash
opencode mcp add
```
此命令將指導您添加本地或遠程 MCP 服務器。
---
#### list
列出所有已配置的 MCP 服務器及其連接狀態。
```bash
opencode mcp list
```
或者使用簡短版本。
```bash
opencode mcp ls
```
---
#### auth
使用啟用 OAuth 的 MCP 服務器進行身份驗證。
```bash
opencode mcp auth [name]
```
如果您不提供服務器名稱,系統將提示您從可用的支持 OAuth 的服務器中進行選擇。
您還可以列出支持 OAuth 的服務器及其身份驗證狀態。
```bash
opencode mcp auth list
```
或者使用簡短版本。
```bash
opencode mcp auth ls
```
---
#### logout
刪除 MCP 服務器的 OAuth 憑據。
```bash
opencode mcp logout [name]
```
---
#### debug
調試 MCP 服務器的 OAuth 連接問題。
```bash
opencode mcp debug <name>
```
---
### models
列出已配置提供商的所有可用模型。
```bash
opencode models [provider]
```
此命令以 `provider/model` 格式顯示您配置的提供程序中可用的所有模型。
這對於確定[你的配置](/docs/config/) 中使用的確切模型名稱很有用。
您可以選擇傳遞提供程序 ID 以按該提供程序篩選模型。
```bash
opencode models anthropic
```
#### 旗幟
|旗幟|描述 |
| ----------- | ------------------------------------------------------------ |
| `--refresh` |從 models.dev | 刷新模型緩存
| `--verbose` |使用更詳細的模型輸出(包括成本等元數據)|
使用`--refresh`標誌來更新緩存的模型列表。當新模型已添加到提供程序並且您希望在 OpenCode 中查看它們時,這非常有用。
```bash
opencode models --refresh
```
---
### run
通過直接傳遞提示以非交互模式運行 opencode。
```bash
opencode run [message..]
```
這對於腳本編寫、自動化,或者當您想要快速得到答案而不啟動完整的 TUI 時非常有用。例如。
```bash "opencode run"
opencode run Explain the use of context in Go
```
您還可以附加到正在運行的 `opencode serve` 實例,以避免每次運行時 MCP 服務器冷啟動時間:
```bash
# Start a headless server in one terminal
opencode serve
# In another terminal, run commands that attach to it
opencode run --attach http://localhost:4096 "Explain async/await in JavaScript"
```
#### 旗幟
|旗幟|短|描述 |
| ------------ | ----- | ------------------------------------------------------------------ |
| `--command` | |要運行的命令,使用消息作為參數 |
| `--continue` | `-c` |繼續上一期 |
| `--session` | `-s` |會話 ID 繼續 |
| `--share` | |分享會議 |
| `--model` | `-m` |以提供者/模型形式使用的模型 |
| `--agent` | |代理使用|
| `--file` | `-f` |要附加到消息的文件 |
| `--format` | |格式:默認(格式化)或 json原始 JSON 事件)|
| `--title` | |會話標題(如果未提供值,則使用截斷的提示)|
| `--attach` | |連接到正在運行的 opencode 服務器例如http://localhost:4096|
| `--port` | |本地服務器的端口(默認為隨機端口) |
---
### serve
啟動無頭 OpenCode 服務器以進行 API 訪問。查看[服務器文檔](/docs/server) 以獲取完整的 HTTP 接口。
```bash
opencode serve
```
這將啟動一個 HTTP 服務器,該服務器提供對 opencode 功能的 API 訪問,而無需 TUI 接口。設置`OPENCODE_SERVER_PASSWORD`以啟用HTTP基本身份驗證用戶名默認為`opencode`)。
#### 旗幟
|旗幟|描述 |
| ------------ | ------------------------------------------ |
| `--port` |監聽端口 |
| `--hostname` |監聽的主機名 |
| `--mdns` |啟用 mDNS 發現 |
| `--cors` |允許 CORS 的其他瀏覽器源 |
---
### session
管理 OpenCode 會話。
```bash
opencode session [command]
```
---
#### list
列出所有 OpenCode 會話。
```bash
opencode session list
```
##### 旗幟
|旗幟|短|描述 |
| ------------- | ----- | ------------------------------------ |
| `--max-count` | `-n` |限制為最近 N 個會話 |
| `--format` | |輸出格式table 或 json(table) |
---
### stats
顯示 OpenCode 會話的令牌使用情況和成本統計信息。
```bash
opencode stats
```
#### 旗幟
|旗幟|描述 |
| ----------- | --------------------------------------------------------------------------- |
| `--days` |顯示過去 N 天(所有時間)的統計數據 |
| `--tools` |顯示的工具數量(全部)|
| `--models` |顯示模型使用情況細分(默認隱藏)。傳遞一個數字來顯示前 N |
| `--project` |按項目過濾(所有項目,空字符串:當前項目)|
---
### export
將會話數據導出為 JSON。
```bash
opencode export [sessionID]
```
如果您不提供會話 ID系統將提示您從可用會話中進行選擇。
---
### import
從 JSON 文件或 OpenCode 共享 URL 導入會話數據。
```bash
opencode import <file>
```
您可以從本地文件或 OpenCode 共享 URL 導入。
```bash
opencode import session.json
opencode import https://opncd.ai/s/abc123
```
---
### web
使用 Web 界面啟動無頭 OpenCode 服務器。
```bash
opencode web
```
這將啟動 HTTP 服務器並打開 Web 瀏覽器以通過 Web 界面訪問 OpenCode。設置`OPENCODE_SERVER_PASSWORD`以啟用HTTP基本身份驗證用戶名默認為`opencode`)。
#### 旗幟
|旗幟|描述 |
| ------------ | ------------------------------------------ |
| `--port` |監聽端口 |
| `--hostname` |監聽的主機名 |
| `--mdns` |啟用 mDNS 發現 |
| `--cors` |允許 CORS 的其他瀏覽器源 |
---
### acp
啟動 ACP代理客戶端協議服務器。
```bash
opencode acp
```
此命令啟動一個 ACP 服務器,該服務器使用 nd-JSON 通過 stdin/stdout 進行通信。
#### 旗幟
|旗幟|描述 |
| ------------ | --------------------- |
| `--cwd` |工作目錄 |
| `--port` |監聽端口 |
| `--hostname` |監聽的主機名 |
---
### uninstall
卸載 OpenCode 並刪除所有相關文件。
```bash
opencode uninstall
```
#### 旗幟
|旗幟|短|描述 |
| --------------- | ----- | ------------------------------------------- |
| `--keep-config` | `-c` |保留配置文件|
| `--keep-data` | `-d` |保留會話數據和快照 |
| `--dry-run` | |顯示在不刪除的情況下將刪除的內容 |
| `--force` | `-f` |跳過確認提示 |
---
### upgrade
將 opencode 更新到最新版本或特定版本。
```bash
opencode upgrade [target]
```
升級到最新版本。
```bash
opencode upgrade
```
升級到特定版本。
```bash
opencode upgrade v0.1.48
```
#### 旗幟
|旗幟|短|描述 |
| ---------- | ----- | ----------------------------------------------------------------- |
| `--method` | `-m` |使用的安裝方法捲曲、npm、pnpm、bun、brew |
---
## 全球旗幟
opencode CLI 採用以下全局標誌。
|旗幟|短|描述 |
| -------------- | ----- | ------------------------------------ |
| `--help` | `-h` |顯示幫助|
| `--version` | `-v` |打印版本號 |
| `--print-logs` | |將日誌打印到 stderr |
| `--log-level` | |日誌級別(調試、信息、警告、錯誤)|
---
## 環境變量
可以使用環境變量配置 OpenCode。
|變量|類型 |描述 |
| ------------------------------------- | ------- | ------------------------------------------------- |
| `OPENCODE_AUTO_SHARE` |布爾 |自動共享會話 |
| `OPENCODE_GIT_BASH_PATH` |字符串| Windows 上 Git Bash 可執行文件的路徑 |
| `OPENCODE_CONFIG` |字符串|配置文件路徑 |
| `OPENCODE_CONFIG_DIR` |字符串|配置目錄的路徑 |
| `OPENCODE_CONFIG_CONTENT` |字符串|內聯 json 配置內容 |
| `OPENCODE_DISABLE_AUTOUPDATE` |布爾 |禁用自動更新檢查 |
| `OPENCODE_DISABLE_PRUNE` |布爾 |禁用舊數據的修剪 |
| `OPENCODE_DISABLE_TERMINAL_TITLE` |布爾 |禁用自動終端標題更新 |
| `OPENCODE_PERMISSION` |字符串|內聯 json 權限配置 |
| `OPENCODE_DISABLE_DEFAULT_PLUGINS` |布爾 |禁用默認插件 |
| `OPENCODE_DISABLE_LSP_DOWNLOAD` |布爾 |禁用自動 LSP 服務器下載 |
| `OPENCODE_ENABLE_EXPERIMENTAL_MODELS` |布爾 |啟用實驗模型 |
| `OPENCODE_DISABLE_AUTOCOMPACT` |布爾 |禁用自動上下文壓縮 |
| `OPENCODE_DISABLE_CLAUDE_CODE` |布爾 |禁止從`.claude`讀取(提示+技巧)|
| `OPENCODE_DISABLE_CLAUDE_CODE_PROMPT` |布爾 |禁用讀取`~/.claude/CLAUDE.md` |
| `OPENCODE_DISABLE_CLAUDE_CODE_SKILLS` |布爾 |禁用加載`.claude/skills` |
| `OPENCODE_DISABLE_MODELS_FETCH` |布爾 |禁用從遠程源獲取模型 |
| `OPENCODE_FAKE_VCS` |字符串|用於測試目的的假 VCS 提供商 |
| `OPENCODE_DISABLE_FILETIME_CHECK` |布爾 |禁用文件時間檢查以進行優化 |
| `OPENCODE_CLIENT` |字符串|客戶端標識符(默認為`cli`|
| `OPENCODE_ENABLE_EXA` |布爾 |啟用 Exa 網絡搜索工具 |
| `OPENCODE_SERVER_PASSWORD` |字符串|為 `serve`/`web` 啟用基本身份驗證 |
| `OPENCODE_SERVER_USERNAME` |字符串|覆蓋基本身份驗證用戶名(默認`opencode`|
| `OPENCODE_MODELS_URL` |字符串|用於獲取模型配置的自定義 URL |
---
### 實驗性的
這些環境變量啟用可能會更改或刪除的實驗性功能。
|變量|類型 |描述 |
| ----------------------------------------------- | ------- | --------------------------------------- |
| `OPENCODE_EXPERIMENTAL` |布爾 |啟用所有實驗性功能 |
| `OPENCODE_EXPERIMENTAL_ICON_DISCOVERY` |布爾 |啟用圖標發現 |
| `OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT` |布爾 |在 TUI 中禁用選擇時復制 |
| `OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS` |數量 | bash 命令的默認超時(以毫秒為單位) |
| `OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX` |數量 | LLM 響應的最大輸出令牌 |
| `OPENCODE_EXPERIMENTAL_FILEWATCHER` |布爾 |為整個目錄啟用文件觀察器 |
| `OPENCODE_EXPERIMENTAL_OXFMT` |布爾 |啟用 oxfmt 格式化程序 |
| `OPENCODE_EXPERIMENTAL_LSP_TOOL` |布爾 |啟用實驗性 LSP 工具 |
| `OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER` |布爾 |禁用文件觀察器 |
| `OPENCODE_EXPERIMENTAL_EXA` |布爾 |啟用實驗性 Exa 功能 |
| `OPENCODE_EXPERIMENTAL_LSP_TY` |布爾 |啟用實驗性 LSP 類型檢查 |
| `OPENCODE_EXPERIMENTAL_MARKDOWN` |布爾 |啟用實驗性 Markdown 功能 |
| `OPENCODE_EXPERIMENTAL_PLAN_MODE` |布爾 |啟用計劃模式 |

View File

@@ -0,0 +1,323 @@
---
title: 命令
description: 為重複任務創建自定義命令。
---
自定義命令允許您指定在 TUI 中執行該命令時要運行的提示。
```bash frame="none"
/my-command
```
除了`/init`、`/undo`、`/redo`、`/share`、`/help` 等內置命令之外,還有自定義命令。 [了解更多](/docs/tui#commands)。
---
## 創建命令文件
在`commands/`目錄中創建markdown文件來定義自定義命令。
創建`.opencode/commands/test.md`
```md title=".opencode/commands/test.md"
---
description: Run tests with coverage
agent: build
model: anthropic/claude-3-5-sonnet-20241022
---
Run the full test suite with coverage report and show any failures.
Focus on the failing tests and suggest fixes.
```
frontmatter 定義命令屬性。內容成為模板。
通過鍵入 `/` 後跟命令名稱來使用該命令。
```bash frame="none"
"/test"
```
---
## 配置
您可以通過 OpenCode 配置或通過在 `commands/` 目錄中創建 markdown 文件來添加自定義命令。
---
### JSON
在 OpenCode [配置](/docs/config) 中使用 `command` 選項:
```json title="opencode.jsonc" {4-12}
{
"$schema": "https://opencode.ai/config.json",
"command": {
// This becomes the name of the command
"test": {
// This is the prompt that will be sent to the LLM
"template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes.",
// This is shown as the description in the TUI
"description": "Run tests with coverage",
"agent": "build",
"model": "anthropic/claude-3-5-sonnet-20241022"
}
}
}
```
現在您可以在 TUI 中運行此命令:
```bash frame="none"
/test
```
---
### 降價
您還可以使用 Markdown 文件定義命令。將它們放入:
- 全球:`~/.config/opencode/commands/`
- 每個項目:`.opencode/commands/`
```markdown title="~/.config/opencode/commands/test.md"
---
description: Run tests with coverage
agent: build
model: anthropic/claude-3-5-sonnet-20241022
---
Run the full test suite with coverage report and show any failures.
Focus on the failing tests and suggest fixes.
```
Markdown 文件名成為命令名。例如,`test.md` 讓
你運行:
```bash frame="none"
/test
```
---
## 提示配置
自定義命令的提示支持幾個特殊的佔位符和語法。
---
### 論據
使用 `$ARGUMENTS` 佔位符將參數傳遞給命令。
```md title=".opencode/commands/component.md"
---
description: Create a new component
---
Create a new React component named $ARGUMENTS with TypeScript support.
Include proper typing and basic structure.
```
使用參數運行命令:
```bash frame="none"
/component Button
```
`$ARGUMENTS` 將替換為 `Button`。
您還可以使用位置參數訪問各個參數:
- `$1` - 第一個參數
- `$2` - 第二個參數
- `$3` - 第三個參數
- 等等...
例如:
```md title=".opencode/commands/create-file.md"
---
description: Create a new file with content
---
Create a file named $1 in the directory $2
with the following content: $3
```
運行命令:
```bash frame="none"
/create-file config.json src "{ \"key\": \"value\" }"
```
這取代了:
- `$1` 與`config.json`
- `$2` 與`src`
- `$3` 與`{ "key": "value" }`
---
### 外殼輸出
使用 _!`command`_ 將 [bash命令](/docs/tui#bash-commands) 輸出注入到提示符中。
例如,要創建分析測試覆蓋率的自定義命令:
```md title=".opencode/commands/analyze-coverage.md"
---
description: Analyze test coverage
---
Here are the current test results:
!`npm test`
Based on these results, suggest improvements to increase coverage.
```
或者查看最近的更改:
```md title=".opencode/commands/review-changes.md"
---
description: Review recent changes
---
Recent git commits:
!`git log --oneline -10`
Review these changes and suggest any improvements.
```
命令在項目的根目錄中運行,其輸出成為提示的一部分。
---
### 文件參考
使用 `@` 後跟文件名將文件包含在命令中。
```md title=".opencode/commands/review-component.md"
---
description: Review component
---
Review the component in @src/components/Button.tsx.
Check for performance issues and suggest improvements.
```
文件內容會自動包含在提示中。
---
## 選項
讓我們詳細看看配置選項。
---
### 模板
`template` 選項定義執行命令時將發送到 LLM 的提示。
```json title="opencode.json"
{
"command": {
"test": {
"template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes."
}
}
}
```
這是一個**必需的**配置選項。
---
### 描述
使用 `description` 選項提供命令功能的簡要描述。
```json title="opencode.json"
{
"command": {
"test": {
"description": "Run tests with coverage"
}
}
}
```
當您鍵入命令時,這將在 TUI 中顯示為描述。
---
### 代理人
使用`agent` 配置可選擇指定哪個[代理人](/docs/agents) 應執行此命令。
如果這是 [子代理](/docs/agents/#subagents) 該命令將默認觸發子代理調用。
要禁用此行為,請將`subtask` 設置為`false`。
```json title="opencode.json"
{
"command": {
"review": {
"agent": "plan"
}
}
}
```
這是一個**可選**配置選項。如果未指定,則默認為您當前的代理。
---
### 子任務
使用`subtask` 布爾值強制命令觸發[子代理](/docs/agents/#subagents) 調用。
如果您希望命令不污染您的主要上下文並且將**強制**代理充當子代理,那麼這非常有用,
即使`mode` 在[代理人](/docs/agents) 配置上設置為`primary`。
```json title="opencode.json"
{
"command": {
"analyze": {
"subtask": true
}
}
}
```
這是一個**可選**配置選項。
---
### 模型
使用 `model` 配置覆蓋此命令的默認模型。
```json title="opencode.json"
{
"command": {
"analyze": {
"model": "anthropic/claude-3-5-sonnet-20241022"
}
}
}
```
這是一個**可選**配置選項。
---
## 內建
opencode包含`/init`、`/undo`、`/redo`、`/share`、`/help`等內置命令; [了解更多](/docs/tui#commands)。
:::note
自定義命令可以覆蓋內置命令。
:::
如果您定義同名的自定義命令,它將覆蓋內置命令。

View File

@@ -0,0 +1,685 @@
---
title: 配置
description: 使用 OpenCode JSON 配置。
---
您可以使用 JSON 配置文件配置 OpenCode。
---
## 格式
OpenCode 支持 **JSON** 和 **JSONC**(帶註釋的 JSON格式。
```jsonc title="opencode.jsonc"
{
"$schema": "https://opencode.ai/config.json",
// Theme configuration
"theme": "opencode",
"model": "anthropic/claude-sonnet-4-5",
"autoupdate": true,
}
```
---
## 地點
您可以將配置放置在幾個不同的位置,它們有一個
不同的優先順序。
:::note
配置文件**合併在一起**,而不是替換。
:::
配置文件被合併在一起,而不是被替換。以下配置位置的設置被合併。僅當密鑰衝突時,後面的配置才會覆蓋前面的配置。保留所有配置中的非衝突設置。
例如,如果您的全局配置設置`theme: "opencode"`和`autoupdate: true`,並且您的項目配置設置`model: "anthropic/claude-sonnet-4-5"`,則最終配置將包括所有三個設置。
---
### 優先順序
配置源按以下順序加載(後面的源覆蓋前面的源):
1. **遠程配置**(來自`.well-known/opencode`-組織默認值
2. **全局配置** (`~/.config/opencode/opencode.json`) - 用戶首選項
3. **自定義配置** (`OPENCODE_CONFIG` env var) - 自定義覆蓋
4. **項目配置**(項目中的`opencode.json`- 項目特定的設置
5. **`.opencode` 目錄** - 代理、命令、插件
6. **內聯配置** (`OPENCODE_CONFIG_CONTENT` env var) - 運行時覆蓋
這意味著項目配置可以覆蓋全局默認值,全局配置可以覆蓋遠程組織默認值。
:::note
`.opencode` 和`~/.config/opencode` 目錄對子目錄使用**複數名稱**`agents/`、`commands/`、`modes/`、`plugins/`、`skills/`、`tools/` 和`themes/`。為了向後兼容,還支持單數名稱(例如`agent/`)。
:::
---
### 偏僻的
組織可以通過 `.well-known/opencode` 端點提供默認配置。當您向支持它的提供商進行身份驗證時,會自動獲取該信息。
首先加載遠程配置,作為基礎層。所有其他配置源(全局、項目)都可以覆蓋這些默認值。
例如,如果您的組織提供默認禁用的 MCP 服務器:
```json title="Remote config from .well-known/opencode"
{
"mcp": {
"jira": {
"type": "remote",
"url": "https://jira.example.com/mcp",
"enabled": false
}
}
}
```
您可以在本地配置中啟用特定服務器:
```json title="opencode.json"
{
"mcp": {
"jira": {
"type": "remote",
"url": "https://jira.example.com/mcp",
"enabled": true
}
}
}
```
---
### 全球的
將全局 OpenCode 配置放在 `~/.config/opencode/opencode.json` 中。使用全局配置來實現用戶範圍的首選項,例如主題、提供程序或按鍵綁定。
全局配置覆蓋遠程組織默認值。
---
### 每個項目
在項目根目錄中添加`opencode.json`。項目配置在標準配置文件中具有最高優先級 - 它覆蓋全局配置和遠程配置。
:::tip
將項目特定配置放在項目的根目錄中。
:::
當 OpenCode 啟動時,它會在當前目錄中查找配置文件或遍歷到最近的 Git 目錄。
這也可以安全地簽入 Git 並使用與全局模式相同的模式。
---
### 自定義路徑
使用 `OPENCODE_CONFIG` 環境變量指定自定義配置文件路徑。
```bash
export OPENCODE_CONFIG=/path/to/my/custom-config.json
opencode run "Hello world"
```
自定義配置按優先順序在全局配置和項目配置之間加載。
---
### 自定義目錄
使用`OPENCODE_CONFIG_DIR`指定自定義配置目錄
環境變量。將在該目錄中搜索代理、命令、
模式和插件就像標準`.opencode`目錄一樣,並且應該
遵循相同的結構。
```bash
export OPENCODE_CONFIG_DIR=/path/to/my/config-directory
opencode run "Hello world"
```
自定義目錄在全局配置和`.opencode`目錄之後加載,因此它**可以覆蓋**它們的設置。
---
## 模式
配置文件具有在 [**`opencode.ai/config.json`**](https://opencode.ai/config.json) 中定義的架構。
您的編輯器應該能夠根據架構進行驗證和自動完成。
---
### TUI
您可以通過 `tui` 選項配置特定於 TUI 的設置。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"tui": {
"scroll_speed": 3,
"scroll_acceleration": {
"enabled": true
},
"diff_style": "auto"
}
}
```
可用選項:
- `scroll_acceleration.enabled` - 啟用 macOS 風格的滾動加速。 **優先於`scroll_speed`。 **
- `scroll_speed` - 自定義滾動速度倍增(默認值:`3`,最小值:`1`)。如果`scroll_acceleration.enabled` 是`true`,則忽略。
- `diff_style` - 控制差異渲染。 `"auto"` 適應終端寬度,`"stacked"` 始終顯示單列。
[在此處了解有關使用 TUI 的更多信息](/docs/tui)。
---
### 伺服器
您可以通過`server` 選項為`opencode serve` 和`opencode web` 命令配置服務器設置。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"server": {
"port": 4096,
"hostname": "0.0.0.0",
"mdns": true,
"mdnsDomain": "myproject.local",
"cors": ["http://localhost:5173"]
}
}
```
可用選項:
- `port` - 監聽的端口。
- `hostname` - 要監聽的主機名。當`mdns`啟用且未設置主機名時,默認為`0.0.0.0`。
- `mdns` - 啟用 mDNS 服務發現。這允許網絡上的其他設備發現您的 OpenCode 服務器。
- `mdnsDomain` - mDNS 服務的自定義域名。默認為`opencode.local`。對於在同一網絡上運行多個實例很有用。
- `cors` - 從基於瀏覽器的客戶端使用 HTTP 服務器時允許 CORS 的其他來源。值必須是完整來源(方案+主機+可選端口),例如`https://app.example.com`。
[在此處了解有關服務器的更多信息](/docs/server)。
---
### 工具
您可以通過 `tools` 選項管理法學碩士可以使用的工具。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"tools": {
"write": false,
"bash": false
}
}
```
[在此處了解有關工具的更多信息](/docs/tools)。
---
### 型號
您可以通過 `provider`、`model` 和 `small_model` 選項來配置要在 OpenCode 配置中使用的提供程序和模型。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"provider": {},
"model": "anthropic/claude-sonnet-4-5",
"small_model": "anthropic/claude-haiku-4-5"
}
```
`small_model` 選項為標題生成等輕量級任務配置單獨的模型。默認情況下如果您的提供商可以提供更便宜的模型OpenCode 會嘗試使用更便宜的模型,否則它會回退到您的主模型。
提供者選項可以包括`timeout`和`setCacheKey`
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"timeout": 600000,
"setCacheKey": true
}
}
}
}
```
- `timeout` - 請求超時以毫秒為單位默認值300000。設置為 `false` 以禁用。
- `setCacheKey` - 確保始終為指定的提供者設置緩存密鑰。
您還可以配置[本地模型](/docs/models#local)。 [了解更多](/docs/models)。
---
#### 特定於提供商的選項
某些提供程序支持除通用`timeout` 和`apiKey` 設置之外的其他配置選項。
##### 亞馬遜基岩
Amazon Bedrock 支持 AWS 特定配置:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"amazon-bedrock": {
"options": {
"region": "us-east-1",
"profile": "my-aws-profile",
"endpoint": "https://bedrock-runtime.us-east-1.vpce-xxxxx.amazonaws.com"
}
}
}
}
```
- `region` - Bedrock 的 AWS 區域(默認為 `AWS_REGION` env var 或 `us-east-1`
- `profile` - 來自 `~/.aws/credentials` 的 AWS 命名配置文件(默認為 `AWS_PROFILE` env var
- `endpoint` - VPC 終端節點的自定義終端節點 URL。這是使用 AWS 特定術語的通用 `baseURL` 選項的別名。如果兩者都指定,`endpoint` 優先。
:::note
承載令牌(`AWS_BEARER_TOKEN_BEDROCK` 或`/connect`)優先於基於配置文件的身份驗證。詳情請參閱[認證優先級](/docs/providers#authentication-precedence)。
:::
[了解有關 Amazon Bedrock 配置的更多信息](/docs/providers#amazon-bedrock)。
---
### 主題
您可以通過 `theme` 選項在 OpenCode 配置中配置要使用的主題。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"theme": ""
}
```
[在這裡了解更多](/docs/themes)。
---
### 代理商
您可以通過 `agent` 選項為特定任務配置專用代理。
```jsonc title="opencode.jsonc"
{
"$schema": "https://opencode.ai/config.json",
"agent": {
"code-reviewer": {
"description": "Reviews code for best practices and potential issues",
"model": "anthropic/claude-sonnet-4-5",
"prompt": "You are a code reviewer. Focus on security, performance, and maintainability.",
"tools": {
// Disable file modification tools for review-only agent
"write": false,
"edit": false,
},
},
},
}
```
您還可以使用 `~/.config/opencode/agents/` 或 `.opencode/agents/` 中的 markdown 文件定義代理。 [在這裡了解更多](/docs/agents)。
---
### 默認代理
您可以使用 `default_agent` 選項設置默認代理。當沒有明確指定時,這將確定使用哪個代理。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"default_agent": "plan"
}
```
默認代理必須是主代理(而不是子代理)。這可以是內置代理,例如`"build"`或`"plan"`,或者您定義的[定制代理](/docs/agents)。如果指定的代理不存在或者是子代理OpenCode 將回退到 `"build"` 並發出警告。
此設置適用於所有界面TUI、CLI (`opencode run`)、桌面應用程序和 GitHub Action。
---
### 分享
您可以通過`share`選項配置[分享](/docs/share)功能。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"share": "manual"
}
```
這需要:
- `"manual"` - 允許通過命令手動共享(默認)
- `"auto"` - 自動分享新對話
- `"disabled"` - 完全禁用共享
默認情況下,共享設置為手動模式,您需要使用 `/share` 命令顯式共享對話。
---
### 命令
您可以通過`command`選項為重複任務配置自定義命令。
```jsonc title="opencode.jsonc"
{
"$schema": "https://opencode.ai/config.json",
"command": {
"test": {
"template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes.",
"description": "Run tests with coverage",
"agent": "build",
"model": "anthropic/claude-haiku-4-5",
},
"component": {
"template": "Create a new React component named $ARGUMENTS with TypeScript support.\nInclude proper typing and basic structure.",
"description": "Create a new component",
},
},
}
```
您還可以使用 `~/.config/opencode/commands/` 或 `.opencode/commands/` 中的 Markdown 文件定義命令。 [在這裡了解更多](/docs/commands)。
---
### 按鍵綁定
您可以通過 `keybinds` 選項自定義您的按鍵綁定。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"keybinds": {}
}
```
[在這裡了解更多](/docs/keybinds)。
---
### 自動更新
OpenCode 將在啟動時自動下載任何新的更新。您可以使用 `autoupdate` 選項禁用此功能。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"autoupdate": false
}
```
如果您不想更新但希望在新版本可用時收到通知,請將`autoupdate` 設置為`"notify"`。
請注意,這僅在未使用 Homebrew 等包管理器安裝時才有效。
---
### 格式化程序
您可以通過`formatter`選項配置代碼格式化程序。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"formatter": {
"prettier": {
"disabled": true
},
"custom-prettier": {
"command": ["npx", "prettier", "--write", "$FILE"],
"environment": {
"NODE_ENV": "development"
},
"extensions": [".js", ".ts", ".jsx", ".tsx"]
}
}
}
```
[在此處了解有關格式化程序的更多信息](/docs/formatters)。
---
### 權限
默認情況下opencode **允許所有操作**,無需明確批准。您可以使用`permission` 選項更改此設置。
例如,要確保 `edit` 和 `bash` 工具需要用戶批准:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"edit": "ask",
"bash": "ask"
}
}
```
[在此處了解有關權限的更多信息](/docs/permissions)。
---
### 壓實
您可以通過 `compaction` 選項控制上下文壓縮行為。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"compaction": {
"auto": true,
"prune": true
}
}
```
- `auto` - 當上下文已滿時自動壓縮會話(默認值:`true`)。
- `prune` - 刪除舊工具輸出以保存令牌(默認值:`true`)。
---
### 守望者
您可以通過`watcher`選項配置文件觀察器忽略模式。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"watcher": {
"ignore": ["node_modules/**", "dist/**", ".git/**"]
}
}
```
模式遵循 glob 語法。使用它可以從文件監視中排除嘈雜的目錄。
---
### MCP服務器
您可以通過 `mcp` 選項配置要使用的 MCP 服務器。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"mcp": {}
}
```
[在這裡了解更多](/docs/mcp-servers)。
---
### 插件
[插件](/docs/plugins) 使用自定義工具、掛鉤和集成擴展 OpenCode。
將插件文件放置在`.opencode/plugins/` 或`~/.config/opencode/plugins/` 中。您還可以通過 `plugin` 選項從 npm 加載插件。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-helicone-session", "@my-org/custom-plugin"]
}
```
[在這裡了解更多](/docs/plugins)。
---
### 指示
您可以通過 `instructions` 選項配置您正在使用的型號的說明。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"instructions": ["CONTRIBUTING.md", "docs/guidelines.md", ".cursor/rules/*.md"]
}
```
這需要指令文件的路徑和全局模式數組。 [了解更多
關於規則在這裡](/docs/rules)。
---
### 殘疾服務提供者
您可以通過 `disabled_providers` 選項禁用自動加載的提供程序。當您想要阻止加載某些提供程序(即使其憑據可用)時,這非常有用。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"disabled_providers": ["openai", "gemini"]
}
```
:::note
`disabled_providers` 優先於`enabled_providers`。
:::
`disabled_providers` 選項接受提供者 ID 數組。當提供者被禁用時:
- 即使設置了環境變量也不會加載。
- 即使通過 `/connect` 命令配置 API 密鑰,也不會加載它。
- 提供商的型號不會出現在型號選擇列表中。
---
### 啟用的提供商
您可以通過 `enabled_providers` 選項指定提供商的允許列表。設置後,僅啟用指定的提供程序,所有其他提供程序將被忽略。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"enabled_providers": ["anthropic", "openai"]
}
```
當您想要限制 OpenCode 僅使用特定的提供程序而不是一一禁用它們時,這非常有用。
:::note
`disabled_providers` 優先於`enabled_providers`。
:::
如果某個提供程序同時出現在`enabled_providers` 和`disabled_providers` 中,則`disabled_providers` 優先考慮向後兼容性。
---
### 實驗性的
`experimental` 鍵包含正在積極開發的選項。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"experimental": {}
}
```
:::caution
實驗選項不穩定。它們可能會更改或被刪除,恕不另行通知。
:::
---
## 變量
您可以在配置文件中使用變量替換來引用環境變量和文件內容。
---
### 環境變量
使用`{env:VARIABLE_NAME}`替換環境變量:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"model": "{env:OPENCODE_MODEL}",
"provider": {
"anthropic": {
"models": {},
"options": {
"apiKey": "{env:ANTHROPIC_API_KEY}"
}
}
}
}
```
如果未設置環境變量,它將被替換為空字符串。
---
### 文件
使用 `{file:path/to/file}` 替換文件的內容:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"instructions": ["./custom-instructions.md"],
"provider": {
"openai": {
"options": {
"apiKey": "{file:~/.secrets/openai-key}"
}
}
}
}
```
文件路徑可以是:
- 相對於配置文件目錄
- 或者以 `/` 或 `~` 開頭的絕對路徑
這些對於:
- 將 API 密鑰等敏感數據保存在單獨的文件中。
- 包含大型指令文件,而不會弄亂您的配置。
- 跨多個配置文件共享通用配置片段。

View File

@@ -0,0 +1,170 @@
---
title: 定制工具
description: 創建法學碩士可以在開放代碼中調用的工具。
---
自定義工具是您創建的函數LLM 可以在對話期間調用。它們與 opencode 的 [內置工具](/docs/tools) 一起工作,例如 `read`、`write` 和 `bash`。
---
## 創建工具
工具定義為 **TypeScript** 或 **JavaScript** 文件。但是,工具定義可以調用用 **任何語言** 編寫的腳本 - TypeScript 或 JavaScript 僅用於工具定義本身。
---
### 地點
它們可以定義為:
- 通過將它們放在項目的 `.opencode/tools/` 目錄中來本地進行。
- 或者在全局範圍內,將它們放置在 `~/.config/opencode/tools/` 中。
---
### 結構
創建工具的最簡單方法是使用 `tool()` 幫助程序,它提供類型安全和驗證。
```ts title=".opencode/tools/database.ts" {1}
import { tool } from "@opencode-ai/plugin"
export default tool({
description: "Query the project database",
args: {
query: tool.schema.string().describe("SQL query to execute"),
},
async execute(args) {
// Your database logic here
return `Executed query: ${args.query}`
},
})
```
**文件名**成為**工具名稱**。以上創建了一個 `database` 工具。
---
#### 每個文件多個工具
您還可以從單個文件導出多個工具。每個導出都會成為**一個單獨的工具**,名稱為**`<filename>_<exportname>`**
```ts title=".opencode/tools/math.ts"
import { tool } from "@opencode-ai/plugin"
export const add = tool({
description: "Add two numbers",
args: {
a: tool.schema.number().describe("First number"),
b: tool.schema.number().describe("Second number"),
},
async execute(args) {
return args.a + args.b
},
})
export const multiply = tool({
description: "Multiply two numbers",
args: {
a: tool.schema.number().describe("First number"),
b: tool.schema.number().describe("Second number"),
},
async execute(args) {
return args.a * args.b
},
})
```
這將創建兩個工具:`math_add` 和`math_multiply`。
---
### 論據
您可以使用`tool.schema`(即[佐德](https://zod.dev))來定義參數類型。
```ts "tool.schema"
args: {
query: tool.schema.string().describe("SQL query to execute")
}
```
您還可以直接導入[佐德](https://zod.dev)並返回一個普通對象:
```ts {6}
import { z } from "zod"
export default {
description: "Tool description",
args: {
param: z.string().describe("Parameter description"),
},
async execute(args, context) {
// Tool implementation
return "result"
},
}
```
---
### 情境
工具接收有關當前會話的上下文:
```ts title=".opencode/tools/project.ts" {8}
import { tool } from "@opencode-ai/plugin"
export default tool({
description: "Get project information",
args: {},
async execute(args, context) {
// Access context information
const { agent, sessionID, messageID, directory, worktree } = context
return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}, Directory: ${directory}, Worktree: ${worktree}`
},
})
```
使用 `context.directory` 作為會話工作目錄。
使用 `context.worktree` 作為 git 工作樹根。
---
## 示例
### 用Python編寫一個工具
您可以用任何您想要的語言編寫工具。下面是一個使用 Python 將兩個數字相加的示例。
首先,將該工具創建為 Python 腳本:
```python title=".opencode/tools/add.py"
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
print(a + b)
```
然後創建調用它的工具定義:
```ts title=".opencode/tools/python-add.ts" {10}
import { tool } from "@opencode-ai/plugin"
import path from "path"
export default tool({
description: "Add two numbers using Python",
args: {
a: tool.schema.number().describe("First number"),
b: tool.schema.number().describe("Second number"),
},
async execute(args, context) {
const script = path.join(context.worktree, ".opencode/tools/add.py")
const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
return result.trim()
},
})
```
這裡我們使用 [`Bun.$`](https://bun.com/docs/runtime/shell) 實用程序來運行 Python 腳本。

View File

@@ -0,0 +1,76 @@
---
title: 生態系統
description: 使用 OpenCode 構建的項目和集成。
---
基於 OpenCode 的社區項目集合。
:::note
想要將您的 OpenCode 相關項目添加到此列表中嗎?提交 PR。
:::
您還可以查看[很棒的開放代碼](https://github.com/awesome-opencode/awesome-opencode)和[開放代碼咖啡館](https://opencode.cafe),這是一個聚合生態系統和社區的社區。
---
## 插件
|名稱 |描述 |
| --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| [Opencode-Daytona](https://github.com/jamesmurdza/daytona/blob/main/guides/typescript/opencode/README.md) |使用 git 同步和實時預覽在隔離的 Daytona 沙箱中自動運行 OpenCode 會話 |
| [opencode-helicone-會話](https://github.com/H2Shami/opencode-helicone-session) |自動注入 Helicone 會話標頭以進行請求分組 |
| [打開代碼類型注入](https://github.com/nick-vi/opencode-type-inject) |使用查找工具將 TypeScript/Svelte 類型自動注入到文件讀取中 |
| [opencode-openai-codex-auth](https://github.com/numman-ali/opencode-openai-codex-auth) |使用您的 ChatGPT Plus/Pro 訂閱而不是 API 積分 |
| [opencode-gemini-auth](https://github.com/jenslys/opencode-gemini-auth) |使用您現有的 Gemini 計劃而不是 API 計費 |
| [opencode-反重力-auth](https://github.com/NoeFabris/opencode-antigravity-auth) |使用 Antigravity 的免費模型代替 API 計費 |
| [opencode-devcontainers 開放代碼開發容器](https://github.com/athal7/opencode-devcontainers) |具有淺克隆和自動分配端口的多分支開發容器隔離 |
| [opencode-google-antigravity-auth](https://github.com/shekohex/opencode-google-antigravity-auth) | Google Antigravity OAuth 插件,支持 Google 搜索和更強大的 API 處理 |
| [opencode-動態上下文修剪](https://github.com/Tarquinen/opencode-dynamic-context-pruning) |通過修剪過時的工具輸出來優化代幣使用 |
| [opencode-websearch-引用](https://github.com/ghoulr/opencode-websearch-cited.git) |為具有 Google 接地風格的受支持提供商添加本機網絡搜索支持 |
| [opencode-pty](https://github.com/shekohex/opencode-pty.git) |使 AI 代理能夠在 PTY 中運行後台進程,並向其發送交互式輸入。 |
| [opencode-shell-策略](https://github.com/JRedeker/opencode-shell-strategy) |非交互式 shell 命令說明 - 防止依賴 TTY 的操作掛起
| [opencode-wakatime](https://github.com/angristan/opencode-wakatime) |使用 Wakatime 跟踪 OpenCode 使用情況 |
| [opencode-md-表格式化程序](https://github.com/franlol/opencode-md-table-formatter/tree/main) |清理法學碩士生成的降價表 |
| [開放代碼變形快速應用](https://github.com/JRedeker/opencode-morph-fast-apply) |使用 Morph Fast Apply API 和惰性編輯標記將代碼編輯速度提高 10 倍 |
| [哦我的開放代碼](https://github.com/code-yeongyu/oh-my-opencode) |後台代理、預構建的 LSP/AST/MCP 工具、精選代理、兼容 Claude 代碼 |
| [開放代碼通知器](https://github.com/panta82/opencode-notificator) | OpenCode 會話的桌面通知和聲音警報 |
| [開放代碼通知程序](https://github.com/mohak34/opencode-notifier) |針對權限、完成和錯誤事件的桌面通知和聲音警報 |
| [opencode-zellij-namer](https://github.com/24601/opencode-zellij-namer) |基於 OpenCode 上下文的 AI 支持的自動 Zellij 會話命名 |
| [開放代碼熟練](https://github.com/zenobi-us/opencode-skillful) |允許 OpenCode 代理通過技能發現和注入按需延遲加載提示 |
| [開放代碼超級內存](https://github.com/supermemoryai/opencode-supermemory) |使用超級內存跨會話持久內存 |
| [@Plannotator/OpenCode](https://github.com/backnotprop/plannotator/tree/main/apps/opencode-plugin) |具有視覺註釋和私人/離線共享的交互式計劃審查|
| [@openspoon/子任務2](https://github.com/spoons-and-mirrors/subtask2) |將開放代碼/命令擴展為具有精細流程控制的強大編排系統 |
| [開放代碼調度程序](https://github.com/different-ai/opencode-scheduler) |使用帶有 cron 語法的 launchd (Mac) 或 systemd (Linux) 安排重複作業 |
| [微碼](https://github.com/vtemian/micode) |結構化頭腦風暴 → 計劃 → 實施具有會議連續性的工作流程 |
| [奧克托](https://github.com/vtemian/octto) |用於通過多問題形式進行 AI 頭腦風暴的交互式瀏覽器 UI
| [opencode-後台代理](https://github.com/kdcokenny/opencode-background-agents) |具有異步委託和上下文持久性的 Claude Code 風格後台代理 |
| [打開代碼通知](https://github.com/kdcokenny/opencode-notify) | OpenCode 的本機操作系統通知 了解任務何時完成 |
| [開放代碼工作區](https://github.com/kdcokenny/opencode-workspace) |捆綁的多代理編排工具 16 個組件,一次安裝 |
| [開放代碼工作樹](https://github.com/kdcokenny/opencode-worktree) | OpenCode 的零摩擦 git 工作樹 |
---
## 專案
|名稱 |描述 |
| ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------- |
| [木牧](https://github.com/remorses/kimaki) |用於控制 OpenCode 會話的 Discord 機器人,基於 SDK 構建 |
| [開放代碼.nvim](https://github.com/NickvanDyke/opencode.nvim) | Neovim 插件,用於編輯器感知提示,基於 API 構建 |
| [門戶網站](https://github.com/hosenur/portal) |通過 Tailscale/VPN 實現 OpenCode 的移動優先 Web UI |
| [打開代碼插件模板](https://github.com/zenobi-us/opencode-plugin-template/) |用於構建 OpenCode 插件的模板 |
| [開放代碼.nvim](https://github.com/sudo-tee/opencode.nvim) | Neovim opencode 前端 - 基於終端的 AI 編碼代理 |
| [ai-sdk-provider-opencode-sdk](https://github.com/ben-vargas/ai-sdk-provider-opencode-sdk) | Vercel AI SDK 提供程序,用於通過 @opencode-ai/sdk 使用 OpenCode |
| [開放室](https://github.com/btriapitsyn/openchamber) | OpenCode 的 Web/桌面應用程序和 VS Code 擴展 |
| [OpenCode-Obsidian](https://github.com/mtymek/opencode-obsidian) |在 Obsidian 的 UI 中嵌入 OpenCode 的 Obsidian 插件 |
| [開放式工作](https://github.com/different-ai/openwork) | Claude Cowork 的開源替代方案,由 OpenCode 提供支持 |
| [奧克斯](https://github.com/kdcokenny/ocx) | OpenCode 擴展管理器具有可移植、隔離的配置文件。 |
| [代碼游牧者](https://github.com/NeuralNomadsAI/CodeNomad) | OpenCode 的桌面、Web、移動和遠程客戶端應用程序 |
---
## 代理商
|名稱 |描述 |
| ----------------------------------------------------------------- | ------------------------------------------------------------ |
| [代理](https://github.com/Cluster444/agentic) |用於結構化開發的模塊化人工智能代理和命令 |
| [開放代碼代理](https://github.com/darrenhinde/opencode-agents) |用於增強工作流程的配置、提示、代理和插件 |

View File

@@ -0,0 +1,170 @@
---
title: 企業
description: 在您的組織中安全地使用 OpenCode。
---
import config from "../../../../config.mjs"
export const email = `mailto:${config.email}`
OpenCode Enterprise 適用於希望確保其代碼和數據永遠不會離開其基礎設施的組織。它可以通過使用與 SSO 和內部 AI 網關集成的集中式配置來實現此目的。
:::note
OpenCode 不存儲您的任何代碼或上下文數據。
:::
要開始使用 OpenCode Enterprise
1. 與您的團隊進行內部試驗。
2. **<a href={email}>聯繫我們</a>**討論定價和實施選項。
---
## 審判
OpenCode 是開源的,不存儲您的任何代碼或上下文數據,因此您的開發人員只需 [開始吧](/docs/) 並進行試用。
---
### 數據處理
**OpenCode 不會存儲您的代碼或上下文數據。 ** 所有處理都在本地進行或通過直接 API 調用您的 AI 提供商。
這意味著只要您使用您信任的提供商或內部提供商
AI網關您可以安全使用OpenCode。
這裡唯一需要注意的是可選的 `/share` 功能。
---
#### 分享對話
如果用戶啟用 `/share` 功能,對話和與之關聯的數據將被發送到我們用於在 opencode.ai 上託管這些共享頁面的服務。
數據當前通過我們的 CDN 邊緣網絡提供服務,並緩存在用戶附近的邊緣。
我們建議您在試用時禁用此功能。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"share": "disabled"
}
```
[了解更多關於分享的信息](/docs/share)。
---
### 代碼所有權
**您擁有 OpenCode 生成的所有代碼。 ** 沒有許可限製或所有權聲明。
---
## 定價
我們對 OpenCode Enterprise 使用單機模型。如果您有自己的 LLM 網關,我們不會對使用的令牌收取費用。有關定價和實施選項的更多詳細信息,請**<a href={email}>聯繫我們</a>**。
---
## 部署
完成試用並準備好使用 OpenCode 後,請訪問:
您的組織,您可以**<a href={email}>聯繫我們</a>**進行討論
定價和實施選項。
---
### 中央配置
我們可以將 OpenCode 設置為為您的整個組織使用單一的中央配置。
這種集中式配置可以與您的 SSO 提供商集成,並確保所有用戶僅訪問您的內部 AI 網關。
---
### 單點登錄集成
通過中央配置OpenCode 可以與您組織的 SSO 提供商集成以進行身份​​驗證。
這使得 OpenCode 能夠通過現有的身份管理系統獲取內部 AI 網關的憑據。
---
### 內部AI網關
通過中央配置OpenCode 還可以配置為僅使用您的內部 AI 網關。
您還可以禁用所有其他 AI 提供商,確保所有請求都通過組織批准的基礎設施。
---
### 自託管
雖然我們建議禁用共享頁面以確保您的數據永遠不會離開
您的組織,我們還可以幫助您在您的基礎設施上自行託管它們。
目前這已在我們的路線圖上。如果您有興趣,**<a href={email}>讓我們知道</a>**。
---
## 常問問題
<details>
<summary>什麼是 OpenCode Enterprise </summary>
OpenCode Enterprise 適用於希望確保其代碼和數據永遠不會離開其基礎設施的組織。它可以通過使用與 SSO 和內部 AI 網關集成的集中式配置來實現此目的。
</details>
<details>
<summary>如何開始使用 OpenCode Enterprise </summary>
只需與您的團隊進行內部試用即可。 OpenCode 默認情況下不存儲您的代碼或上下文數據,因此可以輕鬆上手。
然後**<a href={email}>聯繫我們</a>**討論定價和實施選項。
</details>
<details>
<summary>企業定價如何運作? </summary>
我們提供按席位企業定價。如果您有自己的 LLM 網關,我們不會對使用的令牌收取費用。如需了解更多詳情,請**<a href={email}>聯繫我們</a>**,獲取根據您組織的需求定制的報價。
</details>
<details>
<summary>OpenCode Enterprise 保證我的數據安全嗎? </summary>
是的。 OpenCode 不存儲您的代碼或上下文數據。所有處理都在本地進行或通過直接 API 調用您的 AI 提供商。通過中央配置和 SSO 集成,您的數據在組織的基礎架構中保持安全。
</details>
<details>
<summary>我們可以使用自己的私有 NPM 註冊表嗎? </summary>
OpenCode 通過 Bun 的本機 `.npmrc` 文件支持來支持私有 npm 註冊表。如果您的組織使用私有註冊表,例如 JFrog Artifactory、Nexus 或類似的註冊表,請確保開發人員在運行 OpenCode 之前經過身份驗證。
要使用您的私有註冊表設置身份驗證:
```bash
npm login --registry=https://your-company.jfrog.io/api/npm/npm-virtual/
```
這將創建帶有身份驗證詳細信息的`~/.npmrc`。 OpenCode 會自動
拿起這個。
:::caution
在運行 OpenCode 之前,您必須登錄私有註冊表。
:::
或者,您可以手動配置 `.npmrc` 文件:
```bash title="~/.npmrc"
registry=https://your-company.jfrog.io/api/npm/npm-virtual/
//your-company.jfrog.io/api/npm/npm-virtual/:_authToken=${NPM_AUTH_TOKEN}
```
開發人員必須在運行 OpenCode 之前登錄私有註冊表,以確保可以從企業註冊表安裝軟件包。
</details>

View File

@@ -0,0 +1,130 @@
---
title: 格式化程序
description: OpenCode 使用特定於語言的格式化程序。
---
使用特定於語言的格式化程序編寫或編輯文件後OpenCode 會自動格式化文件。這可確保生成的代碼遵循項目的代碼風格。
---
## 內建
OpenCode 附帶了多個適用於流行語言和框架的內置格式化程序。下面是格式化程序、支持的文件擴展名以及所需的命令或配置選項的列表。
|格式化程序|擴展 |要求|
| -------------------- | -------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
|政府| .go | `gofmt` 命令可用 |
|混合| .ex、.exs、.eex、.heex、.leex、.neex、.sface | `mix` 命令可用 |
|更漂亮 | .js、.jsx、.ts、.tsx、.html、.css、.md、.json、.yaml 和 [更多的](https://prettier.io/docs/en/index.html) | `package.json` | `prettier` 依賴關係
|生物群系 | .js、.jsx、.ts、.tsx、.html、.css、.md、.json、.yaml 和 [更多的](https://biomejs.dev/) | `biome.json(c)` 配置文件 |
|之字形 | .zig、.zon | `zig` 命令可用 |
| clang 格式 | .c、.cpp、.h、.hpp、.ino 和 [更多的](https://clang.llvm.org/docs/ClangFormat.html) | `.clang-format` 配置文件 |
|克特林特 | .kt、.kts | `ktlint` 命令可用 |
|領子 | .py, .pyi | `ruff` 命令可通過配置 |
|生鏽 | .rs | `rustfmt` 命令可用 |
|貨物運輸 | .rs | `cargo fmt` 命令可用 |
|紫外線| .py, .pyi | `uv` 命令可用 |
|魯博科普 | .rb、.rake、.gemspec、.ru | `rubocop` 命令可用 |
|標準rb | .rb、.rake、.gemspec、.ru | `standardrb` 命令可用 |
| html美化器 | .erb、.html.erb | `htmlbeautifier` 命令可用 |
|空氣| .R | `air` 命令可用 |
|飛鏢 | .dart | `dart` 命令可用 |
| ocaml 格式 | .ml、.mli | `ocamlformat` 可用命令和 `.ocamlformat` 配置文件 |
|地形 | .tf、.tfvars | `terraform` 命令可用 |
|微光| .gleam | `gleam` 命令可用 |
|尼克斯夫MTT | .nix | `nixfmt` 命令可用 |
| sfmt| .sh、.bash | `shfmt` 命令可用 |
|品脫 | .php | `composer.json` | `laravel/pint` 依賴關係
| oxfmt實驗| .js、.jsx、.ts、.tsx | `package.json` 和[實驗環境變量標誌](/docs/cli/#experimental) | `oxfmt` 依賴關係
|奧爾莫魯 | .hs | `ormolu` 命令可用 |
因此,如果您的項目的`package.json`中有`prettier`OpenCode將自動使用它。
---
## 它是如何運作的
當 OpenCode 寫入或編輯文件時,它:
1. 根據所有啟用的格式化程序檢查文件擴展名。
2. 對文件運行適當的格式化程序命令。
3. 自動應用格式更改。
此過程在後台進行,確保無需任何手動步驟即可維護您的代碼樣式。
---
## 配置
您可以通過 OpenCode 配置中的 `formatter` 部分自定義格式化程序。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"formatter": {}
}
```
每個格式化程序配置支持以下內容:
|物業 |類型 |描述 |
| ------------- | -------- | ------------------------------------------------------- |
| `disabled` |布爾 |將其設置為 `true` 以禁用格式化程序 |
| `command` |字符串[] |格式化運行的命令 |
| `environment` |對象|運行格式化程序時要設置的環境變量 |
| `extensions` |字符串[] |此格式化程序應處理的文件擴展名 |
讓我們看一些例子。
---
### 禁用格式化程序
要全局禁用**所有**格式化程序,請將`formatter`設置為`false`
```json title="opencode.json" {3}
{
"$schema": "https://opencode.ai/config.json",
"formatter": false
}
```
要禁用**特定**格式化程序,請將`disabled`設置為`true`
```json title="opencode.json" {5}
{
"$schema": "https://opencode.ai/config.json",
"formatter": {
"prettier": {
"disabled": true
}
}
}
```
---
### 自定義格式化程序
您可以覆蓋內置格式化程序或通過指定命令、環境變量和文件擴展名添加新格式化程序:
```json title="opencode.json" {4-14}
{
"$schema": "https://opencode.ai/config.json",
"formatter": {
"prettier": {
"command": ["npx", "prettier", "--write", "$FILE"],
"environment": {
"NODE_ENV": "development"
},
"extensions": [".js", ".ts", ".jsx", ".tsx"]
},
"custom-markdown-formatter": {
"command": ["deno", "fmt", "$FILE"],
"extensions": [".md"]
}
}
}
```
命令中的 **`$FILE` 佔位符** 將替換為正在格式化的文件的路徑。

View File

@@ -0,0 +1,321 @@
---
title: GitHub
description: 在 GitHub 問題和拉取請求中使用 OpenCode。
---
OpenCode 與您的 GitHub 工作流程集成。在評論中提及`/opencode` 或`/oc`OpenCode 將在您的 GitHub Actions 運行器中執行任務。
---
## 特徵
- **分類問題**:要求 OpenCode 調查問題並向您解釋。
- **修復和實施**:要求 OpenCode 修復問題或實施功能。它將在一個新分支中工作並提交包含所有更改的 PR。
- **安全**OpenCode 在 GitHub 的運行器中運行。
---
## 安裝
在 GitHub 存儲庫中的項目中運行以下命令:
```bash
opencode github install
```
這將引導您完成安裝 GitHub 應用程序、創建工作流程和設置機密。
---
### 手動設置
或者您可以手動設置。
1. Error 500 (Server Error)!!1500.Thats an error.There was an error. Please try again later.Thats all we know.
前往[**github.com/apps/opencode-agent**](https://github.com/apps/opencode-agent)。確保它已安裝在目標存儲庫上。
2. **添加工作流程**
將以下工作流程文件添加到存儲庫中的 `.github/workflows/opencode.yml` 中。確保在`env`中設置適當的`model`和所需的API密鑰。
```yml title=".github/workflows/opencode.yml" {24,26}
name: opencode
on:
issue_comment:
types: [created]
pull_request_review_comment:
types: [created]
jobs:
opencode:
if: |
contains(github.event.comment.body, '/oc') ||
contains(github.event.comment.body, '/opencode')
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 1
persist-credentials: false
- name: Run OpenCode
uses: anomalyco/opencode/github@latest
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
with:
model: anthropic/claude-sonnet-4-20250514
# share: true
# github_token: xxxx
```
3. Error 500 (Server Error)!!1500.Thats an error.There was an error. Please try again later.Thats all we know.
在您的組織或項目的**設置**中,展開左側的**秘密和變量**,然後選擇**操作**。並添加所需的 API 密鑰。
---
## 配置
- `model`:與 OpenCode 一起使用的模型。採用`provider/model` 格式。這是**必需的**。
- `agent`: The agent to use.必須是一級代理。如果未找到,則從配置回退到 `default_agent` 或 `"build"`。
- `share`是否共享OpenCode會話。對於公共存儲庫默認為 **true**。
- `prompt`:可選的自定義提示以覆蓋默認行為。使用它來自定義 OpenCode 處理請求的方式。
- `token`:可選的 GitHub 訪問令牌用於執行創建評論、提交更改和打開拉取請求等操作。默認情況下OpenCode 使用來自 OpenCode GitHub 應用程序的安裝訪問令牌,因此提交、評論和拉取請求顯示為來自應用程序。
或者,您可以使用 GitHub Action 運行程序的[內置`GITHUB_TOKEN`](https://docs.github.com/en/actions/tutorials/authenticate-with-github_token),而無需安裝 OpenCode GitHub 應用程序。只需確保在您的工作流程中授予所需的權限:
```yaml
permissions:
id-token: write
contents: write
pull-requests: write
issues: write
```
Error 500 (Server Error)!!1500.Thats an error.There was an error. Please try again later.Thats all we know.
---
## 支持的活動
OpenCode 可以由以下 GitHub 事件觸發:
|事件類型 |觸發者 |詳情 |
| ----------------------------- | -------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `issue_comment` |對問題或 PR 發表評論 | Mention `/opencode` or `/oc` in your comment. OpenCode 讀取上下文並可以創建分支、打開 PR 或回复。 |
| `pull_request_review_comment` |對 PR 中的特定代碼行進行評論 |在檢查代碼時提及`/opencode` 或`/oc`。 OpenCode receives file path, line numbers, and diff context. |
| `issues` |問題已打開或已編輯 |創建或修改問題時自動觸發 OpenCode。需要 `prompt` 輸入。 |
| `pull_request` | PR 已開啟或已更新 | Automatically trigger OpenCode when PRs are opened, synchronized, or reopened.對於自動評論很有用。 |
| `schedule` |基於 Cron 的計劃 |按計劃運行 OpenCode。需要 `prompt` 輸入。輸出進入日誌和 PR沒有可評論的問題。 |
| `workflow_dispatch` |從 GitHub UI 手動觸發 |通過“操作”選項卡按需觸發 OpenCode。需要 `prompt` 輸入。輸出進入日誌和 PR。 |
### 時間表示例
按計劃運行 OpenCode 以執行自動化任務:
```yaml title=".github/workflows/opencode-scheduled.yml"
name: Scheduled OpenCode Task
on:
schedule:
- cron: "0 9 * * 1" # Every Monday at 9am UTC
jobs:
opencode:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
pull-requests: write
issues: write
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
persist-credentials: false
- name: Run OpenCode
uses: anomalyco/opencode/github@latest
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
with:
model: anthropic/claude-sonnet-4-20250514
prompt: |
Review the codebase for any TODO comments and create a summary.
If you find issues worth addressing, open an issue to track them.
```
For scheduled events, the `prompt` input is **required** since there's no comment to extract instructions from.計劃工作流在沒有用戶上下文的情況下運行以進行權限檢查,因此如果您希望 OpenCode 創建分支或 PR工作流必須授予 `contents: write` 和 `pull-requests: write`。
---
### 拉取請求示例
打開或更新 PR 時自動審核:
```yaml title=".github/workflows/opencode-review.yml"
name: opencode-review
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
jobs:
review:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
pull-requests: read
issues: read
steps:
- uses: actions/checkout@v6
with:
persist-credentials: false
- uses: anomalyco/opencode/github@latest
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
model: anthropic/claude-sonnet-4-20250514
use_github_token: true
prompt: |
Review this pull request:
- Check for code quality issues
- Look for potential bugs
- Suggest improvements
```
對於 `pull_request` 事件,如果未提供 `prompt`OpenCode 將默認審核拉取請求。
---
### 問題分類示例
自動分類新問題。此示例過濾超過 30 天的帳戶以減少垃圾郵件:
```yaml title=".github/workflows/opencode-triage.yml"
name: Issue Triage
on:
issues:
types: [opened]
jobs:
triage:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
pull-requests: write
issues: write
steps:
- name: Check account age
id: check
uses: actions/github-script@v7
with:
script: |
const user = await github.rest.users.getByUsername({
username: context.payload.issue.user.login
});
const created = new Date(user.data.created_at);
const days = (Date.now() - created) / (1000 * 60 * 60 * 24);
return days >= 30;
result-encoding: string
- uses: actions/checkout@v6
if: steps.check.outputs.result == 'true'
with:
persist-credentials: false
- uses: anomalyco/opencode/github@latest
if: steps.check.outputs.result == 'true'
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
with:
model: anthropic/claude-sonnet-4-20250514
prompt: |
Review this issue. If there's a clear fix or relevant docs:
- Provide documentation links
- Add error handling guidance for code examples
Otherwise, do not comment.
```
對於 `issues` 事件,`prompt` 輸入是**必需的**,因為沒有註釋可從中提取指令。
---
## 自定義提示
覆蓋默認提示,為您的工作流程自定義 OpenCode 的行為。
```yaml title=".github/workflows/opencode.yml"
- uses: anomalyco/opencode/github@latest
with:
model: anthropic/claude-sonnet-4-5
prompt: |
Review this pull request:
- Check for code quality issues
- Look for potential bugs
- Suggest improvements
```
這對於執行與您的項目相關的特定審查標準、編碼標准或重點領域非常有用。
---
## 示例
以下是如何在 GitHub 中使用 OpenCode 的一些示例。
- **解釋一個問題**
在 GitHub 問題中添加此評論。
```
/opencode explain this issue
```
OpenCode 將閱讀整個線程,包括所有評論,並回复並提供清晰的解釋。
- **解決問題**
在 GitHub 問題中,說:
```
/opencode fix this
```
OpenCode 將創建一個新分支,實施更改,並使用更改打開 PR。
- **審查 PR 並進行更改**
在 GitHub PR 上留下以下評論。
```
Delete the attachment from S3 when the note is removed /oc
```
Error 500 (Server Error)!!1500.Thats an error.There was an error. Please try again later.Thats all we know.
- **查看特定代碼行**
直接在 PR 的“文件”選項卡中的代碼行上留下評論。 OpenCode 自動檢測文件、行號和差異上下文以提供精確的響應。
```
[Comment on specific lines in Files tab]
/oc add error handling here
```
Error 500 (Server Error)!!1500.Thats an error.There was an error. Please try again later.Thats all we know.
- 正在審查的確切文件
- 具體代碼行
- 周圍的 diff 上下文
- 行號信息
這允許更有針對性的請求,而無需手動指定文件路徑或行號。

View File

@@ -0,0 +1,195 @@
---
title: GitLab
description: 在 GitLab 問題和合併請求中使用 OpenCode。
---
OpenCode 通過 GitLab CI/CD 管道或與 GitLab Duo 與您的 GitLab 工作流程集成。
在這兩種情況下OpenCode 都將在您的 GitLab 運行器上運行。
---
## GitLab。
OpenCode 在常規 GitLab 管道中工作。您可以將其構建為管道作為 [CI組件](https://docs.gitlab.com/ee/ci/components/)
在這裡,我們使用社區創建的 OpenCode CI/CD 組件 — [nagyv/gitlab-opencode](https://gitlab.com/nagyv/gitlab-opencode)。
---
### 特徵
- **每個作業使用自定義配置**:使用自定義配置目錄配置 OpenCode例如 `./config/#custom-directory` 以啟用或禁用每個 OpenCode 調用的功能。
- Error 500 (Server Error)!!1500.Thats an error.There was an error. Please try again later.Thats all we know.
- **靈活**CI 組件支持多種輸入來自定義其行為
---
### 設定
1. 將 OpenCode 身份驗證 JSON 作為文件類型 CI 環境變量存儲在 **設置** > **CI/CD** > **變量** 下。確保將它們標記為“屏蔽和隱藏”。
2. 將以下內容添加到您的 `.gitlab-ci.yml` 文件中。
```yaml title=".gitlab-ci.yml"
include:
- component: $CI_SERVER_FQDN/nagyv/gitlab-opencode/opencode@2
inputs:
config_dir: ${CI_PROJECT_DIR}/opencode-config
auth_json: $OPENCODE_AUTH_JSON # The variable name for your OpenCode authentication JSON
command: optional-custom-command
message: "Your prompt here"
```
有關此組件的更多輸入和用例[查看文檔](https://gitlab.com/explore/catalog/nagyv/gitlab-opencode)。
---
## 亞搏體育app二人組
OpenCode 與您的 GitLab 工作流程集成。
在評論中提及`@opencode`OpenCode 將在您的 GitLab CI 管道中執行任務。
---
### 特徵
- **分類問題**:要求 OpenCode 調查問題並向您解釋。
- **修復和實施**:要求 OpenCode 修復問題或實施功能。
它將創建一個新分支並提出包含更改的合併請求。
- **安全**OpenCode 在您的 GitLab 運行器上運行。
---
### 設定
OpenCode 在您的 GitLab CI/CD 管道中運行,您需要進行以下設置:
:::tip
查看[**GitLab 文檔**](https://docs.gitlab.com/user/duo_agent_platform/agent_assistant/) 以獲取最新說明。
:::
1. 配置您的 GitLab 環境
2. 設置 CI/CD
3. 獲取 AI 模型提供商 API 密鑰
4. 創建服務帳戶
5. 配置 CI/CD 變量
6. 創建一個流配置文件,這是一個示例:
<details>
<summary>Flow configuration</summary>
```yaml
image: node:22-slim
commands:
- echo "Installing opencode"
- npm install --global opencode-ai
- echo "Installing glab"
- export GITLAB_TOKEN=$GITLAB_TOKEN_OPENCODE
- apt-get update --quiet && apt-get install --yes curl wget gpg git && rm --recursive --force /var/lib/apt/lists/*
- curl --silent --show-error --location "https://raw.githubusercontent.com/upciti/wakemeops/main/assets/install_repository" | bash
- apt-get install --yes glab
- echo "Configuring glab"
- echo $GITLAB_HOST
- echo "Creating OpenCode auth configuration"
- mkdir --parents ~/.local/share/opencode
- |
cat > ~/.local/share/opencode/auth.json << EOF
{
"anthropic": {
"type": "api",
"key": "$ANTHROPIC_API_KEY"
}
}
EOF
- echo "Configuring git"
- git config --global user.email "opencode@gitlab.com"
- git config --global user.name "OpenCode"
- echo "Testing glab"
- glab issue list
- echo "Running OpenCode"
- |
opencode run "
You are an AI assistant helping with GitLab operations.
Context: $AI_FLOW_CONTEXT
Task: $AI_FLOW_INPUT
Event: $AI_FLOW_EVENT
Please execute the requested task using the available GitLab tools.
Be thorough in your analysis and provide clear explanations.
<important>
Please use the glab CLI to access data from GitLab. The glab CLI has already been authenticated. You can run the corresponding commands.
If you are asked to summarize an MR or issue or asked to provide more information then please post back a note to the MR/Issue so that the user can see it.
You don't need to commit or push up changes, those will be done automatically based on the file changes you make.
</important>
"
- git checkout --branch $CI_WORKLOAD_REF origin/$CI_WORKLOAD_REF
- echo "Checking for git changes and pushing if any exist"
- |
if ! git diff --quiet || ! git diff --cached --quiet || [ --not --zero "$(git ls-files --others --exclude-standard)" ]; then
echo "Git changes detected, adding and pushing..."
git add .
if git diff --cached --quiet; then
echo "No staged changes to commit"
else
echo "Committing changes to branch: $CI_WORKLOAD_REF"
git commit --message "Codex changes"
echo "Pushing changes up to $CI_WORKLOAD_REF"
git push https://gitlab-ci-token:$GITLAB_TOKEN@$GITLAB_HOST/gl-demo-ultimate-dev-ai-epic-17570/test-java-project.git $CI_WORKLOAD_REF
echo "Changes successfully pushed"
fi
else
echo "No git changes detected, skipping push"
fi
variables:
- ANTHROPIC_API_KEY
- GITLAB_TOKEN_OPENCODE
- GITLAB_HOST
```
</details>
詳細說明可以參考[GitLab CLI 代理文檔](https://docs.gitlab.com/user/duo_agent_platform/agent_assistant/)。
---
### 示例
以下是如何在 GitLab 中使用 OpenCode 的一些示例。
:::tip
您可以配置使用與 `@opencode` 不同的觸發短語。
:::
- **解釋一個問題**
在 GitLab 問題中添加此評論。
```
@opencode explain this issue
```
OpenCode 將閱讀該問題並回复並提供清晰的解釋。
- **解決問題**
在 GitLab 問題中,說:
```
@opencode fix this
```
OpenCode 將創建一個新分支,實施更改,並打開包含更改的合併請求。
- **審查合併請求**
對 GitLab 合併請求留下以下評論。
```
@opencode review this merge request
```
OpenCode 將審核合併請求並提供反饋。

View File

@@ -0,0 +1,48 @@
---
title: 集成開發環境
description: VS Code、Cursor 和其他 IDE 的 OpenCode 擴展
---
OpenCode 與 VS Code、Cursor 或任何支持終端的 IDE 集成。只需在終端中運行 `opencode` 即可開始。
---
## 用法
- **快速啟動**:使用 `Cmd+Esc` (Mac) 或 `Ctrl+Esc` (Windows/Linux) 在拆分終端視圖中打開 OpenCode或者聚焦現有終端會話如果已有終端會話正在運行
- **新會話**:使用 `Cmd+Shift+Esc` (Mac) 或 `Ctrl+Shift+Esc` (Windows/Linux) 啟動新的 OpenCode 終端會話,即使該會話已打開。您還可以單擊 UI 中的 OpenCode 按鈕。
- **上下文感知**:自動與 OpenCode 共享您當前的選擇或選項卡。
- **文件引用快捷方式**:使用`Cmd+Option+K` (Mac) 或`Alt+Ctrl+K` (Linux/Windows) 插入文件引用。例如,`@File#L37-42`。
---
## 安裝
要在 VS Code 和 Cursor、Windsurf、VSCodium 等流行分支上安裝 OpenCode
1. 打開 VS 代碼
2. 打開集成終端
3. 運行 `opencode` - 擴展會自動安裝
另一方面,如果您想在從 TUI 運行 `/editor` 或 `/export` 時使用自己的 IDE則需要設置 `export EDITOR="code --wait"`。 [了解更多](/docs/tui/#editor-setup)。
---
### 手動安裝
在擴展市場中搜索 **OpenCode**,然後單擊 **安裝**。
---
### 故障排除
如果擴展無法自動安裝:
- 確保您在集成終端中運行`opencode`。
- 確認您的 IDE 的 CLI 已安裝:
- 對於 VS 代碼:`code` 命令
- 對於光標:`cursor` 命令
- 對於風帆衝浪:`windsurf` 命令
- 對於 VSCodium`codium` 命令
- 如果沒有,請運行 `Cmd+Shift+P` (Mac) 或 `Ctrl+Shift+P` (Windows/Linux) 並蒐索“Shell Command: Install 'code' command in PATH”或適用於您的 IDE 的等效命令)
- 確保 VS Code 有權安裝擴展

View File

@@ -0,0 +1,359 @@
---
title: 簡介
description: 開始使用 OpenCode。
---
import { Tabs, TabItem } from "@astrojs/starlight/components"
import config from "../../../../config.mjs"
export const console = config.console
[**開放代碼**](/) 是一個開源人工智能編碼代理。它可用作基於終端的界面、桌面應用程序或 IDE 擴展。
![具有 opencode 主題的 OpenCode TUI](../../../assets/lander/screenshot.png)
讓我們開始吧。
---
#### 先決條件
要在終端中使用 OpenCode您需要
1. 現代終端模擬器,例如:
- [韋茲術語](https://wezterm.org),跨平台
- [阿拉克里蒂](https://alacritty.org),跨平台
- [幽靈般的](https://ghostty.org)Linux 和 macOS
- [貓咪](https://sw.kovidgoyal.net/kitty/)Linux 和 macOS
2. 您想要使用的 LLM 提供商的 API 密鑰。
---
## 安裝
安裝 OpenCode 最簡單的方法是通過安裝腳本。
```bash
curl -fsSL https://opencode.ai/install | bash
```
您還可以使用以下命令安裝它:
- **使用 Node.js**
<Tabs>
<TabItem label="npm">
```bash
npm install -g opencode-ai
```
</TabItem>
<TabItem label="Bun">
```bash
bun install -g opencode-ai
```
</TabItem>
<TabItem label="pnpm">
```bash
pnpm install -g opencode-ai
```
</TabItem>
<TabItem label="Yarn">
```bash
yarn global add opencode-ai
```
</TabItem>
</Tabs>
- **在 macOS 和 Linux 上使用 Homebrew**
```bash
brew install anomalyco/tap/opencode
```
> 我們建議使用 OpenCode Tap 來獲取最新版本。官方 `brew install opencode` 公式由 Homebrew 團隊維護,更新頻率較低。
- **在 Arch Linux 上使用 Paru**
```bash
paru -S opencode-bin
```
#### 視窗
:::tip[Recommended: Use WSL]
為了在 Windows 上獲得最佳體驗,我們建議使用[適用於 Linux 的 Windows 子系統 (WSL)](/docs/windows-wsl)。它提供了更好的性能並與 OpenCode 的功能完全兼容。
:::
- **使用巧克力**
```bash
choco install opencode
```
- **使用勺子**
```bash
scoop install opencode
```
- **使用 NPM**
```bash
npm install -g opencode-ai
```
- **使用米塞**
```bash
mise use -g github:anomalyco/opencode
```
- **使用 Docker**
```bash
docker run -it --rm ghcr.io/anomalyco/opencode
```
目前正在支持使用 Bun 在 Windows 上安裝 OpenCode。
您還可以從[發布](https://github.com/anomalyco/opencode/releases) 獲取二進製文件。
---
## 配置
借助 OpenCode您可以通過配置 API 密鑰來使用任何 LLM 提供商。
如果您不熟悉使用 LLM 提供商,我們建議使用[開放代碼禪](/docs/zen)。
這是經過 OpenCode 測試和驗證的精選模型列表
團隊。
1. 在 TUI 中運行 `/connect` 命令,選擇 opencode然後前往 [opencode.ai/auth](https://opencode.ai/auth)。
```txt
/connect
```
2. 登錄,添加您的賬單詳細信息,然後復制您的 API 密鑰。
3. 粘貼您的 API 密鑰。
```txt
┌ API key
└ enter
```
或者,您可以選擇其他提供商之一。 [了解更多](/docs/providers#directory)。
---
## 初始化
現在您已經配置了提供程序,您可以導航到一個項目
你想繼續工作。
```bash
cd /path/to/project
```
並運行 OpenCode。
```bash
opencode
```
接下來,通過運行以下命令來初始化項目的 OpenCode。
```bash frame="none"
/init
```
這將使 OpenCode 分析您的項目並在以下位置創建 `AGENTS.md` 文件
項目根。
:::tip
您應該將項目的 `AGENTS.md` 文件提交到 Git。
:::
這有助於 OpenCode 理解項目結構和編碼模式
用過的。
---
## 用法
您現在已準備好使用 OpenCode 來處理您的項目。請隨意詢問
任何事物!
如果您不熟悉使用 AI 編碼代理,以下是一些可能會有所幫助的示例
幫助。
---
### 提出問題
您可以要求 OpenCode 向您解釋代碼庫。
:::tip
使用`@`鍵模糊搜索工程中的文件。
:::
```txt frame="none" "@packages/functions/src/api/index.ts"
How is authentication handled in @packages/functions/src/api/index.ts
```
如果您沒有處理代碼庫的一部分,這會很有幫助。
---
### 添加功能
您可以要求 OpenCode 向您的項目添加新功能。不過我們首先建議要求它制定一個計劃。
1. **制定計劃**
OpenCode 有一個*計劃模式*,該模式禁用其進行更改和
相反,建議*如何*實現該功能。
使用 **Tab** 鍵切換到它。您會在右下角看到一個指示符。
```bash frame="none" title="Switch to Plan mode"
<TAB>
```
現在讓我們描述一下我們想要它做什麼。
```txt frame="none"
When a user deletes a note, we'd like to flag it as deleted in the database.
Then create a screen that shows all the recently deleted notes.
From this screen, the user can undelete a note or permanently delete it.
```
您需要為 OpenCode 提供足夠的詳細信息以了解您想要的內容。它有幫助
就像與團隊中的初級開發人員交談一樣與它交談。
:::tip
為 OpenCode 提供大量上下文和示例,以幫助其理解您的內容
想。
:::
2. **迭代計劃**
一旦它為您提供了計劃,您就可以提供反饋或添加更多詳細信息。
```txt frame="none"
We'd like to design this new screen using a design I've used before.
[Image #1] Take a look at this image and use it as a reference.
```
:::tip
將圖像拖放到終端中以將其添加到提示中。
:::
OpenCode 可以掃描您提供的任何圖像並將其添加到提示中。你可以
通過將圖像拖放到終端中來完成此操作。
3. **構建功能**
一旦您對計劃感到滿意,請切換回*構建模式*
再次按 **Tab** 鍵。
```bash frame="none"
<TAB>
```
並要求它做出改變。
```bash frame="none"
Sounds good! Go ahead and make the changes.
```
---
### 做出改變
對於更直接的更改,您可以要求 OpenCode 直接構建它
無需先審查計劃。
```txt frame="none" "@packages/functions/src/settings.ts" "@packages/functions/src/notes.ts"
We need to add authentication to the /settings route. Take a look at how this is
handled in the /notes route in @packages/functions/src/notes.ts and implement
the same logic in @packages/functions/src/settings.ts
```
您需要確保提供大量詳細信息,以便 OpenCode 做出正確的決定
變化。
---
### 撤消更改
假設您要求 OpenCode 進行一些更改。
```txt frame="none" "@packages/functions/src/api/index.ts"
Can you refactor the function in @packages/functions/src/api/index.ts?
```
但你意識到這不是你想要的。您**可以撤消**更改
使用 `/undo` 命令。
```bash frame="none"
/undo
```
OpenCode 現在將恢復您所做的更改並顯示您的原始消息
再次。
```txt frame="none" "@packages/functions/src/api/index.ts"
Can you refactor the function in @packages/functions/src/api/index.ts?
```
從這裡您可以調整提示並要求 OpenCode 重試。
:::tip
您可以多次運行 `/undo` 以撤消多項更改。
:::
或者您**可以使用 `/redo` 命令重做**更改。
```bash frame="none"
/redo
```
---
## 分享
您與 OpenCode 的對話可以[與您的
團隊](/docs/分享)。
```bash frame="none"
/share
```
這將創建當前對話的鏈接並將其複製到剪貼板。
:::note
默認情況下不共享對話。
:::
這是帶有 OpenCode 的[示例對話](https://opencode.ai/s/4XP1fce5)。
---
## 定制
就是這樣!您現在已經是使用 OpenCode 的專家了。
要使其成為您自己的,我們建議 [選擇一個主題](/docs/themes)、[自定義按鍵綁定](/docs/keybinds)、[配置代碼格式化程序](/docs/formatters)、[創建自定義命令](/docs/commands) 或使用 [開放代碼配置](/docs/config)。

View File

@@ -0,0 +1,192 @@
---
title: 按鍵綁定
description: 自定義您的按鍵綁定。
---
OpenCode 有一個按鍵綁定列表,您可以通過 OpenCode 配置進行自定義。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"keybinds": {
"leader": "ctrl+x",
"app_exit": "ctrl+c,ctrl+d,<leader>q",
"editor_open": "<leader>e",
"theme_list": "<leader>t",
"sidebar_toggle": "<leader>b",
"scrollbar_toggle": "none",
"username_toggle": "none",
"status_view": "<leader>s",
"tool_details": "none",
"session_export": "<leader>x",
"session_new": "<leader>n",
"session_list": "<leader>l",
"session_timeline": "<leader>g",
"session_fork": "none",
"session_rename": "none",
"session_share": "none",
"session_unshare": "none",
"session_interrupt": "escape",
"session_compact": "<leader>c",
"session_child_cycle": "<leader>right",
"session_child_cycle_reverse": "<leader>left",
"session_parent": "<leader>up",
"messages_page_up": "pageup,ctrl+alt+b",
"messages_page_down": "pagedown,ctrl+alt+f",
"messages_line_up": "ctrl+alt+y",
"messages_line_down": "ctrl+alt+e",
"messages_half_page_up": "ctrl+alt+u",
"messages_half_page_down": "ctrl+alt+d",
"messages_first": "ctrl+g,home",
"messages_last": "ctrl+alt+g,end",
"messages_next": "none",
"messages_previous": "none",
"messages_copy": "<leader>y",
"messages_undo": "<leader>u",
"messages_redo": "<leader>r",
"messages_last_user": "none",
"messages_toggle_conceal": "<leader>h",
"model_list": "<leader>m",
"model_cycle_recent": "f2",
"model_cycle_recent_reverse": "shift+f2",
"model_cycle_favorite": "none",
"model_cycle_favorite_reverse": "none",
"variant_cycle": "ctrl+t",
"command_list": "ctrl+p",
"agent_list": "<leader>a",
"agent_cycle": "tab",
"agent_cycle_reverse": "shift+tab",
"input_clear": "ctrl+c",
"input_paste": "ctrl+v",
"input_submit": "return",
"input_newline": "shift+return,ctrl+return,alt+return,ctrl+j",
"input_move_left": "left,ctrl+b",
"input_move_right": "right,ctrl+f",
"input_move_up": "up",
"input_move_down": "down",
"input_select_left": "shift+left",
"input_select_right": "shift+right",
"input_select_up": "shift+up",
"input_select_down": "shift+down",
"input_line_home": "ctrl+a",
"input_line_end": "ctrl+e",
"input_select_line_home": "ctrl+shift+a",
"input_select_line_end": "ctrl+shift+e",
"input_visual_line_home": "alt+a",
"input_visual_line_end": "alt+e",
"input_select_visual_line_home": "alt+shift+a",
"input_select_visual_line_end": "alt+shift+e",
"input_buffer_home": "home",
"input_buffer_end": "end",
"input_select_buffer_home": "shift+home",
"input_select_buffer_end": "shift+end",
"input_delete_line": "ctrl+shift+d",
"input_delete_to_line_end": "ctrl+k",
"input_delete_to_line_start": "ctrl+u",
"input_backspace": "backspace,shift+backspace",
"input_delete": "ctrl+d,delete,shift+delete",
"input_undo": "ctrl+-,super+z",
"input_redo": "ctrl+.,super+shift+z",
"input_word_forward": "alt+f,alt+right,ctrl+right",
"input_word_backward": "alt+b,alt+left,ctrl+left",
"input_select_word_forward": "alt+shift+f,alt+shift+right",
"input_select_word_backward": "alt+shift+b,alt+shift+left",
"input_delete_word_forward": "alt+d,alt+delete,ctrl+delete",
"input_delete_word_backward": "ctrl+w,ctrl+backspace,alt+backspace",
"history_previous": "up",
"history_next": "down",
"terminal_suspend": "ctrl+z",
"terminal_title_toggle": "none",
"tips_toggle": "<leader>h",
"display_thinking": "none"
}
}
```
---
## 領袖鑰匙
OpenCode 對大多數按鍵綁定使用 `leader` 鍵。這可以避免終端中的衝突。
默認情況下,`ctrl+x` 是主鍵,大多數操作要求您先按主鍵,然後再按快捷鍵。例如,要開始新會話,請先按`ctrl+x`,然後按`n`。
您不需要為鍵綁定使用主鍵,但我們建議您這樣做。
---
## 禁用按鍵綁定
您可以通過將鍵添加到您的配置中並使用值“none”來禁用鍵綁定。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"keybinds": {
"session_compact": "none"
}
}
```
---
## 桌面提示快捷方式
OpenCode 桌面應用程序提示輸入支持常見的 Readline/Emacs 風格的文本編輯快捷方式。這些是內置的,目前無法通過`opencode.json`進行配置。
|快捷方式 |行動|
| -------- | ---------------------------------------- |
| `ctrl+a` |移至當前行開頭 |
| `ctrl+e` |移至當前行尾 |
| `ctrl+b` |將光標向後移動一個字符 |
| `ctrl+f` |將光標向前移動一個字符 |
| `alt+b` |將光標向後移動一個字 |
| `alt+f` |將光標向前移動一個字 |
| `ctrl+d` |刪除光標下的字符|
| `ctrl+k` |殺到行尾 |
| `ctrl+u` |殺至行首 |
| `ctrl+w` |刪除前一個單詞 |
| `alt+d` |殺死下一個詞 |
| `ctrl+t` |轉置字符 |
| `ctrl+g` |取消彈出窗口/中止運行響應 |
---
## Shift+Enter
默認情況下,某些終端不發送帶有 Enter 的修飾鍵。您可能需要將終端配置為發送 `Shift+Enter` 作為轉義序列。
### Windows 終端
打開您的`settings.json`
```
%LOCALAPPDATA%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json
```
將其添加到根級 `actions` 數組:
```json
"actions": [
{
"command": {
"action": "sendInput",
"input": "\u001b[13;2u"
},
"id": "User.sendInput.ShiftEnterCustom"
}
]
```
將其添加到根級 `keybindings` 數組:
```json
"keybindings": [
{
"keys": "shift+enter",
"id": "User.sendInput.ShiftEnterCustom"
}
]
```
保存文件並重新啟動 Windows 終端或打開新選項卡。

View File

@@ -0,0 +1,188 @@
---
title: LSP服務器
description: OpenCode 與您的 LSP 服務器集成。
---
OpenCode 與您的語言服務器協議 (LSP) 集成,以幫助 LLM 與您的代碼庫交互。它使用診斷向法學碩士提供反饋。
---
## 內建
OpenCode 附帶了多種適用於流行語言的內置 LSP 服務器:
| LSP服務器|擴展 |要求|
| ------------------ | ------------------------------------------------------------------- | ------------------------------------------------------------ |
|天文 | .astro | Astro 項目自動安裝 |
| bash | .sh、.bash、.zsh、.ksh |自動安裝 bash-語言-服務器 |
|叮叮 | .c、.cpp、.cc、.cxx、.c++、.h、.hpp、.hh、.hxx、.h++ |自動安裝 C/C++ 項目 |
|夏普| .cs | `.NET SDK` 已安裝 |
| Clojure-lsp | 語言.clj、.cljs、.cljc、.edn | `clojure-lsp` 命令可用 |
|飛鏢 | .dart | `dart` 命令可用 |
|給定 | .ts、.tsx、.js、.jsx、.mjs | `deno` 命令可用(自動檢測 deno.json/deno.jsonc|
|長生不老藥-LS | .ex、.exs | `elixir` 命令可用 |
|埃斯林特 | .ts、.tsx、.js、.jsx、.mjs、.cjs、.mts、.cts、.vue | `eslint` 項目中的依賴項 |
|銳利 | .fs、.fsi、.fsx、.fsscript | `.NET SDK` 已安裝 |
|微光| .gleam | `gleam` 命令可用 |
|伙計們| .go | `go` 命令可用 |
| HLS | .hs、.lhs | `haskell-language-server-wrapper` 命令可用 |
| jdtls| .java | `Java SDK (version 21+)` 已安裝 |
| kotlin-ls | .kt、.kts | Kotlin 項目的自動安裝 |
| lua-ls | .lua |自動安裝 Lua 項目 |
|尼克斯 | .nix | `nixd` 命令可用 |
| ocaml-lsp | .ml、.mli | `ocamllsp` 命令可用 |
|奧克斯林特 | .ts、.tsx、.js、.jsx、.mjs、.cjs、.mts、.cts、.vue、.astro、.svelte | `oxlint` 項目中的依賴項 |
| php intelephense | php .php | PHP 項目的自動安裝 |
|棱鏡| .prisma | `prisma` 命令可用 |
|皮賴特| .py, .pyi | `pyright` 依賴項已安裝 |
| ruby-lsprubocop| .rb、.rake、.gemspec、.ru | `ruby` 和 `gem` 命令可用 |
|生鏽| .rs | `rust-analyzer` 命令可用 |
|源套件-lsp | .swift、.objc、.objcpp | `swift` 已安裝(`xcode` 在 macOS 上)|
|苗條| .svelte | Svelte 項目的自動安裝 |
|地形 | .tf、.tfvars |從 GitHub 版本自動安裝 |
|微霧| .typ、.typc |從 GitHub 版本自動安裝 |
|打字稿 | .ts、.tsx、.js、.jsx、.mjs、.cjs、.mts、.cts | `typescript` 項目中的依賴項 |
|視圖 | .vue | Vue 項目自動安裝 |
| yaml-ls | .yaml、.yml |自動安裝 Red Hat yaml-language-server |
|中立 | .zig、.zon | `zig` 命令可用 |
當檢測到上述文件擴展名之一併且滿足要求時LSP 服務器將自動啟用。
:::note
您可以通過將`OPENCODE_DISABLE_LSP_DOWNLOAD` 環境變量設置為`true` 來禁用自動LSP 服務器下載。
:::
---
## 它是如何運作的
當 opencode 打開一個文件時,它:
1. 根據所有啟用的 LSP 服務器檢查文件擴展名。
2. 如果尚未運行,則啟動相應的 LSP 服務器。
---
## 配置
您可以通過 opencode 配置中的 `lsp` 部分自定義 LSP 服務器。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"lsp": {}
}
```
每個LSP服務器支持以下功能
|物業 |類型 |描述 |
| ---------------- | -------- | ------------------------------------------------- |
| `disabled` |布爾 |將其設置為`true` 以禁用LSP 服務器|
| `command` |字符串[] |啟動LSP服務器的命令|
| `extensions` |字符串[] |此 LSP 服務器應處理的文件擴展名 |
| `env` |對象|啟動服務器時設置的環境變量|
| `initialization` |對象|發送到LSP服務器的初始化選項|
讓我們看一些例子。
---
### 環境變量
啟動LSP服務器時使用`env`屬性設置環境變量:
```json title="opencode.json" {5-7}
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"rust": {
"env": {
"RUST_LOG": "debug"
}
}
}
}
```
---
### 初始化選項
使用`initialization` 屬性將初始化選項傳遞給LSP 服務器。這些是在 LSP `initialize` 請求期間發送的服務器特定設置:
```json title="opencode.json" {5-9}
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"typescript": {
"initialization": {
"preferences": {
"importModuleSpecifierPreference": "relative"
}
}
}
}
}
```
:::note
初始化選項因 LSP 服務器而異。檢查 LSP 服務器的文檔以獲取可用選項。
:::
---
### 禁用LSP服務器
要全局禁用**所有** LSP 服務器,請將`lsp` 設置為`false`
```json title="opencode.json" {3}
{
"$schema": "https://opencode.ai/config.json",
"lsp": false
}
```
要禁用**特定** LSP 服務器,請將`disabled` 設置為`true`
```json title="opencode.json" {5}
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"typescript": {
"disabled": true
}
}
}
```
---
### 自定義 LSP 服務器
您可以通過指定命令和文件擴展名來添加自定義 LSP 服務器:
```json title="opencode.json" {4-7}
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"custom-lsp": {
"command": ["custom-lsp-server", "--stdio"],
"extensions": [".custom"]
}
}
}
```
---
## 附加信息
### PHP 英特爾芬斯
PHP Intelephense 通過許可證密鑰提供高級功能。您可以通過將(僅)密鑰放入位於以下位置的文本文件中來提供許可證密鑰:
- 在 macOS/Linux 上:`$HOME/intelephense/licence.txt`
- 在 Windows 上:`%USERPROFILE%/intelephense/licence.txt`
該文件應僅包含許可證密鑰,不包含其他內容。

View File

@@ -0,0 +1,511 @@
---
title: MCP服務器
description: 添加本地和遠程 MCP 工具。
---
您可以使用“模型上下文協議”或 MCP 將外部工具添加到 OpenCode。 OpenCode 支持本地和遠程服務器。
添加後MCP 工具將自動與內置工具一起供法學碩士使用。
---
#### 注意事項
當您使用 MCP 服務器時,它會添加到上下文中。如果您有很多工具,這會很快增加。因此,我們建議您謹慎選擇使用哪些 MCP 服務器。
:::tip
MCP 服務器會添加到您的上下文中,因此您需要小心啟用哪些服務器。
:::
某些 MCP 服務器(例如 GitHub MCP 服務器)往往會添加大量令牌,並且很容易超出上下文限制。
---
## 使能夠
您可以在`mcp` 下的[開放代碼配置](https://opencode.ai/docs/config/) 中定義MCP 服務器。為每個 MCP 添加唯一的名稱。當提示 LLM 時,您可以通過名稱引用該 MCP。
```jsonc title="opencode.jsonc" {6}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"name-of-mcp-server": {
// ...
"enabled": true,
},
"name-of-other-mcp-server": {
// ...
},
},
}
```
您還可以通過將`enabled` 設置為`false` 來禁用服務器。如果您想暫時禁用服務器而不將其從配置中刪除,這非常有用。
---
### 覆蓋遠程默認值
組織可以通過其 `.well-known/opencode` 端點提供默認 MCP 服務器。這些服務器可能默認被禁用,允許用戶選擇他們需要的服務器。
要從組織的遠程配置啟用特定服務器,請使用 `enabled: true` 將其添加到本地配置:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"jira": {
"type": "remote",
"url": "https://jira.example.com/mcp",
"enabled": true
}
}
}
```
您的本地配置值會覆蓋遠程默認值。有關更多詳細信息,請參閱[配置優先級](/docs/config#precedence-order)。
---
## 當地的
使用`type` 將本地MCP 服務器添加到MCP 對像中的`"local"`。
```jsonc title="opencode.jsonc" {15}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-local-mcp-server": {
"type": "local",
// Or ["bun", "x", "my-mcp-command"]
"command": ["npx", "-y", "my-mcp-command"],
"enabled": true,
"environment": {
"MY_ENV_VAR": "my_env_var_value",
},
},
},
}
```
該命令是本地 MCP 服務器的啟動方式。您還可以傳入環境變量列表。
例如,以下是添加測試 [`@modelcontextprotocol/server-everything`](https://www.npmjs.com/package/@modelcontextprotocol/server-everything) MCP 服務器的方法。
```jsonc title="opencode.jsonc"
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"mcp_everything": {
"type": "local",
"command": ["npx", "-y", "@modelcontextprotocol/server-everything"],
},
},
}
```
要使用它,我可以將 `use the mcp_everything tool` 添加到我的提示中。
```txt "mcp_everything"
use the mcp_everything tool to add the number 3 and 4
```
---
#### 選項
以下是配置本地 MCP 服務器的所有選項。
|選項|類型 |必填 |描述 |
| ------------- | ------- | -------- | ----------------------------------------------------------------------------------- |
| `type` |字符串|是 | MCP 服務器連接類型,必須是`"local"`。 |
| `command` |數組 |是 |運行 MCP 服務器的命令和參數。 |
| `environment` |對象| |運行服務器時設置的環境變量。 |
| `enabled` |布爾 | |在啟動時啟用或禁用 MCP 服務器。 |
| `timeout` |數量 | |從 MCP 服務器獲取工具的超時(以毫秒為單位)。默認為 50005 秒)。 |
---
## 偏僻的
通過將`type` 設置為`"remote"` 添加遠程MCP 服務器。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-remote-mcp": {
"type": "remote",
"url": "https://my-mcp-server.com",
"enabled": true,
"headers": {
"Authorization": "Bearer MY_API_KEY"
}
}
}
}
```
`url` 是遠程 MCP 服務器的 URL使用 `headers` 選項您可以傳入標頭列表。
---
#### 選項
|選項|類型 |必填 |描述 |
| --------- | ------- | -------- | ----------------------------------------------------------------------------------- |
| `type` |字符串|是 | MCP 服務器連接類型,必須是`"remote"`。 |
| `url` |字符串|是 |遠程 MCP 服務器的 URL。 |
| `enabled` |布爾 | |在啟動時啟用或禁用 MCP 服務器。 |
| `headers` |對象| |隨請求一起發送的標頭。 |
| `oauth` |對象| | OAuth 身份驗證配置。請參閱下面的[開放認證](#oauth) 部分。 |
| `timeout` |數量 | |從 MCP 服務器獲取工具的超時(以毫秒為單位)。默認為 50005 秒)。 |
---
## 開放認證
OpenCode 自動處理遠程 MCP 服務器的 OAuth 身份驗證。當服務器需要身份驗證時OpenCode 將:
1. 檢測 401 響應並啟動 OAuth 流程
2. 如果服務器支持,請使用**動態客戶端註冊 (RFC 7591)**
3. 安全地存儲令牌以供將來的請求
---
### 自動的
對於大多數支持 OAuth 的 MCP 服務器,不需要特殊配置。只需配置遠程服務器:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-oauth-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp"
}
}
}
```
如果服務器需要身份驗證OpenCode 將在您第一次嘗試使用它時提示您進行身份驗證。如果沒有,您可以使用 `opencode mcp auth <server-name>`[手動觸發流量](#authenticating)。
---
### 預登記
如果您有來自 MCP 服務器提供商的客戶端憑據,則可以配置它們:
```json title="opencode.json" {7-11}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-oauth-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp",
"oauth": {
"clientId": "{env:MY_MCP_CLIENT_ID}",
"clientSecret": "{env:MY_MCP_CLIENT_SECRET}",
"scope": "tools:read tools:execute"
}
}
}
}
```
---
### 正在驗證
您可以手動觸發身份驗證或管理憑據。
使用特定 MCP 服務器進行身份驗證:
```bash
opencode mcp auth my-oauth-server
```
列出所有 MCP 服務器及其身份驗證狀態:
```bash
opencode mcp list
```
刪除存儲的憑據:
```bash
opencode mcp logout my-oauth-server
```
`mcp auth` 命令將打開您的瀏覽器進行授權。授權後OpenCode 會將令牌安全地存儲在`~/.local/share/opencode/mcp-auth.json` 中。
---
#### 禁用 OAuth
如果要禁用服務器的自動 OAuth例如對於使用 API 密鑰的服務器),請將 `oauth` 設置為 `false`
```json title="opencode.json" {7}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-api-key-server": {
"type": "remote",
"url": "https://mcp.example.com/mcp",
"oauth": false,
"headers": {
"Authorization": "Bearer {env:MY_API_KEY}"
}
}
}
}
```
---
#### OAuth 選項
|選項|類型 |描述 |
| -------------- | --------------- | -------------------------------------------------------------------------------- |
| `oauth` |對象\|假 | OAuth 配置對象,或 `false` 以禁用 OAuth 自動檢測。 |
| `clientId` |字符串| OAuth 客戶端 ID。如果未提供將嘗試動態客戶端註冊。 |
| `clientSecret` |字符串| OAuth 客戶端密鑰(如果授權服務器需要)。 |
| `scope` |字符串|授權期間請求的 OAuth 範圍。 |
#### 偵錯
如果遠程 MCP 服務器無法進行身份驗證,您可以通過以下方式診斷問題:
```bash
# View auth status for all OAuth-capable servers
opencode mcp auth list
# Debug connection and OAuth flow for a specific server
opencode mcp debug my-oauth-server
```
`mcp debug` 命令顯示當前身份驗證狀態、測試 HTTP 連接並嘗試 OAuth 發現流程。
---
## 管理
您的 MCP 可作為 OpenCode 中的工具以及內置工具使用。因此,您可以像任何其他工具一樣通過 OpenCode 配置來管理它們。
---
### 全球的
這意味著您可以全局啟用或禁用它們。
```json title="opencode.json" {14}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-mcp-foo": {
"type": "local",
"command": ["bun", "x", "my-mcp-command-foo"]
},
"my-mcp-bar": {
"type": "local",
"command": ["bun", "x", "my-mcp-command-bar"]
}
},
"tools": {
"my-mcp-foo": false
}
}
```
我們還可以使用 glob 模式來禁用所有匹配的 MCP。
```json title="opencode.json" {14}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-mcp-foo": {
"type": "local",
"command": ["bun", "x", "my-mcp-command-foo"]
},
"my-mcp-bar": {
"type": "local",
"command": ["bun", "x", "my-mcp-command-bar"]
}
},
"tools": {
"my-mcp*": false
}
}
```
這裡我們使用 glob 模式 `my-mcp*` 來禁用所有 MCP。
---
### 每個代理人
如果您有大量 MCP 服務器,您可能只想為每個代理啟用它們並全局禁用它們。為此:
1. 全局禁用它作為工具。
2. 在您的[代理配置](/docs/agents#tools) 中啟用MCP 服務器作為工具。
```json title="opencode.json" {11, 14-18}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"my-mcp": {
"type": "local",
"command": ["bun", "x", "my-mcp-command"],
"enabled": true
}
},
"tools": {
"my-mcp*": false
},
"agent": {
"my-agent": {
"tools": {
"my-mcp*": true
}
}
}
}
```
---
#### 全局模式
glob 模式使用簡單的正則表達式 globbing 模式:
- `*` 匹配零個或多個任意字符(例如,`"my-mcp*"` 匹配 `my-mcp_search`、`my-mcp_list` 等)
- `?` 恰好匹配一個字符
- 所有其他字符均按字面意思匹配
:::note
MCP 服務器工具以服務器名稱作為前綴進行註冊,因此要禁用服務器的所有工具,只需使用:
```
"mymcpservername_*": false
```
:::
---
## 示例
以下是一些常見 MCP 服務器的示例。如果您想記錄其他服務器,您可以提交 PR。
---
### 哨兵
添加 [哨兵 MCP 服務器](https://mcp.sentry.dev) 以與您的 Sentry 項目和問題進行交互。
```json title="opencode.json" {4-8}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"sentry": {
"type": "remote",
"url": "https://mcp.sentry.dev/mcp",
"oauth": {}
}
}
}
```
添加配置後使用Sentry進行身份驗證
```bash
opencode mcp auth sentry
```
這將打開一個瀏覽器窗口以完成 OAuth 流程並將 OpenCode 連接到您的 Sentry 帳戶。
通過身份驗證後,您可以在提示中使用 Sentry 工具來查詢問題、項目和錯誤數據。
```txt "use sentry"
Show me the latest unresolved issues in my project. use sentry
```
---
### 背景7
添加[Context7 MCP 服務器](https://github.com/upstash/context7) 以搜索文檔。
```json title="opencode.json" {4-7}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp"
}
}
}
```
如果您註冊了免費帳戶,則可以使用 API 密鑰並獲得更高的速率限制。
```json title="opencode.json" {7-9}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"context7": {
"type": "remote",
"url": "https://mcp.context7.com/mcp",
"headers": {
"CONTEXT7_API_KEY": "{env:CONTEXT7_API_KEY}"
}
}
}
}
```
這裡我們假設您設置了 `CONTEXT7_API_KEY` 環境變量。
將 `use context7` 添加到提示中以使用 Context7 MCP 服務器。
```txt "use context7"
Configure a Cloudflare Worker script to cache JSON API responses for five minutes. use context7
```
或者,您可以將類似的內容添加到您的[代理.md](/docs/rules/)。
```md title="AGENTS.md"
When you need to search docs, use `context7` tools.
```
---
### Vercel 的 Grep
添加 [Vercel 的 Grep](https://grep.app) MCP 服務器以搜索 GitHub 上的代碼片段。
```json title="opencode.json" {4-7}
{
"$schema": "https://opencode.ai/config.json",
"mcp": {
"gh_grep": {
"type": "remote",
"url": "https://mcp.grep.app"
}
}
}
```
由於我們將 MCP 服務器命名為 `gh_grep`,因此您可以將 `use the gh_grep tool` 添加到提示中以使代理使用它。
```txt "use the gh_grep tool"
What's the right way to set a custom domain in an SST Astro component? use the gh_grep tool
```
或者,您可以將類似的內容添加到您的[代理.md](/docs/rules/)。
```md title="AGENTS.md"
If you are unsure how to do something, use `gh_grep` to search code examples from GitHub.
```

View File

@@ -0,0 +1,223 @@
---
title: 型號
description: 配置LLM提供者和模型。
---
OpenCode 使用[人工智能軟件開發工具包](https://ai-sdk.dev/) 和[模型.dev](https://models.dev) 來支持**75+ LLM 提供商**,並且它支持運行本地模型。
---
## 供應商
默認情況下會預加載大多數流行的提供程序。如果您已通過 `/connect` 命令添加了提供程序的憑據,那麼它們將在您啟動 OpenCode 時可用。
了解有關[提供者](/docs/providers) 的更多信息。
---
## 選擇型號
配置完提供商後,您可以通過輸入以下內容來選擇您想要的型號:
```bash frame="none"
/models
```
---
## 推薦型號
那裡有很多型號,每週都會有新型號問世。
:::tip
考慮使用我們推薦的模型之一。
:::
然而,既擅長生成代碼又擅長工具調用的只有少數。
以下是與 OpenCode 配合良好的幾個模型,排名不分先後。 (這不是詳盡的列表,也不一定是最新的):
- GPT 5.2
- GPT 5.1 法典
- 近距離工作4.5
- 克勞德十四行詩 4.5
- 極小極大M2.1
- 雙子座 3 專業版
---
## 設置默認值
要將其中之一設置為默認模型,您可以在您的
打開代碼配置。
```json title="opencode.json" {3}
{
"$schema": "https://opencode.ai/config.json",
"model": "lmstudio/google/gemma-3n-e4b"
}
```
這裡完整的 ID 是`provider_id/model_id`。例如,如果您使用 [開放代碼禪](/docs/zen),則您將使用 `opencode/gpt-5.1-codex` 來表示 GPT 5.1 Codex。
如果您配置了[定制提供商](/docs/providers#custom),則`provider_id` 是配置中`provider` 部分的密鑰,`model_id` 是`provider.models` 中的密鑰。
---
## 配置模型
您可以通過 config.json 全局配置模型的選項。
```jsonc title="opencode.jsonc" {7-12,19-24}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"openai": {
"models": {
"gpt-5": {
"options": {
"reasoningEffort": "high",
"textVerbosity": "low",
"reasoningSummary": "auto",
"include": ["reasoning.encrypted_content"],
},
},
},
},
"anthropic": {
"models": {
"claude-sonnet-4-5-20250929": {
"options": {
"thinking": {
"type": "enabled",
"budgetTokens": 16000,
},
},
},
},
},
},
}
```
在這裡,我們為兩個內置模型配置全局設置:`gpt-5`(通過 `openai` 提供程序訪問時)和 `claude-sonnet-4-20250514`(通過 `anthropic` 提供程序訪問時)。
內置提供商和型號名稱可以在[模型.dev](https://models.dev) 上找到。
您還可以為您正在使用的任何代理配置這些選項。代理配置會覆蓋此處的所有全局選項。 [了解更多](/docs/agents/#additional)。
您還可以定義擴展內置變體的自定義變體。變體允許您為同一模型配置不同的設置,而無需創建重複的條目:
```jsonc title="opencode.jsonc" {6-21}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"opencode": {
"models": {
"gpt-5": {
"variants": {
"high": {
"reasoningEffort": "high",
"textVerbosity": "low",
"reasoningSummary": "auto",
},
"low": {
"reasoningEffort": "low",
"textVerbosity": "low",
"reasoningSummary": "auto",
},
},
},
},
},
},
}
```
---
## 變體
許多型號支持具有不同配置的多種變體。 OpenCode 附帶了流行提供商的內置默認變體。
### 內置變體
OpenCode 附帶了許多提供商的默認變體:
**人擇**
- `high` - 高思維預算(默認)
- `max` - 最大思維預算
**開放人工智能**
因型號而異,但大致如下:
- `none` - 沒有推理
- `minimal` - 最少的推理工作
- `low` - 推理工作量低
- `medium` - 中等推理努力
- `high` - 高推理能力
- `xhigh` - 極高的推理能力
**Google**:
- `low` - 降低工作量/代幣預算
- `high` - 更高的努力/代幣預算
:::tip
該列表並不全面。許多其他提供商也有內置的默認值。
:::
### 定制變體
您可以覆蓋現有變體或添加您自己的變體:
```jsonc title="opencode.jsonc" {7-18}
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"openai": {
"models": {
"gpt-5": {
"variants": {
"thinking": {
"reasoningEffort": "high",
"textVerbosity": "low",
},
"fast": {
"disabled": true,
},
},
},
},
},
},
}
```
### 循環變體
使用鍵綁定 `variant_cycle` 在變體之間快速切換。 [了解更多](/docs/keybinds)。
---
## 加載模型
當 OpenCode 啟動時,它會按以下優先順序檢查模型:
1. `--model` 或 `-m` 命令行標誌。格式與配置文件中的相同:`provider_id/model_id`。
2. OpenCode 配置中的模型列表。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"model": "anthropic/claude-sonnet-4-20250514"
}
```
這裡的格式是`provider/model`。
3. 最後使用的型號。
4. 第一個模型使用內部優先級。

View File

@@ -0,0 +1,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 文件定義模式。將它們放入:
- 全球:`~/.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 將使用特定於模型的默認值(大多數模型通常為 0Qwen 模型為 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 和讀取工具的調查
- **文檔模式**:使用文件操作但不使用系統命令的文檔編寫
您可能還會發現不同的模型適用於不同的用例。

View File

@@ -0,0 +1,57 @@
---
title: 網絡
description: 配置代理和自定義證書。
---
OpenCode 支持企業網絡環境的標準代理環境變量和自定義證書。
---
## 代理人
OpenCode 尊重標準代理環境變量。
```bash
# HTTPS proxy (recommended)
export HTTPS_PROXY=https://proxy.example.com:8080
# HTTP proxy (if HTTPS not available)
export HTTP_PROXY=http://proxy.example.com:8080
# Bypass proxy for local server (required)
export NO_PROXY=localhost,127.0.0.1
```
:::caution
TUI 與本地 HTTP 服務器通信。您必須繞過此連接的代理以防止路由循環。
:::
您可以使用[CLI 標誌](/docs/cli#run)配置服務器的端口和主機名。
---
### 認證
如果您的代理需要基本身份驗證,請在 URL 中包含憑據。
```bash
export HTTPS_PROXY=http://username:password@proxy.example.com:8080
```
:::caution
避免對密碼進行硬編碼。使用環境變量或安全憑證存儲。
:::
對於需要高級身份驗證(例如 NTLM 或 Kerberos的代理請考慮使用支持您的身份驗證方法的 LLM 網關。
---
## 定制證書
如果您的企業使用自定義 CA 進行 HTTPS 連接,請配置 OpenCode 以信任它們。
```bash
export NODE_EXTRA_CA_CERTS=/path/to/ca-cert.pem
```
這適用於代理連接和直接 API 訪問。

View File

@@ -0,0 +1,237 @@
---
title: 權限
description: 控制哪些操作需要批准才能運行。
---
OpenCode 使用`permission` 配置來決定給定的操作是否應自動運行、提示您或被阻止。
從 `v1.1.1` 開始,舊版 `tools` 布爾配置已被棄用,並已合併到 `permission` 中。仍支持舊的 `tools` 配置以實現向後兼容性。
---
## 行動
每個權限規則解析為以下之一:
- `"allow"` — 未經批准運行
- `"ask"` — 提示批准
- `"deny"` — 阻止該操作
---
## 配置
您可以全局設置權限(使用`*`),並覆蓋特定工具。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"*": "ask",
"bash": "allow",
"edit": "deny"
}
}
```
您還可以一次設置所有權限:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"permission": "allow"
}
```
---
## 粒度規則(對象語法)
對於大多數權限,您可以使用對像根據工具輸入應用不同的操作。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"bash": {
"*": "ask",
"git *": "allow",
"npm *": "allow",
"rm *": "deny",
"grep *": "allow"
},
"edit": {
"*": "deny",
"packages/web/src/content/docs/*.mdx": "allow"
}
}
}
```
規則通過模式匹配進行評估,**最後匹配的規則獲勝**。常見的模式是將包羅萬象的 `"*"` 規則放在前面,然後再放置更具體的規則。
### 通配符
權限模式使用簡單的通配符匹配:
- `*` 匹配零個或多個任意字符
- `?` 恰好匹配一個字符
- 所有其他字符均按字面意思匹配
### 主目錄擴展
您可以在模式開頭使用 `~` 或 `$HOME` 來引用您的主目錄。這對於 [`external_directory`](#external-directories) 規則特別有用。
- `~/projects/*` -> `/Users/username/projects/*`
- `$HOME/projects/*` -> `/Users/username/projects/*`
- `~` -> `/Users/username`
### 外部目錄
使用 `external_directory` 允許工具調用觸及啟動 OpenCode 的工作目錄之外的路徑。這適用於任何採用路徑作為輸入的工具(例如`read`、`edit`、`list`、`glob`、`grep` 和許多`bash` 命令)。
主擴展(如`~/...`)僅影響模式的編寫方式。它不會使外部路徑成為當前工作空間的一部分,因此仍必須通過 `external_directory` 允許工作目錄之外的路徑。
例如,這允許訪問 `~/projects/personal/` 下的所有內容:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"external_directory": {
"~/projects/personal/**": "allow"
}
}
}
```
此處允許的任何目錄都會繼承與當前工作空間相同的默認值。自[`read` 默認為 `allow`](#defaults) 起,也允許讀取`external_directory` 下的條目,除非被覆蓋。當工具應限制在這些路徑中時添加顯式規則,例如在保留讀取的同時阻止編輯:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"external_directory": {
"~/projects/personal/**": "allow"
},
"edit": {
"~/projects/personal/**": "deny"
}
}
}
```
將列表重點放在受信任的路徑上,並根據其他工具的需要分層額外的允許或拒絕規則(例如 `bash`)。
---
## 可用權限
OpenCode 權限由工具名稱和一些安全防護措施決定:
- `read` — 讀取文件(與文件路徑匹配)
- `edit` — 所有文件修改(涵蓋`edit`、`write`、`patch`、`multiedit`
- `glob` — 文件通配符(匹配通配符模式)
- `grep` — 內容搜索(匹配正則表達式模式)
- `list` — 列出目錄中的文件(與目錄路徑匹配)
- `bash` — 運行 shell 命令(匹配 `git status --porcelain` 等解析命令)
- `task` — 啟動子代理(與子代理類型匹配)
- `skill` — 加載技能(與技能名稱匹配)
- `lsp` — 運行 LSP 查詢(當前非粒度)
- `todoread`、`todowrite` — 讀取/更新待辦事項列表
- `webfetch` — 獲取 URL與 URL 匹配)
- `websearch`、`codesearch` — 網頁/代碼搜索(與查詢匹配)
- `external_directory` — 當工具觸及項目工作目錄之外的路徑時觸發
- `doom_loop` — 當相同的工具調用使用相同的輸入重複 3 次時觸發
---
## 預設值
如果您不指定任何內容OpenCode 將從許可的默認值開始:
- 大多數權限默認為`"allow"`。
- `doom_loop` 和`external_directory` 默認為`"ask"`。
- `read` 是 `"allow"`,但 `.env` 文件默認被拒絕:
```json title="opencode.json"
{
"permission": {
"read": {
"*": "allow",
"*.env": "deny",
"*.env.*": "deny",
"*.env.example": "allow"
}
}
}
```
---
## “問”的作用是什麼
當 OpenCode 提示批准時UI 會提供三種結果:
- `once` — 僅批准此請求
- `always` — 批准與建議模式匹配的未來請求(對於當前 OpenCode 會話的其餘部分)
- `reject` — 拒絕請求
`always` 將批准的模式集由該工具提供例如bash 批准通常將安全命令前綴(如 `git status*`)列入白名單)。
---
## 代理商
您可以覆蓋每個代理的權限。代理權限與全局​​配置合併,代理規則優先。 [了解更多](/docs/agents#permissions) 關於代理權限。
:::note
有關更詳細的模式匹配示例,請參閱上面的 [粒度規則(對象語法)](#granular-rules-object-syntax) 部分。
:::
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"bash": {
"*": "ask",
"git *": "allow",
"git commit *": "deny",
"git push *": "deny",
"grep *": "allow"
}
},
"agent": {
"build": {
"permission": {
"bash": {
"*": "ask",
"git *": "allow",
"git commit *": "ask",
"git push *": "deny",
"grep *": "allow"
}
}
}
}
}
```
您還可以在 Markdown 中配置代理權限:
```markdown title="~/.config/opencode/agents/review.md"
---
description: Code review without edits
mode: subagent
permission:
edit: deny
bash: ask
webfetch: deny
---
Only analyze code and suggest changes.
```
:::tip
對帶有參數的命令使用模式匹配。 `"grep *"` 允許`grep pattern file.txt`,而`"grep"` 單獨會阻止它。像 `git status` 這樣的命令適用於默認行為,但在傳遞參數時需要顯式許可(如 `"git status *"`)。
:::

View File

@@ -0,0 +1,385 @@
---
title: 插件
description: 編寫您自己的插件來擴展 OpenCode。
---
插件允許您通過掛鉤各種事件和自定義行為來擴展 OpenCode。您可以創建插件來添加新功能、與外部服務集成或修改 OpenCode 的默認行為。
例如,查看社區創建的[插件](/docs/ecosystem#plugins)。
---
## 使用插件
有兩種加載插件的方法。
---
### 從本地文件
將 JavaScript 或 TypeScript 文件放置在插件目錄中。
- `.opencode/plugins/` - 項目級插件
- `~/.config/opencode/plugins/` - 全局插件
這些目錄中的文件會在啟動時自動加載。
---
### 來自 npm
在配置文件中指定 npm 包。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-helicone-session", "opencode-wakatime", "@my-org/custom-plugin"]
}
```
支持常規和範圍 npm 包。
瀏覽[生態系統](/docs/ecosystem#plugins)中的可用插件。
---
### 插件是如何安裝的
**npm 插件** 在啟動時使用 Bun 自動安裝。包及其依賴項緩存在`~/.cache/opencode/node_modules/`中。
**本地插件**直接從插件目錄加載。要使用外部包,您必須在配置目錄中創建`package.json`(請參閱[依賴關係](#dependencies)或將插件發佈到npm和[將其添加到您的配置中](/docs/config#plugins)。
---
### 加載順序
插件從所有源加載,所有掛鉤按順序運行。加載順序為:
1. 全局配置 (`~/.config/opencode/opencode.json`)
2. 項目配置(`opencode.json`
3. 全局插件目錄 (`~/.config/opencode/plugins/`)
4. 項目插件目錄(`.opencode/plugins/`)
具有相同名稱和版本的重複 npm 包將被加載一次。但是,本地插件和名稱相似的 npm 插件都是分開加載的。
---
## 創建一個插件
插件是一個 **JavaScript/TypeScript 模塊**,它導出一個或多個插件
功能。每個函數接收一個上下文對象並返回一個鉤子對象。
---
### 依賴關係
本地插件和自定義工具可以使用外部 npm 包。將 `package.json` 添加到您的配置目錄,其中包含您需要的依賴項。
```json title=".opencode/package.json"
{
"dependencies": {
"shescape": "^2.1.0"
}
}
```
OpenCode 在啟動時運行 `bun install` 來安裝這些。然後您的插件和工具就可以導入它們。
```ts title=".opencode/plugins/my-plugin.ts"
import { escape } from "shescape"
export const MyPlugin = async (ctx) => {
return {
"tool.execute.before": async (input, output) => {
if (input.tool === "bash") {
output.args.command = escape(output.args.command)
}
},
}
}
```
---
### 基本結構
```js title=".opencode/plugins/example.js"
export const MyPlugin = async ({ project, client, $, directory, worktree }) => {
console.log("Plugin initialized!")
return {
// Hook implementations go here
}
}
```
插件函數接收:
- `project`:當前項目信息。
- `directory`:當前工作目錄。
- `worktree`git 工作樹路徑。
- `client`:用於與 AI 交互的開放代碼 SDK 客戶端。
- `$`Bun的[外殼API](https://bun.com/docs/runtime/shell)用於執行命令。
---
### TypeScript 支持
對於 TypeScript 插件,您可以從插件包中導入類型:
```ts title="my-plugin.ts" {1}
import type { Plugin } from "@opencode-ai/plugin"
export const MyPlugin: Plugin = async ({ project, client, $, directory, worktree }) => {
return {
// Type-safe hook implementations
}
}
```
---
### 活動
插件可以訂閱事件,如下面的示例部分所示。以下是可用的不同事件的列表。
#### 命令事件
- `command.executed`
#### 文件事件
- `file.edited`
- `file.watcher.updated`
#### 安裝活動
- `installation.updated`
#### LSP活動
- `lsp.client.diagnostics`
- `lsp.updated`
#### 消息事件
- `message.part.removed`
- `message.part.updated`
- `message.removed`
- `message.updated`
#### 權限事件
- `permission.asked`
- `permission.replied`
#### 服務器事件
- `server.connected`
#### 會議活動
- `session.created`
- `session.compacted`
- `session.deleted`
- `session.diff`
- `session.error`
- `session.idle`
- `session.status`
- `session.updated`
#### 都都活動
- `todo.updated`
#### 殼牌活動
- `shell.env`
#### 工具事件
- `tool.execute.after`
- `tool.execute.before`
#### 途易活動
- `tui.prompt.append`
- `tui.command.execute`
- `tui.toast.show`
---
## 示例
以下是一些可用於擴展 opencode 的插件示例。
---
### 發送通知
當某些事件發生時發送通知:
```js title=".opencode/plugins/notification.js"
export const NotificationPlugin = async ({ project, client, $, directory, worktree }) => {
return {
event: async ({ event }) => {
// Send notification on session completion
if (event.type === "session.idle") {
await $`osascript -e 'display notification "Session completed!" with title "opencode"'`
}
},
}
}
```
我們使用 `osascript` 在 macOS 上運行 AppleScript。這裡我們用它來發送通知。
:::note
如果您使用 OpenCode 桌面應用程序,它可以在響應準備就緒或會話出錯時自動發送系統通知。
:::
---
### .env 保護
阻止 opencode 讀取 `.env` 文件:
```javascript title=".opencode/plugins/env-protection.js"
export const EnvProtection = async ({ project, client, $, directory, worktree }) => {
return {
"tool.execute.before": async (input, output) => {
if (input.tool === "read" && output.args.filePath.includes(".env")) {
throw new Error("Do not read .env files")
}
},
}
}
```
---
### 注入環境變量
將環境變量注入所有 shell 執行AI 工具和用戶終端):
```javascript title=".opencode/plugins/inject-env.js"
export const InjectEnvPlugin = async () => {
return {
"shell.env": async (input, output) => {
output.env.MY_API_KEY = "secret"
output.env.PROJECT_ROOT = input.cwd
},
}
}
```
---
### 定制工具
插件還可以向 opencode 添加自定義工具:
```ts title=".opencode/plugins/custom-tools.ts"
import { type Plugin, tool } from "@opencode-ai/plugin"
export const CustomToolsPlugin: Plugin = async (ctx) => {
return {
tool: {
mytool: tool({
description: "This is a custom tool",
args: {
foo: tool.schema.string(),
},
async execute(args, context) {
const { directory, worktree } = context
return `Hello ${args.foo} from ${directory} (worktree: ${worktree})`
},
}),
},
}
}
```
`tool` 幫助器創建一個 opencode 可以調用的自定義工具。它採用 Zod 模式函數並返回一個工具定義:
- `description`:該工具的作用
- `args`:工具參數的 Zod 模式
- `execute`:調用工具時運行的函數
您的自定義工具將可與內置工具一起用於開放代碼。
---
### 記錄
使用 `client.app.log()` 而不是 `console.log` 進行結構化日誌記錄:
```ts title=".opencode/plugins/my-plugin.ts"
export const MyPlugin = async ({ client }) => {
await client.app.log({
body: {
service: "my-plugin",
level: "info",
message: "Plugin initialized",
extra: { foo: "bar" },
},
})
}
```
級別:`debug`、`info`、`warn`、`error`。詳情請參閱[SDK文檔](https://opencode.ai/docs/sdk)。
---
### 壓實鉤
自定義壓縮會話時包含的上下文:
```ts title=".opencode/plugins/compaction.ts"
import type { Plugin } from "@opencode-ai/plugin"
export const CompactionPlugin: Plugin = async (ctx) => {
return {
"experimental.session.compacting": async (input, output) => {
// Inject additional context into the compaction prompt
output.context.push(`
## Custom Context
Include any state that should persist across compaction:
- Current task status
- Important decisions made
- Files being actively worked on
`)
},
}
}
```
`experimental.session.compacting` 鉤子在 LLM 生成延續摘要之前觸發。使用它來注入默認壓縮提示會錯過的特定於域的上下文。
您還可以通過設置`output.prompt`來完全替換壓縮提示:
```ts title=".opencode/plugins/custom-compaction.ts"
import type { Plugin } from "@opencode-ai/plugin"
export const CustomCompactionPlugin: Plugin = async (ctx) => {
return {
"experimental.session.compacting": async (input, output) => {
// Replace the entire compaction prompt
output.prompt = `
You are generating a continuation prompt for a multi-agent swarm session.
Summarize:
1. The current task and its status
2. Which files are being modified and by whom
3. Any blockers or dependencies between agents
4. The next steps to complete the work
Format as a structured prompt that a new agent can use to resume work.
`
},
}
}
```
當設置`output.prompt`時,它完全取代默認的壓縮提示。在這種情況下,`output.context` 數組將被忽略。

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,180 @@
---
title: 規則
description: 設置開放代碼的自定義指令。
---
您可以通過創建 `AGENTS.md` 文件來提供 opencode 的自定義指令。這和Cursor的規則類似。它包含將包含在 LLM 上下文中的說明,以便為您的特定項目自定義其行為。
---
## 初始化
要創建新的`AGENTS.md`文件您可以在opencode中運行`/init`命令。
:::tip
您應該將項目的 `AGENTS.md` 文件提交到 Git。
:::
這將掃描您的項目及其所有內容,以了解該項目的內容並生成一個 `AGENTS.md` 文件。這有助於 opencode 更好地導航項目。
如果您有現有的 `AGENTS.md` 文件,這將嘗試添加到其中。
---
## 例子
您也可以手動創建此文件。以下是您可以放入 `AGENTS.md` 文件中的一些內容的示例。
```markdown title="AGENTS.md"
# SST v3 Monorepo Project
This is an SST v3 monorepo with TypeScript. The project uses bun workspaces for package management.
## Project Structure
- `packages/` - Contains all workspace packages (functions, core, web, etc.)
- `infra/` - Infrastructure definitions split by service (storage.ts, api.ts, web.ts)
- `sst.config.ts` - Main SST configuration with dynamic imports
## Code Standards
- Use TypeScript with strict mode enabled
- Shared code goes in `packages/core/` with proper exports configuration
- Functions go in `packages/functions/`
- Infrastructure should be split into logical files in `infra/`
## Monorepo Conventions
- Import shared modules using workspace names: `@my-app/core/example`
```
我們在此處添加特定於項目的說明,這將在您的團隊中共享。
---
## 類型
opencode 還支持從多個位置讀取 `AGENTS.md` 文件。這有不同的目的。
### 專案
將 `AGENTS.md` 放置在項目根目錄中以獲取特定於項目的規則。這些僅適用於您在此目錄或其子目錄中工作時。
### 全球的
您還可以在 `~/.config/opencode/AGENTS.md` 文件中包含全局規則。這適用於所有開放代碼會話。
由於這未提交給 Git 或與您的團隊共享,因此我們建議使用它來指定 LLM 應遵循的任何個人規則。
### 克勞德代碼兼容性
對於從 Claude Code 遷移的用戶OpenCode 支持 Claude Code 的文件約定作為後備:
- **項目規則**:項目目錄中的`CLAUDE.md`(如果`AGENTS.md`不存在則使用)
- **全局規則**`~/.claude/CLAUDE.md`(如果不存在`~/.config/opencode/AGENTS.md`則使用)
- **技能**`~/.claude/skills/` — 詳情請參閱[代理技巧](/docs/skills/)
要禁用 Claude Code 兼容性,請設置以下環境變量之一:
```bash
export OPENCODE_DISABLE_CLAUDE_CODE=1 # Disable all .claude support
export OPENCODE_DISABLE_CLAUDE_CODE_PROMPT=1 # Disable only ~/.claude/CLAUDE.md
export OPENCODE_DISABLE_CLAUDE_CODE_SKILLS=1 # Disable only .claude/skills
```
---
## 優先級
當 opencode 啟動時,它會按以下順序查找規則文件:
1. **本地文件**,從當前目錄向上遍歷(`AGENTS.md`,`CLAUDE.md`)
2. **全局文件** `~/.config/opencode/AGENTS.md`
3. **克勞德代碼文件**位於`~/.claude/CLAUDE.md`(除非禁用)
第一個匹配的文件在每個類別中獲勝。例如,如果您同時擁有`AGENTS.md` 和`CLAUDE.md`,則僅使用`AGENTS.md`。同樣,`~/.config/opencode/AGENTS.md` 優先於`~/.claude/CLAUDE.md`。
---
## 定制說明
您可以在 `opencode.json` 或全局 `~/.config/opencode/opencode.json` 中指定自定義指令文件。這允許您和您的團隊重用現有規則,而不必將它們複製到 AGENTS.md。
例子:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"instructions": ["CONTRIBUTING.md", "docs/guidelines.md", ".cursor/rules/*.md"]
}
```
您還可以使用遠程 URL 從 Web 加載說明。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"instructions": ["https://raw.githubusercontent.com/my-org/shared-rules/main/style.md"]
}
```
遠程指令的獲取有 5 秒的超時時間。
所有說明文件均與您的`AGENTS.md` 文件合併。
---
## 引用外部文件
雖然 opencode 不會自動解析 `AGENTS.md` 中的文件引用,但您可以通過兩種方式實現類似的功能:
### 使用 opencode.json
推薦的方法是在`opencode.json`中使用`instructions`字段:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"instructions": ["docs/development-standards.md", "test/testing-guidelines.md", "packages/*/AGENTS.md"]
}
```
### AGENTS.md 中的手動說明
您可以通過在 `AGENTS.md` 中提供明確的指令來教 opencode 讀取外部文件。這是一個實際的例子:
```markdown title="AGENTS.md"
# TypeScript Project Rules
## External File Loading
CRITICAL: When you encounter a file reference (e.g., @rules/general.md), use your Read tool to load it on a need-to-know basis. They're relevant to the SPECIFIC task at hand.
Instructions:
- Do NOT preemptively load all references - use lazy loading based on actual need
- When loaded, treat content as mandatory instructions that override defaults
- Follow references recursively when needed
## Development Guidelines
For TypeScript code style and best practices: @docs/typescript-guidelines.md
For React component architecture and hooks patterns: @docs/react-patterns.md
For REST API design and error handling: @docs/api-standards.md
For testing strategies and coverage requirements: @test/testing-guidelines.md
## General Guidelines
Read the following file immediately as it's relevant to all workflows: @rules/general-guidelines.md.
```
這種方法允許您:
- 創建模塊化、可重用的規則文件
- 通過符號鏈接或 git 子模塊在項目之間共享規則
- 保持 AGENTS.md 簡潔,同時參考詳細指南
- 確保 opencode 僅在特定任務需要時加載文件
:::tip
對於 monorepos 或具有共享標準的項目,使用 `opencode.json` 和 glob 模式(如 `packages/*/AGENTS.md`)比手動指令更易於維護。
:::

View File

@@ -0,0 +1,391 @@
---
title: 軟件開發工具包
description: Opencode 服務器的類型安全 JS 客戶端。
---
import config from "../../../../config.mjs"
export const typesUrl = `${config.github}/blob/dev/packages/sdk/js/src/gen/types.gen.ts`
opencode JS/TS SDK 提供類型安全的客戶端用於與服務器交互。
使用它以編程方式構建集成和控制開放代碼。
[了解更多](/docs/server) 關於服務器如何工作。例如,查看社區構建的[專案](/docs/ecosystem#projects)。
---
## 安裝
從 npm 安裝 SDK
```bash
npm install @opencode-ai/sdk
```
---
## 創建客戶端
創建 opencode 的實例:
```javascript
import { createOpencode } from "@opencode-ai/sdk"
const { client } = await createOpencode()
```
這會同時啟動服務器和客戶端
#### 選項
|選項 |類型 |描述 |默認 |
| ---------- | ------------- | ------------------------------ | ----------- |
| `hostname` | `string` |服務器主機名 | `127.0.0.1` |
| `port` | `number` |服務器端口 | `4096` |
| `signal` | `AbortSignal` |取消的中止信號 | `undefined` |
| `timeout` | `number` |服務器啟動超時(以毫秒為單位)| `5000` |
| `config` | `Config` |配置對象| `{}` |
---
## 配置
您可以傳遞配置對象來自定義行為。該實例仍然會選擇您的`opencode.json`,但您可以覆蓋或添加內聯配置:
```javascript
import { createOpencode } from "@opencode-ai/sdk"
const opencode = await createOpencode({
hostname: "127.0.0.1",
port: 4096,
config: {
model: "anthropic/claude-3-5-sonnet-20241022",
},
})
console.log(`Server running at ${opencode.server.url}`)
opencode.server.close()
```
## 僅限客戶
如果您已經有一個正在運行的 opencode 實例,您可以創建一個客戶端實例來連接到它:
```javascript
import { createOpencodeClient } from "@opencode-ai/sdk"
const client = createOpencodeClient({
baseUrl: "http://localhost:4096",
})
```
#### 選項
|選項 |類型 |描述 |默認|
| --------------- | ---------- | -------------------------------- | ----------------------- |
| `baseUrl` | `string` |服務器的 URL | `http://localhost:4096` |
| `fetch` | `function` |自定義獲取實現 | `globalThis.fetch` |
| `parseAs` | `string` |響應解析方法 | `auto` |
| `responseStyle` | `string` |返回樣式:`data` 或 `fields` | `fields` |
| `throwOnError` | `boolean` |拋出錯誤而不是返回 | `false` |
---
## 類型
SDK 包含所有 API 類型的 TypeScript 定義。直接導入它們:
```typescript
import type { Session, Message, Part } from "@opencode-ai/sdk"
```
所有類型均根據服務器的 OpenAPI 規範生成,並可在 <a href={typesUrl}>types 文件</a>中找到。
---
## 錯誤
SDK 可能會拋出錯誤,您可以捕獲並處理這些錯誤:
```typescript
try {
await client.session.get({ path: { id: "invalid-id" } })
} catch (error) {
console.error("Failed to get session:", (error as Error).message)
}
```
---
## 蜜蜂
SDK 通過類型安全的客戶端公開所有服務器 API。
---
### 全球的
|方法|描述 |回應 |
| ----------------- | ------------------------------- | ------------------------------------ |
| `global.health()` |檢查服務器健康狀況和版本 | `{ healthy: true, version: string }` |
---
#### 示例
```javascript
const health = await client.global.health()
console.log(health.data.version)
```
---
### 應用程式
|方法|描述 |回應 |
| -------------- | ------------------------- | ------------------------------------------- |
| `app.log()` |寫入日誌條目 | `boolean` |
| `app.agents()` |列出所有可用的代理 | <a href={typesUrl}><code>代理[]</code></a> |
---
#### 示例
```javascript
// Write a log entry
await client.app.log({
body: {
service: "my-app",
level: "info",
message: "Operation completed",
},
})
// List available agents
const agents = await client.app.agents()
```
---
### 專案
|方法|描述 |回應 |
| ------------------- | ------------------- | --------------------------------------------- |
| `project.list()` |列出所有項目 | <a href={typesUrl}><code>項目[]</code></a> |
| `project.current()` |獲取當前項目 | <a href={typesUrl}><code>項目</code></a> |
---
#### 示例
```javascript
// List all projects
const projects = await client.project.list()
// Get current project
const currentProject = await client.project.current()
```
---
### 小路
|方法|描述 |回應 |
| ------------ | ---------------- | ---------------------------------------- |
| `path.get()` |獲取當前路徑 | <a href={typesUrl}><code>路徑</code></a> |
---
#### 示例
```javascript
// Get current path information
const pathInfo = await client.path.get()
```
---
### 配置
|方法|描述 |回應 |
| -------------------- | --------------------------------- | ----------------------------------------------------------------------------------------------------- |
| `config.get()` |獲取配置信息 | <a href={typesUrl}><code>配置</code></a> |
| `config.providers()` |列出提供商和默認模型 | `{ providers: `<a href={typesUrl}><code>Provider[]</code></a>`, default: { [key: string]: string } }` |
---
#### 示例
```javascript
const config = await client.config.get()
const { providers, default: defaults } = await client.config.providers()
```
---
### 會議
|方法|描述 |筆記|
| ---------------------------------------------------------- | ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `session.list()` |列出會話 |返回 <a href={typesUrl}><code>Session[]</code></a> |
| `session.get({ path })` |獲取會話 |返回 <a href={typesUrl}><code>Session</code></a> |
| `session.children({ path })` |列出子會話 |返回 <a href={typesUrl}><code>Session[]</code></a> |
| `session.create({ body })` |創建會話 |返回 <a href={typesUrl}><code>Session</code></a> |
| `session.delete({ path })` |刪除會話 |返回 `boolean` |
| `session.update({ path, body })` |更新會話屬性 |返回 <a href={typesUrl}><code>Session</code></a> |
| `session.init({ path, body })` |分析應用程序並創建`AGENTS.md` |返回 `boolean` |
| `session.abort({ path })` |中止正在運行的會話 |返回 `boolean` |
| `session.share({ path })` |分享會 |返回 <a href={typesUrl}><code>Session</code></a> |
| `session.unshare({ path })` |取消共享會話 |返回 <a href={typesUrl}><code>Session</code></a> |
| `session.summarize({ path, body })` |會議總結 |返回 `boolean` |
| `session.messages({ path })` |列出會話中的消息 |返回 `{ info: `<a href={typesUrl}><code>消息</code></a>`, parts: `<a href={typesUrl}><code>Part[]</code></a>`}[]` |
| `session.message({ path })` |獲取消息詳情 |返回 `{ info: `<a href={typesUrl}><code>消息</code></a>`, parts: `<a href={typesUrl}><code>Part[]</code></a>`}` |
| `session.prompt({ path, body })` |發送提示信息 | `body.noReply: true` 返回 UserMessage僅上下文。默認返回帶有 AI 響應的 <a href={typesUrl}><code>AssistantMessage</code></a> |
| `session.command({ path, body })` |向會話發送命令 |返回 `{ info: `<a href={typesUrl}><code>AssistantMessage</code></a>`, parts: `<a href={typesUrl}><code>Part[]</code></a>`}` |
| `session.shell({ path, body })` |運行 shell 命令 |返回 <a href={typesUrl}><code>AssistantMessage</code></a> |
| `session.revert({ path, body })` |回复消息 |返回 <a href={typesUrl}><code>Session</code></a> |
| `session.unrevert({ path })` |恢復已恢復的消息 |返回 <a href={typesUrl}><code>Session</code></a> |
| `postSessionByIdPermissionsByPermissionId({ path, body })` |回復權限請求 |返回 `boolean` |
---
#### 示例
```javascript
// Create and manage sessions
const session = await client.session.create({
body: { title: "My session" },
})
const sessions = await client.session.list()
// Send a prompt message
const result = await client.session.prompt({
path: { id: session.id },
body: {
model: { providerID: "anthropic", modelID: "claude-3-5-sonnet-20241022" },
parts: [{ type: "text", text: "Hello!" }],
},
})
// Inject context without triggering AI response (useful for plugins)
await client.session.prompt({
path: { id: session.id },
body: {
noReply: true,
parts: [{ type: "text", text: "You are a helpful assistant." }],
},
})
```
---
### 文件
|方法|描述 |回應 |
| ------------------------- | ---------------------------------- | ------------------------------------------------------------------------------------------- |
| `find.text({ query })` |搜索文件中的文本 |具有 `path`、`lines`、`line_number`、`absolute_offset`、`submatches` | 的匹配對像數組 |
| `find.files({ query })` |按名稱查找文件和目錄 | `string[]`(路徑)|
| `find.symbols({ query })` |查找工作區符號 | <a href={typesUrl}><code>符號[]</code></a> |
| `file.read({ query })` |讀取文件 | `{ type: "raw" \| "patch", content: string }` |
| `file.status({ query? })` |獲取跟踪文件的狀態 | <a href={typesUrl}><code>文件[]</code></a> |
`find.files` 支持一些可選的查詢字段:
- `type``"file"`或`"directory"`
- `directory`:覆蓋搜索的項目根目錄
- `limit`:最大結果 (1200)
---
#### 示例
```javascript
// Search and read files
const textResults = await client.find.text({
query: { pattern: "function.*opencode" },
})
const files = await client.find.files({
query: { query: "*.ts", type: "file" },
})
const directories = await client.find.files({
query: { query: "packages", type: "directory", limit: 20 },
})
const content = await client.file.read({
query: { path: "src/index.ts" },
})
```
---
### TUI
|方法|描述 |回應 |
| ------------------------------ | ------------------------- | --------- |
| `tui.appendPrompt({ body })` |將文本附加到提示 | `boolean` |
| `tui.openHelp()` |打開幫助對話框 | `boolean` |
| `tui.openSessions()` |打開會話選擇器 | `boolean` |
| `tui.openThemes()` |打開主題選擇器 | `boolean` |
| `tui.openModels()` |打開模型選擇器 | `boolean` |
| `tui.submitPrompt()` |提交當前提示| `boolean` |
| `tui.clearPrompt()` |清除提示| `boolean` |
| `tui.executeCommand({ body })` |執行命令| `boolean` |
| `tui.showToast({ body })` |顯示 toast 通知 | `boolean` |
---
#### 示例
```javascript
// Control TUI interface
await client.tui.appendPrompt({
body: { text: "Add this to prompt" },
})
await client.tui.showToast({
body: { message: "Task completed", variant: "success" },
})
```
---
### 授權
|方法|描述 |回應 |
| ------------------- | ------------------------------ | --------- |
| `auth.set({ ... })` |設置身份驗證憑據 | `boolean` |
---
#### 示例
```javascript
await client.auth.set({
path: { id: "anthropic" },
body: { type: "api", key: "your-api-key" },
})
```
---
### 活動
|方法|描述 |回應 |
| ------------------- | ------------------------- | ------------------------- |
| `event.subscribe()` |服務器發送的事件流 |服務器發送的事件流 |
---
#### 示例
```javascript
// Listen to real-time events
const events = await client.event.subscribe()
for await (const event of events.stream) {
console.log("Event:", event.type, event.properties)
}
```

View File

@@ -0,0 +1,287 @@
---
title: 伺服器
description: 通過 HTTP 與 opencode 服務器交互。
---
import config from "../../../../config.mjs"
export const typesUrl = `${config.github}/blob/dev/packages/sdk/js/src/gen/types.gen.ts`
`opencode serve` 命令運行一個無頭 HTTP 服務器,該服務器公開開放代碼客戶端可以使用的 OpenAPI 端點。
---
### 用法
```bash
opencode serve [--port <number>] [--hostname <string>] [--cors <origin>]
```
#### 選項
|旗幟|描述 |默認 |
| --------------- | ----------------------------------- | ---------------- |
| `--port` |監聽端口 | `4096` |
| `--hostname` |監聽的主機名 | `127.0.0.1` |
| `--mdns` |啟用 mDNS 發現 | `false` |
| `--mdns-domain` | mDNS 服務的自定義域名 | `opencode.local` |
| `--cors` |允許的其他瀏覽器來源 | `[]` |
`--cors` 可以多次傳遞:
```bash
opencode serve --cors http://localhost:5173 --cors https://app.example.com
```
---
### 驗證
設置`OPENCODE_SERVER_PASSWORD`以使用HTTP基本身份驗證保護服務器。用戶名默認為`opencode`,或設置`OPENCODE_SERVER_USERNAME`來覆蓋它。這適用於`opencode serve` 和`opencode web`。
```bash
OPENCODE_SERVER_PASSWORD=your-password opencode serve
```
---
### 它是如何運作的
當您運行 `opencode` 時,它會啟動 TUI 和服務器。 TUI 是哪裡
與服務器對話的客戶端。服務器公開 OpenAPI 3.1 規範
端點。該端點還用於生成[軟件開發工具包](/docs/sdk)。
:::tip
使用 opencode 服務器以編程方式與 opencode 進行交互。
:::
此架構讓 opencode 支持多個客戶端,並允許您以編程方式與 opencode 進行交互。
您可以運行 `opencode serve` 來啟動獨立服務器。如果您有
opencode TUI 運行,`opencode serve` 將啟動一個新服務器。
---
#### 連接到現有服務器
當您啟動 TUI 時,它會隨機分配端口和主機名。您可以改為傳入`--hostname` 和`--port` [旗幟](/docs/cli)。然後使用它連接到其服務器。
[`/tui`](#tui) 端點可用於通過服務器驅動 TUI。例如您可以預填充或運行提示。此設置由 OpenCode [集成開發環境](/docs/ide) 插件使用。
---
## 規格
服務器發布了 OpenAPI 3.1 規範,可以在以下位置查看:
```
http://<hostname>:<port>/doc
```
例如,`http://localhost:4096/doc`。使用規範生成客戶端或檢查請求和響應類型。或者在 Swagger 瀏覽器中查看它。
---
## 蜜蜂
opencode 服務器公開以下 API。
---
### 全球的
|方法|路徑|描述 |回應 |
| ------ | ---------------- | ------------------------------ | ------------------------------------ |
| `GET` | `/global/health` |獲取服務器運行狀況和版本 | `{ healthy: true, version: string }` |
| `GET` | `/global/event` |獲取全局事件SSE 流)|事件流 |
---
### 專案
|方法|路徑|描述 |回應 |
| ------ | ------------------ | ----------------------- | --------------------------------------------- |
| `GET` | `/project` |列出所有項目 | <a href={typesUrl}><code>項目[]</code></a> |
| `GET` | `/project/current` |獲取當前項目 | <a href={typesUrl}><code>項目</code></a> |
---
### 路徑和VCS
|方法|路徑|描述 |回應 |
| ------ | ------- | ------------------------------------ | ------------------------------------------- |
| `GET` | `/path` |獲取當前路徑| <a href={typesUrl}><code>路徑</code></a> |
| `GET` | `/vcs` |獲取當前項目的 VCS 信息 | <a href={typesUrl}><code>VcsInfo</code></a> |
---
### 實例
|方法|路徑|描述 |回應 |
| ------ | ------------------- | ---------------------------- | --------- |
| `POST` | `/instance/dispose` |處置當前實例 | `boolean` |
---
### 配置
|方法|路徑|描述 |回應 |
| ------- | ------------------- | --------------------------------- | ---------------------------------------------------------------------------------------- |
| `GET` | `/config` |獲取配置信息 | <a href={typesUrl}><code>配置</code></a> |
| `PATCH` | `/config` |更新配置| <a href={typesUrl}><code>配置</code></a> |
| `GET` | `/config/providers` |列出提供商和默認模型 | `{ providers: `<a href={typesUrl}>提供商[]</a>`, default: { [key: string]: string } }` |
---
### 提供者
|方法|路徑|描述 |回應 |
| ------ | -------------------------------- | ------------------------------------ | ----------------------------------------------------------------------------------- |
| `GET` | `/provider` |列出所有提供商 | `{ all: `<a href={typesUrl}>提供商[]</a>`, default: {...}, connected: string[] }` |
| `GET` | `/provider/auth` |獲取提供商身份驗證方法 | `{ [providerID: string]: `<a href={typesUrl}>ProviderAuthMethod[]</a>` }` |
| `POST` | `/provider/{id}/oauth/authorize` |使用 OAuth 授權提供商 | <a href={typesUrl}><code>ProviderAuthAuthorization</code></a> |
| `POST` | `/provider/{id}/oauth/callback` |處理提供商的 OAuth 回調 | `boolean` |
---
### 會議
|方法|路徑|描述 |筆記|
| -------- | ---------------------------------------- | ------------------------------------- | ---------------------------------------------------------------------------------- |
| `GET` | `/session` |列出所有會話 |返回 <a href={typesUrl}><code>Session[]</code></a> |
| `POST` | `/session` |創建新會話 |正文:`{ parentID?, title? }`,返回 <a href={typesUrl}><code>Session</code></a> |
| `GET` | `/session/status` |獲取所有會話的會話狀態 |返回 `{ [sessionID: string]: `<a href={typesUrl}>SessionStatus</a>` }` |
| `GET` | `/session/:id` |獲取會話詳細信息 |返回 <a href={typesUrl}><code>Session</code></a> |
| `DELETE` | `/session/:id` |刪除會話及其所有數據 |返回 `boolean` |
| `PATCH` | `/session/:id` |更新會話屬性 |正文:`{ title? }`,返回 <a href={typesUrl}><code>Session</code></a> |
| `GET` | `/session/:id/children` |獲取會話的子會話 |返回 <a href={typesUrl}><code>Session[]</code></a> |
| `GET` | `/session/:id/todo` |獲取會話的待辦事項列表 |返回 <a href={typesUrl}><code>Todo[]</code></a> |
| `POST` | `/session/:id/init` |分析應用程序並創建`AGENTS.md` |主體:`{ messageID, providerID, modelID }`,返回`boolean` |
| `POST` | `/session/:id/fork` |在消息中分叉現有會話 |正文:`{ messageID? }`,返回 <a href={typesUrl}><code>Session</code></a> |
| `POST` | `/session/:id/abort` |中止正在運行的會話 |返回 `boolean` |
| `POST` | `/session/:id/share` |分享會議 |返回 <a href={typesUrl}><code>Session</code></a> |
| `DELETE` | `/session/:id/share` |取消共享會話 |返回 <a href={typesUrl}><code>Session</code></a> |
| `GET` | `/session/:id/diff` |獲取本次會話的差異 |查詢:`messageID?`,返回 <a href={typesUrl}><code>FileDiff[]</code></a> |
| `POST` | `/session/:id/summarize` |會議總結 |主體:`{ providerID, modelID }`,返回`boolean` |
| `POST` | `/session/:id/revert` |回复消息 |主體:`{ messageID, partID? }`,返回`boolean` |
| `POST` | `/session/:id/unrevert` |恢復所有已恢復的消息 |返回 `boolean` |
| `POST` | `/session/:id/permissions/:permissionID` |回復權限請求 |主體:`{ response, remember? }`,返回`boolean` |
---
### 留言
|方法|路徑|描述 |筆記|
| ------ | --------------------------------- | --------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `GET` | `/session/:id/message` |列出會話中的消息 |查詢:`limit?`,返回`{ info: `<a href={typesUrl}>消息</a>`, parts: `<a href={typesUrl}>Part[]</a>`}[]` |
| `POST` | `/session/:id/message` |發送消息並等待回复 |主體:`{ messageID?, model?, agent?, noReply?, system?, tools?, parts }`,返回`{ info: `<a href={typesUrl}>消息</a>`, parts: `<a href={typesUrl}>部分[]</a>`}` |
| `GET` | `/session/:id/message/:messageID` |獲取消息詳情 |返回`{ info: `<a href={typesUrl}>消息</a>`, parts: `<a href={typesUrl}>部分[]</a>`}` |
| `POST` | `/session/:id/prompt_async` |異步發送消息(無需等待)| body與`/session/:id/message`相同,返回`204 No Content` |
| `POST` | `/session/:id/command` |執行斜杠命令|主體:`{ messageID?, agent?, model?, command, arguments }`,返回`{ info: `<a href={typesUrl}>消息</a>`, parts: `<a href={typesUrl}>部分[]</a>`}` |
| `POST` | `/session/:id/shell` |運行 shell 命令 |主體:`{ agent, model?, command }`,返回`{ info: `<a href={typesUrl}>消息</a>`, parts: `<a href={typesUrl}>部分[]</a>`}` |
---
### 命令
|方法|路徑|描述 |回應 |
| ------ | ---------- | ----------------- | --------------------------------------------- |
| `GET` | `/command` |列出所有命令 | <a href={typesUrl}><code>命令[]</code></a> |
---
### 文件
|方法|路徑|描述 |回應 |
| ------ | ------------------------ | ---------------------------------- | ------------------------------------------------------------------------------------------- |
| `GET` | `/find?pattern=<pat>` |搜索文件中的文本 |具有 `path`、`lines`、`line_number`、`absolute_offset`、`submatches` | 的匹配對像數組 |
| `GET` | `/find/file?query=<q>` |按名稱查找文件和目錄 | `string[]`(路徑)|
| `GET` | `/find/symbol?query=<q>` |查找工作區符號 | <a href={typesUrl}><code>符號[]</code></a> |
| `GET` | `/file?path=<path>` |列出文件和目錄 | <a href={typesUrl}><code>FileNode[]</code></a> |
| `GET` | `/file/content?path=<p>` |讀取文件 | <a href={typesUrl}><code>FileContent</code></a> |
| `GET` | `/file/status` |獲取跟踪文件的狀態 | <a href={typesUrl}><code>文件[]</code></a> |
#### `/find/file`查詢參數
- `query`(必需)- 搜索字符串(模糊匹配)
- `type`(可選)- 將結果限制為`"file"` 或`"directory"`
- `directory` (可選) — 覆蓋搜索的項目根目錄
- `limit`(可選)— 最大結果 (1200)
- `dirs`(可選)- 舊標誌(`"false"` 僅返回文件)
---
### 工具(實驗)
|方法|路徑|描述 |回應 |
| ------ | ------------------------------------------- | ---------------------------------------- | -------------------------------------------- |
| `GET` | `/experimental/tool/ids` |列出所有工具 ID | <a href={typesUrl}><code>ToolID</code></a> |
| `GET` | `/experimental/tool?provider=<p>&model=<m>` |列出具有模型 JSON 架構的工具 | <a href={typesUrl}><code>工具列表</code></a> |
---
### LSP、格式化程序和 MCP
|方法|路徑|描述 |回應 |
| ------ | ------------ | -------------------------- | -------------------------------------------------------- |
| `GET` | `/lsp` |獲取LSP服務器狀態| <a href={typesUrl}><code>LSPStatus[]</code></a> |
| `GET` | `/formatter` |獲取格式化程序狀態 | <a href={typesUrl}><code>FormatterStatus[]</code></a> |
| `GET` | `/mcp` |獲取 MCP 服務器狀態 | `{ [name: string]: `<a href={typesUrl}>MCPStatus</a>` }` |
| `POST` | `/mcp` |動態添加MCP服務器| body: `{ name, config }`, 返回 MCP 狀態對象 |
---
### 代理商
|方法|路徑|描述 |回應 |
| ------ | -------- | ------------------------- | ------------------------------------------- |
| `GET` | `/agent` |列出所有可用的代理 | <a href={typesUrl}><code>代理[]</code></a> |
---
### 記錄
|方法|路徑|描述 |回應 |
| ------ | ------ | ------------------------------------------------------------ | --------- |
| `POST` | `/log` |寫入日誌條目。正文:`{ service, level, message, extra? }` | `boolean` |
---
### TUI
|方法|路徑|描述 |回應 |
| ------ | ----------------------- | ------------------------------------------- | ---------------------- |
| `POST` | `/tui/append-prompt` |將文本附加到提示 | `boolean` |
| `POST` | `/tui/open-help` |打開幫助對話框 | `boolean` |
| `POST` | `/tui/open-sessions` |打開會話選擇器 | `boolean` |
| `POST` | `/tui/open-themes` |打開主題選擇器 | `boolean` |
| `POST` | `/tui/open-models` |打開模型選擇器 | `boolean` |
| `POST` | `/tui/submit-prompt` |提交當前提示| `boolean` |
| `POST` | `/tui/clear-prompt` |清除提示| `boolean` |
| `POST` | `/tui/execute-command` |執行命令(`{ command }`) | `boolean` |
| `POST` | `/tui/show-toast` |顯示祝酒 (`{ title?, message, variant }`) | `boolean` |
| `GET` | `/tui/control/next` |等待下一個控制請求 |控制請求對象|
| `POST` | `/tui/control/response` |響應控制請求 (`{ body }`) | `boolean` |
---
### 授權
|方法|路徑|描述 |回應 |
| ------ | ----------- | --------------------------------------------------------------- | --------- |
| `PUT` | `/auth/:id` |設置身份驗證憑據。正文必須與提供者架構匹配 | `boolean` |
---
### 活動
|方法|路徑|描述 |回應 |
| ------ | -------- | ----------------------------------------------------------------------------- | ------------------------- |
| `GET` | `/event` |服務器發送的事件流。第一個活動是`server.connected`,然後是巴士活動 |服務器發送的事件流 |
---
### 文件
|方法|路徑|描述 |回應 |
| ------ | ------ | ------------------------- | --------------------------- |
| `GET` | `/doc` | OpenAPI 3.1 規範 |具有 OpenAPI 規範的 HTML 頁面 |

View File

@@ -0,0 +1,128 @@
---
title: 分享
description: 分享您的 OpenCode 對話。
---
OpenCode 的共享功能允許您創建 OpenCode 對話的公共鏈接,以便您可以與團隊成員協作或從其他人那裡獲得幫助。
:::note
任何知道鏈接的人都可以公開訪問共享對話。
:::
---
## 它是如何運作的
當您共享對話時OpenCode
1. 為您的會話創建唯一的公共 URL
2. 將您的對話歷史記錄同步到我們的服務器
3. 通過可共享鏈接進行對話 — `opncd.ai/s/<share-id>`
---
## 分享
OpenCode 支持三種控制對話共享方式的共享模式:
---
### 手動(默認)
OpenCode默認使用手動共享模式。會話不會自動共享但您可以使用 `/share` 命令手動共享它們:
```
/share
```
這將生成一個唯一的 URL並將其複製到您的剪貼板。
要在 [配置文件](/docs/config) 中顯式設置手動模式:
```json title="opencode.json"
{
"$schema": "https://opncd.ai/config.json",
"share": "manual"
}
```
---
### 自動分享
您可以通過將[配置文件](/docs/config)中的`share`選項設置為`"auto"`來為所有新對話啟用自動共享:
```json title="opencode.json"
{
"$schema": "https://opncd.ai/config.json",
"share": "auto"
}
```
啟用自動共享後,每個新對話都會自動共享並生成鏈接。
---
### 殘疾人
您可以通過將[配置文件](/docs/config)中的`share`選項設置為`"disabled"`來完全禁用共享:
```json title="opencode.json"
{
"$schema": "https://opncd.ai/config.json",
"share": "disabled"
}
```
要在整個團隊中針對給定項目強制執行此操作,請將其添加到項目中的 `opencode.json` 並簽入 Git。
---
## 取消共享
要停止共享對話並將其從公共訪問中刪除:
```
/unshare
```
這將刪除共享鏈接並刪除與對話相關的數據。
---
## 隱私
分享對話時需要記住一些事項。
---
### 數據保留
共享對話仍然可以訪問,直到您明確取消共享。這
包括:
- 完整的對話歷史記錄
- 所有消息和回复
- 會話元數據
---
### 建議
- 僅共享不包含敏感信息的對話。
- 分享之前查看對話內容。
- 協作完成後取消共享對話。
- 避免與專有代碼或機密數據共享對話。
- 對於敏感項目,完全禁用共享。
---
## 對於企業
對於企業部署,共享功能可以是:
- **出於安全合規性完全禁用**
- **僅限**僅通過 SSO 進行身份驗證的用戶
- **在您自己的基礎設施上自行託管**
[了解更多](/docs/enterprise) 關於在您的組織中使用開放代碼。

View File

@@ -0,0 +1,222 @@
---
title: 《代理技巧》
description: “通過 SKILL.md 定義定義可重用行為”
---
代理技能讓 OpenCode 從您的存儲庫或主目錄中發現可重用的指令。
技能通過本機 `skill` 工具按需加載 - 代理可以查看可用技能並可以在需要時加載完整內容。
---
## 放置文件
每個技能名稱創建一個文件夾,並在其中放入`SKILL.md`。
OpenCode 搜索這些位置:
- 項目配置:`.opencode/skills/<name>/SKILL.md`
- 全局配置:`~/.config/opencode/skills/<name>/SKILL.md`
- 克勞德項目兼容:`.claude/skills/<name>/SKILL.md`
- 全球克勞德兼容:`~/.claude/skills/<name>/SKILL.md`
- 項目代理兼容:`.agents/skills/<name>/SKILL.md`
- 全球代理兼容:`~/.agents/skills/<name>/SKILL.md`
---
## 了解發現
對於項目本地路徑OpenCode 將從當前工作目錄開始,直到到達 git 工作樹。
它加載 `.opencode/` 中任何匹配的 `skills/*/SKILL.md` 以及一路上任何匹配的 `.claude/skills/*/SKILL.md` 或 `.agents/skills/*/SKILL.md`。
全局定義也從`~/.config/opencode/skills/*/SKILL.md`、`~/.claude/skills/*/SKILL.md` 和`~/.agents/skills/*/SKILL.md` 加載。
---
## 寫前言
每個`SKILL.md` 必須以 YAML frontmatter 開頭。
僅識別這些字段:
- `name`(必填)
- `description`(必填)
- `license`(可選)
- `compatibility`(可選)
- `metadata`(可選,字符串到字符串映射)
未知的 frontmatter 字段將被忽略。
---
## 驗證姓名
`name` 必須:
- 長度為 164 個字符
- 為小寫字母數字並帶有單個連字符分隔符
- 不以 `-` 開頭或結尾
- 不包含連續`--`
- 匹配包含`SKILL.md`的目錄名
等效的正則表達式:
```text
^[a-z0-9]+(-[a-z0-9]+)*$
```
---
## 遵循長度規則
`description` 必須是 1-1024 個字符。
保持足夠具體,以便代理能夠正確選擇。
---
## 使用一個例子
像這樣創建`.opencode/skills/git-release/SKILL.md`
```markdown
---
name: git-release
description: Create consistent releases and changelogs
license: MIT
compatibility: opencode
metadata:
audience: maintainers
workflow: github
---
## What I do
- Draft release notes from merged PRs
- Propose a version bump
- Provide a copy-pasteable `gh release create` command
## When to use me
Use this when you are preparing a tagged release.
Ask clarifying questions if the target versioning scheme is unclear.
```
---
## 識別工具說明
OpenCode 在`skill` 工具描述中列出了可用的技能。
每個條目都包含技能名稱和描述:
```xml
<available_skills>
<skill>
<name>git-release</name>
<description>Create consistent releases and changelogs</description>
</skill>
</available_skills>
```
代理通過調用工具來加載技能:
```
skill({ name: "git-release" })
```
---
## 配置權限
使用 `opencode.json` 中基於模式的權限控制客服人員可以訪問哪些技能:
```json
{
"permission": {
"skill": {
"*": "allow",
"pr-review": "allow",
"internal-*": "deny",
"experimental-*": "ask"
}
}
}
```
|許可|行為 |
| ---------- | ----------------------------------------- |
| `allow` |技能立即加載 |
| `deny` |技能對特工隱藏,訪問被拒絕 |
| `ask` |加載前提示用戶批准 |
模式支持通配符:`internal-*` 匹配`internal-docs`、`internal-tools` 等。
---
## 覆蓋每個代理
為特定代理授予與全局默認權限不同的權限。
**對於自定義代理**(在代理前言中):
```yaml
---
permission:
skill:
"documents-*": "allow"
---
```
**對於內置代理**(在 `opencode.json` 中):
```json
{
"agent": {
"plan": {
"permission": {
"skill": {
"internal-*": "allow"
}
}
}
}
}
```
---
## 禁用技能工具
完全禁用不應該使用技能的特工:
**對於定制代理**
```yaml
---
tools:
skill: false
---
```
**對於內置代理**
```json
{
"agent": {
"plan": {
"tools": {
"skill": false
}
}
}
}
```
禁用時,`<available_skills>` 部分將被完全省略。
---
## 解決加載問題
如果某項技能沒有顯示:
1. 驗證 `SKILL.md` 是否全部大寫拼寫
2. 檢查 frontmatter 是否包含 `name` 和 `description`
3. 確保技能名稱在所有位置都是唯一的
4. 檢查權限 - `deny` 的技能對代理隱藏

View File

@@ -0,0 +1,369 @@
---
title: 主題
description: 選擇內置主題或定義您自己的主題。
---
使用 OpenCode您可以從多個內置主題之一中進行選擇使用適合您的終端主題的主題或者定義您自己的自定義主題。
默認情況下OpenCode 使用我們自己的 `opencode` 主題。
---
## 終端要求
為了使主題能夠正確顯示完整的調色板,您的終端必須支持**真彩色**24 位顏色)。大多數現代終端默認支持此功能,但您可能需要啟用它:
- **檢查支持**:運行`echo $COLORTERM` - 它應該輸出`truecolor`或`24bit`
- **啟用真彩色**:在 shell 配置文件中設置環境變量 `COLORTERM=truecolor`
- **終端兼容性**:確保您的終端模擬器支持 24 位顏色(大多數現代終端,​​如 iTerm2、Alacritty、Kitty、Windows 終端和最新版本的 GNOME 終端都支持)
如果沒有真彩色支持,主題的顏色精度可能會降低或回落到最接近的 256 色近似值。
---
## 內置主題
OpenCode 附帶了幾個內置主題。
|名稱 |描述 |
| ---------------------- | ---------------------------------------------------------------------------- |
| `system` |適應您終端的背景顏色 |
| `tokyonight` |基於[東京之夜](https://github.com/folke/tokyonight.nvim)主題 |
| `everforest` |基於[永福森林](https://github.com/sainnhe/everforest)主題 |
| `ayu` |基於[香魚](https://github.com/ayu-theme)深色主題 |
| `catppuccin` |基於[卡普布辛](https://github.com/catppuccin)主題 |
| `catppuccin-macchiato` |基於[卡普布辛](https://github.com/catppuccin)主題 |
| `gruvbox` |基於[礦箱](https://github.com/morhetz/gruvbox)主題 |
| `kanagawa` |基於[神奈川](https://github.com/rebelot/kanagawa.nvim)主題 |
| `nord` |基於[諾德](https://github.com/nordtheme/nord)主題 |
| `matrix` |黑客風格黑底綠主題|
| `one-dark` |基於[原子一號](https://github.com/Th3Whit3Wolf/one-nvim) 深色主題 |
此外,我們還在不斷添加新主題。
---
## 系統主題
`system` 主題旨在自動適應您終端的配色方案。與使用固定顏色的傳統主題不同_system_ 主題:
- **生成灰度**:根據終端的背景顏色創建自定義灰度,確保最佳對比度。
- **使用 ANSI 顏色**:利用標準 ANSI 顏色 (0-15) 進行語法突出顯示和 UI 元素,尊重終端的調色板。
- **保留終端默認設置**:使用 `none` 作為文本和背景顏色,以保持終端的本機外觀。
系統主題適合以下用戶:
- 希望 OpenCode 與其終端的外觀相匹配
- 使用自定義終端配色方案
- 希望所有終端應用程序具有一致的外觀
---
## 使用主題
您可以通過使用 `/theme` 命令調出主題選擇來選擇主題。或者您可以在[配置](/docs/config) 中指定它。
```json title="opencode.json" {3}
{
"$schema": "https://opencode.ai/config.json",
"theme": "tokyonight"
}
```
---
## 自定義主題
OpenCode 支持靈活的基於 JSON 的主題系統,允許用戶輕鬆創建和自定義主題。
---
### 等級制度
主題按以下順序從多個目錄加載,其中後面的目錄覆蓋前面的目錄:
1. **內置主題** - 這些主題嵌入在二進製文件中
2. **用戶配置目錄** - 在`~/.config/opencode/themes/*.json` 或`$XDG_CONFIG_HOME/opencode/themes/*.json` 中定義
3. **項目根目錄** - 定義在`<project-root>/.opencode/themes/*.json`
4. **當前工作目錄** - 在`./.opencode/themes/*.json`中定義
如果多個目錄包含同名主題,則將使用優先級較高的目錄中的主題。
---
### 創建主題
要創建自定義主題,請在主題目錄之一中創建一個 JSON 文件。
對於用戶範圍的主題:
```bash no-frame
mkdir -p ~/.config/opencode/themes
vim ~/.config/opencode/themes/my-theme.json
```
以及針對特定項目的主題。
```bash no-frame
mkdir -p .opencode/themes
vim .opencode/themes/my-theme.json
```
---
### JSON格式
主題使用靈活的 JSON 格式,支持:
- **十六進制顏色**`"#ffffff"`
- **ANSI 顏色**`3` (0-255)
- **顏色參考**`"primary"` 或自定義定義
- **深色/淺色版本**`{"dark": "#000", "light": "#fff"}`
- **無顏色**`"none"` - 使用終端的默認顏色或透明
---
### 顏色定義
`defs` 部分是可選的,它允許您定義可在主題中引用的可重用顏色。
---
### 終端默認值
特殊值`"none"`可用於任何顏色以繼承終端的默認顏色。這對於創建與終端配色方案無縫融合的主題特別有用:
- `"text": "none"` - 使用終端的默認前景色
- `"background": "none"` - 使用終端的默認背景顏色
---
### 例子
以下是自定義主題的示例:
```json title="my-theme.json"
{
"$schema": "https://opencode.ai/theme.json",
"defs": {
"nord0": "#2E3440",
"nord1": "#3B4252",
"nord2": "#434C5E",
"nord3": "#4C566A",
"nord4": "#D8DEE9",
"nord5": "#E5E9F0",
"nord6": "#ECEFF4",
"nord7": "#8FBCBB",
"nord8": "#88C0D0",
"nord9": "#81A1C1",
"nord10": "#5E81AC",
"nord11": "#BF616A",
"nord12": "#D08770",
"nord13": "#EBCB8B",
"nord14": "#A3BE8C",
"nord15": "#B48EAD"
},
"theme": {
"primary": {
"dark": "nord8",
"light": "nord10"
},
"secondary": {
"dark": "nord9",
"light": "nord9"
},
"accent": {
"dark": "nord7",
"light": "nord7"
},
"error": {
"dark": "nord11",
"light": "nord11"
},
"warning": {
"dark": "nord12",
"light": "nord12"
},
"success": {
"dark": "nord14",
"light": "nord14"
},
"info": {
"dark": "nord8",
"light": "nord10"
},
"text": {
"dark": "nord4",
"light": "nord0"
},
"textMuted": {
"dark": "nord3",
"light": "nord1"
},
"background": {
"dark": "nord0",
"light": "nord6"
},
"backgroundPanel": {
"dark": "nord1",
"light": "nord5"
},
"backgroundElement": {
"dark": "nord1",
"light": "nord4"
},
"border": {
"dark": "nord2",
"light": "nord3"
},
"borderActive": {
"dark": "nord3",
"light": "nord2"
},
"borderSubtle": {
"dark": "nord2",
"light": "nord3"
},
"diffAdded": {
"dark": "nord14",
"light": "nord14"
},
"diffRemoved": {
"dark": "nord11",
"light": "nord11"
},
"diffContext": {
"dark": "nord3",
"light": "nord3"
},
"diffHunkHeader": {
"dark": "nord3",
"light": "nord3"
},
"diffHighlightAdded": {
"dark": "nord14",
"light": "nord14"
},
"diffHighlightRemoved": {
"dark": "nord11",
"light": "nord11"
},
"diffAddedBg": {
"dark": "#3B4252",
"light": "#E5E9F0"
},
"diffRemovedBg": {
"dark": "#3B4252",
"light": "#E5E9F0"
},
"diffContextBg": {
"dark": "nord1",
"light": "nord5"
},
"diffLineNumber": {
"dark": "nord2",
"light": "nord4"
},
"diffAddedLineNumberBg": {
"dark": "#3B4252",
"light": "#E5E9F0"
},
"diffRemovedLineNumberBg": {
"dark": "#3B4252",
"light": "#E5E9F0"
},
"markdownText": {
"dark": "nord4",
"light": "nord0"
},
"markdownHeading": {
"dark": "nord8",
"light": "nord10"
},
"markdownLink": {
"dark": "nord9",
"light": "nord9"
},
"markdownLinkText": {
"dark": "nord7",
"light": "nord7"
},
"markdownCode": {
"dark": "nord14",
"light": "nord14"
},
"markdownBlockQuote": {
"dark": "nord3",
"light": "nord3"
},
"markdownEmph": {
"dark": "nord12",
"light": "nord12"
},
"markdownStrong": {
"dark": "nord13",
"light": "nord13"
},
"markdownHorizontalRule": {
"dark": "nord3",
"light": "nord3"
},
"markdownListItem": {
"dark": "nord8",
"light": "nord10"
},
"markdownListEnumeration": {
"dark": "nord7",
"light": "nord7"
},
"markdownImage": {
"dark": "nord9",
"light": "nord9"
},
"markdownImageText": {
"dark": "nord7",
"light": "nord7"
},
"markdownCodeBlock": {
"dark": "nord4",
"light": "nord0"
},
"syntaxComment": {
"dark": "nord3",
"light": "nord3"
},
"syntaxKeyword": {
"dark": "nord9",
"light": "nord9"
},
"syntaxFunction": {
"dark": "nord8",
"light": "nord8"
},
"syntaxVariable": {
"dark": "nord7",
"light": "nord7"
},
"syntaxString": {
"dark": "nord14",
"light": "nord14"
},
"syntaxNumber": {
"dark": "nord15",
"light": "nord15"
},
"syntaxType": {
"dark": "nord7",
"light": "nord7"
},
"syntaxOperator": {
"dark": "nord9",
"light": "nord9"
},
"syntaxPunctuation": {
"dark": "nord4",
"light": "nord0"
}
}
}
```

View File

@@ -0,0 +1,379 @@
---
title: 工具
description: 管理法學碩士可以使用的工具。
---
工具允許 LLM 在您的代碼庫中執行操作。 OpenCode 附帶了一組內置工具,但您可以使用[定制工具](/docs/custom-tools) 或[MCP服務器](/docs/mcp-servers) 對其進行擴展。
默認情況下,所有工具都是**啟用**並且不需要運行權限。您可以通過[權限](/docs/permissions)控制工具行為。
---
## 配置
使用 `permission` 字段控制工具行為。您可以允許、拒絕或要求批准每個工具。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"edit": "deny",
"bash": "ask",
"webfetch": "allow"
}
}
```
您還可以使用通配符同時控制多個工具。例如,要求 MCP 服務器批准所有工具:
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"mymcp_*": "ask"
}
}
```
[了解更多](/docs/permissions) 關於配置權限。
---
## 內建
以下是 OpenCode 中可用的所有內置工具。
---
### bash
在項目環境中執行 shell 命令。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"bash": "allow"
}
}
```
該工具允許法學碩士運行 `npm install`、`git status` 等終端命令或任何其他 shell 命令。
---
### edit
使用精確的字符串替換修改現有文件。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"edit": "allow"
}
}
```
該工具通過替換精確的文本匹配來對文件執行精確編輯。這是法學碩士修改代碼的主要方式。
---
### write
創建新文件或覆蓋現有文件。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"edit": "allow"
}
}
```
使用它允許法學碩士創建新文件。如果現有文件已經存在,它將覆蓋它們。
:::note
`write`工具由`edit`權限控制,該權限涵蓋所有文件修改(`edit`、`write`、`patch`、`multiedit`)。
:::
---
### read
從代碼庫中讀取文件內容。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"read": "allow"
}
}
```
該工具讀取文件並返回其內容。它支持讀取大文件的特定行範圍。
---
### grep
使用正則表達式搜索文件內容。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"grep": "allow"
}
}
```
在您的代碼庫中快速進行內容搜索。支持完整的正則表達式語法和文件模式過濾。
---
### glob
通過模式匹配查找文件。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"glob": "allow"
}
}
```
使用`**/*.js` 或`src/**/*.ts` 等全局模式搜索文件。返回按修改時間排序的匹配文件路徑。
---
### list
列出給定路徑中的文件和目錄。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"list": "allow"
}
}
```
該工具列出目錄內容。它接受全局模式來過濾結果。
---
### lsp實驗性
與您配置的 LSP 服務器交互,以獲得代碼智能功能,例如定義、引用、懸停信息和調用層次結構。
:::note
該工具僅在`OPENCODE_EXPERIMENTAL_LSP_TOOL=true`(或`OPENCODE_EXPERIMENTAL=true`)時可用。
:::
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"lsp": "allow"
}
}
```
支持的操作包括`goToDefinition`、`findReferences`、`hover`、`documentSymbol`、`workspaceSymbol`、`goToImplementation`、`prepareCallHierarchy`、`incomingCalls` 和`outgoingCalls`。
要配置哪些 LSP 服務器可用於您的項目,請參閱[LSP服務器](/docs/lsp)。
---
### patch
對文件應用補丁。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"edit": "allow"
}
}
```
該工具將補丁文件應用到您的代碼庫。對於應用來自各種來源的差異和補丁很有用。
:::note
`patch`工具由`edit`權限控制,該權限涵蓋所有文件修改(`edit`、`write`、`patch`、`multiedit`)。
:::
---
### skill
加載[技能](/docs/skills)`SKILL.md` 文件)並在對話中返回其內容。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"skill": "allow"
}
}
```
---
### todowrite
在編碼會話期間管理待辦事項列表。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"todowrite": "allow"
}
}
```
創建和更新任務列表以跟踪複雜操作期間的進度。法學碩士使用它來組織多步驟任務。
:::note
默認情況下,子代理禁用此工具,但您可以手動啟用它。 [了解更多](/docs/agents/#permissions)
:::
---
### todoread
閱讀現有的待辦事項列表。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"todoread": "allow"
}
}
```
讀取當前待辦事項列表狀態。法學碩士用來跟踪哪些任務待處理或已完成。
:::note
默認情況下,子代理禁用此工具,但您可以手動啟用它。 [了解更多](/docs/agents/#permissions)
:::
---
### webfetch
獲取網頁內容。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"webfetch": "allow"
}
}
```
允許法學碩士獲取和閱讀網頁。對於查找文檔或研究在線資源很有用。
---
### websearch
在網絡上搜索信息。
:::note
僅當使用 OpenCode 提供程序或 `OPENCODE_ENABLE_EXA` 環境變量設置為任何真值(例如 `true` 或 `1`)時,此工具才可用。
要在啟動 OpenCode 時啟用:
```bash
OPENCODE_ENABLE_EXA=1 opencode
```
:::
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"websearch": "allow"
}
}
```
使用 Exa AI 執行網絡搜索以在線查找相關信息。對於研究主題、查找時事或收集超出訓練數據截止範圍的信息很有用。
不需要 API 密鑰 - 該工具無需身份驗證即可直接連接到 Exa AI 的託管 MCP 服務。
:::tip
當您需要查找信息(發現)時,請使用 `websearch`;當您需要從特定 URL 檢索內容(檢索)時,請使用 `webfetch`。
:::
---
### question
在執行過程中詢問用戶問題。
```json title="opencode.json" {4}
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"question": "allow"
}
}
```
該工具允許法學碩士在任務期間詢問用戶問題。它適用於:
- 收集用戶偏好或要求
- 澄清不明確的指令
- 就實施選擇做出決策
- 提供選擇方向
每個問題都包含標題、問題文本和選項列表。用戶可以從提供的選項中進行選擇或輸入自定義答案。當存在多個問題時,用戶可以在提交所有答案之前在這些問題之間導航。
---
## 定制工具
自定義工具可讓您定義法學碩士可以調用的自己的函數。這些是在您的配置文件中定義的並且可以執行任意代碼。
[了解更多](/docs/custom-tools) 關於創建自定義工具。
---
## MCP服務器
MCP模型上下文協議服務器允許您集成外部工具和服務。這包括數據庫訪問、API 集成和第三方服務。
[了解更多](/docs/mcp-servers) 關於配置 MCP 服務器。
---
## 內部結構
在內部,`grep`、`glob` 和`list` 等工具在底層使用[ripgrep](https://github.com/BurntSushi/ripgrep)。默認情況下ripgrep 遵循 `.gitignore` 模式,這意味著 `.gitignore` 中列出的文件和目錄將從搜索和列表中排除。
---
### 忽略模式
要包含通常會被忽略的文件,請在項目根目錄中創建一個 `.ignore` 文件。該文件可以明確允許某些路徑。
```text title=".ignore"
!node_modules/
!dist/
!build/
```
例如,此 `.ignore` 文件允許 ripgrep 在 `node_modules/`、`dist/` 和 `build/` 目錄中搜索,即使它們列在 `.gitignore` 中。

View File

@@ -0,0 +1,300 @@
---
title: 故障排除
description: 常見問題以及如何解決它們。
---
要調試 OpenCode 問題,請首先檢查其存儲在磁盤上的日誌和本地數據。
---
## 紀錄
日誌文件寫入:
- **macOS/Linux**`~/.local/share/opencode/log/`
- **Windows**:按`WIN+R`並粘貼`%USERPROFILE%\.local\share\opencode\log`
日誌文件以時間戳命名(例如`2025-01-09T123456.log`),並保留最近的 10 個日誌文件。
您可以使用 `--log-level` 命令行選項設置日誌級別以獲取更詳細的調試信息。例如,`opencode --log-level DEBUG`。
---
## 貯存
opencode 將會話數據和其他應用程序數據存儲在磁盤上:
- **macOS/Linux**`~/.local/share/opencode/`
- **Windows**:按`WIN+R`並粘貼`%USERPROFILE%\.local\share\opencode`
該目錄包含:
- `auth.json` - 身份驗證數據,例如 API 密鑰、OAuth 令牌
- `log/` - 應用程序日誌
- `project/` - 項目特定數據,例如會話和消息數據
- 如果項目位於 Git 存儲庫中,則它存儲在 `./<project-slug>/storage/` 中
- 如果不是 Git 存儲庫,則存儲在 `./global/storage/` 中
---
## 桌面應用程序
OpenCode Desktop 在後台運行本地 OpenCode 服務器(`opencode-cli` sidecar。大多數問題是由行為不當的插件、損壞的緩存或錯誤的服務器設置引起的。
### 快速檢查
- 完全退出並重新啟動應用程序。
- 如果應用程序顯示錯誤屏幕,請單擊“**重新啟動**”並複制錯誤詳細信息。
- 僅限 macOS`OpenCode` 菜單 -> **重新加載 Webview**(如果 UI 為空白/凍結,則有幫助)。
---
### 禁用插件
如果桌面應用程序在啟動時崩潰、掛起或行為異常,請首先禁用插件。
#### 檢查全局配置
打開全局配置文件並查找 `plugin` 密鑰。
- **macOS/Linux**`~/.config/opencode/opencode.jsonc`(或`~/.config/opencode/opencode.json`
- **macOS/Linux**(較舊的安裝):`~/.local/share/opencode/opencode.jsonc`
- **Windows**:按`WIN+R`並粘貼`%USERPROFILE%\.config\opencode\opencode.jsonc`
如果您配置了插件,請通過刪除密鑰或將其設置為空數組來暫時禁用它們:
```jsonc
{
"$schema": "https://opencode.ai/config.json",
"plugin": [],
}
```
#### 檢查插件目錄
OpenCode 還可以從磁盤加載本地插件。暫時將它們移開(或重命名文件夾)並重新啟動桌面應用程序:
- **全局插件**
- **macOS/Linux**`~/.config/opencode/plugins/`
- **Windows**:按`WIN+R`並粘貼`%USERPROFILE%\.config\opencode\plugins`
- **項目插件**(僅當您使用每個項目配置時)
- `<your-project>/.opencode/plugins/`
如果應用程序再次開始工作,請一次重新啟用一個插件,以找出導致問題的插件。
---
### 清除緩存
如果禁用插件沒有幫助(或者插件安裝被卡住),請清除緩存,以便 OpenCode 可以重建它。
1. 完全退出 OpenCode Desktop。
2. 刪除緩存目錄:
- **macOS**Finder -> `Cmd+Shift+G` -> 粘貼`~/.cache/opencode`
- **Linux**:刪除`~/.cache/opencode`(或運行`rm -rf ~/.cache/opencode`
- **Windows**:按`WIN+R`並粘貼`%USERPROFILE%\.cache\opencode`
3. 重新啟動 OpenCode 桌面。
---
### 修復服務器連接問題
OpenCode Desktop 可以啟動自己的本地服務器(默認)或連接到您配置的服務器 URL。
如果您看到 **“連接失敗”** 對話框(或者應用程序永遠無法通過啟動屏幕),請檢查自定義服務器 URL。
#### 清除桌面默認服務器 URL
在主屏幕中,單擊服務器名稱(帶有狀態點)以打開服務器選取器。在“**默認服務器**”部分中,單擊“**清除**”。
#### 從您的配置中刪除`server.port` / `server.hostname`
如果您的 `opencode.json(c)` 包含 `server` 部分,請將其暫時刪除並重新啟動桌面應用程序。
#### 檢查環境變量
如果您在環境中設置了 `OPENCODE_PORT`,桌面應用程序將嘗試將該端口用於本地服務器。
- 取消設置`OPENCODE_PORT`(或選擇一個空閒端口)並重新啟動。
---
### LinuxWayland / X11 問題
在 Linux 上,某些 Wayland 設置可能會導致空白窗口或合成器錯誤。
- 如果您在 Wayland 上且應用程序空白/崩潰,請嘗試使用 `OC_ALLOW_WAYLAND=1` 啟動。
- 如果這讓事情變得更糟,請將其刪除並嘗試在 X11 會話下啟動。
---
### WindowsWebView2 運行時
在 Windows 上OpenCode Desktop 需要 Microsoft Edge **WebView2 運行時**。如果應用程序打開為空白窗口或無法啟動,請安裝/更新 WebView2然後重試。
---
### Windows一般性能問題
如果您在 Windows 上遇到性能緩慢、文件訪問問題或終端問題,請嘗試使用[WSL適用於 Linux 的 Windows 子系統)](/docs/windows-wsl)。 WSL 提供了一個可以與 OpenCode 功能更加無縫協作的 Linux 環境。
---
### 通知不顯示
OpenCode Desktop 僅在以下情況下顯示系統通知:
- 在您的操作系統設置中啟用 OpenCode 通知,並且
- 應用程序窗口未聚焦。
---
### 重置桌面應用程序存儲(最後的手段)
如果應用程序無法啟動並且您無法從 UI 內部清除設置,請重置桌面應用程序的保存狀態。
1. 退出 OpenCode Desktop。
2. 查找並刪除這些文件(它們位於 OpenCode Desktop 應用程序數據目錄中):
- `opencode.settings.dat`(桌面默認服務器 URL
- `opencode.global.dat` 和 `opencode.workspace.*.dat`UI 狀態,如最近的服務器/項目)
快速找到目錄:
- **macOS**Finder -> `Cmd+Shift+G` -> `~/Library/Application Support`(然後搜索上面的文件名)
- **Linux**:在 `~/.local/share` 下搜索上述文件名
- **Windows**:按`WIN+R` -> `%APPDATA%`(然後搜索上面的文件名)
---
## 尋求幫助
如果您遇到 OpenCode 問題:
1. **在 GitHub 上報告問題**
報告錯誤或請求功能的最佳方式是通過我們的 GitHub 存儲庫:
[**github.com/anomalyco/opencode/issues**](https://github.com/anomalyco/opencode/issues)
在創建新問題之前,請搜索現有問題以查看您的問題是否已被報告。
2. **加入我們的不和諧**
如需實時幫助和社區討論,請加入我們的 Discord 服務器:
[**opencode.ai/discord**](https://opencode.ai/discord)
---
## 常見問題
以下是一些常見問題以及解決方法。
---
### OpenCode 無法啟動
1. 檢查日誌中是否有錯誤消息
2. 嘗試使用 `--print-logs` 運行以查看終端中的輸出
3. 確保您擁有最新版本`opencode upgrade`
---
### 身份驗證問題
1. 嘗試使用 TUI 中的 `/connect` 命令重新進行身份驗證
2. 檢查您的 API 密鑰是否有效
3. 確保您的網絡允許連接到提供商的 API
---
### 型號不可用
1. 檢查您是否已通過提供商的身份驗證
2. 驗證配置中的型號名稱是否正確
3. 某些型號可能需要特定的訪問權限或訂閱
如果您遇到`ProviderModelNotFoundError`,您很可能是錯誤的
在某處引用模型。
模型應該像這樣引用:`<providerId>/<modelId>`
示例:
- `openai/gpt-4.1`
- `openrouter/google/gemini-2.5-flash`
- `opencode/kimi-k2`
要了解您可以訪問哪些模型,請運行`opencode models`
---
### 提供者初始化錯誤
如果遇到 ProviderInitError您的配置可能無效或損壞。
要解決這個問題:
1. 首先,按照 [供應商指南](/docs/providers) 驗證您的提供商是否已正確設置
2. 如果問題仍然存在,請嘗試清除存儲的配置:
```bash
rm -rf ~/.local/share/opencode
```
在 Windows 上,按 `WIN+R` 並刪除:`%USERPROFILE%\.local\share\opencode`
3. 使用 TUI 中的 `/connect` 命令向您的提供商重新進行身份驗證。
---
### AI_APICallError 和提供程序包問題
如果您遇到 API 調用錯誤,這可能是由於過時的提供程序包造成的。 opencode 根據需要動態安裝提供程序包OpenAI、Anthropic、Google 等)並將其緩存在本地。
要解決提供程序包問題:
1. 清除提供程序包緩存:
```bash
rm -rf ~/.cache/opencode
```
在 Windows 上,按 `WIN+R` 並刪除:`%USERPROFILE%\.cache\opencode`
2. 重新啟動 opencode 以重新安裝最新的提供程序包
這將強制 opencode 下載最新版本的提供程序包,這通常可以解決模型參數和 API 更改的兼容性問題。
---
### 複製/粘貼在 Linux 上不起作用
Linux 用戶需要安裝以下剪貼板實用程序之一才能使用複制/粘貼功能:
**對於 X11 系統:**
```bash
apt install -y xclip
# or
apt install -y xsel
```
**對於 Wayland 系統:**
```bash
apt install -y wl-clipboard
```
**對於無頭環境:**
```bash
apt install -y xvfb
# and run:
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
export DISPLAY=:99.0
```
opencode 將檢測您是否使用 Wayland 並更喜歡 `wl-clipboard`,否則它將嘗試按 `xclip` 和 `xsel` 的順序查找剪貼板工具。

View File

@@ -0,0 +1,390 @@
---
title: 途易
description: 使用 OpenCode 終端用戶界面。
---
import { Tabs, TabItem } from "@astrojs/starlight/components"
OpenCode 提供了一個交互式終端界面或 TUI供您與法學碩士一起處理項目。
運行 OpenCode 會啟動當前目錄的 TUI。
```bash
opencode
```
或者您可以為特定的工作目錄啟動它。
```bash
opencode /path/to/project
```
進入 TUI 後,您可以通過消息進行提示。
```text
Give me a quick summary of the codebase.
```
---
## 文件參考
您可以使用 `@` 在消息中引用文件。這會在當前工作目錄中進行模糊文件搜索。
:::tip
您還可以使用 `@` 來引用消息中的文件。
:::
```text "@packages/functions/src/api/index.ts"
How is auth handled in @packages/functions/src/api/index.ts?
```
文件的內容會自動添加到對話中。
---
## bash 命令
以 `!` 開始一條消息以運行 shell 命令。
```bash frame="none"
!ls -la
```
命令的輸出將作為工具結果添加到對話中。
---
## 命令
使用 OpenCode TUI 時,您可以鍵入 `/` 後跟命令名稱來快速執行操作。例如:
```bash frame="none"
/help
```
Most commands also have keybind using `ctrl+x` as the leader key, where `ctrl+x` is the default leader key. [了解更多](/docs/keybinds)。
以下是所有可用的斜杠命令:
---
### connect
將提供程序添加到 OpenCode。允許您從可用的提供商中進行選擇並添加其 API 密鑰。
```bash frame="none"
/connect
```
---
### compact
壓縮當前會話。 _別名_`/summarize`
```bash frame="none"
/compact
```
**按鍵綁定:** `ctrl+x c`
---
### details
切換工具執行詳細信息。
```bash frame="none"
/details
```
**按鍵綁定:** `ctrl+x d`
---
### editor
打開外部編輯器來撰寫消息。使用 `EDITOR` 環境變量中設置的編輯器。 [了解更多](#editor-setup)。
```bash frame="none"
/editor
```
**按鍵綁定:** `ctrl+x e`
---
### exit
退出 OpenCode。 _別名_`/quit`、`/q`
```bash frame="none"
/exit
```
**按鍵綁定:** `ctrl+x q`
---
### export
將當前對話導出到 Markdown 並在默認編輯器中打開。使用 `EDITOR` 環境變量中設置的編輯器。 [了解更多](#editor-setup)。
```bash frame="none"
/export
```
**按鍵綁定:** `ctrl+x x`
---
### help
顯示幫助對話框。
```bash frame="none"
/help
```
**按鍵綁定:** `ctrl+x h`
---
### init
創建或更新`AGENTS.md` 文件。 [了解更多](/docs/rules)。
```bash frame="none"
/init
```
**按鍵綁定:** `ctrl+x i`
---
### models
列出可用型號。
```bash frame="none"
/models
```
**按鍵綁定:** `ctrl+x m`
---
### new
開始新的會話。 _別名_`/clear`
```bash frame="none"
/new
```
**按鍵綁定:** `ctrl+x n`
---
### redo
重做之前撤消的消息。僅在使用`/undo`後可用。
:::tip
任何文件更改也將被恢復。
:::
在內部,這使用 Git 來管理文件更改。所以你的項目**需要
是一個 Git 存儲庫**。
```bash frame="none"
/redo
```
**按鍵綁定:** `ctrl+x r`
---
### sessions
列出會話並在會話之間切換。 _別名_`/resume`、`/continue`
```bash frame="none"
/sessions
```
**按鍵綁定:** `ctrl+x l`
---
### share
共享當前會話。 [了解更多](/docs/share)。
```bash frame="none"
/share
```
**按鍵綁定:** `ctrl+x s`
---
### themes
列出可用的主題。
```bash frame="none"
/theme
```
**按鍵綁定:** `ctrl+x t`
---
### thinking
切換對話中思維/推理塊的可見性。啟用後,您可以看到支持擴展思維的模型的推理過程。
:::note
該命令僅控制是否**顯示** - 它不啟用或禁用模型的推理功能。要切換實際推理功能,請使用 `ctrl+t` 循環切換模型變體。
:::
```bash frame="none"
/thinking
```
---
### undo
撤消對話中的最後一條消息。刪除最近的用戶消息、所有後續響應以及任何文件更改。
:::tip
所做的任何文件更改也將被恢復。
:::
在內部,這使用 Git 來管理文件更改。所以你的項目**需要
是一個 Git 存儲庫**。
```bash frame="none"
/undo
```
**按鍵綁定:** `ctrl+x u`
---
### unshare
取消共享當前會話。 [了解更多](/docs/share#un-sharing)。
```bash frame="none"
/unshare
```
---
## 編輯器設置
`/editor` 和`/export` 命令都使用`EDITOR` 環境變量中指定的編輯器。
<Tabs>
<TabItem label="Linux/macOS">
```bash
# Example for nano or vim
export EDITOR=nano
export EDITOR=vim
# For GUI editors, VS Code, Cursor, VSCodium, Windsurf, Zed, etc.
# include --wait
export EDITOR="code --wait"
```
To make it permanent, add this to your shell profile;
`~/.bashrc`, `~/.zshrc`, etc.
</TabItem>
<TabItem label="Windows (CMD)">
```bash
set EDITOR=notepad
# For GUI editors, VS Code, Cursor, VSCodium, Windsurf, Zed, etc.
# include --wait
set EDITOR=code --wait
```
To make it permanent, use **System Properties** > **Environment
Variables**.
</TabItem>
<TabItem label="Windows (PowerShell)">
```powershell
$env:EDITOR = "notepad"
# For GUI editors, VS Code, Cursor, VSCodium, Windsurf, Zed, etc.
# include --wait
$env:EDITOR = "code --wait"
```
To make it permanent, add this to your PowerShell profile.
</TabItem>
</Tabs>
流行的編輯器選項包括:
- `code` - Visual Studio 代碼
- `cursor` - 光標
- `windsurf` - 風帆衝浪
- `nvim` - Neo 的編輯
- `vim` - 我來編輯了
- `nano` - 納米編輯器
- `notepad` - Windows 記事本
- `subl` - Sublime Text
:::note
某些編輯器(例如 VS Code需要以 `--wait` 標誌啟動。
:::
某些編輯器需要命令行參數才能在阻止模式下運行。 `--wait` 標誌使編輯器進程阻塞直到關閉。
---
## 配置
您可以通過 OpenCode 配置文件自定義 TUI 行為。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"tui": {
"scroll_speed": 3,
"scroll_acceleration": {
"enabled": true
}
}
}
```
### 選項
- `scroll_acceleration` - 啟用 macOS 風格的滾動加速,實現平滑、自然的滾動。啟用後,滾動速度會隨著快速滾動手勢而增加,並在較慢的移動時保持精確。 **此設置優先於`scroll_speed`,並在啟用時覆蓋它。 **
- `scroll_speed` - 控制使用滾動命令時 TUI 滾動的速度(最小值:`1`)。默認為`3`。 **注意:如果`scroll_acceleration.enabled` 設置為`true`,則忽略此設置。 **
---
## 定制化
您可以使用命令選項板(`ctrl+x h` 或`/help`)自定義 TUI 視圖的各個方面。這些設置在重新啟動後仍然存在。
---
#### 用戶名顯示
切換您的用戶名是否出現在聊天消息中。通過以下方式訪問:
- 命令面板:搜索“用戶名”或“隱藏用戶名”
- 該設置會自動保留並在 TUI 會話中被記住

View File

@@ -0,0 +1,142 @@
---
title: 網絡
description: 在瀏覽器中使用 OpenCode。
---
OpenCode 可以在瀏覽器中作為 Web 應用程序運行,無需終端即可提供同樣強大的 AI 編碼體驗。
![OpenCode Web - 新會話](../../../assets/web/web-homepage-new-session.png)
## 入門
通過運行以下命令啟動 Web 界面:
```bash
opencode web
```
這將在 `127.0.0.1` 上啟動一個具有隨機可用端口的本地服務器,並自動在默認瀏覽器中打開 OpenCode。
:::caution
如果未設置`OPENCODE_SERVER_PASSWORD`,服務器將不安全。這對於本地使用來說很好,但應該針對網絡訪問進行設置。
:::
:::tip[Windows 用戶]
為了獲得最佳體驗,請從 [世界SL](/docs/windows-wsl) 而不是 PowerShell 運行 `opencode web`。這確保了正確的文件系統訪問和終端集成。
:::
---
## 配置
您可以使用命令行標誌或在 [配置文件](/docs/config) 中配置 Web 服務器。
### 港口
默認情況下OpenCode 選擇一個可用端口。您可以指定一個端口:
```bash
opencode web --port 4096
```
### 主機名
默認情況下,服務器綁定到`127.0.0.1`(僅限本地主機)。要使 OpenCode 在您的網絡上可訪問:
```bash
opencode web --hostname 0.0.0.0
```
當使用`0.0.0.0`時OpenCode將顯示本地地址和網絡地址
```
Local access: http://localhost:4096
Network access: http://192.168.1.100:4096
```
### mDNS 發現
啟用 mDNS 以使您的服務器在本地網絡上可發現:
```bash
opencode web --mdns
```
這會自動將主機名設置為 `0.0.0.0` 並將服務器通告為 `opencode.local`。
您可以自定義 mDNS 域名以在同一網絡上運行多個實例:
```bash
opencode web --mdns --mdns-domain myproject.local
```
### CORS
允許 CORS 的其他域(對於自定義前端有用):
```bash
opencode web --cors https://example.com
```
### 驗證
要保護訪問,請使用 `OPENCODE_SERVER_PASSWORD` 環境變量設置密碼:
```bash
OPENCODE_SERVER_PASSWORD=secret opencode web
```
用戶名默認為`opencode`,但可以使用`OPENCODE_SERVER_USERNAME` 進行更改。
---
## 使用網絡界面
啟動後Web 界面將提供對 OpenCode 會話的訪問。
### 會議
從主頁查看和管理您的會話。您可以查看活動會話並開始新會話。
![OpenCode Web - 活動會話](../../../assets/web/web-homepage-active-session.png)
### 服務器狀態
單擊“查看服務器”可查看連接的服務器及其狀態。
![OpenCode Web - 查看服務器](../../../assets/web/web-homepage-see-servers.png)
---
## 連接終端
您可以將終端 TUI 連接到正在運行的 Web 服務器:
```bash
# Start the web server
opencode web --port 4096
# In another terminal, attach the TUI
opencode attach http://localhost:4096
```
這允許您同時使用 Web 界面和終端,共享相同的會話和狀態。
---
## 配置文件
您還可以在 `opencode.json` 配置文件中配置服務器設置:
```json
{
"server": {
"port": 4096,
"hostname": "0.0.0.0",
"mdns": true,
"cors": ["https://example.com"]
}
}
```
命令行標誌優先於配置文件設置。

View File

@@ -0,0 +1,113 @@
---
title: Windows (WSL)
description: 在 Windows 透過 WSL 使用 OpenCode。
---
import { Steps } from "@astrojs/starlight/components"
雖然 OpenCode 可以直接在 Windows 上執行,但為了獲得最佳體驗,我們建議使用 [Windows Subsystem for Linux (WSL)](https://learn.microsoft.com/en-us/windows/wsl/install)。WSL 提供了可與 OpenCode 功能順暢配合的 Linux 環境。
:::tip[為什麼要用 WSL]
WSL 提供更好的檔案系統效能、完整的終端支援,以及與 OpenCode 依賴之開發工具的相容性。
:::
---
## 設定
<Steps>
1. **安裝 WSL**
如果你尚未安裝,請依照 Microsoft 官方指南[安裝 WSL](https://learn.microsoft.com/en-us/windows/wsl/install)。
2. **在 WSL 中安裝 OpenCode**
完成 WSL 設定後,打開 WSL 終端機,並使用其中一種[安裝方式](/docs/)安裝 OpenCode。
```bash
curl -fsSL https://opencode.ai/install | bash
```
3. **從 WSL 使用 OpenCode**
移動到你的專案目錄(可透過 `/mnt/c/`、`/mnt/d/` 等路徑存取 Windows 檔案),然後執行 OpenCode。
```bash
cd /mnt/c/Users/YourName/project
opencode
```
</Steps>
---
## 桌面應用程式 + WSL 伺服器
如果你偏好使用 OpenCode 桌面應用程式,但希望在 WSL 內執行伺服器:
1. **在 WSL 中啟動伺服器**,並使用 `--hostname 0.0.0.0` 允許外部連線:
```bash
opencode serve --hostname 0.0.0.0 --port 4096
```
2. **將桌面應用程式連線到** `http://localhost:4096`
:::note
若你的環境中 `localhost` 無法使用,請改用 WSL 的 IP 位址連線(在 WSL 執行:`hostname -I`),並使用 `http://<wsl-ip>:4096`。
:::
:::caution
使用 `--hostname 0.0.0.0` 時,請設定 `OPENCODE_SERVER_PASSWORD` 來保護伺服器。
```bash
OPENCODE_SERVER_PASSWORD=your-password opencode serve --hostname 0.0.0.0
```
:::
---
## Web 用戶端 + WSL
在 Windows 上取得最佳 Web 體驗:
1. **請在 WSL 終端機執行 `opencode web`**,而不是在 PowerShell 執行:
```bash
opencode web --hostname 0.0.0.0
```
2. **在 Windows 瀏覽器中開啟** `http://localhost:<port>`OpenCode 會輸出該 URL
從 WSL 執行 `opencode web` 可確保正確的檔案系統存取與終端整合,同時仍可由 Windows 瀏覽器使用。
---
## 存取 Windows 檔案
WSL 可透過 `/mnt/` 目錄存取你所有的 Windows 檔案:
- `C:` drive → `/mnt/c/`
- `D:` drive → `/mnt/d/`
- 其他磁碟機也相同
範例:
```bash
cd /mnt/c/Users/YourName/Documents/project
opencode
```
:::tip
為了更流暢的使用體驗,建議將你的儲存庫 clone 或複製到 WSL 檔案系統(例如 `~/code/`)中,再從那裡執行 OpenCode。
:::
---
## 提示
- 即使專案存放在 Windows 磁碟機上,也建議在 WSL 中執行 OpenCode檔案存取會更順暢
- 可將 OpenCode 與 VS Code 的 [WSL 擴充套件](https://code.visualstudio.com/docs/remote/wsl)搭配使用,建立整合式開發流程
- OpenCode 的設定與工作階段會儲存在 WSL 環境中的 `~/.local/share/opencode/`

View File

@@ -0,0 +1,254 @@
---
title: 禪
description: OpenCode 提供的精選模型列表。
---
import config from "../../../../config.mjs"
export const console = config.console
export const email = `mailto:${config.email}`
OpenCode Zen 是 OpenCode 團隊提供的經過測試和驗證的模型列表。
:::note
OpenCode Zen 目前處於測試階段。
:::
Zen 的工作方式與 OpenCode 中的任何其他提供程序一樣。您登錄 OpenCode Zen 並獲取
您的 API 密鑰。它是**完全可選的**,您不需要使用它即可使用
開放代碼。
---
## 背景
市面上有很多型號,但其中只有少數幾個
這些模型可以很好地用作編碼代理。此外,大多數提供商都
配置非常不同;所以你會得到截然不同的性能和質量。
:::tip
我們測試了一組精選的與 OpenCode 配合良好的模型和提供程序。
:::
因此,如果您通過 OpenRouter 之類的東西使用模型,那麼您永遠無法
確定您是否獲得了您想要的模型的最佳版本。
為了解決這個問題,我們做了幾件事:
1. 我們測試了一組選定的模型,並與他們的團隊討論瞭如何
最好運行它們。
2. 然後我們與一些提供商合作以確保這些服務得到服務
正確。
3. 最後,我們對模型/提供商的組合進行了基準測試並提出了
並附上一份我們覺得不錯的推薦清單。
OpenCode Zen 是一個 AI 網關,可讓您訪問這些模型。
---
## 它是如何運作的
OpenCode Zen 的工作方式與 OpenCode 中的任何其他提供程序一樣。
1. 您登錄 **<a href={console}>OpenCode Zen</a>**,添加您的帳單
詳細信息,然後復制您的 API 密鑰。
2. 您在 TUI 中運行 `/connect` 命令,選擇 OpenCode Zen然後粘貼您的 API 密鑰。
3. 在 TUI 中運行 `/models` 以查看我們推薦的型號列表。
您需要按請求付費,並且可以將積分添加到您的帳戶中。
---
## 端點
您還可以通過以下 API 端點訪問我們的模型。
|型號|型號 ID |端點 |人工智能SDK包|
| ------------------ | ------------------ | -------------------------------------------------- | --------------------------- |
| GPT 5.2 | gpt-5.2 | `https://opencode.ai/zen/v1/responses` | `@ai-sdk/openai` |
| GPT 5.2 法典 | gpt-5.2-codex | gpt-5.2-codex | `https://opencode.ai/zen/v1/responses` | `@ai-sdk/openai` |
| GPT 5.1 | gpt-5.1 | `https://opencode.ai/zen/v1/responses` | `@ai-sdk/openai` |
| GPT 5.1 法典 | gpt-5.1-codex | gpt-5.1-codex | `https://opencode.ai/zen/v1/responses` | `@ai-sdk/openai` |
| GPT 5.1 法典最大 | gpt-5.1-codex-max | gpt-5.1-codex-max | `https://opencode.ai/zen/v1/responses` | `@ai-sdk/openai` |
| GPT 5.1 迷你版 | gpt-5.1-codex-mini | gpt-5.1-codex-mini | gpt-5.1-codex-mini `https://opencode.ai/zen/v1/responses` | `@ai-sdk/openai` |
| GPT 5 | gpt-5 | `https://opencode.ai/zen/v1/responses` | `@ai-sdk/openai` |
| GPT 5 法典 | gpt-5-法典 | `https://opencode.ai/zen/v1/responses` | `@ai-sdk/openai` |
| GPT 5 納米 | GPT-5-納米| @@c0@@ | @@c1@ |
|克勞德十四行詩 4.5 |克勞德十四行詩-4-5 | `https://opencode.ai/zen/v1/messages` | `@ai-sdk/anthropic` |
|克勞德十四行詩 4 |克勞德十四行詩-4 | `https://opencode.ai/zen/v1/messages` | `@ai-sdk/anthropic` |
|克勞德俳句 4.5 |克勞德俳句-4-5 | `https://opencode.ai/zen/v1/messages` | `@ai-sdk/anthropic` |
|克勞德俳句 3.5 | claude-3-5-俳句 | `https://opencode.ai/zen/v1/messages` | `@ai-sdk/anthropic` |
|近距離工作4.6 |近距離工作4-6 | `https://opencode.ai/zen/v1/messages` | `@ai-sdk/anthropic` |
|近距離工作4.5 |近距離工作4-5 | `https://opencode.ai/zen/v1/messages` | `@ai-sdk/anthropic` |
|近距離工作4.1 |近距離工作4-1 | `https://opencode.ai/zen/v1/messages` | `@ai-sdk/anthropic` |
|雙子座 3 Pro |雙 3 為 | `https://opencode.ai/zen/v1/models/gemini-3-pro` | `@ai-sdk/google` |
|雙子座3閃光|雙子座 3 閃光 | `https://opencode.ai/zen/v1/models/gemini-3-flash` | `@ai-sdk/google` |
|迷你最大M2.1 |極小極大-m2.1 | `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
| MiniMax M2.1 免費 | minimax-m2.1-免費 | `https://opencode.ai/zen/v1/messages` | `@ai-sdk/anthropic` |
| GLM 4.7 | glm-4.7 | `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
| GLM 4.7 免費 | glm-4.7-免費 | `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
| GLM 4.6 | glm-4.6 | `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
|作為K2.5| as-k2.5 | `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
| Kimi K2.5 免費 | kimi-k2.5-免費 | `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
| Kimi K2 思考 | kimi-k2-思考 | kimi-k2-thinking | kimi-k2-thinking `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
|作為K2 | as-k2 | `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
| Qwen3 編碼器 480B | qwen3-編碼器 | `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
|大泡菜|大泡菜| `https://opencode.ai/zen/v1/chat/completions` | `@ai-sdk/openai-compatible` |
OpenCode 配置中的 [型號編號](/docs/config/#models)
使用格式`opencode/<model-id>`。例如,對於 GPT 5.2 Codex您將
在您的配置中使用`opencode/gpt-5.2-codex`。
---
### 型號
您可以從以下位置獲取可用模型及其元數據的完整列表:
```
https://opencode.ai/zen/v1/models
```
---
## 定價
我們支持即用即付模式。以下是**每 100 萬代幣的價格**。
|型號|輸入 |輸出|緩存讀取 |緩存寫入|
| --------------------------------- | ------ | ------ | ----------- | ------------ |
|大泡菜|免費|免費|免費| - |
| MiniMax M2.1 免費 |免費|免費|免費| - |
|迷你最大M2.1 | 0.30 美元 | 1.20 美元 | 0.10 美元 | - |
| GLM 4.7 免費 |免費|免費|免費| - |
| GLM 4.7 | 0.60 美元 | 2.20 美元 | 0.10 美元 | - |
| GLM 4.6 | 0.60 美元 | 2.20 美元 | 0.10 美元 | - |
| Kimi K2.5 免費 |免費|免費|免費| - |
|作為K2.5| 0.60 美元 | $3.00 | 0.08 美元 | - |
| Kimi K2 思考 | 0.40 美元 | 2.50 美元 | - | - |
|作為K2 | 0.40 美元 | 2.50 美元 | - | - |
| Qwen3 編碼器 480B | 0.45 美元 | 1.50 美元 | - | - |
|克勞德十四行詩 4.5(≤ 200K 代幣)| $3.00 | 15.00 美元 | 0.30 美元 | 3.75 美元 |
| Claude Sonnet 4.5> 200K 代幣)| 6.00 美元 | 22.50 美元 | 0.60 美元 | 7.50 美元 |
|克勞德十四行詩 4≤ 200K 代幣)| $3.00 | 15.00 美元 | 0.30 美元 | 3.75 美元 |
|克勞德十四行詩 4> 200K 代幣)| 6.00 美元 | 22.50 美元 | 0.60 美元 | 7.50 美元 |
|克勞德俳句 4.5 | 1.00 美元 | 5.00 美元 | 0.10 美元 | 1.25 美元 |
|克勞德俳句 3.5 | 0.80 美元 | 4.00 美元 | 0.08 美元 | 1.00 美元 |
| Claude Opus 4.6(≤ 200K 代幣)| 5.00 美元 | 25.00 美元 | 0.50 美元 | 6.25 美元 |
| Claude Opus 4.6> 200K 代幣)| 10.00 美元 | 37.50 美元 | 1.00 美元 | 12.50 美元 |
|近距離工作4.5 | 5.00 美元 | 25.00 美元 | 0.50 美元 | 6.25 美元 |
|近距離工作4.1 | 15.00 美元 | 75.00 美元 | 1.50 美元 | 18.75 美元 |
| Gemini 3 Pro≤ 200K 代幣)| 2.00 美元 | 12.00 美元 | 0.20 美元 | - |
| Gemini 3 Pro> 200K 代幣)| 4.00 美元 | 18.00 美元 | 0.40 美元 | - |
|雙子座3閃光| 0.50 美元 | $3.00 | 0.05 美元 | - |
| GPT 5.2 | 1.75 美元 | 14.00 美元 | 0.175 美元 | - |
| GPT 5.2 法典 | 1.75 美元 | 14.00 美元 | 0.175 美元 | - |
| GPT 5.1 | 1.07 美元 | 8.50 美元 | 0.107 美元 | - |
| GPT 5.1 法典 | 1.07 美元 | 8.50 美元 | 0.107 美元 | - |
| GPT 5.1 法典最大 | 1.25 美元 | 10.00 美元 | 0.125 美元 | - |
| GPT 5.1 迷你版 | 0.25 美元 | 2.00 美元 | 0.025 美元 | - |
| GPT 5 | 1.07 美元 | 8.50 美元 | 0.107 美元 | - |
| GPT 5 法典 | 1.07 美元 | 8.50 美元 | 0.107 美元 | - |
| GPT 5 納米 |免費|免費|免費| - |
您可能會在使用歷史記錄中註意到_Claude Haiku 3.5_。這是一個[低成本模式](/docs/config/#models),用於生成會話標題。
:::note
信用卡費用按成本轉嫁4.4% + 每筆交易 0.30 美元);除此之外我們不收取任何費用。
:::
免費模型:
- GLM 4.7 免費版在 OpenCode 上限時提供。團隊正在利用這段時間收集反饋並改進模型。
- Kimi K2.5 Free 在 OpenCode 上限時提供。團隊正在利用這段時間收集反饋並改進模型。
- MiniMax M2.1 免費版在 OpenCode 上限時提供。團隊正在利用這段時間收集反饋並改進模型。
- Big Pickle 是一個秘密模型,在 OpenCode 上限時免費。團隊正在利用這段時間收集反饋並改進模型。
如果您有任何疑問,請<a href={email}>聯繫我們</a>。
---
### 自動重新加載
如果您的餘額低於 5 美元Zen 將自動充值 20 美元。
您可以更改自動充值金額。您還可以完全禁用自動重新加載。
---
### 每月限額
您還可以為整個工作區和每個工作區設置每月使用限制
你的團隊的成員。
例如,假設您將每月使用限額設置為 20 美元Zen 將不會使用
一個月超過20美元。但如果你啟用了自動重新加載Zen 可能會結束
如果您的餘額低於 5 美元,則向您收取超過 20 美元的費用。
---
## 隱私
我們所有的模型都在美國託管。我們的提供商遵循零保留政策,不會將您的數據用於模型訓練,但以下情況除外:
- Big Pickle在免費期間收集的數據可用於改進模型。
- GLM 4.7 免費:在免費期間,收集的數據可用於改進模型。
- Kimi K2.5 免費:在免費期間,收集的數據可用於改進模型。
- MiniMax M2.1 免費:在免費期間,收集的數據可用於改進模型。
- OpenAI API根據[OpenAI 的數據政策](https://platform.openai.com/docs/guides/your-data),請求將保留 30 天。
- Anthropic API根據[Anthropic 的數據政策](https://docs.anthropic.com/en/docs/claude-code/data-usage),請求將保留 30 天。
---
## 對於團隊
Zen 對團隊也很有效。您可以邀請隊友、分配角色、策劃
您的團隊使用的模型等等。
:::note
作為測試版的一部分,工作空間目前對團隊免費。
:::
作為測試版的一部分,管理工作空間目前對團隊免費。我們將會
很快就會分享更多有關定價的細節。
---
### 角色
您可以邀請團隊成員到您的工作區並分配角色:
- **管理員**管理模型、成員、API 密鑰和計費
- **成員**:僅管理自己的 API 密鑰
管理員還可以為每個成員設置每月支出限額,以控製成本。
---
### 模型訪問
管理員可以啟用或禁用工作區的特定模型。對禁用模型發出的請求將返回錯誤。
這對於您想要禁用以下模型的情況很有用:
收集數據。
---
### 帶上你自己的鑰匙
您可以使用自己的 OpenAI 或 Anthropic API 密鑰,同時仍然訪問 Zen 中的其他模型。
當您使用自己的密鑰時,令牌將由提供商直接計費,而不是由 Zen。
例如,您的組織可能已經擁有 OpenAI 或 Anthropic 的密鑰
你想使用它而不是 Zen 提供的。
---
## 目標
我們創建 OpenCode Zen 的目的是:
1. **對標**編碼代理的最佳模型/提供商。
2. 可以使用**最高質量**選項,而不是降低性能或轉向更便宜的提供商。
3. 通過按成本價銷售來傳遞任何**價格下跌**;所以唯一的加價就是支付我們的加工費。
4. 通過允許您將其與任何其他編碼代理一起使用,**無鎖定**。並且始終允許您將任何其他提供程序與 OpenCode 一起使用。