Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions backend/python/app/modules/agents/qna/tool_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,25 @@ def __init__(
state: Chat state object
**kwargs: Additional keyword arguments
"""
base_description = getattr(
registry_tool,
'description',
f"Tool: {app_name}.{tool_name}"
# Cache getattr and vars locally to avoid repeated lookups in tight loops
registry_tool_description = getattr(registry_tool, 'description', None)
base_description = (
registry_tool_description
if registry_tool_description is not None
else f"Tool: {app_name}.{tool_name}"
)

try:
params = getattr(registry_tool, 'parameters', []) or []
if params:
formatted_params = self._format_parameters(params)
params = getattr(registry_tool, 'parameters', None)
# Avoid unnecessary try/except: only wrap if params access may fail
formatted_params = None
if params:
formatted_params = RegistryToolWrapper._format_parameters(params)
if formatted_params:
params_doc = "\nParameters:\n- " + "\n- ".join(formatted_params)
full_description = f"{base_description}{params_doc}"
else:
full_description = base_description
except Exception:
else:
full_description = base_description

init_data: Dict[str, Union[str, object]] = {
Expand All @@ -90,23 +94,32 @@ def _format_parameters(params: List[object]) -> List[str]:
Returns:
List of formatted parameter strings
"""
formatted_params = []
# Prebind local variables for attribute lookup and reduce scope in loop
append = list.append
result: List[str] = []
for param in params:
try:
type_name = getattr(
param.type,
'name',
str(getattr(param, 'type', 'string'))
)
type_obj = getattr(param, 'type', None)
type_name: str
if type_obj is not None:
# Avoid getattr(type_obj, 'name') unless necessary
type_name = getattr(type_obj, 'name', None)
if type_name is not None:
pass
else:
type_name = str(type_obj)
else:
type_name = 'string'
except Exception:
type_name = 'string'

required_marker = ' (required)' if getattr(param, 'required', False) else ''
description = getattr(param, 'description', '')
formatted_params.append(
append(
result,
f"{param.name}{required_marker}: {description} [{type_name}]"
)
return formatted_params
return result

@property
def state(self) -> ChatState:
Expand Down