@distlang/opencode-plugin 0.1.15 → 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/README.md +53 -12
- package/package.json +3 -3
- package/src/command.js +21 -4
- package/src/distlang.js +9 -5
- package/src/index.js +49 -30
- package/src/recorder.js +74 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @distlang/opencode-plugin
|
|
2
2
|
|
|
3
|
-
OpenCode plugin for capturing coding sessions and uploading them to Distlang
|
|
3
|
+
OpenCode plugin for capturing coding sessions and uploading them to Distlang Agent Debugger.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -59,31 +59,72 @@ If the plugin installs a managed copy because `distlang` was missing, you can lo
|
|
|
59
59
|
The plugin uses:
|
|
60
60
|
|
|
61
61
|
- `distlang helpers auth status --json`
|
|
62
|
-
- `distlang helpers request POST /
|
|
62
|
+
- `distlang helpers request POST /agent-debugger/v1/ingest ...`
|
|
63
63
|
|
|
64
64
|
If you are not logged in, the plugin logs one warning and continues without uploading.
|
|
65
65
|
|
|
66
|
-
##
|
|
66
|
+
## Distlang commands
|
|
67
67
|
|
|
68
|
-
OpenCode commands are configured separately from plugins, so add
|
|
68
|
+
OpenCode commands are configured separately from plugins, so add command files to make the Distlang controls available in the TUI:
|
|
69
69
|
|
|
70
|
-
`~/.config/opencode/commands/distlang.md`
|
|
70
|
+
`~/.config/opencode/commands/distlang-start.md`
|
|
71
71
|
|
|
72
72
|
```md
|
|
73
73
|
---
|
|
74
|
-
description:
|
|
74
|
+
description: Sign in and enable Distlang Agent Debugger uploads
|
|
75
75
|
---
|
|
76
76
|
```
|
|
77
77
|
|
|
78
|
-
|
|
78
|
+
`~/.config/opencode/commands/distlang-stop.md`
|
|
79
|
+
|
|
80
|
+
```md
|
|
81
|
+
---
|
|
82
|
+
description: Disable Distlang Agent Debugger uploads and sign out
|
|
83
|
+
---
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`~/.config/opencode/commands/distlang-status.md`
|
|
87
|
+
|
|
88
|
+
```md
|
|
89
|
+
---
|
|
90
|
+
description: Show Distlang Agent Debugger upload status
|
|
91
|
+
---
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
The plugin watches for these commands:
|
|
95
|
+
|
|
96
|
+
- `/distlang-start`
|
|
97
|
+
- `/distlang-stop`
|
|
98
|
+
- `/distlang-status`
|
|
99
|
+
|
|
100
|
+
The legacy compact command also remains supported:
|
|
79
101
|
|
|
80
102
|
- `/distlang status`
|
|
81
103
|
- `/distlang start`
|
|
82
104
|
- `/distlang stop`
|
|
105
|
+
- `/distlang login`
|
|
106
|
+
- `/distlang logout`
|
|
83
107
|
|
|
84
108
|
Command results are written to the OpenCode app log, and also to the debug log file when enabled.
|
|
85
109
|
|
|
86
|
-
If `/distlang
|
|
110
|
+
If `/distlang-start` finds that Distlang auth is missing, the plugin starts `distlang helpers login`, which opens the browser login flow.
|
|
111
|
+
|
|
112
|
+
### Command usage
|
|
113
|
+
|
|
114
|
+
Run these inside the OpenCode TUI:
|
|
115
|
+
|
|
116
|
+
```text
|
|
117
|
+
/distlang-start
|
|
118
|
+
/distlang-status
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Commands:
|
|
122
|
+
|
|
123
|
+
- `/distlang-status`: show whether uploads are enabled, whether auth is available, and whether recent Agent Debugger sessions are visible
|
|
124
|
+
- `/distlang-start`: sign in if needed and enable Agent Debugger uploads
|
|
125
|
+
- `/distlang-stop`: disable Agent Debugger uploads and sign out of Distlang
|
|
126
|
+
|
|
127
|
+
Legacy aliases remain available: `/distlang status`, `/distlang start`, `/distlang stop`, `/distlang login`, and `/distlang logout`.
|
|
87
128
|
|
|
88
129
|
## Debugging
|
|
89
130
|
|
|
@@ -106,12 +147,12 @@ DISTLANG_OPENCODE_LOG_FILE=/tmp/distlang-opencode.log opencode
|
|
|
106
147
|
To inspect whether uploads are visible after a run:
|
|
107
148
|
|
|
108
149
|
```bash
|
|
109
|
-
distlang helpers request GET /
|
|
150
|
+
distlang helpers request GET /agent-debugger/v1/sessions --json
|
|
110
151
|
```
|
|
111
152
|
|
|
112
153
|
## Captured Model
|
|
113
154
|
|
|
114
|
-
The plugin builds a session-batch payload for Distlang
|
|
155
|
+
The plugin builds a session-batch payload for Distlang Agent Debugger:
|
|
115
156
|
|
|
116
157
|
- `session`
|
|
117
158
|
- `interaction`
|
|
@@ -144,8 +185,8 @@ If `--model` and `OPENCODE_MODEL` are omitted, the harness defaults to `openai/g
|
|
|
144
185
|
|
|
145
186
|
Useful flags:
|
|
146
187
|
|
|
147
|
-
- `--require-upload` fails unless Distlang auth is available and the uploaded
|
|
148
|
-
- `--keep-session` keeps the uploaded
|
|
188
|
+
- `--require-upload` fails unless Distlang auth is available and the uploaded Agent Debugger session validates.
|
|
189
|
+
- `--keep-session` keeps the uploaded Agent Debugger session for dashboard inspection.
|
|
149
190
|
- `--keep-temp` keeps temporary logs and fixtures for debugging. Do not commit those files.
|
|
150
191
|
- `--verbose` prints redacted command output.
|
|
151
192
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@distlang/opencode-plugin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "OpenCode plugin for Distlang
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"description": "OpenCode plugin for Distlang Agent Debugger",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"exports": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"distlang",
|
|
26
26
|
"opencode",
|
|
27
27
|
"plugin",
|
|
28
|
-
"
|
|
28
|
+
"agent-debugger"
|
|
29
29
|
],
|
|
30
30
|
"repository": {
|
|
31
31
|
"type": "git",
|
package/src/command.js
CHANGED
|
@@ -60,14 +60,31 @@ export function extractDistlangInvocation(input) {
|
|
|
60
60
|
];
|
|
61
61
|
for (const candidate of candidates) {
|
|
62
62
|
const parsed = commandLike(candidate);
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
const invocation = distlangInvocation(parsed);
|
|
64
|
+
if (invocation) {
|
|
65
|
+
return invocation;
|
|
65
66
|
}
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
const directText = splitCommandText(configuredValue(input?.commandLine, configuredValue(input?.text, configuredValue(input?.input, configuredValue(input?.raw, "")))));
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
return distlangInvocation(directText);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function distlangInvocation(parsed) {
|
|
74
|
+
if (!parsed) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
if (parsed.name === "distlang") {
|
|
78
|
+
return parsed;
|
|
79
|
+
}
|
|
80
|
+
const action = parsed.name.startsWith("distlang-") ? parsed.name.slice("distlang-".length) : "";
|
|
81
|
+
if (action === "start" || action === "stop" || action === "status") {
|
|
82
|
+
return {
|
|
83
|
+
...parsed,
|
|
84
|
+
name: "distlang",
|
|
85
|
+
action,
|
|
86
|
+
raw: parsed.raw || `/${parsed.name}`,
|
|
87
|
+
};
|
|
71
88
|
}
|
|
72
89
|
return null;
|
|
73
90
|
}
|
package/src/distlang.js
CHANGED
|
@@ -181,15 +181,19 @@ export async function loginWithDistlang() {
|
|
|
181
181
|
return runDistlang(["helpers", "login"]);
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
export async function
|
|
185
|
-
|
|
184
|
+
export async function logoutWithDistlang() {
|
|
185
|
+
return runDistlang(["helpers", "logout"]);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export async function uploadAgentDebuggerPayload(payload) {
|
|
189
|
+
const tempFile = join(tmpdir(), `distlang-agent-debugger-${randomUUID()}.json`);
|
|
186
190
|
await fs.writeFile(tempFile, `${JSON.stringify(payload, null, 2)}\n`, "utf8");
|
|
187
191
|
try {
|
|
188
192
|
const { stdout } = await runDistlang([
|
|
189
193
|
"helpers",
|
|
190
194
|
"request",
|
|
191
195
|
"POST",
|
|
192
|
-
"/
|
|
196
|
+
"/agent-debugger/v1/ingest",
|
|
193
197
|
`--body-file=${tempFile}`,
|
|
194
198
|
"--content-type=application/json",
|
|
195
199
|
"--json",
|
|
@@ -200,12 +204,12 @@ export async function uploadAIDebuggerPayload(payload) {
|
|
|
200
204
|
}
|
|
201
205
|
}
|
|
202
206
|
|
|
203
|
-
export async function
|
|
207
|
+
export async function fetchAgentDebuggerSessions() {
|
|
204
208
|
const { stdout } = await runDistlang([
|
|
205
209
|
"helpers",
|
|
206
210
|
"request",
|
|
207
211
|
"GET",
|
|
208
|
-
"/
|
|
212
|
+
"/agent-debugger/v1/sessions?source=opencode&limit=5",
|
|
209
213
|
"--json",
|
|
210
214
|
]);
|
|
211
215
|
return JSON.parse(stdout || "{}");
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRecorder, extractSessionTitle } from "./recorder.js";
|
|
2
2
|
import { extractDistlangInvocation } from "./command.js";
|
|
3
|
-
import { distlangCommandInfo,
|
|
3
|
+
import { distlangCommandInfo, fetchAgentDebuggerSessions, getAuthStatus, loginWithDistlang, logoutWithDistlang, resolveDistlangBinary, uploadAgentDebuggerPayload } from "./distlang.js";
|
|
4
4
|
import { pluginStatePath, readPluginState, writePluginState } from "./state.js";
|
|
5
5
|
import { appendFile } from "node:fs/promises";
|
|
6
6
|
|
|
@@ -17,7 +17,7 @@ function debugLogFile() {
|
|
|
17
17
|
return configuredValue(process.env.DISTLANG_OPENCODE_LOG_FILE, "");
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export const
|
|
20
|
+
export const DistlangAgentDebugger = async ({ project, directory, client }) => {
|
|
21
21
|
const debug = debugEnabled();
|
|
22
22
|
const recorder = createRecorder({ project, directory, initialPrompt: extractOpenCodeRunPrompt(process.argv) });
|
|
23
23
|
let loggedInit = false;
|
|
@@ -28,7 +28,7 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
28
28
|
async function log(level, message, extra = undefined) {
|
|
29
29
|
const line = JSON.stringify({
|
|
30
30
|
ts: new Date().toISOString(),
|
|
31
|
-
service: "distlang-
|
|
31
|
+
service: "distlang-agent-debugger",
|
|
32
32
|
level,
|
|
33
33
|
message,
|
|
34
34
|
extra,
|
|
@@ -42,7 +42,7 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
42
42
|
}
|
|
43
43
|
await client.app.log({
|
|
44
44
|
body: {
|
|
45
|
-
service: "distlang-
|
|
45
|
+
service: "distlang-agent-debugger",
|
|
46
46
|
level,
|
|
47
47
|
message,
|
|
48
48
|
extra,
|
|
@@ -62,7 +62,7 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
64
|
loggedInit = true;
|
|
65
|
-
await log(debug ? "debug" : "info", "Distlang OpenCode
|
|
65
|
+
await log(debug ? "debug" : "info", "Distlang OpenCode Agent Debugger plugin initialized", {
|
|
66
66
|
debug,
|
|
67
67
|
statePath: pluginStatePath(),
|
|
68
68
|
distlang: distlangCommandInfo(),
|
|
@@ -96,7 +96,16 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
96
96
|
return;
|
|
97
97
|
}
|
|
98
98
|
commandHandledAt = now;
|
|
99
|
-
const
|
|
99
|
+
const requestedAction = configuredValue(invocation.action, configuredValue(invocation.args[0], "status")).toLowerCase();
|
|
100
|
+
const action = requestedAction === "login" ? "start" : requestedAction === "logout" ? "stop" : requestedAction;
|
|
101
|
+
if (!["status", "start", "stop"].includes(action)) {
|
|
102
|
+
await maybeLogCommandResult("warn", "Unknown Distlang command. Use /distlang-status, /distlang-start, or /distlang-stop", {
|
|
103
|
+
source,
|
|
104
|
+
action: requestedAction,
|
|
105
|
+
command_hint: "/distlang-status | /distlang-start | /distlang-stop",
|
|
106
|
+
});
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
100
109
|
if (action === "start") {
|
|
101
110
|
const state = await writePluginState(true);
|
|
102
111
|
authWarningLogged = false;
|
|
@@ -119,7 +128,7 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
119
128
|
await maybeLogCommandResult("warn", "Distlang uploads enabled, but auth check failed", { source, action, state, distlang: resolved, error: String(error) });
|
|
120
129
|
return;
|
|
121
130
|
}
|
|
122
|
-
await maybeLogCommandResult("info", "Distlang
|
|
131
|
+
await maybeLogCommandResult("info", "Distlang Agent Debugger uploads enabled", {
|
|
123
132
|
source,
|
|
124
133
|
action,
|
|
125
134
|
state,
|
|
@@ -131,7 +140,14 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
131
140
|
|
|
132
141
|
if (action === "stop") {
|
|
133
142
|
const state = await writePluginState(false);
|
|
134
|
-
|
|
143
|
+
authWarningLogged = false;
|
|
144
|
+
try {
|
|
145
|
+
const resolved = await resolveDistlangBinary({ installIfMissing: true });
|
|
146
|
+
await logoutWithDistlang();
|
|
147
|
+
await maybeLogCommandResult("info", "Distlang Agent Debugger uploads disabled and signed out", { source, action, state, distlang: resolved });
|
|
148
|
+
} catch (error) {
|
|
149
|
+
await maybeLogCommandResult("warn", "Distlang Agent Debugger uploads disabled, but sign out failed", { source, action, state, error: String(error) });
|
|
150
|
+
}
|
|
135
151
|
return;
|
|
136
152
|
}
|
|
137
153
|
|
|
@@ -143,11 +159,11 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
143
159
|
try {
|
|
144
160
|
resolved = await resolveDistlangBinary({ installIfMissing: true });
|
|
145
161
|
auth = await getAuthStatus();
|
|
146
|
-
sessions = await
|
|
162
|
+
sessions = await fetchAgentDebuggerSessions();
|
|
147
163
|
} catch (error) {
|
|
148
164
|
resolutionError = String(error);
|
|
149
165
|
}
|
|
150
|
-
await maybeLogCommandResult("info", "Distlang
|
|
166
|
+
await maybeLogCommandResult("info", "Distlang Agent Debugger status", {
|
|
151
167
|
source,
|
|
152
168
|
action,
|
|
153
169
|
state,
|
|
@@ -155,13 +171,13 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
155
171
|
auth,
|
|
156
172
|
sessions,
|
|
157
173
|
error: resolutionError,
|
|
158
|
-
command_hint: "/distlang
|
|
174
|
+
command_hint: "/distlang-status | /distlang-start | /distlang-stop",
|
|
159
175
|
});
|
|
160
176
|
}
|
|
161
177
|
|
|
162
178
|
async function ensureAuthStatus() {
|
|
163
179
|
if (!(await uploadEnabled())) {
|
|
164
|
-
await debugLog("Distlang
|
|
180
|
+
await debugLog("Distlang Agent Debugger uploads are disabled", { state: await readPluginState() });
|
|
165
181
|
return false;
|
|
166
182
|
}
|
|
167
183
|
try {
|
|
@@ -171,7 +187,10 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
171
187
|
await debugLog("Distlang auth status resolved", { authAvailable, payload, distlang: resolved });
|
|
172
188
|
if (!authAvailable && !authWarningLogged) {
|
|
173
189
|
authWarningLogged = true;
|
|
174
|
-
await
|
|
190
|
+
await maybeLogCommandResult("warn", "Distlang Agent Debugger upload disabled: run `/distlang-start` to sign in and enable uploads", {
|
|
191
|
+
auth: payload,
|
|
192
|
+
command_hint: "/distlang-start",
|
|
193
|
+
});
|
|
175
194
|
} else if (authAvailable) {
|
|
176
195
|
authWarningLogged = false;
|
|
177
196
|
}
|
|
@@ -180,11 +199,11 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
180
199
|
if (error && typeof error === "object" && error.code === "ENOENT") {
|
|
181
200
|
if (!distlangMissingLogged) {
|
|
182
201
|
distlangMissingLogged = true;
|
|
183
|
-
await log("warn", "distlang CLI not found;
|
|
202
|
+
await log("warn", "distlang CLI not found; Agent Debugger upload disabled", { distlang: distlangCommandInfo() });
|
|
184
203
|
}
|
|
185
204
|
} else if (!authWarningLogged) {
|
|
186
205
|
authWarningLogged = true;
|
|
187
|
-
await log("warn", "Distlang
|
|
206
|
+
await log("warn", "Distlang Agent Debugger auth check failed; upload disabled", { error: String(error) });
|
|
188
207
|
}
|
|
189
208
|
return false;
|
|
190
209
|
}
|
|
@@ -196,14 +215,14 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
196
215
|
await refreshSessionMetadata(sessionID, { retry: true });
|
|
197
216
|
payload = recorder.finalizeSession(sessionID, result, Date.now());
|
|
198
217
|
} catch (error) {
|
|
199
|
-
await log("error", "
|
|
218
|
+
await log("error", "Agent Debugger session finalization failed", { sessionID, result, error: String(error) });
|
|
200
219
|
return;
|
|
201
220
|
}
|
|
202
221
|
if (!payload) {
|
|
203
222
|
await debugLog("No payload produced during session finalization", { sessionID, result });
|
|
204
223
|
return;
|
|
205
224
|
}
|
|
206
|
-
await debugLog("Finalized
|
|
225
|
+
await debugLog("Finalized Agent Debugger session payload", {
|
|
207
226
|
sessionID,
|
|
208
227
|
result,
|
|
209
228
|
interactions: Array.isArray(payload.interactions) ? payload.interactions.length : 0,
|
|
@@ -215,15 +234,15 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
215
234
|
return;
|
|
216
235
|
}
|
|
217
236
|
try {
|
|
218
|
-
const response = await
|
|
219
|
-
await debugLog("
|
|
237
|
+
const response = await uploadAgentDebuggerPayload(payload);
|
|
238
|
+
await debugLog("Agent Debugger upload response received", { sessionID, response });
|
|
220
239
|
if (!response.ok) {
|
|
221
|
-
await log("warn", "
|
|
240
|
+
await log("warn", "Agent Debugger upload failed", { sessionID, response });
|
|
222
241
|
return;
|
|
223
242
|
}
|
|
224
|
-
await debugLog("
|
|
243
|
+
await debugLog("Agent Debugger session uploaded", { sessionID, response });
|
|
225
244
|
} catch (error) {
|
|
226
|
-
await log("warn", "
|
|
245
|
+
await log("warn", "Agent Debugger upload failed", { sessionID, error: String(error) });
|
|
227
246
|
}
|
|
228
247
|
}
|
|
229
248
|
|
|
@@ -233,14 +252,14 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
233
252
|
await refreshSessionMetadata(sessionID, { retry: true });
|
|
234
253
|
payload = recorder.snapshotSession(sessionID, result, Date.now());
|
|
235
254
|
} catch (error) {
|
|
236
|
-
await log("error", "
|
|
255
|
+
await log("error", "Agent Debugger session snapshot failed", { sessionID, result, error: String(error) });
|
|
237
256
|
return;
|
|
238
257
|
}
|
|
239
258
|
if (!payload) {
|
|
240
259
|
await debugLog("No payload produced during session snapshot", { sessionID, result });
|
|
241
260
|
return;
|
|
242
261
|
}
|
|
243
|
-
await debugLog("Prepared
|
|
262
|
+
await debugLog("Prepared Agent Debugger session snapshot", {
|
|
244
263
|
sessionID,
|
|
245
264
|
result,
|
|
246
265
|
interactions: Array.isArray(payload.interactions) ? payload.interactions.length : 0,
|
|
@@ -252,15 +271,15 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
252
271
|
return;
|
|
253
272
|
}
|
|
254
273
|
try {
|
|
255
|
-
const response = await
|
|
256
|
-
await debugLog("
|
|
274
|
+
const response = await uploadAgentDebuggerPayload(payload);
|
|
275
|
+
await debugLog("Agent Debugger snapshot upload response received", { sessionID, response });
|
|
257
276
|
if (!response.ok) {
|
|
258
|
-
await log("warn", "
|
|
277
|
+
await log("warn", "Agent Debugger snapshot upload failed", { sessionID, response });
|
|
259
278
|
return;
|
|
260
279
|
}
|
|
261
|
-
await debugLog("
|
|
280
|
+
await debugLog("Agent Debugger session snapshot uploaded", { sessionID, response });
|
|
262
281
|
} catch (error) {
|
|
263
|
-
await log("warn", "
|
|
282
|
+
await log("warn", "Agent Debugger snapshot upload failed", { sessionID, error: String(error) });
|
|
264
283
|
}
|
|
265
284
|
}
|
|
266
285
|
|
|
@@ -469,4 +488,4 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
469
488
|
};
|
|
470
489
|
};
|
|
471
490
|
|
|
472
|
-
export default
|
|
491
|
+
export default DistlangAgentDebugger;
|
package/src/recorder.js
CHANGED
|
@@ -68,6 +68,11 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
68
68
|
messageRoles: new Map(),
|
|
69
69
|
messageTextParts: new Map(),
|
|
70
70
|
messageInteractions: new Map(),
|
|
71
|
+
interactionUserMessages: new Map(),
|
|
72
|
+
messageLLMSteps: new Map(),
|
|
73
|
+
toolStepsByCallID: new Map(),
|
|
74
|
+
lastLLMStepByInteraction: new Map(),
|
|
75
|
+
lastToolStepByInteraction: new Map(),
|
|
71
76
|
toolStarts: new Map(),
|
|
72
77
|
seenUserMessages: new Set(),
|
|
73
78
|
contextContributors: [],
|
|
@@ -121,6 +126,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
121
126
|
existing.prompt = cleanPrompt;
|
|
122
127
|
existing.summary = summarizePrompt(cleanPrompt, existing.summary || `Interaction ${existing.index}`);
|
|
123
128
|
applyInteractionMode(existing, mode);
|
|
129
|
+
recorder.interactionUserMessages.set(existing.id, messageID);
|
|
124
130
|
return existing;
|
|
125
131
|
}
|
|
126
132
|
const current = findInteraction(recorder, recorder.currentInteractionID);
|
|
@@ -130,6 +136,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
130
136
|
current.summary = summarizePrompt(cleanPrompt, `Interaction ${current.index}`);
|
|
131
137
|
applyInteractionMode(current, mode);
|
|
132
138
|
recorder.messageInteractions.set(messageID, current.id);
|
|
139
|
+
recorder.interactionUserMessages.set(current.id, messageID);
|
|
133
140
|
recorder.seenUserMessages.add(messageID);
|
|
134
141
|
return current;
|
|
135
142
|
}
|
|
@@ -143,6 +150,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
143
150
|
recorder.seenUserMessages.add(messageID);
|
|
144
151
|
const interaction = createInteraction(recorder, cleanPrompt, timestampMs, messageID, mode);
|
|
145
152
|
recorder.messageInteractions.set(messageID, interaction.id);
|
|
153
|
+
recorder.interactionUserMessages.set(interaction.id, messageID);
|
|
146
154
|
return interaction;
|
|
147
155
|
}
|
|
148
156
|
|
|
@@ -161,7 +169,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
161
169
|
|
|
162
170
|
function addStep(recorder, interaction, step) {
|
|
163
171
|
recorder.stepCounter += 1;
|
|
164
|
-
const stepID = `${recorder.id}:step:${recorder.stepCounter}
|
|
172
|
+
const stepID = configuredValue(step.id, `${recorder.id}:step:${recorder.stepCounter}`);
|
|
165
173
|
const normalized = {
|
|
166
174
|
id: stepID,
|
|
167
175
|
index: recorder.stepCounter,
|
|
@@ -231,13 +239,20 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
231
239
|
const recorder = ensureSession(sessionID, Date.now());
|
|
232
240
|
const interaction = ensureInteraction(recorder, Date.now(), "", inferInteractionMode(event));
|
|
233
241
|
addStep(recorder, interaction, {
|
|
242
|
+
id: `${recorder.id}:step:file:${safeID(filePath)}:${Date.now()}`,
|
|
234
243
|
kind: "file_edit",
|
|
235
244
|
phase: interaction.mode,
|
|
236
245
|
title: `Edit ${filePath}`,
|
|
237
246
|
started_at: new Date().toISOString(),
|
|
238
247
|
ended_at: new Date().toISOString(),
|
|
239
248
|
status: "success",
|
|
240
|
-
payload_json: {
|
|
249
|
+
payload_json: {
|
|
250
|
+
file_path: filePath,
|
|
251
|
+
language: inferLanguage(filePath),
|
|
252
|
+
parent_tool_step_id: recorder.lastToolStepByInteraction.get(interaction.id) || undefined,
|
|
253
|
+
parent_llm_step_id: recorder.lastLLMStepByInteraction.get(interaction.id) || undefined,
|
|
254
|
+
relationship: recorder.lastToolStepByInteraction.get(interaction.id) ? "caused_by_tool" : "caused_by_model",
|
|
255
|
+
},
|
|
241
256
|
});
|
|
242
257
|
return { sessionID, filePath, language: inferLanguage(filePath) };
|
|
243
258
|
}
|
|
@@ -328,7 +343,11 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
328
343
|
existing.finalized = true;
|
|
329
344
|
const targetInteraction = findInteraction(recorder, existing.interactionID) || interaction;
|
|
330
345
|
applyInteractionMode(targetInteraction, mode);
|
|
331
|
-
|
|
346
|
+
const parentMessageID = parentID || recorder.interactionUserMessages.get(targetInteraction.id) || null;
|
|
347
|
+
const loopIteration = targetInteraction.steps.filter((step) => step.kind === "llm_call").length + 1;
|
|
348
|
+
const previousToolStep = findPreviousToolStep(targetInteraction, id);
|
|
349
|
+
const llmStep = addStep(recorder, targetInteraction, {
|
|
350
|
+
id: `${recorder.id}:step:llm:${safeID(id)}`,
|
|
332
351
|
kind: "llm_call",
|
|
333
352
|
phase: targetInteraction.mode,
|
|
334
353
|
title: summarizePrompt(assistantText, `Model response for Interaction ${targetInteraction.index}`),
|
|
@@ -346,9 +365,32 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
346
365
|
cost_usd: finiteNumber(info && info.cost),
|
|
347
366
|
first_token_at: existing.firstTokenAt != null ? new Date(existing.firstTokenAt).toISOString() : null,
|
|
348
367
|
first_token_latency_ms: existing.firstTokenAt != null ? Math.max(0, existing.firstTokenAt - existing.firstSeenAt) : 0,
|
|
349
|
-
payload_json: {
|
|
368
|
+
payload_json: {
|
|
369
|
+
message_id: id,
|
|
370
|
+
parent_message_id: parentMessageID,
|
|
371
|
+
loop_iteration: loopIteration,
|
|
372
|
+
caused_by: previousToolStep ? {
|
|
373
|
+
type: "tool_result",
|
|
374
|
+
step_id: previousToolStep.id,
|
|
375
|
+
call_id: previousToolStep.payload_json?.call_id || null,
|
|
376
|
+
} : {
|
|
377
|
+
type: parentMessageID ? "user_prompt" : "session_start",
|
|
378
|
+
message_id: parentMessageID,
|
|
379
|
+
},
|
|
380
|
+
},
|
|
350
381
|
details: buildLLMStepDetails(recorder, targetInteraction, id),
|
|
351
382
|
});
|
|
383
|
+
recorder.messageLLMSteps.set(id, llmStep.id);
|
|
384
|
+
recorder.lastLLMStepByInteraction.set(targetInteraction.id, llmStep.id);
|
|
385
|
+
for (const step of targetInteraction.steps) {
|
|
386
|
+
if (step.kind !== "tool_call") continue;
|
|
387
|
+
if (step.payload_json?.message_id !== id) continue;
|
|
388
|
+
step.payload_json = {
|
|
389
|
+
...step.payload_json,
|
|
390
|
+
parent_llm_step_id: llmStep.id,
|
|
391
|
+
relationship: "requested_by_model",
|
|
392
|
+
};
|
|
393
|
+
}
|
|
352
394
|
addContextContributor(recorder, {
|
|
353
395
|
category: "assistant_history",
|
|
354
396
|
label: `Previous assistant response from Interaction ${targetInteraction.index}`,
|
|
@@ -383,6 +425,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
383
425
|
startedAt: Date.now(),
|
|
384
426
|
tool: configuredValue(input && input.tool, "unknown"),
|
|
385
427
|
interactionID,
|
|
428
|
+
messageID,
|
|
386
429
|
});
|
|
387
430
|
return { sessionID, callID, tool: configuredValue(input && input.tool, "unknown") };
|
|
388
431
|
}
|
|
@@ -402,7 +445,8 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
402
445
|
const mode = inferInteractionMode(input, output);
|
|
403
446
|
const interaction = mappedInteractionID ? findInteraction(recorder, mappedInteractionID) || ensureInteraction(recorder, Date.now(), "", mode) : ensureInteraction(recorder, Date.now(), "", mode);
|
|
404
447
|
applyInteractionMode(interaction, mode);
|
|
405
|
-
addStep(recorder, interaction, {
|
|
448
|
+
const toolStep = addStep(recorder, interaction, {
|
|
449
|
+
id: callID ? `${recorder.id}:step:tool:${safeID(callID)}` : `${recorder.id}:step:tool:${safeID(messageID || configuredValue(toolState.messageID, "") || Date.now())}`,
|
|
406
450
|
kind: "tool_call",
|
|
407
451
|
phase: interaction.mode,
|
|
408
452
|
title: `Run ${configuredValue(toolState.tool, "unknown")}`,
|
|
@@ -411,8 +455,18 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
411
455
|
duration_ms: Math.max(0, Date.now() - toolState.startedAt),
|
|
412
456
|
status: output && output.error ? "error" : "success",
|
|
413
457
|
tool_name: configuredValue(toolState.tool, "unknown"),
|
|
414
|
-
payload_json:
|
|
458
|
+
payload_json: {
|
|
459
|
+
...safeObject(output && output.metadata),
|
|
460
|
+
call_id: callID || null,
|
|
461
|
+
message_id: messageID || configuredValue(toolState.messageID, "") || null,
|
|
462
|
+
parent_llm_step_id: recorder.messageLLMSteps.get(messageID) || null,
|
|
463
|
+
relationship: "requested_by_model",
|
|
464
|
+
},
|
|
415
465
|
});
|
|
466
|
+
if (callID) {
|
|
467
|
+
recorder.toolStepsByCallID.set(callID, toolStep.id);
|
|
468
|
+
}
|
|
469
|
+
recorder.lastToolStepByInteraction.set(interaction.id, toolStep.id);
|
|
416
470
|
addContextContributor(recorder, {
|
|
417
471
|
category: "tool_output",
|
|
418
472
|
label: `Tool ${configuredValue(toolState.tool, "unknown")}`,
|
|
@@ -717,6 +771,20 @@ function buildInteractionPayload(interaction, sessionTitle = "") {
|
|
|
717
771
|
};
|
|
718
772
|
}
|
|
719
773
|
|
|
774
|
+
function findPreviousToolStep(interaction, messageID) {
|
|
775
|
+
for (let index = interaction.steps.length - 1; index >= 0; index -= 1) {
|
|
776
|
+
const step = interaction.steps[index];
|
|
777
|
+
if (step.kind !== "tool_call") continue;
|
|
778
|
+
if (messageID && step.payload_json?.message_id && step.payload_json.message_id !== messageID) continue;
|
|
779
|
+
return step;
|
|
780
|
+
}
|
|
781
|
+
return null;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
function safeObject(value) {
|
|
785
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
786
|
+
}
|
|
787
|
+
|
|
720
788
|
function isGenericInteractionLabel(value) {
|
|
721
789
|
const label = String(value || "").trim();
|
|
722
790
|
return /^OpenCode interaction \d+$/i.test(label) || /^Interaction \d+$/i.test(label);
|