| 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 | isInAgentMode: true, // always true in tool call |
| 27 | }; |
| 28 | |
| 29 | // Use the existing retrieval function to get context items |
| 30 | const results = await retrieveContextItemsFromEmbeddings( |
| 31 | contextExtras, |
| 32 | undefined, |
| 33 | undefined, |
| 34 | ); |
| 35 | |
| 36 | // If no results found, return helpful message |
| 37 | if (results.length === 0) { |
| 38 | return [ |
| 39 | { |
| 40 | name: "No Results", |
| 41 | description: "Codebase search", |
| 42 | content: `No relevant code found for query: "${args.query}". This could mean: |
| 43 | - The codebase hasn't been indexed yet |
| 44 | - No code matches the search criteria |
| 45 | - Embeddings provider is not configured |
| 46 | |
| 47 | Try re-indexing the codebase or using a more specific query.`, |
| 48 | }, |
| 49 | ]; |
| 50 | } |
| 51 | |
| 52 | return results; |
| 53 | } catch (error) { |
| 54 | return [ |
| 55 | { |
| 56 | name: "Error", |
| 57 | description: "Codebase search error", |
| 58 | content: `Failed to search codebase: ${error instanceof Error ? error.message : String(error)}`, |
| 59 | }, |
| 60 | ]; |
| 61 | } |
| 62 | }; |