create-micdrop 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -2
- package/dist/index.js +1315 -0
- package/package.json +53 -4
- package/templates/expo/App.tsx +123 -0
- package/templates/expo/README.md +65 -0
- package/templates/expo/_env.example +6 -0
- package/templates/expo/_gitignore +7 -0
- package/templates/expo/app.json +34 -0
- package/templates/expo/babel.config.js +6 -0
- package/templates/expo/index.ts +4 -0
- package/templates/expo/package.json +39 -0
- package/templates/expo/server/index.ts +36 -0
- package/templates/expo/server/tsconfig.json +14 -0
- package/templates/expo/src/serverUrl.ts +20 -0
- package/templates/expo/tsconfig.json +8 -0
- package/templates/nextjs/README.md +40 -0
- package/templates/nextjs/_env.example +9 -0
- package/templates/nextjs/_gitignore +7 -0
- package/templates/nextjs/app/CallLoader.tsx +10 -0
- package/templates/nextjs/app/VoiceCall.tsx +71 -0
- package/templates/nextjs/app/globals.css +45 -0
- package/templates/nextjs/app/layout.tsx +16 -0
- package/templates/nextjs/app/page.tsx +10 -0
- package/templates/nextjs/next.config.ts +5 -0
- package/templates/nextjs/package.json +34 -0
- package/templates/nextjs/server/index.ts +35 -0
- package/templates/nextjs/tsconfig.json +25 -0
- package/templates/nextjs/tsconfig.server.json +14 -0
- package/templates/node/README.md +48 -0
- package/templates/node/_env.example +6 -0
- package/templates/node/_gitignore +4 -0
- package/templates/node/package.json +21 -0
- package/templates/node/src/server.ts +34 -0
- package/templates/node/tsconfig.json +14 -0
- package/templates/phone/README.md +86 -0
- package/templates/phone/_env.example +31 -0
- package/templates/phone/_gitignore +5 -0
- package/templates/phone/package.json +25 -0
- package/templates/phone/src/recording.ts +65 -0
- package/templates/phone/src/server.ts +212 -0
- package/templates/phone/src/webhooks.ts +88 -0
- package/templates/phone/telephony/plivo/env.example +6 -0
- package/templates/phone/telephony/plivo/setup.md +7 -0
- package/templates/phone/telephony/plivo/telephony.ts +111 -0
- package/templates/phone/telephony/telnyx/env.example +8 -0
- package/templates/phone/telephony/telnyx/setup.md +9 -0
- package/templates/phone/telephony/telnyx/telephony.ts +107 -0
- package/templates/phone/telephony/twilio/env.example +6 -0
- package/templates/phone/telephony/twilio/setup.md +7 -0
- package/templates/phone/telephony/twilio/telephony.ts +98 -0
- package/templates/phone/telephony/vonage/env.example +7 -0
- package/templates/phone/telephony/vonage/setup.md +15 -0
- package/templates/phone/telephony/vonage/telephony.ts +105 -0
- package/templates/phone/tsconfig.json +14 -0
- package/templates/vite-react/README.md +39 -0
- package/templates/vite-react/_env.example +2 -0
- package/templates/vite-react/_gitignore +4 -0
- package/templates/vite-react/index.html +12 -0
- package/templates/vite-react/package.json +36 -0
- package/templates/vite-react/src/client/App.tsx +75 -0
- package/templates/vite-react/src/client/main.tsx +10 -0
- package/templates/vite-react/src/client/styles.css +45 -0
- package/templates/vite-react/src/server/index.ts +42 -0
- package/templates/vite-react/tsconfig.json +16 -0
- package/templates/vite-react/tsconfig.server.json +14 -0
- package/templates/vite-react/vite.config.ts +23 -0
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-micdrop",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "\ud83d\udd90\ufe0f\ud83c\udfa4 Micdrop: start a voice AI app in one command",
|
|
5
|
+
"author": "Godefroy de Compreignac",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"bin": {
|
|
9
|
+
"create-micdrop": "dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsup",
|
|
13
|
+
"dev": "tsup --watch",
|
|
14
|
+
"clean": "rm -rf dist",
|
|
15
|
+
"typecheck": "tsc --noEmit",
|
|
16
|
+
"prepublishOnly": "npm run build",
|
|
17
|
+
"test": "node --import tsx --test tests/*.test.ts",
|
|
18
|
+
"test:templates": "tsx scripts/test-templates.ts"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"templates"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20.12"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/Godefroy/micdrop.git",
|
|
30
|
+
"directory": "packages/create-micdrop"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://micdrop.dev/docs/templates",
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"micdrop",
|
|
38
|
+
"create",
|
|
39
|
+
"scaffold",
|
|
40
|
+
"template",
|
|
41
|
+
"voice agent",
|
|
42
|
+
"voice ai",
|
|
43
|
+
"phone agent"
|
|
44
|
+
],
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@clack/prompts": "^1.9.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^22.19.13",
|
|
50
|
+
"prettier": "^3.9.10",
|
|
51
|
+
"tsup": "^8.5.1",
|
|
52
|
+
"tsx": "^4.20.6",
|
|
53
|
+
"typescript": "^5.9.3"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// A whole voice call in one screen. Micdrop takes care of the microphone
|
|
2
|
+
// permission, the voice activity detection, the audio session and the
|
|
3
|
+
// WebSocket, so this only reads a state and draws it.
|
|
4
|
+
// https://micdrop.dev/docs/react-native
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
useMicdropEndCall,
|
|
8
|
+
useMicdropError,
|
|
9
|
+
useMicdropState,
|
|
10
|
+
} from '@micdrop/react'
|
|
11
|
+
import { Micdrop, MicdropState } from '@micdrop/react-native'
|
|
12
|
+
import { StatusBar } from 'expo-status-bar'
|
|
13
|
+
import { useCallback, useState } from 'react'
|
|
14
|
+
import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'
|
|
15
|
+
import {
|
|
16
|
+
SafeAreaProvider,
|
|
17
|
+
useSafeAreaInsets,
|
|
18
|
+
} from 'react-native-safe-area-context'
|
|
19
|
+
import { getServerUrl } from './src/serverUrl'
|
|
20
|
+
|
|
21
|
+
export default function App() {
|
|
22
|
+
return (
|
|
23
|
+
<SafeAreaProvider>
|
|
24
|
+
<StatusBar style="dark" />
|
|
25
|
+
<Call />
|
|
26
|
+
</SafeAreaProvider>
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function Call() {
|
|
31
|
+
const insets = useSafeAreaInsets()
|
|
32
|
+
// Everything about the call lives in this one state
|
|
33
|
+
// https://micdrop.dev/docs/react-native/hooks
|
|
34
|
+
const state = useMicdropState()
|
|
35
|
+
const [error, setError] = useState<string>()
|
|
36
|
+
const isOn = state.isStarted || state.isStarting
|
|
37
|
+
|
|
38
|
+
const handleCall = useCallback(async () => {
|
|
39
|
+
setError(undefined)
|
|
40
|
+
try {
|
|
41
|
+
if (isOn) await Micdrop.stop()
|
|
42
|
+
// Asks for the microphone, connects and starts listening
|
|
43
|
+
else await Micdrop.start({ url: getServerUrl() })
|
|
44
|
+
} catch {
|
|
45
|
+
// Reported by useMicdropError below
|
|
46
|
+
}
|
|
47
|
+
}, [isOn])
|
|
48
|
+
|
|
49
|
+
// The assistant hangs up once the user says goodbye
|
|
50
|
+
useMicdropEndCall(useCallback(() => Micdrop.stopAfterAnswer(), []))
|
|
51
|
+
|
|
52
|
+
// Microphone refused, server unreachable, and so on
|
|
53
|
+
useMicdropError(
|
|
54
|
+
useCallback((micdropError) => {
|
|
55
|
+
setError(micdropError.message || micdropError.code)
|
|
56
|
+
}, [])
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<View
|
|
61
|
+
style={[
|
|
62
|
+
styles.screen,
|
|
63
|
+
{ paddingTop: insets.top + 16, paddingBottom: insets.bottom + 16 },
|
|
64
|
+
]}
|
|
65
|
+
>
|
|
66
|
+
<Text style={styles.title}>🖐️🎤 {{ name }}</Text>
|
|
67
|
+
<Text style={styles.status}>{error ?? getStatus(state)}</Text>
|
|
68
|
+
|
|
69
|
+
<ScrollView contentContainerStyle={styles.conversation}>
|
|
70
|
+
{state.conversation.map((item, index) =>
|
|
71
|
+
item.role === 'user' || item.role === 'assistant' ? (
|
|
72
|
+
<Text
|
|
73
|
+
key={index}
|
|
74
|
+
style={[
|
|
75
|
+
styles.message,
|
|
76
|
+
item.role === 'user' ? styles.user : styles.assistant,
|
|
77
|
+
]}
|
|
78
|
+
>
|
|
79
|
+
{item.content}
|
|
80
|
+
</Text>
|
|
81
|
+
) : null
|
|
82
|
+
)}
|
|
83
|
+
</ScrollView>
|
|
84
|
+
|
|
85
|
+
<Pressable
|
|
86
|
+
accessibilityRole="button"
|
|
87
|
+
onPress={handleCall}
|
|
88
|
+
style={[styles.button, isOn && styles.buttonStop]}
|
|
89
|
+
>
|
|
90
|
+
<Text style={styles.buttonText}>
|
|
91
|
+
{isOn ? 'End the call' : 'Start a call'}
|
|
92
|
+
</Text>
|
|
93
|
+
</Pressable>
|
|
94
|
+
</View>
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function getStatus(state: MicdropState) {
|
|
99
|
+
if (state.isStarting) return 'Starting the call'
|
|
100
|
+
if (!state.isStarted) return 'Ready'
|
|
101
|
+
if (state.isUserSpeaking) return 'Listening to you'
|
|
102
|
+
if (state.isAssistantSpeaking) return 'Assistant is speaking'
|
|
103
|
+
if (state.isProcessing) return 'Assistant is thinking'
|
|
104
|
+
return 'Your turn'
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const styles = StyleSheet.create({
|
|
108
|
+
screen: { flex: 1, gap: 12, paddingHorizontal: 20, backgroundColor: '#fff' },
|
|
109
|
+
title: { fontSize: 28, fontWeight: '800' },
|
|
110
|
+
status: { color: '#666' },
|
|
111
|
+
conversation: { gap: 8, paddingVertical: 8 },
|
|
112
|
+
message: { padding: 10, borderRadius: 12, maxWidth: '85%' },
|
|
113
|
+
user: { alignSelf: 'flex-end', backgroundColor: '#ede9fe' },
|
|
114
|
+
assistant: { alignSelf: 'flex-start', backgroundColor: '#f3f4f6' },
|
|
115
|
+
button: {
|
|
116
|
+
alignItems: 'center',
|
|
117
|
+
padding: 16,
|
|
118
|
+
borderRadius: 999,
|
|
119
|
+
backgroundColor: '#6d28d9',
|
|
120
|
+
},
|
|
121
|
+
buttonStop: { backgroundColor: '#dc2626' },
|
|
122
|
+
buttonText: { color: '#fff', fontSize: 17, fontWeight: '700' },
|
|
123
|
+
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# {{name}}
|
|
2
|
+
|
|
3
|
+
A voice AI app for iOS and Android built with [Micdrop](https://micdrop.dev)
|
|
4
|
+
and Expo, with the Node server that runs the AI next to it.
|
|
5
|
+
|
|
6
|
+
- [`App.tsx`](./App.tsx) starts the call and draws its state
|
|
7
|
+
- [`server/index.ts`](./server/index.ts) runs each call
|
|
8
|
+
- [`server/ai.ts`](./server/ai.ts) builds the AI providers
|
|
9
|
+
|
|
10
|
+
## Run it
|
|
11
|
+
|
|
12
|
+
The app records and plays audio natively, so it runs from a development
|
|
13
|
+
build. Expo Go does not include these libraries.
|
|
14
|
+
|
|
15
|
+
Fill `server/.env` with your keys, then build and run the app once, on a
|
|
16
|
+
simulator or a phone plugged in:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
{{run:ios}}
|
|
20
|
+
# or
|
|
21
|
+
{{run:android}}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Then start the server and the bundler together:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
{{run:dev}}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Tap **Start a call**, allow the microphone, and talk. Later runs only need
|
|
31
|
+
`{{run:dev}}`.
|
|
32
|
+
|
|
33
|
+
## The key stays out of the app
|
|
34
|
+
|
|
35
|
+
The keys sit in `server/.env`, next to the server they belong to. Never prefix
|
|
36
|
+
a secret with `EXPO_PUBLIC_`: that prefix puts a value into the compiled app,
|
|
37
|
+
in plain text.
|
|
38
|
+
|
|
39
|
+
## Reaching the server
|
|
40
|
+
|
|
41
|
+
The app finds the server on the machine Expo served it from, so a simulator
|
|
42
|
+
and a phone on the same Wi-Fi work without configuration (see
|
|
43
|
+
[`src/serverUrl.ts`](./src/serverUrl.ts)). If your machine blocks incoming
|
|
44
|
+
connections, allow port 3001. A phone plugged in over USB reaches it through
|
|
45
|
+
`adb reverse tcp:3001 tcp:3001`.
|
|
46
|
+
|
|
47
|
+
In production, point the app at your deployed server with `wss://`.
|
|
48
|
+
|
|
49
|
+
## Bundle identifier
|
|
50
|
+
|
|
51
|
+
`app.json` uses `com.example.{{bundleName}}`. Replace it with your own before
|
|
52
|
+
publishing to the stores.
|
|
53
|
+
|
|
54
|
+
## AI providers
|
|
55
|
+
|
|
56
|
+
{{providers}}
|
|
57
|
+
|
|
58
|
+
Swap any of them in `server/ai.ts`. The [providers page](https://micdrop.dev/docs/providers)
|
|
59
|
+
lists them all.
|
|
60
|
+
|
|
61
|
+
## Learn more
|
|
62
|
+
|
|
63
|
+
- [React Native documentation](https://micdrop.dev/docs/react-native)
|
|
64
|
+
- [Silero voice detection](https://micdrop.dev/docs/react-native/vad) for noisy places
|
|
65
|
+
- [Tools](https://micdrop.dev/docs/server/tools): let the assistant call your functions
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"expo": {
|
|
3
|
+
"name": "{{name}}",
|
|
4
|
+
"slug": "{{name}}",
|
|
5
|
+
"platforms": ["ios", "android"],
|
|
6
|
+
"version": "0.1.0",
|
|
7
|
+
"orientation": "portrait",
|
|
8
|
+
"newArchEnabled": true,
|
|
9
|
+
"ios": {
|
|
10
|
+
"bundleIdentifier": "com.example.{{bundleName}}",
|
|
11
|
+
"supportsTablet": true
|
|
12
|
+
},
|
|
13
|
+
"android": {
|
|
14
|
+
"package": "com.example.{{bundleName}}"
|
|
15
|
+
},
|
|
16
|
+
"plugins": [
|
|
17
|
+
"expo-dev-client",
|
|
18
|
+
[
|
|
19
|
+
"react-native-audio-api",
|
|
20
|
+
{
|
|
21
|
+
"iosMicrophonePermission": "The microphone lets you talk with the assistant.",
|
|
22
|
+
"androidPermissions": [
|
|
23
|
+
"android.permission.RECORD_AUDIO",
|
|
24
|
+
"android.permission.MODIFY_AUDIO_SETTINGS",
|
|
25
|
+
"android.permission.FOREGROUND_SERVICE",
|
|
26
|
+
"android.permission.FOREGROUND_SERVICE_MICROPHONE",
|
|
27
|
+
"android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"
|
|
28
|
+
],
|
|
29
|
+
"androidFSTypes": ["microphone", "mediaPlayback"]
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{name}}",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "concurrently -n server,app -c blue,green \"npm:dev:server\" \"npm:dev:app\"",
|
|
8
|
+
"dev:server": "tsx watch --env-file=server/.env server/index.ts",
|
|
9
|
+
"dev:app": "expo start --dev-client",
|
|
10
|
+
"ios": "expo run:ios",
|
|
11
|
+
"android": "expo run:android",
|
|
12
|
+
"typecheck": "tsc --noEmit && tsc -p server/tsconfig.json"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@micdrop/react": "*",
|
|
16
|
+
"@micdrop/react-native": "*",
|
|
17
|
+
"expo": "~58.1.0",
|
|
18
|
+
"expo-constants": "~58.0.15",
|
|
19
|
+
"expo-dev-client": "~58.0.18",
|
|
20
|
+
"expo-status-bar": "~58.0.4",
|
|
21
|
+
"react": "19.3.0",
|
|
22
|
+
"react-native": "0.88.1",
|
|
23
|
+
"react-native-audio-api": "^0.13.3",
|
|
24
|
+
"react-native-gesture-handler": "~3.3.0",
|
|
25
|
+
"react-native-reanimated": "4.7.1",
|
|
26
|
+
"react-native-safe-area-context": "~5.11.1",
|
|
27
|
+
"react-native-worklets": "0.13.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@micdrop/server": "*",
|
|
31
|
+
"@types/node": "^22.19.13",
|
|
32
|
+
"@types/react": "~19.3.0",
|
|
33
|
+
"@types/ws": "^8.18.1",
|
|
34
|
+
"concurrently": "^9.2.1",
|
|
35
|
+
"tsx": "^4.20.6",
|
|
36
|
+
"typescript": "~6.0.3",
|
|
37
|
+
"ws": "^8.19.0"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// The voice server the app talks to. Each WebSocket connection is a call:
|
|
2
|
+
// Micdrop transcribes what the user says, has the agent answer and speaks the
|
|
3
|
+
// answer back. The keys stay here, out of the app.
|
|
4
|
+
// https://micdrop.dev/docs/server
|
|
5
|
+
|
|
6
|
+
import { endCallTool, MicdropServer } from '@micdrop/server'
|
|
7
|
+
import { WebSocketServer } from 'ws'
|
|
8
|
+
import { createAi } from './ai'
|
|
9
|
+
|
|
10
|
+
const PORT = Number(process.env.PORT || 3001)
|
|
11
|
+
|
|
12
|
+
// Listens on every interface, so a phone on the same Wi-Fi reaches it
|
|
13
|
+
const server = new WebSocketServer({ port: PORT, host: '0.0.0.0' })
|
|
14
|
+
|
|
15
|
+
server.on('connection', (socket) => {
|
|
16
|
+
new MicdropServer(socket, {
|
|
17
|
+
// Spoken right away, before the user says anything
|
|
18
|
+
// https://micdrop.dev/docs/server/first-message
|
|
19
|
+
firstMessage: 'Hi! What can I do for you?',
|
|
20
|
+
|
|
21
|
+
// The answer shows up in the app as it is written
|
|
22
|
+
// https://micdrop.dev/docs/server/partial-messages
|
|
23
|
+
partialMessages: true,
|
|
24
|
+
|
|
25
|
+
// The providers picked when the project was created, in ai.ts
|
|
26
|
+
...createAi({
|
|
27
|
+
systemPrompt:
|
|
28
|
+
'You are a friendly assistant on a phone. Your answers are spoken out loud, so keep them short and conversational.',
|
|
29
|
+
// Lets the assistant hang up once the user says goodbye
|
|
30
|
+
// https://micdrop.dev/docs/server/auto-end-call
|
|
31
|
+
tools: [endCallTool()],
|
|
32
|
+
}),
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
console.log(`Micdrop server listening on ws://0.0.0.0:${PORT}`)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"types": ["node"],
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"noEmit": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["."]
|
|
14
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import Constants from 'expo-constants'
|
|
2
|
+
import { Platform } from 'react-native'
|
|
3
|
+
|
|
4
|
+
const PORT = 3001
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Where the WebSocket of a call connects.
|
|
8
|
+
*
|
|
9
|
+
* Expo knows the address the app was loaded from, the machine running the
|
|
10
|
+
* server, so a simulator and a phone on the same Wi-Fi reach it with nothing
|
|
11
|
+
* to configure. An Android emulator has its own loopback, and reaches its
|
|
12
|
+
* host through 10.0.2.2.
|
|
13
|
+
*/
|
|
14
|
+
export function getServerUrl(): string {
|
|
15
|
+
let host = Constants.expoConfig?.hostUri?.split(':')[0] ?? 'localhost'
|
|
16
|
+
if (Platform.OS === 'android' && ['localhost', '127.0.0.1'].includes(host)) {
|
|
17
|
+
host = '10.0.2.2'
|
|
18
|
+
}
|
|
19
|
+
return `ws://${host}:${PORT}`
|
|
20
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# {{name}}
|
|
2
|
+
|
|
3
|
+
A voice AI app built with [Micdrop](https://micdrop.dev) and Next.js. The page
|
|
4
|
+
holds the call, and a Node server next to Next.js runs the AI over a
|
|
5
|
+
WebSocket.
|
|
6
|
+
|
|
7
|
+
- [`app/VoiceCall.tsx`](./app/VoiceCall.tsx) starts the call and draws its state
|
|
8
|
+
- [`server/index.ts`](./server/index.ts) runs each call
|
|
9
|
+
- [`server/ai.ts`](./server/ai.ts) builds the AI providers
|
|
10
|
+
|
|
11
|
+
## Run it
|
|
12
|
+
|
|
13
|
+
Fill `.env` with your keys, then:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
{{run:dev}}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Open http://localhost:3000, start a call and talk. Say goodbye, and the
|
|
20
|
+
assistant hangs up.
|
|
21
|
+
|
|
22
|
+
## Why a server next to Next.js
|
|
23
|
+
|
|
24
|
+
A call keeps a WebSocket open for its whole length, which the route handlers
|
|
25
|
+
of Next.js do not hold. The voice server is a plain Node process: deploy it
|
|
26
|
+
wherever Node runs a long lived process, and point `NEXT_PUBLIC_MICDROP_URL`
|
|
27
|
+
at it with `wss://`.
|
|
28
|
+
|
|
29
|
+
## AI providers
|
|
30
|
+
|
|
31
|
+
{{providers}}
|
|
32
|
+
|
|
33
|
+
Swap any of them in `server/ai.ts`. The [providers page](https://micdrop.dev/docs/providers)
|
|
34
|
+
lists them all.
|
|
35
|
+
|
|
36
|
+
## Learn more
|
|
37
|
+
|
|
38
|
+
- [React hooks](https://micdrop.dev/docs/client/react-hooks)
|
|
39
|
+
- [Tools](https://micdrop.dev/docs/server/tools): let the assistant call your functions
|
|
40
|
+
- [Auth and parameters](https://micdrop.dev/docs/server/auth-and-parameters): check who connects
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Port of the voice server, which runs next to Next.js
|
|
2
|
+
PORT=3001
|
|
3
|
+
|
|
4
|
+
# Where the browser reaches the voice server. Use wss:// in production.
|
|
5
|
+
NEXT_PUBLIC_MICDROP_URL=ws://localhost:3001
|
|
6
|
+
|
|
7
|
+
# Logs of the calls: info shows each turn, debug each step of the
|
|
8
|
+
# providers. Warnings and errors only by default.
|
|
9
|
+
# MICDROP_LOG=info
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import dynamic from 'next/dynamic'
|
|
4
|
+
|
|
5
|
+
// The call needs the microphone and the Web Audio API, which only exist in
|
|
6
|
+
// the browser, so it is never rendered on the server
|
|
7
|
+
export const CallLoader = dynamic(() => import('./VoiceCall'), {
|
|
8
|
+
ssr: false,
|
|
9
|
+
loading: () => <p className="status">Loading</p>,
|
|
10
|
+
})
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
// A whole voice call in one component. Micdrop takes care of the microphone,
|
|
4
|
+
// the voice activity detection, the speaker and the WebSocket, so this only
|
|
5
|
+
// reads a state and draws it.
|
|
6
|
+
// https://micdrop.dev/docs/client
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
useMicdropEndCall,
|
|
10
|
+
useMicdropError,
|
|
11
|
+
useMicdropState,
|
|
12
|
+
} from '@micdrop/react'
|
|
13
|
+
import { Micdrop, MicdropState } from '@micdrop/web'
|
|
14
|
+
import { useCallback, useState } from 'react'
|
|
15
|
+
|
|
16
|
+
const SERVER_URL = process.env.NEXT_PUBLIC_MICDROP_URL || 'ws://localhost:3001'
|
|
17
|
+
|
|
18
|
+
export default function VoiceCall() {
|
|
19
|
+
// Everything about the call lives in this one state
|
|
20
|
+
// https://micdrop.dev/docs/client/react-hooks
|
|
21
|
+
const state = useMicdropState()
|
|
22
|
+
const [error, setError] = useState<string>()
|
|
23
|
+
const isOn = state.isStarted || state.isStarting
|
|
24
|
+
|
|
25
|
+
const handleCall = useCallback(async () => {
|
|
26
|
+
setError(undefined)
|
|
27
|
+
try {
|
|
28
|
+
if (isOn) await Micdrop.stop()
|
|
29
|
+
// Asks for the microphone, connects and starts listening
|
|
30
|
+
else await Micdrop.start({ url: SERVER_URL })
|
|
31
|
+
} catch {
|
|
32
|
+
// Reported by useMicdropError below
|
|
33
|
+
}
|
|
34
|
+
}, [isOn])
|
|
35
|
+
|
|
36
|
+
// The assistant hangs up once the user says goodbye
|
|
37
|
+
useMicdropEndCall(useCallback(() => Micdrop.stopAfterAnswer(), []))
|
|
38
|
+
|
|
39
|
+
// Microphone refused, server unreachable, and so on
|
|
40
|
+
// https://micdrop.dev/docs/client/error-handling
|
|
41
|
+
useMicdropError(
|
|
42
|
+
useCallback((micdropError) => {
|
|
43
|
+
setError(micdropError.message || micdropError.code)
|
|
44
|
+
}, [])
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<section>
|
|
49
|
+
<button onClick={handleCall}>{isOn ? 'Stop call' : 'Start call'}</button>
|
|
50
|
+
<p className="status">{error ?? getStatus(state)}</p>
|
|
51
|
+
<ul className="conversation">
|
|
52
|
+
{state.conversation.map((item, index) =>
|
|
53
|
+
item.role === 'user' || item.role === 'assistant' ? (
|
|
54
|
+
<li key={index} className={item.role}>
|
|
55
|
+
{item.content}
|
|
56
|
+
</li>
|
|
57
|
+
) : null
|
|
58
|
+
)}
|
|
59
|
+
</ul>
|
|
60
|
+
</section>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getStatus(state: MicdropState) {
|
|
65
|
+
if (state.isStarting) return 'Starting the call'
|
|
66
|
+
if (!state.isStarted) return 'Ready'
|
|
67
|
+
if (state.isUserSpeaking) return 'Listening to you'
|
|
68
|
+
if (state.isAssistantSpeaking) return 'Assistant is speaking'
|
|
69
|
+
if (state.isProcessing) return 'Assistant is thinking'
|
|
70
|
+
return 'Your turn'
|
|
71
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
body {
|
|
2
|
+
font-family: system-ui, sans-serif;
|
|
3
|
+
max-width: 36rem;
|
|
4
|
+
margin: 4rem auto;
|
|
5
|
+
padding: 0 1rem;
|
|
6
|
+
color: #111;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
button {
|
|
10
|
+
font: inherit;
|
|
11
|
+
padding: 0.6rem 1.2rem;
|
|
12
|
+
border-radius: 999px;
|
|
13
|
+
border: none;
|
|
14
|
+
background: #6d28d9;
|
|
15
|
+
color: white;
|
|
16
|
+
cursor: pointer;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
.status {
|
|
20
|
+
color: #666;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.conversation {
|
|
24
|
+
list-style: none;
|
|
25
|
+
padding: 0;
|
|
26
|
+
display: flex;
|
|
27
|
+
flex-direction: column;
|
|
28
|
+
gap: 0.5rem;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.conversation li {
|
|
32
|
+
padding: 0.5rem 0.8rem;
|
|
33
|
+
border-radius: 0.8rem;
|
|
34
|
+
max-width: 80%;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.conversation .user {
|
|
38
|
+
align-self: flex-end;
|
|
39
|
+
background: #ede9fe;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.conversation .assistant {
|
|
43
|
+
align-self: flex-start;
|
|
44
|
+
background: #f3f4f6;
|
|
45
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Metadata } from 'next'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
import './globals.css'
|
|
4
|
+
|
|
5
|
+
export const metadata: Metadata = {
|
|
6
|
+
title: '{{name}}',
|
|
7
|
+
description: 'A voice AI app built with Micdrop',
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default function RootLayout({ children }: { children: ReactNode }) {
|
|
11
|
+
return (
|
|
12
|
+
<html lang="en">
|
|
13
|
+
<body>{children}</body>
|
|
14
|
+
</html>
|
|
15
|
+
)
|
|
16
|
+
}
|