Saltar al contenido principal

useStream

Primitiva fundamental de streaming. Consume cualquier stream asíncrono y devuelve estado reactivo.

Uso

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

Parámetros

ParámetroTipoDescripción
sourceStreamSourceOrigen de stream desde un adaptador o fuente personalizada
options.onChunk(chunk: StreamChunk) => voidSe llama por cada fragmento recibido
options.onComplete(text: string) => voidSe llama al terminar el stream
options.onError(error: Error) => voidSe llama ante error de stream

Valor devuelto

CampoTipoDescripción
dataStreamChunk | nullÚltimo fragmento recibido
textstringTexto acumulado completo
statusStreamStatus'idle' | 'streaming' | 'complete' | 'error'
errorError | nullError si el estado es 'error'
stop() => voidAbortar el stream