@@ -7,6 +7,7 @@ import { formatResponse } from "../prompts/responses"
77import { VectorStoreSearchResult } from "../../services/code-index/interfaces"
88import { AskApproval , HandleError , PushToolResult , RemoveClosingTag , ToolUse } from "../../shared/tools"
99import path from "path"
10+ import { ManagedIndexer } from "../../services/code-index/managed/ManagedIndexer"
1011
1112export async function codebaseSearchTool (
1213 cline : Task ,
@@ -62,6 +63,12 @@ export async function codebaseSearchTool(
6263
6364 cline . consecutiveMistakeCount = 0
6465
66+ // kilode_change start - First try mnaaged indexing
67+ if ( await tryManagedSearch ( cline , pushToolResult , query , directoryPrefix ) ) {
68+ return
69+ }
70+ // kilode_change end - First try mnaaged indexing
71+
6572 // --- Core Logic ---
6673 try {
6774 const context = cline . providerRef . deref ( ) ?. context
@@ -75,16 +82,12 @@ export async function codebaseSearchTool(
7582 throw new Error ( "CodeIndexManager is not available." )
7683 }
7784
78- // kilcode_change start
79- if ( ! manager . isManagedIndexingAvailable ) {
80- if ( ! manager . isFeatureEnabled ) {
81- throw new Error ( "Code Indexing is disabled in the settings." )
82- }
83- if ( ! manager . isFeatureConfigured ) {
84- throw new Error ( "Code Indexing is not configured (Missing OpenAI Key or Qdrant URL)." )
85- }
85+ if ( ! manager . isFeatureEnabled ) {
86+ throw new Error ( "Code Indexing is disabled in the settings." )
87+ }
88+ if ( ! manager . isFeatureConfigured ) {
89+ throw new Error ( "Code Indexing is not configured (Missing OpenAI Key or Qdrant URL)." )
8690 }
87- // kilcode_change end
8891
8992 // kilocode_change start
9093 const status = manager . getCurrentStatus ( )
@@ -198,3 +201,77 @@ ${result.codeChunk ? `Code Chunk: ${result.codeChunk}\n` : ""}`, // kilocode_cha
198201 await handleError ( toolName , error ) // Use the standard error handler
199202 }
200203}
204+
205+ // kilocode_change start - Add managed search block
206+ async function tryManagedSearch (
207+ cline : Task ,
208+ pushToolResult : PushToolResult ,
209+ query : string ,
210+ directoryPrefix ?: string ,
211+ ) : Promise < boolean > {
212+ try {
213+ const managed = ManagedIndexer . getInstance ( )
214+ if ( ! ( await managed . isEnabled ( ) ) ) {
215+ return false
216+ }
217+ const searchResults = await managed . search ( query , directoryPrefix )
218+ // 3. Format and push results
219+ if ( ! searchResults || searchResults . length === 0 ) {
220+ pushToolResult ( `No relevant code snippets found for the query: "${ query } "` ) // Use simple string for no results
221+ return true
222+ }
223+
224+ const jsonResult = {
225+ query,
226+ results : [ ] ,
227+ } as {
228+ query : string
229+ results : Array < {
230+ filePath : string
231+ score : number
232+ startLine : number
233+ endLine : number
234+ codeChunk : string
235+ } >
236+ }
237+
238+ searchResults . forEach ( ( result ) => {
239+ if ( ! result . payload ) return
240+ if ( ! ( "filePath" in result . payload ) ) return
241+
242+ const relativePath = vscode . workspace . asRelativePath ( result . payload . filePath , false )
243+
244+ jsonResult . results . push ( {
245+ filePath : relativePath ,
246+ score : result . score ,
247+ startLine : result . payload . startLine ,
248+ endLine : result . payload . endLine ,
249+ codeChunk : result . payload . codeChunk . trim ( ) ,
250+ } )
251+ } )
252+
253+ // Send results to UI
254+ const payload = { tool : "codebaseSearch" , content : jsonResult }
255+ await cline . say ( "codebase_search_result" , JSON . stringify ( payload ) )
256+
257+ // Push results to AI
258+ const output = `Query: ${ query }
259+ Results:
260+
261+ ${ jsonResult . results
262+ . map (
263+ ( result ) => `File path: ${ result . filePath }
264+ Score: ${ result . score }
265+ Lines: ${ result . startLine } -${ result . endLine }
266+ ${ result . codeChunk ? `Code Chunk: ${ result . codeChunk } \n` : "" } `, // kilocode_change - don't include code chunk managed indexing
267+ )
268+ . join ( "\n" ) } `
269+
270+ pushToolResult ( output )
271+ return true
272+ } catch ( e ) {
273+ console . log ( `[codebaseSearchTool]: Managed search failed with error: ${ e } ` )
274+ return false
275+ }
276+ }
277+ // kilocode_change end - Add managed search block
0 commit comments