skilltally 1.0.3 → 1.1.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.
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/CHANGELOG.md +13 -0
- package/README.md +12 -5
- package/bin/cli.mjs +163 -1
- package/gemini-extension.json +1 -1
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/skills/skilltally/SKILL.md +23 -8
- package/src/install/prune.mjs +355 -0
- package/src/mcp/server.mjs +345 -0
- package/src/report/aggregate.mjs +206 -17
- package/src/report/chains.mjs +187 -0
- package/src/report/dashboard.mjs +1245 -0
- package/src/report/format.mjs +223 -2
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
mkdirSync,
|
|
4
|
+
readdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
renameSync,
|
|
7
|
+
writeFileSync,
|
|
8
|
+
} from 'node:fs';
|
|
9
|
+
import { homedir } from 'node:os';
|
|
10
|
+
import { dirname, join } from 'node:path';
|
|
11
|
+
import { getHomeDir, loadConfig } from '../core/config.mjs';
|
|
12
|
+
import { readAllEvents } from '../core/store.mjs';
|
|
13
|
+
import { scanInventory } from '../inventory/scan.mjs';
|
|
14
|
+
import { aggregateStats } from '../report/aggregate.mjs';
|
|
15
|
+
import { backupFile, readJsonObject, writeJsonAtomic } from './runtime.mjs';
|
|
16
|
+
|
|
17
|
+
function normalizePluginName(raw) {
|
|
18
|
+
const s = String(raw || '').trim();
|
|
19
|
+
if (!s) return '';
|
|
20
|
+
return s.startsWith('plugin:') ? s.slice(7) : s;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function findCodexPluginKeysInCache(homeDir, pluginName) {
|
|
24
|
+
const cacheRoot = join(homeDir, '.codex', 'plugins', 'cache');
|
|
25
|
+
const keys = [];
|
|
26
|
+
if (!existsSync(cacheRoot)) return keys;
|
|
27
|
+
try {
|
|
28
|
+
for (const mkt of readdirSync(cacheRoot, { withFileTypes: true })) {
|
|
29
|
+
if (!mkt.isDirectory() && !mkt.isSymbolicLink()) continue;
|
|
30
|
+
if (existsSync(join(cacheRoot, mkt.name, pluginName))) {
|
|
31
|
+
keys.push(`${pluginName}@${mkt.name}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
} catch {}
|
|
35
|
+
return keys;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function disableCodexPluginInToml(tomlRaw, pluginName, cacheKeys) {
|
|
39
|
+
const lines = tomlRaw ? tomlRaw.split(/\r?\n/) : [];
|
|
40
|
+
const escaped = pluginName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
41
|
+
const secRe = new RegExp(`^\\[plugins\\.(?:"${escaped}@[^"]+"|'${escaped}@[^']+'|${escaped})\\]$`);
|
|
42
|
+
|
|
43
|
+
let matchedSection = false;
|
|
44
|
+
let inTargetSection = false;
|
|
45
|
+
let sawEnabledLine = false;
|
|
46
|
+
const out = [];
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < lines.length; i++) {
|
|
49
|
+
const rawLine = lines[i];
|
|
50
|
+
const clean = rawLine.replace(/#.*$/, '').trim();
|
|
51
|
+
if (clean.startsWith('[')) {
|
|
52
|
+
if (inTargetSection && !sawEnabledLine) {
|
|
53
|
+
out.push('enabled = false');
|
|
54
|
+
}
|
|
55
|
+
inTargetSection = secRe.test(clean);
|
|
56
|
+
if (inTargetSection) {
|
|
57
|
+
matchedSection = true;
|
|
58
|
+
sawEnabledLine = false;
|
|
59
|
+
}
|
|
60
|
+
out.push(rawLine);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (inTargetSection && /^enabled\s*=/.test(clean)) {
|
|
65
|
+
sawEnabledLine = true;
|
|
66
|
+
out.push('enabled = false');
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
out.push(rawLine);
|
|
71
|
+
}
|
|
72
|
+
if (inTargetSection && !sawEnabledLine) {
|
|
73
|
+
out.push('enabled = false');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (matchedSection) {
|
|
77
|
+
return out.join('\n').replace(/\n*$/, '\n');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (cacheKeys.length > 0) {
|
|
81
|
+
const prefix = tomlRaw && !tomlRaw.endsWith('\n') ? tomlRaw + '\n' : tomlRaw;
|
|
82
|
+
const additions = cacheKeys.map((k) => `[plugins."${k}"]\nenabled = false`).join('\n\n');
|
|
83
|
+
return `${prefix}${prefix ? '\n' : ''}${additions}\n`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function hasCodexDisabledSkillPath(tomlRaw, canonPath) {
|
|
90
|
+
if (!tomlRaw) return false;
|
|
91
|
+
let inSkillCfg = false;
|
|
92
|
+
let pathVal = null;
|
|
93
|
+
let enabledVal = null;
|
|
94
|
+
|
|
95
|
+
const check = () =>
|
|
96
|
+
inSkillCfg && pathVal && pathVal.replace(/\\/g, '/') === canonPath && enabledVal === false;
|
|
97
|
+
|
|
98
|
+
for (const rawLine of tomlRaw.split(/\r?\n/)) {
|
|
99
|
+
const line = rawLine.replace(/#.*$/, '').trim();
|
|
100
|
+
if (!line) continue;
|
|
101
|
+
if (line === '[[skills.config]]') {
|
|
102
|
+
if (check()) return true;
|
|
103
|
+
inSkillCfg = true;
|
|
104
|
+
pathVal = null;
|
|
105
|
+
enabledVal = null;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (line.startsWith('[')) {
|
|
109
|
+
if (check()) return true;
|
|
110
|
+
inSkillCfg = false;
|
|
111
|
+
pathVal = null;
|
|
112
|
+
enabledVal = null;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (inSkillCfg) {
|
|
116
|
+
const pm = line.match(/^path\s*=\s*(?:"([^"]+)"|'([^']+)')/);
|
|
117
|
+
if (pm) pathVal = pm[1] || pm[2];
|
|
118
|
+
const em = line.match(/^enabled\s*=\s*(true|false)\b/);
|
|
119
|
+
if (em) enabledVal = em[1] === 'true';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return check();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function appendCodexDisabledSkill(tomlRaw, canonPath) {
|
|
126
|
+
if (hasCodexDisabledSkillPath(tomlRaw, canonPath)) return null;
|
|
127
|
+
const escaped = canonPath.replace(/\\/g, '/').replace(/"/g, '\\"');
|
|
128
|
+
const prefix = tomlRaw ? (tomlRaw.endsWith('\n') ? tomlRaw : tomlRaw + '\n') : '';
|
|
129
|
+
return `${prefix}${prefix ? '\n' : ''}[[skills.config]]\npath = "${escaped}"\nenabled = false\n`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function writeTextAtomic(filePath, text, mode = 0o600) {
|
|
133
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
134
|
+
const tmp = `${filePath}.tmp.${process.pid}`;
|
|
135
|
+
writeFileSync(tmp, text, { mode });
|
|
136
|
+
renameSync(tmp, filePath);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Safely disable 100%-unused plugins or explicitly specified unused skills
|
|
141
|
+
* across Antigravity and Codex with automatic config backups.
|
|
142
|
+
*/
|
|
143
|
+
export function runPrune({
|
|
144
|
+
homeDir = homedir(),
|
|
145
|
+
dataHome = getHomeDir(),
|
|
146
|
+
cwd = process.cwd(),
|
|
147
|
+
since = '90d',
|
|
148
|
+
host = 'all',
|
|
149
|
+
plugins = [],
|
|
150
|
+
skills = [],
|
|
151
|
+
dryRun = false,
|
|
152
|
+
now = new Date(),
|
|
153
|
+
} = {}) {
|
|
154
|
+
const { events } = readAllEvents(dataHome);
|
|
155
|
+
const config = loadConfig(dataHome);
|
|
156
|
+
const inventory = scanInventory({ homeDir, cwd, includeProject: false });
|
|
157
|
+
const filterHost = !host || host === 'all' ? null : host;
|
|
158
|
+
const report = aggregateStats({
|
|
159
|
+
events,
|
|
160
|
+
inventory,
|
|
161
|
+
config,
|
|
162
|
+
since,
|
|
163
|
+
host: filterHost,
|
|
164
|
+
now,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const enabledBySource = new Map();
|
|
168
|
+
for (const s of inventory.skills) {
|
|
169
|
+
if (!s.enabled) continue;
|
|
170
|
+
let list = enabledBySource.get(s.source);
|
|
171
|
+
if (!list) {
|
|
172
|
+
list = [];
|
|
173
|
+
enabledBySource.set(s.source, list);
|
|
174
|
+
}
|
|
175
|
+
list.push(s);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const reqPlugins = new Set(plugins.map(normalizePluginName).filter(Boolean));
|
|
179
|
+
const reqSkills = new Set(skills.map((s) => String(s || '').trim()).filter(Boolean));
|
|
180
|
+
const targetPluginsOnly = reqPlugins.size > 0;
|
|
181
|
+
const targetSkillsOnly = reqSkills.size > 0 && reqPlugins.size === 0;
|
|
182
|
+
|
|
183
|
+
// Qualifying 100% unused plugins
|
|
184
|
+
const qualifyingPlugins = [];
|
|
185
|
+
if (!targetSkillsOnly) {
|
|
186
|
+
for (const group of report.unused.bySource) {
|
|
187
|
+
if (!group.source.startsWith('plugin:')) continue;
|
|
188
|
+
const pluginName = group.source.slice(7);
|
|
189
|
+
if (targetPluginsOnly && !reqPlugins.has(pluginName)) continue;
|
|
190
|
+
|
|
191
|
+
const totalEnabledInSource = (enabledBySource.get(group.source) || []).length;
|
|
192
|
+
if (totalEnabledInSource === 0 || group.skillCount < totalEnabledInSource) {
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const hostsSet = new Set();
|
|
197
|
+
for (const s of group.skills) {
|
|
198
|
+
for (const h of s.hosts || []) hostsSet.add(h);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
qualifyingPlugins.push({
|
|
202
|
+
source: group.source,
|
|
203
|
+
plugin: pluginName,
|
|
204
|
+
skillCount: group.skillCount,
|
|
205
|
+
estTokens: group.estTokens,
|
|
206
|
+
descChars: group.descChars,
|
|
207
|
+
hosts: [...hostsSet],
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const allowAntigravity = !filterHost || filterHost === 'antigravity';
|
|
213
|
+
const allowCodex = !filterHost || filterHost === 'codex';
|
|
214
|
+
|
|
215
|
+
const disabledPlugins = [];
|
|
216
|
+
const disabledSkills = [];
|
|
217
|
+
const backups = [];
|
|
218
|
+
|
|
219
|
+
// 1. Disable Antigravity plugins via ~/.gemini/config/config.json
|
|
220
|
+
if (allowAntigravity && qualifyingPlugins.length > 0) {
|
|
221
|
+
const agyCfgPath = join(homeDir, '.gemini', 'config', 'config.json');
|
|
222
|
+
const agyTargets = qualifyingPlugins.filter((p) => {
|
|
223
|
+
if (!p.hosts.includes('antigravity')) return false;
|
|
224
|
+
return (
|
|
225
|
+
existsSync(join(homeDir, '.gemini', 'config', 'plugins', p.plugin)) ||
|
|
226
|
+
existsSync(join(homeDir, '.gemini', 'antigravity', 'builtin', 'plugins', p.plugin))
|
|
227
|
+
);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
if (agyTargets.length > 0) {
|
|
231
|
+
const doc = existsSync(agyCfgPath) ? readJsonObject(agyCfgPath) : {};
|
|
232
|
+
const pluginsObj =
|
|
233
|
+
doc.plugins && typeof doc.plugins === 'object' && !Array.isArray(doc.plugins)
|
|
234
|
+
? { ...doc.plugins }
|
|
235
|
+
: {};
|
|
236
|
+
let agyChanged = false;
|
|
237
|
+
|
|
238
|
+
for (const p of agyTargets) {
|
|
239
|
+
const cur = pluginsObj[p.plugin];
|
|
240
|
+
if (!cur || cur.enabled !== false) {
|
|
241
|
+
pluginsObj[p.plugin] = { ...(cur && typeof cur === 'object' ? cur : {}), enabled: false };
|
|
242
|
+
agyChanged = true;
|
|
243
|
+
disabledPlugins.push({
|
|
244
|
+
source: p.source,
|
|
245
|
+
plugin: p.plugin,
|
|
246
|
+
host: 'antigravity',
|
|
247
|
+
skillCount: p.skillCount,
|
|
248
|
+
estTokens: p.estTokens,
|
|
249
|
+
descChars: p.descChars,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (agyChanged) {
|
|
255
|
+
doc.plugins = pluginsObj;
|
|
256
|
+
const bak = backupFile(agyCfgPath, dataHome, 'antigravity-config-prune', dryRun);
|
|
257
|
+
if (bak) backups.push(bak);
|
|
258
|
+
if (!dryRun) {
|
|
259
|
+
writeJsonAtomic(agyCfgPath, doc, 0o600);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// 2. Disable Codex plugins & explicitly requested Codex skills via ~/.codex/config.toml
|
|
266
|
+
if (allowCodex) {
|
|
267
|
+
const codexTomlPath = join(homeDir, '.codex', 'config.toml');
|
|
268
|
+
let tomlRaw = existsSync(codexTomlPath) ? readFileSync(codexTomlPath, 'utf8') : '';
|
|
269
|
+
let codexChanged = false;
|
|
270
|
+
|
|
271
|
+
for (const p of qualifyingPlugins) {
|
|
272
|
+
if (!p.hosts.includes('codex')) continue;
|
|
273
|
+
const cacheKeys = findCodexPluginKeysInCache(homeDir, p.plugin);
|
|
274
|
+
const updated = disableCodexPluginInToml(tomlRaw, p.plugin, cacheKeys);
|
|
275
|
+
if (updated !== null && updated !== tomlRaw) {
|
|
276
|
+
tomlRaw = updated;
|
|
277
|
+
codexChanged = true;
|
|
278
|
+
disabledPlugins.push({
|
|
279
|
+
source: p.source,
|
|
280
|
+
plugin: p.plugin,
|
|
281
|
+
host: 'codex',
|
|
282
|
+
skillCount: p.skillCount,
|
|
283
|
+
estTokens: p.estTokens,
|
|
284
|
+
descChars: p.descChars,
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (reqSkills.size > 0) {
|
|
290
|
+
const unusedSkillSet = new Set();
|
|
291
|
+
for (const g of report.unused.bySource) {
|
|
292
|
+
for (const s of g.skills) unusedSkillSet.add(s.path);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
for (const s of inventory.skills) {
|
|
296
|
+
if (!s.enabled) continue;
|
|
297
|
+
if (!reqSkills.has(s.name) && !reqSkills.has(s.dir_name)) continue;
|
|
298
|
+
if (!s.hosts.includes('codex')) continue;
|
|
299
|
+
// Prefer disabling only unused skills unless explicitly absent from used rows
|
|
300
|
+
if (!unusedSkillSet.has(s.path) && report.rows.some((r) => r.skill === s.name)) {
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
const updated = appendCodexDisabledSkill(tomlRaw, s.path);
|
|
304
|
+
if (updated !== null && updated !== tomlRaw) {
|
|
305
|
+
tomlRaw = updated;
|
|
306
|
+
codexChanged = true;
|
|
307
|
+
disabledSkills.push({
|
|
308
|
+
name: s.name,
|
|
309
|
+
source: s.source,
|
|
310
|
+
host: 'codex',
|
|
311
|
+
path: s.path,
|
|
312
|
+
est_tokens: s.est_tokens || 0,
|
|
313
|
+
desc_chars: s.desc_chars || 0,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (codexChanged) {
|
|
320
|
+
const bak = backupFile(codexTomlPath, dataHome, 'codex-config-prune', dryRun);
|
|
321
|
+
if (bak) backups.push(bak);
|
|
322
|
+
if (!dryRun) {
|
|
323
|
+
writeTextAtomic(codexTomlPath, tomlRaw, 0o600);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Deduplicate savings across hosts if the same plugin appeared on multiple hosts
|
|
329
|
+
const seenSavingsKeys = new Set();
|
|
330
|
+
let savedTokensPerSession = 0;
|
|
331
|
+
let savedDescChars = 0;
|
|
332
|
+
|
|
333
|
+
for (const p of disabledPlugins) {
|
|
334
|
+
if (seenSavingsKeys.has(p.source)) continue;
|
|
335
|
+
seenSavingsKeys.add(p.source);
|
|
336
|
+
savedTokensPerSession += p.estTokens || 0;
|
|
337
|
+
savedDescChars += p.descChars || 0;
|
|
338
|
+
}
|
|
339
|
+
for (const s of disabledSkills) {
|
|
340
|
+
if (seenSavingsKeys.has(s.path)) continue;
|
|
341
|
+
seenSavingsKeys.add(s.path);
|
|
342
|
+
savedTokensPerSession += s.est_tokens || 0;
|
|
343
|
+
savedDescChars += s.desc_chars || 0;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return {
|
|
347
|
+
dryRun,
|
|
348
|
+
window: report.window,
|
|
349
|
+
disabledPlugins,
|
|
350
|
+
disabledSkills,
|
|
351
|
+
savedTokensPerSession,
|
|
352
|
+
savedKb: Number((savedDescChars / 1024).toFixed(1)),
|
|
353
|
+
backups,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { getHomeDir, loadConfig } from '../core/config.mjs';
|
|
3
|
+
import { readAllEvents } from '../core/store.mjs';
|
|
4
|
+
import { scanInventory } from '../inventory/scan.mjs';
|
|
5
|
+
import { aggregateStats } from '../report/aggregate.mjs';
|
|
6
|
+
import { buildChains } from '../report/chains.mjs';
|
|
7
|
+
import { buildTimeline } from '../report/timeline.mjs';
|
|
8
|
+
|
|
9
|
+
const MCP_TOOLS = [
|
|
10
|
+
{
|
|
11
|
+
name: 'skilltally_stats',
|
|
12
|
+
description:
|
|
13
|
+
'Get skill activation and read statistics (including 14d sparklines, trigger ratios, and context ROI) across Antigravity, Claude Code, Codex, and Gemini CLI.',
|
|
14
|
+
inputSchema: {
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
since: { type: 'string', description: 'Time window: e.g. 7d, 30d, 90d, 2026-01-01, or all (default: 30d)' },
|
|
18
|
+
host: { type: 'string', description: 'Optional host filter: antigravity, claude, codex, gemini, or all' },
|
|
19
|
+
project: { type: 'string', description: 'Optional project basename filter' },
|
|
20
|
+
bySource: { type: 'boolean', description: 'Separate duplicate skill names by source' },
|
|
21
|
+
metric: { type: 'string', enum: ['activations', 'reads'], description: 'Sort metric (default: activations)' },
|
|
22
|
+
limit: { type: 'number', description: 'Optional max number of skill rows to return' },
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: 'skilltally_unused',
|
|
28
|
+
description:
|
|
29
|
+
'List enabled installed skills with zero activations in the window and compute Context Tax (KB and ~tokens/session) grouped by source.',
|
|
30
|
+
inputSchema: {
|
|
31
|
+
type: 'object',
|
|
32
|
+
properties: {
|
|
33
|
+
since: { type: 'string', description: 'Time window (default: 90d)' },
|
|
34
|
+
host: { type: 'string', description: 'Optional host filter' },
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: 'skilltally_timeline',
|
|
40
|
+
description: 'Aggregate skill activations and reads over time buckets (day, week, or month).',
|
|
41
|
+
inputSchema: {
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: {
|
|
44
|
+
skill: { type: 'string', description: 'Optional skill name filter' },
|
|
45
|
+
by: { type: 'string', enum: ['day', 'week', 'month'], description: 'Bucket granularity (default: day)' },
|
|
46
|
+
since: { type: 'string', description: 'Time window (default: 30d)' },
|
|
47
|
+
host: { type: 'string', description: 'Optional host filter' },
|
|
48
|
+
project: { type: 'string', description: 'Optional project basename filter' },
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: 'skilltally_chains',
|
|
54
|
+
description:
|
|
55
|
+
'Analyze multi-skill session workflows: ordered transitions (A -> B), 3-step sequences, and co-occurring skill pairs with Jaccard similarity.',
|
|
56
|
+
inputSchema: {
|
|
57
|
+
type: 'object',
|
|
58
|
+
properties: {
|
|
59
|
+
since: { type: 'string', description: 'Time window (default: 30d)' },
|
|
60
|
+
host: { type: 'string', description: 'Optional host filter' },
|
|
61
|
+
project: { type: 'string', description: 'Optional project basename filter' },
|
|
62
|
+
limit: { type: 'number', description: 'Max entries per section (default: 20)' },
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: 'skilltally_insights',
|
|
68
|
+
description:
|
|
69
|
+
'Return actionable optimization insights: Context Tax ROI, manual-only skills needing better SKILL.md descriptions, single-project scoping candidates, and model breakdown.',
|
|
70
|
+
inputSchema: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
since: { type: 'string', description: 'Time window (default: 30d)' },
|
|
74
|
+
host: { type: 'string', description: 'Optional host filter' },
|
|
75
|
+
project: { type: 'string', description: 'Optional project basename filter' },
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
function toolError(id, message) {
|
|
82
|
+
return {
|
|
83
|
+
jsonrpc: '2.0',
|
|
84
|
+
id,
|
|
85
|
+
result: {
|
|
86
|
+
isError: true,
|
|
87
|
+
content: [{ type: 'text', text: String(message) }],
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function toolOk(id, payload) {
|
|
93
|
+
return {
|
|
94
|
+
jsonrpc: '2.0',
|
|
95
|
+
id,
|
|
96
|
+
result: {
|
|
97
|
+
content: [{ type: 'text', text: JSON.stringify(payload, null, 2) }],
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function handleMcpRequest(
|
|
103
|
+
msg,
|
|
104
|
+
{ dataHome = getHomeDir(), pkgVersion = '1.1.0', homeDir = homedir(), cwd = process.cwd() } = {}
|
|
105
|
+
) {
|
|
106
|
+
if (!msg || typeof msg !== 'object') {
|
|
107
|
+
return { jsonrpc: '2.0', id: null, error: { code: -32600, message: 'Invalid Request' } };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const method = msg.method;
|
|
111
|
+
const hasId = msg.id !== undefined && msg.id !== null;
|
|
112
|
+
|
|
113
|
+
if (typeof method === 'string' && method.startsWith('notifications/')) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
if (!hasId) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (method === 'initialize') {
|
|
121
|
+
return {
|
|
122
|
+
jsonrpc: '2.0',
|
|
123
|
+
id: msg.id,
|
|
124
|
+
result: {
|
|
125
|
+
protocolVersion: msg.params?.protocolVersion || '2024-11-05',
|
|
126
|
+
capabilities: { tools: {} },
|
|
127
|
+
serverInfo: { name: 'skilltally', version: pkgVersion },
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (method === 'ping') {
|
|
133
|
+
return { jsonrpc: '2.0', id: msg.id, result: {} };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (method === 'tools/list') {
|
|
137
|
+
return {
|
|
138
|
+
jsonrpc: '2.0',
|
|
139
|
+
id: msg.id,
|
|
140
|
+
result: { tools: MCP_TOOLS },
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (method === 'tools/call') {
|
|
145
|
+
const toolName = msg.params?.name;
|
|
146
|
+
const args = msg.params?.arguments && typeof msg.params.arguments === 'object' ? msg.params.arguments : {};
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
const { events, corruptLines } = readAllEvents(dataHome);
|
|
150
|
+
const config = loadConfig(dataHome);
|
|
151
|
+
const inventory = scanInventory({ homeDir, cwd, includeProject: true });
|
|
152
|
+
|
|
153
|
+
if (toolName === 'skilltally_stats') {
|
|
154
|
+
const report = aggregateStats({
|
|
155
|
+
events,
|
|
156
|
+
inventory,
|
|
157
|
+
config,
|
|
158
|
+
since: args.since ?? '30d',
|
|
159
|
+
host: args.host || null,
|
|
160
|
+
project: args.project || null,
|
|
161
|
+
bySource: Boolean(args.bySource),
|
|
162
|
+
metric: args.metric === 'reads' ? 'reads' : 'activations',
|
|
163
|
+
});
|
|
164
|
+
const limit = args.limit !== undefined ? Number(args.limit) : null;
|
|
165
|
+
if (limit !== null && (!Number.isFinite(limit) || limit < 1)) {
|
|
166
|
+
return toolError(msg.id, `Invalid limit: "${args.limit}"`);
|
|
167
|
+
}
|
|
168
|
+
const rows = limit ? report.rows.slice(0, limit) : report.rows;
|
|
169
|
+
return toolOk(msg.id, { ...report, rows, corruptLines });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (toolName === 'skilltally_unused') {
|
|
173
|
+
const report = aggregateStats({
|
|
174
|
+
events,
|
|
175
|
+
inventory,
|
|
176
|
+
config,
|
|
177
|
+
since: args.since ?? '90d',
|
|
178
|
+
host: args.host || null,
|
|
179
|
+
});
|
|
180
|
+
return toolOk(msg.id, {
|
|
181
|
+
window: report.window,
|
|
182
|
+
inventory: report.inventory,
|
|
183
|
+
unused: report.unused,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (toolName === 'skilltally_timeline') {
|
|
188
|
+
const timeline = buildTimeline({
|
|
189
|
+
events,
|
|
190
|
+
inventory,
|
|
191
|
+
config,
|
|
192
|
+
skill: args.skill || null,
|
|
193
|
+
by: args.by ?? 'day',
|
|
194
|
+
since: args.since ?? '30d',
|
|
195
|
+
host: args.host || null,
|
|
196
|
+
project: args.project || null,
|
|
197
|
+
});
|
|
198
|
+
return toolOk(msg.id, timeline);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (toolName === 'skilltally_chains') {
|
|
202
|
+
const limit = args.limit !== undefined ? Number(args.limit) : 20;
|
|
203
|
+
if (!Number.isFinite(limit) || limit < 1) {
|
|
204
|
+
return toolError(msg.id, `Invalid limit: "${args.limit}"`);
|
|
205
|
+
}
|
|
206
|
+
const chains = buildChains({
|
|
207
|
+
events,
|
|
208
|
+
inventory,
|
|
209
|
+
config,
|
|
210
|
+
since: args.since ?? '30d',
|
|
211
|
+
host: args.host || null,
|
|
212
|
+
project: args.project || null,
|
|
213
|
+
limit,
|
|
214
|
+
});
|
|
215
|
+
return toolOk(msg.id, chains);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (toolName === 'skilltally_insights') {
|
|
219
|
+
const report = aggregateStats({
|
|
220
|
+
events,
|
|
221
|
+
inventory,
|
|
222
|
+
config,
|
|
223
|
+
since: args.since ?? '30d',
|
|
224
|
+
host: args.host || null,
|
|
225
|
+
project: args.project || null,
|
|
226
|
+
});
|
|
227
|
+
return toolOk(msg.id, {
|
|
228
|
+
window: report.window,
|
|
229
|
+
totals: report.totals,
|
|
230
|
+
inventory: report.inventory,
|
|
231
|
+
insights: report.insights,
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return toolError(msg.id, `Unknown tool: "${toolName}"`);
|
|
236
|
+
} catch (err) {
|
|
237
|
+
return toolError(msg.id, err?.message || 'Tool execution failed');
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
jsonrpc: '2.0',
|
|
243
|
+
id: msg.id,
|
|
244
|
+
error: { code: -32601, message: `Method not found: ${method}` },
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function sendMcpMessage(output, obj, framed) {
|
|
249
|
+
const json = JSON.stringify(obj);
|
|
250
|
+
if (framed) {
|
|
251
|
+
const bytes = Buffer.byteLength(json, 'utf8');
|
|
252
|
+
output.write(`Content-Length: ${bytes}\r\n\r\n${json}`);
|
|
253
|
+
} else {
|
|
254
|
+
output.write(`${json}\n`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function startMcpServer({
|
|
259
|
+
dataHome = getHomeDir(),
|
|
260
|
+
pkgVersion = '1.1.0',
|
|
261
|
+
homeDir = homedir(),
|
|
262
|
+
cwd = process.cwd(),
|
|
263
|
+
input = process.stdin,
|
|
264
|
+
output = process.stdout,
|
|
265
|
+
} = {}) {
|
|
266
|
+
let buf = Buffer.alloc(0);
|
|
267
|
+
|
|
268
|
+
const dispatchRaw = (rawJson, framed) => {
|
|
269
|
+
const trimmed = rawJson.trim();
|
|
270
|
+
if (!trimmed) return;
|
|
271
|
+
let parsed;
|
|
272
|
+
try {
|
|
273
|
+
parsed = JSON.parse(trimmed);
|
|
274
|
+
} catch {
|
|
275
|
+
sendMcpMessage(
|
|
276
|
+
output,
|
|
277
|
+
{ jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error' } },
|
|
278
|
+
framed
|
|
279
|
+
);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const res = handleMcpRequest(parsed, { dataHome, pkgVersion, homeDir, cwd });
|
|
283
|
+
if (res !== null) {
|
|
284
|
+
sendMcpMessage(output, res, framed);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const drainBuffer = () => {
|
|
289
|
+
while (buf.length > 0) {
|
|
290
|
+
// Trim leading CR/LF before inspecting framing
|
|
291
|
+
let start = 0;
|
|
292
|
+
while (start < buf.length && (buf[start] === 0x0d || buf[start] === 0x0a)) {
|
|
293
|
+
start += 1;
|
|
294
|
+
}
|
|
295
|
+
if (start > 0) buf = buf.subarray(start);
|
|
296
|
+
if (buf.length === 0) break;
|
|
297
|
+
|
|
298
|
+
const prefix = buf.subarray(0, Math.min(32, buf.length)).toString('ascii');
|
|
299
|
+
if (/^content-length:/i.test(prefix)) {
|
|
300
|
+
let sepIdx = buf.indexOf('\r\n\r\n');
|
|
301
|
+
let sepLen = 4;
|
|
302
|
+
if (sepIdx === -1) {
|
|
303
|
+
sepIdx = buf.indexOf('\n\n');
|
|
304
|
+
sepLen = 2;
|
|
305
|
+
}
|
|
306
|
+
if (sepIdx === -1) break;
|
|
307
|
+
|
|
308
|
+
const header = buf.subarray(0, sepIdx).toString('ascii');
|
|
309
|
+
const m = header.match(/content-length:\s*(\d+)/i);
|
|
310
|
+
if (!m) {
|
|
311
|
+
buf = buf.subarray(sepIdx + sepLen);
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
const len = Number(m[1]);
|
|
315
|
+
const bodyStart = sepIdx + sepLen;
|
|
316
|
+
if (buf.length < bodyStart + len) break;
|
|
317
|
+
|
|
318
|
+
const body = buf.subarray(bodyStart, bodyStart + len).toString('utf8');
|
|
319
|
+
buf = buf.subarray(bodyStart + len);
|
|
320
|
+
dispatchRaw(body, true);
|
|
321
|
+
} else {
|
|
322
|
+
const nl = buf.indexOf(0x0a);
|
|
323
|
+
if (nl === -1) break;
|
|
324
|
+
const line = buf.subarray(0, nl).toString('utf8');
|
|
325
|
+
buf = buf.subarray(nl + 1);
|
|
326
|
+
dispatchRaw(line, false);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
input.on('data', (chunk) => {
|
|
332
|
+
const b = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
333
|
+
buf = Buffer.concat([buf, b]);
|
|
334
|
+
drainBuffer();
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
input.on('end', () => {
|
|
338
|
+
drainBuffer();
|
|
339
|
+
const rest = buf.toString('utf8').trim();
|
|
340
|
+
if (rest && !/^content-length:/i.test(rest)) {
|
|
341
|
+
dispatchRaw(rest, false);
|
|
342
|
+
buf = Buffer.alloc(0);
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
}
|