stream-hooks
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

stream-hooks

Hooks for consuming streams in react - specifically json streams coming from LLMS - given a Zod Schema that represents the final output, you can start the stream and start to read the structured result immediately.

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>
    )
  }

Package Sidebar

Install

npm i stream-hooks

Weekly Downloads

83

Version

1.0.4

License

MIT

Unpacked Size

430 kB

Total Files

40

Last publish

Collaborators

  • dimitrikennedy