@distlang/opencode-plugin 0.2.2 → 0.2.4

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/README.md CHANGED
@@ -96,6 +96,7 @@ The plugin watches for these commands:
96
96
  - `/distlang-start`
97
97
  - `/distlang-stop`
98
98
  - `/distlang-status`
99
+ - `/distlang-view [session-id]`
99
100
 
100
101
  The legacy compact command also remains supported:
101
102
 
@@ -123,6 +124,7 @@ Commands:
123
124
  - `/distlang-status`: show whether uploads are enabled, whether auth is available, and whether recent Agent Debugger sessions are visible
124
125
  - `/distlang-start`: sign in if needed and enable Agent Debugger uploads
125
126
  - `/distlang-stop`: disable Agent Debugger uploads and sign out of Distlang
127
+ - `/distlang-view [session-id]`: open the latest uploaded OpenCode Agent Debugger session in the browser, or the provided session id
126
128
 
127
129
  Legacy aliases remain available: `/distlang status`, `/distlang start`, `/distlang stop`, `/distlang login`, and `/distlang logout`.
128
130
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@distlang/opencode-plugin",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "OpenCode plugin for Distlang Agent Debugger",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/command.js CHANGED
@@ -78,7 +78,7 @@ function distlangInvocation(parsed) {
78
78
  return parsed;
79
79
  }
80
80
  const action = parsed.name.startsWith("distlang-") ? parsed.name.slice("distlang-".length) : "";
81
- if (action === "start" || action === "stop" || action === "status") {
81
+ if (action === "start" || action === "stop" || action === "status" || action === "view") {
82
82
  return {
83
83
  ...parsed,
84
84
  name: "distlang",
package/src/index.js CHANGED
@@ -3,6 +3,7 @@ import { extractDistlangInvocation } from "./command.js";
3
3
  import { distlangCommandInfo, fetchAgentDebuggerSessions, getAuthStatus, loginWithDistlang, logoutWithDistlang, resolveDistlangBinary, uploadAgentDebuggerPayload } from "./distlang.js";
4
4
  import { pluginStatePath, readPluginState, writePluginState } from "./state.js";
5
5
  import { appendFile } from "node:fs/promises";
6
+ import { spawn } from "node:child_process";
6
7
 
7
8
  function configuredValue(value, fallback = "") {
8
9
  return typeof value === "string" && value.trim() !== "" ? value.trim() : fallback;
@@ -90,6 +91,76 @@ export const DistlangAgentDebugger = async ({ project, directory, client }) => {
90
91
  }
91
92
  }
92
93
 
94
+ function dashboardBaseUrl() {
95
+ return configuredValue(process.env.DISTLANG_DASHBOARD_URL, "https://dash.distlang.com").replace(/\/+$/, "");
96
+ }
97
+
98
+ function sessionUrl(sessionID) {
99
+ const id = configuredValue(sessionID, "");
100
+ return id ? `${dashboardBaseUrl()}/agent-debugger/sessions/${encodeURIComponent(id)}` : "";
101
+ }
102
+
103
+ function openerCommand() {
104
+ if (process.platform === "darwin") return { command: "open", args: [] };
105
+ if (process.platform === "win32") return { command: "cmd", args: ["/c", "start", ""] };
106
+ return { command: "xdg-open", args: [] };
107
+ }
108
+
109
+ function openUrl(url) {
110
+ const { command, args } = openerCommand();
111
+ return new Promise((resolve) => {
112
+ try {
113
+ const child = spawn(command, [...args, url], { detached: true, stdio: "ignore" });
114
+ child.on("error", () => resolve(false));
115
+ child.unref();
116
+ resolve(true);
117
+ } catch {
118
+ resolve(false);
119
+ }
120
+ });
121
+ }
122
+
123
+ function commandArgument(invocation, action) {
124
+ const args = Array.isArray(invocation?.args) ? invocation.args : [];
125
+ if (configuredValue(invocation?.action, "")) {
126
+ return configuredValue(args[0], "");
127
+ }
128
+ return configuredValue(args[0], "").toLowerCase() === action ? configuredValue(args[1], "") : configuredValue(args[0], "");
129
+ }
130
+
131
+ function sessionsFromResponse(response) {
132
+ const candidates = [
133
+ response?.body?.sessions,
134
+ response?.sessions,
135
+ response?.body?.data?.sessions,
136
+ response?.data?.sessions,
137
+ response?.body?.items,
138
+ response?.items,
139
+ ];
140
+ for (const candidate of candidates) {
141
+ if (Array.isArray(candidate)) return candidate;
142
+ }
143
+ return [];
144
+ }
145
+
146
+ function firstSessionID(response) {
147
+ for (const session of sessionsFromResponse(response)) {
148
+ const id = configuredValue(session?.id, configuredValue(session?.session_id, ""));
149
+ if (id) return id;
150
+ }
151
+ return "";
152
+ }
153
+
154
+ async function ensureCommandAuth(source, action, state = null, resolved = null) {
155
+ let auth = await getAuthStatus();
156
+ if (!auth || auth.ok !== true || auth.logged_in !== true) {
157
+ await maybeLogCommandResult("info", "Opening browser for Distlang login", { source, action, state, distlang: resolved });
158
+ await loginWithDistlang();
159
+ auth = await getAuthStatus();
160
+ }
161
+ return auth;
162
+ }
163
+
93
164
  async function handleDistlangCommand(invocation, source) {
94
165
  const now = Date.now();
95
166
  if (now - commandHandledAt < 250) {
@@ -98,11 +169,11 @@ export const DistlangAgentDebugger = async ({ project, directory, client }) => {
98
169
  commandHandledAt = now;
99
170
  const requestedAction = configuredValue(invocation.action, configuredValue(invocation.args[0], "status")).toLowerCase();
100
171
  const action = requestedAction === "login" ? "start" : requestedAction === "logout" ? "stop" : requestedAction;
101
- if (!["status", "start", "stop"].includes(action)) {
102
- await maybeLogCommandResult("warn", "Unknown Distlang command. Use /distlang-status, /distlang-start, or /distlang-stop", {
172
+ if (!["status", "start", "stop", "view"].includes(action)) {
173
+ await maybeLogCommandResult("warn", "Unknown Distlang command. Use /distlang-status, /distlang-start, /distlang-stop, or /distlang-view", {
103
174
  source,
104
175
  action: requestedAction,
105
- command_hint: "/distlang-status | /distlang-start | /distlang-stop",
176
+ command_hint: "/distlang-status | /distlang-start | /distlang-stop | /distlang-view",
106
177
  });
107
178
  return;
108
179
  }
@@ -118,12 +189,7 @@ export const DistlangAgentDebugger = async ({ project, directory, client }) => {
118
189
  return;
119
190
  }
120
191
  try {
121
- auth = await getAuthStatus();
122
- if (!auth || auth.ok !== true || auth.logged_in !== true) {
123
- await maybeLogCommandResult("info", "Opening browser for Distlang login", { source, action, state, distlang: resolved });
124
- await loginWithDistlang();
125
- auth = await getAuthStatus();
126
- }
192
+ auth = await ensureCommandAuth(source, action, state, resolved);
127
193
  } catch (error) {
128
194
  await maybeLogCommandResult("warn", "Distlang uploads enabled, but auth check failed", { source, action, state, distlang: resolved, error: String(error) });
129
195
  return;
@@ -151,6 +217,48 @@ export const DistlangAgentDebugger = async ({ project, directory, client }) => {
151
217
  return;
152
218
  }
153
219
 
220
+ if (action === "view") {
221
+ const state = await readPluginState();
222
+ let resolved = null;
223
+ let auth = null;
224
+ let sessions = null;
225
+ try {
226
+ resolved = await resolveDistlangBinary({ installIfMissing: true });
227
+ auth = await ensureCommandAuth(source, action, state, resolved);
228
+ sessions = await fetchAgentDebuggerSessions();
229
+ } catch (error) {
230
+ await maybeLogCommandResult("warn", "Unable to open Distlang Agent Debugger session", { source, action, state, distlang: resolved, error: String(error) });
231
+ return;
232
+ }
233
+ const sessionID = commandArgument(invocation, action) || firstSessionID(sessions);
234
+ const url = sessionUrl(sessionID);
235
+ if (!url) {
236
+ await maybeLogCommandResult("warn", "No uploaded OpenCode Agent Debugger session found yet", {
237
+ source,
238
+ action,
239
+ state,
240
+ distlang: resolved,
241
+ auth,
242
+ sessions,
243
+ command_hint: "Run a prompt first, wait for upload, then run /distlang-view again",
244
+ });
245
+ return;
246
+ }
247
+ const opened = await openUrl(url);
248
+ await maybeLogCommandResult("info", `Open Distlang Agent Debugger session: ${url}`, {
249
+ source,
250
+ action,
251
+ state,
252
+ distlang: resolved,
253
+ auth,
254
+ session_id: sessionID,
255
+ url,
256
+ opened,
257
+ sessions,
258
+ });
259
+ return;
260
+ }
261
+
154
262
  const state = await readPluginState();
155
263
  let resolved = null;
156
264
  let auth = null;
@@ -171,7 +279,7 @@ export const DistlangAgentDebugger = async ({ project, directory, client }) => {
171
279
  auth,
172
280
  sessions,
173
281
  error: resolutionError,
174
- command_hint: "/distlang-status | /distlang-start | /distlang-stop",
282
+ command_hint: "/distlang-status | /distlang-start | /distlang-stop | /distlang-view",
175
283
  });
176
284
  }
177
285
 
package/src/recorder.js CHANGED
@@ -947,8 +947,13 @@ function assistantFinalState(info) {
947
947
 
948
948
  function tokensFromMessage(info) {
949
949
  const sources = tokenSources(info);
950
- const cacheValue = firstNumber(sources, ["cache", "cached", "cached_tokens", "cachedTokens", "cacheReadInputTokens", "cachedInput", "cachedInputTokens"]);
951
- const input = firstNumber(sources, ["input", "input_tokens", "inputTokens", "prompt", "prompt_tokens", "promptTokens"]);
950
+ const nestedCacheRead = firstNestedNumber(sources, ["cache", "read"]);
951
+ const nestedCacheWrite = firstNestedNumber(sources, ["cache", "write"]);
952
+ const flatCacheRead = firstNumber(sources, ["cached", "cached_tokens", "cachedTokens", "cacheReadInputTokens", "cachedInput", "cachedInputTokens"]);
953
+ const cacheRead = nestedCacheRead || flatCacheRead;
954
+ const cacheWrite = nestedCacheWrite;
955
+ const rawInput = firstNumber(sources, ["input", "input_tokens", "inputTokens", "prompt", "prompt_tokens", "promptTokens"]);
956
+ const input = rawInput + (nestedCacheRead > 0 || nestedCacheWrite > 0 ? cacheRead + cacheWrite : 0);
952
957
  const output = firstNumber(sources, ["output", "output_tokens", "outputTokens", "completion", "completion_tokens", "completionTokens"]);
953
958
  const reasoning = firstNumber(sources, ["reasoning", "reasoning_tokens", "reasoningTokens"]);
954
959
  const explicitContext = firstNumber(sources, ["context", "context_tokens", "contextTokens", "context_size", "contextSize", "context_size_tokens", "contextSizeTokens"]);
@@ -956,8 +961,8 @@ function tokensFromMessage(info) {
956
961
  input,
957
962
  output,
958
963
  reasoning,
959
- cached: cacheValue,
960
- context: explicitContext > 0 ? explicitContext : input + cacheValue + reasoning,
964
+ cached: cacheRead,
965
+ context: explicitContext > 0 ? explicitContext : rawInput + cacheRead + cacheWrite + reasoning,
961
966
  };
962
967
  }
963
968
 
@@ -995,6 +1000,23 @@ function firstNumber(sources, keys) {
995
1000
  return 0;
996
1001
  }
997
1002
 
1003
+ function firstNestedNumber(sources, path) {
1004
+ for (const source of sources) {
1005
+ let value = source;
1006
+ for (const key of path) {
1007
+ value = value && typeof value === "object" ? value[key] : undefined;
1008
+ if (typeof value === "undefined" || value === null) {
1009
+ break;
1010
+ }
1011
+ }
1012
+ const parsed = finiteNumber(value);
1013
+ if (parsed > 0) {
1014
+ return parsed;
1015
+ }
1016
+ }
1017
+ return 0;
1018
+ }
1019
+
998
1020
  function updateSessionTitle(recorder, info) {
999
1021
  const title = extractSessionTitle(info);
1000
1022
  if (title) {