- 在 LiteLLMProvider 中添加 "reasoning_content" 到允许的消息键集合中 - 修改 _apply_thinking_mode 方法以强制禁用思考模式,不再基于模型名称判断 - 总是设置 enable_thinking 为 False 并添加 thinking.type: disabled 配置 - 更新相关测试用例验证新的思考模式行为 fix(web): 修复非运行状态下的直接处理逻辑 - 创建 _run_web_direct 辅助函数来处理代理服务的直接提交/处理逻辑 - 当代理服务未运行时使用 process_direct 而不是 submit_direct - 更新 REST 和 WebSocket 接口以使用新的处理逻辑 - 添加相应的单元测试验证非运行状态下使用直接处理 test(config): 添加代理配置重载功能的测试 - 添加 test_reload_agent_config_updates_booted_loop_config 测试函数 - 验证配置文件更新后代理循环能够正确加载新配置 - 测试模型、API 基础地址和 API 密钥的更新 chore(frontend): 默认禁用前端思考模式偏好 - 将前端思考模式存储的默认值从 true 改为 false - 确保窗口未定义时返回 false 而不是 true - 更新本地存储缺失时的默认行为为禁用思考模式
207 lines
7.4 KiB
Python
207 lines
7.4 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from typing import Any
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from beaver.interfaces.web.app import create_app
|
|
from beaver.services.agent_service import AgentService
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class StubRunResult:
|
|
session_id: str
|
|
run_id: str = "run-1"
|
|
output_text: str = "ok"
|
|
finish_reason: str = "stop"
|
|
tool_iterations: int = 0
|
|
provider_name: str | None = "stub"
|
|
model: str | None = "stub-model"
|
|
usage: dict[str, Any] = field(default_factory=lambda: {"total_tokens": 3})
|
|
task_id: str | None = "task-1"
|
|
task_status: str | None = "awaiting_feedback"
|
|
validation_result: dict[str, Any] | None = field(default_factory=lambda: {"accepted": True})
|
|
|
|
|
|
class StubAgentService(AgentService):
|
|
def __init__(self, *, fail: bool = False) -> None:
|
|
super().__init__()
|
|
self.fail = fail
|
|
self.calls: list[dict[str, Any]] = []
|
|
|
|
async def process_direct(self, message: str, **kwargs: Any) -> StubRunResult: # type: ignore[override]
|
|
self.calls.append({"message": message, **kwargs})
|
|
if self.fail:
|
|
raise RuntimeError("boom")
|
|
return StubRunResult(
|
|
session_id=kwargs.get("session_id") or "web:default",
|
|
output_text=f"echo:{message}",
|
|
)
|
|
|
|
async def submit_direct(self, message: str, **kwargs: Any) -> StubRunResult: # type: ignore[override]
|
|
self.calls.append({"message": message, **kwargs})
|
|
if self.fail:
|
|
raise RuntimeError("boom")
|
|
return StubRunResult(
|
|
session_id=kwargs.get("session_id") or "web:default",
|
|
output_text=f"echo:{message}",
|
|
)
|
|
|
|
|
|
class DirectModeOnlyAgentService(StubAgentService):
|
|
async def submit_direct(self, message: str, **kwargs: Any) -> StubRunResult: # type: ignore[override]
|
|
raise RuntimeError("submit_direct should not be used when service is not running")
|
|
|
|
|
|
def test_websocket_ping_pong() -> None:
|
|
app = create_app(service=StubAgentService(), manage_service_lifecycle=False)
|
|
|
|
with TestClient(app) as client:
|
|
with client.websocket_connect("/ws/web:alpha") as websocket:
|
|
websocket.send_json({"type": "ping"})
|
|
assert websocket.receive_json() == {"type": "pong"}
|
|
|
|
|
|
def test_websocket_message_returns_chat_metadata_and_session_updated() -> None:
|
|
service = StubAgentService()
|
|
app = create_app(service=service, manage_service_lifecycle=False)
|
|
|
|
with TestClient(app) as client:
|
|
with client.websocket_connect("/ws/web:alpha") as websocket:
|
|
websocket.send_json(
|
|
{
|
|
"type": "message",
|
|
"content": "hello",
|
|
"metadata": {"source": "test"},
|
|
"attachments": [{"file_id": "file-1", "name": "a.txt"}],
|
|
}
|
|
)
|
|
assert websocket.receive_json() == {"type": "status", "status": "thinking"}
|
|
message = websocket.receive_json()
|
|
session_updated = websocket.receive_json()
|
|
|
|
assert service.calls == [
|
|
{
|
|
"message": "hello",
|
|
"session_id": "web:alpha",
|
|
"source": "websocket",
|
|
"user_id": None,
|
|
"title": None,
|
|
"execution_context": None,
|
|
"model": None,
|
|
"provider_name": None,
|
|
"embedding_model": None,
|
|
"max_tool_iterations": None,
|
|
}
|
|
]
|
|
assert message["type"] == "message"
|
|
assert message["role"] == "assistant"
|
|
assert message["content"] == "echo:hello"
|
|
assert message["session_id"] == "web:alpha"
|
|
assert message["run_id"] == "run-1"
|
|
assert message["task_id"] == "task-1"
|
|
assert message["task_status"] == "awaiting_feedback"
|
|
assert message["validation_result"] == {"accepted": True}
|
|
assert message["validation_status"] == "passed"
|
|
assert message["metadata"]["input_metadata"] == {
|
|
"source": "test",
|
|
"attachments": [{"file_id": "file-1", "name": "a.txt"}],
|
|
}
|
|
assert session_updated == {
|
|
"type": "session_updated",
|
|
"session_id": "web:alpha",
|
|
"source": "websocket",
|
|
}
|
|
|
|
|
|
def test_websocket_message_uses_direct_processing_when_loop_is_not_running() -> None:
|
|
service = DirectModeOnlyAgentService()
|
|
app = create_app(service=service, manage_service_lifecycle=False)
|
|
|
|
with TestClient(app) as client:
|
|
with client.websocket_connect("/ws/web:alpha") as websocket:
|
|
websocket.send_json({"type": "message", "content": "hello"})
|
|
assert websocket.receive_json() == {"type": "status", "status": "thinking"}
|
|
message = websocket.receive_json()
|
|
|
|
assert service.calls == [
|
|
{
|
|
"message": "hello",
|
|
"session_id": "web:alpha",
|
|
"source": "websocket",
|
|
"user_id": None,
|
|
"title": None,
|
|
"execution_context": None,
|
|
"model": None,
|
|
"provider_name": None,
|
|
"embedding_model": None,
|
|
"max_tool_iterations": None,
|
|
}
|
|
]
|
|
assert message["type"] == "message"
|
|
assert message["content"] == "echo:hello"
|
|
|
|
|
|
def test_rest_chat_uses_direct_processing_when_loop_is_not_running() -> None:
|
|
service = DirectModeOnlyAgentService()
|
|
app = create_app(service=service, manage_service_lifecycle=False)
|
|
|
|
with TestClient(app) as client:
|
|
response = client.post("/api/chat", json={"session_id": "web:alpha", "message": "hello"})
|
|
|
|
assert response.status_code == 200
|
|
assert service.calls == [
|
|
{
|
|
"message": "hello",
|
|
"session_id": "web:alpha",
|
|
"source": "web",
|
|
"user_id": None,
|
|
"title": None,
|
|
"execution_context": None,
|
|
"model": None,
|
|
"provider_name": None,
|
|
"embedding_model": None,
|
|
"temperature": None,
|
|
"max_tokens": None,
|
|
"max_tool_iterations": None,
|
|
"fallback_target": None,
|
|
"auxiliary_target": None,
|
|
"embedding_target": None,
|
|
}
|
|
]
|
|
assert response.json()["output_text"] == "echo:hello"
|
|
|
|
|
|
def test_websocket_empty_content_returns_error_without_runtime_call() -> None:
|
|
service = StubAgentService()
|
|
app = create_app(service=service, manage_service_lifecycle=False)
|
|
|
|
with TestClient(app) as client:
|
|
with client.websocket_connect("/ws/web:alpha") as websocket:
|
|
websocket.send_json({"type": "message", "content": " "})
|
|
assert websocket.receive_json() == {"type": "error", "error": "'content' is required"}
|
|
|
|
assert service.calls == []
|
|
|
|
|
|
def test_websocket_runtime_error_returns_assistant_error_message() -> None:
|
|
service = StubAgentService(fail=True)
|
|
app = create_app(service=service, manage_service_lifecycle=False)
|
|
|
|
with TestClient(app) as client:
|
|
with client.websocket_connect("/ws/web:alpha") as websocket:
|
|
websocket.send_json({"type": "message", "content": "hello"})
|
|
assert websocket.receive_json() == {"type": "status", "status": "thinking"}
|
|
message = websocket.receive_json()
|
|
websocket.send_json({"type": "ping"})
|
|
pong = websocket.receive_json()
|
|
|
|
assert message["type"] == "message"
|
|
assert message["role"] == "assistant"
|
|
assert message["session_id"] == "web:alpha"
|
|
assert message["finish_reason"] == "error"
|
|
assert message["tool_iterations"] == 0
|
|
assert "boom" in message["content"]
|
|
assert pong == {"type": "pong"}
|