mirror of
https://github.com/agent0ai/agent-zero.git
synced 2026-12-04 09:28:56 +00:00
Every send paid a blocking model_config_get round-trip before dispatching, because the frontend had to know whether a chat model is configured to decide between sending and rendering the setup card. Move the check behind the dispatch: a user_message_ui extension in _model_config flags unconfigured chats, the message handlers reject before logging or dispatching and return a model_not_configured payload, and the client opens the setup gate from that response. Isolated sends no longer make any configuration round-trip, the optimistic user row and draft handling stay intact through the gate flow, and the post-setup retry drops its bypass flag since the backend re-checks on dispatch. Validation: pytest tests/test_model_gate_backend_reject.py tests/test_welcome_composer_static.py tests/test_webui_chat_deletion.py tests/test_webui_send_pause_responsiveness_static.py tests/test_message_action_buttons_static.py tests/test_markdown_tilde_rendering.py tests/test_webui_message_queue_static.py (23 passed); node --check webui/index.js webui/components/chat/model-gate-store.js
92 lines
2.5 KiB
Python
92 lines
2.5 KiB
Python
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
|
if str(PROJECT_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
from helpers import extension
|
|
|
|
_PATCH_TARGET = "plugins._model_config.helpers.model_config.get_effective_config"
|
|
|
|
|
|
class _StubConfig:
|
|
profile = "none"
|
|
|
|
|
|
class _StubContext:
|
|
def get_data(self, _key):
|
|
return None
|
|
|
|
|
|
class _StubAgent:
|
|
config = _StubConfig()
|
|
context = _StubContext()
|
|
|
|
|
|
def test_check_chat_model_flags_unconfigured(monkeypatch):
|
|
monkeypatch.setattr(_PATCH_TARGET, lambda agent: {})
|
|
monkeypatch.setattr(
|
|
"plugins._model_config.helpers.model_config.is_chat_model_configured",
|
|
lambda config: False,
|
|
)
|
|
data = {"message": "hello", "attachment_paths": []}
|
|
asyncio.run(
|
|
extension.call_extensions_async(
|
|
"user_message_ui", agent=_StubAgent(), data=data
|
|
)
|
|
)
|
|
assert data["model_not_configured"] is True
|
|
|
|
|
|
def test_check_chat_model_passes_configured(monkeypatch):
|
|
monkeypatch.setattr(_PATCH_TARGET, lambda agent: {})
|
|
monkeypatch.setattr(
|
|
"plugins._model_config.helpers.model_config.is_chat_model_configured",
|
|
lambda config: True,
|
|
)
|
|
data = {"message": "hello", "attachment_paths": []}
|
|
asyncio.run(
|
|
extension.call_extensions_async(
|
|
"user_message_ui", agent=_StubAgent(), data=data
|
|
)
|
|
)
|
|
assert "model_not_configured" not in data
|
|
|
|
|
|
def test_check_chat_model_skips_missing_agent():
|
|
data = {"message": "hello", "attachment_paths": []}
|
|
asyncio.run(
|
|
extension.call_extensions_async("user_message_ui", agent=None, data=data)
|
|
)
|
|
assert "model_not_configured" not in data
|
|
|
|
|
|
def test_message_communicate_rejects_unconfigured(monkeypatch):
|
|
from api.message import Message
|
|
|
|
handler = Message.__new__(Message)
|
|
context = type("Ctx", (), {})()
|
|
context.get_agent = lambda: _StubAgent()
|
|
|
|
def _no_dispatch(msg):
|
|
raise AssertionError("must not dispatch")
|
|
|
|
context.communicate = _no_dispatch
|
|
handler.use_context = lambda ctxid: context
|
|
|
|
request = type("Req", (), {})()
|
|
request.content_type = "application/json"
|
|
request.get_json = lambda: {"text": "hello", "context": ""}
|
|
|
|
async def fake_call(point, agent=None, **kwargs):
|
|
data = kwargs["data"]
|
|
data["model_not_configured"] = True
|
|
|
|
monkeypatch.setattr("api.message.extension.call_extensions_async", fake_call)
|
|
|
|
task, ctx = asyncio.run(handler.communicate(input={}, request=request))
|
|
assert task is None
|
|
assert ctx is context
|