opencode-apex 0.2.1 → 0.2.2
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 +80 -48
- package/LICENSE +21 -0
- package/README.md +299 -151
- package/browser/endpoint.ts +4 -1
- package/browser.ts +2 -1
- package/core/compat.ts +6 -6
- 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 +206 -89
- package/docs/GUARD.md +142 -104
- package/docs/REDACT.md +143 -89
- package/docs/TROUBLESHOOTING.md +225 -190
- 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 +129 -123
package/docs/CONFIG.md
CHANGED
|
@@ -1,97 +1,187 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Configuring apex
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **OpenCode V2 only (2.1 or newer).** apex is built on the OpenCode V2 plugin API
|
|
4
|
+
> (`@opencode/plugin`). It does not load in OpenCode V1.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
This page covers where apex's settings live, which source wins when several define the
|
|
7
|
+
same key, and the details that trip people up: environment variables, comments, merge
|
|
8
|
+
rules and prompt template files. Every key, with its default, is listed in the annotated
|
|
9
|
+
template [`opencode-apex.jsonc`](../opencode-apex.jsonc).
|
|
10
|
+
|
|
11
|
+
## Background: how OpenCode loads a plugin
|
|
12
|
+
|
|
13
|
+
An OpenCode *plugin* is a package that OpenCode loads into its server process to add
|
|
14
|
+
tools, hooks and agents. You enable it in OpenCode's own config file, `opencode.json`, in
|
|
15
|
+
the `plugins` list:
|
|
9
16
|
|
|
10
17
|
```jsonc
|
|
11
18
|
// opencode.json
|
|
12
19
|
{
|
|
13
20
|
"plugins": [
|
|
14
|
-
{
|
|
21
|
+
{
|
|
22
|
+
"package": "opencode-apex", // npm package name (or a local directory path)
|
|
23
|
+
"options": { "modules": { "events": true } } // handed to the plugin as-is
|
|
24
|
+
}
|
|
15
25
|
]
|
|
16
26
|
}
|
|
17
27
|
```
|
|
18
28
|
|
|
19
|
-
|
|
20
|
-
|
|
29
|
+
OpenCode reads `opencode.json` from two places:
|
|
30
|
+
|
|
31
|
+
| File | Applies to |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `~/.config/opencode/opencode.json` (global) | every project |
|
|
34
|
+
| `<project>/opencode.json` (project) | that project only |
|
|
35
|
+
|
|
36
|
+
When both exist, OpenCode merges them itself (the project file wins) before the plugin
|
|
37
|
+
sees anything. On first use OpenCode downloads the npm package and its dependencies into
|
|
38
|
+
its own cache (`~/.cache/opencode/npm/`); you do not run `npm install` yourself.
|
|
39
|
+
|
|
40
|
+
## The three places apex reads settings from
|
|
41
|
+
|
|
42
|
+
apex accepts its settings through two *channels*:
|
|
21
43
|
|
|
22
|
-
|
|
44
|
+
1. **Its own config files**, named `opencode-apex.jsonc`:
|
|
45
|
+
- **global**: `~/.config/opencode/opencode-apex.jsonc` (`$XDG_CONFIG_HOME` is
|
|
46
|
+
honoured: `$XDG_CONFIG_HOME/opencode/opencode-apex.jsonc`);
|
|
47
|
+
- **local**: `<project>/.opencode/opencode-apex.jsonc`.
|
|
48
|
+
2. **Inline options**: the `options` object of the apex entry in `opencode.json`
|
|
49
|
+
(example above).
|
|
23
50
|
|
|
24
|
-
|
|
51
|
+
All three are optional. With none of them, apex starts with the defaults.
|
|
25
52
|
|
|
26
|
-
|
|
27
|
-
| - | ------------------------------------------------------------- | ------------- |
|
|
28
|
-
| 1 | Fichero global `~/.config/opencode/opencode-apex.jsonc` | global-file |
|
|
29
|
-
| 2 | `options` del plugin en opencode.json | inline |
|
|
30
|
-
| 3 | Fichero local `<proyecto>/.opencode/opencode-apex.jsonc` | local-file |
|
|
53
|
+
### Precedence
|
|
31
54
|
|
|
32
|
-
|
|
33
|
-
fichero gana a las opciones inline**. De ahí que `local-inline > global-file`.
|
|
55
|
+
From **lowest to highest** priority:
|
|
34
56
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
57
|
+
| # | Source | Level |
|
|
58
|
+
| - | --- | --- |
|
|
59
|
+
| 1 | Global file `~/.config/opencode/opencode-apex.jsonc` | global file |
|
|
60
|
+
| 2 | Inline `options` in `opencode.json` | inline |
|
|
61
|
+
| 3 | Local file `<project>/.opencode/opencode-apex.jsonc` | local file |
|
|
40
62
|
|
|
41
|
-
|
|
63
|
+
The idea: **scope wins** (project over global), and **within a scope the file wins over
|
|
64
|
+
the inline options**. The result is that the inline options override the global file
|
|
65
|
+
but are overridden by the local file.
|
|
42
66
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
67
|
+
> **Limitation.** OpenCode hands the plugin a single, already-merged `options` object
|
|
68
|
+
> (global `opencode.json` + project `opencode.json`). apex cannot tell which of the two an
|
|
69
|
+
> inline option came from, so all inline options sit at level 2. If you need fine control
|
|
70
|
+
> between global and project settings, use the two `opencode-apex.jsonc` files.
|
|
46
71
|
|
|
47
|
-
|
|
72
|
+
### How sources are merged
|
|
48
73
|
|
|
49
|
-
|
|
50
|
-
`
|
|
51
|
-
|
|
52
|
-
(
|
|
74
|
+
- Objects are merged key by key, recursively.
|
|
75
|
+
- **Arrays are replaced, not concatenated.** A `bundles` or `patterns` list in a
|
|
76
|
+
higher-priority source replaces the whole list from a lower one.
|
|
77
|
+
- A key that is absent (or `undefined`) never erases a value from a lower source.
|
|
78
|
+
|
|
79
|
+
## Turning modules on and off
|
|
80
|
+
|
|
81
|
+
Each module has exactly one switch, under `modules`:
|
|
53
82
|
|
|
54
83
|
```jsonc
|
|
55
|
-
"modules": { "redact": true, "events": true, "advisor":
|
|
84
|
+
"modules": { "redact": true, "events": true, "advisor": true, "guard": false, "browser": true }
|
|
56
85
|
```
|
|
57
86
|
|
|
58
|
-
|
|
87
|
+
| Module | Default when the key is omitted |
|
|
88
|
+
| --- | --- |
|
|
89
|
+
| `redact` | on |
|
|
90
|
+
| `events` | on |
|
|
91
|
+
| `advisor` | on |
|
|
92
|
+
| `guard` | **off** (it can block the agent if its rules are wrong, so you opt in) |
|
|
93
|
+
| `browser` | on |
|
|
94
|
+
|
|
95
|
+
There is no `<module>.enabled` key. A legacy `events.enabled` is ignored and logs a
|
|
96
|
+
warning. So does `events.listener`: the HTTP listener was removed in 0.2.0, see
|
|
97
|
+
[EVENTS.md](EVENTS.md).
|
|
98
|
+
|
|
99
|
+
## Environment variables
|
|
100
|
+
|
|
101
|
+
Two different substitution mechanisms exist, depending on which file the value is in:
|
|
102
|
+
|
|
103
|
+
| Where the value is written | Syntax | Done by | Missing variable |
|
|
104
|
+
| --- | --- | --- | --- |
|
|
105
|
+
| `opencode.json` (inline options) | `{env:VAR}`, `{file:path}` | OpenCode | replaced by an empty string |
|
|
106
|
+
| `opencode-apex.jsonc` files (and inline values too) | `${VAR}`, `${VAR:-default}`, `$VAR` | apex | `${VAR}`: error · `${VAR:-x}`: `x` · `$VAR`: left as written |
|
|
59
107
|
|
|
60
|
-
|
|
108
|
+
- apex does **not** understand `{env:VAR}`. That is OpenCode syntax and only works inside
|
|
109
|
+
`opencode.json`.
|
|
110
|
+
- apex expands `$NAME` in every string, including regexes in `redact.patterns`. A `$`
|
|
111
|
+
followed by letters is replaced if an environment variable with that name exists.
|
|
112
|
+
- A `${VAR}` without a default whose variable is not set makes the whole configuration
|
|
113
|
+
invalid. apex logs a warning, retries without the inline options and, if it still
|
|
114
|
+
fails, **starts with all defaults**. Use `${VAR:-}` when an empty fallback is acceptable.
|
|
115
|
+
|
|
116
|
+
The redaction key has its own `env:VAR` / `file:/path` forms. They are resolved by the
|
|
117
|
+
redact module, not by this mechanism. See [REDACT.md](REDACT.md#the-encryption-key).
|
|
118
|
+
|
|
119
|
+
## Comments and syntax
|
|
120
|
+
|
|
121
|
+
The apex files are JSON with comments (JSONC). Three comment styles are accepted:
|
|
122
|
+
`// line`, `/* block */` and `# line` (a `#` inside a string is kept). Trailing commas are
|
|
123
|
+
allowed.
|
|
61
124
|
|
|
62
125
|
```jsonc
|
|
63
126
|
{
|
|
64
|
-
#
|
|
127
|
+
# hash comment
|
|
65
128
|
"redact": {
|
|
66
|
-
"prefix": "[SECRET:", //
|
|
67
|
-
/*
|
|
129
|
+
"prefix": "[SECRET:", // line comment
|
|
130
|
+
/* block comment */
|
|
68
131
|
"suffix": "]"
|
|
69
132
|
}
|
|
70
133
|
}
|
|
71
134
|
```
|
|
72
135
|
|
|
73
|
-
|
|
136
|
+
Two JSON rules that bite:
|
|
137
|
+
|
|
138
|
+
- **Strings cannot span lines.** Write line breaks as `\n`:
|
|
139
|
+
`"promptTemplate": "first line\nsecond line"`.
|
|
140
|
+
- **Backslashes must be doubled in regexes**: the regex `\b` is written `"\\b"`
|
|
141
|
+
(`"\b"` is a backspace character in JSON).
|
|
142
|
+
|
|
143
|
+
> **The parser is fault-tolerant.** A syntax error does not stop apex. It logs
|
|
144
|
+
> `[apex] config syntax errors in <file> (<error> @ line N)` and uses whatever part of the
|
|
145
|
+
> file it could parse. A string with a raw line break, for example, is cut at the first
|
|
146
|
+
> line, so the affected keys may be missing. A file that does not parse to an object at
|
|
147
|
+
> all is ignored, with a warning. If a setting seems to have no effect, look for that log
|
|
148
|
+
> line.
|
|
149
|
+
|
|
150
|
+
## Prompt template files
|
|
74
151
|
|
|
75
|
-
|
|
76
|
-
`
|
|
152
|
+
Used by `events.subscriptions[].promptTemplateFile` and `advisor.promptTemplateFile`. A
|
|
153
|
+
template is the text sent to the agent, with placeholders such as `{{payload.x}}` (events)
|
|
154
|
+
or `{{requestID}}` (advisor).
|
|
77
155
|
|
|
78
|
-
- **
|
|
79
|
-
- `prompts/
|
|
80
|
-
- `~/
|
|
81
|
-
- **
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
156
|
+
- **Only relative paths** are accepted:
|
|
157
|
+
- `prompts/event.md` is relative to the **project** directory;
|
|
158
|
+
- `~/templates/event.md` is relative to your **home** directory.
|
|
159
|
+
- **Absolute paths are rejected**, and so is any path that escapes its base directory
|
|
160
|
+
with `../`. apex logs a warning and falls back to the inline `promptTemplate`.
|
|
161
|
+
- **If the file does not exist, apex creates it** (and any missing directories) with the
|
|
162
|
+
module's default template, so you get a starting point to edit. For the advisor, the
|
|
163
|
+
file is created with your inline `promptTemplate` if you set one.
|
|
164
|
+
- Priority: file › inline `promptTemplate` › the module's built-in default.
|
|
85
165
|
|
|
86
|
-
##
|
|
166
|
+
## Startup behaviour
|
|
87
167
|
|
|
88
|
-
|
|
168
|
+
apex always starts, whatever the configuration:
|
|
169
|
+
|
|
170
|
+
| Situation | What happens |
|
|
171
|
+
| --- | --- |
|
|
172
|
+
| No config at all | All defaults |
|
|
173
|
+
| A config file is missing | Treated as empty |
|
|
174
|
+
| A file cannot be read, or does not parse to an object | That file is ignored, with a warning |
|
|
175
|
+
| A value has the wrong type or an unknown enum value | Warning; retry without inline options, then all defaults |
|
|
176
|
+
| An unknown key | Silently dropped |
|
|
177
|
+
|
|
178
|
+
The server log shows which sources were found, for example
|
|
179
|
+
`[apex] config: inline=true global=false local=false · global=<path> · local=<path>`.
|
|
180
|
+
|
|
181
|
+
## Migrating from the old file name
|
|
182
|
+
|
|
183
|
+
The old location `<project>/apex.jsonc` is **no longer read**. Move it:
|
|
89
184
|
|
|
90
185
|
```bash
|
|
91
186
|
mv apex.jsonc .opencode/opencode-apex.jsonc
|
|
92
187
|
```
|
|
93
|
-
|
|
94
|
-
## Arranque
|
|
95
|
-
|
|
96
|
-
El plugin **siempre** arranca: sin ficheros usa los defaults del esquema. Una
|
|
97
|
-
fuente con sintaxis inválida se ignora con aviso (las demás siguen aplicando).
|
package/docs/EVENTS.md
CHANGED
|
@@ -1,145 +1,262 @@
|
|
|
1
|
-
# Apex Events —
|
|
1
|
+
# Apex Events — callbacks, webhooks, subscriptions and `tools.run`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **OpenCode V2 only (≥ 2.1).** apex is built on the V2 plugin runtime; it does not load in
|
|
4
|
+
> OpenCode V1.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
The events module lets something **outside** a conversation wake an agent up: a cron job
|
|
7
|
+
that detects a failure, a CI pipeline that finished, a webhook from GitHub. It also lets
|
|
8
|
+
those outside processes **run OpenCode tools** (including MCP tools) without a model.
|
|
9
|
+
|
|
10
|
+
## Concepts (read this first)
|
|
11
|
+
|
|
12
|
+
| Term | What it means here |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| **OpenCode server** | The process that runs your agents (`opencode serve`, or the background service the desktop app/TUI starts). It exposes an HTTP API on a local URL such as `http://127.0.0.1:49374`, protected with HTTP Basic auth. |
|
|
15
|
+
| **Session** | One conversation with an agent. Sessions live in the server, and each one belongs to a project directory. |
|
|
16
|
+
| **Event** | A small JSON message you send to apex: `{source, type, payload}`. Example: `{"source":"monitor","type":"server.down","payload":{"host":"example.com"}}`. |
|
|
17
|
+
| **Subscription** | A rule stored by apex: "when an event matches these filters, deliver it to this session (optionally switching to this agent), using this prompt". Delivering the event is what wakes the agent up. |
|
|
18
|
+
| **RPC** | A way to call a function that a plugin registered inside the OpenCode server, over plain HTTP. For apex it is an authenticated `POST` (details below). |
|
|
19
|
+
| **`location[directory]`** | Every RPC call names the **project directory** it is for. OpenCode keeps plugins, tools, MCP servers and stored subscriptions separately per directory, so the same server can host several projects; this parameter decides which one handles the call. |
|
|
20
|
+
|
|
21
|
+
An RPC call to apex looks like this:
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
POST <baseUrl>/api/rpc/apex/<method>?location[directory]=<project directory>
|
|
25
|
+
Authorization: Basic base64("opencode:<server password>")
|
|
26
|
+
Content-Type: application/json
|
|
27
|
+
|
|
28
|
+
{"input": { ...method arguments... }}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The answer is `{"output": { ...result... }}`. In `curl`, the brackets must be encoded
|
|
32
|
+
(`location%5Bdirectory%5D=`) and the directory URL-encoded. You do not need to build these
|
|
33
|
+
URLs by hand: the tool `apex_callback_endpoint` returns them ready to use (see
|
|
34
|
+
["Finding the server address"](#finding-the-server-address)).
|
|
35
|
+
|
|
36
|
+
The overall flow:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
1. An agent subscribes to an event → tool apex_subscribe (or static config)
|
|
40
|
+
2. An outside process calls apex/events.ingest → cron script, CI step, webhook relay
|
|
41
|
+
3. apex delivers the event to the session → the agent receives a prompt and acts
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Subscriptions
|
|
45
|
+
|
|
46
|
+
Each subscription filters events (`filters`) and delivers the matching ones to a session,
|
|
47
|
+
either as a **prompt** (a normal user message: the agent starts a turn) or as a
|
|
48
|
+
**synthetic** message (a system-style message; it only starts a turn with `resume: true`).
|
|
49
|
+
|
|
50
|
+
Subscriptions come from two places:
|
|
51
|
+
|
|
52
|
+
- **Dynamic**: created by an agent with the tool `apex_subscribe`. They are bound to the
|
|
53
|
+
session that called the tool and stored in the plugin storage (they survive restarts).
|
|
54
|
+
- **Static**: declared in the apex config under `events.subscriptions`. They get the ids
|
|
55
|
+
`static-0`, `static-1`… and are read-only (`apex_unsubscribe` refuses them).
|
|
7
56
|
|
|
8
57
|
```jsonc
|
|
9
58
|
"events": {
|
|
10
|
-
"routing": "all", // "all" (
|
|
59
|
+
"routing": "all", // "all" (default: every match) | "first" (only the highest priority match)
|
|
11
60
|
"subscriptions": [
|
|
12
61
|
{
|
|
13
|
-
"sessionID": "", //
|
|
62
|
+
"sessionID": "", // empty/null/missing → AUTO (see below); "ses_…" → that session
|
|
63
|
+
"agent": "plan", // optional: switch the target session to this agent before delivering
|
|
14
64
|
"filters": [{ "field": "type", "operator": "equals", "value": "ci.failed" }],
|
|
15
65
|
"promptTemplateFile": "prompts/ci-failure.md",
|
|
16
|
-
"deliverAs": "prompt",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
66
|
+
"deliverAs": "prompt", // "prompt" (default) | "synthetic"
|
|
67
|
+
"resume": false, // synthetic only: true wakes the agent; false leaves it passive
|
|
68
|
+
"delivery": "queue", // "queue" (default: wait for the current turn) | "steer" (inject into the current turn)
|
|
69
|
+
"priority": 10, // higher first (default 0)
|
|
70
|
+
"enabled": true // default true
|
|
20
71
|
}
|
|
21
72
|
]
|
|
22
73
|
}
|
|
23
74
|
```
|
|
24
75
|
|
|
25
|
-
|
|
76
|
+
### Fields
|
|
26
77
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
`
|
|
78
|
+
| Field | Default | Meaning |
|
|
79
|
+
| --- | --- | --- |
|
|
80
|
+
| `sessionID` | auto | Target session. Empty/null/missing binds to the **main session** (see below). |
|
|
81
|
+
| `agent` | — | If set, the target session is switched to this agent (`session.switchAgent`) before the event is delivered. The switch stays after the delivery. |
|
|
82
|
+
| `filters` | `[]` | All conditions must hold. **No filters = the subscription matches every event.** |
|
|
83
|
+
| `promptTemplate` | — | Inline prompt text with placeholders (below). |
|
|
84
|
+
| `promptTemplateFile` | — | Prompt file; wins over `promptTemplate`. Relative paths only: to the project (`prompts/x.md`) or to your home (`~/x.md`). Absolute paths and `..` escapes are rejected. If the file does not exist it is created with a default template. |
|
|
85
|
+
| `deliverAs` | `"prompt"` | `"prompt"` = user message (wakes the agent); `"synthetic"` = synthetic message. |
|
|
86
|
+
| `resume` | `false` | Only for `synthetic`: `true` wakes the agent loop, `false` just leaves the message in the session until its next turn. |
|
|
87
|
+
| `delivery` | `"queue"` | `"queue"` waits for the current turn to end; `"steer"` injects the message into the turn in progress. |
|
|
88
|
+
| `priority` | `0` | Order of delivery; with `routing: "first"` only the highest one is delivered. |
|
|
89
|
+
| `enabled` | `true` | Disabled subscriptions never match. |
|
|
30
90
|
|
|
31
|
-
|
|
32
|
-
- sesiones de **subagentes** (tienen `parentID`),
|
|
33
|
-
- la sesión del **advisor** (`agent === "apex-advisor"`),
|
|
34
|
-
- la sesión **runner** de `tools.run` (`agent === "apex-runner"`).
|
|
91
|
+
### Filters
|
|
35
92
|
|
|
36
|
-
|
|
93
|
+
Each filter is `{field, operator, value?, pattern?}`. `field` is a dot path into the event:
|
|
94
|
+
`source`, `type`, `eventID`, `timestamp`, `location` (the project directory) or
|
|
95
|
+
`payload.<path>` (for example `payload.branch`).
|
|
37
96
|
|
|
38
|
-
|
|
97
|
+
| Operator | True when |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| `equals` | the value is strictly equal to `value` |
|
|
100
|
+
| `contains` | the string contains `value`, or the array includes `value` |
|
|
101
|
+
| `matches` | the string matches the regular expression in **`pattern`** (not `value`) |
|
|
102
|
+
| `gt` / `lt` | greater / lower than `value` (numbers) |
|
|
103
|
+
| `in` / `notIn` | the value is / is not one of the items of the array `value` (exact comparison, no wildcards) |
|
|
104
|
+
| `exists` / `notExists` | the field is present and not null / missing or null |
|
|
39
105
|
|
|
40
|
-
|
|
41
|
-
| ----------------- | ---------------------------------- | --------------------------------------------- |
|
|
42
|
-
| `session.created` | sin `parentID`, ni advisor ni runner | vincula (y persiste en storage) |
|
|
43
|
-
| `session.deleted` | es la sesión vinculada | **desvincula** (auto-desuscripción) y persiste |
|
|
44
|
-
| (arranque) | binding persistido | se restaura si la sesión sigue viva; si no, se limpia |
|
|
106
|
+
### Prompt templates
|
|
45
107
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
108
|
+
Placeholders: `{{eventID}}`, `{{source}}`, `{{type}}`, `{{payload}}` (the whole payload as
|
|
109
|
+
JSON) and `{{payload.<path>}}` (strings are inserted as-is, other values as JSON). Without any
|
|
110
|
+
template the agent receives the whole event as JSON (`eventID`, `source`, `type`, `payload`,
|
|
111
|
+
`timestamp`).
|
|
49
112
|
|
|
50
|
-
|
|
51
|
-
activa), **no se entrega** y se registra un aviso (no se rompe el ingest).
|
|
113
|
+
## Auto-binding to the main session
|
|
52
114
|
|
|
53
|
-
|
|
115
|
+
If `sessionID` is **empty, null or missing**, the subscription is delivered to the
|
|
116
|
+
**active main session**: the latest **root** session (no `parentID`) created in this
|
|
117
|
+
project directory — normally the one you open when you start working.
|
|
54
118
|
|
|
55
|
-
|
|
119
|
+
It is never bound to:
|
|
56
120
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
121
|
+
- **subagent** sessions (they have a `parentID`),
|
|
122
|
+
- the **advisor** session (`agent === "apex-advisor"`),
|
|
123
|
+
- the **runner** session of `tools.run` (`agent === "apex-runner"`),
|
|
124
|
+
- sessions of **another project directory** (OpenCode's event bus is shared by all
|
|
125
|
+
directories; apex ignores events whose `location` is not its own directory).
|
|
126
|
+
|
|
127
|
+
### How it works
|
|
128
|
+
|
|
129
|
+
The module watches OpenCode's event stream:
|
|
130
|
+
|
|
131
|
+
| Event | Condition | Action |
|
|
132
|
+
| --- | --- | --- |
|
|
133
|
+
| `session.created` | root session, not advisor nor runner, same directory | bind it (and persist the binding) |
|
|
134
|
+
| `session.deleted` | it is the bound session | **unbind** (and persist) |
|
|
135
|
+
| (plugin load) | a persisted binding exists | restored if the session still exists; otherwise cleared |
|
|
136
|
+
|
|
137
|
+
When **another** root session is created it becomes the main session (the binding follows
|
|
138
|
+
the latest one). After the bound session is deleted, auto subscriptions have no target
|
|
139
|
+
until a new root session appears.
|
|
140
|
+
|
|
141
|
+
If a subscription has no resolvable target (auto and no main session), the event is **not
|
|
142
|
+
delivered** to it and a warning is logged; the ingest call itself still succeeds. If you
|
|
143
|
+
need delivery to one specific session even after you open new ones, set `sessionID`
|
|
144
|
+
explicitly.
|
|
145
|
+
|
|
146
|
+
## Entry point: the server RPC
|
|
62
147
|
|
|
63
|
-
|
|
148
|
+
Everything enters through OpenCode's RPC, per project directory:
|
|
149
|
+
|
|
150
|
+
| Method | Input | Output |
|
|
64
151
|
| --- | --- | --- |
|
|
65
|
-
| `events.ingest` | `{source, type?, payload, eventID?}` | `{accepted, eventID, delivered[]}`
|
|
66
|
-
| `tools.run` | `{tool, input?, agent?, timeoutMs?}` | `{ok, tool, agent
|
|
67
|
-
| `server.info` | `{}` |
|
|
68
|
-
| `status`
|
|
152
|
+
| `events.ingest` | `{source, type?, payload, eventID?}` | `{accepted, eventID, delivered[]}` |
|
|
153
|
+
| `tools.run` | `{tool, input?, agent?, timeoutMs?}` | `{ok, tool, agent?, output?, content?, metadata?, error?, durationMs}` |
|
|
154
|
+
| `server.info` | `{}` | address and auth of this server (see below) |
|
|
155
|
+
| `status` | `{}` | `{modules, version, uptimeMs, endpoint}` |
|
|
156
|
+
| `ping` | `{payload?}` | `{pong, timestamp}` (`pong` echoes `payload`, default `"pong"`) |
|
|
157
|
+
|
|
158
|
+
`events.ingest` details:
|
|
69
159
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
`
|
|
160
|
+
- `source` (string) and `payload` (object) are required. A call without them is rejected by
|
|
161
|
+
OpenCode itself with HTTP 400 and `{"_tag":"RpcError","type":"rpc.invalid_input",…}`.
|
|
162
|
+
- `eventID` is optional; one is generated if missing. The same `eventID` received again
|
|
163
|
+
within **5 minutes** is accepted but not delivered again (`delivered: []`), so retries are
|
|
164
|
+
safe. This memory is per directory and is lost if OpenCode unloads the plugin.
|
|
165
|
+
- `delivered` lists the ids of the subscriptions that received the event. An empty list
|
|
166
|
+
means no subscription matched, none had a target session, the delivery failed, or the
|
|
167
|
+
`eventID` was a duplicate.
|
|
168
|
+
- Any other failure (for example the events module is disabled) comes back as
|
|
169
|
+
`{"output":{"accepted":false, …, "error":"…"}}`.
|
|
76
170
|
|
|
77
|
-
|
|
78
|
-
|
|
171
|
+
**Why there is no HTTP listener of its own.** OpenCode unloads the plugins of a directory
|
|
172
|
+
after **60 minutes without session activity** (hard-coded in OpenCode, not configurable),
|
|
173
|
+
and after a restart it does not load them until something uses that directory. A listener
|
|
174
|
+
or a timer inside the plugin would silently die with it. An RPC call, instead, waits until
|
|
175
|
+
the plugins of its directory are active and loads the directory on demand (a few seconds
|
|
176
|
+
on the first call; longer the very first time, while OpenCode installs the plugin from
|
|
177
|
+
npm). Since 0.2.0, `events.listener` is ignored with a warning.
|
|
79
178
|
|
|
80
|
-
|
|
179
|
+
The 0.1.x RPC did not work on OpenCode ≥ 2.1 either: it was registered with `name` while V2
|
|
180
|
+
looks plugins up by `id` (every call returned `rpc.unavailable`).
|
|
81
181
|
|
|
82
|
-
|
|
83
|
-
proceso externo a ESTE servidor:
|
|
182
|
+
## Finding the server address
|
|
84
183
|
|
|
85
|
-
|
|
184
|
+
The tool **`apex_callback_endpoint`** (and the RPC `server.info`) tells an agent how an
|
|
185
|
+
outside process reaches THIS server:
|
|
186
|
+
|
|
187
|
+
| Field | Content |
|
|
86
188
|
| --- | --- |
|
|
87
|
-
| `baseUrl` · `source` | URL
|
|
88
|
-
| `rpc
|
|
89
|
-
| `auth` |
|
|
90
|
-
| `publicUrl` | URL
|
|
91
|
-
| `examples` | curl
|
|
189
|
+
| `baseUrl` · `source` | Base URL and where it came from, in this order: `config` (`events.server.url`) > `service` (the background service record `<state>/opencode/service*.json` whose `pid` is this process; `<state>` is `$XDG_STATE_HOME` or `~/.local/state`) > `argv` (the `--hostname`/`--port` of `opencode serve`). `null` if none applies. |
|
|
190
|
+
| `rpc.ingest` · `rpc.toolsRun` · `rpc.status` · `rpc.ping` | Full URLs of the methods, already carrying this project's `location[directory]`. |
|
|
191
|
+
| `auth` | `{scheme:"basic", username:"opencode", password}`. `password` says **where** the password is, never the password itself: `{kind:"file", file, field:"password", shell}` for the background service (the record is a `0600` file), `{kind:"env", env:"OPENCODE_SERVER_PASSWORD", shell}` for `opencode serve`, or `null` if unknown. `shell` is a command that prints it. |
|
|
192
|
+
| `publicUrl` | URL for senders on **other machines** (a tunnel or reverse proxy in front of the server). Runtime value (tool `apex_set_callback_url`) > `events.server.publicUrl`. The `rpc.*` URLs always use `baseUrl`: a remote sender replaces the base with `publicUrl`. |
|
|
193
|
+
| `examples` | Ready-to-run `curl` commands for `ingest` and `toolsRun` (they read the password at run time). Tool only; `server.info` omits them. |
|
|
194
|
+
| `hint` | Present when `baseUrl` is unknown: set `events.server.url`. |
|
|
92
195
|
|
|
93
196
|
```jsonc
|
|
94
197
|
"events": {
|
|
95
|
-
"server": { "url": "", "publicUrl": "" } //
|
|
198
|
+
"server": { "url": "", "publicUrl": "" } // empty = discover
|
|
96
199
|
}
|
|
97
200
|
```
|
|
98
201
|
|
|
99
|
-
|
|
202
|
+
The password is never returned on purpose: it would end up in the conversation and in the
|
|
203
|
+
model provider's logs. Scripts read it when they run.
|
|
204
|
+
|
|
205
|
+
## `tools.run`: OpenCode tools from outside
|
|
100
206
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
OpenCode (
|
|
207
|
+
An outside process runs ONE tool (built-in, from a plugin, or from an MCP server) and gets
|
|
208
|
+
its result back, without any model. It is meant for watchers that depend on something only
|
|
209
|
+
OpenCode can reach (an MCP server such as Easypanel, the browser tools…).
|
|
104
210
|
|
|
105
211
|
```jsonc
|
|
106
212
|
"events": {
|
|
107
213
|
"tools": {
|
|
108
214
|
"enabled": true,
|
|
109
|
-
"allow": ["*"], // globs
|
|
110
|
-
"deny": ["question", "subagent", "execute", "apex_*"], //
|
|
111
|
-
"agent": "build", //
|
|
215
|
+
"allow": ["*"], // tool-name globs (* and ?)
|
|
216
|
+
"deny": ["question", "subagent", "execute", "apex_*"], // wins over allow
|
|
217
|
+
"agent": "build", // whose permissions apply
|
|
112
218
|
"timeoutMs": 120000
|
|
113
219
|
}
|
|
114
220
|
}
|
|
115
221
|
```
|
|
116
222
|
|
|
117
|
-
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
-
|
|
123
|
-
|
|
124
|
-
-
|
|
125
|
-
|
|
223
|
+
- Check order: `enabled` → `deny` → `allow` → the tool must exist in this directory → the
|
|
224
|
+
**permissions of the agent** (`agent` from the call, otherwise `events.tools.agent`) —
|
|
225
|
+
the same rules the tool applies when that agent uses it. With nobody watching, a
|
|
226
|
+
permission that resolves to "ask" waits for the apex advisor (if enabled) or until
|
|
227
|
+
`timeoutMs` (overridable per call).
|
|
228
|
+
- The defaults deny `question` (would wait for an answer forever), `subagent` and `execute`
|
|
229
|
+
(would start a model) and apex's own tools.
|
|
230
|
+
- `tool` is the tool id as agents see it (MCP tools are usually `<server>_<tool>`).
|
|
231
|
+
- Tools run on behalf of a **runner** session per directory (hidden agent `apex-runner`,
|
|
232
|
+
title `apex · tools.run`, persisted in the plugin storage). It never becomes the main
|
|
233
|
+
session and does not appear among the selectable agents.
|
|
234
|
+
- Calls are not recorded in any session history and do not fire the
|
|
235
|
+
`tool.execute.before/after` hooks.
|
|
236
|
+
- Output: `ok`, `tool`, `agent`, plus the tool's `output` (structured, if the tool declares
|
|
237
|
+
it), `content` (the text a model would see) and `metadata`, or `error`. Errors (denied,
|
|
238
|
+
unknown tool, permission blocked, timeout) come back as `ok: false` with `error`, not as
|
|
239
|
+
HTTP errors.
|
|
240
|
+
- Anyone holding the server password can already do this through the session API;
|
|
241
|
+
`allow`/`deny` narrow this particular path.
|
|
126
242
|
|
|
127
243
|
## Webhooks
|
|
128
244
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
245
|
+
A webhook is a callback with an **intermediate process**: the external service calls a
|
|
246
|
+
relay over HTTP (n8n, a small daemon), and the relay turns that call into `events.ingest`.
|
|
247
|
+
OpenCode itself does not expose a webhook URL. The `apex-callbacks` skill includes a
|
|
248
|
+
minimal Node relay and an n8n recipe. To receive webhooks from the Internet, expose the
|
|
249
|
+
relay (with its own token), never the OpenCode server.
|
|
133
250
|
|
|
134
|
-
##
|
|
251
|
+
## Tools
|
|
135
252
|
|
|
136
|
-
- `apex_subscribe` —
|
|
137
|
-
- `apex_unsubscribe` —
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
- `apex_callback_endpoint` —
|
|
141
|
-
- `apex_set_callback_url` —
|
|
253
|
+
- `apex_subscribe` — creates a subscription bound to the **calling session**.
|
|
254
|
+
- `apex_unsubscribe` — removes a dynamic subscription by id (static ones are `readonly`).
|
|
255
|
+
- `apex_list_subscriptions` — lists the subscriptions of this project: the ones from config
|
|
256
|
+
(`readonly: true`) and the dynamic ones.
|
|
257
|
+
- `apex_callback_endpoint` — server address, RPC URLs and auth (see above).
|
|
258
|
+
- `apex_set_callback_url` — sets/clears the server's public URL (tunnel/proxy); `null` clears it.
|
|
142
259
|
|
|
143
|
-
|
|
144
|
-
|
|
260
|
+
The **`apex-callbacks`** skill ships with the package (`skills/apex-callbacks/SKILL.md`) and
|
|
261
|
+
registers itself wherever apex loads: stateful watchers, systemd/cron, webhooks and
|
|
145
262
|
`tools.run`.
|