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:
api-gateway committed 2026-09-29 13:22:12 +08:00
1 parent 4c04a5ed0e
commit c0706d1846
19 files changed
+3064 -26

No files matched your search

+118
View File
@@ -8,6 +8,7 @@ export default {
menuchat: 'Chat',
menuimages: 'Images',
menuvideos: 'Videos',
menuagents: 'Agents',
menumodels: 'Models & Providers',
menumetrics: 'Metrics',
menukeys: 'API Keys',
@@ -22,6 +23,7 @@ export default {
subchat: 'Vision-ready · multi-provider direct routing',
subimages: 'Params on the left, history on the right · downloads proxied by the gateway',
subvideos: '720P · 4–12s · async polling',
subagents: 'All-round assistants for admins · QwenPaw engine',
submodels: 'Providers / models / protocol lines',
submetrics: 'Usage & cost · isolated per account',
subkeys: 'Up to 3 per account · stored in plaintext, copyable anytime',
@@ -366,6 +368,122 @@ export default {
submitMsg: 'Submitted task {id}',
loadErr: 'Failed to load videos',
},
agents: {
title: 'Agents',
sub: 'Route A: UI in the gateway, engine is QwenPaw',
subHint: '{n} agents · engine {engine} ({url}) · {scope}',
scopeAdmin: 'Admins only',
scopeOpen: 'Available to {n}',
new: 'New',
newAgent: 'New agent',
empty: 'No agents yet',
emptyPick: 'Pick an agent on the left',
emptyHint: 'Or create one — a "model + params + own session" assistant',
tabChat: 'Chat',
tabConfig: 'Config',
tabRuns: 'Runs',
pickAgent: 'Pick an agent on the left',
engineBadge: 'QwenPaw engine',
statusPublished: 'Published',
statusDraft: 'Draft',
statusRetired: 'Archived',
newChat: 'New chat',
thisChat: 'This chat',
engineReady: 'Engine ready',
engineDown: 'Engine unreachable',
engineDownHint: 'QwenPaw is not running — start it on the server first',
retry: 'Retry',
stop: 'Stop',
stopRun: 'Background',
running: 'Generating',
inputPlaceholder: 'Say something… this agent can read/write files and run commands on the server',
execSteps: 'Ran {n} steps',
execRunning: 'Running',
tokens: 'Tokens',
elapsed: 'Elapsed',
toolsUsed: 'Tools',
interrupted: '⚠ Engine connection lost, this turn is incomplete',
cfgBasic: 'Basics',
cfgBasicSub: 'Name and slug are editable',
cfgName: 'Name',
cfgSlug: 'slug',
cfgSlugHint: 'Used as session kind agent_{slug}; must not clash with model id / alias / QwenPaw profile',
cfgDesc: 'Description',
cfgModel: 'Model',
cfgModelSub: 'From the gateway models',
cfgModelReadonly: 'Model is set by QwenPaw profile {profile}',
cfgModelHint: 'Engine model is a profile-level setting — all agents share one model. Change it in QwenPaw:',
cfgOpenQwenpaw: 'Open QwenPaw',
cfgExec: 'Execution',
cfgExecSub: 'Affects server safety, read-only for now',
cfgEngine: 'Engine',
cfgWorkspace: 'Workspace',
cfgApproval: 'Approval level',
cfgSandbox: 'Sandbox',
cfgSandboxOff: 'off',
cfgSandboxHint: 'sandbox_enabled=false — tools run directly on the server',
cfgParams: 'Parameters',
cfgTemp: 'temperature',
cfgMaxTokens: 'max_tokens',
cfgTopP: 'top_p',
cfgSuffix: 'Role suffix (system_suffix)',
cfgSuffixHint: 'Appended on top of QwenPaw main prompt (max 2000 chars)',
cfgCapability: 'Capability',
cfgCapabilitySub: '27 built-in tools',
cfgCapabilityHint: 'Decided by QwenPaw profile ai-gateway · fully enabled (files, commands, browser)',
cfgAccess: 'Access',
cfgAccessSub: 'Gradual rollout',
cfgAccessAdmin: 'Admins only',
cfgAccessHint: 'Phase 2 adds per-user / per-role rollout here',
cfgReadonly: 'read-only',
cfgPromptTitle: 'Prompt',
cfgPromptHint: 'Prompts live in QwenPaw (SOUL.md / AGENTS.md); the gateway does not own them',
save: 'Save',
saved: 'Saved',
test: 'Test run',
testing: 'Testing…',
testOk: 'OK ({ms}ms)',
testFail: 'Failed',
archive: 'Archive',
restore: 'Restore',
archived: 'Archived',
showRetired: 'Show archived',
limitReached: 'Limit reached ({n})',
limitHint: 'Limit reached ({n}); archive unused agents first',
sharedNote: 'All agents share one engine — models and params are per-agent, prompts and memory are shared',
riskNote: 'This agent can read/write files and run commands on the server',
runsTotal: 'Calls',
runsTools: 'Tool calls',
runsTokens: 'tokens',
runsFail: 'Failure rate',
runsLast7: 'Last 7 days',
runsDetail: 'Run detail',
runsDetailSub: 'Who used it and when',
colTime: 'Time',
colAccount: 'Account',
colRounds: 'Rounds',
colTools: 'Tool calls',
colTokens: 'tokens',
colMs: 'Elapsed',
colStatus: 'Status',
runOk: 'OK',
runStopped: 'Stopped',
runTimeout: 'Timeout',
runDefaultTitle: 'Chat',
cacheHit: 'cache {n}',
meLabel: 'Me',
aiLabel: 'Assistant',
runsEmpty: 'No runs yet',
formName: 'Name',
formSlug: 'slug',
formDesc: 'Description',
formTemp: 'temperature (optional)',
formSuffix: 'Role suffix (optional)',
create: 'Create',
cancel: 'Cancel',
confirmDelete: 'Archive this agent? (not deleted, can be restored)',
deleteOk: 'Archived',
},
models: {
modelListTitle: 'Model list',
name: 'Name',
+123
View File
@@ -8,6 +8,7 @@ export default {
menuchat: '对话',
menuimages: '图像',
menuvideos: '视频',
menuagents: '智能体',
menumodels: '模型与供应商',
menumetrics: '指标',
menukeys: 'API 密钥',
@@ -22,6 +23,7 @@ export default {
subchat: '支持附图视觉 · 多供应商直连',
subimages: '参数在左 · 历史在右 · 下载走网关代拉',
subvideos: '720P · 时长 4–12 秒 · 异步轮询',
subagents: '给管理员用的全能助手 · 引擎 QwenPaw',
submodels: '供应商 / 模型 / 协议线',
submetrics: '用量与成本 · 按账号隔离',
subkeys: '每个账号最多 3 把 · 明文保存,可反复复制',
@@ -366,6 +368,127 @@ export default {
submitMsg: '已提交任务 {id}',
loadErr: '加载视频失败',
},
agents: {
title: '智能体',
sub: '路线 A:界面在网关,引擎是 QwenPaw',
subHint: '{n} 个智能体 · 引擎 {engine}({url})· {scope}',
scopeAdmin: '仅管理员可见',
scopeOpen: '{n} 人可用',
new: '新建',
newAgent: '新建智能体',
empty: '还没有智能体',
emptyPick: '从左侧选一个智能体',
emptyHint: '或新建一个 —— 它就是「带模型的助手」:引擎 + 参数 + 独立会话',
tabChat: '对话',
tabConfig: '配置',
tabRuns: '运行记录',
pickAgent: '左侧选一个智能体',
// 列表卡片
engineBadge: 'QwenPaw 引擎',
statusPublished: '已发布',
statusDraft: '草稿',
statusRetired: '已归档',
// 对话
newChat: '新建对话',
thisChat: '本条对话',
engineReady: '引擎就绪',
engineDown: '引擎不可达',
engineDownHint: 'QwenPaw 未运行或未就绪 —— 请先在服务器上启动它',
retry: '重试连接',
stop: '停止',
stopRun: '转后台',
running: '生成中',
inputPlaceholder: '说点什么… 这个智能体能读写文件、跑命令、开浏览器(都在服务器上)',
execSteps: '执行了 {n} 步',
execRunning: '执行中',
tokens: 'Tokens',
elapsed: '耗时',
toolsUsed: '工具',
interrupted: '⚠ 引擎连接中断,本轮未完成',
// 配置
cfgBasic: '基本',
cfgBasicSub: '显示名与 slug 可改',
cfgName: '名称',
cfgSlug: 'slug',
cfgSlugHint: '用于会话类别 agent_{slug};不能与模型 id / 别名 / QwenPaw profile 重名',
cfgDesc: '描述',
cfgModel: '模型',
cfgModelSub: '从网关的模型里选',
cfgModelReadonly: '模型由 QwenPaw 的 {profile} profile 决定',
cfgModelHint: '引擎的模型是 profile 级设置 —— 所有智能体共用同一个模型。要改请去 QwenPaw:',
cfgOpenQwenpaw: '打开 QwenPaw',
cfgExec: '执行',
cfgExecSub: '影响服务器安全,一期只读',
cfgEngine: '引擎',
cfgWorkspace: '工作区',
cfgApproval: '审批等级',
cfgSandbox: '沙箱',
cfgSandboxOff: '关',
cfgSandboxHint: '当前 sandbox_enabled=false —— 工具直接在服务器上跑',
cfgParams: '参数',
cfgTemp: 'temperature',
cfgMaxTokens: 'max_tokens',
cfgTopP: 'top_p',
cfgSuffix: '角色补充(system_suffix)',
cfgSuffixHint: '这段是智能体的角色补充,与 QwenPaw 的主提示词叠加生效(上限 2000 字)',
cfgCapability: '能力',
cfgCapabilitySub: '27 项内置工具',
cfgCapabilityHint: '能力由 QwenPaw 的 ai-gateway profile 决定 · 一期全开(可读写文件、执行命令、开浏览器)',
cfgAccess: '访问范围',
cfgAccessSub: '灰度下发',
cfgAccessAdmin: '仅管理员',
cfgAccessHint: '二期这里会出现「指定用户 / 按角色」——先把人加进来内测,稳了再放开',
cfgReadonly: '只读',
cfgPromptTitle: '提示词',
cfgPromptHint: '提示词归 QwenPaw 管(改 SOUL.md / AGENTS.md);网关这边不接管',
save: '保存',
saved: '已保存',
test: '试跑',
testing: '试跑中…',
testOk: '通({ms}ms)',
testFail: '不通',
archive: '归档',
restore: '恢复',
archived: '已归档',
showRetired: '显示已归档',
limitReached: '已达上限({n} 个)',
limitHint: '已达上限 {n} 个(软上限);需要更多时先归档不用的',
sharedNote: '智能体共用同一个引擎 —— 模型与参数各自独立,但提示词与记忆共用',
riskNote: '该智能体可在服务器上读写文件、执行命令',
// 运行记录
runsTotal: '调用次数',
runsTools: '工具调用',
runsTokens: 'tokens',
runsFail: '失败率',
runsLast7: '最近 7 天',
runsDetail: '运行明细',
runsDetailSub: '谁在什么时候用了它',
colTime: '时间',
colAccount: '账号',
colRounds: '轮次',
colTools: '工具调用',
colTokens: 'tokens',
colMs: '耗时',
colStatus: '状态',
runOk: '成功',
runStopped: '已停止',
runTimeout: '超时',
runDefaultTitle: '对话',
cacheHit: '缓存 {n}',
meLabel: '我',
aiLabel: '助手',
runsEmpty: '还没有运行记录',
// 表单
formName: '名称',
formSlug: 'slug',
formDesc: '描述',
formTemp: 'temperature(可选)',
formSuffix: '角色补充(可选)',
create: '创建',
cancel: '取消',
confirmDelete: '确定归档这个智能体吗?(不物理删除,可恢复)',
deleteOk: '已归档',
},
models: {
modelListTitle: '模型清单',
name: '名称',