@dimassetoid/septum-mcp 0.0.0-stage → 0.3.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 +142 -2
- package/bin/septum.ts +4 -0
- package/package.json +70 -4
- package/scripts/postinstall.ts +25 -0
- package/src/cli/commands/check.ts +133 -0
- package/src/cli/commands/hook.ts +205 -0
- package/src/cli/commands/init.ts +570 -0
- package/src/cli/commands/serve.ts +5 -0
- package/src/cli/commands/sync.ts +39 -0
- package/src/cli/hook-payload.ts +43 -0
- package/src/cli/index.ts +124 -0
- package/src/cli/scaffold/harness-config.ts +661 -0
- package/src/cli/templates/hooks.ts +110 -0
- package/src/cli/templates/rules.ts +35 -0
- package/src/cli/templates/skill.ts +55 -0
- package/src/cli/ui/prompts.ts +122 -0
- package/src/core/boundary/evaluator.ts +426 -0
- package/src/core/boundary/staged-auditor.ts +120 -0
- package/src/core/config/domain-ownership.ts +45 -0
- package/src/core/config/loader.ts +174 -0
- package/src/core/config/schema.ts +59 -0
- package/src/core/database/client.ts +60 -0
- package/src/core/database/repositories/dependency.repository.ts +733 -0
- package/src/core/database/repositories/domain.repository.ts +373 -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 +647 -0
- package/src/core/database/repository.ts +358 -0
- package/src/core/database/schema.ts +123 -0
- package/src/core/discovery/navigation.ts +105 -0
- package/src/core/discovery/topology-detector.ts +863 -0
- package/src/core/ingestion/hasher.ts +5 -0
- package/src/core/ingestion/pipeline.ts +590 -0
- package/src/core/parser/behavioral-analyzer.ts +123 -0
- package/src/core/parser/boundary-tracker.ts +458 -0
- package/src/core/parser/extractors/base.ts +71 -0
- package/src/core/parser/extractors/blade.ts +159 -0
- package/src/core/parser/extractors/csharp.ts +248 -0
- package/src/core/parser/extractors/django-semantic.ts +655 -0
- package/src/core/parser/extractors/elysia-semantic.ts +517 -0
- package/src/core/parser/extractors/express-semantic.ts +429 -0
- package/src/core/parser/extractors/fastapi-semantic.ts +702 -0
- package/src/core/parser/extractors/fastify-semantic.ts +759 -0
- package/src/core/parser/extractors/flask-semantic.ts +482 -0
- package/src/core/parser/extractors/frontend.ts +149 -0
- package/src/core/parser/extractors/go-semantic.ts +670 -0
- package/src/core/parser/extractors/go.ts +491 -0
- package/src/core/parser/extractors/hono-semantic.ts +546 -0
- package/src/core/parser/extractors/java.ts +227 -0
- package/src/core/parser/extractors/laravel-semantic.ts +1150 -0
- package/src/core/parser/extractors/mcp-cli-semantic.ts +367 -0
- package/src/core/parser/extractors/nestjs-semantic.ts +527 -0
- package/src/core/parser/extractors/php.ts +585 -0
- package/src/core/parser/extractors/python.ts +503 -0
- package/src/core/parser/extractors/registry.ts +99 -0
- package/src/core/parser/extractors/ruby.ts +237 -0
- package/src/core/parser/extractors/rust.ts +304 -0
- package/src/core/parser/extractors/semantic-extractor.interface.ts +24 -0
- package/src/core/parser/extractors/symfony-semantic.ts +321 -0
- package/src/core/parser/extractors/typescript.ts +839 -0
- package/src/core/parser/implementation-fingerprint.ts +97 -0
- package/src/core/parser/language-capabilities.ts +87 -0
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +239 -0
- package/src/core/parser/schema/schema-provider-registry.ts +41 -0
- package/src/core/parser/schema/schema-provider.interface.ts +34 -0
- package/src/core/parser/semantic-revision.ts +3 -0
- package/src/core/parser/static-string.ts +51 -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 +1033 -0
- package/src/core/resolver/import-resolver.ts +365 -0
- package/src/core/resolver/interpretation-inputs.ts +7 -0
- package/src/core/resolver/languages/go.ts +103 -0
- package/src/core/resolver/languages/managed.ts +200 -0
- package/src/core/resolver/languages/python.ts +115 -0
- package/src/core/resolver/languages/ruby.ts +40 -0
- package/src/core/resolver/languages/rust.ts +127 -0
- package/src/core/resolver/languages/shared.ts +71 -0
- package/src/core/resolver/module-resolver.ts +486 -0
- package/src/core/resolver/path-resolver.ts +200 -0
- package/src/core/resolver/slice-catalog.ts +79 -0
- package/src/core/resolver/slice-format.ts +9 -0
- package/src/core/resolver/symbol-impact.ts +381 -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 +344 -0
- package/src/core/resources/ignore.ts +144 -0
- package/src/core/resources/limits.ts +14 -0
- package/src/core/resources/source-files.ts +185 -0
- package/src/core/resources/source-inventory.ts +55 -0
- package/src/core/resources/work-queue.ts +74 -0
- package/src/core/retrieval/context-page.ts +109 -0
- package/src/core/retrieval/context.ts +320 -0
- package/src/core/retrieval/resolution-warnings.ts +21 -0
- package/src/core/retrieval/symbol-evidence.ts +97 -0
- package/src/core/retrieval/typescript-references.ts +262 -0
- package/src/core/telemetry/telemetry.ts +142 -0
- package/src/index.ts +18 -0
- package/src/mcp/schemas.ts +746 -0
- package/src/mcp/server.ts +600 -0
- package/src/mcp/tools/boundary.ts +17 -0
- package/src/mcp/tools/check-boundary.ts +382 -0
- package/src/mcp/tools/get-domain-catalog.ts +39 -0
- package/src/mcp/tools/get-symbol-impact.ts +104 -0
- package/src/mcp/tools/get-symbol.ts +399 -0
- package/src/mcp/tools/locate-symbol.ts +236 -0
- package/src/mcp/tools/map.ts +26 -0
- package/src/mcp/tools/register-domain.ts +113 -0
- package/src/mcp/tools/slice.ts +16 -0
- package/src/mcp/tools/symbol.ts +95 -0
- package/src/mcp/tools/trace-vertical-slice.ts +106 -0
- package/src/mcp/workspace-manager.ts +170 -0
- package/src/mcp/workspace-watcher.ts +202 -0
- package/src/types/index.ts +438 -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,527 @@
|
|
|
1
|
+
import { listSourceFiles, type SourceInventory } from "../../resources/source-inventory.ts";
|
|
2
|
+
import { readOptionalSourceFile } from "../../resources/source-files.ts";
|
|
3
|
+
import { parseWithTreeSitter } from "../wasm-loader.ts";
|
|
4
|
+
import { ImportResolver } from "../../resolver/import-resolver.ts";
|
|
5
|
+
import type Parser from "web-tree-sitter";
|
|
6
|
+
import * as fs from "node:fs";
|
|
7
|
+
import * as path from "node:path";
|
|
8
|
+
import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
|
|
9
|
+
|
|
10
|
+
const NEST_HTTP_METHODS: Record<string, string> = {
|
|
11
|
+
Get: "GET",
|
|
12
|
+
Post: "POST",
|
|
13
|
+
Put: "PUT",
|
|
14
|
+
Patch: "PATCH",
|
|
15
|
+
Delete: "DELETE",
|
|
16
|
+
All: "ALL",
|
|
17
|
+
Head: "HEAD",
|
|
18
|
+
Options: "OPTIONS",
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
interface ParsedDecorator {
|
|
22
|
+
name: string;
|
|
23
|
+
args: string[];
|
|
24
|
+
startLine: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class NestJsSemanticExtractor implements SemanticSliceExtractor {
|
|
28
|
+
public readonly id = "nestjs";
|
|
29
|
+
public readonly name = "NestJS Modular Semantic Extractor";
|
|
30
|
+
|
|
31
|
+
public canHandle(projectRoot: string, framework?: string): boolean {
|
|
32
|
+
if (framework === "nestjs") return true;
|
|
33
|
+
|
|
34
|
+
if (fs.existsSync(path.join(projectRoot, "nest-cli.json"))) return true;
|
|
35
|
+
|
|
36
|
+
const pkgPath = path.join(projectRoot, "package.json");
|
|
37
|
+
if (fs.existsSync(pkgPath)) {
|
|
38
|
+
try {
|
|
39
|
+
const pkg = JSON.parse(readOptionalSourceFile(pkgPath, "utf-8"));
|
|
40
|
+
const deps = { ...pkg.dependencies, ...pkg.devDependencies };
|
|
41
|
+
if (deps["@nestjs/core"] || deps["@nestjs/common"]) return true;
|
|
42
|
+
} catch {}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public async extractSlices(
|
|
49
|
+
projectRoot: string,
|
|
50
|
+
domainId?: number,
|
|
51
|
+
signal?: AbortSignal, inventory?: SourceInventory
|
|
52
|
+
): Promise<VerticalSliceCandidate[]> {
|
|
53
|
+
const slices: VerticalSliceCandidate[] = [];
|
|
54
|
+
const srcDir = path.join(projectRoot, "src");
|
|
55
|
+
if (!fs.existsSync(srcDir)) return slices;
|
|
56
|
+
|
|
57
|
+
const resolver = new ImportResolver(projectRoot);
|
|
58
|
+
const controllerFiles = listSourceFiles(srcDir, /\.controller\.(ts|js)$/, inventory);
|
|
59
|
+
|
|
60
|
+
for (const file of controllerFiles) {
|
|
61
|
+
if (signal?.aborted) throw new Error("semantic_extraction_cancelled");
|
|
62
|
+
const relPath = path.relative(projectRoot, file).replace(/\\/g, "/");
|
|
63
|
+
const content = readOptionalSourceFile(file, "utf-8");
|
|
64
|
+
if (!content.trim()) continue;
|
|
65
|
+
|
|
66
|
+
const fileSlices = await parseWithTreeSitter(
|
|
67
|
+
"typescript",
|
|
68
|
+
content,
|
|
69
|
+
(rootNode) => this.extractFromAst(file, relPath, content, rootNode, projectRoot, resolver, domainId)
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (fileSlices && fileSlices.length > 0) {
|
|
73
|
+
slices.push(...fileSlices);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return slices;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private extractFromAst(
|
|
81
|
+
_filePath: string,
|
|
82
|
+
relPath: string,
|
|
83
|
+
content: string,
|
|
84
|
+
rootNode: Parser.SyntaxNode,
|
|
85
|
+
projectRoot: string,
|
|
86
|
+
resolver: ImportResolver,
|
|
87
|
+
domainId?: number
|
|
88
|
+
): VerticalSliceCandidate[] {
|
|
89
|
+
const slices: VerticalSliceCandidate[] = [];
|
|
90
|
+
const lines = content.split("\n");
|
|
91
|
+
|
|
92
|
+
// Collect imports in controller file
|
|
93
|
+
const importsMap = new Map<string, string>();
|
|
94
|
+
for (const child of rootNode.namedChildren) {
|
|
95
|
+
if (child.type === "import_statement") {
|
|
96
|
+
const sourceNode = child.childForFieldName("source");
|
|
97
|
+
const specifier = sourceNode ? sourceNode.text.replace(/^['"]|['"]$/g, "") : "";
|
|
98
|
+
if (!specifier) continue;
|
|
99
|
+
const importClause = child.namedChildren.find((c) => c.type === "import_clause");
|
|
100
|
+
if (importClause) {
|
|
101
|
+
for (const ic of importClause.namedChildren) {
|
|
102
|
+
if (ic.type === "identifier") {
|
|
103
|
+
importsMap.set(ic.text.trim(), specifier);
|
|
104
|
+
} else if (ic.type === "namespace_import") {
|
|
105
|
+
const idNode = ic.namedChildren.find((n) => n.type === "identifier");
|
|
106
|
+
if (idNode) importsMap.set(idNode.text.trim(), specifier);
|
|
107
|
+
} else if (ic.type === "named_imports") {
|
|
108
|
+
for (const spec of ic.namedChildren) {
|
|
109
|
+
if (spec.type === "import_specifier") {
|
|
110
|
+
const alias = spec.childForFieldName("alias");
|
|
111
|
+
const name = spec.childForFieldName("name") ?? spec.namedChildren[0];
|
|
112
|
+
const ident = alias ? alias.text.trim() : name ? name.text.trim() : "";
|
|
113
|
+
if (ident) importsMap.set(ident, specifier);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const walkForClasses = (node: Parser.SyntaxNode): void => {
|
|
123
|
+
let classNode: Parser.SyntaxNode | null = null;
|
|
124
|
+
let classDecorators: ParsedDecorator[] = [];
|
|
125
|
+
|
|
126
|
+
if (node.type === "class_declaration") {
|
|
127
|
+
classNode = node;
|
|
128
|
+
classDecorators = node.namedChildren
|
|
129
|
+
.filter((c) => c.type === "decorator")
|
|
130
|
+
.map((c) => this.parseDecorator(c))
|
|
131
|
+
.filter(Boolean) as ParsedDecorator[];
|
|
132
|
+
} else if (node.type === "export_statement") {
|
|
133
|
+
classDecorators = node.namedChildren
|
|
134
|
+
.filter((c) => c.type === "decorator")
|
|
135
|
+
.map((c) => this.parseDecorator(c))
|
|
136
|
+
.filter(Boolean) as ParsedDecorator[];
|
|
137
|
+
const innerClass = node.namedChildren.find((c) => c.type === "class_declaration");
|
|
138
|
+
if (innerClass) {
|
|
139
|
+
classNode = innerClass;
|
|
140
|
+
const innerDecorators = innerClass.namedChildren
|
|
141
|
+
.filter((c) => c.type === "decorator")
|
|
142
|
+
.map((c) => this.parseDecorator(c))
|
|
143
|
+
.filter(Boolean) as ParsedDecorator[];
|
|
144
|
+
classDecorators.push(...innerDecorators);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (classNode) {
|
|
149
|
+
const controllerDec = classDecorators.find((d) => d.name === "Controller");
|
|
150
|
+
if (controllerDec) {
|
|
151
|
+
const rawPrefix = controllerDec.args[0] ?? "";
|
|
152
|
+
const routePrefix = rawPrefix ? `/${rawPrefix.replace(/^\/+|\/+$/g, "")}` : "";
|
|
153
|
+
const nameNode = classNode.namedChildren.find((c) => c.type === "type_identifier" || c.type === "identifier");
|
|
154
|
+
const controllerClass = nameNode ? nameNode.text.trim() : "AnonymousController";
|
|
155
|
+
const classGuards = classDecorators.filter((d) => d.name === "UseGuards").flatMap((d) => d.args);
|
|
156
|
+
|
|
157
|
+
this.processControllerClass(
|
|
158
|
+
classNode,
|
|
159
|
+
controllerClass,
|
|
160
|
+
routePrefix,
|
|
161
|
+
classGuards,
|
|
162
|
+
relPath,
|
|
163
|
+
lines,
|
|
164
|
+
slices,
|
|
165
|
+
importsMap,
|
|
166
|
+
projectRoot,
|
|
167
|
+
resolver,
|
|
168
|
+
domainId
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
for (const child of node.namedChildren) {
|
|
175
|
+
walkForClasses(child);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
walkForClasses(rootNode);
|
|
180
|
+
return slices;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private processControllerClass(
|
|
184
|
+
classNode: Parser.SyntaxNode,
|
|
185
|
+
controllerClass: string,
|
|
186
|
+
routePrefix: string,
|
|
187
|
+
classGuards: string[],
|
|
188
|
+
relPath: string,
|
|
189
|
+
_lines: string[],
|
|
190
|
+
slices: VerticalSliceCandidate[],
|
|
191
|
+
importsMap: Map<string, string>,
|
|
192
|
+
projectRoot: string,
|
|
193
|
+
resolver: ImportResolver,
|
|
194
|
+
domainId?: number
|
|
195
|
+
): void {
|
|
196
|
+
const bodyNode =
|
|
197
|
+
classNode.childForFieldName("body") ?? classNode.namedChildren.find((c) => c.type === "class_body");
|
|
198
|
+
if (!bodyNode) return;
|
|
199
|
+
|
|
200
|
+
// 1. Identify injected services from constructor
|
|
201
|
+
const injectedServices = new Map<string, { type: string; line: number }>();
|
|
202
|
+
|
|
203
|
+
for (const member of bodyNode.namedChildren) {
|
|
204
|
+
if (member.type === "method_definition") {
|
|
205
|
+
const nameNode =
|
|
206
|
+
member.childForFieldName("name") ??
|
|
207
|
+
member.namedChildren.find((c) => c.type === "property_identifier" || c.type === "identifier");
|
|
208
|
+
if (nameNode?.text === "constructor") {
|
|
209
|
+
const paramsNode =
|
|
210
|
+
member.childForFieldName("parameters") ??
|
|
211
|
+
member.namedChildren.find((c) => c.type === "formal_parameters");
|
|
212
|
+
if (paramsNode) {
|
|
213
|
+
for (const param of paramsNode.namedChildren) {
|
|
214
|
+
if (param.type === "required_parameter" || param.type === "optional_parameter") {
|
|
215
|
+
const hasAccessibility = param.namedChildren.some((c) => c.type === "accessibility_modifier");
|
|
216
|
+
const hasReadonly = param.namedChildren.some((c) => c.type === "readonly");
|
|
217
|
+
if (hasAccessibility || hasReadonly) {
|
|
218
|
+
const paramIdNode =
|
|
219
|
+
param.childForFieldName("pattern") ??
|
|
220
|
+
param.childForFieldName("name") ??
|
|
221
|
+
param.namedChildren.find((c) => c.type === "identifier");
|
|
222
|
+
const typeNode =
|
|
223
|
+
param.childForFieldName("type") ??
|
|
224
|
+
param.namedChildren.find((c) => c.type === "type_annotation");
|
|
225
|
+
if (paramIdNode && typeNode) {
|
|
226
|
+
const rawType = typeNode.text.replace(/^:\s*/, "").trim().split(/[<[{]/)[0].trim();
|
|
227
|
+
if (rawType && /^[A-Z]/.test(rawType)) {
|
|
228
|
+
injectedServices.set(paramIdNode.text.trim(), {
|
|
229
|
+
type: rawType,
|
|
230
|
+
line: param.startPosition.row + 1,
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// 2. Extract controller route actions with preceding decorators
|
|
243
|
+
let pendingDecorators: ParsedDecorator[] = [];
|
|
244
|
+
|
|
245
|
+
for (const member of bodyNode.namedChildren) {
|
|
246
|
+
if (member.type === "decorator") {
|
|
247
|
+
const dec = this.parseDecorator(member);
|
|
248
|
+
if (dec) pendingDecorators.push(dec);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (member.type !== "method_definition") {
|
|
253
|
+
pendingDecorators = [];
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const nameNode =
|
|
258
|
+
member.childForFieldName("name") ??
|
|
259
|
+
member.namedChildren.find((c) => c.type === "property_identifier" || c.type === "identifier");
|
|
260
|
+
if (!nameNode || nameNode.text === "constructor") {
|
|
261
|
+
pendingDecorators = [];
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const actionName = nameNode.text.trim();
|
|
266
|
+
const actionLine = member.startPosition.row + 1;
|
|
267
|
+
|
|
268
|
+
// Combine decorators attached to member and preceding decorators in class_body
|
|
269
|
+
const memberAttachedDecorators = member.namedChildren
|
|
270
|
+
.filter((c) => c.type === "decorator")
|
|
271
|
+
.map((c) => this.parseDecorator(c))
|
|
272
|
+
.filter(Boolean) as ParsedDecorator[];
|
|
273
|
+
const methodDecorators = [...pendingDecorators, ...memberAttachedDecorators];
|
|
274
|
+
pendingDecorators = [];
|
|
275
|
+
|
|
276
|
+
const httpDec = methodDecorators.find((d) => NEST_HTTP_METHODS[d.name]);
|
|
277
|
+
if (!httpDec) continue;
|
|
278
|
+
|
|
279
|
+
const httpMethod = NEST_HTTP_METHODS[httpDec.name]!;
|
|
280
|
+
const rawSub = httpDec.args[0] ?? "";
|
|
281
|
+
const cleanSub = rawSub ? `/${rawSub.replace(/^\/+|\/+$/g, "")}` : "";
|
|
282
|
+
const fullUri = `${routePrefix}${cleanSub}` || "/";
|
|
283
|
+
const decoratorLine = httpDec.startLine || actionLine;
|
|
284
|
+
|
|
285
|
+
// Method guards
|
|
286
|
+
const methodGuards = methodDecorators.filter((d) => d.name === "UseGuards").flatMap((d) => d.args);
|
|
287
|
+
const allGuards = Array.from(new Set([...classGuards, ...methodGuards]));
|
|
288
|
+
|
|
289
|
+
// Extract DTO from @Body() parameter
|
|
290
|
+
let validationDto: string | null = null;
|
|
291
|
+
const paramsNode =
|
|
292
|
+
member.childForFieldName("parameters") ??
|
|
293
|
+
member.namedChildren.find((c) => c.type === "formal_parameters");
|
|
294
|
+
if (paramsNode) {
|
|
295
|
+
for (const param of paramsNode.namedChildren) {
|
|
296
|
+
if (param.type === "required_parameter" || param.type === "optional_parameter") {
|
|
297
|
+
const paramDecorators = param.namedChildren
|
|
298
|
+
.filter((c) => c.type === "decorator")
|
|
299
|
+
.map((c) => this.parseDecorator(c))
|
|
300
|
+
.filter(Boolean) as ParsedDecorator[];
|
|
301
|
+
if (paramDecorators.some((d) => d.name === "Body")) {
|
|
302
|
+
const typeNode =
|
|
303
|
+
param.childForFieldName("type") ?? param.namedChildren.find((c) => c.type === "type_annotation");
|
|
304
|
+
if (typeNode) {
|
|
305
|
+
const typeStr = typeNode.text.replace(/^:\s*/, "").trim().split(/[<[{]/)[0].trim();
|
|
306
|
+
const primitiveTypes = new Set(["string", "number", "boolean", "any", "unknown", "object", "Record"]);
|
|
307
|
+
if (typeStr && !primitiveTypes.has(typeStr)) {
|
|
308
|
+
validationDto = typeStr;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// 3. Trace explicit service calls in method body
|
|
317
|
+
interface ExplicitServiceCall {
|
|
318
|
+
symbol: string;
|
|
319
|
+
file: string;
|
|
320
|
+
line: number;
|
|
321
|
+
persistenceNodes: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }>;
|
|
322
|
+
}
|
|
323
|
+
const explicitCalls: ExplicitServiceCall[] = [];
|
|
324
|
+
const methodBody =
|
|
325
|
+
member.childForFieldName("body") ?? member.namedChildren.find((c) => c.type === "statement_block");
|
|
326
|
+
if (methodBody) {
|
|
327
|
+
const walkCalls = (bNode: Parser.SyntaxNode): void => {
|
|
328
|
+
if (bNode.type === "call_expression") {
|
|
329
|
+
const fnNode =
|
|
330
|
+
bNode.childForFieldName("function") ??
|
|
331
|
+
bNode.namedChildren.find((c) => c.type === "member_expression");
|
|
332
|
+
if (fnNode && fnNode.type === "member_expression") {
|
|
333
|
+
const objNode = fnNode.childForFieldName("object") ?? fnNode.namedChildren[0];
|
|
334
|
+
const propNode = fnNode.childForFieldName("property") ?? fnNode.namedChildren[1];
|
|
335
|
+
if (objNode && objNode.type === "member_expression") {
|
|
336
|
+
const subObj = objNode.childForFieldName("object") ?? objNode.namedChildren[0];
|
|
337
|
+
const subProp = objNode.childForFieldName("property") ?? objNode.namedChildren[1];
|
|
338
|
+
if (subObj?.text === "this" && subProp && injectedServices.has(subProp.text)) {
|
|
339
|
+
const svc = injectedServices.get(subProp.text)!;
|
|
340
|
+
const callMethod = propNode ? propNode.text.trim() : "action";
|
|
341
|
+
let serviceFile: string | undefined;
|
|
342
|
+
let serviceLine = bNode.startPosition.row + 1;
|
|
343
|
+
const persistenceNodes: Array<{
|
|
344
|
+
stage: string;
|
|
345
|
+
symbol: string;
|
|
346
|
+
file?: string;
|
|
347
|
+
line?: number;
|
|
348
|
+
description?: string;
|
|
349
|
+
}> = [];
|
|
350
|
+
|
|
351
|
+
const importSpec = importsMap.get(svc.type);
|
|
352
|
+
if (importSpec) {
|
|
353
|
+
const res = resolver.resolve(importSpec, path.resolve(projectRoot, relPath));
|
|
354
|
+
if (res.status === "resolved" && res.targets[0]?.files?.[0]) {
|
|
355
|
+
serviceFile = res.targets[0].files[0];
|
|
356
|
+
const fullServicePath = path.resolve(projectRoot, serviceFile);
|
|
357
|
+
const serviceContent = readOptionalSourceFile(fullServicePath, "utf-8");
|
|
358
|
+
if (serviceContent) {
|
|
359
|
+
const mMatch = new RegExp(`(?:async\\s+)?(?:public\\s+)?\\b${callMethod}\\s*\\(`).exec(serviceContent);
|
|
360
|
+
if (mMatch) {
|
|
361
|
+
serviceLine = serviceContent.slice(0, mMatch.index).split("\n").length;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Inspect for TypeORM / Prisma / Mongoose persistence
|
|
365
|
+
const typeOrmMatch = serviceContent.match(
|
|
366
|
+
/@InjectRepository\s*\(\s*([A-Za-z0-9_]+)\s*\)|Repository<\s*([A-Za-z0-9_]+)\s*>/
|
|
367
|
+
);
|
|
368
|
+
const prismaMatch = serviceContent.match(
|
|
369
|
+
/this\.prisma\.([a-zA-Z0-9_]+)\.(?:find|create|update|delete|upsert)/
|
|
370
|
+
);
|
|
371
|
+
const mongooseMatch = serviceContent.match(/@InjectModel\s*\(\s*([A-Za-z0-9_]+)/);
|
|
372
|
+
const arMatch = serviceContent.match(
|
|
373
|
+
/\b([A-Z][a-zA-Z0-9_]+)\.(?:save|find|findOne|create|update|delete)\s*\(/
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
const entityName =
|
|
377
|
+
typeOrmMatch ? (typeOrmMatch[1] || typeOrmMatch[2]) :
|
|
378
|
+
prismaMatch ? (prismaMatch[1].charAt(0).toUpperCase() + prismaMatch[1].slice(1)) :
|
|
379
|
+
mongooseMatch ? mongooseMatch[1] :
|
|
380
|
+
arMatch && arMatch[1] !== svc.type ? arMatch[1] : undefined;
|
|
381
|
+
|
|
382
|
+
if (entityName) {
|
|
383
|
+
let entityFile: string | undefined;
|
|
384
|
+
const entImportMatch = serviceContent.match(
|
|
385
|
+
new RegExp(
|
|
386
|
+
`import\\s+(?:{[^}]*\\b${entityName}\\b[^}]*}|\\b${entityName}\\b)\\s+from\\s+['"]([^'"]+)['"]`
|
|
387
|
+
)
|
|
388
|
+
);
|
|
389
|
+
if (entImportMatch) {
|
|
390
|
+
const eRes = resolver.resolve(entImportMatch[1], fullServicePath);
|
|
391
|
+
if (eRes.status === "resolved" && eRes.targets[0]?.files?.[0]) {
|
|
392
|
+
entityFile = eRes.targets[0].files[0];
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
persistenceNodes.push({
|
|
397
|
+
stage: "repository",
|
|
398
|
+
symbol: `${entityName}Repository`,
|
|
399
|
+
file: serviceFile,
|
|
400
|
+
description: "TypeORM / Persistence Repository",
|
|
401
|
+
});
|
|
402
|
+
persistenceNodes.push({
|
|
403
|
+
stage: "entity",
|
|
404
|
+
symbol: entityName,
|
|
405
|
+
file: entityFile || serviceFile,
|
|
406
|
+
description: "Database Entity / Persistence Layer",
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
explicitCalls.push({
|
|
414
|
+
symbol: `${svc.type}::${callMethod}`,
|
|
415
|
+
file: serviceFile || relPath,
|
|
416
|
+
line: serviceFile ? serviceLine : bNode.startPosition.row + 1,
|
|
417
|
+
persistenceNodes,
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
for (const c of bNode.namedChildren) {
|
|
424
|
+
walkCalls(c);
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
walkCalls(methodBody);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// 4. Construct execution chain
|
|
431
|
+
const chain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
|
|
432
|
+
{
|
|
433
|
+
stage: "ingress",
|
|
434
|
+
symbol: `${httpMethod} ${fullUri}`,
|
|
435
|
+
file: relPath,
|
|
436
|
+
line: decoratorLine,
|
|
437
|
+
description: `NestJS Route: ${httpMethod} ${fullUri}`,
|
|
438
|
+
},
|
|
439
|
+
];
|
|
440
|
+
|
|
441
|
+
for (const guard of allGuards) {
|
|
442
|
+
chain.push({
|
|
443
|
+
stage: "guard",
|
|
444
|
+
symbol: guard,
|
|
445
|
+
file: relPath,
|
|
446
|
+
line: actionLine,
|
|
447
|
+
description: `NestJS Guard: ${guard}`,
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (validationDto) {
|
|
452
|
+
chain.push({
|
|
453
|
+
stage: "validation",
|
|
454
|
+
symbol: validationDto,
|
|
455
|
+
description: "DTO Validation",
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
chain.push({
|
|
460
|
+
stage: "controller",
|
|
461
|
+
symbol: `${controllerClass}::${actionName}`,
|
|
462
|
+
file: relPath,
|
|
463
|
+
line: actionLine,
|
|
464
|
+
description: `NestJS Controller Action`,
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
for (const call of explicitCalls) {
|
|
468
|
+
chain.push({
|
|
469
|
+
stage: "service",
|
|
470
|
+
symbol: call.symbol,
|
|
471
|
+
file: call.file,
|
|
472
|
+
line: call.line,
|
|
473
|
+
description: `Injected Service Call`,
|
|
474
|
+
});
|
|
475
|
+
for (const pNode of call.persistenceNodes) {
|
|
476
|
+
chain.push(pNode);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
slices.push({
|
|
481
|
+
domain_id: domainId ?? null,
|
|
482
|
+
feature_key: `nest.${controllerClass.toLowerCase().replace("controller", "")}.${actionName}`,
|
|
483
|
+
http_method: httpMethod,
|
|
484
|
+
route_uri: fullUri,
|
|
485
|
+
route_name: `${controllerClass}.${actionName}`,
|
|
486
|
+
controller_class: controllerClass,
|
|
487
|
+
action_name: actionName,
|
|
488
|
+
controller_file: relPath,
|
|
489
|
+
controller_line: actionLine,
|
|
490
|
+
architecture_style: "mvc",
|
|
491
|
+
entry_kind: "http_route",
|
|
492
|
+
execution_chain_json: JSON.stringify(chain),
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
private parseDecorator(node: Parser.SyntaxNode): ParsedDecorator | null {
|
|
498
|
+
if (node.type !== "decorator") return null;
|
|
499
|
+
const startLine = node.startPosition.row + 1;
|
|
500
|
+
const first = node.namedChildren[0];
|
|
501
|
+
if (!first) return null;
|
|
502
|
+
|
|
503
|
+
if (first.type === "identifier") {
|
|
504
|
+
return { name: first.text.trim(), args: [], startLine };
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (first.type === "call_expression") {
|
|
508
|
+
const fnNode = first.childForFieldName("function") ?? first.namedChildren[0];
|
|
509
|
+
const name = fnNode ? fnNode.text.trim() : "";
|
|
510
|
+
const argsNode = first.childForFieldName("arguments") ?? first.namedChildren.find((c) => c.type === "arguments");
|
|
511
|
+
const args: string[] = [];
|
|
512
|
+
|
|
513
|
+
if (argsNode) {
|
|
514
|
+
for (const arg of argsNode.namedChildren) {
|
|
515
|
+
if (arg.type === "string" || arg.type === "template_string") {
|
|
516
|
+
args.push(arg.text.replace(/^['"`]|['"`]$/g, "").trim());
|
|
517
|
+
} else {
|
|
518
|
+
args.push(arg.text.trim());
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return { name, args, startLine };
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return null;
|
|
526
|
+
}
|
|
527
|
+
}
|