From a06d5ddf193fc6d6367963b78bb1d3219b5da94d Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 28 Sep 2026 21:07:47 +0800 Subject: [PATCH] =?UTF-8?q?wxbot:=20stripMarkdown=20=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E9=BB=98=E8=AE=A4=E4=B8=8D=E5=89=A5=E2=80=94?= =?UTF-8?q?=E2=80=94=E5=BE=AE=E4=BF=A1=E5=B7=B2=E6=94=AF=E6=8C=81=20markdo?= =?UTF-8?q?wn=20=E6=B8=B2=E6=9F=93=EF=BC=8C=E5=87=BA=E5=8F=A3=E9=80=8F?= =?UTF-8?q?=E4=BC=A0=E4=BF=9D=E6=A0=BC=E5=BC=8F=EF=BC=9BDEFAULTS.bot.strip?= =?UTF-8?q?=5Fmarkdown=3Dfalse=20+=20env=20WXBOT=5FSTRIP=5FMARKDOWN=3D1/tr?= =?UTF-8?q?ue=20=E5=8F=AF=E6=81=A2=E5=A4=8D=E6=97=A7=E7=BA=AF=E6=96=87?= =?UTF-8?q?=E5=8C=96=E9=99=8D=E7=BA=A7=EF=BC=880/off=20=E6=98=BE=E5=BC=8F?= =?UTF-8?q?=E5=85=B3=E9=97=AD=EF=BC=8C=E5=90=8C=20WXBOT=5FLOG=5FLEVEL=20?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E6=A8=A1=E5=BC=8F=EF=BC=89=EF=BC=8C=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=9B=9E=E6=89=A7=E4=B8=8E=20/api/deliver=20=E4=B8=A4?= =?UTF-8?q?=E5=A4=84=E5=8F=91=E9=80=81=E7=BB=9F=E4=B8=80=E8=B5=B0=20outTex?= =?UTF-8?q?t()=E3=80=82channel.js=20=E5=87=BA=E5=8F=A3=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=EF=BC=88=E6=B8=B2=E6=9F=93=E9=80=82=E9=85=8D?= =?UTF-8?q?=E5=9C=A8=E6=B6=88=E8=B4=B9=E6=96=B9=E4=B8=8D=E5=8F=98=EF=BC=8C?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=BD=A2=E6=80=81=E6=94=B9=E4=B8=BA=E9=80=8F?= =?UTF-8?q?=E4=BC=A0=EF=BC=89=EF=BC=9Bwxbot/README=20env=20=E8=A1=A8?= =?UTF-8?q?=E4=B8=8E=20/api/deliver=20=E8=A1=8C=E6=9B=B4=E6=96=B0=E3=80=82?= =?UTF-8?q?compose=20=E7=9A=84=20WXBOT=5FSTRIP=5FMARKDOWN=20=E6=B3=A8?= =?UTF-8?q?=E7=BA=BF=E4=B8=8E=20ARCHITECTURE=20=E5=90=8C=E6=AD=A5=E6=9A=82?= =?UTF-8?q?=E7=95=99=E5=B7=A5=E4=BD=9C=E6=A0=91=EF=BC=88=E4=B8=8E=E6=9C=AA?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=9A=84=20wechat=20digest=20=E6=94=B9?= =?UTF-8?q?=E5=8A=A8=E3=80=81core=20VIRTUAL=5FHOST=20WIP=20=E6=B7=B7?= =?UTF-8?q?=E5=9C=A8=E5=90=8C=E4=B8=80=E6=96=87=E4=BB=B6=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=A4=B9=E5=B8=A6=EF=BC=89=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/routes/channel.js | 4 ++-- wxbot/README.md | 3 ++- wxbot/app/src/server.js | 17 ++++++++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/server/routes/channel.js b/app/server/routes/channel.js index d2c00e4..114cf21 100644 --- a/app/server/routes/channel.js +++ b/app/server/routes/channel.js @@ -83,7 +83,7 @@ function parseCommand(text) { } // 命令输出统一用 markdown(channel 层的规范格式):支持渲染的接入端直接用, -// 微信这类纯文本端由 wxbot 发送前 stripMarkdown 降级——格式信息留在生产方,渲染适配在消费方。 +// 微信端渲染适配在 wxbot 出口:默认透传 markdown(微信已支持渲染),env WXBOT_STRIP_MARKDOWN=1 才 stripMarkdown 纯文化——格式信息留在生产方,渲染适配在消费方。 const HELP_TEXT = [ '**可用命令**', '- `/new [msg]` 新建会话并切换', @@ -109,7 +109,7 @@ function statusReply(db, s) { const looping = !!(run && run.looping); const lines = [ '**会话状态**', - '- 会话:`' + (s.title || s.id), + '- 会话:`' + (s.title || s.id) + '`', '- 状态:' + (looping ? '回合进行中' : (STATUS_TEXT[s.status] || s.status)), '- 模型:`' + (s.model_id || '未绑定') + '`', ]; diff --git a/wxbot/README.md b/wxbot/README.md index b65488b..4ea068f 100644 --- a/wxbot/README.md +++ b/wxbot/README.md @@ -40,6 +40,7 @@ agent-core 的 `wechat_bindings` 表;扫码二维码呈现在 agent-core 的 W | `WXBOT_TOKEN` | 根 `.env`(`scripts/init.sh` 生成/复用) | 本服务 `/api/*` 鉴权白名单(自动并入)+ 回调 core `/wxhook/*` 的凭据,与 core 侧同一值双向互信 | | `CORE_URL` | compose 静态注 `http://agent-core:8090` | 转发目标 | | `WXBOT_LOG_LEVEL` | compose 静态注 `info` | 日志等级(debug 原样打印收信消息结构;none/off→silent) | +| `WXBOT_STRIP_MARKDOWN` | 根 `.env` 可选(默认空) | 出口 markdown 纯文化开关:空/0 = 原样透传(微信已支持渲染);1 = 恢复 stripMarkdown 降级 | 端口、非文本消息占位文案等次要旋钮走 `src/server.js` 内置 DEFAULTS;日志等级由 env `WXBOT_LOG_LEVEL` 决定(compose 注入,同 executor 的 `EXECUTOR_LOG_LEVEL` 约定;`debug` 原样打印收信消息结构, @@ -68,7 +69,7 @@ docker compose up -d agent-core # core 带 WXBOT_URL/WXBOT_TOKEN 重建( | POST | `/api/instances/:id/refresh` | 重发登录流程(二维码过期/失败后;保留凭证) | | DELETE | `/api/instances/:id` | 销毁实例并删登录凭证(解绑) | | GET | `/api/instances/:id/qr.svg` | 当前登录二维码(core 设置页代理渲染) | -| POST | `/api/deliver {id,to,text}` | 文本投递(core 已分段;出口统一 stripMarkdown,微信不渲染 markdown) | +| POST | `/api/deliver {id,to,text}` | 文本投递(core 已分段;markdown 默认透传,微信已支持渲染,`WXBOT_STRIP_MARKDOWN=1` 时出口纯文化) | | POST | `/api/typing {id,to}` | 「正在输入」心跳(core 在工具调用/结果时触发,每绑定 2s 冷却;typing 自然过期无需 stopTyping) | | POST | `/api/deliver_media {id,to,filename,b64}` | 附件投递(core 已把正文里的 /p/ 链接换成本地字节并做归属/大小校验;SDK 按扩展名自动路由 image/video/file 卡片) | diff --git a/wxbot/app/src/server.js b/wxbot/app/src/server.js index 09cb2b1..67ff64b 100644 --- a/wxbot/app/src/server.js +++ b/wxbot/app/src/server.js @@ -29,6 +29,7 @@ const DEFAULTS = { bot: { storage_dir: "/data/bots", // 每绑定一个子目录(目录名 = binding id) log_level: "info", // debug | info | warn | error | silent;运行期被 env WXBOT_LOG_LEVEL 覆盖(compose 注入,同 EXECUTOR_LOG_LEVEL 模式) + strip_markdown: false, // 出口 markdown 纯文化开关:微信已支持渲染 markdown,默认 false 原样透传;env WXBOT_STRIP_MARKDOWN=1 可恢复剥除 empty_hint: "(这条消息类型暂时接不住——支持文字/图片/视频/文件;指令可发 /help)", // 未接入类型(voice/贴纸等)的本地兑底回执 }, }; @@ -111,6 +112,16 @@ fs.mkdirSync(CONFIG.bot.storage_dir, { recursive: true }); CONFIG.bot.log_level = map[lv] || String(CONFIG.bot.log_level || "info").toLowerCase(); } +// markdown 出口处理开关:微信已支持渲染 markdown → 默认透传;若某些客户端/版本翻车, +// env WXBOT_STRIP_MARKDOWN=1/true(compose 注入)或配置文件 bot.strip_markdown: true 恢复旧行为(出口纯文化)。 +{ + const ev = String(process.env.WXBOT_STRIP_MARKDOWN || "").trim().toLowerCase(); + if (ev) CONFIG.bot.strip_markdown = /^(1|true|on|yes)$/.test(ev); + else CONFIG.bot.strip_markdown = CONFIG.bot.strip_markdown === true || CONFIG.bot.strip_markdown === "true"; +} +// 出口文本:开关开启才 stripMarkdown,否则原样;命令回执与 /api/deliver 两处发送共用。 +const outText = (s) => (CONFIG.bot.strip_markdown ? stripMarkdown(String(s)) : String(s)); + // ── 鉴权 ───────────────────────────────────────────────────────────────────── function requireAuth(req, res, next) { if (!CONFIG.auth.enabled) return next(); @@ -282,8 +293,8 @@ async function onIncoming(inst, msg) { } try { const r = await corePost("/wxhook/inbox", { id: inst.id, from: userId, text: text || undefined, media: media || undefined }, 30000); - // 命令回执可能是 markdown(channel 层规范格式);微信不渲染 markdown,出口统一 stripMarkdown 降级。 - if (r && r.kind === "command" && r.reply) await inst.bot.send(userId, stripMarkdown(r.reply)); + // 命令回执可能是 markdown(channel 层规范格式);默认透传(微信已支持渲染),开关控制是否纯文化。 + if (r && r.kind === "command" && r.reply) await inst.bot.send(userId, outText(r.reply)); } catch (e) { if (e.status === 404) { // core 侧绑定已删(解绑通知丢失的孤儿实例):告知用户并自毁(连凭证一起清,避免复活再骚扰) @@ -357,7 +368,7 @@ app.post("/api/deliver", requireAuth, async (req, res) => { if (!inst) return res.status(404).json({ error: "实例不存在" }); if (!to || !String(text || "").trim()) return res.status(400).json({ error: "to/text 不能为空" }); try { - await inst.bot.send(String(to), stripMarkdown(String(text))); + await inst.bot.send(String(to), outText(text)); res.json({ ok: true }); } catch (e) { res.status(502).json({ error: "发送失败:" + String(e?.message || e).slice(0, 200) });