feat: Migrate base classes to outputs in FrontendNode

This commit migrates the base classes of the FrontendNode to the outputs field. Each base class is converted into an OutputField with the same name and type. This change ensures consistency and improves the structure of the code.
This commit is contained in:
ogabrielluiz 2024-05-30 17:17:36 -03:00
commit 25752b1aa8
2 changed files with 19 additions and 4 deletions

View file

@ -4,7 +4,7 @@ from typing import ClassVar, Dict, List, Optional, Union
from pydantic import BaseModel, Field, field_serializer, model_serializer from pydantic import BaseModel, Field, field_serializer, model_serializer
from langflow.template.field.base import InputField from langflow.template.field.base import InputField, OutputField
from langflow.template.frontend_node.constants import FORCE_SHOW_FIELDS from langflow.template.frontend_node.constants import FORCE_SHOW_FIELDS
from langflow.template.frontend_node.formatter import field_formatters from langflow.template.frontend_node.formatter import field_formatters
from langflow.template.template.base import Template from langflow.template.template.base import Template
@ -77,6 +77,8 @@ class FrontendNode(BaseModel):
"""List of conditional paths for the frontend node.""" """List of conditional paths for the frontend node."""
frozen: bool = False frozen: bool = False
"""Whether the frontend node is frozen.""" """Whether the frontend node is frozen."""
outputs: List[OutputField] = []
"""List of output fields for the frontend node."""
field_order: list[str] = [] field_order: list[str] = []
"""Order of the fields in the frontend node.""" """Order of the fields in the frontend node."""
@ -114,6 +116,15 @@ class FrontendNode(BaseModel):
result["template"] = self.template.to_dict(format_func) result["template"] = self.template.to_dict(format_func)
name = result.pop("name") name = result.pop("name")
# Migrate base classes to outputs
if "output_types" in result:
for base_class in result["output_types"]:
output = OutputField(
name=base_class,
types=[base_class],
)
result["outputs"].append(output.model_dump())
return {name: result} return {name: result}
# For backwards compatibility # For backwards compatibility

View file

@ -444,9 +444,13 @@ export function updateNewOutput({ nodes, edges }: updateEdgesHandleIdsType) {
!sourceNode.data.node?.outputs || !sourceNode.data.node?.outputs ||
sourceNode.data.node!.outputs!.length === 0 sourceNode.data.node!.outputs!.length === 0
) { ) {
sourceNode.data.node!.outputs = [ const outputTypes = sourceNode.data.node!.output_types;
{ types: sourceNode.data.node!.base_classes, selected: selected }, // create a new output field for each output type
]; sourceNode.data.node!.outputs = outputTypes?.map((type) => ({
types: [type],
selected: selected,
name: type,
}));
} }
} }
edge.sourceHandle = scapedJSONStringfy(newSourceHandle); edge.sourceHandle = scapedJSONStringfy(newSourceHandle);