@distlang/opencode-plugin 0.1.2 → 0.1.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 +68 -0
- package/package.json +2 -2
- package/src/command.js +73 -0
- package/src/distlang.js +163 -3
- package/src/index.js +130 -9
- package/src/state.js +50 -0
package/README.md
CHANGED
|
@@ -17,6 +17,28 @@ Add the plugin to your global OpenCode config:
|
|
|
17
17
|
|
|
18
18
|
OpenCode installs npm plugins automatically with Bun at startup.
|
|
19
19
|
|
|
20
|
+
## Local use
|
|
21
|
+
|
|
22
|
+
The plugin uses `distlang` only for two things:
|
|
23
|
+
|
|
24
|
+
- `distlang helpers auth status --json`
|
|
25
|
+
- `distlang helpers request ...`
|
|
26
|
+
|
|
27
|
+
If `distlang` is not already available, the plugin can install a managed copy automatically.
|
|
28
|
+
|
|
29
|
+
Managed install location:
|
|
30
|
+
|
|
31
|
+
```text
|
|
32
|
+
~/.cache/distlang/opencode-plugin/bin/distlang
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Resolution order:
|
|
36
|
+
|
|
37
|
+
1. `DISTLANG_BIN`
|
|
38
|
+
2. `distlang` on `PATH`
|
|
39
|
+
3. managed plugin install
|
|
40
|
+
4. auto-install into the managed plugin path
|
|
41
|
+
|
|
20
42
|
## Auth
|
|
21
43
|
|
|
22
44
|
The plugin does not manage tokens directly.
|
|
@@ -28,6 +50,12 @@ distlang helpers auth status
|
|
|
28
50
|
distlang helpers login
|
|
29
51
|
```
|
|
30
52
|
|
|
53
|
+
If the plugin installs a managed copy because `distlang` was missing, you can log in with that binary directly:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
~/.cache/distlang/opencode-plugin/bin/distlang helpers login
|
|
57
|
+
```
|
|
58
|
+
|
|
31
59
|
The plugin uses:
|
|
32
60
|
|
|
33
61
|
- `distlang helpers auth status --json`
|
|
@@ -35,12 +63,52 @@ The plugin uses:
|
|
|
35
63
|
|
|
36
64
|
If you are not logged in, the plugin logs one warning and continues without uploading.
|
|
37
65
|
|
|
66
|
+
## `/distlang` command
|
|
67
|
+
|
|
68
|
+
OpenCode commands are configured separately from plugins, so add a command file to make `/distlang` available in the TUI:
|
|
69
|
+
|
|
70
|
+
`~/.config/opencode/commands/distlang.md`
|
|
71
|
+
|
|
72
|
+
```md
|
|
73
|
+
---
|
|
74
|
+
description: Manage Distlang AI Debugger uploads
|
|
75
|
+
---
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The plugin watches for this command and handles:
|
|
79
|
+
|
|
80
|
+
- `/distlang status`
|
|
81
|
+
- `/distlang start`
|
|
82
|
+
- `/distlang stop`
|
|
83
|
+
|
|
84
|
+
Command results are written to the OpenCode app log, and also to the debug log file when enabled.
|
|
85
|
+
|
|
86
|
+
If `/distlang start` finds that Distlang auth is missing, the plugin starts `distlang helpers login`, which opens the browser login flow.
|
|
87
|
+
|
|
38
88
|
## Debugging
|
|
39
89
|
|
|
40
90
|
```bash
|
|
41
91
|
DISTLANG_OPENCODE_DEBUG=1 opencode
|
|
42
92
|
```
|
|
43
93
|
|
|
94
|
+
Useful overrides:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
DISTLANG_BIN=/path/to/distlang opencode
|
|
98
|
+
DISTLANG_STORE_BASE_URL=https://api-staging.distlang.com opencode
|
|
99
|
+
DISTLANG_AUTH_BASE_URL=https://auth-staging.distlang.com opencode
|
|
100
|
+
DISTLANG_OPENCODE_NO_INSTALL=1 opencode
|
|
101
|
+
DISTLANG_OPENCODE_INSTALL_DIR=/tmp/distlang-plugin-bin opencode
|
|
102
|
+
DISTLANG_OPENCODE_STATE_FILE=/tmp/distlang-plugin-state.json opencode
|
|
103
|
+
DISTLANG_OPENCODE_LOG_FILE=/tmp/distlang-opencode.log opencode
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
To inspect whether uploads are visible after a run:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
distlang helpers request GET /ai-debugger/v1/sessions --json
|
|
110
|
+
```
|
|
111
|
+
|
|
44
112
|
## Captured Model
|
|
45
113
|
|
|
46
114
|
The plugin builds a session-batch payload for Distlang AI Debugger:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@distlang/opencode-plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "OpenCode plugin for Distlang AI Debugger",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"README.md"
|
|
15
15
|
],
|
|
16
16
|
"scripts": {
|
|
17
|
-
"test": "node --check src/index.js && node --check src/recorder.js && node --check src/distlang.js",
|
|
17
|
+
"test": "node --check src/index.js && node --check src/recorder.js && node --check src/distlang.js && node --check src/state.js && node --check src/command.js",
|
|
18
18
|
"check": "npm test",
|
|
19
19
|
"pack:check": "npm pack --dry-run",
|
|
20
20
|
"release": "npm test && npm run pack:check",
|
package/src/command.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
function configuredValue(value, fallback = "") {
|
|
2
|
+
return typeof value === "string" && value.trim() !== "" ? value.trim() : fallback;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function splitCommandText(value) {
|
|
6
|
+
const trimmed = configuredValue(value, "");
|
|
7
|
+
if (!trimmed.startsWith("/")) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
const parts = trimmed.slice(1).trim().split(/\s+/).filter(Boolean);
|
|
11
|
+
if (parts.length === 0) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
name: parts[0].toLowerCase(),
|
|
16
|
+
args: parts.slice(1),
|
|
17
|
+
raw: trimmed,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function normalizeArgs(value) {
|
|
22
|
+
if (Array.isArray(value)) {
|
|
23
|
+
return value.map((entry) => configuredValue(entry, "")).filter(Boolean);
|
|
24
|
+
}
|
|
25
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
26
|
+
return value.trim().split(/\s+/).filter(Boolean);
|
|
27
|
+
}
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function commandLike(entry) {
|
|
32
|
+
if (!entry || typeof entry !== "object") {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const directName = configuredValue(entry.name, configuredValue(entry.command, configuredValue(entry.id, configuredValue(entry.slug, "")))).replace(/^\//, "").toLowerCase();
|
|
36
|
+
const args = normalizeArgs(entry.args ?? entry.arguments ?? entry.argv);
|
|
37
|
+
if (directName) {
|
|
38
|
+
return {
|
|
39
|
+
name: directName,
|
|
40
|
+
args,
|
|
41
|
+
raw: configuredValue(entry.raw, args.length > 0 ? `/${directName} ${args.join(" ")}` : `/${directName}`),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const text = splitCommandText(configuredValue(entry.text, configuredValue(entry.input, configuredValue(entry.value, configuredValue(entry.prompt, "")))));
|
|
45
|
+
if (text) {
|
|
46
|
+
return text;
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function extractDistlangInvocation(input) {
|
|
52
|
+
const candidates = [
|
|
53
|
+
input,
|
|
54
|
+
input?.event,
|
|
55
|
+
input?.properties,
|
|
56
|
+
input?.command,
|
|
57
|
+
input?.info,
|
|
58
|
+
input?.properties,
|
|
59
|
+
input?.payload,
|
|
60
|
+
];
|
|
61
|
+
for (const candidate of candidates) {
|
|
62
|
+
const parsed = commandLike(candidate);
|
|
63
|
+
if (parsed && parsed.name === "distlang") {
|
|
64
|
+
return parsed;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const directText = splitCommandText(configuredValue(input?.commandLine, configuredValue(input?.text, configuredValue(input?.input, configuredValue(input?.raw, "")))));
|
|
69
|
+
if (directText && directText.name === "distlang") {
|
|
70
|
+
return directText;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
package/src/distlang.js
CHANGED
|
@@ -1,29 +1,174 @@
|
|
|
1
1
|
import { execFile } from "node:child_process";
|
|
2
2
|
import { promises as fs } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
4
|
+
import { homedir, tmpdir } from "node:os";
|
|
5
5
|
import { randomUUID } from "node:crypto";
|
|
6
6
|
import { promisify } from "node:util";
|
|
7
7
|
|
|
8
8
|
const execFileAsync = promisify(execFile);
|
|
9
9
|
|
|
10
|
+
let managedInstallPromise = null;
|
|
11
|
+
let resolvedBinaryPromise = null;
|
|
12
|
+
|
|
13
|
+
function configuredValue(value, fallback = "") {
|
|
14
|
+
return typeof value === "string" && value.trim() !== "" ? value.trim() : fallback;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function autoInstallDisabled() {
|
|
18
|
+
const value = configuredValue(process.env.DISTLANG_OPENCODE_NO_INSTALL, "").toLowerCase();
|
|
19
|
+
return value === "1" || value === "true" || value === "yes" || value === "on";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function managedInstallDir() {
|
|
23
|
+
const override = configuredValue(process.env.DISTLANG_OPENCODE_INSTALL_DIR, "");
|
|
24
|
+
if (override) {
|
|
25
|
+
return override;
|
|
26
|
+
}
|
|
27
|
+
return join(homedir(), ".cache", "distlang", "opencode-plugin", "bin");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function managedBinaryPath() {
|
|
31
|
+
return join(managedInstallDir(), process.platform === "win32" ? "distlang.exe" : "distlang");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function pathHasExecutable(filePath) {
|
|
35
|
+
try {
|
|
36
|
+
await fs.access(filePath, fs.constants.X_OK);
|
|
37
|
+
return true;
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function findDistlangOnPath() {
|
|
44
|
+
const pathValue = configuredValue(process.env.PATH, "");
|
|
45
|
+
if (!pathValue) {
|
|
46
|
+
return "";
|
|
47
|
+
}
|
|
48
|
+
const executableNames = process.platform === "win32" ? ["distlang.exe", "distlang.cmd", "distlang.bat"] : ["distlang"];
|
|
49
|
+
for (const part of pathValue.split(process.platform === "win32" ? ";" : ":")) {
|
|
50
|
+
const base = configuredValue(part, "");
|
|
51
|
+
if (!base) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
for (const executableName of executableNames) {
|
|
55
|
+
const candidate = join(base, executableName);
|
|
56
|
+
if (await pathHasExecutable(candidate)) {
|
|
57
|
+
return candidate;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return "";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function verifyDistlangBinary(filePath) {
|
|
65
|
+
const trimmed = configuredValue(filePath, "");
|
|
66
|
+
if (!trimmed) {
|
|
67
|
+
const error = new Error("distlang binary path is empty");
|
|
68
|
+
error.code = "ENOENT";
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
await execFileAsync(trimmed, ["--version"], {
|
|
72
|
+
env: process.env,
|
|
73
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
74
|
+
});
|
|
75
|
+
return trimmed;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function installManagedDistlang() {
|
|
79
|
+
if (managedInstallPromise) {
|
|
80
|
+
return managedInstallPromise;
|
|
81
|
+
}
|
|
82
|
+
managedInstallPromise = (async () => {
|
|
83
|
+
const installDir = managedInstallDir();
|
|
84
|
+
await fs.mkdir(installDir, { recursive: true });
|
|
85
|
+
await execFileAsync("bash", ["-lc", "curl -fsSL https://distlang.com/install-main | bash"], {
|
|
86
|
+
env: {
|
|
87
|
+
...process.env,
|
|
88
|
+
DISTLANG_INSTALL_DIR: installDir,
|
|
89
|
+
},
|
|
90
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
91
|
+
});
|
|
92
|
+
const binaryPath = managedBinaryPath();
|
|
93
|
+
await verifyDistlangBinary(binaryPath);
|
|
94
|
+
return binaryPath;
|
|
95
|
+
})();
|
|
96
|
+
try {
|
|
97
|
+
return await managedInstallPromise;
|
|
98
|
+
} finally {
|
|
99
|
+
managedInstallPromise = null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
10
103
|
function distlangBinary() {
|
|
11
104
|
return process.env.DISTLANG_BIN && process.env.DISTLANG_BIN.trim() !== ""
|
|
12
105
|
? process.env.DISTLANG_BIN.trim()
|
|
13
106
|
: "distlang";
|
|
14
107
|
}
|
|
15
108
|
|
|
109
|
+
export async function resolveDistlangBinary(options = {}) {
|
|
110
|
+
const installIfMissing = options.installIfMissing !== false;
|
|
111
|
+
const explicit = configuredValue(process.env.DISTLANG_BIN, "");
|
|
112
|
+
if (explicit) {
|
|
113
|
+
await verifyDistlangBinary(explicit);
|
|
114
|
+
return { path: explicit, source: "env" };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (resolvedBinaryPromise && installIfMissing) {
|
|
118
|
+
return resolvedBinaryPromise;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const resolver = (async () => {
|
|
122
|
+
const fromPath = await findDistlangOnPath();
|
|
123
|
+
if (fromPath) {
|
|
124
|
+
await verifyDistlangBinary(fromPath);
|
|
125
|
+
return { path: fromPath, source: "path" };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const managedPath = managedBinaryPath();
|
|
129
|
+
if (await pathHasExecutable(managedPath)) {
|
|
130
|
+
await verifyDistlangBinary(managedPath);
|
|
131
|
+
return { path: managedPath, source: "managed" };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!installIfMissing || autoInstallDisabled()) {
|
|
135
|
+
const error = new Error("distlang CLI not found");
|
|
136
|
+
error.code = "ENOENT";
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const installed = await installManagedDistlang();
|
|
141
|
+
return { path: installed, source: "installed" };
|
|
142
|
+
})();
|
|
143
|
+
|
|
144
|
+
if (installIfMissing) {
|
|
145
|
+
resolvedBinaryPromise = resolver;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
return await resolver;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (installIfMissing) {
|
|
151
|
+
resolvedBinaryPromise = null;
|
|
152
|
+
}
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
16
157
|
export async function runDistlang(args, options = {}) {
|
|
17
|
-
|
|
158
|
+
const { installIfMissing, ...execOptions } = options;
|
|
159
|
+
const resolved = await resolveDistlangBinary({ installIfMissing: installIfMissing !== false });
|
|
160
|
+
return execFileAsync(resolved.path, args, {
|
|
18
161
|
env: process.env,
|
|
19
162
|
maxBuffer: 16 * 1024 * 1024,
|
|
20
|
-
...
|
|
163
|
+
...execOptions,
|
|
21
164
|
});
|
|
22
165
|
}
|
|
23
166
|
|
|
24
167
|
export function distlangCommandInfo() {
|
|
25
168
|
return {
|
|
26
169
|
bin: distlangBinary(),
|
|
170
|
+
managed_bin: managedBinaryPath(),
|
|
171
|
+
auto_install_disabled: autoInstallDisabled(),
|
|
27
172
|
};
|
|
28
173
|
}
|
|
29
174
|
|
|
@@ -32,6 +177,10 @@ export async function getAuthStatus() {
|
|
|
32
177
|
return JSON.parse(stdout || "{}");
|
|
33
178
|
}
|
|
34
179
|
|
|
180
|
+
export async function loginWithDistlang() {
|
|
181
|
+
return runDistlang(["helpers", "login"]);
|
|
182
|
+
}
|
|
183
|
+
|
|
35
184
|
export async function uploadAIDebuggerPayload(payload) {
|
|
36
185
|
const tempFile = join(tmpdir(), `distlang-ai-debugger-${randomUUID()}.json`);
|
|
37
186
|
await fs.writeFile(tempFile, `${JSON.stringify(payload, null, 2)}\n`, "utf8");
|
|
@@ -50,3 +199,14 @@ export async function uploadAIDebuggerPayload(payload) {
|
|
|
50
199
|
await fs.unlink(tempFile).catch(() => {});
|
|
51
200
|
}
|
|
52
201
|
}
|
|
202
|
+
|
|
203
|
+
export async function fetchAIDebuggerSessions() {
|
|
204
|
+
const { stdout } = await runDistlang([
|
|
205
|
+
"helpers",
|
|
206
|
+
"request",
|
|
207
|
+
"GET",
|
|
208
|
+
"/ai-debugger/v1/sessions?source=opencode&limit=5",
|
|
209
|
+
"--json",
|
|
210
|
+
]);
|
|
211
|
+
return JSON.parse(stdout || "{}");
|
|
212
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { createRecorder } from "./recorder.js";
|
|
2
|
-
import {
|
|
2
|
+
import { extractDistlangInvocation } from "./command.js";
|
|
3
|
+
import { distlangCommandInfo, fetchAIDebuggerSessions, getAuthStatus, loginWithDistlang, resolveDistlangBinary, uploadAIDebuggerPayload } from "./distlang.js";
|
|
4
|
+
import { pluginStatePath, readPluginState, writePluginState } from "./state.js";
|
|
3
5
|
import { appendFile } from "node:fs/promises";
|
|
4
6
|
|
|
5
7
|
function configuredValue(value, fallback = "") {
|
|
@@ -19,10 +21,9 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
19
21
|
const debug = debugEnabled();
|
|
20
22
|
const recorder = createRecorder({ project, directory });
|
|
21
23
|
let loggedInit = false;
|
|
22
|
-
let authChecked = false;
|
|
23
|
-
let authAvailable = false;
|
|
24
24
|
let authWarningLogged = false;
|
|
25
25
|
let distlangMissingLogged = false;
|
|
26
|
+
let commandHandledAt = 0;
|
|
26
27
|
|
|
27
28
|
async function log(level, message, extra = undefined) {
|
|
28
29
|
const line = JSON.stringify({
|
|
@@ -63,22 +64,116 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
63
64
|
loggedInit = true;
|
|
64
65
|
await log(debug ? "debug" : "info", "Distlang OpenCode AI Debugger plugin initialized", {
|
|
65
66
|
debug,
|
|
67
|
+
statePath: pluginStatePath(),
|
|
66
68
|
distlang: distlangCommandInfo(),
|
|
67
69
|
});
|
|
68
70
|
}
|
|
69
71
|
|
|
72
|
+
async function uploadEnabled() {
|
|
73
|
+
const state = await readPluginState();
|
|
74
|
+
return state.enabled !== false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function maybeLogCommandResult(level, message, extra = undefined) {
|
|
78
|
+
await log(level, message, extra);
|
|
79
|
+
if (client?.tui && typeof client.tui.showToast === "function") {
|
|
80
|
+
const variant = level === "error" ? "error" : level === "warn" ? "warning" : level === "info" ? "success" : "info";
|
|
81
|
+
const detail = typeof extra?.error === "string" && extra.error.trim() !== "" ? extra.error.trim() : undefined;
|
|
82
|
+
await client.tui.showToast({
|
|
83
|
+
body: {
|
|
84
|
+
title: "Distlang",
|
|
85
|
+
message: detail ? `${message}: ${detail}` : message,
|
|
86
|
+
variant,
|
|
87
|
+
duration: 5000,
|
|
88
|
+
},
|
|
89
|
+
}).catch(() => {});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function handleDistlangCommand(invocation, source) {
|
|
94
|
+
const now = Date.now();
|
|
95
|
+
if (now - commandHandledAt < 250) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
commandHandledAt = now;
|
|
99
|
+
const action = configuredValue(invocation.args[0], "status").toLowerCase();
|
|
100
|
+
if (action === "start") {
|
|
101
|
+
const state = await writePluginState(true);
|
|
102
|
+
authWarningLogged = false;
|
|
103
|
+
let resolved = null;
|
|
104
|
+
let auth = null;
|
|
105
|
+
try {
|
|
106
|
+
resolved = await resolveDistlangBinary({ installIfMissing: true });
|
|
107
|
+
} catch (error) {
|
|
108
|
+
await maybeLogCommandResult("warn", "Distlang uploads enabled, but distlang install/resolve failed", { source, action, error: String(error) });
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
auth = await getAuthStatus();
|
|
113
|
+
if (!auth || auth.ok !== true || auth.logged_in !== true) {
|
|
114
|
+
await maybeLogCommandResult("info", "Opening browser for Distlang login", { source, action, state, distlang: resolved });
|
|
115
|
+
await loginWithDistlang();
|
|
116
|
+
auth = await getAuthStatus();
|
|
117
|
+
}
|
|
118
|
+
} catch (error) {
|
|
119
|
+
await maybeLogCommandResult("warn", "Distlang uploads enabled, but auth check failed", { source, action, state, distlang: resolved, error: String(error) });
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
await maybeLogCommandResult("info", "Distlang AI Debugger uploads enabled", {
|
|
123
|
+
source,
|
|
124
|
+
action,
|
|
125
|
+
state,
|
|
126
|
+
distlang: resolved,
|
|
127
|
+
auth,
|
|
128
|
+
});
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (action === "stop") {
|
|
133
|
+
const state = await writePluginState(false);
|
|
134
|
+
await maybeLogCommandResult("info", "Distlang AI Debugger uploads disabled", { source, action, state });
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const state = await readPluginState();
|
|
139
|
+
let resolved = null;
|
|
140
|
+
let auth = null;
|
|
141
|
+
let sessions = null;
|
|
142
|
+
let resolutionError = null;
|
|
143
|
+
try {
|
|
144
|
+
resolved = await resolveDistlangBinary({ installIfMissing: true });
|
|
145
|
+
auth = await getAuthStatus();
|
|
146
|
+
sessions = await fetchAIDebuggerSessions();
|
|
147
|
+
} catch (error) {
|
|
148
|
+
resolutionError = String(error);
|
|
149
|
+
}
|
|
150
|
+
await maybeLogCommandResult("info", "Distlang AI Debugger status", {
|
|
151
|
+
source,
|
|
152
|
+
action,
|
|
153
|
+
state,
|
|
154
|
+
distlang: resolved,
|
|
155
|
+
auth,
|
|
156
|
+
sessions,
|
|
157
|
+
error: resolutionError,
|
|
158
|
+
command_hint: "/distlang status | /distlang start | /distlang stop",
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
70
162
|
async function ensureAuthStatus() {
|
|
71
|
-
if (
|
|
72
|
-
|
|
163
|
+
if (!(await uploadEnabled())) {
|
|
164
|
+
await debugLog("Distlang AI Debugger uploads are disabled", { state: await readPluginState() });
|
|
165
|
+
return false;
|
|
73
166
|
}
|
|
74
|
-
authChecked = true;
|
|
75
167
|
try {
|
|
168
|
+
const resolved = await resolveDistlangBinary({ installIfMissing: true });
|
|
76
169
|
const payload = await getAuthStatus();
|
|
77
|
-
authAvailable = payload && payload.ok === true && payload.logged_in === true;
|
|
78
|
-
await debugLog("Distlang auth status resolved", { authAvailable, payload });
|
|
170
|
+
const authAvailable = payload && payload.ok === true && payload.logged_in === true;
|
|
171
|
+
await debugLog("Distlang auth status resolved", { authAvailable, payload, distlang: resolved });
|
|
79
172
|
if (!authAvailable && !authWarningLogged) {
|
|
80
173
|
authWarningLogged = true;
|
|
81
174
|
await log("warn", "Distlang AI Debugger upload disabled: run `distlang helpers login` first", { auth: payload });
|
|
175
|
+
} else if (authAvailable) {
|
|
176
|
+
authWarningLogged = false;
|
|
82
177
|
}
|
|
83
178
|
return authAvailable;
|
|
84
179
|
} catch (error) {
|
|
@@ -91,7 +186,6 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
91
186
|
authWarningLogged = true;
|
|
92
187
|
await log("warn", "Distlang AI Debugger auth check failed; upload disabled", { error: String(error) });
|
|
93
188
|
}
|
|
94
|
-
authAvailable = false;
|
|
95
189
|
return false;
|
|
96
190
|
}
|
|
97
191
|
}
|
|
@@ -175,6 +269,14 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
175
269
|
return;
|
|
176
270
|
}
|
|
177
271
|
|
|
272
|
+
if (event.type === "command.executed") {
|
|
273
|
+
const invocation = extractDistlangInvocation(event);
|
|
274
|
+
if (invocation) {
|
|
275
|
+
await handleDistlangCommand(invocation, "command.executed");
|
|
276
|
+
}
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
178
280
|
|
|
179
281
|
if (event.type === "session.created") {
|
|
180
282
|
const observed = recorder.observeSessionCreated(event);
|
|
@@ -224,6 +326,25 @@ export const DistlangAIDebugger = async ({ project, directory, client }) => {
|
|
|
224
326
|
}
|
|
225
327
|
},
|
|
226
328
|
|
|
329
|
+
"tui.command.execute": async (input) => {
|
|
330
|
+
await logInit();
|
|
331
|
+
const invocation = extractDistlangInvocation(input);
|
|
332
|
+
if (!invocation) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
await handleDistlangCommand(invocation, "tui.command.execute");
|
|
336
|
+
},
|
|
337
|
+
|
|
338
|
+
"command.execute.before": async (input, output) => {
|
|
339
|
+
await logInit();
|
|
340
|
+
const invocation = extractDistlangInvocation(input);
|
|
341
|
+
if (!invocation) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
await handleDistlangCommand(invocation, "command.execute.before");
|
|
345
|
+
output.parts = [];
|
|
346
|
+
},
|
|
347
|
+
|
|
227
348
|
"tool.execute.before": async (input) => {
|
|
228
349
|
await logInit();
|
|
229
350
|
const observed = recorder.observeToolBefore(input);
|
package/src/state.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { promises as fs } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
|
|
5
|
+
function configuredValue(value, fallback = "") {
|
|
6
|
+
return typeof value === "string" && value.trim() !== "" ? value.trim() : fallback;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function defaultState() {
|
|
10
|
+
return {
|
|
11
|
+
enabled: true,
|
|
12
|
+
updated_at: null,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function pluginStatePath() {
|
|
17
|
+
const override = configuredValue(process.env.DISTLANG_OPENCODE_STATE_FILE, "");
|
|
18
|
+
if (override) {
|
|
19
|
+
return override;
|
|
20
|
+
}
|
|
21
|
+
return join(homedir(), ".config", "opencode", "distlang-plugin.json");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function readPluginState() {
|
|
25
|
+
const filePath = pluginStatePath();
|
|
26
|
+
try {
|
|
27
|
+
const raw = await fs.readFile(filePath, "utf8");
|
|
28
|
+
const parsed = JSON.parse(raw);
|
|
29
|
+
return {
|
|
30
|
+
enabled: parsed && parsed.enabled === false ? false : true,
|
|
31
|
+
updated_at: typeof parsed?.updated_at === "string" && parsed.updated_at.trim() !== "" ? parsed.updated_at : null,
|
|
32
|
+
};
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (error && typeof error === "object" && error.code === "ENOENT") {
|
|
35
|
+
return defaultState();
|
|
36
|
+
}
|
|
37
|
+
return defaultState();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function writePluginState(enabled) {
|
|
42
|
+
const filePath = pluginStatePath();
|
|
43
|
+
await fs.mkdir(dirname(filePath), { recursive: true });
|
|
44
|
+
const payload = {
|
|
45
|
+
enabled: enabled !== false,
|
|
46
|
+
updated_at: new Date().toISOString(),
|
|
47
|
+
};
|
|
48
|
+
await fs.writeFile(filePath, `${JSON.stringify(payload, null, 2)}\n`, "utf8");
|
|
49
|
+
return payload;
|
|
50
|
+
}
|