Skip to content

Suno-style web UI for a self-hosted MiniMax-Music3 inference server

test: fake upstream can advertise and serve streaming renders

Adam Benhassen1c8f3a5db4f7
diff --git a/server/test/fakeUpstream.ts b/server/test/fakeUpstream.ts
index 7f0c588..f37fa29 100644
--- a/server/test/fakeUpstream.ts
+++ b/server/test/fakeUpstream.ts
@@ -16,11 +16,24 @@ export interface FakeUpstreamOptions {
strictModel?: boolean;
/** expose GET /health answering 503 (model loading) — like inference/server.py; default: no /health route (stock sgl-omni) */
loading?: boolean;
+ /** advertise capabilities:["stream"] on /health and answer stream:true with SSE progress + PCM windows */
+ stream?: { windows: number; windowMs?: number; sampleRate?: number; secondsPerWindow?: number };
+}
+
+/** Interleaved stereo int16 sine, `seconds` long. */
+export function sinePcm(seconds: number, sampleRate = 44100, freq = 440): Buffer {
+ const n = Math.round(seconds * sampleRate);
+ const b = Buffer.alloc(n * 4);
+ for (let i = 0; i < n; i++) {
+ const v = Math.round(Math.sin((2 * Math.PI * freq * i) / sampleRate) * 12000);
+ b.writeInt16LE(v, i * 4); b.writeInt16LE(v, i * 4 + 2);
+ }
+ return b;
}
export interface FakeUpstream {
url: string;
- requests: { method: string; path: string; body?: unknown; auth?: string; aborted?: boolean }[];
+ requests: { method: string; path: string; body?: unknown; auth?: string; aborted?: boolean; stream?: boolean }[];
close(): Promise<void>;
}
@@ -40,19 +53,48 @@ export async function startFakeUpstream(opts: FakeUpstreamOptions = {}): Promise
res.json({ object: 'list', data: [{ id: modelId, object: 'model' }] });
});
- if (opts.loading !== undefined) {
+ if (opts.loading !== undefined || opts.stream) {
app.get('/health', (req, res) => {
requests.push({ method: 'GET', path: req.path });
if (opts.loading) return res.status(503).json({ detail: 'loading model' });
- res.json({ status: 'ready' });
+ res.json({ status: 'ready', ...(opts.stream ? { capabilities: ['stream'] } : {}) });
});
}
+ const streamRender = (res: express.Response, entry: FakeUpstream['requests'][number], b: Record<string, unknown>) => {
+ const s = opts.stream!;
+ const sr = s.sampleRate ?? 44100, per = s.secondsPerWindow ?? 0.5, gap = s.windowMs ?? 20;
+ const send = (event: string, data: unknown) => res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
+ res.status(200).set({ 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'X-Seed': String(b.seed ?? 4242) });
+ res.flushHeaders();
+ entry.stream = true;
+ const frames = Number(b.max_new_tokens ?? 1500);
+ let step = 0; let seconds = 0;
+ const timer = setInterval(() => {
+ if (res.destroyed) return clearInterval(timer);
+ if (step < 2) send('progress', { stage: 'semantic', done: Math.round((frames * (step + 1)) / 2), total: frames, secondsRendered: 0 });
+ else if (step - 2 < s.windows) {
+ const k = step - 2;
+ send('progress', { stage: 'denoise', done: k * 30 + 30, total: s.windows * 30, secondsRendered: seconds });
+ const pcm = sinePcm(per, sr, 220 * (k + 1));
+ seconds += per;
+ send('audio', { pcm: pcm.toString('base64'), samples: pcm.length / 4, sampleRate: sr, channels: 2 });
+ } else {
+ send('done', { seed: Number(b.seed ?? 4242), sampleRate: sr, channels: 2 });
+ clearInterval(timer);
+ res.end();
+ }
+ step++;
+ }, gap);
+ res.on('close', () => { if (!res.writableEnded) { entry.aborted = true; clearInterval(timer); } });
+ };
+
app.post('/v1/audio/speech', (req, res) => {
- const entry = { method: 'POST', path: req.path, body: req.body, auth: req.header('authorization') ?? undefined, aborted: false };
+ const entry: FakeUpstream['requests'][number] = { method: 'POST', path: req.path, body: req.body, auth: req.header('authorization') ?? undefined, aborted: false, stream: false };
requests.push(entry);
const b = req.body ?? {};
- if (b.stream === true) return res.status(400).json({ error: 'stream: true is not supported' });
+ if (b.stream === true && !opts.stream) return res.status(400).json({ error: 'stream: true is not supported' });
+ if (b.stream === true) return streamRender(res, entry, b);
if (opts.strictModel && b.model !== (modelId ?? 'MiniMaxAI/MiniMax-Music3')) return res.status(404).json({ error: `model ${b.model} not found` });
if (!b.instructions) return res.status(422).json({ detail: 'instructions required' });
if ((b.response_format ?? 'wav') !== 'wav') return res.status(422).json({ detail: 'response_format must be wav' });
diff --git a/server/test/fakeUpstreamServer.ts b/server/test/fakeUpstreamServer.ts
index 046b454..53a3f76 100644
--- a/server/test/fakeUpstreamServer.ts
+++ b/server/test/fakeUpstreamServer.ts
@@ -4,7 +4,9 @@ import { startFakeUpstream } from './fakeUpstream.js';
// Standalone fake for `npm run dev:fake` — no GPU needed.
// FAKE_AUDIO=path/to/file.wav serves a real, playable file; FAKE_RENDER_MS sets the blocking render time;
// FAKE_LOADING=1 exposes /health answering 503 (model still loading).
+// FAKE_STREAM=1 advertises streaming and answers stream:true with SSE progress + 4 s PCM windows.
const port = Number(process.env.FAKE_PORT ?? 7999);
const audioBytes = process.env.FAKE_AUDIO ? fs.readFileSync(process.env.FAKE_AUDIO) : undefined;
-const fake = await startFakeUpstream({ renderMs: Number(process.env.FAKE_RENDER_MS ?? 8000), audioBytes, port, loading: process.env.FAKE_LOADING ? true : undefined });
+const fake = await startFakeUpstream({ renderMs: Number(process.env.FAKE_RENDER_MS ?? 8000), audioBytes, port, loading: process.env.FAKE_LOADING ? true : undefined,
+ stream: process.env.FAKE_STREAM ? { windows: 6, windowMs: Number(process.env.FAKE_RENDER_MS ?? 8000) / 8, secondsPerWindow: 4 } : undefined });
console.log(`fake MiniMax-Music3 server (POST /v1/audio/speech) listening on ${fake.url}${audioBytes ? ` (audio: ${process.env.FAKE_AUDIO})` : ''}`);