-
Notifications
You must be signed in to change notification settings - Fork 2
/
useVapi.ts
122 lines (106 loc) · 3.04 KB
/
useVapi.ts
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
import { useEffect, useState } from 'react';
import { Message, MessageTypeEnum, TranscriptMessage, TranscriptMessageTypeEnum } from './lib/types/conversation.type';
import vapi from './vapi.sdk';
export enum CALL_STATUS {
INACTIVE = 'inactive',
ACTIVE = 'active',
LOADING = 'loading',
}
export function useVapi() {
const [callStatus, setCallStatus] = useState<CALL_STATUS>(CALL_STATUS.INACTIVE);
const [messages, setMessages] = useState<Message[]>([]);
const [activeTranscript, setActiveTranscript] = useState<TranscriptMessage | null>(null);
useEffect(() => {
const onCallStartHandler = () => {
console.log('Call has started');
setCallStatus(CALL_STATUS.ACTIVE);
};
const onCallEnd = () => {
console.log('call has ended ok');
console.log('Call has stopped');
setCallStatus(CALL_STATUS.INACTIVE);
};
const onMessageUpdate = (message: Message) => {
console.log('message', message);
if (message.type === MessageTypeEnum.TRANSCRIPT && message.transcriptType === TranscriptMessageTypeEnum.PARTIAL) {
setActiveTranscript(message);
} else {
setMessages((prev) => [...prev, message]);
setActiveTranscript(null);
}
};
const onError = (e: any) => {
console.log('on error triggered');
setCallStatus(CALL_STATUS.INACTIVE);
console.error(e);
};
vapi.on('call-start', onCallStartHandler);
vapi.on('call-end', onCallEnd);
vapi.on('message', onMessageUpdate);
vapi.on('error', onError);
return () => {
vapi.off('call-start', onCallStartHandler);
vapi.off('call-end', onCallEnd);
vapi.off('message', onMessageUpdate);
vapi.off('error', onError);
};
}, []);
const start = async () => {
console.log('starting the call');
setCallStatus(CALL_STATUS.ACTIVE);
// setCallStatus(CALL_STATUS.LOADING);
// const response = vapi.start(characterAssistant);
const response = vapi.start({
endCallFunctionEnabled: true,
model: {
provider: 'openai',
model: 'gpt-3.5-turbo',
// "fallbackModels": ["gpt-4-1106-preview", "gpt-4-0125-preview"],
messages: [
{
content: 'you are an assistant',
role: 'assistant',
},
],
},
});
response
.then((_res) => {
// console.log('call', res);
})
.catch((e) => {
console.error('got an error while starting the call', e);
});
};
const stop = () => {
setCallStatus(CALL_STATUS.LOADING);
vapi.stop();
};
const toggleCall = async () => {
if (callStatus === CALL_STATUS.ACTIVE) {
console.log('stopping the call');
stop();
} else {
await start();
}
};
const setMuted = (value: boolean) => {
console.log('mute');
vapi.setMuted(value);
};
const isMuted = vapi.isMuted;
const send = (msg: any) => {
return vapi.send(msg);
};
return {
callStatus,
activeTranscript,
messages,
start,
stop,
setMuted,
isMuted,
toggleCall,
send,
};
}