@distlang/opencode-plugin 0.1.6 → 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 +54 -9
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
|
|
|
@@ -357,7 +357,8 @@ export function createRecorder({ project, directory }) {
|
|
|
357
357
|
}
|
|
358
358
|
|
|
359
359
|
function buildSessionPayload(recorder) {
|
|
360
|
-
const
|
|
360
|
+
const sessionTitle = sanitizeDebuggerText(recorder.title);
|
|
361
|
+
const interactions = recorder.interactions.map((interaction) => buildInteractionPayload(interaction, sessionTitle));
|
|
361
362
|
const allSteps = interactions.flatMap((interaction) => interaction.steps);
|
|
362
363
|
const llmSteps = allSteps.filter((step) => step.kind === "llm_call");
|
|
363
364
|
const contextValues = llmSteps.map((step) => step.context_size_tokens).filter((value) => value > 0);
|
|
@@ -371,7 +372,7 @@ function buildSessionPayload(recorder) {
|
|
|
371
372
|
ended_at: new Date(recorder.endedAtMs || Date.now()).toISOString(),
|
|
372
373
|
duration_ms: Math.max(0, (recorder.endedAtMs || Date.now()) - recorder.startedAtMs),
|
|
373
374
|
status: inferResultStatus(interactions.map((interaction) => interaction.status), recorder.status),
|
|
374
|
-
summary:
|
|
375
|
+
summary: bestSessionSummary(recorder, interactions),
|
|
375
376
|
total_cost_usd: allSteps.reduce((total, step) => total + finiteNumber(step.cost_usd), 0),
|
|
376
377
|
input_tokens: allSteps.reduce((total, step) => total + finiteNumber(step.input_tokens), 0),
|
|
377
378
|
output_tokens: allSteps.reduce((total, step) => total + finiteNumber(step.output_tokens), 0),
|
|
@@ -389,10 +390,14 @@ function buildSessionPayload(recorder) {
|
|
|
389
390
|
};
|
|
390
391
|
}
|
|
391
392
|
|
|
392
|
-
function buildInteractionPayload(interaction) {
|
|
393
|
+
function buildInteractionPayload(interaction, sessionTitle = "") {
|
|
393
394
|
const llmSteps = interaction.steps.filter((step) => step.kind === "llm_call");
|
|
394
395
|
const contextValues = llmSteps.map((step) => step.context_size_tokens).filter((value) => value > 0);
|
|
395
|
-
const
|
|
396
|
+
const promptFallback = configuredValue(sessionTitle, `Interaction ${interaction.index}`);
|
|
397
|
+
const rawPrompt = sanitizeDebuggerText(interaction.prompt);
|
|
398
|
+
const rawSummary = sanitizeDebuggerText(interaction.summary);
|
|
399
|
+
const prompt = isGenericInteractionLabel(rawPrompt) ? promptFallback : configuredValue(rawPrompt, promptFallback);
|
|
400
|
+
const summaryFallback = summarizePrompt(prompt, promptFallback);
|
|
396
401
|
return {
|
|
397
402
|
id: interaction.id,
|
|
398
403
|
index: interaction.index,
|
|
@@ -402,7 +407,7 @@ function buildInteractionPayload(interaction) {
|
|
|
402
407
|
ended_at: interaction.endedAtMs > 0 ? new Date(interaction.endedAtMs).toISOString() : new Date(Date.now()).toISOString(),
|
|
403
408
|
duration_ms: Math.max(0, (interaction.endedAtMs || Date.now()) - interaction.startedAtMs),
|
|
404
409
|
status: inferResultStatus(interaction.steps.map((step) => step.status), interaction.status),
|
|
405
|
-
summary:
|
|
410
|
+
summary: isGenericInteractionLabel(rawSummary) ? summaryFallback : configuredValue(rawSummary, summaryFallback),
|
|
406
411
|
llm_call_count: llmSteps.length,
|
|
407
412
|
cost_usd: interaction.steps.reduce((total, step) => total + finiteNumber(step.cost_usd), 0),
|
|
408
413
|
input_tokens: interaction.steps.reduce((total, step) => total + finiteNumber(step.input_tokens), 0),
|
|
@@ -417,6 +422,11 @@ function buildInteractionPayload(interaction) {
|
|
|
417
422
|
};
|
|
418
423
|
}
|
|
419
424
|
|
|
425
|
+
function isGenericInteractionLabel(value) {
|
|
426
|
+
const label = String(value || "").trim();
|
|
427
|
+
return /^OpenCode interaction \d+$/i.test(label) || /^Interaction \d+$/i.test(label);
|
|
428
|
+
}
|
|
429
|
+
|
|
420
430
|
function configuredValue(value, fallback = "") {
|
|
421
431
|
return typeof value === "string" && value.trim() !== "" ? value.trim() : fallback;
|
|
422
432
|
}
|
|
@@ -575,7 +585,7 @@ function firstNumber(sources, keys) {
|
|
|
575
585
|
}
|
|
576
586
|
|
|
577
587
|
function updateSessionTitle(recorder, info) {
|
|
578
|
-
const title =
|
|
588
|
+
const title = extractSessionTitle(info);
|
|
579
589
|
if (title) {
|
|
580
590
|
recorder.title = title;
|
|
581
591
|
recorder.summary = title;
|
|
@@ -583,6 +593,41 @@ function updateSessionTitle(recorder, info) {
|
|
|
583
593
|
return recorder.title;
|
|
584
594
|
}
|
|
585
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
|
+
|
|
586
631
|
function extractMessageText(info) {
|
|
587
632
|
const direct = sanitizeDebuggerText(configuredValue(info && (info.text ?? info.prompt ?? info.message ?? info.body), ""));
|
|
588
633
|
if (direct !== "") {
|