Elffuss BraidHub

kikocis/continue public

6e4b7e9fc0a8 · 1.5 KiBRawPermalink
1import {
2 ConfigValidationError,
3 markdownToRule,
4} from "@continuedev/config-yaml";
5import { IDE, RuleWithSource } from "../..";
6import { walkDirs } from "../../indexing/walkDir";
7import { RULES_MARKDOWN_FILENAME } from "../../llm/rules/constants";
8import { getUriPathBasename } from "../../util/uri";
9
10/**
11 * Loads rules from rules.md files colocated in the codebase
12 */
13export async function loadCodebaseRules(ide: IDE): Promise<{
14 rules: RuleWithSource[];
15 errors: ConfigValidationError[];
16}> {
17 const errors: ConfigValidationError[] = [];
18 const rules: RuleWithSource[] = [];
19
20 try {
21 // Get all files from the workspace
22 const allFiles = await walkDirs(ide);
23
24 // Filter to just rules.md files
25 const rulesMdFiles = allFiles.filter((file) => {
26 const filename = getUriPathBasename(file);
27 return filename === RULES_MARKDOWN_FILENAME;
28 });
29
30 // Process each rules.md file
31 for (const filePath of rulesMdFiles) {
32 try {
33 const content = await ide.readFile(filePath);
34 const rule = markdownToRule(content, { uriType: "file", filePath });
35
36 rules.push({ ...rule, source: "rules-block", ruleFile: filePath });
37 } catch (e) {
38 errors.push({
39 fatal: false,
40 message: `Failed to parse colocated rule file ${filePath}: ${e instanceof Error ? e.message : e}`,
41 });
42 }
43 }
44 } catch (e) {
45 errors.push({
46 fatal: false,
47 message: `Error loading colocated rule files: ${e instanceof Error ? e.message : e}`,
48 });
49 }
50
51 return { rules, errors };
52}