flowchart LR D([document]) -- text --> S[TextSplitter] -- out --> C([chunks])
What it shows
TextSplitterpacks paragraphs into chunks of at mostchunkSizecharacters.- Paragraphs that fit in
overlaprepeat at the start of the next chunk.
Split a document into overlapping chunks with a built-in component.
flowchart LR D([document]) -- text --> S[TextSplitter] -- out --> C([chunks])
TextSplitter packs paragraphs into chunks of at most chunkSize characters.overlap repeat at the start of the next chunk.import { TextSplitter } from '@astack-tech/components';
import { Pipeline } from '@astack-tech/core';
const article = `
Flow-based programming describes an application as a graph of components.
Each component reads from input ports and writes to output ports.
Components do not call each other. The graph decides where data goes.
This makes each component small, testable and reusable.
`.trim();
// Pack paragraphs into chunks of at most 160 characters.
// Paragraphs that fit in `overlap` are repeated at the start of the next chunk.
const splitter = new TextSplitter({ chunkSize: 160, overlap: 80 });
const pipeline = new Pipeline();
pipeline.addComponent('splitter', splitter);
// TextSplitter reads from its `text` port and writes the chunks to `out`.
const chunks = await pipeline.run<string[]>('splitter.text', article, 'splitter.out');
chunks.forEach((chunk, index) => {
console.log(`--- chunk ${String(index + 1)} (${String(chunk.length)} chars)\n${chunk}`);
});
pnpm install && pnpm build
pnpm --filter @astack-examples/text-splitter start