feat: move rule type logic into config-yaml

This commit is contained in:
Patrick Erichsen 2025-07-02 15:25:48 -07:00
parent 2e691bfd58
commit fd94ef2b31
8 changed files with 285 additions and 76 deletions

View file

@ -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.",
},
},
},
},

View file

@ -5,29 +5,18 @@ import { createRuleFilePath } from "../../config/markdown/utils";
export type CreateRuleBlockArgs = Pick<
Required<RuleWithSource>,
"rule" | "description" | "alwaysApply" | "name"
"rule" | "name"
> &
Pick<RuleWithSource, "globs" | "regex">;
Pick<RuleWithSource, "globs" | "regex" | "description" | "alwaysApply">;
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,

View file

@ -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({
</label>
<InformationCircleIcon
data-tooltip-id={tooltipId}
data-tooltip-content={getRuleTypeTooltip(selectedRuleType)}
data-tooltip-content={
RuleTypeDescriptions[selectedRuleType]
}
className="h-4 w-4 text-gray-500"
/>
<ToolTip id={tooltipId} style={{ zIndex: 100001 }} />
</div>
<select
className="border-input-border bg-input text-input-foreground focus:border-border-focus w-full rounded-md border px-3 py-2 text-xs focus:outline-none"
value={selectedRuleType}
onChange={(e) =>
handleRuleTypeChange(e.target.value as RuleType)
}
>
<option value={RuleType.Always}>Always</option>
<option value={RuleType.AutoAttached}>Auto Attached</option>
<option value={RuleType.AgentRequested}>
Agent Requested
</option>
<option value={RuleType.Manual}>Manual</option>
</select>
<div className="relative">
<select
className="border-input-border bg-input text-input-foreground focus:border-border-focus w-full rounded-md border px-3 py-2 text-xs focus:outline-none"
value={isGenerating ? "" : selectedRuleType}
onChange={(e) =>
handleRuleTypeChange(e.target.value as RuleType)
}
disabled={isGenerating}
>
{isGenerating ? (
<option value=""></option>
) : (
<>
<option value={RuleType.Always}>Always</option>
<option value={RuleType.AutoAttached}>
Auto Attached
</option>
<option value={RuleType.AgentRequested}>
Agent Requested
</option>
<option value={RuleType.Manual}>Manual</option>
</>
)}
</select>
{isGenerating && (
<div className="absolute left-3 top-1/2 -translate-y-1/2">
<Spinner />
</div>
)}
</div>
</div>
{/* Description (for Agent Requested only) */}
@ -275,7 +266,7 @@ export function GenerationScreen({
{...register("rule")}
/>
{showSpinner && (
<div className="absolute left-2 top-8">
<div className="absolute left-3 top-8">
<Spinner />
</div>
)}

View file

@ -51,7 +51,7 @@ export function InputScreen({
/>
</div>
<div className="mt-2 flex flex-wrap items-center justify-center gap-2">
<div className="mt-2 flex flex-wrap items-center justify-center gap-x-3">
{ruleTemplates.map((template, index) => (
<RuleTemplateChip
key={index}

View file

@ -1,4 +1,9 @@
import { DocumentTextIcon } from "@heroicons/react/24/outline";
import {
AtSymbolIcon,
CodeBracketIcon,
GlobeAltIcon,
SparklesIcon,
} from "@heroicons/react/24/outline";
export interface RuleTemplate {
icon: React.ComponentType<{ className?: string }>;
@ -8,8 +13,24 @@ export interface RuleTemplate {
export const ruleTemplates: RuleTemplate[] = [
{
icon: DocumentTextIcon,
title: "Summarize learnings",
template: "Lorem ipsum dolor est"
}
];
icon: GlobeAltIcon,
title: "Always Applied",
template: "Create an always applied rule where for all files, <BEHAVIOR>",
},
{
icon: CodeBracketIcon,
title: "Auto attached",
template:
"Create an auto-attached rule where for all <GLOB_PATTERN> files, do <BEHAVIOR>",
},
{
icon: SparklesIcon,
title: "Agent Requested",
template: "Create an agent requested rule where <BEHAVIOR>",
},
{
icon: AtSymbolIcon,
title: "Manual",
template: "Create a manually requested rule where <BEHAVIOR>",
},
];

View file

@ -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);
});
});
});

View file

@ -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, string> = {
[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<RuleObject>): 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;
}

View file

@ -1,2 +1,3 @@
export * from "./createMarkdownRule.js";
export * from "./getRuleType.js";
export * from "./markdownToRule.js";