@zaofan/dsh-qqbot 1.13.19 → 1.13.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/client/qqbot-settings.js +14 -0
  2. package/dist/config.d.ts +62 -0
  3. package/dist/config.d.ts.map +1 -1
  4. package/dist/config.js +12 -0
  5. package/dist/config.js.map +1 -1
  6. package/dist/features/context-memo.d.ts +10 -0
  7. package/dist/features/context-memo.d.ts.map +1 -1
  8. package/dist/features/context-memo.js +35 -11
  9. package/dist/features/context-memo.js.map +1 -1
  10. package/dist/features/ext-outbound.d.ts +16 -0
  11. package/dist/features/ext-outbound.d.ts.map +1 -1
  12. package/dist/features/ext-outbound.js +78 -5
  13. package/dist/features/ext-outbound.js.map +1 -1
  14. package/dist/features/ext-reload.d.ts.map +1 -1
  15. package/dist/features/ext-reload.js +39 -0
  16. package/dist/features/ext-reload.js.map +1 -1
  17. package/dist/features/reasoning-bus.d.ts +27 -0
  18. package/dist/features/reasoning-bus.d.ts.map +1 -1
  19. package/dist/features/reasoning-bus.js +122 -6
  20. package/dist/features/reasoning-bus.js.map +1 -1
  21. package/dist/features/session-kind.d.ts +73 -0
  22. package/dist/features/session-kind.d.ts.map +1 -0
  23. package/dist/features/session-kind.js +212 -0
  24. package/dist/features/session-kind.js.map +1 -0
  25. package/dist/gateway/bootstrap.d.ts.map +1 -1
  26. package/dist/gateway/bootstrap.js +158 -18
  27. package/dist/gateway/bootstrap.js.map +1 -1
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +32 -0
  30. package/dist/index.js.map +1 -1
  31. package/dist/session/session-manager.d.ts.map +1 -1
  32. package/dist/session/session-manager.js +221 -7
  33. package/dist/session/session-manager.js.map +1 -1
  34. package/dist/transport/outbound-buffer.d.ts +5 -4
  35. package/dist/transport/outbound-buffer.d.ts.map +1 -1
  36. package/dist/transport/outbound-buffer.js +6 -3
  37. package/dist/transport/outbound-buffer.js.map +1 -1
  38. package/dist/transport/outbound.d.ts.map +1 -1
  39. package/dist/transport/outbound.js +33 -6
  40. package/dist/transport/outbound.js.map +1 -1
  41. package/dist/transport/turn-error.d.ts +38 -1
  42. package/dist/transport/turn-error.d.ts.map +1 -1
  43. package/dist/transport/turn-error.js +51 -2
  44. package/dist/transport/turn-error.js.map +1 -1
  45. package/package.json +1 -1
@@ -19,13 +19,14 @@ import { describeExtCommandReload, describeExtInboundReload, describeExtOutbound
19
19
  import { SessionId } from '@deepseek-ai/dsh-session';
20
20
  import { FIXED_CHANNEL_CONTEXT, REFERENCE_CONTEXT } from '../config.js';
21
21
  import { LATEX_HINT_BLOCK } from '../config.js';
22
+ import { describeSessionKind, buildSessionKindHint } from '../features/session-kind.js';
22
23
  import { takePendingMemo, recallLines } from '../features/people-memo.js';
23
24
  import { isContextlessActive, contextlessSmartOf, contextlessMemoMaxOf, contextlessWindowOf, getContextless, listContextless, describeStorePath } from '../features/contextless-store.js';
24
25
  import { contextlessSegmentOf, contextlessSegmentKeepOf, contextlessSegmentScoreMinOf } from '../features/contextless-store.js';
25
26
  import { getSegmentScore } from '../features/segment-score.js';
26
27
  import { offloadHistoryImages } from '../features/image-offload.js';
27
28
  import { trimHistoryForContextless } from '../features/contextless-trim.js';
28
- import { memoTextForInject, setMemoMaxItems } from '../features/context-memo.js';
29
+ import { memoForInject, setMemoMaxItems } from '../features/context-memo.js';
29
30
  import { traceContextless } from '../features/contextless-store.js';
30
31
  import { dataRootOf, stickerDirOf } from '../gateway/data-root.js';
31
32
  import { SettingsReader } from '../model/settings-reader.js';
@@ -1777,6 +1778,21 @@ export class SessionManager {
1777
1778
  ctxSmartOn = isContextlessActive(skSmart, this.config.contextlessMode)
1778
1779
  && contextlessSmartOf(skSmart, this.config.contextlessSmart);
1779
1780
  }
1781
+ // ★ CodeRabbit:**重启/回收后 `findByAgent` 全空 ⇒ ctxSmartOn 恒 false**,
1782
+ // 智能提示与自己的备忘都注不进去(连下面那段反查都没机会执行)。
1783
+ // 补一次 model-prefs 反查(与后面 `_kindKey` 同口径),有活跃记录时仍以记录为准。
1784
+ if (!ctxSmartOn) {
1785
+ try {
1786
+ const sidNow = String(agent?.session?.id ?? '');
1787
+ const keyNow = sidNow ? String(this.modelResolver.findKeyBySessionId(sidNow) ?? '') : '';
1788
+ if (keyNow) {
1789
+ ctxSmartOn = isContextlessActive(keyNow, this.config.contextlessMode)
1790
+ && contextlessSmartOf(keyNow, this.config.contextlessSmart);
1791
+ traceContextless('[ctxsmart] 反查兜底 key=' + keyNow + ' => on=' + String(ctxSmartOn));
1792
+ }
1793
+ }
1794
+ catch { /* ignore */ }
1795
+ }
1780
1796
  if (ctxSmartOn && skSmart && dataRootSmart && (!Number.isFinite(currentStep) || currentStep <= 1)) {
1781
1797
  const req = readContextPending(dataRootSmart, skSmart);
1782
1798
  if (req) {
@@ -1884,6 +1900,10 @@ export class SessionManager {
1884
1900
  // ⚠️ 原来是挂在 `if (!body) return decision;` 之后的 —— 群守则体为空时直接早退,
1885
1901
  // 注入根本没机会执行(2026-10-01 实测:AI 反馈"没有任何一条要求我判断是不是同一个话题")。
1886
1902
  let ctxSmartText = '';
1903
+ let selfMemoText = '';
1904
+ let selfMemoKey = '';
1905
+ /** 备忘**读取是否成功**(CodeRabbit:区分「读到空」=用户清空 与 「读失败」=不能作废现有备忘) */
1906
+ let selfMemoOk = false;
1887
1907
  try {
1888
1908
  // ⚠️ 2026-10-01:靠「这个 turn 注过没有」去重 —— 原来用 step<=1 判,实测仍会一回合注两份
1889
1909
  // (宿主每 step 都触发,且 currentStep 未必从 1 递增)。
@@ -1891,14 +1911,30 @@ export class SessionManager {
1891
1911
  // 同一回合内 step2 的 messages 必然带着 step1 注入的那份,因此一定拦得住(比按 turn 可靠)。
1892
1912
  // ★★★ 2026-10-01 最终解:**替换式注入** —— 不赌任何时序判据,改为"每次注之前先剔掉旧的同类块",
1893
1913
  // 这样无论宿主触发多少次(实测每次工具往返都触发),上下文里恒定只有 1 份。
1914
+ //
1915
+ // ★★ 2026-11-21 主人指出:备忘原来**和群守则拼在同一条 body 里**,而那条的去重只比群守则指纹
1916
+ // ⇒ 群守则不变时,备忘改了也**永远不会重新注入**(模型看到的始终是最老那份备忘)。
1917
+ // 现在拆开:固定提示继续跟 body 走;**备忘独立成块**(`qqbot:self-memo`),自己判自己的重。
1894
1918
  if (ctxSmartOn && dataRootSmart) {
1895
1919
  const recM = agent ? this.findByAgent(agent) : undefined;
1896
- const skM = recM?.sessionKey;
1920
+ let skM = recM?.sessionKey;
1921
+ if (!skM) {
1922
+ // 重启/回收后 findByAgent 查不到宿主恢复的会话 ⇒ 用 model-prefs 反查(同会话类型注入那套)
1923
+ try {
1924
+ const sidNow = String(agent?.session?.id ?? '');
1925
+ skM = sidNow ? String(this.modelResolver.findKeyBySessionId(sidNow) ?? '') : '';
1926
+ }
1927
+ catch { /* ignore */ }
1928
+ }
1897
1929
  if (skM) {
1898
- const memo = memoTextForInject(dataRootSmart, skM).trim();
1899
- const inner = [CTX_SMART_REMINDER, memo].filter(Boolean).join('\n\n');
1900
- if (inner)
1901
- ctxSmartText = '<system-reminder>\n' + inner + '\n</system-reminder>';
1930
+ selfMemoKey = skM;
1931
+ const mr = memoForInject(dataRootSmart, skM);
1932
+ selfMemoOk = mr.ok;
1933
+ const memo = mr.text.trim();
1934
+ if (CTX_SMART_REMINDER)
1935
+ ctxSmartText = '<system-reminder>\n' + CTX_SMART_REMINDER + '\n</system-reminder>';
1936
+ if (memo)
1937
+ selfMemoText = memo;
1902
1938
  }
1903
1939
  }
1904
1940
  }
@@ -1920,7 +1956,7 @@ export class SessionManager {
1920
1956
  // 备忘会逐轮变化,所以下面的去重只比对固定部分(CTX_SMART_REMINDER),不受备忘影响。
1921
1957
  // ⚠️ 2026-11-10 修:memoLine **不能拼进 body** —— body 会被"群守则已在上下文里"的去重整体跳过
1922
1958
  // (skipGroupRules),于是小传从此再也注不进去。小传单独成块,自己判自己的重。
1923
- const body = [FIXED_CHANNEL_CONTEXT.trim(), refCtx.trim(), rules.trim(), ctxSmartText].filter(Boolean).join('\n\n');
1959
+ const body = [FIXED_CHANNEL_CONTEXT.trim(), refCtx.trim(), rules.trim()].filter(Boolean).join('\n\n');
1924
1960
  if (!body)
1925
1961
  return decision;
1926
1962
  const text = `<system-reminder>\n${body}\n</system-reminder>`;
@@ -2051,6 +2087,122 @@ export class SessionManager {
2051
2087
  });
2052
2088
  this.logger?.info?.(`[小传] 注入 ${memoLine.length} 字 (session=${recMemo?.sessionKey ?? '?'})`);
2053
2089
  }
2090
+ // ★ CodeRabbit:**智能模式指令独立成块**(`qqbot:ctx-smart`)。
2091
+ // 原来它拼在群守则 body 里、共用 `rulesFp` 判重 ⇒ 群守则没变时(智能模式刚打开、
2092
+ // 或重启后恢复的会话)`skipGroupRules` 会把整条 body 丢掉 ⇒ 话题判断/压缩要求永远缺失。
2093
+ if (ctxSmartOn && ctxSmartText) {
2094
+ const SMART_KIND = 'qqbot:ctx-smart';
2095
+ const smartFp = CTX_SMART_REMINDER.slice(0, 40);
2096
+ // ★ CodeRabbit:判重要看**剔旧之后**的 `_baseMsgs` —— `_stripOldCtxSmart` 已经把本批里的旧智能块删掉了,
2097
+ // 若还拿原始 `dec.messages` 判,就会误以为"本批已有"而不再补注,最终消息里反而没有这条提示。
2098
+ const _smartInBatch = _baseMsgs.some((m) => {
2099
+ const mm = m;
2100
+ return mm?.source?.kind === SMART_KIND && String(mm.content?.[0]?.text ?? '').includes(smartFp);
2101
+ });
2102
+ let _smartInSurface = false;
2103
+ if (!_smartInBatch && agent?.session?.surface?.nodes && agent.session.eventAt) {
2104
+ for (const seq of agent.session.surface.nodes) {
2105
+ const ev = agent.session.eventAt(seq);
2106
+ if (ev?.type !== 'user/message' || ev.data?.source?.kind !== SMART_KIND)
2107
+ continue;
2108
+ const c = ev.data?.content;
2109
+ const txt = typeof c === 'string'
2110
+ ? c
2111
+ : Array.isArray(c)
2112
+ ? c.map((b) => String(b?.text ?? '')).join(String.fromCharCode(10))
2113
+ : '';
2114
+ if (txt.includes(smartFp)) {
2115
+ _smartInSurface = true;
2116
+ break;
2117
+ }
2118
+ }
2119
+ }
2120
+ if (!_smartInBatch && !_smartInSurface) {
2121
+ extraAll.push({
2122
+ role: 'user',
2123
+ id: randomUUID(),
2124
+ content: [{ type: 'text', text: `<system-reminder>` + String.fromCharCode(10) + CTX_SMART_REMINDER + String.fromCharCode(10) + `</system-reminder>` }],
2125
+ source: { kind: SMART_KIND, form: 'instructions', ns },
2126
+ });
2127
+ }
2128
+ }
2129
+ // ★ 2026-11-21(主人定):**自己的备忘独立注入、独立判重**。
2130
+ // 原来和群守则共用一条 body(去重只比群守则指纹)⇒ 群守则不变时,备忘改了也**永远不会重注**,
2131
+ // 模型看到的始终是最老那份。现在按"上下文里最后一条备忘块是否已含当前备忘文本"判重:
2132
+ // 变了就补注一条(旧版留在历史里,新块写明"以此为准")。
2133
+ if (selfMemoText) {
2134
+ const MEMO_KIND = 'qqbot:self-memo';
2135
+ const _memoInBatch = dec.messages.some((m) => {
2136
+ const mm = m;
2137
+ return mm?.source?.kind === MEMO_KIND && String(mm.content?.[0]?.text ?? '').includes(selfMemoText);
2138
+ });
2139
+ let _lastMemo = '';
2140
+ if (!_memoInBatch && agent?.session?.surface?.nodes && agent.session.eventAt) {
2141
+ for (const seq of agent.session.surface.nodes) {
2142
+ const ev = agent.session.eventAt(seq);
2143
+ if (ev?.type !== 'user/message' || ev.data?.source?.kind !== MEMO_KIND)
2144
+ continue;
2145
+ const c = ev.data?.content;
2146
+ _lastMemo = typeof c === 'string'
2147
+ ? c
2148
+ : Array.isArray(c)
2149
+ ? c.map((b) => String(b?.text ?? '')).join(String.fromCharCode(10))
2150
+ : '';
2151
+ }
2152
+ }
2153
+ if (!_memoInBatch && !_lastMemo.includes(selfMemoText)) {
2154
+ extraAll.push({
2155
+ role: 'user',
2156
+ id: randomUUID(),
2157
+ content: [{
2158
+ type: 'text',
2159
+ text: `<system-reminder>`
2160
+ + String.fromCharCode(10)
2161
+ + '〔你自己的备忘 · 最新版(之前若有旧版,一律以这条为准)〕'
2162
+ + String.fromCharCode(10)
2163
+ + selfMemoText
2164
+ + String.fromCharCode(10)
2165
+ + `</system-reminder>`,
2166
+ }],
2167
+ source: { kind: MEMO_KIND, form: 'instructions', ns },
2168
+ });
2169
+ this.logger?.info?.(`[备忘] 注入最新版 ${selfMemoText.length} 字`);
2170
+ }
2171
+ }
2172
+ else if (ctxSmartOn && selfMemoKey && selfMemoOk) {
2173
+ // ★ CodeRabbit:备忘被清空(用户用 context_memo 删光了)⇒ 历史里旧块还写着「以这条为准」,
2174
+ // 模型会继续把已删内容当备忘。这里补注一条「清空」状态;且只在**确实读到空**时补
2175
+ // (未启用 / 读取失败时 selfMemoKey 为空,走不到这里,绝不覆盖有效备忘)。
2176
+ const MEMO_KIND = 'qqbot:self-memo';
2177
+ let _lastMemoEmpty = '';
2178
+ if (agent?.session?.surface?.nodes && agent.session.eventAt) {
2179
+ for (const seq of agent.session.surface.nodes) {
2180
+ const ev = agent.session.eventAt(seq);
2181
+ if (ev?.type !== 'user/message' || ev.data?.source?.kind !== MEMO_KIND)
2182
+ continue;
2183
+ const c = ev.data?.content;
2184
+ _lastMemoEmpty = typeof c === 'string'
2185
+ ? c
2186
+ : Array.isArray(c)
2187
+ ? c.map((b) => String(b?.text ?? '')).join(String.fromCharCode(10))
2188
+ : '';
2189
+ }
2190
+ }
2191
+ if (_lastMemoEmpty && !_lastMemoEmpty.includes('备忘已清空')) {
2192
+ extraAll.push({
2193
+ role: 'user',
2194
+ id: randomUUID(),
2195
+ content: [{
2196
+ type: 'text',
2197
+ text: `<system-reminder>` + String.fromCharCode(10)
2198
+ + '〔你的备忘已清空:上下文里之前列出的备忘条目全部作废,不要再当作当前备忘使用。〕'
2199
+ + String.fromCharCode(10) + `</system-reminder>`,
2200
+ }],
2201
+ source: { kind: MEMO_KIND, form: 'instructions', ns },
2202
+ });
2203
+ this.logger?.info?.('[备忘] 已清空 → 注入作废标记');
2204
+ }
2205
+ }
2054
2206
  // ★ 2026-11-20:**数学公式提示独立成块**(维护手册 §11.39 铁律:自己的 kind + 自己的判重)。
2055
2207
  // 不拼进群守则 body —— 那样开关切换时指纹判断容易漏,而且和群守则耦合。
2056
2208
  // 内容固定(182 字符),用"查有没有就不注"的去重(见下面的两步判断)。
@@ -2084,6 +2236,68 @@ export class SessionManager {
2084
2236
  });
2085
2237
  this.logger?.debug?.('[latexHint] 注入数学公式提示');
2086
2238
  }
2239
+ // ★ 2026-11-21(issue #79):**会话类型标注** —— 独立事件 + 自己的 source.kind。
2240
+ // 私聊与群聊的注入格式几乎同构,模型只能靠猜(实测把私聊判成群聊、按群规矩回复)。
2241
+ // 群名/昵称走官方 API(`session-kind.ts` 里带缓存 + 后台预取,注入本身同步不等网络)。
2242
+ // ⚠️ source.kind 必须带 `qqbot:` 前缀(维护手册 §11.21:否则 dock 会把注入当成对方发言)。
2243
+ //
2244
+ // ★★ 2026-11-21 主人实测「重启后没重新注入」:**不能依赖 `_ownedByMe`/`recMemo`** ——
2245
+ // `findByAgent` 只查 this.sessions,重启/回收后从宿主恢复的 QQ 会话在它眼里是 undefined,
2246
+ // 于是这条注入会被整块跳过(group-rules 那套有同样隐患,但它另有 model-prefs 兜底)。
2247
+ // 这里改用 **sessionKey 反查**(model-prefs 的 sessionIds,纯内存),从 key 里解析 scope/peerId。
2248
+ const _kindKey = (() => {
2249
+ if (recMemo?.sessionKey)
2250
+ return String(recMemo.sessionKey);
2251
+ try {
2252
+ const sidNow = String(agent?.session?.id ?? '');
2253
+ return sidNow ? String(this.modelResolver.findKeyBySessionId(sidNow) ?? '') : '';
2254
+ }
2255
+ catch {
2256
+ return '';
2257
+ }
2258
+ })();
2259
+ const _kindParts = (() => {
2260
+ const m = /^qqbot:[^:]+:(group|c2c):(.+)$/.exec(_kindKey);
2261
+ if (!m)
2262
+ return undefined;
2263
+ const peer = String(m[2] ?? '').replace(/@[0-9a-f-]{8,}$/i, '');
2264
+ return peer ? { scope: String(m[1] ?? 'group'), peerId: peer } : undefined;
2265
+ })();
2266
+ if (this.config.sessionKindHint !== false && _kindParts) {
2267
+ const KIND = 'qqbot:session-kind';
2268
+ const info = describeSessionKind(dataRootOf(this.config), String(this.config.appId ?? ''), _kindParts.scope, _kindParts.peerId, recMemo ? recMemo.peerName : undefined);
2269
+ const _kindInBatch = dec.messages.some((m) => m?.source?.kind === KIND);
2270
+ // ★ 名字是**后来**才从 API 拿到的,首次注入往往是"群名未取到"。
2271
+ // 所以判重不只看"有没有注入过",还要看**有没有注入过带名字的版本**:
2272
+ // 现在拿到名字了就再补一条(之后不再重复)。
2273
+ // ★ CodeRabbit:判重只看**最新一条**会话类型提示。
2274
+ // 名字变来变去(甲 → 乙 → 甲)时,扫任意一条命中会让"最新的乙"继续留在上下文里,
2275
+ // 模型看到的群名就是过期的。所以扫完全部,留下最后一条的文本再比。
2276
+ let _lastHintText = '';
2277
+ if (agent?.session?.surface?.nodes && agent.session.eventAt) {
2278
+ for (const seq of agent.session.surface.nodes) {
2279
+ const ev = agent.session.eventAt(seq);
2280
+ if (ev?.type !== 'user/message' || ev.data?.source?.kind !== KIND)
2281
+ continue;
2282
+ const c = ev.data?.content;
2283
+ _lastHintText = typeof c === 'string'
2284
+ ? c
2285
+ : Array.isArray(c)
2286
+ ? c.map((b) => String(b?.text ?? '')).join(String.fromCharCode(10))
2287
+ : '';
2288
+ }
2289
+ }
2290
+ const need = info.name ? !_lastHintText.includes(info.name) : _lastHintText === '';
2291
+ if (!_kindInBatch && need) {
2292
+ extraAll.push({
2293
+ role: 'user',
2294
+ id: randomUUID(),
2295
+ content: [{ type: 'text', text: `<system-reminder>` + String.fromCharCode(10) + buildSessionKindHint(info) + String.fromCharCode(10) + `</system-reminder>` }],
2296
+ source: { kind: KIND, form: 'instructions', ns },
2297
+ });
2298
+ this.logger?.info?.(`[会话类型] 注入 kind=${info.kind}${info.name ? ` name=「${info.name}」` : ' name=(未取到)'} key=${_kindKey}`);
2299
+ }
2300
+ }
2087
2301
  const entered = [..._baseMsgs.slice(0, _at2), ...(skipGroupRules ? [] : [desired]), ...extraAll, ..._baseMsgs.slice(_at2)];
2088
2302
  return { ...dec, messages: entered };
2089
2303
  }