@240xu/dsh-message-ops 0.9.6 → 0.10.2
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/README.md +103 -0
- package/package.json +1 -2
- package/src/client.js +336 -151
- package/src/index.js +149 -55
- package/src/ops-core.js +12 -41
- package/src/session-file.js +55 -8
- package/test/client-p0-locks.test.js +131 -0
- package/test/client-perf-locks.test.js +259 -0
- package/test/derived-cache.test.js +202 -0
- package/test/revert-commit.test.js +98 -0
- package/test/transcript-visibility.test.js +41 -2
- package/test/undo-slash.test.js +28 -0
package/src/client.js
CHANGED
|
@@ -1,16 +1,8 @@
|
|
|
1
1
|
/* global window, document, fetch, navigator */
|
|
2
2
|
/**
|
|
3
3
|
* dsh-message-ops 客户端:会话头部按钮 + 共享操作对话框 + 侧栏行菜单项。
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* - 拉取 GET /api/message-ops/messages 展示消息级列表(可见性已标注)
|
|
7
|
-
* - 回滚/删除为破坏性操作:需勾选风险确认(回滚仍可通过再次发送恢复,
|
|
8
|
-
* 删除为遮蔽语义;日志永远 append-only,原事件保留)
|
|
9
|
-
* - 分支为唯一非破坏操作:无需确认,成功后提示新会话 id 并刷新列表
|
|
10
|
-
* 回滚/删除成功后整页刷新(surface 变化需要会话重渲染)。
|
|
11
|
-
*
|
|
12
|
-
* 结构照抄 @huanlin/dsh-plugin-session-delete 的成熟模式:slots.inject
|
|
13
|
-
* 延迟注册(宿主晚声明也不白屏)、locale 服务 zh/en、DOM 注入侧栏菜单。
|
|
4
|
+
* 一个对话框承载回滚 / 删除 / 分支三种操作;日志 append-only,遮蔽可恢复。
|
|
5
|
+
* 结构照抄 @huanlin/dsh-plugin-session-delete。事故复盘见 README「历史事故记录」#1。
|
|
14
6
|
*/
|
|
15
7
|
window.__ModuleLoader__.load({
|
|
16
8
|
id: '@240xu/dsh-message-ops',
|
|
@@ -28,9 +20,58 @@ window.__ModuleLoader__.load({
|
|
|
28
20
|
const EVENT = 'dsh-message-ops:open'
|
|
29
21
|
// 0.5.2(P2):回滚/恢复成功后广播 → RevertDock 重拉标记(否则 dock 只在切会话时刷新)
|
|
30
22
|
const CHANGED_EVENT = 'dsh-message-ops:changed'
|
|
31
|
-
|
|
23
|
+
// 0.9.7(P4):messageId→seq 索引失效(dataSig 变化)—— 已挂载的
|
|
24
|
+
// MsgSlotActions 靠它重跑解析 effect,否则「新 AI 消息旁没有按钮」。
|
|
25
|
+
const SEQ_INDEX_EVENT = 'dsh-message-ops:seq-index'
|
|
26
|
+
// 0.9.7(P0-2 读合并):一次写操作会同时触发 150/700/1600ms 三连 +
|
|
27
|
+
// RevertDock 的 CHANGED 重拉 + openSession 后 MO 调度的 scan(5~6 次 GET)。
|
|
28
|
+
// 同一会话在此窗口内的请求共享同一个 promise;窗口**必须 < 1600ms**,
|
|
29
|
+
// 否则「视图重建后重算隐藏」的 1600ms 兜底会被上一次响应吞掉。
|
|
30
|
+
const MSG_COALESCE_MS = 1000
|
|
31
|
+
var __messagesMemo = { url: null, at: 0, p: null, settled: false }
|
|
32
|
+
var invalidateMessagesMemo = () => { __messagesMemo = { url: null, at: 0, p: null, settled: false } }
|
|
33
|
+
const emitChanged = () => {
|
|
34
|
+
// 数据变了:连同本地缓存一起作废(否则 8s TTL 会让 openSession 后的
|
|
35
|
+
// scan 拿旧 payload 重算隐藏 = 「回滚了但界面没变」)。
|
|
36
|
+
invalidateMessagesMemo()
|
|
37
|
+
if (__peekRowsCache) { const c = __peekRowsCache(); if (c) c.at = 0 }
|
|
38
|
+
try { window.dispatchEvent(new CustomEvent(CHANGED_EVENT)) } catch { /* noop */ }
|
|
39
|
+
}
|
|
32
40
|
const NS = 'dsh-message-ops'
|
|
33
41
|
|
|
42
|
+
/**
|
|
43
|
+
* GET /api/message-ops/messages 的唯一出口(0.9.7 读合并)。
|
|
44
|
+
* - 并发调用共享同一个 promise;
|
|
45
|
+
* - 已结算的响应在 MSG_COALESCE_MS 内复用(一次写操作的 5~6 路重拉 → 1~2 次网络);
|
|
46
|
+
* - 只在 `ok:true` 时才留窗口:失败/非 2xx 立刻作废,免得把后续重试也吃掉;
|
|
47
|
+
* - 1600ms 那档兜底刷新由调用方先 `invalidateMessagesMemo()` 再拉,保证是真网络。
|
|
48
|
+
*/
|
|
49
|
+
var fetchMessagesJson = (rawUrl) => {
|
|
50
|
+
// 归一 sessionId 写法:本文件有的调用方传 `session-xxx`(dock / 对话框),
|
|
51
|
+
// 有的传裸 uuid(fetchRows / /redo)。不归一就是两个缓存槽,合并直接失效。
|
|
52
|
+
// 服务端两种写法都认(index.js "uuid or session-<uuid> form")。
|
|
53
|
+
const url = String(rawUrl).replace(/([?&]sessionId=)session-/, '$1')
|
|
54
|
+
const m = __messagesMemo
|
|
55
|
+
if (m.url === url && m.p && (!m.settled || Date.now() - m.at < MSG_COALESCE_MS)) return m.p
|
|
56
|
+
const entry = { url, at: Date.now(), p: null, settled: false }
|
|
57
|
+
entry.p = fetch(url)
|
|
58
|
+
.then((r) => r.json().catch(() => null).then((d) => ({ r, d })))
|
|
59
|
+
.then(({ r, d }) => {
|
|
60
|
+
if (!r.ok || !d || d.ok !== true) throw new Error((d && d.error) || ('HTTP ' + r.status))
|
|
61
|
+
entry.settled = true
|
|
62
|
+
entry.at = Date.now()
|
|
63
|
+
return d
|
|
64
|
+
})
|
|
65
|
+
.catch((e) => { if (__messagesMemo === entry) invalidateMessagesMemo(); throw e })
|
|
66
|
+
__messagesMemo = entry
|
|
67
|
+
return entry.p
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 0.9.7(P0-4):zh/en 两本字典 key 集合**必须一致且顺序一致**——宿主 locale
|
|
71
|
+
// 的 translate() 按「活跃语言 → en」回退链查表(zh 回退 en,en 无回退),
|
|
72
|
+
// 所以缺 key 不一定看得见:zh 缺 → 落到 en(0.9.x 这批 en 值是中文,
|
|
73
|
+
// zh 用户"碰巧"正常);en 缺 → 直接把 key 当文案显示。
|
|
74
|
+
// 反过来 en 值写成中文时,英文用户会看到中文。测试锁死了两侧 key 序列相等。
|
|
34
75
|
const zhDict = {
|
|
35
76
|
'button.title': '消息操作(回滚/删除/分支)',
|
|
36
77
|
'dialog.title': '消息操作',
|
|
@@ -42,7 +83,7 @@ window.__ModuleLoader__.load({
|
|
|
42
83
|
'dialog.runningWarn': '⚠ 会话正在运行:请先停止该会话再执行回滚/删除/分支。',
|
|
43
84
|
'dialog.pick': '选择一条消息:',
|
|
44
85
|
'dialog.pickFirst': '第 1 步 · 在下方选择一条消息,第 2 步在这里选择操作。',
|
|
45
|
-
'dialog.pickedAs': '已选 #{seq} · {
|
|
86
|
+
'dialog.pickedAs': '已选 #{seq} · {text}',
|
|
46
87
|
'dialog.invisible': '(已遮蔽,仅日志可见)',
|
|
47
88
|
'dialog.showMore': '显示更多(剩余 {n} 条)',
|
|
48
89
|
'dialog.more': '…(仅显示最近 200 条,共 {n} 条)',
|
|
@@ -52,6 +93,11 @@ window.__ModuleLoader__.load({
|
|
|
52
93
|
'op.deleteDesc': '仅遮蔽所选的一条消息;其余内容保持不变。',
|
|
53
94
|
'op.branch': '从此条分支为新会话',
|
|
54
95
|
'op.branchDesc': '复制所选消息(含)之前的全部事件为新会话(原会话不变,非破坏操作)。',
|
|
96
|
+
'slot.revert': '回滚到此条',
|
|
97
|
+
'slot.revertUnmatched': '在日志里定位不到这条消息的 seq(通常是模型写的摘要与渲染文本偏差过大)——请改用「消息操作」对话框。',
|
|
98
|
+
'toast.openNew': '打开新会话',
|
|
99
|
+
'fork.official': '分支完成:新会话 {id}',
|
|
100
|
+
'fork.unavailable': '官方分支不可用:通常是因为会话正在运行,请先停止会话后重试。分支功能由 dsh 内置的 session/fork 提供。',
|
|
55
101
|
'ack.revert': '我已了解:回滚会遮蔽所选消息及其后的全部内容',
|
|
56
102
|
'ack.delete': '我已了解:将遮蔽这一条消息(日志中仍保留)',
|
|
57
103
|
'confirm.revert': '回滚',
|
|
@@ -60,27 +106,29 @@ window.__ModuleLoader__.load({
|
|
|
60
106
|
'busy.revert': '回滚中…',
|
|
61
107
|
'busy.delete': '删除中…',
|
|
62
108
|
'busy.branch': '分支中…',
|
|
63
|
-
'done.
|
|
109
|
+
'done.restore': '恢复完成:重放 {n} 条,跳过不可重放 {s} 条',
|
|
64
110
|
'done.revert': '回滚完成,刷新页面后生效',
|
|
65
111
|
'done.delete': '删除完成,刷新页面后生效',
|
|
66
112
|
'action.reload': '刷新页面',
|
|
67
113
|
'errorPrefix': '操作失败:',
|
|
68
|
-
'slot.revert': '回滚到此条',
|
|
69
|
-
'slot.delete': '删除此条',
|
|
70
|
-
'slot.branch': '从此分支',
|
|
71
|
-
'toast.openNew': '打开新会话',
|
|
72
|
-
'fork.official': '分支完成:新会话 {id}',
|
|
73
|
-
'fork.unavailable': '官方分支不可用:通常是因为会话正在运行,请先停止会话后重试。分支功能由 dsh 内置的 session/fork 提供。',
|
|
74
|
-
'done.restore': '恢复完成:重放 {n} 条,跳过不可重放 {s} 条',
|
|
75
|
-
'busy.restore': '恢复中…',
|
|
76
114
|
'dock.title': '已回撤 {n} 条消息',
|
|
77
115
|
'dock.expand': '展开回撤列表',
|
|
78
116
|
'dock.collapse': '折叠回撤列表',
|
|
117
|
+
'dock.shadowedN': '{n} 条已遮蔽',
|
|
79
118
|
'dock.restore': '恢复',
|
|
119
|
+
'dock.restoreHint': '恢复为重放:消息将出现在会话末尾,不会回到原位',
|
|
80
120
|
'dock.restoring': '恢复中…',
|
|
81
121
|
'slot.quote': '引用到输入框',
|
|
82
122
|
'quote.done': '已引用到输入框',
|
|
83
123
|
'quote.unavailable': '引用不可用(输入框不可写或无全文)',
|
|
124
|
+
'undo.label': '回滚上一条消息',
|
|
125
|
+
'undo.desc': '移除最后一条用户消息,原文回填输入框',
|
|
126
|
+
'undo.done.undo': '已回滚上一条消息',
|
|
127
|
+
'undo.done.redo': '已恢复上一条被回滚的消息',
|
|
128
|
+
'undo.none': '没有可回滚的消息',
|
|
129
|
+
'undo.none.redo': '没有可恢复的回滚',
|
|
130
|
+
'redo.label': '恢复上一条被回滚的消息',
|
|
131
|
+
'redo.desc': '把回滚掉的消息重新放回对话',
|
|
84
132
|
'menu.ops': '消息操作',
|
|
85
133
|
}
|
|
86
134
|
|
|
@@ -95,7 +143,7 @@ window.__ModuleLoader__.load({
|
|
|
95
143
|
'dialog.runningWarn': '⚠ Session is running: stop it before revert / delete / branch.',
|
|
96
144
|
'dialog.pick': 'Pick a message:',
|
|
97
145
|
'dialog.pickFirst': 'Step 1 · pick a message below, then choose an operation here.',
|
|
98
|
-
'dialog.pickedAs': 'Selected
|
|
146
|
+
'dialog.pickedAs': 'Selected #{seq} · {text}',
|
|
99
147
|
'dialog.invisible': ' (shadowed, log-only)',
|
|
100
148
|
'dialog.showMore': 'Show more ({n} older)',
|
|
101
149
|
'dialog.more': '…(showing latest 200 of {n})',
|
|
@@ -107,8 +155,6 @@ window.__ModuleLoader__.load({
|
|
|
107
155
|
'op.branchDesc': 'Copies everything up to and including the picked message into a new session (original untouched, non-destructive).',
|
|
108
156
|
'slot.revert': 'Revert to here',
|
|
109
157
|
'slot.revertUnmatched': 'Cannot locate this message seq in the log (usually the model-written summary drifted too far from the rendered text) — use the Message ops dialog instead.',
|
|
110
|
-
'slot.delete': 'Delete this message',
|
|
111
|
-
'slot.branch': 'Branch from here',
|
|
112
158
|
'toast.openNew': 'Open new session',
|
|
113
159
|
'fork.official': 'Branched (official fork): new session {id}',
|
|
114
160
|
'fork.unavailable': 'Built-in branch unavailable — usually because the session is running. Stop it and retry. Branching is provided by the dsh built-in session/fork.',
|
|
@@ -120,9 +166,7 @@ window.__ModuleLoader__.load({
|
|
|
120
166
|
'busy.revert': 'Reverting…',
|
|
121
167
|
'busy.delete': 'Deleting…',
|
|
122
168
|
'busy.branch': 'Branching…',
|
|
123
|
-
'done.branch': 'Branched: new session {id} (visible after list refresh)',
|
|
124
169
|
'done.restore': 'Restored: replayed {n}, skipped non-replayable {s}',
|
|
125
|
-
'busy.restore': 'Restoring…',
|
|
126
170
|
'done.revert': 'Reverted; reload to apply',
|
|
127
171
|
'done.delete': 'Deleted; reload to apply',
|
|
128
172
|
'action.reload': 'Reload page',
|
|
@@ -132,18 +176,19 @@ window.__ModuleLoader__.load({
|
|
|
132
176
|
'dock.collapse': 'Collapse revert list',
|
|
133
177
|
'dock.shadowedN': '{n} shadowed',
|
|
134
178
|
'dock.restore': 'Restore',
|
|
179
|
+
'dock.restoreHint': 'Restores by replay: messages appear at the end of the session, not in their original place',
|
|
135
180
|
'dock.restoring': 'Restoring…',
|
|
136
181
|
'slot.quote': 'Quote to composer',
|
|
137
182
|
'quote.done': 'Quoted into composer',
|
|
138
183
|
'quote.unavailable': 'Quote unavailable (composer locked or no full text)',
|
|
139
|
-
'undo.label': '
|
|
140
|
-
'undo.desc': '
|
|
141
|
-
'undo.done.undo': '
|
|
142
|
-
'undo.done.redo': '
|
|
143
|
-
'undo.none': '
|
|
144
|
-
'undo.none.redo': '
|
|
145
|
-
'redo.label': '
|
|
146
|
-
'redo.desc': '
|
|
184
|
+
'undo.label': 'Undo last message',
|
|
185
|
+
'undo.desc': 'Remove the last user message and put its original text back into the composer',
|
|
186
|
+
'undo.done.undo': 'Undid the last message',
|
|
187
|
+
'undo.done.redo': 'Restored the last reverted message',
|
|
188
|
+
'undo.none': 'No message to undo',
|
|
189
|
+
'undo.none.redo': 'No revert to redo',
|
|
190
|
+
'redo.label': 'Restore the last reverted message',
|
|
191
|
+
'redo.desc': 'Put the reverted message back into the conversation',
|
|
147
192
|
'menu.ops': 'Message ops',
|
|
148
193
|
}
|
|
149
194
|
|
|
@@ -303,8 +348,7 @@ window.__ModuleLoader__.load({
|
|
|
303
348
|
useEffect(() => {
|
|
304
349
|
if (state !== 'loading' || !target || !target.sessionId) return
|
|
305
350
|
let cancelled = false
|
|
306
|
-
|
|
307
|
-
.then((r) => r.json())
|
|
351
|
+
fetchMessagesJson('/api/message-ops/messages?sessionId=' + encodeURIComponent(target.sessionId))
|
|
308
352
|
.then((data) => {
|
|
309
353
|
if (cancelled) return
|
|
310
354
|
if (!data || !data.ok) throw new Error(data && data.error ? data.error : `HTTP ${data && data.status}`)
|
|
@@ -343,11 +387,8 @@ window.__ModuleLoader__.load({
|
|
|
343
387
|
setBusyMsg(t('busy.' + (mode === 'revert' || mode === 'delete' ? mode : 'branch')))
|
|
344
388
|
setState('busy'); setError(null)
|
|
345
389
|
if (mode === 'branch') {
|
|
346
|
-
// 0.9.0:分支**只走官方** sessions.fork({atSeq})
|
|
347
|
-
//
|
|
348
|
-
// 它删掉 isSeeded(官方 fork 设 true)、不写 inheritedEventCount、
|
|
349
|
-
// 也不调 buildForkSeed 补 step/turn closers → 产出的子会话边界可能悬空。
|
|
350
|
-
// 官方 fork 失败通常意味着源会话正在运行,正确做法是拒绝而非绕过引擎写盘。
|
|
390
|
+
// 0.9.0:分支**只走官方** sessions.fork({atSeq});失败即拒绝(源会话多半在
|
|
391
|
+
// 运行),不再回落写盘 —— 被删的磁盘 applyBranch 语义复盘见 README「为什么不重复造轮子」。
|
|
351
392
|
const forkPath = pickForkPath(__sessionsSvc)
|
|
352
393
|
if (forkPath.kind !== 'official') {
|
|
353
394
|
setError(t('fork.unavailable'))
|
|
@@ -385,8 +426,12 @@ window.__ModuleLoader__.load({
|
|
|
385
426
|
// S6 修复(G-M1):不再 900ms 裸 location.reload。成功走 devkit
|
|
386
427
|
// 标准 toast(无 devkit 时降级为对话框内 doneMsg),并提供手动
|
|
387
428
|
// 「刷新页面」按钮。
|
|
388
|
-
|
|
389
|
-
|
|
429
|
+
// 0.9.7:补回 ec862d6(0.9.0) 误删的 `const okMsg =`——留下悬挂的
|
|
430
|
+
// `? :` 会让成功路径抛 ReferenceError 落进 .catch(用户看到「操作失败」,
|
|
431
|
+
// notifyDone/emitChanged/openSession/回填全不执行),失败路径则恒走
|
|
432
|
+
// `?` 分支把成功文案当错误抛出。对话框 mode 只有 revert|delete|branch,
|
|
433
|
+
// branch 在上面已 return,故 done.restore 分支在此不可达,一并删掉。
|
|
434
|
+
const okMsg = mode === 'revert' ? t('done.revert') : t('done.delete')
|
|
390
435
|
// 0.4.0 composer 回填(opencode 式):回滚用户消息 → 原文回填输入框,「编辑重发」零按钮
|
|
391
436
|
if (mode === 'revert' && pickedMsg && pickedMsg.role === 'user' && pickedMsg.fullText
|
|
392
437
|
&& __inputActions && typeof __inputActions.setDraft === 'function') {
|
|
@@ -493,7 +538,9 @@ window.__ModuleLoader__.load({
|
|
|
493
538
|
'#' + m.seq + ' ' + (m.role || '') + ' · ' + (m.snippet || '') + (m.visible === false ? t('dialog.invisible') : '')),
|
|
494
539
|
)),
|
|
495
540
|
),
|
|
496
|
-
opsPanel
|
|
541
|
+
// 0.9.7:删掉列表**之后**的旧 opsPanel——0.9.0 把操作区挪到列表之前时
|
|
542
|
+
// 没删旧位置,两组同名 radio(name='dsh-message-ops-mode')互抢,
|
|
543
|
+
// checked 状态在两份 DOM 之间互相顶掉。
|
|
497
544
|
])
|
|
498
545
|
}
|
|
499
546
|
|
|
@@ -626,20 +673,20 @@ window.__ModuleLoader__.load({
|
|
|
626
673
|
const [seq, setSeq] = React.useState(null)
|
|
627
674
|
const [fullText, setFullText] = React.useState(null)
|
|
628
675
|
const [busy, setBusy] = React.useState(null) // 'revert' | null(S11 双提交防护)
|
|
676
|
+
// 0.9.7(P4):索引被 dataSig 重建后要重跑解析,否则「新 AI 消息旁
|
|
677
|
+
// 没有回撤/引用按钮」—— 已经挂载的槽位 effect 不会因缓存被换而重跑。
|
|
678
|
+
const [idxRev, setIdxRev] = React.useState(0)
|
|
679
|
+
React.useEffect(() => {
|
|
680
|
+
const onIdx = () => setIdxRev((v) => v + 1)
|
|
681
|
+
window.addEventListener(SEQ_INDEX_EVENT, onIdx)
|
|
682
|
+
return () => window.removeEventListener(SEQ_INDEX_EVENT, onIdx)
|
|
683
|
+
}, [])
|
|
629
684
|
React.useEffect(() => {
|
|
630
685
|
if (!messageId || !sessionId) return
|
|
631
686
|
let alive = true
|
|
632
|
-
|
|
633
|
-
const
|
|
634
|
-
|
|
635
|
-
const hit = index && index.get(String(messageId))
|
|
636
|
-
if (typeof hit === 'number') setSeq(hit)
|
|
637
|
-
}
|
|
638
|
-
if (cached && typeof cached.then === 'function') cached.then(resolve).catch(() => {})
|
|
639
|
-
else if (cached) resolve(cached)
|
|
640
|
-
else {
|
|
641
|
-
const p = fetch('/api/message-ops/messages?sessionId=' + encodeURIComponent(sessionId))
|
|
642
|
-
.then((r) => (r.ok ? r.json() : Promise.reject(new Error('HTTP ' + r.status))))
|
|
687
|
+
let retried = false
|
|
688
|
+
const load = () => {
|
|
689
|
+
const p = fetchMessagesJson('/api/message-ops/messages?sessionId=' + encodeURIComponent(sessionId))
|
|
643
690
|
.then((data) => {
|
|
644
691
|
const index = new Map()
|
|
645
692
|
const full = new Map()
|
|
@@ -656,10 +703,30 @@ window.__ModuleLoader__.load({
|
|
|
656
703
|
__seqIndexCache.set(sessionId, p)
|
|
657
704
|
// 0.5.2(P2):失败必须清缓存——否则 rejected promise 被永久缓存,
|
|
658
705
|
// 该会话按钮/dock 到刷新页面为止全部失效
|
|
659
|
-
|
|
706
|
+
// 0.9.7:只在缓存里还是**我们这条** promise 时才删(syncSeqIndex 可能已换掉)
|
|
707
|
+
p.catch(() => { if (__seqIndexCache.get(sessionId) === p) __seqIndexCache.delete(sessionId) })
|
|
708
|
+
return p
|
|
709
|
+
}
|
|
710
|
+
const resolve = (index) => {
|
|
711
|
+
if (!alive) return
|
|
712
|
+
const hit = index && index.get(String(messageId))
|
|
713
|
+
if (typeof hit === 'number') { setSeq(hit); return }
|
|
714
|
+
// 0.9.7(P4):索引里没有这个 messageId = 会话已增长(或索引过期)。
|
|
715
|
+
// 旧实现这里**什么都不做**,`seq` 永远是 null → 组件 return null →
|
|
716
|
+
// 按钮再也不渲染。现在失效并重拉一次(retried 保证不自旋);
|
|
717
|
+
// 复用 fetchMessagesJson 的 1s 合并窗口,连续上新不会打爆请求。
|
|
718
|
+
if (retried) return
|
|
719
|
+
retried = true
|
|
720
|
+
__seqIndexCache.delete(sessionId)
|
|
721
|
+
__fullTextCache.delete(sessionId)
|
|
722
|
+
load().then(resolve).catch(() => {})
|
|
660
723
|
}
|
|
724
|
+
const cached = __seqIndexCache.get(sessionId)
|
|
725
|
+
if (cached && typeof cached.then === 'function') cached.then(resolve).catch(() => {})
|
|
726
|
+
else if (cached) resolve(cached)
|
|
727
|
+
else load().then(resolve).catch(() => {})
|
|
661
728
|
return () => { alive = false }
|
|
662
|
-
}, [messageId, sessionId])
|
|
729
|
+
}, [messageId, sessionId, idxRev])
|
|
663
730
|
|
|
664
731
|
// 原文(引用用):seq 解析后按需取全文
|
|
665
732
|
React.useEffect(() => {
|
|
@@ -763,7 +830,14 @@ window.__ModuleLoader__.load({
|
|
|
763
830
|
// (按 restoredSourceSeqs 进度算;旧 notice 无该字段 → 服务端视为整段已恢复)。
|
|
764
831
|
// 旧的"行内 restoresSeq 集合"客户端判定已删除——按轮步进下部分恢复的标记
|
|
765
832
|
// 也带 restoresSeq,旧逻辑会把它们误杀(实测:第一轮恢复后整条消失)。
|
|
766
|
-
|
|
833
|
+
// 0.10.1:再排掉**已提交**的 marker —— 对齐 opencode
|
|
834
|
+
// (用户发出下一条真实消息后,opencode 在 Session.prompt 里自动
|
|
835
|
+
// SessionRevert.commit() → revert:null → affordance 消失)。
|
|
836
|
+
// 我们是 append-only,marker 不会真消失,所以由服务端按
|
|
837
|
+
// 「marker 之后有无 source.kind==='user' 的真实输入」算出 committed。
|
|
838
|
+
return (msgs || []).filter((m) =>
|
|
839
|
+
m.marker && m.sourceKind !== 'compact-checkpoint'
|
|
840
|
+
&& m.restoreComplete !== true && m.committed !== true)
|
|
767
841
|
}
|
|
768
842
|
|
|
769
843
|
function RevertDock(props) {
|
|
@@ -815,8 +889,7 @@ window.__ModuleLoader__.load({
|
|
|
815
889
|
let alive = true
|
|
816
890
|
const onChange = () => load()
|
|
817
891
|
window.addEventListener(CHANGED_EVENT, onChange)
|
|
818
|
-
const load = () =>
|
|
819
|
-
.then((r) => (r.ok ? r.json() : null))
|
|
892
|
+
const load = () => fetchMessagesJson('/api/message-ops/messages?sessionId=' + encodeURIComponent(sessionId))
|
|
820
893
|
.then((data) => {
|
|
821
894
|
if (!alive || !data || !data.ok) return
|
|
822
895
|
const msgs = data.messages || []
|
|
@@ -860,13 +933,11 @@ window.__ModuleLoader__.load({
|
|
|
860
933
|
try {
|
|
861
934
|
if (__uiWorkspace && typeof __uiWorkspace.openSession === 'function') __uiWorkspace.openSession(sessionId)
|
|
862
935
|
} catch { /* fallthrough */ }
|
|
863
|
-
// 0.9.1:重放的消息带**新 seq**,被恢复的行必须解除 hidden
|
|
864
|
-
//
|
|
865
|
-
|
|
866
|
-
// 0.9.1:重放的消息带**新 seq**,被恢复的行必须解除 hidden;
|
|
867
|
-
// 同时 dock 的 marker 列表要按权威数据重算(restoreComplete 会变 true)。
|
|
936
|
+
// 0.9.1:重放的消息带**新 seq**,被恢复的行必须解除 hidden;dock 的
|
|
937
|
+
// marker 列表要按权威数据重算(restoreComplete 会变 true)后重拉。
|
|
938
|
+
emitChanged()
|
|
868
939
|
for (const delay of [150, 700, 1600]) setTimeout(() => { if (__refreshSurface) __refreshSurface(sessionId) }, delay)
|
|
869
|
-
const fresh = await
|
|
940
|
+
const fresh = await fetchMessagesJson('/api/message-ops/messages?sessionId=' + encodeURIComponent(sessionId)).catch(() => null)
|
|
870
941
|
if (fresh && fresh.ok) setMarkers(activeMarkers(fresh.messages || []))
|
|
871
942
|
})
|
|
872
943
|
.catch((reason) => {
|
|
@@ -889,6 +960,11 @@ window.__ModuleLoader__.load({
|
|
|
889
960
|
React.createElement('span', { className: 'mopsRdRowText' }, '#' + row.seq + ' · ' + (row.snippet || '')),
|
|
890
961
|
React.createElement('button', {
|
|
891
962
|
type: 'button', className: 'mopsRdRestore', disabled: restoring != null || running,
|
|
963
|
+
// 0.10.2 专家组评估结论:唯一「严重度=高」的真实缺口是 restore 为**重放**语义 ——
|
|
964
|
+
// 消息带新 seq 落到会话末尾,**不会回到原位**(引擎 SurfaceOp 只有 append/replace,
|
|
965
|
+
// 不存在解除遮蔽的原语)。用户点下去看到消息"搬家到底部"会以为出 bug,
|
|
966
|
+
// 所以把语义写在按钮上,别让人靠猜。
|
|
967
|
+
title: t('dock.restoreHint'),
|
|
892
968
|
onClick: () => restoreRow(row),
|
|
893
969
|
}, restoring === rowKey(row) ? t('dock.restoring') : t('dock.restore')),
|
|
894
970
|
)
|
|
@@ -938,49 +1014,57 @@ window.__ModuleLoader__.load({
|
|
|
938
1014
|
}
|
|
939
1015
|
|
|
940
1016
|
// --- 0.7.0 用户消息「回滚」按钮(DOM 注入,挂在官方动作行内)-------------------
|
|
941
|
-
// 0.6.0 的教训(用户实测反馈):absolute 覆盖在气泡上 = 压住文字、布局乱;
|
|
942
|
-
// 且 opacity:0 + :hover 在触屏上永远不显形(点不了)。
|
|
943
1017
|
// 官方用户消息的动作行 = .xzv4MW_actions(含 Copy,height28,位于消息下方 16px),
|
|
944
|
-
// 可见性由宿主 [data-actions-reveal]
|
|
945
|
-
//
|
|
1018
|
+
// 可见性由宿主 [data-actions-reveal] 控制。把按钮 append 进该行、与 Copy 同级同层,
|
|
1019
|
+
// 可见性天然继承宿主动作行(不再自己玩 hover)。0.6.0 的 absolute 覆盖教训见 README「历史事故记录」#2。
|
|
946
1020
|
function installUserRevertInjector() {
|
|
947
1021
|
if (window.__MOPS_USER_REVERT_INSTALLED) return
|
|
948
1022
|
window.__MOPS_USER_REVERT_INSTALLED = true
|
|
949
1023
|
var rowsCache = { sid: null, rows: null, at: 0 }
|
|
950
1024
|
__peekRowsCache = () => rowsCache
|
|
951
1025
|
var norm = (x) => String(x || '').replace(/\s+/g, '')
|
|
952
|
-
|
|
1026
|
+
|
|
1027
|
+
// 0.9.7(P4):messageId→seq / 全文索引挂在**同一个 dataSig** 上。
|
|
1028
|
+
// 旧实现每会话只写一次、仅失败时 delete —— 会话增长后新 assistant 消息的
|
|
1029
|
+
// messageId 不在索引里,`seq == null` → MsgSlotActions 直接 return null,
|
|
1030
|
+
// 那条消息旁的回撤/引用按钮**再也不渲染**。dataSig 变了就用手里这份全量
|
|
1031
|
+
// 载荷就地重建(零额外请求),并广播 SEQ_INDEX_EVENT 让已挂载的槽位重跑解析。
|
|
1032
|
+
var __seqDataSig = null
|
|
1033
|
+
var syncSeqIndex = (sid, messages, idx) => {
|
|
1034
|
+
const sig = sid + '/' + idx.total + '/' + idx.shadowed.size + '/' + idx.visibleTurns.size
|
|
1035
|
+
if (sig === __seqDataSig) return
|
|
1036
|
+
__seqDataSig = sig
|
|
1037
|
+
const index = new Map()
|
|
1038
|
+
const full = new Map()
|
|
1039
|
+
for (const m of (messages || [])) {
|
|
1040
|
+
if (!m || m.id == null) continue
|
|
1041
|
+
const k = String(m.id)
|
|
1042
|
+
// 同 id 取**最早** seq —— 与槽位自己的 buildSeqIndex 口径一致
|
|
1043
|
+
// (隐藏判据 buildIndex 取最新,两套语义不能混)。
|
|
1044
|
+
if (!index.has(k)) index.set(k, m.seq)
|
|
1045
|
+
if (m.fullText) full.set(k, m.fullText)
|
|
1046
|
+
}
|
|
1047
|
+
for (const key of [sid, 'session-' + sid]) {
|
|
1048
|
+
__seqIndexCache.set(key, index)
|
|
1049
|
+
__fullTextCache.set(key, full)
|
|
1050
|
+
}
|
|
1051
|
+
try { window.dispatchEvent(new CustomEvent(SEQ_INDEX_EVENT)) } catch { /* non-browser */ }
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
var fetchRows = (sidIn) => {
|
|
1055
|
+
// 0.9.7:统一按**裸 uuid** 作缓存键。调用方有的传 `session-xxx`(scan /
|
|
1056
|
+
// /undo 的 rawSid)有的传裸 uuid(/redo 的 bareSessionId)—— 混用会让
|
|
1057
|
+
// rowsCache.sid 在两个键之间来回横跳,每次都是缓存未命中(白白多发 GET)。
|
|
1058
|
+
const sid = bareSessionId(sidIn)
|
|
953
1059
|
if (rowsCache.sid === sid && rowsCache.rows && Date.now() - rowsCache.at < 8000) return Promise.resolve(rowsCache)
|
|
954
|
-
return
|
|
955
|
-
.then((r) => (r.ok ? r.json() : null))
|
|
1060
|
+
return fetchMessagesJson('/api/message-ops/messages?sessionId=' + encodeURIComponent(sid))
|
|
956
1061
|
.then((d) => {
|
|
957
1062
|
// 宿主会把 <system-reminder> / runtime context 等也记为 role=user 的行,
|
|
958
|
-
// DOM
|
|
959
|
-
//
|
|
960
|
-
// 0.9.0 两个实机 bug(浏览器实测:4 个按钮 data-mops-seq 全为 null,点击必失败):
|
|
961
|
-
// ① 正则跑在 norm() 之后的串上,而 norm 是 replace(/\s+/g,'') —— 删掉**全部**空白,
|
|
962
|
-
// `current\s+runtime\s+context` 永远匹配不上 → runtime-context 伪行漏进配对。
|
|
963
|
-
// 修法:injected() 改判**原始 snippet**。
|
|
964
|
-
// ② 过滤了 `m.visible !== false`,但宿主聊天视图**不施��**遮蔽模型,照常渲染
|
|
965
|
-
// 被回滚遮蔽的消息 → 配对候选与 DOM 块零交集。
|
|
966
|
-
// 修法:DOM 配对必须覆盖全部 user 行(含遮蔽态)。
|
|
967
|
-
// 注意:对话框的「可回滚目标」列表仍应过滤 visible(那是另一套语义)。
|
|
968
|
-
// 0.9.6:原先只排 system-reminder / runtime context 两种,
|
|
969
|
-
// 但宿主把 `background job …`、`Agent … sent a message:`、
|
|
970
|
-
// `Background subagent … finished`、`[model changed: …]` 也记成 role=user,
|
|
971
|
-
// 于是 /undo 会把**通知行**当目标 —— 回滚范围与回填文本都变成通知内容
|
|
972
|
-
// (378 会话普查:38 个 10% 命中;实测 2188f4ac 目标是
|
|
973
|
-
// "Background subagent f09a18f6… finished…",而用户那句在 seq829)。
|
|
1063
|
+
// DOM 里并不渲染成"我的消息"块 —— 必须滤掉,否则按序配对会错位(0.7.0 根因)。
|
|
1064
|
+
// 注意:对话框的「可回滚目标」列表仍应过滤 visible(那是另一套语义)。
|
|
974
1065
|
// 稳妥做法本该在服务端打 injected 标志,先在客户端补齐正则。
|
|
975
|
-
//
|
|
976
|
-
//
|
|
977
|
-
// 通知内容(378 会话普查 38 个 10% 命中;实测 2188f4ac 目标是
|
|
978
|
-
// "Background subagent f09a18f6… finished…",用户那句其实在 seq829)。
|
|
979
|
-
//
|
|
980
|
-
// 正则**按 120 份日志的真实样本**收紧,不靠猜:
|
|
981
|
-
// 256 × <system-reminder>… 128 × background job bash-358 (…
|
|
982
|
-
// 38 × [model changed: … 26 × Background subagent <uuid>
|
|
983
|
-
// 21 × Agent <uuid> sent a message:
|
|
1066
|
+
// 判据一律用**原始 snippet**(norm() 删掉全部空白,正则必然失配)。
|
|
1067
|
+
// 0.9.0 / 0.9.6 两轮事故复盘 + 120 份日志普查样本见 README「历史事故记录」#3。
|
|
984
1068
|
// 关键约束:id 后必须跟得上具体形态([\w-]+ / [0-9a-f]{8}-),
|
|
985
1069
|
// 否则 `Background job 是我自己的问题` 这种真人输入会被误杀
|
|
986
1070
|
// (曾用 ^background job\b,测试当场抓到这个误杀)。
|
|
@@ -993,27 +1077,16 @@ window.__ModuleLoader__.load({
|
|
|
993
1077
|
// 0.9.1:同时产出 node-key 查表索引(隐藏 transcript 用)
|
|
994
1078
|
const idx = buildIndex(d)
|
|
995
1079
|
rowsCache = { sid: sid, rows: rows, idx: idx, at: Date.now() }
|
|
1080
|
+
syncSeqIndex(sid, d && d.messages, idx)
|
|
996
1081
|
return rowsCache
|
|
997
1082
|
})
|
|
998
1083
|
.catch(() => (rowsCache.sid === sid ? rowsCache : null))
|
|
999
1084
|
}
|
|
1000
1085
|
|
|
1001
1086
|
// ═══ 0.9.1 宿主 node-key ↔ 日志 seq 的精确映射 ═══════════════════════════
|
|
1002
|
-
//
|
|
1003
|
-
//
|
|
1004
|
-
//
|
|
1005
|
-
// 我们写的 surfaceOp replace 只折叠**模型 surface**(实测 393→13 节点),
|
|
1006
|
-
// 被遮蔽的消息在人类 transcript 里**照常渲染且完全可见**,连 marker 本身
|
|
1007
|
-
// 都不渲染(systemMessageDefinition 对 surfaceOp !== 'append' 直接 return null)。
|
|
1008
|
-
// 所以要让回滚「看得见」,只能在渲染层处理。
|
|
1009
|
-
//
|
|
1010
|
-
// 好消息:每一行的 `data-chat-node-key` 形如 `<len>:<kind><id>`,而 <id>
|
|
1011
|
-
// 就是日志事件的字段(user → messageId、assistant-step → `<turn>:<step>`、
|
|
1012
|
-
// tool-call → callId、turn-* → turn 号),客户端本来就已经建了
|
|
1013
|
-
// messageId → seq 索引(__seqIndexCache)。**精确查表,零启发式。**
|
|
1014
|
-
//
|
|
1015
|
-
// 旧实现用「40 字符前缀包含匹配」配对 DOM 与日志,实测 4 条里错 2 条
|
|
1016
|
-
// ("继续" 命中了 "继续,…"),已整体删除。
|
|
1087
|
+
// dsh transcript 刻意不折叠(surface.d.ts:51-63 "replacement copies stay
|
|
1088
|
+
// model-only"),回滚只能在渲染层按 `data-chat-node-key` 精确查表(零启发式)。
|
|
1089
|
+
// 背景长文与「40 字符前缀匹配」事故见 README「0.9.1 / 0.9.2 · 回滚「真的生效了」」。
|
|
1017
1090
|
var parseNodeKey = (k) => {
|
|
1018
1091
|
var m = /^(\d+):/.exec(k || '');
|
|
1019
1092
|
if (!m) return null;
|
|
@@ -1026,9 +1099,8 @@ window.__ModuleLoader__.load({
|
|
|
1026
1099
|
|
|
1027
1100
|
/**
|
|
1028
1101
|
* 从 /api/message-ops/messages 的响应构建全部查表。
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
*/
|
|
1102
|
+
* 两条铁律:① 索引只认唯一 id;② 缺数据一律返回 undefined(调用方保持可见)。
|
|
1103
|
+
*/
|
|
1032
1104
|
var buildIndex = (payload) => {
|
|
1033
1105
|
var list = (payload && payload.messages) || []
|
|
1034
1106
|
// 0.9.1:回合页脚/过程条不是消息,用 turn/end 的 seq 定位(边界常落在 turn 内部,
|
|
@@ -1038,10 +1110,8 @@ window.__ModuleLoader__.load({
|
|
|
1038
1110
|
var byTurnStep = new Map();
|
|
1039
1111
|
var byCallId = new Map();
|
|
1040
1112
|
var byTurn = new Map();
|
|
1041
|
-
var visible = new Set(); // 在模型 surface 上(仅用于「可回滚目标」)
|
|
1042
1113
|
var shadowed = new Set(); // 落在 replace 区间内 = 已被回滚(transcript 藏行判据)
|
|
1043
1114
|
var visibleTurns = new Set(); // 含至少一条**未被遮蔽**消息的 turn 号(回合 chrome)
|
|
1044
|
-
var dupMessageId = 0
|
|
1045
1115
|
var dupByMessageId = new Map();
|
|
1046
1116
|
for (var i = 0; i < list.length; i++) {
|
|
1047
1117
|
var m = list[i];
|
|
@@ -1053,7 +1123,6 @@ window.__ModuleLoader__.load({
|
|
|
1053
1123
|
// fence」。回放副本才是当前 surface 上的那条,用最早的 seq 判会让
|
|
1054
1124
|
// 「恢复后再回滚」漏藏(实测 fence=70、副本 seq=70、原件 seq=54)。
|
|
1055
1125
|
if (byMessageId.has(k)) {
|
|
1056
|
-
dupMessageId++
|
|
1057
1126
|
const arr = dupByMessageId.get(k) || [byMessageId.get(k)]
|
|
1058
1127
|
arr.push(m.seq)
|
|
1059
1128
|
dupByMessageId.set(k, arr)
|
|
@@ -1065,15 +1134,15 @@ window.__ModuleLoader__.load({
|
|
|
1065
1134
|
}
|
|
1066
1135
|
if (m.type === 'tool/call' && m.id != null) byCallId.set(String(m.id), m.seq);
|
|
1067
1136
|
if (m.turn != null && !byTurn.has(String(m.turn))) byTurn.set(String(m.turn), m.seq);
|
|
1068
|
-
|
|
1069
|
-
//
|
|
1070
|
-
//
|
|
1137
|
+
// 0.9.6:藏行判据用 shadowed(replace 区间),**不用** m.visible(模型 surface)。
|
|
1138
|
+
// visible 在 live 路径下对 tool/call 恒 false → 拿它当判据会把整页工具行全藏,
|
|
1139
|
+
// 故这里连集合都不建(判据样本见 test/surface-visibility.test.js)。
|
|
1071
1140
|
if (m.shadowed === true) shadowed.add(m.seq);
|
|
1072
1141
|
else if (m.turn != null) visibleTurns.add(String(m.turn));
|
|
1073
1142
|
}
|
|
1074
1143
|
// turnEnd 优先:它对「该 turn 内一条消息都没有」的情况仍然有效
|
|
1075
1144
|
if (turnEnd) for (const k in turnEnd) if (Object.prototype.hasOwnProperty.call(turnEnd, k)) byTurn.set(k, turnEnd[k])
|
|
1076
|
-
return { byMessageId, dupByMessageId, byTurnStep, byCallId, byTurn,
|
|
1145
|
+
return { byMessageId, dupByMessageId, byTurnStep, byCallId, byTurn, shadowed, visibleTurns, total: list.length };
|
|
1077
1146
|
};
|
|
1078
1147
|
|
|
1079
1148
|
/** node-key → { seq, all }(all = 同 id 的全部 seq,处理 restore 重放副本)。 */
|
|
@@ -1123,19 +1192,25 @@ window.__ModuleLoader__.load({
|
|
|
1123
1192
|
*/
|
|
1124
1193
|
var applySurfaceVisibility = (root, idx) => {
|
|
1125
1194
|
if (!idx) return { hidden: 0, unresolved: 0, shown: 0 };
|
|
1126
|
-
|
|
1195
|
+
// 0.9.7(P2-1):root 既可传 document/元素(调用方自查),也可直接传
|
|
1196
|
+
// 已经查好的行集合 —— ensureAssign 每次都要算 rows.length 当签名,
|
|
1197
|
+
// 传集合避免同一次扫描里 querySelectorAll 跑两遍。
|
|
1198
|
+
var rows = (root && typeof root.querySelectorAll === 'function')
|
|
1199
|
+
? root.querySelectorAll('[data-chat-node-key]')
|
|
1200
|
+
: (root || document.querySelectorAll('[data-chat-node-key]'));
|
|
1127
1201
|
var hidden = 0, unresolved = 0, shown = 0;
|
|
1128
1202
|
for (var i = 0; i < rows.length; i++) {
|
|
1129
1203
|
var row = rows[i];
|
|
1130
|
-
// 0.9.6
|
|
1131
|
-
//
|
|
1132
|
-
// live 路径下 tool/call 全藏)→ 现在的 shadowed(恒定、与会话活跃无关)。
|
|
1133
|
-
// 0.9.4 判据(见下):**模型 surface**。此前用 revertFences 塌缩成
|
|
1134
|
-
// `seq >= min(fences)`,在一个有 19 个**离散** marker 的会话里
|
|
1135
|
-
// min fence=10 → seq>=10 全藏 → 4792/4793 全被隐藏 → 正文几乎空白
|
|
1136
|
-
// (截图复现)。改用 visible 后与宿主 visibleCount 精确吻合。
|
|
1204
|
+
// 0.9.6 藏行判据 = **replace 区间(shadowed)**;revertFences→visible→shadowed
|
|
1205
|
+
// 演进史(含 4792/4793 全藏事故)见 README「历史事故记录」#4。
|
|
1137
1206
|
var hit = seqOfNodeKey(row.getAttribute('data-chat-node-key'), idx);
|
|
1138
|
-
|
|
1207
|
+
// 0.9.7(P2-1)幂等跳写:判据与执行时机**一字未改**,只在
|
|
1208
|
+
// 「目标状态 ≠ 当前状态」时才碰属性 —— 省掉全表几百次无效写。
|
|
1209
|
+
if (hit === null) {
|
|
1210
|
+
unresolved++;
|
|
1211
|
+
if (row.hasAttribute('hidden')) row.removeAttribute('hidden');
|
|
1212
|
+
continue;
|
|
1213
|
+
}
|
|
1139
1214
|
var shouldHide;
|
|
1140
1215
|
if (hit.turn !== undefined) {
|
|
1141
1216
|
// 回合 chrome:本 turn 还有未被遮蔽的消息就露出
|
|
@@ -1146,8 +1221,13 @@ window.__ModuleLoader__.load({
|
|
|
1146
1221
|
// 会把整页工具行全藏(实测 157 行藏 152 行)。
|
|
1147
1222
|
shouldHide = idx.shadowed.has(hit.seq);
|
|
1148
1223
|
}
|
|
1149
|
-
if (shouldHide) {
|
|
1150
|
-
|
|
1224
|
+
if (shouldHide) {
|
|
1225
|
+
hidden++;
|
|
1226
|
+
if (row.getAttribute('hidden') !== 'until-found') row.setAttribute('hidden', 'until-found');
|
|
1227
|
+
} else {
|
|
1228
|
+
shown++;
|
|
1229
|
+
if (row.hasAttribute('hidden')) row.removeAttribute('hidden');
|
|
1230
|
+
}
|
|
1151
1231
|
}
|
|
1152
1232
|
return { hidden, unresolved, shown };
|
|
1153
1233
|
};
|
|
@@ -1196,11 +1276,11 @@ window.__ModuleLoader__.load({
|
|
|
1196
1276
|
btn.setAttribute('data-mops-unmatched', '1')
|
|
1197
1277
|
}
|
|
1198
1278
|
}
|
|
1199
|
-
var assignSeqs = (sid, cache) => {
|
|
1279
|
+
var assignSeqs = (sid, cache, anchors) => {
|
|
1200
1280
|
const idx = cache && cache.idx
|
|
1201
1281
|
if (!idx) return
|
|
1202
1282
|
// 0.9.1:node-key 查表,替代旧的 40 字符前缀包含匹配(实测 4 条错 2 条)
|
|
1203
|
-
for (const b of userAnchors()) {
|
|
1283
|
+
for (const b of (anchors || userAnchors())) {
|
|
1204
1284
|
b.removeAttribute('data-mops-seq')
|
|
1205
1285
|
b.removeAttribute('data-mops-unmatched')
|
|
1206
1286
|
const hit = seqOfNodeKey(b.getAttribute('data-chat-node-key'), idx)
|
|
@@ -1218,9 +1298,40 @@ window.__ModuleLoader__.load({
|
|
|
1218
1298
|
// 之前放在 fetch 回调里做事后全局扫描,会与 DOM 注入/重排竞态,导致
|
|
1219
1299
|
// 块已打上 seq 而按钮仍是禁用态(实测:block seq=[8,54],按钮 disabled=true)。
|
|
1220
1300
|
// 按钮可用性与 transcript 隐藏都在这里统一落地(同一批数据,避免竞态)
|
|
1301
|
+
//
|
|
1302
|
+
// 0.9.7(P2-1)三签名门控。scan **无条件**调 ensureAssign 是故意的
|
|
1303
|
+
// (早先做成「只在没打标时刷」→ 界面停在旧状态),所以门控只放在
|
|
1304
|
+
// assignSeqs 的全量 DOM 改写上,三者全等才跳过:
|
|
1305
|
+
// dataSig —— idx.total / shadowed.size / visibleTurns.size(数据变了必须重算)
|
|
1306
|
+
// domSig —— transcript 行数(行集合变了必须重算)
|
|
1307
|
+
// anchorSig —— 每个 user 块的 node-key + 打标状态(宿主重建块 / CHANGED
|
|
1308
|
+
// 摘牌后 node-key 相同但打标为空,靠这里发现)
|
|
1309
|
+
// 🔴 applySurfaceVisibility 仍然**无条件**执行,藏行判据与时机一字未改。
|
|
1310
|
+
var assignSig = null
|
|
1311
|
+
var anchorSigOf = (anchors) => {
|
|
1312
|
+
let s = ''
|
|
1313
|
+
for (let i = 0; i < anchors.length; i++) {
|
|
1314
|
+
const b = anchors[i]
|
|
1315
|
+
s += (b.getAttribute('data-chat-node-key') || '') + '=' +
|
|
1316
|
+
(b.getAttribute('data-mops-seq') || (b.hasAttribute('data-mops-unmatched') ? '?' : '-')) + ','
|
|
1317
|
+
}
|
|
1318
|
+
return s
|
|
1319
|
+
}
|
|
1320
|
+
var sigOf = (sid, dataSig, rowsLen, anchors) =>
|
|
1321
|
+
bareSessionId(sid) + '|' + dataSig + '|' + rowsLen + '|' + anchorSigOf(anchors)
|
|
1221
1322
|
var ensureAssign = (sid) => fetchRows(sid).then((cache) => {
|
|
1222
|
-
|
|
1223
|
-
|
|
1323
|
+
if (!cache || !cache.idx) return cache
|
|
1324
|
+
const idx = cache.idx
|
|
1325
|
+
const rows = document.querySelectorAll('[data-chat-node-key]')
|
|
1326
|
+
const dataSig = idx.total + '/' + idx.shadowed.size + '/' + idx.visibleTurns.size
|
|
1327
|
+
const anchors = userAnchors()
|
|
1328
|
+
const before = sigOf(sid, dataSig, rows.length, anchors)
|
|
1329
|
+
if (before !== assignSig) {
|
|
1330
|
+
assignSeqs(sid, cache, anchors)
|
|
1331
|
+
// 存**改写之后**的真实签名:下一次直接相等就跳过,不用跑两轮才收敛
|
|
1332
|
+
assignSig = sigOf(sid, dataSig, rows.length, anchors)
|
|
1333
|
+
}
|
|
1334
|
+
applySurfaceVisibility(rows, idx)
|
|
1224
1335
|
return cache
|
|
1225
1336
|
})
|
|
1226
1337
|
// 0.7.0:点击时先**同步完成**配对(打标是异步 fetch 的结果,
|
|
@@ -1256,7 +1367,7 @@ window.__ModuleLoader__.load({
|
|
|
1256
1367
|
notifyDone(__t('done.revert'))
|
|
1257
1368
|
// 0.9.6:统一回填出口 —— 这个入口原先也不回填
|
|
1258
1369
|
refillComposer(sid, row.seq)
|
|
1259
|
-
|
|
1370
|
+
emitChanged()
|
|
1260
1371
|
try {
|
|
1261
1372
|
if (__uiWorkspace && typeof __uiWorkspace.openSession === 'function') __uiWorkspace.openSession(sid)
|
|
1262
1373
|
} catch { /* 视图重建失败不阻断 */ }
|
|
@@ -1293,13 +1404,38 @@ window.__ModuleLoader__.load({
|
|
|
1293
1404
|
runUserRevert(btn, block)
|
|
1294
1405
|
})
|
|
1295
1406
|
host.appendChild(btn)
|
|
1407
|
+
// 0.9.7(P2-1):新按钮按块的**当前**打标状态立刻同步一次。
|
|
1408
|
+
// 门控之后 assignSeqs 可能被跳过(签名未变),靠这里保证「宿主重建了
|
|
1409
|
+
// 动作行 → 按钮重新挂上」这种情况仍然是 disabled/title 正确的。
|
|
1410
|
+
syncEnabled(block, btn)
|
|
1296
1411
|
return btn
|
|
1297
1412
|
}
|
|
1413
|
+
// 0.9.7(P0-2 读合并):把 150/700/1600ms 三档并成**一次**真网络请求,
|
|
1414
|
+
// 落点 = 本 burst 第一档 + 1500ms ≈ 原来的 1600ms 那一档。
|
|
1415
|
+
// 🔴 红线:这一档是「视图重建后重算隐藏」的兜底,绝不能砍成 0。
|
|
1416
|
+
// 调用当下立刻 rowsCache.at = 0 —— openSession 触发的那次 scan 因此能用
|
|
1417
|
+
// 共享窗口里的新鲜载荷先把隐藏刷对,不必等兜底那一发。
|
|
1418
|
+
var refreshTimer = null
|
|
1419
|
+
var refreshDeadline = 0
|
|
1298
1420
|
__refreshSurface = (sid) => {
|
|
1299
1421
|
const id = sid || __currentSessionId
|
|
1300
1422
|
if (!id) return
|
|
1301
1423
|
if (rowsCache) rowsCache.at = 0 // 强制重取权威数据
|
|
1302
|
-
|
|
1424
|
+
const now = Date.now()
|
|
1425
|
+
if (!refreshTimer) refreshDeadline = now + 1500
|
|
1426
|
+
clearTimeout(refreshTimer)
|
|
1427
|
+
refreshTimer = setTimeout(() => {
|
|
1428
|
+
refreshTimer = null
|
|
1429
|
+
try {
|
|
1430
|
+
// 会话已切走就别把旧会话的 idx 刷到新会话的 DOM 上
|
|
1431
|
+
if (bareSessionId(__currentSessionId) !== bareSessionId(id)) return
|
|
1432
|
+
invalidateMessagesMemo() // 兜底档必须是真网络,不许吃缓存
|
|
1433
|
+
if (rowsCache) rowsCache.at = 0
|
|
1434
|
+
fetchRows(id)
|
|
1435
|
+
.then((cache) => { if (cache && cache.idx) applySurfaceVisibility(document, cache.idx) })
|
|
1436
|
+
.catch(() => {})
|
|
1437
|
+
} catch { /* noop */ }
|
|
1438
|
+
}, Math.max(0, refreshDeadline - now))
|
|
1303
1439
|
}
|
|
1304
1440
|
var scan = () => {
|
|
1305
1441
|
try {
|
|
@@ -1326,8 +1462,56 @@ window.__ModuleLoader__.load({
|
|
|
1326
1462
|
}, { passive: true })
|
|
1327
1463
|
window.addEventListener('scroll', scan, { passive: true })
|
|
1328
1464
|
try {
|
|
1329
|
-
|
|
1330
|
-
|
|
1465
|
+
// 0.9.7(P1-2)回调过滤 + maxWait。
|
|
1466
|
+
// ① 过滤:整个 document.body 的 childList 海量 mutation(流式文本、
|
|
1467
|
+
// chunk 重排)里,绝大多数与「打标 / 藏行」无关。只认本插件真正
|
|
1468
|
+
// 关心的四类节点。🔴 判据宁可**漏判放行**(多扫一次 ~0.4ms)也不可
|
|
1469
|
+
// 误杀 —— 漏了就是「遮蔽行不更新」。新增子树可能是祖先容器,
|
|
1470
|
+
// matches() 与 querySelector() 两者都试;过滤自身抛错一律放行。
|
|
1471
|
+
// ② 防抖 trailing 200ms + maxWait 1000ms:纯 trailing 在连续 mutation
|
|
1472
|
+
// 下会**无限推迟**,流式输出时新挂载的遮蔽行会长时间保持可见 ——
|
|
1473
|
+
// 这是正确性问题,不只是性能。
|
|
1474
|
+
// ③ attributeFilter 只盯 data-chat-node-key:本插件不写该属性(写的是
|
|
1475
|
+
// data-mops-seq / hidden),所以不会被自己触发成环。
|
|
1476
|
+
var MO_SEL = '[data-chat-node-key],[data-chat-flow-kind="user"],[class*="xzv4MW_actions"],[class*="_userRow"]'
|
|
1477
|
+
var scanPendingSince = 0
|
|
1478
|
+
var scheduleScan = () => {
|
|
1479
|
+
var now = Date.now()
|
|
1480
|
+
if (!scan._t) {
|
|
1481
|
+
scanPendingSince = now
|
|
1482
|
+
} else if (now - scanPendingSince >= 1000) {
|
|
1483
|
+
clearTimeout(scan._t)
|
|
1484
|
+
scan._t = null
|
|
1485
|
+
scanPendingSince = 0
|
|
1486
|
+
scan()
|
|
1487
|
+
return
|
|
1488
|
+
}
|
|
1489
|
+
clearTimeout(scan._t)
|
|
1490
|
+
scan._t = setTimeout(() => { scan._t = null; scanPendingSince = 0; scan() }, 200)
|
|
1491
|
+
}
|
|
1492
|
+
var mo = new MutationObserver((records) => {
|
|
1493
|
+
try {
|
|
1494
|
+
for (var i = 0; i < records.length; i++) {
|
|
1495
|
+
var rec = records[i]
|
|
1496
|
+
if (rec.type === 'attributes') {
|
|
1497
|
+
var tgt = rec.target
|
|
1498
|
+
if (tgt && tgt.nodeType === 1 && typeof tgt.matches === 'function' && tgt.matches(MO_SEL)) { scheduleScan(); return }
|
|
1499
|
+
continue
|
|
1500
|
+
}
|
|
1501
|
+
var added = rec.addedNodes
|
|
1502
|
+
for (var j = 0; j < added.length; j++) {
|
|
1503
|
+
var n = added[j]
|
|
1504
|
+
if (!n || n.nodeType !== 1) continue
|
|
1505
|
+
if ((typeof n.matches === 'function' && n.matches(MO_SEL))
|
|
1506
|
+
|| (typeof n.querySelector === 'function' && n.querySelector(MO_SEL))) { scheduleScan(); return }
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
} catch { scheduleScan() }
|
|
1510
|
+
})
|
|
1511
|
+
mo.observe(document.body, {
|
|
1512
|
+
childList: true, subtree: true,
|
|
1513
|
+
attributes: true, attributeFilter: ['data-chat-node-key'],
|
|
1514
|
+
})
|
|
1331
1515
|
} catch { /* noop */ }
|
|
1332
1516
|
window.addEventListener(CHANGED_EVENT, () => {
|
|
1333
1517
|
try {
|
|
@@ -1500,13 +1684,14 @@ window.__ModuleLoader__.load({
|
|
|
1500
1684
|
*/
|
|
1501
1685
|
var afterWrite = (rawSid, okMsg) => {
|
|
1502
1686
|
notifyDone(okMsg)
|
|
1503
|
-
|
|
1687
|
+
// emitChanged = 作废消息读缓存 + 清 rowsCache TTL + 广播 CHANGED
|
|
1688
|
+
// (dock 重拉 / 已打标 seq 摘牌),一次写只做一次。
|
|
1689
|
+
emitChanged()
|
|
1504
1690
|
try {
|
|
1505
1691
|
if (__uiWorkspace && typeof __uiWorkspace.openSession === 'function') __uiWorkspace.openSession(rawSid)
|
|
1506
1692
|
} catch (e) {
|
|
1507
1693
|
if (window.__MOPS_DEBUG) console.warn('[message-ops] openSession failed:', e)
|
|
1508
1694
|
}
|
|
1509
|
-
if (__peekRowsCache) { const c = __peekRowsCache(); if (c) c.at = 0 }
|
|
1510
1695
|
const bare = bareSessionId(rawSid)
|
|
1511
1696
|
for (const delay of [150, 700, 1600]) setTimeout(() => { if (__refreshSurface) __refreshSurface(bare) }, delay)
|
|
1512
1697
|
}
|
|
@@ -1514,9 +1699,7 @@ window.__ModuleLoader__.load({
|
|
|
1514
1699
|
/**
|
|
1515
1700
|
* 回填 composer(opencode 语义:回滚后把被删掉的那句原文还给输入框)。
|
|
1516
1701
|
*
|
|
1517
|
-
* 0.9.6 统一出口 ——
|
|
1518
|
-
* (对话框 :403 与 /undo :1495),悬停回滚按钮、AI 时钟按钮、dock Restore、
|
|
1519
|
-
* /redo **全部不回填**,表现正是「回滚成功了但消息框是空的」。
|
|
1702
|
+
* 0.9.6 统一出口 —— 此前「5 个入口只有 2 个回填」的事故史见 README「历史事故记录」#5。
|
|
1520
1703
|
*
|
|
1521
1704
|
* 取文本规则:rowsCache 里 seq <= boundary 的最后一条带 fullText 的行。
|
|
1522
1705
|
* - /undo → boundary 就是被回滚的那条用户消息 → 取到它自己 ✓
|
|
@@ -1574,10 +1757,10 @@ window.__ModuleLoader__.load({
|
|
|
1574
1757
|
* (dock 传的 row.seq 就来自 activeMarkers)。传消息 seq 会找不到 marker 空转。
|
|
1575
1758
|
*/
|
|
1576
1759
|
var runRedo = () => {
|
|
1577
|
-
const
|
|
1760
|
+
const rawSid = __currentSessionId
|
|
1761
|
+
const sid = bareSessionId(rawSid)
|
|
1578
1762
|
if (!sid) return false
|
|
1579
|
-
|
|
1580
|
-
.then((r) => (r.ok ? r.json() : null))
|
|
1763
|
+
fetchMessagesJson('/api/message-ops/messages?sessionId=' + encodeURIComponent(sid))
|
|
1581
1764
|
.then((d) => {
|
|
1582
1765
|
if (!d || !d.ok) throw new Error((d && d.error) || 'messages unavailable')
|
|
1583
1766
|
const markers = activeMarkers(d.messages || [])
|
|
@@ -1589,7 +1772,9 @@ window.__ModuleLoader__.load({
|
|
|
1589
1772
|
}).then(async (res) => {
|
|
1590
1773
|
const data = await res.json().catch(() => ({}))
|
|
1591
1774
|
if (!res.ok || !data.ok) throw new Error(data.error || ('HTTP ' + res.status))
|
|
1592
|
-
afterWrite
|
|
1775
|
+
// afterWrite 契约:rawSid 必须带 session- 前缀(传裸 uuid 会抛
|
|
1776
|
+
// `sessions.retain: unknown session` 并被外层 catch 静默吞掉)
|
|
1777
|
+
afterWrite(rawSid, __t('undo.done.redo'))
|
|
1593
1778
|
})
|
|
1594
1779
|
})
|
|
1595
1780
|
.catch((reason) => notifyDone(__t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)), 'error'))
|