@hasna/terminal 2.3.2 → 3.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/dist/ai.js +78 -85
- package/dist/cache.js +3 -2
- package/dist/cli.js +1 -1
- package/dist/compression.js +8 -30
- package/dist/context-hints.js +20 -10
- package/dist/diff-cache.js +1 -1
- package/dist/discover.js +1 -1
- package/dist/economy.js +37 -5
- package/dist/expand-store.js +7 -1
- package/dist/mcp/server.js +44 -68
- package/dist/output-processor.js +10 -7
- package/dist/providers/anthropic.js +6 -2
- package/dist/providers/cerebras.js +6 -93
- package/dist/providers/groq.js +6 -93
- package/dist/providers/index.js +85 -36
- package/dist/providers/openai-compat.js +93 -0
- package/dist/providers/xai.js +6 -93
- package/dist/tokens.js +17 -0
- package/dist/tool-profiles.js +9 -2
- package/package.json +1 -1
- package/src/ai.ts +83 -94
- package/src/cache.ts +3 -2
- package/src/cli.tsx +1 -1
- package/src/compression.ts +8 -35
- package/src/context-hints.ts +20 -10
- package/src/diff-cache.ts +1 -1
- package/src/discover.ts +1 -1
- package/src/economy.ts +37 -5
- package/src/expand-store.ts +8 -1
- package/src/mcp/server.ts +45 -73
- package/src/output-processor.ts +11 -8
- package/src/providers/anthropic.ts +6 -2
- package/src/providers/base.ts +2 -0
- package/src/providers/cerebras.ts +6 -105
- package/src/providers/groq.ts +6 -105
- package/src/providers/index.ts +84 -33
- package/src/providers/openai-compat.ts +109 -0
- package/src/providers/xai.ts +6 -105
- package/src/tokens.ts +18 -0
- package/src/tool-profiles.ts +9 -2
- package/src/compression.test.ts +0 -49
- package/src/output-router.ts +0 -56
- package/src/parsers/base.ts +0 -72
- package/src/parsers/build.ts +0 -73
- package/src/parsers/errors.ts +0 -107
- package/src/parsers/files.ts +0 -91
- package/src/parsers/git.ts +0 -101
- package/src/parsers/index.ts +0 -66
- package/src/parsers/parsers.test.ts +0 -153
- package/src/parsers/tests.ts +0 -98
package/src/parsers/base.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
// Base types for output parsers
|
|
2
|
-
|
|
3
|
-
export interface Parser<T = unknown> {
|
|
4
|
-
/** Name of this parser */
|
|
5
|
-
readonly name: string;
|
|
6
|
-
|
|
7
|
-
/** Test if this parser can handle the given command/output */
|
|
8
|
-
detect(command: string, output: string): boolean;
|
|
9
|
-
|
|
10
|
-
/** Parse the output into structured data */
|
|
11
|
-
parse(command: string, output: string): T;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface FileEntry {
|
|
15
|
-
name: string;
|
|
16
|
-
type: "file" | "dir" | "symlink" | "other";
|
|
17
|
-
size?: number;
|
|
18
|
-
modified?: string;
|
|
19
|
-
permissions?: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface TestResult {
|
|
23
|
-
passed: number;
|
|
24
|
-
failed: number;
|
|
25
|
-
skipped: number;
|
|
26
|
-
total: number;
|
|
27
|
-
duration?: string;
|
|
28
|
-
failures: { test: string; error: string }[];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface GitLogEntry {
|
|
32
|
-
hash: string;
|
|
33
|
-
author: string;
|
|
34
|
-
date: string;
|
|
35
|
-
message: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface GitStatus {
|
|
39
|
-
branch: string;
|
|
40
|
-
staged: string[];
|
|
41
|
-
unstaged: string[];
|
|
42
|
-
untracked: string[];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface BuildResult {
|
|
46
|
-
status: "success" | "failure";
|
|
47
|
-
warnings: number;
|
|
48
|
-
errors: number;
|
|
49
|
-
duration?: string;
|
|
50
|
-
output?: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface NpmInstallResult {
|
|
54
|
-
installed: number;
|
|
55
|
-
duration?: string;
|
|
56
|
-
vulnerabilities: number;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export interface ErrorInfo {
|
|
60
|
-
type: string;
|
|
61
|
-
message: string;
|
|
62
|
-
file?: string;
|
|
63
|
-
line?: number;
|
|
64
|
-
suggestion?: string;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export interface SearchResult {
|
|
68
|
-
total: number;
|
|
69
|
-
source: FileEntry[];
|
|
70
|
-
other: FileEntry[];
|
|
71
|
-
filtered: { count: number; reason: string }[];
|
|
72
|
-
}
|
package/src/parsers/build.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// Parser for build output (npm/bun/pnpm build, tsc, webpack, vite, etc.)
|
|
2
|
-
|
|
3
|
-
import type { Parser, BuildResult, NpmInstallResult } from "./base.js";
|
|
4
|
-
|
|
5
|
-
export const buildParser: Parser<BuildResult> = {
|
|
6
|
-
name: "build",
|
|
7
|
-
|
|
8
|
-
detect(command: string, output: string): boolean {
|
|
9
|
-
if (/\b(npm|bun|pnpm|yarn)\s+(run\s+)?build\b/.test(command)) return true;
|
|
10
|
-
if (/\btsc\b/.test(command)) return true;
|
|
11
|
-
if (/\b(webpack|vite|esbuild|rollup|turbo)\b/.test(command)) return true;
|
|
12
|
-
return /\b(compiled|bundled|built)\b/i.test(output) && /\b(success|error|warning)\b/i.test(output);
|
|
13
|
-
},
|
|
14
|
-
|
|
15
|
-
parse(_command: string, output: string): BuildResult {
|
|
16
|
-
const lines = output.split("\n");
|
|
17
|
-
let warnings = 0, errors = 0, duration: string | undefined;
|
|
18
|
-
|
|
19
|
-
// Count warnings and errors
|
|
20
|
-
for (const line of lines) {
|
|
21
|
-
if (/\bwarning\b/i.test(line)) warnings++;
|
|
22
|
-
if (/\berror\b/i.test(line) && !/0 errors/.test(line)) errors++;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Specific patterns
|
|
26
|
-
const tscErrors = output.match(/Found (\d+) error/);
|
|
27
|
-
if (tscErrors) errors = parseInt(tscErrors[1]);
|
|
28
|
-
|
|
29
|
-
const warningCount = output.match(/(\d+)\s+warning/);
|
|
30
|
-
if (warningCount) warnings = parseInt(warningCount[1]);
|
|
31
|
-
|
|
32
|
-
// Duration
|
|
33
|
-
const timeMatch = output.match(/(?:in|took)\s+([\d.]+\s*(?:s|ms|m))/i) ||
|
|
34
|
-
output.match(/Done in ([\d.]+s)/);
|
|
35
|
-
if (timeMatch) duration = timeMatch[1];
|
|
36
|
-
|
|
37
|
-
const status: "success" | "failure" = errors > 0 ? "failure" : "success";
|
|
38
|
-
|
|
39
|
-
return { status, warnings, errors, duration };
|
|
40
|
-
},
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const npmInstallParser: Parser<NpmInstallResult> = {
|
|
44
|
-
name: "npm-install",
|
|
45
|
-
|
|
46
|
-
detect(command: string, _output: string): boolean {
|
|
47
|
-
return /\b(npm|bun|pnpm|yarn)\s+(install|add|i)\b/.test(command);
|
|
48
|
-
},
|
|
49
|
-
|
|
50
|
-
parse(_command: string, output: string): NpmInstallResult {
|
|
51
|
-
let installed = 0, vulnerabilities = 0, duration: string | undefined;
|
|
52
|
-
|
|
53
|
-
// npm: added 47 packages in 3.2s
|
|
54
|
-
const npmMatch = output.match(/added\s+(\d+)\s+packages?\s+in\s+([\d.]+s)/);
|
|
55
|
-
if (npmMatch) {
|
|
56
|
-
installed = parseInt(npmMatch[1]);
|
|
57
|
-
duration = npmMatch[2];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// bun: 47 packages installed [1.2s]
|
|
61
|
-
const bunMatch = output.match(/(\d+)\s+packages?\s+installed.*?\[([\d.]+[ms]*s)\]/);
|
|
62
|
-
if (!npmMatch && bunMatch) {
|
|
63
|
-
installed = parseInt(bunMatch[1]);
|
|
64
|
-
duration = bunMatch[2];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Vulnerabilities
|
|
68
|
-
const vulnMatch = output.match(/(\d+)\s+vulnerabilit/);
|
|
69
|
-
if (vulnMatch) vulnerabilities = parseInt(vulnMatch[1]);
|
|
70
|
-
|
|
71
|
-
return { installed, vulnerabilities, duration };
|
|
72
|
-
},
|
|
73
|
-
};
|
package/src/parsers/errors.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
// Parser for common error patterns
|
|
2
|
-
|
|
3
|
-
import type { Parser, ErrorInfo } from "./base.js";
|
|
4
|
-
|
|
5
|
-
const ERROR_PATTERNS: { type: string; pattern: RegExp; extract: (m: RegExpMatchArray, output: string) => ErrorInfo }[] = [
|
|
6
|
-
{
|
|
7
|
-
type: "port_in_use",
|
|
8
|
-
pattern: /EADDRINUSE.*?(?::(\d+))|port\s+(\d+)\s+(?:is\s+)?(?:already\s+)?in\s+use/i,
|
|
9
|
-
extract: (m) => ({
|
|
10
|
-
type: "port_in_use",
|
|
11
|
-
message: m[0],
|
|
12
|
-
suggestion: `Kill the process: lsof -i :${m[1] ?? m[2]} -t | xargs kill`,
|
|
13
|
-
}),
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
type: "file_not_found",
|
|
17
|
-
pattern: /ENOENT.*?'([^']+)'|No such file or directory:\s*(.+)/,
|
|
18
|
-
extract: (m) => ({
|
|
19
|
-
type: "file_not_found",
|
|
20
|
-
message: m[0],
|
|
21
|
-
file: m[1] ?? m[2]?.trim(),
|
|
22
|
-
suggestion: "Check the file path exists",
|
|
23
|
-
}),
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
type: "permission_denied",
|
|
27
|
-
pattern: /EACCES.*?'([^']+)'|Permission denied:\s*(.+)/,
|
|
28
|
-
extract: (m) => ({
|
|
29
|
-
type: "permission_denied",
|
|
30
|
-
message: m[0],
|
|
31
|
-
file: m[1] ?? m[2]?.trim(),
|
|
32
|
-
suggestion: "Check file permissions or run with sudo",
|
|
33
|
-
}),
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
type: "command_not_found",
|
|
37
|
-
pattern: /command not found:\s*(\S+)|(\S+):\s*not found/,
|
|
38
|
-
extract: (m) => ({
|
|
39
|
-
type: "command_not_found",
|
|
40
|
-
message: m[0],
|
|
41
|
-
suggestion: `Install ${m[1] ?? m[2]} or check your PATH`,
|
|
42
|
-
}),
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
type: "dependency_missing",
|
|
46
|
-
pattern: /Cannot find module\s+'([^']+)'|Module not found.*?'([^']+)'/,
|
|
47
|
-
extract: (m) => ({
|
|
48
|
-
type: "dependency_missing",
|
|
49
|
-
message: m[0],
|
|
50
|
-
suggestion: `Install: npm install ${m[1] ?? m[2]}`,
|
|
51
|
-
}),
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
type: "syntax_error",
|
|
55
|
-
pattern: /SyntaxError:\s*(.+)|error TS\d+:\s*(.+)/,
|
|
56
|
-
extract: (m, output) => {
|
|
57
|
-
const fileMatch = output.match(/(\S+\.\w+):(\d+)/);
|
|
58
|
-
return {
|
|
59
|
-
type: "syntax_error",
|
|
60
|
-
message: m[1] ?? m[2] ?? m[0],
|
|
61
|
-
file: fileMatch?.[1],
|
|
62
|
-
line: fileMatch ? parseInt(fileMatch[2]) : undefined,
|
|
63
|
-
suggestion: "Fix the syntax error in the referenced file",
|
|
64
|
-
};
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
type: "out_of_memory",
|
|
69
|
-
pattern: /ENOMEM|JavaScript heap out of memory|Killed/,
|
|
70
|
-
extract: (m) => ({
|
|
71
|
-
type: "out_of_memory",
|
|
72
|
-
message: m[0],
|
|
73
|
-
suggestion: "Increase memory: NODE_OPTIONS=--max-old-space-size=4096",
|
|
74
|
-
}),
|
|
75
|
-
},
|
|
76
|
-
{
|
|
77
|
-
type: "network_error",
|
|
78
|
-
pattern: /ECONNREFUSED|ENOTFOUND|ETIMEDOUT|fetch failed/,
|
|
79
|
-
extract: (m) => ({
|
|
80
|
-
type: "network_error",
|
|
81
|
-
message: m[0],
|
|
82
|
-
suggestion: "Check network connection and target URL/host",
|
|
83
|
-
}),
|
|
84
|
-
},
|
|
85
|
-
];
|
|
86
|
-
|
|
87
|
-
export const errorParser: Parser<ErrorInfo> = {
|
|
88
|
-
name: "error",
|
|
89
|
-
|
|
90
|
-
detect(_command: string, output: string): boolean {
|
|
91
|
-
return ERROR_PATTERNS.some(({ pattern }) => pattern.test(output));
|
|
92
|
-
},
|
|
93
|
-
|
|
94
|
-
parse(_command: string, output: string): ErrorInfo {
|
|
95
|
-
for (const { pattern, extract } of ERROR_PATTERNS) {
|
|
96
|
-
const match = output.match(pattern);
|
|
97
|
-
if (match) return extract(match, output);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Generic error fallback
|
|
101
|
-
const errorLine = output.split("\n").find(l => /error/i.test(l));
|
|
102
|
-
return {
|
|
103
|
-
type: "unknown",
|
|
104
|
-
message: errorLine?.trim() ?? "Unknown error",
|
|
105
|
-
};
|
|
106
|
-
},
|
|
107
|
-
};
|
package/src/parsers/files.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
// Parser for file listing output (ls -la, find, etc.)
|
|
2
|
-
|
|
3
|
-
import type { Parser, FileEntry, SearchResult } from "./base.js";
|
|
4
|
-
|
|
5
|
-
const NODE_MODULES_RE = /node_modules/;
|
|
6
|
-
const DIST_RE = /\b(dist|build|\.next|__pycache__|coverage|\.git)\b/;
|
|
7
|
-
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)$/;
|
|
8
|
-
|
|
9
|
-
export const lsParser: Parser<FileEntry[]> = {
|
|
10
|
-
name: "ls",
|
|
11
|
-
|
|
12
|
-
detect(command: string, output: string): boolean {
|
|
13
|
-
return /^\s*(ls|ll|la)\b/.test(command) && output.includes(" ");
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
parse(_command: string, output: string): FileEntry[] {
|
|
17
|
-
const lines = output.split("\n").filter(l => l.trim());
|
|
18
|
-
const entries: FileEntry[] = [];
|
|
19
|
-
|
|
20
|
-
for (const line of lines) {
|
|
21
|
-
// ls -la format: drwxr-xr-x 5 user group 160 Mar 10 09:00 dirname
|
|
22
|
-
const match = line.match(/^([dlcbps-])([rwxsStT-]{9})\s+\d+\s+\S+\s+\S+\s+(\d+)\s+(\w+\s+\d+\s+[\d:]+)\s+(.+)$/);
|
|
23
|
-
if (match) {
|
|
24
|
-
const typeChar = match[1];
|
|
25
|
-
entries.push({
|
|
26
|
-
name: match[5],
|
|
27
|
-
type: typeChar === "d" ? "dir" : typeChar === "l" ? "symlink" : "file",
|
|
28
|
-
size: parseInt(match[3]),
|
|
29
|
-
modified: match[4],
|
|
30
|
-
permissions: match[1] + match[2],
|
|
31
|
-
});
|
|
32
|
-
} else if (line.trim() && !line.startsWith("total ")) {
|
|
33
|
-
// Simple ls output — just filenames
|
|
34
|
-
entries.push({ name: line.trim(), type: "file" });
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return entries;
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export const findParser: Parser<SearchResult> = {
|
|
43
|
-
name: "find",
|
|
44
|
-
|
|
45
|
-
detect(command: string, _output: string): boolean {
|
|
46
|
-
return /^\s*(find|fd)\b/.test(command);
|
|
47
|
-
},
|
|
48
|
-
|
|
49
|
-
parse(_command: string, output: string): SearchResult {
|
|
50
|
-
const lines = output.split("\n").filter(l => l.trim());
|
|
51
|
-
const source: FileEntry[] = [];
|
|
52
|
-
const other: FileEntry[] = [];
|
|
53
|
-
let nodeModulesCount = 0;
|
|
54
|
-
let distCount = 0;
|
|
55
|
-
|
|
56
|
-
for (const line of lines) {
|
|
57
|
-
const path = line.trim();
|
|
58
|
-
if (!path) continue;
|
|
59
|
-
|
|
60
|
-
if (NODE_MODULES_RE.test(path)) {
|
|
61
|
-
nodeModulesCount++;
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (DIST_RE.test(path)) {
|
|
66
|
-
distCount++;
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const name = path.split("/").pop() ?? path;
|
|
71
|
-
const entry: FileEntry = { name: path, type: SOURCE_EXTS.test(name) ? "file" : "other" };
|
|
72
|
-
|
|
73
|
-
if (SOURCE_EXTS.test(name)) {
|
|
74
|
-
source.push(entry);
|
|
75
|
-
} else {
|
|
76
|
-
other.push(entry);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const filtered: { count: number; reason: string }[] = [];
|
|
81
|
-
if (nodeModulesCount > 0) filtered.push({ count: nodeModulesCount, reason: "node_modules" });
|
|
82
|
-
if (distCount > 0) filtered.push({ count: distCount, reason: "dist/build" });
|
|
83
|
-
|
|
84
|
-
return {
|
|
85
|
-
total: lines.length,
|
|
86
|
-
source,
|
|
87
|
-
other,
|
|
88
|
-
filtered,
|
|
89
|
-
};
|
|
90
|
-
},
|
|
91
|
-
};
|
package/src/parsers/git.ts
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
// Parsers for git output (log, status, diff)
|
|
2
|
-
|
|
3
|
-
import type { Parser, GitLogEntry, GitStatus } from "./base.js";
|
|
4
|
-
|
|
5
|
-
export const gitLogParser: Parser<GitLogEntry[]> = {
|
|
6
|
-
name: "git-log",
|
|
7
|
-
|
|
8
|
-
detect(command: string, _output: string): boolean {
|
|
9
|
-
return /\bgit\s+log\b/.test(command);
|
|
10
|
-
},
|
|
11
|
-
|
|
12
|
-
parse(_command: string, output: string): GitLogEntry[] {
|
|
13
|
-
const entries: GitLogEntry[] = [];
|
|
14
|
-
const lines = output.split("\n");
|
|
15
|
-
|
|
16
|
-
// Detect oneline format: "abc1234 commit message"
|
|
17
|
-
const firstLine = lines[0]?.trim() ?? "";
|
|
18
|
-
const isOneline = /^[a-f0-9]{7,12}\s+/.test(firstLine) && !firstLine.startsWith("commit ");
|
|
19
|
-
|
|
20
|
-
if (isOneline) {
|
|
21
|
-
for (const line of lines) {
|
|
22
|
-
const match = line.trim().match(/^([a-f0-9]{7,12})\s+(.+)$/);
|
|
23
|
-
if (match) {
|
|
24
|
-
entries.push({ hash: match[1], author: "", date: "", message: match[2] });
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
return entries;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Verbose format
|
|
31
|
-
let hash = "", author = "", date = "", message: string[] = [];
|
|
32
|
-
|
|
33
|
-
for (const line of lines) {
|
|
34
|
-
const commitMatch = line.match(/^commit\s+([a-f0-9]+)/);
|
|
35
|
-
if (commitMatch) {
|
|
36
|
-
if (hash) {
|
|
37
|
-
entries.push({ hash: hash.slice(0, 8), author, date, message: message.join(" ").trim() });
|
|
38
|
-
}
|
|
39
|
-
hash = commitMatch[1];
|
|
40
|
-
author = ""; date = ""; message = [];
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const authorMatch = line.match(/^Author:\s+(.+)/);
|
|
45
|
-
if (authorMatch) { author = authorMatch[1]; continue; }
|
|
46
|
-
|
|
47
|
-
const dateMatch = line.match(/^Date:\s+(.+)/);
|
|
48
|
-
if (dateMatch) { date = dateMatch[1].trim(); continue; }
|
|
49
|
-
|
|
50
|
-
if (line.startsWith(" ")) {
|
|
51
|
-
message.push(line.trim());
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (hash) {
|
|
56
|
-
entries.push({ hash: hash.slice(0, 8), author, date, message: message.join(" ").trim() });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return entries;
|
|
60
|
-
},
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export const gitStatusParser: Parser<GitStatus> = {
|
|
64
|
-
name: "git-status",
|
|
65
|
-
|
|
66
|
-
detect(command: string, _output: string): boolean {
|
|
67
|
-
return /\bgit\s+status\b/.test(command);
|
|
68
|
-
},
|
|
69
|
-
|
|
70
|
-
parse(_command: string, output: string): GitStatus {
|
|
71
|
-
const lines = output.split("\n");
|
|
72
|
-
let branch = "";
|
|
73
|
-
const staged: string[] = [];
|
|
74
|
-
const unstaged: string[] = [];
|
|
75
|
-
const untracked: string[] = [];
|
|
76
|
-
|
|
77
|
-
const branchMatch = output.match(/On branch\s+(\S+)/);
|
|
78
|
-
if (branchMatch) branch = branchMatch[1];
|
|
79
|
-
|
|
80
|
-
let section = "";
|
|
81
|
-
for (const line of lines) {
|
|
82
|
-
if (line.includes("Changes to be committed")) { section = "staged"; continue; }
|
|
83
|
-
if (line.includes("Changes not staged")) { section = "unstaged"; continue; }
|
|
84
|
-
if (line.includes("Untracked files")) { section = "untracked"; continue; }
|
|
85
|
-
|
|
86
|
-
const fileMatch = line.match(/^\s+(?:new file|modified|deleted|renamed):\s+(.+)/);
|
|
87
|
-
if (fileMatch) {
|
|
88
|
-
if (section === "staged") staged.push(fileMatch[1].trim());
|
|
89
|
-
else if (section === "unstaged") unstaged.push(fileMatch[1].trim());
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Untracked files are just indented filenames
|
|
94
|
-
if (section === "untracked" && line.match(/^\s+\S/) && !line.includes("(use ")) {
|
|
95
|
-
untracked.push(line.trim());
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return { branch, staged, unstaged, untracked };
|
|
100
|
-
},
|
|
101
|
-
};
|
package/src/parsers/index.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
// Output parser registry — auto-detect command output type and parse to structured JSON
|
|
2
|
-
|
|
3
|
-
import type { Parser } from "./base.js";
|
|
4
|
-
import { lsParser, findParser } from "./files.js";
|
|
5
|
-
import { testParser } from "./tests.js";
|
|
6
|
-
import { gitLogParser, gitStatusParser } from "./git.js";
|
|
7
|
-
import { buildParser, npmInstallParser } from "./build.js";
|
|
8
|
-
import { errorParser } from "./errors.js";
|
|
9
|
-
|
|
10
|
-
export type { Parser } from "./base.js";
|
|
11
|
-
export type {
|
|
12
|
-
FileEntry, TestResult, GitLogEntry, GitStatus,
|
|
13
|
-
BuildResult, NpmInstallResult, ErrorInfo, SearchResult,
|
|
14
|
-
} from "./base.js";
|
|
15
|
-
|
|
16
|
-
// Ordered by specificity — more specific parsers first
|
|
17
|
-
const parsers: Parser[] = [
|
|
18
|
-
npmInstallParser,
|
|
19
|
-
testParser,
|
|
20
|
-
gitLogParser,
|
|
21
|
-
gitStatusParser,
|
|
22
|
-
buildParser,
|
|
23
|
-
findParser,
|
|
24
|
-
lsParser,
|
|
25
|
-
errorParser, // fallback for error detection
|
|
26
|
-
];
|
|
27
|
-
|
|
28
|
-
export interface ParseResult {
|
|
29
|
-
parser: string;
|
|
30
|
-
data: unknown;
|
|
31
|
-
raw: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Try to parse command output with the best matching parser */
|
|
35
|
-
export function parseOutput(command: string, output: string): ParseResult | null {
|
|
36
|
-
for (const parser of parsers) {
|
|
37
|
-
if (parser.detect(command, output)) {
|
|
38
|
-
try {
|
|
39
|
-
const data = parser.parse(command, output);
|
|
40
|
-
return { parser: parser.name, data, raw: output };
|
|
41
|
-
} catch {
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Get all parsers that match (for debugging/info) */
|
|
50
|
-
export function detectParsers(command: string, output: string): string[] {
|
|
51
|
-
return parsers.filter(p => p.detect(command, output)).map(p => p.name);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** Estimate token count for a string (rough: ~4 chars per token) */
|
|
55
|
-
export function estimateTokens(text: string): number {
|
|
56
|
-
return Math.ceil(text.length / 4);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** Calculate token savings between raw output and parsed JSON */
|
|
60
|
-
export function tokenSavings(raw: string, parsed: unknown): { rawTokens: number; parsedTokens: number; saved: number; percent: number } {
|
|
61
|
-
const rawTokens = estimateTokens(raw);
|
|
62
|
-
const parsedTokens = estimateTokens(JSON.stringify(parsed));
|
|
63
|
-
const saved = Math.max(0, rawTokens - parsedTokens);
|
|
64
|
-
const percent = rawTokens > 0 ? Math.round((saved / rawTokens) * 100) : 0;
|
|
65
|
-
return { rawTokens, parsedTokens, saved, percent };
|
|
66
|
-
}
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "bun:test";
|
|
2
|
-
import { parseOutput, tokenSavings, estimateTokens } from "./index.js";
|
|
3
|
-
|
|
4
|
-
describe("parseOutput", () => {
|
|
5
|
-
it("parses ls -la output", () => {
|
|
6
|
-
const output = `total 32
|
|
7
|
-
drwxr-xr-x 5 user staff 160 Mar 10 09:00 src
|
|
8
|
-
-rw-r--r-- 1 user staff 450 Mar 10 09:00 package.json
|
|
9
|
-
lrwxr-xr-x 1 user staff 20 Mar 10 09:00 link -> target`;
|
|
10
|
-
|
|
11
|
-
const result = parseOutput("ls -la", output);
|
|
12
|
-
expect(result).not.toBeNull();
|
|
13
|
-
expect(result!.parser).toBe("ls");
|
|
14
|
-
const data = result!.data as any[];
|
|
15
|
-
expect(data.length).toBe(3);
|
|
16
|
-
expect(data[0].name).toBe("src");
|
|
17
|
-
expect(data[0].type).toBe("dir");
|
|
18
|
-
expect(data[1].name).toBe("package.json");
|
|
19
|
-
expect(data[1].type).toBe("file");
|
|
20
|
-
expect(data[2].type).toBe("symlink");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("parses find output and filters node_modules", () => {
|
|
24
|
-
const output = `./src/lib/webhooks.ts
|
|
25
|
-
./node_modules/@types/node/async_hooks.d.ts
|
|
26
|
-
./node_modules/@types/node/perf_hooks.d.ts
|
|
27
|
-
./dist/lib/webhooks.d.ts
|
|
28
|
-
./src/routes/api.ts`;
|
|
29
|
-
|
|
30
|
-
const result = parseOutput("find . -name '*hooks*' -type f", output);
|
|
31
|
-
expect(result).not.toBeNull();
|
|
32
|
-
expect(result!.parser).toBe("find");
|
|
33
|
-
const data = result!.data as any;
|
|
34
|
-
expect(data.source.length).toBe(2); // webhooks.ts and api.ts
|
|
35
|
-
expect(data.filtered.length).toBeGreaterThan(0);
|
|
36
|
-
expect(data.filtered.find((f: any) => f.reason === "node_modules")?.count).toBe(2);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("parses test output (jest style)", () => {
|
|
40
|
-
const output = `PASS src/auth.test.ts
|
|
41
|
-
FAIL src/db.test.ts
|
|
42
|
-
✗ should connect to database
|
|
43
|
-
Error: Connection refused
|
|
44
|
-
Tests: 5 passed, 1 failed, 1 skipped, 7 total
|
|
45
|
-
Time: 3.2s`;
|
|
46
|
-
|
|
47
|
-
const result = parseOutput("npm test", output);
|
|
48
|
-
expect(result).not.toBeNull();
|
|
49
|
-
expect(result!.parser).toBe("test");
|
|
50
|
-
const data = result!.data as any;
|
|
51
|
-
expect(data.passed).toBe(5);
|
|
52
|
-
expect(data.failed).toBe(1);
|
|
53
|
-
expect(data.skipped).toBe(1);
|
|
54
|
-
expect(data.total).toBe(7);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("parses git status", () => {
|
|
58
|
-
const output = `On branch main
|
|
59
|
-
Changes to be committed:
|
|
60
|
-
new file: src/mcp/server.ts
|
|
61
|
-
modified: src/ai.ts
|
|
62
|
-
|
|
63
|
-
Changes not staged for commit:
|
|
64
|
-
modified: package.json
|
|
65
|
-
|
|
66
|
-
Untracked files:
|
|
67
|
-
src/tree.ts`;
|
|
68
|
-
|
|
69
|
-
const result = parseOutput("git status", output);
|
|
70
|
-
expect(result).not.toBeNull();
|
|
71
|
-
expect(result!.parser).toBe("git-status");
|
|
72
|
-
const data = result!.data as any;
|
|
73
|
-
expect(data.branch).toBe("main");
|
|
74
|
-
expect(data.staged.length).toBe(2);
|
|
75
|
-
expect(data.unstaged.length).toBe(1);
|
|
76
|
-
expect(data.untracked.length).toBe(1);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it("parses git log", () => {
|
|
80
|
-
const output = `commit af19ce3456789
|
|
81
|
-
Author: Andrei Hasna <andrei@hasna.com>
|
|
82
|
-
Date: Sat Mar 15 10:00:00 2026
|
|
83
|
-
|
|
84
|
-
feat: add MCP server
|
|
85
|
-
|
|
86
|
-
commit 3963db5123456
|
|
87
|
-
Author: Andrei Hasna <andrei@hasna.com>
|
|
88
|
-
Date: Fri Mar 14 09:00:00 2026
|
|
89
|
-
|
|
90
|
-
feat: tabs and browse mode`;
|
|
91
|
-
|
|
92
|
-
const result = parseOutput("git log", output);
|
|
93
|
-
expect(result).not.toBeNull();
|
|
94
|
-
expect(result!.parser).toBe("git-log");
|
|
95
|
-
const data = result!.data as any[];
|
|
96
|
-
expect(data.length).toBe(2);
|
|
97
|
-
expect(data[0].hash).toBe("af19ce34");
|
|
98
|
-
expect(data[0].message).toBe("feat: add MCP server");
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("parses npm install output", () => {
|
|
102
|
-
const output = `added 47 packages in 3.2s
|
|
103
|
-
2 vulnerabilities found`;
|
|
104
|
-
|
|
105
|
-
const result = parseOutput("npm install", output);
|
|
106
|
-
expect(result).not.toBeNull();
|
|
107
|
-
expect(result!.parser).toBe("npm-install");
|
|
108
|
-
const data = result!.data as any;
|
|
109
|
-
expect(data.installed).toBe(47);
|
|
110
|
-
expect(data.duration).toBe("3.2s");
|
|
111
|
-
expect(data.vulnerabilities).toBe(2);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it("parses build output", () => {
|
|
115
|
-
const output = `Compiling...
|
|
116
|
-
1 warning
|
|
117
|
-
Found 0 errors
|
|
118
|
-
Done in 2.5s`;
|
|
119
|
-
|
|
120
|
-
const result = parseOutput("npm run build", output);
|
|
121
|
-
expect(result).not.toBeNull();
|
|
122
|
-
expect(result!.parser).toBe("build");
|
|
123
|
-
const data = result!.data as any;
|
|
124
|
-
expect(data.status).toBe("success");
|
|
125
|
-
expect(data.warnings).toBe(1);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("detects errors", () => {
|
|
129
|
-
const output = `Error: EADDRINUSE: address already in use :3000`;
|
|
130
|
-
const result = parseOutput("node server.js", output);
|
|
131
|
-
expect(result).not.toBeNull();
|
|
132
|
-
expect(result!.parser).toBe("error");
|
|
133
|
-
const data = result!.data as any;
|
|
134
|
-
expect(data.type).toBe("port_in_use");
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
describe("estimateTokens", () => {
|
|
139
|
-
it("estimates roughly 4 chars per token", () => {
|
|
140
|
-
expect(estimateTokens("hello world")).toBe(3); // 11 chars / 4 = 2.75 → 3
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
describe("tokenSavings", () => {
|
|
145
|
-
it("calculates savings correctly", () => {
|
|
146
|
-
const raw = "a".repeat(400); // 100 tokens
|
|
147
|
-
const parsed = { status: "ok" };
|
|
148
|
-
const result = tokenSavings(raw, parsed);
|
|
149
|
-
expect(result.rawTokens).toBe(100);
|
|
150
|
-
expect(result.saved).toBeGreaterThan(0);
|
|
151
|
-
expect(result.percent).toBeGreaterThan(0);
|
|
152
|
-
});
|
|
153
|
-
});
|