Skip to main content

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

ParamTypeDescription
sourceStreamSourceA stream source from an adapter or custom source
options.onChunk(chunk: StreamChunk) => voidCalled for each chunk received
options.onComplete(text: string) => voidCalled when stream finishes
options.onError(error: Error) => voidCalled on stream error

Returns

FieldTypeDescription
dataStreamChunk | nullLatest chunk received
textstringAccumulated full text
statusStreamStatus'idle' | 'streaming' | 'complete' | 'error'
errorError | nullError if status is 'error'
stop() => voidAbort the stream