| 1 | import * as YAML from "yaml"; |
| 2 | import { ToolImpl } from "."; |
| 3 | import { joinPathsToUri } from "../../util/uri"; |
| 4 | |
| 5 | export interface CreateRuleBlockArgs { |
| 6 | name: string; |
| 7 | rule: string; |
| 8 | description: string; |
| 9 | globs?: string; |
| 10 | } |
| 11 | |
| 12 | export const createRuleBlockImpl: ToolImpl = async ( |
| 13 | args: CreateRuleBlockArgs, |
| 14 | extras, |
| 15 | ) => { |
| 16 | // Sanitize rule name for use in filename (remove special chars, replace spaces with dashes) |
| 17 | const safeRuleName = args.name |
| 18 | .toLowerCase() |
| 19 | .replace(/[^a-z0-9\s-]/g, "") |
| 20 | .replace(/\s+/g, "-"); |
| 21 | |
| 22 | const fileExtension = "md"; |
| 23 | |
| 24 | const frontmatter: Record<string, string> = {}; |
| 25 | |
| 26 | if (args.globs) { |
| 27 | frontmatter.globs = args.globs.trim(); |
| 28 | } |
| 29 | |
| 30 | if (args.description) { |
| 31 | frontmatter.description = args.description.trim(); |
| 32 | } |
| 33 | const frontmatterYaml = YAML.stringify(frontmatter).trim(); |
| 34 | let fileContent = `--- |
| 35 | ${frontmatterYaml} |
| 36 | --- |
| 37 | |
| 38 | # ${args.name} |
| 39 | |
| 40 | ${args.rule} |
| 41 | `; |
| 42 | const [localContinueDir] = await extras.ide.getWorkspaceDirs(); |
| 43 | const rulesDirUri = joinPathsToUri( |
| 44 | localContinueDir, |
| 45 | ".continue", |
| 46 | "rules", |
| 47 | `${safeRuleName}.${fileExtension}`, |
| 48 | ); |
| 49 | |
| 50 | await extras.ide.writeFile(rulesDirUri, fileContent); |
| 51 | await extras.ide.openFile(rulesDirUri); |
| 52 | |
| 53 | return [ |
| 54 | { |
| 55 | name: "New Rule Block", |
| 56 | description: args.description || "", |
| 57 | uri: { |
| 58 | type: "file", |
| 59 | value: rulesDirUri, |
| 60 | }, |
| 61 | content: `Rule created successfully`, |
| 62 | }, |
| 63 | ]; |
| 64 | }; |