diff --git a/core/config/load.ts b/core/config/load.ts index e3d83a6b6..9948071fb 100644 --- a/core/config/load.ts +++ b/core/config/load.ts @@ -51,7 +51,6 @@ import { LLMReranker } from "../llm/llms/llm"; import TransformersJsEmbeddingsProvider from "../llm/llms/TransformersJsEmbeddingsProvider"; import { slashCommandFromPromptFileV1 } from "../promptFiles/v1/slashCommandFromPromptFile"; import { getAllPromptFiles } from "../promptFiles/v2/getPromptFiles"; -import { allTools } from "../tools"; import { copyOf } from "../util"; import { GlobalContext } from "../util/GlobalContext"; import mergeJson from "../util/merge"; @@ -67,6 +66,7 @@ import { } from "../util/paths"; import { localPathToUri } from "../util/pathToUri"; +import { baseToolDefinitions } from "../tools"; import { modifyAnyConfigWithSharedConfig } from "./sharedConfig"; import { getModelByRole, @@ -529,7 +529,7 @@ async function intermediateToFinalConfig({ const continueConfig: ContinueConfig = { ...config, contextProviders, - tools: [...allTools], + tools: [...baseToolDefinitions], mcpServerStatuses: [], slashCommands: config.slashCommands ?? [], modelsByRole: { diff --git a/core/config/profile/doLoadConfig.ts b/core/config/profile/doLoadConfig.ts index f5a0568da..863c4d447 100644 --- a/core/config/profile/doLoadConfig.ts +++ b/core/config/profile/doLoadConfig.ts @@ -25,6 +25,7 @@ import { ControlPlaneClient } from "../../control-plane/client.js"; import { getControlPlaneEnv } from "../../control-plane/env.js"; import { TeamAnalytics } from "../../control-plane/TeamAnalytics.js"; import ContinueProxy from "../../llm/llms/stubs/ContinueProxy"; +import { getConfigDependentToolDefinitions } from "../../tools"; import { encodeMCPToolUri } from "../../tools/callTool"; import { getConfigJsonPath, getConfigYamlPath } from "../../util/paths"; import { localPathOrUriToPath } from "../../util/pathToUri"; @@ -202,6 +203,12 @@ export default async function doLoadConfig(options: { } } + newConfig.tools.push( + ...getConfigDependentToolDefinitions({ + rules: newConfig.rules, + }), + ); + // Detect duplicate tool names const counts: Record = {}; newConfig.tools.forEach((tool) => { @@ -211,6 +218,7 @@ export default async function doLoadConfig(options: { counts[tool.function.name] = 1; } }); + Object.entries(counts).forEach(([toolName, count]) => { if (count > 1) { errors!.push({ @@ -220,6 +228,26 @@ export default async function doLoadConfig(options: { } }); + const ruleCounts: Record = {}; + newConfig.rules.forEach((rule) => { + if (rule.name) { + if (ruleCounts[rule.name]) { + ruleCounts[rule.name] = ruleCounts[rule.name] + 1; + } else { + ruleCounts[rule.name] = 1; + } + } + }); + + Object.entries(ruleCounts).forEach(([ruleName, count]) => { + if (count > 1) { + errors!.push({ + fatal: false, + message: `Duplicate (${count}) rules named "${ruleName}" detected. This may cause unexpected behavior`, + }); + } + }); + newConfig.allowAnonymousTelemetry = newConfig.allowAnonymousTelemetry && (await ide.isTelemetryEnabled()); diff --git a/core/config/yaml/loadYaml.ts b/core/config/yaml/loadYaml.ts index 0a0dd03fc..daeecf57e 100644 --- a/core/config/yaml/loadYaml.ts +++ b/core/config/yaml/loadYaml.ts @@ -36,11 +36,11 @@ import FreeTrial from "../../llm/llms/FreeTrial"; import TransformersJsEmbeddingsProvider from "../../llm/llms/TransformersJsEmbeddingsProvider"; import { slashCommandFromPromptFileV1 } from "../../promptFiles/v1/slashCommandFromPromptFile"; import { getAllPromptFiles } from "../../promptFiles/v2/getPromptFiles"; -import { allTools } from "../../tools"; import { GlobalContext } from "../../util/GlobalContext"; import { modifyAnyConfigWithSharedConfig } from "../sharedConfig"; import { getControlPlaneEnvSync } from "../../control-plane/env"; +import { baseToolDefinitions } from "../../tools"; import { getCleanUriPath } from "../../util/uri"; import { getAllDotContinueDefinitionFiles } from "../loadLocalAssistants"; import { LocalPlatformClient } from "./LocalPlatformClient"; @@ -193,7 +193,7 @@ async function configYamlToContinueConfig(options: { const continueConfig: ContinueConfig = { slashCommands: [], - tools: [...allTools], + tools: [...baseToolDefinitions], mcpServerStatuses: [], contextProviders: [], modelsByRole: { diff --git a/core/core.ts b/core/core.ts index 6b5f9814e..497e64c66 100644 --- a/core/core.ts +++ b/core/core.ts @@ -723,6 +723,7 @@ export class Core { }; return await callTool(tool, toolCall.function.arguments, { + config, ide: this.ide, llm: config.selectedModelByRole.chat, fetch: (url, init) => diff --git a/core/index.d.ts b/core/index.d.ts index f7aa806fd..1f5d0d4a5 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -975,6 +975,7 @@ export interface ToolExtras { toolCallId: string; contextItems: ContextItem[]; }) => void; + config: ContinueConfig; } export interface Tool { @@ -1004,6 +1005,12 @@ interface ToolChoice { }; } +export interface ConfigDependentToolParams { + rules: RuleWithSource[]; +} + +export type GetTool = (params: ConfigDependentToolParams) => Tool; + export interface BaseCompletionOptions { temperature?: number; topP?: number; diff --git a/core/tools/builtIn.ts b/core/tools/builtIn.ts index 9f70b1a22..892cc3ff8 100644 --- a/core/tools/builtIn.ts +++ b/core/tools/builtIn.ts @@ -10,6 +10,7 @@ export enum BuiltInToolNames { ViewDiff = "builtin_view_diff", LSTool = "builtin_ls", CreateRuleBlock = "builtin_create_rule_block", + RequestRule = "builtin_request_rule", // excluded from allTools for now ViewRepoMap = "builtin_view_repo_map", @@ -18,4 +19,4 @@ export enum BuiltInToolNames { export const BUILT_IN_GROUP_NAME = "Built-In"; -export const CLIENT_TOOLS = [BuiltInToolNames.EditExistingFile]; +export const CLIENT_TOOLS_IMPLS = [BuiltInToolNames.EditExistingFile]; diff --git a/core/tools/callTool.ts b/core/tools/callTool.ts index 34501550b..243c162bb 100644 --- a/core/tools/callTool.ts +++ b/core/tools/callTool.ts @@ -10,6 +10,7 @@ import { grepSearchImpl } from "./implementations/grepSearch"; import { lsToolImpl } from "./implementations/lsTool"; import { readCurrentlyOpenFileImpl } from "./implementations/readCurrentlyOpenFile"; import { readFileImpl } from "./implementations/readFile"; +import { requestRuleImpl } from "./implementations/requestRule"; import { runTerminalCommandImpl } from "./implementations/runTerminalCommand"; import { searchWebImpl } from "./implementations/searchWeb"; import { viewDiffImpl } from "./implementations/viewDiff"; @@ -157,6 +158,8 @@ async function callBuiltInTool( return await readCurrentlyOpenFileImpl(args, extras); case BuiltInToolNames.CreateRuleBlock: return await createRuleBlockImpl(args, extras); + case BuiltInToolNames.RequestRule: + return await requestRuleImpl(args, extras); default: throw new Error(`Tool "${functionName}" not found`); } diff --git a/core/tools/definitions/requestRule.test.ts b/core/tools/definitions/requestRule.test.ts new file mode 100644 index 000000000..e1d0feb81 --- /dev/null +++ b/core/tools/definitions/requestRule.test.ts @@ -0,0 +1,237 @@ +import { RuleWithSource } from "../.."; +import { getRequestRuleDescription } from "./requestRule"; + +describe("getRequestRuleDescription", () => { + it("should return no rules message when no agent-requested rules exist", () => { + const rules: RuleWithSource[] = [ + { + name: "Always Apply Rule", + description: "This rule always applies", + source: "default-chat", + rule: "Always use semicolons", + alwaysApply: true, + ruleFile: "/path/to/rule1.md", + }, + { + name: "Rule with Globs", + description: "This rule has globs", + source: "rules-block", + rule: "Use TypeScript", + globs: "**/*.ts", + ruleFile: "/path/to/rule2.md", + }, + ]; + + const result = getRequestRuleDescription(rules); + + expect(result).toBe( + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\nNo rules available.", + ); + }); + + it("should return formatted rules when agent-requested rules exist", () => { + const rules: RuleWithSource[] = [ + { + name: "Agent Rule 1", + description: "First agent-requested rule", + source: "rules-block", + rule: "Use consistent formatting", + alwaysApply: false, + ruleFile: "/path/to/agent-rule1.md", + }, + { + name: "Agent Rule 2", + description: "Second agent-requested rule", + source: "default-agent", + rule: "Follow naming conventions", + alwaysApply: false, + ruleFile: "/path/to/agent-rule2.md", + }, + // These should be filtered out + { + name: "Always Apply Rule", + description: "This rule always applies", + source: "default-chat", + rule: "Always use semicolons", + alwaysApply: true, + ruleFile: "/path/to/rule1.md", + }, + { + name: "Rule with Globs", + description: "This rule has globs", + source: "rules-block", + rule: "Use TypeScript", + globs: "**/*.ts", + ruleFile: "/path/to/rule2.md", + }, + ]; + + const result = getRequestRuleDescription(rules); + + const expected = + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Agent Rule 1: First agent-requested rule\n" + + "Agent Rule 2: Second agent-requested rule"; + + expect(result).toBe(expected); + }); + + it("should handle rules with missing description", () => { + const rules: RuleWithSource[] = [ + { + name: "Rule Without Description", + source: "rules-block", + rule: "Some rule content", + alwaysApply: false, + ruleFile: "/path/to/no-desc-rule.md", + }, + ]; + + const result = getRequestRuleDescription(rules); + + const expected = + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Rule Without Description: undefined"; + + expect(result).toBe(expected); + }); + + it("should handle rules with missing name", () => { + const rules: RuleWithSource[] = [ + { + description: "Rule without name", + source: "rules-block", + rule: "Some rule content", + alwaysApply: false, + ruleFile: "/path/to/no-name-rule.md", + }, + ]; + + const result = getRequestRuleDescription(rules); + + const expected = + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "undefined: Rule without name"; + + expect(result).toBe(expected); + }); + + it("should handle rules with missing ruleFile", () => { + const rules: RuleWithSource[] = [ + { + name: "Rule Without File", + description: "Rule without file path", + source: "rules-block", + rule: "Some rule content", + alwaysApply: false, + }, + ]; + + const result = getRequestRuleDescription(rules); + + const expected = + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Rule Without File: Rule without file path"; + + expect(result).toBe(expected); + }); + + it("should filter out rules with alwaysApply undefined (truthy)", () => { + const rules: RuleWithSource[] = [ + { + name: "Rule with undefined alwaysApply", + description: "This should be filtered out", + source: "rules-block", + rule: "Some rule", + ruleFile: "/path/to/rule.md", + // alwaysApply is undefined, which is truthy in the filter condition + }, + { + name: "Valid Agent Rule", + description: "This should be included", + source: "rules-block", + rule: "Some rule", + alwaysApply: false, + ruleFile: "/path/to/valid-rule.md", + }, + ]; + + const result = getRequestRuleDescription(rules); + + const expected = + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Valid Agent Rule: This should be included"; + + expect(result).toBe(expected); + }); + + it("should filter out rules with any globs defined", () => { + const rules: RuleWithSource[] = [ + { + name: "Rule with string globs", + description: "This should be filtered out", + source: "rules-block", + rule: "Some rule", + alwaysApply: false, + globs: "**/*.ts", + ruleFile: "/path/to/rule1.md", + }, + { + name: "Rule with array globs", + description: "This should also be filtered out", + source: "rules-block", + rule: "Some rule", + alwaysApply: false, + globs: ["**/*.ts", "**/*.js"], + ruleFile: "/path/to/rule2.md", + }, + { + name: "Valid Agent Rule", + description: "This should be included", + source: "rules-block", + rule: "Some rule", + alwaysApply: false, + ruleFile: "/path/to/valid-rule.md", + }, + ]; + + const result = getRequestRuleDescription(rules); + + const expected = + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Valid Agent Rule: This should be included"; + + expect(result).toBe(expected); + }); + + it("should handle empty rules array", () => { + const rules: RuleWithSource[] = []; + + const result = getRequestRuleDescription(rules); + + expect(result).toBe( + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\nNo rules available.", + ); + }); + + it("should handle single agent-requested rule", () => { + const rules: RuleWithSource[] = [ + { + name: "Single Rule", + description: "The only agent-requested rule", + source: "rules-block", + rule: "Follow this guideline", + alwaysApply: false, + ruleFile: "/path/to/single-rule.md", + }, + ]; + + const result = getRequestRuleDescription(rules); + + const expected = + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n" + + "Single Rule: The only agent-requested rule"; + + expect(result).toBe(expected); + }); +}); diff --git a/core/tools/definitions/requestRule.ts b/core/tools/definitions/requestRule.ts new file mode 100644 index 000000000..cb60cda73 --- /dev/null +++ b/core/tools/definitions/requestRule.ts @@ -0,0 +1,52 @@ +import { ConfigDependentToolParams, GetTool } from "../.."; +import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; + +export interface RequestRuleArgs { + name: string; +} + +export function getRequestRuleDescription( + rules: ConfigDependentToolParams["rules"], +): string { + // Must be explicitly false and no globs + const agentRequestedRules = rules.filter( + (rule) => rule.alwaysApply === false && !rule.globs, + ); + + const prefix = + "Use this tool to select additional rules, specifically based on their descriptions. Available rules:\n"; + + const body = agentRequestedRules + .map((rule) => `${rule.name}: ${rule.description}`) + .join("\n"); + + if (body.length === 0) { + return prefix + "No rules available."; + } + + return prefix + body; +} + +export const requestRuleTool: GetTool = ({ rules }) => ({ + type: "function", + displayTitle: "Request Rules", + wouldLikeTo: "request rule {{{ name }}}", + isCurrently: "reading rule {{{ name }}}", + hasAlready: "read rule {{{ name }}}", + group: BUILT_IN_GROUP_NAME, + readonly: false, + function: { + name: BuiltInToolNames.RequestRule, + description: getRequestRuleDescription(rules), + parameters: { + type: "object", + required: ["name"], + properties: { + name: { + type: "string", + description: "Name of the rule", + }, + }, + }, + }, +}); diff --git a/core/tools/implementations/requestRule.ts b/core/tools/implementations/requestRule.ts new file mode 100644 index 000000000..86aff6280 --- /dev/null +++ b/core/tools/implementations/requestRule.ts @@ -0,0 +1,28 @@ +import { ToolImpl } from "."; +import { parseMarkdownRule } from "../../config/markdown/parseMarkdownRule"; + +export const requestRuleImpl: ToolImpl = async (args, extras) => { + // Find the rule by name in the config + const rule = extras.config.rules.find((r) => r.name === args.name); + + if (!rule || !rule.ruleFile) { + throw new Error( + `Rule with name "${args.name}" not found or has no file path`, + ); + } + + const fileContent = await extras.ide.readFile(rule.ruleFile); + const { markdown, frontmatter } = parseMarkdownRule(fileContent); + + return [ + { + name: frontmatter.name ?? "", + description: frontmatter.description ?? "", + content: markdown, + uri: { + type: "file", + value: rule.ruleFile, + }, + }, + ]; +}; diff --git a/core/tools/index.ts b/core/tools/index.ts index d89cf8744..f7ed3be4b 100644 --- a/core/tools/index.ts +++ b/core/tools/index.ts @@ -1,3 +1,4 @@ +import { ConfigDependentToolParams, Tool } from ".."; import { createNewFileTool } from "./definitions/createNewFile"; import { createRuleBlock } from "./definitions/createRuleBlock"; import { editFileTool } from "./definitions/editFile"; @@ -6,11 +7,12 @@ import { grepSearchTool } from "./definitions/grepSearch"; import { lsTool } from "./definitions/lsTool"; import { readCurrentlyOpenFileTool } from "./definitions/readCurrentlyOpenFile"; import { readFileTool } from "./definitions/readFile"; +import { requestRuleTool } from "./definitions/requestRule"; import { runTerminalCommandTool } from "./definitions/runTerminalCommand"; import { searchWebTool } from "./definitions/searchWeb"; import { viewDiffTool } from "./definitions/viewDiff"; -export const allTools = [ +export const baseToolDefinitions = [ readFileTool, editFileTool, createNewFileTool, @@ -26,3 +28,7 @@ export const allTools = [ // viewSubdirectoryTool, // viewRepoMapTool, ]; + +export const getConfigDependentToolDefinitions = ( + params: ConfigDependentToolParams, +): Tool[] => [requestRuleTool(params)]; diff --git a/gui/src/redux/slices/uiSlice.ts b/gui/src/redux/slices/uiSlice.ts index 4105d3319..b8961a0b1 100644 --- a/gui/src/redux/slices/uiSlice.ts +++ b/gui/src/redux/slices/uiSlice.ts @@ -56,6 +56,7 @@ export const uiSlice = createSlice({ [BuiltInToolNames.ViewDiff]: "allowedWithoutPermission", [BuiltInToolNames.LSTool]: "allowedWithoutPermission", [BuiltInToolNames.CreateRuleBlock]: "allowedWithPermission", + [BuiltInToolNames.RequestRule]: "disabled", }, toolGroupSettings: { [BUILT_IN_GROUP_NAME]: "include", diff --git a/gui/src/redux/thunks/callCurrentTool.ts b/gui/src/redux/thunks/callCurrentTool.ts index f6f408238..84270fdd8 100644 --- a/gui/src/redux/thunks/callCurrentTool.ts +++ b/gui/src/redux/thunks/callCurrentTool.ts @@ -1,6 +1,6 @@ import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; import { ContextItem } from "core"; -import { CLIENT_TOOLS } from "core/tools/builtIn"; +import { CLIENT_TOOLS_IMPLS } from "core/tools/builtIn"; import { callClientTool } from "../../util/clientTools/callClientTool"; import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall"; import { selectSelectedChatModel } from "../slices/configSlice"; @@ -51,7 +51,7 @@ export const callCurrentTool = createAsyncThunk( // Errors that occur outside specifically calling the tool // Should not be caught here - should be handled as normal stream errors if ( - CLIENT_TOOLS.find( + CLIENT_TOOLS_IMPLS.find( (toolName) => toolName === toolCallState.toolCall.function.name, ) ) {