lakonai 0.7.0 → 0.8.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/README.md +33 -12
- package/bin/lakonai.js +33 -24
- package/package.json +17 -13
- package/src/filters/auto.js +22 -0
- package/src/filters/defs.js +103 -0
- package/src/filters/engine.js +117 -0
- package/src/filters/find.js +18 -0
- package/src/filters/git.js +2 -2
- package/src/filters/index.js +58 -5
- package/src/filters/ls.js +1 -1
- package/src/filters/test.js +92 -0
- package/src/filters/utils.js +53 -1
- package/src/hooks/bash-rewrite.js +10 -5
- package/src/hooks/grep-guard.js +7 -6
- package/src/hooks/read-guard.js +18 -9
- package/src/hooks/session-start.js +6 -2
- package/src/hooks/stop-hook.js +13 -8
- package/src/hooks/throttle.js +2 -3
- package/src/hooks/version-check.js +11 -11
- package/src/install/claude-commands.js +1 -0
- package/src/install/claude-hook.js +8 -7
- package/src/install/index.js +8 -8
- package/src/install/paths.js +9 -1
- package/src/install/platforms.js +3 -3
- package/src/learn.js +157 -0
- package/src/rules/lakonai.md +8 -2
- package/src/tracking.js +3 -3
package/src/filters/index.js
CHANGED
|
@@ -4,6 +4,11 @@ const git = require('./git');
|
|
|
4
4
|
const ls = require('./ls');
|
|
5
5
|
const cat = require('./cat');
|
|
6
6
|
const grep = require('./grep');
|
|
7
|
+
const find = require('./find');
|
|
8
|
+
const test = require('./test');
|
|
9
|
+
const engine = require('./engine');
|
|
10
|
+
const auto = require('./auto');
|
|
11
|
+
const learn = require('../learn');
|
|
7
12
|
const { countTokensApprox } = require('./utils');
|
|
8
13
|
|
|
9
14
|
const HANDLERS = {
|
|
@@ -16,20 +21,68 @@ const HANDLERS = {
|
|
|
16
21
|
grep: (_args, raw) => grep.filter(raw),
|
|
17
22
|
rg: (_args, raw) => grep.filter(raw),
|
|
18
23
|
ag: (_args, raw) => grep.filter(raw),
|
|
24
|
+
find: (_args, raw) => find.filter(raw),
|
|
19
25
|
};
|
|
20
26
|
|
|
27
|
+
// Commands shipped with a hand-written or declarative filter (excludes the
|
|
28
|
+
// auto-learned ones). Used by the learner to skip commands we already cover.
|
|
29
|
+
function builtinFirstWords() {
|
|
30
|
+
const set = new Set(Object.keys(HANDLERS));
|
|
31
|
+
for (const w of test.TEST_FIRST_WORDS) set.add(w);
|
|
32
|
+
for (const w of engine.firstWords()) set.add(w);
|
|
33
|
+
return set;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isBuiltinSupported(cmd) {
|
|
37
|
+
return builtinFirstWords().has(cmd);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Every first-word command the hook should intercept: built-ins + auto-learned.
|
|
41
|
+
function supportedFirstWords() {
|
|
42
|
+
const set = builtinFirstWords();
|
|
43
|
+
for (const w of learn.learnedCommands()) set.add(w);
|
|
44
|
+
return set;
|
|
45
|
+
}
|
|
46
|
+
|
|
21
47
|
function isSupported(cmd) {
|
|
22
|
-
return
|
|
48
|
+
return supportedFirstWords().has(cmd);
|
|
23
49
|
}
|
|
24
50
|
|
|
51
|
+
// Resolve and run the right filter for a command. Routing order:
|
|
52
|
+
// 1. test runner (jest/pytest/go test/cargo test/npm test/…)
|
|
53
|
+
// 2. built-in JS handler keyed by first word (git/ls/cat/grep/…)
|
|
54
|
+
// 3. declarative engine def matched against the full command line
|
|
55
|
+
// 4. auto-learned command -> conservative near-lossless filter
|
|
56
|
+
// 5. passthrough
|
|
25
57
|
function filterCommand(cmd, args, raw) {
|
|
26
|
-
const handler = HANDLERS[cmd];
|
|
27
|
-
if (!handler) return raw;
|
|
28
58
|
try {
|
|
29
|
-
|
|
59
|
+
if (test.isTestCommand(cmd, args)) return test.filter(raw);
|
|
60
|
+
const handler = HANDLERS[cmd];
|
|
61
|
+
if (handler) return handler(args, raw);
|
|
62
|
+
const def = engine.findFilter([cmd, ...args].join(' '));
|
|
63
|
+
if (def) return engine.applyDef(def, raw);
|
|
64
|
+
if (learn.isLearned(cmd)) return auto.filter(raw);
|
|
65
|
+
return raw;
|
|
30
66
|
} catch {
|
|
31
67
|
return raw;
|
|
32
68
|
}
|
|
33
69
|
}
|
|
34
70
|
|
|
35
|
-
|
|
71
|
+
// Whether stderr must be captured and merged with stdout before filtering.
|
|
72
|
+
// Test runners (jest/vitest/pytest) emit results to stderr; some engine defs
|
|
73
|
+
// opt in via filterStderr.
|
|
74
|
+
function needsStderr(cmd, args) {
|
|
75
|
+
if (test.isTestCommand(cmd, args)) return true;
|
|
76
|
+
return engine.filterStderrFor([cmd, ...args].join(' '));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = {
|
|
80
|
+
filterCommand,
|
|
81
|
+
isSupported,
|
|
82
|
+
isBuiltinSupported,
|
|
83
|
+
needsStderr,
|
|
84
|
+
supportedFirstWords,
|
|
85
|
+
builtinFirstWords,
|
|
86
|
+
countTokensApprox,
|
|
87
|
+
HANDLERS,
|
|
88
|
+
};
|
package/src/filters/ls.js
CHANGED
|
@@ -23,7 +23,7 @@ function filterLong(raw) {
|
|
|
23
23
|
|
|
24
24
|
function filter(raw) {
|
|
25
25
|
const text = stripAnsi(raw);
|
|
26
|
-
/*
|
|
26
|
+
/* istanbul ignore next */
|
|
27
27
|
const firstReal = text.split('\n').find((l) => l.trim() && !/^total\s+\d+/.test(l)) || '';
|
|
28
28
|
if (LONG_FORMAT_RE.test(firstReal)) {
|
|
29
29
|
return filterLong(text);
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { stripAnsi, truncateLines } = require('./utils');
|
|
4
|
+
|
|
5
|
+
const DEFAULT_MAX_LINES = 80;
|
|
6
|
+
|
|
7
|
+
// Summary lines worth keeping verbatim across frameworks (jest/vitest/pytest/go/cargo/mocha).
|
|
8
|
+
const SUMMARY_RE = [
|
|
9
|
+
/^Tests?:\s/i, // jest/vitest: Tests: 1 failed, 9 passed
|
|
10
|
+
/^Test Suites:\s/i, // jest
|
|
11
|
+
/\b\d+ (passed|failed|pending|skipped|todo)\b/i, // vitest/mocha counts
|
|
12
|
+
/\bpassing\b|\bfailing\b/i, // mocha
|
|
13
|
+
/^=+ .*\b(passed|failed|error)\b.* =+$/i, // pytest: === 1 failed, 9 passed in 0.4s ===
|
|
14
|
+
/^test result:/i, // cargo: test result: FAILED. 9 passed; 1 failed
|
|
15
|
+
/^(ok|FAIL)\b/i, // go per-package: ok pkg 0.1s / FAIL pkg
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
// Failure markers — keep the line and let surrounding detail through.
|
|
19
|
+
const FAIL_RE =
|
|
20
|
+
/(✕|✖|✗|×|\bFAIL(ED)?\b|\bnot ok\b|--- FAIL:|\bpanic(ked)?\b|AssertionError|\bError\b|\bThrown\b|Expected|Received|\bassert\b)/;
|
|
21
|
+
|
|
22
|
+
// Pass-only noise to drop when there ARE failures (keep signal, cut PASS spam).
|
|
23
|
+
const PASS_NOISE_RE = [
|
|
24
|
+
/^\s*(✓|√|·|✔)\s/, // jest/vitest/mocha passing test line
|
|
25
|
+
/^\s*PASS\b/, // jest: PASS src/foo.test.js
|
|
26
|
+
/^\s*ok\s+\d+\b/, // TAP: ok 1 - desc
|
|
27
|
+
/^--- PASS:/, // go passing subtest
|
|
28
|
+
/^\s*test .+ \.\.\. ok\s*$/, // cargo passing test
|
|
29
|
+
/^\s*(Compiling|Finished|Running|Downloaded|Fresh)\b/, // cargo build noise
|
|
30
|
+
/^\s*\.+\s*\[?\s*\d+%/, // pytest progress dots: ..... [ 80%]
|
|
31
|
+
/^\s*$/, // blank
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
function isAllPass(lines) {
|
|
35
|
+
let sawTest = false;
|
|
36
|
+
for (const line of lines) {
|
|
37
|
+
if (/✓|√|✔|\bPASS\b|--- PASS:|\.\.\. ok|\bpassed\b|\bpassing\b|\bok\b/i.test(line)) sawTest = true;
|
|
38
|
+
if (FAIL_RE.test(line) && !/0 (failed|failing)/i.test(line)) return false;
|
|
39
|
+
}
|
|
40
|
+
return sawTest;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function pickSummary(lines) {
|
|
44
|
+
return lines.filter((l) => SUMMARY_RE.some((re) => re.test(l.trim())));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function filter(raw, opts = {}) {
|
|
48
|
+
const max = opts.maxLines || DEFAULT_MAX_LINES;
|
|
49
|
+
const text = stripAnsi(raw);
|
|
50
|
+
if (!text.trim()) return 'tests: ok';
|
|
51
|
+
const lines = text.split('\n');
|
|
52
|
+
|
|
53
|
+
if (isAllPass(lines)) {
|
|
54
|
+
const summary = pickSummary(lines);
|
|
55
|
+
return summary.length ? summary.join('\n') : 'tests: ok';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Failures present: keep failure lines + summary, drop pass-only noise.
|
|
59
|
+
const out = [];
|
|
60
|
+
for (const line of lines) {
|
|
61
|
+
const trimmed = line.trim();
|
|
62
|
+
if (SUMMARY_RE.some((re) => re.test(trimmed))) {
|
|
63
|
+
out.push(line);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (PASS_NOISE_RE.some((re) => re.test(line))) continue;
|
|
67
|
+
out.push(line);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Collapse runs of blanks left behind.
|
|
71
|
+
const compact = out.join('\n').replace(/\n{3,}/g, '\n\n').trim();
|
|
72
|
+
return truncateLines(compact || 'tests: ok', max);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Direct test-runner binaries (always a test invocation).
|
|
76
|
+
const TEST_BINS = new Set(['jest', 'vitest', 'mocha', 'pytest', 'py.test', 'ava']);
|
|
77
|
+
// Package-manager / toolchain front-ends that run tests via a subcommand.
|
|
78
|
+
const TEST_FIRST_WORDS = new Set([...TEST_BINS, 'npm', 'pnpm', 'yarn', 'bun', 'go', 'cargo']);
|
|
79
|
+
|
|
80
|
+
// Decide whether `<cmd> <args...>` is a test run whose output test.filter handles.
|
|
81
|
+
function isTestCommand(cmd, args = []) {
|
|
82
|
+
if (TEST_BINS.has(cmd)) return true;
|
|
83
|
+
const a = args.filter((x) => !x.startsWith('-'));
|
|
84
|
+
if (cmd === 'go') return a[0] === 'test';
|
|
85
|
+
if (cmd === 'cargo') return a[0] === 'test' || a[0] === 'nextest';
|
|
86
|
+
if (cmd === 'npm' || cmd === 'pnpm' || cmd === 'yarn' || cmd === 'bun') {
|
|
87
|
+
return a[0] === 'test' || a[0] === 't' || (a[0] === 'run' && a[1] === 'test');
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
module.exports = { filter, isAllPass, pickSummary, isTestCommand, TEST_FIRST_WORDS };
|
package/src/filters/utils.js
CHANGED
|
@@ -26,4 +26,56 @@ function countTokensApprox(text) {
|
|
|
26
26
|
return text.split(/\s+/).filter(Boolean).length;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
// Collapse runs of identical adjacent lines into one, annotated with the count:
|
|
30
|
+
// a / a / a / b -> a (×3) / b
|
|
31
|
+
function dedupConsecutive(text) {
|
|
32
|
+
const lines = text.split('\n');
|
|
33
|
+
const out = [];
|
|
34
|
+
let prev = null;
|
|
35
|
+
let count = 0;
|
|
36
|
+
const flush = () => {
|
|
37
|
+
if (prev === null) return;
|
|
38
|
+
out.push(count > 1 ? `${prev} (×${count})` : prev);
|
|
39
|
+
};
|
|
40
|
+
for (const line of lines) {
|
|
41
|
+
if (line === prev) {
|
|
42
|
+
count++;
|
|
43
|
+
} else {
|
|
44
|
+
flush();
|
|
45
|
+
prev = line;
|
|
46
|
+
count = 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
flush();
|
|
50
|
+
return out.join('\n');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Group file-like lines by their directory, emitting a header per dir followed
|
|
54
|
+
// by the basenames. Lines without a slash are grouped under '.'.
|
|
55
|
+
function groupByDir(lines) {
|
|
56
|
+
const groups = new Map();
|
|
57
|
+
for (const line of lines) {
|
|
58
|
+
const trimmed = line.trim();
|
|
59
|
+
if (!trimmed) continue;
|
|
60
|
+
const slash = trimmed.lastIndexOf('/');
|
|
61
|
+
const dir = slash === -1 ? '.' : trimmed.slice(0, slash);
|
|
62
|
+
const base = slash === -1 ? trimmed : trimmed.slice(slash + 1);
|
|
63
|
+
if (!groups.has(dir)) groups.set(dir, []);
|
|
64
|
+
groups.get(dir).push(base);
|
|
65
|
+
}
|
|
66
|
+
const out = [];
|
|
67
|
+
for (const [dir, names] of groups) {
|
|
68
|
+
out.push(`${dir}/ (${names.length})`);
|
|
69
|
+
for (const n of names) out.push(` ${n}`);
|
|
70
|
+
}
|
|
71
|
+
return out.join('\n');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
module.exports = {
|
|
75
|
+
stripAnsi,
|
|
76
|
+
truncateLines,
|
|
77
|
+
truncateBytes,
|
|
78
|
+
countTokensApprox,
|
|
79
|
+
dedupConsecutive,
|
|
80
|
+
groupByDir,
|
|
81
|
+
};
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const { supportedFirstWords } = require('../filters');
|
|
5
|
+
|
|
6
|
+
const FILTERED_CMDS = supportedFirstWords();
|
|
7
7
|
|
|
8
8
|
function rewriteIfNeeded(command) {
|
|
9
9
|
if (typeof command !== 'string') return null;
|
|
10
10
|
const trimmed = command.trim();
|
|
11
11
|
if (!trimmed) return null;
|
|
12
|
-
if (/^
|
|
12
|
+
if (/^lakonai(\s|$)/.test(trimmed)) return null;
|
|
13
13
|
const firstWord = trimmed.split(/\s+/)[0];
|
|
14
14
|
if (!FILTERED_CMDS.has(firstWord)) return null;
|
|
15
15
|
return `lakonai ${trimmed}`;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
/* istanbul ignore next */
|
|
18
19
|
async function readStdin() {
|
|
19
20
|
let raw = '';
|
|
20
21
|
process.stdin.setEncoding('utf8');
|
|
@@ -22,6 +23,7 @@ async function readStdin() {
|
|
|
22
23
|
return raw;
|
|
23
24
|
}
|
|
24
25
|
|
|
26
|
+
/* istanbul ignore next */
|
|
25
27
|
async function main() {
|
|
26
28
|
try {
|
|
27
29
|
const raw = await readStdin();
|
|
@@ -51,4 +53,7 @@ async function main() {
|
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
/* istanbul ignore next */
|
|
57
|
+
if (require.main === module) main();
|
|
58
|
+
|
|
59
|
+
module.exports = { rewriteIfNeeded, FILTERED_CMDS };
|
package/src/hooks/grep-guard.js
CHANGED
|
@@ -9,11 +9,11 @@ const { shouldEmit } = require('./throttle');
|
|
|
9
9
|
const DEFAULT_HEAD_LIMIT = 30;
|
|
10
10
|
|
|
11
11
|
function lakonHome() {
|
|
12
|
-
/*
|
|
12
|
+
/* istanbul ignore next */
|
|
13
13
|
return process.env.LAKON_HOME || path.join(os.homedir(), '.lakon');
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
/*
|
|
16
|
+
/* istanbul ignore next */
|
|
17
17
|
function trackRecord({ cmd, args, rawTokens, filteredTokens }) {
|
|
18
18
|
if (process.env.LAKON_NO_TRACK === '1') return;
|
|
19
19
|
try {
|
|
@@ -32,8 +32,8 @@ function trackRecord({ cmd, args, rawTokens, filteredTokens }) {
|
|
|
32
32
|
// never let tracking break the hook
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
-
/* c8 ignore stop */
|
|
36
35
|
|
|
36
|
+
/* istanbul ignore next */
|
|
37
37
|
async function readStdin() {
|
|
38
38
|
let raw = '';
|
|
39
39
|
process.stdin.setEncoding('utf8');
|
|
@@ -41,7 +41,7 @@ async function readStdin() {
|
|
|
41
41
|
return raw;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
/*
|
|
44
|
+
/* istanbul ignore next */
|
|
45
45
|
async function main() {
|
|
46
46
|
try {
|
|
47
47
|
const raw = await readStdin();
|
|
@@ -78,6 +78,7 @@ async function main() {
|
|
|
78
78
|
process.exit(0);
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
-
/*
|
|
81
|
+
/* istanbul ignore next */
|
|
82
|
+
if (require.main === module) main();
|
|
82
83
|
|
|
83
|
-
|
|
84
|
+
module.exports = { trackRecord, lakonHome, DEFAULT_HEAD_LIMIT };
|
package/src/hooks/read-guard.js
CHANGED
|
@@ -6,11 +6,11 @@ const path = require('path');
|
|
|
6
6
|
const os = require('os');
|
|
7
7
|
|
|
8
8
|
function lakonHome() {
|
|
9
|
-
/*
|
|
9
|
+
/* istanbul ignore next */
|
|
10
10
|
return process.env.LAKON_HOME || path.join(os.homedir(), '.lakon');
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
/*
|
|
13
|
+
/* istanbul ignore next */
|
|
14
14
|
function trackRecord({ cmd, args, rawTokens, filteredTokens }) {
|
|
15
15
|
if (process.env.LAKON_NO_TRACK === '1') return;
|
|
16
16
|
try {
|
|
@@ -29,7 +29,6 @@ function trackRecord({ cmd, args, rawTokens, filteredTokens }) {
|
|
|
29
29
|
// never let tracking break the hook
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
/* c8 ignore stop */
|
|
33
32
|
|
|
34
33
|
const DENY_DIRS = [
|
|
35
34
|
'node_modules',
|
|
@@ -69,7 +68,7 @@ const DENY_FILE_RE = /(^|\/)(package-lock\.json|pnpm-lock\.yaml|yarn\.lock|bun\.
|
|
|
69
68
|
const AUTO_CAP_LINES = 800;
|
|
70
69
|
|
|
71
70
|
function isDeniedPath(p) {
|
|
72
|
-
/*
|
|
71
|
+
/* istanbul ignore next */
|
|
73
72
|
if (typeof p !== 'string' || !p) return null;
|
|
74
73
|
const norm = p.replace(/\\/g, '/');
|
|
75
74
|
for (const dir of DENY_DIRS) {
|
|
@@ -90,7 +89,7 @@ function fileLineCount(p) {
|
|
|
90
89
|
for (let i = 0; i < data.length; i++) if (data.charCodeAt(i) === 10) n++;
|
|
91
90
|
if (data.length && data.charCodeAt(data.length - 1) !== 10) n++;
|
|
92
91
|
return n;
|
|
93
|
-
/*
|
|
92
|
+
/* istanbul ignore next */
|
|
94
93
|
} catch {
|
|
95
94
|
return null;
|
|
96
95
|
}
|
|
@@ -105,6 +104,7 @@ function estimateTokensByBytes(p) {
|
|
|
105
104
|
}
|
|
106
105
|
}
|
|
107
106
|
|
|
107
|
+
/* istanbul ignore next */
|
|
108
108
|
async function readStdin() {
|
|
109
109
|
let raw = '';
|
|
110
110
|
process.stdin.setEncoding('utf8');
|
|
@@ -112,7 +112,7 @@ async function readStdin() {
|
|
|
112
112
|
return raw;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
/*
|
|
115
|
+
/* istanbul ignore next */
|
|
116
116
|
async function main() {
|
|
117
117
|
try {
|
|
118
118
|
const raw = await readStdin();
|
|
@@ -178,6 +178,15 @@ async function main() {
|
|
|
178
178
|
process.exit(0);
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
|
-
/*
|
|
182
|
-
|
|
183
|
-
|
|
181
|
+
/* istanbul ignore next */
|
|
182
|
+
if (require.main === module) main();
|
|
183
|
+
|
|
184
|
+
module.exports = {
|
|
185
|
+
isDeniedPath,
|
|
186
|
+
fileLineCount,
|
|
187
|
+
estimateTokensByBytes,
|
|
188
|
+
lakonHome,
|
|
189
|
+
trackRecord,
|
|
190
|
+
DENY_DIRS,
|
|
191
|
+
AUTO_CAP_LINES,
|
|
192
|
+
};
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
/* istanbul ignore file -- thin SessionStart I/O shell: stdin + version check */
|
|
2
3
|
'use strict';
|
|
3
4
|
|
|
4
5
|
const { checkForUpdate, formatNotice } = require('./version-check');
|
|
5
6
|
|
|
7
|
+
/* istanbul ignore next */
|
|
6
8
|
async function readStdin() {
|
|
7
9
|
let raw = '';
|
|
8
10
|
process.stdin.setEncoding('utf8');
|
|
@@ -10,6 +12,7 @@ async function readStdin() {
|
|
|
10
12
|
return raw;
|
|
11
13
|
}
|
|
12
14
|
|
|
15
|
+
/* istanbul ignore next */
|
|
13
16
|
async function main() {
|
|
14
17
|
try {
|
|
15
18
|
await readStdin();
|
|
@@ -24,10 +27,11 @@ async function main() {
|
|
|
24
27
|
};
|
|
25
28
|
process.stdout.write(JSON.stringify(response));
|
|
26
29
|
process.exit(0);
|
|
27
|
-
/*
|
|
30
|
+
/* istanbul ignore next */
|
|
28
31
|
} catch {
|
|
29
32
|
process.exit(0);
|
|
30
33
|
}
|
|
31
34
|
}
|
|
32
35
|
|
|
33
|
-
|
|
36
|
+
/* istanbul ignore next */
|
|
37
|
+
if (require.main === module) main();
|
package/src/hooks/stop-hook.js
CHANGED
|
@@ -6,11 +6,11 @@ const path = require('path');
|
|
|
6
6
|
const os = require('os');
|
|
7
7
|
|
|
8
8
|
function lakonHome() {
|
|
9
|
-
/*
|
|
9
|
+
/* istanbul ignore next */
|
|
10
10
|
return process.env.LAKON_HOME || path.join(os.homedir(), '.lakon');
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
/*
|
|
13
|
+
/* istanbul ignore next */
|
|
14
14
|
function trackSession(payload) {
|
|
15
15
|
if (process.env.LAKON_NO_TRACK === '1') return;
|
|
16
16
|
try {
|
|
@@ -22,8 +22,8 @@ function trackSession(payload) {
|
|
|
22
22
|
// never let tracking break the hook
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
/* c8 ignore stop */
|
|
26
25
|
|
|
26
|
+
/* istanbul ignore next */
|
|
27
27
|
async function readStdin() {
|
|
28
28
|
let raw = '';
|
|
29
29
|
process.stdin.setEncoding('utf8');
|
|
@@ -41,7 +41,6 @@ function extractUsage(transcriptPath) {
|
|
|
41
41
|
const msg = obj.message;
|
|
42
42
|
if (msg && msg.role === 'assistant' && msg.usage) {
|
|
43
43
|
return {
|
|
44
|
-
/* c8 ignore next 2 */
|
|
45
44
|
in_tokens: msg.usage.input_tokens || 0,
|
|
46
45
|
out_tokens: msg.usage.output_tokens || 0,
|
|
47
46
|
cache_read: msg.usage.cache_read_input_tokens || 0,
|
|
@@ -52,14 +51,14 @@ function extractUsage(transcriptPath) {
|
|
|
52
51
|
// skip malformed lines
|
|
53
52
|
}
|
|
54
53
|
}
|
|
55
|
-
/*
|
|
54
|
+
/* istanbul ignore next */
|
|
56
55
|
} catch {
|
|
57
56
|
return null;
|
|
58
57
|
}
|
|
59
58
|
return null;
|
|
60
59
|
}
|
|
61
60
|
|
|
62
|
-
/*
|
|
61
|
+
/* istanbul ignore next */
|
|
63
62
|
async function main() {
|
|
64
63
|
try {
|
|
65
64
|
const raw = await readStdin();
|
|
@@ -67,6 +66,11 @@ async function main() {
|
|
|
67
66
|
const data = JSON.parse(raw);
|
|
68
67
|
if (!data.transcript_path) process.exit(0);
|
|
69
68
|
|
|
69
|
+
// Learn which unfiltered commands are worth auto-filtering (best effort).
|
|
70
|
+
const learn = require('../learn');
|
|
71
|
+
const { isBuiltinSupported } = require('../filters');
|
|
72
|
+
learn.analyzeTranscript(data.transcript_path, isBuiltinSupported);
|
|
73
|
+
|
|
70
74
|
const usage = extractUsage(data.transcript_path);
|
|
71
75
|
if (!usage) process.exit(0);
|
|
72
76
|
|
|
@@ -79,6 +83,7 @@ async function main() {
|
|
|
79
83
|
process.exit(0);
|
|
80
84
|
}
|
|
81
85
|
}
|
|
82
|
-
/*
|
|
86
|
+
/* istanbul ignore next */
|
|
87
|
+
if (require.main === module) main();
|
|
83
88
|
|
|
84
|
-
|
|
89
|
+
module.exports = { extractUsage, trackSession, lakonHome };
|
package/src/hooks/throttle.js
CHANGED
|
@@ -4,11 +4,10 @@ const fs = require('fs');
|
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const os = require('os');
|
|
6
6
|
|
|
7
|
-
/*
|
|
7
|
+
/* istanbul ignore next */
|
|
8
8
|
const MARKER_DIR = path.join(os.tmpdir(), `lakon-${process.env.USER || 'session'}`);
|
|
9
9
|
const TTL_MS = 4 * 60 * 60 * 1000;
|
|
10
10
|
|
|
11
|
-
/* c8 ignore start */
|
|
12
11
|
function shouldEmit(category) {
|
|
13
12
|
if (process.env.LAKON_NO_THROTTLE === '1') return true;
|
|
14
13
|
try {
|
|
@@ -24,9 +23,9 @@ function shouldEmit(category) {
|
|
|
24
23
|
fs.closeSync(fd);
|
|
25
24
|
return true;
|
|
26
25
|
} catch {
|
|
26
|
+
/* istanbul ignore next */
|
|
27
27
|
return true;
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
/* c8 ignore stop */
|
|
31
30
|
|
|
32
31
|
module.exports = { shouldEmit };
|
|
@@ -11,7 +11,7 @@ const FETCH_TIMEOUT_MS = 1500;
|
|
|
11
11
|
const REGISTRY_URL = 'https://registry.npmjs.org/lakonai/latest';
|
|
12
12
|
|
|
13
13
|
function lakonHome() {
|
|
14
|
-
/*
|
|
14
|
+
/* istanbul ignore next */
|
|
15
15
|
return process.env.LAKON_HOME || path.join(os.homedir(), '.lakon');
|
|
16
16
|
}
|
|
17
17
|
|
|
@@ -31,7 +31,7 @@ function writeCache(data) {
|
|
|
31
31
|
try {
|
|
32
32
|
fs.mkdirSync(lakonHome(), { recursive: true });
|
|
33
33
|
fs.writeFileSync(cachePath(), JSON.stringify(data));
|
|
34
|
-
/*
|
|
34
|
+
/* istanbul ignore next */
|
|
35
35
|
} catch {
|
|
36
36
|
// never let cache write break anything
|
|
37
37
|
}
|
|
@@ -47,9 +47,9 @@ function currentVersion() {
|
|
|
47
47
|
} catch {
|
|
48
48
|
// ignore — fall through to package.json lookup
|
|
49
49
|
}
|
|
50
|
+
/* istanbul ignore next */
|
|
50
51
|
try {
|
|
51
52
|
return require('../../package.json').version;
|
|
52
|
-
/* c8 ignore next 3 */
|
|
53
53
|
} catch {
|
|
54
54
|
return null;
|
|
55
55
|
}
|
|
@@ -59,7 +59,7 @@ function writeInstalledVersionMarker(version) {
|
|
|
59
59
|
try {
|
|
60
60
|
fs.mkdirSync(lakonHome(), { recursive: true });
|
|
61
61
|
fs.writeFileSync(installedVersionMarkerPath(), JSON.stringify({ version }));
|
|
62
|
-
/*
|
|
62
|
+
/* istanbul ignore next */
|
|
63
63
|
} catch {
|
|
64
64
|
// never let marker write break install
|
|
65
65
|
}
|
|
@@ -76,7 +76,7 @@ function semverCmp(a, b) {
|
|
|
76
76
|
return 0;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
/*
|
|
79
|
+
/* istanbul ignore next */
|
|
80
80
|
function fetchLatest(url = process.env.LAKON_REGISTRY_URL || REGISTRY_URL, timeout = FETCH_TIMEOUT_MS) {
|
|
81
81
|
return new Promise((resolve) => {
|
|
82
82
|
let settled = false;
|
|
@@ -87,7 +87,7 @@ function fetchLatest(url = process.env.LAKON_REGISTRY_URL || REGISTRY_URL, timeo
|
|
|
87
87
|
}
|
|
88
88
|
};
|
|
89
89
|
try {
|
|
90
|
-
/*
|
|
90
|
+
/* istanbul ignore next */
|
|
91
91
|
const client = url.startsWith('http://') ? http : https;
|
|
92
92
|
const req = client.get(url, { timeout }, (res) => {
|
|
93
93
|
if (res.statusCode !== 200) {
|
|
@@ -99,7 +99,7 @@ function fetchLatest(url = process.env.LAKON_REGISTRY_URL || REGISTRY_URL, timeo
|
|
|
99
99
|
res.on('data', (c) => (body += c));
|
|
100
100
|
res.on('end', () => {
|
|
101
101
|
try {
|
|
102
|
-
/*
|
|
102
|
+
/* istanbul ignore next */
|
|
103
103
|
finish(JSON.parse(body).version || null);
|
|
104
104
|
} catch {
|
|
105
105
|
finish(null);
|
|
@@ -108,10 +108,10 @@ function fetchLatest(url = process.env.LAKON_REGISTRY_URL || REGISTRY_URL, timeo
|
|
|
108
108
|
});
|
|
109
109
|
req.on('error', () => finish(null));
|
|
110
110
|
req.on('timeout', () => {
|
|
111
|
-
try { req.destroy(); /*
|
|
111
|
+
try { req.destroy(); /* istanbul ignore next */ } catch {}
|
|
112
112
|
finish(null);
|
|
113
113
|
});
|
|
114
|
-
/*
|
|
114
|
+
/* istanbul ignore next */
|
|
115
115
|
} catch {
|
|
116
116
|
finish(null);
|
|
117
117
|
}
|
|
@@ -125,7 +125,7 @@ function isDisabled() {
|
|
|
125
125
|
async function checkForUpdate({ force = false } = {}) {
|
|
126
126
|
if (isDisabled()) return null;
|
|
127
127
|
const current = currentVersion();
|
|
128
|
-
/*
|
|
128
|
+
/* istanbul ignore next */
|
|
129
129
|
if (!current) return null;
|
|
130
130
|
|
|
131
131
|
const cache = readCache();
|
|
@@ -150,7 +150,7 @@ async function checkForUpdate({ force = false } = {}) {
|
|
|
150
150
|
function getCachedUpdate() {
|
|
151
151
|
if (isDisabled()) return null;
|
|
152
152
|
const current = currentVersion();
|
|
153
|
-
/*
|
|
153
|
+
/* istanbul ignore next */
|
|
154
154
|
if (!current) return null;
|
|
155
155
|
const cache = readCache();
|
|
156
156
|
if (!cache || !cache.latest) return null;
|