96 lines
4.4 KiB
JavaScript
96 lines
4.4 KiB
JavaScript
// 用 message_id 精确回读 history,补齐「API + 事件 + history」第三条证据。
|
||||
|
|
// 矩阵脚本靠 raw_message 里的标记文本匹配会失败:history 的 raw_message 把
|
|||
|
|
// [ ] , 等字符做了实体转义([ ] ,),文本匹配不可靠,改用 message_id 关联。
|
|||
|
|
// 只读接口,无副作用。凭据只从环境变量读取。
|
|||
|
|
//
|
|||
|
|
// 用法:
|
|||
|
|
// NAPCAT_WS_TOKEN=xxx node tools/probe/history-audit.mjs local/matrix-result-xxx.jsonl
|
|||
|
|
|
|||
|
|
import fs from 'node:fs';
|
|||
|
|
import path from 'node:path';
|
|||
|
|
|
|||
|
|
const WS_URL = process.env.NAPCAT_WS_URL || 'ws://192.168.5.32:16099';
|
|||
|
|
const TOKEN = process.env.NAPCAT_WS_TOKEN;
|
|||
|
|
const GROUP = Number(process.env.PROBE_GROUP || 933280401);
|
|||
|
|
const FRIEND = Number(process.env.PROBE_FRIEND || 2161656029);
|
|||
|
|
if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); }
|
|||
|
|
|
|||
|
|
const src = process.argv.slice(2).find((a) => !a.startsWith('--'));
|
|||
|
|
if (!src || !fs.existsSync(src)) { console.error('用法: node history-audit.mjs <matrix-result.jsonl>'); process.exit(1); }
|
|||
|
|
|
|||
|
|
const rows = fs.readFileSync(src, 'utf8').trim().split('\n')
|
|||
|
|
.map((l) => { try { return JSON.parse(l); } catch { return null; } })
|
|||
|
|
.filter((r) => r && r.message_id);
|
|||
|
|
const want = new Map(rows.map((r) => [Number(r.message_id), r]));
|
|||
|
|
console.log(`待回读 message_id ${want.size} 个(来自 ${src})`);
|
|||
|
|
|
|||
|
|
const pending = new Map();
|
|||
|
|
let opened = false;
|
|||
|
|
const ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`);
|
|||
|
|
ws.onopen = () => { opened = true; };
|
|||
|
|
ws.onmessage = (ev) => {
|
|||
|
|
let o; try { o = JSON.parse(String(ev.data)); } catch { return; }
|
|||
|
|
if (o.echo && pending.has(o.echo)) { pending.get(o.echo).resolve(o); pending.delete(o.echo); }
|
|||
|
|
};
|
|||
|
|
for (let i = 0; i < 100 && !opened; i++) await new Promise((r) => setTimeout(r, 100));
|
|||
|
|
if (!opened) { console.error('连接超时'); process.exit(1); }
|
|||
|
|
|
|||
|
|
const api = (action, params, ms = 30000) => new Promise((resolve) => {
|
|||
|
|
const echo = `ha-${action}-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`;
|
|||
|
|
const to = setTimeout(() => { pending.delete(echo); resolve({ __timeout: true }); }, ms);
|
|||
|
|
pending.set(echo, { resolve: (o) => { clearTimeout(to); resolve(o); }, to });
|
|||
|
|
ws.send(JSON.stringify({ action, params, echo }));
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
const segTypes = (m) => Array.isArray(m) ? m.map((s) => s.type).join('+') : '(无)';
|
|||
|
|
const found = new Map();
|
|||
|
|
const rawOf = new Map();
|
|||
|
|
|
|||
|
|
async function pull(action, key, val, rounds = 8) {
|
|||
|
|
let params = { [key]: val, count: 100 };
|
|||
|
|
let pages = 0;
|
|||
|
|
for (let i = 0; i < rounds; i++) {
|
|||
|
|
const r = await api(action, params);
|
|||
|
|
const list = r?.data?.messages ?? [];
|
|||
|
|
pages += 1;
|
|||
|
|
if (!list.length) break;
|
|||
|
|
for (const m of list) {
|
|||
|
|
const id = Number(m.message_id);
|
|||
|
|
if (want.has(id) && !found.has(id)) {
|
|||
|
|
found.set(id, { segs: segTypes(m.message), real_seq: m.real_seq,
|
|||
|
|
empty: Array.isArray(m.message) && m.message.length === 0 });
|
|||
|
|
rawOf.set(id, m);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if ([...want.keys()].every((id) => found.has(id))) break;
|
|||
|
|
const oldest = list[list.length - 1];
|
|||
|
|
if (!oldest?.message_id) break;
|
|||
|
|
params = { [key]: val, count: 100, message_id: Number(oldest.message_id) };
|
|||
|
|
}
|
|||
|
|
return pages;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const pg = await pull('get_group_msg_history', 'group_id', GROUP);
|
|||
|
|
const pf = await pull('get_friend_msg_history', 'user_id', FRIEND);
|
|||
|
|
console.log(`翻页:群 ${pg} 页 / 私聊 ${pf} 页`);
|
|||
|
|
|
|||
|
|
const out = [];
|
|||
|
|
console.log('\n' + '用例'.padEnd(4) + 'id'.padEnd(20) + '事件段'.padEnd(20) + 'history段'.padEnd(20) + 'seq');
|
|||
|
|
for (const r of rows) {
|
|||
|
|
const h = found.get(Number(r.message_id));
|
|||
|
|
const line = { id: r.id, event_segs: r.event_segs, retcode: r.retcode, message_id: r.message_id,
|
|||
|
|
history_segs: h ? h.segs : 'NOT_IN_HISTORY', history_seq: h?.real_seq ?? null,
|
|||
|
|
history_empty: h?.empty ?? null };
|
|||
|
|
out.push(line);
|
|||
|
|
console.log(String(out.length).padEnd(4) + String(r.id).padEnd(20)
|
|||
|
|
+ String(r.event_segs).padEnd(20) + String(line.history_segs).padEnd(20)
|
|||
|
|
+ String(line.history_seq ?? '-'));
|
|||
|
|
}
|
|||
|
|
const dst = path.join(path.dirname(src), `history-audit-${Date.now()}.jsonl`);
|
|||
|
|
fs.writeFileSync(dst, out.map((o) => JSON.stringify(o)).join('\n') + '\n');
|
|||
|
|
fs.writeFileSync(path.join(path.dirname(src), `history-raw-${Date.now()}.jsonl`),
|
|||
|
|
[...rawOf.entries()].map(([id, m]) => JSON.stringify({ message_id: id, msg: m })).join('\n') + '\n');
|
|||
|
|
console.log(`\n命中 ${found.size}/${want.size} -> ${dst}`);
|
|||
|
|
try { ws.close(); } catch {}
|
|||
|
|
process.exit(0);
|