Skip to content

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): add SSE heartbeats to prevent tool execution timeouts (#11855)

Swiftyfac10c422bb4
diff --git a/autogpt_platform/backend/backend/api/features/chat/response_model.py b/autogpt_platform/backend/backend/api/features/chat/response_model.py
index 49a9b38e8f..53a8cf3a1f 100644
--- a/autogpt_platform/backend/backend/api/features/chat/response_model.py
+++ b/autogpt_platform/backend/backend/api/features/chat/response_model.py
@@ -31,6 +31,7 @@ class ResponseType(str, Enum):
# Other
ERROR = "error"
USAGE = "usage"
+ HEARTBEAT = "heartbeat"
class StreamBaseResponse(BaseModel):
@@ -142,3 +143,20 @@ class StreamError(StreamBaseResponse):
details: dict[str, Any] | None = Field(
default=None, description="Additional error details"
)
+
+
+class StreamHeartbeat(StreamBaseResponse):
+ """Heartbeat to keep SSE connection alive during long-running operations.
+
+ Uses SSE comment format (: comment) which is ignored by clients but keeps
+ the connection alive through proxies and load balancers.
+ """
+
+ type: ResponseType = ResponseType.HEARTBEAT
+ toolCallId: str | None = Field(
+ default=None, description="Tool call ID if heartbeat is for a specific tool"
+ )
+
+ def to_sse(self) -> str:
+ """Convert to SSE comment format to keep connection alive."""
+ return ": heartbeat\n\n"
diff --git a/autogpt_platform/backend/backend/api/features/chat/service.py b/autogpt_platform/backend/backend/api/features/chat/service.py
index f8336b9107..386b37784d 100644
--- a/autogpt_platform/backend/backend/api/features/chat/service.py
+++ b/autogpt_platform/backend/backend/api/features/chat/service.py
@@ -38,6 +38,7 @@ from .response_model import (
StreamBaseResponse,
StreamError,
StreamFinish,
+ StreamHeartbeat,
StreamStart,
StreamTextDelta,
StreamTextEnd,
@@ -48,6 +49,7 @@ from .response_model import (
StreamUsage,
)
from .tools import execute_tool, tools
+from .tools.models import ErrorResponse
from .tracking import track_user_message
logger = logging.getLogger(__name__)
@@ -1258,6 +1260,9 @@ async def _yield_tool_call(
"""
Yield a tool call and its execution result.
+ For long-running tools, yields heartbeat events every 15 seconds to keep
+ the SSE connection alive through proxies and load balancers.
+
Raises:
orjson.JSONDecodeError: If tool call arguments cannot be parsed as JSON
KeyError: If expected tool call fields are missing
@@ -1280,12 +1285,53 @@ async def _yield_tool_call(
input=arguments,
)
- tool_execution_response: StreamToolOutputAvailable = await execute_tool(
- tool_name=tool_name,
- parameters=arguments,
- tool_call_id=tool_call_id,
- user_id=session.user_id,
- session=session,
+ # Run tool execution in background task with heartbeats to keep connection alive
+ tool_task = asyncio.create_task(
+ execute_tool(
+ tool_name=tool_name,
+ parameters=arguments,
+ tool_call_id=tool_call_id,
+ user_id=session.user_id,
+ session=session,
+ )
)
+ # Yield heartbeats every 15 seconds while waiting for tool to complete
+ heartbeat_interval = 15.0 # seconds
+ while not tool_task.done():
+ try:
+ # Wait for either the task to complete or the heartbeat interval
+ await asyncio.wait_for(
+ asyncio.shield(tool_task), timeout=heartbeat_interval
+ )
+ except asyncio.TimeoutError:
+ # Task still running, send heartbeat to keep connection alive
+ logger.debug(f"Sending heartbeat for tool {tool_name} ({tool_call_id})")
+ yield StreamHeartbeat(toolCallId=tool_call_id)
+ except CancelledError:
+ # Task was cancelled, clean up and propagate
+ tool_task.cancel()
+ logger.warning(f"Tool execution cancelled: {tool_name} ({tool_call_id})")
+ raise
+
+ # Get the result - handle any exceptions that occurred during execution
+ try:
+ tool_execution_response: StreamToolOutputAvailable = await tool_task
+ except Exception as e:
+ # Task raised an exception - ensure we send an error response to the frontend
+ logger.error(
+ f"Tool execution failed: {tool_name} ({tool_call_id}): {e}", exc_info=True
+ )
+ error_response = ErrorResponse(
+ message=f"Tool execution failed: {e!s}",
+ error=type(e).__name__,
+ session_id=session.session_id,
+ )
+ tool_execution_response = StreamToolOutputAvailable(
+ toolCallId=tool_call_id,
+ toolName=tool_name,
+ output=error_response.model_dump_json(),
+ success=False,
+ )
+
yield tool_execution_response
diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/create_agent.py b/autogpt_platform/backend/backend/api/features/chat/tools/create_agent.py
index 6469cc4442..87ca5ebca7 100644
--- a/autogpt_platform/backend/backend/api/features/chat/tools/create_agent.py
+++ b/autogpt_platform/backend/backend/api/features/chat/tools/create_agent.py
@@ -113,8 +113,11 @@ class CreateAgentTool(BaseTool):
if decomposition_result is None:
return ErrorResponse(
- message="Failed to analyze the goal. Please try rephrasing.",
- error="Decomposition failed",
+ message="Failed to analyze the goal. The agent generation service may be unavailable or timed out. Please try again.",
+ error="decomposition_failed",
+ details={
+ "description": description[:100]
+ }, # Include context for debugging
session_id=session_id,
)
@@ -179,8 +182,11 @@ class CreateAgentTool(BaseTool):
if agent_json is None:
return ErrorResponse(
- message="Failed to generate the agent. Please try again.",
- error="Generation failed",
+ message="Failed to generate the agent. The agent generation service may be unavailable or timed out. Please try again.",
+ error="generation_failed",
+ details={
+ "description": description[:100]
+ }, # Include context for debugging
session_id=session_id,
)
diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/edit_agent.py b/autogpt_platform/backend/backend/api/features/chat/tools/edit_agent.py
index df1e4a9c3e..d65b050f06 100644
--- a/autogpt_platform/backend/backend/api/features/chat/tools/edit_agent.py
+++ b/autogpt_platform/backend/backend/api/features/chat/tools/edit_agent.py
@@ -142,8 +142,9 @@ class EditAgentTool(BaseTool):
if result is None:
return ErrorResponse(
- message="Failed to generate changes. Please try rephrasing.",
- error="Update generation failed",
+ message="Failed to generate changes. The agent generation service may be unavailable or timed out. Please try again.",
+ error="update_generation_failed",
+ details={"agent_id": agent_id, "changes": changes[:100]},
session_id=session_id,
)