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.
- package/.github/SECURITY.md +4 -4
- package/README.en.md +252 -250
- package/README.md +252 -250
- package/bin/luckytri.js +5 -1
- package/docs/en/CHANGELOG.md +8 -0
- package/docs/en/connect.md +18 -18
- package/docs/en/guide.md +202 -216
- package/docs/en/npm-release.md +25 -25
- package/docs/zh/CHANGELOG.md +8 -0
- package/docs/zh/connect.md +17 -17
- package/docs/zh/guide.md +202 -216
- package/docs/zh/npm-release.md +33 -33
- package/package.json +110 -108
- package/plugins/sdk/index.d.ts +155 -155
- package/plugins/sdk/testing.js +58 -58
- package/plugins/weather/index.js +82 -82
- package/plugins/weather/luckytri-plugin.json +29 -29
- package/public/app/assets/ChatsPage-BhlJHseG.js +3 -0
- package/public/app/assets/Empty-DP6WMGHi.js +2 -0
- package/public/app/assets/Field-CtbdqaiB.js +1 -0
- package/public/app/assets/HeartPage-32BeTPn2.js +1 -0
- package/public/app/assets/LifePage-CxSm0XKI.js +1 -0
- package/public/app/assets/MemoryPage-BeKBcPCY.js +1 -0
- package/public/app/assets/NaturePage-NEPYl2HS.js +3 -0
- package/public/app/assets/PeoplePage-DxtpekwM.js +1 -0
- package/public/app/assets/PluginsPage-CVVQwEBI.css +1 -0
- package/public/app/assets/PluginsPage-DksEZbUU.js +2 -0
- package/public/app/assets/{SystemPage-CAMPoYqM.css → SystemPage-BkRu0Rvx.css} +1 -1
- package/public/app/assets/SystemPage-BvzJlHuY.js +1 -0
- package/public/app/assets/TimePage-7-awH5XL.js +3 -0
- package/public/app/assets/index-4rItIkD-.js +4 -0
- package/public/app/assets/{index-YDz1ree1.css → index-DY4e15Kh.css} +1 -1
- package/public/app/assets/{knowledge-DG0e0uKw.js → knowledge-C6_HvoyB.js} +1 -1
- package/public/app/assets/media-SMfTT-yc.js +1 -0
- package/public/app/index.html +3 -3
- package/public/guide.en.html +72 -99
- package/public/guide.html +44 -86
- package/public/tutorial.en.md +25 -39
- package/public/tutorial.md +25 -39
- package/scripts/backup.js +311 -311
- package/scripts/browser.js +41 -41
- package/scripts/import-data.js +128 -0
- package/scripts/launch.js +101 -94
- package/scripts/plugin.js +150 -150
- package/scripts/reset-password.js +22 -0
- package/scripts/runtime.js +25 -25
- package/scripts/service.js +56 -47
- package/scripts/setup.js +50 -50
- package/scripts/start-server.js +41 -41
- package/scripts/stop.js +36 -36
- package/server/app.js +112 -96
- package/server/auth.js +217 -0
- package/server/channels/contract.js +57 -57
- package/server/channels/hub.js +165 -165
- package/server/channels/index.js +28 -25
- package/server/channels/onebot/index.js +335 -336
- package/server/channels/onebot/token.js +22 -3
- package/server/core/capabilities.js +208 -208
- package/server/core/events.js +64 -60
- package/server/core/image-preprocessor.js +208 -208
- package/server/core/model-manager.js +1213 -1213
- package/server/core/net-guard.js +49 -49
- package/server/core/orchestrator.js +569 -569
- package/server/core/perceivers.js +91 -91
- package/server/core/persona-manager.js +243 -243
- package/server/core/repository.js +293 -293
- package/server/core/response-validator.js +508 -508
- package/server/core/turn-processor.js +630 -630
- package/server/core/turn.js +236 -236
- package/server/core/vision-manager.js +633 -633
- package/server/i18n/errors.js +548 -497
- package/server/i18n/labels.js +649 -653
- package/server/index.js +151 -102
- package/server/knowledge/manager.js +386 -386
- package/server/knowledge/schema.js +12 -8
- package/server/local-access.js +32 -0
- package/server/mind/anticipations.js +507 -507
- package/server/mind/api.js +703 -703
- package/server/mind/budget.js +152 -152
- package/server/mind/context-selection.js +61 -61
- package/server/mind/day-planner.js +324 -324
- package/server/mind/evidence.js +148 -148
- package/server/mind/index.js +445 -445
- package/server/mind/life/diary.js +786 -786
- package/server/mind/life/index.js +1049 -1049
- package/server/mind/observations.js +84 -84
- package/server/mind/own-day.js +324 -324
- package/server/mind/own-voice.js +318 -318
- package/server/mind/senses.js +53 -53
- package/server/mind/time/activity-clock.js +196 -196
- package/server/mind/time/availability.js +69 -69
- package/server/mind/time/commitment-review.js +198 -198
- package/server/mind/time/executor.js +512 -512
- package/server/mind/time/experiences.js +128 -128
- package/server/mind/time/games.js +562 -562
- package/server/mind/time/index.js +739 -739
- package/server/mind/time/intent.js +166 -166
- package/server/mind/time/kinds.js +171 -171
- package/server/mind/time/plugin-activity.js +207 -207
- package/server/mind/time/tasks.js +587 -587
- package/server/mind/util.js +110 -110
- package/server/mind/view.js +273 -273
- package/server/paths.js +102 -102
- package/server/plugins/api.js +204 -204
- package/server/plugins/host.js +967 -967
- package/server/plugins/install.js +295 -295
- package/server/plugins/manifest.js +201 -201
- package/server/plugins/permissions.js +42 -42
- package/server/plugins/runner/guard.mjs +36 -36
- package/server/plugins/runner/worker.mjs +250 -250
- package/server/plugins/schema.js +91 -91
- package/server/plugins/zip.js +112 -112
- package/server/startup-lock.js +118 -118
- package/server/storage/archive.js +242 -241
- package/server/storage/backup-cleanup-worker.js +8 -8
- package/server/storage/backup-cleanup.js +415 -414
- package/server/storage/backup-lock.js +76 -76
- package/server/storage/backup-scheduler.js +131 -131
- package/server/storage/data-package.js +298 -0
- package/server/storage/data-transfer-api.js +50 -0
- package/server/storage/data-transfer-worker.js +18 -0
- package/server/storage/data-transfer.js +415 -0
- package/server/storage/store.js +202 -199
- package/server/studio/readiness.js +181 -190
- package/server/studio/settings.js +236 -236
- package/public/app/assets/ChatsPage-C-FcRapR.js +0 -3
- package/public/app/assets/Empty-CpP5kQHH.js +0 -2
- package/public/app/assets/Field-D4gY-INU.js +0 -1
- package/public/app/assets/HeartPage-CPy_thQd.js +0 -1
- package/public/app/assets/LifePage-CxWvwmSf.js +0 -1
- package/public/app/assets/MemoryPage-DC4rhQ68.js +0 -1
- package/public/app/assets/NaturePage-IvYw04Zq.js +0 -3
- package/public/app/assets/PeoplePage-Cqefm9-q.js +0 -1
- package/public/app/assets/PluginsPage-DKSaKvXb.js +0 -2
- package/public/app/assets/PluginsPage-HyBe5D8w.css +0 -1
- package/public/app/assets/SystemPage-CJmtJkrK.js +0 -1
- package/public/app/assets/TimePage-Cw44inM4.js +0 -3
- package/public/app/assets/index-BheRW-1t.js +0 -4
- package/public/app/assets/media-BzPDBm7e.js +0 -1
|
@@ -1,241 +1,242 @@
|
|
|
1
|
-
import { DatabaseSync } from "node:sqlite";
|
|
2
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
-
import {
|
|
4
|
-
openSync,
|
|
5
|
-
readSync,
|
|
6
|
-
closeSync,
|
|
7
|
-
existsSync,
|
|
8
|
-
readFileSync,
|
|
9
|
-
writeFileSync,
|
|
10
|
-
renameSync,
|
|
11
|
-
rmSync,
|
|
12
|
-
mkdirSync,
|
|
13
|
-
linkSync,
|
|
14
|
-
} from "node:fs";
|
|
15
|
-
import { dirname, basename, resolve, join } from "node:path";
|
|
16
|
-
import { VERSION } from "../version.js";
|
|
17
|
-
import { withBackupLock } from "./backup-lock.js";
|
|
18
|
-
|
|
19
|
-
export const DATABASE_VERSION = 4;
|
|
20
|
-
const appVersion = VERSION;
|
|
21
|
-
const IDENTITY = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i;
|
|
22
|
-
|
|
23
|
-
export function archiveIdentity(file) {
|
|
24
|
-
const db = new DatabaseSync(file, { readOnly: true });
|
|
25
|
-
try {
|
|
26
|
-
const row = db.prepare("SELECT value FROM settings WHERE id=1").get();
|
|
27
|
-
const value = row && JSON.parse(row.value).backupIdentity;
|
|
28
|
-
return typeof value === "string" && IDENTITY.test(value) ? value : null;
|
|
29
|
-
} finally {
|
|
30
|
-
db.close();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function ensureArchiveIdentity(file) {
|
|
35
|
-
const db = new DatabaseSync(file);
|
|
36
|
-
try {
|
|
37
|
-
db.exec("PRAGMA busy_timeout=5000; BEGIN IMMEDIATE");
|
|
38
|
-
const row = db.prepare("SELECT value FROM settings WHERE id=1").get();
|
|
39
|
-
if (!row) throw Error("这不是 LuckyTri 数据库");
|
|
40
|
-
const settings = JSON.parse(row.value);
|
|
41
|
-
let value = settings.backupIdentity;
|
|
42
|
-
if (typeof value !== "string" || !IDENTITY.test(value)) {
|
|
43
|
-
value = randomUUID();
|
|
44
|
-
db.prepare("UPDATE settings SET value=? WHERE id=1").run(
|
|
45
|
-
JSON.stringify({ ...settings, backupIdentity: value }),
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
db.exec("COMMIT");
|
|
49
|
-
return value;
|
|
50
|
-
} catch (error) {
|
|
51
|
-
if (db.isTransaction) db.exec("ROLLBACK");
|
|
52
|
-
throw error;
|
|
53
|
-
} finally {
|
|
54
|
-
db.close();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function fileHash(file) {
|
|
59
|
-
const fd = openSync(file, "r"),
|
|
60
|
-
hash = createHash("sha256"),
|
|
61
|
-
buffer = Buffer.alloc(1024 * 1024);
|
|
62
|
-
try {
|
|
63
|
-
let bytes;
|
|
64
|
-
while ((bytes = readSync(fd, buffer, 0, buffer.length, null)))
|
|
65
|
-
hash.update(buffer.subarray(0, bytes));
|
|
66
|
-
return hash.digest("hex");
|
|
67
|
-
} finally {
|
|
68
|
-
closeSync(fd);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function inspectDatabase(file) {
|
|
73
|
-
const db = new DatabaseSync(file, { readOnly: true });
|
|
74
|
-
try {
|
|
75
|
-
const schemaVersion = db.prepare("PRAGMA user_version").get().user_version;
|
|
76
|
-
if (schemaVersion > DATABASE_VERSION)
|
|
77
|
-
throw Error("数据库来自更新版本,请先升级 LuckyTri;未修改数据");
|
|
78
|
-
const check = db.prepare("PRAGMA integrity_check").all();
|
|
79
|
-
if (check.length !== 1 || check[0].integrity_check !== "ok")
|
|
80
|
-
throw Error("数据库完整性检查失败");
|
|
81
|
-
if (db.prepare("PRAGMA foreign_key_check").all().length)
|
|
82
|
-
throw Error("数据库引用检查失败");
|
|
83
|
-
const schema = db
|
|
84
|
-
.prepare(
|
|
85
|
-
"SELECT type,name,sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%' ORDER BY type,name",
|
|
86
|
-
)
|
|
87
|
-
.all();
|
|
88
|
-
if (!schema.some((row) => row.type === "table" && row.name === "settings"))
|
|
89
|
-
throw Error("这不是 LuckyTri 数据库");
|
|
90
|
-
const tables = {};
|
|
91
|
-
for (const row of schema.filter(
|
|
92
|
-
(row) =>
|
|
93
|
-
row.type === "table" &&
|
|
94
|
-
/^(mind_|core_events$|core_memories$|core_chunks$|settings$|plugin_installs$)/.test(
|
|
95
|
-
row.name,
|
|
96
|
-
),
|
|
97
|
-
))
|
|
98
|
-
tables[row.name] = Number(
|
|
99
|
-
db
|
|
100
|
-
.prepare(`SELECT COUNT(*) n FROM "${row.name.replace(/"/g, '""')}"`)
|
|
101
|
-
.get().n,
|
|
102
|
-
);
|
|
103
|
-
return {
|
|
104
|
-
schemaVersion,
|
|
105
|
-
schemaHash: createHash("sha256")
|
|
106
|
-
.update(JSON.stringify(schema))
|
|
107
|
-
.digest("hex"),
|
|
108
|
-
tables,
|
|
109
|
-
};
|
|
110
|
-
} finally {
|
|
111
|
-
db.close();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function writeArchiveManifest(
|
|
116
|
-
file,
|
|
117
|
-
{ kind = "full", filename = basename(file) } = {},
|
|
118
|
-
) {
|
|
119
|
-
const inspected = inspectDatabase(file);
|
|
120
|
-
const manifest = {
|
|
121
|
-
format: 1,
|
|
122
|
-
app: "luckytri",
|
|
123
|
-
appVersion,
|
|
124
|
-
created: Date.now(),
|
|
125
|
-
kind,
|
|
126
|
-
databaseId: archiveIdentity(file),
|
|
127
|
-
file: filename,
|
|
128
|
-
sha256: fileHash(file),
|
|
129
|
-
...inspected,
|
|
130
|
-
};
|
|
131
|
-
const temporary = `${file}.json.${randomUUID()}.partial`;
|
|
132
|
-
try {
|
|
133
|
-
writeFileSync(temporary, JSON.stringify(manifest, null, 2), {
|
|
134
|
-
flush: true,
|
|
135
|
-
});
|
|
136
|
-
renameSync(temporary, `${file}.json`);
|
|
137
|
-
} catch (error) {
|
|
138
|
-
rmSync(temporary, { force: true });
|
|
139
|
-
throw error;
|
|
140
|
-
}
|
|
141
|
-
return manifest;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
export function verifyArchive(file) {
|
|
145
|
-
const inspected = inspectDatabase(file);
|
|
146
|
-
const sidecar = `${file}.json`;
|
|
147
|
-
if (!existsSync(sidecar)) return { ...inspected, manifest: false }; // Readable legacy backups remain usable.
|
|
148
|
-
const manifest = JSON.parse(readFileSync(sidecar, "utf8"));
|
|
149
|
-
if (
|
|
150
|
-
manifest.format !== 1 ||
|
|
151
|
-
manifest.app !== "luckytri" ||
|
|
152
|
-
manifest.sha256 !== fileHash(file) ||
|
|
153
|
-
manifest.schemaHash !== inspected.schemaHash ||
|
|
154
|
-
(manifest.databaseId && manifest.databaseId !== archiveIdentity(file)) ||
|
|
155
|
-
JSON.stringify(manifest.tables) !== JSON.stringify(inspected.tables)
|
|
156
|
-
)
|
|
157
|
-
throw Error("备份校验清单不匹配");
|
|
158
|
-
return {
|
|
159
|
-
...inspected,
|
|
160
|
-
manifest: true,
|
|
161
|
-
kind: manifest.kind,
|
|
162
|
-
databaseId: manifest.databaseId || null,
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export function copyDatabaseFile(source, target) {
|
|
167
|
-
const db = new DatabaseSync(source, { readOnly: true });
|
|
168
|
-
try {
|
|
169
|
-
db.prepare("VACUUM INTO ?").run(target);
|
|
170
|
-
} finally {
|
|
171
|
-
db.close();
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export function restoreToNewFile(source, destination) {
|
|
176
|
-
const target = resolve(destination);
|
|
177
|
-
if ([target, `${target}-wal`, `${target}-shm`].some(existsSync))
|
|
178
|
-
throw Error("恢复目标或伴随文件已存在,请选一个新文件;未覆盖任何数据");
|
|
179
|
-
verifyArchive(source);
|
|
180
|
-
mkdirSync(dirname(target), { recursive: true });
|
|
181
|
-
const partial = `${target}.${randomUUID()}.partial`;
|
|
182
|
-
try {
|
|
183
|
-
copyDatabaseFile(source, partial);
|
|
184
|
-
inspectDatabase(partial);
|
|
185
|
-
// Another process may have created the destination during the copy.
|
|
186
|
-
if (existsSync(target)) throw Error("恢复目标已被其他进程创建");
|
|
187
|
-
// Publish the verified file atomically and exclusively in the same directory.
|
|
188
|
-
linkSync(partial, target);
|
|
189
|
-
rmSync(partial);
|
|
190
|
-
return target;
|
|
191
|
-
} catch (error) {
|
|
192
|
-
rmSync(partial, { force: true });
|
|
193
|
-
throw error;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export function prepareDatabaseMigration(file) {
|
|
198
|
-
if (file === ":memory:" || !existsSync(file)) return null;
|
|
199
|
-
const db = new DatabaseSync(file, { readOnly: true });
|
|
200
|
-
let version, hasData;
|
|
201
|
-
try {
|
|
202
|
-
version = db.prepare("PRAGMA user_version").get().user_version;
|
|
203
|
-
if (version > DATABASE_VERSION)
|
|
204
|
-
throw Error("数据库版本较新,请升级 LuckyTri;未修改数据");
|
|
205
|
-
hasData = !!db
|
|
206
|
-
.prepare(
|
|
207
|
-
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='settings'",
|
|
208
|
-
)
|
|
209
|
-
.get();
|
|
210
|
-
} finally {
|
|
211
|
-
db.close();
|
|
212
|
-
}
|
|
213
|
-
if (version === DATABASE_VERSION || !hasData) return null;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
renameSync(
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
rmSync(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
}
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import {
|
|
4
|
+
openSync,
|
|
5
|
+
readSync,
|
|
6
|
+
closeSync,
|
|
7
|
+
existsSync,
|
|
8
|
+
readFileSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
renameSync,
|
|
11
|
+
rmSync,
|
|
12
|
+
mkdirSync,
|
|
13
|
+
linkSync,
|
|
14
|
+
} from "node:fs";
|
|
15
|
+
import { dirname, basename, resolve, join } from "node:path";
|
|
16
|
+
import { VERSION } from "../version.js";
|
|
17
|
+
import { withBackupLock } from "./backup-lock.js";
|
|
18
|
+
|
|
19
|
+
export const DATABASE_VERSION = 4;
|
|
20
|
+
const appVersion = VERSION;
|
|
21
|
+
const IDENTITY = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i;
|
|
22
|
+
|
|
23
|
+
export function archiveIdentity(file) {
|
|
24
|
+
const db = new DatabaseSync(file, { readOnly: true });
|
|
25
|
+
try {
|
|
26
|
+
const row = db.prepare("SELECT value FROM settings WHERE id=1").get();
|
|
27
|
+
const value = row && JSON.parse(row.value).backupIdentity;
|
|
28
|
+
return typeof value === "string" && IDENTITY.test(value) ? value : null;
|
|
29
|
+
} finally {
|
|
30
|
+
db.close();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function ensureArchiveIdentity(file) {
|
|
35
|
+
const db = new DatabaseSync(file);
|
|
36
|
+
try {
|
|
37
|
+
db.exec("PRAGMA busy_timeout=5000; BEGIN IMMEDIATE");
|
|
38
|
+
const row = db.prepare("SELECT value FROM settings WHERE id=1").get();
|
|
39
|
+
if (!row) throw Error("这不是 LuckyTri 数据库");
|
|
40
|
+
const settings = JSON.parse(row.value);
|
|
41
|
+
let value = settings.backupIdentity;
|
|
42
|
+
if (typeof value !== "string" || !IDENTITY.test(value)) {
|
|
43
|
+
value = randomUUID();
|
|
44
|
+
db.prepare("UPDATE settings SET value=? WHERE id=1").run(
|
|
45
|
+
JSON.stringify({ ...settings, backupIdentity: value }),
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
db.exec("COMMIT");
|
|
49
|
+
return value;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (db.isTransaction) db.exec("ROLLBACK");
|
|
52
|
+
throw error;
|
|
53
|
+
} finally {
|
|
54
|
+
db.close();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function fileHash(file) {
|
|
59
|
+
const fd = openSync(file, "r"),
|
|
60
|
+
hash = createHash("sha256"),
|
|
61
|
+
buffer = Buffer.alloc(1024 * 1024);
|
|
62
|
+
try {
|
|
63
|
+
let bytes;
|
|
64
|
+
while ((bytes = readSync(fd, buffer, 0, buffer.length, null)))
|
|
65
|
+
hash.update(buffer.subarray(0, bytes));
|
|
66
|
+
return hash.digest("hex");
|
|
67
|
+
} finally {
|
|
68
|
+
closeSync(fd);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function inspectDatabase(file) {
|
|
73
|
+
const db = new DatabaseSync(file, { readOnly: true });
|
|
74
|
+
try {
|
|
75
|
+
const schemaVersion = db.prepare("PRAGMA user_version").get().user_version;
|
|
76
|
+
if (schemaVersion > DATABASE_VERSION)
|
|
77
|
+
throw Error("数据库来自更新版本,请先升级 LuckyTri;未修改数据");
|
|
78
|
+
const check = db.prepare("PRAGMA integrity_check").all();
|
|
79
|
+
if (check.length !== 1 || check[0].integrity_check !== "ok")
|
|
80
|
+
throw Error("数据库完整性检查失败");
|
|
81
|
+
if (db.prepare("PRAGMA foreign_key_check").all().length)
|
|
82
|
+
throw Error("数据库引用检查失败");
|
|
83
|
+
const schema = db
|
|
84
|
+
.prepare(
|
|
85
|
+
"SELECT type,name,sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%' ORDER BY type,name",
|
|
86
|
+
)
|
|
87
|
+
.all();
|
|
88
|
+
if (!schema.some((row) => row.type === "table" && row.name === "settings"))
|
|
89
|
+
throw Error("这不是 LuckyTri 数据库");
|
|
90
|
+
const tables = {};
|
|
91
|
+
for (const row of schema.filter(
|
|
92
|
+
(row) =>
|
|
93
|
+
row.type === "table" &&
|
|
94
|
+
/^(mind_|core_events$|core_memories$|core_chunks$|settings$|plugin_installs$)/.test(
|
|
95
|
+
row.name,
|
|
96
|
+
),
|
|
97
|
+
))
|
|
98
|
+
tables[row.name] = Number(
|
|
99
|
+
db
|
|
100
|
+
.prepare(`SELECT COUNT(*) n FROM "${row.name.replace(/"/g, '""')}"`)
|
|
101
|
+
.get().n,
|
|
102
|
+
);
|
|
103
|
+
return {
|
|
104
|
+
schemaVersion,
|
|
105
|
+
schemaHash: createHash("sha256")
|
|
106
|
+
.update(JSON.stringify(schema))
|
|
107
|
+
.digest("hex"),
|
|
108
|
+
tables,
|
|
109
|
+
};
|
|
110
|
+
} finally {
|
|
111
|
+
db.close();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function writeArchiveManifest(
|
|
116
|
+
file,
|
|
117
|
+
{ kind = "full", filename = basename(file) } = {},
|
|
118
|
+
) {
|
|
119
|
+
const inspected = inspectDatabase(file);
|
|
120
|
+
const manifest = {
|
|
121
|
+
format: 1,
|
|
122
|
+
app: "luckytri",
|
|
123
|
+
appVersion,
|
|
124
|
+
created: Date.now(),
|
|
125
|
+
kind,
|
|
126
|
+
databaseId: archiveIdentity(file),
|
|
127
|
+
file: filename,
|
|
128
|
+
sha256: fileHash(file),
|
|
129
|
+
...inspected,
|
|
130
|
+
};
|
|
131
|
+
const temporary = `${file}.json.${randomUUID()}.partial`;
|
|
132
|
+
try {
|
|
133
|
+
writeFileSync(temporary, JSON.stringify(manifest, null, 2), {
|
|
134
|
+
flush: true,
|
|
135
|
+
});
|
|
136
|
+
renameSync(temporary, `${file}.json`);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
rmSync(temporary, { force: true });
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
return manifest;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function verifyArchive(file) {
|
|
145
|
+
const inspected = inspectDatabase(file);
|
|
146
|
+
const sidecar = `${file}.json`;
|
|
147
|
+
if (!existsSync(sidecar)) return { ...inspected, manifest: false }; // Readable legacy backups remain usable.
|
|
148
|
+
const manifest = JSON.parse(readFileSync(sidecar, "utf8"));
|
|
149
|
+
if (
|
|
150
|
+
manifest.format !== 1 ||
|
|
151
|
+
manifest.app !== "luckytri" ||
|
|
152
|
+
manifest.sha256 !== fileHash(file) ||
|
|
153
|
+
manifest.schemaHash !== inspected.schemaHash ||
|
|
154
|
+
(manifest.databaseId && manifest.databaseId !== archiveIdentity(file)) ||
|
|
155
|
+
JSON.stringify(manifest.tables) !== JSON.stringify(inspected.tables)
|
|
156
|
+
)
|
|
157
|
+
throw Error("备份校验清单不匹配");
|
|
158
|
+
return {
|
|
159
|
+
...inspected,
|
|
160
|
+
manifest: true,
|
|
161
|
+
kind: manifest.kind,
|
|
162
|
+
databaseId: manifest.databaseId || null,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function copyDatabaseFile(source, target) {
|
|
167
|
+
const db = new DatabaseSync(source, { readOnly: true });
|
|
168
|
+
try {
|
|
169
|
+
db.prepare("VACUUM INTO ?").run(target);
|
|
170
|
+
} finally {
|
|
171
|
+
db.close();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function restoreToNewFile(source, destination) {
|
|
176
|
+
const target = resolve(destination);
|
|
177
|
+
if ([target, `${target}-wal`, `${target}-shm`].some(existsSync))
|
|
178
|
+
throw Error("恢复目标或伴随文件已存在,请选一个新文件;未覆盖任何数据");
|
|
179
|
+
verifyArchive(source);
|
|
180
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
181
|
+
const partial = `${target}.${randomUUID()}.partial`;
|
|
182
|
+
try {
|
|
183
|
+
copyDatabaseFile(source, partial);
|
|
184
|
+
inspectDatabase(partial);
|
|
185
|
+
// Another process may have created the destination during the copy.
|
|
186
|
+
if (existsSync(target)) throw Error("恢复目标已被其他进程创建");
|
|
187
|
+
// Publish the verified file atomically and exclusively in the same directory.
|
|
188
|
+
linkSync(partial, target);
|
|
189
|
+
rmSync(partial);
|
|
190
|
+
return target;
|
|
191
|
+
} catch (error) {
|
|
192
|
+
rmSync(partial, { force: true });
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function prepareDatabaseMigration(file, { quiet = false } = {}) {
|
|
198
|
+
if (file === ":memory:" || !existsSync(file)) return null;
|
|
199
|
+
const db = new DatabaseSync(file, { readOnly: true });
|
|
200
|
+
let version, hasData;
|
|
201
|
+
try {
|
|
202
|
+
version = db.prepare("PRAGMA user_version").get().user_version;
|
|
203
|
+
if (version > DATABASE_VERSION)
|
|
204
|
+
throw Error("数据库版本较新,请升级 LuckyTri;未修改数据");
|
|
205
|
+
hasData = !!db
|
|
206
|
+
.prepare(
|
|
207
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='settings'",
|
|
208
|
+
)
|
|
209
|
+
.get();
|
|
210
|
+
} finally {
|
|
211
|
+
db.close();
|
|
212
|
+
}
|
|
213
|
+
if (version === DATABASE_VERSION || !hasData) return null;
|
|
214
|
+
if (!quiet)
|
|
215
|
+
console.log(
|
|
216
|
+
`正在把数据库从 v${version} 升级到 v${DATABASE_VERSION}。会先复制整份数据,记录越多越久,请不要关闭窗口。`,
|
|
217
|
+
);
|
|
218
|
+
const directory = join(dirname(resolve(file)), "backups");
|
|
219
|
+
mkdirSync(directory, { recursive: true });
|
|
220
|
+
const target = join(
|
|
221
|
+
directory,
|
|
222
|
+
`luckytri-migration-v${version}-to-v${DATABASE_VERSION}-${Date.now()}-${randomUUID().slice(0, 8)}.db`,
|
|
223
|
+
);
|
|
224
|
+
return withBackupLock(directory, () => {
|
|
225
|
+
const partial = `${target}.partial`;
|
|
226
|
+
try {
|
|
227
|
+
ensureArchiveIdentity(file);
|
|
228
|
+
copyDatabaseFile(file, partial);
|
|
229
|
+
writeArchiveManifest(partial, {
|
|
230
|
+
kind: "migration",
|
|
231
|
+
filename: basename(target),
|
|
232
|
+
});
|
|
233
|
+
renameSync(partial, target);
|
|
234
|
+
renameSync(`${partial}.json`, `${target}.json`);
|
|
235
|
+
return target;
|
|
236
|
+
} catch (error) {
|
|
237
|
+
rmSync(partial, { force: true });
|
|
238
|
+
rmSync(`${partial}.json`, { force: true });
|
|
239
|
+
throw error;
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { parentPort, workerData } from "node:worker_threads";
|
|
2
|
-
import { executeCleanup } from "./backup-cleanup.js";
|
|
3
|
-
|
|
4
|
-
try {
|
|
5
|
-
parentPort.postMessage({ result: executeCleanup(workerData) });
|
|
6
|
-
} catch (error) {
|
|
7
|
-
parentPort.postMessage({ error: error.message });
|
|
8
|
-
}
|
|
1
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
2
|
+
import { executeCleanup } from "./backup-cleanup.js";
|
|
3
|
+
|
|
4
|
+
try {
|
|
5
|
+
parentPort.postMessage({ result: executeCleanup(workerData) });
|
|
6
|
+
} catch (error) {
|
|
7
|
+
parentPort.postMessage({ error: error.message });
|
|
8
|
+
}
|