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 bccb72bad5
commit 51c9af59c4
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',