@danielblomma/cortex-mcp 2.1.1 → 2.1.4
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/README.md +19 -0
- package/bin/cortex.mjs +2 -0
- package/package.json +3 -2
- package/scaffold/mcp/build.mjs +92 -0
- package/scaffold/mcp/package.json +4 -4
- package/scaffold/mcp/src/embed.ts +181 -77
- package/scaffold/mcp/src/embedScheduler.ts +509 -0
- package/scaffold/mcp/src/embeddings.ts +31 -30
- package/scaffold/mcp/src/graphCsv.ts +65 -0
- package/scaffold/mcp/src/jsonl.ts +69 -14
- package/scaffold/mcp/src/loadGraph.ts +276 -40
- package/scaffold/mcp/src/lruCache.ts +41 -0
- package/scaffold/mcp/src/searchCore.ts +1 -1
- package/scaffold/mcp/src/searchResults.ts +3 -3
- package/scaffold/mcp/src/types.ts +6 -1
- package/scaffold/mcp/tests/embed-scheduler.test.mjs +474 -0
- package/scaffold/mcp/tests/embedding-jsonl.test.mjs +39 -0
- package/scaffold/mcp/tests/graph-bulk-load.test.mjs +258 -0
- package/scaffold/mcp/tests/graph-csv.test.mjs +118 -0
- package/scaffold/mcp/tests/lru-cache.test.mjs +37 -0
- package/scaffold/mcp/tests/vector-index.test.mjs +109 -0
- package/scaffold/mcp/tsconfig.json +3 -1
- package/scaffold/scripts/bootstrap.sh +34 -2
- package/scaffold/scripts/dashboard.mjs +1 -1
- package/scaffold/scripts/ingest-parsers.mjs +387 -0
- package/scaffold/scripts/ingest-worker.mjs +52 -0
- package/scaffold/scripts/ingest.mjs +550 -378
- package/scaffold/scripts/parsers/csharp.mjs +2 -1
- package/scaffold/scripts/parsers/javascript/ast.mjs +160 -8
- package/scaffold/scripts/parsers/javascript/chunks.mjs +129 -22
- package/scaffold/scripts/parsers/javascript/imports.mjs +38 -4
- package/scaffold/scripts/parsers/vbnet.mjs +2 -1
- package/scaffold/scripts/status.sh +50 -14
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared chunk-parser registry.
|
|
3
|
+
*
|
|
4
|
+
* Single source of truth for "which parser handles which extension", used by
|
|
5
|
+
* both the main ingest process and the worker-thread pool. Keeping one table
|
|
6
|
+
* guarantees a file parses identically whether it runs inline or in a worker.
|
|
7
|
+
*
|
|
8
|
+
* Parsers self-initialize lazily (tree-sitter grammars load WASM on first
|
|
9
|
+
* parse and cache per module instance), so a worker amortizes that cost over
|
|
10
|
+
* its lifetime.
|
|
11
|
+
*/
|
|
12
|
+
import { parseCode } from "./parsers/javascript.mjs";
|
|
13
|
+
|
|
14
|
+
const parseJavaScriptCode = parseCode;
|
|
15
|
+
let parseVbNetCode = null;
|
|
16
|
+
let parseCSharpCode = null;
|
|
17
|
+
let parseCSharpProjectImpl = null;
|
|
18
|
+
let parseCppCode = null;
|
|
19
|
+
let parseConfigCode = null;
|
|
20
|
+
let parseResourcesCode = null;
|
|
21
|
+
let parseSqlCode = null;
|
|
22
|
+
let parseRustCode = null;
|
|
23
|
+
let parsePythonCode = null;
|
|
24
|
+
let parseGoCode = null;
|
|
25
|
+
let parseJavaCode = null;
|
|
26
|
+
let parseRubyCode = null;
|
|
27
|
+
let parseBashCode = null;
|
|
28
|
+
let parseVb6Code = null;
|
|
29
|
+
let parseMarkdownCode = null;
|
|
30
|
+
let isVbNetParserAvailable = () => false;
|
|
31
|
+
let isCSharpParserAvailableImpl = () => false;
|
|
32
|
+
let isCppParserAvailable = () => false;
|
|
33
|
+
let getCSharpParserRuntimeImpl = () => ({ available: false, reason: "parser module not loaded" });
|
|
34
|
+
|
|
35
|
+
let loadPromise = null;
|
|
36
|
+
|
|
37
|
+
// Languages whose parser runs purely in-process (JS or WASM) with no
|
|
38
|
+
// subprocess and no cross-file global state, so they are safe to run in a
|
|
39
|
+
// worker thread. csharp (Roslyn subprocess + project-wide batch parse in the
|
|
40
|
+
// main process), vbnet (subprocess), and cpp (clang-bridge fallback
|
|
41
|
+
// subprocess) stay on the main thread.
|
|
42
|
+
export const PARALLEL_SAFE_LANGUAGES = new Set([
|
|
43
|
+
"javascript",
|
|
44
|
+
"jsx",
|
|
45
|
+
"typescript",
|
|
46
|
+
"tsx",
|
|
47
|
+
"python",
|
|
48
|
+
"go",
|
|
49
|
+
"java",
|
|
50
|
+
"ruby",
|
|
51
|
+
"bash",
|
|
52
|
+
"sql",
|
|
53
|
+
"config",
|
|
54
|
+
"resource",
|
|
55
|
+
"settings",
|
|
56
|
+
"markdown",
|
|
57
|
+
"rust",
|
|
58
|
+
"vb6"
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
export async function loadParsers() {
|
|
62
|
+
if (loadPromise) {
|
|
63
|
+
return loadPromise;
|
|
64
|
+
}
|
|
65
|
+
const loaders = [
|
|
66
|
+
import("./parsers/vbnet.mjs").then((module) => {
|
|
67
|
+
parseVbNetCode = module.parseCode;
|
|
68
|
+
isVbNetParserAvailable =
|
|
69
|
+
typeof module.isVbNetParserAvailable === "function"
|
|
70
|
+
? module.isVbNetParserAvailable
|
|
71
|
+
: () => typeof module.parseCode === "function";
|
|
72
|
+
}),
|
|
73
|
+
import("./parsers/csharp.mjs").then((module) => {
|
|
74
|
+
parseCSharpCode = module.parseCode;
|
|
75
|
+
parseCSharpProjectImpl = module.parseProject ?? null;
|
|
76
|
+
getCSharpParserRuntimeImpl =
|
|
77
|
+
typeof module.getCSharpParserRuntime === "function"
|
|
78
|
+
? module.getCSharpParserRuntime
|
|
79
|
+
: () => ({ available: typeof module.parseCode === "function", reason: "runtime details unavailable" });
|
|
80
|
+
isCSharpParserAvailableImpl =
|
|
81
|
+
typeof module.isCSharpParserAvailable === "function"
|
|
82
|
+
? module.isCSharpParserAvailable
|
|
83
|
+
: () => typeof module.parseCode === "function";
|
|
84
|
+
}),
|
|
85
|
+
import("./parsers/cpp-dispatch.mjs").then((module) => {
|
|
86
|
+
parseCppCode = module.parseCode;
|
|
87
|
+
isCppParserAvailable =
|
|
88
|
+
typeof module.isCppParserAvailable === "function"
|
|
89
|
+
? module.isCppParserAvailable
|
|
90
|
+
: () => typeof module.parseCode === "function";
|
|
91
|
+
}),
|
|
92
|
+
import("./parsers/config.mjs").then((module) => {
|
|
93
|
+
parseConfigCode = module.parseCode;
|
|
94
|
+
}),
|
|
95
|
+
import("./parsers/resources.mjs").then((module) => {
|
|
96
|
+
parseResourcesCode = module.parseCode;
|
|
97
|
+
}),
|
|
98
|
+
import("./parsers/sql.mjs").then((module) => {
|
|
99
|
+
parseSqlCode = module.parseCode;
|
|
100
|
+
}),
|
|
101
|
+
import("./parsers/rust-dispatch.mjs").then((module) => {
|
|
102
|
+
parseRustCode = module.parseCode;
|
|
103
|
+
}),
|
|
104
|
+
import("./parsers/python-treesitter.mjs").then((module) => {
|
|
105
|
+
parsePythonCode = module.parseCode;
|
|
106
|
+
}),
|
|
107
|
+
import("./parsers/go-treesitter.mjs").then((module) => {
|
|
108
|
+
parseGoCode = module.parseCode;
|
|
109
|
+
}),
|
|
110
|
+
import("./parsers/java-treesitter.mjs").then((module) => {
|
|
111
|
+
parseJavaCode = module.parseCode;
|
|
112
|
+
}),
|
|
113
|
+
import("./parsers/ruby-treesitter.mjs").then((module) => {
|
|
114
|
+
parseRubyCode = module.parseCode;
|
|
115
|
+
}),
|
|
116
|
+
import("./parsers/bash-treesitter.mjs").then((module) => {
|
|
117
|
+
parseBashCode = module.parseCode;
|
|
118
|
+
}),
|
|
119
|
+
import("./parsers/vb6.mjs").then((module) => {
|
|
120
|
+
parseVb6Code = module.parseCode;
|
|
121
|
+
}),
|
|
122
|
+
import("./parsers/markdown.mjs").then((module) => {
|
|
123
|
+
parseMarkdownCode = module.parseCode;
|
|
124
|
+
})
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
loadPromise = Promise.allSettled(loaders).then(() => undefined);
|
|
128
|
+
return loadPromise;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const CHUNK_PARSERS = new Map([
|
|
132
|
+
[".js", { language: "javascript", parse: parseJavaScriptCode }],
|
|
133
|
+
[".jsx", { language: "jsx", parse: parseJavaScriptCode }],
|
|
134
|
+
[".mjs", { language: "javascript", parse: parseJavaScriptCode }],
|
|
135
|
+
[".cjs", { language: "javascript", parse: parseJavaScriptCode }],
|
|
136
|
+
[".ts", { language: "typescript", parse: parseJavaScriptCode }],
|
|
137
|
+
[".tsx", { language: "tsx", parse: parseJavaScriptCode }],
|
|
138
|
+
[".mts", { language: "typescript", parse: parseJavaScriptCode }],
|
|
139
|
+
[".cts", { language: "typescript", parse: parseJavaScriptCode }],
|
|
140
|
+
[
|
|
141
|
+
".vb",
|
|
142
|
+
{
|
|
143
|
+
language: "vbnet",
|
|
144
|
+
parse: (...args) => parseVbNetCode(...args),
|
|
145
|
+
isAvailable: () => typeof parseVbNetCode === "function" && isVbNetParserAvailable()
|
|
146
|
+
}
|
|
147
|
+
],
|
|
148
|
+
[
|
|
149
|
+
".cs",
|
|
150
|
+
{
|
|
151
|
+
language: "csharp",
|
|
152
|
+
parse: (...args) => parseCSharpCode(...args),
|
|
153
|
+
isAvailable: () => typeof parseCSharpCode === "function" && isCSharpParserAvailableImpl()
|
|
154
|
+
}
|
|
155
|
+
],
|
|
156
|
+
[
|
|
157
|
+
".sql",
|
|
158
|
+
{
|
|
159
|
+
language: "sql",
|
|
160
|
+
parse: (...args) => parseSqlCode(...args),
|
|
161
|
+
isAvailable: () => typeof parseSqlCode === "function"
|
|
162
|
+
}
|
|
163
|
+
],
|
|
164
|
+
[
|
|
165
|
+
".md",
|
|
166
|
+
{
|
|
167
|
+
language: "markdown",
|
|
168
|
+
parse: (...args) => parseMarkdownCode(...args),
|
|
169
|
+
isAvailable: () => typeof parseMarkdownCode === "function"
|
|
170
|
+
}
|
|
171
|
+
],
|
|
172
|
+
[
|
|
173
|
+
".mdx",
|
|
174
|
+
{
|
|
175
|
+
language: "markdown",
|
|
176
|
+
parse: (...args) => parseMarkdownCode(...args),
|
|
177
|
+
isAvailable: () => typeof parseMarkdownCode === "function"
|
|
178
|
+
}
|
|
179
|
+
],
|
|
180
|
+
[
|
|
181
|
+
".config",
|
|
182
|
+
{
|
|
183
|
+
language: "config",
|
|
184
|
+
parse: (...args) => parseConfigCode(...args),
|
|
185
|
+
isAvailable: () => typeof parseConfigCode === "function"
|
|
186
|
+
}
|
|
187
|
+
],
|
|
188
|
+
[
|
|
189
|
+
".resx",
|
|
190
|
+
{
|
|
191
|
+
language: "resource",
|
|
192
|
+
parse: (...args) => parseResourcesCode(...args),
|
|
193
|
+
isAvailable: () => typeof parseResourcesCode === "function"
|
|
194
|
+
}
|
|
195
|
+
],
|
|
196
|
+
[
|
|
197
|
+
".settings",
|
|
198
|
+
{
|
|
199
|
+
language: "settings",
|
|
200
|
+
parse: (...args) => parseResourcesCode(...args),
|
|
201
|
+
isAvailable: () => typeof parseResourcesCode === "function"
|
|
202
|
+
}
|
|
203
|
+
],
|
|
204
|
+
[
|
|
205
|
+
".c",
|
|
206
|
+
{
|
|
207
|
+
language: "c",
|
|
208
|
+
parse: (...args) => parseCppCode(...args),
|
|
209
|
+
isAvailable: () => typeof parseCppCode === "function" && isCppParserAvailable()
|
|
210
|
+
}
|
|
211
|
+
],
|
|
212
|
+
[
|
|
213
|
+
".h",
|
|
214
|
+
{
|
|
215
|
+
language: "c",
|
|
216
|
+
parse: (...args) => parseCppCode(...args),
|
|
217
|
+
isAvailable: () => typeof parseCppCode === "function" && isCppParserAvailable()
|
|
218
|
+
}
|
|
219
|
+
],
|
|
220
|
+
[
|
|
221
|
+
".cpp",
|
|
222
|
+
{
|
|
223
|
+
language: "cpp",
|
|
224
|
+
parse: (...args) => parseCppCode(...args),
|
|
225
|
+
isAvailable: () => typeof parseCppCode === "function" && isCppParserAvailable()
|
|
226
|
+
}
|
|
227
|
+
],
|
|
228
|
+
[
|
|
229
|
+
".cc",
|
|
230
|
+
{
|
|
231
|
+
language: "cpp",
|
|
232
|
+
parse: (...args) => parseCppCode(...args),
|
|
233
|
+
isAvailable: () => typeof parseCppCode === "function" && isCppParserAvailable()
|
|
234
|
+
}
|
|
235
|
+
],
|
|
236
|
+
[
|
|
237
|
+
".hpp",
|
|
238
|
+
{
|
|
239
|
+
language: "cpp",
|
|
240
|
+
parse: (...args) => parseCppCode(...args),
|
|
241
|
+
isAvailable: () => typeof parseCppCode === "function" && isCppParserAvailable()
|
|
242
|
+
}
|
|
243
|
+
],
|
|
244
|
+
[
|
|
245
|
+
".hh",
|
|
246
|
+
{
|
|
247
|
+
language: "cpp",
|
|
248
|
+
parse: (...args) => parseCppCode(...args),
|
|
249
|
+
isAvailable: () => typeof parseCppCode === "function" && isCppParserAvailable()
|
|
250
|
+
}
|
|
251
|
+
],
|
|
252
|
+
[
|
|
253
|
+
".rs",
|
|
254
|
+
{
|
|
255
|
+
language: "rust",
|
|
256
|
+
parse: (...args) => parseRustCode(...args),
|
|
257
|
+
isAvailable: () => typeof parseRustCode === "function"
|
|
258
|
+
}
|
|
259
|
+
],
|
|
260
|
+
[
|
|
261
|
+
".py",
|
|
262
|
+
{
|
|
263
|
+
language: "python",
|
|
264
|
+
parse: (...args) => parsePythonCode(...args),
|
|
265
|
+
isAvailable: () => typeof parsePythonCode === "function"
|
|
266
|
+
}
|
|
267
|
+
],
|
|
268
|
+
[
|
|
269
|
+
".go",
|
|
270
|
+
{
|
|
271
|
+
language: "go",
|
|
272
|
+
parse: (...args) => parseGoCode(...args),
|
|
273
|
+
isAvailable: () => typeof parseGoCode === "function"
|
|
274
|
+
}
|
|
275
|
+
],
|
|
276
|
+
[
|
|
277
|
+
".java",
|
|
278
|
+
{
|
|
279
|
+
language: "java",
|
|
280
|
+
parse: (...args) => parseJavaCode(...args),
|
|
281
|
+
isAvailable: () => typeof parseJavaCode === "function"
|
|
282
|
+
}
|
|
283
|
+
],
|
|
284
|
+
[
|
|
285
|
+
".rb",
|
|
286
|
+
{
|
|
287
|
+
language: "ruby",
|
|
288
|
+
parse: (...args) => parseRubyCode(...args),
|
|
289
|
+
isAvailable: () => typeof parseRubyCode === "function"
|
|
290
|
+
}
|
|
291
|
+
],
|
|
292
|
+
[
|
|
293
|
+
".sh",
|
|
294
|
+
{
|
|
295
|
+
language: "bash",
|
|
296
|
+
parse: (...args) => parseBashCode(...args),
|
|
297
|
+
isAvailable: () => typeof parseBashCode === "function"
|
|
298
|
+
}
|
|
299
|
+
],
|
|
300
|
+
[
|
|
301
|
+
".bash",
|
|
302
|
+
{
|
|
303
|
+
language: "bash",
|
|
304
|
+
parse: (...args) => parseBashCode(...args),
|
|
305
|
+
isAvailable: () => typeof parseBashCode === "function"
|
|
306
|
+
}
|
|
307
|
+
],
|
|
308
|
+
[
|
|
309
|
+
".zsh",
|
|
310
|
+
{
|
|
311
|
+
language: "bash",
|
|
312
|
+
parse: (...args) => parseBashCode(...args),
|
|
313
|
+
isAvailable: () => typeof parseBashCode === "function"
|
|
314
|
+
}
|
|
315
|
+
],
|
|
316
|
+
[
|
|
317
|
+
".bas",
|
|
318
|
+
{
|
|
319
|
+
language: "vb6",
|
|
320
|
+
parse: (...args) => parseVb6Code(...args),
|
|
321
|
+
isAvailable: () => typeof parseVb6Code === "function"
|
|
322
|
+
}
|
|
323
|
+
],
|
|
324
|
+
[
|
|
325
|
+
".cls",
|
|
326
|
+
{
|
|
327
|
+
language: "vb6",
|
|
328
|
+
parse: (...args) => parseVb6Code(...args),
|
|
329
|
+
isAvailable: () => typeof parseVb6Code === "function"
|
|
330
|
+
}
|
|
331
|
+
],
|
|
332
|
+
[
|
|
333
|
+
".frm",
|
|
334
|
+
{
|
|
335
|
+
language: "vb6",
|
|
336
|
+
parse: (...args) => parseVb6Code(...args),
|
|
337
|
+
isAvailable: () => typeof parseVb6Code === "function"
|
|
338
|
+
}
|
|
339
|
+
],
|
|
340
|
+
[
|
|
341
|
+
".ctl",
|
|
342
|
+
{
|
|
343
|
+
language: "vb6",
|
|
344
|
+
parse: (...args) => parseVb6Code(...args),
|
|
345
|
+
isAvailable: () => typeof parseVb6Code === "function"
|
|
346
|
+
}
|
|
347
|
+
]
|
|
348
|
+
]);
|
|
349
|
+
|
|
350
|
+
export function getChunkParserForExtension(ext) {
|
|
351
|
+
return CHUNK_PARSERS.get(ext) ?? null;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Stable wrappers so callers can import these once; they delegate to whatever
|
|
355
|
+
// the dynamic import populated.
|
|
356
|
+
export function isCSharpParserAvailable() {
|
|
357
|
+
return isCSharpParserAvailableImpl();
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export function getCSharpParserRuntime() {
|
|
361
|
+
return getCSharpParserRuntimeImpl();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export function parseCSharpProject(inputs) {
|
|
365
|
+
if (typeof parseCSharpProjectImpl !== "function") {
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
return parseCSharpProjectImpl(inputs);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function hasCSharpProjectParser() {
|
|
372
|
+
return typeof parseCSharpProjectImpl === "function";
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Parse a single file with the registered parser for its extension. Used by
|
|
376
|
+
// the worker; returns the parser's { chunks, errors } result, or null if no
|
|
377
|
+
// parser is registered or it is unavailable in this process.
|
|
378
|
+
export async function parseFileContent(ext, content, filePath) {
|
|
379
|
+
const parser = getChunkParserForExtension(ext);
|
|
380
|
+
if (!parser) {
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
if (typeof parser.isAvailable === "function" && !(await parser.isAvailable())) {
|
|
384
|
+
return null;
|
|
385
|
+
}
|
|
386
|
+
return { language: parser.language, result: await parser.parse(content, filePath, parser.language) };
|
|
387
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ingest worker thread.
|
|
3
|
+
*
|
|
4
|
+
* Runs the pure, parallel-safe parse step (tree-sitter / acorn / regex
|
|
5
|
+
* parsers) for one file at a time off the main thread. It does nothing
|
|
6
|
+
* stateful: no id allocation, windowing, checksums, or relation building —
|
|
7
|
+
* all of that stays on the main thread in deterministic order. The worker
|
|
8
|
+
* only turns (ext, file path) into a parse result.
|
|
9
|
+
*
|
|
10
|
+
* Parsers initialize lazily on first use and cache per module instance, so a
|
|
11
|
+
* long-lived worker pays each grammar's WASM init once.
|
|
12
|
+
*/
|
|
13
|
+
import fs from "node:fs";
|
|
14
|
+
import { parentPort } from "node:worker_threads";
|
|
15
|
+
import { loadParsers, parseFileContent } from "./ingest-parsers.mjs";
|
|
16
|
+
|
|
17
|
+
if (!parentPort) {
|
|
18
|
+
throw new Error("ingest-worker.mjs must be run as a worker thread");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ready = loadParsers();
|
|
22
|
+
|
|
23
|
+
parentPort.on("message", async (message) => {
|
|
24
|
+
if (message && message.type === "shutdown") {
|
|
25
|
+
process.exit(0);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { taskId, ext, filePath } = message;
|
|
29
|
+
try {
|
|
30
|
+
await ready;
|
|
31
|
+
let content = typeof message.content === "string" ? message.content : null;
|
|
32
|
+
if (content === null) {
|
|
33
|
+
const limit = Number.isFinite(message.contentLimit) ? Math.max(0, Math.floor(message.contentLimit)) : null;
|
|
34
|
+
content = fs.readFileSync(message.absolutePath, "utf8");
|
|
35
|
+
if (limit !== null) {
|
|
36
|
+
content = content.slice(0, limit);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const parsed = await parseFileContent(ext, content, filePath);
|
|
40
|
+
if (!parsed) {
|
|
41
|
+
parentPort.postMessage({ taskId, ok: false, reason: "no parser available" });
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
parentPort.postMessage({ taskId, ok: true, result: parsed.result });
|
|
45
|
+
} catch (error) {
|
|
46
|
+
parentPort.postMessage({
|
|
47
|
+
taskId,
|
|
48
|
+
ok: false,
|
|
49
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|