@@ -312,6 +312,9 @@ def handle_tool_call(self, name: str, arguments: dict[str, Any]) -> str:
312312 Returns:
313313 JSON string result.
314314 """
315+ if not isinstance (arguments , dict ):
316+ return json .dumps ({"error" : "arguments must be a dictionary" })
317+
315318 try :
316319 from knowcode .telemetry import log_event
317320 log_event (
@@ -322,37 +325,47 @@ def handle_tool_call(self, name: str, arguments: dict[str, Any]) -> str:
322325 "arguments" : arguments ,
323326 }
324327 )
325- except Exception as e :
328+ except OSError as e :
326329 import logging
327- logging .getLogger (__name__ ).warning ("Ignored exception : %s" , e )
330+ logging .getLogger (__name__ ).warning ("Ignored telemetry OS error : %s" , e )
328331
329332 try :
330333 if name == "search_codebase" :
331-
334+ if "query" not in arguments or not isinstance (arguments ["query" ], str ):
335+ raise ValueError ("search_codebase requires 'query' as a string" )
336+ limit = arguments .get ("limit" , 10 )
337+ if not isinstance (limit , int ):
338+ raise ValueError ("'limit' must be an integer" )
332339 result = self .search_codebase (
333340 query = arguments ["query" ],
334- limit = arguments . get ( " limit" , 10 ) ,
341+ limit = limit ,
335342 )
336343 elif name == "get_entity_context" :
344+ if "entity_id" not in arguments or not isinstance (arguments ["entity_id" ], str ):
345+ raise ValueError ("get_entity_context requires 'entity_id' as a string" )
337346 result = self .get_entity_context ( # type: ignore
338347 entity_id = arguments ["entity_id" ],
339- task_type = arguments .get ("task_type" , "general" ),
340- max_tokens = arguments .get ("max_tokens" , 2000 ),
348+ task_type = str ( arguments .get ("task_type" , "general" ) ),
349+ max_tokens = int ( arguments .get ("max_tokens" , 2000 ) ),
341350 )
342351 elif name == "trace_calls" :
352+ if "entity_id" not in arguments or not isinstance (arguments ["entity_id" ], str ):
353+ raise ValueError ("trace_calls requires 'entity_id' as a string" )
343354 result = self .trace_calls (
344355 entity_id = arguments ["entity_id" ],
345- direction = arguments .get ("direction" , "callees" ),
346- depth = arguments .get ("depth" , 1 ),
356+ direction = str ( arguments .get ("direction" , "callees" ) ),
357+ depth = int ( arguments .get ("depth" , 1 ) ),
347358 )
348359 elif name == "retrieve_context_for_query" :
360+ if "query" not in arguments or not isinstance (arguments ["query" ], str ):
361+ raise ValueError ("retrieve_context_for_query requires 'query' as a string" )
349362 result = self .retrieve_context_for_query ( # type: ignore
350363 query = arguments ["query" ],
351- task_type = arguments .get ("task_type" , "auto" ),
352- max_tokens = arguments .get ("max_tokens" , 4000 ),
353- limit_entities = arguments .get ("limit_entities" , 3 ),
354- expand_deps = arguments .get ("expand_deps" , True ),
355- verbosity = arguments .get ("verbosity" , "minimal" ),
364+ task_type = str ( arguments .get ("task_type" , "auto" ) ),
365+ max_tokens = int ( arguments .get ("max_tokens" , 4000 ) ),
366+ limit_entities = int ( arguments .get ("limit_entities" , 3 ) ),
367+ expand_deps = bool ( arguments .get ("expand_deps" , True ) ),
368+ verbosity = str ( arguments .get ("verbosity" , "minimal" ) ),
356369 )
357370 else :
358371 result = {"error" : f"Unknown tool: { name } " } # type: ignore
@@ -363,6 +376,8 @@ def handle_tool_call(self, name: str, arguments: dict[str, Any]) -> str:
363376 {"error" : str (e ), "code" : e .code , "hint" : e .hint },
364377 separators = ("," , ":" ),
365378 )
379+ except ValueError as e :
380+ return json .dumps ({"error" : f"Validation Error: { e } " }, separators = (',' , ':' ))
366381 except Exception as e :
367382 return json .dumps ({"error" : str (e )}, separators = (',' , ':' ))
368383
0 commit comments