@240xu/dsh-message-ops 0.10.8 → 0.11.0

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
@@ -129,6 +129,50 @@ window.__ModuleLoader__.load({
129
129
  'undo.none.redo': '没有可恢复的回滚',
130
130
  'redo.label': '恢复上一条被回滚的消息',
131
131
  'redo.desc': '把回滚掉的消息重新放回对话',
132
+ 'panel.messages': '消息',
133
+ 'panel.trash': '回收站',
134
+ 'panel.search': '搜索当前会话',
135
+ 'panel.searchHint': '搜索全文,不只是摘要',
136
+ 'panel.all': '全部',
137
+ 'panel.user': '用户',
138
+ 'panel.assistant': '助手',
139
+ 'panel.tool': '工具',
140
+ 'panel.includeShadowed': '含已回滚',
141
+ 'panel.loading': '正在读取…',
142
+ 'panel.loadFail': '读取失败:',
143
+ 'panel.searchFail': '搜索失败:',
144
+ 'panel.empty': '没有可显示的消息。',
145
+ 'panel.noMatch': '没有匹配结果。',
146
+ 'panel.selectHint': '选择一条消息,查看全文和操作。',
147
+ 'panel.detailLoading': '正在读取全文…',
148
+ 'panel.shadowed': '已回滚',
149
+ 'panel.running': '会话正在运行:回滚、删除、分支需先停止。',
150
+ 'panel.revert': '回滚到此条',
151
+ 'panel.revertHint': '包含此条及其后的可见消息',
152
+ 'panel.delete': '仅删除此条',
153
+ 'panel.deleteHint': '只遮蔽这一条,其他消息不动',
154
+ 'panel.branch': '从此条分支',
155
+ 'panel.branchHint': '复制到此条为止的内容为新会话',
156
+ 'panel.branchDone': '分支已创建:{id}',
157
+ 'panel.openNew': '打开新会话',
158
+ 'panel.revertDone': '回滚已生效',
159
+ 'panel.deleteDone': '删除已生效',
160
+ 'panel.count': '共 {n} 条 · 可见 {v} · 已回滚 {s}',
161
+ 'panel.deleteSession': '删除整条会话',
162
+ 'panel.deleteSessionHint': '移入回收站,7 天内可恢复。请输入标题确认:',
163
+ 'panel.cancel': '取消',
164
+ 'panel.confirmDelete': '确认删除',
165
+ 'panel.deleting': '删除中…',
166
+ 'panel.deleted': '会话已移入回收站',
167
+ 'panel.trashEmpty': '回收站是空的。',
168
+ 'panel.untitled': '未命名会话',
169
+ 'panel.restore': '恢复',
170
+ 'panel.purge': '永久删除',
171
+ 'panel.purgeConfirm': '再点一次永久删除',
172
+ 'panel.restored': '会话已恢复',
173
+ 'panel.purged': '回收站条目已永久删除',
174
+ 'panel.expires': '到期',
175
+ 'panel.busy': '处理中…',
132
176
  'menu.ops': '消息操作',
133
177
  }
134
178
 
@@ -189,6 +233,50 @@ window.__ModuleLoader__.load({
189
233
  'undo.none.redo': 'No revert to redo',
190
234
  'redo.label': 'Restore the last reverted message',
191
235
  'redo.desc': 'Put the reverted message back into the conversation',
236
+ 'panel.messages': 'Messages',
237
+ 'panel.trash': 'Trash',
238
+ 'panel.search': 'Search this session',
239
+ 'panel.searchHint': 'Search full text, not just snippets',
240
+ 'panel.all': 'All',
241
+ 'panel.user': 'User',
242
+ 'panel.assistant': 'Assistant',
243
+ 'panel.tool': 'Tool',
244
+ 'panel.includeShadowed': 'Include shadowed',
245
+ 'panel.loading': 'Loading…',
246
+ 'panel.loadFail': 'Failed to load: ',
247
+ 'panel.searchFail': 'Search failed: ',
248
+ 'panel.empty': 'No messages to show.',
249
+ 'panel.noMatch': 'No matches.',
250
+ 'panel.selectHint': 'Pick a message to see its full text and actions.',
251
+ 'panel.detailLoading': 'Loading full text…',
252
+ 'panel.shadowed': 'reverted',
253
+ 'panel.running': 'Session is running: stop it before revert, delete, or branch.',
254
+ 'panel.revert': 'Revert to here',
255
+ 'panel.revertHint': 'Includes this message and all visible messages after it',
256
+ 'panel.delete': 'Delete only this',
257
+ 'panel.deleteHint': 'Shadows only this message',
258
+ 'panel.branch': 'Branch from here',
259
+ 'panel.branchHint': 'Copy everything through this message into a new session',
260
+ 'panel.branchDone': 'Branched: {id}',
261
+ 'panel.openNew': 'Open new session',
262
+ 'panel.revertDone': 'Revert applied',
263
+ 'panel.deleteDone': 'Delete applied',
264
+ 'panel.count': 'Total {n} · visible {v} · shadowed {s}',
265
+ 'panel.deleteSession': 'Delete whole session',
266
+ 'panel.deleteSessionHint': 'Moves it to trash for 7 days. Type the title to confirm:',
267
+ 'panel.cancel': 'Cancel',
268
+ 'panel.confirmDelete': 'Delete',
269
+ 'panel.deleting': 'Deleting…',
270
+ 'panel.deleted': 'Session moved to trash',
271
+ 'panel.trashEmpty': 'Trash is empty.',
272
+ 'panel.untitled': 'Untitled session',
273
+ 'panel.restore': 'Restore',
274
+ 'panel.purge': 'Delete forever',
275
+ 'panel.purgeConfirm': 'Click again to delete forever',
276
+ 'panel.restored': 'Session restored',
277
+ 'panel.purged': 'Trash entry permanently deleted',
278
+ 'panel.expires': 'expires',
279
+ 'panel.busy': 'Working…',
192
280
  'menu.ops': 'Message ops',
193
281
  }
194
282
 
@@ -592,6 +680,563 @@ window.__ModuleLoader__.load({
592
680
  ])
593
681
  }
594
682
 
683
+ // --- A · 功能主义抽屉 -------------------------------------------------------
684
+
685
+ const PANEL_ROW_H = 46
686
+ const PANEL_SEARCH_LIMIT = 100
687
+
688
+ function fmtClock(value) {
689
+ if (typeof value !== 'number') return ''
690
+ try {
691
+ return new Date(value).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
692
+ } catch { return '' }
693
+ }
694
+
695
+ function roleCode(role) {
696
+ if (role === 'user') return 'U'
697
+ if (role === 'assistant') return 'A'
698
+ if (role === 'tool') return 'T'
699
+ return 'S'
700
+ }
701
+
702
+ function ensurePanelStyle() {
703
+ try {
704
+ if (document.getElementById('dsh-message-ops-panel-style')) return
705
+ const tag = document.createElement('style')
706
+ tag.id = 'dsh-message-ops-panel-style'
707
+ tag.textContent = [
708
+ '.mopsPanelScrim{position:fixed;inset:0;z-index:1200;background:rgba(15,17,21,.18);display:flex;justify-content:flex-end}',
709
+ '.mopsPanel{width:560px;max-width:100vw;height:100%;display:flex;flex-direction:column;background:var(--dsw-alias-bg-primary,#fff);color:var(--dsw-alias-label-primary,#0f1115);border-left:1px solid var(--dsw-alias-border-l2,#e6e6e6);box-shadow:-16px 0 48px rgba(15,17,21,.14);font-family:inherit;font-size:13px;line-height:1.45}',
710
+ '.mopsPanel *{box-sizing:border-box}',
711
+ '.mopsPanel button{font:inherit;color:inherit;border-radius:0}',
712
+ '.mopsPanelHead{display:flex;align-items:flex-start;justify-content:space-between;gap:12px;padding:16px 18px 12px;border-bottom:1px solid var(--dsw-alias-border-l2,#e6e6e6)}',
713
+ '.mopsPanelKicker{font-size:10px;letter-spacing:.14em;text-transform:uppercase;color:var(--dsw-alias-label-tertiary,#8a8a8e)}',
714
+ '.mopsPanelTitle{font-size:15px;font-weight:600;margin-top:2px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:420px}',
715
+ '.mopsPanelSub{font-size:11px;color:var(--dsw-alias-label-tertiary,#8a8a8e);margin-top:2px}',
716
+ '.mopsPanelClose{width:30px;height:30px;border:1px solid var(--dsw-alias-border-l2,#e6e6e6);background:transparent;cursor:pointer;font-size:16px;line-height:1;flex:none}',
717
+ '.mopsPanelClose:hover{background:var(--dsw-alias-interactive-bg-hover,rgba(15,17,21,.06))}',
718
+ '.mopsTabs{display:flex;border-bottom:1px solid var(--dsw-alias-border-l2,#e6e6e6)}',
719
+ '.mopsTab{flex:1;padding:9px 12px;border:0;border-bottom:2px solid transparent;background:transparent;cursor:pointer;font-size:12px;color:var(--dsw-alias-label-secondary,#6b6b70)}',
720
+ '.mopsTab.on{border-bottom-color:var(--dsw-alias-brand-primary,#4d6bfe);color:var(--dsw-alias-label-primary,#0f1115);font-weight:600}',
721
+ '.mopsSearchRow{display:flex;gap:8px;padding:12px 18px 8px}',
722
+ '.mopsSearch{flex:1;min-width:0;height:32px;border:1px solid var(--dsw-alias-border-l2,#d8d8d8);background:transparent;color:inherit;padding:0 9px;font:inherit;font-size:12px;outline:none}',
723
+ '.mopsSearch:focus{border-color:var(--dsw-alias-brand-primary,#4d6bfe)}',
724
+ '.mopsChips{display:flex;gap:6px;padding:0 18px 10px;flex-wrap:wrap;align-items:center}',
725
+ '.mopsChip{height:26px;padding:0 9px;border:1px solid var(--dsw-alias-border-l2,#d8d8d8);background:transparent;cursor:pointer;font-size:11px;color:var(--dsw-alias-label-secondary,#6b6b70)}',
726
+ '.mopsChip.on{border-color:var(--dsw-alias-label-primary,#0f1115);background:var(--dsw-alias-label-primary,#0f1115);color:var(--dsw-alias-bg-primary,#fff)}',
727
+ '.mopsToggle{display:inline-flex;align-items:center;gap:5px;font-size:11px;color:var(--dsw-alias-label-secondary,#6b6b70);margin-left:auto;cursor:pointer}',
728
+ '.mopsToggle input{accent-color:var(--dsw-alias-brand-primary,#4d6bfe)}',
729
+ '.mopsList{flex:1;min-height:0;overflow-y:auto;position:relative;border-top:1px solid var(--dsw-alias-border-l1,#efefef)}',
730
+ '.mopsRow{position:absolute;left:0;right:0;display:grid;grid-template-columns:44px 26px minmax(0,1fr) 48px;align-items:center;gap:8px;width:100%;border:0;border-bottom:1px solid var(--dsw-alias-border-l1,#f0f0f0);background:transparent;text-align:left;cursor:pointer;padding:0 12px 0 18px;color:inherit}',
731
+ '.mopsRow:hover{background:var(--dsw-alias-interactive-bg-hover,rgba(15,17,21,.045))}',
732
+ '.mopsRow.on{background:rgba(77,107,254,.08);box-shadow:inset 2px 0 0 var(--dsw-alias-brand-primary,#4d6bfe)}',
733
+ '.mopsRow.shadowed{opacity:.42}',
734
+ '.mopsSeq{font-variant-numeric:tabular-nums;font-size:10px;color:var(--dsw-alias-label-tertiary,#9a9aa0)}',
735
+ '.mopsRole{width:20px;height:20px;display:inline-flex;align-items:center;justify-content:center;border:1px solid var(--dsw-alias-border-l2,#d8d8d8);font-size:9px;font-weight:600;color:var(--dsw-alias-label-secondary,#6b6b70)}',
736
+ '.mopsSnippet{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:12px}',
737
+ '.mopsClock{font-size:10px;color:var(--dsw-alias-label-tertiary,#9a9aa0);text-align:right;font-variant-numeric:tabular-nums}',
738
+ '.mopsEmpty{padding:28px 18px;color:var(--dsw-alias-label-tertiary,#8a8a8e);font-size:12px}',
739
+ '.mopsDetail{flex:none;max-height:37%;overflow-y:auto;border-top:1px solid var(--dsw-alias-border-l2,#e6e6e6);padding:12px 18px 10px;background:var(--dsw-alias-bg-secondary,#fafafa)}',
740
+ '.mopsDetailMeta{display:flex;gap:8px;align-items:center;font-size:10px;color:var(--dsw-alias-label-tertiary,#8a8a8e);margin-bottom:6px}',
741
+ '.mopsDetailText{margin:0;white-space:pre-wrap;word-break:break-word;max-height:108px;overflow:auto;font-family:inherit;font-size:12px;line-height:1.55;color:var(--dsw-alias-label-primary,#0f1115)}',
742
+ '.mopsDetailHint{padding:10px 0 14px;color:var(--dsw-alias-label-tertiary,#8a8a8e);font-size:12px}',
743
+ '.mopsActions{display:flex;gap:6px;flex-wrap:wrap;margin-top:10px}',
744
+ '.mopsAction{height:30px;padding:0 10px;border:1px solid var(--dsw-alias-border-l2,#cfcfcf);background:transparent;cursor:pointer;font-size:11px}',
745
+ '.mopsAction:hover:not(:disabled){border-color:var(--dsw-alias-label-primary,#0f1115)}',
746
+ '.mopsAction.primary{border-color:var(--dsw-alias-label-primary,#0f1115);background:var(--dsw-alias-label-primary,#0f1115);color:var(--dsw-alias-bg-primary,#fff)}',
747
+ '.mopsAction.danger{border-color:var(--dsw-alias-state-error-primary,#c63b32);color:var(--dsw-alias-state-error-primary,#c63b32)}',
748
+ '.mopsAction:disabled{opacity:.35;cursor:default}',
749
+ '.mopsPanelFoot{flex:none;display:flex;align-items:center;gap:10px;justify-content:space-between;padding:10px 18px;border-top:1px solid var(--dsw-alias-border-l2,#e6e6e6);background:var(--dsw-alias-bg-primary,#fff)}',
750
+ '.mopsCount{font-size:10px;color:var(--dsw-alias-label-tertiary,#8a8a8e)}',
751
+ '.mopsDanger{border:1px solid var(--dsw-alias-state-error-primary,#c63b32);background:transparent;color:var(--dsw-alias-state-error-primary,#c63b32);height:28px;padding:0 9px;cursor:pointer;font-size:11px}',
752
+ '.mopsDanger:hover{background:rgba(198,59,50,.06)}',
753
+ '.mopsBanner{padding:7px 18px;font-size:11px;border-bottom:1px solid var(--dsw-alias-border-l1,#efefef)}',
754
+ '.mopsBanner.warn{color:var(--dsw-alias-state-warn-primary,#9a6500);background:rgba(154,101,0,.06)}',
755
+ '.mopsBanner.err{color:var(--dsw-alias-state-error-primary,#c63b32);background:rgba(198,59,50,.06)}',
756
+ '.mopsBanner.ok{color:var(--dsw-alias-state-success-primary,#2f7d4a);background:rgba(47,125,74,.06)}',
757
+ '.mopsConfirm{position:absolute;inset:0;z-index:2;background:var(--dsw-alias-bg-primary,#fff);display:flex;flex-direction:column;justify-content:center;padding:28px 24px;gap:10px}',
758
+ '.mopsConfirm h3{margin:0;font-size:15px}',
759
+ '.mopsConfirm p{margin:0;font-size:12px;color:var(--dsw-alias-label-secondary,#6b6b70)}',
760
+ '.mopsConfirm input{height:34px;border:1px solid var(--dsw-alias-border-l2,#d8d8d8);background:transparent;color:inherit;padding:0 9px;font:inherit;font-size:13px}',
761
+ '.mopsConfirmRow{display:flex;gap:8px;margin-top:4px}',
762
+ '.mopsTrashItem{display:flex;align-items:center;gap:10px;padding:11px 18px;border-bottom:1px solid var(--dsw-alias-border-l1,#f0f0f0)}',
763
+ '.mopsTrashMain{flex:1;min-width:0}',
764
+ '.mopsTrashTitle{font-size:12px;font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
765
+ '.mopsTrashMeta{font-size:10px;color:var(--dsw-alias-label-tertiary,#8a8a8e);margin-top:2px}',
766
+ '@media(max-width:760px){.mopsPanel{width:100vw;border-left:0}.mopsDetail{max-height:42%}}',
767
+ ].join('')
768
+ document.head.appendChild(tag)
769
+ } catch { /* style failure must not break the panel */ }
770
+ }
771
+
772
+ function OpsPanel(props) {
773
+ const t = (props && props.t) || __t
774
+ useLocaleRevision()
775
+ const [open, setOpen] = useState(false)
776
+ const [target, setTarget] = useState(null)
777
+ const [state, setState] = useState('idle')
778
+ const [messages, setMessages] = useState([])
779
+ const [query, setQuery] = useState('')
780
+ const [role, setRole] = useState('all')
781
+ const [includeShadowed, setIncludeShadowed] = useState(false)
782
+ const [picked, setPicked] = useState(null)
783
+ const [detail, setDetail] = useState('')
784
+ const [detailState, setDetailState] = useState('idle')
785
+ const [tab, setTab] = useState('messages')
786
+ const [hits, setHits] = useState(null)
787
+ const [searching, setSearching] = useState(false)
788
+ const [trash, setTrash] = useState([])
789
+ const [trashState, setTrashState] = useState('idle')
790
+ const [confirmDelete, setConfirmDelete] = useState(false)
791
+ const [confirmTitle, setConfirmTitle] = useState('')
792
+ const [purgeId, setPurgeId] = useState(null)
793
+ const [childId, setChildId] = useState(null)
794
+ const [busy, setBusy] = useState('')
795
+ const [error, setError] = useState(null)
796
+ const [done, setDone] = useState('')
797
+ const [scrollTop, setScrollTop] = useState(0)
798
+ const [viewport, setViewport] = useState(480)
799
+ const listRef = useRef(null)
800
+ const searchRef = useRef(null)
801
+ const detailReq = useRef(0)
802
+
803
+ useEffect(() => {
804
+ ensurePanelStyle()
805
+ const handler = (e) => {
806
+ const d = e && e.detail ? e.detail : {}
807
+ if (!d.sessionId) return
808
+ setTarget({ sessionId: d.sessionId, title: d.title || null, running: d.running === true })
809
+ setOpen(true)
810
+ setState('loading')
811
+ setMessages([])
812
+ setHits(null)
813
+ setQuery('')
814
+ setRole('all')
815
+ setIncludeShadowed(false)
816
+ setPicked(null)
817
+ setDetail('')
818
+ setTab('messages')
819
+ setTrash([])
820
+ setConfirmDelete(false)
821
+ setConfirmTitle('')
822
+ setPurgeId(null)
823
+ setChildId(null)
824
+ setError(null)
825
+ setDone('')
826
+ setScrollTop(0)
827
+ }
828
+ window.addEventListener(EVENT, handler)
829
+ return () => window.removeEventListener(EVENT, handler)
830
+ }, [])
831
+
832
+ const sid = target && target.sessionId
833
+
834
+ const loadMessages = useCallback(async (quiet) => {
835
+ if (!sid) return
836
+ if (!quiet) setState('loading')
837
+ try {
838
+ const data = await fetchMessagesJson('/api/message-ops/messages?sessionId=' + encodeURIComponent(sid) + '&view=compact')
839
+ setMessages(Array.isArray(data.messages) ? data.messages : [])
840
+ setTarget((prev) => prev ? {
841
+ ...prev,
842
+ running: data.running === true,
843
+ title: prev.title || (data.session && data.session.title) || null,
844
+ expectedTitle: (data.session && data.session.confirmTitle) || prev.expectedTitle || prev.title || prev.sessionId,
845
+ } : prev)
846
+ setState('ready')
847
+ } catch (reason) {
848
+ setError(t('panel.loadFail') + (reason && reason.message ? reason.message : String(reason)))
849
+ setState('ready')
850
+ }
851
+ }, [sid, t])
852
+
853
+ useEffect(() => {
854
+ if (!open || !sid) return
855
+ loadMessages(false)
856
+ }, [open, sid, loadMessages])
857
+
858
+ useEffect(() => {
859
+ if (!open || tab !== 'messages') return
860
+ const q = query.trim()
861
+ if (!q) { setHits(null); setSearching(false); return }
862
+ let cancelled = false
863
+ const timer = setTimeout(() => {
864
+ setSearching(true)
865
+ const url = '/api/message-ops/search?sessionId=' + encodeURIComponent(sid)
866
+ + '&q=' + encodeURIComponent(q)
867
+ + (role !== 'all' ? '&role=' + encodeURIComponent(role) : '')
868
+ + '&limit=' + PANEL_SEARCH_LIMIT
869
+ fetch(url)
870
+ .then((res) => res.json().catch(() => null).then((data) => ({ res, data })))
871
+ .then(({ res, data }) => {
872
+ if (cancelled) return
873
+ if (!res.ok || !data || data.ok !== true) throw new Error((data && data.error) || ('HTTP ' + res.status))
874
+ setHits(Array.isArray(data.results) ? data.results : [])
875
+ })
876
+ .catch((reason) => {
877
+ if (!cancelled) setError(t('panel.searchFail') + (reason && reason.message ? reason.message : String(reason)))
878
+ })
879
+ .finally(() => { if (!cancelled) setSearching(false) })
880
+ }, 200)
881
+ return () => { cancelled = true; clearTimeout(timer) }
882
+ }, [open, tab, sid, query, role, t])
883
+
884
+ const loadTrash = useCallback(async () => {
885
+ setTrashState('loading')
886
+ try {
887
+ const res = await fetch('/api/message-ops/session/trash')
888
+ const data = await res.json().catch(() => null)
889
+ if (!res.ok || !data || data.ok !== true) throw new Error((data && data.error) || ('HTTP ' + res.status))
890
+ setTrash(Array.isArray(data.entries) ? data.entries : [])
891
+ setTrashState('ready')
892
+ } catch (reason) {
893
+ setError(t('panel.loadFail') + (reason && reason.message ? reason.message : String(reason)))
894
+ setTrashState('ready')
895
+ }
896
+ }, [t])
897
+
898
+ useEffect(() => {
899
+ if (open && tab === 'trash') loadTrash()
900
+ }, [open, tab, loadTrash])
901
+
902
+ const close = useCallback(() => {
903
+ if (busy) return
904
+ setOpen(false)
905
+ setTarget(null)
906
+ setPicked(null)
907
+ setConfirmDelete(false)
908
+ }, [busy])
909
+
910
+ useEffect(() => {
911
+ if (!open) return undefined
912
+ const onKey = (e) => {
913
+ if (e.key === 'Escape') {
914
+ if (busy) return
915
+ if (confirmDelete) setConfirmDelete(false)
916
+ else close()
917
+ } else if (e.key === '/' && e.target && e.target.tagName !== 'INPUT') {
918
+ e.preventDefault()
919
+ if (searchRef.current) searchRef.current.focus()
920
+ }
921
+ }
922
+ window.addEventListener('keydown', onKey)
923
+ return () => window.removeEventListener('keydown', onKey)
924
+ }, [open, busy, confirmDelete, close])
925
+
926
+ const baseRows = query.trim() ? (hits || []) : messages
927
+ const rows = baseRows
928
+ .filter((m) => m && m.role !== 'system'
929
+ && (role === 'all' || m.role === role)
930
+ && (includeShadowed || !m.shadowed))
931
+ .sort((a, b) => b.seq - a.seq)
932
+
933
+ useEffect(() => {
934
+ if (!open || tab !== 'messages') return undefined
935
+ const el = listRef.current
936
+ if (!el) return undefined
937
+ const measure = () => setViewport(el.clientHeight || 480)
938
+ measure()
939
+ if (typeof ResizeObserver === 'undefined') return undefined
940
+ const ro = new ResizeObserver(measure)
941
+ ro.observe(el)
942
+ return () => ro.disconnect()
943
+ }, [open, tab, rows.length])
944
+
945
+ const pick = useCallback((m) => {
946
+ if (!m || busy) return
947
+ setPicked(m)
948
+ setDetail(m.snippet || '')
949
+ setDetailState('loading')
950
+ setDone('')
951
+ const req = ++detailReq.current
952
+ fetch('/api/message-ops/text?sessionId=' + encodeURIComponent(sid) + '&seq=' + encodeURIComponent(m.seq))
953
+ .then((res) => res.json().catch(() => null).then((data) => ({ res, data })))
954
+ .then(({ res, data }) => {
955
+ if (req !== detailReq.current) return
956
+ if (!res.ok || !data || data.ok !== true) throw new Error((data && data.error) || ('HTTP ' + res.status))
957
+ setDetail(typeof data.text === 'string' && data.text ? data.text : (m.snippet || ''))
958
+ setDetailState('ready')
959
+ })
960
+ .catch(() => {
961
+ if (req !== detailReq.current) return
962
+ setDetail(m.snippet || '')
963
+ setDetailState('ready')
964
+ })
965
+ }, [busy, sid])
966
+
967
+ const runAction = useCallback(async (mode) => {
968
+ if (!target || !picked || busy) return
969
+ if (target.running && mode !== 'branch') return
970
+ setBusy(mode)
971
+ setError(null)
972
+ setDone('')
973
+ try {
974
+ if (mode === 'branch') {
975
+ const forkPath = pickForkPath(__sessionsSvc)
976
+ if (forkPath.kind !== 'official') throw new Error(t('fork.unavailable'))
977
+ const created = await forkPath.fork({ sessionId: target.sessionId, atSeq: picked.seq, increaseTitle: true })
978
+ setChildId(created || null)
979
+ setDone(t('panel.branchDone', { id: String(created || '') }))
980
+ notifyDone(t('panel.branchDone', { id: String(created || '') }))
981
+ return
982
+ }
983
+ const res = await fetch('/api/message-ops/' + mode, {
984
+ method: 'POST',
985
+ headers: { 'content-type': 'application/json' },
986
+ body: JSON.stringify({ sessionId: target.sessionId, seq: picked.seq }),
987
+ })
988
+ const data = await res.json().catch(() => null)
989
+ if (!res.ok || !data || data.ok !== true) throw new Error((data && data.error) || ('HTTP ' + res.status))
990
+ if (mode === 'revert') {
991
+ setMessages((prev) => prev.map((m) => (m.seq >= picked.seq ? { ...m, shadowed: true, visible: false } : m)))
992
+ if (picked.role === 'user') {
993
+ try { refillComposer(target.sessionId, picked.seq) } catch { /* non-blocking */ }
994
+ }
995
+ } else {
996
+ setMessages((prev) => prev.map((m) => (m.seq === picked.seq ? { ...m, shadowed: true, visible: false } : m)))
997
+ }
998
+ const msg = t(mode === 'revert' ? 'panel.revertDone' : 'panel.deleteDone')
999
+ setDone(msg)
1000
+ notifyDone(msg)
1001
+ emitChanged()
1002
+ try {
1003
+ if (__uiWorkspace && typeof __uiWorkspace.openSession === 'function') __uiWorkspace.openSession(target.sessionId)
1004
+ } catch { /* view rebuild is best effort */ }
1005
+ setTimeout(() => { loadMessages(true).catch(() => {}) }, 650)
1006
+ } catch (reason) {
1007
+ setError(t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)))
1008
+ } finally {
1009
+ setBusy('')
1010
+ }
1011
+ }, [target, picked, busy, t, loadMessages])
1012
+
1013
+ const deleteSession = useCallback(async () => {
1014
+ if (!target || busy) return
1015
+ setBusy('session-delete')
1016
+ setError(null)
1017
+ setDone('')
1018
+ try {
1019
+ const res = await fetch('/api/message-ops/session/delete', {
1020
+ method: 'POST',
1021
+ headers: { 'content-type': 'application/json' },
1022
+ body: JSON.stringify({ sessionId: target.sessionId, confirmTitle: confirmTitle }),
1023
+ })
1024
+ const data = await res.json().catch(() => null)
1025
+ if (!res.ok || !data || data.ok !== true) throw new Error((data && data.error) || ('HTTP ' + res.status))
1026
+ setDone(t('panel.deleted'))
1027
+ notifyDone(t('panel.deleted'))
1028
+ emitChanged()
1029
+ setTimeout(() => { try { window.location.reload() } catch { /* non-browser guard */ } }, 700)
1030
+ } catch (reason) {
1031
+ setError(t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)))
1032
+ } finally {
1033
+ setBusy('')
1034
+ }
1035
+ }, [target, busy, confirmTitle, t])
1036
+
1037
+ const restoreEntry = useCallback(async (trashId) => {
1038
+ if (busy) return
1039
+ setBusy('restore:' + trashId)
1040
+ setError(null)
1041
+ setDone('')
1042
+ try {
1043
+ const res = await fetch('/api/message-ops/session/restore', {
1044
+ method: 'POST',
1045
+ headers: { 'content-type': 'application/json' },
1046
+ body: JSON.stringify({ trashId }),
1047
+ })
1048
+ const data = await res.json().catch(() => null)
1049
+ if (!res.ok || !data || data.ok !== true) throw new Error((data && data.error) || ('HTTP ' + res.status))
1050
+ setTrash((prev) => prev.filter((x) => x.trashId !== trashId))
1051
+ setDone(t('panel.restored'))
1052
+ notifyDone(t('panel.restored'))
1053
+ emitChanged()
1054
+ setTimeout(() => { try { window.location.reload() } catch { /* non-browser guard */ } }, 700)
1055
+ } catch (reason) {
1056
+ setError(t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)))
1057
+ } finally {
1058
+ setBusy('')
1059
+ }
1060
+ }, [busy, t])
1061
+
1062
+ const purgeEntry = useCallback(async (trashId) => {
1063
+ if (busy) return
1064
+ if (purgeId !== trashId) { setPurgeId(trashId); return }
1065
+ setBusy('purge:' + trashId)
1066
+ setError(null)
1067
+ setDone('')
1068
+ try {
1069
+ const res = await fetch('/api/message-ops/session/purge', {
1070
+ method: 'POST',
1071
+ headers: { 'content-type': 'application/json' },
1072
+ body: JSON.stringify({ trashId }),
1073
+ })
1074
+ const data = await res.json().catch(() => null)
1075
+ if (!res.ok || !data || data.ok !== true) throw new Error((data && data.error) || ('HTTP ' + res.status))
1076
+ setTrash((prev) => prev.filter((x) => x.trashId !== trashId))
1077
+ setPurgeId(null)
1078
+ setDone(t('panel.purged'))
1079
+ notifyDone(t('panel.purged'))
1080
+ } catch (reason) {
1081
+ setError(t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)))
1082
+ } finally {
1083
+ setBusy('')
1084
+ }
1085
+ }, [busy, purgeId, t])
1086
+
1087
+ if (!open) return null
1088
+
1089
+ const pickedLive = picked ? (messages.find((m) => m.seq === picked.seq) || picked) : null
1090
+ const canAct = !busy && !target.running && pickedLive && !pickedLive.shadowed
1091
+ const visibleCount = messages.filter((m) => m.role !== 'system' && m.visible && !m.shadowed).length
1092
+ const shadowedCount = messages.filter((m) => m.role !== 'system' && m.shadowed).length
1093
+ const totalCount = messages.filter((m) => m.role !== 'system').length
1094
+ const revertCount = pickedLive
1095
+ ? messages.filter((m) => m.role !== 'system' && !m.shadowed && m.seq >= pickedLive.seq).length
1096
+ : 0
1097
+ const start = Math.max(0, Math.floor(scrollTop / PANEL_ROW_H) - 6)
1098
+ const end = Math.min(rows.length, start + Math.ceil(viewport / PANEL_ROW_H) + 12)
1099
+ const windowRows = rows.slice(start, end)
1100
+ const showSearchLoading = searching && query.trim().length > 0
1101
+
1102
+ return React.createElement('div', {
1103
+ className: 'mopsPanelScrim', role: 'presentation',
1104
+ onMouseDown: (e) => { if (e.target === e.currentTarget) close() },
1105
+ },
1106
+ React.createElement('section', {
1107
+ className: 'mopsPanel', role: 'dialog', 'aria-modal': 'true', 'aria-label': t('dialog.title'),
1108
+ style: { position: 'relative' },
1109
+ },
1110
+ React.createElement('header', { className: 'mopsPanelHead' },
1111
+ React.createElement('div', { style: { minWidth: 0 } },
1112
+ React.createElement('div', { className: 'mopsPanelKicker' }, t('dialog.title')),
1113
+ React.createElement('div', { className: 'mopsPanelTitle' }, target.title || target.expectedTitle || target.sessionId),
1114
+ React.createElement('div', { className: 'mopsPanelSub' }, target.sessionId)),
1115
+ React.createElement('button', {
1116
+ type: 'button', className: 'mopsPanelClose', title: t('dialog.cancel'),
1117
+ onClick: close, disabled: !!busy,
1118
+ }, '×')),
1119
+ React.createElement('div', { className: 'mopsTabs' },
1120
+ React.createElement('button', {
1121
+ type: 'button', className: 'mopsTab' + (tab === 'messages' ? ' on' : ''),
1122
+ onClick: () => { setTab('messages'); setConfirmDelete(false) },
1123
+ }, t('panel.messages') + ' ' + totalCount),
1124
+ React.createElement('button', {
1125
+ type: 'button', className: 'mopsTab' + (tab === 'trash' ? ' on' : ''),
1126
+ onClick: () => { setTab('trash'); setConfirmDelete(false) },
1127
+ }, t('panel.trash') + (trash.length ? ' ' + trash.length : ''))),
1128
+ target.running && tab === 'messages'
1129
+ ? React.createElement('div', { className: 'mopsBanner warn' }, t('panel.running')) : null,
1130
+ error ? React.createElement('div', { className: 'mopsBanner err', role: 'alert' }, error) : null,
1131
+ done ? React.createElement('div', { className: 'mopsBanner ok' }, done,
1132
+ childId ? React.createElement('button', {
1133
+ type: 'button', className: 'mopsAction primary', style: { marginLeft: 10 },
1134
+ onClick: () => { try { if (__uiWorkspace && typeof __uiWorkspace.openSession === 'function') __uiWorkspace.openSession(childId) } catch { /* best effort */ } },
1135
+ }, t('panel.openNew')) : null) : null,
1136
+ tab === 'messages'
1137
+ ? React.createElement(React.Fragment, null,
1138
+ React.createElement('div', { className: 'mopsSearchRow' },
1139
+ React.createElement('input', {
1140
+ ref: searchRef, className: 'mopsSearch', value: query,
1141
+ placeholder: t('panel.searchHint'), 'aria-label': t('panel.search'),
1142
+ onChange: (e) => { setQuery(e.target.value); setScrollTop(0); if (listRef.current) listRef.current.scrollTop = 0 },
1143
+ })),
1144
+ React.createElement('div', { className: 'mopsChips' },
1145
+ ['all', 'user', 'assistant', 'tool'].map((key) => React.createElement('button', {
1146
+ key, type: 'button', className: 'mopsChip' + (role === key ? ' on' : ''),
1147
+ onClick: () => { setRole(key); setScrollTop(0); if (listRef.current) listRef.current.scrollTop = 0 },
1148
+ }, t('panel.' + key))),
1149
+ React.createElement('label', { className: 'mopsToggle' },
1150
+ React.createElement('input', {
1151
+ type: 'checkbox', checked: includeShadowed,
1152
+ onChange: (e) => { setIncludeShadowed(e.target.checked); setScrollTop(0); if (listRef.current) listRef.current.scrollTop = 0 },
1153
+ }), t('panel.includeShadowed'))),
1154
+ React.createElement('div', {
1155
+ className: 'mopsList', ref: listRef,
1156
+ onScroll: (e) => setScrollTop(e.currentTarget.scrollTop),
1157
+ },
1158
+ rows.length === 0
1159
+ ? React.createElement('div', { className: 'mopsEmpty' }, showSearchLoading ? t('panel.loading') : (query.trim() ? t('panel.noMatch') : t('panel.empty')))
1160
+ : React.createElement('div', { style: { height: rows.length * PANEL_ROW_H, position: 'relative' } },
1161
+ windowRows.map((m, i) => React.createElement('button', {
1162
+ key: m.seq, type: 'button',
1163
+ className: 'mopsRow' + (pickedLive && pickedLive.seq === m.seq ? ' on' : '') + (m.shadowed ? ' shadowed' : ''),
1164
+ style: { top: (start + i) * PANEL_ROW_H, height: PANEL_ROW_H },
1165
+ onClick: () => pick(m),
1166
+ title: '#' + m.seq + ' ' + (m.role || '') + ' · ' + (m.snippet || ''),
1167
+ },
1168
+ React.createElement('span', { className: 'mopsSeq' }, '#' + m.seq),
1169
+ React.createElement('span', { className: 'mopsRole' }, roleCode(m.role)),
1170
+ React.createElement('span', { className: 'mopsSnippet' }, m.snippet || '—'),
1171
+ React.createElement('span', { className: 'mopsClock' }, fmtClock(m.time)))))),
1172
+ React.createElement('div', { className: 'mopsDetail' },
1173
+ pickedLive
1174
+ ? React.createElement(React.Fragment, null,
1175
+ React.createElement('div', { className: 'mopsDetailMeta' },
1176
+ React.createElement('span', null, '#' + pickedLive.seq),
1177
+ React.createElement('span', null, pickedLive.role || ''),
1178
+ pickedLive.shadowed ? React.createElement('span', null, t('panel.shadowed')) : null,
1179
+ pickedLive.time ? React.createElement('span', null, fmtClock(pickedLive.time)) : null),
1180
+ React.createElement('pre', { className: 'mopsDetailText' },
1181
+ detailState === 'loading' ? t('panel.detailLoading') : (detail || pickedLive.snippet || '—')),
1182
+ React.createElement('div', { className: 'mopsActions' },
1183
+ React.createElement('button', {
1184
+ type: 'button', className: 'mopsAction primary', disabled: !canAct,
1185
+ onClick: () => runAction('revert'),
1186
+ title: t('panel.revertHint'),
1187
+ }, t('panel.revert') + (revertCount ? ' · ' + revertCount : '')),
1188
+ React.createElement('button', {
1189
+ type: 'button', className: 'mopsAction danger', disabled: !canAct,
1190
+ onClick: () => runAction('delete'), title: t('panel.deleteHint'),
1191
+ }, t('panel.delete')),
1192
+ React.createElement('button', {
1193
+ type: 'button', className: 'mopsAction', disabled: !canAct,
1194
+ onClick: () => runAction('branch'), title: t('panel.branchHint'),
1195
+ }, t('panel.branch'))))
1196
+ : React.createElement('div', { className: 'mopsDetailHint' }, t('panel.selectHint'))),
1197
+ React.createElement('footer', { className: 'mopsPanelFoot' },
1198
+ React.createElement('span', { className: 'mopsCount' },
1199
+ t('panel.count', { n: String(totalCount), v: String(visibleCount), s: String(shadowedCount) })),
1200
+ React.createElement('button', {
1201
+ type: 'button', className: 'mopsDanger', disabled: !!busy,
1202
+ onClick: () => { setConfirmTitle(''); setConfirmDelete(true) },
1203
+ }, t('panel.deleteSession'))),
1204
+ confirmDelete
1205
+ ? React.createElement('div', { className: 'mopsConfirm' },
1206
+ React.createElement('h3', null, t('panel.deleteSession')),
1207
+ React.createElement('p', null, t('panel.deleteSessionHint')),
1208
+ React.createElement('input', {
1209
+ value: confirmTitle, autoFocus: true,
1210
+ placeholder: target.expectedTitle || target.title || target.sessionId,
1211
+ onChange: (e) => setConfirmTitle(e.target.value),
1212
+ }),
1213
+ React.createElement('div', { className: 'mopsConfirmRow' },
1214
+ React.createElement('button', { type: 'button', className: 'mopsAction', onClick: () => setConfirmDelete(false), disabled: !!busy }, t('panel.cancel')),
1215
+ React.createElement('button', {
1216
+ type: 'button', className: 'mopsAction danger', disabled: !!busy || confirmTitle.trim() !== (target.expectedTitle || target.title || target.sessionId),
1217
+ onClick: deleteSession,
1218
+ }, busy === 'session-delete' ? t('panel.deleting') : t('panel.confirmDelete'))))
1219
+ : null)
1220
+ : React.createElement('div', { className: 'mopsList', style: { borderTop: 0 } },
1221
+ trashState === 'loading'
1222
+ ? React.createElement('div', { className: 'mopsEmpty' }, t('panel.loading'))
1223
+ : trash.length === 0
1224
+ ? React.createElement('div', { className: 'mopsEmpty' }, t('panel.trashEmpty'))
1225
+ : trash.map((item) => React.createElement('div', { className: 'mopsTrashItem', key: item.trashId },
1226
+ React.createElement('div', { className: 'mopsTrashMain' },
1227
+ React.createElement('div', { className: 'mopsTrashTitle' }, item.title || t('panel.untitled')),
1228
+ React.createElement('div', { className: 'mopsTrashMeta' },
1229
+ item.sessionId + (item.expiresAt ? ' · ' + t('panel.expires') + ' ' + new Date(item.expiresAt).toLocaleDateString() : ''))),
1230
+ React.createElement('button', {
1231
+ type: 'button', className: 'mopsAction', disabled: !!busy,
1232
+ onClick: () => restoreEntry(item.trashId),
1233
+ }, busy === 'restore:' + item.trashId ? t('panel.busy') : t('panel.restore')),
1234
+ React.createElement('button', {
1235
+ type: 'button', className: 'mopsAction danger', disabled: !!busy,
1236
+ onClick: () => purgeEntry(item.trashId),
1237
+ }, busy === 'purge:' + item.trashId ? t('panel.busy') : (purgeId === item.trashId ? t('panel.purgeConfirm') : t('panel.purge'))))))))
1238
+ }
1239
+
595
1240
  // --- 头部按钮 ---------------------------------------------------------------
596
1241
 
597
1242
  // 0.3.0 分支双路径选择:0.2.0+ 官方 sessions.fork({atSeq}) 优先。
@@ -1995,7 +2640,7 @@ window.__ModuleLoader__.load({
1995
2640
  ctx.slots.inject(OVERLAY_SLOT, () => ctx.slots.register({
1996
2641
  name: OVERLAY_SLOT, id: DIALOG_ID, order: 101,
1997
2642
  ...(__locale ? { locale: NS } : {}),
1998
- }, OpsDialog))
2643
+ }, window.__MOPS_LEGACY_DIALOG === true ? OpsDialog : OpsPanel))
1999
2644
  installSidebarOps()
2000
2645
  installUserRevertInjector()
2001
2646
  }