From 2c8032c3397f6ac8c26b8ce5e5b2fe89d079d3da Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Thu, 17 Apr 2025 21:42:52 -0700 Subject: [PATCH] feat: `create_rule_block` tool --- core/tools/builtIn.ts | 1 + core/tools/callTool.ts | 3 + core/tools/definitions/createRuleBlock.ts | 37 +++++++++++ core/tools/implementations/createRuleBlock.ts | 62 +++++++++++++++++++ core/tools/index.ts | 4 +- docs/docs/customize/deep-dives/rules.md | 10 ++- .../gui/ToolCallDiv/ToolCallButtonsDiv.tsx | 2 +- 7 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 core/tools/definitions/createRuleBlock.ts create mode 100644 core/tools/implementations/createRuleBlock.ts diff --git a/core/tools/builtIn.ts b/core/tools/builtIn.ts index 7cff1c70e..5e269c232 100644 --- a/core/tools/builtIn.ts +++ b/core/tools/builtIn.ts @@ -9,6 +9,7 @@ export enum BuiltInToolNames { SearchWeb = "builtin_search_web", ViewDiff = "builtin_view_diff", LSTool = "builtin_ls", + CreateRuleBlock = "builtin_create_rule_block", // excluded from allTools for now ViewRepoMap = "builtin_view_repo_map", diff --git a/core/tools/callTool.ts b/core/tools/callTool.ts index 28a8f8c30..436b71874 100644 --- a/core/tools/callTool.ts +++ b/core/tools/callTool.ts @@ -4,6 +4,7 @@ import { canParseUrl } from "../util/url"; import { BuiltInToolNames } from "./builtIn"; import { createNewFileImpl } from "./implementations/createNewFile"; +import { createRuleBlockImpl } from "./implementations/createRuleBlock"; import { fileGlobSearchImpl } from "./implementations/globSearch"; import { grepSearchImpl } from "./implementations/grepSearch"; import { lsToolImpl } from "./implementations/lsTool"; @@ -156,6 +157,8 @@ export async function callTool( return await lsToolImpl(args, extras); case BuiltInToolNames.ReadCurrentlyOpenFile: return await readCurrentlyOpenFileImpl(args, extras); + case BuiltInToolNames.CreateRuleBlock: + return await createRuleBlockImpl(args, extras); // case BuiltInToolNames.ViewRepoMap: // return await viewRepoMapImpl(args, extras); // case BuiltInToolNames.ViewSubdirectory: diff --git a/core/tools/definitions/createRuleBlock.ts b/core/tools/definitions/createRuleBlock.ts new file mode 100644 index 000000000..987ef0f80 --- /dev/null +++ b/core/tools/definitions/createRuleBlock.ts @@ -0,0 +1,37 @@ +import { Tool } from "../.."; +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 }}}"', + readonly: false, + group: BUILT_IN_GROUP_NAME, + function: { + name: BuiltInToolNames.CreateRuleBlock, + description: + "Creates a persistent rule block that will be applied to all future Chat, Edit and Agent conversations. " + + "Use this tool when the user wants to establish ongoing behaviors, preferences, or standards for code generation. " + + "This is ideal for capturing recurring preferences like 'always use named exports instead of default exports' or 'follow Google style docstrings'. " + + "Do NOT use this tool for one-time instructions that should only apply to the current conversation. " + + "The created rule will be formatted as a YAML file and stored in the user's `.continue/rules` directory.", + parameters: { + type: "object", + required: ["rule_name", "rule_content"], + properties: { + rule_name: { + type: "string", + description: + "A clear, descriptive name for the rule block that summarizes its purpose. This name will be visible in the Continue UI and should help users identify the rule's function. For example: 'React Component Standards', 'Python Type Hints', or 'Code Documentation Requirements'.", + }, + rule_content: { + type: "string", + description: + "The specific instruction or standard to be followed in all future conversations. This should be a clear, concise directive written in imperative form. For example: 'Always use named exports instead of default exports in JavaScript modules', 'Always annotate Python functions with parameter and return types', or 'Use camelCase for variable names in JavaScript'. Each rule should focus on a single, well-defined behavior or standard.", + }, + }, + }, + }, +}; diff --git a/core/tools/implementations/createRuleBlock.ts b/core/tools/implementations/createRuleBlock.ts new file mode 100644 index 000000000..5e6cf14d9 --- /dev/null +++ b/core/tools/implementations/createRuleBlock.ts @@ -0,0 +1,62 @@ +import { ConfigYaml } from "@continuedev/config-yaml"; +import * as YAML from "yaml"; +import { ToolImpl } from "."; +import { joinPathsToUri } from "../../util/uri"; + +export interface CreateRuleBlockArgs { + rule_name: string; + rule_content: string; +} + +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.rule_name + .toLowerCase() + .replace(/[^a-z0-9\s-]/g, "") + .replace(/\s+/g, "-"); + + const ruleBlock: ConfigYaml = { + name: args.rule_name, + version: "0.0.1", + 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, + ], + }; + + // Convert the rule block to YAML + const ruleYaml = YAML.stringify(ruleBlock); + + const [localContinueDir] = await extras.ide.getWorkspaceDirs(); + const rulesDirUri = joinPathsToUri( + localContinueDir, + ".continue", + "rules", + `${safeRuleName}.yaml`, + ); + + try { + await extras.ide.writeFile(rulesDirUri, ruleYaml); + await extras.ide.openFile(rulesDirUri); + + return [ + { + name: "Rule Block Created", + description: `Created ${args.rule_name} rule`, + content: ruleYaml, + }, + ]; + } catch (error) { + return [ + { + name: "Error", + description: "Failed to create rule block", + content: `Error creating rule block: ${error instanceof Error ? error.message : String(error)}`, + }, + ]; + } +}; diff --git a/core/tools/index.ts b/core/tools/index.ts index c9875c5eb..d89cf8744 100644 --- a/core/tools/index.ts +++ b/core/tools/index.ts @@ -1,4 +1,5 @@ import { createNewFileTool } from "./definitions/createNewFile"; +import { createRuleBlock } from "./definitions/createRuleBlock"; import { editFileTool } from "./definitions/editFile"; import { globSearchTool } from "./definitions/globSearch"; import { grepSearchTool } from "./definitions/grepSearch"; @@ -14,14 +15,13 @@ export const allTools = [ editFileTool, createNewFileTool, runTerminalCommandTool, - grepSearchTool, globSearchTool, searchWebTool, viewDiffTool, readCurrentlyOpenFileTool, - lsTool, + createRuleBlock, // replacing with ls tool for now // viewSubdirectoryTool, // viewRepoMapTool, diff --git a/docs/docs/customize/deep-dives/rules.md b/docs/docs/customize/deep-dives/rules.md index 3143b12cf..a37909d77 100644 --- a/docs/docs/customize/deep-dives/rules.md +++ b/docs/docs/customize/deep-dives/rules.md @@ -17,9 +17,15 @@ To form the system message, rules are joined with new lines, in the order they a ## `rules` blocks -Rules can be added to an Assistant on the Continue Hub. Explore available rules [here](https://hub.continue.dev/explore/rules), or [create your own](https://hub.continue.dev/new?type=block&blockType=rules) in the Hub. +Rules can be added to an Assistant on the Continue Hub. Explore available rules [here](https://hub.continue.dev/explore/rules), or [create your own](https://hub.continue.dev/new?type=block&blockType=rules) in the Hub. These blocks are defined using the [`config.yaml` syntax](../../reference.md#rules) and can also be created locally. -These blocks are defined using the [`config.yaml` syntax](../../reference.md#rules) and can also be created locally. Rules blocks can be simple text, or have the following properties: +:::info Automatically create local rule blocks +When in Agent mode, you can simply prompt the agent to create a rule for you. + +For example, you can say "Create a rule to always use named exports instead of default exports", and a rule will be created for you in `~/.continue/rules`. +::: + +Rules blocks can be simple text, or have the following properties: - `name` (**required**): A display name/title for the rule - `rule` (**required**): The text content of the rule diff --git a/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx b/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx index 956b98158..2792efafd 100644 --- a/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx +++ b/gui/src/pages/gui/ToolCallDiv/ToolCallButtonsDiv.tsx @@ -79,7 +79,7 @@ export function ToolCallButtons(props: ToolCallButtonsProps) { onClick={() => dispatch(callTool())} data-testid="accept-tool-call-button" > - Continue + Accept ) : toolCallState.status === "calling" ? (