fix: Ensure chat inputs with dependencies are not prioritized in graph sorting (#4666)

Ensure chat inputs with dependencies are not sorted to the first layer in graph.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-11-18 12:35:08 -03:00 committed by GitHub
commit 13620629d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1928,6 +1928,13 @@ class Graph:
return [layer for layer in refined_layers if layer]
def sort_chat_inputs_first(self, vertices_layers: list[list[str]]) -> list[list[str]]:
# First check if any chat inputs have dependencies
for layer in vertices_layers:
for vertex_id in layer:
if "ChatInput" in vertex_id and self.get_predecessors(self.get_vertex(vertex_id)):
return vertices_layers
# If no chat inputs have dependencies, move them to first layer
chat_inputs_first = []
for layer in vertices_layers:
layer_chat_inputs_first = [vertex_id for vertex_id in layer if "ChatInput" in vertex_id]
@ -1935,6 +1942,7 @@ class Graph:
for vertex_id in layer_chat_inputs_first:
# Remove the ChatInput from the layer
layer.remove(vertex_id)
if not chat_inputs_first:
return vertices_layers