ca73a41a0b
Make homepage cards and live sessions produce sound when the server is configured for StepFun TTS, instead of silently failing (the prebaked Xiaomi voice was useless on a StepFun server, and wasted ~220KB/beat in Fast Origin Transfer). Three coordinated changes: 1. CharacterDesigner now picks a StepFun preset voice id directly from the 32-entry catalog in the SAME LLM call that designs the character — zero extra latency, LLM-grade match quality. The Xiaomi prompt path is byte-identical to history (verified programmatically) so cache hit rate and voice quality are preserved. pickStepfunVoiceId (keyword scorer) remains the fallback for orphan speakers / invalid LLM picks. 2. The 32-preset catalog moves to lib/tts-client/stepfun-voices.json as the single source of truth, shared by the scorer, the CharacterDesigner prompt, /api/tts-provider, and the offline enrich script. 3. A new GET /api/tts-provider endpoint lets the client probe the server's TTS provider at /play mount. fetchBeatAudio then shapes its request body: on a StepFun server it sends the lightweight stepfunVoiceId / voiceDescription and omits the ~220KB Xiaomi reference audio (FOT saving ~13MB per protagonist per session on prebaked cards). requestBeatAudio re-provisions on a provider mismatch before synth, so audio never goes silent on a cross-provider replay or mid-session provider flip. New type fields are all optional and backward-compatible: Character.stepfunVoiceId, BeatAudioRequest.voiceDescription/characterName/stepfunVoiceId, voice made optional. AGENTS.md updated for the new route, type fields, dependency map, and StepFun voice-selection flow.
52 lines
2.1 KiB
TypeScript
52 lines
2.1 KiB
TypeScript
import { requestBeatAudio } from "@infiplot/engine";
|
|
import type { BeatAudioRequest } from "@infiplot/types";
|
|
import { NextResponse } from "next/server";
|
|
import { loadEngineConfig } from "@/lib/config";
|
|
import { requireUser } from "@/lib/supabase/guard";
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
export async function POST(req: Request) {
|
|
const auth = await requireUser();
|
|
if (auth instanceof NextResponse) return auth;
|
|
|
|
let body: BeatAudioRequest;
|
|
try {
|
|
body = (await req.json()) as BeatAudioRequest;
|
|
} catch {
|
|
return NextResponse.json({ error: "Invalid JSON" }, { status: 400 });
|
|
}
|
|
|
|
// Voice is now optional — when the server runs StepFun, the client omits
|
|
// the ~220KB Xiaomi reference audio and sends stepfunVoiceId /
|
|
// voiceDescription instead (saves Fast Origin Transfer bandwidth). The
|
|
// engine's resolveVoice re-provisions on a provider mismatch. We only
|
|
// require the beat text + SOMETHING to synthesize from.
|
|
const VALID_TTS_PROVIDERS = ["xiaomi", "stepfun"];
|
|
const hasVoice =
|
|
!!body.voice?.provider && VALID_TTS_PROVIDERS.includes(body.voice.provider);
|
|
const hasFallback =
|
|
!!body.stepfunVoiceId || !!body.voiceDescription;
|
|
if (!body.beat?.id || !body.beat?.line || (!hasVoice && !hasFallback)) {
|
|
return NextResponse.json(
|
|
{ error: "beat.id and beat.line are required, plus either voice.provider (xiaomi|stepfun) or stepfunVoiceId/voiceDescription" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
try {
|
|
const config = loadEngineConfig();
|
|
const result = await requestBeatAudio(config, body);
|
|
if (!result.audio) return new Response(null, { status: 204 });
|
|
const binary = Buffer.from(result.audio.base64, "base64");
|
|
return new Response(binary, {
|
|
headers: { "Content-Type": result.audio.mime },
|
|
});
|
|
} catch (err) {
|
|
// Engine already swallows synth errors and returns audio:null. Anything
|
|
// that reaches here is config-level — surface so the client can log it.
|
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
return NextResponse.json({ error: message }, { status: 500 });
|
|
}
|
|
}
|