Refactor activated_vertices to activated_layers
This commit is contained in:
parent
48ec2d2fa8
commit
8ac3f4579e
6 changed files with 63 additions and 52 deletions
|
|
@ -156,13 +156,13 @@ async def build_vertex(
|
||||||
inactivated_vertices = None
|
inactivated_vertices = None
|
||||||
inactivated_vertices = list(graph.inactivated_vertices)
|
inactivated_vertices = list(graph.inactivated_vertices)
|
||||||
graph.reset_inactivated_vertices()
|
graph.reset_inactivated_vertices()
|
||||||
activated_vertices = list(graph.activated_vertices)
|
activated_layers = graph.activated_layers
|
||||||
graph.reset_activated_vertices()
|
graph.reset_activated_vertices()
|
||||||
chat_service.set_cache(flow_id, graph)
|
chat_service.set_cache(flow_id, graph)
|
||||||
|
|
||||||
build_response = VertexBuildResponse(
|
build_response = VertexBuildResponse(
|
||||||
inactivated_vertices=inactivated_vertices,
|
inactivated_vertices=inactivated_vertices,
|
||||||
activated_vertices=activated_vertices,
|
activated_layers=activated_layers,
|
||||||
valid=valid,
|
valid=valid,
|
||||||
params=params,
|
params=params,
|
||||||
id=vertex.id,
|
id=vertex.id,
|
||||||
|
|
|
||||||
|
|
@ -230,7 +230,7 @@ class ResultDataResponse(BaseModel):
|
||||||
class VertexBuildResponse(BaseModel):
|
class VertexBuildResponse(BaseModel):
|
||||||
id: Optional[str] = None
|
id: Optional[str] = None
|
||||||
inactivated_vertices: Optional[List[str]] = None
|
inactivated_vertices: Optional[List[str]] = None
|
||||||
activated_vertices: Optional[List[str]] = None
|
activated_layers: Optional[List[List[str]]] = None
|
||||||
valid: bool
|
valid: bool
|
||||||
params: Optional[str]
|
params: Optional[str]
|
||||||
"""JSON string of the params."""
|
"""JSON string of the params."""
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ class Graph:
|
||||||
self._vertices = self._graph_data["nodes"]
|
self._vertices = self._graph_data["nodes"]
|
||||||
self._edges = self._graph_data["edges"]
|
self._edges = self._graph_data["edges"]
|
||||||
self.inactivated_vertices: set = set()
|
self.inactivated_vertices: set = set()
|
||||||
self.activated_vertices: set = set()
|
self.activated_layers: List[List[str]] = []
|
||||||
self.edges: List[ContractEdge] = []
|
self.edges: List[ContractEdge] = []
|
||||||
self.vertices: List[Vertex] = []
|
self.vertices: List[Vertex] = []
|
||||||
self._build_graph()
|
self._build_graph()
|
||||||
|
|
@ -80,20 +80,26 @@ class Graph:
|
||||||
self.state_manager.update_state(name, record)
|
self.state_manager.update_state(name, record)
|
||||||
|
|
||||||
def activate_state_vertices(self, name: str, caller: str):
|
def activate_state_vertices(self, name: str, caller: str):
|
||||||
|
layers = []
|
||||||
for vertex_id in self._is_state_vertices:
|
for vertex_id in self._is_state_vertices:
|
||||||
|
if vertex_id == caller:
|
||||||
|
continue
|
||||||
vertex = self.get_vertex(vertex_id)
|
vertex = self.get_vertex(vertex_id)
|
||||||
if (
|
if (
|
||||||
name in vertex._raw_params["name"]
|
isinstance(vertex._raw_params["name"], str)
|
||||||
|
and name in vertex._raw_params["name"]
|
||||||
and vertex_id != caller
|
and vertex_id != caller
|
||||||
and isinstance(vertex, StateVertex)
|
and isinstance(vertex, StateVertex)
|
||||||
):
|
):
|
||||||
successors = self.get_all_successors(vertex)
|
layers.append([vertex_id])
|
||||||
self.activated_vertices.add(vertex_id)
|
successors = self.get_all_successors(vertex, flat=False)
|
||||||
for successor in successors:
|
for layer in successors:
|
||||||
self.activated_vertices.add(successor.id)
|
|
||||||
|
layers.append([v.id for v in layer])
|
||||||
|
self.activated_layers = layers
|
||||||
|
|
||||||
def reset_activated_vertices(self):
|
def reset_activated_vertices(self):
|
||||||
self.activated_vertices = set()
|
self.activated_layers = []
|
||||||
|
|
||||||
def append_state(
|
def append_state(
|
||||||
self, name: str, record: Union[str, Record], caller: Optional[str] = None
|
self, name: str, record: Union[str, Record], caller: Optional[str] = None
|
||||||
|
|
@ -555,18 +561,41 @@ class Graph:
|
||||||
for source_id in self.predecessor_map.get(vertex.id, [])
|
for source_id in self.predecessor_map.get(vertex.id, [])
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_all_successors(self, vertex, recursive=True):
|
def get_all_successors(self, vertex, recursive=True, flat=True):
|
||||||
# Recursively get the successors of the current vertex
|
# Recursively get the successors of the current vertex
|
||||||
|
# successors = vertex.successors
|
||||||
|
# if not successors:
|
||||||
|
# return []
|
||||||
|
# successors_result = []
|
||||||
|
# for successor in successors:
|
||||||
|
# # Just return a list of successors
|
||||||
|
# if recursive:
|
||||||
|
# next_successors = self.get_all_successors(successor)
|
||||||
|
# successors_result.extend(next_successors)
|
||||||
|
# successors_result.append(successor)
|
||||||
|
# return successors_result
|
||||||
|
# The above is the version without the flat parameter
|
||||||
|
# The below is the version with the flat parameter
|
||||||
|
# the flat parameter will define if each layer of successors
|
||||||
|
# becomes one list or if the result is a list of lists
|
||||||
|
# if flat is True, the result will be a list of vertices
|
||||||
|
# if flat is False, the result will be a list of lists of vertices
|
||||||
|
# each list will represent a layer of successors
|
||||||
successors = vertex.successors
|
successors = vertex.successors
|
||||||
if not successors:
|
if not successors:
|
||||||
return []
|
return []
|
||||||
successors_result = []
|
successors_result = []
|
||||||
for successor in successors:
|
for successor in successors:
|
||||||
# Just return a list of successors
|
|
||||||
if recursive:
|
if recursive:
|
||||||
next_successors = self.get_all_successors(successor)
|
next_successors = self.get_all_successors(successor)
|
||||||
successors_result.extend(next_successors)
|
if flat:
|
||||||
successors_result.append(successor)
|
successors_result.extend(next_successors)
|
||||||
|
else:
|
||||||
|
successors_result.append(next_successors)
|
||||||
|
if flat:
|
||||||
|
successors_result.append(successor)
|
||||||
|
else:
|
||||||
|
successors_result.append([successor])
|
||||||
return successors_result
|
return successors_result
|
||||||
|
|
||||||
def get_successors(self, vertex):
|
def get_successors(self, vertex):
|
||||||
|
|
|
||||||
|
|
@ -223,12 +223,10 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
|
||||||
selection.nodes.some((node) => node.data.type === "ChatInput") &&
|
selection.nodes.some((node) => node.data.type === "ChatInput") &&
|
||||||
checkChatInput(get().nodes)
|
checkChatInput(get().nodes)
|
||||||
) {
|
) {
|
||||||
useAlertStore
|
useAlertStore.getState().setErrorData({
|
||||||
.getState()
|
title: "Error pasting components",
|
||||||
.setErrorData({
|
list: ["You can only have one ChatInput component in the flow"],
|
||||||
title: "Error pasting components",
|
});
|
||||||
list: ["You can only have one ChatInput component in the flow"],
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let minimumX = Infinity;
|
let minimumX = Infinity;
|
||||||
|
|
@ -451,8 +449,8 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
|
||||||
if (vertexBuildData && vertexBuildData.inactivated_vertices) {
|
if (vertexBuildData && vertexBuildData.inactivated_vertices) {
|
||||||
get().removeFromVerticesBuild(vertexBuildData.inactivated_vertices);
|
get().removeFromVerticesBuild(vertexBuildData.inactivated_vertices);
|
||||||
}
|
}
|
||||||
if (vertexBuildData && vertexBuildData.activated_vertices) {
|
if (vertexBuildData && vertexBuildData.activated_layers) {
|
||||||
get().addToVerticesBuild(vertexBuildData.activated_vertices);
|
get().addToVerticesBuild(vertexBuildData.activated_layers.flat());
|
||||||
}
|
}
|
||||||
get().addDataToFlowPool(
|
get().addDataToFlowPool(
|
||||||
{ ...vertexBuildData, buildId },
|
{ ...vertexBuildData, buildId },
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,7 @@ export type VerticesOrderTypeAPI = {
|
||||||
export type VertexBuildTypeAPI = {
|
export type VertexBuildTypeAPI = {
|
||||||
id: string;
|
id: string;
|
||||||
inactivated_vertices: Array<string> | null;
|
inactivated_vertices: Array<string> | null;
|
||||||
activated_vertices: Array<string> | null;
|
activated_layers: Array<Array<string>> | null;
|
||||||
valid: boolean;
|
valid: boolean;
|
||||||
params: string;
|
params: string;
|
||||||
data: VertexDataTypeAPI;
|
data: VertexDataTypeAPI;
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ function getInactiveVertexData(vertexId: string): VertexBuildTypeAPI {
|
||||||
data: inactiveData,
|
data: inactiveData,
|
||||||
params: "Inactive",
|
params: "Inactive",
|
||||||
inactivated_vertices: null,
|
inactivated_vertices: null,
|
||||||
activated_vertices: null,
|
activated_layers: null,
|
||||||
valid: false,
|
valid: false,
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
};
|
};
|
||||||
|
|
@ -131,41 +131,25 @@ export async function buildVertices({
|
||||||
const handleBuildUpdate = (data: VertexBuildTypeAPI, status: BuildStatus) => {
|
const handleBuildUpdate = (data: VertexBuildTypeAPI, status: BuildStatus) => {
|
||||||
// Handle activated vertices
|
// Handle activated vertices
|
||||||
console.log("handleBuildUpdate", data, status);
|
console.log("handleBuildUpdate", data, status);
|
||||||
if (data.activated_vertices && data.activated_vertices.length > 0) {
|
if (data.activated_layers && data.activated_layers.length > 0) {
|
||||||
// Logic to determine the correct placement for activated vertices in dynamicVerticesLayers
|
|
||||||
// For simplicity, this example adds them to the next layer
|
|
||||||
// const nextLayerIndex = i + 1; i doesnt exist in this scope
|
|
||||||
// we don't want to add the activated vertices to the last layer
|
|
||||||
// because these vertices should be built right away
|
|
||||||
const thisVertexLayer = dynamicVerticesLayers.findIndex((layer) =>
|
const thisVertexLayer = dynamicVerticesLayers.findIndex((layer) =>
|
||||||
layer.includes(data.id)
|
layer.includes(data.id)
|
||||||
);
|
);
|
||||||
const nextLayerIndex = thisVertexLayer + 1;
|
let nextLayerIndex = thisVertexLayer + 1;
|
||||||
|
|
||||||
console.log("nextLayerIndex", nextLayerIndex);
|
console.log("nextLayerIndex", nextLayerIndex);
|
||||||
console.log("dynamicVerticesLayers", dynamicVerticesLayers);
|
console.log("dynamicVerticesLayers", dynamicVerticesLayers);
|
||||||
if (dynamicVerticesLayers[nextLayerIndex]) {
|
|
||||||
// If the next layer exists, add the activated vertices to it
|
|
||||||
// dynamicVerticesLayers[nextLayerIndex] = dynamicVerticesLayers[
|
|
||||||
// nextLayerIndex
|
|
||||||
// ].concat(data.activated_vertices);
|
|
||||||
// instead of adding them all at once, add them one by one
|
|
||||||
// add one per layer and if the next layer doesn't exist, create it
|
|
||||||
|
|
||||||
for (const vertex of data.activated_vertices) {
|
data.activated_layers.forEach((newLayer) => {
|
||||||
console.log("vertex", vertex);
|
if (!dynamicVerticesLayers[nextLayerIndex]) {
|
||||||
if (dynamicVerticesLayers[nextLayerIndex].includes(vertex)) {
|
dynamicVerticesLayers[nextLayerIndex] = [];
|
||||||
continue;
|
|
||||||
} else if (dynamicVerticesLayers[nextLayerIndex].length > 0) {
|
|
||||||
dynamicVerticesLayers[nextLayerIndex].push(vertex);
|
|
||||||
} else {
|
|
||||||
dynamicVerticesLayers[nextLayerIndex] = [vertex];
|
|
||||||
}
|
|
||||||
console.log("dynamicVerticesLayers", dynamicVerticesLayers);
|
|
||||||
}
|
}
|
||||||
} else {
|
dynamicVerticesLayers[nextLayerIndex] = [
|
||||||
dynamicVerticesLayers.push(data.activated_vertices);
|
...dynamicVerticesLayers[nextLayerIndex],
|
||||||
console.log(dynamicVerticesLayers);
|
...newLayer,
|
||||||
}
|
];
|
||||||
|
nextLayerIndex += 1;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (onBuildUpdate) onBuildUpdate(data, status, runId);
|
if (onBuildUpdate) onBuildUpdate(data, status, runId);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue