@napuketto/adapter 0.16.4 → 0.17.0
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/dist/{essence-CU3kvItK.mjs → buddy-req-BvAxJo6d.mjs} +84 -84
- package/dist/buddy-req-BvAxJo6d.mjs.map +1 -0
- package/dist/core/index.d.mts +5 -2
- package/dist/core/index.mjs +1 -1
- package/dist/{core-l7TM-aKF.mjs → core-D8sq68iP.mjs} +44 -5
- package/dist/core-D8sq68iP.mjs.map +1 -0
- package/dist/{log-AtQxAmbQ.mjs → gray-tip-DmZmdtjP.mjs} +46 -46
- package/dist/gray-tip-DmZmdtjP.mjs.map +1 -0
- package/dist/index-B-HpCes-.d.mts +820 -0
- package/dist/index-B-HpCes-.d.mts.map +1 -0
- package/dist/index-CepyMhuM.d.mts +701 -0
- package/dist/index-CepyMhuM.d.mts.map +1 -0
- package/dist/index-DBD-IZKn.d.mts +808 -0
- package/dist/index-DBD-IZKn.d.mts.map +1 -0
- package/dist/{index-jdSniAj1.d.mts → index-DBjlR2dn.d.mts} +26 -6
- package/dist/index-DBjlR2dn.d.mts.map +1 -0
- package/dist/index-dsurF2t_.d.mts +435 -0
- package/dist/index-dsurF2t_.d.mts.map +1 -0
- package/dist/index.d.mts +23 -1
- package/dist/index.mjs +6 -1
- package/dist/kurobot/index.d.mts +2 -819
- package/dist/kurobot/index.mjs +1 -1125
- package/dist/kurobot-cMBAVX3L.mjs +1126 -0
- package/dist/kurobot-cMBAVX3L.mjs.map +1 -0
- package/dist/milky/index.d.mts +5 -206
- package/dist/milky/index.mjs +1 -3321
- package/dist/milky-CYAOJLv_.mjs +3323 -0
- package/dist/milky-CYAOJLv_.mjs.map +1 -0
- package/dist/onebot11/index.d.mts +2 -805
- package/dist/onebot11/index.mjs +1 -3899
- package/dist/onebot11-BikYDo3k.mjs +3900 -0
- package/dist/onebot11-BikYDo3k.mjs.map +1 -0
- package/dist/satori/index.d.mts +2 -433
- package/dist/satori/index.mjs +1 -1743
- package/dist/satori-Cb2uM3PY.mjs +1754 -0
- package/dist/satori-Cb2uM3PY.mjs.map +1 -0
- package/package.json +4 -4
- package/dist/core-l7TM-aKF.mjs.map +0 -1
- package/dist/essence-CU3kvItK.mjs.map +0 -1
- package/dist/index-jdSniAj1.d.mts.map +0 -1
- package/dist/kurobot/index.d.mts.map +0 -1
- package/dist/kurobot/index.mjs.map +0 -1
- package/dist/log-AtQxAmbQ.mjs.map +0 -1
- package/dist/milky/index.d.mts.map +0 -1
- package/dist/milky/index.mjs.map +0 -1
- package/dist/onebot11/index.d.mts.map +0 -1
- package/dist/onebot11/index.mjs.map +0 -1
- package/dist/satori/index.d.mts.map +0 -1
- package/dist/satori/index.mjs.map +0 -1
package/dist/satori/index.mjs
CHANGED
|
@@ -1,1743 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { c as MS_TO_SEC, i as toUnixSeconds, p as toUinString, r as subscribeRawMessages } from "../raw-message-D5nMIebz.mjs";
|
|
3
|
-
import { a as ensureSilk, c as collectGrayTipUids, i as downloadBufferToFile, t as logEventFailure } from "../log-AtQxAmbQ.mjs";
|
|
4
|
-
import { ChatType, GrayTipSubType, NTGroupRequestOperateTypes, TipGroupElementType, kernelError, toCanonicalElements } from "@napuketto/kernel";
|
|
5
|
-
import { z } from "zod";
|
|
6
|
-
import { join } from "node:path";
|
|
7
|
-
import { transcodeVideo } from "@napuketto/media";
|
|
8
|
-
import { HttpServer, WsServer } from "@napuketto/network";
|
|
9
|
-
// src/satori/helper/config.ts
|
|
10
|
-
/**
|
|
11
|
-
* Satori 协议配置(zod schema,归属本包)
|
|
12
|
-
* 协议配置 schema 在各自协议包,kernel 只提供 ConfigBase 基类。
|
|
13
|
-
*/
|
|
14
|
-
/** HTTP RPC 服务器默认端口(satori.chat 惯例 5500 区间,取 5500)。 */
|
|
15
|
-
const DEFAULT_HTTP_PORT = 5500;
|
|
16
|
-
/** WS 事件服务默认端口(satori.chat 惯例 5500 区间,取 5501)。 */
|
|
17
|
-
const DEFAULT_WS_PORT = 5501;
|
|
18
|
-
/** HTTP RPC 服务器实例(POST /v1/{resource}.{method})。 */
|
|
19
|
-
const httpServerSchema = createServerSchema(DEFAULT_HTTP_PORT);
|
|
20
|
-
/** WS 事件服务实例(/v1/events 信令,第三方 SDK 连入)。 */
|
|
21
|
-
const wsServerSchema = createServerSchema(DEFAULT_WS_PORT);
|
|
22
|
-
/** Satori 配置 schema。 */
|
|
23
|
-
const satoriConfigSchema = z.object({
|
|
24
|
-
/** 鉴权 token(全局默认,实例未指定时继承)。 */
|
|
25
|
-
token: z.string().optional(),
|
|
26
|
-
/** 媒体资源(img/audio/video/file)下载缓存目录。 */
|
|
27
|
-
cacheDir: z.string().optional(),
|
|
28
|
-
/** HTTP RPC 服务器列表。 */
|
|
29
|
-
httpServers: z.array(httpServerSchema).default([]),
|
|
30
|
-
/** WS 事件服务列表。 */
|
|
31
|
-
wsServers: z.array(wsServerSchema).default([])
|
|
32
|
-
});
|
|
33
|
-
// src/satori/helper/error.ts
|
|
34
|
-
/** KernelErrorCode → HTTP 状态码映射表。 */
|
|
35
|
-
const satoriHttpStatusMap = {
|
|
36
|
-
SEND_FAILED: 500,
|
|
37
|
-
PERMISSION_DENIED: 403,
|
|
38
|
-
NOT_FOUND: 404,
|
|
39
|
-
TIMEOUT: 500,
|
|
40
|
-
NOT_LOGIN: 403,
|
|
41
|
-
INVALID_PARAM: 400,
|
|
42
|
-
INVALID_STATE: 500,
|
|
43
|
-
UNKNOWN: 500
|
|
44
|
-
};
|
|
45
|
-
/** HTTP 状态码常量。 */
|
|
46
|
-
const HTTP_STATUS = {
|
|
47
|
-
ok: 200,
|
|
48
|
-
badRequest: 400,
|
|
49
|
-
unauthorized: 401,
|
|
50
|
-
forbidden: 403,
|
|
51
|
-
notFound: 404,
|
|
52
|
-
notImplemented: 501,
|
|
53
|
-
serverError: 500
|
|
54
|
-
};
|
|
55
|
-
/** 适配器名(固定 napuketto)。 */
|
|
56
|
-
const ADAPTER = "napuketto";
|
|
57
|
-
/** 平台特性(API 可用性判断;群聊频道与群组重合 → guild.plain)。 */
|
|
58
|
-
const FEATURES = ["guild.plain", "message.list.from"];
|
|
59
|
-
/** 构造用户(uin)。 */
|
|
60
|
-
function toUser(id, name, nick) {
|
|
61
|
-
const user = { id };
|
|
62
|
-
if (name !== void 0 && name !== "") user.name = name;
|
|
63
|
-
if (nick !== void 0 && nick !== "") user.nick = nick;
|
|
64
|
-
return user;
|
|
65
|
-
}
|
|
66
|
-
/** 构造群聊频道(id = 群号)。 */
|
|
67
|
-
function toGroupChannel(groupCode, name) {
|
|
68
|
-
const channel = {
|
|
69
|
-
id: String(groupCode),
|
|
70
|
-
type: 0
|
|
71
|
-
};
|
|
72
|
-
if (name !== void 0 && name !== "") channel.name = name;
|
|
73
|
-
return channel;
|
|
74
|
-
}
|
|
75
|
-
/** 构造私聊频道(id = 对端 uin)。 */
|
|
76
|
-
function toDirectChannel(uin, name) {
|
|
77
|
-
const channel = {
|
|
78
|
-
id: String(uin),
|
|
79
|
-
type: 1
|
|
80
|
-
};
|
|
81
|
-
if (name !== void 0 && name !== "") channel.name = name;
|
|
82
|
-
return channel;
|
|
83
|
-
}
|
|
84
|
-
/** 构造群组(id = 群号)。 */
|
|
85
|
-
function toGuild(groupCode, name, avatar) {
|
|
86
|
-
const guild = { id: String(groupCode) };
|
|
87
|
-
if (name !== void 0 && name !== "") guild.name = name;
|
|
88
|
-
if (avatar !== void 0 && avatar !== "") guild.avatar = avatar;
|
|
89
|
-
return guild;
|
|
90
|
-
}
|
|
91
|
-
/** 构造登录信息(login.get / READY logins)。 */
|
|
92
|
-
function toLogin(self, sn, online) {
|
|
93
|
-
return {
|
|
94
|
-
sn,
|
|
95
|
-
platform: "qq",
|
|
96
|
-
user: toUser(self.uin, self.nickname),
|
|
97
|
-
status: online ? 1 : 0,
|
|
98
|
-
adapter: ADAPTER,
|
|
99
|
-
features: [...FEATURES]
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
/** 构造非登录事件的最小 login(只带 sn/user/platform,规范)。 */
|
|
103
|
-
function toMinimalLogin(sn, selfUin) {
|
|
104
|
-
return {
|
|
105
|
-
sn,
|
|
106
|
-
platform: "qq",
|
|
107
|
-
user: toUser(selfUin),
|
|
108
|
-
status: 1,
|
|
109
|
-
adapter: ADAPTER
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
// src/satori/helper/element/canonical.ts
|
|
113
|
-
/** 收集 at uid → 批量 uidToUin(失败不阻塞,at 原样 uid)。 */
|
|
114
|
-
async function resolveAtUids(elements, uidToUin) {
|
|
115
|
-
const atUids = [];
|
|
116
|
-
for (const el of elements) if (el.type === "at" && el.target !== "all") atUids.push(el.target);
|
|
117
|
-
if (atUids.length === 0 || uidToUin === void 0) return /* @__PURE__ */ new Map();
|
|
118
|
-
try {
|
|
119
|
-
return await uidToUin([...new Set(atUids)]);
|
|
120
|
-
} catch {
|
|
121
|
-
return /* @__PURE__ */ new Map();
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
/** canonical at → Satori at(uid → uin,name = display)。 */
|
|
125
|
-
function atToSatori(el, uinMap) {
|
|
126
|
-
if (el.target === "all") return {
|
|
127
|
-
type: "at",
|
|
128
|
-
attrs: { id: "all" }
|
|
129
|
-
};
|
|
130
|
-
const attrs = { id: uinMap.get(el.target) ?? el.target };
|
|
131
|
-
if (el.display !== void 0 && el.display !== "") attrs["name"] = el.display;
|
|
132
|
-
return {
|
|
133
|
-
type: "at",
|
|
134
|
-
attrs
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
/** canonical file → Satori file(title = name)。 */
|
|
138
|
-
function fileToSatori(el) {
|
|
139
|
-
const attrs = { src: el.path };
|
|
140
|
-
if (el.name !== void 0 && el.name !== "") attrs["title"] = el.name;
|
|
141
|
-
return {
|
|
142
|
-
type: "file",
|
|
143
|
-
attrs
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
/** canonical forward → Satori message(取首个消息 id)。 */
|
|
147
|
-
function forwardToSatori(el) {
|
|
148
|
-
const first = el.messageIds[0];
|
|
149
|
-
if (first === void 0 || first === "") return null;
|
|
150
|
-
return {
|
|
151
|
-
type: "message",
|
|
152
|
-
attrs: {
|
|
153
|
-
forward: "",
|
|
154
|
-
id: first
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
/** 单个 canonical → Satori(null = 宽容忽略,如 json/xml/unknown)。 */
|
|
159
|
-
function canonicalToSatori(el, uinMap) {
|
|
160
|
-
switch (el.type) {
|
|
161
|
-
case "text": return el.text === "" ? null : {
|
|
162
|
-
type: "text",
|
|
163
|
-
text: el.text
|
|
164
|
-
};
|
|
165
|
-
case "at": return atToSatori(el, uinMap);
|
|
166
|
-
case "image": return {
|
|
167
|
-
type: "img",
|
|
168
|
-
attrs: { src: el.url ?? el.path }
|
|
169
|
-
};
|
|
170
|
-
case "face": return {
|
|
171
|
-
type: "emoji",
|
|
172
|
-
attrs: { id: el.id }
|
|
173
|
-
};
|
|
174
|
-
case "voice": return {
|
|
175
|
-
type: "audio",
|
|
176
|
-
attrs: { src: el.path }
|
|
177
|
-
};
|
|
178
|
-
case "video": return {
|
|
179
|
-
type: "video",
|
|
180
|
-
attrs: { src: el.url ?? el.path }
|
|
181
|
-
};
|
|
182
|
-
case "file": return fileToSatori(el);
|
|
183
|
-
case "reply": return {
|
|
184
|
-
type: "quote",
|
|
185
|
-
attrs: { id: el.messageId }
|
|
186
|
-
};
|
|
187
|
-
case "forward": return forwardToSatori(el);
|
|
188
|
-
case "json":
|
|
189
|
-
case "xml":
|
|
190
|
-
case "unknown": return null;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* 收方向:kernel canonical 元素 → Satori 元素树(纯函数映射,宽容忽略 unknown)。
|
|
195
|
-
* at 目标为 uid 时经 uidToUin 转换(异步);无转换器时原样。
|
|
196
|
-
*/
|
|
197
|
-
async function canonicalToSatoriElements(elements, deps = {}) {
|
|
198
|
-
const uinMap = await resolveAtUids(elements, deps.uidToUin);
|
|
199
|
-
const out = [];
|
|
200
|
-
for (const el of elements) {
|
|
201
|
-
const converted = canonicalToSatori(el, uinMap);
|
|
202
|
-
if (converted !== null) out.push(converted);
|
|
203
|
-
}
|
|
204
|
-
return out;
|
|
205
|
-
}
|
|
206
|
-
// src/satori/helper/element/parse-tag.ts
|
|
207
|
-
/**
|
|
208
|
-
* Satori 标签读取(元素名 / 属性解析,从 element.ts 拆分,FTA 优化)
|
|
209
|
-
*
|
|
210
|
-
* - decodeEntity/decodeEntities:命名/十进制/十六进制实体解码(属性值用)
|
|
211
|
-
* - readTagName:读取元素名(小写字母/数字/连字符,以字母开头)
|
|
212
|
-
* - readTagAttrs / readAttr / skipSpaces:标签属性区解析(无值属性 / 引号值 / 无引号值)
|
|
213
|
-
* - readTag:完整标签(名 + 属性 + 自闭合指示)
|
|
214
|
-
*/
|
|
215
|
-
/** 命名实体(规范规定的四个 + apos 宽容)。 */
|
|
216
|
-
const NAMED_ENTITIES = {
|
|
217
|
-
quot: "\"",
|
|
218
|
-
amp: "&",
|
|
219
|
-
lt: "<",
|
|
220
|
-
gt: ">",
|
|
221
|
-
apos: "'"
|
|
222
|
-
};
|
|
223
|
-
/** 解码单个实体(命名/十进制/十六进制)。 */
|
|
224
|
-
function decodeEntity(body) {
|
|
225
|
-
const named = NAMED_ENTITIES[body];
|
|
226
|
-
if (named !== void 0) return named;
|
|
227
|
-
if (body.startsWith("#x") || body.startsWith("#X")) {
|
|
228
|
-
const code = Number.parseInt(body.slice(2), 16);
|
|
229
|
-
if (Number.isInteger(code)) return String.fromCodePoint(code);
|
|
230
|
-
}
|
|
231
|
-
if (body.startsWith("#")) {
|
|
232
|
-
const code = Number.parseInt(body.slice(1), 10);
|
|
233
|
-
if (Number.isInteger(code)) return String.fromCodePoint(code);
|
|
234
|
-
}
|
|
235
|
-
return `&${body};`;
|
|
236
|
-
}
|
|
237
|
-
/** 解码字符串中的全部实体。 */
|
|
238
|
-
const ENTITY_RE = /&([^;]+);/g;
|
|
239
|
-
/** 解码字符串中的全部实体。 */
|
|
240
|
-
function decodeEntities(input) {
|
|
241
|
-
return input.replace(ENTITY_RE, (_full, body) => decodeEntity(body));
|
|
242
|
-
}
|
|
243
|
-
/** 元素名:小写字母/数字/连字符,以字母开头(规范)。 */
|
|
244
|
-
const NAME_RE = /^[a-z][a-z0-9-]*/i;
|
|
245
|
-
/** 读取元素名(pos 起);未匹配返回空名。 */
|
|
246
|
-
function readTagName(source, pos) {
|
|
247
|
-
const rest = source.slice(pos);
|
|
248
|
-
const m = NAME_RE.exec(rest);
|
|
249
|
-
if (m === null) return {
|
|
250
|
-
name: "",
|
|
251
|
-
end: pos
|
|
252
|
-
};
|
|
253
|
-
return {
|
|
254
|
-
name: m[0],
|
|
255
|
-
end: pos + m[0].length
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
/** 空白字符。 */
|
|
259
|
-
const SPACE_RE = /\s/;
|
|
260
|
-
/** 跳过空白;返回下一个非空白位置。 */
|
|
261
|
-
function skipSpaces(source, start) {
|
|
262
|
-
let i = start;
|
|
263
|
-
while (i < source.length && SPACE_RE.test(source.charAt(i))) i += 1;
|
|
264
|
-
return i;
|
|
265
|
-
}
|
|
266
|
-
/** 属性名(字母数字 / 冒号(命名空间)/ 连字符 / 下划线)。 */
|
|
267
|
-
const ATTR_NAME_RE = /^[a-zA-Z0-9:_-]+/;
|
|
268
|
-
/** 无引号属性值的结束字符(空白 / >)。 */
|
|
269
|
-
const ATTR_VALUE_END_RE = /[\s>]/;
|
|
270
|
-
/** 读取单个属性(名 / = / 值 / 无值属性),更新 attrs,返回下一个位置。 */
|
|
271
|
-
function readAttr(source, start, attrs) {
|
|
272
|
-
const keyMatch = ATTR_NAME_RE.exec(source.slice(start));
|
|
273
|
-
if (keyMatch === null) return start + 1;
|
|
274
|
-
const key = keyMatch[0];
|
|
275
|
-
let i = skipSpaces(source, start + key.length);
|
|
276
|
-
if (source.charAt(i) !== "=") {
|
|
277
|
-
attrs[key] = "";
|
|
278
|
-
return i;
|
|
279
|
-
}
|
|
280
|
-
i = skipSpaces(source, i + 1);
|
|
281
|
-
const quote = source.charAt(i);
|
|
282
|
-
if (quote === "\"" || quote === "'") {
|
|
283
|
-
i += 1;
|
|
284
|
-
const valueStart = i;
|
|
285
|
-
while (i < source.length && source.charAt(i) !== quote) i += 1;
|
|
286
|
-
attrs[key] = decodeEntities(source.slice(valueStart, i));
|
|
287
|
-
if (i < source.length) i += 1;
|
|
288
|
-
} else {
|
|
289
|
-
const valueStart = i;
|
|
290
|
-
while (i < source.length && !ATTR_VALUE_END_RE.test(source.charAt(i))) i += 1;
|
|
291
|
-
attrs[key] = decodeEntities(source.slice(valueStart, i));
|
|
292
|
-
}
|
|
293
|
-
return i;
|
|
294
|
-
}
|
|
295
|
-
/** 读取标签属性区(到 > 或 /> 为止),返回属性表与闭合指示、结束位置。 */
|
|
296
|
-
function readTagAttrs(source, start) {
|
|
297
|
-
const attrs = {};
|
|
298
|
-
let i = start;
|
|
299
|
-
let selfClosing = false;
|
|
300
|
-
while (i < source.length) {
|
|
301
|
-
const ch = source.charAt(i);
|
|
302
|
-
if (ch === ">") {
|
|
303
|
-
i += 1;
|
|
304
|
-
break;
|
|
305
|
-
}
|
|
306
|
-
if (ch === "/") {
|
|
307
|
-
if (source.charAt(i + 1) === ">") {
|
|
308
|
-
selfClosing = true;
|
|
309
|
-
i += 2;
|
|
310
|
-
break;
|
|
311
|
-
}
|
|
312
|
-
i += 1;
|
|
313
|
-
continue;
|
|
314
|
-
}
|
|
315
|
-
if (SPACE_RE.test(ch)) {
|
|
316
|
-
i += 1;
|
|
317
|
-
continue;
|
|
318
|
-
}
|
|
319
|
-
i = readAttr(source, i, attrs);
|
|
320
|
-
}
|
|
321
|
-
return {
|
|
322
|
-
attrs,
|
|
323
|
-
selfClosing,
|
|
324
|
-
end: i
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
/** 读取完整标签(元素名 + 属性 + 闭合指示),返回属性表与结束位置。 */
|
|
328
|
-
function readTag(source, pos) {
|
|
329
|
-
const { name, end } = readTagName(source, pos);
|
|
330
|
-
if (name === "") return {
|
|
331
|
-
name: "",
|
|
332
|
-
attrs: {},
|
|
333
|
-
selfClosing: false,
|
|
334
|
-
end: pos
|
|
335
|
-
};
|
|
336
|
-
const { attrs, selfClosing, end: attrEnd } = readTagAttrs(source, end);
|
|
337
|
-
return {
|
|
338
|
-
name,
|
|
339
|
-
attrs,
|
|
340
|
-
selfClosing,
|
|
341
|
-
end: attrEnd
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
// src/satori/helper/element/element.ts
|
|
345
|
-
/**
|
|
346
|
-
* Satori 消息元素解析/渲染(协议 v1,自研实现,规范参考 satori.chat/zh-CN/protocol/message.html)
|
|
347
|
-
*
|
|
348
|
-
* - parseElements:XML 风格字符串 → 元素树(注释/转义/无值属性/未配对标签容错)
|
|
349
|
-
* - renderElements:元素树 → 字符串(消息 content 序列化)
|
|
350
|
-
*
|
|
351
|
-
* canonical ↔ Satori 双向转换不在本文件:收方向(canonical → Satori)在
|
|
352
|
-
* canonical.ts,发方向(Satori → canonical)在 element-convert.ts,调用方直引。
|
|
353
|
-
*
|
|
354
|
-
* 语法要点(对齐规范):
|
|
355
|
-
* - 转义:" & < > ' ' 与十进制/十六进制数字实体
|
|
356
|
-
* - 注释 <!-- --> 不渲染
|
|
357
|
-
* - 无值属性(key)与 key="value" / key='value' 均合法
|
|
358
|
-
* - 未配对的标签视为文本内容的一部分
|
|
359
|
-
* - 文本内容前后包含换行符的连续空白会被忽略
|
|
360
|
-
*/
|
|
361
|
-
/** 文本转义正则(& < >)。 */
|
|
362
|
-
const TEXT_ESCAPE_RE = /[&<>]/g;
|
|
363
|
-
/** 文本转义(& < >)。 */
|
|
364
|
-
function encodeText(input) {
|
|
365
|
-
return input.replace(TEXT_ESCAPE_RE, (ch) => {
|
|
366
|
-
if (ch === "&") return "&";
|
|
367
|
-
if (ch === "<") return "<";
|
|
368
|
-
return ">";
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
/** 属性值转义正则(& " < >)。 */
|
|
372
|
-
const ATTR_ESCAPE_RE = /[&"<>]/g;
|
|
373
|
-
/** 属性值转义(& " < >)。 */
|
|
374
|
-
function encodeAttr(input) {
|
|
375
|
-
return input.replace(ATTR_ESCAPE_RE, (ch) => {
|
|
376
|
-
if (ch === "&") return "&";
|
|
377
|
-
if (ch === "\"") return """;
|
|
378
|
-
if (ch === "<") return "<";
|
|
379
|
-
return ">";
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
/** 行首连续空白。 */
|
|
383
|
-
const LEADING_WS_RE = /^\s+/;
|
|
384
|
-
/** 行尾连续空白。 */
|
|
385
|
-
const TRAILING_WS_RE = /\s+$/;
|
|
386
|
-
/** 换行符。 */
|
|
387
|
-
const NEWLINE_RE = /[\r\n]/;
|
|
388
|
-
/**
|
|
389
|
-
* 文本规范化:开头/结尾的「包含换行符的连续空白」被忽略(规范),
|
|
390
|
-
* 中间的空白与不含换行的首尾空白保留。
|
|
391
|
-
*/
|
|
392
|
-
function normalizeText(raw) {
|
|
393
|
-
const leading = LEADING_WS_RE.exec(raw);
|
|
394
|
-
const trailing = TRAILING_WS_RE.exec(raw);
|
|
395
|
-
let out = raw;
|
|
396
|
-
if (leading !== null && NEWLINE_RE.test(leading[0])) out = out.slice(leading[0].length);
|
|
397
|
-
if (trailing !== null && NEWLINE_RE.test(trailing[0])) out = out.slice(0, -trailing[0].length);
|
|
398
|
-
return out;
|
|
399
|
-
}
|
|
400
|
-
/** Satori 消息解析器状态(挂载点 / 文本缓冲 / 栈管理,拆分主循环复杂度)。 */
|
|
401
|
-
var ElementParser = class {
|
|
402
|
-
root = [];
|
|
403
|
-
stack = [];
|
|
404
|
-
text = "";
|
|
405
|
-
/** 当前挂载数组(栈顶元素 children 或根数组)。 */
|
|
406
|
-
mountPoint() {
|
|
407
|
-
const top = this.stack[this.stack.length - 1];
|
|
408
|
-
if (top === void 0) return this.root;
|
|
409
|
-
top.children ??= [];
|
|
410
|
-
return top.children;
|
|
411
|
-
}
|
|
412
|
-
/** 归一化文本落位到当前挂载点。 */
|
|
413
|
-
pushText() {
|
|
414
|
-
const normalized = normalizeText(this.text);
|
|
415
|
-
this.text = "";
|
|
416
|
-
if (normalized !== "") this.mountPoint().push({
|
|
417
|
-
type: "text",
|
|
418
|
-
text: normalized
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
/** 解析完成的元素树。 */
|
|
422
|
-
get elements() {
|
|
423
|
-
return this.root;
|
|
424
|
-
}
|
|
425
|
-
/** 追加原始文本(解析间隙累积)。 */
|
|
426
|
-
appendRaw(raw) {
|
|
427
|
-
this.text += raw;
|
|
428
|
-
}
|
|
429
|
-
/** 起始/自闭合标签落位(压栈或仅挂载)。 */
|
|
430
|
-
openTag(name, attrs, selfClosing) {
|
|
431
|
-
const el = {
|
|
432
|
-
type: name,
|
|
433
|
-
attrs
|
|
434
|
-
};
|
|
435
|
-
this.mountPoint().push(el);
|
|
436
|
-
if (!selfClosing) this.stack.push(el);
|
|
437
|
-
}
|
|
438
|
-
/** 收尾:剩余文本落位(栈剩余未闭合元素:文本落在栈顶元素内,宽容)。 */
|
|
439
|
-
flush() {
|
|
440
|
-
this.pushText();
|
|
441
|
-
}
|
|
442
|
-
/** 注释:返回跳过后的位置;-1 = 未闭合终止解析(注释不渲染)。 */
|
|
443
|
-
skipComment(source, pos) {
|
|
444
|
-
const end = source.indexOf("-->", pos);
|
|
445
|
-
return end === -1 ? -1 : end + 3;
|
|
446
|
-
}
|
|
447
|
-
/**
|
|
448
|
-
* 结束标签:匹配栈顶则闭合,否则按文本保留。
|
|
449
|
-
* @returns 下一个解析位置;-1 = 终止解析。
|
|
450
|
-
*/
|
|
451
|
-
closeTag(source, lt, slashPos) {
|
|
452
|
-
const { name, end } = readTagName(source, slashPos + 1);
|
|
453
|
-
if (name === "") {
|
|
454
|
-
this.text += "<";
|
|
455
|
-
return lt + 1;
|
|
456
|
-
}
|
|
457
|
-
const gt = source.indexOf(">", end);
|
|
458
|
-
if (gt === -1) {
|
|
459
|
-
this.text += source.slice(lt);
|
|
460
|
-
return -1;
|
|
461
|
-
}
|
|
462
|
-
const top = this.stack[this.stack.length - 1];
|
|
463
|
-
if (top !== void 0 && top.type === name) {
|
|
464
|
-
this.pushText();
|
|
465
|
-
this.stack.pop();
|
|
466
|
-
} else this.text += source.slice(lt, gt + 1);
|
|
467
|
-
return gt + 1;
|
|
468
|
-
}
|
|
469
|
-
};
|
|
470
|
-
/**
|
|
471
|
-
* 解析 Satori 消息字符串 → 元素树。
|
|
472
|
-
* 容错:未配对标签/非法标签按文本保留;解析不抛错(消息内容宽容)。
|
|
473
|
-
*/
|
|
474
|
-
function parseElements(source) {
|
|
475
|
-
const parser = new ElementParser();
|
|
476
|
-
let i = 0;
|
|
477
|
-
while (i < source.length) {
|
|
478
|
-
const lt = source.indexOf("<", i);
|
|
479
|
-
if (lt === -1) {
|
|
480
|
-
parser.appendRaw(source.slice(i));
|
|
481
|
-
break;
|
|
482
|
-
}
|
|
483
|
-
parser.appendRaw(source.slice(i, lt));
|
|
484
|
-
i = lt + 1;
|
|
485
|
-
if (source.startsWith("!--", i)) {
|
|
486
|
-
const next = parser.skipComment(source, i);
|
|
487
|
-
if (next === -1) break;
|
|
488
|
-
i = next;
|
|
489
|
-
continue;
|
|
490
|
-
}
|
|
491
|
-
if (source.startsWith("/", i)) {
|
|
492
|
-
const next = parser.closeTag(source, lt, i);
|
|
493
|
-
if (next === -1) break;
|
|
494
|
-
i = next;
|
|
495
|
-
continue;
|
|
496
|
-
}
|
|
497
|
-
const tag = readTag(source, i);
|
|
498
|
-
if (tag.name === "") {
|
|
499
|
-
parser.appendRaw("<");
|
|
500
|
-
continue;
|
|
501
|
-
}
|
|
502
|
-
parser.pushText();
|
|
503
|
-
parser.openTag(tag.name, tag.attrs, tag.selfClosing);
|
|
504
|
-
i = tag.end;
|
|
505
|
-
}
|
|
506
|
-
parser.flush();
|
|
507
|
-
return parser.elements;
|
|
508
|
-
}
|
|
509
|
-
/** 元素树 → Satori 消息字符串。 */
|
|
510
|
-
function renderElements(elements) {
|
|
511
|
-
let out = "";
|
|
512
|
-
for (const el of elements) {
|
|
513
|
-
if (el.type === "text") {
|
|
514
|
-
out += encodeText(el.text ?? "");
|
|
515
|
-
continue;
|
|
516
|
-
}
|
|
517
|
-
out += renderElement(el);
|
|
518
|
-
}
|
|
519
|
-
return out;
|
|
520
|
-
}
|
|
521
|
-
/** 渲染单个元素(含子元素递归)。 */
|
|
522
|
-
function renderElement(el) {
|
|
523
|
-
let attrs = "";
|
|
524
|
-
for (const [key, value] of Object.entries(el.attrs ?? {})) if (value === "") attrs += ` ${key}`;
|
|
525
|
-
else attrs += ` ${key}="${encodeAttr(value)}"`;
|
|
526
|
-
const children = el.children ?? [];
|
|
527
|
-
if (children.length === 0) return `<${el.type}${attrs}/>`;
|
|
528
|
-
const inner = renderElements(children);
|
|
529
|
-
return `<${el.type}${attrs}>${inner}</${el.type}>`;
|
|
530
|
-
}
|
|
531
|
-
// src/satori/helper/translate.ts
|
|
532
|
-
/**
|
|
533
|
-
* Satori 资源翻译(RawMessage → Satori Message / 频道 / 群组,纯函数 + 注入转换)
|
|
534
|
-
*
|
|
535
|
-
* 收方向 ID 空间:canonical at.target 是 uid,Satori 规范是 uin——经
|
|
536
|
-
* uidToUin 批量转换(依赖注入,与 OB11 的 ReceiveTranslateContext 同思路)。
|
|
537
|
-
*/
|
|
538
|
-
/** RawMessage → Satori Message(资源提升:message 不嵌套 user/member/channel)。 */
|
|
539
|
-
async function toSatoriMessage(raw, deps) {
|
|
540
|
-
const elementDeps = {};
|
|
541
|
-
if (deps.uidToUin !== void 0) elementDeps.uidToUin = deps.uidToUin;
|
|
542
|
-
const content = renderElements(await canonicalToSatoriElements(toCanonicalElements(raw), elementDeps));
|
|
543
|
-
const isGroup = raw.chatType === ChatType.GROUP;
|
|
544
|
-
const peerUin = String(raw.peerUin ?? "");
|
|
545
|
-
const senderUin = String(raw.senderUin ?? "");
|
|
546
|
-
const nickname = raw.sendNickName ?? "";
|
|
547
|
-
const memberName = raw.sendMemberName ?? "";
|
|
548
|
-
const message = { id: raw.msgId };
|
|
549
|
-
if (content !== "") message.content = content;
|
|
550
|
-
if (isGroup) {
|
|
551
|
-
message.guild = toGuild(peerUin, raw.peerName);
|
|
552
|
-
message.channel = toGroupChannel(peerUin, raw.peerName);
|
|
553
|
-
message.user = toUser(senderUin, nickname);
|
|
554
|
-
message.member = { user: message.user };
|
|
555
|
-
if (memberName !== "" && memberName !== nickname) message.member.nick = memberName;
|
|
556
|
-
} else {
|
|
557
|
-
const peerId = senderUin !== "" ? senderUin : peerUin;
|
|
558
|
-
message.channel = toDirectChannel(peerId, nickname);
|
|
559
|
-
message.user = toUser(peerId, nickname);
|
|
560
|
-
}
|
|
561
|
-
const created = toUnixSeconds(raw.msgTime) * MS_TO_SEC;
|
|
562
|
-
if (created > 0) message.created_at = created;
|
|
563
|
-
return message;
|
|
564
|
-
}
|
|
565
|
-
/** 按频道 id 构造频道(isGroup 由调用方从事件/缓存得知)。 */
|
|
566
|
-
function toChannelById(channelId, isGroup, name) {
|
|
567
|
-
if (isGroup) return toGroupChannel(channelId, name);
|
|
568
|
-
return toDirectChannel(channelId, name);
|
|
569
|
-
}
|
|
570
|
-
// src/satori/action/base-action.ts
|
|
571
|
-
/** 动作错误(带 HTTP 状态码;由 transport 层透传)。 */
|
|
572
|
-
var SatoriActionError = class extends Error {
|
|
573
|
-
status;
|
|
574
|
-
constructor(status, message) {
|
|
575
|
-
super(message);
|
|
576
|
-
this.status = status;
|
|
577
|
-
}
|
|
578
|
-
};
|
|
579
|
-
/** 校验失败默认状态码。 */
|
|
580
|
-
const INVALID_PARAM_STATUS = 400;
|
|
581
|
-
/** 声明式动作工厂:闭包依赖 + 统一校验与错误映射。 */
|
|
582
|
-
function defineSatoriAction(spec) {
|
|
583
|
-
return {
|
|
584
|
-
name: spec.name,
|
|
585
|
-
schema: spec.schema,
|
|
586
|
-
run: async (payload) => {
|
|
587
|
-
const outcome = await runAction({
|
|
588
|
-
schema: spec.schema,
|
|
589
|
-
handle: spec.handle,
|
|
590
|
-
errorCodeMap: satoriHttpStatusMap,
|
|
591
|
-
invalidRetcode: INVALID_PARAM_STATUS,
|
|
592
|
-
passthrough: (err) => err instanceof SatoriActionError
|
|
593
|
-
}, payload);
|
|
594
|
-
if (!outcome.ok) throw new SatoriActionError(outcome.retcode, outcome.message);
|
|
595
|
-
return outcome.data;
|
|
596
|
-
}
|
|
597
|
-
};
|
|
598
|
-
}
|
|
599
|
-
// src/satori/action/channel.ts
|
|
600
|
-
/**
|
|
601
|
-
* Satori 频道动作:channel.get / channel.list
|
|
602
|
-
* (channel.create / update / delete / mute:QQ 不支持 → 501,见 registry)
|
|
603
|
-
*/
|
|
604
|
-
/** 频道动作工厂(channel.get / channel.list)。 */
|
|
605
|
-
function createChannelActions(deps) {
|
|
606
|
-
return [defineSatoriAction({
|
|
607
|
-
name: "channel.get",
|
|
608
|
-
schema: z.object({ channel_id: z.string() }),
|
|
609
|
-
handle: async (payload) => {
|
|
610
|
-
const { channel_id: channelId } = payload;
|
|
611
|
-
const isGroup = (await deps.resolvePeer(channelId)).chatType === 2;
|
|
612
|
-
deps.setChannelType(channelId, isGroup);
|
|
613
|
-
if (isGroup) return toChannelById(channelId, true, (await deps.groupCache?.getGroupDetail(channelId))?.groupName);
|
|
614
|
-
return toChannelById(channelId, false);
|
|
615
|
-
}
|
|
616
|
-
}), defineSatoriAction({
|
|
617
|
-
name: "channel.list",
|
|
618
|
-
schema: z.object({
|
|
619
|
-
guild_id: z.string(),
|
|
620
|
-
next: z.string().optional()
|
|
621
|
-
}),
|
|
622
|
-
handle: async (payload) => {
|
|
623
|
-
const { guild_id: guildId } = payload;
|
|
624
|
-
const name = (await deps.groupCache?.getGroupDetail(guildId))?.groupName;
|
|
625
|
-
return { data: [toChannelById(guildId, true, name)] };
|
|
626
|
-
}
|
|
627
|
-
})];
|
|
628
|
-
}
|
|
629
|
-
// src/satori/action/friend.ts
|
|
630
|
-
/**
|
|
631
|
-
* Satori 好友动作:friend.list / friend.delete / friend.approve
|
|
632
|
-
*/
|
|
633
|
-
/** 好友动作工厂(friend.list / friend.delete / friend.approve)。 */
|
|
634
|
-
function createFriendActions(deps) {
|
|
635
|
-
return [
|
|
636
|
-
defineSatoriAction({
|
|
637
|
-
name: "friend.list",
|
|
638
|
-
schema: z.object({ next: z.string().optional() }),
|
|
639
|
-
handle: async () => {
|
|
640
|
-
return { data: (await deps.friendApi.getFriendList()).map((f) => {
|
|
641
|
-
const out = { user: toUser(f.uin, f.nickname) };
|
|
642
|
-
if (f.remark !== "" && f.remark !== f.nickname) out.nick = f.remark;
|
|
643
|
-
return out;
|
|
644
|
-
}) };
|
|
645
|
-
}
|
|
646
|
-
}),
|
|
647
|
-
defineSatoriAction({
|
|
648
|
-
name: "friend.delete",
|
|
649
|
-
schema: z.object({ user_id: z.string() }),
|
|
650
|
-
handle: async (payload) => {
|
|
651
|
-
const { user_id: userId } = payload;
|
|
652
|
-
const uid = (await deps.uinToUid([userId])).get(userId) ?? userId;
|
|
653
|
-
await deps.friendApi.deleteFriend(uid);
|
|
654
|
-
}
|
|
655
|
-
}),
|
|
656
|
-
defineSatoriAction({
|
|
657
|
-
name: "friend.approve",
|
|
658
|
-
schema: z.object({
|
|
659
|
-
message_id: z.string(),
|
|
660
|
-
approve: z.boolean(),
|
|
661
|
-
comment: z.string().optional()
|
|
662
|
-
}),
|
|
663
|
-
handle: async (payload) => {
|
|
664
|
-
const { message_id: messageId, approve } = payload;
|
|
665
|
-
const notify = await findBuddyReq(deps, messageId);
|
|
666
|
-
await deps.friendApi.handleFriendRequest(notify, approve);
|
|
667
|
-
}
|
|
668
|
-
})
|
|
669
|
-
];
|
|
670
|
-
}
|
|
671
|
-
/** 按 message_id(申请方 uin)查找好友申请。 */
|
|
672
|
-
async function findBuddyReq(deps, messageId) {
|
|
673
|
-
const reqs = await deps.friendApi.getBuddyReqList();
|
|
674
|
-
if (reqs.length === 0) throw kernelError("好友申请不存在", "NOT_FOUND");
|
|
675
|
-
const uinMap = await deps.uidToUin(reqs.map((r) => r.friendUid));
|
|
676
|
-
const found = reqs.find((req) => uinMap.get(req.friendUid) === messageId);
|
|
677
|
-
if (found === void 0) throw kernelError("好友申请不存在", "NOT_FOUND");
|
|
678
|
-
return found;
|
|
679
|
-
}
|
|
680
|
-
// src/satori/action/guild.ts
|
|
681
|
-
/** 成员列表默认条数。 */
|
|
682
|
-
const DEFAULT_MEMBER_LIMIT = 100;
|
|
683
|
-
/** guild.approve / guild.member.approve 共入参(处理群邀请与加群申请)。 */
|
|
684
|
-
const groupApproveSchema = z.object({
|
|
685
|
-
message_id: z.string(),
|
|
686
|
-
approve: z.boolean(),
|
|
687
|
-
comment: z.string().optional()
|
|
688
|
-
});
|
|
689
|
-
/** 群组动作工厂(guild.get / list / approve + member 五动作)。 */
|
|
690
|
-
function createGuildActions(deps) {
|
|
691
|
-
return [
|
|
692
|
-
defineSatoriAction({
|
|
693
|
-
name: "guild.get",
|
|
694
|
-
schema: z.object({ guild_id: z.string() }),
|
|
695
|
-
handle: async (payload) => {
|
|
696
|
-
const { guild_id: guildId } = payload;
|
|
697
|
-
const detail = await deps.groupApi.getGroupInfo(guildId);
|
|
698
|
-
return toGuild(detail.groupCode, detail.groupName);
|
|
699
|
-
}
|
|
700
|
-
}),
|
|
701
|
-
defineSatoriAction({
|
|
702
|
-
name: "guild.list",
|
|
703
|
-
schema: z.object({ next: z.string().optional() }),
|
|
704
|
-
handle: async () => {
|
|
705
|
-
return { data: (deps.groupCache === void 0 ? await deps.groupApi.getGroupList(true) : await deps.groupCache.listGroupsRefreshed()).map((g) => toGuild(g.groupCode, g.groupName)) };
|
|
706
|
-
}
|
|
707
|
-
}),
|
|
708
|
-
defineSatoriAction({
|
|
709
|
-
name: "guild.approve",
|
|
710
|
-
schema: groupApproveSchema,
|
|
711
|
-
handle: async (payload) => {
|
|
712
|
-
const { message_id: messageId, approve, comment } = payload;
|
|
713
|
-
await respondGroupRequest(deps, messageId, approve, comment);
|
|
714
|
-
}
|
|
715
|
-
}),
|
|
716
|
-
defineSatoriAction({
|
|
717
|
-
name: "guild.member.get",
|
|
718
|
-
schema: z.object({
|
|
719
|
-
guild_id: z.string(),
|
|
720
|
-
user_id: z.string()
|
|
721
|
-
}),
|
|
722
|
-
handle: async (payload) => {
|
|
723
|
-
const { guild_id: guildId, user_id: userId } = payload;
|
|
724
|
-
const uid = (await deps.uinToUid([userId])).get(userId) ?? userId;
|
|
725
|
-
const member = await deps.groupCache?.getMember(guildId, uid);
|
|
726
|
-
if (member !== void 0) return toGuildMember(member, userId);
|
|
727
|
-
const found = (await deps.groupApi.getGroupMemberInfo(guildId, [uid])).find((m) => m.uid === uid);
|
|
728
|
-
if (found === void 0) throw kernelError("群成员不存在", "NOT_FOUND");
|
|
729
|
-
return toGuildMember(found, userId);
|
|
730
|
-
}
|
|
731
|
-
}),
|
|
732
|
-
defineSatoriAction({
|
|
733
|
-
name: "guild.member.list",
|
|
734
|
-
schema: z.object({
|
|
735
|
-
guild_id: z.string(),
|
|
736
|
-
next: z.string().optional()
|
|
737
|
-
}),
|
|
738
|
-
handle: async (payload) => {
|
|
739
|
-
const { guild_id: guildId } = payload;
|
|
740
|
-
let members;
|
|
741
|
-
const cached = await deps.groupCache?.getMembers(guildId);
|
|
742
|
-
if (cached !== void 0 && cached.length > 0) members = cached;
|
|
743
|
-
else members = await deps.groupApi.getGroupMemberList(guildId, true);
|
|
744
|
-
const limited = members.slice(0, DEFAULT_MEMBER_LIMIT);
|
|
745
|
-
const uids = limited.map((m) => m.uid);
|
|
746
|
-
const uinMap = await deps.uidToUin(uids);
|
|
747
|
-
const out = { data: limited.map((m) => toGuildMember(m, uinMap.get(m.uid) ?? m.uin)) };
|
|
748
|
-
if (members.length > DEFAULT_MEMBER_LIMIT) out.next = String(DEFAULT_MEMBER_LIMIT);
|
|
749
|
-
return out;
|
|
750
|
-
}
|
|
751
|
-
}),
|
|
752
|
-
defineSatoriAction({
|
|
753
|
-
name: "guild.member.kick",
|
|
754
|
-
schema: z.object({
|
|
755
|
-
guild_id: z.string(),
|
|
756
|
-
user_id: z.string(),
|
|
757
|
-
/** 是否永久踢出(无法再次加入)。 */
|
|
758
|
-
permanent: z.boolean().optional()
|
|
759
|
-
}),
|
|
760
|
-
handle: async (payload) => {
|
|
761
|
-
const { guild_id: guildId, user_id: userId, permanent } = payload;
|
|
762
|
-
const uid = (await deps.uinToUid([userId])).get(userId) ?? userId;
|
|
763
|
-
await deps.groupApi.kickMember(guildId, [uid], permanent === true);
|
|
764
|
-
}
|
|
765
|
-
}),
|
|
766
|
-
defineSatoriAction({
|
|
767
|
-
name: "guild.member.mute",
|
|
768
|
-
schema: z.object({
|
|
769
|
-
guild_id: z.string(),
|
|
770
|
-
user_id: z.string(),
|
|
771
|
-
/** 禁言时长(毫秒,Satori 规范);0 解除禁言。 */
|
|
772
|
-
duration: z.number()
|
|
773
|
-
}),
|
|
774
|
-
handle: async (payload) => {
|
|
775
|
-
const { guild_id: guildId, user_id: userId, duration } = payload;
|
|
776
|
-
const uid = (await deps.uinToUid([userId])).get(userId) ?? userId;
|
|
777
|
-
await deps.groupApi.setMemberShutUp(guildId, [{
|
|
778
|
-
uid,
|
|
779
|
-
duration: msToSeconds(duration)
|
|
780
|
-
}]);
|
|
781
|
-
}
|
|
782
|
-
}),
|
|
783
|
-
defineSatoriAction({
|
|
784
|
-
name: "guild.member.approve",
|
|
785
|
-
schema: groupApproveSchema,
|
|
786
|
-
handle: async (payload) => {
|
|
787
|
-
const { message_id: messageId, approve, comment } = payload;
|
|
788
|
-
await respondGroupRequest(deps, messageId, approve, comment);
|
|
789
|
-
}
|
|
790
|
-
})
|
|
791
|
-
];
|
|
792
|
-
}
|
|
793
|
-
/** 处理群请求(guild.approve / guild.member.approve 共用实现)。 */
|
|
794
|
-
async function respondGroupRequest(deps, messageId, approve, comment) {
|
|
795
|
-
const notify = await findGroupNotify(deps, messageId);
|
|
796
|
-
const operateType = approve ? NTGroupRequestOperateTypes.KAGREE : NTGroupRequestOperateTypes.KREFUSE;
|
|
797
|
-
await deps.groupNotifyApi.handleGroupRequest(false, notify, operateType, comment ?? "");
|
|
798
|
-
}
|
|
799
|
-
/** 毫秒 → 秒(QQ setMemberShutUp 单位;向下取整,0 解除禁言)。 */
|
|
800
|
-
function msToSeconds(ms) {
|
|
801
|
-
return Math.max(0, Math.floor(ms / 1e3));
|
|
802
|
-
}
|
|
803
|
-
/** 按 message_id(通知 seq)查找群通知(单屏非可疑快照)。 */
|
|
804
|
-
async function findGroupNotify(deps, messageId) {
|
|
805
|
-
const found = (await deps.groupNotifyApi.getSingleScreenNotifies(false, 50)).find((item) => item.seq === messageId);
|
|
806
|
-
if (found === void 0) throw kernelError("群通知不存在", "NOT_FOUND");
|
|
807
|
-
return found;
|
|
808
|
-
}
|
|
809
|
-
/** GroupMember → Satori GuildMember(user.id = uin;nick = 群名片优先)。 */
|
|
810
|
-
function toGuildMember(member, uin) {
|
|
811
|
-
const nick = member.cardName !== "" ? member.cardName : member.nick;
|
|
812
|
-
const out = { user: toUser(uin, member.nick) };
|
|
813
|
-
if (nick !== "" && nick !== member.nick) out.nick = nick;
|
|
814
|
-
return out;
|
|
815
|
-
}
|
|
816
|
-
// src/satori/action/login.ts
|
|
817
|
-
/**
|
|
818
|
-
* Satori 登录动作:login.get
|
|
819
|
-
*/
|
|
820
|
-
/** 登录动作工厂(login.get)。 */
|
|
821
|
-
function createLoginActions(deps) {
|
|
822
|
-
return [defineSatoriAction({
|
|
823
|
-
name: "login.get",
|
|
824
|
-
schema: z.object({}),
|
|
825
|
-
handle: async () => toLogin(deps.self, 0, true)
|
|
826
|
-
})];
|
|
827
|
-
}
|
|
828
|
-
// src/satori/helper/element/asset.ts
|
|
829
|
-
/**
|
|
830
|
-
* Satori 发方向资源处理(从 element-convert.ts 拆分,FTA 优化)
|
|
831
|
-
*
|
|
832
|
-
* - resolveAsset:internal: 路径 / http(s) 下载 / 本地路径原样
|
|
833
|
-
* - ensureSilk 已上移到 adapter core(core/media.ts,onebot11/satori 共用)
|
|
834
|
-
* - 下载落盘统一走 shared/media.ts 的 downloadBufferToFile(异步 I/O 单一入口)
|
|
835
|
-
*/
|
|
836
|
-
/** internal:{platform}/{user.id}/{path} → 本地路径(取 _tmp 之后的路径,回落 cacheDir)。 */
|
|
837
|
-
function resolveInternalPath(src) {
|
|
838
|
-
const pathPart = src.slice(9);
|
|
839
|
-
const slash1 = pathPart.indexOf("/");
|
|
840
|
-
const slash2 = slash1 === -1 ? -1 : pathPart.indexOf("/", slash1 + 1);
|
|
841
|
-
if (slash2 === -1) return pathPart;
|
|
842
|
-
const rest = pathPart.slice(slash2 + 1);
|
|
843
|
-
const tmpIdx = rest.indexOf("_tmp/");
|
|
844
|
-
return tmpIdx === -1 ? rest : rest.slice(tmpIdx + 5);
|
|
845
|
-
}
|
|
846
|
-
/** http(s) URL 前缀。 */
|
|
847
|
-
const HTTP_URL_RE = /^https?:\/\//i;
|
|
848
|
-
/** 文件名不安全字符(URL 尾部段转文件名时替换)。 */
|
|
849
|
-
const UNSAFE_NAME_RE = /[^a-zA-Z0-9._-]/g;
|
|
850
|
-
/**
|
|
851
|
-
* 解析资源 src:
|
|
852
|
-
* - internal:{platform}/{user.id}/{path} → 本地路径(第一版仅支持 _tmp 保留路径)
|
|
853
|
-
* - http(s):// → 下载到 cacheDir
|
|
854
|
-
* - 其他 → 视为本地路径原样返回
|
|
855
|
-
*/
|
|
856
|
-
async function resolveAsset(src, cacheDir) {
|
|
857
|
-
if (src.startsWith("internal:")) return resolveInternalPath(src);
|
|
858
|
-
if (HTTP_URL_RE.test(src)) return downloadAsset(src, cacheDir);
|
|
859
|
-
return src;
|
|
860
|
-
}
|
|
861
|
-
/** 下载 URL 资源到缓存目录(返回本地路径)。 */
|
|
862
|
-
async function downloadAsset(url, cacheDir) {
|
|
863
|
-
const safeName = (url.split("/").pop() ?? `asset-${Date.now()}`).replace(UNSAFE_NAME_RE, "_") || `asset-${Date.now()}`;
|
|
864
|
-
return await downloadBufferToFile(url, join(cacheDir, safeName), void 0, "下载资源失败");
|
|
865
|
-
}
|
|
866
|
-
// src/satori/helper/element/media-convert.ts
|
|
867
|
-
/** 解析元素媒体 src(空 → null 忽略),返回本地路径。 */
|
|
868
|
-
async function resolveMediaSrc(el, deps) {
|
|
869
|
-
const src = el.attrs?.["src"] ?? "";
|
|
870
|
-
if (src === "") return null;
|
|
871
|
-
return resolveAsset(src, deps.cacheDir);
|
|
872
|
-
}
|
|
873
|
-
/** Satori img → canonical image。 */
|
|
874
|
-
async function imgToCanonical(el, deps) {
|
|
875
|
-
const path = await resolveMediaSrc(el, deps);
|
|
876
|
-
return path === null ? [] : [{
|
|
877
|
-
type: "image",
|
|
878
|
-
path
|
|
879
|
-
}];
|
|
880
|
-
}
|
|
881
|
-
/** Satori audio → canonical voice(非 silk 转码)。 */
|
|
882
|
-
async function audioToCanonical(el, deps) {
|
|
883
|
-
const path = await resolveMediaSrc(el, deps);
|
|
884
|
-
return path === null ? [] : [{
|
|
885
|
-
type: "voice",
|
|
886
|
-
path: await ensureSilk(path)
|
|
887
|
-
}];
|
|
888
|
-
}
|
|
889
|
-
/**
|
|
890
|
-
* Satori video → canonical video(非 mp4 输入 ffmpeg 归一化,fail-soft)。
|
|
891
|
-
*
|
|
892
|
-
* QQ 视频协议固定 mp4/H.264;非 mp4(webm/mov 等)先转码,ffmpeg 缺失或
|
|
893
|
-
* 转码失败原样透传(kernel 发送侧兜底,可能发送失败但不阻断装配)。
|
|
894
|
-
*/
|
|
895
|
-
async function videoToCanonical(el, deps) {
|
|
896
|
-
const path = await resolveMediaSrc(el, deps);
|
|
897
|
-
if (path === null) return [];
|
|
898
|
-
if (path.toLowerCase().endsWith(".mp4")) return [{
|
|
899
|
-
type: "video",
|
|
900
|
-
path
|
|
901
|
-
}];
|
|
902
|
-
try {
|
|
903
|
-
return [{
|
|
904
|
-
type: "video",
|
|
905
|
-
path: await transcodeVideo(path)
|
|
906
|
-
}];
|
|
907
|
-
} catch {
|
|
908
|
-
return [{
|
|
909
|
-
type: "video",
|
|
910
|
-
path
|
|
911
|
-
}];
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
/** Satori file → canonical file(title → name)。 */
|
|
915
|
-
async function fileToCanonical(el, deps) {
|
|
916
|
-
const path = await resolveMediaSrc(el, deps);
|
|
917
|
-
if (path === null) return [];
|
|
918
|
-
const title = el.attrs?.["title"];
|
|
919
|
-
const base = {
|
|
920
|
-
type: "file",
|
|
921
|
-
path
|
|
922
|
-
};
|
|
923
|
-
if (title !== void 0 && title !== "") return [{
|
|
924
|
-
...base,
|
|
925
|
-
name: title
|
|
926
|
-
}];
|
|
927
|
-
return [base];
|
|
928
|
-
}
|
|
929
|
-
/** 媒体元素转换器子表(并入 element-convert 主表)。 */
|
|
930
|
-
const MEDIA_CONVERTERS = {
|
|
931
|
-
img: imgToCanonical,
|
|
932
|
-
audio: audioToCanonical,
|
|
933
|
-
video: videoToCanonical,
|
|
934
|
-
file: fileToCanonical
|
|
935
|
-
};
|
|
936
|
-
// src/satori/helper/element/element-convert.ts
|
|
937
|
-
/** 发方向:Satori 元素树 → kernel canonical(at uin→uid + 资源下载/转码)。 */
|
|
938
|
-
async function satoriToCanonicalElements(elements, deps) {
|
|
939
|
-
const atUins = [];
|
|
940
|
-
const collectAt = (list) => {
|
|
941
|
-
for (const el of list) {
|
|
942
|
-
const id = el.attrs?.["id"];
|
|
943
|
-
if (el.type === "at" && id !== void 0 && id !== "all") atUins.push(id);
|
|
944
|
-
if (el.children !== void 0) collectAt(el.children);
|
|
945
|
-
}
|
|
946
|
-
};
|
|
947
|
-
collectAt(elements);
|
|
948
|
-
let uinToUidMap = /* @__PURE__ */ new Map();
|
|
949
|
-
if (atUins.length > 0) try {
|
|
950
|
-
uinToUidMap = await deps.uinToUid([...new Set(atUins)]);
|
|
951
|
-
} catch {}
|
|
952
|
-
const out = [];
|
|
953
|
-
for (const el of elements) out.push(...await elementToCanonical(el, deps, uinToUidMap));
|
|
954
|
-
return out;
|
|
955
|
-
}
|
|
956
|
-
/** Satori at → canonical at(uin → uid,name → display)。 */
|
|
957
|
-
function atToCanonical(el, uinToUidMap) {
|
|
958
|
-
const id = el.attrs?.["id"] ?? "";
|
|
959
|
-
if (id === "all") return [{
|
|
960
|
-
type: "at",
|
|
961
|
-
target: "all"
|
|
962
|
-
}];
|
|
963
|
-
if (id === "") return [];
|
|
964
|
-
const target = uinToUidMap.get(id) ?? id;
|
|
965
|
-
const display = el.attrs?.["name"];
|
|
966
|
-
const base = {
|
|
967
|
-
type: "at",
|
|
968
|
-
target
|
|
969
|
-
};
|
|
970
|
-
if (display !== void 0 && display !== "") return [{
|
|
971
|
-
...base,
|
|
972
|
-
display
|
|
973
|
-
}];
|
|
974
|
-
return [base];
|
|
975
|
-
}
|
|
976
|
-
/** Satori message:forward 属性 → canonical forward;否则展开子元素。 */
|
|
977
|
-
async function messageToCanonical(el, deps, uinToUidMap) {
|
|
978
|
-
if (el.attrs?.["forward"] !== void 0) {
|
|
979
|
-
const id = el.attrs?.["id"] ?? "";
|
|
980
|
-
return id === "" ? [] : [{
|
|
981
|
-
type: "forward",
|
|
982
|
-
messageIds: [id]
|
|
983
|
-
}];
|
|
984
|
-
}
|
|
985
|
-
return flattenChildren(el.children, deps, uinToUidMap);
|
|
986
|
-
}
|
|
987
|
-
/** 元素 type → 转换器(未知/修饰/排版类型走 fallback,不在此表)。 */
|
|
988
|
-
const ELEMENT_CONVERTERS = {
|
|
989
|
-
text: async (el) => {
|
|
990
|
-
const text = el.text ?? "";
|
|
991
|
-
return text === "" ? [] : [{
|
|
992
|
-
type: "text",
|
|
993
|
-
text
|
|
994
|
-
}];
|
|
995
|
-
},
|
|
996
|
-
at: async (el, _deps, uinToUidMap) => atToCanonical(el, uinToUidMap),
|
|
997
|
-
emoji: async (el) => {
|
|
998
|
-
const id = el.attrs?.["id"] ?? "";
|
|
999
|
-
return id === "" ? [] : [{
|
|
1000
|
-
type: "face",
|
|
1001
|
-
id
|
|
1002
|
-
}];
|
|
1003
|
-
},
|
|
1004
|
-
quote: async (el) => {
|
|
1005
|
-
const id = el.attrs?.["id"] ?? "";
|
|
1006
|
-
return id === "" ? [] : [{
|
|
1007
|
-
type: "reply",
|
|
1008
|
-
messageId: id
|
|
1009
|
-
}];
|
|
1010
|
-
},
|
|
1011
|
-
message: async (el, deps, uinToUidMap) => messageToCanonical(el, deps, uinToUidMap),
|
|
1012
|
-
br: async () => [{
|
|
1013
|
-
type: "text",
|
|
1014
|
-
text: "\n"
|
|
1015
|
-
}],
|
|
1016
|
-
author: async () => [],
|
|
1017
|
-
sharp: async () => [],
|
|
1018
|
-
a: async () => [],
|
|
1019
|
-
button: async () => [],
|
|
1020
|
-
...MEDIA_CONVERTERS
|
|
1021
|
-
};
|
|
1022
|
-
/** 单个元素 → canonical(修饰/排版元素展开子元素)。 */
|
|
1023
|
-
async function elementToCanonical(el, deps, uinToUidMap) {
|
|
1024
|
-
const converter = ELEMENT_CONVERTERS[el.type];
|
|
1025
|
-
if (converter !== void 0) return converter(el, deps, uinToUidMap);
|
|
1026
|
-
return flattenChildren(el.children, deps, uinToUidMap);
|
|
1027
|
-
}
|
|
1028
|
-
/** 展开子元素为 canonical(递归)。 */
|
|
1029
|
-
async function flattenChildren(children, deps, uinToUidMap) {
|
|
1030
|
-
const out = [];
|
|
1031
|
-
for (const child of children ?? []) out.push(...await elementToCanonical(child, deps, uinToUidMap));
|
|
1032
|
-
return out;
|
|
1033
|
-
}
|
|
1034
|
-
/** 解析消息 content 字符串 → canonical(message.create 用)。 */
|
|
1035
|
-
async function parseContentToCanonical(content, deps) {
|
|
1036
|
-
return satoriToCanonicalElements(parseElements(content), deps);
|
|
1037
|
-
}
|
|
1038
|
-
// src/satori/action/message.ts
|
|
1039
|
-
/**
|
|
1040
|
-
* Satori 消息动作:message.create / get / list / delete
|
|
1041
|
-
* (message.update:QQ 不支持编辑 → 501,见 registry)
|
|
1042
|
-
*/
|
|
1043
|
-
/** 消息列表默认条数(规范推荐 50)。 */
|
|
1044
|
-
const DEFAULT_LIST_LIMIT = 50;
|
|
1045
|
-
/** message.create 参数。 */
|
|
1046
|
-
const messageCreateSchema = z.object({
|
|
1047
|
-
channel_id: z.string(),
|
|
1048
|
-
content: z.string(),
|
|
1049
|
-
/** 被动请求来源(第一版透传忽略,QQ 不区分主动/被动)。 */
|
|
1050
|
-
referrer: z.unknown().optional()
|
|
1051
|
-
});
|
|
1052
|
-
/** 消息动作工厂(create / get / list / delete)。 */
|
|
1053
|
-
function createMessageActions(deps) {
|
|
1054
|
-
return [
|
|
1055
|
-
defineSatoriAction({
|
|
1056
|
-
name: "message.create",
|
|
1057
|
-
schema: messageCreateSchema,
|
|
1058
|
-
handle: async (payload) => {
|
|
1059
|
-
const { channel_id: channelId, content } = payload;
|
|
1060
|
-
const peer = await deps.resolvePeer(channelId);
|
|
1061
|
-
deps.setChannelType(channelId, peer.chatType === 2);
|
|
1062
|
-
const elements = await parseContentToCanonical(content, deps.toCanonicalDeps());
|
|
1063
|
-
const { msgId } = await deps.msgApi.sendMessage(peer, elements);
|
|
1064
|
-
const message = {
|
|
1065
|
-
id: msgId,
|
|
1066
|
-
channel: toChannelById(channelId, peer.chatType === 2)
|
|
1067
|
-
};
|
|
1068
|
-
message.content = content;
|
|
1069
|
-
return [message];
|
|
1070
|
-
}
|
|
1071
|
-
}),
|
|
1072
|
-
defineSatoriAction({
|
|
1073
|
-
name: "message.get",
|
|
1074
|
-
schema: z.object({
|
|
1075
|
-
channel_id: z.string(),
|
|
1076
|
-
message_id: z.string()
|
|
1077
|
-
}),
|
|
1078
|
-
handle: async (payload) => {
|
|
1079
|
-
const { channel_id: channelId, message_id: messageId } = payload;
|
|
1080
|
-
const peer = await deps.resolvePeer(channelId);
|
|
1081
|
-
const first = (await deps.msgApi.fetchMsgsByMsgId(peer, [messageId]))[0];
|
|
1082
|
-
if (first === void 0) throw kernelError("消息不存在", "NOT_FOUND");
|
|
1083
|
-
deps.setChannelType(channelId, peer.chatType === 2);
|
|
1084
|
-
return toSatoriMessage(first, {
|
|
1085
|
-
selfUin: deps.selfUin,
|
|
1086
|
-
uidToUin: (uids) => deps.uidToUin(uids)
|
|
1087
|
-
});
|
|
1088
|
-
}
|
|
1089
|
-
}),
|
|
1090
|
-
defineSatoriAction({
|
|
1091
|
-
name: "message.list",
|
|
1092
|
-
schema: z.object({
|
|
1093
|
-
channel_id: z.string(),
|
|
1094
|
-
/** 分页令牌(缺省从最新开始)。 */
|
|
1095
|
-
next: z.string().optional(),
|
|
1096
|
-
/** 查询方向(缺省 before)。 */
|
|
1097
|
-
direction: z.enum([
|
|
1098
|
-
"before",
|
|
1099
|
-
"after",
|
|
1100
|
-
"around"
|
|
1101
|
-
]).optional(),
|
|
1102
|
-
/** 消息数量限制(缺省平台默认,推荐 50)。 */
|
|
1103
|
-
limit: z.number().int().optional(),
|
|
1104
|
-
/** 排序(缺省 asc)。 */
|
|
1105
|
-
order: z.enum(["asc", "desc"]).optional()
|
|
1106
|
-
}),
|
|
1107
|
-
handle: async (payload) => {
|
|
1108
|
-
const { channel_id: channelId, next, direction, limit, order } = payload;
|
|
1109
|
-
if (direction === "around") throw kernelError("暂不支持 around 方向", "INVALID_PARAM");
|
|
1110
|
-
const peer = await deps.resolvePeer(channelId);
|
|
1111
|
-
const count = limit ?? DEFAULT_LIST_LIMIT;
|
|
1112
|
-
const raw = await deps.msgApi.fetchMessages(peer, {
|
|
1113
|
-
count,
|
|
1114
|
-
msgId: next ?? ""
|
|
1115
|
-
});
|
|
1116
|
-
deps.setChannelType(channelId, peer.chatType === 2);
|
|
1117
|
-
const ordered = order !== "desc" ? [...raw].reverse() : raw;
|
|
1118
|
-
const messages = [];
|
|
1119
|
-
for (const msg of ordered) messages.push(await toSatoriMessage(msg, {
|
|
1120
|
-
selfUin: deps.selfUin,
|
|
1121
|
-
uidToUin: (uids) => deps.uidToUin(uids)
|
|
1122
|
-
}));
|
|
1123
|
-
const last = messages[messages.length - 1];
|
|
1124
|
-
const out = { data: messages };
|
|
1125
|
-
if (last !== void 0 && raw.length >= count) out.next = last.id;
|
|
1126
|
-
return out;
|
|
1127
|
-
}
|
|
1128
|
-
}),
|
|
1129
|
-
defineSatoriAction({
|
|
1130
|
-
name: "message.delete",
|
|
1131
|
-
schema: z.object({
|
|
1132
|
-
channel_id: z.string(),
|
|
1133
|
-
message_id: z.string()
|
|
1134
|
-
}),
|
|
1135
|
-
handle: async (payload) => {
|
|
1136
|
-
const { channel_id: channelId, message_id: messageId } = payload;
|
|
1137
|
-
const peer = await deps.resolvePeer(channelId);
|
|
1138
|
-
await deps.msgApi.recallMessage(peer, [messageId]);
|
|
1139
|
-
}
|
|
1140
|
-
})
|
|
1141
|
-
];
|
|
1142
|
-
}
|
|
1143
|
-
// src/satori/action/reaction.ts
|
|
1144
|
-
/**
|
|
1145
|
-
* Satori 表态动作:reaction.create / reaction.delete
|
|
1146
|
-
* (reaction.clear / reaction.list:501,见 registry)
|
|
1147
|
-
*/
|
|
1148
|
-
/** 表情 ID 默认类型(QQ 表情 id 为 qq 表情索引)。 */
|
|
1149
|
-
const EMOJI_TYPE = "1";
|
|
1150
|
-
/** 表态动作工厂(reaction.create / reaction.delete)。 */
|
|
1151
|
-
function createReactionActions(deps) {
|
|
1152
|
-
return [defineSatoriAction({
|
|
1153
|
-
name: "reaction.create",
|
|
1154
|
-
schema: z.object({
|
|
1155
|
-
channel_id: z.string(),
|
|
1156
|
-
message_id: z.string(),
|
|
1157
|
-
emoji_id: z.string()
|
|
1158
|
-
}),
|
|
1159
|
-
handle: async (payload) => {
|
|
1160
|
-
const { channel_id: channelId, message_id: messageId, emoji_id: emojiId } = payload;
|
|
1161
|
-
const peer = await deps.resolvePeer(channelId);
|
|
1162
|
-
await deps.msgApi.setMsgEmojiLike(peer, {
|
|
1163
|
-
msgSeq: messageId,
|
|
1164
|
-
emojiId,
|
|
1165
|
-
emojiType: EMOJI_TYPE,
|
|
1166
|
-
like: true
|
|
1167
|
-
});
|
|
1168
|
-
}
|
|
1169
|
-
}), defineSatoriAction({
|
|
1170
|
-
name: "reaction.delete",
|
|
1171
|
-
schema: z.object({
|
|
1172
|
-
channel_id: z.string(),
|
|
1173
|
-
message_id: z.string(),
|
|
1174
|
-
emoji_id: z.string(),
|
|
1175
|
-
/** 删除指定用户的表态(缺省删除自己的)。 */
|
|
1176
|
-
user_id: z.string().optional()
|
|
1177
|
-
}),
|
|
1178
|
-
handle: async (payload) => {
|
|
1179
|
-
const { channel_id: channelId, message_id: messageId, emoji_id: emojiId, user_id: userId } = payload;
|
|
1180
|
-
if (userId !== void 0) throw kernelError("暂不支持删除他人表态", "INVALID_PARAM");
|
|
1181
|
-
const peer = await deps.resolvePeer(channelId);
|
|
1182
|
-
await deps.msgApi.setMsgEmojiLike(peer, {
|
|
1183
|
-
msgSeq: messageId,
|
|
1184
|
-
emojiId,
|
|
1185
|
-
emojiType: EMOJI_TYPE,
|
|
1186
|
-
like: false
|
|
1187
|
-
});
|
|
1188
|
-
}
|
|
1189
|
-
})];
|
|
1190
|
-
}
|
|
1191
|
-
// src/satori/action/user.ts
|
|
1192
|
-
/**
|
|
1193
|
-
* Satori 用户动作:user.get / user.channel.create
|
|
1194
|
-
*/
|
|
1195
|
-
/** 用户动作工厂(user.get / user.channel.create)。 */
|
|
1196
|
-
function createUserActions(deps) {
|
|
1197
|
-
return [defineSatoriAction({
|
|
1198
|
-
name: "user.get",
|
|
1199
|
-
schema: z.object({ user_id: z.string() }),
|
|
1200
|
-
handle: async (payload) => {
|
|
1201
|
-
const { user_id: userId } = payload;
|
|
1202
|
-
let nickname;
|
|
1203
|
-
try {
|
|
1204
|
-
const info = await deps.profileApi.getStrangerInfo(userId);
|
|
1205
|
-
nickname = info.nickname ?? info.long_nick ?? void 0;
|
|
1206
|
-
} catch {}
|
|
1207
|
-
return toUser(userId, nickname);
|
|
1208
|
-
}
|
|
1209
|
-
}), defineSatoriAction({
|
|
1210
|
-
name: "user.channel.create",
|
|
1211
|
-
schema: z.object({
|
|
1212
|
-
user_id: z.string(),
|
|
1213
|
-
guild_id: z.string().optional()
|
|
1214
|
-
}),
|
|
1215
|
-
handle: async (payload) => {
|
|
1216
|
-
const { user_id: userId } = payload;
|
|
1217
|
-
return toChannelById(userId, false);
|
|
1218
|
-
}
|
|
1219
|
-
})];
|
|
1220
|
-
}
|
|
1221
|
-
// src/satori/action/index.ts
|
|
1222
|
-
/**
|
|
1223
|
-
* Satori 动作注册表:动作依赖收敛为 { api: SatoriApi } 单聚合对象。
|
|
1224
|
-
*
|
|
1225
|
-
* 501 动作:规范「平台支持但适配器未实现 → 501」——QQ 平台不支持的
|
|
1226
|
-
* 能力(消息编辑/频道管理/角色管理等)统一注册同名占位动作。
|
|
1227
|
-
*/
|
|
1228
|
-
/** QQ 平台不支持的 Satori 标准动作(501)。 */
|
|
1229
|
-
const NOT_IMPLEMENTED = [
|
|
1230
|
-
"message.update",
|
|
1231
|
-
"channel.create",
|
|
1232
|
-
"channel.update",
|
|
1233
|
-
"channel.delete",
|
|
1234
|
-
"channel.mute",
|
|
1235
|
-
"guild.member.role.set",
|
|
1236
|
-
"guild.member.role.unset",
|
|
1237
|
-
"guild.role.list",
|
|
1238
|
-
"guild.role.create",
|
|
1239
|
-
"guild.role.update",
|
|
1240
|
-
"guild.role.delete",
|
|
1241
|
-
"reaction.clear",
|
|
1242
|
-
"reaction.list"
|
|
1243
|
-
];
|
|
1244
|
-
/** 创建 Satori 动作注册表。 */
|
|
1245
|
-
function createSatoriActionRegistry(deps) {
|
|
1246
|
-
const registry = new ActionRegistry();
|
|
1247
|
-
const { api } = deps;
|
|
1248
|
-
for (const action of [
|
|
1249
|
-
...createMessageActions(api),
|
|
1250
|
-
...createChannelActions(api),
|
|
1251
|
-
...createUserActions(api),
|
|
1252
|
-
...createGuildActions(api),
|
|
1253
|
-
...createFriendActions(api),
|
|
1254
|
-
...createLoginActions(api),
|
|
1255
|
-
...createReactionActions(api)
|
|
1256
|
-
]) registry.register(action);
|
|
1257
|
-
for (const name of NOT_IMPLEMENTED) registry.register(defineSatoriAction({
|
|
1258
|
-
name,
|
|
1259
|
-
schema: notImplementedSchema,
|
|
1260
|
-
handle: async () => {
|
|
1261
|
-
throw new SatoriActionError(501, `平台不支持该操作: ${name}`);
|
|
1262
|
-
}
|
|
1263
|
-
}));
|
|
1264
|
-
return registry;
|
|
1265
|
-
}
|
|
1266
|
-
/** 501 占位动作共用 schema(接受任意对象)。 */
|
|
1267
|
-
const notImplementedSchema = z.object({}).passthrough();
|
|
1268
|
-
// src/satori/api/satori-api.ts
|
|
1269
|
-
/** Satori 动作统一依赖聚合。 */
|
|
1270
|
-
var SatoriApi = class {
|
|
1271
|
-
msgApi;
|
|
1272
|
-
groupApi;
|
|
1273
|
-
groupNotifyApi;
|
|
1274
|
-
friendApi;
|
|
1275
|
-
profileApi;
|
|
1276
|
-
self;
|
|
1277
|
-
selfUin;
|
|
1278
|
-
cacheDir;
|
|
1279
|
-
groupCache;
|
|
1280
|
-
/**
|
|
1281
|
-
* 频道类型缓存:channel_id → isGroup(群聊频道 id 与私聊 uin 无法从数字
|
|
1282
|
-
* 规则区分,需从事件/查询回填——QQ 平台群号与用户 uin 同为数字)。
|
|
1283
|
-
*/
|
|
1284
|
-
channelTypeCache = /* @__PURE__ */ new Map();
|
|
1285
|
-
constructor(opts) {
|
|
1286
|
-
this.msgApi = opts.msgApi;
|
|
1287
|
-
this.groupApi = opts.groupApi;
|
|
1288
|
-
this.groupNotifyApi = opts.groupNotifyApi;
|
|
1289
|
-
this.friendApi = opts.friendApi;
|
|
1290
|
-
this.profileApi = opts.profileApi;
|
|
1291
|
-
this.self = opts.self;
|
|
1292
|
-
this.selfUin = opts.self.uin;
|
|
1293
|
-
this.cacheDir = opts.cacheDir;
|
|
1294
|
-
this.groupCache = opts.groupCache;
|
|
1295
|
-
}
|
|
1296
|
-
/** 记录频道类型(消息事件翻译时回填:chatType GROUP → true)。 */
|
|
1297
|
-
setChannelType(channelId, isGroup) {
|
|
1298
|
-
this.channelTypeCache.set(channelId, isGroup);
|
|
1299
|
-
}
|
|
1300
|
-
/**
|
|
1301
|
-
* 解析 channel_id → Peer(消息动作共用)。
|
|
1302
|
-
* 缓存命中直接取类型;未命中先试群号(getGroupInfo 成功 = 群聊),
|
|
1303
|
-
* 失败按私聊(uin → uid)。
|
|
1304
|
-
*/
|
|
1305
|
-
async resolvePeer(channelId) {
|
|
1306
|
-
const cached = this.channelTypeCache.get(channelId);
|
|
1307
|
-
if (cached === true) return {
|
|
1308
|
-
chatType: ChatType.GROUP,
|
|
1309
|
-
peerUid: channelId
|
|
1310
|
-
};
|
|
1311
|
-
if (cached === false) return this.toC2cPeer(channelId);
|
|
1312
|
-
const isGroup = await this.groupApi.getGroupInfo(channelId).then(() => true).catch(() => false);
|
|
1313
|
-
this.channelTypeCache.set(channelId, isGroup);
|
|
1314
|
-
if (isGroup) return {
|
|
1315
|
-
chatType: ChatType.GROUP,
|
|
1316
|
-
peerUid: channelId
|
|
1317
|
-
};
|
|
1318
|
-
return this.toC2cPeer(channelId);
|
|
1319
|
-
}
|
|
1320
|
-
/** uin → 私聊 Peer(uin→uid 转换;转换失败原样 uin 兜底)。 */
|
|
1321
|
-
async toC2cPeer(uin) {
|
|
1322
|
-
const uid = (await this.groupApi.uinToUid([uin])).get(uin) ?? uin;
|
|
1323
|
-
return {
|
|
1324
|
-
chatType: ChatType.C2C,
|
|
1325
|
-
peerUid: uid
|
|
1326
|
-
};
|
|
1327
|
-
}
|
|
1328
|
-
/** uin → uid(委托 groupApi;私聊发送 / at 目标用)。 */
|
|
1329
|
-
uinToUid(uins) {
|
|
1330
|
-
return this.groupApi.uinToUid(uins);
|
|
1331
|
-
}
|
|
1332
|
-
/** uid → uin(委托 groupApi;收方向 at / 用户 ID 用)。 */
|
|
1333
|
-
uidToUin(uids) {
|
|
1334
|
-
return this.groupApi.uidToUin(uids);
|
|
1335
|
-
}
|
|
1336
|
-
/** 发方向元素转换依赖(at uin→uid + 资源下载目录)。 */
|
|
1337
|
-
toCanonicalDeps() {
|
|
1338
|
-
return {
|
|
1339
|
-
uinToUid: (uins) => this.groupApi.uinToUid(uins),
|
|
1340
|
-
cacheDir: this.cacheDir
|
|
1341
|
-
};
|
|
1342
|
-
}
|
|
1343
|
-
};
|
|
1344
|
-
// src/satori/event/translate.ts
|
|
1345
|
-
/**
|
|
1346
|
-
* Satori 事件翻译(kernel RawMessage → Satori Event 内容,纯函数 + 注入转换)
|
|
1347
|
-
*
|
|
1348
|
-
* 第一版支持:
|
|
1349
|
-
* - message-created:普通消息(Msg/onRecvMsg)
|
|
1350
|
-
* - message-deleted:grayTip REVOKE(撤回)
|
|
1351
|
-
* - guild-member-added / guild-member-removed:grayTip GROUP(TipGroupElement)
|
|
1352
|
-
* (friend-request / guild-request 事件留待后续:grayTip 的 jsonGrayTipElement
|
|
1353
|
-
* 结构待运行时探测,OB11 走主动查询 get_group_system_msg 不广播。)
|
|
1354
|
-
*
|
|
1355
|
-
* 收方向 ID 空间:uid → uin 批量转换(deps.uidToUin,与消息翻译同依赖)。
|
|
1356
|
-
*/
|
|
1357
|
-
/** 普通消息 → message-created 事件。 */
|
|
1358
|
-
async function toSatoriMessageEvent(raw, deps) {
|
|
1359
|
-
const message = await toSatoriMessage(raw, deps);
|
|
1360
|
-
const content = {
|
|
1361
|
-
type: "message-created",
|
|
1362
|
-
timestamp: toUnixSeconds(raw.msgTime) * MS_TO_SEC
|
|
1363
|
-
};
|
|
1364
|
-
if (message.channel !== void 0) content.channel = message.channel;
|
|
1365
|
-
if (message.guild !== void 0) content.guild = message.guild;
|
|
1366
|
-
if (message.user !== void 0) content.user = message.user;
|
|
1367
|
-
if (message.member !== void 0) content.member = message.member;
|
|
1368
|
-
content.message = message;
|
|
1369
|
-
return content;
|
|
1370
|
-
}
|
|
1371
|
-
/** 检查消息是否含可翻译的 grayTip(撤回 / 群成员变动)。 */
|
|
1372
|
-
function hasSatoriGrayTip(raw) {
|
|
1373
|
-
for (const el of raw.elements) {
|
|
1374
|
-
const g = el.grayTipElement;
|
|
1375
|
-
if (g == null) continue;
|
|
1376
|
-
if (g.subElementType === GrayTipSubType.REVOKE || g.subElementType === GrayTipSubType.GROUP) return true;
|
|
1377
|
-
}
|
|
1378
|
-
return false;
|
|
1379
|
-
}
|
|
1380
|
-
/** 收集 grayTip 涉及的 uid(批量 uidToUin 用,core 共享实现)。 */
|
|
1381
|
-
const collectSatoriGrayTipUids = collectGrayTipUids;
|
|
1382
|
-
/** 判断消息是否为群聊 grayTip(peerUid 是群号)。 */
|
|
1383
|
-
function isGroupMessage(raw) {
|
|
1384
|
-
return raw.chatType === ChatType.GROUP;
|
|
1385
|
-
}
|
|
1386
|
-
/** grayTip → 事件(撤回 / 群成员变动;无匹配返回 null)。 */
|
|
1387
|
-
async function toSatoriGrayTipEvent(raw, uidToUinMap) {
|
|
1388
|
-
for (const el of raw.elements) {
|
|
1389
|
-
const g = el.grayTipElement;
|
|
1390
|
-
if (g == null) continue;
|
|
1391
|
-
if (g.subElementType === GrayTipSubType.REVOKE) {
|
|
1392
|
-
const revoked = toRevokeEvent(raw, uidToUinMap);
|
|
1393
|
-
if (revoked !== null) return revoked;
|
|
1394
|
-
}
|
|
1395
|
-
if (g.subElementType === GrayTipSubType.GROUP && isGroupMessage(raw)) {
|
|
1396
|
-
const member = toMemberChangeEvent(raw, uidToUinMap);
|
|
1397
|
-
if (member !== null) return member;
|
|
1398
|
-
}
|
|
1399
|
-
}
|
|
1400
|
-
return null;
|
|
1401
|
-
}
|
|
1402
|
-
/** 撤回 → message-deleted 事件。 */
|
|
1403
|
-
function toRevokeEvent(raw, uidToUinMap) {
|
|
1404
|
-
for (const el of raw.elements) {
|
|
1405
|
-
const revoke = el.grayTipElement?.revokeElement;
|
|
1406
|
-
if (revoke == null) continue;
|
|
1407
|
-
const operatorUin = toUinString(revoke.operatorUid, uidToUinMap);
|
|
1408
|
-
const peerUin = String(raw.peerUin ?? "");
|
|
1409
|
-
const content = {
|
|
1410
|
-
type: "message-deleted",
|
|
1411
|
-
timestamp: toUnixSeconds(raw.msgTime) * MS_TO_SEC
|
|
1412
|
-
};
|
|
1413
|
-
content.channel = {
|
|
1414
|
-
id: peerUin,
|
|
1415
|
-
type: isGroupMessage(raw) ? 0 : 1
|
|
1416
|
-
};
|
|
1417
|
-
if (isGroupMessage(raw)) content.guild = {
|
|
1418
|
-
id: peerUin,
|
|
1419
|
-
name: raw.peerName
|
|
1420
|
-
};
|
|
1421
|
-
content.message = { id: raw.msgSeq };
|
|
1422
|
-
content.operator = toUser(operatorUin, revoke.operatorNick);
|
|
1423
|
-
return content;
|
|
1424
|
-
}
|
|
1425
|
-
return null;
|
|
1426
|
-
}
|
|
1427
|
-
/** 群成员变动 → guild-member-added / guild-member-removed 事件。 */
|
|
1428
|
-
function toMemberChangeEvent(raw, uidToUinMap) {
|
|
1429
|
-
for (const el of raw.elements) {
|
|
1430
|
-
const grp = el.grayTipElement?.groupElement;
|
|
1431
|
-
if (grp == null || grp.type === void 0) continue;
|
|
1432
|
-
const type = memberChangeType(grp.type);
|
|
1433
|
-
if (type === null) return null;
|
|
1434
|
-
const memberUid = grp.memberUid ?? "";
|
|
1435
|
-
const adminUid = grp.adminUid ?? "";
|
|
1436
|
-
const peerUin = String(raw.peerUin ?? "");
|
|
1437
|
-
const content = {
|
|
1438
|
-
type,
|
|
1439
|
-
timestamp: toUnixSeconds(raw.msgTime) * MS_TO_SEC,
|
|
1440
|
-
guild: toGroupGuild(peerUin, raw.peerName),
|
|
1441
|
-
channel: {
|
|
1442
|
-
id: peerUin,
|
|
1443
|
-
type: 0
|
|
1444
|
-
},
|
|
1445
|
-
member: toMember(memberUid, grp.memberNick, grp.memberRemark, uidToUinMap),
|
|
1446
|
-
user: toUser(toUinString(memberUid, uidToUinMap), grp.memberNick)
|
|
1447
|
-
};
|
|
1448
|
-
if (adminUid !== "" && adminUid !== memberUid) content.operator = toUser(toUinString(adminUid, uidToUinMap), grp.adminNick);
|
|
1449
|
-
return content;
|
|
1450
|
-
}
|
|
1451
|
-
return null;
|
|
1452
|
-
}
|
|
1453
|
-
/** 成员变动类型(MEMBER_ADD=added / QUIT=removed;其余 null)。 */
|
|
1454
|
-
function memberChangeType(type) {
|
|
1455
|
-
if (type === TipGroupElementType.MEMBER_ADD) return "guild-member-added";
|
|
1456
|
-
if (type === TipGroupElementType.QUIT) return "guild-member-removed";
|
|
1457
|
-
return null;
|
|
1458
|
-
}
|
|
1459
|
-
/** 构造群组(带可选群名)。 */
|
|
1460
|
-
function toGroupGuild(id, name) {
|
|
1461
|
-
const guild = { id };
|
|
1462
|
-
if (name !== void 0 && name !== "") guild.name = name;
|
|
1463
|
-
return guild;
|
|
1464
|
-
}
|
|
1465
|
-
/** 构造群成员(user + 可选备注 nick)。 */
|
|
1466
|
-
function toMember(uid, nick, remark, uidToUinMap) {
|
|
1467
|
-
const member = { user: toUser(toUinString(uid, uidToUinMap), nick) };
|
|
1468
|
-
if (remark !== void 0 && remark !== "" && remark !== nick) member.nick = remark;
|
|
1469
|
-
return member;
|
|
1470
|
-
}
|
|
1471
|
-
// src/satori/types/event.ts
|
|
1472
|
-
/** 信令类型。 */
|
|
1473
|
-
const Opcode = {
|
|
1474
|
-
EVENT: 0,
|
|
1475
|
-
PING: 1,
|
|
1476
|
-
PONG: 2,
|
|
1477
|
-
IDENTIFY: 3,
|
|
1478
|
-
READY: 4,
|
|
1479
|
-
META: 5
|
|
1480
|
-
};
|
|
1481
|
-
// src/satori/transport.ts
|
|
1482
|
-
/** WS 事件服务路径(规范固定)。 */
|
|
1483
|
-
const WS_EVENTS_PATH = "/v1/events";
|
|
1484
|
-
/** HTTP RPC 版本前缀。 */
|
|
1485
|
-
const V1_PREFIX = "/v1/";
|
|
1486
|
-
/** Authorization 头前缀。 */
|
|
1487
|
-
const BEARER_PREFIX = "Bearer ";
|
|
1488
|
-
/** resource.method 路径模式(resource 与 method 均为字母开头的小写标识符,可含连字符/数字)。 */
|
|
1489
|
-
const ACTION_PATH_RE = /^([a-z][a-z0-9-]*)(?:\.[a-z][a-z0-9-]*)+$/i;
|
|
1490
|
-
/** 按配置装配 Satori 传输。 */
|
|
1491
|
-
function assembleSatoriTransports(opts) {
|
|
1492
|
-
const { config, broadcaster, registry, login } = opts;
|
|
1493
|
-
const adapters = [];
|
|
1494
|
-
for (const item of config.httpServers) {
|
|
1495
|
-
if (!item.enabled) continue;
|
|
1496
|
-
const token = item.token ?? config.token;
|
|
1497
|
-
const server = new HttpServer({
|
|
1498
|
-
host: item.host,
|
|
1499
|
-
port: item.port,
|
|
1500
|
-
...token !== void 0 && token !== "" ? { authorize: makeBearerAuthorize(token) } : {}
|
|
1501
|
-
});
|
|
1502
|
-
server.onPathRequest = (ctx, req, respond) => {
|
|
1503
|
-
handleHttpRequest(ctx, req, respond, registry, login);
|
|
1504
|
-
};
|
|
1505
|
-
adapters.push(server);
|
|
1506
|
-
}
|
|
1507
|
-
for (const item of config.wsServers) {
|
|
1508
|
-
if (!item.enabled) continue;
|
|
1509
|
-
const token = item.token ?? config.token;
|
|
1510
|
-
const server = new WsServer({
|
|
1511
|
-
host: item.host,
|
|
1512
|
-
port: item.port,
|
|
1513
|
-
path: WS_EVENTS_PATH
|
|
1514
|
-
});
|
|
1515
|
-
server.onRequest = (req, respond) => {
|
|
1516
|
-
handleWsSignal(req, respond, token, login);
|
|
1517
|
-
};
|
|
1518
|
-
adapters.push(server);
|
|
1519
|
-
}
|
|
1520
|
-
for (const adapter of adapters) broadcaster.register(adapter);
|
|
1521
|
-
return {
|
|
1522
|
-
adapters,
|
|
1523
|
-
close: async () => {
|
|
1524
|
-
await Promise.all(adapters.map((a) => a.close()));
|
|
1525
|
-
for (const adapter of adapters) broadcaster.unregister(adapter);
|
|
1526
|
-
}
|
|
1527
|
-
};
|
|
1528
|
-
}
|
|
1529
|
-
/** Bearer token 鉴权钩子。 */
|
|
1530
|
-
function makeBearerAuthorize(token) {
|
|
1531
|
-
return (ctx) => {
|
|
1532
|
-
const auth = ctx.headers["authorization"] ?? ctx.headers["Authorization"];
|
|
1533
|
-
if (auth?.startsWith(BEARER_PREFIX)) return auth.slice(7) === token;
|
|
1534
|
-
return false;
|
|
1535
|
-
};
|
|
1536
|
-
}
|
|
1537
|
-
/** 解析路径:/v1/{resource}.{method} → 动作名;不匹配返回 null。 */
|
|
1538
|
-
function parseActionPath(path) {
|
|
1539
|
-
if (!path.startsWith(V1_PREFIX)) return null;
|
|
1540
|
-
const rest = path.slice(4);
|
|
1541
|
-
if (ACTION_PATH_RE.exec(rest) === null) return null;
|
|
1542
|
-
return rest;
|
|
1543
|
-
}
|
|
1544
|
-
/** HTTP RPC 分发。 */
|
|
1545
|
-
function handleHttpRequest(ctx, req, respond, registry, login) {
|
|
1546
|
-
if (ctx.method !== "POST") {
|
|
1547
|
-
respond({ error: "method not allowed" }, 405);
|
|
1548
|
-
return;
|
|
1549
|
-
}
|
|
1550
|
-
const action = parseActionPath(ctx.path);
|
|
1551
|
-
if (action === null) {
|
|
1552
|
-
respond({ error: "api not found" }, HTTP_STATUS.notFound);
|
|
1553
|
-
return;
|
|
1554
|
-
}
|
|
1555
|
-
if (action === "meta") {
|
|
1556
|
-
respond({
|
|
1557
|
-
logins: [login()],
|
|
1558
|
-
proxy_urls: []
|
|
1559
|
-
}, HTTP_STATUS.ok);
|
|
1560
|
-
return;
|
|
1561
|
-
}
|
|
1562
|
-
const act = registry.get(action);
|
|
1563
|
-
if (act === void 0) {
|
|
1564
|
-
respond({ error: "api not found" }, HTTP_STATUS.notFound);
|
|
1565
|
-
return;
|
|
1566
|
-
}
|
|
1567
|
-
act.run(req).then((data) => {
|
|
1568
|
-
respond(data ?? {}, HTTP_STATUS.ok);
|
|
1569
|
-
}).catch((err) => {
|
|
1570
|
-
if (err instanceof SatoriActionError) {
|
|
1571
|
-
respond({ error: err.message }, err.status);
|
|
1572
|
-
return;
|
|
1573
|
-
}
|
|
1574
|
-
respond({ error: err instanceof Error ? err.message : "internal error" }, HTTP_STATUS.serverError);
|
|
1575
|
-
});
|
|
1576
|
-
}
|
|
1577
|
-
/** WS 信令处理(IDENTIFY → READY;PING → PONG)。 */
|
|
1578
|
-
function handleWsSignal(signal, respond, token, login) {
|
|
1579
|
-
const parsed = signal ?? {};
|
|
1580
|
-
const op = parsed.op;
|
|
1581
|
-
if (op === Opcode.IDENTIFY) {
|
|
1582
|
-
const body = parsed.body ?? {};
|
|
1583
|
-
if (token !== void 0 && token !== "" && body.token !== token) return;
|
|
1584
|
-
respond({
|
|
1585
|
-
op: Opcode.READY,
|
|
1586
|
-
body: {
|
|
1587
|
-
logins: [login()],
|
|
1588
|
-
proxy_urls: []
|
|
1589
|
-
}
|
|
1590
|
-
});
|
|
1591
|
-
return;
|
|
1592
|
-
}
|
|
1593
|
-
if (op === Opcode.PING) respond({
|
|
1594
|
-
op: Opcode.PONG,
|
|
1595
|
-
body: null
|
|
1596
|
-
});
|
|
1597
|
-
}
|
|
1598
|
-
/** 事件信令(adapter 广播用)。 */
|
|
1599
|
-
function toEventSignal(event) {
|
|
1600
|
-
return {
|
|
1601
|
-
op: Opcode.EVENT,
|
|
1602
|
-
body: event
|
|
1603
|
-
};
|
|
1604
|
-
}
|
|
1605
|
-
// src/satori/adapter.ts
|
|
1606
|
-
/** Satori 协议适配器。 */
|
|
1607
|
-
var NapukettoSatoriAdapter = class extends BaseProtocolAdapter {
|
|
1608
|
-
protocol = "satori";
|
|
1609
|
-
msgChannel;
|
|
1610
|
-
api;
|
|
1611
|
-
registry;
|
|
1612
|
-
logger;
|
|
1613
|
-
transports = null;
|
|
1614
|
-
unsubscribe = null;
|
|
1615
|
-
eventSn = 0;
|
|
1616
|
-
constructor(opts) {
|
|
1617
|
-
super({
|
|
1618
|
-
config: opts.config,
|
|
1619
|
-
broadcaster: opts.broadcaster,
|
|
1620
|
-
hooks: {
|
|
1621
|
-
onStart: (config) => this.startTransports(config),
|
|
1622
|
-
onStop: () => this.stopAll(),
|
|
1623
|
-
onReload: (config) => this.reloadTransports(config)
|
|
1624
|
-
}
|
|
1625
|
-
});
|
|
1626
|
-
this.msgChannel = opts.msgChannel;
|
|
1627
|
-
this.api = new SatoriApi(opts);
|
|
1628
|
-
this.logger = opts.logger;
|
|
1629
|
-
this.registry = createSatoriActionRegistry({ api: this.api });
|
|
1630
|
-
}
|
|
1631
|
-
/** 启动传输:装配(HTTP RPC + WS 事件服务)+ 打开 server + 订阅消息事件。 */
|
|
1632
|
-
async startTransports(config) {
|
|
1633
|
-
const broadcaster = this.getBroadcaster();
|
|
1634
|
-
if (broadcaster !== void 0) {
|
|
1635
|
-
this.transports = assembleSatoriTransports({
|
|
1636
|
-
config,
|
|
1637
|
-
broadcaster,
|
|
1638
|
-
registry: this.registry,
|
|
1639
|
-
login: () => this.currentLogin()
|
|
1640
|
-
});
|
|
1641
|
-
await Promise.all(this.transports.adapters.map((t) => t.open()));
|
|
1642
|
-
}
|
|
1643
|
-
this.subscribe();
|
|
1644
|
-
this.broadcastLoginUpdated(true);
|
|
1645
|
-
}
|
|
1646
|
-
/** 当前登录信息(READY logins / meta / login.get 用)。 */
|
|
1647
|
-
currentLogin() {
|
|
1648
|
-
return toLogin(this.api.self, 0, true);
|
|
1649
|
-
}
|
|
1650
|
-
/**
|
|
1651
|
-
* 配置热更新(实现):stop 旧传输/退订(广播 login-updated 离线)→
|
|
1652
|
-
* 按新配置重建(广播在线)。Satori 无 IPC 桥模式,无传输时同样走重建路径。
|
|
1653
|
-
*/
|
|
1654
|
-
async reloadTransports(config) {
|
|
1655
|
-
await this.stopAll();
|
|
1656
|
-
await this.startTransports(config);
|
|
1657
|
-
}
|
|
1658
|
-
/** 广播 login-updated 事件(适配器启停时)。 */
|
|
1659
|
-
broadcastLoginUpdated(online) {
|
|
1660
|
-
const login = toLogin(this.api.self, 0, online);
|
|
1661
|
-
this.broadcastEvent(toEventSignal({
|
|
1662
|
-
sn: this.nextSn(),
|
|
1663
|
-
type: "login-updated",
|
|
1664
|
-
timestamp: Date.now(),
|
|
1665
|
-
login
|
|
1666
|
-
}));
|
|
1667
|
-
}
|
|
1668
|
-
/** 停止:退订 + 关闭传输 + 广播 login-updated(离线)。 */
|
|
1669
|
-
async stopAll() {
|
|
1670
|
-
this.unsubscribeAll();
|
|
1671
|
-
if (this.transports !== null) {
|
|
1672
|
-
await this.transports.close();
|
|
1673
|
-
this.transports = null;
|
|
1674
|
-
}
|
|
1675
|
-
this.broadcastLoginUpdated(false);
|
|
1676
|
-
}
|
|
1677
|
-
/** 订阅 kernel 消息事件(幂等;启动前历史消息由共享订阅拦截)。 */
|
|
1678
|
-
subscribe() {
|
|
1679
|
-
if (this.unsubscribe !== null) return;
|
|
1680
|
-
this.unsubscribe = subscribeRawMessages(this.msgChannel, (msg) => {
|
|
1681
|
-
if (hasSatoriGrayTip(msg)) {
|
|
1682
|
-
this.broadcastGrayTip(msg).catch((err) => {
|
|
1683
|
-
logEventFailure(this.logger, "satori: grayTip 翻译失败,事件跳过", err);
|
|
1684
|
-
});
|
|
1685
|
-
return;
|
|
1686
|
-
}
|
|
1687
|
-
this.broadcastMessageCreated(msg).catch((err) => {
|
|
1688
|
-
logEventFailure(this.logger, "satori: 消息翻译失败,事件跳过", err);
|
|
1689
|
-
});
|
|
1690
|
-
}, { logger: this.logger });
|
|
1691
|
-
}
|
|
1692
|
-
/** 广播消息事件(message-created)。 */
|
|
1693
|
-
async broadcastMessageCreated(msg) {
|
|
1694
|
-
const content = await toSatoriMessageEvent(msg, {
|
|
1695
|
-
selfUin: this.api.selfUin,
|
|
1696
|
-
uidToUin: (uids) => this.api.uidToUin(uids)
|
|
1697
|
-
});
|
|
1698
|
-
this.api.setChannelType(String(msg.peerUin ?? ""), msg.chatType === 2);
|
|
1699
|
-
this.broadcastContent(content);
|
|
1700
|
-
}
|
|
1701
|
-
/** 广播 grayTip 事件(message-deleted / guild-member-added / removed)。 */
|
|
1702
|
-
async broadcastGrayTip(msg) {
|
|
1703
|
-
const uids = collectSatoriGrayTipUids(msg);
|
|
1704
|
-
let uidToUin = /* @__PURE__ */ new Map();
|
|
1705
|
-
if (uids.length > 0) uidToUin = await this.api.uidToUin(uids);
|
|
1706
|
-
const content = await toSatoriGrayTipEvent(msg, uidToUin);
|
|
1707
|
-
if (content !== null) this.broadcastContent(content);
|
|
1708
|
-
}
|
|
1709
|
-
/** 广播事件内容(sn 递增 + 最小 login 包装 + EVENT 信令)。 */
|
|
1710
|
-
broadcastContent(content) {
|
|
1711
|
-
this.broadcastEvent(toEventSignal({
|
|
1712
|
-
sn: this.nextSn(),
|
|
1713
|
-
type: content.type,
|
|
1714
|
-
timestamp: content.timestamp,
|
|
1715
|
-
login: toMinimalLogin(0, this.api.selfUin),
|
|
1716
|
-
...pickEventResources(content)
|
|
1717
|
-
}));
|
|
1718
|
-
}
|
|
1719
|
-
/** 事件 sn(递增,从 1 起)。 */
|
|
1720
|
-
nextSn() {
|
|
1721
|
-
this.eventSn += 1;
|
|
1722
|
-
return this.eventSn;
|
|
1723
|
-
}
|
|
1724
|
-
/** 退订(幂等)。 */
|
|
1725
|
-
unsubscribeAll() {
|
|
1726
|
-
this.unsubscribe?.();
|
|
1727
|
-
this.unsubscribe = null;
|
|
1728
|
-
}
|
|
1729
|
-
};
|
|
1730
|
-
/** 从事件内容提取可选资源字段(channel/guild/message/user/member/operator)。 */
|
|
1731
|
-
function pickEventResources(content) {
|
|
1732
|
-
const out = {};
|
|
1733
|
-
if (content.channel !== void 0) out.channel = content.channel;
|
|
1734
|
-
if (content.guild !== void 0) out.guild = content.guild;
|
|
1735
|
-
if (content.message !== void 0) out.message = content.message;
|
|
1736
|
-
if (content.user !== void 0) out.user = content.user;
|
|
1737
|
-
if (content.member !== void 0) out.member = content.member;
|
|
1738
|
-
if (content.operator !== void 0) out.operator = content.operator;
|
|
1739
|
-
return out;
|
|
1740
|
-
}
|
|
1741
|
-
export { HTTP_STATUS, NapukettoSatoriAdapter, SatoriActionError, SatoriApi, assembleSatoriTransports, canonicalToSatoriElements, createSatoriActionRegistry, parseContentToCanonical, parseElements, renderElements, satoriConfigSchema, satoriHttpStatusMap, satoriToCanonicalElements, toEventSignal };
|
|
1742
|
-
|
|
1743
|
-
//# sourceMappingURL=index.mjs.map
|
|
1
|
+
export { f as HTTP_STATUS, t as NapukettoSatoriAdapter, c as SatoriActionError, i as SatoriApi, n as assembleSatoriTransports, d as canonicalToSatoriElements, a as createSatoriActionRegistry, o as parseContentToCanonical, l as parseElements, u as renderElements, m as satoriConfigSchema, p as satoriHttpStatusMap, s as satoriToCanonicalElements, r as toEventSignal } from "../satori-Cb2uM3PY.mjs";
|