@shahmilsaari/memory-core 0.1.5 → 0.1.6
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/cli.js +65 -13
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -788,6 +788,30 @@ import { execSync as execSync2 } from "child_process";
|
|
|
788
788
|
import { existsSync as existsSync5, readFileSync as readFileSync4 } from "fs";
|
|
789
789
|
import { join as join5, relative } from "path";
|
|
790
790
|
import chalk2 from "chalk";
|
|
791
|
+
function getFileLines(filePath) {
|
|
792
|
+
try {
|
|
793
|
+
return readFileSync4(filePath, "utf-8").split("\n");
|
|
794
|
+
} catch {
|
|
795
|
+
return [];
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
function printCodeContext(filePath, line, contextLines = 2) {
|
|
799
|
+
const lines = getFileLines(filePath);
|
|
800
|
+
if (lines.length === 0) return;
|
|
801
|
+
const start = Math.max(0, line - 1 - contextLines);
|
|
802
|
+
const end = Math.min(lines.length - 1, line - 1 + contextLines);
|
|
803
|
+
console.log(chalk2.dim(" \u250C\u2500 code \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"));
|
|
804
|
+
for (let i = start; i <= end; i++) {
|
|
805
|
+
const lineNum = String(i + 1).padStart(4, " ");
|
|
806
|
+
const isViolation = i === line - 1;
|
|
807
|
+
if (isViolation) {
|
|
808
|
+
console.log(chalk2.red(` \u2502 ${lineNum} \u25B6 ${lines[i]}`));
|
|
809
|
+
} else {
|
|
810
|
+
console.log(chalk2.dim(` \u2502 ${lineNum} ${lines[i]}`));
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
console.log(chalk2.dim(" \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"));
|
|
814
|
+
}
|
|
791
815
|
var SOURCE_EXTENSIONS = /\.(ts|tsx|js|jsx|py|php|rb|go|java|cs|swift|kt|rs|vue|svelte)$/;
|
|
792
816
|
var reasonMap2 = new Map(
|
|
793
817
|
seeds.filter((s) => s.reason).map((s) => [s.content, s.reason])
|
|
@@ -863,7 +887,17 @@ Things that must never appear:
|
|
|
863
887
|
${avoids.map((a, i) => `${i + 1}. ${a}`).join("\n")}
|
|
864
888
|
|
|
865
889
|
IMPORTANT: Respond with JSON: {"violations":[...]} or {"violations":[]}.
|
|
866
|
-
Each violation
|
|
890
|
+
Each violation must include ALL of these fields:
|
|
891
|
+
- "rule": the rule that was broken (exact text)
|
|
892
|
+
- "file": filename
|
|
893
|
+
- "line": line number where the violation occurs (integer)
|
|
894
|
+
- "code": the exact bad line of code (just that one line, no line number prefix)
|
|
895
|
+
- "issue": plain English description of what is wrong
|
|
896
|
+
- "suggestion": concrete fix \u2014 what to write instead
|
|
897
|
+
- "reason": copy the WHY from the rule to explain why this matters
|
|
898
|
+
- "severity": "error" for definite violations, "warning" for possible violations
|
|
899
|
+
|
|
900
|
+
Example: {"violations":[{"rule":"Thin controllers","file":"user.ts","line":34,"code":"const hash = await bcrypt.hash(password, 10)","issue":"Password hashing inside controller","suggestion":"Move to UserService.hashPassword()","reason":"Business logic in controllers cannot be reused","severity":"error"}]}
|
|
867
901
|
No text outside the JSON.`;
|
|
868
902
|
try {
|
|
869
903
|
const res = await fetch(`${ollamaUrl}/api/chat`, {
|
|
@@ -909,22 +943,40 @@ ${diffToSend}` }
|
|
|
909
943
|
console.log(chalk2.green(` \u2713 ${rel}`) + chalk2.dim(" \u2014 no violations"));
|
|
910
944
|
return;
|
|
911
945
|
}
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
`)
|
|
916
|
-
|
|
946
|
+
const errors = violations.filter((v) => v.severity !== "warning").length;
|
|
947
|
+
const warnings = violations.filter((v) => v.severity === "warning").length;
|
|
948
|
+
const summary = [
|
|
949
|
+
errors > 0 ? chalk2.red.bold(`${errors} error${errors > 1 ? "s" : ""}`) : "",
|
|
950
|
+
warnings > 0 ? chalk2.yellow.bold(`${warnings} warning${warnings > 1 ? "s" : ""}`) : ""
|
|
951
|
+
].filter(Boolean).join(chalk2.dim(", "));
|
|
952
|
+
console.log(`
|
|
953
|
+
${chalk2.red.bold("\u2717")} ${chalk2.bold(rel)} \u2014 ${summary}
|
|
954
|
+
`);
|
|
917
955
|
violations.forEach((v, i) => {
|
|
918
|
-
const
|
|
919
|
-
|
|
920
|
-
|
|
956
|
+
const isWarning = v.severity === "warning";
|
|
957
|
+
const badge = isWarning ? chalk2.yellow.bold(" WARN ") : chalk2.red.bold(" ERR ");
|
|
958
|
+
const loc = v.line ? chalk2.dim(`line ${v.line}`) : "";
|
|
959
|
+
console.log(` ${badge} ${chalk2.bold(v.rule)} ${loc}`);
|
|
921
960
|
const why = v.reason ?? reasonMap2.get(v.rule);
|
|
922
|
-
if (why)
|
|
923
|
-
|
|
924
|
-
|
|
961
|
+
if (why) {
|
|
962
|
+
console.log(chalk2.dim(` Why: ${why}`));
|
|
963
|
+
}
|
|
964
|
+
console.log();
|
|
965
|
+
if (v.line && existsSync5(filePath)) {
|
|
966
|
+
printCodeContext(filePath, v.line);
|
|
967
|
+
} else if (v.code) {
|
|
968
|
+
console.log(chalk2.dim(" \u250C\u2500 code \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"));
|
|
969
|
+
console.log(chalk2.red(` \u2502 \u25B6 ${v.code}`));
|
|
970
|
+
console.log(chalk2.dim(" \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"));
|
|
971
|
+
}
|
|
972
|
+
console.log();
|
|
973
|
+
if (v.issue) console.log(chalk2.red(` Problem: `) + v.issue);
|
|
974
|
+
if (v.suggestion) console.log(chalk2.green(` Fix: `) + chalk2.green(v.suggestion));
|
|
975
|
+
console.log();
|
|
976
|
+
console.log(chalk2.dim(" " + "\u2500".repeat(55)));
|
|
925
977
|
console.log();
|
|
926
978
|
});
|
|
927
|
-
console.log(chalk2.dim('
|
|
979
|
+
console.log(chalk2.dim(' Run: memory-core remember "<lesson>" to save this as a rule'));
|
|
928
980
|
console.log();
|
|
929
981
|
} catch (err) {
|
|
930
982
|
if (err.cause?.code === "ECONNREFUSED" || err.message?.includes("ECONNREFUSED")) {
|