useStream
The fundamental streaming primitive. Consumes any async stream and returns reactive state.
Usage
import { useStream } from '@agentskit/react'
function StreamViewer({ source }) {
const { text, status, error, stop } = useStream(source)
return (
<div>
<p>{text}</p>
{status === 'streaming' && <button onClick={stop}>Stop</button>}
{status === 'error' && <p>Error: {error.message}</p>}
</div>
)
}
API
const { data, text, status, error, stop } = useStream(source, options?)
Parameters
| Param | Type | Description |
|---|---|---|
source | StreamSource | A stream source from an adapter or custom source |
options.onChunk | (chunk: StreamChunk) => void | Called for each chunk received |
options.onComplete | (text: string) => void | Called when stream finishes |
options.onError | (error: Error) => void | Called on stream error |
Returns
| Field | Type | Description |
|---|---|---|
data | StreamChunk | null | Latest chunk received |
text | string | Accumulated full text |
status | StreamStatus | 'idle' | 'streaming' | 'complete' | 'error' |
error | Error | null | Error if status is 'error' |
stop | () => void | Abort the stream |