Elffuss BraidHub

kikocis/continue public

main · 2.8 KiBRawPermalink
1import {
2 ConfigValidationError,
3 markdownToRule,
4} from "@continuedev/config-yaml";
5import { IDE, RuleWithSource } from "../..";
6import { findUriInDirs, joinPathsToUri } from "../../util/uri";
7import { getAllDotContinueDefinitionFiles } from "../loadLocalAssistants";
8
9export const SUPPORTED_AGENT_FILES = ["AGENTS.md", "AGENT.md", "CLAUDE.md"];
10/**
11 * Loads rules from markdown files in the .continue/rules directory
12 * and agent files (AGENTS.md, AGENT.md, CLAUDE.md) at workspace root
13 */
14export async function loadMarkdownRules(ide: IDE): Promise<{
15 rules: RuleWithSource[];
16 errors: ConfigValidationError[];
17}> {
18 const errors: ConfigValidationError[] = [];
19 const rules: RuleWithSource[] = [];
20
21 // First, try to load agent files from workspace root
22 const workspaceDirs = await ide.getWorkspaceDirs();
23
24 for (const workspaceDir of workspaceDirs) {
25 let agentFileFound = false;
26 for (const fileName of SUPPORTED_AGENT_FILES) {
27 try {
28 const agentFilePath = joinPathsToUri(workspaceDir, fileName);
29 const agentContent = await ide.readFile(agentFilePath);
30
31 const rule = markdownToRule(agentContent, {
32 uriType: "file",
33 fileUri: agentFilePath,
34 });
35 rules.push({
36 ...rule,
37 source: "agent-file",
38 ruleFile: agentFilePath,
39 alwaysApply: true,
40 });
41 agentFileFound = true;
42 break; // Use the first found agent file in this workspace
43 } catch (e) {
44 // File doesn't exist or can't be read, continue to next file
45 }
46 }
47
48 if (agentFileFound) {
49 break; // Use agent file from first workspace that has one
50 }
51 }
52
53 try {
54 // Get all .md files from .continue/rules
55 const markdownFiles = await getAllDotContinueDefinitionFiles(
56 ide,
57 { includeGlobal: true, includeWorkspace: true, fileExtType: "markdown" },
58 "rules",
59 );
60
61 // Filter to just .md files
62 const mdFiles = markdownFiles.filter((file) => file.path.endsWith(".md"));
63
64 // Process each markdown file
65 for (const file of mdFiles) {
66 try {
67 const { relativePathOrBasename } = findUriInDirs(
68 file.path,
69 await ide.getWorkspaceDirs(),
70 );
71 const rule = markdownToRule(file.content, {
72 uriType: "file",
73 fileUri: relativePathOrBasename,
74 });
75 rules.push({ ...rule, source: "rules-block", ruleFile: file.path });
76 } catch (e) {
77 errors.push({
78 fatal: false,
79 message: `Failed to parse markdown rule file ${file.path}: ${e instanceof Error ? e.message : e}`,
80 });
81 }
82 }
83 } catch (e) {
84 errors.push({
85 fatal: false,
86 message: `Error loading markdown rule files: ${e instanceof Error ? e.message : e}`,
87 });
88 }
89
90 return { rules, errors };
91}