feat(agents): 新增「智能体」功能模块(路线 A)+ 版本 2.5.0
老板要求「在 ai 网关里做智能体 agent,新的一个菜单」,一期仅管理员、成熟后下发。 完整设计(27 条决策,4 轮评审)见 docs/agents-dev-plan.md。 架构(路线 A):界面与权限在网关,执行在 QwenPaw(独立 profile ai-gateway)。 网关当门面(鉴权/计量/灰度/审计),引擎提供工具能力(文件/命令/浏览器)。 Step -1 技术验证(实测,全部改变或确认了设计): - 请求级指定模型【无效】:传 model 被忽略,turn_usage 回 provider_id=opencode、 model_name=deepseek-v4.1-flash;AgentRequest 无 model 字段。模型是 profile 级。 → 甲案:所有智能体共用 ai-gateway 一个 profile ⇒ 共用同一模型,配置页模型栏只读。 - role:"system"【被接受且生效】:塞入「只用英文回答」后模型真的用英文 → params.system_suffix 走注入 system 消息。 - SSE 事件谱已录制 → tests/fixtures/sse-tool-flow.sse + docs/qwenpaw-agent-api-sse.md 后端: - app/agents.py:AgentStore(data/agents.yaml,600/原子写/mtime 热载)+ 严格校验 (slug 三重查重:模型 id/别名/QwenPaw profile;params 4 键白名单;suffix 限长 2000; max_agents=5 软上限,归档不计入;restore 可恢复) - app/adapters/qwenpaw_engine.py:OpenAI 形状 ↔ AgentScope Agent API + SSE 翻译器 (text 累积、delta:false 快照忽略、plugin_call→轨迹、call_id 配对 output、turn_usage→计量) - app/agents_routes.py:GET/POST/PUT/DELETE /agents、/restore、/chat(SSE)、/stop、 /runs、/sessions、/test + 新增 GET /auth/me(灰度通道,features.agents 后端权威); 权限唯一入口 can_use_agent();防自指(引擎模型不得指向 agent/*) - app/history.py:会话类别扩展支持 agent_<slug>(复用现有存储) - data/agents.yaml(配置,入库);data/agent_runs.json(运行时,gitignore) 前端: - views/Agents.vue(列表 + 对话/配置/运行记录)+ lib/agents.ts(SSE 解析) - 菜单核心组第 5 位 + 图标 bot;router meta.admin 双层守卫;i18n 中英对称 871/871 上线实测:真实对话跑通 —— 让智能体 read_file 读 /etc/hostname,前端正确显示 「执行了 1 步」+ 工具参数、正文、计量(wjd · 47196 tokens · 缓存 13696)、终态; 引擎调用经 wangjinding 过网关(计量闭环)。 测试 209 → 217 全过(新增 8 条,含用真实夹具验证 SSE 翻译器);ci_check 全绿。
This commit is contained in:
1 parent
4c04a5ed0e
commit
c0706d1846
19 files changed
+3064
-26
No files matched your search
+30
-2
@@ -57,6 +57,8 @@ from app.history import HistoryStore, HistoryError, MAX_ITEMS as HISTORY_CAP
|
||||
from app.feedback import FeedbackStore, FeedbackError, MAX_ENTRIES as FEEDBACK_CAP
|
||||
from app.video_owners import VideoOwnerStore
|
||||
from app import settings as gw_settings
|
||||
from app.agents import AgentStore, AgentsError
|
||||
from app import agents_routes
|
||||
|
||||
# 功能 8: 读取 .env(若存在),补充 token 等环境变量。
|
||||
# 项目根 .env:不覆盖真实 env(override=False);
|
||||
@@ -85,6 +87,8 @@ else:
|
||||
CONFIG_PATH = _PROJECT_ROOT / "data" / "gateway.yaml"
|
||||
|
||||
config: GatewayConfig = load_config(CONFIG_PATH)
|
||||
# 智能体(第三十八轮):定义存 data/agents.yaml(可入库),会话复用 history
|
||||
_agents_store = AgentStore(os.environ.get("GATEWAY_AGENTS_PATH"))
|
||||
_metrics = Metrics(enabled=config.server.metrics_enabled)
|
||||
_access = AccessLog(maxlen=config.observability.access_log_maxlen)
|
||||
_limiter = RateLimiter()
|
||||
@@ -332,9 +336,9 @@ _docs_on = config.server.docs_enabled # 安全默认关(report: /docs、/ope
|
||||
|
||||
# 本轮修复: 版本号收敛到单一常量。此前 "2.4.0" 硬编码在 FastAPI / / 导航 / /auth/login
|
||||
# 三处,/health 干脆没返回版本 —— 前端「网关版本」一直显示 v-。
|
||||
# 2026-09-29: 2.4.0 → 2.4.1(前端间距规范统一:页面四周等距 + 卡片间距走令牌)。
|
||||
# 2026-09-29: 2.4.0 → 2.4.1(前端间距规范统一);2.4.1 → 2.5.0(新增「智能体」功能模块)。
|
||||
# ★ 版本号纪律:每次改动都要往上加,小改动加小版本号、大改动加大版本号(README 有专章)。
|
||||
VERSION = "2.4.1"
|
||||
VERSION = "2.5.0"
|
||||
|
||||
app = FastAPI(
|
||||
title="API Gateway",
|
||||
@@ -3211,6 +3215,30 @@ def _require_admin(request: Request) -> str:
|
||||
return account
|
||||
|
||||
|
||||
# ── 智能体端点装配(第三十八轮)───────────────────────────────────────
|
||||
def _model_ids_and_aliases() -> tuple[list[str], list[str]]:
|
||||
"""模型 id 与别名清单(供智能体 slug 查重)。"""
|
||||
cfg = _cfg()
|
||||
ids: list[str] = []
|
||||
for p in (cfg.providers or {}).values():
|
||||
for m in (p.models or []):
|
||||
ids.append(m.id)
|
||||
return ids, list((cfg.aliases or {}).keys())
|
||||
|
||||
|
||||
# 注意:_require_admin / _json_object 定义在本文件更靠后的位置 —— 用 lambda 延迟解析,
|
||||
# 避免"install 时名字还不存在"。安装只发生在 import 期,但调用发生在请求期,安全。
|
||||
agents_routes.install(
|
||||
app,
|
||||
store=_agents_store,
|
||||
require_auth=require_auth,
|
||||
require_admin=lambda req: _require_admin(req),
|
||||
json_body=lambda req: _json_object(req),
|
||||
model_index=_model_ids_and_aliases,
|
||||
history_get=lambda account, kind: _history.get(account, kind),
|
||||
)
|
||||
|
||||
|
||||
@app.get("/auth/users")
|
||||
async def auth_list_users(request: Request, _auth=Depends(require_auth)):
|
||||
_require_admin(request)
|
||||
|
||||
Reference in new issue
Block a user