stream-hooks
v2.0.0
Published
<div align="center"> <a aria-label="NPM version" href="https://twitter.com/dimitrikennedy"> <img alt="stream-hooks" src="https://img.shields.io/twitter/follow/dimitrikennedy?style=social&labelColor=000000"> </a> <a aria-label="GH Issues" href="h
Downloads
731
Maintainers
Readme
stream-hooks
Installation
with pnpm
$ pnpm add stream-hooks zod zod-stream
with npm
$ npm install stream-hooks zod zod-stream
with bun
$ bun add stream-hooks zod zod-stream
useJsonStream
import { useJsonStream } from "stream-hooks"
export function Test() {
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}>
start
</button>
<button onClick={stopStream}>
stop
</button>
</div>
)
}