@240xu/dsh-message-ops 0.9.4 → 0.9.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@240xu/dsh-message-ops",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "description": "DSH web 插件:消息级回滚 / 删除 / 恢复(补 dsh 官方没有的部分)。分支与导出用官方内置能力。",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/client.js CHANGED
@@ -141,6 +141,7 @@ window.__ModuleLoader__.load({
141
141
  'undo.done.undo': '已回滚上一条消息',
142
142
  'undo.done.redo': '已恢复上一条被回滚的消息',
143
143
  'undo.none': '没有可回滚的消息',
144
+ 'undo.none.redo': '没有可恢复的回滚',
144
145
  'redo.label': '恢复上一条被回滚的消息',
145
146
  'redo.desc': '把回滚掉的消息重新放回对话',
146
147
  'menu.ops': 'Message ops',
@@ -1454,41 +1455,76 @@ window.__ModuleLoader__.load({
1454
1455
  /** 执行 undo/redo。返回 true 表示确实动过盘。 */
1455
1456
  /** __currentSessionId 来自 DOM dataset,可能带 `session-` 前缀;API 只要裸 uuid。 */
1456
1457
  var bareSessionId = (id) => String(id || '').replace(/^session-/, '')
1457
- var runUndoRedo = (kind) => {
1458
+ /** 写盘成功后的统一收尾:通知、刷新、重放隐藏。 */
1459
+ var afterWrite = (sid, okMsg) => {
1460
+ notifyDone(okMsg)
1461
+ try { window.dispatchEvent(new Event(CHANGED_EVENT)) } catch { /* non-browser */ }
1462
+ try {
1463
+ if (__uiWorkspace && typeof __uiWorkspace.openSession === 'function') __uiWorkspace.openSession(sid)
1464
+ } catch { /* 视图重建失败不阻断 */ }
1465
+ if (__peekRowsCache) { const c = __peekRowsCache(); if (c) c.at = 0 }
1466
+ for (const delay of [150, 700, 1600]) setTimeout(() => { if (__refreshSurface) __refreshSurface(sid) }, delay)
1467
+ }
1468
+
1469
+ /**
1470
+ * /undo —— 回滚最后一条**在模型 surface 上**的用户消息,原文回填输入框。
1471
+ * 目标必须是 surface 上的那条:restore 是「重放」语义,副本带新 seq 落在日志
1472
+ * 尾部但不在 surface 上,服务端会以 `surface replace: start seq N not found in surface` 拒绝。
1473
+ */
1474
+ var runUndo = () => {
1458
1475
  const sid = bareSessionId(__currentSessionId)
1459
1476
  if (!sid) return false
1460
1477
  const target = lastRevertable()
1461
- if (!target) {
1462
- notifyDone(__t('undo.none') || '没有可回滚的消息', 'error')
1463
- return false
1464
- }
1465
- const verb = kind === 'redo' ? 'restore' : 'revert'
1466
- fetch('/api/message-ops/' + verb, {
1478
+ if (!target) { notifyDone(__t('undo.none') || '没有可回滚的消息', 'error'); return false }
1479
+ fetch('/api/message-ops/revert', {
1467
1480
  method: 'POST', headers: { 'content-type': 'application/json' },
1468
1481
  body: JSON.stringify({ sessionId: sid, seq: target.seq }),
1469
1482
  })
1470
1483
  .then(async (res) => {
1471
- let data = {}
1472
- try { data = await res.json() } catch { /* keep {} */ }
1484
+ const data = await res.json().catch(() => ({}))
1473
1485
  if (!res.ok || !data.ok) throw new Error(data.error || ('HTTP ' + res.status))
1474
- notifyDone(kind === 'redo' ? __t('undo.done.redo') : __t('undo.done.undo'))
1486
+ afterWrite(sid, __t('undo.done.undo'))
1475
1487
  // opencode 语义:回滚后**原文回输入框**,用户可改后重发
1476
- if (kind !== 'redo' && target.fullText && __inputActions && typeof __inputActions.setDraft === 'function') {
1488
+ if (target.fullText && __inputActions && typeof __inputActions.setDraft === 'function') {
1477
1489
  try { __inputActions.setDraft(target.fullText) } catch { /* 回填失败不阻断 */ }
1478
1490
  }
1479
- try { window.dispatchEvent(new Event(CHANGED_EVENT)) } catch { /* non-browser */ }
1480
- try {
1481
- if (__uiWorkspace && typeof __uiWorkspace.openSession === 'function') __uiWorkspace.openSession(sid)
1482
- } catch { /* 视图重建失败不阻断 */ }
1483
- if (__peekRowsCache) { const c = __peekRowsCache(); if (c) c.at = 0 }
1484
- for (const delay of [150, 700, 1600]) setTimeout(() => { if (__refreshSurface) __refreshSurface(sid) }, delay)
1485
1491
  })
1486
- .catch((reason) => {
1487
- notifyDone(__t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)), 'error')
1492
+ .catch((reason) => notifyDone(__t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)), 'error'))
1493
+ return true
1494
+ }
1495
+
1496
+ /**
1497
+ * /redo —— 恢复**最新一条未恢复的 marker**。
1498
+ *
1499
+ * ⚠️ 0.9.5 修正:先前把 `lastRevertable()`(用户**消息** seq)传给 /restore,
1500
+ * 但 `opsRestore` 走 `planRestore(events, restoreSeq)` —— 它要的是 **marker seq**
1501
+ * (dock 传的 row.seq 就来自 activeMarkers)。传消息 seq 会找不到 marker 空转。
1502
+ */
1503
+ var runRedo = () => {
1504
+ const sid = bareSessionId(__currentSessionId)
1505
+ if (!sid) return false
1506
+ fetch('/api/message-ops/messages?sessionId=' + encodeURIComponent(sid))
1507
+ .then((r) => (r.ok ? r.json() : null))
1508
+ .then((d) => {
1509
+ if (!d || !d.ok) throw new Error((d && d.error) || 'messages unavailable')
1510
+ const markers = activeMarkers(d.messages || [])
1511
+ if (!markers.length) { notifyDone(__t('undo.none.redo') || '没有可恢复的回滚', 'error'); return null }
1512
+ const mk = markers[markers.length - 1] // 最新一条未恢复标记
1513
+ return fetch('/api/message-ops/restore', {
1514
+ method: 'POST', headers: { 'content-type': 'application/json' },
1515
+ body: JSON.stringify({ sessionId: sid, seq: mk.seq }),
1516
+ }).then(async (res) => {
1517
+ const data = await res.json().catch(() => ({}))
1518
+ if (!res.ok || !data.ok) throw new Error(data.error || ('HTTP ' + res.status))
1519
+ afterWrite(sid, __t('undo.done.redo'))
1520
+ })
1488
1521
  })
1522
+ .catch((reason) => notifyDone(__t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)), 'error'))
1489
1523
  return true
1490
1524
  }
1491
1525
 
1526
+ var runUndoRedo = (kind) => (kind === 'redo' ? runRedo() : runUndo())
1527
+
1492
1528
  /** 注册 /undo /redo 到官方斜杠管线。服务缺失时安静 no-op。 */
1493
1529
  var __undoRegistered = false
1494
1530
  var doRegisterUndoSlash = (ctx, it) => {
@@ -0,0 +1,55 @@
1
+ /**
2
+ * 0.9.5 /redo 的 seq 契约 —— 回归「传了消息 seq 导致空转」。
3
+ *
4
+ * `POST /api/message-ops/restore` 的 seq 是 **marker seq**:
5
+ * opsRestore(targetCtx, sessionId, restoreSeq, ...)
6
+ * → restoreProgress(events, restoreSeq) / planRestore(events, restoreSeq, ...)
7
+ * dock 传的 row.seq 来自 activeMarkers(marker 行)。
8
+ *
9
+ * 0.9.4 之前 /redo 传的是 lastRevertable() —— 那是**用户消息** seq,
10
+ * planRestore 找不到对应 marker → 计划为空 → 恢复不发生。
11
+ */
12
+ import test from 'node:test'
13
+ import assert from 'node:assert/strict'
14
+
15
+ const activeMarkers = (msgs) => (msgs || []).filter(
16
+ (m) => m.marker && m.sourceKind !== 'compact-checkpoint' && m.restoreComplete !== true,
17
+ )
18
+
19
+ const FIXTURE = [
20
+ { seq: 8, type: 'user/message', marker: false, visible: true },
21
+ { seq: 63, type: 'system/message', marker: true, range: { start: 54, end: 54 }, restoreComplete: true },
22
+ { seq: 76, type: 'system/message', marker: true, range: { start: 70, end: 70 }, restoreComplete: false },
23
+ { seq: 90, type: 'user/message', marker: false, visible: false },
24
+ ]
25
+
26
+ test('activeMarkers 只留未恢复、且非 compact-checkpoint 的 marker', () => {
27
+ const ms = activeMarkers(FIXTURE)
28
+ assert.equal(ms.length, 1)
29
+ assert.equal(ms[0].seq, 76)
30
+ })
31
+
32
+ test('/redo 必须传 marker seq,不是消息 seq', () => {
33
+ const markers = activeMarkers(FIXTURE)
34
+ const redoTarget = markers[markers.length - 1]
35
+ assert.equal(redoTarget.seq, 76, 'redo 目标 = 最新未恢复 marker')
36
+ // 反例:旧实现取最后一条用户消息
37
+ const messageTarget = FIXTURE.filter((m) => m.type === 'user/message').slice(-1)[0]
38
+ assert.notEqual(messageTarget.seq, redoTarget.seq,
39
+ '消息 seq 与 marker seq 不同 —— 混用会让 planRestore 找不到 marker')
40
+ assert.equal(redoTarget.marker, true, 'redo 目标必须是 marker')
41
+ })
42
+
43
+ test('全部恢复完毕 → 不发请求,给出无事可做的提示', () => {
44
+ const done = FIXTURE.map((m) => ({ ...m, restoreComplete: true }))
45
+ assert.equal(activeMarkers(done).length, 0, 'activeMarkers 为空 → 应短路')
46
+ })
47
+
48
+ test('/undo 与 /redo 的目标集合互不重叠', () => {
49
+ // undo 取 surface 上最后一条用户消息;redo 取最新未恢复 marker
50
+ const undoTarget = FIXTURE.filter((m) => m.type === 'user/message' && m.visible).slice(-1)[0]
51
+ const redoTarget = activeMarkers(FIXTURE).slice(-1)[0]
52
+ assert.equal(undoTarget.seq, 8)
53
+ assert.equal(redoTarget.seq, 76)
54
+ assert.notEqual(undoTarget.seq, redoTarget.seq)
55
+ })