From 47fdb91dbe423efa20c5203adf4535074eaaf793 Mon Sep 17 00:00:00 2001 From: lik Date: Sun, 1 Nov 2026 12:31:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(agent):=20=E5=9B=BE=E7=89=87=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E5=8E=9F=E6=96=87=E7=9B=B4=E6=8E=A5=E5=9B=9E=E6=98=BE?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E9=95=BF=E6=8A=A5=E5=91=8A=E8=A2=AB?= =?UTF-8?q?=E6=8A=A4=E6=A0=8F=E6=88=AA=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 WS format 帧:识别原文只并入会话上下文并分片原样回显,不再经过模型二次改写 - 原方案走 chat 会被咨询提示词压缩成 1-3 句,并被 OutputGuard 的 400 字上限截断 - 补充分片回显测试与 README 协议说明 --- README.md | 5 +++ agent/bots/bot-escort/agent.js | 24 ++++++++++++++ test/agent_format.test.js | 60 ++++++++++++++++++++++++++++++++++ websocket.js | 3 +- 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/agent_format.test.js diff --git a/README.md b/README.md index d876bff..14dd442 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,11 @@ curl -s http://localhost:9004/health/ai/ocr \ 因此服务端会把连续的分列块转成 markdown 表格(列数不足补空列,超出并进最后一列), 其余行独立成段保留换行。小程序与 Taro 端都按 markdown 渲染即可。 +小程序把识别文本以 WS `format` 帧投递(`{ type: "format", content, id, token, ts }`): +服务端只把原文并入会话上下文,并按分片原样回显给客户端展示,**不再让模型二次改写**。 +识别结果通常有几百到上千字,模型改写会按咨询提示词压缩成 1-3 句,还会被输出护栏的 +400 字上限截断,长报告必然丢行;原文投递既完整,也省一次生成。 + ### 错误码 业务码统一走响应体 `code`(HTTP 恒 200),同时写入 `X-Biz-Code` 响应头: diff --git a/agent/bots/bot-escort/agent.js b/agent/bots/bot-escort/agent.js index dac7c38..7464017 100644 --- a/agent/bots/bot-escort/agent.js +++ b/agent/bots/bot-escort/agent.js @@ -22,6 +22,10 @@ import { createEmployeeQueryTool, createEmployeeReviewQueryTool } from "../../infra/tools/index.js"; +// 图片识别原文回显的分片长度:与模型流式输出的单帧量级接近, +// 避免把很长的报告塞进一帧,也让客户端能逐段追加展示 +const FORMAT_ECHO_CHUNK = 800; + export default class EscortAgent { constructor(options = {}) { this.messages = []; @@ -59,6 +63,26 @@ export default class EscortAgent { return; } + // 图片识别原文:服务端 OCR 已按原图排版成 markdown,这里只并入会话上下文并原样回显。 + // 不再交给模型「二次整理」:模型会按咨询提示词压缩成 1-3 句,而且输出护栏有字符上限, + // 长报告必然丢行(护栏 400 字);原文投递既完整,也省一次生成 + const formatMsg = msgs.find(msg => msg.type === "format"); + if (formatMsg) { + const text = String(formatMsg.content || "").trim(); + if (!text) return; + + // 原文留在历史里,用户接着问「这项指标什么意思」时模型能看到报告内容 + this.messages.push(new HumanMessage( + `${formatMsg.ts} - 用户上传了一张图片,图像识别结果如下(原文可能有误,数字与单位需与图片核对):\n${text}` + )); + this._trimHistory(); + + for (let offset = 0; offset < text.length; offset += FORMAT_ECHO_CHUNK) { + callback("main", "ai", text.slice(offset, offset + FORMAT_ECHO_CHUNK), formatMsg.id); + } + return; + } + msgs.forEach(msg => { this.messages.push(new HumanMessage(`${msg.ts} - ${msg.content}`)); }); diff --git a/test/agent_format.test.js b/test/agent_format.test.js new file mode 100644 index 0000000..de9d9d1 --- /dev/null +++ b/test/agent_format.test.js @@ -0,0 +1,60 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; + +import EscortAgent from "../agent/bots/bot-escort/agent.js"; + +// 一份超过护栏 400 字上限的识别原文:用来确认回显分支不再截断长报告 +const IMAGE_TEXT = [ + "【图像识别】检验报告|血常规", + "", + "【原文转写】", + "", + "白细胞计数 WBC 6.2 10^9/L 3.5-9.5".repeat(30), + "", + "【说明】本内容为图像文字识别结果,不构成诊断意见。", +].join("\n"); + +test("图片识别原文完整回显,不经过模型与输出护栏", async () => { + const agent = new EscortAgent(); + const chunks = []; + await agent.streamChat( + { _id: "test-user", profile: {}, location: {} }, + [{ + type: "format", + ts: "2026-11-01 10:00:00", + content: IMAGE_TEXT, + id: "req_format", + }], + (source, type, content, id) => { + chunks.push({ source, type, content, id }); + } + ); + + // 分片回显,拼接后必须与原文完全一致:长报告不能被字符上限截断 + assert.ok(chunks.length > 1); + assert.equal(chunks.map(item => item.content).join(""), IMAGE_TEXT); + assert.deepEqual(new Set(chunks.map(item => item.id)), new Set(["req_format"])); + assert.deepEqual(new Set(chunks.map(item => item.source)), new Set(["main"])); + assert.deepEqual(new Set(chunks.map(item => item.type)), new Set(["ai"])); + + // 原文进历史:用户接着问「这项指标什么意思」时模型能看到报告内容 + assert.equal(agent.messages.length, 1); + assert.match(agent.messages[0].text, /白细胞计数 WBC 6\.2/); + + // 走回显分支时不构建 agent,也就不会产生模型调用 + assert.equal(agent.agent, undefined); +}); + +test("空白识别原文不投递", async () => { + const agent = new EscortAgent(); + const chunks = []; + await agent.streamChat( + { _id: "test-user", profile: {}, location: {} }, + [{ type: "format", ts: "2026-11-01 10:00:00", content: " ", id: "req_empty" }], + (source, type, content, id) => { + chunks.push({ source, type, content, id }); + } + ); + assert.equal(chunks.length, 0); + assert.equal(agent.messages.length, 0); +}); diff --git a/websocket.js b/websocket.js index 4bb0e08..43b7127 100644 --- a/websocket.js +++ b/websocket.js @@ -171,7 +171,8 @@ export default class WebSocketServerManager { return; } - if (msg.type === 'chat' || msg.type === 'clear') { + // format 是图片识别原文的投递帧:只并入会话上下文并回显原文,不触发模型生成 + if (msg.type === 'chat' || msg.type === 'clear' || msg.type === 'format') { // 客户端断开后不再处理消息 if (ws.readyState !== WebSocket.OPEN) return;