opencode-prompt-logger 0.1.0
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/LICENSE +21 -0
- package/README.md +575 -0
- package/dist/core.d.ts +71 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +121 -0
- package/dist/index.js.map +11 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hugo Batista
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
# opencode-prompt-logger 📝
|
|
2
|
+
|
|
3
|
+
[](https://go.hugobatista.com/gh/opencode-prompt-logger/releases)
|
|
4
|
+
[](https://go.hugobatista.com/gh/opencode-prompt-logger/actions/workflows/lint.yml)
|
|
5
|
+
[](https://go.hugobatista.com/gh/opencode-prompt-logger/actions/workflows/test.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/opencode-prompt-logger)
|
|
7
|
+
|
|
8
|
+
OpenCode plugin. Appends what is actually sent to the model — the fully
|
|
9
|
+
assembled system prompt and the raw provider request body — to an NDJSON file,
|
|
10
|
+
one line per model request. Nothing is rewritten; the plugin only observes.
|
|
11
|
+
|
|
12
|
+
> **Requires OpenCode V2.** OpenCode V2 changed the plugin API; V1 plugin
|
|
13
|
+
> implementations do not run in V2. This plugin is built against
|
|
14
|
+
> `@opencode/plugin` V2 only.
|
|
15
|
+
|
|
16
|
+
## Motivation
|
|
17
|
+
|
|
18
|
+
The session transcript shows your message and the model's answer. It does not
|
|
19
|
+
show what the model actually received:
|
|
20
|
+
|
|
21
|
+
- The **assembled system prompt** is the agent prompt plus the environment
|
|
22
|
+
block, every `AGENTS.md` in scope, and the descriptions of the loaded skills.
|
|
23
|
+
It changes whenever an instruction file changes, and it is easy to write a
|
|
24
|
+
config that silently never reaches the model.
|
|
25
|
+
- The **raw request body** is the provider-level payload after OpenCode lowers
|
|
26
|
+
it to the selected protocol. It carries the real tool schemas, the resolved
|
|
27
|
+
model parameters, and the message layout the API sees.
|
|
28
|
+
|
|
29
|
+
Both matter when you are debugging a prompt, estimating context size, or
|
|
30
|
+
diffing two runs that behaved differently. This plugin records both, per model
|
|
31
|
+
request, in a file you can search with `jq`.
|
|
32
|
+
|
|
33
|
+
## What it does
|
|
34
|
+
|
|
35
|
+
- Registers `ctx.session.hook(kind, …)` for `context` (the agent loop,
|
|
36
|
+
including tool-driven continuations), `compaction`, `generate`, and `title`,
|
|
37
|
+
and writes one `assembled` record per request with `system`, `messages`,
|
|
38
|
+
`model`, `sessionID`, and the names of the available `tools`.
|
|
39
|
+
- Registers `ctx.session.hook("http.request", …)` and writes one `http` record
|
|
40
|
+
with the request `url` and the raw body. Bodies are one-shot streams, so the
|
|
41
|
+
plugin reads a clone and leaves the original untouched.
|
|
42
|
+
- Creates missing parent directories for the output file, with `0700`
|
|
43
|
+
permissions on POSIX, because the log holds unredacted prompts.
|
|
44
|
+
- Writes to the per-user state directory of your OS by default — no shared
|
|
45
|
+
temp directory. Windows uses `%LOCALAPPDATA%\opencode\`, macOS
|
|
46
|
+
`~/Library/Application Support/opencode/`, and Linux
|
|
47
|
+
`$XDG_STATE_HOME/opencode/`. Override it with the `file` option.
|
|
48
|
+
- Rotates at `rotateBytes` (256 MiB by default), keeping the previous copy as
|
|
49
|
+
`<file>.1`, so a forgotten log cannot fill the disk unbounded.
|
|
50
|
+
- Is **enabled by default** after installation, and can be turned off with the
|
|
51
|
+
`enabled` option without uninstalling.
|
|
52
|
+
- Swallows write and rotation failures: they are reported on stderr and never
|
|
53
|
+
break a model request.
|
|
54
|
+
|
|
55
|
+
## Sensitive data
|
|
56
|
+
|
|
57
|
+
Every record is plain, **unredacted** JSON. A log contains:
|
|
58
|
+
|
|
59
|
+
- the fully assembled system prompt, including your `AGENTS.md` files,
|
|
60
|
+
environment details, and skill instructions;
|
|
61
|
+
- the whole message history, which usually carries file contents, command
|
|
62
|
+
output, and other tool results;
|
|
63
|
+
- the raw request body sent to the provider, including the tool schemas;
|
|
64
|
+
- session, agent, model, and URL identifiers.
|
|
65
|
+
|
|
66
|
+
Logging prompts is a **troubleshooting tool**, not a permanent feature. Use it
|
|
67
|
+
to audit what actually reaches the context, analyse context and token usage,
|
|
68
|
+
debug a prompt, investigate why the model saw (or did not see) something, and
|
|
69
|
+
compare two runs. That is what it is for.
|
|
70
|
+
|
|
71
|
+
Do not leave it running as everyday logging:
|
|
72
|
+
|
|
73
|
+
- **Regulated or personal data** — if a session handles PII, PHI, secrets, or
|
|
74
|
+
client data, the log reproduces all of it in a single file.
|
|
75
|
+
- **Credentials in context** — anything that reached the model (API keys in
|
|
76
|
+
tool output, tokens, private keys) lands in the file, including in
|
|
77
|
+
`<file>.1`.
|
|
78
|
+
- **Shared machines** — the default destination is a per-user state
|
|
79
|
+
directory, private to your account on Windows and macOS and created with
|
|
80
|
+
`0700` permissions on POSIX. It is still a file on a disk someone else may
|
|
81
|
+
have access to: don't set `file` to a shared or world-readable location.
|
|
82
|
+
- **Sharing** — never paste the file, a line, or a record into an issue, a
|
|
83
|
+
chat, or a support ticket without redacting it first.
|
|
84
|
+
|
|
85
|
+
Enable it while you investigate, disable it when you are done, and delete the
|
|
86
|
+
file afterwards.
|
|
87
|
+
|
|
88
|
+
## Disk usage
|
|
89
|
+
|
|
90
|
+
The log grows fast. Measured on one machine running agent sessions:
|
|
91
|
+
|
|
92
|
+
| Measurement | Value |
|
|
93
|
+
| -------------------- | ----------------------------------------- |
|
|
94
|
+
| Average record | ~500 KB |
|
|
95
|
+
| Largest record | 676 KB |
|
|
96
|
+
| Observed growth | 12 MB → 46 MB in 6 minutes of one session |
|
|
97
|
+
|
|
98
|
+
Two things drive that growth:
|
|
99
|
+
|
|
100
|
+
- Every `assembled` record carries the whole message history, so a record is
|
|
101
|
+
as large as the conversation at that point. Records grow with the session.
|
|
102
|
+
- Every model request writes two records: the `assembled` view plus the `http`
|
|
103
|
+
body, which repeats the payload. Roughly 2× per request.
|
|
104
|
+
|
|
105
|
+
A busy agent therefore writes tens of MB per hour, and a forgotten log fills
|
|
106
|
+
the disk in hours to days. A full disk does not only break the log: it breaks
|
|
107
|
+
everything else on the host, including OpenCode's own database and logs.
|
|
108
|
+
|
|
109
|
+
### Rotation
|
|
110
|
+
|
|
111
|
+
By default the plugin rotates at 256 MiB. The current file is renamed to
|
|
112
|
+
`<file>.1` and logging continues in a fresh file; only one backup is kept, so
|
|
113
|
+
the plugin never uses much more than twice `rotateBytes`.
|
|
114
|
+
|
|
115
|
+
`rotateBytes` is a rotation point, not a size cap. A file grows past it and is
|
|
116
|
+
then renamed, so what bounds the disk is roughly twice the value — the current
|
|
117
|
+
file plus the one backup.
|
|
118
|
+
|
|
119
|
+
The size is read from the file before every write, so two OpenCode processes
|
|
120
|
+
sharing the same log (a TUI and a server, for example) still rotate at the
|
|
121
|
+
point instead of both appending past it.
|
|
122
|
+
|
|
123
|
+
```jsonc
|
|
124
|
+
"options": {
|
|
125
|
+
"rotateBytes": 1073741824, // rotate at 1 GiB
|
|
126
|
+
// "rotateBytes": false // never rotate: one file grows until the disk is full
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The limit is approximate: a record is never cut in half, so a file can
|
|
131
|
+
overshoot by up to one record (~500 KB).
|
|
132
|
+
|
|
133
|
+
### Monitor
|
|
134
|
+
|
|
135
|
+
Set the log path once; every command below uses it.
|
|
136
|
+
|
|
137
|
+
```sh
|
|
138
|
+
# Linux (default)
|
|
139
|
+
export LOG="$HOME/.local/state/opencode/prompts.ndjson"
|
|
140
|
+
# macOS (default)
|
|
141
|
+
# export LOG="$HOME/Library/Application Support/opencode/prompts.ndjson"
|
|
142
|
+
# Windows, Git Bash or WSL (%LOCALAPPDATA% is usually C:\Users\you\AppData\Local)
|
|
143
|
+
# export LOG="$LOCALAPPDATA/opencode/prompts.ndjson"
|
|
144
|
+
# Override: set LOG to whatever the `file` option says.
|
|
145
|
+
|
|
146
|
+
ls -lh "$LOG"* # the current log and its .1 backup
|
|
147
|
+
wc -l "$LOG"
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
```powershell
|
|
151
|
+
# Windows (PowerShell)
|
|
152
|
+
$LOG = "$env:LOCALAPPDATA\opencode\prompts.ndjson"
|
|
153
|
+
Get-Item "$LOG*"
|
|
154
|
+
(Get-Content $LOG | Measure-Object -Line).Lines
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Truncate
|
|
158
|
+
|
|
159
|
+
Safe to run while OpenCode is up: the plugin only appends.
|
|
160
|
+
|
|
161
|
+
```sh
|
|
162
|
+
# empty the current log and keep the file
|
|
163
|
+
truncate -s 0 "$LOG"
|
|
164
|
+
|
|
165
|
+
# free the current log and the rotated backup
|
|
166
|
+
rm -f "$LOG" "$LOG.1"
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
```powershell
|
|
170
|
+
# Windows (PowerShell)
|
|
171
|
+
Clear-Content $LOG -ErrorAction SilentlyContinue
|
|
172
|
+
Remove-Item $LOG, "$LOG.1" -ErrorAction SilentlyContinue
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Both files are recreated on the next model request.
|
|
176
|
+
|
|
177
|
+
### Keep it small
|
|
178
|
+
|
|
179
|
+
- Set `"enabled": false` when the investigation is over. Cheapest fix, and
|
|
180
|
+
nothing else is written.
|
|
181
|
+
- Set `"http": false` to drop the duplicate payload: roughly half the volume.
|
|
182
|
+
- Point `file` at a volume with room, not at a small or memory-backed one.
|
|
183
|
+
|
|
184
|
+
## Requirements
|
|
185
|
+
|
|
186
|
+
- **OpenCode V2.** The V2 release changed the plugin API; V1 plugin
|
|
187
|
+
implementations do not run in V2.
|
|
188
|
+
- **Windows, macOS, and Linux.** The default log path follows the state
|
|
189
|
+
directory of your OS; nothing in the plugin is POSIX-specific.
|
|
190
|
+
- Nothing else at runtime. [Bun](https://bun.sh) is needed to develop the
|
|
191
|
+
plugin, not to use it. `jq` is only needed for the analysis examples.
|
|
192
|
+
|
|
193
|
+
## Install
|
|
194
|
+
|
|
195
|
+
```sh
|
|
196
|
+
opencode plugin add opencode-prompt-logger
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Or add the package to `opencode.json(c)` (project or
|
|
200
|
+
`~/.config/opencode/opencode.jsonc`):
|
|
201
|
+
|
|
202
|
+
```jsonc
|
|
203
|
+
{
|
|
204
|
+
"$schema": "https://opencode.ai/config.json",
|
|
205
|
+
"plugins": ["opencode-prompt-logger"]
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
With no options the plugin is **enabled**, writes to the per-user state
|
|
210
|
+
directory of your OS (see the default paths in
|
|
211
|
+
[Configuration](#configuration)), logs the HTTP body of every request, and
|
|
212
|
+
rotates at 256 MiB.
|
|
213
|
+
|
|
214
|
+
> This is a **server** plugin. Configure it in `opencode.json(c)`. Terminal-only
|
|
215
|
+
> plugins belong in `cli.json`.
|
|
216
|
+
|
|
217
|
+
## Configuration
|
|
218
|
+
|
|
219
|
+
```jsonc
|
|
220
|
+
{
|
|
221
|
+
"$schema": "https://opencode.ai/config.json",
|
|
222
|
+
"plugins": [
|
|
223
|
+
{
|
|
224
|
+
"package": "opencode-prompt-logger",
|
|
225
|
+
"options": {
|
|
226
|
+
"enabled": true,
|
|
227
|
+
"file": "/home/me/.local/state/opencode/prompts.ndjson", // Linux default
|
|
228
|
+
"http": true,
|
|
229
|
+
"rotateBytes": 268435456
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
]
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
| Option | Type | Default | Description |
|
|
237
|
+
| ------------- | ---------------- | --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
238
|
+
| `enabled` | boolean | `true` | `false` keeps the plugin installed but idle: no hooks, no file. |
|
|
239
|
+
| `file` | string | OS state directory | NDJSON destination. Parent directories are created on first write with `0700` on POSIX. |
|
|
240
|
+
| `http` | boolean | `true` | Log the raw provider body. `false` keeps only the `assembled` records. |
|
|
241
|
+
| `rotateBytes` | number or false | `268435456` (256 MiB) | Rotation point: the file is renamed to `<file>.1` and a fresh one starts, so the disk is bounded at roughly twice this value. `false` never rotates. |
|
|
242
|
+
|
|
243
|
+
Default paths when `file` is not set:
|
|
244
|
+
|
|
245
|
+
| OS | Default log path |
|
|
246
|
+
| ------- | ------------------------------------------------------------- |
|
|
247
|
+
| Windows | `%LOCALAPPDATA%\opencode\prompts.ndjson` |
|
|
248
|
+
| macOS | `~/Library/Application Support/opencode/prompts.ndjson` |
|
|
249
|
+
| Linux | `$XDG_STATE_HOME/opencode/prompts.ndjson`, or `~/.local/state/opencode/prompts.ndjson` when `XDG_STATE_HOME` is unset |
|
|
250
|
+
|
|
251
|
+
## Enabling, disabling and uninstalling
|
|
252
|
+
|
|
253
|
+
### Enable
|
|
254
|
+
|
|
255
|
+
Enabled is the default: after installing, the plugin starts logging on the
|
|
256
|
+
next model request. `enabled: true` is optional and only useful to make the
|
|
257
|
+
intent explicit in a shared config.
|
|
258
|
+
|
|
259
|
+
### Disable
|
|
260
|
+
|
|
261
|
+
Both ways below need a config reload: `opencode service restart`, or wait for
|
|
262
|
+
the config watcher to reload it.
|
|
263
|
+
|
|
264
|
+
**1. The `enabled` option (recommended).** The plugin stays installed and
|
|
265
|
+
loaded but idle: it registers no hooks, creates no file, and writes nothing.
|
|
266
|
+
|
|
267
|
+
```jsonc
|
|
268
|
+
{
|
|
269
|
+
"plugins": [
|
|
270
|
+
{
|
|
271
|
+
"package": "opencode-prompt-logger",
|
|
272
|
+
"options": { "enabled": false }
|
|
273
|
+
}
|
|
274
|
+
]
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
Set `"enabled": true` again (or remove the option) to go back to logging.
|
|
279
|
+
|
|
280
|
+
**2. The plugin ID in `plugins`.** OpenCode processes `plugins` entries in
|
|
281
|
+
order, and an ID prefixed with `-` disables everything registered so far. The
|
|
282
|
+
line therefore has to come **after** the package entry:
|
|
283
|
+
|
|
284
|
+
```jsonc
|
|
285
|
+
{
|
|
286
|
+
"plugins": [
|
|
287
|
+
{ "package": "opencode-prompt-logger", "options": {} },
|
|
288
|
+
"-prompt-logger"
|
|
289
|
+
]
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
To re-enable, remove the `-prompt-logger` line — or put the package entry
|
|
294
|
+
after it, because a later entry for the same ID re-enables the plugin. This
|
|
295
|
+
ordering is easy to get wrong: a `-prompt-logger` line left in place silently
|
|
296
|
+
keeps the plugin off. Run `opencode plugin list` to see whether it is active.
|
|
297
|
+
|
|
298
|
+
### Uninstall
|
|
299
|
+
|
|
300
|
+
```sh
|
|
301
|
+
opencode plugin remove opencode-prompt-logger
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
Or remove the package entry from `plugins` in your `opencode.json(c)` and
|
|
305
|
+
restart OpenCode. Then delete the log, which the plugin will not touch again
|
|
306
|
+
(`rm -f "$LOG" "$LOG.1"` on POSIX, `Remove-Item $LOG, "$LOG.1"` in PowerShell;
|
|
307
|
+
see [Truncate](#truncate) for how to set `$LOG`).
|
|
308
|
+
|
|
309
|
+
## NDJSON format
|
|
310
|
+
|
|
311
|
+
Every line is one JSON object with an `at` ISO-8601 timestamp, a `type`, and
|
|
312
|
+
the `kind` of the model request: `context`, `compaction`, `generate`, or
|
|
313
|
+
`title`. After a rotation, `<file>.1` holds the previous file in the same
|
|
314
|
+
format.
|
|
315
|
+
|
|
316
|
+
### `assembled`
|
|
317
|
+
|
|
318
|
+
| Field | Notes |
|
|
319
|
+
| ----------- | ------------------------------------------------------------ |
|
|
320
|
+
| `type` | `"assembled"` |
|
|
321
|
+
| `kind` | `context`, `compaction`, `generate`, or `title` |
|
|
322
|
+
| `sessionID` | Session that issued the request |
|
|
323
|
+
| `agent` | Absent on `title` requests |
|
|
324
|
+
| `model` | `{ providerID, id, variant? }` |
|
|
325
|
+
| `system` | The assembled system prompt parts, in order |
|
|
326
|
+
| `messages` | The message array sent to the model |
|
|
327
|
+
| `tools` | Names of the available tools; `[]` on `title` requests |
|
|
328
|
+
|
|
329
|
+
### `http`
|
|
330
|
+
|
|
331
|
+
| Field | Notes |
|
|
332
|
+
| ----------- | ------------------------------------------------------------ |
|
|
333
|
+
| `type` | `"http"` |
|
|
334
|
+
| `kind` | `primary`, `compaction`, `title`, or `generate` |
|
|
335
|
+
| `sessionID` | Session that issued the request |
|
|
336
|
+
| `agent` | Agent that issued the request |
|
|
337
|
+
| `model` | `{ providerID, id, variant? }` |
|
|
338
|
+
| `url` | Request URL |
|
|
339
|
+
| `body` | Raw body as text; `unreadable: …` if the clone could not be read |
|
|
340
|
+
|
|
341
|
+
## Analysing the log with jq
|
|
342
|
+
|
|
343
|
+
The examples use `$LOG`, the path you configured — set it as shown in
|
|
344
|
+
[Monitor](#monitor). Add `.1` to read the rotated copy too (`cat "$LOG.1" "$LOG"`).
|
|
345
|
+
Records are one JSON object per line, so `jq -c` keeps them on a single line —
|
|
346
|
+
that matters whenever you pipe into `tail`.
|
|
347
|
+
|
|
348
|
+
These are POSIX shell examples (Linux, macOS, WSL, or Git Bash on Windows).
|
|
349
|
+
On Windows, install `jq` with `winget install jqlang.jq`.
|
|
350
|
+
|
|
351
|
+
### Overview
|
|
352
|
+
|
|
353
|
+
```sh
|
|
354
|
+
# records per type and kind
|
|
355
|
+
jq -r '[.type, .kind] | join("/")' "$LOG" | sort | uniq -c
|
|
356
|
+
|
|
357
|
+
# largest records first: type, kind, size in characters
|
|
358
|
+
jq -r '[.type, .kind, (tostring | length)] | @tsv' "$LOG" |
|
|
359
|
+
sort -k3 -rn | head
|
|
360
|
+
|
|
361
|
+
# how many requests a session produced
|
|
362
|
+
jq -r '[.sessionID, .type] | @tsv' "$LOG" | sort | uniq -c
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### System prompt
|
|
366
|
+
|
|
367
|
+
```sh
|
|
368
|
+
# the system prompt of the last agent-loop request
|
|
369
|
+
jq -c 'select(.type == "assembled" and .kind == "context")' "$LOG" |
|
|
370
|
+
tail -1 | jq -r '.system[]?.text'
|
|
371
|
+
|
|
372
|
+
# its size in characters
|
|
373
|
+
jq -c 'select(.type == "assembled" and .kind == "context")' "$LOG" |
|
|
374
|
+
tail -1 | jq '[.system[]?.text // ""] | join("\n") | length'
|
|
375
|
+
|
|
376
|
+
# did an instruction file reach the model?
|
|
377
|
+
jq -c 'select(.type == "assembled")' "$LOG" |
|
|
378
|
+
grep -c 'AGENTS.md'
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
To diff two moments (before and after a config change), extract into files and
|
|
382
|
+
compare:
|
|
383
|
+
|
|
384
|
+
```sh
|
|
385
|
+
jq -c 'select(.type == "assembled" and .kind == "context")' "$LOG" |
|
|
386
|
+
tail -1 | jq -r '.system[]?.text' > after.txt
|
|
387
|
+
diff -u before.txt after.txt
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### Messages and tools
|
|
391
|
+
|
|
392
|
+
```sh
|
|
393
|
+
# message count of the last agent-loop request
|
|
394
|
+
jq -c 'select(.type == "assembled" and .kind == "context")' "$LOG" |
|
|
395
|
+
tail -1 | jq '.messages | length'
|
|
396
|
+
|
|
397
|
+
# roles across the whole log
|
|
398
|
+
jq -r '.messages[]?.role // empty' "$LOG" | sort | uniq -c
|
|
399
|
+
|
|
400
|
+
# transcript size per request
|
|
401
|
+
jq -r 'select(.type == "assembled") | [.at, (.messages | tostring | length)] | @tsv' \
|
|
402
|
+
"$LOG"
|
|
403
|
+
|
|
404
|
+
# tools offered in the last agent-loop request
|
|
405
|
+
jq -c 'select(.type == "assembled" and .kind == "context")' "$LOG" |
|
|
406
|
+
tail -1 | jq -r '.tools | join(", ")'
|
|
407
|
+
|
|
408
|
+
# how the tool set changed over the session
|
|
409
|
+
jq -c 'select(.type == "assembled") | { at, tools }' "$LOG" | tail -20
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### Raw HTTP bodies
|
|
413
|
+
|
|
414
|
+
```sh
|
|
415
|
+
# last body sent to the provider, pretty-printed
|
|
416
|
+
jq -c 'select(.type == "http")' "$LOG" |
|
|
417
|
+
tail -1 | jq -r '.body' | jq .
|
|
418
|
+
|
|
419
|
+
# requests per endpoint
|
|
420
|
+
jq -r 'select(.type == "http") | .url' "$LOG" | sort | uniq -c
|
|
421
|
+
|
|
422
|
+
# which protocol each body was lowered to (chat completions vs responses)
|
|
423
|
+
jq -r 'select(.type == "http") | .body' "$LOG" |
|
|
424
|
+
jq -r 'if has("messages") then "chat/completions" elif has("input") then "responses" else "unknown" end' |
|
|
425
|
+
sort | uniq -c
|
|
426
|
+
|
|
427
|
+
# reasoning settings sent to the model
|
|
428
|
+
jq -r 'select(.type == "http") | .body' "$LOG" |
|
|
429
|
+
jq -r 'if has("reasoning") then (.reasoning | tostring) else empty end' | sort | uniq -c
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
### Sessions, agents, and models
|
|
433
|
+
|
|
434
|
+
```sh
|
|
435
|
+
# lines per session
|
|
436
|
+
jq -r '.sessionID' "$LOG" | sort | uniq -c | sort -rn | head
|
|
437
|
+
|
|
438
|
+
# requests per model
|
|
439
|
+
jq -r '.model | if . == null then "unknown" else (.providerID + "/" + .id) end' \
|
|
440
|
+
"$LOG" | sort | uniq -c
|
|
441
|
+
|
|
442
|
+
# requests per agent
|
|
443
|
+
jq -r '.agent // "unknown"' "$LOG" | sort | uniq -c
|
|
444
|
+
|
|
445
|
+
# timeline of one session
|
|
446
|
+
jq -c 'select(.sessionID == "ses_…") | { at, type, kind }' "$LOG"
|
|
447
|
+
|
|
448
|
+
# everything recorded in one hour
|
|
449
|
+
jq -c 'select(.at >= "2026-09-25T17:00:00" and .at < "2026-09-25T18:00:00")' \
|
|
450
|
+
"$LOG"
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
### Searching and exporting
|
|
454
|
+
|
|
455
|
+
```sh
|
|
456
|
+
# any record mentioning a string, anywhere in the payload
|
|
457
|
+
jq -c 'select(tostring | contains("opencode-branch-guard"))' "$LOG"
|
|
458
|
+
|
|
459
|
+
# faster on very large files: search the raw lines first, then parse one line
|
|
460
|
+
grep -n 'opencode-branch-guard' "$LOG" | head -5
|
|
461
|
+
|
|
462
|
+
# keep only the assembled records (drops the duplicate payloads)
|
|
463
|
+
jq -c 'select(.type == "assembled")' "$LOG" > assembled.ndjson
|
|
464
|
+
|
|
465
|
+
# keep only the most recent 1000 records
|
|
466
|
+
tail -1000 "$LOG" > recent.ndjson
|
|
467
|
+
|
|
468
|
+
# records of one session, ready to share after redacting
|
|
469
|
+
jq -c 'select(.sessionID == "ses_…")' "$LOG" > one-session.ndjson
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
`jq` reads the file line by line, so it stays usable on large logs. For a
|
|
473
|
+
first pass over a multi-hundred-MB file, `grep` or `rg` is faster.
|
|
474
|
+
|
|
475
|
+
## Verify
|
|
476
|
+
|
|
477
|
+
After configuring, restart OpenCode and check:
|
|
478
|
+
|
|
479
|
+
1. `opencode plugin list` includes `prompt-logger`.
|
|
480
|
+
2. Send one prompt to the agent.
|
|
481
|
+
3. With `$LOG` set as in [Monitor](#monitor), `tail -1 "$LOG" | jq .type` prints
|
|
482
|
+
`assembled` (and `http` when the HTTP hook ran).
|
|
483
|
+
4. With `"http": false`, no `type: "http"` record appears.
|
|
484
|
+
5. With `"enabled": false`, no new line appears after another prompt, and no
|
|
485
|
+
output directory is created; `opencode plugin list` still lists the plugin.
|
|
486
|
+
6. With a small `"rotateBytes"`, a `<file>.1` copy appears once the rotation
|
|
487
|
+
point is passed, and the current file starts again at one record.
|
|
488
|
+
|
|
489
|
+
## Development
|
|
490
|
+
|
|
491
|
+
```sh
|
|
492
|
+
bun install
|
|
493
|
+
bun run typecheck # tsc --noEmit, strict
|
|
494
|
+
bun test # unit (options, records, writer, rotation) + functional (mocked plugin context)
|
|
495
|
+
bun run build # dist/index.js + dist/index.d.ts (npm entrypoint)
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
- `src/core.ts` — pure logic: option resolution (`enabled`, `file`,
|
|
499
|
+
`defaultFile()`, `http`, `rotateBytes`), record building, and the append-only
|
|
500
|
+
NDJSON writer that reads the current size before each write and rotates to
|
|
501
|
+
`<file>.1`. No OpenCode imports, and no
|
|
502
|
+
POSIX-only assumptions: `defaultFile()` takes `env` and `platform` so every
|
|
503
|
+
OS is unit-tested from any machine.
|
|
504
|
+
- `src/index.ts` — the plugin (`id: "prompt-logger"`), a
|
|
505
|
+
`Plugin.define({ id, setup })` from `@opencode/plugin`. It returns early when
|
|
506
|
+
disabled, otherwise registers the four model-request hooks plus
|
|
507
|
+
`http.request` and delegates to `core`.
|
|
508
|
+
- `scripts/build.ts` — bundles `src/index.ts` to `dist/index.js` with
|
|
509
|
+
`@opencode/plugin` external, then emits declarations by running
|
|
510
|
+
`node_modules/typescript/lib/tsc.js` with the current runtime. Calling the
|
|
511
|
+
entry file directly avoids the `.cmd` shim that `tsc` resolves to on Windows
|
|
512
|
+
and that `spawn` cannot execute.
|
|
513
|
+
|
|
514
|
+
To run the plugin straight from a checkout, point `plugins` at the source
|
|
515
|
+
instead of the npm package:
|
|
516
|
+
|
|
517
|
+
```jsonc
|
|
518
|
+
{
|
|
519
|
+
"plugins": [
|
|
520
|
+
{
|
|
521
|
+
"package": "/absolute/path/opencode-prompt-logger/src",
|
|
522
|
+
"options": { "file": "/home/me/.local/state/opencode/prompts.ndjson" }
|
|
523
|
+
}
|
|
524
|
+
]
|
|
525
|
+
}
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
## Pre-release checklist
|
|
529
|
+
|
|
530
|
+
```sh
|
|
531
|
+
bun install
|
|
532
|
+
bun run typecheck
|
|
533
|
+
bun test
|
|
534
|
+
bun run build
|
|
535
|
+
npm pack --dry-run
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
Inspect the pack list (`dist/`, `README.md`, `LICENSE` only). Scan for secrets
|
|
539
|
+
before `npm publish` — the test fixtures and this README must not contain real
|
|
540
|
+
prompts.
|
|
541
|
+
|
|
542
|
+
## Releasing
|
|
543
|
+
|
|
544
|
+
The first version is published by hand, because npm trusted publishing can only
|
|
545
|
+
be configured for a package that already exists:
|
|
546
|
+
|
|
547
|
+
```sh
|
|
548
|
+
npm login
|
|
549
|
+
npm publish --access public
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
Then configure the trusted publisher at npmjs.com: package
|
|
553
|
+
`opencode-prompt-logger`, repository `hugobatista/opencode-prompt-logger`,
|
|
554
|
+
workflow `npm.yml`. Allow the `npm publish` action explicitly — trusted
|
|
555
|
+
publisher configurations created after 2026-09-03 default to `npm stage
|
|
556
|
+
publish` only.
|
|
557
|
+
|
|
558
|
+
After that, every release is automatic:
|
|
559
|
+
|
|
560
|
+
1. Bump `version` in `package.json`, commit, and push.
|
|
561
|
+
2. Run `gh workflow run create_draft_release.yml`. It builds `dist/`, pushes a
|
|
562
|
+
`release/vX.Y.Z` branch, and creates a **draft** GitHub release with the
|
|
563
|
+
build assets.
|
|
564
|
+
3. Review the draft and publish it. Publishing creates the tag `vX.Y.Z` and
|
|
565
|
+
triggers `npm.yml`, which publishes to npm with provenance over OIDC.
|
|
566
|
+
|
|
567
|
+
`npm.yml` skips a version that is already on npm, so publishing the release for
|
|
568
|
+
a version you published by hand is safe: the workflow reports a skip instead of
|
|
569
|
+
failing. Tags come from published releases, and the version badge in this
|
|
570
|
+
README reads them; pushing a tag on its own triggers nothing.
|
|
571
|
+
|
|
572
|
+
## License
|
|
573
|
+
|
|
574
|
+
MIT — see [LICENSE](./LICENSE). Author: Hugo Batista
|
|
575
|
+
(<https://github.com/hugobatista>).
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export type Options = {
|
|
2
|
+
/** NDJSON destination for captured prompts. */
|
|
3
|
+
file?: string;
|
|
4
|
+
/** Log the raw HTTP body of every model request. */
|
|
5
|
+
http?: boolean;
|
|
6
|
+
/** Turn logging off: no hooks, no output file. */
|
|
7
|
+
enabled?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Rotate the log at this many bytes, keeping the previous copy as
|
|
10
|
+
* `<file>.1`. It is a rotation point, not a hard cap: with a log and its
|
|
11
|
+
* backup the plugin uses up to about twice this value. `false` keeps a
|
|
12
|
+
* single growing file.
|
|
13
|
+
*/
|
|
14
|
+
rotateBytes?: number | false;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* NDJSON destination used when no `file` option is configured: the per-user
|
|
18
|
+
* state directory of the current OS, never a shared temp directory.
|
|
19
|
+
*
|
|
20
|
+
* - Windows: `%LOCALAPPDATA%\opencode\prompts.ndjson`
|
|
21
|
+
* - macOS: `~/Library/Application Support/opencode/prompts.ndjson`
|
|
22
|
+
* - Linux/other: `$XDG_STATE_HOME/opencode/prompts.ndjson`, falling back to
|
|
23
|
+
* `~/.local/state/opencode/prompts.ndjson`
|
|
24
|
+
*
|
|
25
|
+
* Pure by design so every OS can be asserted from any machine.
|
|
26
|
+
*/
|
|
27
|
+
export declare function defaultFile(env?: Record<string, string | undefined>, platform?: string): string;
|
|
28
|
+
/** Rotation point used when no `rotateBytes` option is configured: 256 MiB. */
|
|
29
|
+
export declare const DEFAULT_ROTATE_BYTES: number;
|
|
30
|
+
/** Model request kinds captured through one hook each. */
|
|
31
|
+
export declare const CONTEXT_KINDS: readonly ["context", "compaction", "generate", "title"];
|
|
32
|
+
export type ContextKind = (typeof CONTEXT_KINDS)[number];
|
|
33
|
+
export type LogRecord = Record<string, unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* Structural view of the `context`, `compaction`, `generate` and `title` hook
|
|
36
|
+
* events. `agent` and `tools` are only present on the kinds that carry them
|
|
37
|
+
* (`title` has neither), so every field accepts the shape of the event it
|
|
38
|
+
* actually receives.
|
|
39
|
+
*/
|
|
40
|
+
export type AssembledEvent = {
|
|
41
|
+
readonly sessionID: string;
|
|
42
|
+
readonly model?: unknown;
|
|
43
|
+
readonly system?: unknown;
|
|
44
|
+
readonly messages?: unknown;
|
|
45
|
+
readonly agent?: unknown;
|
|
46
|
+
readonly tools?: Record<string, unknown>;
|
|
47
|
+
};
|
|
48
|
+
/** Structural view of the `http.request` hook event. */
|
|
49
|
+
export type HttpEvent = {
|
|
50
|
+
readonly sessionID: string;
|
|
51
|
+
readonly agent: unknown;
|
|
52
|
+
readonly model: unknown;
|
|
53
|
+
readonly kind: string;
|
|
54
|
+
readonly request: Request;
|
|
55
|
+
};
|
|
56
|
+
export declare function resolveFile(options: Options): string;
|
|
57
|
+
export declare function isEnabled(options: Options): boolean;
|
|
58
|
+
export declare function shouldLogHttp(options: Options): boolean;
|
|
59
|
+
/** Rotation point in bytes. `0` means one file that grows without a limit. */
|
|
60
|
+
export declare function resolveRotateBytes(options: Options): number;
|
|
61
|
+
export type Write = (record: LogRecord) => void;
|
|
62
|
+
/**
|
|
63
|
+
* Append one NDJSON line per record, rotating to `<file>.1` once the file
|
|
64
|
+
* passes `rotateBytes`. The size is read from the file before every write, so
|
|
65
|
+
* a second writer on the same file (a TUI and a server, say) cannot push it
|
|
66
|
+
* past the rotation point unnoticed. A failed write or rotation is reported on
|
|
67
|
+
* stderr and swallowed: a logger must never break a model request.
|
|
68
|
+
*/
|
|
69
|
+
export declare function createWriter(file: string, rotateBytes?: number): Write;
|
|
70
|
+
export declare function assembledRecord(kind: ContextKind, event: AssembledEvent): LogRecord;
|
|
71
|
+
export declare function httpRecord(event: HttpEvent): Promise<LogRecord>;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Plugin } from "@opencode/plugin";
|
|
2
|
+
/**
|
|
3
|
+
* Capture what is actually sent to the model: `event.system` is the fully
|
|
4
|
+
* assembled system prompt (agent prompt + environment + AGENTS.md + skills),
|
|
5
|
+
* and the `http.request` body is the provider-level payload after lowering.
|
|
6
|
+
* Both are read-only observations; nothing is rewritten.
|
|
7
|
+
*/
|
|
8
|
+
declare const _default: Plugin.Plugin;
|
|
9
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.ts
|
|
3
|
+
import { Plugin } from "@opencode/plugin";
|
|
4
|
+
|
|
5
|
+
// src/core.ts
|
|
6
|
+
import { appendFileSync, mkdirSync, renameSync, statSync } from "fs";
|
|
7
|
+
import { homedir } from "os";
|
|
8
|
+
import { dirname, join } from "path";
|
|
9
|
+
function defaultFile(env = process.env, platform = process.platform) {
|
|
10
|
+
const name = join("opencode", "prompts.ndjson");
|
|
11
|
+
if (platform === "win32") {
|
|
12
|
+
return join(env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local"), name);
|
|
13
|
+
}
|
|
14
|
+
if (platform === "darwin") {
|
|
15
|
+
return join(homedir(), "Library", "Application Support", name);
|
|
16
|
+
}
|
|
17
|
+
return join(env.XDG_STATE_HOME ?? join(homedir(), ".local", "state"), name);
|
|
18
|
+
}
|
|
19
|
+
var DEFAULT_ROTATE_BYTES = 256 * 1024 * 1024;
|
|
20
|
+
var CONTEXT_KINDS = ["context", "compaction", "generate", "title"];
|
|
21
|
+
function resolveFile(options) {
|
|
22
|
+
return options.file ?? defaultFile();
|
|
23
|
+
}
|
|
24
|
+
function isEnabled(options) {
|
|
25
|
+
return options.enabled !== false;
|
|
26
|
+
}
|
|
27
|
+
function shouldLogHttp(options) {
|
|
28
|
+
return options.http !== false;
|
|
29
|
+
}
|
|
30
|
+
function resolveRotateBytes(options) {
|
|
31
|
+
const value = options.rotateBytes;
|
|
32
|
+
if (value === false)
|
|
33
|
+
return 0;
|
|
34
|
+
if (value === undefined)
|
|
35
|
+
return DEFAULT_ROTATE_BYTES;
|
|
36
|
+
return value > 0 ? Math.floor(value) : 0;
|
|
37
|
+
}
|
|
38
|
+
function existingSize(file) {
|
|
39
|
+
try {
|
|
40
|
+
return statSync(file).size;
|
|
41
|
+
} catch {
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function createWriter(file, rotateBytes = DEFAULT_ROTATE_BYTES) {
|
|
46
|
+
mkdirSync(dirname(file), { recursive: true, mode: 448 });
|
|
47
|
+
const backup = `${file}.1`;
|
|
48
|
+
return (record) => {
|
|
49
|
+
try {
|
|
50
|
+
const line = JSON.stringify({ at: new Date().toISOString(), ...record }) + `
|
|
51
|
+
`;
|
|
52
|
+
const size = Buffer.byteLength(line, "utf8");
|
|
53
|
+
const current = existingSize(file);
|
|
54
|
+
if (rotateBytes > 0 && current > 0 && current + size > rotateBytes) {
|
|
55
|
+
try {
|
|
56
|
+
renameSync(file, backup);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error(`prompt-logger: rotation failed: ${String(error)}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
appendFileSync(file, line);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.error(`prompt-logger: ${String(error)}`);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function assembledRecord(kind, event) {
|
|
68
|
+
return {
|
|
69
|
+
type: "assembled",
|
|
70
|
+
kind,
|
|
71
|
+
sessionID: event.sessionID,
|
|
72
|
+
agent: event.agent,
|
|
73
|
+
model: event.model,
|
|
74
|
+
system: event.system,
|
|
75
|
+
messages: event.messages,
|
|
76
|
+
tools: event.tools ? Object.keys(event.tools) : []
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
async function httpRecord(event) {
|
|
80
|
+
let body;
|
|
81
|
+
try {
|
|
82
|
+
body = await event.request.clone().text();
|
|
83
|
+
} catch (error) {
|
|
84
|
+
body = `unreadable: ${String(error)}`;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
type: "http",
|
|
88
|
+
kind: event.kind,
|
|
89
|
+
sessionID: event.sessionID,
|
|
90
|
+
agent: event.agent,
|
|
91
|
+
model: event.model,
|
|
92
|
+
url: event.request.url,
|
|
93
|
+
body
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/index.ts
|
|
98
|
+
var src_default = Plugin.define({
|
|
99
|
+
id: "prompt-logger",
|
|
100
|
+
async setup(ctx) {
|
|
101
|
+
const options = ctx.options ?? {};
|
|
102
|
+
if (!isEnabled(options))
|
|
103
|
+
return;
|
|
104
|
+
const write = createWriter(resolveFile(options), resolveRotateBytes(options));
|
|
105
|
+
for (const kind of CONTEXT_KINDS) {
|
|
106
|
+
await ctx.session.hook(kind, (event) => {
|
|
107
|
+
write(assembledRecord(kind, event));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
if (shouldLogHttp(options)) {
|
|
111
|
+
await ctx.session.hook("http.request", async (event) => {
|
|
112
|
+
write(await httpRecord(event));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
export {
|
|
118
|
+
src_default as default
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
//# debugId=31CC535C2A05F23364756E2164756E21
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.ts", "../src/core.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { Plugin } from \"@opencode/plugin\"\nimport {\n CONTEXT_KINDS,\n assembledRecord,\n createWriter,\n httpRecord,\n isEnabled,\n resolveFile,\n resolveRotateBytes,\n shouldLogHttp,\n type Options,\n} from \"./core\"\n\n/**\n * Capture what is actually sent to the model: `event.system` is the fully\n * assembled system prompt (agent prompt + environment + AGENTS.md + skills),\n * and the `http.request` body is the provider-level payload after lowering.\n * Both are read-only observations; nothing is rewritten.\n */\nexport default Plugin.define({\n id: \"prompt-logger\",\n async setup(ctx) {\n const options = (ctx.options ?? {}) as Options\n\n // Disabled means idle: no hooks and no output file.\n if (!isEnabled(options)) return\n\n const write = createWriter(resolveFile(options), resolveRotateBytes(options))\n\n for (const kind of CONTEXT_KINDS) {\n await ctx.session.hook(kind, (event) => {\n write(assembledRecord(kind, event))\n })\n }\n\n if (shouldLogHttp(options)) {\n await ctx.session.hook(\"http.request\", async (event) => {\n write(await httpRecord(event))\n })\n }\n },\n})\n",
|
|
6
|
+
"import { appendFileSync, mkdirSync, renameSync, statSync } from \"node:fs\"\nimport { homedir } from \"node:os\"\nimport { dirname, join } from \"node:path\"\n\nexport type Options = {\n /** NDJSON destination for captured prompts. */\n file?: string\n /** Log the raw HTTP body of every model request. */\n http?: boolean\n /** Turn logging off: no hooks, no output file. */\n enabled?: boolean\n /**\n * Rotate the log at this many bytes, keeping the previous copy as\n * `<file>.1`. It is a rotation point, not a hard cap: with a log and its\n * backup the plugin uses up to about twice this value. `false` keeps a\n * single growing file.\n */\n rotateBytes?: number | false\n}\n\n/**\n * NDJSON destination used when no `file` option is configured: the per-user\n * state directory of the current OS, never a shared temp directory.\n *\n * - Windows: `%LOCALAPPDATA%\\opencode\\prompts.ndjson`\n * - macOS: `~/Library/Application Support/opencode/prompts.ndjson`\n * - Linux/other: `$XDG_STATE_HOME/opencode/prompts.ndjson`, falling back to\n * `~/.local/state/opencode/prompts.ndjson`\n *\n * Pure by design so every OS can be asserted from any machine.\n */\nexport function defaultFile(\n env: Record<string, string | undefined> = process.env,\n platform: string = process.platform,\n): string {\n const name = join(\"opencode\", \"prompts.ndjson\")\n if (platform === \"win32\") {\n return join(env.LOCALAPPDATA ?? join(homedir(), \"AppData\", \"Local\"), name)\n }\n if (platform === \"darwin\") {\n return join(homedir(), \"Library\", \"Application Support\", name)\n }\n return join(env.XDG_STATE_HOME ?? join(homedir(), \".local\", \"state\"), name)\n}\n\n/** Rotation point used when no `rotateBytes` option is configured: 256 MiB. */\nexport const DEFAULT_ROTATE_BYTES = 256 * 1024 * 1024\n\n/** Model request kinds captured through one hook each. */\nexport const CONTEXT_KINDS = [\"context\", \"compaction\", \"generate\", \"title\"] as const\n\nexport type ContextKind = (typeof CONTEXT_KINDS)[number]\n\nexport type LogRecord = Record<string, unknown>\n\n/**\n * Structural view of the `context`, `compaction`, `generate` and `title` hook\n * events. `agent` and `tools` are only present on the kinds that carry them\n * (`title` has neither), so every field accepts the shape of the event it\n * actually receives.\n */\nexport type AssembledEvent = {\n readonly sessionID: string\n readonly model?: unknown\n readonly system?: unknown\n readonly messages?: unknown\n readonly agent?: unknown\n readonly tools?: Record<string, unknown>\n}\n\n/** Structural view of the `http.request` hook event. */\nexport type HttpEvent = {\n readonly sessionID: string\n readonly agent: unknown\n readonly model: unknown\n readonly kind: string\n readonly request: Request\n}\n\nexport function resolveFile(options: Options): string {\n return options.file ?? defaultFile()\n}\n\nexport function isEnabled(options: Options): boolean {\n return options.enabled !== false\n}\n\nexport function shouldLogHttp(options: Options): boolean {\n return options.http !== false\n}\n\n/** Rotation point in bytes. `0` means one file that grows without a limit. */\nexport function resolveRotateBytes(options: Options): number {\n const value = options.rotateBytes\n if (value === false) return 0\n if (value === undefined) return DEFAULT_ROTATE_BYTES\n return value > 0 ? Math.floor(value) : 0\n}\n\nfunction existingSize(file: string): number {\n try {\n return statSync(file).size\n } catch {\n return 0\n }\n}\n\nexport type Write = (record: LogRecord) => void\n\n/**\n * Append one NDJSON line per record, rotating to `<file>.1` once the file\n * passes `rotateBytes`. The size is read from the file before every write, so\n * a second writer on the same file (a TUI and a server, say) cannot push it\n * past the rotation point unnoticed. A failed write or rotation is reported on\n * stderr and swallowed: a logger must never break a model request.\n */\nexport function createWriter(file: string, rotateBytes: number = DEFAULT_ROTATE_BYTES): Write {\n // The log holds unredacted prompts: private to the user where POSIX allows.\n mkdirSync(dirname(file), { recursive: true, mode: 0o700 })\n const backup = `${file}.1`\n\n return (record) => {\n try {\n const line = JSON.stringify({ at: new Date().toISOString(), ...record }) + \"\\n\"\n const size = Buffer.byteLength(line, \"utf8\")\n const current = existingSize(file)\n\n // `current > 0` keeps a single oversized record from rotating an empty\n // or missing file.\n if (rotateBytes > 0 && current > 0 && current + size > rotateBytes) {\n try {\n // One backup: the previous copy is replaced on every rotation.\n renameSync(file, backup)\n } catch (error) {\n console.error(`prompt-logger: rotation failed: ${String(error)}`)\n }\n }\n\n appendFileSync(file, line)\n } catch (error) {\n console.error(`prompt-logger: ${String(error)}`)\n }\n }\n}\n\nexport function assembledRecord(kind: ContextKind, event: AssembledEvent): LogRecord {\n return {\n type: \"assembled\",\n kind,\n sessionID: event.sessionID,\n agent: event.agent,\n model: event.model,\n system: event.system,\n messages: event.messages,\n tools: event.tools ? Object.keys(event.tools) : [],\n }\n}\n\nexport async function httpRecord(event: HttpEvent): Promise<LogRecord> {\n let body: string | undefined\n try {\n // Bodies are one-shot streams; clone before reading.\n body = await event.request.clone().text()\n } catch (error) {\n body = `unreadable: ${String(error)}`\n }\n return {\n type: \"http\",\n kind: event.kind,\n sessionID: event.sessionID,\n agent: event.agent,\n model: event.model,\n url: event.request.url,\n body,\n }\n}\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": "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",
|
|
9
|
+
"debugId": "31CC535C2A05F23364756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "opencode-prompt-logger",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OpenCode plugin: logs the assembled system prompt and the raw model request body of every model request to an NDJSON file.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Hugo Batista",
|
|
9
|
+
"url": "https://github.com/hugobatista"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/hugobatista/opencode-prompt-logger.git"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/hugobatista/opencode-prompt-logger#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/hugobatista/opencode-prompt-logger/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"opencode",
|
|
21
|
+
"opencode-plugin",
|
|
22
|
+
"prompt",
|
|
23
|
+
"logging",
|
|
24
|
+
"ndjson",
|
|
25
|
+
"debugging"
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"opencode": ">=2.0.0"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"import": "./dist/index.js",
|
|
42
|
+
"default": "./dist/index.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"test": "bun test",
|
|
47
|
+
"typecheck": "tsc --noEmit",
|
|
48
|
+
"build": "bun run scripts/build.ts",
|
|
49
|
+
"prepublishOnly": "bun run typecheck && bun test && bun run build"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@opencode/plugin": ">=2.0.16"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@opencode/plugin": "2.0.16",
|
|
56
|
+
"@types/bun": "^1.4.1",
|
|
57
|
+
"@types/node": "^24.0.0",
|
|
58
|
+
"typescript": "^5.8.0"
|
|
59
|
+
}
|
|
60
|
+
}
|