splitTextBlocks
KIT · function · 运行时值
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 { splitTextBlocks } from '@jangts/agent-core/kits/text-blocks';import { splitTextBlocks } from '@jangts/agent-core/kits/text-blocks';其他公开入口:@jangts/agent-core/types、@jangts/agent-core/kits、@jangts/agent-core/kits/text-blocks。
定义
调用
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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
text | string | 必需 | — |
options | TextBlockOptions | 必需 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
targetChars | number | 必需 | 只读。Target block size in characters; only a single over-long line may exceed it. |
返回: TextBlock[]。
使用示例
- TextBlocks:长文本结构分片:把长文本切成有界结构块,为分页读取或索引保留原文与偏移。
先切一份带章节的说明
入门 · Node.js
示例只演示分片合同;分页窗口、上下文预算和提示词仍由 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);按块分页并记录覆盖范围
配置 · Node.js
块边界不等于固定字符窗口;报告已读范围时应记录块的 start/end,而不是推算字符位置。
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);