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 a9b74ef5d5
commit a77b1fcf92
9 files changed
+428 -27

No files matched your search

+68
View File
@@ -348,3 +348,71 @@ async def check_engine(base_url: str, *, timeout: float = 5.0) -> tuple[bool, st
return False, ENGINE_DOWN_MSG
except Exception as exc: # noqa: BLE001
return False, f"引擎不可达:{exc}"
# ── 引擎模型读写(第四十轮 · 「模型可选」方案 A)────────────────────────
#
# 事实(实测 + 源码):
# - 模型不是"全局唯一",而是**每个 profile 各存一份** `active_model`
# (落盘 `workspaces/<profile>/agent.json`)。所以改它 = 只影响这一个 profile。
# - 端点前缀是 **`/api/models/active`**(`routers/providers.py` 里 `prefix="/models"`)。
# ⚠️ 别写成 `/api/providers/active` —— 那条是 **404**(本项目旧代码就踩过这个坑,
# 导致模型名一直读不出来、页面显示 `—`)。
# - 改完 **即时生效**,不用重启引擎(每轮对话都会重读 agent_config)。
# - 我们这边所有智能体**共用 `ai-gateway` 一个 profile** ⇒ 改这个 profile 的模型
# 等于"换所有智能体用的模型"。这是方案 A 的既定语义(做不到 A/B 各用各的)。
ACTIVE_MODEL_PATH = "/api/models/active"
async def get_active_model(base_url: str, agent_id: str,
*, timeout: float = 8.0) -> dict[str, Any] | None:
"""读某个 profile 当前生效的模型。返回 `{provider_id, model}`,读不到返回 None。"""
url = base_url.rstrip("/") + ACTIVE_MODEL_PATH
try:
async with httpx.AsyncClient(timeout=timeout) as client:
r = await client.get(
url,
params={"scope": "agent", "agent_id": agent_id},
headers={"X-Agent-Id": agent_id},
)
if r.status_code >= 400:
return None
slot = (r.json() or {}).get("active_llm") or {}
if not slot.get("model"):
return None
return {"provider_id": slot.get("provider_id") or "", "model": slot.get("model") or ""}
except Exception: # noqa: BLE001
return None
async def set_active_model(base_url: str, agent_id: str,
provider_id: str, model: str,
*, timeout: float = 15.0) -> dict[str, Any]:
"""改某个 profile 的模型(即时生效)。校验失败抛 EngineError。
引擎会先 `_validate_model_slot()`(供应商/模型必须存在),所以这里
直接把上游的错误原文透出去,比自己编文案准确。
"""
url = base_url.rstrip("/") + ACTIVE_MODEL_PATH
payload = {"scope": "agent", "agent_id": agent_id,
"provider_id": provider_id, "model": model}
try:
async with httpx.AsyncClient(timeout=timeout) as client:
r = await client.put(url, json=payload, headers={"X-Agent-Id": agent_id})
except httpx.ConnectError:
raise EngineError(ENGINE_DOWN_MSG)
except Exception as exc: # noqa: BLE001
raise EngineError(f"引擎不可达:{exc}")
if r.status_code >= 400:
detail = ""
try:
b = r.json()
detail = b.get("detail") or b.get("message") or ""
except Exception: # noqa: BLE001
detail = (r.text or "")[:200]
raise EngineError(f"引擎拒绝该模型:{detail or f'HTTP {r.status_code}'}")
slot = (r.json() or {}).get("active_llm") or {}
return {"provider_id": slot.get("provider_id") or provider_id,
"model": slot.get("model") or model}