-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathApp.jsx
185 lines (156 loc) · 5.13 KB
/
App.jsx
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
import { useEffect, useState } from "react";
import ActiveCallDetail from "./components/ActiveCallDetail";
import Button from "./components/base/Button";
import Vapi from "@vapi-ai/web";
import { isPublicKeyMissingError } from "./utils";
// Put your Vapi Public Key below.
const vapi = new Vapi("310f0d43-27c2-47a5-a76d-e55171d024f7"); // Replace with your actual public key
const App = () => {
const [connecting, setConnecting] = useState(false);
const [connected, setConnected] = useState(false);
const [assistantIsSpeaking, setAssistantIsSpeaking] = useState(false);
const [volumeLevel, setVolumeLevel] = useState(0);
const [microphoneAllowed, setMicrophoneAllowed] = useState(false); // Add state for microphone permission
const { showPublicKeyInvalidMessage, setShowPublicKeyInvalidMessage } = usePublicKeyInvalid();
// hook into Vapi events
useEffect(() => {
const handleCallStart = () => {
setConnecting(false);
setConnected(true);
setShowPublicKeyInvalidMessage(false);
};
const handleCallEnd = () => {
setConnecting(false);
setConnected(false);
setShowPublicKeyInvalidMessage(false);
};
const handleSpeechStart = () => {
setAssistantIsSpeaking(true);
};
const handleSpeechEnd = () => {
setAssistantIsSpeaking(false);
};
const handleVolumeLevel = (level) => {
setVolumeLevel(level);
};
const handleError = (error) => {
console.error(error);
setConnecting(false);
if (isPublicKeyMissingError({ vapiError: error })) {
setShowPublicKeyInvalidMessage(true);
}
};
// Add event listeners
vapi.on("call-start", handleCallStart);
vapi.on("call-end", handleCallEnd);
vapi.on("speech-start", handleSpeechStart);
vapi.on("speech-end", handleSpeechEnd);
vapi.on("volume-level", handleVolumeLevel);
vapi.on("error", handleError);
// Clean up event listeners on unmount
return () => {
vapi.off("call-start", handleCallStart);
vapi.off("call-end", handleCallEnd);
vapi.off("speech-start", handleSpeechStart);
vapi.off("speech-end", handleSpeechEnd);
vapi.off("volume-level", handleVolumeLevel);
vapi.off("error", handleError);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// call start handler
const startCallInline = async () => {
setConnecting(true);
try {
// Request microphone access
await navigator.mediaDevices.getUserMedia({ audio: true });
setMicrophoneAllowed(true);
const assistantOverrides = {
transcriber: {
provider: "deepgram",
model: "nova-2",
language: "en-US",
},
recordingEnabled: true,
// Add any other overrides you need here, like:
// endCallOnNoSpeech: false, // Disable automatic end on no speech
// maxDuration: 3600, // Set a longer max duration (in seconds)
};
vapi.start('e3fff1dd-2e82-4cce-ac6c-8c3271eb0865', assistantOverrides);
} catch (error) {
console.error("Error accessing microphone:", error);
setConnecting(false);
// Handle microphone access errors (e.g., display an error message)
}
};
const endCall = () => {
vapi.stop();
};
return (
<div
style={{
display: "flex",
width: "100vw",
height: "100vh",
justifyContent: "center",
alignItems: "center",
}}
>
{!connected ? (
<Button
label="Call Scout"
onClick={startCallInline}
isLoading={connecting}
disabled={!microphoneAllowed} // Disable button if mic access is not allowed
icon={<LegalScoutIcon />}
/>
) : (
<ActiveCallDetail
assistantIsSpeaking={assistantIsSpeaking}
volumeLevel={volumeLevel}
onEndCallClick={endCall}
/>
)}
{showPublicKeyInvalidMessage ? <PleaseSetYourPublicKeyMessage /> : null}
</div>
);
};
// Make sure the image URL is correct and accessible
const LegalScoutIcon = () => (
<img
src="https://res.cloudinary.com/glide/image/fetch/f_auto,w_500,c_limit/https%3A%2F%2Fstorage.googleapis.com%2Fglide-prod.appspot.com%2Fuploads-v2%2FZf7Uh2x67Yz3nEftEH2i%2Fpub%2FipEv2VSSLIL0o0e2ostK.png"
alt="LegalScout Icon"
style={{ width: "24px", height: "24px" }}
/>
);
const usePublicKeyInvalid = () => {
const [showPublicKeyInvalidMessage, setShowPublicKeyInvalidMessage] = useState(false);
// close public key invalid message after delay
useEffect(() => {
if (showPublicKeyInvalidMessage) {
setTimeout(() => {
setShowPublicKeyInvalidMessage(false);
}, 3000);
}
}, [showPublicKeyInvalidMessage]);
return {
showPublicKeyInvalidMessage,
setShowPublicKeyInvalidMessage,
};
};
const PleaseSetYourPublicKeyMessage = () => {
return (
<div
style={{
display: "flex",
width: "100vw",
height: "100vh",
justifyContent: "center", // This centers horizontally
alignItems: "center", // This centers vertically
}}
>
Is your Vapi Public Key missing? (recheck your code)
</div>
);
};
export default App;