-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
172 lines (152 loc) · 5.84 KB
/
main.js
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
const audioContextOut = new (window.AudioContext || window.webkitAudioContext)({ latencyHint: "interactive", sampleRate: 48000 });
const audioContext = new (window.AudioContext || window.webkitAudioContext)({ latencyHint: "interactive" });
let startTime = -1;
window.onload = function () {
prepareAgentConfig();
const voice = document.getElementById("voice");
const model = document.getElementById("model");
document.getElementById("startConversationBtn").addEventListener("click", () => {
startConversaton(model, voice);
});
};
function startConversaton(model, voice) {
const config = configureSettings(model, voice);
let ws = new WebSocket("wss://agent.deepgram.com/agent", ["token", "<your-api-key-here>"]);
ws.binaryType = 'arraybuffer';
ws.onopen = function () {
console.log("WebSocket connection established.");
// Send initial config on connection
ws.send(JSON.stringify(config));
// Send the microphone audio to the websocket
captureAudio((data)=>{
ws.send(data);
});
};
ws.onerror = function (error) {
console.error("WebSocket error:", error);
};
ws.onmessage = function (event) {
if (typeof event.data === "string") {
console.log("Text message received:", event.data);
// Handle text messages
handleMessageEvent(event.data);
} else if (event.data instanceof ArrayBuffer) {
// Update the animation
updateBlobSize(0.25);
// Play the audio
receiveAudio(event.data);
} else {
console.error("Unsupported message format.");
}
};
updateUI();
updateVoices((voice_selection) => {
ws.send(JSON.stringify({
"type": "UpdateSpeak",
"model": voice_selection
}));
});
updateInstructions((instructions) => {
ws.send(JSON.stringify({
"type": "UpdateInstructions",
"instructions": instructions
}))
});
}
function updateVoices(callback) {
document.querySelectorAll(".circle-button").forEach((button) => {
button.addEventListener("click", function () {
document
.querySelector(".circle-button.selected")
.classList.remove("selected");
this.classList.add("selected");
const voice_selection = this.id;
console.log("Voice selection changed to:", voice_selection);
callback(voice_selection);
});
});
}
function updateInstructions(callback) {
// Update the instructions when a button is clicked
document
.getElementById("updateInstructionsBtn")
.addEventListener("click", function () {
let instructions = document.getElementById("instructionsInput").value;
callback(instructions);
});
}
function updateQuestions(){
const itemsDiv = document.querySelector('#items');
let questions = [
"Can you schedule a meeting for September 30th 2024 at 10am?",
"What is the overall deductible for an individual and for a family?",
"Are there services covered before meeting the deductible? If yes, which services?",
"Are there other deductibles for specific services? If yes, what are they?",
"What is the out-of-pocket limit for this plan for both network and out-of-network providers?",
"What is not included in the out-of-pocket limit?",
"Will the user pay less if they use a network provider? How can they find a list of network providers?",
"Does the plan cover prescription drugs?",
"What is the coverage for preventive care services?",
"How does the plan handle out-of-network care, and what are the costs associated with that?",
"Can I keep my current doctor under the new plan?",
"What is the process for filing a claim, and how long does it typically take to get reimbursed?"
];
questions.forEach((item) => {
let itemLi = document.createElement('li');
itemLi.innerHTML = item;
itemLi.className = 'no-bullets items';
itemsDiv.appendChild(itemLi);
});
}
function updateUI() {
document.getElementById("startContainer").style.display = "none";
document.getElementById("blobCanvas").style.display = "flex";
document.getElementById("buttonContainer").style.display = "flex";
animateBlob();
}
function configureSettings(model, voice) {
const voiceSelection = voice.options[voice.selectedIndex].value;
const providerAndModel = model.options[model.selectedIndex].value.split("+");
// Configuration settings for the agent
let config_settings = getStsConfig(state.callID);
config_settings.agent.think.provider.type = providerAndModel[0];
config_settings.agent.think.model = providerAndModel[1];
console.log('config_settings', JSON.stringify(config_settings))
// Update the text area to match the initial instructions
document.getElementById("instructionsInput").value = config_settings.agent.think.instructions;
document.getElementById(voiceSelection).classList.add("selected");
return config_settings;
}
async function handleMessageEvent(data){
let msgObj = JSON.parse(data);
if (msgObj["type"] === "UserStartedSpeaking") {
clearScheduledAudio();
}
if (!state.callID || state.status === 'sleeping') return;
const events = await service.getEvents(state.callID);
if (events) {
// Consolidate order needed because sometimes server can send back duplicate items
state.events = events;
let eventItems = document.querySelector('#eventItems');
eventItems.innerHTML = '';
let total = 0;
state.events.forEach((item) => {
let itemLi = document.createElement('li');
itemLi.innerHTML = item;
itemLi.className = 'no-bullets';
eventItems.appendChild(itemLi);
});
}
}
async function prepareAgentConfig() {
state.initializedAgent = true;
try {
updateQuestions();
state.callID = await service.getCallID();
let button = document.querySelector('#startConversationBtn');
button.innerHTML = 'Start Conversation';
button.removeAttribute('disabled');
} catch (error) {
console.error("Config error:", error);
}
}