LLM 源代码语法感知分块
tree-sitter-language-pack-chunking-for-llms
当用户希望将源代码拆分为适配 LLM 上下文窗口的分块、且不破坏语法结构时使用。涵盖 `ts-pack process --chunk-size`、语法感知分块优于固定字节分块的原因、分块大小的选择以及分块 JSON 结构。
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 485
- Forks
- 71
01 前言 Front Matter
- name
- tree-sitter-language-pack-chunking-for-llms
- description
- Use when the user wants to split source code into chunks for an LLM context window without breaking syntax mid-construct. Covers `ts-pack process --chunk-size`, why syntax-aware splits beat fixed-byte splits, picking a size, and the chunk JSON shape.
02 资源文件 1
- 参考文档index.md566 B
03 SKILL.md 正文
<!-- AI-RULEZ :: GENERATED FILE — DO NOT EDIT Content-Hash: blake3:f73428c23ccd047c1e8d74065c6be1dcc21b97f1f3e235ebc2a6779317208188 Source-Hash: blake3:fbed630db85ed02cff8e3bc8c836d453724480351ecb0896122f193a21670041 Schema-Version: v1 -->
Syntax-aware chunking for LLMs
Splitting code on a fixed byte or line count cuts functions in half and
strips context. ts-pack process <file> --chunk-size <bytes> splits on
syntactic boundaries (whole functions, classes, blocks) so each chunk is a
coherent unit, and emits them in the JSON chunks array.
Quick recipe
# ~2 KB chunks aligned to syntax boundaries
ts-pack process src/app.ts --chunk-size 2000
--chunk-size is a maximum size in bytes. The splitter packs whole
syntactic units up to that bound; an oversized single construct becomes its
own chunk rather than being cut. Chunks are added to the normal process
JSON output under chunks.
Picking a size
- Match the downstream model's token budget. A rough rule: bytes ÷ 4 ≈
tokens for code, so
--chunk-size 4000is on the order of ~1k tokens. - Larger chunks preserve more local context but fit fewer per request.
- Leave headroom for the prompt, the surrounding messages, and the response — do not size chunks to the full context window.
Combining with extraction
Chunking composes with the other process features, so you can attach
structure metadata to each request:
ts-pack process src/service.py --structure --chunk-size 3000 \
| jq '{chunks: (.chunks | length), functions: (.structure | length)}'
Chunk output
chunks is a list of code-chunk objects in the process JSON. Each chunk
carries its source text plus span information (line/byte offsets), so you
can cite or re-locate a chunk back in the original file. Iterate the array
to feed an LLM one coherent unit at a time:
ts-pack process big_module.py --chunk-size 2500 \
| jq -c '.chunks[]'
SDK equivalent
The SDK exposes chunking through the process config: set the
chunk_max_size field (in bytes) on ProcessConfig — the same value the
CLI's --chunk-size flag sets. ProcessConfig is a frozen dataclass, so
pass it to the constructor:
from tree_sitter_language_pack import process, ProcessConfig
config = ProcessConfig("python", chunk_max_size=2500)
result = process(source_code, config)
for chunk in result.chunks: # ProcessResult is an object, not a dict
send_to_llm(chunk.content)
When not to chunk
For a single small file that already fits the context window, skip chunking and pass the file whole. Reach for chunking when a file is large, when you are batching many files into a RAG index, or when you need stable, syntactically coherent units to cite.
04 同仓库更多 Skills
tree-sitter 代码智能解析
tree-sitter-language-pack
使用 tree-sitter 语法从 371 种编程语言中解析并提取代码智能信息。适用于编写解析源代码、提取结构/导入/导出/符号/文档字符串/注释、检测语言、运行语法诊断或为大模型(LLM)生成语法感知分块的代码——支持 Rust、Python、Node.js/TypeScript 或 ts-pack CLI。涵盖安装、CLI 接口、SDK 接口以及解析器缓存管理。
tree-sitter MCP 服务使用
tree-sitter-language-pack-using-the-mcp-server
当需要通过 tree-sitter-language-pack 的 MCP 服务器工具来解析源代码、提取代码结构或检测语言(而非调用 ts-pack CLI)时使用。涵盖工具接口、自动安装启动器,以及 MCP 相较于 CLI 或 SDK 的优势场景。
源代码语法树解析
tree-sitter-language-pack-parsing-source
当用户需要为源文件生成 tree-sitter 语法树时使用——以 s-表达式转储或 JSON 树的形式呈现。涵盖 `ts-pack parse`、语言自动检测与 `--language` 的对比、标准输入(stdin)输入以及读取 `has_errors`。
解析器缓存管理
tree-sitter-language-pack-managing-parsers
当用户需要管理 tree-sitter 解析器缓存时使用——为离线或 CI 运行预取解析器、列出已下载内容、检查某种语言、查找缓存目录或清理缓存。涵盖 `ts-pack download`、`list`、`info`、`cache-dir`、`clean` 和 `init`。
代码结构提取
tree-sitter-language-pack-extracting-code-structure
当用户需要从源文件获取结构化代码元数据时使用——包括函数、类、导入、导出、符号、文档字符串、注释或语法诊断。涵盖 `ts-pack process` 的功能开关、JSON 结果结构以及默认功能集。
编程语言检测
tree-sitter-language-pack-detecting-languages
当用户想知道某个文件或代码片段属于哪种编程语言时使用。涵盖 `ts-pack parse`/`process` 中的隐式检测、通过 `ts-pack list`/`info` 确认是否支持某种语言,以及用于路径、扩展名和原始内容的 SDK 检测函数。