feat: add globs to create rule tool

This commit is contained in:
Patrick Erichsen 2025-04-25 22:41:03 -07:00
parent 5f4a9e5189
commit 047e4e418b
5 changed files with 112 additions and 16 deletions

View file

@ -4,9 +4,9 @@ import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn";
export const createRuleBlock: Tool = {
type: "function",
displayTitle: "Create Rule Block",
wouldLikeTo: 'create a rule block for "{{{ rule_name }}}"',
isCurrently: 'creating a rule block for "{{{ rule_name }}}"',
hasAlready: 'created a rule block for "{{{ rule_name }}}"',
wouldLikeTo: 'create a rule block for "{{{ name }}}"',
isCurrently: 'creating a rule block for "{{{ name }}}"',
hasAlready: 'created a rule block for "{{{ name }}}"',
readonly: false,
isInstant: true,
group: BUILT_IN_GROUP_NAME,
@ -18,18 +18,23 @@ export const createRuleBlock: Tool = {
"Not for one-time instructions. Rules are stored as YAML in `.continue/rules`.",
parameters: {
type: "object",
required: ["rule_name", "rule_content"],
required: ["name", "rule"],
properties: {
rule_name: {
name: {
type: "string",
description:
"Short, descriptive name summarizing the rule's purpose (e.g. 'React Standards', 'Type Hints')",
},
rule_content: {
rule: {
type: "string",
description:
"Clear, imperative instruction for future code generation (e.g. 'Use named exports', 'Add Python type hints'). Each rule should focus on one specific standard.",
},
globs: {
type: "string",
description:
"Optional file patterns to which this rule applies (e.g. ['**/*.{ts,tsx}'] or ['src/**/*.ts', 'tests/**/*.ts'])",
},
},
},
},

View file

@ -0,0 +1,83 @@
import * as YAML from "yaml";
import { createRuleBlockImpl } from "./createRuleBlock";
// Mock the extras parameter with necessary functions
const mockIde = {
getWorkspaceDirs: jest.fn().mockResolvedValue(["/"]),
writeFile: jest.fn().mockResolvedValue(undefined),
openFile: jest.fn().mockResolvedValue(undefined),
};
const mockExtras = {
ide: mockIde,
};
describe("createRuleBlockImpl", () => {
beforeEach(() => {
jest.clearAllMocks();
});
it("should create a basic rule with name and rule content", async () => {
const args = {
name: "Test Rule",
rule: "Always write tests",
};
const result = await createRuleBlockImpl(args, mockExtras as any);
// Verify that writeFile was called with the correct YAML
expect(mockIde.writeFile).toHaveBeenCalled();
const yamlContent = mockIde.writeFile.mock.calls[0][1];
const parsedYaml = YAML.parse(yamlContent);
// Verify the structure of the YAML
expect(parsedYaml).toEqual({
name: "Test Rule",
version: "0.0.1",
schema: "v1",
rules: [
{
name: "Test Rule",
rule: "Always write tests",
},
],
});
// Verify the result
expect(result).toHaveLength(1);
expect(result[0].name).toBe("Rule Block Created");
expect(result[0].description).toBe("Created Test Rule rule");
});
it("should create a rule with glob pattern", async () => {
const args = {
name: "TypeScript Rule",
rule: "Use interfaces for object shapes",
globs: "**/*.{ts,tsx}",
};
await createRuleBlockImpl(args, mockExtras as any);
// Verify the YAML structure with globs
const yamlContent = mockIde.writeFile.mock.calls[0][1];
const parsedYaml = YAML.parse(yamlContent);
\ expect(parsedYaml.rules[0]).toEqual({
name: "TypeScript Rule",
rule: "Use interfaces for object shapes",
globs: "**/*.{ts,tsx}",
});
});
it("should create a filename based on sanitized rule name", async () => {
const args = {
name: "Special Ch@racters & Spaces",
rule: "Handle special characters",
};
await createRuleBlockImpl(args, mockExtras as any);
// Check that the filename is sanitized
const fileUri = mockIde.writeFile.mock.calls[0][0];
expect(fileUri).toContain("special-chracters-spaces.yaml");
});
});

View file

@ -4,8 +4,9 @@ import { ToolImpl } from ".";
import { joinPathsToUri } from "../../util/uri";
export interface CreateRuleBlockArgs {
rule_name: string;
rule_content: string;
name: string;
rule: string;
globs?: string;
}
export const createRuleBlockImpl: ToolImpl = async (
@ -13,18 +14,25 @@ export const createRuleBlockImpl: ToolImpl = async (
extras,
) => {
// Sanitize rule name for use in filename (remove special chars, replace spaces with dashes)
const safeRuleName = args.rule_name
const safeRuleName = args.name
.toLowerCase()
.replace(/[^a-z0-9\s-]/g, "")
.replace(/\s+/g, "-");
// Build the rule object
const ruleObject = {
name: args.name,
rule: args.rule,
...(args.globs ? { globs: args.globs.trim() } : {}),
};
const ruleBlock: ConfigYaml = {
name: args.rule_name,
name: args.name,
version: "0.0.1",
schema: "v1",
rules: [
// This can be either a string or an object with {name, rule}
// Since we want a simple rule, we use the string format
args.rule_content,
// Add the rule object instead of a simple string
ruleObject,
],
};
@ -45,7 +53,7 @@ export const createRuleBlockImpl: ToolImpl = async (
return [
{
name: "Rule Block Created",
description: `Created ${args.rule_name} rule`,
description: `Created ${args.name} rule`,
content: ruleYaml,
},
];

View file

@ -109,7 +109,7 @@
"@continuedev/config-yaml": "file:../packages/config-yaml",
"@continuedev/fetch": "^1.0.4",
"@continuedev/llm-info": "^1.0.8",
"@continuedev/openai-adapters": "^1.0.18",
"@continuedev/openai-adapters": "^1.0.19",
"@modelcontextprotocol/sdk": "^1.5.0",
"@mozilla/readability": "^0.5.0",
"@octokit/rest": "^20.1.1",

2
gui/package-lock.json generated
View file

@ -112,7 +112,7 @@
"@continuedev/config-yaml": "file:../packages/config-yaml",
"@continuedev/fetch": "^1.0.4",
"@continuedev/llm-info": "^1.0.8",
"@continuedev/openai-adapters": "^1.0.18",
"@continuedev/openai-adapters": "^1.0.19",
"@modelcontextprotocol/sdk": "^1.5.0",
"@mozilla/readability": "^0.5.0",
"@octokit/rest": "^20.1.1",