@240xu/dsh-message-ops 0.47.3 → 0.48.1

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 CHANGED
@@ -2050,7 +2050,12 @@ applyScriptLive`),replace 语义由 `applySurfaceReplace` 唯一拥有。旧
2050
2050
 
2051
2051
  基准:`npm run bench:incremental`(离线合成事件,可复现增量派生成本);CI 每次在
2052
2052
  Node 22/24 上跑 20k 事件档并设 200ms 宽松上限(本地 revert 批 1.5ms / restore 批 1.9ms,
2053
- 全量重算 49ms 是增量合并的 14.4×)。
2053
+ 全量重算 49ms 是增量合并的 14.4×)。冷读路径的组件成本可用
2054
+ `npm run bench:derive -- <会话日志路径>`(需设 `DSH_HOME`)复现。
2055
+
2056
+ 0.48.0 摘要生成改为「边扫描边折叠、凑满上限即停」:`listMessages` 在 20k 事件 / 9.1MB
2057
+ 文本的会话上从 **132ms → 43ms**,冷 `/messages` 从 528ms → 473ms;新旧实现逐字符等价
2058
+ (12000 组随机对拍 + 真实语料 5892 行 0 差异)。
2054
2059
 
2055
2060
  真机验证记录见 [`docs/verification/`](docs/verification/README.md)(环境基线、复核手法、
2056
2061
  每轮逐项判定与证据)。最近一轮:0.46.1 在宿主 0.2.3-alpha.3 上的 P0 修复复验(9 项 PASS)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@240xu/dsh-message-ops",
3
- "version": "0.47.3",
3
+ "version": "0.48.1",
4
4
  "description": "DSH web 插件:消息级回滚 / 删除 / 恢复 / 分叉 / 编辑重发(补 dsh 官方没有的部分)。",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -27,6 +27,38 @@
27
27
  const HOST_CLS = {
28
28
  actions: 'xzv4MW_actions', // MessageIconActions.module.css(官方用户消息动作行)
29
29
  userRow: 'Sixlwa_userRow', // MessageItem.module.css(用户消息容器)
30
+ /**
31
+ * 动作行的 **hash-agnostic 形状**(兜底用,不是新的 hash 快照)。
32
+ *
33
+ * CSS module 产出的类名恒为 `<6 位 base36>_actions`:`xzv4MW_actions`(0.2.3 web)、
34
+ * `1KfHza_actions`(0.2.2-rc.5 Desktop,issue #10 实测)都是这个形状。
35
+ * 宿主换版本后 `actions` 失配时,靠形状在本条消息块内兜底;否则只能退回
36
+ * `mopsUserActionsRow` —— 按钮挂得上,但不跟随官方 hover 显隐。
37
+ *
38
+ * 只按**整段 class token** 比对:`_group_actions` / `n_file_actions` 这类包含
39
+ * `_actions` 子串的类名不会误命中(前缀不是 6 位 base36)。
40
+ */
41
+ actionsShape: /^[A-Za-z0-9]{6}_actions$/,
42
+ }
43
+ /** 元素是否**形状上**是宿主动作行(见 `HOST_CLS.actionsShape`)。 */
44
+ const isHostActionsRow = el => {
45
+ if (!el || el.nodeType !== 1) return false
46
+ const cls = (el.getAttribute && el.getAttribute('class')) || ''
47
+ return String(cls).split(/\s+/).some(c => HOST_CLS.actionsShape.test(c))
48
+ }
49
+ /**
50
+ * `root` 内全部候选动作行:精确 hash 命中项**排在前**(保持精确优先),
51
+ * 其后才是形状匹配的兜底项。宿主有多个 `*_actions` 类(用户动作行 / 其它行),
52
+ * 调用方仍需用 `closest(user 块)` 收窄,见 `actionsRowsIn` 的消费点。
53
+ */
54
+ const actionsRowsIn = root => {
55
+ const out = []
56
+ const exact = root.querySelectorAll('[class*="' + HOST_CLS.actions + '"]')
57
+ for (const el of exact) out.push(el)
58
+ for (const el of root.querySelectorAll('[class*="_actions"]')) {
59
+ if (out.indexOf(el) === -1 && isHostActionsRow(el)) out.push(el)
60
+ }
61
+ return out
30
62
  }
31
63
  // 0.5.2(P2):回滚/恢复成功后广播 → RevertDock 重拉标记(否则 dock 只在切会话时刷新)
32
64
  const CHANGED_EVENT = 'dsh-message-ops:changed'
@@ -98,7 +98,7 @@
98
98
  /** 行内挂载点:气泡栈之后、宿主动作行之前(动作行始终在最下面)。 */
99
99
  function mountInlineEdit(block, el, rowHint) {
100
100
  const rowEl = rowHint || block.querySelector('[class*="userRow"]') || block
101
- const acts = rowEl.querySelector('[class*="' + HOST_CLS.actions + '"]')
101
+ const acts = rowEl.querySelector('[class*="' + HOST_CLS.actions + '"]') || rowEl.querySelector('[class*="_actions"]')
102
102
  if (acts && acts.parentElement === rowEl) rowEl.insertBefore(el, acts)
103
103
  else rowEl.appendChild(el)
104
104
  return rowEl
@@ -211,7 +211,7 @@
211
211
  out.push(el)
212
212
  }
213
213
  }
214
- const rows = Array.from(document.querySelectorAll('[class*="' + HOST_CLS.actions + '"]'))
214
+ const rows = actionsRowsIn(document)
215
215
  for (const r of rows) {
216
216
  const block =
217
217
  r.closest('[data-chat-flow-kind="user"]') ||
@@ -444,6 +444,18 @@
444
444
  var ensureActionsHost = block => {
445
445
  // ① 宿主动作行(可见性跟随 hover 显隐,首选)
446
446
  let host = block.querySelector('[class*="' + HOST_CLS.actions + '"]')
447
+ // ①b hash 漂移兜底:宿主换版本后 `HOST_CLS.actions` 会 0 命中(issue #10:
448
+ // 0.2.2-rc.5 的动作行是 `1KfHza_actions`)。不兜底就只能退回 ② 的自建行 ——
449
+ // 按钮还在,但不跟随官方 hover 显隐,行高也与官方行脱节。
450
+ // 形状正则来自 `HOST_CLS.actionsShape`(单点定义,避免再散落一份 hash 知识)。
451
+ if (!host && typeof block.querySelectorAll === 'function') {
452
+ for (const el of block.querySelectorAll('[class*="_actions"]')) {
453
+ if (isHostActionsRow(el)) {
454
+ host = el
455
+ break
456
+ }
457
+ }
458
+ }
447
459
  if (host) return { host: host, created: false }
448
460
  // ② **我们上一轮建的兜底行**:必须一起查,否则第二个工厂查不到就再建一行
449
461
  // (原实现正是这样:`ensureBtn` 建了一行、`ensureEditBtn` 又建一行 →
@@ -570,7 +582,12 @@
570
582
  ensureEditBtn(block)
571
583
  // 宿主重建动作行后旧按钮可能被替换:保证标记只落在第一颗上
572
584
  if (first && !first.hasAttribute('data-mops-first')) {
573
- const marked = block.querySelector('[class*="' + HOST_CLS.actions + '"] [data-mops-first="1"]')
585
+ const rows = actionsRowsIn(block)
586
+ let marked = null
587
+ for (const row of rows) {
588
+ marked = row.querySelector('[data-mops-first="1"]')
589
+ if (marked) break
590
+ }
574
591
  if (!marked) first.setAttribute('data-mops-first', '1')
575
592
  }
576
593
  }
@@ -698,7 +715,7 @@
698
715
  // ④ `data-chat-flow-kind` / `class*` 只认 **added/removed 节点**(attributeFilter
699
716
  // 不含 className 与 flow-kind,见下方 observe 选项);hash 单点见 HOST_CLS。
700
717
  var MO_SEL =
701
- '[data-chat-node-key],[data-chat-flow-kind="user"],[class*="' + HOST_CLS.actions + '"],[class*="_userRow"],[data-turn-tail]'
718
+ '[data-chat-node-key],[data-chat-flow-kind="user"],[class*="' + HOST_CLS.actions + '"],[class*="_actions"],[class*="_userRow"],[data-turn-tail]'
702
719
  var scanPendingSince = 0
703
720
  var scheduleScan = () => {
704
721
  var now = Date.now()
package/src/client.js CHANGED
@@ -40,6 +40,38 @@ window.__ModuleLoader__.load({
40
40
  const HOST_CLS = {
41
41
  actions: 'xzv4MW_actions', // MessageIconActions.module.css(官方用户消息动作行)
42
42
  userRow: 'Sixlwa_userRow', // MessageItem.module.css(用户消息容器)
43
+ /**
44
+ * 动作行的 **hash-agnostic 形状**(兜底用,不是新的 hash 快照)。
45
+ *
46
+ * CSS module 产出的类名恒为 `<6 位 base36>_actions`:`xzv4MW_actions`(0.2.3 web)、
47
+ * `1KfHza_actions`(0.2.2-rc.5 Desktop,issue #10 实测)都是这个形状。
48
+ * 宿主换版本后 `actions` 失配时,靠形状在本条消息块内兜底;否则只能退回
49
+ * `mopsUserActionsRow` —— 按钮挂得上,但不跟随官方 hover 显隐。
50
+ *
51
+ * 只按**整段 class token** 比对:`_group_actions` / `n_file_actions` 这类包含
52
+ * `_actions` 子串的类名不会误命中(前缀不是 6 位 base36)。
53
+ */
54
+ actionsShape: /^[A-Za-z0-9]{6}_actions$/,
55
+ }
56
+ /** 元素是否**形状上**是宿主动作行(见 `HOST_CLS.actionsShape`)。 */
57
+ const isHostActionsRow = el => {
58
+ if (!el || el.nodeType !== 1) return false
59
+ const cls = (el.getAttribute && el.getAttribute('class')) || ''
60
+ return String(cls).split(/\s+/).some(c => HOST_CLS.actionsShape.test(c))
61
+ }
62
+ /**
63
+ * `root` 内全部候选动作行:精确 hash 命中项**排在前**(保持精确优先),
64
+ * 其后才是形状匹配的兜底项。宿主有多个 `*_actions` 类(用户动作行 / 其它行),
65
+ * 调用方仍需用 `closest(user 块)` 收窄,见 `actionsRowsIn` 的消费点。
66
+ */
67
+ const actionsRowsIn = root => {
68
+ const out = []
69
+ const exact = root.querySelectorAll('[class*="' + HOST_CLS.actions + '"]')
70
+ for (const el of exact) out.push(el)
71
+ for (const el of root.querySelectorAll('[class*="_actions"]')) {
72
+ if (out.indexOf(el) === -1 && isHostActionsRow(el)) out.push(el)
73
+ }
74
+ return out
43
75
  }
44
76
  // 0.5.2(P2):回滚/恢复成功后广播 → RevertDock 重拉标记(否则 dock 只在切会话时刷新)
45
77
  const CHANGED_EVENT = 'dsh-message-ops:changed'
@@ -2646,7 +2678,7 @@ window.__ModuleLoader__.load({
2646
2678
  /** 行内挂载点:气泡栈之后、宿主动作行之前(动作行始终在最下面)。 */
2647
2679
  function mountInlineEdit(block, el, rowHint) {
2648
2680
  const rowEl = rowHint || block.querySelector('[class*="userRow"]') || block
2649
- const acts = rowEl.querySelector('[class*="' + HOST_CLS.actions + '"]')
2681
+ const acts = rowEl.querySelector('[class*="' + HOST_CLS.actions + '"]') || rowEl.querySelector('[class*="_actions"]')
2650
2682
  if (acts && acts.parentElement === rowEl) rowEl.insertBefore(el, acts)
2651
2683
  else rowEl.appendChild(el)
2652
2684
  return rowEl
@@ -4142,7 +4174,7 @@ window.__ModuleLoader__.load({
4142
4174
  out.push(el)
4143
4175
  }
4144
4176
  }
4145
- const rows = Array.from(document.querySelectorAll('[class*="' + HOST_CLS.actions + '"]'))
4177
+ const rows = actionsRowsIn(document)
4146
4178
  for (const r of rows) {
4147
4179
  const block =
4148
4180
  r.closest('[data-chat-flow-kind="user"]') ||
@@ -4375,6 +4407,18 @@ window.__ModuleLoader__.load({
4375
4407
  var ensureActionsHost = block => {
4376
4408
  // ① 宿主动作行(可见性跟随 hover 显隐,首选)
4377
4409
  let host = block.querySelector('[class*="' + HOST_CLS.actions + '"]')
4410
+ // ①b hash 漂移兜底:宿主换版本后 `HOST_CLS.actions` 会 0 命中(issue #10:
4411
+ // 0.2.2-rc.5 的动作行是 `1KfHza_actions`)。不兜底就只能退回 ② 的自建行 ——
4412
+ // 按钮还在,但不跟随官方 hover 显隐,行高也与官方行脱节。
4413
+ // 形状正则来自 `HOST_CLS.actionsShape`(单点定义,避免再散落一份 hash 知识)。
4414
+ if (!host && typeof block.querySelectorAll === 'function') {
4415
+ for (const el of block.querySelectorAll('[class*="_actions"]')) {
4416
+ if (isHostActionsRow(el)) {
4417
+ host = el
4418
+ break
4419
+ }
4420
+ }
4421
+ }
4378
4422
  if (host) return { host: host, created: false }
4379
4423
  // ② **我们上一轮建的兜底行**:必须一起查,否则第二个工厂查不到就再建一行
4380
4424
  // (原实现正是这样:`ensureBtn` 建了一行、`ensureEditBtn` 又建一行 →
@@ -4501,7 +4545,12 @@ window.__ModuleLoader__.load({
4501
4545
  ensureEditBtn(block)
4502
4546
  // 宿主重建动作行后旧按钮可能被替换:保证标记只落在第一颗上
4503
4547
  if (first && !first.hasAttribute('data-mops-first')) {
4504
- const marked = block.querySelector('[class*="' + HOST_CLS.actions + '"] [data-mops-first="1"]')
4548
+ const rows = actionsRowsIn(block)
4549
+ let marked = null
4550
+ for (const row of rows) {
4551
+ marked = row.querySelector('[data-mops-first="1"]')
4552
+ if (marked) break
4553
+ }
4505
4554
  if (!marked) first.setAttribute('data-mops-first', '1')
4506
4555
  }
4507
4556
  }
@@ -4629,7 +4678,7 @@ window.__ModuleLoader__.load({
4629
4678
  // ④ `data-chat-flow-kind` / `class*` 只认 **added/removed 节点**(attributeFilter
4630
4679
  // 不含 className 与 flow-kind,见下方 observe 选项);hash 单点见 HOST_CLS。
4631
4680
  var MO_SEL =
4632
- '[data-chat-node-key],[data-chat-flow-kind="user"],[class*="' + HOST_CLS.actions + '"],[class*="_userRow"],[data-turn-tail]'
4681
+ '[data-chat-node-key],[data-chat-flow-kind="user"],[class*="' + HOST_CLS.actions + '"],[class*="_actions"],[class*="_userRow"],[data-turn-tail]'
4633
4682
  var scanPendingSince = 0
4634
4683
  var scheduleScan = () => {
4635
4684
  var now = Date.now()
@@ -354,6 +354,45 @@ export function findSessionDirs(sessionId) {
354
354
  }
355
355
 
356
356
  /** 从事件流提取消息级列表(user/assistant/system message + tool/call 摘要)。 */
357
+ /**
358
+ * 折叠空白后取前缀摘要(与 `text.replace(/\s+/g, ' ').trim().slice(0, limit)` 逐字符等价)。
359
+ *
360
+ * 0.48.0(性能):旧实现对每条消息的全文做一次全串正则替换再截断 —— 20k 事件日志实测
361
+ * 占 `listMessages` 的约 70%(9.1MB 文本 90ms / 总 132ms),而结果只取前 160 字符。
362
+ * 改为「边扫描边折叠、凑满上限即停」:常见文本只扫前约 200 字符。
363
+ * 语义必须保持:首部空白丢弃、连续空白折叠为一个空格、上限**包含**可能的尾部空格
364
+ *(旧实现先 trim 再 slice,故第 limit 位若是空格会保留)。
365
+ *
366
+ * @param {string} text 原始文本
367
+ * @param {number} [limit] 摘要字符上限(默认 160)
368
+ * @returns {string} 折叠后的摘要
369
+ */
370
+ export function collapsedSnippet(text, limit = 160) {
371
+ if (typeof text !== 'string' || text.length === 0) return ''
372
+ const isSpace = ch =>
373
+ ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r' || ch === '\f' || ch === '\v' ||
374
+ ch === '\u00a0' || ch === '\u1680' || ch === '\u2028' || ch === '\u2029' || ch === '\u202f' ||
375
+ ch === '\u205f' || ch === '\u3000' || ch === '\ufeff' || (ch >= '\u2000' && ch <= '\u200a')
376
+ let out = ''
377
+ let pendingSpace = false
378
+ for (let i = 0; i < text.length; i += 1) {
379
+ if (out.length >= limit) break
380
+ const ch = text[i]
381
+ if (isSpace(ch)) {
382
+ if (out.length > 0) pendingSpace = true
383
+ continue
384
+ }
385
+ if (pendingSpace) {
386
+ if (out.length >= limit) break
387
+ out += ' '
388
+ pendingSpace = false
389
+ if (out.length >= limit) break
390
+ }
391
+ out += ch
392
+ }
393
+ return out
394
+ }
395
+
357
396
  /**
358
397
  * turn 号 → 该 turn 首/末事件 seq 的映射。
359
398
  *
@@ -439,7 +478,7 @@ export function listMessages(events) {
439
478
  : 'system'),
440
479
  id: (msg && msg.id) || (e.data && e.data.id) || null,
441
480
  marker: isReplaceMarker,
442
- snippet: text.replace(/\s+/g, ' ').trim().slice(0, 160),
481
+ snippet: collapsedSnippet(text, 160),
443
482
  // 0.4.0 composer 回填:user 消息携带全文(revert 后 setDraft 原文)
444
483
  fullText: e.type === 'user/message' ? fullTextValue : null,
445
484
  // 0.4.1 dock:replace 标记携带范围与来源(区分手工回撤 vs compaction)