@twinsearth/timemachine-dsh-plugin 0.0.0-stage → 0.1.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/CHANGELOG.md +55 -0
- package/LICENSE +21 -0
- package/README.md +225 -2
- package/cordis.patch.yml +28 -0
- package/docs/ARCHITECTURE.md +172 -0
- package/docs/REQUIREMENTS.md +81 -0
- package/docs/TESTPLAN.md +155 -0
- package/lib/client.js +818 -0
- package/lib/index.js +5 -0
- package/package.json +48 -4
- package/plugin.json +9 -0
- package/scripts/host-menu-patch.mjs +274 -0
- package/src/archive/tar.mjs +138 -0
- package/src/archive/zstd.mjs +206 -0
- package/src/plugin.mjs +213 -0
- package/src/plugins.mjs +355 -0
- package/src/point.mjs +671 -0
- package/src/registry.mjs +120 -0
- package/src/restore.mjs +777 -0
- package/src/scan.mjs +244 -0
- package/src/service.mjs +448 -0
- package/src/session-log.mjs +123 -0
- package/src/util/fs.mjs +92 -0
- package/src/util/paths.mjs +82 -0
package/lib/client.js
ADDED
|
@@ -0,0 +1,818 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: '@twinsearth/timemachine-dsh-plugin',
|
|
3
|
+
factory: (require) => {
|
|
4
|
+
const module = { exports: {} };
|
|
5
|
+
const exports = module.exports;
|
|
6
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
7
|
+
|
|
8
|
+
const React = require('react');
|
|
9
|
+
const h = React.createElement;
|
|
10
|
+
const { useCallback, useEffect, useMemo, useRef, useState } = React;
|
|
11
|
+
|
|
12
|
+
const PREFIX = '/api/twinsearth/timemachine';
|
|
13
|
+
const STYLE_ID = 'timemachine-dsh-style';
|
|
14
|
+
const PANEL_ID = 'timemachine';
|
|
15
|
+
|
|
16
|
+
async function request(path, init = {}) {
|
|
17
|
+
const response = await fetch(PREFIX + path, {
|
|
18
|
+
credentials: 'same-origin',
|
|
19
|
+
headers: { 'content-type': 'application/json', ...(init.headers ?? {}) },
|
|
20
|
+
...init,
|
|
21
|
+
});
|
|
22
|
+
let body;
|
|
23
|
+
try {
|
|
24
|
+
body = await response.json();
|
|
25
|
+
} catch {
|
|
26
|
+
body = { ok: false, reason_code: 'TIMEMACHINE_BAD_RESPONSE', detail: `HTTP ${response.status}` };
|
|
27
|
+
}
|
|
28
|
+
if (!response.ok || body?.ok === false) {
|
|
29
|
+
const error = new Error(body?.detail ?? body?.reason_code ?? `HTTP ${response.status}`);
|
|
30
|
+
error.payload = body;
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
return body;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function formatTime(value) {
|
|
37
|
+
if (typeof value !== 'string' || value === '') return '—';
|
|
38
|
+
const date = new Date(value);
|
|
39
|
+
if (Number.isNaN(date.getTime())) return value;
|
|
40
|
+
return date.toLocaleString('zh-CN', { hour12: false });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function formatBytes(value) {
|
|
44
|
+
const bytes = Number(value);
|
|
45
|
+
if (!Number.isFinite(bytes) || bytes <= 0) return '0 B';
|
|
46
|
+
if (bytes >= 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024 / 1024).toFixed(2)} GB`;
|
|
47
|
+
if (bytes >= 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
|
|
48
|
+
if (bytes >= 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
49
|
+
return `${bytes} B`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function countText(point) {
|
|
53
|
+
const counts = point?.counts ?? point?.point?.counts ?? {};
|
|
54
|
+
return `会话 ${counts.sessions ?? 0} · 配置 ${counts.config ?? 0} · 状态 ${counts.state ?? 0} · 插件 ${counts.plugins ?? 0}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function pickDirectory() {
|
|
58
|
+
const api = typeof window === 'undefined' ? null : window.__DSH_DIRECTORY_PICKER__;
|
|
59
|
+
if (api === null || api === undefined || typeof api.pick !== 'function') return null;
|
|
60
|
+
try { return await api.pick(); } catch { return null; }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function pickFile() {
|
|
64
|
+
return new Promise((resolve) => {
|
|
65
|
+
if (typeof document === 'undefined') return resolve(null);
|
|
66
|
+
const input = document.createElement('input');
|
|
67
|
+
input.type = 'file';
|
|
68
|
+
input.accept = '.zst,.tar.zst,application/zstd,application/octet-stream';
|
|
69
|
+
input.style.display = 'none';
|
|
70
|
+
(document.body ?? document.documentElement).appendChild(input);
|
|
71
|
+
let settled = false;
|
|
72
|
+
const finish = (value) => {
|
|
73
|
+
if (settled) return;
|
|
74
|
+
settled = true;
|
|
75
|
+
input.remove();
|
|
76
|
+
resolve(value);
|
|
77
|
+
};
|
|
78
|
+
input.addEventListener('change', () => {
|
|
79
|
+
const file = input.files?.[0] ?? null;
|
|
80
|
+
if (file === null) return finish(null);
|
|
81
|
+
const paths = typeof window === 'undefined' ? null : window.__DSH_HOST_PATHS__;
|
|
82
|
+
const absolute = paths !== null && paths !== undefined && typeof paths.pathFor === 'function'
|
|
83
|
+
? paths.pathFor(file)
|
|
84
|
+
: (typeof file.path === 'string' ? file.path : null);
|
|
85
|
+
finish(typeof absolute === 'string' && absolute !== '' ? absolute : null);
|
|
86
|
+
}, { once: true });
|
|
87
|
+
input.addEventListener('cancel', () => finish(null), { once: true });
|
|
88
|
+
input.click();
|
|
89
|
+
window.setTimeout(() => finish(null), 120_000);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function injectStyle(doc) {
|
|
94
|
+
if (doc === null || doc === undefined || doc.getElementById === undefined) return null;
|
|
95
|
+
if (doc.getElementById(STYLE_ID) !== null) return null;
|
|
96
|
+
const style = doc.createElement('style');
|
|
97
|
+
style.id = STYLE_ID;
|
|
98
|
+
style.textContent = `
|
|
99
|
+
.tm-root{color:var(--dsh-text-primary,#1f2329);font:13px/1.55 -apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;display:flex;flex-direction:column;gap:12px;min-width:0}
|
|
100
|
+
.tm-head{display:flex;align-items:flex-start;justify-content:space-between;gap:12px;flex-wrap:wrap}
|
|
101
|
+
.tm-title{font-size:17px;font-weight:650;letter-spacing:.01em;margin:0}
|
|
102
|
+
.tm-sub{color:var(--dsh-text-secondary,#6b7280);font-size:12px;margin-top:4px;word-break:break-all}
|
|
103
|
+
.tm-card{background:var(--dsh-bg-primary,#fff);border:1px solid var(--dsh-border,#e5e7eb);border-radius:12px;padding:14px;box-shadow:0 1px 2px rgba(0,0,0,.03)}
|
|
104
|
+
.tm-cardTitle{font-weight:600;font-size:13px;margin:0 0 10px;display:flex;align-items:center;gap:8px;justify-content:space-between;flex-wrap:wrap}
|
|
105
|
+
.tm-row{display:flex;align-items:center;gap:8px;flex-wrap:wrap}
|
|
106
|
+
.tm-grow{flex:1;min-width:160px}
|
|
107
|
+
.tm-muted{color:var(--dsh-text-secondary,#6b7280);font-size:12px}
|
|
108
|
+
.tm-mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace;font-size:11px;word-break:break-all}
|
|
109
|
+
.tm-tag{display:inline-flex;align-items:center;gap:4px;padding:2px 7px;border-radius:999px;background:var(--dsh-bg-secondary,#f3f4f6);color:var(--dsh-text-secondary,#4b5563);font-size:11px;white-space:nowrap}
|
|
110
|
+
.tm-tag.ok{background:#e8f7ee;color:#177245}
|
|
111
|
+
.tm-tag.warn{background:#fff4e5;color:#9a5b00}
|
|
112
|
+
.tm-tag.bad{background:#fef2f2;color:#991b1b}
|
|
113
|
+
.tm-btn{appearance:none;border:1px solid var(--dsh-border,#d1d5db);background:var(--dsh-bg-primary,#fff);color:inherit;border-radius:8px;padding:6px 11px;font-size:12px;cursor:pointer;line-height:1.3}
|
|
114
|
+
.tm-btn:hover:not(:disabled){border-color:#9ca3af;background:var(--dsh-bg-secondary,#f9fafb)}
|
|
115
|
+
.tm-btn:disabled{opacity:.5;cursor:not-allowed}
|
|
116
|
+
.tm-btn.primary{background:#2563eb;border-color:#2563eb;color:#fff;font-weight:600}
|
|
117
|
+
.tm-btn.primary:hover:not(:disabled){background:#1d4ed8;border-color:#1d4ed8}
|
|
118
|
+
.tm-btn.danger{background:#b91c1c;border-color:#b91c1c;color:#fff}
|
|
119
|
+
.tm-btn.danger:hover:not(:disabled){background:#991b1b;border-color:#991b1b}
|
|
120
|
+
.tm-btn.ghost{border-color:transparent;background:transparent;padding:4px 6px}
|
|
121
|
+
.tm-btn.ghost:hover:not(:disabled){background:var(--dsh-bg-secondary,#f3f4f6)}
|
|
122
|
+
.tm-input{border:1px solid var(--dsh-border,#d1d5db);background:var(--dsh-bg-primary,#fff);color:inherit;border-radius:8px;padding:6px 9px;font-size:12px;min-width:0}
|
|
123
|
+
.tm-input:focus{outline:2px solid rgba(37,99,235,.25);border-color:#2563eb}
|
|
124
|
+
.tm-check{display:flex;align-items:flex-start;gap:8px;padding:8px 10px;border:1px solid var(--dsh-border,#e5e7eb);border-radius:10px;background:var(--dsh-bg-secondary,#fafafa);cursor:pointer;flex:1;min-width:180px}
|
|
125
|
+
.tm-check input{margin-top:2px;accent-color:#2563eb}
|
|
126
|
+
.tm-check b{font-size:12px;display:block;margin-bottom:2px}
|
|
127
|
+
.tm-check span{color:var(--dsh-text-secondary,#6b7280);font-size:11px}
|
|
128
|
+
.tm-tabs{display:flex;gap:4px;padding:3px;background:var(--dsh-bg-secondary,#f3f4f6);border-radius:9px;width:max-content;max-width:100%;flex-wrap:wrap}
|
|
129
|
+
.tm-tab{border:0;background:transparent;color:var(--dsh-text-secondary,#4b5563);border-radius:7px;padding:5px 10px;font-size:12px;cursor:pointer}
|
|
130
|
+
.tm-tab.active{background:var(--dsh-bg-primary,#fff);color:inherit;box-shadow:0 1px 2px rgba(0,0,0,.08);font-weight:600}
|
|
131
|
+
.tm-list{display:flex;flex-direction:column;gap:8px}
|
|
132
|
+
.tm-point{border:1px solid var(--dsh-border,#e5e7eb);border-radius:10px;padding:10px 12px;display:flex;flex-direction:column;gap:6px;background:var(--dsh-bg-primary,#fff)}
|
|
133
|
+
.tm-pointTop{display:flex;align-items:center;gap:8px;flex-wrap:wrap}
|
|
134
|
+
.tm-pointTime{font-weight:600;font-size:12px}
|
|
135
|
+
.tm-pointMeta{color:var(--dsh-text-secondary,#6b7280);font-size:11px;display:flex;gap:10px;flex-wrap:wrap}
|
|
136
|
+
.tm-pointActions{display:flex;gap:6px;margin-left:auto;flex-wrap:wrap}
|
|
137
|
+
.tm-empty{border:1px dashed var(--dsh-border,#d1d5db);border-radius:10px;padding:20px;text-align:center;color:var(--dsh-text-secondary,#6b7280);font-size:12px}
|
|
138
|
+
.tm-banner{border-radius:10px;padding:9px 12px;font-size:12px;display:flex;align-items:flex-start;justify-content:space-between;gap:10px}
|
|
139
|
+
.tm-banner.error{background:#fef2f2;border:1px solid #fecaca;color:#991b1b}
|
|
140
|
+
.tm-banner.success{background:#ecfdf5;border:1px solid #a7f3d0;color:#065f46}
|
|
141
|
+
.tm-banner.info{background:#eff6ff;border:1px solid #bfdbfe;color:#1e40af}
|
|
142
|
+
.tm-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(120px,1fr));gap:8px}
|
|
143
|
+
.tm-kv{border:1px solid var(--dsh-border,#e5e7eb);border-radius:9px;padding:8px 10px;background:var(--dsh-bg-secondary,#fafafa)}
|
|
144
|
+
.tm-kv b{display:block;font-size:15px;line-height:1.2;margin-bottom:2px}
|
|
145
|
+
.tm-kv span{font-size:11px;color:var(--dsh-text-secondary,#6b7280)}
|
|
146
|
+
.tm-details{border:1px solid var(--dsh-border,#e5e7eb);border-radius:10px;padding:0 12px;background:var(--dsh-bg-secondary,#fafafa)}
|
|
147
|
+
.tm-details summary{cursor:pointer;padding:9px 0;font-size:12px;font-weight:600;color:var(--dsh-text-secondary,#4b5563)}
|
|
148
|
+
.tm-detailsBody{padding:0 0 10px;font-size:11px;color:var(--dsh-text-secondary,#6b7280);line-height:1.6;display:flex;flex-direction:column;gap:8px}
|
|
149
|
+
.tm-plugin{display:flex;align-items:center;gap:8px;padding:5px 0;border-top:1px dashed var(--dsh-border,#e5e7eb);flex-wrap:wrap}
|
|
150
|
+
.tm-plugin:first-child{border-top:0}
|
|
151
|
+
.tm-yaml{margin:0;padding:8px;background:#0f172a;color:#e2e8f0;border-radius:8px;font:11px/1.5 ui-monospace,SFMono-Regular,Consolas,monospace;max-height:220px;overflow:auto;white-space:pre-wrap;word-break:break-word}
|
|
152
|
+
.tm-modalMask{position:fixed;inset:0;background:rgba(15,23,42,.45);z-index:1000;display:flex;align-items:center;justify-content:center;padding:18px}
|
|
153
|
+
.tm-modal{background:var(--dsh-bg-primary,#fff);color:var(--dsh-text-primary,#111827);border-radius:14px;max-width:680px;width:100%;max-height:86vh;overflow:auto;padding:18px;box-shadow:0 20px 60px rgba(0,0,0,.25);display:flex;flex-direction:column;gap:12px}
|
|
154
|
+
.tm-modal h3{margin:0;font-size:15px}
|
|
155
|
+
.tm-modal ul{margin:0;padding-left:18px;font-size:12px;color:var(--dsh-text-secondary,#4b5563);line-height:1.7}
|
|
156
|
+
.tm-modal .tm-row{justify-content:flex-end}
|
|
157
|
+
.tm-icon{width:18px;height:18px;display:block}
|
|
158
|
+
.tm-sessionRow{display:flex;gap:8px;align-items:center;padding:4px 0;border-top:1px dashed var(--dsh-border,#e5e7eb);font-size:11px;flex-wrap:wrap}
|
|
159
|
+
.tm-sessionRow:first-child{border-top:0}
|
|
160
|
+
.tm-sessionId{font-family:ui-monospace,SFMono-Regular,Consolas,monospace;word-break:break-all}
|
|
161
|
+
@media (max-width:640px){.tm-pointActions{margin-left:0}.tm-card{padding:12px}}
|
|
162
|
+
`;
|
|
163
|
+
(doc.head ?? doc.documentElement).appendChild(style);
|
|
164
|
+
return () => {
|
|
165
|
+
try { style.remove(); } catch { /* already gone */ }
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function Banner({ kind = 'info', title, detail, onClose }) {
|
|
170
|
+
if (title === null || title === undefined || title === '') return null;
|
|
171
|
+
return h('div', { className: `tm-banner ${kind}` }, [
|
|
172
|
+
h('span', { key: 't' }, [h('b', { key: 'b' }, title), detail ? h('div', { key: 'd', className: 'tm-muted' }, detail) : null]),
|
|
173
|
+
onClose ? h('button', { key: 'x', type: 'button', className: 'tm-btn ghost', onClick: onClose, 'aria-label': '关闭' }, '×') : null,
|
|
174
|
+
]);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function useStatus() {
|
|
178
|
+
const [status, setStatus] = useState(null);
|
|
179
|
+
const [loading, setLoading] = useState(true);
|
|
180
|
+
const [error, setError] = useState(null);
|
|
181
|
+
const mounted = useRef(true);
|
|
182
|
+
const refresh = useCallback(async () => {
|
|
183
|
+
setLoading(true);
|
|
184
|
+
try {
|
|
185
|
+
const next = await request('/status');
|
|
186
|
+
if (mounted.current) {
|
|
187
|
+
setStatus(next);
|
|
188
|
+
setError(null);
|
|
189
|
+
}
|
|
190
|
+
return next;
|
|
191
|
+
} catch (caught) {
|
|
192
|
+
if (mounted.current) setError(caught.payload ?? { reason_code: 'TIMEMACHINE_STATUS_FAILED', detail: caught.message });
|
|
193
|
+
return null;
|
|
194
|
+
} finally {
|
|
195
|
+
if (mounted.current) setLoading(false);
|
|
196
|
+
}
|
|
197
|
+
}, []);
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
mounted.current = true;
|
|
200
|
+
void refresh();
|
|
201
|
+
return () => { mounted.current = false; };
|
|
202
|
+
}, [refresh]);
|
|
203
|
+
return { status, loading, error, refresh, setError };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function PointRow({ point, busy, onRestore, onDetails }) {
|
|
207
|
+
const p = point.point ?? {};
|
|
208
|
+
return h('div', { className: 'tm-point', 'data-tm-point': p.kind ?? '' },
|
|
209
|
+
h('div', { className: 'tm-pointTop' }, [
|
|
210
|
+
h('span', { key: 'time', className: 'tm-pointTime' }, formatTime(p.created_at)),
|
|
211
|
+
h('span', { key: 'src', className: 'tm-tag' }, p.source_machine ?? '本机'),
|
|
212
|
+
point.metadata_source === 'sidecar' ? null : h('span', { key: 'v', className: 'tm-tag warn' }, '待校验'),
|
|
213
|
+
h('span', { key: 'size', className: 'tm-tag' }, formatBytes(point.bytes)),
|
|
214
|
+
h('span', { key: 'actions', className: 'tm-pointActions' }, [
|
|
215
|
+
h('button', { key: 'detail', type: 'button', className: 'tm-btn ghost', onClick: () => onDetails(point) }, '详情'),
|
|
216
|
+
h('button', { key: 'restore', type: 'button', className: 'tm-btn danger', disabled: busy, onClick: () => onRestore(point) }, '还原'),
|
|
217
|
+
]),
|
|
218
|
+
]),
|
|
219
|
+
h('div', { key: 'meta', className: 'tm-pointMeta' }, [
|
|
220
|
+
h('span', { key: 'counts' }, countText(point)),
|
|
221
|
+
h('span', { key: 'opt' }, `范围:${p.session_scope === 'all' ? '所有会话' : p.session_scope === 'archived' ? '已归档' : '活跃会话'}`),
|
|
222
|
+
h('span', { key: 'file', className: 'tm-mono', title: point.file }, point.file),
|
|
223
|
+
]),
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function PlanModal({ plan, busy, pathMap, setPathMap, installPlugins, setInstallPlugins, onCancel, onConfirm }) {
|
|
228
|
+
if (plan === null) return null;
|
|
229
|
+
const will = plan.will ?? {};
|
|
230
|
+
const workspaces = plan.workspaces ?? [];
|
|
231
|
+
const plugins = plan.plugins ?? [];
|
|
232
|
+
const packages = Object.keys(plan.plugin_packages ?? {});
|
|
233
|
+
return h('div', { className: 'tm-modalMask', role: 'dialog', 'aria-modal': 'true' },
|
|
234
|
+
h('div', { className: 'tm-modal' }, [
|
|
235
|
+
h('h3', { key: 'h' }, '确认还原到本机'),
|
|
236
|
+
h('div', { key: 'src', className: 'tm-muted' }, `${formatTime(plan.point?.created_at)} · 来自 ${plan.point?.source_machine ?? '未知机器'} · ${formatBytes(plan.bytes)}`),
|
|
237
|
+
plan.cross_platform ? h(Banner, { key: 'cross', kind: 'info', title: '跨平台还原', detail: '来源与当前系统不同,工作区路径需要映射;你可以在下面直接修改。' }) : null,
|
|
238
|
+
h('div', { key: 'grid', className: 'tm-grid' }, [
|
|
239
|
+
h('div', { key: 's', className: 'tm-kv' }, [h('b', { key: 'n' }, String(will.sessions ?? 0)), h('span', { key: 'l' }, '会话')]),
|
|
240
|
+
h('div', { key: 'a', className: 'tm-kv' }, [h('b', { key: 'n' }, String(will.attachments ?? 0)), h('span', { key: 'l' }, '附件')]),
|
|
241
|
+
h('div', { key: 'c', className: 'tm-kv' }, [h('b', { key: 'n' }, String(will.config_files ?? 0)), h('span', { key: 'l' }, '配置文件')]),
|
|
242
|
+
h('div', { key: 'p', className: 'tm-kv' }, [h('b', { key: 'n' }, String(will.plugins ?? 0)), h('span', { key: 'l' }, '第三方插件')]),
|
|
243
|
+
]),
|
|
244
|
+
workspaces.length === 0 ? null : h('div', { key: 'ws' }, [
|
|
245
|
+
h('div', { key: 't', style: { fontWeight: 600, marginBottom: 6 } }, '工作区将导入到'),
|
|
246
|
+
...workspaces.map((workspace, index) => h('div', { key: workspace.source_path ?? String(index), className: 'tm-row', style: { marginBottom: 6 } }, [
|
|
247
|
+
h('span', { key: 'l', className: 'tm-muted', style: { minWidth: 120 } }, workspace.title ?? workspace.source_title ?? '工作区'),
|
|
248
|
+
h('input', {
|
|
249
|
+
key: 'i',
|
|
250
|
+
className: 'tm-input tm-grow',
|
|
251
|
+
value: pathMap[workspace.source_path] ?? workspace.target_path,
|
|
252
|
+
onChange: (event) => setPathMap((current) => ({ ...current, [workspace.source_path]: event.target.value })),
|
|
253
|
+
}),
|
|
254
|
+
])),
|
|
255
|
+
]),
|
|
256
|
+
will.plugins > 0 ? h('label', { key: 'plugins', className: 'tm-check', style: { marginTop: 8, padding: '8px 10px' } }, [
|
|
257
|
+
h('input', { type: 'checkbox', checked: installPlugins, disabled: busy, onChange: () => setInstallPlugins((value) => !value) }),
|
|
258
|
+
h('span', { key: 'x' }, [
|
|
259
|
+
h('b', { key: 'b' }, '安装并还原第三方插件'),
|
|
260
|
+
h('span', { key: 's' }, `${packages.length > 0 ? `优先使用还原点内 ${packages.length} 个安装包,否则` : ''}联网安装 ${plugins.length} 个 npm latest,并恢复原配置;取消勾选则只还原配置,不联网安装`),
|
|
261
|
+
]),
|
|
262
|
+
]) : h('div', { key: 'plugins', className: 'tm-muted' }, '本还原点未包含第三方插件安装列表。'),
|
|
263
|
+
h('ul', { key: 'warn' }, (plan.warnings ?? []).map((text, index) => h('li', { key: String(index) }, text))),
|
|
264
|
+
h('div', { key: 'actions', className: 'tm-row' }, [
|
|
265
|
+
h('button', { key: 'cancel', type: 'button', className: 'tm-btn', disabled: busy, onClick: onCancel }, '取消'),
|
|
266
|
+
h('button', { key: 'go', type: 'button', className: 'tm-btn danger', disabled: busy, onClick: onConfirm }, busy ? '还原中…' : '确认还原'),
|
|
267
|
+
]),
|
|
268
|
+
]),
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function DetailsModal({ details, onClose, onRestore }) {
|
|
273
|
+
if (details === null) return null;
|
|
274
|
+
return h('div', { className: 'tm-modalMask', role: 'dialog', 'aria-modal': 'true' },
|
|
275
|
+
h('div', { className: 'tm-modal' }, [
|
|
276
|
+
h('h3', { key: 'h' }, '还原点详情'),
|
|
277
|
+
h('div', { key: 'meta', className: 'tm-muted' }, `${formatTime(details.point?.created_at)} · ${details.point?.source_machine ?? ''} · ${formatBytes(details.bytes)}`),
|
|
278
|
+
h('div', { key: 'path', className: 'tm-mono' }, details.file),
|
|
279
|
+
h('div', { key: 'counts', className: 'tm-grid' }, [
|
|
280
|
+
h('div', { key: 's', className: 'tm-kv' }, [h('b', { key: 'n' }, String(details.counts?.sessions ?? 0)), h('span', { key: 'l' }, '会话')]),
|
|
281
|
+
h('div', { key: 'a', className: 'tm-kv' }, [h('b', { key: 'n' }, String(details.counts?.attachments ?? 0)), h('span', { key: 'l' }, '附件')]),
|
|
282
|
+
h('div', { key: 'c', className: 'tm-kv' }, [h('b', { key: 'n' }, String(details.counts?.config ?? 0)), h('span', { key: 'l' }, '配置')]),
|
|
283
|
+
h('div', { key: 'p', className: 'tm-kv' }, [h('b', { key: 'n' }, String(details.counts?.plugins ?? 0)), h('span', { key: 'l' }, '插件')]),
|
|
284
|
+
]),
|
|
285
|
+
h('div', { key: 'plugins', className: 'tm-muted' }, `插件清单:${(details.point?.plugins ?? []).map((row) => row.name).join('、') || '未包含'}`),
|
|
286
|
+
h('div', { key: 'ws', className: 'tm-muted' }, `工作区:${(details.point?.workspaces ?? []).map((row) => row.title ?? row.path).join('、') || '—'}`),
|
|
287
|
+
h('details', { key: 'sess', className: 'tm-details' }, [
|
|
288
|
+
h('summary', { key: 's' }, `会话列表(${details.sessions?.length ?? 0})`),
|
|
289
|
+
h('div', { key: 'b', className: 'tm-detailsBody' }, (details.sessions ?? []).slice(0, 300).map((session) => h('div', { key: session.session_id, className: 'tm-sessionRow' }, [
|
|
290
|
+
h('span', { key: 'id', className: 'tm-sessionId' }, session.session_id),
|
|
291
|
+
h('span', { key: 'ws', className: 'tm-muted' }, session.workspace_title ?? session.workspace_path ?? ''),
|
|
292
|
+
]))),
|
|
293
|
+
]),
|
|
294
|
+
h('div', { key: 'actions', className: 'tm-row' }, [
|
|
295
|
+
h('button', { key: 'close', type: 'button', className: 'tm-btn', onClick: onClose }, '关闭'),
|
|
296
|
+
h('button', { key: 'go', type: 'button', className: 'tm-btn danger', onClick: onRestore }, '还原这份'),
|
|
297
|
+
]),
|
|
298
|
+
]),
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function PluginSummary({ status, onOpen }) {
|
|
303
|
+
const user = status?.plugins?.user ?? [];
|
|
304
|
+
const third = status?.plugins?.third_party ?? [];
|
|
305
|
+
if (user.length === 0 && third.length === 0) return h('div', { className: 'tm-muted' }, '没有读取到插件清单。');
|
|
306
|
+
return h('div', { className: 'tm-detailsBody' }, [
|
|
307
|
+
h('div', { key: 'sum', className: 'tm-row' }, [
|
|
308
|
+
h('span', { className: 'tm-tag' }, `内置/实验 ${user.length}`),
|
|
309
|
+
h('span', { className: 'tm-tag' }, `第三方 ${third.length}`),
|
|
310
|
+
h('span', { className: 'tm-tag ok' }, '开启/关闭状态与参数随配置一起备份'),
|
|
311
|
+
h('button', { key: 'o', type: 'button', className: 'tm-btn ghost', onClick: onOpen }, '查看'),
|
|
312
|
+
]),
|
|
313
|
+
user.slice(0, 40).map((row) => h(PluginRow, { key: row.name, row })),
|
|
314
|
+
third.map((row) => h(PluginRow, { key: row.name, row, third: true })),
|
|
315
|
+
]);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function PluginRow({ row, third = false }) {
|
|
319
|
+
const enabled = row.enabled === true ? '已开启' : row.enabled === false ? '已关闭' : '未声明';
|
|
320
|
+
return h('div', { className: 'tm-plugin' }, [
|
|
321
|
+
h('span', { key: 'n', className: 'tm-sessionId', style: { flex: 1 } }, row.name),
|
|
322
|
+
h('span', { key: 'k', className: 'tm-tag' }, third ? '第三方' : row.kind === 'experimental' ? '实验性' : row.kind === 'self' ? '本插件' : '内置'),
|
|
323
|
+
h('span', { key: 'e', className: `tm-tag ${row.enabled === false ? 'warn' : 'ok'}` }, enabled),
|
|
324
|
+
row.version ? h('span', { key: 'v', className: 'tm-muted' }, row.version) : null,
|
|
325
|
+
row.config_yaml ? h('details', { key: 'c', style: { width: '100%' } }, [
|
|
326
|
+
h('summary', { key: 's', className: 'tm-muted', style: { cursor: 'pointer' } }, '参数配置'),
|
|
327
|
+
h('pre', { key: 'p', className: 'tm-yaml' }, row.config_yaml),
|
|
328
|
+
]) : null,
|
|
329
|
+
]);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function RestorePointBody({ compact = false, onDone = null }) {
|
|
333
|
+
const { status, loading, error, refresh, setError } = useStatus();
|
|
334
|
+
const [tab, setTab] = useState('manual');
|
|
335
|
+
const [options, setOptions] = useState({ includeConfig: true, includePlugins: true, includeSessions: true, includePluginPackages: false, sessionScope: 'active' });
|
|
336
|
+
const [busy, setBusy] = useState('');
|
|
337
|
+
const [plan, setPlan] = useState(null);
|
|
338
|
+
const [pathMap, setPathMap] = useState({});
|
|
339
|
+
const [installPlugins, setInstallPlugins] = useState(true);
|
|
340
|
+
const [notice, setNotice] = useState(null);
|
|
341
|
+
const [details, setDetails] = useState(null);
|
|
342
|
+
const [autoTime, setAutoTime] = useState('22:00');
|
|
343
|
+
const [autoOn, setAutoOn] = useState(false);
|
|
344
|
+
const [showPlugins, setShowPlugins] = useState(false);
|
|
345
|
+
const synced = useRef(false);
|
|
346
|
+
|
|
347
|
+
useEffect(() => {
|
|
348
|
+
if (status === null || synced.current) return;
|
|
349
|
+
synced.current = true;
|
|
350
|
+
const stored = status.settings ?? {};
|
|
351
|
+
setOptions({
|
|
352
|
+
includeConfig: stored.includeConfig !== false,
|
|
353
|
+
includePlugins: stored.includePlugins !== false,
|
|
354
|
+
includeSessions: stored.includeSessions !== false,
|
|
355
|
+
includePluginPackages: stored.includePluginPackages === true,
|
|
356
|
+
sessionScope: stored.sessionScope ?? 'active',
|
|
357
|
+
});
|
|
358
|
+
setAutoTime(stored.autoTime ?? '22:00');
|
|
359
|
+
setAutoOn(stored.autoEnabled === true);
|
|
360
|
+
}, [status]);
|
|
361
|
+
|
|
362
|
+
const stores = status?.stores ?? { manual: { points: [] }, auto: { points: [] }, imported: { points: [] } };
|
|
363
|
+
const points = tab === 'auto' ? (stores.auto?.points ?? []) : tab === 'imported' ? (stores.imported?.points ?? []) : (stores.manual?.points ?? []);
|
|
364
|
+
|
|
365
|
+
const run = async (label, work) => {
|
|
366
|
+
if (busy !== '') return null;
|
|
367
|
+
setBusy(label);
|
|
368
|
+
setError(null);
|
|
369
|
+
setNotice(null);
|
|
370
|
+
try {
|
|
371
|
+
const result = await work();
|
|
372
|
+
await refresh();
|
|
373
|
+
return result;
|
|
374
|
+
} catch (caught) {
|
|
375
|
+
setError(caught.payload ?? { reason_code: 'TIMEMACHINE_FAILED', detail: caught.message });
|
|
376
|
+
return null;
|
|
377
|
+
} finally {
|
|
378
|
+
setBusy('');
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
const saveOptions = async () => {
|
|
383
|
+
await request('/settings', { method: 'POST', body: JSON.stringify(options) });
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
const create = () => run('create', async () => {
|
|
387
|
+
await saveOptions();
|
|
388
|
+
const result = await request('/create', { method: 'POST', body: JSON.stringify({ kind: 'manual' }) });
|
|
389
|
+
setNotice({ kind: 'success', title: '还原点已创建', detail: `${result.file} · ${formatBytes(result.bytes)}` });
|
|
390
|
+
return result;
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
const changeRoot = () => run('pick', async () => {
|
|
394
|
+
const picked = await pickDirectory();
|
|
395
|
+
if (picked === null || picked === '') {
|
|
396
|
+
setNotice({ kind: 'info', title: '没有选择文件夹' });
|
|
397
|
+
return null;
|
|
398
|
+
}
|
|
399
|
+
await request('/settings', { method: 'POST', body: JSON.stringify({ root: picked }) });
|
|
400
|
+
setNotice({ kind: 'success', title: '保存文件夹已设置', detail: picked });
|
|
401
|
+
return picked;
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
const useDefault = () => run('settings', async () => {
|
|
405
|
+
const root = status?.root_default ?? 'D:\\ToFuture';
|
|
406
|
+
await request('/settings', { method: 'POST', body: JSON.stringify({ root }) });
|
|
407
|
+
setNotice({ kind: 'success', title: '已使用默认文件夹', detail: root });
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
const importPoint = () => run('import', async () => {
|
|
411
|
+
const file = await pickFile();
|
|
412
|
+
if (file === null) {
|
|
413
|
+
setNotice({ kind: 'info', title: '没有选择还原点文件', detail: '也可以把 .tar.zst 文件拖到窗口后再试。' });
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
const result = await request('/import', { method: 'POST', body: JSON.stringify({ file }) });
|
|
417
|
+
setNotice({ kind: 'success', title: '还原点已导入', detail: result.file });
|
|
418
|
+
setTab('imported');
|
|
419
|
+
return result;
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
const saveAuto = async (nextOn = autoOn, nextTime = autoTime) => {
|
|
423
|
+
setAutoOn(nextOn);
|
|
424
|
+
await run('settings', async () => {
|
|
425
|
+
await request('/settings', { method: 'POST', body: JSON.stringify({ autoEnabled: nextOn, autoTime: nextTime }) });
|
|
426
|
+
setNotice({ kind: 'success', title: nextOn ? '已开启定时还原点' : '已关闭定时还原点', detail: nextOn ? `每天 ${nextTime} 自动保存一份,保留 ${status?.settings?.keepAuto ?? 2} 份` : null });
|
|
427
|
+
});
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
const startRestore = (point) => run('plan', async () => {
|
|
431
|
+
const next = await request('/plan', { method: 'POST', body: JSON.stringify({ file: point.file }) });
|
|
432
|
+
const map = {};
|
|
433
|
+
for (const workspace of next.workspaces ?? []) {
|
|
434
|
+
map[workspace.source_path] = workspace.target_path;
|
|
435
|
+
}
|
|
436
|
+
setPathMap(map);
|
|
437
|
+
setInstallPlugins((next.plugins ?? []).length > 0);
|
|
438
|
+
setPlan(next);
|
|
439
|
+
return next;
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
const confirmRestore = () => run('restore', async () => {
|
|
443
|
+
const result = await request('/restore', {
|
|
444
|
+
method: 'POST',
|
|
445
|
+
body: JSON.stringify({
|
|
446
|
+
file: plan.file,
|
|
447
|
+
confirm: 'restore',
|
|
448
|
+
pathMap: Object.entries(pathMap).map(([from, to]) => ({ from, to })),
|
|
449
|
+
installPlugins,
|
|
450
|
+
useSavedPackages: (plan.plugin_packages && Object.keys(plan.plugin_packages).length > 0) || false,
|
|
451
|
+
}),
|
|
452
|
+
});
|
|
453
|
+
setPlan(null);
|
|
454
|
+
setNotice({
|
|
455
|
+
kind: result.live_registration?.ok === true ? 'success' : 'info',
|
|
456
|
+
title: result.live_registration?.ok === true ? '还原完成,正在打开工作区' : '还原完成,需要重启 DSH',
|
|
457
|
+
detail: result.display_hint ?? '请完全退出 DSH 客户端后重新启动。',
|
|
458
|
+
});
|
|
459
|
+
onDone?.(result);
|
|
460
|
+
return result;
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
const toggleOption = (key) => setOptions((current) => ({ ...current, [key]: !current[key] }));
|
|
464
|
+
const canCreate = status?.root_chosen === true && status?.root_writable !== false;
|
|
465
|
+
|
|
466
|
+
if (loading && status === null) {
|
|
467
|
+
return h('div', { className: 'tm-root' }, h('div', { className: 'tm-empty' }, '正在读取还原点状态…'));
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (status?.root_chosen !== true) {
|
|
471
|
+
return h('div', { className: 'tm-root' },
|
|
472
|
+
h('div', { className: 'tm-head' }, [
|
|
473
|
+
h('div', { key: 't' }, [h('h2', { className: 'tm-title' }, 'DSH 一键还原点'), h('div', { className: 'tm-sub' }, '先选择保存文件夹,之后所有还原点都放在这里。')]),
|
|
474
|
+
]),
|
|
475
|
+
h('div', { className: 'tm-card' }, [
|
|
476
|
+
h('div', { key: 'h', className: 'tm-cardTitle' }, '选择还原点保存文件夹'),
|
|
477
|
+
h('div', { key: 'p', className: 'tm-muted' }, status?.platform === 'win32'
|
|
478
|
+
? `Windows 默认使用 ${status?.root_default ?? 'D:\\ToFuture'};也可以换到其他磁盘。`
|
|
479
|
+
: 'macOS / Linux 不会自动写死路径,请选择你自己的文件夹。'),
|
|
480
|
+
h('div', { key: 'r', className: 'tm-row', style: { marginTop: 10 } }, [
|
|
481
|
+
h('button', { key: 'pick', type: 'button', className: 'tm-btn primary', disabled: busy !== '', onClick: changeRoot }, busy === 'pick' ? '正在打开…' : '选择文件夹'),
|
|
482
|
+
status?.root_defaultable ? h('button', { key: 'default', type: 'button', className: 'tm-btn', disabled: busy !== '', onClick: useDefault }, `使用默认 ${status.root_default}`) : null,
|
|
483
|
+
]),
|
|
484
|
+
h('div', { key: 'e', className: 'tm-mono', style: { marginTop: 8 } }, status?.root || '尚未选择'),
|
|
485
|
+
]),
|
|
486
|
+
error ? h(Banner, { kind: 'error', title: '读取失败', detail: `${error.reason_code ?? ''} ${error.detail ?? ''}` }) : null,
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const counts = status?.counts ?? {};
|
|
491
|
+
return h('div', { className: 'tm-root', 'data-tm-surface': compact ? 'settings' : 'main' },
|
|
492
|
+
h('div', { className: 'tm-head' }, [
|
|
493
|
+
h('div', { key: 't', style: { minWidth: 220 } }, [
|
|
494
|
+
h('h2', { className: 'tm-title' }, 'DSH 一键还原点'),
|
|
495
|
+
h('div', { key: 's', className: 'tm-sub' }, `保存到 ${status?.root ?? ''} · DSH ${status?.dsh_home ?? ''} · ${counts.plugins ?? 0} 个第三方插件`),
|
|
496
|
+
]),
|
|
497
|
+
h('div', { key: 'a', className: 'tm-row' }, [
|
|
498
|
+
h('button', { key: 'pick', type: 'button', className: 'tm-btn ghost', disabled: busy !== '', onClick: changeRoot }, '更改文件夹'),
|
|
499
|
+
h('button', { key: 'refresh', type: 'button', className: 'tm-btn ghost', disabled: busy !== '', onClick: () => void refresh() }, '刷新'),
|
|
500
|
+
]),
|
|
501
|
+
]),
|
|
502
|
+
notice ? h(Banner, { kind: notice.kind, title: notice.title, detail: notice.detail, onClose: () => setNotice(null) }) : null,
|
|
503
|
+
error ? h(Banner, { kind: 'error', title: '操作失败', detail: `${error.reason_code ?? ''} ${error.detail ?? ''}`, onClose: () => setError(null) }) : null,
|
|
504
|
+
|
|
505
|
+
h('div', { className: 'tm-card', 'data-tm-create': 'true' }, [
|
|
506
|
+
h('div', { key: 'h', className: 'tm-cardTitle' }, [
|
|
507
|
+
h('span', { key: 't' }, '一键设置还原点'),
|
|
508
|
+
h('span', { key: 'n', className: 'tm-tag' }, `手动保留 ${status?.settings?.keepManual ?? 2} 份`),
|
|
509
|
+
]),
|
|
510
|
+
h('div', { key: 'grid', className: 'tm-row', style: { alignItems: 'stretch' } }, [
|
|
511
|
+
h('label', { key: 'config', className: 'tm-check' }, [
|
|
512
|
+
h('input', { type: 'checkbox', checked: options.includeConfig, onChange: () => toggleOption('includeConfig') }),
|
|
513
|
+
h('span', { key: 'x' }, [h('b', { key: 'b' }, '用户插件与 DSH 配置'), h('span', { key: 's' }, '内置/实验性插件的开关与参数、通用配置、自定义模型、状态文件(账户与凭据除外)')]),
|
|
514
|
+
]),
|
|
515
|
+
h('label', { key: 'plugins', className: 'tm-check' }, [
|
|
516
|
+
h('input', { type: 'checkbox', checked: options.includePlugins, onChange: () => toggleOption('includePlugins') }),
|
|
517
|
+
h('span', { key: 'x' }, [h('b', { key: 'b' }, '第三方插件安装列表'), h('span', { key: 's' }, `还原时在确认弹窗手动勾选“安装并还原”,联网安装 npm latest;当前 ${counts.plugins ?? 0} 个,默认不保存安装包`) ]),
|
|
518
|
+
]),
|
|
519
|
+
h('label', { key: 'sessions', className: 'tm-check' }, [
|
|
520
|
+
h('input', { type: 'checkbox', checked: options.includeSessions, onChange: () => toggleOption('includeSessions') }),
|
|
521
|
+
h('span', { key: 'x' }, [h('b', { key: 'b' }, '会话与文件'), h('span', { key: 's' }, '每个会话按 Session ID 单独存放,并生成 RESTORE-GUIDE.json / .md 还原引导')]),
|
|
522
|
+
]),
|
|
523
|
+
]),
|
|
524
|
+
h('div', { key: 'row', className: 'tm-row', style: { marginTop: 10 } }, [
|
|
525
|
+
h('select', { key: 'scope', className: 'tm-input', value: options.sessionScope, onChange: (event) => setOptions((current) => ({ ...current, sessionScope: event.target.value })) }, [
|
|
526
|
+
h('option', { key: 'active', value: 'active' }, '活跃会话(默认,不含已归档)'),
|
|
527
|
+
h('option', { key: 'all', value: 'all' }, '所有会话'),
|
|
528
|
+
h('option', { key: 'archived', value: 'archived' }, '已归档会话'),
|
|
529
|
+
]),
|
|
530
|
+
h('label', { key: 'pkg', className: 'tm-check', style: { minWidth: 220, padding: '4px 8px' } }, [
|
|
531
|
+
h('input', { type: 'checkbox', checked: options.includePluginPackages, onChange: () => toggleOption('includePluginPackages') }),
|
|
532
|
+
h('span', { key: 'x' }, [h('b', { key: 'b' }, '同时备份插件安装包'), h('span', { key: 's' }, '默认关闭;勾选后还原可离线安装 .tgz')]),
|
|
533
|
+
]),
|
|
534
|
+
h('button', { key: 'go', type: 'button', className: 'tm-btn primary', style: { marginLeft: 'auto' }, disabled: busy !== '' || canCreate !== true, onClick: create }, busy === 'create' ? '正在写入…' : '一键设置还原点'),
|
|
535
|
+
]),
|
|
536
|
+
h('div', { key: 'pj', style: { marginTop: 8 } }, [
|
|
537
|
+
h('div', { className: 'tm-row' }, [
|
|
538
|
+
h('span', { key: 'l', className: 'tm-muted' }, '插件备份范围'),
|
|
539
|
+
h('button', { key: 'b', type: 'button', className: 'tm-btn ghost', onClick: () => setShowPlugins((value) => !value) }, showPlugins ? '收起' : '查看插件清单/参数'),
|
|
540
|
+
]),
|
|
541
|
+
showPlugins ? h(PluginSummary, { status, onOpen: () => setShowPlugins(false) }) : null,
|
|
542
|
+
]),
|
|
543
|
+
]),
|
|
544
|
+
|
|
545
|
+
h('div', { className: 'tm-card', 'data-tm-auto': 'true' }, [
|
|
546
|
+
h('div', { key: 'h', className: 'tm-cardTitle' }, [
|
|
547
|
+
h('span', { key: 't' }, '自动还原点'),
|
|
548
|
+
h('span', { key: 'n', className: `tm-tag ${status?.settings?.autoEnabled ? 'ok' : ''}` }, status?.settings?.autoEnabled ? `已开启 · 每天 ${status.settings.autoTime}` : '默认关闭'),
|
|
549
|
+
]),
|
|
550
|
+
h('div', { key: 'r', className: 'tm-row' }, [
|
|
551
|
+
h('label', { key: 'sw', className: 'tm-check', style: { minWidth: 180, padding: '6px 9px' } }, [
|
|
552
|
+
h('input', { type: 'checkbox', checked: autoOn, onChange: (event) => void saveAuto(event.target.checked, autoTime) }),
|
|
553
|
+
h('span', { key: 'x' }, [h('b', { key: 'b' }, '定时更新还原点'), h('span', { key: 's' }, `自动存放,保留 ${status?.settings?.keepAuto ?? 2} 份,和手动分开显示`)]),
|
|
554
|
+
]),
|
|
555
|
+
h('input', { key: 'time', className: 'tm-input', type: 'time', value: autoTime, onChange: (event) => setAutoTime(event.target.value), onBlur: () => void saveAuto(autoOn, autoTime), disabled: busy !== '' }),
|
|
556
|
+
h('button', { key: 'run', type: 'button', className: 'tm-btn', disabled: busy !== '' || !status?.settings?.autoEnabled, onClick: () => run('auto', () => request('/auto/run', { method: 'POST', body: '{}' })) }, '立即生成一份'),
|
|
557
|
+
]),
|
|
558
|
+
status?.last_auto?.at ? h('div', { key: 'last', className: 'tm-muted', style: { marginTop: 6 } }, `上次自动:${formatTime(status.last_auto.at)}${status.last_auto.ok === false ? ` · 失败:${status.last_auto.error ?? ''}` : ''}`) : null,
|
|
559
|
+
]),
|
|
560
|
+
|
|
561
|
+
h('div', { className: 'tm-card', 'data-tm-list': 'true' }, [
|
|
562
|
+
h('div', { key: 'h', className: 'tm-cardTitle' }, [
|
|
563
|
+
h('span', { key: 't' }, '还原点(手动 / 自动 / 导入 分开存储)'),
|
|
564
|
+
h('div', { key: 'a', className: 'tm-row' }, [
|
|
565
|
+
h('button', { key: 'imp', type: 'button', className: 'tm-btn', disabled: busy !== '', onClick: importPoint }, '导入其他机器的还原点'),
|
|
566
|
+
]),
|
|
567
|
+
]),
|
|
568
|
+
h('div', { key: 'tabs', className: 'tm-tabs' }, [
|
|
569
|
+
h('button', { key: 'manual', type: 'button', className: `tm-tab ${tab === 'manual' ? 'active' : ''}`, onClick: () => setTab('manual') }, `手动 ${counts.manual ?? 0}`),
|
|
570
|
+
h('button', { key: 'auto', type: 'button', className: `tm-tab ${tab === 'auto' ? 'active' : ''}`, onClick: () => setTab('auto') }, `自动 ${counts.auto ?? 0}`),
|
|
571
|
+
h('button', { key: 'imported', type: 'button', className: `tm-tab ${tab === 'imported' ? 'active' : ''}`, onClick: () => setTab('imported') }, `导入 ${counts.imported ?? 0}`),
|
|
572
|
+
]),
|
|
573
|
+
points.length === 0
|
|
574
|
+
? h('div', { key: 'empty', className: 'tm-empty', style: { marginTop: 10 } }, tab === 'manual' ? '还没有手动还原点。点上面的「一键设置还原点」即可创建第一份。' : tab === 'auto' ? '自动还原点默认关闭;打开上面的开关,或先手动生成一份。' : '还没有导入其他机器的还原点。')
|
|
575
|
+
: h('div', { key: 'list', className: 'tm-list', style: { marginTop: 10 } }, points.map((point) => h(PointRow, { key: point.file, point, busy: busy !== '', onRestore: startRestore, onDetails: setDetails }))),
|
|
576
|
+
]),
|
|
577
|
+
|
|
578
|
+
h('details', { key: 'adv', className: 'tm-details' }, [
|
|
579
|
+
h('summary', { key: 's' }, '说明与边界'),
|
|
580
|
+
h('div', { key: 'b', className: 'tm-detailsBody' }, [
|
|
581
|
+
h('div', { key: 'p1' }, '还原会先写一份预还原副本到 DSH home 的 .timemachine-backups/,再写入会话与配置;校验失败会回滚本次写入。'),
|
|
582
|
+
h('div', { key: 'p2' }, '账户与凭据(.credentials.yaml、匿名 ID、xclient/llm-deepseek 会话)不会备份;插件安装包默认不保存,可手动勾选。'),
|
|
583
|
+
h('div', { key: 'p3' }, '还原完成后会自动导入工作区并打开第一个会话;配置文件与插件列表会在下次启动时生效。若宿主未能即时注册,才需要完全退出并重启 DSH。'),
|
|
584
|
+
h('div', { key: 'p4', className: 'tm-mono' }, `format: twinsearth-timemachine-point/1 · profile: ${status?.profile ?? 'desktop'}`),
|
|
585
|
+
]),
|
|
586
|
+
]),
|
|
587
|
+
|
|
588
|
+
details === null ? null : h(DetailsModal, { details, onClose: () => setDetails(null), onRestore: () => { setDetails(null); void startRestore(details); } }),
|
|
589
|
+
h(PlanModal, { plan, busy: busy === 'restore', pathMap, setPathMap, installPlugins, setInstallPlugins, onCancel: () => setPlan(null), onConfirm: confirmRestore }),
|
|
590
|
+
);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function MainPage({ onRestoreDone = null }) {
|
|
594
|
+
return h('div', { style: { padding: '18px 20px', maxWidth: 1080, margin: '0 auto' } }, h(RestorePointBody, { onDone: onRestoreDone }));
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function SettingsGeneralRow({ open }) {
|
|
598
|
+
const { status, loading } = useStatus();
|
|
599
|
+
const [busy, setBusy] = useState('');
|
|
600
|
+
const [note, setNote] = useState('');
|
|
601
|
+
const create = async () => {
|
|
602
|
+
if (busy !== '') return;
|
|
603
|
+
setBusy('create');
|
|
604
|
+
setNote('');
|
|
605
|
+
try {
|
|
606
|
+
await request('/create', { method: 'POST', body: JSON.stringify({ kind: 'manual' }) });
|
|
607
|
+
setNote('已创建');
|
|
608
|
+
} catch (caught) {
|
|
609
|
+
setNote(caught.payload?.detail ?? caught.message);
|
|
610
|
+
} finally {
|
|
611
|
+
setBusy('');
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
const counts = status?.counts ?? {};
|
|
615
|
+
return h('div', {
|
|
616
|
+
className: 'tm-row',
|
|
617
|
+
style: { width: '100%', justifyContent: 'space-between', gap: 16, padding: '10px 0' },
|
|
618
|
+
'data-tm-general-entry': 'true',
|
|
619
|
+
}, [
|
|
620
|
+
h('div', { key: 'text', style: { minWidth: 0 } }, [
|
|
621
|
+
h('div', { key: 'title', style: { fontWeight: 600, fontSize: 13 } }, 'DSH 一键还原点'),
|
|
622
|
+
h('div', { key: 'desc', className: 'tm-muted', style: { marginTop: 2 } }, loading
|
|
623
|
+
? '正在读取…'
|
|
624
|
+
: `手动 ${counts.manual ?? 0} 份 · 自动 ${counts.auto ?? 0} 份 · 保存到 ${status?.root ?? '未设置'}${note ? ` · ${note}` : ''}`),
|
|
625
|
+
]),
|
|
626
|
+
h('div', { key: 'actions', className: 'tm-row' }, [
|
|
627
|
+
h('button', { key: 'create', type: 'button', className: 'tm-btn', disabled: busy !== '' || status?.root_chosen !== true, onClick: create }, busy === 'create' ? '正在备份…' : '立即备份'),
|
|
628
|
+
h('button', {
|
|
629
|
+
key: 'open',
|
|
630
|
+
type: 'button',
|
|
631
|
+
className: 'tm-btn primary',
|
|
632
|
+
onClick: () => { try { open?.(); } catch { /* layout unavailable */ } },
|
|
633
|
+
}, '打开'),
|
|
634
|
+
]),
|
|
635
|
+
]);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
function SidebarEntry({ size = 18 }) {
|
|
639
|
+
return h('svg', {
|
|
640
|
+
className: 'tm-icon',
|
|
641
|
+
width: size,
|
|
642
|
+
height: size,
|
|
643
|
+
viewBox: '0 0 24 24',
|
|
644
|
+
fill: 'none',
|
|
645
|
+
stroke: 'currentColor',
|
|
646
|
+
strokeWidth: 1.8,
|
|
647
|
+
strokeLinecap: 'round',
|
|
648
|
+
strokeLinejoin: 'round',
|
|
649
|
+
'aria-hidden': 'true',
|
|
650
|
+
'data-tm-sidebar': 'true',
|
|
651
|
+
}, [
|
|
652
|
+
h('circle', { key: 'c', cx: 12, cy: 12, r: 8 }),
|
|
653
|
+
h('path', { key: 'h', d: 'M12 7v5l-3 2' }),
|
|
654
|
+
h('path', { key: 'a', d: 'M4.5 4.5 2.5 6.5M2.5 6.5 4.5 8.5' }),
|
|
655
|
+
]);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function installCaptionEntry(openPanel) {
|
|
659
|
+
if (typeof document === 'undefined' || typeof window === 'undefined') return () => {};
|
|
660
|
+
const ATTR = 'data-timemachine-caption';
|
|
661
|
+
let button = null;
|
|
662
|
+
let observer = null;
|
|
663
|
+
let timer = null;
|
|
664
|
+
|
|
665
|
+
const stopWatching = () => {
|
|
666
|
+
if (observer !== null) {
|
|
667
|
+
try { observer.disconnect(); } catch { /* already gone */ }
|
|
668
|
+
observer = null;
|
|
669
|
+
}
|
|
670
|
+
if (timer !== null) {
|
|
671
|
+
try { window.clearTimeout(timer); } catch { /* already gone */ }
|
|
672
|
+
timer = null;
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
const cleanup = () => {
|
|
677
|
+
stopWatching();
|
|
678
|
+
if (button !== null) {
|
|
679
|
+
try { button.remove(); } catch { /* already gone */ }
|
|
680
|
+
button = null;
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
const mount = () => {
|
|
685
|
+
if (button !== null && button.isConnected) return true;
|
|
686
|
+
const host = document.querySelector('[data-windows-menu]');
|
|
687
|
+
const bar = host?.shadowRoot?.querySelector('[role="menubar"]');
|
|
688
|
+
if (!bar) return false;
|
|
689
|
+
const existing = bar.querySelector(`[${ATTR}]`);
|
|
690
|
+
if (existing !== null) {
|
|
691
|
+
button = existing;
|
|
692
|
+
return true;
|
|
693
|
+
}
|
|
694
|
+
const element = document.createElement('button');
|
|
695
|
+
element.type = 'button';
|
|
696
|
+
element.setAttribute('role', 'menuitem');
|
|
697
|
+
element.setAttribute('aria-label', 'DSH 一键还原点');
|
|
698
|
+
element.setAttribute(ATTR, '');
|
|
699
|
+
element.tabIndex = -1;
|
|
700
|
+
element.textContent = 'DSH一键还原点';
|
|
701
|
+
element.addEventListener('pointerdown', (event) => event.preventDefault());
|
|
702
|
+
element.addEventListener('click', (event) => {
|
|
703
|
+
event.preventDefault();
|
|
704
|
+
openPanel();
|
|
705
|
+
});
|
|
706
|
+
bar.append(element);
|
|
707
|
+
button = element;
|
|
708
|
+
return true;
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
if (mount()) return cleanup;
|
|
712
|
+
try {
|
|
713
|
+
observer = new MutationObserver(() => {
|
|
714
|
+
if (!mount()) return;
|
|
715
|
+
stopWatching();
|
|
716
|
+
});
|
|
717
|
+
observer.observe(document.body ?? document.documentElement, { childList: true, subtree: true });
|
|
718
|
+
timer = window.setTimeout(stopWatching, 30_000);
|
|
719
|
+
} catch { /* no DOM observer in a test harness */ }
|
|
720
|
+
return cleanup;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
function mount(ctx) {
|
|
724
|
+
const slots = ctx?.slots;
|
|
725
|
+
if (typeof slots?.inject !== 'function' || typeof slots?.register !== 'function') {
|
|
726
|
+
console.error('@twinsearth/timemachine-dsh-plugin: no slot registry; UI not mounted');
|
|
727
|
+
return () => {};
|
|
728
|
+
}
|
|
729
|
+
const guard = Symbol.for('@twinsearth/timemachine-dsh-plugin.client.mounted');
|
|
730
|
+
try {
|
|
731
|
+
if (slots[guard] === true) return () => {};
|
|
732
|
+
Object.defineProperty(slots, guard, { value: true, configurable: true, enumerable: false });
|
|
733
|
+
} catch { /* frozen host */ }
|
|
734
|
+
const disposers = [];
|
|
735
|
+
const styleDisposer = injectStyle(typeof document === 'undefined' ? null : document);
|
|
736
|
+
if (typeof styleDisposer === 'function') disposers.push(styleDisposer);
|
|
737
|
+
const mountSlot = (seat, options, component, what) => {
|
|
738
|
+
try {
|
|
739
|
+
const dispose = slots.inject(seat, () => slots.register({ ...options, name: seat }, component));
|
|
740
|
+
if (typeof dispose === 'function') disposers.push(dispose);
|
|
741
|
+
return true;
|
|
742
|
+
} catch (error) {
|
|
743
|
+
console.error(`@twinsearth/timemachine-dsh-plugin: ${what} was not mounted`, error);
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
const getService = (name) => (typeof ctx?.get === 'function' ? ctx.get(name) : undefined);
|
|
748
|
+
const layout = getService('layout') ?? (ctx?.layout ?? undefined);
|
|
749
|
+
const openPanel = () => {
|
|
750
|
+
try {
|
|
751
|
+
layout?.selectPanel?.(PANEL_ID);
|
|
752
|
+
} catch (error) {
|
|
753
|
+
console.error('@twinsearth/timemachine-dsh-plugin: could not open the panel', error);
|
|
754
|
+
}
|
|
755
|
+
};
|
|
756
|
+
try {
|
|
757
|
+
window.__DSH_TIMEMACHINE_OPEN__ = openPanel;
|
|
758
|
+
} catch { /* non-browser test harness */ }
|
|
759
|
+
const captionDisposer = installCaptionEntry(openPanel);
|
|
760
|
+
if (typeof captionDisposer === 'function') disposers.push(captionDisposer);
|
|
761
|
+
const openRestored = (result) => {
|
|
762
|
+
const sessionId = result?.display?.session_id;
|
|
763
|
+
if (typeof sessionId !== 'string' || sessionId === '') return;
|
|
764
|
+
let attempts = 0;
|
|
765
|
+
const open = () => {
|
|
766
|
+
attempts += 1;
|
|
767
|
+
try {
|
|
768
|
+
const uiWorkspace = getService('uiWorkspace');
|
|
769
|
+
if (typeof uiWorkspace?.openSession !== 'function') throw new Error('uiWorkspace.openSession is unavailable');
|
|
770
|
+
uiWorkspace.openSession(sessionId);
|
|
771
|
+
} catch (error) {
|
|
772
|
+
if (attempts < 4) {
|
|
773
|
+
window.setTimeout(open, 350 * attempts);
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
console.error('@twinsearth/timemachine-dsh-plugin: restored session could not be opened', error);
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
const start = () => window.setTimeout(open, 350);
|
|
780
|
+
try {
|
|
781
|
+
const sessions = getService('sessions');
|
|
782
|
+
const refresh = typeof sessions?.refresh === 'function' ? sessions.refresh() : null;
|
|
783
|
+
if (refresh !== null && typeof refresh.then === 'function') refresh.then(start, (error) => {
|
|
784
|
+
console.warn('@twinsearth/timemachine-dsh-plugin: session list refresh failed; opening anyway', error);
|
|
785
|
+
start();
|
|
786
|
+
});
|
|
787
|
+
else start();
|
|
788
|
+
} catch (error) {
|
|
789
|
+
console.warn('@twinsearth/timemachine-dsh-plugin: session list refresh threw; opening anyway', error);
|
|
790
|
+
start();
|
|
791
|
+
}
|
|
792
|
+
};
|
|
793
|
+
mountSlot('settings.general.item', { id: 'timemachine-general', order: 80, label: () => 'DSH 一键还原点', inject: () => ({ open: openPanel }) }, SettingsGeneralRow, 'the general-settings row');
|
|
794
|
+
mountSlot('sidebar.panellist', { id: PANEL_ID, order: 3, label: () => 'DSH 一键还原点' }, SidebarEntry, 'the sidebar entry');
|
|
795
|
+
mountSlot('main', { key: PANEL_ID, label: () => 'DSH 一键还原点', inject: () => ({ onRestoreDone: openRestored }) }, MainPage, 'the main panel');
|
|
796
|
+
return () => {
|
|
797
|
+
try {
|
|
798
|
+
if (window.__DSH_TIMEMACHINE_OPEN__ === openPanel) delete window.__DSH_TIMEMACHINE_OPEN__;
|
|
799
|
+
} catch { /* non-browser test harness */ }
|
|
800
|
+
for (const dispose of disposers.splice(0)) {
|
|
801
|
+
try { dispose(); } catch { /* already gone */ }
|
|
802
|
+
}
|
|
803
|
+
};
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
exports.apply = (ctx) => mount(ctx);
|
|
807
|
+
exports.inject = ['slots', 'layout'];
|
|
808
|
+
exports.RestorePointBody = RestorePointBody;
|
|
809
|
+
exports.MainPage = MainPage;
|
|
810
|
+
exports.SettingsGeneralRow = SettingsGeneralRow;
|
|
811
|
+
exports.SidebarEntry = SidebarEntry;
|
|
812
|
+
exports.injectStyle = injectStyle;
|
|
813
|
+
exports.formatBytes = formatBytes;
|
|
814
|
+
exports.formatTime = formatTime;
|
|
815
|
+
exports.ROUTE_PREFIX = PREFIX;
|
|
816
|
+
return exports;
|
|
817
|
+
},
|
|
818
|
+
});
|