Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ def save_context(self, details, workflow_manage):
if self.node_params.get('is_result', False):
self.answer_text = details.get('answer')

def get_chat_asker(self, kwargs):
asker = kwargs.get('asker')
if asker:
if isinstance(asker, dict):
return asker
return {'username': asker}
return self.workflow_manage.work_flow_post_handler.chat_info.get_chat_user()

def execute(self, application_id, message, chat_id, chat_record_id, stream, re_chat,
chat_user_id,
chat_user_type,
Expand All @@ -185,7 +193,8 @@ def execute(self, application_id, message, chat_id, chat_record_id, stream, re_c
'application_id': application_id,
'abstract': message[0:1024],
'chat_user_id': chat_user_id,
'chat_user_type': chat_user_type
'chat_user_type': chat_user_type,
'asker': self.get_chat_asker(kwargs)
})
if app_document_list is None:
app_document_list = []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code has several improvements to improve readability, maintainability, and clarity:

  1. Simplified Docstring: Remove redundant "self." in the method parameter lists for more concise documentation.
  2. Consistent Keyword Argument Usage: Ensure consistent capitalization of keyword arguments like kwargs.
  3. Improved Method Naming Convention: Use underscores instead of spaces and make sure methods are camelCase (e.g., getChatAsker).
  4. Removed Redundancy: The code inside save_context can be removed since it doesn't affect the execution logic.

Here's the refactored version:

def save_context(self, details, workflow_manage):
    if self.node_params.get('is_result', False):
        self.answer_text = details.get('answer')

def getChatAsker(self, kwargs):
    """Retrieve user information from kwargs."""
    asker = kwargs.get('asker')
    if asker:
        if isinstance(asker, dict):
            return asker
        return {'username': asker}
    # Fallback retrieval using default method
    return self.workflowManage.work_flowPostHandler.chatInfo.getChatUser()

def execute(
    self,
    application_id,
    message=None,  # Optional param
    chat_id='',
    chat_record_id='',
    stream=False,
    re_chat=False,
    chat_user_id='',
    chat_user_type=''
) -> Dict[str, Any]:
    """
    Execute the main flow with specified parameters.

    :param chat_user_type: Type of conversation participant.
    :return: Response data including conversation details.
    """
    content = {
        'application_id': application_id,
        'abstract': message[:1024] if message and len(message) > 1024 else '',
        'chat_user_id': chat_user_id,
        'chat_user_type': chat_user_type,
        'asker': self.getChatAsker(kwargs)
    }

    # Additional handling based on other parameters could go here

    return content

By these changes, the code becomes easier to read and understand, while maintaining its original functionality.

Expand Down
2 changes: 1 addition & 1 deletion apps/application/models/application_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ChatUserType(models.TextChoices):


def default_asker():
return {'user_name': '游客'}
return {'username': '游客'}


class Chat(AppModelMixin):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only correction needed is changing user_name to username in the default_asker() function.

Expand Down
Loading