Skip to content
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

feat: The form node supports obtaining data from other nodes #1924

Merged
merged 1 commit into from
Dec 27, 2024

Conversation

shaohuzhang1
Copy link
Contributor

feat: The form node supports obtaining data from other nodes

Copy link

f2c-ci-robot bot commented Dec 27, 2024

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

f2c-ci-robot bot commented Dec 27, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@shaohuzhang1 shaohuzhang1 merged commit f111ae1 into main Dec 27, 2024
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@feat_workflow_from branch December 27, 2024 02:39
prompt_template = PromptTemplate.from_template(form_content_format, template_format='jinja2')
value = prompt_template.format(form=form)
value = prompt_template.format(form=form, context=context)
return {
'name': self.node.properties.get('stepName'),
"index": index,
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 code has some issues that need to be addressed:

  1. Potential Null Pointer Exceptions: The code uses self.context.get("is_submit"), which can lead to a null pointer exception if the key is not found. Consider adding default values or handling missing keys explicitly.

  2. Unnecessary JSON Serialization: The code serializes form_setting directly into a string within the HTML template using <form_rander>. This isn't necessary and can introduce complexity since form_setting is already serialized by json.dumps before usage.

  3. Lack of Input Validation: There's no explicit validation on input parameters such as form_field_list, form_content_format, or form_data. This could help prevent unexpected behavior.

Here are some optimizations and suggestions:

Optimizations and Recommendations

Validate Required Arguments

Ensure that all required arguments (form_field_list, etc.) are provided and handle exceptions where they might be missing.

from typing import Any, Dict

class CustomNode(Node):
    def execute(self, form_field_list: list[Any], form_content_format: str, form_data: dict[str, Any], **kwargs) -> NodeResult:
        try:
            is_submit = kwargs.get("is_submit", False)
            
            # Validate inputs
            if not form_field_list:
                raise ValueError("Missing form field list")
            if not isinstance(form_settings, (dict, list)):  # Adjust based on type expected
                raise TypeError("Invalid form settings type")
            if not isinstance(form_data, dict):  # Ensure only dictionaries are processed
                raise TypeError("Form data must be a dictionary")
        
            form_setting_str = json.dumps(form_settings)

            context = self.workflow_manage.get_workflow_content()
            form_content_format = self.workflow_manage.reset_prompt(form_content_format)
            prompt_template = PromptTemplate.from_template(form_content_format, template_format='jinja2')

            value = prompt_template.format(form=form_setting_str, context=context)
            return NodeResult({'result': value, 'form_field_list': form_field_list, 'form_content_format': form_content_format}, {}, _write_context=write_context)

Key Changes:

  • Added checks to ensure input types and contents meet expectations.
  • Used error messages instead of raising exceptions globally for better feedback during debugging.

Simplify HTML Templating

If you're dynamically creating HTML forms, consider simplifying it further. The template should focus on formatting and content rather than including serialization steps.

html_form_template = """
<FORM>
  <INPUT id="input1" value="{{ form.form_field_list }}" type="text">
</FORM>
"""

# Render form using jinja2 template engine
context = {"form": {"form_field_list": ["value1", "value2"]}}
html_form = html_form_template.render(context)

By addressing these points, you make the code more robust, readable, and maintainable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant