Refactor build_field_config function to update

field dictionary
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-11-21 16:42:41 -03:00
commit 59adf6064d

View file

@ -7,8 +7,6 @@ from typing import Any, Dict, List, Optional, Union
from uuid import UUID from uuid import UUID
from fastapi import HTTPException from fastapi import HTTPException
from loguru import logger
from langflow.api.utils import get_new_key from langflow.api.utils import get_new_key
from langflow.field_typing.constants import CUSTOM_COMPONENT_SUPPORTED_TYPES from langflow.field_typing.constants import CUSTOM_COMPONENT_SUPPORTED_TYPES
from langflow.interface.agents.base import agent_creator from langflow.interface.agents.base import agent_creator
@ -35,6 +33,7 @@ from langflow.template.field.base import TemplateField
from langflow.template.frontend_node.constants import CLASSES_TO_REMOVE from langflow.template.frontend_node.constants import CLASSES_TO_REMOVE
from langflow.template.frontend_node.custom_components import CustomComponentFrontendNode from langflow.template.frontend_node.custom_components import CustomComponentFrontendNode
from langflow.utils.util import get_base_classes from langflow.utils.util import get_base_classes
from loguru import logger
# Used to get the base_classes list # Used to get the base_classes list
@ -216,29 +215,14 @@ def build_field_config(
if update_field is not None: if update_field is not None:
try: try:
field_dict = build_config[update_field] field_dict = build_config.get(update_field, {})
# If "options" in field_dict, and it is a callable update_field_dict(field_dict)
# then call it to get the options
if "options" in field_dict and callable(field_dict["options"]):
field_dict["options"] = field_dict["options"]()
elif "value" in field_dict and callable(field_dict["value"]):
field_dict["value"] = field_dict["value"]()
# Now we need to update the build_config
# with the new field_dict
build_config[update_field] = field_dict build_config[update_field] = field_dict
except Exception as exc: except Exception as exc:
logger.error(f"Error while getting build_config: {str(exc)}") logger.error(f"Error while getting build_config: {str(exc)}")
else: else:
# Update all the fields
for field_name, field_dict in build_config.items(): for field_name, field_dict in build_config.items():
# If "options" in field_dict, and it is a callable update_field_dict(field_dict)
# then call it to get the options
if "options" in field_dict and callable(field_dict["options"]):
field_dict["options"] = field_dict["options"]()
elif "value" in field_dict and callable(field_dict["value"]):
field_dict["value"] = field_dict["value"]()
# Now we need to update the build_config
# with the new field_dict
build_config[field_name] = field_dict build_config[field_name] = field_dict
return build_config return build_config
@ -248,6 +232,17 @@ def build_field_config(
return {} return {}
def update_field_dict(field_dict):
"""Update the field dictionary by calling options() or value() if they are callable"""
if "options" in field_dict and callable(field_dict["options"]):
field_dict["options"] = field_dict["options"]()
# Also update the "refresh" key
field_dict["refresh"] = True
elif "value" in field_dict and callable(field_dict["value"]):
field_dict["value"] = field_dict["value"]()
field_dict["refresh"] = True
def add_extra_fields(frontend_node, field_config, function_args): def add_extra_fields(frontend_node, field_config, function_args):
"""Add extra fields to the frontend node""" """Add extra fields to the frontend node"""
if function_args is None or function_args == "": if function_args is None or function_args == "":