From 297f9df89f113b3162022e0654d95c184c5ca269 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 25 Mar 2024 13:45:18 -0300 Subject: [PATCH] Add vertices_to_run field to VerticesOrderResponse --- src/backend/langflow/api/v1/chat.py | 2 +- src/backend/langflow/api/v1/schemas.py | 1 + src/frontend/src/stores/flowStore.ts | 6 ++- src/frontend/src/types/api/index.ts | 1 + src/frontend/src/types/zustand/flow/index.ts | 2 + src/frontend/src/utils/buildUtils.ts | 51 ++++++++------------ 6 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/backend/langflow/api/v1/chat.py b/src/backend/langflow/api/v1/chat.py index fbc206fb1..6edbc3b4e 100644 --- a/src/backend/langflow/api/v1/chat.py +++ b/src/backend/langflow/api/v1/chat.py @@ -93,7 +93,7 @@ async def get_vertices( # and return the same structure but only with the ids run_id = uuid.uuid4() graph.set_run_id(run_id) - return VerticesOrderResponse(ids=first_layer, run_id=run_id) + return VerticesOrderResponse(ids=first_layer, run_id=run_id, vertices_to_run=list(graph.vertices_to_run)) except Exception as exc: logger.error(f"Error checking build status: {exc}") diff --git a/src/backend/langflow/api/v1/schemas.py b/src/backend/langflow/api/v1/schemas.py index 7a7f60bec..3aaa5eded 100644 --- a/src/backend/langflow/api/v1/schemas.py +++ b/src/backend/langflow/api/v1/schemas.py @@ -228,6 +228,7 @@ class ApiKeyCreateRequest(BaseModel): class VerticesOrderResponse(BaseModel): ids: List[str] run_id: UUID + vertices_to_run: List[str] class ResultDataResponse(BaseModel): diff --git a/src/frontend/src/stores/flowStore.ts b/src/frontend/src/stores/flowStore.ts index 3eb1b44c1..09b8d5240 100644 --- a/src/frontend/src/stores/flowStore.ts +++ b/src/frontend/src/stores/flowStore.ts @@ -442,6 +442,7 @@ const useFlowStore = create((set, get) => ({ get().nodes.filter((node) => nodes.includes(node.id)), get().edges ); + const errors = errorsObjs.map((obj) => obj.errors).flat(); if (errors.length > 0) { setErrorData({ @@ -450,7 +451,7 @@ const useFlowStore = create((set, get) => ({ }); get().setIsBuilding(false); const ids = errorsObjs.map((obj) => obj.id).flat(); - console.log("ids", ids); + get().updateBuildStatus(ids, BuildStatus.ERROR); throw new Error("Invalid nodes"); } @@ -490,6 +491,7 @@ const useFlowStore = create((set, get) => ({ verticesIds: newIds, verticesLayers: newLayers, runId: runId, + verticesToRun: get().verticesBuild!.verticesToRun, }); get().updateBuildStatus( vertexBuildData.top_level_vertices, @@ -559,6 +561,7 @@ const useFlowStore = create((set, get) => ({ verticesIds: string[]; verticesLayers: VertexLayerElementType[][]; runId: string; + verticesToRun: string[]; } | null ) => { set({ verticesBuild: vertices }); @@ -588,6 +591,7 @@ const useFlowStore = create((set, get) => ({ }, updateBuildStatus: (nodeIdList: string[], status: BuildStatus) => { const newFlowBuildStatus = { ...get().flowBuildStatus }; + nodeIdList.forEach((id) => { newFlowBuildStatus[id] = { status, diff --git a/src/frontend/src/types/api/index.ts b/src/frontend/src/types/api/index.ts index 297984d28..ca8d25ee3 100644 --- a/src/frontend/src/types/api/index.ts +++ b/src/frontend/src/types/api/index.ts @@ -139,6 +139,7 @@ export type Component = { export type VerticesOrderTypeAPI = { ids: Array; + vertices_to_run: Array; run_id: string; }; diff --git a/src/frontend/src/types/zustand/flow/index.ts b/src/frontend/src/types/zustand/flow/index.ts index 0fa8c971d..71853c5a7 100644 --- a/src/frontend/src/types/zustand/flow/index.ts +++ b/src/frontend/src/types/zustand/flow/index.ts @@ -110,6 +110,7 @@ export type FlowStoreType = { verticesIds: string[]; verticesLayers: VertexLayerElementType[][]; runId: string; + verticesToRun: string[]; } | null ) => void; addToVerticesBuild: (vertices: string[]) => void; @@ -118,6 +119,7 @@ export type FlowStoreType = { verticesIds: string[]; verticesLayers: VertexLayerElementType[][]; runId: string; + verticesToRun: string[]; } | null; updateBuildStatus: (nodeId: string[], status: BuildStatus) => void; revertBuiltStatusFromBuilding: () => void; diff --git a/src/frontend/src/utils/buildUtils.ts b/src/frontend/src/utils/buildUtils.ts index 1121fca04..03264d1b9 100644 --- a/src/frontend/src/utils/buildUtils.ts +++ b/src/frontend/src/utils/buildUtils.ts @@ -53,6 +53,7 @@ export async function updateVerticesOrder( verticesLayers: VertexLayerElementType[][]; verticesIds: string[]; runId: string; + verticesToRun: string[]; }> { return new Promise(async (resolve, reject) => { const setErrorData = useAlertStore.getState().setErrorData; @@ -60,7 +61,6 @@ export async function updateVerticesOrder( try { orderResponse = await getVerticesOrder(flowId, startNodeId, stopNodeId); } catch (error: any) { - console.log(error); setErrorData({ title: "Oops! Looks like you missed something", list: [error.response?.data?.detail ?? "Unknown Error"], @@ -77,30 +77,16 @@ export async function updateVerticesOrder( }); const runId = orderResponse.data.run_id; - // if (nodeId) { - // for (let i = 0; i < verticesOrder.length; i += 1) { - // const innerArray = verticesOrder[i]; - // const idIndex = innerArray.indexOf(nodeId); - // if (idIndex !== -1) { - // // If there's a nodeId, we want to run just that component and not the entire layer - // // because a layer contains dependencies for the next layer - // // and we are stopping at the layer that contains the nodeId - // verticesLayers.push([innerArray[idIndex]]); - // break; // Stop searching after finding the first occurrence - // } - // // If the targetId is not found, include the entire inner array - // verticesLayers.push(innerArray); - // } - // } else { - // verticesLayers = verticesOrder; - // } + const verticesToRun = orderResponse.data.vertices_to_run; + const verticesIds = orderResponse.data.ids; useFlowStore.getState().updateVerticesBuild({ verticesLayers, verticesIds, runId, + verticesToRun, }); - resolve({ verticesLayers, verticesIds, runId }); + resolve({ verticesLayers, verticesIds, runId, verticesToRun }); }); } @@ -122,8 +108,22 @@ export async function buildVertices({ if (startNodeId && stopNodeId) { return; } + if (!verticesBuild || startNodeId || stopNodeId) { - verticesBuild = await updateVerticesOrder(flowId, startNodeId, stopNodeId); + let verticesOrderResponse = await updateVerticesOrder( + flowId, + startNodeId, + stopNodeId + ); + if (onValidateNodes) { + try { + onValidateNodes(verticesOrderResponse.verticesToRun); + } catch (e) { + return; + } + } + if (onGetOrderSuccess) onGetOrderSuccess(); + verticesBuild = useFlowStore.getState().verticesBuild; } const verticesIds = verticesBuild?.verticesIds!; @@ -131,17 +131,6 @@ export async function buildVertices({ const runId = verticesBuild?.runId!; let stop = false; - if (onGetOrderSuccess) onGetOrderSuccess(); - - if (onValidateNodes) { - try { - const nodes = useFlowStore.getState().nodes; - onValidateNodes(nodes.map((node) => node.id)); - } catch (e) { - return; - } - } - useFlowStore.getState().updateBuildStatus(verticesIds, BuildStatus.TO_BUILD); useFlowStore.getState().setIsBuilding(true); let currentLayerIndex = 0; // Start with the first layer