-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathserver.js
More file actions
306 lines (267 loc) · 9.32 KB
/
server.js
File metadata and controls
306 lines (267 loc) · 9.32 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* Node Voice Agent Starter - Backend Server
*
* Simple WebSocket proxy to Deepgram's Voice Agent API.
* Forwards all messages (JSON and binary) bidirectionally between client and Deepgram.
*
* Routes:
* GET /api/session - Issue JWT session token
* GET /api/metadata - Project metadata from deepgram.toml
* WS /api/voice-agent - WebSocket proxy to Deepgram Agent API (auth required)
*/
const { WebSocketServer, WebSocket } = require('ws');
const express = require('express');
const { createServer } = require('http');
const cors = require('cors');
const crypto = require('crypto');
const jwt = require('jsonwebtoken');
require('dotenv').config();
const path = require('path');
const fs = require('fs');
const toml = require('toml');
// Validate required environment variables
if (!process.env.DEEPGRAM_API_KEY) {
console.error('ERROR: DEEPGRAM_API_KEY environment variable is required');
console.error('Please copy sample.env to .env and add your API key');
process.exit(1);
}
// Configuration
const CONFIG = {
deepgramApiKey: process.env.DEEPGRAM_API_KEY,
deepgramAgentUrl: 'wss://agent.deepgram.com/v1/agent/converse',
port: process.env.PORT || 8081,
host: process.env.HOST || '0.0.0.0',
};
// ============================================================================
// SESSION AUTH - JWT tokens for production security
// ============================================================================
const SESSION_SECRET =
process.env.SESSION_SECRET || crypto.randomBytes(32).toString('hex');
const JWT_EXPIRY = '1h';
/**
* Validates JWT from WebSocket subprotocol: access_token.<jwt>
* Returns the token string if valid, null if invalid.
*/
function validateWsToken(protocols) {
if (!protocols) return null;
const list = Array.isArray(protocols) ? protocols : protocols.split(',').map(s => s.trim());
const tokenProto = list.find(p => p.startsWith('access_token.'));
if (!tokenProto) return null;
const token = tokenProto.slice('access_token.'.length);
try {
jwt.verify(token, SESSION_SECRET);
return tokenProto;
} catch {
return null;
}
}
const app = express();
const server = createServer(app);
const wss = new WebSocketServer({
noServer: true,
handleProtocols: (protocols) => {
// Accept the access_token.* subprotocol so the client sees it echoed back
for (const proto of protocols) {
if (proto.startsWith('access_token.')) return proto;
}
return false;
},
});
// Track all active WebSocket connections for graceful shutdown
const activeConnections = new Set();
// Enable CORS
app.use(cors());
// ============================================================================
// SESSION ROUTES - Auth endpoints (unprotected)
// ============================================================================
/**
* GET /api/session — Issues a signed JWT for session authentication.
*/
app.get('/api/session', (req, res) => {
const token = jwt.sign(
{ iat: Math.floor(Date.now() / 1000) },
SESSION_SECRET,
{ expiresIn: JWT_EXPIRY }
);
res.json({ token });
});
/**
* Metadata endpoint - required for standardization compliance
*/
app.get('/api/metadata', (req, res) => {
try {
const tomlPath = path.join(__dirname, 'deepgram.toml');
const tomlContent = fs.readFileSync(tomlPath, 'utf-8');
const config = toml.parse(tomlContent);
if (!config.meta) {
return res.status(500).json({
error: 'INTERNAL_SERVER_ERROR',
message: 'Missing [meta] section in deepgram.toml'
});
}
res.json(config.meta);
} catch (error) {
console.error('Error reading metadata:', error);
res.status(500).json({
error: 'INTERNAL_SERVER_ERROR',
message: 'Failed to read metadata from deepgram.toml'
});
}
});
/**
* WebSocket proxy handler
* Forwards all messages bidirectionally between client and Deepgram Agent API
*/
wss.on('connection', async (clientWs, request) => {
console.log('Client connected to /api/voice-agent');
activeConnections.add(clientWs);
try {
// Always use server-side API key for Deepgram connection
console.log('Initiating Deepgram connection...');
const deepgramWs = new WebSocket(CONFIG.deepgramAgentUrl, {
headers: {
'Authorization': `Token ${CONFIG.deepgramApiKey}`
}
});
// Forward all messages from Deepgram to client
deepgramWs.on('open', () => {
console.log('✓ Connected to Deepgram Agent API');
// Deepgram sends Welcome message automatically - just forward it
});
deepgramWs.on('message', (data, isBinary) => {
if (clientWs.readyState === WebSocket.OPEN) {
clientWs.send(data, { binary: isBinary });
}
});
deepgramWs.on('error', (error) => {
console.error('Deepgram WebSocket error:', error);
if (clientWs.readyState === WebSocket.OPEN) {
clientWs.send(JSON.stringify({
type: 'Error',
description: error.message || 'Deepgram connection error',
code: 'PROVIDER_ERROR'
}));
}
});
deepgramWs.on('close', (code, reason) => {
console.log(`Deepgram connection closed: ${code} ${reason}`);
if (clientWs.readyState === WebSocket.OPEN) {
// Use valid close code or default to 1000
// Reserved codes (1004, 1005, 1006, 1015) cannot be set by application
const reservedCodes = [1004, 1005, 1006, 1015];
const closeCode = (typeof code === 'number' && code >= 1000 && code <= 4999 && !reservedCodes.includes(code)) ? code : 1000;
clientWs.close(closeCode, reason);
}
});
// Forward all messages from client to Deepgram
clientWs.on('message', (data, isBinary) => {
if (deepgramWs.readyState === WebSocket.OPEN) {
deepgramWs.send(data, { binary: isBinary });
}
});
// Handle client disconnect
clientWs.on('close', (code, reason) => {
console.log(`Client disconnected: ${code} ${reason}`);
if (deepgramWs.readyState === WebSocket.OPEN) {
deepgramWs.close();
}
activeConnections.delete(clientWs);
});
// Handle client errors
clientWs.on('error', (error) => {
console.error('Client WebSocket error:', error);
if (deepgramWs.readyState === WebSocket.OPEN) {
deepgramWs.close();
}
});
} catch (error) {
console.error('Error setting up proxy:', error);
if (clientWs.readyState === WebSocket.OPEN) {
clientWs.send(JSON.stringify({
type: 'Error',
description: 'Failed to establish proxy connection',
code: 'CONNECTION_FAILED'
}));
clientWs.close();
}
}
});
/**
* Handle WebSocket upgrade requests for /api/voice-agent.
* Validates JWT from access_token.<jwt> subprotocol before upgrading.
*/
server.on('upgrade', (request, socket, head) => {
const pathname = new URL(request.url, 'http://localhost').pathname;
console.log(`WebSocket upgrade request for: ${pathname}`);
if (pathname === '/api/voice-agent') {
// Validate JWT from subprotocol
const protocols = request.headers['sec-websocket-protocol'];
const validProto = validateWsToken(protocols);
if (!validProto) {
console.log('WebSocket auth failed: invalid or missing token');
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
console.log('Backend handling /api/voice-agent WebSocket (authenticated)');
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request);
});
return;
}
// Unknown WebSocket path - reject
console.log(`Unknown WebSocket path: ${pathname}`);
socket.destroy();
});
/**
* Graceful shutdown handler
*/
function gracefulShutdown(signal) {
console.log(`\n${signal} signal received: starting graceful shutdown...`);
// Stop accepting new connections
wss.close(() => {
console.log('WebSocket server closed to new connections');
});
// Close all active WebSocket connections
console.log(`Closing ${activeConnections.size} active WebSocket connection(s)...`);
activeConnections.forEach((ws) => {
try {
ws.close(1001, 'Server shutting down');
} catch (error) {
console.error('Error closing WebSocket:', error);
}
});
// Close the HTTP server
server.close(() => {
console.log('HTTP server closed');
console.log('Shutdown complete');
process.exit(0);
});
// Force shutdown after 10 seconds if graceful shutdown fails
setTimeout(() => {
console.error('Could not close connections in time, forcefully shutting down');
process.exit(1);
}, 10000);
}
// Handle shutdown signals
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
// Handle uncaught errors
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
gracefulShutdown('UNCAUGHT_EXCEPTION');
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
gracefulShutdown('UNHANDLED_REJECTION');
});
// Start server
server.listen(CONFIG.port, CONFIG.host, () => {
console.log("\n" + "=".repeat(70));
console.log(`🚀 Backend API Server running at http://localhost:${CONFIG.port}`);
console.log("");
console.log(`📡 GET /api/session`);
console.log(`📡 WS /api/voice-agent (auth required)`);
console.log(`📡 GET /api/metadata`);
console.log("=".repeat(70) + "\n");
});