@hasna/terminal 0.1.4 → 0.2.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/scheduled_tasks.lock +1 -1
- package/README.md +186 -0
- package/dist/App.js +217 -105
- package/dist/Browse.js +79 -0
- package/dist/FuzzyPicker.js +47 -0
- package/dist/StatusBar.js +20 -16
- package/dist/ai.js +45 -50
- package/dist/cli.js +138 -6
- package/dist/compression.js +107 -0
- package/dist/compression.test.js +42 -0
- package/dist/diff-cache.js +87 -0
- package/dist/diff-cache.test.js +27 -0
- package/dist/economy.js +79 -0
- package/dist/economy.test.js +13 -0
- package/dist/mcp/install.js +98 -0
- package/dist/mcp/server.js +333 -0
- package/dist/output-router.js +41 -0
- package/dist/parsers/base.js +2 -0
- package/dist/parsers/build.js +64 -0
- package/dist/parsers/errors.js +101 -0
- package/dist/parsers/files.js +78 -0
- package/dist/parsers/git.js +86 -0
- package/dist/parsers/index.js +48 -0
- package/dist/parsers/parsers.test.js +136 -0
- package/dist/parsers/tests.js +89 -0
- package/dist/providers/anthropic.js +39 -0
- package/dist/providers/base.js +4 -0
- package/dist/providers/cerebras.js +95 -0
- package/dist/providers/index.js +49 -0
- package/dist/providers/providers.test.js +14 -0
- package/dist/recipes/model.js +20 -0
- package/dist/recipes/recipes.test.js +36 -0
- package/dist/recipes/storage.js +118 -0
- package/dist/search/content-search.js +61 -0
- package/dist/search/file-search.js +61 -0
- package/dist/search/filters.js +34 -0
- package/dist/search/index.js +4 -0
- package/dist/search/search.test.js +22 -0
- package/dist/snapshots.js +51 -0
- package/dist/supervisor.js +112 -0
- package/dist/tree.js +94 -0
- package/package.json +7 -4
- package/src/App.tsx +371 -245
- package/src/Browse.tsx +103 -0
- package/src/FuzzyPicker.tsx +69 -0
- package/src/StatusBar.tsx +28 -34
- package/src/ai.ts +63 -51
- package/src/cli.tsx +132 -6
- package/src/compression.test.ts +50 -0
- package/src/compression.ts +140 -0
- package/src/diff-cache.test.ts +30 -0
- package/src/diff-cache.ts +125 -0
- package/src/economy.test.ts +16 -0
- package/src/economy.ts +99 -0
- package/src/mcp/install.ts +94 -0
- package/src/mcp/server.ts +476 -0
- package/src/output-router.ts +56 -0
- package/src/parsers/base.ts +72 -0
- package/src/parsers/build.ts +73 -0
- package/src/parsers/errors.ts +107 -0
- package/src/parsers/files.ts +91 -0
- package/src/parsers/git.ts +86 -0
- package/src/parsers/index.ts +66 -0
- package/src/parsers/parsers.test.ts +153 -0
- package/src/parsers/tests.ts +98 -0
- package/src/providers/anthropic.ts +44 -0
- package/src/providers/base.ts +34 -0
- package/src/providers/cerebras.ts +108 -0
- package/src/providers/index.ts +60 -0
- package/src/providers/providers.test.ts +16 -0
- package/src/recipes/model.ts +55 -0
- package/src/recipes/recipes.test.ts +44 -0
- package/src/recipes/storage.ts +142 -0
- package/src/search/content-search.ts +97 -0
- package/src/search/file-search.ts +86 -0
- package/src/search/filters.ts +36 -0
- package/src/search/index.ts +7 -0
- package/src/search/search.test.ts +25 -0
- package/src/snapshots.ts +67 -0
- package/src/supervisor.ts +129 -0
- package/src/tree.ts +101 -0
- package/tsconfig.json +2 -1
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Output intelligence router — auto-detect command type and optimize output
|
|
2
|
+
import { parseOutput, estimateTokens } from "./parsers/index.js";
|
|
3
|
+
import { compress, stripAnsi } from "./compression.js";
|
|
4
|
+
import { recordSaving } from "./economy.js";
|
|
5
|
+
/** Route command output through the best optimization path */
|
|
6
|
+
export function routeOutput(command, output, maxTokens) {
|
|
7
|
+
const clean = stripAnsi(output);
|
|
8
|
+
const rawTokens = estimateTokens(clean);
|
|
9
|
+
// Try structured parsing first
|
|
10
|
+
const parsed = parseOutput(command, clean);
|
|
11
|
+
if (parsed) {
|
|
12
|
+
const json = JSON.stringify(parsed.data);
|
|
13
|
+
const jsonTokens = estimateTokens(json);
|
|
14
|
+
const saved = rawTokens - jsonTokens;
|
|
15
|
+
if (saved > 0) {
|
|
16
|
+
recordSaving("structured", saved);
|
|
17
|
+
return {
|
|
18
|
+
raw: clean,
|
|
19
|
+
structured: parsed.data,
|
|
20
|
+
parser: parsed.parser,
|
|
21
|
+
tokensSaved: saved,
|
|
22
|
+
format: "json",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
// Try compression if structured didn't save enough
|
|
27
|
+
if (maxTokens || rawTokens > 200) {
|
|
28
|
+
const compressed = compress(command, clean, { maxTokens, format: "text" });
|
|
29
|
+
if (compressed.tokensSaved > 0) {
|
|
30
|
+
recordSaving("compressed", compressed.tokensSaved);
|
|
31
|
+
return {
|
|
32
|
+
raw: clean,
|
|
33
|
+
compressed: compressed.content,
|
|
34
|
+
tokensSaved: compressed.tokensSaved,
|
|
35
|
+
format: "compressed",
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// Return raw if no optimization helps
|
|
40
|
+
return { raw: clean, tokensSaved: 0, format: "raw" };
|
|
41
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Parser for build output (npm/bun/pnpm build, tsc, webpack, vite, etc.)
|
|
2
|
+
export const buildParser = {
|
|
3
|
+
name: "build",
|
|
4
|
+
detect(command, output) {
|
|
5
|
+
if (/\b(npm|bun|pnpm|yarn)\s+(run\s+)?build\b/.test(command))
|
|
6
|
+
return true;
|
|
7
|
+
if (/\btsc\b/.test(command))
|
|
8
|
+
return true;
|
|
9
|
+
if (/\b(webpack|vite|esbuild|rollup|turbo)\b/.test(command))
|
|
10
|
+
return true;
|
|
11
|
+
return /\b(compiled|bundled|built)\b/i.test(output) && /\b(success|error|warning)\b/i.test(output);
|
|
12
|
+
},
|
|
13
|
+
parse(_command, output) {
|
|
14
|
+
const lines = output.split("\n");
|
|
15
|
+
let warnings = 0, errors = 0, duration;
|
|
16
|
+
// Count warnings and errors
|
|
17
|
+
for (const line of lines) {
|
|
18
|
+
if (/\bwarning\b/i.test(line))
|
|
19
|
+
warnings++;
|
|
20
|
+
if (/\berror\b/i.test(line) && !/0 errors/.test(line))
|
|
21
|
+
errors++;
|
|
22
|
+
}
|
|
23
|
+
// Specific patterns
|
|
24
|
+
const tscErrors = output.match(/Found (\d+) error/);
|
|
25
|
+
if (tscErrors)
|
|
26
|
+
errors = parseInt(tscErrors[1]);
|
|
27
|
+
const warningCount = output.match(/(\d+)\s+warning/);
|
|
28
|
+
if (warningCount)
|
|
29
|
+
warnings = parseInt(warningCount[1]);
|
|
30
|
+
// Duration
|
|
31
|
+
const timeMatch = output.match(/(?:in|took)\s+([\d.]+\s*(?:s|ms|m))/i) ||
|
|
32
|
+
output.match(/Done in ([\d.]+s)/);
|
|
33
|
+
if (timeMatch)
|
|
34
|
+
duration = timeMatch[1];
|
|
35
|
+
const status = errors > 0 ? "failure" : "success";
|
|
36
|
+
return { status, warnings, errors, duration };
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
export const npmInstallParser = {
|
|
40
|
+
name: "npm-install",
|
|
41
|
+
detect(command, _output) {
|
|
42
|
+
return /\b(npm|bun|pnpm|yarn)\s+(install|add|i)\b/.test(command);
|
|
43
|
+
},
|
|
44
|
+
parse(_command, output) {
|
|
45
|
+
let installed = 0, vulnerabilities = 0, duration;
|
|
46
|
+
// npm: added 47 packages in 3.2s
|
|
47
|
+
const npmMatch = output.match(/added\s+(\d+)\s+packages?\s+in\s+([\d.]+s)/);
|
|
48
|
+
if (npmMatch) {
|
|
49
|
+
installed = parseInt(npmMatch[1]);
|
|
50
|
+
duration = npmMatch[2];
|
|
51
|
+
}
|
|
52
|
+
// bun: 47 packages installed [1.2s]
|
|
53
|
+
const bunMatch = output.match(/(\d+)\s+packages?\s+installed.*?\[([\d.]+[ms]*s)\]/);
|
|
54
|
+
if (!npmMatch && bunMatch) {
|
|
55
|
+
installed = parseInt(bunMatch[1]);
|
|
56
|
+
duration = bunMatch[2];
|
|
57
|
+
}
|
|
58
|
+
// Vulnerabilities
|
|
59
|
+
const vulnMatch = output.match(/(\d+)\s+vulnerabilit/);
|
|
60
|
+
if (vulnMatch)
|
|
61
|
+
vulnerabilities = parseInt(vulnMatch[1]);
|
|
62
|
+
return { installed, vulnerabilities, duration };
|
|
63
|
+
},
|
|
64
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Parser for common error patterns
|
|
2
|
+
const ERROR_PATTERNS = [
|
|
3
|
+
{
|
|
4
|
+
type: "port_in_use",
|
|
5
|
+
pattern: /EADDRINUSE.*?(?::(\d+))|port\s+(\d+)\s+(?:is\s+)?(?:already\s+)?in\s+use/i,
|
|
6
|
+
extract: (m) => ({
|
|
7
|
+
type: "port_in_use",
|
|
8
|
+
message: m[0],
|
|
9
|
+
suggestion: `Kill the process: lsof -i :${m[1] ?? m[2]} -t | xargs kill`,
|
|
10
|
+
}),
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
type: "file_not_found",
|
|
14
|
+
pattern: /ENOENT.*?'([^']+)'|No such file or directory:\s*(.+)/,
|
|
15
|
+
extract: (m) => ({
|
|
16
|
+
type: "file_not_found",
|
|
17
|
+
message: m[0],
|
|
18
|
+
file: m[1] ?? m[2]?.trim(),
|
|
19
|
+
suggestion: "Check the file path exists",
|
|
20
|
+
}),
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
type: "permission_denied",
|
|
24
|
+
pattern: /EACCES.*?'([^']+)'|Permission denied:\s*(.+)/,
|
|
25
|
+
extract: (m) => ({
|
|
26
|
+
type: "permission_denied",
|
|
27
|
+
message: m[0],
|
|
28
|
+
file: m[1] ?? m[2]?.trim(),
|
|
29
|
+
suggestion: "Check file permissions or run with sudo",
|
|
30
|
+
}),
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
type: "command_not_found",
|
|
34
|
+
pattern: /command not found:\s*(\S+)|(\S+):\s*not found/,
|
|
35
|
+
extract: (m) => ({
|
|
36
|
+
type: "command_not_found",
|
|
37
|
+
message: m[0],
|
|
38
|
+
suggestion: `Install ${m[1] ?? m[2]} or check your PATH`,
|
|
39
|
+
}),
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
type: "dependency_missing",
|
|
43
|
+
pattern: /Cannot find module\s+'([^']+)'|Module not found.*?'([^']+)'/,
|
|
44
|
+
extract: (m) => ({
|
|
45
|
+
type: "dependency_missing",
|
|
46
|
+
message: m[0],
|
|
47
|
+
suggestion: `Install: npm install ${m[1] ?? m[2]}`,
|
|
48
|
+
}),
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: "syntax_error",
|
|
52
|
+
pattern: /SyntaxError:\s*(.+)|error TS\d+:\s*(.+)/,
|
|
53
|
+
extract: (m, output) => {
|
|
54
|
+
const fileMatch = output.match(/(\S+\.\w+):(\d+)/);
|
|
55
|
+
return {
|
|
56
|
+
type: "syntax_error",
|
|
57
|
+
message: m[1] ?? m[2] ?? m[0],
|
|
58
|
+
file: fileMatch?.[1],
|
|
59
|
+
line: fileMatch ? parseInt(fileMatch[2]) : undefined,
|
|
60
|
+
suggestion: "Fix the syntax error in the referenced file",
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
type: "out_of_memory",
|
|
66
|
+
pattern: /ENOMEM|JavaScript heap out of memory|Killed/,
|
|
67
|
+
extract: (m) => ({
|
|
68
|
+
type: "out_of_memory",
|
|
69
|
+
message: m[0],
|
|
70
|
+
suggestion: "Increase memory: NODE_OPTIONS=--max-old-space-size=4096",
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
type: "network_error",
|
|
75
|
+
pattern: /ECONNREFUSED|ENOTFOUND|ETIMEDOUT|fetch failed/,
|
|
76
|
+
extract: (m) => ({
|
|
77
|
+
type: "network_error",
|
|
78
|
+
message: m[0],
|
|
79
|
+
suggestion: "Check network connection and target URL/host",
|
|
80
|
+
}),
|
|
81
|
+
},
|
|
82
|
+
];
|
|
83
|
+
export const errorParser = {
|
|
84
|
+
name: "error",
|
|
85
|
+
detect(_command, output) {
|
|
86
|
+
return ERROR_PATTERNS.some(({ pattern }) => pattern.test(output));
|
|
87
|
+
},
|
|
88
|
+
parse(_command, output) {
|
|
89
|
+
for (const { pattern, extract } of ERROR_PATTERNS) {
|
|
90
|
+
const match = output.match(pattern);
|
|
91
|
+
if (match)
|
|
92
|
+
return extract(match, output);
|
|
93
|
+
}
|
|
94
|
+
// Generic error fallback
|
|
95
|
+
const errorLine = output.split("\n").find(l => /error/i.test(l));
|
|
96
|
+
return {
|
|
97
|
+
type: "unknown",
|
|
98
|
+
message: errorLine?.trim() ?? "Unknown error",
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Parser for file listing output (ls -la, find, etc.)
|
|
2
|
+
const NODE_MODULES_RE = /node_modules/;
|
|
3
|
+
const DIST_RE = /\b(dist|build|\.next|__pycache__|coverage|\.git)\b/;
|
|
4
|
+
const SOURCE_EXTS = /\.(ts|tsx|js|jsx|py|go|rs|java|rb|sh|c|cpp|h|css|scss|html|vue|svelte|md|json|yaml|yml|toml)$/;
|
|
5
|
+
export const lsParser = {
|
|
6
|
+
name: "ls",
|
|
7
|
+
detect(command, output) {
|
|
8
|
+
return /^\s*(ls|ll|la)\b/.test(command) && output.includes(" ");
|
|
9
|
+
},
|
|
10
|
+
parse(_command, output) {
|
|
11
|
+
const lines = output.split("\n").filter(l => l.trim());
|
|
12
|
+
const entries = [];
|
|
13
|
+
for (const line of lines) {
|
|
14
|
+
// ls -la format: drwxr-xr-x 5 user group 160 Mar 10 09:00 dirname
|
|
15
|
+
const match = line.match(/^([dlcbps-])([rwxsStT-]{9})\s+\d+\s+\S+\s+\S+\s+(\d+)\s+(\w+\s+\d+\s+[\d:]+)\s+(.+)$/);
|
|
16
|
+
if (match) {
|
|
17
|
+
const typeChar = match[1];
|
|
18
|
+
entries.push({
|
|
19
|
+
name: match[5],
|
|
20
|
+
type: typeChar === "d" ? "dir" : typeChar === "l" ? "symlink" : "file",
|
|
21
|
+
size: parseInt(match[3]),
|
|
22
|
+
modified: match[4],
|
|
23
|
+
permissions: match[1] + match[2],
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
else if (line.trim() && !line.startsWith("total ")) {
|
|
27
|
+
// Simple ls output — just filenames
|
|
28
|
+
entries.push({ name: line.trim(), type: "file" });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return entries;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
export const findParser = {
|
|
35
|
+
name: "find",
|
|
36
|
+
detect(command, _output) {
|
|
37
|
+
return /^\s*(find|fd)\b/.test(command);
|
|
38
|
+
},
|
|
39
|
+
parse(_command, output) {
|
|
40
|
+
const lines = output.split("\n").filter(l => l.trim());
|
|
41
|
+
const source = [];
|
|
42
|
+
const other = [];
|
|
43
|
+
let nodeModulesCount = 0;
|
|
44
|
+
let distCount = 0;
|
|
45
|
+
for (const line of lines) {
|
|
46
|
+
const path = line.trim();
|
|
47
|
+
if (!path)
|
|
48
|
+
continue;
|
|
49
|
+
if (NODE_MODULES_RE.test(path)) {
|
|
50
|
+
nodeModulesCount++;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (DIST_RE.test(path)) {
|
|
54
|
+
distCount++;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const name = path.split("/").pop() ?? path;
|
|
58
|
+
const entry = { name: path, type: SOURCE_EXTS.test(name) ? "file" : "other" };
|
|
59
|
+
if (SOURCE_EXTS.test(name)) {
|
|
60
|
+
source.push(entry);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
other.push(entry);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const filtered = [];
|
|
67
|
+
if (nodeModulesCount > 0)
|
|
68
|
+
filtered.push({ count: nodeModulesCount, reason: "node_modules" });
|
|
69
|
+
if (distCount > 0)
|
|
70
|
+
filtered.push({ count: distCount, reason: "dist/build" });
|
|
71
|
+
return {
|
|
72
|
+
total: lines.length,
|
|
73
|
+
source,
|
|
74
|
+
other,
|
|
75
|
+
filtered,
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Parsers for git output (log, status, diff)
|
|
2
|
+
export const gitLogParser = {
|
|
3
|
+
name: "git-log",
|
|
4
|
+
detect(command, _output) {
|
|
5
|
+
return /\bgit\s+log\b/.test(command);
|
|
6
|
+
},
|
|
7
|
+
parse(_command, output) {
|
|
8
|
+
const entries = [];
|
|
9
|
+
const lines = output.split("\n");
|
|
10
|
+
let hash = "", author = "", date = "", message = [];
|
|
11
|
+
for (const line of lines) {
|
|
12
|
+
const commitMatch = line.match(/^commit\s+([a-f0-9]+)/);
|
|
13
|
+
if (commitMatch) {
|
|
14
|
+
if (hash) {
|
|
15
|
+
entries.push({ hash: hash.slice(0, 8), author, date, message: message.join(" ").trim() });
|
|
16
|
+
}
|
|
17
|
+
hash = commitMatch[1];
|
|
18
|
+
author = "";
|
|
19
|
+
date = "";
|
|
20
|
+
message = [];
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const authorMatch = line.match(/^Author:\s+(.+)/);
|
|
24
|
+
if (authorMatch) {
|
|
25
|
+
author = authorMatch[1];
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const dateMatch = line.match(/^Date:\s+(.+)/);
|
|
29
|
+
if (dateMatch) {
|
|
30
|
+
date = dateMatch[1].trim();
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (line.startsWith(" ")) {
|
|
34
|
+
message.push(line.trim());
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (hash) {
|
|
38
|
+
entries.push({ hash: hash.slice(0, 8), author, date, message: message.join(" ").trim() });
|
|
39
|
+
}
|
|
40
|
+
return entries;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
export const gitStatusParser = {
|
|
44
|
+
name: "git-status",
|
|
45
|
+
detect(command, _output) {
|
|
46
|
+
return /\bgit\s+status\b/.test(command);
|
|
47
|
+
},
|
|
48
|
+
parse(_command, output) {
|
|
49
|
+
const lines = output.split("\n");
|
|
50
|
+
let branch = "";
|
|
51
|
+
const staged = [];
|
|
52
|
+
const unstaged = [];
|
|
53
|
+
const untracked = [];
|
|
54
|
+
const branchMatch = output.match(/On branch\s+(\S+)/);
|
|
55
|
+
if (branchMatch)
|
|
56
|
+
branch = branchMatch[1];
|
|
57
|
+
let section = "";
|
|
58
|
+
for (const line of lines) {
|
|
59
|
+
if (line.includes("Changes to be committed")) {
|
|
60
|
+
section = "staged";
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (line.includes("Changes not staged")) {
|
|
64
|
+
section = "unstaged";
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (line.includes("Untracked files")) {
|
|
68
|
+
section = "untracked";
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const fileMatch = line.match(/^\s+(?:new file|modified|deleted|renamed):\s+(.+)/);
|
|
72
|
+
if (fileMatch) {
|
|
73
|
+
if (section === "staged")
|
|
74
|
+
staged.push(fileMatch[1].trim());
|
|
75
|
+
else if (section === "unstaged")
|
|
76
|
+
unstaged.push(fileMatch[1].trim());
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
// Untracked files are just indented filenames
|
|
80
|
+
if (section === "untracked" && line.match(/^\s+\S/) && !line.includes("(use ")) {
|
|
81
|
+
untracked.push(line.trim());
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return { branch, staged, unstaged, untracked };
|
|
85
|
+
},
|
|
86
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Output parser registry — auto-detect command output type and parse to structured JSON
|
|
2
|
+
import { lsParser, findParser } from "./files.js";
|
|
3
|
+
import { testParser } from "./tests.js";
|
|
4
|
+
import { gitLogParser, gitStatusParser } from "./git.js";
|
|
5
|
+
import { buildParser, npmInstallParser } from "./build.js";
|
|
6
|
+
import { errorParser } from "./errors.js";
|
|
7
|
+
// Ordered by specificity — more specific parsers first
|
|
8
|
+
const parsers = [
|
|
9
|
+
npmInstallParser,
|
|
10
|
+
testParser,
|
|
11
|
+
gitLogParser,
|
|
12
|
+
gitStatusParser,
|
|
13
|
+
buildParser,
|
|
14
|
+
findParser,
|
|
15
|
+
lsParser,
|
|
16
|
+
errorParser, // fallback for error detection
|
|
17
|
+
];
|
|
18
|
+
/** Try to parse command output with the best matching parser */
|
|
19
|
+
export function parseOutput(command, output) {
|
|
20
|
+
for (const parser of parsers) {
|
|
21
|
+
if (parser.detect(command, output)) {
|
|
22
|
+
try {
|
|
23
|
+
const data = parser.parse(command, output);
|
|
24
|
+
return { parser: parser.name, data, raw: output };
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
/** Get all parsers that match (for debugging/info) */
|
|
34
|
+
export function detectParsers(command, output) {
|
|
35
|
+
return parsers.filter(p => p.detect(command, output)).map(p => p.name);
|
|
36
|
+
}
|
|
37
|
+
/** Estimate token count for a string (rough: ~4 chars per token) */
|
|
38
|
+
export function estimateTokens(text) {
|
|
39
|
+
return Math.ceil(text.length / 4);
|
|
40
|
+
}
|
|
41
|
+
/** Calculate token savings between raw output and parsed JSON */
|
|
42
|
+
export function tokenSavings(raw, parsed) {
|
|
43
|
+
const rawTokens = estimateTokens(raw);
|
|
44
|
+
const parsedTokens = estimateTokens(JSON.stringify(parsed));
|
|
45
|
+
const saved = Math.max(0, rawTokens - parsedTokens);
|
|
46
|
+
const percent = rawTokens > 0 ? Math.round((saved / rawTokens) * 100) : 0;
|
|
47
|
+
return { rawTokens, parsedTokens, saved, percent };
|
|
48
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { describe, it, expect } from "bun:test";
|
|
2
|
+
import { parseOutput, tokenSavings, estimateTokens } from "./index.js";
|
|
3
|
+
describe("parseOutput", () => {
|
|
4
|
+
it("parses ls -la output", () => {
|
|
5
|
+
const output = `total 32
|
|
6
|
+
drwxr-xr-x 5 user staff 160 Mar 10 09:00 src
|
|
7
|
+
-rw-r--r-- 1 user staff 450 Mar 10 09:00 package.json
|
|
8
|
+
lrwxr-xr-x 1 user staff 20 Mar 10 09:00 link -> target`;
|
|
9
|
+
const result = parseOutput("ls -la", output);
|
|
10
|
+
expect(result).not.toBeNull();
|
|
11
|
+
expect(result.parser).toBe("ls");
|
|
12
|
+
const data = result.data;
|
|
13
|
+
expect(data.length).toBe(3);
|
|
14
|
+
expect(data[0].name).toBe("src");
|
|
15
|
+
expect(data[0].type).toBe("dir");
|
|
16
|
+
expect(data[1].name).toBe("package.json");
|
|
17
|
+
expect(data[1].type).toBe("file");
|
|
18
|
+
expect(data[2].type).toBe("symlink");
|
|
19
|
+
});
|
|
20
|
+
it("parses find output and filters node_modules", () => {
|
|
21
|
+
const output = `./src/lib/webhooks.ts
|
|
22
|
+
./node_modules/@types/node/async_hooks.d.ts
|
|
23
|
+
./node_modules/@types/node/perf_hooks.d.ts
|
|
24
|
+
./dist/lib/webhooks.d.ts
|
|
25
|
+
./src/routes/api.ts`;
|
|
26
|
+
const result = parseOutput("find . -name '*hooks*' -type f", output);
|
|
27
|
+
expect(result).not.toBeNull();
|
|
28
|
+
expect(result.parser).toBe("find");
|
|
29
|
+
const data = result.data;
|
|
30
|
+
expect(data.source.length).toBe(2); // webhooks.ts and api.ts
|
|
31
|
+
expect(data.filtered.length).toBeGreaterThan(0);
|
|
32
|
+
expect(data.filtered.find((f) => f.reason === "node_modules")?.count).toBe(2);
|
|
33
|
+
});
|
|
34
|
+
it("parses test output (jest style)", () => {
|
|
35
|
+
const output = `PASS src/auth.test.ts
|
|
36
|
+
FAIL src/db.test.ts
|
|
37
|
+
✗ should connect to database
|
|
38
|
+
Error: Connection refused
|
|
39
|
+
Tests: 5 passed, 1 failed, 1 skipped, 7 total
|
|
40
|
+
Time: 3.2s`;
|
|
41
|
+
const result = parseOutput("npm test", output);
|
|
42
|
+
expect(result).not.toBeNull();
|
|
43
|
+
expect(result.parser).toBe("test");
|
|
44
|
+
const data = result.data;
|
|
45
|
+
expect(data.passed).toBe(5);
|
|
46
|
+
expect(data.failed).toBe(1);
|
|
47
|
+
expect(data.skipped).toBe(1);
|
|
48
|
+
expect(data.total).toBe(7);
|
|
49
|
+
});
|
|
50
|
+
it("parses git status", () => {
|
|
51
|
+
const output = `On branch main
|
|
52
|
+
Changes to be committed:
|
|
53
|
+
new file: src/mcp/server.ts
|
|
54
|
+
modified: src/ai.ts
|
|
55
|
+
|
|
56
|
+
Changes not staged for commit:
|
|
57
|
+
modified: package.json
|
|
58
|
+
|
|
59
|
+
Untracked files:
|
|
60
|
+
src/tree.ts`;
|
|
61
|
+
const result = parseOutput("git status", output);
|
|
62
|
+
expect(result).not.toBeNull();
|
|
63
|
+
expect(result.parser).toBe("git-status");
|
|
64
|
+
const data = result.data;
|
|
65
|
+
expect(data.branch).toBe("main");
|
|
66
|
+
expect(data.staged.length).toBe(2);
|
|
67
|
+
expect(data.unstaged.length).toBe(1);
|
|
68
|
+
expect(data.untracked.length).toBe(1);
|
|
69
|
+
});
|
|
70
|
+
it("parses git log", () => {
|
|
71
|
+
const output = `commit af19ce3456789
|
|
72
|
+
Author: Andrei Hasna <andrei@hasna.com>
|
|
73
|
+
Date: Sat Mar 15 10:00:00 2026
|
|
74
|
+
|
|
75
|
+
feat: add MCP server
|
|
76
|
+
|
|
77
|
+
commit 3963db5123456
|
|
78
|
+
Author: Andrei Hasna <andrei@hasna.com>
|
|
79
|
+
Date: Fri Mar 14 09:00:00 2026
|
|
80
|
+
|
|
81
|
+
feat: tabs and browse mode`;
|
|
82
|
+
const result = parseOutput("git log", output);
|
|
83
|
+
expect(result).not.toBeNull();
|
|
84
|
+
expect(result.parser).toBe("git-log");
|
|
85
|
+
const data = result.data;
|
|
86
|
+
expect(data.length).toBe(2);
|
|
87
|
+
expect(data[0].hash).toBe("af19ce34");
|
|
88
|
+
expect(data[0].message).toBe("feat: add MCP server");
|
|
89
|
+
});
|
|
90
|
+
it("parses npm install output", () => {
|
|
91
|
+
const output = `added 47 packages in 3.2s
|
|
92
|
+
2 vulnerabilities found`;
|
|
93
|
+
const result = parseOutput("npm install", output);
|
|
94
|
+
expect(result).not.toBeNull();
|
|
95
|
+
expect(result.parser).toBe("npm-install");
|
|
96
|
+
const data = result.data;
|
|
97
|
+
expect(data.installed).toBe(47);
|
|
98
|
+
expect(data.duration).toBe("3.2s");
|
|
99
|
+
expect(data.vulnerabilities).toBe(2);
|
|
100
|
+
});
|
|
101
|
+
it("parses build output", () => {
|
|
102
|
+
const output = `Compiling...
|
|
103
|
+
1 warning
|
|
104
|
+
Found 0 errors
|
|
105
|
+
Done in 2.5s`;
|
|
106
|
+
const result = parseOutput("npm run build", output);
|
|
107
|
+
expect(result).not.toBeNull();
|
|
108
|
+
expect(result.parser).toBe("build");
|
|
109
|
+
const data = result.data;
|
|
110
|
+
expect(data.status).toBe("success");
|
|
111
|
+
expect(data.warnings).toBe(1);
|
|
112
|
+
});
|
|
113
|
+
it("detects errors", () => {
|
|
114
|
+
const output = `Error: EADDRINUSE: address already in use :3000`;
|
|
115
|
+
const result = parseOutput("node server.js", output);
|
|
116
|
+
expect(result).not.toBeNull();
|
|
117
|
+
expect(result.parser).toBe("error");
|
|
118
|
+
const data = result.data;
|
|
119
|
+
expect(data.type).toBe("port_in_use");
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
describe("estimateTokens", () => {
|
|
123
|
+
it("estimates roughly 4 chars per token", () => {
|
|
124
|
+
expect(estimateTokens("hello world")).toBe(3); // 11 chars / 4 = 2.75 → 3
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe("tokenSavings", () => {
|
|
128
|
+
it("calculates savings correctly", () => {
|
|
129
|
+
const raw = "a".repeat(400); // 100 tokens
|
|
130
|
+
const parsed = { status: "ok" };
|
|
131
|
+
const result = tokenSavings(raw, parsed);
|
|
132
|
+
expect(result.rawTokens).toBe(100);
|
|
133
|
+
expect(result.saved).toBeGreaterThan(0);
|
|
134
|
+
expect(result.percent).toBeGreaterThan(0);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Parser for test runner output (jest, vitest, bun test, pytest, go test)
|
|
2
|
+
export const testParser = {
|
|
3
|
+
name: "test",
|
|
4
|
+
detect(command, output) {
|
|
5
|
+
if (/\b(jest|vitest|bun\s+test|pytest|go\s+test|mocha|ava|tap)\b/.test(command))
|
|
6
|
+
return true;
|
|
7
|
+
if (/\b(npm|bun|pnpm|yarn)\s+(run\s+)?test\b/.test(command))
|
|
8
|
+
return true;
|
|
9
|
+
// Detect by output patterns
|
|
10
|
+
return /Tests:\s+\d+/.test(output) || /\d+\s+(passing|passed|failed)/.test(output) || /PASS|FAIL/.test(output);
|
|
11
|
+
},
|
|
12
|
+
parse(_command, output) {
|
|
13
|
+
const failures = [];
|
|
14
|
+
let passed = 0, failed = 0, skipped = 0, duration;
|
|
15
|
+
// Jest/Vitest style: Tests: 5 passed, 2 failed, 7 total
|
|
16
|
+
const jestMatch = output.match(/Tests:\s+(?:(\d+)\s+passed)?[,\s]*(?:(\d+)\s+failed)?[,\s]*(?:(\d+)\s+skipped)?[,\s]*(\d+)\s+total/);
|
|
17
|
+
if (jestMatch) {
|
|
18
|
+
passed = parseInt(jestMatch[1] ?? "0");
|
|
19
|
+
failed = parseInt(jestMatch[2] ?? "0");
|
|
20
|
+
skipped = parseInt(jestMatch[3] ?? "0");
|
|
21
|
+
}
|
|
22
|
+
// Bun test style: 5 pass, 2 fail
|
|
23
|
+
const bunMatch = output.match(/(\d+)\s+pass.*?(\d+)\s+fail/);
|
|
24
|
+
if (!jestMatch && bunMatch) {
|
|
25
|
+
passed = parseInt(bunMatch[1]);
|
|
26
|
+
failed = parseInt(bunMatch[2]);
|
|
27
|
+
}
|
|
28
|
+
// Pytest style: 5 passed, 2 failed
|
|
29
|
+
const pytestMatch = output.match(/(\d+)\s+passed(?:.*?(\d+)\s+failed)?/);
|
|
30
|
+
if (!jestMatch && !bunMatch && pytestMatch) {
|
|
31
|
+
passed = parseInt(pytestMatch[1]);
|
|
32
|
+
failed = parseInt(pytestMatch[2] ?? "0");
|
|
33
|
+
}
|
|
34
|
+
// Go test: ok/FAIL + count
|
|
35
|
+
const goPassMatch = output.match(/ok\s+\S+\s+([\d.]+s)/);
|
|
36
|
+
const goFailMatch = output.match(/FAIL\s+\S+/);
|
|
37
|
+
if (!jestMatch && !bunMatch && !pytestMatch && (goPassMatch || goFailMatch)) {
|
|
38
|
+
const passLines = (output.match(/--- PASS/g) || []).length;
|
|
39
|
+
const failLines = (output.match(/--- FAIL/g) || []).length;
|
|
40
|
+
passed = passLines;
|
|
41
|
+
failed = failLines;
|
|
42
|
+
if (goPassMatch)
|
|
43
|
+
duration = goPassMatch[1];
|
|
44
|
+
}
|
|
45
|
+
// Duration
|
|
46
|
+
const timeMatch = output.match(/Time:\s+([\d.]+\s*(?:s|ms|m))/i) || output.match(/in\s+([\d.]+\s*(?:s|ms|m))/i);
|
|
47
|
+
if (timeMatch)
|
|
48
|
+
duration = timeMatch[1];
|
|
49
|
+
// Extract failure details: lines starting with FAIL or ✗ or ×
|
|
50
|
+
const lines = output.split("\n");
|
|
51
|
+
let capturingFailure = false;
|
|
52
|
+
let currentTest = "";
|
|
53
|
+
let currentError = [];
|
|
54
|
+
for (const line of lines) {
|
|
55
|
+
const failMatch = line.match(/(?:FAIL|✗|×|✕)\s+(.+)/);
|
|
56
|
+
if (failMatch) {
|
|
57
|
+
if (capturingFailure && currentTest) {
|
|
58
|
+
failures.push({ test: currentTest, error: currentError.join("\n").trim() });
|
|
59
|
+
}
|
|
60
|
+
currentTest = failMatch[1].trim();
|
|
61
|
+
currentError = [];
|
|
62
|
+
capturingFailure = true;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (capturingFailure) {
|
|
66
|
+
if (line.match(/^(PASS|✓|✔|FAIL|✗|×|✕)\s/) || line.match(/^Tests:|^\d+ pass/)) {
|
|
67
|
+
failures.push({ test: currentTest, error: currentError.join("\n").trim() });
|
|
68
|
+
capturingFailure = false;
|
|
69
|
+
currentTest = "";
|
|
70
|
+
currentError = [];
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
currentError.push(line);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (capturingFailure && currentTest) {
|
|
78
|
+
failures.push({ test: currentTest, error: currentError.join("\n").trim() });
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
passed,
|
|
82
|
+
failed,
|
|
83
|
+
skipped,
|
|
84
|
+
total: passed + failed + skipped,
|
|
85
|
+
duration,
|
|
86
|
+
failures,
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
};
|