// 语音段(record)可用性专项探测。 // 第 19 章用 WAV 得到 "No decodable audio stream: Decoder not found", // 本轮改用 NapCat 自己 get_ai_record 产出的真实 QQ 语音(magic = \x03#!SILK_V3), // 排除「素材非法」这一变量,再用多种 file 传法定位到底是素材、参数还是解码器问题。 // 默认 dry-run,--yes 才真实发送。凭据只从环境变量读取。 // // 用法: // NAPCAT_WS_TOKEN=xxx node tools/probe/record-probe.mjs // NAPCAT_WS_TOKEN=xxx node tools/probe/record-probe.mjs --yes 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); const DO_WRITE = process.argv.includes('--yes'); 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-probe-${stamp}.jsonl`); const say = (...a) => console.log(...a); const pending = new Map(); let authenticated = false; const ws = new WebSocket(`${WS_URL.replace(/\/$/, '')}/?access_token=${encodeURIComponent(TOKEN)}`); 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') authenticated = true; }; await new Promise((res, rej) => { ws.onopen = res; setTimeout(() => rej(new Error('连接超时')), 15000); }) .catch((e) => { console.error(e.message); process.exit(1); }); for (let i = 0; i < 60 && !authenticated; i++) await new Promise((r) => setTimeout(r, 100)); say('认证=' + authenticated); const api = (action, params, ms = 30000) => new Promise((resolve) => { const echo = `rp-${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 })); }); // 素材:让 NapCat 自己生成语音,拿到官方 CDN URL,再下载成 silk 字节 let silkB64 = null, silkUrl = null, magic = null, bytes = 0; const ai = await api('get_ai_record', { group_id: GROUP, character: 'lucy-voice-houge', text: 'QLinker 语音可用性探测' }); if (ai.retcode === 0 && typeof ai.data === 'string') { silkUrl = ai.data; try { const buf = Buffer.from(await (await fetch(silkUrl)).arrayBuffer()); bytes = buf.length; magic = buf.subarray(0, 16).toString('hex'); silkB64 = buf.toString('base64'); say(`素材: ${bytes} 字节 magic=${magic} ascii=${JSON.stringify(buf.subarray(0, 12).toString('latin1'))}`); fs.writeFileSync(path.join(OUT_DIR, `record-silk-${stamp}.bin`), buf); } catch (e) { say('素材下载失败: ' + e.message); } } else { say('get_ai_record 失败: ' + JSON.stringify(ai).slice(0, 200)); } const VARIANTS = [ { id: 'b64_plain', desc: 'record.file = base64://', need: silkB64, data: () => ({ file: 'base64://' + silkB64 }) }, { id: 'b64_format_silk', desc: 'record.file = base64:// + format=silk', need: silkB64, data: () => ({ file: 'base64://' + silkB64, format: 'silk' }) }, { id: 'b64_name_silk', desc: 'record.file = base64:// + file_name=a.silk', need: silkB64, data: () => ({ file: 'base64://' + silkB64, file_name: 'a.silk' }) }, { id: 'url_plain', desc: 'record.file = ', need: silkUrl, data: () => ({ file: silkUrl }) }, { id: 'url_field', desc: 'record.url = (不放 file)', need: silkUrl, data: () => ({ url: silkUrl }) }, { id: 'url_format_silk', desc: 'record.file = + format=silk + path', need: silkUrl, data: () => ({ file: silkUrl, format: 'silk', path: '/tmp/qlinker-probe.silk' }) }, { id: 'b64_url_both', desc: 'record.file = base64 + url 同时给', need: silkB64 && silkUrl, data: () => ({ file: 'base64://' + silkB64, url: silkUrl }) }, { id: 'ai_record_api', desc: 'send_group_ai_record(官方 TTS 语音,作为语音能力旁证)', api: 'send_group_ai_record', params: () => ({ group_id: GROUP, character: 'lucy-voice-houge', text: 'QLinker 语音可用性探测' }) }, ]; const results = []; say(`\n模式=${DO_WRITE ? '真实发送' : 'DRY-RUN'}\n`); for (const v of VARIANTS) { // 只有显式声明 need 的变体才受素材门禁约束 if ('need' in v && !v.need) { say(`${v.id.padEnd(18)} 跳过:素材缺失`); results.push({ id: v.id, status: 'skipped' }); continue; } if (!DO_WRITE) { say(`${v.id.padEnd(18)} [计划] ${v.desc}`); continue; } let r; if (v.api) r = await api(v.api, v.params()); else r = await api('send_group_msg', { group_id: GROUP, message: [ { type: 'record', data: v.data() }, { type: 'text', data: { text: `[QLr:${v.id}]` } }] }); const rec = { id: v.id, desc: v.desc, retcode: r.__timeout ? 'TIMEOUT' : r.retcode, message: r.message ?? r.wording ?? '', message_id: r?.data?.message_id ?? null, status: r.__timeout ? 'timeout' : (r.retcode === 0 ? 'sent' : 'rejected') }; results.push(rec); fs.appendFileSync(OUT, JSON.stringify(rec) + '\n'); say(`${v.id.padEnd(18)} retcode=${String(rec.retcode).padEnd(7)} ${String(rec.message).slice(0, 70)}`); await new Promise((r2) => setTimeout(r2, 2500)); } if (DO_WRITE) { say('\nhistory 回读:'); const h = await api('get_group_msg_history', { group_id: GROUP, count: 40 }); for (const m of h?.data?.messages ?? []) { const raw = String(m.raw_message ?? ''); const hit = raw.match(/\[QLr:([a-z_0-9]+)\]/); if (hit) say(` ${hit[1].padEnd(18)} seq=${m.real_seq} 段=${(m.message ?? []).map((s) => s.type).join('+')}`); } fs.appendFileSync(OUT, JSON.stringify({ __summary: true, magic, bytes, silk_url: silkUrl }) + '\n'); say('\n结果 -> ' + OUT); } try { ws.close(); } catch {} process.exit(0);