@hasna/terminal 4.3.0 → 4.3.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/package.json +4 -3
- package/src/ai.ts +4 -4
- package/src/mcp/server.ts +26 -1704
- package/src/mcp/tools/batch.ts +106 -0
- package/src/mcp/tools/execute.ts +248 -0
- package/src/mcp/tools/files.ts +369 -0
- package/src/mcp/tools/git.ts +306 -0
- package/src/mcp/tools/helpers.ts +92 -0
- package/src/mcp/tools/memory.ts +170 -0
- package/src/mcp/tools/meta.ts +202 -0
- package/src/mcp/tools/process.ts +94 -0
- package/src/mcp/tools/project.ts +297 -0
- package/src/mcp/tools/search.ts +118 -0
- package/src/snapshots.ts +2 -2
- package/dist/App.js +0 -404
- package/dist/Browse.js +0 -79
- package/dist/FuzzyPicker.js +0 -47
- package/dist/Onboarding.js +0 -51
- package/dist/Spinner.js +0 -12
- package/dist/StatusBar.js +0 -49
- package/dist/ai.js +0 -315
- package/dist/cache.js +0 -42
- package/dist/cli.js +0 -778
- package/dist/command-rewriter.js +0 -64
- package/dist/command-validator.js +0 -86
- package/dist/compression.js +0 -91
- package/dist/context-hints.js +0 -285
- package/dist/diff-cache.js +0 -107
- package/dist/discover.js +0 -212
- package/dist/economy.js +0 -155
- package/dist/expand-store.js +0 -44
- package/dist/file-cache.js +0 -72
- package/dist/file-index.js +0 -62
- package/dist/history.js +0 -62
- package/dist/lazy-executor.js +0 -54
- package/dist/line-dedup.js +0 -59
- package/dist/loop-detector.js +0 -75
- package/dist/mcp/install.js +0 -189
- package/dist/mcp/server.js +0 -1375
- package/dist/noise-filter.js +0 -94
- package/dist/output-processor.js +0 -233
- package/dist/output-router.js +0 -41
- package/dist/output-store.js +0 -111
- package/dist/parsers/base.js +0 -2
- package/dist/parsers/build.js +0 -64
- package/dist/parsers/errors.js +0 -101
- package/dist/parsers/files.js +0 -78
- package/dist/parsers/git.js +0 -99
- package/dist/parsers/index.js +0 -48
- package/dist/parsers/tests.js +0 -89
- package/dist/providers/anthropic.js +0 -43
- package/dist/providers/base.js +0 -4
- package/dist/providers/cerebras.js +0 -8
- package/dist/providers/groq.js +0 -8
- package/dist/providers/index.js +0 -142
- package/dist/providers/openai-compat.js +0 -93
- package/dist/providers/xai.js +0 -8
- package/dist/recipes/model.js +0 -20
- package/dist/recipes/storage.js +0 -153
- package/dist/search/content-search.js +0 -70
- package/dist/search/file-search.js +0 -61
- package/dist/search/filters.js +0 -34
- package/dist/search/index.js +0 -5
- package/dist/search/semantic.js +0 -346
- package/dist/session-boot.js +0 -59
- package/dist/session-context.js +0 -55
- package/dist/sessions-db.js +0 -231
- package/dist/smart-display.js +0 -286
- package/dist/snapshots.js +0 -51
- package/dist/supervisor.js +0 -112
- package/dist/test-watchlist.js +0 -131
- package/dist/tokens.js +0 -17
- package/dist/tool-profiles.js +0 -129
- package/dist/tree.js +0 -94
- package/dist/usage-cache.js +0 -65
package/dist/command-rewriter.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
// Command rewriter — auto-optimize commands to produce less output
|
|
2
|
-
// Only rewrites when semantic result is identical
|
|
3
|
-
const rules = [
|
|
4
|
-
// find | grep -v node_modules → find -not -path
|
|
5
|
-
{
|
|
6
|
-
pattern: /find\s+(\S+)\s+(.*?)\|\s*grep\s+-v\s+node_modules/,
|
|
7
|
-
rewrite: (m, cmd) => cmd.replace(m[0], `find ${m[1]} ${m[2]}-not -path '*/node_modules/*'`),
|
|
8
|
-
reason: "avoid pipe, filter in-kernel",
|
|
9
|
-
},
|
|
10
|
-
// cat file | grep X → grep X file
|
|
11
|
-
{
|
|
12
|
-
pattern: /cat\s+(\S+)\s*\|\s*grep\s+(.*)/,
|
|
13
|
-
rewrite: (m) => `grep ${m[2]} ${m[1]}`,
|
|
14
|
-
reason: "useless cat",
|
|
15
|
-
},
|
|
16
|
-
// find without node_modules exclusion → add it
|
|
17
|
-
{
|
|
18
|
-
pattern: /^find\s+\.\s+(.*)(?!.*node_modules)/,
|
|
19
|
-
rewrite: (m, cmd) => {
|
|
20
|
-
if (cmd.includes("node_modules") || cmd.includes("-not -path"))
|
|
21
|
-
return cmd;
|
|
22
|
-
return cmd.replace(/^find\s+\.\s+/, "find . -not -path '*/node_modules/*' -not -path '*/.git/*' ");
|
|
23
|
-
},
|
|
24
|
-
reason: "auto-exclude node_modules and .git",
|
|
25
|
-
},
|
|
26
|
-
// git log without limit → add --oneline -20
|
|
27
|
-
{
|
|
28
|
-
pattern: /^git\s+log\s*$/,
|
|
29
|
-
rewrite: () => "git log --oneline -20",
|
|
30
|
-
reason: "prevent unbounded log output",
|
|
31
|
-
},
|
|
32
|
-
// git diff without stat → add --stat for overview
|
|
33
|
-
{
|
|
34
|
-
pattern: /^git\s+diff\s*$/,
|
|
35
|
-
rewrite: () => "git diff --stat",
|
|
36
|
-
reason: "stat overview is usually sufficient",
|
|
37
|
-
},
|
|
38
|
-
// npm ls without depth → add --depth=0
|
|
39
|
-
{
|
|
40
|
-
pattern: /^npm\s+ls\s*$/,
|
|
41
|
-
rewrite: () => "npm ls --depth=0",
|
|
42
|
-
reason: "full tree is massive, top-level usually enough",
|
|
43
|
-
},
|
|
44
|
-
// ps aux without filter → sort by memory and head (macOS compatible)
|
|
45
|
-
{
|
|
46
|
-
pattern: /^ps\s+aux\s*$/,
|
|
47
|
-
rewrite: () => "ps aux | sort -k4 -rn | head -20",
|
|
48
|
-
reason: "full process list is noise, show top consumers",
|
|
49
|
-
},
|
|
50
|
-
];
|
|
51
|
-
/** Rewrite a command to produce less output */
|
|
52
|
-
export function rewriteCommand(cmd) {
|
|
53
|
-
const trimmed = cmd.trim();
|
|
54
|
-
for (const rule of rules) {
|
|
55
|
-
const match = trimmed.match(rule.pattern);
|
|
56
|
-
if (match) {
|
|
57
|
-
const rewritten = rule.rewrite(match, trimmed);
|
|
58
|
-
if (rewritten !== trimmed) {
|
|
59
|
-
return { original: trimmed, rewritten, changed: true, reason: rule.reason };
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return { original: trimmed, rewritten: trimmed, changed: false };
|
|
64
|
-
}
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
// Command validator — catch invalid commands BEFORE executing
|
|
2
|
-
// Prevents shell errors from hallucinated flags, wrong paths, bad syntax
|
|
3
|
-
import { existsSync } from "fs";
|
|
4
|
-
import { join } from "path";
|
|
5
|
-
/** Extract file paths referenced in a command */
|
|
6
|
-
function extractPaths(command) {
|
|
7
|
-
const paths = [];
|
|
8
|
-
// Match quoted paths
|
|
9
|
-
const quoted = command.match(/["']([^"']+\.\w+)["']/g);
|
|
10
|
-
if (quoted)
|
|
11
|
-
paths.push(...quoted.map(q => q.replace(/["']/g, "")));
|
|
12
|
-
// Match unquoted paths with extensions or directory separators
|
|
13
|
-
const tokens = command.split(/\s+/);
|
|
14
|
-
for (const t of tokens) {
|
|
15
|
-
if (t.includes("/") && !t.startsWith("-") && !t.startsWith("|") && !t.startsWith("&")) {
|
|
16
|
-
// Clean shell operators from end
|
|
17
|
-
const clean = t.replace(/[;|&>]+$/, "");
|
|
18
|
-
if (clean && !clean.startsWith("-"))
|
|
19
|
-
paths.push(clean);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return [...new Set(paths)];
|
|
23
|
-
}
|
|
24
|
-
/** Check for obviously broken shell syntax */
|
|
25
|
-
function checkSyntax(command) {
|
|
26
|
-
const issues = [];
|
|
27
|
-
// Unmatched quotes
|
|
28
|
-
const singleQuotes = (command.match(/'/g) || []).length;
|
|
29
|
-
const doubleQuotes = (command.match(/"/g) || []).length;
|
|
30
|
-
if (singleQuotes % 2 !== 0)
|
|
31
|
-
issues.push("unmatched single quote");
|
|
32
|
-
if (doubleQuotes % 2 !== 0)
|
|
33
|
-
issues.push("unmatched double quote");
|
|
34
|
-
// Unmatched parentheses
|
|
35
|
-
const openParens = (command.match(/\(/g) || []).length;
|
|
36
|
-
const closeParens = (command.match(/\)/g) || []).length;
|
|
37
|
-
if (openParens !== closeParens)
|
|
38
|
-
issues.push("unmatched parentheses");
|
|
39
|
-
// Empty pipe targets
|
|
40
|
-
if (/\|\s*$/.test(command))
|
|
41
|
-
issues.push("pipe with no target");
|
|
42
|
-
if (/^\s*\|/.test(command))
|
|
43
|
-
issues.push("pipe with no source");
|
|
44
|
-
return issues;
|
|
45
|
-
}
|
|
46
|
-
/** Validate a command before execution */
|
|
47
|
-
export function validateCommand(command, cwd) {
|
|
48
|
-
const issues = [];
|
|
49
|
-
// Check syntax
|
|
50
|
-
issues.push(...checkSyntax(command));
|
|
51
|
-
// Check file paths exist
|
|
52
|
-
const paths = extractPaths(command);
|
|
53
|
-
for (const p of paths) {
|
|
54
|
-
const fullPath = p.startsWith("/") ? p : join(cwd, p);
|
|
55
|
-
if (p.includes("*") || p.includes("?"))
|
|
56
|
-
continue; // skip globs
|
|
57
|
-
if (p.startsWith("-"))
|
|
58
|
-
continue; // skip flags
|
|
59
|
-
if ([".", "..", "/", "~"].includes(p))
|
|
60
|
-
continue; // skip special
|
|
61
|
-
if (!existsSync(fullPath) && !existsSync(p)) {
|
|
62
|
-
// Only flag source file paths, not output paths
|
|
63
|
-
if (/\.(ts|tsx|js|jsx|json|md|yaml|yml|py|go|rs)$/.test(p)) {
|
|
64
|
-
issues.push(`file not found: ${p}`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
// Check for common GNU flags on macOS
|
|
69
|
-
const gnuFlags = command.match(/--max-depth|--color=|--sort=|--field-type|--no-deps/g);
|
|
70
|
-
if (gnuFlags) {
|
|
71
|
-
issues.push(`GNU flag on macOS: ${gnuFlags.join(", ")}`);
|
|
72
|
-
}
|
|
73
|
-
// Complexity guard — extreme pipe chains are fragile
|
|
74
|
-
const pipeCount = (command.match(/\|/g) || []).length;
|
|
75
|
-
if (pipeCount > 7) {
|
|
76
|
-
issues.push(`too complex: ${pipeCount} pipes — simplify`);
|
|
77
|
-
}
|
|
78
|
-
// grep -P (PCRE) doesn't exist on macOS
|
|
79
|
-
if (/grep\s+.*-[a-zA-Z]*P/.test(command)) {
|
|
80
|
-
issues.push("grep -P (PCRE) not available on macOS — use grep -E");
|
|
81
|
-
}
|
|
82
|
-
return {
|
|
83
|
-
valid: issues.length === 0,
|
|
84
|
-
issues,
|
|
85
|
-
};
|
|
86
|
-
}
|
package/dist/compression.js
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
// Token compression engine — reduces CLI output to fit within token budgets
|
|
2
|
-
// No regex parsing — just ANSI stripping, deduplication, and smart truncation.
|
|
3
|
-
// All intelligent output processing goes through AI via processOutput().
|
|
4
|
-
import { estimateTokens } from "./tokens.js";
|
|
5
|
-
/** Strip ANSI escape codes from text */
|
|
6
|
-
export function stripAnsi(text) {
|
|
7
|
-
// eslint-disable-next-line no-control-regex
|
|
8
|
-
return text.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "").replace(/\x1b\][^\x07]*\x07/g, "");
|
|
9
|
-
}
|
|
10
|
-
/** Deduplicate consecutive similar lines (e.g., "Compiling X... Compiling Y...") */
|
|
11
|
-
function deduplicateLines(lines) {
|
|
12
|
-
if (lines.length <= 3)
|
|
13
|
-
return lines;
|
|
14
|
-
const result = [];
|
|
15
|
-
let repeatCount = 0;
|
|
16
|
-
let repeatPattern = "";
|
|
17
|
-
for (let i = 0; i < lines.length; i++) {
|
|
18
|
-
const line = lines[i];
|
|
19
|
-
const pattern = line.replace(/[0-9]+/g, "N").replace(/\/\S+/g, "/PATH").replace(/\s+/g, " ").trim();
|
|
20
|
-
if (pattern === repeatPattern) {
|
|
21
|
-
repeatCount++;
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
if (repeatCount > 2) {
|
|
25
|
-
result.push(` ... (${repeatCount} similar lines)`);
|
|
26
|
-
}
|
|
27
|
-
else if (repeatCount > 0) {
|
|
28
|
-
for (let j = i - repeatCount; j < i; j++) {
|
|
29
|
-
result.push(lines[j]);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
result.push(line);
|
|
33
|
-
repeatPattern = pattern;
|
|
34
|
-
repeatCount = 0;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
if (repeatCount > 2) {
|
|
38
|
-
result.push(` ... (${repeatCount} similar lines)`);
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
for (let j = lines.length - repeatCount; j < lines.length; j++) {
|
|
42
|
-
result.push(lines[j]);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
/** Smart truncation: keep first 60% + last 40% of lines */
|
|
48
|
-
function smartTruncate(text, maxTokens) {
|
|
49
|
-
const lines = text.split("\n");
|
|
50
|
-
const currentTokens = estimateTokens(text);
|
|
51
|
-
if (currentTokens <= maxTokens)
|
|
52
|
-
return text;
|
|
53
|
-
const targetLines = Math.floor((maxTokens * lines.length) / currentTokens);
|
|
54
|
-
const firstCount = Math.ceil(targetLines * 0.6);
|
|
55
|
-
const lastCount = Math.floor(targetLines * 0.4);
|
|
56
|
-
if (firstCount + lastCount >= lines.length)
|
|
57
|
-
return text;
|
|
58
|
-
const first = lines.slice(0, firstCount);
|
|
59
|
-
const last = lines.slice(-lastCount);
|
|
60
|
-
const hiddenCount = lines.length - firstCount - lastCount;
|
|
61
|
-
return [...first, `\n--- ${hiddenCount} lines hidden ---\n`, ...last].join("\n");
|
|
62
|
-
}
|
|
63
|
-
/** Compress command output — ANSI strip, dedup, truncate. No parsing. */
|
|
64
|
-
export function compress(command, output, options = {}) {
|
|
65
|
-
const { maxTokens, stripAnsi: doStrip = true } = options;
|
|
66
|
-
const originalTokens = estimateTokens(output);
|
|
67
|
-
// Short output — no-op, skip all processing
|
|
68
|
-
if (output.split("\n").length <= 20 && !maxTokens) {
|
|
69
|
-
const clean = doStrip ? stripAnsi(output) : output;
|
|
70
|
-
const cleanTokens = estimateTokens(clean);
|
|
71
|
-
return { content: clean, originalTokens, compressedTokens: cleanTokens, tokensSaved: Math.max(0, originalTokens - cleanTokens), savingsPercent: 0 };
|
|
72
|
-
}
|
|
73
|
-
// Step 1: Strip ANSI codes
|
|
74
|
-
let text = doStrip ? stripAnsi(output) : output;
|
|
75
|
-
// Step 2: Deduplicate similar lines
|
|
76
|
-
const lines = text.split("\n");
|
|
77
|
-
const deduped = deduplicateLines(lines);
|
|
78
|
-
text = deduped.join("\n");
|
|
79
|
-
// Step 3: Smart truncation if over budget
|
|
80
|
-
if (maxTokens) {
|
|
81
|
-
text = smartTruncate(text, maxTokens);
|
|
82
|
-
}
|
|
83
|
-
const compressedTokens = estimateTokens(text);
|
|
84
|
-
return {
|
|
85
|
-
content: text,
|
|
86
|
-
originalTokens,
|
|
87
|
-
compressedTokens,
|
|
88
|
-
tokensSaved: Math.max(0, originalTokens - compressedTokens),
|
|
89
|
-
savingsPercent: originalTokens > 0 ? Math.round(((originalTokens - compressedTokens) / originalTokens) * 100) : 0,
|
|
90
|
-
};
|
|
91
|
-
}
|
package/dist/context-hints.js
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
// Context hints — discover context via lightweight checks, inject into AI prompt
|
|
2
|
-
// Regex DISCOVERS, AI DECIDES. No hardcoded logic that makes decisions.
|
|
3
|
-
import { existsSync, readFileSync, readdirSync } from "fs";
|
|
4
|
-
import { join } from "path";
|
|
5
|
-
/** Discover project context from the filesystem */
|
|
6
|
-
export function discoverProjectHints(cwd) {
|
|
7
|
-
const hints = [];
|
|
8
|
-
// Package managers and project files
|
|
9
|
-
const projectFiles = [
|
|
10
|
-
["package.json", "Node.js/TypeScript"],
|
|
11
|
-
["pyproject.toml", "Python"],
|
|
12
|
-
["requirements.txt", "Python"],
|
|
13
|
-
["go.mod", "Go"],
|
|
14
|
-
["Cargo.toml", "Rust"],
|
|
15
|
-
["pom.xml", "Java/Maven"],
|
|
16
|
-
["build.gradle", "Java/Gradle"],
|
|
17
|
-
["build.gradle.kts", "Java/Gradle (Kotlin DSL)"],
|
|
18
|
-
["Makefile", "Has Makefile"],
|
|
19
|
-
["Dockerfile", "Has Docker"],
|
|
20
|
-
["docker-compose.yml", "Has Docker Compose"],
|
|
21
|
-
["docker-compose.yaml", "Has Docker Compose"],
|
|
22
|
-
[".github/workflows", "Has GitHub Actions CI"],
|
|
23
|
-
["Gemfile", "Ruby"],
|
|
24
|
-
["composer.json", "PHP"],
|
|
25
|
-
["mix.exs", "Elixir"],
|
|
26
|
-
["build.zig", "Zig"],
|
|
27
|
-
["CMakeLists.txt", "C/C++ (CMake)"],
|
|
28
|
-
];
|
|
29
|
-
for (const [file, lang] of projectFiles) {
|
|
30
|
-
if (existsSync(join(cwd, file))) {
|
|
31
|
-
hints.push(`Project type: ${lang} (${file} found)`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
// Extract metadata from package.json — trimmed to save tokens
|
|
35
|
-
const pkgPath = join(cwd, "package.json");
|
|
36
|
-
if (existsSync(pkgPath)) {
|
|
37
|
-
try {
|
|
38
|
-
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
39
|
-
if (pkg.name)
|
|
40
|
-
hints.push(`Package: ${pkg.name}@${pkg.version ?? "?"}`);
|
|
41
|
-
if (pkg.scripts) {
|
|
42
|
-
// Only top-5 most useful scripts
|
|
43
|
-
const priority = ["dev", "build", "test", "lint", "start", "typecheck", "check"];
|
|
44
|
-
const scripts = Object.keys(pkg.scripts);
|
|
45
|
-
const top = priority.filter(s => scripts.includes(s));
|
|
46
|
-
const rest = scripts.filter(s => !priority.includes(s)).slice(0, Math.max(0, 5 - top.length));
|
|
47
|
-
hints.push(`Scripts: ${[...top, ...rest].join(", ")}`);
|
|
48
|
-
}
|
|
49
|
-
if (pkg.dependencies) {
|
|
50
|
-
// Only framework/major deps — skip utility libs
|
|
51
|
-
const major = ["react", "next", "express", "fastify", "hono", "vue", "angular", "svelte",
|
|
52
|
-
"prisma", "drizzle", "mongoose", "typeorm", "zod", "trpc", "graphql", "tailwindcss",
|
|
53
|
-
"electron", "bun", "elysia", "nest", "nuxt", "remix", "astro", "vite"];
|
|
54
|
-
const deps = Object.keys(pkg.dependencies);
|
|
55
|
-
const found = deps.filter(d => major.some(m => d.includes(m)));
|
|
56
|
-
if (found.length > 0)
|
|
57
|
-
hints.push(`Key deps: ${found.slice(0, 10).join(", ")}`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
catch { }
|
|
61
|
-
}
|
|
62
|
-
// Extract from pyproject.toml
|
|
63
|
-
const pyPath = join(cwd, "pyproject.toml");
|
|
64
|
-
if (existsSync(pyPath)) {
|
|
65
|
-
try {
|
|
66
|
-
const py = readFileSync(pyPath, "utf8");
|
|
67
|
-
const name = py.match(/name\s*=\s*"([^"]+)"/)?.[1];
|
|
68
|
-
if (name)
|
|
69
|
-
hints.push(`Python package: ${name}`);
|
|
70
|
-
}
|
|
71
|
-
catch { }
|
|
72
|
-
}
|
|
73
|
-
// Extract from go.mod
|
|
74
|
-
const goPath = join(cwd, "go.mod");
|
|
75
|
-
if (existsSync(goPath)) {
|
|
76
|
-
try {
|
|
77
|
-
const go = readFileSync(goPath, "utf8");
|
|
78
|
-
const mod = go.match(/module\s+(\S+)/)?.[1];
|
|
79
|
-
if (mod)
|
|
80
|
-
hints.push(`Go module: ${mod}`);
|
|
81
|
-
}
|
|
82
|
-
catch { }
|
|
83
|
-
}
|
|
84
|
-
// Extract from Cargo.toml
|
|
85
|
-
const cargoPath = join(cwd, "Cargo.toml");
|
|
86
|
-
if (existsSync(cargoPath)) {
|
|
87
|
-
try {
|
|
88
|
-
const cargo = readFileSync(cargoPath, "utf8");
|
|
89
|
-
const name = cargo.match(/name\s*=\s*"([^"]+)"/)?.[1];
|
|
90
|
-
if (name)
|
|
91
|
-
hints.push(`Rust crate: ${name}`);
|
|
92
|
-
}
|
|
93
|
-
catch { }
|
|
94
|
-
}
|
|
95
|
-
// Monorepo detection
|
|
96
|
-
if (existsSync(join(cwd, "packages"))) {
|
|
97
|
-
try {
|
|
98
|
-
const pkgs = readdirSync(join(cwd, "packages")).filter(d => !d.startsWith("."));
|
|
99
|
-
hints.push(`MONOREPO: ${pkgs.length} packages in packages/ — search packages/ not src/`);
|
|
100
|
-
hints.push(`Packages: ${pkgs.slice(0, 10).join(", ")}`);
|
|
101
|
-
}
|
|
102
|
-
catch { }
|
|
103
|
-
}
|
|
104
|
-
if (existsSync(join(cwd, "apps"))) {
|
|
105
|
-
hints.push("MONOREPO: apps/ directory detected");
|
|
106
|
-
}
|
|
107
|
-
// Makefile targets
|
|
108
|
-
if (existsSync(join(cwd, "Makefile"))) {
|
|
109
|
-
try {
|
|
110
|
-
const { execSync } = require("child_process");
|
|
111
|
-
const targets = execSync("grep -E '^[a-zA-Z_-]+:' Makefile | head -10 | cut -d: -f1", { cwd, encoding: "utf8", timeout: 1000 }).trim();
|
|
112
|
-
if (targets)
|
|
113
|
-
hints.push(`Makefile targets: ${targets.split("\n").join(", ")}`);
|
|
114
|
-
}
|
|
115
|
-
catch { }
|
|
116
|
-
}
|
|
117
|
-
// Source directory structure — max 20 files to save tokens
|
|
118
|
-
try {
|
|
119
|
-
const { execSync } = require("child_process");
|
|
120
|
-
const srcDirs = ["src", "lib", "app", "packages"];
|
|
121
|
-
for (const dir of srcDirs) {
|
|
122
|
-
if (existsSync(join(cwd, dir))) {
|
|
123
|
-
const tree = execSync(`find ${dir} -maxdepth 2 -not -path '*/node_modules/*' -not -path '*/dist/*' -not -name '*.test.*' -not -name '*.spec.*' 2>/dev/null | sort | head -20`, { cwd, encoding: "utf8", timeout: 2000 }).trim();
|
|
124
|
-
if (tree)
|
|
125
|
-
hints.push(`Files in ${dir}/:\n${tree}`);
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
catch { }
|
|
131
|
-
return hints;
|
|
132
|
-
}
|
|
133
|
-
/** Discover output-specific hints (observations about command output) */
|
|
134
|
-
export function discoverOutputHints(output, command) {
|
|
135
|
-
const hints = [];
|
|
136
|
-
const lines = output.split("\n");
|
|
137
|
-
hints.push(`Output: ${lines.length} lines, ${output.length} chars`);
|
|
138
|
-
// Only detect test results from actual test runners (not grep output containing "pass"/"fail" in code)
|
|
139
|
-
const isGrepOutput = /^\s*(src\/|\.\/|packages\/).*:\d+:/.test(output);
|
|
140
|
-
if (!isGrepOutput) {
|
|
141
|
-
const passMatch = output.match(/(\d+)\s+pass(?:ed|ing)?\b/i);
|
|
142
|
-
const failMatch = output.match(/(\d+)\s+fail(?:ed|ing|ure)?\b/i);
|
|
143
|
-
if (passMatch)
|
|
144
|
-
hints.push(`Test results detected: ${passMatch[0]}`);
|
|
145
|
-
if (failMatch)
|
|
146
|
-
hints.push(`Test results detected: ${failMatch[0]}`);
|
|
147
|
-
// Error patterns (only from actual command output, not code search)
|
|
148
|
-
if (output.match(/error\s*TS\d+/i))
|
|
149
|
-
hints.push("TypeScript errors detected in output");
|
|
150
|
-
if (output.match(/ENOENT|EACCES|EADDRINUSE/))
|
|
151
|
-
hints.push("System error code detected in output");
|
|
152
|
-
}
|
|
153
|
-
// Coverage patterns
|
|
154
|
-
if (output.match(/%\s*Funcs|%\s*Lines|coverage/i))
|
|
155
|
-
hints.push("Code coverage data detected in output");
|
|
156
|
-
// Large/repetitive output
|
|
157
|
-
if (lines.length > 100)
|
|
158
|
-
hints.push(`Large output (${lines.length} lines) — consider summarizing`);
|
|
159
|
-
const uniqueLines = new Set(lines.map(l => l.trim())).size;
|
|
160
|
-
if (uniqueLines < lines.length * 0.5)
|
|
161
|
-
hints.push("Output has many duplicate/similar lines");
|
|
162
|
-
// Sensitive data (only env var assignments, not code containing the word KEY/TOKEN)
|
|
163
|
-
if (output.match(/^[A-Z_]+(KEY|TOKEN|SECRET|PASSWORD)\s*=\s*\S+/m))
|
|
164
|
-
hints.push("Output may contain sensitive data — redact credentials");
|
|
165
|
-
// Error block extraction — state machine that captures multi-line errors
|
|
166
|
-
if (!isGrepOutput) {
|
|
167
|
-
const errorBlocks = extractErrorBlocks(output);
|
|
168
|
-
if (errorBlocks.length > 0) {
|
|
169
|
-
const summary = errorBlocks.slice(0, 3).map(b => b.trim().split("\n").slice(0, 5).join("\n")).join("\n---\n");
|
|
170
|
-
hints.push(`ERROR BLOCKS FOUND (${errorBlocks.length}):\n${summary}`);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
return hints;
|
|
174
|
-
}
|
|
175
|
-
/** Extract multi-line error blocks using a state machine */
|
|
176
|
-
function extractErrorBlocks(output) {
|
|
177
|
-
const lines = output.split("\n");
|
|
178
|
-
const blocks = [];
|
|
179
|
-
let currentBlock = [];
|
|
180
|
-
let inErrorBlock = false;
|
|
181
|
-
let blankCount = 0;
|
|
182
|
-
// Patterns that START an error block
|
|
183
|
-
const errorStarters = [
|
|
184
|
-
/^error/i, /^Error:/i, /^ERROR/,
|
|
185
|
-
/^Traceback/i, /^panic:/i, /^fatal:/i,
|
|
186
|
-
/^FAIL/i, /^✗/, /^✘/,
|
|
187
|
-
/error\s*TS\d+/i, /error\[E\d+\]/,
|
|
188
|
-
/^SyntaxError/i, /^TypeError/i, /^ReferenceError/i,
|
|
189
|
-
/^Unhandled/i, /^Exception/i,
|
|
190
|
-
/ENOENT|EACCES|EADDRINUSE|ECONNREFUSED/,
|
|
191
|
-
];
|
|
192
|
-
for (const line of lines) {
|
|
193
|
-
const trimmed = line.trim();
|
|
194
|
-
if (!trimmed) {
|
|
195
|
-
blankCount++;
|
|
196
|
-
if (inErrorBlock) {
|
|
197
|
-
currentBlock.push(line);
|
|
198
|
-
// 2+ blank lines = end of error block
|
|
199
|
-
if (blankCount >= 2) {
|
|
200
|
-
blocks.push(currentBlock.join("\n").trim());
|
|
201
|
-
currentBlock = [];
|
|
202
|
-
inErrorBlock = false;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
continue;
|
|
206
|
-
}
|
|
207
|
-
blankCount = 0;
|
|
208
|
-
// Check if this line starts a new error block
|
|
209
|
-
if (!inErrorBlock && errorStarters.some(p => p.test(trimmed))) {
|
|
210
|
-
inErrorBlock = true;
|
|
211
|
-
currentBlock = [line];
|
|
212
|
-
continue;
|
|
213
|
-
}
|
|
214
|
-
if (inErrorBlock) {
|
|
215
|
-
// Continuation: indented lines, "at ..." stack frames, "--->" pointers, "File ..." python traces
|
|
216
|
-
const isContinuation = /^\s+/.test(line) ||
|
|
217
|
-
/^\s*at\s/.test(trimmed) ||
|
|
218
|
-
/^\s*-+>/.test(trimmed) ||
|
|
219
|
-
/^\s*\|/.test(trimmed) ||
|
|
220
|
-
/^\s*File "/.test(trimmed) ||
|
|
221
|
-
/^\s*\d+\s*\|/.test(trimmed) || // rust/compiler line numbers
|
|
222
|
-
/^Caused by:/i.test(trimmed);
|
|
223
|
-
if (isContinuation) {
|
|
224
|
-
currentBlock.push(line);
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
// Non-continuation, non-blank = end of error block
|
|
228
|
-
blocks.push(currentBlock.join("\n").trim());
|
|
229
|
-
currentBlock = [];
|
|
230
|
-
inErrorBlock = false;
|
|
231
|
-
// Check if THIS line starts a new error block
|
|
232
|
-
if (errorStarters.some(p => p.test(trimmed))) {
|
|
233
|
-
inErrorBlock = true;
|
|
234
|
-
currentBlock = [line];
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
// Flush remaining block
|
|
240
|
-
if (currentBlock.length > 0) {
|
|
241
|
-
blocks.push(currentBlock.join("\n").trim());
|
|
242
|
-
}
|
|
243
|
-
return blocks;
|
|
244
|
-
}
|
|
245
|
-
/** Discover safety hints about a command */
|
|
246
|
-
export function discoverSafetyHints(command) {
|
|
247
|
-
const hints = [];
|
|
248
|
-
// Observations about the command (AI decides if it's safe)
|
|
249
|
-
if (command.match(/\brm\b|\brmdir\b|\btruncate\b/))
|
|
250
|
-
hints.push("SAFETY: command contains file deletion (rm/rmdir/truncate)");
|
|
251
|
-
if (command.match(/\bkill\b|\bkillall\b|\bpkill\b/))
|
|
252
|
-
hints.push("SAFETY: command kills processes");
|
|
253
|
-
if (command.match(/\bgit\s+push\b|\bgit\s+reset\s+--hard\b/))
|
|
254
|
-
hints.push("SAFETY: command pushes/resets git");
|
|
255
|
-
if (command.match(/\bnpx\b|\bnpm\s+install\b|\bpip\s+install\b/))
|
|
256
|
-
hints.push("SAFETY: command installs packages");
|
|
257
|
-
if (command.match(/\bsed\s+-i\b|\bcodemod\b/))
|
|
258
|
-
hints.push("SAFETY: command modifies files in-place");
|
|
259
|
-
if (command.match(/\btouch\b|\bmkdir\b/))
|
|
260
|
-
hints.push("SAFETY: command creates files/directories");
|
|
261
|
-
if (command.match(/>\s*\S+\.\w+/))
|
|
262
|
-
hints.push("SAFETY: command writes to a file via redirect");
|
|
263
|
-
if (command.match(/\b(bun|npm|pnpm)\s+run\s+dev\b|\bstart\b/))
|
|
264
|
-
hints.push("SAFETY: command starts a server/process");
|
|
265
|
-
// Read-only observations
|
|
266
|
-
if (command.match(/^\s*git\s+(log|show|diff|status|branch|blame|tag)\b/))
|
|
267
|
-
hints.push("This is a read-only git command");
|
|
268
|
-
if (command.match(/^\s*(ls|cat|head|tail|grep|find|wc|du|df|uptime|whoami|pwd)\b/))
|
|
269
|
-
hints.push("This is a read-only command");
|
|
270
|
-
return hints;
|
|
271
|
-
}
|
|
272
|
-
/** Format all hints for system prompt injection */
|
|
273
|
-
export function formatHints(project, output, safety) {
|
|
274
|
-
const sections = [];
|
|
275
|
-
if (project.length > 0) {
|
|
276
|
-
sections.push("PROJECT CONTEXT:\n" + project.join("\n"));
|
|
277
|
-
}
|
|
278
|
-
if (output && output.length > 0) {
|
|
279
|
-
sections.push("OUTPUT OBSERVATIONS:\n" + output.join("\n"));
|
|
280
|
-
}
|
|
281
|
-
if (safety && safety.length > 0) {
|
|
282
|
-
sections.push("SAFETY OBSERVATIONS:\n" + safety.join("\n"));
|
|
283
|
-
}
|
|
284
|
-
return sections.join("\n\n");
|
|
285
|
-
}
|
package/dist/diff-cache.js
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
// Diff-aware output caching — when same command runs again, return only what changed
|
|
2
|
-
import { estimateTokens } from "./tokens.js";
|
|
3
|
-
const cache = new Map();
|
|
4
|
-
function cacheKey(command, cwd) {
|
|
5
|
-
return `${cwd}:${command}`;
|
|
6
|
-
}
|
|
7
|
-
/** Compute a simple line diff between two outputs */
|
|
8
|
-
function lineDiff(prev, curr) {
|
|
9
|
-
const prevLines = new Set(prev.split("\n"));
|
|
10
|
-
const currLines = curr.split("\n");
|
|
11
|
-
const added = [];
|
|
12
|
-
const removed = [];
|
|
13
|
-
let unchanged = 0;
|
|
14
|
-
for (const line of currLines) {
|
|
15
|
-
if (prevLines.has(line)) {
|
|
16
|
-
unchanged++;
|
|
17
|
-
prevLines.delete(line);
|
|
18
|
-
}
|
|
19
|
-
else {
|
|
20
|
-
added.push(line);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
for (const line of prevLines) {
|
|
24
|
-
removed.push(line);
|
|
25
|
-
}
|
|
26
|
-
return { added, removed, unchanged };
|
|
27
|
-
}
|
|
28
|
-
/** Generate a human-readable diff summary */
|
|
29
|
-
function summarizeDiff(diff) {
|
|
30
|
-
const parts = [];
|
|
31
|
-
if (diff.added.length > 0)
|
|
32
|
-
parts.push(`+${diff.added.length} new lines`);
|
|
33
|
-
if (diff.removed.length > 0)
|
|
34
|
-
parts.push(`-${diff.removed.length} removed lines`);
|
|
35
|
-
parts.push(`${diff.unchanged} unchanged`);
|
|
36
|
-
return parts.join(", ");
|
|
37
|
-
}
|
|
38
|
-
/** Run diffing on command output. Caches the output for next comparison. */
|
|
39
|
-
export function diffOutput(command, cwd, output) {
|
|
40
|
-
const key = cacheKey(command, cwd);
|
|
41
|
-
const prev = cache.get(key);
|
|
42
|
-
// Store current for next time
|
|
43
|
-
cache.set(key, { command, cwd, output, timestamp: Date.now() });
|
|
44
|
-
if (!prev) {
|
|
45
|
-
return {
|
|
46
|
-
full: output,
|
|
47
|
-
hasPrevious: false,
|
|
48
|
-
added: [],
|
|
49
|
-
removed: [],
|
|
50
|
-
diffSummary: "first run",
|
|
51
|
-
unchanged: false,
|
|
52
|
-
tokensSaved: 0,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
if (prev.output === output) {
|
|
56
|
-
const fullTokens = estimateTokens(output);
|
|
57
|
-
return {
|
|
58
|
-
full: output,
|
|
59
|
-
hasPrevious: true,
|
|
60
|
-
added: [],
|
|
61
|
-
removed: [],
|
|
62
|
-
diffSummary: "identical to previous run",
|
|
63
|
-
unchanged: true,
|
|
64
|
-
tokensSaved: fullTokens - 10, // ~10 tokens for the "unchanged" message
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
const diff = lineDiff(prev.output, output);
|
|
68
|
-
const total = diff.added.length + diff.removed.length + diff.unchanged;
|
|
69
|
-
const similarity = total > 0 ? diff.unchanged / total : 0;
|
|
70
|
-
// Fuzzy threshold: if >80% similar, return diff-only (massive token savings)
|
|
71
|
-
const fullTokens = estimateTokens(output);
|
|
72
|
-
if (similarity > 0.8 && diff.added.length + diff.removed.length > 0) {
|
|
73
|
-
const diffContent = [
|
|
74
|
-
...diff.added.map(l => `+ ${l}`),
|
|
75
|
-
...diff.removed.map(l => `- ${l}`),
|
|
76
|
-
].join("\n");
|
|
77
|
-
const diffTokens = estimateTokens(diffContent);
|
|
78
|
-
return {
|
|
79
|
-
full: output,
|
|
80
|
-
hasPrevious: true,
|
|
81
|
-
added: diff.added,
|
|
82
|
-
removed: diff.removed,
|
|
83
|
-
diffSummary: `${Math.round(similarity * 100)}% similar — ${summarizeDiff(diff)}`,
|
|
84
|
-
unchanged: false,
|
|
85
|
-
tokensSaved: Math.max(0, fullTokens - diffTokens),
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
// Less than 80% similar — return full output with diff info
|
|
89
|
-
const diffContent = [
|
|
90
|
-
...diff.added.map(l => `+ ${l}`),
|
|
91
|
-
...diff.removed.map(l => `- ${l}`),
|
|
92
|
-
].join("\n");
|
|
93
|
-
const diffTokens = estimateTokens(diffContent);
|
|
94
|
-
return {
|
|
95
|
-
full: output,
|
|
96
|
-
hasPrevious: true,
|
|
97
|
-
added: diff.added,
|
|
98
|
-
removed: diff.removed,
|
|
99
|
-
diffSummary: summarizeDiff(diff),
|
|
100
|
-
unchanged: false,
|
|
101
|
-
tokensSaved: Math.max(0, fullTokens - diffTokens),
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
/** Clear the diff cache */
|
|
105
|
-
export function clearDiffCache() {
|
|
106
|
-
cache.clear();
|
|
107
|
-
}
|