'use client'; import ChatInput from '@/component/chat-input'; import { useChat } from '@ai-sdk/react'; import { DefaultChatTransport, UIMessage } from 'ai'; type MyMessage = UIMessage< never, { weather: { city: string; weather: string; status: 'loading' | 'success'; }; } >; export default function Chat() { const { error, status, sendMessage, messages, regenerate, stop } = useChat({ transport: new DefaultChatTransport({ api: '/api/use-chat-data-ui-parts', }), onData: dataPart => { console.log('dataPart', JSON.stringify(dataPart, null, 2)); }, }); return (
{messages.map(message => (
{message.role === 'user' ? 'User: ' : 'AI: '}{' '} {message.parts .filter(part => part.type === 'data-weather') .map((part, index) => ( {part.data.status === 'loading' ? ( <> Getting weather for {part.data.city}... ) : part.data.status === 'success' ? ( <> Weather in {part.data.city}:{' '} {part.data.weather} ) : ( <>Unknown weather state )} ))} {message.parts .filter(part => part.type !== 'data-weather') .map((part, index) => { if (part.type === 'text') { return
{part.text}
; } })}
))} {(status === 'submitted' || status === 'streaming') && (
{status === 'submitted' &&
Loading...
}
)} {error && (
An error occurred.
)} sendMessage({ text })} />
); }