mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-02-19 02:54:28 -05:00
- Resolves #8782 ### Changes 🏗️ - feat(frontend/library): Use WS subscription to get real-time execution updates - feat(backend/ws_api): Send `GraphExecutionUpdate` on all new agent I/O - Include agent I/O in `GraphExecutionUpdate` (by subclassing `GraphExecution`) - Add `IO_BLOCK_IDs` to `.blocks.io` - feat(backend/ws_api): Add `subscribe_graph_executions` method to WebSocket API - feat(backend): Withhold `GraphExecution.node_executions` from requests by non-graph-owners - Split `GraphExecutionWithNodes` off of `GraphExecution` - Use `GraphExecution` as much as possible, as it's a much cheaper query than `GraphExecutionWithNodes` - refactor(frontend): Make `GraphExecution.node_executions` optional - fix(frontend): Parse dates in responses of `/executions` and `/graphs/{graph_id}/executions` - refactor(frontend/library): Move sorting logic for agent runs list from `AgentRunsPage` to `AgentRunsSelectorList` - refactor(backend/ws_api): Clean up message handler implementations - refactor(backend/tests): Use `.data.execution.get_graph_execution(..)` directly instead of `AgentServer.test_get_graph_run_results(..)` Out-of-scope changes: - refactor(backend): Remove unnecessary query include from `.data.graph.get_graph_metadata(..)` Demo: https://github.com/user-attachments/assets/8ea6225d-7334-49cb-a522-83f153d840da ### Checklist 📋 #### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [x] I have tested my changes according to the test plan: - Go to `/library/agents/[id]` for an agent with inputs and outputs - Draft and run a new run - [x] -> should appear in the list of runs at the top - [x] -> should be selected as soon as the request finishes - [x] -> new I/O should appear as it is generated - [x] -> status should be updated in real-time (both in list and in adjacent details view) - Click "Run again" - [x] -> should appear in the list of runs at the top - [x] -> should be selected as soon as the request finishes - [x] -> new I/O should appear as it is generated - [x] -> status should be updated in real-time (both in list and in adjacent details view) - Click "Open in builder" under "Agent actions"; run the agent from the builder - [x] -> should work the same as before - [x] -> node I/O should appear in real-time - [x] -> node execution statuses should update in real-time
204 lines
6.2 KiB
Python
204 lines
6.2 KiB
Python
from datetime import datetime, timezone
|
|
from unittest.mock import AsyncMock
|
|
|
|
import pytest
|
|
from fastapi import WebSocket
|
|
|
|
from backend.data.execution import (
|
|
ExecutionStatus,
|
|
GraphExecutionEvent,
|
|
NodeExecutionEvent,
|
|
)
|
|
from backend.server.conn_manager import ConnectionManager
|
|
from backend.server.model import WSMessage, WSMethod
|
|
|
|
|
|
@pytest.fixture
|
|
def connection_manager() -> ConnectionManager:
|
|
return ConnectionManager()
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_websocket() -> AsyncMock:
|
|
websocket: AsyncMock = AsyncMock(spec=WebSocket)
|
|
websocket.send_text = AsyncMock()
|
|
return websocket
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_connect(
|
|
connection_manager: ConnectionManager, mock_websocket: AsyncMock
|
|
) -> None:
|
|
await connection_manager.connect_socket(mock_websocket)
|
|
assert mock_websocket in connection_manager.active_connections
|
|
mock_websocket.accept.assert_called_once()
|
|
|
|
|
|
def test_disconnect(
|
|
connection_manager: ConnectionManager, mock_websocket: AsyncMock
|
|
) -> None:
|
|
connection_manager.active_connections.add(mock_websocket)
|
|
connection_manager.subscriptions["test_channel_42"] = {mock_websocket}
|
|
|
|
connection_manager.disconnect_socket(mock_websocket)
|
|
|
|
assert mock_websocket not in connection_manager.active_connections
|
|
assert mock_websocket not in connection_manager.subscriptions["test_channel_42"]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_subscribe(
|
|
connection_manager: ConnectionManager, mock_websocket: AsyncMock
|
|
) -> None:
|
|
await connection_manager.subscribe_graph_exec(
|
|
user_id="user-1",
|
|
graph_exec_id="graph-exec-1",
|
|
websocket=mock_websocket,
|
|
)
|
|
assert (
|
|
mock_websocket
|
|
in connection_manager.subscriptions["user-1|graph_exec#graph-exec-1"]
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unsubscribe(
|
|
connection_manager: ConnectionManager, mock_websocket: AsyncMock
|
|
) -> None:
|
|
channel_key = "user-1|graph_exec#graph-exec-1"
|
|
connection_manager.subscriptions[channel_key] = {mock_websocket}
|
|
|
|
await connection_manager.unsubscribe_graph_exec(
|
|
user_id="user-1",
|
|
graph_exec_id="graph-exec-1",
|
|
websocket=mock_websocket,
|
|
)
|
|
|
|
assert "test_graph" not in connection_manager.subscriptions
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_graph_execution_result(
|
|
connection_manager: ConnectionManager, mock_websocket: AsyncMock
|
|
) -> None:
|
|
channel_key = "user-1|graph_exec#graph-exec-1"
|
|
connection_manager.subscriptions[channel_key] = {mock_websocket}
|
|
result = GraphExecutionEvent(
|
|
id="graph-exec-1",
|
|
user_id="user-1",
|
|
graph_id="test_graph",
|
|
graph_version=1,
|
|
status=ExecutionStatus.COMPLETED,
|
|
cost=0,
|
|
duration=1.2,
|
|
total_run_time=0.5,
|
|
started_at=datetime.now(tz=timezone.utc),
|
|
ended_at=datetime.now(tz=timezone.utc),
|
|
inputs={
|
|
"input_1": "some input value :)",
|
|
"input_2": "some *other* input value",
|
|
},
|
|
outputs={
|
|
"the_output": ["some output value"],
|
|
"other_output": ["sike there was another output"],
|
|
},
|
|
)
|
|
|
|
await connection_manager.send_execution_update(result)
|
|
|
|
mock_websocket.send_text.assert_called_once_with(
|
|
WSMessage(
|
|
method=WSMethod.GRAPH_EXECUTION_EVENT,
|
|
channel="user-1|graph_exec#graph-exec-1",
|
|
data=result.model_dump(),
|
|
).model_dump_json()
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_node_execution_result(
|
|
connection_manager: ConnectionManager, mock_websocket: AsyncMock
|
|
) -> None:
|
|
channel_key = "user-1|graph_exec#graph-exec-1"
|
|
connection_manager.subscriptions[channel_key] = {mock_websocket}
|
|
result = NodeExecutionEvent(
|
|
user_id="user-1",
|
|
graph_id="test_graph",
|
|
graph_version=1,
|
|
graph_exec_id="graph-exec-1",
|
|
node_exec_id="test_node_exec_id",
|
|
node_id="test_node_id",
|
|
block_id="test_block_id",
|
|
status=ExecutionStatus.COMPLETED,
|
|
input_data={"input1": "value1"},
|
|
output_data={"output1": ["result1"]},
|
|
add_time=datetime.now(tz=timezone.utc),
|
|
queue_time=None,
|
|
start_time=datetime.now(tz=timezone.utc),
|
|
end_time=datetime.now(tz=timezone.utc),
|
|
)
|
|
|
|
await connection_manager.send_execution_update(result)
|
|
|
|
mock_websocket.send_text.assert_called_once_with(
|
|
WSMessage(
|
|
method=WSMethod.NODE_EXECUTION_EVENT,
|
|
channel="user-1|graph_exec#graph-exec-1",
|
|
data=result.model_dump(),
|
|
).model_dump_json()
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_execution_result_user_mismatch(
|
|
connection_manager: ConnectionManager, mock_websocket: AsyncMock
|
|
) -> None:
|
|
channel_key = "user-1|graph_exec#graph-exec-1"
|
|
connection_manager.subscriptions[channel_key] = {mock_websocket}
|
|
result = NodeExecutionEvent(
|
|
user_id="user-2",
|
|
graph_id="test_graph",
|
|
graph_version=1,
|
|
graph_exec_id="graph-exec-1",
|
|
node_exec_id="test_node_exec_id",
|
|
node_id="test_node_id",
|
|
block_id="test_block_id",
|
|
status=ExecutionStatus.COMPLETED,
|
|
input_data={"input1": "value1"},
|
|
output_data={"output1": ["result1"]},
|
|
add_time=datetime.now(tz=timezone.utc),
|
|
queue_time=None,
|
|
start_time=datetime.now(tz=timezone.utc),
|
|
end_time=datetime.now(tz=timezone.utc),
|
|
)
|
|
|
|
await connection_manager.send_execution_update(result)
|
|
|
|
mock_websocket.send_text.assert_not_called()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_send_execution_result_no_subscribers(
|
|
connection_manager: ConnectionManager, mock_websocket: AsyncMock
|
|
) -> None:
|
|
result = NodeExecutionEvent(
|
|
user_id="user-1",
|
|
graph_id="test_graph",
|
|
graph_version=1,
|
|
graph_exec_id="test_exec_id",
|
|
node_exec_id="test_node_exec_id",
|
|
node_id="test_node_id",
|
|
block_id="test_block_id",
|
|
status=ExecutionStatus.COMPLETED,
|
|
input_data={"input1": "value1"},
|
|
output_data={"output1": ["result1"]},
|
|
add_time=datetime.now(),
|
|
queue_time=None,
|
|
start_time=datetime.now(),
|
|
end_time=datetime.now(),
|
|
)
|
|
|
|
await connection_manager.send_execution_update(result)
|
|
|
|
mock_websocket.send_text.assert_not_called()
|