52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
'use client';
|
|
|
|
import { useChat } from '@ai-sdk/react';
|
|
import ChatInput from '@/component/chat-input';
|
|
|
|
export default function Chat() {
|
|
const { error, status, sendMessage, messages, regenerate, stop } = useChat();
|
|
|
|
return (
|
|
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">
|
|
{messages.map(m => (
|
|
<div key={m.id} className="whitespace-pre-wrap">
|
|
{m.role === 'user' ? 'User: ' : 'AI: '}
|
|
{m.parts.map(part => {
|
|
if (part.type === 'text') {
|
|
return part.text;
|
|
}
|
|
})}
|
|
</div>
|
|
))}
|
|
|
|
{(status === 'submitted' || status === 'streaming') && (
|
|
<div className="mt-4 text-gray-500">
|
|
{status === 'submitted' && <div>Loading...</div>}
|
|
<button
|
|
type="button"
|
|
className="px-4 py-2 mt-4 text-blue-500 border border-blue-500 rounded-md"
|
|
onClick={stop}
|
|
>
|
|
Stop
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{error && (
|
|
<div className="mt-4">
|
|
<div className="text-red-500">An error occurred.</div>
|
|
<button
|
|
type="button"
|
|
className="px-4 py-2 mt-4 text-blue-500 border border-blue-500 rounded-md"
|
|
onClick={() => regenerate()}
|
|
>
|
|
Retry
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
<ChatInput status={status} onSubmit={text => sendMessage({ text })} />
|
|
</div>
|
|
);
|
|
}
|