micro-models-agent 0.61.0 → 0.61.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/CHANGELOG.md +601 -595
- package/README.md +358 -358
- package/dist/certification/certifications.json +493 -493
- package/dist/cli/commands.js +447 -0
- package/dist/cli/completer.js +167 -0
- package/dist/cli/index.js +2 -0
- package/dist/cli/main.js +153 -0
- package/dist/cli/plugin-commands.js +36 -0
- package/dist/cli/repl-commands.js +761 -0
- package/dist/cli/repl.js +702 -0
- package/dist/cli/run-result.js +33 -0
- package/dist/cli/security-commands.js +164 -0
- package/dist/cli/setup.js +237 -0
- package/dist/config/config.js +276 -0
- package/dist/config/defaults.js +141 -0
- package/dist/config/domains.js +179 -0
- package/dist/config/experts.js +15 -0
- package/dist/config/index.js +4 -0
- package/dist/config/security.js +213 -0
- package/dist/config/types.js +1 -0
- package/dist/core/agent-moe.js +102 -0
- package/dist/core/agent.js +1018 -0
- package/dist/core/bootstrap.js +481 -0
- package/dist/core/crash-handler.js +51 -0
- package/dist/core/environment.js +199 -0
- package/dist/core/index.js +2 -0
- package/dist/core/prompt-builder.js +76 -0
- package/dist/core/session-logger.js +251 -0
- package/dist/core/types.js +1 -0
- package/dist/core/version.js +26 -0
- package/dist/core/workspace.js +76 -0
- package/dist/i18n/en.json +757 -757
- package/dist/i18n/index.js +46 -0
- package/dist/i18n/ru.json +757 -757
- package/dist/index.js +22 -0
- package/dist/llm/image-utils.js +143 -0
- package/dist/llm/index.js +4 -0
- package/dist/llm/model-loader.js +78 -0
- package/dist/llm/openai-compat.js +497 -0
- package/dist/llm/orchestrator.js +200 -0
- package/dist/llm/provider.js +10 -0
- package/dist/llm/response.js +39 -0
- package/dist/llm/token-counter.js +39 -0
- package/dist/llm/types.js +1 -0
- package/dist/logger/app-logger.js +189 -0
- package/dist/logger/file-log.js +151 -0
- package/dist/logger/index.js +1 -0
- package/dist/main.js +364 -325
- package/dist/migration/backup.js +45 -0
- package/dist/migration/detect.js +50 -0
- package/dist/migration/index.js +2 -0
- package/dist/modules/artifacts/store.js +61 -0
- package/dist/modules/browser/actions.js +76 -0
- package/dist/modules/browser/bridge-client.js +199 -0
- package/dist/modules/browser/bridge-path.js +10 -0
- package/dist/modules/browser/bridge-server.mjs +219 -219
- package/dist/modules/browser/cookie-store.js +24 -0
- package/dist/modules/browser/driver.js +136 -0
- package/dist/modules/browser/index.js +7 -0
- package/dist/modules/browser/module.js +29 -0
- package/dist/modules/browser/session.js +342 -0
- package/dist/modules/browser/snapshot.js +148 -0
- package/dist/modules/browser/types.js +12 -0
- package/dist/modules/certification/cli.js +213 -0
- package/dist/modules/certification/fact-checker.js +82 -0
- package/dist/modules/certification/loader.js +106 -0
- package/dist/modules/certification/manifest.js +58 -0
- package/dist/modules/certification/runner.js +245 -0
- package/dist/modules/certification/scenarios.js +407 -0
- package/dist/modules/certification/types.js +1 -0
- package/dist/modules/context/chunk-query.js +100 -0
- package/dist/modules/context/fact-extractor.js +168 -0
- package/dist/modules/context/history.js +15 -0
- package/dist/modules/context/index.js +1 -0
- package/dist/modules/context/manager.js +440 -0
- package/dist/modules/execution/audit-runners.js +206 -0
- package/dist/modules/execution/auditor.js +218 -0
- package/dist/modules/execution/execution-plugin.js +431 -0
- package/dist/modules/execution/index.js +8 -0
- package/dist/modules/execution/module.js +625 -0
- package/dist/modules/execution/moe-executor.js +304 -0
- package/dist/modules/execution/plan-coverage.js +68 -0
- package/dist/modules/execution/plan-persister.js +46 -0
- package/dist/modules/execution/plan-store.js +196 -0
- package/dist/modules/execution/plan-tool.js +677 -0
- package/dist/modules/execution/plan-validator.js +153 -0
- package/dist/modules/execution/planner.js +94 -0
- package/dist/modules/execution/stuck-detector.js +746 -0
- package/dist/modules/execution/tracker.js +69 -0
- package/dist/modules/execution/types.js +1 -0
- package/dist/modules/execution/verifier.js +235 -0
- package/dist/modules/execution/windows-commands.js +41 -0
- package/dist/modules/hallucination/confidence.js +66 -0
- package/dist/modules/hallucination/consistency.js +26 -0
- package/dist/modules/hallucination/detector.js +47 -0
- package/dist/modules/hallucination/factual.js +169 -0
- package/dist/modules/hallucination/index.js +5 -0
- package/dist/modules/hallucination/js-identifiers.js +262 -0
- package/dist/modules/hallucination/llm-judge.js +101 -0
- package/dist/modules/index.js +5 -0
- package/dist/modules/indexer/cache.js +40 -0
- package/dist/modules/indexer/index.js +3 -0
- package/dist/modules/indexer/module.js +246 -0
- package/dist/modules/indexer/project-profile.js +183 -0
- package/dist/modules/indexer/walker.js +101 -0
- package/dist/modules/lsp/check-tool.js +58 -0
- package/dist/modules/lsp/client.js +389 -0
- package/dist/modules/lsp/command.js +60 -0
- package/dist/modules/lsp/config.js +135 -0
- package/dist/modules/lsp/index.js +3 -0
- package/dist/modules/lsp/module.js +260 -0
- package/dist/modules/lsp/probe.js +86 -0
- package/dist/modules/lsp/project-root.js +32 -0
- package/dist/modules/lsp/startup-check.js +144 -0
- package/dist/modules/lsp/types.js +1 -0
- package/dist/modules/mcp/client.js +399 -0
- package/dist/modules/mcp/index.js +3 -0
- package/dist/modules/mcp/module.js +142 -0
- package/dist/modules/mcp/registry.js +15 -0
- package/dist/modules/memory/index.js +1 -0
- package/dist/modules/memory/module.js +96 -0
- package/dist/modules/memory/search.js +42 -0
- package/dist/modules/memory/store.js +69 -0
- package/dist/modules/pipelines/engine.js +60 -0
- package/dist/modules/pipelines/index.js +3 -0
- package/dist/modules/pipelines/parser.js +56 -0
- package/dist/modules/pipelines/template.js +14 -0
- package/dist/modules/plugins/builtin/lint-on-write.js +334 -0
- package/dist/modules/plugins/builtin/notify.js +9 -0
- package/dist/modules/plugins/index.js +1 -0
- package/dist/modules/plugins/loader.js +70 -0
- package/dist/modules/plugins/manager.js +261 -0
- package/dist/modules/plugins/types.js +1 -0
- package/dist/modules/pricing/index.js +61 -0
- package/dist/modules/pricing/prices.js +129 -0
- package/dist/modules/processes/detect.js +34 -0
- package/dist/modules/processes/index.js +2 -0
- package/dist/modules/processes/registry.js +327 -0
- package/dist/modules/processes/runner.js +23 -0
- package/dist/modules/providers/create.js +22 -0
- package/dist/modules/providers/fallback.js +79 -0
- package/dist/modules/providers/health.js +46 -0
- package/dist/modules/providers/index.js +5 -0
- package/dist/modules/providers/manager.js +161 -0
- package/dist/modules/providers/presets.js +128 -0
- package/dist/modules/providers/registry.js +22 -0
- package/dist/modules/providers/types.js +1 -0
- package/dist/modules/registry.js +48 -0
- package/dist/modules/security/audit-log.js +136 -0
- package/dist/modules/security/audit-notifier.js +292 -0
- package/dist/modules/security/command-validator.js +219 -0
- package/dist/modules/security/content-scanner.js +53 -0
- package/dist/modules/security/data-sanitizer.js +89 -0
- package/dist/modules/security/encryption.js +242 -0
- package/dist/modules/security/index.js +14 -0
- package/dist/modules/security/network-validator.js +88 -0
- package/dist/modules/security/path-validator.js +203 -0
- package/dist/modules/security/rate-limiter.js +119 -0
- package/dist/modules/security/security-policies.js +531 -0
- package/dist/modules/security/session-encryption.js +210 -0
- package/dist/modules/security/session-isolation.js +95 -0
- package/dist/modules/session/index.js +3 -0
- package/dist/modules/session/manager.js +172 -0
- package/dist/modules/session/module.js +24 -0
- package/dist/modules/session/store.js +222 -0
- package/dist/modules/session/types.js +1 -0
- package/dist/modules/skills/index.js +2 -0
- package/dist/modules/skills/loader.js +72 -0
- package/dist/modules/skills/matcher.js +27 -0
- package/dist/modules/skills/module.js +129 -0
- package/dist/modules/types.js +1 -0
- package/dist/modules/updater/checker.js +96 -0
- package/dist/modules/updater/index.js +2 -0
- package/dist/modules/updater/module.js +116 -0
- package/dist/modules/user-profile/compressor.js +16 -0
- package/dist/modules/user-profile/index.js +1 -0
- package/dist/modules/user-profile/profile.js +68 -0
- package/dist/skills/builtin/git.md +36 -36
- package/dist/skills/builtin/typescript.md +35 -35
- package/dist/tools/approve.js +33 -0
- package/dist/tools/attach-image.js +101 -0
- package/dist/tools/bash.js +519 -0
- package/dist/tools/browser.js +115 -0
- package/dist/tools/chunk-query.js +100 -0
- package/dist/tools/create-dir.js +56 -0
- package/dist/tools/delete-file.js +63 -0
- package/dist/tools/download-file.js +117 -0
- package/dist/tools/edit-file.js +80 -0
- package/dist/tools/enable-tools.js +59 -0
- package/dist/tools/executor.js +154 -0
- package/dist/tools/file-info.js +47 -0
- package/dist/tools/filter-tools.js +17 -0
- package/dist/tools/glob-tool.js +27 -0
- package/dist/tools/grep-tool.js +125 -0
- package/dist/tools/hidden-tools-block.js +37 -0
- package/dist/tools/index.js +78 -0
- package/dist/tools/list-dir.js +49 -0
- package/dist/tools/load-skill.js +43 -0
- package/dist/tools/mcp-call.js +69 -0
- package/dist/tools/move-file.js +86 -0
- package/dist/tools/path-utils.js +101 -0
- package/dist/tools/pipeline-run.js +145 -0
- package/dist/tools/preview.js +2 -0
- package/dist/tools/process-kill.js +40 -0
- package/dist/tools/process-list.js +37 -0
- package/dist/tools/process-log.js +54 -0
- package/dist/tools/question.js +141 -0
- package/dist/tools/read-file.js +179 -0
- package/dist/tools/recall.js +118 -0
- package/dist/tools/registry.js +47 -0
- package/dist/tools/remember.js +68 -0
- package/dist/tools/scope-check.js +32 -0
- package/dist/tools/search-history.js +85 -0
- package/dist/tools/subagent.js +196 -0
- package/dist/tools/types.js +1 -0
- package/dist/tools/user-input.js +123 -0
- package/dist/tools/web-browse.js +87 -0
- package/dist/tools/web-fetch.js +119 -0
- package/dist/tools/web-search.js +105 -0
- package/dist/tools/write-file.js +82 -0
- package/dist/ui/box.js +77 -0
- package/dist/ui/colors.js +4 -0
- package/dist/ui/diff.js +178 -0
- package/dist/ui/index.js +6 -0
- package/dist/ui/line-editor.js +822 -0
- package/dist/ui/line-math.js +73 -0
- package/dist/ui/md-formatter.js +212 -0
- package/dist/ui/output.js +13 -0
- package/dist/ui/plan-view.js +103 -0
- package/dist/ui/renderer.js +259 -0
- package/dist/ui/spinner.js +70 -0
- package/dist/ui/table.js +144 -0
- package/package.json +51 -51
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, appendFileSync, existsSync, mkdirSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { MemorySearch } from "./search";
|
|
4
|
+
const MEMORY_FILES = ["conventions", "decisions", "errors", "facts"];
|
|
5
|
+
export class MemoryStore {
|
|
6
|
+
memoryDir;
|
|
7
|
+
constructor(memoryDir) {
|
|
8
|
+
this.memoryDir = memoryDir;
|
|
9
|
+
this.ensureDir();
|
|
10
|
+
for (const name of MEMORY_FILES) {
|
|
11
|
+
const path = join(this.memoryDir, `${name}.md`);
|
|
12
|
+
if (!existsSync(path)) {
|
|
13
|
+
writeFileSync(path, `# ${name.charAt(0).toUpperCase() + name.slice(1)}\n\n`, "utf-8");
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
ensureDir() {
|
|
18
|
+
if (!existsSync(this.memoryDir)) {
|
|
19
|
+
mkdirSync(this.memoryDir, { recursive: true });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
read(name) {
|
|
23
|
+
const path = join(this.memoryDir, `${name}.md`);
|
|
24
|
+
if (!existsSync(path))
|
|
25
|
+
return "";
|
|
26
|
+
return readFileSync(path, "utf-8");
|
|
27
|
+
}
|
|
28
|
+
append(name, entry) {
|
|
29
|
+
const path = join(this.memoryDir, `${name}.md`);
|
|
30
|
+
const timestamp = new Date().toISOString().replace("T", " ").slice(0, 19);
|
|
31
|
+
const formatted = `- **${timestamp}** — ${entry}\n`;
|
|
32
|
+
appendFileSync(path, formatted, "utf-8");
|
|
33
|
+
}
|
|
34
|
+
search(query) {
|
|
35
|
+
const searchModule = new MemorySearch(this.memoryDir);
|
|
36
|
+
return searchModule.query(query);
|
|
37
|
+
}
|
|
38
|
+
prefsPath() {
|
|
39
|
+
return join(this.memoryDir, "preferences.json");
|
|
40
|
+
}
|
|
41
|
+
getPreferences() {
|
|
42
|
+
const path = this.prefsPath();
|
|
43
|
+
if (!existsSync(path))
|
|
44
|
+
return {};
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return {};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
setPreference(key, value) {
|
|
53
|
+
const prefs = this.getPreferences();
|
|
54
|
+
prefs[key] = value;
|
|
55
|
+
writeFileSync(this.prefsPath(), JSON.stringify(prefs, null, 2), "utf-8");
|
|
56
|
+
}
|
|
57
|
+
deletePreference(key) {
|
|
58
|
+
const prefs = this.getPreferences();
|
|
59
|
+
if (!(key in prefs))
|
|
60
|
+
return false;
|
|
61
|
+
delete prefs[key];
|
|
62
|
+
writeFileSync(this.prefsPath(), JSON.stringify(prefs, null, 2), "utf-8");
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
appendRule(category, pattern, cause, solution) {
|
|
66
|
+
const entry = `**pattern:** ${pattern}\n **cause:** ${cause}\n **solution:** ${solution}`;
|
|
67
|
+
this.append(category, entry);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { t } from "../../i18n/index";
|
|
2
|
+
export class PipelineEngine {
|
|
3
|
+
stepStatus = new Map();
|
|
4
|
+
resolveDependencies(steps) {
|
|
5
|
+
const visited = new Set();
|
|
6
|
+
const inStack = new Set();
|
|
7
|
+
const order = [];
|
|
8
|
+
function visit(stepId, stepMap) {
|
|
9
|
+
if (inStack.has(stepId))
|
|
10
|
+
throw new Error(t("pipeline.circular", { stepId }));
|
|
11
|
+
if (visited.has(stepId))
|
|
12
|
+
return;
|
|
13
|
+
inStack.add(stepId);
|
|
14
|
+
const step = stepMap.get(stepId);
|
|
15
|
+
if (step?.depends_on) {
|
|
16
|
+
for (const dep of step.depends_on) {
|
|
17
|
+
visit(dep, stepMap);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
inStack.delete(stepId);
|
|
21
|
+
visited.add(stepId);
|
|
22
|
+
order.push(stepId);
|
|
23
|
+
}
|
|
24
|
+
const stepMap = new Map(steps.map((s) => [s.id, s]));
|
|
25
|
+
for (const step of steps) {
|
|
26
|
+
if (!visited.has(step.id)) {
|
|
27
|
+
visit(step.id, stepMap);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return order;
|
|
31
|
+
}
|
|
32
|
+
getReadySteps(steps, completed) {
|
|
33
|
+
return steps.filter((s) => {
|
|
34
|
+
if (completed.has(s.id))
|
|
35
|
+
return false;
|
|
36
|
+
if (this.stepStatus.get(s.id) === "running")
|
|
37
|
+
return false;
|
|
38
|
+
if (this.stepStatus.get(s.id) === "failed")
|
|
39
|
+
return false;
|
|
40
|
+
if (!s.depends_on || s.depends_on.length === 0)
|
|
41
|
+
return true;
|
|
42
|
+
return s.depends_on.every((d) => completed.has(d));
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
setStepStatus(stepId, status) {
|
|
46
|
+
this.stepStatus.set(stepId, status);
|
|
47
|
+
}
|
|
48
|
+
getStepStatus(stepId) {
|
|
49
|
+
return this.stepStatus.get(stepId) || "pending";
|
|
50
|
+
}
|
|
51
|
+
isComplete(stepIds) {
|
|
52
|
+
return stepIds.every((id) => {
|
|
53
|
+
const s = this.stepStatus.get(id);
|
|
54
|
+
return s === "done" || s === "skipped";
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
reset() {
|
|
58
|
+
this.stepStatus.clear();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { t } from "../../i18n/index";
|
|
2
|
+
export class PipelineParser {
|
|
3
|
+
static parse(yaml) {
|
|
4
|
+
const lines = yaml.split("\n").filter((l) => l.trim());
|
|
5
|
+
const result = {};
|
|
6
|
+
let currentSection = null;
|
|
7
|
+
let currentSteps = [];
|
|
8
|
+
for (const line of lines) {
|
|
9
|
+
if (line.match(/^\w+:/) && !line.startsWith(" ") && !line.startsWith(" -")) {
|
|
10
|
+
const [key, ...valParts] = line.split(":");
|
|
11
|
+
const val = valParts.join(":").trim();
|
|
12
|
+
if (key === "steps") {
|
|
13
|
+
currentSection = "steps";
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
result[key] = val;
|
|
17
|
+
currentSection = null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
else if (currentSection === "steps") {
|
|
21
|
+
const stepMatch = line.match(/^\s+- id:\s*(\S+)/);
|
|
22
|
+
if (stepMatch) {
|
|
23
|
+
currentSteps.push({ id: stepMatch[1] });
|
|
24
|
+
}
|
|
25
|
+
else if (currentSteps.length > 0) {
|
|
26
|
+
const lastStep = currentSteps[currentSteps.length - 1];
|
|
27
|
+
const kvMatch = line.match(/^\s+(\w+):\s*(.+)$/);
|
|
28
|
+
if (kvMatch) {
|
|
29
|
+
const key = kvMatch[1];
|
|
30
|
+
let val = kvMatch[2].trim();
|
|
31
|
+
if (val === "true")
|
|
32
|
+
val = true;
|
|
33
|
+
else if (val === "false")
|
|
34
|
+
val = false;
|
|
35
|
+
else if (val.startsWith("[") && val.endsWith("]")) {
|
|
36
|
+
val = val
|
|
37
|
+
.slice(1, -1)
|
|
38
|
+
.split(",")
|
|
39
|
+
.map((s) => s.trim().replace(/^['"]|['"]$/g, ""));
|
|
40
|
+
}
|
|
41
|
+
lastStep[key] = val;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (!result.name || currentSteps.length === 0) {
|
|
47
|
+
throw new Error(t("pipeline.invalid"));
|
|
48
|
+
}
|
|
49
|
+
for (const step of currentSteps) {
|
|
50
|
+
if (!step.id || !step.agent || !step.prompt) {
|
|
51
|
+
throw new Error(t("pipeline.step_missing_fields", { step: JSON.stringify(step) }));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return { name: result.name, steps: currentSteps };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export class TemplateEngine {
|
|
2
|
+
static render(template, params, stepOutputs) {
|
|
3
|
+
let result = template;
|
|
4
|
+
for (const [key, val] of Object.entries(params)) {
|
|
5
|
+
result = result.replace(new RegExp(`\\{${key}\\}`, "g"), val);
|
|
6
|
+
}
|
|
7
|
+
if (stepOutputs) {
|
|
8
|
+
result = result.replace(/\{\{steps\.(\w+)\.output\}\}/g, (_, stepId) => {
|
|
9
|
+
return stepOutputs[stepId]?.output || `{{steps.${stepId}.output}}`;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { spawn, execSync } from "child_process";
|
|
2
|
+
import { existsSync, readFileSync } from "fs";
|
|
3
|
+
import { resolve, extname, join } from "path";
|
|
4
|
+
import { platform } from "os";
|
|
5
|
+
import { findProjectRoot } from "../../lsp/project-root";
|
|
6
|
+
const TYPE_CHECK_DEBOUNCE_MS = 2000;
|
|
7
|
+
// Projects whose lint script points at a binary that is not installed
|
|
8
|
+
// (keyed by baseDir::script). The run itself is expensive and the failure
|
|
9
|
+
// marker is pure noise — skip for the rest of the process.
|
|
10
|
+
const missingLintBinaries = new Set();
|
|
11
|
+
function lintCacheKey(baseDir, lintScript) {
|
|
12
|
+
return `${baseDir}::${lintScript}`;
|
|
13
|
+
}
|
|
14
|
+
// Shell-level "command not found" across locales/runtimes: POSIX `sh` prints
|
|
15
|
+
// "command not found" (exit 127); Windows cmd.exe prints "is not recognized"
|
|
16
|
+
// (en) / "не является внутренней или внешней командой" (ru) with exit 1/9009.
|
|
17
|
+
const MISSING_BINARY_RE = /command not found|not recognized|не является внутренней|не распознан|не є внутрішньою|".+?" не является/i;
|
|
18
|
+
function isMissingBinaryError(err, combinedOutput) {
|
|
19
|
+
// 127 = POSIX sh "command not found"; 9009 = Windows cmd.exe "is not
|
|
20
|
+
// recognized" — both are authoritative regardless of output encoding.
|
|
21
|
+
if (err.status === 127 || err.status === 9009)
|
|
22
|
+
return true;
|
|
23
|
+
if (err.code === "ENOENT")
|
|
24
|
+
return true;
|
|
25
|
+
return MISSING_BINARY_RE.test(combinedOutput);
|
|
26
|
+
}
|
|
27
|
+
// Per-file syntax checks are cached by (path, content hash): re-writing a file
|
|
28
|
+
// with identical content skips the (expensive) check. Content changes → cache
|
|
29
|
+
// miss → fresh check.
|
|
30
|
+
const syntaxCache = new Map();
|
|
31
|
+
function contentHash(content) {
|
|
32
|
+
let h = 5381;
|
|
33
|
+
for (let i = 0; i < content.length; i++) {
|
|
34
|
+
h = ((h << 5) + h + content.charCodeAt(i)) | 0;
|
|
35
|
+
}
|
|
36
|
+
return String(h);
|
|
37
|
+
}
|
|
38
|
+
let _winDecoder;
|
|
39
|
+
/**
|
|
40
|
+
* OEM decoder for the active console code page (CP866 on Russian Windows).
|
|
41
|
+
* Mirrors `getOemDecoder()` in modules/processes/registry.ts: a transient
|
|
42
|
+
* chcp.com failure under load must NOT be cached — only a successful probe
|
|
43
|
+
* (or the hard ibm866 fallback) is memoized.
|
|
44
|
+
*/
|
|
45
|
+
function getWinDecoder() {
|
|
46
|
+
if (_winDecoder !== undefined)
|
|
47
|
+
return _winDecoder;
|
|
48
|
+
if (platform() !== "win32") {
|
|
49
|
+
_winDecoder = new TextDecoder("utf-8");
|
|
50
|
+
return _winDecoder;
|
|
51
|
+
}
|
|
52
|
+
let decoder = null;
|
|
53
|
+
try {
|
|
54
|
+
const cpOut = execSync("chcp.com", {
|
|
55
|
+
encoding: "buffer",
|
|
56
|
+
timeout: 2000,
|
|
57
|
+
windowsHide: true,
|
|
58
|
+
}).toString("latin1");
|
|
59
|
+
const m = cpOut.match(/(\d+)/);
|
|
60
|
+
if (m)
|
|
61
|
+
decoder = new TextDecoder("ibm" + m[1]);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
/* chcp failed (load/ENOENT) — retry on the next call */
|
|
65
|
+
}
|
|
66
|
+
if (!decoder) {
|
|
67
|
+
try {
|
|
68
|
+
decoder = new TextDecoder("ibm866");
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return new TextDecoder("utf-8"); // unknown encoding — do not cache
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
_winDecoder = decoder;
|
|
75
|
+
return _winDecoder;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Decode child-process output to UTF-8. Modern tools (bun, node, npm, tsc)
|
|
79
|
+
* write UTF-8 into the pipe; cmd.exe builtins/errors use the OEM code page.
|
|
80
|
+
* Strict UTF-8 first; on invalid byte sequences fall back to OEM.
|
|
81
|
+
*/
|
|
82
|
+
function decodeOutput(chunks) {
|
|
83
|
+
const raw = Buffer.concat(chunks);
|
|
84
|
+
try {
|
|
85
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(raw);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
/* not valid UTF-8 → OEM code page output */
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
return getWinDecoder().decode(raw);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
return raw.toString("latin1");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export class LintOnWritePlugin {
|
|
98
|
+
name = "lint-on-write";
|
|
99
|
+
priority = 10;
|
|
100
|
+
isBuiltin = true;
|
|
101
|
+
_checkPromise = null;
|
|
102
|
+
_checkTimestamp = 0;
|
|
103
|
+
async onAfterTool(ctx, call, result) {
|
|
104
|
+
if (call.name !== "write_file" && call.name !== "edit_file") {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (!result.success) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const path = String(call.arguments.path || "");
|
|
111
|
+
if (!path)
|
|
112
|
+
return;
|
|
113
|
+
const fullPath = resolve(ctx.baseDir, path);
|
|
114
|
+
if (!existsSync(fullPath))
|
|
115
|
+
return;
|
|
116
|
+
const signal = ctx.signal;
|
|
117
|
+
if (signal?.aborted)
|
|
118
|
+
return;
|
|
119
|
+
const ext = extname(fullPath);
|
|
120
|
+
const syntaxError = await this.checkSyntax(fullPath, ext, ctx.baseDir, signal);
|
|
121
|
+
if (signal?.aborted)
|
|
122
|
+
return;
|
|
123
|
+
if (syntaxError) {
|
|
124
|
+
result.output += `\n\n[Syntax check failed]: ${syntaxError}`;
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
await this.runProjectLint(ctx, result, signal);
|
|
128
|
+
if (signal?.aborted)
|
|
129
|
+
return;
|
|
130
|
+
await this.runProjectTypeCheck(fullPath, ctx.baseDir, result, signal);
|
|
131
|
+
}
|
|
132
|
+
async checkSyntax(filePath, ext, baseDir, signal) {
|
|
133
|
+
if (ext === ".ts" || ext === ".tsx" || ext === ".cts" || ext === ".mts") {
|
|
134
|
+
// Syntax-only check via bun's parser (~200ms) instead of `npx tsc`
|
|
135
|
+
// (~3-5s per write). Full type errors are still surfaced by the
|
|
136
|
+
// debounced project typecheck below when a tsconfig exists.
|
|
137
|
+
let content = "";
|
|
138
|
+
try {
|
|
139
|
+
content = readFileSync(filePath, "utf-8");
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const hash = contentHash(content);
|
|
145
|
+
const cached = syntaxCache.get(filePath);
|
|
146
|
+
if (cached && cached.hash === hash) {
|
|
147
|
+
return cached.error;
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
await runAsync(`bun build --no-bundle --target=bun "${filePath}"`, baseDir, 10000, signal);
|
|
151
|
+
// Aborted check (interrupt) never validated the file — don't cache it.
|
|
152
|
+
if (signal?.aborted)
|
|
153
|
+
return null;
|
|
154
|
+
syntaxCache.set(filePath, { hash, error: null });
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
if (err.status === 127 ||
|
|
159
|
+
err.message.includes("not found") ||
|
|
160
|
+
err.message.includes("ENOENT")) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
const stderr = err.stderr?.toString() || err.stdout?.toString() || "";
|
|
164
|
+
const firstError = stderr.split("\n").find((line) => line.trim()) || "TypeScript syntax error";
|
|
165
|
+
syntaxCache.set(filePath, { hash, error: firstError.trim() });
|
|
166
|
+
return firstError.trim();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (ext === ".js" || ext === ".jsx" || ext === ".cjs" || ext === ".mjs") {
|
|
170
|
+
try {
|
|
171
|
+
await runAsync(`node --check "${filePath}"`, baseDir, 5000, signal);
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
const stderr = err.stderr?.toString() || "";
|
|
176
|
+
const firstError = stderr.split("\n").find((line) => line.trim()) || "JavaScript syntax error";
|
|
177
|
+
return firstError.trim();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
async runProjectLint(ctx, result, signal) {
|
|
183
|
+
let lintScript;
|
|
184
|
+
try {
|
|
185
|
+
const packageJsonPath = join(ctx.baseDir, "package.json");
|
|
186
|
+
if (!existsSync(packageJsonPath)) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
|
|
190
|
+
lintScript = packageJson.scripts?.lint;
|
|
191
|
+
if (!lintScript) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
// A declared-but-missing linter binary ("eslint" not installed) is a
|
|
195
|
+
// project-setup gap, not a code problem — appending it to every write
|
|
196
|
+
// result polluted the context ~25×/session (ses_mt4fn58c). Detect it
|
|
197
|
+
// once and skip silently for the rest of the process.
|
|
198
|
+
if (missingLintBinaries.has(lintCacheKey(ctx.baseDir, lintScript))) {
|
|
199
|
+
ctx.logger.debug(`Lint skipped (linter for "${lintScript}" not installed)`);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
ctx.logger.debug(`Running lint: ${lintScript}`);
|
|
203
|
+
await runAsync(lintScript, ctx.baseDir, 30_000, signal);
|
|
204
|
+
ctx.logger.debug("Lint passed");
|
|
205
|
+
}
|
|
206
|
+
catch (err) {
|
|
207
|
+
const stderr = (err.stderr?.toString?.() || "").trim();
|
|
208
|
+
const stdout = (err.stdout?.toString?.() || "").trim();
|
|
209
|
+
const combined = `${stderr}\n${stdout}`;
|
|
210
|
+
if (lintScript && isMissingBinaryError(err, combined)) {
|
|
211
|
+
missingLintBinaries.add(lintCacheKey(ctx.baseDir, lintScript));
|
|
212
|
+
ctx.logger.debug(`Lint binary missing — skipping project lint for this session`);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const detail = combined
|
|
216
|
+
.trim()
|
|
217
|
+
.split("\n")
|
|
218
|
+
.filter(Boolean)
|
|
219
|
+
.slice(-5)
|
|
220
|
+
.join("\n");
|
|
221
|
+
ctx.logger.warn(`Lint failed: ${err.message}`);
|
|
222
|
+
result.output += `\n\n[Project lint failed]: ${detail}`;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async runProjectTypeCheck(filePath, baseDir, result, signal) {
|
|
226
|
+
// Resolve the project root from the EDITED FILE, not the agent baseDir.
|
|
227
|
+
// In a nested layout (`baseDir/proj/tsconfig.json`) the old baseDir-based
|
|
228
|
+
// lookup silently skipped the typecheck on every write — the whole
|
|
229
|
+
// session then ran with zero type feedback (observed in ses_mst2r0r5).
|
|
230
|
+
const projectRoot = findProjectRoot(filePath, baseDir, ["tsconfig.json", "package.json"]);
|
|
231
|
+
const tsconfigPath = join(projectRoot, "tsconfig.json");
|
|
232
|
+
if (!existsSync(tsconfigPath)) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const now = Date.now();
|
|
236
|
+
if (this._checkPromise && now - this._checkTimestamp < TYPE_CHECK_DEBOUNCE_MS) {
|
|
237
|
+
const error = await this._checkPromise;
|
|
238
|
+
if (error) {
|
|
239
|
+
result.output += `\n\n[Project typecheck failed]: ${error}`;
|
|
240
|
+
}
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
this._checkTimestamp = now;
|
|
244
|
+
this._checkPromise = this.runTscCheck(projectRoot, signal);
|
|
245
|
+
const error = await this._checkPromise;
|
|
246
|
+
if (error) {
|
|
247
|
+
result.output += `\n\n[Project typecheck failed]: ${error}`;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
async runTscCheck(baseDir, signal) {
|
|
251
|
+
try {
|
|
252
|
+
await runAsync(`npx tsc --noEmit --skipLibCheck`, baseDir, 30000, signal);
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
catch (err) {
|
|
256
|
+
if (err.status === 127 ||
|
|
257
|
+
err.message.includes("not found") ||
|
|
258
|
+
err.message.includes("ENOENT")) {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
const stderr = err.stderr?.toString() || err.stdout?.toString() || "";
|
|
262
|
+
if (stderr.includes("error TS")) {
|
|
263
|
+
const firstError = stderr.split("\n").find((line) => line.includes("error TS")) ||
|
|
264
|
+
"TypeScript type error";
|
|
265
|
+
return firstError.trim();
|
|
266
|
+
}
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Run a command asynchronously (non-blocking event loop) and resolve when it
|
|
273
|
+
* exits. Rejects with captured stderr/stdout on non-zero exit or timeout.
|
|
274
|
+
*/
|
|
275
|
+
function runAsync(command, cwd, timeoutMs, signal) {
|
|
276
|
+
return new Promise((resolve, reject) => {
|
|
277
|
+
const child = spawn(command, {
|
|
278
|
+
cwd,
|
|
279
|
+
shell: true,
|
|
280
|
+
windowsHide: true,
|
|
281
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
282
|
+
});
|
|
283
|
+
const utf8Chunks = [];
|
|
284
|
+
const oemChunks = [];
|
|
285
|
+
child.stdout?.on("data", (d) => {
|
|
286
|
+
utf8Chunks.push(Buffer.from(d));
|
|
287
|
+
});
|
|
288
|
+
child.stderr?.on("data", (d) => {
|
|
289
|
+
oemChunks.push(Buffer.from(d));
|
|
290
|
+
});
|
|
291
|
+
const timer = setTimeout(() => {
|
|
292
|
+
child.kill();
|
|
293
|
+
reject(new Error(`Command timed out after ${timeoutMs}ms`));
|
|
294
|
+
}, timeoutMs);
|
|
295
|
+
// Interrupt (Esc/Ctrl+C): kill the child so the agent loop can return to
|
|
296
|
+
// the prompt immediately instead of waiting out a long tsc/lint run. The
|
|
297
|
+
// close handler resolves empty when aborted — the caller's `signal.aborted`
|
|
298
|
+
// guard discards the result anyway.
|
|
299
|
+
const onAbort = () => child.kill();
|
|
300
|
+
if (signal?.aborted) {
|
|
301
|
+
child.kill();
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
305
|
+
}
|
|
306
|
+
child.on("error", (err) => {
|
|
307
|
+
clearTimeout(timer);
|
|
308
|
+
signal?.removeEventListener("abort", onAbort);
|
|
309
|
+
if (signal?.aborted) {
|
|
310
|
+
resolve({ stdout: decodeOutput(utf8Chunks), stderr: decodeOutput(oemChunks) });
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
reject(err);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
child.on("close", (code) => {
|
|
317
|
+
clearTimeout(timer);
|
|
318
|
+
signal?.removeEventListener("abort", onAbort);
|
|
319
|
+
const stdout = decodeOutput(utf8Chunks);
|
|
320
|
+
const stderr = decodeOutput(oemChunks);
|
|
321
|
+
if (signal?.aborted || code === 0) {
|
|
322
|
+
resolve({ stdout, stderr });
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
const err = new Error(`Command failed with exit code ${code}`);
|
|
326
|
+
err.stdout = stdout;
|
|
327
|
+
err.stderr = stderr;
|
|
328
|
+
err.status = code;
|
|
329
|
+
reject(err);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
export const plugin = new LintOnWritePlugin();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { PluginManager } from "./manager";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { readdirSync, existsSync, statSync } from "fs";
|
|
2
|
+
import { join, basename } from "path";
|
|
3
|
+
import { t } from "../../i18n/index";
|
|
4
|
+
import { semverGt } from "../updater/checker";
|
|
5
|
+
/**
|
|
6
|
+
* Entry file candidates for folder plugins. Looked up in order:
|
|
7
|
+
* `plugin.ts` > `plugin.js` > `index.ts` > `index.js`.
|
|
8
|
+
*/
|
|
9
|
+
const FOLDER_ENTRY_NAMES = ["plugin.ts", "plugin.js", "index.ts", "index.js"];
|
|
10
|
+
export class PluginLoader {
|
|
11
|
+
loadFromDir(dirPath, pluginManager, logger, options) {
|
|
12
|
+
if (!existsSync(dirPath))
|
|
13
|
+
return;
|
|
14
|
+
const entries = readdirSync(dirPath).sort();
|
|
15
|
+
for (const entry of entries) {
|
|
16
|
+
const fullPath = join(dirPath, entry);
|
|
17
|
+
const stat = statSync(fullPath);
|
|
18
|
+
if (stat.isFile()) {
|
|
19
|
+
if (!entry.endsWith(".ts") && !entry.endsWith(".js"))
|
|
20
|
+
continue;
|
|
21
|
+
this.tryLoad(fullPath, entry, pluginManager, logger, options);
|
|
22
|
+
}
|
|
23
|
+
else if (stat.isDirectory()) {
|
|
24
|
+
const entryFile = this.findEntryFile(fullPath);
|
|
25
|
+
if (entryFile) {
|
|
26
|
+
this.tryLoad(entryFile, `${entry}/${basename(entryFile)}`, pluginManager, logger, options);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
findEntryFile(dir) {
|
|
32
|
+
for (const name of FOLDER_ENTRY_NAMES) {
|
|
33
|
+
const candidate = join(dir, name);
|
|
34
|
+
if (existsSync(candidate))
|
|
35
|
+
return candidate;
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
tryLoad(fullPath, label, pluginManager, logger, options) {
|
|
40
|
+
try {
|
|
41
|
+
const mod = require(fullPath);
|
|
42
|
+
const plugin = mod.default || mod.plugin;
|
|
43
|
+
if (plugin && plugin.name) {
|
|
44
|
+
if (plugin.minMmaVersion &&
|
|
45
|
+
options?.mmaVersion &&
|
|
46
|
+
semverGt(plugin.minMmaVersion, options.mmaVersion)) {
|
|
47
|
+
logger?.warn(t("plugin.incompatible", {
|
|
48
|
+
name: plugin.name,
|
|
49
|
+
min: plugin.minMmaVersion,
|
|
50
|
+
current: options.mmaVersion,
|
|
51
|
+
}));
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const result = pluginManager.register(plugin, options?.source);
|
|
55
|
+
if (result.status === "registered") {
|
|
56
|
+
logger?.warn(t("plugin.loaded", { name: plugin.name }));
|
|
57
|
+
}
|
|
58
|
+
else if (result.status === "skipped_older") {
|
|
59
|
+
logger?.warn(t("plugin.dedup_older", {
|
|
60
|
+
name: plugin.name,
|
|
61
|
+
version: plugin.version || "0.0.0",
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
logger?.warn(t("plugin.skipped", { entry: label, message: e.message }));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|