Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
224 lines (197 loc) · 7.94 KB
/
Copy pathindex.js
File metadata and controls
224 lines (197 loc) · 7.94 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
// index.js
/* -------------------------------------------------------------
Browser ⇆ Deepgram Voice-Agent bridge
– Listens on ws://localhost:5005
– Expects "realtime_audio.mixed" messages from the browser
– Sends "realtime_audio.bot_output" messages back
----------------------------------------------------------------*/
require("dotenv").config();
const { WebSocketServer } = require("ws");
const { createClient, AgentEvents } = require("@deepgram/sdk");
const fs = require("fs");
const http = require("http");
const https = require("https");
const path = require("path");
const { parse } = require("querystring");
const PORT = process.env.PORT || 5005;
const SAMPLE_RATE = 16_000;
const ATTENDEE_API_BASE_URL = process.env.ATTENDEE_API_BASE_URL || 'app.attendee.dev';
// Store agent configuration from form submission
let agentConfig = {
prompt: "You are a super duper helpful assistant",
greeting: "Hello! I'm your voice assistant. How can I help you today?",
model: "aura-2-thalia-en"
};
// -----------------------------------------------------------------------------
// Sanity-check API keys
// -----------------------------------------------------------------------------
const DG_KEY = process.env.DEEPGRAM_API_KEY;
if (!DG_KEY) {
console.error("❌ Set DEEPGRAM_API_KEY in your environment");
process.exit(1);
}
const ATTENDEE_API_KEY = process.env.ATTENDEE_API_KEY;
if (!ATTENDEE_API_KEY) {
console.error("❌ Set ATTENDEE_API_KEY in your environment");
process.exit(1);
}
// -----------------------------------------------------------------------------
// Helper: spin up a fresh Deepgram agent connection
// -----------------------------------------------------------------------------
function createAgent(onAudio) {
const deepgram = createClient(DG_KEY);
const agent = deepgram.agent();
agent.on(AgentEvents.Open, () => {
console.log("🟢 Deepgram agent WebSocket opened");
// Configure once the socket is up
agent.configure({
audio: {
input: { encoding: "linear16", sample_rate: SAMPLE_RATE },
output: { encoding: "linear16", sample_rate: SAMPLE_RATE, container: "none" },
},
agent: {
listen: { provider: { type: "deepgram", model: "nova-3" } },
think: {
provider: { type: "open_ai", model: "gpt-4o" },
prompt: agentConfig.prompt,
},
speak: { provider: { type: "deepgram", model: agentConfig.model } },
greeting: agentConfig.greeting,
},
});
});
// Pass raw audio bytes back to the caller
agent.on(AgentEvents.Audio, raw => onAudio(Buffer.from(raw)));
agent.on(AgentEvents.Error, err => console.error("Deepgram error:", err));
agent.on(AgentEvents.Close, () => console.log("Deepgram agent closed"));
agent.on(AgentEvents.Welcome, w => console.log("🙌 [DG] Welcome:", w));
agent.on(AgentEvents.ConversationText,
m => console.log(`💬 [DG] ${m.role}:`, m.content)); // role=user|agent
agent.on(AgentEvents.AgentThinking,
t => console.log("🤔 [DG] Agent thinking…", t)); // latency etc.
agent.on(AgentEvents.AgentStartedSpeaking,
s => console.log("🗣️ [DG] Agent started speaking:", s));
agent.on(AgentEvents.UserStartedSpeaking,
u => console.log("🎙️ [DG] User started speaking:", u));
agent.on(AgentEvents.AgentAudioDone,
d => console.log("✅ [DG] Agent audio done:", d));
// Keep the socket alive
const keepAlive = setInterval(() => agent.keepAlive?.(), 8_000);
agent.once(AgentEvents.Close, () => clearInterval(keepAlive));
return agent;
}
// -----------------------------------------------------------------------------
// HTTP and WebSocket server
// -----------------------------------------------------------------------------
const server = http.createServer((req, res) => {
if (req.method === "GET" && req.url === "/") {
fs.readFile(path.join(__dirname, "public", "index.html"), (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Error loading index.html");
return;
}
res.writeHead(200, { "Content-Type": "text/html" });
res.end(data);
});
} else if (req.method === "POST" && req.url === "/join-meeting") {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
req.on("end", () => {
const formData = parse(body);
// Store agent configuration from form
agentConfig.prompt = formData.prompt || agentConfig.prompt;
agentConfig.greeting = formData.greeting || agentConfig.greeting;
agentConfig.model = formData.model || agentConfig.model;
console.log("Meeting URL:", formData.meetingUrl);
console.log("WebSocket Tunnel URL:", formData.wsUrl);
console.log("Agent Prompt:", agentConfig.prompt);
console.log("Agent Greeting:", agentConfig.greeting);
console.log("Agent Model:", agentConfig.model);
// Make API request to attendee
const attendeeData = JSON.stringify({
meeting_url: formData.meetingUrl,
bot_name: "Attendee Voice Agent",
websocket_settings: {
audio: {
url: formData.wsUrl,
sample_rate: 16000
}
}
});
const options = {
hostname: ATTENDEE_API_BASE_URL,
port: 443,
path: '/api/v1/bots',
method: 'POST',
headers: {
'Authorization': `Token ${ATTENDEE_API_KEY}`,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(attendeeData)
}
};
const attendeeReq = https.request(options, (attendeeRes) => {
let responseData = '';
attendeeRes.on('data', (chunk) => {
responseData += chunk;
});
attendeeRes.on('end', () => {
if (attendeeRes.statusCode >= 200 && attendeeRes.statusCode < 300) {
console.log('✅ Bot launch successful:', responseData);
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Success! The bot will join the meeting in 30 seconds and start speaking 30 seconds after joining.");
} else {
console.error('❌ Bot launch failed:', attendeeRes.statusCode, responseData);
res.writeHead(500, { "Content-Type": "text/plain" });
res.end(`Error launching bot: ${attendeeRes.statusCode} - ${responseData}`);
}
});
});
attendeeReq.on('error', (error) => {
console.error('❌ API request error:', error);
res.writeHead(500, { "Content-Type": "text/plain" });
res.end(`Error making API request: ${error.message}`);
});
attendeeReq.write(attendeeData);
attendeeReq.end();
});
} else {
res.writeHead(404);
res.end();
}
});
const wss = new WebSocketServer({ server });
server.listen(PORT, () => {
console.log(`🚀 HTTP and Voice bridge running on http://localhost:${PORT}`);
});
wss.on("connection", (client, req) => {
console.log(`⇦ Browser connected from ${req.socket.remoteAddress}`);
// Create agent and wire audio → browser using stored configuration
const agent = createAgent(buffer => {
const payload = {
trigger: "realtime_audio.bot_output",
data: { chunk: buffer.toString("base64"), sample_rate: SAMPLE_RATE },
};
client.send(JSON.stringify(payload));
});
// Browser → agent
client.on("message", msg => {
try {
const parsed = JSON.parse(msg.toString());
if (parsed.trigger === "realtime_audio.mixed" && parsed?.data?.chunk) {
const audio = Buffer.from(parsed.data.chunk, "base64");
agent.send(audio); // stream to Deepgram
} else {
console.log("Received non-audio message:", parsed);
}
} catch (err) {
console.error("Bad WS message:", err);
}
});
client.on("close", () => {
console.log("⇨ Browser disconnected");
agent.finish?.();
});
});