qq-bot-roost 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/docs//345/256/230/346/226/271/346/216/245/345/217/243/351/231/220/345/210/266.md +32 -0
- package/package.json +3 -3
- package/src/api.js +22 -0
- package/src/bot.js +149 -8
- package/src/dev/platform.js +140 -9
- package/src/dev/url-guard.js +209 -0
- package/src/endpoints.js +2 -1
- package/src/groups.js +17 -1
- package/src/host/methods.js +191 -1
- package/src/host/serialize.js +15 -0
- package/src/ledger.js +202 -5
- package/src/message.js +66 -4
- package/src/onebot/actions.js +131 -17
- package/src/onebot/capability.js +1 -1
- package/src/onebot/translate.js +7 -3
- package/src/people-search.js +88 -0
- package/src/search-query.js +87 -0
- package/src/web/server.js +348 -7
- package/web/app.js +371 -15
- package/web/index.html +65 -1
- package/web/style.css +63 -2
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 出站图片地址的安全校验
|
|
3
|
+
* ======================
|
|
4
|
+
*
|
|
5
|
+
* 为什么单独一个文件:`readImage` 有三个入口——面板 `/api/dev/avatar`、
|
|
6
|
+
* `ctx.dev.setAvatar`(内核插件)、OneBot `set_qq_profile`(**模型可触发**)。
|
|
7
|
+
* 只验 `^https?://` 等于把「让宿主去 fetch 任意地址」开给模型:内网服务、
|
|
8
|
+
* 云元数据(169.254.169.254)、本机管理端口都能打,错误回显还能当端口探针。
|
|
9
|
+
*
|
|
10
|
+
* 判据与 `dsh-roost/src/ssrf.js` 同一套(那个仓已经在用;两个仓互不依赖,
|
|
11
|
+
* 所以按同样口径各实现一份,改一边要想着另一边):
|
|
12
|
+
*
|
|
13
|
+
* · 只放行 http / https,不许带账号密码
|
|
14
|
+
* · 字面 IP 直接判私有;域名先解析,**任一**地址私有就拒(防 DNS rebinding)
|
|
15
|
+
* · `::ffff:` 映射地址还原成 IPv4 再判——`new URL` 会把
|
|
16
|
+
* `::ffff:127.0.0.1` 规范成 `::ffff:7f00:1`,只认点分会漏
|
|
17
|
+
* · 跟随重定向要**逐跳**再校验,不能只查第一跳
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { isIP } from 'node:net';
|
|
21
|
+
import { lookup as dnsLookup } from 'node:dns/promises';
|
|
22
|
+
|
|
23
|
+
/** 把 IPv6 展开成 8 个 16 位整数;不是合法 IPv6 返回 null */
|
|
24
|
+
function expandV6(ip) {
|
|
25
|
+
const s = String(ip ?? '').toLowerCase().split('%')[0];
|
|
26
|
+
if (isIP(s) !== 6) return null;
|
|
27
|
+
/** 尾部可能是点分 IPv4(`::ffff:127.0.0.1`),先换成两组十六进制 */
|
|
28
|
+
let body = s;
|
|
29
|
+
const tail = /(\d+\.\d+\.\d+\.\d+)$/.exec(body);
|
|
30
|
+
if (tail) {
|
|
31
|
+
const p = tail[1].split('.').map(Number);
|
|
32
|
+
if (p.length !== 4 || p.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) return null;
|
|
33
|
+
const hi = ((p[0] << 8) | p[1]).toString(16);
|
|
34
|
+
const lo = ((p[2] << 8) | p[3]).toString(16);
|
|
35
|
+
body = `${body.slice(0, -tail[1].length)}${hi}:${lo}`;
|
|
36
|
+
}
|
|
37
|
+
const [head, rest] = body.includes('::') ? body.split('::') : [body, null];
|
|
38
|
+
const a = head ? head.split(':') : [];
|
|
39
|
+
const b = rest ? rest.split(':') : [];
|
|
40
|
+
if (rest !== null && b.length && b[b.length - 1] === '') b.pop();
|
|
41
|
+
const fill = rest === null ? 0 : 8 - a.length - b.length;
|
|
42
|
+
if (fill < 0) return null;
|
|
43
|
+
const groups = [...a, ...Array(rest === null ? 0 : fill).fill('0'), ...b];
|
|
44
|
+
if (groups.length !== 8) return null;
|
|
45
|
+
const out = groups.map((g) => parseInt(g, 16));
|
|
46
|
+
return out.every((n) => Number.isInteger(n) && n >= 0 && n <= 0xffff) ? out : null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 取出 IPv6 里**编进去的 IPv4**;没有就返回 ''。
|
|
51
|
+
*
|
|
52
|
+
* 四种形态都要认,否则一条 IPv6 就能绕过全部 IPv4 判断:
|
|
53
|
+
* `::ffff:a.b.c.d`(IPv4-mapped)、`::a.b.c.d`(IPv4-compatible,`::/96`,已废弃但仍可能被路由)、
|
|
54
|
+
* `64:ff9b::/96`(NAT64,NAT64 网关会把它翻回 IPv4)、`2002::/16`(6to4,前 32 位就是 IPv4)
|
|
55
|
+
*/
|
|
56
|
+
function embeddedV4(ip) {
|
|
57
|
+
const h = expandV6(ip);
|
|
58
|
+
if (!h) return '';
|
|
59
|
+
const v4 = (hi, lo) => `${(hi >>> 8) & 255}.${hi & 255}.${(lo >>> 8) & 255}.${lo & 255}`;
|
|
60
|
+
const zeros = (from, to) => h.slice(from, to).every((n) => n === 0);
|
|
61
|
+
/** ::ffff:a.b.c.d —— 前 5 组为 0、第 6 组是 ffff */
|
|
62
|
+
if (zeros(0, 5) && h[5] === 0xffff) return v4(h[6], h[7]);
|
|
63
|
+
/** ::a.b.c.d —— 前 6 组全 0(`::` 与 `::1` 由调用方先排除) */
|
|
64
|
+
if (zeros(0, 6)) return v4(h[6], h[7]);
|
|
65
|
+
/** 64:ff9b::/96 */
|
|
66
|
+
if (h[0] === 0x64 && h[1] === 0xff9b && zeros(2, 6)) return v4(h[6], h[7]);
|
|
67
|
+
/** 2002::/16 —— 6to4 把 IPv4 放在第 2、3 组 */
|
|
68
|
+
if (h[0] === 0x2002) return v4(h[1], h[2]);
|
|
69
|
+
return '';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** IPv4 / IPv6 的私有、回环、链路本地、保留、组播地址 */
|
|
73
|
+
export function isPrivateIp(ip) {
|
|
74
|
+
const s = String(ip ?? '').toLowerCase().split('%')[0];
|
|
75
|
+
if (!s) return true; // 空地址一律当不安全
|
|
76
|
+
if (isIP(s) === 4) {
|
|
77
|
+
const p = s.split('.').map(Number);
|
|
78
|
+
const [a, b] = p;
|
|
79
|
+
return a === 0 || a === 10 || a === 127
|
|
80
|
+
|| (a === 169 && b === 254) // 链路本地 / 云元数据
|
|
81
|
+
|| (a === 172 && b >= 16 && b <= 31)
|
|
82
|
+
|| (a === 192 && b === 168)
|
|
83
|
+
|| (a === 100 && b >= 64 && b <= 127) // CGNAT
|
|
84
|
+
|| (a === 192 && b === 0 && p[2] === 0)
|
|
85
|
+
|| (a === 198 && (b === 18 || b === 19))
|
|
86
|
+
|| a >= 224; // 组播 + 保留
|
|
87
|
+
}
|
|
88
|
+
if (isIP(s) === 6) {
|
|
89
|
+
if (s === '::' || s === '::1') return true;
|
|
90
|
+
/** 编了 IPv4 的形态一律按里面那个 IPv4 判(映射 / 兼容 / NAT64 / 6to4) */
|
|
91
|
+
const v4 = embeddedV4(s);
|
|
92
|
+
if (v4) return isPrivateIp(v4);
|
|
93
|
+
/** 落在 `::/96` 却认不出映射目标 → fail-closed,别再放行 */
|
|
94
|
+
if (s.startsWith('::')) return true;
|
|
95
|
+
const head = s.split(':')[0];
|
|
96
|
+
if (/^f[cd]/.test(head)) return true; // fc00::/7 唯一本地
|
|
97
|
+
if (/^fe[89ab]/.test(head)) return true; // fe80::/10 链路本地
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 校验一个出站地址。不合法就抛,消息直接给用户看(中文、说清哪一条不过)。
|
|
105
|
+
*
|
|
106
|
+
* @param {string} raw
|
|
107
|
+
* @param {{lookup?:Function}} [o] `lookup` 只为测试注入,生产用系统解析
|
|
108
|
+
* @returns {Promise<URL>}
|
|
109
|
+
*/
|
|
110
|
+
export async function assertSafeHttpUrl(raw, { lookup = dnsLookup } = {}) {
|
|
111
|
+
let u;
|
|
112
|
+
try {
|
|
113
|
+
u = new URL(String(raw));
|
|
114
|
+
} catch {
|
|
115
|
+
throw new Error('不是合法的地址');
|
|
116
|
+
}
|
|
117
|
+
if (u.protocol !== 'http:' && u.protocol !== 'https:') {
|
|
118
|
+
throw new Error('只允许 http / https 地址');
|
|
119
|
+
}
|
|
120
|
+
if (u.username || u.password) {
|
|
121
|
+
throw new Error('地址里不许带账号密码');
|
|
122
|
+
}
|
|
123
|
+
const host = u.hostname.replace(/^\[|\]$/g, '');
|
|
124
|
+
if (!host) throw new Error('地址缺少主机名');
|
|
125
|
+
|
|
126
|
+
if (isIP(host)) {
|
|
127
|
+
if (isPrivateIp(host)) throw new Error('不允许访问内网 / 本机地址');
|
|
128
|
+
return u;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let addrs = [];
|
|
132
|
+
try {
|
|
133
|
+
addrs = await lookup(host, { all: true });
|
|
134
|
+
} catch (e) {
|
|
135
|
+
throw new Error(`解析不了这个域名(${e?.message ?? e})`);
|
|
136
|
+
}
|
|
137
|
+
if (!Array.isArray(addrs) || !addrs.length) throw new Error('这个域名没有解析结果');
|
|
138
|
+
/** 任一地址私有就拒:不能「有公网就放行」——那正是 DNS rebinding 的用法 */
|
|
139
|
+
for (const a of addrs) {
|
|
140
|
+
if (isPrivateIp(a?.address)) throw new Error('这个域名指向内网 / 本机地址,已拒绝');
|
|
141
|
+
}
|
|
142
|
+
return u;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 读响应体,超过上限立刻中止(避免无 content-length 的流把内存顶满) */
|
|
146
|
+
async function readBodyLimited(res, maxBytes) {
|
|
147
|
+
const reader = res.body?.getReader?.();
|
|
148
|
+
if (!reader) {
|
|
149
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
150
|
+
if (buf.length > maxBytes) {
|
|
151
|
+
throw new Error(`这张 ${(buf.length / 1024 / 1024).toFixed(1)} MB,超过 ${maxBytes / 1024 / 1024} MB 上限`);
|
|
152
|
+
}
|
|
153
|
+
return buf;
|
|
154
|
+
}
|
|
155
|
+
const chunks = [];
|
|
156
|
+
let total = 0;
|
|
157
|
+
for (;;) {
|
|
158
|
+
const { done, value } = await reader.read();
|
|
159
|
+
if (done) break;
|
|
160
|
+
total += value?.byteLength ?? 0;
|
|
161
|
+
if (total > maxBytes) {
|
|
162
|
+
try { await reader.cancel(); } catch { /* 已经断了就算了 */ }
|
|
163
|
+
throw new Error(`这张超过 ${maxBytes / 1024 / 1024} MB 上限`);
|
|
164
|
+
}
|
|
165
|
+
chunks.push(Buffer.from(value));
|
|
166
|
+
}
|
|
167
|
+
return Buffer.concat(chunks);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 取一张图:逐跳校验地址、手动跟重定向、按上限流式读取。
|
|
172
|
+
*
|
|
173
|
+
* @param {string} raw
|
|
174
|
+
* @param {{maxBytes:number, fetchImpl?:Function, lookup?:Function,
|
|
175
|
+
* timeoutMs?:number, maxRedirects?:number}} o
|
|
176
|
+
* @returns {Promise<{buf:Buffer, type:string, finalUrl:string}>}
|
|
177
|
+
*/
|
|
178
|
+
export async function fetchImageLimited(raw, {
|
|
179
|
+
maxBytes,
|
|
180
|
+
fetchImpl = fetch,
|
|
181
|
+
lookup = dnsLookup,
|
|
182
|
+
timeoutMs = 15_000,
|
|
183
|
+
maxRedirects = 3,
|
|
184
|
+
} = {}) {
|
|
185
|
+
let cur = String(raw);
|
|
186
|
+
for (let hop = 0; hop <= maxRedirects; hop += 1) {
|
|
187
|
+
await assertSafeHttpUrl(cur, { lookup });
|
|
188
|
+
const res = await fetchImpl(cur, {
|
|
189
|
+
redirect: 'manual',
|
|
190
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
191
|
+
});
|
|
192
|
+
if (res.status >= 300 && res.status < 400) {
|
|
193
|
+
const loc = res.headers.get('location');
|
|
194
|
+
if (!loc) throw new Error(`取图被重定向(HTTP ${res.status})但没给目标地址`);
|
|
195
|
+
/** 相对地址要按当前跳解析;下一轮会再校验它是否私有 */
|
|
196
|
+
cur = new URL(loc, cur).href;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (!res.ok) throw new Error(`取图失败(HTTP ${res.status})`);
|
|
200
|
+
const type = String(res.headers.get('content-type') || '').split(';')[0].trim().toLowerCase();
|
|
201
|
+
const declared = Number(res.headers.get('content-length') || 0);
|
|
202
|
+
if (declared > maxBytes) {
|
|
203
|
+
throw new Error(`这张 ${(declared / 1024 / 1024).toFixed(1)} MB,超过 ${maxBytes / 1024 / 1024} MB 上限`);
|
|
204
|
+
}
|
|
205
|
+
const buf = await readBodyLimited(res, maxBytes);
|
|
206
|
+
return { buf, type, finalUrl: cur };
|
|
207
|
+
}
|
|
208
|
+
throw new Error('重定向次数太多,已放弃');
|
|
209
|
+
}
|
package/src/endpoints.js
CHANGED
|
@@ -147,6 +147,7 @@ export const OFFICIAL_ENDPOINTS = [
|
|
|
147
147
|
|
|
148
148
|
/* ---------------- 用户 / 机器人 ---------------- */
|
|
149
149
|
{ method: 'GET', path: '/users/@me', api: 'getMe', group: '用户' },
|
|
150
|
+
{ method: 'GET', path: '/v2/users/{user_openid}/info', api: 'getUserInfo', group: '用户', note: '2026-11-16 新增,10 QPS,需好友关系' },
|
|
150
151
|
{ method: 'GET', path: '/users/@me/guilds', api: 'getMyGuilds', group: '用户' },
|
|
151
152
|
|
|
152
153
|
/* ---------------- 网关 ---------------- */
|
|
@@ -156,7 +157,7 @@ export const OFFICIAL_ENDPOINTS = [
|
|
|
156
157
|
{ method: 'POST', path: '/v2/generate_url_link', api: 'generateUrlLink', group: '分享链接', note: 'callback_data ≤ 32 字符' },
|
|
157
158
|
|
|
158
159
|
/* ---------------- 群信息 ---------------- */
|
|
159
|
-
{ method: 'GET', path: '/v2/groups/{group_openid}/info', api: 'getGroupInfo', group: '群·信息' },
|
|
160
|
+
{ method: 'GET', path: '/v2/groups/{group_openid}/info', api: 'getGroupInfo', group: '群·信息', note: '30 QPM' },
|
|
160
161
|
{ method: 'GET', path: '/v2/groups/{group_openid}/bot_state', api: 'getBotGroupState', group: '群·信息', note: '内邀' },
|
|
161
162
|
|
|
162
163
|
/* ---------------- 群成员 ---------------- */
|
package/src/groups.js
CHANGED
|
@@ -16,7 +16,9 @@ const log = createLogger('groups');
|
|
|
16
16
|
* {
|
|
17
17
|
* "<group_openid>": {
|
|
18
18
|
* groupOpenid, inviterOpenid, ownerOpenid,
|
|
19
|
-
* enabled, addedAt, lastActiveAt, note
|
|
19
|
+
* enabled, addedAt, lastActiveAt, note,
|
|
20
|
+
* // 2026-11-16 起:群基本信息(展示用)
|
|
21
|
+
* name, memberNum, fingerMemo, classText, tags, infoAt
|
|
20
22
|
*
|
|
21
23
|
* 注意:这里**不存权限**。谁能在本群做什么,统一看 roles.json 的 group:<id> 作用域。
|
|
22
24
|
* 群档案只负责「这个群是什么情况」。
|
|
@@ -85,6 +87,20 @@ export class GroupStore {
|
|
|
85
87
|
addedAt: Date.now(),
|
|
86
88
|
lastActiveAt: 0,
|
|
87
89
|
note: '',
|
|
90
|
+
/**
|
|
91
|
+
* 群基本信息(2026-11-16 官方补齐):名字 / 人数 / 简介 / 分类 / 标签。
|
|
92
|
+
*
|
|
93
|
+
* 都是展示用,拿不到就是空值。**不存 `union_group_openid`**——
|
|
94
|
+
* 跨机器人的群关联一律走 `identity` 的「群号 ↔ openid」绑定,
|
|
95
|
+
* 那个字段只在同一开发者 + 同一个 3 方应用下才有值,还换不出对方的
|
|
96
|
+
* openid,留着只会让人以为它能当群 id 用。
|
|
97
|
+
*/
|
|
98
|
+
name: '',
|
|
99
|
+
memberNum: 0,
|
|
100
|
+
fingerMemo: '',
|
|
101
|
+
classText: '',
|
|
102
|
+
tags: [],
|
|
103
|
+
infoAt: 0,
|
|
88
104
|
};
|
|
89
105
|
d[groupOpenid] = { ...cur, ...patch, groupOpenid };
|
|
90
106
|
this.#scheduleSave();
|
package/src/host/methods.js
CHANGED
|
@@ -20,6 +20,7 @@ import { syncNickname } from '../services/nickname.js';
|
|
|
20
20
|
import { INTENTS, INTENT_META, EVENTS } from '../events.js';
|
|
21
21
|
import { CHANNEL_DAILY_ACTIVE } from '../outbound.js';
|
|
22
22
|
import { registerMedia } from './media.js';
|
|
23
|
+
import { splitQuery } from '../search-query.js';
|
|
23
24
|
import { registerOps } from './ops.js';
|
|
24
25
|
import { registerDev } from './dev.js';
|
|
25
26
|
import { sendOut, receivedAtOf, isChatKind, CHAT_KINDS } from './dispatch.js';
|
|
@@ -267,6 +268,157 @@ export function createMethods({ runtime: R, getState, log = () => {}, extBridge
|
|
|
267
268
|
});
|
|
268
269
|
});
|
|
269
270
|
|
|
271
|
+
/**
|
|
272
|
+
* 围绕某条消息的前后文(按 `seq`)。
|
|
273
|
+
*
|
|
274
|
+
* 模型搜到一条之后想看「当时到底怎么聊的」,但它手里只有 `seq`——
|
|
275
|
+
* 所以先用 `bySeq` 定位这条(拿到 chatId),再取邻文。等价于面板上
|
|
276
|
+
* 「点一条看上下文」。
|
|
277
|
+
*
|
|
278
|
+
* **`anchor` 会一起返回**:宿主侧要靠 `anchor.userId` 判「非特权用户是不是
|
|
279
|
+
* 在翻别人的对话」——这个判断不能只在框架里做一半,因为「谁能看谁」是
|
|
280
|
+
* 会话身份的事,只有宿主知道。
|
|
281
|
+
*/
|
|
282
|
+
def('ledger.around', 'read', '取某条消息的前后文(按 seq)', async (p) => {
|
|
283
|
+
if (!R.ledger) throw notReady('账本未就绪');
|
|
284
|
+
const seq = Math.max(1, Number(p.seq) || 0);
|
|
285
|
+
if (!seq) throw bad('要给 seq(搜索结果里每条都带)');
|
|
286
|
+
|
|
287
|
+
const anchor = R.ledger.bySeq?.(seq) ?? null;
|
|
288
|
+
if (!anchor) throw bad(`账本里没有 seq=${seq} 这条记录`);
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* `before` / `after`:缺省或非法(负数、NaN)都退默认 3;`0` 是合法值
|
|
292
|
+
* (表示「只要后面 / 只要前面」),上限 20。
|
|
293
|
+
*/
|
|
294
|
+
const clampAround = (v, dft = 3) => {
|
|
295
|
+
const n = Number(v);
|
|
296
|
+
if (!Number.isFinite(n) || n < 0) return dft;
|
|
297
|
+
return Math.min(20, Math.floor(n));
|
|
298
|
+
};
|
|
299
|
+
const before = clampAround(p.before);
|
|
300
|
+
const after = clampAround(p.after);
|
|
301
|
+
const items = R.ledger.timeline?.({
|
|
302
|
+
chatId: anchor.chatId,
|
|
303
|
+
anchorSeq: seq,
|
|
304
|
+
before,
|
|
305
|
+
after,
|
|
306
|
+
}) ?? [];
|
|
307
|
+
|
|
308
|
+
return {
|
|
309
|
+
anchor: {
|
|
310
|
+
seq: anchor.seq,
|
|
311
|
+
chatId: anchor.chatId,
|
|
312
|
+
kind: anchor.kind,
|
|
313
|
+
userId: anchor.userId,
|
|
314
|
+
at: anchor.at,
|
|
315
|
+
},
|
|
316
|
+
items,
|
|
317
|
+
};
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* 跨会话搜索账本。
|
|
322
|
+
*
|
|
323
|
+
* 宿主(dsh-roost)拿它给模型做「翻以前说过什么」——这是**唯一**能让模型
|
|
324
|
+
* 接触到任意历史消息的口子,所以两件事必须在这里做对:
|
|
325
|
+
*
|
|
326
|
+
* · 参数一律白名单化:`terms` 最多 8 个、`openids` 最多 20 个、`limit` 封顶 200;
|
|
327
|
+
* · **权限不在这里判**。host 协议是本地受信进程(token + read scope),
|
|
328
|
+
* 它天然能读全库;「一个人只能查自己」是**宿主侧**按会话里的发送者身份
|
|
329
|
+
* 收窄的(见 dsh-roost 的 `search-scope.js`)。这里如果也判一遍,
|
|
330
|
+
* 就会变成「两个地方各有一套规则」,迟早分叉。
|
|
331
|
+
*
|
|
332
|
+
* `person` 容三种输入(openid / QQ号 / 昵称),解析口径与面板一致:
|
|
333
|
+
* QQ 号只有绑定过才反查得到 openid,没绑过就当昵称去碰运气。
|
|
334
|
+
*/
|
|
335
|
+
def('ledger.search', 'read', '跨会话搜索账本(关键词 / 发言人 / 类型 / 时间)', async (p) => {
|
|
336
|
+
if (!R.ledger) throw notReady('账本未就绪');
|
|
337
|
+
|
|
338
|
+
const terms = (Array.isArray(p.terms) ? p.terms : [])
|
|
339
|
+
.map((x) => str(x).trim())
|
|
340
|
+
.filter(Boolean)
|
|
341
|
+
.slice(0, 8);
|
|
342
|
+
/** 只给了 `q`(一整句)时用同一套词法拆:剥疑问词、单字丢弃 */
|
|
343
|
+
const q = str(p.q).trim();
|
|
344
|
+
const finalTerms = terms.length ? terms : splitQuery(q);
|
|
345
|
+
|
|
346
|
+
const openids = (Array.isArray(p.openids) ? p.openids : [])
|
|
347
|
+
.map((x) => str(x).trim())
|
|
348
|
+
.filter(Boolean)
|
|
349
|
+
.slice(0, 20);
|
|
350
|
+
let name = str(p.name).trim();
|
|
351
|
+
|
|
352
|
+
const person = str(p.person).trim();
|
|
353
|
+
if (person) {
|
|
354
|
+
if (/^[0-9A-Za-z_-]{16,}$/.test(person) && /[A-Za-z]/.test(person)) {
|
|
355
|
+
openids.push(person);
|
|
356
|
+
} else if (/^\d{5,12}$/.test(person)) {
|
|
357
|
+
const opid = R.identity?.openidOf?.(R.id, person) ?? '';
|
|
358
|
+
if (opid) openids.push(String(opid));
|
|
359
|
+
else name = person;
|
|
360
|
+
} else {
|
|
361
|
+
name = person;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const kind = str(p.kind).trim();
|
|
366
|
+
/** 只支持单会话精确匹配:多会话由宿主各查一次再合并,别在这里做笛卡尔积 */
|
|
367
|
+
const chatId = str(p.chatId).trim();
|
|
368
|
+
const since = Math.max(0, Number(p.since) || 0);
|
|
369
|
+
const beforeSeq = Math.max(0, Number(p.beforeSeq) || 0);
|
|
370
|
+
const limit = Math.max(1, Math.min(200, Number(p.limit) || 20));
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* 只给了「什么 / 怎么 / 吗」这类词时,拆完一个关键词都不剩。
|
|
374
|
+
*
|
|
375
|
+
* 这时候**不能**当成「没有关键词」去跑:SQL 里只剩 `bot_id = ?`,
|
|
376
|
+
* 返回的是最新 N 条,模型会以为「搜到了」——面板上已经踩过这个坑。
|
|
377
|
+
* 直接回空列表 + 说明。
|
|
378
|
+
*/
|
|
379
|
+
if (q && !finalTerms.length && !openids.length && !name && !kind && !chatId) {
|
|
380
|
+
return {
|
|
381
|
+
items: [],
|
|
382
|
+
terms: [],
|
|
383
|
+
relaxed: false,
|
|
384
|
+
hasMore: false,
|
|
385
|
+
note: '关键词里没有可检索的内容(全是疑问词/语气词),换个实义词试试',
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const base = {
|
|
390
|
+
terms: finalTerms,
|
|
391
|
+
openids,
|
|
392
|
+
name,
|
|
393
|
+
kind,
|
|
394
|
+
since,
|
|
395
|
+
beforeSeq,
|
|
396
|
+
limit,
|
|
397
|
+
withPeople: true,
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const run = (extra) => R.ledger.search({ ...base, ...extra });
|
|
401
|
+
|
|
402
|
+
let rows = run({ chatId, mode: 'and' });
|
|
403
|
+
let relaxed = false;
|
|
404
|
+
/** 多词全中 0 条时放宽成任一命中,和面板同一条策略 */
|
|
405
|
+
if (!rows.length && finalTerms.length > 1) {
|
|
406
|
+
rows = run({ chatId, mode: 'or' });
|
|
407
|
+
relaxed = rows.length > 0;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
return {
|
|
411
|
+
items: rows.map((r) => ({
|
|
412
|
+
...r,
|
|
413
|
+
openid: String(r.userId ?? ''),
|
|
414
|
+
chatName: r.kind === 'group' ? String(R.groupName?.(r.chatId) ?? '') : '',
|
|
415
|
+
})),
|
|
416
|
+
terms: finalTerms,
|
|
417
|
+
relaxed,
|
|
418
|
+
hasMore: rows.length >= limit,
|
|
419
|
+
};
|
|
420
|
+
});
|
|
421
|
+
|
|
270
422
|
/* ---------------- 官方 API 全量透传 ---------------- */
|
|
271
423
|
|
|
272
424
|
def('qq.methods', 'read', '列出 qq.call 可透传的方法、风险档与所需 scope', async () => ({
|
|
@@ -565,6 +717,34 @@ export function createMethods({ runtime: R, getState, log = () => {}, extBridge
|
|
|
565
717
|
return out;
|
|
566
718
|
};
|
|
567
719
|
|
|
720
|
+
/**
|
|
721
|
+
* 最近在这个机器人这儿说过话的人(内存索引)。
|
|
722
|
+
*
|
|
723
|
+
* 为什么需要:群成员资料本应「公开」,但官方 `getUserInfo` 要求**好友关系**
|
|
724
|
+
* (非好友回 `40184001`),而群成员绝大多数不是好友——只看官方接口的话,
|
|
725
|
+
* 「刚才那位是谁」永远查不到。`recentPeople` 是框架在每条消息上攒下来的
|
|
726
|
+
* (昵称 / 群名片 / 头像 / QQ号),正好补这个洞。
|
|
727
|
+
*
|
|
728
|
+
* 只读、只含「说过话的人」、不落盘:重启后为空,有人说话又会填上。
|
|
729
|
+
*/
|
|
730
|
+
def('people.list', 'read', '最近说过话的人(昵称/群名片/头像/QQ号),给宿主认人用', async (p) => {
|
|
731
|
+
const limit = Math.min(Math.max(Number(p.limit) || 200, 1), 200);
|
|
732
|
+
const list = R.listRecentPeople ? R.listRecentPeople(limit) : [];
|
|
733
|
+
return {
|
|
734
|
+
people: (Array.isArray(list) ? list : []).map((x) => ({
|
|
735
|
+
openid: str(x.openid),
|
|
736
|
+
name: str(x.name),
|
|
737
|
+
card: str(x.card),
|
|
738
|
+
nickname: str(x.nickname),
|
|
739
|
+
avatar: str(x.avatar),
|
|
740
|
+
uin: str(x.uin),
|
|
741
|
+
unionOpenid: str(x.unionOpenid),
|
|
742
|
+
scope: str(x.scope),
|
|
743
|
+
at: Number(x.at) || 0,
|
|
744
|
+
})),
|
|
745
|
+
};
|
|
746
|
+
});
|
|
747
|
+
|
|
568
748
|
def('group.info', 'read', '群信息 + 机器人在群里的角色 + 能不能管(没内邀也能读公开信息)', async (p) => {
|
|
569
749
|
if (!R.api) throw notReady('官方接口未就绪');
|
|
570
750
|
const groupOpenid = str(p.groupOpenid).trim();
|
|
@@ -581,7 +761,17 @@ export function createMethods({ runtime: R, getState, log = () => {}, extBridge
|
|
|
581
761
|
return {
|
|
582
762
|
groupOpenid,
|
|
583
763
|
name: str(info?.group_name ?? info?.name),
|
|
584
|
-
|
|
764
|
+
/**
|
|
765
|
+
* 官方字段是 `group_member_num`(2026-11-16 文档)。
|
|
766
|
+
* 以前这里读 `member_count` / `max_member_count`——两个都不存在,
|
|
767
|
+
* 所以群人数**恒为 0**:面板和模型问「这个群多少人」永远答 0,
|
|
768
|
+
* 不报错、也不缺字段,属于最难发现的那种错。
|
|
769
|
+
*/
|
|
770
|
+
memberCount: Number(info?.group_member_num ?? info?.member_count ?? 0) || 0,
|
|
771
|
+
/** 简介 / 分类 / 标签:2026-11-16 起群信息接口才给,用来回答「这是个什么群」 */
|
|
772
|
+
memo: str(info?.group_finger_memo),
|
|
773
|
+
classText: str(info?.group_class_text),
|
|
774
|
+
tags: Array.isArray(info?.group_tags) ? info.group_tags.map(String).filter(Boolean) : [],
|
|
585
775
|
role: manage.role ?? null,
|
|
586
776
|
canManage: manage.ok === true,
|
|
587
777
|
manageReason: str(manage.reason),
|
package/src/host/serialize.js
CHANGED
|
@@ -33,6 +33,11 @@ export function serializeMessage(msg, { groupName = '' } = {}) {
|
|
|
33
33
|
groupUin: msg.groupUin ?? '',
|
|
34
34
|
groupIdentityId: msg.groupIdentityId ?? '',
|
|
35
35
|
groupAliases: Array.isArray(msg.groupAliases) ? msg.groupAliases : [],
|
|
36
|
+
/**
|
|
37
|
+
* 官方 2026-11-16 新加的跨应用群标识,**只透传**。它换不出对方机器人的
|
|
38
|
+
* openid,发消息只认 `groupId`;跨机器人关联走 identity 的群号绑定。
|
|
39
|
+
*/
|
|
40
|
+
unionGroupId: msg.unionGroupId ?? '',
|
|
36
41
|
name: groupName,
|
|
37
42
|
},
|
|
38
43
|
sender: {
|
|
@@ -40,7 +45,17 @@ export function serializeMessage(msg, { groupName = '' } = {}) {
|
|
|
40
45
|
uin: msg.uin ?? '',
|
|
41
46
|
identityId: msg.identityId ?? '',
|
|
42
47
|
aliases: Array.isArray(msg.userAliases) ? msg.userAliases : [],
|
|
48
|
+
/**
|
|
49
|
+
* 显示名(群聊取群内昵称)。`nickname` / `card` 两个原始值也带上:
|
|
50
|
+
* 客户端要「群名片」和「QQ 昵称」分开显示时,用 name 是分不出来的。
|
|
51
|
+
*/
|
|
43
52
|
name: msg.userName ?? '',
|
|
53
|
+
nickname: msg.userNickname ?? '',
|
|
54
|
+
card: msg.userCard ?? '',
|
|
55
|
+
/** 头像原图 URL;官方没给就是空串(别拼默认头像,那是猜) */
|
|
56
|
+
avatar: msg.userAvatar ?? '',
|
|
57
|
+
/** 跨应用统一用户 id,绑了 3 方应用才有 */
|
|
58
|
+
unionOpenid: msg.unionUserId ?? '',
|
|
44
59
|
isBot: msg.raw?.author?.bot === true,
|
|
45
60
|
},
|
|
46
61
|
msgId: msg.id ?? '',
|