Merge pull request #5867 from continuedev/pe/agent-requested-rule

feat: `requestRule` tool
This commit is contained in:
Patrick Erichsen 2025-05-29 09:28:23 -07:00 • committed by GitHub
commit fe5bcfe367
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 372 additions and 8 deletions

View file

@ -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: {

View file

@ -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<string, number> = {};
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<string, number> = {};
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());

View file

@ -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: {

View file

@ -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) =>

7
core/index.d.ts vendored
View file

@ -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;

View file

@ -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];

View file

@ -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`);
}

View file

@ -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);
});
});

View file

@ -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",
},
},
},
},
});

View file

@ -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,
},
},
];
};

View file

@ -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)];

View file

@ -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",

View file

@ -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<void, undefined, ThunkApiType>(
// 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,
)
) {