Refactor modes

This commit is contained in:
Matt Rubens
2025-01-17 01:12:06 -05:00
parent b35206bc9d
commit 72fe12d096
39 changed files with 909 additions and 4310 deletions

View File

@@ -1,4 +1,9 @@
export function getAccessMcpResourceDescription(): string {
import { ToolArgs } from './types';
export function getAccessMcpResourceDescription(args: ToolArgs): string | undefined {
if (!args.mcpHub) {
return undefined;
}
return `## access_mcp_resource
Description: Request to access a resource provided by a connected MCP server. Resources represent data sources that can be used as context, such as files, API responses, or system information.
Parameters:

View File

@@ -1,9 +1,14 @@
export function getBrowserActionDescription(cwd: string, browserViewportSize: string = "900x600"): string {
import { ToolArgs } from './types';
export function getBrowserActionDescription(args: ToolArgs): string | undefined {
if (!args.supportsComputerUse) {
return undefined;
}
return `## browser_action
Description: Request to interact with a Puppeteer-controlled browser. Every action, except \`close\`, will be responded to with a screenshot of the browser's current state, along with any new console logs. You may only perform one browser action per message, and wait for the user's response including a screenshot and logs to determine the next action.
- The sequence of actions **must always start with** launching the browser at a URL, and **must always end with** closing the browser. If you need to visit a new URL that is not possible to navigate to from the current webpage, you must first close the browser, then launch again at the new URL.
- While the browser is active, only the \`browser_action\` tool can be used. No other tools should be called during this time. You may proceed to use other tools only after closing the browser. For example if you run into an error and need to fix a file, you must close the browser, then use other tools to make the necessary changes, then re-launch the browser to verify the result.
- The browser window has a resolution of **${browserViewportSize}** pixels. When performing any click actions, ensure the coordinates are within this resolution range.
- The browser window has a resolution of **${args.browserViewportSize}** pixels. When performing any click actions, ensure the coordinates are within this resolution range.
- Before clicking on any elements such as icons, links, or buttons, you must consult the provided screenshot of the page to determine the coordinates of the element. The click should be targeted at the **center of the element**, not on its edges.
Parameters:
- action: (required) The action to perform. The available actions are:
@@ -21,7 +26,7 @@ Parameters:
- Example: \`<action>close</action>\`
- url: (optional) Use this for providing the URL for the \`launch\` action.
* Example: <url>https://example.com</url>
- coordinate: (optional) The X and Y coordinates for the \`click\` action. Coordinates should be within the **${browserViewportSize}** resolution.
- coordinate: (optional) The X and Y coordinates for the \`click\` action. Coordinates should be within the **${args.browserViewportSize}** resolution.
* Example: <coordinate>450,300</coordinate>
- text: (optional) Use this for providing the text for the \`type\` action.
* Example: <text>Hello, world!</text>

View File

@@ -1,6 +1,8 @@
export function getExecuteCommandDescription(cwd: string): string {
import { ToolArgs } from './types';
export function getExecuteCommandDescription(args: ToolArgs): string | undefined {
return `## execute_command
Description: Request to execute a CLI command on the system. Use this when you need to perform system operations or run specific commands to accomplish any step in the user's task. You must tailor your command to the user's system and provide a clear explanation of what the command does. Prefer to execute complex CLI commands over creating executable scripts, as they are more flexible and easier to run. Commands will be executed in the current working directory: ${cwd}
Description: Request to execute a CLI command on the system. Use this when you need to perform system operations or run specific commands to accomplish any step in the user's task. You must tailor your command to the user's system and provide a clear explanation of what the command does. Prefer to execute complex CLI commands over creating executable scripts, as they are more flexible and easier to run. Commands will be executed in the current working directory: ${args.cwd}
Parameters:
- command: (required) The CLI command to execute. This should be valid for the current operating system. Ensure the command is properly formatted and does not contain any harmful instructions.
Usage:

View File

@@ -11,15 +11,24 @@ import { getUseMcpToolDescription } from './use-mcp-tool'
import { getAccessMcpResourceDescription } from './access-mcp-resource'
import { DiffStrategy } from '../../diff/DiffStrategy'
import { McpHub } from '../../../services/mcp/McpHub'
import { Mode, codeMode, askMode } from '../modes'
import { CODE_ALLOWED_TOOLS, READONLY_ALLOWED_TOOLS, ToolName, ReadOnlyToolName } from '../../tool-lists'
import { Mode, ToolName, getModeConfig, isToolAllowedForMode } from '../../../shared/modes'
import { ToolArgs } from './types'
type AllToolNames = ToolName | ReadOnlyToolName;
// Helper function to safely check if a tool is allowed
function hasAllowedTool(tools: readonly string[], tool: AllToolNames): boolean {
return tools.includes(tool);
}
// Map of tool names to their description functions
const toolDescriptionMap: Record<string, (args: ToolArgs) => string | undefined> = {
'execute_command': args => getExecuteCommandDescription(args),
'read_file': args => getReadFileDescription(args),
'write_to_file': args => getWriteToFileDescription(args),
'search_files': args => getSearchFilesDescription(args),
'list_files': args => getListFilesDescription(args),
'list_code_definition_names': args => getListCodeDefinitionNamesDescription(args),
'browser_action': args => getBrowserActionDescription(args),
'ask_followup_question': () => getAskFollowupQuestionDescription(),
'attempt_completion': () => getAttemptCompletionDescription(),
'use_mcp_tool': args => getUseMcpToolDescription(args),
'access_mcp_resource': args => getAccessMcpResourceDescription(args),
'apply_diff': args => args.diffStrategy ? args.diffStrategy.getToolDescription({ cwd: args.cwd, toolOptions: args.toolOptions }) : ''
};
export function getToolDescriptionsForMode(
mode: Mode,
@@ -29,63 +38,32 @@ export function getToolDescriptionsForMode(
browserViewportSize?: string,
mcpHub?: McpHub
): string {
const descriptions = []
const config = getModeConfig(mode);
const args: ToolArgs = {
cwd,
supportsComputerUse,
diffStrategy,
browserViewportSize,
mcpHub
};
const allowedTools = mode === codeMode ? CODE_ALLOWED_TOOLS : READONLY_ALLOWED_TOOLS;
// Core tools based on mode
if (hasAllowedTool(allowedTools, 'execute_command')) {
descriptions.push(getExecuteCommandDescription(cwd));
}
if (hasAllowedTool(allowedTools, 'read_file')) {
descriptions.push(getReadFileDescription(cwd));
}
if (hasAllowedTool(allowedTools, 'write_to_file')) {
descriptions.push(getWriteToFileDescription(cwd));
}
// Optional diff strategy
if (diffStrategy && hasAllowedTool(allowedTools, 'apply_diff')) {
descriptions.push(diffStrategy.getToolDescription(cwd));
}
// File operation tools
if (hasAllowedTool(allowedTools, 'search_files')) {
descriptions.push(getSearchFilesDescription(cwd));
}
if (hasAllowedTool(allowedTools, 'list_files')) {
descriptions.push(getListFilesDescription(cwd));
}
if (hasAllowedTool(allowedTools, 'list_code_definition_names')) {
descriptions.push(getListCodeDefinitionNamesDescription(cwd));
}
// Browser actions
if (supportsComputerUse && hasAllowedTool(allowedTools, 'browser_action')) {
descriptions.push(getBrowserActionDescription(cwd, browserViewportSize));
}
// Common tools at the end
if (hasAllowedTool(allowedTools, 'ask_followup_question')) {
descriptions.push(getAskFollowupQuestionDescription());
}
if (hasAllowedTool(allowedTools, 'attempt_completion')) {
descriptions.push(getAttemptCompletionDescription());
}
// MCP tools if available
if (mcpHub) {
if (hasAllowedTool(allowedTools, 'use_mcp_tool')) {
descriptions.push(getUseMcpToolDescription());
// Map tool descriptions in the exact order specified in the mode's tools array
const descriptions = config.tools.map(([toolName, toolOptions]) => {
const descriptionFn = toolDescriptionMap[toolName];
if (!descriptionFn || !isToolAllowedForMode(toolName as ToolName, mode)) {
return undefined;
}
if (hasAllowedTool(allowedTools, 'access_mcp_resource')) {
descriptions.push(getAccessMcpResourceDescription());
}
}
return `# Tools\n\n${descriptions.filter(Boolean).join('\n\n')}`
return descriptionFn({
...args,
toolOptions
});
});
return `# Tools\n\n${descriptions.filter(Boolean).join('\n\n')}`;
}
// Export individual description functions for backward compatibility
export {
getExecuteCommandDescription,
getReadFileDescription,

View File

@@ -1,8 +1,10 @@
export function getListCodeDefinitionNamesDescription(cwd: string): string {
import { ToolArgs } from './types';
export function getListCodeDefinitionNamesDescription(args: ToolArgs): string {
return `## list_code_definition_names
Description: Request to list definition names (classes, functions, methods, etc.) used in source code files at the top level of the specified directory. This tool provides insights into the codebase structure and important constructs, encapsulating high-level concepts and relationships that are crucial for understanding the overall architecture.
Parameters:
- path: (required) The path of the directory (relative to the current working directory ${cwd.toPosix()}) to list top level source code definitions for.
- path: (required) The path of the directory (relative to the current working directory ${args.cwd}) to list top level source code definitions for.
Usage:
<list_code_definition_names>
<path>Directory path here</path>

View File

@@ -1,8 +1,10 @@
export function getListFilesDescription(cwd: string): string {
import { ToolArgs } from './types';
export function getListFilesDescription(args: ToolArgs): string {
return `## list_files
Description: Request to list files and directories within the specified directory. If recursive is true, it will list all files and directories recursively. If recursive is false or not provided, it will only list the top-level contents. Do not use this tool to confirm the existence of files you may have created, as the user will let you know if the files were created successfully or not.
Parameters:
- path: (required) The path of the directory to list contents for (relative to the current working directory ${cwd.toPosix()})
- path: (required) The path of the directory to list contents for (relative to the current working directory ${args.cwd})
- recursive: (optional) Whether to list files recursively. Use true for recursive listing, false or omit for top-level only.
Usage:
<list_files>

View File

@@ -1,8 +1,10 @@
export function getReadFileDescription(cwd: string): string {
import { ToolArgs } from './types';
export function getReadFileDescription(args: ToolArgs): string {
return `## read_file
Description: Request to read the contents of a file at the specified path. Use this when you need to examine the contents of an existing file you do not know the contents of, for example to analyze code, review text files, or extract information from configuration files. The output includes line numbers prefixed to each line (e.g. "1 | const x = 1"), making it easier to reference specific lines when creating diffs or discussing code. Automatically extracts raw text from PDF and DOCX files. May not be suitable for other types of binary files, as it returns the raw content as a string.
Parameters:
- path: (required) The path of the file to read (relative to the current working directory ${cwd})
- path: (required) The path of the file to read (relative to the current working directory ${args.cwd})
Usage:
<read_file>
<path>File path here</path>

View File

@@ -1,8 +1,10 @@
export function getSearchFilesDescription(cwd: string): string {
import { ToolArgs } from './types';
export function getSearchFilesDescription(args: ToolArgs): string {
return `## search_files
Description: Request to perform a regex search across files in a specified directory, providing context-rich results. This tool searches for patterns or specific content across multiple files, displaying each match with encapsulating context.
Parameters:
- path: (required) The path of the directory to search in (relative to the current working directory ${cwd.toPosix()}). This directory will be recursively searched.
- path: (required) The path of the directory to search in (relative to the current working directory ${args.cwd}). This directory will be recursively searched.
- regex: (required) The regular expression pattern to search for. Uses Rust regex syntax.
- file_pattern: (optional) Glob pattern to filter files (e.g., '*.ts' for TypeScript files). If not provided, it will search all files (*).
Usage:

View File

@@ -0,0 +1,11 @@
import { DiffStrategy } from '../../diff/DiffStrategy'
import { McpHub } from '../../../services/mcp/McpHub'
export type ToolArgs = {
cwd: string;
supportsComputerUse: boolean;
diffStrategy?: DiffStrategy;
browserViewportSize?: string;
mcpHub?: McpHub;
toolOptions?: any;
};

View File

@@ -1,4 +1,9 @@
export function getUseMcpToolDescription(): string {
import { ToolArgs } from './types';
export function getUseMcpToolDescription(args: ToolArgs): string | undefined {
if (!args.mcpHub) {
return undefined;
}
return `## use_mcp_tool
Description: Request to use a tool provided by a connected MCP server. Each MCP server can provide multiple tools with different capabilities. Tools have defined input schemas that specify required and optional parameters.
Parameters:

View File

@@ -1,8 +1,10 @@
export function getWriteToFileDescription(cwd: string): string {
import { ToolArgs } from './types';
export function getWriteToFileDescription(args: ToolArgs): string {
return `## write_to_file
Description: Request to write full content to a file at the specified path. If the file exists, it will be overwritten with the provided content. If the file doesn't exist, it will be created. This tool will automatically create any directories needed to write the file.
Parameters:
- path: (required) The path of the file to write to (relative to the current working directory ${cwd.toPosix()})
- path: (required) The path of the file to write to (relative to the current working directory ${args.cwd})
- content: (required) The content to write to the file. ALWAYS provide the COMPLETE intended content of the file, without any truncation or omissions. You MUST include ALL parts of the file, even if they haven't been modified. Do NOT include the line numbers in the content though, just the actual content of the file.
- line_count: (required) The number of lines in the file. Make sure to compute this based on the actual content of the file, not the number of lines in the content you're providing.
Usage: