Add is_conditional attribute to CustomComponent and FrontendNode classes

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-02-29 23:07:27 -03:00
commit ac2758c0ac
3 changed files with 26 additions and 6 deletions

View file

@ -40,4 +40,5 @@ ATTR_FUNC_MAPPING: dict[str, Callable] = {
"pinned": getattr_return_bool,
"is_input": getattr_return_bool,
"is_output": getattr_return_bool,
"is_conditional": getattr_return_bool,
}

View file

@ -66,6 +66,8 @@ class CustomComponent(Component):
"""The selected output type of the component. Defaults to None."""
vertex: Optional["Vertex"] = None
"""The edge target parameter of the component. Defaults to None."""
is_conditional: Optional[bool] = False
"""The conditional state of the component. Defaults to False."""
code_class_base_inheritance: ClassVar[str] = "CustomComponent"
function_entrypoint_name: ClassVar[str] = "build"
function: Optional[Callable] = None

View file

@ -73,6 +73,8 @@ class FrontendNode(BaseModel):
"""Field formatters for the frontend node."""
pinned: bool = False
"""Whether the frontend node is pinned."""
is_conditional: bool = False
"""Whether the frontend node is conditional. This is used for the frontend node to show two output handles."""
beta: bool = False
error: Optional[str] = None
@ -171,7 +173,9 @@ class FrontendNode(BaseModel):
return _type
@staticmethod
def handle_special_field(field, key: str, _type: str, SPECIAL_FIELD_HANDLERS) -> str:
def handle_special_field(
field, key: str, _type: str, SPECIAL_FIELD_HANDLERS
) -> str:
"""Handles special field by using the respective handler if present."""
handler = SPECIAL_FIELD_HANDLERS.get(key)
return handler(field) if handler else _type
@ -182,7 +186,11 @@ class FrontendNode(BaseModel):
if "dict" in _type.lower() and field.name == "dict_":
field.field_type = "file"
field.file_types = [".json", ".yaml", ".yml"]
elif _type.startswith("Dict") or _type.startswith("Mapping") or _type.startswith("dict"):
elif (
_type.startswith("Dict")
or _type.startswith("Mapping")
or _type.startswith("dict")
):
field.field_type = "dict"
return _type
@ -193,7 +201,9 @@ class FrontendNode(BaseModel):
field.value = value["default"]
@staticmethod
def handle_specific_field_values(field: TemplateField, key: str, name: Optional[str] = None) -> None:
def handle_specific_field_values(
field: TemplateField, key: str, name: Optional[str] = None
) -> None:
"""Handles specific field values for certain fields."""
if key == "headers":
field.value = """{"Authorization": "Bearer <token>"}"""
@ -201,7 +211,9 @@ class FrontendNode(BaseModel):
FrontendNode._handle_api_key_specific_field_values(field, key, name)
@staticmethod
def _handle_model_specific_field_values(field: TemplateField, key: str, name: Optional[str] = None) -> None:
def _handle_model_specific_field_values(
field: TemplateField, key: str, name: Optional[str] = None
) -> None:
"""Handles specific field values related to models."""
model_dict = {
"OpenAI": constants.OPENAI_MODELS,
@ -214,7 +226,9 @@ class FrontendNode(BaseModel):
field.is_list = True
@staticmethod
def _handle_api_key_specific_field_values(field: TemplateField, key: str, name: Optional[str] = None) -> None:
def _handle_api_key_specific_field_values(
field: TemplateField, key: str, name: Optional[str] = None
) -> None:
"""Handles specific field values related to API keys."""
if "api_key" in key and "OpenAI" in str(name):
field.display_name = "OpenAI API Key"
@ -254,7 +268,10 @@ class FrontendNode(BaseModel):
@staticmethod
def should_be_password(key: str, show: bool) -> bool:
"""Determines whether the field should be a password field."""
return any(text in key.lower() for text in {"password", "token", "api", "key"}) and show
return (
any(text in key.lower() for text in {"password", "token", "api", "key"})
and show
)
@staticmethod
def should_be_multiline(key: str) -> bool: