From fd94ef2b31e998c4e94235b586b2a900863044d0 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Wed, 2 Jul 2025 15:25:48 -0700 Subject: [PATCH] feat: move rule type logic into `config-yaml` --- core/tools/definitions/createRuleBlock.ts | 9 +- core/tools/implementations/createRuleBlock.ts | 23 +-- .../GenerateRuleDialog/GenerationScreen.tsx | 91 +++++------ .../GenerateRuleDialog/InputScreen.tsx | 2 +- .../GenerateRuleDialog/ruleTemplates.ts | 33 +++- .../src/markdown/getRuleType.test.ts | 147 ++++++++++++++++++ .../config-yaml/src/markdown/getRuleType.ts | 55 +++++++ packages/config-yaml/src/markdown/index.ts | 1 + 8 files changed, 285 insertions(+), 76 deletions(-) create mode 100644 packages/config-yaml/src/markdown/getRuleType.test.ts create mode 100644 packages/config-yaml/src/markdown/getRuleType.ts diff --git a/core/tools/definitions/createRuleBlock.ts b/core/tools/definitions/createRuleBlock.ts index 17c4d67df..92aecb158 100644 --- a/core/tools/definitions/createRuleBlock.ts +++ b/core/tools/definitions/createRuleBlock.ts @@ -13,7 +13,7 @@ export const createRuleBlock: Tool = { function: { name: BuiltInToolNames.CreateRuleBlock, description: - 'Creates a "rule" that can be referenced in future conversations. This should be used whenever you want to establish code standards / preferences that should be applied consistently, or when you want to avoid making a mistake again. To modify existing rules, use the edit tool instead.', + 'Creates a "rule" that can be referenced in future conversations. This should be used whenever you want to establish code standards / preferences that should be applied consistently, or when you want to avoid making a mistake again. To modify existing rules, use the edit tool instead.\n\nRule Types:\n- Always: Include only "rule" (always included in model context)\n- Auto Attached: Include "rule", "globs", and/or "regex" (included when files match patterns)\n- Agent Requested: Include "rule" and "description" (AI decides when to apply based on description)\n- Manual: Include only "rule" (only included when explicitly mentioned using @ruleName)', parameters: { type: "object", required: ["name", "rule", "description"], @@ -31,7 +31,7 @@ export const createRuleBlock: Tool = { description: { type: "string", description: - "Optional, only include a description if the rule should not be always applied.", + "Description of when this rule should be applied. Required for Agent Requested rules (AI decides when to apply). Optional for other types.", }, globs: { type: "string", @@ -43,6 +43,11 @@ export const createRuleBlock: Tool = { description: "Optional regex patterns to match against file content. Rule applies only to files whose content matches the pattern (e.g. 'useEffect' for React hooks or '\\bclass\\b' for class definitions)", }, + alwaysApply: { + type: "boolean", + description: + "Whether this rule should always be applied. Set to false for Agent Requested and Manual rules. Omit or set to true for Always and Auto Attached rules.", + }, }, }, }, diff --git a/core/tools/implementations/createRuleBlock.ts b/core/tools/implementations/createRuleBlock.ts index 06921f770..e433c233d 100644 --- a/core/tools/implementations/createRuleBlock.ts +++ b/core/tools/implementations/createRuleBlock.ts @@ -5,29 +5,18 @@ import { createRuleFilePath } from "../../config/markdown/utils"; export type CreateRuleBlockArgs = Pick< Required, - "rule" | "description" | "alwaysApply" | "name" + "rule" | "name" > & - Pick; + Pick; export const createRuleBlockImpl: ToolImpl = async ( - args: CreateRuleBlockArgs, + { name, rule, ...otherArgs }: CreateRuleBlockArgs, extras, ) => { - // Create options object with the fields that createRuleMarkdown expects - const options: any = { - description: args.description, - globs: args.globs, - }; - - // Add regex if provided - if (args.regex) { - options.regex = args.regex; - } - - const fileContent = createRuleMarkdown(args.name, args.rule, options); + const fileContent = createRuleMarkdown(name, rule, otherArgs); const [localContinueDir] = await extras.ide.getWorkspaceDirs(); - const ruleFilePath = createRuleFilePath(localContinueDir, args.name); + const ruleFilePath = createRuleFilePath(localContinueDir, name); await extras.ide.writeFile(ruleFilePath, fileContent); await extras.ide.openFile(ruleFilePath); @@ -35,7 +24,7 @@ export const createRuleBlockImpl: ToolImpl = async ( return [ { name: "New Rule Block", - description: args.description || "", + description: otherArgs.description || "", uri: { type: "file", value: ruleFilePath, diff --git a/gui/src/components/GenerateRuleDialog/GenerationScreen.tsx b/gui/src/components/GenerateRuleDialog/GenerationScreen.tsx index d46308f2a..b697313d7 100644 --- a/gui/src/components/GenerateRuleDialog/GenerationScreen.tsx +++ b/gui/src/components/GenerateRuleDialog/GenerationScreen.tsx @@ -1,4 +1,9 @@ -import { createRuleMarkdown } from "@continuedev/config-yaml"; +import { + createRuleMarkdown, + getRuleType, + RuleType, + RuleTypeDescriptions, +} from "@continuedev/config-yaml"; import { InformationCircleIcon } from "@heroicons/react/24/outline"; import { createRuleFilePath } from "core/config/markdown/utils"; import { CreateRuleBlockArgs } from "core/tools/implementations/createRuleBlock"; @@ -10,44 +15,12 @@ import { ToolTip } from "../gui/Tooltip"; import { Button } from "../ui"; import { useRuleGeneration } from "./useRuleGeneration"; -export enum RuleType { - Always = "Always", - AutoAttached = "Auto Attached", - AgentRequested = "Agent Requested", - Manual = "Manual", -} - interface GenerationScreenProps { inputPrompt: string; onBack: () => void; onSuccess: () => void; } -function determineRuleTypeFromData(data: CreateRuleBlockArgs): RuleType { - if (data.globs) { - return RuleType.AutoAttached; - } - if (data.description && !data.globs) { - return RuleType.AgentRequested; - } - return RuleType.Always; -} - -function getRuleTypeTooltip(ruleType: RuleType): string { - switch (ruleType) { - case RuleType.Always: - return "*Always: Included with every request"; - case RuleType.AutoAttached: - return "Auto Attached: Included when files matching a glob pattern are added as context"; - case RuleType.AgentRequested: - return "Agent Requested: When in Agent mode, the description of the rule is made available to the agent to decide whether or not it needs to read the full content of the rule"; - case RuleType.Manual: - return "Manual: Included only when manually referenced as @ruleName using the Rule context provider"; - default: - return ""; - } -} - export function GenerationScreen({ inputPrompt, onBack, @@ -87,7 +60,7 @@ export function GenerationScreen({ useEffect(() => { if (createRuleBlockArgs && !isGenerating && !formData.rule) { reset(createRuleBlockArgs); - handleRuleTypeChange(determineRuleTypeFromData(createRuleBlockArgs)); + handleRuleTypeChange(getRuleType(createRuleBlockArgs)); } }, [createRuleBlockArgs, isGenerating, formData.rule, reset]); @@ -208,25 +181,43 @@ export function GenerationScreen({ - +
+ + {isGenerating && ( +
+ +
+ )} +
{/* Description (for Agent Requested only) */} @@ -275,7 +266,7 @@ export function GenerationScreen({ {...register("rule")} /> {showSpinner && ( -
+
)} diff --git a/gui/src/components/GenerateRuleDialog/InputScreen.tsx b/gui/src/components/GenerateRuleDialog/InputScreen.tsx index 29629e685..db43e78ae 100644 --- a/gui/src/components/GenerateRuleDialog/InputScreen.tsx +++ b/gui/src/components/GenerateRuleDialog/InputScreen.tsx @@ -51,7 +51,7 @@ export function InputScreen({ />
-
+
{ruleTemplates.map((template, index) => ( ; @@ -8,8 +13,24 @@ export interface RuleTemplate { export const ruleTemplates: RuleTemplate[] = [ { - icon: DocumentTextIcon, - title: "Summarize learnings", - template: "Lorem ipsum dolor est" - } -]; \ No newline at end of file + icon: GlobeAltIcon, + title: "Always Applied", + template: "Create an always applied rule where for all files, ", + }, + { + icon: CodeBracketIcon, + title: "Auto attached", + template: + "Create an auto-attached rule where for all files, do ", + }, + { + icon: SparklesIcon, + title: "Agent Requested", + template: "Create an agent requested rule where ", + }, + { + icon: AtSymbolIcon, + title: "Manual", + template: "Create a manually requested rule where ", + }, +]; diff --git a/packages/config-yaml/src/markdown/getRuleType.test.ts b/packages/config-yaml/src/markdown/getRuleType.test.ts new file mode 100644 index 000000000..85bdf6a7f --- /dev/null +++ b/packages/config-yaml/src/markdown/getRuleType.test.ts @@ -0,0 +1,147 @@ +import { getRuleType, RuleType } from "./getRuleType.js"; + +describe("getRuleType", () => { + describe("Always type", () => { + it("should return Always for rule with no special properties", () => { + const rule = { rule: "Some rule content" }; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + + it("should return Always when alwaysApply is true", () => { + const rule = { rule: "Some rule content", alwaysApply: true }; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + + it("should return Always when alwaysApply is undefined", () => { + const rule = { rule: "Some rule content", alwaysApply: undefined }; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + }); + + describe("Auto Attached type", () => { + it("should return AutoAttached when globs is provided", () => { + const rule = { rule: "Some rule content", globs: ["*.ts"] }; + expect(getRuleType(rule)).toBe(RuleType.AutoAttached); + }); + + it("should return AutoAttached when regex is provided", () => { + const rule = { rule: "Some rule content", regex: "useEffect" }; + expect(getRuleType(rule)).toBe(RuleType.AutoAttached); + }); + + it("should return AutoAttached when both globs and regex are provided", () => { + const rule = { + rule: "Some rule content", + globs: ["*.tsx"], + regex: "useState", + }; + expect(getRuleType(rule)).toBe(RuleType.AutoAttached); + }); + + it("should prioritize AutoAttached over other types when globs/regex present", () => { + const rule = { + rule: "Some rule content", + globs: ["*.ts"], + description: "Some description", + alwaysApply: false, + }; + expect(getRuleType(rule)).toBe(RuleType.AutoAttached); + }); + + it("should handle string globs", () => { + const rule = { rule: "Some rule content", globs: "*.js" }; + expect(getRuleType(rule)).toBe(RuleType.AutoAttached); + }); + + it("should handle array regex", () => { + const rule = { + rule: "Some rule content", + regex: ["pattern1", "pattern2"], + }; + expect(getRuleType(rule)).toBe(RuleType.AutoAttached); + }); + }); + + describe("Agent Requested type", () => { + it("should return AgentRequested when description provided and alwaysApply is false", () => { + const rule = { + rule: "Some rule content", + description: "Helpful for testing", + alwaysApply: false, + }; + expect(getRuleType(rule)).toBe(RuleType.AgentRequested); + }); + + it("should not return AgentRequested when description provided but alwaysApply is true", () => { + const rule = { + rule: "Some rule content", + description: "Helpful for testing", + alwaysApply: true, + }; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + + it("should not return AgentRequested when description provided but alwaysApply is undefined", () => { + const rule = { + rule: "Some rule content", + description: "Helpful for testing", + }; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + }); + + describe("Manual type", () => { + it("should return Manual when alwaysApply is false and no description", () => { + const rule = { + rule: "Some rule content", + alwaysApply: false, + }; + expect(getRuleType(rule)).toBe(RuleType.Manual); + }); + + it("should not return Manual when description is provided", () => { + const rule = { + rule: "Some rule content", + alwaysApply: false, + description: "Some description", + }; + expect(getRuleType(rule)).toBe(RuleType.AgentRequested); + }); + }); + + describe("Edge cases", () => { + it("should handle empty rule object", () => { + const rule = {}; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + + it("should handle rule with empty string globs", () => { + const rule = { rule: "Some rule content", globs: "" }; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + + it("should handle rule with empty array globs", () => { + const rule = { rule: "Some rule content", globs: [] }; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + + it("should handle rule with empty string regex", () => { + const rule = { rule: "Some rule content", regex: "" }; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + + it("should handle rule with empty array regex", () => { + const rule = { rule: "Some rule content", regex: [] }; + expect(getRuleType(rule)).toBe(RuleType.Always); + }); + + it("should handle rule with empty string description", () => { + const rule = { + rule: "Some rule content", + description: "", + alwaysApply: false, + }; + expect(getRuleType(rule)).toBe(RuleType.Manual); + }); + }); +}); diff --git a/packages/config-yaml/src/markdown/getRuleType.ts b/packages/config-yaml/src/markdown/getRuleType.ts new file mode 100644 index 000000000..274246545 --- /dev/null +++ b/packages/config-yaml/src/markdown/getRuleType.ts @@ -0,0 +1,55 @@ +import { RuleObject } from "../schemas/index.js"; + +export enum RuleType { + Always = "Always", + AutoAttached = "Auto Attached", + AgentRequested = "Agent Requested", + Manual = "Manual", +} + +export const RuleTypeDescriptions: Record = { + [RuleType.Always]: "Always included in model context", + [RuleType.AutoAttached]: + "Included when files matching a glob pattern are referenced", + [RuleType.AgentRequested]: + "Available to AI, which decides whether to include it. Must provide a description", + [RuleType.Manual]: "Only included when explicitly mentioned using @ruleName", +}; + +/** + * Determines the rule type based on the rule properties + */ +export function getRuleType(rule: Partial): RuleType { + // Check if globs/regex have meaningful values (not empty arrays/strings) + const hasGlobs = + rule.globs && + (Array.isArray(rule.globs) + ? rule.globs.length > 0 + : rule.globs.trim().length > 0); + const hasRegex = + rule.regex && + (Array.isArray(rule.regex) + ? rule.regex.length > 0 + : rule.regex.trim().length > 0); + + // Auto Attached: has globs and/or regex patterns + if (hasGlobs || hasRegex) { + return RuleType.AutoAttached; + } + + // Check if description has meaningful value + const hasDescription = rule.description && rule.description.trim().length > 0; + + // Agent Requested: has description and alwaysApply is false + if (hasDescription && rule.alwaysApply === false) { + return RuleType.AgentRequested; + } + + // Manual: alwaysApply is false but no description + if (rule.alwaysApply === false && !hasDescription) { + return RuleType.Manual; + } + + // Always: default case (alwaysApply true/undefined, no globs/regex) + return RuleType.Always; +} diff --git a/packages/config-yaml/src/markdown/index.ts b/packages/config-yaml/src/markdown/index.ts index 7cf4903de..89e05eb1f 100644 --- a/packages/config-yaml/src/markdown/index.ts +++ b/packages/config-yaml/src/markdown/index.ts @@ -1,2 +1,3 @@ export * from "./createMarkdownRule.js"; +export * from "./getRuleType.js"; export * from "./markdownToRule.js";