@dimassetoid/septum-mcp 0.0.0-stage → 0.2.1
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 +124 -2
- package/bin/septum.ts +4 -0
- package/package.json +70 -4
- package/scripts/postinstall.ts +25 -0
- package/src/cli/commands/check.ts +122 -0
- package/src/cli/commands/hook.ts +268 -0
- package/src/cli/commands/init.ts +553 -0
- package/src/cli/commands/serve.ts +5 -0
- package/src/cli/commands/sync.ts +41 -0
- package/src/cli/commands/topology.ts +161 -0
- package/src/cli/index.ts +167 -0
- package/src/cli/scaffold/harness-config.ts +621 -0
- package/src/cli/templates/hooks.ts +75 -0
- package/src/cli/templates/rules.ts +50 -0
- package/src/cli/templates/skill.ts +51 -0
- package/src/cli/ui/prompts.ts +122 -0
- package/src/core/boundary/evaluator.ts +442 -0
- package/src/core/boundary/staged-auditor.ts +58 -0
- package/src/core/config/loader.ts +122 -0
- package/src/core/config/schema.ts +69 -0
- package/src/core/database/client.ts +60 -0
- package/src/core/database/repositories/dependency.repository.ts +712 -0
- package/src/core/database/repositories/domain.repository.ts +330 -0
- package/src/core/database/repositories/environment.repository.ts +207 -0
- package/src/core/database/repositories/file.repository.ts +117 -0
- package/src/core/database/repositories/meta.repository.ts +33 -0
- package/src/core/database/repositories/symbol.repository.ts +508 -0
- package/src/core/database/repository.ts +303 -0
- package/src/core/database/schema.ts +154 -0
- package/src/core/discovery/topology-detector.ts +805 -0
- package/src/core/ingestion/hasher.ts +5 -0
- package/src/core/ingestion/pipeline.ts +623 -0
- package/src/core/parser/behavioral-analyzer.ts +119 -0
- package/src/core/parser/boundary-tracker.ts +443 -0
- package/src/core/parser/extractors/base.ts +67 -0
- package/src/core/parser/extractors/blade.ts +159 -0
- package/src/core/parser/extractors/csharp.ts +244 -0
- package/src/core/parser/extractors/django-semantic.ts +687 -0
- package/src/core/parser/extractors/elysia-semantic.ts +538 -0
- package/src/core/parser/extractors/express-semantic.ts +444 -0
- package/src/core/parser/extractors/fastapi-semantic.ts +736 -0
- package/src/core/parser/extractors/fastify-semantic.ts +774 -0
- package/src/core/parser/extractors/flask-semantic.ts +515 -0
- package/src/core/parser/extractors/frontend.ts +149 -0
- package/src/core/parser/extractors/go-semantic.ts +611 -0
- package/src/core/parser/extractors/go.ts +491 -0
- package/src/core/parser/extractors/hono-semantic.ts +567 -0
- package/src/core/parser/extractors/java.ts +225 -0
- package/src/core/parser/extractors/laravel-semantic.ts +1122 -0
- package/src/core/parser/extractors/mcp-cli-semantic.ts +368 -0
- package/src/core/parser/extractors/nestjs-semantic.ts +543 -0
- package/src/core/parser/extractors/php.ts +569 -0
- package/src/core/parser/extractors/python.ts +498 -0
- package/src/core/parser/extractors/registry.ts +78 -0
- package/src/core/parser/extractors/ruby.ts +234 -0
- package/src/core/parser/extractors/rust.ts +286 -0
- package/src/core/parser/extractors/semantic-extractor.interface.ts +22 -0
- package/src/core/parser/extractors/symfony-semantic.ts +354 -0
- package/src/core/parser/extractors/typescript.ts +826 -0
- package/src/core/parser/language-capabilities.ts +71 -0
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +216 -0
- package/src/core/parser/schema/providers/prisma-schema-provider.ts +90 -0
- package/src/core/parser/schema/schema-provider-registry.ts +43 -0
- package/src/core/parser/schema/schema-provider.interface.ts +33 -0
- package/src/core/parser/tree-sitter.ts +63 -0
- package/src/core/parser/wasm-assets.ts +25 -0
- package/src/core/parser/wasm-loader.ts +274 -0
- package/src/core/resolver/call-graph-tracer.ts +724 -0
- package/src/core/resolver/import-resolver.ts +437 -0
- package/src/core/resolver/module-resolver.ts +482 -0
- package/src/core/resolver/path-resolver.ts +200 -0
- package/src/core/resolver/symbol-locator.ts +276 -0
- package/src/core/resolver/symbol-reference.ts +53 -0
- package/src/core/resolver/vertical-slice-tracer.ts +366 -0
- package/src/core/resources/ignore.ts +123 -0
- package/src/core/resources/limits.ts +14 -0
- package/src/core/resources/source-files.ts +62 -0
- package/src/core/resources/work-queue.ts +74 -0
- package/src/core/session/session-manager.ts +107 -0
- package/src/core/telemetry/telemetry.ts +97 -0
- package/src/core/topology/environment-detector.ts +973 -0
- package/src/core/topology/types.ts +83 -0
- package/src/index.ts +21 -0
- package/src/mcp/schemas.ts +1105 -0
- package/src/mcp/server.ts +774 -0
- package/src/mcp/tools/boundary.ts +17 -0
- package/src/mcp/tools/check-boundary.ts +387 -0
- package/src/mcp/tools/domain.ts +40 -0
- package/src/mcp/tools/feature.ts +67 -0
- package/src/mcp/tools/get-domain-catalog.ts +226 -0
- package/src/mcp/tools/get-environment-topology.ts +138 -0
- package/src/mcp/tools/get-feature-context.ts +224 -0
- package/src/mcp/tools/get-symbol-hotspots.ts +54 -0
- package/src/mcp/tools/get-symbol-impact.ts +64 -0
- package/src/mcp/tools/get-symbol.ts +175 -0
- package/src/mcp/tools/locate-symbol.ts +196 -0
- package/src/mcp/tools/register-domain.ts +75 -0
- package/src/mcp/tools/register-feature.ts +110 -0
- package/src/mcp/tools/slice.ts +16 -0
- package/src/mcp/tools/symbol.ts +93 -0
- package/src/mcp/tools/trace-vertical-slice.ts +80 -0
- package/src/mcp/workspace-manager.ts +159 -0
- package/src/mcp/workspace-watcher.ts +202 -0
- package/src/types/index.ts +456 -0
- package/src/types/wasm.d.ts +4 -0
- package/src/utils/version-check.ts +209 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
import { getLanguageCapability } from "../language-capabilities.ts";
|
|
2
|
+
import type { ExtractedDependency, ExtractedSymbol, ParsedFileAST, Visibility } from "../../../types/index.ts";
|
|
3
|
+
import type { CodeExtractor } from "./base.ts";
|
|
4
|
+
|
|
5
|
+
const PYTHON_STDLIB_AND_EXTERNALS = new Set([
|
|
6
|
+
"os",
|
|
7
|
+
"sys",
|
|
8
|
+
"json",
|
|
9
|
+
"math",
|
|
10
|
+
"typing",
|
|
11
|
+
"re",
|
|
12
|
+
"datetime",
|
|
13
|
+
"time",
|
|
14
|
+
"collections",
|
|
15
|
+
"itertools",
|
|
16
|
+
"functools",
|
|
17
|
+
"pathlib",
|
|
18
|
+
"io",
|
|
19
|
+
"logging",
|
|
20
|
+
"subprocess",
|
|
21
|
+
"threading",
|
|
22
|
+
"multiprocessing",
|
|
23
|
+
"asyncio",
|
|
24
|
+
"abc",
|
|
25
|
+
"copy",
|
|
26
|
+
"enum",
|
|
27
|
+
"dataclasses",
|
|
28
|
+
"uuid",
|
|
29
|
+
"hashlib",
|
|
30
|
+
"random",
|
|
31
|
+
"socket",
|
|
32
|
+
"unittest",
|
|
33
|
+
// Common 3rd party frameworks
|
|
34
|
+
"pydantic",
|
|
35
|
+
"fastapi",
|
|
36
|
+
"django",
|
|
37
|
+
"flask",
|
|
38
|
+
"sqlalchemy",
|
|
39
|
+
"celery",
|
|
40
|
+
"numpy",
|
|
41
|
+
"pandas",
|
|
42
|
+
"requests",
|
|
43
|
+
"httpx",
|
|
44
|
+
"pytest",
|
|
45
|
+
"starlette",
|
|
46
|
+
"alembic",
|
|
47
|
+
"redis",
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
import type Parser from "web-tree-sitter";
|
|
51
|
+
import {
|
|
52
|
+
blankCommentsAndStrings,
|
|
53
|
+
calculatePythonNesting,
|
|
54
|
+
findPythonBlockEnd,
|
|
55
|
+
} from "../boundary-tracker.ts";
|
|
56
|
+
import { calculateAstControlFlowNesting, getNodeHeader, parseSourceWithTreeSitter } from "../wasm-loader.ts";
|
|
57
|
+
|
|
58
|
+
const PYTHON_CONTROL_FLOW_NODES = new Set([
|
|
59
|
+
"if_statement",
|
|
60
|
+
"for_statement",
|
|
61
|
+
"while_statement",
|
|
62
|
+
"try_statement",
|
|
63
|
+
"with_statement",
|
|
64
|
+
"match_statement",
|
|
65
|
+
"conditional_expression",
|
|
66
|
+
]);
|
|
67
|
+
|
|
68
|
+
const PYTHON_FUNCTION_BOUNDARY_NODES = new Set([
|
|
69
|
+
"function_definition",
|
|
70
|
+
"lambda",
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
export class PythonExtractor implements CodeExtractor {
|
|
74
|
+
public canHandle(filePath: string): boolean {
|
|
75
|
+
return getLanguageCapability(filePath)?.extractor === "python";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public async extract(_filePath: string, content: string): Promise<ParsedFileAST> {
|
|
79
|
+
const astResult = await parseSourceWithTreeSitter("python", content, (rootNode) =>
|
|
80
|
+
this.extractFromAst(rootNode, content),
|
|
81
|
+
() => this.extractFallback(content)
|
|
82
|
+
);
|
|
83
|
+
if (astResult) {
|
|
84
|
+
return astResult;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return { ...this.extractFallback(content), status: "degraded" };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private extractFallback(content: string): ParsedFileAST {
|
|
91
|
+
const symbols: ExtractedSymbol[] = [];
|
|
92
|
+
const dependencies: ExtractedDependency[] = [];
|
|
93
|
+
|
|
94
|
+
const lines = content.split("\n");
|
|
95
|
+
const cleanLines = blankCommentsAndStrings(content, { hashComments: true }).split("\n");
|
|
96
|
+
|
|
97
|
+
// 1. Extract Python Imports
|
|
98
|
+
for (let i = 0; i < lines.length; i++) {
|
|
99
|
+
const line = lines[i];
|
|
100
|
+
const trimmed = cleanLines[i].trim();
|
|
101
|
+
const rawTrimmed = line.trim();
|
|
102
|
+
const lineNum = i + 1;
|
|
103
|
+
|
|
104
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
105
|
+
|
|
106
|
+
// Pattern A: from <module> import <symbols>
|
|
107
|
+
const fromMatch = /^from\s+([.\w]+)\s+import\s+([*\w\s,()]+)/.exec(trimmed);
|
|
108
|
+
if (fromMatch) {
|
|
109
|
+
const modulePath = fromMatch[1];
|
|
110
|
+
const importedList = fromMatch[2]
|
|
111
|
+
.replace(/[()]/g, " ")
|
|
112
|
+
.split(",")
|
|
113
|
+
.map((s) => s.trim().split(/\s+as\s+/)[0].trim())
|
|
114
|
+
.filter((s) => Boolean(s) && s !== "*");
|
|
115
|
+
const isRelative = modulePath.startsWith(".");
|
|
116
|
+
const topPackage = isRelative ? "" : modulePath.split(".")[0];
|
|
117
|
+
const isExternal = !isRelative && PYTHON_STDLIB_AND_EXTERNALS.has(topPackage);
|
|
118
|
+
|
|
119
|
+
dependencies.push({
|
|
120
|
+
target: modulePath,
|
|
121
|
+
statement: rawTrimmed,
|
|
122
|
+
line_number: lineNum,
|
|
123
|
+
is_external: isExternal,
|
|
124
|
+
...(importedList.length > 0 ? { imported_symbols: importedList } : {}),
|
|
125
|
+
});
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Pattern B: import <module> [as <alias>]
|
|
130
|
+
const importMatch = /^import\s+([.\w]+)(?:\s+as\s+(\w+))?/.exec(trimmed);
|
|
131
|
+
if (importMatch) {
|
|
132
|
+
const modulePath = importMatch[1];
|
|
133
|
+
const alias = importMatch[2];
|
|
134
|
+
const topPackage = modulePath.split(".")[0];
|
|
135
|
+
const isExternal = PYTHON_STDLIB_AND_EXTERNALS.has(topPackage);
|
|
136
|
+
const shortName = alias || modulePath.split(".").pop() || modulePath;
|
|
137
|
+
|
|
138
|
+
dependencies.push({
|
|
139
|
+
target: modulePath,
|
|
140
|
+
statement: rawTrimmed,
|
|
141
|
+
line_number: lineNum,
|
|
142
|
+
is_external: isExternal,
|
|
143
|
+
imported_symbols: [shortName],
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
// 2. Extract Python Classes and Functions
|
|
150
|
+
let currentClass: string | null = null;
|
|
151
|
+
let currentClassIndent = -1;
|
|
152
|
+
|
|
153
|
+
for (let i = 0; i < lines.length; i++) {
|
|
154
|
+
const line = lines[i];
|
|
155
|
+
const trimmed = line.trim();
|
|
156
|
+
const lineNum = i + 1;
|
|
157
|
+
|
|
158
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
159
|
+
|
|
160
|
+
const indentMatch = /^(\s*)/.exec(line);
|
|
161
|
+
const indent = indentMatch ? indentMatch[1].length : 0;
|
|
162
|
+
|
|
163
|
+
// Check if we exited current class scope
|
|
164
|
+
if (currentClass !== null && indent <= currentClassIndent) {
|
|
165
|
+
currentClass = null;
|
|
166
|
+
currentClassIndent = -1;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Class Definition
|
|
170
|
+
const classMatch = /^class\s+([A-Za-z0-9_]+)(?:\s*\([^)]*\))?\s*:/.exec(trimmed);
|
|
171
|
+
if (classMatch) {
|
|
172
|
+
const className = classMatch[1];
|
|
173
|
+
currentClass = className;
|
|
174
|
+
currentClassIndent = indent;
|
|
175
|
+
|
|
176
|
+
const visibility: Visibility = className.startsWith("__") && className.endsWith("__")
|
|
177
|
+
? "public"
|
|
178
|
+
: className.startsWith("__")
|
|
179
|
+
? "private"
|
|
180
|
+
: className.startsWith("_")
|
|
181
|
+
? "protected"
|
|
182
|
+
: "public";
|
|
183
|
+
|
|
184
|
+
const endLine = findPythonBlockEnd(lines, i);
|
|
185
|
+
symbols.push({
|
|
186
|
+
name: className,
|
|
187
|
+
kind: "class",
|
|
188
|
+
signature: trimmed.replace(/:$/, "").trim(),
|
|
189
|
+
visibility,
|
|
190
|
+
line_start: lineNum,
|
|
191
|
+
line_end: endLine,
|
|
192
|
+
line_count: Math.max(1, endLine - lineNum + 1),
|
|
193
|
+
});
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Class Attributes or Pydantic fields inside class
|
|
198
|
+
if (currentClass !== null && indent > currentClassIndent) {
|
|
199
|
+
const attrMatch = /^([A-Za-z0-9_]+)\s*(?::\s*([^=]+))?(?:\s*=\s*(.+))?$/.exec(trimmed);
|
|
200
|
+
if (
|
|
201
|
+
attrMatch &&
|
|
202
|
+
!trimmed.startsWith("def ") &&
|
|
203
|
+
!trimmed.startsWith("async def ") &&
|
|
204
|
+
!["return", "pass", "raise", "assert", "import", "from", "yield"].includes(attrMatch[1])
|
|
205
|
+
) {
|
|
206
|
+
const attrName = attrMatch[1];
|
|
207
|
+
const visibility: Visibility = attrName.startsWith("__")
|
|
208
|
+
? "private"
|
|
209
|
+
: attrName.startsWith("_")
|
|
210
|
+
? "protected"
|
|
211
|
+
: "public";
|
|
212
|
+
|
|
213
|
+
symbols.push({
|
|
214
|
+
name: `${currentClass}::${attrName}`,
|
|
215
|
+
kind: "property",
|
|
216
|
+
signature: trimmed,
|
|
217
|
+
visibility,
|
|
218
|
+
line_start: lineNum,
|
|
219
|
+
line_end: lineNum,
|
|
220
|
+
line_count: 1,
|
|
221
|
+
});
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Module-level UPPERCASE constants: MAX_RETRIES = 5
|
|
227
|
+
if (currentClass === null && indent === 0) {
|
|
228
|
+
const constMatch = /^([A-Z][A-Z0-9_]*)\s*(?::\s*([^=]+))?\s*=\s*(.+)$/.exec(trimmed);
|
|
229
|
+
if (constMatch) {
|
|
230
|
+
const constName = constMatch[1];
|
|
231
|
+
symbols.push({
|
|
232
|
+
name: constName,
|
|
233
|
+
kind: "property",
|
|
234
|
+
signature: trimmed,
|
|
235
|
+
visibility: "public",
|
|
236
|
+
line_start: lineNum,
|
|
237
|
+
line_end: lineNum,
|
|
238
|
+
line_count: 1,
|
|
239
|
+
});
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Function or Method Definition
|
|
245
|
+
const funcMatch = /^(?:async\s+)?def\s+([A-Za-z0-9_]+)\s*\(([^)]*)\)(?:\s*->\s*[^:]+)?\s*:/.exec(trimmed);
|
|
246
|
+
if (funcMatch) {
|
|
247
|
+
const funcName = funcMatch[1];
|
|
248
|
+
const isMethod = currentClass !== null && indent > currentClassIndent;
|
|
249
|
+
|
|
250
|
+
const visibility: Visibility = funcName.startsWith("__") && funcName.endsWith("__")
|
|
251
|
+
? "public"
|
|
252
|
+
: funcName.startsWith("__")
|
|
253
|
+
? "private"
|
|
254
|
+
: funcName.startsWith("_")
|
|
255
|
+
? "protected"
|
|
256
|
+
: "public";
|
|
257
|
+
|
|
258
|
+
const endLine = findPythonBlockEnd(cleanLines, i);
|
|
259
|
+
const nestingDepth = calculatePythonNesting(cleanLines, i, endLine - 1);
|
|
260
|
+
symbols.push({
|
|
261
|
+
name: isMethod ? `${currentClass}::${funcName}` : funcName,
|
|
262
|
+
kind: isMethod ? "method" : "function",
|
|
263
|
+
signature: trimmed.replace(/:$/, "").trim(),
|
|
264
|
+
visibility,
|
|
265
|
+
line_start: lineNum,
|
|
266
|
+
line_end: endLine,
|
|
267
|
+
line_count: Math.max(1, endLine - lineNum + 1),
|
|
268
|
+
nesting_depth: nestingDepth,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return {
|
|
274
|
+
symbols,
|
|
275
|
+
dependencies,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private extractFromAst(rootNode: Parser.SyntaxNode, content: string): ParsedFileAST {
|
|
280
|
+
const symbols: ExtractedSymbol[] = [];
|
|
281
|
+
const dependencies: ExtractedDependency[] = [];
|
|
282
|
+
const lines = content.split("\n");
|
|
283
|
+
|
|
284
|
+
const getVisibility = (name: string): Visibility =>
|
|
285
|
+
name.startsWith("__") && name.endsWith("__")
|
|
286
|
+
? "public"
|
|
287
|
+
: name.startsWith("__")
|
|
288
|
+
? "private"
|
|
289
|
+
: name.startsWith("_")
|
|
290
|
+
? "protected"
|
|
291
|
+
: "public";
|
|
292
|
+
|
|
293
|
+
const processNode = (node: Parser.SyntaxNode, currentClass: string | null, pendingDecorators?: string[]): void => {
|
|
294
|
+
if (node.type === "import_from_statement") {
|
|
295
|
+
const modNode =
|
|
296
|
+
node.childForFieldName("module_name") ??
|
|
297
|
+
node.namedChildren.find((c) => c.type === "dotted_name" || c.type === "relative_import");
|
|
298
|
+
if (modNode) {
|
|
299
|
+
const modulePath = modNode.text.trim();
|
|
300
|
+
const isRelative = modulePath.startsWith(".");
|
|
301
|
+
const topPackage = isRelative ? "" : modulePath.split(".")[0];
|
|
302
|
+
const isExternal = !isRelative && PYTHON_STDLIB_AND_EXTERNALS.has(topPackage);
|
|
303
|
+
const importedSymbols: string[] = [];
|
|
304
|
+
|
|
305
|
+
for (const child of node.namedChildren) {
|
|
306
|
+
if (child.equals(modNode)) continue;
|
|
307
|
+
if (child.type === "dotted_name") {
|
|
308
|
+
importedSymbols.push(child.text.trim());
|
|
309
|
+
} else if (child.type === "aliased_import") {
|
|
310
|
+
const orig = child.childForFieldName("name") ?? child.namedChildren[0];
|
|
311
|
+
const alias = child.childForFieldName("alias") ?? child.namedChildren[1];
|
|
312
|
+
if (orig?.text) importedSymbols.push(orig.text.trim());
|
|
313
|
+
if (alias?.text) importedSymbols.push(alias.text.trim());
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
dependencies.push({
|
|
318
|
+
target: modulePath,
|
|
319
|
+
statement: (lines[node.startPosition.row] ?? node.text).trim(),
|
|
320
|
+
line_number: node.startPosition.row + 1,
|
|
321
|
+
is_external: isExternal,
|
|
322
|
+
...(importedSymbols.length > 0 ? { imported_symbols: Array.from(new Set(importedSymbols)) } : {}),
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (node.type === "import_statement") {
|
|
329
|
+
for (const child of node.namedChildren) {
|
|
330
|
+
if (child.type === "dotted_name") {
|
|
331
|
+
const modulePath = child.text.trim();
|
|
332
|
+
const topPackage = modulePath.split(".")[0];
|
|
333
|
+
const shortName = modulePath.split(".").pop() || modulePath;
|
|
334
|
+
dependencies.push({
|
|
335
|
+
target: modulePath,
|
|
336
|
+
statement: (lines[node.startPosition.row] ?? node.text).trim(),
|
|
337
|
+
line_number: node.startPosition.row + 1,
|
|
338
|
+
is_external: PYTHON_STDLIB_AND_EXTERNALS.has(topPackage),
|
|
339
|
+
imported_symbols: [shortName],
|
|
340
|
+
});
|
|
341
|
+
} else if (child.type === "aliased_import") {
|
|
342
|
+
const orig = child.childForFieldName("name") ?? child.namedChildren[0];
|
|
343
|
+
const alias = child.childForFieldName("alias") ?? child.namedChildren[1];
|
|
344
|
+
if (orig) {
|
|
345
|
+
const modulePath = orig.text.trim();
|
|
346
|
+
const topPackage = modulePath.split(".")[0];
|
|
347
|
+
const shortName = alias?.text?.trim() || modulePath.split(".").pop() || modulePath;
|
|
348
|
+
dependencies.push({
|
|
349
|
+
target: modulePath,
|
|
350
|
+
statement: (lines[node.startPosition.row] ?? node.text).trim(),
|
|
351
|
+
line_number: node.startPosition.row + 1,
|
|
352
|
+
is_external: PYTHON_STDLIB_AND_EXTERNALS.has(topPackage),
|
|
353
|
+
imported_symbols: [shortName],
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (node.type === "decorated_definition") {
|
|
362
|
+
const decorators: string[] = [];
|
|
363
|
+
for (const child of node.namedChildren) {
|
|
364
|
+
if (child.type === "decorator") {
|
|
365
|
+
decorators.push(child.text.trim());
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
const defNode =
|
|
369
|
+
node.childForFieldName("definition") ??
|
|
370
|
+
node.namedChildren.find((c) => c.type === "class_definition" || c.type === "function_definition");
|
|
371
|
+
if (defNode) {
|
|
372
|
+
processNode(defNode, currentClass, decorators);
|
|
373
|
+
}
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (node.type === "class_definition") {
|
|
378
|
+
const nameNode = node.childForFieldName("name") ?? node.namedChildren.find((c) => c.type === "identifier");
|
|
379
|
+
if (!nameNode) return;
|
|
380
|
+
|
|
381
|
+
const className = nameNode.text;
|
|
382
|
+
const qualifiedName = currentClass ? `${currentClass}::${className}` : className;
|
|
383
|
+
const lineStart = node.startPosition.row + 1;
|
|
384
|
+
const lineEnd = Math.max(lineStart, node.endPosition.row + 1);
|
|
385
|
+
const headerLine = getNodeHeader(node).replace(/:$/, "").trim();
|
|
386
|
+
const baseSignature = headerLine || `class ${className}`;
|
|
387
|
+
const signature = pendingDecorators?.length ? `${pendingDecorators.join("\n")}\n${baseSignature}` : baseSignature;
|
|
388
|
+
|
|
389
|
+
symbols.push({
|
|
390
|
+
name: qualifiedName,
|
|
391
|
+
kind: "class",
|
|
392
|
+
signature,
|
|
393
|
+
visibility: getVisibility(className),
|
|
394
|
+
line_start: lineStart,
|
|
395
|
+
line_end: lineEnd,
|
|
396
|
+
line_count: Math.max(1, lineEnd - lineStart + 1),
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
const bodyNode = node.childForFieldName("body") ?? node.namedChildren.find((c) => c.type === "block");
|
|
400
|
+
if (bodyNode) {
|
|
401
|
+
for (const stmt of bodyNode.namedChildren) {
|
|
402
|
+
processNode(stmt, qualifiedName);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (node.type === "function_definition") {
|
|
409
|
+
const nameNode = node.childForFieldName("name") ?? node.namedChildren.find((c) => c.type === "identifier");
|
|
410
|
+
if (!nameNode) return;
|
|
411
|
+
|
|
412
|
+
const funcName = nameNode.text;
|
|
413
|
+
let parentNode = node.parent;
|
|
414
|
+
if (parentNode?.type === "decorated_definition") {
|
|
415
|
+
parentNode = parentNode.parent;
|
|
416
|
+
}
|
|
417
|
+
const isDirectClassMethod = parentNode?.parent?.type === "class_definition";
|
|
418
|
+
const isProperty = pendingDecorators?.some((d) => d === "@property");
|
|
419
|
+
const lineStart = node.startPosition.row + 1;
|
|
420
|
+
const lineEnd = Math.max(lineStart, node.endPosition.row + 1);
|
|
421
|
+
const headerLine = getNodeHeader(node).replace(/:$/, "").trim();
|
|
422
|
+
const baseSignature = headerLine || `def ${funcName}()`;
|
|
423
|
+
const signature = pendingDecorators?.length ? `${pendingDecorators.join("\n")}\n${baseSignature}` : baseSignature;
|
|
424
|
+
const nestingDepth = calculateAstControlFlowNesting(
|
|
425
|
+
node,
|
|
426
|
+
PYTHON_CONTROL_FLOW_NODES,
|
|
427
|
+
PYTHON_FUNCTION_BOUNDARY_NODES
|
|
428
|
+
);
|
|
429
|
+
|
|
430
|
+
symbols.push({
|
|
431
|
+
name: currentClass ? `${currentClass}::${funcName}` : funcName,
|
|
432
|
+
kind: isProperty ? "property" : isDirectClassMethod ? "method" : "function",
|
|
433
|
+
signature,
|
|
434
|
+
visibility: getVisibility(funcName),
|
|
435
|
+
line_start: lineStart,
|
|
436
|
+
line_end: lineEnd,
|
|
437
|
+
line_count: Math.max(1, lineEnd - lineStart + 1),
|
|
438
|
+
nesting_depth: nestingDepth,
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
const bodyNode = node.childForFieldName("body") ?? node.namedChildren.find((c) => c.type === "block");
|
|
442
|
+
if (bodyNode) {
|
|
443
|
+
for (const stmt of bodyNode.namedChildren) {
|
|
444
|
+
if (stmt.type === "function_definition" || stmt.type === "class_definition" || stmt.type === "decorated_definition") {
|
|
445
|
+
const scopeName = currentClass ? `${currentClass}::${funcName}` : funcName;
|
|
446
|
+
processNode(stmt, scopeName);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (node.type === "expression_statement") {
|
|
454
|
+
const assignNode =
|
|
455
|
+
node.namedChildren.find((c) => c.type === "assignment") ??
|
|
456
|
+
(node.firstNamedChild?.type === "assignment" ? node.firstNamedChild : null);
|
|
457
|
+
if (assignNode) {
|
|
458
|
+
const leftNode = assignNode.childForFieldName("left") ?? assignNode.namedChildren[0];
|
|
459
|
+
if (leftNode && leftNode.type === "identifier") {
|
|
460
|
+
const varName = leftNode.text.trim();
|
|
461
|
+
const lineStart = assignNode.startPosition.row + 1;
|
|
462
|
+
const lineEnd = Math.max(lineStart, assignNode.endPosition.row + 1);
|
|
463
|
+
const rawLine = (lines[assignNode.startPosition.row] ?? assignNode.text).trim();
|
|
464
|
+
|
|
465
|
+
if (currentClass !== null) {
|
|
466
|
+
symbols.push({
|
|
467
|
+
name: `${currentClass}::${varName}`,
|
|
468
|
+
kind: "property",
|
|
469
|
+
signature: rawLine,
|
|
470
|
+
visibility: getVisibility(varName),
|
|
471
|
+
line_start: lineStart,
|
|
472
|
+
line_end: lineEnd,
|
|
473
|
+
line_count: Math.max(1, lineEnd - lineStart + 1),
|
|
474
|
+
});
|
|
475
|
+
} else if (/^[A-Z][A-Z0-9_]*$/.test(varName)) {
|
|
476
|
+
symbols.push({
|
|
477
|
+
name: varName,
|
|
478
|
+
kind: "property",
|
|
479
|
+
signature: rawLine,
|
|
480
|
+
visibility: "public",
|
|
481
|
+
line_start: lineStart,
|
|
482
|
+
line_end: lineEnd,
|
|
483
|
+
line_count: Math.max(1, lineEnd - lineStart + 1),
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
for (const child of rootNode.namedChildren) {
|
|
492
|
+
processNode(child, null);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return { symbols, dependencies };
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SemanticSliceExtractor,
|
|
3
|
+
VerticalSliceCandidate,
|
|
4
|
+
} from "./semantic-extractor.interface.ts";
|
|
5
|
+
import { LaravelSemanticExtractor } from "./laravel-semantic.ts";
|
|
6
|
+
import { SymfonySemanticExtractor } from "./symfony-semantic.ts";
|
|
7
|
+
import { McpCliSemanticExtractor } from "./mcp-cli-semantic.ts";
|
|
8
|
+
import { NestJsSemanticExtractor } from "./nestjs-semantic.ts";
|
|
9
|
+
import { FastApiSemanticExtractor } from "./fastapi-semantic.ts";
|
|
10
|
+
import { DjangoSemanticExtractor } from "./django-semantic.ts";
|
|
11
|
+
import { FlaskSemanticExtractor } from "./flask-semantic.ts";
|
|
12
|
+
import { GoSemanticExtractor } from "./go-semantic.ts";
|
|
13
|
+
import { ExpressSemanticExtractor } from "./express-semantic.ts";
|
|
14
|
+
import { FastifySemanticExtractor } from "./fastify-semantic.ts";
|
|
15
|
+
import { HonoSemanticExtractor } from "./hono-semantic.ts";
|
|
16
|
+
import { ElysiaSemanticExtractor } from "./elysia-semantic.ts";
|
|
17
|
+
|
|
18
|
+
export class SemanticSliceExtractorRegistry {
|
|
19
|
+
private static extractors: SemanticSliceExtractor[] = [
|
|
20
|
+
new LaravelSemanticExtractor(),
|
|
21
|
+
new SymfonySemanticExtractor(),
|
|
22
|
+
new McpCliSemanticExtractor(),
|
|
23
|
+
new NestJsSemanticExtractor(),
|
|
24
|
+
new ExpressSemanticExtractor(),
|
|
25
|
+
new FastifySemanticExtractor(),
|
|
26
|
+
new HonoSemanticExtractor(),
|
|
27
|
+
new ElysiaSemanticExtractor(),
|
|
28
|
+
new FastApiSemanticExtractor(),
|
|
29
|
+
new DjangoSemanticExtractor(),
|
|
30
|
+
new FlaskSemanticExtractor(),
|
|
31
|
+
new GoSemanticExtractor(),
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
public static getCapabilities() {
|
|
35
|
+
return this.extractors.map(({ id, name }) => ({ id, name, coverage: "partial" }));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Registers a new semantic slice extractor strategy.
|
|
40
|
+
* Prepends to the list to give higher priority to custom extractors.
|
|
41
|
+
*/
|
|
42
|
+
public static register(extractor: SemanticSliceExtractor): () => void {
|
|
43
|
+
this.extractors.unshift(extractor);
|
|
44
|
+
return () => { this.extractors = this.extractors.filter(item => item !== extractor); };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns all extractors capable of handling the specified project topology.
|
|
49
|
+
*/
|
|
50
|
+
public static getExtractors(projectRoot: string, framework?: string): SemanticSliceExtractor[] {
|
|
51
|
+
return this.extractors.filter((extractor) => extractor.canHandle(projectRoot, framework));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Executes all matched extractors and combines their vertical slices.
|
|
56
|
+
*/
|
|
57
|
+
public static async extractAllSlices(
|
|
58
|
+
projectRoot: string,
|
|
59
|
+
framework?: string,
|
|
60
|
+
domainId?: number,
|
|
61
|
+
signal?: AbortSignal
|
|
62
|
+
): Promise<VerticalSliceCandidate[]> {
|
|
63
|
+
const matched = this.getExtractors(projectRoot, framework);
|
|
64
|
+
const combinedSlices: VerticalSliceCandidate[] = [];
|
|
65
|
+
|
|
66
|
+
for (const extractor of matched) {
|
|
67
|
+
signal?.throwIfAborted();
|
|
68
|
+
let slices: VerticalSliceCandidate[];
|
|
69
|
+
try { slices = await extractor.extractSlices(projectRoot, domainId, signal); }
|
|
70
|
+
catch (error) { signal?.throwIfAborted(); throw new Error(`Semantic adapter '${extractor.id}' failed: ${error instanceof Error ? error.message : String(error)}`); }
|
|
71
|
+
signal?.throwIfAborted();
|
|
72
|
+
combinedSlices.push(...slices);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return combinedSlices;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|