| 1 | import { |
| 2 | ConfigValidationError, |
| 3 | markdownToRule, |
| 4 | } from "@continuedev/config-yaml"; |
| 5 | import { IDE, RuleWithSource } from "../.."; |
| 6 | import { findUriInDirs } from "../../util/uri"; |
| 7 | import { getAllDotContinueDefinitionFiles } from "../loadLocalAssistants"; |
| 8 | |
| 9 | /** |
| 10 | * Loads rules from markdown files in the .continue/rules directory |
| 11 | */ |
| 12 | export async function loadMarkdownRules(ide: IDE): Promise<{ |
| 13 | rules: RuleWithSource[]; |
| 14 | errors: ConfigValidationError[]; |
| 15 | }> { |
| 16 | const errors: ConfigValidationError[] = []; |
| 17 | const rules: RuleWithSource[] = []; |
| 18 | |
| 19 | try { |
| 20 | // Get all .md files from .continue/rules |
| 21 | const markdownFiles = await getAllDotContinueDefinitionFiles( |
| 22 | ide, |
| 23 | { includeGlobal: true, includeWorkspace: true, fileExtType: "markdown" }, |
| 24 | "rules", |
| 25 | ); |
| 26 | |
| 27 | // Filter to just .md files |
| 28 | const mdFiles = markdownFiles.filter((file) => file.path.endsWith(".md")); |
| 29 | |
| 30 | // Process each markdown file |
| 31 | for (const file of mdFiles) { |
| 32 | try { |
| 33 | const { relativePathOrBasename } = findUriInDirs( |
| 34 | file.path, |
| 35 | await ide.getWorkspaceDirs(), |
| 36 | ); |
| 37 | const rule = markdownToRule(file.content, { |
| 38 | uriType: "file", |
| 39 | fileUri: relativePathOrBasename, |
| 40 | }); |
| 41 | rules.push({ ...rule, source: "rules-block", ruleFile: file.path }); |
| 42 | } catch (e) { |
| 43 | errors.push({ |
| 44 | fatal: false, |
| 45 | message: `Failed to parse markdown rule file ${file.path}: ${e instanceof Error ? e.message : e}`, |
| 46 | }); |
| 47 | } |
| 48 | } |
| 49 | } catch (e) { |
| 50 | errors.push({ |
| 51 | fatal: false, |
| 52 | message: `Error loading markdown rule files: ${e instanceof Error ? e.message : e}`, |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | return { rules, errors }; |
| 57 | } |