微信工具心跳:tool_call/tool_result 事件向 wxbot /api/typing 发 sendTyping(每绑定 2s 冷却、失败静默),长工具回合微信端持续显示「正在输入」
This commit is contained in:
+19
-2
@@ -374,11 +374,14 @@ function attachBus() {
|
||||
if (busAttached || !bus) return;
|
||||
busAttached = true;
|
||||
bus.onInternal((sessionId, event, data) => {
|
||||
if (event !== 'assistant_message' || !data) return;
|
||||
if (event === 'assistant_message') { if (!data) return; }
|
||||
else if (event !== 'tool_call' && event !== 'tool_result') return;
|
||||
try {
|
||||
const cs = db.prepare('SELECT user, channel FROM channel_sessions WHERE session_id = ?').get(sessionId);
|
||||
if (!cs || !String(cs.channel || '').startsWith('wx:')) return;
|
||||
enqueueDelivery(cs.channel.slice(3), () => deliverNow(cs.channel.slice(3), data, sessionId));
|
||||
const bid = cs.channel.slice(3);
|
||||
if (event === 'assistant_message') enqueueDelivery(bid, () => deliverNow(bid, data, sessionId));
|
||||
else maybeTypingPing(bid); // 工具调用/回执→「正在输入」心跳:微信端在长工具回合里能看到 AI 还在干活
|
||||
} catch (e) {
|
||||
log.warn('wechat', '投递监听异常:' + msg(e));
|
||||
}
|
||||
@@ -394,6 +397,20 @@ function enqueueDelivery(key, fn) {
|
||||
next.then(() => { if (deliveryQ.get(key) === next) deliveryQ.delete(key); });
|
||||
}
|
||||
|
||||
// sendTyping 心跳:tool_call/tool_result 事件驱动(并行工具批会连发,2s 冷却去重);
|
||||
// 纯 UI 锦上添花,一切失败静默(wxbot 不可达/实例已销都不值得刷日志),不进入投递队列。
|
||||
const typingLast = new Map(); // bid -> 上次发出时间戳
|
||||
function maybeTypingPing(bid) {
|
||||
try {
|
||||
const b = getBinding(bid);
|
||||
if (!b || b.status !== 'online' || !b.wx_to_user) return;
|
||||
const t = now();
|
||||
if (t - (typingLast.get(bid) || 0) < 2000) return;
|
||||
typingLast.set(bid, t);
|
||||
wxbotApi('POST', '/api/typing', { id: b.id, to: b.wx_to_user }, 5000).catch(() => {});
|
||||
} catch (_) { /* 心跳尽力而为 */ }
|
||||
}
|
||||
|
||||
async function deliverNow(bid, m, sessionId) {
|
||||
const b = getBinding(bid);
|
||||
if (!b || b.status !== 'online' || !b.wx_to_user) return;
|
||||
|
||||
@@ -428,7 +428,7 @@ AI 通过 `schedule` 工具为所在会话建周期性任务(`scheduled_tasks`
|
||||
背景:仓库内 `wxbot/`(已入库,作为根 compose 的 `wxbot` 服务)是基于官方 iLink 协议 SDK 的微信「调制解调器」,只挂 `agent-mcp-net` 内网、不暴露公网;身份权威与全部会话逻辑在 core,引擎不感知微信。
|
||||
|
||||
- **模型**:一条 `wechat_bindings` = 一个 core 用户 = 一个 bot 实例(一人一绑,换号先解绑)。binding id 三用:实例控制/投递的寻址键、wxbot 凭证目录名、channel 键(`channel='wx:'+id`,因此命令层、会话自动新建、投递游标查询全部白借 §5.4 的 channel 机制,零新逻辑)。iLink 协议保证扫码登录后仅本人可与 bot 单线对话,无陌生人入口,故无联系人授权面。
|
||||
- **方向三条线**:① Web(设置页→账户→微信内联卡)→ `/api/wechat` → wxbot `/api/instances*`(实例控制;QR SVG 代理内联渲染,只在 has_qr 就绪后取码,服务 token 不出内网);② wxbot 收信 → `POST core:/wxhook/inbox` → 直调 `channel.handleSend()`(文本;image/video/file 媒体先落会话任务目录再拼「用户上传了文件」告知行,同 web 附件链路,voice 二期——命令回执同步回文本;普通消息不回执,等异步投递);③ core 总线监听 assistant_message → 分段(~1800 字/段,行优先聚合)→ `POST wxbot:/api/deliver` → `bot.send`;wxbot 出口统一 stripMarkdown(微信不渲染 markdown)。
|
||||
- **方向三条线**:① Web(设置页→账户→微信内联卡)→ `/api/wechat` → wxbot `/api/instances*`(实例控制;QR SVG 代理内联渲染,只在 has_qr 就绪后取码,服务 token 不出内网);② wxbot 收信 → `POST core:/wxhook/inbox` → 直调 `channel.handleSend()`(文本;image/video/file 媒体先落会话任务目录再拼「用户上传了文件」告知行,同 web 附件链路,voice 二期——命令回执同步回文本;普通消息不回执,等异步投递);③ core 总线监听 assistant_message → 分段(~1800 字/段,行优先聚合)→ `POST wxbot:/api/deliver` → `bot.send`;wxbot 出口统一 stripMarkdown(微信不渲染 markdown);tool_call/tool_result 事件另发 `/api/typing` sendTyping 心跳(每绑定 2s 冷却、失败静默),长工具回合里微信持续显示「正在输入」,文本投递后自动取消。
|
||||
- **渠道上下文**(ensureChannelContext,普通/媒体/带尾随文本的 /new 收信时幂等确保,先于首条消息入库):微信会话直接落 executing(跳过顾问 handoff,与定时任务同理由)+ 注入一条 `channel_note` user 行(微信环境说明:单线纯文本、给链接即交付卡片、入站文件已存工作目录等,AI 第一回合即知);/clear 删光历史后下条消息自动重注入。
|
||||
- **出站文件(链接→卡片)**:deliverNow 扫正文 `/p/<22位id>` 链 → 同进程 `publicFiles.resolve` + 归属硬校验(只转本会话签发的链接,防外流)→ 本地读字节(单文件≤20MB、每条回复最多 5 个)→ `POST wxbot:/api/deliver_media` → SDK 按扩展名自动路由 image/video/file 卡片;尽力而为:失败只 warn,链接文字留在正文作兜底(AI 无需感知卡片机制,channel_note 已告知「给链接即交付」)。
|
||||
- **投递游标** `last_msg_id` 落库 + 每绑定串行队列:重启不重发旧回复,wxbot 短暂不可达不丢(下一条连带补齐);errAck/纯工具轮(空文本)不进微信,`__blocks__` 图消息降级为占位文案。文本投递经 wxbot 出口统一 stripMarkdown(微信不渲染 markdown)。
|
||||
|
||||
@@ -66,6 +66,7 @@ docker compose up -d agent-core # core 带 WXBOT_URL/WXBOT_TOKEN 重建(
|
||||
| DELETE | `/api/instances/:id` | 销毁实例并删登录凭证(解绑) |
|
||||
| GET | `/api/instances/:id/qr.svg` | 当前登录二维码(core 设置页代理渲染) |
|
||||
| POST | `/api/deliver {id,to,text}` | 文本投递(core 已分段;出口统一 stripMarkdown,微信不渲染 markdown) |
|
||||
| POST | `/api/typing {id,to}` | 「正在输入」心跳(core 在工具调用/结果时触发,每绑定 2s 冷却;typing 自然过期无需 stopTyping) |
|
||||
| POST | `/api/deliver_media {id,to,filename,b64}` | 附件投递(core 已把正文里的 /p/ 链接换成本地字节并做归属/大小校验;SDK 按扩展名自动路由 image/video/file 卡片) |
|
||||
|
||||
## 本地调试
|
||||
|
||||
@@ -333,6 +333,20 @@ app.post("/api/deliver", requireAuth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// core → wxbot「正在输入」心跳(工具调用/回执节奏触发;typing 由微信侧自动过期,无需 stopTyping)
|
||||
app.post("/api/typing", requireAuth, async (req, res) => {
|
||||
const { id, to } = req.body || {};
|
||||
const inst = instances.get(String(id || ""));
|
||||
if (!inst) return res.status(404).json({ error: "实例不存在" });
|
||||
if (!to) return res.status(400).json({ error: "to 不能为空" });
|
||||
try {
|
||||
await inst.bot.sendTyping(String(to));
|
||||
res.json({ ok: true });
|
||||
} catch (e) {
|
||||
res.status(502).json({ error: "typing 失败:" + String(e?.message || e).slice(0, 200) });
|
||||
}
|
||||
});
|
||||
|
||||
// core → wxbot 附件投递(链接→卡片):core 已把正文里的 /p/ 链接换成本地读好的字节并做
|
||||
// 归属/大小/数量校验;这里只转发 SDK——bot.send 的 file 按扩展名自动路由 image/video/file 卡片,
|
||||
// CDN 上传 + 加密全在 SDK 内。
|
||||
|
||||
Reference in New Issue
Block a user