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..bf5caa6dc --- /dev/null +++ b/core/tools/definitions/createRuleBlock.ts @@ -0,0 +1,35 @@ +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 that applies to all future conversations. " + + "Use for establishing ongoing code standards or preferences (e.g. 'use named exports', 'follow Google docstrings'). " + + "Not for one-time instructions. Rules are stored as YAML in `.continue/rules`.", + parameters: { + type: "object", + required: ["rule_name", "rule_content"], + properties: { + rule_name: { + type: "string", + description: + "Short, descriptive name summarizing the rule's purpose (e.g. 'React Standards', 'Type Hints')", + }, + rule_content: { + 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.", + }, + }, + }, + }, +}; diff --git a/core/tools/implementations/createRuleBlock.ts b/core/tools/implementations/createRuleBlock.ts new file mode 100644 index 000000000..4e37f8818 --- /dev/null +++ b/core/tools/implementations/createRuleBlock.ts @@ -0,0 +1,52 @@ +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`, + ); + + await extras.ide.writeFile(rulesDirUri, ruleYaml); + await extras.ide.openFile(rulesDirUri); + + return [ + { + name: "Rule Block Created", + description: `Created ${args.rule_name} rule`, + content: ruleYaml, + }, + ]; +}; 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/agent/how-it-works.md b/docs/docs/agent/how-it-works.md index b89c7ac85..1191fa9f8 100644 --- a/docs/docs/agent/how-it-works.md +++ b/docs/docs/agent/how-it-works.md @@ -9,9 +9,10 @@ Agent offers the same functionality as Chat, while also including tools in the r ## The tool handshake -Tools provide a flexible, powerful way for models to interface with the external world. They are provided to the model as a JSON object with a name and an arguments schema. For example, a `read_file` tool with a `filepath` argument will give the model the ability to request the contents of a specific file. +Tools provide a flexible, powerful way for models to interface with the external world. They are provided to the model as a JSON object with a name and an arguments schema. For example, a `read_file` tool with a `filepath` argument will give the model the ability to request the contents of a specific file. The following handshake describes how Agent uses tools: + 1. In Agent mode, available tools are sent along with `user` chat requests 2. The model can choose to include a tool call in its response 3. The user gives permission. This step is skipped if the policy for that tool is set to `Automatic` @@ -32,7 +33,8 @@ Continue includes several built-in tools which provide the model access to IDE f - **Create new file** (`builtin_create_new_file`): Create a new file within the project, with path and contents specified by the model - **Exact search** (`builtin_exact_search`): perform a `ripgrep` search within the project - **Run terminal command** (`builtin_run_terminal_command`): run a terminal command from the workspace root -- **Search web** (`builtin_search_web`): Perform a web search to get top results +- **Search web** (`builtin_search_web`): Perform a web search to get top results - **View diff** (`builtin_view_diff`): View the current working git diff - **View repo map** (`builtin_view_repo_map`): request a copy of the repository map—same as the [Repo Map Context Provider](../customize/context-providers.mdx#repository-map) -- **View subdirectory** (`builtin_view_subdirectory`): request a copy of a repo map for a specific directory within the project \ No newline at end of file +- **View subdirectory** (`builtin_view_subdirectory`): request a copy of a repo map for a specific directory within the project +- **Create Rule Block** (`builtin_create_rule_block`): creates a new rule block in `.continue/rules` based on the contents of the conversation diff --git a/docs/docs/customize/deep-dives/rules.md b/docs/docs/customize/deep-dives/rules.md index 3143b12cf..94dec8a07 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 for this", and a rule will be created for you in `~/.continue/rules` based on your conversation. +::: + +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/redux/slices/uiSlice.ts b/gui/src/redux/slices/uiSlice.ts index 0ea07b190..7cc964cd6 100644 --- a/gui/src/redux/slices/uiSlice.ts +++ b/gui/src/redux/slices/uiSlice.ts @@ -52,6 +52,7 @@ export const uiSlice = createSlice({ [BuiltInToolNames.SearchWeb]: "allowedWithoutPermission", [BuiltInToolNames.ViewDiff]: "allowedWithoutPermission", [BuiltInToolNames.LSTool]: "allowedWithoutPermission", + [BuiltInToolNames.CreateRuleBlock]: "allowedWithPermission", }, toolGroupSettings: { BUILT_IN_GROUP_NAME: "include",