luckytri 1.0.4 → 1.0.6
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/LICENSE +21 -21
- package/README.en.md +2 -0
- package/README.md +2 -0
- package/docs/en/CHANGELOG.md +20 -13
- package/docs/en/guide.md +7 -5
- package/docs/en/her-life.md +2 -2
- package/docs/en/webui.md +1 -1
- package/docs/zh/CHANGELOG.md +20 -13
- package/docs/zh/guide.md +7 -5
- package/docs/zh/her-life.md +2 -2
- package/docs/zh/webui.md +1 -1
- package/package.json +2 -1
- package/public/app/assets/ChatsPage-CFMVozoG.css +1 -0
- package/public/app/assets/{ChatsPage-CshdtTzV.js → ChatsPage-DCYMkJ91.js} +2 -2
- package/public/app/assets/{Empty-C7yzOBzy.js → Empty-CLyZkxR5.js} +1 -1
- package/public/app/assets/{Empty-DmUB2-Bk.css → Empty-DIobYG3x.css} +1 -1
- package/public/app/assets/{Field-BjFv4Rfn.js → Field-BpaRwLI_.js} +1 -1
- package/public/app/assets/{HeartPage-eqo6ru16.js → HeartPage-BIOMTiwr.js} +1 -1
- package/public/app/assets/HeartPage-C-2jvgzF.css +1 -0
- package/public/app/assets/{LifePage-CN8GE5NK.js → LifePage-BCgM4JkR.js} +1 -1
- package/public/app/assets/LifePage-CG1OHbRK.css +1 -0
- package/public/app/assets/MemoryPage-C_Bu4rfb.css +1 -0
- package/public/app/assets/{MemoryPage-CCpMN4Wg.js → MemoryPage-vShvRqMt.js} +1 -1
- package/public/app/assets/NaturePage-CPZlImT0.css +1 -0
- package/public/app/assets/{NaturePage-C0JqdBTR.js → NaturePage-Cy9ptr4G.js} +2 -2
- package/public/app/assets/PeoplePage-3RRUITDz.css +1 -0
- package/public/app/assets/{PeoplePage-BXGBbO4u.js → PeoplePage-CUZekB2E.js} +1 -1
- package/public/app/assets/{PluginsPage-Dksp5AV5.js → PluginsPage-1RM4rmJP.js} +1 -1
- package/public/app/assets/{SystemPage-BenIyTvk.css → SystemPage-BI2CB5Ig.css} +1 -1
- package/public/app/assets/{SystemPage-Cd7aa8eE.js → SystemPage-BeT5TmIV.js} +1 -1
- package/public/app/assets/TimePage-C-guwPk2.css +1 -0
- package/public/app/assets/{TimePage-Bs_zImb4.js → TimePage-taTmPMM8.js} +2 -2
- package/public/app/assets/{index-BHZcHUMh.css → index-DY5AM0Hz.css} +1 -1
- package/public/app/assets/{index-CyRtYxP3.js → index-DdYwC5ZU.js} +3 -3
- package/public/app/assets/{knowledge-BJOIyuJo.js → knowledge-DJmfu2KN.js} +1 -1
- package/public/app/assets/{media-Bwtx8blz.js → media-BEtnE0Gv.js} +1 -1
- package/public/app/assets/{mind-DtTVrW6Z.js → mind-Dtiw0-QK.js} +1 -1
- package/public/app/index.html +6 -5
- package/public/guide.en.html +11 -3
- package/public/guide.html +6 -3
- package/public/tutorial.en.md +2 -0
- package/public/tutorial.md +2 -0
- package/scripts/import-data.js +128 -128
- package/scripts/proxy-config.js +167 -167
- package/scripts/service-supervisor.js +50 -0
- package/scripts/start-server.js +71 -16
- package/scripts/update-instance.js +201 -201
- package/server/channels/adapters.js +27 -27
- package/server/channels/onebot/adapter.js +288 -288
- package/server/channels/qqbot/adapter.js +253 -253
- package/server/channels/qqbot/api.js +199 -199
- package/server/channels/qqbot/credentials.js +11 -11
- package/server/channels/qqbot/gateway.js +274 -274
- package/server/channels/qqbot/index.js +389 -389
- package/server/channels/segments.js +40 -40
- package/server/channels/session-key.js +153 -153
- package/server/channels/state.js +42 -42
- package/server/core/api.js +564 -564
- package/server/core/context-builder.js +288 -288
- package/server/core/context-compactor.js +454 -454
- package/server/core/conversation-cues.js +325 -325
- package/server/core/conversation-manager.js +87 -87
- package/server/core/feedback.js +8 -8
- package/server/core/group-style.js +72 -72
- package/server/core/initiative-context.js +66 -66
- package/server/core/input-budget.js +32 -32
- package/server/core/local-demo.js +98 -98
- package/server/core/message-manager.js +118 -118
- package/server/core/message-scheduler.js +78 -78
- package/server/core/model-json.js +190 -190
- package/server/core/model-presets.js +900 -900
- package/server/core/name-call.js +29 -29
- package/server/core/network.js +157 -157
- package/server/core/person-name.js +22 -22
- package/server/core/persona-manager.js +2 -1
- package/server/core/reply-delivery.js +404 -403
- package/server/core/reply-target-resolver.js +50 -50
- package/server/core/response-generator.js +50 -50
- package/server/core/response-validator.js +14 -2
- package/server/core/retired-prompts.js +51 -51
- package/server/core/sessions.js +109 -109
- package/server/core/speaker-names.js +92 -92
- package/server/core/topic-tracker.js +65 -65
- package/server/http.js +18 -18
- package/server/i18n/decisions.js +209 -206
- package/server/i18n/errors.js +4 -0
- package/server/i18n/index.js +144 -144
- package/server/i18n/models.js +111 -111
- package/server/knowledge/api.js +342 -342
- package/server/knowledge/embedding-profile.js +114 -114
- package/server/knowledge/index.js +13 -13
- package/server/knowledge/retrieval.js +93 -93
- package/server/knowledge/schema.js +158 -158
- package/server/knowledge/vector-index.js +90 -90
- package/server/mind/affect.js +265 -265
- package/server/mind/attention.js +200 -200
- package/server/mind/bonds.js +565 -565
- package/server/mind/clock.js +83 -83
- package/server/mind/continuity.js +562 -562
- package/server/mind/conversation-origin.js +175 -175
- package/server/mind/days.js +210 -210
- package/server/mind/evidence-read.js +45 -45
- package/server/mind/faces.js +373 -373
- package/server/mind/guard.js +60 -60
- package/server/mind/initiative.js +338 -338
- package/server/mind/life/activities.js +21 -21
- package/server/mind/life/context.js +330 -323
- package/server/mind/life/diary.js +20 -0
- package/server/mind/life/growth.js +261 -254
- package/server/mind/life/index.js +10 -2
- package/server/mind/life/presence.js +305 -305
- package/server/mind/meetings.js +1069 -1069
- package/server/mind/memory-policy.js +48 -48
- package/server/mind/memory.js +888 -888
- package/server/mind/nature.js +358 -358
- package/server/mind/novelty.js +19 -19
- package/server/mind/periods.js +118 -118
- package/server/mind/reading.js +120 -120
- package/server/mind/relationship-context.js +71 -0
- package/server/mind/relationships.js +390 -279
- package/server/mind/salience.js +201 -201
- package/server/mind/schema.js +567 -567
- package/server/mind/self.js +484 -484
- package/server/mind/thoughts.js +274 -274
- package/server/mind/time/agenda.js +242 -242
- package/server/mind/time/api.js +200 -200
- package/server/mind/time/attention.js +34 -34
- package/server/mind/time/delivery-links.js +130 -130
- package/server/mind/time/model-material.js +77 -77
- package/server/mind/time/presentation.js +51 -51
- package/server/mind/time/schedule.js +83 -83
- package/server/mind/time/schema.js +84 -84
- package/server/mind/time/search.js +383 -383
- package/server/mind/time/sharing.js +472 -472
- package/server/mind/time/task-links.js +311 -311
- package/server/mind/time/works.js +432 -432
- package/server/mind/traits.js +191 -191
- package/server/mind/view.js +9 -1
- package/server/runtime/installation.js +116 -116
- package/server/runtime/update-execution.js +2 -1
- package/server/runtime/update-manager.js +140 -140
- package/server/storage/data-package.js +298 -298
- package/server/storage/data-transfer-api.js +50 -50
- package/server/storage/data-transfer-worker.js +18 -18
- package/server/storage/data-transfer.js +416 -416
- package/server/studio/display-names.js +139 -139
- package/server/studio/management.js +102 -102
- package/server/studio/updates.js +144 -144
- package/server/version.js +10 -10
- package/public/app/assets/ChatsPage-DfyPCS9N.css +0 -1
- package/public/app/assets/HeartPage-Bft9wZp5.css +0 -1
- package/public/app/assets/LifePage-BUi5euBF.css +0 -1
- package/public/app/assets/MemoryPage-cZvXfkXb.css +0 -1
- package/public/app/assets/NaturePage-CHkpb5NK.css +0 -1
- package/public/app/assets/PeoplePage-BTU0H1pM.css +0 -1
- package/public/app/assets/TimePage-BOms_n4I.css +0 -1
|
@@ -1,253 +1,253 @@
|
|
|
1
|
-
import { formatSessionKey, parseSessionKey } from "../session-key.js";
|
|
2
|
-
|
|
3
|
-
// Turns events of the QQ 开放平台 into the neutral message the core reads, and
|
|
4
|
-
// knows how the platform addresses a room. No network and no state here.
|
|
5
|
-
|
|
6
|
-
const text = (value) => (value == null ? "" : String(value));
|
|
7
|
-
|
|
8
|
-
// `message_scene.ext` is a list of "key=value" strings.
|
|
9
|
-
export function sceneExt(scene) {
|
|
10
|
-
const out = {};
|
|
11
|
-
for (const item of Array.isArray(scene?.ext) ? scene.ext : []) {
|
|
12
|
-
const at = String(item).indexOf("=");
|
|
13
|
-
if (at > 0) out[String(item).slice(0, at)] = String(item).slice(at + 1);
|
|
14
|
-
}
|
|
15
|
-
return out;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const placeholderName = (id) =>
|
|
19
|
-
`QQ 用户·${text(id).slice(-4).toUpperCase()}`;
|
|
20
|
-
export const isPlaceholderName = (name) => /^QQ 用户·/.test(text(name));
|
|
21
|
-
|
|
22
|
-
function mediaUrl(value) {
|
|
23
|
-
const raw = text(value).trim();
|
|
24
|
-
if (!raw) return "";
|
|
25
|
-
if (raw.startsWith("//")) return `https:${raw}`;
|
|
26
|
-
if (/^https?:\/\//i.test(raw)) return raw;
|
|
27
|
-
return `https://${raw}`;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function attachmentOf(item) {
|
|
31
|
-
const type = text(item?.content_type).toLowerCase();
|
|
32
|
-
const url = mediaUrl(item?.url);
|
|
33
|
-
const kind = type.startsWith("image/")
|
|
34
|
-
? "image"
|
|
35
|
-
: type.startsWith("voice") || type.startsWith("audio")
|
|
36
|
-
? "record"
|
|
37
|
-
: type.startsWith("video")
|
|
38
|
-
? "video"
|
|
39
|
-
: "file";
|
|
40
|
-
return {
|
|
41
|
-
type: kind,
|
|
42
|
-
url,
|
|
43
|
-
file: "",
|
|
44
|
-
filename: text(item?.filename).slice(0, 120),
|
|
45
|
-
contentType: type,
|
|
46
|
-
width: Number(item?.width) || undefined,
|
|
47
|
-
height: Number(item?.height) || undefined,
|
|
48
|
-
size: Number(item?.size) || undefined,
|
|
49
|
-
summary: "",
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const MENTION = /<@!?([^>\s]+)>/g;
|
|
54
|
-
|
|
55
|
-
// The quoted message carried inside a message_type 103 event, if any.
|
|
56
|
-
export function quotedElement(d, replyId) {
|
|
57
|
-
const elements = Array.isArray(d?.msg_elements) ? d.msg_elements : [];
|
|
58
|
-
return (
|
|
59
|
-
elements.find((item) => replyId && text(item?.msg_idx) === replyId) ||
|
|
60
|
-
elements.find((item) => item && typeof item === "object") ||
|
|
61
|
-
null
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export const qqbot = {
|
|
66
|
-
type: "qqbot",
|
|
67
|
-
capabilities: {
|
|
68
|
-
mention: true,
|
|
69
|
-
quote: true,
|
|
70
|
-
image: true,
|
|
71
|
-
thread: false,
|
|
72
|
-
sticker: false,
|
|
73
|
-
},
|
|
74
|
-
mediaHosts: /(^|\.)(qpic\.cn|gtimg\.cn|qq\.com\.cn|qq\.com|myqcloud\.com)$/i,
|
|
75
|
-
usableMediaUrl(url) {
|
|
76
|
-
try {
|
|
77
|
-
const u = new URL(url);
|
|
78
|
-
return (
|
|
79
|
-
u.protocol === "https:" &&
|
|
80
|
-
!u.username &&
|
|
81
|
-
!u.password &&
|
|
82
|
-
this.mediaHosts.test(u.hostname)
|
|
83
|
-
);
|
|
84
|
-
} catch {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
},
|
|
88
|
-
nativeChatId(sessionId) {
|
|
89
|
-
return parseSessionKey(sessionId).nativeId;
|
|
90
|
-
},
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// `type` is the event name (`t`), `d` its data. `context` supplies what only
|
|
94
|
-
// the channel knows: the AppID, the references of messages she sent herself,
|
|
95
|
-
// and a name remembered for a person.
|
|
96
|
-
export function normalize(type, d, context = {}) {
|
|
97
|
-
const group =
|
|
98
|
-
type === "GROUP_AT_MESSAGE_CREATE" || type === "GROUP_MESSAGE_CREATE";
|
|
99
|
-
if (!group && type !== "C2C_MESSAGE_CREATE") return null;
|
|
100
|
-
if (!d || typeof d !== "object" || d.id == null) return null;
|
|
101
|
-
const appId = text(context.appId);
|
|
102
|
-
const author = d.author && typeof d.author === "object" ? d.author : {};
|
|
103
|
-
if (author.bot === true) return null;
|
|
104
|
-
const openid = text(
|
|
105
|
-
group ? author.member_openid || author.id : author.user_openid || author.id,
|
|
106
|
-
);
|
|
107
|
-
// Someone seen in several rooms is only recognised as one person when the
|
|
108
|
-
// platform hands out a unified identity; otherwise each room's openid is a
|
|
109
|
-
// separate person, as it would be for anyone met in different places.
|
|
110
|
-
const person = text(author.union_openid) || openid;
|
|
111
|
-
const kind = group ? "group" : "private";
|
|
112
|
-
const nativeId = group ? text(d.group_openid ?? d.group_id) : person;
|
|
113
|
-
if (!appId || !openid || !nativeId) return null;
|
|
114
|
-
|
|
115
|
-
const ext = sceneExt(d.message_scene);
|
|
116
|
-
const platformId = ext.msg_idx || text(d.id);
|
|
117
|
-
const replyId = ext.ref_msg_idx || "";
|
|
118
|
-
const quoted = replyId ? quotedElement(d, replyId) : null;
|
|
119
|
-
const botRefs = context.botRefs;
|
|
120
|
-
const replyToBot =
|
|
121
|
-
!!replyId &&
|
|
122
|
-
(!!botRefs?.has?.(replyId) ||
|
|
123
|
-
quoted?.author?.bot === true ||
|
|
124
|
-
(!!quoted?.author?.id && text(quoted.author.id) === text(context.botId)));
|
|
125
|
-
|
|
126
|
-
const mentionList = Array.isArray(d.mentions) ? d.mentions : [];
|
|
127
|
-
const self = new Set(
|
|
128
|
-
mentionList
|
|
129
|
-
.filter((user) => user?.is_you === true)
|
|
130
|
-
.flatMap((user) => [user.id, user.member_openid, user.user_openid])
|
|
131
|
-
.filter(Boolean)
|
|
132
|
-
.map(text),
|
|
133
|
-
);
|
|
134
|
-
const atSelf =
|
|
135
|
-
type === "GROUP_AT_MESSAGE_CREATE" ||
|
|
136
|
-
mentionList.some((user) => user?.is_you === true);
|
|
137
|
-
const mentions = [
|
|
138
|
-
...(atSelf ? [appId] : []),
|
|
139
|
-
...mentionList
|
|
140
|
-
.filter((user) => user?.is_you !== true)
|
|
141
|
-
.map((user) => text(user.union_openid || user.member_openid || user.id))
|
|
142
|
-
.filter(Boolean),
|
|
143
|
-
];
|
|
144
|
-
|
|
145
|
-
const attachments = (Array.isArray(d.attachments) ? d.attachments : [])
|
|
146
|
-
.filter((item) => item && typeof item === "object")
|
|
147
|
-
.map(attachmentOf);
|
|
148
|
-
const media = {
|
|
149
|
-
images: attachments.filter((item) => item.type === "image").length,
|
|
150
|
-
stickers: 0,
|
|
151
|
-
other: attachments.filter((item) => item.type !== "image").length,
|
|
152
|
-
};
|
|
153
|
-
const body = text(d.content)
|
|
154
|
-
.replace(MENTION, (_, id) => (self.has(id) ? "@我" : "[提及成员]"))
|
|
155
|
-
.replace(/\u00a0/g, " ");
|
|
156
|
-
const plain = body.replace(/\[提及成员\]|@我/g, "").trim();
|
|
157
|
-
const marks = attachments
|
|
158
|
-
.map((item) => (item.type === "image" ? "[图片]" : "[媒体]"))
|
|
159
|
-
.join("");
|
|
160
|
-
const addressed = atSelf && !/@我/.test(body) ? "@我 " : "";
|
|
161
|
-
const visible = (addressed + body.trim() + marks).trim();
|
|
162
|
-
const out =
|
|
163
|
-
visible ||
|
|
164
|
-
(atSelf || replyToBot ? (replyToBot ? "[回复你]" : "[叫了你一声]") : "");
|
|
165
|
-
if (!out) return null;
|
|
166
|
-
const mediaCount = media.images + media.other;
|
|
167
|
-
|
|
168
|
-
const name =
|
|
169
|
-
text(author.username).trim() ||
|
|
170
|
-
text(context.knownName?.(person)).trim() ||
|
|
171
|
-
placeholderName(person);
|
|
172
|
-
const time = Date.parse(text(d.timestamp));
|
|
173
|
-
return {
|
|
174
|
-
sessionId: formatSessionKey({
|
|
175
|
-
channel: "qqbot",
|
|
176
|
-
accountId: appId,
|
|
177
|
-
kind,
|
|
178
|
-
nativeId,
|
|
179
|
-
}),
|
|
180
|
-
channel: "qqbot",
|
|
181
|
-
accountId: appId,
|
|
182
|
-
nativeId,
|
|
183
|
-
kind,
|
|
184
|
-
userId: person,
|
|
185
|
-
name: name.slice(0, 100),
|
|
186
|
-
accountName: name.slice(0, 100),
|
|
187
|
-
text: out.slice(0, 4000),
|
|
188
|
-
mentioned: atSelf || replyToBot,
|
|
189
|
-
replyToBot,
|
|
190
|
-
platformId,
|
|
191
|
-
replyId,
|
|
192
|
-
time: Number.isFinite(time) ? time : Date.now(),
|
|
193
|
-
mentions,
|
|
194
|
-
attachments,
|
|
195
|
-
segments: [],
|
|
196
|
-
media: {
|
|
197
|
-
...media,
|
|
198
|
-
count: mediaCount,
|
|
199
|
-
only: mediaCount > 0 && !plain && !atSelf && !replyToBot,
|
|
200
|
-
imageOnly:
|
|
201
|
-
media.images > 0 &&
|
|
202
|
-
mediaCount === media.images &&
|
|
203
|
-
!plain &&
|
|
204
|
-
!atSelf &&
|
|
205
|
-
!replyToBot,
|
|
206
|
-
stickerOnly: false,
|
|
207
|
-
},
|
|
208
|
-
eventId: `${appId}:${kind}:${nativeId}:${text(d.id)}`,
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// The openid the platform wants when it is sent something for this event.
|
|
213
|
-
export function openidOf(type, d) {
|
|
214
|
-
const author = d?.author || {};
|
|
215
|
-
return text(
|
|
216
|
-
type === "C2C_MESSAGE_CREATE"
|
|
217
|
-
? author.user_openid || author.id
|
|
218
|
-
: author.member_openid || author.id,
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// The message `replyId` pointed to, rebuilt from the event that quoted it.
|
|
223
|
-
export function quotedMessage(current, element, context = {}) {
|
|
224
|
-
if (!element || typeof element !== "object") return null;
|
|
225
|
-
const author = element.author || {};
|
|
226
|
-
const bot =
|
|
227
|
-
author.bot === true ||
|
|
228
|
-
(!!author.id && text(author.id) === text(context.botId)) ||
|
|
229
|
-
!!context.botRefs?.has?.(text(current.replyId));
|
|
230
|
-
const id = text(
|
|
231
|
-
author.union_openid ||
|
|
232
|
-
author.member_openid ||
|
|
233
|
-
author.user_openid ||
|
|
234
|
-
author.id,
|
|
235
|
-
);
|
|
236
|
-
const content = text(element.content).replace(MENTION, "[提及成员]").trim();
|
|
237
|
-
const marks = (Array.isArray(element.attachments) ? element.attachments : [])
|
|
238
|
-
.map((item) => (attachmentOf(item).type === "image" ? "[图片]" : "[媒体]"))
|
|
239
|
-
.join("");
|
|
240
|
-
return {
|
|
241
|
-
sessionId: current.sessionId,
|
|
242
|
-
kind: current.kind,
|
|
243
|
-
userId: bot ? "bot" : id || "unknown",
|
|
244
|
-
name: bot
|
|
245
|
-
? ""
|
|
246
|
-
: text(author.username).trim() || (id ? placeholderName(id) : ""),
|
|
247
|
-
text: (content + marks).trim(),
|
|
248
|
-
role: bot ? "assistant" : "user",
|
|
249
|
-
platformId: text(current.replyId),
|
|
250
|
-
accountId: current.accountId,
|
|
251
|
-
time: Date.now(),
|
|
252
|
-
};
|
|
253
|
-
}
|
|
1
|
+
import { formatSessionKey, parseSessionKey } from "../session-key.js";
|
|
2
|
+
|
|
3
|
+
// Turns events of the QQ 开放平台 into the neutral message the core reads, and
|
|
4
|
+
// knows how the platform addresses a room. No network and no state here.
|
|
5
|
+
|
|
6
|
+
const text = (value) => (value == null ? "" : String(value));
|
|
7
|
+
|
|
8
|
+
// `message_scene.ext` is a list of "key=value" strings.
|
|
9
|
+
export function sceneExt(scene) {
|
|
10
|
+
const out = {};
|
|
11
|
+
for (const item of Array.isArray(scene?.ext) ? scene.ext : []) {
|
|
12
|
+
const at = String(item).indexOf("=");
|
|
13
|
+
if (at > 0) out[String(item).slice(0, at)] = String(item).slice(at + 1);
|
|
14
|
+
}
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const placeholderName = (id) =>
|
|
19
|
+
`QQ 用户·${text(id).slice(-4).toUpperCase()}`;
|
|
20
|
+
export const isPlaceholderName = (name) => /^QQ 用户·/.test(text(name));
|
|
21
|
+
|
|
22
|
+
function mediaUrl(value) {
|
|
23
|
+
const raw = text(value).trim();
|
|
24
|
+
if (!raw) return "";
|
|
25
|
+
if (raw.startsWith("//")) return `https:${raw}`;
|
|
26
|
+
if (/^https?:\/\//i.test(raw)) return raw;
|
|
27
|
+
return `https://${raw}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function attachmentOf(item) {
|
|
31
|
+
const type = text(item?.content_type).toLowerCase();
|
|
32
|
+
const url = mediaUrl(item?.url);
|
|
33
|
+
const kind = type.startsWith("image/")
|
|
34
|
+
? "image"
|
|
35
|
+
: type.startsWith("voice") || type.startsWith("audio")
|
|
36
|
+
? "record"
|
|
37
|
+
: type.startsWith("video")
|
|
38
|
+
? "video"
|
|
39
|
+
: "file";
|
|
40
|
+
return {
|
|
41
|
+
type: kind,
|
|
42
|
+
url,
|
|
43
|
+
file: "",
|
|
44
|
+
filename: text(item?.filename).slice(0, 120),
|
|
45
|
+
contentType: type,
|
|
46
|
+
width: Number(item?.width) || undefined,
|
|
47
|
+
height: Number(item?.height) || undefined,
|
|
48
|
+
size: Number(item?.size) || undefined,
|
|
49
|
+
summary: "",
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const MENTION = /<@!?([^>\s]+)>/g;
|
|
54
|
+
|
|
55
|
+
// The quoted message carried inside a message_type 103 event, if any.
|
|
56
|
+
export function quotedElement(d, replyId) {
|
|
57
|
+
const elements = Array.isArray(d?.msg_elements) ? d.msg_elements : [];
|
|
58
|
+
return (
|
|
59
|
+
elements.find((item) => replyId && text(item?.msg_idx) === replyId) ||
|
|
60
|
+
elements.find((item) => item && typeof item === "object") ||
|
|
61
|
+
null
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const qqbot = {
|
|
66
|
+
type: "qqbot",
|
|
67
|
+
capabilities: {
|
|
68
|
+
mention: true,
|
|
69
|
+
quote: true,
|
|
70
|
+
image: true,
|
|
71
|
+
thread: false,
|
|
72
|
+
sticker: false,
|
|
73
|
+
},
|
|
74
|
+
mediaHosts: /(^|\.)(qpic\.cn|gtimg\.cn|qq\.com\.cn|qq\.com|myqcloud\.com)$/i,
|
|
75
|
+
usableMediaUrl(url) {
|
|
76
|
+
try {
|
|
77
|
+
const u = new URL(url);
|
|
78
|
+
return (
|
|
79
|
+
u.protocol === "https:" &&
|
|
80
|
+
!u.username &&
|
|
81
|
+
!u.password &&
|
|
82
|
+
this.mediaHosts.test(u.hostname)
|
|
83
|
+
);
|
|
84
|
+
} catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
nativeChatId(sessionId) {
|
|
89
|
+
return parseSessionKey(sessionId).nativeId;
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// `type` is the event name (`t`), `d` its data. `context` supplies what only
|
|
94
|
+
// the channel knows: the AppID, the references of messages she sent herself,
|
|
95
|
+
// and a name remembered for a person.
|
|
96
|
+
export function normalize(type, d, context = {}) {
|
|
97
|
+
const group =
|
|
98
|
+
type === "GROUP_AT_MESSAGE_CREATE" || type === "GROUP_MESSAGE_CREATE";
|
|
99
|
+
if (!group && type !== "C2C_MESSAGE_CREATE") return null;
|
|
100
|
+
if (!d || typeof d !== "object" || d.id == null) return null;
|
|
101
|
+
const appId = text(context.appId);
|
|
102
|
+
const author = d.author && typeof d.author === "object" ? d.author : {};
|
|
103
|
+
if (author.bot === true) return null;
|
|
104
|
+
const openid = text(
|
|
105
|
+
group ? author.member_openid || author.id : author.user_openid || author.id,
|
|
106
|
+
);
|
|
107
|
+
// Someone seen in several rooms is only recognised as one person when the
|
|
108
|
+
// platform hands out a unified identity; otherwise each room's openid is a
|
|
109
|
+
// separate person, as it would be for anyone met in different places.
|
|
110
|
+
const person = text(author.union_openid) || openid;
|
|
111
|
+
const kind = group ? "group" : "private";
|
|
112
|
+
const nativeId = group ? text(d.group_openid ?? d.group_id) : person;
|
|
113
|
+
if (!appId || !openid || !nativeId) return null;
|
|
114
|
+
|
|
115
|
+
const ext = sceneExt(d.message_scene);
|
|
116
|
+
const platformId = ext.msg_idx || text(d.id);
|
|
117
|
+
const replyId = ext.ref_msg_idx || "";
|
|
118
|
+
const quoted = replyId ? quotedElement(d, replyId) : null;
|
|
119
|
+
const botRefs = context.botRefs;
|
|
120
|
+
const replyToBot =
|
|
121
|
+
!!replyId &&
|
|
122
|
+
(!!botRefs?.has?.(replyId) ||
|
|
123
|
+
quoted?.author?.bot === true ||
|
|
124
|
+
(!!quoted?.author?.id && text(quoted.author.id) === text(context.botId)));
|
|
125
|
+
|
|
126
|
+
const mentionList = Array.isArray(d.mentions) ? d.mentions : [];
|
|
127
|
+
const self = new Set(
|
|
128
|
+
mentionList
|
|
129
|
+
.filter((user) => user?.is_you === true)
|
|
130
|
+
.flatMap((user) => [user.id, user.member_openid, user.user_openid])
|
|
131
|
+
.filter(Boolean)
|
|
132
|
+
.map(text),
|
|
133
|
+
);
|
|
134
|
+
const atSelf =
|
|
135
|
+
type === "GROUP_AT_MESSAGE_CREATE" ||
|
|
136
|
+
mentionList.some((user) => user?.is_you === true);
|
|
137
|
+
const mentions = [
|
|
138
|
+
...(atSelf ? [appId] : []),
|
|
139
|
+
...mentionList
|
|
140
|
+
.filter((user) => user?.is_you !== true)
|
|
141
|
+
.map((user) => text(user.union_openid || user.member_openid || user.id))
|
|
142
|
+
.filter(Boolean),
|
|
143
|
+
];
|
|
144
|
+
|
|
145
|
+
const attachments = (Array.isArray(d.attachments) ? d.attachments : [])
|
|
146
|
+
.filter((item) => item && typeof item === "object")
|
|
147
|
+
.map(attachmentOf);
|
|
148
|
+
const media = {
|
|
149
|
+
images: attachments.filter((item) => item.type === "image").length,
|
|
150
|
+
stickers: 0,
|
|
151
|
+
other: attachments.filter((item) => item.type !== "image").length,
|
|
152
|
+
};
|
|
153
|
+
const body = text(d.content)
|
|
154
|
+
.replace(MENTION, (_, id) => (self.has(id) ? "@我" : "[提及成员]"))
|
|
155
|
+
.replace(/\u00a0/g, " ");
|
|
156
|
+
const plain = body.replace(/\[提及成员\]|@我/g, "").trim();
|
|
157
|
+
const marks = attachments
|
|
158
|
+
.map((item) => (item.type === "image" ? "[图片]" : "[媒体]"))
|
|
159
|
+
.join("");
|
|
160
|
+
const addressed = atSelf && !/@我/.test(body) ? "@我 " : "";
|
|
161
|
+
const visible = (addressed + body.trim() + marks).trim();
|
|
162
|
+
const out =
|
|
163
|
+
visible ||
|
|
164
|
+
(atSelf || replyToBot ? (replyToBot ? "[回复你]" : "[叫了你一声]") : "");
|
|
165
|
+
if (!out) return null;
|
|
166
|
+
const mediaCount = media.images + media.other;
|
|
167
|
+
|
|
168
|
+
const name =
|
|
169
|
+
text(author.username).trim() ||
|
|
170
|
+
text(context.knownName?.(person)).trim() ||
|
|
171
|
+
placeholderName(person);
|
|
172
|
+
const time = Date.parse(text(d.timestamp));
|
|
173
|
+
return {
|
|
174
|
+
sessionId: formatSessionKey({
|
|
175
|
+
channel: "qqbot",
|
|
176
|
+
accountId: appId,
|
|
177
|
+
kind,
|
|
178
|
+
nativeId,
|
|
179
|
+
}),
|
|
180
|
+
channel: "qqbot",
|
|
181
|
+
accountId: appId,
|
|
182
|
+
nativeId,
|
|
183
|
+
kind,
|
|
184
|
+
userId: person,
|
|
185
|
+
name: name.slice(0, 100),
|
|
186
|
+
accountName: name.slice(0, 100),
|
|
187
|
+
text: out.slice(0, 4000),
|
|
188
|
+
mentioned: atSelf || replyToBot,
|
|
189
|
+
replyToBot,
|
|
190
|
+
platformId,
|
|
191
|
+
replyId,
|
|
192
|
+
time: Number.isFinite(time) ? time : Date.now(),
|
|
193
|
+
mentions,
|
|
194
|
+
attachments,
|
|
195
|
+
segments: [],
|
|
196
|
+
media: {
|
|
197
|
+
...media,
|
|
198
|
+
count: mediaCount,
|
|
199
|
+
only: mediaCount > 0 && !plain && !atSelf && !replyToBot,
|
|
200
|
+
imageOnly:
|
|
201
|
+
media.images > 0 &&
|
|
202
|
+
mediaCount === media.images &&
|
|
203
|
+
!plain &&
|
|
204
|
+
!atSelf &&
|
|
205
|
+
!replyToBot,
|
|
206
|
+
stickerOnly: false,
|
|
207
|
+
},
|
|
208
|
+
eventId: `${appId}:${kind}:${nativeId}:${text(d.id)}`,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// The openid the platform wants when it is sent something for this event.
|
|
213
|
+
export function openidOf(type, d) {
|
|
214
|
+
const author = d?.author || {};
|
|
215
|
+
return text(
|
|
216
|
+
type === "C2C_MESSAGE_CREATE"
|
|
217
|
+
? author.user_openid || author.id
|
|
218
|
+
: author.member_openid || author.id,
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// The message `replyId` pointed to, rebuilt from the event that quoted it.
|
|
223
|
+
export function quotedMessage(current, element, context = {}) {
|
|
224
|
+
if (!element || typeof element !== "object") return null;
|
|
225
|
+
const author = element.author || {};
|
|
226
|
+
const bot =
|
|
227
|
+
author.bot === true ||
|
|
228
|
+
(!!author.id && text(author.id) === text(context.botId)) ||
|
|
229
|
+
!!context.botRefs?.has?.(text(current.replyId));
|
|
230
|
+
const id = text(
|
|
231
|
+
author.union_openid ||
|
|
232
|
+
author.member_openid ||
|
|
233
|
+
author.user_openid ||
|
|
234
|
+
author.id,
|
|
235
|
+
);
|
|
236
|
+
const content = text(element.content).replace(MENTION, "[提及成员]").trim();
|
|
237
|
+
const marks = (Array.isArray(element.attachments) ? element.attachments : [])
|
|
238
|
+
.map((item) => (attachmentOf(item).type === "image" ? "[图片]" : "[媒体]"))
|
|
239
|
+
.join("");
|
|
240
|
+
return {
|
|
241
|
+
sessionId: current.sessionId,
|
|
242
|
+
kind: current.kind,
|
|
243
|
+
userId: bot ? "bot" : id || "unknown",
|
|
244
|
+
name: bot
|
|
245
|
+
? ""
|
|
246
|
+
: text(author.username).trim() || (id ? placeholderName(id) : ""),
|
|
247
|
+
text: (content + marks).trim(),
|
|
248
|
+
role: bot ? "assistant" : "user",
|
|
249
|
+
platformId: text(current.replyId),
|
|
250
|
+
accountId: current.accountId,
|
|
251
|
+
time: Date.now(),
|
|
252
|
+
};
|
|
253
|
+
}
|