splitTextBlocks
KIT · function · Runtime value
Split long plain text on structural boundaries.
Headings and horizontal rules start a new block, blank lines end the current one, and all other lines break only when the target size would be exceeded. A single line longer than the target becomes its own block so that paging can cut it into fragments later without splitting the line into unrelated sections. Concatenating every block reproduces the input exactly: no truncation, padding or dropped whitespace.
The input must be plain text; binaries and rich documents are read elsewhere.
Import
import { splitTextBlocks } from '@jangts/agent-core/kits/text-blocks';import { splitTextBlocks } from '@jangts/agent-core/kits/text-blocks';Other public entry points:@jangts/agent-core/types, @jangts/agent-core/kits, @jangts/agent-core/kits/text-blocks.
Definition
Call
splitTextBlocks(text: string, options: TextBlockOptions): TextBlock[]Split long plain text on structural boundaries.
Headings and horizontal rules start a new block, blank lines end the current one, and all other lines break only when the target size would be exceeded. A single line longer than the target becomes its own block so that paging can cut it into fragments later without splitting the line into unrelated sections. Concatenating every block reproduces the input exactly: no truncation, padding or dropped whitespace.
The input must be plain text; binaries and rich documents are read elsewhere.
| Parameter | Type | Passing | Description |
|---|---|---|---|
text | string | Required | — |
options | TextBlockOptions | Required | — |
Fields of options
| Field | Type | Required | Description |
|---|---|---|---|
targetChars | number | Required | Read-only. Target block size in characters; only a single over-long line may exceed it. |
Returns: TextBlock[].
Examples
- TextBlocks: structure-aware splitting of long text: Split long text into bounded structural blocks while keeping the original text and offsets for paging or indexing.
First split a short document with two sections
Beginner · Node.js
The example only demonstrates the splitting contract; paging windows, context budgets and prompts still belong to the App.
import { TextBlocks } from '@jangts/agent-core/kits/text-blocks';
const text = '# 概览\n\n' + '说明内容\n'.repeat(4) + '\n## 细节\n\n' + '更多内容\n'.repeat(4);
const blocks = TextBlocks.split(text, { targetChars: 40 });
console.log(blocks.map(block => [block.number, block.start, block.end]));
console.log(blocks.map(block => block.text).join('') === text);import { TextBlocks } from '@jangts/agent-core/kits/text-blocks';
const text = '# 概览\n\n' + '说明内容\n'.repeat(4) + '\n## 细节\n\n' + '更多内容\n'.repeat(4);
const blocks = TextBlocks.split(text, { targetChars: 40 });
console.log(blocks.map(block => [block.number, block.start, block.end]));
console.log(blocks.map(block => block.text).join('') === text);Page through blocks and record coverage
Configuration · Node.js
Block boundaries are not a fixed character window; record each block start/end instead of inferring character positions.
import { TextBlocks, type TextBlock } from '@jangts/agent-core/kits/text-blocks';
const text = '# 报告\n\n' + '记录内容\n'.repeat(200);
const blocks = TextBlocks.split(text, { targetChars: 4000 });
const page = (offset: number, limit: number): { blocks: TextBlock[]; nextOffset?: number } => ({
blocks: blocks.slice(offset, offset + limit),
nextOffset: offset + limit < blocks.length ? offset + limit : undefined,
});
const first = page(0, 2);
console.log(first.blocks.map(block => block.number), first.nextOffset);import { TextBlocks } from '@jangts/agent-core/kits/text-blocks';
const text = '# 报告\n\n' + '记录内容\n'.repeat(200);
const blocks = TextBlocks.split(text, { targetChars: 4000 });
const page = (offset, limit) => ({
blocks: blocks.slice(offset, offset + limit),
nextOffset: offset + limit < blocks.length ? offset + limit : undefined,
});
const first = page(0, 2);
console.log(first.blocks.map(block => block.number), first.nextOffset);