UNPKG

graphlit-client

Version:
41 lines (40 loc) 1.42 kB
/** * Breaks an LLM’s streaming token deltas into character, word, or sentence * chunks – or lets you plug in your own chunker. * * Usage * ----- * const buf = new ChunkBuffer('sentence'); * stream.on('delta', d => buf.addToken(d).forEach(pushToUI)); * stream.on('end', () => buf.flush().forEach(pushToUI)); */ export type ChunkingStrategy = "character" | "word" | "sentence" | ((text: string) => { chunks: string[]; remainder: string; }); export interface ChunkerOpts { /** Flush “words” longer than this (default = 50 chars). */ maxWordLen?: number; /** Force a break after this many chars with no whitespace (default = 400). */ maxBufferNoBreak?: number; } export declare class ChunkBuffer { constructor(strategy: ChunkingStrategy, opts?: ChunkerOpts); /** Feed one LLM delta; receive zero‑or‑more flushed chunks. */ addToken(token: string): string[]; /** Call when the stream closes to emit the final remainder. */ flush(): string[]; private buffer; private readonly strategy; private readonly customChunker?; private readonly MAX_WORD_LEN; private readonly MAX_BUFFER_NO_BREAK; private readonly graphemeSeg?; private readonly wordSeg?; private readonly sentenceSeg?; private flushGraphemes; private flushWords; private flushSentences; private flushLongRuns; private flushCustom; }