UNPKG

agents

Version:

A home for your AI agents

69 lines (68 loc) 2.72 kB
//#region src/voice/text-stream.d.ts /** * Utilities for normalising various text-producing sources into a uniform * `AsyncGenerator<string>`. This lets `onTurn()` return any of: * * - A plain `string` * - An `AsyncIterable<string>` (deprecated for AI SDK `textStream`) * - An `AsyncIterable` of AI SDK `stream` parts * - A `ReadableStream<Uint8Array>` (e.g. a raw `fetch` response body * containing newline-delimited JSON / SSE) * - A `ReadableStream<string>` * * The generator yields individual text chunks as they become available. */ /** Union of every source type that {@link iterateText} accepts. */ type TextSource = string | TextReadableStream | AsyncIterable<unknown>; type TextReadableStreamReader = { read(): Promise<ReadableStreamReadResult<unknown>>; }; interface TextReadableStream { getReader(): TextReadableStreamReader; } /** * Turn any {@link TextSource} into a lazy async generator of string chunks. * * - `string` → yields the string once (if non-empty). * - `ReadableStream<string>` → yields each chunk directly. * - `ReadableStream<Uint8Array>` → decodes and parses as newline-delimited * JSON (NDJSON) / SSE (`data: …` lines), extracting text from common AI * response formats. * - `AsyncIterable<string>` → re-yields each chunk. */ declare function iterateText(source: TextSource): AsyncGenerator<string>; //#endregion //#region src/voice/sentence-chunker.d.ts /** * Sentence chunker — accumulates streaming text and yields complete sentences. * * Isolated and testable: no dependencies on the voice pipeline, Agent, or AI APIs. * Feed it tokens via `add()`, get back sentences via the return value. * Call `flush()` at end-of-stream to get any remaining text. * * Current implementation: splits on sentence-ending punctuation (. ! ?) followed * by a space or end-of-input. This is intentionally simple — optimize later with * better heuristics (abbreviations, decimal numbers, quoted speech, etc.). */ declare class SentenceChunker { #private; /** * Add a chunk of text (e.g. a streamed LLM token). * Returns an array of complete sentences extracted from the buffer. * May return 0, 1, or multiple sentences depending on the input. */ add(text: string): string[]; /** * Flush any remaining text in the buffer as a final sentence. * Call this when the LLM stream ends. * Returns the remaining text (trimmed), or an empty array if nothing is left. */ flush(): string[]; /** * Reset the chunker, discarding any buffered text. */ reset(): void; } //#endregion export { TextSource as n, iterateText as r, SentenceChunker as t }; //# sourceMappingURL=sentence-chunker-BAidJ4DA.d.ts.map