@240xu/dsh-message-ops 0.48.1 → 0.49.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/src/client.js CHANGED
@@ -21,6 +21,18 @@ window.__ModuleLoader__.load({
21
21
  const INPUT_DOCK_ID = 'message-ops-revert-dock'
22
22
  const DIALOG_ID = 'message-ops-dialog'
23
23
  const EVENT = 'dsh-message-ops:open'
24
+ /**
25
+ * 本次**模块求值**的身份(0.48.2)。
26
+ *
27
+ * 宿主 HMR(`dsh-client-hmr` 的 rebuilt → fiber 重建)会重新执行本 bundle,
28
+ * 得到一份全新的模块闭包,再对新的 ctx 调一次 `apply`。`window` 上的安装标记
29
+ * 因此不能是「一次性布尔」:那会让新模块误以为注入器已装好而直接返回 ——
30
+ * 新模块的 `__refreshSurface` / `__peekRowsCache` 永远是 null,而旧闭包的
31
+ * 监听器还活着、继续用旧模块的 `__currentSessionId`(切会话后就是旧 sid)。
32
+ * 带 token 的安装记录才能区分「同一模块重复 apply」(幂等)与「新模块 HMR」
33
+ * (先 dispose 旧监听再装新的),见 `installUserRevertInjector`。
34
+ */
35
+ const MODULE_TOKEN = {}
24
36
  /**
25
37
  * 宿主 CSS-module hash(**单点**,审计 §3.1)。
26
38
  *
@@ -43,15 +55,26 @@ window.__ModuleLoader__.load({
43
55
  /**
44
56
  * 动作行的 **hash-agnostic 形状**(兜底用,不是新的 hash 快照)。
45
57
  *
46
- * CSS module 产出的类名恒为 `<6 位 base36>_actions`:`xzv4MW_actions`(0.2.3 web)、
58
+ * CSS module 产出的类名恒为 `<6 位 hash>_actions`:`xzv4MW_actions`(0.2.3 web)、
47
59
  * `1KfHza_actions`(0.2.2-rc.5 Desktop,issue #10 实测)都是这个形状。
60
+ * 字符集含 `-`:宿主 0.2.3 里其它模块的 hash 实测有 `hHd-Xa_*` / `l_V-RG_*`
61
+ * 这种带连字符的写法(ui-sidebar / ui-chat 的 CSS module),所以只排除 `_`
62
+ * (`_group_actions` 这类「下划线开头的子串类名」正是要避免的误命中)。
48
63
  * 宿主换版本后 `actions` 失配时,靠形状在本条消息块内兜底;否则只能退回
49
64
  * `mopsUserActionsRow` —— 按钮挂得上,但不跟随官方 hover 显隐。
50
65
  *
51
- * 只按**整段 class token** 比对:`_group_actions` / `n_file_actions` 这类包含
52
- * `_actions` 子串的类名不会误命中(前缀不是 6 位 base36)。
66
+ * 只按**整段 class token** 比对:`_group_actions` / `n_file_actions` /
67
+ * `my-group_actions` 这类包含 `_actions` 子串的类名不会误命中。
53
68
  */
54
- actionsShape: /^[A-Za-z0-9]{6}_actions$/,
69
+ actionsShape: /^[A-Za-z0-9-]{6}_actions$/,
70
+ /**
71
+ * 用户行(`MessageItem` 容器)的 **hash-agnostic 形状**,只服务「整棵子树
72
+ * 都没有 `data-chat-node-key`」的极老宿主兜底(0.1.x)。当前宿主的锚点判据是
73
+ * `data-chat-flow-kind="user"|"steering"` + 最近的 `data-chat-node-key`,
74
+ * 不依赖这个形状;这里只把裸子串 `[class*="_userRow"]` 收紧成整段 token,
75
+ * 避免 `foo_userRowBar` 这类类名被误当成用户行。
76
+ */
77
+ userRowShape: /^[A-Za-z0-9-]{6}_userRow$/,
55
78
  }
56
79
  /** 元素是否**形状上**是宿主动作行(见 `HOST_CLS.actionsShape`)。 */
57
80
  const isHostActionsRow = el => {
@@ -59,6 +82,12 @@ window.__ModuleLoader__.load({
59
82
  const cls = (el.getAttribute && el.getAttribute('class')) || ''
60
83
  return String(cls).split(/\s+/).some(c => HOST_CLS.actionsShape.test(c))
61
84
  }
85
+ /** 元素是否**形状上**是宿主用户行(见 `HOST_CLS.userRowShape`)。 */
86
+ const isHostUserRow = el => {
87
+ if (!el || el.nodeType !== 1) return false
88
+ const cls = (el.getAttribute && el.getAttribute('class')) || ''
89
+ return String(cls).split(/\s+/).some(c => HOST_CLS.userRowShape.test(c))
90
+ }
62
91
  /**
63
92
  * `root` 内全部候选动作行:精确 hash 命中项**排在前**(保持精确优先),
64
93
  * 其后才是形状匹配的兜底项。宿主有多个 `*_actions` 类(用户动作行 / 其它行),
@@ -2677,8 +2706,13 @@ window.__ModuleLoader__.load({
2677
2706
 
2678
2707
  /** 行内挂载点:气泡栈之后、宿主动作行之前(动作行始终在最下面)。 */
2679
2708
  function mountInlineEdit(block, el, rowHint) {
2709
+ // rowHint 来自铅笔自己的祖先:批量 steering 的一个 flow 块里可能有多个 userRow,
2710
+ // 从 block 里 querySelector 会挂到上一条消息下面。
2680
2711
  const rowEl = rowHint || block.querySelector('[class*="userRow"]') || block
2681
- const acts = rowEl.querySelector('[class*="' + HOST_CLS.actions + '"]') || rowEl.querySelector('[class*="_actions"]')
2712
+ // 0.48.2:宿主动作行统一走 actionsRowsIn(精确 hash → 形状兜底,整段 token 比对)。
2713
+ // 旧实现的第二条腿是裸 `[class*="_actions"]`:hash 漂移时它可能先命中
2714
+ // `_group_actions` 这类**包含子串**的类名,把编辑区插到错误元素前面。
2715
+ const acts = actionsRowsIn(rowEl)[0] || null
2682
2716
  if (acts && acts.parentElement === rowEl) rowEl.insertBefore(el, acts)
2683
2717
  else rowEl.appendChild(el)
2684
2718
  return rowEl
@@ -3100,14 +3134,16 @@ window.__ModuleLoader__.load({
3100
3134
  /** 把焦点送回输入框:回撤/发送后那一行会被重建,焦点本来会掉到 body。 */
3101
3135
  function focusComposer() {
3102
3136
  try {
3103
- // 0.33.12:只在**当前会话**的容器里找编辑器。全文档第一个
3104
- // [contenteditable=true] 可能是隐藏的旧会话/别的编辑器。
3137
+ // 0.48.2:composer 在宿主 `[data-composer-seat]` 里(`[data-conversation-content]`
3138
+ // 的子节点),**不在** `[data-chat-flow]`(消息列)里 —— 旧实现只在消息列里
3139
+ // 找 contenteditable,恒 0 命中 → 发送成功后焦点掉到 body。
3140
+ // `[data-session-id]` 在当前宿主不存在(死分支,留作老宿主兜底)。
3105
3141
  const sid = typeof __currentSessionId === 'string' ? __currentSessionId : ''
3106
- const scope =
3107
- (sid && document.querySelector('[data-session-id="' + sid.replace(/"/g, '') + '"]')) ||
3108
- document.querySelector('[data-chat-flow]') ||
3109
- document
3110
- const ed = scope.querySelector('[contenteditable="true"]')
3142
+ const seat = document.querySelector('[data-composer-seat]')
3143
+ const ed =
3144
+ (seat && seat.querySelector('[contenteditable="true"]')) ||
3145
+ (sid && document.querySelector('[data-session-id="' + sid.replace(/"/g, '') + '"] [contenteditable="true"]')) ||
3146
+ (document.querySelector('[data-conversation-content]') || document).querySelector('[contenteditable="true"]')
3111
3147
  if (ed && typeof ed.focus === 'function') ed.focus()
3112
3148
  } catch (e) {
3113
3149
  /* 焦点失败不影响主流程 */
@@ -3332,6 +3368,11 @@ window.__ModuleLoader__.load({
3332
3368
  fullText: m.fullText ?? null,
3333
3369
  visible: m.visible !== false,
3334
3370
  shadowed: m.shadowed === true,
3371
+ // 0.48.2:恢复重放出的副本带 `source.replayedFrom`。它**仍然是**合法回滚
3372
+ // 目标(在 surface 上、是用户消息),但 UI 需要能识别「这不是真人这次说的」
3373
+ // —— 行表把标记透出来,按钮/面板才可能加「恢复副本」提示(当前不改行为)。
3374
+ replayed: m.replayed === true,
3375
+ replayedFrom: Number.isSafeInteger(m.replayedFrom) ? m.replayedFrom : null,
3335
3376
  turnStartSeq: m.turnStartSeq ?? null,
3336
3377
  }))
3337
3378
  .filter(r => r.sn && (r.sourceKind === null ? !injected(r.raw) : r.sourceKind === 'user'))
@@ -3978,8 +4019,36 @@ window.__ModuleLoader__.load({
3978
4019
  // 可见性由宿主 [data-actions-reveal] 控制。把按钮 append 进该行、与 Copy 同级同层,
3979
4020
  // 可见性天然继承宿主动作行(不再自己玩 hover)。0.6.0 的 absolute 覆盖教训见 README「历史事故记录」#2。
3980
4021
  function installUserRevertInjector() {
3981
- if (window.__MOPS_USER_REVERT_INSTALLED) return
3982
- window.__MOPS_USER_REVERT_INSTALLED = true
4022
+ // 0.48.2(HMR 回归):安装标记按**模块实例**判重,不再是布尔。
4023
+ // 同模块重复 apply(profile 重复 insert / 测试重复调用)→ 直接复用;
4024
+ // 新模块(HMR 重新求值)→ 先 dispose 旧实例的监听器/观察器/定时器再装新的。
4025
+ // 旧写法只置 true:HMR 后新模块的注入器根本不装(`__refreshSurface` 恒 null,
4026
+ // 写后不刷新),旧闭包却继续用旧模块的 `__currentSessionId` 打标 —— 切会话后
4027
+ // 整页按钮变「定位不到 seq」。
4028
+ const prevInstall = window.__MOPS_USER_REVERT_INSTALLED
4029
+ if (prevInstall && prevInstall.token === MODULE_TOKEN) return
4030
+ if (prevInstall && typeof prevInstall.dispose === 'function') {
4031
+ try {
4032
+ prevInstall.dispose()
4033
+ } catch {
4034
+ /* 旧实例清理失败不阻断新实例安装 */
4035
+ }
4036
+ }
4037
+ var disposed = false
4038
+ var disposers = []
4039
+ window.__MOPS_USER_REVERT_INSTALLED = {
4040
+ token: MODULE_TOKEN,
4041
+ dispose: () => {
4042
+ disposed = true
4043
+ for (const fn of disposers.splice(0)) {
4044
+ try {
4045
+ fn()
4046
+ } catch {
4047
+ /* 单个清理失败不影响其余 */
4048
+ }
4049
+ }
4050
+ },
4051
+ }
3983
4052
  var rowsCache = { sid: null, rows: null, at: 0, running: false }
3984
4053
  __peekRowsCache = () => rowsCache
3985
4054
 
@@ -4161,10 +4230,49 @@ window.__ModuleLoader__.load({
4161
4230
  return { hidden, unresolved, shown }
4162
4231
  }
4163
4232
 
4164
- // 用户消息锚点:优先官方 `HOST_CLS.userRow`(含 `HOST_CLS.actions` 动作行),
4165
- // 回退稳定属性 `data-chat-flow-kind="user"`(二者都在顶部 HOST_CLS 单点定义)。
4166
- // 锚点 = 每条用户消息自己的容器(一个块只算一次):
4167
- // 从官方动作行反查最近的消息容器,最贴近"一条消息"的粒度。
4233
+ /**
4234
+ * 动作行 → **块级**用户消息锚点(0.48.2)。
4235
+ *
4236
+ * 宿主把用户发言渲染成 `data-chat-flow-kind="user"`(正常发送)或
4237
+ * `"steering"`(运行中插话;宿主 CSS 里与 user 同组排版)的 flowItem,
4238
+ * 动作行在 flowItem **内部**的 `*_userRow` 里。块级锚点必须是**带
4239
+ * `data-chat-node-key` 的那一层**:打标(`data-mops-seq`)与藏行都按 node-key
4240
+ * 查表,停在中间的 `*_userRow` 上会拿到一个查不到 seq 的元素 ——
4241
+ * 旧实现正是这样:steering 消息的按钮恒 disabled(「定位不到 seq」)。
4242
+ *
4243
+ * 非用户类 flow(`question-reply` / `turn-tail` / 过程行 / 工具行)一律返回
4244
+ * null:不给内部节点挂用户动作(社区 PR #5 的判据;question-reply 的
4245
+ * sourceKind 是 `user-question-reply`,本来就不是「我发的消息」)。
4246
+ *
4247
+ * 整棵子树都没有 node-key 的极老宿主:退回类名后缀(保持 0.9.0 语义),
4248
+ * 且 `*_userRow` 用**整段 token** 形状校验(`HOST_CLS.userRowShape`),
4249
+ * 不用裸子串误命中 `foo_userRowBar`。
4250
+ */
4251
+ var userAnchorOf = row => {
4252
+ if (!row || typeof row.closest !== 'function') return null
4253
+ const keyed = row.closest('[data-chat-node-key]')
4254
+ if (keyed) {
4255
+ const flow = keyed.getAttribute('data-chat-flow-kind')
4256
+ if (flow === 'user' || flow === 'steering') return keyed
4257
+ // 没有 flow 属性的老宿主:按**命中行**的类名后缀认用户行
4258
+ // (`*_userRow` 用整段 token 形状校验,`_userStack`/`_bubble` 只作旧类名兜底)。
4259
+ if (flow == null) {
4260
+ const hit = row.closest('[class*="_userRow"], [class*="_userStack"], [class*="_bubble"]')
4261
+ const looksUser =
4262
+ !!hit && (isHostUserRow(hit) || /_userStack|_bubble/.test(String(hit.className || '')))
4263
+ if (looksUser) return keyed
4264
+ }
4265
+ return null
4266
+ }
4267
+ const rowEl = row.closest('[class*="_userRow"]')
4268
+ if (rowEl) {
4269
+ // 精确 hash 快路径 + 形状兜底(与 actionsRowsIn 的「精确优先」同一口径)。
4270
+ const tokens = String(rowEl.className || '').split(/\s+/)
4271
+ if (tokens.includes(HOST_CLS.userRow) || isHostUserRow(rowEl)) return rowEl
4272
+ }
4273
+ return row.closest('[class*="_userStack"], [class*="_bubble"]')
4274
+ }
4275
+ // 用户消息锚点:一条消息一个块,从官方动作行反查最近的 keyed flowItem。
4168
4276
  var userAnchors = () => {
4169
4277
  const out = []
4170
4278
  const seen = new Set()
@@ -4174,27 +4282,14 @@ window.__ModuleLoader__.load({
4174
4282
  out.push(el)
4175
4283
  }
4176
4284
  }
4177
- const rows = actionsRowsIn(document)
4178
- for (const r of rows) {
4179
- const block =
4180
- r.closest('[data-chat-flow-kind="user"]') ||
4181
- r.closest('[class*="_userRow"], [class*="_userStack"], [class*="_bubble"]') ||
4182
- r.parentElement
4183
- // 0.9.0:只按**稳定属性** + hash-immune 类名后缀判定,不再匹配 CSS-module
4184
- // 生成的前缀(`Sixlwa_*`)。dsh 0.2.0-rc.2 的哈希已变为 `EvIC1a_flowItem`,
4185
- // 旧前缀正则把所有块都滤掉 → anchors 为空 → 按钮拿不到 seq → 点击必失败。
4186
- //
4187
- // ⚠️ 不要把 `_flowItem` 当兜底:那是**通用**流块,官方把 branch 按钮��放在
4188
- // `data-chat-flow-kind="turn-tail"` 的 _flowItem 里(实测会把 turn-tail 当成
4189
- // 用户消息,给它挂上回滚按钮)。只用 user 属性 + user 类名后缀。
4190
- if (block) {
4191
- const flow = block.getAttribute('data-chat-flow-kind')
4192
- const isUser = flow === 'user' || /_userRow|_userStack|_bubble/.test(block.className || '')
4193
- if (isUser) add(block)
4194
- }
4285
+ for (const r of actionsRowsIn(document)) {
4286
+ const anchor = userAnchorOf(r)
4287
+ if (anchor) add(anchor)
4288
+ }
4289
+ // 回退:属性锚点(宿主没渲染动作行时也能挂上)
4290
+ for (const k of document.querySelectorAll('[data-chat-flow-kind="user"], [data-chat-flow-kind="steering"]')) {
4291
+ add(k.getAttribute('data-chat-node-key') ? k : k.closest('[data-chat-node-key]') || k)
4195
4292
  }
4196
- // 回退:属性锚点
4197
- for (const k of document.querySelectorAll('[data-chat-flow-kind="user"]')) add(k)
4198
4293
  return out
4199
4294
  }
4200
4295
  // 0.9.0:把配对结果反映到按钮可用性上。
@@ -4306,6 +4401,12 @@ window.__ModuleLoader__.load({
4306
4401
  var ensureAssign = sid =>
4307
4402
  fetchRows(sid).then(cache => {
4308
4403
  if (!cache || !cache.idx) return cache
4404
+ // 0.48.2:会话已切走就别把旧会话的 idx 应用到新会话的 DOM。
4405
+ // node-key 跨会话不重合,旧 idx 只会把新会话所有块打成 unmatched(一帧
4406
+ // 「全部禁用」),下一次 scan 才自愈;这里直接放弃,交给新会话自己的 scan。
4407
+ // 点击路径传入的 sid 是**点击那一刻**的会话:此时跳过配对不会发错 seq
4408
+ // (块的 data-mops-seq 来自该会话上一次成功配对;拿不到就报「定位不到」)。
4409
+ if (__currentSessionId && bareSessionId(sid) !== bareSessionId(__currentSessionId)) return cache
4309
4410
  const idx = cache.idx
4310
4411
  const rows = document.querySelectorAll('[data-chat-node-key]')
4311
4412
  const dataSig = idx.total + '/' + idx.shadowed.size + '/' + idx.visibleTurns.size
@@ -4568,17 +4669,23 @@ window.__ModuleLoader__.load({
4568
4669
  // 共享窗口里的新鲜载荷先把隐藏刷对,不必等兜底那一发。
4569
4670
  var refreshTimer = null
4570
4671
  var refreshDeadline = 0
4672
+ // 卸载代次:dispose(HMR 换新模块)后旧实例的尾随刷新必须作废,
4673
+ // 否则旧闭包会把旧会话的 idx 刷到新 DOM 上。放在本片段内声明,
4674
+ // 使按片段抽取执行的测试(perf-handfeel 1c/1d)也能拿到它。
4675
+ var refreshEpoch = 0
4571
4676
  __refreshSurface = sid => {
4572
4677
  const id = sid || __currentSessionId
4573
4678
  if (!id) return
4574
4679
  if (rowsCache) rowsCache.at = 0 // 强制重取权威数据
4575
4680
  const now = Date.now()
4681
+ const epoch = refreshEpoch
4576
4682
  if (!refreshTimer) refreshDeadline = now + 300
4577
4683
  clearTimeout(refreshTimer)
4578
4684
  refreshTimer = setTimeout(
4579
4685
  () => {
4580
4686
  refreshTimer = null
4581
4687
  try {
4688
+ if (epoch !== refreshEpoch) return
4582
4689
  // 会话已切走就别把旧会话的 idx 刷到新会话的 DOM 上
4583
4690
  if (bareSessionId(__currentSessionId) !== bareSessionId(id)) return
4584
4691
  invalidateMessagesMemo() // 兜底档必须是真网络,不许吃缓存
@@ -4597,6 +4704,7 @@ window.__ModuleLoader__.load({
4597
4704
  }
4598
4705
  var scan = () => {
4599
4706
  try {
4707
+ if (disposed) return
4600
4708
  const sid = __currentSessionId
4601
4709
  if (!sid) return
4602
4710
  const blocks = userAnchors()
@@ -4619,24 +4727,28 @@ window.__ModuleLoader__.load({
4619
4727
  }
4620
4728
  }
4621
4729
  // 触发:hover / 滚动 / DOM 变化 / 变更事件
4622
- document.addEventListener(
4623
- 'mouseover',
4624
- e => {
4625
- try {
4626
- if (!e || !e.target || !e.target.closest) return
4627
- if (!__currentSessionId) return
4628
- // hash 单点:HOST_CLS.userRow 不硬编码在选择器里(见顶部常量块)。
4629
- // 稳定属性 `data-chat-flow-kind="user"` 在前 —— 宿主换 hash 也不会失配。
4630
- const block = e.target.closest('[class*="' + HOST_CLS.userRow + '"], [data-chat-flow-kind="user"]')
4631
- if (!block) return
4632
- ensureUserActions(block)
4633
- if (!block.hasAttribute('data-mops-seq')) ensureAssign(__currentSessionId)
4634
- } catch {
4635
- /* noop */
4636
- }
4637
- },
4638
- { passive: true },
4639
- )
4730
+ var onMouseover = e => {
4731
+ try {
4732
+ if (disposed) return
4733
+ if (!e || !e.target || !e.target.closest) return
4734
+ if (!__currentSessionId) return
4735
+ // 0.48.2:不再把 `Sixlwa_userRow` 这个**全 hash** 写进选择器(宿主换构建即失配)。
4736
+ // 稳定属性 user/steering 在前,`_userRow` 后缀只当 hash-immune 候选;
4737
+ // 候选是否真是用户消息由 userAnchorOf 的 flow-kind 判据决定。
4738
+ const hit = e.target.closest(
4739
+ '[data-chat-flow-kind="user"], [data-chat-flow-kind="steering"], [class*="_userRow"]',
4740
+ )
4741
+ if (!hit) return
4742
+ const block = userAnchorOf(hit)
4743
+ if (!block) return
4744
+ ensureUserActions(block)
4745
+ if (!block.hasAttribute('data-mops-seq')) ensureAssign(__currentSessionId)
4746
+ } catch {
4747
+ /* noop */
4748
+ }
4749
+ }
4750
+ document.addEventListener('mouseover', onMouseover, { passive: true })
4751
+ disposers.push(() => document.removeEventListener('mouseover', onMouseover))
4640
4752
  // 0.25.0(手感):滚动走 rAF 合并。
4641
4753
  //
4642
4754
  // 改前:`scroll` 上一次事件跑一次 `scan()`,而 scan 每次要做 **3 次全文档
@@ -4663,6 +4775,7 @@ window.__ModuleLoader__.load({
4663
4775
  }
4664
4776
  }
4665
4777
  window.addEventListener('scroll', onScroll, { passive: true })
4778
+ disposers.push(() => window.removeEventListener('scroll', onScroll))
4666
4779
  try {
4667
4780
  // 0.9.7(P1-2)回调过滤 + maxWait。
4668
4781
  // ① 过滤:整个 document.body 的 childList 海量 mutation(流式文本、
@@ -4733,15 +4846,28 @@ window.__ModuleLoader__.load({
4733
4846
  attributes: true,
4734
4847
  attributeFilter: ['data-chat-node-key'],
4735
4848
  })
4849
+ disposers.push(() => mo.disconnect())
4736
4850
  } catch {
4737
4851
  /* noop */
4738
4852
  }
4739
- window.addEventListener(CHANGED_EVENT, () => {
4853
+ var onChanged = () => {
4740
4854
  try {
4741
4855
  document.querySelectorAll('[data-mops-seq]').forEach(b => b.removeAttribute('data-mops-seq'))
4742
4856
  } catch {
4743
4857
  /* noop */
4744
4858
  }
4859
+ }
4860
+ window.addEventListener(CHANGED_EVENT, onChanged)
4861
+ disposers.push(() => window.removeEventListener(CHANGED_EVENT, onChanged))
4862
+ // 卸载时清掉在途定时器:rAF 合并的滚动、300ms 兜底刷新、200ms 扫描防抖。
4863
+ // 不清的话旧闭包会在卸载后再跑一次 scan,拿旧 sid 给新会话的 DOM 打标。
4864
+ disposers.push(() => {
4865
+ rafPending = false
4866
+ refreshEpoch += 1
4867
+ if (refreshTimer) clearTimeout(refreshTimer)
4868
+ refreshTimer = null
4869
+ if (scan && scan._t) clearTimeout(scan._t)
4870
+ if (scan) scan._t = null
4745
4871
  })
4746
4872
  // 样式:与官方 action 按钮同款(28px、ghost),可见性继承宿主动作行
4747
4873
  try {
@@ -4845,8 +4971,28 @@ window.__ModuleLoader__.load({
4845
4971
 
4846
4972
  // ══ 85-sidebar · 侧栏菜单注入 ══
4847
4973
  function installSidebarOps() {
4848
- if (window.__dshMessageOpsSidebarInstalled) return
4849
- window.__dshMessageOpsSidebarInstalled = true
4974
+ // 0.48.2:与聊天注入器同一套「按模块实例判重」的安装记录。
4975
+ // HMR 重新求值后旧模块的观察器仍在跑(用旧闭包的字典/状态),必须由新模块接管。
4976
+ const prev = window.__dshMessageOpsSidebarInstalled
4977
+ if (prev && prev.token === MODULE_TOKEN) return
4978
+ if (prev && typeof prev.dispose === 'function') {
4979
+ try {
4980
+ prev.dispose()
4981
+ } catch {
4982
+ /* 旧实例清理失败不阻断新实例 */
4983
+ }
4984
+ }
4985
+ var observer = null
4986
+ window.__dshMessageOpsSidebarInstalled = {
4987
+ token: MODULE_TOKEN,
4988
+ dispose: () => {
4989
+ try {
4990
+ if (observer) observer.disconnect()
4991
+ } catch {
4992
+ /* 已断开 */
4993
+ }
4994
+ },
4995
+ }
4850
4996
  try {
4851
4997
  ensureSidebarOpsItem()
4852
4998
  } catch (e) {
@@ -4870,7 +5016,7 @@ window.__ModuleLoader__.load({
4870
5016
  }
4871
5017
  }, 0)
4872
5018
  }
4873
- var observer = new MutationObserver(function (mutations) {
5019
+ observer = new MutationObserver(function (mutations) {
4874
5020
  for (var i = 0; i < mutations.length; i++) {
4875
5021
  var added = mutations[i].addedNodes
4876
5022
  for (var j = 0; j < added.length; j++) {
@@ -4971,10 +5117,15 @@ window.__ModuleLoader__.load({
4971
5117
  // emitChanged = 作废消息读缓存 + 清 rowsCache TTL + 广播 CHANGED
4972
5118
  // (dock 重拉 / 已打标 seq 摘牌),一次写只做一次。
4973
5119
  emitChanged()
5120
+ // 0.48.2:写完成时用户可能已经切到别的会话 —— 此时不能把界面**拉回**旧会话。
5121
+ // `safeOpenSession` 是全局导航动作(`uiWorkspace.openSession`),不判当前会话
5122
+ // 就会在慢请求落定时抢走用户正在看的会话。仍停在同一会话才重建视图;
5123
+ // 当前会话未知(dock 未挂载/老宿主)时保持旧行为。
5124
+ const stillHere = !__currentSessionId || bareSessionId(__currentSessionId) === bareSessionId(rawSid)
4974
5125
  // 0.26.0:统一走 safeOpenSession。原实现只在 __MOPS_DEBUG 下才 warn,
4975
5126
  // 而这段注释本身就把「静默吞掉 → 视图重建从未生效」写成了事故档案 ——
4976
5127
  // 现在无条件 warn(排障时不必先知道要开哪个开关)。
4977
- safeOpenSession(rawSid, 'after-write')
5128
+ if (stillHere) safeOpenSession(rawSid, 'after-write')
4978
5129
  const bare = bareSessionId(rawSid)
4979
5130
  // 0.25.0(手感):三连 [150,700,1600] → 立即一次(__refreshSurface 自带
4980
5131
  // 300ms 尾随防抖)。视图重建后的「重算隐藏」兜底必须留,只是不再白等 1.6s。