Elffuss BraidHub

kikocis/continue public

d7c7c28e99d9 · 1.9 KiBRawPermalink
1import * as YAML from "yaml";
2import { ToolImpl } from ".";
3import { RuleWithSource } from "../..";
4import { RuleFrontmatter } from "../../config/markdown/parseMarkdownRule";
5import { joinPathsToUri, pathToUriPathSegment } from "../../util/uri";
6
7export type CreateRuleBlockArgs = Pick<
8 Required<RuleWithSource>,
9 "rule" | "description" | "alwaysApply" | "name"
10> &
11 Pick<RuleWithSource, "globs">;
12
13export const createRuleBlockImpl: ToolImpl = async (
14 args: CreateRuleBlockArgs,
15 extras,
16) => {
17 // Use the pathToUriPathSegment utility for sanitizing the name
18 const safeRuleName = pathToUriPathSegment(args.name)
19 .toLowerCase()
20 .replace(/[^a-z0-9-]/g, "") // Further sanitize to remove any remaining special chars
21 .replace(/-+/g, "-") // Replace multiple dashes with a single dash
22 .replace(/^-|-$/g, ""); // Remove leading/trailing dashes
23
24 const fileExtension = "md";
25
26 const frontmatter: RuleFrontmatter = {};
27
28 if (args.globs) {
29 frontmatter.globs =
30 typeof args.globs === "string" ? args.globs.trim() : args.globs;
31 }
32
33 if (args.description) {
34 frontmatter.description = args.description.trim();
35 }
36
37 if (args.alwaysApply !== undefined) {
38 frontmatter.alwaysApply = args.alwaysApply;
39 }
40
41 const frontmatterYaml = YAML.stringify(frontmatter).trim();
42 let fileContent = `---
43${frontmatterYaml}
44---
45
46# ${args.name}
47
48${args.rule}
49`;
50 const [localContinueDir] = await extras.ide.getWorkspaceDirs();
51 const rulesDirUri = joinPathsToUri(
52 localContinueDir,
53 ".continue",
54 "rules",
55 `${safeRuleName}.${fileExtension}`,
56 );
57
58 await extras.ide.writeFile(rulesDirUri, fileContent);
59 await extras.ide.openFile(rulesDirUri);
60
61 return [
62 {
63 name: "New Rule Block",
64 description: args.description || "",
65 uri: {
66 type: "file",
67 value: rulesDirUri,
68 },
69 content: `Rule created successfully`,
70 },
71 ];
72};