function useStream(model: string) {
const [text, setText] = useState("")
const [streaming, setStreaming] = useState(false)
const controller = useRef<AbortController>()
async function send(message: string) {
setStreaming(true)
setText("")
controller.current = new AbortController()
try {
for await (const chunk of client.stream({
model,
messages: [{ role: "user", content: message }],
signal: controller.current.signal,
})) {
setText(prev => prev + chunk.delta)
}
} finally {
setStreaming(false)
}
}
function stop() { controller.current?.abort() }
return { text, streaming, send, stop }
}