@240xu/dsh-message-ops 0.9.2 → 0.9.4
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 +88 -0
- package/package.json +1 -1
- package/src/client.js +184 -23
- package/src/index.js +5 -17
- package/test/transcript-visibility.test.js +72 -0
- package/test/undo-slash.test.js +61 -0
package/README.md
CHANGED
|
@@ -553,3 +553,91 @@ marker 自身都不渲染(`systemMessageDefinition.buildViewNode` 对 `surface
|
|
|
553
553
|
所以「恢复后再回滚」的复合场景下,重放副本的行可能不被识别为 user 行。
|
|
554
554
|
- `restore` 后界面需要触发刷新才更新(已接 `__refreshSurface` 钩子 + MutationObserver,
|
|
555
555
|
但极少数情况下仍需重载页面)。
|
|
556
|
+
|
|
557
|
+
---
|
|
558
|
+
|
|
559
|
+
## 0.9.3 · `/undo` —— 复刻 opencode 的招牌交互
|
|
560
|
+
|
|
561
|
+
「我说的那句话不对」→ 打 `/undo` → 消息从 transcript 消失 + **原文回填输入框**。
|
|
562
|
+
|
|
563
|
+
走**官方斜杠管线**(`ctx.inputTriggers.registerSource`),不自己劫持键盘,
|
|
564
|
+
所以菜单分组、搜索、Enter 裁决、composer 状态机全由宿主负责。
|
|
565
|
+
|
|
566
|
+
```js
|
|
567
|
+
ctx.inject(['inputTriggers'], (sub) => {
|
|
568
|
+
doRegisterUndoSlash(ctx, sub.inputTriggers || sub) // ⚠️ 见下方坑 2
|
|
569
|
+
})
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
### 注册路径踩过的四个坑
|
|
573
|
+
|
|
574
|
+
1. **`candidates` 必须返回 Promise**。宿主在 `InputTriggerController.fetchCandidates`
|
|
575
|
+
里直接 `source.candidates(...).then(...)`,同步返回数���会抛
|
|
576
|
+
`source.candidates(...).then is not a function`,**整个菜单静默失效**。
|
|
577
|
+
2. **cordis `inject` 回调收到的是「注入命名空间」,服务在其同名字段下**
|
|
578
|
+
(与本文件 `sub.uiWorkspace || sub` 同模式)。把 `sub` 直接当服务用会抛
|
|
579
|
+
`cannot get property "registerSource" without inject`。
|
|
580
|
+
3. **`rowsCache` 是注入器闭包私有的**(`installUserRevertInjector()` 内)。
|
|
581
|
+
`/undo` 的 `onPick` 在模块级作用域,直接引用会 ReferenceError,
|
|
582
|
+
表现为「点菜单毫无反应」。需要 `__peekRowsCache()` 出口。
|
|
583
|
+
4. **`__currentSessionId` 来自 DOM dataset,可能带 `session-` 前缀**,
|
|
584
|
+
而 API 只要裸 uuid → 请求静默失败。
|
|
585
|
+
|
|
586
|
+
### 目标必须挑 surface 上的那条
|
|
587
|
+
|
|
588
|
+
`restore` 是「重放」语义 → 原件被遮蔽、副本带**新 seq** 落在日志尾部。
|
|
589
|
+
无脑取最后一条会挑到副本,而副本不在模型 surface 上,服务端以
|
|
590
|
+
`surface replace: start seq N not found in surface` 拒绝(实测)。
|
|
591
|
+
回滚的前提就是「目标在 surface 上」,所以先筛 `visible`。
|
|
592
|
+
|
|
593
|
+
### 实测
|
|
594
|
+
|
|
595
|
+
```
|
|
596
|
+
composer 输入 /und → 官方菜单出现「回滚上一条消息 undo 移除最后一条用户消息,原文回填输入框」
|
|
597
|
+
点击 → 目标 = seq 8(surface 上的那条,不是重放副本)
|
|
598
|
+
→ 转��成功,dock 计数 13
|
|
599
|
+
→ 原文「分配几个子代理把模型广场的模型价格什么的做好…」回填进 composer ✅
|
|
600
|
+
→ transcript:24 行中 22 行 hidden,被回滚消息从正文消失 ✅
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
---
|
|
604
|
+
|
|
605
|
+
## 0.9.4 · 修「正文几乎空白」
|
|
606
|
+
|
|
607
|
+
### 症状
|
|
608
|
+
|
|
609
|
+
用户截图里 133 轮的会话正文几乎全空,只露出零星几个「工具已更新」标记。
|
|
610
|
+
|
|
611
|
+
### 根因
|
|
612
|
+
|
|
613
|
+
19 个 marker 是**离散区间**(`[9,9]`、`[10,4368]`、`[6830,8573]` …),
|
|
614
|
+
服务端把它们塌缩成 `revertFences = range.start 列表 = [10, 6830, 9829, 11429]`,
|
|
615
|
+
客户端判据 `seq >= fence`(任一命中)等价于 **`seq >= min(fences)` = `seq >= 10`**。
|
|
616
|
+
|
|
617
|
+
于是 **4792/4793 条被判为已回滚** → 全部 hidden → 正文空白。
|
|
618
|
+
|
|
619
|
+
### 量化验证(两条真实会话)
|
|
620
|
+
|
|
621
|
+
| 判据 | 隐藏数 | 与宿主 `visibleCount` 吻合? |
|
|
622
|
+
|---|---|---|
|
|
623
|
+
| `seq >= min(fence)`(事故判据)| 4792 / 4793 | ✗ 只剩 1 条可见 |
|
|
624
|
+
| 未恢复 marker 区间并集 | 2707 | 部分吻合 |
|
|
625
|
+
| **`!visible`(模型 surface)** | **2710 / 4574** | ✓ **2083 / 464,精确吻合** |
|
|
626
|
+
|
|
627
|
+
顺带纠正一处旧断言:`tool/call` **并非**永远不在 surface 上
|
|
628
|
+
—— 实测 957 条 `tool/call` 是 visible 的,`visible` 对它同样有意义。
|
|
629
|
+
|
|
630
|
+
### 修复
|
|
631
|
+
|
|
632
|
+
- **消息类行**:`hide = !visible.has(seq)`(直接问模型 surface)
|
|
633
|
+
- **回合 chrome**(`turn-tail` / `turn-process` / `turn-error` / `model-retry`):
|
|
634
|
+
`hide = !visibleTurns.has(turn)` —— 该 turn 还有可见消息就露出,
|
|
635
|
+
因为 `turn/end` 本身多半不在 surface 上
|
|
636
|
+
- 删除 `revertFences` / `m.reverted` 两个**编码了错误判据**的字段(已无消费者)
|
|
637
|
+
|
|
638
|
+
### 实测
|
|
639
|
+
|
|
640
|
+
```
|
|
641
|
+
修复前: 正文几乎空白(截图)
|
|
642
|
+
修复后: 113 行 → 可见 67 / 隐藏 46,可见字符 36,851
|
|
643
|
+
```
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -136,6 +136,13 @@ window.__ModuleLoader__.load({
|
|
|
136
136
|
'slot.quote': 'Quote to composer',
|
|
137
137
|
'quote.done': 'Quoted into composer',
|
|
138
138
|
'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
|
+
'redo.label': '恢复上一条被回滚的消息',
|
|
145
|
+
'redo.desc': '把回滚掉的消息重新放回对话',
|
|
139
146
|
'menu.ops': 'Message ops',
|
|
140
147
|
}
|
|
141
148
|
|
|
@@ -148,7 +155,11 @@ window.__ModuleLoader__.load({
|
|
|
148
155
|
// 之前 restore 路径里直接调 scan(),但 scan 定义在注入器的闭包里,
|
|
149
156
|
// dock 组件根本取不到 —— `typeof scan === 'function'` 静默跳过了,
|
|
150
157
|
// 结果「恢复后消息回来了但仍被藏 / dock 计数不变」,必须重载页面才对。
|
|
151
|
-
var __refreshSurface = null
|
|
158
|
+
var __refreshSurface = null
|
|
159
|
+
// 0.9.3:注入器闭包内 rowsCache 的只读出口。
|
|
160
|
+
// rowsCache 声明在 installUserRevertInjector() 内(闭包私有),而 /undo 的
|
|
161
|
+
// onPick 在模块级作用域 —— 直接引用会 ReferenceError,表现为点菜单毫无反应。
|
|
162
|
+
var __peekRowsCache = null // 0.6.0:会话视图会话 id(RevertDock/MsgSlotActions 刷新)
|
|
152
163
|
// messageId→seq 索引缓存(每会话一次拉取;0.3.0 assistant-actions 槽用)
|
|
153
164
|
var __seqIndexCache = new Map()
|
|
154
165
|
// messageId→全文缓存(0.5.0 消息引用用)
|
|
@@ -932,6 +943,7 @@ window.__ModuleLoader__.load({
|
|
|
932
943
|
if (window.__MOPS_USER_REVERT_INSTALLED) return
|
|
933
944
|
window.__MOPS_USER_REVERT_INSTALLED = true
|
|
934
945
|
var rowsCache = { sid: null, rows: null, at: 0 }
|
|
946
|
+
__peekRowsCache = () => rowsCache
|
|
935
947
|
var norm = (x) => String(x || '').replace(/\s+/g, '')
|
|
936
948
|
var fetchRows = (sid) => {
|
|
937
949
|
if (rowsCache.sid === sid && rowsCache.rows && Date.now() - rowsCache.at < 8000) return Promise.resolve(rowsCache)
|
|
@@ -952,7 +964,7 @@ window.__ModuleLoader__.load({
|
|
|
952
964
|
const injected = (raw) => /^<system-reminder|current\s+runtime\s+context|^<system-Reminder/i.test(String(raw || ''))
|
|
953
965
|
const rows = ((d && d.messages) || [])
|
|
954
966
|
.filter((m) => m && m.role === 'user')
|
|
955
|
-
.map((m) => ({ seq: m.seq, sn: norm(m.snippet), raw: String(m.snippet == null ? '' : m.snippet) }))
|
|
967
|
+
.map((m) => ({ seq: m.seq, sn: norm(m.snippet), raw: String(m.snippet == null ? '' : m.snippet), fullText: m.fullText ?? null, visible: m.visible !== false }))
|
|
956
968
|
.filter((r) => r.sn && !injected(r.raw))
|
|
957
969
|
// 0.9.1:同时产出 node-key 查表索引(隐藏 transcript 用)
|
|
958
970
|
const idx = buildIndex(d)
|
|
@@ -1002,15 +1014,10 @@ window.__ModuleLoader__.load({
|
|
|
1002
1014
|
var byTurnStep = new Map();
|
|
1003
1015
|
var byCallId = new Map();
|
|
1004
1016
|
var byTurn = new Map();
|
|
1005
|
-
var visible = new Set();
|
|
1006
|
-
var
|
|
1017
|
+
var visible = new Set(); // 在模型 surface 上的 seq
|
|
1018
|
+
var visibleTurns = new Set(); // 含至少一条可见消息的 turn 号
|
|
1007
1019
|
var dupMessageId = 0
|
|
1008
1020
|
var dupByMessageId = new Map();
|
|
1009
|
-
// 回合页脚/过程条锚到 turn/end 的 seq —— 那是**非消息**事件,永远不会出现在
|
|
1010
|
-
// reverted 集合里,所以隐藏判据必须直接对 fence 比较,不能用集合成员。
|
|
1011
|
-
var fences = Array.isArray(payload && payload.revertFences)
|
|
1012
|
-
? payload.revertFences.filter(function (n) { return Number.isSafeInteger(n); })
|
|
1013
|
-
: [];
|
|
1014
1021
|
for (var i = 0; i < list.length; i++) {
|
|
1015
1022
|
var m = list[i];
|
|
1016
1023
|
if (!m || typeof m.seq !== 'number') continue;
|
|
@@ -1027,21 +1034,20 @@ window.__ModuleLoader__.load({
|
|
|
1027
1034
|
dupByMessageId.set(k, arr)
|
|
1028
1035
|
byMessageId.set(k, m.seq) // 指向最新的
|
|
1029
1036
|
} else { byMessageId.set(k, m.seq) }
|
|
1030
|
-
byMessageId.set(k, m.seq);
|
|
1031
1037
|
}
|
|
1032
1038
|
if (m.type === 'assistant/message' && m.turn != null && m.step != null) {
|
|
1033
1039
|
byTurnStep.set(m.turn + ':' + m.step, m.seq);
|
|
1034
1040
|
}
|
|
1035
1041
|
if (m.type === 'tool/call' && m.id != null) byCallId.set(String(m.id), m.seq);
|
|
1036
1042
|
if (m.turn != null && !byTurn.has(String(m.turn))) byTurn.set(String(m.turn), m.seq);
|
|
1037
|
-
if (m.visible === true)
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1043
|
+
if (m.visible === true) {
|
|
1044
|
+
visible.add(m.seq);
|
|
1045
|
+
if (m.turn != null) visibleTurns.add(String(m.turn));
|
|
1046
|
+
}
|
|
1041
1047
|
}
|
|
1042
1048
|
// turnEnd 优先:它对「该 turn 内一条消息都没有」的情况仍然有效
|
|
1043
1049
|
if (turnEnd) for (const k in turnEnd) if (Object.prototype.hasOwnProperty.call(turnEnd, k)) byTurn.set(k, turnEnd[k])
|
|
1044
|
-
return { byMessageId, dupByMessageId, byTurnStep, byCallId, byTurn, visible,
|
|
1050
|
+
return { byMessageId, dupByMessageId, byTurnStep, byCallId, byTurn, visible, visibleTurns, dupMessageId, total: list.length };
|
|
1045
1051
|
};
|
|
1046
1052
|
|
|
1047
1053
|
/** node-key → { seq, all }(all = 同 id 的全部 seq,处理 restore 重放副本)。 */
|
|
@@ -1055,15 +1061,15 @@ window.__ModuleLoader__.load({
|
|
|
1055
1061
|
case 'trajectory-note': {
|
|
1056
1062
|
const hit = idx.byMessageId.get(id)
|
|
1057
1063
|
if (hit === undefined) return null
|
|
1058
|
-
return { seq: hit, all: idx.dupByMessageId.get(id) || [hit] }
|
|
1064
|
+
return { kind: parsed.kind, seq: hit, all: idx.dupByMessageId.get(id) || [hit] }
|
|
1059
1065
|
}
|
|
1060
1066
|
case 'assistant-step': {
|
|
1061
1067
|
const hit = idx.byTurnStep.get(id)
|
|
1062
|
-
return hit === undefined ? null : { seq: hit, all: [hit] }
|
|
1068
|
+
return hit === undefined ? null : { kind: parsed.kind, seq: hit, all: [hit] }
|
|
1063
1069
|
}
|
|
1064
1070
|
case 'tool-call': {
|
|
1065
1071
|
const hit = idx.byCallId.get(id)
|
|
1066
|
-
return hit === undefined ? null : { seq: hit, all: [hit] }
|
|
1072
|
+
return hit === undefined ? null : { kind: parsed.kind, seq: hit, all: [hit] }
|
|
1067
1073
|
}
|
|
1068
1074
|
case 'turn-tail':
|
|
1069
1075
|
case 'turn-process':
|
|
@@ -1071,7 +1077,9 @@ window.__ModuleLoader__.load({
|
|
|
1071
1077
|
case 'turn-max-tokens':
|
|
1072
1078
|
case 'model-retry': {
|
|
1073
1079
|
const hit = idx.byTurn.get(id)
|
|
1074
|
-
|
|
1080
|
+
// 这类是**回合 chrome**,id 就是 turn 号 —— 判据要按「这个 turn 还有
|
|
1081
|
+
// 没有可见消息」而不是按 seq,因为 turn/end 本身多半不在 surface 上。
|
|
1082
|
+
return hit === undefined ? null : { kind: parsed.kind, seq: hit, all: [hit], turn: id }
|
|
1075
1083
|
}
|
|
1076
1084
|
default:
|
|
1077
1085
|
return null; // compaction / command / workflow-run 等不参与
|
|
@@ -1093,10 +1101,21 @@ window.__ModuleLoader__.load({
|
|
|
1093
1101
|
var hidden = 0, unresolved = 0, shown = 0;
|
|
1094
1102
|
for (var i = 0; i < rows.length; i++) {
|
|
1095
1103
|
var row = rows[i];
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1104
|
+
// 0.9.4 判据(见下):**模型 surface**。此前用 revertFences 塌缩成
|
|
1105
|
+
// `seq >= min(fences)`,在一个有 19 个**离散** marker 的会话里
|
|
1106
|
+
// min fence=10 → seq>=10 全藏 → 4792/4793 全被隐藏 → 正文几乎空白
|
|
1107
|
+
// (截图复现)。改用 visible 后与宿主 visibleCount 精确吻合。
|
|
1108
|
+
var hit = seqOfNodeKey(row.getAttribute('data-chat-node-key'), idx);
|
|
1109
|
+
if (hit === null) { unresolved++; row.removeAttribute('hidden'); continue; }
|
|
1110
|
+
var shouldHide;
|
|
1111
|
+
if (hit.turn !== undefined) {
|
|
1112
|
+
// 回合 chrome:这个 turn 还有可见消息就露出来
|
|
1113
|
+
shouldHide = !idx.visibleTurns.has(String(hit.turn));
|
|
1114
|
+
} else {
|
|
1115
|
+
// 消息类:直接问「它在模型 surface 上吗」
|
|
1116
|
+
shouldHide = !idx.visible.has(hit.seq);
|
|
1117
|
+
}
|
|
1118
|
+
if (shouldHide) { hidden++; row.setAttribute('hidden', 'until-found'); }
|
|
1100
1119
|
else { shown++; row.removeAttribute('hidden'); }
|
|
1101
1120
|
}
|
|
1102
1121
|
return { hidden, unresolved, shown };
|
|
@@ -1396,6 +1415,147 @@ window.__ModuleLoader__.load({
|
|
|
1396
1415
|
} catch { /* namespace already registered: keep existing copy */ }
|
|
1397
1416
|
}
|
|
1398
1417
|
|
|
1418
|
+
|
|
1419
|
+
// ═══ 0.9.3 /undo —— opencode 招牌交互 ═════════════════════════════════════
|
|
1420
|
+
//
|
|
1421
|
+
// 「我说的那句话不对」→ 一个命令 → 消息从 transcript 消失 + 原文回输入框。
|
|
1422
|
+
// 走**官方斜杠命令管线**(`ctx.inputTriggers.registerSource`),不自己劫持键盘,
|
|
1423
|
+
// 因此:菜单分组、搜索、Enter 裁决、composer 状态机全部由宿主负责。
|
|
1424
|
+
//
|
|
1425
|
+
// 契约(dsh-client-ui-input-trigger/lib/types/client/contract.d.ts):
|
|
1426
|
+
// registerSource({ trigger:'/', name, candidates(session,req), onPick(pick),
|
|
1427
|
+
// matchEnter?(session,line,signal,envelope) })
|
|
1428
|
+
// PickOutcome = {claim} | {insert} | {text,continue?} | 'handled' | undefined
|
|
1429
|
+
// undefined = 「不是我的」,交回管线;'handled' = 已消费,**不要**当成消���发出去。
|
|
1430
|
+
var UNDO_CANDIDATES = [
|
|
1431
|
+
{ name: 'undo', value: 'undo', icon: 'undo' },
|
|
1432
|
+
{ name: 'redo', value: 'redo', icon: 'redo' },
|
|
1433
|
+
]
|
|
1434
|
+
var __undoSourceName = 'message-ops'
|
|
1435
|
+
|
|
1436
|
+
/**
|
|
1437
|
+
* 找到当前会话里最后一条**可回滚**的用户消息。
|
|
1438
|
+
*
|
|
1439
|
+
* ⚠️ 必须挑 surface 上的那一条。`restore` 是「重放」语义 → 原件被遮蔽、副本带**新 seq**
|
|
1440
|
+
* 落在日志尾部。若无脑取最后一条,就会挑到那个副本,而副本不在模型 surface 上,
|
|
1441
|
+
* 服务端会以 `surface replace: start seq N not found in surface` 拒绝(实测)。
|
|
1442
|
+
* 回滚的前提就是「目标在 surface 上」,所以先筛 visible。
|
|
1443
|
+
*/
|
|
1444
|
+
var lastRevertable = () => {
|
|
1445
|
+
const cache = __peekRowsCache ? __peekRowsCache() : null
|
|
1446
|
+
if (!cache || !Array.isArray(cache.rows)) return null
|
|
1447
|
+
for (var i = cache.rows.length - 1; i >= 0; i--) {
|
|
1448
|
+
var r = cache.rows[i]
|
|
1449
|
+
if (r && r.seq != null && r.visible) return r
|
|
1450
|
+
}
|
|
1451
|
+
return null
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
/** 执行 undo/redo。返回 true 表示确实动过盘。 */
|
|
1455
|
+
/** __currentSessionId 来自 DOM dataset,可能带 `session-` 前缀;API 只要裸 uuid。 */
|
|
1456
|
+
var bareSessionId = (id) => String(id || '').replace(/^session-/, '')
|
|
1457
|
+
var runUndoRedo = (kind) => {
|
|
1458
|
+
const sid = bareSessionId(__currentSessionId)
|
|
1459
|
+
if (!sid) return false
|
|
1460
|
+
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, {
|
|
1467
|
+
method: 'POST', headers: { 'content-type': 'application/json' },
|
|
1468
|
+
body: JSON.stringify({ sessionId: sid, seq: target.seq }),
|
|
1469
|
+
})
|
|
1470
|
+
.then(async (res) => {
|
|
1471
|
+
let data = {}
|
|
1472
|
+
try { data = await res.json() } catch { /* keep {} */ }
|
|
1473
|
+
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'))
|
|
1475
|
+
// opencode 语义:回滚后**原文回输入框**,用户可改后重发
|
|
1476
|
+
if (kind !== 'redo' && target.fullText && __inputActions && typeof __inputActions.setDraft === 'function') {
|
|
1477
|
+
try { __inputActions.setDraft(target.fullText) } catch { /* 回填失败不阻断 */ }
|
|
1478
|
+
}
|
|
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
|
+
})
|
|
1486
|
+
.catch((reason) => {
|
|
1487
|
+
notifyDone(__t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)), 'error')
|
|
1488
|
+
})
|
|
1489
|
+
return true
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
/** 注册 /undo /redo 到官方斜杠管线。服务缺失时安静 no-op。 */
|
|
1493
|
+
var __undoRegistered = false
|
|
1494
|
+
var doRegisterUndoSlash = (ctx, it) => {
|
|
1495
|
+
if (__undoRegistered || !it || typeof it.registerSource !== 'function') return
|
|
1496
|
+
__undoRegistered = true
|
|
1497
|
+
try {
|
|
1498
|
+
ctx.effect(() => it.registerSource({
|
|
1499
|
+
trigger: '/',
|
|
1500
|
+
name: __undoSourceName,
|
|
1501
|
+
order: 20,
|
|
1502
|
+
showGroupTitle: true,
|
|
1503
|
+
// ⚠️ 必须返回 **Promise**:宿主在 InputTriggerController.fetchCandidates 里
|
|
1504
|
+
// 直接 `source.candidates(...).then(...)`,同步返回数组会抛
|
|
1505
|
+
// `source.candidates(...).then is not a function`,整个菜单静默失效。
|
|
1506
|
+
candidates: async (session, req) => {
|
|
1507
|
+
const q = String((req && req.query) || '').trim().toLowerCase()
|
|
1508
|
+
return UNDO_CANDIDATES
|
|
1509
|
+
.filter((c) => !q || c.name.includes(q))
|
|
1510
|
+
.map((c) => ({
|
|
1511
|
+
name: c.name,
|
|
1512
|
+
icon: c.icon,
|
|
1513
|
+
label: c.name === 'undo'
|
|
1514
|
+
? (__t('undo.label') || '回滚上一条消息')
|
|
1515
|
+
: (__t('redo.label') || '恢复上一条被回滚的消息'),
|
|
1516
|
+
description: c.name === 'undo'
|
|
1517
|
+
? (__t('undo.desc') || '移除最后一条用户消息,原文回填输入框')
|
|
1518
|
+
: (__t('redo.desc') || '把回滚掉的消息重新放回对话'),
|
|
1519
|
+
value: c.value,
|
|
1520
|
+
}))
|
|
1521
|
+
},
|
|
1522
|
+
onPick: (pick) => {
|
|
1523
|
+
const v = pick && pick.candidate && pick.candidate.value
|
|
1524
|
+
if (v !== 'undo' && v !== 'redo') return undefined
|
|
1525
|
+
runUndoRedo(v)
|
|
1526
|
+
return 'handled'
|
|
1527
|
+
},
|
|
1528
|
+
// Enter 直接执行:`/undo` 不必先在菜单里选中
|
|
1529
|
+
matchEnter: (session, line) => {
|
|
1530
|
+
const t = String(line || '').trim().toLowerCase()
|
|
1531
|
+
if (t !== '/undo' && t !== '/redo') return undefined
|
|
1532
|
+
runUndoRedo(t === '/redo' ? 'redo' : 'undo')
|
|
1533
|
+
return 'handled'
|
|
1534
|
+
},
|
|
1535
|
+
}))
|
|
1536
|
+
} catch (e) {
|
|
1537
|
+
__undoRegistered = false
|
|
1538
|
+
if (window.__MOPS_DEBUG) console.warn('[message-ops] /undo 注册失败:', e)
|
|
1539
|
+
}
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
var registerUndoSlash = (ctx) => {
|
|
1543
|
+
// ⚠️ cordis 的 inject 回调**第一个参数是服务本身**,不是 ctx。
|
|
1544
|
+
// 早先写成 `registerUndoSlash(sub.ctx || sub)` → 在服务对象上找 .get
|
|
1545
|
+
// 必然是 undefined → 静默不注册(表现为 /undo 根本不出现)。
|
|
1546
|
+
const it = typeof ctx.get === 'function' ? ctx.get('inputTriggers') : null
|
|
1547
|
+
if (it) { doRegisterUndoSlash(ctx, it); return }
|
|
1548
|
+
try {
|
|
1549
|
+
// ⚠️ cordis inject 回调收到的是**注入命名空间**,服务在其同名字段下
|
|
1550
|
+
// (与本文件 `ctx.inject(['uiWorkspace'], sub => sub.uiWorkspace || sub)` 同模式)。
|
|
1551
|
+
// 直接把 sub 当服务用会抛 `cannot get property "registerSource" without inject`。
|
|
1552
|
+
ctx.inject(['inputTriggers'], (sub) => {
|
|
1553
|
+
doRegisterUndoSlash(ctx, sub && (sub.inputTriggers || sub))
|
|
1554
|
+
})
|
|
1555
|
+
} catch (e) {
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1399
1559
|
function apply(ctx) {
|
|
1400
1560
|
__sessionsSvc = ctx.get('sessions')
|
|
1401
1561
|
__uiWorkspace = typeof ctx.get === 'function' ? ctx.get('uiWorkspace') : null
|
|
@@ -1405,6 +1565,7 @@ window.__ModuleLoader__.load({
|
|
|
1405
1565
|
if (!__sessionsSvc) {
|
|
1406
1566
|
ctx.inject(['sessions'], (sub) => { __sessionsSvc = sub.sessions })
|
|
1407
1567
|
}
|
|
1568
|
+
registerUndoSlash(ctx)
|
|
1408
1569
|
adoptLocale(ctx.get('locale'), ctx)
|
|
1409
1570
|
if (!__locale) {
|
|
1410
1571
|
ctx.inject(['locale'], (sub) => {
|
package/src/index.js
CHANGED
|
@@ -298,22 +298,11 @@ async function opsList(targetCtx, sessionId) {
|
|
|
298
298
|
}));
|
|
299
299
|
}
|
|
300
300
|
|
|
301
|
-
// 0.9.
|
|
302
|
-
//
|
|
303
|
-
//
|
|
304
|
-
//
|
|
305
|
-
//
|
|
306
|
-
// 照抄会把**所有**工具调用行永久藏掉(实测 3/3 全被误藏)。
|
|
307
|
-
//
|
|
308
|
-
// 正确口径:每个**未恢复**的回撤标记 range.start 之后的全部 seq 都算已回撤 ——
|
|
309
|
-
// 这正是 opencode 的 `messages.slice(0, boundaryIndex)`(边界自己也不显示)。
|
|
310
|
-
const revertFences = messages
|
|
311
|
-
.filter((m) => m && m.marker === true && m.restoreComplete === false
|
|
312
|
-
&& m.range && typeof m.range.start === 'number')
|
|
313
|
-
.map((m) => m.range.start);
|
|
314
|
-
for (const m of messages) {
|
|
315
|
-
m.reverted = typeof m.seq === 'number' && revertFences.some((start) => m.seq >= start);
|
|
316
|
-
}
|
|
301
|
+
// 0.9.4:原先这里发 `revertFences`(= 未恢复 marker 的 range.start 列表)和
|
|
302
|
+
// 逐条 `m.reverted = seq >= 某个 fence`。**两样都已删除**,因为它们把 19 个
|
|
303
|
+
// **离散** marker 区间塌缩成「seq >= 最小值」,在一个会话里导致
|
|
304
|
+
// 4792/4793 条被判为已回滚 → 客户端全藏 → 正文几乎空白(截图复现)。
|
|
305
|
+
// 正确判据是 `m.visible`(模型 surface),实测与下面的 visibleCount 精确吻合。
|
|
317
306
|
// 0.9.1:回合页脚/过程条的 node-key id 就是 turn 号,而它们**不是消息**,
|
|
318
307
|
// 无法用「首个带 turn 字段的消息」定位(该 turn 内可能一条消息都没有,
|
|
319
308
|
// 实测 turn 2 就映射不到)。这里直接给 turn/start 的 seq 映射表。
|
|
@@ -333,7 +322,6 @@ async function opsList(targetCtx, sessionId) {
|
|
|
333
322
|
total: messages.length,
|
|
334
323
|
visibleCount,
|
|
335
324
|
turnEndSeq,
|
|
336
|
-
revertFences,
|
|
337
325
|
messages,
|
|
338
326
|
};
|
|
339
327
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.9.4 transcript 隐藏判据 —— 回归「正文几乎空白」事故。
|
|
3
|
+
*
|
|
4
|
+
* 事故:会话有 19 个**离散** marker([9,9]、[10,4368]、[6830,8573] …),
|
|
5
|
+
* 服务端把它们塌缩成 revertFences = range.start 的列表 [10, 6830, 9829, 11429],
|
|
6
|
+
* 客户端判据 `seq >= min(fences)` 等价于 `seq >= 10`,
|
|
7
|
+
* 于是 4792/4793 条全被 hidden —— 截图里正文几乎空白。
|
|
8
|
+
*
|
|
9
|
+
* 正确判据 = 模型 surface(`m.visible`)。实测它与宿主 visibleCount 精确吻合:
|
|
10
|
+
* 156b9ecf: total 4793, visibleCount 2083 → !visible 应为 2710
|
|
11
|
+
* 4e10c1a2: total 5038, visibleCount 464 → !visible 应为 4574
|
|
12
|
+
*/
|
|
13
|
+
import test from 'node:test'
|
|
14
|
+
import assert from 'node:assert/strict'
|
|
15
|
+
|
|
16
|
+
/** 与 client.js 同构:隐藏判据 */
|
|
17
|
+
function decide(hit, idx) {
|
|
18
|
+
if (hit === null) return null // 查不到 → 保持可见
|
|
19
|
+
if (hit.turn !== undefined) return !idx.visibleTurns.has(String(hit.turn))
|
|
20
|
+
return !idx.visible.has(hit.seq)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
test('回归:fence 塌缩会把离散区间放大成「seq >= 最小值」', () => {
|
|
24
|
+
const fences = [10, 6830, 9829, 11429] // 实测(19 个 marker 的 range.start)
|
|
25
|
+
const total = 4793
|
|
26
|
+
const hiddenByFence = Array.from({ length: total }, (_, i) => i + 7).filter(s => fences.some(f => s >= f))
|
|
27
|
+
// seq 从 7 起;min fence=10 → 几乎全中
|
|
28
|
+
assert.ok(hiddenByFence.length >= 4790,
|
|
29
|
+
`fence 塌缩藏了 ${hiddenByFence.length}/${total} —— 正文必然空白`)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('正确判据与宿主 visibleCount 精确吻合(会话 156b9ecf)', () => {
|
|
33
|
+
const total = 4793, visibleCount = 2083
|
|
34
|
+
// 构造:前 visibleCount 条可见,其余被遮蔽
|
|
35
|
+
const visible = new Set()
|
|
36
|
+
for (let i = 0; i < visibleCount; i++) visible.add(i + 7)
|
|
37
|
+
const hidden = Array.from({ length: total }, (_, i) => i + 7).filter(s => !visible.has(s))
|
|
38
|
+
assert.equal(hidden.length, total - visibleCount, 'hidden 必须等于 total - visibleCount')
|
|
39
|
+
assert.equal(total - hidden.length, visibleCount)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('第二条会话同样吻合(4e10c1a2)', () => {
|
|
43
|
+
const total = 5038, visibleCount = 464
|
|
44
|
+
const visible = new Set()
|
|
45
|
+
for (let i = 0; i < visibleCount; i++) visible.add(i + 7)
|
|
46
|
+
const hidden = Array.from({ length: total }, (_, i) => i + 7).filter(s => !visible.has(s))
|
|
47
|
+
assert.equal(hidden.length, 4574)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('回合 chrome 按「本 turn 有无可见消息」判,不按 seq', () => {
|
|
51
|
+
const idx = { visible: new Set([7]), visibleTurns: new Set(['1']) }
|
|
52
|
+
// turn 1 有可见消息 → chrome 露出(即使 turn/end 的 seq 不在 surface 上)
|
|
53
|
+
assert.equal(decide({ kind: 'turn-tail', seq: 48, turn: '1' }, idx), false)
|
|
54
|
+
// turn 2 没有可见消息 → chrome 隐藏
|
|
55
|
+
assert.equal(decide({ kind: 'turn-tail', seq: 57, turn: '2' }, idx), true)
|
|
56
|
+
// 无可见消息的 turn 一律藏
|
|
57
|
+
const empty = { visible: new Set(), visibleTurns: new Set() }
|
|
58
|
+
assert.equal(decide({ kind: 'turn-process', seq: 9, turn: '9' }, empty), true)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('消息类:在 surface 上就露,不在就藏', () => {
|
|
62
|
+
const idx = { visible: new Set([8, 18]), visibleTurns: new Set() }
|
|
63
|
+
assert.equal(decide({ kind: 'input-message', seq: 8, all: [8] }, idx), false, '在 surface → 露')
|
|
64
|
+
assert.equal(decide({ kind: 'input-message', seq: 54, all: [54] }, idx), true, '不在 → 藏')
|
|
65
|
+
assert.equal(decide({ kind: 'tool-call', seq: 19, all: [19] }, idx), true)
|
|
66
|
+
assert.equal(decide({ kind: 'tool-call', seq: 18, all: [18] }, idx), false, 'tool/call 也可能在 surface 上(实测 957 条 visible)')
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('查不到一律保持可见(绝不误藏)', () => {
|
|
70
|
+
const idx = { visible: new Set([1]), visibleTurns: new Set() }
|
|
71
|
+
assert.equal(decide(null, idx), null)
|
|
72
|
+
})
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.9.3 /undo 的纯逻辑回归。
|
|
3
|
+
*
|
|
4
|
+
* /undo 走官方斜杠管线 `ctx.inputTriggers.registerSource`,注册路径踩过四个坑,
|
|
5
|
+
* 全部锁在这里(宿主侧的契约坑无法用单测覆盖,故此文件锁「我方判定」那一半)。
|
|
6
|
+
*/
|
|
7
|
+
import test from 'node:test'
|
|
8
|
+
import assert from 'node:assert/strict'
|
|
9
|
+
|
|
10
|
+
const bare = (id) => String(id || '').replace(/^session-/, '')
|
|
11
|
+
const pickLastVisible = (rows) => {
|
|
12
|
+
for (let i = rows.length - 1; i >= 0; i--) if (rows[i] && rows[i].seq != null && rows[i].visible) return rows[i]
|
|
13
|
+
return null
|
|
14
|
+
}
|
|
15
|
+
const shouldHide = (cands, fences, reverted) =>
|
|
16
|
+
fences.length ? cands.some((q) => fences.some((f) => q >= f)) : cands.some((q) => reverted.has(q))
|
|
17
|
+
|
|
18
|
+
test('sessionId 归一化:DOM dataset 带 session- 前缀', () => {
|
|
19
|
+
assert.equal(bare('session-0da37218-2672-48b7-adaa-7735af4adee2'), '0da37218-2672-48b7-adaa-7735af4adee2')
|
|
20
|
+
assert.equal(bare('0da37218-2672'), '0da37218-2672')
|
|
21
|
+
assert.equal(bare(null), '')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('回归:__currentSessionId 带前缀会让 API 请求静默失败', () => {
|
|
25
|
+
// 送 "session-<uuid>" 给 /api/message-ops/revert 会落不到会话目录
|
|
26
|
+
assert.notEqual(bare('session-x'), 'session-x')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('lastRevertable 必须挑 surface 上的那条', () => {
|
|
30
|
+
const rows = [
|
|
31
|
+
{ seq: 8, visible: true },
|
|
32
|
+
{ seq: 54, visible: true },
|
|
33
|
+
{ seq: 70, visible: false }, // restore 重放副本:不在模型 surface 上
|
|
34
|
+
]
|
|
35
|
+
// 无脑取最后一条 → 70 → 服务端报 "start seq 70 not found in surface"(实测���
|
|
36
|
+
assert.equal(rows[rows.length - 1].seq, 70)
|
|
37
|
+
// 正确:取最后一条 visible
|
|
38
|
+
assert.equal(pickLastVisible(rows).seq, 54)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('没有 surface 上的用户消息 → 返回 null,不发请求', () => {
|
|
42
|
+
assert.equal(pickLastVisible([{ seq: 8, visible: false }]), null)
|
|
43
|
+
assert.equal(pickLastVisible([]), null)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('fence 判定:边界及其后都藏', () => {
|
|
47
|
+
assert.equal(shouldHide([8], [8], new Set()), true)
|
|
48
|
+
assert.equal(shouldHide([7], [8], new Set()), false)
|
|
49
|
+
assert.equal(shouldHide([70], [70, 8], new Set()), true, '多个 fence 取并集')
|
|
50
|
+
assert.equal(shouldHide([54], [70, 8], new Set()), true)
|
|
51
|
+
assert.equal(shouldHide([7], [70, 8], new Set()), false)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('fence 为空时退回 reverted 集合', () => {
|
|
55
|
+
assert.equal(shouldHide([54], [], new Set([54, 63])), true)
|
|
56
|
+
assert.equal(shouldHide([8], [], new Set([54, 63])), false)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('空 fences + 空 reverted → 全部可见', () => {
|
|
60
|
+
assert.equal(shouldHide([1, 2, 3], [], new Set()), false)
|
|
61
|
+
})
|