File size: 2,482 Bytes
7bedc79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
031fd6d
7bedc79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type { Rule, AgentKind, RuleCategory } from "../core/types.js";

/**
 * Parser interface: each agent config format implements this.
 */
export interface ConfigParser {
  agent: AgentKind;
  /** File patterns to look for (relative to project root) */
  filePatterns: string[];
  /** Parse raw file content into rules */
  parse(content: string, filePath: string): Rule[];
}

let ruleCounter = 0;
export function nextRuleId(): string {
  return `rule-${++ruleCounter}`;
}

/**
 * Classify a rule line into a category using keyword heuristics.
 * Order matters: constraints override style/workflow/tooling matches.
 */
export function classifyRule(text: string): RuleCategory {
  const lower = text.toLowerCase();
  // Constraint keywords win first (禁止/必须/never/always must not)
  if (/(must not|never|do not|don't|avoid|forbidden|required|禁止|必须|不要|不可)/.test(lower)) return "constraint";
  if (/(format|style|indent|naming|convention|lint|prettier|eslint)/.test(lower)) return "style";
  if (/(workflow|process|deploy|commit|branch|pr |pull request|review)/.test(lower)) return "workflow";
  if (/(use |prefer|tool|library|framework|import|依赖|使用)/.test(lower)) return "tooling";
  return "context";
}

/**
 * Split markdown content into rule blocks.
 * Each bullet/numbered list item is its own block.
 * Headings become blocks. Paragraphs are grouped.
 */
export function extractRuleBlocks(content: string): string[] {
  const blocks: string[] = [];
  const lines = content.split("\n");
  let paragraph: string[] = [];

  function flushParagraph() {
    if (paragraph.length > 0) {
      const text = paragraph.join(" ").trim();
      if (text.length > 5) blocks.push(text);
      paragraph = [];
    }
  }

  for (const line of lines) {
    const trimmed = line.trim();

    if (trimmed === "") {
      flushParagraph();
      continue;
    }

    // Headings are section labels, not rules: flush the paragraph and skip.
    if (/^#{1,6}\s/.test(trimmed)) {
      flushParagraph();
      continue;
    }

    // Bullet / numbered list item: each one is its own block
    if (/^[-*+]\s+/.test(trimmed) || /^\d+[.)]\s+/.test(trimmed)) {
      flushParagraph();
      const cleaned = trimmed.replace(/^[-*+]\s+/, "").replace(/^\d+[.)]\s+/, "").trim();
      if (cleaned.length > 5) blocks.push(cleaned);
      continue;
    }

    // Regular text: accumulate into paragraph
    paragraph.push(trimmed);
  }
  flushParagraph();

  return blocks;
}