Stworzę nowoczesny czat z funkcją streamingu w jednym pliku HTML z kolorowymi akcentami i jasnym stylem. Użyję Tailwind CSS dla zwięzłości kodu.
<!DOCTYPE html>
<html lang="pl" class="h-full">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>StreamChat Pro</title>
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
theme: {
extend: {
animation: {
'float': 'float 6s ease-in-out infinite',
'pulse-slow': 'pulse 3s ease-in-out infinite',
'bounce-gentle': 'bounce-gentle 2s ease-in-out infinite',
'gradient': 'gradient 8s ease infinite',
'typing': 'typing 1.5s ease-in-out infinite',
'slide-up': 'slide-up 0.3s ease-out',
'fade-in': 'fade-in 0.5s ease-out'
},
keyframes: {
float: {
'0%, 100%': { transform: 'translateY(0px)' },
'50%': { transform: 'translateY(-10px)' }
},
'bounce-gentle': {
'0%, 100%': { transform: 'translateY(0)' },
'50%': { transform: 'translateY(-5px)' }
},
gradient: {
'0%, 100%': { backgroundPosition: '0% 50%' },
'50%': { backgroundPosition: '100% 50%' }
},
typing: {
'0%, 100%': { opacity: '1' },
'50%': { opacity: '0.3' }
},
'slide-up': {
'0%': { transform: 'translateY(20px)', opacity: '0' },
'100%': { transform: 'translateY(0)', opacity: '1' }
},
'fade-in': {
'0%': { opacity: '0' },
'100%': { opacity: '1' }
}
}
}
}
}
</script>
<style>
.glass-effect {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.2);
}
.gradient-text {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4, #feca57);
background-size: 300% 300%;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
animation: gradient 8s ease infinite;
}
.message-bubble {
transform: translateY(20px);
opacity: 0;
animation: slide-up 0.3s ease-out forwards;
}
.typing-indicator {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
background: #4ecdc4;
animation: typing 1.5s ease-in-out infinite;
}
.typing-indicator:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator:nth-child(3) { animation-delay: 0.4s; }
.hover-lift {
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.hover-lift:hover {
transform: translateY(-2px);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}
.scroll-smooth {
scroll-behavior: smooth;
}
</style>
</head>
<body class="h-full bg-gradient-to-br from-blue-50 via-white to-purple-50">
<div class="h-full flex flex-col max-w-4xl mx-auto">
<!-- Header -->
<header class="glass-effect rounded-b-3xl p-6 mb-4 hover-lift">
<div class="flex items-center justify-between">
<div class="flex items-center space-x-4">
<div class="w-12 h-12 bg-gradient-to-r from-pink-500 to-violet-500 rounded-2xl flex items-center justify-center animate-float">
<svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"></path>
</svg>
</div>
<div>
<h1 class="text-2xl font-bold gradient-text">StreamChat Pro</h1>
<p class="text-gray-600 text-sm">Nowoczesny czat ze streamingiem</p>
</div>
</div>
<div class="flex items-center space-x-2">
<div class="w-3 h-3 bg-green-400 rounded-full animate-pulse-slow"></div>
<span class="text-sm text-gray-600">Online</span>
</div>
</div>
</header>
<!-- Chat Container -->
<div class="flex-1 flex flex-col min-h-0">
<!-- Messages Area -->
<div id="messagesContainer" class="flex-1 overflow-y-auto p-4 space-y-4 scroll-smooth">
<div class="text-center py-8">
<div class="inline-block p-4 glass-effect rounded-2xl">
<p class="text-gray-600">Rozpocznij konwersację! 🚀</p>
</div>
</div>
</div>
<!-- Typing Indicator -->
<div id="typingIndicator" class="px-4 py-2 hidden">
<div class="flex items-center space-x-2 text-gray-500 text-sm">
<span>AI pisze</span>
<div class="flex space-x-1">
<div class="typing-indicator"></div>
<div class="typing-indicator"></div>
<div class="typing-indicator"></div>
</div>
</div>
</div>
<!-- Input Area -->
<div class="p-4">
<div class="glass-effect rounded-3xl p-4 hover-lift">
<div class="flex items-end space-x-4">
<div class="flex-1">
<textarea
id="messageInput"
placeholder="Napisz wiadomość..."
class="w-full bg-transparent border-none outline-none resize-none text-gray-800 placeholder-gray-500"
rows="1"
maxlength="500"
></textarea>
</div>
<button
id="sendButton"
class="bg-gradient-to-r from-blue-500 to-purple-600 text-white p-3 rounded-2xl hover:from-blue-600 hover:to-purple-700 transition-all duration-300 transform hover:scale-105 disabled:opacity-50 disabled:cursor-not-allowed"
disabled
>
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8"></path>
</svg>
</button>
</div>
<div class="flex justify-between items-center mt-2 text-xs text-gray-500">
<span>Naciśnij Enter aby wysłać</span>
<span id="charCounter">0/500</span>
</div>
</div>
</div>
</div>
</div>
<script>
class StreamChat {
constructor() {
this.messagesContainer = document.getElementById('messagesContainer');
this.messageInput = document.getElementById('messageInput');
this.sendButton = document.getElementById('sendButton');
this.typingIndicator = document.getElementById('typingIndicator');
this.charCounter = document.getElementById('charCounter');
this.isStreaming = false;
this.initializeEventListeners();
this.autoResizeTextarea();
}
initializeEventListeners() {
this.messageInput.addEventListener('input', () => {
this.updateCharCounter();
this.toggleSendButton();
this.autoResizeTextarea();
});
this.messageInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
this.sendMessage();
}
});
this.sendButton.addEventListener('click', () => this.sendMessage());
}
updateCharCounter() {
const length = this.messageInput.value.length;
this.charCounter.textContent = `${length}/500`;
this.charCounter.className = length > 450 ? 'text-red-500' : 'text-gray-500';
}
toggleSendButton() {
const hasText = this.messageInput.value.trim().length > 0;
this.sendButton.disabled = !hasText || this.isStreaming;
}
autoResizeTextarea() {
this.messageInput.style.height = 'auto';
this.messageInput.style.height = Math.min(this.messageInput.scrollHeight, 120) + 'px';
}
async sendMessage() {
const message = this.messageInput.value.trim();
if (!message || this.isStreaming) return;
this.addMessage(message, 'user');
this.messageInput.value = '';
this.updateCharCounter();
this.toggleSendButton();
this.autoResizeTextarea();
await this.simulateAIResponse(message);
}
addMessage(content, sender) {
const messageDiv = document.createElement('div');
messageDiv.className = `message-bubble flex ${sender === 'user' ? 'justify-end' : 'justify-start'}`;
const bubble = document.createElement('div');
bubble.className = `max-w-xs lg:max-w-md px-4 py-3 rounded-2xl ${
sender === 'user'
? 'bg-gradient-to-r from-blue-500 to-purple-600 text-white ml-auto'
: 'glass-effect text-gray-800'
} hover-lift`;
if (sender === 'user') {
bubble.innerHTML = `
<div class="flex items-start space-x-2">
<div class="flex-1">
<p class="text-sm">${this.escapeHtml(content)}</p>
<p class="text-xs opacity-75 mt-1">${this.getCurrentTime()}</p>
</div>
<div class="w-8 h-8 bg-white bg-opacity-20 rounded-full flex items-center justify-center">
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
<path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
</svg>
</div>
</div>
`;
} else {
bubble.innerHTML = `
<div class="flex items-start space-x-2">
<div class="w-8 h-8 bg-gradient-to-r from-pink-500 to-violet-500 rounded-full flex items-center justify-center animate-bounce-gentle">
<svg class="w-4 h-4 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
</svg>
</div>
<div class="flex-1">
<p class="text-sm" id="ai-message-${Date.now()}"></p>
<p class="text-xs text-gray-500 mt-1">${this.getCurrentTime()}</p>
</div>
</div>
`;
}
messageDiv.appendChild(bubble);
this.messagesContainer.appendChild(messageDiv);
this.scrollToBottom();
return bubble.querySelector(`#ai-message-${Date.now() - 1}`) || null;
}
async simulateAIResponse(userMessage) {
this.isStreaming = true;
this.toggleSendButton();
this.showTypingIndicator();
// Symulacja opóźnienia myślenia AI
await this.delay(1000 + Math.random() * 2000);
this.hideTypingIndicator();
const aiMessageElement = this.addMessage('', 'ai');
const messageContainer = aiMessageElement.querySelector('p');
// Generowanie odpowiedzi AI
const responses = [
"To bardzo interesujące pytanie! Pozwól, że się nad tym zastanowię...",
"Świetny punkt! Z mojego doświadczenia mogę powiedzieć, że...",
"Fascynujące! To przypomina mi podobną sytuację, gdzie...",
"Doskonałe pytanie! Myślę, że kluczem do zrozumienia tego jest...",
"Wow, to naprawdę skłania do myślenia! Według mnie...",
"Bardzo dobra obserwacja! Pozwól, że podzielę się moimi przemyśleniami..."
];
const response = responses[Math.floor(Math.random() * responses.length)] +
`