const ADDIS_AI_API_KEY = "your_api_key_here";
const API_ENDPOINT = "https://api.addis-ai.com";
const startButton = document.getElementById("start-recording");
const stopButton = document.getElementById("stop-recording");
const conversationHistory = document.getElementById("conversation-history");
let mediaRecorder;
let audioChunks = [];
let stream;
startButton.addEventListener("click", startRecording);
stopButton.addEventListener("click", stopRecording);
async function startRecording() {
try {
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = (event) => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = processAudio;
audioChunks = [];
mediaRecorder.start();
startButton.disabled = true;
stopButton.disabled = false;
addMessageToHistory("Listening...", "system");
} catch (error) {
console.error("Error accessing microphone:", error);
addMessageToHistory(
"Error accessing microphone. Please check permissions.",
"error",
);
}
}
function stopRecording() {
if (mediaRecorder && mediaRecorder.state !== "inactive") {
mediaRecorder.stop();
stream.getTracks().forEach((track) => track.stop());
startButton.disabled = false;
stopButton.disabled = true;
addMessageToHistory("Processing audio...", "system");
}
}
async function processAudio() {
const audioBlob = new Blob(audioChunks, { type: "audio/wav" });
try {
const formData = new FormData();
formData.append("audio", audioBlob);
formData.append("language", "am");
const response = await fetch(`${API_ENDPOINT}/v1/audio/transcribe`, {
method: "POST",
headers: {
"X-API-Key": ADDIS_AI_API_KEY,
},
body: formData,
});
const data = await response.json();
if (data.text) {
addMessageToHistory(data.text, "user");
await processTextWithChat(data.text);
} else {
addMessageToHistory(
"Could not transcribe audio. Please try again.",
"error",
);
}
} catch (error) {
console.error("Error processing audio:", error);
addMessageToHistory("Error processing audio. Please try again.", "error");
}
}
async function processTextWithChat(text) {
try {
const response = await fetch(`${API_ENDPOINT}/v1/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": ADDIS_AI_API_KEY,
},
body: JSON.stringify({
model: "addis-1-alef",
messages: [{ role: "user", content: text }],
language: "am",
}),
});
const data = await response.json();
if (data.choices && data.choices[0] && data.choices[0].message) {
const assistantResponse = data.choices[0].message.content;
addMessageToHistory(assistantResponse, "assistant");
await convertTextToSpeech(assistantResponse);
} else {
addMessageToHistory(
"Error processing your request. Please try again.",
"error",
);
}
} catch (error) {
console.error("Error calling chat API:", error);
addMessageToHistory(
"Error processing your request. Please try again.",
"error",
);
}
}
async function convertTextToSpeech(text) {
try {
const response = await fetch(`${API_ENDPOINT}/v1/audio/speech`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": ADDIS_AI_API_KEY,
},
body: JSON.stringify({
text: text,
language: "am",
voice_id: "female-1",
}),
});
if (response.ok) {
const audioBlob = await response.blob();
playAudio(audioBlob);
} else {
console.error("Error generating speech");
}
} catch (error) {
console.error("Error calling TTS API:", error);
}
}
function playAudio(audioBlob) {
const audioUrl = URL.createObjectURL(audioBlob);
const audio = new Audio(audioUrl);
audio.play();
}
function addMessageToHistory(message, role) {
const messageDiv = document.createElement("div");
messageDiv.className = `message ${role}`;
messageDiv.textContent = message;
conversationHistory.appendChild(messageDiv);
conversationHistory.scrollTop = conversationHistory.scrollHeight;
}