opencode-ahp 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,77 +1,69 @@
1
1
  # opencode-ahp
2
2
 
3
- OpenCode v2 plugin that hosts the **Agent Host Protocol (AHP) 1.0.0**
4
- in-process so opencode is a native agent in the **VS Code Agents window** —
5
- on this machine and on other machines over the local LAN.
3
+ An opencode v2 plugin that makes opencode an agent in the **VS Code
4
+ Agents window** — on this machine, and on any other machine on your LAN.
5
+
6
+ It works by adding one WebSocket listener to your existing opencode
7
+ server, speaking [AHP](https://microsoft.github.io/agent-host-protocol/)
8
+ (Agent Host Protocol 1.0.0) on it — the protocol VS Code uses to talk to
9
+ external coding agents. No separate process runs on the host, and nothing
10
+ is installed on the client machine: VS Code ships the AHP client.
6
11
 
7
12
  ## What you get
8
13
 
9
14
  In the Agents window, an `opencode` host with:
10
15
 
11
- - **Sessions** — new sessions (AHP `createSession`), a live session list
12
- (opencode titles/statuses), and chat state across reconnects (snapshot +
13
- replay).
14
- - **Model picker** — the real opencode catalog: every provider/model the
15
- opencode server exposes, including llama.cpp servers registered by sync
16
- plugins.
16
+ - **Sessions** — start new sessions, see the live list with opencode's
17
+ titles and statuses, and pick up where you left off after a reconnect.
18
+ - **Working directories** — VS Code's folder picker works against the host's
19
+ filesystem, and a session started in a picked folder runs *there*: its
20
+ agent, changesets, terminal, and file reads are scoped to that
21
+ directory (sessions without one use the plugin's own location).
22
+ - **Model picker** — the real opencode catalog: every provider and model
23
+ your server exposes.
17
24
  - **Agent picker** — opencode's user-invocable agents (Build, Plan, and
18
- anything user-defined) appear as AHP custom agents; subagent-mode agents
19
- are hidden from the picker.
20
- - **Chat** — streaming text + reasoning, the full tool-call lifecycle
21
- (started → ready → confirmed → complete), and usage totals.
22
- - **Permissions** — opencode's `permission.asked` becomes an in-window
23
- approval prompt; approve runs the tool, deny blocks it.
24
- - **Client tools** — a client can register its own tools per session
25
- (`createSession` or `session/activeClientSet`): the model calls them
26
- directly, the host delegates execution to the owning client
27
- (`toolClientExecution` request — the session stays in progress while the
28
- client runs), and the client's result flows back into the turn, including
29
- `requiresResultConfirmation` approval. Client tools bypass opencode's
30
- permission prompt; only the client that provided the tool may execute it
31
- or confirm its result.
32
- - **Multi-chat** — a session can host additional chats (AHP `createChat`,
33
- gated on the advertised `multipleChats` capability): each chat runs
34
- against its own opencode backend session with fully isolated turns, and
35
- `disposeChat` removes a chat without touching the session. The chats
36
- appear in the session's catalog (default chat first); each chat carries
37
- its own changesets. Fork/side-chat creation is not supported (opencode
38
- exposes no fork to plugins). Peer-chat backends appear in opencode's own
39
- UI as ordinary sessions — they are real opencode sessions underneath.
40
- **Survives restarts:** the host stamps small binding markers into the
41
- opencode sessions' metadata at create, so after a location reload each
42
- chat re-attaches at its original URI (and a client-chosen session
43
- channel resurfaces under its original name); re-attached chat titles are
44
- re-derived (`Chat 2`, …) while session titles and messages persist.
45
- - **Steering + queue** — a message sent mid-turn steers the active turn or
46
- queues for the next one (AHP `pendingMessageSet`).
47
- - **Changesets** — per-session diff channels (uncommitted, session, and
48
- per-turn) backed by the location's git state: live `file://` refs,
49
- `git show HEAD:` before-refs, and in-memory turn snapshots; the
50
- `discard-changes` operation restores a file. Requires the location
51
- directory to be a git repository (otherwise the changesets are empty);
25
+ anything you define) appear as selectable agents; subagent-only agents
26
+ are hidden.
27
+ - **Chat** — streaming text and reasoning, the full tool-call lifecycle
28
+ (a call appears, gets approved, and shows its result as it runs), and
29
+ usage totals.
30
+ - **Permissions** — when opencode asks for permission to run a tool, the
31
+ Agents window shows an approval prompt: approve runs the tool, deny
32
+ blocks it.
33
+ - **Client tools** — other AHP clients can register their own tools on a
34
+ session; the model calls them directly and the host delegates execution
35
+ back to the client that provided the tool, with optional approval of the
36
+ result.
37
+ - **Multi-chat** — one session can host additional chats, each running
38
+ against its own opencode backend session with fully isolated turns; a
39
+ chat can be removed without touching the session. Chats (and
40
+ client-chosen session names) survive location reloads, and the peer-chat
41
+ backends are real opencode sessions, so they also appear in opencode's
42
+ own UI. Forking a chat into a new one is not supported (opencode
43
+ exposes no fork to plugins).
44
+ - **Steering + queue** — send a message mid-turn and it either steers the
45
+ running turn or queues for the next one.
46
+ - **Changesets** — diff views of the uncommitted, per-session, and
47
+ per-turn changes, backed by the project's git state, with a
48
+ discard-changes operation to restore a file. Requires the project
49
+ directory to be a git repository (otherwise the views are empty);
52
50
  opt out with `capabilities.changesets: false`.
53
- - **Terminals** — two kinds, both in the root catalog:
54
- - **Interactive pty shells** (`createTerminal`) — a real shell in a pty
55
- (your `$SHELL`, default 80×24, the location's working directory):
56
- client-chosen channel, `isPty: true`, input/resize drive the process,
57
- `disposeTerminal` kills it. This is what makes a VS Code Agents-window
51
+ - **Terminals** — two kinds:
52
+ - **Interactive shells** — a real shell in the project directory (your
53
+ `$SHELL`, default 80×24); input and resize drive the process, and
54
+ closing it kills the shell. This is what makes a VS Code Agents-window
58
55
  session usable — clicking it opens a working terminal.
59
- - **Read-only renderings** of `shell`-tool executions
60
- (`ahp-terminal:`, `isPty: false`, completed-command output with exit
61
- codes): while a turn runs a foreground shell command, the terminal
62
- appears in the root catalog, streams
63
- `data` → `commandFinished` → `exited` to subscribers, and leaves the
64
- catalog at turn end (the channel state stays subscribable until the
65
- chat/session is removed). These are host-managed — `createTerminal`
66
- can't target them and client `terminal/*` actions get clean
67
- read-only errors.
56
+ - **Command output** — read-only renderings of `shell`-tool runs: while
57
+ a turn runs a foreground command, a terminal shows its streaming
58
+ output and exit code, and leaves the list at turn end.
68
59
 
69
60
  Opt out of both with `capabilities.terminals: false`.
70
61
 
71
62
  ## Requirements
72
63
 
73
- - **opencode v2** (≥ 2.2.6 verified). The plugin runs *inside* the opencode
74
- server — no extra process; it only opens one extra port.
64
+ - **opencode v2** (≥ 2.2.6 verified; check with `opencode --version`). The
65
+ plugin runs *inside* the opencode server — no extra process; it only
66
+ opens one extra port.
75
67
  - **VS Code ≥ 1.144** (1.145.2 verified) for the Agents window and
76
68
  `Agents: Add Remote Agent Host…`. VS Code ships the AHP client — nothing
77
69
  is installed on the client machine.
@@ -90,6 +82,14 @@ opencode plugin add opencode-ahp
90
82
  opencode plugin add github:mattzink/opencode-ahp
91
83
  ```
92
84
 
85
+ `opencode plugin add` writes a bare entry to your global config (the
86
+ command prints which file). To set options, edit that entry into object
87
+ form — and keep it the only entry for this plugin:
88
+
89
+ ```jsonc
90
+ { "plugins": [{ "package": "opencode-ahp", "options": { "token": "…" } }] }
91
+ ```
92
+
93
93
  Installed copies don't auto-update. To update:
94
94
 
95
95
  ```sh
@@ -110,38 +110,91 @@ opencode service restart
110
110
  ```
111
111
 
112
112
  Restart the opencode service afterwards (`opencode service restart`). The
113
- AHP listener starts when the location for the configured directory boots.
113
+ AHP listener starts when the project containing the plugin config —
114
+ opencode calls it a *location* — is first opened.
114
115
 
115
116
  ## Options
116
117
 
117
- Per-key resolution: the config entry's `options` ▸ an `options.jsonc`
118
- sibling of `src/index.ts` (checkouts only — copy
119
- [`options.example.jsonc`](options.example.jsonc)) ▸ built-in defaults.
120
-
121
- [`options.example.jsonc`](options.example.jsonc) documents the full
122
- option set — every key, its default, and what it does. A few of the
123
- important ones:
118
+ Set options in the `options` object of your plugin entry (see
119
+ [Install](#install)). If you're running from a checkout, you can instead
120
+ keep them in an `options.jsonc` file next to `src/index.ts` — start from
121
+ [`options.example.jsonc`](options.example.jsonc), which documents the
122
+ full option set, key by key. When both are present they merge per key,
123
+ with the `opencode.jsonc` value winning. A few of the important ones:
124
124
 
125
125
  | Key | Default | Description |
126
126
  | --- | --- | --- |
127
- | `token` | auto | Omit → generated on first start, persisted in opencode's plugin storage, printed once to the service log. Accepted via the `?token=` query param and `Authorization: Bearer <token>`. **Always enforced**, loopback or not |
127
+ | `token` | auto | The credential clients must present, via `?token=…` in the URL or an `Authorization: Bearer` header. **Always enforced**, loopback or not. Omit to have the plugin generate one on first start — it's persisted in opencode's plugin storage and printed once to the server log (`[opencode-ahp] AHP token …`) |
128
128
  | `host` | `"127.0.0.1"` | Bind address; a **non-loopback bind requires an explicit `token`** (the listener refuses to start otherwise) |
129
129
  | `port` | `8765` | WS listen port |
130
- | `keepAlive` | `true` | Keep the opencode location loaded against its idle timeout by periodically touching an internal anchor session — with this on, **no `location_idle_timeout` config entry is needed**. See the example file for the anchor's visibility details |
130
+ | `allowedWorkingDirectories` | `[]` (unrestricted) | Optional directory allowlist: absolute path prefixes that bound which paths a client may name (the `createSession.workingDirectories` target and the unconfined `resource*` commands). Opt-in hardening — the AHP token is a full client credential, so this scopes blast radius rather than sandboxing (design.md §3.3) |
131
+
132
+ ## Quick start (this machine)
133
+
134
+ 1. **Install** (above), then put your options in the plugin entry. The
135
+ cleanest setup is a single plugin entry in the project's
136
+ `.opencode/opencode.jsonc` (if `opencode plugin add` wrote a bare
137
+ entry to your global config, remove it — the plugin should be
138
+ configured in exactly one place):
139
+
140
+ ```jsonc
141
+ // the project's .opencode/opencode.jsonc
142
+ {
143
+ "experimental": { "location_idle_timeout": false },
144
+ "plugins": [{
145
+ "package": "opencode-ahp",
146
+ "options": { "token": "change-me-to-a-long-random-string" }
147
+ }]
148
+ }
149
+ ```
150
+
151
+ Two things worth knowing:
152
+
153
+ - **Set your own `token`.** If you omit one, the plugin generates a
154
+ token and prints it to the opencode server log once — that works,
155
+ but picking your own is easier.
156
+ - **`location_idle_timeout: false` is required.** In opencode v2 a
157
+ project directory (a *location*) loads when it's first used and is
158
+ unloaded after 60 minutes of idleness. AHP connections don't count
159
+ as location activity, so without this line the listener — and your
160
+ host in VS Code — goes away while you're away. A plugin cannot
161
+ prevent this sweep ([docs/design.md](docs/design.md) §15.3).
131
162
 
132
- ## Remote access (local LAN)
163
+ 2. **Restart the server** (`opencode service restart`) and **open the
164
+ project once** — in the opencode TUI, in your IDE, or headless:
133
165
 
134
- The LAN transport is plain `ws://` + token (the token in the URL is
135
- cleartext — LAN only, by design). Off-LAN options (Tailscale, a dev-tunnel
136
- port forward, any `wss://` proxy) are operational; the host accepts any
137
- `ws(s)://` URL whose path and token match.
166
+ ```sh
167
+ curl -g -u opencode:<server-password> \
168
+ 'http://127.0.0.1:<port>/api/location?location[directory]=<project-dir>'
169
+ ```
170
+
171
+ (Drop `-u opencode:<server-password>` if your server has no
172
+ password.) This is the only manual step per server process: the plugin
173
+ can't run before its location has booted, and booting starts the AHP
174
+ listener.
175
+
176
+ 3. **Add the host in VS Code** — Command Palette →
177
+ **`Agents: Add Remote Agent Host…`** → paste:
178
+
179
+ ```
180
+ ws://127.0.0.1:8765/ahp?token=<token>
181
+ ```
182
+
183
+ Pick the `opencode` host in the Agents window's picker, start a
184
+ session, and you're chatting with your local opencode.
185
+
186
+ ## Use it from another machine on your LAN
187
+
188
+ Everything in the [quick start](#quick-start-this-machine) still applies;
189
+ this section is the delta. The transport is plain `ws://` with the token
190
+ in the URL (cleartext — LAN only, by design). Off-LAN options (Tailscale,
191
+ a dev-tunnel port forward, any `wss://` proxy) are operational; the host
192
+ accepts any `ws(s)://` URL whose path and token match.
138
193
 
139
194
  ### Host side
140
195
 
141
- 1. Serve the project on all interfaces: `opencode service set hostname
142
- 0.0.0.0` + restart, or a dedicated
143
- `opencode serve --hostname 0.0.0.0 --port <port>`.
144
- 2. Configure the plugin (project or global `opencode.jsonc`):
196
+ 1. **Bind the listener to a LAN address, with an explicit token** — a
197
+ non-loopback bind refuses to start without one:
145
198
 
146
199
  ```jsonc
147
200
  {
@@ -156,29 +209,36 @@ port forward, any `wss://` proxy) are operational; the host accepts any
156
209
  }
157
210
  ```
158
211
 
159
- No `experimental.location_idle_timeout` entry needed: by default the
160
- plugin keeps its own location loaded (`keepAlive`, see Options) by
161
- periodically touching an internal anchor session (visible in the TUI
162
- session list as "opencode-ahp (keep-alive)", hidden from AHP clients)
163
- — opencode's idle sweep never fires.
212
+ Keep the quick start's `experimental.location_idle_timeout: false`
213
+ line — it's mandatory for every setup, LAN or not.
164
214
 
165
- 3. **Boot the location once** — the AHP listener exists only while the
166
- location is up, and a location boots on first use. After a fresh server
167
- start (e.g. machine reboot), open the project once (run the `opencode`
168
- TUI in it, or open it in an IDE), or headless:
215
+ 2. **The opencode server itself can stay on loopback.** The AHP listener
216
+ binds its own address, and the client talks only to the AHP
217
+ WebSocket — the opencode HTTP API is not involved. Expose the server
218
+ on all interfaces (`opencode service set hostname 0.0.0.0` + restart)
219
+ only if you also want to reach the opencode HTTP API from the second
220
+ machine (e.g. to boot the location headlessly from there).
221
+
222
+ 3. **Boot the location once after each fresh server process** (e.g. a
223
+ machine reboot) — the AHP listener exists only while the location is
224
+ up, and a location boots on first use. Open the project once (run the
225
+ `opencode` TUI in it, or open it in an IDE), or headless:
169
226
 
170
227
  ```sh
171
228
  curl -g -u opencode:<server-password> \
172
229
  'http://127.0.0.1:<port>/api/location?location[directory]=<project-dir>'
173
230
  ```
174
231
 
175
- This is a one-time step per server process: the keep-alive then holds
176
- the location (and the listener) indefinitely, and reloading the
177
- location (config change, `opencode service restart`) re-boots it
178
- automatically. (The one-time boot itself is an opencode limitation —
179
- plugins cannot run before their location has booted; a zero-touch boot
180
- needs an upstream auto-boot option.)
181
- 4. Verify: `curl -s http://<host-lan-ip>:<port>/healthz` → `ok`.
232
+ With the idle-timeout line set, the location (and the listener) then
233
+ stays loaded indefinitely, and reloading the location (config change,
234
+ `opencode service restart`) re-boots it automatically. (The one-time
235
+ boot is an opencode limitation — plugins cannot run before their
236
+ location has booted; a zero-touch boot needs an upstream auto-boot
237
+ option.)
238
+
239
+ 4. Verify: `curl -s http://<host-lan-ip>:<port>/healthz` → `ok`. If the
240
+ second PC can't reach the port: check the host's firewall, e.g.
241
+ `sudo ufw allow 8765/tcp`.
182
242
 
183
243
  Full two-machine walkthrough (and the complete testing strategy: unit,
184
244
  static, live verification, two-machine, conformance):
@@ -192,8 +252,11 @@ Command Palette → **`Agents: Add Remote Agent Host…`** → paste:
192
252
  ws://<host-lan-ip>:8765/ahp?token=<token>
193
253
  ```
194
254
 
195
- Or persist it in `settings.json` — with Settings Sync on, that one line
196
- propagates to every PC:
255
+ The host then appears in the Agents window's picker — select it and start
256
+ a session.
257
+
258
+ Or persist the URL in `settings.json` — with Settings Sync on, that one
259
+ line propagates to every PC:
197
260
 
198
261
  ```jsonc
199
262
  "chat.remoteAgentHosts": [{ "name": "opencode", "address": "ws://…:8765/ahp?token=…" }]
@@ -222,7 +285,7 @@ the middle:
222
285
  }
223
286
  ```
224
287
 
225
- The client URL becomes `wss://<host>:8767/ahp?token=<token>` and the
288
+ The client URL becomes `wss://<host>:<port>/ahp?token=<token>` and the
226
289
  token/Origin policy is unchanged. The certificate must be trusted by the
227
290
  client machine for the URL's hostname: VS Code expects a real chain
228
291
  (mkcert on a dev network, or any CA the client trusts); self-signed
@@ -231,6 +294,11 @@ certs only work for script-based checks
231
294
  **TLS-only** — plain-HTTP requests to it fail, and the health check
232
295
  becomes `curl -k https://<host>:<port>/healthz` → `ok`.
233
296
 
297
+ ---
298
+
299
+ *Everything below is for people who work on the plugin itself, not for
300
+ users.*
301
+
234
302
  ## Development
235
303
 
236
304
  ```sh
@@ -274,11 +342,13 @@ this repo requires it.
274
342
 
275
343
  ## Publishing
276
344
 
277
- House pattern: push to `main` → CI (`typecheck` + `test`) → the publish
278
- workflow computes the version as `<major>.<minor>.<run-number>` and
279
- `npm publish`es via **npm trusted publishing (OIDC)** — no npm token or
280
- repo secret is stored (one-time setup: npmjs.com → package → *Package
281
- settings* → *Trusted publishers* → this workflow).
345
+ The published package is [`opencode-ahp` on npm](https://www.npmjs.com/package/opencode-ahp).
346
+
347
+ Publishing is CI-only: push to `main` → CI (`typecheck` + `test`) → the
348
+ publish workflow computes the version as `<major>.<minor>.<run-number>`
349
+ and `npm publish`es via **npm trusted publishing (OIDC)** — no npm token
350
+ or repo secret is stored (one-time setup: npmjs.com → package →
351
+ *Package settings* → *Trusted publishers* → this workflow).
282
352
 
283
353
  ## Documentation
284
354
 
@@ -28,6 +28,21 @@
28
28
  // ["vscode://vscode"]. The token is always required regardless.
29
29
  // "allowedOrigins": "*",
30
30
 
31
+ // Directory allowlist: absolute path prefixes that
32
+ // bound which paths a client may name on the host — the
33
+ // createSession.workingDirectories target and the unconfined resource*
34
+ // commands (resourceResolve/resourceList/resourceMkdir). Absent or
35
+ // empty (default) → unrestricted (the default posture matches the VS
36
+ // Code reference host, which exposes the whole host filesystem to an
37
+ // authenticated client). A path is permitted when its symlink-resolved
38
+ // form is under one of the prefixes (a symlink under an allowed prefix
39
+ // that points outside is denied). The token is a full client credential
40
+ // (e.g. the agent's shell tool is not directory-confined), so this is
41
+ // blast-radius scoping, not a sandbox. A non-absolute entry is a
42
+ // configuration error: the host fails closed (AHP off, opencode keeps
43
+ // serving).
44
+ // "allowedWorkingDirectories": ["/tmp", "/home"],
45
+
31
46
  // Native TLS termination via Bun.serve → wss:// (off-LAN transports,
32
47
  // D3). Paths to the PEM key and certificate files; with this set the
33
48
  // port is TLS-only (health endpoint → `curl -k https://…/healthz`).
@@ -37,18 +52,6 @@
37
52
  // Plain-HTTP health endpoint (no auth).
38
53
  // "healthPath": "/healthz",
39
54
 
40
- // Keep the opencode location loaded against the idle timeout
41
- // (experimental.location_idle_timeout, default 60 min) by touching an
42
- // internal anchor session periodically. On, the
43
- // location_idle_timeout config entry is not needed. The anchor shows in
44
- // the TUI session list as "opencode-ahp (keep-alive)"; AHP clients never
45
- // see it.
46
- // "keepAlive": true,
47
-
48
- // Minutes between keep-alive touches; keep well below the effective idle
49
- // timeout (10 leaves sixfold headroom under the 60-minute default).
50
- // "keepAliveIntervalMinutes": 10,
51
-
52
55
  // Global actions kept for reconnect replay; older gaps → fresh snapshot.
53
56
  // "replayWindow": 1000,
54
57
 
@@ -59,7 +62,7 @@
59
62
  // type) — with the raw `data` JSON (truncated to 300 chars). Off by
60
63
  // default: a busy turn emits dozens of lines. The LAN test server
61
64
  // script (scripts/ahp-server.mjs) enables it. Use it to attribute turn
62
- // latency to host vs client vs model (design.md §8.1).
65
+ // latency to host vs client vs model.
63
66
  // "logEvents": true,
64
67
 
65
68
  // Host-side capability switches — never advertise what we don't
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-ahp",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "OpenCode V2 plugin: hosts Agent Host Protocol (AHP) 1.0.0 in-process so opencode is a native agent in the VS Code Agents window",
5
5
  "type": "module",
6
6
  "exports": {