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
27 changes: 25 additions & 2 deletions apps/application/flow/i_step_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,36 @@ def __init__(self, chat_info, knowledge_action_id):
self.knowledge_action_id = knowledge_action_id

def handler(self, workflow):
err = [True for key, value in workflow.get_runtime_details().items() if value.get('status') == 500]
state = get_workflow_state(workflow)
QuerySet(KnowledgeAction).filter(id=self.knowledge_action_id).update(
state=State.FAILURE if any(err) else State.SUCCESS,
state=state,
run_time=time.time() - workflow.context.get('start_time') if workflow.context.get(
'start_time') is not None else 0)


def get_loop_workflow_node(node_list):
result = []
for item in node_list:
if item.get('type') == 'loop-node':
for loop_item in item.get('loop_node_data') or []:
for inner_item in loop_item.values():
result.append(inner_item)
return result


def get_workflow_state(workflow):
details = workflow.get_runtime_details()
node_list = details.values()
all_node = [*node_list, *get_loop_workflow_node(node_list)]
err = any([True for value in all_node if value.get('status') == 500])
if err:
return State.FAILURE
write_is_exist = any([True for value in all_node if value.get('type') == 'knowledge-write-node'])
if not write_is_exist:
return State.FAILURE
return State.SUCCESS


class NodeResult:
def __init__(self, node_variable: Dict, workflow_variable: Dict,
_write_context=write_context, _is_interrupt=is_interrupt):
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 provided Python code seems generally well-written and follows Python best practices for encapsulation, functions, and error handling. Here are some minor corrections, optimizations, and improvements:

  1. Code Clarity:

    • Adding comments could clarify the purpose of each function.
  2. Function Naming:

    • handler can be renamed to something more descriptive like process_workflows.
  3. Loop Workflow Node Handling:

    • The get_loop_workflow_node function is good but it can be made a bit cleaner by reducing nesting levels.
  4. Workflow State Determination:

    • The logic in get_workflow_state looks correct, but it might benefit from additional logging or debugging information if needed.
  5. NodeResult Class:

    • The initialization method includes parameters that don't have default values yet.

Here's an optimized version of the code with these considerations:

@@ -105,13 +105,27 @@ def __init__(self, chat_info, knowledge_action_id):
         self.knowledge_action_id = knowledge_action_id

     def process_workflows(self, workflows):
+        # Initialize results list
         results = []
+        
         for workflow in workflows:
             state = get_workflow_state(workflow)
             updated_record = KnowledgeAction.objects.filter(id=self.knowledge_action_id).update(state=state,
                                                                                         run_time=time.time() -
                                                                                             workflow.context.get('start_time')
                                                                                              if workflow.context.get(
                                                                                                'start_time') is not None else 0)
-
-            records.append(record)

Additional Improvements:

  • Ensure that any used modules (like write_context, is_interrupt) are imported at the beginning of the file.
  • Validate input data types where applicable to prevent runtime errors.
  • Consider using asynchronous operations if necessary for performance reasons on large datasets.

These changes will make the code clearer, maintainable, and slightly more performant. If you need further assistance or optimizations beyond this scope, feel free to ask!

Expand Down
Loading