@distlang/opencode-plugin 0.1.7 → 0.1.8
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 +25 -13
- package/src/recorder.js +40 -5
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";
|
|
@@ -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,23 +264,35 @@ 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) });
|
|
285
|
+
}
|
|
286
|
+
if (attempt < attempts) {
|
|
287
|
+
await sleep(500);
|
|
278
288
|
}
|
|
279
|
-
} catch (error) {
|
|
280
|
-
await debugLog("OpenCode session metadata refresh failed", { sessionID, error: String(error) });
|
|
281
289
|
}
|
|
282
290
|
}
|
|
283
291
|
|
|
292
|
+
function sleep(ms) {
|
|
293
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
294
|
+
}
|
|
295
|
+
|
|
284
296
|
function messageShape(event) {
|
|
285
297
|
const info = event && typeof event === "object" ? event.info ?? event.properties?.info : null;
|
|
286
298
|
if (!info || typeof info !== "object") {
|
package/src/recorder.js
CHANGED
|
@@ -93,7 +93,7 @@ export function createRecorder({ project, directory }) {
|
|
|
93
93
|
function ensureInteraction(recorder, timestampMs, prompt = "") {
|
|
94
94
|
const current = findInteraction(recorder, recorder.currentInteractionID);
|
|
95
95
|
if (current) {
|
|
96
|
-
if (prompt && (!current.prompt || current.prompt
|
|
96
|
+
if (prompt && (!current.prompt || isGenericInteractionLabel(current.prompt))) {
|
|
97
97
|
current.prompt = prompt;
|
|
98
98
|
}
|
|
99
99
|
return current;
|
|
@@ -139,7 +139,7 @@ export function createRecorder({ project, directory }) {
|
|
|
139
139
|
return null;
|
|
140
140
|
}
|
|
141
141
|
const recorder = ensureSession(sessionID, parseTimestamp(sessionInfo.time, Date.now()));
|
|
142
|
-
updateSessionTitle(recorder,
|
|
142
|
+
updateSessionTitle(recorder, event);
|
|
143
143
|
return { sessionID };
|
|
144
144
|
}
|
|
145
145
|
|
|
@@ -150,7 +150,7 @@ export function createRecorder({ project, directory }) {
|
|
|
150
150
|
return null;
|
|
151
151
|
}
|
|
152
152
|
const recorder = ensureSession(sessionID, parseTimestamp(sessionInfo.time, Date.now()));
|
|
153
|
-
const title = updateSessionTitle(recorder,
|
|
153
|
+
const title = updateSessionTitle(recorder, event);
|
|
154
154
|
return { sessionID, title };
|
|
155
155
|
}
|
|
156
156
|
|
|
@@ -372,7 +372,7 @@ function buildSessionPayload(recorder) {
|
|
|
372
372
|
ended_at: new Date(recorder.endedAtMs || Date.now()).toISOString(),
|
|
373
373
|
duration_ms: Math.max(0, (recorder.endedAtMs || Date.now()) - recorder.startedAtMs),
|
|
374
374
|
status: inferResultStatus(interactions.map((interaction) => interaction.status), recorder.status),
|
|
375
|
-
summary:
|
|
375
|
+
summary: bestSessionSummary(recorder, interactions),
|
|
376
376
|
total_cost_usd: allSteps.reduce((total, step) => total + finiteNumber(step.cost_usd), 0),
|
|
377
377
|
input_tokens: allSteps.reduce((total, step) => total + finiteNumber(step.input_tokens), 0),
|
|
378
378
|
output_tokens: allSteps.reduce((total, step) => total + finiteNumber(step.output_tokens), 0),
|
|
@@ -585,7 +585,7 @@ function firstNumber(sources, keys) {
|
|
|
585
585
|
}
|
|
586
586
|
|
|
587
587
|
function updateSessionTitle(recorder, info) {
|
|
588
|
-
const title =
|
|
588
|
+
const title = extractSessionTitle(info);
|
|
589
589
|
if (title) {
|
|
590
590
|
recorder.title = title;
|
|
591
591
|
recorder.summary = title;
|
|
@@ -593,6 +593,41 @@ function updateSessionTitle(recorder, info) {
|
|
|
593
593
|
return recorder.title;
|
|
594
594
|
}
|
|
595
595
|
|
|
596
|
+
export function extractSessionTitle(info) {
|
|
597
|
+
const title = findSessionTitle(info, new Set());
|
|
598
|
+
return isGenericInteractionLabel(title) ? "" : title;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
function findSessionTitle(value, seen) {
|
|
602
|
+
if (!value || typeof value !== "object" || seen.has(value)) {
|
|
603
|
+
return "";
|
|
604
|
+
}
|
|
605
|
+
seen.add(value);
|
|
606
|
+
for (const key of ["title", "name", "summary"]) {
|
|
607
|
+
const candidate = sanitizeDebuggerText(configuredValue(value[key], ""));
|
|
608
|
+
if (candidate && !isGenericInteractionLabel(candidate)) {
|
|
609
|
+
return candidate;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
for (const key of ["session", "metadata", "info", "properties", "data"]) {
|
|
613
|
+
const candidate = findSessionTitle(value[key], seen);
|
|
614
|
+
if (candidate) {
|
|
615
|
+
return candidate;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return "";
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
function bestSessionSummary(recorder, interactions) {
|
|
622
|
+
for (const candidate of [recorder.title, recorder.summary, interactions[0]?.prompt, interactions[0]?.summary]) {
|
|
623
|
+
const cleaned = sanitizeDebuggerText(configuredValue(candidate, ""));
|
|
624
|
+
if (cleaned && !isGenericInteractionLabel(cleaned)) {
|
|
625
|
+
return cleaned;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return `OpenCode session ${recorder.id}`;
|
|
629
|
+
}
|
|
630
|
+
|
|
596
631
|
function extractMessageText(info) {
|
|
597
632
|
const direct = sanitizeDebuggerText(configuredValue(info && (info.text ?? info.prompt ?? info.message ?? info.body), ""));
|
|
598
633
|
if (direct !== "") {
|