skilltally 0.0.0-stage → 1.0.1
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/.agents/plugins/marketplace.json +21 -0
- package/.claude-plugin/marketplace.json +18 -0
- package/.claude-plugin/plugin.json +13 -0
- package/.codex-plugin/plugin.json +39 -0
- package/CHANGELOG.md +46 -0
- package/LICENSE +21 -0
- package/README.md +146 -2
- package/bin/cli.mjs +321 -0
- package/bin/track.mjs +331 -0
- package/gemini-extension.json +5 -0
- package/hooks/agy-fast-track.sh +28 -0
- package/hooks/claude-codex-hooks.json +18 -0
- package/hooks.json +16 -0
- package/package.json +53 -4
- package/plugin.json +17 -0
- package/skills/skilltally/SKILL.md +59 -0
- package/src/backfill/antigravity.mjs +150 -0
- package/src/backfill/claude.mjs +129 -0
- package/src/backfill/codex.mjs +200 -0
- package/src/backfill/gemini.mjs +152 -0
- package/src/backfill/index.mjs +83 -0
- package/src/core/config.mjs +33 -0
- package/src/core/debug.mjs +58 -0
- package/src/core/detect.mjs +9 -0
- package/src/core/normalize.mjs +78 -0
- package/src/core/store.mjs +99 -0
- package/src/install/antigravity.mjs +135 -0
- package/src/install/claude.mjs +116 -0
- package/src/install/codex.mjs +187 -0
- package/src/install/doctor.mjs +268 -0
- package/src/install/gemini.mjs +93 -0
- package/src/install/index.mjs +110 -0
- package/src/install/runtime.mjs +210 -0
- package/src/inventory/scan.mjs +397 -0
- package/src/report/aggregate.mjs +217 -0
- package/src/report/export.mjs +93 -0
- package/src/report/format.mjs +271 -0
- package/src/report/timeline.mjs +117 -0
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, realpathSync } from 'node:fs';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { basename, dirname, join } from 'node:path';
|
|
4
|
+
import { resolveSource } from '../core/detect.mjs';
|
|
5
|
+
|
|
6
|
+
function stripJsonCommentsAndTrailingCommas(raw) {
|
|
7
|
+
let out = '';
|
|
8
|
+
let inString = false;
|
|
9
|
+
let escape = false;
|
|
10
|
+
for (let i = 0; i < raw.length; i++) {
|
|
11
|
+
const ch = raw[i];
|
|
12
|
+
const next = raw[i + 1];
|
|
13
|
+
if (inString) {
|
|
14
|
+
out += ch;
|
|
15
|
+
if (escape) {
|
|
16
|
+
escape = false;
|
|
17
|
+
} else if (ch === '\\') {
|
|
18
|
+
escape = true;
|
|
19
|
+
} else if (ch === '"') {
|
|
20
|
+
inString = false;
|
|
21
|
+
}
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (ch === '"') {
|
|
25
|
+
inString = true;
|
|
26
|
+
out += ch;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (ch === '/' && next === '/') {
|
|
30
|
+
while (i < raw.length && raw[i] !== '\n') i++;
|
|
31
|
+
out += '\n';
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (ch === '/' && next === '*') {
|
|
35
|
+
i += 2;
|
|
36
|
+
while (i < raw.length && !(raw[i] === '*' && raw[i + 1] === '/')) i++;
|
|
37
|
+
i++;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
out += ch;
|
|
41
|
+
}
|
|
42
|
+
return out.replace(/,\s*([}\]])/g, '$1');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readJsonSafe(filePath, jsonc = false) {
|
|
46
|
+
try {
|
|
47
|
+
const raw = readFileSync(filePath, 'utf8');
|
|
48
|
+
return JSON.parse(jsonc ? stripJsonCommentsAndTrailingCommas(raw) : raw);
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function parseFrontmatter(md) {
|
|
55
|
+
const clean = String(md || '').replace(/^\uFEFF/, '');
|
|
56
|
+
const match = clean.match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/);
|
|
57
|
+
if (!match) return { name: '', description: '' };
|
|
58
|
+
|
|
59
|
+
const lines = match[1].split(/\r?\n/);
|
|
60
|
+
const fields = {};
|
|
61
|
+
|
|
62
|
+
for (let i = 0; i < lines.length; i++) {
|
|
63
|
+
const line = lines[i];
|
|
64
|
+
const kv = line.match(/^([A-Za-z0-9_-]+):\s*(.*)$/);
|
|
65
|
+
if (!kv) continue;
|
|
66
|
+
const key = kv[1];
|
|
67
|
+
const rest = kv[2].trim();
|
|
68
|
+
|
|
69
|
+
if (/^[>|][+-]?$/.test(rest)) {
|
|
70
|
+
const folded = rest.startsWith('>');
|
|
71
|
+
const block = [];
|
|
72
|
+
let minIndent = Infinity;
|
|
73
|
+
while (i + 1 < lines.length) {
|
|
74
|
+
const nextLine = lines[i + 1];
|
|
75
|
+
if (nextLine.trim() === '') {
|
|
76
|
+
block.push('');
|
|
77
|
+
i++;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const indentMatch = nextLine.match(/^([ \t]+)/);
|
|
81
|
+
if (!indentMatch) break;
|
|
82
|
+
minIndent = Math.min(minIndent, indentMatch[1].length);
|
|
83
|
+
block.push(nextLine);
|
|
84
|
+
i++;
|
|
85
|
+
}
|
|
86
|
+
const strip = Number.isFinite(minIndent) ? minIndent : 0;
|
|
87
|
+
const normalized = block.map((l) => (l.length >= strip ? l.slice(strip) : ''));
|
|
88
|
+
while (normalized.length > 0 && normalized[normalized.length - 1] === '') normalized.pop();
|
|
89
|
+
fields[key] = (folded ? normalized.join(' ').replace(/\s+/g, ' ') : normalized.join('\n')).trim();
|
|
90
|
+
} else {
|
|
91
|
+
const parts = [rest];
|
|
92
|
+
while (i + 1 < lines.length && /^[ \t]+\S/.test(lines[i + 1])) {
|
|
93
|
+
parts.push(lines[++i].trim());
|
|
94
|
+
}
|
|
95
|
+
let val = parts.join(' ').trim();
|
|
96
|
+
if (
|
|
97
|
+
val.length >= 2 &&
|
|
98
|
+
((val[0] === '"' && val[val.length - 1] === '"') ||
|
|
99
|
+
(val[0] === "'" && val[val.length - 1] === "'"))
|
|
100
|
+
) {
|
|
101
|
+
val = val.slice(1, -1);
|
|
102
|
+
}
|
|
103
|
+
fields[key] = val;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
name: fields.name || '',
|
|
109
|
+
description: fields.description || '',
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function normalizePath(p) {
|
|
114
|
+
if (!p) return '';
|
|
115
|
+
let resolved = String(p);
|
|
116
|
+
try {
|
|
117
|
+
resolved = realpathSync(resolved);
|
|
118
|
+
} catch {}
|
|
119
|
+
return resolved.replace(/\\+/g, '/');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function readAntigravityPluginStates(homeDir) {
|
|
123
|
+
const cfg = readJsonSafe(join(homeDir, '.gemini', 'config', 'config.json'));
|
|
124
|
+
const pluginsCfg = cfg?.plugins && typeof cfg.plugins === 'object' ? cfg.plugins : {};
|
|
125
|
+
const map = new Map();
|
|
126
|
+
for (const [dir, state] of Object.entries(pluginsCfg)) {
|
|
127
|
+
if (state && typeof state === 'object' && typeof state.enabled === 'boolean') {
|
|
128
|
+
map.set(dir, state.enabled);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return map;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function readCodexConfigStates(homeDir) {
|
|
135
|
+
const disabledSkillPaths = new Set();
|
|
136
|
+
const pluginEnabled = new Map();
|
|
137
|
+
const tomlPath = join(homeDir, '.codex', 'config.toml');
|
|
138
|
+
if (!existsSync(tomlPath)) return { disabledSkillPaths, pluginEnabled };
|
|
139
|
+
|
|
140
|
+
let raw;
|
|
141
|
+
try {
|
|
142
|
+
raw = readFileSync(tomlPath, 'utf8');
|
|
143
|
+
} catch {
|
|
144
|
+
return { disabledSkillPaths, pluginEnabled };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
let sectionType = null;
|
|
148
|
+
let sectionKey = null;
|
|
149
|
+
let skillCfgPath = null;
|
|
150
|
+
let skillCfgEnabled = null;
|
|
151
|
+
|
|
152
|
+
const flushSkillCfg = () => {
|
|
153
|
+
if (sectionType === 'skills.config' && skillCfgPath && skillCfgEnabled === false) {
|
|
154
|
+
disabledSkillPaths.add(normalizePath(skillCfgPath));
|
|
155
|
+
}
|
|
156
|
+
skillCfgPath = null;
|
|
157
|
+
skillCfgEnabled = null;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
for (const rawLine of raw.split(/\r?\n/)) {
|
|
161
|
+
const line = rawLine.replace(/#.*$/, '').trim();
|
|
162
|
+
if (!line) continue;
|
|
163
|
+
|
|
164
|
+
if (line === '[[skills.config]]') {
|
|
165
|
+
flushSkillCfg();
|
|
166
|
+
sectionType = 'skills.config';
|
|
167
|
+
sectionKey = null;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const pluginSec = line.match(/^\[plugins\.(?:"([^"]+)"|'([^']+)'|([A-Za-z0-9_-]+))\]$/);
|
|
172
|
+
if (pluginSec) {
|
|
173
|
+
flushSkillCfg();
|
|
174
|
+
sectionType = 'plugin';
|
|
175
|
+
sectionKey = pluginSec[1] || pluginSec[2] || pluginSec[3];
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (line.startsWith('[')) {
|
|
180
|
+
flushSkillCfg();
|
|
181
|
+
sectionType = null;
|
|
182
|
+
sectionKey = null;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (sectionType === 'plugin' && sectionKey) {
|
|
187
|
+
const m = line.match(/^enabled\s*=\s*(true|false)\b/);
|
|
188
|
+
if (m) pluginEnabled.set(sectionKey, m[1] === 'true');
|
|
189
|
+
} else if (sectionType === 'skills.config') {
|
|
190
|
+
const pm = line.match(/^path\s*=\s*(?:"([^"]+)"|'([^']+)')/);
|
|
191
|
+
if (pm) skillCfgPath = pm[1] || pm[2];
|
|
192
|
+
const em = line.match(/^enabled\s*=\s*(true|false)\b/);
|
|
193
|
+
if (em) skillCfgEnabled = em[1] === 'true';
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
flushSkillCfg();
|
|
197
|
+
|
|
198
|
+
return { disabledSkillPaths, pluginEnabled };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function listImmediateSkillDirs(skillsRoot) {
|
|
202
|
+
if (!existsSync(skillsRoot)) return [];
|
|
203
|
+
let entries;
|
|
204
|
+
try {
|
|
205
|
+
entries = readdirSync(skillsRoot, { withFileTypes: true });
|
|
206
|
+
} catch {
|
|
207
|
+
return [];
|
|
208
|
+
}
|
|
209
|
+
const results = [];
|
|
210
|
+
for (const e of entries) {
|
|
211
|
+
if (e.name.startsWith('.')) continue;
|
|
212
|
+
if (!e.isDirectory() && !e.isSymbolicLink()) continue;
|
|
213
|
+
const skillMd = join(skillsRoot, e.name, 'SKILL.md');
|
|
214
|
+
if (existsSync(skillMd)) {
|
|
215
|
+
results.push({ dirName: e.name, skillMd });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return results;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function listDirectories(dir) {
|
|
222
|
+
if (!existsSync(dir)) return [];
|
|
223
|
+
try {
|
|
224
|
+
return readdirSync(dir, { withFileTypes: true })
|
|
225
|
+
.filter((e) => !e.name.startsWith('.') && (e.isDirectory() || e.isSymbolicLink()))
|
|
226
|
+
.map((e) => e.name)
|
|
227
|
+
.sort();
|
|
228
|
+
} catch {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function pickCodexCacheVersion(pluginDir) {
|
|
234
|
+
const versions = listDirectories(pluginDir);
|
|
235
|
+
if (versions.length === 0) return null;
|
|
236
|
+
if (versions.includes('latest')) return 'latest';
|
|
237
|
+
return versions[versions.length - 1];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function walkClaudePluginSkills(baseDir, depth = 0, out = []) {
|
|
241
|
+
if (depth > 5 || !existsSync(baseDir)) return out;
|
|
242
|
+
let entries;
|
|
243
|
+
try {
|
|
244
|
+
entries = readdirSync(baseDir, { withFileTypes: true });
|
|
245
|
+
} catch {
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
for (const e of entries) {
|
|
249
|
+
if (e.name.startsWith('.') || e.name === 'node_modules') continue;
|
|
250
|
+
if (!e.isDirectory() && !e.isSymbolicLink()) continue;
|
|
251
|
+
const full = join(baseDir, e.name);
|
|
252
|
+
if (e.name === 'skills') {
|
|
253
|
+
out.push(...listImmediateSkillDirs(full));
|
|
254
|
+
} else {
|
|
255
|
+
walkClaudePluginSkills(full, depth + 1, out);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return out;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function scanInventory({ homeDir = homedir(), cwd = process.cwd(), includeProject = true } = {}) {
|
|
262
|
+
const agyStates = readAntigravityPluginStates(homeDir);
|
|
263
|
+
const { disabledSkillPaths, pluginEnabled: codexPluginEnabled } = readCodexConfigStates(homeDir);
|
|
264
|
+
|
|
265
|
+
const byCanonPath = new Map();
|
|
266
|
+
|
|
267
|
+
const register = ({ dirName, skillMd, host, source, enabled = true }) => {
|
|
268
|
+
let md;
|
|
269
|
+
try {
|
|
270
|
+
md = readFileSync(skillMd, 'utf8');
|
|
271
|
+
} catch {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const fm = parseFrontmatter(md);
|
|
275
|
+
const canon = normalizePath(skillMd);
|
|
276
|
+
if (disabledSkillPaths.has(canon)) {
|
|
277
|
+
enabled = false;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const existing = byCanonPath.get(canon);
|
|
281
|
+
if (existing) {
|
|
282
|
+
if (!existing.hosts.includes(host)) existing.hosts.push(host);
|
|
283
|
+
existing.enabled = existing.enabled || enabled;
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const descChars = fm.description.length;
|
|
288
|
+
byCanonPath.set(canon, {
|
|
289
|
+
name: fm.name || dirName,
|
|
290
|
+
dir_name: dirName,
|
|
291
|
+
path: canon,
|
|
292
|
+
source: source || resolveSource(canon, cwd, homeDir),
|
|
293
|
+
hosts: [host],
|
|
294
|
+
bytes: Buffer.byteLength(md, 'utf8'),
|
|
295
|
+
desc_chars: descChars,
|
|
296
|
+
est_tokens: descChars > 0 ? Math.ceil(descChars / 4) : 0,
|
|
297
|
+
enabled,
|
|
298
|
+
});
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// 1. Built-in & Plugins first so symlinked personal entries preserve plugin/builtin source.
|
|
302
|
+
for (const item of listImmediateSkillDirs(join(homeDir, '.gemini', 'antigravity', 'builtin', 'skills'))) {
|
|
303
|
+
register({ ...item, host: 'antigravity', source: 'builtin', enabled: true });
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const agyBuiltinPluginsDir = join(homeDir, '.gemini', 'antigravity', 'builtin', 'plugins');
|
|
307
|
+
for (const pluginDir of listDirectories(agyBuiltinPluginsDir)) {
|
|
308
|
+
const manifest = readJsonSafe(join(agyBuiltinPluginsDir, pluginDir, 'plugin.json'), true);
|
|
309
|
+
const cfgState = agyStates.get(pluginDir);
|
|
310
|
+
const enabled = cfgState !== undefined ? cfgState : manifest?.disabled !== true;
|
|
311
|
+
for (const item of listImmediateSkillDirs(join(agyBuiltinPluginsDir, pluginDir, 'skills'))) {
|
|
312
|
+
register({ ...item, host: 'antigravity', source: `plugin:${pluginDir}`, enabled });
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const agyPluginsDir = join(homeDir, '.gemini', 'config', 'plugins');
|
|
317
|
+
for (const pluginDir of listDirectories(agyPluginsDir)) {
|
|
318
|
+
const manifest = readJsonSafe(join(agyPluginsDir, pluginDir, 'plugin.json'), true);
|
|
319
|
+
const cfgState = agyStates.get(pluginDir);
|
|
320
|
+
const enabled = cfgState !== undefined ? cfgState : manifest?.disabled !== true;
|
|
321
|
+
for (const item of listImmediateSkillDirs(join(agyPluginsDir, pluginDir, 'skills'))) {
|
|
322
|
+
register({ ...item, host: 'antigravity', source: `plugin:${pluginDir}`, enabled });
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const codexCacheRoot = join(homeDir, '.codex', 'plugins', 'cache');
|
|
327
|
+
for (const mkt of listDirectories(codexCacheRoot)) {
|
|
328
|
+
for (const pluginName of listDirectories(join(codexCacheRoot, mkt))) {
|
|
329
|
+
const pluginRoot = join(codexCacheRoot, mkt, pluginName);
|
|
330
|
+
const ver = pickCodexCacheVersion(pluginRoot);
|
|
331
|
+
if (!ver) continue;
|
|
332
|
+
const key = `${pluginName}@${mkt}`;
|
|
333
|
+
const enabled = codexPluginEnabled.get(key) !== false;
|
|
334
|
+
for (const item of listImmediateSkillDirs(join(pluginRoot, ver, 'skills'))) {
|
|
335
|
+
register({ ...item, host: 'codex', source: `plugin:${pluginName}`, enabled });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const claudePluginsRoot = join(homeDir, '.claude', 'plugins');
|
|
341
|
+
for (const item of walkClaudePluginSkills(claudePluginsRoot)) {
|
|
342
|
+
const src = resolveSource(item.skillMd, cwd, homeDir);
|
|
343
|
+
register({ ...item, host: 'claude', source: src !== 'unknown' ? src : 'plugin:claude', enabled: true });
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// 2. Personal skill roots
|
|
347
|
+
const personalRoots = [
|
|
348
|
+
{ dir: join(homeDir, '.gemini', 'config', 'skills'), host: 'antigravity' },
|
|
349
|
+
{ dir: join(homeDir, '.gemini', 'skills'), host: 'gemini' },
|
|
350
|
+
{ dir: join(homeDir, '.agents', 'skills'), host: 'codex' },
|
|
351
|
+
{ dir: join(homeDir, '.codex', 'skills'), host: 'codex' },
|
|
352
|
+
{ dir: join(homeDir, '.claude', 'skills'), host: 'claude' },
|
|
353
|
+
];
|
|
354
|
+
for (const root of personalRoots) {
|
|
355
|
+
for (const item of listImmediateSkillDirs(root.dir)) {
|
|
356
|
+
register({ ...item, host: root.host, source: 'personal', enabled: true });
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// 3. Project-level skills
|
|
361
|
+
if (includeProject && cwd) {
|
|
362
|
+
const projectRoots = [
|
|
363
|
+
{ dir: join(cwd, '.agents', 'skills'), host: 'antigravity' },
|
|
364
|
+
{ dir: join(cwd, '.claude', 'skills'), host: 'claude' },
|
|
365
|
+
{ dir: join(cwd, '.gemini', 'skills'), host: 'gemini' },
|
|
366
|
+
];
|
|
367
|
+
for (const root of projectRoots) {
|
|
368
|
+
for (const item of listImmediateSkillDirs(root.dir)) {
|
|
369
|
+
register({ ...item, host: root.host, source: 'project', enabled: true });
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const skills = [...byCanonPath.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
375
|
+
|
|
376
|
+
// Build lookup indexes (prefer enabled entries when duplicates exist by dir_name or name).
|
|
377
|
+
const byPath = new Map();
|
|
378
|
+
const byDirName = new Map();
|
|
379
|
+
const byName = new Map();
|
|
380
|
+
|
|
381
|
+
for (const entry of skills) {
|
|
382
|
+
byPath.set(entry.path, entry);
|
|
383
|
+
const prevDir = byDirName.get(entry.dir_name);
|
|
384
|
+
if (!prevDir || (!prevDir.enabled && entry.enabled)) {
|
|
385
|
+
byDirName.set(entry.dir_name, entry);
|
|
386
|
+
}
|
|
387
|
+
const prevName = byName.get(entry.name);
|
|
388
|
+
if (!prevName || (!prevName.enabled && entry.enabled)) {
|
|
389
|
+
byName.set(entry.name, entry);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
return {
|
|
394
|
+
skills,
|
|
395
|
+
index: { byPath, byDirName, byName },
|
|
396
|
+
};
|
|
397
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { canonicalizeEvent, dedupeEvents } from '../core/normalize.mjs';
|
|
2
|
+
|
|
3
|
+
export function parseSince(sinceStr, now = new Date()) {
|
|
4
|
+
if (sinceStr === undefined || sinceStr === null || sinceStr === '' || sinceStr === 'all') {
|
|
5
|
+
return null;
|
|
6
|
+
}
|
|
7
|
+
const s = String(sinceStr).trim();
|
|
8
|
+
if (s === 'all') return null;
|
|
9
|
+
|
|
10
|
+
const rel = s.match(/^(\d+)([hdwm])$/i);
|
|
11
|
+
if (rel) {
|
|
12
|
+
const amount = Number(rel[1]);
|
|
13
|
+
const unit = rel[2].toLowerCase();
|
|
14
|
+
const d = new Date(now.getTime());
|
|
15
|
+
if (unit === 'h') {
|
|
16
|
+
d.setTime(d.getTime() - amount * 3600_000);
|
|
17
|
+
} else if (unit === 'd') {
|
|
18
|
+
d.setTime(d.getTime() - amount * 86400_000);
|
|
19
|
+
} else if (unit === 'w') {
|
|
20
|
+
d.setTime(d.getTime() - amount * 7 * 86400_000);
|
|
21
|
+
} else if (unit === 'm') {
|
|
22
|
+
d.setUTCMonth(d.getUTCMonth() - amount);
|
|
23
|
+
}
|
|
24
|
+
return d;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(s)) {
|
|
28
|
+
const ms = Date.parse(`${s}T00:00:00.000Z`);
|
|
29
|
+
if (Number.isFinite(ms)) return new Date(ms);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const ms = Date.parse(s);
|
|
33
|
+
if (Number.isFinite(ms)) return new Date(ms);
|
|
34
|
+
|
|
35
|
+
throw new Error(`Invalid --since value: "${sinceStr}" (use e.g. 7d, 30d, 90d, 4w, 6m, 2026-01-01, or all)`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function hostMatches(eventHost, filterHost) {
|
|
39
|
+
if (!filterHost || filterHost === 'all') return true;
|
|
40
|
+
if (eventHost === filterHost) return true;
|
|
41
|
+
if (filterHost === 'antigravity' && String(eventHost).startsWith('antigravity')) return true;
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function aggregateStats({
|
|
46
|
+
events = [],
|
|
47
|
+
inventory = null,
|
|
48
|
+
config = {},
|
|
49
|
+
since = '30d',
|
|
50
|
+
host = null,
|
|
51
|
+
project = null,
|
|
52
|
+
bySource = false,
|
|
53
|
+
metric = 'activations',
|
|
54
|
+
now = new Date(),
|
|
55
|
+
} = {}) {
|
|
56
|
+
const sinceDate = since === null ? null : parseSince(since, now);
|
|
57
|
+
const sinceMs = sinceDate ? sinceDate.getTime() : null;
|
|
58
|
+
const aliases = config?.aliases || {};
|
|
59
|
+
const exclSkills = new Set(config?.exclude_skills || []);
|
|
60
|
+
const exclSources = new Set(config?.exclude_sources || []);
|
|
61
|
+
|
|
62
|
+
const canonicalized = events.map((ev) => canonicalizeEvent(ev, inventory?.index, aliases));
|
|
63
|
+
const deduped = dedupeEvents(canonicalized);
|
|
64
|
+
|
|
65
|
+
const filtered = [];
|
|
66
|
+
for (const ev of deduped) {
|
|
67
|
+
if (ev.ok === false) continue;
|
|
68
|
+
if (!hostMatches(ev.host, host)) continue;
|
|
69
|
+
if (project && ev.project !== project) continue;
|
|
70
|
+
if (exclSkills.has(ev.skill) || exclSources.has(ev.source)) continue;
|
|
71
|
+
|
|
72
|
+
if (sinceMs !== null) {
|
|
73
|
+
const tsMs = Date.parse(ev.ts);
|
|
74
|
+
if (!Number.isFinite(tsMs) || tsMs < sinceMs) continue;
|
|
75
|
+
}
|
|
76
|
+
filtered.push(ev);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const hostTotals = {};
|
|
80
|
+
const groups = new Map();
|
|
81
|
+
|
|
82
|
+
for (const ev of filtered) {
|
|
83
|
+
const h = ev.host || 'unknown';
|
|
84
|
+
hostTotals[h] = (hostTotals[h] || 0) + 1;
|
|
85
|
+
|
|
86
|
+
const key = bySource ? `${ev.skill}\0${ev.source || 'unknown'}` : ev.skill;
|
|
87
|
+
let bucket = groups.get(key);
|
|
88
|
+
if (!bucket) {
|
|
89
|
+
bucket = {
|
|
90
|
+
skill: ev.skill,
|
|
91
|
+
source: ev.source || 'unknown',
|
|
92
|
+
sources: new Set([ev.source || 'unknown']),
|
|
93
|
+
reads: 0,
|
|
94
|
+
sessions: new Set(),
|
|
95
|
+
hosts: new Set(),
|
|
96
|
+
last_used: ev.ts || '',
|
|
97
|
+
};
|
|
98
|
+
groups.set(key, bucket);
|
|
99
|
+
} else if (ev.source) {
|
|
100
|
+
bucket.sources.add(ev.source);
|
|
101
|
+
if (bucket.source === 'unknown' && ev.source !== 'unknown') {
|
|
102
|
+
bucket.source = ev.source;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
bucket.reads += 1;
|
|
107
|
+
bucket.sessions.add(ev.session_id || ev.id || `${ev.ts}:${bucket.reads}`);
|
|
108
|
+
bucket.hosts.add(h);
|
|
109
|
+
if (ev.ts && (!bucket.last_used || ev.ts > bucket.last_used)) {
|
|
110
|
+
bucket.last_used = ev.ts;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const rows = [...groups.values()].map((b) => ({
|
|
115
|
+
skill: b.skill,
|
|
116
|
+
source: b.source,
|
|
117
|
+
sources: [...b.sources].sort(),
|
|
118
|
+
activations: b.sessions.size,
|
|
119
|
+
reads: b.reads,
|
|
120
|
+
last_used: b.last_used,
|
|
121
|
+
hosts: [...b.hosts].sort(),
|
|
122
|
+
}));
|
|
123
|
+
|
|
124
|
+
rows.sort((a, b) => {
|
|
125
|
+
if (metric === 'reads') {
|
|
126
|
+
return (
|
|
127
|
+
b.reads - a.reads ||
|
|
128
|
+
b.activations - a.activations ||
|
|
129
|
+
b.last_used.localeCompare(a.last_used) ||
|
|
130
|
+
a.skill.localeCompare(b.skill)
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
return (
|
|
134
|
+
b.activations - a.activations ||
|
|
135
|
+
b.reads - a.reads ||
|
|
136
|
+
b.last_used.localeCompare(a.last_used) ||
|
|
137
|
+
a.skill.localeCompare(b.skill)
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Compute inventory & unused
|
|
142
|
+
const invSkills = (inventory?.skills || []).filter((s) =>
|
|
143
|
+
!host || host === 'all' ? true : Array.isArray(s.hosts) && s.hosts.some((h) => hostMatches(h, host))
|
|
144
|
+
);
|
|
145
|
+
const enabledSkills = invSkills.filter(
|
|
146
|
+
(s) => s.enabled && !exclSkills.has(s.name) && !exclSources.has(s.source)
|
|
147
|
+
);
|
|
148
|
+
const disabledCount = invSkills.filter(
|
|
149
|
+
(s) => !s.enabled && !exclSkills.has(s.name) && !exclSources.has(s.source)
|
|
150
|
+
).length;
|
|
151
|
+
|
|
152
|
+
const usedNames = new Set(rows.map((r) => r.skill));
|
|
153
|
+
const usedBySource = new Set(rows.map((r) => `${r.skill}\0${r.source}`));
|
|
154
|
+
|
|
155
|
+
let usedInstalledCount = 0;
|
|
156
|
+
const unusedSkills = [];
|
|
157
|
+
|
|
158
|
+
for (const s of enabledSkills) {
|
|
159
|
+
const used = bySource
|
|
160
|
+
? usedBySource.has(`${s.name}\0${s.source}`) || usedBySource.has(`${s.dir_name}\0${s.source}`)
|
|
161
|
+
: usedNames.has(s.name) || usedNames.has(s.dir_name);
|
|
162
|
+
if (used) {
|
|
163
|
+
usedInstalledCount += 1;
|
|
164
|
+
} else {
|
|
165
|
+
unusedSkills.push(s);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const unusedGroupMap = new Map();
|
|
170
|
+
let totalDescChars = 0;
|
|
171
|
+
let totalEstTokens = 0;
|
|
172
|
+
|
|
173
|
+
for (const s of unusedSkills) {
|
|
174
|
+
totalDescChars += s.desc_chars || 0;
|
|
175
|
+
totalEstTokens += s.est_tokens || 0;
|
|
176
|
+
let g = unusedGroupMap.get(s.source);
|
|
177
|
+
if (!g) {
|
|
178
|
+
g = { source: s.source, skillCount: 0, descChars: 0, estTokens: 0, skills: [] };
|
|
179
|
+
unusedGroupMap.set(s.source, g);
|
|
180
|
+
}
|
|
181
|
+
g.skillCount += 1;
|
|
182
|
+
g.descChars += s.desc_chars || 0;
|
|
183
|
+
g.estTokens += s.est_tokens || 0;
|
|
184
|
+
g.skills.push(s);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const unusedGroups = [...unusedGroupMap.values()].sort(
|
|
188
|
+
(a, b) => b.estTokens - a.estTokens || b.skillCount - a.skillCount || a.source.localeCompare(b.source)
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
window: {
|
|
193
|
+
since: since ?? 'all',
|
|
194
|
+
from: sinceDate ? sinceDate.toISOString().slice(0, 10) : null,
|
|
195
|
+
to: now.toISOString().slice(0, 10),
|
|
196
|
+
},
|
|
197
|
+
totals: {
|
|
198
|
+
events: filtered.length,
|
|
199
|
+
activations: rows.reduce((acc, r) => acc + r.activations, 0),
|
|
200
|
+
uniqueSkills: rows.length,
|
|
201
|
+
hosts: hostTotals,
|
|
202
|
+
},
|
|
203
|
+
inventory: {
|
|
204
|
+
enabled: enabledSkills.length,
|
|
205
|
+
disabled: disabledCount,
|
|
206
|
+
used: usedInstalledCount,
|
|
207
|
+
unused: unusedSkills.length,
|
|
208
|
+
},
|
|
209
|
+
rows,
|
|
210
|
+
unused: {
|
|
211
|
+
total: unusedSkills.length,
|
|
212
|
+
totalDescChars,
|
|
213
|
+
totalEstTokens,
|
|
214
|
+
bySource: unusedGroups,
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { canonicalizeEvent, dedupeEvents } from '../core/normalize.mjs';
|
|
2
|
+
import { parseSince } from './aggregate.mjs';
|
|
3
|
+
|
|
4
|
+
const VALID_FORMATS = new Set(['csv', 'json']);
|
|
5
|
+
const CSV_HEADERS = [
|
|
6
|
+
'ts',
|
|
7
|
+
'host',
|
|
8
|
+
'skill',
|
|
9
|
+
'source',
|
|
10
|
+
'via',
|
|
11
|
+
'tool',
|
|
12
|
+
'session_id',
|
|
13
|
+
'project',
|
|
14
|
+
'model',
|
|
15
|
+
'origin',
|
|
16
|
+
'ok',
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export function csvEscape(val) {
|
|
20
|
+
if (val === undefined || val === null) return '';
|
|
21
|
+
const str = String(val);
|
|
22
|
+
if (str.includes(',') || str.includes('"') || str.includes('\n') || str.includes('\r')) {
|
|
23
|
+
return `"${str.replace(/"/g, '""')}"`;
|
|
24
|
+
}
|
|
25
|
+
return str;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function hostMatches(eventHost, filterHost) {
|
|
29
|
+
if (!filterHost || filterHost === 'all') return true;
|
|
30
|
+
if (eventHost === filterHost) return true;
|
|
31
|
+
if (filterHost === 'antigravity' && String(eventHost).startsWith('antigravity')) return true;
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function exportEvents({
|
|
36
|
+
events = [],
|
|
37
|
+
inventory = null,
|
|
38
|
+
config = {},
|
|
39
|
+
format = 'json',
|
|
40
|
+
since = 'all',
|
|
41
|
+
host = null,
|
|
42
|
+
project = null,
|
|
43
|
+
includeFailed = false,
|
|
44
|
+
now = new Date(),
|
|
45
|
+
} = {}) {
|
|
46
|
+
const fmt = String(format || 'json').toLowerCase();
|
|
47
|
+
if (!VALID_FORMATS.has(fmt)) {
|
|
48
|
+
throw new Error(`Invalid --format value: "${format}" (expected: csv, json)`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const sinceDate = since === null ? null : parseSince(since, now);
|
|
52
|
+
const sinceMs = sinceDate ? sinceDate.getTime() : null;
|
|
53
|
+
const aliases = config?.aliases || {};
|
|
54
|
+
const exclSkills = new Set(config?.exclude_skills || []);
|
|
55
|
+
const exclSources = new Set(config?.exclude_sources || []);
|
|
56
|
+
|
|
57
|
+
const canonicalized = events.map((ev) => canonicalizeEvent(ev, inventory?.index, aliases));
|
|
58
|
+
const deduped = dedupeEvents(canonicalized);
|
|
59
|
+
|
|
60
|
+
const rows = [];
|
|
61
|
+
for (const ev of deduped) {
|
|
62
|
+
if (!includeFailed && ev.ok === false) continue;
|
|
63
|
+
if (!hostMatches(ev.host, host)) continue;
|
|
64
|
+
if (project && ev.project !== project) continue;
|
|
65
|
+
if (exclSkills.has(ev.skill) || exclSources.has(ev.source)) continue;
|
|
66
|
+
|
|
67
|
+
if (sinceMs !== null) {
|
|
68
|
+
const tsMs = Date.parse(ev.ts);
|
|
69
|
+
if (!Number.isFinite(tsMs) || tsMs < sinceMs) continue;
|
|
70
|
+
}
|
|
71
|
+
rows.push(ev);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
rows.sort((a, b) => String(a.ts || '').localeCompare(String(b.ts || '')));
|
|
75
|
+
|
|
76
|
+
if (fmt === 'json') {
|
|
77
|
+
return {
|
|
78
|
+
format: 'json',
|
|
79
|
+
count: rows.length,
|
|
80
|
+
content: JSON.stringify(rows, null, 2) + '\n',
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const lines = [CSV_HEADERS.join(',')];
|
|
85
|
+
for (const r of rows) {
|
|
86
|
+
lines.push(CSV_HEADERS.map((col) => csvEscape(r[col])).join(','));
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
format: 'csv',
|
|
90
|
+
count: rows.length,
|
|
91
|
+
content: lines.join('\n') + '\n',
|
|
92
|
+
};
|
|
93
|
+
}
|