feat: create markdown rules in notch

This commit is contained in:
Patrick Erichsen 2025-06-02 14:33:51 -07:00
parent 15b16a185d
commit c2571e2a86
10 changed files with 498 additions and 89 deletions

View file

@ -0,0 +1,157 @@
import {
createMarkdownWithFrontmatter,
createRuleFilePath,
createRuleMarkdown,
sanitizeRuleName,
} from "./createMarkdownRule";
import { parseMarkdownRule } from "./parseMarkdownRule";
describe("sanitizeRuleName", () => {
it("should sanitize rule names for filenames", () => {
expect(sanitizeRuleName("My Test Rule")).toBe("my-test-rule");
expect(sanitizeRuleName("Rule with @#$% chars")).toBe("rule-with-chars");
expect(sanitizeRuleName("Multiple spaces")).toBe("multiple-spaces");
expect(sanitizeRuleName("UPPERCASE-rule")).toBe("uppercase-rule");
expect(sanitizeRuleName("already-sanitized")).toBe("already-sanitized");
});
it("should handle empty and edge case inputs", () => {
expect(sanitizeRuleName("")).toBe("");
expect(sanitizeRuleName(" ")).toBe("");
expect(sanitizeRuleName("123")).toBe("123");
expect(sanitizeRuleName("rule-with-numbers-123")).toBe(
"rule-with-numbers-123",
);
});
});
describe("createRuleFilePath", () => {
it("should create correct rule file path", () => {
const result = createRuleFilePath("/workspace", "My Test Rule");
expect(result).toBe("/workspace/.continue/rules/my-test-rule.md");
});
it("should handle special characters in rule name", () => {
const result = createRuleFilePath("/home/user", "Rule with @#$% chars");
expect(result).toBe("/home/user/.continue/rules/rule-with-chars.md");
});
it("should handle edge case rule names", () => {
const result = createRuleFilePath("/test", " Multiple Spaces ");
expect(result).toBe("/test/.continue/rules/multiple-spaces.md");
});
});
describe("createMarkdownWithFrontmatter", () => {
it("should create properly formatted markdown with frontmatter", () => {
const frontmatter = {
name: "Test Rule",
description: "A test rule",
globs: "*.ts",
};
const markdown = "# Test Rule\n\nThis is a test rule.";
const result = createMarkdownWithFrontmatter(frontmatter, markdown);
// The exact quote style doesn't matter as long as it parses correctly
expect(result).toContain("name: Test Rule");
expect(result).toContain("description: A test rule");
expect(result).toContain("globs:");
expect(result).toContain("*.ts");
expect(result).toContain("---\n\n# Test Rule\n\nThis is a test rule.");
});
it("should handle empty frontmatter", () => {
const frontmatter = {};
const markdown = "# Simple Rule\n\nJust markdown content.";
const result = createMarkdownWithFrontmatter(frontmatter, markdown);
const expected = `---
{}
---
# Simple Rule
Just markdown content.`;
expect(result).toBe(expected);
});
it("should create content that can be parsed back correctly", () => {
const originalFrontmatter = {
name: "Roundtrip Test",
description: "Testing roundtrip parsing",
globs: ["*.js", "*.ts"],
alwaysApply: true,
};
const originalMarkdown =
"# Roundtrip Test\n\nThis should parse back correctly.";
const created = createMarkdownWithFrontmatter(
originalFrontmatter,
originalMarkdown,
);
const parsed = parseMarkdownRule(created);
expect(parsed.frontmatter).toEqual(originalFrontmatter);
expect(parsed.markdown).toBe(originalMarkdown);
});
});
describe("createRuleMarkdown", () => {
it("should create rule markdown with all options", () => {
const result = createRuleMarkdown("Test Rule", "This is the rule content", {
description: "Test description",
globs: ["*.ts", "*.js"],
alwaysApply: true,
});
const parsed = parseMarkdownRule(result);
expect(parsed.frontmatter.description).toBe("Test description");
expect(parsed.frontmatter.globs).toEqual(["*.ts", "*.js"]);
expect(parsed.frontmatter.alwaysApply).toBe(true);
expect(parsed.markdown).toBe("# Test Rule\n\nThis is the rule content");
});
it("should create rule markdown with minimal options", () => {
const result = createRuleMarkdown("Simple Rule", "Simple content");
const parsed = parseMarkdownRule(result);
expect(parsed.frontmatter.description).toBeUndefined();
expect(parsed.frontmatter.globs).toBeUndefined();
expect(parsed.frontmatter.alwaysApply).toBeUndefined();
expect(parsed.markdown).toBe("# Simple Rule\n\nSimple content");
});
it("should handle string globs", () => {
const result = createRuleMarkdown("String Glob Rule", "Content", {
globs: "*.py",
});
const parsed = parseMarkdownRule(result);
expect(parsed.frontmatter.globs).toBe("*.py");
});
it("should trim description and globs", () => {
const result = createRuleMarkdown("Trim Test", "Content", {
description: " spaced description ",
globs: " *.ts ",
});
const parsed = parseMarkdownRule(result);
expect(parsed.frontmatter.description).toBe("spaced description");
expect(parsed.frontmatter.globs).toBe("*.ts");
});
it("should handle alwaysApply false explicitly", () => {
const result = createRuleMarkdown("Always Apply False", "Content", {
alwaysApply: false,
});
const parsed = parseMarkdownRule(result);
expect(parsed.frontmatter.alwaysApply).toBe(false);
});
});

View file

@ -0,0 +1,75 @@
import * as YAML from "yaml";
import { joinPathsToUri } from "../../util/uri";
import { RuleFrontmatter } from "./parseMarkdownRule";
export const RULE_FILE_EXTENSION = "md";
/**
* Sanitizes a rule name for use in filenames (removes special chars, replaces spaces with dashes)
*/
export function sanitizeRuleName(name: string): string {
return name
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, "")
.replace(/\s+/g, "-")
.replace(/^-+|-+$/g, ""); // Remove leading/trailing dashes
}
/**
* Creates the file path for a rule in the workspace .continue/rules directory
*/
export function createRuleFilePath(
workspaceDir: string,
ruleName: string,
): string {
const safeRuleName = sanitizeRuleName(ruleName);
return joinPathsToUri(
workspaceDir,
".continue",
"rules",
`${safeRuleName}.${RULE_FILE_EXTENSION}`,
);
}
/**
* Creates markdown content with YAML frontmatter in the format expected by parseMarkdownRule
*/
export function createMarkdownWithFrontmatter(
frontmatter: RuleFrontmatter,
markdown: string,
): string {
const frontmatterStr = YAML.stringify(frontmatter).trim();
return `---\n${frontmatterStr}\n---\n\n${markdown}`;
}
/**
* Creates a rule markdown file content from rule components
*/
export function createRuleMarkdown(
name: string,
ruleContent: string,
options: {
description?: string;
globs?: string | string[];
alwaysApply?: boolean;
} = {},
): string {
const frontmatter: RuleFrontmatter = {};
if (options.globs) {
frontmatter.globs =
typeof options.globs === "string" ? options.globs.trim() : options.globs;
}
if (options.description) {
frontmatter.description = options.description.trim();
}
if (options.alwaysApply !== undefined) {
frontmatter.alwaysApply = options.alwaysApply;
}
const markdownBody = `# ${name}\n\n${ruleContent}`;
return createMarkdownWithFrontmatter(frontmatter, markdownBody);
}

View file

@ -0,0 +1,5 @@
export * from "./parseMarkdownRule";
export * from "./createMarkdownRule";
export * from "./loadMarkdownRules";

View file

@ -0,0 +1,141 @@
import { BlockType } from "@continuedev/config-yaml";
import { describe, expect, test } from "@jest/globals";
import { RULE_FILE_EXTENSION } from "../markdown";
import { blockTypeToSingular, findAvailableFilename } from "./workspaceBlocks";
describe("blockTypeToSingular", () => {
test("converts block types to singular form correctly", () => {
expect(blockTypeToSingular("models")).toBe("model");
expect(blockTypeToSingular("context")).toBe("context");
expect(blockTypeToSingular("rules")).toBe("rule");
expect(blockTypeToSingular("docs")).toBe("doc");
expect(blockTypeToSingular("prompts")).toBe("prompt");
expect(blockTypeToSingular("mcpServers")).toBe("mcp-server");
});
});
describe("findAvailableFilename", () => {
test("returns base filename when it doesn't exist", async () => {
const mockFileExists = async (uri: string) => false;
const result = await findAvailableFilename(
"/workspace/.continue/models",
"models",
mockFileExists,
);
expect(result).toBe("/workspace/.continue/models/new-model.yaml");
});
test("returns filename with counter when base exists", async () => {
const mockFileExists = async (uri: string) => {
return uri === "/workspace/.continue/models/new-model.yaml";
};
const result = await findAvailableFilename(
"/workspace/.continue/models",
"models",
mockFileExists,
);
expect(result).toBe("/workspace/.continue/models/new-model-1.yaml");
});
test("increments counter until available filename is found", async () => {
const existingFiles = new Set([
"/workspace/.continue/context/new-context.yaml",
"/workspace/.continue/context/new-context-1.yaml",
"/workspace/.continue/context/new-context-2.yaml",
]);
const mockFileExists = async (uri: string) => {
return existingFiles.has(uri);
};
const result = await findAvailableFilename(
"/workspace/.continue/context",
"context",
mockFileExists,
);
expect(result).toBe("/workspace/.continue/context/new-context-3.yaml");
});
test("handles different block types correctly with proper extensions", async () => {
const mockFileExists = async (uri: string) => false;
const testCases: Array<{ blockType: BlockType; expected: string }> = [
{ blockType: "models", expected: "/test/new-model.yaml" },
{ blockType: "context", expected: "/test/new-context.yaml" },
{ blockType: "rules", expected: `/test/new-rule.${RULE_FILE_EXTENSION}` },
{ blockType: "docs", expected: "/test/new-doc.yaml" },
{ blockType: "prompts", expected: "/test/new-prompt.yaml" },
{ blockType: "mcpServers", expected: "/test/new-mcp-server.yaml" },
];
for (const { blockType, expected } of testCases) {
const result = await findAvailableFilename(
"/test",
blockType,
mockFileExists,
);
expect(result).toBe(expected);
}
});
test("respects custom extension parameter", async () => {
const mockFileExists = async (uri: string) => false;
const result = await findAvailableFilename(
"/test",
"models",
mockFileExists,
"json",
);
expect(result).toBe("/test/new-model.json");
});
test("handles rules markdown files with counter", async () => {
const existingFiles = new Set([
`/workspace/.continue/rules/new-rule.${RULE_FILE_EXTENSION}`,
`/workspace/.continue/rules/new-rule-1.${RULE_FILE_EXTENSION}`,
]);
const mockFileExists = async (uri: string) => {
return existingFiles.has(uri);
};
const result = await findAvailableFilename(
"/workspace/.continue/rules",
"rules",
mockFileExists,
);
expect(result).toBe(
`/workspace/.continue/rules/new-rule-2.${RULE_FILE_EXTENSION}`,
);
});
test("handles large counter values", async () => {
const existingFiles = new Set(
Array.from({ length: 100 }, (_, i) =>
i === 0
? "/workspace/.continue/prompts/new-prompt.yaml"
: `/workspace/.continue/prompts/new-prompt-${i}.yaml`,
),
);
const mockFileExists = async (uri: string) => {
return existingFiles.has(uri);
};
const result = await findAvailableFilename(
"/workspace/.continue/prompts",
"prompts",
mockFileExists,
);
expect(result).toBe("/workspace/.continue/prompts/new-prompt-100.yaml");
});
});

View file

@ -2,10 +2,35 @@ import { BlockType, ConfigYaml } from "@continuedev/config-yaml";
import * as YAML from "yaml";
import { IDE } from "../..";
import { joinPathsToUri } from "../../util/uri";
import {
RULE_FILE_EXTENSION,
createMarkdownWithFrontmatter,
type RuleFrontmatter,
} from "../markdown";
export function blockTypeToSingular(blockType: BlockType): string {
switch (blockType) {
case "context":
return "context";
case "models":
return "model";
case "rules":
return "rule";
case "docs":
return "doc";
case "prompts":
return "prompt";
case "mcpServers":
return "mcp-server";
default:
// Fallback to slice approach for any new block types
return blockType.slice(0, -1);
}
}
function getContentsForNewBlock(blockType: BlockType): ConfigYaml {
const configYaml: ConfigYaml = {
name: `New ${blockType.slice(0, -1)}`,
name: `New ${blockTypeToSingular(blockType)}`,
version: "0.0.1",
schema: "v1",
};
@ -64,6 +89,51 @@ function getContentsForNewBlock(blockType: BlockType): ConfigYaml {
return configYaml;
}
function getFileExtension(blockType: BlockType): string {
return blockType === "rules" ? RULE_FILE_EXTENSION : "yaml";
}
/**
* Creates a new rule markdown file content with default values
* Only used by workspace block creation
*/
function createNewRuleMarkdown(
name: string = "New rule",
description: string = "Rule description",
): string {
const frontmatter: RuleFrontmatter = {
name,
description,
};
const markdownBody = `# ${name}\n\nWrite your rule content here.`;
return createMarkdownWithFrontmatter(frontmatter, markdownBody);
}
export async function findAvailableFilename(
baseDirUri: string,
blockType: BlockType,
fileExists: (uri: string) => Promise<boolean>,
extension?: string,
): Promise<string> {
const baseFilename = `new-${blockTypeToSingular(blockType)}`;
const fileExtension = extension ?? getFileExtension(blockType);
let counter = 0;
let fileUri: string;
do {
const suffix = counter === 0 ? "" : `-${counter}`;
fileUri = joinPathsToUri(
baseDirUri,
`${baseFilename}${suffix}.${fileExtension}`,
);
counter++;
} while (await fileExists(fileUri));
return fileUri;
}
export async function createNewWorkspaceBlockFile(
ide: IDE,
blockType: BlockType,
@ -77,21 +147,19 @@ export async function createNewWorkspaceBlockFile(
const baseDirUri = joinPathsToUri(workspaceDirs[0], `.continue/${blockType}`);
// Find the first available filename
let counter = 0;
let fileUri: string;
do {
const suffix = counter === 0 ? "" : `-${counter}`;
fileUri = joinPathsToUri(
baseDirUri,
`new-${blockType.slice(0, -1)}${suffix}.yaml`,
);
counter++;
} while (await ide.fileExists(fileUri));
await ide.writeFile(
fileUri,
YAML.stringify(getContentsForNewBlock(blockType)),
const fileUri = await findAvailableFilename(
baseDirUri,
blockType,
ide.fileExists.bind(ide),
);
let fileContent: string;
if (blockType === "rules") {
fileContent = createNewRuleMarkdown();
} else {
fileContent = YAML.stringify(getContentsForNewBlock(blockType));
}
await ide.writeFile(fileUri, fileContent);
await ide.openFile(fileUri);
}

View file

@ -37,11 +37,6 @@ export const createRuleBlock: Tool = {
description:
"Optional file patterns to which this rule applies (e.g. ['**/*.{ts,tsx}'] or ['src/**/*.ts', 'tests/**/*.ts'])",
},
alwaysApply: {
type: "boolean",
description:
"Whether this rule should always be applied regardless of file pattern matching",
},
},
},
},

View file

@ -1,4 +1,4 @@
import { parseMarkdownRule } from "../../config/markdown/parseMarkdownRule";
import { parseMarkdownRule } from "../../config/markdown";
import { createRuleBlockImpl } from "./createRuleBlock";
// Mock the extras parameter with necessary functions
@ -21,6 +21,8 @@ describe("createRuleBlockImpl", () => {
const args = {
name: "TypeScript Rule",
rule: "Use interfaces for object shapes",
description: "Always use interfaces",
alwaysApply: true,
globs: "**/*.{ts,tsx}",
};
@ -31,23 +33,27 @@ describe("createRuleBlockImpl", () => {
const { frontmatter, markdown } = parseMarkdownRule(fileContent);
expect(frontmatter).toEqual({
description: "Always use interfaces",
globs: "**/*.{ts,tsx}",
alwaysApply: true,
});
expect(markdown).toContain("# TypeScript Rule");
expect(markdown).toContain("Use interfaces for object shapes");
});
it("should create a filename based on sanitized rule name", async () => {
it("should create a filename based on sanitized rule name using shared path function", async () => {
const args = {
name: "Special Ch@racters & Spaces",
rule: "Handle special characters",
description: "Test rule",
alwaysApply: false,
};
await createRuleBlockImpl(args, mockExtras as any);
const fileUri = mockIde.writeFile.mock.calls[0][0];
expect(fileUri).toContain("special-chracters-spaces.md");
expect(fileUri).toBe("/.continue/rules/special-chracters-spaces.md");
});
it("should create a rule with description pattern", async () => {
@ -55,6 +61,7 @@ describe("createRuleBlockImpl", () => {
name: "Description Test",
rule: "This is the rule content",
description: "This is a detailed explanation of the rule",
alwaysApply: true,
};
await createRuleBlockImpl(args, mockExtras as any);
@ -65,6 +72,7 @@ describe("createRuleBlockImpl", () => {
expect(frontmatter).toEqual({
description: "This is a detailed explanation of the rule",
alwaysApply: true,
});
expect(markdown).toContain("# Description Test");
@ -76,6 +84,7 @@ describe("createRuleBlockImpl", () => {
name: "Complete Rule",
rule: "Follow this standard",
description: "This rule enforces our team standards",
alwaysApply: false,
globs: "**/*.js",
};
@ -88,6 +97,7 @@ describe("createRuleBlockImpl", () => {
expect(frontmatter).toEqual({
description: "This rule enforces our team standards",
globs: "**/*.js",
alwaysApply: false,
});
expect(markdown).toContain("# Complete Rule");
@ -98,6 +108,7 @@ describe("createRuleBlockImpl", () => {
const args = {
name: "Conditional Rule",
rule: "This rule should not always be applied",
description: "Optional rule",
alwaysApply: false,
};
@ -108,6 +119,7 @@ describe("createRuleBlockImpl", () => {
const { frontmatter } = parseMarkdownRule(fileContent);
expect(frontmatter).toEqual({
description: "Optional rule",
alwaysApply: false,
});
});

View file

@ -1,8 +1,6 @@
import * as YAML from "yaml";
import { ToolImpl } from ".";
import { RuleWithSource } from "../..";
import { RuleFrontmatter } from "../../config/markdown/parseMarkdownRule";
import { joinPathsToUri } from "../../util/uri";
import { createRuleFilePath, createRuleMarkdown } from "../../config/markdown";
export type CreateRuleBlockArgs = Pick<
Required<RuleWithSource>,
@ -14,48 +12,17 @@ export const createRuleBlockImpl: ToolImpl = async (
args: CreateRuleBlockArgs,
extras,
) => {
// Sanitize rule name for use in filename (remove special chars, replace spaces with dashes)
const safeRuleName = args.name
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, "")
.replace(/\s+/g, "-");
// Create markdown content using shared utility
const fileContent = createRuleMarkdown(args.name, args.rule, {
description: args.description,
globs: args.globs,
});
const fileExtension = "md";
const frontmatter: RuleFrontmatter = {};
if (args.globs) {
frontmatter.globs =
typeof args.globs === "string" ? args.globs.trim() : args.globs;
}
if (args.description) {
frontmatter.description = args.description.trim();
}
if (args.alwaysApply !== undefined) {
frontmatter.alwaysApply = args.alwaysApply;
}
const frontmatterYaml = YAML.stringify(frontmatter).trim();
let fileContent = `---
${frontmatterYaml}
---
# ${args.name}
${args.rule}
`;
const [localContinueDir] = await extras.ide.getWorkspaceDirs();
const rulesDirUri = joinPathsToUri(
localContinueDir,
".continue",
"rules",
`${safeRuleName}.${fileExtension}`,
);
const ruleFilePath = createRuleFilePath(localContinueDir, args.name);
await extras.ide.writeFile(rulesDirUri, fileContent);
await extras.ide.openFile(rulesDirUri);
await extras.ide.writeFile(ruleFilePath, fileContent);
await extras.ide.openFile(ruleFilePath);
return [
{
@ -63,7 +30,7 @@ ${args.rule}
description: args.description || "",
uri: {
type: "file",
value: rulesDirUri,
value: ruleFilePath,
},
content: `Rule created successfully`,
},

View file

@ -7,13 +7,11 @@ import { useContext } from "react";
import { GhostButton } from "../../..";
import { useAuth } from "../../../../context/Auth";
import { IdeMessengerContext } from "../../../../context/IdeMessenger";
import { useAppDispatch } from "../../../../redux/hooks";
import { fontSize } from "../../../../util";
export function ExploreBlocksButton(props: { blockType: string }) {
const { selectedProfile } = useAuth();
const ideMessenger = useContext(IdeMessengerContext);
const dispatch = useAppDispatch();
const isLocal = selectedProfile?.profileType === "local";
@ -26,21 +24,11 @@ export function ExploreBlocksButton(props: { blockType: string }) {
const handleClick = () => {
if (isLocal) {
ideMessenger.request("config/addLocalWorkspaceBlock", {
void ideMessenger.request("config/addLocalWorkspaceBlock", {
blockType: props.blockType as BlockType,
});
// switch (props.blockType) {
// case "docs":
// dispatch(setShowDialog(true));
// dispatch(setDialogMessage(<AddDocsDialog />));
// break;
// default:
// ideMessenger.request("config/openProfile", {
// profileId: selectedProfile.id,
// });
// }
} else {
ideMessenger.request("controlPlane/openUrl", {
void ideMessenger.request("controlPlane/openUrl", {
path: `new?type=block&blockType=${props.blockType}`,
orgSlug: undefined,
});

View file

@ -6,10 +6,10 @@ import {
} from "@heroicons/react/24/outline";
import { RuleWithSource } from "core";
import {
DEFAULT_AGENT_SYSTEM_MESSAGE,
DEFAULT_AGENT_SYSTEM_MESSAGE_URL,
DEFAULT_CHAT_SYSTEM_MESSAGE,
DEFAULT_CHAT_SYSTEM_MESSAGE_URL,
DEFAULT_AGENT_SYSTEM_MESSAGE,
DEFAULT_AGENT_SYSTEM_MESSAGE_URL
} from "core/llm/constructMessages";
import { useContext, useMemo } from "react";
import { defaultBorderRadius, vscCommandCenterActiveBorder } from "../../..";
@ -35,7 +35,7 @@ const RuleCard: React.FC<RuleCardProps> = ({ rule }) => {
const handleOpen = async () => {
if (rule.slug) {
ideMessenger.request("controlPlane/openUrl", {
void ideMessenger.request("controlPlane/openUrl", {
path: `${rule.slug}/new-version`,
orgSlug: undefined,
});
@ -112,7 +112,8 @@ const RuleCard: React.FC<RuleCardProps> = ({ rule }) => {
<HeaderButtonWithToolTip onClick={onClickExpand} text="Expand">
<ArrowsPointingOutIcon className="h-3 w-3 text-gray-400" />
</HeaderButtonWithToolTip>{" "}
{rule.source === "default-chat" || rule.source === "default-agent" ? (
{rule.source === "default-chat" ||
rule.source === "default-agent" ? (
<HeaderButtonWithToolTip onClick={handleOpen} text="View">
<EyeIcon className="h-3 w-3 text-gray-400" />
</HeaderButtonWithToolTip>
@ -195,24 +196,24 @@ export function RulesSection() {
rules.unshift({
rule: config.selectedModelByRole.chat?.baseAgentSystemMessage,
source: "model-agent-options",
})
});
} else {
rules.unshift({
rule: DEFAULT_AGENT_SYSTEM_MESSAGE,
source: "default-agent",
})
});
}
} else {
if (config.selectedModelByRole.chat?.baseChatSystemMessage) {
rules.unshift({
rule: config.selectedModelByRole.chat?.baseChatSystemMessage,
source: "model-chat-options",
})
});
} else {
rules.unshift({
rule: DEFAULT_CHAT_SYSTEM_MESSAGE,
source: "default-chat",
})
});
}
}