Files

96 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

// record 解码能力的决定性验证(全部只读,不发送任何消息)。
//
// 此前 record 发送侧全部失败,错误为「Failed to open input: Invalid data found」,
// 只能定性为「疑似受 NapCat 运行环境解码能力限制」,但缺少旁证。
// 本脚本改用 NapCat **自己的** get_record(官方文档:获取语音文件信息并支持格式转换):
// 输入使用群里**真实存在**的 record 段的 file / file_id / url / path,
// 若转换成功 → 说明 NapCat 侧 silk/amr 解码链路可用,record 发送失败应归因于
// 发送路径的入参形态;若同样失败 → 才支持「运行环境解码能力受限」的结论。
//
// 凭据只从环境变量读取。
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 OUT_DIR = process.env.PROBE_OUT || 'local';
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, `record-decoder-${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 = `rd-${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 }));
});
// 现场取真实 record 段(record 的 url 含时效 rkey,必须现取现用)
const h = await api('get_group_msg_history', { group_id: GROUP, count: 80 });
let rec = null;
for (const m of h?.data?.messages ?? []) {
const s = (m.message ?? []).find((x) => x.type === 'record');
if (s) { rec = { seq: m.real_seq, ...s.data }; break; }
}
if (!rec) { say('history 中未找到 record 段,无法验证解码能力。'); try { ws.close(); } catch {} process.exit(3); }
say(`取到 record: seq=${rec.seq} file=${rec.file} file_id=${rec.file_id ?? '-'}`);
fs.appendFileSync(OUT, JSON.stringify({ kind: 'source_record_seg', seq: rec.seq, data: rec }) + '\n');
// 群文件直链(只读):验证 file 类媒体的下载读回路径
const rf = await api('get_group_root_files', { group_id: GROUP });
const firstFile = rf?.data?.files?.[0];
say(`取到群文件: file_id=${firstFile?.file_id ?? '-'} name=${firstFile?.file_name ?? '-'}`);
fs.appendFileSync(OUT, JSON.stringify({ kind: 'source_group_file', data: firstFile ?? null }) + '\n');
const CASES = [];
for (const fmt of ['mp3', 'wav', 'silk']) {
if (rec.file) CASES.push({ id: `get_record.file.${fmt}`, p: { file: rec.file, out_format: fmt } });
if (rec.file_id) CASES.push({ id: `get_record.file_id.${fmt}`, p: { file_id: rec.file_id, out_format: fmt } });
if (rec.url) CASES.push({ id: `get_record.url.${fmt}`, p: { file: rec.url, out_format: fmt } });
if (rec.path) CASES.push({ id: `get_record.path.${fmt}`, p: { file: rec.path, out_format: fmt } });
}
if (firstFile?.file_id) {
CASES.push({ id: 'get_group_file_url.real', p: { group_id: GROUP, file_id: firstFile.file_id } });
}
const rows = [];
for (const c of CASES) {
const r = await api(c.id.startsWith('get_group_file_url') ? 'get_group_file_url' : 'get_record', c.p);
const rc = r.__timeout ? 'TIMEOUT' : r.retcode;
const msg = String(r.message ?? r.wording ?? '');
const rec2 = { id: c.id, params_preview: JSON.stringify(c.p).slice(0, 160),
retcode: rc, message: msg,
data_preview: JSON.stringify(r.data ?? null).slice(0, 400) };
rows.push(rec2);
fs.appendFileSync(OUT, JSON.stringify({ kind: 'probe', ...rec2 }) + '\n');
say(`${c.id.padEnd(28)} retcode=${String(rc).padEnd(7)} ${msg.slice(0, 52).padEnd(54)} ${rec2.data_preview.slice(0, 60)}`);
}
const ok = rows.filter((r) => r.retcode === 0 && r.id.startsWith('get_record'));
say(`\n判定:get_record 成功 ${ok.length} 次 / 共 ${rows.filter((r) => r.id.startsWith('get_record')).length} 次`);
say(ok.length > 0
? '→ NapCat 侧 silk/amr 解码链路可用,record 发送失败应归因于发送路径入参形态,不得写「环境缺解码器」'
: '→ get_record 同样无法解码,支持「受 NapCat 运行环境解码能力限制」的定性');
say('\n结果 -> ' + OUT);
try { ws.close(); } catch {}
process.exit(0);