From 48d40bfdd30abaf8c218b4c353e0d2b7f8ce819b Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 28 Jun 2023 22:29:56 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(chat.py):=20add=20status=20fie?= =?UTF-8?q?ld=20to=20flow=5Fdata=5Fstore=20to=20track=20build=20status=20o?= =?UTF-8?q?f=20flows=20The=20import=20statement=20for=20the=20BuildStatus?= =?UTF-8?q?=20enum=20in=20the=20schemas=20module=20has=20been=20updated=20?= =?UTF-8?q?to=20ensure=20the=20correct=20import.=20Additionally,=20a=20new?= =?UTF-8?q?=20"status"=20field=20has=20been=20added=20to=20the=20flow=5Fda?= =?UTF-8?q?ta=5Fstore=20dictionary=20to=20track=20the=20build=20status=20o?= =?UTF-8?q?f=20flows.=20The=20status=20is=20set=20to=20BuildStatus.IN=5FPR?= =?UTF-8?q?OGRESS=20when=20a=20flow=20is=20being=20built,=20BuildStatus.SU?= =?UTF-8?q?CCESS=20when=20the=20build=20is=20successful,=20and=20BuildStat?= =?UTF-8?q?us.FAILURE=20when=20there=20is=20an=20error=20during=20the=20bu?= =?UTF-8?q?ild=20process.=20This=20allows=20for=20better=20tracking=20and?= =?UTF-8?q?=20management=20of=20flow=20builds.=20=F0=9F=90=9B=20fix(chat.p?= =?UTF-8?q?y):=20change=20import=20statement=20for=20BuildStatus=20in=20sc?= =?UTF-8?q?hemas=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/chat.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/backend/langflow/api/v1/chat.py b/src/backend/langflow/api/v1/chat.py index a730758d2..b992afe9a 100644 --- a/src/backend/langflow/api/v1/chat.py +++ b/src/backend/langflow/api/v1/chat.py @@ -6,7 +6,7 @@ from fastapi import ( status, ) from fastapi.responses import StreamingResponse -from langflow.api.v1.schemas import BuiltResponse, InitResponse, StreamData +from langflow.api.v1.schemas import BuildStatus, BuiltResponse, InitResponse, StreamData from langflow.chat.manager import ChatManager from langflow.graph.graph.base import Graph @@ -49,7 +49,10 @@ async def init_build(graph_data: dict): with chat_manager.in_memory_cache._lock: chat_manager.in_memory_cache.delete(flow_id) logger.debug(f"Deleted flow {flow_id} from cache") - flow_data_store[flow_id] = {"graph_data": graph_data, "building": False} + flow_data_store[flow_id] = { + "graph_data": graph_data, + "status": BuildStatus.IN_PROGRESS, + } return InitResponse(flowId=flow_id) except Exception as exc: @@ -61,8 +64,9 @@ async def init_build(graph_data: dict): async def build_status(flow_id: str): """Check the flow_id is in the flow_data_store.""" try: - built = flow_id in flow_data_store and not isinstance( - flow_data_store[flow_id], dict + built = ( + flow_id in flow_data_store + and flow_data_store[flow_id]["status"] == BuildStatus.SUCCESS ) return BuiltResponse( @@ -86,7 +90,7 @@ async def stream_build(flow_id: str): yield str(StreamData(event="error", data={"error": error_message})) return - if flow_data_store[flow_id].get("building"): + if flow_data_store[flow_id].get("status") == BuildStatus.IN_PROGRESS: error_message = "Already building" yield str(StreamData(event="error", data={"error": error_message})) return @@ -124,6 +128,7 @@ async def stream_build(flow_id: str): except Exception as exc: params = str(exc) valid = False + flow_data_store[flow_id]["status"] = BuildStatus.FAILURE response = { "valid": valid, @@ -135,8 +140,10 @@ async def stream_build(flow_id: str): yield str(StreamData(event="message", data=response)) chat_manager.set_cache(flow_id, graph.build()) + flow_data_store[flow_id]["status"] = BuildStatus.SUCCESS except Exception as exc: logger.error("Error while building the flow: %s", exc) + flow_data_store[flow_id]["status"] = BuildStatus.FAILURE yield str(StreamData(event="error", data={"error": str(exc)})) finally: yield str(StreamData(event="message", data=final_response))