feat: Add Go AST-aware chunking with tree-sitter support#88
Conversation
- Implement comprehensive Go AST chunker using tree-sitter-go - Extract semantic metadata (functions, methods, structs, interfaces, generics) - Support modern Go features including generics and type constraints - Provide intelligent fallback chunking for malformed code - Add comprehensive documentation and usage examples - Integrate seamlessly with existing LEANN chunking utilities
|
Nice PR, let's fix the lint error first and https://github.com/yichuan-w/LEANN/blob/main/docs/CONTRIBUTING.md#-pre-commit-hooks. We can follow this, @gabriel-dehan, if you have time you can TAL at this(I dont know why i cannot add you a s a reviewer hah, this ast stuff is created by gabriel-dehan) |
|
And @osobh , I was sort of confused why we cannot use the same code of previous java/C++ chunker and we need a new Go class, but anyway thanks for contribution! |
Thanks for the review @yichuan-w! ✅ Lint fixes are complete - all pre-commit checks are passing now. Regarding why Go needs its own chunker:
This implementation follows the same pattern as @gabriel-dehan's work but adds Go-specific support that wasn't available before. The code automatically routes Go files to this local implementation since the external Happy to make any adjustments needed! |
|
He @osobh! Thanks for the contribution! My opinion is that we should avoid having the Go specific AST chunker inside LEANN. And if that really isn't possible I feel we should instead use the adapter pattern and have a very clean and straighforward "common API" inside LEANN for all of the AST Chunking that would call the correct AST Chunker (yours or Also I am not sure I understand the purpose of the 3 new files at the root level "chunking.py", "leann" and "leann_cli.py" and I have trouble seeing what they bring in relation to AST Chunking. Why do we need those re-exports and those "entrypoints"? (I am not super familiar with python coding patterns though) Cheers |
|
Yeah, I agree with @gabriel-dehan, the PR is not ready yet, and I am ok with a separate go function, but we should keep minimal changes with the existing api done by @gabriel-dehan (if we are unable to do that, then something like if language == go may be ok). I think only https://github.com/osobh/LEANN/blob/ast-go/apps/chunking/ast_chunkers/go.py is useful, and we should only keep this and make sure the interface modification is minimal. |
|
Again, thanks for the contribution and let's make a cleaner PR and then we can merge @osobh |
|
I agree as well, if you can fork the astchunk repo and we can work on that, I'd love to contribute Go and Rust (that was my next one), i'm down , lets make it happen! |
|
Yeah, let's do a PR in the submodule and make LEANN clean, cheers! |
Pull Request: Go AST-Aware Chunking Implementation
🚀 Summary
This PR implements comprehensive Go AST-aware chunking for LEANN, enabling semantic code understanding and improved RAG performance on Go codebases. The implementation uses tree-sitter-go for robust AST parsing and extracts rich metadata while maintaining backward compatibility.
Key Features
• Complete Go language support - functions, methods, structs, interfaces, generics
• Rich semantic metadata - complexity scoring, dependency tracking, type information
• Modern Go features - generics, type constraints, embedded types, pointer receivers
• Robust error handling - graceful fallback when parsing fails or dependencies missing
• Performance optimized - intelligent chunking with size management and overlap control
• Comprehensive testing - 27+ test cases covering all constructs and edge cases
📋 Changes
Core Implementation
apps/chunking/ast_chunkers/go.py- Complete Go AST chunker implementationGoASTChunkerclass with tree-sitter-go integrationGoCodeBlockdataclass for structured metadataapps/chunking/utils.py- Enhanced chunking utilitieschunking.py- Bridge module for CLI integrationTesting & Quality
tests/test_astchunk_integration.py- Comprehensive test suiteDocumentation
apps/chunking/README.md- Complete system documentation🧪 Test Results
Test Results Breakdown:
Go-Specific Tests: 6/6 PASSED ✅
📈 Performance & Impact
Storage & Retrieval Benefits
Integration Points
leann build --enable-code-chunking🔧 Dependencies
Already declared in
pyproject.toml:Fallback Behavior: When tree-sitter dependencies unavailable, falls back to heuristic-based Go parsing, then traditional chunking.
💡 Usage Examples
CLI Usage
Programmatic Usage
Metadata Example
{ "text": "func (u *User) GetName() string { return u.Name }", "metadata": { "type": "method", "name": "GetName", "receiver": "User", "receiver_pointer": true, "package_name": "user", "complexity_score": 1, "dependencies": ["string"], "start_line": 15, "end_line": 17 } }🚦 Compatibility
🔮 Future Extensions
This implementation provides a solid foundation for:
✅ Checklist