Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
84 lines (73 loc) · 3.02 KB
/
Copy pathserver.ts
File metadata and controls
84 lines (73 loc) · 3.02 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
// Server side of the call, the same one a browser would talk to.
// It lets Micdrop orchestrate the three AI steps: transcribe what the user
// says, answer with an LLM, speak the answer out loud. The app reaches it
// over a WebSocket, or over WebRTC when switched to it.
// https://micdrop.dev/docs/server
import { createServer } from 'node:http'
import { OpenaiAgent, OpenaiSTT, OpenaiTTS } from '@micdrop/openai'
import { MicdropConfig, MicdropServer } from '@micdrop/server'
import { WebRtcTransport } from '@micdrop/webrtc/server'
import { WebSocketServer } from 'ws'
// Loads Silero, which finds the turns of the WebRTC calls on the server
import '@micdrop/vad/node'
const PORT = 8087
const apiKey = process.env.OPENAI_API_KEY || ''
/** The AI steps of a call, whatever carries it */
function callConfig(): MicdropConfig {
return {
// Spoken right away, so the user hears a voice before the LLM has run.
// https://micdrop.dev/docs/server/first-message
firstMessage: 'Hi! What can I do for you?',
// The brain, an LLM that writes the answers.
// https://micdrop.dev/docs/agents/openai
agent: new OpenaiAgent({
apiKey,
systemPrompt:
'You are a friendly assistant on a phone. Your answers are spoken out loud, so keep them short and conversational.',
}),
// The ears, speech to text.
stt: new OpenaiSTT({ apiKey, language: 'en' }),
// The conversation shows the answer as the agent writes it, ahead of the
// voice that reads it out.
// https://micdrop.dev/docs/server/partial-messages
partialMessages: true,
// The mouth, text to speech.
tts: new OpenaiTTS({ apiKey, voice: 'alloy' }),
}
}
const http = createServer(async (request, response) => {
if (request.method !== 'POST' || request.url !== '/call') {
response.writeHead(404).end()
return
}
// A WebRTC call: the app posts its offer, the server answers it
// https://micdrop.dev/docs/transports/webrtc
let body = ''
for await (const chunk of request) body += chunk
try {
const transport = await WebRtcTransport.accept(JSON.parse(body))
new MicdropServer(transport, {
...callConfig(),
// react-native-webrtc sends the microphone without handing its samples
// to the app, so the server finds the turns
// https://micdrop.dev/docs/server/vad
vad: 'silero',
})
response.writeHead(200, { 'Content-Type': 'application/json' })
response.end(JSON.stringify(transport.answer))
} catch (error) {
console.error(error)
response.writeHead(400).end('Invalid offer')
}
})
// A WebSocket call, one per connection, on the same port
// https://micdrop.dev/docs/server/installation
const sockets = new WebSocketServer({ server: http })
sockets.on('connection', (socket) => {
new MicdropServer(socket, callConfig())
})
// Listening on every interface, so a phone on the same Wi-Fi can reach it
http.listen(PORT, '0.0.0.0', () => {
console.log(`Micdrop server listening on ws://0.0.0.0:${PORT}`)
console.log(`WebRTC offers answered on http://0.0.0.0:${PORT}/call`)
})