@prubbs88/codebase-cartographer 0.0.0-stage → 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 +21 -0
- package/README.md +106 -2
- package/dist/ask.d.ts +20 -0
- package/dist/ask.js +93 -0
- package/dist/ask.js.map +1 -0
- package/dist/graph/analysis.d.ts +2 -0
- package/dist/graph/analysis.js +8 -0
- package/dist/graph/analysis.js.map +1 -0
- package/dist/graph/rank.d.ts +9 -0
- package/dist/graph/rank.js +173 -0
- package/dist/graph/rank.js.map +1 -0
- package/dist/graph/resolve.d.ts +11 -0
- package/dist/graph/resolve.js +299 -0
- package/dist/graph/resolve.js.map +1 -0
- package/dist/graph/serialize.d.ts +16 -0
- package/dist/graph/serialize.js +38 -0
- package/dist/graph/serialize.js.map +1 -0
- package/dist/graph/types.d.ts +64 -0
- package/dist/graph/types.js +2 -0
- package/dist/graph/types.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +101 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/client.d.ts +13 -0
- package/dist/llm/client.js +46 -0
- package/dist/llm/client.js.map +1 -0
- package/dist/output/map.d.ts +2 -0
- package/dist/output/map.js +38 -0
- package/dist/output/map.js.map +1 -0
- package/dist/output/report.d.ts +22 -0
- package/dist/output/report.js +198 -0
- package/dist/output/report.js.map +1 -0
- package/dist/output/stats.d.ts +2 -0
- package/dist/output/stats.js +51 -0
- package/dist/output/stats.js.map +1 -0
- package/dist/parser/python.d.ts +2 -0
- package/dist/parser/python.js +366 -0
- package/dist/parser/python.js.map +1 -0
- package/dist/retrieval/chunk.d.ts +16 -0
- package/dist/retrieval/chunk.js +88 -0
- package/dist/retrieval/chunk.js.map +1 -0
- package/dist/retrieval/local-embed.d.ts +4 -0
- package/dist/retrieval/local-embed.js +53 -0
- package/dist/retrieval/local-embed.js.map +1 -0
- package/dist/retrieval/retrieve.d.ts +12 -0
- package/dist/retrieval/retrieve.js +85 -0
- package/dist/retrieval/retrieve.js.map +1 -0
- package/dist/scan.d.ts +2 -0
- package/dist/scan.js +46 -0
- package/dist/scan.js.map +1 -0
- package/dist/utils/module-name.d.ts +2 -0
- package/dist/utils/module-name.js +14 -0
- package/dist/utils/module-name.js.map +1 -0
- package/docs/benchmark.md +68 -0
- package/package.json +63 -4
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import Parser from "tree-sitter";
|
|
2
|
+
import Python from "tree-sitter-python";
|
|
3
|
+
import { toDottedModuleName } from "../utils/module-name.js";
|
|
4
|
+
let parser = null;
|
|
5
|
+
function getParser() {
|
|
6
|
+
if (!parser) {
|
|
7
|
+
parser = new Parser();
|
|
8
|
+
parser.setLanguage(Python);
|
|
9
|
+
}
|
|
10
|
+
return parser;
|
|
11
|
+
}
|
|
12
|
+
export function parsePythonFile(absPath, relPath, source) {
|
|
13
|
+
const tree = getParser().parse(source);
|
|
14
|
+
const moduleName = toDottedModuleName(relPath);
|
|
15
|
+
const moduleSymbolId = `${relPath}::<module>`;
|
|
16
|
+
const moduleSymbol = {
|
|
17
|
+
id: moduleSymbolId,
|
|
18
|
+
name: "<module>",
|
|
19
|
+
qualifiedName: "<module>",
|
|
20
|
+
kind: "module",
|
|
21
|
+
file: relPath,
|
|
22
|
+
startLine: 1,
|
|
23
|
+
endLine: tree.rootNode.endPosition.row + 1,
|
|
24
|
+
decorators: [],
|
|
25
|
+
};
|
|
26
|
+
const ctx = {
|
|
27
|
+
file: relPath,
|
|
28
|
+
symbols: [moduleSymbol],
|
|
29
|
+
calls: [],
|
|
30
|
+
assignments: [],
|
|
31
|
+
imports: [],
|
|
32
|
+
moduleSymbolId,
|
|
33
|
+
classStack: [],
|
|
34
|
+
};
|
|
35
|
+
walkNode(tree.rootNode, ctx);
|
|
36
|
+
collectModuleLevelCalls(tree.rootNode, ctx);
|
|
37
|
+
return {
|
|
38
|
+
path: relPath,
|
|
39
|
+
moduleName,
|
|
40
|
+
isPackage: relPath.endsWith("__init__.py"),
|
|
41
|
+
symbols: ctx.symbols,
|
|
42
|
+
calls: ctx.calls,
|
|
43
|
+
assignments: ctx.assignments,
|
|
44
|
+
imports: ctx.imports,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function walkNode(node, ctx) {
|
|
48
|
+
for (const child of node.namedChildren) {
|
|
49
|
+
switch (child.type) {
|
|
50
|
+
case "decorated_definition":
|
|
51
|
+
walkDecorated(child, ctx);
|
|
52
|
+
break;
|
|
53
|
+
case "function_definition":
|
|
54
|
+
walkFunction(child, [], ctx);
|
|
55
|
+
break;
|
|
56
|
+
case "class_definition":
|
|
57
|
+
walkClass(child, [], ctx);
|
|
58
|
+
break;
|
|
59
|
+
case "import_statement":
|
|
60
|
+
extractImportStatement(child, ctx);
|
|
61
|
+
break;
|
|
62
|
+
case "import_from_statement":
|
|
63
|
+
extractImportFromStatement(child, ctx);
|
|
64
|
+
break;
|
|
65
|
+
default:
|
|
66
|
+
walkNode(child, ctx);
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function walkDecorated(node, ctx) {
|
|
72
|
+
const decoratorNames = [];
|
|
73
|
+
let definition = null;
|
|
74
|
+
for (const child of node.namedChildren) {
|
|
75
|
+
if (child.type === "decorator") {
|
|
76
|
+
decoratorNames.push(cleanDecorator(child.text));
|
|
77
|
+
}
|
|
78
|
+
else if (child.type === "function_definition" ||
|
|
79
|
+
child.type === "class_definition") {
|
|
80
|
+
definition = child;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!definition)
|
|
84
|
+
return;
|
|
85
|
+
if (definition.type === "function_definition") {
|
|
86
|
+
walkFunction(definition, decoratorNames, ctx);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
walkClass(definition, decoratorNames, ctx);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function cleanDecorator(text) {
|
|
93
|
+
return text.replace(/^@\s*/, "").replace(/\s+/g, " ").trim();
|
|
94
|
+
}
|
|
95
|
+
function walkFunction(node, decorators, ctx) {
|
|
96
|
+
const nameNode = node.childForFieldName("name");
|
|
97
|
+
if (!nameNode)
|
|
98
|
+
return;
|
|
99
|
+
const name = nameNode.text;
|
|
100
|
+
const className = ctx.classStack.length > 0 ? ctx.classStack[ctx.classStack.length - 1] : undefined;
|
|
101
|
+
const qualifiedName = className ? `${className}.${name}` : name;
|
|
102
|
+
const id = `${ctx.file}::${qualifiedName}`;
|
|
103
|
+
const symbol = {
|
|
104
|
+
id,
|
|
105
|
+
name,
|
|
106
|
+
qualifiedName,
|
|
107
|
+
kind: className ? "method" : "function",
|
|
108
|
+
file: ctx.file,
|
|
109
|
+
startLine: node.startPosition.row + 1,
|
|
110
|
+
endLine: node.endPosition.row + 1,
|
|
111
|
+
decorators,
|
|
112
|
+
className,
|
|
113
|
+
docstring: extractDocstring(node),
|
|
114
|
+
};
|
|
115
|
+
ctx.symbols.push(symbol);
|
|
116
|
+
const params = node.childForFieldName("parameters");
|
|
117
|
+
if (params)
|
|
118
|
+
collectCalls(params, id, ctx);
|
|
119
|
+
const body = node.childForFieldName("body");
|
|
120
|
+
if (body)
|
|
121
|
+
collectCalls(body, id, ctx);
|
|
122
|
+
walkNestedDefs(node, ctx);
|
|
123
|
+
}
|
|
124
|
+
function walkClass(node, decorators, ctx) {
|
|
125
|
+
const nameNode = node.childForFieldName("name");
|
|
126
|
+
if (!nameNode)
|
|
127
|
+
return;
|
|
128
|
+
const name = nameNode.text;
|
|
129
|
+
const parentClass = ctx.classStack.length > 0 ? ctx.classStack[ctx.classStack.length - 1] : undefined;
|
|
130
|
+
const className = parentClass ? `${parentClass}.${name}` : name;
|
|
131
|
+
const id = `${ctx.file}::${className}`;
|
|
132
|
+
const symbol = {
|
|
133
|
+
id,
|
|
134
|
+
name,
|
|
135
|
+
qualifiedName: className,
|
|
136
|
+
kind: "class",
|
|
137
|
+
file: ctx.file,
|
|
138
|
+
startLine: node.startPosition.row + 1,
|
|
139
|
+
endLine: node.endPosition.row + 1,
|
|
140
|
+
decorators,
|
|
141
|
+
className: parentClass,
|
|
142
|
+
docstring: extractDocstring(node),
|
|
143
|
+
};
|
|
144
|
+
ctx.symbols.push(symbol);
|
|
145
|
+
ctx.classStack.push(className);
|
|
146
|
+
const body = node.childForFieldName("body");
|
|
147
|
+
if (body) {
|
|
148
|
+
for (const child of body.namedChildren) {
|
|
149
|
+
if (child.type === "decorated_definition") {
|
|
150
|
+
walkDecorated(child, ctx);
|
|
151
|
+
}
|
|
152
|
+
else if (child.type === "function_definition") {
|
|
153
|
+
walkFunction(child, [], ctx);
|
|
154
|
+
}
|
|
155
|
+
else if (child.type === "class_definition") {
|
|
156
|
+
walkClass(child, [], ctx);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
ctx.classStack.pop();
|
|
161
|
+
}
|
|
162
|
+
function walkNestedDefs(node, ctx) {
|
|
163
|
+
const body = node.childForFieldName("body");
|
|
164
|
+
if (!body)
|
|
165
|
+
return;
|
|
166
|
+
for (const child of body.namedChildren) {
|
|
167
|
+
if (child.type === "decorated_definition") {
|
|
168
|
+
walkDecorated(child, ctx);
|
|
169
|
+
}
|
|
170
|
+
else if (child.type === "function_definition") {
|
|
171
|
+
walkFunction(child, [], ctx);
|
|
172
|
+
}
|
|
173
|
+
else if (child.type === "class_definition") {
|
|
174
|
+
walkClass(child, [], ctx);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function collectCalls(node, callerId, ctx) {
|
|
179
|
+
for (const child of node.namedChildren) {
|
|
180
|
+
if (child.type === "function_definition" || child.type === "class_definition")
|
|
181
|
+
continue;
|
|
182
|
+
if (child.type === "decorated_definition") {
|
|
183
|
+
const def = child.namedChildren.find((d) => d.type === "function_definition" || d.type === "class_definition");
|
|
184
|
+
if (def)
|
|
185
|
+
collectCalls(def, callerId, ctx);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (child.type === "assignment") {
|
|
189
|
+
const left = child.childForFieldName("left");
|
|
190
|
+
const right = child.childForFieldName("right");
|
|
191
|
+
if (left?.type === "identifier" && right?.type === "call") {
|
|
192
|
+
const fn = right.childForFieldName("function");
|
|
193
|
+
if (fn) {
|
|
194
|
+
const target = flattenCallTarget(fn);
|
|
195
|
+
if (target)
|
|
196
|
+
ctx.assignments.push({ callerId, varName: left.text, target });
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
collectCalls(child, callerId, ctx);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (child.type === "call") {
|
|
203
|
+
const fn = child.childForFieldName("function");
|
|
204
|
+
if (fn) {
|
|
205
|
+
ctx.calls.push({
|
|
206
|
+
callerId,
|
|
207
|
+
target: flattenCallTarget(fn),
|
|
208
|
+
callSiteLine: child.startPosition.row + 1,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const args = child.childForFieldName("arguments");
|
|
212
|
+
if (args)
|
|
213
|
+
collectCalls(args, callerId, ctx);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
collectCalls(child, callerId, ctx);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
function collectModuleLevelCalls(root, ctx) {
|
|
220
|
+
const mainBlocks = [];
|
|
221
|
+
const findMain = (node) => {
|
|
222
|
+
for (const child of node.namedChildren) {
|
|
223
|
+
if (child.type === "if_statement") {
|
|
224
|
+
const cond = child.childForFieldName("condition");
|
|
225
|
+
if (cond && cond.text.includes("__name__") && cond.text.includes("__main__")) {
|
|
226
|
+
const cons = child.childForFieldName("consequence");
|
|
227
|
+
if (cons)
|
|
228
|
+
mainBlocks.push(cons);
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (child.type === "function_definition" ||
|
|
233
|
+
child.type === "class_definition" ||
|
|
234
|
+
child.type === "decorated_definition") {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
findMain(child);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
findMain(root);
|
|
241
|
+
const gather = (node, inMain) => {
|
|
242
|
+
for (const child of node.namedChildren) {
|
|
243
|
+
if (child.type === "call") {
|
|
244
|
+
const fn = child.childForFieldName("function");
|
|
245
|
+
if (fn) {
|
|
246
|
+
ctx.calls.push({
|
|
247
|
+
callerId: ctx.moduleSymbolId,
|
|
248
|
+
target: flattenCallTarget(fn),
|
|
249
|
+
callSiteLine: child.startPosition.row + 1,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
const args = child.childForFieldName("arguments");
|
|
253
|
+
if (args)
|
|
254
|
+
gather(args, inMain);
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (child.type === "function_definition" || child.type === "class_definition")
|
|
258
|
+
continue;
|
|
259
|
+
gather(child, inMain || mainBlocks.some((m) => m.id === child.id));
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
for (const child of root.namedChildren) {
|
|
263
|
+
if (child.type === "function_definition" ||
|
|
264
|
+
child.type === "class_definition" ||
|
|
265
|
+
child.type === "decorated_definition" ||
|
|
266
|
+
child.type === "import_statement" ||
|
|
267
|
+
child.type === "import_from_statement") {
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
gather(child, mainBlocks.some((m) => m.id === child.id));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function flattenCallTarget(fn) {
|
|
274
|
+
if (fn.type === "identifier")
|
|
275
|
+
return fn.text;
|
|
276
|
+
if (fn.type === "attribute") {
|
|
277
|
+
const obj = fn.childForFieldName("object");
|
|
278
|
+
const attr = fn.childForFieldName("attribute");
|
|
279
|
+
if (obj && attr) {
|
|
280
|
+
const base = flattenCallTarget(obj);
|
|
281
|
+
return base ? `${base}.${attr.text}` : attr.text;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return fn.text;
|
|
285
|
+
}
|
|
286
|
+
function extractDocstring(node) {
|
|
287
|
+
const body = node.childForFieldName("body");
|
|
288
|
+
if (!body)
|
|
289
|
+
return undefined;
|
|
290
|
+
const first = body.namedChildren[0];
|
|
291
|
+
if (!first || first.type !== "expression_statement")
|
|
292
|
+
return undefined;
|
|
293
|
+
const expr = first.namedChildren[0];
|
|
294
|
+
if (!expr || (expr.type !== "string" && expr.type !== "concatenated_string"))
|
|
295
|
+
return undefined;
|
|
296
|
+
const text = expr.text;
|
|
297
|
+
if (!text.startsWith('"""') && !text.startsWith("'''"))
|
|
298
|
+
return undefined;
|
|
299
|
+
return text.slice(3, -3).trim();
|
|
300
|
+
}
|
|
301
|
+
function extractImportStatement(node, ctx) {
|
|
302
|
+
for (const child of node.namedChildren) {
|
|
303
|
+
if (child.type === "dotted_name") {
|
|
304
|
+
ctx.imports.push({ kind: "import", module: child.text, alias: child.text, level: 0 });
|
|
305
|
+
}
|
|
306
|
+
else if (child.type === "aliased_import") {
|
|
307
|
+
const nameNode = child.childForFieldName("name");
|
|
308
|
+
const aliasNode = child.childForFieldName("alias");
|
|
309
|
+
if (nameNode) {
|
|
310
|
+
ctx.imports.push({
|
|
311
|
+
kind: "import",
|
|
312
|
+
module: nameNode.text,
|
|
313
|
+
alias: aliasNode?.text ?? nameNode.text,
|
|
314
|
+
level: 0,
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
function extractImportFromStatement(node, ctx) {
|
|
321
|
+
const moduleNode = node.childForFieldName("module_name");
|
|
322
|
+
let moduleText = "";
|
|
323
|
+
let level = 0;
|
|
324
|
+
if (moduleNode) {
|
|
325
|
+
if (moduleNode.type === "relative_import") {
|
|
326
|
+
const dots = moduleNode.text.match(/^\.+/);
|
|
327
|
+
level = dots ? dots[0].length : 1;
|
|
328
|
+
const inner = moduleNode.namedChildren.find((c) => c.type === "dotted_name");
|
|
329
|
+
moduleText = inner?.text ?? "";
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
moduleText = moduleNode.text;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
const rel = node.namedChildren.find((c) => c.type === "relative_import");
|
|
337
|
+
if (rel) {
|
|
338
|
+
const dots = rel.text.match(/^\.+/);
|
|
339
|
+
level = dots ? dots[0].length : 1;
|
|
340
|
+
const inner = rel.namedChildren.find((c) => c.type === "dotted_name");
|
|
341
|
+
moduleText = inner?.text ?? "";
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
for (const child of node.namedChildren) {
|
|
345
|
+
if (child.type === "dotted_name" && child.id !== moduleNode?.id) {
|
|
346
|
+
ctx.imports.push({ kind: "from", module: moduleText, name: child.text, alias: child.text, level });
|
|
347
|
+
}
|
|
348
|
+
else if (child.type === "aliased_import") {
|
|
349
|
+
const nameNode = child.childForFieldName("name");
|
|
350
|
+
const aliasNode = child.childForFieldName("alias");
|
|
351
|
+
if (nameNode) {
|
|
352
|
+
ctx.imports.push({
|
|
353
|
+
kind: "from",
|
|
354
|
+
module: moduleText,
|
|
355
|
+
name: nameNode.text,
|
|
356
|
+
alias: aliasNode?.text ?? nameNode.text,
|
|
357
|
+
level,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
else if (child.type === "wildcard_import") {
|
|
362
|
+
ctx.imports.push({ kind: "from", module: moduleText, alias: "*", level });
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
//# sourceMappingURL=python.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"python.js","sourceRoot":"","sources":["../../src/parser/python.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,MAAM,MAAM,oBAAoB,CAAC;AAExC,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,IAAI,MAAM,GAAkB,IAAI,CAAC;AAEjC,SAAS,SAAS;IAChB,IAAI,CAAC,MAAM,EAAE,CAAC;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AAC,IAAI,CAAC;oBACf,IAAI,EAAE,MAAM;oBACZ,MAAM,EAAE,UAAU;oBAClB,IAAI,EAAE,QAAQ,CAAC,IAAI;oBACnB,KAAK,EAAE,SAAS,EAAE,IAAI,IAAI,QAAQ,CAAC,IAAI;oBACvC,KAAK;iBACN,CAAC,CAAC;YACL,CAAC;QACH,CAAC;aAAM,IAAI,KAAK,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;YAC5C,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;QAC5E,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnalysisResult, Classification } from "../graph/types.js";
|
|
2
|
+
export interface Chunk {
|
|
3
|
+
id: string;
|
|
4
|
+
symbolId: string;
|
|
5
|
+
title: string;
|
|
6
|
+
kind: string;
|
|
7
|
+
classification: Classification;
|
|
8
|
+
file: string;
|
|
9
|
+
startLine: number;
|
|
10
|
+
endLine: number;
|
|
11
|
+
code: string;
|
|
12
|
+
context: string;
|
|
13
|
+
embedding: number[] | null;
|
|
14
|
+
}
|
|
15
|
+
export declare function buildChunks(rootDir: string, analysis: AnalysisResult): Chunk[];
|
|
16
|
+
export declare function chunkToText(chunk: Chunk): string;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export function buildChunks(rootDir, analysis) {
|
|
4
|
+
const fileCache = new Map();
|
|
5
|
+
const readLines = (file) => {
|
|
6
|
+
let lines = fileCache.get(file);
|
|
7
|
+
if (!lines) {
|
|
8
|
+
try {
|
|
9
|
+
lines = fs.readFileSync(path.join(rootDir, file), "utf8").split("\n");
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
lines = [];
|
|
13
|
+
}
|
|
14
|
+
fileCache.set(file, lines);
|
|
15
|
+
}
|
|
16
|
+
return lines;
|
|
17
|
+
};
|
|
18
|
+
const edgesByCaller = new Map();
|
|
19
|
+
const edgesByCallee = new Map();
|
|
20
|
+
for (const edge of analysis.edges) {
|
|
21
|
+
if (!edge.resolved)
|
|
22
|
+
continue;
|
|
23
|
+
const calls = edgesByCaller.get(edge.callerId) ?? [];
|
|
24
|
+
calls.push(edge.calleeId);
|
|
25
|
+
edgesByCaller.set(edge.callerId, calls);
|
|
26
|
+
const callers = edgesByCallee.get(edge.calleeId) ?? [];
|
|
27
|
+
callers.push(edge.callerId);
|
|
28
|
+
edgesByCallee.set(edge.calleeId, callers);
|
|
29
|
+
}
|
|
30
|
+
const label = (id) => {
|
|
31
|
+
const sym = analysis.symbols.get(id);
|
|
32
|
+
return sym ? `${sym.qualifiedName} (${sym.file}:${sym.startLine})` : id;
|
|
33
|
+
};
|
|
34
|
+
const chunks = [];
|
|
35
|
+
for (const ranked of analysis.ranked.values()) {
|
|
36
|
+
if (ranked.classification === "external")
|
|
37
|
+
continue;
|
|
38
|
+
const sym = ranked.symbol;
|
|
39
|
+
if (sym.kind === "module")
|
|
40
|
+
continue;
|
|
41
|
+
const lines = readLines(sym.file);
|
|
42
|
+
const code = lines
|
|
43
|
+
.slice(sym.startLine - 1, Math.min(sym.endLine, sym.startLine + 120))
|
|
44
|
+
.join("\n")
|
|
45
|
+
.trimEnd();
|
|
46
|
+
const contextParts = [];
|
|
47
|
+
if (sym.docstring)
|
|
48
|
+
contextParts.push(`Docstring: ${sym.docstring}`);
|
|
49
|
+
contextParts.push(`Role: ${ranked.classification}`);
|
|
50
|
+
contextParts.push(`Blast radius: ${ranked.directInbound} direct inbound calls, reached from ${ranked.entryPointReachCount} entry point(s)`);
|
|
51
|
+
const callers = [...new Set(edgesByCallee.get(sym.id) ?? [])].slice(0, 8);
|
|
52
|
+
if (callers.length > 0)
|
|
53
|
+
contextParts.push(`Called by: ${callers.map(label).join(", ")}`);
|
|
54
|
+
const callees = [...new Set(edgesByCaller.get(sym.id) ?? [])].slice(0, 10);
|
|
55
|
+
if (callees.length > 0)
|
|
56
|
+
contextParts.push(`Calls: ${callees.map(label).join(", ")}`);
|
|
57
|
+
if (sym.kind === "class") {
|
|
58
|
+
const methods = [...analysis.ranked.values()]
|
|
59
|
+
.filter((r) => r.symbol.className === sym.qualifiedName && r.symbol.file === sym.file && r.symbol.kind === "method")
|
|
60
|
+
.map((r) => `${r.symbol.name} (${r.symbol.startLine})`);
|
|
61
|
+
if (methods.length > 0)
|
|
62
|
+
contextParts.push(`Methods: ${methods.join(", ")}`);
|
|
63
|
+
}
|
|
64
|
+
chunks.push({
|
|
65
|
+
id: sym.id,
|
|
66
|
+
symbolId: sym.id,
|
|
67
|
+
title: `${sym.qualifiedName} — ${sym.file}:${sym.startLine}-${sym.endLine}`,
|
|
68
|
+
kind: sym.kind,
|
|
69
|
+
classification: ranked.classification,
|
|
70
|
+
file: sym.file,
|
|
71
|
+
startLine: sym.startLine,
|
|
72
|
+
endLine: sym.endLine,
|
|
73
|
+
code,
|
|
74
|
+
context: contextParts.join("\n"),
|
|
75
|
+
embedding: null,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return chunks;
|
|
79
|
+
}
|
|
80
|
+
export function chunkToText(chunk) {
|
|
81
|
+
return [
|
|
82
|
+
`${chunk.kind}: ${chunk.title} [${chunk.classification}]`,
|
|
83
|
+
chunk.context,
|
|
84
|
+
"",
|
|
85
|
+
chunk.code,
|
|
86
|
+
].join("\n");
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=chunk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chunk.js","sourceRoot":"","sources":["../../src/retrieval/chunk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAiB7B,MAAM,UAAU,WAAW,CAAC,OAAe,EAAE,QAAwB;IACnE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AC;QAC9E,CAAC;QAED,MAAM,CAAC,IAAI,CAAC;YACV,EAAE,EAAE,GAAG,CAAC,EAAE;YACV,QAAQ,EAAE,GAAG,CAAC,EAAE;YAChB,KAAK,EAAE,GAAG,GAAG,CAAC,aAAa,MAAM,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,SAAS,IAAI,GAAG,CAAC,OAAO,EAAE;YAC3E,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,cAAc,EAAE,MAAM,CAAC,cAAc;YACrC,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,IAAI;YACJ,OAAO,EAAE,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC;YAChC,SAAS,EAAE,IAAI;SAChB,CAAC,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAY;IACtC,OAAO;QACL,GAAG,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,KAAK,KAAK,KAAK,CAAC,cAAc,GAAG;QACzD,KAAK,CAAC,OAAO;QACb,EAAE;QACF,KAAK,CAAC,IAAI;KACX,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export function stem(token) {
|
|
2
|
+
if (token.length > 4 && token.endsWith("ies"))
|
|
3
|
+
return `${token.slice(0, -3)}y`;
|
|
4
|
+
if (token.length > 4 && token.endsWith("ed"))
|
|
5
|
+
return token.slice(0, -2);
|
|
6
|
+
if (token.length > 4 && token.endsWith("ing"))
|
|
7
|
+
return token.slice(0, -3);
|
|
8
|
+
if (token.length > 3 && token.endsWith("es"))
|
|
9
|
+
return token.slice(0, -2);
|
|
10
|
+
if (token.length > 3 && token.endsWith("s") && !token.endsWith("ss"))
|
|
11
|
+
return token.slice(0, -1);
|
|
12
|
+
if (token.length > 5 && token.endsWith("e"))
|
|
13
|
+
return token.slice(0, -1);
|
|
14
|
+
return token;
|
|
15
|
+
}
|
|
16
|
+
export function tokenize(text) {
|
|
17
|
+
return text
|
|
18
|
+
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
|
19
|
+
.replace(/_/g, " ")
|
|
20
|
+
.toLowerCase()
|
|
21
|
+
.split(/[^a-z0-9]+/)
|
|
22
|
+
.filter((t) => t.length > 1)
|
|
23
|
+
.map(stem);
|
|
24
|
+
}
|
|
25
|
+
export function localEmbed(text) {
|
|
26
|
+
const tokens = tokenize(text);
|
|
27
|
+
const counts = new Map();
|
|
28
|
+
for (const t of tokens)
|
|
29
|
+
counts.set(t, (counts.get(t) ?? 0) + 1);
|
|
30
|
+
const vector = new Array(256).fill(0);
|
|
31
|
+
for (const [token, count] of counts) {
|
|
32
|
+
let h = 2166136261;
|
|
33
|
+
for (let i = 0; i < token.length; i++) {
|
|
34
|
+
h ^= token.charCodeAt(i);
|
|
35
|
+
h = Math.imul(h, 16777619);
|
|
36
|
+
}
|
|
37
|
+
const bucket = Math.abs(h) % 256;
|
|
38
|
+
vector[bucket] += count;
|
|
39
|
+
}
|
|
40
|
+
let norm = 0;
|
|
41
|
+
for (const v of vector)
|
|
42
|
+
norm += v * v;
|
|
43
|
+
norm = Math.sqrt(norm) || 1;
|
|
44
|
+
return vector.map((v) => v / norm);
|
|
45
|
+
}
|
|
46
|
+
export function cosineSimilarity(a, b) {
|
|
47
|
+
let dot = 0;
|
|
48
|
+
const len = Math.min(a.length, b.length);
|
|
49
|
+
for (let i = 0; i < len; i++)
|
|
50
|
+
dot += a[i] * b[i];
|
|
51
|
+
return dot;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=local-embed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-embed.js","sourceRoot":"","sources":["../../src/retrieval/local-embed.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,IAAI,CAAC,KAAa;IAChC,IAAI,K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C,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,CAAW,EAAE,CAAW;IACvD,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;IACzC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE;QAAE,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACjD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Chunk } from "./chunk.js";
|
|
2
|
+
export interface RetrievedChunk {
|
|
3
|
+
chunk: Chunk;
|
|
4
|
+
score: number;
|
|
5
|
+
via: "direct" | "graph-expansion";
|
|
6
|
+
}
|
|
7
|
+
export interface RetrievalResult {
|
|
8
|
+
matches: RetrievedChunk[];
|
|
9
|
+
usedLocalEmbeddings: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function embedChunks(chunks: Chunk[], embeddings: number[][] | null): boolean;
|
|
12
|
+
export declare function retrieve(chunks: Chunk[], question: string, questionEmbedding: number[], topK?: number, expansionLimit?: number): RetrievalResult;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { chunkToText } from "./chunk.js";
|
|
2
|
+
import { localEmbed, cosineSimilarity, tokenize } from "./local-embed.js";
|
|
3
|
+
export function embedChunks(chunks, embeddings) {
|
|
4
|
+
if (embeddings && embeddings.length === chunks.length) {
|
|
5
|
+
for (let i = 0; i < chunks.length; i++)
|
|
6
|
+
chunks[i].embedding = embeddings[i];
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
for (const chunk of chunks)
|
|
10
|
+
chunk.embedding = localEmbed(chunkToText(chunk));
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
export function retrieve(chunks, question, questionEmbedding, topK = 6, expansionLimit = 4) {
|
|
14
|
+
const qTokens = new Set(tokenize(question));
|
|
15
|
+
const scored = [];
|
|
16
|
+
for (const chunk of chunks) {
|
|
17
|
+
const emb = chunk.embedding ?? localEmbed(chunkToText(chunk));
|
|
18
|
+
const vectorScore = cosineSimilarity(questionEmbedding, emb);
|
|
19
|
+
const titleTokens = tokenizeIdentifiers(chunk.title);
|
|
20
|
+
const codeTokens = new Set(tokenizeIdentifiers(chunk.code));
|
|
21
|
+
let lexical = 0;
|
|
22
|
+
for (const t of qTokens) {
|
|
23
|
+
if (titleTokens.has(t))
|
|
24
|
+
lexical += 3;
|
|
25
|
+
else if (codeTokens.has(t))
|
|
26
|
+
lexical += 1;
|
|
27
|
+
}
|
|
28
|
+
const lexicalScore = qTokens.size > 0 ? lexical / (2 * qTokens.size) : 0;
|
|
29
|
+
const roleBoost = chunk.classification === "entry-point" || chunk.classification === "load-bearing" ? 0.05 : 0;
|
|
30
|
+
scored.push({ chunk, score: lexicalScore + 0.25 * vectorScore + roleBoost, via: "direct" });
|
|
31
|
+
}
|
|
32
|
+
scored.sort((a, b) => b.score - a.score);
|
|
33
|
+
const matches = scored.slice(0, topK);
|
|
34
|
+
const selected = new Set(matches.map((m) => m.chunk.id));
|
|
35
|
+
const expansionIds = collectGraphNeighbors(matches.map((m) => m.chunk.id), chunks, selected, expansionLimit);
|
|
36
|
+
const expansions = [];
|
|
37
|
+
for (const id of expansionIds) {
|
|
38
|
+
const chunk = chunks.find((c) => c.id === id);
|
|
39
|
+
if (chunk)
|
|
40
|
+
expansions.push({ chunk, score: 0, via: "graph-expansion" });
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
matches: [...matches, ...expansions],
|
|
44
|
+
usedLocalEmbeddings: chunks.length > 0 && chunks[0].embedding !== null && chunks[0].embedding.length === 256,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function collectGraphNeighbors(seedIds, chunks, excluded, limit) {
|
|
48
|
+
const byId = new Map(chunks.map((c) => [c.id, c]));
|
|
49
|
+
const result = [];
|
|
50
|
+
const seen = new Set();
|
|
51
|
+
for (const seedId of seedIds) {
|
|
52
|
+
const chunk = byId.get(seedId);
|
|
53
|
+
if (!chunk)
|
|
54
|
+
continue;
|
|
55
|
+
for (const line of [
|
|
56
|
+
(chunk.context.match(/Called by: (.*)/) ?? [])[1],
|
|
57
|
+
(chunk.context.match(/Calls: (.*)/) ?? [])[1],
|
|
58
|
+
]) {
|
|
59
|
+
if (!line)
|
|
60
|
+
continue;
|
|
61
|
+
for (const ref of line.split(", ")) {
|
|
62
|
+
const id = refToChunkId(ref);
|
|
63
|
+
if (!id || excluded.has(id) || seen.has(id))
|
|
64
|
+
continue;
|
|
65
|
+
if (!byId.has(id))
|
|
66
|
+
continue;
|
|
67
|
+
seen.add(id);
|
|
68
|
+
result.push(id);
|
|
69
|
+
if (result.length >= limit)
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
function refToChunkId(ref) {
|
|
77
|
+
const match = ref.match(/^(.+) \(([^:]+):(\d+)\)$/);
|
|
78
|
+
if (!match)
|
|
79
|
+
return null;
|
|
80
|
+
return `${match[2]}::${match[1].trim()}`;
|
|
81
|
+
}
|
|
82
|
+
function tokenizeIdentifiers(text) {
|
|
83
|
+
return new Set(tokenize(text));
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=retrieve.js.map
|