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,250 +1,250 @@
|
|
|
1
|
-
import { pathToFileURL } from "node:url";
|
|
2
|
-
import "./guard.mjs";
|
|
3
|
-
|
|
4
|
-
const rawNet = process.env.LUCKYTRI_NET_RAW === "1";
|
|
5
|
-
const handlers = new Map();
|
|
6
|
-
const listeners = new Map();
|
|
7
|
-
let seq = 0;
|
|
8
|
-
const pending = new Map();
|
|
9
|
-
|
|
10
|
-
function send(message) {
|
|
11
|
-
const encoded = JSON.stringify(message);
|
|
12
|
-
if (encoded.length > 1024 * 1024) throw new Error("插件消息过大");
|
|
13
|
-
process.send(encoded);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function call(method, params, timeout = 15000) {
|
|
17
|
-
const id = `p${++seq}`;
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
const timer = setTimeout(() => {
|
|
20
|
-
pending.delete(id);
|
|
21
|
-
reject(new Error("插件请求超时"));
|
|
22
|
-
}, timeout);
|
|
23
|
-
pending.set(id, { resolve, reject, timer });
|
|
24
|
-
try {
|
|
25
|
-
send({ v: 1, id, type: "req", method, params });
|
|
26
|
-
} catch (error) {
|
|
27
|
-
clearTimeout(timer);
|
|
28
|
-
pending.delete(id);
|
|
29
|
-
reject(error);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function reply(id, ok, result, error) {
|
|
35
|
-
send({
|
|
36
|
-
v: 1,
|
|
37
|
-
id,
|
|
38
|
-
type: "res",
|
|
39
|
-
ok,
|
|
40
|
-
result: ok ? result : undefined,
|
|
41
|
-
error: ok ? undefined : String(error || "插件调用失败").slice(0, 300),
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const plugin = {
|
|
46
|
-
id: process.env.LUCKYTRI_PLUGIN_ID,
|
|
47
|
-
version: process.env.LUCKYTRI_PLUGIN_VERSION,
|
|
48
|
-
dataDir: process.env.LUCKYTRI_PLUGIN_DATA,
|
|
49
|
-
};
|
|
50
|
-
const features = JSON.parse(process.env.LUCKYTRI_API_FEATURES || "[]");
|
|
51
|
-
|
|
52
|
-
const ctx = {
|
|
53
|
-
plugin,
|
|
54
|
-
api: { version: "1.0", has: (feature) => features.includes(feature) },
|
|
55
|
-
now: () => call("now"),
|
|
56
|
-
log: {
|
|
57
|
-
info: (...parts) =>
|
|
58
|
-
call("log", { level: "info", message: parts.join(" ") }),
|
|
59
|
-
warn: (...parts) =>
|
|
60
|
-
call("log", { level: "warn", message: parts.join(" ") }),
|
|
61
|
-
error: (...parts) =>
|
|
62
|
-
call("log", { level: "error", message: parts.join(" ") }),
|
|
63
|
-
},
|
|
64
|
-
settings: {
|
|
65
|
-
get: () => call("settings.get"),
|
|
66
|
-
onChange(fn) {
|
|
67
|
-
listeners.set("settings", fn);
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
storage: {
|
|
71
|
-
get: (key) => call("storage.get", { key }),
|
|
72
|
-
set: (key, value) => call("storage.set", { key, value }),
|
|
73
|
-
delete: (key) => call("storage.delete", { key }),
|
|
74
|
-
list: (prefix) => call("storage.list", { prefix }),
|
|
75
|
-
},
|
|
76
|
-
net: {
|
|
77
|
-
fetch: async (url, init = {}) => {
|
|
78
|
-
const res = await call(
|
|
79
|
-
"net.fetch",
|
|
80
|
-
{
|
|
81
|
-
url: String(url),
|
|
82
|
-
method: init.method || "GET",
|
|
83
|
-
headers: init.headers || {},
|
|
84
|
-
body: typeof init.body === "string" ? init.body : "",
|
|
85
|
-
},
|
|
86
|
-
30000,
|
|
87
|
-
);
|
|
88
|
-
return new Response(res.body, {
|
|
89
|
-
status: res.status,
|
|
90
|
-
headers: res.headers,
|
|
91
|
-
});
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
mind: {
|
|
95
|
-
state: () => call("mind.state"),
|
|
96
|
-
observe: (observation) => call("observe", observation),
|
|
97
|
-
on(event, fn) {
|
|
98
|
-
const list = listeners.get(event) || [];
|
|
99
|
-
list.push(fn);
|
|
100
|
-
listeners.set(event, list);
|
|
101
|
-
call("mind.listen", { event }).catch(() => {});
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
senses: {
|
|
105
|
-
set: (key, sense) => call("senses.set", { key, sense }),
|
|
106
|
-
clear: (key) => call("senses.clear", { key }),
|
|
107
|
-
},
|
|
108
|
-
knowledge: {
|
|
109
|
-
offer: (doc) => call("knowledge.offer", doc),
|
|
110
|
-
},
|
|
111
|
-
activities: {
|
|
112
|
-
handle(kind, prepare) {
|
|
113
|
-
handlers.set(`activity:${kind}`, prepare);
|
|
114
|
-
return call("activities.handle", { kind });
|
|
115
|
-
},
|
|
116
|
-
setAvailable: (kind, reason) =>
|
|
117
|
-
call("activities.available", { kind, reason: reason || "" }),
|
|
118
|
-
},
|
|
119
|
-
actions: {
|
|
120
|
-
handle(name, run) {
|
|
121
|
-
handlers.set(`action:${name}`, run);
|
|
122
|
-
return call("actions.handle", { name });
|
|
123
|
-
},
|
|
124
|
-
},
|
|
125
|
-
perceive: {
|
|
126
|
-
handle(type, run) {
|
|
127
|
-
handlers.set(`perceive:${type}`, run);
|
|
128
|
-
return call("perceive.handle", { type });
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
channel: {
|
|
132
|
-
provide(type, impl) {
|
|
133
|
-
for (const method of [
|
|
134
|
-
"start",
|
|
135
|
-
"stop",
|
|
136
|
-
"send",
|
|
137
|
-
"fetchQuoted",
|
|
138
|
-
"fetchImage",
|
|
139
|
-
"fetchMedia",
|
|
140
|
-
"refreshDirectory",
|
|
141
|
-
"canReach",
|
|
142
|
-
"status",
|
|
143
|
-
])
|
|
144
|
-
if (typeof impl?.[method] === "function")
|
|
145
|
-
handlers.set(`channel:${type}:${method}`, impl[method]);
|
|
146
|
-
return call("channel.provide", { type });
|
|
147
|
-
},
|
|
148
|
-
receive: (type, message) => call("channel.receive", { type, message }),
|
|
149
|
-
status: (type, status) => call("channel.status", { type, status }),
|
|
150
|
-
reachable: (type, sessionKey, ok) =>
|
|
151
|
-
call("channel.reachable", { type, sessionKey, ok: !!ok }),
|
|
152
|
-
},
|
|
153
|
-
models: {
|
|
154
|
-
call: (req) => call("models.call", req, 120000),
|
|
155
|
-
},
|
|
156
|
-
http: {
|
|
157
|
-
route(method, path, handler) {
|
|
158
|
-
handlers.set(`http:${method}:${path}`, handler);
|
|
159
|
-
return call("http.route", { method, path, public: false });
|
|
160
|
-
},
|
|
161
|
-
hook(method, path, handler) {
|
|
162
|
-
handlers.set(`http:${method}:${path}`, handler);
|
|
163
|
-
return call("http.route", { method, path, public: true });
|
|
164
|
-
},
|
|
165
|
-
},
|
|
166
|
-
status: {
|
|
167
|
-
set: (status) => call("status.set", status),
|
|
168
|
-
},
|
|
169
|
-
notify: (notice) => call("notify", notice),
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
if (!rawNet) globalThis.fetch = (url, init) => ctx.net.fetch(url, init);
|
|
173
|
-
|
|
174
|
-
async function activate() {
|
|
175
|
-
const imported = await import(
|
|
176
|
-
pathToFileURL(process.env.LUCKYTRI_PLUGIN_ENTRY)
|
|
177
|
-
);
|
|
178
|
-
const plugin = imported.default;
|
|
179
|
-
if (!plugin || typeof plugin.activate !== "function")
|
|
180
|
-
throw new Error("插件入口无效");
|
|
181
|
-
await plugin.activate(ctx);
|
|
182
|
-
return { ok: true };
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
async function onMessage(raw) {
|
|
186
|
-
let message;
|
|
187
|
-
try {
|
|
188
|
-
message = JSON.parse(String(raw));
|
|
189
|
-
} catch {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
if (!message || message.v !== 1) return;
|
|
193
|
-
if (message.type === "res") {
|
|
194
|
-
const waiting = pending.get(message.id);
|
|
195
|
-
if (!waiting) return;
|
|
196
|
-
clearTimeout(waiting.timer);
|
|
197
|
-
pending.delete(message.id);
|
|
198
|
-
message.ok
|
|
199
|
-
? waiting.resolve(message.result)
|
|
200
|
-
: waiting.reject(new Error(message.error || "插件调用失败"));
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
if (message.type === "evt") {
|
|
204
|
-
const list = listeners.get(message.method) || [];
|
|
205
|
-
for (const fn of list)
|
|
206
|
-
Promise.resolve()
|
|
207
|
-
.then(() => fn(message.params))
|
|
208
|
-
.catch(() => {});
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
if (message.type !== "req") return;
|
|
212
|
-
try {
|
|
213
|
-
if (message.method === "activate")
|
|
214
|
-
return reply(message.id, true, await activate());
|
|
215
|
-
if (message.method === "ping")
|
|
216
|
-
return reply(message.id, true, { pong: true });
|
|
217
|
-
if (message.method === "deactivate") {
|
|
218
|
-
const imported = await import(
|
|
219
|
-
pathToFileURL(process.env.LUCKYTRI_PLUGIN_ENTRY)
|
|
220
|
-
).catch(() => null);
|
|
221
|
-
if (typeof imported?.default?.deactivate === "function")
|
|
222
|
-
await imported.default.deactivate();
|
|
223
|
-
return reply(message.id, true, { ok: true });
|
|
224
|
-
}
|
|
225
|
-
if (message.method === "dispatch") {
|
|
226
|
-
const fn = handlers.get(message.params?.slot);
|
|
227
|
-
if (!fn) throw new Error("插件没有这个处理");
|
|
228
|
-
return reply(message.id, true, await fn(message.params.input));
|
|
229
|
-
}
|
|
230
|
-
reply(message.id, false, null, "未知的插件调用");
|
|
231
|
-
} catch (error) {
|
|
232
|
-
reply(message.id, false, null, error.message);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
process.on("message", (raw) => {
|
|
237
|
-
onMessage(raw).catch((error) => {
|
|
238
|
-
try {
|
|
239
|
-
send({
|
|
240
|
-
v: 1,
|
|
241
|
-
id: "p0",
|
|
242
|
-
type: "evt",
|
|
243
|
-
method: "crash",
|
|
244
|
-
params: error.message,
|
|
245
|
-
});
|
|
246
|
-
} catch {
|
|
247
|
-
process.exit(1);
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
});
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
import "./guard.mjs";
|
|
3
|
+
|
|
4
|
+
const rawNet = process.env.LUCKYTRI_NET_RAW === "1";
|
|
5
|
+
const handlers = new Map();
|
|
6
|
+
const listeners = new Map();
|
|
7
|
+
let seq = 0;
|
|
8
|
+
const pending = new Map();
|
|
9
|
+
|
|
10
|
+
function send(message) {
|
|
11
|
+
const encoded = JSON.stringify(message);
|
|
12
|
+
if (encoded.length > 1024 * 1024) throw new Error("插件消息过大");
|
|
13
|
+
process.send(encoded);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function call(method, params, timeout = 15000) {
|
|
17
|
+
const id = `p${++seq}`;
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const timer = setTimeout(() => {
|
|
20
|
+
pending.delete(id);
|
|
21
|
+
reject(new Error("插件请求超时"));
|
|
22
|
+
}, timeout);
|
|
23
|
+
pending.set(id, { resolve, reject, timer });
|
|
24
|
+
try {
|
|
25
|
+
send({ v: 1, id, type: "req", method, params });
|
|
26
|
+
} catch (error) {
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
pending.delete(id);
|
|
29
|
+
reject(error);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function reply(id, ok, result, error) {
|
|
35
|
+
send({
|
|
36
|
+
v: 1,
|
|
37
|
+
id,
|
|
38
|
+
type: "res",
|
|
39
|
+
ok,
|
|
40
|
+
result: ok ? result : undefined,
|
|
41
|
+
error: ok ? undefined : String(error || "插件调用失败").slice(0, 300),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const plugin = {
|
|
46
|
+
id: process.env.LUCKYTRI_PLUGIN_ID,
|
|
47
|
+
version: process.env.LUCKYTRI_PLUGIN_VERSION,
|
|
48
|
+
dataDir: process.env.LUCKYTRI_PLUGIN_DATA,
|
|
49
|
+
};
|
|
50
|
+
const features = JSON.parse(process.env.LUCKYTRI_API_FEATURES || "[]");
|
|
51
|
+
|
|
52
|
+
const ctx = {
|
|
53
|
+
plugin,
|
|
54
|
+
api: { version: "1.0", has: (feature) => features.includes(feature) },
|
|
55
|
+
now: () => call("now"),
|
|
56
|
+
log: {
|
|
57
|
+
info: (...parts) =>
|
|
58
|
+
call("log", { level: "info", message: parts.join(" ") }),
|
|
59
|
+
warn: (...parts) =>
|
|
60
|
+
call("log", { level: "warn", message: parts.join(" ") }),
|
|
61
|
+
error: (...parts) =>
|
|
62
|
+
call("log", { level: "error", message: parts.join(" ") }),
|
|
63
|
+
},
|
|
64
|
+
settings: {
|
|
65
|
+
get: () => call("settings.get"),
|
|
66
|
+
onChange(fn) {
|
|
67
|
+
listeners.set("settings", fn);
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
storage: {
|
|
71
|
+
get: (key) => call("storage.get", { key }),
|
|
72
|
+
set: (key, value) => call("storage.set", { key, value }),
|
|
73
|
+
delete: (key) => call("storage.delete", { key }),
|
|
74
|
+
list: (prefix) => call("storage.list", { prefix }),
|
|
75
|
+
},
|
|
76
|
+
net: {
|
|
77
|
+
fetch: async (url, init = {}) => {
|
|
78
|
+
const res = await call(
|
|
79
|
+
"net.fetch",
|
|
80
|
+
{
|
|
81
|
+
url: String(url),
|
|
82
|
+
method: init.method || "GET",
|
|
83
|
+
headers: init.headers || {},
|
|
84
|
+
body: typeof init.body === "string" ? init.body : "",
|
|
85
|
+
},
|
|
86
|
+
30000,
|
|
87
|
+
);
|
|
88
|
+
return new Response(res.body, {
|
|
89
|
+
status: res.status,
|
|
90
|
+
headers: res.headers,
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
mind: {
|
|
95
|
+
state: () => call("mind.state"),
|
|
96
|
+
observe: (observation) => call("observe", observation),
|
|
97
|
+
on(event, fn) {
|
|
98
|
+
const list = listeners.get(event) || [];
|
|
99
|
+
list.push(fn);
|
|
100
|
+
listeners.set(event, list);
|
|
101
|
+
call("mind.listen", { event }).catch(() => {});
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
senses: {
|
|
105
|
+
set: (key, sense) => call("senses.set", { key, sense }),
|
|
106
|
+
clear: (key) => call("senses.clear", { key }),
|
|
107
|
+
},
|
|
108
|
+
knowledge: {
|
|
109
|
+
offer: (doc) => call("knowledge.offer", doc),
|
|
110
|
+
},
|
|
111
|
+
activities: {
|
|
112
|
+
handle(kind, prepare) {
|
|
113
|
+
handlers.set(`activity:${kind}`, prepare);
|
|
114
|
+
return call("activities.handle", { kind });
|
|
115
|
+
},
|
|
116
|
+
setAvailable: (kind, reason) =>
|
|
117
|
+
call("activities.available", { kind, reason: reason || "" }),
|
|
118
|
+
},
|
|
119
|
+
actions: {
|
|
120
|
+
handle(name, run) {
|
|
121
|
+
handlers.set(`action:${name}`, run);
|
|
122
|
+
return call("actions.handle", { name });
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
perceive: {
|
|
126
|
+
handle(type, run) {
|
|
127
|
+
handlers.set(`perceive:${type}`, run);
|
|
128
|
+
return call("perceive.handle", { type });
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
channel: {
|
|
132
|
+
provide(type, impl) {
|
|
133
|
+
for (const method of [
|
|
134
|
+
"start",
|
|
135
|
+
"stop",
|
|
136
|
+
"send",
|
|
137
|
+
"fetchQuoted",
|
|
138
|
+
"fetchImage",
|
|
139
|
+
"fetchMedia",
|
|
140
|
+
"refreshDirectory",
|
|
141
|
+
"canReach",
|
|
142
|
+
"status",
|
|
143
|
+
])
|
|
144
|
+
if (typeof impl?.[method] === "function")
|
|
145
|
+
handlers.set(`channel:${type}:${method}`, impl[method]);
|
|
146
|
+
return call("channel.provide", { type });
|
|
147
|
+
},
|
|
148
|
+
receive: (type, message) => call("channel.receive", { type, message }),
|
|
149
|
+
status: (type, status) => call("channel.status", { type, status }),
|
|
150
|
+
reachable: (type, sessionKey, ok) =>
|
|
151
|
+
call("channel.reachable", { type, sessionKey, ok: !!ok }),
|
|
152
|
+
},
|
|
153
|
+
models: {
|
|
154
|
+
call: (req) => call("models.call", req, 120000),
|
|
155
|
+
},
|
|
156
|
+
http: {
|
|
157
|
+
route(method, path, handler) {
|
|
158
|
+
handlers.set(`http:${method}:${path}`, handler);
|
|
159
|
+
return call("http.route", { method, path, public: false });
|
|
160
|
+
},
|
|
161
|
+
hook(method, path, handler) {
|
|
162
|
+
handlers.set(`http:${method}:${path}`, handler);
|
|
163
|
+
return call("http.route", { method, path, public: true });
|
|
164
|
+
},
|
|
165
|
+
},
|
|
166
|
+
status: {
|
|
167
|
+
set: (status) => call("status.set", status),
|
|
168
|
+
},
|
|
169
|
+
notify: (notice) => call("notify", notice),
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
if (!rawNet) globalThis.fetch = (url, init) => ctx.net.fetch(url, init);
|
|
173
|
+
|
|
174
|
+
async function activate() {
|
|
175
|
+
const imported = await import(
|
|
176
|
+
pathToFileURL(process.env.LUCKYTRI_PLUGIN_ENTRY)
|
|
177
|
+
);
|
|
178
|
+
const plugin = imported.default;
|
|
179
|
+
if (!plugin || typeof plugin.activate !== "function")
|
|
180
|
+
throw new Error("插件入口无效");
|
|
181
|
+
await plugin.activate(ctx);
|
|
182
|
+
return { ok: true };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function onMessage(raw) {
|
|
186
|
+
let message;
|
|
187
|
+
try {
|
|
188
|
+
message = JSON.parse(String(raw));
|
|
189
|
+
} catch {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if (!message || message.v !== 1) return;
|
|
193
|
+
if (message.type === "res") {
|
|
194
|
+
const waiting = pending.get(message.id);
|
|
195
|
+
if (!waiting) return;
|
|
196
|
+
clearTimeout(waiting.timer);
|
|
197
|
+
pending.delete(message.id);
|
|
198
|
+
message.ok
|
|
199
|
+
? waiting.resolve(message.result)
|
|
200
|
+
: waiting.reject(new Error(message.error || "插件调用失败"));
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (message.type === "evt") {
|
|
204
|
+
const list = listeners.get(message.method) || [];
|
|
205
|
+
for (const fn of list)
|
|
206
|
+
Promise.resolve()
|
|
207
|
+
.then(() => fn(message.params))
|
|
208
|
+
.catch(() => {});
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (message.type !== "req") return;
|
|
212
|
+
try {
|
|
213
|
+
if (message.method === "activate")
|
|
214
|
+
return reply(message.id, true, await activate());
|
|
215
|
+
if (message.method === "ping")
|
|
216
|
+
return reply(message.id, true, { pong: true });
|
|
217
|
+
if (message.method === "deactivate") {
|
|
218
|
+
const imported = await import(
|
|
219
|
+
pathToFileURL(process.env.LUCKYTRI_PLUGIN_ENTRY)
|
|
220
|
+
).catch(() => null);
|
|
221
|
+
if (typeof imported?.default?.deactivate === "function")
|
|
222
|
+
await imported.default.deactivate();
|
|
223
|
+
return reply(message.id, true, { ok: true });
|
|
224
|
+
}
|
|
225
|
+
if (message.method === "dispatch") {
|
|
226
|
+
const fn = handlers.get(message.params?.slot);
|
|
227
|
+
if (!fn) throw new Error("插件没有这个处理");
|
|
228
|
+
return reply(message.id, true, await fn(message.params.input));
|
|
229
|
+
}
|
|
230
|
+
reply(message.id, false, null, "未知的插件调用");
|
|
231
|
+
} catch (error) {
|
|
232
|
+
reply(message.id, false, null, error.message);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
process.on("message", (raw) => {
|
|
237
|
+
onMessage(raw).catch((error) => {
|
|
238
|
+
try {
|
|
239
|
+
send({
|
|
240
|
+
v: 1,
|
|
241
|
+
id: "p0",
|
|
242
|
+
type: "evt",
|
|
243
|
+
method: "crash",
|
|
244
|
+
params: error.message,
|
|
245
|
+
});
|
|
246
|
+
} catch {
|
|
247
|
+
process.exit(1);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
});
|