-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: ensure only active tools are processed in base_chat_node.py #3982
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -279,7 +279,7 @@ def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers, | |
| mcp_servers_config = json.loads(mcp_servers) | ||
| elif mcp_tool_id: | ||
| mcp_tool = QuerySet(Tool).filter(id=mcp_tool_id).first() | ||
| if mcp_tool: | ||
| if mcp_tool and mcp_tool.is_active: | ||
| mcp_servers_config = json.loads(mcp_tool.code) | ||
|
|
||
| if tool_enable: | ||
|
|
@@ -288,6 +288,8 @@ def _handle_mcp_request(self, mcp_enable, tool_enable, mcp_source, mcp_servers, | |
| self.context['execute_ids'] = [] | ||
| for tool_id in tool_ids: | ||
| tool = QuerySet(Tool).filter(id=tool_id).first() | ||
| if not tool.is_active: | ||
| continue | ||
| executor = ToolExecutor(CONFIG.get('SANDBOX')) | ||
| if tool.init_params is not None: | ||
| params = json.loads(rsa_long_decrypt(tool.init_params)) | ||
|
Contributor
Author
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. The code looks good overall, but there's an unnecessary condition that checks if Additionally, consider converting conditional expressions into single-line ternary statements for clarity and readability: if not tool.is_active:
continueHere's how it could be rewritten with a ternary statement: self.context['execute_ids'] = [] or next((executor for _id, executor in enumerate(executors) if _id == idx), None)
executor = executor_executor CONFIG.get('SANDBOX')) if toolbar_init_params else None
tool_contexts = {tool.id(): context for tool in tools} if len(tools) > 0 else {}However, these changes are quite minor. The original code should work fine as it is. Here’s just one final tip from an optimization perspective: If you use more than once inside a loop, it might benefit from being defined outside (like Overall, the code is concise and clear despite the very small adjustments mentioned above. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| from django.core.cache import cache | ||
| from django.db.models import QuerySet | ||
| from django.utils.translation import gettext_lazy as _ | ||
| from django.utils import timezone | ||
|
|
||
| from application.chat_pipeline.step.chat_step.i_chat_step import PostResponseHandler | ||
| from application.models import Application, ChatRecord, Chat, ApplicationVersion, ChatUserType, ApplicationTypeChoices, \ | ||
|
|
@@ -213,7 +214,7 @@ def append_chat_record(self, chat_record: ChatRecord): | |
| chat_user_id=self.chat_user_id, chat_user_type=self.chat_user_type, | ||
| asker=self.get_chat_user()).save() | ||
| else: | ||
| QuerySet(Chat).filter(id=self.chat_id).update(update_time=datetime.now()) | ||
| QuerySet(Chat).filter(id=self.chat_id).update(update_time=timezone.now()) | ||
| # 插入会话记录 | ||
| chat_record.save() | ||
| ChatCountSerializer(data={'chat_id': self.chat_id}).update_chat() | ||
|
Contributor
Author
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. The code looks generally correct and well-structured according to the provided context. However, here's a few minor suggestions and optimizations:
These changes make the code slightly more robust and potentially more performant in certain scenarios depending on the load and environment it will run in. |
||
|
|
||
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.
The provided code has a few potential improvements:
Avoid Multiple Database Queries: The current implementation uses two database calls in a row (
QuerySet(Tool).filter(id=mcp_tool_id).first()andQuerySet(Tool).filter(id=tool_id).first()) when bothmcp_tool_idandtool_idsare non-empty. This can be optimized to reduce duplicate queries.Enhance Readability: Consider adding more descriptive variable names or comments around parts of the code to improve readability.
Check for Empty List: Ensure that
tool_idsis not an empty list before entering the loop, as it might lead to unnecessary operations.Use Python Collections: Instead of using raw strings for concatenation (like
'execute_ids': [],'self.context.update(...)'), use Python's collection literals like{}anddict()where appropriate.Here’s the improved version of the code:
Key Improvements:
id.tool_ids.