<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fiore - Assistente AI</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; background: #f4f4f9; margin: 0; padding: 20px; display: flex; justify-content: center; }
.chat-container { width: 100%; max-width: 600px; background: white; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); display: flex; flex-direction: column; height: 85vh; overflow: hidden; }
.chat-header { background: #007bff; color: white; padding: 15px; text-align: center; font-weight: bold; font-size: 18px; }
.chat-messages { flex: 1; padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 15px; }
.message { padding: 12px 16px; border-radius: 18px; max-width: 85%; line-height: 1.5; font-size: 15px; }
.message.user { background: #e3f2fd; align-self: flex-end; border-bottom-right-radius: 4px; color: #0d47a1; }
.message.ai { background: #f1f1f1; align-self: flex-start; border-bottom-left-radius: 4px; color: #333; }
.chat-input-area { display: flex; padding: 15px; border-top: 1px solid #ddd; background: white; }
input[type="text"] { flex: 1; padding: 12px; border: 1px solid #ccc; border-radius: 25px; outline: none; font-size: 16px; }
button { background: #007bff; color: white; border: none; padding: 12px 20px; margin-left: 10px; border-radius: 25px; cursor: pointer; font-size: 16px; font-weight: bold; transition: background 0.2s; }
button:disabled { background: #ccc; cursor: not-allowed; }
/* Player Audio Personalizzato */
.audio-player { margin-top: 10px; display: flex; align-items: center; gap: 10px; background: #fff3cd; padding: 8px 12px; border-radius: 10px; width: fit-content; }
.audio-player button { background: #ffc107; color: #000; margin: 0; padding: 6px 12px; font-size: 14px; border-radius: 15px; }
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">Fiore - Esperto di Valutazione d'Impresa</div>
<div class="chat-messages" id="chat-messages">
<!-- I messaggi appariranno qui -->
</div>
<div class="chat-input-area">
<input type="text" id="user-input" placeholder="Scrivi un messaggio a Fiore..." autocomplete="off">
<button id="send-btn">Invia</button>
</div>
</div>
<script>
// ⚠️ SOSTITUISCI QUESTO URL CON QUELLO DEL TUO WORKER
const WORKER_URL = 'https://chat-tts-proxy.trai-911.workers.dev';
const chatMessages = document.getElementById('chat-messages');
const userInput = document.getElementById('user-input');
const sendBtn = document.getElementById('send-btn');
let isWaiting = false;
function appendMessage(text, sender) {
const msgDiv = document.createElement('div');
msgDiv.className = `message ${sender}`;
msgDiv.textContent = text;
chatMessages.appendChild(msgDiv);
chatMessages.scrollTop = chatMessages.scrollHeight;
return msgDiv;
}
function base64ToBlob(base64, mime) {
const byteCharacters = atob(base64);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
return new Blob([byteArray], {type: mime});
}
function playAudio(base64Audio) {
const blob = base64ToBlob(base64Audio, 'audio/mpeg');
const url = URL.createObjectURL(blob);
const playerContainer = document.createElement('div');
playerContainer.className = 'audio-player';
const playBtn = document.createElement('button');
playBtn.textContent = '▶ Ascolta';
playBtn.onclick = () => {
const audio = new Audio(url);
audio.play();
audio.onended = () => URL.revokeObjectURL(url);
};
playerContainer.appendChild(playBtn);
const lastAiMessage = chatMessages.querySelector('.message.ai:last-child');
if (lastAiMessage) {
lastAiMessage.appendChild(playerContainer);
}
}
async function sendMessage() {
const text = userInput.value.trim();
if (!text || isWaiting) return;
isWaiting = true;
userInput.value = '';
sendBtn.disabled = true;
sendBtn.textContent = '...';
appendMessage(text, 'user');
try {
const response = await fetch(WORKER_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: text })
});
if (!response.ok) {
const errData = await response.json();
throw new Error(errData.error || 'Errore dal server');
}
const data = await response.json();
appendMessage(data.text, 'ai');
if (data.audio) {
playAudio(data.audio);
}
} catch (error) {
appendMessage('Si è verificato un errore di connessione. Riprova.', 'ai');
console.error(error);
} finally {
isWaiting = false;
sendBtn.disabled = false;
sendBtn.textContent = 'Invia';
userInput.focus();
}
}
sendBtn.addEventListener('click', sendMessage);
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') sendMessage();
});
// All'avvio, chiediamo a Fiore di presentarsi
window.onload = () => {
userInput.value = "Ciao, presentati.";
sendMessage();
};
</script>
</body>
</html>