微信收信泛化为媒体管线:image/video/file 经 SDK download → inbox(base64) → core magic 嗅探落会话任务目录(文件留消毒原名、图/视频生成 wximg-/wxvid- 名)+ files 元数据 + 「用户上传了文件」告知行(同 web 附件链路,vision 模型 read_file 即见);voice 二期占位;hookRouter 自带 32mb 解析器并前置于全局 json(2mb)

This commit is contained in:
2026-09-27 23:39:17 +08:00
parent b2e81a4f24
commit d1f4b354c1
7 changed files with 139 additions and 19 deletions
+7
View File
@@ -58,6 +58,12 @@ function newSessionFor(db, user, channel, firstText) {
return { s, sent };
}
// 解析 (user,channel) 的当前会话,无绑定则新建并绑定(不发消息)——
// 微信图片入口需要先拿到会话才能落任务目录文件,随后再走 handleSend 拼告知行。
function ensureSession(db, user, channel) {
return boundSession(db, user, channel) || newSessionFor(db, user, channel, '').s;
}
// ---------- 命令解析 ----------
// 严格匹配:整条消息恰好是命令词(仅 /new 可带尾随文本)才算命令。
// 首词形如 /纯ASCII词 但不在清单 → 回「未知命令」提示、不转发模型(防弱模型就着 /hlep 演一段);
@@ -209,3 +215,4 @@ router.post('/send', wrap((req, res) => {
module.exports = router;
module.exports.handleSend = handleSend;
module.exports.ensureSession = ensureSession;
+7 -4
View File
@@ -1,8 +1,8 @@
// 微信接入路由:
// - 本文件默认导出 = Web 侧 router(挂在 auth.middleware 之后,cookie 鉴权 + req.user 隔离);
// - 具名导出 hookRouter = wxbot → core 的内网回调入口(/wxhook/*),挂在 auth.middleware 之前,
// 形态同 GET /p/<id>(Core 上除登录外的匿名入口家族),靠共享服务 token(Bearer,常量时间比较)把门。
// 业务全部在 wechat.js,这里只做鉴权、归属过滤与 HTTP 形状。
// 本文件默认导出 = Web 侧 router(挂在 auth.middleware 之后,cookie 鉴权 + req.user 隔离);
// 具名导出 hookRouter = wxbot → core 的内网回调入口(/wxhook/*),挂在 auth.middleware 与全局
// express.json 之前(媒体转发的 base64 远超 2mb,由下方专用大限额解析器处理),
// 凭 Bearer 服务 token(常量时间比较)把门。业务全在 wechat.js,这里只做鉴权、归属过滤与 HTTP 形状。
const crypto = require('crypto');
const express = require('express');
const engine = require('../engine');
@@ -10,6 +10,9 @@ const wechat = require('../wechat');
const router = express.Router();
const hookRouter = express.Router();
// wxbot 专用 JSON 入口:媒体 base64 大 body(解码后限 20MB,b64 膨胀 4/3);比全局 2mb 宽得多。
// 本 router 先于全局解析器挂载,body-parser 认到 req._body 后全局那份会自动跳过。
hookRouter.use(express.json({ limit: '32mb' }));
function wrap(fn) {
return function (req, res) {
+5 -4
View File
@@ -34,6 +34,11 @@ wechat.init(cfg, db, bus);
const app = express();
const server = http.createServer(app);
// wxbot 内网回调(/wxhook/inbox|hook):凭共享服务 token(Bearer)把门,微信用户永远不该直接碰
// 这两个端点(wxbot 只挂内网)。必须挂在全局 express.json(2mb) 之前:媒体 base64 转发体远超 2mb,
// 由 hookRouter 自带的 32mb 解析器先读掉并置 req._body,下游全局 json 解析器自动跳过(同 executor 不冲突)。
app.use('/wxhook', require('./routes/wechat').hookRouter);
app.use(express.json({ limit: '2mb' }));
// 登录认证(cookie 会话):users 配在 config.yml 的 auth.users;留空则不启用。
@@ -45,10 +50,6 @@ app.use(auth.router); // /login、/logout、/api/login 等免认证端点
// 安全性来自“22 字符随机 ID + 服务端过期”,详见 routes/public-files.js 头注释。
app.use('/p', require('./routes/public-files'));
// wxbot 内网回调(/wxhook/inbox|hook):同在 auth.middleware 之前,凭共享服务 token(Bearer)把门,
// 微信用户永远不该直接碰这两个端点(wxbot 只挂内网,不暴露公网)。
app.use('/wxhook', require('./routes/wechat').hookRouter);
app.use(auth.middleware); // 其余请求需登录态(cookie 或 Basic 头)
app.use(express.static(path.join(__dirname, 'public')));
+82 -2
View File
@@ -12,9 +12,11 @@
// 降级承诺:wxbot.url 未配置 = 功能整体静默关闭;配置了但 wxbot 没起 = 启动只留一行日志,
// 面板显示「未连接」、操作时报可读错误,绝不抛出未捕获异常影响 Web 主流程。
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const engine = require('./engine');
const log = require('./log');
const channel = require('./routes/channel'); // 只用 handleSend(模块加载无副作用;engine 不 require 本模块,无环)
const channel = require('./routes/channel'); // 只用 handleSend/ensureSession(模块加载无副作用;engine 不 require 本模块,无环)
let cfg, db, bus;
let busAttached = false;
@@ -234,6 +236,12 @@ function handleHook(body) {
// 微信来消息 → channel 层同一入口。返回 handleSend 结果;抛错冒给 hook 路由映射成非 2xx,
// wxbot 会把 error 文案直接回给用户(IM 里「出错要出声」)。
// 媒体消息(inbox 带 media:{b64,kind,filename?},wxbot 已从 CDN 下好):统一落会话任务目录
// + 记 files 元数据表 → 按 web 上传同款拼「用户上传了文件:<名>」作正文走 handleSend。
// 命名规则:图片/视频微信侧本无名 → 生成 wximg-/wxvid- 时间戳名;文件保留原名(sanitise 后,
// 对 AI 可读性重要),撞名加 -N;扩展名以 magic 嗅探为准(不信声明值)。
const MEDIA_MAX_BYTES = 20 * 1024 * 1024;
const MEDIA_KINDS = new Set(['image', 'video', 'file']);
function handleInbox(body) {
const b = getBinding(String((body || {}).id || ''));
if (!b) throw Object.assign(new Error('绑定不存在(可能已解绑)'), { status_code: 404 });
@@ -241,14 +249,86 @@ function handleInbox(body) {
if (from && from !== b.wx_to_user) touchBinding(b.id, { wx_to_user: from });
// 能收到消息就说明实例活着;漏收 online 回调(core 重启窗口)时顺带纠偏
if (b.status !== 'online') touchBinding(b.id, { status: 'online', note: null });
let text = String((body || {}).text || '').trim();
const m = (body || {}).media;
if (m && String(m.b64 || '').trim()) {
const kind = MEDIA_KINDS.has(String(m.kind || '')) ? String(m.kind) : 'file';
let buf;
try { buf = Buffer.from(String(m.b64).trim().slice(0, Math.ceil(MEDIA_MAX_BYTES / 3) * 4 + 1024), 'base64'); } catch (_) { buf = null; }
if (!buf || !buf.length) throw Object.assign(new Error('媒体数据无法解析'), { status_code: 400 });
if (buf.length > MEDIA_MAX_BYTES) throw Object.assign(new Error('附件过大(限 20MB)'), { status_code: 400 });
const s = channel.ensureSession(db, b.user, 'wx:' + b.id);
const name = saveTaskMedia(s, buf, kind, String(m.filename || ''));
text = text ? '用户上传了文件:' + name + '\n\n' + text : '用户上传了文件:' + name;
log.info('file', (b.user || '-') + ' [微信 ' + b.id + '] ' + kind + ' 落任务目录 ' + s.id + '/' + name + '(' + buf.length + ' 字节)');
}
return channel.handleSend(db, {
user: b.user,
channel: 'wx:' + b.id,
text: (body || {}).text,
text,
tag: (b.user || '-') + ' [微信 ' + b.id + ']',
});
}
// magic bytes 定真实格式(不信声明扩展名):图片四类 + 常见视频两类;认不出的交给命名处兕底。
function sniffMediaExt(buf) {
if (buf.length > 3 && buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) return 'jpg';
if (buf.length > 8 && buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47) return 'png';
if (buf.length > 3 && buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46) return 'gif';
if (buf.length > 12 && buf.toString('ascii', 0, 4) === 'RIFF' && buf.toString('ascii', 8, 12) === 'WEBP') return 'webp';
if (buf.length > 12 && buf.toString('ascii', 4, 8) === 'ftyp') return 'mp4';
if (buf.length > 4 && buf[0] === 0x00 && buf[1] === 0x00 && buf[2] === 0x01 && buf[3] === 0x00) return 'mp4'; // 裸 MPEG 容器/其他
return '';
}
// 文件名消毒:只留路径末段,剔除控制字符与路径/shell 危符(保留中文),限长;全脏则退回 fallback。
function sanitizeMediaName(raw, fallback) {
let n = String(raw || '').split(/[\\/]/).pop().trim();
n = n.replace(/[\u0000-\u001f"'`$\\:*?<>|\u200b\u200e\u200f]+/g, '-').replace(/\s+/g, ' ').replace(/^-+/, '').replace(/\.+$/, '');
if (n.startsWith('.')) n = n.slice(1); // 防隐藏文件(点前缀在文件列表被隐)
if (n.length > 48) {
const dot = n.lastIndexOf('.');
const ext = dot > 0 && n.length - dot <= 6 ? n.slice(dot) : '';
n = n.slice(0, 48 - ext.length) + ext;
}
return n || (fallback || 'file');
}
// 落盘:图片/视频生成时间戳名(扩展名以嗅探为准),文件保留消毒后的原名;撞名加 -N;记 files 元数据行。
function saveTaskMedia(s, buf, kind, rawName) {
const dir = engine.taskDirOf(s.id);
const sniff = sniffMediaExt(buf);
const d = new Date();
const pad = n => String(n).padStart(2, '0');
const ts = d.getFullYear() + pad(d.getMonth() + 1) + pad(d.getDate()) + '-' + pad(d.getHours()) + pad(d.getMinutes()) + pad(d.getSeconds());
let name;
if (kind === 'image' || kind === 'video') {
const prefix = kind === 'image' ? 'wximg' : 'wxvid';
let ext = sniff;
if (!ext) { // 嗅探失败才退回声明名里的扩展名(≤5 位字母数字),再不行 bin
const dot = String(rawName || '').lastIndexOf('.');
const tail = dot >= 0 ? String(rawName).slice(dot + 1).toLowerCase() : '';
ext = /^[a-z0-9]{1,5}$/.test(tail) ? tail : 'bin';
}
name = prefix + '-' + ts + '.' + ext;
} else {
const safe = sanitizeMediaName(rawName, 'wxfile-' + ts);
const dot = safe.lastIndexOf('.');
const stem = dot > 0 ? safe.slice(0, dot) : safe;
const ext = dot > 0 ? safe.slice(dot) : (sniff ? '.' + sniff : '');
name = stem + ext;
}
let final = name;
for (let n = 1; fs.existsSync(path.join(dir, final)); n++) {
const dot = name.lastIndexOf('.');
final = (dot > 0 ? name.slice(0, dot) : name) + '-' + n + (dot > 0 ? name.slice(dot) : '');
}
fs.writeFileSync(path.join(dir, final), buf);
db.prepare('INSERT INTO files (session_id, filename, size, created_at) VALUES (?,?,?,?)')
.run(s.id, final, buf.length, now());
return final;
}
// ---------- 投递:进程内总线监听 → 分段 push wxbot /api/deliver ----------
// 只推最终 assistant 文本(type=errAck / 纯工具轮空文本 / 存档类事件都不进微信);
// 游标 last_msg_id 落库:core 重启不补发历史,wxbot 短暂不可达时不丢(下一条事件连带补齐)。