| 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 | throw new Error("`query` parameter is required and cannot be empty."); |
| 8 | } |
| 9 | |
| 10 | try { |
| 11 | const contextExtras = { |
| 12 | config: extras.config, |
| 13 | fullInput: args.query, |
| 14 | embeddingsProvider: extras.config.selectedModelByRole.embed, |
| 15 | reranker: extras.config.selectedModelByRole.rerank, |
| 16 | llm: extras.llm, |
| 17 | ide: extras.ide, |
| 18 | selectedCode: [] as RangeInFile[], |
| 19 | fetch: extras.fetch, |
| 20 | isInAgentMode: true, // always true in tool call |
| 21 | }; |
| 22 | |
| 23 | // Use the existing retrieval function to get context items |
| 24 | const results = await retrieveContextItemsFromEmbeddings( |
| 25 | contextExtras, |
| 26 | undefined, |
| 27 | undefined, |
| 28 | ); |
| 29 | |
| 30 | // If no results found, return helpful message |
| 31 | if (results.length === 0) { |
| 32 | return [ |
| 33 | { |
| 34 | name: "No Results", |
| 35 | description: "Codebase search", |
| 36 | content: `No relevant code found for query: "${args.query}". This could mean: |
| 37 | - The codebase hasn't been indexed yet |
| 38 | - No code matches the search criteria |
| 39 | - Embeddings provider is not configured |
| 40 | |
| 41 | Try re-indexing the codebase or using a more specific query.`, |
| 42 | }, |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | return results; |
| 47 | } catch (error) { |
| 48 | return [ |
| 49 | { |
| 50 | name: "Error", |
| 51 | description: "Codebase search error", |
| 52 | content: `Failed to search codebase: ${error instanceof Error ? error.message : String(error)}`, |
| 53 | }, |
| 54 | ]; |
| 55 | } |
| 56 | }; |