@hasna/terminal 2.3.0 → 2.3.2
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/App.js +404 -0
- package/dist/Browse.js +79 -0
- package/dist/FuzzyPicker.js +47 -0
- package/dist/Onboarding.js +51 -0
- package/dist/Spinner.js +12 -0
- package/dist/StatusBar.js +49 -0
- package/dist/ai.js +322 -0
- package/dist/cache.js +41 -0
- package/dist/cli.js +64 -16
- package/dist/command-rewriter.js +64 -0
- package/dist/command-validator.js +86 -0
- package/dist/compression.js +107 -0
- package/dist/context-hints.js +275 -0
- package/dist/diff-cache.js +107 -0
- package/dist/discover.js +212 -0
- package/dist/economy.js +123 -0
- package/dist/expand-store.js +38 -0
- package/dist/file-cache.js +72 -0
- package/dist/file-index.js +62 -0
- package/dist/history.js +62 -0
- package/dist/lazy-executor.js +54 -0
- package/dist/line-dedup.js +59 -0
- package/dist/loop-detector.js +75 -0
- package/dist/mcp/install.js +98 -0
- package/dist/mcp/server.js +569 -0
- package/dist/noise-filter.js +86 -0
- package/dist/output-processor.js +129 -0
- package/dist/output-router.js +41 -0
- package/dist/output-store.js +111 -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 +99 -0
- package/dist/parsers/index.js +48 -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/groq.js +95 -0
- package/dist/providers/index.js +73 -0
- package/dist/providers/xai.js +95 -0
- package/dist/recipes/model.js +20 -0
- package/dist/recipes/storage.js +136 -0
- package/dist/search/content-search.js +68 -0
- package/dist/search/file-search.js +61 -0
- package/dist/search/filters.js +34 -0
- package/dist/search/index.js +5 -0
- package/dist/search/semantic.js +320 -0
- package/dist/session-boot.js +59 -0
- package/dist/session-context.js +55 -0
- package/dist/sessions-db.js +173 -0
- package/dist/smart-display.js +286 -0
- package/dist/snapshots.js +51 -0
- package/dist/supervisor.js +112 -0
- package/dist/test-watchlist.js +131 -0
- package/dist/tool-profiles.js +122 -0
- package/dist/tree.js +94 -0
- package/dist/usage-cache.js +65 -0
- package/package.json +8 -1
- package/src/ai.ts +8 -0
- package/src/cli.tsx +57 -18
- package/src/output-processor.ts +6 -1
- package/src/output-store.ts +58 -12
- package/src/tool-profiles.ts +139 -0
- package/.claude/scheduled_tasks.lock +0 -1
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -20
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -14
- package/CONTRIBUTING.md +0 -80
- package/benchmarks/benchmark.mjs +0 -115
- package/imported_modules.txt +0 -0
- package/temp/rtk/.claude/agents/code-reviewer.md +0 -221
- package/temp/rtk/.claude/agents/debugger.md +0 -519
- package/temp/rtk/.claude/agents/rtk-testing-specialist.md +0 -461
- package/temp/rtk/.claude/agents/rust-rtk.md +0 -511
- package/temp/rtk/.claude/agents/technical-writer.md +0 -355
- package/temp/rtk/.claude/commands/diagnose.md +0 -352
- package/temp/rtk/.claude/commands/test-routing.md +0 -362
- package/temp/rtk/.claude/hooks/bash/pre-commit-format.sh +0 -16
- package/temp/rtk/.claude/hooks/rtk-rewrite.sh +0 -70
- package/temp/rtk/.claude/hooks/rtk-suggest.sh +0 -152
- package/temp/rtk/.claude/rules/cli-testing.md +0 -526
- package/temp/rtk/.claude/skills/issue-triage/SKILL.md +0 -348
- package/temp/rtk/.claude/skills/issue-triage/templates/issue-comment.md +0 -134
- package/temp/rtk/.claude/skills/performance.md +0 -435
- package/temp/rtk/.claude/skills/pr-triage/SKILL.md +0 -315
- package/temp/rtk/.claude/skills/pr-triage/templates/review-comment.md +0 -71
- package/temp/rtk/.claude/skills/repo-recap.md +0 -206
- package/temp/rtk/.claude/skills/rtk-tdd/SKILL.md +0 -78
- package/temp/rtk/.claude/skills/rtk-tdd/references/testing-patterns.md +0 -124
- package/temp/rtk/.claude/skills/security-guardian.md +0 -503
- package/temp/rtk/.claude/skills/ship.md +0 -404
- package/temp/rtk/.github/workflows/benchmark.yml +0 -34
- package/temp/rtk/.github/workflows/dco-check.yaml +0 -12
- package/temp/rtk/.github/workflows/release-please.yml +0 -51
- package/temp/rtk/.github/workflows/release.yml +0 -343
- package/temp/rtk/.github/workflows/security-check.yml +0 -135
- package/temp/rtk/.github/workflows/validate-docs.yml +0 -78
- package/temp/rtk/.release-please-manifest.json +0 -3
- package/temp/rtk/ARCHITECTURE.md +0 -1491
- package/temp/rtk/CHANGELOG.md +0 -640
- package/temp/rtk/CLAUDE.md +0 -605
- package/temp/rtk/CONTRIBUTING.md +0 -199
- package/temp/rtk/Cargo.lock +0 -1668
- package/temp/rtk/Cargo.toml +0 -64
- package/temp/rtk/Formula/rtk.rb +0 -43
- package/temp/rtk/INSTALL.md +0 -390
- package/temp/rtk/LICENSE +0 -21
- package/temp/rtk/README.md +0 -386
- package/temp/rtk/README_es.md +0 -159
- package/temp/rtk/README_fr.md +0 -197
- package/temp/rtk/README_ja.md +0 -159
- package/temp/rtk/README_ko.md +0 -159
- package/temp/rtk/README_zh.md +0 -167
- package/temp/rtk/ROADMAP.md +0 -15
- package/temp/rtk/SECURITY.md +0 -217
- package/temp/rtk/TEST_EXEC_TIME.md +0 -102
- package/temp/rtk/build.rs +0 -57
- package/temp/rtk/docs/AUDIT_GUIDE.md +0 -432
- package/temp/rtk/docs/FEATURES.md +0 -1410
- package/temp/rtk/docs/TROUBLESHOOTING.md +0 -309
- package/temp/rtk/docs/filter-workflow.md +0 -102
- package/temp/rtk/docs/images/gain-dashboard.jpg +0 -0
- package/temp/rtk/docs/tracking.md +0 -583
- package/temp/rtk/hooks/opencode-rtk.ts +0 -39
- package/temp/rtk/hooks/rtk-awareness.md +0 -29
- package/temp/rtk/hooks/rtk-rewrite.sh +0 -61
- package/temp/rtk/hooks/test-rtk-rewrite.sh +0 -442
- package/temp/rtk/install.sh +0 -124
- package/temp/rtk/release-please-config.json +0 -10
- package/temp/rtk/scripts/benchmark.sh +0 -592
- package/temp/rtk/scripts/check-installation.sh +0 -162
- package/temp/rtk/scripts/install-local.sh +0 -37
- package/temp/rtk/scripts/rtk-economics.sh +0 -137
- package/temp/rtk/scripts/test-all.sh +0 -561
- package/temp/rtk/scripts/test-aristote.sh +0 -227
- package/temp/rtk/scripts/test-tracking.sh +0 -79
- package/temp/rtk/scripts/update-readme-metrics.sh +0 -32
- package/temp/rtk/scripts/validate-docs.sh +0 -73
- package/temp/rtk/src/aws_cmd.rs +0 -880
- package/temp/rtk/src/binlog.rs +0 -1645
- package/temp/rtk/src/cargo_cmd.rs +0 -1727
- package/temp/rtk/src/cc_economics.rs +0 -1157
- package/temp/rtk/src/ccusage.rs +0 -340
- package/temp/rtk/src/config.rs +0 -187
- package/temp/rtk/src/container.rs +0 -855
- package/temp/rtk/src/curl_cmd.rs +0 -134
- package/temp/rtk/src/deps.rs +0 -268
- package/temp/rtk/src/diff_cmd.rs +0 -367
- package/temp/rtk/src/discover/mod.rs +0 -274
- package/temp/rtk/src/discover/provider.rs +0 -388
- package/temp/rtk/src/discover/registry.rs +0 -2022
- package/temp/rtk/src/discover/report.rs +0 -202
- package/temp/rtk/src/discover/rules.rs +0 -667
- package/temp/rtk/src/display_helpers.rs +0 -402
- package/temp/rtk/src/dotnet_cmd.rs +0 -1771
- package/temp/rtk/src/dotnet_format_report.rs +0 -133
- package/temp/rtk/src/dotnet_trx.rs +0 -593
- package/temp/rtk/src/env_cmd.rs +0 -204
- package/temp/rtk/src/filter.rs +0 -462
- package/temp/rtk/src/filters/README.md +0 -52
- package/temp/rtk/src/filters/ansible-playbook.toml +0 -34
- package/temp/rtk/src/filters/basedpyright.toml +0 -47
- package/temp/rtk/src/filters/biome.toml +0 -45
- package/temp/rtk/src/filters/brew-install.toml +0 -37
- package/temp/rtk/src/filters/composer-install.toml +0 -40
- package/temp/rtk/src/filters/df.toml +0 -16
- package/temp/rtk/src/filters/dotnet-build.toml +0 -64
- package/temp/rtk/src/filters/du.toml +0 -16
- package/temp/rtk/src/filters/fail2ban-client.toml +0 -15
- package/temp/rtk/src/filters/gcc.toml +0 -49
- package/temp/rtk/src/filters/gcloud.toml +0 -22
- package/temp/rtk/src/filters/hadolint.toml +0 -24
- package/temp/rtk/src/filters/helm.toml +0 -29
- package/temp/rtk/src/filters/iptables.toml +0 -27
- package/temp/rtk/src/filters/jj.toml +0 -28
- package/temp/rtk/src/filters/jq.toml +0 -24
- package/temp/rtk/src/filters/make.toml +0 -41
- package/temp/rtk/src/filters/markdownlint.toml +0 -24
- package/temp/rtk/src/filters/mix-compile.toml +0 -27
- package/temp/rtk/src/filters/mix-format.toml +0 -15
- package/temp/rtk/src/filters/mvn-build.toml +0 -44
- package/temp/rtk/src/filters/oxlint.toml +0 -43
- package/temp/rtk/src/filters/ping.toml +0 -63
- package/temp/rtk/src/filters/pio-run.toml +0 -40
- package/temp/rtk/src/filters/poetry-install.toml +0 -50
- package/temp/rtk/src/filters/pre-commit.toml +0 -35
- package/temp/rtk/src/filters/ps.toml +0 -16
- package/temp/rtk/src/filters/quarto-render.toml +0 -41
- package/temp/rtk/src/filters/rsync.toml +0 -48
- package/temp/rtk/src/filters/shellcheck.toml +0 -27
- package/temp/rtk/src/filters/shopify-theme.toml +0 -29
- package/temp/rtk/src/filters/skopeo.toml +0 -45
- package/temp/rtk/src/filters/sops.toml +0 -16
- package/temp/rtk/src/filters/ssh.toml +0 -44
- package/temp/rtk/src/filters/stat.toml +0 -34
- package/temp/rtk/src/filters/swift-build.toml +0 -41
- package/temp/rtk/src/filters/systemctl-status.toml +0 -33
- package/temp/rtk/src/filters/terraform-plan.toml +0 -35
- package/temp/rtk/src/filters/tofu-fmt.toml +0 -16
- package/temp/rtk/src/filters/tofu-init.toml +0 -38
- package/temp/rtk/src/filters/tofu-plan.toml +0 -35
- package/temp/rtk/src/filters/tofu-validate.toml +0 -17
- package/temp/rtk/src/filters/trunk-build.toml +0 -39
- package/temp/rtk/src/filters/ty.toml +0 -50
- package/temp/rtk/src/filters/uv-sync.toml +0 -37
- package/temp/rtk/src/filters/xcodebuild.toml +0 -99
- package/temp/rtk/src/filters/yamllint.toml +0 -25
- package/temp/rtk/src/find_cmd.rs +0 -598
- package/temp/rtk/src/format_cmd.rs +0 -386
- package/temp/rtk/src/gain.rs +0 -723
- package/temp/rtk/src/gh_cmd.rs +0 -1651
- package/temp/rtk/src/git.rs +0 -2012
- package/temp/rtk/src/go_cmd.rs +0 -592
- package/temp/rtk/src/golangci_cmd.rs +0 -254
- package/temp/rtk/src/grep_cmd.rs +0 -288
- package/temp/rtk/src/gt_cmd.rs +0 -810
- package/temp/rtk/src/hook_audit_cmd.rs +0 -283
- package/temp/rtk/src/hook_check.rs +0 -171
- package/temp/rtk/src/init.rs +0 -1859
- package/temp/rtk/src/integrity.rs +0 -537
- package/temp/rtk/src/json_cmd.rs +0 -231
- package/temp/rtk/src/learn/detector.rs +0 -628
- package/temp/rtk/src/learn/mod.rs +0 -119
- package/temp/rtk/src/learn/report.rs +0 -184
- package/temp/rtk/src/lint_cmd.rs +0 -694
- package/temp/rtk/src/local_llm.rs +0 -316
- package/temp/rtk/src/log_cmd.rs +0 -248
- package/temp/rtk/src/ls.rs +0 -324
- package/temp/rtk/src/main.rs +0 -2482
- package/temp/rtk/src/mypy_cmd.rs +0 -389
- package/temp/rtk/src/next_cmd.rs +0 -241
- package/temp/rtk/src/npm_cmd.rs +0 -236
- package/temp/rtk/src/parser/README.md +0 -267
- package/temp/rtk/src/parser/error.rs +0 -46
- package/temp/rtk/src/parser/formatter.rs +0 -336
- package/temp/rtk/src/parser/mod.rs +0 -311
- package/temp/rtk/src/parser/types.rs +0 -119
- package/temp/rtk/src/pip_cmd.rs +0 -302
- package/temp/rtk/src/playwright_cmd.rs +0 -479
- package/temp/rtk/src/pnpm_cmd.rs +0 -573
- package/temp/rtk/src/prettier_cmd.rs +0 -221
- package/temp/rtk/src/prisma_cmd.rs +0 -482
- package/temp/rtk/src/psql_cmd.rs +0 -382
- package/temp/rtk/src/pytest_cmd.rs +0 -384
- package/temp/rtk/src/read.rs +0 -217
- package/temp/rtk/src/rewrite_cmd.rs +0 -50
- package/temp/rtk/src/ruff_cmd.rs +0 -402
- package/temp/rtk/src/runner.rs +0 -271
- package/temp/rtk/src/summary.rs +0 -297
- package/temp/rtk/src/tee.rs +0 -405
- package/temp/rtk/src/telemetry.rs +0 -248
- package/temp/rtk/src/toml_filter.rs +0 -1655
- package/temp/rtk/src/tracking.rs +0 -1416
- package/temp/rtk/src/tree.rs +0 -209
- package/temp/rtk/src/tsc_cmd.rs +0 -259
- package/temp/rtk/src/utils.rs +0 -432
- package/temp/rtk/src/verify_cmd.rs +0 -47
- package/temp/rtk/src/vitest_cmd.rs +0 -385
- package/temp/rtk/src/wc_cmd.rs +0 -401
- package/temp/rtk/src/wget_cmd.rs +0 -260
- package/temp/rtk/tests/fixtures/dotnet/build_failed.txt +0 -11
- package/temp/rtk/tests/fixtures/dotnet/format_changes.json +0 -31
- package/temp/rtk/tests/fixtures/dotnet/format_empty.json +0 -1
- package/temp/rtk/tests/fixtures/dotnet/format_success.json +0 -12
- package/temp/rtk/tests/fixtures/dotnet/test_failed.txt +0 -18
- package/tsconfig.json +0 -15
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Token compression engine — reduces CLI output to fit within token budgets
|
|
2
|
+
import { parseOutput, estimateTokens, tokenSavings } from "./parsers/index.js";
|
|
3
|
+
/** Strip ANSI escape codes from text */
|
|
4
|
+
export function stripAnsi(text) {
|
|
5
|
+
// eslint-disable-next-line no-control-regex
|
|
6
|
+
return text.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "").replace(/\x1b\][^\x07]*\x07/g, "");
|
|
7
|
+
}
|
|
8
|
+
/** Deduplicate consecutive similar lines (e.g., "Compiling X... Compiling Y...") */
|
|
9
|
+
function deduplicateLines(lines) {
|
|
10
|
+
if (lines.length <= 3)
|
|
11
|
+
return lines;
|
|
12
|
+
const result = [];
|
|
13
|
+
let repeatCount = 0;
|
|
14
|
+
let repeatPattern = "";
|
|
15
|
+
for (let i = 0; i < lines.length; i++) {
|
|
16
|
+
const line = lines[i];
|
|
17
|
+
// Extract a "pattern" — the line without numbers, paths, specific identifiers
|
|
18
|
+
const pattern = line.replace(/[0-9]+/g, "N").replace(/\/\S+/g, "/PATH").replace(/\s+/g, " ").trim();
|
|
19
|
+
if (pattern === repeatPattern) {
|
|
20
|
+
repeatCount++;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
if (repeatCount > 2) {
|
|
24
|
+
result.push(` ... (${repeatCount} similar lines)`);
|
|
25
|
+
}
|
|
26
|
+
else if (repeatCount > 0) {
|
|
27
|
+
// Push the skipped lines back
|
|
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 N + last M 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
|
+
// Keep proportional first/last, with first getting more
|
|
54
|
+
const targetLines = Math.floor((maxTokens * lines.length) / currentTokens);
|
|
55
|
+
const firstCount = Math.ceil(targetLines * 0.6);
|
|
56
|
+
const lastCount = Math.floor(targetLines * 0.4);
|
|
57
|
+
if (firstCount + lastCount >= lines.length)
|
|
58
|
+
return text;
|
|
59
|
+
const first = lines.slice(0, firstCount);
|
|
60
|
+
const last = lines.slice(-lastCount);
|
|
61
|
+
const hiddenCount = lines.length - firstCount - lastCount;
|
|
62
|
+
return [...first, `\n--- ${hiddenCount} lines hidden ---\n`, ...last].join("\n");
|
|
63
|
+
}
|
|
64
|
+
/** Compress command output to fit within a token budget */
|
|
65
|
+
export function compress(command, output, options = {}) {
|
|
66
|
+
const { maxTokens, format = "text", stripAnsi: doStrip = true } = options;
|
|
67
|
+
const originalTokens = estimateTokens(output);
|
|
68
|
+
// Step 1: Strip ANSI codes
|
|
69
|
+
let text = doStrip ? stripAnsi(output) : output;
|
|
70
|
+
// Step 2: Try structured parsing (format=json or when it saves tokens)
|
|
71
|
+
if (format === "json" || format === "summary") {
|
|
72
|
+
const parsed = parseOutput(command, text);
|
|
73
|
+
if (parsed) {
|
|
74
|
+
const json = JSON.stringify(parsed.data, null, format === "summary" ? 0 : 2);
|
|
75
|
+
const savings = tokenSavings(output, parsed.data);
|
|
76
|
+
const compressedTokens = estimateTokens(json);
|
|
77
|
+
// ONLY use JSON if it actually saves tokens (never return larger output)
|
|
78
|
+
if (savings.saved > 0 && (!maxTokens || compressedTokens <= maxTokens)) {
|
|
79
|
+
return {
|
|
80
|
+
content: json,
|
|
81
|
+
format: "json",
|
|
82
|
+
originalTokens,
|
|
83
|
+
compressedTokens,
|
|
84
|
+
tokensSaved: savings.saved,
|
|
85
|
+
savingsPercent: savings.percent,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Step 3: Deduplicate similar lines
|
|
91
|
+
const lines = text.split("\n");
|
|
92
|
+
const deduped = deduplicateLines(lines);
|
|
93
|
+
text = deduped.join("\n");
|
|
94
|
+
// Step 4: Smart truncation if over budget
|
|
95
|
+
if (maxTokens) {
|
|
96
|
+
text = smartTruncate(text, maxTokens);
|
|
97
|
+
}
|
|
98
|
+
const compressedTokens = estimateTokens(text);
|
|
99
|
+
return {
|
|
100
|
+
content: text,
|
|
101
|
+
format: "text",
|
|
102
|
+
originalTokens,
|
|
103
|
+
compressedTokens,
|
|
104
|
+
tokensSaved: Math.max(0, originalTokens - compressedTokens),
|
|
105
|
+
savingsPercent: originalTokens > 0 ? Math.round(((originalTokens - compressedTokens) / originalTokens) * 100) : 0,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
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 rich metadata from package.json
|
|
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 name: ${pkg.name}@${pkg.version ?? "unknown"}`);
|
|
41
|
+
if (pkg.scripts) {
|
|
42
|
+
hints.push(`Available scripts: ${Object.entries(pkg.scripts).map(([k, v]) => `${k}: ${v}`).slice(0, 10).join(", ")}`);
|
|
43
|
+
}
|
|
44
|
+
if (pkg.dependencies)
|
|
45
|
+
hints.push(`Dependencies: ${Object.keys(pkg.dependencies).join(", ")}`);
|
|
46
|
+
}
|
|
47
|
+
catch { }
|
|
48
|
+
}
|
|
49
|
+
// Extract from pyproject.toml
|
|
50
|
+
const pyPath = join(cwd, "pyproject.toml");
|
|
51
|
+
if (existsSync(pyPath)) {
|
|
52
|
+
try {
|
|
53
|
+
const py = readFileSync(pyPath, "utf8");
|
|
54
|
+
const name = py.match(/name\s*=\s*"([^"]+)"/)?.[1];
|
|
55
|
+
if (name)
|
|
56
|
+
hints.push(`Python package: ${name}`);
|
|
57
|
+
}
|
|
58
|
+
catch { }
|
|
59
|
+
}
|
|
60
|
+
// Extract from go.mod
|
|
61
|
+
const goPath = join(cwd, "go.mod");
|
|
62
|
+
if (existsSync(goPath)) {
|
|
63
|
+
try {
|
|
64
|
+
const go = readFileSync(goPath, "utf8");
|
|
65
|
+
const mod = go.match(/module\s+(\S+)/)?.[1];
|
|
66
|
+
if (mod)
|
|
67
|
+
hints.push(`Go module: ${mod}`);
|
|
68
|
+
}
|
|
69
|
+
catch { }
|
|
70
|
+
}
|
|
71
|
+
// Extract from Cargo.toml
|
|
72
|
+
const cargoPath = join(cwd, "Cargo.toml");
|
|
73
|
+
if (existsSync(cargoPath)) {
|
|
74
|
+
try {
|
|
75
|
+
const cargo = readFileSync(cargoPath, "utf8");
|
|
76
|
+
const name = cargo.match(/name\s*=\s*"([^"]+)"/)?.[1];
|
|
77
|
+
if (name)
|
|
78
|
+
hints.push(`Rust crate: ${name}`);
|
|
79
|
+
}
|
|
80
|
+
catch { }
|
|
81
|
+
}
|
|
82
|
+
// Monorepo detection
|
|
83
|
+
if (existsSync(join(cwd, "packages"))) {
|
|
84
|
+
try {
|
|
85
|
+
const pkgs = readdirSync(join(cwd, "packages")).filter(d => !d.startsWith("."));
|
|
86
|
+
hints.push(`MONOREPO: ${pkgs.length} packages in packages/ — search packages/ not src/`);
|
|
87
|
+
hints.push(`Packages: ${pkgs.slice(0, 10).join(", ")}`);
|
|
88
|
+
}
|
|
89
|
+
catch { }
|
|
90
|
+
}
|
|
91
|
+
if (existsSync(join(cwd, "apps"))) {
|
|
92
|
+
hints.push("MONOREPO: apps/ directory detected");
|
|
93
|
+
}
|
|
94
|
+
// Makefile targets
|
|
95
|
+
if (existsSync(join(cwd, "Makefile"))) {
|
|
96
|
+
try {
|
|
97
|
+
const { execSync } = require("child_process");
|
|
98
|
+
const targets = execSync("grep -E '^[a-zA-Z_-]+:' Makefile | head -10 | cut -d: -f1", { cwd, encoding: "utf8", timeout: 1000 }).trim();
|
|
99
|
+
if (targets)
|
|
100
|
+
hints.push(`Makefile targets: ${targets.split("\n").join(", ")}`);
|
|
101
|
+
}
|
|
102
|
+
catch { }
|
|
103
|
+
}
|
|
104
|
+
// Source directory structure
|
|
105
|
+
try {
|
|
106
|
+
const { execSync } = require("child_process");
|
|
107
|
+
const srcDirs = ["src", "lib", "app", "packages"];
|
|
108
|
+
for (const dir of srcDirs) {
|
|
109
|
+
if (existsSync(join(cwd, dir))) {
|
|
110
|
+
const tree = execSync(`find ${dir} -maxdepth 3 -not -path '*/node_modules/*' -not -path '*/dist/*' -not -name '*.test.*' 2>/dev/null | sort | head -60`, { cwd, encoding: "utf8", timeout: 3000 }).trim();
|
|
111
|
+
if (tree)
|
|
112
|
+
hints.push(`Files in ${dir}/:\n${tree}`);
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
// Top-level files
|
|
117
|
+
const topLevel = execSync("ls -1", { cwd, encoding: "utf8", timeout: 1000 }).trim();
|
|
118
|
+
hints.push(`Top-level: ${topLevel.split("\n").join(", ")}`);
|
|
119
|
+
}
|
|
120
|
+
catch { }
|
|
121
|
+
return hints;
|
|
122
|
+
}
|
|
123
|
+
/** Discover output-specific hints (observations about command output) */
|
|
124
|
+
export function discoverOutputHints(output, command) {
|
|
125
|
+
const hints = [];
|
|
126
|
+
const lines = output.split("\n");
|
|
127
|
+
hints.push(`Output: ${lines.length} lines, ${output.length} chars`);
|
|
128
|
+
// Only detect test results from actual test runners (not grep output containing "pass"/"fail" in code)
|
|
129
|
+
const isGrepOutput = /^\s*(src\/|\.\/|packages\/).*:\d+:/.test(output);
|
|
130
|
+
if (!isGrepOutput) {
|
|
131
|
+
const passMatch = output.match(/(\d+)\s+pass(?:ed|ing)?\b/i);
|
|
132
|
+
const failMatch = output.match(/(\d+)\s+fail(?:ed|ing|ure)?\b/i);
|
|
133
|
+
if (passMatch)
|
|
134
|
+
hints.push(`Test results detected: ${passMatch[0]}`);
|
|
135
|
+
if (failMatch)
|
|
136
|
+
hints.push(`Test results detected: ${failMatch[0]}`);
|
|
137
|
+
// Error patterns (only from actual command output, not code search)
|
|
138
|
+
if (output.match(/error\s*TS\d+/i))
|
|
139
|
+
hints.push("TypeScript errors detected in output");
|
|
140
|
+
if (output.match(/ENOENT|EACCES|EADDRINUSE/))
|
|
141
|
+
hints.push("System error code detected in output");
|
|
142
|
+
}
|
|
143
|
+
// Coverage patterns
|
|
144
|
+
if (output.match(/%\s*Funcs|%\s*Lines|coverage/i))
|
|
145
|
+
hints.push("Code coverage data detected in output");
|
|
146
|
+
// Large/repetitive output
|
|
147
|
+
if (lines.length > 100)
|
|
148
|
+
hints.push(`Large output (${lines.length} lines) — consider summarizing`);
|
|
149
|
+
const uniqueLines = new Set(lines.map(l => l.trim())).size;
|
|
150
|
+
if (uniqueLines < lines.length * 0.5)
|
|
151
|
+
hints.push("Output has many duplicate/similar lines");
|
|
152
|
+
// Sensitive data (only env var assignments, not code containing the word KEY/TOKEN)
|
|
153
|
+
if (output.match(/^[A-Z_]+(KEY|TOKEN|SECRET|PASSWORD)\s*=\s*\S+/m))
|
|
154
|
+
hints.push("Output may contain sensitive data — redact credentials");
|
|
155
|
+
// Error block extraction — state machine that captures multi-line errors
|
|
156
|
+
if (!isGrepOutput) {
|
|
157
|
+
const errorBlocks = extractErrorBlocks(output);
|
|
158
|
+
if (errorBlocks.length > 0) {
|
|
159
|
+
const summary = errorBlocks.slice(0, 3).map(b => b.trim().split("\n").slice(0, 5).join("\n")).join("\n---\n");
|
|
160
|
+
hints.push(`ERROR BLOCKS FOUND (${errorBlocks.length}):\n${summary}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return hints;
|
|
164
|
+
}
|
|
165
|
+
/** Extract multi-line error blocks using a state machine */
|
|
166
|
+
function extractErrorBlocks(output) {
|
|
167
|
+
const lines = output.split("\n");
|
|
168
|
+
const blocks = [];
|
|
169
|
+
let currentBlock = [];
|
|
170
|
+
let inErrorBlock = false;
|
|
171
|
+
let blankCount = 0;
|
|
172
|
+
// Patterns that START an error block
|
|
173
|
+
const errorStarters = [
|
|
174
|
+
/^error/i, /^Error:/i, /^ERROR/,
|
|
175
|
+
/^Traceback/i, /^panic:/i, /^fatal:/i,
|
|
176
|
+
/^FAIL/i, /^✗/, /^✘/,
|
|
177
|
+
/error\s*TS\d+/i, /error\[E\d+\]/,
|
|
178
|
+
/^SyntaxError/i, /^TypeError/i, /^ReferenceError/i,
|
|
179
|
+
/^Unhandled/i, /^Exception/i,
|
|
180
|
+
/ENOENT|EACCES|EADDRINUSE|ECONNREFUSED/,
|
|
181
|
+
];
|
|
182
|
+
for (const line of lines) {
|
|
183
|
+
const trimmed = line.trim();
|
|
184
|
+
if (!trimmed) {
|
|
185
|
+
blankCount++;
|
|
186
|
+
if (inErrorBlock) {
|
|
187
|
+
currentBlock.push(line);
|
|
188
|
+
// 2+ blank lines = end of error block
|
|
189
|
+
if (blankCount >= 2) {
|
|
190
|
+
blocks.push(currentBlock.join("\n").trim());
|
|
191
|
+
currentBlock = [];
|
|
192
|
+
inErrorBlock = false;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
blankCount = 0;
|
|
198
|
+
// Check if this line starts a new error block
|
|
199
|
+
if (!inErrorBlock && errorStarters.some(p => p.test(trimmed))) {
|
|
200
|
+
inErrorBlock = true;
|
|
201
|
+
currentBlock = [line];
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (inErrorBlock) {
|
|
205
|
+
// Continuation: indented lines, "at ..." stack frames, "--->" pointers, "File ..." python traces
|
|
206
|
+
const isContinuation = /^\s+/.test(line) ||
|
|
207
|
+
/^\s*at\s/.test(trimmed) ||
|
|
208
|
+
/^\s*-+>/.test(trimmed) ||
|
|
209
|
+
/^\s*\|/.test(trimmed) ||
|
|
210
|
+
/^\s*File "/.test(trimmed) ||
|
|
211
|
+
/^\s*\d+\s*\|/.test(trimmed) || // rust/compiler line numbers
|
|
212
|
+
/^Caused by:/i.test(trimmed);
|
|
213
|
+
if (isContinuation) {
|
|
214
|
+
currentBlock.push(line);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
// Non-continuation, non-blank = end of error block
|
|
218
|
+
blocks.push(currentBlock.join("\n").trim());
|
|
219
|
+
currentBlock = [];
|
|
220
|
+
inErrorBlock = false;
|
|
221
|
+
// Check if THIS line starts a new error block
|
|
222
|
+
if (errorStarters.some(p => p.test(trimmed))) {
|
|
223
|
+
inErrorBlock = true;
|
|
224
|
+
currentBlock = [line];
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// Flush remaining block
|
|
230
|
+
if (currentBlock.length > 0) {
|
|
231
|
+
blocks.push(currentBlock.join("\n").trim());
|
|
232
|
+
}
|
|
233
|
+
return blocks;
|
|
234
|
+
}
|
|
235
|
+
/** Discover safety hints about a command */
|
|
236
|
+
export function discoverSafetyHints(command) {
|
|
237
|
+
const hints = [];
|
|
238
|
+
// Observations about the command (AI decides if it's safe)
|
|
239
|
+
if (command.match(/\brm\b|\brmdir\b|\btruncate\b/))
|
|
240
|
+
hints.push("SAFETY: command contains file deletion (rm/rmdir/truncate)");
|
|
241
|
+
if (command.match(/\bkill\b|\bkillall\b|\bpkill\b/))
|
|
242
|
+
hints.push("SAFETY: command kills processes");
|
|
243
|
+
if (command.match(/\bgit\s+push\b|\bgit\s+reset\s+--hard\b/))
|
|
244
|
+
hints.push("SAFETY: command pushes/resets git");
|
|
245
|
+
if (command.match(/\bnpx\b|\bnpm\s+install\b|\bpip\s+install\b/))
|
|
246
|
+
hints.push("SAFETY: command installs packages");
|
|
247
|
+
if (command.match(/\bsed\s+-i\b|\bcodemod\b/))
|
|
248
|
+
hints.push("SAFETY: command modifies files in-place");
|
|
249
|
+
if (command.match(/\btouch\b|\bmkdir\b/))
|
|
250
|
+
hints.push("SAFETY: command creates files/directories");
|
|
251
|
+
if (command.match(/>\s*\S+\.\w+/))
|
|
252
|
+
hints.push("SAFETY: command writes to a file via redirect");
|
|
253
|
+
if (command.match(/\b(bun|npm|pnpm)\s+run\s+dev\b|\bstart\b/))
|
|
254
|
+
hints.push("SAFETY: command starts a server/process");
|
|
255
|
+
// Read-only observations
|
|
256
|
+
if (command.match(/^\s*git\s+(log|show|diff|status|branch|blame|tag)\b/))
|
|
257
|
+
hints.push("This is a read-only git command");
|
|
258
|
+
if (command.match(/^\s*(ls|cat|head|tail|grep|find|wc|du|df|uptime|whoami|pwd)\b/))
|
|
259
|
+
hints.push("This is a read-only command");
|
|
260
|
+
return hints;
|
|
261
|
+
}
|
|
262
|
+
/** Format all hints for system prompt injection */
|
|
263
|
+
export function formatHints(project, output, safety) {
|
|
264
|
+
const sections = [];
|
|
265
|
+
if (project.length > 0) {
|
|
266
|
+
sections.push("PROJECT CONTEXT:\n" + project.join("\n"));
|
|
267
|
+
}
|
|
268
|
+
if (output && output.length > 0) {
|
|
269
|
+
sections.push("OUTPUT OBSERVATIONS:\n" + output.join("\n"));
|
|
270
|
+
}
|
|
271
|
+
if (safety && safety.length > 0) {
|
|
272
|
+
sections.push("SAFETY OBSERVATIONS:\n" + safety.join("\n"));
|
|
273
|
+
}
|
|
274
|
+
return sections.join("\n\n");
|
|
275
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Diff-aware output caching — when same command runs again, return only what changed
|
|
2
|
+
import { estimateTokens } from "./parsers/index.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
|
+
}
|