Skip to content

Commit

Permalink
fix: outdated code conditions and global variable (langflow-ai#2581)
Browse files Browse the repository at this point in the history
* Fixed Check Code Validity to set outdated as false when type does not exist

* feat: Refactor update_template_values function

Refactor the `update_template_values` function to improve code readability and maintainability. Rename the parameters `frontend_template` to `new_template` and `raw_template` to `previous_template` for clarity. Update the variable names within the function accordingly.

* feat(update_template_field): update load_from_db in case field value is not the default

* refactor: update template values in PromptComponent

Update the `update_template_values` function in the `PromptComponent` class to improve code readability and maintainability. Rename the parameters `frontend_template` to `new_template` and `raw_template` to `previous_template` for clarity. Update the variable names within the function accordingly.

---------

Co-authored-by: Gabriel Luiz Freitas Almeida <[email protected]>
  • Loading branch information
lucaseduoli and ogabrielluiz committed Jul 9, 2024
1 parent 279bc18 commit 8efc50d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/backend/base/langflow/components/prompts/Prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ def post_code_processing(self, new_build_config: dict, current_build_config: dic
)
# Now that template is updated, we need to grab any values that were set in the current_build_config
# and update the frontend_node with those values
update_template_values(frontend_template=frontend_node, raw_template=current_build_config["template"])
update_template_values(new_template=frontend_node, previous_template=current_build_config["template"])
return frontend_node
27 changes: 16 additions & 11 deletions src/backend/base/langflow/template/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ def get_file_path_value(file_path):
return file_path


def update_template_field(frontend_template, key, value_dict):
def update_template_field(new_template, key, previous_value_dict):
"""Updates a specific field in the frontend template."""
template_field = frontend_template.get(key)
if not template_field or template_field.get("type") != value_dict.get("type"):
template_field = new_template.get(key)
if not template_field or template_field.get("type") != previous_value_dict.get("type"):
return

if "value" in value_dict and value_dict["value"]:
template_field["value"] = value_dict["value"]
if "value" in previous_value_dict and previous_value_dict["value"] is not None:
# if the new value is different, this means the default value has been changed
# so we need to update the value in the template_field
# and set other parameters to the new ones as well
if template_field.get("value") != previous_value_dict["value"]:
template_field["load_from_db"] = previous_value_dict.get("load_from_db", False)
template_field["value"] = previous_value_dict["value"]

if "file_path" in value_dict and value_dict["file_path"]:
file_path_value = get_file_path_value(value_dict["file_path"])
if "file_path" in previous_value_dict and previous_value_dict["file_path"]:
file_path_value = get_file_path_value(previous_value_dict["file_path"])
if not file_path_value:
# If the file does not exist, remove the value from the template_field["value"]
template_field["value"] = ""
Expand All @@ -50,13 +55,13 @@ def is_valid_data(frontend_node, raw_frontend_data):
return frontend_node and "template" in frontend_node and raw_frontend_data_is_valid(raw_frontend_data)


def update_template_values(frontend_template, raw_template):
def update_template_values(new_template, previous_template):
"""Updates the frontend template with values from the raw template."""
for key, value_dict in raw_template.items():
if key == "code" or not isinstance(value_dict, dict):
for key, previous_value_dict in previous_template.items():
if key == "code" or not isinstance(previous_value_dict, dict):
continue

update_template_field(frontend_template, key, value_dict)
update_template_field(new_template, key, previous_value_dict)


def update_frontend_node_with_template_values(frontend_node, raw_frontend_node):
Expand Down
19 changes: 8 additions & 11 deletions src/frontend/src/CustomNodes/hooks/use-check-code-validity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ const useCheckCodeValidity = (
// This one should run only once
// first check if data.type in NATIVE_CATEGORIES
// if not return
if (
!Object.keys(nodeNames).includes(types[data.type]) ||
!data.node?.template?.code?.value
)
return;
const thisNodeTemplate = templates[data.type].template;
// if the template does not have a code key
// return
if (!thisNodeTemplate.code) return;
const currentCode = thisNodeTemplate.code?.value;
if (!data?.node || !templates) return;
const currentCode = templates[data.type]?.template?.code?.value;
const thisNodesCode = data.node!.template?.code?.value;
const componentsToIgnore = ["CustomComponent"];
setIsOutdated(
currentCode !== thisNodesCode && !componentsToIgnore.includes(data.type),
currentCode &&
thisNodesCode &&
currentCode !== thisNodesCode &&
!componentsToIgnore.includes(data.type) &&
Object.keys(nodeNames).includes(types[data.type]),
);
setIsUserEdited(data.node?.edited ?? false);
// template.code can be undefined
}, [
data.node,
data.node?.template?.code?.value,
templates,
setIsOutdated,
Expand Down

0 comments on commit 8efc50d

Please sign in to comment.