Added nameless to dict
This commit is contained in:
parent
14efa160f1
commit
9cfcdbad9e
4 changed files with 31 additions and 7 deletions
|
|
@ -216,7 +216,7 @@ async def custom_component(
|
||||||
):
|
):
|
||||||
component = create_and_validate_component(raw_code.code)
|
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)
|
built_frontend_node = update_frontend_node_with_template_values(built_frontend_node, raw_code)
|
||||||
return built_frontend_node
|
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 = CustomComponent(code=content)
|
||||||
extractor.validate()
|
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:
|
except Exception as exc:
|
||||||
raise HTTPException(status_code=400, detail=str(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 = 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
|
# Update the field
|
||||||
return component_node
|
return component_node
|
||||||
|
|
|
||||||
|
|
@ -174,10 +174,14 @@ def extract_type_from_optional(field_type):
|
||||||
return match[1] if match else None
|
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"""
|
"""Build a frontend node for a custom component"""
|
||||||
try:
|
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:
|
except Exception as exc:
|
||||||
logger.error(f"Error while building base frontend node: {exc}")
|
logger.error(f"Error while building base frontend node: {exc}")
|
||||||
|
|
@ -340,11 +344,12 @@ def build_custom_component_template(
|
||||||
custom_component: CustomComponent,
|
custom_component: CustomComponent,
|
||||||
user_id: Optional[Union[str, UUID]] = None,
|
user_id: Optional[Union[str, UUID]] = None,
|
||||||
update_field: Optional[str] = None,
|
update_field: Optional[str] = None,
|
||||||
|
nameless: Optional[bool] = False,
|
||||||
) -> Optional[Dict[str, Any]]:
|
) -> Optional[Dict[str, Any]]:
|
||||||
"""Build a custom component template for the langchain"""
|
"""Build a custom component template for the langchain"""
|
||||||
try:
|
try:
|
||||||
logger.debug("Building custom component template")
|
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:
|
if frontend_node is None:
|
||||||
return None
|
return None
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ class FrontendNode(BaseModel):
|
||||||
"template": self.template.to_dict(self.format_field),
|
"template": self.template.to_dict(self.format_field),
|
||||||
"description": self.description,
|
"description": self.description,
|
||||||
"base_classes": self.base_classes,
|
"base_classes": self.base_classes,
|
||||||
"display_name": self.display_name,
|
"display_name": self.display_name or self.name,
|
||||||
"custom_fields": self.custom_fields,
|
"custom_fields": self.custom_fields,
|
||||||
"output_types": self.output_types,
|
"output_types": self.output_types,
|
||||||
"documentation": self.documentation,
|
"documentation": self.documentation,
|
||||||
|
|
|
||||||
|
|
@ -69,3 +69,20 @@ class CustomComponentFrontendNode(FrontendNode):
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return super().to_dict()
|
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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue