Elffuss BraidHub

kikocis/continue public

1f089284a191 · 2.0 KiBRawPermalink
1import { ConfigYaml } from "@continuedev/config-yaml";
2import * as YAML from "yaml";
3import { ToolImpl } from ".";
4import { joinPathsToUri } from "../../util/uri";
5
6export interface CreateRuleBlockArgs {
7 name: string;
8 rule: string;
9 globs?: string;
10 format?: "yaml" | "markdown"; // Add format option with yaml as default
11}
12
13export const createRuleBlockImpl: ToolImpl = async (
14 args: CreateRuleBlockArgs,
15 extras,
16) => {
17 // Sanitize rule name for use in filename (remove special chars, replace spaces with dashes)
18 const safeRuleName = args.name
19 .toLowerCase()
20 .replace(/[^a-z0-9\s-]/g, "")
21 .replace(/\s+/g, "-");
22
23 // Default to YAML format
24 const format = args.format ?? "yaml";
25 const fileExtension = format === "markdown" ? "md" : "yaml";
26
27 const ruleObject = {
28 name: args.name,
29 rule: args.rule,
30 ...(args.globs ? { globs: args.globs.trim() } : {}),
31 };
32
33 let fileContent: string;
34
35 if (format === "markdown") {
36 // Generate markdown format with frontmatter
37 const frontmatter = {
38 ...(args.globs ? { globs: args.globs.trim() } : {}),
39 };
40
41 const frontmatterYaml = YAML.stringify(frontmatter).trim();
42 fileContent = `---
43${frontmatterYaml}
44---
45
46# ${args.name}
47
48${args.rule}
49`;
50 } else {
51 // Generate YAML format
52 const ruleBlock: ConfigYaml = {
53 name: args.name,
54 version: "0.0.1",
55 schema: "v1",
56 rules: [ruleObject],
57 };
58
59 fileContent = YAML.stringify(ruleBlock);
60 }
61
62 const [localContinueDir] = await extras.ide.getWorkspaceDirs();
63 const rulesDirUri = joinPathsToUri(
64 localContinueDir,
65 ".continue",
66 "rules",
67 `${safeRuleName}.${fileExtension}`,
68 );
69
70 await extras.ide.writeFile(rulesDirUri, fileContent);
71 await extras.ide.openFile(rulesDirUri);
72
73 return [
74 {
75 name: "New Rule Block",
76 description: "", // No description throws an error in the GUI
77 uri: {
78 type: "file",
79 value: rulesDirUri,
80 },
81 content: `Rule created successfully in ${format} format`,
82 },
83 ];
84};