@distlang/opencode-plugin 0.1.7 → 0.1.9
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/package.json +1 -1
- package/src/index.js +59 -14
- package/src/recorder.js +44 -7
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createRecorder } from "./recorder.js";
|
|
1
|
+
import { createRecorder, extractSessionTitle } from "./recorder.js";
|
|
2
2
|
import { extractDistlangInvocation } from "./command.js";
|
|
3
3
|
import { distlangCommandInfo, fetchAIDebuggerSessions, getAuthStatus, loginWithDistlang, resolveDistlangBinary, uploadAIDebuggerPayload } from "./distlang.js";
|
|
4
4
|
import { pluginStatePath, readPluginState, writePluginState } from "./state.js";
|
|
@@ -19,7 +19,7 @@ function debugLogFile() {
|
|
|
19
19
|
|
|
20
20
|
export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
21
21
|
const debug = debugEnabled();
|
|
22
|
-
const recorder = createRecorder({ project, directory });
|
|
22
|
+
const recorder = createRecorder({ project, directory, initialPrompt: extractOpenCodeRunPrompt(process.argv) });
|
|
23
23
|
let loggedInit = false;
|
|
24
24
|
let authWarningLogged = false;
|
|
25
25
|
let distlangMissingLogged = false;
|
|
@@ -193,7 +193,7 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
193
193
|
async function finalizeSession(sessionID, result) {
|
|
194
194
|
let payload;
|
|
195
195
|
try {
|
|
196
|
-
await refreshSessionMetadata(sessionID);
|
|
196
|
+
await refreshSessionMetadata(sessionID, { retry: true });
|
|
197
197
|
payload = recorder.finalizeSession(sessionID, result, Date.now());
|
|
198
198
|
} catch (error) {
|
|
199
199
|
await log("error", "AI Debugger session finalization failed", { sessionID, result, error: String(error) });
|
|
@@ -230,7 +230,7 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
230
230
|
async function uploadSessionSnapshot(sessionID, result = "success") {
|
|
231
231
|
let payload;
|
|
232
232
|
try {
|
|
233
|
-
await refreshSessionMetadata(sessionID);
|
|
233
|
+
await refreshSessionMetadata(sessionID, { retry: true });
|
|
234
234
|
payload = recorder.snapshotSession(sessionID, result, Date.now());
|
|
235
235
|
} catch (error) {
|
|
236
236
|
await log("error", "AI Debugger session snapshot failed", { sessionID, result, error: String(error) });
|
|
@@ -264,21 +264,66 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
264
264
|
}
|
|
265
265
|
}
|
|
266
266
|
|
|
267
|
-
async function refreshSessionMetadata(sessionID) {
|
|
267
|
+
async function refreshSessionMetadata(sessionID, options = {}) {
|
|
268
268
|
if (!sessionID || !client?.session || typeof client.session.get !== "function") {
|
|
269
269
|
return;
|
|
270
270
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
await debugLog("OpenCode session metadata
|
|
271
|
+
const attempts = options.retry ? 3 : 1;
|
|
272
|
+
for (let attempt = 1; attempt <= attempts; attempt += 1) {
|
|
273
|
+
try {
|
|
274
|
+
const response = await client.session.get({ path: { id: sessionID } });
|
|
275
|
+
const session = response?.data ?? response;
|
|
276
|
+
const title = extractSessionTitle(session);
|
|
277
|
+
await debugLog("OpenCode session metadata inspected", { sessionID, attempt, sessionKeys: session && typeof session === "object" ? Object.keys(session) : [] });
|
|
278
|
+
if (title) {
|
|
279
|
+
recorder.setSessionTitle(sessionID, title);
|
|
280
|
+
await debugLog("OpenCode session metadata refreshed", { sessionID, attempt, titleLength: title.length });
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
} catch (error) {
|
|
284
|
+
await debugLog("OpenCode session metadata refresh failed", { sessionID, attempt, error: String(error) });
|
|
278
285
|
}
|
|
279
|
-
|
|
280
|
-
|
|
286
|
+
if (attempt < attempts) {
|
|
287
|
+
await sleep(500);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function sleep(ms) {
|
|
293
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function extractOpenCodeRunPrompt(argv) {
|
|
297
|
+
const args = Array.isArray(argv) ? argv : [];
|
|
298
|
+
const runIndex = args.findIndex((arg) => arg === "run");
|
|
299
|
+
if (runIndex < 0) {
|
|
300
|
+
return "";
|
|
301
|
+
}
|
|
302
|
+
const promptParts = [];
|
|
303
|
+
for (let index = runIndex + 1; index < args.length; index += 1) {
|
|
304
|
+
const arg = configuredValue(args[index], "");
|
|
305
|
+
if (!arg) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (arg === "--") {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (arg === "--dangerously-skip-permissions") {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (arg === "--model" || arg === "-m") {
|
|
315
|
+
index += 1;
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (arg.startsWith("--model=")) {
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (arg.startsWith("-")) {
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
promptParts.push(arg);
|
|
281
325
|
}
|
|
326
|
+
return promptParts.join(" ").trim();
|
|
282
327
|
}
|
|
283
328
|
|
|
284
329
|
function messageShape(event) {
|
package/src/recorder.js
CHANGED
|
@@ -32,8 +32,9 @@ const extensionLanguages = {
|
|
|
32
32
|
".yml": "yaml",
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
export function createRecorder({ project, directory }) {
|
|
35
|
+
export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
36
36
|
const projectName = deriveProjectName(project, directory);
|
|
37
|
+
const configuredInitialPrompt = sanitizeDebuggerText(initialPrompt);
|
|
37
38
|
const sessions = new Map();
|
|
38
39
|
|
|
39
40
|
function activeSessionID() {
|
|
@@ -56,6 +57,7 @@ export function createRecorder({ project, directory }) {
|
|
|
56
57
|
status: "success",
|
|
57
58
|
summary: "",
|
|
58
59
|
title: "",
|
|
60
|
+
initialPrompt: configuredInitialPrompt,
|
|
59
61
|
interactions: [],
|
|
60
62
|
currentInteractionID: "",
|
|
61
63
|
interactionCounter: 0,
|
|
@@ -93,12 +95,12 @@ export function createRecorder({ project, directory }) {
|
|
|
93
95
|
function ensureInteraction(recorder, timestampMs, prompt = "") {
|
|
94
96
|
const current = findInteraction(recorder, recorder.currentInteractionID);
|
|
95
97
|
if (current) {
|
|
96
|
-
if (prompt && (!current.prompt || current.prompt
|
|
98
|
+
if (prompt && (!current.prompt || isGenericInteractionLabel(current.prompt))) {
|
|
97
99
|
current.prompt = prompt;
|
|
98
100
|
}
|
|
99
101
|
return current;
|
|
100
102
|
}
|
|
101
|
-
return createInteraction(recorder, prompt, timestampMs);
|
|
103
|
+
return createInteraction(recorder, configuredValue(prompt, recorder.initialPrompt), timestampMs);
|
|
102
104
|
}
|
|
103
105
|
|
|
104
106
|
function addStep(recorder, interaction, step) {
|
|
@@ -139,7 +141,7 @@ export function createRecorder({ project, directory }) {
|
|
|
139
141
|
return null;
|
|
140
142
|
}
|
|
141
143
|
const recorder = ensureSession(sessionID, parseTimestamp(sessionInfo.time, Date.now()));
|
|
142
|
-
updateSessionTitle(recorder,
|
|
144
|
+
updateSessionTitle(recorder, event);
|
|
143
145
|
return { sessionID };
|
|
144
146
|
}
|
|
145
147
|
|
|
@@ -150,7 +152,7 @@ export function createRecorder({ project, directory }) {
|
|
|
150
152
|
return null;
|
|
151
153
|
}
|
|
152
154
|
const recorder = ensureSession(sessionID, parseTimestamp(sessionInfo.time, Date.now()));
|
|
153
|
-
const title = updateSessionTitle(recorder,
|
|
155
|
+
const title = updateSessionTitle(recorder, event);
|
|
154
156
|
return { sessionID, title };
|
|
155
157
|
}
|
|
156
158
|
|
|
@@ -372,7 +374,7 @@ function buildSessionPayload(recorder) {
|
|
|
372
374
|
ended_at: new Date(recorder.endedAtMs || Date.now()).toISOString(),
|
|
373
375
|
duration_ms: Math.max(0, (recorder.endedAtMs || Date.now()) - recorder.startedAtMs),
|
|
374
376
|
status: inferResultStatus(interactions.map((interaction) => interaction.status), recorder.status),
|
|
375
|
-
summary:
|
|
377
|
+
summary: bestSessionSummary(recorder, interactions),
|
|
376
378
|
total_cost_usd: allSteps.reduce((total, step) => total + finiteNumber(step.cost_usd), 0),
|
|
377
379
|
input_tokens: allSteps.reduce((total, step) => total + finiteNumber(step.input_tokens), 0),
|
|
378
380
|
output_tokens: allSteps.reduce((total, step) => total + finiteNumber(step.output_tokens), 0),
|
|
@@ -585,7 +587,7 @@ function firstNumber(sources, keys) {
|
|
|
585
587
|
}
|
|
586
588
|
|
|
587
589
|
function updateSessionTitle(recorder, info) {
|
|
588
|
-
const title =
|
|
590
|
+
const title = extractSessionTitle(info);
|
|
589
591
|
if (title) {
|
|
590
592
|
recorder.title = title;
|
|
591
593
|
recorder.summary = title;
|
|
@@ -593,6 +595,41 @@ function updateSessionTitle(recorder, info) {
|
|
|
593
595
|
return recorder.title;
|
|
594
596
|
}
|
|
595
597
|
|
|
598
|
+
export function extractSessionTitle(info) {
|
|
599
|
+
const title = findSessionTitle(info, new Set());
|
|
600
|
+
return isGenericInteractionLabel(title) ? "" : title;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function findSessionTitle(value, seen) {
|
|
604
|
+
if (!value || typeof value !== "object" || seen.has(value)) {
|
|
605
|
+
return "";
|
|
606
|
+
}
|
|
607
|
+
seen.add(value);
|
|
608
|
+
for (const key of ["title", "name", "summary"]) {
|
|
609
|
+
const candidate = sanitizeDebuggerText(configuredValue(value[key], ""));
|
|
610
|
+
if (candidate && !isGenericInteractionLabel(candidate)) {
|
|
611
|
+
return candidate;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
for (const key of ["session", "metadata", "info", "properties", "data"]) {
|
|
615
|
+
const candidate = findSessionTitle(value[key], seen);
|
|
616
|
+
if (candidate) {
|
|
617
|
+
return candidate;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
return "";
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function bestSessionSummary(recorder, interactions) {
|
|
624
|
+
for (const candidate of [recorder.title, recorder.summary, interactions[0]?.prompt, interactions[0]?.summary]) {
|
|
625
|
+
const cleaned = sanitizeDebuggerText(configuredValue(candidate, ""));
|
|
626
|
+
if (cleaned && !isGenericInteractionLabel(cleaned)) {
|
|
627
|
+
return cleaned;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return `OpenCode session ${recorder.id}`;
|
|
631
|
+
}
|
|
632
|
+
|
|
596
633
|
function extractMessageText(info) {
|
|
597
634
|
const direct = sanitizeDebuggerText(configuredValue(info && (info.text ?? info.prompt ?? info.message ?? info.body), ""));
|
|
598
635
|
if (direct !== "") {
|