@distlang/opencode-plugin 0.1.8 → 0.1.10

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@distlang/opencode-plugin",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "OpenCode plugin for Distlang AI Debugger",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -18,7 +18,7 @@
18
18
  "check": "npm test",
19
19
  "pack:check": "npm pack --dry-run",
20
20
  "release": "npm test && npm run pack:check",
21
- "publish": "npm publish --access public"
21
+ "publish:public": "npm publish --access public"
22
22
  },
23
23
  "keywords": [
24
24
  "distlang",
package/src/index.js CHANGED
@@ -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;
@@ -293,6 +293,39 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
293
293
  return new Promise((resolve) => setTimeout(resolve, ms));
294
294
  }
295
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);
325
+ }
326
+ return promptParts.join(" ").trim();
327
+ }
328
+
296
329
  function messageShape(event) {
297
330
  const info = event && typeof event === "object" ? event.info ?? event.properties?.info : null;
298
331
  if (!info || typeof info !== "object") {
@@ -362,6 +395,14 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
362
395
  return;
363
396
  }
364
397
 
398
+ if (event.type === "message.part.updated") {
399
+ const observed = recorder.observeMessagePartUpdated(event);
400
+ if (observed) {
401
+ await debugLog("message.part.updated observed", observed);
402
+ }
403
+ return;
404
+ }
405
+
365
406
  if (event.type !== "message.updated") {
366
407
  return;
367
408
  }
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,11 +57,15 @@ 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,
62
64
  stepCounter: 0,
63
65
  assistantStates: new Map(),
66
+ messageRoles: new Map(),
67
+ messageTextParts: new Map(),
68
+ messageInteractions: new Map(),
64
69
  toolStarts: new Map(),
65
70
  seenUserMessages: new Set(),
66
71
  };
@@ -98,7 +103,34 @@ export function createRecorder({ project, directory }) {
98
103
  }
99
104
  return current;
100
105
  }
101
- return createInteraction(recorder, prompt, timestampMs);
106
+ return createInteraction(recorder, configuredValue(prompt, recorder.initialPrompt), timestampMs);
107
+ }
108
+
109
+ function observeUserPrompt(recorder, messageID, prompt, timestampMs) {
110
+ const cleanPrompt = sanitizeDebuggerText(prompt);
111
+ const existingID = recorder.messageInteractions.get(messageID);
112
+ const existing = existingID ? findInteraction(recorder, existingID) : null;
113
+ if (existing) {
114
+ existing.prompt = cleanPrompt;
115
+ existing.summary = summarizePrompt(cleanPrompt, existing.summary || `Interaction ${existing.index}`);
116
+ return existing;
117
+ }
118
+ const current = findInteraction(recorder, recorder.currentInteractionID);
119
+ const currentHasMessage = Array.from(recorder.messageInteractions.values()).includes(current?.id);
120
+ if (current && isGenericInteractionLabel(current.prompt) && !currentHasMessage) {
121
+ current.prompt = cleanPrompt;
122
+ current.summary = summarizePrompt(cleanPrompt, `Interaction ${current.index}`);
123
+ recorder.messageInteractions.set(messageID, current.id);
124
+ recorder.seenUserMessages.add(messageID);
125
+ return current;
126
+ }
127
+ if (recorder.seenUserMessages.has(messageID)) {
128
+ return current || createInteraction(recorder, cleanPrompt, timestampMs);
129
+ }
130
+ recorder.seenUserMessages.add(messageID);
131
+ const interaction = createInteraction(recorder, cleanPrompt, timestampMs);
132
+ recorder.messageInteractions.set(messageID, interaction.id);
133
+ return interaction;
102
134
  }
103
135
 
104
136
  function addStep(recorder, interaction, step) {
@@ -190,13 +222,35 @@ export function createRecorder({ project, directory }) {
190
222
  }
191
223
  const recorder = ensureSession(sessionID, Date.now());
192
224
  const id = messageID(info) || `${sessionID}:user:${Date.now()}`;
193
- const prompt = extractMessageText(info);
194
- if (!prompt || recorder.seenUserMessages.has(id)) {
225
+ recorder.messageRoles.set(id, "user");
226
+ const prompt = extractMessageText(info) || storedMessageText(recorder, id);
227
+ if (!prompt) {
228
+ return { sessionID, messageID: id, prompt: "" };
229
+ }
230
+ const interaction = observeUserPrompt(recorder, id, prompt, parseMessageTime(info, Date.now()));
231
+ return { sessionID, messageID: id, prompt: interaction.prompt };
232
+ }
233
+
234
+ function observeMessagePartUpdated(event) {
235
+ const part = extractMessagePart(event);
236
+ if (!part || part.type !== "text") {
195
237
  return null;
196
238
  }
197
- recorder.seenUserMessages.add(id);
198
- createInteraction(recorder, prompt, parseTimestamp(info && info.time, Date.now()));
199
- return { sessionID, messageID: id, prompt };
239
+ const sessionID = configuredValue(part.sessionID, activeSessionID());
240
+ const messageID = configuredValue(part.messageID, "");
241
+ const partID = configuredValue(part.id, "");
242
+ const text = sanitizeDebuggerText(configuredValue(part.text, configuredValue(event?.properties?.delta, "")));
243
+ if (!sessionID || !messageID || !partID || !text) {
244
+ return null;
245
+ }
246
+ const recorder = ensureSession(sessionID, Date.now());
247
+ storeMessageTextPart(recorder, messageID, partID, text);
248
+ const role = recorder.messageRoles.get(messageID);
249
+ if (role !== "user") {
250
+ return { sessionID, messageID, role: configuredValue(role, "unknown"), text };
251
+ }
252
+ const interaction = observeUserPrompt(recorder, messageID, storedMessageText(recorder, messageID), parsePartTime(part, Date.now()));
253
+ return { sessionID, messageID, role, text: interaction.prompt };
200
254
  }
201
255
 
202
256
  function observeAssistantMessage(event) {
@@ -210,6 +264,7 @@ export function createRecorder({ project, directory }) {
210
264
  return null;
211
265
  }
212
266
  const recorder = ensureSession(sessionID, Date.now());
267
+ recorder.messageRoles.set(id, "assistant");
213
268
  const interaction = ensureInteraction(recorder, Date.now());
214
269
  const now = Date.now();
215
270
  const existing = recorder.assistantStates.get(id) || {
@@ -230,10 +285,11 @@ export function createRecorder({ project, directory }) {
230
285
  if (finalState.isFinal && !existing.finalized) {
231
286
  existing.finalized = true;
232
287
  const targetInteraction = findInteraction(recorder, existing.interactionID) || interaction;
288
+ const assistantText = extractMessageText(info) || storedMessageText(recorder, id);
233
289
  addStep(recorder, targetInteraction, {
234
290
  kind: "llm_call",
235
291
  phase: targetInteraction.mode,
236
- title: summarizePrompt(extractMessageText(info), `Assistant response ${targetInteraction.index}`),
292
+ title: summarizePrompt(assistantText, `Assistant response ${targetInteraction.index}`),
237
293
  started_at: new Date(existing.firstSeenAt).toISOString(),
238
294
  ended_at: new Date(now).toISOString(),
239
295
  duration_ms: Math.max(0, now - existing.firstSeenAt),
@@ -348,6 +404,7 @@ export function createRecorder({ project, directory }) {
348
404
  setSessionTitle,
349
405
  observeFileEdited,
350
406
  observeUserMessage,
407
+ observeMessagePartUpdated,
351
408
  observeAssistantMessage,
352
409
  observeToolBefore,
353
410
  observeToolAfter,
@@ -356,6 +413,20 @@ export function createRecorder({ project, directory }) {
356
413
  };
357
414
  }
358
415
 
416
+ function storeMessageTextPart(recorder, messageID, partID, text) {
417
+ const parts = recorder.messageTextParts.get(messageID) || new Map();
418
+ parts.set(partID, sanitizeDebuggerText(text));
419
+ recorder.messageTextParts.set(messageID, parts);
420
+ }
421
+
422
+ function storedMessageText(recorder, messageID) {
423
+ const parts = recorder.messageTextParts.get(messageID);
424
+ if (!parts) {
425
+ return "";
426
+ }
427
+ return sanitizeDebuggerText(Array.from(parts.values()).filter(Boolean).join("\n"));
428
+ }
429
+
359
430
  function buildSessionPayload(recorder) {
360
431
  const sessionTitle = sanitizeDebuggerText(recorder.title);
361
432
  const interactions = recorder.interactions.map((interaction) => buildInteractionPayload(interaction, sessionTitle));
@@ -483,6 +554,35 @@ function extractMessageInfo(event) {
483
554
  return null;
484
555
  }
485
556
 
557
+ function extractMessagePart(event) {
558
+ if (!event || typeof event !== "object") {
559
+ return null;
560
+ }
561
+ if (event.part && typeof event.part === "object") {
562
+ return event.part;
563
+ }
564
+ if (event.properties && event.properties.part && typeof event.properties.part === "object") {
565
+ return event.properties.part;
566
+ }
567
+ return null;
568
+ }
569
+
570
+ function parseMessageTime(info, fallback) {
571
+ const time = info && info.time;
572
+ if (time && typeof time === "object") {
573
+ return parseTimestamp(time.created, fallback);
574
+ }
575
+ return parseTimestamp(time, fallback);
576
+ }
577
+
578
+ function parsePartTime(part, fallback) {
579
+ const time = part && part.time;
580
+ if (time && typeof time === "object") {
581
+ return parseTimestamp(time.start ?? time.created, fallback);
582
+ }
583
+ return parseTimestamp(time, fallback);
584
+ }
585
+
486
586
  function isAssistantMessage(info) {
487
587
  if (!info || typeof info !== "object") {
488
588
  return false;