Refactor buildVertices process one layer at a time

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-03 14:40:43 -03:00
commit 6781460869
3 changed files with 61 additions and 41 deletions

View file

@ -223,9 +223,7 @@ 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()
.setErrorData({
title: "Error pasting components", title: "Error pasting components",
list: ["You can only have one ChatInput component in the flow"], list: ["You can only have one ChatInput component in the flow"],
}); });
@ -451,10 +449,23 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
if (vertexBuildData && vertexBuildData.inactive_vertices) { if (vertexBuildData && vertexBuildData.inactive_vertices) {
get().removeFromVerticesBuild(vertexBuildData.inactive_vertices); get().removeFromVerticesBuild(vertexBuildData.inactive_vertices);
} }
get().verticesBuild &&
get().updateVerticesBuild({
verticesIds: [
...get().verticesBuild!.verticesIds,
vertexBuildData.id,
],
verticesLayers: [
...get().verticesBuild!.verticesLayers,
vertexBuildData.next_vertices_ids,
],
runId: vertexBuildData.run_id,
});
get().addDataToFlowPool( get().addDataToFlowPool(
{ ...vertexBuildData, buildId }, { ...vertexBuildData, buildId },
vertexBuildData.id vertexBuildData.id
); );
useFlowStore.getState().updateBuildStatus([vertexBuildData.id], status); useFlowStore.getState().updateBuildStatus([vertexBuildData.id], status);
} }
await buildVertices({ await buildVertices({

View file

@ -140,7 +140,9 @@ export type VerticesOrderTypeAPI = {
export type VertexBuildTypeAPI = { export type VertexBuildTypeAPI = {
id: string; id: string;
next_vertices_ids: Array<string>;
inactive_vertices: Array<string> | null; inactive_vertices: Array<string> | null;
run_id: string;
valid: boolean; valid: boolean;
params: string; params: string;
data: VertexDataTypeAPI; data: VertexDataTypeAPI;

View file

@ -125,23 +125,29 @@ export async function buildVertices({
useFlowStore.getState().updateBuildStatus(verticesIds, BuildStatus.TO_BUILD); useFlowStore.getState().updateBuildStatus(verticesIds, BuildStatus.TO_BUILD);
useFlowStore.getState().setIsBuilding(true); useFlowStore.getState().setIsBuilding(true);
let currentLayerIndex = 0; // Start with the first layer
// Set each vertex state to building // Set each vertex state to building
const buildResults: Array<boolean> = []; const buildResults: Array<boolean> = [];
console.log(verticesLayers); console.log(verticesLayers);
for (const layer of verticesLayers) { while (currentLayerIndex < verticesLayers.length) {
if (onBuildStart) onBuildStart(layer); const currentLayer = verticesLayers[currentLayerIndex];
for (const id of layer) { if (onBuildStart) onBuildStart(currentLayer);
await Promise.all(
currentLayer.map(async (vertexId) => {
// Check if id is in the list of inactive nodes // Check if id is in the list of inactive nodes
if (!verticesIds.includes(id) && onBuildUpdate) { if (!verticesIds.includes(vertexId) && onBuildUpdate) {
// If it is, skip building and set the state to inactive // If it is, skip building and set the state to inactive
onBuildUpdate(getInactiveVertexData(id), BuildStatus.INACTIVE, runId); onBuildUpdate(
getInactiveVertexData(vertexId),
BuildStatus.INACTIVE,
runId
);
buildResults.push(false); buildResults.push(false);
continue; return;
} }
await buildVertex({ await buildVertex({
flowId, flowId,
id, id: vertexId,
input_value, input_value,
onBuildUpdate: (data: VertexBuildTypeAPI, status: BuildStatus) => { onBuildUpdate: (data: VertexBuildTypeAPI, status: BuildStatus) => {
if (onBuildUpdate) onBuildUpdate(data, status, runId); if (onBuildUpdate) onBuildUpdate(data, status, runId);
@ -154,13 +160,14 @@ export async function buildVertices({
}, },
}); });
if (stop) { if (stop) {
break; return;
}
} }
})
);
if (stop) { if (stop) {
break; break;
} }
}
if (onBuildComplete) { if (onBuildComplete) {
const allNodesValid = buildResults.every((result) => result); const allNodesValid = buildResults.every((result) => result);
@ -168,7 +175,7 @@ export async function buildVertices({
useFlowStore.getState().setIsBuilding(false); useFlowStore.getState().setIsBuilding(false);
} }
} }
}
async function buildVertex({ async function buildVertex({
flowId, flowId,
id, id,