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 @@ -30,11 +30,15 @@ def execute(self, variable_list, stream, **kwargs) -> NodeResult:
val = json.loads(variable['value'])
self.workflow_manage.context[variable['fields'][1]] = val
result['output_value'] = variable['value'] = val
else:
elif variable['type'] == 'string':
# 变量解析 例如:{{global.xxx}}
val = self.workflow_manage.generate_prompt(variable['value'])
self.workflow_manage.context[variable['fields'][1]] = val
result['output_value'] = val
else:
val = variable['value']
self.workflow_manage.context[variable['fields'][1]] = val
result['output_value'] = val
else:
reference = self.get_reference_content(variable['reference'])
self.workflow_manage.context[variable['fields'][1]] = reference
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The provided code snippet appears to be a function within a class called execute. It's designed to handle different types of variables during execution. Here are a few comments on the code:

  1. Empty Default Case: The commented-out else block after checking if the type is 'string' suggests that there might have been a default case intended, but it's currently not used. Consider removing this commented-out block or making it functional.

  2. Type Check for 'string' Only: Since the condition checks only for 'str', all other types (like dict or any custom object) will fall into the default if block or remain unchanged by the current logic. This can lead to potential issues when trying to process complex variable values.

  3. Variable Value Assignment: Assigning both result['output_value'] and variable['value'] inside the same condition might not be necessary unless specifically required in the context of some operation you're missing from the comment above.

  4. Optimization Suggestions:

    • Avoid Repeated Context Updates: Ensure that updating the workflow context is done efficiently without redundant operations.
    • Validate Variable Type Before Parsing: Although your parsing based on type seems correct now, consider adding additional validation before attempting to convert strings to JSON, dictionaries, or other objects.

Here’s an improved version with these considerations taken into account:

def execute(self, variable_list, stream, **kwargs):
    result = {'output_value': ''}
    for variable in variable_list:
        if variable['type'].lower() == 'json':
            try:
                val = json.loads(variable['value'])
            except (JSONDecodeError, TypeError):
                print(f"Failed to decode JSON: {variable['value']}")
                continue
        elif variable['type'].upper() == 'STRING':
            # Parse string variables using self.workflow_manage.generate_prompt
            val = self.workflow_manage.generate_prompt(variable['value'])
        # Assuming no conversion needed for scalar primitives (int, float)
        else:
            val = variable['value']

        self.workflow_manage.context[variable['fields'][1]] = val

        # Optionally, update output value
        result['output_value'] = val

    return NodeResult(result=result, status_code=0)  # Add appropriate status codes

This revision includes type-checking explicitly for "json" and "STRING", uses exceptions to manage JSONDecodeError/TypeError cases gracefully, ensures consistent variable handling, and improves code readability by reducing duplicate assignments and improving conditional structure.

Expand Down
Loading