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 51c9af59c4
commit 96301897c4
9 files changed
+230 -16

No files matched your search

+1 -1
View File
@@ -100,7 +100,7 @@ export const stopAgent = (slug: string, chatId: string) =>
*/
export function streamAgentChat(
slug: string,
messages: { role: string; content: string }[],
messages: { role: string; content: string | any[] }[],
sessionId: string,
onEvent: (ev: AgentEvent) => void,
): () => void {
+4
View File
@@ -469,6 +469,10 @@ export default {
runOk: 'OK',
runStopped: 'Stopped',
runTimeout: 'Timeout',
attach: 'Attach image',
imgMax: 'Up to {n} images',
imgErr: 'Failed to pick image: {msg}',
imgCount: '{n}/{max}',
runDefaultTitle: 'Chat',
cacheHit: 'cache {n}',
meLabel: 'Me',
+4
View File
@@ -473,6 +473,10 @@ export default {
runOk: '成功',
runStopped: '已停止',
runTimeout: '超时',
attach: '添加图片',
imgMax: '最多 {n} 张',
imgErr: '选图失败:{msg}',
imgCount: '{n}/{max} 张',
runDefaultTitle: '对话',
cacheHit: '缓存 {n}',
meLabel: '我',
+64 -5
View File
@@ -171,6 +171,10 @@
</template>
</div>
<div v-if="m.images && m.images.length" class="msg-imgs">
<img v-for="(u, i) in m.images" :key="i" :src="u" alt="" />
</div>
<div v-if="m.text" v-html="render(m.text)" />
<div v-else-if="streaming && m.role === 'assistant'" class="typing">…</div>
@@ -189,10 +193,22 @@
:placeholder="t('agents.inputPlaceholder')"
@keydown.enter.exact.prevent="send"
/>
<div v-if="pendingImgs.length" class="pending">
<div v-for="(im, i) in pendingImgs" :key="i" class="pend-i">
<img :src="im.thumb || im.uri" alt="" />
<span class="pend-x" @click="removeImg(i)">×</span>
</div>
<span class="pend-hint">{{ t('agents.imgCount', { n: pendingImgs.length, max: MAX_IMGS }) }}</span>
</div>
<div class="bar">
<input ref="fileInput" type="file" accept="image/*" multiple hidden @change="onFiles" />
<button class="clip-btn" :disabled="streaming" :title="t('agents.attach')" @click="fileInput?.click()">
<Icon name="clip" :size="15" />
</button>
<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">
<el-button v-if="!streaming" type="primary" size="small"
:disabled="!input.trim() && !pendingImgs.length" @click="send">
<Icon name="send" :size="13" />{{ t('chat.send') }}
</el-button>
<el-button v-else size="small" class="stop-btn" @click="stopLocal">
@@ -351,6 +367,7 @@
<script setup lang="ts">
import { computed, nextTick, onMounted, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { ElMessage } from 'element-plus'
import Icon from '../components/Icon.vue'
import {
type AgentEvent, type AgentItem, type AgentRun,
@@ -358,10 +375,11 @@ import {
restoreAgent, retireAgent, streamAgentChat, testAgent, updateAgent,
} from '../lib/agents'
import { renderMarkdown } from '../lib/markdown'
import { PRESETS, pickImage } from '../lib/upload'
const { t } = useI18n()
interface Msg { id: number; role: 'user' | 'assistant'; text: string; steps: Step[]; open?: boolean; meta?: string }
interface Msg { id: number; role: 'user' | 'assistant'; text: string; steps: Step[]; open?: boolean; meta?: string; images?: string[] }
interface Step { id: string; name?: string; args?: any; status: 'running' | 'done'; ms?: number; output?: any }
const err = ref('')
@@ -378,6 +396,9 @@ const messages = ref<Msg[]>([])
const sessions = ref<any[]>([])
const sessionId = ref('')
const input = ref('')
const pendingImgs = ref<{ uri: string; name: string; thumb: string }[]>([])
const MAX_IMGS = 4
const fileInput = ref<HTMLInputElement | null>(null)
const streaming = ref(false)
const stepsDone = ref(0)
const lastUsage = ref<any>(null)
@@ -503,9 +524,12 @@ async function persistSession() {
function send() {
const text = input.value.trim()
if (!text || streaming.value || !current.value) return
// 允许"只发图不带字"
if ((!text && !pendingImgs.value.length) || streaming.value || !current.value) return
err.value = ''
messages.value.push({ id: Date.now(), role: 'user', text, steps: [] })
const imgs = pendingImgs.value.map((x) => x.uri)
pendingImgs.value = []
messages.value.push({ id: Date.now(), role: 'user', text, steps: [], images: imgs })
const reply: Msg = { id: Date.now() + 1, role: 'assistant', text: '', steps: [], open: true, meta: '' }
messages.value.push(reply)
input.value = ''
@@ -518,7 +542,14 @@ function send() {
const history = messages.value
.filter((m) => m !== reply)
.map((m) => ({ role: m.role, content: m.text }))
.map((m) => {
const im = (m.images || []).filter(Boolean)
if (!im.length) return { role: m.role, content: m.text }
const parts: any[] = []
if (m.text) parts.push({ type: 'text', text: m.text })
for (const url of im) parts.push({ type: 'image', image_url: url })
return { role: m.role, content: parts }
})
abort = streamAgentChat(current.value, history, sessionId.value || `agent-${current.value}-${Date.now()}`, (ev: AgentEvent) => {
if (ev.type === 'start') { sessionId.value = ev.session_id }
@@ -546,6 +577,24 @@ function send() {
})
}
async function onFiles(ev: Event) {
const el = ev.target as HTMLInputElement
for (const f of Array.from(el.files || [])) {
if (pendingImgs.value.length >= MAX_IMGS) {
ElMessage.warning(t('agents.imgMax', { n: MAX_IMGS }))
break
}
try {
const m = await pickImage(f, PRESETS.chat)
pendingImgs.value.push({ uri: m.uri, name: m.name, thumb: m.thumb })
} catch (e: any) {
ElMessage.error(t('agents.imgErr', { msg: (e && e.message) || e }))
}
}
if (fileInput.value) fileInput.value.value = ''
}
function removeImg(i: number) { pendingImgs.value.splice(i, 1) }
function finish() {
streaming.value = false
elapsedText.value = `${((Date.now() - startedAt.value) / 1000).toFixed(1)}s`
@@ -690,6 +739,16 @@ onMounted(load)
.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); }
.msg-imgs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.msg-imgs img { max-width: 132px; max-height: 132px; border-radius: var(--radius-s); border: 1px solid var(--line); display: block; }
.pending { display: flex; align-items: center; gap: 8px; margin-top: var(--gap-sm); }
.pend-i { position: relative; }
.pend-i img { width: 46px; height: 46px; object-fit: cover; border-radius: var(--radius-sm, 8px); border: 1px solid var(--line); display: block; }
.pend-x { position: absolute; top: -6px; right: -6px; width: 17px; height: 17px; border-radius: 50%; background: var(--err); color: #fff; font-size: 12px; line-height: 17px; text-align: center; cursor: pointer; }
.pend-hint { font-size: 11px; color: var(--muted-2); }
.clip-btn { background: transparent; border: 0; color: var(--muted-2); cursor: pointer; padding: 4px; display: grid; place-items: center; }
.clip-btn:disabled { opacity: .4; cursor: not-allowed; }
.clip-btn:hover:not(:disabled) { color: var(--primary); }
.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); }