-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
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. |
[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 |
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, |
There was a problem hiding this comment.
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:
-
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. -
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 sinceform_setting
is already serialized byjson.dumps
before usage. -
Lack of Input Validation: There's no explicit validation on input parameters such as
form_field_list
,form_content_format
, orform_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.
feat: The form node supports obtaining data from other nodes