Changesets · Branches · Tags · Compare
| Changeset | afb5df1f558f68d090beced11a9d14a77813d372b0b890d0bfe9899eeb12fd1b |
|---|---|
| Author | Ting-Wai To <tingwai.to@gmail.com> · 2025-06-30 18:01 -07:00 |
| Committer | Ting-Wai To <tingwai.to@gmail.com> · 2025-06-30 22:27 -07:00 |
| Parents | c9f65f59bfed |
5 files changed
modified core/tools/builtIn.ts +1 −0
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | CreateRuleBlock = "builtin_create_rule_block", |
| 14 | 14 | RequestRule = "builtin_request_rule", |
| 15 | 15 | FetchUrlContent = "builtin_fetch_url_content", |
| 16 | + CodebaseTool = "builtin_codebase", | |
| 16 | 17 | |
| 17 | 18 | // excluded from allTools for now |
| 18 | 19 | ViewRepoMap = "builtin_view_repo_map", |
modified core/tools/callTool.ts +3 −0
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | import { canParseUrl } from "../util/url"; |
| 4 | 4 | import { BuiltInToolNames } from "./builtIn"; |
| 5 | 5 | |
| 6 | +import { codebaseToolImpl } from "./implementations/codebaseTool"; | |
| 6 | 7 | import { createNewFileImpl } from "./implementations/createNewFile"; |
| 7 | 8 | import { createRuleBlockImpl } from "./implementations/createRuleBlock"; |
| 8 | 9 | import { fetchUrlContentImpl } from "./implementations/fetchUrlContent"; |
| @@ -164,6 +165,8 @@ | ||
| 164 | 165 | return await createRuleBlockImpl(args, extras); |
| 165 | 166 | case BuiltInToolNames.RequestRule: |
| 166 | 167 | return await requestRuleImpl(args, extras); |
| 168 | + case BuiltInToolNames.CodebaseTool: | |
| 169 | + return await codebaseToolImpl(args, extras); | |
| 167 | 170 | default: |
| 168 | 171 | throw new Error(`Tool "${functionName}" not found`); |
| 169 | 172 | } |
added core/tools/definitions/codebaseTool.ts +29 −0
| @@ -0,0 +1,29 @@ | ||
| 1 | +import { Tool } from "../.."; | |
| 2 | +import { BUILT_IN_GROUP_NAME, BuiltInToolNames } from "../builtIn"; | |
| 3 | + | |
| 4 | +export const codebaseTool: Tool = { | |
| 5 | + type: "function", | |
| 6 | + displayTitle: "Codebase Search", | |
| 7 | + wouldLikeTo: "search the codebase for: {{{ query }}}", | |
| 8 | + isCurrently: "searching the codebase for: {{{ query }}}", | |
| 9 | + hasAlready: "searched the codebase for: {{{ query }}}", | |
| 10 | + readonly: true, | |
| 11 | + isInstant: false, | |
| 12 | + group: BUILT_IN_GROUP_NAME, | |
| 13 | + function: { | |
| 14 | + name: BuiltInToolNames.CodebaseTool, | |
| 15 | + description: | |
| 16 | + "Use this tool to semantically search through the codebase and retrieve relevant code snippets based on a natural language query. This helps find relevant code context for understanding or working with the codebase.", | |
| 17 | + parameters: { | |
| 18 | + type: "object", | |
| 19 | + required: ["query"], | |
| 20 | + properties: { | |
| 21 | + query: { | |
| 22 | + type: "string", | |
| 23 | + description: | |
| 24 | + "Natural language description of what you're looking for in the codebase (e.g., 'authentication logic', 'database connection setup', 'error handling')", | |
| 25 | + }, | |
| 26 | + }, | |
| 27 | + }, | |
| 28 | + }, | |
| 29 | +}; |
added core/tools/implementations/codebaseTool.ts +61 −0
| @@ -0,0 +1,61 @@ | ||
| 1 | +import { ToolImpl } from "."; | |
| 2 | +import { RangeInFile } from "../.."; | |
| 3 | +import { retrieveContextItemsFromEmbeddings } from "../../context/retrieval/retrieval"; | |
| 4 | + | |
| 5 | +export const codebaseToolImpl: ToolImpl = async (args, extras) => { | |
| 6 | + if (!args.query || args.query.trim() === "") { | |
| 7 | + return [ | |
| 8 | + { | |
| 9 | + name: "Error", | |
| 10 | + description: "Codebase search error", | |
| 11 | + content: "Query parameter is required and cannot be empty.", | |
| 12 | + }, | |
| 13 | + ]; | |
| 14 | + } | |
| 15 | + | |
| 16 | + try { | |
| 17 | + const contextExtras = { | |
| 18 | + config: extras.config, | |
| 19 | + fullInput: args.query, | |
| 20 | + embeddingsProvider: extras.config.selectedModelByRole.embed, | |
| 21 | + reranker: extras.config.selectedModelByRole.rerank, | |
| 22 | + llm: extras.llm, | |
| 23 | + ide: extras.ide, | |
| 24 | + selectedCode: [] as RangeInFile[], | |
| 25 | + fetch: extras.fetch, | |
| 26 | + }; | |
| 27 | + | |
| 28 | + // Use the existing retrieval function to get context items | |
| 29 | + const results = await retrieveContextItemsFromEmbeddings( | |
| 30 | + contextExtras, | |
| 31 | + undefined, | |
| 32 | + undefined, | |
| 33 | + ); | |
| 34 | + | |
| 35 | + // If no results found, return helpful message | |
| 36 | + if (results.length === 0) { | |
| 37 | + return [ | |
| 38 | + { | |
| 39 | + name: "No Results", | |
| 40 | + description: "Codebase search", | |
| 41 | + content: `No relevant code found for query: "${args.query}". This could mean: | |
| 42 | +- The codebase hasn't been indexed yet | |
| 43 | +- No code matches the search criteria | |
| 44 | +- Embeddings provider is not configured | |
| 45 | + | |
| 46 | +Try re-indexing the codebase or using a more specific query.`, | |
| 47 | + }, | |
| 48 | + ]; | |
| 49 | + } | |
| 50 | + | |
| 51 | + return results; | |
| 52 | + } catch (error) { | |
| 53 | + return [ | |
| 54 | + { | |
| 55 | + name: "Error", | |
| 56 | + description: "Codebase search error", | |
| 57 | + content: `Failed to search codebase: ${error instanceof Error ? error.message : String(error)}`, | |
| 58 | + }, | |
| 59 | + ]; | |
| 60 | + } | |
| 61 | +}; |
modified core/tools/index.ts +7 −1
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import { ConfigDependentToolParams, IDE, Tool } from ".."; |
| 2 | +import { codebaseTool } from "./definitions/codebaseTool"; | |
| 2 | 3 | import { createNewFileTool } from "./definitions/createNewFile"; |
| 3 | 4 | import { createRuleBlock } from "./definitions/createRuleBlock"; |
| 4 | 5 | import { editFileTool } from "./definitions/editFile"; |
| @@ -37,7 +38,12 @@ | ||
| 37 | 38 | ): Tool[] => [ |
| 38 | 39 | requestRuleTool(params), |
| 39 | 40 | ...(params.enableExperimentalTools |
| 40 | - ? [searchAndReplaceInFileTool, viewRepoMapTool, viewSubdirectoryTool] | |
| 41 | + ? [ | |
| 42 | + searchAndReplaceInFileTool, | |
| 43 | + viewRepoMapTool, | |
| 44 | + viewSubdirectoryTool, | |
| 45 | + codebaseTool, | |
| 46 | + ] | |
| 41 | 47 | : [editFileTool]), |
| 42 | 48 | ]; |
| 43 | 49 |