opencode-apex 0.2.1 → 0.2.3
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/CHANGELOG.md +102 -48
- package/LICENSE +21 -0
- package/README.md +308 -152
- package/browser/endpoint.ts +4 -1
- package/browser.ts +2 -1
- package/core/compat.ts +13 -12
- package/core/config.ts +14 -3
- package/core/crypto.ts +2 -1
- package/core/rpc.ts +1 -1
- package/core/server-endpoint.ts +1 -1
- package/core/tools-run.ts +7 -7
- package/docs/ADVISOR.md +129 -71
- package/docs/BROWSER.md +185 -158
- package/docs/CONFIG.md +145 -55
- package/docs/EVENTS.md +211 -90
- package/docs/GUARD.md +142 -104
- package/docs/REDACT.md +143 -89
- package/docs/TROUBLESHOOTING.md +237 -189
- package/events.ts +28 -22
- package/index.ts +9 -2
- package/opencode-apex.jsonc +150 -132
- package/package.json +32 -6
- package/redact.ts +5 -4
- package/skills/apex-callbacks/SKILL.md +139 -200
- package/skills/apex-callbacks/references/examples.md +261 -0
- package/skills/apex-callbacks/references/reachability.md +103 -0
- package/skills/apex-callbacks/references/relays.md +116 -0
- package/skills/apex-callbacks/references/run-anywhere.md +133 -0
package/docs/TROUBLESHOOTING.md
CHANGED
|
@@ -1,232 +1,280 @@
|
|
|
1
1
|
# Troubleshooting — opencode-apex
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **OpenCode V2 only (2.1 or newer).** OpenCode V1 has a different plugin system
|
|
4
|
+
> (`@opencode-ai/plugin`, config key `plugin`). apex is written for V2 (`@opencode/plugin`,
|
|
5
|
+
> config key `plugins`) and cannot load on V1. That is not a bug.
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
This page has two parts:
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
- [Troubleshooting](#troubleshooting): symptom → cause → fix, for anyone using apex.
|
|
10
|
+
- [Developer notes](#developer-notes): gotchas for people changing apex's code.
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|---|---|---|
|
|
11
|
-
| **Paquete SDK** | `@opencode-ai/plugin` | `@opencode/plugin` |
|
|
12
|
-
| **API de entrada** | `plugin(input, options) => Promise<Hooks>` | `Plugin.define({ id, setup(ctx) })` |
|
|
13
|
-
| **Subrutas** | `.`, `./tool`, `./tui` | `.`, `./effect`, `./promise/*`, `./tui`, ... |
|
|
14
|
-
| **Contexto** | `PluginInput` (client, project, directory, $) | `ctx.rpc/storage/session/tool/event/permission/...` |
|
|
15
|
-
| **Este plugin** | ❌ NO carga | ✅ Diseñado para esto |
|
|
12
|
+
## Before you start: where to look
|
|
16
13
|
|
|
17
|
-
|
|
14
|
+
| What | Where |
|
|
15
|
+
| --- | --- |
|
|
16
|
+
| OpenCode version | `opencode --version` (the desktop app ships the same CLI, e.g. `/opt/OpenCode/resources/opencode-cli` on Linux) |
|
|
17
|
+
| Server log (plugin loading, apex warnings) | `~/.local/share/opencode/log/opencode.log`. For a foreground server, `opencode serve --print-logs`. |
|
|
18
|
+
| Installed copy of apex | `~/.cache/opencode/npm/opencode-apex@<spec>/<generation>/node_modules/opencode-apex` |
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
Useful log searches:
|
|
20
21
|
|
|
21
22
|
```bash
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
npm view @opencode/plugin version # 2.0.14 = SDK v2 (existe en npm)
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
### Síntoma exacto en el log (host v1 cargando plugin v2)
|
|
28
|
-
|
|
29
|
-
```
|
|
30
|
-
~/.local/share/opencode/log/opencode.log:
|
|
31
|
-
level=WARN message="failed to load plugin" target=<plugin-dir>
|
|
32
|
-
cause="Cannot find package '@opencode/plugin' imported from <plugin>/index.ts"
|
|
23
|
+
grep -a "failed to load plugin" ~/.local/share/opencode/log/opencode.log | tail
|
|
24
|
+
grep -a "\[apex" ~/.local/share/opencode/log/opencode.log | tail
|
|
33
25
|
```
|
|
34
26
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
- **B**: `mv .opencode/plugins/apex .opencode/plugins/apex.disabled` para silenciarlo en v1
|
|
27
|
+
On every load apex logs a `[apex] config: inline=… global=… local=…` line, which says
|
|
28
|
+
which config sources it found.
|
|
38
29
|
|
|
39
30
|
---
|
|
40
31
|
|
|
41
|
-
|
|
32
|
+
# Troubleshooting
|
|
42
33
|
|
|
43
|
-
|
|
44
|
-
cd opencode-v2
|
|
45
|
-
bun install # la primera vez (tarda; usar pty si la red es lenta)
|
|
46
|
-
bun run dev # TUI dev
|
|
47
|
-
bun run dev:live [dir] # TUI contra el server electo (preferido para testing)
|
|
48
|
-
bun run --cwd packages/cli src/index.ts serve --port <port> # server headless
|
|
49
|
-
```
|
|
34
|
+
## The plugin does not load
|
|
50
35
|
|
|
51
|
-
|
|
36
|
+
**Symptom:** apex tools such as `apex_subscribe` do not exist, and the log shows
|
|
37
|
+
`failed to load plugin … target=opencode-apex`.
|
|
52
38
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
39
|
+
| Cause in the log | Fix |
|
|
40
|
+
| --- | --- |
|
|
41
|
+
| `Cannot find package '@opencode/plugin'`, or OpenCode reports a 1.x version | You are on OpenCode V1. Upgrade to V2 (2.1+). |
|
|
42
|
+
| `Schema.isStartsWith is not a function`, `missing TypeId`, or other errors from inside `effect` | An `@opencode/*` package installed with apex is older than your OpenCode (see next section). Force a reinstall. |
|
|
43
|
+
| `Plugin must export a default definition…` | You pointed `package` at a file. Point it at the plugin **directory** or at the npm name `opencode-apex`. |
|
|
44
|
+
| `Plugin entrypoint not found` | The path in `package` is wrong, or the directory has no `index.ts`. |
|
|
56
45
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
## 3. Gotchas de bun (aprendidos por las malas)
|
|
60
|
-
|
|
61
|
-
### 3.1 `bun -e` resuelve imports bare relativo al CWD, no al archivo importado
|
|
62
|
-
|
|
63
|
-
```bash
|
|
64
|
-
# FALLA/cuelga desde la raíz del repo (no encuentra 'effect'):
|
|
65
|
-
bun -e "await import('/abs/path/plugin/module.ts')"
|
|
66
|
-
# OK — ejecutar desde el directorio del plugin (ahí resuelve node_modules):
|
|
67
|
-
cd .opencode/plugins/apex && bun -e "await import('./index.ts')"
|
|
68
|
-
```
|
|
46
|
+
## Version mismatch with OpenCode (`effect`)
|
|
69
47
|
|
|
70
|
-
|
|
48
|
+
apex is written with the [`effect`](https://effect.website) library, and so is OpenCode.
|
|
71
49
|
|
|
72
|
-
|
|
50
|
+
**What OpenCode guarantees.** Since 2.1, OpenCode serves `effect` and `@opencode/plugin` (every
|
|
51
|
+
subpath) to plugins **from its own copy**:
|
|
52
|
+
- an `import "effect"` inside a plugin always gets the host's version;
|
|
53
|
+
- loading those two packages from a plugin's own `node_modules` is refused, with
|
|
54
|
+
`… is not provided by OpenCode; plugins must use the host's "effect" and "@opencode/plugin" modules`.
|
|
73
55
|
|
|
74
|
-
|
|
56
|
+
**What can still drift.** The *other* `@opencode/*` packages come from apex's own
|
|
57
|
+
`node_modules`: `@opencode/schema` and `@opencode/client`, pulled in by `@opencode/plugin`,
|
|
58
|
+
and `@opencode/plugin-browser`. They run on the host's `effect`, so they must come from the
|
|
59
|
+
same release line as your OpenCode. When they don't, errors come from deep inside `effect`.
|
|
60
|
+
Example: `@opencode/schema` 2.0.14 calls `Schema.isStartsWith`, which no longer exists in the
|
|
61
|
+
`effect` 4.0.1 that OpenCode 2.1 serves.
|
|
75
62
|
|
|
76
|
-
|
|
77
|
-
// ❌ FALLA en runtime bun: "export 'ApexBus' not found" (ApexBus es type-only)
|
|
78
|
-
export { apexBus, ApexBus } from "./bus"
|
|
63
|
+
**How apex keeps them aligned (since 0.2.0):**
|
|
79
64
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
65
|
+
1. `@opencode/plugin` and `@opencode/plugin-browser` are `^2.1.1` dependencies. A fresh
|
|
66
|
+
install pulls the current 2.x packages.
|
|
67
|
+
2. `effect` is only an **optional peer** (`^4.0.0`), for editors and tests outside OpenCode.
|
|
68
|
+
apex never pins its own version.
|
|
69
|
+
3. On load, apex warns (`[apex] …` in the log) in two cases:
|
|
70
|
+
- OpenCode is a newer minor version than the `@opencode/plugin` installed with apex,
|
|
71
|
+
which means its `@opencode/*` packages are older than the host;
|
|
72
|
+
- it finds two copies of `effect` on disk.
|
|
73
|
+
4. Before every publish, `scripts/check-install.mjs` installs the package the way OpenCode
|
|
74
|
+
does and refuses to publish if the install is inconsistent or the plugin does not import.
|
|
84
75
|
|
|
85
|
-
|
|
76
|
+
**The one thing you have to do yourself:** OpenCode caches installed plugins and **does
|
|
77
|
+
not refresh them on its own**. After upgrading OpenCode, or to move to a new apex
|
|
78
|
+
release, force a reinstall:
|
|
86
79
|
|
|
87
|
-
|
|
80
|
+
1. Stop OpenCode: `opencode service stop` for the background service, or close the app.
|
|
81
|
+
2. Delete the cached copy: `rm -rf ~/.cache/opencode/npm/opencode-apex@*`.
|
|
82
|
+
3. Start OpenCode again. The first time a directory is used, it installs apex again
|
|
83
|
+
(this takes a few seconds).
|
|
88
84
|
|
|
89
|
-
|
|
85
|
+
In OpenCode 2.1.1, `opencode plugin update` only manages **TUI** plugins (those in
|
|
86
|
+
`cli.json`). For apex it answers `Plugin is not configured`, so use the steps above.
|
|
90
87
|
|
|
91
|
-
|
|
88
|
+
A breaking OpenCode 3.x is deliberately **not** covered by the `^2` ranges: it would need
|
|
89
|
+
code changes, not just new dependencies.
|
|
92
90
|
|
|
93
|
-
|
|
91
|
+
## RPC calls fail
|
|
94
92
|
|
|
95
|
-
|
|
96
|
-
2. **Re-exports type-only en cláusula de valor** (ver 3.3).
|
|
93
|
+
apex's RPC methods are HTTP endpoints of the OpenCode server:
|
|
97
94
|
|
|
98
|
-
**Protocolo de verificación obligatorio antes de dar un módulo por terminado:**
|
|
99
|
-
|
|
100
|
-
```bash
|
|
101
|
-
cd .opencode/plugins/apex
|
|
102
|
-
bun -e "await import('./index.ts'); console.log('OK')" # smoke de carga real
|
|
103
95
|
```
|
|
96
|
+
POST <server>/api/rpc/apex/<method>?location%5Bdirectory%5D=<url-encoded project dir>
|
|
97
|
+
Authorization: Basic <base64 of "opencode:<server password>">
|
|
98
|
+
Content-Type: application/json
|
|
104
99
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
```bash
|
|
108
|
-
bun -e "await import('./index.ts'); console.log('OK')" # smoke de carga real
|
|
109
|
-
bun x tsc --noEmit # typecheck
|
|
110
|
-
bun test test/guard.test.ts # guard
|
|
111
|
-
TEST_CDP_URL=<puerto> bun test test/browser.test.ts # browser (Chromium headless en ese puerto)
|
|
100
|
+
{"input": { … }}
|
|
112
101
|
```
|
|
113
102
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
103
|
+
The `apex_callback_endpoint` tool prints the exact URLs, and tells you where the
|
|
104
|
+
password is stored, for the current project.
|
|
105
|
+
|
|
106
|
+
| Response | Cause | Fix |
|
|
107
|
+
| --- | --- | --- |
|
|
108
|
+
| `401 Unauthorized` | No or wrong Basic auth | Username `opencode`, plus the server password (see `apex_callback_endpoint` → `auth.password`). |
|
|
109
|
+
| `rpc.unavailable` — "RPC is unavailable: apex" | apex is not loaded **in the directory you targeted**: it failed to load (see above), it is configured only for another project, or the location parameter is wrong | Check the log. Use `location[directory]=…` (URL-encoded `location%5Bdirectory%5D=`). `?directory=` is **ignored**. |
|
|
110
|
+
| The call works but acts on the wrong project | The location parameter was missing or misspelled, so the server used its **default** directory | Same fix: always send `location[directory]`. |
|
|
111
|
+
| `rpc.invalid_input` — e.g. `Missing key at ["payload"]` | The body does not match the method | The body must be `{"input": {…}}`. `events.ingest` needs `source` and `payload`; `tools.run` needs `tool`. |
|
|
112
|
+
| `rpc.unavailable` with apex **0.1.x** | 0.1.x registered its RPC under the wrong key and never worked on OpenCode 2.1 | Upgrade apex (see the reinstall steps). |
|
|
113
|
+
| `events.ingest` returns an `error` about the events module being disabled | `modules.events` is `false` | Turn the module on. |
|
|
114
|
+
|
|
115
|
+
## `events.ingest` succeeds but nobody wakes up
|
|
116
|
+
|
|
117
|
+
`{"accepted": true, "delivered": []}` means the event was received but **no subscription
|
|
118
|
+
matched it**.
|
|
119
|
+
|
|
120
|
+
- Check the subscription's `filters` against the event's `type` and `source`.
|
|
121
|
+
- A subscription with an empty `sessionID` is bound to the project's **main session**
|
|
122
|
+
(the root session you opened). If that session was deleted, there is nothing to
|
|
123
|
+
deliver to until a new root session appears. For long-running automations, give the
|
|
124
|
+
subscription an explicit `sessionID`.
|
|
125
|
+
- `deliverAs: "synthetic"` with `resume: false` only *stores* the message; the agent
|
|
126
|
+
reads it on its next turn. Use `deliverAs: "prompt"`, or `resume: true`, to wake it.
|
|
127
|
+
- Re-sending the same `eventID` within 5 minutes is deduplicated on purpose: the second
|
|
128
|
+
call returns `delivered: []`.
|
|
129
|
+
|
|
130
|
+
## Automations stop after an hour, or after a restart
|
|
131
|
+
|
|
132
|
+
This is expected OpenCode V2 behavior, not an apex bug. OpenCode loads plugins **per
|
|
133
|
+
project directory** and **unloads** a directory's plugins after **60 minutes without
|
|
134
|
+
session activity** there. The value is fixed in OpenCode 2.1.1. After a restart, nothing
|
|
135
|
+
is loaded until something uses that directory.
|
|
136
|
+
|
|
137
|
+
So nothing that runs *inside* the plugin survives on its own: timers, open ports,
|
|
138
|
+
watchers. apex 0.2.0 removed its HTTP listener for exactly this reason. What does
|
|
139
|
+
survive is the **RPC**: the server waits for the directory's plugins and loads them if
|
|
140
|
+
needed, as part of the call itself.
|
|
141
|
+
|
|
142
|
+
**Rule:** keep the clock (cron, systemd timer) and the door (webhook relay, n8n)
|
|
143
|
+
**outside** OpenCode, and have them call the RPC. The bundled `apex-callbacks` skill
|
|
144
|
+
explains the pattern step by step.
|
|
145
|
+
|
|
146
|
+
## The first call is slow
|
|
147
|
+
|
|
148
|
+
- **First use after installing or upgrading:** OpenCode downloads apex and its
|
|
149
|
+
dependencies from npm. Expect 20–30 s once.
|
|
150
|
+
- **First call to a directory that is not loaded** (idle unload or restart): the server
|
|
151
|
+
loads every plugin of that directory first, about 5 s. Later calls are fast.
|
|
152
|
+
- Scripts should use generous timeouts and retry with backoff on that first call.
|
|
153
|
+
|
|
154
|
+
## `tools.run` fails
|
|
155
|
+
|
|
156
|
+
| `error` | Cause | Fix |
|
|
157
|
+
| --- | --- | --- |
|
|
158
|
+
| `tool denied for tools.run (events.tools.deny): …` | The tool matches `events.tools.deny` (`question`, `subagent`, `execute` and `apex_*` by default) | Adjust `events.tools.deny` if you really need it. |
|
|
159
|
+
| `tool not in events.tools.allow: …` | The tool is not in `events.tools.allow` | Add it, or a glob such as `easypanel_*`. |
|
|
160
|
+
| `tool not registered in this project: …` | No tool with that exact id in this project, e.g. an MCP server not enabled there | Use the exact tool id as the agent sees it (MCP tools are usually `<server>_<tool>`). |
|
|
161
|
+
| `timed out after … ms` | The permission check answered "ask" and nobody approved it | Add an `allow` rule for that action to the agent in `events.tools.agent` (default `build`), or enable the apex advisor. |
|
|
162
|
+
| A generic message such as `Unable to write …` | The tool ran but was blocked by a permission rule (`deny`) or failed | Check that agent's permissions, and the log. |
|
|
163
|
+
| `tools.run is disabled (events.tools.enabled=false)` | Turned off in config | Set `events.tools.enabled: true`. |
|
|
164
|
+
|
|
165
|
+
## Configuration does not seem to apply
|
|
166
|
+
|
|
167
|
+
apex merges three sources. Each one overrides the previous:
|
|
168
|
+
|
|
169
|
+
1. `~/.config/opencode/opencode-apex.jsonc` — global file
|
|
170
|
+
2. the `options` object of the apex entry in `opencode.json` — global and project
|
|
171
|
+
`opencode.json` are already merged by OpenCode
|
|
172
|
+
3. `<project>/.opencode/opencode-apex.jsonc` — project file
|
|
173
|
+
|
|
174
|
+
- The `[apex] config: inline=… global=… local=…` log line shows which ones were found.
|
|
175
|
+
- An invalid config is reported (`[apex] … inválida`) and apex falls back to defaults.
|
|
176
|
+
- Keys that no longer exist are ignored with a warning: `events.enabled` (use
|
|
177
|
+
`modules.events`), `events.listener` (removed in 0.2.0).
|
|
178
|
+
- Modules are switched on and off **only** in `modules` (`"modules": { "guard": true }`).
|
|
179
|
+
- Config is read when the plugin loads: after editing, restart OpenCode or wait for the
|
|
180
|
+
directory to reload.
|
|
181
|
+
|
|
182
|
+
## The browser tools say "No desktop browser is connected"
|
|
183
|
+
|
|
184
|
+
See [BROWSER.md](BROWSER.md): a session only gets a browser after `browser_target` or
|
|
185
|
+
`/browser-target` asks for one.
|
|
117
186
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
| Dep | Uso |
|
|
121
|
-
|---|---|
|
|
122
|
-
| `effect` | Efectos/Scope del runtime `@opencode/plugin/effect` |
|
|
123
|
-
| `zod` | Validación de config (core/config.ts) |
|
|
124
|
-
| `jsonc-parser` | Parseo de la config (JSONC + `#`) |
|
|
125
|
-
| `ws` | Cliente CDP del módulo browser (browser/cdp.ts) |
|
|
126
|
-
| `@opencode/plugin-browser` | Protocolo `browser.*` (schemas Browser, RPC del host) |
|
|
187
|
+
---
|
|
127
188
|
|
|
128
|
-
|
|
189
|
+
# Developer notes
|
|
129
190
|
|
|
130
|
-
|
|
191
|
+
## Running a local checkout
|
|
131
192
|
|
|
132
|
-
|
|
193
|
+
Point OpenCode at the plugin **directory** (not a file):
|
|
133
194
|
|
|
134
|
-
|
|
195
|
+
```jsonc
|
|
196
|
+
// opencode.json
|
|
197
|
+
{ "plugins": [{ "package": "/path/to/opencode-apex" }] }
|
|
198
|
+
```
|
|
135
199
|
|
|
136
|
-
|
|
200
|
+
Then, in that directory:
|
|
137
201
|
|
|
138
202
|
```bash
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
"
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
## 6. Checklist para nuevos módulos del plugin
|
|
148
|
-
|
|
149
|
-
- [ ] Imports solo desde `@opencode/plugin` (v2) — nunca `@opencode-ai/plugin`
|
|
150
|
-
- [ ] `bun -e "await import('./<modulo>.ts')"` pasa desde el dir del plugin
|
|
151
|
-
- [ ] Exports usados por otros módulos existen (grep cruzado, no confiar en LSP)
|
|
152
|
-
- [ ] Re-exports de tipos separados (`export type {...}`)
|
|
153
|
-
- [ ] Deps runtime añadidas a package.json
|
|
154
|
-
- [ ] Smoke `bun -e "import('./index.ts')"` + `tsc --noEmit` + suites del módulo pasan
|
|
155
|
-
- [ ] Sin `as any` / `@ts-ignore` nuevos
|
|
156
|
-
- [ ] Sin `globalThis` hacks (usar exports de módulo)
|
|
157
|
-
|
|
158
|
-
---
|
|
203
|
+
bun install
|
|
204
|
+
npm run typecheck # tsc --noEmit
|
|
205
|
+
npm test # events + guard suites
|
|
206
|
+
npm run check:install # install like OpenCode does: one effect copy, plugin imports
|
|
207
|
+
bun -e "await import('./index.ts'); console.log('OK')" # load smoke test
|
|
208
|
+
TEST_CDP_URL=9333 bun test test/browser.test.ts # needs a Chromium with CDP on that port
|
|
209
|
+
```
|
|
159
210
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
-
|
|
170
|
-
- **
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
- **
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
- **
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
- **
|
|
186
|
-
|
|
187
|
-
- **
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
`Plugin is not configured` para los plugins de servidor: solo gestiona los de TUI (`cli.json`).
|
|
231
|
-
- **Rotura de API (OpenCode 3.x)**: el rango `^2` no la absorbe a propósito. Eso exige
|
|
232
|
-
revisar el código, no solo las dependencias.
|
|
211
|
+
`prepublishOnly` runs `test`, `typecheck` and `check:install`. A misaligned dependency
|
|
212
|
+
cannot be published.
|
|
213
|
+
|
|
214
|
+
To watch the plugin load, run a throwaway server in the foreground:
|
|
215
|
+
`opencode serve --port 4096 --print-logs --log-level info`.
|
|
216
|
+
|
|
217
|
+
## Rules that keep apex loading
|
|
218
|
+
|
|
219
|
+
- **V2 imports only:** `@opencode/plugin` and `@opencode/plugin/effect`, never the V1
|
|
220
|
+
package `@opencode-ai/plugin`.
|
|
221
|
+
- **No own `effect` version** (see the version mismatch section above). OpenCode serves
|
|
222
|
+
`effect` and `@opencode/plugin` to plugins. Import them freely, but never declare `effect`
|
|
223
|
+
as a regular dependency.
|
|
224
|
+
- **Keep `@opencode/plugin` current.** It is the package that pulls the `@opencode/schema`
|
|
225
|
+
and `@opencode/client` matching the host. Keep its range in step with the OpenCode
|
|
226
|
+
versions you support.
|
|
227
|
+
- **Tool and RPC schemas are plain JSON Schema**, not `Schema.Struct` from `effect`.
|
|
228
|
+
OpenCode validates them with its own `effect`. A schema object built by another copy
|
|
229
|
+
is not recognized, and the plugin is disabled with
|
|
230
|
+
`undefined is not an object (evaluating 'ast._tag')`.
|
|
231
|
+
- **The RPC definition needs an `id`** (`{ id: "apex", methods, events }`). OpenCode
|
|
232
|
+
registers RPCs by `id`.
|
|
233
|
+
- **The plugin must start with no configuration at all.** A missing config file means
|
|
234
|
+
defaults, and every field of the Zod schema in `core/config.ts` has a `.default()`.
|
|
235
|
+
Never add a required top-level field.
|
|
236
|
+
- **No per-directory state at module level.** The module is imported once per process
|
|
237
|
+
and registered once per directory, so mutable state belongs inside `register()`.
|
|
238
|
+
- **The event bus is global.** `ctx.event.subscribe()` delivers events from every
|
|
239
|
+
directory. Filter session events by `event.location.directory`.
|
|
240
|
+
|
|
241
|
+
## Effect 4, not 3
|
|
242
|
+
|
|
243
|
+
The API names changed: `Effect.catch` (not `catchAll`), `Effect.andThen` (not
|
|
244
|
+
`zipRight`), `Effect.promise`, `Stream.tap` / `Stream.runDrain` / `Effect.forkScoped`.
|
|
245
|
+
Each module exports `register(ctx, config): Effect<…, never, Scope>`. Registrations use
|
|
246
|
+
`yield* ctx.tool.transform(…)`, `ctx.session.hook(…)` or `ctx.permission.hook(…)`, and
|
|
247
|
+
cleanup goes in `Effect.addFinalizer`.
|
|
248
|
+
|
|
249
|
+
## Bun quirks
|
|
250
|
+
|
|
251
|
+
- **`bun -e` resolves bare imports from the current directory**, not from the imported
|
|
252
|
+
file. Run smoke tests from the plugin directory, or `effect` will not be found.
|
|
253
|
+
- **`bun install` can hang silently** when `package.json` references a package that does
|
|
254
|
+
not exist on npm. Check with `npm view <pkg> version` first.
|
|
255
|
+
- **Type-only re-exports must be marked:** `export { apexBus, ApexBus }` fails at run time
|
|
256
|
+
if `ApexBus` is a type. Write `export { apexBus }` plus `export type { ApexBus }`.
|
|
257
|
+
- **Stale `node_modules` symlinks** (for example to a temporary test directory) cause
|
|
258
|
+
`Not a directory` errors when loading. Remove them before debugging anything else.
|
|
259
|
+
|
|
260
|
+
## The editor will not catch these — the runtime will
|
|
261
|
+
|
|
262
|
+
The TypeScript language server reported zero errors on builds that failed to load:
|
|
263
|
+
|
|
264
|
+
1. importing a name that another file does not export;
|
|
265
|
+
2. type-only re-exports in a value position (see above).
|
|
266
|
+
|
|
267
|
+
Always run the load smoke test and `npm run typecheck` before calling a change done.
|
|
268
|
+
|
|
269
|
+
## Checklist for a new module
|
|
270
|
+
|
|
271
|
+
- [ ] Imports only from `@opencode/plugin` (V2)
|
|
272
|
+
- [ ] `bun -e "await import('./<module>.ts')"` passes from the plugin directory
|
|
273
|
+
- [ ] Names imported from other modules really exist (grep, do not trust the editor)
|
|
274
|
+
- [ ] Type re-exports use `export type { … }`
|
|
275
|
+
- [ ] New runtime dependencies are in `package.json`, and `npm run check:install` passes
|
|
276
|
+
- [ ] Tool and RPC schemas are JSON Schema
|
|
277
|
+
- [ ] No module-level per-directory state; events filtered by directory
|
|
278
|
+
- [ ] Config fields have defaults; the module is switched by `modules.<name>`
|
|
279
|
+
- [ ] `npm test` and `npm run typecheck` pass
|
|
280
|
+
- [ ] No new `as any`, `@ts-ignore` or `globalThis` hacks
|
package/events.ts
CHANGED
|
@@ -279,11 +279,13 @@ export function resolveTemplate(
|
|
|
279
279
|
return subscription.promptTemplate
|
|
280
280
|
}
|
|
281
281
|
|
|
282
|
-
function renderPromptTemplate(template: string, event: InternalEvent): string {
|
|
282
|
+
export function renderPromptTemplate(template: string, event: InternalEvent): string {
|
|
283
283
|
return template
|
|
284
284
|
.replace(/\{\{eventID\}\}/g, event.eventID)
|
|
285
285
|
.replace(/\{\{source\}\}/g, event.source)
|
|
286
286
|
.replace(/\{\{type\}\}/g, event.type ?? "")
|
|
287
|
+
// `{{payload}}` a secas: el payload entero en JSON (lo usa la plantilla por defecto).
|
|
288
|
+
.replace(/\{\{payload\}\}/g, () => JSON.stringify(event.payload))
|
|
287
289
|
.replace(/\{\{payload\.([^}]+)\}\}/g, (_match, path) => {
|
|
288
290
|
const value = path.split(".").reduce((acc: unknown, key: string) => {
|
|
289
291
|
return (acc as Record<string, unknown>)?.[key]
|
|
@@ -450,21 +452,21 @@ function registerCallbackEndpointTool(
|
|
|
450
452
|
editor.add({
|
|
451
453
|
name: "apex_callback_endpoint",
|
|
452
454
|
description:
|
|
453
|
-
"
|
|
454
|
-
"
|
|
455
|
-
"
|
|
456
|
-
"
|
|
455
|
+
"Returns how an external program (script, cron, systemd, n8n, webhook relay) reaches THIS OpenCode " +
|
|
456
|
+
"server: base URL, the apex RPC URLs for this project (events.ingest to notify events that wake an " +
|
|
457
|
+
"agent, tools.run to run OpenCode tools), Basic auth and where to read the password (never the " +
|
|
458
|
+
"password itself), plus curl examples. Call it before writing any external script or loop.",
|
|
457
459
|
input: { type: "object", properties: {}, additionalProperties: false },
|
|
458
460
|
output: {
|
|
459
461
|
type: "object",
|
|
460
462
|
properties: {
|
|
461
|
-
baseUrl: { type: ["string", "null"], description: "
|
|
462
|
-
source: { type: ["string", "null"], description: "
|
|
463
|
-
publicUrl: { type: ["string", "null"], description: "URL
|
|
464
|
-
directory: { type: "string", description: "
|
|
465
|
-
auth: { type: "object", description: "Basic opencode:<password>; password.shell
|
|
466
|
-
rpc: { type: "object", description: "URLs
|
|
467
|
-
examples: { type: "object", description: "curl
|
|
463
|
+
baseUrl: { type: ["string", "null"], description: "Server base URL, as reachable from this machine" },
|
|
464
|
+
source: { type: ["string", "null"], description: "Where baseUrl came from: config | service | argv" },
|
|
465
|
+
publicUrl: { type: ["string", "null"], description: "URL for senders on other machines (tunnel/proxy)" },
|
|
466
|
+
directory: { type: "string", description: "Value of location[directory] for every call" },
|
|
467
|
+
auth: { type: "object", description: "Basic opencode:<password>; password says where it is, password.shell prints it" },
|
|
468
|
+
rpc: { type: "object", description: "Full URLs: ingest, toolsRun, status, ping" },
|
|
469
|
+
examples: { type: "object", description: "Ready-to-run curl for ingest and toolsRun" },
|
|
468
470
|
hint: { type: "string" },
|
|
469
471
|
},
|
|
470
472
|
additionalProperties: true,
|
|
@@ -506,15 +508,15 @@ function registerSetCallbackUrlTool(
|
|
|
506
508
|
editor.add({
|
|
507
509
|
name: "apex_set_callback_url",
|
|
508
510
|
description:
|
|
509
|
-
"
|
|
510
|
-
"
|
|
511
|
-
"
|
|
511
|
+
"Sets (or clears) the public URL through which programs on OTHER machines reach this OpenCode " +
|
|
512
|
+
"server, e.g. a tunnel (cloudflared/ngrok) pointing at its base URL. It is persisted and shown by " +
|
|
513
|
+
"apex_callback_endpoint. Pass null to clear it. Precedence: runtime > config.",
|
|
512
514
|
input: {
|
|
513
515
|
type: "object",
|
|
514
516
|
properties: {
|
|
515
517
|
url: {
|
|
516
518
|
type: ["string", "null"],
|
|
517
|
-
description: "URL
|
|
519
|
+
description: "Public base URL of the server (e.g. https://x.trycloudflare.com) or null to clear",
|
|
518
520
|
},
|
|
519
521
|
},
|
|
520
522
|
required: ["url"],
|
|
@@ -569,8 +571,8 @@ function registerSubscribeTool(ctx: Plugin.Context): Effect.Effect<void, never,
|
|
|
569
571
|
additionalProperties: false,
|
|
570
572
|
},
|
|
571
573
|
},
|
|
572
|
-
agent: { type: "string", description: "
|
|
573
|
-
promptTemplate: { type: "string", description: "Inline prompt template with {{payload.x}}, {{source}}, {{type}}, {{eventID}} placeholders" },
|
|
574
|
+
agent: { type: "string", description: "Agent to switch the target session to before delivering (the switch persists)" },
|
|
575
|
+
promptTemplate: { type: "string", description: "Inline prompt template with {{payload}}, {{payload.x}}, {{source}}, {{type}}, {{eventID}} placeholders" },
|
|
574
576
|
promptTemplateFile: { type: "string", description: "Path to a prompt template file (takes priority over promptTemplate)" },
|
|
575
577
|
deliverAs: { type: "string", enum: ["synthetic", "prompt"], description: "Deliver as synthetic message or user prompt (default prompt)" },
|
|
576
578
|
resume: { type: "boolean", description: "Synthetic only: wake the agent loop so the model processes the message (default false = passive)" },
|
|
@@ -674,7 +676,10 @@ function registerUnsubscribeTool(ctx: Plugin.Context): Effect.Effect<void, never
|
|
|
674
676
|
}
|
|
675
677
|
|
|
676
678
|
/** Tool: apex_list_subscriptions — list all subscriptions (static marked readonly) */
|
|
677
|
-
function registerListSubscriptionsTool(
|
|
679
|
+
function registerListSubscriptionsTool(
|
|
680
|
+
ctx: Plugin.Context,
|
|
681
|
+
staticSubscriptions: EventSubscription[],
|
|
682
|
+
): Effect.Effect<void, never, Scope.Scope> {
|
|
678
683
|
return Effect.gen(function* () {
|
|
679
684
|
yield* ctx.tool.transform((editor) => {
|
|
680
685
|
editor.add({
|
|
@@ -690,7 +695,8 @@ function registerListSubscriptionsTool(ctx: Plugin.Context): Effect.Effect<void,
|
|
|
690
695
|
},
|
|
691
696
|
execute: () =>
|
|
692
697
|
Effect.promise(async () => {
|
|
693
|
-
|
|
698
|
+
// Las de config (readonly: true) primero, igual que en la entrega.
|
|
699
|
+
const subscriptions = [...staticSubscriptions, ...(await loadSubscriptions(ctx))]
|
|
694
700
|
return { output: { subscriptions } }
|
|
695
701
|
}),
|
|
696
702
|
})
|
|
@@ -850,7 +856,7 @@ export function register(
|
|
|
850
856
|
// Register tools
|
|
851
857
|
yield* registerSubscribeTool(ctx)
|
|
852
858
|
yield* registerUnsubscribeTool(ctx)
|
|
853
|
-
yield* registerListSubscriptionsTool(ctx)
|
|
859
|
+
yield* registerListSubscriptionsTool(ctx, staticSubscriptions)
|
|
854
860
|
|
|
855
861
|
// Dirección del servidor: se resuelve en cada consulta (el servicio puede haber
|
|
856
862
|
// reescrito su registro tras un reinicio).
|
|
@@ -874,7 +880,7 @@ export function register(
|
|
|
874
880
|
|
|
875
881
|
const current = endpoint()
|
|
876
882
|
console.log(
|
|
877
|
-
`[Apex Events] Event system initialized · RPC ${current.baseUrl ?? "(URL
|
|
883
|
+
`[Apex Events] Event system initialized · RPC ${current.baseUrl ?? "(server URL unknown)"} · ` +
|
|
878
884
|
`tools.run ${eventsConfig.tools.enabled ? `on (agente ${eventsConfig.tools.agent})` : "off"}`,
|
|
879
885
|
)
|
|
880
886
|
|