ai-sdk-next-openai/app/test-cohere/page.tsx
2025-09-26 15:46:29 +00:00

59 lines
1.6 KiB
TypeScript

'use client';
import { useChat } from '@ai-sdk/react';
import { DefaultChatTransport } from 'ai';
import ChatInput from '@/component/chat-input';
export default function TestCohere() {
const { error, status, sendMessage, messages, regenerate, stop } = useChat({
transport: new DefaultChatTransport({ api: '/api/chat-cohere' }),
});
return (
<div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">
<h1 className="mb-4 text-xl font-bold">
Cohere Block-Based Streaming Test
</h1>
{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>
);
}