AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): Fix exception response on RPC error & Missing Graph `Running` Status (#9529)
Zamil Majdy538ad06547c0
diff --git a/autogpt_platform/backend/backend/data/execution.py b/autogpt_platform/backend/backend/data/execution.pyindex bd6d79d47e..57dba68ab7 100644--- a/autogpt_platform/backend/backend/data/execution.py+++ b/autogpt_platform/backend/backend/data/execution.py@@ -265,14 +265,18 @@ async def upsert_execution_output()-async def update_graph_execution_start_time(graph_exec_id: str):- await AgentGraphExecution.prisma().update(+async def update_graph_execution_start_time(graph_exec_id: str) -> ExecutionResult:+ res = await AgentGraphExecution.prisma().update(where={"id": graph_exec_id},data={"executionStatus": ExecutionStatus.RUNNING,"startedAt": datetime.now(tz=timezone.utc),},)+ if not res:+ raise ValueError(f"Execution {graph_exec_id} not found.")++ return ExecutionResult.from_graph(res)async def update_graph_execution_stats(diff --git a/autogpt_platform/backend/backend/executor/database.py b/autogpt_platform/backend/backend/executor/database.pyindex fad1a678de..5ead75c5ae 100644--- a/autogpt_platform/backend/backend/executor/database.py+++ b/autogpt_platform/backend/backend/executor/database.py@@ -8,6 +8,7 @@ from backend.data.execution import (get_incomplete_executions,get_latest_execution,update_execution_status,+ update_graph_execution_start_time,update_graph_execution_stats,update_node_execution_stats,upsert_execution_input,@@ -52,6 +53,9 @@ class DatabaseManager(AppService):get_incomplete_executions = exposed_run_and_wait(get_incomplete_executions)get_latest_execution = exposed_run_and_wait(get_latest_execution)update_execution_status = exposed_run_and_wait(update_execution_status)+ update_graph_execution_start_time = exposed_run_and_wait(+ update_graph_execution_start_time+ )update_graph_execution_stats = exposed_run_and_wait(update_graph_execution_stats)update_node_execution_stats = exposed_run_and_wait(update_node_execution_stats)upsert_execution_input = exposed_run_and_wait(upsert_execution_input)diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.pyindex 6ad8f05164..7dfcc86dca 100644--- a/autogpt_platform/backend/backend/executor/manager.py+++ b/autogpt_platform/backend/backend/executor/manager.py@@ -644,6 +644,7 @@ class Executor:node_eid="*",block_name="-",)+ cls.db_client.update_graph_execution_start_time(graph_exec.graph_exec_id)timing_info, (exec_stats, status, error) = cls._on_graph_execution(graph_exec, cancel, log_metadata)diff --git a/autogpt_platform/backend/backend/util/service.py b/autogpt_platform/backend/backend/util/service.pyindex 5f1c8b56f0..189b20b13c 100644--- a/autogpt_platform/backend/backend/util/service.py+++ b/autogpt_platform/backend/backend/util/service.py@@ -36,7 +36,7 @@ from typing import (import httpximport Pyro5.apiimport uvicorn-from fastapi import FastAPI, HTTPException+from fastapi import FastAPI, Request, responsesfrom pydantic import BaseModel, TypeAdapter, create_modelfrom Pyro5 import api as pyrofrom Pyro5 import config as pyro_config@@ -52,9 +52,11 @@ T = TypeVar("T")C = TypeVar("C", bound=Callable)config = Config()-pyro_config.MAX_RETRIES = config.pyro_client_comm_retry # type: ignore-pyro_config.COMMTIMEOUT = config.pyro_client_comm_timeout # type: ignoreapi_host = config.pyro_host+api_comm_retry = config.pyro_client_comm_retry+api_comm_timeout = config.pyro_client_comm_timeout+pyro_config.MAX_RETRIES = api_comm_retry # type: ignore+pyro_config.COMMTIMEOUT = api_comm_timeout # type: ignoreP = ParamSpec("P")@@ -241,6 +243,18 @@ class BaseAppService(AppProcess, ABC):class FastApiAppService(BaseAppService, ABC):fastapi_app: FastAPI+ @staticmethod+ def _handle_internal_http_error(status_code: int = 500, log_error: bool = True):+ def handler(request: Request, exc: Exception):+ if log_error:+ logger.exception(f"{request.method} {request.url.path} failed: {exc}")+ return responses.Response(+ content=str(exc),+ status_code=status_code,+ )++ return handler+def _create_fastapi_endpoint(self, func: Callable) -> Callable:"""Generates a FastAPI endpoint for the given function, handling default and optional parameters.@@ -274,25 +288,15 @@ class FastApiAppService(BaseAppService, ABC):if asyncio.iscoroutinefunction(f):async def async_endpoint(body: RequestBodyModel): # type: ignore #RequestBodyModel being variable- try:- return await f(- **{name: getattr(body, name) for name in body.model_fields}- )- except Exception as e:- logger.exception(f"Error in {func.__name__}: {e}")- raise HTTPException(status_code=500, detail=e)+ return await f(+ **{name: getattr(body, name) for name in body.model_fields}+ )return async_endpointelse:def sync_endpoint(body: RequestBodyModel): # type: ignore #RequestBodyModel being variable- try:- return f(- **{name: getattr(body, name) for name in body.model_fields}- )- except Exception as e:- logger.exception(f"Error in {func.__name__}: {e}")- raise HTTPException(status_code=500, detail=e)+ return f(**{name: getattr(body, name) for name in body.model_fields})return sync_endpoint@@ -322,6 +326,12 @@ class FastApiAppService(BaseAppService, ABC):self.fastapi_app.add_api_route("/health_check", self.health_check, methods=["POST"])+ self.fastapi_app.add_exception_handler(+ ValueError, self._handle_internal_http_error(400)+ )+ self.fastapi_app.add_exception_handler(+ Exception, self._handle_internal_http_error(500)+ )# Start the FastAPI server in a separate thread.api_thread = threading.Thread(target=self.__start_fastapi, daemon=True)@@ -393,19 +403,21 @@ def fastapi_close_service_client(client: Any) -> None:logger.warning(f"Client {client} is not closable")-@conn_retry("FastAPI client", "Creating service client")+@conn_retry("FastAPI client", "Creating service client", max_retry=api_comm_retry)def fastapi_get_service_client(service_type: Type[AS]) -> AS:class DynamicClient:def __init__(self):host = service_type.get_host()port = service_type.get_port()self.base_url = f"http://{host}:{port}".rstrip("/")- self.client = httpx.Client()+ self.client = httpx.Client(+ base_url=self.base_url,+ timeout=api_comm_timeout,+ )def _call_method(self, method_name: str, **kwargs) -> Any:try:- url = f"{self.base_url}/{method_name}"- response = self.client.post(url, json=to_dict(kwargs))+ response = self.client.post(method_name, json=to_dict(kwargs))response.raise_for_status()return response.json()except httpx.HTTPStatusError as e:diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.pyindex 1593b6a5a8..613093acd5 100644--- a/autogpt_platform/backend/backend/util/settings.py+++ b/autogpt_platform/backend/backend/util/settings.py@@ -66,7 +66,7 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):description="Maximum number of workers to use for node execution within a single graph.",)use_http_based_rpc: bool = Field(- default=False,+ default=True,description="Whether to use HTTP-based RPC for communication between services.",)pyro_host: str = Field(