mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-01 01:10:21 +08:00
* refactor: migrate to fastapi * structure refactor * fix: pyright fix * refactor: improve error handling and public messages in plugin services * feat(api): refactor API client integration and enhance request handling - Updated API client configuration to use a dedicated HTTP client. - Introduced utility functions for generating options, queries, and form data for API requests. - Refactored multiple API methods to utilize the new utility functions for improved consistency and readability. - Renamed types for clarity and updated import statements accordingly. feat(docs): add script to update OpenAPI JSON from YAML spec - Created a Python script to convert OpenAPI YAML specification to JSON format. - The script supports customizable input and output paths. - Ensured the script handles directory creation for output paths and validates the YAML structure. * fix * feat(auth): implement rate limiting for v1 login endpoint and enhance request handling * Refactor dashboard API routers to use legacy_router for backward compatibility - Changed all instances of dashboard_router to legacy_router across multiple API modules including platform, plugins, providers, sessions, skills, stats, subagents, t2i, tools, updates, and asgi_runtime. - Updated route definitions to ensure existing endpoints remain functional under the new router structure. - Introduced support for Quart request context in asgi_runtime to enhance compatibility with existing Quart-based plugins. - Added a test case to validate the functionality of the new Quart request context handling in plugin extensions. * chore: remove cli test * fix: update dashboard tests for fastapi migration * chore: satisfy ruff checks * fix: update openapi api key scopes * fix: sync config scope chip selection * fix: restore quart dependency * docs: clarify quart plugin api compatibility * docs: update openapi scope documentation * fix: use singular skill openapi scope * fix: hide update service exception details * fix: address fastapi review comments * fix: address dashboard review findings * docs: revert unrelated package deployment changes * docs: update agent api generation guidance * feat: add plugin page web api helpers * docs: add plugin page bridge demo * fix: type plugin upload files * fix: stabilize plugin page uploads * fix: type plugin web request proxy * docs: remove plugin page docs example * fix: authenticate plugin page SSE bridge
194 lines
5.9 KiB
Python
194 lines
5.9 KiB
Python
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from astrbot.builtin_stars.builtin_commands.commands import (
|
|
conversation as conversation_module,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_clear_third_party_agent_runner_state_deletes_deerflow_thread_before_local_state(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
calls: list[object] = []
|
|
|
|
class FakeClient:
|
|
def __init__(self, **kwargs):
|
|
calls.append(("init", kwargs))
|
|
|
|
async def delete_thread(self, thread_id: str, timeout: float = 20):
|
|
calls.append(("delete", thread_id, timeout))
|
|
|
|
async def close(self):
|
|
calls.append(("close",))
|
|
|
|
async def fake_get_async(*args, **kwargs):
|
|
_ = args, kwargs
|
|
return "thread-123"
|
|
|
|
async def fake_remove_async(*args, **kwargs):
|
|
calls.append(("remove", kwargs["scope"], kwargs["scope_id"], kwargs["key"]))
|
|
|
|
context = SimpleNamespace(
|
|
get_config=lambda **kwargs: {
|
|
"provider_settings": {
|
|
"deerflow_agent_runner_provider_id": "deerflow-runner"
|
|
}
|
|
},
|
|
provider_manager=SimpleNamespace(
|
|
get_provider_config_by_id=lambda provider_id, merged=False: (
|
|
{
|
|
"id": provider_id,
|
|
"deerflow_api_base": "http://127.0.0.1:2026",
|
|
"deerflow_api_key": "token",
|
|
"deerflow_auth_header": "",
|
|
"proxy": "",
|
|
}
|
|
if merged
|
|
else {"id": provider_id}
|
|
),
|
|
),
|
|
)
|
|
|
|
monkeypatch.setattr(conversation_module, "DeerFlowAPIClient", FakeClient)
|
|
monkeypatch.setattr(conversation_module.sp, "get_async", fake_get_async)
|
|
monkeypatch.setattr(conversation_module.sp, "remove_async", fake_remove_async)
|
|
|
|
await conversation_module._clear_third_party_agent_runner_state(
|
|
context,
|
|
"umo-1",
|
|
conversation_module.DEERFLOW_PROVIDER_TYPE,
|
|
)
|
|
|
|
assert ("delete", "thread-123", 20) in calls
|
|
assert (
|
|
"remove",
|
|
"umo",
|
|
"umo-1",
|
|
conversation_module.DEERFLOW_THREAD_ID_KEY,
|
|
) in calls
|
|
assert calls.index(("delete", "thread-123", 20)) < calls.index(
|
|
("remove", "umo", "umo-1", conversation_module.DEERFLOW_THREAD_ID_KEY)
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_clear_third_party_agent_runner_state_removes_local_state_when_deerflow_cleanup_fails(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
calls: list[object] = []
|
|
|
|
class FakeClient:
|
|
def __init__(self, **kwargs):
|
|
_ = kwargs
|
|
|
|
async def delete_thread(self, thread_id: str, timeout: float = 20):
|
|
_ = thread_id, timeout
|
|
raise RuntimeError("gateway down")
|
|
|
|
async def close(self):
|
|
calls.append(("close",))
|
|
|
|
async def fake_get_async(*args, **kwargs):
|
|
_ = args, kwargs
|
|
return "thread-456"
|
|
|
|
async def fake_remove_async(*args, **kwargs):
|
|
calls.append(("remove", kwargs["scope"], kwargs["scope_id"], kwargs["key"]))
|
|
|
|
context = SimpleNamespace(
|
|
get_config=lambda **kwargs: {
|
|
"provider_settings": {
|
|
"deerflow_agent_runner_provider_id": "deerflow-runner"
|
|
}
|
|
},
|
|
provider_manager=SimpleNamespace(
|
|
get_provider_config_by_id=lambda provider_id, merged=False: (
|
|
{
|
|
"id": provider_id,
|
|
"deerflow_api_base": "http://127.0.0.1:2026",
|
|
"deerflow_api_key": "",
|
|
"deerflow_auth_header": "",
|
|
"proxy": "",
|
|
}
|
|
if merged
|
|
else {"id": provider_id}
|
|
),
|
|
),
|
|
)
|
|
|
|
monkeypatch.setattr(conversation_module, "DeerFlowAPIClient", FakeClient)
|
|
monkeypatch.setattr(conversation_module.sp, "get_async", fake_get_async)
|
|
monkeypatch.setattr(conversation_module.sp, "remove_async", fake_remove_async)
|
|
|
|
await conversation_module._clear_third_party_agent_runner_state(
|
|
context,
|
|
"umo-2",
|
|
conversation_module.DEERFLOW_PROVIDER_TYPE,
|
|
)
|
|
|
|
assert (
|
|
"remove",
|
|
"umo",
|
|
"umo-2",
|
|
conversation_module.DEERFLOW_THREAD_ID_KEY,
|
|
) in calls
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_clear_third_party_agent_runner_state_removes_local_state_when_deerflow_client_init_fails(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
):
|
|
calls: list[object] = []
|
|
|
|
class FakeClient:
|
|
def __init__(self, **kwargs):
|
|
_ = kwargs
|
|
raise RuntimeError("invalid deerflow config")
|
|
|
|
async def fake_get_async(*args, **kwargs):
|
|
_ = args, kwargs
|
|
return "thread-789"
|
|
|
|
async def fake_remove_async(*args, **kwargs):
|
|
calls.append(("remove", kwargs["scope"], kwargs["scope_id"], kwargs["key"]))
|
|
|
|
context = SimpleNamespace(
|
|
get_config=lambda **kwargs: {
|
|
"provider_settings": {
|
|
"deerflow_agent_runner_provider_id": "deerflow-runner"
|
|
}
|
|
},
|
|
provider_manager=SimpleNamespace(
|
|
get_provider_config_by_id=lambda provider_id, merged=False: (
|
|
{
|
|
"id": provider_id,
|
|
"deerflow_api_base": "http://127.0.0.1:2026",
|
|
"deerflow_api_key": "",
|
|
"deerflow_auth_header": "",
|
|
"proxy": "",
|
|
}
|
|
if merged
|
|
else {"id": provider_id}
|
|
),
|
|
),
|
|
)
|
|
|
|
monkeypatch.setattr(conversation_module, "DeerFlowAPIClient", FakeClient)
|
|
monkeypatch.setattr(conversation_module.sp, "get_async", fake_get_async)
|
|
monkeypatch.setattr(conversation_module.sp, "remove_async", fake_remove_async)
|
|
|
|
await conversation_module._clear_third_party_agent_runner_state(
|
|
context,
|
|
"umo-3",
|
|
conversation_module.DEERFLOW_PROVIDER_TYPE,
|
|
)
|
|
|
|
assert (
|
|
"remove",
|
|
"umo",
|
|
"umo-3",
|
|
conversation_module.DEERFLOW_THREAD_ID_KEY,
|
|
) in calls
|