-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcode-chunker.ts
More file actions
183 lines (153 loc) · 6.15 KB
/
code-chunker.ts
File metadata and controls
183 lines (153 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import * as fs from 'fs';
import * as path from 'path';
import { Language, Parser } from 'web-tree-sitter';
import type { SyntaxNode } from 'web-tree-sitter';
type TokenCounter = (text: string) => Promise<number>;
export interface CodeChunkerOptions {
lang: string;
chunkSize?: number;
tokenCounter?: TokenCounter;
}
export interface CodeChunk {
text: string;
tokenCount: number;
}
export class CodeChunker {
private readonly lang: string;
private readonly chunkSize: number;
private readonly tokenCounter: TokenCounter;
private static treeSitterInitialized = false;
private static parserCache: Map<string, Promise<Parser>> = new Map();
private constructor(lang: string, chunkSize: number, tokenCounter: TokenCounter) {
this.lang = lang;
this.chunkSize = chunkSize;
this.tokenCounter = tokenCounter;
}
static async create(options: CodeChunkerOptions): Promise<CodeChunker> {
if (!CodeChunker.treeSitterInitialized && Parser.init) {
try {
await Parser.init();
CodeChunker.treeSitterInitialized = true;
} catch (error) {
console.warn('Failed to initialize tree-sitter parser:', error);
}
}
const chunkSize = options.chunkSize ?? 512;
if (chunkSize <= 0) {
throw new Error('chunkSize must be greater than 0');
}
const tokenCounter = options.tokenCounter ?? (async (text: string) => text.length);
const chunker = new CodeChunker(options.lang, chunkSize, tokenCounter);
return chunker;
}
async chunk(text: string): Promise<CodeChunk[]> {
if (!text.trim()) {
return [];
}
const parser = await CodeChunker.getParser(this.lang);
const originalTextBytes = Buffer.from(text, 'utf-8');
const tree = parser.parse(originalTextBytes.toString());
if (!tree) {
throw new Error('Failed to parse code');
}
const chunks: CodeChunk[] = [];
await this.recursiveChunk(tree.rootNode, originalTextBytes.toString(), chunks);
return this.mergeChunks(chunks);
}
private static async getParser(lang: string): Promise<Parser> {
const formattedLang = lang.toLowerCase().replace(/-/g, '_');
const cached = this.parserCache.get(formattedLang);
if (cached) {
return cached;
}
const parserPromise = (async () => {
if (!CodeChunker.treeSitterInitialized && Parser.init) {
try {
await Parser.init();
CodeChunker.treeSitterInitialized = true;
} catch (error) {
console.warn('Failed to initialize tree-sitter parser:', error);
}
}
const wasmPath = CodeChunker.resolveWasmPath(formattedLang);
const wasmBuffer = fs.readFileSync(wasmPath);
const language = await Language.load(wasmBuffer);
const parser = new Parser();
parser.setLanguage(language);
return parser;
})();
this.parserCache.set(formattedLang, parserPromise);
return parserPromise;
}
private static resolveWasmPath(formattedLang: string): string {
const nodeModulesPath = CodeChunker.findNearestNodeModules(__dirname);
if (!nodeModulesPath) {
throw new Error('node_modules directory not found.');
}
const wasmPath = path.join(nodeModulesPath, `tree-sitter-wasms/out/tree-sitter-${formattedLang}.wasm`);
if (!fs.existsSync(wasmPath)) {
throw new Error(`Tree-sitter WASM file for language "${formattedLang}" not found at ${wasmPath}.`);
}
return wasmPath;
}
private static findNearestNodeModules(startDir: string): string | null {
let dir = path.resolve(startDir);
while (true) {
const candidate = path.join(dir, 'node_modules');
if (fs.existsSync(candidate) && fs.statSync(candidate).isDirectory()) {
return candidate;
}
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
return null;
}
private async recursiveChunk(node: SyntaxNode, source: string, chunks: CodeChunk[]): Promise<void> {
const nodeText = source.substring(node.startIndex, node.endIndex);
const tokenCount = await this.tokenCounter(nodeText);
const children = (node.children || []).filter((child): child is SyntaxNode => Boolean(child));
if (tokenCount <= this.chunkSize || children.length === 0) {
if (nodeText.trim()) {
chunks.push({ text: nodeText, tokenCount });
}
return;
}
const beforeCount = chunks.length;
for (const child of children) {
await this.recursiveChunk(child, source, chunks);
}
if (chunks.length === beforeCount && nodeText.trim()) {
chunks.push({ text: nodeText, tokenCount });
}
}
private mergeChunks(chunks: CodeChunk[]): CodeChunk[] {
const merged: CodeChunk[] = [];
let currentText = '';
let currentTokens = 0;
const separatorTokens = 1; // Account for the '\n' separator between merged chunks
for (const chunk of chunks) {
if (!chunk.text.trim()) {
continue;
}
const nextTokens = currentTokens + separatorTokens + chunk.tokenCount;
if (currentTokens === 0) {
currentText = chunk.text;
currentTokens = chunk.tokenCount;
continue;
}
if (nextTokens <= this.chunkSize) {
currentText = `${currentText}\n${chunk.text}`;
currentTokens = nextTokens;
continue;
}
merged.push({ text: currentText, tokenCount: currentTokens });
currentText = chunk.text;
currentTokens = chunk.tokenCount;
}
if (currentTokens > 0) {
merged.push({ text: currentText, tokenCount: currentTokens });
}
return merged;
}
}