forked from vijaygupta18/ai-interview-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-custom.js
More file actions
283 lines (254 loc) · 11.2 KB
/
server-custom.js
File metadata and controls
283 lines (254 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Wrapper: loads the standard Next.js standalone server.js + adds WebSocket STT proxy
// In dev mode: uses next() directly
// In production: monkey-patches http.createServer to capture the server, then loads server.js
const path = require("path");
const fs = require("fs");
// Load .env.local
const envPath = path.join(__dirname, ".env.local");
if (fs.existsSync(envPath)) {
for (const line of fs.readFileSync(envPath, "utf8").split("\n")) {
const t = line.trim();
if (!t || t.startsWith("#")) continue;
const eq = t.indexOf("=");
if (eq === -1) continue;
const k = t.substring(0, eq).trim();
let v = t.substring(eq + 1).trim();
if ((v.startsWith('"') && v.endsWith('"')) || (v.startsWith("'") && v.endsWith("'"))) v = v.slice(1, -1);
if (!process.env[k]) process.env[k] = v;
}
}
const { parse } = require("url");
const { WebSocket, WebSocketServer } = require("ws");
const { Pool } = require("pg");
const pool = new Pool({
connectionString: process.env.DATABASE_URL || "postgresql://postgres@localhost:5432/ai_interview_platform",
max: 3,
});
function getSTTConfig() {
const provider = process.env.STT_PROVIDER || "deepgram";
const language = process.env.STT_LANGUAGE || "en-IN";
if (provider === "soniox") {
const k = process.env.SONIOX_API_KEY || "";
// Soniox auth is sent as first JSON message after WS connect, not in URL/headers
return {
provider: "soniox",
wsUrl: "wss://stt-rt.soniox.com/transcribe-websocket",
// Config sent on connect — language_hints uses ISO 639-1 (en, hi, etc.)
initConfig: {
api_key: k,
model: "stt-rt-v4",
audio_format: "auto", // auto-detects webm/opus from MediaRecorder
num_channels: 1,
language_hints: [language.split("-")[0]], // "en-IN" → "en"
language_hints_strict: true,
enable_endpoint_detection: true,
max_endpoint_delay_ms: 3000,
// NOTE: diarization DISABLED — it prevents <end> tokens from being sent
// enable_speaker_diarization: true,
},
};
}
if (provider === "sarvam") {
const k = process.env.SARVAM_API_KEY || "";
return { provider: "sarvam", wsUrl: `wss://api.sarvam.ai/speech-to-text-streaming/transcribe/ws?api_subscription_key=${k}&language_code=${language}&model=saaras:v3`, headers: { "Api-Subscription-Key": k } };
}
const k = process.env.DEEPGRAM_API_KEY || "";
return { provider: "deepgram", wsUrl: `wss://api.deepgram.com/v1/listen?model=nova-3&language=${language}&punctuate=true&interim_results=true&endpointing=800&vad_events=true&diarize=true&utterance_end_ms=4000`, protocols: ["token", k] };
}
function addWSProxy(server) {
const wss = new WebSocketServer({ noServer: true });
server.on("upgrade", async (req, socket, head) => {
const { pathname, query } = parse(req.url, true);
if (pathname !== "/api/stt-ws") return;
if (!query.token) { socket.write("HTTP/1.1 401\r\n\r\n"); socket.destroy(); return; }
try {
const { rows } = await pool.query("SELECT id FROM interviews WHERE token=$1 AND status IN ('in_progress','waiting')", [query.token]);
if (!rows.length) { socket.write("HTTP/1.1 403\r\n\r\n"); socket.destroy(); return; }
} catch { socket.write("HTTP/1.1 500\r\n\r\n"); socket.destroy(); return; }
wss.handleUpgrade(req, socket, head, (ws) => wss.emit("connection", ws));
});
// ─── Soniox response normalizer (stateful per connection) ──────────
// KEY PROTOCOL FACT: Final tokens appear ONCE then disappear from subsequent messages.
// Non-final tokens are the live preview that gets REPLACED each message.
// Client MUST accumulate final tokens — they are never repeated.
function createSonioxNormalizer() {
let accumulatedFinals = []; // all finalized token texts for current utterance
return function normalize(raw) {
try {
const msg = JSON.parse(typeof raw === "string" ? raw : raw.toString());
if (msg.error_code) {
console.error(`[STT-WS:soniox] Error ${msg.error_code}: ${msg.error_message}`);
return [];
}
if (!msg.tokens || msg.tokens.length === 0) return [];
const results = [];
const nonFinalTexts = [];
let hasEnd = false;
for (const token of msg.tokens) {
if (token.text === "<end>" || token.text === "<fin>") {
hasEnd = true;
continue;
}
if (token.text.startsWith("<")) continue; // skip other special tokens
if (token.is_final) {
// Final token — appears ONCE then disappears. Accumulate it.
accumulatedFinals.push(token.text);
} else {
// Non-final — live preview tail, replaced each message
nonFinalTexts.push(token.text);
}
}
// Full text = all accumulated finals + current non-final tail
const finalText = accumulatedFinals.join("");
const nonFinalText = nonFinalTexts.join("");
const fullText = (finalText + nonFinalText).trim();
if (hasEnd) {
// <end> guarantees all preceding tokens are final.
// Emit the complete accumulated text as is_final=true.
if (fullText) {
results.push(JSON.stringify({
type: "Results",
is_final: true,
speech_final: true,
channel: { alternatives: [{ transcript: fullText, confidence: 0.95 }] },
}));
}
results.push(JSON.stringify({ type: "UtteranceEnd" }));
// Reset for next utterance
accumulatedFinals = [];
} else if (fullText) {
// Still speaking — emit interim with full accumulated text
results.push(JSON.stringify({
type: "Results",
is_final: false,
speech_final: false,
channel: { alternatives: [{ transcript: fullText, confidence: 0.8 }] },
}));
}
return results;
} catch (e) {
console.error("[STT-WS:soniox] Parse error:", e.message);
return [];
}
};
}
wss.on("connection", (clientWs) => {
const cfg = getSTTConfig();
console.log(`[STT-WS] Proxying to ${cfg.provider}`);
let upstream;
try {
if (cfg.protocols) {
upstream = new WebSocket(cfg.wsUrl, cfg.protocols);
} else if (cfg.headers) {
upstream = new WebSocket(cfg.wsUrl, { headers: cfg.headers });
} else {
// Soniox and others: plain WebSocket, auth in first message
upstream = new WebSocket(cfg.wsUrl);
}
} catch (e) { clientWs.close(1011, "STT error"); return; }
upstream.on("unexpected-response", (_, res) => {
let b = ""; res.on("data", c => b += c);
res.on("end", () => { console.error(`[STT-WS] Rejected: ${res.statusCode}`); clientWs.close(1011); });
});
const buf = []; let ready = false;
upstream.on("open", () => {
ready = true; console.log(`[STT-WS] Connected to ${cfg.provider}`);
// Soniox: send config JSON as first message
if (cfg.initConfig) {
upstream.send(JSON.stringify(cfg.initConfig));
console.log(`[STT-WS:soniox] Sent init config (model=${cfg.initConfig.model}, lang=${cfg.initConfig.language_hints})`);
}
if (buf.length) { buf.forEach(c => upstream.send(c.isBinary ? c.data : c.data.toString())); buf.length = 0; }
});
clientWs.on("message", (d, isBinary) => {
if (ready && upstream.readyState === WebSocket.OPEN) {
if (isBinary) {
// Audio data — forward as-is
upstream.send(d);
} else {
// Text frame from client (KeepAlive, CloseStream, etc.)
const text = d.toString();
if (cfg.provider === "soniox") {
// Translate client keepalive/close to Soniox format
try {
const parsed = JSON.parse(text);
if (parsed.type === "KeepAlive") {
upstream.send(JSON.stringify({ type: "keepalive" }));
} else if (parsed.type === "CloseStream") {
upstream.send(""); // Soniox: empty string = end of audio
} else {
upstream.send(text);
}
} catch {
upstream.send(text);
}
} else {
upstream.send(text);
}
}
} else if (buf.length < 20) {
buf.push({ data: d, isBinary });
}
});
const sonioxNormalize = cfg.provider === "soniox" ? createSonioxNormalizer() : null;
upstream.on("message", (d, bin) => {
if (clientWs.readyState !== WebSocket.OPEN) return;
if (sonioxNormalize) {
const rawStr = typeof d === "string" ? d : d.toString();
const messages = sonioxNormalize(rawStr);
for (const m of messages) clientWs.send(m);
} else {
clientWs.send(bin ? d : d.toString());
}
});
const ping = setInterval(() => {
if (clientWs.readyState === WebSocket.OPEN) clientWs.ping();
if (upstream?.readyState === WebSocket.OPEN) {
upstream.ping();
if (cfg.provider === "deepgram") upstream.send(JSON.stringify({ type: "KeepAlive" }));
if (cfg.provider === "soniox") upstream.send(JSON.stringify({ type: "keepalive" }));
}
}, 5000);
const cleanup = () => { clearInterval(ping); };
clientWs.on("close", () => { cleanup(); if (upstream.readyState <= 1) upstream.terminate(); });
upstream.on("close", () => { cleanup(); if (clientWs.readyState === WebSocket.OPEN) clientWs.close(); });
clientWs.on("error", e => console.error("[STT-WS]", e.message));
upstream.on("error", e => { cleanup(); console.error("[STT-WS]", e.message); if (clientWs.readyState === WebSocket.OPEN) clientWs.close(1011); });
});
const shutdown = () => { wss.clients.forEach(ws => ws.close(1001)); wss.close(); pool.end(); server.close(() => process.exit(0)); setTimeout(() => process.exit(1), 5000); };
process.on("SIGTERM", shutdown);
process.on("SIGINT", shutdown);
console.log(`> STT WebSocket proxy active on /api/stt-ws (${getSTTConfig().provider})`);
}
// === START ===
if (process.env.NODE_ENV !== "production") {
// DEV MODE
const { createServer } = require("http");
const next = require("next");
const port = parseInt(process.env.PORT || "3000", 10);
const app = next({ dev: true, hostname: "0.0.0.0", port });
app.prepare().then(() => {
const handle = app.getRequestHandler();
const server = createServer((req, res) => handle(req, res, parse(req.url, true)));
addWSProxy(server);
server.listen(port, "0.0.0.0", () => console.log(`> Ready on http://0.0.0.0:${port}`));
});
} else {
// PRODUCTION: intercept the HTTP server that startServer creates, then add WS proxy
const http = require("http");
const origCreate = http.createServer;
http.createServer = function (...args) {
const server = origCreate.apply(this, args);
http.createServer = origCreate; // restore immediately
// Add WS proxy once server starts listening
const origListen = server.listen;
server.listen = function (...listenArgs) {
const result = origListen.apply(this, listenArgs);
addWSProxy(server);
return result;
};
return server;
};
// Now load the standard standalone server.js which calls startServer → http.createServer → listen
require("./server.js");
}