diff --git a/src/backend/langflow/graph/graph/base.py b/src/backend/langflow/graph/graph/base.py index ac7b0d81d..a4e78f02c 100644 --- a/src/backend/langflow/graph/graph/base.py +++ b/src/backend/langflow/graph/graph/base.py @@ -5,6 +5,7 @@ from langchain.chains.base import Chain from langflow.graph.edge.base import ContractEdge from langflow.graph.graph.constants import lazy_load_vertex_dict from langflow.graph.graph.utils import process_flow +from langflow.graph.schema import InterfaceComponentTypes from langflow.graph.vertex.base import Vertex from langflow.graph.vertex.types import ( ChatVertex, @@ -453,4 +454,18 @@ class Graph: def sort_vertices(self) -> List[List[str]]: """Sorts the vertices in the graph.""" vertices = self.layered_topological_sort() + # Sort each layer to have ChatInput or ChatOutput first + # each layer consists of a list of vertex ids + # formatted as ComponentName-5letters + # e.g. ChatInput-abcde + # we just need to check if the vertex id contains ChatInput or ChatOutput + # and sort the layers accordingly + # InterfaceComponentTypes is an enum + for layer in vertices: + layer.sort( + key=lambda x: any( + InterfaceComponentTypes.CHAT_INPUT.value in x, + InterfaceComponentTypes.CHAT_OUTPUT.value in x, + ) + ) return self.sort_chat_inputs_first(vertices) diff --git a/src/backend/langflow/graph/schema.py b/src/backend/langflow/graph/schema.py new file mode 100644 index 000000000..a3a2822e1 --- /dev/null +++ b/src/backend/langflow/graph/schema.py @@ -0,0 +1,8 @@ +from enum import Enum + + +class InterfaceComponentTypes(Enum): + # ChatInput and ChatOutput are the only ones that are + # power components + ChatInput = "ChatInput" + ChatOutput = "ChatOutput" diff --git a/src/backend/langflow/graph/vertex/base.py b/src/backend/langflow/graph/vertex/base.py index 4029f422e..3214c847f 100644 --- a/src/backend/langflow/graph/vertex/base.py +++ b/src/backend/langflow/graph/vertex/base.py @@ -1,8 +1,8 @@ import ast import inspect import types -from enum import Enum from typing import TYPE_CHECKING, Any, Callable, Coroutine, Dict, List, Optional +from langflow.graph.schema import InterfaceComponentTypes from langflow.graph.utils import UnbuiltObject, UnbuiltResult from langflow.graph.vertex.utils import generate_result @@ -18,13 +18,6 @@ if TYPE_CHECKING: from langflow.graph.graph.base import Graph -class InterfaceComponentTypes(Enum): - # ChatInput and ChatOutput are the only ones that are - # power components - ChatInput = "ChatInput" - ChatOutput = "ChatOutput" - - class Vertex: def __init__( self,