67 lines
3.8 KiB
JavaScript
67 lines
3.8 KiB
JavaScript
// 验证 get_ai_record 是否是「只读生成 URL」还是「会真实往群里发语音」。
|
||||
|
|
// 起因:history 里出现 4 条 record 消息,而所有显式 record 段发送都返回 retcode=1200,
|
|||
|
|
// 且这 4 条的 fileid 与历次 get_ai_record 返回的 URL fileid 逐一对应。
|
|||
|
|
// 若成立,则 get_ai_record 名义上是查询接口、实际有外部副作用,QLinker 侧必须按写操作管控。
|
|||
|
|
//
|
|||
|
|
// 用法(会产生真实语音消息,属有副作用脚本):
|
|||
|
|
// NAPCAT_WS_TOKEN=xxx node tools/probe/ai-record-sideeffect.mjs
|
|||
|
|
import fs from 'node:fs';
|
|||
|
|
|
|||
|
|
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);
|
|||
|
|
if (!TOKEN) { console.error('缺少环境变量 NAPCAT_WS_TOKEN。凭据不得硬编码。'); process.exit(1); }
|
|||
|
|
|
|||
|
|
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 = 40000) => new Promise((resolve) => {
|
|||
|
|
const echo = `se-${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 fileIdOf = (u) => (String(u).match(/fileid=([^&]+)/) || [])[1] ?? null;
|
|||
|
|
|
|||
|
|
async function recordsInHistory() {
|
|||
|
|
const r = await api('get_group_msg_history', { group_id: GROUP, count: 60 });
|
|||
|
|
return (r?.data?.messages ?? [])
|
|||
|
|
.filter((m) => (m.message ?? []).some((s) => s.type === 'record'))
|
|||
|
|
.map((m) => ({ seq: m.real_seq, mid: m.message_id,
|
|||
|
|
urlFileid: fileIdOf((m.message ?? []).find((s) => s.type === 'record')?.data?.url) }));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const before = await recordsInHistory();
|
|||
|
|
console.log(`调用前 history 中 record 消息 ${before.length} 条: ` + before.map((x) => x.seq).join(','));
|
|||
|
|
|
|||
|
|
const r = await api('get_ai_record', { group_id: GROUP, character: 'lucy-voice-houge', text: '副作用验证' });
|
|||
|
|
console.log(`get_ai_record retcode=${r.retcode} 返回类型=${typeof r.data} message_id=${JSON.stringify(r?.data?.message_id ?? null)}`);
|
|||
|
|
const urlFileid = typeof r.data === 'string' ? fileIdOf(r.data) : null;
|
|||
|
|
console.log('返回 URL fileid = ' + String(urlFileid).slice(0, 40));
|
|||
|
|
|
|||
|
|
await new Promise((x) => setTimeout(x, 12000));
|
|||
|
|
const after = await recordsInHistory();
|
|||
|
|
console.log(`调用后 history 中 record 消息 ${after.length} 条: ` + after.map((x) => x.seq).join(','));
|
|||
|
|
|
|||
|
|
const added = after.filter((a) => !before.some((b) => b.seq === a.seq));
|
|||
|
|
console.log(`\n新增 ${added.length} 条: ` + JSON.stringify(added.map((a) => ({ seq: a.seq, mid: a.mid }))));
|
|||
|
|
const matched = added.some((a) => a.urlFileid && a.urlFileid === urlFileid);
|
|||
|
|
console.log(`新增消息的 url fileid 是否等于 get_ai_record 返回的 fileid: ${matched}`);
|
|||
|
|
console.log(`\n结论候选: ${added.length === 1 && matched
|
|||
|
|
? 'get_ai_record 会真实发送语音消息(名义查询接口实为写操作)'
|
|||
|
|
: (added.length === 0 ? 'get_ai_record 未产生新消息(确为只读)' : '新增条数/指纹不匹配,需人工判读')}`);
|
|||
|
|
|
|||
|
|
fs.writeFileSync(`local/ai-record-sideeffect-${Date.now()}.jsonl`,
|
|||
|
|
JSON.stringify({ before, after, added, matched, urlFileid, retcode: r.retcode }, null, 2) + '\n');
|
|||
|
|
try { ws.close(); } catch {}
|
|||
|
|
process.exit(0);
|