feat(agents): 模型改为页面可选(方案 A)+ 修引擎模型读不到的静默 bug(版本 2.5.4 → 2.6.0)

推翻旧结论:模型不是全局唯一,而是每个 profile 各存一份 active_model
(PUT /api/models/active scope=agent → workspaces/<profile>/agent.json),
改完即时生效不用重启 —— 实测切模型后真对话 turn_usage 的 model_name 确实变了。

真 bug:refresh_engine_live() 读的是 /api/providers/active,那条是 404,
异常被吞 → engine_live['model'] 恒为空串 → 配置页模型栏一直显示「—」。
改用 ACTIVE_MODEL_PATH=/api/models/active + get_active_model()。

新增:
- 适配器 get_active_model / set_active_model(4xx 抛 EngineError 并透传引擎原文)
- GET /agents/engine/models(只列真能用的;实时 current,不走 TTL 缓存)
- PUT /agents/engine/model(带防自指护栏,不得指向 agent/*)
- 前端配置页模型栏:只读 → 可搜索下拉(按供应商分组 + 视觉标)+ 刷新;失败回滚
- i18n 删过期键、加 5 新键(中英 883/883 对称)
- 测试 +5(端点/路径常量回归/body 与头/4xx 抛错/防自指),224 passed

语义:方案 A = 所有智能体共用这一个模型(一个 profile 一个 active_model),
做不到 A 用 flash 且 B 用 pro 并存;那需方案 B(一智能体一 profile)。
This commit is contained in:
api-gateway committed 2026-09-30 14:40:58 +08:00
1 parent a92fc3c788
commit 678b984b95
9 files changed
+428 -27

No files matched your search

+105
View File
@@ -5967,6 +5967,111 @@ def test_agents_routes_installed():
assert need in paths, f"缺少端点 {need}"
def test_agents_engine_model_endpoints_installed():
"""引擎模型端点已挂上(第四十轮 · 方案 A)。"""
import app.main as m
paths = {r.path for r in m.app.routes if hasattr(r, "path")}
for need in ("/agents/engine/models", "/agents/engine/model"):
assert need in paths, f"缺少端点 {need}"
def test_engine_active_model_path_is_models_not_providers():
"""回归:读/写引擎 active_model 必须用 `/api/models/active`(第四十轮)。
旧代码写的是 `/api/providers/active` —— 那条**实测是 404**,导致
`engine_live["model"]` 一直是空串、配置页模型栏永远显示 `—`。
这是"静默失效"型 bug:不报错、不抛异常,只是值一直是空。
"""
from app.adapters import qwenpaw_engine as qe
assert qe.ACTIVE_MODEL_PATH == "/api/models/active"
assert "providers/active" not in qe.ACTIVE_MODEL_PATH
# 两个函数都要用这个常量(别再各写一份字面量)
import inspect
for fn in (qe.get_active_model, qe.set_active_model):
src = inspect.getsource(fn)
assert "ACTIVE_MODEL_PATH" in src, f"{fn.__name__} 未复用 ACTIVE_MODEL_PATH"
def test_engine_set_active_model_sends_agent_scope():
"""PUT 的 body 必须是 scope=agent + agent_id(profile 级写入)。"""
import asyncio, json as _json
from app.adapters import qwenpaw_engine as qe
captured = {}
class _Resp:
status_code = 200
def json(self):
return {"active_llm": {"provider_id": "wangjinding", "model": "wjd"}}
class _Client:
def __init__(self, *a, **k): pass
async def __aenter__(self): return self
async def __aexit__(self, *a): return False
async def put(self, url, json=None, headers=None):
captured["url"] = url
captured["json"] = json
captured["headers"] = headers
return _Resp()
orig = qe.httpx.AsyncClient
qe.httpx.AsyncClient = _Client
try:
out = asyncio.run(qe.set_active_model("http://127.0.0.1:8088", "ai-gateway",
"wangjinding", "wjd"))
finally:
qe.httpx.AsyncClient = orig
assert captured["url"].endswith("/api/models/active"), captured["url"]
assert captured["json"] == {"scope": "agent", "agent_id": "ai-gateway",
"provider_id": "wangjinding", "model": "wjd"}
assert captured["headers"].get("X-Agent-Id") == "ai-gateway"
assert out == {"provider_id": "wangjinding", "model": "wjd"}
def test_engine_set_active_model_raises_on_4xx():
"""引擎拒绝(如模型不存在)→ 抛 EngineError,错误原文透传(别吞成 ok)。"""
import asyncio
from app.adapters import qwenpaw_engine as qe
class _Resp:
status_code = 400
text = ""
def json(self):
return {"detail": "model 'nope' not found"}
class _Client:
def __init__(self, *a, **k): pass
async def __aenter__(self): return self
async def __aexit__(self, *a): return False
async def put(self, url, json=None, headers=None): return _Resp()
orig = qe.httpx.AsyncClient
qe.httpx.AsyncClient = _Client
try:
try:
asyncio.run(qe.set_active_model("http://x", "ai-gateway", "p", "nope"))
raise AssertionError("本应抛 EngineError")
except qe.EngineError as e:
assert "not found" in str(e), str(e)
finally:
qe.httpx.AsyncClient = orig
def test_agents_engine_set_model_rejects_agent_self_reference():
"""防自指:引擎模型不能指向 agent/*(否则引擎→智能体→引擎 死循环)。
这条护栏在对话端点也有;新增的切换端点必须同样拦住,否则用户可以从
模型下拉里把引擎指向智能体自身,把服务打成死循环。
"""
import inspect
import app.agents_routes as ar
src = inspect.getsource(ar.install)
# 切换端点里必须有这条检查(与对话入口同款)
assert "agent/" in src and "死循环" in src, "切换端点缺少防自指护栏"
def test_qwenpaw_engine_image_content_passthrough():
"""图片附件:内容块必须原样转成 AgentScope 的 image 形状(第三十九轮)。