From 569be9e0802c14852cde8877d8c26a87137bff15 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 1 Mar 2024 23:57:59 -0300 Subject: [PATCH] Refactor buildUtils to handle activated vertices --- src/frontend/src/stores/flowStore.ts | 23 ++++++-- src/frontend/src/types/api/index.ts | 1 + src/frontend/src/types/zustand/flow/index.ts | 1 + src/frontend/src/utils/buildUtils.ts | 62 ++++++++++++++++++-- 4 files changed, 75 insertions(+), 12 deletions(-) diff --git a/src/frontend/src/stores/flowStore.ts b/src/frontend/src/stores/flowStore.ts index 9ee25623b..fb995273b 100644 --- a/src/frontend/src/stores/flowStore.ts +++ b/src/frontend/src/stores/flowStore.ts @@ -223,12 +223,10 @@ const useFlowStore = create((set, get) => ({ selection.nodes.some((node) => node.data.type === "ChatInput") && checkChatInput(get().nodes) ) { - useAlertStore - .getState() - .setErrorData({ - title: "Error pasting components", - list: ["You can only have one ChatInput component in the flow"], - }); + useAlertStore.getState().setErrorData({ + title: "Error pasting components", + list: ["You can only have one ChatInput component in the flow"], + }); return; } let minimumX = Infinity; @@ -451,6 +449,9 @@ const useFlowStore = create((set, get) => ({ if (vertexBuildData && vertexBuildData.inactivated_vertices) { get().removeFromVerticesBuild(vertexBuildData.inactivated_vertices); } + if (vertexBuildData && vertexBuildData.activated_vertices) { + get().addToVerticesBuild(vertexBuildData.activated_vertices); + } get().addDataToFlowPool( { ...vertexBuildData, buildId }, vertexBuildData.id @@ -507,6 +508,16 @@ const useFlowStore = create((set, get) => ({ set({ verticesBuild: vertices }); }, verticesBuild: null, + addToVerticesBuild: (vertices: string[]) => { + const verticesBuild = get().verticesBuild; + if (!verticesBuild) return; + set({ + verticesBuild: { + ...verticesBuild, + verticesIds: [...verticesBuild.verticesIds, ...vertices], + }, + }); + }, removeFromVerticesBuild: (vertices: string[]) => { const verticesBuild = get().verticesBuild; if (!verticesBuild) return; diff --git a/src/frontend/src/types/api/index.ts b/src/frontend/src/types/api/index.ts index 27e5946b3..076ecbf45 100644 --- a/src/frontend/src/types/api/index.ts +++ b/src/frontend/src/types/api/index.ts @@ -141,6 +141,7 @@ export type VerticesOrderTypeAPI = { export type VertexBuildTypeAPI = { id: string; inactivated_vertices: Array | null; + activated_vertices: Array | null; valid: boolean; params: string; data: VertexDataTypeAPI; diff --git a/src/frontend/src/types/zustand/flow/index.ts b/src/frontend/src/types/zustand/flow/index.ts index c969d8d6b..5b19cee88 100644 --- a/src/frontend/src/types/zustand/flow/index.ts +++ b/src/frontend/src/types/zustand/flow/index.ts @@ -105,6 +105,7 @@ export type FlowStoreType = { runId: string; } | null ) => void; + addToVerticesBuild: (vertices: string[]) => void; removeFromVerticesBuild: (vertices: string[]) => void; verticesBuild: { verticesIds: string[]; diff --git a/src/frontend/src/utils/buildUtils.ts b/src/frontend/src/utils/buildUtils.ts index 1ce675eec..1e09d5398 100644 --- a/src/frontend/src/utils/buildUtils.ts +++ b/src/frontend/src/utils/buildUtils.ts @@ -32,6 +32,7 @@ function getInactiveVertexData(vertexId: string): VertexBuildTypeAPI { data: inactiveData, params: "Inactive", inactivated_vertices: null, + activated_vertices: null, valid: false, timestamp: new Date().toISOString(), }; @@ -110,7 +111,7 @@ export async function buildVertices({ if (!verticesBuild || nodeId) { verticesBuild = await updateVerticesOrder(flowId, nodeId); } - const verticesIds = verticesBuild?.verticesIds!; + let verticesIds = verticesBuild?.verticesIds!; const verticesLayers = verticesBuild?.verticesLayers!; const verticesOrder = verticesBuild?.verticesOrder!; const runId = verticesBuild?.runId!; @@ -128,26 +129,75 @@ export async function buildVertices({ useFlowStore.getState().updateBuildStatus(verticesIds, BuildStatus.TO_BUILD); useFlowStore.getState().setIsBuilding(true); + let dynamicVerticesLayers: Array> = [...verticesLayers]; + + const handleBuildUpdate = (data: VertexBuildTypeAPI, status: BuildStatus) => { + // Handle activated vertices + console.log("handleBuildUpdate", data, status); + if (data.activated_vertices && data.activated_vertices.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) => + layer.includes(data.id) + ); + const nextLayerIndex = thisVertexLayer + 1; + console.log("nextLayerIndex", nextLayerIndex); + 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) { + console.log("vertex", vertex); + if (dynamicVerticesLayers[nextLayerIndex].includes(vertex)) { + continue; + } else if (dynamicVerticesLayers[nextLayerIndex].length > 0) { + dynamicVerticesLayers[nextLayerIndex].push(vertex); + } else { + dynamicVerticesLayers[nextLayerIndex] = [vertex]; + } + console.log("dynamicVerticesLayers", dynamicVerticesLayers); + } + } else { + dynamicVerticesLayers.push(data.activated_vertices); + console.log(dynamicVerticesLayers); + } + } + if (onBuildUpdate) onBuildUpdate(data, status, runId); + }; // Set each vertex state to building const buildResults: Array = []; - for (const layer of verticesLayers) { + for (let i = 0; i < dynamicVerticesLayers.length; i++) { + console.log(dynamicVerticesLayers); + const layer = dynamicVerticesLayers[i]; if (onBuildStart) onBuildStart(layer); for (const id of layer) { // Check if id is in the list of inactive nodes - if (!verticesIds.includes(id) && onBuildUpdate) { + // useFlowStore because it gets updated constantly + if ( + !useFlowStore.getState().verticesBuild?.verticesIds.includes(id) && + onBuildUpdate + ) { // If it is, skip building and set the state to inactive + console.log("inactive", id); onBuildUpdate(getInactiveVertexData(id), BuildStatus.INACTIVE, runId); buildResults.push(false); continue; } + await buildVertex({ flowId, id, input_value, - onBuildUpdate: (data: VertexBuildTypeAPI, status: BuildStatus) => { - if (onBuildUpdate) onBuildUpdate(data, status, runId); - }, + onBuildUpdate: handleBuildUpdate, onBuildError, verticesIds, buildResults,