@rpcajr/smart-graph-indexer 1.0.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/.agents/rules/antigravity-rtk-rules.md +32 -0
- package/README.md +116 -0
- package/dist/graph.d.ts +32 -0
- package/dist/graph.js +86 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +227 -0
- package/dist/mcp_server.d.ts +48 -0
- package/dist/mcp_server.js +433 -0
- package/dist/parser.d.ts +42 -0
- package/dist/parser.js +529 -0
- package/dist/stats.d.ts +43 -0
- package/dist/stats.js +146 -0
- package/dist/ui.d.ts +2 -0
- package/dist/ui.js +1003 -0
- package/dist/vector.d.ts +66 -0
- package/dist/vector.js +144 -0
- package/dist/wasm/tree-sitter-c.wasm +0 -0
- package/dist/wasm/tree-sitter-c_sharp.wasm +0 -0
- package/dist/wasm/tree-sitter-cpp.wasm +0 -0
- package/dist/wasm/tree-sitter-go.wasm +0 -0
- package/dist/wasm/tree-sitter-java.wasm +0 -0
- package/dist/wasm/tree-sitter-python.wasm +0 -0
- package/dist/wasm/tree-sitter-ruby.wasm +0 -0
- package/dist/wasm/tree-sitter-rust.wasm +0 -0
- package/dist/wasm/tree-sitter-typescript.wasm +0 -0
- package/dist/wasm/tree-sitter.wasm +0 -0
- package/dist/watcher.d.ts +36 -0
- package/dist/watcher.js +166 -0
- package/mcp-config-example.json +12 -0
- package/mcp_smart_indexer_implementation_spec.md +366 -0
- package/package.json +35 -0
- package/src/graph.ts +93 -0
- package/src/index.ts +216 -0
- package/src/mcp_server.ts +454 -0
- package/src/parser.ts +484 -0
- package/src/stats.ts +156 -0
- package/src/ui.ts +956 -0
- package/src/vector.ts +166 -0
- package/src/watcher.ts +144 -0
- package/test_project/App.java +16 -0
- package/test_project/BillingService.cs +31 -0
- package/test_project/auth.ts +18 -0
- package/test_project/config.ts +11 -0
- package/test_project/database.ts +21 -0
- package/test_project/index.ts +13 -0
- package/test_project/main.go +11 -0
- package/test_project/main.py +21 -0
- package/test_project/processor.py +12 -0
- package/test_project/utils.py +13 -0
- package/tsconfig.json +16 -0
- package/wasm/tree-sitter-c.wasm +0 -0
- package/wasm/tree-sitter-c_sharp.wasm +0 -0
- package/wasm/tree-sitter-cpp.wasm +0 -0
- package/wasm/tree-sitter-go.wasm +0 -0
- package/wasm/tree-sitter-java.wasm +0 -0
- package/wasm/tree-sitter-python.wasm +0 -0
- package/wasm/tree-sitter-ruby.wasm +0 -0
- package/wasm/tree-sitter-rust.wasm +0 -0
- package/wasm/tree-sitter-typescript.wasm +0 -0
- package/wasm/tree-sitter.wasm +0 -0
package/dist/parser.js
ADDED
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.ASTParser = void 0;
|
|
40
|
+
const fs = __importStar(require("fs"));
|
|
41
|
+
const path = __importStar(require("path"));
|
|
42
|
+
const crypto = __importStar(require("crypto"));
|
|
43
|
+
const web_tree_sitter_1 = __importDefault(require("web-tree-sitter"));
|
|
44
|
+
class ASTParser {
|
|
45
|
+
isInitialized = false;
|
|
46
|
+
languages = {};
|
|
47
|
+
constructor() { }
|
|
48
|
+
/**
|
|
49
|
+
* Initializes the web-tree-sitter parser and loads language WASM files.
|
|
50
|
+
*/
|
|
51
|
+
async init(wasmDir) {
|
|
52
|
+
if (this.isInitialized)
|
|
53
|
+
return;
|
|
54
|
+
await web_tree_sitter_1.default.init({
|
|
55
|
+
locateFile: (filepath) => {
|
|
56
|
+
if (filepath === 'tree-sitter.wasm') {
|
|
57
|
+
return path.join(wasmDir, 'tree-sitter.wasm');
|
|
58
|
+
}
|
|
59
|
+
return filepath;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
const langFiles = {
|
|
63
|
+
csharp: 'tree-sitter-c_sharp.wasm',
|
|
64
|
+
typescript: 'tree-sitter-typescript.wasm',
|
|
65
|
+
python: 'tree-sitter-python.wasm',
|
|
66
|
+
java: 'tree-sitter-java.wasm',
|
|
67
|
+
go: 'tree-sitter-go.wasm',
|
|
68
|
+
rust: 'tree-sitter-rust.wasm',
|
|
69
|
+
ruby: 'tree-sitter-ruby.wasm',
|
|
70
|
+
cpp: 'tree-sitter-cpp.wasm',
|
|
71
|
+
c: 'tree-sitter-c.wasm',
|
|
72
|
+
};
|
|
73
|
+
for (const [key, file] of Object.entries(langFiles)) {
|
|
74
|
+
const fullPath = path.join(wasmDir, file);
|
|
75
|
+
if (fs.existsSync(fullPath)) {
|
|
76
|
+
try {
|
|
77
|
+
this.languages[key] = await web_tree_sitter_1.default.Language.load(fullPath);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
console.error(`Failed to load grammar: ${file}`, err);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
this.isInitialized = true;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Calculates MD5 hash of file content.
|
|
88
|
+
*/
|
|
89
|
+
getHash(content) {
|
|
90
|
+
return crypto.createHash('md5').update(content).digest('hex');
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Parsers a file and extracts its skeleton.
|
|
94
|
+
*/
|
|
95
|
+
parseFile(filePath, content) {
|
|
96
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
97
|
+
const file_hash = this.getHash(content);
|
|
98
|
+
const dependencies = this.extractDependencies(content, ext);
|
|
99
|
+
let symbols = [];
|
|
100
|
+
if (!this.isInitialized) {
|
|
101
|
+
return { file_path: filePath, file_hash, symbols, dependencies };
|
|
102
|
+
}
|
|
103
|
+
const parser = new web_tree_sitter_1.default();
|
|
104
|
+
try {
|
|
105
|
+
if (ext === '.cs' && this.languages.csharp) {
|
|
106
|
+
parser.setLanguage(this.languages.csharp);
|
|
107
|
+
const tree = parser.parse(content);
|
|
108
|
+
symbols = this.parseCSharp(tree.rootNode, content);
|
|
109
|
+
}
|
|
110
|
+
else if (['.ts', '.tsx', '.js', '.jsx'].includes(ext) && this.languages.typescript) {
|
|
111
|
+
parser.setLanguage(this.languages.typescript);
|
|
112
|
+
const tree = parser.parse(content);
|
|
113
|
+
symbols = this.parseTypeScript(tree.rootNode, content);
|
|
114
|
+
}
|
|
115
|
+
else if (ext === '.py' && this.languages.python) {
|
|
116
|
+
parser.setLanguage(this.languages.python);
|
|
117
|
+
const tree = parser.parse(content);
|
|
118
|
+
symbols = this.parsePython(tree.rootNode, content);
|
|
119
|
+
}
|
|
120
|
+
else if (ext === '.java' && this.languages.java) {
|
|
121
|
+
parser.setLanguage(this.languages.java);
|
|
122
|
+
const tree = parser.parse(content);
|
|
123
|
+
symbols = this.parseJava(tree.rootNode, content);
|
|
124
|
+
}
|
|
125
|
+
else if (ext === '.go' && this.languages.go) {
|
|
126
|
+
parser.setLanguage(this.languages.go);
|
|
127
|
+
const tree = parser.parse(content);
|
|
128
|
+
symbols = this.parseGo(tree.rootNode, content);
|
|
129
|
+
}
|
|
130
|
+
else if (ext === '.rs' && this.languages.rust) {
|
|
131
|
+
parser.setLanguage(this.languages.rust);
|
|
132
|
+
const tree = parser.parse(content);
|
|
133
|
+
symbols = this.parseRust(tree.rootNode, content);
|
|
134
|
+
}
|
|
135
|
+
else if (ext === '.rb' && this.languages.ruby) {
|
|
136
|
+
parser.setLanguage(this.languages.ruby);
|
|
137
|
+
const tree = parser.parse(content);
|
|
138
|
+
symbols = this.parseRuby(tree.rootNode, content);
|
|
139
|
+
}
|
|
140
|
+
else if (['.cpp', '.hpp', '.cc', '.cxx', '.h', '.c'].includes(ext)) {
|
|
141
|
+
const lang = ['.c', '.h'].includes(ext) ? this.languages.c : this.languages.cpp;
|
|
142
|
+
if (lang) {
|
|
143
|
+
parser.setLanguage(lang);
|
|
144
|
+
const tree = parser.parse(content);
|
|
145
|
+
symbols = this.parseCpp(tree.rootNode, content);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (err) {
|
|
150
|
+
console.error(`Error parsing AST for ${filePath}:`, err);
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
file_path: filePath,
|
|
154
|
+
file_hash,
|
|
155
|
+
symbols,
|
|
156
|
+
dependencies,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
extractDependencies(content, ext) {
|
|
160
|
+
const deps = new Set();
|
|
161
|
+
if (ext === '.cs') {
|
|
162
|
+
const match = content.matchAll(/^\s*using\s+([^;=]+);/gm);
|
|
163
|
+
for (const m of match)
|
|
164
|
+
deps.add(m[1].trim());
|
|
165
|
+
}
|
|
166
|
+
else if (['.ts', '.tsx', '.js', '.jsx'].includes(ext)) {
|
|
167
|
+
const importMatch = content.matchAll(/import\s+(?:[^'"]+\s+from\s+)?['"]([^'"]+)['"]/g);
|
|
168
|
+
for (const m of importMatch)
|
|
169
|
+
deps.add(m[1]);
|
|
170
|
+
const requireMatch = content.matchAll(/(?:const|let|var)\s+.*\s*=\s*require\(['"]([^'"]+)['"]\)/g);
|
|
171
|
+
for (const m of requireMatch)
|
|
172
|
+
deps.add(m[1]);
|
|
173
|
+
}
|
|
174
|
+
else if (ext === '.py') {
|
|
175
|
+
const importMatch = content.matchAll(/^\s*import\s+([^\s\n#]+)/gm);
|
|
176
|
+
for (const m of importMatch)
|
|
177
|
+
deps.add(m[1].trim());
|
|
178
|
+
const fromMatch = content.matchAll(/^\s*from\s+([^\s\n#]+)\s+import/gm);
|
|
179
|
+
for (const m of fromMatch)
|
|
180
|
+
deps.add(m[1].trim());
|
|
181
|
+
}
|
|
182
|
+
else if (ext === '.java') {
|
|
183
|
+
const match = content.matchAll(/^\s*import\s+([^;]+);/gm);
|
|
184
|
+
for (const m of match)
|
|
185
|
+
deps.add(m[1].trim());
|
|
186
|
+
}
|
|
187
|
+
else if (ext === '.go') {
|
|
188
|
+
const match = content.matchAll(/^\s*import\s+(?:\([^)]+\)|"[^"]+")/g);
|
|
189
|
+
for (const m of match) {
|
|
190
|
+
const inner = m[0].match(/"([^"]+)"/g);
|
|
191
|
+
if (inner) {
|
|
192
|
+
for (const item of inner)
|
|
193
|
+
deps.add(item.replace(/"/g, ''));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else if (ext === '.rs') {
|
|
198
|
+
const match = content.matchAll(/^\s*use\s+([^;]+);/gm);
|
|
199
|
+
for (const m of match)
|
|
200
|
+
deps.add(m[1].trim());
|
|
201
|
+
}
|
|
202
|
+
else if (ext === '.rb') {
|
|
203
|
+
const match = content.matchAll(/^\s*(?:require|require_relative)\s+['"]([^'"]+)['"]/gm);
|
|
204
|
+
for (const m of match)
|
|
205
|
+
deps.add(m[1].trim());
|
|
206
|
+
}
|
|
207
|
+
else if (['.cpp', '.hpp', '.cc', '.cxx', '.h', '.c'].includes(ext)) {
|
|
208
|
+
const match = content.matchAll(/^\s*#\s*include\s+([<"][^>"]+[>"])/gm);
|
|
209
|
+
for (const m of match)
|
|
210
|
+
deps.add(m[1].trim());
|
|
211
|
+
}
|
|
212
|
+
return Array.from(deps);
|
|
213
|
+
}
|
|
214
|
+
getPrecedingDocstring(node, content) {
|
|
215
|
+
const startLine = node.startPosition.row;
|
|
216
|
+
const lines = content.split('\n');
|
|
217
|
+
let lineIdx = startLine - 1;
|
|
218
|
+
const comments = [];
|
|
219
|
+
while (lineIdx >= 0) {
|
|
220
|
+
const currentLine = lines[lineIdx].trim();
|
|
221
|
+
if (currentLine.startsWith('///') || currentLine.startsWith('//') || currentLine.startsWith('/*') || currentLine.startsWith('*') || currentLine.endsWith('*/') || currentLine.startsWith('#')) {
|
|
222
|
+
comments.unshift(lines[lineIdx].trim());
|
|
223
|
+
lineIdx--;
|
|
224
|
+
}
|
|
225
|
+
else if (currentLine === '') {
|
|
226
|
+
lineIdx--;
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (comments.length > 0) {
|
|
233
|
+
return comments.join('\n');
|
|
234
|
+
}
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
parseCSharp(root, content) {
|
|
238
|
+
const symbols = [];
|
|
239
|
+
const traverse = (node) => {
|
|
240
|
+
const type = node.type;
|
|
241
|
+
if (type === 'class_declaration' || type === 'interface_declaration' || type === 'struct_declaration') {
|
|
242
|
+
const nameNode = node.childForFieldName('name');
|
|
243
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
244
|
+
const kind = type === 'class_declaration' ? 'Class' : type === 'interface_declaration' ? 'Interface' : 'Struct';
|
|
245
|
+
const bodyNode = node.childForFieldName('body') || node.lastChild;
|
|
246
|
+
const sigEnd = bodyNode && bodyNode.type === 'declaration_list' ? bodyNode.startIndex : node.endIndex;
|
|
247
|
+
symbols.push({
|
|
248
|
+
name, kind,
|
|
249
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
250
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
251
|
+
start_line: node.startPosition.row + 1,
|
|
252
|
+
end_line: node.endPosition.row + 1,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
else if (type === 'method_declaration') {
|
|
256
|
+
const nameNode = node.childForFieldName('name');
|
|
257
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
258
|
+
const bodyNode = node.childForFieldName('body');
|
|
259
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
260
|
+
symbols.push({
|
|
261
|
+
name, kind: 'Method',
|
|
262
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
263
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
264
|
+
start_line: node.startPosition.row + 1,
|
|
265
|
+
end_line: node.endPosition.row + 1,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
for (let i = 0; i < node.childCount; i++)
|
|
269
|
+
traverse(node.child(i));
|
|
270
|
+
};
|
|
271
|
+
traverse(root);
|
|
272
|
+
return symbols;
|
|
273
|
+
}
|
|
274
|
+
parseTypeScript(root, content) {
|
|
275
|
+
const symbols = [];
|
|
276
|
+
const traverse = (node) => {
|
|
277
|
+
const type = node.type;
|
|
278
|
+
if (type === 'class_declaration' || type === 'interface_declaration') {
|
|
279
|
+
const nameNode = node.childForFieldName('name');
|
|
280
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
281
|
+
const kind = type === 'class_declaration' ? 'Class' : 'Interface';
|
|
282
|
+
const bodyNode = node.childForFieldName('body') || node.lastChild;
|
|
283
|
+
const sigEnd = bodyNode && (bodyNode.type === 'class_body' || bodyNode.type === 'object_type') ? bodyNode.startIndex : node.endIndex;
|
|
284
|
+
symbols.push({
|
|
285
|
+
name, kind,
|
|
286
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
287
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
288
|
+
start_line: node.startPosition.row + 1,
|
|
289
|
+
end_line: node.endPosition.row + 1,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
else if (type === 'method_definition' || type === 'function_declaration') {
|
|
293
|
+
const nameNode = node.childForFieldName('name');
|
|
294
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
295
|
+
const bodyNode = node.childForFieldName('body');
|
|
296
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
297
|
+
symbols.push({
|
|
298
|
+
name, kind: type === 'method_definition' ? 'Method' : 'Function',
|
|
299
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
300
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
301
|
+
start_line: node.startPosition.row + 1,
|
|
302
|
+
end_line: node.endPosition.row + 1,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
for (let i = 0; i < node.childCount; i++)
|
|
306
|
+
traverse(node.child(i));
|
|
307
|
+
};
|
|
308
|
+
traverse(root);
|
|
309
|
+
return symbols;
|
|
310
|
+
}
|
|
311
|
+
parsePython(root, content) {
|
|
312
|
+
const symbols = [];
|
|
313
|
+
const traverse = (node) => {
|
|
314
|
+
const type = node.type;
|
|
315
|
+
if (type === 'class_definition' || type === 'function_definition') {
|
|
316
|
+
const nameNode = node.childForFieldName('name');
|
|
317
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
318
|
+
const bodyNode = node.childForFieldName('body');
|
|
319
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
320
|
+
symbols.push({
|
|
321
|
+
name, kind: type === 'class_definition' ? 'Class' : 'Function',
|
|
322
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
323
|
+
docstring: this.getPythonDocstring(bodyNode, content),
|
|
324
|
+
start_line: node.startPosition.row + 1,
|
|
325
|
+
end_line: node.endPosition.row + 1,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
for (let i = 0; i < node.childCount; i++)
|
|
329
|
+
traverse(node.child(i));
|
|
330
|
+
};
|
|
331
|
+
traverse(root);
|
|
332
|
+
return symbols;
|
|
333
|
+
}
|
|
334
|
+
getPythonDocstring(bodyNode, content) {
|
|
335
|
+
if (!bodyNode || bodyNode.childCount === 0)
|
|
336
|
+
return undefined;
|
|
337
|
+
const firstStmt = bodyNode.child(0);
|
|
338
|
+
if (firstStmt && firstStmt.type === 'expression_statement') {
|
|
339
|
+
const stringNode = firstStmt.child(0);
|
|
340
|
+
if (stringNode && stringNode.type === 'string') {
|
|
341
|
+
let text = stringNode.text.trim();
|
|
342
|
+
if (text.startsWith('"""') && text.endsWith('"""'))
|
|
343
|
+
return text.substring(3, text.length - 3).trim();
|
|
344
|
+
if (text.startsWith("'''") && text.endsWith("'''"))
|
|
345
|
+
return text.substring(3, text.length - 3).trim();
|
|
346
|
+
return text;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return undefined;
|
|
350
|
+
}
|
|
351
|
+
parseJava(root, content) {
|
|
352
|
+
const symbols = [];
|
|
353
|
+
const traverse = (node) => {
|
|
354
|
+
const type = node.type;
|
|
355
|
+
if (type === 'class_declaration' || type === 'interface_declaration') {
|
|
356
|
+
const nameNode = node.childForFieldName('name');
|
|
357
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
358
|
+
const bodyNode = node.childForFieldName('body');
|
|
359
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
360
|
+
symbols.push({
|
|
361
|
+
name, kind: type === 'class_declaration' ? 'Class' : 'Interface',
|
|
362
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
363
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
364
|
+
start_line: node.startPosition.row + 1,
|
|
365
|
+
end_line: node.endPosition.row + 1,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
else if (type === 'method_declaration') {
|
|
369
|
+
const nameNode = node.childForFieldName('name');
|
|
370
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
371
|
+
const bodyNode = node.childForFieldName('body');
|
|
372
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
373
|
+
symbols.push({
|
|
374
|
+
name, kind: 'Method',
|
|
375
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
376
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
377
|
+
start_line: node.startPosition.row + 1,
|
|
378
|
+
end_line: node.endPosition.row + 1,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
for (let i = 0; i < node.childCount; i++)
|
|
382
|
+
traverse(node.child(i));
|
|
383
|
+
};
|
|
384
|
+
traverse(root);
|
|
385
|
+
return symbols;
|
|
386
|
+
}
|
|
387
|
+
parseGo(root, content) {
|
|
388
|
+
const symbols = [];
|
|
389
|
+
const traverse = (node) => {
|
|
390
|
+
const type = node.type;
|
|
391
|
+
if (type === 'type_spec') {
|
|
392
|
+
const nameNode = node.childForFieldName('name');
|
|
393
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
394
|
+
const typeNode = node.childForFieldName('type');
|
|
395
|
+
const kind = typeNode && typeNode.type === 'interface_type' ? 'Interface' : 'Struct';
|
|
396
|
+
symbols.push({
|
|
397
|
+
name, kind,
|
|
398
|
+
signature: content.substring(node.startIndex, node.endIndex).trim(),
|
|
399
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
400
|
+
start_line: node.startPosition.row + 1,
|
|
401
|
+
end_line: node.endPosition.row + 1,
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
else if (type === 'function_declaration' || type === 'method_declaration') {
|
|
405
|
+
const nameNode = node.childForFieldName('name');
|
|
406
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
407
|
+
const bodyNode = node.childForFieldName('body');
|
|
408
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
409
|
+
symbols.push({
|
|
410
|
+
name, kind: type === 'method_declaration' ? 'Method' : 'Function',
|
|
411
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
412
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
413
|
+
start_line: node.startPosition.row + 1,
|
|
414
|
+
end_line: node.endPosition.row + 1,
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
for (let i = 0; i < node.childCount; i++)
|
|
418
|
+
traverse(node.child(i));
|
|
419
|
+
};
|
|
420
|
+
traverse(root);
|
|
421
|
+
return symbols;
|
|
422
|
+
}
|
|
423
|
+
parseRust(root, content) {
|
|
424
|
+
const symbols = [];
|
|
425
|
+
const traverse = (node) => {
|
|
426
|
+
const type = node.type;
|
|
427
|
+
if (type === 'struct_item' || type === 'enum_item' || type === 'trait_item') {
|
|
428
|
+
const nameNode = node.childForFieldName('name');
|
|
429
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
430
|
+
const kind = type === 'struct_item' ? 'Struct' : type === 'enum_item' ? 'Enum' : 'Trait';
|
|
431
|
+
const bodyNode = node.childForFieldName('body') || node.lastChild;
|
|
432
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
433
|
+
symbols.push({
|
|
434
|
+
name, kind,
|
|
435
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
436
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
437
|
+
start_line: node.startPosition.row + 1,
|
|
438
|
+
end_line: node.endPosition.row + 1,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
else if (type === 'function_item') {
|
|
442
|
+
const nameNode = node.childForFieldName('name');
|
|
443
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
444
|
+
const bodyNode = node.childForFieldName('body');
|
|
445
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
446
|
+
symbols.push({
|
|
447
|
+
name, kind: 'Function',
|
|
448
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
449
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
450
|
+
start_line: node.startPosition.row + 1,
|
|
451
|
+
end_line: node.endPosition.row + 1,
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
for (let i = 0; i < node.childCount; i++)
|
|
455
|
+
traverse(node.child(i));
|
|
456
|
+
};
|
|
457
|
+
traverse(root);
|
|
458
|
+
return symbols;
|
|
459
|
+
}
|
|
460
|
+
parseRuby(root, content) {
|
|
461
|
+
const symbols = [];
|
|
462
|
+
const traverse = (node) => {
|
|
463
|
+
const type = node.type;
|
|
464
|
+
if (type === 'class' || type === 'module') {
|
|
465
|
+
const nameNode = node.childForFieldName('name');
|
|
466
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
467
|
+
symbols.push({
|
|
468
|
+
name, kind: type === 'class' ? 'Class' : 'Interface',
|
|
469
|
+
signature: `${type} ${name}`,
|
|
470
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
471
|
+
start_line: node.startPosition.row + 1,
|
|
472
|
+
end_line: node.endPosition.row + 1,
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
else if (type === 'method') {
|
|
476
|
+
const nameNode = node.childForFieldName('name');
|
|
477
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
478
|
+
symbols.push({
|
|
479
|
+
name, kind: 'Method',
|
|
480
|
+
signature: `def ${name}`,
|
|
481
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
482
|
+
start_line: node.startPosition.row + 1,
|
|
483
|
+
end_line: node.endPosition.row + 1,
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
for (let i = 0; i < node.childCount; i++)
|
|
487
|
+
traverse(node.child(i));
|
|
488
|
+
};
|
|
489
|
+
traverse(root);
|
|
490
|
+
return symbols;
|
|
491
|
+
}
|
|
492
|
+
parseCpp(root, content) {
|
|
493
|
+
const symbols = [];
|
|
494
|
+
const traverse = (node) => {
|
|
495
|
+
const type = node.type;
|
|
496
|
+
if (type === 'class_specifier' || type === 'struct_specifier') {
|
|
497
|
+
const nameNode = node.childForFieldName('name');
|
|
498
|
+
const name = nameNode ? nameNode.text : 'Unknown';
|
|
499
|
+
const bodyNode = node.childForFieldName('body');
|
|
500
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
501
|
+
symbols.push({
|
|
502
|
+
name, kind: type === 'class_specifier' ? 'Class' : 'Struct',
|
|
503
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
504
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
505
|
+
start_line: node.startPosition.row + 1,
|
|
506
|
+
end_line: node.endPosition.row + 1,
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
else if (type === 'function_definition') {
|
|
510
|
+
const declarator = node.childForFieldName('declarator');
|
|
511
|
+
const name = declarator ? declarator.text : 'Unknown';
|
|
512
|
+
const bodyNode = node.childForFieldName('body');
|
|
513
|
+
const sigEnd = bodyNode ? bodyNode.startIndex : node.endIndex;
|
|
514
|
+
symbols.push({
|
|
515
|
+
name, kind: 'Function',
|
|
516
|
+
signature: content.substring(node.startIndex, sigEnd).trim(),
|
|
517
|
+
docstring: this.getPrecedingDocstring(node, content),
|
|
518
|
+
start_line: node.startPosition.row + 1,
|
|
519
|
+
end_line: node.endPosition.row + 1,
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
for (let i = 0; i < node.childCount; i++)
|
|
523
|
+
traverse(node.child(i));
|
|
524
|
+
};
|
|
525
|
+
traverse(root);
|
|
526
|
+
return symbols;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
exports.ASTParser = ASTParser;
|
package/dist/stats.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export interface ToolCallStats {
|
|
2
|
+
calls: number;
|
|
3
|
+
saved: number;
|
|
4
|
+
actual: number;
|
|
5
|
+
alternative: number;
|
|
6
|
+
totalTimeMs: number;
|
|
7
|
+
}
|
|
8
|
+
export interface HistoryEntry {
|
|
9
|
+
timestamp: string;
|
|
10
|
+
sessionId: string;
|
|
11
|
+
toolName: string;
|
|
12
|
+
saved: number;
|
|
13
|
+
actual: number;
|
|
14
|
+
alternative: number;
|
|
15
|
+
target?: string;
|
|
16
|
+
execTimeMs: number;
|
|
17
|
+
}
|
|
18
|
+
export interface TokenStats {
|
|
19
|
+
totalCalls: number;
|
|
20
|
+
totalTokensSaved: number;
|
|
21
|
+
totalTokensActual: number;
|
|
22
|
+
totalTokensAlternative: number;
|
|
23
|
+
efficiencyPercentage: number;
|
|
24
|
+
callsByType: Record<string, ToolCallStats>;
|
|
25
|
+
history: HistoryEntry[];
|
|
26
|
+
}
|
|
27
|
+
export declare class StatsManager {
|
|
28
|
+
private statsPath;
|
|
29
|
+
private stats;
|
|
30
|
+
private currentSessionId;
|
|
31
|
+
constructor(projectPath: string);
|
|
32
|
+
private loadStats;
|
|
33
|
+
private saveStats;
|
|
34
|
+
/**
|
|
35
|
+
* Estimates tokens based on character count (1 token approx 4 characters).
|
|
36
|
+
*/
|
|
37
|
+
estimateTokens(text: string): number;
|
|
38
|
+
/**
|
|
39
|
+
* Records a tool call and computes the token savings.
|
|
40
|
+
*/
|
|
41
|
+
recordCall(toolName: string, actualPayload: string, alternativeText: string, target?: string, execTimeMs?: number): void;
|
|
42
|
+
getStats(): TokenStats;
|
|
43
|
+
}
|