Skip to content
Merged
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 @@ -226,11 +226,14 @@ class BaseLoopNode(ILoopNode):
def save_context(self, details, workflow_manage):
self.context['loop_context_data'] = details.get('loop_context_data')
self.context['loop_answer_data'] = details.get('loop_answer_data')
self.context['loop_node_data'] = details.get('loop_node_data')
self.context['result'] = details.get('result')
self.context['params'] = details.get('params')
self.context['run_time'] = details.get('run_time')
self.context['index'] = details.get('current_index')
self.context['item'] = details.get('current_item')
for key, value in (details.get('loop_context_data') or {}).items():
self.context[key] = value
self.answer_text = ""

def get_answer_list(self) -> List[Answer] | None:
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 code you provided contains several minor improvements and optimizations. Here’s a revised version with comments:

class BaseLoopNode(ILoopNode):
    def save_context(self, details, workflow_manage: WorkflowManager):
        # Save loop-specific context data
        if 'loop_context_data' in details:
            self.context['loop_context_data'] = copy.deepcopy(details['loop_context_data'])
        
        # Ensure loop answer data is saved safely
        if 'loop_answer_data' in details:
            self.context['loop_answer_data'] = safe_copy(data)

        # Add additional loop-related context data
        if 'loop_node_data' in details:
            self.context['loop_node_data'] = safe_copy(details['loop_node_data'])

        # Save other common execution context variables
        self.context.update({
            'result': details.get('result'),
            'params': details.get('params'),
            'run_time': details.get('run_time'),
            'index': details.get('current_index', 0),
            'item': details.get('current_item')
        })

    def get_answer_list(self) -> List[Answer] | None:

Key Improvements and Optimizations:

  1. Deep Copy for loop_context_data:

    if 'loop_context_data' in details:
        self.context['loop_context_data'] = copy.deepcopy(details['loop_context_data'])

    Ensures that changes to this data do not affect the original structure.

  2. Use of copy.deepcopy() for Safeguarding Data Structure:

    import copy
    
    def safe_copy(data):
        try:
            return copy.deepcopy(data)
        except Exception as e:
            print(f"Safe copy failed due to {e}, returning None.")
            return None

    A helper function to handle deepcopy gracefully and avoid exceptions during deep copying.

  3. Updating Context Directly:

    self.context.update({
        ...
        'index': details.get('current_index', 0),  # Default index to 0 if not existent
        ...
    })

    Updates multiple context fields at once using update(), making the code cleaner and more efficient.

These enhancements help ensure the robustness and accuracy of the context handling logic within the class.

Expand Down
Loading