feat(agents): 图片附件 + 修两个真 bug(版本 2.5.0 → 2.5.1)

老板要求「图片附件功能加上,其他的先不做」。

QwenPaw 侧格式(实测确认后再动手):{"type":"image","image_url":"<data-URI>"
—— 塞纯红 64×64 PNG 的 data URI,模型正确答出「红色」。

改动:
- app/adapters/qwenpaw_engine.py:放行图片内容块,并同时支持两种写法 ——
  AgentScope 原生 image 与 OpenAI 风格 image_url(前端发的是后者);
  新增 _image_url();缺 url 的块跳过;audio/video/file 仍忽略
- views/Agents.vue:对话输入区加附件按钮(pickImage 复用现有浏览器内压缩,最多 4 张)
  + 待发缩略图可删 + 消息气泡内渲染图片;允许「只发图不带字」
- lib/agents.ts:content 放宽为 string | any[]
- i18n 新增 4 键,中英对称 879/879

修掉两个真 bug(本轮才暴露):
1. image_url 写法被忽略 —— 分支只认 type=="image",而前端发 image_url
   → 图片会静默丢失(功能等于没做)。测试用例抓出来的。
2. /agents/{slug}/chat 500 —— 上轮加计量打标时 log.info(..., rounds) 写在
   rounds = ... 之前,UnboundLocalError → 端点 500。已加回归用例
   test_agent_chat_endpoint_source_order 固化(这类声明顺序错误跑不出来)。

上线实测:两张图(红+蓝)→ 模型答「红色,蓝色」顺序正确,system_suffix 同时生效,
usage wjd/24112 tokens/缓存 2048;浏览器路径也验证过(上传→缩略图→发送→渲染→无 JS 错误)。

已知引擎侧行为(非网关问题):QwenPaw SSE 只有 text 事件、无独立 reasoning 通道,
模型有时把思考写进正文;且上下文会污染视觉判断(同图脏上下文答「白色」、干净会话答「蓝色」)。

测试 218 → 219 全过。
This commit is contained in:
api-gateway committed 2026-09-29 14:00:49 +08:00
1 parent c0706d1846
commit c62be67aa8
9 files changed
+230 -16

No files matched your search

+34 -4
View File
@@ -11,6 +11,7 @@
- 回 SSE(前缀固定 `data: `,**没有 event: 行**)
- 事件:`text` / `plugin_call` / `plugin_call_output` / `data` / `message` / `response` / `turn_usage`
- `role: "system"` 的输入消息**被接受且生效**(实测)→ `system_suffix` 走这条
- **图片输入**:`{"type":"image", "image_url":"<data-URI/URL>"}`(实测可识别)
- `model` 字段**被忽略**(实测)→ 模型是 profile 级,不在请求里指定
网关对前端的输出形状(自定义、简单):
@@ -67,7 +68,9 @@ def build_input(messages: list[dict[str, Any]], system_suffix: str = "") -> list
if role not in ("user", "assistant", "system"):
role = "user"
content = m.get("content")
# content 允许是字符串或 [{type:text,text}] 形状
# content 允许:字符串 / [{type:text,text}] / [{type:image,image_url}]
# 图片格式(2026-09-29 实测确认):{"type":"image","image_url":"<data-URI 或 http URL>"}
# —— 端到端验证过:塞一张纯红 64×64 PNG 的 data URI,模型正确答出"红色"。
if isinstance(content, str):
parts = [{"type": "text", "text": content}]
elif isinstance(content, list):
@@ -75,15 +78,42 @@ def build_input(messages: list[dict[str, Any]], system_suffix: str = "") -> list
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 "")})
# 图片等一期不支持,静默跳过(计划「一期不做图片/附件」)
elif isinstance(c, dict):
ctype = c.get("type")
if ctype == "text":
parts.append({"type": "text", "text": str(c.get("text") or "")})
elif ctype in ("image", "image_url"):
# 两种写法都认(实测见 test_qwenpaw_engine_image_content_passthrough):
# AgentScope 原生 `{"type":"image","image_url":"<uri>"}`
# OpenAI 风格 `{"type":"image_url","image_url":{"url":"<uri>"}}`
# —— 前端「对话」页发的是后者,所以必须同时支持。
url = _image_url(c)
if url:
parts.append({"type": "image", "image_url": url})
else:
log.debug("图片块缺 image_url,已跳过")
# 其余类型(audio/video/file)暂不支持,忽略
else:
parts = [{"type": "text", "text": str(content or "")}]
out.append({"role": role, "content": parts})
return out
def _image_url(block: dict[str, Any]) -> str:
"""从图片内容块里取出 URL。兼容两种写法:
- AgentScope 原生:`{"type":"image", "image_url":"data:image/png;base64,..."}`
- OpenAI 风格: `{"type":"image_url", "image_url":{"url":"..."}}`
"""
v = block.get("image_url")
if isinstance(v, str):
return v.strip()
if isinstance(v, dict):
u = v.get("url")
return str(u).strip() if u else ""
return ""
def build_body(messages: list[dict[str, Any]], session_id: str,
system_suffix: str = "") -> dict[str, Any]:
return {