import { useJsonStream } from "stream-hooks"
import { z } from "zod"
export function ChatComponent() {
const { loading, startStream, stopStream, data } = useJsonStream({
schema: z.object({
content: z.string()
}),
onReceive: data => {
console.log("incremental update to final response model", data)
}
})
const submit = async () => {
try {
await startStream({
url: "/api/ai/chat",
method: "POST",
body: {
messages: [
{
content: "yo",
role: "user"
}
]
}
})
} catch (e) {
console.error(e)
}
}
return (
<div>
{data?.content}
<button onClick={submit} disabled={loading}>
Start
</button>
<button onClick={stopStream}>
Stop
</button>
</div>
)
}