agentlens / src /parsers /base.ts
hongcoding's picture
feat: 9-agent migration matrix (dsh/workbuddy/zcode/mimo), 81 combinations tested, 210 tests
031fd6d verified
Raw History Blame Contribute Delete
2.48 kB
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;
}