@distlang/opencode-plugin 0.1.14 → 0.2.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/recorder.js +87 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@distlang/opencode-plugin",
3
- "version": "0.1.14",
3
+ "version": "0.2.0",
4
4
  "description": "OpenCode plugin for Distlang AI Debugger",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
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
 
@@ -237,7 +245,13 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
237
245
  started_at: new Date().toISOString(),
238
246
  ended_at: new Date().toISOString(),
239
247
  status: "success",
240
- payload_json: { file_path: filePath, language: inferLanguage(filePath) },
248
+ payload_json: {
249
+ file_path: filePath,
250
+ language: inferLanguage(filePath),
251
+ parent_tool_step_id: recorder.lastToolStepByInteraction.get(interaction.id) || undefined,
252
+ parent_llm_step_id: recorder.lastLLMStepByInteraction.get(interaction.id) || undefined,
253
+ relationship: recorder.lastToolStepByInteraction.get(interaction.id) ? "caused_by_tool" : "caused_by_model",
254
+ },
241
255
  });
242
256
  return { sessionID, filePath, language: inferLanguage(filePath) };
243
257
  }
@@ -259,7 +273,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
259
273
  const interaction = observeUserPrompt(recorder, id, prompt, parseMessageTime(info, Date.now()), mode);
260
274
  addContextContributor(recorder, {
261
275
  category: "user_prompt",
262
- label: `User prompt ${interaction.index}`,
276
+ label: `User prompt from Interaction ${interaction.index}`,
263
277
  source: "opencode.message",
264
278
  text: prompt,
265
279
  });
@@ -328,10 +342,13 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
328
342
  existing.finalized = true;
329
343
  const targetInteraction = findInteraction(recorder, existing.interactionID) || interaction;
330
344
  applyInteractionMode(targetInteraction, mode);
331
- addStep(recorder, targetInteraction, {
345
+ const parentMessageID = parentID || recorder.interactionUserMessages.get(targetInteraction.id) || null;
346
+ const loopIteration = targetInteraction.steps.filter((step) => step.kind === "llm_call").length + 1;
347
+ const previousToolStep = findPreviousToolStep(targetInteraction, id);
348
+ const llmStep = addStep(recorder, targetInteraction, {
332
349
  kind: "llm_call",
333
350
  phase: targetInteraction.mode,
334
- title: summarizePrompt(assistantText, `Assistant response ${targetInteraction.index}`),
351
+ title: summarizePrompt(assistantText, `Model response for Interaction ${targetInteraction.index}`),
335
352
  started_at: new Date(existing.firstSeenAt).toISOString(),
336
353
  ended_at: new Date(now).toISOString(),
337
354
  duration_ms: Math.max(0, now - existing.firstSeenAt),
@@ -346,12 +363,35 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
346
363
  cost_usd: finiteNumber(info && info.cost),
347
364
  first_token_at: existing.firstTokenAt != null ? new Date(existing.firstTokenAt).toISOString() : null,
348
365
  first_token_latency_ms: existing.firstTokenAt != null ? Math.max(0, existing.firstTokenAt - existing.firstSeenAt) : 0,
349
- payload_json: { message_id: id },
366
+ payload_json: {
367
+ message_id: id,
368
+ parent_message_id: parentMessageID,
369
+ loop_iteration: loopIteration,
370
+ caused_by: previousToolStep ? {
371
+ type: "tool_result",
372
+ step_id: previousToolStep.id,
373
+ call_id: previousToolStep.payload_json?.call_id || null,
374
+ } : {
375
+ type: parentMessageID ? "user_prompt" : "session_start",
376
+ message_id: parentMessageID,
377
+ },
378
+ },
350
379
  details: buildLLMStepDetails(recorder, targetInteraction, id),
351
380
  });
381
+ recorder.messageLLMSteps.set(id, llmStep.id);
382
+ recorder.lastLLMStepByInteraction.set(targetInteraction.id, llmStep.id);
383
+ for (const step of targetInteraction.steps) {
384
+ if (step.kind !== "tool_call") continue;
385
+ if (step.payload_json?.message_id !== id) continue;
386
+ step.payload_json = {
387
+ ...step.payload_json,
388
+ parent_llm_step_id: llmStep.id,
389
+ relationship: "requested_by_model",
390
+ };
391
+ }
352
392
  addContextContributor(recorder, {
353
393
  category: "assistant_history",
354
- label: `Assistant response ${targetInteraction.index}`,
394
+ label: `Previous assistant response from Interaction ${targetInteraction.index}`,
355
395
  source: "opencode.message",
356
396
  text: assistantText,
357
397
  });
@@ -383,6 +423,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
383
423
  startedAt: Date.now(),
384
424
  tool: configuredValue(input && input.tool, "unknown"),
385
425
  interactionID,
426
+ messageID,
386
427
  });
387
428
  return { sessionID, callID, tool: configuredValue(input && input.tool, "unknown") };
388
429
  }
@@ -402,7 +443,7 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
402
443
  const mode = inferInteractionMode(input, output);
403
444
  const interaction = mappedInteractionID ? findInteraction(recorder, mappedInteractionID) || ensureInteraction(recorder, Date.now(), "", mode) : ensureInteraction(recorder, Date.now(), "", mode);
404
445
  applyInteractionMode(interaction, mode);
405
- addStep(recorder, interaction, {
446
+ const toolStep = addStep(recorder, interaction, {
406
447
  kind: "tool_call",
407
448
  phase: interaction.mode,
408
449
  title: `Run ${configuredValue(toolState.tool, "unknown")}`,
@@ -411,8 +452,18 @@ export function createRecorder({ project, directory, initialPrompt = "" }) {
411
452
  duration_ms: Math.max(0, Date.now() - toolState.startedAt),
412
453
  status: output && output.error ? "error" : "success",
413
454
  tool_name: configuredValue(toolState.tool, "unknown"),
414
- payload_json: output && output.metadata && typeof output.metadata === "object" ? output.metadata : null,
455
+ payload_json: {
456
+ ...safeObject(output && output.metadata),
457
+ call_id: callID || null,
458
+ message_id: messageID || configuredValue(toolState.messageID, "") || null,
459
+ parent_llm_step_id: recorder.messageLLMSteps.get(messageID) || null,
460
+ relationship: "requested_by_model",
461
+ },
415
462
  });
463
+ if (callID) {
464
+ recorder.toolStepsByCallID.set(callID, toolStep.id);
465
+ }
466
+ recorder.lastToolStepByInteraction.set(interaction.id, toolStep.id);
416
467
  addContextContributor(recorder, {
417
468
  category: "tool_output",
418
469
  label: `Tool ${configuredValue(toolState.tool, "unknown")}`,
@@ -598,7 +649,20 @@ function estimateTokens(text) {
598
649
  }
599
650
 
600
651
  function detailCategoryLabel(category) {
601
- return String(category || "unknown").split("_").map((part) => part ? part[0].toUpperCase() + part.slice(1) : part).join(" ");
652
+ const labels = {
653
+ assistant_history: "Previous assistant messages",
654
+ file_context: "File/context snippets",
655
+ instructions: "Instructions",
656
+ system_event: "System event",
657
+ system_prompt: "System prompt",
658
+ tool_call: "Tool call",
659
+ tool_output: "Tool outputs",
660
+ user_prompt: "User prompts",
661
+ llm_call: "Model call",
662
+ file_edit: "File edit",
663
+ };
664
+ const normalized = String(category || "unknown");
665
+ return labels[normalized] || normalized.split("_").map((part) => part ? part[0].toUpperCase() + part.slice(1) : part).join(" ");
602
666
  }
603
667
 
604
668
  function safeDetailLabel(value, fallback) {
@@ -704,6 +768,20 @@ function buildInteractionPayload(interaction, sessionTitle = "") {
704
768
  };
705
769
  }
706
770
 
771
+ function findPreviousToolStep(interaction, messageID) {
772
+ for (let index = interaction.steps.length - 1; index >= 0; index -= 1) {
773
+ const step = interaction.steps[index];
774
+ if (step.kind !== "tool_call") continue;
775
+ if (messageID && step.payload_json?.message_id && step.payload_json.message_id !== messageID) continue;
776
+ return step;
777
+ }
778
+ return null;
779
+ }
780
+
781
+ function safeObject(value) {
782
+ return value && typeof value === "object" && !Array.isArray(value) ? value : {};
783
+ }
784
+
707
785
  function isGenericInteractionLabel(value) {
708
786
  const label = String(value || "").trim();
709
787
  return /^OpenCode interaction \d+$/i.test(label) || /^Interaction \d+$/i.test(label);