feat: 支持 agent 运行取消功能

This commit is contained in:
qzl
2026-03-25 18:33:25 +08:00
parent 599c597e69
commit 96fc4a1e77
21 changed files with 778 additions and 85 deletions
@@ -17,6 +17,7 @@ from v1.users.dependencies import get_current_user
class _FakeAgentService:
def __init__(self) -> None:
self._stream_called = False
self.cancel_calls: list[tuple[str, str, str]] = []
async def enqueue_run(
self,
@@ -102,6 +103,16 @@ class _FakeAgentService:
"url": "https://signed.example/temp-url.png",
}
async def cancel_run(
self,
*,
thread_id: str,
run_id: str,
current_user: CurrentUser,
) -> SimpleNamespace:
self.cancel_calls.append((thread_id, run_id, str(current_user.id)))
return SimpleNamespace(thread_id=thread_id, run_id=run_id, accepted=True)
class _FailingStreamAgentService(_FakeAgentService):
async def stream_events(
@@ -306,6 +317,29 @@ def test_stream_rejects_invalid_last_event_id() -> None:
app.dependency_overrides = {}
def test_cancel_run_returns_202_and_payload() -> None:
service = _FakeAgentService()
app.dependency_overrides[get_agent_service] = lambda: service
app.dependency_overrides[get_current_user] = lambda: CurrentUser(
id=uuid4(), phone="+8613812345678"
)
client = TestClient(app)
try:
response = client.post(
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/cancel",
params={"runId": "run-99"},
)
assert response.status_code == 202
payload = response.json()
assert payload["threadId"] == "00000000-0000-0000-0000-000000000001"
assert payload["runId"] == "run-99"
assert payload["accepted"] is True
assert service.cancel_calls
finally:
app.dependency_overrides = {}
def test_history_returns_state_snapshot() -> None:
app.dependency_overrides[get_agent_service] = lambda: _FakeAgentService()
client = TestClient(app)