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,336 +1,335 @@
|
|
|
1
|
-
import { WebSocketServer, WebSocket } from "ws";
|
|
2
|
-
import { randomUUID } from "node:crypto";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// OneBot 11
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
let
|
|
23
|
-
let
|
|
24
|
-
let
|
|
25
|
-
let
|
|
26
|
-
let
|
|
27
|
-
let
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (Array.isArray(data
|
|
34
|
-
return
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
//
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
sock.
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
ws.isAlive = true;
|
|
207
|
-
ws.on("
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
event.status === "
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
event.
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
});
|
|
251
|
-
ws.on("
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
ws.
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
//
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
}
|
|
1
|
+
import { WebSocketServer, WebSocket } from "ws";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { effectiveOneBotToken, authorizeOneBot } from "./token.js";
|
|
4
|
+
import { onebot, normalize } from "./adapter.js";
|
|
5
|
+
import { defineChannel } from "../contract.js";
|
|
6
|
+
import { parseSessionKey } from "../session-key.js";
|
|
7
|
+
import { applyDirectoryNames } from "../../core/sessions.js";
|
|
8
|
+
import { saveAccountNames } from "../../studio/display-names.js";
|
|
9
|
+
import { visionImageLimits } from "../../core/image-preprocessor.js";
|
|
10
|
+
|
|
11
|
+
const EVENT_MAX_BYTES = 1024 * 1024;
|
|
12
|
+
const IMAGE_TIMEOUT_MS = 30000;
|
|
13
|
+
|
|
14
|
+
// OneBot 11 over a reverse WebSocket: a client she does not manage (any
|
|
15
|
+
// OneBot 11 implementation) connects to /onebot/v11/ws with the shared token.
|
|
16
|
+
export function createOneBotChannel(
|
|
17
|
+
store,
|
|
18
|
+
{ verifyPassword = async () => false } = {},
|
|
19
|
+
) {
|
|
20
|
+
const pending = new Map();
|
|
21
|
+
const botMessageIds = new Set();
|
|
22
|
+
let running = false;
|
|
23
|
+
let socket = null;
|
|
24
|
+
let connectedAt = null;
|
|
25
|
+
let lastEventAt = null;
|
|
26
|
+
let lastDisconnectAt = null;
|
|
27
|
+
let heartbeat = null;
|
|
28
|
+
let wss = null;
|
|
29
|
+
let directoryAt = 0;
|
|
30
|
+
const checkedNames = new Map();
|
|
31
|
+
|
|
32
|
+
function asList(data) {
|
|
33
|
+
if (Array.isArray(data)) return data;
|
|
34
|
+
if (Array.isArray(data?.data)) return data.data;
|
|
35
|
+
if (Array.isArray(data?.groups)) return data.groups;
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function refreshDirectory() {
|
|
40
|
+
if (!socket || socket.readyState !== WebSocket.OPEN) return 0;
|
|
41
|
+
if (Date.now() - directoryAt < 15000) return 0;
|
|
42
|
+
directoryAt = Date.now();
|
|
43
|
+
const listed = async (action, params = {}) => {
|
|
44
|
+
try {
|
|
45
|
+
return asList(await rpc(action, params, 8000));
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.error(`读取 QQ ${action} 失败:${error.message}`);
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const [groups, friends] = await Promise.all([
|
|
52
|
+
listed("get_group_list"),
|
|
53
|
+
listed("get_friend_list"),
|
|
54
|
+
]);
|
|
55
|
+
const known = store.db.prepare("SELECT id,kind FROM sessions").all();
|
|
56
|
+
for (const row of known) {
|
|
57
|
+
const native = String(row.id).split(":").pop();
|
|
58
|
+
const isGroup =
|
|
59
|
+
row.kind === "group" || String(row.id).includes(":group:");
|
|
60
|
+
if (!isGroup || !/^\d+$/.test(native)) continue;
|
|
61
|
+
if (groups.some((group) => String(group.group_id) === native)) continue;
|
|
62
|
+
try {
|
|
63
|
+
const info = await rpc(
|
|
64
|
+
"get_group_info",
|
|
65
|
+
{ group_id: Number(native) },
|
|
66
|
+
8000,
|
|
67
|
+
);
|
|
68
|
+
if (info && typeof info === "object") groups.push(info);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error(`读取群 ${native} 的名字失败:${error.message}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
let changed = applyDirectoryNames(store.db, { groups, friends });
|
|
74
|
+
changed += saveAccountNames(store.db, friends);
|
|
75
|
+
const ids = store.db
|
|
76
|
+
.prepare(
|
|
77
|
+
"SELECT user_id FROM mind_people ORDER BY last_seen DESC LIMIT 200",
|
|
78
|
+
)
|
|
79
|
+
.all()
|
|
80
|
+
.map((row) => row.user_id)
|
|
81
|
+
.filter(
|
|
82
|
+
(id) =>
|
|
83
|
+
/^\d{4,20}$/.test(id) &&
|
|
84
|
+
!friends.some((friend) => String(friend.user_id) === id) &&
|
|
85
|
+
Date.now() - (checkedNames.get(id) || 0) > 3600000,
|
|
86
|
+
);
|
|
87
|
+
const accounts = [];
|
|
88
|
+
for (let start = 0; start < ids.length; start += 4) {
|
|
89
|
+
await Promise.all(
|
|
90
|
+
ids.slice(start, start + 4).map(async (id) => {
|
|
91
|
+
checkedNames.set(id, Date.now());
|
|
92
|
+
try {
|
|
93
|
+
const person = await rpc(
|
|
94
|
+
"get_stranger_info",
|
|
95
|
+
{ user_id: Number(id) },
|
|
96
|
+
2500,
|
|
97
|
+
);
|
|
98
|
+
if (person?.nickname) accounts.push({ ...person, user_id: id });
|
|
99
|
+
} catch {
|
|
100
|
+
/* The message nickname remains usable if QQ cannot look it up. */
|
|
101
|
+
}
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
changed += saveAccountNames(store.db, accounts);
|
|
106
|
+
if (changed) store.revision++;
|
|
107
|
+
return changed;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const rpc = (action, params, timeoutMs) => {
|
|
111
|
+
if (!socket || socket.readyState !== WebSocket.OPEN)
|
|
112
|
+
throw new Error("QQ 尚未连接");
|
|
113
|
+
const echo = randomUUID();
|
|
114
|
+
return new Promise((resolve, reject) => {
|
|
115
|
+
const timer = setTimeout(() => {
|
|
116
|
+
pending.delete(echo);
|
|
117
|
+
reject(
|
|
118
|
+
new Error(
|
|
119
|
+
action === "get_msg"
|
|
120
|
+
? "引用恢复超时"
|
|
121
|
+
: action === "get_image"
|
|
122
|
+
? "QQ 图片读取超时"
|
|
123
|
+
: "QQ 发送确认超时,不自动重发",
|
|
124
|
+
),
|
|
125
|
+
);
|
|
126
|
+
}, timeoutMs);
|
|
127
|
+
pending.set(echo, { action, resolve, reject, timer });
|
|
128
|
+
socket.send(JSON.stringify({ action, params, echo }));
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const send = async (m, text) => {
|
|
133
|
+
if (m.simulated) return { message_id: "sim" };
|
|
134
|
+
const result = await rpc(
|
|
135
|
+
onebot.sendAction(m),
|
|
136
|
+
onebot.sendParams(m, text),
|
|
137
|
+
10000,
|
|
138
|
+
);
|
|
139
|
+
if (result?.message_id != null) {
|
|
140
|
+
botMessageIds.add(String(result.message_id));
|
|
141
|
+
while (botMessageIds.size > 2000)
|
|
142
|
+
botMessageIds.delete(botMessageIds.values().next().value);
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
async function fetchQuoted(message) {
|
|
148
|
+
const data = await rpc("get_msg", { message_id: message.replyId }, 4000);
|
|
149
|
+
return onebot.quotedMessage(data, message);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function fetchImage(file) {
|
|
153
|
+
const data = await rpc(
|
|
154
|
+
"get_image",
|
|
155
|
+
{ file: String(file) },
|
|
156
|
+
IMAGE_TIMEOUT_MS,
|
|
157
|
+
);
|
|
158
|
+
return data && typeof data === "object" ? data : null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function fetchMedia(file, type) {
|
|
162
|
+
const action = type === "record" ? "get_record" : "get_file";
|
|
163
|
+
const data = await rpc(action, { file: String(file) }, 8000);
|
|
164
|
+
return data && typeof data === "object" ? data : null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function attach(httpServer, chatSystem) {
|
|
168
|
+
// Only authenticated OneBot clients reach this server. get_image may carry
|
|
169
|
+
// base64 rather than a URL, so its frame must fit the source-image budget.
|
|
170
|
+
// Ordinary events still have the smaller independent limit below.
|
|
171
|
+
const imageBytes = visionImageLimits().maxSourceBytes;
|
|
172
|
+
wss = new WebSocketServer({
|
|
173
|
+
noServer: true,
|
|
174
|
+
maxPayload: Math.ceil(imageBytes / 3) * 4 + EVENT_MAX_BYTES,
|
|
175
|
+
});
|
|
176
|
+
httpServer.on("upgrade", async (req, sock, head) => {
|
|
177
|
+
let authorized = false;
|
|
178
|
+
try {
|
|
179
|
+
authorized = await authorizeOneBot(req, store, verifyPassword);
|
|
180
|
+
} catch {
|
|
181
|
+
/* Never expose credentials or verifier errors. */
|
|
182
|
+
}
|
|
183
|
+
if (!running || req.url !== "/onebot/v11/ws" || !authorized || socket) {
|
|
184
|
+
sock.write("HTTP/1.1 401 Unauthorized\r\n\r\n");
|
|
185
|
+
sock.destroy();
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
wss.handleUpgrade(req, sock, head, (ws) => wss.emit("connection", ws));
|
|
189
|
+
});
|
|
190
|
+
wss.on("connection", (ws) => {
|
|
191
|
+
socket = ws;
|
|
192
|
+
connectedAt = Date.now();
|
|
193
|
+
directoryAt = 0;
|
|
194
|
+
const pullNames = (attempt = 0) => {
|
|
195
|
+
if (!running || socket !== ws) return;
|
|
196
|
+
directoryAt = 0;
|
|
197
|
+
refreshDirectory()
|
|
198
|
+
.then((changed) => {
|
|
199
|
+
if (changed || attempt >= 4) return;
|
|
200
|
+
setTimeout(() => pullNames(attempt + 1), 4000).unref?.();
|
|
201
|
+
})
|
|
202
|
+
.catch(() => {});
|
|
203
|
+
};
|
|
204
|
+
pullNames();
|
|
205
|
+
ws.isAlive = true;
|
|
206
|
+
ws.on("pong", () => (ws.isAlive = true));
|
|
207
|
+
ws.on("message", async (raw) => {
|
|
208
|
+
try {
|
|
209
|
+
const event = JSON.parse(raw.toString());
|
|
210
|
+
if (!event || typeof event !== "object" || Array.isArray(event))
|
|
211
|
+
return;
|
|
212
|
+
const reply =
|
|
213
|
+
typeof event.echo === "string" && pending.get(event.echo);
|
|
214
|
+
const imageReply =
|
|
215
|
+
reply?.action === "get_image" &&
|
|
216
|
+
!event.post_type &&
|
|
217
|
+
((["number", "string"].includes(typeof event.retcode) &&
|
|
218
|
+
String(event.retcode).trim() !== "" &&
|
|
219
|
+
Number.isFinite(Number(event.retcode))) ||
|
|
220
|
+
["ok", "success", "failed"].includes(event.status));
|
|
221
|
+
if (raw.byteLength > EVENT_MAX_BYTES && !imageReply) {
|
|
222
|
+
console.error("OneBot: 超过普通消息大小限制,已忽略");
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
lastEventAt = Date.now();
|
|
226
|
+
if (reply) {
|
|
227
|
+
const p = reply;
|
|
228
|
+
clearTimeout(p.timer);
|
|
229
|
+
pending.delete(event.echo);
|
|
230
|
+
Number(event.retcode) === 0 ||
|
|
231
|
+
event.status === "ok" ||
|
|
232
|
+
event.status === "success"
|
|
233
|
+
? p.resolve(event.data ?? {})
|
|
234
|
+
: p.reject(
|
|
235
|
+
new Error(
|
|
236
|
+
event.message ||
|
|
237
|
+
event.wording ||
|
|
238
|
+
event.msg ||
|
|
239
|
+
"QQ 没有完成这个请求",
|
|
240
|
+
),
|
|
241
|
+
);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const m = normalize(event, { botMessageIds });
|
|
245
|
+
if (m) chatSystem.receive(m);
|
|
246
|
+
} catch (err) {
|
|
247
|
+
console.error("OneBot:", err.message);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
ws.on("error", () => {});
|
|
251
|
+
ws.on("close", () => {
|
|
252
|
+
if (socket === ws) socket = null;
|
|
253
|
+
lastDisconnectAt = Date.now();
|
|
254
|
+
for (const p of pending.values()) {
|
|
255
|
+
clearTimeout(p.timer);
|
|
256
|
+
p.reject(new Error("QQ 连接已断开"));
|
|
257
|
+
}
|
|
258
|
+
pending.clear();
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
heartbeat = setInterval(() => {
|
|
262
|
+
for (const ws of wss.clients) {
|
|
263
|
+
if (!ws.isAlive) {
|
|
264
|
+
ws.terminate();
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
ws.isAlive = false;
|
|
268
|
+
ws.ping();
|
|
269
|
+
}
|
|
270
|
+
}, 30000);
|
|
271
|
+
heartbeat.unref();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Whether a client may connect at all. Stopping also drops the one that is
|
|
275
|
+
// connected, so switching to another channel never leaves a stale line open.
|
|
276
|
+
function start() {
|
|
277
|
+
running = true;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function stop() {
|
|
281
|
+
running = false;
|
|
282
|
+
if (wss) for (const ws of wss.clients) ws.terminate();
|
|
283
|
+
for (const p of pending.values()) {
|
|
284
|
+
clearTimeout(p.timer);
|
|
285
|
+
p.reject(new Error("QQ 连接已断开"));
|
|
286
|
+
}
|
|
287
|
+
pending.clear();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function close() {
|
|
291
|
+
if (heartbeat) clearInterval(heartbeat);
|
|
292
|
+
stop();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// A connected client can reach every room it knows; there is no window and
|
|
296
|
+
// no per-room permission to respect.
|
|
297
|
+
function canReach(sessionId) {
|
|
298
|
+
if (!socket) return false;
|
|
299
|
+
try {
|
|
300
|
+
return parseSessionKey(sessionId).channel === "onebot";
|
|
301
|
+
} catch {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function status() {
|
|
307
|
+
return {
|
|
308
|
+
type: "onebot",
|
|
309
|
+
online: !!socket,
|
|
310
|
+
connectedAt,
|
|
311
|
+
lastEventAt,
|
|
312
|
+
lastDisconnectAt,
|
|
313
|
+
tokenConfigured: !!effectiveOneBotToken(store),
|
|
314
|
+
localConnectionAllowed: true,
|
|
315
|
+
wsPath: "/onebot/v11/ws",
|
|
316
|
+
port: Number(process.env.PORT || 3210),
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return defineChannel({
|
|
321
|
+
type: "onebot",
|
|
322
|
+
capabilities: onebot.capabilities,
|
|
323
|
+
attach,
|
|
324
|
+
start,
|
|
325
|
+
stop,
|
|
326
|
+
send,
|
|
327
|
+
fetchQuoted,
|
|
328
|
+
fetchImage,
|
|
329
|
+
fetchMedia,
|
|
330
|
+
refreshDirectory,
|
|
331
|
+
canReach,
|
|
332
|
+
status,
|
|
333
|
+
close,
|
|
334
|
+
});
|
|
335
|
+
}
|