Added nameless to dict

This commit is contained in:
Lucas Oliveira 2023-12-09 22:50:34 -03:00
commit 9cfcdbad9e
4 changed files with 31 additions and 7 deletions

View file

@ -216,7 +216,7 @@ async def custom_component(
):
component = create_and_validate_component(raw_code.code)
built_frontend_node = build_custom_component_template(component, user_id=user.id)
built_frontend_node = build_custom_component_template(component, user_id=user.id, nameless=True)
built_frontend_node = update_frontend_node_with_template_values(built_frontend_node, raw_code)
return built_frontend_node
@ -234,7 +234,7 @@ async def reload_custom_component(path: str, user: User = Depends(get_current_ac
extractor = CustomComponent(code=content)
extractor.validate()
return build_custom_component_template(extractor, user_id=user.id)
return build_custom_component_template(extractor, user_id=user.id, nameless=True)
except Exception as exc:
raise HTTPException(status_code=400, detail=str(exc))
@ -246,6 +246,8 @@ async def custom_component_update(
):
component = create_and_validate_component(raw_code.code)
component_node = build_custom_component_template(component, user_id=user.id, update_field=raw_code.field)
component_node = build_custom_component_template(
component, user_id=user.id, update_field=raw_code.field, nameless=True
)
# Update the field
return component_node

View file

@ -174,10 +174,14 @@ def extract_type_from_optional(field_type):
return match[1] if match else None
def build_frontend_node(custom_component: CustomComponent):
def build_frontend_node(custom_component: CustomComponent, nameless: Optional[bool] = False):
"""Build a frontend node for a custom component"""
try:
return CustomComponentFrontendNode().to_dict().get(type(custom_component).__name__)
return (
CustomComponentFrontendNode().to_dict_nameless().get(type(custom_component).__name__)
if nameless
else CustomComponentFrontendNode().to_dict().get(type(custom_component).__name__)
)
except Exception as exc:
logger.error(f"Error while building base frontend node: {exc}")
@ -340,11 +344,12 @@ def build_custom_component_template(
custom_component: CustomComponent,
user_id: Optional[Union[str, UUID]] = None,
update_field: Optional[str] = None,
nameless: Optional[bool] = False,
) -> Optional[Dict[str, Any]]:
"""Build a custom component template for the langchain"""
try:
logger.debug("Building custom component template")
frontend_node = build_frontend_node(custom_component)
frontend_node = build_frontend_node(custom_component, nameless)
if frontend_node is None:
return None

View file

@ -75,7 +75,7 @@ class FrontendNode(BaseModel):
"template": self.template.to_dict(self.format_field),
"description": self.description,
"base_classes": self.base_classes,
"display_name": self.display_name,
"display_name": self.display_name or self.name,
"custom_fields": self.custom_fields,
"output_types": self.output_types,
"documentation": self.documentation,

View file

@ -69,3 +69,20 @@ class CustomComponentFrontendNode(FrontendNode):
def to_dict(self):
return super().to_dict()
def to_dict_nameless(self):
"""Returns a dict representation of the frontend node."""
self.process_base_classes()
return {
self.name: {
"template": self.template.to_dict(self.format_field),
"description": self.description,
"base_classes": self.base_classes,
"display_name": self.display_name,
"custom_fields": self.custom_fields,
"output_types": self.output_types,
"documentation": self.documentation,
"beta": self.beta,
"error": self.error,
},
}