@floydous/pi-agent-kernel 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +73 -86
- package/docs/testing.md +10 -9
- package/package.json +5 -4
- package/src/dedup/content_store.ts +365 -365
- package/src/index.ts +918 -918
- package/src/retrieval/ast_search.ts +930 -930
- package/src/retrieval/repomap.ts +192 -7
- package/src/retrieval/search_bm25.ts +190 -190
- package/src/retrieval/search_chunker.ts +254 -254
- package/src/retrieval/symbol_reader.ts +198 -203
- package/src/retrieval/tree_sitter_engine.ts +1 -1
- package/src/safety/epistemic_guard.ts +8 -1
- package/src/tools/ast_search_formatter.ts +161 -0
- package/src/tools/ast_search_tool.ts +17 -10
- package/src/tools/context.ts +20 -20
- package/src/tools/lsp_tool.ts +13 -2
- package/src/ui/tui_utils.ts +278 -278
- package/static/with-agent-kernel.png +0 -0
- package/static/without-agent-kernel-1.png +0 -0
- package/static/without-agent-kernel-2.png +0 -0
- package/static/without-agent-kernel-3.png +0 -0
|
@@ -1,254 +1,254 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import * as crypto from "node:crypto";
|
|
4
|
-
import { extractFileTags, SymbolDef } from "./repomap";
|
|
5
|
-
|
|
6
|
-
export interface CodeChunk {
|
|
7
|
-
id: string; // e.g. "src/auth.ts:45-90#verifyToken"
|
|
8
|
-
filePath: string; // Relative path
|
|
9
|
-
absolutePath: string;
|
|
10
|
-
startLine: number;
|
|
11
|
-
endLine: number;
|
|
12
|
-
symbolName: string;
|
|
13
|
-
kind: string; // "function" | "class" | "method" | "interface" | "type" | "block"
|
|
14
|
-
signature: string;
|
|
15
|
-
breadcrumb: string; // e.g. "// [File: src/auth.ts] > [Class: AuthManager] > [Function: verifyToken]"
|
|
16
|
-
content: string; // The full code block text
|
|
17
|
-
textForEmbedding: string;// Search-optimized representation
|
|
18
|
-
hash: string; // SHA-256 hash of content
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const SUPPORTED_EXTENSIONS = new Set([
|
|
22
|
-
".ts", ".tsx", ".js", ".jsx", ".py", ".rs", ".go", ".c", ".cpp", ".h", ".hpp", ".java", ".cs", ".rb", ".php", ".swift", ".sh", ".bash", ".sql", ".md"
|
|
23
|
-
]);
|
|
24
|
-
|
|
25
|
-
const IGNORED_DIRS = new Set([
|
|
26
|
-
".git", "node_modules", ".venv", "venv", "__pycache__", "dist", "build", "coverage", ".pi", ".hermes", ".next", ".turbo", ".cache", "target", "vendor"
|
|
27
|
-
]);
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Compute SHA-256 hash of a string.
|
|
31
|
-
*/
|
|
32
|
-
export function computeHash(content: string): string {
|
|
33
|
-
return crypto.createHash("sha256").update(content, "utf8").digest("hex").slice(0, 16);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Chunk a single source file into logical AST-bounded chunks.
|
|
38
|
-
*/
|
|
39
|
-
export function chunkFile(rootDir: string, filePath: string, content?: string): CodeChunk[] {
|
|
40
|
-
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(rootDir, filePath);
|
|
41
|
-
const relPath = path.relative(rootDir, absPath).replace(/\\/g, "/");
|
|
42
|
-
|
|
43
|
-
let fileContent = content;
|
|
44
|
-
if (fileContent === undefined) {
|
|
45
|
-
try {
|
|
46
|
-
fileContent = fs.readFileSync(absPath, "utf-8");
|
|
47
|
-
} catch {
|
|
48
|
-
return [];
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const lines = fileContent.split("\n");
|
|
53
|
-
if (lines.length === 0) return [];
|
|
54
|
-
|
|
55
|
-
const ext = path.extname(absPath).toLowerCase();
|
|
56
|
-
const chunks: CodeChunk[] = [];
|
|
57
|
-
|
|
58
|
-
// 1. Extract AST symbols using extractFileTags
|
|
59
|
-
const tags = extractFileTags(absPath, fileContent);
|
|
60
|
-
const defs = tags.definitions.sort((a, b) => a.line - b.line);
|
|
61
|
-
|
|
62
|
-
// If file has no AST definitions or is small (< 40 lines), chunk as single whole file
|
|
63
|
-
if (defs.length === 0 || lines.length <= 40) {
|
|
64
|
-
const wholeContent = lines.join("\n");
|
|
65
|
-
const breadcrumb = `// [File: ${relPath}]`;
|
|
66
|
-
const hash = computeHash(wholeContent);
|
|
67
|
-
chunks.push({
|
|
68
|
-
id: `${relPath}:1-${lines.length}`,
|
|
69
|
-
filePath: relPath,
|
|
70
|
-
absolutePath: absPath,
|
|
71
|
-
startLine: 1,
|
|
72
|
-
endLine: lines.length,
|
|
73
|
-
symbolName: path.basename(relPath),
|
|
74
|
-
kind: "file",
|
|
75
|
-
signature: `File: ${relPath}`,
|
|
76
|
-
breadcrumb,
|
|
77
|
-
content: wholeContent,
|
|
78
|
-
textForEmbedding: `${breadcrumb}\n${wholeContent.slice(0, 1500)}`,
|
|
79
|
-
hash,
|
|
80
|
-
});
|
|
81
|
-
return chunks;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// 2. Map symbol spans using delimiter & indentation boundaries
|
|
85
|
-
interface SymbolSpan {
|
|
86
|
-
def: SymbolDef;
|
|
87
|
-
startLine: number;
|
|
88
|
-
endLine: number;
|
|
89
|
-
parentClass?: string;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const spans: SymbolSpan[] = [];
|
|
93
|
-
let currentClass: string | undefined = undefined;
|
|
94
|
-
let currentClassEndLine = -1;
|
|
95
|
-
|
|
96
|
-
for (let i = 0; i < defs.length; i++) {
|
|
97
|
-
const def = defs[i];
|
|
98
|
-
const startIdx = Math.max(0, def.line - 1);
|
|
99
|
-
let endIdx = lines.length - 1;
|
|
100
|
-
|
|
101
|
-
if (def.line > currentClassEndLine) {
|
|
102
|
-
currentClass = undefined;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (def.kind === "class") {
|
|
106
|
-
currentClass = def.name;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (def.endLine && def.endLine >= def.line) {
|
|
110
|
-
// Exact AST node boundaries from Tree-sitter
|
|
111
|
-
endIdx = Math.min(lines.length - 1, def.endLine - 1);
|
|
112
|
-
} else if (ext === ".py") {
|
|
113
|
-
// Python scope
|
|
114
|
-
const defLine = lines[startIdx];
|
|
115
|
-
const matchIndent = defLine.match(/^(\s*)/);
|
|
116
|
-
const baseIndent = matchIndent ? matchIndent[1].length : 0;
|
|
117
|
-
|
|
118
|
-
for (let j = startIdx + 1; j < lines.length; j++) {
|
|
119
|
-
const line = lines[j];
|
|
120
|
-
if (!line.trim() || line.trim().startsWith("#")) continue;
|
|
121
|
-
const indent = line.match(/^(\s*)/)?.[1].length || 0;
|
|
122
|
-
if (indent <= baseIndent) {
|
|
123
|
-
endIdx = j - 1;
|
|
124
|
-
break;
|
|
125
|
-
}
|
|
126
|
-
endIdx = j;
|
|
127
|
-
}
|
|
128
|
-
} else {
|
|
129
|
-
// Brace based scope (fallback)
|
|
130
|
-
let braceCount = 0;
|
|
131
|
-
let foundOpenBrace = false;
|
|
132
|
-
|
|
133
|
-
for (let j = startIdx; j < lines.length; j++) {
|
|
134
|
-
const line = lines[j];
|
|
135
|
-
for (const ch of line) {
|
|
136
|
-
if (ch === "{") {
|
|
137
|
-
braceCount++;
|
|
138
|
-
foundOpenBrace = true;
|
|
139
|
-
} else if (ch === "}") {
|
|
140
|
-
braceCount--;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
if (foundOpenBrace && braceCount <= 0) {
|
|
144
|
-
endIdx = j;
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// Bound endLine to before next top-level def if necessary
|
|
151
|
-
if (i + 1 < defs.length && endIdx >= defs[i + 1].line - 1 && def.kind !== "class") {
|
|
152
|
-
endIdx = Math.min(endIdx, defs[i + 1].line - 2);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
endIdx = Math.max(startIdx, Math.min(lines.length - 1, endIdx));
|
|
156
|
-
|
|
157
|
-
if (def.kind === "class") {
|
|
158
|
-
currentClassEndLine = endIdx + 1;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
spans.push({
|
|
162
|
-
def,
|
|
163
|
-
startLine: startIdx + 1,
|
|
164
|
-
endLine: endIdx + 1,
|
|
165
|
-
parentClass: def.kind !== "class" && def.line <= currentClassEndLine ? currentClass : undefined,
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// 3. Construct CodeChunk objects from spans
|
|
170
|
-
for (const span of spans) {
|
|
171
|
-
const startIdx = span.startLine - 1;
|
|
172
|
-
const endIdx = span.endLine - 1;
|
|
173
|
-
const chunkLines = lines.slice(startIdx, endIdx + 1);
|
|
174
|
-
const chunkContent = chunkLines.join("\n");
|
|
175
|
-
|
|
176
|
-
if (!chunkContent.trim()) continue;
|
|
177
|
-
|
|
178
|
-
let breadcrumb = `// [File: ${relPath}]`;
|
|
179
|
-
if (span.parentClass) {
|
|
180
|
-
breadcrumb += ` > [Class: ${span.parentClass}]`;
|
|
181
|
-
}
|
|
182
|
-
breadcrumb += ` > [${span.def.kind.toUpperCase()}: ${span.def.name}]`;
|
|
183
|
-
|
|
184
|
-
const textForEmbedding = `${breadcrumb}\nSignature: ${span.def.signature}\n\n${chunkContent.slice(0, 1500)}`;
|
|
185
|
-
const hash = computeHash(chunkContent);
|
|
186
|
-
|
|
187
|
-
chunks.push({
|
|
188
|
-
id: `${relPath}:${span.startLine}-${span.endLine}#${span.def.name}`,
|
|
189
|
-
filePath: relPath,
|
|
190
|
-
absolutePath: absPath,
|
|
191
|
-
startLine: span.startLine,
|
|
192
|
-
endLine: span.endLine,
|
|
193
|
-
symbolName: span.def.name,
|
|
194
|
-
kind: span.def.kind,
|
|
195
|
-
signature: span.def.signature,
|
|
196
|
-
breadcrumb,
|
|
197
|
-
content: chunkContent,
|
|
198
|
-
textForEmbedding,
|
|
199
|
-
hash,
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return chunks;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Find supported source files in a workspace using the same traversal policy as
|
|
208
|
-
* chunkWorkspace. The search index uses this to check live file freshness
|
|
209
|
-
* without reparsing unchanged files.
|
|
210
|
-
*/
|
|
211
|
-
export function findChunkableFiles(rootDir: string, maxFiles = 500): string[] {
|
|
212
|
-
const files: string[] = [];
|
|
213
|
-
|
|
214
|
-
function scan(dir: string) {
|
|
215
|
-
if (files.length >= maxFiles) return;
|
|
216
|
-
let entries: fs.Dirent[];
|
|
217
|
-
try {
|
|
218
|
-
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
219
|
-
} catch {
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
for (const entry of entries) {
|
|
224
|
-
if (entry.name.startsWith(".") && entry.name !== ".github") continue;
|
|
225
|
-
if (IGNORED_DIRS.has(entry.name)) continue;
|
|
226
|
-
|
|
227
|
-
const fullPath = path.join(dir, entry.name);
|
|
228
|
-
if (entry.isDirectory()) {
|
|
229
|
-
scan(fullPath);
|
|
230
|
-
} else if (entry.isFile()) {
|
|
231
|
-
const ext = path.extname(entry.name).toLowerCase();
|
|
232
|
-
if (SUPPORTED_EXTENSIONS.has(ext)) {
|
|
233
|
-
files.push(fullPath);
|
|
234
|
-
if (files.length >= maxFiles) return;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
scan(rootDir);
|
|
241
|
-
return files;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Scan workspace and chunk all supported source files.
|
|
246
|
-
*/
|
|
247
|
-
export function chunkWorkspace(rootDir: string, maxFiles = 500): { chunks: CodeChunk[]; fileCount: number } {
|
|
248
|
-
const files = findChunkableFiles(rootDir, maxFiles);
|
|
249
|
-
const chunks: CodeChunk[] = [];
|
|
250
|
-
for (const filePath of files) {
|
|
251
|
-
chunks.push(...chunkFile(rootDir, filePath));
|
|
252
|
-
}
|
|
253
|
-
return { chunks, fileCount: files.length };
|
|
254
|
-
}
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import * as crypto from "node:crypto";
|
|
4
|
+
import { extractFileTags, SymbolDef } from "./repomap";
|
|
5
|
+
|
|
6
|
+
export interface CodeChunk {
|
|
7
|
+
id: string; // e.g. "src/auth.ts:45-90#verifyToken"
|
|
8
|
+
filePath: string; // Relative path
|
|
9
|
+
absolutePath: string;
|
|
10
|
+
startLine: number;
|
|
11
|
+
endLine: number;
|
|
12
|
+
symbolName: string;
|
|
13
|
+
kind: string; // "function" | "class" | "method" | "interface" | "type" | "block"
|
|
14
|
+
signature: string;
|
|
15
|
+
breadcrumb: string; // e.g. "// [File: src/auth.ts] > [Class: AuthManager] > [Function: verifyToken]"
|
|
16
|
+
content: string; // The full code block text
|
|
17
|
+
textForEmbedding: string;// Search-optimized representation
|
|
18
|
+
hash: string; // SHA-256 hash of content
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const SUPPORTED_EXTENSIONS = new Set([
|
|
22
|
+
".ts", ".tsx", ".js", ".jsx", ".py", ".rs", ".go", ".c", ".cpp", ".h", ".hpp", ".java", ".cs", ".rb", ".php", ".swift", ".sh", ".bash", ".sql", ".md"
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
const IGNORED_DIRS = new Set([
|
|
26
|
+
".git", "node_modules", ".venv", "venv", "__pycache__", "dist", "build", "coverage", ".pi", ".hermes", ".next", ".turbo", ".cache", "target", "vendor"
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Compute SHA-256 hash of a string.
|
|
31
|
+
*/
|
|
32
|
+
export function computeHash(content: string): string {
|
|
33
|
+
return crypto.createHash("sha256").update(content, "utf8").digest("hex").slice(0, 16);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Chunk a single source file into logical AST-bounded chunks.
|
|
38
|
+
*/
|
|
39
|
+
export function chunkFile(rootDir: string, filePath: string, content?: string): CodeChunk[] {
|
|
40
|
+
const absPath = path.isAbsolute(filePath) ? filePath : path.resolve(rootDir, filePath);
|
|
41
|
+
const relPath = path.relative(rootDir, absPath).replace(/\\/g, "/");
|
|
42
|
+
|
|
43
|
+
let fileContent = content;
|
|
44
|
+
if (fileContent === undefined) {
|
|
45
|
+
try {
|
|
46
|
+
fileContent = fs.readFileSync(absPath, "utf-8");
|
|
47
|
+
} catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const lines = fileContent.split("\n");
|
|
53
|
+
if (lines.length === 0) return [];
|
|
54
|
+
|
|
55
|
+
const ext = path.extname(absPath).toLowerCase();
|
|
56
|
+
const chunks: CodeChunk[] = [];
|
|
57
|
+
|
|
58
|
+
// 1. Extract AST symbols using extractFileTags
|
|
59
|
+
const tags = extractFileTags(absPath, fileContent);
|
|
60
|
+
const defs = tags.definitions.sort((a, b) => a.line - b.line);
|
|
61
|
+
|
|
62
|
+
// If file has no AST definitions or is small (< 40 lines), chunk as single whole file
|
|
63
|
+
if (defs.length === 0 || lines.length <= 40) {
|
|
64
|
+
const wholeContent = lines.join("\n");
|
|
65
|
+
const breadcrumb = `// [File: ${relPath}]`;
|
|
66
|
+
const hash = computeHash(wholeContent);
|
|
67
|
+
chunks.push({
|
|
68
|
+
id: `${relPath}:1-${lines.length}`,
|
|
69
|
+
filePath: relPath,
|
|
70
|
+
absolutePath: absPath,
|
|
71
|
+
startLine: 1,
|
|
72
|
+
endLine: lines.length,
|
|
73
|
+
symbolName: path.basename(relPath),
|
|
74
|
+
kind: "file",
|
|
75
|
+
signature: `File: ${relPath}`,
|
|
76
|
+
breadcrumb,
|
|
77
|
+
content: wholeContent,
|
|
78
|
+
textForEmbedding: `${breadcrumb}\n${wholeContent.slice(0, 1500)}`,
|
|
79
|
+
hash,
|
|
80
|
+
});
|
|
81
|
+
return chunks;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 2. Map symbol spans using delimiter & indentation boundaries
|
|
85
|
+
interface SymbolSpan {
|
|
86
|
+
def: SymbolDef;
|
|
87
|
+
startLine: number;
|
|
88
|
+
endLine: number;
|
|
89
|
+
parentClass?: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const spans: SymbolSpan[] = [];
|
|
93
|
+
let currentClass: string | undefined = undefined;
|
|
94
|
+
let currentClassEndLine = -1;
|
|
95
|
+
|
|
96
|
+
for (let i = 0; i < defs.length; i++) {
|
|
97
|
+
const def = defs[i];
|
|
98
|
+
const startIdx = Math.max(0, def.line - 1);
|
|
99
|
+
let endIdx = lines.length - 1;
|
|
100
|
+
|
|
101
|
+
if (def.line > currentClassEndLine) {
|
|
102
|
+
currentClass = undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (def.kind === "class") {
|
|
106
|
+
currentClass = def.name;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (def.endLine && def.endLine >= def.line) {
|
|
110
|
+
// Exact AST node boundaries from Tree-sitter
|
|
111
|
+
endIdx = Math.min(lines.length - 1, def.endLine - 1);
|
|
112
|
+
} else if (ext === ".py") {
|
|
113
|
+
// Python scope
|
|
114
|
+
const defLine = lines[startIdx];
|
|
115
|
+
const matchIndent = defLine.match(/^(\s*)/);
|
|
116
|
+
const baseIndent = matchIndent ? matchIndent[1].length : 0;
|
|
117
|
+
|
|
118
|
+
for (let j = startIdx + 1; j < lines.length; j++) {
|
|
119
|
+
const line = lines[j];
|
|
120
|
+
if (!line.trim() || line.trim().startsWith("#")) continue;
|
|
121
|
+
const indent = line.match(/^(\s*)/)?.[1].length || 0;
|
|
122
|
+
if (indent <= baseIndent) {
|
|
123
|
+
endIdx = j - 1;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
endIdx = j;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
// Brace based scope (fallback)
|
|
130
|
+
let braceCount = 0;
|
|
131
|
+
let foundOpenBrace = false;
|
|
132
|
+
|
|
133
|
+
for (let j = startIdx; j < lines.length; j++) {
|
|
134
|
+
const line = lines[j];
|
|
135
|
+
for (const ch of line) {
|
|
136
|
+
if (ch === "{") {
|
|
137
|
+
braceCount++;
|
|
138
|
+
foundOpenBrace = true;
|
|
139
|
+
} else if (ch === "}") {
|
|
140
|
+
braceCount--;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (foundOpenBrace && braceCount <= 0) {
|
|
144
|
+
endIdx = j;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Bound endLine to before next top-level def if necessary
|
|
151
|
+
if (i + 1 < defs.length && endIdx >= defs[i + 1].line - 1 && def.kind !== "class") {
|
|
152
|
+
endIdx = Math.min(endIdx, defs[i + 1].line - 2);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
endIdx = Math.max(startIdx, Math.min(lines.length - 1, endIdx));
|
|
156
|
+
|
|
157
|
+
if (def.kind === "class") {
|
|
158
|
+
currentClassEndLine = endIdx + 1;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
spans.push({
|
|
162
|
+
def,
|
|
163
|
+
startLine: startIdx + 1,
|
|
164
|
+
endLine: endIdx + 1,
|
|
165
|
+
parentClass: def.kind !== "class" && def.line <= currentClassEndLine ? currentClass : undefined,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 3. Construct CodeChunk objects from spans
|
|
170
|
+
for (const span of spans) {
|
|
171
|
+
const startIdx = span.startLine - 1;
|
|
172
|
+
const endIdx = span.endLine - 1;
|
|
173
|
+
const chunkLines = lines.slice(startIdx, endIdx + 1);
|
|
174
|
+
const chunkContent = chunkLines.join("\n");
|
|
175
|
+
|
|
176
|
+
if (!chunkContent.trim()) continue;
|
|
177
|
+
|
|
178
|
+
let breadcrumb = `// [File: ${relPath}]`;
|
|
179
|
+
if (span.parentClass) {
|
|
180
|
+
breadcrumb += ` > [Class: ${span.parentClass}]`;
|
|
181
|
+
}
|
|
182
|
+
breadcrumb += ` > [${span.def.kind.toUpperCase()}: ${span.def.name}]`;
|
|
183
|
+
|
|
184
|
+
const textForEmbedding = `${breadcrumb}\nSignature: ${span.def.signature}\n\n${chunkContent.slice(0, 1500)}`;
|
|
185
|
+
const hash = computeHash(chunkContent);
|
|
186
|
+
|
|
187
|
+
chunks.push({
|
|
188
|
+
id: `${relPath}:${span.startLine}-${span.endLine}#${span.def.name}`,
|
|
189
|
+
filePath: relPath,
|
|
190
|
+
absolutePath: absPath,
|
|
191
|
+
startLine: span.startLine,
|
|
192
|
+
endLine: span.endLine,
|
|
193
|
+
symbolName: span.def.name,
|
|
194
|
+
kind: span.def.kind,
|
|
195
|
+
signature: span.def.signature,
|
|
196
|
+
breadcrumb,
|
|
197
|
+
content: chunkContent,
|
|
198
|
+
textForEmbedding,
|
|
199
|
+
hash,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return chunks;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Find supported source files in a workspace using the same traversal policy as
|
|
208
|
+
* chunkWorkspace. The search index uses this to check live file freshness
|
|
209
|
+
* without reparsing unchanged files.
|
|
210
|
+
*/
|
|
211
|
+
export function findChunkableFiles(rootDir: string, maxFiles = 500): string[] {
|
|
212
|
+
const files: string[] = [];
|
|
213
|
+
|
|
214
|
+
function scan(dir: string) {
|
|
215
|
+
if (files.length >= maxFiles) return;
|
|
216
|
+
let entries: fs.Dirent[];
|
|
217
|
+
try {
|
|
218
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
219
|
+
} catch {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
for (const entry of entries) {
|
|
224
|
+
if (entry.name.startsWith(".") && entry.name !== ".github") continue;
|
|
225
|
+
if (IGNORED_DIRS.has(entry.name)) continue;
|
|
226
|
+
|
|
227
|
+
const fullPath = path.join(dir, entry.name);
|
|
228
|
+
if (entry.isDirectory()) {
|
|
229
|
+
scan(fullPath);
|
|
230
|
+
} else if (entry.isFile()) {
|
|
231
|
+
const ext = path.extname(entry.name).toLowerCase();
|
|
232
|
+
if (SUPPORTED_EXTENSIONS.has(ext)) {
|
|
233
|
+
files.push(fullPath);
|
|
234
|
+
if (files.length >= maxFiles) return;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
scan(rootDir);
|
|
241
|
+
return files;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Scan workspace and chunk all supported source files.
|
|
246
|
+
*/
|
|
247
|
+
export function chunkWorkspace(rootDir: string, maxFiles = 500): { chunks: CodeChunk[]; fileCount: number } {
|
|
248
|
+
const files = findChunkableFiles(rootDir, maxFiles);
|
|
249
|
+
const chunks: CodeChunk[] = [];
|
|
250
|
+
for (const filePath of files) {
|
|
251
|
+
chunks.push(...chunkFile(rootDir, filePath));
|
|
252
|
+
}
|
|
253
|
+
return { chunks, fileCount: files.length };
|
|
254
|
+
}
|