From 95d666636b9345fc2bfad5f7db69ea9b42a76316 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sat, 22 Jun 2024 18:00:32 -0300 Subject: [PATCH] refactor: Update post_code_validation method in Component class Refactor the post_code_validation method in the Component class to improve code readability and maintainability. The changes include: - Rename the parameters to better reflect their purpose. - Update the function documentation to provide a clear explanation of the method's purpose. - Remove unnecessary comments and whitespace. - Improve variable naming for better clarity. These changes enhance the overall quality of the code and make it easier to understand and maintain. --- src/backend/base/langflow/api/v1/endpoints.py | 5 ++--- .../base/langflow/custom/custom_component/component.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/backend/base/langflow/api/v1/endpoints.py b/src/backend/base/langflow/api/v1/endpoints.py index d69cc3612..dda83cc31 100644 --- a/src/backend/base/langflow/api/v1/endpoints.py +++ b/src/backend/base/langflow/api/v1/endpoints.py @@ -8,7 +8,6 @@ from fastapi import APIRouter, BackgroundTasks, Body, Depends, HTTPException, Re from loguru import logger from sqlmodel import Session, select -from langflow.api.utils import update_frontend_node_with_template_values from langflow.api.v1.schemas import ( ConfigResponse, CustomComponentRequest, @@ -466,9 +465,9 @@ async def custom_component( ): component = Component(code=raw_code.code) - built_frontend_node, _ = build_custom_component_template(component, user_id=user.id) + built_frontend_node, component_instance = build_custom_component_template(component, user_id=user.id) - built_frontend_node = update_frontend_node_with_template_values(built_frontend_node, raw_code.frontend_node) + built_frontend_node = component_instance.post_code_validation(built_frontend_node, raw_code.frontend_node) return built_frontend_node diff --git a/src/backend/base/langflow/custom/custom_component/component.py b/src/backend/base/langflow/custom/custom_component/component.py index 1f6693f5f..532bd9fbc 100644 --- a/src/backend/base/langflow/custom/custom_component/component.py +++ b/src/backend/base/langflow/custom/custom_component/component.py @@ -10,6 +10,7 @@ from langflow.schema.artifact import get_artifact_type, post_process_raw from langflow.schema.data import Data from langflow.schema.message import Message from langflow.template.field.base import UNDEFINED, Output +from langflow.template.utils import update_frontend_node_with_template_values from .custom_component import CustomComponent @@ -224,3 +225,12 @@ class Component(CustomComponent): def build(self, **kwargs): self.set_attributes(kwargs) + + def post_code_validation(self, new_build_config: dict, current_build_config: dict): + """ + This function is called after the code validation is done. + """ + frontend_node = update_frontend_node_with_template_values( + frontend_node=new_build_config, raw_frontend_node=current_build_config + ) + return frontend_node