Refactor viewSourceCodeDefinitionsTopLevel to listCodeDefinitionNames

This commit is contained in:
Saoud Rizwan
2024-08-29 20:53:11 -04:00
parent 2e93928c8f
commit a0f89959a4
5 changed files with 26 additions and 21 deletions

View File

@@ -31,18 +31,18 @@ Claude Dev has access to the following capabilities:
1. **`execute_command`**: Execute terminal commands on the system (only with your permission, output is streamed into the chat and you can respond to stdin or exit long-running processes when you're ready)
2. **`read_file`**: Read the contents of a file at the specified path
3. **`write_to_file`**: Write content to a file at the specified path, automatically creating any necessary directories
4. **`view_source_code_definitions_top_level`**: Parses all source code files at the top level of the specified directory to extract names of key elements like classes and functions (see more below)
5. **`list_files`**: List all paths for files in the specified directory. When `recursive = true`, it recursively lists all files in the directory and its nested folders (excludes files in .gitignore). When `recursive = false`, it lists only top-level files (useful for generic file operations like retrieving a file from your Desktop).
4. **`list_files`**: List all paths for files in the specified directory. When `recursive = true`, it recursively lists all files in the directory and its nested folders (excludes files in .gitignore). When `recursive = false`, it lists only top-level files (useful for generic file operations like retrieving a file from your Desktop).
5. **`list_code_definition_names`**: Parses all source code files at the top level of the specified directory to extract names of key elements like classes and functions (see more below)
6. **`ask_followup_question`**: Ask the user a question to gather additional information needed to complete a task (due to the autonomous nature of the program, this isn't a typical chatbotClaude Dev must explicitly interrupt his task loop to ask for more information)
7. **`attempt_completion`**: Present the result to the user after completing a task, potentially with a terminal command to kickoff a demonstration
### Working in Existing Projects
When given a task in an existing project, Claude will look for the most relevant files to read and edit the same way you or I wouldby first looking at the names of directories, files, classes, and functions since these names tend to reflect their purpose and role within the broader system, and often encapsulate high-level concepts and relationships that help understand a project's overall architecture. With tools like `view_source_code_definitions_top_level`, Claude is able to extract names of various elements in a project to determine what files are most relevant to a given task without you having to mention `@file`s or `@folder`s yourself.
When given a task in an existing project, Claude will look for the most relevant files to read and edit the same way you or I wouldby first looking at the names of directories, files, classes, and functions since these names tend to reflect their purpose and role within the broader system, and often encapsulate high-level concepts and relationships that help understand a project's overall architecture. With tools like `list_code_definition_names`, Claude is able to extract names of various elements in a project to determine what files are most relevant to a given task without you having to mention `@file`s or `@folder`s yourself.
1. **File Structure**: When a task is started, Claude is given an overview of your project's file structure. It turns out Claude 3.5 Sonnet is _really_ good at inferring what it needs to process further just from these file names alone.
2. **Source Code Definitions**: Claude may then use the `view_source_code_definitions_top_level` tool on specific directories of interest. This tool uses [tree-sitter](https://github.com/tree-sitter/tree-sitter) to parse source code with custom tag queries that extract names of classes, functions, methods, and other definitions. It works by first identifying source code files that tree-sitter can parse (currently supports `python`, `javascript`, `typescript`, `ruby`, `go`, `java`, `php`, `rust`, `c`, `c++`, `c#`, `swift`), then parsing each file into an abstract syntax tree, and finally applying a language-specific query to extract definition names (you can see the exact query used for each language in `src/parse-source-code/queries`). The results are formatted into a concise & readable output that Claude can easily interpret to quickly understand the code's structure and purpose.
2. **Source Code Definitions**: Claude may then use the `list_code_definition_names` tool on specific directories of interest. This tool uses [tree-sitter](https://github.com/tree-sitter/tree-sitter) to parse source code with custom tag queries that extract names of classes, functions, methods, and other definitions. It works by first identifying source code files that tree-sitter can parse (currently supports `python`, `javascript`, `typescript`, `ruby`, `go`, `java`, `php`, `rust`, `c`, `c++`, `c#`, `swift`), then parsing each file into an abstract syntax tree, and finally applying a language-specific query to extract definition names (you can see the exact query used for each language in `src/parse-source-code/queries`). The results are formatted into a concise & readable output that Claude can easily interpret to quickly understand the code's structure and purpose.
3. **Read Relevant Files**: With insights gained from the names of various files and source code definitions, Claude can then use the `read_file` tool to examine specific files that are most relevant to the task at hand.

View File

@@ -38,8 +38,8 @@ CAPABILITIES
- You can debug complex issues and providing detailed explanations, offering architectural insights and design patterns.
- You have access to tools that let you execute CLI commands on the user's computer, list files in a directory (top level or recursively), extract source code definitions, read and write files, and ask follow-up questions. These tools help you effectively accomplish a wide range of tasks, such as writing code, making edits or improvements to existing files, understanding the current state of a project, performing system operations, and much more.
- When the user initially gives you a task, a recursive list of all filepaths in the current working directory ('${cwd}') will be included in potentially_relevant_details. This provides an overview of the project's file structure, offering key insights into the project from directory/file names (how developers conceptualize and organize their code) and file extensions (the language used). This can also guide decision-making on which files to explore further. If you need to further explore directories such as outside the current working directory, you can use the list_files tool. If you pass 'true' for the recursive parameter, it will list files recursively. Otherwise, it will list files at the top level, which is better suited for generic directories where you don't necessarily need the nested structure, like the Desktop.
- You can use the view_source_code_definitions_top_level tool to get an overview of source code definitions for all files at the top level of a specified directory. This can be particularly useful when you need to understand the broader context and relationships between certain parts of the code. You may need to call this tool multiple times to understand various parts of the codebase related to the task.
- For example, when asked to make edits or improvements you might analyze the file structure in the initial potentially_relevant_details to get an overview of the project, then use view_source_code_definitions_top_level to get further insight using source code definitions for files located in relevant directories, then read_file to examine the contents of relevant files, analyze the code and suggest improvements or make necessary edits, then use the write_to_file tool to implement changes.
- You can use the list_code_definition_names tool to get an overview of source code definitions for all files at the top level of a specified directory. This can be particularly useful when you need to understand the broader context and relationships between certain parts of the code. You may need to call this tool multiple times to understand various parts of the codebase related to the task.
- For example, when asked to make edits or improvements you might analyze the file structure in the initial potentially_relevant_details to get an overview of the project, then use list_code_definition_names to get further insight using source code definitions for files located in relevant directories, then read_file to examine the contents of relevant files, analyze the code and suggest improvements or make necessary edits, then use the write_to_file tool to implement changes.
- The execute_command tool lets you run commands on the user's computer and should be used whenever you feel it can help accomplish the user's task. When you need to execute a CLI command, you must provide a clear explanation of what the command does. Prefer to execute complex CLI commands over creating executable scripts, since they are more flexible and easier to run. Interactive and long-running commands are allowed, since the user has the ability to send input to stdin and terminate the command on their own if needed.
====
@@ -127,15 +127,15 @@ const tools: Tool[] = [
},
},
{
name: "view_source_code_definitions_top_level",
name: "list_code_definition_names",
description:
"Parse all source code files at the top level of the specified directory to extract names of key elements like classes and functions. 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.",
"Lists 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.",
input_schema: {
type: "object",
properties: {
path: {
type: "string",
description: `The path of the directory (relative to the current working directory ${cwd}) to parse top level source code files for to view their definitions`,
description: `The path of the directory (relative to the current working directory ${cwd}) to list top level source code definitions for`,
},
},
required: ["path"],
@@ -684,8 +684,8 @@ export class ClaudeDev {
return this.readFile(toolInput.path)
case "list_files":
return this.listFiles(toolInput.path, toolInput.recursive)
case "view_source_code_definitions_top_level":
return this.viewSourceCodeDefinitionsTopLevel(toolInput.path)
case "list_code_definition_names":
return this.listCodeDefinitionNames(toolInput.path)
case "execute_command":
return this.executeCommand(toolInput.command)
case "ask_followup_question":
@@ -998,11 +998,11 @@ export class ClaudeDev {
}
}
async viewSourceCodeDefinitionsTopLevel(relDirPath?: string): Promise<ToolResponse> {
async listCodeDefinitionNames(relDirPath?: string): Promise<ToolResponse> {
if (relDirPath === undefined) {
await this.say(
"error",
"Claude tried to use view_source_code_definitions_top_level without value for required parameter 'path'. Retrying..."
"Claude tried to use list_code_definition_names without value for required parameter 'path'. Retrying..."
)
return "Error: Missing value for required parameter 'path'. Please retry with complete response."
}
@@ -1011,7 +1011,7 @@ export class ClaudeDev {
const result = await parseSourceCodeForDefinitionsTopLevel(absolutePath)
const message = JSON.stringify({
tool: "viewSourceCodeDefinitionsTopLevel",
tool: "listCodeDefinitionNames",
path: this.getReadablePath(relDirPath),
content: result,
} as ClaudeSayTool)
@@ -1433,9 +1433,14 @@ ${
`
if (verbose) {
const files = await listFiles(cwd, true)
const isDesktop = cwd === path.join(os.homedir(), "Desktop")
const files = await listFiles(cwd, !isDesktop)
const result = this.formatFilesList(cwd, files)
details += `\n# Current Working Directory Project Structure:\n${result}\n`
details += `\n# Current Working Directory ('${cwd}') File Structure:${
isDesktop
? "\n(Desktop so only top-level contents shown for brevity, use list_files to explore further if necessary)"
: ""
}:\n${result}\n`
}
details += "</potentially_relevant_details>"

View File

@@ -64,7 +64,7 @@ export interface ClaudeSayTool {
| "readFile"
| "listFilesTopLevel"
| "listFilesRecursive"
| "viewSourceCodeDefinitionsTopLevel"
| "listCodeDefinitionNames"
path?: string
diff?: string
content?: string

View File

@@ -4,7 +4,7 @@ export type ToolName =
| "write_to_file"
| "read_file"
| "list_files"
| "view_source_code_definitions_top_level"
| "list_code_definition_names"
| "execute_command"
| "ask_followup_question"
| "attempt_completion"

View File

@@ -571,15 +571,15 @@ const ChatRow: React.FC<ChatRowProps> = ({
/>
</>
)
case "viewSourceCodeDefinitionsTopLevel":
case "listCodeDefinitionNames":
return (
<>
<div style={headerStyle}>
{toolIcon("file-code")}
<span style={{ fontWeight: "bold" }}>
{message.type === "ask"
? "Claude wants to view source code definitions in files at the top level of this directory:"
: "Claude viewed source code definitions in files at the top level of this directory:"}
? "Claude wants to view source code definition names used in this directory:"
: "Claude viewed source code definition names used in this directory:"}
</span>
</div>
<CodeBlock