@nandoblanco/opencode-local-tokens 0.1.1 → 0.1.2
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/CHANGELOG.md +5 -0
- package/README.md +45 -2
- package/cli.js +286 -6
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.2 (2026-11-26)
|
|
4
|
+
|
|
5
|
+
- Added `install` (`init` / `setup`) CLI subcommand (`installPlugin` in `cli.js`, tested in `test/install.test.ts`) writing a V2 entry to `opencode.jsonc` only, without requiring TypeScript execution.
|
|
6
|
+
- Aligned `package.json` `"author"` to `"nandoblanco"` and standardized `README.md` Installation with all 4 install options (`opencode plugin add`, manual `opencode.jsonc`, `npx ... install`, and local directory) matching the other public OpenCode plugins.
|
|
7
|
+
|
|
3
8
|
## 0.1.1 (2026-11-26)
|
|
4
9
|
|
|
5
10
|
- Removed eager startup `prewarmTokensCache()` timers from `src/server.ts` and `src/tui.ts` so OpenCode server and TUI startup are not blocked by synchronous SQLite scans on large `opencode.db` files; `/tokens` still prewarms and caches on demand when opened.
|
package/README.md
CHANGED
|
@@ -23,13 +23,56 @@ Tokens reads local session records and transcripts to make that audit easier. Us
|
|
|
23
23
|
|
|
24
24
|
## Installation
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
### Option 1: OpenCode Plugin CLI (Recommended)
|
|
27
|
+
|
|
28
|
+
Install with OpenCode's native plugin CLI. OpenCode downloads the package from npm (or GitHub) into its own cache automatically and adds one entry to `~/.config/opencode/opencode.jsonc` that loads the server tools, `/tokens` and `/handoff` TUI commands, and Desktop panel together:
|
|
27
29
|
|
|
28
30
|
```sh
|
|
29
31
|
opencode plugin add @nandoblanco/opencode-local-tokens
|
|
32
|
+
# Auto-update on startup and every 24 hours:
|
|
33
|
+
opencode plugin add @nandoblanco/opencode-local-tokens@latest
|
|
34
|
+
# Or install directly from GitHub:
|
|
35
|
+
opencode plugin add github:nandoblanco/opencode-local-tokens
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Option 2: Manual Configuration
|
|
39
|
+
|
|
40
|
+
Add `@nandoblanco/opencode-local-tokens` to `"plugins"` in the global `~/.config/opencode/opencode.jsonc` (or project `./opencode.jsonc` / `./.opencode/opencode.jsonc`). OpenCode fetches the npm package automatically and loads the server, TUI, and Desktop parts from this single entry (no manual `npm install` or separate `cli.json` entry is required):
|
|
41
|
+
|
|
42
|
+
```jsonc
|
|
43
|
+
{
|
|
44
|
+
"plugins": [
|
|
45
|
+
{
|
|
46
|
+
"package": "@nandoblanco/opencode-local-tokens",
|
|
47
|
+
"options": {
|
|
48
|
+
"cacheAlerts": false
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Option 3: Built-in CLI Helper (`npx`)
|
|
56
|
+
|
|
57
|
+
Run the package CLI once via `npx` to register the plugin in `~/.config/opencode/opencode.jsonc`:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
npx @nandoblanco/opencode-local-tokens install
|
|
61
|
+
# Or opt into prompt-cache alerts during setup:
|
|
62
|
+
npx @nandoblanco/opencode-local-tokens install --cache-alerts
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Option 4: From a Local Directory
|
|
66
|
+
|
|
67
|
+
Clone this repository, run `npm ci` and `npm run build:gui` after GUI source changes, then reference the package directory in `opencode.jsonc` (or place the directory inside `~/.config/opencode/plugins/` or `.opencode/plugins/`):
|
|
68
|
+
|
|
69
|
+
```json
|
|
70
|
+
{
|
|
71
|
+
"plugins": ["file:///path/to/opencode-local-tokens"]
|
|
72
|
+
}
|
|
30
73
|
```
|
|
31
74
|
|
|
32
|
-
|
|
75
|
+
Entrypoints are `./server`, `./tui`, and `./gui-experimental`. Package publication and live host loading are separate checks. Source execution requires a Node version with TypeScript execution support or equivalent tooling; the declared Node 22.5 minimum alone is insufficient for running these `.ts` entrypoints directly (the `install` CLI subcommand runs in plain Node without loading TypeScript entrypoints).
|
|
33
76
|
|
|
34
77
|
## Using Tokens
|
|
35
78
|
|
package/cli.js
CHANGED
|
@@ -1,9 +1,289 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
3
|
+
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
const cliName = "open" + "code";
|
|
10
|
+
|
|
11
|
+
export function resolveConfigDir(explicitDir) {
|
|
12
|
+
if (explicitDir) {
|
|
13
|
+
if (explicitDir === "~") return os.homedir();
|
|
14
|
+
if (explicitDir.startsWith("~/") || explicitDir.startsWith("~\\")) {
|
|
15
|
+
return path.join(os.homedir(), explicitDir.slice(2));
|
|
16
|
+
}
|
|
17
|
+
return path.resolve(explicitDir);
|
|
18
|
+
}
|
|
19
|
+
if (process.env.OPENCODE_CONFIG_DIR) return path.resolve(process.env.OPENCODE_CONFIG_DIR);
|
|
20
|
+
if (process.env.XDG_CONFIG_HOME) return path.join(process.env.XDG_CONFIG_HOME, cliName);
|
|
21
|
+
return path.join(os.homedir(), ".config", cliName);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function findPluginsArraySpan(raw) {
|
|
25
|
+
const pluginsMatch = /"plugins"\s*:\s*\[/.exec(raw);
|
|
26
|
+
if (!pluginsMatch) return null;
|
|
27
|
+
const openBracketIdx = pluginsMatch.index + pluginsMatch[0].length - 1;
|
|
28
|
+
let depth = 0;
|
|
29
|
+
let inString = false;
|
|
30
|
+
let escape = false;
|
|
31
|
+
let inLineComment = false;
|
|
32
|
+
let inBlockComment = false;
|
|
33
|
+
|
|
34
|
+
for (let i = openBracketIdx; i < raw.length; i++) {
|
|
35
|
+
const ch = raw[i];
|
|
36
|
+
const next = raw[i + 1];
|
|
37
|
+
|
|
38
|
+
if (inLineComment) {
|
|
39
|
+
if (ch === "\n") inLineComment = false;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (inBlockComment) {
|
|
43
|
+
if (ch === "*" && next === "/") {
|
|
44
|
+
inBlockComment = false;
|
|
45
|
+
i++;
|
|
46
|
+
}
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (escape) {
|
|
50
|
+
escape = false;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (inString) {
|
|
54
|
+
if (ch === "\\") escape = true;
|
|
55
|
+
else if (ch === '"') inString = false;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (ch === "/" && next === "/") {
|
|
59
|
+
inLineComment = true;
|
|
60
|
+
i++;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (ch === "/" && next === "*") {
|
|
64
|
+
inBlockComment = true;
|
|
65
|
+
i++;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (ch === '"') {
|
|
69
|
+
inString = true;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (ch === "[") depth++;
|
|
73
|
+
else if (ch === "]") {
|
|
74
|
+
depth--;
|
|
75
|
+
if (depth === 0) {
|
|
76
|
+
return { openBracketIdx, closeBracketIdx: i };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function findLastCodeCharIndex(str) {
|
|
84
|
+
let inString = false;
|
|
85
|
+
let escape = false;
|
|
86
|
+
let inLineComment = false;
|
|
87
|
+
let inBlockComment = false;
|
|
88
|
+
let lastCodeIdx = -1;
|
|
89
|
+
|
|
90
|
+
for (let i = 0; i < str.length; i++) {
|
|
91
|
+
const ch = str[i];
|
|
92
|
+
const next = str[i + 1];
|
|
93
|
+
|
|
94
|
+
if (inLineComment) {
|
|
95
|
+
if (ch === "\n") inLineComment = false;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (inBlockComment) {
|
|
99
|
+
if (ch === "*" && next === "/") {
|
|
100
|
+
inBlockComment = false;
|
|
101
|
+
i++;
|
|
102
|
+
}
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (escape) {
|
|
106
|
+
escape = false;
|
|
107
|
+
lastCodeIdx = i;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (inString) {
|
|
111
|
+
if (ch === "\\") escape = true;
|
|
112
|
+
else if (ch === '"') inString = false;
|
|
113
|
+
lastCodeIdx = i;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (ch === "/" && next === "/") {
|
|
117
|
+
inLineComment = true;
|
|
118
|
+
i++;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (ch === "/" && next === "*") {
|
|
122
|
+
inBlockComment = true;
|
|
123
|
+
i++;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
if (ch === '"') {
|
|
127
|
+
inString = true;
|
|
128
|
+
lastCodeIdx = i;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (!/\s/.test(ch)) {
|
|
132
|
+
lastCodeIdx = i;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return lastCodeIdx;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function addPluginToJsoncFile(filePath, defaultSchema, pluginEntryStr, matchSubstr) {
|
|
139
|
+
if (!fs.existsSync(filePath)) {
|
|
140
|
+
const initial =
|
|
141
|
+
"{\n" +
|
|
142
|
+
' "$schema": "' +
|
|
143
|
+
defaultSchema +
|
|
144
|
+
'",\n' +
|
|
145
|
+
' "plugins": [\n' +
|
|
146
|
+
" " +
|
|
147
|
+
pluginEntryStr +
|
|
148
|
+
"\n" +
|
|
149
|
+
" ]\n" +
|
|
150
|
+
"}\n";
|
|
151
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
152
|
+
fs.writeFileSync(filePath, initial, "utf8");
|
|
153
|
+
return "created";
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const raw = fs.readFileSync(filePath, "utf8");
|
|
157
|
+
if (raw.includes(matchSubstr)) {
|
|
158
|
+
return "already-present";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const span = findPluginsArraySpan(raw);
|
|
162
|
+
if (span) {
|
|
163
|
+
const { openBracketIdx, closeBracketIdx } = span;
|
|
164
|
+
const insideRaw = raw.slice(openBracketIdx + 1, closeBracketIdx);
|
|
165
|
+
const lastCodeIdx = findLastCodeCharIndex(insideRaw);
|
|
166
|
+
if (lastCodeIdx === -1) {
|
|
167
|
+
const updated =
|
|
168
|
+
raw.slice(0, openBracketIdx + 1) +
|
|
169
|
+
"\n " +
|
|
170
|
+
pluginEntryStr +
|
|
171
|
+
"\n " +
|
|
172
|
+
raw.slice(closeBracketIdx);
|
|
173
|
+
fs.writeFileSync(filePath, updated, "utf8");
|
|
174
|
+
return "updated";
|
|
175
|
+
}
|
|
176
|
+
const lastChar = insideRaw[lastCodeIdx];
|
|
177
|
+
const withComma =
|
|
178
|
+
lastChar === ","
|
|
179
|
+
? insideRaw.replace(/\s+$/, "")
|
|
180
|
+
: insideRaw.slice(0, lastCodeIdx + 1) + "," + insideRaw.slice(lastCodeIdx + 1).replace(/\s+$/, "");
|
|
181
|
+
const updated =
|
|
182
|
+
raw.slice(0, openBracketIdx + 1) +
|
|
183
|
+
withComma +
|
|
184
|
+
"\n " +
|
|
185
|
+
pluginEntryStr +
|
|
186
|
+
"\n " +
|
|
187
|
+
raw.slice(closeBracketIdx);
|
|
188
|
+
fs.writeFileSync(filePath, updated, "utf8");
|
|
189
|
+
return "updated";
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const lastBraceIdx = raw.lastIndexOf("}");
|
|
193
|
+
if (lastBraceIdx !== -1) {
|
|
194
|
+
const beforeRaw = raw.slice(0, lastBraceIdx);
|
|
195
|
+
const lastCodeIdx = findLastCodeCharIndex(beforeRaw);
|
|
196
|
+
const lastChar = lastCodeIdx !== -1 ? beforeRaw[lastCodeIdx] : "{";
|
|
197
|
+
const needsComma = lastChar !== "{" && lastChar !== ",";
|
|
198
|
+
const withComma = needsComma
|
|
199
|
+
? beforeRaw.slice(0, lastCodeIdx + 1) + "," + beforeRaw.slice(lastCodeIdx + 1).replace(/\s+$/, "")
|
|
200
|
+
: beforeRaw.replace(/\s+$/, "");
|
|
201
|
+
const updated =
|
|
202
|
+
withComma +
|
|
203
|
+
'\n "plugins": [\n ' +
|
|
204
|
+
pluginEntryStr +
|
|
205
|
+
"\n ]\n}\n";
|
|
206
|
+
fs.writeFileSync(filePath, updated, "utf8");
|
|
207
|
+
return "updated";
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return "skipped";
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export async function installPlugin(options) {
|
|
214
|
+
const opts = options || {};
|
|
215
|
+
const configDir = resolveConfigDir(opts.configDir);
|
|
216
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
217
|
+
|
|
218
|
+
const pkgName = "@nandoblanco/opencode-local-tokens";
|
|
219
|
+
const matchKey = "opencode-local-tokens";
|
|
220
|
+
const entryStr = opts.cacheAlerts === true
|
|
221
|
+
? JSON.stringify({ package: pkgName, options: { cacheAlerts: true } })
|
|
222
|
+
: JSON.stringify(pkgName);
|
|
223
|
+
|
|
224
|
+
const jsoncCandidate = path.join(configDir, cliName + ".jsonc");
|
|
225
|
+
const jsonCandidate = path.join(configDir, cliName + ".json");
|
|
226
|
+
const serverConfigFile = fs.existsSync(jsoncCandidate)
|
|
227
|
+
? jsoncCandidate
|
|
228
|
+
: fs.existsSync(jsonCandidate)
|
|
229
|
+
? jsonCandidate
|
|
230
|
+
: jsoncCandidate;
|
|
231
|
+
|
|
232
|
+
const serverStatus = addPluginToJsoncFile(
|
|
233
|
+
serverConfigFile,
|
|
234
|
+
"https://" + cliName + ".ai/config.json",
|
|
235
|
+
entryStr,
|
|
236
|
+
matchKey
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
return {
|
|
240
|
+
configDir,
|
|
241
|
+
serverConfigFile,
|
|
242
|
+
serverStatus,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const isDirectRun =
|
|
247
|
+
process.argv[1] && path.resolve(process.argv[1]) === path.resolve(fileURLToPath(import.meta.url));
|
|
248
|
+
if (isDirectRun) {
|
|
249
|
+
const args = process.argv.slice(2);
|
|
250
|
+
const cmd = args[0];
|
|
251
|
+
|
|
252
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
253
|
+
console.log(
|
|
254
|
+
"opencode-tokens install [--dir <config-dir>] [--cache-alerts]\n" +
|
|
255
|
+
"opencode-tokens [query] [--project name] [--model name] [--tag tag] [--liked] [--limit count] [--sort mode]\n" +
|
|
256
|
+
"Sort: billed, total, steps, updated, newest, oldest, title, relevance\n" +
|
|
257
|
+
"Commands: install, like <session>, unlike <session>, tag <session> <tags>, untag <session> <tags>, note <session> <text>\n" +
|
|
258
|
+
"Tags: comma-separated, e.g. review,billing\n" +
|
|
259
|
+
"In OpenCode: /tokens opens analytics (Ctrl+S sort, Ctrl+T add tags, Ctrl+R remove tags); /handoff creates a checkpoint for a fresh session."
|
|
260
|
+
);
|
|
261
|
+
} else if (cmd === "install" || cmd === "init" || cmd === "setup") {
|
|
262
|
+
let configDir;
|
|
263
|
+
let cacheAlerts = false;
|
|
264
|
+
for (let i = 1; i < args.length; i++) {
|
|
265
|
+
if ((args[i] === "--dir" || args[i] === "-d") && args[i + 1]) {
|
|
266
|
+
configDir = args[i + 1];
|
|
267
|
+
i++;
|
|
268
|
+
} else if (args[i] === "--cache-alerts") {
|
|
269
|
+
cacheAlerts = true;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
installPlugin({ configDir, cacheAlerts })
|
|
273
|
+
.then((res) => {
|
|
274
|
+
console.log("opencode-local-tokens installed:");
|
|
275
|
+
console.log(" Config file: " + res.serverConfigFile + " (" + res.serverStatus + ")");
|
|
276
|
+
})
|
|
277
|
+
.catch((err) => {
|
|
278
|
+
console.error("Install failed:", err);
|
|
279
|
+
process.exit(1);
|
|
280
|
+
});
|
|
281
|
+
} else {
|
|
282
|
+
import("./src/tokens.ts")
|
|
283
|
+
.then(({ runTokensCli }) => runTokensCli(args))
|
|
284
|
+
.catch((err) => {
|
|
285
|
+
console.error("Command failed:", err);
|
|
286
|
+
process.exit(1);
|
|
287
|
+
});
|
|
288
|
+
}
|
|
9
289
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nandoblanco/opencode-local-tokens",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"opentui"
|
|
19
19
|
],
|
|
20
20
|
"license": "MIT",
|
|
21
|
-
"author": "
|
|
21
|
+
"author": "nandoblanco",
|
|
22
22
|
"repository": {
|
|
23
23
|
"type": "git",
|
|
24
24
|
"url": "git+https://github.com/nandoblanco/opencode-local-tokens.git"
|