Skip to content

Commit c81cde0

Browse files
committed
feat(cli): add acp attach mode
1 parent bd44489 commit c81cde0

10 files changed

Lines changed: 116 additions & 32 deletions

File tree

packages/opencode/src/acp/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ opencode acp
4242

4343
# Start in a specific directory
4444
opencode acp --cwd /path/to/project
45+
46+
# Bridge ACP stdio to an existing OpenCode server
47+
opencode acp --attach http://localhost:4096
48+
49+
# When attaching, --cwd still applies on the attached server
50+
opencode acp --attach http://localhost:4096 --cwd /path/to/project
4551
```
4652

4753
### Question Tool Opt-In

packages/opencode/src/cli/cmd/acp.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ export const AcpCommand = cmd({
1313
command: "acp",
1414
describe: "start ACP (Agent Client Protocol) server",
1515
builder: (yargs) => {
16-
return withNetworkOptions(yargs).option("cwd", {
17-
describe: "working directory",
18-
type: "string",
19-
default: process.cwd(),
20-
})
16+
return withNetworkOptions(yargs)
17+
.option("attach", {
18+
type: "string",
19+
describe: "attach to a running opencode server (e.g., http://localhost:4096)",
20+
})
21+
.option("cwd", {
22+
describe: "working directory",
23+
type: "string",
24+
default: process.cwd(),
25+
})
2126
},
2227
handler: async (args) => {
2328
process.env.OPENCODE_CLIENT = "acp"
24-
await bootstrap(process.cwd(), async () => {
25-
const opts = await resolveNetworkOptions(args)
26-
const server = Server.listen(opts)
27-
28-
const sdk = createOpencodeClient({
29-
baseUrl: `http://${server.hostname}:${server.port}`,
30-
})
29+
const dir = args.cwd
3130

31+
async function execute(sdk: ReturnType<typeof createOpencodeClient>) {
3232
const input = new WritableStream<Uint8Array>({
3333
write(chunk) {
3434
return new Promise<void>((resolve, reject) => {
@@ -65,6 +65,24 @@ export const AcpCommand = cmd({
6565
process.stdin.on("end", resolve)
6666
process.stdin.on("error", reject)
6767
})
68+
}
69+
70+
if (args.attach) {
71+
const sdk = createOpencodeClient({
72+
baseUrl: args.attach,
73+
directory: dir,
74+
})
75+
return await execute(sdk)
76+
}
77+
78+
await bootstrap(dir, async () => {
79+
const opts = await resolveNetworkOptions(args)
80+
const server = Server.listen(opts)
81+
const sdk = createOpencodeClient({
82+
baseUrl: `http://${server.hostname}:${server.port}`,
83+
directory: dir,
84+
})
85+
return await execute(sdk)
6886
})
6987
},
7088
})

packages/web/src/content/docs/acp.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ To use OpenCode via ACP, configure your editor to run the `opencode acp` command
1919

2020
The command starts OpenCode as an ACP-compatible subprocess that communicates with your editor over JSON-RPC via stdio.
2121

22+
You can also run `opencode acp --attach <url>` to bridge ACP stdio to an existing OpenCode server instead of starting a local backend. `--cwd` still applies when attaching, and the path is interpreted on the attached server.
23+
2224
Below are examples for popular editors that support ACP.
2325

2426
---

packages/web/src/content/docs/cli.mdx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,26 @@ opencode acp
479479

480480
This command starts an ACP server that communicates via stdin/stdout using nd-JSON.
481481

482+
You can also attach to a running `opencode serve` instance to bridge ACP stdio to it instead of starting a local backend:
483+
484+
```bash
485+
# Start a headless server in one terminal
486+
opencode serve
487+
488+
# In another terminal, bridge ACP stdio to it
489+
opencode acp --attach http://localhost:4096
490+
```
491+
492+
When attaching, `--cwd` still applies, but the path is interpreted on the attached server.
493+
482494
#### Flags
483495

484-
| Flag | Description |
485-
| ------------ | --------------------- |
486-
| `--cwd` | Working directory |
487-
| `--port` | Port to listen on |
488-
| `--hostname` | Hostname to listen on |
496+
| Flag | Description |
497+
| ------------ | ------------------------------------------------------------- |
498+
| `--cwd` | Working directory |
499+
| `--attach` | Attach to a running opencode server (e.g. http://localhost:4096) |
500+
| `--port` | Port to listen on |
501+
| `--hostname` | Hostname to listen on |
489502

490503
---
491504

packages/web/src/content/docs/pl/acp.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Aby używać OpenCode przez ACP, skonfiguruj swój edytor tak, aby uruchamiał p
2121

2222
Uruchamia to OpenCode jako podproces zgodny z ACP, który komunikuje się z edytorem za pomocą JSON-RPC przez stdio.
2323

24+
Możesz też uruchomić `opencode acp --attach <url>`, aby zmostkować stdio ACP do istniejącego serwera OpenCode zamiast uruchamiać lokalny backend. Podczas podłączania `--cwd` nadal działa, ale ścieżka jest interpretowana na podłączonym serwerze.
25+
2426
Poniżej znajdują się przykłady dla edytorów obsługujących ACP.
2527

2628
---

packages/web/src/content/docs/pl/cli.mdx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,26 @@ opencode acp
479479

480480
Uruchamia serwer ACP, który komunikuje się przez stdin/stdout przy użyciu JSON-RPC.
481481

482+
Możesz też podłączyć się do działającego `opencode serve`, aby zmostkować stdio ACP do istniejącego serwera zamiast uruchamiać lokalny backend:
483+
484+
```bash
485+
# Start a headless server in one terminal
486+
opencode serve
487+
488+
# In another terminal, bridge ACP stdio to it
489+
opencode acp --attach http://localhost:4096
490+
```
491+
492+
Podczas podłączania `--cwd` nadal działa, ale ścieżka jest interpretowana na podłączonym serwerze.
493+
482494
#### Flagi
483495

484-
| Flaga | Opis |
485-
| ------------ | ------------------------------------------ |
486-
| `--cwd` | Katalog roboczy |
487-
| `--port` | Port do nasłuchiwania |
488-
| `--hostname` | Nazwa hosta, do której należy się powiązać |
496+
| Flaga | Opis |
497+
| ------------ | ------------------------------------------------------------- |
498+
| `--cwd` | Katalog roboczy |
499+
| `--attach` | Dołącz do działającego serwera OpenCode (np. http://localhost:4096) |
500+
| `--port` | Port do nasłuchiwania |
501+
| `--hostname` | Nazwa hosta, do której należy się powiązać |
489502

490503
---
491504

packages/web/src/content/docs/tr/acp.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ opencode'u ACP aracılığıyla kullanmak için düzenleyicinizi `opencode acp`
1919

2020
Komut, opencode'u, editörünüzle stdio aracılığıyla JSON-RPC üzerinden iletişim kuran ACP uyumlu bir alt süreç olarak başlatır.
2121

22+
Yerel bir arka uç başlatmak yerine ACP stdio'yu mevcut bir OpenCode sunucusuna köprülemek için `opencode acp --attach <url>` komutunu da çalıştırabilirsiniz. Bağlanırken `--cwd` yine geçerlidir, ancak yol bağlı sunucuda yorumlanır.
23+
2224
Aşağıda ACP'yi destekleyen popüler düzenleyicilere ilişkin örnekler verilmiştir.
2325

2426
---

packages/web/src/content/docs/tr/cli.mdx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,26 @@ opencode acp
479479

480480
Bu komut, nd-JSON kullanarak stdin/stdout aracılığıyla iletişim kuran bir ACP sunucusunu başlatır.
481481

482+
Yerel bir arka uç başlatmak yerine ACP stdio'yu mevcut bir sunucuya köprülemek için çalışan bir `opencode serve` örneğine de bağlanabilirsiniz:
483+
484+
```bash
485+
# Start a headless server in one terminal
486+
opencode serve
487+
488+
# In another terminal, bridge ACP stdio to it
489+
opencode acp --attach http://localhost:4096
490+
```
491+
492+
Bağlanırken `--cwd` yine geçerlidir, ancak yol bağlı sunucuda yorumlanır.
493+
482494
#### Bayraklar
483495

484-
| Bayrak | Açıklama |
485-
| ------------ | ------------------- |
486-
| `--cwd` | Çalışma dizini |
487-
| `--port` | Dinlenecek port |
488-
| `--hostname` | Dinlenecek host adı |
496+
| Bayrak | Açıklama |
497+
| ------------ | ------------------------------------------------------------- |
498+
| `--cwd` | Çalışma dizini |
499+
| `--attach` | Çalışan bir opencode sunucusuna bağlanın (ör. http://localhost:4096) |
500+
| `--port` | Dinlenecek port |
501+
| `--hostname` | Dinlenecek host adı |
489502

490503
---
491504

packages/web/src/content/docs/zh-cn/acp.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ ACP 是一个开放协议,用于标准化代码编辑器与 AI 编码代理之
1919

2020
该命令会将 OpenCode 作为兼容 ACP 的子进程启动,通过 stdio 上的 JSON-RPC 与编辑器进行通信。
2121

22+
你也可以运行 `opencode acp --attach <url>`,将 ACP 的 stdio 桥接到现有的 OpenCode 服务器,而不是启动本地后端。连接时,`--cwd` 仍然生效,但该路径会在所连接的服务器上解析。
23+
2224
以下是支持 ACP 的常用编辑器的配置示例。
2325

2426
---

packages/web/src/content/docs/zh-cn/cli.mdx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,13 +479,26 @@ opencode acp
479479

480480
此命令启动一个通过 stdin/stdout 使用 nd-JSON 进行通信的 ACP 服务器。
481481

482+
您也可以连接到正在运行的 `opencode serve` 实例,将 ACP 的 stdio 桥接到该实例,而不是启动本地后端:
483+
484+
```bash
485+
# Start a headless server in one terminal
486+
opencode serve
487+
488+
# In another terminal, bridge ACP stdio to it
489+
opencode acp --attach http://localhost:4096
490+
```
491+
492+
连接时,`--cwd` 仍然生效,但该路径会在所连接的服务器上解析。
493+
482494
#### 标志
483495

484-
| 标志 | 描述 |
485-
| ------------ | ---------- |
486-
| `--cwd` | 工作目录 |
487-
| `--port` | 监听端口 |
488-
| `--hostname` | 监听主机名 |
496+
| 标志 | 描述 |
497+
| ------------ | ------------------------------------------------------ |
498+
| `--cwd` | 工作目录 |
499+
| `--attach` | 连接到正在运行的 opencode 服务器(例如 http://localhost:4096) |
500+
| `--port` | 监听端口 |
501+
| `--hostname` | 监听主机名 |
489502

490503
---
491504

0 commit comments

Comments
 (0)