diff --git a/src/backend/base/langflow/components/prompts/Prompt.py b/src/backend/base/langflow/components/prompts/Prompt.py index c102fba3b5a5..8fb7c740dc22 100644 --- a/src/backend/base/langflow/components/prompts/Prompt.py +++ b/src/backend/base/langflow/components/prompts/Prompt.py @@ -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 diff --git a/src/backend/base/langflow/template/utils.py b/src/backend/base/langflow/template/utils.py index b436b898744e..f41167cf7954 100644 --- a/src/backend/base/langflow/template/utils.py +++ b/src/backend/base/langflow/template/utils.py @@ -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"] = "" @@ -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): diff --git a/src/frontend/src/CustomNodes/hooks/use-check-code-validity.tsx b/src/frontend/src/CustomNodes/hooks/use-check-code-validity.tsx index 49f6de757e2e..df60fd1ef9ef 100644 --- a/src/frontend/src/CustomNodes/hooks/use-check-code-validity.tsx +++ b/src/frontend/src/CustomNodes/hooks/use-check-code-validity.tsx @@ -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,