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
package/server/paths.js
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import { existsSync, realpathSync } from "node:fs";
|
|
2
|
-
import { homedir } from "node:os";
|
|
3
|
-
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
|
|
6
|
-
// Code and static assets belong to the package; all mutable state belongs to
|
|
7
|
-
// an instance. Neither is ever resolved against the caller's working directory.
|
|
8
|
-
export const PACKAGE_ROOT = fileURLToPath(new URL("../", import.meta.url));
|
|
9
|
-
export const SOURCE_CHECKOUT = existsSync(join(PACKAGE_ROOT, ".git"));
|
|
10
|
-
|
|
11
|
-
export function defaultHome({
|
|
12
|
-
platform = process.platform,
|
|
13
|
-
env = process.env,
|
|
14
|
-
userHome = homedir(),
|
|
15
|
-
} = {}) {
|
|
16
|
-
if (platform === "win32")
|
|
17
|
-
return join(
|
|
18
|
-
env.LOCALAPPDATA && isAbsolute(env.LOCALAPPDATA)
|
|
19
|
-
? env.LOCALAPPDATA
|
|
20
|
-
: join(userHome, "AppData", "Local"),
|
|
21
|
-
"LuckyTri",
|
|
22
|
-
);
|
|
23
|
-
if (platform === "darwin")
|
|
24
|
-
return join(userHome, "Library", "Application Support", "LuckyTri");
|
|
25
|
-
return join(
|
|
26
|
-
env.XDG_DATA_HOME && isAbsolute(env.XDG_DATA_HOME)
|
|
27
|
-
? env.XDG_DATA_HOME
|
|
28
|
-
: join(userHome, ".local", "share"),
|
|
29
|
-
"luckytri",
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function inside(file, directory) {
|
|
34
|
-
const part = relative(directory, file);
|
|
35
|
-
return (
|
|
36
|
-
!part ||
|
|
37
|
-
(part !== ".." && !part.startsWith(`..${sep}`) && !isAbsolute(part))
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Resolve through existing ancestors as well: a symlink must not route user
|
|
42
|
-
// data back into node_modules, where an npm update would delete it.
|
|
43
|
-
function canonical(file) {
|
|
44
|
-
if (existsSync(file)) return realpathSync(file);
|
|
45
|
-
const parent = dirname(file);
|
|
46
|
-
return parent === file
|
|
47
|
-
? file
|
|
48
|
-
: join(canonical(parent), relative(parent, file));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function runtimePaths(env = process.env) {
|
|
52
|
-
if (env.LUCKYTRI_HOME && !isAbsolute(env.LUCKYTRI_HOME))
|
|
53
|
-
throw Error(
|
|
54
|
-
"LUCKYTRI_HOME / --home 必须是绝对路径 (must be an absolute path)",
|
|
55
|
-
);
|
|
56
|
-
const home = canonical(
|
|
57
|
-
resolve(
|
|
58
|
-
env.LUCKYTRI_HOME ||
|
|
59
|
-
(SOURCE_CHECKOUT ? PACKAGE_ROOT : defaultHome({ env })),
|
|
60
|
-
),
|
|
61
|
-
);
|
|
62
|
-
const data = canonical(join(home, "data"));
|
|
63
|
-
const config = canonical(join(home, ".env"));
|
|
64
|
-
const log = canonical(join(data, "launcher.log"));
|
|
65
|
-
const knowledge = canonical(join(data, "knowledge"));
|
|
66
|
-
const plugins = canonical(join(data, "plugins"));
|
|
67
|
-
const database =
|
|
68
|
-
env.DB_PATH === ":memory:"
|
|
69
|
-
? ":memory:"
|
|
70
|
-
: canonical(resolve(home, env.DB_PATH || join("data", "friend.db")));
|
|
71
|
-
const backups = canonical(
|
|
72
|
-
resolve(home, env.BACKUP_DIR || join(dirname(database), "backups")),
|
|
73
|
-
);
|
|
74
|
-
if (!SOURCE_CHECKOUT) {
|
|
75
|
-
for (const file of [
|
|
76
|
-
home,
|
|
77
|
-
config,
|
|
78
|
-
data,
|
|
79
|
-
database,
|
|
80
|
-
backups,
|
|
81
|
-
log,
|
|
82
|
-
knowledge,
|
|
83
|
-
plugins,
|
|
84
|
-
])
|
|
85
|
-
if (file !== ":memory:" && inside(file, canonical(PACKAGE_ROOT)))
|
|
86
|
-
throw Error(
|
|
87
|
-
"用户数据不能存放在 npm 安装目录内 (user data cannot live inside the npm package)",
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
package: PACKAGE_ROOT,
|
|
92
|
-
home,
|
|
93
|
-
config,
|
|
94
|
-
data,
|
|
95
|
-
database,
|
|
96
|
-
backups,
|
|
97
|
-
log,
|
|
98
|
-
knowledge,
|
|
99
|
-
plugins,
|
|
100
|
-
public: join(PACKAGE_ROOT, "public"),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
1
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
// Code and static assets belong to the package; all mutable state belongs to
|
|
7
|
+
// an instance. Neither is ever resolved against the caller's working directory.
|
|
8
|
+
export const PACKAGE_ROOT = fileURLToPath(new URL("../", import.meta.url));
|
|
9
|
+
export const SOURCE_CHECKOUT = existsSync(join(PACKAGE_ROOT, ".git"));
|
|
10
|
+
|
|
11
|
+
export function defaultHome({
|
|
12
|
+
platform = process.platform,
|
|
13
|
+
env = process.env,
|
|
14
|
+
userHome = homedir(),
|
|
15
|
+
} = {}) {
|
|
16
|
+
if (platform === "win32")
|
|
17
|
+
return join(
|
|
18
|
+
env.LOCALAPPDATA && isAbsolute(env.LOCALAPPDATA)
|
|
19
|
+
? env.LOCALAPPDATA
|
|
20
|
+
: join(userHome, "AppData", "Local"),
|
|
21
|
+
"LuckyTri",
|
|
22
|
+
);
|
|
23
|
+
if (platform === "darwin")
|
|
24
|
+
return join(userHome, "Library", "Application Support", "LuckyTri");
|
|
25
|
+
return join(
|
|
26
|
+
env.XDG_DATA_HOME && isAbsolute(env.XDG_DATA_HOME)
|
|
27
|
+
? env.XDG_DATA_HOME
|
|
28
|
+
: join(userHome, ".local", "share"),
|
|
29
|
+
"luckytri",
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function inside(file, directory) {
|
|
34
|
+
const part = relative(directory, file);
|
|
35
|
+
return (
|
|
36
|
+
!part ||
|
|
37
|
+
(part !== ".." && !part.startsWith(`..${sep}`) && !isAbsolute(part))
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Resolve through existing ancestors as well: a symlink must not route user
|
|
42
|
+
// data back into node_modules, where an npm update would delete it.
|
|
43
|
+
function canonical(file) {
|
|
44
|
+
if (existsSync(file)) return realpathSync(file);
|
|
45
|
+
const parent = dirname(file);
|
|
46
|
+
return parent === file
|
|
47
|
+
? file
|
|
48
|
+
: join(canonical(parent), relative(parent, file));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function runtimePaths(env = process.env) {
|
|
52
|
+
if (env.LUCKYTRI_HOME && !isAbsolute(env.LUCKYTRI_HOME))
|
|
53
|
+
throw Error(
|
|
54
|
+
"LUCKYTRI_HOME / --home 必须是绝对路径 (must be an absolute path)",
|
|
55
|
+
);
|
|
56
|
+
const home = canonical(
|
|
57
|
+
resolve(
|
|
58
|
+
env.LUCKYTRI_HOME ||
|
|
59
|
+
(SOURCE_CHECKOUT ? PACKAGE_ROOT : defaultHome({ env })),
|
|
60
|
+
),
|
|
61
|
+
);
|
|
62
|
+
const data = canonical(join(home, "data"));
|
|
63
|
+
const config = canonical(join(home, ".env"));
|
|
64
|
+
const log = canonical(join(data, "launcher.log"));
|
|
65
|
+
const knowledge = canonical(join(data, "knowledge"));
|
|
66
|
+
const plugins = canonical(join(data, "plugins"));
|
|
67
|
+
const database =
|
|
68
|
+
env.DB_PATH === ":memory:"
|
|
69
|
+
? ":memory:"
|
|
70
|
+
: canonical(resolve(home, env.DB_PATH || join("data", "friend.db")));
|
|
71
|
+
const backups = canonical(
|
|
72
|
+
resolve(home, env.BACKUP_DIR || join(dirname(database), "backups")),
|
|
73
|
+
);
|
|
74
|
+
if (!SOURCE_CHECKOUT) {
|
|
75
|
+
for (const file of [
|
|
76
|
+
home,
|
|
77
|
+
config,
|
|
78
|
+
data,
|
|
79
|
+
database,
|
|
80
|
+
backups,
|
|
81
|
+
log,
|
|
82
|
+
knowledge,
|
|
83
|
+
plugins,
|
|
84
|
+
])
|
|
85
|
+
if (file !== ":memory:" && inside(file, canonical(PACKAGE_ROOT)))
|
|
86
|
+
throw Error(
|
|
87
|
+
"用户数据不能存放在 npm 安装目录内 (user data cannot live inside the npm package)",
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
package: PACKAGE_ROOT,
|
|
92
|
+
home,
|
|
93
|
+
config,
|
|
94
|
+
data,
|
|
95
|
+
database,
|
|
96
|
+
backups,
|
|
97
|
+
log,
|
|
98
|
+
knowledge,
|
|
99
|
+
plugins,
|
|
100
|
+
public: join(PACKAGE_ROOT, "public"),
|
|
101
|
+
};
|
|
102
|
+
}
|
package/server/plugins/api.js
CHANGED
|
@@ -1,204 +1,204 @@
|
|
|
1
|
-
import { createReadStream, existsSync, statSync } from "node:fs";
|
|
2
|
-
import { join, normalize } from "node:path";
|
|
3
|
-
import { PERMISSIONS } from "./permissions.js";
|
|
4
|
-
|
|
5
|
-
const SANDBOX =
|
|
6
|
-
"sandbox allow-scripts allow-forms; default-src 'none'; img-src data: https:; style-src 'unsafe-inline' *; script-src 'unsafe-inline' *; connect-src 'none'";
|
|
7
|
-
|
|
8
|
-
function safeFile(root, requestPath) {
|
|
9
|
-
const relative = normalize(requestPath).replace(/^(\.\.[/\\])+/, "");
|
|
10
|
-
const file = join(root, relative);
|
|
11
|
-
if (!file.startsWith(root) || !existsSync(file) || !statSync(file).isFile())
|
|
12
|
-
return "";
|
|
13
|
-
return file;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function mountPlugins(app, plugins) {
|
|
17
|
-
if (!plugins) return;
|
|
18
|
-
app.get("/api/plugins", (req, res) => {
|
|
19
|
-
res.json({ plugins: plugins.list(), permissions: PERMISSIONS });
|
|
20
|
-
});
|
|
21
|
-
app.get("/api/plugins/market", async (req, res) => {
|
|
22
|
-
try {
|
|
23
|
-
res.json(await plugins.install.catalog());
|
|
24
|
-
} catch (error) {
|
|
25
|
-
res.status(400).json({ error: error.message });
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
app.post("/api/plugins/market/stage", async (req, res) => {
|
|
29
|
-
try {
|
|
30
|
-
res.json(await plugins.install.stageRegistry(req.body));
|
|
31
|
-
} catch (error) {
|
|
32
|
-
res.status(400).json({ error: error.message });
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
app.post("/api/plugins/import/url", async (req, res) => {
|
|
36
|
-
try {
|
|
37
|
-
res.json(await plugins.install.stageUrl(req.body));
|
|
38
|
-
} catch (error) {
|
|
39
|
-
res.status(400).json({ error: error.message });
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
app.post("/api/plugins/import/github", async (req, res) => {
|
|
43
|
-
try {
|
|
44
|
-
res.json(await plugins.install.stageGithub(req.body.repository));
|
|
45
|
-
} catch (error) {
|
|
46
|
-
res.status(400).json({ error: error.message });
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
app.post("/api/plugins/import/dir", (req, res) => {
|
|
50
|
-
try {
|
|
51
|
-
res.json(plugins.install.stageDir(req.body.dir));
|
|
52
|
-
} catch (error) {
|
|
53
|
-
res.status(400).json({ error: error.message });
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
app.post("/api/plugins/import/commit", (req, res) => {
|
|
57
|
-
try {
|
|
58
|
-
res.json(plugins.install.commit(req.body.token));
|
|
59
|
-
} catch (error) {
|
|
60
|
-
res.status(400).json({ error: error.message });
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
app.post("/api/plugins/sources", (req, res) => {
|
|
64
|
-
try {
|
|
65
|
-
plugins.install.saveRegistry(req.body.url);
|
|
66
|
-
res.json({ ok: true });
|
|
67
|
-
} catch (error) {
|
|
68
|
-
res.status(400).json({ error: error.message });
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
app.delete("/api/plugins/sources/:source", (req, res) => {
|
|
72
|
-
plugins.install.removeRegistry(req.params.source);
|
|
73
|
-
res.json({ ok: true });
|
|
74
|
-
});
|
|
75
|
-
app.post("/api/plugins/:id/rollback", (req, res) => {
|
|
76
|
-
try {
|
|
77
|
-
res.json(plugins.install.rollback(req.params.id));
|
|
78
|
-
} catch (error) {
|
|
79
|
-
res.status(400).json({ error: error.message });
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
app.delete("/api/plugins/:id", async (req, res) => {
|
|
83
|
-
try {
|
|
84
|
-
res.json(
|
|
85
|
-
await plugins.install.remove(req.params.id, { data: !!req.body?.data }),
|
|
86
|
-
);
|
|
87
|
-
} catch (error) {
|
|
88
|
-
res.status(400).json({ error: error.message });
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
app.post("/api/plugins/:id/grant", (req, res) => {
|
|
92
|
-
try {
|
|
93
|
-
res.json(plugins.grant(req.params.id, req.body.permissions || []));
|
|
94
|
-
} catch (error) {
|
|
95
|
-
res.status(400).json({ error: error.message });
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
app.post("/api/plugins/:id/enable", async (req, res) => {
|
|
99
|
-
try {
|
|
100
|
-
res.json(await plugins.enable(req.params.id));
|
|
101
|
-
} catch (error) {
|
|
102
|
-
res.status(400).json({ error: error.message });
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
app.post("/api/plugins/:id/disable", async (req, res) => {
|
|
106
|
-
try {
|
|
107
|
-
res.json(await plugins.disable(req.params.id));
|
|
108
|
-
} catch (error) {
|
|
109
|
-
res.status(400).json({ error: error.message });
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
app.get("/api/plugins/:id/settings", (req, res) => {
|
|
113
|
-
try {
|
|
114
|
-
res.json(plugins.settings(req.params.id));
|
|
115
|
-
} catch (error) {
|
|
116
|
-
res.status(404).json({ error: error.message });
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
app.put("/api/plugins/:id/settings", (req, res) => {
|
|
120
|
-
try {
|
|
121
|
-
res.json(
|
|
122
|
-
plugins.saveSettings(req.params.id, req.body.values || req.body),
|
|
123
|
-
);
|
|
124
|
-
} catch (error) {
|
|
125
|
-
res.status(400).json({ error: error.message });
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
app.get("/api/plugins/:id/log", (req, res) => {
|
|
129
|
-
const rows = plugins.db
|
|
130
|
-
.prepare(
|
|
131
|
-
"SELECT id,time,kind,level,message FROM plugin_events WHERE plugin_id=? ORDER BY id DESC LIMIT 80",
|
|
132
|
-
)
|
|
133
|
-
.all(req.params.id);
|
|
134
|
-
res.json({ events: rows });
|
|
135
|
-
});
|
|
136
|
-
app.get("/api/plugins/actions/pending", (req, res) => {
|
|
137
|
-
res.json({ actions: plugins.chat.capabilities.pending() });
|
|
138
|
-
});
|
|
139
|
-
app.post("/api/plugins/actions/:action/approve", async (req, res) => {
|
|
140
|
-
try {
|
|
141
|
-
res.json(await plugins.chat.capabilities.approve(req.params.action));
|
|
142
|
-
} catch (error) {
|
|
143
|
-
res.status(400).json({ error: error.message });
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
app.post("/api/plugins/actions/:action/reject", (req, res) => {
|
|
147
|
-
try {
|
|
148
|
-
res.json(
|
|
149
|
-
plugins.chat.capabilities.reject(req.params.action, req.body.reason),
|
|
150
|
-
);
|
|
151
|
-
} catch (error) {
|
|
152
|
-
res.status(400).json({ error: error.message });
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
app.use("/api/plugins/:id/x", async (req, res) => {
|
|
156
|
-
try {
|
|
157
|
-
const path = req.path && req.path !== "/" ? req.path : "/";
|
|
158
|
-
const result = await plugins.handleHttp(
|
|
159
|
-
req.params.id,
|
|
160
|
-
req.method,
|
|
161
|
-
path,
|
|
162
|
-
req.body,
|
|
163
|
-
);
|
|
164
|
-
if (result == null)
|
|
165
|
-
return res.status(404).json({ error: "插件接口不存在" });
|
|
166
|
-
res.json(result && typeof result === "object" ? result : { result });
|
|
167
|
-
} catch (error) {
|
|
168
|
-
res.status(400).json({ error: error.message });
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
app.use("/hook/:id", async (req, res) => {
|
|
172
|
-
try {
|
|
173
|
-
const path = req.path && req.path !== "/" ? req.path : "/";
|
|
174
|
-
const route = plugins.routes.find(
|
|
175
|
-
(item) => item.id === req.params.id && item.open && item.path === path,
|
|
176
|
-
);
|
|
177
|
-
if (!route) return res.status(404).end();
|
|
178
|
-
const result = await plugins.handleHttp(
|
|
179
|
-
req.params.id,
|
|
180
|
-
req.method,
|
|
181
|
-
path,
|
|
182
|
-
req.body || {},
|
|
183
|
-
);
|
|
184
|
-
res.json(result && typeof result === "object" ? result : { result });
|
|
185
|
-
} catch {
|
|
186
|
-
res.status(400).end();
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
app.use("/plugin-ui/:id", (req, res) => {
|
|
190
|
-
const row = plugins.list().find((item) => item.id === req.params.id);
|
|
191
|
-
const page = row?.manifest.contributes?.page;
|
|
192
|
-
if (!row?.enabled || !page) return res.status(404).end();
|
|
193
|
-
const root = join(row.path, "ui");
|
|
194
|
-
const relative = req.path.replace(/^\//, "") || "index.html";
|
|
195
|
-
const file = safeFile(root, relative);
|
|
196
|
-
if (!file) return res.status(404).end();
|
|
197
|
-
if (file.endsWith(".html")) {
|
|
198
|
-
res.setHeader("Content-Security-Policy", SANDBOX);
|
|
199
|
-
res.setHeader("Cache-Control", "no-store");
|
|
200
|
-
}
|
|
201
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
202
|
-
createReadStream(file).pipe(res);
|
|
203
|
-
});
|
|
204
|
-
}
|
|
1
|
+
import { createReadStream, existsSync, statSync } from "node:fs";
|
|
2
|
+
import { join, normalize } from "node:path";
|
|
3
|
+
import { PERMISSIONS } from "./permissions.js";
|
|
4
|
+
|
|
5
|
+
const SANDBOX =
|
|
6
|
+
"sandbox allow-scripts allow-forms; default-src 'none'; img-src data: https:; style-src 'unsafe-inline' *; script-src 'unsafe-inline' *; connect-src 'none'";
|
|
7
|
+
|
|
8
|
+
function safeFile(root, requestPath) {
|
|
9
|
+
const relative = normalize(requestPath).replace(/^(\.\.[/\\])+/, "");
|
|
10
|
+
const file = join(root, relative);
|
|
11
|
+
if (!file.startsWith(root) || !existsSync(file) || !statSync(file).isFile())
|
|
12
|
+
return "";
|
|
13
|
+
return file;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function mountPlugins(app, plugins) {
|
|
17
|
+
if (!plugins) return;
|
|
18
|
+
app.get("/api/plugins", (req, res) => {
|
|
19
|
+
res.json({ plugins: plugins.list(), permissions: PERMISSIONS });
|
|
20
|
+
});
|
|
21
|
+
app.get("/api/plugins/market", async (req, res) => {
|
|
22
|
+
try {
|
|
23
|
+
res.json(await plugins.install.catalog());
|
|
24
|
+
} catch (error) {
|
|
25
|
+
res.status(400).json({ error: error.message });
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
app.post("/api/plugins/market/stage", async (req, res) => {
|
|
29
|
+
try {
|
|
30
|
+
res.json(await plugins.install.stageRegistry(req.body));
|
|
31
|
+
} catch (error) {
|
|
32
|
+
res.status(400).json({ error: error.message });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
app.post("/api/plugins/import/url", async (req, res) => {
|
|
36
|
+
try {
|
|
37
|
+
res.json(await plugins.install.stageUrl(req.body));
|
|
38
|
+
} catch (error) {
|
|
39
|
+
res.status(400).json({ error: error.message });
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
app.post("/api/plugins/import/github", async (req, res) => {
|
|
43
|
+
try {
|
|
44
|
+
res.json(await plugins.install.stageGithub(req.body.repository));
|
|
45
|
+
} catch (error) {
|
|
46
|
+
res.status(400).json({ error: error.message });
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
app.post("/api/plugins/import/dir", (req, res) => {
|
|
50
|
+
try {
|
|
51
|
+
res.json(plugins.install.stageDir(req.body.dir));
|
|
52
|
+
} catch (error) {
|
|
53
|
+
res.status(400).json({ error: error.message });
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
app.post("/api/plugins/import/commit", (req, res) => {
|
|
57
|
+
try {
|
|
58
|
+
res.json(plugins.install.commit(req.body.token));
|
|
59
|
+
} catch (error) {
|
|
60
|
+
res.status(400).json({ error: error.message });
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
app.post("/api/plugins/sources", (req, res) => {
|
|
64
|
+
try {
|
|
65
|
+
plugins.install.saveRegistry(req.body.url);
|
|
66
|
+
res.json({ ok: true });
|
|
67
|
+
} catch (error) {
|
|
68
|
+
res.status(400).json({ error: error.message });
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
app.delete("/api/plugins/sources/:source", (req, res) => {
|
|
72
|
+
plugins.install.removeRegistry(req.params.source);
|
|
73
|
+
res.json({ ok: true });
|
|
74
|
+
});
|
|
75
|
+
app.post("/api/plugins/:id/rollback", (req, res) => {
|
|
76
|
+
try {
|
|
77
|
+
res.json(plugins.install.rollback(req.params.id));
|
|
78
|
+
} catch (error) {
|
|
79
|
+
res.status(400).json({ error: error.message });
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
app.delete("/api/plugins/:id", async (req, res) => {
|
|
83
|
+
try {
|
|
84
|
+
res.json(
|
|
85
|
+
await plugins.install.remove(req.params.id, { data: !!req.body?.data }),
|
|
86
|
+
);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
res.status(400).json({ error: error.message });
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
app.post("/api/plugins/:id/grant", (req, res) => {
|
|
92
|
+
try {
|
|
93
|
+
res.json(plugins.grant(req.params.id, req.body.permissions || []));
|
|
94
|
+
} catch (error) {
|
|
95
|
+
res.status(400).json({ error: error.message });
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
app.post("/api/plugins/:id/enable", async (req, res) => {
|
|
99
|
+
try {
|
|
100
|
+
res.json(await plugins.enable(req.params.id));
|
|
101
|
+
} catch (error) {
|
|
102
|
+
res.status(400).json({ error: error.message });
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
app.post("/api/plugins/:id/disable", async (req, res) => {
|
|
106
|
+
try {
|
|
107
|
+
res.json(await plugins.disable(req.params.id));
|
|
108
|
+
} catch (error) {
|
|
109
|
+
res.status(400).json({ error: error.message });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
app.get("/api/plugins/:id/settings", (req, res) => {
|
|
113
|
+
try {
|
|
114
|
+
res.json(plugins.settings(req.params.id));
|
|
115
|
+
} catch (error) {
|
|
116
|
+
res.status(404).json({ error: error.message });
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
app.put("/api/plugins/:id/settings", (req, res) => {
|
|
120
|
+
try {
|
|
121
|
+
res.json(
|
|
122
|
+
plugins.saveSettings(req.params.id, req.body.values || req.body),
|
|
123
|
+
);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
res.status(400).json({ error: error.message });
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
app.get("/api/plugins/:id/log", (req, res) => {
|
|
129
|
+
const rows = plugins.db
|
|
130
|
+
.prepare(
|
|
131
|
+
"SELECT id,time,kind,level,message FROM plugin_events WHERE plugin_id=? ORDER BY id DESC LIMIT 80",
|
|
132
|
+
)
|
|
133
|
+
.all(req.params.id);
|
|
134
|
+
res.json({ events: rows });
|
|
135
|
+
});
|
|
136
|
+
app.get("/api/plugins/actions/pending", (req, res) => {
|
|
137
|
+
res.json({ actions: plugins.chat.capabilities.pending() });
|
|
138
|
+
});
|
|
139
|
+
app.post("/api/plugins/actions/:action/approve", async (req, res) => {
|
|
140
|
+
try {
|
|
141
|
+
res.json(await plugins.chat.capabilities.approve(req.params.action));
|
|
142
|
+
} catch (error) {
|
|
143
|
+
res.status(400).json({ error: error.message });
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
app.post("/api/plugins/actions/:action/reject", (req, res) => {
|
|
147
|
+
try {
|
|
148
|
+
res.json(
|
|
149
|
+
plugins.chat.capabilities.reject(req.params.action, req.body.reason),
|
|
150
|
+
);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
res.status(400).json({ error: error.message });
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
app.use("/api/plugins/:id/x", async (req, res) => {
|
|
156
|
+
try {
|
|
157
|
+
const path = req.path && req.path !== "/" ? req.path : "/";
|
|
158
|
+
const result = await plugins.handleHttp(
|
|
159
|
+
req.params.id,
|
|
160
|
+
req.method,
|
|
161
|
+
path,
|
|
162
|
+
req.body,
|
|
163
|
+
);
|
|
164
|
+
if (result == null)
|
|
165
|
+
return res.status(404).json({ error: "插件接口不存在" });
|
|
166
|
+
res.json(result && typeof result === "object" ? result : { result });
|
|
167
|
+
} catch (error) {
|
|
168
|
+
res.status(400).json({ error: error.message });
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
app.use("/hook/:id", async (req, res) => {
|
|
172
|
+
try {
|
|
173
|
+
const path = req.path && req.path !== "/" ? req.path : "/";
|
|
174
|
+
const route = plugins.routes.find(
|
|
175
|
+
(item) => item.id === req.params.id && item.open && item.path === path,
|
|
176
|
+
);
|
|
177
|
+
if (!route) return res.status(404).end();
|
|
178
|
+
const result = await plugins.handleHttp(
|
|
179
|
+
req.params.id,
|
|
180
|
+
req.method,
|
|
181
|
+
path,
|
|
182
|
+
req.body || {},
|
|
183
|
+
);
|
|
184
|
+
res.json(result && typeof result === "object" ? result : { result });
|
|
185
|
+
} catch {
|
|
186
|
+
res.status(400).end();
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
app.use("/plugin-ui/:id", (req, res) => {
|
|
190
|
+
const row = plugins.list().find((item) => item.id === req.params.id);
|
|
191
|
+
const page = row?.manifest.contributes?.page;
|
|
192
|
+
if (!row?.enabled || !page) return res.status(404).end();
|
|
193
|
+
const root = join(row.path, "ui");
|
|
194
|
+
const relative = req.path.replace(/^\//, "") || "index.html";
|
|
195
|
+
const file = safeFile(root, relative);
|
|
196
|
+
if (!file) return res.status(404).end();
|
|
197
|
+
if (file.endsWith(".html")) {
|
|
198
|
+
res.setHeader("Content-Security-Policy", SANDBOX);
|
|
199
|
+
res.setHeader("Cache-Control", "no-store");
|
|
200
|
+
}
|
|
201
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
202
|
+
createReadStream(file).pipe(res);
|
|
203
|
+
});
|
|
204
|
+
}
|