111 lines
5.6 KiB
JavaScript
111 lines
5.6 KiB
JavaScript
// 私聊段类型矩阵的第三证据补齐:用 get_friend_msg_history 回读。
|
||||
|
|
//
|
|||
|
|
// 此前私聊用例(p_text / p_face / p_image_b64 / p_json / p_reply / p_at / p_upload_file)
|
|||
|
|
// 只有「API retcode + 事件回推段」两条证据,缺 history 回读,
|
|||
|
|
// 因为 get_group_msg_history 不适用于私聊。本脚本改用 get_friend_msg_history,
|
|||
|
|
// 纯只读,不发送任何消息,把已有事件的段类型与私聊 history 的段类型逐条对齐。
|
|||
|
|
//
|
|||
|
|
// 判定口径与三证据矩阵一致:retcode=0 不等于送达,必须看回读段。
|
|||
|
|
// 凭据只从环境变量读取。
|
|||
|
|
|
|||
|
|
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 FRIEND = Number(process.env.PROBE_FRIEND || 2161656029);
|
|||
|
|
const OUT_DIR = process.env.PROBE_OUT || 'local';
|
|||
|
|
const EVENT_FILE = process.env.PROBE_EV || 'local/matrix-ev-2026-10-29T06-48-10-358Z.jsonl';
|
|||
|
|
if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); }
|
|||
|
|
|
|||
|
|
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|||
|
|
fs.mkdirSync(OUT_DIR, { recursive: true });
|
|||
|
|
const OUT = path.join(OUT_DIR, `private-history-audit-${stamp}.jsonl`);
|
|||
|
|
const say = (...a) => console.log(...a);
|
|||
|
|
|
|||
|
|
const ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`);
|
|||
|
|
const pending = new Map();
|
|||
|
|
let opened = false, authed = false;
|
|||
|
|
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); return; }
|
|||
|
|
if (o.post_type === 'meta_event' && o.meta_event_type === 'lifecycle') authed = true;
|
|||
|
|
};
|
|||
|
|
for (let i = 0; i < 100 && !opened; i++) await new Promise((r) => setTimeout(r, 100));
|
|||
|
|
if (!opened) { console.error('连接超时'); process.exit(1); }
|
|||
|
|
for (let i = 0; i < 100 && !authed; i++) await new Promise((r) => setTimeout(r, 100));
|
|||
|
|
say('认证=' + authed);
|
|||
|
|
|
|||
|
|
const api = (action, params, ms = 40000) => new Promise((resolve) => {
|
|||
|
|
const echo = `ph-${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 }));
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 1) 从已落盘的事件样本里提取私聊自发送消息的标记与段类型
|
|||
|
|
const segsOf = (o) => (Array.isArray(o.message) ? o.message : []).map((s) => s.type).join('+');
|
|||
|
|
const labelOf = (o) => {
|
|||
|
|
const t = (Array.isArray(o.message) ? o.message : [])
|
|||
|
|
.filter((s) => s.type === 'text').map((s) => s.data?.text ?? '').join('');
|
|||
|
|
return (t.match(/\[QLm?:([a-z0-9_]+)\]/i) || [])[1] ?? null;
|
|||
|
|
};
|
|||
|
|
const evById = new Map();
|
|||
|
|
if (fs.existsSync(EVENT_FILE)) {
|
|||
|
|
for (const l of fs.readFileSync(EVENT_FILE, 'utf8').trim().split('\n')) {
|
|||
|
|
let o; try { o = JSON.parse(l); } catch { continue; }
|
|||
|
|
const d = o.data ?? o;
|
|||
|
|
if (d.post_type !== 'message_sent' && d.message_type !== 'private') continue;
|
|||
|
|
if (d.message_type !== 'private') continue;
|
|||
|
|
const lab = labelOf(d);
|
|||
|
|
if (lab) evById.set(lab, { segs: segsOf(d), message_id: d.message_id ?? null, seq: d.real_seq ?? null });
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
say(`事件样本中的私聊消息 ${evById.size} 条:${[...evById.keys()].join(', ') || '(无)'}`);
|
|||
|
|
|
|||
|
|
// 2) 只读回读私聊 history
|
|||
|
|
const h = await api('get_friend_msg_history', { user_id: FRIEND, count: Number(process.env.PROBE_COUNT || 60) });
|
|||
|
|
if (h.__timeout || h.retcode !== 0) {
|
|||
|
|
say('get_friend_msg_history 失败:' + JSON.stringify(h).slice(0, 200));
|
|||
|
|
try { ws.close(); } catch {} process.exit(2);
|
|||
|
|
}
|
|||
|
|
const msgs = h?.data?.messages ?? [];
|
|||
|
|
say(`私聊 history 回读 ${msgs.length} 条`);
|
|||
|
|
fs.appendFileSync(OUT, JSON.stringify({ kind: 'history_raw', count: msgs.length }) + '\n');
|
|||
|
|
for (const m of msgs) {
|
|||
|
|
fs.appendFileSync(OUT, JSON.stringify({ kind: 'history_msg', seq: m.real_seq,
|
|||
|
|
message_id: m.message_id, segs: segsOf(m), raw: String(m.raw_message ?? '').slice(0, 300),
|
|||
|
|
message: m.message }) + '\n');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3) 逐条对齐:事件段 vs history 段
|
|||
|
|
const histByLabel = new Map();
|
|||
|
|
for (const m of msgs) {
|
|||
|
|
const lab = labelOf(m);
|
|||
|
|
if (lab && !histByLabel.has(lab)) histByLabel.set(lab, { segs: segsOf(m), seq: m.real_seq, message_id: m.message_id });
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
say('\n--- 私聊三证据对齐(事件段 vs history 回读段)---');
|
|||
|
|
const rows = [];
|
|||
|
|
let hit = 0, total = 0;
|
|||
|
|
for (const [lab, ev] of evById) {
|
|||
|
|
total++;
|
|||
|
|
const hb = histByLabel.get(lab);
|
|||
|
|
if (!hb) { rows.push({ id: lab, event_segs: ev.segs, readback_segs: null, verdict: 'readback_gap' });
|
|||
|
|
say(` ${lab.padEnd(16)} 事件段=${ev.segs.padEnd(18)} history=未命中 → 回读缺口`); continue; }
|
|||
|
|
const same = ev.segs === hb.segs;
|
|||
|
|
if (same) hit++;
|
|||
|
|
rows.push({ id: lab, event_segs: ev.segs, readback_segs: hb.segs, seq: hb.seq,
|
|||
|
|
verdict: same ? 'consistent' : 'seg_mismatch' });
|
|||
|
|
// 与 history-audit / record-amr 探针保持同一字段口径,使 capability-check 能自动比对 history 列
|
|||
|
|
fs.appendFileSync(OUT, JSON.stringify({ kind: 'history', id: lab, event_segs: ev.segs,
|
|||
|
|
history_segs: hb.segs, history_seq: hb.seq }) + '\n');
|
|||
|
|
say(` ${lab.padEnd(16)} 事件段=${ev.segs.padEnd(18)} history=${hb.segs.padEnd(18)} seq=${hb.seq} ${same ? '一致' : '★段不一致'}`);
|
|||
|
|
}
|
|||
|
|
say(`\n命中并对齐 ${hit}/${total}(history 中未带标记的私聊消息不参与统计)`);
|
|||
|
|
fs.appendFileSync(OUT, JSON.stringify({ __summary: true, rows, hit, total }) + '\n');
|
|||
|
|
say('结果 -> ' + OUT);
|
|||
|
|
try { ws.close(); } catch {}
|
|||
|
|
process.exit(0);
|