dsh-qqbot-muu 0.1.20 → 0.1.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.
- package/CHANGELOG.md +168 -154
- package/client/qqbot-settings.js +13401 -13380
- package/dist/config.js +573 -581
- package/dist/features/contextless-store.js +247 -247
- package/dist/features/four-source.js +338 -336
- package/dist/features/group-join-request.js +289 -287
- package/dist/features/local-models.js +1494 -1467
- package/dist/features/style-layer.js +501 -498
- package/dist/features/thinking-log.js +71 -69
- package/dist/gateway/bootstrap.js +1704 -1652
- package/dist/model/settings-reader.js +193 -102
- package/dist/shared/paths.js +128 -0
- package/dist/shared/settings-io.js +79 -83
- package/dshqqbot/.qqbot-extensions/inbound/00-vision.mjs +111 -0
- package/dshqqbot/.qqbot-extensions/inbound/aa-memory-recall.mjs +802 -794
- package/dshqqbot/.qqbot-extensions/inbound/az-feedback.mjs +113 -115
- package/dshqqbot/.qqbot-extensions/inbound/local-answer.mjs +689 -568
- package/dshqqbot/.qqbot-extensions/lib/local-llm.mjs +455 -444
- package/dshqqbot/.qqbot-extensions/lib/memory-agent.mjs +930 -928
- package/dshqqbot/.qqbot-extensions/lib/memory-core.mjs +537 -537
- package/dshqqbot/.qqbot-extensions/lib/memory-db.mjs +2476 -2245
- package/dshqqbot/.qqbot-extensions/lib/memory-hippo.mjs +18 -0
- package/dshqqbot/.qqbot-extensions/lib/memory.mjs +191 -186
- package/dshqqbot/.qqbot-extensions/lib/scope-util.mjs +84 -0
- package/dshqqbot/.qqbot-extensions/outbound/aa-sanitize.mjs +158 -154
- package/dshqqbot/.qqbot-extensions/outbound/memory-agent.mjs +121 -130
- package/dshqqbot/.qqbot-extensions/outbound/turn-memory.mjs +92 -101
- package/dshqqbot/.qqbot-extensions/tools/memory.mjs +178 -120
- package/dshqqbot/.qqbot-extensions/vendor/abogus/test-abogus.mjs +128 -126
- package/package.json +119 -118
- package/scripts/__pycache__/fan-mode.cpython-311.pyc +0 -0
- package/scripts/__pycache__/memory-audit.cpython-311.pyc +0 -0
- package/scripts/__pycache__/qq-ip-sync.cpython-311.pyc +0 -0
- package/scripts/__pycache__/qq-whitelist.cpython-311.pyc +0 -0
- package/scripts/ai-toolkit.py +607 -478
- package/scripts/chat-archive/__pycache__/build-sft-v4.cpython-311.pyc +0 -0
- package/scripts/chat-archive/__pycache__/build-sft.cpython-311.pyc +0 -0
- package/scripts/chat-archive/build-sft-v4.py +566 -0
- package/scripts/chat-archive/build-sft.py +390 -0
- package/scripts/chat-archive/eval-sft.py +155 -0
- package/scripts/chat-archive/extract-memory.py +143 -0
- package/scripts/chat-archive/inventory.py +182 -0
- package/scripts/chat-archive/merge-sft.py +114 -0
- package/scripts/chat-archive/qce-pull.py +217 -0
- package/scripts/chat-archive/qqheader.py +110 -0
- package/scripts/chat-archive/qqkey2.py +113 -0
- package/scripts/chat-archive/qqprobe.py +153 -0
- package/scripts/chat-archive/screen-collect.py +163 -0
- package/scripts/chat-archive/sqlcipher_probe.py +444 -0
- package/scripts/episode.py +529 -0
- package/scripts/fan-curve.py +218 -0
- package/scripts/fan-guard.cmd +8 -0
- package/scripts/fan-mode.py +380 -263
- package/scripts/gen-image.py +420 -413
- package/scripts/gen-video.py +186 -178
- package/scripts/hw-profile.py +175 -0
- package/scripts/media-enhance.py +402 -0
- package/scripts/memory-audit.py +581 -0
- package/scripts/migrate-scope.mjs +140 -139
- package/scripts/qq-connect-login.py +133 -0
- package/scripts/qq-ip-sync-hidden.vbs +5 -0
- package/scripts/qq-ip-sync.py +300 -0
- package/scripts/qq-whitelist.py +71 -0
- package/scripts/release-all.mjs +636 -509
- package/scripts/release-check.mjs +244 -226
- package/scripts/secure-room/index.html +334 -0
- package/scripts/secure-room/server.py +582 -0
- package/scripts/stickers/auto-tag.mjs +161 -0
- package/scripts/train/__pycache__/train-lora.cpython-311.pyc +0 -0
- package/scripts/train/gguf-quantize.py +92 -0
- package/scripts/train/gguf-to-hf.py +112 -0
- package/scripts/train/merge-lora.py +51 -0
- package/scripts/train/probe-mirrors.sh +12 -0
- package/scripts/train/probe-mirrors2.sh +20 -0
- package/scripts/train/setup-wsl.sh +23 -0
- package/scripts/train/setup-wsl2.sh +25 -0
- package/scripts/train/setup-wsl3.sh +22 -0
- package/scripts/train/setup-wsl4.sh +22 -0
- package/scripts/train/train-lora.py +319 -0
- package/dist/_backup_ctx_20261125_183653/dist/config.d.ts +0 -473
- package/dist/_backup_ctx_20261125_183653/dist/config.js +0 -450
- package/dist/_backup_ctx_20261125_183653/dist/features/contextless-trim.js +0 -304
- package/dist/_backup_ctx_20261125_183653/dist/session/session-manager.js +0 -1588
- package/dist/_backup_ctx_20261125_183653/dist/transport/inbound.js +0 -1567
- package/dist/_backup_ctx_20261125_183653/dist/transport/outbound.js +0 -647
- package/scripts/__pycache__/ai-mode.cpython-311.pyc +0 -0
- package/scripts/__pycache__/ai-toolkit.cpython-311.pyc +0 -0
- package/scripts/__pycache__/comfy_gen.cpython-311.pyc +0 -0
- package/scripts/__pycache__/gen-image.cpython-311.pyc +0 -0
- package/scripts/__pycache__/gen-video.cpython-311.pyc +0 -0
- package/scripts/__pycache__/model-registry.cpython-311.pyc +0 -0
- package/scripts/__pycache__/purge-standby.cpython-311.pyc +0 -0
|
@@ -1,304 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* contextless-trim.ts — 「无上下文模式」真正干活的部分(2026-09-27 重写 · 完整四事件版)
|
|
3
|
-
*
|
|
4
|
-
* 需求(主人):开启后该会话**每轮丢掉更早的对话历史**,只留系统规则 + 最近 N 条;
|
|
5
|
-
* web 记录照旧保留(只多一行「上下文已压缩」);**不调 LLM**。
|
|
6
|
-
*
|
|
7
|
-
* ── 走过的两条错路(都记在这里,别再踩)────────────────────────────
|
|
8
|
-
* 错路①:在 `agent/pre-step` 里裁 `decision.messages`。
|
|
9
|
-
* dsh-agent 的 runtime-types 写得很清楚:
|
|
10
|
-
* `@param payload.messages - messages removed from the inbox for this step`
|
|
11
|
-
* `PreStepDecision = { kind:'enter'; messages: UserMessage[] }`
|
|
12
|
-
* 即:pre-step 的 messages 是"本步从收件箱取出的**新消息**",不是"发给模型的完整上下文"。
|
|
13
|
-
* 在那儿裁剪 = 把用户刚发的话删掉(实测:模型只收到 system-reminder → 空转 → abort)。
|
|
14
|
-
* 错路②:只 append 一条带 `surfaceOp: replace` 的 user/message。
|
|
15
|
-
* 模型视野确实变小了,但 token-meter 的计费契约要求有一条**紧邻的**计费事件,
|
|
16
|
-
* 否则压力账本记 0 delta(占用不降、自动压缩反复触发)。
|
|
17
|
-
*
|
|
18
|
-
* ── 正解:照 dsh 自己的 compaction 协议写**四事件** ────────────────
|
|
19
|
-
* ① compaction/start { compactionId, turn }
|
|
20
|
-
* ② compaction/summary { compactionId, summary, shadowedRange, shadowedSeqs, shadowedTokenCount, provider, model }
|
|
21
|
-
* ▲ 必须**紧邻**在下一步之前(中间不能插任何事件)
|
|
22
|
-
* ③ user/message + surfaceOp{op:'replace',startSeq,endSeq} + sourceEventSeqs(含 start/summary/全部被影子节点)
|
|
23
|
-
* ④ compaction/end { compactionId, turn }
|
|
24
|
-
*
|
|
25
|
-
* ── 证据(源码/实测)─────────────────────────────────────────────
|
|
26
|
-
* · dsh-session `types.d.ts:445-462`:replace 是"The node's sourceEventSeqs must include every
|
|
27
|
-
* shadowed surface node",且 "Used by compaction; **any surface-replacing producer may use it**"
|
|
28
|
-
* → 第三方插件用 replace 是官方允许的。
|
|
29
|
-
* · dsh-compaction-basic `:888-902`:官方留了"模板/远端摘要器"的定制钩子
|
|
30
|
-
* → **非 LLM 摘要被明确支持**(类型 `llmStreamCall?: never`)。
|
|
31
|
-
* · dsh-token-meter `surface-projection.js:37-66` 的计费契约:
|
|
32
|
-
* ① summary 必须紧邻 replace;② shadowedRange 必须与 {startSeq,endSeq} 完全一致;
|
|
33
|
-
* ③ shadowedTokenCount 必须是**固定启发式价**之和(用错价会让压力账本永久漂移)。
|
|
34
|
-
* · dsh-session `surface.js:236-269`:sourceEventSeqs 非空、无重复、必须全部更早、
|
|
35
|
-
* 且必须覆盖全部被 shadow 的 surface 节点。
|
|
36
|
-
* · `surface.js:388-397`:node0 若是 system/message,只能被"恰好 1 个 system/message"替换
|
|
37
|
-
* → 故范围一律**从第一个非 system 节点开始**,绝不碰 node0。
|
|
38
|
-
* · `session.append` 会对 data 做 JSON 快照,**任何 `undefined` 属性都会抛错**
|
|
39
|
-
* → provider/model 给空串;可选字段用条件展开。
|
|
40
|
-
*
|
|
41
|
-
* ── web 端行为(主人关心的)───────────────────────────────────
|
|
42
|
-
* surface 是"模型视野",不是人类 transcript(surface.js:39-46 原文注释)。
|
|
43
|
-
* 被 shadow 的原消息**仍留在会话日志**,web 上照样能翻;只多出一行可折叠的
|
|
44
|
-
* 「上下文已压缩 · 约 N tokens」标记。**不存在"把历史从记录里删掉"的机制。**
|
|
45
|
-
*/
|
|
46
|
-
import { randomUUID } from 'node:crypto';
|
|
47
|
-
import { traceContextless } from './contextless-store.js';
|
|
48
|
-
/** 参与"保留最近 N 条"计数的消息型事件(与 dsh 的 SurfaceEventType 对齐) */
|
|
49
|
-
const MESSAGE_TYPES = new Set(['user/message', 'assistant/message', 'system/message', 'developer/message']);
|
|
50
|
-
/** 替身文本(摘要器输出;web 的「上下文已压缩」折叠块显示它) */
|
|
51
|
-
const FIXED_SUMMARY_TEXT = '(更早的对话已按「无上下文模式」省略,仅保留最近若干条)';
|
|
52
|
-
/**
|
|
53
|
-
* 幂等标记(2026-09-27 修):记住每个 session **已压到哪个 seq**。
|
|
54
|
-
*
|
|
55
|
-
* ⚠️ 原实现用 replaceGeneration,被群聊 AI 抓到是 bug:
|
|
56
|
-
* replaceGeneration 在"压过一次"后就不变,于是"跳过: 同一 replaceGeneration 已压缩过"
|
|
57
|
-
* 会在之后每一轮都命中 → **新攒的历史被永久跳过**(实测 22:44:32 起全部跳过)。
|
|
58
|
-
* 改为按 seq 记账:只要本次要压的区间末尾 seq 比"上次压到的"更靠后,就允许再压。
|
|
59
|
-
*/
|
|
60
|
-
const lastTrimmedEndSeq = new WeakMap();
|
|
61
|
-
/** 双保险:记住每个 session 上一次压缩的 turn,同一 turn 内不再重复压(step 缺失时兜底) */
|
|
62
|
-
const lastTrimmedTurn = new WeakMap();
|
|
63
|
-
function newId() {
|
|
64
|
-
try {
|
|
65
|
-
return randomUUID();
|
|
66
|
-
}
|
|
67
|
-
catch { /* fallthrough */ }
|
|
68
|
-
return 'ctxless-' + Date.now().toString(36) + '-' + Math.floor(Math.random() * 1e9).toString(36);
|
|
69
|
-
}
|
|
70
|
-
/** 拿不到宿主 tokenMeter 时的粗估:≈ 字符数 / 4 */
|
|
71
|
-
function roughTokens(value) {
|
|
72
|
-
try {
|
|
73
|
-
if (value === null || value === undefined)
|
|
74
|
-
return 0;
|
|
75
|
-
const text = typeof value === 'string' ? value : JSON.stringify(value);
|
|
76
|
-
return Math.max(1, Math.ceil((text?.length ?? 0) / 4));
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
return 0;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/** tool 相关事件(回退边界用,避免切断 tool-call/tool-result 配对) */
|
|
83
|
-
function isToolish(ev) {
|
|
84
|
-
const t = String(ev?.type ?? '');
|
|
85
|
-
return t === 'tool/result' || t === 'tool/call';
|
|
86
|
-
}
|
|
87
|
-
/** 从事件里读消息体(用于估价) */
|
|
88
|
-
function messageOf(sess, seq) {
|
|
89
|
-
const ev = sess.eventAt?.(seq);
|
|
90
|
-
try {
|
|
91
|
-
if (typeof sess.deriveEventMessage === 'function')
|
|
92
|
-
return sess.deriveEventMessage(ev) ?? ev;
|
|
93
|
-
}
|
|
94
|
-
catch { /* ignore */ }
|
|
95
|
-
return ev;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* 把「最近 keepRecent 条消息」之前的历史整段替换成一句固定文本(四事件协议)。
|
|
99
|
-
*
|
|
100
|
-
* @param agent 目标 agent(取 agent.session)
|
|
101
|
-
* @param keepRecent 保留最近多少条消息(面板里的"带 @ 前 N 条";<=0 表示全清只留替身)
|
|
102
|
-
* @param logger 可选日志
|
|
103
|
-
* @param ctx 可选宿主上下文(用于拿 tokenMeter 估价)
|
|
104
|
-
* @returns 本次是否真的替换了
|
|
105
|
-
*/
|
|
106
|
-
export async function trimHistoryForContextless(agent, keepRecent, logger, ctx,
|
|
107
|
-
/** 当前打开的轮次号(来自 agent/pre-step 的 payload.turn)。
|
|
108
|
-
* ⚠️ 绝不能传 undefined/null:我们在 pre-step 里执行,回合是开着的,
|
|
109
|
-
* 而 compaction/start、compaction/end 的 turn 必须与"开着的那个 turn"一致,
|
|
110
|
-
* 否则会话日志会以 SessionFormatError 写坏(实测把两个群的会话都写崩了)。 */
|
|
111
|
-
turn) {
|
|
112
|
-
const sess = agent?.session;
|
|
113
|
-
traceContextless('trim 被调用: keep=' + keepRecent + ' turn=' + String(turn) + ' agent=' + (agent ? 'yes' : 'no') + ' session=' + (sess ? 'yes' : 'no'));
|
|
114
|
-
let started = false; // 是否已 append compaction/start(失败时要补 end)
|
|
115
|
-
let compactionId = '';
|
|
116
|
-
const log = logger ?? ctx?.logger;
|
|
117
|
-
try {
|
|
118
|
-
const nodes = sess?.surface?.nodes;
|
|
119
|
-
if (!Array.isArray(nodes) || nodes.length === 0) {
|
|
120
|
-
log?.debug?.('[contextless] 跳过: 会话无 surface.nodes');
|
|
121
|
-
traceContextless('跳过: 无 surface.nodes');
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
if (typeof sess?.eventAt !== 'function' || typeof sess?.append !== 'function') {
|
|
125
|
-
log?.debug?.('[contextless] 跳过: session 缺 eventAt/append');
|
|
126
|
-
traceContextless('跳过: 缺 eventAt/append');
|
|
127
|
-
return false;
|
|
128
|
-
}
|
|
129
|
-
const keep = Math.max(0, Math.trunc(Number(keepRecent)) || 0);
|
|
130
|
-
// ① node0 若是 system/message 则永不纳入范围(surface.js:388 的保护)
|
|
131
|
-
const headSeq = nodes[0];
|
|
132
|
-
const headEv = headSeq === undefined ? undefined : sess.eventAt(headSeq);
|
|
133
|
-
// ⚠️⚠️ 2026-09-27 死保护:**绝不从 node0 开始**。
|
|
134
|
-
// dsh 的 surface node0 是"受保护的第一节点"(通常是 system/message):
|
|
135
|
-
// surface.js:388 assertSystemHeadRewrite 规定它只能被"恰好一个 system/message"替换。
|
|
136
|
-
// 人家原来写成 `headEv?.type === 'system/message' ? 1 : 0` —— 只要 node0 不是 system
|
|
137
|
-
// (例如它已经是上一次压缩的 checkpoint),firstIdx 就退化成 0,于是一次 replace 把
|
|
138
|
-
// 受保护 head 换掉 → 会话日志变成
|
|
139
|
-
// SessionFormatError: system/message requires a protected first surface head
|
|
140
|
-
// 实测把"月饼群"的会话写坏 5 次。现在无条件从 1 起,宁可少压一个节点,也绝不碰 node0。
|
|
141
|
-
const _headIsSystem = headEv?.type === 'system/message';
|
|
142
|
-
const firstIdx = 1;
|
|
143
|
-
if (!_headIsSystem && headSeq !== undefined) {
|
|
144
|
-
log?.debug?.('[contextless] 注意: node0 不是 system/message(而是 ' + String(headEv?.type) + '),仍从 1 起以保护 head');
|
|
145
|
-
}
|
|
146
|
-
// ② 定位保留段起点(从尾往前数最近 keep 条对话消息)
|
|
147
|
-
let keepIdxInNodes;
|
|
148
|
-
if (keep <= 0) {
|
|
149
|
-
if (nodes.length <= firstIdx + 1) {
|
|
150
|
-
log?.debug?.('[contextless] 跳过: 节点数不足(' + nodes.length + ')');
|
|
151
|
-
traceContextless('跳过: 节点数不足 ' + nodes.length);
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
keepIdxInNodes = nodes.length; // 全清(只留替身)
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
const msgIdx = [];
|
|
158
|
-
for (let i = firstIdx; i < nodes.length; i++) {
|
|
159
|
-
const seqI = nodes[i];
|
|
160
|
-
if (seqI === undefined)
|
|
161
|
-
continue;
|
|
162
|
-
const ev = sess.eventAt(seqI);
|
|
163
|
-
if (ev && MESSAGE_TYPES.has(String(ev.type)))
|
|
164
|
-
msgIdx.push(i);
|
|
165
|
-
}
|
|
166
|
-
if (msgIdx.length <= keep + 1) {
|
|
167
|
-
log?.debug?.('[contextless] 跳过: 消息数 ' + msgIdx.length + ' <= 保留 ' + keep + '+1');
|
|
168
|
-
traceContextless('跳过: 消息数 ' + msgIdx.length + ' <= 保留 ' + keep + '+1');
|
|
169
|
-
return false;
|
|
170
|
-
}
|
|
171
|
-
const keepFromRaw = msgIdx[msgIdx.length - keep];
|
|
172
|
-
if (keepFromRaw === undefined)
|
|
173
|
-
return false;
|
|
174
|
-
keepIdxInNodes = keepFromRaw;
|
|
175
|
-
}
|
|
176
|
-
// ③ 回退到 tool 配对平衡的边界(surface 层不查,但 provider 会因孤立 tool 调用报错)
|
|
177
|
-
while (keepIdxInNodes > firstIdx + 1) {
|
|
178
|
-
const seqAt = nodes[keepIdxInNodes];
|
|
179
|
-
if (seqAt === undefined || !isToolish(sess.eventAt(seqAt)))
|
|
180
|
-
break;
|
|
181
|
-
keepIdxInNodes -= 1;
|
|
182
|
-
}
|
|
183
|
-
if (keepIdxInNodes <= firstIdx) {
|
|
184
|
-
log?.debug?.('[contextless] 跳过: 保留段起点已到头部');
|
|
185
|
-
traceContextless('跳过: 保留段起点已到头部');
|
|
186
|
-
return false;
|
|
187
|
-
}
|
|
188
|
-
const startRaw = nodes[firstIdx];
|
|
189
|
-
const endRaw = nodes[keepIdxInNodes - 1];
|
|
190
|
-
if (startRaw === undefined || endRaw === undefined)
|
|
191
|
-
return false;
|
|
192
|
-
const start = startRaw;
|
|
193
|
-
const end = endRaw;
|
|
194
|
-
const shadowedSeqs = nodes.slice(firstIdx, keepIdxInNodes);
|
|
195
|
-
if (shadowedSeqs.length === 0)
|
|
196
|
-
return false;
|
|
197
|
-
// ③.5 双保险:同一 turn 只压一次
|
|
198
|
-
if (typeof turn === 'number') {
|
|
199
|
-
const prevTurn = lastTrimmedTurn.get(sess);
|
|
200
|
-
if (typeof prevTurn === 'number' && prevTurn === turn) {
|
|
201
|
-
traceContextless('跳过: 同一 turn(' + turn + ') 已压缩过');
|
|
202
|
-
return false;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
// ④ 幂等(按 seq):本次要压到的 end 不晚于"上次压到的"→ 说明没有新历史 → 跳过。
|
|
206
|
-
// 有新消息时 end 会变大 → 允许再压(这正是原 replaceGeneration 写法漏掉的情况)。
|
|
207
|
-
const trimmedTo = lastTrimmedEndSeq.get(sess);
|
|
208
|
-
if (typeof trimmedTo === 'number' && end <= trimmedTo) {
|
|
209
|
-
traceContextless('跳过: 已压到 seq ' + trimmedTo + ',本次 end=' + end + ' 没有更新的历史');
|
|
210
|
-
return false;
|
|
211
|
-
}
|
|
212
|
-
// ⑤ shadowedTokenCount = 固定启发式价之和(契约要求;用错价会让压力账本漂移)
|
|
213
|
-
// ⚠️ 2026-09-27 修:插件的 ctx 未必 inject 了 tokenMeter,直接访问会抛
|
|
214
|
-
// "cannot get property \"tokenMeter\" without inject"(实测把整轮清理打挂)。
|
|
215
|
-
// 所以整段包 try,拿不到就退回字符估算 —— 宁可价不准,也不能不干活。
|
|
216
|
-
let estimateFn;
|
|
217
|
-
try {
|
|
218
|
-
const tm = ctx?.tokenMeter;
|
|
219
|
-
if (tm && typeof tm.estimateMessage === 'function')
|
|
220
|
-
estimateFn = tm.estimateMessage.bind(tm);
|
|
221
|
-
}
|
|
222
|
-
catch {
|
|
223
|
-
estimateFn = undefined;
|
|
224
|
-
}
|
|
225
|
-
let shadowedTokenCount = 0;
|
|
226
|
-
for (const seq of shadowedSeqs) {
|
|
227
|
-
const msg = messageOf(sess, seq);
|
|
228
|
-
let got = NaN;
|
|
229
|
-
if (estimateFn) {
|
|
230
|
-
try {
|
|
231
|
-
got = Number(estimateFn(msg));
|
|
232
|
-
}
|
|
233
|
-
catch {
|
|
234
|
-
got = NaN;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
shadowedTokenCount += Number.isFinite(got) && got > 0 ? Math.round(got) : roughTokens(msg);
|
|
238
|
-
}
|
|
239
|
-
if (!Number.isFinite(shadowedTokenCount) || shadowedTokenCount < 0)
|
|
240
|
-
shadowedTokenCount = 0;
|
|
241
|
-
compactionId = newId();
|
|
242
|
-
// ⑥ ① 开括号(standalone:turn 为 null —— 本函数只在回合空闲后运行)
|
|
243
|
-
const startEvent = sess.append('compaction/start', {
|
|
244
|
-
compactionId,
|
|
245
|
-
turn: typeof turn === 'number' ? turn : null,
|
|
246
|
-
});
|
|
247
|
-
started = true;
|
|
248
|
-
// ⑦ ② 计费事件
|
|
249
|
-
const summaryEvent = sess.append('compaction/summary', {
|
|
250
|
-
compactionId,
|
|
251
|
-
summary: [{ type: 'text', text: FIXED_SUMMARY_TEXT }],
|
|
252
|
-
shadowedRange: { start, end },
|
|
253
|
-
shadowedSeqs: [...shadowedSeqs],
|
|
254
|
-
shadowedTokenCount,
|
|
255
|
-
provider: '', // 类型必填 string;绝不能是 undefined(append 的 JSON 快照会抛)
|
|
256
|
-
model: '',
|
|
257
|
-
});
|
|
258
|
-
// ⚠️ ⑧ ③ 替换(必须**紧邻** summary,中间不能插任何 append)
|
|
259
|
-
const sources = [startEvent?.seq, summaryEvent?.seq, ...shadowedSeqs]
|
|
260
|
-
.filter((s) => typeof s === 'number' && Number.isFinite(s));
|
|
261
|
-
sess.append('user/message', {
|
|
262
|
-
id: newId(),
|
|
263
|
-
role: 'user',
|
|
264
|
-
content: [{ type: 'text', text: FIXED_SUMMARY_TEXT }],
|
|
265
|
-
source: { kind: 'compact-checkpoint', compactionId },
|
|
266
|
-
}, {
|
|
267
|
-
surfaceOp: { op: 'replace', startSeq: start, endSeq: end },
|
|
268
|
-
sourceEventSeqs: sources,
|
|
269
|
-
});
|
|
270
|
-
// ⑨ ④ 闭括号
|
|
271
|
-
sess.append('compaction/end', { compactionId, turn: typeof turn === 'number' ? turn : null });
|
|
272
|
-
started = false;
|
|
273
|
-
lastTrimmedEndSeq.set(sess, end); // 记账:已压到这个 seq
|
|
274
|
-
if (typeof turn === 'number')
|
|
275
|
-
lastTrimmedTurn.set(sess, turn);
|
|
276
|
-
traceContextless('✅ 成功: 替换 seq ' + start + '..' + end + '(' + shadowedSeqs.length + ' 节点 / ' + shadowedTokenCount + ' tokens),保留 ' + keep);
|
|
277
|
-
log?.info?.('[contextless] 已省略历史: 替换 seq ' + start + '..' + end
|
|
278
|
-
+ '(' + shadowedSeqs.length + ' 个节点 / 约 ' + shadowedTokenCount + ' tokens)→ 保留最近 ' + keep + ' 条消息');
|
|
279
|
-
return true;
|
|
280
|
-
}
|
|
281
|
-
catch (err) {
|
|
282
|
-
// 失败必须补一条 compaction/end(带 error),否则该会话后续压缩会被 compaction-basic 判 busy 挡死
|
|
283
|
-
if (started && compactionId && typeof sess?.append === 'function') {
|
|
284
|
-
try {
|
|
285
|
-
sess.append('compaction/end', { compactionId, turn: typeof turn === 'number' ? turn : null, error: { message: err instanceof Error ? err.message : String(err) } });
|
|
286
|
-
}
|
|
287
|
-
catch { /* ignore */ }
|
|
288
|
-
}
|
|
289
|
-
traceContextless('❌ 失败: ' + (err instanceof Error ? (err.message + '\n' + (err.stack ?? '')) : String(err)));
|
|
290
|
-
log?.debug?.('[contextless] 省略历史失败(已忽略): ' + (err instanceof Error ? err.message : String(err)));
|
|
291
|
-
return false;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* 回合空闲后执行清理(插件硬约束:回合中 append 会坏记录 —— inject.ts 的既有约定)。
|
|
296
|
-
* fire-and-forget,不阻塞调用方。
|
|
297
|
-
*/
|
|
298
|
-
export function scheduleContextlessTrim(agent, keepRecent, logger, ctx) {
|
|
299
|
-
const wait = typeof agent?.whenIdle === 'function'
|
|
300
|
-
? agent.whenIdle().catch(() => undefined)
|
|
301
|
-
: Promise.resolve();
|
|
302
|
-
void wait.then(() => trimHistoryForContextless(agent, keepRecent, logger, ctx)).catch(() => undefined);
|
|
303
|
-
}
|
|
304
|
-
//# sourceMappingURL=contextless-trim.js.map
|