微信收信泛化为媒体管线: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
+5 -3
View File
@@ -24,6 +24,8 @@ agent-core 的 `wechat_bindings` 表;扫码二维码呈现在 agent-core 的 W
- 收信 → core `/wxhook/inbox` 复用 **channel 命令层**(`/new /stop /compact /clear /status /help`),
命令回执同步回文本;普通消息(AI 回复)异步由 core 经 `/api/deliver` 推送(`bot.send`)。
图片/视频/文件消息经 SDK `download` 取字节随 inbox 转发(限 20MB),core 落会话任务目录并拼
「用户上传了文件」告知行——与 web 上传附件同一条链路,vision 模型 read_file 即可看图。
- **单线对话**:iLink 扫码登录后只有绑定者本人能与 bot 会话,无陌生人入口,故无联系人授权面。
- 实例凭证按绑定隔离在 `./data/bots/<binding_id>/`,容器重启自动恢复登录免扫码;
启动时扫描该目录逐个拉起实例(core 不在线也照常上线,状态回调失败不影响,首条消息会纠偏)。
@@ -76,7 +78,7 @@ WXBOT_TOKEN=$WXBOT_TOKEN CORE_URL=http://127.0.0.1:8090 \
## 已知边界 / 后续扩展
- 非文本消息(图/语音/文件)目前只回 `bot.empty_hint` 占位。二期:
语音 → SDK `bot.download(msg)` 拿 WAV(需 `silk-wasm`)→ tools/asr `/api/transcribe`;
图片/文件 → core 的 `GET /p/<id>` 公网链喂 `bot.send(userId, {url})`(SDK 自动下载上传 CDN)。
- 语音消息目前回 `bot.empty_hint` 占位(二期:SDK `bot.download(msg)` 拿 WAV(需 `silk-wasm`)
→ tools/asr `/api/transcribe` 转写后进会话)。
- 出站图片/文件二期:core 的 `GET /p/<id>` 公网链喂 `bot.send(userId, {url})`(SDK 自动下载上传 CDN)。
- core 未配 `WXBOT_URL` 时微信功能整体静默关闭(设置页不出现入口,服务零报错)。
+31 -4
View File
@@ -29,7 +29,7 @@ const DEFAULTS = {
bot: {
storage_dir: "/data/bots", // 每绑定一个子目录(目录名 = binding id)
log_level: "info", // debug | info | warn | error | silent
empty_hint: "(暂时只收得到文字哦,发文字或 /help 试试)", // 非文本消息的本地兜底回执
empty_hint: "(这条消息类型暂时接不住——支持文字/图片/视频/文件;指令可发 /help)", // 未接入类型(voice/贴纸等)的本地兑底回执
},
};
@@ -214,16 +214,43 @@ function createInstance(id) {
// 收信 → 转发 core inbox。命令回执(kind=command)同步回文本;
// 普通消息(kind=message)不回执——最终回复稍后由 core 经 /api/deliver 推过来。
// 媒体消息(image/video/file):SDK 下好字节随 inbox 转发(core 落任务目录 + 拼「用户上传了文件」
// 告知行,同 web 附件模式)。msg.text 对媒体消息是 SDK 占位描述(如 "[image]"),不能当真正文,
// 故只认 type==='text';voice 等未接入类型回占位提示。
const MEDIA_KINDS = new Set(["image", "video", "file"]);
async function onIncoming(inst, msg) {
const text = String(msg?.text ?? "").trim();
const type = String(msg?.type || "text");
const text = type === "text" ? String(msg?.text ?? "").trim() : "";
const userId = String(msg?.userId ?? "");
if (!text) {
const isMedia = MEDIA_KINDS.has(type);
if (!text && !isMedia) {
try { await inst.bot.reply(msg, CONFIG.bot.empty_hint); } catch { /* 尽力而为 */ }
return;
}
inst.bot.sendTyping(userId).catch(() => {});
let media = null;
if (isMedia) {
try {
const dl = await inst.bot.download(msg);
const buf = dl && dl.data ? (Buffer.isBuffer(dl.data) ? dl.data : Buffer.from(dl.data)) : null;
if (!buf || !buf.length) {
console.warn(`[wxbot:${inst.id}] ${type} 无可下载媒体`);
try { await inst.bot.reply(msg, CONFIG.bot.empty_hint); } catch { /* 忽略 */ }
return;
}
if (buf.length > 20 * 1024 * 1024) {
try { await inst.bot.send(userId, "⚠️ 附件过大(限 20MB),未转发"); } catch { /* 忽略 */ }
return;
}
media = { kind: type, b64: buf.toString("base64"), filename: dl.fileName || undefined };
} catch (e) {
console.error(`[wxbot:${inst.id}] 媒体下载失败(${type}):`, e?.message || e);
try { await inst.bot.send(userId, "⚠️ 媒体下载失败:" + String(e?.message || e).slice(0, 120)); } catch { /* 忽略 */ }
return; // 媒体即消息主体,下载失败不降级成空文本
}
}
try {
const r = await corePost("/wxhook/inbox", { id: inst.id, from: userId, text });
const r = await corePost("/wxhook/inbox", { id: inst.id, from: userId, text: text || undefined, media: media || undefined }, 30000);
if (r && r.kind === "command" && r.reply) await inst.bot.send(userId, r.reply);
} catch (e) {
if (e.status === 404) {