- 사이트 서버: discord.js 로 디스코드 게이트웨이에 한 번 붙고(discord-source.ts), 로그인한 앱에 WS /app/bot/ws 로 중계(bot-broker.ts) · 앱이 보낸 op 4 는 지켜보는 서버의 음성채널 참여 · 나가기만 허용하고 필드를 다시 만들어 보낸다 · 디스코드가 준 일회용 음성 접속 정보(VOICE_STATE/SERVER_UPDATE)는 음성을 가진 앱에만 넘긴다. 음성은 앱 ↔ 디스코드로 바로 · 한 서버의 봇 음성은 마지막으로 요청한 앱이 가져가고 이전 앱은 taken. 이전 앱의 늦은 나가기 무시, 음성 가진 앱이 끊기면 봇을 뺀다 · 슬래시 명령은 서버가 받아 defer 후 그 서버를 지켜보는 앱으로 보낸다 · 등록 해제 · 로그아웃하면 그 유저의 중계 연결을 바로 끊는다 (4401), 30초마다 다시 확인 - /app/me 에서 botToken 제거. 앱은 v1.2.0 이 저장해 둔 봇 토큰을 시작할 때 지운다 - 앱: BotGateway 로 디스코드 연결을 분리 (RemoteGateway = 겜녹이 봇 서버 중계, LocalGateway = 다른 봇 토큰 직접 연결) Bot 은 discord.js 객체 대신 이 인터페이스만 쓴다. 중계가 로그인 만료를 알리면 바로 다시 확인해서 잠근다 - 로그인 확인: 잠그라는 응답(401 · 403)은 겹친 확인 때문에 늦게 와도 같은 토큰이면 따른다 (겹친 두 번째 확인이 401 을 받아 "등록되지 않은 아이디" 대신 "로그인 만료"로 잠기던 문제). 미등록 화면은 다시 확인해도 유지 - 테스트: 봇 중계 4건(토큰 미노출 · 음성 정보 전달 · 다른 PC 인계 · 명령 라우팅 · 해제 시 끊김), 겹친 확인 1건 실제 디스코드: voice-e2e GVR_E2E_RELAY=1 (서버 중계로 따라 들어가 녹음 · STT · 녹음 제외), login-e2e 19/19 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
207 lines
13 KiB
TypeScript
207 lines
13 KiB
TypeScript
// M1 실제 음성 E2E: 테스트봇이 음성채널에 들어가 한국어 문장을 말하면, 겜녹이(코어)가 따라 들어와 녹음 → STT → DB 저장까지.
|
|
// node --import tsx scripts/voice-e2e.mts <guildId> <voiceChannelId>
|
|
// 필요: secrets.json 의 겜녹이·테스트봇 토큰, worker/.venv, tiny 모델(HF 캐시)
|
|
// M7 실사용 시험: GVR_E2E_SET=demo (시연 대화 28문장, 문장별 CER·지연), GVR_E2E_MODEL=<모델 폴더 경로>, GVR_E2E_DEVICE=cuda
|
|
// GVR_E2E_RELAY=1: 겜녹이 봇을 사이트 서버 중계로 쓴다 (이 저장소의 사이트 서버 코드를 띄워 봇 토큰은 서버에만 두고,
|
|
// 앱은 로그인 토큰으로 /app/bot/ws 에 붙는다. 디스코드 OAuth 만 가짜). 끝나면 앱 데이터 폴더에 봇 토큰이 없는지도 본다
|
|
import { Client, GatewayIntentBits } from 'discord.js';
|
|
import { joinVoiceChannel, entersState, VoiceConnectionStatus, createAudioPlayer, createAudioResource, StreamType, AudioPlayerStatus } from '@discordjs/voice';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { synthesize } from '../src/core/discord/tts.ts';
|
|
|
|
const [guildId, channelId] = process.argv.slice(2);
|
|
const secrets = JSON.parse(fs.readFileSync('/home/claude/.config/ejclaw/secrets.json', 'utf8')).credentials;
|
|
const GVR_TOKEN = secrets['discord.com/bot/game-voice-recorder'].token;
|
|
const SPEAKER_TOKEN = secrets['discord.com/bot/testbot'].token;
|
|
const SPEAKER_ID = secrets['discord.com/bot/testbot'].client_id || '1538122882528321536';
|
|
const root = path.resolve(import.meta.dirname, '..', '..');
|
|
const work = fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-voice-e2e-'));
|
|
const models = path.join(work, 'models');
|
|
fs.mkdirSync(models, { recursive: true });
|
|
const hf = path.join(os.homedir(), '.cache/huggingface/hub/models--Systran--faster-whisper-small/snapshots');
|
|
const modelArg = process.env.GVR_E2E_MODEL;
|
|
const model = modelArg ? path.basename(modelArg) : fs.existsSync(hf) ? 'small' : 'tiny';
|
|
const device = process.env.GVR_E2E_DEVICE === 'cuda' ? 'cuda' : 'cpu';
|
|
fs.symlinkSync(modelArg ? path.resolve(modelArg) : path.join(hf.replace('small', model), fs.readdirSync(hf.replace('small', model))[0]), path.join(models, model));
|
|
fs.writeFileSync(
|
|
path.join(work, 'settings.json'),
|
|
JSON.stringify({
|
|
discord: { guildId, followUserId: SPEAKER_ID, followEnabled: true, ttsEnabled: true, ttsOnNewMember: false },
|
|
stt: { realtimeModel: model, preciseModel: model, device, computeType: device === 'cuda' ? 'float16' : 'int8' },
|
|
games: { valorant: false, lol: false },
|
|
}),
|
|
);
|
|
process.env.GVR_RECORD_BOTS = '1';
|
|
const RELAY = process.env.GVR_E2E_RELAY === '1';
|
|
let relayStop: (() => Promise<void>) | null = null;
|
|
let settings: any;
|
|
if (RELAY) {
|
|
delete process.env.GVR_BOT_TOKEN;
|
|
const http = await import('node:http');
|
|
const { buildServer } = await import('../../site/server/src/app.ts');
|
|
const { DiscordSource } = await import('../../site/server/src/discord-source.ts');
|
|
const { AppAuth } = await import('../src/core/auth.ts');
|
|
const { SettingsStore } = await import('../src/core/settings.ts');
|
|
const SUPER = '293719842274541579';
|
|
// 가짜 디스코드 OAuth: code = 로그인할 아이디
|
|
const dc = http.createServer((req, res) => {
|
|
res.setHeader('content-type', 'application/json');
|
|
if (req.url === '/oauth2/token') return void res.end(JSON.stringify({ access_token: SUPER }));
|
|
if (req.url === '/users/@me') return void res.end(JSON.stringify({ id: SUPER, username: 'e2e', global_name: 'e2e', avatar: null }));
|
|
res.statusCode = 404;
|
|
res.end('{}');
|
|
});
|
|
await new Promise<void>((r) => dc.listen(0, '127.0.0.1', () => r()));
|
|
const source = new DiscordSource({ info: (o, m) => console.log('[site]', m ?? '', JSON.stringify(o)), error: (o, m) => console.error('[site]', m ?? '', o) });
|
|
const { app: site } = await buildServer(
|
|
{
|
|
port: 0, host: '127.0.0.1', publicUrl: 'http://placeholder', clientId: 'cid', clientSecret: 's', botToken: GVR_TOKEN,
|
|
allowedGuilds: [], owners: [SUPER], superAdmins: [SUPER], sessionSecret: 'x'.repeat(32), dataDir: path.join(work, 'site'),
|
|
webDir: '/nonexistent', version: 'e2e', discordApi: `http://127.0.0.1:${(dc.address() as any).port}`, assetUpstream: {},
|
|
},
|
|
{ botSource: source },
|
|
);
|
|
site.log.level = 'warn';
|
|
await site.listen({ host: '127.0.0.1', port: 0 });
|
|
const base = `http://127.0.0.1:${(site.server.address() as any).port}`;
|
|
await source.login(GVR_TOKEN);
|
|
const appDir = path.join(work, 'app');
|
|
fs.mkdirSync(appDir);
|
|
fs.copyFileSync(path.join(work, 'settings.json'), path.join(appDir, 'settings.json'));
|
|
settings = new SettingsStore(appDir);
|
|
settings.update({ site: { serverUrl: base }, stt: { modelsDir: models } });
|
|
const auth = new AppAuth(settings, {
|
|
version: 'e2e',
|
|
pollMs: 100,
|
|
openUrl: async (url: string) => {
|
|
const start = await fetch(url.replace('http://placeholder', base), { redirect: 'manual' });
|
|
const state = new URL(start.headers.get('location')!).searchParams.get('state')!;
|
|
await fetch(`${base}/auth/discord/callback?code=${SUPER}&state=${encodeURIComponent(state)}`, { headers: { cookie: start.headers.getSetCookie()[0]!.split(';')[0]! }, redirect: 'manual' });
|
|
},
|
|
});
|
|
const a = await auth.login();
|
|
if (a.status !== 'ok') throw new Error(`앱 로그인 실패: ${a.status} ${a.error}`);
|
|
console.log('앱 로그인 (서버 중계 모드):', a.user?.name, a.role);
|
|
relayStop = async () => {
|
|
await site.close();
|
|
await source.destroy();
|
|
dc.close();
|
|
};
|
|
} else process.env.GVR_BOT_TOKEN = GVR_TOKEN;
|
|
const DEMO = process.env.GVR_E2E_SET === 'demo';
|
|
const demoSrc = fs.readFileSync(path.join(import.meta.dirname, 'demo-stack.mts'), 'utf8');
|
|
const SENTENCES = DEMO
|
|
? [...demoSrc.matchAll(/\{ u: \d, text: '([^']+)', at: \{ (?:val|lol|off)/g)].map((m) => m[1])
|
|
: ['레이즈 궁 빠졌어 B 사이트 로테 하자', '오늘 저녁에 치킨 먹을래', '나 오퍼레이터 살게'];
|
|
const clips: string[] = [];
|
|
const clipDir = path.join(os.homedir(), '.tmp', DEMO ? 'gvr-demo-clips' : 'gvr-e2e-clips');
|
|
fs.mkdirSync(clipDir, { recursive: true });
|
|
for (const [i, s] of SENTENCES.entries()) {
|
|
const f = path.join(clipDir, DEMO ? `${i}.webm` : `say-${i}.webm`);
|
|
if (!fs.existsSync(f)) await synthesize(s, i % 2 ? 'ko-KR-InJoonNeural' : 'ko-KR-SunHiNeural', f);
|
|
clips.push(f);
|
|
}
|
|
const { startCore } = await import('../src/core/index.ts');
|
|
const core = await startCore({ dataDir: RELAY ? path.join(work, 'app') : work, settings, resourcesDir: path.join(root, 'app', 'resources'), worker: { cmd: path.join(root, 'worker/.venv/bin/python'), args: ['-m', 'gvr_worker'], cwd: path.join(root, 'worker') }, version: 'e2e' });
|
|
const wait = async (fn: () => boolean, ms: number, label: string) => {
|
|
const t = Date.now();
|
|
while (!fn()) {
|
|
if (Date.now() - t > ms) throw new Error('timeout: ' + label);
|
|
await new Promise((r) => setTimeout(r, 200));
|
|
}
|
|
};
|
|
let ok = false;
|
|
const spk = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });
|
|
try {
|
|
await wait(() => core.bot.state.status === 'ready', 30000, 'gvr bot ready');
|
|
if (RELAY) console.log('겜녹이 봇 연결:', core.bot.gateway?.kind, core.bot.state.user?.name);
|
|
await wait(() => core.stt.status.slots.realtime.state === 'loaded', 120000, 'model');
|
|
await spk.login(SPEAKER_TOKEN);
|
|
await new Promise((r) => spk.once('clientReady', r));
|
|
const ch: any = await spk.channels.fetch(channelId);
|
|
const conn = joinVoiceChannel({ channelId, guildId, adapterCreator: ch.guild.voiceAdapterCreator, selfDeaf: false, selfMute: false, daveEncryption: true });
|
|
await entersState(conn, VoiceConnectionStatus.Ready, 20000);
|
|
console.log('speaker joined', ch.name);
|
|
await wait(() => core.bot.state.recording.active, 20000, 'gvr follows speaker');
|
|
console.log('겜녹이 따라 들어옴 (follow):', core.bot.state.recording.channel);
|
|
await new Promise((r) => setTimeout(r, 9000)); // 안내 음성이 끝나고 DAVE 키 교환이 안정될 때까지
|
|
// 발화가 끝난 뒤 글자가 나오기까지 걸린 시간 (실시간 모델 지연)
|
|
const seen = new Map<string, number>();
|
|
const poll = setInterval(() => {
|
|
for (const r of core.store.db.prepare(`SELECT id FROM utterances WHERE status!='pending'`).all() as { id: string }[]) if (!seen.has(r.id)) seen.set(r.id, Date.now());
|
|
}, 100);
|
|
const player = createAudioPlayer();
|
|
conn.subscribe(player);
|
|
for (const f of clips) {
|
|
player.play(createAudioResource(fs.createReadStream(f), { inputType: StreamType.WebmOpus }));
|
|
await entersState(player, AudioPlayerStatus.Playing, 5000);
|
|
await entersState(player, AudioPlayerStatus.Idle, 30000);
|
|
await new Promise((r) => setTimeout(r, 1500));
|
|
}
|
|
await wait(() => core.store.db.prepare(`SELECT count(*) n FROM utterances WHERE status!='pending'`).get().n >= SENTENCES.length, 90000, 'stt');
|
|
clearInterval(poll);
|
|
const rows = core.store.db.prepare('SELECT id, user_id, started_at, ended_at, text, gain_db, status FROM utterances ORDER BY started_at').all() as any[];
|
|
const lat = rows.map((r) => (seen.get(r.id) ?? Date.now()) - r.ended_at);
|
|
for (const [i, r] of rows.entries()) console.log(` [${r.user_id === SPEAKER_ID ? '테스트봇' : r.user_id}] ${((r.ended_at - r.started_at) / 1000).toFixed(1)}s gain ${r.gain_db?.toFixed(1)}dB 지연 ${(lat[i] / 1000).toFixed(2)}s ${r.status}: ${r.text}`);
|
|
const sorted = [...lat].sort((a, b) => a - b);
|
|
console.log(`발화 ${rows.length}개 (말한 문장 ${SENTENCES.length}개) · 모델 ${model} (${device}) · 지연 중앙값 ${(sorted[Math.floor(sorted.length / 2)] / 1000).toFixed(2)}s · 최대 ${(sorted[sorted.length - 1] / 1000).toFixed(2)}s`);
|
|
let cerAvg = 0;
|
|
if (DEMO) {
|
|
// 발화가 문장 수와 같으면 순서대로 짝지어 CER 을 잰다 (stt-eval 과 같은 정규화)
|
|
const norm = (x: string) => x.replace(/[\s.,!?~]/g, '').replace(/PC방/g, '피시방').replace(/2명/g, '두명');
|
|
const cer = (a0: string, b0: string) => {
|
|
const a = [...norm(a0)];
|
|
const b = [...norm(b0)];
|
|
const d = Array.from({ length: a.length + 1 }, (_, i) => [i, ...new Array(b.length).fill(0)]);
|
|
for (let j = 1; j <= b.length; j++) d[0][j] = j;
|
|
for (let i = 1; i <= a.length; i++) for (let j = 1; j <= b.length; j++) d[i][j] = Math.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
return d[a.length][b.length] / Math.max(1, a.length);
|
|
};
|
|
if (rows.length === SENTENCES.length) {
|
|
const cs = rows.map((r, i) => cer(SENTENCES[i], r.text || ''));
|
|
cerAvg = cs.reduce((a, b) => a + b, 0) / cs.length;
|
|
console.log(`CER ${(cerAvg * 100).toFixed(1)}% · 완전 일치 ${cs.filter((c) => c === 0).length}/${cs.length}`);
|
|
} else {
|
|
cerAvg = 1;
|
|
console.log('발화 수가 문장 수와 달라 CER 을 짝지을 수 없음 (발화 분할 확인 필요)');
|
|
}
|
|
}
|
|
// 녹음 제외 확인
|
|
core.store.setExcluded(SPEAKER_ID, true);
|
|
const before = rows.length;
|
|
player.play(createAudioResource(fs.createReadStream(clips[0]), { inputType: StreamType.WebmOpus }));
|
|
await entersState(player, AudioPlayerStatus.Idle, 30000).catch(() => undefined);
|
|
await new Promise((r) => setTimeout(r, 2500));
|
|
const after = (core.store.db.prepare('SELECT count(*) n FROM utterances').get() as any).n;
|
|
console.log(`녹음 제외 후 새 발화: ${after - before}개 (0 이어야 함)`);
|
|
const texts = rows.map((r) => r.text || '').join(' ');
|
|
ok = rows.length >= SENTENCES.length && /레이즈|오퍼|치킨/.test(texts) && after === before && (!DEMO || cerAvg < 0.15);
|
|
if (RELAY) {
|
|
// 서버 중계 모드: 봇은 서버 연결이어야 하고, 앱 데이터 폴더 어디에도 봇 토큰이 없어야 한다
|
|
const leaked: string[] = [];
|
|
const scan = (d: string) => {
|
|
for (const f of fs.readdirSync(d, { withFileTypes: true })) {
|
|
const p = path.join(d, f.name);
|
|
if (f.isDirectory()) scan(p);
|
|
else if (f.isFile() && fs.statSync(p).size < 200 * 1024 * 1024 && fs.readFileSync(p).includes(Buffer.from(GVR_TOKEN))) leaked.push(p);
|
|
}
|
|
};
|
|
scan(path.join(work, 'app'));
|
|
console.log(`봇 연결 방식: ${core.bot.gateway?.kind} · 앱 데이터에 봇 토큰: ${leaked.length ? leaked.join(', ') : '없음'}`);
|
|
ok &&= core.bot.gateway?.kind === 'remote' && leaked.length === 0;
|
|
}
|
|
conn.destroy();
|
|
await wait(() => !core.bot.state.recording.active, 20000, 'gvr leaves after speaker').catch(() => console.log('(겜녹이 퇴장 대기 시간 초과)'));
|
|
console.log('겜녹이 퇴장:', !core.bot.state.recording.active);
|
|
} catch (e) {
|
|
console.error('E2E 실패', e);
|
|
} finally {
|
|
await spk.destroy();
|
|
await core.stop();
|
|
await relayStop?.();
|
|
console.log(ok ? 'VOICE E2E PASS' : 'VOICE E2E FAIL');
|
|
process.exit(ok ? 0 : 1);
|
|
}
|