@distlang/opencode-plugin 0.1.11 → 0.1.13
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 +12 -2
- package/src/recorder.js +171 -1
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -375,15 +375,16 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
375
375
|
}
|
|
376
376
|
|
|
377
377
|
|
|
378
|
+
|
|
378
379
|
if (event.type === "session.idle" || event.type === "session.error") {
|
|
379
380
|
const sessionID = configuredValue(event.sessionID, recorder.activeSessionID());
|
|
380
381
|
if (!sessionID) {
|
|
381
|
-
await debugLog("Session
|
|
382
|
+
await debugLog("Session snapshot event missing sessionID", { type: event.type, eventKeys: Object.keys(event) });
|
|
382
383
|
return;
|
|
383
384
|
}
|
|
384
385
|
const result = event.type === "session.error" ? "error" : "success";
|
|
385
386
|
await debugLog(`${event.type} observed`, { sessionID, result });
|
|
386
|
-
await
|
|
387
|
+
await uploadSessionSnapshot(sessionID, result);
|
|
387
388
|
return;
|
|
388
389
|
}
|
|
389
390
|
|
|
@@ -456,6 +457,15 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
456
457
|
await debugLog("tool.execute.after observed", observed);
|
|
457
458
|
}
|
|
458
459
|
},
|
|
460
|
+
|
|
461
|
+
"experimental.chat.system.transform": async (input, output) => {
|
|
462
|
+
await logInit();
|
|
463
|
+
const sessionID = configuredValue(input && input.sessionID, recorder.activeSessionID());
|
|
464
|
+
const observed = recorder.observeSystemPrompt(sessionID, output && output.system);
|
|
465
|
+
if (observed) {
|
|
466
|
+
await debugLog("experimental.chat.system.transform observed", observed);
|
|
467
|
+
}
|
|
468
|
+
},
|
|
459
469
|
};
|
|
460
470
|
};
|
|
461
471
|
|
package/src/recorder.js
CHANGED
|
@@ -68,6 +68,8 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
68
68
|
messageInteractions: new Map(),
|
|
69
69
|
toolStarts: new Map(),
|
|
70
70
|
seenUserMessages: new Set(),
|
|
71
|
+
contextContributors: [],
|
|
72
|
+
systemContributors: [],
|
|
71
73
|
};
|
|
72
74
|
sessions.set(sessionID, recorder);
|
|
73
75
|
return recorder;
|
|
@@ -149,8 +151,9 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
149
151
|
|
|
150
152
|
function addStep(recorder, interaction, step) {
|
|
151
153
|
recorder.stepCounter += 1;
|
|
154
|
+
const stepID = `${recorder.id}:step:${recorder.stepCounter}`;
|
|
152
155
|
const normalized = {
|
|
153
|
-
id:
|
|
156
|
+
id: stepID,
|
|
154
157
|
index: recorder.stepCounter,
|
|
155
158
|
kind: step.kind,
|
|
156
159
|
phase: configuredValue(step.phase, "build"),
|
|
@@ -171,6 +174,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
171
174
|
first_token_at: step.first_token_at ? normalizeDateTime(step.first_token_at, new Date().toISOString()) : null,
|
|
172
175
|
first_token_latency_ms: Math.max(0, Math.floor(finiteNumber(step.first_token_latency_ms))),
|
|
173
176
|
payload_json: step.payload_json ?? null,
|
|
177
|
+
details: normalizeStepDetails(step.details, stepID, interaction.id),
|
|
174
178
|
};
|
|
175
179
|
interaction.steps.push(normalized);
|
|
176
180
|
interaction.endedAtMs = Math.max(interaction.endedAtMs || 0, Date.parse(normalized.ended_at || normalized.started_at));
|
|
@@ -242,6 +246,12 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
242
246
|
return { sessionID, messageID: id, prompt: "" };
|
|
243
247
|
}
|
|
244
248
|
const interaction = observeUserPrompt(recorder, id, prompt, parseMessageTime(info, Date.now()));
|
|
249
|
+
addContextContributor(recorder, {
|
|
250
|
+
category: "user_prompt",
|
|
251
|
+
label: `User prompt ${interaction.index}`,
|
|
252
|
+
source: "opencode.message",
|
|
253
|
+
text: prompt,
|
|
254
|
+
});
|
|
245
255
|
return { sessionID, messageID: id, prompt: interaction.prompt };
|
|
246
256
|
}
|
|
247
257
|
|
|
@@ -323,6 +333,13 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
323
333
|
first_token_at: existing.firstTokenAt != null ? new Date(existing.firstTokenAt).toISOString() : null,
|
|
324
334
|
first_token_latency_ms: existing.firstTokenAt != null ? Math.max(0, existing.firstTokenAt - existing.firstSeenAt) : 0,
|
|
325
335
|
payload_json: { message_id: id },
|
|
336
|
+
details: buildLLMStepDetails(recorder, targetInteraction, id),
|
|
337
|
+
});
|
|
338
|
+
addContextContributor(recorder, {
|
|
339
|
+
category: "assistant_history",
|
|
340
|
+
label: `Assistant response ${targetInteraction.index}`,
|
|
341
|
+
source: "opencode.message",
|
|
342
|
+
text: assistantText,
|
|
326
343
|
});
|
|
327
344
|
}
|
|
328
345
|
recorder.assistantStates.set(id, existing);
|
|
@@ -378,6 +395,13 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
378
395
|
tool_name: configuredValue(toolState.tool, "unknown"),
|
|
379
396
|
payload_json: output && output.metadata && typeof output.metadata === "object" ? output.metadata : null,
|
|
380
397
|
});
|
|
398
|
+
addContextContributor(recorder, {
|
|
399
|
+
category: "tool_output",
|
|
400
|
+
label: `Tool ${configuredValue(toolState.tool, "unknown")}`,
|
|
401
|
+
source: "opencode.tool",
|
|
402
|
+
text: extractToolOutputText(output),
|
|
403
|
+
metadata: safeToolMetadata(output),
|
|
404
|
+
});
|
|
381
405
|
if (callID) {
|
|
382
406
|
recorder.toolStarts.delete(callID);
|
|
383
407
|
}
|
|
@@ -420,6 +444,22 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
420
444
|
return buildSessionPayload(recorder);
|
|
421
445
|
}
|
|
422
446
|
|
|
447
|
+
function observeSystemPrompt(sessionID, system) {
|
|
448
|
+
const id = configuredValue(sessionID, activeSessionID());
|
|
449
|
+
if (!id) {
|
|
450
|
+
return null;
|
|
451
|
+
}
|
|
452
|
+
const recorder = ensureSession(id, Date.now());
|
|
453
|
+
const entries = Array.isArray(system) ? system : [];
|
|
454
|
+
recorder.systemContributors = entries.map((text, index) => contextContributor({
|
|
455
|
+
category: index === 0 ? "system_prompt" : "instructions",
|
|
456
|
+
label: index === 0 ? "OpenCode system prompt" : `System instruction ${index + 1}`,
|
|
457
|
+
source: "opencode.system",
|
|
458
|
+
text: String(text || ""),
|
|
459
|
+
})).filter((item) => item.char_count > 0);
|
|
460
|
+
return { sessionID: id, details: recorder.systemContributors.length };
|
|
461
|
+
}
|
|
462
|
+
|
|
423
463
|
return {
|
|
424
464
|
activeSessionID,
|
|
425
465
|
observeSessionCreated,
|
|
@@ -433,6 +473,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
|
|
|
433
473
|
observeToolAfter,
|
|
434
474
|
finalizeSession,
|
|
435
475
|
snapshotSession,
|
|
476
|
+
observeSystemPrompt,
|
|
436
477
|
};
|
|
437
478
|
}
|
|
438
479
|
|
|
@@ -442,6 +483,135 @@ function storeMessageTextPart(recorder, messageID, partID, text) {
|
|
|
442
483
|
recorder.messageTextParts.set(messageID, parts);
|
|
443
484
|
}
|
|
444
485
|
|
|
486
|
+
function addContextContributor(recorder, input) {
|
|
487
|
+
const contributor = contextContributor(input);
|
|
488
|
+
if (contributor.char_count <= 0) {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
recorder.contextContributors.push(contributor);
|
|
492
|
+
if (recorder.contextContributors.length > 200) {
|
|
493
|
+
recorder.contextContributors = recorder.contextContributors.slice(-200);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function contextContributor(input) {
|
|
498
|
+
const text = configuredValue(input && input.text, "");
|
|
499
|
+
const charCount = text.length;
|
|
500
|
+
return {
|
|
501
|
+
category: configuredValue(input && input.category, "unknown"),
|
|
502
|
+
label: safeDetailLabel(input && input.label, configuredValue(input && input.category, "unknown")),
|
|
503
|
+
source: configuredValue(input && input.source, "opencode"),
|
|
504
|
+
token_estimate: estimateTokens(text),
|
|
505
|
+
char_count: charCount,
|
|
506
|
+
item_count: 1,
|
|
507
|
+
metadata_json: input && input.metadata && typeof input.metadata === "object" ? input.metadata : null,
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function buildLLMStepDetails(recorder, interaction, assistantMessageID) {
|
|
512
|
+
const contributors = [
|
|
513
|
+
...recorder.systemContributors,
|
|
514
|
+
...recorder.contextContributors,
|
|
515
|
+
].filter((item) => item && item.char_count > 0);
|
|
516
|
+
const byCategory = new Map();
|
|
517
|
+
for (const item of contributors) {
|
|
518
|
+
const category = configuredValue(item.category, "unknown");
|
|
519
|
+
const current = byCategory.get(category) || {
|
|
520
|
+
category,
|
|
521
|
+
label: detailCategoryLabel(category),
|
|
522
|
+
token_estimate: 0,
|
|
523
|
+
char_count: 0,
|
|
524
|
+
item_count: 0,
|
|
525
|
+
source: item.source,
|
|
526
|
+
top_items: [],
|
|
527
|
+
};
|
|
528
|
+
current.token_estimate += Math.max(0, Math.floor(finiteNumber(item.token_estimate)));
|
|
529
|
+
current.char_count += Math.max(0, Math.floor(finiteNumber(item.char_count)));
|
|
530
|
+
current.item_count += Math.max(1, Math.floor(finiteNumber(item.item_count)) || 1);
|
|
531
|
+
current.top_items.push({
|
|
532
|
+
label: safeDetailLabel(item.label, detailCategoryLabel(category)),
|
|
533
|
+
token_estimate: Math.max(0, Math.floor(finiteNumber(item.token_estimate))),
|
|
534
|
+
char_count: Math.max(0, Math.floor(finiteNumber(item.char_count))),
|
|
535
|
+
source: configuredValue(item.source, "opencode"),
|
|
536
|
+
...(item.metadata_json ? { metadata: item.metadata_json } : {}),
|
|
537
|
+
});
|
|
538
|
+
byCategory.set(category, current);
|
|
539
|
+
}
|
|
540
|
+
return Array.from(byCategory.values())
|
|
541
|
+
.sort((left, right) => right.token_estimate - left.token_estimate || left.category.localeCompare(right.category))
|
|
542
|
+
.map((item, index) => ({
|
|
543
|
+
id: `${assistantMessageID || interaction.id}:detail:${index + 1}`,
|
|
544
|
+
index: index + 1,
|
|
545
|
+
category: item.category,
|
|
546
|
+
label: item.label,
|
|
547
|
+
token_estimate: item.token_estimate,
|
|
548
|
+
char_count: item.char_count,
|
|
549
|
+
item_count: item.item_count,
|
|
550
|
+
source: item.source,
|
|
551
|
+
metadata_json: {
|
|
552
|
+
top_items: item.top_items
|
|
553
|
+
.sort((left, right) => right.token_estimate - left.token_estimate)
|
|
554
|
+
.slice(0, 5),
|
|
555
|
+
},
|
|
556
|
+
}));
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function normalizeStepDetails(details, stepID, interactionID) {
|
|
560
|
+
if (!Array.isArray(details)) {
|
|
561
|
+
return [];
|
|
562
|
+
}
|
|
563
|
+
return details.map((detail, index) => ({
|
|
564
|
+
id: configuredValue(detail && detail.id, `${stepID}:detail:${index + 1}`),
|
|
565
|
+
step_id: stepID,
|
|
566
|
+
interaction_id: interactionID,
|
|
567
|
+
index: Math.max(1, Math.floor(finiteNumber(detail && detail.index)) || index + 1),
|
|
568
|
+
category: configuredValue(detail && detail.category, "unknown"),
|
|
569
|
+
label: safeDetailLabel(detail && detail.label, configuredValue(detail && detail.category, "unknown")),
|
|
570
|
+
token_estimate: Math.max(0, Math.floor(finiteNumber(detail && detail.token_estimate))),
|
|
571
|
+
char_count: Math.max(0, Math.floor(finiteNumber(detail && detail.char_count))),
|
|
572
|
+
item_count: Math.max(0, Math.floor(finiteNumber(detail && detail.item_count))),
|
|
573
|
+
source: configuredValue(detail && detail.source, "") || null,
|
|
574
|
+
metadata_json: detail && detail.metadata_json && typeof detail.metadata_json === "object" ? detail.metadata_json : null,
|
|
575
|
+
}));
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function estimateTokens(text) {
|
|
579
|
+
return Math.max(0, Math.round(String(text || "").length / 4));
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function detailCategoryLabel(category) {
|
|
583
|
+
return String(category || "unknown").split("_").map((part) => part ? part[0].toUpperCase() + part.slice(1) : part).join(" ");
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function safeDetailLabel(value, fallback) {
|
|
587
|
+
const label = configuredValue(value, fallback).replace(/\s+/g, " ").trim();
|
|
588
|
+
return label.length > 120 ? `${label.slice(0, 117)}...` : label;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function extractToolOutputText(output) {
|
|
592
|
+
if (!output || typeof output !== "object") {
|
|
593
|
+
return "";
|
|
594
|
+
}
|
|
595
|
+
if (typeof output.output === "string") {
|
|
596
|
+
return output.output;
|
|
597
|
+
}
|
|
598
|
+
if (typeof output.text === "string") {
|
|
599
|
+
return output.text;
|
|
600
|
+
}
|
|
601
|
+
if (output.metadata && typeof output.metadata.output === "string") {
|
|
602
|
+
return output.metadata.output;
|
|
603
|
+
}
|
|
604
|
+
if (Array.isArray(output.content)) {
|
|
605
|
+
return output.content.map((item) => item && typeof item.text === "string" ? item.text : "").filter(Boolean).join("\n");
|
|
606
|
+
}
|
|
607
|
+
return "";
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
function safeToolMetadata(output) {
|
|
611
|
+
const attachments = Array.isArray(output && output.attachments) ? output.attachments : [];
|
|
612
|
+
return attachments.length > 0 ? { attachments: attachments.length } : null;
|
|
613
|
+
}
|
|
614
|
+
|
|
445
615
|
function storedMessageText(recorder, messageID) {
|
|
446
616
|
const parts = recorder.messageTextParts.get(messageID);
|
|
447
617
|
if (!parts) {
|