feat(engine): 添加技能查看工具并优化异步任务管理 - 添加SkillViewTool到引擎加载器中,增强技能管理功能 - 在AgentLoop中引入_active_direct_task来跟踪活跃任务 - 实现直接任务执行时的同步处理逻辑 - 更新工具实例化方式以支持依赖注入 feat(config): 增加智能体运行时参数配置支持 - 扩展AgentDefaultsConfig添加max_tokens和temperature字段 - 实现配置解析函数_first_config_value处理多个配置源 - 支持通过Web API动态更新智能体运行时参数 - 添加前端页面配置表单和验证逻辑 refactor(provider): 统一最大令牌数参数类型为可选整型 - 将所有LLM提供者的max_tokens参数改为int | None类型 - 为AnthropicProvider实现模型特定的最大令牌数默认值 - 调整参数传递逻辑,优先级:调用参数 > 配置文件 > 模型默认值 - 移除硬编码的默认值,改用条件判断 feat(process): 增强事件投影功能 - 添加工具调用开始/结束事件的映射逻辑 - 实现技能激活事件的识别和展示 - 添加辅助函数处理工具调用名称和参数提取 - 优化运行记录关联逻辑,提升事件匹配准确性 fix(web): 更新网络请求客户端信任环境设置 - 将WebFetchTool和WebSearchTool的trust_env参数设为True - 确保HTTP客户端能够正确使用系统代理配置 - 修复可能的网络连接问题 test: 添加配置加载和事件投影相关测试 - 新增智能体默认参数配置测试用例 - 实现API配置持久化和重载测试 - 添加技能卡片和工具事件的投影测试 ```
324 lines
11 KiB
Python
324 lines
11 KiB
Python
import json
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
from beaver.engine import AgentLoop, EngineLoader
|
|
from beaver.engine.providers import make_provider_bundle
|
|
from beaver.engine.providers.litellm import LiteLLMProvider
|
|
from beaver.foundation.config import load_config
|
|
from beaver.interfaces.web.app import create_app, _reload_agent_config
|
|
from beaver.services.agent_service import AgentService
|
|
|
|
|
|
def test_load_config_reads_current_instance_shape(tmp_path) -> None:
|
|
config_path = tmp_path / "config.json"
|
|
config_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"agents": {
|
|
"defaults": {
|
|
"workspace": str(tmp_path / "workspace"),
|
|
"model": "qwen-plus",
|
|
}
|
|
},
|
|
"providers": {
|
|
"openai": {
|
|
"apiKey": "sk-test",
|
|
"apiBase": "https://oai.example.com/v1",
|
|
"extraHeaders": {"X-Test": "1"},
|
|
}
|
|
},
|
|
"embeddingModel": "text-embedding-v4",
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
config = load_config(config_path=config_path)
|
|
target = config.resolve_provider_target()
|
|
|
|
assert config.default_model == "qwen-plus"
|
|
assert config.default_embedding_model == "text-embedding-v4"
|
|
assert target["provider_name"] == "openai"
|
|
assert target["model"] == "qwen-plus"
|
|
assert target["api_key"] == "sk-test"
|
|
assert target["api_base"] == "https://oai.example.com/v1"
|
|
assert target["extra_headers"] == {"X-Test": "1"}
|
|
|
|
|
|
def test_provider_resolution_ignores_custom_and_disabled_overrides(tmp_path) -> None:
|
|
config_path = tmp_path / "config.json"
|
|
config_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"agents": {
|
|
"defaults": {
|
|
"workspace": str(tmp_path / "workspace"),
|
|
"model": "qwen-plus",
|
|
"provider": "custom",
|
|
}
|
|
},
|
|
"providers": {
|
|
"custom": {},
|
|
"openai": {
|
|
"apiKey": "sk-test",
|
|
"apiBase": "https://oai.example.com/v1",
|
|
},
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
config = load_config(config_path=config_path)
|
|
|
|
assert config.resolve_provider_target()["provider_name"] == "openai"
|
|
assert config.resolve_provider_target(provider_name="custom")["provider_name"] == "openai"
|
|
assert config.resolve_provider_target(provider_name="deepseek")["provider_name"] == "openai"
|
|
|
|
|
|
def test_engine_loader_uses_config_workspace(tmp_path) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
config_path = tmp_path / "config.json"
|
|
config_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"agents": {
|
|
"defaults": {
|
|
"workspace": str(workspace),
|
|
"model": "qwen-plus",
|
|
}
|
|
},
|
|
"providers": {"openai": {"apiKey": "sk-test", "apiBase": "https://oai.example.com/v1"}},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
loader = EngineLoader(config_path=config_path)
|
|
assert loader.workspace == workspace
|
|
|
|
|
|
def test_agent_loop_config_drives_provider_bundle(tmp_path) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
config_path = tmp_path / "config.json"
|
|
config_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"agents": {
|
|
"defaults": {
|
|
"workspace": str(workspace),
|
|
"model": "qwen-plus",
|
|
}
|
|
},
|
|
"providers": {"openai": {"apiKey": "sk-test", "apiBase": "https://oai.example.com/v1"}},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
loop = AgentLoop(loader=EngineLoader(config_path=config_path))
|
|
loaded = loop.boot()
|
|
target = loaded.config.resolve_provider_target()
|
|
|
|
assert target["provider_name"] == "openai"
|
|
assert target["model"] == "qwen-plus"
|
|
assert target["api_key"] == "sk-test"
|
|
assert target["api_base"] == "https://oai.example.com/v1"
|
|
loop.close()
|
|
|
|
|
|
def test_reload_agent_config_updates_booted_loop_config(tmp_path) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
config_path = tmp_path / "config.json"
|
|
config_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"agents": {"defaults": {"workspace": str(workspace), "model": "old-model"}},
|
|
"providers": {"openai": {"apiKey": "sk-test", "apiBase": "https://old.example.com/v1"}},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
service = AgentService(config_path=config_path)
|
|
loaded = service.create_loop().boot()
|
|
assert loaded.config.default_model == "old-model"
|
|
|
|
config_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"agents": {"defaults": {"workspace": str(workspace), "model": "new-model"}},
|
|
"providers": {"openai": {"apiKey": "sk-test", "apiBase": "https://new.example.com/v1"}},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
_reload_agent_config(service, config_path)
|
|
|
|
target = service.create_loop().boot().config.resolve_provider_target()
|
|
assert target["model"] == "new-model"
|
|
assert target["api_base"] == "https://new.example.com/v1"
|
|
assert target["api_key"] == "sk-test"
|
|
service.close()
|
|
|
|
|
|
def test_agent_defaults_include_runtime_controls(tmp_path) -> None:
|
|
config_path = tmp_path / "config.json"
|
|
config_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"agents": {
|
|
"defaults": {
|
|
"maxTokens": 12345,
|
|
"temperature": 0.4,
|
|
"maxToolIterations": 9,
|
|
}
|
|
}
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
config = load_config(config_path=config_path)
|
|
service = AgentService(config_path=config_path)
|
|
|
|
assert config.agents_defaults.max_tokens == 12345
|
|
assert config.agents_defaults.temperature == 0.4
|
|
assert config.agents_defaults.max_tool_iterations == 9
|
|
assert service.profile.max_tokens == 12345
|
|
assert service.profile.temperature == 0.4
|
|
assert service.profile.max_tool_iterations == 9
|
|
service.close()
|
|
|
|
|
|
def test_agent_config_api_persists_and_reloads_defaults(tmp_path) -> None:
|
|
config_path = tmp_path / "config.json"
|
|
config_path.write_text(json.dumps({"agents": {"defaults": {}}}), encoding="utf-8")
|
|
service = AgentService(config_path=config_path)
|
|
app = create_app(service=service, manage_service_lifecycle=False)
|
|
|
|
with TestClient(app) as client:
|
|
response = client.post(
|
|
"/api/agent-config",
|
|
json={"max_tokens": 8192, "temperature": 0.6, "max_tool_iterations": 12},
|
|
)
|
|
status = client.get("/api/status")
|
|
|
|
saved = json.loads(config_path.read_text(encoding="utf-8"))
|
|
defaults = saved["agents"]["defaults"]
|
|
|
|
assert response.status_code == 200
|
|
assert response.json() == {"ok": True}
|
|
assert defaults["maxTokens"] == 8192
|
|
assert defaults["temperature"] == 0.6
|
|
assert defaults["maxToolIterations"] == 12
|
|
assert service.profile.max_tokens == 8192
|
|
assert service.profile.temperature == 0.6
|
|
assert service.profile.max_tool_iterations == 12
|
|
assert status.json()["max_tokens"] == 8192
|
|
assert status.json()["temperature"] == 0.6
|
|
assert status.json()["max_tool_iterations"] == 12
|
|
service.close()
|
|
|
|
|
|
def test_agent_config_api_accepts_zero_temperature_and_iterations(tmp_path) -> None:
|
|
config_path = tmp_path / "config.json"
|
|
service = AgentService(config_path=config_path)
|
|
app = create_app(service=service, manage_service_lifecycle=False)
|
|
|
|
with TestClient(app) as client:
|
|
response = client.post(
|
|
"/api/agent-config",
|
|
json={"max_tokens": None, "temperature": 0, "max_tool_iterations": 0},
|
|
)
|
|
|
|
config = load_config(config_path=config_path)
|
|
|
|
assert response.status_code == 200
|
|
assert config.agents_defaults.max_tokens is None
|
|
assert config.agents_defaults.temperature == 0
|
|
assert config.agents_defaults.max_tool_iterations == 0
|
|
assert service.profile.max_tokens is None
|
|
assert service.profile.temperature == 0
|
|
assert service.profile.max_tool_iterations == 0
|
|
service.close()
|
|
|
|
|
|
def test_openai_compatible_qwen_config_keeps_openai_provider() -> None:
|
|
bundle = make_provider_bundle(
|
|
model="qwen-plus",
|
|
provider_name="openai",
|
|
api_key="sk-test",
|
|
api_base="https://oai.example.com/v1",
|
|
)
|
|
|
|
assert bundle.main_runtime.provider_name == "openai"
|
|
assert bundle.main_runtime.api_base == "https://oai.example.com/v1"
|
|
assert isinstance(bundle.main_provider, LiteLLMProvider)
|
|
assert bundle.main_provider._resolve_model("qwen-plus") == "openai/qwen-plus"
|
|
|
|
|
|
def test_load_config_reads_mcp_authz_identity(tmp_path) -> None:
|
|
config_path = tmp_path / "beaver-home" / "config.json"
|
|
config_path.parent.mkdir()
|
|
config_path.write_text(
|
|
json.dumps(
|
|
{
|
|
"tools": {
|
|
"mcpServers": {
|
|
"outlook_mcp": {
|
|
"url": "http://10.6.80.29:8000/mcp",
|
|
"authMode": "oauth_backend_token",
|
|
"authAudience": "mcp:outlook_mcp",
|
|
"authScopes": ["list_tools", "tool:mail_list_messages"],
|
|
"toolTimeout": 60,
|
|
"sensitive": True,
|
|
}
|
|
}
|
|
},
|
|
"authz": {
|
|
"enabled": True,
|
|
"baseUrl": "http://beaver-authz-service:19090",
|
|
},
|
|
"backend_identity": {
|
|
"backend_id": "stevenli",
|
|
"client_id": "stevenli",
|
|
},
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
config = load_config(config_path=config_path)
|
|
|
|
server = config.tools.mcp_servers["outlook_mcp"]
|
|
assert server.transport == "http"
|
|
assert server.url == "http://10.6.80.29:8000/mcp"
|
|
assert server.auth_mode == "oauth_backend_token"
|
|
assert server.auth_audience == "mcp:outlook_mcp"
|
|
assert "tool:mail_list_messages" in server.auth_scopes
|
|
assert server.tool_timeout == 60
|
|
assert server.sensitive is True
|
|
|
|
assert config.authz.enabled is True
|
|
assert config.authz.base_url == "http://beaver-authz-service:19090"
|
|
assert config.backend_identity.backend_id == "stevenli"
|
|
assert config.backend_identity.client_id == "stevenli"
|
|
|
|
|
|
def test_load_config_adds_managed_local_mcp_servers(tmp_path) -> None:
|
|
config_path = tmp_path / "config.json"
|
|
config_path.write_text(
|
|
json.dumps({"tools": {"mcpServers": {}}}),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
config = load_config(config_path=config_path)
|
|
|
|
local = config.tools.mcp_servers["local_filesystem_mcp"]
|
|
assert local.transport == "stdio"
|
|
assert local.kind == "local"
|
|
assert local.category == "filesystem"
|
|
assert local.managed is True
|
|
assert "beaver.interfaces.mcp.tools_server" in local.args
|