opencode-ahp 0.1.4 → 0.1.6
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 +91 -37
- package/options.example.jsonc +25 -8
- package/package.json +1 -1
- package/src/bridge.ts +335 -127
- package/src/channels.ts +74 -32
- package/src/client-tools.ts +127 -0
- package/src/handshake.ts +2 -3
- package/src/host-base.ts +335 -0
- package/src/host-changesets.ts +381 -0
- package/src/host-chats.ts +300 -0
- package/src/host-client-tools.ts +205 -0
- package/src/host-dispatch.ts +593 -0
- package/src/host-errors.ts +29 -0
- package/src/host-events.ts +386 -0
- package/src/host-rpc.ts +435 -0
- package/src/host-sessions.ts +558 -0
- package/src/host-terminals.ts +654 -0
- package/src/host-turns.ts +82 -0
- package/src/host-types.ts +225 -0
- package/src/host-util.ts +194 -0
- package/src/host.ts +23 -2299
- package/src/index.ts +265 -33
- package/src/keep-alive.ts +3 -3
- package/src/options.ts +23 -9
- package/src/seq.ts +4 -5
- package/src/state.ts +40 -6
- package/src/transport.ts +6 -6
package/README.md
CHANGED
|
@@ -2,15 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
OpenCode v2 plugin that hosts the **Agent Host Protocol (AHP) 1.0.0**
|
|
4
4
|
in-process so opencode is a native agent in the **VS Code Agents window** —
|
|
5
|
-
on this machine and
|
|
6
|
-
|
|
7
|
-
> **Status: Phase 3 — wss:// transport complete.** Sessions, model +
|
|
8
|
-
> agent pickers, streaming, tool calls, permission prompts,
|
|
9
|
-
> steering/queue, usage, **changesets (uncommitted / session / per-turn
|
|
10
|
-
> diffs with `discard-changes`)**, and reconnect replay are
|
|
11
|
-
> live-verified over the LAN with VS Code 1.145.2 (real Agents window ↔
|
|
12
|
-
> host), over plain `ws://` **and native `wss://`** (TLS-terminated).
|
|
13
|
-
> The full plan and wire findings live in [`plan.md`](plan.md).
|
|
5
|
+
on this machine and on other machines over the local LAN.
|
|
14
6
|
|
|
15
7
|
## What you get
|
|
16
8
|
|
|
@@ -29,6 +21,27 @@ In the Agents window, an `opencode` host with:
|
|
|
29
21
|
(started → ready → confirmed → complete), and usage totals.
|
|
30
22
|
- **Permissions** — opencode's `permission.asked` becomes an in-window
|
|
31
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.
|
|
32
45
|
- **Steering + queue** — a message sent mid-turn steers the active turn or
|
|
33
46
|
queues for the next one (AHP `pendingMessageSet`).
|
|
34
47
|
- **Changesets** — per-session diff channels (uncommitted, session, and
|
|
@@ -37,10 +50,23 @@ In the Agents window, an `opencode` host with:
|
|
|
37
50
|
`discard-changes` operation restores a file. Requires the location
|
|
38
51
|
directory to be a git repository (otherwise the changesets are empty);
|
|
39
52
|
opt out with `capabilities.changesets: false`.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
(
|
|
43
|
-
|
|
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
|
|
58
|
+
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.
|
|
68
|
+
|
|
69
|
+
Opt out of both with `capabilities.terminals: false`.
|
|
44
70
|
|
|
45
71
|
## Requirements
|
|
46
72
|
|
|
@@ -64,6 +90,13 @@ opencode plugin add opencode-ahp
|
|
|
64
90
|
opencode plugin add github:mattzink/opencode-ahp
|
|
65
91
|
```
|
|
66
92
|
|
|
93
|
+
Installed copies don't auto-update. To update:
|
|
94
|
+
|
|
95
|
+
```sh
|
|
96
|
+
opencode plugin update opencode-ahp
|
|
97
|
+
opencode service restart
|
|
98
|
+
```
|
|
99
|
+
|
|
67
100
|
**Dev (local checkout):**
|
|
68
101
|
|
|
69
102
|
```jsonc
|
|
@@ -85,26 +118,22 @@ Per-key resolution: the config entry's `options` ▸ an `options.jsonc`
|
|
|
85
118
|
sibling of `src/index.ts` (checkouts only — copy
|
|
86
119
|
[`options.example.jsonc`](options.example.jsonc)) ▸ built-in defaults.
|
|
87
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:
|
|
124
|
+
|
|
88
125
|
| Key | Default | Description |
|
|
89
126
|
| --- | --- | --- |
|
|
90
|
-
| `enabled` | `true` | Master switch; `false` → no listener, the plugin is fully inert |
|
|
91
|
-
| `port` | `8765` | WS listen port |
|
|
92
|
-
| `host` | `"127.0.0.1"` | Bind address; a **non-loopback bind requires an explicit `token`** (the listener refuses to start otherwise) |
|
|
93
|
-
| `path` | `"/ahp"` | WS path — VS Code's URL carries it verbatim |
|
|
94
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 |
|
|
95
|
-
| `
|
|
96
|
-
| `
|
|
97
|
-
| `
|
|
98
|
-
| `keepAlive` | `true` | Keep the opencode location loaded against its idle timeout (`experimental.location_idle_timeout`, default 60 min) by periodically touching an internal anchor session — with this on, **no `location_idle_timeout` config entry is needed**. The anchor (`opencode-ahp (keep-alive)`) is a real opencode session: it shows in the TUI session list but is hidden from AHP clients (opencode's plugin surface cannot archive it) |
|
|
99
|
-
| `keepAliveIntervalMinutes` | `10` | Minutes between keep-alive touches; keep well below the effective idle timeout |
|
|
100
|
-
| `replayWindow` | `1000` | Per-channel actions kept for reconnect replay; older gaps → fresh snapshot |
|
|
101
|
-
| `capabilities` | `{ changesets: true, terminals: false, automations: false }` | Advertised in `initialize` — never advertise what we don't implement. Set `changesets` to `false` to disable the diff channels (the session/chat snapshots then carry no changeset catalogues and changeset URIs resolve to nothing) |
|
|
128
|
+
| `host` | `"127.0.0.1"` | Bind address; a **non-loopback bind requires an explicit `token`** (the listener refuses to start otherwise) |
|
|
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 |
|
|
102
131
|
|
|
103
|
-
## Remote access (local LAN
|
|
132
|
+
## Remote access (local LAN)
|
|
104
133
|
|
|
105
|
-
|
|
134
|
+
The LAN transport is plain `ws://` + token (the token in the URL is
|
|
106
135
|
cleartext — LAN only, by design). Off-LAN options (Tailscale, a dev-tunnel
|
|
107
|
-
port forward, any `wss://` proxy) are
|
|
136
|
+
port forward, any `wss://` proxy) are operational; the host accepts any
|
|
108
137
|
`ws(s)://` URL whose path and token match.
|
|
109
138
|
|
|
110
139
|
### Host side
|
|
@@ -151,8 +180,9 @@ port forward, any `wss://` proxy) are post-MVP; the host accepts any
|
|
|
151
180
|
needs an upstream auto-boot option.)
|
|
152
181
|
4. Verify: `curl -s http://<host-lan-ip>:<port>/healthz` → `ok`.
|
|
153
182
|
|
|
154
|
-
Full two-machine walkthrough:
|
|
155
|
-
|
|
183
|
+
Full two-machine walkthrough (and the complete testing strategy: unit,
|
|
184
|
+
static, live verification, two-machine, conformance):
|
|
185
|
+
[`docs/testing.md`](docs/testing.md).
|
|
156
186
|
|
|
157
187
|
### Client side (each PC)
|
|
158
188
|
|
|
@@ -174,7 +204,7 @@ Note: the Agents window's built-in **SSH** and **Dev tunnels** flows run
|
|
|
174
204
|
For a third-party host, only raw network reachability of the `ws(s)://`
|
|
175
205
|
URL matters.
|
|
176
206
|
|
|
177
|
-
### Off-LAN: native `wss://`
|
|
207
|
+
### Off-LAN: native `wss://`
|
|
178
208
|
|
|
179
209
|
When plain LAN `ws://` doesn't fit (host on another network, or
|
|
180
210
|
token-in-URL on a shared network you don't want), set `tls` and the same
|
|
@@ -214,14 +244,34 @@ Dependency-free live probes (Node ≥ 20 global WebSocket):
|
|
|
214
244
|
|
|
215
245
|
```sh
|
|
216
246
|
node scripts/ahp-client.mjs --token <token> [--base http://127.0.0.1:8765]
|
|
217
|
-
#
|
|
218
|
-
node scripts/ahp-
|
|
219
|
-
#
|
|
220
|
-
node scripts/ahp-
|
|
247
|
+
# end-to-end: createSession → agent-picker snapshot → full turn lifecycle
|
|
248
|
+
node scripts/ahp-turn-lifecycle.mjs --token <token> --base http://127.0.0.1:8765 --provider opencode
|
|
249
|
+
# changesets end-to-end: edit → diffs → discard, plus mid-turn reconnect/replay
|
|
250
|
+
node scripts/ahp-changesets.mjs --token <token> --base http://127.0.0.1:8765
|
|
251
|
+
# client-provided tools (ping tool, failed result, confirmation cycle)
|
|
252
|
+
node scripts/ahp-client-tools.mjs --token <token> --base http://127.0.0.1:8765
|
|
253
|
+
# multi-chat (createChat/disposeChat, per-chat backends, no ghost sessions)
|
|
254
|
+
node scripts/ahp-multi-chat.mjs --token <token> --base http://127.0.0.1:8765
|
|
255
|
+
# multi-chat durability: leave the peer chat + backend alive, reload the
|
|
256
|
+
# location, then recheck the re-attachment (chat at its original URI)
|
|
257
|
+
node scripts/ahp-multi-chat.mjs --token <token> --base http://127.0.0.1:8765 --leave-peer
|
|
258
|
+
node scripts/ahp-multi-chat.mjs --token <token> --base http://127.0.0.1:8765 --recheck <sessionChannel> <peerChatURI>
|
|
221
259
|
# the same probes over native wss:// (self-signed test cert):
|
|
222
|
-
NODE_TLS_REJECT_UNAUTHORIZED=0 node scripts/ahp-
|
|
260
|
+
NODE_TLS_REJECT_UNAUTHORIZED=0 node scripts/ahp-changesets.mjs --token <token> --base https://127.0.0.1:8765
|
|
223
261
|
```
|
|
224
262
|
|
|
263
|
+
### Local environment (git-ignored)
|
|
264
|
+
|
|
265
|
+
`docs/local.md` is the designated place for each developer to keep
|
|
266
|
+
local development environment specifics — facts about their own machine
|
|
267
|
+
that never belong in the committed tree, such as LAN IPs and hostnames,
|
|
268
|
+
per-machine versions, the port and config location of a daily opencode
|
|
269
|
+
service, the current LAN test server token/URL, reference checkouts
|
|
270
|
+
(the opencode and AHP repos, the extracted VS Code AHP client
|
|
271
|
+
sources), and scratch roots. The file is git-ignored and optional: it
|
|
272
|
+
does not exist on machines that have never created it, and nothing in
|
|
273
|
+
this repo requires it.
|
|
274
|
+
|
|
225
275
|
## Publishing
|
|
226
276
|
|
|
227
277
|
House pattern: push to `main` → CI (`typecheck` + `test`) → the publish
|
|
@@ -230,8 +280,12 @@ workflow computes the version as `<major>.<minor>.<run-number>` and
|
|
|
230
280
|
repo secret is stored (one-time setup: npmjs.com → package → *Package
|
|
231
281
|
settings* → *Trusted publishers* → this workflow).
|
|
232
282
|
|
|
233
|
-
|
|
234
|
-
|
|
283
|
+
## Documentation
|
|
284
|
+
|
|
285
|
+
- [`docs/design.md`](docs/design.md) — architecture and wire-contract findings.
|
|
286
|
+
- [`docs/futures.md`](docs/futures.md) — candidate future work
|
|
287
|
+
- [`docs/testing.md`](docs/testing.md) — the testing strategy: unit, live
|
|
288
|
+
verification, two-machine, and conformance.
|
|
235
289
|
|
|
236
290
|
## License
|
|
237
291
|
|
package/options.example.jsonc
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
// Documented option set for opencode-ahp
|
|
2
|
+
// Documented option set for opencode-ahp.
|
|
3
3
|
//
|
|
4
4
|
// How to use: copy this file to `options.jsonc` next to src/index.ts when
|
|
5
5
|
// running from a checkout, or — the usual case — put the values you want
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
// WS listen port.
|
|
13
13
|
// "port": 8765,
|
|
14
14
|
|
|
15
|
-
// Bind address. Binding a non-loopback address REQUIRES a token (
|
|
16
|
-
// listener refuses to start without one
|
|
15
|
+
// Bind address. Binding a non-loopback address REQUIRES a token (R4:
|
|
16
|
+
// the listener refuses to start without one).
|
|
17
17
|
// "host": "127.0.0.1",
|
|
18
18
|
|
|
19
19
|
// WS path — VS Code's remoteAgentHosts URL carries it verbatim.
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
// "token": "…",
|
|
26
26
|
|
|
27
27
|
// Origin allowlist: "*" (default) or an exact list, e.g.
|
|
28
|
-
// ["vscode://vscode"]. The token is always required regardless
|
|
28
|
+
// ["vscode://vscode"]. The token is always required regardless.
|
|
29
29
|
// "allowedOrigins": "*",
|
|
30
30
|
|
|
31
31
|
// Native TLS termination via Bun.serve → wss:// (off-LAN transports,
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
|
|
40
40
|
// Keep the opencode location loaded against the idle timeout
|
|
41
41
|
// (experimental.location_idle_timeout, default 60 min) by touching an
|
|
42
|
-
// internal anchor session periodically
|
|
42
|
+
// internal anchor session periodically. On, the
|
|
43
43
|
// location_idle_timeout config entry is not needed. The anchor shows in
|
|
44
44
|
// the TUI session list as "opencode-ahp (keep-alive)"; AHP clients never
|
|
45
45
|
// see it.
|
|
@@ -52,11 +52,28 @@
|
|
|
52
52
|
// Global actions kept for reconnect replay; older gaps → fresh snapshot.
|
|
53
53
|
// "replayWindow": 1000,
|
|
54
54
|
|
|
55
|
-
//
|
|
56
|
-
//
|
|
57
|
-
//
|
|
55
|
+
// Timestamped capture of the raw opencode event stream to the server
|
|
56
|
+
// log — one line per raw V2 event: `event ok` (mapped to the host),
|
|
57
|
+
// `event dropped (missing: <fields>)` (known type, unusable instance —
|
|
58
|
+
// or a malformed envelope), `event unimplemented` (no handler for the
|
|
59
|
+
// type) — with the raw `data` JSON (truncated to 300 chars). Off by
|
|
60
|
+
// default: a busy turn emits dozens of lines. The LAN test server
|
|
61
|
+
// script (scripts/ahp-server.mjs) enables it. Use it to attribute turn
|
|
62
|
+
// latency to host vs client vs model (design.md §8.1).
|
|
63
|
+
// "logEvents": true,
|
|
64
|
+
|
|
65
|
+
// Host-side capability switches — never advertise what we don't
|
|
66
|
+
// implement (in AHP 1.0.0 none of these is a wire field; discovery is
|
|
67
|
+
// the state's optionality).
|
|
68
|
+
// Default: { "changesets": true, "terminals": true, "automations": false }.
|
|
69
|
+
// "changesets": false disables the diff channels (no changeset
|
|
58
70
|
// catalogues in the session/chat snapshots; changeset URIs resolve to
|
|
59
71
|
// nothing). Changesets require the location directory to be a git
|
|
60
72
|
// repository.
|
|
73
|
+
// "terminals": false disables both terminal kinds: the synthetic
|
|
74
|
+
// shell-command terminal channels (shell-tool calls open no
|
|
75
|
+
// ahp-terminal: channels and the root state carries no terminals list)
|
|
76
|
+
// and the interactive pty shells (createTerminal/disposeTerminal
|
|
77
|
+
// answer not-found).
|
|
61
78
|
// "capabilities": { "changesets": false, "terminals": false, "automations": false }
|
|
62
79
|
}
|
package/package.json
CHANGED