libragen: First-Class AST-Aware Code Chunking Support
**Status:** In Progress
libragen: First-Class AST-Aware Code Chunking Support
Version: 1.0.0 Date: 2024-12-20 Status: In Progress
Summary
Integrate the code-chunk library from supermemoryai to provide AST-aware, semantic
code chunking as a first-class feature in libragen. This replaces the current
LangChain-based RecursiveCharacterTextSplitter for supported code files with
tree-sitter-powered chunking that respects semantic boundaries (functions, classes,
methods) and provides rich context (scope chains, imports, siblings, entity signatures)
for better embedding quality.
Objectives & Scope
In Scope
- Add
code-chunkas a dependency to@libragen/core - Create a new
CodeChunkerclass that wrapscode-chunkand implements the same interface pattern as the existingChunker - Extend
ChunkandChunkMetadatatypes to include semantic context from code-chunk (scope, entities, imports, siblings) - Update
Builderto useCodeChunkerfor supported code files, falling back to the existingChunkerfor unsupported files - Store semantic context in the database for retrieval-time enrichment
- Store
contextualizedTextin the database alongside raw content for maximum flexibility - Update CLI and MCP to expose new chunking options (e.g.,
--no-ast-chunking,--context-mode) - Add build option to enable/disable AST-aware chunking (default: enabled for code files)
- Update documentation across all packages
Out of Scope
- WASM/Cloudflare Workers support (code-chunk supports this, but libragen is Node.js-focused)
- Effect.js integration (use Promise-based API)
- Streaming chunking API (batch processing is sufficient for build)
Future Considerations
- Custom tree-sitter grammar support beyond what code-chunk provides - this would allow users to add support for additional languages by providing their own tree-sitter grammars
Assumptions & Open Questions
Assumptions
code-chunkis stable enough for production use (v0.1.11)- The
contextualizedTextfield from code-chunk is suitable for embedding (this is its intended use) - Node.js native tree-sitter bindings will work in libragen's target environments (Node 24+)
- Users will benefit from richer chunk context even if it increases storage slightly
Resolved Questions
-
Should AST chunking be opt-in or opt-out?
- Answer: Opt-out (enabled by default for code files)
-
Should we store both raw
textandcontextualizedText?- Answer: Yes, store both. Use
contextualizedTextfor embeddings, store rawcontentfor display. This provides maximum flexibility and highest quality output.
- Answer: Yes, store both. Use
-
How should we handle files that code-chunk doesn't support?
- Answer: Fall back to existing
Chunker
- Answer: Fall back to existing
-
Should chunk context (scope, entities, etc.) be stored in the database?
- Answer: Yes, in the metadata JSON column
-
What about backward compatibility with old libraries?
- Answer: Old libraries won't have semantic context data, but CLI/MCP should handle both old and new formats gracefully without errors
Requirements
Functional
- FR-1: Support AST-aware chunking for TypeScript, JavaScript, Python, Rust, Go, and Java files
- FR-2: Fall back to existing text-based chunking for unsupported file types
- FR-3: Store semantic context (scope chain, entities, imports, siblings) in chunk metadata
- FR-4: Store
contextualizedTextin the database for embedding and retrieval - FR-5: Use
contextualizedTextfor embedding generation to improve retrieval quality - FR-6: Expose
--no-ast-chunkingflag in CLI to disable AST-aware chunking - FR-7: Expose
--context-modeoption (none,minimal,full) for controlling context richness (default:full) - FR-8: Maintain backward compatibility with existing
.libragenfiles - FR-9: CLI and MCP must handle libraries with and without semantic context gracefully
Non-Functional
- NFR-1 (Performance): AST chunking should not significantly increase build time (tree-sitter is fast)
- NFR-2 (Storage): Chunk metadata storage increase should be reasonable (<30% file size increase due to storing contextualizedText)
- NFR-3 (Compatibility): Must work with Node.js 24+ on macOS, Linux, and Windows
Architecture & Design Overview
Data Flow
Source Files
│
▼
┌─────────────────────────────────────────────────────────┐
│ Builder._chunkSource() │
│ ├─ For each file: │
│ │ ├─ Is code file supported by code-chunk? │
│ │ │ ├─ YES → CodeChunker.chunkText() │
│ │ │ │ └─ Returns Chunk[] with context │
│ │ │ │ + embeddingContent │
│ │ │ └─ NO → Chunker.chunkText() (existing) │
│ │ │ └─ Returns Chunk[] (basic metadata) │
│ │ └─ Collect all chunks │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ Builder._generateEmbeddings() │
│ └─ Use chunk.embeddingContent ?? chunk.content │
│ (contextualizedText for code, raw for others) │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ VectorStore.addChunks() │
│ └─ Store: │
│ - chunk.content (raw text for display) │
│ - chunk.embeddingContent (contextualizedText) │
│ - chunk.metadata (includes codeContext) │
└─────────────────────────────────────────────────────────┘
Key Interfaces
// Semantic context from code-chunk
interface CodeContext {
scope: EntityInfo[]; // Scope chain (e.g., class > method)
entities: ChunkEntityInfo[]; // Entities defined in this chunk
siblings: SiblingInfo[]; // Nearby entities for context
imports: ImportInfo[]; // Relevant imports
}
// Extended ChunkMetadata
interface ChunkMetadata {
sourceFile: string;
startLine?: number;
endLine?: number;
language?: string;
// NEW: Semantic context from code-chunk
codeContext?: CodeContext;
}
// Extended Chunk
interface Chunk {
content: string; // Raw code text (for display)
embeddingContent?: string; // contextualizedText (for embedding)
metadata: ChunkMetadata;
}
Database Schema Changes
The chunks table already has a metadata JSON column. We will:
- Store
codeContextin the metadata JSON - Add a new column
embedding_contentto storecontextualizedTextseparately fromcontent
This allows:
- Displaying raw code to users
- Using enriched context for embeddings
- Retrieving semantic context at search time
Decisions & Trade-offs
-
Decision: Store both
contentandembeddingContent(contextualizedText)- Rationale: Maximum flexibility - raw content for display, contextualized for embeddings and potential re-embedding
- Trade-off: ~20-30% storage increase, but worth it for quality
-
Decision: Store semantic context in metadata JSON column
- Rationale: Enables rich retrieval-time features without schema migration
- Trade-off: Increases storage, but provides valuable context
-
Decision: Default to AST chunking with
contextMode: 'full'- Rationale: Best quality out of the box
- Trade-off: Users who want smaller files can opt out
-
Decision: Graceful fallback for unsupported files and old libraries
- Rationale: Seamless experience regardless of file type or library age
- Trade-off: Some code paths need to handle both cases
Task Grid
| Status | ID | Task | Priority | Depends On | Acceptance Criteria |
|---|---|---|---|---|---|
| [ ] | T-01 | Add code-chunk dependency | H | — | Package installed, types available |
| [ ] | T-02 | Extend Chunk and ChunkMetadata types | H | T-01 | Types include semantic context fields |
| [ ] | T-03 | Create CodeChunker class | H | T-02 | Class wraps code-chunk, matches Chunker pattern |
| [ ] | T-04 | Update Builder to use CodeChunker | H | T-03 | Builder uses AST chunking for supported files |
| [ ] | T-05 | Update VectorStore schema | M | T-02 | embedding_content column added |
| [ ] | T-06 | Add build options for AST chunking | M | T-04 | noAstChunking, contextMode options work |
| [ ] | T-07 | Update CLI with new options | M | T-06 | --no-ast-chunking, --context-mode flags |
| [ ] | T-08 | Update MCP tools with new options | M | T-06 | MCP build tool accepts new options |
| [ ] | T-09 | Write unit tests | H | T-03, T-04 | ≥90% coverage for new code |
| [ ] | T-10 | Update documentation | M | T-07, T-08 | READMEs, website docs updated |
Task Details
T-01 — Add code-chunk dependency
Goal: Install code-chunk package in @libragen/core.
Step-by-step instructions:
- Run
npm install --save-exact code-chunkinpackages/core - Verify TypeScript types are available
- Test basic import:
import { chunk, detectLanguage } from 'code-chunk'
T-02 — Extend Chunk and ChunkMetadata types
Goal: Add semantic context fields to chunk types.
Step-by-step instructions:
- Update
packages/core/src/chunker.ts:- Add
CodeContextinterface with scope, entities, siblings, imports - Add
codeContext?: CodeContexttoChunkMetadata - Add
embeddingContent?: stringtoChunkinterface
- Add
- Export new types from
packages/core/src/index.ts
T-03 — Create CodeChunker class
Goal: Create a wrapper around code-chunk that matches the Chunker interface
pattern.
Step-by-step instructions:
- Create
packages/core/src/code-chunker.ts - Implement
CodeChunkerclass with:static isSupported(filePath: string): boolean- check if file is supportedstatic detectLanguage(filePath: string): Language | nullasync chunkText(content: string, filePath: string): Promise<Chunk[]>async chunkFile(filePath: string): Promise<Chunk[]>async chunkSourceFiles(files: SourceFile[]): Promise<Chunk[]>
- Map code-chunk's
Chunktype to libragen'sChunktype - Handle errors gracefully (fall back to null/empty on parse errors)
T-04 — Update Builder to use CodeChunker
Goal: Integrate CodeChunker into the build pipeline.
Step-by-step instructions:
- Update
packages/core/src/builder.ts:- Import
CodeChunker - Add
noAstChunking?: booleanandcontextMode?: 'none' | 'minimal' | 'full'toBuildOptions - Modify
_chunkSource()to:- Use
CodeChunkerfor supported files (unlessnoAstChunkingis true) - Fall back to
Chunkerfor unsupported files
- Use
- Modify
_generateEmbeddings()to usechunk.embeddingContent ?? chunk.content
- Import
- Update
chunking.strategyin metadata to indicate AST chunking was used
T-05 — Update VectorStore schema
Goal: Add embedding_content column to store contextualizedText.
Step-by-step instructions:
- Update
packages/core/src/store.ts:- Add
embedding_content TEXTcolumn to chunks table - Update
addChunk()andaddChunks()to storeembeddingContent - Update
StoredChunktype to includeembeddingContent - Update retrieval methods to return
embeddingContent
- Add
- Handle backward compatibility: if
embedding_contentis NULL, fall back tocontent
T-06 — Add build options for AST chunking
Goal: Allow users to control AST chunking behavior.
Step-by-step instructions:
- Add to
BuildOptionsinpackages/core/src/builder.ts:/** Disable AST-aware chunking for code files (default: false) */ noAstChunking?: boolean; /** Context mode for AST chunking (default: 'full') */ contextMode?: 'none' | 'minimal' | 'full'; - Pass options through to
CodeChunker
T-07 — Update CLI with new options
Goal: Expose AST chunking options in the CLI.
Step-by-step instructions:
- Update
packages/cli/src/commands/build.ts:- Add
--no-ast-chunkingflag - Add
--context-mode <mode>option with choices:none,minimal,full
- Add
- Pass options to
Builder.build() - Update CLI help text
T-08 — Update MCP tools with new options
Goal: Expose AST chunking options in MCP build tool.
Step-by-step instructions:
- Update
packages/mcp/src/tools/build.ts:- Add
noAstChunkingandcontextModeto tool parameters
- Add
- Update
packages/mcp/src/tasks/build-worker.tsto pass options
T-09 — Write unit tests
Goal: Ensure new functionality is well-tested.
Step-by-step instructions:
- Create
packages/core/src/__tests__/code-chunker.test.ts:- Test
isSupported()for various file extensions - Test
chunkText()with TypeScript, Python, etc. - Test fallback behavior for unsupported files
- Test error handling for malformed code
- Test
- Update
packages/core/src/__tests__/builder.test.ts:- Test build with AST chunking enabled
- Test build with
noAstChunking: true - Test
contextModeoptions - Test mixed file types (code + markdown)
T-10 — Update documentation
Goal: Document new features for users.
Step-by-step instructions:
- Update
packages/core/README.md:- Document
CodeChunkerclass - Document new
BuildOptions
- Document
- Update
packages/cli/README.md:- Document
--no-ast-chunkingand--context-modeflags
- Document
- Update
packages/mcp/README.md:- Document new build tool parameters
- Update
packages/website/src/content/docs/building.md:- Add section on AST-aware chunking
- Explain benefits and when to use/disable
New Code
packages/core/src/code-chunker.ts (new file)
Creates a CodeChunker class that:
- Wraps the
code-chunklibrary - Provides
isSupported(),detectLanguage(),chunkText(),chunkFile(),chunkSourceFiles()methods - Maps code-chunk's
Chunktype to libragen'sChunktype - Handles errors gracefully
packages/core/src/chunker.ts (modifications)
- Add
CodeContextinterface - Add
codeContext?: CodeContexttoChunkMetadata - Add
embeddingContent?: stringtoChunk
packages/core/src/builder.ts (modifications)
- Import
CodeChunker - Add
noAstChunking,contextModetoBuildOptions - Update
_chunkSource()to useCodeChunkerfor supported files - Update
_generateEmbeddings()to useembeddingContent - Update metadata to reflect chunking strategy
packages/core/src/store.ts (modifications)
- Add
embedding_contentcolumn - Update insert/retrieve methods
- Handle backward compatibility
packages/cli/src/commands/build.ts (modifications)
- Add
--no-ast-chunkingflag - Add
--context-modeoption
packages/mcp/src/tools/build.ts (modifications)
- Add
noAstChunking,contextModeparameters
Tests
-
code-chunker.test.ts:isSupported()returns true for.ts,.tsx,.js,.jsx,.py,.rs,.go,.javaisSupported()returns false for.md,.json,.txt, etc.chunkText()produces chunks with semantic context for TypeScript codechunkText()handles parse errors gracefullychunkSourceFiles()processes multiple files correctly
-
builder.test.ts(additions):- Build with AST chunking produces chunks with
codeContext - Build with
noAstChunking: trueproduces chunks withoutcodeContext contextModeoption is respected- Mixed file types (code + markdown) are handled correctly
- Build with AST chunking produces chunks with
-
Integration tests:
- Build a library from a TypeScript project
- Verify chunks have semantic context
- Verify search quality improvement (manual verification)
Review Checklist
- Have all outstanding questions been answered?
- Are there any ambiguities that need to be resolved?
Related Documents
基于命题分块以增强RAG
命题分块技术(Proposition Chunking)——这是一种通过将文档分解为原子级事实陈述来实现更精准检索的先进方法。与传统仅按字符数分割文本的分块方式不同,命题分块能保持单个事实的语义完整性。
TileMap Chunk Manager
**Category:** Performance - 2D Rendering & Memory Management
🤖 n8n AI Agent Mastery Course 2025
Welcome to the most comprehensive n8n AI Agent course! Build powerful automation workflows and intelligent AI agents using n8n's visual workflow builder.
Document Chunking/Splitting in Langroid
Langroid's [`ParsingConfig`][langroid.parsing.parser.ParsingConfig]