feat(channel): 微信端全局深度思考开关 /think(=H5 按钮的指令形态,复用既有 deepThink 链路)

- wechat_bindings 补 deep_think 列(默认 1=开,微信没按钮可点,默认按开走);
  开关随绑定不随会话,/switch /new 都不丢
- /think on|off(隐式 1|0 不写 help;裸 /think 报当前态):写 binding 行;
  投递普通消息时(channel=wx:)把开关值替进既有 postUserMessage 的 deepThink
  参数——开=沿用现链路合并模型 thinking_params,关=什么都不带,与 H5 按钮
  勾选/取消完全同构;llm/engine/web 零改动
- /status 对 wx 通道显示「深度思考:开/关」
- attachBus 放行 reasoning_delta:思考流也驱动 sendTyping 心跳(不进台账);
  心跳冷却判定前置到查库之前,逐 token 高频帧的开销仅为 Map 读写
- ARCHITECTURE 目录树/§5.13/§路由表同步
This commit is contained in:
2026-10-02 11:20:28 +08:00
parent 9fb7577860
commit 4c44604566
4 changed files with 58 additions and 19 deletions
+6
View File
@@ -145,6 +145,8 @@ function initDb(dbPath) {
bot_account_id TEXT, -- bot 上线后的微信账号标识(登录回调带回,展示用)
wx_to_user TEXT, -- 最近来话人 userId(deliver 回发对象;实际上只会是主人自己)
last_msg_id INTEGER NOT NULL DEFAULT 0, -- 投递游标:id ≤ 此值的 assistant 消息已送过微信
deep_think INTEGER NOT NULL DEFAULT 1, -- 微信端全局深度思考开关(/think;= H5 按钮的微信形态,
-- 随绑定不随会话;投递时替进消息 API 的 deepThink 字段)
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
@@ -228,6 +230,10 @@ function initDb(dbPath) {
// 迁移:title_locked 用户手动重命名过标题(renameSession 置 1):handoff 的自动标题见锁即跳过,
// 不得静默冲掉用户给会话起的名字;清空会话时随标题一并复位(旧名字已文不对题)。
if (!cols.some(c => c.name === 'title_locked')) db.exec("ALTER TABLE sessions ADD COLUMN title_locked INTEGER NOT NULL DEFAULT 0");
// 迁移:wechat_bindings 补 deep_think 列——微信端全局深度思考开关(routes/channel.js /think 读写,
// 投递时作为既有 deepThink 参数的取值来源);缺行/旧库默认 1=开(微信没按钮,默认按开走)。
const bindCols = db.prepare('PRAGMA table_info(wechat_bindings)').all();
if (!bindCols.some(c => c.name === 'deep_think')) db.exec('ALTER TABLE wechat_bindings ADD COLUMN deep_think INTEGER NOT NULL DEFAULT 1');
// 迁移:phase 语义重定义为 advising(顾问:澄清需求/规划)/ executing(执行)。
// 存量会话(旧值 planning/NULL)统一回填 executing——它们都有历史,行为与改造前一致、零打扰;
// 只有新建会话进 advising(createSession 显式写入)。幂等,每次启动一条廉价 UPDATE。
+41 -10
View File
@@ -52,10 +52,10 @@ function setBinding(db, user, channel, sessionId) {
}
// /new 与「无绑定时发普通消息」共用:新建(engine 立即绑模型,空池 503 冒泡)+ 换绑 + 可选首条消息
function newSessionFor(db, user, channel, firstText) {
function newSessionFor(db, user, channel, firstText, opts) {
const s = engine.createSession('', '', 'secretary', user);
setBinding(db, user, channel, s.id);
const sent = firstText ? engine.postUserMessage(s.id, firstText) : null;
const sent = firstText ? engine.postUserMessage(s.id, firstText, opts) : null;
return { s, sent };
}
@@ -147,10 +147,11 @@ function modelsReply(ms, curModelId) {
// 严格匹配:整条消息恰好是命令词(ARG_CMDS 内的可带参数)才算命令。
// 首词形如 /纯ASCII词 但不在清单 → 回「未知命令」提示、不转发模型(防弱模型就着 /hlep 演一段);
// 其余一律普通消息:/home/x、/a.b 这类路径代码,以及 /clear abc 这种裸命令带尾巴的(用户约定:强检查不匹配)。
const CMDS = new Set(['new', 'stop', 'compact', 'clear', 'status', 'help', 'list', 'switch', 'rename', 'delete', 'models']);
const CMDS = new Set(['new', 'stop', 'compact', 'clear', 'status', 'help', 'list', 'switch', 'rename', 'delete', 'models', 'think']);
// 可带参数的命令:/new 尾随文本作首条消息;/switch /delete 吃 /list 编号或会话 id;
// /rename 吃新标题;/models 可带模型编号或完整 id。清单外的命令带尾巴仍按普通消息回落。
const ARG_CMDS = new Set(['new', 'switch', 'rename', 'delete', 'models']);
// /rename 吃新标题;/models 可带模型编号或完整 id;/think 吃 on|off(1|0 同样认,隐式不写 help)。
// 清单外的命令带尾巴仍按普通消息回落。
const ARG_CMDS = new Set(['new', 'switch', 'rename', 'delete', 'models', 'think']);
function parseCommand(text) {
const t = String(text || '').trim();
@@ -178,6 +179,7 @@ const HELP_TEXT = [
'',
'**模型**',
'- `/models [编号]` 切换指定模型',
'- `/think on|off` 微信深度思考开关',
'',
'**上下文**',
'- `/stop` 终止当前回合',
@@ -199,7 +201,7 @@ function fmtTime(ts) {
return (d.getMonth() + 1) + '-' + pad(d.getDate()) + ' ' + pad(d.getHours()) + ':' + pad(d.getMinutes());
}
function statusReply(db, s) {
function statusReply(db, s, channel) {
const run = engine.getRun(s.id);
const looping = !!(run && run.looping);
const lines = [
@@ -208,6 +210,11 @@ function statusReply(db, s) {
'- 状态:' + (looping ? '回合进行中' : (STATUS_TEXT[s.status] || s.status)),
'- 模型:`' + (s.model_id || '未绑定') + '`',
];
// 思考开关只对微信通道有意义(web 端用按钮,不读这列)
if (String(channel || '').startsWith('wx:')) {
const bt = db.prepare('SELECT deep_think FROM wechat_bindings WHERE id = ?').get(String(channel).slice(3));
if (bt) lines.push('- 深度思考:' + (bt.deep_think !== 0 ? '开' : '关'));
}
const t = db.prepare('SELECT cron_expr, next_run_at, last_run_at, last_status FROM scheduled_tasks WHERE session_id = ?').get(s.id);
lines.push(t
? '- 定时任务:cron `' + t.cron_expr + '`,下次 ' + fmtTime(t.next_run_at) + '(上次 ' + fmtTime(t.last_run_at) + ' ' + (t.last_status || '-') + ')'
@@ -228,15 +235,19 @@ function handleSend(db, opts) {
const parsed = parseCommand(text);
// 普通消息:解析当前会话,无绑定自动新建(IM 用户不会先打 /new 才能聊天)
// 普通消息:解析当前会话,无绑定自动新建(IM 用户不会先打 /new 才能聊天)。
// 微信通道以 binding 行的全局 deep_think 开关作消息的 deepThink 取值(与 H5 按钮 body 同义,
// 只是没得点、改用 /think 拨);非 wx 通道照旧不传 opts,行为零变化。
if (!parsed || parsed.normal) {
const wx = String(channel).startsWith('wx:');
const deepThink = wx ? ((db.prepare('SELECT deep_think FROM wechat_bindings WHERE id = ?').get(channel.slice(3)) || {}).deep_think !== 0) : undefined;
let s = boundSession(db, user, channel);
let sent;
if (!s) {
const r = newSessionFor(db, user, channel, text);
const r = newSessionFor(db, user, channel, text, wx ? { deepThink } : undefined);
s = r.s; sent = r.sent;
} else {
sent = engine.postUserMessage(s.id, text);
sent = engine.postUserMessage(s.id, text, wx ? { deepThink } : undefined);
}
log.info('msg', tag + ' channel 消息 → ' + s.id);
return { kind: 'message', reply: '已发送到会话 ' + s.id, session_id: s.id, message_id: sent.message_id };
@@ -313,6 +324,26 @@ function handleSend(db, opts) {
return { kind: 'command', reply: modelsReply(modelChoices(), s ? s.model_id : ''), session_id: s ? s.id : null };
}
// /think:微信全局深度思考开关——存用户 binding 行(不随会话/switch 丢),投递时替进
// 既有 deepThink 参数(开=沿用现链路合并模型 thinking_params,关=什么都不带,
// 与 H5 按钮勾选/取消完全同构)。裸 /think 报当前态;1|0 隐式同义(不写 help)。
if (cmd === 'think') {
const v = /^(on|1)$/i.test(parsed.rest) ? 1 : /^(off|0)$/i.test(parsed.rest) ? 0 : null;
if (parsed.rest && v === null) {
return { kind: 'command', reply: '用法:/think on|off', session_id: s ? s.id : null };
}
const b = db.prepare('SELECT id, deep_think FROM wechat_bindings WHERE user = ?').get(user);
if (!b) {
return { kind: 'command', reply: '未绑定微信,此开关不适用', session_id: s ? s.id : null };
}
if (v === null) {
return { kind: 'command', reply: '微信深度思考当前:' + (b.deep_think !== 0 ? '开' : '关') + '(/think on|off 切换)', session_id: s ? s.id : null };
}
if (v !== b.deep_think) db.prepare('UPDATE wechat_bindings SET deep_think = ? WHERE id = ?').run(v, b.id);
log.info('task', tag + ' channel /think → ' + (v ? 'on' : 'off') + '(用户全局,binding ' + b.id + ')');
return { kind: 'command', reply: '微信深度思考已' + (v ? '开启(后续消息带思考参数)' : '关闭(后续消息不带)'), session_id: s ? s.id : null };
}
// 其余命令都作用于当前会话;无绑定时不隐式新建(那不是用户点这条命令的意图)
if (!s) {
return { kind: 'command', reply: '当前没有会话,请直接发送消息', session_id: null };
@@ -363,7 +394,7 @@ function handleSend(db, opts) {
}
// cmd === 'status'
return { kind: 'command', reply: statusReply(db, s), session_id: s.id };
return { kind: 'command', reply: statusReply(db, s, channel), session_id: s.id };
}
// HTTP 入口:POST /api/channel/send {channel?, text} → handleSend 结果。
+8 -6
View File
@@ -433,7 +433,7 @@ function attachBus() {
busAttached = true;
bus.onInternal((sessionId, event, data) => {
if (event === 'assistant_message') { if (!data) return; }
else if (event !== 'done' && event !== 'tool_call' && event !== 'tool_result') return;
else if (event !== 'done' && event !== 'tool_call' && event !== 'tool_result' && event !== 'reasoning_delta') 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;
@@ -441,12 +441,13 @@ function attachBus() {
if (event === 'assistant_message') enqueueDelivery(bid, () => onAssistantEvent(bid, data, sessionId));
else if (event === 'done') enqueueDelivery(bid, () => flushHeld(bid));
else {
// 工具调用/回执:心跳 + 台账记账;攒满一屏即经串行队列独立发台账(见策略注释第 4 条)
// 工具调用/回执:心跳 + 台账记账;攒满一屏即经串行队列独立发台账(见策略注释第 4 条)。
// reasoning_delta(思考流)不进台账,只走下面的心跳——微信用户能看到「正在输入」在思考。
if (event === 'tool_call') {
toolDigestPush(bid, data);
if (getTurn(bid).tools.length >= DIGEST_MAX_LINES) enqueueDelivery(bid, () => flushLedger(bid, DIGEST_MAX_LINES));
} else if (event === 'tool_result') toolDigestMark(bid, data);
maybeTypingPing(bid); // 「正在输入」心跳:微信端在长工具回合里能看到 AI 还在干活
maybeTypingPing(bid); // 「正在输入」心跳:长工具/思考回合里微信持续看到 AI 还在干活
}
} catch (e) {
log.warn('wechat', '投递监听异常:' + msg(e));
@@ -463,15 +464,16 @@ function enqueueDelivery(key, fn) {
next.then(() => { if (deliveryQ.get(key) === next) deliveryQ.delete(key); });
}
// sendTyping 心跳:tool_call/tool_result 事件驱动(并行工具批会连发,2s 冷却去重);
// sendTyping 心跳:tool_call/tool_result 与 reasoning_delta(逐 token 高频)事件驱动;
// 冷却判定前置在查库之前,思考流的每帧开销只是两次 Map 读写(并行工具批/思考流都靠它去重);
// 纯 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;
const b = getBinding(bid);
if (!b || b.status !== 'online' || !b.wx_to_user) return;
typingLast.set(bid, t);
wxbotApi('POST', '/api/typing', { id: b.id, to: b.wx_to_user }, 5000).catch(() => {});
} catch (_) { /* 心跳尽力而为 */ }