-
Notifications
You must be signed in to change notification settings - Fork 478
✨ Add Chinese localization for tool descriptions #2527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
geruihappy-creator
wants to merge
8
commits into
develop
Choose a base branch
from
gerui-bugfix
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ca9ed62
feat: add Chinese localization for tool descriptions
0cb51f8
feat: add init_param_descriptions with i18n for exa_search_tool
c842f96
fix: add services module mock to fix test_add_tool_field test
762019b
fix: fix TypeScript type error in ToolTestPanel.tsx
8a7cf06
test: add tests for get_local_tools_description_zh function
774a32b
test: add tests for get_local_tools_description_zh i18n function
f9c1dbe
test: add tests for add_tool_field description_zh i18n merge logic
8601b99
test: fix test imports for get_local_tools_description_zh
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,10 +82,15 @@ def get_local_tools() -> List[ToolInfo]: | |
| if param_name == "self" or param.default.exclude: | ||
| continue | ||
|
|
||
| # Get description in both languages | ||
| param_description = param.default.description if hasattr(param.default, 'description') else "" | ||
| param_description_zh = param.default.description_zh if hasattr(param.default, 'description_zh') else None | ||
|
|
||
| param_info = { | ||
| "type": python_type_to_json_schema(param.annotation), | ||
| "name": param_name, | ||
| "description": param.default.description | ||
| "description": param_description, | ||
| "description_zh": param_description_zh | ||
| } | ||
| if param.default.default is PydanticUndefined: | ||
| param_info["optional"] = False | ||
|
|
@@ -95,14 +100,29 @@ def get_local_tools() -> List[ToolInfo]: | |
|
|
||
| init_params_list.append(param_info) | ||
|
|
||
| # get tool fixed attributes | ||
| # Get tool fixed attributes with bilingual support | ||
| tool_description_zh = getattr(tool_class, 'description_zh', None) | ||
| tool_inputs = getattr(tool_class, 'inputs', {}) | ||
|
|
||
| # Process inputs to add bilingual descriptions | ||
| processed_inputs = {} | ||
| if isinstance(tool_inputs, dict): | ||
| for key, value in tool_inputs.items(): | ||
| if isinstance(value, dict): | ||
| processed_inputs[key] = { | ||
| **value, | ||
| "description_zh": value.get("description_zh") | ||
| } | ||
| else: | ||
| processed_inputs[key] = value | ||
|
|
||
| tool_info = ToolInfo( | ||
| name=getattr(tool_class, 'name'), | ||
| description=getattr(tool_class, 'description'), | ||
| description_zh=tool_description_zh, | ||
| params=init_params_list, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| source=ToolSourceEnum.LOCAL.value, | ||
| inputs=json.dumps(getattr(tool_class, 'inputs'), | ||
| ensure_ascii=False), | ||
| inputs=json.dumps(processed_inputs, ensure_ascii=False), | ||
| output_type=getattr(tool_class, 'output_type'), | ||
| category=getattr(tool_class, 'category'), | ||
| class_name=tool_class.__name__, | ||
|
|
@@ -113,6 +133,61 @@ def get_local_tools() -> List[ToolInfo]: | |
| return tools_info | ||
|
|
||
|
|
||
| def get_local_tools_description_zh() -> Dict[str, Dict]: | ||
| """ | ||
| Get description_zh for all local tools from SDK (not persisted to DB). | ||
|
|
||
| Returns: | ||
| Dict mapping tool name to {"description_zh": ..., "params": [...], "inputs": {...}} | ||
| """ | ||
| tools_classes = get_local_tools_classes() | ||
| result = {} | ||
| for tool_class in tools_classes: | ||
| tool_name = getattr(tool_class, 'name') | ||
|
|
||
| # Get tool-level description_zh | ||
| description_zh = getattr(tool_class, 'description_zh', None) | ||
|
|
||
| # Get class-level init_param_descriptions for fallback | ||
| init_param_descriptions = getattr(tool_class, 'init_param_descriptions', {}) | ||
|
|
||
| # Get param-level description_zh | ||
| init_params_list = [] | ||
| sig = inspect.signature(tool_class.__init__) | ||
| for param_name, param in sig.parameters.items(): | ||
| if param_name == "self" or param.default.exclude: | ||
| continue | ||
|
|
||
| # First try to get from param.default.description_zh (FieldInfo) | ||
| param_description_zh = param.default.description_zh if hasattr(param.default, 'description_zh') else None | ||
|
|
||
| # Fallback to init_param_descriptions if not found | ||
| if param_description_zh is None and param_name in init_param_descriptions: | ||
| param_description_zh = init_param_descriptions[param_name].get('description_zh') | ||
|
|
||
| init_params_list.append({ | ||
| "name": param_name, | ||
| "description_zh": param_description_zh | ||
| }) | ||
|
|
||
| # Get inputs description_zh | ||
| tool_inputs = getattr(tool_class, 'inputs', {}) | ||
| inputs_description_zh = {} | ||
| if isinstance(tool_inputs, dict): | ||
| for key, value in tool_inputs.items(): | ||
| if isinstance(value, dict) and value.get("description_zh"): | ||
| inputs_description_zh[key] = { | ||
| "description_zh": value.get("description_zh") | ||
| } | ||
|
|
||
| result[tool_name] = { | ||
| "description_zh": description_zh, | ||
| "params": init_params_list, | ||
| "inputs": inputs_description_zh | ||
| } | ||
| return result | ||
|
|
||
|
|
||
| def get_local_tools_classes() -> List[type]: | ||
| """ | ||
| Get all tool classes from the nexent.core.tools package | ||
|
|
@@ -371,20 +446,61 @@ async def list_all_tools(tenant_id: str): | |
| List all tools for a given tenant | ||
| """ | ||
| tools_info = query_all_tools(tenant_id) | ||
|
|
||
| # Get description_zh from SDK for local tools (not persisted to DB) | ||
| local_tool_descriptions = get_local_tools_description_zh() | ||
|
|
||
| # only return the fields needed | ||
| formatted_tools = [] | ||
| for tool in tools_info: | ||
| tool_name = tool.get("name") | ||
|
|
||
| # Merge description_zh from SDK for local tools | ||
| if tool.get("source") == "local" and tool_name in local_tool_descriptions: | ||
| sdk_info = local_tool_descriptions[tool_name] | ||
| description_zh = sdk_info.get("description_zh") | ||
|
|
||
| # Merge params description_zh from SDK (independent of tool-level description_zh) | ||
| params = tool.get("params", []) | ||
| if params: | ||
| for param in params: | ||
| if not param.get("description_zh"): | ||
| # Find matching param in SDK | ||
| for sdk_param in sdk_info.get("params", []): | ||
| if sdk_param.get("name") == param.get("name"): | ||
| param["description_zh"] = sdk_param.get("description_zh") | ||
| break | ||
|
|
||
| # Merge inputs description_zh from SDK | ||
| inputs_str = tool.get("inputs", "{}") | ||
| try: | ||
| inputs = json.loads(inputs_str) if isinstance(inputs_str, str) else inputs_str | ||
| if isinstance(inputs, dict): | ||
| for key, value in inputs.items(): | ||
| if isinstance(value, dict) and not value.get("description_zh"): | ||
| # Find matching input in SDK | ||
| sdk_inputs = sdk_info.get("inputs", {}) | ||
| if key in sdk_inputs: | ||
| value["description_zh"] = sdk_inputs[key].get("description_zh") | ||
| inputs_str = json.dumps(inputs, ensure_ascii=False) | ||
| except (json.JSONDecodeError, TypeError): | ||
| pass | ||
| else: | ||
| description_zh = tool.get("description_zh") | ||
| inputs_str = tool.get("inputs", "{}") | ||
|
|
||
| formatted_tool = { | ||
| "tool_id": tool.get("tool_id"), | ||
| "name": tool.get("name"), | ||
| "name": tool_name, | ||
| "origin_name": tool.get("origin_name"), | ||
| "description": tool.get("description"), | ||
| "description_zh": description_zh, | ||
| "source": tool.get("source"), | ||
| "is_available": tool.get("is_available"), | ||
| "create_time": tool.get("create_time"), | ||
| "usage": tool.get("usage"), | ||
| "params": tool.get("params", []), | ||
| "inputs": tool.get("inputs", {}), | ||
| "inputs": inputs_str, | ||
| "category": tool.get("category") | ||
| } | ||
| formatted_tools.append(formatted_tool) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import 建议放到文件最前方,集中管理。