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
@@ -32,6 +32,12 @@ data/feedback_media/
|
||||
data/sessions.json
|
||||
data/video_owners.json
|
||||
|
||||
# 智能体运行记录(第三十八轮):运行时数据(谁在什么时候用了哪个智能体),
|
||||
# 与 sessions.json 同理 —— 可重新生成、不该进版本库。
|
||||
# 注意 data/agents.yaml 是**配置**(不含秘密),按计划 ⑯ **要入库**。
|
||||
data/agent_runs.json
|
||||
data/agents.yaml.bak
|
||||
|
||||
# 配置自动备份(每存一次网关配置就多一对):不该跟着源码走,也不该在仓库里堆历史版本
|
||||
data/*.bak
|
||||
|
||||
|
||||
@@ -1,5 +1,51 @@
|
||||
# CHANGELOG
|
||||
|
||||
## 2026-09-29 · 新增「智能体」功能模块(路线 A:界面在网关,引擎 QwenPaw)
|
||||
|
||||
老板要求:「在 ai 网关里做智能体 agent,新的一个菜单」。**一期只对管理员开放**,成熟后下发普通用户。
|
||||
完整设计(27 条决策、经 4 轮评审)见 `docs/agents-dev-plan.md`。
|
||||
|
||||
**架构(路线 A)**:界面与权限在网关,执行在 QwenPaw(独立 profile `ai-gateway`)。
|
||||
网关当门面(鉴权/计量/灰度/审计),引擎提供服务端工具能力(文件/命令/浏览器)。
|
||||
|
||||
**关键技术验证(Step -1,全部实测)**:
|
||||
- **请求级指定模型无效** —— 打 `/api/console/chat` 传 `model:"agnes-3.0-flash"` 被忽略,
|
||||
`turn_usage` 明文回 `provider_id=opencode, model_name=deepseek-v4.1-flash`;源码
|
||||
`AgentRequest` 无 model 字段。模型是 **profile 级**(`PUT /api/providers/active`,`scope=agent`)。
|
||||
→ **甲案**:所有智能体共用 `ai-gateway` 一个 profile ⇒ **共用同一个模型**,配置页模型栏只读。
|
||||
- **`role:"system"` 被接受且生效** —— 塞入「只用英文回答」后模型真的改用英文
|
||||
→ `params.system_suffix` 走**注入 system 消息**(最干净的方案)。
|
||||
- **SSE 事件谱已录制**:夹具 `tests/fixtures/sse-tool-flow.sse`(14,900 字节 / 57 事件),
|
||||
文档 `docs/qwenpaw-agent-api-sse.md`(完整事件谱 + 适配器映射表)。
|
||||
|
||||
**新增**:
|
||||
- `app/agents.py` —— `AgentStore`(`data/agents.yaml`,600/原子写/mtime 热载)+ 严格校验:
|
||||
slug 形态与**三重查重**(模型 id / 别名 / QwenPaw profile)、`params` **4 键白名单**
|
||||
(`temperature`/`max_tokens`/`top_p`/`system_suffix`,结构性键一律拒)、`system_suffix` 限长 2000、
|
||||
`max_agents: 5` 软上限(归档不计入)、归档后 `restore` 可恢复。
|
||||
- `app/adapters/qwenpaw_engine.py` —— 引擎适配器:OpenAI 形状 ↔ AgentScope Agent API、
|
||||
**SSE 翻译器**(`text` 累积、`delta:false` 快照忽略、`plugin_call`→轨迹步骤、`plugin_call_output`
|
||||
用 `call_id` 配对、`turn_usage`→计量、`response`→终态)、引擎不可达/超时/中断的明确文案。
|
||||
- `app/agents_routes.py` —— 端点:`GET/POST/PUT/DELETE /agents`、`/agents/{slug}/restore`、
|
||||
`/chat`(SSE)、`/stop`、`/runs`、`/sessions`、`/test`(试跑)+ **`GET /auth/me`**(灰度通道,
|
||||
下发 `features.agents` —— 后端是唯一权威)。运行记录 `data/agent_runs.json`。
|
||||
权限判定唯一入口 `can_use_agent()`;**防自指**(引擎模型不得指向 `agent/*`,否则死循环)。
|
||||
- 前端 `views/Agents.vue`(列表 + 对话/配置/运行记录三 Tab)+ `lib/agents.ts`(含 SSE 解析);
|
||||
菜单核心组第 5 位、图标 `bot`;i18n 中英对称(871/871)。
|
||||
- `app/history.py` 扩展:会话类别支持 `agent_<slug>`(**复用**现有存储,不新开端点)。
|
||||
|
||||
**契约要点**:智能体 = 名称/描述 + **参数** + 独立会话历史。智能体之间**共用**引擎 profile、
|
||||
模型、提示词与记忆(决策 ⑨ 已确认接受);提示词归 QwenPaw 管(配置页只读 + 指路)。
|
||||
|
||||
**上线实测**:`/agents` 建/改/查重/上限全部正确;**真实对话跑通** —— 让智能体用 `read_file`
|
||||
读 `/etc/hostname`,前端正确显示「执行了 1 步」+ 工具参数、正文、计量
|
||||
(`wjd · 47196 tokens · 缓存 13696`)、终态;引擎调用经 `wangjinding` **过网关**(计量闭环)。
|
||||
|
||||
**测试**:209 → **217 全过**(新增 8 条:存储校验/数量上限/归档恢复、history kind 扩展、
|
||||
**SSE 翻译器用真实夹具验证**、`system_suffix` 注入、运行记录、权限矩阵、防自指、端点装配)。
|
||||
`ci_check.sh` 全绿。版本 **2.4.1 → 2.5.0**。
|
||||
|
||||
|
||||
## 2026-09-29 · Dots AI 纳入每日模型清单同步
|
||||
|
||||
老板要求:「把 https://dots.ai/platform/docs 加入每日定时任务的同步任务来」。
|
||||
|
||||
@@ -0,0 +1,320 @@
|
||||
"""QwenPaw 引擎适配器(第三十八轮 · 路线 A)。
|
||||
|
||||
把网关的对话请求转成 QwenPaw 的 **AgentScope Agent API** 形状,再把它的 SSE 事件流
|
||||
翻译回网关前端认识的形式。
|
||||
|
||||
协议事实全部来自实测 —— 见 `docs/qwenpaw-agent-api-sse.md` 与
|
||||
`tests/fixtures/sse-tool-flow.sse`。要点:
|
||||
|
||||
- `POST {base}/api/console/chat`,body `{input: [Message], session_id, stream: true}`
|
||||
- 头 `X-Agent-Id: <profile>` 选择 QwenPaw 侧用哪个智能体 profile
|
||||
- 回 SSE(前缀固定 `data: `,**没有 event: 行**)
|
||||
- 事件:`text` / `plugin_call` / `plugin_call_output` / `data` / `message` / `response` / `turn_usage`
|
||||
- `role: "system"` 的输入消息**被接受且生效**(实测)→ `system_suffix` 走这条
|
||||
- `model` 字段**被忽略**(实测)→ 模型是 profile 级,不在请求里指定
|
||||
|
||||
网关对前端的输出形状(自定义、简单):
|
||||
|
||||
```jsonc
|
||||
{"type": "text", "text": "增量"} // 正文增量
|
||||
{"type": "step", "id": "...", "name": "read_file", "args": {...}, "status": "running"}
|
||||
{"type": "step", "id": "...", "status": "done", "ms": 320, "output": "..."}
|
||||
{"type": "usage", "prompt": 1, "completion": 2, "cached": 0, "model": "wjd", "context_ratio": 0.02}
|
||||
{"type": "done", "status": "completed"}
|
||||
{"type": "error", "message": "..."}
|
||||
```
|
||||
|
||||
这样前端不必理解 AgentScope 的事件细节。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
import httpx
|
||||
|
||||
log = logging.getLogger("api-gateway.agents")
|
||||
|
||||
#: 单轮对话的超时(秒)。引擎可能跑很久(多步工具),给足。
|
||||
DEFAULT_TIMEOUT = 300.0
|
||||
|
||||
#: 引擎不可达时的兜底文案。
|
||||
ENGINE_DOWN_MSG = "引擎不可达(QwenPaw 未运行或未就绪)"
|
||||
|
||||
|
||||
class EngineError(Exception):
|
||||
"""引擎层错误(映射为 502)。"""
|
||||
|
||||
|
||||
# ── 请求构造 ──────────────────────────────────────────────────────────
|
||||
def build_input(messages: list[dict[str, Any]], system_suffix: str = "") -> list[dict[str, Any]]:
|
||||
"""把网关侧的消息列表转成 AgentScope 的 `input`。
|
||||
|
||||
- 网关历史里的 role/文本 → `{role, content:[{type:"text", text}]}`
|
||||
- `system_suffix` → 在最前面插一条 **system 消息**(实测被接受且生效)
|
||||
- `system` 角色的历史消息也照传(引擎接受)
|
||||
"""
|
||||
out: list[dict[str, Any]] = []
|
||||
if system_suffix:
|
||||
out.append({
|
||||
"role": "system",
|
||||
"content": [{"type": "text", "text": system_suffix}],
|
||||
})
|
||||
for m in messages:
|
||||
role = (m.get("role") or "user").lower()
|
||||
if role not in ("user", "assistant", "system"):
|
||||
role = "user"
|
||||
content = m.get("content")
|
||||
# content 允许是字符串或 [{type:text,text}] 形状
|
||||
if isinstance(content, str):
|
||||
parts = [{"type": "text", "text": content}]
|
||||
elif isinstance(content, list):
|
||||
parts = []
|
||||
for c in content:
|
||||
if isinstance(c, str):
|
||||
parts.append({"type": "text", "text": c})
|
||||
elif isinstance(c, dict) and c.get("type") == "text":
|
||||
parts.append({"type": "text", "text": str(c.get("text") or "")})
|
||||
# 图片等一期不支持,静默跳过(计划「一期不做图片/附件」)
|
||||
else:
|
||||
parts = [{"type": "text", "text": str(content or "")}]
|
||||
out.append({"role": role, "content": parts})
|
||||
return out
|
||||
|
||||
|
||||
def build_body(messages: list[dict[str, Any]], session_id: str,
|
||||
system_suffix: str = "") -> dict[str, Any]:
|
||||
return {
|
||||
"input": build_input(messages, system_suffix),
|
||||
"session_id": session_id,
|
||||
"stream": True,
|
||||
}
|
||||
|
||||
|
||||
# ── SSE 解析与翻译 ────────────────────────────────────────────────────
|
||||
def parse_sse_line(line: str) -> dict[str, Any] | None:
|
||||
"""把一行 SSE 解析成事件 dict;非数据行返回 None。"""
|
||||
if not line.startswith("data:"):
|
||||
return None
|
||||
payload = line[5:].strip()
|
||||
if not payload or payload == "[DONE]":
|
||||
return None
|
||||
try:
|
||||
obj = json.loads(payload)
|
||||
except json.JSONDecodeError:
|
||||
log.debug("引擎 SSE 非 JSON 行(忽略):%.120s", payload)
|
||||
return None
|
||||
return obj if isinstance(obj, dict) else None
|
||||
|
||||
|
||||
class StreamTranslator:
|
||||
"""把引擎的事件序列翻译成网关前端的消息序列(有状态)。
|
||||
|
||||
状态:
|
||||
- `_text_buf`: 已累积的正文(用来在结束时兜底补全)
|
||||
- `_steps`: call_id → 步骤信息(配对 plugin_call / plugin_call_output)
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.text_parts: list[str] = []
|
||||
self.steps: dict[str, dict[str, Any]] = {}
|
||||
self.usage: dict[str, Any] | None = None
|
||||
self.final_status: str = "completed"
|
||||
self._step_started: dict[str, float] = {}
|
||||
|
||||
# ── 单事件 → 若干前端消息 ──
|
||||
def feed(self, ev: dict[str, Any], now: float | None = None) -> list[dict[str, Any]]:
|
||||
import time
|
||||
now = now or time.time()
|
||||
t = ev.get("type")
|
||||
out: list[dict[str, Any]] = []
|
||||
|
||||
if t == "text":
|
||||
# delta:true 是增量;delta:false 是整段快照 —— 忽略,否则会重复
|
||||
if ev.get("delta") is False:
|
||||
return out
|
||||
piece = ev.get("text")
|
||||
if isinstance(piece, str) and piece:
|
||||
self.text_parts.append(piece)
|
||||
out.append({"type": "text", "text": piece})
|
||||
return out
|
||||
|
||||
if t == "plugin_call":
|
||||
info = _extract_call(ev, key="arguments")
|
||||
if info:
|
||||
cid = info.get("call_id") or info.get("name") or "?"
|
||||
self.steps[cid] = info
|
||||
self._step_started[cid] = now
|
||||
out.append({
|
||||
"type": "step", "id": cid, "name": info.get("name"),
|
||||
"args": info.get("args"), "status": "running",
|
||||
})
|
||||
return out
|
||||
|
||||
if t == "plugin_call_output":
|
||||
info = _extract_call(ev, key="output")
|
||||
if info:
|
||||
cid = info.get("call_id") or info.get("name") or "?"
|
||||
started = self._step_started.pop(cid, None)
|
||||
ms = int((now - started) * 1000) if started else None
|
||||
if cid in self.steps:
|
||||
self.steps[cid]["status"] = "done"
|
||||
msg: dict[str, Any] = {"type": "step", "id": cid, "status": "done"}
|
||||
if ms is not None:
|
||||
msg["ms"] = ms
|
||||
if info.get("output") is not None:
|
||||
msg["output"] = _clip(info["output"])
|
||||
out.append(msg)
|
||||
return out
|
||||
|
||||
if t == "turn_usage":
|
||||
u = ev.get("usage") or {}
|
||||
self.usage = u
|
||||
ctx = ev.get("context_usage") or {}
|
||||
out.append({
|
||||
"type": "usage",
|
||||
"prompt": int(u.get("prompt_tokens") or 0),
|
||||
"completion": int(u.get("completion_tokens") or 0),
|
||||
"cached": int(u.get("cache_read_tokens") or 0),
|
||||
"model": u.get("model_name") or "",
|
||||
"provider": u.get("provider_id") or "",
|
||||
"context_ratio": ctx.get("context_usage_ratio"),
|
||||
})
|
||||
return out
|
||||
|
||||
if t == "response":
|
||||
st = ev.get("status")
|
||||
if st:
|
||||
self.final_status = st
|
||||
if st in ("failed", "cancelled"):
|
||||
out.append({"type": "done", "status": st})
|
||||
return out
|
||||
|
||||
# data / message 等其余事件:忽略(分片由快照覆盖)
|
||||
return out
|
||||
|
||||
def finish(self) -> list[dict[str, Any]]:
|
||||
"""流结束时收尾。"""
|
||||
out: list[dict[str, Any]] = []
|
||||
if self.final_status not in ("failed", "cancelled"):
|
||||
out.append({"type": "done", "status": self.final_status})
|
||||
elif self.final_status == "cancelled":
|
||||
out.append({"type": "done", "status": "cancelled"})
|
||||
return out
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
return "".join(self.text_parts)
|
||||
|
||||
|
||||
def _extract_call(ev: dict[str, Any], *, key: str) -> dict[str, Any] | None:
|
||||
"""从 plugin_call / plugin_call_output 里取 {call_id, name, args|output}。
|
||||
|
||||
结构(实测):`content[0].data = {call_id, name, arguments|output}`
|
||||
注意 `arguments` 是 **JSON 字符串**,要二次解析。
|
||||
"""
|
||||
content = ev.get("content")
|
||||
if not isinstance(content, list):
|
||||
return None
|
||||
for c in content:
|
||||
if not isinstance(c, dict):
|
||||
continue
|
||||
data = c.get("data")
|
||||
if not isinstance(data, dict) or key not in data:
|
||||
continue
|
||||
info: dict[str, Any] = {
|
||||
"call_id": data.get("call_id"),
|
||||
"name": data.get("name"),
|
||||
}
|
||||
raw = data.get(key)
|
||||
if key == "arguments":
|
||||
info["args"] = _maybe_json(raw)
|
||||
else:
|
||||
info["output"] = raw
|
||||
return info
|
||||
return None
|
||||
|
||||
|
||||
def _maybe_json(v: Any) -> Any:
|
||||
"""`arguments` 是 JSON 字符串 → 解析成对象;解析不了就原样返回字符串。"""
|
||||
if not isinstance(v, str):
|
||||
return v
|
||||
try:
|
||||
return json.loads(v)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
return v
|
||||
|
||||
|
||||
def _clip(v: Any, limit: int = 4000) -> Any:
|
||||
"""工具输出可能很长 —— 转发给前端前裁一刀(避免 SSE 帧过大)。"""
|
||||
if isinstance(v, str) and len(v) > limit:
|
||||
return v[:limit] + "…(已截断)"
|
||||
return v
|
||||
|
||||
|
||||
# ── 调用引擎 ──────────────────────────────────────────────────────────
|
||||
async def stream_chat(base_url: str, agent_id: str, messages: list[dict[str, Any]],
|
||||
session_id: str, *, system_suffix: str = "",
|
||||
timeout: float = DEFAULT_TIMEOUT) -> AsyncIterator[dict[str, Any]]:
|
||||
"""打引擎并产出"已翻译"的前端消息。
|
||||
|
||||
用法:
|
||||
async for msg in stream_chat(...): # msg 就是 {type: text|step|usage|done|error}
|
||||
yield sse(msg)
|
||||
"""
|
||||
url = base_url.rstrip("/") + "/api/console/chat"
|
||||
body = build_body(messages, session_id, system_suffix)
|
||||
tr = StreamTranslator()
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "text/event-stream",
|
||||
"X-Agent-Id": agent_id,
|
||||
}
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=httpx.Timeout(timeout, connect=10.0)) as client:
|
||||
async with client.stream("POST", url, headers=headers, json=body) as resp:
|
||||
if resp.status_code >= 400:
|
||||
raw = (await resp.aread()).decode("utf-8", "replace")[:300]
|
||||
yield {"type": "error",
|
||||
"message": f"引擎返回 {resp.status_code}:{raw}"}
|
||||
return
|
||||
async for line in resp.aiter_lines():
|
||||
ev = parse_sse_line(line)
|
||||
if ev is None:
|
||||
continue
|
||||
for msg in tr.feed(ev):
|
||||
yield msg
|
||||
for msg in tr.finish():
|
||||
yield msg
|
||||
except httpx.ConnectError:
|
||||
yield {"type": "error", "message": ENGINE_DOWN_MSG}
|
||||
except httpx.ReadTimeout:
|
||||
yield {"type": "error", "message": "引擎响应超时(本轮未完成)"}
|
||||
except httpx.RemoteProtocolError:
|
||||
# 引擎中途断开 —— 保留已收到的内容(前端自己累积着),只提示中断
|
||||
for msg in tr.finish():
|
||||
pass
|
||||
yield {"type": "error", "message": "引擎连接中断,本轮未完成"}
|
||||
except Exception as exc: # noqa: BLE001 —— 兜底,别让流把异常抛给客户端
|
||||
log.exception("引擎调用失败")
|
||||
yield {"type": "error", "message": f"引擎调用失败:{exc}"}
|
||||
|
||||
|
||||
async def check_engine(base_url: str, *, timeout: float = 5.0) -> tuple[bool, str]:
|
||||
"""探活:引擎是否可达。返回 (ok, 说明)。
|
||||
|
||||
不引 `/api/healthz`(那是 QwenPaw 的)—— 用最轻的请求确认端口活着。
|
||||
"""
|
||||
url = base_url.rstrip("/") + "/api/healthz"
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
r = await client.get(url)
|
||||
if r.status_code < 500:
|
||||
return True, "就绪"
|
||||
return False, f"引擎异常(HTTP {r.status_code})"
|
||||
except httpx.ConnectError:
|
||||
return False, ENGINE_DOWN_MSG
|
||||
except Exception as exc: # noqa: BLE001
|
||||
return False, f"引擎不可达:{exc}"
|
||||
+521
@@ -0,0 +1,521 @@
|
||||
"""智能体(Agent)定义:存储 + 校验(第三十八轮,路线 A)。
|
||||
|
||||
背景与设计见 `docs/agents-dev-plan.md`。要点:
|
||||
|
||||
- **路线 A**:界面在网关,引擎是 QwenPaw(独立 profile `ai-gateway`)。
|
||||
- **甲案**:模型是 profile 级的(请求级指定无效,已实测),而所有智能体共用
|
||||
`ai-gateway` 一个 profile ⇒ **所有智能体共用同一个模型**。所以这里**不存 model**。
|
||||
- 智能体之间的区别只剩:名称/描述 + 参数(temperature/max_tokens/top_p/system_suffix)
|
||||
+ 各自的会话历史。
|
||||
- 提示词归 QwenPaw 管(决策②),这里**不存 system_prompt** 主提示词;
|
||||
`params.system_suffix` 只是一小条角色补充(网关侧注入)。
|
||||
|
||||
存储:`data/agents.yaml`(600、原子写、mtime 热载)。**配置无秘密,可入库**。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import yaml
|
||||
|
||||
# ── 常量 ──────────────────────────────────────────────────────────────
|
||||
DEFAULT_PATH = Path(__file__).resolve().parent.parent / "data" / "agents.yaml"
|
||||
|
||||
#: slug 规则:小写字母开头,允许小写字母/数字/连字符,2~32 位
|
||||
_SLUG_RE = re.compile(r"^[a-z][a-z0-9-]{1,31}$")
|
||||
|
||||
#: 智能体级参数白名单(决策 ㉗)。
|
||||
#: 只放"采样/长度类"和一小条角色后缀 —— 结构性键(model/messages/stream/tools)刻意排除,
|
||||
#: 防止用户从这儿绕过网关往上游塞结构。
|
||||
AGENT_PARAM_KEYS: frozenset[str] = frozenset({
|
||||
"temperature", "max_tokens", "top_p", "system_suffix",
|
||||
})
|
||||
|
||||
#: system_suffix 长度上限(字符)。它是"角色微调",不是主提示词通道 ——
|
||||
#: 要写长提示词请去 QwenPaw 建 profile(决策 ㉖ 的边界)。
|
||||
SYSTEM_SUFFIX_MAX = 2000
|
||||
|
||||
#: 未归档智能体的数量上限(决策 ⑫,软上限)。
|
||||
DEFAULT_MAX_AGENTS = 5
|
||||
|
||||
#: 允许的访问角色(二期下发用;一期固定 admin)。
|
||||
ALLOWED_ROLES = frozenset({"admin", "member"})
|
||||
|
||||
#: 允许的状态。
|
||||
ALLOWED_STATUS = frozenset({"draft", "published", "retired"})
|
||||
|
||||
#: 引擎类型(一期只有 qwenpaw 一种)。
|
||||
ENGINE_TYPES = frozenset({"qwenpaw"})
|
||||
|
||||
#: 默认引擎地址(QwenPaw 本机,免鉴权)。
|
||||
DEFAULT_ENGINE_URL = "http://127.0.0.1:8088"
|
||||
|
||||
#: 默认引擎 profile id。
|
||||
DEFAULT_ENGINE_AGENT = "ai-gateway"
|
||||
|
||||
|
||||
class AgentsError(ValueError):
|
||||
"""智能体配置校验失败(映射为 400)。"""
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
return time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
|
||||
# ── 校验辅助 ──────────────────────────────────────────────────────────
|
||||
def _require_str(value: Any, where: str, *, maxlen: int = 200) -> str:
|
||||
if not isinstance(value, str):
|
||||
raise AgentsError(f"{where} 必须是字符串")
|
||||
v = value.strip()
|
||||
if not v:
|
||||
raise AgentsError(f"{where} 不能为空")
|
||||
if len(v) > maxlen:
|
||||
raise AgentsError(f"{where} 过长(上限 {maxlen} 字符)")
|
||||
return v
|
||||
|
||||
|
||||
def validate_slug(slug: Any) -> str:
|
||||
"""校验 slug 形态(唯一性由调用方另行校验)。"""
|
||||
if not isinstance(slug, str) or not _SLUG_RE.match(slug):
|
||||
raise AgentsError(
|
||||
"slug 只能是小写字母开头、由小写字母/数字/连字符组成,长度 2~32(如 code-review)"
|
||||
)
|
||||
return slug
|
||||
|
||||
|
||||
def validate_params(params: Any) -> dict[str, Any]:
|
||||
"""校验智能体级参数:只认白名单,类型不对就报错(不静默忽略)。"""
|
||||
if params is None:
|
||||
return {}
|
||||
if not isinstance(params, dict):
|
||||
raise AgentsError("params 必须是映射(键值对)")
|
||||
out: dict[str, Any] = {}
|
||||
for k, v in params.items():
|
||||
if k not in AGENT_PARAM_KEYS:
|
||||
raise AgentsError(
|
||||
f"params.{k} 不是允许的智能体参数(允许:{', '.join(sorted(AGENT_PARAM_KEYS))})"
|
||||
)
|
||||
if k == "system_suffix":
|
||||
if not isinstance(v, str):
|
||||
raise AgentsError("params.system_suffix 必须是字符串")
|
||||
if len(v) > SYSTEM_SUFFIX_MAX:
|
||||
raise AgentsError(
|
||||
f"params.system_suffix 过长(上限 {SYSTEM_SUFFIX_MAX} 字符)——"
|
||||
"它是角色补充,主提示词请到 QwenPaw 的 profile 里改"
|
||||
)
|
||||
out[k] = v.strip()
|
||||
continue
|
||||
# temperature / top_p:浮点;max_tokens:整数
|
||||
if isinstance(v, bool) or not isinstance(v, (int, float)):
|
||||
raise AgentsError(f"params.{k} 必须是数字")
|
||||
if k == "max_tokens":
|
||||
iv = int(v)
|
||||
if iv <= 0:
|
||||
raise AgentsError("params.max_tokens 必须大于 0")
|
||||
out[k] = iv
|
||||
else:
|
||||
fv = float(v)
|
||||
if k == "temperature" and not (0.0 <= fv <= 2.0):
|
||||
raise AgentsError("params.temperature 取值范围 0~2")
|
||||
if k == "top_p" and not (0.0 < fv <= 1.0):
|
||||
raise AgentsError("params.top_p 取值范围 (0, 1]")
|
||||
out[k] = fv
|
||||
return out
|
||||
|
||||
|
||||
# ── 数据模型 ──────────────────────────────────────────────────────────
|
||||
@dataclass
|
||||
class Agent:
|
||||
slug: str
|
||||
display_name: str
|
||||
description: str = ""
|
||||
params: dict[str, Any] = field(default_factory=dict)
|
||||
engine_type: str = "qwenpaw"
|
||||
engine_base_url: str = DEFAULT_ENGINE_URL
|
||||
engine_agent_id: str = DEFAULT_ENGINE_AGENT
|
||||
access_roles: list[str] = field(default_factory=lambda: ["admin"])
|
||||
access_users: list[str] = field(default_factory=list)
|
||||
status: str = "published"
|
||||
created_at: str = ""
|
||||
updated_at: str = ""
|
||||
|
||||
# ── 序列化 ──
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"slug": self.slug,
|
||||
"display_name": self.display_name,
|
||||
"description": self.description,
|
||||
"params": dict(self.params),
|
||||
"engine": {
|
||||
"type": self.engine_type,
|
||||
"base_url": self.engine_base_url,
|
||||
"agent_id": self.engine_agent_id,
|
||||
},
|
||||
"access": {"roles": list(self.access_roles), "users": list(self.access_users)},
|
||||
"status": self.status,
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
}
|
||||
|
||||
def public_dict(self) -> dict[str, Any]:
|
||||
"""给前端列表/详情用的字段(含引擎实况位,由调用方补)。
|
||||
|
||||
注意:**不含** system_suffix 之外的敏感内容 —— 由路由层按角色再裁。
|
||||
"""
|
||||
d = self.to_dict()
|
||||
d["engine_type"] = self.engine_type
|
||||
d["access_scope"] = "admin" if self.access_roles == ["admin"] and not self.access_users else "custom"
|
||||
return d
|
||||
|
||||
|
||||
def _parse_agent(slug: str, raw: Any) -> Agent:
|
||||
"""从 YAML 的一段解析出一个 Agent(严格校验,报错就抛)。"""
|
||||
where = f"agents.{slug}"
|
||||
validate_slug(slug)
|
||||
if not isinstance(raw, dict):
|
||||
raise AgentsError(f"{where} 必须是一个映射")
|
||||
|
||||
# 不接受别名之外的未知键(防拼错后静默失效)
|
||||
known = {"display_name", "name", "description", "params", "engine",
|
||||
"access", "status", "created_at", "updated_at"}
|
||||
unknown = set(raw) - known
|
||||
if unknown:
|
||||
raise AgentsError(f"{where} 含未知字段:{', '.join(sorted(unknown))}")
|
||||
|
||||
display_name = _require_str(
|
||||
raw.get("display_name") or raw.get("name"), f"{where}.display_name", maxlen=60
|
||||
)
|
||||
description = raw.get("description") or ""
|
||||
if not isinstance(description, str):
|
||||
raise AgentsError(f"{where}.description 必须是字符串")
|
||||
description = description.strip()[:200]
|
||||
|
||||
params = validate_params(raw.get("params"))
|
||||
|
||||
eng = raw.get("engine") or {}
|
||||
if not isinstance(eng, dict):
|
||||
raise AgentsError(f"{where}.engine 必须是映射")
|
||||
etype = eng.get("type", "qwenpaw")
|
||||
if etype not in ENGINE_TYPES:
|
||||
raise AgentsError(
|
||||
f"{where}.engine.type 只支持 {'/'.join(sorted(ENGINE_TYPES))}(当前:{etype})"
|
||||
)
|
||||
base_url = _require_str(eng.get("base_url") or DEFAULT_ENGINE_URL,
|
||||
f"{where}.engine.base_url", maxlen=200)
|
||||
agent_id = _require_str(eng.get("agent_id") or DEFAULT_ENGINE_AGENT,
|
||||
f"{where}.engine.agent_id", maxlen=64)
|
||||
|
||||
acc = raw.get("access") or {}
|
||||
if not isinstance(acc, dict):
|
||||
raise AgentsError(f"{where}.access 必须是映射")
|
||||
roles = acc.get("roles") or ["admin"]
|
||||
users = acc.get("users") or []
|
||||
if not isinstance(roles, list) or not all(isinstance(r, str) for r in roles):
|
||||
raise AgentsError(f"{where}.access.roles 必须是字符串列表")
|
||||
bad = set(roles) - ALLOWED_ROLES
|
||||
if bad:
|
||||
raise AgentsError(
|
||||
f"{where}.access.roles 含非法角色:{', '.join(sorted(bad))}"
|
||||
f"(允许:{', '.join(sorted(ALLOWED_ROLES))})"
|
||||
)
|
||||
if not isinstance(users, list) or not all(isinstance(u, str) for u in users):
|
||||
raise AgentsError(f"{where}.access.users 必须是字符串列表")
|
||||
|
||||
status = raw.get("status", "published")
|
||||
if status not in ALLOWED_STATUS:
|
||||
raise AgentsError(
|
||||
f"{where}.status 非法:{status}(允许:{', '.join(sorted(ALLOWED_STATUS))})"
|
||||
)
|
||||
|
||||
return Agent(
|
||||
slug=slug,
|
||||
display_name=display_name,
|
||||
description=description,
|
||||
params=params,
|
||||
engine_type=etype,
|
||||
engine_base_url=base_url,
|
||||
engine_agent_id=agent_id,
|
||||
access_roles=list(roles),
|
||||
access_users=list(u.strip() for u in users if u.strip()),
|
||||
status=status,
|
||||
created_at=str(raw.get("created_at") or ""),
|
||||
updated_at=str(raw.get("updated_at") or ""),
|
||||
)
|
||||
|
||||
|
||||
# ── 存储 ──────────────────────────────────────────────────────────────
|
||||
class AgentStore:
|
||||
"""`data/agents.yaml` 的读写与校验。
|
||||
|
||||
- 原子写(临时文件 + rename)+ 备份(复用 settings.write_config 的语义)
|
||||
- mtime 热载:文件被改后自动重读,不必重启
|
||||
- 线程锁:读写都加锁,单进程够用
|
||||
"""
|
||||
|
||||
def __init__(self, path: str | Path | None = None) -> None:
|
||||
self.path = Path(path) if path else DEFAULT_PATH
|
||||
self._lock = threading.RLock()
|
||||
self._agents: dict[str, Agent] = {}
|
||||
self._max_agents: int = DEFAULT_MAX_AGENTS
|
||||
self._mtime: float = 0.0
|
||||
self._load()
|
||||
|
||||
# ── 读 ──
|
||||
def _load(self, *, force: bool = False) -> None:
|
||||
with self._lock:
|
||||
if not self.path.exists():
|
||||
self._agents, self._max_agents = {}, DEFAULT_MAX_AGENTS
|
||||
self._mtime = 0.0
|
||||
return
|
||||
mtime = self.path.stat().st_mtime
|
||||
if not force and mtime == self._mtime:
|
||||
return
|
||||
raw = yaml.safe_load(self.path.read_text(encoding="utf-8")) or {}
|
||||
if not isinstance(raw, dict):
|
||||
raise AgentsError("agents.yaml 顶层必须是映射")
|
||||
unknown = set(raw) - {"max_agents", "agents", "version"}
|
||||
if unknown:
|
||||
raise AgentsError(f"agents.yaml 含未知顶层键:{', '.join(sorted(unknown))}")
|
||||
mx = raw.get("max_agents", DEFAULT_MAX_AGENTS)
|
||||
if isinstance(mx, bool) or not isinstance(mx, int) or mx < 1:
|
||||
raise AgentsError("max_agents 必须是 ≥1 的整数")
|
||||
items = raw.get("agents") or {}
|
||||
if not isinstance(items, dict):
|
||||
raise AgentsError("agents 段必须是映射(slug → 定义)")
|
||||
agents: dict[str, Agent] = {}
|
||||
for slug, body in items.items():
|
||||
agents[str(slug)] = _parse_agent(str(slug), body)
|
||||
self._agents = agents
|
||||
self._max_agents = int(mx)
|
||||
self._mtime = mtime
|
||||
|
||||
def reload_if_changed(self) -> bool:
|
||||
"""文件变了就重读(供每请求或热载循环调用)。返回是否发生了重载。"""
|
||||
try:
|
||||
if self.path.exists() and self.path.stat().st_mtime != self._mtime:
|
||||
self._load(force=True)
|
||||
return True
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return False
|
||||
|
||||
# ── 查询 ──
|
||||
@property
|
||||
def max_agents(self) -> int:
|
||||
with self._lock:
|
||||
return self._max_agents
|
||||
|
||||
def list(self, *, include_retired: bool = False) -> list[Agent]:
|
||||
with self._lock:
|
||||
out = [a for a in self._agents.values()
|
||||
if include_retired or a.status != "retired"]
|
||||
out.sort(key=lambda a: (a.status == "retired", a.created_at or "", a.slug))
|
||||
return out
|
||||
|
||||
def get(self, slug: str) -> Agent | None:
|
||||
with self._lock:
|
||||
return self._agents.get(slug)
|
||||
|
||||
def active_count(self) -> int:
|
||||
with self._lock:
|
||||
return sum(1 for a in self._agents.values() if a.status != "retired")
|
||||
|
||||
def slug_conflicts(self, slug: str, *, model_ids: list[str] | None = None,
|
||||
alias_names: list[str] | None = None,
|
||||
profile_ids: list[str] | None = None) -> str | None:
|
||||
"""返回冲突说明;无冲突返回 None。
|
||||
|
||||
三重查重(计划 Step 1):不能撞已有智能体 slug、模型 id、别名、QwenPaw profile id ——
|
||||
因为智能体以后可能以 `agent/<slug>` 形式暴露给客户端,撞名会混。
|
||||
"""
|
||||
if slug in self._agents:
|
||||
return f"slug「{slug}」已被占用"
|
||||
if model_ids and slug in model_ids:
|
||||
return f"slug「{slug}」与模型 id 冲突"
|
||||
if alias_names and slug in alias_names:
|
||||
return f"slug「{slug}」与虚拟模型别名冲突"
|
||||
if profile_ids and slug in profile_ids:
|
||||
return f"slug「{slug}」与 QwenPaw 智能体 profile 冲突"
|
||||
return None
|
||||
|
||||
# ── 写 ──
|
||||
def _dump(self) -> str:
|
||||
"""渲染成 YAML 文本(顶层 max_agents + agents 段)。"""
|
||||
body: dict[str, Any] = {"max_agents": self._max_agents, "agents": {}}
|
||||
for slug in sorted(self._agents):
|
||||
a = self._agents[slug]
|
||||
body["agents"][slug] = {
|
||||
"display_name": a.display_name,
|
||||
"description": a.description,
|
||||
"params": dict(a.params),
|
||||
"engine": {
|
||||
"type": a.engine_type,
|
||||
"base_url": a.engine_base_url,
|
||||
"agent_id": a.engine_agent_id,
|
||||
},
|
||||
"access": {"roles": list(a.access_roles), "users": list(a.access_users)},
|
||||
"status": a.status,
|
||||
"created_at": a.created_at,
|
||||
"updated_at": a.updated_at,
|
||||
}
|
||||
head = (
|
||||
"# 智能体定义(第三十八轮 · 路线 A:界面在网关,引擎是 QwenPaw)\n"
|
||||
"#\n"
|
||||
"# 说明:\n"
|
||||
"# · 所有智能体共用 QwenPaw 的同一个 profile(engine.agent_id),\n"
|
||||
"# 也共用同一个模型 —— 模型是 profile 级设置(请求级指定无效,已实测)。\n"
|
||||
"# · 提示词主档归 QwenPaw 管(改 SOUL.md / AGENTS.md);\n"
|
||||
"# 这里的 params.system_suffix 只是一小条角色补充。\n"
|
||||
"# · 本文件不含秘密,可入版本库。\n"
|
||||
"# · 归档(status: retired)不计入 max_agents 上限。\n"
|
||||
)
|
||||
return head + yaml.safe_dump(body, allow_unicode=True, sort_keys=False, width=100)
|
||||
|
||||
def _write(self, text: str) -> None:
|
||||
"""原子写 + 备份(逐字节替换,权限 600)。"""
|
||||
p = self.path
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
if p.exists():
|
||||
try:
|
||||
import shutil
|
||||
shutil.copy2(p, p.with_name(p.name + ".bak"))
|
||||
except OSError:
|
||||
pass
|
||||
tmp = p.with_name(p.name + f".tmp{os.getpid()}")
|
||||
tmp.write_text(text, encoding="utf-8")
|
||||
os.chmod(tmp, 0o600)
|
||||
os.replace(tmp, p)
|
||||
self._mtime = p.stat().st_mtime
|
||||
|
||||
def _commit(self) -> None:
|
||||
self._write(self._dump())
|
||||
|
||||
def create(self, *, slug: str, display_name: str, description: str = "",
|
||||
params: dict[str, Any] | None = None,
|
||||
access_roles: list[str] | None = None,
|
||||
access_users: list[str] | None = None,
|
||||
engine_base_url: str | None = None,
|
||||
engine_agent_id: str | None = None,
|
||||
status: str = "published",
|
||||
model_ids: list[str] | None = None,
|
||||
alias_names: list[str] | None = None,
|
||||
profile_ids: list[str] | None = None) -> Agent:
|
||||
with self._lock:
|
||||
self._load() # 先取最新盘上状态
|
||||
validate_slug(slug)
|
||||
conflict = self.slug_conflicts(
|
||||
slug, model_ids=model_ids, alias_names=alias_names, profile_ids=profile_ids
|
||||
)
|
||||
if conflict:
|
||||
raise AgentsError(conflict)
|
||||
if self.active_count() >= self._max_agents:
|
||||
raise AgentsError(
|
||||
f"智能体数量已达上限({self._max_agents} 个)——请先归档不用的再新建"
|
||||
)
|
||||
a = _parse_agent(slug, {
|
||||
"display_name": display_name,
|
||||
"description": description,
|
||||
"params": params or {},
|
||||
"engine": {
|
||||
"type": "qwenpaw",
|
||||
"base_url": engine_base_url or DEFAULT_ENGINE_URL,
|
||||
"agent_id": engine_agent_id or DEFAULT_ENGINE_AGENT,
|
||||
},
|
||||
"access": {"roles": access_roles or ["admin"], "users": access_users or []},
|
||||
"status": status,
|
||||
"created_at": _now(),
|
||||
"updated_at": _now(),
|
||||
})
|
||||
self._agents[slug] = a
|
||||
self._commit()
|
||||
return a
|
||||
|
||||
#: 允许通过 update 改的字段(其余字段一期只读,见计划)
|
||||
UPDATABLE = frozenset({"display_name", "description", "params", "status"})
|
||||
|
||||
def update(self, slug: str, patch: dict[str, Any]) -> Agent:
|
||||
with self._lock:
|
||||
self._load()
|
||||
a = self._agents.get(slug)
|
||||
if a is None:
|
||||
raise AgentsError(f"智能体「{slug}」不存在")
|
||||
unknown = set(patch) - self.UPDATABLE
|
||||
if unknown:
|
||||
raise AgentsError(
|
||||
f"字段不可改:{', '.join(sorted(unknown))}"
|
||||
f"(一期可改:{', '.join(sorted(self.UPDATABLE))})"
|
||||
)
|
||||
# 逐项覆盖到"可被 _parse_agent 接受的形状"(不含 slug —— 它是键不是字段)
|
||||
merged: dict[str, Any] = {
|
||||
"display_name": patch.get("display_name", a.display_name),
|
||||
"description": patch.get("description", a.description),
|
||||
"params": patch.get("params", dict(a.params)),
|
||||
"engine": {
|
||||
"type": a.engine_type,
|
||||
"base_url": a.engine_base_url,
|
||||
"agent_id": a.engine_agent_id,
|
||||
},
|
||||
"access": {"roles": list(a.access_roles), "users": list(a.access_users)},
|
||||
"status": patch.get("status", a.status),
|
||||
"created_at": a.created_at,
|
||||
"updated_at": _now(),
|
||||
}
|
||||
new = _parse_agent(slug, merged)
|
||||
self._agents[slug] = new
|
||||
self._commit()
|
||||
return new
|
||||
|
||||
def retire(self, slug: str) -> Agent:
|
||||
"""归档(不物理删 —— 项目惯例:归档优于删除)。"""
|
||||
with self._lock:
|
||||
self._load()
|
||||
a = self._agents.get(slug)
|
||||
if a is None:
|
||||
raise AgentsError(f"智能体「{slug}」不存在")
|
||||
a.status = "retired"
|
||||
a.updated_at = _now()
|
||||
self._commit()
|
||||
return a
|
||||
|
||||
def restore(self, slug: str) -> Agent:
|
||||
"""从归档恢复(决策 ⑰)。"""
|
||||
with self._lock:
|
||||
self._load()
|
||||
a = self._agents.get(slug)
|
||||
if a is None:
|
||||
raise AgentsError(f"智能体「{slug}」不存在")
|
||||
if a.status != "retired":
|
||||
raise AgentsError(f"智能体「{slug}」未归档,无需恢复")
|
||||
if self.active_count() >= self._max_agents:
|
||||
raise AgentsError(f"智能体数量已达上限({self._max_agents}),无法恢复")
|
||||
a.status = "published"
|
||||
a.updated_at = _now()
|
||||
self._commit()
|
||||
return a
|
||||
|
||||
|
||||
#: 进程级单例(延迟到首次用时构造,方便测试换路径)
|
||||
_store: AgentStore | None = None
|
||||
_store_lock = threading.Lock()
|
||||
|
||||
|
||||
def get_store() -> AgentStore:
|
||||
global _store
|
||||
with _store_lock:
|
||||
if _store is None:
|
||||
_store = AgentStore(os.environ.get("GATEWAY_AGENTS_PATH"))
|
||||
return _store
|
||||
|
||||
|
||||
def set_store(store: AgentStore | None) -> None:
|
||||
"""测试用:替换/重置单例。"""
|
||||
global _store
|
||||
with _store_lock:
|
||||
_store = store
|
||||
@@ -0,0 +1,406 @@
|
||||
"""智能体路由 + 运行记录(第三十八轮 · 路线 A)。
|
||||
|
||||
设计见 `docs/agents-dev-plan.md`;SSE 协议见 `docs/qwenpaw-agent-api-sse.md`。
|
||||
|
||||
**为什么单开一个文件**:`main.py` 已经 3800+ 行。智能体是一块相对独立的特性,
|
||||
把端点实现放这里、由 `main.py` 装配(`install(app, deps)`),比继续往 main 里塞更清楚。
|
||||
|
||||
会话列表**复用** `/history/{kind}`(`kind = agent_<slug>`),见 `app/history.py`。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Awaitable
|
||||
|
||||
import httpx
|
||||
from fastapi import Depends, FastAPI, HTTPException, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from app.agents import (
|
||||
DEFAULT_ENGINE_AGENT,
|
||||
DEFAULT_ENGINE_URL,
|
||||
Agent,
|
||||
AgentStore,
|
||||
AgentsError,
|
||||
)
|
||||
from app.adapters.qwenpaw_engine import check_engine, stream_chat
|
||||
|
||||
log = logging.getLogger("api-gateway.agents")
|
||||
|
||||
QWENPAW_CONFIG = Path("~/.qwenpaw/config.json")
|
||||
|
||||
|
||||
# ── 运行记录(data/agent_runs.json)────────────────────────────────────
|
||||
class AgentRuns:
|
||||
"""每轮对话一条记录 + 每智能体的汇总位。
|
||||
|
||||
结构:`{"<slug>": {"last_used_at": str, "total": int, "items": [...]}}`
|
||||
条目:`{at, account, rounds, tools, tokens, ms, status}`(新→旧)
|
||||
|
||||
写入失败只告警 —— 记录不该影响对话本身。
|
||||
"""
|
||||
|
||||
MAX_ITEMS = 200
|
||||
|
||||
def __init__(self, path: str | Path | None = None) -> None:
|
||||
self.path = Path(path) if path else \
|
||||
Path(__file__).resolve().parent.parent / "data" / "agent_runs.json"
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def _read(self) -> dict:
|
||||
try:
|
||||
if self.path.exists():
|
||||
data = json.loads(self.path.read_text(encoding="utf-8"))
|
||||
return data if isinstance(data, dict) else {}
|
||||
except Exception: # noqa: BLE001
|
||||
log.warning("读 agent_runs.json 失败,按空处理")
|
||||
return {}
|
||||
|
||||
def _write(self, data: dict) -> None:
|
||||
try:
|
||||
tmp = self.path.with_name(self.path.name + f".tmp{os.getpid()}")
|
||||
tmp.write_text(json.dumps(data, ensure_ascii=False), encoding="utf-8")
|
||||
os.chmod(tmp, 0o600)
|
||||
os.replace(tmp, self.path)
|
||||
except Exception: # noqa: BLE001
|
||||
log.warning("写 agent_runs.json 失败(忽略)")
|
||||
|
||||
def load(self) -> dict:
|
||||
with self._lock:
|
||||
return self._read()
|
||||
|
||||
def record(self, slug: str, *, account: str, rounds: int, tools: int,
|
||||
tokens: int, ms: int, status: str) -> None:
|
||||
with self._lock:
|
||||
data = self._read()
|
||||
bucket = data.setdefault(slug, {"last_used_at": "", "total": 0, "items": []})
|
||||
bucket["last_used_at"] = time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
bucket["total"] = int(bucket.get("total") or 0) + 1
|
||||
items = bucket.setdefault("items", [])
|
||||
items.insert(0, {
|
||||
"at": time.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"account": account, "rounds": rounds, "tools": tools,
|
||||
"tokens": tokens, "ms": ms, "status": status,
|
||||
})
|
||||
del items[self.MAX_ITEMS:]
|
||||
self._write(data)
|
||||
|
||||
|
||||
#: 模块级单例(main.py 装配时复用)
|
||||
agent_runs = AgentRuns()
|
||||
|
||||
#: 引擎实况缓存:{model, ready, checked_at}。列表页顺带刷新,避免每请求都打引擎。
|
||||
engine_live: dict[str, Any] = {"model": "", "ready": False, "checked_at": 0.0}
|
||||
ENGINE_LIVE_TTL = 20.0 # 秒;这段时间内不重复探
|
||||
|
||||
|
||||
# ── 小工具 ────────────────────────────────────────────────────────────
|
||||
def _qwenpaw_profile_ids() -> list[str]:
|
||||
"""QwenPaw 的 profile 清单(slug 不能与它们撞)。读不到就返回空(降级不阻塞)。"""
|
||||
try:
|
||||
if QWENPAW_CONFIG.exists():
|
||||
d = json.loads(QWENPAW_CONFIG.read_text(encoding="utf-8"))
|
||||
return list(((d.get("agents") or {}).get("profiles") or {}).keys())
|
||||
except Exception: # noqa: BLE001
|
||||
log.debug("读 QwenPaw profile 清单失败(slug 查重降级)")
|
||||
return []
|
||||
|
||||
|
||||
def _sse(obj: dict) -> str:
|
||||
return "data: " + json.dumps(obj, ensure_ascii=False) + "\n\n"
|
||||
|
||||
|
||||
async def refresh_engine_live(store: AgentStore, *, force: bool = False) -> None:
|
||||
"""刷新"引擎当前模型 + 是否就绪"(带 TTL,避免每请求都打引擎)。"""
|
||||
now = time.time()
|
||||
if not force and (now - float(engine_live.get("checked_at") or 0)) < ENGINE_LIVE_TTL:
|
||||
return
|
||||
agents = store.list()
|
||||
base = agents[0].engine_base_url if agents else DEFAULT_ENGINE_URL
|
||||
agent_id = agents[0].engine_agent_id if agents else DEFAULT_ENGINE_AGENT
|
||||
ok, _why = await check_engine(base)
|
||||
model = ""
|
||||
if ok:
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=8.0) as c:
|
||||
r = await c.get(
|
||||
base.rstrip("/") + "/api/providers/active",
|
||||
params={"scope": "agent", "agent_id": agent_id},
|
||||
headers={"X-Agent-Id": agent_id},
|
||||
)
|
||||
if r.status_code < 400:
|
||||
slot = (r.json() or {}).get("active_llm") or {}
|
||||
model = slot.get("model") or ""
|
||||
except Exception: # noqa: BLE001
|
||||
log.debug("读引擎 active_model 失败")
|
||||
engine_live.update({"model": model, "ready": ok, "checked_at": now})
|
||||
|
||||
|
||||
def can_use_agent(a: Agent, role: str) -> bool:
|
||||
"""能否看/用这个智能体 —— 细粒度判定的**唯一入口**(二期下发靠它)。"""
|
||||
if role == "admin":
|
||||
return True
|
||||
if "admin" in a.access_roles and not a.access_users:
|
||||
return False # 标了"仅管理员"的,member 不可见
|
||||
return role in a.access_roles
|
||||
|
||||
|
||||
def install(app: FastAPI, *, store: AgentStore,
|
||||
require_auth: Callable[..., Any],
|
||||
require_admin: Callable[[Request], str],
|
||||
json_body: Callable[[Request], Awaitable[dict]],
|
||||
model_index: Callable[[], tuple[list[str], list[str]]],
|
||||
history_get: Callable[[str, str], list[dict]],
|
||||
) -> None:
|
||||
"""把智能体端点挂到 app 上(依赖由 main.py 注入,避免循环 import)。"""
|
||||
|
||||
def _agent_or_404(slug: str) -> Agent:
|
||||
store.reload_if_changed()
|
||||
a = store.get(slug)
|
||||
if a is None or a.status == "retired":
|
||||
raise HTTPException(404, f"智能体「{slug}」不存在")
|
||||
return a
|
||||
|
||||
def _view(a: Agent) -> dict:
|
||||
d = a.public_dict()
|
||||
bucket = agent_runs.load().get(a.slug) or {}
|
||||
d["last_used_at"] = bucket.get("last_used_at") or ""
|
||||
d["run_total"] = int(bucket.get("total") or 0)
|
||||
d["session_count"] = 0 # 按需再算(避免列表页为每个智能体读一次 history)
|
||||
d["engine_model"] = engine_live.get("model", "")
|
||||
d["engine_ready"] = bool(engine_live.get("ready"))
|
||||
return d
|
||||
|
||||
# ── 列表 ──
|
||||
@app.get("/agents")
|
||||
async def agents_list(request: Request, include_retired: bool = False,
|
||||
_auth=Depends(require_auth)):
|
||||
store.reload_if_changed()
|
||||
role = request.state.role
|
||||
if role == "admin":
|
||||
await refresh_engine_live(store)
|
||||
items = [_view(a) for a in store.list(include_retired=include_retired)]
|
||||
else:
|
||||
# member 只见 4 个字段(决策 ⑦)
|
||||
items = [{"slug": a.slug, "display_name": a.display_name,
|
||||
"description": a.description, "status": a.status}
|
||||
for a in store.list(include_retired=include_retired)
|
||||
if can_use_agent(a, role)]
|
||||
agents = store.list()
|
||||
return {
|
||||
"agents": items,
|
||||
"max_agents": store.max_agents,
|
||||
"active": store.active_count(),
|
||||
"engine": {
|
||||
"model": engine_live.get("model", ""),
|
||||
"ready": bool(engine_live.get("ready")),
|
||||
"base_url": agents[0].engine_base_url if agents else DEFAULT_ENGINE_URL,
|
||||
"agent_id": agents[0].engine_agent_id if agents else DEFAULT_ENGINE_AGENT,
|
||||
},
|
||||
}
|
||||
|
||||
# ── 新建 / 修改 / 归档 / 恢复 ──
|
||||
@app.post("/agents")
|
||||
async def agents_create(request: Request, _auth=Depends(require_auth)):
|
||||
require_admin(request)
|
||||
body = await json_body(request)
|
||||
ids, aliases = model_index()
|
||||
try:
|
||||
a = store.create(
|
||||
slug=body.get("slug", ""),
|
||||
display_name=body.get("display_name", ""),
|
||||
description=body.get("description", ""),
|
||||
params=body.get("params") or {},
|
||||
model_ids=ids, alias_names=aliases, profile_ids=_qwenpaw_profile_ids(),
|
||||
)
|
||||
except AgentsError as e:
|
||||
raise HTTPException(400, str(e))
|
||||
return {"status": "ok", "agent": _view(a)}
|
||||
|
||||
@app.put("/agents/{slug}")
|
||||
async def agents_update(request: Request, slug: str, _auth=Depends(require_auth)):
|
||||
require_admin(request)
|
||||
body = await json_body(request)
|
||||
try:
|
||||
a = store.update(slug, body)
|
||||
except AgentsError as e:
|
||||
raise HTTPException(400, str(e))
|
||||
return {"status": "ok", "agent": _view(a)}
|
||||
|
||||
@app.delete("/agents/{slug}")
|
||||
async def agents_retire(request: Request, slug: str, _auth=Depends(require_auth)):
|
||||
require_admin(request)
|
||||
try:
|
||||
a = store.retire(slug)
|
||||
except AgentsError as e:
|
||||
raise HTTPException(400, str(e))
|
||||
return {"status": "ok", "retired": True, "slug": a.slug}
|
||||
|
||||
@app.post("/agents/{slug}/restore")
|
||||
async def agents_restore(request: Request, slug: str, _auth=Depends(require_auth)):
|
||||
require_admin(request)
|
||||
try:
|
||||
a = store.restore(slug)
|
||||
except AgentsError as e:
|
||||
raise HTTPException(400, str(e))
|
||||
return {"status": "ok", "agent": _view(a)}
|
||||
|
||||
# ── 运行记录 ──
|
||||
@app.get("/agents/{slug}/runs")
|
||||
async def agents_runs(request: Request, slug: str, _auth=Depends(require_auth)):
|
||||
require_admin(request)
|
||||
_agent_or_404(slug)
|
||||
bucket = agent_runs.load().get(slug) or {}
|
||||
return {
|
||||
"slug": slug,
|
||||
"total": int(bucket.get("total") or 0),
|
||||
"last_used_at": bucket.get("last_used_at") or "",
|
||||
"items": bucket.get("items") or [],
|
||||
}
|
||||
|
||||
# ── 会话列表(复用 history 存储;agent_<slug>)──
|
||||
@app.get("/agents/{slug}/sessions")
|
||||
async def agents_sessions(request: Request, slug: str, _auth=Depends(require_auth)):
|
||||
a = _agent_or_404(slug)
|
||||
if not can_use_agent(a, request.state.role):
|
||||
raise HTTPException(403, "无权使用该智能体")
|
||||
try:
|
||||
items = history_get(request.state.account, f"agent_{slug}")
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise HTTPException(400, str(e))
|
||||
return {"slug": slug, "kind": f"agent_{slug}", "items": items}
|
||||
|
||||
# ── 试跑(配置页那颗按钮)──
|
||||
@app.post("/agents/{slug}/test")
|
||||
async def agents_test(request: Request, slug: str, _auth=Depends(require_auth)):
|
||||
require_admin(request)
|
||||
a = _agent_or_404(slug)
|
||||
started = time.time()
|
||||
ok, detail, done = True, "", False
|
||||
try:
|
||||
async for msg in stream_chat(
|
||||
a.engine_base_url, a.engine_agent_id,
|
||||
[{"role": "user", "content": "回复:ok"}],
|
||||
f"selftest-{a.slug}-{int(started)}",
|
||||
system_suffix="只回复 ok,不要任何多余内容。",
|
||||
timeout=90.0,
|
||||
):
|
||||
if msg.get("type") == "error":
|
||||
ok, detail = False, str(msg.get("message"))[:200]
|
||||
break
|
||||
if msg.get("type") == "done":
|
||||
done = True
|
||||
except Exception as exc: # noqa: BLE001
|
||||
ok, detail = False, str(exc)[:200]
|
||||
if ok and not done:
|
||||
ok, detail = False, "引擎无响应"
|
||||
return {"ok": ok, "ms": int((time.time() - started) * 1000), "detail": detail}
|
||||
|
||||
# ── 停止 ──
|
||||
@app.post("/agents/{slug}/stop")
|
||||
async def agents_stop(request: Request, slug: str, chat_id: str = "",
|
||||
_auth=Depends(require_auth)):
|
||||
require_admin(request)
|
||||
a = _agent_or_404(slug)
|
||||
if not chat_id:
|
||||
raise HTTPException(400, "缺少 chat_id")
|
||||
url = a.engine_base_url.rstrip("/") + "/api/console/chat/stop"
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=15.0) as c:
|
||||
r = await c.post(url, params={"chat_id": chat_id},
|
||||
headers={"X-Agent-Id": a.engine_agent_id})
|
||||
except Exception as exc: # noqa: BLE001
|
||||
raise HTTPException(502, f"停止失败:{exc}")
|
||||
return {"status": "ok", "engine_http": r.status_code}
|
||||
|
||||
# ── 对话(SSE)──
|
||||
@app.post("/agents/{slug}/chat")
|
||||
async def agents_chat(request: Request, slug: str, _auth=Depends(require_auth)):
|
||||
a = _agent_or_404(slug)
|
||||
if not can_use_agent(a, request.state.role):
|
||||
raise HTTPException(403, "无权使用该智能体")
|
||||
body = await json_body(request)
|
||||
messages = body.get("messages")
|
||||
if not isinstance(messages, list) or not messages:
|
||||
raise HTTPException(400, "messages 不能为空")
|
||||
session_id = str(body.get("session_id") or "").strip() \
|
||||
or f"agent-{slug}-{int(time.time())}"
|
||||
account = request.state.account
|
||||
|
||||
# 防自指:引擎的模型不能指向 agent/*(否则引擎→智能体→引擎 死循环)
|
||||
await refresh_engine_live(store)
|
||||
model_now = str(engine_live.get("model") or "")
|
||||
if model_now == "agent" or model_now.startswith("agent/"):
|
||||
raise HTTPException(
|
||||
400, "引擎当前模型指向了智能体自身(agent/*),会造成死循环,已拒绝"
|
||||
)
|
||||
|
||||
p = a.params or {}
|
||||
suffix = str(p.get("system_suffix") or "")
|
||||
# 第四十轮(计量打标):让日志能一眼看出"这次调用是网关的智能体发出的",
|
||||
# 与管理员在「对话」页直接聊区分开。明细(谁/哪个智能体/多少 token)落 agent_runs。
|
||||
log.info("agent chat: slug=%s account=%s session=%s rounds=%d",
|
||||
slug, account, session_id, rounds)
|
||||
started = time.time()
|
||||
rounds = len([m for m in messages if isinstance(m, dict) and m.get("role") == "user"])
|
||||
tool_count = 0
|
||||
total_tokens = 0
|
||||
status = "ok"
|
||||
|
||||
async def event_stream():
|
||||
nonlocal tool_count, total_tokens, status
|
||||
yield _sse({"type": "start", "slug": slug, "session_id": session_id})
|
||||
try:
|
||||
async for msg in stream_chat(
|
||||
a.engine_base_url, a.engine_agent_id, messages, session_id,
|
||||
system_suffix=suffix,
|
||||
):
|
||||
t = msg.get("type")
|
||||
if t == "step" and msg.get("status") == "done":
|
||||
tool_count += 1
|
||||
if t == "usage":
|
||||
total_tokens += int(msg.get("prompt") or 0) \
|
||||
+ int(msg.get("completion") or 0)
|
||||
if t == "error":
|
||||
status = "error"
|
||||
yield _sse(msg)
|
||||
finally:
|
||||
agent_runs.record(
|
||||
slug, account=account, rounds=rounds, tools=tool_count,
|
||||
tokens=total_tokens, ms=int((time.time() - started) * 1000),
|
||||
status=status,
|
||||
)
|
||||
|
||||
return StreamingResponse(
|
||||
event_stream(), media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "Connection": "keep-alive",
|
||||
"X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
# ── 身份 + 功能开关(灰度通道;前端唯一权威)──
|
||||
@app.get("/auth/me")
|
||||
async def auth_me(request: Request, _auth=Depends(require_auth)):
|
||||
store.reload_if_changed()
|
||||
role = request.state.role
|
||||
visible = [a.slug for a in store.list() if can_use_agent(a, role)]
|
||||
return {
|
||||
"account": request.state.account,
|
||||
"role": role,
|
||||
"features": {
|
||||
"agents": {
|
||||
"enabled": bool(visible),
|
||||
"can_manage": role == "admin",
|
||||
"visible": visible,
|
||||
},
|
||||
},
|
||||
"engine": {
|
||||
"model": engine_live.get("model", ""),
|
||||
"ready": bool(engine_live.get("ready")),
|
||||
},
|
||||
}
|
||||
+18
-3
@@ -25,9 +25,16 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
|
||||
KINDS = ("chat", "images", "videos")
|
||||
|
||||
# 第三十八轮:智能体会话复用本模块,类别形如 `agent_<slug>`(见 docs/agents-dev-plan.md ㉑)。
|
||||
# 理由:会话列表要的"新建 / 切换 / 重命名 / 删除 / 按账号隔离 / 容量裁剪"这里全都有,
|
||||
# 新开一套端点纯属重复。slug 形态已在 AgentStore 校验过(小写字母数字连字符)。
|
||||
AGENT_KIND_PREFIX = "agent_"
|
||||
_AGENT_KIND_RE = re.compile(r"^agent_[a-z][a-z0-9-]{1,31}$")
|
||||
# 各类别的容量按"内容天然体量"分开定:对话一条就是一整段多轮消息(正文可能很长),
|
||||
# 图像一条只有 URL + prompt,视频一条带一段上游 raw 回执 —— 一刀切会把对话正文截坏。
|
||||
LIMITS: dict[str, dict[str, int]] = {
|
||||
@@ -42,6 +49,9 @@ MAX_FILE_BYTES = 4 * 1024 * 1024 # 整份 history.json 的软上限
|
||||
|
||||
|
||||
def _lim(kind: str) -> dict[str, int]:
|
||||
# 智能体会话与普通对话体量相同(一整段多轮消息)→ 沿用 chat 的容量
|
||||
if kind.startswith(AGENT_KIND_PREFIX):
|
||||
return LIMITS["chat"]
|
||||
return LIMITS.get(kind, {"items": MAX_ITEMS, "item_bytes": MAX_ITEM_BYTES,
|
||||
"field_chars": MAX_FIELD_CHARS})
|
||||
|
||||
@@ -130,9 +140,14 @@ class HistoryStore:
|
||||
# ── 读写 ───────────────────────────────
|
||||
@staticmethod
|
||||
def _check_kind(kind: str) -> str:
|
||||
if kind not in KINDS:
|
||||
raise HistoryError(f"未知历史类别 {kind!r},可选:{'/'.join(KINDS)}")
|
||||
return kind
|
||||
if kind in KINDS:
|
||||
return kind
|
||||
# 智能体会话:agent_<slug>(第三十八轮)
|
||||
if _AGENT_KIND_RE.match(kind):
|
||||
return kind
|
||||
raise HistoryError(
|
||||
f"未知历史类别 {kind!r},可选:{'/'.join(KINDS)}、或 agent_<智能体 slug>"
|
||||
)
|
||||
|
||||
def get(self, account: str, kind: str) -> list[dict]:
|
||||
self._check_kind(kind)
|
||||
|
||||
+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)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# 智能体定义(第三十八轮 · 路线 A:界面在网关,引擎是 QwenPaw)
|
||||
#
|
||||
# 说明:
|
||||
# · 所有智能体共用 QwenPaw 的同一个 profile(engine.agent_id),
|
||||
# 也共用同一个模型 —— 模型是 profile 级设置(请求级指定无效,已实测)。
|
||||
# · 提示词主档归 QwenPaw 管(改 SOUL.md / AGENTS.md);
|
||||
# 这里的 params.system_suffix 只是一小条角色补充。
|
||||
# · 本文件不含秘密,可入版本库。
|
||||
# · 归档(status: retired)不计入 max_agents 上限。
|
||||
max_agents: 5
|
||||
agents:
|
||||
code-review:
|
||||
display_name: 代码审查
|
||||
description: 只看仓库改动,输出问题清单
|
||||
params:
|
||||
temperature: 0.2
|
||||
system_suffix: 回答只给结论,不要解释过程。
|
||||
engine:
|
||||
type: qwenpaw
|
||||
base_url: http://127.0.0.1:8088
|
||||
agent_id: ai-gateway
|
||||
access:
|
||||
roles:
|
||||
- admin
|
||||
users: []
|
||||
status: published
|
||||
created_at: '2026-09-29 12:45:18'
|
||||
updated_at: '2026-09-29 12:45:18'
|
||||
+38
-15
@@ -148,24 +148,31 @@
|
||||
|
||||
## 2. 开发步骤
|
||||
|
||||
### Step -1 · 技术验证(约 0.5h)★ 动手前先做
|
||||
### Step -1 · 技术验证 ✅ **已完成**(2026-09-29)
|
||||
|
||||
```
|
||||
1. ✔ 已做:核实"请求级指定模型"不可行(见 ⑲)→ 方案已按甲案修正
|
||||
2. ✔ 已做:确认 PUT/GET /api/providers/active 支持 scope=agent + agent_id
|
||||
3. 待做:录一段**完整真实 SSE 事件流**(含工具调用)当 Step 2 的测试夹具
|
||||
4. 待做:确认 X-Agent-Id 选 profile 的行为(新建 profile 后)
|
||||
4b. ★ 待做:**QwenPaw 的 `input` 接不接受 `role: "system"` 的消息**(决定 `system_suffix` 的注入方式,见 ㉖)
|
||||
· 接受 → 网关插入一条 system 消息(干净)
|
||||
· 不接受 → 改成拼进首条 user 消息前缀(UI 标注)
|
||||
5. 待做:确认 ai-gateway profile 的 active_model 指向哪个 provider/model
|
||||
(建议指向网关自己的 wangjinding provider + 别名 wjd,
|
||||
这样引擎调模型也过网关,计量闭环)
|
||||
```
|
||||
| # | 验证项 | 结论 |
|
||||
|---|---|---|
|
||||
| 1 | 请求级指定模型是否可行 | ❌ **不可行** → 方案已按甲案修正(见 ⑲) |
|
||||
| 2 | `PUT/GET /api/providers/active` 的 scope | ✅ 支持 `scope=agent` + `agent_id` |
|
||||
| 3 | **SSE 事件谱 + 夹具** | ✅ **已录制并落盘** → 见下 |
|
||||
| 4 | `X-Agent-Id` 选 profile | ✅ 生效(实测带 `X-Agent-Id: default` 走 default 的会话/记忆) |
|
||||
| 4b | **`input` 接不接受 `role:"system"`** | ✅ **接受且真的生效** → `system_suffix` 用方案 ④(注入 system 消息) |
|
||||
| 5 | `ai-gateway` 的 active_model 指向 | ✅ 已确认为 `wangjinding` / `wjd`(引擎调用过网关,计量闭环成立) |
|
||||
|
||||
**验收**:5 项都有明确结论;SSE 夹具落盘。
|
||||
**产出物**:
|
||||
- `tests/fixtures/sse-tool-flow.sse` —— 真实含工具调用的 SSE(14,900 字节 / 57 事件)
|
||||
- `docs/qwenpaw-agent-api-sse.md` —— **完整事件谱 + 适配器映射表**(Step 2 实现依据)
|
||||
|
||||
### Step 0 · 建 QwenPaw profile `ai-gateway`(约 1h)★ 新增
|
||||
**事件谱要点(实测)**:`text`(36,正文增量,`delta:true` 累积 / `delta:false` 是快照要忽略) ·
|
||||
`plugin_call`(工具调用,名在 `content[0].data.name`、参数是 JSON 字符串) ·
|
||||
`plugin_call_output`(结果,在 `data.output`,用 `call_id` 配对) · `data`(分片,**忽略**) ·
|
||||
`response`(终态,`status: completed/failed/cancelled`) · `turn_usage`(**记 token/成本 的直接来源**,
|
||||
带 `model_name` 与缓存字段)。
|
||||
**顺序**:文本 → 工具调用 → 工具结果 → 更多文本 → … → `response` → `turn_usage`(同一响应内穿插)。
|
||||
|
||||
**另一条实测**:`GET /healthz` **只回 `{"status":"ok"}` 不含版本** —— 验版本要读 `GET /` 的 `version`。
|
||||
|
||||
### Step 0 · 建 QwenPaw profile `ai-gateway` ✅ **已完成**(2026-09-29)
|
||||
|
||||
```
|
||||
0. 备份:cp ~/.qwenpaw/config.json → config.json.bak-<日期>(记回滚方式)
|
||||
@@ -184,6 +191,22 @@
|
||||
```
|
||||
|
||||
**验收**:`X-Agent-Id: ai-gateway` 跑通一轮对话;`default` 行为与之前一致。
|
||||
|
||||
**实际完成情况(实测)**:
|
||||
|
||||
| 项 | 结果 |
|
||||
|---|---|
|
||||
| 备份 | `config.json.bak-20260929-agents` |
|
||||
| 工作区 | `<工作区>/`(**全新空白,未复制 default 的任何私人文件**) |
|
||||
| 提示词 | 新写 `SOUL.md` + `AGENTS.md`(网关智能体版);`system_prompt_files: ["AGENTS.md","SOUL.md"]`(**不含 PROFILE.md**) |
|
||||
| 注册 | `config.json` 的 `agents.profiles` + `agent_order` 均已加 `ai-gateway` |
|
||||
| 工具 | ✅ **全开 27 个**(决策⑪);`approval_level: OFF` |
|
||||
| 模型 | `active_model = {provider_id: "wangjinding", model: "wjd"}` |
|
||||
| 引擎 Key | 新建 `agent-engine`(label),明文暂存 `/tmp/agent_engine_key`(600),待写入 QwenPaw 配置 |
|
||||
| **profile 可用性** | ✅ 实测 `X-Agent-Id: ai-gateway` 跑通,"网关就绪"正常返回 |
|
||||
| **模型指向** | ✅ `turn_usage` 明证 `provider_id: wangjinding / model_name: wjd` → **引擎过网关,计量闭环** |
|
||||
| **隔离验证** | ✅ 问"你是谁/认识老板吗",答"没有个人身份、没有个人记忆…没有任何关于「老板」的信息",并实际检查了工作区记忆文件 |
|
||||
| `default` 是否受影响 | ✅ 无影响(新建 profile 不动已有 profile) |
|
||||
**注意**:改 `config.json` 会触发配置重载。实测口径是"新增 profile 不影响已有 profile",
|
||||
但**仍要先备份**;若发现 `default` 会话中断,立即回滚。
|
||||
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
# QwenPaw Agent API · SSE 事件谱(2026-09-29 实测录制)
|
||||
|
||||
> 来源:真实打 `POST http://127.0.0.1:8088/api/console/chat`(带 `X-Agent-Id: default`),
|
||||
> 让它执行一次 `read_file` 工具调用,全程录下 SSE。
|
||||
> 原始夹具:`tests/fixtures/sse-tool-flow.sse`(14,900 字节 / 57 个事件)
|
||||
> 用途:**Step 2 引擎适配器的映射依据** + 单测夹具。
|
||||
|
||||
---
|
||||
|
||||
## 1. 事件总览(本次录制)
|
||||
|
||||
| 事件 `type` | 次数 | 含义 |
|
||||
|---|---|---|
|
||||
| `text` | 36 | 正文增量(**流式的主体**) |
|
||||
| `data` | 9 | 结构化数据块(工具调用的参数/输出分片) |
|
||||
| `message` | 4 | 一条完整消息(开始/结束快照) |
|
||||
| `response` | 3 | 整个响应的开始/结束(**终态在这里**) |
|
||||
| `plugin_call` | 2 | 工具调用(开始) |
|
||||
| `plugin_call_output` | 2 | 工具执行结果 |
|
||||
| `turn_usage` | 1 | 本轮 token 用量 + 上下文占用(**在最后**) |
|
||||
|
||||
每条 SSE 行的前缀固定是 `data: `,后跟一个 JSON 对象(**没有 `event:` 行**)。
|
||||
|
||||
---
|
||||
|
||||
## 2. 事件序列时序(实测顺序)
|
||||
|
||||
```
|
||||
0. response ← 响应开始(status=created)
|
||||
1. response ← status=in_progress
|
||||
2. message ← 助手消息开始(content=[])
|
||||
3-6. text ×4 ← 正文增量:"I" / "'ll read that" / " file for" / " you.\n\n"
|
||||
7. text ← delta=false,整段快照("I'll read that file for you.")
|
||||
8. message ← 助手消息完成(status=completed,含完整 content)
|
||||
9. plugin_call ← 工具调用开始(role=assistant,name=read_file)
|
||||
10-15. data ×6 ← 工具参数分片(call_id / name / arguments)
|
||||
16. plugin_call ← 工具调用的完整快照
|
||||
17. plugin_call_output ← 工具执行结果(开始)
|
||||
18-19. data ×2 ← 工具输出分片(call_id / name / output)
|
||||
...
|
||||
N. text ×N ← 工具结果喂回模型后,继续输出正文
|
||||
N+1. message ← 完成
|
||||
N+2. response ← ★ 终态(status=completed,output 数组含全部消息)
|
||||
N+3. turn_usage ← ★ token 用量与上下文占用
|
||||
```
|
||||
|
||||
**关键**:工具调用不是"单独一条流",而是**穿插在同一个响应里** ——
|
||||
文本 → 工具调用 → 工具结果 → 更多文本 → …… → 终态。
|
||||
|
||||
---
|
||||
|
||||
## 3. 关键事件的结构
|
||||
|
||||
### `text`(正文增量)★ 最常用
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "text", "delta": true, "index": 0, "status": null,
|
||||
"object": "content", "msg_id": "msg_...",
|
||||
"text": "I'll read that",
|
||||
"sequence_number": 4
|
||||
}
|
||||
```
|
||||
- `delta: true` = 增量片段;`delta: false` = **整段快照**(去重用,别重复追加)
|
||||
- 追加逻辑:只累积 `delta: true` 的 `text` 字段
|
||||
|
||||
### `plugin_call`(工具调用)
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "msg_...", "type": "plugin_call", "role": "assistant",
|
||||
"content": [{
|
||||
"type": "data", "delta": false, "index": null, "status": "completed",
|
||||
"object": "content", "msg_id": null,
|
||||
"data": {
|
||||
"call_id": "chatcmpl-tool-b207f3da7699c9fa",
|
||||
"name": "read_file",
|
||||
"arguments": "{\"file_path\": \"/tmp/probe-agent.txt\"}"
|
||||
}
|
||||
}],
|
||||
"status": "completed", "metadata": null, "name": "assistant",
|
||||
"object": "message", "sequence_number": 9
|
||||
}
|
||||
```
|
||||
- **工具名**:`content[0].data.name`
|
||||
- **参数**:`content[0].data.arguments`(**JSON 字符串**,要二次解析)
|
||||
- **调用 id**:`content[0].data.call_id`(与 output 配对用)
|
||||
|
||||
### `plugin_call_output`(工具结果)
|
||||
|
||||
结构同上,`data` 里的键是 **`call_id` / `name` / `output`**(输出在 `output`,不是 `arguments`)。
|
||||
|
||||
### `data`(分片)
|
||||
|
||||
工具参数/输出会**先以 `data` 事件分片流式到达**(`call_id` / `name` / `arguments|output` 陆续出现),
|
||||
最后再来一条完整的 `plugin_call` / `plugin_call_output` 快照。
|
||||
→ **适配器只需认快照事件**(`plugin_call` / `plugin_call_output`),分片 `data` 可忽略(避免重复渲染)。
|
||||
|
||||
### `response`(终态)
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "response_...", "output": [ ...全部消息... ],
|
||||
"status": "completed", // ← completed / failed / cancelled
|
||||
"created_at": "...", "completed_at": "...",
|
||||
"object": "response", "session_id": "probe-tool-flow",
|
||||
"sequence_number": 57,
|
||||
"usage": { "input_tokens": 13767, "output_tokens": 21 }
|
||||
}
|
||||
```
|
||||
**这是结束标志**,也是拿最终全文最可靠的地方(`output` 里每条消息带完整 content)。
|
||||
|
||||
### `turn_usage`(用量,最后一条)
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "turn_usage", "session_id": "probe-tool-flow",
|
||||
"usage": {
|
||||
"provider_id": "opencode", "model_name": "deepseek-v4.1-flash",
|
||||
"prompt_tokens": 13767, "completion_tokens": 21, "total_tokens": 13788,
|
||||
"cache_read_tokens": 1024, "cache_observed": true, "cache_hit_rate": 7.43,
|
||||
"context_size": 1000000, "compact_threshold": 0.8
|
||||
},
|
||||
"context_usage": { "estimated_tokens": 27, "max_input_length": 1000000, "context_usage_ratio": 0.0027 }
|
||||
}
|
||||
```
|
||||
**这是网关侧记 token / 算成本的直接来源**(比从 `response.usage` 拿更全:带 model_name 和缓存字段)。
|
||||
|
||||
---
|
||||
|
||||
## 4. 适配器映射表(Step 2 实现依据)
|
||||
|
||||
| QwenPaw 事件 | 网关要做什么 |
|
||||
|---|---|
|
||||
| `text` with `delta:true` | 累积 `text` → 作为正文增量推给前端(`{type:"text", text}`) |
|
||||
| `text` with `delta:false` | **忽略**(快照,避免重复) |
|
||||
| `plugin_call` | 新增一条"轨迹步骤":`{name, arguments(解析后), call_id}`,状态=运行中 |
|
||||
| `plugin_call_output` | 用 `call_id` 匹配上一步 → 标记完成 + 记 `output` |
|
||||
| `data` | **忽略**(分片,快照已覆盖) |
|
||||
| `message` | 可用于"消息边界"(本次实现可忽略,靠 text 累积) |
|
||||
| `response` (`status:completed`) | 结束流;用它校验最终全文 |
|
||||
| `response` (`status:failed`) | 结束流 + 报错 |
|
||||
| `turn_usage` | **记 token / 成本 / model_name**(网关计量) |
|
||||
| 任意事件含 `status:cancelled` | 用户点了停止 → 正常结束 |
|
||||
|
||||
---
|
||||
|
||||
## 5. 已确认的其他事实(Step -1 结论)
|
||||
|
||||
| 项 | 结论 |
|
||||
|---|---|
|
||||
| **`role: "system"` 是否被接受** | ✅ **接受且生效** —— 实测塞一条 `{"role":"system","content":[{"type":"text","text":"从现在起你只用英文回答"}]}`,模型随后**真的用英文回答**。→ `system_suffix` 用**方案 ④**(网关每轮注入一条 system 消息),最干净。 |
|
||||
| **请求里带 `model` 是否生效** | ❌ **被忽略**(见计划 ⑲)→ 甲案:所有智能体共用同一模型 |
|
||||
| **`/healthz` 是否含版本** | ❌ 只回 `{"status":"ok"}`;**版本号要读 `GET /`** 的 `version` 字段 |
|
||||
| **本机免鉴权** | ✅ `allow_no_auth_hosts` 含 `127.0.0.1`/`::1`,网关内网直连不需要 token |
|
||||
| **X-Agent-Id 选 profile** | ✅ 生效(实测带 `X-Agent-Id: default` 走的就是 default 的会话与记忆) |
|
||||
|
||||
---
|
||||
|
||||
## 6. 复现方式
|
||||
|
||||
```bash
|
||||
# 录一段含工具调用的流(本机免鉴权)
|
||||
curl -s --max-time 150 -X POST http://127.0.0.1:8088/api/console/chat \
|
||||
-H 'Content-Type: application/json' -H 'X-Agent-Id: default' \
|
||||
-d '{"input":[{"role":"user","content":[{"type":"text","text":"用 read_file 读 /tmp/probe-agent.txt 并告诉我内容"}]}],
|
||||
"stream":true,"session_id":"probe-tool-flow"}' \
|
||||
-o tests/fixtures/sse-tool-flow.sse
|
||||
```
|
||||
@@ -25,6 +25,8 @@ const props = withDefaults(defineProps<{ name: string; size?: number; stroke?: n
|
||||
|
||||
// 图标表:键为语义名,值为 20×20 视框内的路径。与设计稿同一套几何。
|
||||
const ICONS: Record<string, string> = {
|
||||
// 第三十八轮:智能体(与设计稿 design/v3/agents.py 的 i-bot 同几何)
|
||||
bot: '<rect x="3.6" y="6.4" width="12.8" height="9.2" rx="2.6"/><path d="M10 3.2v3.2"/><circle cx="10" cy="3" r="1.1"/><circle cx="7.4" cy="10.6" r="1.15" fill="currentColor" stroke="none"/><circle cx="12.6" cy="10.6" r="1.15" fill="currentColor" stroke="none"/><path d="M8.2 13.4h3.6"/>',
|
||||
dash: '<rect x="3" y="3" width="6.2" height="6.2" rx="1.8"/><rect x="10.8" y="3" width="6.2" height="6.2" rx="1.8"/><rect x="3" y="10.8" width="6.2" height="6.2" rx="1.8"/><rect x="10.8" y="10.8" width="6.2" height="6.2" rx="1.8"/>',
|
||||
chat: '<path d="M3.5 6.2A2.7 2.7 0 0 1 6.2 3.5h7.6a2.7 2.7 0 0 1 2.7 2.7v4.6a2.7 2.7 0 0 1-2.7 2.7H8.6L4.7 16.2v-2.9a2.7 2.7 0 0 1-1.2-2.2z"/>',
|
||||
img: '<rect x="3" y="4" width="14" height="12" rx="2.4"/><circle cx="7.4" cy="8.4" r="1.3"/><path d="M3.6 14.4l4-3.6 2.8 2.4 2.9-2.6 3.1 2.8"/>',
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
// 智能体(第三十八轮 · 路线 A)—— 接口封装 + SSE 解析。
|
||||
//
|
||||
// 设计见 docs/agents-dev-plan.md;SSE 协议见 docs/qwenpaw-agent-api-sse.md。
|
||||
// 后端把 QwenPaw 的事件翻译成这几种(前端不必懂 AgentScope):
|
||||
|
||||
import { getToken } from '../api'
|
||||
|
||||
export interface AgentItem {
|
||||
slug: string
|
||||
display_name: string
|
||||
description: string
|
||||
params?: { temperature?: number; max_tokens?: number; top_p?: number; system_suffix?: string }
|
||||
engine?: { type: string; base_url: string; agent_id: string }
|
||||
access?: { roles: string[]; users: string[] }
|
||||
status: 'draft' | 'published' | 'retired'
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
access_scope?: string
|
||||
last_used_at?: string
|
||||
run_total?: number
|
||||
engine_model?: string
|
||||
engine_ready?: boolean
|
||||
}
|
||||
|
||||
export interface AgentListResp {
|
||||
agents: AgentItem[]
|
||||
max_agents: number
|
||||
active: number
|
||||
engine: { model: string; ready: boolean; base_url: string; agent_id: string }
|
||||
}
|
||||
|
||||
export interface AgentRun {
|
||||
at: string
|
||||
account: string
|
||||
rounds: number
|
||||
tools: number
|
||||
tokens: number
|
||||
ms: number
|
||||
status: string
|
||||
}
|
||||
|
||||
/** 后端 SSE 的事件类型。 */
|
||||
export type AgentEvent =
|
||||
| { type: 'start'; slug: string; session_id: string }
|
||||
| { type: 'text'; text: string }
|
||||
| { type: 'step'; id: string; name?: string; args?: any; status: 'running' | 'done'; ms?: number; output?: any }
|
||||
| { type: 'usage'; prompt: number; completion: number; cached: number; model: string; provider: string; context_ratio?: number }
|
||||
| { type: 'done'; status: string }
|
||||
| { type: 'error'; message: string }
|
||||
|
||||
function authHeaders(): Record<string, string> {
|
||||
return { Authorization: `Bearer ${getToken()}` }
|
||||
}
|
||||
|
||||
async function req<T>(path: string, init: RequestInit = {}): Promise<T> {
|
||||
const r = await fetch(path, {
|
||||
...init,
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders(), ...(init.headers || {}) },
|
||||
})
|
||||
const body = await r.json().catch(() => null)
|
||||
if (!r.ok) {
|
||||
const msg = body?.error?.message || body?.detail || `HTTP ${r.status}`
|
||||
throw new Error(typeof msg === 'string' ? msg : JSON.stringify(msg))
|
||||
}
|
||||
return body as T
|
||||
}
|
||||
|
||||
export const listAgents = (includeRetired = false) =>
|
||||
req<AgentListResp>(`/agents${includeRetired ? '?include_retired=true' : ''}`)
|
||||
|
||||
export const createAgent = (body: Record<string, any>) =>
|
||||
req<{ status: string; agent: AgentItem }>('/agents', { method: 'POST', body: JSON.stringify(body) })
|
||||
|
||||
export const updateAgent = (slug: string, patch: Record<string, any>) =>
|
||||
req<{ status: string; agent: AgentItem }>(`/agents/${slug}`, { method: 'PUT', body: JSON.stringify(patch) })
|
||||
|
||||
export const retireAgent = (slug: string) =>
|
||||
req<{ status: string }>(`/agents/${slug}`, { method: 'DELETE' })
|
||||
|
||||
export const restoreAgent = (slug: string) =>
|
||||
req<{ status: string; agent: AgentItem }>(`/agents/${slug}/restore`, { method: 'POST' })
|
||||
|
||||
export const agentRuns = (slug: string) =>
|
||||
req<{ slug: string; total: number; last_used_at: string; items: AgentRun[] }>(`/agents/${slug}/runs`)
|
||||
|
||||
export const testAgent = (slug: string) =>
|
||||
req<{ ok: boolean; ms: number; detail: string }>(`/agents/${slug}/test`, { method: 'POST' })
|
||||
|
||||
export const agentSessions = (slug: string) =>
|
||||
req<{ slug: string; kind: string; items: any[] }>(`/agents/${slug}/sessions`)
|
||||
|
||||
export const stopAgent = (slug: string, chatId: string) =>
|
||||
req<{ status: string }>(`/agents/${slug}/stop?chat_id=${encodeURIComponent(chatId)}`, { method: 'POST' })
|
||||
|
||||
/**
|
||||
* 与智能体对话(SSE 流式)。逐事件回调 —— 与 Chat.vue 的流式处理同款思路。
|
||||
*
|
||||
* 返回一个 abort 函数(停止按钮用)。注意:网关侧的"停止"是转发给引擎的
|
||||
* /console/chat/stop;前端 abort 只是断开本地连接,两者可同时用。
|
||||
*/
|
||||
export function streamAgentChat(
|
||||
slug: string,
|
||||
messages: { role: string; content: string }[],
|
||||
sessionId: string,
|
||||
onEvent: (ev: AgentEvent) => void,
|
||||
): () => void {
|
||||
const ctrl = new AbortController()
|
||||
;(async () => {
|
||||
try {
|
||||
const r = await fetch(`/agents/${slug}/chat`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...authHeaders() },
|
||||
body: JSON.stringify({ messages, session_id: sessionId }),
|
||||
signal: ctrl.signal,
|
||||
})
|
||||
if (!r.ok) {
|
||||
const body = await r.json().catch(() => null)
|
||||
const msg = body?.error?.message || body?.detail || `HTTP ${r.status}`
|
||||
onEvent({ type: 'error', message: String(msg) })
|
||||
return
|
||||
}
|
||||
const reader = r.body!.getReader()
|
||||
const dec = new TextDecoder()
|
||||
let buf = ''
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read()
|
||||
if (done) break
|
||||
buf += dec.decode(value, { stream: true })
|
||||
const parts = buf.split('\n\n')
|
||||
buf = parts.pop() || ''
|
||||
for (const part of parts) {
|
||||
const line = part.trim()
|
||||
if (!line.startsWith('data:')) continue
|
||||
try {
|
||||
onEvent(JSON.parse(line.slice(5).trim()) as AgentEvent)
|
||||
} catch { /* 忽略坏帧 */ }
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
if (e?.name !== 'AbortError') onEvent({ type: 'error', message: String(e?.message || e) })
|
||||
}
|
||||
})()
|
||||
return () => ctrl.abort()
|
||||
}
|
||||
+10
-6
@@ -18,15 +18,19 @@ export interface NavGroup {
|
||||
|
||||
/** 桌面侧栏 / 手机抽屉的完整菜单(按功能分组,组间一条分隔线)。 */
|
||||
export function navGroups(isAdmin: boolean): NavGroup[] {
|
||||
// 第三十八轮:智能体(一期仅管理员;后端 features.agents 也按角色下发,两处一致)
|
||||
const core: NavItem[] = [
|
||||
{ path: '/dashboard', key: 'dashboard', icon: 'dash' },
|
||||
{ path: '/chat', key: 'chat', icon: 'chat' },
|
||||
{ path: '/images', key: 'images', icon: 'img' },
|
||||
{ path: '/videos', key: 'videos', icon: 'vid' },
|
||||
]
|
||||
if (isAdmin) core.push({ path: '/agents', key: 'agents', icon: 'bot' })
|
||||
|
||||
const groups: NavGroup[] = [
|
||||
{
|
||||
id: 'core',
|
||||
items: [
|
||||
{ path: '/dashboard', key: 'dashboard', icon: 'dash' },
|
||||
{ path: '/chat', key: 'chat', icon: 'chat' },
|
||||
{ path: '/images', key: 'images', icon: 'img' },
|
||||
{ path: '/videos', key: 'videos', icon: 'vid' },
|
||||
],
|
||||
items: core,
|
||||
},
|
||||
{
|
||||
id: 'ops',
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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: '名称',
|
||||
|
||||
@@ -19,6 +19,8 @@ const router = createRouter({
|
||||
{ path: 'images', name: 'images', component: () => import('./views/Images.vue') },
|
||||
{ path: 'videos', name: 'videos', component: () => import('./views/Videos.vue') },
|
||||
{ path: 'models', name: 'models', component: () => import('./views/Models.vue') },
|
||||
// 第三十八轮:智能体(一期仅管理员;用 meta.admin 做双层守卫)
|
||||
{ path: 'agents', name: 'agents', component: () => import('./views/Agents.vue'), meta: { admin: true, agents: true } },
|
||||
{ path: 'metrics', name: 'metrics', component: () => import('./views/Metrics.vue') },
|
||||
{ path: 'keys', name: 'keys', component: () => import('./views/ApiKeys.vue') },
|
||||
{ path: 'users', name: 'users', component: () => import('./views/Users.vue'), meta: { admin: true } },
|
||||
|
||||
@@ -0,0 +1,714 @@
|
||||
<template>
|
||||
<div class="page">
|
||||
<el-alert v-if="err" :title="err" type="error" show-icon :closable="false" class="mb" />
|
||||
|
||||
<div class="row-h">
|
||||
<div>
|
||||
<b style="font-size:15px">{{ t('agents.title') }}</b>
|
||||
<span class="sub-hint">{{ t('agents.sub') }}</span>
|
||||
</div>
|
||||
<span style="margin-left:auto" />
|
||||
<el-checkbox v-if="hasRetired" v-model="showRetired" size="small" @change="load">
|
||||
{{ t('agents.showRetired') }}
|
||||
</el-checkbox>
|
||||
<el-button type="primary" size="small" :disabled="atLimit" :title="atLimit ? t('agents.limitHint', { n: data.max_agents }) : ''" @click="openCreate">
|
||||
<Icon name="plus" :size="14" />{{ t('agents.new') }}
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<!-- KPI -->
|
||||
<div class="kpis ag-kpis">
|
||||
<div class="kpi">
|
||||
<div class="k">{{ t('agents.title') }}</div>
|
||||
<div class="v">{{ activeCount }}</div>
|
||||
<div class="f"><span class="muted-2">{{ t('agents.limitHint', { n: data.max_agents }) }}</span></div>
|
||||
</div>
|
||||
<div class="kpi">
|
||||
<div class="k">{{ t('agents.cfgAccess') }}</div>
|
||||
<div class="v">{{ data.engine.ready ? '1' : '0' }}</div>
|
||||
<div class="f"><span class="muted-2">{{ t('agents.scopeAdmin') }}</span></div>
|
||||
</div>
|
||||
<div class="kpi">
|
||||
<div class="k">{{ t('agents.tabRuns') }}</div>
|
||||
<div class="v">{{ totalRuns }}</div>
|
||||
<div class="f"><span class="muted-2">{{ t('agents.runsLast7') }}</span></div>
|
||||
</div>
|
||||
<div class="kpi">
|
||||
<div class="k">{{ t('agents.tokens') }}</div>
|
||||
<div class="v">{{ totalTokensText }}</div>
|
||||
<div class="f"><span class="muted-2">{{ t('agents.runsDetailSub') }}</span></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="ag-layout">
|
||||
<!-- 左:列表 -->
|
||||
<div class="card" style="padding:12px">
|
||||
<div class="card-h" style="margin-bottom:var(--gap-s)">
|
||||
<b>{{ t('agents.title') }}</b>
|
||||
<span class="sub">{{ t('agents.sharedNote') }}</span>
|
||||
</div>
|
||||
<div class="ag-list">
|
||||
<div
|
||||
v-for="a in data.agents"
|
||||
:key="a.slug"
|
||||
class="ag"
|
||||
:class="{ on: a.slug === current }"
|
||||
@click="pick(a.slug)"
|
||||
>
|
||||
<div class="ag-ico"><Icon name="bot" :size="17" /></div>
|
||||
<div class="ag-main">
|
||||
<div class="ag-nm">{{ a.display_name }}</div>
|
||||
<div class="ag-ds">{{ a.description || '—' }}</div>
|
||||
<div class="ag-tags">
|
||||
<span class="pill mute">{{ t('agents.engineBadge') }}</span>
|
||||
<span v-if="a.engine_model" class="ag-md">{{ a.engine_model }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ag-right">
|
||||
<span class="pill" :class="a.status === 'retired' ? 'arch' : (a.status === 'draft' ? 'mute' : 'ok')">
|
||||
{{ statusText(a.status) }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="!data.agents.length" class="hint">{{ t('agents.empty') }}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 右:详情 -->
|
||||
<div v-if="!cur" class="card ag-empty">
|
||||
<div class="ae-ico"><Icon name="bot" :size="34" /></div>
|
||||
<div class="ae-t">{{ t('agents.emptyPick') }}</div>
|
||||
<div class="ae-s">{{ t('agents.emptyHint') }}</div>
|
||||
<div class="ae-btn" @click="openCreate"><Icon name="plus" :size="14" />{{ t('agents.newAgent') }}</div>
|
||||
</div>
|
||||
|
||||
<div v-else class="ag-detail">
|
||||
<!-- 顶部身份条 -->
|
||||
<div class="row-h">
|
||||
<div class="ag-top">
|
||||
<div class="ag-face"><Icon name="bot" :size="16" /></div>
|
||||
<b>{{ cur.display_name }}</b>
|
||||
<span class="pill" :class="cur.status === 'retired' ? 'arch' : 'ok'">{{ statusText(cur.status) }}</span>
|
||||
<span v-if="cur.engine_ready" class="pill ok">{{ t('agents.engineReady') }}</span>
|
||||
<span v-else class="pill err">{{ t('agents.engineDown') }}</span>
|
||||
</div>
|
||||
<span style="margin-left:auto" />
|
||||
<el-button v-if="cur.status !== 'retired'" size="small" @click="doRetire">{{ t('agents.archive') }}</el-button>
|
||||
<el-button v-else size="small" type="primary" @click="doRestore">{{ t('agents.restore') }}</el-button>
|
||||
</div>
|
||||
|
||||
<div class="ag-tabs">
|
||||
<span :class="{ on: tab === 'chat' }" @click="tab = 'chat'">{{ t('agents.tabChat') }}</span>
|
||||
<span :class="{ on: tab === 'config' }" @click="tab = 'config'">{{ t('agents.tabConfig') }}</span>
|
||||
<span :class="{ on: tab === 'runs' }" @click="tab = 'runs'; loadRuns()">{{ t('agents.tabRuns') }}</span>
|
||||
</div>
|
||||
|
||||
<!-- ① 对话 -->
|
||||
<div v-if="tab === 'chat'" class="chat-layout">
|
||||
<aside class="chat-side">
|
||||
<div class="card side-in">
|
||||
<el-button type="primary" class="new-conv" :disabled="streaming" @click="newChat">
|
||||
<Icon name="plus" :size="14" /><span>{{ t('agents.newChat') }}</span>
|
||||
</el-button>
|
||||
<div class="conv-list">
|
||||
<div
|
||||
v-for="c in sessions" :key="c.id" class="conv" :class="{ on: c.id === sessionId }"
|
||||
@click="switchSession(c.id)"
|
||||
>{{ c.title }}
|
||||
<small>{{ c.time }} · {{ c.n }} 条</small>
|
||||
</div>
|
||||
<div v-if="!sessions.length" class="hint">{{ t('common.empty') }}</div>
|
||||
</div>
|
||||
<div class="sep" />
|
||||
<div class="card-h" style="margin-bottom:var(--gap-xs)"><b style="font-size:12.5px">{{ t('agents.thisChat') }}</b></div>
|
||||
<div class="pv" style="gap:8px;font-size:12px">
|
||||
<div class="pv-i"><span class="nm">Tokens</span><span class="st">{{ lastUsage ? (lastUsage.prompt + lastUsage.completion) : 0 }}</span></div>
|
||||
<div class="pv-i"><span class="nm">{{ t('agents.toolsUsed') }}</span><span class="st">{{ stepsDone }}</span></div>
|
||||
<div class="pv-i"><span class="nm">{{ t('agents.elapsed') }}</span><span class="st">{{ elapsedText }}</span></div>
|
||||
</div>
|
||||
<div class="ops-row">
|
||||
<el-button v-if="streaming" size="small" class="op" @click="stopLocal">{{ t('agents.stop') }}</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<section class="chat-main">
|
||||
<!-- 风险提示(决策 ⑪ 要求常驻) -->
|
||||
<div class="risk-bar"><Icon name="bell" :size="12" />{{ t('agents.riskNote') }}</div>
|
||||
|
||||
<div class="card chat-card">
|
||||
<div class="card-h">
|
||||
<b>{{ curSessionTitle }}</b>
|
||||
<span class="sub">{{ messages.length }} 条</span>
|
||||
<div class="pager">
|
||||
<el-button size="small" :disabled="streaming || !messages.length" @click="copyAll">
|
||||
<Icon name="copy" :size="13" /><span>{{ t('chat.copyAll') }}</span>
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div ref="scrollEl" class="msgs">
|
||||
<div v-if="!messages.length" class="empty">
|
||||
<Icon name="bot" :size="34" />
|
||||
<div class="empty-t">{{ t('agents.emptyHint') }}</div>
|
||||
</div>
|
||||
|
||||
<div v-for="m in messages" :key="m.id" class="msg" :class="m.role === 'user' ? 'me' : 'ai'">
|
||||
<!-- ★ 工具执行轨迹 -->
|
||||
<div v-if="m.role === 'assistant' && m.steps && m.steps.length" class="trace">
|
||||
<div class="trace-h">
|
||||
<Icon name="bolt" :size="12" />
|
||||
{{ t('agents.execSteps', { n: m.steps.length }) }}
|
||||
<span class="trace-caret" @click="m.open = !m.open">{{ m.open ? '▴' : '▾' }}</span>
|
||||
</div>
|
||||
<template v-if="m.open">
|
||||
<div v-for="st in m.steps" :key="st.id" class="step" :class="st.status">
|
||||
<Icon :name="st.status === 'done' ? 'check' : 'refresh'" :size="13" />
|
||||
<span class="s-nm">{{ st.name || st.id }}</span>
|
||||
<span class="s-dt">{{ stepDetail(st) }}</span>
|
||||
<span class="s-ms">{{ st.ms != null ? st.ms + 'ms' : '' }}</span>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<div v-if="m.text" v-html="render(m.text)" />
|
||||
<div v-else-if="streaming && m.role === 'assistant'" class="typing">…</div>
|
||||
|
||||
<div class="meta">
|
||||
<template v-if="m.role === 'assistant' && m.meta">{{ m.meta }}</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="composer">
|
||||
<el-input
|
||||
v-model="input"
|
||||
type="textarea"
|
||||
:rows="2"
|
||||
resize="none"
|
||||
:placeholder="t('agents.inputPlaceholder')"
|
||||
@keydown.enter.exact.prevent="send"
|
||||
/>
|
||||
<div class="bar">
|
||||
<span class="tok">{{ cur.engine_model || '—' }}</span>
|
||||
<span style="flex:1" />
|
||||
<el-button v-if="!streaming" type="primary" size="small" :disabled="!input.trim()" @click="send">
|
||||
<Icon name="send" :size="13" />{{ t('chat.send') }}
|
||||
</el-button>
|
||||
<el-button v-else size="small" class="stop-btn" @click="stopLocal">
|
||||
<Icon name="ban" :size="13" />{{ t('agents.stop') }}
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<!-- ② 配置 -->
|
||||
<div v-if="tab === 'config'" class="cfg-grid">
|
||||
<div class="card" style="padding:14px">
|
||||
<div class="card-h"><b>{{ t('agents.cfgBasic') }}</b><span class="sub">{{ t('agents.cfgBasicSub') }}</span></div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.cfgName') }}</div>
|
||||
<el-input v-model="form.display_name" size="small" />
|
||||
</div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.cfgSlug') }}</div>
|
||||
<div class="f-val code">{{ cur.slug }}</div>
|
||||
</div>
|
||||
<div class="f-hint">{{ t('agents.cfgSlugHint') }}</div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.cfgDesc') }}</div>
|
||||
<el-input v-model="form.description" size="small" />
|
||||
</div>
|
||||
<div class="row-actions">
|
||||
<el-button type="primary" size="small" :disabled="!dirty" @click="saveCfg">{{ t('agents.save') }}</el-button>
|
||||
<el-button size="small" :loading="testing" @click="doTest">
|
||||
{{ testing ? t('agents.testing') : t('agents.test') }}
|
||||
</el-button>
|
||||
<span v-if="testResult" class="test-res" :class="{ bad: !testResult.ok }">
|
||||
{{ testResult.ok ? t('agents.testOk', { ms: testResult.ms }) : t('agents.testFail') + ':' + testResult.detail }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="padding:14px">
|
||||
<div class="card-h"><b>{{ t('agents.cfgModel') }}</b><span class="sub">{{ t('agents.cfgModelSub') }}</span></div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.cfgModel') }}</div>
|
||||
<div class="f-val">
|
||||
<span class="code">{{ cur.engine_model || '—' }}</span>
|
||||
<span class="f-lock"><Icon name="lock" :size="12" />{{ t('agents.cfgReadonly') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="f-hint">
|
||||
{{ t('agents.cfgModelHint') }}
|
||||
<a :href="qwenpawUrl" target="_blank" rel="noopener">{{ t('agents.cfgOpenQwenpaw') }} ↗</a>
|
||||
</div>
|
||||
<div class="card-h" style="margin-top:var(--gap)">
|
||||
<b>{{ t('agents.cfgParams') }}</b>
|
||||
</div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.cfgTemp') }}</div>
|
||||
<el-input v-model="form.params.temperature" size="small" placeholder="0.7" />
|
||||
</div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.cfgMaxTokens') }}</div>
|
||||
<el-input v-model="form.params.max_tokens" size="small" placeholder="—" />
|
||||
</div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.cfgTopP') }}</div>
|
||||
<el-input v-model="form.params.top_p" size="small" placeholder="—" />
|
||||
</div>
|
||||
<div class="frow" style="align-items:flex-start">
|
||||
<div class="f-lab">{{ t('agents.cfgSuffix') }}</div>
|
||||
<el-input v-model="form.params.system_suffix" type="textarea" :rows="3" resize="none" />
|
||||
</div>
|
||||
<div class="f-hint">{{ t('agents.cfgSuffixHint') }}</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="padding:14px">
|
||||
<div class="card-h">
|
||||
<b>{{ t('agents.cfgExec') }}</b>
|
||||
<span class="sub"><Icon name="lock" :size="12" />{{ t('agents.cfgExecSub') }}</span>
|
||||
</div>
|
||||
<div class="frow"><div class="f-lab">{{ t('agents.cfgEngine') }}</div>
|
||||
<div class="f-val">{{ cur.engine?.type }} · {{ cur.engine?.base_url }}</div></div>
|
||||
<div class="frow"><div class="f-lab">{{ t('agents.cfgWorkspace') }}</div>
|
||||
<div class="f-val">{{ cur.engine?.agent_id }}</div></div>
|
||||
<div class="f-row-note">{{ t('agents.cfgCapabilityHint') }}</div>
|
||||
<div class="f-hint">
|
||||
{{ t('agents.cfgPromptTitle') }}:{{ t('agents.cfgPromptHint') }}
|
||||
<a :href="qwenpawUrl" target="_blank" rel="noopener">{{ t('agents.cfgOpenQwenpaw') }} ↗</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card" style="padding:14px">
|
||||
<div class="card-h"><b>{{ t('agents.cfgAccess') }}</b><span class="sub">{{ t('agents.cfgAccessSub') }}</span></div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.cfgAccess') }}</div>
|
||||
<div class="f-val">
|
||||
{{ t('agents.cfgAccessAdmin') }}
|
||||
<span class="f-lock"><Icon name="lock" :size="12" />{{ t('agents.cfgReadonly') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="f-hint">{{ t('agents.cfgAccessHint') }}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- ③ 运行记录 -->
|
||||
<div v-if="tab === 'runs'">
|
||||
<div class="card" style="padding:14px">
|
||||
<div class="card-h"><b>{{ t('agents.runsDetail') }}</b><span class="sub">{{ t('agents.runsDetailSub') }}</span></div>
|
||||
<el-table v-if="runList.length" :data="runList" size="small" style="margin-top:var(--gap-s)">
|
||||
<el-table-column prop="at" :label="t('agents.colTime')" width="160" />
|
||||
<el-table-column prop="account" :label="t('agents.colAccount')" width="110" />
|
||||
<el-table-column prop="rounds" :label="t('agents.colRounds')" width="80" />
|
||||
<el-table-column prop="tools" :label="t('agents.colTools')" width="90" />
|
||||
<el-table-column prop="tokens" :label="t('agents.colTokens')" width="100" />
|
||||
<el-table-column :label="t('agents.colMs')" width="90">
|
||||
<template #default="{ row }">{{ (row.ms / 1000).toFixed(1) }}s</template>
|
||||
</el-table-column>
|
||||
<el-table-column :label="t('agents.colStatus')">
|
||||
<template #default="{ row }">
|
||||
<span class="pill" :class="runStatusCls(row.status)">{{ runStatusText(row.status) }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
<div v-else class="hint">{{ t('agents.runsEmpty') }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 新建弹窗 -->
|
||||
<el-dialog v-model="createOpen" :title="t('agents.newAgent')" width="520px">
|
||||
<div class="shared-note">{{ t('agents.sharedNote') }}</div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.formName') }}</div>
|
||||
<el-input v-model="create.display_name" size="small" />
|
||||
</div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.formSlug') }}</div>
|
||||
<el-input v-model="create.slug" size="small" placeholder="code-review" />
|
||||
</div>
|
||||
<div class="frow">
|
||||
<div class="f-lab">{{ t('agents.formDesc') }}</div>
|
||||
<el-input v-model="create.description" size="small" />
|
||||
</div>
|
||||
<div class="frow" style="align-items:flex-start">
|
||||
<div class="f-lab">{{ t('agents.formSuffix') }}</div>
|
||||
<el-input v-model="create.system_suffix" type="textarea" :rows="2" resize="none" />
|
||||
</div>
|
||||
<template #footer>
|
||||
<el-button size="small" @click="createOpen = false">{{ t('agents.cancel') }}</el-button>
|
||||
<el-button size="small" type="primary" @click="doCreate">{{ t('agents.create') }}</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onMounted, reactive, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import Icon from '../components/Icon.vue'
|
||||
import {
|
||||
type AgentEvent, type AgentItem, type AgentRun,
|
||||
agentRuns, agentSessions, createAgent, listAgents,
|
||||
restoreAgent, retireAgent, streamAgentChat, testAgent, updateAgent,
|
||||
} from '../lib/agents'
|
||||
import { renderMarkdown } from '../lib/markdown'
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
interface Msg { id: number; role: 'user' | 'assistant'; text: string; steps: Step[]; open?: boolean; meta?: string }
|
||||
interface Step { id: string; name?: string; args?: any; status: 'running' | 'done'; ms?: number; output?: any }
|
||||
|
||||
const err = ref('')
|
||||
const data = reactive<{ agents: AgentItem[]; max_agents: number; active: number; engine: any }>({
|
||||
agents: [], max_agents: 5, active: 0,
|
||||
engine: { model: '', ready: false, base_url: '', agent_id: '' },
|
||||
})
|
||||
const showRetired = ref(false)
|
||||
const current = ref('')
|
||||
const tab = ref<'chat' | 'config' | 'runs'>('chat')
|
||||
const runList = ref<AgentRun[]>([])
|
||||
|
||||
const messages = ref<Msg[]>([])
|
||||
const sessions = ref<any[]>([])
|
||||
const sessionId = ref('')
|
||||
const input = ref('')
|
||||
const streaming = ref(false)
|
||||
const stepsDone = ref(0)
|
||||
const lastUsage = ref<any>(null)
|
||||
const startedAt = ref(0)
|
||||
const elapsedText = ref('—')
|
||||
const scrollEl = ref<HTMLElement | null>(null)
|
||||
let abort: (() => void) | null = null
|
||||
|
||||
const form = reactive({ display_name: '', description: '', params: { temperature: '', max_tokens: '', top_p: '', system_suffix: '' } as any })
|
||||
const createOpen = ref(false)
|
||||
const create = reactive({ display_name: '', slug: '', description: '', system_suffix: '' })
|
||||
const testing = ref(false)
|
||||
const testResult = ref<{ ok: boolean; ms: number; detail: string } | null>(null)
|
||||
|
||||
const cur = computed(() => data.agents.find((a) => a.slug === current.value) || null)
|
||||
const hasRetired = computed(() => data.agents.some((a) => a.status === 'retired'))
|
||||
const activeCount = computed(() => data.agents.filter((a) => a.status !== 'retired').length)
|
||||
const atLimit = computed(() => activeCount.value >= data.max_agents)
|
||||
const totalRuns = computed(() => data.agents.reduce((s, a) => s + (a.run_total || 0), 0))
|
||||
const totalTokensText = computed(() => `${(totalRuns.value * 12).toFixed(0)}k`)
|
||||
const qwenpawUrl = computed(() => (data.engine.base_url || 'http://127.0.0.1:8088').replace(/\/api$/, ''))
|
||||
const curSessionTitle = computed(() => sessions.value.find((s) => s.id === sessionId.value)?.title || t('agents.newChat'))
|
||||
const dirty = computed(() => {
|
||||
const c = cur.value; if (!c) return false
|
||||
const p = c.params || {}
|
||||
return form.display_name !== c.display_name || form.description !== c.description
|
||||
|| String(form.params.temperature ?? '') !== String(p.temperature ?? '')
|
||||
|| String(form.params.max_tokens ?? '') !== String(p.max_tokens ?? '')
|
||||
|| String(form.params.top_p ?? '') !== String(p.top_p ?? '')
|
||||
|| String(form.params.system_suffix ?? '') !== String(p.system_suffix ?? '')
|
||||
})
|
||||
|
||||
function statusText(s: string) {
|
||||
return s === 'retired' ? t('agents.statusRetired') : s === 'draft' ? t('agents.statusDraft') : t('agents.statusPublished')
|
||||
}
|
||||
function runStatusText(s: string) {
|
||||
return s === 'ok' ? t('agents.runOk') : s === 'stopped' ? t('agents.runStopped') : s === 'error' ? t('agents.runTimeout') : s
|
||||
}
|
||||
function runStatusCls(s: string) { return s === 'ok' ? 'ok' : s === 'error' ? 'err' : 'mute' }
|
||||
function render(md: string) { return renderMarkdown(md || '') }
|
||||
function stepDetail(st: Step) {
|
||||
if (st.status === 'done' && st.output != null) return String(st.output).slice(0, 120)
|
||||
if (st.args) { try { return JSON.stringify(st.args).slice(0, 120) } catch { return '' } }
|
||||
return ''
|
||||
}
|
||||
|
||||
async function load() {
|
||||
err.value = ''
|
||||
try {
|
||||
const j = await listAgents(showRetired.value)
|
||||
data.agents = j.agents; data.max_agents = j.max_agents; data.active = j.active; data.engine = j.engine
|
||||
if (!cur.value) {
|
||||
const first = data.agents.find((a) => a.status !== 'retired')
|
||||
if (first) pick(first.slug)
|
||||
}
|
||||
} catch (e: any) { err.value = String(e?.message || e) }
|
||||
}
|
||||
|
||||
function syncForm(a: AgentItem) {
|
||||
form.display_name = a.display_name
|
||||
form.description = a.description || ''
|
||||
const p = a.params || {}
|
||||
form.params.temperature = p.temperature ?? ''
|
||||
form.params.max_tokens = p.max_tokens ?? ''
|
||||
form.params.top_p = p.top_p ?? ''
|
||||
form.params.system_suffix = p.system_suffix ?? ''
|
||||
}
|
||||
|
||||
async function pick(slug: string) {
|
||||
current.value = slug
|
||||
tab.value = 'chat'
|
||||
const a = cur.value
|
||||
if (a) syncForm(a)
|
||||
sessionId.value = ''
|
||||
messages.value = []
|
||||
await loadSessions()
|
||||
}
|
||||
|
||||
async function loadSessions() {
|
||||
if (!current.value) return
|
||||
try {
|
||||
const j = await agentSessions(current.value)
|
||||
sessions.value = (j.items || []).map((it: any) => ({
|
||||
id: it.id, title: it.title || t('agents.runDefaultTitle'), time: it.time || '', n: (it.messages || []).length, raw: it,
|
||||
}))
|
||||
} catch { sessions.value = [] }
|
||||
}
|
||||
|
||||
function newChat() {
|
||||
sessionId.value = ''
|
||||
messages.value = []
|
||||
stepsDone.value = 0
|
||||
lastUsage.value = null
|
||||
elapsedText.value = '—'
|
||||
}
|
||||
|
||||
function switchSession(id: string) {
|
||||
const s = sessions.value.find((x) => x.id === id)
|
||||
sessionId.value = id
|
||||
messages.value = (s?.raw?.messages || []).map((m: any, i: number) => ({
|
||||
id: i, role: m.role === 'user' ? 'user' : 'assistant', text: m.content || '', steps: [], meta: '',
|
||||
}))
|
||||
}
|
||||
|
||||
async function persistSession() {
|
||||
if (!current.value || !messages.value.length) return
|
||||
const body = {
|
||||
id: sessionId.value || `s-${Date.now()}`,
|
||||
title: (messages.value.find((m) => m.role === 'user')?.text || t('agents.runDefaultTitle')).slice(0, 24),
|
||||
time: new Date().toLocaleString('zh-CN', { hour12: false }).slice(5, 16),
|
||||
messages: messages.value.map((m) => ({ role: m.role, content: m.text })),
|
||||
}
|
||||
sessionId.value = body.id
|
||||
try {
|
||||
await fetch(`/history/agent_${current.value}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${localStorage.getItem('gw_token') || ''}` },
|
||||
body: JSON.stringify({ items: [body] }),
|
||||
})
|
||||
await loadSessions()
|
||||
} catch { /* 持久化失败不阻塞对话 */ }
|
||||
}
|
||||
|
||||
function send() {
|
||||
const text = input.value.trim()
|
||||
if (!text || streaming.value || !current.value) return
|
||||
err.value = ''
|
||||
messages.value.push({ id: Date.now(), role: 'user', text, steps: [] })
|
||||
const reply: Msg = { id: Date.now() + 1, role: 'assistant', text: '', steps: [], open: true, meta: '' }
|
||||
messages.value.push(reply)
|
||||
input.value = ''
|
||||
streaming.value = true
|
||||
stepsDone.value = 0
|
||||
lastUsage.value = null
|
||||
startedAt.value = Date.now()
|
||||
elapsedText.value = '—'
|
||||
scroll()
|
||||
|
||||
const history = messages.value
|
||||
.filter((m) => m !== reply)
|
||||
.map((m) => ({ role: m.role, content: m.text }))
|
||||
|
||||
abort = streamAgentChat(current.value, history, sessionId.value || `agent-${current.value}-${Date.now()}`, (ev: AgentEvent) => {
|
||||
if (ev.type === 'start') { sessionId.value = ev.session_id }
|
||||
else if (ev.type === 'text') { reply.text += ev.text; scroll() }
|
||||
else if (ev.type === 'step') {
|
||||
if (ev.status === 'running') reply.steps.push({ id: ev.id, name: ev.name, args: ev.args, status: 'running' })
|
||||
else {
|
||||
const st = reply.steps.find((x) => x.id === ev.id)
|
||||
if (st) { st.status = 'done'; st.ms = ev.ms; st.output = ev.output }
|
||||
stepsDone.value++
|
||||
}
|
||||
scroll()
|
||||
} else if (ev.type === 'usage') {
|
||||
lastUsage.value = ev
|
||||
reply.meta = `${ev.model || ''} · ${ev.prompt + ev.completion} tokens`
|
||||
+ (ev.cached ? ` · ${t('agents.cacheHit', { n: ev.cached })}` : '')
|
||||
} else if (ev.type === 'done') {
|
||||
reply.open = false
|
||||
finish()
|
||||
} else if (ev.type === 'error') {
|
||||
err.value = ev.message
|
||||
reply.text += `\n\n> ⚠️ ${ev.message}`
|
||||
finish()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function finish() {
|
||||
streaming.value = false
|
||||
elapsedText.value = `${((Date.now() - startedAt.value) / 1000).toFixed(1)}s`
|
||||
abort = null
|
||||
persistSession()
|
||||
scroll()
|
||||
}
|
||||
|
||||
function stopLocal() {
|
||||
if (abort) abort()
|
||||
streaming.value = false
|
||||
elapsedText.value = `${((Date.now() - startedAt.value) / 1000).toFixed(1)}s`
|
||||
}
|
||||
|
||||
function scroll() {
|
||||
nextTick(() => { if (scrollEl.value) scrollEl.value.scrollTop = scrollEl.value.scrollHeight })
|
||||
}
|
||||
|
||||
function copyAll() {
|
||||
const text = messages.value.map((m) => `${m.role === 'user' ? t('agents.meLabel') : t('agents.aiLabel')}:${m.text}`).join('\n\n')
|
||||
navigator.clipboard?.writeText(text)
|
||||
}
|
||||
|
||||
async function saveCfg() {
|
||||
if (!cur.value) return
|
||||
err.value = ''
|
||||
const patch: any = { display_name: form.display_name, description: form.description, params: {} }
|
||||
for (const k of ['temperature', 'max_tokens', 'top_p'] as const) {
|
||||
const v = form.params[k]
|
||||
if (v !== '' && v != null) patch.params[k] = Number(v)
|
||||
}
|
||||
if (form.params.system_suffix) patch.params.system_suffix = form.params.system_suffix
|
||||
try {
|
||||
await updateAgent(cur.value.slug, patch)
|
||||
await load()
|
||||
await pick(cur.value.slug)
|
||||
} catch (e: any) { err.value = String(e?.message || e) }
|
||||
}
|
||||
|
||||
async function doTest() {
|
||||
if (!cur.value) return
|
||||
testing.value = true; testResult.value = null
|
||||
try { testResult.value = await testAgent(cur.value.slug) }
|
||||
catch (e: any) { testResult.value = { ok: false, ms: 0, detail: String(e?.message || e) } }
|
||||
finally { testing.value = false }
|
||||
}
|
||||
|
||||
async function doRetire() {
|
||||
if (!cur.value || !confirm(t('agents.confirmDelete'))) return
|
||||
try { await retireAgent(cur.value.slug); await load(); pick(data.agents[0]?.slug || '') }
|
||||
catch (e: any) { err.value = String(e?.message || e) }
|
||||
}
|
||||
|
||||
async function doRestore() {
|
||||
if (!cur.value) return
|
||||
try { await restoreAgent(cur.value.slug); await load(); pick(cur.value.slug) }
|
||||
catch (e: any) { err.value = String(e?.message || e) }
|
||||
}
|
||||
|
||||
function openCreate() {
|
||||
create.display_name = ''; create.slug = ''; create.description = ''; create.system_suffix = ''
|
||||
createOpen.value = true
|
||||
}
|
||||
|
||||
async function doCreate() {
|
||||
err.value = ''
|
||||
const body: any = {
|
||||
display_name: create.display_name, slug: create.slug.trim(),
|
||||
description: create.description,
|
||||
}
|
||||
if (create.system_suffix) body.params = { system_suffix: create.system_suffix }
|
||||
try {
|
||||
const j = await createAgent(body)
|
||||
createOpen.value = false
|
||||
await load()
|
||||
await pick(j.agent.slug)
|
||||
} catch (e: any) { err.value = String(e?.message || e) }
|
||||
}
|
||||
|
||||
async function loadRuns() {
|
||||
if (!current.value) return
|
||||
try { runList.value = (await agentRuns(current.value)).items || [] }
|
||||
catch { runList.value = [] }
|
||||
}
|
||||
|
||||
onMounted(load)
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.sub-hint { margin-left: 8px; color: var(--muted); font-size: 12.5px; }
|
||||
.row-h { display: flex; align-items: center; gap: var(--gap-sm); margin-bottom: var(--gap-lg); }
|
||||
.ag-layout { display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: var(--gap-lg); align-items: start; }
|
||||
/* KPI 卡等高(第三十八轮:之前几张高度不一,因为内容行数不同) */
|
||||
.ag-kpis { align-items: stretch; }
|
||||
.ag-kpis .kpi { display: flex; flex-direction: column; }
|
||||
.ag-kpis .kpi .f { margin-top: auto; }
|
||||
.ag-layout > * { min-height: 0; }
|
||||
.ag-list { display: flex; flex-direction: column; gap: 2px; }
|
||||
.ag { display: flex; gap: 10px; align-items: flex-start; padding: 10px; border-radius: var(--radius); border: 1px solid transparent; cursor: pointer; }
|
||||
.ag:hover { background: var(--surface-2); }
|
||||
.ag.on { background: var(--primary-soft); border-color: color-mix(in srgb, var(--primary) 32%, transparent); }
|
||||
.ag-ico { width: 34px; height: 34px; flex: none; border-radius: 10px; display: grid; place-items: center; background: var(--chip); color: var(--primary); border: 1px solid var(--line); }
|
||||
.ag-main { min-width: 0; flex: 1; }
|
||||
.ag-nm { font-weight: 650; font-size: 13.5px; }
|
||||
.ag-ds { font-size: 12px; color: var(--muted); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.ag-tags { display: flex; align-items: center; gap: 6px; margin-top: 7px; }
|
||||
.ag-md { font-family: ui-monospace, Menlo, monospace; font-size: 11px; color: var(--muted-2); }
|
||||
.ag-right { flex: none; }
|
||||
.ag-empty { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; text-align: center; padding: var(--pad-page); min-height: 340px; }
|
||||
.ae-ico { width: 64px; height: 64px; border-radius: 18px; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); }
|
||||
.ae-t { font-weight: 650; font-size: 14.5px; }
|
||||
.ae-s { font-size: 12.5px; color: var(--muted); line-height: 1.7; }
|
||||
.ae-btn { margin-top: 8px; display: inline-flex; align-items: center; gap: 6px; padding: 7px 14px; border-radius: 10px; font-size: 12.5px; font-weight: 600; background: var(--primary); color: var(--primary-ink); cursor: pointer; }
|
||||
.ag-detail { display: flex; flex-direction: column; gap: var(--gap-lg); min-width: 0; }
|
||||
.ag-top { display: flex; align-items: center; gap: 9px; }
|
||||
.ag-face { width: 28px; height: 28px; border-radius: 9px; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); flex: none; }
|
||||
.ag-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); }
|
||||
.ag-tabs span { padding: 7px 14px; font-size: 12.5px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; cursor: pointer; }
|
||||
.ag-tabs span.on { color: var(--primary); border-bottom-color: var(--primary); }
|
||||
.risk-bar { display: flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--warn); background: var(--warn-soft); padding: 6px 10px; border-radius: var(--radius-s); margin-bottom: var(--gap-sm); }
|
||||
.chat-layout { display: grid; grid-template-columns: 236px minmax(0, 1fr); gap: var(--gap-lg); align-items: start; }
|
||||
.chat-side .card { padding: 12px; display: flex; flex-direction: column; overflow: hidden; max-height: 100%; }
|
||||
.new-conv { width: 100%; margin-bottom: 10px; }
|
||||
.conv-list { flex: 0 1 auto; overflow: auto; min-height: 0; max-height: 42%; }
|
||||
.conv { padding: 7px 9px; border-radius: var(--radius-s); font-size: 12.5px; cursor: pointer; }
|
||||
.conv small { display: block; color: var(--muted-2); font-size: 11px; margin-top: 2px; }
|
||||
.conv.on { background: var(--primary-soft); }
|
||||
.chat-main { display: flex; flex-direction: column; min-width: 0; height: calc(100vh - 300px); min-height: 400px; }
|
||||
.chat-card { flex: 1; display: flex; flex-direction: column; overflow: hidden; padding: 14px; }
|
||||
.msgs { flex: 1; overflow: auto; margin-top: var(--gap); min-height: 0; }
|
||||
.trace { border: 1px solid var(--line); border-left: 2px solid var(--primary); border-radius: 10px; padding: 8px 10px; margin-bottom: 9px; background: var(--surface-2); }
|
||||
.trace-h { display: flex; align-items: center; gap: 6px; font-size: 11.5px; font-weight: 600; color: var(--primary); }
|
||||
.trace-caret { margin-left: auto; color: var(--muted-2); cursor: pointer; }
|
||||
.step { display: flex; align-items: center; gap: 7px; font-size: 11.5px; padding: 4px 0 0; color: var(--muted); }
|
||||
.step.ok svg { color: var(--ok); }
|
||||
.step.running svg { color: var(--warn); }
|
||||
.s-nm { font-family: ui-monospace, Menlo, monospace; font-size: 11px; color: var(--text); }
|
||||
.s-dt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.s-ms { margin-left: auto; color: var(--muted-2); }
|
||||
.composer { border-top: 1px solid var(--line-2); padding-top: var(--gap-sm); }
|
||||
.bar { display: flex; align-items: center; gap: var(--gap-sm); margin-top: 8px; }
|
||||
.tok { font-size: 11.5px; color: var(--muted-2); font-family: ui-monospace, Menlo, monospace; }
|
||||
.stop-btn { background: var(--err-soft); color: var(--err); border-color: transparent; }
|
||||
.typing { color: var(--muted-2); }
|
||||
.cfg-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--gap-lg); align-items: start; }
|
||||
.cfg-grid > .card:first-child { grid-column: 1 / -1; }
|
||||
.frow { display: flex; align-items: center; gap: 10px; margin-top: var(--gap-s); }
|
||||
.f-lab { width: 78px; flex: none; font-size: 12px; color: var(--muted); }
|
||||
.f-val { flex: 1; min-width: 0; display: flex; align-items: center; gap: 7px; font-size: 12.5px; padding: 7px 10px; border-radius: 9px; background: var(--chip); }
|
||||
.f-val.code { font-family: ui-monospace, Menlo, monospace; font-size: 11.5px; }
|
||||
.f-lock { margin-left: auto; display: inline-flex; align-items: center; gap: 4px; font-size: 10.5px; color: var(--muted-2); }
|
||||
.f-hint { font-size: 11px; color: var(--muted-2); line-height: 1.6; margin: var(--gap-xs) 0 0 88px; }
|
||||
.f-row-note { font-size: 11.5px; color: var(--muted); line-height: 1.6; margin-top: var(--gap-s); }
|
||||
.row-actions { display: flex; align-items: center; gap: var(--gap-sm); margin-top: var(--gap); }
|
||||
.test-res { font-size: 11.5px; color: var(--ok); }
|
||||
.test-res.bad { color: var(--err); }
|
||||
.shared-note { font-size: 12px; color: var(--muted); background: var(--chip); padding: 8px 10px; border-radius: var(--radius-s); margin-bottom: var(--gap-s); }
|
||||
.ops-row { display: flex; gap: 7px; margin-top: 12px; }
|
||||
@media (max-width: 768px) {
|
||||
.ag-layout { grid-template-columns: 1fr; }
|
||||
.chat-layout { grid-template-columns: 1fr; height: auto; }
|
||||
.chat-side { display: none; }
|
||||
.cfg-grid { grid-template-columns: 1fr; }
|
||||
.cfg-grid > .card:first-child { grid-column: auto; }
|
||||
}
|
||||
</style>
|
||||
Vendored
+114
@@ -0,0 +1,114 @@
|
||||
data: {"id":"response_f5a2f704a9864e86839393e63d6e8cbc","output":[],"status":"created","created_at":"2026-09-29T04:17:20+00:00","completed_at":null,"metadata":null,"object":"response","session_id":"probe-tool-flow","sequence_number":1}
|
||||
|
||||
data: {"id":"response_f5a2f704a9864e86839393e63d6e8cbc","output":[],"status":"in_progress","created_at":"2026-09-29T04:17:20+00:00","completed_at":null,"metadata":null,"object":"response","session_id":"probe-tool-flow","sequence_number":2}
|
||||
|
||||
data: {"id":"msg_c9b1cc9ebc644ff4a26a1a88eeb4213e","type":"message","role":"assistant","content":[],"status":"in_progress","metadata":null,"name":"assistant","object":"message","sequence_number":3}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_c9b1cc9ebc644ff4a26a1a88eeb4213e", "text": "I", "sequence_number": 4}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_c9b1cc9ebc644ff4a26a1a88eeb4213e", "text": "'ll read that", "sequence_number": 5}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_c9b1cc9ebc644ff4a26a1a88eeb4213e", "text": " file for", "sequence_number": 6}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_c9b1cc9ebc644ff4a26a1a88eeb4213e", "text": " you.\n\n", "sequence_number": 7}
|
||||
|
||||
data: {"type":"text","delta":false,"index":0,"status":null,"object":"content","msg_id":"msg_c9b1cc9ebc644ff4a26a1a88eeb4213e","text":"I'll read that file for you.\n\n","sequence_number":8}
|
||||
|
||||
data: {"id":"msg_c9b1cc9ebc644ff4a26a1a88eeb4213e","type":"message","role":"assistant","content":[{"type":"text","delta":false,"index":0,"status":null,"object":"content","msg_id":null,"text":"I'll read that file for you.\n\n"}],"status":"completed","metadata":null,"name":"assistant","object":"message","sequence_number":9}
|
||||
|
||||
data: {"id":"msg_72e987ec0cb5460581b85c365b86182f","type":"plugin_call","role":"assistant","content":[],"status":"in_progress","metadata":null,"name":"assistant","object":"message","sequence_number":10}
|
||||
|
||||
data: {"type": "data", "delta": true, "index": 0, "status": "in_progress", "object": "content", "msg_id": "msg_72e987ec0cb5460581b85c365b86182f", "data": {"call_id": "chatcmpl-tool-b207f3da7699c9fa", "name": "read_file", "arguments": ""}, "sequence_number": 11}
|
||||
|
||||
data: {"type": "data", "delta": true, "index": 0, "status": "in_progress", "object": "content", "msg_id": "msg_72e987ec0cb5460581b85c365b86182f", "data": {"arguments": ""}, "sequence_number": 12}
|
||||
|
||||
data: {"type": "data", "delta": true, "index": 0, "status": "in_progress", "object": "content", "msg_id": "msg_72e987ec0cb5460581b85c365b86182f", "data": {"arguments": ""}, "sequence_number": 13}
|
||||
|
||||
data: {"type": "data", "delta": true, "index": 0, "status": "in_progress", "object": "content", "msg_id": "msg_72e987ec0cb5460581b85c365b86182f", "data": {"arguments": ""}, "sequence_number": 14}
|
||||
|
||||
data: {"type": "data", "delta": true, "index": 0, "status": "in_progress", "object": "content", "msg_id": "msg_72e987ec0cb5460581b85c365b86182f", "data": {"arguments": "{\"file_path\": \"/tmp/probe-agent.txt\"}"}, "sequence_number": 15}
|
||||
|
||||
data: {"type":"data","delta":false,"index":null,"status":"completed","object":"content","msg_id":null,"data":{"call_id":"chatcmpl-tool-b207f3da7699c9fa","name":"read_file","arguments":"{\"file_path\": \"/tmp/probe-agent.txt\"}"},"sequence_number":16}
|
||||
|
||||
data: {"id":"msg_72e987ec0cb5460581b85c365b86182f","type":"plugin_call","role":"assistant","content":[{"type":"data","delta":false,"index":null,"status":"completed","object":"content","msg_id":null,"data":{"call_id":"chatcmpl-tool-b207f3da7699c9fa","name":"read_file","arguments":"{\"file_path\": \"/tmp/probe-agent.txt\"}"},"sequence_number":16}],"status":"completed","metadata":null,"name":"assistant","object":"message","sequence_number":17}
|
||||
|
||||
data: {"id":"msg_fcd7df26a1d04ea4adb2b318b82be07e","type":"plugin_call_output","role":"tool","content":[],"status":"in_progress","metadata":null,"name":"assistant","object":"message","sequence_number":18}
|
||||
|
||||
data: {"type":"data","delta":false,"index":0,"status":"in_progress","object":"content","msg_id":"msg_fcd7df26a1d04ea4adb2b318b82be07e","data":{"call_id":"chatcmpl-tool-b207f3da7699c9fa","name":"read_file","output":""},"sequence_number":19}
|
||||
|
||||
data: {"type":"data","delta":false,"index":0,"status":"in_progress","object":"content","msg_id":"msg_fcd7df26a1d04ea4adb2b318b82be07e","data":{"call_id":"chatcmpl-tool-b207f3da7699c9fa","name":"read_file","output":"Error: The file /tmp/probe-agent.txt does not exist."},"sequence_number":20}
|
||||
|
||||
data: {"type":"data","delta":false,"index":0,"status":"completed","object":"content","msg_id":null,"data":{"call_id":"chatcmpl-tool-b207f3da7699c9fa","name":"read_file","output":"Error: The file /tmp/probe-agent.txt does not exist.","state":"error"},"sequence_number":21}
|
||||
|
||||
data: {"id":"msg_fcd7df26a1d04ea4adb2b318b82be07e","type":"plugin_call_output","role":"tool","content":[{"type":"data","delta":false,"index":0,"status":"completed","object":"content","msg_id":null,"data":{"call_id":"chatcmpl-tool-b207f3da7699c9fa","name":"read_file","output":"Error: The file /tmp/probe-agent.txt does not exist.","state":"error"},"sequence_number":21}],"status":"completed","metadata":null,"name":"assistant","object":"message","sequence_number":22}
|
||||
|
||||
data: {"id":"msg_d4642dbdee5d453493a61c3a167ca2f6","type":"message","role":"assistant","content":[],"status":"in_progress","metadata":null,"name":"assistant","object":"message","sequence_number":23}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "文件", "sequence_number": 24}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "不存在,读不到内容", "sequence_number": 25}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "。", "sequence_number": 26}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "😕\n\n`/tmp/pro", "sequence_number": 27}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "be-agent.txt` 没有", "sequence_number": 28}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "找到", "sequence_number": 29}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": " —— 可能是", "sequence_number": 30}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "路径写", "sequence_number": 31}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "错了,或者文件", "sequence_number": 32}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "还没", "sequence_number": 33}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "创建/已经被", "sequence_number": 34}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "清掉了", "sequence_number": 35}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "。", "sequence_number": 36}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "要我", "sequence_number": 37}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "看看", "sequence_number": 38}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": " `/tmp` 下", "sequence_number": 39}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "有什么别的", "sequence_number": 40}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "文件吗?\n\n", "sequence_number": 41}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 42}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 43}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 44}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 45}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 46}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 47}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 48}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 49}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 50}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 51}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 52}
|
||||
|
||||
data: {"type": "text", "delta": true, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "", "sequence_number": 53}
|
||||
|
||||
data: {"type": "text", "delta": false, "index": 0, "status": null, "object": "content", "msg_id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "text": "文件不存在,读不到内容。😕\n\n`/tmp/probe-agent.txt` 没有找到 —— 可能是路径写错了,或者文件还没创建/已经被清掉了。要我看看 `/tmp` 下有什么别的文件吗?", "sequence_number": 54}
|
||||
|
||||
data: {"id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "type": "message", "role": "assistant", "content": [{"type": "text", "delta": false, "index": 0, "status": null, "object": "content", "msg_id": null, "text": "文件不存在,读不到内容。😕\n\n`/tmp/probe-agent.txt` 没有找到 —— 可能是路径写错了,或者文件还没创建/已经被清掉了。要我看看 `/tmp` 下有什么别的文件吗?"}], "status": "completed", "metadata": null, "name": "assistant", "object": "message", "sequence_number": 55, "usage": {"input_tokens": 13864, "output_tokens": 90}}
|
||||
|
||||
data: {"id": "response_f5a2f704a9864e86839393e63d6e8cbc", "output": [{"id": "msg_c9b1cc9ebc644ff4a26a1a88eeb4213e", "type": "message", "role": "assistant", "content": [{"type": "text", "delta": false, "index": 0, "status": null, "object": "content", "msg_id": null, "text": "I'll read that file for you.\n\n"}], "status": "completed", "metadata": null, "name": "assistant", "object": "message", "sequence_number": 9}, {"id": "msg_72e987ec0cb5460581b85c365b86182f", "type": "plugin_call", "role": "assistant", "content": [{"type": "data", "delta": false, "index": null, "status": "completed", "object": "content", "msg_id": null, "data": {"call_id": "chatcmpl-tool-b207f3da7699c9fa", "name": "read_file", "arguments": "{\"file_path\": \"/tmp/probe-agent.txt\"}"}, "sequence_number": 16}], "status": "completed", "metadata": null, "name": "assistant", "object": "message", "sequence_number": 17}, {"id": "msg_fcd7df26a1d04ea4adb2b318b82be07e", "type": "plugin_call_output", "role": "tool", "content": [{"type": "data", "delta": false, "index": 0, "status": "completed", "object": "content", "msg_id": null, "data": {"call_id": "chatcmpl-tool-b207f3da7699c9fa", "name": "read_file", "output": "Error: The file /tmp/probe-agent.txt does not exist.", "state": "error"}, "sequence_number": 21}], "status": "completed", "metadata": null, "name": "assistant", "object": "message", "sequence_number": 22}, {"id": "msg_d4642dbdee5d453493a61c3a167ca2f6", "type": "message", "role": "assistant", "content": [{"type": "text", "delta": false, "index": 0, "status": null, "object": "content", "msg_id": null, "text": "文件不存在,读不到内容。😕\n\n`/tmp/probe-agent.txt` 没有找到 —— 可能是路径写错了,或者文件还没创建/已经被清掉了。要我看看 `/tmp` 下有什么别的文件吗?"}], "status": "completed", "metadata": null, "name": "assistant", "object": "message", "sequence_number": 55, "usage": {"input_tokens": 13864, "output_tokens": 90}}], "status": "completed", "created_at": "2026-09-29T04:17:20+00:00", "completed_at": "2026-09-29T04:17:23+00:00", "metadata": null, "object": "response", "session_id": "probe-tool-flow", "sequence_number": 56, "usage": {"input_tokens": 13864, "output_tokens": 90}}
|
||||
|
||||
data: {"type": "turn_usage", "session_id": "probe-tool-flow", "usage": {"provider_id": "opencode", "model_name": "deepseek-v4.1-flash", "prompt_tokens": 27653, "completion_tokens": 141, "total_tokens": 27794, "cache_read_tokens": 17152, "cache_write_tokens": 0, "cache_eligible_input_tokens": 27653, "cache_observed": true, "cache_hit_rate": 62.02581998336528, "context_size": 1000000, "compact_threshold": 0.8, "session_cache_read_tokens": 17152, "session_cache_eligible_input_tokens": 27653, "session_cache_observed": true, "session_cache_hit_rate": 62.02581998336528}, "context_usage": {"estimated_tokens": 149, "max_input_length": 1000000, "context_usage_ratio": 0.014899999999999998}}
|
||||
|
||||
@@ -5712,6 +5712,261 @@ def test_sync_dots_fetch_report_and_apply():
|
||||
assert sm.apply_plan(out, plan, "2026-09-29") == out
|
||||
|
||||
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
# 第三十八轮:智能体(路线 A)—— 存储校验 / 权限 / SSE 映射 / 防自指
|
||||
# ══════════════════════════════════════════════════════════════════
|
||||
|
||||
def _tmp_agents_yaml():
|
||||
"""每用例一份干净的 agents.yaml(绝不碰生产的 data/agents.yaml)。"""
|
||||
import tempfile, pathlib as _pl
|
||||
d = _pl.Path(tempfile.mkdtemp())
|
||||
f = d / "agents.yaml"
|
||||
f.write_text("max_agents: 5\nagents: {}\n", encoding="utf-8")
|
||||
return f
|
||||
|
||||
|
||||
def test_agents_store_create_validate_and_limit():
|
||||
"""智能体存储:创建 / slug 查重 / params 白名单 / 数量上限 / 归档恢复。"""
|
||||
from app.agents import AgentStore, AgentsError, DEFAULT_MAX_AGENTS
|
||||
|
||||
st = AgentStore(_tmp_agents_yaml())
|
||||
assert st.max_agents == DEFAULT_MAX_AGENTS == 5
|
||||
assert st.list() == []
|
||||
|
||||
a = st.create(slug="code-review", display_name="代码审查",
|
||||
params={"temperature": 0.2, "system_suffix": "只给结论"})
|
||||
assert a.slug == "code-review" and a.params["temperature"] == 0.2
|
||||
assert a.engine_agent_id == "ai-gateway" # 默认共用同一个 profile
|
||||
assert "model" not in a.to_dict() # 甲案:不存 model
|
||||
|
||||
# slug 形态
|
||||
for bad in ("Bad", "Code-Review", "x", "a" * 40, "9start"):
|
||||
try:
|
||||
st.create(slug=bad, display_name="x")
|
||||
raise AssertionError(f"{bad} 本应被拒")
|
||||
except AgentsError:
|
||||
pass
|
||||
|
||||
# 三重查重
|
||||
for bad, kw in (("ai-gateway", {"profile_ids": ["ai-gateway"]}),
|
||||
("wjd", {"alias_names": ["wjd"]}),
|
||||
("agnes-3.0-flash", {"model_ids": ["agnes-3.0-flash"]}),
|
||||
("code-review", {})):
|
||||
try:
|
||||
st.create(slug=bad, display_name="x", **kw)
|
||||
raise AssertionError(f"{bad} 本应被拒")
|
||||
except AgentsError:
|
||||
pass
|
||||
|
||||
# params 白名单(结构性键必须被拦)
|
||||
for badp in ({"model": "x"}, {"messages": []}, {"tools": []}):
|
||||
try:
|
||||
st.create(slug="p-test", display_name="x", params=badp)
|
||||
raise AssertionError(f"params {badp} 本应被拒")
|
||||
except AgentsError:
|
||||
pass
|
||||
# system_suffix 超长
|
||||
try:
|
||||
st.create(slug="p-long", display_name="x", params={"system_suffix": "x" * 2001})
|
||||
raise AssertionError("超长 suffix 本应被拒")
|
||||
except AgentsError:
|
||||
pass
|
||||
# 越界值
|
||||
for badv in ({"temperature": 9.9}, {"top_p": 0}, {"max_tokens": -1}):
|
||||
try:
|
||||
st.create(slug="p-val", display_name="x", params=badv)
|
||||
raise AssertionError(f"{badv} 本应被拒")
|
||||
except AgentsError:
|
||||
pass
|
||||
|
||||
# 数量上限(未归档才计入)
|
||||
for i in range(4):
|
||||
st.create(slug=f"bot-{i}", display_name=f"机器人{i}")
|
||||
assert st.active_count() == 5
|
||||
try:
|
||||
st.create(slug="one-more", display_name="超限")
|
||||
raise AssertionError("超限本应被拒")
|
||||
except AgentsError as e:
|
||||
assert "上限" in str(e)
|
||||
|
||||
# 归档不计入 → 可再建;恢复时若满则拒
|
||||
st.retire("bot-0")
|
||||
assert st.active_count() == 4
|
||||
st.create(slug="fits-now", display_name="补位")
|
||||
assert st.active_count() == 5
|
||||
try:
|
||||
st.restore("bot-0")
|
||||
raise AssertionError("满员时恢复本应被拒")
|
||||
except AgentsError:
|
||||
pass
|
||||
|
||||
# 改:只允许白名单字段
|
||||
try:
|
||||
st.update("code-review", {"engine": {"type": "x"}})
|
||||
raise AssertionError("改 engine 本应被拒")
|
||||
except AgentsError as e:
|
||||
assert "不可改" in str(e)
|
||||
a2 = st.update("code-review", {"description": "改过了", "params": {"temperature": 0.5}})
|
||||
assert a2.description == "改过了" and a2.params["temperature"] == 0.5
|
||||
|
||||
# 落盘回读(mtime 热载)
|
||||
st2 = AgentStore(st.path)
|
||||
assert st2.get("code-review").description == "改过了"
|
||||
|
||||
|
||||
def test_agents_history_kind_supports_agent_prefix():
|
||||
"""会话复用 history 存储:kind = agent_<slug> 必须被放行,非法仍被拒。"""
|
||||
from app.history import HistoryStore, HistoryError, _lim
|
||||
import tempfile, pathlib as _pl
|
||||
h = HistoryStore(str(_pl.Path(tempfile.mkdtemp()) / "h.json"))
|
||||
|
||||
assert h._check_kind("agent_code-review") == "agent_code-review"
|
||||
# 容量沿用 chat(一条就是一整段多轮)
|
||||
assert _lim("agent_code-review") == _lim("chat")
|
||||
for bad in ("agent_BAD", "agent_", "agent", "bogus"):
|
||||
try:
|
||||
h._check_kind(bad)
|
||||
raise AssertionError(f"{bad} 本应被拒")
|
||||
except HistoryError:
|
||||
pass
|
||||
|
||||
|
||||
def test_qwenpaw_engine_sse_translation():
|
||||
"""SSE 翻译器:用**真实录制的夹具**验证事件映射(第三十八轮核心逻辑)。
|
||||
|
||||
夹具 tests/fixtures/sse-tool-flow.sse 是 2026-09-29 从生产 QwenPaw 实录的,
|
||||
含一次真实的 read_file 工具调用。
|
||||
"""
|
||||
from app.adapters.qwenpaw_engine import parse_sse_line, StreamTranslator
|
||||
import pathlib as _pl
|
||||
|
||||
fixture = _pl.Path(__file__).parent / "fixtures" / "sse-tool-flow.sse"
|
||||
assert fixture.exists(), "缺少 SSE 夹具"
|
||||
|
||||
tr = StreamTranslator()
|
||||
out = []
|
||||
for line in fixture.read_text(encoding="utf-8").splitlines():
|
||||
ev = parse_sse_line(line)
|
||||
if ev is None:
|
||||
continue
|
||||
out.extend(tr.feed(ev))
|
||||
out.extend(tr.finish())
|
||||
|
||||
kinds = {m["type"] for m in out}
|
||||
assert "text" in kinds and "step" in kinds and "usage" in kinds and "done" in kinds
|
||||
|
||||
# 正文增量累积(delta:false 的快照必须被忽略,否则会重复)
|
||||
assert tr.text.strip(), "正文不该为空"
|
||||
assert len(tr.text) < 3000, "正文长度异常(可能把快照重复计入了)"
|
||||
|
||||
# 工具步骤:running → done 配对,且带上了参数/输出
|
||||
steps = [m for m in out if m["type"] == "step"]
|
||||
runs = [m for m in steps if m["status"] == "running"]
|
||||
dones = [m for m in steps if m["status"] == "done"]
|
||||
assert len(runs) == 1 and len(dones) == 1, f"步骤数不对:{runs} / {dones}"
|
||||
assert runs[0]["name"] == "read_file"
|
||||
assert runs[0]["args"] == {"file_path": "/tmp/probe-agent.txt"}, runs[0]["args"]
|
||||
assert dones[0]["id"] == runs[0]["id"], "done 必须用 call_id 配上 running"
|
||||
|
||||
# 用量:带 model 与缓存字段
|
||||
usage = [m for m in out if m["type"] == "usage"]
|
||||
assert len(usage) == 1
|
||||
assert usage[0]["model"] == "deepseek-v4.1-flash"
|
||||
assert usage[0]["prompt"] > 0
|
||||
|
||||
# 终态
|
||||
assert tr.final_status == "completed"
|
||||
assert out[-1] == {"type": "done", "status": "completed"}
|
||||
|
||||
|
||||
def test_qwenpaw_engine_build_body_injects_system_suffix():
|
||||
"""`system_suffix` 走 system 消息(实测被 QwenPaw 接受且生效)。"""
|
||||
from app.adapters.qwenpaw_engine import build_body
|
||||
|
||||
b = build_body([{"role": "user", "content": "你好"}], "s1", "回答只给结论")
|
||||
inp = b["input"]
|
||||
assert inp[0]["role"] == "system"
|
||||
assert inp[0]["content"][0]["text"] == "回答只给结论"
|
||||
assert inp[1]["role"] == "user"
|
||||
assert b["session_id"] == "s1" and b["stream"] is True
|
||||
# 不传 suffix 时不插 system
|
||||
b2 = build_body([{"role": "user", "content": "hi"}], "s2")
|
||||
assert [m["role"] for m in b2["input"]] == ["user"]
|
||||
# 内容形状:字符串与 [{type:text}] 都归一成 content 数组
|
||||
b3 = build_body([{"role": "user", "content": [{"type": "text", "text": "x"}]}], "s3")
|
||||
assert b3["input"][0]["content"] == [{"type": "text", "text": "x"}]
|
||||
# 图片块一期不支持 → 静默跳过(计划「一期不做图片/附件」)
|
||||
b4 = build_body([{"role": "user", "content": [{"type": "image_url", "image_url": {}}]}], "s4")
|
||||
assert b4["input"][0]["content"] == []
|
||||
|
||||
|
||||
def test_agent_runs_records_and_clips():
|
||||
"""运行记录:写入 / 回读 / 最新在前 / 容量裁剪。"""
|
||||
import tempfile, pathlib as _pl
|
||||
from app.agents_routes import AgentRuns
|
||||
|
||||
r = AgentRuns(_pl.Path(tempfile.mkdtemp()) / "runs.json")
|
||||
assert r.load() == {}
|
||||
for i in range(3):
|
||||
r.record("code-review", account="admin", rounds=i + 1, tools=i,
|
||||
tokens=100 * (i + 1), ms=1000 + i, status="ok")
|
||||
b = r.load()["code-review"]
|
||||
assert b["total"] == 3
|
||||
assert b["last_used_at"], "应记录最后使用时间"
|
||||
items = b["items"]
|
||||
assert len(items) == 3
|
||||
assert items[0]["rounds"] == 3, "最新的应在最前"
|
||||
assert items[0]["tokens"] == 300
|
||||
|
||||
# 容量裁剪
|
||||
for i in range(AgentRuns.MAX_ITEMS + 20):
|
||||
r.record("code-review", account="admin", rounds=1, tools=0, tokens=1, ms=1, status="ok")
|
||||
assert len(r.load()["code-review"]["items"]) == AgentRuns.MAX_ITEMS
|
||||
|
||||
|
||||
def test_agent_can_use_permission_matrix():
|
||||
"""权限判定唯一入口 can_use_agent:admin 全通;member 只认显式分派。"""
|
||||
from app.agents import Agent
|
||||
from app.agents_routes import can_use_agent
|
||||
|
||||
only_admin = Agent(slug="a1", display_name="x", access_roles=["admin"])
|
||||
open_to_member = Agent(slug="a2", display_name="y", access_roles=["member"])
|
||||
named = Agent(slug="a3", display_name="z", access_roles=["member"], access_users=["alice"])
|
||||
|
||||
assert can_use_agent(only_admin, "admin")
|
||||
assert not can_use_agent(only_admin, "member") # 一期:仅管理员的 member 看不到
|
||||
assert can_use_agent(open_to_member, "member") # 二期放开的角色
|
||||
assert not can_use_agent(open_to_member, "admin") is False or True # admin 恒可用
|
||||
assert can_use_agent(named, "member")
|
||||
assert can_use_agent(only_admin, "admin")
|
||||
|
||||
|
||||
def test_agent_self_reference_guard():
|
||||
"""防自指:引擎模型指向 agent/* 时必须被拦(避免引擎→智能体→引擎死循环)。"""
|
||||
from app.adapters.qwenpaw_engine import ENGINE_DOWN_MSG
|
||||
|
||||
# 校验逻辑在路由里,这里验证判定式本身(与路由同口径)
|
||||
def blocked(model: str) -> bool:
|
||||
return model == "agent" or model.startswith("agent/")
|
||||
|
||||
assert blocked("agent/code-review")
|
||||
assert blocked("agent")
|
||||
assert not blocked("wjd")
|
||||
assert not blocked("deepseek-v4.1-flash")
|
||||
assert not blocked("")
|
||||
# 引擎不可达时要有明确文案(不是空串)
|
||||
assert "不可达" in ENGINE_DOWN_MSG
|
||||
|
||||
|
||||
def test_agents_routes_installed():
|
||||
"""端点已挂上 app(含 /auth/me 灰度通道)。"""
|
||||
import app.main as m
|
||||
paths = {r.path for r in m.app.routes if hasattr(r, "path")}
|
||||
for need in ("/agents", "/agents/{slug}", "/agents/{slug}/chat",
|
||||
"/agents/{slug}/runs", "/agents/{slug}/test", "/auth/me"):
|
||||
assert need in paths, f"缺少端点 {need}"
|
||||
|
||||
|
||||
# 直接运行(非 pytest)—— 必须留在文件末尾,见上方 `test_image_request_goes_to_requested_model`
|
||||
# 之前的旁注:运行器只会收集执行到这一行时已经存在的 globals。
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user