fix(tts): parallel synth, request timeouts, token cache, voice diagnostics

- Chzzk: 3s/8s axios timeouts; cache token (5m ok / 10m fail) with
  in-flight dedup so an expired cookie no longer costs a 401 round trip
  per message
- TTSClient: synthesize signature-split parts concurrently (order kept);
  log per-message part count, failures and synth time; stop silently
  swallowing Chzzk errors
- VoiceSession: decide "latest" by message arrival order so a slow older
  synth can no longer overwrite a newer message already playing; log
  voice connection state changes/errors and player (Auto)Paused events

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Claude Owner
2026-11-11 07:56:19 +09:00
parent c87a16a683
commit 3019b4bcfa
3 changed files with 124 additions and 52 deletions

View File

@@ -6,7 +6,8 @@ import { createResourceFromMp3Buffer } from "../utils/Transcode";
export class VoiceSession {
private player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Pause } });
private generation = 0;
// 마지막으로 재생을 시작한 채팅의 도착 순번(TTSClient.seq).
private lastPlayedSeq = 0;
private isSessionEnd = false;
private lastTime = Date.now();
private endTimer?: NodeJS.Timeout | null;
@@ -29,7 +30,33 @@ export class VoiceSession {
this.init();
}
private get tag() {
return `[Voice] guild=${this.guild.id}`;
}
// 끊김 원인 추적용: 음성 연결 상태 변화와 오류를 남긴다.
private watchConnection(connection: VoiceConnection) {
connection.on("stateChange", (oldState, newState) => {
if (oldState.status === newState.status) return;
const reason = newState.status === VoiceConnectionStatus.Disconnected
? ` reason=${newState.reason} closeCode=${"closeCode" in newState ? newState.closeCode : "-"}`
: "";
Logger.info(`${this.tag} connection ${oldState.status} -> ${newState.status}${reason}`);
});
connection.on("error", (e) => {
Logger.error(`${this.tag} connection error: ${String(e)}`);
});
}
private async init() {
this.watchConnection(this.connection);
// 재생 중 일시정지(연결 미준비로 AutoPaused 등)는 음성이 끊기는 직접 신호다.
this.player.on("stateChange", (oldState, newState) => {
const s = [oldState.status, newState.status];
if (s.includes(AudioPlayerStatus.AutoPaused) || s.includes(AudioPlayerStatus.Paused)) {
Logger.info(`${this.tag} player ${oldState.status} -> ${newState.status}`);
}
});
this.connection.subscribe(this.player);
await entersState(this.connection, VoiceConnectionStatus.Ready, 20_000).catch(() => {});
this.player.on(AudioPlayerStatus.Playing, () => {
@@ -64,18 +91,23 @@ export class VoiceSession {
* 새 TTS가 들어오면 이전 재생을 의도적으로 interrupt한다 (큐잉 없음).
* 다중 사용자 환경에서 모든 메시지를 큐로 쌓으면 자기 발화가 수십 초
* 밀려 채팅 맥락과 어긋나므로, "최신 발화 우선, 앞건 잘림"을 정책으로
* 채택했다. generation 카운터는 같은 메시지에 대해 한 번만 play 되도록
* 보호하는 용도이고, 이전 mp3가 잘리는 동작 자체는 설계 의도이다.
* 채택했다. 이전 mp3가 잘리는 동작 자체는 설계 의도이다.
* 단, "최신"은 합성 완료 순서가 아니라 채팅 도착 순서(seq)로 판단한다.
* 먼저 온 채팅의 합성이 늦게 끝났다면, 이미 재생을 시작한 더 최신
* 채팅을 덮어쓰지 않고 버린다.
*/
public async play(mp3Buf: Buffer) {
public async play(mp3Buf: Buffer, seq: number) {
if (this.isSessionEnd) return;
const myGen = ++this.generation;
if (seq <= this.lastPlayedSeq) {
Logger.info(`${this.tag} seq=${seq} dropped (newer seq=${this.lastPlayedSeq} already playing)`);
return;
}
this.lastPlayedSeq = seq;
this.lastTime = Date.now();
this.startEndTimer();
const res = createResourceFromMp3Buffer(mp3Buf);
if (myGen !== this.generation || this.isSessionEnd) return;
// try { this.player.stop(true); } catch {};
try { this.player.play(res); } catch {};
}
@@ -94,6 +126,7 @@ export class VoiceSession {
selfDeaf: true,
selfMute: false,
});
this.watchConnection(this.connection);
this.connection.subscribe(this.player);
await entersState(this.connection, VoiceConnectionStatus.Ready, 20_000).catch(() => {});
}