/** How much of a text the user has heard so far. */ export type HeardTextProgress = { /** * Number of characters of the text the user has heard, counted in Unicode code points * (as `Array.from(text).length` counts them, not UTF-16 code units like `text.length`). * May be fractional; it is floored before rendering. A value at or above the text's * code point length reveals the whole text. */ chars: number; /** `true` once the speech for the text has finished (played out, interrupted or skipped). */ done: boolean; }; /** * Returns how much of the text the user has heard by `now` (a `performance.now()` * timestamp). * * Return `undefined` only for a text that is not spoken at all: the full text is then * shown, and the reveal never moves back past what has been shown. A text that will be * spoken but whose audio has not started, or that the voice source does not know yet, * must return `{ chars: 0, done: false }`, or it is shown in full before it is heard. * * Also called during render, so keep it free of side effects. */ export type HeardTextFunction = (now: number) => HeardTextProgress | undefined; /** * Registers a listener that is called whenever the speech progress source changes * (playback started, paused, finished...). Returns an unsubscribe function. */ export type HeardTextSubscribe = (listener: () => void) => () => void; export type UseHeardTextParams = { /** The speech progress of `text`, see `HeardTextFunction`. */ heard: HeardTextFunction; /** The full text to reveal. */ text: string; /** * `true` while the text is still being written. The full text is only revealed * automatically once `heard` reports `done` and `generating` is falsy. */ generating?: boolean; /** * Defaults to `true`. `false` reveals the full text at once, for example for a message * loaded from history. Read when the hook mounts and when `text` is replaced by a * different text, not on every change. */ live?: boolean; /** * Optional change notifications from the speech progress source. Without it, `heard` * is only checked again while the text is paced and its speech is not done, and when * `text` or `generating` change. Subscribed once; to switch to another source, remount * the component (for example with a `key`). */ subscribe?: HeardTextSubscribe; }; export type UseHeardTextResult = { /** The part of the text the user has heard (the full text when not paced). */ heardText: string; /** `false` when `heard` returned `undefined`; `heardText` is then the full text. */ isPaced: boolean; /** Reveals the full text from now on (e.g. when the user stops the AI response). */ skip: () => void; }; /** * Reveals a text at the pace the user hears it spoken, for example the reply of a voice * AI agent that is written into the chat faster than the agent speaks it. The speech * progress comes from the app as a plain function, so the hook works with any voice * backend and has no dependency on the chat SDK. * * - Reveals the first `floor(chars)` Unicode code points of `text` (emoji and other * astral characters are never split) and never moves backwards while `text` grows or * is truncated. When `text` is replaced by a different text (the next reply rendered * by the same component), the reveal starts over: the length shown, `skip()` and * `live` are reset. * - While the text is paced and its speech is not done, samples `heard` on every * animation frame (a 16 ms timeout without `requestAnimationFrame`). It stops ticking * when the speech is done or the component unmounts; `subscribe` notifications, and * changes of `text` or `generating`, make it check again. * - Reveals the full text once `heard` reports `done` and `generating` is falsy, when * `live` is `false`, and after `skip()`. * - When `heard` returns `undefined`, `isPaced` is `false` and `heardText` is the full * text. If it becomes paced later, the reveal continues from the length already shown. * * `heard` and `subscribe` are read through refs, so passing new function identities on * every render (inline arrow functions) does not restart the pacing. `subscribe` is * called once on mount (and again only if it goes from absent to present) and its * unsubscribe function runs on unmount; to switch to another speech source, remount the * component. `heard` is also called during render, so keep it free of side effects. * * @example * ```tsx * import { useHeardText } from 'stream-chat-react/ai-components'; * * const SpokenReply = ({ generating, session, text, turnId }) => { * const { heardText } = useHeardText({ * generating, * heard: (now) => session.speech.heard(turnId, now), * subscribe: (listener) => session.speech.subscribe(listener), * text, * }); * return
{heardText}
; * }; * ``` */ export declare const useHeardText: ({ generating, heard, live, subscribe, text, }: UseHeardTextParams) => UseHeardTextResult; //# sourceMappingURL=useHeardText.d.ts.map