luckytri 1.0.0 → 1.0.2

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.
Files changed (139) hide show
  1. package/.github/SECURITY.md +4 -4
  2. package/README.en.md +252 -250
  3. package/README.md +252 -250
  4. package/bin/luckytri.js +5 -1
  5. package/docs/en/CHANGELOG.md +8 -0
  6. package/docs/en/connect.md +18 -18
  7. package/docs/en/guide.md +202 -216
  8. package/docs/en/npm-release.md +25 -25
  9. package/docs/zh/CHANGELOG.md +8 -0
  10. package/docs/zh/connect.md +17 -17
  11. package/docs/zh/guide.md +202 -216
  12. package/docs/zh/npm-release.md +33 -33
  13. package/package.json +110 -108
  14. package/plugins/sdk/index.d.ts +155 -155
  15. package/plugins/sdk/testing.js +58 -58
  16. package/plugins/weather/index.js +82 -82
  17. package/plugins/weather/luckytri-plugin.json +29 -29
  18. package/public/app/assets/ChatsPage-BhlJHseG.js +3 -0
  19. package/public/app/assets/Empty-DP6WMGHi.js +2 -0
  20. package/public/app/assets/Field-CtbdqaiB.js +1 -0
  21. package/public/app/assets/HeartPage-32BeTPn2.js +1 -0
  22. package/public/app/assets/LifePage-CxSm0XKI.js +1 -0
  23. package/public/app/assets/MemoryPage-BeKBcPCY.js +1 -0
  24. package/public/app/assets/NaturePage-NEPYl2HS.js +3 -0
  25. package/public/app/assets/PeoplePage-DxtpekwM.js +1 -0
  26. package/public/app/assets/PluginsPage-CVVQwEBI.css +1 -0
  27. package/public/app/assets/PluginsPage-DksEZbUU.js +2 -0
  28. package/public/app/assets/{SystemPage-CAMPoYqM.css → SystemPage-BkRu0Rvx.css} +1 -1
  29. package/public/app/assets/SystemPage-BvzJlHuY.js +1 -0
  30. package/public/app/assets/TimePage-7-awH5XL.js +3 -0
  31. package/public/app/assets/index-4rItIkD-.js +4 -0
  32. package/public/app/assets/{index-YDz1ree1.css → index-DY4e15Kh.css} +1 -1
  33. package/public/app/assets/{knowledge-DG0e0uKw.js → knowledge-C6_HvoyB.js} +1 -1
  34. package/public/app/assets/media-SMfTT-yc.js +1 -0
  35. package/public/app/index.html +3 -3
  36. package/public/guide.en.html +72 -99
  37. package/public/guide.html +44 -86
  38. package/public/tutorial.en.md +25 -39
  39. package/public/tutorial.md +25 -39
  40. package/scripts/backup.js +311 -311
  41. package/scripts/browser.js +41 -41
  42. package/scripts/import-data.js +128 -0
  43. package/scripts/launch.js +101 -94
  44. package/scripts/plugin.js +150 -150
  45. package/scripts/reset-password.js +22 -0
  46. package/scripts/runtime.js +25 -25
  47. package/scripts/service.js +56 -47
  48. package/scripts/setup.js +50 -50
  49. package/scripts/start-server.js +41 -41
  50. package/scripts/stop.js +36 -36
  51. package/server/app.js +112 -96
  52. package/server/auth.js +217 -0
  53. package/server/channels/contract.js +57 -57
  54. package/server/channels/hub.js +165 -165
  55. package/server/channels/index.js +28 -25
  56. package/server/channels/onebot/index.js +335 -336
  57. package/server/channels/onebot/token.js +22 -3
  58. package/server/core/capabilities.js +208 -208
  59. package/server/core/events.js +64 -60
  60. package/server/core/image-preprocessor.js +208 -208
  61. package/server/core/model-manager.js +1213 -1213
  62. package/server/core/net-guard.js +49 -49
  63. package/server/core/orchestrator.js +569 -569
  64. package/server/core/perceivers.js +91 -91
  65. package/server/core/persona-manager.js +243 -243
  66. package/server/core/repository.js +293 -293
  67. package/server/core/response-validator.js +508 -508
  68. package/server/core/turn-processor.js +630 -630
  69. package/server/core/turn.js +236 -236
  70. package/server/core/vision-manager.js +633 -633
  71. package/server/i18n/errors.js +548 -497
  72. package/server/i18n/labels.js +649 -653
  73. package/server/index.js +151 -102
  74. package/server/knowledge/manager.js +386 -386
  75. package/server/knowledge/schema.js +12 -8
  76. package/server/local-access.js +32 -0
  77. package/server/mind/anticipations.js +507 -507
  78. package/server/mind/api.js +703 -703
  79. package/server/mind/budget.js +152 -152
  80. package/server/mind/context-selection.js +61 -61
  81. package/server/mind/day-planner.js +324 -324
  82. package/server/mind/evidence.js +148 -148
  83. package/server/mind/index.js +445 -445
  84. package/server/mind/life/diary.js +786 -786
  85. package/server/mind/life/index.js +1049 -1049
  86. package/server/mind/observations.js +84 -84
  87. package/server/mind/own-day.js +324 -324
  88. package/server/mind/own-voice.js +318 -318
  89. package/server/mind/senses.js +53 -53
  90. package/server/mind/time/activity-clock.js +196 -196
  91. package/server/mind/time/availability.js +69 -69
  92. package/server/mind/time/commitment-review.js +198 -198
  93. package/server/mind/time/executor.js +512 -512
  94. package/server/mind/time/experiences.js +128 -128
  95. package/server/mind/time/games.js +562 -562
  96. package/server/mind/time/index.js +739 -739
  97. package/server/mind/time/intent.js +166 -166
  98. package/server/mind/time/kinds.js +171 -171
  99. package/server/mind/time/plugin-activity.js +207 -207
  100. package/server/mind/time/tasks.js +587 -587
  101. package/server/mind/util.js +110 -110
  102. package/server/mind/view.js +273 -273
  103. package/server/paths.js +102 -102
  104. package/server/plugins/api.js +204 -204
  105. package/server/plugins/host.js +967 -967
  106. package/server/plugins/install.js +295 -295
  107. package/server/plugins/manifest.js +201 -201
  108. package/server/plugins/permissions.js +42 -42
  109. package/server/plugins/runner/guard.mjs +36 -36
  110. package/server/plugins/runner/worker.mjs +250 -250
  111. package/server/plugins/schema.js +91 -91
  112. package/server/plugins/zip.js +112 -112
  113. package/server/startup-lock.js +118 -118
  114. package/server/storage/archive.js +242 -241
  115. package/server/storage/backup-cleanup-worker.js +8 -8
  116. package/server/storage/backup-cleanup.js +415 -414
  117. package/server/storage/backup-lock.js +76 -76
  118. package/server/storage/backup-scheduler.js +131 -131
  119. package/server/storage/data-package.js +298 -0
  120. package/server/storage/data-transfer-api.js +50 -0
  121. package/server/storage/data-transfer-worker.js +18 -0
  122. package/server/storage/data-transfer.js +415 -0
  123. package/server/storage/store.js +202 -199
  124. package/server/studio/readiness.js +181 -190
  125. package/server/studio/settings.js +236 -236
  126. package/public/app/assets/ChatsPage-C-FcRapR.js +0 -3
  127. package/public/app/assets/Empty-CpP5kQHH.js +0 -2
  128. package/public/app/assets/Field-D4gY-INU.js +0 -1
  129. package/public/app/assets/HeartPage-CPy_thQd.js +0 -1
  130. package/public/app/assets/LifePage-CxWvwmSf.js +0 -1
  131. package/public/app/assets/MemoryPage-DC4rhQ68.js +0 -1
  132. package/public/app/assets/NaturePage-IvYw04Zq.js +0 -3
  133. package/public/app/assets/PeoplePage-Cqefm9-q.js +0 -1
  134. package/public/app/assets/PluginsPage-DKSaKvXb.js +0 -2
  135. package/public/app/assets/PluginsPage-HyBe5D8w.css +0 -1
  136. package/public/app/assets/SystemPage-CJmtJkrK.js +0 -1
  137. package/public/app/assets/TimePage-Cw44inM4.js +0 -3
  138. package/public/app/assets/index-BheRW-1t.js +0 -4
  139. package/public/app/assets/media-BzPDBm7e.js +0 -1
@@ -1,414 +1,415 @@
1
- import { Worker } from "node:worker_threads";
2
- import { runtimePaths } from "../paths.js";
3
- import { backupDirectory } from "../../scripts/backup.js";
4
- import { dirname, join, resolve } from "node:path";
5
- import {
6
- existsSync,
7
- lstatSync,
8
- readdirSync,
9
- readFileSync,
10
- realpathSync,
11
- rmSync,
12
- statSync,
13
- } from "node:fs";
14
- import { archiveIdentity, verifyArchive } from "./archive.js";
15
- import { withBackupLock } from "./backup-lock.js";
16
-
17
- const DAY = 86400000;
18
- const DEFAULTS = Object.freeze({
19
- enabled: true,
20
- time: "04:30",
21
- retainDays: 14,
22
- keepFull: 2,
23
- });
24
-
25
- function kind(scope, name) {
26
- if (scope === "root")
27
- return /^friend\.pre-[\w-]+\.db$/.test(name) ? "snapshot" : null;
28
- if (/^luckytri-auto-\d{4}-\d\d-\d\dT[\d-]+Z\.db$/.test(name)) return "auto";
29
- if (/^luckytri-migration-v\d+-to-v\d+-\d+-[\da-f]{8}\.db$/.test(name))
30
- return "migration";
31
- if (/^luckytri-\d{4}-\d\d-\d\dT[\d-]+Z-[\da-f]{8}\.db$/.test(name))
32
- return "full";
33
- if (/^(?:luckybot|unlucky|xiaoman)-[\w-]+\.db$/.test(name)) return "legacy";
34
- return null;
35
- }
36
-
37
- function stamped(name, modified) {
38
- const match = /(\d{4}-\d\d-\d\d)T(\d\d)-(\d\d)-(\d\d)-(\d{3})Z/.exec(name);
39
- const value = match
40
- ? Date.parse(`${match[1]}T${match[2]}:${match[3]}:${match[4]}.${match[5]}Z`)
41
- : NaN;
42
- return Number.isFinite(value) ? value : modified;
43
- }
44
-
45
- export function cleanupPolicy(value = {}) {
46
- if (
47
- typeof value !== "object" ||
48
- !value ||
49
- Array.isArray(value) ||
50
- typeof value.enabled !== "boolean" ||
51
- !/^([01]\d|2[0-3]):[0-5]\d$/.test(value.time) ||
52
- !Number.isInteger(value.retainDays) ||
53
- value.retainDays < 1 ||
54
- value.retainDays > 3650 ||
55
- !Number.isInteger(value.keepFull) ||
56
- value.keepFull < 0 ||
57
- value.keepFull > 100
58
- )
59
- throw Error("清理规则无效:保留天数为 1–3650,完整备份数量为 0–100");
60
- return Object.fromEntries(
61
- Object.keys(DEFAULTS).map((key) => [key, value[key]]),
62
- );
63
- }
64
-
65
- export function backupInventory({ directory, source }) {
66
- const root = dirname(resolve(source));
67
- const sourcePath = existsSync(source)
68
- ? realpathSync(source)
69
- : resolve(source);
70
- const sourceStat = existsSync(source) ? statSync(source) : null;
71
- const sourceId = sourceStat ? archiveIdentity(source) : null;
72
- const normalize = (path) =>
73
- process.platform === "win32" ? path.toLowerCase() : path;
74
- const folders = [
75
- ["backup", resolve(directory)],
76
- ["root", root],
77
- ];
78
- const entries = [];
79
- for (const [scope, folder] of folders) {
80
- if (!existsSync(folder)) continue;
81
- for (const entry of readdirSync(folder, { withFileTypes: true })) {
82
- const category = kind(scope, entry.name);
83
- if (!category || !entry.isFile()) continue;
84
- const path = join(folder, entry.name);
85
- const stat = lstatSync(path);
86
- if (!stat.isFile()) continue;
87
- if (
88
- normalize(realpathSync(path)) === normalize(sourcePath) ||
89
- (sourceStat?.ino &&
90
- stat.ino === sourceStat.ino &&
91
- stat.dev === sourceStat.dev)
92
- )
93
- continue;
94
- let databaseId = null;
95
- const manifestPath = `${path}.json`;
96
- if (existsSync(manifestPath)) {
97
- try {
98
- if (lstatSync(manifestPath).isFile())
99
- databaseId =
100
- JSON.parse(readFileSync(manifestPath, "utf8")).databaseId || null;
101
- } catch {
102
- // A damaged manifest is not a trusted recovery point.
103
- }
104
- }
105
- const inUse = existsSync(`${path}-wal`) || existsSync(`${path}-shm`);
106
- entries.push({
107
- scope,
108
- name: entry.name,
109
- kind: category,
110
- bytes: stat.size,
111
- modified: stat.mtimeMs,
112
- at: stamped(entry.name, stat.mtimeMs),
113
- manifest: existsSync(`${path}.json`),
114
- databaseId,
115
- protected: inUse,
116
- protectionReason: inUse ? "存在运行伴随文件" : "",
117
- });
118
- }
119
- }
120
- entries.sort((a, b) => b.at - a.at || b.name.localeCompare(a.name));
121
- for (const entry of entries
122
- .filter(
123
- (item) =>
124
- item.kind === "auto" && sourceId && item.databaseId === sourceId,
125
- )
126
- .slice(0, 2)) {
127
- entry.protected = true;
128
- entry.protectionReason = "近期自动恢复点";
129
- }
130
- return entries;
131
- }
132
-
133
- export function cleanupPlan(entries, policy, now = Date.now()) {
134
- const full = entries.filter((item) => item.kind === "full");
135
- const items = full.filter(
136
- (item, index) =>
137
- !item.protected &&
138
- (index >= policy.keepFull || item.at < now - policy.retainDays * DAY),
139
- );
140
- return { items, bytes: items.reduce((sum, item) => sum + item.bytes, 0) };
141
- }
142
-
143
- function safeRecoveryPoint(entries, directory, source, now, chosen) {
144
- const identity = archiveIdentity(source);
145
- const selected = new Set(chosen.map((item) => `${item.scope}:${item.name}`));
146
- for (const candidate of entries.filter(
147
- (item) =>
148
- item.kind === "auto" &&
149
- identity &&
150
- item.databaseId === identity &&
151
- item.manifest &&
152
- now - item.at <= 72 * 3600000 &&
153
- item.at <= now + 5 * 60000 &&
154
- !selected.has(`${item.scope}:${item.name}`),
155
- )) {
156
- try {
157
- const result = verifyArchive(join(resolve(directory), candidate.name));
158
- if (result.databaseId === identity)
159
- return {
160
- name: candidate.name,
161
- at: candidate.at,
162
- bytes: candidate.bytes,
163
- };
164
- } catch {
165
- // A newer damaged copy must not hide an older valid recovery point.
166
- }
167
- }
168
- throw Error("缺少同一实例近 72 小时内可校验的自动备份,已停止清理");
169
- }
170
-
171
- export function executeCleanup({
172
- directory,
173
- source,
174
- policy,
175
- mode,
176
- selection = [],
177
- now = Date.now(),
178
- remove = rmSync,
179
- }) {
180
- return withBackupLock(directory, () =>
181
- cleanupBatch({ directory, source, policy, mode, selection, now, remove }),
182
- );
183
- }
184
-
185
- function cleanupBatch({
186
- directory,
187
- source,
188
- policy,
189
- mode,
190
- selection,
191
- now,
192
- remove,
193
- }) {
194
- const entries = backupInventory({ directory, source });
195
- const chosen =
196
- mode === "scheduled"
197
- ? cleanupPlan(entries, cleanupPolicy(policy), now).items
198
- : selection.map((request) => {
199
- if (
200
- !request ||
201
- typeof request.name !== "string" ||
202
- typeof request.scope !== "string" ||
203
- !Number.isSafeInteger(request.bytes) ||
204
- !Number.isFinite(request.modified)
205
- )
206
- throw Error("选择的备份信息无效");
207
- const found = entries.find(
208
- (item) =>
209
- item.name === request.name && item.scope === request.scope,
210
- );
211
- if (
212
- !found ||
213
- found.protected ||
214
- found.bytes !== request.bytes ||
215
- found.modified !== request.modified
216
- )
217
- throw Error("备份已变化或属于必须保留的恢复点,请刷新列表");
218
- return found;
219
- });
220
- if (!["scheduled", "manual"].includes(mode)) throw Error("清理方式无效");
221
- if (
222
- new Set(chosen.map((item) => `${item.scope}:${item.name}`)).size !==
223
- chosen.length
224
- )
225
- throw Error("不能重复选择同一份备份");
226
- if (!chosen.length) return { removed: [], bytes: 0, failures: [] };
227
- const recovery = safeRecoveryPoint(entries, directory, source, now, chosen);
228
- const removed = [];
229
- const failures = [];
230
- for (const item of chosen) {
231
- const folder =
232
- item.scope === "root" ? dirname(resolve(source)) : resolve(directory);
233
- const path = join(folder, item.name);
234
- try {
235
- const stat = lstatSync(path);
236
- if (
237
- !stat.isFile() ||
238
- stat.size !== item.bytes ||
239
- stat.mtimeMs !== item.modified ||
240
- existsSync(`${path}-wal`) ||
241
- existsSync(`${path}-shm`)
242
- )
243
- throw Error("备份在清理期间发生变化,已停止后续清理");
244
- remove(path);
245
- removed.push({ scope: item.scope, name: item.name, bytes: item.bytes });
246
- remove(`${path}.json`, { force: true });
247
- } catch (error) {
248
- failures.push({
249
- scope: item.scope,
250
- name: item.name,
251
- error: error.code
252
- ? `删除失败(${error.code})`
253
- : "备份文件已变化或不可删除",
254
- });
255
- }
256
- }
257
- return {
258
- removed,
259
- bytes: removed.reduce((sum, item) => sum + item.bytes, 0),
260
- failures,
261
- recovery,
262
- };
263
- }
264
-
265
- function localDay(now) {
266
- const d = new Date(now);
267
- const day = [
268
- d.getFullYear(),
269
- String(d.getMonth() + 1).padStart(2, "0"),
270
- String(d.getDate()).padStart(2, "0"),
271
- ].join("-");
272
- const time = `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
273
- return { day, time };
274
- }
275
-
276
- function workerRun(payload) {
277
- return new Promise((resolve, reject) => {
278
- const worker = new Worker(
279
- new URL("./backup-cleanup-worker.js", import.meta.url),
280
- {
281
- workerData: payload,
282
- },
283
- );
284
- worker.once("message", ({ result, error }) =>
285
- error ? reject(Error(error)) : resolve(result),
286
- );
287
- worker.once("error", reject);
288
- worker.once("exit", (code) => {
289
- if (code) reject(Error(`清理工作异常结束:${code}`));
290
- });
291
- });
292
- }
293
-
294
- export function createBackupCleanup({
295
- store,
296
- backup,
297
- source = runtimePaths().database,
298
- directory = backupDirectory(source),
299
- now = () => Date.now(),
300
- run = workerRun,
301
- log = console,
302
- } = {}) {
303
- let running = false;
304
- const policy = () => ({ ...DEFAULTS, ...store.settings().backupCleanup });
305
- const last = () => store.settings().backupCleanupLast || null;
306
- const info = () => {
307
- const entries = backupInventory({ directory, source });
308
- return {
309
- policy: policy(),
310
- last: last(),
311
- running,
312
- serverTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
313
- totalBytes: entries.reduce((sum, item) => sum + item.bytes, 0),
314
- plan: cleanupPlan(entries, policy(), now()),
315
- entries,
316
- };
317
- };
318
- const launch = async (mode, selection = []) => {
319
- if (running || backup?.running)
320
- throw Error("备份或清理正在进行,请稍后再试");
321
- running = true;
322
- try {
323
- return await run({
324
- directory,
325
- source,
326
- policy: policy(),
327
- mode,
328
- selection,
329
- now: now(),
330
- });
331
- } finally {
332
- running = false;
333
- }
334
- };
335
- return {
336
- info,
337
- get running() {
338
- return running;
339
- },
340
- save(value) {
341
- const next = cleanupPolicy(value);
342
- store.save({ backupCleanup: next });
343
- return info();
344
- },
345
- async manual(selection) {
346
- if (
347
- !Array.isArray(selection) ||
348
- !selection.length ||
349
- selection.length > 100
350
- )
351
- throw Error("请先选择要清理的备份,单次最多 100 份");
352
- return launch("manual", selection);
353
- },
354
- tick() {
355
- const current = policy();
356
- const clock = localDay(now());
357
- if (
358
- !current.enabled ||
359
- running ||
360
- backup?.running ||
361
- last()?.day === clock.day ||
362
- clock.time < current.time
363
- )
364
- return false;
365
- const preview = cleanupPlan(
366
- backupInventory({ directory, source }),
367
- current,
368
- now(),
369
- );
370
- if (!preview.items.length) {
371
- store.save({
372
- backupCleanupLast: {
373
- day: clock.day,
374
- at: now(),
375
- removed: 0,
376
- bytes: 0,
377
- },
378
- });
379
- return false;
380
- }
381
- launch("scheduled")
382
- .then((result) => {
383
- store.save({
384
- backupCleanupLast: {
385
- day: clock.day,
386
- at: now(),
387
- removed: result.removed.length,
388
- bytes: result.bytes,
389
- ...(result.failures?.length
390
- ? {
391
- error: "部分备份未清理,请刷新列表后重试",
392
- failures: result.failures,
393
- }
394
- : {}),
395
- },
396
- });
397
- log.log(`定时备份整理完成:${result.removed.length} 份`);
398
- })
399
- .catch((error) => {
400
- store.save({
401
- backupCleanupLast: {
402
- day: clock.day,
403
- at: now(),
404
- removed: 0,
405
- bytes: 0,
406
- error: error.message,
407
- },
408
- });
409
- log.error(`定时备份整理失败:${error.message}`);
410
- });
411
- return true;
412
- },
413
- };
414
- }
1
+ import { Worker } from "node:worker_threads";
2
+ import { runtimePaths } from "../paths.js";
3
+ import { backupDirectory } from "../../scripts/backup.js";
4
+ import { dirname, join, resolve } from "node:path";
5
+ import {
6
+ existsSync,
7
+ lstatSync,
8
+ readdirSync,
9
+ readFileSync,
10
+ realpathSync,
11
+ rmSync,
12
+ statSync,
13
+ } from "node:fs";
14
+ import { archiveIdentity, verifyArchive } from "./archive.js";
15
+ import { withBackupLock } from "./backup-lock.js";
16
+
17
+ const DAY = 86400000;
18
+ const DEFAULTS = Object.freeze({
19
+ enabled: true,
20
+ time: "04:30",
21
+ retainDays: 14,
22
+ keepFull: 2,
23
+ });
24
+
25
+ function kind(scope, name) {
26
+ if (scope === "root")
27
+ return /^friend\.pre-[\w-]+\.db$/.test(name) ? "snapshot" : null;
28
+ if (/^luckytri-before-import-\d+-[\da-f]{8}\.db$/.test(name)) return "import";
29
+ if (/^luckytri-auto-\d{4}-\d\d-\d\dT[\d-]+Z\.db$/.test(name)) return "auto";
30
+ if (/^luckytri-migration-v\d+-to-v\d+-\d+-[\da-f]{8}\.db$/.test(name))
31
+ return "migration";
32
+ if (/^luckytri-\d{4}-\d\d-\d\dT[\d-]+Z-[\da-f]{8}\.db$/.test(name))
33
+ return "full";
34
+ if (/^(?:luckybot|unlucky|xiaoman)-[\w-]+\.db$/.test(name)) return "legacy";
35
+ return null;
36
+ }
37
+
38
+ function stamped(name, modified) {
39
+ const match = /(\d{4}-\d\d-\d\d)T(\d\d)-(\d\d)-(\d\d)-(\d{3})Z/.exec(name);
40
+ const value = match
41
+ ? Date.parse(`${match[1]}T${match[2]}:${match[3]}:${match[4]}.${match[5]}Z`)
42
+ : NaN;
43
+ return Number.isFinite(value) ? value : modified;
44
+ }
45
+
46
+ export function cleanupPolicy(value = {}) {
47
+ if (
48
+ typeof value !== "object" ||
49
+ !value ||
50
+ Array.isArray(value) ||
51
+ typeof value.enabled !== "boolean" ||
52
+ !/^([01]\d|2[0-3]):[0-5]\d$/.test(value.time) ||
53
+ !Number.isInteger(value.retainDays) ||
54
+ value.retainDays < 1 ||
55
+ value.retainDays > 3650 ||
56
+ !Number.isInteger(value.keepFull) ||
57
+ value.keepFull < 0 ||
58
+ value.keepFull > 100
59
+ )
60
+ throw Error("清理规则无效:保留天数为 1–3650,完整备份数量为 0–100");
61
+ return Object.fromEntries(
62
+ Object.keys(DEFAULTS).map((key) => [key, value[key]]),
63
+ );
64
+ }
65
+
66
+ export function backupInventory({ directory, source }) {
67
+ const root = dirname(resolve(source));
68
+ const sourcePath = existsSync(source)
69
+ ? realpathSync(source)
70
+ : resolve(source);
71
+ const sourceStat = existsSync(source) ? statSync(source) : null;
72
+ const sourceId = sourceStat ? archiveIdentity(source) : null;
73
+ const normalize = (path) =>
74
+ process.platform === "win32" ? path.toLowerCase() : path;
75
+ const folders = [
76
+ ["backup", resolve(directory)],
77
+ ["root", root],
78
+ ];
79
+ const entries = [];
80
+ for (const [scope, folder] of folders) {
81
+ if (!existsSync(folder)) continue;
82
+ for (const entry of readdirSync(folder, { withFileTypes: true })) {
83
+ const category = kind(scope, entry.name);
84
+ if (!category || !entry.isFile()) continue;
85
+ const path = join(folder, entry.name);
86
+ const stat = lstatSync(path);
87
+ if (!stat.isFile()) continue;
88
+ if (
89
+ normalize(realpathSync(path)) === normalize(sourcePath) ||
90
+ (sourceStat?.ino &&
91
+ stat.ino === sourceStat.ino &&
92
+ stat.dev === sourceStat.dev)
93
+ )
94
+ continue;
95
+ let databaseId = null;
96
+ const manifestPath = `${path}.json`;
97
+ if (existsSync(manifestPath)) {
98
+ try {
99
+ if (lstatSync(manifestPath).isFile())
100
+ databaseId =
101
+ JSON.parse(readFileSync(manifestPath, "utf8")).databaseId || null;
102
+ } catch {
103
+ // A damaged manifest is not a trusted recovery point.
104
+ }
105
+ }
106
+ const inUse = existsSync(`${path}-wal`) || existsSync(`${path}-shm`);
107
+ entries.push({
108
+ scope,
109
+ name: entry.name,
110
+ kind: category,
111
+ bytes: stat.size,
112
+ modified: stat.mtimeMs,
113
+ at: stamped(entry.name, stat.mtimeMs),
114
+ manifest: existsSync(`${path}.json`),
115
+ databaseId,
116
+ protected: inUse,
117
+ protectionReason: inUse ? "存在运行伴随文件" : "",
118
+ });
119
+ }
120
+ }
121
+ entries.sort((a, b) => b.at - a.at || b.name.localeCompare(a.name));
122
+ for (const entry of entries
123
+ .filter(
124
+ (item) =>
125
+ item.kind === "auto" && sourceId && item.databaseId === sourceId,
126
+ )
127
+ .slice(0, 2)) {
128
+ entry.protected = true;
129
+ entry.protectionReason = "近期自动恢复点";
130
+ }
131
+ return entries;
132
+ }
133
+
134
+ export function cleanupPlan(entries, policy, now = Date.now()) {
135
+ const full = entries.filter((item) => item.kind === "full");
136
+ const items = full.filter(
137
+ (item, index) =>
138
+ !item.protected &&
139
+ (index >= policy.keepFull || item.at < now - policy.retainDays * DAY),
140
+ );
141
+ return { items, bytes: items.reduce((sum, item) => sum + item.bytes, 0) };
142
+ }
143
+
144
+ function safeRecoveryPoint(entries, directory, source, now, chosen) {
145
+ const identity = archiveIdentity(source);
146
+ const selected = new Set(chosen.map((item) => `${item.scope}:${item.name}`));
147
+ for (const candidate of entries.filter(
148
+ (item) =>
149
+ item.kind === "auto" &&
150
+ identity &&
151
+ item.databaseId === identity &&
152
+ item.manifest &&
153
+ now - item.at <= 72 * 3600000 &&
154
+ item.at <= now + 5 * 60000 &&
155
+ !selected.has(`${item.scope}:${item.name}`),
156
+ )) {
157
+ try {
158
+ const result = verifyArchive(join(resolve(directory), candidate.name));
159
+ if (result.databaseId === identity)
160
+ return {
161
+ name: candidate.name,
162
+ at: candidate.at,
163
+ bytes: candidate.bytes,
164
+ };
165
+ } catch {
166
+ // A newer damaged copy must not hide an older valid recovery point.
167
+ }
168
+ }
169
+ throw Error("缺少同一实例近 72 小时内可校验的自动备份,已停止清理");
170
+ }
171
+
172
+ export function executeCleanup({
173
+ directory,
174
+ source,
175
+ policy,
176
+ mode,
177
+ selection = [],
178
+ now = Date.now(),
179
+ remove = rmSync,
180
+ }) {
181
+ return withBackupLock(directory, () =>
182
+ cleanupBatch({ directory, source, policy, mode, selection, now, remove }),
183
+ );
184
+ }
185
+
186
+ function cleanupBatch({
187
+ directory,
188
+ source,
189
+ policy,
190
+ mode,
191
+ selection,
192
+ now,
193
+ remove,
194
+ }) {
195
+ const entries = backupInventory({ directory, source });
196
+ const chosen =
197
+ mode === "scheduled"
198
+ ? cleanupPlan(entries, cleanupPolicy(policy), now).items
199
+ : selection.map((request) => {
200
+ if (
201
+ !request ||
202
+ typeof request.name !== "string" ||
203
+ typeof request.scope !== "string" ||
204
+ !Number.isSafeInteger(request.bytes) ||
205
+ !Number.isFinite(request.modified)
206
+ )
207
+ throw Error("选择的备份信息无效");
208
+ const found = entries.find(
209
+ (item) =>
210
+ item.name === request.name && item.scope === request.scope,
211
+ );
212
+ if (
213
+ !found ||
214
+ found.protected ||
215
+ found.bytes !== request.bytes ||
216
+ found.modified !== request.modified
217
+ )
218
+ throw Error("备份已变化或属于必须保留的恢复点,请刷新列表");
219
+ return found;
220
+ });
221
+ if (!["scheduled", "manual"].includes(mode)) throw Error("清理方式无效");
222
+ if (
223
+ new Set(chosen.map((item) => `${item.scope}:${item.name}`)).size !==
224
+ chosen.length
225
+ )
226
+ throw Error("不能重复选择同一份备份");
227
+ if (!chosen.length) return { removed: [], bytes: 0, failures: [] };
228
+ const recovery = safeRecoveryPoint(entries, directory, source, now, chosen);
229
+ const removed = [];
230
+ const failures = [];
231
+ for (const item of chosen) {
232
+ const folder =
233
+ item.scope === "root" ? dirname(resolve(source)) : resolve(directory);
234
+ const path = join(folder, item.name);
235
+ try {
236
+ const stat = lstatSync(path);
237
+ if (
238
+ !stat.isFile() ||
239
+ stat.size !== item.bytes ||
240
+ stat.mtimeMs !== item.modified ||
241
+ existsSync(`${path}-wal`) ||
242
+ existsSync(`${path}-shm`)
243
+ )
244
+ throw Error("备份在清理期间发生变化,已停止后续清理");
245
+ remove(path);
246
+ removed.push({ scope: item.scope, name: item.name, bytes: item.bytes });
247
+ remove(`${path}.json`, { force: true });
248
+ } catch (error) {
249
+ failures.push({
250
+ scope: item.scope,
251
+ name: item.name,
252
+ error: error.code
253
+ ? `删除失败(${error.code})`
254
+ : "备份文件已变化或不可删除",
255
+ });
256
+ }
257
+ }
258
+ return {
259
+ removed,
260
+ bytes: removed.reduce((sum, item) => sum + item.bytes, 0),
261
+ failures,
262
+ recovery,
263
+ };
264
+ }
265
+
266
+ function localDay(now) {
267
+ const d = new Date(now);
268
+ const day = [
269
+ d.getFullYear(),
270
+ String(d.getMonth() + 1).padStart(2, "0"),
271
+ String(d.getDate()).padStart(2, "0"),
272
+ ].join("-");
273
+ const time = `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
274
+ return { day, time };
275
+ }
276
+
277
+ function workerRun(payload) {
278
+ return new Promise((resolve, reject) => {
279
+ const worker = new Worker(
280
+ new URL("./backup-cleanup-worker.js", import.meta.url),
281
+ {
282
+ workerData: payload,
283
+ },
284
+ );
285
+ worker.once("message", ({ result, error }) =>
286
+ error ? reject(Error(error)) : resolve(result),
287
+ );
288
+ worker.once("error", reject);
289
+ worker.once("exit", (code) => {
290
+ if (code) reject(Error(`清理工作异常结束:${code}`));
291
+ });
292
+ });
293
+ }
294
+
295
+ export function createBackupCleanup({
296
+ store,
297
+ backup,
298
+ source = runtimePaths().database,
299
+ directory = backupDirectory(source),
300
+ now = () => Date.now(),
301
+ run = workerRun,
302
+ log = console,
303
+ } = {}) {
304
+ let running = false;
305
+ const policy = () => ({ ...DEFAULTS, ...store.settings().backupCleanup });
306
+ const last = () => store.settings().backupCleanupLast || null;
307
+ const info = () => {
308
+ const entries = backupInventory({ directory, source });
309
+ return {
310
+ policy: policy(),
311
+ last: last(),
312
+ running,
313
+ serverTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
314
+ totalBytes: entries.reduce((sum, item) => sum + item.bytes, 0),
315
+ plan: cleanupPlan(entries, policy(), now()),
316
+ entries,
317
+ };
318
+ };
319
+ const launch = async (mode, selection = []) => {
320
+ if (running || backup?.running)
321
+ throw Error("备份或清理正在进行,请稍后再试");
322
+ running = true;
323
+ try {
324
+ return await run({
325
+ directory,
326
+ source,
327
+ policy: policy(),
328
+ mode,
329
+ selection,
330
+ now: now(),
331
+ });
332
+ } finally {
333
+ running = false;
334
+ }
335
+ };
336
+ return {
337
+ info,
338
+ get running() {
339
+ return running;
340
+ },
341
+ save(value) {
342
+ const next = cleanupPolicy(value);
343
+ store.save({ backupCleanup: next });
344
+ return info();
345
+ },
346
+ async manual(selection) {
347
+ if (
348
+ !Array.isArray(selection) ||
349
+ !selection.length ||
350
+ selection.length > 100
351
+ )
352
+ throw Error("请先选择要清理的备份,单次最多 100 份");
353
+ return launch("manual", selection);
354
+ },
355
+ tick() {
356
+ const current = policy();
357
+ const clock = localDay(now());
358
+ if (
359
+ !current.enabled ||
360
+ running ||
361
+ backup?.running ||
362
+ last()?.day === clock.day ||
363
+ clock.time < current.time
364
+ )
365
+ return false;
366
+ const preview = cleanupPlan(
367
+ backupInventory({ directory, source }),
368
+ current,
369
+ now(),
370
+ );
371
+ if (!preview.items.length) {
372
+ store.save({
373
+ backupCleanupLast: {
374
+ day: clock.day,
375
+ at: now(),
376
+ removed: 0,
377
+ bytes: 0,
378
+ },
379
+ });
380
+ return false;
381
+ }
382
+ launch("scheduled")
383
+ .then((result) => {
384
+ store.save({
385
+ backupCleanupLast: {
386
+ day: clock.day,
387
+ at: now(),
388
+ removed: result.removed.length,
389
+ bytes: result.bytes,
390
+ ...(result.failures?.length
391
+ ? {
392
+ error: "部分备份未清理,请刷新列表后重试",
393
+ failures: result.failures,
394
+ }
395
+ : {}),
396
+ },
397
+ });
398
+ log.log(`定时备份整理完成:${result.removed.length} 份`);
399
+ })
400
+ .catch((error) => {
401
+ store.save({
402
+ backupCleanupLast: {
403
+ day: clock.day,
404
+ at: now(),
405
+ removed: 0,
406
+ bytes: 0,
407
+ error: error.message,
408
+ },
409
+ });
410
+ log.error(`定时备份整理失败:${error.message}`);
411
+ });
412
+ return true;
413
+ },
414
+ };
415
+ }