Spaces:
Runtime error
Runtime error
File size: 2,607 Bytes
a40a50f |
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 |
'use client';
import { useState, useRef } from 'react';
import { streamChat, ChatRequest } from '../utils/api';
interface Message {
role: 'user' | 'assistant';
content: string;
}
export default function ChatApp() {
const [messages, setMessages] = useState<Message[]>([]);
const [input, setInput] = useState('');
const [loading, setLoading] = useState(false);
const endRef = useRef<HTMLDivElement>(null);
const scrollToEnd = () => {
endRef.current?.scrollIntoView({ behavior: 'smooth' });
};
const sendMessage = async () => {
if (!input.trim() || loading) return;
const prompt = input;
setInput('');
setMessages((prev) => [...prev, { role: 'user', content: prompt }, { role: 'assistant', content: '' }]);
setLoading(true);
try {
const req: ChatRequest = { user: 'demo', session: 'default', prompt };
for await (const chunk of streamChat(req)) {
setMessages((prev) => {
const msgs = [...prev];
msgs[msgs.length - 1] = {
role: 'assistant',
content: msgs[msgs.length - 1].content + chunk,
};
return msgs;
});
scrollToEnd();
}
} catch (err) {
console.error(err);
setMessages((prev) => {
const msgs = [...prev];
msgs[msgs.length - 1] = {
role: 'assistant',
content: 'Error retrieving response',
};
return msgs;
});
} finally {
setLoading(false);
scrollToEnd();
}
};
const onSubmit = (e: React.FormEvent) => {
e.preventDefault();
void sendMessage();
};
return (
<div className="flex flex-col max-w-2xl mx-auto h-screen p-4">
<div className="flex-1 overflow-y-auto space-y-4">
{messages.map((msg, idx) => (
<div key={idx} className={msg.role === 'user' ? 'text-right' : 'text-left'}>
<span className="px-3 py-2 inline-block rounded bg-gray-200 dark:bg-gray-700">
{msg.content}
</span>
</div>
))}
<div ref={endRef} />
</div>
<form onSubmit={onSubmit} className="mt-4 flex gap-2">
<input
type="text"
className="flex-1 border rounded px-3 py-2 text-black"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Type your message..."
/>
<button
type="submit"
className="bg-blue-600 text-white px-4 py-2 rounded disabled:opacity-50"
disabled={loading}
>
Send
</button>
</form>
</div>
);
}
|