@floydous/pi-agent-kernel 0.1.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/LICENSE +15 -0
- package/README.md +108 -0
- package/config.toml +24 -0
- package/docs/README.md +22 -0
- package/docs/architecture.md +40 -0
- package/docs/configuration.md +43 -0
- package/docs/editing-and-verification.md +41 -0
- package/docs/lsp.md +31 -0
- package/docs/retrieval.md +42 -0
- package/docs/testing.md +32 -0
- package/package.json +48 -0
- package/src/config/index.ts +2 -0
- package/src/config/kernel_config.ts +218 -0
- package/src/config/toml.ts +237 -0
- package/src/context/session_repair.ts +99 -0
- package/src/dedup/content_store.ts +365 -0
- package/src/dedup/recall_tool.ts +157 -0
- package/src/editing/patch.ts +482 -0
- package/src/editing/post_edit_verification.ts +200 -0
- package/src/editing/syntax-verify.ts +353 -0
- package/src/index.ts +852 -0
- package/src/lsp/adapters/base.ts +42 -0
- package/src/lsp/adapters/index.ts +21 -0
- package/src/lsp/adapters/python.ts +37 -0
- package/src/lsp/adapters/rust.ts +35 -0
- package/src/lsp/adapters/typescript.ts +40 -0
- package/src/lsp/index.ts +13 -0
- package/src/lsp/lsp_client.ts +650 -0
- package/src/lsp/lsp_detector.ts +52 -0
- package/src/lsp/lsp_formatter.ts +284 -0
- package/src/lsp/lsp_installer.ts +604 -0
- package/src/lsp/lsp_manager.ts +202 -0
- package/src/lsp/lsp_modal.ts +675 -0
- package/src/lsp/lsp_registry.ts +539 -0
- package/src/lsp/lsp_types.ts +143 -0
- package/src/retrieval/ast_search.ts +926 -0
- package/src/retrieval/repomap.ts +779 -0
- package/src/retrieval/search_bm25.ts +190 -0
- package/src/retrieval/search_chunker.ts +251 -0
- package/src/retrieval/search_config.ts +162 -0
- package/src/retrieval/search_embedder.ts +362 -0
- package/src/retrieval/search_index.ts +753 -0
- package/src/retrieval/search_modal.ts +358 -0
- package/src/retrieval/symbol_reader.ts +200 -0
- package/src/safety/atomic_write.ts +26 -0
- package/src/safety/epistemic_guard.ts +733 -0
- package/src/safety/kernel_debug.ts +15 -0
- package/src/safety/output_clamper.ts +213 -0
- package/src/tools/ast_search_tool.ts +131 -0
- package/src/tools/code_search_tool.ts +147 -0
- package/src/tools/context.ts +20 -0
- package/src/tools/edit_tool.ts +288 -0
- package/src/tools/lsp_tool.ts +813 -0
- package/src/tools/read_tool.ts +348 -0
- package/src/tools/repo_map_tool.ts +57 -0
- package/src/ui/footer.ts +287 -0
- package/src/ui/tui_utils.ts +278 -0
|
@@ -0,0 +1,779 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
|
|
4
|
+
export interface SymbolDef {
|
|
5
|
+
name: string;
|
|
6
|
+
kind:
|
|
7
|
+
| "function"
|
|
8
|
+
| "class"
|
|
9
|
+
| "interface"
|
|
10
|
+
| "type"
|
|
11
|
+
| "enum"
|
|
12
|
+
| "variable"
|
|
13
|
+
| "constant"
|
|
14
|
+
| "method"
|
|
15
|
+
| "alias";
|
|
16
|
+
signature: string;
|
|
17
|
+
line: number;
|
|
18
|
+
aliasedFrom?: {
|
|
19
|
+
module?: string;
|
|
20
|
+
originalName: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface FileTags {
|
|
25
|
+
filePath: string;
|
|
26
|
+
definitions: SymbolDef[];
|
|
27
|
+
references: Set<string>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const IGNORED_DIRS = new Set([
|
|
31
|
+
".git",
|
|
32
|
+
"node_modules",
|
|
33
|
+
".venv",
|
|
34
|
+
"venv",
|
|
35
|
+
"__pycache__",
|
|
36
|
+
"dist",
|
|
37
|
+
"build",
|
|
38
|
+
"coverage",
|
|
39
|
+
".pi",
|
|
40
|
+
".hermes",
|
|
41
|
+
".next",
|
|
42
|
+
".turbo",
|
|
43
|
+
".cache",
|
|
44
|
+
"target",
|
|
45
|
+
"vendor",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
const SUPPORTED_EXTENSIONS = new Set([
|
|
49
|
+
".ts",
|
|
50
|
+
".tsx",
|
|
51
|
+
".js",
|
|
52
|
+
".jsx",
|
|
53
|
+
".py",
|
|
54
|
+
".rs",
|
|
55
|
+
".go",
|
|
56
|
+
".c",
|
|
57
|
+
".cpp",
|
|
58
|
+
".h",
|
|
59
|
+
".hpp",
|
|
60
|
+
".java",
|
|
61
|
+
".cs",
|
|
62
|
+
".rb",
|
|
63
|
+
".php",
|
|
64
|
+
".swift",
|
|
65
|
+
".sh",
|
|
66
|
+
".bash",
|
|
67
|
+
]);
|
|
68
|
+
|
|
69
|
+
// Helper to extract balanced signatures across multiple lines
|
|
70
|
+
function extractBalancedSig(
|
|
71
|
+
lines: string[],
|
|
72
|
+
startIdx: number,
|
|
73
|
+
kind:
|
|
74
|
+
| "function"
|
|
75
|
+
| "class"
|
|
76
|
+
| "interface"
|
|
77
|
+
| "arrow"
|
|
78
|
+
| "python_fn"
|
|
79
|
+
| "python_class"
|
|
80
|
+
| "rust_fn"
|
|
81
|
+
| "rust_type"
|
|
82
|
+
| "go_fn"
|
|
83
|
+
| "go_type",
|
|
84
|
+
): string | null {
|
|
85
|
+
let collected = "";
|
|
86
|
+
let parenDepth = 0;
|
|
87
|
+
let braceDepth = 0;
|
|
88
|
+
let angleDepth = 0;
|
|
89
|
+
let seenOpenParen = false;
|
|
90
|
+
let seenArrow = false;
|
|
91
|
+
|
|
92
|
+
for (let j = startIdx; j < Math.min(lines.length, startIdx + 20); j++) {
|
|
93
|
+
const raw = lines[j];
|
|
94
|
+
const trimmed = raw.trim();
|
|
95
|
+
if (
|
|
96
|
+
!trimmed ||
|
|
97
|
+
(j > startIdx &&
|
|
98
|
+
(trimmed.startsWith("//") ||
|
|
99
|
+
trimmed.startsWith("#") ||
|
|
100
|
+
trimmed.startsWith("/*") ||
|
|
101
|
+
trimmed.startsWith("*")))
|
|
102
|
+
)
|
|
103
|
+
continue;
|
|
104
|
+
|
|
105
|
+
for (let c = 0; c < trimmed.length; c++) {
|
|
106
|
+
const ch = trimmed[c];
|
|
107
|
+
const nextCh = trimmed[c + 1];
|
|
108
|
+
|
|
109
|
+
if (ch === "(") {
|
|
110
|
+
parenDepth++;
|
|
111
|
+
seenOpenParen = true;
|
|
112
|
+
} else if (ch === ")") {
|
|
113
|
+
if (parenDepth > 0) parenDepth--;
|
|
114
|
+
} else if (ch === "<") {
|
|
115
|
+
angleDepth++;
|
|
116
|
+
} else if (ch === ">") {
|
|
117
|
+
if (angleDepth > 0) angleDepth--;
|
|
118
|
+
} else if (ch === "{") {
|
|
119
|
+
if (
|
|
120
|
+
kind === "class" ||
|
|
121
|
+
kind === "interface" ||
|
|
122
|
+
kind === "rust_type" ||
|
|
123
|
+
kind === "go_type"
|
|
124
|
+
) {
|
|
125
|
+
if (angleDepth === 0 && parenDepth === 0) {
|
|
126
|
+
collected += (collected ? " " : "") + trimmed.slice(0, c);
|
|
127
|
+
return collected.replace(/\s+/g, " ").trim();
|
|
128
|
+
}
|
|
129
|
+
} else if (kind === "function" || kind === "rust_fn" || kind === "go_fn") {
|
|
130
|
+
if (parenDepth === 0 && angleDepth === 0) {
|
|
131
|
+
collected += (collected ? " " : "") + trimmed.slice(0, c);
|
|
132
|
+
return collected.replace(/\s+/g, " ").trim();
|
|
133
|
+
}
|
|
134
|
+
} else if (kind === "arrow") {
|
|
135
|
+
if (seenArrow && parenDepth === 0 && angleDepth === 0) {
|
|
136
|
+
collected += (collected ? " " : "") + trimmed.slice(0, c);
|
|
137
|
+
return collected.replace(/\s+/g, " ").trim();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
braceDepth++;
|
|
141
|
+
} else if (ch === "}") {
|
|
142
|
+
if (braceDepth > 0) braceDepth--;
|
|
143
|
+
} else if (ch === "=" && nextCh === ">") {
|
|
144
|
+
seenArrow = true;
|
|
145
|
+
} else if (ch === ";") {
|
|
146
|
+
if (parenDepth === 0 && angleDepth === 0 && braceDepth === 0) {
|
|
147
|
+
if (kind === "arrow") {
|
|
148
|
+
if (!seenArrow && !collected.includes("=>")) return null;
|
|
149
|
+
collected += (collected ? " " : "") + trimmed.slice(0, c);
|
|
150
|
+
return collected.replace(/\s+/g, " ").trim();
|
|
151
|
+
}
|
|
152
|
+
if (
|
|
153
|
+
kind === "function" ||
|
|
154
|
+
kind === "rust_fn" ||
|
|
155
|
+
kind === "rust_type" ||
|
|
156
|
+
kind === "go_fn" ||
|
|
157
|
+
kind === "go_type" ||
|
|
158
|
+
kind === "class" ||
|
|
159
|
+
kind === "interface"
|
|
160
|
+
) {
|
|
161
|
+
collected += (collected ? " " : "") + trimmed.slice(0, c);
|
|
162
|
+
return collected.replace(/\s+/g, " ").trim();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
} else if (ch === ":" && (kind === "python_fn" || kind === "python_class")) {
|
|
166
|
+
if (parenDepth === 0 && angleDepth === 0 && braceDepth === 0) {
|
|
167
|
+
collected += (collected ? " " : "") + trimmed.slice(0, c);
|
|
168
|
+
return collected.replace(/\s+/g, " ").trim();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (kind === "arrow" && trimmed.includes(";")) {
|
|
174
|
+
if (!seenArrow && !collected.includes("=>")) return null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
collected += (collected ? " " : "") + trimmed;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Fast AST & Signature tag extractor with multi-line support
|
|
184
|
+
export function extractFileTags(filePath: string, content: string): FileTags {
|
|
185
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
186
|
+
const lines = content.split("\n");
|
|
187
|
+
const definitions: SymbolDef[] = [];
|
|
188
|
+
const references = new Set<string>();
|
|
189
|
+
|
|
190
|
+
// Extract identifier references (words matching [a-zA-Z_][a-zA-Z0-9_]*)
|
|
191
|
+
const idRegex = /\b([a-zA-Z_][a-zA-Z0-9_]{2,})\b/g;
|
|
192
|
+
let match: RegExpExecArray | null;
|
|
193
|
+
while ((match = idRegex.exec(content)) !== null) {
|
|
194
|
+
references.add(match[1]);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
for (let i = 0; i < lines.length; i++) {
|
|
198
|
+
const rawLine = lines[i];
|
|
199
|
+
const line = rawLine.trim();
|
|
200
|
+
if (
|
|
201
|
+
!line ||
|
|
202
|
+
line.startsWith("//") ||
|
|
203
|
+
line.startsWith("#") ||
|
|
204
|
+
line.startsWith("/*") ||
|
|
205
|
+
line.startsWith("*")
|
|
206
|
+
) {
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// TypeScript / JavaScript
|
|
211
|
+
if (ext === ".ts" || ext === ".tsx" || ext === ".js" || ext === ".jsx") {
|
|
212
|
+
// function
|
|
213
|
+
const fnStart = line.match(
|
|
214
|
+
/^(?:export\s+)?(?:async\s+)?function\s+([a-zA-Z0-9_]+)/,
|
|
215
|
+
);
|
|
216
|
+
if (fnStart) {
|
|
217
|
+
const fullSig =
|
|
218
|
+
extractBalancedSig(lines, i, "function") || line.split("{")[0].trim();
|
|
219
|
+
definitions.push({
|
|
220
|
+
name: fnStart[1],
|
|
221
|
+
kind: "function",
|
|
222
|
+
signature: fullSig.replace(/^export\s+/, ""),
|
|
223
|
+
line: i + 1,
|
|
224
|
+
});
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// class
|
|
229
|
+
const classStart = line.match(
|
|
230
|
+
/^(?:export\s+)?(?:abstract\s+)?class\s+([a-zA-Z0-9_]+)/,
|
|
231
|
+
);
|
|
232
|
+
if (classStart) {
|
|
233
|
+
const fullSig =
|
|
234
|
+
extractBalancedSig(lines, i, "class") || line.split("{")[0].trim();
|
|
235
|
+
definitions.push({
|
|
236
|
+
name: classStart[1],
|
|
237
|
+
kind: "class",
|
|
238
|
+
signature: fullSig.replace(/^export\s+/, ""),
|
|
239
|
+
line: i + 1,
|
|
240
|
+
});
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// interface
|
|
245
|
+
const ifaceStart = line.match(/^(?:export\s+)?interface\s+([a-zA-Z0-9_]+)/);
|
|
246
|
+
if (ifaceStart) {
|
|
247
|
+
const fullSig =
|
|
248
|
+
extractBalancedSig(lines, i, "interface") || line.split("{")[0].trim();
|
|
249
|
+
definitions.push({
|
|
250
|
+
name: ifaceStart[1],
|
|
251
|
+
kind: "interface",
|
|
252
|
+
signature: fullSig.replace(/^export\s+/, ""),
|
|
253
|
+
line: i + 1,
|
|
254
|
+
});
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// type alias
|
|
259
|
+
const typeMatch = line.match(
|
|
260
|
+
/^(?:export\s+)?type\s+([a-zA-Z0-9_]+)(?:<[^>]+>)?\s*=/,
|
|
261
|
+
);
|
|
262
|
+
if (typeMatch) {
|
|
263
|
+
definitions.push({
|
|
264
|
+
name: typeMatch[1],
|
|
265
|
+
kind: "type",
|
|
266
|
+
signature: `type ${typeMatch[1]}`,
|
|
267
|
+
line: i + 1,
|
|
268
|
+
});
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// const arrow function
|
|
273
|
+
const constFnMatch = line.match(
|
|
274
|
+
/^(?:export\s+)?const\s+([a-zA-Z0-9_]+)\s*=\s*(?:async\s*)?\(/,
|
|
275
|
+
);
|
|
276
|
+
if (constFnMatch) {
|
|
277
|
+
const fullSig = extractBalancedSig(lines, i, "arrow");
|
|
278
|
+
if (fullSig && fullSig.includes("=>")) {
|
|
279
|
+
definitions.push({
|
|
280
|
+
name: constFnMatch[1],
|
|
281
|
+
kind: "function",
|
|
282
|
+
signature: fullSig.replace(/^export\s+/, ""),
|
|
283
|
+
line: i + 1,
|
|
284
|
+
});
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// const variable / constant declaration: const FOO = ... or const FOO: string = ...
|
|
290
|
+
const constDecl = line.match(
|
|
291
|
+
/^(?:export\s+)?const\s+([a-zA-Z0-9_]+)(?:\s*:\s*([^=]+))?\s*=/,
|
|
292
|
+
);
|
|
293
|
+
if (constDecl) {
|
|
294
|
+
const isAllUpper = /^[A-Z0-9_]{2,}$/.test(constDecl[1]);
|
|
295
|
+
definitions.push({
|
|
296
|
+
name: constDecl[1],
|
|
297
|
+
kind: isAllUpper ? "constant" : "variable",
|
|
298
|
+
signature: line.replace(/;$/, "").trim(),
|
|
299
|
+
line: i + 1,
|
|
300
|
+
});
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// TS/JS Aliased re-exports and imports: import/export { a as b } from "./mod"
|
|
305
|
+
if (
|
|
306
|
+
(line.startsWith("import") || line.startsWith("export")) &&
|
|
307
|
+
line.includes("{")
|
|
308
|
+
) {
|
|
309
|
+
let importBlock = line;
|
|
310
|
+
if (!line.includes("}")) {
|
|
311
|
+
const collected: string[] = [];
|
|
312
|
+
for (let j = i; j < Math.min(lines.length, i + 10); j++) {
|
|
313
|
+
collected.push(lines[j].trim());
|
|
314
|
+
if (lines[j].includes("}")) break;
|
|
315
|
+
}
|
|
316
|
+
importBlock = collected.join(" ");
|
|
317
|
+
}
|
|
318
|
+
const modMatch = importBlock.match(/from\s*['"]([^'"]+)['"]/);
|
|
319
|
+
const mod = modMatch ? modMatch[1] : undefined;
|
|
320
|
+
const innerMatch = importBlock.match(/\{([^}]+)\}/);
|
|
321
|
+
if (innerMatch) {
|
|
322
|
+
const rawItems = innerMatch[1].split(",");
|
|
323
|
+
for (const item of rawItems) {
|
|
324
|
+
const trimmed = item.trim();
|
|
325
|
+
const asMatch = trimmed.match(/^([a-zA-Z0-9_]+)\s+as\s+([a-zA-Z0-9_]+)$/);
|
|
326
|
+
if (asMatch) {
|
|
327
|
+
definitions.push({
|
|
328
|
+
name: asMatch[2],
|
|
329
|
+
kind: "alias",
|
|
330
|
+
signature: `import { ${asMatch[1]} as ${asMatch[2]} } from "${mod || ""}"`,
|
|
331
|
+
line: i + 1,
|
|
332
|
+
aliasedFrom: { module: mod, originalName: asMatch[1] },
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// TS/JS import/export * as alias
|
|
340
|
+
const starAsMatch = line.match(
|
|
341
|
+
/^(?:export|import)\s*\*\s*as\s+([a-zA-Z0-9_]+)\s*from\s*['"]([^'"]+)['"]/,
|
|
342
|
+
);
|
|
343
|
+
if (starAsMatch) {
|
|
344
|
+
definitions.push({
|
|
345
|
+
name: starAsMatch[1],
|
|
346
|
+
kind: "alias",
|
|
347
|
+
signature: line,
|
|
348
|
+
line: i + 1,
|
|
349
|
+
aliasedFrom: { module: starAsMatch[2], originalName: starAsMatch[1] },
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Python
|
|
355
|
+
if (ext === ".py") {
|
|
356
|
+
// def (with multi-line signature support)
|
|
357
|
+
const pyFnStart = line.match(/^(?:async\s+)?def\s+([a-zA-Z0-9_]+)/);
|
|
358
|
+
if (pyFnStart) {
|
|
359
|
+
const isMethod = rawLine.startsWith(" ") || rawLine.startsWith("\t");
|
|
360
|
+
const sig = (extractBalancedSig(lines, i, "python_fn") || line)
|
|
361
|
+
.replace(/:$/, "")
|
|
362
|
+
.trim();
|
|
363
|
+
definitions.push({
|
|
364
|
+
name: pyFnStart[1],
|
|
365
|
+
kind: isMethod ? "method" : "function",
|
|
366
|
+
signature: sig,
|
|
367
|
+
line: i + 1,
|
|
368
|
+
});
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// class (with multi-line base class support)
|
|
373
|
+
const pyClassStart = line.match(/^class\s+([a-zA-Z0-9_]+)/);
|
|
374
|
+
if (pyClassStart) {
|
|
375
|
+
const sig = (extractBalancedSig(lines, i, "python_class") || line)
|
|
376
|
+
.replace(/:$/, "")
|
|
377
|
+
.trim();
|
|
378
|
+
definitions.push({
|
|
379
|
+
name: pyClassStart[1],
|
|
380
|
+
kind: "class",
|
|
381
|
+
signature: sig,
|
|
382
|
+
line: i + 1,
|
|
383
|
+
});
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Python import with alias: `from .mod import ( ... )` or `from .mod import a as b`
|
|
388
|
+
if (line.startsWith("from ") && line.includes("import")) {
|
|
389
|
+
let importBlock = line;
|
|
390
|
+
if (line.includes("(") && !line.includes(")")) {
|
|
391
|
+
const collected: string[] = [];
|
|
392
|
+
for (let j = i; j < Math.min(lines.length, i + 10); j++) {
|
|
393
|
+
collected.push(lines[j].trim());
|
|
394
|
+
if (lines[j].includes(")")) break;
|
|
395
|
+
}
|
|
396
|
+
importBlock = collected.join(" ");
|
|
397
|
+
}
|
|
398
|
+
const fromModMatch = importBlock.match(
|
|
399
|
+
/^from\s+([.\w]+)\s+import\s+(?:\(([^)]+)\)|(.+))$/,
|
|
400
|
+
);
|
|
401
|
+
if (fromModMatch) {
|
|
402
|
+
const mod = fromModMatch[1];
|
|
403
|
+
const rawItems = (fromModMatch[2] || fromModMatch[3] || "").split(",");
|
|
404
|
+
for (const item of rawItems) {
|
|
405
|
+
const trimmed = item.trim();
|
|
406
|
+
if (!trimmed) continue;
|
|
407
|
+
const asMatch = trimmed.match(/^([a-zA-Z0-9_]+)\s+as\s+([a-zA-Z0-9_]+)$/);
|
|
408
|
+
if (asMatch) {
|
|
409
|
+
definitions.push({
|
|
410
|
+
name: asMatch[2],
|
|
411
|
+
kind: "alias",
|
|
412
|
+
signature: `from ${mod} import ${asMatch[1]} as ${asMatch[2]}`,
|
|
413
|
+
line: i + 1,
|
|
414
|
+
aliasedFrom: { module: mod, originalName: asMatch[1] },
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Python module alias: import websocket as ws
|
|
422
|
+
const pyImportAs = line.match(/^import\s+([.\w]+)\s+as\s+([a-zA-Z0-9_]+)/);
|
|
423
|
+
if (pyImportAs) {
|
|
424
|
+
definitions.push({
|
|
425
|
+
name: pyImportAs[2],
|
|
426
|
+
kind: "alias",
|
|
427
|
+
signature: line,
|
|
428
|
+
line: i + 1,
|
|
429
|
+
aliasedFrom: { module: pyImportAs[1], originalName: pyImportAs[1] },
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Python top-level assignment alias: WebSocketClient = client
|
|
434
|
+
const pyAssignAlias = line.match(/^([a-zA-Z0-9_]+)\s*=\s*([a-zA-Z0-9_]+)$/);
|
|
435
|
+
if (
|
|
436
|
+
pyAssignAlias &&
|
|
437
|
+
!rawLine.startsWith(" ") &&
|
|
438
|
+
!rawLine.startsWith("\t") &&
|
|
439
|
+
!pyAssignAlias[1].startsWith("__")
|
|
440
|
+
) {
|
|
441
|
+
definitions.push({
|
|
442
|
+
name: pyAssignAlias[1],
|
|
443
|
+
kind: "alias",
|
|
444
|
+
signature: line,
|
|
445
|
+
line: i + 1,
|
|
446
|
+
aliasedFrom: { originalName: pyAssignAlias[2] },
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// Python top-level constant / variable assignment (e.g. TIMEOUT = 30 or MAX_RETRIES: int = 5)
|
|
451
|
+
const pyConstMatch = line.match(/^([A-Z0-9_]{2,})(?:\s*:\s*([^=]+))?\s*=/);
|
|
452
|
+
if (
|
|
453
|
+
pyConstMatch &&
|
|
454
|
+
!rawLine.startsWith(" ") &&
|
|
455
|
+
!rawLine.startsWith("\t") &&
|
|
456
|
+
!pyConstMatch[1].startsWith("__")
|
|
457
|
+
) {
|
|
458
|
+
definitions.push({
|
|
459
|
+
name: pyConstMatch[1],
|
|
460
|
+
kind: "constant",
|
|
461
|
+
signature: line.trim(),
|
|
462
|
+
line: i + 1,
|
|
463
|
+
});
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// Rust
|
|
469
|
+
if (ext === ".rs") {
|
|
470
|
+
// fn / async fn / method
|
|
471
|
+
const rsFn = line.match(
|
|
472
|
+
/^(?:pub(?:\([^)]+\))?\s+)?(?:async\s+)?(?:unsafe\s+)?fn\s+([a-zA-Z0-9_]+)/,
|
|
473
|
+
);
|
|
474
|
+
if (rsFn) {
|
|
475
|
+
const isMethod = rawLine.startsWith(" ") || rawLine.startsWith("\t");
|
|
476
|
+
const sig =
|
|
477
|
+
extractBalancedSig(lines, i, "rust_fn") || line.split("{")[0].trim();
|
|
478
|
+
definitions.push({
|
|
479
|
+
name: rsFn[1],
|
|
480
|
+
kind: isMethod ? "method" : "function",
|
|
481
|
+
signature: sig,
|
|
482
|
+
line: i + 1,
|
|
483
|
+
});
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// struct / enum / trait
|
|
488
|
+
const rsStruct = line.match(
|
|
489
|
+
/^(?:pub(?:\([^)]+\))?\s+)?(struct|enum|trait)\s+([a-zA-Z0-9_]+)/,
|
|
490
|
+
);
|
|
491
|
+
if (rsStruct) {
|
|
492
|
+
const sig =
|
|
493
|
+
extractBalancedSig(lines, i, "rust_type") || line.split("{")[0].trim();
|
|
494
|
+
definitions.push({
|
|
495
|
+
name: rsStruct[2],
|
|
496
|
+
kind: rsStruct[1] as "class",
|
|
497
|
+
signature: sig,
|
|
498
|
+
line: i + 1,
|
|
499
|
+
});
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// static / const
|
|
504
|
+
const rsStatic = line.match(
|
|
505
|
+
/^(?:pub(?:\([^)]+\))?\s+)?(static|const)\s+(?:mut\s+)?([a-zA-Z0-9_]+)\s*:\s*([^=;]+)/,
|
|
506
|
+
);
|
|
507
|
+
if (rsStatic) {
|
|
508
|
+
const kind = rsStatic[1] === "const" ? "constant" : "variable";
|
|
509
|
+
definitions.push({
|
|
510
|
+
name: rsStatic[2],
|
|
511
|
+
kind,
|
|
512
|
+
signature: line.replace(/;$/, "").trim(),
|
|
513
|
+
line: i + 1,
|
|
514
|
+
});
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// type alias
|
|
519
|
+
const rsType = line.match(
|
|
520
|
+
/^(?:pub(?:\([^)]+\))?\s+)?type\s+([a-zA-Z0-9_]+)(?:<[^>]+>)?\s*=/,
|
|
521
|
+
);
|
|
522
|
+
if (rsType) {
|
|
523
|
+
definitions.push({
|
|
524
|
+
name: rsType[1],
|
|
525
|
+
kind: "type",
|
|
526
|
+
signature: line.replace(/;$/, "").trim(),
|
|
527
|
+
line: i + 1,
|
|
528
|
+
});
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// Go
|
|
534
|
+
if (ext === ".go") {
|
|
535
|
+
const goFn = line.match(/^func\s+(?:\([^)]+\)\s+)?([a-zA-Z0-9_]+)/);
|
|
536
|
+
if (goFn) {
|
|
537
|
+
const sig =
|
|
538
|
+
extractBalancedSig(lines, i, "go_fn") || line.split("{")[0].trim();
|
|
539
|
+
definitions.push({
|
|
540
|
+
name: goFn[1],
|
|
541
|
+
kind: "function",
|
|
542
|
+
signature: sig,
|
|
543
|
+
line: i + 1,
|
|
544
|
+
});
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
const goType = line.match(/^type\s+([a-zA-Z0-9_]+)\s+(struct|interface)/);
|
|
549
|
+
if (goType) {
|
|
550
|
+
const sig =
|
|
551
|
+
extractBalancedSig(lines, i, "go_type") ||
|
|
552
|
+
`type ${goType[1]} ${goType[2]}`;
|
|
553
|
+
definitions.push({
|
|
554
|
+
name: goType[1],
|
|
555
|
+
kind: "class",
|
|
556
|
+
signature: sig,
|
|
557
|
+
line: i + 1,
|
|
558
|
+
});
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// const / var
|
|
563
|
+
const goConst = line.match(/^(?:const|var)\s+([a-zA-Z0-9_]+)(?:\s+([^=]+))?\s*=/);
|
|
564
|
+
if (goConst) {
|
|
565
|
+
const isConst = line.startsWith("const");
|
|
566
|
+
definitions.push({
|
|
567
|
+
name: goConst[1],
|
|
568
|
+
kind: isConst ? "constant" : "variable",
|
|
569
|
+
signature: line.trim(),
|
|
570
|
+
line: i + 1,
|
|
571
|
+
});
|
|
572
|
+
continue;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
return {
|
|
578
|
+
filePath,
|
|
579
|
+
definitions,
|
|
580
|
+
references,
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// Recursively find all supported code files in a directory
|
|
585
|
+
export function findSourceFiles(rootDir: string, maxFiles = 300): string[] {
|
|
586
|
+
const results: string[] = [];
|
|
587
|
+
|
|
588
|
+
function scan(dir: string) {
|
|
589
|
+
if (results.length >= maxFiles) return;
|
|
590
|
+
let entries: fs.Dirent[];
|
|
591
|
+
try {
|
|
592
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
593
|
+
} catch {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
for (const entry of entries) {
|
|
598
|
+
if (entry.name.startsWith(".") && entry.name !== ".github") continue;
|
|
599
|
+
if (IGNORED_DIRS.has(entry.name)) continue;
|
|
600
|
+
|
|
601
|
+
const fullPath = path.join(dir, entry.name);
|
|
602
|
+
if (entry.isDirectory()) {
|
|
603
|
+
scan(fullPath);
|
|
604
|
+
} else if (entry.isFile()) {
|
|
605
|
+
const ext = path.extname(entry.name).toLowerCase();
|
|
606
|
+
if (SUPPORTED_EXTENSIONS.has(ext)) {
|
|
607
|
+
results.push(fullPath);
|
|
608
|
+
if (results.length >= maxFiles) return;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
scan(rootDir);
|
|
615
|
+
return results;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// Classify a file path into a PageRank demotion factor. Test suites form
|
|
619
|
+
// dense mutual-reference clusters that otherwise dominate raw PageRank —
|
|
620
|
+
// mirrors the test-path penalties in ast_search.computeSymbolRankScore.
|
|
621
|
+
function getPathDemotion(relPath: string): number {
|
|
622
|
+
const p = relPath.replace(/\\/g, "/").toLowerCase();
|
|
623
|
+
const isTestPath =
|
|
624
|
+
p.includes("/test/") ||
|
|
625
|
+
p.includes("/tests/") ||
|
|
626
|
+
p.includes("/__tests__/") ||
|
|
627
|
+
p.includes("/spec/") ||
|
|
628
|
+
p.includes("/specs/") ||
|
|
629
|
+
p.startsWith("test/") ||
|
|
630
|
+
p.startsWith("tests/") ||
|
|
631
|
+
p.startsWith("spec/") ||
|
|
632
|
+
p.includes("test_") ||
|
|
633
|
+
p.includes("_test.") ||
|
|
634
|
+
p.includes(".test.") ||
|
|
635
|
+
p.includes(".spec.");
|
|
636
|
+
if (isTestPath) return 0.25; // strong demotion
|
|
637
|
+
const isBenchmarkOrExample =
|
|
638
|
+
p.includes("/benchmarks/") ||
|
|
639
|
+
p.includes("/benchmark/") ||
|
|
640
|
+
p.includes("/examples/") ||
|
|
641
|
+
p.includes("/example/") ||
|
|
642
|
+
p.includes("/samples/") ||
|
|
643
|
+
p.includes("/fixtures/") ||
|
|
644
|
+
p.startsWith("benchmarks/") ||
|
|
645
|
+
p.startsWith("examples/") ||
|
|
646
|
+
p.startsWith("samples/");
|
|
647
|
+
if (isBenchmarkOrExample) return 0.6; // moderate demotion
|
|
648
|
+
return 1;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
// Compute Graph PageRank for files and their symbols
|
|
652
|
+
export function computeRepoMap(rootDir: string, tokenBudget = 1024): string {
|
|
653
|
+
const sourceFiles = findSourceFiles(rootDir);
|
|
654
|
+
if (sourceFiles.length === 0) {
|
|
655
|
+
return "(No supported source files found in repository)";
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
const fileTagsMap = new Map<string, FileTags>();
|
|
659
|
+
const defToFile = new Map<string, Set<string>>();
|
|
660
|
+
|
|
661
|
+
for (const file of sourceFiles) {
|
|
662
|
+
try {
|
|
663
|
+
const content = fs.readFileSync(file, "utf8");
|
|
664
|
+
const tags = extractFileTags(file, content);
|
|
665
|
+
fileTagsMap.set(file, tags);
|
|
666
|
+
|
|
667
|
+
for (const def of tags.definitions) {
|
|
668
|
+
if (!defToFile.has(def.name)) {
|
|
669
|
+
defToFile.set(def.name, new Set());
|
|
670
|
+
}
|
|
671
|
+
defToFile.get(def.name)!.add(file);
|
|
672
|
+
}
|
|
673
|
+
} catch {
|
|
674
|
+
// Skip unreadable files
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// Build adjacency graph between files
|
|
679
|
+
const files = Array.from(fileTagsMap.keys());
|
|
680
|
+
const N = files.length;
|
|
681
|
+
if (N === 0) return "(Empty repository)";
|
|
682
|
+
|
|
683
|
+
const fileIndex = new Map<string, number>();
|
|
684
|
+
files.forEach((f, idx) => fileIndex.set(f, idx));
|
|
685
|
+
|
|
686
|
+
const inEdges: number[][] = Array.from({ length: N }, () => []);
|
|
687
|
+
const outDegree = new Array(N).fill(0);
|
|
688
|
+
|
|
689
|
+
for (let i = 0; i < N; i++) {
|
|
690
|
+
const fileA = files[i];
|
|
691
|
+
const tagsA = fileTagsMap.get(fileA)!;
|
|
692
|
+
const referencedFiles = new Set<number>();
|
|
693
|
+
|
|
694
|
+
for (const ref of tagsA.references) {
|
|
695
|
+
const targetFiles = defToFile.get(ref);
|
|
696
|
+
if (targetFiles) {
|
|
697
|
+
for (const targetFile of targetFiles) {
|
|
698
|
+
const targetIdx = fileIndex.get(targetFile);
|
|
699
|
+
if (targetIdx !== undefined && targetIdx !== i) {
|
|
700
|
+
referencedFiles.add(targetIdx);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
outDegree[i] = referencedFiles.size;
|
|
707
|
+
for (const targetIdx of referencedFiles) {
|
|
708
|
+
inEdges[targetIdx].push(i);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// PageRank iteration (d = 0.85)
|
|
713
|
+
const d = 0.85;
|
|
714
|
+
let rank = new Array(N).fill(1 / N);
|
|
715
|
+
|
|
716
|
+
for (let iter = 0; iter < 15; iter++) {
|
|
717
|
+
const nextRank = new Array(N).fill((1 - d) / N);
|
|
718
|
+
for (let i = 0; i < N; i++) {
|
|
719
|
+
let sum = 0;
|
|
720
|
+
for (const src of inEdges[i]) {
|
|
721
|
+
if (outDegree[src] > 0) {
|
|
722
|
+
sum += rank[src] / outDegree[src];
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
nextRank[i] += d * sum;
|
|
726
|
+
}
|
|
727
|
+
rank = nextRank;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// Sort files by effective rank descending (raw PageRank demoted for
|
|
731
|
+
// test/benchmark/example paths so production code leads the map).
|
|
732
|
+
const rankedFiles = files
|
|
733
|
+
.map((f, idx) => ({
|
|
734
|
+
file: f,
|
|
735
|
+
effectiveRank: rank[idx] * getPathDemotion(path.relative(rootDir, f)),
|
|
736
|
+
tags: fileTagsMap.get(f)!,
|
|
737
|
+
}))
|
|
738
|
+
.sort((a, b) => b.effectiveRank - a.effectiveRank);
|
|
739
|
+
|
|
740
|
+
// Pack symbols into token budget (~4 chars per token)
|
|
741
|
+
const charBudget = tokenBudget * 4;
|
|
742
|
+
let currentChars = 0;
|
|
743
|
+
const lines: string[] = [];
|
|
744
|
+
|
|
745
|
+
lines.push("Repository Map (Tree-Sitter AST & PageRank Ranked):");
|
|
746
|
+
|
|
747
|
+
for (const item of rankedFiles) {
|
|
748
|
+
const relPath = path.relative(rootDir, item.file).replace(/\\/g, "/");
|
|
749
|
+
const fileHeader = `\n${relPath}:`;
|
|
750
|
+
if (currentChars + fileHeader.length > charBudget) break;
|
|
751
|
+
|
|
752
|
+
const defs = item.tags.definitions.filter((d) => d.kind !== "alias");
|
|
753
|
+
if (defs.length === 0) continue;
|
|
754
|
+
|
|
755
|
+
lines.push(fileHeader);
|
|
756
|
+
currentChars += fileHeader.length;
|
|
757
|
+
|
|
758
|
+
for (const def of defs) {
|
|
759
|
+
const cleanSig = def.signature.replace(/\s+/g, " ").trim();
|
|
760
|
+
const sigLine = ` ${cleanSig}`;
|
|
761
|
+
if (currentChars + sigLine.length > charBudget) {
|
|
762
|
+
break;
|
|
763
|
+
}
|
|
764
|
+
lines.push(sigLine);
|
|
765
|
+
currentChars += sigLine.length;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// A bare header with nothing under it gives the caller zero guidance —
|
|
770
|
+
// say why the map is empty and how to get useful output.
|
|
771
|
+
if (lines.length === 1) {
|
|
772
|
+
lines.push("");
|
|
773
|
+
lines.push(
|
|
774
|
+
`[No symbols fit within budget_tokens=${tokenBudget} (~${charBudget} chars). Increase 'budget_tokens' (e.g. 1024+) to see the ranked symbol map.]`,
|
|
775
|
+
);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
return lines.join("\n");
|
|
779
|
+
}
|