diff --git a/core/tools/definitions/createRuleBlock.ts b/core/tools/definitions/createRuleBlock.ts index 5c7b69c64..098edda10 100644 --- a/core/tools/definitions/createRuleBlock.ts +++ b/core/tools/definitions/createRuleBlock.ts @@ -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'])", + }, }, }, }, diff --git a/core/tools/implementations/createRuleBlock.test.ts b/core/tools/implementations/createRuleBlock.test.ts new file mode 100644 index 000000000..698c572b2 --- /dev/null +++ b/core/tools/implementations/createRuleBlock.test.ts @@ -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"); + }); +}); \ No newline at end of file diff --git a/core/tools/implementations/createRuleBlock.ts b/core/tools/implementations/createRuleBlock.ts index 4e37f8818..7d58b8281 100644 --- a/core/tools/implementations/createRuleBlock.ts +++ b/core/tools/implementations/createRuleBlock.ts @@ -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, }, ]; diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 22390e25e..9bd027d77 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -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", diff --git a/gui/package-lock.json b/gui/package-lock.json index 765d4d78a..bb64c07ff 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -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",