1212
1313logger = logging .getLogger (__name__ )
1414
15- from api_client import api_get , api_post
15+ from api_client import api_get , api_post , api_delete
1616from formatters import (
1717 format_codebase_dna ,
1818 format_code_style ,
@@ -79,6 +79,99 @@ async def _handle_dna(args: dict[str, Any]) -> str:
7979 return format_codebase_dna (result )
8080
8181
82+ # --- Write tool handlers ---
83+
84+ async def _handle_add_repository (args : dict [str , Any ]) -> str :
85+ payload = {
86+ "name" : args ["name" ],
87+ "git_url" : args ["git_url" ],
88+ "branch" : args .get ("branch" , "main" ),
89+ }
90+ result = await api_post ("/repos" , json = payload )
91+ repo_id = result .get ("repo_id" , "unknown" )
92+ name = result .get ("name" , args ["name" ])
93+ status = result .get ("status" , "added" )
94+ needs_selection = result .get ("needs_directory_selection" , False )
95+ lines = [
96+ f"Repository '{ name } ' added successfully." ,
97+ f"ID: `{ repo_id } `" ,
98+ f"Status: { status } " ,
99+ ]
100+ if needs_selection :
101+ lines .append (
102+ "\n This repo may benefit from subset indexing. "
103+ "Use get_repo_directories to see available directories, "
104+ "then index_repository with include_paths."
105+ )
106+ else :
107+ lines .append (
108+ f"\n Ready to index. Run: index_repository(repo_id='{ repo_id } ')"
109+ )
110+ return "\n " .join (lines )
111+
112+
113+ async def _handle_get_repo_directories (args : dict [str , Any ]) -> str :
114+ result = await api_get (f"/repos/{ args ['repo_id' ]} /directories" )
115+ dirs = result .get ("directories" , [])
116+ if not dirs :
117+ return "No directories found (repo may be flat or not yet cloned)."
118+ lines = ["# Repository Directories\n " ]
119+ for d in dirs :
120+ name = d .get ("name" , d .get ("path" , "unknown" ))
121+ count = d .get ("file_count" , 0 )
122+ lines .append (f"- **{ name } /** -- { count } code files" )
123+ lines .append (
124+ "\n To index specific directories, use index_repository "
125+ "with include_paths=['dir1', 'dir2']."
126+ )
127+ return "\n " .join (lines )
128+
129+
130+ async def _handle_index_repository (args : dict [str , Any ]) -> str :
131+ repo_id = args ["repo_id" ]
132+ include_paths = args .get ("include_paths" )
133+
134+ if include_paths is not None and len (include_paths ) == 0 :
135+ return "Error: include_paths cannot be empty. Omit it to index the full repo, or provide directory names."
136+
137+ if include_paths :
138+ # Async endpoint supports include_paths for monorepo subset indexing
139+ result = await api_post (
140+ f"/repos/{ repo_id } /index/async" ,
141+ json = {"include_paths" : include_paths },
142+ )
143+ status = result .get ("status" , "accepted" )
144+ return (
145+ f"Async indexing started for subset: { ', ' .join (include_paths )} \n "
146+ f"Status: { status } \n "
147+ f"Repo ID: `{ repo_id } `\n "
148+ "\n Indexing runs in the background. Use list_repositories "
149+ "to check when status changes to 'indexed'."
150+ )
151+
152+ # Sync endpoint for full-repo indexing
153+ result = await api_post (f"/repos/{ repo_id } /index" , json = {})
154+ status = result .get ("status" , "unknown" )
155+ fn_count = result .get ("functions" , 0 )
156+ lines = [
157+ "Indexing complete." ,
158+ f"Status: { status } " ,
159+ f"Functions extracted: { fn_count } " ,
160+ ]
161+ lines .append (
162+ f"\n You can now use search_code(repo_id='{ repo_id } ') "
163+ "to search this codebase."
164+ )
165+ return "\n " .join (lines )
166+
167+
168+ async def _handle_delete_repository (args : dict [str , Any ]) -> str :
169+ repo_id = args ["repo_id" ]
170+ result = await api_delete (f"/repos/{ repo_id } " )
171+ msg = result .get ("message" , "Repository deleted." )
172+ return f"{ msg } \n Repo ID `{ repo_id } ` has been removed."
173+
174+
82175# Tool name -> handler mapping
83176_HANDLERS : dict [str , Any ] = {
84177 "search_code" : _handle_search ,
@@ -88,6 +181,10 @@ async def _handle_dna(args: dict[str, Any]) -> str:
88181 "analyze_impact" : _handle_impact ,
89182 "get_repository_insights" : _handle_insights ,
90183 "get_codebase_dna" : _handle_dna ,
184+ "add_repository" : _handle_add_repository ,
185+ "get_repo_directories" : _handle_get_repo_directories ,
186+ "index_repository" : _handle_index_repository ,
187+ "delete_repository" : _handle_delete_repository ,
91188}
92189
93190
0 commit comments