mcp-cognition-engine 1.0.0-alpha.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.
Files changed (158) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +281 -0
  3. package/dist/cli.d.ts +18 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +66 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/cognition-engine/ast-constraint-solver.d.ts +67 -0
  8. package/dist/cognition-engine/ast-constraint-solver.d.ts.map +1 -0
  9. package/dist/cognition-engine/ast-constraint-solver.js +294 -0
  10. package/dist/cognition-engine/ast-constraint-solver.js.map +1 -0
  11. package/dist/cognition-engine/constraint-validator.d.ts +35 -0
  12. package/dist/cognition-engine/constraint-validator.d.ts.map +1 -0
  13. package/dist/cognition-engine/constraint-validator.js +55 -0
  14. package/dist/cognition-engine/constraint-validator.js.map +1 -0
  15. package/dist/cognition-engine/graph-traverser.d.ts +43 -0
  16. package/dist/cognition-engine/graph-traverser.d.ts.map +1 -0
  17. package/dist/cognition-engine/graph-traverser.js +205 -0
  18. package/dist/cognition-engine/graph-traverser.js.map +1 -0
  19. package/dist/cognition-engine/index.d.ts +51 -0
  20. package/dist/cognition-engine/index.d.ts.map +1 -0
  21. package/dist/cognition-engine/index.js +60 -0
  22. package/dist/cognition-engine/index.js.map +1 -0
  23. package/dist/cognition-engine/intent-recognizer.d.ts +25 -0
  24. package/dist/cognition-engine/intent-recognizer.d.ts.map +1 -0
  25. package/dist/cognition-engine/intent-recognizer.js +226 -0
  26. package/dist/cognition-engine/intent-recognizer.js.map +1 -0
  27. package/dist/cognition-engine/types.d.ts +96 -0
  28. package/dist/cognition-engine/types.d.ts.map +1 -0
  29. package/dist/cognition-engine/types.js +17 -0
  30. package/dist/cognition-engine/types.js.map +1 -0
  31. package/dist/conflict/arbitrator.d.ts +24 -0
  32. package/dist/conflict/arbitrator.d.ts.map +1 -0
  33. package/dist/conflict/arbitrator.js +43 -0
  34. package/dist/conflict/arbitrator.js.map +1 -0
  35. package/dist/index.d.ts +17 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +90 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/legacy-engine/ast-diff.d.ts +23 -0
  40. package/dist/legacy-engine/ast-diff.d.ts.map +1 -0
  41. package/dist/legacy-engine/ast-diff.js +146 -0
  42. package/dist/legacy-engine/ast-diff.js.map +1 -0
  43. package/dist/legacy-engine/ast-node.d.ts +24 -0
  44. package/dist/legacy-engine/ast-node.d.ts.map +1 -0
  45. package/dist/legacy-engine/ast-node.js +52 -0
  46. package/dist/legacy-engine/ast-node.js.map +1 -0
  47. package/dist/legacy-engine/parsers.d.ts +34 -0
  48. package/dist/legacy-engine/parsers.d.ts.map +1 -0
  49. package/dist/legacy-engine/parsers.js +134 -0
  50. package/dist/legacy-engine/parsers.js.map +1 -0
  51. package/dist/legacy-engine/regex-fallback.d.ts +23 -0
  52. package/dist/legacy-engine/regex-fallback.d.ts.map +1 -0
  53. package/dist/legacy-engine/regex-fallback.js +40 -0
  54. package/dist/legacy-engine/regex-fallback.js.map +1 -0
  55. package/dist/legacy-engine/rule-generator.d.ts +31 -0
  56. package/dist/legacy-engine/rule-generator.d.ts.map +1 -0
  57. package/dist/legacy-engine/rule-generator.js +54 -0
  58. package/dist/legacy-engine/rule-generator.js.map +1 -0
  59. package/dist/legacy-engine/rule-matcher.d.ts +28 -0
  60. package/dist/legacy-engine/rule-matcher.d.ts.map +1 -0
  61. package/dist/legacy-engine/rule-matcher.js +79 -0
  62. package/dist/legacy-engine/rule-matcher.js.map +1 -0
  63. package/dist/legacy-engine/token-controller.d.ts +31 -0
  64. package/dist/legacy-engine/token-controller.d.ts.map +1 -0
  65. package/dist/legacy-engine/token-controller.js +66 -0
  66. package/dist/legacy-engine/token-controller.js.map +1 -0
  67. package/dist/middleware/response-validation.d.ts +27 -0
  68. package/dist/middleware/response-validation.d.ts.map +1 -0
  69. package/dist/middleware/response-validation.js +50 -0
  70. package/dist/middleware/response-validation.js.map +1 -0
  71. package/dist/modes/confirm.d.ts +29 -0
  72. package/dist/modes/confirm.d.ts.map +1 -0
  73. package/dist/modes/confirm.js +27 -0
  74. package/dist/modes/confirm.js.map +1 -0
  75. package/dist/modes/silent.d.ts +24 -0
  76. package/dist/modes/silent.d.ts.map +1 -0
  77. package/dist/modes/silent.js +24 -0
  78. package/dist/modes/silent.js.map +1 -0
  79. package/dist/resources/cognition-resources.d.ts +28 -0
  80. package/dist/resources/cognition-resources.d.ts.map +1 -0
  81. package/dist/resources/cognition-resources.js +124 -0
  82. package/dist/resources/cognition-resources.js.map +1 -0
  83. package/dist/storage/client.d.ts +26 -0
  84. package/dist/storage/client.d.ts.map +1 -0
  85. package/dist/storage/client.js +45 -0
  86. package/dist/storage/client.js.map +1 -0
  87. package/dist/storage/cognition-repository.d.ts +55 -0
  88. package/dist/storage/cognition-repository.d.ts.map +1 -0
  89. package/dist/storage/cognition-repository.js +287 -0
  90. package/dist/storage/cognition-repository.js.map +1 -0
  91. package/dist/storage/cognition-types.d.ts +96 -0
  92. package/dist/storage/cognition-types.d.ts.map +1 -0
  93. package/dist/storage/cognition-types.js +41 -0
  94. package/dist/storage/cognition-types.js.map +1 -0
  95. package/dist/storage/conflict-repo.d.ts +41 -0
  96. package/dist/storage/conflict-repo.d.ts.map +1 -0
  97. package/dist/storage/conflict-repo.js +67 -0
  98. package/dist/storage/conflict-repo.js.map +1 -0
  99. package/dist/storage/diff-log-repo.d.ts +48 -0
  100. package/dist/storage/diff-log-repo.d.ts.map +1 -0
  101. package/dist/storage/diff-log-repo.js +51 -0
  102. package/dist/storage/diff-log-repo.js.map +1 -0
  103. package/dist/storage/metric-repo.d.ts +20 -0
  104. package/dist/storage/metric-repo.d.ts.map +1 -0
  105. package/dist/storage/metric-repo.js +36 -0
  106. package/dist/storage/metric-repo.js.map +1 -0
  107. package/dist/storage/rule-repo.d.ts +66 -0
  108. package/dist/storage/rule-repo.d.ts.map +1 -0
  109. package/dist/storage/rule-repo.js +188 -0
  110. package/dist/storage/rule-repo.js.map +1 -0
  111. package/dist/tools/analyze-workspace.d.ts +26 -0
  112. package/dist/tools/analyze-workspace.d.ts.map +1 -0
  113. package/dist/tools/analyze-workspace.js +147 -0
  114. package/dist/tools/analyze-workspace.js.map +1 -0
  115. package/dist/tools/capture-diff.d.ts +26 -0
  116. package/dist/tools/capture-diff.d.ts.map +1 -0
  117. package/dist/tools/capture-diff.js +58 -0
  118. package/dist/tools/capture-diff.js.map +1 -0
  119. package/dist/tools/cognition-tools.d.ts +60 -0
  120. package/dist/tools/cognition-tools.d.ts.map +1 -0
  121. package/dist/tools/cognition-tools.js +170 -0
  122. package/dist/tools/cognition-tools.js.map +1 -0
  123. package/dist/tools/config-tools.d.ts +29 -0
  124. package/dist/tools/config-tools.d.ts.map +1 -0
  125. package/dist/tools/config-tools.js +58 -0
  126. package/dist/tools/config-tools.js.map +1 -0
  127. package/dist/tools/confirm-rule.d.ts +32 -0
  128. package/dist/tools/confirm-rule.d.ts.map +1 -0
  129. package/dist/tools/confirm-rule.js +64 -0
  130. package/dist/tools/confirm-rule.js.map +1 -0
  131. package/dist/tools/injection-approval.d.ts +43 -0
  132. package/dist/tools/injection-approval.d.ts.map +1 -0
  133. package/dist/tools/injection-approval.js +102 -0
  134. package/dist/tools/injection-approval.js.map +1 -0
  135. package/dist/tools/list-rules.d.ts +24 -0
  136. package/dist/tools/list-rules.d.ts.map +1 -0
  137. package/dist/tools/list-rules.js +20 -0
  138. package/dist/tools/list-rules.js.map +1 -0
  139. package/dist/tools/query-rules.d.ts +25 -0
  140. package/dist/tools/query-rules.d.ts.map +1 -0
  141. package/dist/tools/query-rules.js +65 -0
  142. package/dist/tools/query-rules.js.map +1 -0
  143. package/dist/tools/resolve-conflict.d.ts +33 -0
  144. package/dist/tools/resolve-conflict.d.ts.map +1 -0
  145. package/dist/tools/resolve-conflict.js +35 -0
  146. package/dist/tools/resolve-conflict.js.map +1 -0
  147. package/dist/types.d.ts +190 -0
  148. package/dist/types.d.ts.map +1 -0
  149. package/dist/types.js +50 -0
  150. package/dist/types.js.map +1 -0
  151. package/docs/api-reference.md +1229 -0
  152. package/docs/community/feedback-playbook.md +94 -0
  153. package/docs/integration-snippets.md +100 -0
  154. package/docs/mcp-integration-guide.md +151 -0
  155. package/docs/phase4-mcp-feedback.md +155 -0
  156. package/docs/trust-governance-protocol.md +72 -0
  157. package/package.json +74 -0
  158. package/prisma/schema.prisma +145 -0
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * @deprecated LEGACY ENGINE MODULE — Preserved for reference only.
18
+ * Do NOT modify. The new cognition-engine module replaces this entire subsystem.
19
+ * See src/cognition-engine/ for the replacement.
20
+ */
21
+ import { AtomicOp, RuleConfidence, RuleSpec } from "../types.js";
22
+ export interface RuleCandidateEval {
23
+ generate: boolean;
24
+ ruleCandidate?: RuleSpec & {
25
+ confidence?: RuleConfidence;
26
+ };
27
+ reason: string;
28
+ confidence: RuleConfidence;
29
+ }
30
+ export declare function evaluateRuleCandidate(ops: AtomicOp[], language: string, distinctFiles: number, repeatCount: number, windowDays: number): RuleCandidateEval;
31
+ //# sourceMappingURL=rule-generator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule-generator.d.ts","sourceRoot":"","sources":["../../src/legacy-engine/rule-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,QAAQ,EAA8B,MAAM,aAAa,CAAC;AAE7F,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,OAAO,CAAC;IAClB,aAAa,CAAC,EAAE,QAAQ,GAAG;QAAE,UAAU,CAAC,EAAE,cAAc,CAAA;KAAE,CAAC;IAC3D,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,cAAc,CAAC;CAC5B;AAED,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,QAAQ,EAAE,EAAE,QAAQ,EAAE,MAAM,EACjC,aAAa,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAC7D,iBAAiB,CA4BnB"}
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * @deprecated LEGACY ENGINE MODULE — Preserved for reference only.
18
+ * Do NOT modify. The new cognition-engine module replaces this entire subsystem.
19
+ * See src/cognition-engine/ for the replacement.
20
+ */
21
+ import { RULE_GENERATION_THRESHOLDS } from "../types.js";
22
+ export function evaluateRuleCandidate(ops, language, distinctFiles, repeatCount, windowDays) {
23
+ if (ops.length === 0) {
24
+ return { generate: false, reason: "no operations", confidence: "medium" };
25
+ }
26
+ const meetsFileThreshold = distinctFiles >= RULE_GENERATION_THRESHOLDS.minDistinctFiles;
27
+ const meetsRepeatThreshold = repeatCount >= RULE_GENERATION_THRESHOLDS.minRepeatsInDays;
28
+ if (!meetsFileThreshold && !meetsRepeatThreshold) {
29
+ return { generate: false, reason: `below threshold: ${distinctFiles} files (need ${RULE_GENERATION_THRESHOLDS.minDistinctFiles}), ${repeatCount} repeats (need ${RULE_GENERATION_THRESHOLDS.minRepeatsInDays})`, confidence: "medium" };
30
+ }
31
+ const updateOps = ops.filter(o => o.type === "UPDATE");
32
+ const moveOps = ops.filter(o => o.type === "MOVE");
33
+ let ruleType = "replace";
34
+ if (moveOps.length > 0 && moveOps.length >= updateOps.length)
35
+ ruleType = "restructure";
36
+ if (ops.every(o => o.type === "INSERT" || o.type === "DELETE")) {
37
+ return { generate: false, reason: "only insert/delete ops with insufficient pattern", confidence: "low" };
38
+ }
39
+ const dominantUpdate = updateOps.length > 0 ? updateOps[0] : ops[0];
40
+ const pattern = dominantUpdate.originalText ?? "";
41
+ const suggestion = dominantUpdate.modifiedText ?? "";
42
+ let confidence = "high";
43
+ if (ops.length > 3)
44
+ confidence = "low";
45
+ else if (ops.some(o => o.type === "INSERT" || o.type === "DELETE"))
46
+ confidence = "medium";
47
+ return {
48
+ generate: true,
49
+ ruleCandidate: { type: ruleType, pattern, suggestion, language, confidence },
50
+ reason: `meets threshold: ${distinctFiles} files, ${repeatCount} repeats`,
51
+ confidence,
52
+ };
53
+ }
54
+ //# sourceMappingURL=rule-generator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule-generator.js","sourceRoot":"","sources":["../../src/legacy-engine/rule-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;GAIG;AAEH,OAAO,EAAsC,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAS7F,MAAM,UAAU,qBAAqB,CACnC,GAAe,EAAE,QAAgB,EACjC,aAAqB,EAAE,WAAmB,EAAE,UAAkB;IAE9D,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,UAAU,EAAE,QAA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O,GAAG,cAAc,CAAC,YAAY,IAAI,EAAE,CAAC;IAClD,MAAM,UAAU,GAAG,cAAc,CAAC,YAAY,IAAI,EAAE,CAAC;IACrD,IAAI,UAAU,GAAmB,MAAM,CAAC;IACxC,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC;QAAE,UAAU,GAAG,KAAK,CAAC;SAClC,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC;QAAE,UAAU,GAAG,QAAQ,CAAC;IAC1F,OAAO;QACL,QAAQ,EAAE,IAAI;QACd,aAAa,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE;QAC5E,MAAM,EAAE,oBAAoB,aAAa,WAAW,WAAW,UAAU;QACzE,UAAU;KACX,CAAC;AACJ,CAAC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * @deprecated LEGACY ENGINE MODULE — Preserved for reference only.
18
+ * Do NOT modify. The new cognition-engine module replaces this entire subsystem.
19
+ * See src/cognition-engine/ for the replacement.
20
+ */
21
+ import { Rule, MatchContext, MatchResult } from "../types.js";
22
+ export declare function computeScore(rule: Rule, context: MatchContext): number;
23
+ export interface MatchOptions {
24
+ topK?: number;
25
+ maxTokens?: number;
26
+ }
27
+ export declare function matchRules(rules: Rule[], context: MatchContext, options?: MatchOptions): MatchResult;
28
+ //# sourceMappingURL=rule-matcher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule-matcher.d.ts","sourceRoot":"","sources":["../../src/legacy-engine/rule-matcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;GAIG;AAEH,OAAO,EAAE,IAAI,EAAc,YAAY,EAAE,WAAW,EAAqC,MAAM,aAAa,CAAC;AAG7G,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,YAAY,GAAG,MAAM,CA6BtE;AAED,MAAM,WAAW,YAAY;IAAG,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAAE;AAEpE,wBAAgB,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO,GAAE,YAAiB,GAAG,WAAW,CAoBxG"}
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * @deprecated LEGACY ENGINE MODULE — Preserved for reference only.
18
+ * Do NOT modify. The new cognition-engine module replaces this entire subsystem.
19
+ * See src/cognition-engine/ for the replacement.
20
+ */
21
+ import { DEFAULT_WEIGHTS, SCOPE_PRIORITIES } from "../types.js";
22
+ import { truncateRules } from "./token-controller.js";
23
+ export function computeScore(rule, context) {
24
+ if (rule.language !== "*" && rule.language !== context.language)
25
+ return 0;
26
+ const now = context.currentTime ?? new Date();
27
+ const { typeWeight, timeWeight, matchWeight, timeDecayLambda } = DEFAULT_WEIGHTS;
28
+ const typeValue = rule.type === "replace" ? 1.0 : rule.type === "restructure" ? 0.8 : 0.6;
29
+ const hoursSinceCreation = (now.getTime() - rule.createdAt.getTime()) / 3600000;
30
+ const timeValue = Math.exp(-timeDecayLambda * hoursSinceCreation);
31
+ let matchValue = 0;
32
+ const path = context.filePath.toLowerCase();
33
+ const content = context.fileContent?.toLowerCase() ?? "";
34
+ const contextTags = context.ruleTags ?? [];
35
+ if (rule.tags) {
36
+ for (const tag of rule.tags) {
37
+ if (path.includes(tag.toLowerCase()))
38
+ matchValue += 1;
39
+ }
40
+ for (const tag of contextTags) {
41
+ if (rule.tags.some(t => t.toLowerCase() === tag.toLowerCase()))
42
+ matchValue += 1;
43
+ }
44
+ }
45
+ // Content-based pattern matching: if rule pattern appears in file content, strong signal
46
+ if (content && rule.pattern) {
47
+ const patternLower = rule.pattern.toLowerCase();
48
+ if (content.includes(patternLower)) {
49
+ matchValue += 2;
50
+ }
51
+ }
52
+ const priorityBonus = SCOPE_PRIORITIES[rule.scope ?? "project"] ?? 0.5;
53
+ const score = (typeWeight * typeValue) + (timeWeight * timeValue) + (matchWeight * (matchValue / Math.max(matchValue, 1)));
54
+ return score * priorityBonus;
55
+ }
56
+ export function matchRules(rules, context, options = {}) {
57
+ const startTime = performance.now();
58
+ const topK = options.topK ?? 10;
59
+ const scored = rules.map(rule => {
60
+ const score = computeScore(rule, context);
61
+ const matchReasons = [];
62
+ if (rule.language === context.language || rule.language === "*")
63
+ matchReasons.push("language_match");
64
+ if (rule.tags?.some(t => context.filePath.toLowerCase().includes(t.toLowerCase())))
65
+ matchReasons.push("path_match");
66
+ // New match reason for content-based pattern matching
67
+ if (context.fileContent && rule.pattern && context.fileContent.toLowerCase().includes(rule.pattern.toLowerCase())) {
68
+ matchReasons.push("content_match");
69
+ }
70
+ return { rule, score, matchReasons };
71
+ }).filter(s => s.score > 0);
72
+ scored.sort((a, b) => b.score - a.score);
73
+ const topScored = scored.slice(0, topK);
74
+ const { rules: selected, totalTokens, truncated } = truncateRules(topScored.map(s => s.rule), options.maxTokens ?? 2000);
75
+ const resultMap = new Map(topScored.map(s => [s.rule.id, s]));
76
+ const finalScored = selected.map(r => resultMap.get(r.id)).filter((s) => s !== undefined);
77
+ return { rules: finalScored, totalTokens, truncated, queryDurationMs: performance.now() - startTime };
78
+ }
79
+ //# sourceMappingURL=rule-matcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rule-matcher.js","sourceRoot":"","sources":["../../src/legacy-engine/rule-matcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;GAIG;AAEH,OAAO,EAA+C,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC7G,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,MAAM,UAAU,YAAY,CAAC,IAAU,EAAE,OAAqB;IAC5D,IAAI,IAAI,CAAC,QAAQ,KAAK,GAAG,IAAI,IAAI,CAAC,QAAQ,KAAK,OAAO,CAAC,QAAQ;QAAE,OAAO,CAAC,CAAC;IAC1E,MAAM,GAAG,GAAG,OAAO,CAAC,WAAW,IAAI,IAAI,IAAI,EAAE,CAAC;IAC9C,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,GAAG,eAAe,CAAC;IACjF,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;IAC1F,MAAM,kBAAkB,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC;IAChF,MAAM,SAAS,GAAG,IAAI,CA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M,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IACzC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IACxC,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,GAAG,aAAa,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC;IACzH,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9D,MAAM,WAAW,GAAiB,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAmB,EAAE,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC;IACzH,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,SAAS,EAAE,CAAC;AACxG,CAAC"}
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * @deprecated LEGACY ENGINE MODULE — Preserved for reference only.
18
+ * Do NOT modify. The new cognition-engine module replaces this entire subsystem.
19
+ * See src/cognition-engine/ for the replacement.
20
+ */
21
+ import { Rule } from "../types.js";
22
+ export declare function getSessionTokens(taskId: string): number;
23
+ export declare function addSessionTokens(taskId: string, tokens: number): number;
24
+ export declare function clearSession(taskId: string): void;
25
+ export declare function estimateTokens(text: string): number;
26
+ export declare function truncateRules(rules: Rule[], maxTokens?: number, taskId?: string): {
27
+ rules: Rule[];
28
+ totalTokens: number;
29
+ truncated: boolean;
30
+ };
31
+ //# sourceMappingURL=token-controller.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-controller.d.ts","sourceRoot":"","sources":["../../src/legacy-engine/token-controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;GAIG;AAEH,OAAO,EAAE,IAAI,EAAgB,MAAM,aAAa,CAAC;AAKhD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEvD;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAKvE;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAEjD;AAEF,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGnD;AAQD,wBAAgB,aAAa,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,SAAS,GAAE,MAAwC,EAAE,MAAM,CAAC,EAAE,MAAM;;;;EAgBhH"}
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * @deprecated LEGACY ENGINE MODULE — Preserved for reference only.
18
+ * Do NOT modify. The new cognition-engine module replaces this entire subsystem.
19
+ * See src/cognition-engine/ for the replacement.
20
+ */
21
+ import { TOKEN_LIMITS } from "../types.js";
22
+ /** Session-level token tracking keyed by taskId */
23
+ const sessionTokens = new Map();
24
+ export function getSessionTokens(taskId) {
25
+ return sessionTokens.get(taskId) ?? 0;
26
+ }
27
+ export function addSessionTokens(taskId, tokens) {
28
+ const current = sessionTokens.get(taskId) ?? 0;
29
+ const updated = current + tokens;
30
+ sessionTokens.set(taskId, updated);
31
+ return updated;
32
+ }
33
+ export function clearSession(taskId) {
34
+ sessionTokens.delete(taskId);
35
+ }
36
+ export function estimateTokens(text) {
37
+ const bytes = new TextEncoder().encode(text).length;
38
+ return Math.ceil(bytes / 3.5);
39
+ }
40
+ function formatRule(r) {
41
+ const tagsStr = r.tags?.length ? " [" + r.tags.join(", ") + "]" : "";
42
+ const extStr = r.fileExtensions?.length ? " (files: " + r.fileExtensions.join(", ") + ")" : "";
43
+ return `[${r.type}] ${r.pattern} → ${r.suggestion}${extStr}${tagsStr}${r.priority !== 1.0 ? " priority:" + r.priority : ""}`;
44
+ }
45
+ export function truncateRules(rules, maxTokens = TOKEN_LIMITS.maxInjectionTokens, taskId) {
46
+ let totalTokens = 0;
47
+ const selected = [];
48
+ const sessionUsed = taskId ? getSessionTokens(taskId) : 0;
49
+ const budget = Math.min(maxTokens, TOKEN_LIMITS.maxInjectionTokens) - sessionUsed;
50
+ if (budget <= 0)
51
+ return { rules: [], totalTokens: 0, truncated: true };
52
+ for (const rule of rules) {
53
+ const formatted = formatRule(rule);
54
+ const tokens = estimateTokens(formatted);
55
+ if (tokens > TOKEN_LIMITS.maxSingleRuleTokens)
56
+ continue;
57
+ if (totalTokens + tokens > budget)
58
+ break;
59
+ selected.push(rule);
60
+ totalTokens += tokens;
61
+ }
62
+ if (taskId && totalTokens > 0)
63
+ addSessionTokens(taskId, totalTokens);
64
+ return { rules: selected, totalTokens, truncated: selected.length < rules.length };
65
+ }
66
+ //# sourceMappingURL=token-controller.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-controller.js","sourceRoot":"","sources":["../../src/legacy-engine/token-controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;;GAIG;AAEH,OAAO,EAAQ,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,mDAAmD;AACnD,MAAM,aAAa,GAAG,IAAI,GAAG,EAAkB,CAAC;AAEhD,MAAM,UAAU,gBAAgB,CAAC,MAAc;IAC7C,OAAO,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,MAAc,EAAE,MAAc;IAC7D,MAAM,OAAO,GAAG,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAC/C,MAAM,OAAO,GAAG,OAAO,GAAG,MAAM,CAAC;IACjC,aAAa,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,MAAc;IACzC,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC;AAEF,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,MAAM,KAAK,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC;IACpD,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC;AAChC,CAAC;AAED,SAAS,UAAU,CAAC,CAAO;IACzB,MAAM,OAAO,GAAG,CAAC,CA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AG,YAAY,CAAC,mBAAmB;YAAE,SAAS;QACxD,IAAI,WAAW,GAAG,MAAM,GAAG,MAAM;YAAE,MAAM;QACzC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpB,WAAW,IAAI,MAAM,CAAC;IACxB,CAAC;IACD,IAAI,MAAM,IAAI,WAAW,GAAG,CAAC;QAAE,gBAAgB,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACrE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;AACrF,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export declare function validateToolResponse(toolName: string, response: {
17
+ content: {
18
+ type: string;
19
+ text: string;
20
+ }[];
21
+ }): {
22
+ content: {
23
+ type: string;
24
+ text: string;
25
+ }[];
26
+ };
27
+ //# sourceMappingURL=response-validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response-validation.d.ts","sourceRoot":"","sources":["../../src/middleware/response-validation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAcH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE;IAAE,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAAE,GAAG;IAAE,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAAE,CAa7J"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * @file Response Validation Middleware
18
+ * Validates tools/call responses for Schema compliance.
19
+ * Auto-adds validationRequired if missing, logs WARN.
20
+ */
21
+ import { writeFileSync, existsSync, mkdirSync } from "node:fs";
22
+ import { join, dirname } from "node:path";
23
+ import { fileURLToPath } from "node:url";
24
+ const __dirname = dirname(fileURLToPath(import.meta.url));
25
+ const LOG_DIR = join(__dirname, "../../logs");
26
+ export function validateToolResponse(toolName, response) {
27
+ try {
28
+ for (const item of response.content) {
29
+ if (item.type !== "text")
30
+ continue;
31
+ const data = JSON.parse(item.text);
32
+ if ((toolName === "cognition_query" || toolName === "cognition_validate") && data.validationRequired === undefined) {
33
+ data.validationRequired = true;
34
+ item.text = JSON.stringify(data);
35
+ logWarn("Auto-patched validationRequired for " + toolName);
36
+ }
37
+ }
38
+ }
39
+ catch { /* non-JSON content */ }
40
+ return response;
41
+ }
42
+ function logWarn(msg) {
43
+ try {
44
+ if (!existsSync(LOG_DIR))
45
+ mkdirSync(LOG_DIR, { recursive: true });
46
+ writeFileSync(join(LOG_DIR, "validation-warnings.log"), "[" + new Date().toISOString() + "] WARN: " + msg + "\n", { flag: "a" });
47
+ }
48
+ catch { /* silent */ }
49
+ }
50
+ //# sourceMappingURL=response-validation.js.map
@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CAAC,OAAO,CAAC;YAAE,SAAS,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAClE,aAAa,CAAC,IAAI,CAAC,OAAO,EAAE,yBAAyB,CAAC,EAAE,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,GAAG,UAAU,GAAG,GAAG,GAAG,IAAI,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,CAAC;IACnI,CAAC;IAAC,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC;AAC1B,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { AtomicOp, RuleSpec } from "../types.js";
17
+ import { MetricRepo } from "../storage/metric-repo.js";
18
+ export type ConfirmAction = "accept" | "reject" | "edit" | "skip";
19
+ export interface ConfirmCard {
20
+ title: string;
21
+ ruleSpec: RuleSpec;
22
+ actions: ConfirmAction[];
23
+ message: string;
24
+ }
25
+ export declare function buildConfirmCard(ops: AtomicOp[], language: string, distinctFiles: number, repeatCount: number, windowDays: number, metricRepo: MetricRepo): Promise<{
26
+ shouldShow: boolean;
27
+ card?: ConfirmCard;
28
+ }>;
29
+ //# sourceMappingURL=confirm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"confirm.d.ts","sourceRoot":"","sources":["../../src/modes/confirm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAEvD,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAElE,MAAM,WAAW,WAAW;IAAG,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,aAAa,EAAE,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;CAAE;AAE9G,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,QAAQ,EAAE,EAAE,QAAQ,EAAE,MAAM,EACjC,aAAa,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GACrF,OAAO,CAAC;IAAE,UAAU,EAAE,OAAO,CAAC;IAAC,IAAI,CAAC,EAAE,WAAW,CAAA;CAAE,CAAC,CAQtD"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { evaluateRuleCandidate } from "../legacy-engine/rule-generator.js";
17
+ export async function buildConfirmCard(ops, language, distinctFiles, repeatCount, windowDays, metricRepo) {
18
+ const evalResult = evaluateRuleCandidate(ops, language, distinctFiles, repeatCount, windowDays);
19
+ await metricRepo.track("confirm_mode_eval", { opsCount: ops.length, language, generated: evalResult.generate });
20
+ if (!evalResult.generate || !evalResult.ruleCandidate)
21
+ return { shouldShow: false };
22
+ return {
23
+ shouldShow: true,
24
+ card: { title: "检测到新的编码规则候选", ruleSpec: evalResult.ruleCandidate, actions: ["accept", "reject", "edit", "skip"], message: `类型: ${evalResult.ruleCandidate.type}\n模式: ${evalResult.ruleCandidate.pattern}\n建议: ${evalResult.ruleCandidate.suggestion}\n置信度: ${evalResult.ruleCandidate.confidence}` },
25
+ };
26
+ }
27
+ //# sourceMappingURL=confirm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"confirm.js","sourceRoot":"","sources":["../../src/modes/confirm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAO3E,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,GAAe,EAAE,QAAgB,EACjC,aAAqB,EAAE,WAAmB,EAAE,UAAkB,EAAE,UAAsB;IAEtF,MAAM,UAAU,GAAG,qBAAqB,CAAC,GAAG,EAAE,QAAQ,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,CAAC,CAAC;IAChG,MAAM,UAAU,CAAC,KAAK,CAAC,mBAAmB,EAAE,EAAE,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAC;IAChH,IAAI,CAAC,UAAU,CAAC,QAAQ,IAAI,CAAC,UAAU,CAAC,aAAa;QAAE,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC;IACpF,OAAO;QACL,UAAU,EAAE,IAAI;QAChB,IAAI,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,UAAU,CAAC,aAAa,EAAE,OAAO,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,OAAO,UAAU,CAAC,aAAa,CAAC,IAAI,SAAS,UAAU,CAAC,aAAa,CAAC,OAAO,SAAS,UAAU,CAAC,aAAa,CAAC,UAAU,UAAU,UAAU,CAAC,aAAa,CAAC,UAAU,EAAE,EAAE;KACrS,CAAC;AACJ,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { AtomicOp, RuleSpec } from "../types.js";
17
+ import { MetricRepo } from "../storage/metric-repo.js";
18
+ export interface SilentModeResult {
19
+ generatedRule: boolean;
20
+ ruleSpec?: RuleSpec;
21
+ notification?: string;
22
+ }
23
+ export declare function processSilent(ops: AtomicOp[], language: string, distinctFiles: number, repeatCount: number, windowDays: number, metricRepo: MetricRepo): Promise<SilentModeResult>;
24
+ //# sourceMappingURL=silent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"silent.d.ts","sourceRoot":"","sources":["../../src/modes/silent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAEvD,MAAM,WAAW,gBAAgB;IAAG,aAAa,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAC;CAAE;AAEzG,wBAAsB,aAAa,CACjC,GAAG,EAAE,QAAQ,EAAE,EAAE,QAAQ,EAAE,MAAM,EACjC,aAAa,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GACrF,OAAO,CAAC,gBAAgB,CAAC,CAK3B"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { evaluateRuleCandidate } from "../legacy-engine/rule-generator.js";
17
+ export async function processSilent(ops, language, distinctFiles, repeatCount, windowDays, metricRepo) {
18
+ const evalResult = evaluateRuleCandidate(ops, language, distinctFiles, repeatCount, windowDays);
19
+ await metricRepo.track("silent_mode_process", { opsCount: ops.length, language, generated: evalResult.generate });
20
+ if (!evalResult.generate)
21
+ return { generatedRule: false };
22
+ return { generatedRule: true, ruleSpec: evalResult.ruleCandidate, notification: `已学习新规则: ${evalResult.ruleCandidate.type} — ${evalResult.ruleCandidate.pattern}` };
23
+ }
24
+ //# sourceMappingURL=silent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"silent.js","sourceRoot":"","sources":["../../src/modes/silent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAK3E,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,GAAe,EAAE,QAAgB,EACjC,aAAqB,EAAE,WAAmB,EAAE,UAAkB,EAAE,UAAsB;IAEtF,MAAM,UAAU,GAAG,qBAAqB,CAAC,GAAG,EAAE,QAAQ,EAAE,aAAa,EAAE,WAAW,EAAE,UAAU,CAAC,CAAC;IAChG,MAAM,UAAU,CAAC,KAAK,CAAC,qBAAqB,EAAE,EAAE,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAC;IAClH,IAAI,CAAC,UAAU,CAAC,QAAQ;QAAE,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC;IAC1D,OAAO,EAAE,aAAa,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,CAAC,aAAa,EAAE,YAAY,EAAE,WAAW,UAAU,CAAC,aAAc,CAAC,IAAI,MAAM,UAAU,CAAC,aAAc,CAAC,OAAO,EAAE,EAAE,CAAC;AACvK,CAAC"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Copyright 2026 熊高锐
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ export declare const RESOURCES: {
17
+ uri: string;
18
+ name: string;
19
+ description: string;
20
+ mimeType: string;
21
+ }[];
22
+ /** Return the JSON schema for the cognition graph data model. */
23
+ export declare function readCognitionSchema(): Promise<string>;
24
+ /** Return current graph statistics with approval rate. */
25
+ export declare function readCognitionStats(): Promise<string>;
26
+ /** Return the integration documentation markdown. */
27
+ export declare function readCognitionDocs(): Promise<string>;
28
+ //# sourceMappingURL=cognition-resources.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cognition-resources.d.ts","sourceRoot":"","sources":["../../src/resources/cognition-resources.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAoBH,eAAO,MAAM,SAAS;;;;;GAyBrB,CAAC;AAIF,iEAAiE;AACjE,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,MAAM,CAAC,CAuC3D;AAED,0DAA0D;AAC1D,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC,CAe1D;AAED,qDAAqD;AACrD,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC,CAMzD"}